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>
23 #include <asm/arch/ep93xx.h>
27 #include <linux/bug.h>
28 #include <linux/types.h>
29 #include "ep93xx_eth.h"
31 #define GET_PRIV(eth_dev) ((struct ep93xx_priv *)(eth_dev)->priv)
32 #define GET_REGS(eth_dev) (GET_PRIV(eth_dev)->regs)
34 /* ep93xx_miiphy ops forward declarations */
35 static int ep93xx_miiphy_read(struct mii_dev *bus, int addr, int devad,
37 static int ep93xx_miiphy_write(struct mii_dev *bus, int addr, int devad,
40 #if defined(EP93XX_MAC_DEBUG)
42 * Dump ep93xx_mac values to the terminal.
44 static void dump_dev(struct eth_device *dev)
46 struct ep93xx_priv *priv = GET_PRIV(dev);
49 printf("\ndump_dev()\n");
50 printf(" rx_dq.base %p\n", priv->rx_dq.base);
51 printf(" rx_dq.current %p\n", priv->rx_dq.current);
52 printf(" rx_dq.end %p\n", priv->rx_dq.end);
53 printf(" rx_sq.base %p\n", priv->rx_sq.base);
54 printf(" rx_sq.current %p\n", priv->rx_sq.current);
55 printf(" rx_sq.end %p\n", priv->rx_sq.end);
57 for (i = 0; i < NUMRXDESC; i++)
58 printf(" rx_buffer[%2.d] %p\n", i, net_rx_packets[i]);
60 printf(" tx_dq.base %p\n", priv->tx_dq.base);
61 printf(" tx_dq.current %p\n", priv->tx_dq.current);
62 printf(" tx_dq.end %p\n", priv->tx_dq.end);
63 printf(" tx_sq.base %p\n", priv->tx_sq.base);
64 printf(" tx_sq.current %p\n", priv->tx_sq.current);
65 printf(" tx_sq.end %p\n", priv->tx_sq.end);
69 * Dump all RX status queue entries to the terminal.
71 static void dump_rx_status_queue(struct eth_device *dev)
73 struct ep93xx_priv *priv = GET_PRIV(dev);
76 printf("\ndump_rx_status_queue()\n");
77 printf(" descriptor address word1 word2\n");
78 for (i = 0; i < NUMRXDESC; i++) {
79 printf(" [ %p ] %08X %08X\n",
81 (priv->rx_sq.base + i)->word1,
82 (priv->rx_sq.base + i)->word2);
87 * Dump all RX descriptor queue entries to the terminal.
89 static void dump_rx_descriptor_queue(struct eth_device *dev)
91 struct ep93xx_priv *priv = GET_PRIV(dev);
94 printf("\ndump_rx_descriptor_queue()\n");
95 printf(" descriptor address word1 word2\n");
96 for (i = 0; i < NUMRXDESC; i++) {
97 printf(" [ %p ] %08X %08X\n",
99 (priv->rx_dq.base + i)->word1,
100 (priv->rx_dq.base + i)->word2);
105 * Dump all TX descriptor queue entries to the terminal.
107 static void dump_tx_descriptor_queue(struct eth_device *dev)
109 struct ep93xx_priv *priv = GET_PRIV(dev);
112 printf("\ndump_tx_descriptor_queue()\n");
113 printf(" descriptor address word1 word2\n");
114 for (i = 0; i < NUMTXDESC; i++) {
115 printf(" [ %p ] %08X %08X\n",
116 priv->tx_dq.base + i,
117 (priv->tx_dq.base + i)->word1,
118 (priv->tx_dq.base + i)->word2);
123 * Dump all TX status queue entries to the terminal.
125 static void dump_tx_status_queue(struct eth_device *dev)
127 struct ep93xx_priv *priv = GET_PRIV(dev);
130 printf("\ndump_tx_status_queue()\n");
131 printf(" descriptor address word1\n");
132 for (i = 0; i < NUMTXDESC; i++) {
133 printf(" [ %p ] %08X\n",
134 priv->rx_sq.base + i,
135 (priv->rx_sq.base + i)->word1);
140 #define dump_rx_descriptor_queue(x)
141 #define dump_rx_status_queue(x)
142 #define dump_tx_descriptor_queue(x)
143 #define dump_tx_status_queue(x)
144 #endif /* defined(EP93XX_MAC_DEBUG) */
147 * Reset the EP93xx MAC by twiddling the soft reset bit and spinning until
150 static void ep93xx_mac_reset(struct eth_device *dev)
152 struct mac_regs *mac = GET_REGS(dev);
155 debug("+ep93xx_mac_reset");
157 value = readl(&mac->selfctl);
158 value |= SELFCTL_RESET;
159 writel(value, &mac->selfctl);
161 while (readl(&mac->selfctl) & SELFCTL_RESET)
164 debug("-ep93xx_mac_reset");
167 /* Eth device open */
168 static int ep93xx_eth_open(struct eth_device *dev, bd_t *bd)
170 struct ep93xx_priv *priv = GET_PRIV(dev);
171 struct mac_regs *mac = GET_REGS(dev);
172 uchar *mac_addr = dev->enetaddr;
175 debug("+ep93xx_eth_open");
178 ep93xx_mac_reset(dev);
180 /* Reset the descriptor queues' current and end address values */
181 priv->tx_dq.current = priv->tx_dq.base;
182 priv->tx_dq.end = (priv->tx_dq.base + NUMTXDESC);
184 priv->tx_sq.current = priv->tx_sq.base;
185 priv->tx_sq.end = (priv->tx_sq.base + NUMTXDESC);
187 priv->rx_dq.current = priv->rx_dq.base;
188 priv->rx_dq.end = (priv->rx_dq.base + NUMRXDESC);
190 priv->rx_sq.current = priv->rx_sq.base;
191 priv->rx_sq.end = (priv->rx_sq.base + NUMRXDESC);
194 * Set the transmit descriptor and status queues' base address,
195 * current address, and length registers. Set the maximum frame
196 * length and threshold. Enable the transmit descriptor processor.
198 writel((uint32_t)priv->tx_dq.base, &mac->txdq.badd);
199 writel((uint32_t)priv->tx_dq.base, &mac->txdq.curadd);
200 writel(sizeof(struct tx_descriptor) * NUMTXDESC, &mac->txdq.blen);
202 writel((uint32_t)priv->tx_sq.base, &mac->txstsq.badd);
203 writel((uint32_t)priv->tx_sq.base, &mac->txstsq.curadd);
204 writel(sizeof(struct tx_status) * NUMTXDESC, &mac->txstsq.blen);
206 writel(0x00040000, &mac->txdthrshld);
207 writel(0x00040000, &mac->txststhrshld);
209 writel((TXSTARTMAX << 0) | (PKTSIZE_ALIGN << 16), &mac->maxfrmlen);
210 writel(BMCTL_TXEN, &mac->bmctl);
213 * Set the receive descriptor and status queues' base address,
214 * current address, and length registers. Enable the receive
215 * descriptor processor.
217 writel((uint32_t)priv->rx_dq.base, &mac->rxdq.badd);
218 writel((uint32_t)priv->rx_dq.base, &mac->rxdq.curadd);
219 writel(sizeof(struct rx_descriptor) * NUMRXDESC, &mac->rxdq.blen);
221 writel((uint32_t)priv->rx_sq.base, &mac->rxstsq.badd);
222 writel((uint32_t)priv->rx_sq.base, &mac->rxstsq.curadd);
223 writel(sizeof(struct rx_status) * NUMRXDESC, &mac->rxstsq.blen);
225 writel(0x00040000, &mac->rxdthrshld);
227 writel(BMCTL_RXEN, &mac->bmctl);
229 writel(0x00040000, &mac->rxststhrshld);
231 /* Wait until the receive descriptor processor is active */
232 while (!(readl(&mac->bmsts) & BMSTS_RXACT))
236 * Initialize the RX descriptor queue. Clear the TX descriptor queue.
237 * Clear the RX and TX status queues. Enqueue the RX descriptor and
238 * status entries to the MAC.
240 for (i = 0; i < NUMRXDESC; i++) {
241 /* set buffer address */
242 (priv->rx_dq.base + i)->word1 = (uint32_t)net_rx_packets[i];
244 /* set buffer length, clear buffer index and NSOF */
245 (priv->rx_dq.base + i)->word2 = PKTSIZE_ALIGN;
248 memset(priv->tx_dq.base, 0,
249 (sizeof(struct tx_descriptor) * NUMTXDESC));
250 memset(priv->rx_sq.base, 0,
251 (sizeof(struct rx_status) * NUMRXDESC));
252 memset(priv->tx_sq.base, 0,
253 (sizeof(struct tx_status) * NUMTXDESC));
255 writel(NUMRXDESC, &mac->rxdqenq);
256 writel(NUMRXDESC, &mac->rxstsqenq);
258 /* Set the primary MAC address */
259 writel(AFP_IAPRIMARY, &mac->afp);
260 writel(mac_addr[0] | (mac_addr[1] << 8) |
261 (mac_addr[2] << 16) | (mac_addr[3] << 24),
263 writel(mac_addr[4] | (mac_addr[5] << 8), &mac->indad_upper);
265 /* Turn on RX and TX */
266 writel(RXCTL_IA0 | RXCTL_BA | RXCTL_SRXON |
267 RXCTL_RCRCA | RXCTL_MA, &mac->rxctl);
268 writel(TXCTL_STXON, &mac->txctl);
270 /* Dump data structures if we're debugging */
272 dump_rx_descriptor_queue(dev);
273 dump_rx_status_queue(dev);
274 dump_tx_descriptor_queue(dev);
275 dump_tx_status_queue(dev);
277 debug("-ep93xx_eth_open");
283 * Halt EP93xx MAC transmit and receive by clearing the TxCTL and RxCTL
286 static void ep93xx_eth_close(struct eth_device *dev)
288 struct mac_regs *mac = GET_REGS(dev);
290 debug("+ep93xx_eth_close");
292 writel(0x00000000, &mac->rxctl);
293 writel(0x00000000, &mac->txctl);
295 debug("-ep93xx_eth_close");
299 * Copy a frame of data from the MAC into the protocol layer for further
302 static int ep93xx_eth_rcv_packet(struct eth_device *dev)
304 struct mac_regs *mac = GET_REGS(dev);
305 struct ep93xx_priv *priv = GET_PRIV(dev);
308 debug("+ep93xx_eth_rcv_packet");
310 if (RX_STATUS_RFP(priv->rx_sq.current)) {
311 if (RX_STATUS_RWE(priv->rx_sq.current)) {
313 * We have a good frame. Extract the frame's length
314 * from the current rx_status_queue entry, and copy
315 * the frame's data into net_rx_packets[] of the
316 * protocol stack. We track the total number of
317 * bytes in the frame (nbytes_frame) which will be
318 * used when we pass the data off to the protocol
319 * layer via net_process_received_packet().
321 len = RX_STATUS_FRAME_LEN(priv->rx_sq.current);
323 net_process_received_packet(
324 (uchar *)priv->rx_dq.current->word1, len);
326 debug("reporting %d bytes...\n", len);
328 /* Do we have an erroneous packet? */
329 pr_err("packet rx error, status %08X %08X",
330 priv->rx_sq.current->word1,
331 priv->rx_sq.current->word2);
332 dump_rx_descriptor_queue(dev);
333 dump_rx_status_queue(dev);
337 * Clear the associated status queue entry, and
338 * increment our current pointers to the next RX
339 * descriptor and status queue entries (making sure
342 memset((void *)priv->rx_sq.current, 0,
343 sizeof(struct rx_status));
345 priv->rx_sq.current++;
346 if (priv->rx_sq.current >= priv->rx_sq.end)
347 priv->rx_sq.current = priv->rx_sq.base;
349 priv->rx_dq.current++;
350 if (priv->rx_dq.current >= priv->rx_dq.end)
351 priv->rx_dq.current = priv->rx_dq.base;
354 * Finally, return the RX descriptor and status entries
355 * back to the MAC engine, and loop again, checking for
356 * more descriptors to process.
358 writel(1, &mac->rxdqenq);
359 writel(1, &mac->rxstsqenq);
364 debug("-ep93xx_eth_rcv_packet %d", len);
369 * Send a block of data via ethernet.
371 static int ep93xx_eth_send_packet(struct eth_device *dev,
372 void * const packet, int const length)
374 struct mac_regs *mac = GET_REGS(dev);
375 struct ep93xx_priv *priv = GET_PRIV(dev);
378 debug("+ep93xx_eth_send_packet");
380 /* Parameter check */
381 BUG_ON(packet == NULL);
384 * Initialize the TX descriptor queue with the new packet's info.
385 * Clear the associated status queue entry. Enqueue the packet
386 * to the MAC for transmission.
389 /* set buffer address */
390 priv->tx_dq.current->word1 = (uint32_t)packet;
392 /* set buffer length and EOF bit */
393 priv->tx_dq.current->word2 = length | TX_DESC_EOF;
395 /* clear tx status */
396 priv->tx_sq.current->word1 = 0;
398 /* enqueue the TX descriptor */
399 writel(1, &mac->txdqenq);
401 /* wait for the frame to become processed */
402 while (!TX_STATUS_TXFP(priv->tx_sq.current))
405 if (!TX_STATUS_TXWE(priv->tx_sq.current)) {
406 pr_err("packet tx error, status %08X",
407 priv->tx_sq.current->word1);
408 dump_tx_descriptor_queue(dev);
409 dump_tx_status_queue(dev);
411 /* TODO: Add better error handling? */
419 debug("-ep93xx_eth_send_packet %d", ret);
423 #if defined(CONFIG_MII)
424 int ep93xx_miiphy_initialize(bd_t * const bd)
427 struct mii_dev *mdiodev = mdio_alloc();
430 strncpy(mdiodev->name, "ep93xx_eth0", MDIO_NAME_LEN);
431 mdiodev->read = ep93xx_miiphy_read;
432 mdiodev->write = ep93xx_miiphy_write;
434 retval = mdio_register(mdiodev);
442 * Initialize the EP93xx MAC. The MAC hardware is reset. Buffers are
443 * allocated, if necessary, for the TX and RX descriptor and status queues,
444 * as well as for received packets. The EP93XX MAC hardware is initialized.
445 * Transmit and receive operations are enabled.
447 int ep93xx_eth_initialize(u8 dev_num, int base_addr)
450 struct eth_device *dev;
451 struct ep93xx_priv *priv;
453 debug("+ep93xx_eth_initialize");
455 priv = malloc(sizeof(*priv));
457 pr_err("malloc() failed");
458 goto eth_init_failed_0;
460 memset(priv, 0, sizeof(*priv));
462 priv->regs = (struct mac_regs *)base_addr;
464 priv->tx_dq.base = calloc(NUMTXDESC,
465 sizeof(struct tx_descriptor));
466 if (priv->tx_dq.base == NULL) {
467 pr_err("calloc() failed");
468 goto eth_init_failed_1;
471 priv->tx_sq.base = calloc(NUMTXDESC,
472 sizeof(struct tx_status));
473 if (priv->tx_sq.base == NULL) {
474 pr_err("calloc() failed");
475 goto eth_init_failed_2;
478 priv->rx_dq.base = calloc(NUMRXDESC,
479 sizeof(struct rx_descriptor));
480 if (priv->rx_dq.base == NULL) {
481 pr_err("calloc() failed");
482 goto eth_init_failed_3;
485 priv->rx_sq.base = calloc(NUMRXDESC,
486 sizeof(struct rx_status));
487 if (priv->rx_sq.base == NULL) {
488 pr_err("calloc() failed");
489 goto eth_init_failed_4;
492 dev = malloc(sizeof *dev);
494 pr_err("malloc() failed");
495 goto eth_init_failed_5;
497 memset(dev, 0, sizeof *dev);
499 dev->iobase = base_addr;
501 dev->init = ep93xx_eth_open;
502 dev->halt = ep93xx_eth_close;
503 dev->send = ep93xx_eth_send_packet;
504 dev->recv = ep93xx_eth_rcv_packet;
506 sprintf(dev->name, "ep93xx_eth-%hu", dev_num);
515 free(priv->rx_sq.base);
519 free(priv->rx_dq.base);
523 free(priv->tx_sq.base);
527 free(priv->tx_dq.base);
538 debug("-ep93xx_eth_initialize %d", ret);
542 #if defined(CONFIG_MII)
545 * Maximum MII address we support
547 #define MII_ADDRESS_MAX 31
550 * Maximum MII register address we support
552 #define MII_REGISTER_MAX 31
555 * Read a 16-bit value from an MII register.
557 static int ep93xx_miiphy_read(struct mii_dev *bus, int addr, int devad,
560 unsigned short value = 0;
561 struct mac_regs *mac = (struct mac_regs *)MAC_BASE;
565 debug("+ep93xx_miiphy_read");
567 /* Parameter checks */
568 BUG_ON(bus->name == NULL);
569 BUG_ON(addr > MII_ADDRESS_MAX);
570 BUG_ON(reg > MII_REGISTER_MAX);
573 * Save the current SelfCTL register value. Set MAC to suppress
574 * preamble bits. Wait for any previous MII command to complete
575 * before issuing the new command.
577 self_ctl = readl(&mac->selfctl);
578 #if defined(CONFIG_MII_SUPPRESS_PREAMBLE)
579 writel(self_ctl & ~(1 << 8), &mac->selfctl);
580 #endif /* defined(CONFIG_MII_SUPPRESS_PREAMBLE) */
582 while (readl(&mac->miists) & MIISTS_BUSY)
586 * Issue the MII 'read' command. Wait for the command to complete.
587 * Read the MII data value.
589 writel(MIICMD_OPCODE_READ | ((uint32_t)addr << 5) | (uint32_t)reg,
591 while (readl(&mac->miists) & MIISTS_BUSY)
594 value = (unsigned short)readl(&mac->miidata);
596 /* Restore the saved SelfCTL value and return. */
597 writel(self_ctl, &mac->selfctl);
602 debug("-ep93xx_miiphy_read");
609 * Write a 16-bit value to an MII register.
611 static int ep93xx_miiphy_write(struct mii_dev *bus, int addr, int devad,
614 struct mac_regs *mac = (struct mac_regs *)MAC_BASE;
618 debug("+ep93xx_miiphy_write");
620 /* Parameter checks */
621 BUG_ON(bus->name == NULL);
622 BUG_ON(addr > MII_ADDRESS_MAX);
623 BUG_ON(reg > MII_REGISTER_MAX);
626 * Save the current SelfCTL register value. Set MAC to suppress
627 * preamble bits. Wait for any previous MII command to complete
628 * before issuing the new command.
630 self_ctl = readl(&mac->selfctl);
631 #if defined(CONFIG_MII_SUPPRESS_PREAMBLE)
632 writel(self_ctl & ~(1 << 8), &mac->selfctl);
633 #endif /* defined(CONFIG_MII_SUPPRESS_PREAMBLE) */
635 while (readl(&mac->miists) & MIISTS_BUSY)
638 /* Issue the MII 'write' command. Wait for the command to complete. */
639 writel((uint32_t)value, &mac->miidata);
640 writel(MIICMD_OPCODE_WRITE | ((uint32_t)addr << 5) | (uint32_t)reg,
642 while (readl(&mac->miists) & MIISTS_BUSY)
645 /* Restore the saved SelfCTL value and return. */
646 writel(self_ctl, &mac->selfctl);
651 debug("-ep93xx_miiphy_write");
654 #endif /* defined(CONFIG_MII) */