blob: 31462b30127ed5da4127eb319e9084d54d718267 [file] [log] [blame]
wdenk5d5317e2003-12-07 00:46:27 +00001/*
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
Jon Loeligerd8c86302007-06-11 19:02:10 -050037#if (CONFIG_COMMANDS & CFG_CMD_NET) || defined(CONFIG_CMD_NET)
wdenk5d5317e2003-12-07 00:46:27 +000038
39
40/* packet page register access functions */
41
42
43static unsigned char get_reg (unsigned int regno)
44{
45 return (*(unsigned char *) regno);
46}
47
48
49static void put_reg (unsigned int regno, unsigned char val)
50{
51 *(volatile unsigned char *) regno = val;
52}
53
54static void eth_reset (void)
55{
56 unsigned char ucTemp;
57
58 /* reset NIC */
59 ucTemp = get_reg (RTL8019_RESET);
60 put_reg (RTL8019_RESET, ucTemp);
61 put_reg (RTL8019_INTERRUPTSTATUS, 0xff);
62 udelay (2000); /* wait for 2ms */
63}
64
65void rtl8019_get_enetaddr (uchar * addr)
66{
67 unsigned char i;
68 unsigned char temp;
69
70 eth_reset ();
71
72 put_reg (RTL8019_COMMAND, RTL8019_REMOTEDMARD);
73 put_reg (RTL8019_DATACONFIGURATION, 0x48);
74 put_reg (RTL8019_REMOTESTARTADDRESS0, 0x00);
75 put_reg (RTL8019_REMOTESTARTADDRESS1, 0x00);
76 put_reg (RTL8019_REMOTEBYTECOUNT0, 12);
77 put_reg (RTL8019_REMOTEBYTECOUNT1, 0x00);
78 put_reg (RTL8019_COMMAND, RTL8019_REMOTEDMARD);
79 printf ("MAC: ");
80 for (i = 0; i < 6; i++) {
81 temp = get_reg (RTL8019_DMA_DATA);
82 *addr++ = temp;
83 temp = get_reg (RTL8019_DMA_DATA);
84 printf ("%x:", temp);
85 }
86
87 while ((!get_reg (RTL8019_INTERRUPTSTATUS) & 0x40));
88 printf ("\b \n");
89 put_reg (RTL8019_REMOTEBYTECOUNT0, 0x00);
90 put_reg (RTL8019_REMOTEBYTECOUNT1, 0x00);
91 put_reg (RTL8019_COMMAND, RTL8019_PAGE0);
92}
93
94
95void eth_halt (void)
96{
97 put_reg (RTL8019_COMMAND, 0x01);
98}
99
100int eth_init (bd_t * bd)
101{
102 eth_reset ();
103 put_reg (RTL8019_COMMAND, RTL8019_PAGE0STOP);
104 put_reg (RTL8019_DATACONFIGURATION, 0x48);
105 put_reg (RTL8019_REMOTEBYTECOUNT0, 0x00);
106 put_reg (RTL8019_REMOTEBYTECOUNT1, 0x00);
107 put_reg (RTL8019_RECEIVECONFIGURATION, 0x00); /*00; */
108 put_reg (RTL8019_TRANSMITPAGE, RTL8019_TPSTART);
109 put_reg (RTL8019_TRANSMITCONFIGURATION, 0x02);
110 put_reg (RTL8019_PAGESTART, RTL8019_PSTART);
111 put_reg (RTL8019_BOUNDARY, RTL8019_PSTART);
112 put_reg (RTL8019_PAGESTOP, RTL8019_PSTOP);
113 put_reg (RTL8019_INTERRUPTSTATUS, 0xff);
114 put_reg (RTL8019_INTERRUPTMASK, 0x11); /*b; */
115 put_reg (RTL8019_COMMAND, RTL8019_PAGE1STOP);
116 put_reg (RTL8019_PHYSICALADDRESS0, bd->bi_enetaddr[0]);
117 put_reg (RTL8019_PHYSICALADDRESS1, bd->bi_enetaddr[1]);
118 put_reg (RTL8019_PHYSICALADDRESS2, bd->bi_enetaddr[2]);
119 put_reg (RTL8019_PHYSICALADDRESS3, bd->bi_enetaddr[3]);
120 put_reg (RTL8019_PHYSICALADDRESS4, bd->bi_enetaddr[4]);
121 put_reg (RTL8019_PHYSICALADDRESS5, bd->bi_enetaddr[5]);
122 put_reg (RTL8019_MULTIADDRESS0, 0x00);
123 put_reg (RTL8019_MULTIADDRESS1, 0x00);
124 put_reg (RTL8019_MULTIADDRESS2, 0x00);
125 put_reg (RTL8019_MULTIADDRESS3, 0x00);
126 put_reg (RTL8019_MULTIADDRESS4, 0x00);
127 put_reg (RTL8019_MULTIADDRESS5, 0x00);
128 put_reg (RTL8019_MULTIADDRESS6, 0x00);
129 put_reg (RTL8019_MULTIADDRESS7, 0x00);
130 put_reg (RTL8019_CURRENT, RTL8019_PSTART);
131 put_reg (RTL8019_COMMAND, RTL8019_PAGE0);
132 put_reg (RTL8019_TRANSMITCONFIGURATION, 0xe0); /*58; */
133
134 return 0;
135}
136
137
138static unsigned char nic_to_pc (void)
139{
140 unsigned char rec_head_status;
141 unsigned char next_packet_pointer;
142 unsigned char packet_length0;
143 unsigned char packet_length1;
144 unsigned short rxlen = 0;
145 unsigned int i = 4;
146 unsigned char current_point;
147 unsigned char *addr;
148
149 /*
150 * The RTL8019's first 4B is packet status,page of next packet
151 * and packet length(2B).So we receive the fist 4B.
152 */
153 put_reg (RTL8019_REMOTESTARTADDRESS1, get_reg (RTL8019_BOUNDARY));
154 put_reg (RTL8019_REMOTESTARTADDRESS0, 0x00);
155 put_reg (RTL8019_REMOTEBYTECOUNT1, 0x00);
156 put_reg (RTL8019_REMOTEBYTECOUNT0, 0x04);
157
158 put_reg (RTL8019_COMMAND, RTL8019_REMOTEDMARD);
159
160 rec_head_status = get_reg (RTL8019_DMA_DATA);
161 next_packet_pointer = get_reg (RTL8019_DMA_DATA);
162 packet_length0 = get_reg (RTL8019_DMA_DATA);
163 packet_length1 = get_reg (RTL8019_DMA_DATA);
164
165 put_reg (RTL8019_COMMAND, RTL8019_PAGE0);
166 /*Packet length is in two 8bit registers */
167 rxlen = packet_length1;
168 rxlen = (((rxlen << 8) & 0xff00) + packet_length0);
169 rxlen -= 4;
170
171 if (rxlen > PKTSIZE_ALIGN + PKTALIGN)
172 printf ("packet too big!\n");
173
174 /*Receive the packet */
175 put_reg (RTL8019_REMOTESTARTADDRESS0, 0x04);
176 put_reg (RTL8019_REMOTESTARTADDRESS1, get_reg (RTL8019_BOUNDARY));
177
178 put_reg (RTL8019_REMOTEBYTECOUNT0, (rxlen & 0xff));
179 put_reg (RTL8019_REMOTEBYTECOUNT1, ((rxlen >> 8) & 0xff));
180
181
182 put_reg (RTL8019_COMMAND, RTL8019_REMOTEDMARD);
183
184 for (addr = (unsigned char *) NetRxPackets[0], i = rxlen; i > 0; i--)
185 *addr++ = get_reg (RTL8019_DMA_DATA);
186 /* Pass the packet up to the protocol layers. */
187 NetReceive (NetRxPackets[0], rxlen);
188
189 while (!(get_reg (RTL8019_INTERRUPTSTATUS)) & 0x40); /* wait for the op. */
190
191 /*
192 * To test whether the packets are all received,get the
193 * location of current point
194 */
195 put_reg (RTL8019_COMMAND, RTL8019_PAGE1);
196 current_point = get_reg (RTL8019_CURRENT);
197 put_reg (RTL8019_COMMAND, RTL8019_PAGE0);
198 put_reg (RTL8019_BOUNDARY, next_packet_pointer);
199 return current_point;
200}
201
202/* Get a data block via Ethernet */
203extern int eth_rx (void)
204{
205 unsigned char temp, current_point;
206
207 put_reg (RTL8019_COMMAND, RTL8019_PAGE0);
208
209 while (1) {
210 temp = get_reg (RTL8019_INTERRUPTSTATUS);
211
212 if (temp & 0x90) {
213 /*overflow */
214 put_reg (RTL8019_COMMAND, RTL8019_PAGE0STOP);
215 udelay (2000);
216 put_reg (RTL8019_REMOTEBYTECOUNT0, 0);
217 put_reg (RTL8019_REMOTEBYTECOUNT1, 0);
218 put_reg (RTL8019_TRANSMITCONFIGURATION, 2);
219 do {
220 current_point = nic_to_pc ();
221 } while (get_reg (RTL8019_BOUNDARY) != current_point);
222
223 put_reg (RTL8019_TRANSMITCONFIGURATION, 0xe0);
224 }
225
226 if (temp & 0x1) {
227 /*packet received */
228 do {
229 put_reg (RTL8019_INTERRUPTSTATUS, 0x01);
230 current_point = nic_to_pc ();
231 } while (get_reg (RTL8019_BOUNDARY) != current_point);
232 }
233
234 if (!(temp & 0x1))
235 return 0;
236 /* done and exit. */
237 }
238}
239
240/* Send a data block via Ethernet. */
241extern int eth_send (volatile void *packet, int length)
242{
243 volatile unsigned char *p;
244 unsigned int pn;
245
246 pn = length;
247 p = (volatile unsigned char *) packet;
248
249 while (get_reg (RTL8019_COMMAND) == RTL8019_TRANSMIT);
250
251 put_reg (RTL8019_REMOTESTARTADDRESS0, 0);
252 put_reg (RTL8019_REMOTESTARTADDRESS1, RTL8019_TPSTART);
253 put_reg (RTL8019_REMOTEBYTECOUNT0, (pn & 0xff));
254 put_reg (RTL8019_REMOTEBYTECOUNT1, ((pn >> 8) & 0xff));
255
256 put_reg (RTL8019_COMMAND, RTL8019_REMOTEDMAWR);
257 while (pn > 0) {
258 put_reg (RTL8019_DMA_DATA, *p++);
259 pn--;
260 }
261
262 pn = length;
263
264 while (pn < 60) { /*Padding */
265 put_reg (RTL8019_DMA_DATA, 0);
266 pn++;
267 }
268
269 while (!(get_reg (RTL8019_INTERRUPTSTATUS)) & 0x40);
270
271 put_reg (RTL8019_INTERRUPTSTATUS, 0x40);
272 put_reg (RTL8019_TRANSMITPAGE, RTL8019_TPSTART);
273 put_reg (RTL8019_TRANSMITBYTECOUNT0, (pn & 0xff));
274 put_reg (RTL8019_TRANSMITBYTECOUNT1, ((pn >> 8 & 0xff)));
275 put_reg (RTL8019_COMMAND, RTL8019_TRANSMIT);
276
277 return 0;
278}
279
280#endif /* COMMANDS & CFG_NET */
281
282#endif /* CONFIG_DRIVER_RTL8019 */