3 * Motorola Three Speed Ethernet Controller driver
5 * This software may be used and distributed according to the
6 * terms of the GNU Public License, Version 2, incorporated
9 * (C) Copyright 2003, Motorola, Inc.
10 * maintained by Xianghua Xiao (x.xiao@motorola.com)
22 #if defined(CONFIG_TSEC_ENET)
29 #define DBGPRINT(x) printf(x)
34 static uint rxIdx; /* index of the current RX buffer */
35 static uint txIdx; /* index of the current TX buffer */
37 typedef volatile struct rtxbd {
38 txbd8_t txbd[TX_BUF_CNT];
39 rxbd8_t rxbd[PKTBUFSRX];
43 static RTXBD rtx __attribute__ ((aligned(8)));
45 #error "rtx must be 64-bit aligned"
48 static int tsec_send(struct eth_device* dev, volatile void *packet, int length);
49 static int tsec_recv(struct eth_device* dev);
50 static int tsec_init(struct eth_device* dev, bd_t * bd);
51 static void tsec_halt(struct eth_device* dev);
52 static void init_registers(tsec_t *regs);
53 static void startup_tsec(tsec_t *regs);
54 static void init_phy(tsec_t *regs);
56 /* Initialize device structure. returns 0 on failure, 1 on
58 int tsec_initialize(bd_t *bis)
60 struct eth_device* dev;
63 dev = (struct eth_device*) malloc(sizeof *dev);
68 memset(dev, 0, sizeof *dev);
70 sprintf(dev->name, "MOTOROLA ETHERNET");
73 dev->init = tsec_init;
74 dev->halt = tsec_halt;
75 dev->send = tsec_send;
76 dev->recv = tsec_recv;
78 /* Tell u-boot to get the addr from the env */
88 /* Initializes data structures and registers for the controller,
89 * and brings the interface up */
90 int tsec_init(struct eth_device* dev, bd_t * bd)
94 char tmpbuf[MAC_ADDR_LEN];
97 regs = (tsec_t *)(TSEC_BASE_ADDR);
99 /* Make sure the controller is stopped */
103 regs->maccfg1 |= MACCFG1_SOFT_RESET;
105 /* Clear MACCFG1[Soft_Reset] */
106 regs->maccfg1 &= ~(MACCFG1_SOFT_RESET);
108 /* Init MACCFG2. Defaults to GMII/MII */
109 regs->maccfg2 = MACCFG2_INIT_SETTINGS;
112 regs->ecntrl = ECNTRL_INIT_SETTINGS;
114 /* Copy the station address into the address registers.
115 * Backwards, because little endian MACS are dumb */
116 for(i=0;i<MAC_ADDR_LEN;i++) {
117 tmpbuf[MAC_ADDR_LEN - 1 - i] = bd->bi_enetaddr[i];
119 (uint)(regs->macstnaddr1) = *((uint *)(tmpbuf));
121 tempval = *((uint *)(tmpbuf +4));
123 (uint)(regs->macstnaddr2) = tempval;
125 /* Initialize the PHY */
128 /* reset the indices to zero */
132 /* Clear out (for the most part) the other registers */
133 init_registers(regs);
135 /* Ready the device for tx/rx */
143 /* Reads from the register at offset in the PHY at phyid, */
144 /* using the register set defined in regbase. It waits until the */
145 /* bits in the miimstat are valid (miimind notvalid bit cleared), */
146 /* and then passes those bits on to the variable specified in */
148 /* Before it does the read, it needs to clear the command field */
149 uint read_phy_reg(tsec_t *regbase, uint phyid, uint offset)
153 /* Put the address of the phy, and the register number into
156 regbase->miimadd = (phyid << 8) | offset;
158 /* Clear the command register, and wait */
159 regbase->miimcom = 0;
162 /* Initiate a read command, and wait */
163 regbase->miimcom = MIIM_READ_COMMAND;
166 /* Wait for the the indication that the read is done */
167 while((regbase->miimind & (MIIMIND_NOTVALID | MIIMIND_BUSY)));
169 /* Grab the value read from the PHY */
170 value = regbase->miimstat;
176 static void init_phy(tsec_t *regs)
179 unsigned int timeout = TSEC_TIMEOUT;
181 /* Assign a Physical address to the TBI */
182 regs->tbipa=TBIPA_VALUE;
184 /* reset the management interface */
185 regs->miimcfg=MIIMCFG_RESET;
187 regs->miimcfg=MIIMCFG_INIT_VALUE;
189 /* Wait until the bus is free */
190 while(regs->miimind & MIIMIND_BUSY);
192 #ifdef CONFIG_PHY_CIS8201
193 /* override PHY config settings */
194 write_phy_reg(regs, 0, MIIM_AUX_CONSTAT, MIIM_AUXCONSTAT_INIT);
196 /* Set up interface mode */
197 write_phy_reg(regs, 0, MIIM_EXT_CON1, MIIM_EXTCON1_INIT);
200 /* Set the PHY to gigabit, full duplex, Auto-negotiate */
201 write_phy_reg(regs, 0, MIIM_CONTROL, MIIM_CONTROL_INIT);
203 /* Wait until TBI_STATUS indicates AN is done */
204 DBGPRINT("Waiting for Auto-negotiation to complete\n");
205 testval=read_phy_reg(regs, 0, MIIM_TBI_STATUS);
207 while((!(testval & MIIM_TBI_STATUS_AN_DONE))&& timeout--) {
208 testval=read_phy_reg(regs, 0, MIIM_TBI_STATUS);
211 if(testval & MIIM_TBI_STATUS_AN_DONE)
212 DBGPRINT("Auto-negotiation done\n");
214 DBGPRINT("Auto-negotiation timed-out.\n");
216 #ifdef CONFIG_PHY_CIS8201
217 /* Find out what duplexity (duplicity?) we have */
218 /* Read it twice to make sure */
219 testval=read_phy_reg(regs, 0, MIIM_AUX_CONSTAT);
221 if(testval & MIIM_AUXCONSTAT_DUPLEX) {
222 DBGPRINT("Enet starting in full duplex\n");
223 regs->maccfg2 |= MACCFG2_FULL_DUPLEX;
225 DBGPRINT("Enet starting in half duplex\n");
226 regs->maccfg2 &= ~MACCFG2_FULL_DUPLEX;
229 /* Also, we look to see what speed we are at
230 * if Gigabit, MACCFG2 goes in GMII, otherwise,
233 if((testval & MIIM_AUXCONSTAT_SPEED) != MIIM_AUXCONSTAT_GBIT) {
234 if((testval & MIIM_AUXCONSTAT_SPEED) == MIIM_AUXCONSTAT_100)
235 DBGPRINT("Enet starting in 100BT\n");
237 DBGPRINT("Enet starting in 10BT\n");
239 /* mark the mode in MACCFG2 */
240 regs->maccfg2 = ((regs->maccfg2&~(MACCFG2_IF)) | MACCFG2_MII);
242 DBGPRINT("Enet starting in 1000BT\n");
247 #ifdef CONFIG_PHY_M88E1011
248 /* Read the PHY to see what speed and duplex we are */
249 testval=read_phy_reg(regs, 0, MIIM_PHY_STATUS);
251 timeout = TSEC_TIMEOUT;
252 while((!(testval & MIIM_PHYSTAT_SPDDONE)) && timeout--) {
253 testval = read_phy_reg(regs,0,MIIM_PHY_STATUS);
256 if(!(testval & MIIM_PHYSTAT_SPDDONE))
257 DBGPRINT("Enet: Speed not resolved\n");
259 testval=read_phy_reg(regs, 0, MIIM_PHY_STATUS);
260 if(testval & MIIM_PHYSTAT_DUPLEX) {
261 DBGPRINT("Enet starting in Full Duplex\n");
262 regs->maccfg2 |= MACCFG2_FULL_DUPLEX;
264 DBGPRINT("Enet starting in Half Duplex\n");
265 regs->maccfg2 &= ~MACCFG2_FULL_DUPLEX;
268 if(!((testval&MIIM_PHYSTAT_SPEED) == MIIM_PHYSTAT_GBIT)) {
269 if((testval & MIIM_PHYSTAT_SPEED) == MIIM_PHYSTAT_100)
270 DBGPRINT("Enet starting in 100BT\n");
272 DBGPRINT("Enet starting in 10BT\n");
274 regs->maccfg2 = ((regs->maccfg2&~(MACCFG2_IF)) | MACCFG2_MII);
276 DBGPRINT("Enet starting in 1000BT\n");
283 static void init_registers(tsec_t *regs)
286 regs->ievent = IEVENT_INIT_CLEAR;
288 regs->imask = IMASK_INIT_CLEAR;
290 regs->hash.iaddr0 = 0;
291 regs->hash.iaddr1 = 0;
292 regs->hash.iaddr2 = 0;
293 regs->hash.iaddr3 = 0;
294 regs->hash.iaddr4 = 0;
295 regs->hash.iaddr5 = 0;
296 regs->hash.iaddr6 = 0;
297 regs->hash.iaddr7 = 0;
299 regs->hash.gaddr0 = 0;
300 regs->hash.gaddr1 = 0;
301 regs->hash.gaddr2 = 0;
302 regs->hash.gaddr3 = 0;
303 regs->hash.gaddr4 = 0;
304 regs->hash.gaddr5 = 0;
305 regs->hash.gaddr6 = 0;
306 regs->hash.gaddr7 = 0;
308 regs->rctrl = 0x00000000;
310 /* Init RMON mib registers */
311 memset((void *)&(regs->rmon), 0, sizeof(rmon_mib_t));
313 regs->rmon.cam1 = 0xffffffff;
314 regs->rmon.cam2 = 0xffffffff;
316 regs->mrblr = MRBLR_INIT_SETTINGS;
318 regs->minflr = MINFLR_INIT_SETTINGS;
320 regs->attr = ATTR_INIT_SETTINGS;
321 regs->attreli = ATTRELI_INIT_SETTINGS;
325 static void startup_tsec(tsec_t *regs)
329 /* Point to the buffer descriptors */
330 regs->tbase = (unsigned int)(&rtx.txbd[txIdx]);
331 regs->rbase = (unsigned int)(&rtx.rxbd[rxIdx]);
333 /* Initialize the Rx Buffer descriptors */
334 for (i = 0; i < PKTBUFSRX; i++) {
335 rtx.rxbd[i].status = RXBD_EMPTY;
336 rtx.rxbd[i].length = 0;
337 rtx.rxbd[i].bufPtr = (uint)NetRxPackets[i];
339 rtx.rxbd[PKTBUFSRX -1].status |= RXBD_WRAP;
341 /* Initialize the TX Buffer Descriptors */
342 for(i=0; i<TX_BUF_CNT; i++) {
343 rtx.txbd[i].status = 0;
344 rtx.txbd[i].length = 0;
345 rtx.txbd[i].bufPtr = 0;
347 rtx.txbd[TX_BUF_CNT -1].status |= TXBD_WRAP;
349 /* Enable Transmit and Receive */
350 regs->maccfg1 |= (MACCFG1_RX_EN | MACCFG1_TX_EN);
352 /* Tell the DMA it is clear to go */
353 regs->dmactrl |= DMACTRL_INIT_SETTINGS;
354 regs->tstat = TSTAT_CLEAR_THALT;
355 regs->dmactrl &= ~(DMACTRL_GRS | DMACTRL_GTS);
358 /* This returns the status bits of the device. The return value
359 * is never checked, and this is what the 8260 driver did, so we
360 * do the same. Presumably, this would be zero if there were no
362 static int tsec_send(struct eth_device* dev, volatile void *packet, int length)
366 tsec_t * regs = (tsec_t *)(TSEC_BASE_ADDR);
368 /* Find an empty buffer descriptor */
369 for(i=0; rtx.txbd[txIdx].status & TXBD_READY; i++) {
370 if (i >= TOUT_LOOP) {
371 DBGPRINT("tsec: tx buffers full\n");
376 rtx.txbd[txIdx].bufPtr = (uint)packet;
377 rtx.txbd[txIdx].length = length;
378 rtx.txbd[txIdx].status |= (TXBD_READY | TXBD_LAST | TXBD_CRC | TXBD_INTERRUPT);
380 /* Tell the DMA to go */
381 regs->tstat = TSTAT_CLEAR_THALT;
383 /* Wait for buffer to be transmitted */
384 for(i=0; rtx.txbd[txIdx].status & TXBD_READY; i++) {
385 if (i >= TOUT_LOOP) {
386 DBGPRINT("tsec: tx error\n");
391 txIdx = (txIdx + 1) % TX_BUF_CNT;
392 result = rtx.txbd[txIdx].status & TXBD_STATS;
397 static int tsec_recv(struct eth_device* dev)
400 tsec_t *regs = (tsec_t *)(TSEC_BASE_ADDR);
402 while(!(rtx.rxbd[rxIdx].status & RXBD_EMPTY)) {
404 length = rtx.rxbd[rxIdx].length;
406 /* Send the packet up if there were no errors */
407 if (!(rtx.rxbd[rxIdx].status & RXBD_STATS)) {
408 NetReceive(NetRxPackets[rxIdx], length - 4);
411 rtx.rxbd[rxIdx].length = 0;
413 /* Set the wrap bit if this is the last element in the list */
414 rtx.rxbd[rxIdx].status = RXBD_EMPTY | (((rxIdx + 1) == PKTBUFSRX) ? RXBD_WRAP : 0);
416 rxIdx = (rxIdx + 1) % PKTBUFSRX;
419 if(regs->ievent&IEVENT_BSY) {
420 regs->ievent = IEVENT_BSY;
421 regs->rstat = RSTAT_CLEAR_RHALT;
429 static void tsec_halt(struct eth_device* dev)
431 tsec_t *regs = (tsec_t *)(TSEC_BASE_ADDR);
433 regs->dmactrl &= ~(DMACTRL_GRS | DMACTRL_GTS);
434 regs->dmactrl |= (DMACTRL_GRS | DMACTRL_GTS);
436 while(!(regs->ievent & (IEVENT_GRSC | IEVENT_GTSC)));
438 regs->maccfg1 &= ~(MACCFG1_TX_EN | MACCFG1_RX_EN);
441 #endif /* CONFIG_TSEC_ENET */