blob: cb68d45e0275f24cc49b076594a293d1e1f4a356 [file] [log] [blame]
Daniel Hellstromf1431792008-03-28 20:22:53 +01001/* Gaisler.com GRETH 10/100/1000 Ethernet MAC driver
2 *
3 * Driver use polling mode (no Interrupt)
4 *
5 * (C) Copyright 2007
6 * Daniel Hellstrom, Gaisler Research, daniel@gaisler.com
7 *
8 * See file CREDITS for list of people who contributed to this
9 * project.
10 *
11 * This program is free software; you can redistribute it and/or
12 * modify it under the terms of the GNU General Public License as
13 * published by the Free Software Foundation; either version 2 of
14 * the License, or (at your option) any later version.
15 *
16 * This program is distributed in the hope that it will be useful,
17 * but WITHOUT ANY WARRANTY; without even the implied warranty of
18 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
19 * GNU General Public License for more details.
20 *
21 * You should have received a copy of the GNU General Public License
22 * along with this program; if not, write to the Free Software
23 * Foundation, Inc., 59 Temple Place, Suite 330, Boston,
24 * MA 02111-1307 USA
25 */
26
Daniel Hellstromebb753c2010-10-27 09:24:13 +020027/* #define DEBUG */
28
Daniel Hellstromf1431792008-03-28 20:22:53 +010029#include <common.h>
30#include <command.h>
31#include <net.h>
Ben Warren2f2b6b62008-08-31 22:22:04 -070032#include <netdev.h>
Daniel Hellstromf1431792008-03-28 20:22:53 +010033#include <malloc.h>
34#include <asm/processor.h>
35#include <ambapp.h>
36#include <asm/leon.h>
37
Daniel Hellstromf1431792008-03-28 20:22:53 +010038#include "greth.h"
39
40/* Default to 3s timeout on autonegotiation */
41#ifndef GRETH_PHY_TIMEOUT_MS
42#define GRETH_PHY_TIMEOUT_MS 3000
43#endif
44
Daniel Hellstrom9ae3be82010-10-22 11:26:49 +020045/* Default to PHY adrress 0 not not specified */
46#ifdef CONFIG_SYS_GRLIB_GRETH_PHYADDR
47#define GRETH_PHY_ADR_DEFAULT CONFIG_SYS_GRLIB_GRETH_PHYADDR
48#else
49#define GRETH_PHY_ADR_DEFAULT 0
50#endif
51
Daniel Hellstromf1431792008-03-28 20:22:53 +010052/* ByPass Cache when reading regs */
53#define GRETH_REGLOAD(addr) SPARC_NOCACHE_READ(addr)
54/* Write-through cache ==> no bypassing needed on writes */
Daniel Hellstrom2a860eb2010-10-27 09:39:46 +020055#define GRETH_REGSAVE(addr,data) (*(volatile unsigned int *)(addr) = (data))
Daniel Hellstromf1431792008-03-28 20:22:53 +010056#define GRETH_REGORIN(addr,data) GRETH_REGSAVE(addr,GRETH_REGLOAD(addr)|data)
57#define GRETH_REGANDIN(addr,data) GRETH_REGSAVE(addr,GRETH_REGLOAD(addr)&data)
58
59#define GRETH_RXBD_CNT 4
60#define GRETH_TXBD_CNT 1
61
62#define GRETH_RXBUF_SIZE 1540
63#define GRETH_BUF_ALIGN 4
64#define GRETH_RXBUF_EFF_SIZE \
65 ( (GRETH_RXBUF_SIZE&~(GRETH_BUF_ALIGN-1))+GRETH_BUF_ALIGN )
66
67typedef struct {
68 greth_regs *regs;
69 int irq;
70 struct eth_device *dev;
71
72 /* Hardware info */
73 unsigned char phyaddr;
74 int gbit_mac;
75
76 /* Current operating Mode */
77 int gb; /* GigaBit */
78 int fd; /* Full Duplex */
79 int sp; /* 10/100Mbps speed (1=100,0=10) */
80 int auto_neg; /* Auto negotiate done */
81
82 unsigned char hwaddr[6]; /* MAC Address */
83
84 /* Descriptors */
85 greth_bd *rxbd_base, *rxbd_max;
86 greth_bd *txbd_base, *txbd_max;
87
88 greth_bd *rxbd_curr;
89
90 /* rx buffers in rx descriptors */
91 void *rxbuf_base; /* (GRETH_RXBUF_SIZE+ALIGNBYTES) * GRETH_RXBD_CNT */
92
93 /* unused for gbit_mac, temp buffer for sending packets with unligned
94 * start.
95 * Pointer to packet allocated with malloc.
96 */
97 void *txbuf;
98
99 struct {
100 /* rx status */
101 unsigned int rx_packets,
102 rx_crc_errors, rx_frame_errors, rx_length_errors, rx_errors;
103
104 /* tx stats */
105 unsigned int tx_packets,
106 tx_latecol_errors,
107 tx_underrun_errors, tx_limit_errors, tx_errors;
108 } stats;
109} greth_priv;
110
111/* Read MII register 'addr' from core 'regs' */
Daniel Hellstrom9ae3be82010-10-22 11:26:49 +0200112static int read_mii(int phyaddr, int regaddr, volatile greth_regs * regs)
Daniel Hellstromf1431792008-03-28 20:22:53 +0100113{
114 while (GRETH_REGLOAD(&regs->mdio) & GRETH_MII_BUSY) {
115 }
116
Daniel Hellstrom9ae3be82010-10-22 11:26:49 +0200117 GRETH_REGSAVE(&regs->mdio, ((phyaddr & 0x1F) << 11) | ((regaddr & 0x1F) << 6) | 2);
Daniel Hellstromf1431792008-03-28 20:22:53 +0100118
119 while (GRETH_REGLOAD(&regs->mdio) & GRETH_MII_BUSY) {
120 }
121
122 if (!(GRETH_REGLOAD(&regs->mdio) & GRETH_MII_NVALID)) {
123 return (GRETH_REGLOAD(&regs->mdio) >> 16) & 0xFFFF;
124 } else {
125 return -1;
126 }
127}
128
Daniel Hellstrom9ae3be82010-10-22 11:26:49 +0200129static void write_mii(int phyaddr, int regaddr, int data, volatile greth_regs * regs)
Daniel Hellstromf1431792008-03-28 20:22:53 +0100130{
131 while (GRETH_REGLOAD(&regs->mdio) & GRETH_MII_BUSY) {
132 }
133
134 GRETH_REGSAVE(&regs->mdio,
Daniel Hellstrom9ae3be82010-10-22 11:26:49 +0200135 ((data & 0xFFFF) << 16) | ((phyaddr & 0x1F) << 11) |
136 ((regaddr & 0x1F) << 6) | 1);
Daniel Hellstromf1431792008-03-28 20:22:53 +0100137
138 while (GRETH_REGLOAD(&regs->mdio) & GRETH_MII_BUSY) {
139 }
140
141}
142
143/* init/start hardware and allocate descriptor buffers for rx side
144 *
145 */
146int greth_init(struct eth_device *dev, bd_t * bis)
147{
148 int i;
149
150 greth_priv *greth = dev->priv;
151 greth_regs *regs = greth->regs;
Daniel Hellstromebb753c2010-10-27 09:24:13 +0200152
153 debug("greth_init\n");
Daniel Hellstromf1431792008-03-28 20:22:53 +0100154
Daniel Hellstromf1431792008-03-28 20:22:53 +0100155 if (!greth->rxbd_base) {
156
157 /* allocate descriptors */
158 greth->rxbd_base = (greth_bd *)
159 memalign(0x1000, GRETH_RXBD_CNT * sizeof(greth_bd));
160 greth->txbd_base = (greth_bd *)
161 memalign(0x1000, GRETH_RXBD_CNT * sizeof(greth_bd));
162
163 /* allocate buffers to all descriptors */
164 greth->rxbuf_base =
165 malloc(GRETH_RXBUF_EFF_SIZE * GRETH_RXBD_CNT);
166 }
167
168 /* initate rx decriptors */
169 for (i = 0; i < GRETH_RXBD_CNT; i++) {
170 greth->rxbd_base[i].addr = (unsigned int)
171 greth->rxbuf_base + (GRETH_RXBUF_EFF_SIZE * i);
172 /* enable desciptor & set wrap bit if last descriptor */
173 if (i >= (GRETH_RXBD_CNT - 1)) {
174 greth->rxbd_base[i].stat = GRETH_BD_EN | GRETH_BD_WR;
175 } else {
176 greth->rxbd_base[i].stat = GRETH_BD_EN;
177 }
178 }
179
180 /* initiate indexes */
181 greth->rxbd_curr = greth->rxbd_base;
182 greth->rxbd_max = greth->rxbd_base + (GRETH_RXBD_CNT - 1);
183 greth->txbd_max = greth->txbd_base + (GRETH_TXBD_CNT - 1);
184 /*
185 * greth->txbd_base->addr = 0;
186 * greth->txbd_base->stat = GRETH_BD_WR;
187 */
188
189 /* initate tx decriptors */
190 for (i = 0; i < GRETH_TXBD_CNT; i++) {
191 greth->txbd_base[i].addr = 0;
192 /* enable desciptor & set wrap bit if last descriptor */
193 if (i >= (GRETH_RXBD_CNT - 1)) {
194 greth->txbd_base[i].stat = GRETH_BD_WR;
195 } else {
196 greth->txbd_base[i].stat = 0;
197 }
198 }
199
200 /**** SET HARDWARE REGS ****/
201
202 /* Set pointer to tx/rx descriptor areas */
203 GRETH_REGSAVE(&regs->rx_desc_p, (unsigned int)&greth->rxbd_base[0]);
204 GRETH_REGSAVE(&regs->tx_desc_p, (unsigned int)&greth->txbd_base[0]);
205
206 /* Enable Transmitter, GRETH will now scan descriptors for packets
207 * to transmitt */
Daniel Hellstromebb753c2010-10-27 09:24:13 +0200208 debug("greth_init: enabling receiver\n");
Daniel Hellstromf1431792008-03-28 20:22:53 +0100209 GRETH_REGORIN(&regs->control, GRETH_RXEN);
210
211 return 0;
212}
213
214/* Initiate PHY to a relevant speed
215 * return:
216 * - 0 = success
217 * - 1 = timeout/fail
218 */
219int greth_init_phy(greth_priv * dev, bd_t * bis)
220{
221 greth_regs *regs = dev->regs;
222 int tmp, tmp1, tmp2, i;
223 unsigned int start, timeout;
Daniel Hellstrom9ae3be82010-10-22 11:26:49 +0200224 int phyaddr = GRETH_PHY_ADR_DEFAULT;
225
226#ifndef CONFIG_SYS_GRLIB_GRETH_PHYADDR
227 /* If BSP doesn't provide a hardcoded PHY address the driver will
228 * try to autodetect PHY address by stopping the search on the first
229 * PHY address which has REG0 implemented.
230 */
231 for (i=0; i<32; i++) {
232 tmp = read_mii(i, 0, regs);
233 if ( (tmp != 0) && (tmp != 0xffff) ) {
234 phyaddr = i;
235 break;
236 }
237 }
238#endif
239
240 /* Save PHY Address */
241 dev->phyaddr = phyaddr;
242
243 debug("GRETH PHY ADDRESS: %d\n", phyaddr);
Daniel Hellstromf1431792008-03-28 20:22:53 +0100244
245 /* X msecs to ticks */
246 timeout = usec2ticks(GRETH_PHY_TIMEOUT_MS * 1000);
247
248 /* Get system timer0 current value
249 * Total timeout is 5s
250 */
251 start = get_timer(0);
252
253 /* get phy control register default values */
254
Daniel Hellstrom9ae3be82010-10-22 11:26:49 +0200255 while ((tmp = read_mii(phyaddr, 0, regs)) & 0x8000) {
256 if (get_timer(start) > timeout) {
257 debug("greth_init_phy: PHY read 1 failed\n");
Daniel Hellstromf1431792008-03-28 20:22:53 +0100258 return 1; /* Fail */
Daniel Hellstrom9ae3be82010-10-22 11:26:49 +0200259 }
Daniel Hellstromf1431792008-03-28 20:22:53 +0100260 }
261
262 /* reset PHY and wait for completion */
Daniel Hellstrom9ae3be82010-10-22 11:26:49 +0200263 write_mii(phyaddr, 0, 0x8000 | tmp, regs);
Daniel Hellstromf1431792008-03-28 20:22:53 +0100264
Daniel Hellstrom9ae3be82010-10-22 11:26:49 +0200265 while (((tmp = read_mii(phyaddr, 0, regs))) & 0x8000) {
266 if (get_timer(start) > timeout) {
267 debug("greth_init_phy: PHY read 2 failed\n");
Daniel Hellstromf1431792008-03-28 20:22:53 +0100268 return 1; /* Fail */
Daniel Hellstrom9ae3be82010-10-22 11:26:49 +0200269 }
Daniel Hellstromf1431792008-03-28 20:22:53 +0100270 }
271
272 /* Check if PHY is autoneg capable and then determine operating
273 * mode, otherwise force it to 10 Mbit halfduplex
274 */
275 dev->gb = 0;
276 dev->fd = 0;
277 dev->sp = 0;
278 dev->auto_neg = 0;
279 if (!((tmp >> 12) & 1)) {
Daniel Hellstrom9ae3be82010-10-22 11:26:49 +0200280 write_mii(phyaddr, 0, 0, regs);
Daniel Hellstromf1431792008-03-28 20:22:53 +0100281 } else {
282 /* wait for auto negotiation to complete and then check operating mode */
283 dev->auto_neg = 1;
284 i = 0;
Daniel Hellstrom9ae3be82010-10-22 11:26:49 +0200285 while (!(((tmp = read_mii(phyaddr, 1, regs)) >> 5) & 1)) {
Daniel Hellstromf1431792008-03-28 20:22:53 +0100286 if (get_timer(start) > timeout) {
287 printf("Auto negotiation timed out. "
288 "Selecting default config\n");
Daniel Hellstrom9ae3be82010-10-22 11:26:49 +0200289 tmp = read_mii(phyaddr, 0, regs);
Daniel Hellstromf1431792008-03-28 20:22:53 +0100290 dev->gb = ((tmp >> 6) & 1)
291 && !((tmp >> 13) & 1);
292 dev->sp = !((tmp >> 6) & 1)
293 && ((tmp >> 13) & 1);
294 dev->fd = (tmp >> 8) & 1;
295 goto auto_neg_done;
296 }
297 }
298 if ((tmp >> 8) & 1) {
Daniel Hellstrom9ae3be82010-10-22 11:26:49 +0200299 tmp1 = read_mii(phyaddr, 9, regs);
300 tmp2 = read_mii(phyaddr, 10, regs);
Daniel Hellstromf1431792008-03-28 20:22:53 +0100301 if ((tmp1 & GRETH_MII_EXTADV_1000FD) &&
302 (tmp2 & GRETH_MII_EXTPRT_1000FD)) {
303 dev->gb = 1;
304 dev->fd = 1;
305 }
306 if ((tmp1 & GRETH_MII_EXTADV_1000HD) &&
307 (tmp2 & GRETH_MII_EXTPRT_1000HD)) {
308 dev->gb = 1;
309 dev->fd = 0;
310 }
311 }
312 if ((dev->gb == 0) || ((dev->gb == 1) && (dev->gbit_mac == 0))) {
Daniel Hellstrom9ae3be82010-10-22 11:26:49 +0200313 tmp1 = read_mii(phyaddr, 4, regs);
314 tmp2 = read_mii(phyaddr, 5, regs);
Daniel Hellstromf1431792008-03-28 20:22:53 +0100315 if ((tmp1 & GRETH_MII_100TXFD) &&
316 (tmp2 & GRETH_MII_100TXFD)) {
317 dev->sp = 1;
318 dev->fd = 1;
319 }
320 if ((tmp1 & GRETH_MII_100TXHD) &&
321 (tmp2 & GRETH_MII_100TXHD)) {
322 dev->sp = 1;
323 dev->fd = 0;
324 }
325 if ((tmp1 & GRETH_MII_10FD) && (tmp2 & GRETH_MII_10FD)) {
326 dev->fd = 1;
327 }
328 if ((dev->gb == 1) && (dev->gbit_mac == 0)) {
329 dev->gb = 0;
330 dev->fd = 0;
Daniel Hellstrom9ae3be82010-10-22 11:26:49 +0200331 write_mii(phyaddr, 0, dev->sp << 13, regs);
Daniel Hellstromf1431792008-03-28 20:22:53 +0100332 }
333 }
334
335 }
336 auto_neg_done:
Daniel Hellstromebb753c2010-10-27 09:24:13 +0200337 debug("%s GRETH Ethermac at [0x%x] irq %d. Running \
Daniel Hellstromf1431792008-03-28 20:22:53 +0100338 %d Mbps %s duplex\n", dev->gbit_mac ? "10/100/1000" : "10/100", (unsigned int)(regs), (unsigned int)(dev->irq), dev->gb ? 1000 : (dev->sp ? 100 : 10), dev->fd ? "full" : "half");
Daniel Hellstromf1431792008-03-28 20:22:53 +0100339 /* Read out PHY info if extended registers are available */
340 if (tmp & 1) {
Daniel Hellstrom9ae3be82010-10-22 11:26:49 +0200341 tmp1 = read_mii(phyaddr, 2, regs);
342 tmp2 = read_mii(phyaddr, 3, regs);
Daniel Hellstromf1431792008-03-28 20:22:53 +0100343 tmp1 = (tmp1 << 6) | ((tmp2 >> 10) & 0x3F);
344 tmp = tmp2 & 0xF;
345
346 tmp2 = (tmp2 >> 4) & 0x3F;
Daniel Hellstromebb753c2010-10-27 09:24:13 +0200347 debug("PHY: Vendor %x Device %x Revision %d\n", tmp1,
Daniel Hellstromf1431792008-03-28 20:22:53 +0100348 tmp2, tmp);
Daniel Hellstromf1431792008-03-28 20:22:53 +0100349 } else {
350 printf("PHY info not available\n");
351 }
352
353 /* set speed and duplex bits in control register */
354 GRETH_REGORIN(&regs->control,
355 (dev->gb << 8) | (dev->sp << 7) | (dev->fd << 4));
356
357 return 0;
358}
359
360void greth_halt(struct eth_device *dev)
361{
362 greth_priv *greth;
363 greth_regs *regs;
364 int i;
Daniel Hellstromebb753c2010-10-27 09:24:13 +0200365
366 debug("greth_halt\n");
367
Daniel Hellstromf1431792008-03-28 20:22:53 +0100368 if (!dev || !dev->priv)
369 return;
370
371 greth = dev->priv;
372 regs = greth->regs;
373
374 if (!regs)
375 return;
376
377 /* disable receiver/transmitter by clearing the enable bits */
378 GRETH_REGANDIN(&regs->control, ~(GRETH_RXEN | GRETH_TXEN));
379
380 /* reset rx/tx descriptors */
381 if (greth->rxbd_base) {
382 for (i = 0; i < GRETH_RXBD_CNT; i++) {
383 greth->rxbd_base[i].stat =
384 (i >= (GRETH_RXBD_CNT - 1)) ? GRETH_BD_WR : 0;
385 }
386 }
387
388 if (greth->txbd_base) {
389 for (i = 0; i < GRETH_TXBD_CNT; i++) {
390 greth->txbd_base[i].stat =
391 (i >= (GRETH_TXBD_CNT - 1)) ? GRETH_BD_WR : 0;
392 }
393 }
394}
395
396int greth_send(struct eth_device *dev, volatile void *eth_data, int data_length)
397{
398 greth_priv *greth = dev->priv;
399 greth_regs *regs = greth->regs;
400 greth_bd *txbd;
401 void *txbuf;
402 unsigned int status;
Daniel Hellstromebb753c2010-10-27 09:24:13 +0200403
404 debug("greth_send\n");
405
Daniel Hellstromf1431792008-03-28 20:22:53 +0100406 /* send data, wait for data to be sent, then return */
407 if (((unsigned int)eth_data & (GRETH_BUF_ALIGN - 1))
408 && !greth->gbit_mac) {
409 /* data not aligned as needed by GRETH 10/100, solve this by allocating 4 byte aligned buffer
410 * and copy data to before giving it to GRETH.
411 */
412 if (!greth->txbuf) {
413 greth->txbuf = malloc(GRETH_RXBUF_SIZE);
Daniel Hellstromf1431792008-03-28 20:22:53 +0100414 }
415
416 txbuf = greth->txbuf;
417
418 /* copy data info buffer */
419 memcpy((char *)txbuf, (char *)eth_data, data_length);
420
421 /* keep buffer to next time */
422 } else {
423 txbuf = (void *)eth_data;
424 }
425 /* get descriptor to use, only 1 supported... hehe easy */
426 txbd = greth->txbd_base;
427
428 /* setup descriptor to wrap around to it self */
429 txbd->addr = (unsigned int)txbuf;
430 txbd->stat = GRETH_BD_EN | GRETH_BD_WR | data_length;
431
432 /* Remind Core which descriptor to use when sending */
433 GRETH_REGSAVE(&regs->tx_desc_p, (unsigned int)txbd);
434
435 /* initate send by enabling transmitter */
436 GRETH_REGORIN(&regs->control, GRETH_TXEN);
437
438 /* Wait for data to be sent */
439 while ((status = GRETH_REGLOAD(&txbd->stat)) & GRETH_BD_EN) {
440 ;
441 }
442
443 /* was the packet transmitted succesfully? */
444 if (status & GRETH_TXBD_ERR_AL) {
445 greth->stats.tx_limit_errors++;
446 }
447
448 if (status & GRETH_TXBD_ERR_UE) {
449 greth->stats.tx_underrun_errors++;
450 }
451
452 if (status & GRETH_TXBD_ERR_LC) {
453 greth->stats.tx_latecol_errors++;
454 }
455
456 if (status &
457 (GRETH_TXBD_ERR_LC | GRETH_TXBD_ERR_UE | GRETH_TXBD_ERR_AL)) {
458 /* any error */
459 greth->stats.tx_errors++;
460 return -1;
461 }
462
463 /* bump tx packet counter */
464 greth->stats.tx_packets++;
465
466 /* return succefully */
467 return 0;
468}
469
470int greth_recv(struct eth_device *dev)
471{
472 greth_priv *greth = dev->priv;
473 greth_regs *regs = greth->regs;
474 greth_bd *rxbd;
475 unsigned int status, len = 0, bad;
476 unsigned char *d;
477 int enable = 0;
478 int i;
Daniel Hellstromebb753c2010-10-27 09:24:13 +0200479
Daniel Hellstromf1431792008-03-28 20:22:53 +0100480 /* Receive One packet only, but clear as many error packets as there are
481 * available.
482 */
483 {
484 /* current receive descriptor */
485 rxbd = greth->rxbd_curr;
486
487 /* get status of next received packet */
488 status = GRETH_REGLOAD(&rxbd->stat);
489
490 bad = 0;
491
492 /* stop if no more packets received */
493 if (status & GRETH_BD_EN) {
494 goto done;
495 }
Daniel Hellstromebb753c2010-10-27 09:24:13 +0200496
497 debug("greth_recv: packet 0x%lx, 0x%lx, len: %d\n",
Daniel Hellstromf1431792008-03-28 20:22:53 +0100498 (unsigned int)rxbd, status, status & GRETH_BD_LEN);
Daniel Hellstromf1431792008-03-28 20:22:53 +0100499
500 /* Check status for errors.
501 */
502 if (status & GRETH_RXBD_ERR_FT) {
503 greth->stats.rx_length_errors++;
504 bad = 1;
505 }
506 if (status & (GRETH_RXBD_ERR_AE | GRETH_RXBD_ERR_OE)) {
507 greth->stats.rx_frame_errors++;
508 bad = 1;
509 }
510 if (status & GRETH_RXBD_ERR_CRC) {
511 greth->stats.rx_crc_errors++;
512 bad = 1;
513 }
514 if (bad) {
515 greth->stats.rx_errors++;
516 printf
517 ("greth_recv: Bad packet (%d, %d, %d, 0x%08x, %d)\n",
518 greth->stats.rx_length_errors,
519 greth->stats.rx_frame_errors,
520 greth->stats.rx_crc_errors, status,
521 greth->stats.rx_packets);
522 /* print all rx descriptors */
523 for (i = 0; i < GRETH_RXBD_CNT; i++) {
524 printf("[%d]: Stat=0x%lx, Addr=0x%lx\n", i,
525 GRETH_REGLOAD(&greth->rxbd_base[i].stat),
Daniel Hellstrom9ae3be82010-10-22 11:26:49 +0200526 GRETH_REGLOAD(&greth->rxbd_base[i].addr));
Daniel Hellstromf1431792008-03-28 20:22:53 +0100527 }
528 } else {
529 /* Process the incoming packet. */
530 len = status & GRETH_BD_LEN;
531 d = (char *)rxbd->addr;
Daniel Hellstromebb753c2010-10-27 09:24:13 +0200532
533 debug
Daniel Hellstromf1431792008-03-28 20:22:53 +0100534 ("greth_recv: new packet, length: %d. data: %x %x %x %x %x %x %x %x\n",
535 len, d[0], d[1], d[2], d[3], d[4], d[5], d[6],
536 d[7]);
Daniel Hellstromebb753c2010-10-27 09:24:13 +0200537
Daniel Hellstromf1431792008-03-28 20:22:53 +0100538 /* flush all data cache to make sure we're not reading old packet data */
539 sparc_dcache_flush_all();
540
541 /* pass packet on to network subsystem */
542 NetReceive((void *)d, len);
543
544 /* bump stats counters */
545 greth->stats.rx_packets++;
546
547 /* bad is now 0 ==> will stop loop */
548 }
549
550 /* reenable descriptor to receive more packet with this descriptor, wrap around if needed */
551 rxbd->stat =
552 GRETH_BD_EN |
553 (((unsigned int)greth->rxbd_curr >=
554 (unsigned int)greth->rxbd_max) ? GRETH_BD_WR : 0);
555 enable = 1;
556
557 /* increase index */
558 greth->rxbd_curr =
559 ((unsigned int)greth->rxbd_curr >=
560 (unsigned int)greth->rxbd_max) ? greth->
561 rxbd_base : (greth->rxbd_curr + 1);
562
Daniel Hellstrom9ae3be82010-10-22 11:26:49 +0200563 }
Daniel Hellstromf1431792008-03-28 20:22:53 +0100564
565 if (enable) {
566 GRETH_REGORIN(&regs->control, GRETH_RXEN);
567 }
568 done:
569 /* return positive length of packet or 0 if non recieved */
570 return len;
571}
572
573void greth_set_hwaddr(greth_priv * greth, unsigned char *mac)
574{
575 /* save new MAC address */
576 greth->dev->enetaddr[0] = greth->hwaddr[0] = mac[0];
577 greth->dev->enetaddr[1] = greth->hwaddr[1] = mac[1];
578 greth->dev->enetaddr[2] = greth->hwaddr[2] = mac[2];
579 greth->dev->enetaddr[3] = greth->hwaddr[3] = mac[3];
580 greth->dev->enetaddr[4] = greth->hwaddr[4] = mac[4];
581 greth->dev->enetaddr[5] = greth->hwaddr[5] = mac[5];
582 greth->regs->esa_msb = (mac[0] << 8) | mac[1];
583 greth->regs->esa_lsb =
584 (mac[2] << 24) | (mac[3] << 16) | (mac[4] << 8) | mac[5];
Daniel Hellstromebb753c2010-10-27 09:24:13 +0200585
586 debug("GRETH: New MAC address: %02x:%02x:%02x:%02x:%02x:%02x\n",
Daniel Hellstromf1431792008-03-28 20:22:53 +0100587 mac[0], mac[1], mac[2], mac[3], mac[4], mac[5]);
Daniel Hellstromf1431792008-03-28 20:22:53 +0100588}
589
590int greth_initialize(bd_t * bis)
591{
592 greth_priv *greth;
593 ambapp_apbdev apbdev;
594 struct eth_device *dev;
595 int i;
596 char *addr_str, *end;
597 unsigned char addr[6];
Daniel Hellstromebb753c2010-10-27 09:24:13 +0200598
599 debug("Scanning for GRETH\n");
600
Daniel Hellstromf1431792008-03-28 20:22:53 +0100601 /* Find Device & IRQ via AMBA Plug&Play information */
602 if (ambapp_apb_first(VENDOR_GAISLER, GAISLER_ETHMAC, &apbdev) != 1) {
603 return -1; /* GRETH not found */
604 }
605
606 greth = (greth_priv *) malloc(sizeof(greth_priv));
607 dev = (struct eth_device *)malloc(sizeof(struct eth_device));
608 memset(dev, 0, sizeof(struct eth_device));
609 memset(greth, 0, sizeof(greth_priv));
610
611 greth->regs = (greth_regs *) apbdev.address;
612 greth->irq = apbdev.irq;
Daniel Hellstromebb753c2010-10-27 09:24:13 +0200613 debug("Found GRETH at 0x%lx, irq %d\n", greth->regs, greth->irq);
Daniel Hellstromf1431792008-03-28 20:22:53 +0100614 dev->priv = (void *)greth;
615 dev->iobase = (unsigned int)greth->regs;
616 dev->init = greth_init;
617 dev->halt = greth_halt;
618 dev->send = greth_send;
619 dev->recv = greth_recv;
620 greth->dev = dev;
621
622 /* Reset Core */
623 GRETH_REGSAVE(&greth->regs->control, GRETH_RESET);
624
625 /* Wait for core to finish reset cycle */
626 while (GRETH_REGLOAD(&greth->regs->control) & GRETH_RESET) ;
627
628 /* Get the phy address which assumed to have been set
629 correctly with the reset value in hardware */
630 greth->phyaddr = (GRETH_REGLOAD(&greth->regs->mdio) >> 11) & 0x1F;
631
632 /* Check if mac is gigabit capable */
633 greth->gbit_mac = (GRETH_REGLOAD(&greth->regs->control) >> 27) & 1;
634
635 /* Make descriptor string */
636 if (greth->gbit_mac) {
637 sprintf(dev->name, "GRETH 10/100/GB");
638 } else {
639 sprintf(dev->name, "GRETH 10/100");
640 }
641
642 /* initiate PHY, select speed/duplex depending on connected PHY */
643 if (greth_init_phy(greth, bis)) {
644 /* Failed to init PHY (timedout) */
Daniel Hellstrom9ae3be82010-10-22 11:26:49 +0200645 debug("GRETH[0x%08x]: Failed to init PHY\n", greth->regs);
Daniel Hellstromf1431792008-03-28 20:22:53 +0100646 return -1;
647 }
648
649 /* Register Device to EtherNet subsystem */
650 eth_register(dev);
651
652 /* Get MAC address */
653 if ((addr_str = getenv("ethaddr")) != NULL) {
654 for (i = 0; i < 6; i++) {
655 addr[i] =
656 addr_str ? simple_strtoul(addr_str, &end, 16) : 0;
657 if (addr_str) {
658 addr_str = (*end) ? end + 1 : end;
659 }
660 }
661 } else {
662 /* HW Address not found in environment, Set default HW address */
663 addr[0] = GRETH_HWADDR_0; /* MSB */
664 addr[1] = GRETH_HWADDR_1;
665 addr[2] = GRETH_HWADDR_2;
666 addr[3] = GRETH_HWADDR_3;
667 addr[4] = GRETH_HWADDR_4;
668 addr[5] = GRETH_HWADDR_5; /* LSB */
669 }
670
671 /* set and remember MAC address */
672 greth_set_hwaddr(greth, addr);
673
Daniel Hellstrom9ae3be82010-10-22 11:26:49 +0200674 debug("GRETH[0x%08x]: Initialized successfully\n", greth->regs);
Ben Warren8c042b62008-07-09 01:04:19 -0700675 return 0;
Daniel Hellstromf1431792008-03-28 20:22:53 +0100676}