cpmac.c 33 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258
  1. // SPDX-License-Identifier: GPL-2.0+
  2. /*
  3. * Copyright (C) 2006, 2007 Eugene Konev
  4. *
  5. */
  6. #include <linux/module.h>
  7. #include <linux/interrupt.h>
  8. #include <linux/moduleparam.h>
  9. #include <linux/sched.h>
  10. #include <linux/kernel.h>
  11. #include <linux/slab.h>
  12. #include <linux/errno.h>
  13. #include <linux/types.h>
  14. #include <linux/delay.h>
  15. #include <linux/netdevice.h>
  16. #include <linux/if_vlan.h>
  17. #include <linux/etherdevice.h>
  18. #include <linux/ethtool.h>
  19. #include <linux/skbuff.h>
  20. #include <linux/mii.h>
  21. #include <linux/phy.h>
  22. #include <linux/phy_fixed.h>
  23. #include <linux/platform_device.h>
  24. #include <linux/dma-mapping.h>
  25. #include <linux/clk.h>
  26. #include <linux/gpio.h>
  27. #include <linux/atomic.h>
  28. #include <asm/mach-ar7/ar7.h>
  29. MODULE_AUTHOR("Eugene Konev <ejka@imfi.kspu.ru>");
  30. MODULE_DESCRIPTION("TI AR7 ethernet driver (CPMAC)");
  31. MODULE_LICENSE("GPL");
  32. MODULE_ALIAS("platform:cpmac");
  33. static int debug_level = 8;
  34. static int dumb_switch;
  35. /* Next 2 are only used in cpmac_probe, so it's pointless to change them */
  36. module_param(debug_level, int, 0444);
  37. module_param(dumb_switch, int, 0444);
  38. MODULE_PARM_DESC(debug_level, "Number of NETIF_MSG bits to enable");
  39. MODULE_PARM_DESC(dumb_switch, "Assume switch is not connected to MDIO bus");
  40. #define CPMAC_VERSION "0.5.2"
  41. /* frame size + 802.1q tag + FCS size */
  42. #define CPMAC_SKB_SIZE (ETH_FRAME_LEN + ETH_FCS_LEN + VLAN_HLEN)
  43. #define CPMAC_QUEUES 8
  44. /* Ethernet registers */
  45. #define CPMAC_TX_CONTROL 0x0004
  46. #define CPMAC_TX_TEARDOWN 0x0008
  47. #define CPMAC_RX_CONTROL 0x0014
  48. #define CPMAC_RX_TEARDOWN 0x0018
  49. #define CPMAC_MBP 0x0100
  50. #define MBP_RXPASSCRC 0x40000000
  51. #define MBP_RXQOS 0x20000000
  52. #define MBP_RXNOCHAIN 0x10000000
  53. #define MBP_RXCMF 0x01000000
  54. #define MBP_RXSHORT 0x00800000
  55. #define MBP_RXCEF 0x00400000
  56. #define MBP_RXPROMISC 0x00200000
  57. #define MBP_PROMISCCHAN(channel) (((channel) & 0x7) << 16)
  58. #define MBP_RXBCAST 0x00002000
  59. #define MBP_BCASTCHAN(channel) (((channel) & 0x7) << 8)
  60. #define MBP_RXMCAST 0x00000020
  61. #define MBP_MCASTCHAN(channel) ((channel) & 0x7)
  62. #define CPMAC_UNICAST_ENABLE 0x0104
  63. #define CPMAC_UNICAST_CLEAR 0x0108
  64. #define CPMAC_MAX_LENGTH 0x010c
  65. #define CPMAC_BUFFER_OFFSET 0x0110
  66. #define CPMAC_MAC_CONTROL 0x0160
  67. #define MAC_TXPTYPE 0x00000200
  68. #define MAC_TXPACE 0x00000040
  69. #define MAC_MII 0x00000020
  70. #define MAC_TXFLOW 0x00000010
  71. #define MAC_RXFLOW 0x00000008
  72. #define MAC_MTEST 0x00000004
  73. #define MAC_LOOPBACK 0x00000002
  74. #define MAC_FDX 0x00000001
  75. #define CPMAC_MAC_STATUS 0x0164
  76. #define MAC_STATUS_QOS 0x00000004
  77. #define MAC_STATUS_RXFLOW 0x00000002
  78. #define MAC_STATUS_TXFLOW 0x00000001
  79. #define CPMAC_TX_INT_ENABLE 0x0178
  80. #define CPMAC_TX_INT_CLEAR 0x017c
  81. #define CPMAC_MAC_INT_VECTOR 0x0180
  82. #define MAC_INT_STATUS 0x00080000
  83. #define MAC_INT_HOST 0x00040000
  84. #define MAC_INT_RX 0x00020000
  85. #define MAC_INT_TX 0x00010000
  86. #define CPMAC_MAC_EOI_VECTOR 0x0184
  87. #define CPMAC_RX_INT_ENABLE 0x0198
  88. #define CPMAC_RX_INT_CLEAR 0x019c
  89. #define CPMAC_MAC_INT_ENABLE 0x01a8
  90. #define CPMAC_MAC_INT_CLEAR 0x01ac
  91. #define CPMAC_MAC_ADDR_LO(channel) (0x01b0 + (channel) * 4)
  92. #define CPMAC_MAC_ADDR_MID 0x01d0
  93. #define CPMAC_MAC_ADDR_HI 0x01d4
  94. #define CPMAC_MAC_HASH_LO 0x01d8
  95. #define CPMAC_MAC_HASH_HI 0x01dc
  96. #define CPMAC_TX_PTR(channel) (0x0600 + (channel) * 4)
  97. #define CPMAC_RX_PTR(channel) (0x0620 + (channel) * 4)
  98. #define CPMAC_TX_ACK(channel) (0x0640 + (channel) * 4)
  99. #define CPMAC_RX_ACK(channel) (0x0660 + (channel) * 4)
  100. #define CPMAC_REG_END 0x0680
  101. /* Rx/Tx statistics
  102. * TODO: use some of them to fill stats in cpmac_stats()
  103. */
  104. #define CPMAC_STATS_RX_GOOD 0x0200
  105. #define CPMAC_STATS_RX_BCAST 0x0204
  106. #define CPMAC_STATS_RX_MCAST 0x0208
  107. #define CPMAC_STATS_RX_PAUSE 0x020c
  108. #define CPMAC_STATS_RX_CRC 0x0210
  109. #define CPMAC_STATS_RX_ALIGN 0x0214
  110. #define CPMAC_STATS_RX_OVER 0x0218
  111. #define CPMAC_STATS_RX_JABBER 0x021c
  112. #define CPMAC_STATS_RX_UNDER 0x0220
  113. #define CPMAC_STATS_RX_FRAG 0x0224
  114. #define CPMAC_STATS_RX_FILTER 0x0228
  115. #define CPMAC_STATS_RX_QOSFILTER 0x022c
  116. #define CPMAC_STATS_RX_OCTETS 0x0230
  117. #define CPMAC_STATS_TX_GOOD 0x0234
  118. #define CPMAC_STATS_TX_BCAST 0x0238
  119. #define CPMAC_STATS_TX_MCAST 0x023c
  120. #define CPMAC_STATS_TX_PAUSE 0x0240
  121. #define CPMAC_STATS_TX_DEFER 0x0244
  122. #define CPMAC_STATS_TX_COLLISION 0x0248
  123. #define CPMAC_STATS_TX_SINGLECOLL 0x024c
  124. #define CPMAC_STATS_TX_MULTICOLL 0x0250
  125. #define CPMAC_STATS_TX_EXCESSCOLL 0x0254
  126. #define CPMAC_STATS_TX_LATECOLL 0x0258
  127. #define CPMAC_STATS_TX_UNDERRUN 0x025c
  128. #define CPMAC_STATS_TX_CARRIERSENSE 0x0260
  129. #define CPMAC_STATS_TX_OCTETS 0x0264
  130. #define cpmac_read(base, reg) (readl((void __iomem *)(base) + (reg)))
  131. #define cpmac_write(base, reg, val) (writel(val, (void __iomem *)(base) + \
  132. (reg)))
  133. /* MDIO bus */
  134. #define CPMAC_MDIO_VERSION 0x0000
  135. #define CPMAC_MDIO_CONTROL 0x0004
  136. #define MDIOC_IDLE 0x80000000
  137. #define MDIOC_ENABLE 0x40000000
  138. #define MDIOC_PREAMBLE 0x00100000
  139. #define MDIOC_FAULT 0x00080000
  140. #define MDIOC_FAULTDETECT 0x00040000
  141. #define MDIOC_INTTEST 0x00020000
  142. #define MDIOC_CLKDIV(div) ((div) & 0xff)
  143. #define CPMAC_MDIO_ALIVE 0x0008
  144. #define CPMAC_MDIO_LINK 0x000c
  145. #define CPMAC_MDIO_ACCESS(channel) (0x0080 + (channel) * 8)
  146. #define MDIO_BUSY 0x80000000
  147. #define MDIO_WRITE 0x40000000
  148. #define MDIO_REG(reg) (((reg) & 0x1f) << 21)
  149. #define MDIO_PHY(phy) (((phy) & 0x1f) << 16)
  150. #define MDIO_DATA(data) ((data) & 0xffff)
  151. #define CPMAC_MDIO_PHYSEL(channel) (0x0084 + (channel) * 8)
  152. #define PHYSEL_LINKSEL 0x00000040
  153. #define PHYSEL_LINKINT 0x00000020
  154. struct cpmac_desc {
  155. u32 hw_next;
  156. u32 hw_data;
  157. u16 buflen;
  158. u16 bufflags;
  159. u16 datalen;
  160. u16 dataflags;
  161. #define CPMAC_SOP 0x8000
  162. #define CPMAC_EOP 0x4000
  163. #define CPMAC_OWN 0x2000
  164. #define CPMAC_EOQ 0x1000
  165. struct sk_buff *skb;
  166. struct cpmac_desc *next;
  167. struct cpmac_desc *prev;
  168. dma_addr_t mapping;
  169. dma_addr_t data_mapping;
  170. };
  171. struct cpmac_priv {
  172. spinlock_t lock;
  173. spinlock_t rx_lock;
  174. struct cpmac_desc *rx_head;
  175. int ring_size;
  176. struct cpmac_desc *desc_ring;
  177. dma_addr_t dma_ring;
  178. void __iomem *regs;
  179. struct mii_bus *mii_bus;
  180. char phy_name[MII_BUS_ID_SIZE + 3];
  181. int oldlink, oldspeed, oldduplex;
  182. u32 msg_enable;
  183. struct net_device *dev;
  184. struct work_struct reset_work;
  185. struct platform_device *pdev;
  186. struct napi_struct napi;
  187. atomic_t reset_pending;
  188. };
  189. static irqreturn_t cpmac_irq(int, void *);
  190. static void cpmac_hw_start(struct net_device *dev);
  191. static void cpmac_hw_stop(struct net_device *dev);
  192. static int cpmac_stop(struct net_device *dev);
  193. static int cpmac_open(struct net_device *dev);
  194. static void cpmac_dump_regs(struct net_device *dev)
  195. {
  196. int i;
  197. struct cpmac_priv *priv = netdev_priv(dev);
  198. for (i = 0; i < CPMAC_REG_END; i += 4) {
  199. if (i % 16 == 0) {
  200. if (i)
  201. printk("\n");
  202. printk("%s: reg[%p]:", dev->name, priv->regs + i);
  203. }
  204. printk(" %08x", cpmac_read(priv->regs, i));
  205. }
  206. printk("\n");
  207. }
  208. static void cpmac_dump_desc(struct net_device *dev, struct cpmac_desc *desc)
  209. {
  210. int i;
  211. printk("%s: desc[%p]:", dev->name, desc);
  212. for (i = 0; i < sizeof(*desc) / 4; i++)
  213. printk(" %08x", ((u32 *)desc)[i]);
  214. printk("\n");
  215. }
  216. static void cpmac_dump_all_desc(struct net_device *dev)
  217. {
  218. struct cpmac_priv *priv = netdev_priv(dev);
  219. struct cpmac_desc *dump = priv->rx_head;
  220. do {
  221. cpmac_dump_desc(dev, dump);
  222. dump = dump->next;
  223. } while (dump != priv->rx_head);
  224. }
  225. static void cpmac_dump_skb(struct net_device *dev, struct sk_buff *skb)
  226. {
  227. int i;
  228. printk("%s: skb 0x%p, len=%d\n", dev->name, skb, skb->len);
  229. for (i = 0; i < skb->len; i++) {
  230. if (i % 16 == 0) {
  231. if (i)
  232. printk("\n");
  233. printk("%s: data[%p]:", dev->name, skb->data + i);
  234. }
  235. printk(" %02x", ((u8 *)skb->data)[i]);
  236. }
  237. printk("\n");
  238. }
  239. static int cpmac_mdio_read(struct mii_bus *bus, int phy_id, int reg)
  240. {
  241. u32 val;
  242. while (cpmac_read(bus->priv, CPMAC_MDIO_ACCESS(0)) & MDIO_BUSY)
  243. cpu_relax();
  244. cpmac_write(bus->priv, CPMAC_MDIO_ACCESS(0), MDIO_BUSY | MDIO_REG(reg) |
  245. MDIO_PHY(phy_id));
  246. while ((val = cpmac_read(bus->priv, CPMAC_MDIO_ACCESS(0))) & MDIO_BUSY)
  247. cpu_relax();
  248. return MDIO_DATA(val);
  249. }
  250. static int cpmac_mdio_write(struct mii_bus *bus, int phy_id,
  251. int reg, u16 val)
  252. {
  253. while (cpmac_read(bus->priv, CPMAC_MDIO_ACCESS(0)) & MDIO_BUSY)
  254. cpu_relax();
  255. cpmac_write(bus->priv, CPMAC_MDIO_ACCESS(0), MDIO_BUSY | MDIO_WRITE |
  256. MDIO_REG(reg) | MDIO_PHY(phy_id) | MDIO_DATA(val));
  257. return 0;
  258. }
  259. static int cpmac_mdio_reset(struct mii_bus *bus)
  260. {
  261. struct clk *cpmac_clk;
  262. cpmac_clk = clk_get(&bus->dev, "cpmac");
  263. if (IS_ERR(cpmac_clk)) {
  264. pr_err("unable to get cpmac clock\n");
  265. return -1;
  266. }
  267. ar7_device_reset(AR7_RESET_BIT_MDIO);
  268. cpmac_write(bus->priv, CPMAC_MDIO_CONTROL, MDIOC_ENABLE |
  269. MDIOC_CLKDIV(clk_get_rate(cpmac_clk) / 2200000 - 1));
  270. return 0;
  271. }
  272. static struct mii_bus *cpmac_mii;
  273. static void cpmac_set_multicast_list(struct net_device *dev)
  274. {
  275. struct netdev_hw_addr *ha;
  276. u8 tmp;
  277. u32 mbp, bit, hash[2] = { 0, };
  278. struct cpmac_priv *priv = netdev_priv(dev);
  279. mbp = cpmac_read(priv->regs, CPMAC_MBP);
  280. if (dev->flags & IFF_PROMISC) {
  281. cpmac_write(priv->regs, CPMAC_MBP, (mbp & ~MBP_PROMISCCHAN(0)) |
  282. MBP_RXPROMISC);
  283. } else {
  284. cpmac_write(priv->regs, CPMAC_MBP, mbp & ~MBP_RXPROMISC);
  285. if (dev->flags & IFF_ALLMULTI) {
  286. /* enable all multicast mode */
  287. cpmac_write(priv->regs, CPMAC_MAC_HASH_LO, 0xffffffff);
  288. cpmac_write(priv->regs, CPMAC_MAC_HASH_HI, 0xffffffff);
  289. } else {
  290. /* cpmac uses some strange mac address hashing
  291. * (not crc32)
  292. */
  293. netdev_for_each_mc_addr(ha, dev) {
  294. bit = 0;
  295. tmp = ha->addr[0];
  296. bit ^= (tmp >> 2) ^ (tmp << 4);
  297. tmp = ha->addr[1];
  298. bit ^= (tmp >> 4) ^ (tmp << 2);
  299. tmp = ha->addr[2];
  300. bit ^= (tmp >> 6) ^ tmp;
  301. tmp = ha->addr[3];
  302. bit ^= (tmp >> 2) ^ (tmp << 4);
  303. tmp = ha->addr[4];
  304. bit ^= (tmp >> 4) ^ (tmp << 2);
  305. tmp = ha->addr[5];
  306. bit ^= (tmp >> 6) ^ tmp;
  307. bit &= 0x3f;
  308. hash[bit / 32] |= 1 << (bit % 32);
  309. }
  310. cpmac_write(priv->regs, CPMAC_MAC_HASH_LO, hash[0]);
  311. cpmac_write(priv->regs, CPMAC_MAC_HASH_HI, hash[1]);
  312. }
  313. }
  314. }
  315. static struct sk_buff *cpmac_rx_one(struct cpmac_priv *priv,
  316. struct cpmac_desc *desc)
  317. {
  318. struct sk_buff *skb, *result = NULL;
  319. if (unlikely(netif_msg_hw(priv)))
  320. cpmac_dump_desc(priv->dev, desc);
  321. cpmac_write(priv->regs, CPMAC_RX_ACK(0), (u32)desc->mapping);
  322. if (unlikely(!desc->datalen)) {
  323. if (netif_msg_rx_err(priv) && net_ratelimit())
  324. netdev_warn(priv->dev, "rx: spurious interrupt\n");
  325. return NULL;
  326. }
  327. skb = netdev_alloc_skb_ip_align(priv->dev, CPMAC_SKB_SIZE);
  328. if (likely(skb)) {
  329. skb_put(desc->skb, desc->datalen);
  330. desc->skb->protocol = eth_type_trans(desc->skb, priv->dev);
  331. skb_checksum_none_assert(desc->skb);
  332. priv->dev->stats.rx_packets++;
  333. priv->dev->stats.rx_bytes += desc->datalen;
  334. result = desc->skb;
  335. dma_unmap_single(&priv->dev->dev, desc->data_mapping,
  336. CPMAC_SKB_SIZE, DMA_FROM_DEVICE);
  337. desc->skb = skb;
  338. desc->data_mapping = dma_map_single(&priv->dev->dev, skb->data,
  339. CPMAC_SKB_SIZE,
  340. DMA_FROM_DEVICE);
  341. desc->hw_data = (u32)desc->data_mapping;
  342. if (unlikely(netif_msg_pktdata(priv))) {
  343. netdev_dbg(priv->dev, "received packet:\n");
  344. cpmac_dump_skb(priv->dev, result);
  345. }
  346. } else {
  347. if (netif_msg_rx_err(priv) && net_ratelimit())
  348. netdev_warn(priv->dev,
  349. "low on skbs, dropping packet\n");
  350. priv->dev->stats.rx_dropped++;
  351. }
  352. desc->buflen = CPMAC_SKB_SIZE;
  353. desc->dataflags = CPMAC_OWN;
  354. return result;
  355. }
  356. static int cpmac_poll(struct napi_struct *napi, int budget)
  357. {
  358. struct sk_buff *skb;
  359. struct cpmac_desc *desc, *restart;
  360. struct cpmac_priv *priv = container_of(napi, struct cpmac_priv, napi);
  361. int received = 0, processed = 0;
  362. spin_lock(&priv->rx_lock);
  363. if (unlikely(!priv->rx_head)) {
  364. if (netif_msg_rx_err(priv) && net_ratelimit())
  365. netdev_warn(priv->dev, "rx: polling, but no queue\n");
  366. spin_unlock(&priv->rx_lock);
  367. napi_complete(napi);
  368. return 0;
  369. }
  370. desc = priv->rx_head;
  371. restart = NULL;
  372. while (((desc->dataflags & CPMAC_OWN) == 0) && (received < budget)) {
  373. processed++;
  374. if ((desc->dataflags & CPMAC_EOQ) != 0) {
  375. /* The last update to eoq->hw_next didn't happen
  376. * soon enough, and the receiver stopped here.
  377. * Remember this descriptor so we can restart
  378. * the receiver after freeing some space.
  379. */
  380. if (unlikely(restart)) {
  381. if (netif_msg_rx_err(priv))
  382. netdev_err(priv->dev, "poll found a"
  383. " duplicate EOQ: %p and %p\n",
  384. restart, desc);
  385. goto fatal_error;
  386. }
  387. restart = desc->next;
  388. }
  389. skb = cpmac_rx_one(priv, desc);
  390. if (likely(skb)) {
  391. netif_receive_skb(skb);
  392. received++;
  393. }
  394. desc = desc->next;
  395. }
  396. if (desc != priv->rx_head) {
  397. /* We freed some buffers, but not the whole ring,
  398. * add what we did free to the rx list
  399. */
  400. desc->prev->hw_next = (u32)0;
  401. priv->rx_head->prev->hw_next = priv->rx_head->mapping;
  402. }
  403. /* Optimization: If we did not actually process an EOQ (perhaps because
  404. * of quota limits), check to see if the tail of the queue has EOQ set.
  405. * We should immediately restart in that case so that the receiver can
  406. * restart and run in parallel with more packet processing.
  407. * This lets us handle slightly larger bursts before running
  408. * out of ring space (assuming dev->weight < ring_size)
  409. */
  410. if (!restart &&
  411. (priv->rx_head->prev->dataflags & (CPMAC_OWN|CPMAC_EOQ))
  412. == CPMAC_EOQ &&
  413. (priv->rx_head->dataflags & CPMAC_OWN) != 0) {
  414. /* reset EOQ so the poll loop (above) doesn't try to
  415. * restart this when it eventually gets to this descriptor.
  416. */
  417. priv->rx_head->prev->dataflags &= ~CPMAC_EOQ;
  418. restart = priv->rx_head;
  419. }
  420. if (restart) {
  421. priv->dev->stats.rx_errors++;
  422. priv->dev->stats.rx_fifo_errors++;
  423. if (netif_msg_rx_err(priv) && net_ratelimit())
  424. netdev_warn(priv->dev, "rx dma ring overrun\n");
  425. if (unlikely((restart->dataflags & CPMAC_OWN) == 0)) {
  426. if (netif_msg_drv(priv))
  427. netdev_err(priv->dev, "cpmac_poll is trying "
  428. "to restart rx from a descriptor "
  429. "that's not free: %p\n", restart);
  430. goto fatal_error;
  431. }
  432. cpmac_write(priv->regs, CPMAC_RX_PTR(0), restart->mapping);
  433. }
  434. priv->rx_head = desc;
  435. spin_unlock(&priv->rx_lock);
  436. if (unlikely(netif_msg_rx_status(priv)))
  437. netdev_dbg(priv->dev, "poll processed %d packets\n", received);
  438. if (processed == 0) {
  439. /* we ran out of packets to read,
  440. * revert to interrupt-driven mode
  441. */
  442. napi_complete(napi);
  443. cpmac_write(priv->regs, CPMAC_RX_INT_ENABLE, 1);
  444. return 0;
  445. }
  446. return 1;
  447. fatal_error:
  448. /* Something went horribly wrong.
  449. * Reset hardware to try to recover rather than wedging.
  450. */
  451. if (netif_msg_drv(priv)) {
  452. netdev_err(priv->dev, "cpmac_poll is confused. "
  453. "Resetting hardware\n");
  454. cpmac_dump_all_desc(priv->dev);
  455. netdev_dbg(priv->dev, "RX_PTR(0)=0x%08x RX_ACK(0)=0x%08x\n",
  456. cpmac_read(priv->regs, CPMAC_RX_PTR(0)),
  457. cpmac_read(priv->regs, CPMAC_RX_ACK(0)));
  458. }
  459. spin_unlock(&priv->rx_lock);
  460. napi_complete(napi);
  461. netif_tx_stop_all_queues(priv->dev);
  462. napi_disable(&priv->napi);
  463. atomic_inc(&priv->reset_pending);
  464. cpmac_hw_stop(priv->dev);
  465. if (!schedule_work(&priv->reset_work))
  466. atomic_dec(&priv->reset_pending);
  467. return 0;
  468. }
  469. static int cpmac_start_xmit(struct sk_buff *skb, struct net_device *dev)
  470. {
  471. int queue;
  472. unsigned int len;
  473. struct cpmac_desc *desc;
  474. struct cpmac_priv *priv = netdev_priv(dev);
  475. if (unlikely(atomic_read(&priv->reset_pending)))
  476. return NETDEV_TX_BUSY;
  477. if (unlikely(skb_padto(skb, ETH_ZLEN)))
  478. return NETDEV_TX_OK;
  479. len = max_t(unsigned int, skb->len, ETH_ZLEN);
  480. queue = skb_get_queue_mapping(skb);
  481. netif_stop_subqueue(dev, queue);
  482. desc = &priv->desc_ring[queue];
  483. if (unlikely(desc->dataflags & CPMAC_OWN)) {
  484. if (netif_msg_tx_err(priv) && net_ratelimit())
  485. netdev_warn(dev, "tx dma ring full\n");
  486. return NETDEV_TX_BUSY;
  487. }
  488. spin_lock(&priv->lock);
  489. spin_unlock(&priv->lock);
  490. desc->dataflags = CPMAC_SOP | CPMAC_EOP | CPMAC_OWN;
  491. desc->skb = skb;
  492. desc->data_mapping = dma_map_single(&dev->dev, skb->data, len,
  493. DMA_TO_DEVICE);
  494. desc->hw_data = (u32)desc->data_mapping;
  495. desc->datalen = len;
  496. desc->buflen = len;
  497. if (unlikely(netif_msg_tx_queued(priv)))
  498. netdev_dbg(dev, "sending 0x%p, len=%d\n", skb, skb->len);
  499. if (unlikely(netif_msg_hw(priv)))
  500. cpmac_dump_desc(dev, desc);
  501. if (unlikely(netif_msg_pktdata(priv)))
  502. cpmac_dump_skb(dev, skb);
  503. cpmac_write(priv->regs, CPMAC_TX_PTR(queue), (u32)desc->mapping);
  504. return NETDEV_TX_OK;
  505. }
  506. static void cpmac_end_xmit(struct net_device *dev, int queue)
  507. {
  508. struct cpmac_desc *desc;
  509. struct cpmac_priv *priv = netdev_priv(dev);
  510. desc = &priv->desc_ring[queue];
  511. cpmac_write(priv->regs, CPMAC_TX_ACK(queue), (u32)desc->mapping);
  512. if (likely(desc->skb)) {
  513. spin_lock(&priv->lock);
  514. dev->stats.tx_packets++;
  515. dev->stats.tx_bytes += desc->skb->len;
  516. spin_unlock(&priv->lock);
  517. dma_unmap_single(&dev->dev, desc->data_mapping, desc->skb->len,
  518. DMA_TO_DEVICE);
  519. if (unlikely(netif_msg_tx_done(priv)))
  520. netdev_dbg(dev, "sent 0x%p, len=%d\n",
  521. desc->skb, desc->skb->len);
  522. dev_consume_skb_irq(desc->skb);
  523. desc->skb = NULL;
  524. if (__netif_subqueue_stopped(dev, queue))
  525. netif_wake_subqueue(dev, queue);
  526. } else {
  527. if (netif_msg_tx_err(priv) && net_ratelimit())
  528. netdev_warn(dev, "end_xmit: spurious interrupt\n");
  529. if (__netif_subqueue_stopped(dev, queue))
  530. netif_wake_subqueue(dev, queue);
  531. }
  532. }
  533. static void cpmac_hw_stop(struct net_device *dev)
  534. {
  535. int i;
  536. struct cpmac_priv *priv = netdev_priv(dev);
  537. struct plat_cpmac_data *pdata = dev_get_platdata(&priv->pdev->dev);
  538. ar7_device_reset(pdata->reset_bit);
  539. cpmac_write(priv->regs, CPMAC_RX_CONTROL,
  540. cpmac_read(priv->regs, CPMAC_RX_CONTROL) & ~1);
  541. cpmac_write(priv->regs, CPMAC_TX_CONTROL,
  542. cpmac_read(priv->regs, CPMAC_TX_CONTROL) & ~1);
  543. for (i = 0; i < 8; i++) {
  544. cpmac_write(priv->regs, CPMAC_TX_PTR(i), 0);
  545. cpmac_write(priv->regs, CPMAC_RX_PTR(i), 0);
  546. }
  547. cpmac_write(priv->regs, CPMAC_UNICAST_CLEAR, 0xff);
  548. cpmac_write(priv->regs, CPMAC_RX_INT_CLEAR, 0xff);
  549. cpmac_write(priv->regs, CPMAC_TX_INT_CLEAR, 0xff);
  550. cpmac_write(priv->regs, CPMAC_MAC_INT_CLEAR, 0xff);
  551. cpmac_write(priv->regs, CPMAC_MAC_CONTROL,
  552. cpmac_read(priv->regs, CPMAC_MAC_CONTROL) & ~MAC_MII);
  553. }
  554. static void cpmac_hw_start(struct net_device *dev)
  555. {
  556. int i;
  557. struct cpmac_priv *priv = netdev_priv(dev);
  558. struct plat_cpmac_data *pdata = dev_get_platdata(&priv->pdev->dev);
  559. ar7_device_reset(pdata->reset_bit);
  560. for (i = 0; i < 8; i++) {
  561. cpmac_write(priv->regs, CPMAC_TX_PTR(i), 0);
  562. cpmac_write(priv->regs, CPMAC_RX_PTR(i), 0);
  563. }
  564. cpmac_write(priv->regs, CPMAC_RX_PTR(0), priv->rx_head->mapping);
  565. cpmac_write(priv->regs, CPMAC_MBP, MBP_RXSHORT | MBP_RXBCAST |
  566. MBP_RXMCAST);
  567. cpmac_write(priv->regs, CPMAC_BUFFER_OFFSET, 0);
  568. for (i = 0; i < 8; i++)
  569. cpmac_write(priv->regs, CPMAC_MAC_ADDR_LO(i), dev->dev_addr[5]);
  570. cpmac_write(priv->regs, CPMAC_MAC_ADDR_MID, dev->dev_addr[4]);
  571. cpmac_write(priv->regs, CPMAC_MAC_ADDR_HI, dev->dev_addr[0] |
  572. (dev->dev_addr[1] << 8) | (dev->dev_addr[2] << 16) |
  573. (dev->dev_addr[3] << 24));
  574. cpmac_write(priv->regs, CPMAC_MAX_LENGTH, CPMAC_SKB_SIZE);
  575. cpmac_write(priv->regs, CPMAC_UNICAST_CLEAR, 0xff);
  576. cpmac_write(priv->regs, CPMAC_RX_INT_CLEAR, 0xff);
  577. cpmac_write(priv->regs, CPMAC_TX_INT_CLEAR, 0xff);
  578. cpmac_write(priv->regs, CPMAC_MAC_INT_CLEAR, 0xff);
  579. cpmac_write(priv->regs, CPMAC_UNICAST_ENABLE, 1);
  580. cpmac_write(priv->regs, CPMAC_RX_INT_ENABLE, 1);
  581. cpmac_write(priv->regs, CPMAC_TX_INT_ENABLE, 0xff);
  582. cpmac_write(priv->regs, CPMAC_MAC_INT_ENABLE, 3);
  583. cpmac_write(priv->regs, CPMAC_RX_CONTROL,
  584. cpmac_read(priv->regs, CPMAC_RX_CONTROL) | 1);
  585. cpmac_write(priv->regs, CPMAC_TX_CONTROL,
  586. cpmac_read(priv->regs, CPMAC_TX_CONTROL) | 1);
  587. cpmac_write(priv->regs, CPMAC_MAC_CONTROL,
  588. cpmac_read(priv->regs, CPMAC_MAC_CONTROL) | MAC_MII |
  589. MAC_FDX);
  590. }
  591. static void cpmac_clear_rx(struct net_device *dev)
  592. {
  593. struct cpmac_priv *priv = netdev_priv(dev);
  594. struct cpmac_desc *desc;
  595. int i;
  596. if (unlikely(!priv->rx_head))
  597. return;
  598. desc = priv->rx_head;
  599. for (i = 0; i < priv->ring_size; i++) {
  600. if ((desc->dataflags & CPMAC_OWN) == 0) {
  601. if (netif_msg_rx_err(priv) && net_ratelimit())
  602. netdev_warn(dev, "packet dropped\n");
  603. if (unlikely(netif_msg_hw(priv)))
  604. cpmac_dump_desc(dev, desc);
  605. desc->dataflags = CPMAC_OWN;
  606. dev->stats.rx_dropped++;
  607. }
  608. desc->hw_next = desc->next->mapping;
  609. desc = desc->next;
  610. }
  611. priv->rx_head->prev->hw_next = 0;
  612. }
  613. static void cpmac_clear_tx(struct net_device *dev)
  614. {
  615. struct cpmac_priv *priv = netdev_priv(dev);
  616. int i;
  617. if (unlikely(!priv->desc_ring))
  618. return;
  619. for (i = 0; i < CPMAC_QUEUES; i++) {
  620. priv->desc_ring[i].dataflags = 0;
  621. if (priv->desc_ring[i].skb) {
  622. dev_kfree_skb_any(priv->desc_ring[i].skb);
  623. priv->desc_ring[i].skb = NULL;
  624. }
  625. }
  626. }
  627. static void cpmac_hw_error(struct work_struct *work)
  628. {
  629. struct cpmac_priv *priv =
  630. container_of(work, struct cpmac_priv, reset_work);
  631. spin_lock(&priv->rx_lock);
  632. cpmac_clear_rx(priv->dev);
  633. spin_unlock(&priv->rx_lock);
  634. cpmac_clear_tx(priv->dev);
  635. cpmac_hw_start(priv->dev);
  636. barrier();
  637. atomic_dec(&priv->reset_pending);
  638. netif_tx_wake_all_queues(priv->dev);
  639. cpmac_write(priv->regs, CPMAC_MAC_INT_ENABLE, 3);
  640. }
  641. static void cpmac_check_status(struct net_device *dev)
  642. {
  643. struct cpmac_priv *priv = netdev_priv(dev);
  644. u32 macstatus = cpmac_read(priv->regs, CPMAC_MAC_STATUS);
  645. int rx_channel = (macstatus >> 8) & 7;
  646. int rx_code = (macstatus >> 12) & 15;
  647. int tx_channel = (macstatus >> 16) & 7;
  648. int tx_code = (macstatus >> 20) & 15;
  649. if (rx_code || tx_code) {
  650. if (netif_msg_drv(priv) && net_ratelimit()) {
  651. /* Can't find any documentation on what these
  652. * error codes actually are. So just log them and hope..
  653. */
  654. if (rx_code)
  655. netdev_warn(dev, "host error %d on rx "
  656. "channel %d (macstatus %08x), resetting\n",
  657. rx_code, rx_channel, macstatus);
  658. if (tx_code)
  659. netdev_warn(dev, "host error %d on tx "
  660. "channel %d (macstatus %08x), resetting\n",
  661. tx_code, tx_channel, macstatus);
  662. }
  663. netif_tx_stop_all_queues(dev);
  664. cpmac_hw_stop(dev);
  665. if (schedule_work(&priv->reset_work))
  666. atomic_inc(&priv->reset_pending);
  667. if (unlikely(netif_msg_hw(priv)))
  668. cpmac_dump_regs(dev);
  669. }
  670. cpmac_write(priv->regs, CPMAC_MAC_INT_CLEAR, 0xff);
  671. }
  672. static irqreturn_t cpmac_irq(int irq, void *dev_id)
  673. {
  674. struct net_device *dev = dev_id;
  675. struct cpmac_priv *priv;
  676. int queue;
  677. u32 status;
  678. priv = netdev_priv(dev);
  679. status = cpmac_read(priv->regs, CPMAC_MAC_INT_VECTOR);
  680. if (unlikely(netif_msg_intr(priv)))
  681. netdev_dbg(dev, "interrupt status: 0x%08x\n", status);
  682. if (status & MAC_INT_TX)
  683. cpmac_end_xmit(dev, (status & 7));
  684. if (status & MAC_INT_RX) {
  685. queue = (status >> 8) & 7;
  686. if (napi_schedule_prep(&priv->napi)) {
  687. cpmac_write(priv->regs, CPMAC_RX_INT_CLEAR, 1 << queue);
  688. __napi_schedule(&priv->napi);
  689. }
  690. }
  691. cpmac_write(priv->regs, CPMAC_MAC_EOI_VECTOR, 0);
  692. if (unlikely(status & (MAC_INT_HOST | MAC_INT_STATUS)))
  693. cpmac_check_status(dev);
  694. return IRQ_HANDLED;
  695. }
  696. static void cpmac_tx_timeout(struct net_device *dev)
  697. {
  698. struct cpmac_priv *priv = netdev_priv(dev);
  699. spin_lock(&priv->lock);
  700. dev->stats.tx_errors++;
  701. spin_unlock(&priv->lock);
  702. if (netif_msg_tx_err(priv) && net_ratelimit())
  703. netdev_warn(dev, "transmit timeout\n");
  704. atomic_inc(&priv->reset_pending);
  705. barrier();
  706. cpmac_clear_tx(dev);
  707. barrier();
  708. atomic_dec(&priv->reset_pending);
  709. netif_tx_wake_all_queues(priv->dev);
  710. }
  711. static int cpmac_ioctl(struct net_device *dev, struct ifreq *ifr, int cmd)
  712. {
  713. if (!(netif_running(dev)))
  714. return -EINVAL;
  715. if (!dev->phydev)
  716. return -EINVAL;
  717. return phy_mii_ioctl(dev->phydev, ifr, cmd);
  718. }
  719. static void cpmac_get_ringparam(struct net_device *dev,
  720. struct ethtool_ringparam *ring)
  721. {
  722. struct cpmac_priv *priv = netdev_priv(dev);
  723. ring->rx_max_pending = 1024;
  724. ring->rx_mini_max_pending = 1;
  725. ring->rx_jumbo_max_pending = 1;
  726. ring->tx_max_pending = 1;
  727. ring->rx_pending = priv->ring_size;
  728. ring->rx_mini_pending = 1;
  729. ring->rx_jumbo_pending = 1;
  730. ring->tx_pending = 1;
  731. }
  732. static int cpmac_set_ringparam(struct net_device *dev,
  733. struct ethtool_ringparam *ring)
  734. {
  735. struct cpmac_priv *priv = netdev_priv(dev);
  736. if (netif_running(dev))
  737. return -EBUSY;
  738. priv->ring_size = ring->rx_pending;
  739. return 0;
  740. }
  741. static void cpmac_get_drvinfo(struct net_device *dev,
  742. struct ethtool_drvinfo *info)
  743. {
  744. strlcpy(info->driver, "cpmac", sizeof(info->driver));
  745. strlcpy(info->version, CPMAC_VERSION, sizeof(info->version));
  746. snprintf(info->bus_info, sizeof(info->bus_info), "%s", "cpmac");
  747. }
  748. static const struct ethtool_ops cpmac_ethtool_ops = {
  749. .get_drvinfo = cpmac_get_drvinfo,
  750. .get_link = ethtool_op_get_link,
  751. .get_ringparam = cpmac_get_ringparam,
  752. .set_ringparam = cpmac_set_ringparam,
  753. .get_link_ksettings = phy_ethtool_get_link_ksettings,
  754. .set_link_ksettings = phy_ethtool_set_link_ksettings,
  755. };
  756. static void cpmac_adjust_link(struct net_device *dev)
  757. {
  758. struct cpmac_priv *priv = netdev_priv(dev);
  759. int new_state = 0;
  760. spin_lock(&priv->lock);
  761. if (dev->phydev->link) {
  762. netif_tx_start_all_queues(dev);
  763. if (dev->phydev->duplex != priv->oldduplex) {
  764. new_state = 1;
  765. priv->oldduplex = dev->phydev->duplex;
  766. }
  767. if (dev->phydev->speed != priv->oldspeed) {
  768. new_state = 1;
  769. priv->oldspeed = dev->phydev->speed;
  770. }
  771. if (!priv->oldlink) {
  772. new_state = 1;
  773. priv->oldlink = 1;
  774. }
  775. } else if (priv->oldlink) {
  776. new_state = 1;
  777. priv->oldlink = 0;
  778. priv->oldspeed = 0;
  779. priv->oldduplex = -1;
  780. }
  781. if (new_state && netif_msg_link(priv) && net_ratelimit())
  782. phy_print_status(dev->phydev);
  783. spin_unlock(&priv->lock);
  784. }
  785. static int cpmac_open(struct net_device *dev)
  786. {
  787. int i, size, res;
  788. struct cpmac_priv *priv = netdev_priv(dev);
  789. struct resource *mem;
  790. struct cpmac_desc *desc;
  791. struct sk_buff *skb;
  792. mem = platform_get_resource_byname(priv->pdev, IORESOURCE_MEM, "regs");
  793. if (!request_mem_region(mem->start, resource_size(mem), dev->name)) {
  794. if (netif_msg_drv(priv))
  795. netdev_err(dev, "failed to request registers\n");
  796. res = -ENXIO;
  797. goto fail_reserve;
  798. }
  799. priv->regs = ioremap(mem->start, resource_size(mem));
  800. if (!priv->regs) {
  801. if (netif_msg_drv(priv))
  802. netdev_err(dev, "failed to remap registers\n");
  803. res = -ENXIO;
  804. goto fail_remap;
  805. }
  806. size = priv->ring_size + CPMAC_QUEUES;
  807. priv->desc_ring = dma_alloc_coherent(&dev->dev,
  808. sizeof(struct cpmac_desc) * size,
  809. &priv->dma_ring,
  810. GFP_KERNEL);
  811. if (!priv->desc_ring) {
  812. res = -ENOMEM;
  813. goto fail_alloc;
  814. }
  815. for (i = 0; i < size; i++)
  816. priv->desc_ring[i].mapping = priv->dma_ring + sizeof(*desc) * i;
  817. priv->rx_head = &priv->desc_ring[CPMAC_QUEUES];
  818. for (i = 0, desc = priv->rx_head; i < priv->ring_size; i++, desc++) {
  819. skb = netdev_alloc_skb_ip_align(dev, CPMAC_SKB_SIZE);
  820. if (unlikely(!skb)) {
  821. res = -ENOMEM;
  822. goto fail_desc;
  823. }
  824. desc->skb = skb;
  825. desc->data_mapping = dma_map_single(&dev->dev, skb->data,
  826. CPMAC_SKB_SIZE,
  827. DMA_FROM_DEVICE);
  828. desc->hw_data = (u32)desc->data_mapping;
  829. desc->buflen = CPMAC_SKB_SIZE;
  830. desc->dataflags = CPMAC_OWN;
  831. desc->next = &priv->rx_head[(i + 1) % priv->ring_size];
  832. desc->next->prev = desc;
  833. desc->hw_next = (u32)desc->next->mapping;
  834. }
  835. priv->rx_head->prev->hw_next = (u32)0;
  836. res = request_irq(dev->irq, cpmac_irq, IRQF_SHARED, dev->name, dev);
  837. if (res) {
  838. if (netif_msg_drv(priv))
  839. netdev_err(dev, "failed to obtain irq\n");
  840. goto fail_irq;
  841. }
  842. atomic_set(&priv->reset_pending, 0);
  843. INIT_WORK(&priv->reset_work, cpmac_hw_error);
  844. cpmac_hw_start(dev);
  845. napi_enable(&priv->napi);
  846. phy_start(dev->phydev);
  847. return 0;
  848. fail_irq:
  849. fail_desc:
  850. for (i = 0; i < priv->ring_size; i++) {
  851. if (priv->rx_head[i].skb) {
  852. dma_unmap_single(&dev->dev,
  853. priv->rx_head[i].data_mapping,
  854. CPMAC_SKB_SIZE,
  855. DMA_FROM_DEVICE);
  856. kfree_skb(priv->rx_head[i].skb);
  857. }
  858. }
  859. dma_free_coherent(&dev->dev, sizeof(struct cpmac_desc) * size,
  860. priv->desc_ring, priv->dma_ring);
  861. fail_alloc:
  862. iounmap(priv->regs);
  863. fail_remap:
  864. release_mem_region(mem->start, resource_size(mem));
  865. fail_reserve:
  866. return res;
  867. }
  868. static int cpmac_stop(struct net_device *dev)
  869. {
  870. int i;
  871. struct cpmac_priv *priv = netdev_priv(dev);
  872. struct resource *mem;
  873. netif_tx_stop_all_queues(dev);
  874. cancel_work_sync(&priv->reset_work);
  875. napi_disable(&priv->napi);
  876. phy_stop(dev->phydev);
  877. cpmac_hw_stop(dev);
  878. for (i = 0; i < 8; i++)
  879. cpmac_write(priv->regs, CPMAC_TX_PTR(i), 0);
  880. cpmac_write(priv->regs, CPMAC_RX_PTR(0), 0);
  881. cpmac_write(priv->regs, CPMAC_MBP, 0);
  882. free_irq(dev->irq, dev);
  883. iounmap(priv->regs);
  884. mem = platform_get_resource_byname(priv->pdev, IORESOURCE_MEM, "regs");
  885. release_mem_region(mem->start, resource_size(mem));
  886. priv->rx_head = &priv->desc_ring[CPMAC_QUEUES];
  887. for (i = 0; i < priv->ring_size; i++) {
  888. if (priv->rx_head[i].skb) {
  889. dma_unmap_single(&dev->dev,
  890. priv->rx_head[i].data_mapping,
  891. CPMAC_SKB_SIZE,
  892. DMA_FROM_DEVICE);
  893. kfree_skb(priv->rx_head[i].skb);
  894. }
  895. }
  896. dma_free_coherent(&dev->dev, sizeof(struct cpmac_desc) *
  897. (CPMAC_QUEUES + priv->ring_size),
  898. priv->desc_ring, priv->dma_ring);
  899. return 0;
  900. }
  901. static const struct net_device_ops cpmac_netdev_ops = {
  902. .ndo_open = cpmac_open,
  903. .ndo_stop = cpmac_stop,
  904. .ndo_start_xmit = cpmac_start_xmit,
  905. .ndo_tx_timeout = cpmac_tx_timeout,
  906. .ndo_set_rx_mode = cpmac_set_multicast_list,
  907. .ndo_do_ioctl = cpmac_ioctl,
  908. .ndo_validate_addr = eth_validate_addr,
  909. .ndo_set_mac_address = eth_mac_addr,
  910. };
  911. static int external_switch;
  912. static int cpmac_probe(struct platform_device *pdev)
  913. {
  914. int rc, phy_id;
  915. char mdio_bus_id[MII_BUS_ID_SIZE];
  916. struct resource *mem;
  917. struct cpmac_priv *priv;
  918. struct net_device *dev;
  919. struct plat_cpmac_data *pdata;
  920. struct phy_device *phydev = NULL;
  921. pdata = dev_get_platdata(&pdev->dev);
  922. if (external_switch || dumb_switch) {
  923. strncpy(mdio_bus_id, "fixed-0", MII_BUS_ID_SIZE); /* fixed phys bus */
  924. phy_id = pdev->id;
  925. } else {
  926. for (phy_id = 0; phy_id < PHY_MAX_ADDR; phy_id++) {
  927. if (!(pdata->phy_mask & (1 << phy_id)))
  928. continue;
  929. if (!mdiobus_get_phy(cpmac_mii, phy_id))
  930. continue;
  931. strncpy(mdio_bus_id, cpmac_mii->id, MII_BUS_ID_SIZE);
  932. break;
  933. }
  934. }
  935. if (phy_id == PHY_MAX_ADDR) {
  936. dev_err(&pdev->dev, "no PHY present, falling back "
  937. "to switch on MDIO bus 0\n");
  938. strncpy(mdio_bus_id, "fixed-0", MII_BUS_ID_SIZE); /* fixed phys bus */
  939. phy_id = pdev->id;
  940. }
  941. mdio_bus_id[sizeof(mdio_bus_id) - 1] = '\0';
  942. dev = alloc_etherdev_mq(sizeof(*priv), CPMAC_QUEUES);
  943. if (!dev)
  944. return -ENOMEM;
  945. SET_NETDEV_DEV(dev, &pdev->dev);
  946. platform_set_drvdata(pdev, dev);
  947. priv = netdev_priv(dev);
  948. priv->pdev = pdev;
  949. mem = platform_get_resource_byname(pdev, IORESOURCE_MEM, "regs");
  950. if (!mem) {
  951. rc = -ENODEV;
  952. goto fail;
  953. }
  954. dev->irq = platform_get_irq_byname(pdev, "irq");
  955. dev->netdev_ops = &cpmac_netdev_ops;
  956. dev->ethtool_ops = &cpmac_ethtool_ops;
  957. netif_napi_add(dev, &priv->napi, cpmac_poll, 64);
  958. spin_lock_init(&priv->lock);
  959. spin_lock_init(&priv->rx_lock);
  960. priv->dev = dev;
  961. priv->ring_size = 64;
  962. priv->msg_enable = netif_msg_init(debug_level, 0xff);
  963. memcpy(dev->dev_addr, pdata->dev_addr, sizeof(pdata->dev_addr));
  964. snprintf(priv->phy_name, MII_BUS_ID_SIZE, PHY_ID_FMT,
  965. mdio_bus_id, phy_id);
  966. phydev = phy_connect(dev, priv->phy_name, cpmac_adjust_link,
  967. PHY_INTERFACE_MODE_MII);
  968. if (IS_ERR(phydev)) {
  969. if (netif_msg_drv(priv))
  970. dev_err(&pdev->dev, "Could not attach to PHY\n");
  971. rc = PTR_ERR(phydev);
  972. goto fail;
  973. }
  974. rc = register_netdev(dev);
  975. if (rc) {
  976. dev_err(&pdev->dev, "Could not register net device\n");
  977. goto fail;
  978. }
  979. if (netif_msg_probe(priv)) {
  980. dev_info(&pdev->dev, "regs: %p, irq: %d, phy: %s, "
  981. "mac: %pM\n", (void *)mem->start, dev->irq,
  982. priv->phy_name, dev->dev_addr);
  983. }
  984. return 0;
  985. fail:
  986. free_netdev(dev);
  987. return rc;
  988. }
  989. static int cpmac_remove(struct platform_device *pdev)
  990. {
  991. struct net_device *dev = platform_get_drvdata(pdev);
  992. unregister_netdev(dev);
  993. free_netdev(dev);
  994. return 0;
  995. }
  996. static struct platform_driver cpmac_driver = {
  997. .driver = {
  998. .name = "cpmac",
  999. },
  1000. .probe = cpmac_probe,
  1001. .remove = cpmac_remove,
  1002. };
  1003. int cpmac_init(void)
  1004. {
  1005. u32 mask;
  1006. int i, res;
  1007. cpmac_mii = mdiobus_alloc();
  1008. if (cpmac_mii == NULL)
  1009. return -ENOMEM;
  1010. cpmac_mii->name = "cpmac-mii";
  1011. cpmac_mii->read = cpmac_mdio_read;
  1012. cpmac_mii->write = cpmac_mdio_write;
  1013. cpmac_mii->reset = cpmac_mdio_reset;
  1014. cpmac_mii->priv = ioremap(AR7_REGS_MDIO, 256);
  1015. if (!cpmac_mii->priv) {
  1016. pr_err("Can't ioremap mdio registers\n");
  1017. res = -ENXIO;
  1018. goto fail_alloc;
  1019. }
  1020. /* FIXME: unhardcode gpio&reset bits */
  1021. ar7_gpio_disable(26);
  1022. ar7_gpio_disable(27);
  1023. ar7_device_reset(AR7_RESET_BIT_CPMAC_LO);
  1024. ar7_device_reset(AR7_RESET_BIT_CPMAC_HI);
  1025. ar7_device_reset(AR7_RESET_BIT_EPHY);
  1026. cpmac_mii->reset(cpmac_mii);
  1027. for (i = 0; i < 300; i++) {
  1028. mask = cpmac_read(cpmac_mii->priv, CPMAC_MDIO_ALIVE);
  1029. if (mask)
  1030. break;
  1031. else
  1032. msleep(10);
  1033. }
  1034. mask &= 0x7fffffff;
  1035. if (mask & (mask - 1)) {
  1036. external_switch = 1;
  1037. mask = 0;
  1038. }
  1039. cpmac_mii->phy_mask = ~(mask | 0x80000000);
  1040. snprintf(cpmac_mii->id, MII_BUS_ID_SIZE, "cpmac-1");
  1041. res = mdiobus_register(cpmac_mii);
  1042. if (res)
  1043. goto fail_mii;
  1044. res = platform_driver_register(&cpmac_driver);
  1045. if (res)
  1046. goto fail_cpmac;
  1047. return 0;
  1048. fail_cpmac:
  1049. mdiobus_unregister(cpmac_mii);
  1050. fail_mii:
  1051. iounmap(cpmac_mii->priv);
  1052. fail_alloc:
  1053. mdiobus_free(cpmac_mii);
  1054. return res;
  1055. }
  1056. void cpmac_exit(void)
  1057. {
  1058. platform_driver_unregister(&cpmac_driver);
  1059. mdiobus_unregister(cpmac_mii);
  1060. iounmap(cpmac_mii->priv);
  1061. mdiobus_free(cpmac_mii);
  1062. }
  1063. module_init(cpmac_init);
  1064. module_exit(cpmac_exit);