2 * Driver for the IDT RC32434 (Korina) on-chip ethernet controller.
4 * Copyright 2004 IDT Inc. (rischelp@idt.com)
5 * Copyright 2006 Felix Fietkau <nbd@openwrt.org>
6 * Copyright 2008 Florian Fainelli <florian@openwrt.org>
8 * This program is free software; you can redistribute it and/or modify it
9 * under the terms of the GNU General Public License as published by the
10 * Free Software Foundation; either version 2 of the License, or (at your
11 * option) any later version.
13 * THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESS OR IMPLIED
14 * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
15 * MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN
16 * NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
17 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
18 * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF
19 * USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON
20 * ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
21 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
22 * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
24 * You should have received a copy of the GNU General Public License along
25 * with this program; if not, write to the Free Software Foundation, Inc.,
26 * 675 Mass Ave, Cambridge, MA 02139, USA.
28 * Writing to a DMA status register:
30 * When writing to the status register, you should mask the bit you have
31 * been testing the status register with. Both Tx and Rx DMA registers
32 * should stick to this procedure.
35 #include <linux/module.h>
36 #include <linux/kernel.h>
37 #include <linux/moduleparam.h>
38 #include <linux/sched.h>
39 #include <linux/ctype.h>
40 #include <linux/types.h>
41 #include <linux/interrupt.h>
42 #include <linux/init.h>
43 #include <linux/ioport.h>
45 #include <linux/slab.h>
46 #include <linux/string.h>
47 #include <linux/delay.h>
48 #include <linux/netdevice.h>
49 #include <linux/etherdevice.h>
50 #include <linux/skbuff.h>
51 #include <linux/errno.h>
52 #include <linux/platform_device.h>
53 #include <linux/mii.h>
54 #include <linux/ethtool.h>
55 #include <linux/crc32.h>
57 #include <asm/bootinfo.h>
58 #include <asm/system.h>
59 #include <asm/bitops.h>
60 #include <asm/pgtable.h>
61 #include <asm/segment.h>
65 #include <asm/mach-rc32434/rb.h>
66 #include <asm/mach-rc32434/rc32434.h>
67 #include <asm/mach-rc32434/eth.h>
68 #include <asm/mach-rc32434/dma_v.h>
70 #define DRV_NAME "korina"
71 #define DRV_VERSION "0.10"
72 #define DRV_RELDATE "04Mar2008"
74 #define STATION_ADDRESS_HIGH(dev) (((dev)->dev_addr[0] << 8) | \
76 #define STATION_ADDRESS_LOW(dev) (((dev)->dev_addr[2] << 24) | \
77 ((dev)->dev_addr[3] << 16) | \
78 ((dev)->dev_addr[4] << 8) | \
81 #define MII_CLOCK 1250000 /* no more than 2.5MHz */
83 /* the following must be powers of two */
84 #define KORINA_NUM_RDS 64 /* number of receive descriptors */
85 #define KORINA_NUM_TDS 64 /* number of transmit descriptors */
87 #define KORINA_RBSIZE 536 /* size of one resource buffer = Ether MTU */
88 #define KORINA_RDS_MASK (KORINA_NUM_RDS - 1)
89 #define KORINA_TDS_MASK (KORINA_NUM_TDS - 1)
90 #define RD_RING_SIZE (KORINA_NUM_RDS * sizeof(struct dma_desc))
91 #define TD_RING_SIZE (KORINA_NUM_TDS * sizeof(struct dma_desc))
93 #define TX_TIMEOUT (6000 * HZ / 1000)
95 enum chain_status { desc_filled, desc_empty };
96 #define IS_DMA_FINISHED(X) (((X) & (DMA_DESC_FINI)) != 0)
97 #define IS_DMA_DONE(X) (((X) & (DMA_DESC_DONE)) != 0)
98 #define RCVPKT_LENGTH(X) (((X) & ETH_RX_LEN) >> ETH_RX_LEN_BIT)
100 /* Information that need to be kept for each board. */
101 struct korina_private {
102 struct eth_regs *eth_regs;
103 struct dma_reg *rx_dma_regs;
104 struct dma_reg *tx_dma_regs;
105 struct dma_desc *td_ring; /* transmit descriptor ring */
106 struct dma_desc *rd_ring; /* receive descriptor ring */
108 struct sk_buff *tx_skb[KORINA_NUM_TDS];
109 struct sk_buff *rx_skb[KORINA_NUM_RDS];
114 enum chain_status rx_chain_status;
119 enum chain_status tx_chain_status;
128 spinlock_t lock; /* NIC xmit lock */
132 struct napi_struct napi;
133 struct mii_if_info mii_if;
134 struct net_device *dev;
138 extern unsigned int idt_cpu_freq;
140 static inline void korina_start_dma(struct dma_reg *ch, u32 dma_addr)
142 writel(0, &ch->dmandptr);
143 writel(dma_addr, &ch->dmadptr);
146 static inline void korina_abort_dma(struct net_device *dev,
149 if (readl(&ch->dmac) & DMA_CHAN_RUN_BIT) {
150 writel(0x10, &ch->dmac);
152 while (!(readl(&ch->dmas) & DMA_STAT_HALT))
153 dev->trans_start = jiffies;
155 writel(0, &ch->dmas);
158 writel(0, &ch->dmadptr);
159 writel(0, &ch->dmandptr);
162 static inline void korina_chain_dma(struct dma_reg *ch, u32 dma_addr)
164 writel(dma_addr, &ch->dmandptr);
167 static void korina_abort_tx(struct net_device *dev)
169 struct korina_private *lp = netdev_priv(dev);
171 korina_abort_dma(dev, lp->tx_dma_regs);
174 static void korina_abort_rx(struct net_device *dev)
176 struct korina_private *lp = netdev_priv(dev);
178 korina_abort_dma(dev, lp->rx_dma_regs);
181 static void korina_start_rx(struct korina_private *lp,
184 korina_start_dma(lp->rx_dma_regs, CPHYSADDR(rd));
187 static void korina_chain_rx(struct korina_private *lp,
190 korina_chain_dma(lp->rx_dma_regs, CPHYSADDR(rd));
193 /* transmit packet */
194 static int korina_send_packet(struct sk_buff *skb, struct net_device *dev)
196 struct korina_private *lp = netdev_priv(dev);
202 spin_lock_irqsave(&lp->lock, flags);
204 td = &lp->td_ring[lp->tx_chain_tail];
206 /* stop queue when full, drop pkts if queue already full */
207 if (lp->tx_count >= (KORINA_NUM_TDS - 2)) {
210 if (lp->tx_count == (KORINA_NUM_TDS - 2))
211 netif_stop_queue(dev);
213 dev->stats.tx_dropped++;
214 dev_kfree_skb_any(skb);
215 spin_unlock_irqrestore(&lp->lock, flags);
217 return NETDEV_TX_BUSY;
223 lp->tx_skb[lp->tx_chain_tail] = skb;
226 dma_cache_wback((u32)skb->data, skb->len);
228 /* Setup the transmit descriptor. */
229 dma_cache_inv((u32) td, sizeof(*td));
230 td->ca = CPHYSADDR(skb->data);
231 chain_index = (lp->tx_chain_tail - 1) &
234 if (readl(&(lp->tx_dma_regs->dmandptr)) == 0) {
235 if (lp->tx_chain_status == desc_empty) {
237 td->control = DMA_COUNT(length) |
238 DMA_DESC_COF | DMA_DESC_IOF;
240 lp->tx_chain_tail = chain_index;
242 writel(CPHYSADDR(&lp->td_ring[lp->tx_chain_head]),
243 &lp->tx_dma_regs->dmandptr);
244 /* Move head to tail */
245 lp->tx_chain_head = lp->tx_chain_tail;
248 td->control = DMA_COUNT(length) |
249 DMA_DESC_COF | DMA_DESC_IOF;
251 lp->td_ring[chain_index].control &=
254 lp->td_ring[chain_index].link = CPHYSADDR(td);
256 lp->tx_chain_tail = chain_index;
258 writel(CPHYSADDR(&lp->td_ring[lp->tx_chain_head]),
259 &(lp->tx_dma_regs->dmandptr));
260 /* Move head to tail */
261 lp->tx_chain_head = lp->tx_chain_tail;
262 lp->tx_chain_status = desc_empty;
265 if (lp->tx_chain_status == desc_empty) {
267 td->control = DMA_COUNT(length) |
268 DMA_DESC_COF | DMA_DESC_IOF;
270 lp->tx_chain_tail = chain_index;
271 lp->tx_chain_status = desc_filled;
272 netif_stop_queue(dev);
275 td->control = DMA_COUNT(length) |
276 DMA_DESC_COF | DMA_DESC_IOF;
277 lp->td_ring[chain_index].control &=
279 lp->td_ring[chain_index].link = CPHYSADDR(td);
280 lp->tx_chain_tail = chain_index;
283 dma_cache_wback((u32) td, sizeof(*td));
285 dev->trans_start = jiffies;
286 spin_unlock_irqrestore(&lp->lock, flags);
291 static int mdio_read(struct net_device *dev, int mii_id, int reg)
293 struct korina_private *lp = netdev_priv(dev);
296 mii_id = ((lp->rx_irq == 0x2c ? 1 : 0) << 8);
298 writel(0, &lp->eth_regs->miimcfg);
299 writel(0, &lp->eth_regs->miimcmd);
300 writel(mii_id | reg, &lp->eth_regs->miimaddr);
301 writel(ETH_MII_CMD_SCN, &lp->eth_regs->miimcmd);
303 ret = (int)(readl(&lp->eth_regs->miimrdd));
307 static void mdio_write(struct net_device *dev, int mii_id, int reg, int val)
309 struct korina_private *lp = netdev_priv(dev);
311 mii_id = ((lp->rx_irq == 0x2c ? 1 : 0) << 8);
313 writel(0, &lp->eth_regs->miimcfg);
314 writel(1, &lp->eth_regs->miimcmd);
315 writel(mii_id | reg, &lp->eth_regs->miimaddr);
316 writel(ETH_MII_CMD_SCN, &lp->eth_regs->miimcmd);
317 writel(val, &lp->eth_regs->miimwtd);
320 /* Ethernet Rx DMA interrupt */
321 static irqreturn_t korina_rx_dma_interrupt(int irq, void *dev_id)
323 struct net_device *dev = dev_id;
324 struct korina_private *lp = netdev_priv(dev);
328 dmas = readl(&lp->rx_dma_regs->dmas);
329 if (dmas & (DMA_STAT_DONE | DMA_STAT_HALT | DMA_STAT_ERR)) {
330 netif_rx_schedule_prep(&lp->napi);
332 dmasm = readl(&lp->rx_dma_regs->dmasm);
333 writel(dmasm | (DMA_STAT_DONE |
334 DMA_STAT_HALT | DMA_STAT_ERR),
335 &lp->rx_dma_regs->dmasm);
337 if (dmas & DMA_STAT_ERR)
338 printk(KERN_ERR DRV_NAME "%s: DMA error\n", dev->name);
340 retval = IRQ_HANDLED;
347 static int korina_rx(struct net_device *dev, int limit)
349 struct korina_private *lp = netdev_priv(dev);
350 struct dma_desc *rd = &lp->rd_ring[lp->rx_next_done];
351 struct sk_buff *skb, *skb_new;
353 u32 devcs, pkt_len, dmas, rx_free_desc;
356 dma_cache_inv((u32)rd, sizeof(*rd));
358 for (count = 0; count < limit; count++) {
362 /* Update statistics counters */
363 if (devcs & ETH_RX_CRC)
364 dev->stats.rx_crc_errors++;
365 if (devcs & ETH_RX_LOR)
366 dev->stats.rx_length_errors++;
367 if (devcs & ETH_RX_LE)
368 dev->stats.rx_length_errors++;
369 if (devcs & ETH_RX_OVR)
370 dev->stats.rx_over_errors++;
371 if (devcs & ETH_RX_CV)
372 dev->stats.rx_frame_errors++;
373 if (devcs & ETH_RX_CES)
374 dev->stats.rx_length_errors++;
375 if (devcs & ETH_RX_MP)
376 dev->stats.multicast++;
378 if ((devcs & ETH_RX_LD) != ETH_RX_LD) {
379 /* check that this is a whole packet
380 * WARNING: DMA_FD bit incorrectly set
381 * in Rc32434 (errata ref #077) */
382 dev->stats.rx_errors++;
383 dev->stats.rx_dropped++;
386 while ((rx_free_desc = KORINA_RBSIZE - (u32)DMA_COUNT(rd->control)) != 0) {
387 /* init the var. used for the later
388 * operations within the while loop */
390 pkt_len = RCVPKT_LENGTH(devcs);
391 skb = lp->rx_skb[lp->rx_next_done];
393 if ((devcs & ETH_RX_ROK)) {
394 /* must be the (first and) last
396 pkt_buf = (u8 *)lp->rx_skb[lp->rx_next_done]->data;
398 /* invalidate the cache */
399 dma_cache_inv((unsigned long)pkt_buf, pkt_len - 4);
401 /* Malloc up new buffer. */
402 skb_new = netdev_alloc_skb(dev, KORINA_RBSIZE + 2);
406 /* Do not count the CRC */
407 skb_put(skb, pkt_len - 4);
408 skb->protocol = eth_type_trans(skb, dev);
410 /* Pass the packet to upper layers */
411 netif_receive_skb(skb);
412 dev->stats.rx_packets++;
413 dev->stats.rx_bytes += pkt_len;
415 /* Update the mcast stats */
416 if (devcs & ETH_RX_MP)
417 dev->stats.multicast++;
419 lp->rx_skb[lp->rx_next_done] = skb_new;
424 /* Restore descriptor's curr_addr */
426 rd->ca = CPHYSADDR(skb_new->data);
428 rd->ca = CPHYSADDR(skb->data);
430 rd->control = DMA_COUNT(KORINA_RBSIZE) |
431 DMA_DESC_COD | DMA_DESC_IOD;
432 lp->rd_ring[(lp->rx_next_done - 1) &
433 KORINA_RDS_MASK].control &=
436 lp->rx_next_done = (lp->rx_next_done + 1) & KORINA_RDS_MASK;
437 dma_cache_wback((u32)rd, sizeof(*rd));
438 rd = &lp->rd_ring[lp->rx_next_done];
439 writel(~DMA_STAT_DONE, &lp->rx_dma_regs->dmas);
443 dmas = readl(&lp->rx_dma_regs->dmas);
445 if (dmas & DMA_STAT_HALT) {
446 writel(~(DMA_STAT_HALT | DMA_STAT_ERR),
447 &lp->rx_dma_regs->dmas);
451 skb = lp->rx_skb[lp->rx_next_done];
452 rd->ca = CPHYSADDR(skb->data);
453 dma_cache_wback((u32)rd, sizeof(*rd));
454 korina_chain_rx(lp, rd);
460 static int korina_poll(struct napi_struct *napi, int budget)
462 struct korina_private *lp =
463 container_of(napi, struct korina_private, napi);
464 struct net_device *dev = lp->dev;
467 work_done = korina_rx(dev, budget);
468 if (work_done < budget) {
469 netif_rx_complete(napi);
471 writel(readl(&lp->rx_dma_regs->dmasm) &
472 ~(DMA_STAT_DONE | DMA_STAT_HALT | DMA_STAT_ERR),
473 &lp->rx_dma_regs->dmasm);
479 * Set or clear the multicast filter for this adaptor.
481 static void korina_multicast_list(struct net_device *dev)
483 struct korina_private *lp = netdev_priv(dev);
485 struct dev_mc_list *dmi = dev->mc_list;
486 u32 recognise = ETH_ARC_AB; /* always accept broadcasts */
489 /* Set promiscuous mode */
490 if (dev->flags & IFF_PROMISC)
491 recognise |= ETH_ARC_PRO;
493 else if ((dev->flags & IFF_ALLMULTI) || (dev->mc_count > 4))
494 /* All multicast and broadcast */
495 recognise |= ETH_ARC_AM;
497 /* Build the hash table */
498 if (dev->mc_count > 4) {
502 for (i = 0; i < 4; i++)
505 for (i = 0; i < dev->mc_count; i++) {
506 char *addrs = dmi->dmi_addr;
513 crc = ether_crc_le(6, addrs);
515 hash_table[crc >> 4] |= 1 << (15 - (crc & 0xf));
517 /* Accept filtered multicast */
518 recognise |= ETH_ARC_AFM;
520 /* Fill the MAC hash tables with their values */
521 writel((u32)(hash_table[1] << 16 | hash_table[0]),
522 &lp->eth_regs->ethhash0);
523 writel((u32)(hash_table[3] << 16 | hash_table[2]),
524 &lp->eth_regs->ethhash1);
527 spin_lock_irqsave(&lp->lock, flags);
528 writel(recognise, &lp->eth_regs->etharc);
529 spin_unlock_irqrestore(&lp->lock, flags);
532 static void korina_tx(struct net_device *dev)
534 struct korina_private *lp = netdev_priv(dev);
535 struct dma_desc *td = &lp->td_ring[lp->tx_next_done];
539 spin_lock(&lp->lock);
541 /* Process all desc that are done */
542 while (IS_DMA_FINISHED(td->control)) {
543 if (lp->tx_full == 1) {
544 netif_wake_queue(dev);
548 devcs = lp->td_ring[lp->tx_next_done].devcs;
549 if ((devcs & (ETH_TX_FD | ETH_TX_LD)) !=
550 (ETH_TX_FD | ETH_TX_LD)) {
551 dev->stats.tx_errors++;
552 dev->stats.tx_dropped++;
554 /* Should never happen */
555 printk(KERN_ERR DRV_NAME "%s: split tx ignored\n",
557 } else if (devcs & ETH_TX_TOK) {
558 dev->stats.tx_packets++;
559 dev->stats.tx_bytes +=
560 lp->tx_skb[lp->tx_next_done]->len;
562 dev->stats.tx_errors++;
563 dev->stats.tx_dropped++;
566 if (devcs & ETH_TX_UND)
567 dev->stats.tx_fifo_errors++;
569 /* Oversized frame */
570 if (devcs & ETH_TX_OF)
571 dev->stats.tx_aborted_errors++;
573 /* Excessive deferrals */
574 if (devcs & ETH_TX_ED)
575 dev->stats.tx_carrier_errors++;
577 /* Collisions: medium busy */
578 if (devcs & ETH_TX_EC)
579 dev->stats.collisions++;
582 if (devcs & ETH_TX_LC)
583 dev->stats.tx_window_errors++;
586 /* We must always free the original skb */
587 if (lp->tx_skb[lp->tx_next_done]) {
588 dev_kfree_skb_any(lp->tx_skb[lp->tx_next_done]);
589 lp->tx_skb[lp->tx_next_done] = NULL;
592 lp->td_ring[lp->tx_next_done].control = DMA_DESC_IOF;
593 lp->td_ring[lp->tx_next_done].devcs = ETH_TX_FD | ETH_TX_LD;
594 lp->td_ring[lp->tx_next_done].link = 0;
595 lp->td_ring[lp->tx_next_done].ca = 0;
598 /* Go on to next transmission */
599 lp->tx_next_done = (lp->tx_next_done + 1) & KORINA_TDS_MASK;
600 td = &lp->td_ring[lp->tx_next_done];
604 /* Clear the DMA status register */
605 dmas = readl(&lp->tx_dma_regs->dmas);
606 writel(~dmas, &lp->tx_dma_regs->dmas);
608 writel(readl(&lp->tx_dma_regs->dmasm) &
609 ~(DMA_STAT_FINI | DMA_STAT_ERR),
610 &lp->tx_dma_regs->dmasm);
612 spin_unlock(&lp->lock);
616 korina_tx_dma_interrupt(int irq, void *dev_id)
618 struct net_device *dev = dev_id;
619 struct korina_private *lp = netdev_priv(dev);
623 dmas = readl(&lp->tx_dma_regs->dmas);
625 if (dmas & (DMA_STAT_FINI | DMA_STAT_ERR)) {
628 dmasm = readl(&lp->tx_dma_regs->dmasm);
629 writel(dmasm | (DMA_STAT_FINI | DMA_STAT_ERR),
630 &lp->tx_dma_regs->dmasm);
632 if (lp->tx_chain_status == desc_filled &&
633 (readl(&(lp->tx_dma_regs->dmandptr)) == 0)) {
634 writel(CPHYSADDR(&lp->td_ring[lp->tx_chain_head]),
635 &(lp->tx_dma_regs->dmandptr));
636 lp->tx_chain_status = desc_empty;
637 lp->tx_chain_head = lp->tx_chain_tail;
638 dev->trans_start = jiffies;
640 if (dmas & DMA_STAT_ERR)
641 printk(KERN_ERR DRV_NAME "%s: DMA error\n", dev->name);
643 retval = IRQ_HANDLED;
651 static void korina_check_media(struct net_device *dev, unsigned int init_media)
653 struct korina_private *lp = netdev_priv(dev);
655 mii_check_media(&lp->mii_if, 0, init_media);
657 if (lp->mii_if.full_duplex)
658 writel(readl(&lp->eth_regs->ethmac2) | ETH_MAC2_FD,
659 &lp->eth_regs->ethmac2);
661 writel(readl(&lp->eth_regs->ethmac2) & ~ETH_MAC2_FD,
662 &lp->eth_regs->ethmac2);
665 static void korina_set_carrier(struct mii_if_info *mii)
667 if (mii->force_media) {
668 /* autoneg is off: Link is always assumed to be up */
669 if (!netif_carrier_ok(mii->dev))
670 netif_carrier_on(mii->dev);
671 } else /* Let MMI library update carrier status */
672 korina_check_media(mii->dev, 0);
675 static int korina_ioctl(struct net_device *dev, struct ifreq *rq, int cmd)
677 struct korina_private *lp = netdev_priv(dev);
678 struct mii_ioctl_data *data = if_mii(rq);
681 if (!netif_running(dev))
683 spin_lock_irq(&lp->lock);
684 rc = generic_mii_ioctl(&lp->mii_if, data, cmd, NULL);
685 spin_unlock_irq(&lp->lock);
686 korina_set_carrier(&lp->mii_if);
691 /* ethtool helpers */
692 static void netdev_get_drvinfo(struct net_device *dev,
693 struct ethtool_drvinfo *info)
695 struct korina_private *lp = netdev_priv(dev);
697 strcpy(info->driver, DRV_NAME);
698 strcpy(info->version, DRV_VERSION);
699 strcpy(info->bus_info, lp->dev->name);
702 static int netdev_get_settings(struct net_device *dev, struct ethtool_cmd *cmd)
704 struct korina_private *lp = netdev_priv(dev);
707 spin_lock_irq(&lp->lock);
708 rc = mii_ethtool_gset(&lp->mii_if, cmd);
709 spin_unlock_irq(&lp->lock);
714 static int netdev_set_settings(struct net_device *dev, struct ethtool_cmd *cmd)
716 struct korina_private *lp = netdev_priv(dev);
719 spin_lock_irq(&lp->lock);
720 rc = mii_ethtool_sset(&lp->mii_if, cmd);
721 spin_unlock_irq(&lp->lock);
722 korina_set_carrier(&lp->mii_if);
727 static u32 netdev_get_link(struct net_device *dev)
729 struct korina_private *lp = netdev_priv(dev);
731 return mii_link_ok(&lp->mii_if);
734 static struct ethtool_ops netdev_ethtool_ops = {
735 .get_drvinfo = netdev_get_drvinfo,
736 .get_settings = netdev_get_settings,
737 .set_settings = netdev_set_settings,
738 .get_link = netdev_get_link,
741 static void korina_alloc_ring(struct net_device *dev)
743 struct korina_private *lp = netdev_priv(dev);
746 /* Initialize the transmit descriptors */
747 for (i = 0; i < KORINA_NUM_TDS; i++) {
748 lp->td_ring[i].control = DMA_DESC_IOF;
749 lp->td_ring[i].devcs = ETH_TX_FD | ETH_TX_LD;
750 lp->td_ring[i].ca = 0;
751 lp->td_ring[i].link = 0;
753 lp->tx_next_done = lp->tx_chain_head = lp->tx_chain_tail =
754 lp->tx_full = lp->tx_count = 0;
755 lp->tx_chain_status = desc_empty;
757 /* Initialize the receive descriptors */
758 for (i = 0; i < KORINA_NUM_RDS; i++) {
759 struct sk_buff *skb = lp->rx_skb[i];
761 skb = dev_alloc_skb(KORINA_RBSIZE + 2);
766 lp->rd_ring[i].control = DMA_DESC_IOD |
767 DMA_COUNT(KORINA_RBSIZE);
768 lp->rd_ring[i].devcs = 0;
769 lp->rd_ring[i].ca = CPHYSADDR(skb->data);
770 lp->rd_ring[i].link = CPHYSADDR(&lp->rd_ring[i+1]);
774 lp->rd_ring[i].link = CPHYSADDR(&lp->rd_ring[0]);
775 lp->rx_next_done = 0;
777 lp->rd_ring[i].control |= DMA_DESC_COD;
778 lp->rx_chain_head = 0;
779 lp->rx_chain_tail = 0;
780 lp->rx_chain_status = desc_empty;
783 static void korina_free_ring(struct net_device *dev)
785 struct korina_private *lp = netdev_priv(dev);
788 for (i = 0; i < KORINA_NUM_RDS; i++) {
789 lp->rd_ring[i].control = 0;
791 dev_kfree_skb_any(lp->rx_skb[i]);
792 lp->rx_skb[i] = NULL;
795 for (i = 0; i < KORINA_NUM_TDS; i++) {
796 lp->td_ring[i].control = 0;
798 dev_kfree_skb_any(lp->tx_skb[i]);
799 lp->tx_skb[i] = NULL;
804 * Initialize the RC32434 ethernet controller.
806 static int korina_init(struct net_device *dev)
808 struct korina_private *lp = netdev_priv(dev);
811 korina_abort_tx(dev);
812 korina_abort_rx(dev);
814 /* reset ethernet logic */
815 writel(0, &lp->eth_regs->ethintfc);
816 while ((readl(&lp->eth_regs->ethintfc) & ETH_INT_FC_RIP))
817 dev->trans_start = jiffies;
819 /* Enable Ethernet Interface */
820 writel(ETH_INT_FC_EN, &lp->eth_regs->ethintfc);
823 korina_alloc_ring(dev);
825 writel(0, &lp->rx_dma_regs->dmas);
827 korina_start_rx(lp, &lp->rd_ring[0]);
829 writel(readl(&lp->tx_dma_regs->dmasm) &
830 ~(DMA_STAT_FINI | DMA_STAT_ERR),
831 &lp->tx_dma_regs->dmasm);
832 writel(readl(&lp->rx_dma_regs->dmasm) &
833 ~(DMA_STAT_DONE | DMA_STAT_HALT | DMA_STAT_ERR),
834 &lp->rx_dma_regs->dmasm);
836 /* Accept only packets destined for this Ethernet device address */
837 writel(ETH_ARC_AB, &lp->eth_regs->etharc);
839 /* Set all Ether station address registers to their initial values */
840 writel(STATION_ADDRESS_LOW(dev), &lp->eth_regs->ethsal0);
841 writel(STATION_ADDRESS_HIGH(dev), &lp->eth_regs->ethsah0);
843 writel(STATION_ADDRESS_LOW(dev), &lp->eth_regs->ethsal1);
844 writel(STATION_ADDRESS_HIGH(dev), &lp->eth_regs->ethsah1);
846 writel(STATION_ADDRESS_LOW(dev), &lp->eth_regs->ethsal2);
847 writel(STATION_ADDRESS_HIGH(dev), &lp->eth_regs->ethsah2);
849 writel(STATION_ADDRESS_LOW(dev), &lp->eth_regs->ethsal3);
850 writel(STATION_ADDRESS_HIGH(dev), &lp->eth_regs->ethsah3);
853 /* Frame Length Checking, Pad Enable, CRC Enable, Full Duplex set */
854 writel(ETH_MAC2_PE | ETH_MAC2_CEN | ETH_MAC2_FD,
855 &lp->eth_regs->ethmac2);
857 /* Back to back inter-packet-gap */
858 writel(0x15, &lp->eth_regs->ethipgt);
859 /* Non - Back to back inter-packet-gap */
860 writel(0x12, &lp->eth_regs->ethipgr);
862 /* Management Clock Prescaler Divisor
863 * Clock independent setting */
864 writel(((idt_cpu_freq) / MII_CLOCK + 1) & ~1,
865 &lp->eth_regs->ethmcp);
867 /* don't transmit until fifo contains 48b */
868 writel(48, &lp->eth_regs->ethfifott);
870 writel(ETH_MAC1_RE, &lp->eth_regs->ethmac1);
872 napi_enable(&lp->napi);
873 netif_start_queue(dev);
879 * Restart the RC32434 ethernet controller.
880 * FIXME: check the return status where we call it
882 static int korina_restart(struct net_device *dev)
884 struct korina_private *lp = netdev_priv(dev);
890 disable_irq(lp->rx_irq);
891 disable_irq(lp->tx_irq);
892 disable_irq(lp->ovr_irq);
893 disable_irq(lp->und_irq);
895 writel(readl(&lp->tx_dma_regs->dmasm) |
896 DMA_STAT_FINI | DMA_STAT_ERR,
897 &lp->tx_dma_regs->dmasm);
898 writel(readl(&lp->rx_dma_regs->dmasm) |
899 DMA_STAT_DONE | DMA_STAT_HALT | DMA_STAT_ERR,
900 &lp->rx_dma_regs->dmasm);
902 korina_free_ring(dev);
904 ret = korina_init(dev);
906 printk(KERN_ERR DRV_NAME "%s: cannot restart device\n",
910 korina_multicast_list(dev);
912 enable_irq(lp->und_irq);
913 enable_irq(lp->ovr_irq);
914 enable_irq(lp->tx_irq);
915 enable_irq(lp->rx_irq);
920 static void korina_clear_and_restart(struct net_device *dev, u32 value)
922 struct korina_private *lp = netdev_priv(dev);
924 netif_stop_queue(dev);
925 writel(value, &lp->eth_regs->ethintfc);
929 /* Ethernet Tx Underflow interrupt */
930 static irqreturn_t korina_und_interrupt(int irq, void *dev_id)
932 struct net_device *dev = dev_id;
933 struct korina_private *lp = netdev_priv(dev);
936 spin_lock(&lp->lock);
938 und = readl(&lp->eth_regs->ethintfc);
940 if (und & ETH_INT_FC_UND)
941 korina_clear_and_restart(dev, und & ~ETH_INT_FC_UND);
943 spin_unlock(&lp->lock);
948 static void korina_tx_timeout(struct net_device *dev)
950 struct korina_private *lp = netdev_priv(dev);
953 spin_lock_irqsave(&lp->lock, flags);
955 spin_unlock_irqrestore(&lp->lock, flags);
958 /* Ethernet Rx Overflow interrupt */
960 korina_ovr_interrupt(int irq, void *dev_id)
962 struct net_device *dev = dev_id;
963 struct korina_private *lp = netdev_priv(dev);
966 spin_lock(&lp->lock);
967 ovr = readl(&lp->eth_regs->ethintfc);
969 if (ovr & ETH_INT_FC_OVR)
970 korina_clear_and_restart(dev, ovr & ~ETH_INT_FC_OVR);
972 spin_unlock(&lp->lock);
977 #ifdef CONFIG_NET_POLL_CONTROLLER
978 static void korina_poll_controller(struct net_device *dev)
980 disable_irq(dev->irq);
981 korina_tx_dma_interrupt(dev->irq, dev);
982 enable_irq(dev->irq);
986 static int korina_open(struct net_device *dev)
988 struct korina_private *lp = netdev_priv(dev);
992 ret = korina_init(dev);
994 printk(KERN_ERR DRV_NAME "%s: cannot open device\n", dev->name);
998 /* Install the interrupt handler
999 * that handles the Done Finished
1000 * Ovr and Und Events */
1001 ret = request_irq(lp->rx_irq, &korina_rx_dma_interrupt,
1002 IRQF_SHARED | IRQF_DISABLED, "Korina ethernet Rx", dev);
1004 printk(KERN_ERR DRV_NAME "%s: unable to get Rx DMA IRQ %d\n",
1005 dev->name, lp->rx_irq);
1008 ret = request_irq(lp->tx_irq, &korina_tx_dma_interrupt,
1009 IRQF_SHARED | IRQF_DISABLED, "Korina ethernet Tx", dev);
1011 printk(KERN_ERR DRV_NAME "%s: unable to get Tx DMA IRQ %d\n",
1012 dev->name, lp->tx_irq);
1013 goto err_free_rx_irq;
1016 /* Install handler for overrun error. */
1017 ret = request_irq(lp->ovr_irq, &korina_ovr_interrupt,
1018 IRQF_SHARED | IRQF_DISABLED, "Ethernet Overflow", dev);
1020 printk(KERN_ERR DRV_NAME"%s: unable to get OVR IRQ %d\n",
1021 dev->name, lp->ovr_irq);
1022 goto err_free_tx_irq;
1025 /* Install handler for underflow error. */
1026 ret = request_irq(lp->und_irq, &korina_und_interrupt,
1027 IRQF_SHARED | IRQF_DISABLED, "Ethernet Underflow", dev);
1029 printk(KERN_ERR DRV_NAME "%s: unable to get UND IRQ %d\n",
1030 dev->name, lp->und_irq);
1031 goto err_free_ovr_irq;
1037 free_irq(lp->ovr_irq, dev);
1039 free_irq(lp->tx_irq, dev);
1041 free_irq(lp->rx_irq, dev);
1043 korina_free_ring(dev);
1047 static int korina_close(struct net_device *dev)
1049 struct korina_private *lp = netdev_priv(dev);
1052 /* Disable interrupts */
1053 disable_irq(lp->rx_irq);
1054 disable_irq(lp->tx_irq);
1055 disable_irq(lp->ovr_irq);
1056 disable_irq(lp->und_irq);
1058 korina_abort_tx(dev);
1059 tmp = readl(&lp->tx_dma_regs->dmasm);
1060 tmp = tmp | DMA_STAT_FINI | DMA_STAT_ERR;
1061 writel(tmp, &lp->tx_dma_regs->dmasm);
1063 korina_abort_rx(dev);
1064 tmp = readl(&lp->rx_dma_regs->dmasm);
1065 tmp = tmp | DMA_STAT_DONE | DMA_STAT_HALT | DMA_STAT_ERR;
1066 writel(tmp, &lp->rx_dma_regs->dmasm);
1068 korina_free_ring(dev);
1070 free_irq(lp->rx_irq, dev);
1071 free_irq(lp->tx_irq, dev);
1072 free_irq(lp->ovr_irq, dev);
1073 free_irq(lp->und_irq, dev);
1078 static int korina_probe(struct platform_device *pdev)
1080 struct korina_device *bif = platform_get_drvdata(pdev);
1081 struct korina_private *lp;
1082 struct net_device *dev;
1086 dev = alloc_etherdev(sizeof(struct korina_private));
1088 printk(KERN_ERR DRV_NAME ": alloc_etherdev failed\n");
1091 SET_NETDEV_DEV(dev, &pdev->dev);
1092 platform_set_drvdata(pdev, dev);
1093 lp = netdev_priv(dev);
1096 memcpy(dev->dev_addr, bif->mac, 6);
1098 lp->rx_irq = platform_get_irq_byname(pdev, "korina_rx");
1099 lp->tx_irq = platform_get_irq_byname(pdev, "korina_tx");
1100 lp->ovr_irq = platform_get_irq_byname(pdev, "korina_ovr");
1101 lp->und_irq = platform_get_irq_byname(pdev, "korina_und");
1103 r = platform_get_resource_byname(pdev, IORESOURCE_MEM, "korina_regs");
1104 dev->base_addr = r->start;
1105 lp->eth_regs = ioremap_nocache(r->start, r->end - r->start);
1106 if (!lp->eth_regs) {
1107 printk(KERN_ERR DRV_NAME "cannot remap registers\n");
1112 r = platform_get_resource_byname(pdev, IORESOURCE_MEM, "korina_dma_rx");
1113 lp->rx_dma_regs = ioremap_nocache(r->start, r->end - r->start);
1114 if (!lp->rx_dma_regs) {
1115 printk(KERN_ERR DRV_NAME "cannot remap Rx DMA registers\n");
1117 goto probe_err_dma_rx;
1120 r = platform_get_resource_byname(pdev, IORESOURCE_MEM, "korina_dma_tx");
1121 lp->tx_dma_regs = ioremap_nocache(r->start, r->end - r->start);
1122 if (!lp->tx_dma_regs) {
1123 printk(KERN_ERR DRV_NAME "cannot remap Tx DMA registers\n");
1125 goto probe_err_dma_tx;
1128 lp->td_ring = kmalloc(TD_RING_SIZE + RD_RING_SIZE, GFP_KERNEL);
1130 printk(KERN_ERR DRV_NAME "cannot allocate descriptors\n");
1132 goto probe_err_td_ring;
1135 dma_cache_inv((unsigned long)(lp->td_ring),
1136 TD_RING_SIZE + RD_RING_SIZE);
1138 /* now convert TD_RING pointer to KSEG1 */
1139 lp->td_ring = (struct dma_desc *)KSEG1ADDR(lp->td_ring);
1140 lp->rd_ring = &lp->td_ring[KORINA_NUM_TDS];
1142 spin_lock_init(&lp->lock);
1143 /* just use the rx dma irq */
1144 dev->irq = lp->rx_irq;
1147 dev->open = korina_open;
1148 dev->stop = korina_close;
1149 dev->hard_start_xmit = korina_send_packet;
1150 dev->set_multicast_list = &korina_multicast_list;
1151 dev->ethtool_ops = &netdev_ethtool_ops;
1152 dev->tx_timeout = korina_tx_timeout;
1153 dev->watchdog_timeo = TX_TIMEOUT;
1154 dev->do_ioctl = &korina_ioctl;
1155 #ifdef CONFIG_NET_POLL_CONTROLLER
1156 dev->poll_controller = korina_poll_controller;
1158 netif_napi_add(dev, &lp->napi, korina_poll, 64);
1160 lp->phy_addr = (((lp->rx_irq == 0x2c? 1:0) << 8) | 0x05);
1161 lp->mii_if.dev = dev;
1162 lp->mii_if.mdio_read = mdio_read;
1163 lp->mii_if.mdio_write = mdio_write;
1164 lp->mii_if.phy_id = lp->phy_addr;
1165 lp->mii_if.phy_id_mask = 0x1f;
1166 lp->mii_if.reg_num_mask = 0x1f;
1168 rc = register_netdev(dev);
1170 printk(KERN_ERR DRV_NAME
1171 ": cannot register net device %d\n", rc);
1172 goto probe_err_register;
1180 iounmap(lp->tx_dma_regs);
1182 iounmap(lp->rx_dma_regs);
1184 iounmap(lp->eth_regs);
1190 static int korina_remove(struct platform_device *pdev)
1192 struct korina_device *bif = platform_get_drvdata(pdev);
1193 struct korina_private *lp = netdev_priv(bif->dev);
1195 iounmap(lp->eth_regs);
1196 iounmap(lp->rx_dma_regs);
1197 iounmap(lp->tx_dma_regs);
1199 platform_set_drvdata(pdev, NULL);
1200 unregister_netdev(bif->dev);
1201 free_netdev(bif->dev);
1206 static struct platform_driver korina_driver = {
1207 .driver.name = "korina",
1208 .probe = korina_probe,
1209 .remove = korina_remove,
1212 static int __init korina_init_module(void)
1214 return platform_driver_register(&korina_driver);
1217 static void korina_cleanup_module(void)
1219 return platform_driver_unregister(&korina_driver);
1222 module_init(korina_init_module);
1223 module_exit(korina_cleanup_module);
1225 MODULE_AUTHOR("Philip Rischel <rischelp@idt.com>");
1226 MODULE_AUTHOR("Felix Fietkau <nbd@openwrt.org>");
1227 MODULE_AUTHOR("Florian Fainelli <florian@openwrt.org>");
1228 MODULE_DESCRIPTION("IDT RC32434 (Korina) Ethernet driver");
1229 MODULE_LICENSE("GPL");