cpmac.c 31 KB

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