1 // SPDX-License-Identifier: GPL-2.0+
3 * Cirrus Logic EP93xx ethernet MAC / MII driver.
5 * Copyright (C) 2010, 2009
6 * Matthias Kaehlcke <matthias@kaehlcke.net>
8 * Copyright (C) 2004, 2005
9 * Cory T. Tusar, Videon Central, Inc., <ctusar@videon-central.com>
11 * Based on the original eth.[ch] Cirrus Logic EP93xx Rev D. Ethernet Driver,
14 * (C) Copyright 2002 2003
15 * Adam Bezanson, Network Audio Technologies, Inc.
16 * <bezanson@netaudiotech.com>
21 #include <asm/arch/ep93xx.h>
25 #include <linux/types.h>
26 #include "ep93xx_eth.h"
28 #define GET_PRIV(eth_dev) ((struct ep93xx_priv *)(eth_dev)->priv)
29 #define GET_REGS(eth_dev) (GET_PRIV(eth_dev)->regs)
31 /* ep93xx_miiphy ops forward declarations */
32 static int ep93xx_miiphy_read(struct mii_dev *bus, int addr, int devad,
34 static int ep93xx_miiphy_write(struct mii_dev *bus, int addr, int devad,
37 #if defined(EP93XX_MAC_DEBUG)
39 * Dump ep93xx_mac values to the terminal.
41 static void dump_dev(struct eth_device *dev)
43 struct ep93xx_priv *priv = GET_PRIV(dev);
46 printf("\ndump_dev()\n");
47 printf(" rx_dq.base %p\n", priv->rx_dq.base);
48 printf(" rx_dq.current %p\n", priv->rx_dq.current);
49 printf(" rx_dq.end %p\n", priv->rx_dq.end);
50 printf(" rx_sq.base %p\n", priv->rx_sq.base);
51 printf(" rx_sq.current %p\n", priv->rx_sq.current);
52 printf(" rx_sq.end %p\n", priv->rx_sq.end);
54 for (i = 0; i < NUMRXDESC; i++)
55 printf(" rx_buffer[%2.d] %p\n", i, net_rx_packets[i]);
57 printf(" tx_dq.base %p\n", priv->tx_dq.base);
58 printf(" tx_dq.current %p\n", priv->tx_dq.current);
59 printf(" tx_dq.end %p\n", priv->tx_dq.end);
60 printf(" tx_sq.base %p\n", priv->tx_sq.base);
61 printf(" tx_sq.current %p\n", priv->tx_sq.current);
62 printf(" tx_sq.end %p\n", priv->tx_sq.end);
66 * Dump all RX status queue entries to the terminal.
68 static void dump_rx_status_queue(struct eth_device *dev)
70 struct ep93xx_priv *priv = GET_PRIV(dev);
73 printf("\ndump_rx_status_queue()\n");
74 printf(" descriptor address word1 word2\n");
75 for (i = 0; i < NUMRXDESC; i++) {
76 printf(" [ %p ] %08X %08X\n",
78 (priv->rx_sq.base + i)->word1,
79 (priv->rx_sq.base + i)->word2);
84 * Dump all RX descriptor queue entries to the terminal.
86 static void dump_rx_descriptor_queue(struct eth_device *dev)
88 struct ep93xx_priv *priv = GET_PRIV(dev);
91 printf("\ndump_rx_descriptor_queue()\n");
92 printf(" descriptor address word1 word2\n");
93 for (i = 0; i < NUMRXDESC; i++) {
94 printf(" [ %p ] %08X %08X\n",
96 (priv->rx_dq.base + i)->word1,
97 (priv->rx_dq.base + i)->word2);
102 * Dump all TX descriptor queue entries to the terminal.
104 static void dump_tx_descriptor_queue(struct eth_device *dev)
106 struct ep93xx_priv *priv = GET_PRIV(dev);
109 printf("\ndump_tx_descriptor_queue()\n");
110 printf(" descriptor address word1 word2\n");
111 for (i = 0; i < NUMTXDESC; i++) {
112 printf(" [ %p ] %08X %08X\n",
113 priv->tx_dq.base + i,
114 (priv->tx_dq.base + i)->word1,
115 (priv->tx_dq.base + i)->word2);
120 * Dump all TX status queue entries to the terminal.
122 static void dump_tx_status_queue(struct eth_device *dev)
124 struct ep93xx_priv *priv = GET_PRIV(dev);
127 printf("\ndump_tx_status_queue()\n");
128 printf(" descriptor address word1\n");
129 for (i = 0; i < NUMTXDESC; i++) {
130 printf(" [ %p ] %08X\n",
131 priv->rx_sq.base + i,
132 (priv->rx_sq.base + i)->word1);
137 #define dump_rx_descriptor_queue(x)
138 #define dump_rx_status_queue(x)
139 #define dump_tx_descriptor_queue(x)
140 #define dump_tx_status_queue(x)
141 #endif /* defined(EP93XX_MAC_DEBUG) */
144 * Reset the EP93xx MAC by twiddling the soft reset bit and spinning until
147 static void ep93xx_mac_reset(struct eth_device *dev)
149 struct mac_regs *mac = GET_REGS(dev);
152 debug("+ep93xx_mac_reset");
154 value = readl(&mac->selfctl);
155 value |= SELFCTL_RESET;
156 writel(value, &mac->selfctl);
158 while (readl(&mac->selfctl) & SELFCTL_RESET)
161 debug("-ep93xx_mac_reset");
164 /* Eth device open */
165 static int ep93xx_eth_open(struct eth_device *dev, bd_t *bd)
167 struct ep93xx_priv *priv = GET_PRIV(dev);
168 struct mac_regs *mac = GET_REGS(dev);
169 uchar *mac_addr = dev->enetaddr;
172 debug("+ep93xx_eth_open");
175 ep93xx_mac_reset(dev);
177 /* Reset the descriptor queues' current and end address values */
178 priv->tx_dq.current = priv->tx_dq.base;
179 priv->tx_dq.end = (priv->tx_dq.base + NUMTXDESC);
181 priv->tx_sq.current = priv->tx_sq.base;
182 priv->tx_sq.end = (priv->tx_sq.base + NUMTXDESC);
184 priv->rx_dq.current = priv->rx_dq.base;
185 priv->rx_dq.end = (priv->rx_dq.base + NUMRXDESC);
187 priv->rx_sq.current = priv->rx_sq.base;
188 priv->rx_sq.end = (priv->rx_sq.base + NUMRXDESC);
191 * Set the transmit descriptor and status queues' base address,
192 * current address, and length registers. Set the maximum frame
193 * length and threshold. Enable the transmit descriptor processor.
195 writel((uint32_t)priv->tx_dq.base, &mac->txdq.badd);
196 writel((uint32_t)priv->tx_dq.base, &mac->txdq.curadd);
197 writel(sizeof(struct tx_descriptor) * NUMTXDESC, &mac->txdq.blen);
199 writel((uint32_t)priv->tx_sq.base, &mac->txstsq.badd);
200 writel((uint32_t)priv->tx_sq.base, &mac->txstsq.curadd);
201 writel(sizeof(struct tx_status) * NUMTXDESC, &mac->txstsq.blen);
203 writel(0x00040000, &mac->txdthrshld);
204 writel(0x00040000, &mac->txststhrshld);
206 writel((TXSTARTMAX << 0) | (PKTSIZE_ALIGN << 16), &mac->maxfrmlen);
207 writel(BMCTL_TXEN, &mac->bmctl);
210 * Set the receive descriptor and status queues' base address,
211 * current address, and length registers. Enable the receive
212 * descriptor processor.
214 writel((uint32_t)priv->rx_dq.base, &mac->rxdq.badd);
215 writel((uint32_t)priv->rx_dq.base, &mac->rxdq.curadd);
216 writel(sizeof(struct rx_descriptor) * NUMRXDESC, &mac->rxdq.blen);
218 writel((uint32_t)priv->rx_sq.base, &mac->rxstsq.badd);
219 writel((uint32_t)priv->rx_sq.base, &mac->rxstsq.curadd);
220 writel(sizeof(struct rx_status) * NUMRXDESC, &mac->rxstsq.blen);
222 writel(0x00040000, &mac->rxdthrshld);
224 writel(BMCTL_RXEN, &mac->bmctl);
226 writel(0x00040000, &mac->rxststhrshld);
228 /* Wait until the receive descriptor processor is active */
229 while (!(readl(&mac->bmsts) & BMSTS_RXACT))
233 * Initialize the RX descriptor queue. Clear the TX descriptor queue.
234 * Clear the RX and TX status queues. Enqueue the RX descriptor and
235 * status entries to the MAC.
237 for (i = 0; i < NUMRXDESC; i++) {
238 /* set buffer address */
239 (priv->rx_dq.base + i)->word1 = (uint32_t)net_rx_packets[i];
241 /* set buffer length, clear buffer index and NSOF */
242 (priv->rx_dq.base + i)->word2 = PKTSIZE_ALIGN;
245 memset(priv->tx_dq.base, 0,
246 (sizeof(struct tx_descriptor) * NUMTXDESC));
247 memset(priv->rx_sq.base, 0,
248 (sizeof(struct rx_status) * NUMRXDESC));
249 memset(priv->tx_sq.base, 0,
250 (sizeof(struct tx_status) * NUMTXDESC));
252 writel(NUMRXDESC, &mac->rxdqenq);
253 writel(NUMRXDESC, &mac->rxstsqenq);
255 /* Set the primary MAC address */
256 writel(AFP_IAPRIMARY, &mac->afp);
257 writel(mac_addr[0] | (mac_addr[1] << 8) |
258 (mac_addr[2] << 16) | (mac_addr[3] << 24),
260 writel(mac_addr[4] | (mac_addr[5] << 8), &mac->indad_upper);
262 /* Turn on RX and TX */
263 writel(RXCTL_IA0 | RXCTL_BA | RXCTL_SRXON |
264 RXCTL_RCRCA | RXCTL_MA, &mac->rxctl);
265 writel(TXCTL_STXON, &mac->txctl);
267 /* Dump data structures if we're debugging */
269 dump_rx_descriptor_queue(dev);
270 dump_rx_status_queue(dev);
271 dump_tx_descriptor_queue(dev);
272 dump_tx_status_queue(dev);
274 debug("-ep93xx_eth_open");
280 * Halt EP93xx MAC transmit and receive by clearing the TxCTL and RxCTL
283 static void ep93xx_eth_close(struct eth_device *dev)
285 struct mac_regs *mac = GET_REGS(dev);
287 debug("+ep93xx_eth_close");
289 writel(0x00000000, &mac->rxctl);
290 writel(0x00000000, &mac->txctl);
292 debug("-ep93xx_eth_close");
296 * Copy a frame of data from the MAC into the protocol layer for further
299 static int ep93xx_eth_rcv_packet(struct eth_device *dev)
301 struct mac_regs *mac = GET_REGS(dev);
302 struct ep93xx_priv *priv = GET_PRIV(dev);
305 debug("+ep93xx_eth_rcv_packet");
307 if (RX_STATUS_RFP(priv->rx_sq.current)) {
308 if (RX_STATUS_RWE(priv->rx_sq.current)) {
310 * We have a good frame. Extract the frame's length
311 * from the current rx_status_queue entry, and copy
312 * the frame's data into net_rx_packets[] of the
313 * protocol stack. We track the total number of
314 * bytes in the frame (nbytes_frame) which will be
315 * used when we pass the data off to the protocol
316 * layer via net_process_received_packet().
318 len = RX_STATUS_FRAME_LEN(priv->rx_sq.current);
320 net_process_received_packet(
321 (uchar *)priv->rx_dq.current->word1, len);
323 debug("reporting %d bytes...\n", len);
325 /* Do we have an erroneous packet? */
326 pr_err("packet rx error, status %08X %08X",
327 priv->rx_sq.current->word1,
328 priv->rx_sq.current->word2);
329 dump_rx_descriptor_queue(dev);
330 dump_rx_status_queue(dev);
334 * Clear the associated status queue entry, and
335 * increment our current pointers to the next RX
336 * descriptor and status queue entries (making sure
339 memset((void *)priv->rx_sq.current, 0,
340 sizeof(struct rx_status));
342 priv->rx_sq.current++;
343 if (priv->rx_sq.current >= priv->rx_sq.end)
344 priv->rx_sq.current = priv->rx_sq.base;
346 priv->rx_dq.current++;
347 if (priv->rx_dq.current >= priv->rx_dq.end)
348 priv->rx_dq.current = priv->rx_dq.base;
351 * Finally, return the RX descriptor and status entries
352 * back to the MAC engine, and loop again, checking for
353 * more descriptors to process.
355 writel(1, &mac->rxdqenq);
356 writel(1, &mac->rxstsqenq);
361 debug("-ep93xx_eth_rcv_packet %d", len);
366 * Send a block of data via ethernet.
368 static int ep93xx_eth_send_packet(struct eth_device *dev,
369 void * const packet, int const length)
371 struct mac_regs *mac = GET_REGS(dev);
372 struct ep93xx_priv *priv = GET_PRIV(dev);
375 debug("+ep93xx_eth_send_packet");
377 /* Parameter check */
378 BUG_ON(packet == NULL);
381 * Initialize the TX descriptor queue with the new packet's info.
382 * Clear the associated status queue entry. Enqueue the packet
383 * to the MAC for transmission.
386 /* set buffer address */
387 priv->tx_dq.current->word1 = (uint32_t)packet;
389 /* set buffer length and EOF bit */
390 priv->tx_dq.current->word2 = length | TX_DESC_EOF;
392 /* clear tx status */
393 priv->tx_sq.current->word1 = 0;
395 /* enqueue the TX descriptor */
396 writel(1, &mac->txdqenq);
398 /* wait for the frame to become processed */
399 while (!TX_STATUS_TXFP(priv->tx_sq.current))
402 if (!TX_STATUS_TXWE(priv->tx_sq.current)) {
403 pr_err("packet tx error, status %08X",
404 priv->tx_sq.current->word1);
405 dump_tx_descriptor_queue(dev);
406 dump_tx_status_queue(dev);
408 /* TODO: Add better error handling? */
416 debug("-ep93xx_eth_send_packet %d", ret);
420 #if defined(CONFIG_MII)
421 int ep93xx_miiphy_initialize(bd_t * const bd)
424 struct mii_dev *mdiodev = mdio_alloc();
427 strncpy(mdiodev->name, "ep93xx_eth0", MDIO_NAME_LEN);
428 mdiodev->read = ep93xx_miiphy_read;
429 mdiodev->write = ep93xx_miiphy_write;
431 retval = mdio_register(mdiodev);
439 * Initialize the EP93xx MAC. The MAC hardware is reset. Buffers are
440 * allocated, if necessary, for the TX and RX descriptor and status queues,
441 * as well as for received packets. The EP93XX MAC hardware is initialized.
442 * Transmit and receive operations are enabled.
444 int ep93xx_eth_initialize(u8 dev_num, int base_addr)
447 struct eth_device *dev;
448 struct ep93xx_priv *priv;
450 debug("+ep93xx_eth_initialize");
452 priv = malloc(sizeof(*priv));
454 pr_err("malloc() failed");
455 goto eth_init_failed_0;
457 memset(priv, 0, sizeof(*priv));
459 priv->regs = (struct mac_regs *)base_addr;
461 priv->tx_dq.base = calloc(NUMTXDESC,
462 sizeof(struct tx_descriptor));
463 if (priv->tx_dq.base == NULL) {
464 pr_err("calloc() failed");
465 goto eth_init_failed_1;
468 priv->tx_sq.base = calloc(NUMTXDESC,
469 sizeof(struct tx_status));
470 if (priv->tx_sq.base == NULL) {
471 pr_err("calloc() failed");
472 goto eth_init_failed_2;
475 priv->rx_dq.base = calloc(NUMRXDESC,
476 sizeof(struct rx_descriptor));
477 if (priv->rx_dq.base == NULL) {
478 pr_err("calloc() failed");
479 goto eth_init_failed_3;
482 priv->rx_sq.base = calloc(NUMRXDESC,
483 sizeof(struct rx_status));
484 if (priv->rx_sq.base == NULL) {
485 pr_err("calloc() failed");
486 goto eth_init_failed_4;
489 dev = malloc(sizeof *dev);
491 pr_err("malloc() failed");
492 goto eth_init_failed_5;
494 memset(dev, 0, sizeof *dev);
496 dev->iobase = base_addr;
498 dev->init = ep93xx_eth_open;
499 dev->halt = ep93xx_eth_close;
500 dev->send = ep93xx_eth_send_packet;
501 dev->recv = ep93xx_eth_rcv_packet;
503 sprintf(dev->name, "ep93xx_eth-%hu", dev_num);
512 free(priv->rx_sq.base);
516 free(priv->rx_dq.base);
520 free(priv->tx_sq.base);
524 free(priv->tx_dq.base);
535 debug("-ep93xx_eth_initialize %d", ret);
539 #if defined(CONFIG_MII)
542 * Maximum MII address we support
544 #define MII_ADDRESS_MAX 31
547 * Maximum MII register address we support
549 #define MII_REGISTER_MAX 31
552 * Read a 16-bit value from an MII register.
554 static int ep93xx_miiphy_read(struct mii_dev *bus, int addr, int devad,
557 unsigned short value = 0;
558 struct mac_regs *mac = (struct mac_regs *)MAC_BASE;
562 debug("+ep93xx_miiphy_read");
564 /* Parameter checks */
565 BUG_ON(bus->name == NULL);
566 BUG_ON(addr > MII_ADDRESS_MAX);
567 BUG_ON(reg > MII_REGISTER_MAX);
570 * Save the current SelfCTL register value. Set MAC to suppress
571 * preamble bits. Wait for any previous MII command to complete
572 * before issuing the new command.
574 self_ctl = readl(&mac->selfctl);
575 #if defined(CONFIG_MII_SUPPRESS_PREAMBLE)
576 writel(self_ctl & ~(1 << 8), &mac->selfctl);
577 #endif /* defined(CONFIG_MII_SUPPRESS_PREAMBLE) */
579 while (readl(&mac->miists) & MIISTS_BUSY)
583 * Issue the MII 'read' command. Wait for the command to complete.
584 * Read the MII data value.
586 writel(MIICMD_OPCODE_READ | ((uint32_t)addr << 5) | (uint32_t)reg,
588 while (readl(&mac->miists) & MIISTS_BUSY)
591 value = (unsigned short)readl(&mac->miidata);
593 /* Restore the saved SelfCTL value and return. */
594 writel(self_ctl, &mac->selfctl);
599 debug("-ep93xx_miiphy_read");
606 * Write a 16-bit value to an MII register.
608 static int ep93xx_miiphy_write(struct mii_dev *bus, int addr, int devad,
611 struct mac_regs *mac = (struct mac_regs *)MAC_BASE;
615 debug("+ep93xx_miiphy_write");
617 /* Parameter checks */
618 BUG_ON(bus->name == NULL);
619 BUG_ON(addr > MII_ADDRESS_MAX);
620 BUG_ON(reg > MII_REGISTER_MAX);
623 * Save the current SelfCTL register value. Set MAC to suppress
624 * preamble bits. Wait for any previous MII command to complete
625 * before issuing the new command.
627 self_ctl = readl(&mac->selfctl);
628 #if defined(CONFIG_MII_SUPPRESS_PREAMBLE)
629 writel(self_ctl & ~(1 << 8), &mac->selfctl);
630 #endif /* defined(CONFIG_MII_SUPPRESS_PREAMBLE) */
632 while (readl(&mac->miists) & MIISTS_BUSY)
635 /* Issue the MII 'write' command. Wait for the command to complete. */
636 writel((uint32_t)value, &mac->miidata);
637 writel(MIICMD_OPCODE_WRITE | ((uint32_t)addr << 5) | (uint32_t)reg,
639 while (readl(&mac->miists) & MIISTS_BUSY)
642 /* Restore the saved SelfCTL value and return. */
643 writel(self_ctl, &mac->selfctl);
648 debug("-ep93xx_miiphy_write");
651 #endif /* defined(CONFIG_MII) */