blob: 0d858ed977bfb62505fbb460ca71fefb64e69632 [file] [log] [blame]
wdenk42d1f032003-10-15 23:53:47 +00001/*
2 * tsec.c
wdenk97d80fc2004-06-09 00:34:46 +00003 * Freescale Three Speed Ethernet Controller driver
wdenk42d1f032003-10-15 23:53:47 +00004 *
5 * This software may be used and distributed according to the
6 * terms of the GNU Public License, Version 2, incorporated
7 * herein by reference.
8 *
wdenk97d80fc2004-06-09 00:34:46 +00009 * Copyright 2004 Freescale Semiconductor.
wdenk42d1f032003-10-15 23:53:47 +000010 * (C) Copyright 2003, Motorola, Inc.
wdenk97d80fc2004-06-09 00:34:46 +000011 * maintained by Jon Loeliger (loeliger@freescale.com)
wdenk42d1f032003-10-15 23:53:47 +000012 * author Andy Fleming
13 *
14 */
15
16#include <config.h>
17#include <mpc85xx.h>
18#include <common.h>
19#include <malloc.h>
20#include <net.h>
21#include <command.h>
22
23#if defined(CONFIG_TSEC_ENET)
24#include "tsec.h"
25
26#define TX_BUF_CNT 2
27
28#undef TSEC_DEBUG
29#ifdef TSEC_DEBUG
wdenk97d80fc2004-06-09 00:34:46 +000030#define DBGPRINT(x,y) printf(x,y)
wdenk42d1f032003-10-15 23:53:47 +000031#else
wdenk97d80fc2004-06-09 00:34:46 +000032#define DBGPRINT(x,y)
wdenk42d1f032003-10-15 23:53:47 +000033#endif
34
35static uint rxIdx; /* index of the current RX buffer */
36static uint txIdx; /* index of the current TX buffer */
37
38typedef volatile struct rtxbd {
39 txbd8_t txbd[TX_BUF_CNT];
40 rxbd8_t rxbd[PKTBUFSRX];
41} RTXBD;
42
wdenk97d80fc2004-06-09 00:34:46 +000043struct tsec_info_struct {
44 unsigned int phyaddr;
45 unsigned int gigabit;
46 unsigned int phyregidx;
47};
48
49
50/* The tsec_info structure contains 3 values which the
51 * driver uses to determine how to operate a given ethernet
52 * device. For now, the structure is initialized with the
53 * knowledge that all current implementations have 2 TSEC
54 * devices, and one FEC. The information needed is:
55 * phyaddr - The address of the PHY which is attached to
56 * the given device.
57 *
58 * gigabit - This variable indicates whether the device
59 * supports gigabit speed ethernet
60 *
61 * phyregidx - This variable specifies which ethernet device
62 * controls the MII Management registers which are connected
63 * to the PHY. For 8540/8560, only TSEC1 (index 0) has
64 * access to the PHYs, so all of the entries have "0".
65 *
66 * The values specified in the table are taken from the board's
67 * config file in include/configs/. When implementing a new
68 * board with ethernet capability, it is necessary to define:
69 * TSEC1_PHY_ADDR
70 * TSEC1_PHYIDX
71 * TSEC2_PHY_ADDR
72 * TSEC2_PHYIDX
73 *
74 * and for 8560:
75 * FEC_PHY_ADDR
76 * FEC_PHYIDX
77 */
78static struct tsec_info_struct tsec_info[] = {
79#ifdef CONFIG_MPC85XX_TSEC1
80 {TSEC1_PHY_ADDR, 1, TSEC1_PHYIDX},
81#endif
82#ifdef CONFIG_MPC85XX_TSEC2
83 {TSEC2_PHY_ADDR, 1, TSEC2_PHYIDX},
84#endif
85#ifdef CONFIG_MPC85XX_FEC
86 {FEC_PHY_ADDR, 0, FEC_PHYIDX},
87#endif
88};
89
90#define MAXCONTROLLERS 3
91
92static int relocated = 0;
93
94static struct tsec_private *privlist[MAXCONTROLLERS];
95
wdenk42d1f032003-10-15 23:53:47 +000096#ifdef __GNUC__
97static RTXBD rtx __attribute__ ((aligned(8)));
98#else
99#error "rtx must be 64-bit aligned"
100#endif
101
102static int tsec_send(struct eth_device* dev, volatile void *packet, int length);
103static int tsec_recv(struct eth_device* dev);
104static int tsec_init(struct eth_device* dev, bd_t * bd);
105static void tsec_halt(struct eth_device* dev);
wdenk97d80fc2004-06-09 00:34:46 +0000106static void init_registers(volatile tsec_t *regs);
107static void startup_tsec(struct eth_device *dev);
108static int init_phy(struct eth_device *dev);
109void write_phy_reg(struct tsec_private *priv, uint regnum, uint value);
110uint read_phy_reg(struct tsec_private *priv, uint regnum);
111struct phy_info * get_phy_info(struct eth_device *dev);
112void phy_run_commands(struct tsec_private *priv, struct phy_cmd *cmd);
113static void adjust_link(struct eth_device *dev);
114static void relocate_cmds(void);
wdenk7abf0c52004-04-18 21:45:42 +0000115
wdenk97d80fc2004-06-09 00:34:46 +0000116/* Initialize device structure. Returns success if PHY
117 * initialization succeeded (i.e. if it recognizes the PHY)
118 */
119int tsec_initialize(bd_t *bis, int index)
wdenk42d1f032003-10-15 23:53:47 +0000120{
121 struct eth_device* dev;
122 int i;
wdenk97d80fc2004-06-09 00:34:46 +0000123 struct tsec_private *priv;
wdenk42d1f032003-10-15 23:53:47 +0000124
125 dev = (struct eth_device*) malloc(sizeof *dev);
126
wdenk97d80fc2004-06-09 00:34:46 +0000127 if(NULL == dev)
wdenk42d1f032003-10-15 23:53:47 +0000128 return 0;
129
130 memset(dev, 0, sizeof *dev);
131
wdenk97d80fc2004-06-09 00:34:46 +0000132 priv = (struct tsec_private *) malloc(sizeof(*priv));
133
134 if(NULL == priv)
135 return 0;
136
137 privlist[index] = priv;
138 priv->regs = (volatile tsec_t *)(TSEC_BASE_ADDR + index*TSEC_SIZE);
139 priv->phyregs = (volatile tsec_t *)(TSEC_BASE_ADDR +
140 tsec_info[index].phyregidx*TSEC_SIZE);
141
142 priv->phyaddr = tsec_info[index].phyaddr;
143 priv->gigabit = tsec_info[index].gigabit;
144
145 sprintf(dev->name, "MOTO ENET%d", index);
wdenk42d1f032003-10-15 23:53:47 +0000146 dev->iobase = 0;
wdenk97d80fc2004-06-09 00:34:46 +0000147 dev->priv = priv;
wdenk42d1f032003-10-15 23:53:47 +0000148 dev->init = tsec_init;
149 dev->halt = tsec_halt;
150 dev->send = tsec_send;
151 dev->recv = tsec_recv;
152
153 /* Tell u-boot to get the addr from the env */
154 for(i=0;i<6;i++)
155 dev->enetaddr[i] = 0;
156
157 eth_register(dev);
158
wdenk7abf0c52004-04-18 21:45:42 +0000159
wdenk97d80fc2004-06-09 00:34:46 +0000160 /* Reset the MAC */
161 priv->regs->maccfg1 |= MACCFG1_SOFT_RESET;
162 priv->regs->maccfg1 &= ~(MACCFG1_SOFT_RESET);
wdenk7abf0c52004-04-18 21:45:42 +0000163
wdenk97d80fc2004-06-09 00:34:46 +0000164 /* Try to initialize PHY here, and return */
165 return init_phy(dev);
wdenk42d1f032003-10-15 23:53:47 +0000166}
167
168
169/* Initializes data structures and registers for the controller,
wdenk97d80fc2004-06-09 00:34:46 +0000170 * and brings the interface up. Returns the link status, meaning
171 * that it returns success if the link is up, failure otherwise.
172 * This allows u-boot to find the first active controller. */
wdenk42d1f032003-10-15 23:53:47 +0000173int tsec_init(struct eth_device* dev, bd_t * bd)
174{
wdenk42d1f032003-10-15 23:53:47 +0000175 uint tempval;
176 char tmpbuf[MAC_ADDR_LEN];
177 int i;
wdenk97d80fc2004-06-09 00:34:46 +0000178 struct tsec_private *priv = (struct tsec_private *)dev->priv;
179 volatile tsec_t *regs = priv->regs;
wdenk42d1f032003-10-15 23:53:47 +0000180
181 /* Make sure the controller is stopped */
182 tsec_halt(dev);
183
wdenk97d80fc2004-06-09 00:34:46 +0000184 /* Init MACCFG2. Defaults to GMII */
wdenk42d1f032003-10-15 23:53:47 +0000185 regs->maccfg2 = MACCFG2_INIT_SETTINGS;
186
187 /* Init ECNTRL */
188 regs->ecntrl = ECNTRL_INIT_SETTINGS;
189
190 /* Copy the station address into the address registers.
191 * Backwards, because little endian MACS are dumb */
192 for(i=0;i<MAC_ADDR_LEN;i++) {
wdenk97d80fc2004-06-09 00:34:46 +0000193 tmpbuf[MAC_ADDR_LEN - 1 - i] = dev->enetaddr[i];
wdenk42d1f032003-10-15 23:53:47 +0000194 }
195 (uint)(regs->macstnaddr1) = *((uint *)(tmpbuf));
196
197 tempval = *((uint *)(tmpbuf +4));
198
199 (uint)(regs->macstnaddr2) = tempval;
200
wdenk42d1f032003-10-15 23:53:47 +0000201 /* reset the indices to zero */
202 rxIdx = 0;
203 txIdx = 0;
204
205 /* Clear out (for the most part) the other registers */
206 init_registers(regs);
207
208 /* Ready the device for tx/rx */
wdenk97d80fc2004-06-09 00:34:46 +0000209 startup_tsec(dev);
wdenk42d1f032003-10-15 23:53:47 +0000210
wdenk97d80fc2004-06-09 00:34:46 +0000211 /* If there's no link, fail */
212 return priv->link;
wdenk42d1f032003-10-15 23:53:47 +0000213
214}
215
216
wdenk97d80fc2004-06-09 00:34:46 +0000217/* Write value to the device's PHY through the registers
218 * specified in priv, modifying the register specified in regnum.
219 * It will wait for the write to be done (or for a timeout to
220 * expire) before exiting
221 */
222void write_phy_reg(struct tsec_private *priv, uint regnum, uint value)
223{
224 volatile tsec_t *regbase = priv->phyregs;
225 uint phyid = priv->phyaddr;
226 int timeout=1000000;
227
228 regbase->miimadd = (phyid << 8) | regnum;
229 regbase->miimcon = value;
230 asm("msync");
231
232 timeout=1000000;
233 while((regbase->miimind & MIIMIND_BUSY) && timeout--);
234}
235
236
237/* Reads register regnum on the device's PHY through the
238 * registers specified in priv. It lowers and raises the read
239 * command, and waits for the data to become valid (miimind
240 * notvalid bit cleared), and the bus to cease activity (miimind
241 * busy bit cleared), and then returns the value
242 */
243uint read_phy_reg(struct tsec_private *priv, uint regnum)
wdenk42d1f032003-10-15 23:53:47 +0000244{
245 uint value;
wdenk97d80fc2004-06-09 00:34:46 +0000246 volatile tsec_t *regbase = priv->phyregs;
247 uint phyid = priv->phyaddr;
wdenk42d1f032003-10-15 23:53:47 +0000248
wdenk97d80fc2004-06-09 00:34:46 +0000249 /* Put the address of the phy, and the register
250 * number into MIIMADD */
251 regbase->miimadd = (phyid << 8) | regnum;
wdenk42d1f032003-10-15 23:53:47 +0000252
253 /* Clear the command register, and wait */
254 regbase->miimcom = 0;
255 asm("msync");
256
257 /* Initiate a read command, and wait */
258 regbase->miimcom = MIIM_READ_COMMAND;
259 asm("msync");
260
261 /* Wait for the the indication that the read is done */
262 while((regbase->miimind & (MIIMIND_NOTVALID | MIIMIND_BUSY)));
263
264 /* Grab the value read from the PHY */
265 value = regbase->miimstat;
266
267 return value;
268}
269
wdenk97d80fc2004-06-09 00:34:46 +0000270
271/* Discover which PHY is attached to the device, and configure it
272 * properly. If the PHY is not recognized, then return 0
273 * (failure). Otherwise, return 1
274 */
275static int init_phy(struct eth_device *dev)
wdenk42d1f032003-10-15 23:53:47 +0000276{
wdenk97d80fc2004-06-09 00:34:46 +0000277 struct tsec_private *priv = (struct tsec_private *)dev->priv;
278 struct phy_info *curphy;
wdenk42d1f032003-10-15 23:53:47 +0000279
280 /* Assign a Physical address to the TBI */
wdenk97d80fc2004-06-09 00:34:46 +0000281 priv->regs->tbipa=TBIPA_VALUE;
wdenk42d1f032003-10-15 23:53:47 +0000282
wdenk97d80fc2004-06-09 00:34:46 +0000283 if(0 == relocated)
284 relocate_cmds();
wdenk42d1f032003-10-15 23:53:47 +0000285
wdenk97d80fc2004-06-09 00:34:46 +0000286 /* Get the cmd structure corresponding to the attached
287 * PHY */
288 curphy = get_phy_info(dev);
wdenk42d1f032003-10-15 23:53:47 +0000289
wdenk97d80fc2004-06-09 00:34:46 +0000290 if(NULL == curphy) {
291 printf("%s: No PHY found\n", dev->name);
wdenk42d1f032003-10-15 23:53:47 +0000292
wdenk97d80fc2004-06-09 00:34:46 +0000293 return 0;
wdenk42d1f032003-10-15 23:53:47 +0000294 }
295
wdenk97d80fc2004-06-09 00:34:46 +0000296 priv->phyinfo = curphy;
wdenk42d1f032003-10-15 23:53:47 +0000297
wdenk97d80fc2004-06-09 00:34:46 +0000298 phy_run_commands(priv, priv->phyinfo->config);
wdenk42d1f032003-10-15 23:53:47 +0000299
wdenk97d80fc2004-06-09 00:34:46 +0000300 return 1;
wdenk42d1f032003-10-15 23:53:47 +0000301}
302
303
wdenk97d80fc2004-06-09 00:34:46 +0000304/* Returns which value to write to the control register. */
305/* For 10/100, the value is slightly different */
306uint mii_cr_init(uint mii_reg, struct tsec_private *priv)
307{
308 if(priv->gigabit)
309 return MIIM_CONTROL_INIT;
310 else
311 return MIIM_CR_INIT;
312}
313
314
315/* Parse the status register for link, and then do
316 * auto-negotiation */
317uint mii_parse_sr(uint mii_reg, struct tsec_private *priv)
318{
319 uint timeout = TSEC_TIMEOUT;
320
321 if(mii_reg & MIIM_STATUS_LINK)
322 priv->link = 1;
323 else
324 priv->link = 0;
325
326 if(priv->link) {
327 while((!(mii_reg & MIIM_STATUS_AN_DONE)) && timeout--)
328 mii_reg = read_phy_reg(priv, MIIM_STATUS);
329 }
330
331 return 0;
332}
333
334
335/* Parse the 88E1011's status register for speed and duplex
336 * information */
337uint mii_parse_88E1011_psr(uint mii_reg, struct tsec_private *priv)
338{
339 uint speed;
340
341 if(mii_reg & MIIM_88E1011_PHYSTAT_DUPLEX)
342 priv->duplexity = 1;
343 else
344 priv->duplexity = 0;
345
346 speed = (mii_reg &MIIM_88E1011_PHYSTAT_SPEED);
347
348 switch(speed) {
349 case MIIM_88E1011_PHYSTAT_GBIT:
350 priv->speed = 1000;
351 break;
352 case MIIM_88E1011_PHYSTAT_100:
353 priv->speed = 100;
354 break;
355 default:
356 priv->speed = 10;
357 }
358
359 return 0;
360}
361
362
363/* Parse the cis8201's status register for speed and duplex
364 * information */
365uint mii_parse_cis8201(uint mii_reg, struct tsec_private *priv)
366{
367 uint speed;
368
369 if(mii_reg & MIIM_CIS8201_AUXCONSTAT_DUPLEX)
370 priv->duplexity = 1;
371 else
372 priv->duplexity = 0;
373
374 speed = mii_reg & MIIM_CIS8201_AUXCONSTAT_SPEED;
375 switch(speed) {
376 case MIIM_CIS8201_AUXCONSTAT_GBIT:
377 priv->speed = 1000;
378 break;
379 case MIIM_CIS8201_AUXCONSTAT_100:
380 priv->speed = 100;
381 break;
382 default:
383 priv->speed = 10;
384 break;
385 }
386
387 return 0;
388}
389
390
391/* Parse the DM9161's status register for speed and duplex
392 * information */
393uint mii_parse_dm9161_scsr(uint mii_reg, struct tsec_private *priv)
394{
395 if(mii_reg & (MIIM_DM9161_SCSR_100F | MIIM_DM9161_SCSR_100H))
396 priv->speed = 100;
397 else
398 priv->speed = 10;
399
400 if(mii_reg & (MIIM_DM9161_SCSR_100F | MIIM_DM9161_SCSR_10F))
401 priv->duplexity = 1;
402 else
403 priv->duplexity = 0;
404
405 return 0;
406}
407
408
409/* Hack to write all 4 PHYs with the LED values */
410uint mii_cis8204_fixled(uint mii_reg, struct tsec_private *priv)
411{
412 uint phyid;
413 volatile tsec_t *regbase = priv->phyregs;
414 int timeout=1000000;
415
416 for(phyid=0;phyid<4;phyid++) {
417 regbase->miimadd = (phyid << 8) | mii_reg;
418 regbase->miimcon = MIIM_CIS8204_SLEDCON_INIT;
419 asm("msync");
420
421 timeout=1000000;
422 while((regbase->miimind & MIIMIND_BUSY) && timeout--);
423 }
424
425 return MIIM_CIS8204_SLEDCON_INIT;
426}
427
428
429/* Initialized required registers to appropriate values, zeroing
430 * those we don't care about (unless zero is bad, in which case,
431 * choose a more appropriate value) */
432static void init_registers(volatile tsec_t *regs)
wdenk42d1f032003-10-15 23:53:47 +0000433{
434 /* Clear IEVENT */
435 regs->ievent = IEVENT_INIT_CLEAR;
436
437 regs->imask = IMASK_INIT_CLEAR;
438
439 regs->hash.iaddr0 = 0;
440 regs->hash.iaddr1 = 0;
441 regs->hash.iaddr2 = 0;
442 regs->hash.iaddr3 = 0;
443 regs->hash.iaddr4 = 0;
444 regs->hash.iaddr5 = 0;
445 regs->hash.iaddr6 = 0;
446 regs->hash.iaddr7 = 0;
447
448 regs->hash.gaddr0 = 0;
449 regs->hash.gaddr1 = 0;
450 regs->hash.gaddr2 = 0;
451 regs->hash.gaddr3 = 0;
452 regs->hash.gaddr4 = 0;
453 regs->hash.gaddr5 = 0;
454 regs->hash.gaddr6 = 0;
455 regs->hash.gaddr7 = 0;
456
457 regs->rctrl = 0x00000000;
458
459 /* Init RMON mib registers */
460 memset((void *)&(regs->rmon), 0, sizeof(rmon_mib_t));
461
462 regs->rmon.cam1 = 0xffffffff;
463 regs->rmon.cam2 = 0xffffffff;
464
465 regs->mrblr = MRBLR_INIT_SETTINGS;
466
467 regs->minflr = MINFLR_INIT_SETTINGS;
468
469 regs->attr = ATTR_INIT_SETTINGS;
470 regs->attreli = ATTRELI_INIT_SETTINGS;
471
472}
473
wdenk97d80fc2004-06-09 00:34:46 +0000474
475/* Configure maccfg2 based on negotiated speed and duplex
476 * reported by PHY handling code */
477static void adjust_link(struct eth_device *dev)
478{
479 struct tsec_private *priv = (struct tsec_private *)dev->priv;
480 volatile tsec_t *regs = priv->regs;
481
482 if(priv->link) {
483 if(priv->duplexity != 0)
484 regs->maccfg2 |= MACCFG2_FULL_DUPLEX;
485 else
486 regs->maccfg2 &= ~(MACCFG2_FULL_DUPLEX);
487
488 switch(priv->speed) {
489 case 1000:
490 regs->maccfg2 = ((regs->maccfg2&~(MACCFG2_IF))
491 | MACCFG2_GMII);
492 break;
493 case 100:
494 case 10:
495 regs->maccfg2 = ((regs->maccfg2&~(MACCFG2_IF))
496 | MACCFG2_MII);
497 break;
498 default:
499 printf("%s: Speed was bad\n", dev->name);
500 break;
501 }
502
503 printf("Speed: %d, %s duplex\n", priv->speed,
504 (priv->duplexity) ? "full" : "half");
505
506 } else {
507 printf("%s: No link.\n", dev->name);
508 }
509}
510
511
512/* Set up the buffers and their descriptors, and bring up the
513 * interface */
514static void startup_tsec(struct eth_device *dev)
wdenk42d1f032003-10-15 23:53:47 +0000515{
516 int i;
wdenk97d80fc2004-06-09 00:34:46 +0000517 struct tsec_private *priv = (struct tsec_private *)dev->priv;
518 volatile tsec_t *regs = priv->regs;
wdenk42d1f032003-10-15 23:53:47 +0000519
520 /* Point to the buffer descriptors */
521 regs->tbase = (unsigned int)(&rtx.txbd[txIdx]);
522 regs->rbase = (unsigned int)(&rtx.rxbd[rxIdx]);
523
524 /* Initialize the Rx Buffer descriptors */
525 for (i = 0; i < PKTBUFSRX; i++) {
526 rtx.rxbd[i].status = RXBD_EMPTY;
527 rtx.rxbd[i].length = 0;
528 rtx.rxbd[i].bufPtr = (uint)NetRxPackets[i];
529 }
530 rtx.rxbd[PKTBUFSRX -1].status |= RXBD_WRAP;
531
532 /* Initialize the TX Buffer Descriptors */
533 for(i=0; i<TX_BUF_CNT; i++) {
534 rtx.txbd[i].status = 0;
535 rtx.txbd[i].length = 0;
536 rtx.txbd[i].bufPtr = 0;
537 }
538 rtx.txbd[TX_BUF_CNT -1].status |= TXBD_WRAP;
539
wdenk97d80fc2004-06-09 00:34:46 +0000540 /* Start up the PHY */
541 phy_run_commands(priv, priv->phyinfo->startup);
542 adjust_link(dev);
543
wdenk42d1f032003-10-15 23:53:47 +0000544 /* Enable Transmit and Receive */
545 regs->maccfg1 |= (MACCFG1_RX_EN | MACCFG1_TX_EN);
546
547 /* Tell the DMA it is clear to go */
548 regs->dmactrl |= DMACTRL_INIT_SETTINGS;
549 regs->tstat = TSTAT_CLEAR_THALT;
550 regs->dmactrl &= ~(DMACTRL_GRS | DMACTRL_GTS);
551}
552
553/* This returns the status bits of the device. The return value
554 * is never checked, and this is what the 8260 driver did, so we
555 * do the same. Presumably, this would be zero if there were no
556 * errors */
557static int tsec_send(struct eth_device* dev, volatile void *packet, int length)
558{
559 int i;
560 int result = 0;
wdenk97d80fc2004-06-09 00:34:46 +0000561 struct tsec_private *priv = (struct tsec_private *)dev->priv;
562 volatile tsec_t *regs = priv->regs;
wdenk42d1f032003-10-15 23:53:47 +0000563
564 /* Find an empty buffer descriptor */
565 for(i=0; rtx.txbd[txIdx].status & TXBD_READY; i++) {
566 if (i >= TOUT_LOOP) {
wdenk97d80fc2004-06-09 00:34:46 +0000567 DBGPRINT("%s: tsec: tx buffers full\n", dev->name);
wdenk42d1f032003-10-15 23:53:47 +0000568 return result;
569 }
570 }
571
572 rtx.txbd[txIdx].bufPtr = (uint)packet;
573 rtx.txbd[txIdx].length = length;
574 rtx.txbd[txIdx].status |= (TXBD_READY | TXBD_LAST | TXBD_CRC | TXBD_INTERRUPT);
575
576 /* Tell the DMA to go */
577 regs->tstat = TSTAT_CLEAR_THALT;
578
579 /* Wait for buffer to be transmitted */
580 for(i=0; rtx.txbd[txIdx].status & TXBD_READY; i++) {
581 if (i >= TOUT_LOOP) {
wdenk97d80fc2004-06-09 00:34:46 +0000582 DBGPRINT("%s: tsec: tx error\n", dev->name);
wdenk42d1f032003-10-15 23:53:47 +0000583 return result;
584 }
585 }
586
587 txIdx = (txIdx + 1) % TX_BUF_CNT;
588 result = rtx.txbd[txIdx].status & TXBD_STATS;
589
590 return result;
591}
592
593static int tsec_recv(struct eth_device* dev)
594{
595 int length;
wdenk97d80fc2004-06-09 00:34:46 +0000596 struct tsec_private *priv = (struct tsec_private *)dev->priv;
597 volatile tsec_t *regs = priv->regs;
wdenk42d1f032003-10-15 23:53:47 +0000598
599 while(!(rtx.rxbd[rxIdx].status & RXBD_EMPTY)) {
600
601 length = rtx.rxbd[rxIdx].length;
602
603 /* Send the packet up if there were no errors */
604 if (!(rtx.rxbd[rxIdx].status & RXBD_STATS)) {
605 NetReceive(NetRxPackets[rxIdx], length - 4);
wdenk97d80fc2004-06-09 00:34:46 +0000606 } else {
607 printf("Got error %x\n",
608 (rtx.rxbd[rxIdx].status & RXBD_STATS));
wdenk42d1f032003-10-15 23:53:47 +0000609 }
610
611 rtx.rxbd[rxIdx].length = 0;
612
613 /* Set the wrap bit if this is the last element in the list */
614 rtx.rxbd[rxIdx].status = RXBD_EMPTY | (((rxIdx + 1) == PKTBUFSRX) ? RXBD_WRAP : 0);
615
616 rxIdx = (rxIdx + 1) % PKTBUFSRX;
617 }
618
619 if(regs->ievent&IEVENT_BSY) {
620 regs->ievent = IEVENT_BSY;
621 regs->rstat = RSTAT_CLEAR_RHALT;
622 }
623
624 return -1;
625
626}
627
628
wdenk97d80fc2004-06-09 00:34:46 +0000629/* Stop the interface */
wdenk42d1f032003-10-15 23:53:47 +0000630static void tsec_halt(struct eth_device* dev)
631{
wdenk97d80fc2004-06-09 00:34:46 +0000632 struct tsec_private *priv = (struct tsec_private *)dev->priv;
633 volatile tsec_t *regs = priv->regs;
wdenk42d1f032003-10-15 23:53:47 +0000634
635 regs->dmactrl &= ~(DMACTRL_GRS | DMACTRL_GTS);
636 regs->dmactrl |= (DMACTRL_GRS | DMACTRL_GTS);
637
638 while(!(regs->ievent & (IEVENT_GRSC | IEVENT_GTSC)));
639
640 regs->maccfg1 &= ~(MACCFG1_TX_EN | MACCFG1_RX_EN);
641
wdenk97d80fc2004-06-09 00:34:46 +0000642 /* Shut down the PHY, as needed */
643 phy_run_commands(priv, priv->phyinfo->shutdown);
wdenk42d1f032003-10-15 23:53:47 +0000644}
wdenk7abf0c52004-04-18 21:45:42 +0000645
wdenk97d80fc2004-06-09 00:34:46 +0000646
647struct phy_info phy_info_M88E1011S = {
648 0x01410c6,
649 "Marvell 88E1011S",
650 4,
651 (struct phy_cmd[]) { /* config */
652 /* Reset and configure the PHY */
653 {MIIM_CONTROL, MIIM_CONTROL_RESET, NULL},
654 {0x1d, 0x1f, NULL},
655 {0x1e, 0x200c, NULL},
656 {0x1d, 0x5, NULL},
657 {0x1e, 0x0, NULL},
658 {0x1e, 0x100, NULL},
659 {MIIM_GBIT_CONTROL, MIIM_GBIT_CONTROL_INIT, NULL},
660 {MIIM_ANAR, MIIM_ANAR_INIT, NULL},
661 {MIIM_CONTROL, MIIM_CONTROL_RESET, NULL},
662 {MIIM_CONTROL, MIIM_CONTROL_INIT, &mii_cr_init},
663 {miim_end,}
664 },
665 (struct phy_cmd[]) { /* startup */
666 /* Status is read once to clear old link state */
667 {MIIM_STATUS, miim_read, NULL},
668 /* Auto-negotiate */
669 {MIIM_STATUS, miim_read, &mii_parse_sr},
670 /* Read the status */
671 {MIIM_88E1011_PHY_STATUS, miim_read, &mii_parse_88E1011_psr},
672 {miim_end,}
673 },
674 (struct phy_cmd[]) { /* shutdown */
675 {miim_end,}
676 },
677};
678
679struct phy_info phy_info_cis8204 = {
680 0x3f11,
681 "Cicada Cis8204",
682 6,
683 (struct phy_cmd[]) { /* config */
684 /* Override PHY config settings */
685 {MIIM_CIS8201_AUX_CONSTAT, MIIM_CIS8201_AUXCONSTAT_INIT, NULL},
686 /* Configure some basic stuff */
687 {MIIM_CONTROL, MIIM_CONTROL_INIT, &mii_cr_init},
688 {MIIM_CIS8204_SLED_CON, MIIM_CIS8204_SLEDCON_INIT, &mii_cis8204_fixled},
689 {MIIM_CIS8204_EPHY_CON, MIIM_CIS8204_EPHYCON_INIT, NULL},
690 {miim_end,}
691 },
692 (struct phy_cmd[]) { /* startup */
693 /* Read the Status (2x to make sure link is right) */
694 {MIIM_STATUS, miim_read, NULL},
695 /* Auto-negotiate */
696 {MIIM_STATUS, miim_read, &mii_parse_sr},
697 /* Read the status */
698 {MIIM_CIS8201_AUX_CONSTAT, miim_read, &mii_parse_cis8201},
699 {miim_end,}
700 },
701 (struct phy_cmd[]) { /* shutdown */
702 {miim_end,}
703 },
704};
705
706/* Cicada 8201 */
707struct phy_info phy_info_cis8201 = {
708 0xfc41,
709 "CIS8201",
710 4,
711 (struct phy_cmd[]) { /* config */
712 /* Override PHY config settings */
713 {MIIM_CIS8201_AUX_CONSTAT, MIIM_CIS8201_AUXCONSTAT_INIT, NULL},
714 /* Set up the interface mode */
715 {MIIM_CIS8201_EXT_CON1, MIIM_CIS8201_EXTCON1_INIT, NULL},
716 /* Configure some basic stuff */
717 {MIIM_CONTROL, MIIM_CONTROL_INIT, &mii_cr_init},
718 {miim_end,}
719 },
720 (struct phy_cmd[]) { /* startup */
721 /* Read the Status (2x to make sure link is right) */
722 {MIIM_STATUS, miim_read, NULL},
723 /* Auto-negotiate */
724 {MIIM_STATUS, miim_read, &mii_parse_sr},
725 /* Read the status */
726 {MIIM_CIS8201_AUX_CONSTAT, miim_read, &mii_parse_cis8201},
727 {miim_end,}
728 },
729 (struct phy_cmd[]) { /* shutdown */
730 {miim_end,}
731 },
732};
733
734
735struct phy_info phy_info_dm9161 = {
736 0x0181b88,
737 "Davicom DM9161E",
738 4,
739 (struct phy_cmd[]) { /* config */
740 {MIIM_CONTROL, MIIM_DM9161_CR_STOP, NULL},
741 /* Do not bypass the scrambler/descrambler */
742 {MIIM_DM9161_SCR, MIIM_DM9161_SCR_INIT, NULL},
743 /* Clear 10BTCSR to default */
744 {MIIM_DM9161_10BTCSR, MIIM_DM9161_10BTCSR_INIT, NULL},
745 /* Configure some basic stuff */
746 {MIIM_CONTROL, MIIM_CR_INIT, NULL},
747 /* Restart Auto Negotiation */
748 {MIIM_CONTROL, MIIM_DM9161_CR_RSTAN, NULL},
749 {miim_end,}
750 },
751 (struct phy_cmd[]) { /* startup */
752 /* Status is read once to clear old link state */
753 {MIIM_STATUS, miim_read, NULL},
754 /* Auto-negotiate */
755 {MIIM_STATUS, miim_read, &mii_parse_sr},
756 /* Read the status */
757 {MIIM_DM9161_SCSR, miim_read, &mii_parse_dm9161_scsr},
758 {miim_end,}
759 },
760 (struct phy_cmd[]) { /* shutdown */
761 {miim_end,}
762 },
763};
764
765struct phy_info *phy_info[] = {
766#if 0
767 &phy_info_cis8201,
768#endif
769 &phy_info_cis8204,
770 &phy_info_M88E1011S,
771 &phy_info_dm9161,
772 NULL
773};
774
775
776/* Grab the identifier of the device's PHY, and search through
777 * all of the known PHYs to see if one matches. If so, return
778 * it, if not, return NULL */
779struct phy_info * get_phy_info(struct eth_device *dev)
780{
781 struct tsec_private *priv = (struct tsec_private *)dev->priv;
782 uint phy_reg, phy_ID;
783 int i;
784 struct phy_info *theInfo = NULL;
785
786 /* Grab the bits from PHYIR1, and put them in the upper half */
787 phy_reg = read_phy_reg(priv, MIIM_PHYIR1);
788 phy_ID = (phy_reg & 0xffff) << 16;
789
790 /* Grab the bits from PHYIR2, and put them in the lower half */
791 phy_reg = read_phy_reg(priv, MIIM_PHYIR2);
792 phy_ID |= (phy_reg & 0xffff);
793
794 /* loop through all the known PHY types, and find one that */
795 /* matches the ID we read from the PHY. */
796 for(i=0; phy_info[i]; i++) {
797 if(phy_info[i]->id == (phy_ID >> phy_info[i]->shift))
798 theInfo = phy_info[i];
799 }
800
801 if(theInfo == NULL)
802 {
803 printf("%s: PHY id %x is not supported!\n", dev->name, phy_ID);
804 return NULL;
805 } else {
806 printf("%s: PHY is %s (%x)\n", dev->name, theInfo->name,
807 phy_ID);
808 }
809
810 return theInfo;
811}
812
813
814/* Execute the given series of commands on the given device's
815 * PHY, running functions as necessary*/
816void phy_run_commands(struct tsec_private *priv, struct phy_cmd *cmd)
817{
818 int i;
819 uint result;
820 volatile tsec_t *phyregs = priv->phyregs;
821
822 phyregs->miimcfg = MIIMCFG_RESET;
823
824 phyregs->miimcfg = MIIMCFG_INIT_VALUE;
825
826 while(phyregs->miimind & MIIMIND_BUSY);
827
828 for(i=0;cmd->mii_reg != miim_end;i++) {
829 if(cmd->mii_data == miim_read) {
830 result = read_phy_reg(priv, cmd->mii_reg);
831
832 if(cmd->funct != NULL)
833 (*(cmd->funct))(result, priv);
834
835 } else {
836 if(cmd->funct != NULL)
837 result = (*(cmd->funct))(cmd->mii_reg, priv);
838 else
839 result = cmd->mii_data;
840
841 write_phy_reg(priv, cmd->mii_reg, result);
842
843 }
844 cmd++;
845 }
846}
847
848
849/* Relocate the function pointers in the phy cmd lists */
850static void relocate_cmds(void)
851{
852 struct phy_cmd **cmdlistptr;
853 struct phy_cmd *cmd;
854 int i,j,k;
855 DECLARE_GLOBAL_DATA_PTR;
856
857 for(i=0; phy_info[i]; i++) {
858 /* First thing's first: relocate the pointers to the
859 * PHY command structures (the structs were done) */
860 phy_info[i] = (struct phy_info *) ((uint)phy_info[i]
861 + gd->reloc_off);
862 phy_info[i]->name += gd->reloc_off;
863 phy_info[i]->config =
864 (struct phy_cmd *)((uint)phy_info[i]->config
865 + gd->reloc_off);
866 phy_info[i]->startup =
867 (struct phy_cmd *)((uint)phy_info[i]->startup
868 + gd->reloc_off);
869 phy_info[i]->shutdown =
870 (struct phy_cmd *)((uint)phy_info[i]->shutdown
871 + gd->reloc_off);
872
873 cmdlistptr = &phy_info[i]->config;
874 j=0;
875 for(;cmdlistptr <= &phy_info[i]->shutdown;cmdlistptr++) {
876 k=0;
877 for(cmd=*cmdlistptr;cmd->mii_reg != miim_end;cmd++) {
878 /* Only relocate non-NULL pointers */
879 if(cmd->funct)
880 cmd->funct += gd->reloc_off;
881
882 k++;
883 }
884 j++;
885 }
886 }
887
888 relocated = 1;
889}
890
891
wdenk7abf0c52004-04-18 21:45:42 +0000892#ifndef CONFIG_BITBANGMII
wdenk97d80fc2004-06-09 00:34:46 +0000893
894struct tsec_private * get_priv_for_phy(unsigned char phyaddr)
895{
896 int i;
897
898 for(i=0;i<MAXCONTROLLERS;i++) {
899 if(privlist[i]->phyaddr == phyaddr)
900 return privlist[i];
901 }
902
903 return NULL;
904}
905
wdenk7abf0c52004-04-18 21:45:42 +0000906/*
907 * Read a MII PHY register.
908 *
909 * Returns:
wdenk97d80fc2004-06-09 00:34:46 +0000910 * 0 on success
wdenk7abf0c52004-04-18 21:45:42 +0000911 */
wdenk97d80fc2004-06-09 00:34:46 +0000912int miiphy_read(unsigned char addr, unsigned char reg, unsigned short *value)
wdenk7abf0c52004-04-18 21:45:42 +0000913{
wdenk97d80fc2004-06-09 00:34:46 +0000914 unsigned short ret;
915 struct tsec_private *priv = get_priv_for_phy(addr);
wdenk7abf0c52004-04-18 21:45:42 +0000916
wdenk97d80fc2004-06-09 00:34:46 +0000917 if(NULL == priv) {
918 printf("Can't read PHY at address %d\n", addr);
919 return -1;
920 }
921
922 ret = (unsigned short)read_phy_reg(priv, reg);
923 *value = ret;
wdenk7abf0c52004-04-18 21:45:42 +0000924
925 return 0;
926}
927
928/*
929 * Write a MII PHY register.
930 *
931 * Returns:
wdenk97d80fc2004-06-09 00:34:46 +0000932 * 0 on success
wdenk7abf0c52004-04-18 21:45:42 +0000933 */
wdenk97d80fc2004-06-09 00:34:46 +0000934int miiphy_write(unsigned char addr, unsigned char reg, unsigned short value)
wdenk7abf0c52004-04-18 21:45:42 +0000935{
wdenk97d80fc2004-06-09 00:34:46 +0000936 struct tsec_private *priv = get_priv_for_phy(addr);
wdenk7abf0c52004-04-18 21:45:42 +0000937
wdenk97d80fc2004-06-09 00:34:46 +0000938 if(NULL == priv) {
939 printf("Can't write PHY at address %d\n", addr);
940 return -1;
941 }
942
943 write_phy_reg(priv, reg, value);
wdenk7abf0c52004-04-18 21:45:42 +0000944
945 return 0;
946}
wdenk97d80fc2004-06-09 00:34:46 +0000947
wdenk7abf0c52004-04-18 21:45:42 +0000948#endif /* CONFIG_BITBANGMII */
wdenk97d80fc2004-06-09 00:34:46 +0000949
wdenk42d1f032003-10-15 23:53:47 +0000950#endif /* CONFIG_TSEC_ENET */