19 #include <linux/module.h>
24 #include <linux/sched.h>
25 #include <linux/kernel.h>
26 #include <linux/slab.h>
27 #include <linux/errno.h>
28 #include <linux/types.h>
31 #include <linux/netdevice.h>
32 #include <linux/if_vlan.h>
34 #include <linux/ethtool.h>
36 #include <linux/mii.h>
50 static int debug_level = 8;
51 static int dumb_switch;
60 #define CPMAC_VERSION "0.5.2"
62 #define CPMAC_SKB_SIZE (ETH_FRAME_LEN + ETH_FCS_LEN + VLAN_HLEN)
63 #define CPMAC_QUEUES 8
66 #define CPMAC_TX_CONTROL 0x0004
67 #define CPMAC_TX_TEARDOWN 0x0008
68 #define CPMAC_RX_CONTROL 0x0014
69 #define CPMAC_RX_TEARDOWN 0x0018
70 #define CPMAC_MBP 0x0100
71 # define MBP_RXPASSCRC 0x40000000
72 # define MBP_RXQOS 0x20000000
73 # define MBP_RXNOCHAIN 0x10000000
74 # define MBP_RXCMF 0x01000000
75 # define MBP_RXSHORT 0x00800000
76 # define MBP_RXCEF 0x00400000
77 # define MBP_RXPROMISC 0x00200000
78 # define MBP_PROMISCCHAN(channel) (((channel) & 0x7) << 16)
79 # define MBP_RXBCAST 0x00002000
80 # define MBP_BCASTCHAN(channel) (((channel) & 0x7) << 8)
81 # define MBP_RXMCAST 0x00000020
82 # define MBP_MCASTCHAN(channel) ((channel) & 0x7)
83 #define CPMAC_UNICAST_ENABLE 0x0104
84 #define CPMAC_UNICAST_CLEAR 0x0108
85 #define CPMAC_MAX_LENGTH 0x010c
86 #define CPMAC_BUFFER_OFFSET 0x0110
87 #define CPMAC_MAC_CONTROL 0x0160
88 # define MAC_TXPTYPE 0x00000200
89 # define MAC_TXPACE 0x00000040
90 # define MAC_MII 0x00000020
91 # define MAC_TXFLOW 0x00000010
92 # define MAC_RXFLOW 0x00000008
93 # define MAC_MTEST 0x00000004
94 # define MAC_LOOPBACK 0x00000002
95 # define MAC_FDX 0x00000001
96 #define CPMAC_MAC_STATUS 0x0164
97 # define MAC_STATUS_QOS 0x00000004
98 # define MAC_STATUS_RXFLOW 0x00000002
99 # define MAC_STATUS_TXFLOW 0x00000001
100 #define CPMAC_TX_INT_ENABLE 0x0178
101 #define CPMAC_TX_INT_CLEAR 0x017c
102 #define CPMAC_MAC_INT_VECTOR 0x0180
103 # define MAC_INT_STATUS 0x00080000
104 # define MAC_INT_HOST 0x00040000
105 # define MAC_INT_RX 0x00020000
106 # define MAC_INT_TX 0x00010000
107 #define CPMAC_MAC_EOI_VECTOR 0x0184
108 #define CPMAC_RX_INT_ENABLE 0x0198
109 #define CPMAC_RX_INT_CLEAR 0x019c
110 #define CPMAC_MAC_INT_ENABLE 0x01a8
111 #define CPMAC_MAC_INT_CLEAR 0x01ac
112 #define CPMAC_MAC_ADDR_LO(channel) (0x01b0 + (channel) * 4)
113 #define CPMAC_MAC_ADDR_MID 0x01d0
114 #define CPMAC_MAC_ADDR_HI 0x01d4
115 #define CPMAC_MAC_HASH_LO 0x01d8
116 #define CPMAC_MAC_HASH_HI 0x01dc
117 #define CPMAC_TX_PTR(channel) (0x0600 + (channel) * 4)
118 #define CPMAC_RX_PTR(channel) (0x0620 + (channel) * 4)
119 #define CPMAC_TX_ACK(channel) (0x0640 + (channel) * 4)
120 #define CPMAC_RX_ACK(channel) (0x0660 + (channel) * 4)
121 #define CPMAC_REG_END 0x0680
126 #define CPMAC_STATS_RX_GOOD 0x0200
127 #define CPMAC_STATS_RX_BCAST 0x0204
128 #define CPMAC_STATS_RX_MCAST 0x0208
129 #define CPMAC_STATS_RX_PAUSE 0x020c
130 #define CPMAC_STATS_RX_CRC 0x0210
131 #define CPMAC_STATS_RX_ALIGN 0x0214
132 #define CPMAC_STATS_RX_OVER 0x0218
133 #define CPMAC_STATS_RX_JABBER 0x021c
134 #define CPMAC_STATS_RX_UNDER 0x0220
135 #define CPMAC_STATS_RX_FRAG 0x0224
136 #define CPMAC_STATS_RX_FILTER 0x0228
137 #define CPMAC_STATS_RX_QOSFILTER 0x022c
138 #define CPMAC_STATS_RX_OCTETS 0x0230
140 #define CPMAC_STATS_TX_GOOD 0x0234
141 #define CPMAC_STATS_TX_BCAST 0x0238
142 #define CPMAC_STATS_TX_MCAST 0x023c
143 #define CPMAC_STATS_TX_PAUSE 0x0240
144 #define CPMAC_STATS_TX_DEFER 0x0244
145 #define CPMAC_STATS_TX_COLLISION 0x0248
146 #define CPMAC_STATS_TX_SINGLECOLL 0x024c
147 #define CPMAC_STATS_TX_MULTICOLL 0x0250
148 #define CPMAC_STATS_TX_EXCESSCOLL 0x0254
149 #define CPMAC_STATS_TX_LATECOLL 0x0258
150 #define CPMAC_STATS_TX_UNDERRUN 0x025c
151 #define CPMAC_STATS_TX_CARRIERSENSE 0x0260
152 #define CPMAC_STATS_TX_OCTETS 0x0264
154 #define cpmac_read(base, reg) (readl((void __iomem *)(base) + (reg)))
155 #define cpmac_write(base, reg, val) (writel(val, (void __iomem *)(base) + \
159 #define CPMAC_MDIO_VERSION 0x0000
160 #define CPMAC_MDIO_CONTROL 0x0004
161 # define MDIOC_IDLE 0x80000000
162 # define MDIOC_ENABLE 0x40000000
163 # define MDIOC_PREAMBLE 0x00100000
164 # define MDIOC_FAULT 0x00080000
165 # define MDIOC_FAULTDETECT 0x00040000
166 # define MDIOC_INTTEST 0x00020000
167 # define MDIOC_CLKDIV(div) ((div) & 0xff)
168 #define CPMAC_MDIO_ALIVE 0x0008
169 #define CPMAC_MDIO_LINK 0x000c
170 #define CPMAC_MDIO_ACCESS(channel) (0x0080 + (channel) * 8)
171 # define MDIO_BUSY 0x80000000
172 # define MDIO_WRITE 0x40000000
173 # define MDIO_REG(reg) (((reg) & 0x1f) << 21)
174 # define MDIO_PHY(phy) (((phy) & 0x1f) << 16)
175 # define MDIO_DATA(data) ((data) & 0xffff)
176 #define CPMAC_MDIO_PHYSEL(channel) (0x0084 + (channel) * 8)
177 # define PHYSEL_LINKSEL 0x00000040
178 # define PHYSEL_LINKINT 0x00000020
187 #define CPMAC_SOP 0x8000
188 #define CPMAC_EOP 0x4000
189 #define CPMAC_OWN 0x2000
190 #define CPMAC_EOQ 0x1000
244 for (i = 0; i <
sizeof(*desc) / 4; i++)
249 static void cpmac_dump_all_desc(
struct net_device *dev)
254 cpmac_dump_desc(dev, dump);
256 }
while (dump != priv->
rx_head);
263 for (i = 0; i < skb->
len; i++) {
288 static int cpmac_mdio_write(
struct mii_bus *bus,
int phy_id,
298 static int cpmac_mdio_reset(
struct mii_bus *bus)
300 struct clk *cpmac_clk;
303 if (IS_ERR(cpmac_clk)) {
315 static struct mii_bus *cpmac_mii;
330 static void cpmac_set_multicast_list(
struct net_device *dev)
355 bit ^= (tmp >> 2) ^ (tmp << 4);
357 bit ^= (tmp >> 4) ^ (tmp << 2);
359 bit ^= (tmp >> 6) ^ tmp;
361 bit ^= (tmp >> 2) ^ (tmp << 4);
363 bit ^= (tmp >> 4) ^ (tmp << 2);
365 bit ^= (tmp >> 6) ^ tmp;
367 hash[bit / 32] |= 1 << (bit % 32);
382 cpmac_dump_desc(priv->
dev, desc);
395 skb_checksum_none_assert(desc->
skb);
396 priv->
dev->stats.rx_packets++;
409 cpmac_dump_skb(priv->
dev, result);
414 "%s: low on skbs, dropping packet\n",
416 priv->
dev->stats.rx_dropped++;
430 int received = 0, processed = 0;
456 " duplicate EOQ: %p and %p\n",
457 priv->
dev->name, restart, desc);
461 restart = desc->
next;
464 skb = cpmac_rx_one(priv, desc);
498 priv->
dev->stats.rx_errors++;
499 priv->
dev->stats.rx_fifo_errors++;
507 "restart rx from a descriptor that's "
509 priv->
dev->name, restart);
520 priv->
dev->name, received);
521 if (processed == 0) {
537 "Resetting hardware\n", priv->
dev->name);
538 cpmac_dump_all_desc(priv->
dev);
547 netif_tx_stop_all_queues(priv->
dev);
548 napi_disable(&priv->
napi);
551 cpmac_hw_stop(priv->
dev);
571 queue = skb_get_queue_mapping(skb);
572 netif_stop_subqueue(dev, queue);
582 spin_lock(&priv->
lock);
583 spin_unlock(&priv->
lock);
595 cpmac_dump_desc(dev, desc);
597 cpmac_dump_skb(dev, skb);
603 static void cpmac_end_xmit(
struct net_device *dev,
int queue)
611 spin_lock(&priv->
lock);
612 dev->
stats.tx_packets++;
613 dev->
stats.tx_bytes += desc->
skb->len;
614 spin_unlock(&priv->
lock);
620 desc->
skb, desc->
skb->len);
624 if (__netif_subqueue_stopped(dev, queue))
625 netif_wake_subqueue(dev, queue);
629 "%s: end_xmit: spurious interrupt\n", dev->
name);
630 if (__netif_subqueue_stopped(dev, queue))
631 netif_wake_subqueue(dev, queue);
635 static void cpmac_hw_stop(
struct net_device *dev)
646 for (i = 0; i < 8; i++) {
658 static void cpmac_hw_start(
struct net_device *dev)
665 for (i = 0; i < 8; i++) {
674 for (i = 0; i < 8; i++)
699 static void cpmac_clear_rx(
struct net_device *dev)
713 cpmac_dump_desc(dev, desc);
715 dev->
stats.rx_dropped++;
720 priv->
rx_head->prev->hw_next = 0;
723 static void cpmac_clear_tx(
struct net_device *dev)
744 cpmac_clear_rx(priv->
dev);
746 cpmac_clear_tx(priv->
dev);
747 cpmac_hw_start(priv->
dev);
751 netif_tx_wake_all_queues(priv->
dev);
755 static void cpmac_check_status(
struct net_device *dev)
760 int rx_channel = (macstatus >> 8) & 7;
761 int rx_code = (macstatus >> 12) & 15;
762 int tx_channel = (macstatus >> 16) & 7;
763 int tx_code = (macstatus >> 20) & 15;
765 if (rx_code || tx_code) {
772 "channel %d (macstatus %08x), resetting\n",
773 dev->
name, rx_code, rx_channel, macstatus);
776 "channel %d (macstatus %08x), resetting\n",
777 dev->
name, tx_code, tx_channel, macstatus);
780 netif_tx_stop_all_queues(dev);
785 cpmac_dump_regs(dev);
797 priv = netdev_priv(dev);
806 cpmac_end_xmit(dev, (status & 7));
809 queue = (status >> 8) & 7;
810 if (napi_schedule_prep(&priv->
napi)) {
819 cpmac_check_status(dev);
824 static void cpmac_tx_timeout(
struct net_device *dev)
828 spin_lock(&priv->
lock);
829 dev->
stats.tx_errors++;
830 spin_unlock(&priv->
lock);
840 netif_tx_wake_all_queues(priv->
dev);
846 if (!(netif_running(dev)))
877 static void cpmac_get_ringparam(
struct net_device *dev,
893 static int cpmac_set_ringparam(
struct net_device *dev,
898 if (netif_running(dev))
904 static void cpmac_get_drvinfo(
struct net_device *dev,
914 static const struct ethtool_ops cpmac_ethtool_ops = {
915 .get_settings = cpmac_get_settings,
916 .set_settings = cpmac_set_settings,
917 .get_drvinfo = cpmac_get_drvinfo,
919 .get_ringparam = cpmac_get_ringparam,
920 .set_ringparam = cpmac_set_ringparam,
923 static void cpmac_adjust_link(
struct net_device *dev)
928 spin_lock(&priv->
lock);
929 if (priv->
phy->link) {
930 netif_tx_start_all_queues(dev);
955 spin_unlock(&priv->
lock);
994 for (i = 0; i <
size; i++)
1012 desc->
next->prev = desc;
1028 cpmac_hw_start(dev);
1030 napi_enable(&priv->
napi);
1041 priv->
rx_head[i].data_mapping,
1058 static int cpmac_stop(
struct net_device *dev)
1064 netif_tx_stop_all_queues(dev);
1067 napi_disable(&priv->
napi);
1072 for (i = 0; i < 8; i++)
1085 priv->
rx_head[i].data_mapping,
1099 .ndo_open = cpmac_open,
1100 .ndo_stop = cpmac_stop,
1101 .ndo_start_xmit = cpmac_start_xmit,
1102 .ndo_tx_timeout = cpmac_tx_timeout,
1103 .ndo_set_rx_mode = cpmac_set_multicast_list,
1104 .ndo_do_ioctl = cpmac_ioctl,
1105 .ndo_set_config = cpmac_config,
1111 static int external_switch;
1122 pdata = pdev->
dev.platform_data;
1124 if (external_switch || dumb_switch) {
1129 if (!(pdata->
phy_mask & (1 << phy_id)))
1131 if (!cpmac_mii->
phy_map[phy_id])
1138 if (phy_id == PHY_MAX_ADDR) {
1139 dev_err(&pdev->
dev,
"no PHY present, falling back "
1140 "to switch on MDIO bus 0\n");
1145 dev = alloc_etherdev_mq(
sizeof(*priv), CPMAC_QUEUES);
1149 platform_set_drvdata(pdev, dev);
1150 priv = netdev_priv(dev);
1170 priv->
msg_enable = netif_msg_init(debug_level, 0xff);
1174 mdio_bus_id, phy_id);
1179 if (IS_ERR(priv->
phy)) {
1183 rc = PTR_ERR(priv->
phy);
1196 "cpmac: device %s (regs: %p, irq: %d, phy: %s, "
1197 "mac: %pM)\n", dev->
name, (
void *)mem->
start, dev->
irq,
1209 struct net_device *dev = platform_get_drvdata(pdev);
1216 .driver.name =
"cpmac",
1218 .probe = cpmac_probe,
1227 cpmac_mii = mdiobus_alloc();
1228 if (cpmac_mii ==
NULL)
1231 cpmac_mii->
name =
"cpmac-mii";
1232 cpmac_mii->
read = cpmac_mdio_read;
1233 cpmac_mii->
write = cpmac_mdio_write;
1234 cpmac_mii->
reset = cpmac_mdio_reset;
1235 cpmac_mii->
irq = mii_irqs;
1239 if (!cpmac_mii->
priv) {
1245 #warning FIXME: unhardcode gpio&reset bits
1252 cpmac_mii->
reset(cpmac_mii);
1254 for (i = 0; i < 300; i++) {
1263 if (mask & (mask - 1)) {
1264 external_switch = 1;
1268 cpmac_mii->
phy_mask = ~(mask | 0x80000000);