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