cpmac.c 35 KB

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