blob: eee5f25d64b6b5247f04943c818199cb41f06313 [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
wdenk42d1f032003-10-15 23:53:47 +000028static uint rxIdx; /* index of the current RX buffer */
29static uint txIdx; /* index of the current TX buffer */
30
31typedef volatile struct rtxbd {
32 txbd8_t txbd[TX_BUF_CNT];
33 rxbd8_t rxbd[PKTBUFSRX];
34} RTXBD;
35
wdenk97d80fc2004-06-09 00:34:46 +000036struct tsec_info_struct {
37 unsigned int phyaddr;
38 unsigned int gigabit;
39 unsigned int phyregidx;
40};
41
42
43/* The tsec_info structure contains 3 values which the
44 * driver uses to determine how to operate a given ethernet
45 * device. For now, the structure is initialized with the
46 * knowledge that all current implementations have 2 TSEC
47 * devices, and one FEC. The information needed is:
48 * phyaddr - The address of the PHY which is attached to
wdenk9d46ea42005-03-14 23:56:42 +000049 * the given device.
wdenk97d80fc2004-06-09 00:34:46 +000050 *
51 * gigabit - This variable indicates whether the device
wdenk9d46ea42005-03-14 23:56:42 +000052 * supports gigabit speed ethernet
wdenk97d80fc2004-06-09 00:34:46 +000053 *
54 * phyregidx - This variable specifies which ethernet device
wdenk9d46ea42005-03-14 23:56:42 +000055 * controls the MII Management registers which are connected
56 * to the PHY. For 8540/8560, only TSEC1 (index 0) has
57 * access to the PHYs, so all of the entries have "0".
wdenk97d80fc2004-06-09 00:34:46 +000058 *
59 * The values specified in the table are taken from the board's
60 * config file in include/configs/. When implementing a new
61 * board with ethernet capability, it is necessary to define:
62 * TSEC1_PHY_ADDR
63 * TSEC1_PHYIDX
64 * TSEC2_PHY_ADDR
65 * TSEC2_PHYIDX
66 *
67 * and for 8560:
68 * FEC_PHY_ADDR
69 * FEC_PHYIDX
70 */
71static struct tsec_info_struct tsec_info[] = {
72#ifdef CONFIG_MPC85XX_TSEC1
73 {TSEC1_PHY_ADDR, 1, TSEC1_PHYIDX},
wdenk9d46ea42005-03-14 23:56:42 +000074#else
75 { 0, 0, 0},
wdenk97d80fc2004-06-09 00:34:46 +000076#endif
77#ifdef CONFIG_MPC85XX_TSEC2
78 {TSEC2_PHY_ADDR, 1, TSEC2_PHYIDX},
wdenk9d46ea42005-03-14 23:56:42 +000079#else
80 { 0, 0, 0},
wdenk97d80fc2004-06-09 00:34:46 +000081#endif
82#ifdef CONFIG_MPC85XX_FEC
83 {FEC_PHY_ADDR, 0, FEC_PHYIDX},
wdenk9d46ea42005-03-14 23:56:42 +000084#else
85 { 0, 0, 0},
wdenk97d80fc2004-06-09 00:34:46 +000086#endif
87};
88
89#define MAXCONTROLLERS 3
90
91static int relocated = 0;
92
93static struct tsec_private *privlist[MAXCONTROLLERS];
94
wdenk42d1f032003-10-15 23:53:47 +000095#ifdef __GNUC__
96static RTXBD rtx __attribute__ ((aligned(8)));
97#else
98#error "rtx must be 64-bit aligned"
99#endif
100
101static int tsec_send(struct eth_device* dev, volatile void *packet, int length);
102static int tsec_recv(struct eth_device* dev);
103static int tsec_init(struct eth_device* dev, bd_t * bd);
104static void tsec_halt(struct eth_device* dev);
wdenk97d80fc2004-06-09 00:34:46 +0000105static void init_registers(volatile tsec_t *regs);
106static void startup_tsec(struct eth_device *dev);
107static int init_phy(struct eth_device *dev);
108void write_phy_reg(struct tsec_private *priv, uint regnum, uint value);
109uint read_phy_reg(struct tsec_private *priv, uint regnum);
110struct phy_info * get_phy_info(struct eth_device *dev);
111void phy_run_commands(struct tsec_private *priv, struct phy_cmd *cmd);
112static void adjust_link(struct eth_device *dev);
113static void relocate_cmds(void);
wdenk7abf0c52004-04-18 21:45:42 +0000114
wdenk97d80fc2004-06-09 00:34:46 +0000115/* Initialize device structure. Returns success if PHY
116 * initialization succeeded (i.e. if it recognizes the PHY)
117 */
118int tsec_initialize(bd_t *bis, int index)
wdenk42d1f032003-10-15 23:53:47 +0000119{
120 struct eth_device* dev;
121 int i;
wdenk97d80fc2004-06-09 00:34:46 +0000122 struct tsec_private *priv;
wdenk42d1f032003-10-15 23:53:47 +0000123
124 dev = (struct eth_device*) malloc(sizeof *dev);
125
wdenk97d80fc2004-06-09 00:34:46 +0000126 if(NULL == dev)
wdenk42d1f032003-10-15 23:53:47 +0000127 return 0;
128
129 memset(dev, 0, sizeof *dev);
130
wdenk97d80fc2004-06-09 00:34:46 +0000131 priv = (struct tsec_private *) malloc(sizeof(*priv));
132
133 if(NULL == priv)
134 return 0;
135
136 privlist[index] = priv;
137 priv->regs = (volatile tsec_t *)(TSEC_BASE_ADDR + index*TSEC_SIZE);
138 priv->phyregs = (volatile tsec_t *)(TSEC_BASE_ADDR +
139 tsec_info[index].phyregidx*TSEC_SIZE);
140
141 priv->phyaddr = tsec_info[index].phyaddr;
142 priv->gigabit = tsec_info[index].gigabit;
143
wdenk6c9e7892005-03-15 22:56:53 +0000144 sprintf(dev->name, "ENET%d", index);
wdenk42d1f032003-10-15 23:53:47 +0000145 dev->iobase = 0;
wdenk97d80fc2004-06-09 00:34:46 +0000146 dev->priv = priv;
wdenk42d1f032003-10-15 23:53:47 +0000147 dev->init = tsec_init;
148 dev->halt = tsec_halt;
149 dev->send = tsec_send;
150 dev->recv = tsec_recv;
151
152 /* Tell u-boot to get the addr from the env */
153 for(i=0;i<6;i++)
154 dev->enetaddr[i] = 0;
155
156 eth_register(dev);
157
wdenk7abf0c52004-04-18 21:45:42 +0000158
wdenk97d80fc2004-06-09 00:34:46 +0000159 /* Reset the MAC */
160 priv->regs->maccfg1 |= MACCFG1_SOFT_RESET;
161 priv->regs->maccfg1 &= ~(MACCFG1_SOFT_RESET);
wdenk7abf0c52004-04-18 21:45:42 +0000162
wdenk97d80fc2004-06-09 00:34:46 +0000163 /* Try to initialize PHY here, and return */
164 return init_phy(dev);
wdenk42d1f032003-10-15 23:53:47 +0000165}
166
167
168/* Initializes data structures and registers for the controller,
wdenk9d46ea42005-03-14 23:56:42 +0000169 * and brings the interface up. Returns the link status, meaning
wdenk97d80fc2004-06-09 00:34:46 +0000170 * that it returns success if the link is up, failure otherwise.
171 * This allows u-boot to find the first active controller. */
wdenk42d1f032003-10-15 23:53:47 +0000172int tsec_init(struct eth_device* dev, bd_t * bd)
173{
wdenk42d1f032003-10-15 23:53:47 +0000174 uint tempval;
175 char tmpbuf[MAC_ADDR_LEN];
176 int i;
wdenk97d80fc2004-06-09 00:34:46 +0000177 struct tsec_private *priv = (struct tsec_private *)dev->priv;
178 volatile tsec_t *regs = priv->regs;
wdenk42d1f032003-10-15 23:53:47 +0000179
180 /* Make sure the controller is stopped */
181 tsec_halt(dev);
182
wdenk97d80fc2004-06-09 00:34:46 +0000183 /* Init MACCFG2. Defaults to GMII */
wdenk42d1f032003-10-15 23:53:47 +0000184 regs->maccfg2 = MACCFG2_INIT_SETTINGS;
185
186 /* Init ECNTRL */
187 regs->ecntrl = ECNTRL_INIT_SETTINGS;
188
189 /* Copy the station address into the address registers.
190 * Backwards, because little endian MACS are dumb */
191 for(i=0;i<MAC_ADDR_LEN;i++) {
wdenk97d80fc2004-06-09 00:34:46 +0000192 tmpbuf[MAC_ADDR_LEN - 1 - i] = dev->enetaddr[i];
wdenk42d1f032003-10-15 23:53:47 +0000193 }
194 (uint)(regs->macstnaddr1) = *((uint *)(tmpbuf));
195
196 tempval = *((uint *)(tmpbuf +4));
197
198 (uint)(regs->macstnaddr2) = tempval;
199
wdenk42d1f032003-10-15 23:53:47 +0000200 /* reset the indices to zero */
201 rxIdx = 0;
202 txIdx = 0;
203
204 /* Clear out (for the most part) the other registers */
205 init_registers(regs);
206
207 /* Ready the device for tx/rx */
wdenk97d80fc2004-06-09 00:34:46 +0000208 startup_tsec(dev);
wdenk42d1f032003-10-15 23:53:47 +0000209
wdenk97d80fc2004-06-09 00:34:46 +0000210 /* If there's no link, fail */
211 return priv->link;
wdenk42d1f032003-10-15 23:53:47 +0000212
213}
214
215
wdenk97d80fc2004-06-09 00:34:46 +0000216/* Write value to the device's PHY through the registers
217 * specified in priv, modifying the register specified in regnum.
218 * It will wait for the write to be done (or for a timeout to
219 * expire) before exiting
220 */
221void write_phy_reg(struct tsec_private *priv, uint regnum, uint value)
222{
223 volatile tsec_t *regbase = priv->phyregs;
224 uint phyid = priv->phyaddr;
225 int timeout=1000000;
226
227 regbase->miimadd = (phyid << 8) | regnum;
228 regbase->miimcon = value;
229 asm("msync");
230
231 timeout=1000000;
232 while((regbase->miimind & MIIMIND_BUSY) && timeout--);
233}
234
235
236/* Reads register regnum on the device's PHY through the
wdenk9d46ea42005-03-14 23:56:42 +0000237 * registers specified in priv. It lowers and raises the read
wdenk97d80fc2004-06-09 00:34:46 +0000238 * command, and waits for the data to become valid (miimind
239 * notvalid bit cleared), and the bus to cease activity (miimind
240 * busy bit cleared), and then returns the value
241 */
242uint read_phy_reg(struct tsec_private *priv, uint regnum)
wdenk42d1f032003-10-15 23:53:47 +0000243{
244 uint value;
wdenk97d80fc2004-06-09 00:34:46 +0000245 volatile tsec_t *regbase = priv->phyregs;
246 uint phyid = priv->phyaddr;
wdenk42d1f032003-10-15 23:53:47 +0000247
wdenk97d80fc2004-06-09 00:34:46 +0000248 /* Put the address of the phy, and the register
249 * number into MIIMADD */
250 regbase->miimadd = (phyid << 8) | regnum;
wdenk42d1f032003-10-15 23:53:47 +0000251
252 /* Clear the command register, and wait */
253 regbase->miimcom = 0;
254 asm("msync");
255
256 /* Initiate a read command, and wait */
257 regbase->miimcom = MIIM_READ_COMMAND;
258 asm("msync");
259
260 /* Wait for the the indication that the read is done */
261 while((regbase->miimind & (MIIMIND_NOTVALID | MIIMIND_BUSY)));
262
263 /* Grab the value read from the PHY */
264 value = regbase->miimstat;
265
266 return value;
267}
268
wdenk97d80fc2004-06-09 00:34:46 +0000269
270/* Discover which PHY is attached to the device, and configure it
271 * properly. If the PHY is not recognized, then return 0
272 * (failure). Otherwise, return 1
273 */
274static int init_phy(struct eth_device *dev)
wdenk42d1f032003-10-15 23:53:47 +0000275{
wdenk97d80fc2004-06-09 00:34:46 +0000276 struct tsec_private *priv = (struct tsec_private *)dev->priv;
277 struct phy_info *curphy;
wdenk42d1f032003-10-15 23:53:47 +0000278
279 /* Assign a Physical address to the TBI */
wdenk3dd7f0f2005-04-04 23:43:44 +0000280
281 {
282 volatile tsec_t *regs = (volatile tsec_t *)(TSEC_BASE_ADDR);
283 regs->tbipa = TBIPA_VALUE;
284 regs = (volatile tsec_t *)(TSEC_BASE_ADDR + TSEC_SIZE);
285 regs->tbipa = TBIPA_VALUE;
286 asm("msync");
287 }
288
289 /* Reset MII (due to new addresses) */
290 priv->phyregs->miimcfg = MIIMCFG_RESET;
291 asm("msync");
292 priv->phyregs->miimcfg = MIIMCFG_INIT_VALUE;
293 asm("msync");
294 while(priv->phyregs->miimind & MIIMIND_BUSY);
wdenk42d1f032003-10-15 23:53:47 +0000295
wdenk97d80fc2004-06-09 00:34:46 +0000296 if(0 == relocated)
297 relocate_cmds();
wdenk42d1f032003-10-15 23:53:47 +0000298
wdenk97d80fc2004-06-09 00:34:46 +0000299 /* Get the cmd structure corresponding to the attached
300 * PHY */
301 curphy = get_phy_info(dev);
wdenk42d1f032003-10-15 23:53:47 +0000302
wdenk97d80fc2004-06-09 00:34:46 +0000303 if(NULL == curphy) {
304 printf("%s: No PHY found\n", dev->name);
wdenk42d1f032003-10-15 23:53:47 +0000305
wdenk97d80fc2004-06-09 00:34:46 +0000306 return 0;
wdenk42d1f032003-10-15 23:53:47 +0000307 }
308
wdenk97d80fc2004-06-09 00:34:46 +0000309 priv->phyinfo = curphy;
wdenk42d1f032003-10-15 23:53:47 +0000310
wdenk97d80fc2004-06-09 00:34:46 +0000311 phy_run_commands(priv, priv->phyinfo->config);
wdenk42d1f032003-10-15 23:53:47 +0000312
wdenk97d80fc2004-06-09 00:34:46 +0000313 return 1;
wdenk42d1f032003-10-15 23:53:47 +0000314}
315
316
wdenk97d80fc2004-06-09 00:34:46 +0000317/* Returns which value to write to the control register. */
318/* For 10/100, the value is slightly different */
319uint mii_cr_init(uint mii_reg, struct tsec_private *priv)
320{
321 if(priv->gigabit)
322 return MIIM_CONTROL_INIT;
323 else
324 return MIIM_CR_INIT;
325}
326
327
328/* Parse the status register for link, and then do
329 * auto-negotiation */
330uint mii_parse_sr(uint mii_reg, struct tsec_private *priv)
331{
332 uint timeout = TSEC_TIMEOUT;
333
334 if(mii_reg & MIIM_STATUS_LINK)
335 priv->link = 1;
336 else
337 priv->link = 0;
338
339 if(priv->link) {
340 while((!(mii_reg & MIIM_STATUS_AN_DONE)) && timeout--)
341 mii_reg = read_phy_reg(priv, MIIM_STATUS);
342 }
343
344 return 0;
345}
346
347
348/* Parse the 88E1011's status register for speed and duplex
349 * information */
350uint mii_parse_88E1011_psr(uint mii_reg, struct tsec_private *priv)
351{
352 uint speed;
353
354 if(mii_reg & MIIM_88E1011_PHYSTAT_DUPLEX)
355 priv->duplexity = 1;
356 else
357 priv->duplexity = 0;
358
359 speed = (mii_reg &MIIM_88E1011_PHYSTAT_SPEED);
360
361 switch(speed) {
362 case MIIM_88E1011_PHYSTAT_GBIT:
363 priv->speed = 1000;
364 break;
365 case MIIM_88E1011_PHYSTAT_100:
366 priv->speed = 100;
367 break;
368 default:
369 priv->speed = 10;
370 }
371
372 return 0;
373}
374
375
376/* Parse the cis8201's status register for speed and duplex
377 * information */
378uint mii_parse_cis8201(uint mii_reg, struct tsec_private *priv)
379{
380 uint speed;
381
382 if(mii_reg & MIIM_CIS8201_AUXCONSTAT_DUPLEX)
383 priv->duplexity = 1;
384 else
385 priv->duplexity = 0;
386
387 speed = mii_reg & MIIM_CIS8201_AUXCONSTAT_SPEED;
388 switch(speed) {
389 case MIIM_CIS8201_AUXCONSTAT_GBIT:
390 priv->speed = 1000;
391 break;
392 case MIIM_CIS8201_AUXCONSTAT_100:
393 priv->speed = 100;
394 break;
395 default:
396 priv->speed = 10;
397 break;
398 }
399
400 return 0;
401}
402
403
404/* Parse the DM9161's status register for speed and duplex
405 * information */
406uint mii_parse_dm9161_scsr(uint mii_reg, struct tsec_private *priv)
407{
408 if(mii_reg & (MIIM_DM9161_SCSR_100F | MIIM_DM9161_SCSR_100H))
409 priv->speed = 100;
410 else
411 priv->speed = 10;
412
413 if(mii_reg & (MIIM_DM9161_SCSR_100F | MIIM_DM9161_SCSR_10F))
414 priv->duplexity = 1;
415 else
416 priv->duplexity = 0;
417
418 return 0;
419}
420
421
422/* Hack to write all 4 PHYs with the LED values */
423uint mii_cis8204_fixled(uint mii_reg, struct tsec_private *priv)
424{
425 uint phyid;
426 volatile tsec_t *regbase = priv->phyregs;
427 int timeout=1000000;
428
429 for(phyid=0;phyid<4;phyid++) {
430 regbase->miimadd = (phyid << 8) | mii_reg;
431 regbase->miimcon = MIIM_CIS8204_SLEDCON_INIT;
432 asm("msync");
433
434 timeout=1000000;
435 while((regbase->miimind & MIIMIND_BUSY) && timeout--);
436 }
437
438 return MIIM_CIS8204_SLEDCON_INIT;
439}
440
441
442/* Initialized required registers to appropriate values, zeroing
443 * those we don't care about (unless zero is bad, in which case,
444 * choose a more appropriate value) */
445static void init_registers(volatile tsec_t *regs)
wdenk42d1f032003-10-15 23:53:47 +0000446{
447 /* Clear IEVENT */
448 regs->ievent = IEVENT_INIT_CLEAR;
449
450 regs->imask = IMASK_INIT_CLEAR;
451
452 regs->hash.iaddr0 = 0;
453 regs->hash.iaddr1 = 0;
454 regs->hash.iaddr2 = 0;
455 regs->hash.iaddr3 = 0;
456 regs->hash.iaddr4 = 0;
457 regs->hash.iaddr5 = 0;
458 regs->hash.iaddr6 = 0;
459 regs->hash.iaddr7 = 0;
460
461 regs->hash.gaddr0 = 0;
462 regs->hash.gaddr1 = 0;
463 regs->hash.gaddr2 = 0;
464 regs->hash.gaddr3 = 0;
465 regs->hash.gaddr4 = 0;
466 regs->hash.gaddr5 = 0;
467 regs->hash.gaddr6 = 0;
468 regs->hash.gaddr7 = 0;
469
470 regs->rctrl = 0x00000000;
471
472 /* Init RMON mib registers */
473 memset((void *)&(regs->rmon), 0, sizeof(rmon_mib_t));
474
475 regs->rmon.cam1 = 0xffffffff;
476 regs->rmon.cam2 = 0xffffffff;
477
478 regs->mrblr = MRBLR_INIT_SETTINGS;
479
480 regs->minflr = MINFLR_INIT_SETTINGS;
481
482 regs->attr = ATTR_INIT_SETTINGS;
483 regs->attreli = ATTRELI_INIT_SETTINGS;
484
485}
486
wdenk97d80fc2004-06-09 00:34:46 +0000487
488/* Configure maccfg2 based on negotiated speed and duplex
489 * reported by PHY handling code */
490static void adjust_link(struct eth_device *dev)
491{
492 struct tsec_private *priv = (struct tsec_private *)dev->priv;
493 volatile tsec_t *regs = priv->regs;
494
495 if(priv->link) {
496 if(priv->duplexity != 0)
497 regs->maccfg2 |= MACCFG2_FULL_DUPLEX;
498 else
499 regs->maccfg2 &= ~(MACCFG2_FULL_DUPLEX);
500
501 switch(priv->speed) {
502 case 1000:
503 regs->maccfg2 = ((regs->maccfg2&~(MACCFG2_IF))
504 | MACCFG2_GMII);
505 break;
506 case 100:
507 case 10:
508 regs->maccfg2 = ((regs->maccfg2&~(MACCFG2_IF))
509 | MACCFG2_MII);
510 break;
511 default:
512 printf("%s: Speed was bad\n", dev->name);
513 break;
514 }
515
516 printf("Speed: %d, %s duplex\n", priv->speed,
517 (priv->duplexity) ? "full" : "half");
518
519 } else {
520 printf("%s: No link.\n", dev->name);
521 }
522}
523
524
525/* Set up the buffers and their descriptors, and bring up the
526 * interface */
527static void startup_tsec(struct eth_device *dev)
wdenk42d1f032003-10-15 23:53:47 +0000528{
529 int i;
wdenk97d80fc2004-06-09 00:34:46 +0000530 struct tsec_private *priv = (struct tsec_private *)dev->priv;
531 volatile tsec_t *regs = priv->regs;
wdenk42d1f032003-10-15 23:53:47 +0000532
533 /* Point to the buffer descriptors */
534 regs->tbase = (unsigned int)(&rtx.txbd[txIdx]);
535 regs->rbase = (unsigned int)(&rtx.rxbd[rxIdx]);
536
537 /* Initialize the Rx Buffer descriptors */
538 for (i = 0; i < PKTBUFSRX; i++) {
539 rtx.rxbd[i].status = RXBD_EMPTY;
540 rtx.rxbd[i].length = 0;
541 rtx.rxbd[i].bufPtr = (uint)NetRxPackets[i];
542 }
543 rtx.rxbd[PKTBUFSRX -1].status |= RXBD_WRAP;
544
545 /* Initialize the TX Buffer Descriptors */
546 for(i=0; i<TX_BUF_CNT; i++) {
547 rtx.txbd[i].status = 0;
548 rtx.txbd[i].length = 0;
549 rtx.txbd[i].bufPtr = 0;
550 }
551 rtx.txbd[TX_BUF_CNT -1].status |= TXBD_WRAP;
552
wdenk97d80fc2004-06-09 00:34:46 +0000553 /* Start up the PHY */
554 phy_run_commands(priv, priv->phyinfo->startup);
555 adjust_link(dev);
556
wdenk42d1f032003-10-15 23:53:47 +0000557 /* Enable Transmit and Receive */
558 regs->maccfg1 |= (MACCFG1_RX_EN | MACCFG1_TX_EN);
559
560 /* Tell the DMA it is clear to go */
561 regs->dmactrl |= DMACTRL_INIT_SETTINGS;
562 regs->tstat = TSTAT_CLEAR_THALT;
563 regs->dmactrl &= ~(DMACTRL_GRS | DMACTRL_GTS);
564}
565
wdenk9d46ea42005-03-14 23:56:42 +0000566/* This returns the status bits of the device. The return value
wdenk42d1f032003-10-15 23:53:47 +0000567 * is never checked, and this is what the 8260 driver did, so we
wdenk9d46ea42005-03-14 23:56:42 +0000568 * do the same. Presumably, this would be zero if there were no
wdenk42d1f032003-10-15 23:53:47 +0000569 * errors */
570static int tsec_send(struct eth_device* dev, volatile void *packet, int length)
571{
572 int i;
573 int result = 0;
wdenk97d80fc2004-06-09 00:34:46 +0000574 struct tsec_private *priv = (struct tsec_private *)dev->priv;
575 volatile tsec_t *regs = priv->regs;
wdenk42d1f032003-10-15 23:53:47 +0000576
577 /* Find an empty buffer descriptor */
578 for(i=0; rtx.txbd[txIdx].status & TXBD_READY; i++) {
579 if (i >= TOUT_LOOP) {
wdenk8b07a112004-07-10 21:45:47 +0000580 debug ("%s: tsec: tx buffers full\n", dev->name);
wdenk42d1f032003-10-15 23:53:47 +0000581 return result;
582 }
583 }
584
585 rtx.txbd[txIdx].bufPtr = (uint)packet;
586 rtx.txbd[txIdx].length = length;
587 rtx.txbd[txIdx].status |= (TXBD_READY | TXBD_LAST | TXBD_CRC | TXBD_INTERRUPT);
588
589 /* Tell the DMA to go */
590 regs->tstat = TSTAT_CLEAR_THALT;
591
592 /* Wait for buffer to be transmitted */
593 for(i=0; rtx.txbd[txIdx].status & TXBD_READY; i++) {
594 if (i >= TOUT_LOOP) {
wdenk8b07a112004-07-10 21:45:47 +0000595 debug ("%s: tsec: tx error\n", dev->name);
wdenk42d1f032003-10-15 23:53:47 +0000596 return result;
597 }
598 }
599
600 txIdx = (txIdx + 1) % TX_BUF_CNT;
601 result = rtx.txbd[txIdx].status & TXBD_STATS;
602
603 return result;
604}
605
606static int tsec_recv(struct eth_device* dev)
607{
608 int length;
wdenk97d80fc2004-06-09 00:34:46 +0000609 struct tsec_private *priv = (struct tsec_private *)dev->priv;
610 volatile tsec_t *regs = priv->regs;
wdenk42d1f032003-10-15 23:53:47 +0000611
612 while(!(rtx.rxbd[rxIdx].status & RXBD_EMPTY)) {
613
614 length = rtx.rxbd[rxIdx].length;
615
616 /* Send the packet up if there were no errors */
617 if (!(rtx.rxbd[rxIdx].status & RXBD_STATS)) {
618 NetReceive(NetRxPackets[rxIdx], length - 4);
wdenk97d80fc2004-06-09 00:34:46 +0000619 } else {
620 printf("Got error %x\n",
621 (rtx.rxbd[rxIdx].status & RXBD_STATS));
wdenk42d1f032003-10-15 23:53:47 +0000622 }
623
624 rtx.rxbd[rxIdx].length = 0;
625
626 /* Set the wrap bit if this is the last element in the list */
627 rtx.rxbd[rxIdx].status = RXBD_EMPTY | (((rxIdx + 1) == PKTBUFSRX) ? RXBD_WRAP : 0);
628
629 rxIdx = (rxIdx + 1) % PKTBUFSRX;
630 }
631
632 if(regs->ievent&IEVENT_BSY) {
633 regs->ievent = IEVENT_BSY;
634 regs->rstat = RSTAT_CLEAR_RHALT;
635 }
636
637 return -1;
638
639}
640
641
wdenk97d80fc2004-06-09 00:34:46 +0000642/* Stop the interface */
wdenk42d1f032003-10-15 23:53:47 +0000643static void tsec_halt(struct eth_device* dev)
644{
wdenk97d80fc2004-06-09 00:34:46 +0000645 struct tsec_private *priv = (struct tsec_private *)dev->priv;
646 volatile tsec_t *regs = priv->regs;
wdenk42d1f032003-10-15 23:53:47 +0000647
648 regs->dmactrl &= ~(DMACTRL_GRS | DMACTRL_GTS);
649 regs->dmactrl |= (DMACTRL_GRS | DMACTRL_GTS);
650
651 while(!(regs->ievent & (IEVENT_GRSC | IEVENT_GTSC)));
652
653 regs->maccfg1 &= ~(MACCFG1_TX_EN | MACCFG1_RX_EN);
654
wdenk97d80fc2004-06-09 00:34:46 +0000655 /* Shut down the PHY, as needed */
656 phy_run_commands(priv, priv->phyinfo->shutdown);
wdenk42d1f032003-10-15 23:53:47 +0000657}
wdenk7abf0c52004-04-18 21:45:42 +0000658
wdenk97d80fc2004-06-09 00:34:46 +0000659
660struct phy_info phy_info_M88E1011S = {
661 0x01410c6,
662 "Marvell 88E1011S",
663 4,
664 (struct phy_cmd[]) { /* config */
665 /* Reset and configure the PHY */
666 {MIIM_CONTROL, MIIM_CONTROL_RESET, NULL},
667 {0x1d, 0x1f, NULL},
668 {0x1e, 0x200c, NULL},
669 {0x1d, 0x5, NULL},
670 {0x1e, 0x0, NULL},
671 {0x1e, 0x100, NULL},
672 {MIIM_GBIT_CONTROL, MIIM_GBIT_CONTROL_INIT, NULL},
673 {MIIM_ANAR, MIIM_ANAR_INIT, NULL},
674 {MIIM_CONTROL, MIIM_CONTROL_RESET, NULL},
675 {MIIM_CONTROL, MIIM_CONTROL_INIT, &mii_cr_init},
676 {miim_end,}
677 },
678 (struct phy_cmd[]) { /* startup */
679 /* Status is read once to clear old link state */
680 {MIIM_STATUS, miim_read, NULL},
681 /* Auto-negotiate */
682 {MIIM_STATUS, miim_read, &mii_parse_sr},
683 /* Read the status */
684 {MIIM_88E1011_PHY_STATUS, miim_read, &mii_parse_88E1011_psr},
685 {miim_end,}
686 },
687 (struct phy_cmd[]) { /* shutdown */
688 {miim_end,}
689 },
690};
691
wdenk9d46ea42005-03-14 23:56:42 +0000692struct phy_info phy_info_M88E1111S = {
693 0x01410cc,
694 "Marvell 88E1111S",
695 4,
696 (struct phy_cmd[]) { /* config */
697 /* Reset and configure the PHY */
698 {MIIM_CONTROL, MIIM_CONTROL_RESET, NULL},
699 {0x1d, 0x1f, NULL},
700 {0x1e, 0x200c, NULL},
701 {0x1d, 0x5, NULL},
702 {0x1e, 0x0, NULL},
703 {0x1e, 0x100, NULL},
704 {MIIM_GBIT_CONTROL, MIIM_GBIT_CONTROL_INIT, NULL},
705 {MIIM_ANAR, MIIM_ANAR_INIT, NULL},
706 {MIIM_CONTROL, MIIM_CONTROL_RESET, NULL},
707 {MIIM_CONTROL, MIIM_CONTROL_INIT, &mii_cr_init},
708 {miim_end,}
709 },
710 (struct phy_cmd[]) { /* startup */
711 /* Status is read once to clear old link state */
712 {MIIM_STATUS, miim_read, NULL},
713 /* Auto-negotiate */
714 {MIIM_STATUS, miim_read, &mii_parse_sr},
715 /* Read the status */
716 {MIIM_88E1011_PHY_STATUS, miim_read, &mii_parse_88E1011_psr},
717 {miim_end,}
718 },
719 (struct phy_cmd[]) { /* shutdown */
720 {miim_end,}
721 },
722};
723
wdenk97d80fc2004-06-09 00:34:46 +0000724struct phy_info phy_info_cis8204 = {
725 0x3f11,
726 "Cicada Cis8204",
727 6,
728 (struct phy_cmd[]) { /* config */
729 /* Override PHY config settings */
730 {MIIM_CIS8201_AUX_CONSTAT, MIIM_CIS8201_AUXCONSTAT_INIT, NULL},
731 /* Configure some basic stuff */
732 {MIIM_CONTROL, MIIM_CONTROL_INIT, &mii_cr_init},
733 {MIIM_CIS8204_SLED_CON, MIIM_CIS8204_SLEDCON_INIT, &mii_cis8204_fixled},
734 {MIIM_CIS8204_EPHY_CON, MIIM_CIS8204_EPHYCON_INIT, NULL},
735 {miim_end,}
736 },
737 (struct phy_cmd[]) { /* startup */
738 /* Read the Status (2x to make sure link is right) */
739 {MIIM_STATUS, miim_read, NULL},
740 /* Auto-negotiate */
741 {MIIM_STATUS, miim_read, &mii_parse_sr},
742 /* Read the status */
743 {MIIM_CIS8201_AUX_CONSTAT, miim_read, &mii_parse_cis8201},
744 {miim_end,}
745 },
746 (struct phy_cmd[]) { /* shutdown */
747 {miim_end,}
748 },
749};
750
751/* Cicada 8201 */
752struct phy_info phy_info_cis8201 = {
753 0xfc41,
754 "CIS8201",
755 4,
756 (struct phy_cmd[]) { /* config */
757 /* Override PHY config settings */
758 {MIIM_CIS8201_AUX_CONSTAT, MIIM_CIS8201_AUXCONSTAT_INIT, NULL},
759 /* Set up the interface mode */
760 {MIIM_CIS8201_EXT_CON1, MIIM_CIS8201_EXTCON1_INIT, NULL},
761 /* Configure some basic stuff */
762 {MIIM_CONTROL, MIIM_CONTROL_INIT, &mii_cr_init},
763 {miim_end,}
764 },
765 (struct phy_cmd[]) { /* startup */
766 /* Read the Status (2x to make sure link is right) */
767 {MIIM_STATUS, miim_read, NULL},
768 /* Auto-negotiate */
769 {MIIM_STATUS, miim_read, &mii_parse_sr},
770 /* Read the status */
771 {MIIM_CIS8201_AUX_CONSTAT, miim_read, &mii_parse_cis8201},
772 {miim_end,}
773 },
774 (struct phy_cmd[]) { /* shutdown */
775 {miim_end,}
776 },
777};
778
779
780struct phy_info phy_info_dm9161 = {
781 0x0181b88,
782 "Davicom DM9161E",
783 4,
784 (struct phy_cmd[]) { /* config */
785 {MIIM_CONTROL, MIIM_DM9161_CR_STOP, NULL},
786 /* Do not bypass the scrambler/descrambler */
787 {MIIM_DM9161_SCR, MIIM_DM9161_SCR_INIT, NULL},
788 /* Clear 10BTCSR to default */
789 {MIIM_DM9161_10BTCSR, MIIM_DM9161_10BTCSR_INIT, NULL},
790 /* Configure some basic stuff */
791 {MIIM_CONTROL, MIIM_CR_INIT, NULL},
792 /* Restart Auto Negotiation */
793 {MIIM_CONTROL, MIIM_DM9161_CR_RSTAN, NULL},
794 {miim_end,}
795 },
796 (struct phy_cmd[]) { /* startup */
797 /* Status is read once to clear old link state */
798 {MIIM_STATUS, miim_read, NULL},
799 /* Auto-negotiate */
800 {MIIM_STATUS, miim_read, &mii_parse_sr},
801 /* Read the status */
802 {MIIM_DM9161_SCSR, miim_read, &mii_parse_dm9161_scsr},
803 {miim_end,}
804 },
805 (struct phy_cmd[]) { /* shutdown */
806 {miim_end,}
807 },
808};
809
wdenk3dd7f0f2005-04-04 23:43:44 +0000810uint mii_parse_lxt971_sr2(uint mii_reg, struct tsec_private *priv)
811{
812 unsigned int speed;
813 if (priv->link) {
814 speed = mii_reg & MIIM_LXT971_SR2_SPEED_MASK;
815
816 switch (speed) {
817 case MIIM_LXT971_SR2_10HDX:
818 priv->speed = 10;
819 priv->duplexity = 0;
820 break;
821 case MIIM_LXT971_SR2_10FDX:
822 priv->speed = 10;
823 priv->duplexity = 1;
824 break;
825 case MIIM_LXT971_SR2_100HDX:
826 priv->speed = 100;
827 priv->duplexity = 0;
828 default:
829 priv->speed = 100;
830 priv->duplexity = 1;
831 break;
832 }
833 } else {
834 priv->speed = 0;
835 priv->duplexity = 0;
836 }
837
838 return 0;
839}
840
wdenk9d46ea42005-03-14 23:56:42 +0000841static struct phy_info phy_info_lxt971 = {
842 0x0001378e,
843 "LXT971",
844 4,
845 (struct phy_cmd []) { /* config */
wdenk3dd7f0f2005-04-04 23:43:44 +0000846 { MIIM_CR, MIIM_CR_INIT, mii_cr_init }, /* autonegotiate */
wdenk9d46ea42005-03-14 23:56:42 +0000847 { miim_end, }
848 },
849 (struct phy_cmd []) { /* startup - enable interrupts */
850 /* { 0x12, 0x00f2, NULL }, */
wdenk9d46ea42005-03-14 23:56:42 +0000851 { MIIM_STATUS, miim_read, NULL },
wdenk3dd7f0f2005-04-04 23:43:44 +0000852 { MIIM_STATUS, miim_read, &mii_parse_sr },
853 { MIIM_LXT971_SR2, miim_read, &mii_parse_lxt971_sr2 },
wdenk9d46ea42005-03-14 23:56:42 +0000854 { miim_end, }
855 },
856 (struct phy_cmd []) { /* shutdown - disable interrupts */
857 { miim_end, }
858 },
859};
860
wdenk97d80fc2004-06-09 00:34:46 +0000861struct phy_info *phy_info[] = {
862#if 0
863 &phy_info_cis8201,
864#endif
865 &phy_info_cis8204,
866 &phy_info_M88E1011S,
wdenk9d46ea42005-03-14 23:56:42 +0000867 &phy_info_M88E1111S,
wdenk97d80fc2004-06-09 00:34:46 +0000868 &phy_info_dm9161,
wdenk9d46ea42005-03-14 23:56:42 +0000869 &phy_info_lxt971,
wdenk97d80fc2004-06-09 00:34:46 +0000870 NULL
871};
872
873
874/* Grab the identifier of the device's PHY, and search through
wdenk9d46ea42005-03-14 23:56:42 +0000875 * all of the known PHYs to see if one matches. If so, return
wdenk97d80fc2004-06-09 00:34:46 +0000876 * it, if not, return NULL */
877struct phy_info * get_phy_info(struct eth_device *dev)
878{
879 struct tsec_private *priv = (struct tsec_private *)dev->priv;
880 uint phy_reg, phy_ID;
881 int i;
882 struct phy_info *theInfo = NULL;
883
884 /* Grab the bits from PHYIR1, and put them in the upper half */
885 phy_reg = read_phy_reg(priv, MIIM_PHYIR1);
886 phy_ID = (phy_reg & 0xffff) << 16;
887
888 /* Grab the bits from PHYIR2, and put them in the lower half */
889 phy_reg = read_phy_reg(priv, MIIM_PHYIR2);
890 phy_ID |= (phy_reg & 0xffff);
891
892 /* loop through all the known PHY types, and find one that */
893 /* matches the ID we read from the PHY. */
894 for(i=0; phy_info[i]; i++) {
895 if(phy_info[i]->id == (phy_ID >> phy_info[i]->shift))
896 theInfo = phy_info[i];
897 }
898
899 if(theInfo == NULL)
900 {
901 printf("%s: PHY id %x is not supported!\n", dev->name, phy_ID);
902 return NULL;
903 } else {
904 printf("%s: PHY is %s (%x)\n", dev->name, theInfo->name,
905 phy_ID);
906 }
907
908 return theInfo;
909}
910
911
912/* Execute the given series of commands on the given device's
913 * PHY, running functions as necessary*/
914void phy_run_commands(struct tsec_private *priv, struct phy_cmd *cmd)
915{
916 int i;
917 uint result;
918 volatile tsec_t *phyregs = priv->phyregs;
919
920 phyregs->miimcfg = MIIMCFG_RESET;
921
922 phyregs->miimcfg = MIIMCFG_INIT_VALUE;
923
924 while(phyregs->miimind & MIIMIND_BUSY);
925
926 for(i=0;cmd->mii_reg != miim_end;i++) {
927 if(cmd->mii_data == miim_read) {
928 result = read_phy_reg(priv, cmd->mii_reg);
929
930 if(cmd->funct != NULL)
931 (*(cmd->funct))(result, priv);
932
933 } else {
934 if(cmd->funct != NULL)
935 result = (*(cmd->funct))(cmd->mii_reg, priv);
936 else
937 result = cmd->mii_data;
938
939 write_phy_reg(priv, cmd->mii_reg, result);
940
941 }
942 cmd++;
943 }
944}
945
946
947/* Relocate the function pointers in the phy cmd lists */
948static void relocate_cmds(void)
949{
950 struct phy_cmd **cmdlistptr;
951 struct phy_cmd *cmd;
952 int i,j,k;
953 DECLARE_GLOBAL_DATA_PTR;
954
955 for(i=0; phy_info[i]; i++) {
956 /* First thing's first: relocate the pointers to the
957 * PHY command structures (the structs were done) */
958 phy_info[i] = (struct phy_info *) ((uint)phy_info[i]
959 + gd->reloc_off);
960 phy_info[i]->name += gd->reloc_off;
961 phy_info[i]->config =
962 (struct phy_cmd *)((uint)phy_info[i]->config
963 + gd->reloc_off);
964 phy_info[i]->startup =
965 (struct phy_cmd *)((uint)phy_info[i]->startup
966 + gd->reloc_off);
967 phy_info[i]->shutdown =
968 (struct phy_cmd *)((uint)phy_info[i]->shutdown
969 + gd->reloc_off);
970
971 cmdlistptr = &phy_info[i]->config;
972 j=0;
973 for(;cmdlistptr <= &phy_info[i]->shutdown;cmdlistptr++) {
974 k=0;
975 for(cmd=*cmdlistptr;cmd->mii_reg != miim_end;cmd++) {
976 /* Only relocate non-NULL pointers */
977 if(cmd->funct)
978 cmd->funct += gd->reloc_off;
979
980 k++;
981 }
982 j++;
983 }
984 }
985
986 relocated = 1;
987}
988
989
wdenk7abf0c52004-04-18 21:45:42 +0000990#ifndef CONFIG_BITBANGMII
wdenk97d80fc2004-06-09 00:34:46 +0000991
992struct tsec_private * get_priv_for_phy(unsigned char phyaddr)
993{
994 int i;
995
996 for(i=0;i<MAXCONTROLLERS;i++) {
997 if(privlist[i]->phyaddr == phyaddr)
998 return privlist[i];
999 }
1000
1001 return NULL;
1002}
1003
wdenk7abf0c52004-04-18 21:45:42 +00001004/*
1005 * Read a MII PHY register.
1006 *
1007 * Returns:
wdenk97d80fc2004-06-09 00:34:46 +00001008 * 0 on success
wdenk7abf0c52004-04-18 21:45:42 +00001009 */
wdenk97d80fc2004-06-09 00:34:46 +00001010int miiphy_read(unsigned char addr, unsigned char reg, unsigned short *value)
wdenk7abf0c52004-04-18 21:45:42 +00001011{
wdenk97d80fc2004-06-09 00:34:46 +00001012 unsigned short ret;
1013 struct tsec_private *priv = get_priv_for_phy(addr);
wdenk7abf0c52004-04-18 21:45:42 +00001014
wdenk97d80fc2004-06-09 00:34:46 +00001015 if(NULL == priv) {
1016 printf("Can't read PHY at address %d\n", addr);
1017 return -1;
1018 }
1019
1020 ret = (unsigned short)read_phy_reg(priv, reg);
1021 *value = ret;
wdenk7abf0c52004-04-18 21:45:42 +00001022
1023 return 0;
1024}
1025
1026/*
1027 * Write a MII PHY register.
1028 *
1029 * Returns:
wdenk97d80fc2004-06-09 00:34:46 +00001030 * 0 on success
wdenk7abf0c52004-04-18 21:45:42 +00001031 */
wdenk97d80fc2004-06-09 00:34:46 +00001032int miiphy_write(unsigned char addr, unsigned char reg, unsigned short value)
wdenk7abf0c52004-04-18 21:45:42 +00001033{
wdenk97d80fc2004-06-09 00:34:46 +00001034 struct tsec_private *priv = get_priv_for_phy(addr);
wdenk7abf0c52004-04-18 21:45:42 +00001035
wdenk97d80fc2004-06-09 00:34:46 +00001036 if(NULL == priv) {
1037 printf("Can't write PHY at address %d\n", addr);
1038 return -1;
1039 }
1040
1041 write_phy_reg(priv, reg, value);
wdenk7abf0c52004-04-18 21:45:42 +00001042
1043 return 0;
1044}
wdenk97d80fc2004-06-09 00:34:46 +00001045
wdenk7abf0c52004-04-18 21:45:42 +00001046#endif /* CONFIG_BITBANGMII */
wdenk97d80fc2004-06-09 00:34:46 +00001047
wdenk42d1f032003-10-15 23:53:47 +00001048#endif /* CONFIG_TSEC_ENET */