cpmac.c 31 KB

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