11 #define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
13 #include <linux/module.h>
15 #include <linux/kernel.h>
16 #include <linux/types.h>
17 #include <linux/slab.h>
20 #include <linux/netdevice.h>
32 #define RX_BUFFER_SIZE 128
33 #define RX_RING_SIZE 512
34 #define RX_RING_BYTES (sizeof(struct dma_desc) * RX_RING_SIZE)
39 #define TX_RING_SIZE 128
40 #define DEF_TX_RING_PENDING (TX_RING_SIZE - 1)
41 #define TX_RING_BYTES (sizeof(struct dma_desc) * TX_RING_SIZE)
43 #define TX_RING_GAP(bp) \
44 (TX_RING_SIZE - (bp)->tx_pending)
45 #define TX_BUFFS_AVAIL(bp) \
46 (((bp)->tx_tail <= (bp)->tx_head) ? \
47 (bp)->tx_tail + (bp)->tx_pending - (bp)->tx_head : \
48 (bp)->tx_tail - (bp)->tx_head - TX_RING_GAP(bp))
49 #define NEXT_TX(n) (((n) + 1) & (TX_RING_SIZE - 1))
51 #define NEXT_RX(n) (((n) + 1) & (RX_RING_SIZE - 1))
54 #define MACB_TX_WAKEUP_THRESH (TX_RING_SIZE / 4)
56 #define MACB_RX_INT_FLAGS (MACB_BIT(RCOMP) | MACB_BIT(RXUBR) \
59 static void __macb_set_hwaddr(
struct macb *bp)
70 static void __init macb_get_hwaddr(
struct macb *bp)
79 addr[0] = bottom & 0xff;
80 addr[1] = (bottom >> 8) & 0xff;
81 addr[2] = (bottom >> 16) & 0xff;
82 addr[3] = (bottom >> 24) & 0xff;
84 addr[5] = (top >> 8) & 0xff;
86 if (is_valid_ether_addr(addr)) {
87 memcpy(bp->
dev->dev_addr, addr,
sizeof(addr));
89 netdev_info(bp->
dev,
"invalid hw address, using random\n");
90 eth_hw_addr_random(bp->
dev);
94 static int macb_mdio_read(
struct mii_bus *
bus,
int mii_id,
int regnum)
114 static int macb_mdio_write(
struct mii_bus *bus,
int mii_id,
int regnum,
133 static int macb_mdio_reset(
struct mii_bus *bus)
140 struct macb *bp = netdev_priv(dev);
144 int status_change = 0;
179 spin_unlock_irqrestore(&bp->
lock, flags);
184 netdev_info(dev,
"link up (%d/%s)\n",
190 netdev_info(dev,
"link down\n");
196 static int macb_mii_probe(
struct net_device *dev)
198 struct macb *bp = netdev_priv(dev);
204 netdev_err(dev,
"no PHY found\n");
214 netdev_err(dev,
"Could not attach to PHY\n");
231 static int macb_mii_init(
struct macb *bp)
245 bp->
mii_bus->name =
"MACB_mii_bus";
246 bp->
mii_bus->read = &macb_mdio_read;
247 bp->
mii_bus->write = &macb_mdio_write;
248 bp->
mii_bus->reset = &macb_mdio_reset;
253 pdata = bp->
pdev->dev.platform_data;
261 goto err_out_free_mdiobus;
270 goto err_out_free_mdio_irq;
272 if (macb_mii_probe(bp->
dev) != 0) {
273 goto err_out_unregister_bus;
278 err_out_unregister_bus:
280 err_out_free_mdio_irq:
282 err_out_free_mdiobus:
288 static void macb_update_stats(
struct macb *bp)
296 for(; p <
end; p++, reg++)
300 static void macb_tx(
struct macb *bp)
309 netdev_dbg(bp->
dev,
"macb_tx status = %02lx\n", (
unsigned long)status);
313 netdev_err(bp->
dev,
"TX %s, resetting buffers\n",
315 "underrun" :
"retry limit exceeded");
378 bp->
stats.tx_packets++;
385 if (netif_queue_stopped(bp->
dev) &&
387 netif_wake_queue(bp->
dev);
390 static int macb_rx_frame(
struct macb *bp,
unsigned int first_frag,
391 unsigned int last_frag)
400 netdev_dbg(bp->
dev,
"macb_rx_frame frags %u - %u (len %u)\n",
401 first_frag, last_frag, len);
405 bp->
stats.rx_dropped++;
406 for (frag = first_frag; ; frag =
NEXT_RX(frag)) {
408 if (frag == last_frag)
416 skb_checksum_none_assert(skb);
419 for (frag = first_frag; ; frag =
NEXT_RX(frag)) {
422 if (offset + frag_len > len) {
423 BUG_ON(frag != last_frag);
426 skb_copy_to_linear_data_offset(skb, offset,
434 if (frag == last_frag)
440 bp->
stats.rx_packets++;
442 netdev_dbg(bp->
dev,
"received skb of length %u, csum: %08x\n",
450 static void discard_partial_frame(
struct macb *bp,
unsigned int begin,
455 for (frag = begin; frag !=
end; frag =
NEXT_RX(frag))
466 static int macb_rx(
struct macb *bp,
int budget)
469 unsigned int tail = bp->
rx_tail;
472 for (; budget > 0; tail =
NEXT_RX(tail)) {
483 if (first_frag != -1)
484 discard_partial_frame(bp, first_frag, tail);
492 dropped = macb_rx_frame(bp, first_frag, tail);
501 if (first_frag != -1)
521 (
unsigned long)status, budget);
523 work_done = macb_rx(bp, budget);
524 if (work_done < budget) {
542 struct macb *bp = netdev_priv(dev);
550 spin_lock(&bp->
lock);
554 if (
unlikely(!netif_running(dev))) {
569 if (napi_schedule_prep(&bp->
napi)) {
598 netdev_err(dev,
"DMA bus error: HRESP not OK\n");
604 spin_unlock(&bp->
lock);
609 #ifdef CONFIG_NET_POLL_CONTROLLER
614 static void macb_poll_controller(
struct net_device *dev)
619 macb_interrupt(dev->
irq, dev);
626 struct macb *bp = netdev_priv(dev);
628 unsigned int len,
entry;
634 "start_xmit: len %u head %p data %p tail %p end %p\n",
636 skb_tail_pointer(skb), skb_end_pointer(skb));
638 skb->
data, 16,
true);
646 netif_stop_queue(dev);
647 spin_unlock_irqrestore(&bp->
lock, flags);
648 netdev_err(bp->
dev,
"BUG! Tx Ring full when queue awake!\n");
660 netdev_dbg(bp->
dev,
"Mapped skb data %p to DMA addr %08lx\n",
661 skb->
data, (
unsigned long)mapping);
663 ctrl =
MACB_BF(TX_FRMLEN, len);
665 if (entry == (TX_RING_SIZE - 1))
675 skb_tx_timestamp(skb);
680 netif_stop_queue(dev);
682 spin_unlock_irqrestore(&bp->
lock, flags);
687 static void macb_free_consistent(
struct macb *bp)
711 static int macb_alloc_consistent(
struct macb *bp)
715 size = TX_RING_SIZE *
sizeof(
struct ring_info);
726 "Allocated RX ring of %d bytes at %08lx (mapped %p)\n",
735 "Allocated TX ring of %d bytes at %08lx (mapped %p)\n",
744 "Allocated RX buffers of %d bytes at %08lx (mapped %p)\n",
750 macb_free_consistent(bp);
754 static void macb_init_rings(
struct macb *bp)
776 static void macb_reset_hw(
struct macb *bp)
799 static u32 gem_mdc_clk_div(
struct macb *bp)
804 if (pclk_hz <= 20000000)
806 else if (pclk_hz <= 40000000)
808 else if (pclk_hz <= 80000000)
810 else if (pclk_hz <= 120000000)
812 else if (pclk_hz <= 160000000)
820 static u32 macb_mdc_clk_div(
struct macb *bp)
823 unsigned long pclk_hz;
826 return gem_mdc_clk_div(bp);
829 if (pclk_hz <= 20000000)
831 else if (pclk_hz <= 40000000)
833 else if (pclk_hz <= 80000000)
846 static u32 macb_dbw(
struct macb *bp)
848 if (!macb_is_gem(bp))
866 static void macb_configure_dma(
struct macb *bp)
870 if (macb_is_gem(bp)) {
872 dmacfg |=
GEM_BF(RXBS, RX_BUFFER_SIZE / 64);
877 static void macb_init_hw(
struct macb *bp)
882 __macb_set_hwaddr(bp);
884 config = macb_mdc_clk_div(bp);
892 config |= macb_dbw(bp);
895 macb_configure_dma(bp);
950 static inline int hash_bit_value(
int bitnr,
__u8 *addr)
952 if (addr[bitnr / 8] & (1 << (bitnr % 8)))
960 static int hash_get_index(
__u8 *addr)
965 for (j = 0; j < 6; j++) {
966 for (i = 0, bitval = 0; i < 8; i++)
967 bitval ^= hash_bit_value(i*6 + j, addr);
969 hash_index |= (bitval <<
j);
978 static void macb_sethashtable(
struct net_device *dev)
981 unsigned long mc_filter[2];
983 struct macb *bp = netdev_priv(dev);
985 mc_filter[0] = mc_filter[1] = 0;
988 bitnr = hash_get_index(ha->
addr);
989 mc_filter[bitnr >> 5] |= 1 << (bitnr & 31);
999 static void macb_set_rx_mode(
struct net_device *dev)
1002 struct macb *bp = netdev_priv(dev);
1020 macb_sethashtable(dev);
1034 struct macb *bp = netdev_priv(dev);
1046 if (!is_valid_ether_addr(dev->
dev_addr))
1049 err = macb_alloc_consistent(bp);
1051 netdev_err(dev,
"Unable to allocate DMA memory (error %d)\n",
1056 napi_enable(&bp->
napi);
1058 macb_init_rings(bp);
1064 netif_start_queue(dev);
1069 static int macb_close(
struct net_device *dev)
1071 struct macb *bp = netdev_priv(dev);
1072 unsigned long flags;
1074 netif_stop_queue(dev);
1075 napi_disable(&bp->
napi);
1083 spin_unlock_irqrestore(&bp->
lock, flags);
1085 macb_free_consistent(bp);
1090 static void gem_update_stats(
struct macb *bp)
1096 for (; p <
end; p++, reg++)
1105 gem_update_stats(bp);
1140 struct macb *bp = netdev_priv(dev);
1144 if (macb_is_gem(bp))
1145 return gem_get_stats(bp);
1148 macb_update_stats(bp);
1187 struct macb *bp = netdev_priv(dev);
1198 struct macb *bp = netdev_priv(dev);
1207 static void macb_get_drvinfo(
struct net_device *dev,
1210 struct macb *bp = netdev_priv(dev);
1217 static const struct ethtool_ops macb_ethtool_ops = {
1218 .get_settings = macb_get_settings,
1219 .set_settings = macb_set_settings,
1220 .get_drvinfo = macb_get_drvinfo,
1227 struct macb *bp = netdev_priv(dev);
1230 if (!netif_running(dev))
1240 .ndo_open = macb_open,
1241 .ndo_stop = macb_close,
1242 .ndo_start_xmit = macb_start_xmit,
1243 .ndo_set_rx_mode = macb_set_rx_mode,
1244 .ndo_get_stats = macb_get_stats,
1245 .ndo_do_ioctl = macb_ioctl,
1249 #ifdef CONFIG_NET_POLL_CONTROLLER
1250 .ndo_poll_controller = macb_poll_controller,
1254 #if defined(CONFIG_OF)
1257 { .compatible =
"cdns,at91sam9260-macb" },
1258 { .compatible =
"cdns,macb" },
1259 { .compatible =
"cdns,pc302-gem" },
1260 { .compatible =
"cdns,gem" },
1312 dev_err(&pdev->
dev,
"no mmio resource defined\n");
1317 dev = alloc_etherdev(
sizeof(*bp));
1326 bp = netdev_priv(dev);
1333 if (IS_ERR(bp->
pclk)) {
1334 dev_err(&pdev->
dev,
"failed to get macb_clk\n");
1335 goto err_out_free_dev;
1340 if (IS_ERR(bp->
hclk)) {
1342 goto err_out_put_pclk;
1348 dev_err(&pdev->
dev,
"failed to map registers, aborting.\n");
1350 goto err_out_disable_clocks;
1356 dev_err(&pdev->
dev,
"Unable to request IRQ %d (error %d)\n",
1358 goto err_out_iounmap;
1368 config = macb_mdc_clk_div(bp);
1369 config |= macb_dbw(bp);
1372 err = macb_get_hwaddr_dt(bp);
1374 macb_get_hwaddr(bp);
1376 err = macb_get_phy_mode_dt(pdev);
1378 pdata = pdev->
dev.platform_data;
1388 #if defined(CONFIG_ARCH_AT91)
1395 #if defined(CONFIG_ARCH_AT91)
1405 dev_err(&pdev->
dev,
"Cannot register net device, aborting.\n");
1406 goto err_out_free_irq;
1409 if (macb_mii_init(bp) != 0) {
1410 goto err_out_unregister_netdev;
1413 platform_set_drvdata(pdev, dev);
1417 netdev_info(dev,
"Cadence %s at 0x%08lx irq %d (%pM)\n",
1418 macb_is_gem(bp) ?
"GEM" :
"MACB", dev->
base_addr,
1422 netdev_info(dev,
"attached PHY driver [%s] (mii_bus:phy_addr=%s, irq=%d)\n",
1423 phydev->
drv->name, dev_name(&phydev->
dev), phydev->
irq);
1427 err_out_unregister_netdev:
1433 err_out_disable_clocks:
1442 platform_set_drvdata(pdev,
NULL);
1451 dev = platform_get_drvdata(pdev);
1454 bp = netdev_priv(dev);
1468 platform_set_drvdata(pdev,
NULL);
1477 struct net_device *netdev = platform_get_drvdata(pdev);
1478 struct macb *bp = netdev_priv(netdev);
1491 struct net_device *netdev = platform_get_drvdata(pdev);
1492 struct macb *bp = netdev_priv(netdev);
1502 #define macb_suspend NULL
1503 #define macb_resume NULL
1517 static int __init macb_init(
void)
1522 static void __exit macb_exit(
void)