2 * linux/drivers/net/ethoc.c
4 * Copyright (C) 2007-2008 Avionic Design Development GmbH
5 * Copyright (C) 2008-2009 Avionic Design GmbH
7 * This program is free software; you can redistribute it and/or modify
8 * it under the terms of the GNU General Public License version 2 as
9 * published by the Free Software Foundation.
11 * Written by Thierry Reding <thierry.reding@avionic-design.de>
14 #include <linux/etherdevice.h>
15 #include <linux/crc32.h>
17 #include <linux/mii.h>
18 #include <linux/phy.h>
19 #include <linux/platform_device.h>
20 #include <linux/sched.h>
21 #include <linux/slab.h>
23 #include <net/ethoc.h>
25 static int buffer_size = 0x8000; /* 32 KBytes */
26 module_param(buffer_size, int, 0);
27 MODULE_PARM_DESC(buffer_size, "DMA buffer allocation size");
29 /* register offsets */
31 #define INT_SOURCE 0x04
36 #define PACKETLEN 0x18
38 #define TX_BD_NUM 0x20
39 #define CTRLMODER 0x24
41 #define MIICOMMAND 0x2c
42 #define MIIADDRESS 0x30
43 #define MIITX_DATA 0x34
44 #define MIIRX_DATA 0x38
45 #define MIISTATUS 0x3c
46 #define MAC_ADDR0 0x40
47 #define MAC_ADDR1 0x44
48 #define ETH_HASH0 0x48
49 #define ETH_HASH1 0x4c
50 #define ETH_TXCTRL 0x50
53 #define MODER_RXEN (1 << 0) /* receive enable */
54 #define MODER_TXEN (1 << 1) /* transmit enable */
55 #define MODER_NOPRE (1 << 2) /* no preamble */
56 #define MODER_BRO (1 << 3) /* broadcast address */
57 #define MODER_IAM (1 << 4) /* individual address mode */
58 #define MODER_PRO (1 << 5) /* promiscuous mode */
59 #define MODER_IFG (1 << 6) /* interframe gap for incoming frames */
60 #define MODER_LOOP (1 << 7) /* loopback */
61 #define MODER_NBO (1 << 8) /* no back-off */
62 #define MODER_EDE (1 << 9) /* excess defer enable */
63 #define MODER_FULLD (1 << 10) /* full duplex */
64 #define MODER_RESET (1 << 11) /* FIXME: reset (undocumented) */
65 #define MODER_DCRC (1 << 12) /* delayed CRC enable */
66 #define MODER_CRC (1 << 13) /* CRC enable */
67 #define MODER_HUGE (1 << 14) /* huge packets enable */
68 #define MODER_PAD (1 << 15) /* padding enabled */
69 #define MODER_RSM (1 << 16) /* receive small packets */
71 /* interrupt source and mask registers */
72 #define INT_MASK_TXF (1 << 0) /* transmit frame */
73 #define INT_MASK_TXE (1 << 1) /* transmit error */
74 #define INT_MASK_RXF (1 << 2) /* receive frame */
75 #define INT_MASK_RXE (1 << 3) /* receive error */
76 #define INT_MASK_BUSY (1 << 4)
77 #define INT_MASK_TXC (1 << 5) /* transmit control frame */
78 #define INT_MASK_RXC (1 << 6) /* receive control frame */
80 #define INT_MASK_TX (INT_MASK_TXF | INT_MASK_TXE)
81 #define INT_MASK_RX (INT_MASK_RXF | INT_MASK_RXE)
83 #define INT_MASK_ALL ( \
84 INT_MASK_TXF | INT_MASK_TXE | \
85 INT_MASK_RXF | INT_MASK_RXE | \
86 INT_MASK_TXC | INT_MASK_RXC | \
90 /* packet length register */
91 #define PACKETLEN_MIN(min) (((min) & 0xffff) << 16)
92 #define PACKETLEN_MAX(max) (((max) & 0xffff) << 0)
93 #define PACKETLEN_MIN_MAX(min, max) (PACKETLEN_MIN(min) | \
96 /* transmit buffer number register */
97 #define TX_BD_NUM_VAL(x) (((x) <= 0x80) ? (x) : 0x80)
99 /* control module mode register */
100 #define CTRLMODER_PASSALL (1 << 0) /* pass all receive frames */
101 #define CTRLMODER_RXFLOW (1 << 1) /* receive control flow */
102 #define CTRLMODER_TXFLOW (1 << 2) /* transmit control flow */
104 /* MII mode register */
105 #define MIIMODER_CLKDIV(x) ((x) & 0xfe) /* needs to be an even number */
106 #define MIIMODER_NOPRE (1 << 8) /* no preamble */
108 /* MII command register */
109 #define MIICOMMAND_SCAN (1 << 0) /* scan status */
110 #define MIICOMMAND_READ (1 << 1) /* read status */
111 #define MIICOMMAND_WRITE (1 << 2) /* write control data */
113 /* MII address register */
114 #define MIIADDRESS_FIAD(x) (((x) & 0x1f) << 0)
115 #define MIIADDRESS_RGAD(x) (((x) & 0x1f) << 8)
116 #define MIIADDRESS_ADDR(phy, reg) (MIIADDRESS_FIAD(phy) | \
117 MIIADDRESS_RGAD(reg))
119 /* MII transmit data register */
120 #define MIITX_DATA_VAL(x) ((x) & 0xffff)
122 /* MII receive data register */
123 #define MIIRX_DATA_VAL(x) ((x) & 0xffff)
125 /* MII status register */
126 #define MIISTATUS_LINKFAIL (1 << 0)
127 #define MIISTATUS_BUSY (1 << 1)
128 #define MIISTATUS_INVALID (1 << 2)
130 /* TX buffer descriptor */
131 #define TX_BD_CS (1 << 0) /* carrier sense lost */
132 #define TX_BD_DF (1 << 1) /* defer indication */
133 #define TX_BD_LC (1 << 2) /* late collision */
134 #define TX_BD_RL (1 << 3) /* retransmission limit */
135 #define TX_BD_RETRY_MASK (0x00f0)
136 #define TX_BD_RETRY(x) (((x) & 0x00f0) >> 4)
137 #define TX_BD_UR (1 << 8) /* transmitter underrun */
138 #define TX_BD_CRC (1 << 11) /* TX CRC enable */
139 #define TX_BD_PAD (1 << 12) /* pad enable for short packets */
140 #define TX_BD_WRAP (1 << 13)
141 #define TX_BD_IRQ (1 << 14) /* interrupt request enable */
142 #define TX_BD_READY (1 << 15) /* TX buffer ready */
143 #define TX_BD_LEN(x) (((x) & 0xffff) << 16)
144 #define TX_BD_LEN_MASK (0xffff << 16)
146 #define TX_BD_STATS (TX_BD_CS | TX_BD_DF | TX_BD_LC | \
147 TX_BD_RL | TX_BD_RETRY_MASK | TX_BD_UR)
149 /* RX buffer descriptor */
150 #define RX_BD_LC (1 << 0) /* late collision */
151 #define RX_BD_CRC (1 << 1) /* RX CRC error */
152 #define RX_BD_SF (1 << 2) /* short frame */
153 #define RX_BD_TL (1 << 3) /* too long */
154 #define RX_BD_DN (1 << 4) /* dribble nibble */
155 #define RX_BD_IS (1 << 5) /* invalid symbol */
156 #define RX_BD_OR (1 << 6) /* receiver overrun */
157 #define RX_BD_MISS (1 << 7)
158 #define RX_BD_CF (1 << 8) /* control frame */
159 #define RX_BD_WRAP (1 << 13)
160 #define RX_BD_IRQ (1 << 14) /* interrupt request enable */
161 #define RX_BD_EMPTY (1 << 15)
162 #define RX_BD_LEN(x) (((x) & 0xffff) << 16)
164 #define RX_BD_STATS (RX_BD_LC | RX_BD_CRC | RX_BD_SF | RX_BD_TL | \
165 RX_BD_DN | RX_BD_IS | RX_BD_OR | RX_BD_MISS)
167 #define ETHOC_BUFSIZ 1536
168 #define ETHOC_ZLEN 64
169 #define ETHOC_BD_BASE 0x400
170 #define ETHOC_TIMEOUT (HZ / 2)
171 #define ETHOC_MII_TIMEOUT (1 + (HZ / 5))
174 * struct ethoc - driver-private device structure
175 * @iobase: pointer to I/O memory region
176 * @membase: pointer to buffer memory region
177 * @dma_alloc: dma allocated buffer size
178 * @io_region_size: I/O memory region size
179 * @num_tx: number of send buffers
180 * @cur_tx: last send buffer written
181 * @dty_tx: last buffer actually sent
182 * @num_rx: number of receive buffers
183 * @cur_rx: current receive buffer
184 * @vma: pointer to array of virtual memory addresses for buffers
185 * @netdev: pointer to network device structure
186 * @napi: NAPI structure
187 * @msg_enable: device state flags
188 * @rx_lock: receive lock
191 * @mdio: MDIO bus for PHY access
192 * @phy_id: address of attached PHY
195 void __iomem *iobase;
196 void __iomem *membase;
198 resource_size_t io_region_size;
209 struct net_device *netdev;
210 struct napi_struct napi;
216 struct phy_device *phy;
217 struct mii_bus *mdio;
222 * struct ethoc_bd - buffer descriptor
223 * @stat: buffer statistics
224 * @addr: physical memory address
231 static inline u32 ethoc_read(struct ethoc *dev, loff_t offset)
233 return ioread32(dev->iobase + offset);
236 static inline void ethoc_write(struct ethoc *dev, loff_t offset, u32 data)
238 iowrite32(data, dev->iobase + offset);
241 static inline void ethoc_read_bd(struct ethoc *dev, int index,
244 loff_t offset = ETHOC_BD_BASE + (index * sizeof(struct ethoc_bd));
245 bd->stat = ethoc_read(dev, offset + 0);
246 bd->addr = ethoc_read(dev, offset + 4);
249 static inline void ethoc_write_bd(struct ethoc *dev, int index,
250 const struct ethoc_bd *bd)
252 loff_t offset = ETHOC_BD_BASE + (index * sizeof(struct ethoc_bd));
253 ethoc_write(dev, offset + 0, bd->stat);
254 ethoc_write(dev, offset + 4, bd->addr);
257 static inline void ethoc_enable_irq(struct ethoc *dev, u32 mask)
259 u32 imask = ethoc_read(dev, INT_MASK);
261 ethoc_write(dev, INT_MASK, imask);
264 static inline void ethoc_disable_irq(struct ethoc *dev, u32 mask)
266 u32 imask = ethoc_read(dev, INT_MASK);
268 ethoc_write(dev, INT_MASK, imask);
271 static inline void ethoc_ack_irq(struct ethoc *dev, u32 mask)
273 ethoc_write(dev, INT_SOURCE, mask);
276 static inline void ethoc_enable_rx_and_tx(struct ethoc *dev)
278 u32 mode = ethoc_read(dev, MODER);
279 mode |= MODER_RXEN | MODER_TXEN;
280 ethoc_write(dev, MODER, mode);
283 static inline void ethoc_disable_rx_and_tx(struct ethoc *dev)
285 u32 mode = ethoc_read(dev, MODER);
286 mode &= ~(MODER_RXEN | MODER_TXEN);
287 ethoc_write(dev, MODER, mode);
290 static int ethoc_init_ring(struct ethoc *dev, unsigned long mem_start)
300 ethoc_write(dev, TX_BD_NUM, dev->num_tx);
302 /* setup transmission buffers */
304 bd.stat = TX_BD_IRQ | TX_BD_CRC;
307 for (i = 0; i < dev->num_tx; i++) {
308 if (i == dev->num_tx - 1)
309 bd.stat |= TX_BD_WRAP;
311 ethoc_write_bd(dev, i, &bd);
312 bd.addr += ETHOC_BUFSIZ;
318 bd.stat = RX_BD_EMPTY | RX_BD_IRQ;
320 for (i = 0; i < dev->num_rx; i++) {
321 if (i == dev->num_rx - 1)
322 bd.stat |= RX_BD_WRAP;
324 ethoc_write_bd(dev, dev->num_tx + i, &bd);
325 bd.addr += ETHOC_BUFSIZ;
327 dev->vma[dev->num_tx + i] = vma;
334 static int ethoc_reset(struct ethoc *dev)
338 /* TODO: reset controller? */
340 ethoc_disable_rx_and_tx(dev);
342 /* TODO: setup registers */
344 /* enable FCS generation and automatic padding */
345 mode = ethoc_read(dev, MODER);
346 mode |= MODER_CRC | MODER_PAD;
347 ethoc_write(dev, MODER, mode);
349 /* set full-duplex mode */
350 mode = ethoc_read(dev, MODER);
352 ethoc_write(dev, MODER, mode);
353 ethoc_write(dev, IPGT, 0x15);
355 ethoc_ack_irq(dev, INT_MASK_ALL);
356 ethoc_enable_irq(dev, INT_MASK_ALL);
357 ethoc_enable_rx_and_tx(dev);
361 static unsigned int ethoc_update_rx_stats(struct ethoc *dev,
364 struct net_device *netdev = dev->netdev;
365 unsigned int ret = 0;
367 if (bd->stat & RX_BD_TL) {
368 dev_err(&netdev->dev, "RX: frame too long\n");
369 netdev->stats.rx_length_errors++;
373 if (bd->stat & RX_BD_SF) {
374 dev_err(&netdev->dev, "RX: frame too short\n");
375 netdev->stats.rx_length_errors++;
379 if (bd->stat & RX_BD_DN) {
380 dev_err(&netdev->dev, "RX: dribble nibble\n");
381 netdev->stats.rx_frame_errors++;
384 if (bd->stat & RX_BD_CRC) {
385 dev_err(&netdev->dev, "RX: wrong CRC\n");
386 netdev->stats.rx_crc_errors++;
390 if (bd->stat & RX_BD_OR) {
391 dev_err(&netdev->dev, "RX: overrun\n");
392 netdev->stats.rx_over_errors++;
396 if (bd->stat & RX_BD_MISS)
397 netdev->stats.rx_missed_errors++;
399 if (bd->stat & RX_BD_LC) {
400 dev_err(&netdev->dev, "RX: late collision\n");
401 netdev->stats.collisions++;
408 static int ethoc_rx(struct net_device *dev, int limit)
410 struct ethoc *priv = netdev_priv(dev);
413 for (count = 0; count < limit; ++count) {
417 entry = priv->num_tx + (priv->cur_rx % priv->num_rx);
418 ethoc_read_bd(priv, entry, &bd);
419 if (bd.stat & RX_BD_EMPTY)
422 if (ethoc_update_rx_stats(priv, &bd) == 0) {
423 int size = bd.stat >> 16;
426 size -= 4; /* strip the CRC */
427 skb = netdev_alloc_skb_ip_align(dev, size);
430 void *src = priv->vma[entry];
431 memcpy_fromio(skb_put(skb, size), src, size);
432 skb->protocol = eth_type_trans(skb, dev);
433 dev->stats.rx_packets++;
434 dev->stats.rx_bytes += size;
435 netif_receive_skb(skb);
438 dev_warn(&dev->dev, "low on memory - "
441 dev->stats.rx_dropped++;
446 /* clear the buffer descriptor so it can be reused */
447 bd.stat &= ~RX_BD_STATS;
448 bd.stat |= RX_BD_EMPTY;
449 ethoc_write_bd(priv, entry, &bd);
456 static int ethoc_update_tx_stats(struct ethoc *dev, struct ethoc_bd *bd)
458 struct net_device *netdev = dev->netdev;
460 if (bd->stat & TX_BD_LC) {
461 dev_err(&netdev->dev, "TX: late collision\n");
462 netdev->stats.tx_window_errors++;
465 if (bd->stat & TX_BD_RL) {
466 dev_err(&netdev->dev, "TX: retransmit limit\n");
467 netdev->stats.tx_aborted_errors++;
470 if (bd->stat & TX_BD_UR) {
471 dev_err(&netdev->dev, "TX: underrun\n");
472 netdev->stats.tx_fifo_errors++;
475 if (bd->stat & TX_BD_CS) {
476 dev_err(&netdev->dev, "TX: carrier sense lost\n");
477 netdev->stats.tx_carrier_errors++;
480 if (bd->stat & TX_BD_STATS)
481 netdev->stats.tx_errors++;
483 netdev->stats.collisions += (bd->stat >> 4) & 0xf;
484 netdev->stats.tx_bytes += bd->stat >> 16;
485 netdev->stats.tx_packets++;
489 static void ethoc_tx(struct net_device *dev)
491 struct ethoc *priv = netdev_priv(dev);
493 spin_lock(&priv->lock);
495 while (priv->dty_tx != priv->cur_tx) {
496 unsigned int entry = priv->dty_tx % priv->num_tx;
499 ethoc_read_bd(priv, entry, &bd);
500 if (bd.stat & TX_BD_READY)
503 entry = (++priv->dty_tx) % priv->num_tx;
504 (void)ethoc_update_tx_stats(priv, &bd);
507 if ((priv->cur_tx - priv->dty_tx) <= (priv->num_tx / 2))
508 netif_wake_queue(dev);
510 ethoc_ack_irq(priv, INT_MASK_TX);
511 spin_unlock(&priv->lock);
514 static irqreturn_t ethoc_interrupt(int irq, void *dev_id)
516 struct net_device *dev = dev_id;
517 struct ethoc *priv = netdev_priv(dev);
520 ethoc_disable_irq(priv, INT_MASK_ALL);
521 pending = ethoc_read(priv, INT_SOURCE);
522 if (unlikely(pending == 0)) {
523 ethoc_enable_irq(priv, INT_MASK_ALL);
527 ethoc_ack_irq(priv, pending);
529 if (pending & INT_MASK_BUSY) {
530 dev_err(&dev->dev, "packet dropped\n");
531 dev->stats.rx_dropped++;
534 if (pending & INT_MASK_RX) {
535 if (napi_schedule_prep(&priv->napi))
536 __napi_schedule(&priv->napi);
538 ethoc_enable_irq(priv, INT_MASK_RX);
541 if (pending & INT_MASK_TX)
544 ethoc_enable_irq(priv, INT_MASK_ALL & ~INT_MASK_RX);
548 static int ethoc_get_mac_address(struct net_device *dev, void *addr)
550 struct ethoc *priv = netdev_priv(dev);
551 u8 *mac = (u8 *)addr;
554 reg = ethoc_read(priv, MAC_ADDR0);
555 mac[2] = (reg >> 24) & 0xff;
556 mac[3] = (reg >> 16) & 0xff;
557 mac[4] = (reg >> 8) & 0xff;
558 mac[5] = (reg >> 0) & 0xff;
560 reg = ethoc_read(priv, MAC_ADDR1);
561 mac[0] = (reg >> 8) & 0xff;
562 mac[1] = (reg >> 0) & 0xff;
567 static int ethoc_poll(struct napi_struct *napi, int budget)
569 struct ethoc *priv = container_of(napi, struct ethoc, napi);
572 work_done = ethoc_rx(priv->netdev, budget);
573 if (work_done < budget) {
574 ethoc_enable_irq(priv, INT_MASK_RX);
581 static int ethoc_mdio_read(struct mii_bus *bus, int phy, int reg)
583 unsigned long timeout = jiffies + ETHOC_MII_TIMEOUT;
584 struct ethoc *priv = bus->priv;
586 ethoc_write(priv, MIIADDRESS, MIIADDRESS_ADDR(phy, reg));
587 ethoc_write(priv, MIICOMMAND, MIICOMMAND_READ);
589 while (time_before(jiffies, timeout)) {
590 u32 status = ethoc_read(priv, MIISTATUS);
591 if (!(status & MIISTATUS_BUSY)) {
592 u32 data = ethoc_read(priv, MIIRX_DATA);
593 /* reset MII command register */
594 ethoc_write(priv, MIICOMMAND, 0);
604 static int ethoc_mdio_write(struct mii_bus *bus, int phy, int reg, u16 val)
606 unsigned long timeout = jiffies + ETHOC_MII_TIMEOUT;
607 struct ethoc *priv = bus->priv;
609 ethoc_write(priv, MIIADDRESS, MIIADDRESS_ADDR(phy, reg));
610 ethoc_write(priv, MIITX_DATA, val);
611 ethoc_write(priv, MIICOMMAND, MIICOMMAND_WRITE);
613 while (time_before(jiffies, timeout)) {
614 u32 stat = ethoc_read(priv, MIISTATUS);
615 if (!(stat & MIISTATUS_BUSY)) {
616 /* reset MII command register */
617 ethoc_write(priv, MIICOMMAND, 0);
627 static int ethoc_mdio_reset(struct mii_bus *bus)
632 static void ethoc_mdio_poll(struct net_device *dev)
636 static int __devinit ethoc_mdio_probe(struct net_device *dev)
638 struct ethoc *priv = netdev_priv(dev);
639 struct phy_device *phy;
642 if (priv->phy_id != -1) {
643 phy = priv->mdio->phy_map[priv->phy_id];
645 phy = phy_find_first(priv->mdio);
649 dev_err(&dev->dev, "no PHY found\n");
653 err = phy_connect_direct(dev, phy, ethoc_mdio_poll, 0,
654 PHY_INTERFACE_MODE_GMII);
656 dev_err(&dev->dev, "could not attach to PHY\n");
664 static int ethoc_open(struct net_device *dev)
666 struct ethoc *priv = netdev_priv(dev);
669 ret = request_irq(dev->irq, ethoc_interrupt, IRQF_SHARED,
674 ethoc_init_ring(priv, dev->mem_start);
677 if (netif_queue_stopped(dev)) {
678 dev_dbg(&dev->dev, " resuming queue\n");
679 netif_wake_queue(dev);
681 dev_dbg(&dev->dev, " starting queue\n");
682 netif_start_queue(dev);
685 phy_start(priv->phy);
686 napi_enable(&priv->napi);
688 if (netif_msg_ifup(priv)) {
689 dev_info(&dev->dev, "I/O: %08lx Memory: %08lx-%08lx\n",
690 dev->base_addr, dev->mem_start, dev->mem_end);
696 static int ethoc_stop(struct net_device *dev)
698 struct ethoc *priv = netdev_priv(dev);
700 napi_disable(&priv->napi);
705 ethoc_disable_rx_and_tx(priv);
706 free_irq(dev->irq, dev);
708 if (!netif_queue_stopped(dev))
709 netif_stop_queue(dev);
714 static int ethoc_ioctl(struct net_device *dev, struct ifreq *ifr, int cmd)
716 struct ethoc *priv = netdev_priv(dev);
717 struct mii_ioctl_data *mdio = if_mii(ifr);
718 struct phy_device *phy = NULL;
720 if (!netif_running(dev))
723 if (cmd != SIOCGMIIPHY) {
724 if (mdio->phy_id >= PHY_MAX_ADDR)
727 phy = priv->mdio->phy_map[mdio->phy_id];
734 return phy_mii_ioctl(phy, ifr, cmd);
737 static int ethoc_config(struct net_device *dev, struct ifmap *map)
742 static int ethoc_set_mac_address(struct net_device *dev, void *addr)
744 struct ethoc *priv = netdev_priv(dev);
745 u8 *mac = (u8 *)addr;
747 ethoc_write(priv, MAC_ADDR0, (mac[2] << 24) | (mac[3] << 16) |
748 (mac[4] << 8) | (mac[5] << 0));
749 ethoc_write(priv, MAC_ADDR1, (mac[0] << 8) | (mac[1] << 0));
754 static void ethoc_set_multicast_list(struct net_device *dev)
756 struct ethoc *priv = netdev_priv(dev);
757 u32 mode = ethoc_read(priv, MODER);
758 struct netdev_hw_addr *ha;
759 u32 hash[2] = { 0, 0 };
761 /* set loopback mode if requested */
762 if (dev->flags & IFF_LOOPBACK)
767 /* receive broadcast frames if requested */
768 if (dev->flags & IFF_BROADCAST)
773 /* enable promiscuous mode if requested */
774 if (dev->flags & IFF_PROMISC)
779 ethoc_write(priv, MODER, mode);
781 /* receive multicast frames */
782 if (dev->flags & IFF_ALLMULTI) {
783 hash[0] = 0xffffffff;
784 hash[1] = 0xffffffff;
786 netdev_for_each_mc_addr(ha, dev) {
787 u32 crc = ether_crc(ETH_ALEN, ha->addr);
788 int bit = (crc >> 26) & 0x3f;
789 hash[bit >> 5] |= 1 << (bit & 0x1f);
793 ethoc_write(priv, ETH_HASH0, hash[0]);
794 ethoc_write(priv, ETH_HASH1, hash[1]);
797 static int ethoc_change_mtu(struct net_device *dev, int new_mtu)
802 static void ethoc_tx_timeout(struct net_device *dev)
804 struct ethoc *priv = netdev_priv(dev);
805 u32 pending = ethoc_read(priv, INT_SOURCE);
807 ethoc_interrupt(dev->irq, dev);
810 static netdev_tx_t ethoc_start_xmit(struct sk_buff *skb, struct net_device *dev)
812 struct ethoc *priv = netdev_priv(dev);
817 if (unlikely(skb->len > ETHOC_BUFSIZ)) {
818 dev->stats.tx_errors++;
822 entry = priv->cur_tx % priv->num_tx;
823 spin_lock_irq(&priv->lock);
826 ethoc_read_bd(priv, entry, &bd);
827 if (unlikely(skb->len < ETHOC_ZLEN))
828 bd.stat |= TX_BD_PAD;
830 bd.stat &= ~TX_BD_PAD;
832 dest = priv->vma[entry];
833 memcpy_toio(dest, skb->data, skb->len);
835 bd.stat &= ~(TX_BD_STATS | TX_BD_LEN_MASK);
836 bd.stat |= TX_BD_LEN(skb->len);
837 ethoc_write_bd(priv, entry, &bd);
839 bd.stat |= TX_BD_READY;
840 ethoc_write_bd(priv, entry, &bd);
842 if (priv->cur_tx == (priv->dty_tx + priv->num_tx)) {
843 dev_dbg(&dev->dev, "stopping queue\n");
844 netif_stop_queue(dev);
847 spin_unlock_irq(&priv->lock);
853 static const struct net_device_ops ethoc_netdev_ops = {
854 .ndo_open = ethoc_open,
855 .ndo_stop = ethoc_stop,
856 .ndo_do_ioctl = ethoc_ioctl,
857 .ndo_set_config = ethoc_config,
858 .ndo_set_mac_address = ethoc_set_mac_address,
859 .ndo_set_multicast_list = ethoc_set_multicast_list,
860 .ndo_change_mtu = ethoc_change_mtu,
861 .ndo_tx_timeout = ethoc_tx_timeout,
862 .ndo_start_xmit = ethoc_start_xmit,
866 * ethoc_probe() - initialize OpenCores ethernet MAC
867 * pdev: platform device
869 static int __devinit ethoc_probe(struct platform_device *pdev)
871 struct net_device *netdev = NULL;
872 struct resource *res = NULL;
873 struct resource *mmio = NULL;
874 struct resource *mem = NULL;
875 struct ethoc *priv = NULL;
880 /* allocate networking device */
881 netdev = alloc_etherdev(sizeof(struct ethoc));
883 dev_err(&pdev->dev, "cannot allocate network device\n");
888 SET_NETDEV_DEV(netdev, &pdev->dev);
889 platform_set_drvdata(pdev, netdev);
891 /* obtain I/O memory space */
892 res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
894 dev_err(&pdev->dev, "cannot obtain I/O memory space\n");
899 mmio = devm_request_mem_region(&pdev->dev, res->start,
900 resource_size(res), res->name);
902 dev_err(&pdev->dev, "cannot request I/O memory space\n");
907 netdev->base_addr = mmio->start;
909 /* obtain buffer memory space */
910 res = platform_get_resource(pdev, IORESOURCE_MEM, 1);
912 mem = devm_request_mem_region(&pdev->dev, res->start,
913 resource_size(res), res->name);
915 dev_err(&pdev->dev, "cannot request memory space\n");
920 netdev->mem_start = mem->start;
921 netdev->mem_end = mem->end;
925 /* obtain device IRQ number */
926 res = platform_get_resource(pdev, IORESOURCE_IRQ, 0);
928 dev_err(&pdev->dev, "cannot obtain IRQ\n");
933 netdev->irq = res->start;
935 /* setup driver-private data */
936 priv = netdev_priv(netdev);
937 priv->netdev = netdev;
939 priv->io_region_size = mmio->end - mmio->start + 1;
941 priv->iobase = devm_ioremap_nocache(&pdev->dev, netdev->base_addr,
942 resource_size(mmio));
944 dev_err(&pdev->dev, "cannot remap I/O memory space\n");
949 if (netdev->mem_end) {
950 priv->membase = devm_ioremap_nocache(&pdev->dev,
951 netdev->mem_start, resource_size(mem));
952 if (!priv->membase) {
953 dev_err(&pdev->dev, "cannot remap memory space\n");
958 /* Allocate buffer memory */
959 priv->membase = dmam_alloc_coherent(&pdev->dev,
960 buffer_size, (void *)&netdev->mem_start,
962 if (!priv->membase) {
963 dev_err(&pdev->dev, "cannot allocate %dB buffer\n",
968 netdev->mem_end = netdev->mem_start + buffer_size;
969 priv->dma_alloc = buffer_size;
972 /* calculate the number of TX/RX buffers, maximum 128 supported */
973 num_bd = min_t(unsigned int,
974 128, (netdev->mem_end - netdev->mem_start + 1) / ETHOC_BUFSIZ);
975 priv->num_tx = max(2, num_bd / 4);
976 priv->num_rx = num_bd - priv->num_tx;
978 priv->vma = devm_kzalloc(&pdev->dev, num_bd*sizeof(void*), GFP_KERNEL);
984 /* Allow the platform setup code to pass in a MAC address. */
985 if (pdev->dev.platform_data) {
986 struct ethoc_platform_data *pdata = pdev->dev.platform_data;
987 memcpy(netdev->dev_addr, pdata->hwaddr, IFHWADDRLEN);
988 priv->phy_id = pdata->phy_id;
996 mac = of_get_property(pdev->dev.of_node,
1000 memcpy(netdev->dev_addr, mac, IFHWADDRLEN);
1005 /* Check that the given MAC address is valid. If it isn't, read the
1006 * current MAC from the controller. */
1007 if (!is_valid_ether_addr(netdev->dev_addr))
1008 ethoc_get_mac_address(netdev, netdev->dev_addr);
1010 /* Check the MAC again for validity, if it still isn't choose and
1011 * program a random one. */
1012 if (!is_valid_ether_addr(netdev->dev_addr))
1013 random_ether_addr(netdev->dev_addr);
1015 ethoc_set_mac_address(netdev, netdev->dev_addr);
1017 /* register MII bus */
1018 priv->mdio = mdiobus_alloc();
1024 priv->mdio->name = "ethoc-mdio";
1025 snprintf(priv->mdio->id, MII_BUS_ID_SIZE, "%s-%d",
1026 priv->mdio->name, pdev->id);
1027 priv->mdio->read = ethoc_mdio_read;
1028 priv->mdio->write = ethoc_mdio_write;
1029 priv->mdio->reset = ethoc_mdio_reset;
1030 priv->mdio->priv = priv;
1032 priv->mdio->irq = kmalloc(sizeof(int) * PHY_MAX_ADDR, GFP_KERNEL);
1033 if (!priv->mdio->irq) {
1038 for (phy = 0; phy < PHY_MAX_ADDR; phy++)
1039 priv->mdio->irq[phy] = PHY_POLL;
1041 ret = mdiobus_register(priv->mdio);
1043 dev_err(&netdev->dev, "failed to register MDIO bus\n");
1047 ret = ethoc_mdio_probe(netdev);
1049 dev_err(&netdev->dev, "failed to probe MDIO bus\n");
1053 ether_setup(netdev);
1055 /* setup the net_device structure */
1056 netdev->netdev_ops = ðoc_netdev_ops;
1057 netdev->watchdog_timeo = ETHOC_TIMEOUT;
1058 netdev->features |= 0;
1061 netif_napi_add(netdev, &priv->napi, ethoc_poll, 64);
1063 spin_lock_init(&priv->rx_lock);
1064 spin_lock_init(&priv->lock);
1066 ret = register_netdev(netdev);
1068 dev_err(&netdev->dev, "failed to register interface\n");
1075 netif_napi_del(&priv->napi);
1077 mdiobus_unregister(priv->mdio);
1079 kfree(priv->mdio->irq);
1080 mdiobus_free(priv->mdio);
1082 free_netdev(netdev);
1088 * ethoc_remove() - shutdown OpenCores ethernet MAC
1089 * @pdev: platform device
1091 static int __devexit ethoc_remove(struct platform_device *pdev)
1093 struct net_device *netdev = platform_get_drvdata(pdev);
1094 struct ethoc *priv = netdev_priv(netdev);
1096 platform_set_drvdata(pdev, NULL);
1099 netif_napi_del(&priv->napi);
1100 phy_disconnect(priv->phy);
1104 mdiobus_unregister(priv->mdio);
1105 kfree(priv->mdio->irq);
1106 mdiobus_free(priv->mdio);
1108 unregister_netdev(netdev);
1109 free_netdev(netdev);
1116 static int ethoc_suspend(struct platform_device *pdev, pm_message_t state)
1121 static int ethoc_resume(struct platform_device *pdev)
1126 # define ethoc_suspend NULL
1127 # define ethoc_resume NULL
1131 static struct of_device_id ethoc_match[] = {
1133 .compatible = "opencores,ethoc",
1137 MODULE_DEVICE_TABLE(of, ethoc_match);
1140 static struct platform_driver ethoc_driver = {
1141 .probe = ethoc_probe,
1142 .remove = __devexit_p(ethoc_remove),
1143 .suspend = ethoc_suspend,
1144 .resume = ethoc_resume,
1147 .owner = THIS_MODULE,
1149 .of_match_table = ethoc_match,
1154 static int __init ethoc_init(void)
1156 return platform_driver_register(ðoc_driver);
1159 static void __exit ethoc_exit(void)
1161 platform_driver_unregister(ðoc_driver);
1164 module_init(ethoc_init);
1165 module_exit(ethoc_exit);
1167 MODULE_AUTHOR("Thierry Reding <thierry.reding@avionic-design.de>");
1168 MODULE_DESCRIPTION("OpenCores Ethernet MAC driver");
1169 MODULE_LICENSE("GPL v2");