cpmac.c 35 KB

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