]> git.kernelconcepts.de Git - karo-tx-uboot.git/blob - drivers/net/rtl8019.c
Merge commit 'wd/master'
[karo-tx-uboot.git] / drivers / net / rtl8019.c
1 /*
2  * Realtek 8019AS Ethernet
3  * (C) Copyright 2002-2003
4  * Xue Ligong(lgxue@hotmail.com),Wang Kehao, ESLAB, whut.edu.cn
5  *
6  * See file CREDITS for list of people who contributed to this
7  * project.
8  *
9  * This program is free software; you can redistribute it and/or
10  * modify it under the terms of the GNU General Public License as
11  * published by the Free Software Foundation; either version 2 of
12  * the License, or (at your option) any later version.
13  *
14  * This program is distributed in the hope that it will be useful,
15  * but WITHOUT ANY WARRANTY; without even the implied warranty of
16  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
17  * GNU General Public License for more details.
18  *
19  * You should have received a copy of the GNU General Public License
20  * along with this program; if not, write to the Free Software
21  * Foundation, Inc., 59 Temple Place, Suite 330, Boston,
22  * MA 02111-1307 USA
23  */
24
25 /*
26  * This code works in 8bit mode.
27  * If you need to work in 16bit mode, PLS change it!
28  */
29
30 #include <common.h>
31 #include <command.h>
32 #include "rtl8019.h"
33 #include <net.h>
34
35 #ifdef CONFIG_DRIVER_RTL8019
36
37 #if defined(CONFIG_CMD_NET)
38
39 /* packet page register access functions */
40
41 static unsigned char get_reg (unsigned int regno)
42 {
43         return (*(unsigned char *) regno);
44 }
45
46 static void put_reg (unsigned int regno, unsigned char val)
47 {
48         *(volatile unsigned char *) regno = val;
49 }
50
51 static void eth_reset (void)
52 {
53         unsigned char ucTemp;
54
55         /* reset NIC */
56         ucTemp = get_reg (RTL8019_RESET);
57         put_reg (RTL8019_RESET, ucTemp);
58         put_reg (RTL8019_INTERRUPTSTATUS, 0xff);
59         udelay (2000);          /* wait for 2ms */
60 }
61
62 void rtl8019_get_enetaddr (uchar * addr)
63 {
64         unsigned char i;
65         unsigned char temp;
66
67         eth_reset ();
68
69         put_reg (RTL8019_COMMAND, RTL8019_REMOTEDMARD);
70         put_reg (RTL8019_DATACONFIGURATION, 0x48);
71         put_reg (RTL8019_REMOTESTARTADDRESS0, 0x00);
72         put_reg (RTL8019_REMOTESTARTADDRESS1, 0x00);
73         put_reg (RTL8019_REMOTEBYTECOUNT0, 12);
74         put_reg (RTL8019_REMOTEBYTECOUNT1, 0x00);
75         put_reg (RTL8019_COMMAND, RTL8019_REMOTEDMARD);
76         printf ("MAC: ");
77         for (i = 0; i < 6; i++) {
78                 temp = get_reg (RTL8019_DMA_DATA);
79                 *addr++ = temp;
80                 temp = get_reg (RTL8019_DMA_DATA);
81                 printf ("%x:", temp);
82         }
83
84         while ((!get_reg (RTL8019_INTERRUPTSTATUS) & 0x40));
85         printf ("\b \n");
86         put_reg (RTL8019_REMOTEBYTECOUNT0, 0x00);
87         put_reg (RTL8019_REMOTEBYTECOUNT1, 0x00);
88         put_reg (RTL8019_COMMAND, RTL8019_PAGE0);
89 }
90
91 void eth_halt (void)
92 {
93         put_reg (RTL8019_COMMAND, 0x01);
94 }
95
96 int eth_init (bd_t * bd)
97 {
98         eth_reset ();
99         put_reg (RTL8019_COMMAND, RTL8019_PAGE0STOP);
100         put_reg (RTL8019_DATACONFIGURATION, 0x48);
101         put_reg (RTL8019_REMOTEBYTECOUNT0, 0x00);
102         put_reg (RTL8019_REMOTEBYTECOUNT1, 0x00);
103         put_reg (RTL8019_RECEIVECONFIGURATION, 0x00);   /*00; */
104         put_reg (RTL8019_TRANSMITPAGE, RTL8019_TPSTART);
105         put_reg (RTL8019_TRANSMITCONFIGURATION, 0x02);
106         put_reg (RTL8019_PAGESTART, RTL8019_PSTART);
107         put_reg (RTL8019_BOUNDARY, RTL8019_PSTART);
108         put_reg (RTL8019_PAGESTOP, RTL8019_PSTOP);
109         put_reg (RTL8019_INTERRUPTSTATUS, 0xff);
110         put_reg (RTL8019_INTERRUPTMASK, 0x11);  /*b; */
111         put_reg (RTL8019_COMMAND, RTL8019_PAGE1STOP);
112         put_reg (RTL8019_PHYSICALADDRESS0, bd->bi_enetaddr[0]);
113         put_reg (RTL8019_PHYSICALADDRESS1, bd->bi_enetaddr[1]);
114         put_reg (RTL8019_PHYSICALADDRESS2, bd->bi_enetaddr[2]);
115         put_reg (RTL8019_PHYSICALADDRESS3, bd->bi_enetaddr[3]);
116         put_reg (RTL8019_PHYSICALADDRESS4, bd->bi_enetaddr[4]);
117         put_reg (RTL8019_PHYSICALADDRESS5, bd->bi_enetaddr[5]);
118         put_reg (RTL8019_MULTIADDRESS0, 0x00);
119         put_reg (RTL8019_MULTIADDRESS1, 0x00);
120         put_reg (RTL8019_MULTIADDRESS2, 0x00);
121         put_reg (RTL8019_MULTIADDRESS3, 0x00);
122         put_reg (RTL8019_MULTIADDRESS4, 0x00);
123         put_reg (RTL8019_MULTIADDRESS5, 0x00);
124         put_reg (RTL8019_MULTIADDRESS6, 0x00);
125         put_reg (RTL8019_MULTIADDRESS7, 0x00);
126         put_reg (RTL8019_CURRENT, RTL8019_PSTART);
127         put_reg (RTL8019_COMMAND, RTL8019_PAGE0);
128         put_reg (RTL8019_TRANSMITCONFIGURATION, 0xe0);  /*58; */
129
130         return 0;
131 }
132
133 static unsigned char nic_to_pc (void)
134 {
135         unsigned char rec_head_status;
136         unsigned char next_packet_pointer;
137         unsigned char packet_length0;
138         unsigned char packet_length1;
139         unsigned short rxlen = 0;
140         unsigned int i = 4;
141         unsigned char current_point;
142         unsigned char *addr;
143
144         /*
145          * The RTL8019's first 4B is packet status,page of next packet
146          * and packet length(2B).So we receive the fist 4B.
147          */
148         put_reg (RTL8019_REMOTESTARTADDRESS1, get_reg (RTL8019_BOUNDARY));
149         put_reg (RTL8019_REMOTESTARTADDRESS0, 0x00);
150         put_reg (RTL8019_REMOTEBYTECOUNT1, 0x00);
151         put_reg (RTL8019_REMOTEBYTECOUNT0, 0x04);
152
153         put_reg (RTL8019_COMMAND, RTL8019_REMOTEDMARD);
154
155         rec_head_status = get_reg (RTL8019_DMA_DATA);
156         next_packet_pointer = get_reg (RTL8019_DMA_DATA);
157         packet_length0 = get_reg (RTL8019_DMA_DATA);
158         packet_length1 = get_reg (RTL8019_DMA_DATA);
159
160         put_reg (RTL8019_COMMAND, RTL8019_PAGE0);
161         /*Packet length is in two 8bit registers */
162         rxlen = packet_length1;
163         rxlen = (((rxlen << 8) & 0xff00) + packet_length0);
164         rxlen -= 4;
165
166         if (rxlen > PKTSIZE_ALIGN + PKTALIGN)
167                 printf ("packet too big!\n");
168
169         /*Receive the packet */
170         put_reg (RTL8019_REMOTESTARTADDRESS0, 0x04);
171         put_reg (RTL8019_REMOTESTARTADDRESS1, get_reg (RTL8019_BOUNDARY));
172
173         put_reg (RTL8019_REMOTEBYTECOUNT0, (rxlen & 0xff));
174         put_reg (RTL8019_REMOTEBYTECOUNT1, ((rxlen >> 8) & 0xff));
175
176
177         put_reg (RTL8019_COMMAND, RTL8019_REMOTEDMARD);
178
179         for (addr = (unsigned char *) NetRxPackets[0], i = rxlen; i > 0; i--)
180                 *addr++ = get_reg (RTL8019_DMA_DATA);
181         /* Pass the packet up to the protocol layers. */
182         NetReceive (NetRxPackets[0], rxlen);
183
184         while (!(get_reg (RTL8019_INTERRUPTSTATUS)) & 0x40);    /* wait for the op. */
185
186         /*
187          * To test whether the packets are all received,get the
188          * location of current point
189          */
190         put_reg (RTL8019_COMMAND, RTL8019_PAGE1);
191         current_point = get_reg (RTL8019_CURRENT);
192         put_reg (RTL8019_COMMAND, RTL8019_PAGE0);
193         put_reg (RTL8019_BOUNDARY, next_packet_pointer);
194         return current_point;
195 }
196
197 /* Get a data block via Ethernet */
198 extern int eth_rx (void)
199 {
200         unsigned char temp, current_point;
201
202         put_reg (RTL8019_COMMAND, RTL8019_PAGE0);
203
204         while (1) {
205                 temp = get_reg (RTL8019_INTERRUPTSTATUS);
206
207                 if (temp & 0x90) {
208                         /*overflow */
209                         put_reg (RTL8019_COMMAND, RTL8019_PAGE0STOP);
210                         udelay (2000);
211                         put_reg (RTL8019_REMOTEBYTECOUNT0, 0);
212                         put_reg (RTL8019_REMOTEBYTECOUNT1, 0);
213                         put_reg (RTL8019_TRANSMITCONFIGURATION, 2);
214                         do {
215                                 current_point = nic_to_pc ();
216                         } while (get_reg (RTL8019_BOUNDARY) != current_point);
217
218                         put_reg (RTL8019_TRANSMITCONFIGURATION, 0xe0);
219                 }
220
221                 if (temp & 0x1) {
222                         /*packet received */
223                         do {
224                                 put_reg (RTL8019_INTERRUPTSTATUS, 0x01);
225                                 current_point = nic_to_pc ();
226                         } while (get_reg (RTL8019_BOUNDARY) != current_point);
227                 }
228
229                 if (!(temp & 0x1))
230                         return 0;
231                 /* done and exit. */
232         }
233 }
234
235 /* Send a data block via Ethernet. */
236 extern int eth_send (volatile void *packet, int length)
237 {
238         volatile unsigned char *p;
239         unsigned int pn;
240
241         pn = length;
242         p = (volatile unsigned char *) packet;
243
244         while (get_reg (RTL8019_COMMAND) == RTL8019_TRANSMIT);
245
246         put_reg (RTL8019_REMOTESTARTADDRESS0, 0);
247         put_reg (RTL8019_REMOTESTARTADDRESS1, RTL8019_TPSTART);
248         put_reg (RTL8019_REMOTEBYTECOUNT0, (pn & 0xff));
249         put_reg (RTL8019_REMOTEBYTECOUNT1, ((pn >> 8) & 0xff));
250
251         put_reg (RTL8019_COMMAND, RTL8019_REMOTEDMAWR);
252         while (pn > 0) {
253                 put_reg (RTL8019_DMA_DATA, *p++);
254                 pn--;
255         }
256
257         pn = length;
258
259         while (pn < 60) {       /*Padding */
260                 put_reg (RTL8019_DMA_DATA, 0);
261                 pn++;
262         }
263
264         while (!(get_reg (RTL8019_INTERRUPTSTATUS)) & 0x40);
265
266         put_reg (RTL8019_INTERRUPTSTATUS, 0x40);
267         put_reg (RTL8019_TRANSMITPAGE, RTL8019_TPSTART);
268         put_reg (RTL8019_TRANSMITBYTECOUNT0, (pn & 0xff));
269         put_reg (RTL8019_TRANSMITBYTECOUNT1, ((pn >> 8 & 0xff)));
270         put_reg (RTL8019_COMMAND, RTL8019_TRANSMIT);
271
272         return 0;
273 }
274
275 #endif /* CONFIG_CMD_NET */
276
277 #endif /* CONFIG_DRIVER_RTL8019 */