2 * Cirrus Logic EP93xx ethernet MAC / MII driver.
4 * Copyright (C) 2010, 2009
5 * Matthias Kaehlcke <matthias@kaehlcke.net>
7 * Copyright (C) 2004, 2005
8 * Cory T. Tusar, Videon Central, Inc., <ctusar@videon-central.com>
10 * Based on the original eth.[ch] Cirrus Logic EP93xx Rev D. Ethernet Driver,
13 * (C) Copyright 2002 2003
14 * Adam Bezanson, Network Audio Technologies, Inc.
15 * <bezanson@netaudiotech.com>
17 * See file CREDITS for list of people who contributed to this project.
19 * This program is free software; you can redistribute it and/or modify
20 * it under the terms of the GNU General Public License as published by
21 * the Free Software Foundation; either version 2 of the License, or
22 * (at your option) any later version.
24 * This program is distributed in the hope that it will be useful, but
25 * WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
26 * or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
29 * You should have received a copy of the GNU General Public License along
30 * with this program; if not, write to the Free Software Foundation, Inc.,
31 * 675 Mass Ave, Cambridge, MA 02139, USA.
36 #include <asm/arch/ep93xx.h>
40 #include <linux/types.h>
41 #include "ep93xx_eth.h"
43 #define GET_PRIV(eth_dev) ((struct ep93xx_priv *)(eth_dev)->priv)
44 #define GET_REGS(eth_dev) (GET_PRIV(eth_dev)->regs)
46 /* ep93xx_miiphy ops forward declarations */
47 static int ep93xx_miiphy_read(char * const dev, unsigned char const addr,
48 unsigned char const reg, unsigned short * const value);
49 static int ep93xx_miiphy_write(char * const dev, unsigned char const addr,
50 unsigned char const reg, unsigned short const value);
52 #if defined(EP93XX_MAC_DEBUG)
54 * Dump ep93xx_mac values to the terminal.
56 static void dump_dev(struct eth_device *dev)
58 struct ep93xx_priv *priv = GET_PRIV(dev);
61 printf("\ndump_dev()\n");
62 printf(" rx_dq.base %p\n", priv->rx_dq.base);
63 printf(" rx_dq.current %p\n", priv->rx_dq.current);
64 printf(" rx_dq.end %p\n", priv->rx_dq.end);
65 printf(" rx_sq.base %p\n", priv->rx_sq.base);
66 printf(" rx_sq.current %p\n", priv->rx_sq.current);
67 printf(" rx_sq.end %p\n", priv->rx_sq.end);
69 for (i = 0; i < NUMRXDESC; i++)
70 printf(" rx_buffer[%2.d] %p\n", i, NetRxPackets[i]);
72 printf(" tx_dq.base %p\n", priv->tx_dq.base);
73 printf(" tx_dq.current %p\n", priv->tx_dq.current);
74 printf(" tx_dq.end %p\n", priv->tx_dq.end);
75 printf(" tx_sq.base %p\n", priv->tx_sq.base);
76 printf(" tx_sq.current %p\n", priv->tx_sq.current);
77 printf(" tx_sq.end %p\n", priv->tx_sq.end);
81 * Dump all RX status queue entries to the terminal.
83 static void dump_rx_status_queue(struct eth_device *dev)
85 struct ep93xx_priv *priv = GET_PRIV(dev);
88 printf("\ndump_rx_status_queue()\n");
89 printf(" descriptor address word1 word2\n");
90 for (i = 0; i < NUMRXDESC; i++) {
91 printf(" [ %p ] %08X %08X\n",
93 (priv->rx_sq.base + i)->word1,
94 (priv->rx_sq.base + i)->word2);
99 * Dump all RX descriptor queue entries to the terminal.
101 static void dump_rx_descriptor_queue(struct eth_device *dev)
103 struct ep93xx_priv *priv = GET_PRIV(dev);
106 printf("\ndump_rx_descriptor_queue()\n");
107 printf(" descriptor address word1 word2\n");
108 for (i = 0; i < NUMRXDESC; i++) {
109 printf(" [ %p ] %08X %08X\n",
110 priv->rx_dq.base + i,
111 (priv->rx_dq.base + i)->word1,
112 (priv->rx_dq.base + i)->word2);
117 * Dump all TX descriptor queue entries to the terminal.
119 static void dump_tx_descriptor_queue(struct eth_device *dev)
121 struct ep93xx_priv *priv = GET_PRIV(dev);
124 printf("\ndump_tx_descriptor_queue()\n");
125 printf(" descriptor address word1 word2\n");
126 for (i = 0; i < NUMTXDESC; i++) {
127 printf(" [ %p ] %08X %08X\n",
128 priv->tx_dq.base + i,
129 (priv->tx_dq.base + i)->word1,
130 (priv->tx_dq.base + i)->word2);
135 * Dump all TX status queue entries to the terminal.
137 static void dump_tx_status_queue(struct eth_device *dev)
139 struct ep93xx_priv *priv = GET_PRIV(dev);
142 printf("\ndump_tx_status_queue()\n");
143 printf(" descriptor address word1\n");
144 for (i = 0; i < NUMTXDESC; i++) {
145 printf(" [ %p ] %08X\n",
146 priv->rx_sq.base + i,
147 (priv->rx_sq.base + i)->word1);
152 #define dump_rx_descriptor_queue(x)
153 #define dump_rx_status_queue(x)
154 #define dump_tx_descriptor_queue(x)
155 #define dump_tx_status_queue(x)
156 #endif /* defined(EP93XX_MAC_DEBUG) */
159 * Reset the EP93xx MAC by twiddling the soft reset bit and spinning until
162 static void ep93xx_mac_reset(struct eth_device *dev)
164 struct mac_regs *mac = GET_REGS(dev);
167 debug("+ep93xx_mac_reset");
169 value = readl(&mac->selfctl);
170 value |= SELFCTL_RESET;
171 writel(value, &mac->selfctl);
173 while (readl(&mac->selfctl) & SELFCTL_RESET)
176 debug("-ep93xx_mac_reset");
179 /* Eth device open */
180 static int ep93xx_eth_open(struct eth_device *dev, bd_t *bd)
182 struct ep93xx_priv *priv = GET_PRIV(dev);
183 struct mac_regs *mac = GET_REGS(dev);
184 uchar *mac_addr = dev->enetaddr;
187 debug("+ep93xx_eth_open");
190 ep93xx_mac_reset(dev);
192 /* Reset the descriptor queues' current and end address values */
193 priv->tx_dq.current = priv->tx_dq.base;
194 priv->tx_dq.end = (priv->tx_dq.base + NUMTXDESC);
196 priv->tx_sq.current = priv->tx_sq.base;
197 priv->tx_sq.end = (priv->tx_sq.base + NUMTXDESC);
199 priv->rx_dq.current = priv->rx_dq.base;
200 priv->rx_dq.end = (priv->rx_dq.base + NUMRXDESC);
202 priv->rx_sq.current = priv->rx_sq.base;
203 priv->rx_sq.end = (priv->rx_sq.base + NUMRXDESC);
206 * Set the transmit descriptor and status queues' base address,
207 * current address, and length registers. Set the maximum frame
208 * length and threshold. Enable the transmit descriptor processor.
210 writel((uint32_t)priv->tx_dq.base, &mac->txdq.badd);
211 writel((uint32_t)priv->tx_dq.base, &mac->txdq.curadd);
212 writel(sizeof(struct tx_descriptor) * NUMTXDESC, &mac->txdq.blen);
214 writel((uint32_t)priv->tx_sq.base, &mac->txstsq.badd);
215 writel((uint32_t)priv->tx_sq.base, &mac->txstsq.curadd);
216 writel(sizeof(struct tx_status) * NUMTXDESC, &mac->txstsq.blen);
218 writel(0x00040000, &mac->txdthrshld);
219 writel(0x00040000, &mac->txststhrshld);
221 writel((TXSTARTMAX << 0) | (PKTSIZE_ALIGN << 16), &mac->maxfrmlen);
222 writel(BMCTL_TXEN, &mac->bmctl);
225 * Set the receive descriptor and status queues' base address,
226 * current address, and length registers. Enable the receive
227 * descriptor processor.
229 writel((uint32_t)priv->rx_dq.base, &mac->rxdq.badd);
230 writel((uint32_t)priv->rx_dq.base, &mac->rxdq.curadd);
231 writel(sizeof(struct rx_descriptor) * NUMRXDESC, &mac->rxdq.blen);
233 writel((uint32_t)priv->rx_sq.base, &mac->rxstsq.badd);
234 writel((uint32_t)priv->rx_sq.base, &mac->rxstsq.curadd);
235 writel(sizeof(struct rx_status) * NUMRXDESC, &mac->rxstsq.blen);
237 writel(0x00040000, &mac->rxdthrshld);
239 writel(BMCTL_RXEN, &mac->bmctl);
241 writel(0x00040000, &mac->rxststhrshld);
243 /* Wait until the receive descriptor processor is active */
244 while (!(readl(&mac->bmsts) & BMSTS_RXACT))
248 * Initialize the RX descriptor queue. Clear the TX descriptor queue.
249 * Clear the RX and TX status queues. Enqueue the RX descriptor and
250 * status entries to the MAC.
252 for (i = 0; i < NUMRXDESC; i++) {
253 /* set buffer address */
254 (priv->rx_dq.base + i)->word1 = (uint32_t)NetRxPackets[i];
256 /* set buffer length, clear buffer index and NSOF */
257 (priv->rx_dq.base + i)->word2 = PKTSIZE_ALIGN;
260 memset(priv->tx_dq.base, 0,
261 (sizeof(struct tx_descriptor) * NUMTXDESC));
262 memset(priv->rx_sq.base, 0,
263 (sizeof(struct rx_status) * NUMRXDESC));
264 memset(priv->tx_sq.base, 0,
265 (sizeof(struct tx_status) * NUMTXDESC));
267 writel(NUMRXDESC, &mac->rxdqenq);
268 writel(NUMRXDESC, &mac->rxstsqenq);
270 /* Set the primary MAC address */
271 writel(AFP_IAPRIMARY, &mac->afp);
272 writel(mac_addr[0] | (mac_addr[1] << 8) |
273 (mac_addr[2] << 16) | (mac_addr[3] << 24),
275 writel(mac_addr[4] | (mac_addr[5] << 8), &mac->indad_upper);
277 /* Turn on RX and TX */
278 writel(RXCTL_IA0 | RXCTL_BA | RXCTL_SRXON |
279 RXCTL_RCRCA | RXCTL_MA, &mac->rxctl);
280 writel(TXCTL_STXON, &mac->txctl);
282 /* Dump data structures if we're debugging */
284 dump_rx_descriptor_queue(dev);
285 dump_rx_status_queue(dev);
286 dump_tx_descriptor_queue(dev);
287 dump_tx_status_queue(dev);
289 debug("-ep93xx_eth_open");
295 * Halt EP93xx MAC transmit and receive by clearing the TxCTL and RxCTL
298 static void ep93xx_eth_close(struct eth_device *dev)
300 struct mac_regs *mac = GET_REGS(dev);
302 debug("+ep93xx_eth_close");
304 writel(0x00000000, &mac->rxctl);
305 writel(0x00000000, &mac->txctl);
307 debug("-ep93xx_eth_close");
311 * Copy a frame of data from the MAC into the protocol layer for further
314 static int ep93xx_eth_rcv_packet(struct eth_device *dev)
316 struct mac_regs *mac = GET_REGS(dev);
317 struct ep93xx_priv *priv = GET_PRIV(dev);
320 debug("+ep93xx_eth_rcv_packet");
322 if (RX_STATUS_RFP(priv->rx_sq.current)) {
323 if (RX_STATUS_RWE(priv->rx_sq.current)) {
325 * We have a good frame. Extract the frame's length
326 * from the current rx_status_queue entry, and copy
327 * the frame's data into NetRxPackets[] of the
328 * protocol stack. We track the total number of
329 * bytes in the frame (nbytes_frame) which will be
330 * used when we pass the data off to the protocol
331 * layer via NetReceive().
333 len = RX_STATUS_FRAME_LEN(priv->rx_sq.current);
335 NetReceive((uchar *)priv->rx_dq.current->word1, len);
337 debug("reporting %d bytes...\n", len);
339 /* Do we have an erroneous packet? */
340 error("packet rx error, status %08X %08X",
341 priv->rx_sq.current->word1,
342 priv->rx_sq.current->word2);
343 dump_rx_descriptor_queue(dev);
344 dump_rx_status_queue(dev);
348 * Clear the associated status queue entry, and
349 * increment our current pointers to the next RX
350 * descriptor and status queue entries (making sure
353 memset((void *)priv->rx_sq.current, 0,
354 sizeof(struct rx_status));
356 priv->rx_sq.current++;
357 if (priv->rx_sq.current >= priv->rx_sq.end)
358 priv->rx_sq.current = priv->rx_sq.base;
360 priv->rx_dq.current++;
361 if (priv->rx_dq.current >= priv->rx_dq.end)
362 priv->rx_dq.current = priv->rx_dq.base;
365 * Finally, return the RX descriptor and status entries
366 * back to the MAC engine, and loop again, checking for
367 * more descriptors to process.
369 writel(1, &mac->rxdqenq);
370 writel(1, &mac->rxstsqenq);
375 debug("-ep93xx_eth_rcv_packet %d", len);
380 * Send a block of data via ethernet.
382 static int ep93xx_eth_send_packet(struct eth_device *dev,
383 volatile void * const packet, int const length)
385 struct mac_regs *mac = GET_REGS(dev);
386 struct ep93xx_priv *priv = GET_PRIV(dev);
389 debug("+ep93xx_eth_send_packet");
391 /* Parameter check */
392 BUG_ON(packet == NULL);
395 * Initialize the TX descriptor queue with the new packet's info.
396 * Clear the associated status queue entry. Enqueue the packet
397 * to the MAC for transmission.
400 /* set buffer address */
401 priv->tx_dq.current->word1 = (uint32_t)packet;
403 /* set buffer length and EOF bit */
404 priv->tx_dq.current->word2 = length | TX_DESC_EOF;
406 /* clear tx status */
407 priv->tx_sq.current->word1 = 0;
409 /* enqueue the TX descriptor */
410 writel(1, &mac->txdqenq);
412 /* wait for the frame to become processed */
413 while (!TX_STATUS_TXFP(priv->tx_sq.current))
416 if (!TX_STATUS_TXWE(priv->tx_sq.current)) {
417 error("packet tx error, status %08X",
418 priv->tx_sq.current->word1);
419 dump_tx_descriptor_queue(dev);
420 dump_tx_status_queue(dev);
422 /* TODO: Add better error handling? */
430 debug("-ep93xx_eth_send_packet %d", ret);
434 #if defined(CONFIG_MII)
435 int ep93xx_miiphy_initialize(bd_t * const bd)
437 miiphy_register("ep93xx_eth0", ep93xx_miiphy_read, ep93xx_miiphy_write);
443 * Initialize the EP93xx MAC. The MAC hardware is reset. Buffers are
444 * allocated, if necessary, for the TX and RX descriptor and status queues,
445 * as well as for received packets. The EP93XX MAC hardware is initialized.
446 * Transmit and receive operations are enabled.
448 int ep93xx_eth_initialize(u8 dev_num, int base_addr)
451 struct eth_device *dev;
452 struct ep93xx_priv *priv;
454 debug("+ep93xx_eth_initialize");
456 priv = malloc(sizeof(*priv));
458 error("malloc() failed");
459 goto eth_init_failed_0;
461 memset(priv, 0, sizeof(*priv));
463 priv->regs = (struct mac_regs *)base_addr;
465 priv->tx_dq.base = calloc(NUMTXDESC,
466 sizeof(struct tx_descriptor));
467 if (priv->tx_dq.base == NULL) {
468 error("calloc() failed");
469 goto eth_init_failed_1;
472 priv->tx_sq.base = calloc(NUMTXDESC,
473 sizeof(struct tx_status));
474 if (priv->tx_sq.base == NULL) {
475 error("calloc() failed");
476 goto eth_init_failed_2;
479 priv->rx_dq.base = calloc(NUMRXDESC,
480 sizeof(struct rx_descriptor));
481 if (priv->rx_dq.base == NULL) {
482 error("calloc() failed");
483 goto eth_init_failed_3;
486 priv->rx_sq.base = calloc(NUMRXDESC,
487 sizeof(struct rx_status));
488 if (priv->rx_sq.base == NULL) {
489 error("calloc() failed");
490 goto eth_init_failed_4;
493 dev = malloc(sizeof *dev);
495 error("malloc() failed");
496 goto eth_init_failed_5;
498 memset(dev, 0, sizeof *dev);
500 dev->iobase = base_addr;
502 dev->init = ep93xx_eth_open;
503 dev->halt = ep93xx_eth_close;
504 dev->send = ep93xx_eth_send_packet;
505 dev->recv = ep93xx_eth_rcv_packet;
507 sprintf(dev->name, "ep93xx_eth-%hu", dev_num);
516 free(priv->rx_sq.base);
520 free(priv->rx_dq.base);
524 free(priv->tx_sq.base);
528 free(priv->tx_dq.base);
539 debug("-ep93xx_eth_initialize %d", ret);
543 #if defined(CONFIG_MII)
546 * Maximum MII address we support
548 #define MII_ADDRESS_MAX 31
551 * Maximum MII register address we support
553 #define MII_REGISTER_MAX 31
556 * Read a 16-bit value from an MII register.
558 static int ep93xx_miiphy_read(char * const dev, unsigned char const addr,
559 unsigned char const reg, unsigned short * const value)
561 struct mac_regs *mac = (struct mac_regs *)MAC_BASE;
565 debug("+ep93xx_miiphy_read");
567 /* Parameter checks */
569 BUG_ON(addr > MII_ADDRESS_MAX);
570 BUG_ON(reg > MII_REGISTER_MAX);
571 BUG_ON(value == NULL);
574 * Save the current SelfCTL register value. Set MAC to suppress
575 * preamble bits. Wait for any previous MII command to complete
576 * before issuing the new command.
578 self_ctl = readl(&mac->selfctl);
579 #if defined(CONFIG_MII_SUPPRESS_PREAMBLE)
580 writel(self_ctl & ~(1 << 8), &mac->selfctl);
581 #endif /* defined(CONFIG_MII_SUPPRESS_PREAMBLE) */
583 while (readl(&mac->miists) & MIISTS_BUSY)
587 * Issue the MII 'read' command. Wait for the command to complete.
588 * Read the MII data value.
590 writel(MIICMD_OPCODE_READ | ((uint32_t)addr << 5) | (uint32_t)reg,
592 while (readl(&mac->miists) & MIISTS_BUSY)
595 *value = (unsigned short)readl(&mac->miidata);
597 /* Restore the saved SelfCTL value and return. */
598 writel(self_ctl, &mac->selfctl);
603 debug("-ep93xx_miiphy_read");
608 * Write a 16-bit value to an MII register.
610 static int ep93xx_miiphy_write(char * const dev, unsigned char const addr,
611 unsigned char const reg, unsigned short const value)
613 struct mac_regs *mac = (struct mac_regs *)MAC_BASE;
617 debug("+ep93xx_miiphy_write");
619 /* Parameter checks */
621 BUG_ON(addr > MII_ADDRESS_MAX);
622 BUG_ON(reg > MII_REGISTER_MAX);
625 * Save the current SelfCTL register value. Set MAC to suppress
626 * preamble bits. Wait for any previous MII command to complete
627 * before issuing the new command.
629 self_ctl = readl(&mac->selfctl);
630 #if defined(CONFIG_MII_SUPPRESS_PREAMBLE)
631 writel(self_ctl & ~(1 << 8), &mac->selfctl);
632 #endif /* defined(CONFIG_MII_SUPPRESS_PREAMBLE) */
634 while (readl(&mac->miists) & MIISTS_BUSY)
637 /* Issue the MII 'write' command. Wait for the command to complete. */
638 writel((uint32_t)value, &mac->miidata);
639 writel(MIICMD_OPCODE_WRITE | ((uint32_t)addr << 5) | (uint32_t)reg,
641 while (readl(&mac->miists) & MIISTS_BUSY)
644 /* Restore the saved SelfCTL value and return. */
645 writel(self_ctl, &mac->selfctl);
650 debug("-ep93xx_miiphy_write");
653 #endif /* defined(CONFIG_MII) */