cpmac.c 34 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278
  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_tx_stop_all_queues(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. netif_stop_subqueue(dev, queue);
  514. desc = &priv->desc_ring[queue];
  515. if (unlikely(desc->dataflags & CPMAC_OWN)) {
  516. if (netif_msg_tx_err(priv) && net_ratelimit())
  517. printk(KERN_WARNING "%s: tx dma ring full\n",
  518. dev->name);
  519. return NETDEV_TX_BUSY;
  520. }
  521. spin_lock(&priv->lock);
  522. dev->trans_start = jiffies;
  523. spin_unlock(&priv->lock);
  524. desc->dataflags = CPMAC_SOP | CPMAC_EOP | CPMAC_OWN;
  525. desc->skb = skb;
  526. desc->data_mapping = dma_map_single(&dev->dev, skb->data, len,
  527. DMA_TO_DEVICE);
  528. desc->hw_data = (u32)desc->data_mapping;
  529. desc->datalen = len;
  530. desc->buflen = len;
  531. if (unlikely(netif_msg_tx_queued(priv)))
  532. printk(KERN_DEBUG "%s: sending 0x%p, len=%d\n", dev->name, skb,
  533. skb->len);
  534. if (unlikely(netif_msg_hw(priv)))
  535. cpmac_dump_desc(dev, desc);
  536. if (unlikely(netif_msg_pktdata(priv)))
  537. cpmac_dump_skb(dev, skb);
  538. cpmac_write(priv->regs, CPMAC_TX_PTR(queue), (u32)desc->mapping);
  539. return NETDEV_TX_OK;
  540. }
  541. static void cpmac_end_xmit(struct net_device *dev, int queue)
  542. {
  543. struct cpmac_desc *desc;
  544. struct cpmac_priv *priv = netdev_priv(dev);
  545. desc = &priv->desc_ring[queue];
  546. cpmac_write(priv->regs, CPMAC_TX_ACK(queue), (u32)desc->mapping);
  547. if (likely(desc->skb)) {
  548. spin_lock(&priv->lock);
  549. dev->stats.tx_packets++;
  550. dev->stats.tx_bytes += desc->skb->len;
  551. spin_unlock(&priv->lock);
  552. dma_unmap_single(&dev->dev, desc->data_mapping, desc->skb->len,
  553. DMA_TO_DEVICE);
  554. if (unlikely(netif_msg_tx_done(priv)))
  555. printk(KERN_DEBUG "%s: sent 0x%p, len=%d\n", dev->name,
  556. desc->skb, desc->skb->len);
  557. dev_kfree_skb_irq(desc->skb);
  558. desc->skb = NULL;
  559. if (netif_subqueue_stopped(dev, queue))
  560. netif_wake_subqueue(dev, queue);
  561. } else {
  562. if (netif_msg_tx_err(priv) && net_ratelimit())
  563. printk(KERN_WARNING
  564. "%s: end_xmit: spurious interrupt\n", dev->name);
  565. if (netif_subqueue_stopped(dev, queue))
  566. netif_wake_subqueue(dev, queue);
  567. }
  568. }
  569. static void cpmac_hw_stop(struct net_device *dev)
  570. {
  571. int i;
  572. struct cpmac_priv *priv = netdev_priv(dev);
  573. struct plat_cpmac_data *pdata = priv->pdev->dev.platform_data;
  574. ar7_device_reset(pdata->reset_bit);
  575. cpmac_write(priv->regs, CPMAC_RX_CONTROL,
  576. cpmac_read(priv->regs, CPMAC_RX_CONTROL) & ~1);
  577. cpmac_write(priv->regs, CPMAC_TX_CONTROL,
  578. cpmac_read(priv->regs, CPMAC_TX_CONTROL) & ~1);
  579. for (i = 0; i < 8; i++) {
  580. cpmac_write(priv->regs, CPMAC_TX_PTR(i), 0);
  581. cpmac_write(priv->regs, CPMAC_RX_PTR(i), 0);
  582. }
  583. cpmac_write(priv->regs, CPMAC_UNICAST_CLEAR, 0xff);
  584. cpmac_write(priv->regs, CPMAC_RX_INT_CLEAR, 0xff);
  585. cpmac_write(priv->regs, CPMAC_TX_INT_CLEAR, 0xff);
  586. cpmac_write(priv->regs, CPMAC_MAC_INT_CLEAR, 0xff);
  587. cpmac_write(priv->regs, CPMAC_MAC_CONTROL,
  588. cpmac_read(priv->regs, CPMAC_MAC_CONTROL) & ~MAC_MII);
  589. }
  590. static void cpmac_hw_start(struct net_device *dev)
  591. {
  592. int i;
  593. struct cpmac_priv *priv = netdev_priv(dev);
  594. struct plat_cpmac_data *pdata = priv->pdev->dev.platform_data;
  595. ar7_device_reset(pdata->reset_bit);
  596. for (i = 0; i < 8; i++) {
  597. cpmac_write(priv->regs, CPMAC_TX_PTR(i), 0);
  598. cpmac_write(priv->regs, CPMAC_RX_PTR(i), 0);
  599. }
  600. cpmac_write(priv->regs, CPMAC_RX_PTR(0), priv->rx_head->mapping);
  601. cpmac_write(priv->regs, CPMAC_MBP, MBP_RXSHORT | MBP_RXBCAST |
  602. MBP_RXMCAST);
  603. cpmac_write(priv->regs, CPMAC_BUFFER_OFFSET, 0);
  604. for (i = 0; i < 8; i++)
  605. cpmac_write(priv->regs, CPMAC_MAC_ADDR_LO(i), dev->dev_addr[5]);
  606. cpmac_write(priv->regs, CPMAC_MAC_ADDR_MID, dev->dev_addr[4]);
  607. cpmac_write(priv->regs, CPMAC_MAC_ADDR_HI, dev->dev_addr[0] |
  608. (dev->dev_addr[1] << 8) | (dev->dev_addr[2] << 16) |
  609. (dev->dev_addr[3] << 24));
  610. cpmac_write(priv->regs, CPMAC_MAX_LENGTH, CPMAC_SKB_SIZE);
  611. cpmac_write(priv->regs, CPMAC_UNICAST_CLEAR, 0xff);
  612. cpmac_write(priv->regs, CPMAC_RX_INT_CLEAR, 0xff);
  613. cpmac_write(priv->regs, CPMAC_TX_INT_CLEAR, 0xff);
  614. cpmac_write(priv->regs, CPMAC_MAC_INT_CLEAR, 0xff);
  615. cpmac_write(priv->regs, CPMAC_UNICAST_ENABLE, 1);
  616. cpmac_write(priv->regs, CPMAC_RX_INT_ENABLE, 1);
  617. cpmac_write(priv->regs, CPMAC_TX_INT_ENABLE, 0xff);
  618. cpmac_write(priv->regs, CPMAC_MAC_INT_ENABLE, 3);
  619. cpmac_write(priv->regs, CPMAC_RX_CONTROL,
  620. cpmac_read(priv->regs, CPMAC_RX_CONTROL) | 1);
  621. cpmac_write(priv->regs, CPMAC_TX_CONTROL,
  622. cpmac_read(priv->regs, CPMAC_TX_CONTROL) | 1);
  623. cpmac_write(priv->regs, CPMAC_MAC_CONTROL,
  624. cpmac_read(priv->regs, CPMAC_MAC_CONTROL) | MAC_MII |
  625. MAC_FDX);
  626. }
  627. static void cpmac_clear_rx(struct net_device *dev)
  628. {
  629. struct cpmac_priv *priv = netdev_priv(dev);
  630. struct cpmac_desc *desc;
  631. int i;
  632. if (unlikely(!priv->rx_head))
  633. return;
  634. desc = priv->rx_head;
  635. for (i = 0; i < priv->ring_size; i++) {
  636. if ((desc->dataflags & CPMAC_OWN) == 0) {
  637. if (netif_msg_rx_err(priv) && net_ratelimit())
  638. printk(KERN_WARNING "%s: packet dropped\n",
  639. dev->name);
  640. if (unlikely(netif_msg_hw(priv)))
  641. cpmac_dump_desc(dev, desc);
  642. desc->dataflags = CPMAC_OWN;
  643. dev->stats.rx_dropped++;
  644. }
  645. desc->hw_next = desc->next->mapping;
  646. desc = desc->next;
  647. }
  648. priv->rx_head->prev->hw_next = 0;
  649. }
  650. static void cpmac_clear_tx(struct net_device *dev)
  651. {
  652. struct cpmac_priv *priv = netdev_priv(dev);
  653. int i;
  654. if (unlikely(!priv->desc_ring))
  655. return;
  656. for (i = 0; i < CPMAC_QUEUES; i++) {
  657. priv->desc_ring[i].dataflags = 0;
  658. if (priv->desc_ring[i].skb) {
  659. dev_kfree_skb_any(priv->desc_ring[i].skb);
  660. priv->desc_ring[i].skb = NULL;
  661. }
  662. }
  663. }
  664. static void cpmac_hw_error(struct work_struct *work)
  665. {
  666. int i;
  667. struct cpmac_priv *priv =
  668. container_of(work, struct cpmac_priv, reset_work);
  669. spin_lock(&priv->rx_lock);
  670. cpmac_clear_rx(priv->dev);
  671. spin_unlock(&priv->rx_lock);
  672. cpmac_clear_tx(priv->dev);
  673. cpmac_hw_start(priv->dev);
  674. barrier();
  675. atomic_dec(&priv->reset_pending);
  676. netif_tx_wake_all_queues(priv->dev);
  677. cpmac_write(priv->regs, CPMAC_MAC_INT_ENABLE, 3);
  678. }
  679. static void cpmac_check_status(struct net_device *dev)
  680. {
  681. struct cpmac_priv *priv = netdev_priv(dev);
  682. u32 macstatus = cpmac_read(priv->regs, CPMAC_MAC_STATUS);
  683. int rx_channel = (macstatus >> 8) & 7;
  684. int rx_code = (macstatus >> 12) & 15;
  685. int tx_channel = (macstatus >> 16) & 7;
  686. int tx_code = (macstatus >> 20) & 15;
  687. if (rx_code || tx_code) {
  688. if (netif_msg_drv(priv) && net_ratelimit()) {
  689. /* Can't find any documentation on what these
  690. *error codes actually are. So just log them and hope..
  691. */
  692. if (rx_code)
  693. printk(KERN_WARNING "%s: host error %d on rx "
  694. "channel %d (macstatus %08x), resetting\n",
  695. dev->name, rx_code, rx_channel, macstatus);
  696. if (tx_code)
  697. printk(KERN_WARNING "%s: host error %d on tx "
  698. "channel %d (macstatus %08x), resetting\n",
  699. dev->name, tx_code, tx_channel, macstatus);
  700. }
  701. netif_tx_stop_all_queues(dev);
  702. cpmac_hw_stop(dev);
  703. if (schedule_work(&priv->reset_work))
  704. atomic_inc(&priv->reset_pending);
  705. if (unlikely(netif_msg_hw(priv)))
  706. cpmac_dump_regs(dev);
  707. }
  708. cpmac_write(priv->regs, CPMAC_MAC_INT_CLEAR, 0xff);
  709. }
  710. static irqreturn_t cpmac_irq(int irq, void *dev_id)
  711. {
  712. struct net_device *dev = dev_id;
  713. struct cpmac_priv *priv;
  714. int queue;
  715. u32 status;
  716. priv = netdev_priv(dev);
  717. status = cpmac_read(priv->regs, CPMAC_MAC_INT_VECTOR);
  718. if (unlikely(netif_msg_intr(priv)))
  719. printk(KERN_DEBUG "%s: interrupt status: 0x%08x\n", dev->name,
  720. status);
  721. if (status & MAC_INT_TX)
  722. cpmac_end_xmit(dev, (status & 7));
  723. if (status & MAC_INT_RX) {
  724. queue = (status >> 8) & 7;
  725. if (netif_rx_schedule_prep(dev, &priv->napi)) {
  726. cpmac_write(priv->regs, CPMAC_RX_INT_CLEAR, 1 << queue);
  727. __netif_rx_schedule(dev, &priv->napi);
  728. }
  729. }
  730. cpmac_write(priv->regs, CPMAC_MAC_EOI_VECTOR, 0);
  731. if (unlikely(status & (MAC_INT_HOST | MAC_INT_STATUS)))
  732. cpmac_check_status(dev);
  733. return IRQ_HANDLED;
  734. }
  735. static void cpmac_tx_timeout(struct net_device *dev)
  736. {
  737. int i;
  738. struct cpmac_priv *priv = netdev_priv(dev);
  739. spin_lock(&priv->lock);
  740. dev->stats.tx_errors++;
  741. spin_unlock(&priv->lock);
  742. if (netif_msg_tx_err(priv) && net_ratelimit())
  743. printk(KERN_WARNING "%s: transmit timeout\n", dev->name);
  744. atomic_inc(&priv->reset_pending);
  745. barrier();
  746. cpmac_clear_tx(dev);
  747. barrier();
  748. atomic_dec(&priv->reset_pending);
  749. netif_tx_wake_all_queues(priv->dev);
  750. }
  751. static int cpmac_ioctl(struct net_device *dev, struct ifreq *ifr, int cmd)
  752. {
  753. struct cpmac_priv *priv = netdev_priv(dev);
  754. if (!(netif_running(dev)))
  755. return -EINVAL;
  756. if (!priv->phy)
  757. return -EINVAL;
  758. if ((cmd == SIOCGMIIPHY) || (cmd == SIOCGMIIREG) ||
  759. (cmd == SIOCSMIIREG))
  760. return phy_mii_ioctl(priv->phy, if_mii(ifr), cmd);
  761. return -EOPNOTSUPP;
  762. }
  763. static int cpmac_get_settings(struct net_device *dev, struct ethtool_cmd *cmd)
  764. {
  765. struct cpmac_priv *priv = netdev_priv(dev);
  766. if (priv->phy)
  767. return phy_ethtool_gset(priv->phy, cmd);
  768. return -EINVAL;
  769. }
  770. static int cpmac_set_settings(struct net_device *dev, struct ethtool_cmd *cmd)
  771. {
  772. struct cpmac_priv *priv = netdev_priv(dev);
  773. if (!capable(CAP_NET_ADMIN))
  774. return -EPERM;
  775. if (priv->phy)
  776. return phy_ethtool_sset(priv->phy, cmd);
  777. return -EINVAL;
  778. }
  779. static void cpmac_get_ringparam(struct net_device *dev, struct ethtool_ringparam* ring)
  780. {
  781. struct cpmac_priv *priv = netdev_priv(dev);
  782. ring->rx_max_pending = 1024;
  783. ring->rx_mini_max_pending = 1;
  784. ring->rx_jumbo_max_pending = 1;
  785. ring->tx_max_pending = 1;
  786. ring->rx_pending = priv->ring_size;
  787. ring->rx_mini_pending = 1;
  788. ring->rx_jumbo_pending = 1;
  789. ring->tx_pending = 1;
  790. }
  791. static int cpmac_set_ringparam(struct net_device *dev, struct ethtool_ringparam* ring)
  792. {
  793. struct cpmac_priv *priv = netdev_priv(dev);
  794. if (netif_running(dev))
  795. return -EBUSY;
  796. priv->ring_size = ring->rx_pending;
  797. return 0;
  798. }
  799. static void cpmac_get_drvinfo(struct net_device *dev,
  800. struct ethtool_drvinfo *info)
  801. {
  802. strcpy(info->driver, "cpmac");
  803. strcpy(info->version, CPMAC_VERSION);
  804. info->fw_version[0] = '\0';
  805. sprintf(info->bus_info, "%s", "cpmac");
  806. info->regdump_len = 0;
  807. }
  808. static const struct ethtool_ops cpmac_ethtool_ops = {
  809. .get_settings = cpmac_get_settings,
  810. .set_settings = cpmac_set_settings,
  811. .get_drvinfo = cpmac_get_drvinfo,
  812. .get_link = ethtool_op_get_link,
  813. .get_ringparam = cpmac_get_ringparam,
  814. .set_ringparam = cpmac_set_ringparam,
  815. };
  816. static void cpmac_adjust_link(struct net_device *dev)
  817. {
  818. struct cpmac_priv *priv = netdev_priv(dev);
  819. int new_state = 0;
  820. spin_lock(&priv->lock);
  821. if (priv->phy->link) {
  822. netif_tx_start_all_queues(dev);
  823. if (priv->phy->duplex != priv->oldduplex) {
  824. new_state = 1;
  825. priv->oldduplex = priv->phy->duplex;
  826. }
  827. if (priv->phy->speed != priv->oldspeed) {
  828. new_state = 1;
  829. priv->oldspeed = priv->phy->speed;
  830. }
  831. if (!priv->oldlink) {
  832. new_state = 1;
  833. priv->oldlink = 1;
  834. }
  835. } else if (priv->oldlink) {
  836. new_state = 1;
  837. priv->oldlink = 0;
  838. priv->oldspeed = 0;
  839. priv->oldduplex = -1;
  840. }
  841. if (new_state && netif_msg_link(priv) && net_ratelimit())
  842. phy_print_status(priv->phy);
  843. spin_unlock(&priv->lock);
  844. }
  845. static int cpmac_open(struct net_device *dev)
  846. {
  847. int i, size, res;
  848. struct cpmac_priv *priv = netdev_priv(dev);
  849. struct resource *mem;
  850. struct cpmac_desc *desc;
  851. struct sk_buff *skb;
  852. mem = platform_get_resource_byname(priv->pdev, IORESOURCE_MEM, "regs");
  853. if (!request_mem_region(mem->start, mem->end - mem->start, dev->name)) {
  854. if (netif_msg_drv(priv))
  855. printk(KERN_ERR "%s: failed to request registers\n",
  856. dev->name);
  857. res = -ENXIO;
  858. goto fail_reserve;
  859. }
  860. priv->regs = ioremap(mem->start, mem->end - mem->start);
  861. if (!priv->regs) {
  862. if (netif_msg_drv(priv))
  863. printk(KERN_ERR "%s: failed to remap registers\n",
  864. dev->name);
  865. res = -ENXIO;
  866. goto fail_remap;
  867. }
  868. size = priv->ring_size + CPMAC_QUEUES;
  869. priv->desc_ring = dma_alloc_coherent(&dev->dev,
  870. sizeof(struct cpmac_desc) * size,
  871. &priv->dma_ring,
  872. GFP_KERNEL);
  873. if (!priv->desc_ring) {
  874. res = -ENOMEM;
  875. goto fail_alloc;
  876. }
  877. for (i = 0; i < size; i++)
  878. priv->desc_ring[i].mapping = priv->dma_ring + sizeof(*desc) * i;
  879. priv->rx_head = &priv->desc_ring[CPMAC_QUEUES];
  880. for (i = 0, desc = priv->rx_head; i < priv->ring_size; i++, desc++) {
  881. skb = netdev_alloc_skb(dev, CPMAC_SKB_SIZE);
  882. if (unlikely(!skb)) {
  883. res = -ENOMEM;
  884. goto fail_desc;
  885. }
  886. skb_reserve(skb, 2);
  887. desc->skb = skb;
  888. desc->data_mapping = dma_map_single(&dev->dev, skb->data,
  889. CPMAC_SKB_SIZE,
  890. DMA_FROM_DEVICE);
  891. desc->hw_data = (u32)desc->data_mapping;
  892. desc->buflen = CPMAC_SKB_SIZE;
  893. desc->dataflags = CPMAC_OWN;
  894. desc->next = &priv->rx_head[(i + 1) % priv->ring_size];
  895. desc->next->prev = desc;
  896. desc->hw_next = (u32)desc->next->mapping;
  897. }
  898. priv->rx_head->prev->hw_next = (u32)0;
  899. if ((res = request_irq(dev->irq, cpmac_irq, IRQF_SHARED,
  900. dev->name, dev))) {
  901. if (netif_msg_drv(priv))
  902. printk(KERN_ERR "%s: failed to obtain irq\n",
  903. dev->name);
  904. goto fail_irq;
  905. }
  906. atomic_set(&priv->reset_pending, 0);
  907. INIT_WORK(&priv->reset_work, cpmac_hw_error);
  908. cpmac_hw_start(dev);
  909. napi_enable(&priv->napi);
  910. priv->phy->state = PHY_CHANGELINK;
  911. phy_start(priv->phy);
  912. return 0;
  913. fail_irq:
  914. fail_desc:
  915. for (i = 0; i < priv->ring_size; i++) {
  916. if (priv->rx_head[i].skb) {
  917. dma_unmap_single(&dev->dev,
  918. priv->rx_head[i].data_mapping,
  919. CPMAC_SKB_SIZE,
  920. DMA_FROM_DEVICE);
  921. kfree_skb(priv->rx_head[i].skb);
  922. }
  923. }
  924. fail_alloc:
  925. kfree(priv->desc_ring);
  926. iounmap(priv->regs);
  927. fail_remap:
  928. release_mem_region(mem->start, mem->end - mem->start);
  929. fail_reserve:
  930. return res;
  931. }
  932. static int cpmac_stop(struct net_device *dev)
  933. {
  934. int i;
  935. struct cpmac_priv *priv = netdev_priv(dev);
  936. struct resource *mem;
  937. netif_tx_stop_all_queues(dev);
  938. cancel_work_sync(&priv->reset_work);
  939. napi_disable(&priv->napi);
  940. phy_stop(priv->phy);
  941. cpmac_hw_stop(dev);
  942. for (i = 0; i < 8; i++)
  943. cpmac_write(priv->regs, CPMAC_TX_PTR(i), 0);
  944. cpmac_write(priv->regs, CPMAC_RX_PTR(0), 0);
  945. cpmac_write(priv->regs, CPMAC_MBP, 0);
  946. free_irq(dev->irq, dev);
  947. iounmap(priv->regs);
  948. mem = platform_get_resource_byname(priv->pdev, IORESOURCE_MEM, "regs");
  949. release_mem_region(mem->start, mem->end - mem->start);
  950. priv->rx_head = &priv->desc_ring[CPMAC_QUEUES];
  951. for (i = 0; i < priv->ring_size; i++) {
  952. if (priv->rx_head[i].skb) {
  953. dma_unmap_single(&dev->dev,
  954. priv->rx_head[i].data_mapping,
  955. CPMAC_SKB_SIZE,
  956. DMA_FROM_DEVICE);
  957. kfree_skb(priv->rx_head[i].skb);
  958. }
  959. }
  960. dma_free_coherent(&dev->dev, sizeof(struct cpmac_desc) *
  961. (CPMAC_QUEUES + priv->ring_size),
  962. priv->desc_ring, priv->dma_ring);
  963. return 0;
  964. }
  965. static int external_switch;
  966. static int __devinit cpmac_probe(struct platform_device *pdev)
  967. {
  968. int rc, phy_id, i;
  969. char *mdio_bus_id = "0";
  970. struct resource *mem;
  971. struct cpmac_priv *priv;
  972. struct net_device *dev;
  973. struct plat_cpmac_data *pdata;
  974. DECLARE_MAC_BUF(mac);
  975. pdata = pdev->dev.platform_data;
  976. for (phy_id = 0; phy_id < PHY_MAX_ADDR; phy_id++) {
  977. if (!(pdata->phy_mask & (1 << phy_id)))
  978. continue;
  979. if (!cpmac_mii.phy_map[phy_id])
  980. continue;
  981. break;
  982. }
  983. if (phy_id == PHY_MAX_ADDR) {
  984. if (external_switch || dumb_switch) {
  985. mdio_bus_id = 0; /* fixed phys bus */
  986. phy_id = pdev->id;
  987. } else {
  988. dev_err(&pdev->dev, "no PHY present\n");
  989. return -ENODEV;
  990. }
  991. }
  992. dev = alloc_etherdev_mq(sizeof(*priv), CPMAC_QUEUES);
  993. if (!dev) {
  994. printk(KERN_ERR "cpmac: Unable to allocate net_device\n");
  995. return -ENOMEM;
  996. }
  997. platform_set_drvdata(pdev, dev);
  998. priv = netdev_priv(dev);
  999. priv->pdev = pdev;
  1000. mem = platform_get_resource_byname(pdev, IORESOURCE_MEM, "regs");
  1001. if (!mem) {
  1002. rc = -ENODEV;
  1003. goto fail;
  1004. }
  1005. dev->irq = platform_get_irq_byname(pdev, "irq");
  1006. dev->open = cpmac_open;
  1007. dev->stop = cpmac_stop;
  1008. dev->set_config = cpmac_config;
  1009. dev->hard_start_xmit = cpmac_start_xmit;
  1010. dev->do_ioctl = cpmac_ioctl;
  1011. dev->set_multicast_list = cpmac_set_multicast_list;
  1012. dev->tx_timeout = cpmac_tx_timeout;
  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(dev->dev_addr));
  1021. priv->phy = phy_connect(dev, cpmac_mii.phy_map[phy_id]->dev.bus_id,
  1022. &cpmac_adjust_link, 0, PHY_INTERFACE_MODE_MII);
  1023. if (IS_ERR(priv->phy)) {
  1024. if (netif_msg_drv(priv))
  1025. printk(KERN_ERR "%s: Could not attach to PHY\n",
  1026. dev->name);
  1027. return PTR_ERR(priv->phy);
  1028. }
  1029. if ((rc = register_netdev(dev))) {
  1030. printk(KERN_ERR "cpmac: error %i registering device %s\n", rc,
  1031. dev->name);
  1032. goto fail;
  1033. }
  1034. if (netif_msg_probe(priv)) {
  1035. printk(KERN_INFO
  1036. "cpmac: device %s (regs: %p, irq: %d, phy: %s, "
  1037. "mac: %s)\n", dev->name, (void *)mem->start, dev->irq,
  1038. priv->phy_name, print_mac(mac, dev->dev_addr));
  1039. }
  1040. return 0;
  1041. fail:
  1042. free_netdev(dev);
  1043. return rc;
  1044. }
  1045. static int __devexit cpmac_remove(struct platform_device *pdev)
  1046. {
  1047. struct net_device *dev = platform_get_drvdata(pdev);
  1048. unregister_netdev(dev);
  1049. free_netdev(dev);
  1050. return 0;
  1051. }
  1052. static struct platform_driver cpmac_driver = {
  1053. .driver.name = "cpmac",
  1054. .driver.owner = THIS_MODULE,
  1055. .probe = cpmac_probe,
  1056. .remove = __devexit_p(cpmac_remove),
  1057. };
  1058. int __devinit cpmac_init(void)
  1059. {
  1060. u32 mask;
  1061. int i, res;
  1062. cpmac_mii.priv = ioremap(AR7_REGS_MDIO, 256);
  1063. if (!cpmac_mii.priv) {
  1064. printk(KERN_ERR "Can't ioremap mdio registers\n");
  1065. return -ENXIO;
  1066. }
  1067. #warning FIXME: unhardcode gpio&reset bits
  1068. ar7_gpio_disable(26);
  1069. ar7_gpio_disable(27);
  1070. ar7_device_reset(AR7_RESET_BIT_CPMAC_LO);
  1071. ar7_device_reset(AR7_RESET_BIT_CPMAC_HI);
  1072. ar7_device_reset(AR7_RESET_BIT_EPHY);
  1073. cpmac_mii.reset(&cpmac_mii);
  1074. for (i = 0; i < 300000; i++)
  1075. if ((mask = cpmac_read(cpmac_mii.priv, CPMAC_MDIO_ALIVE)))
  1076. break;
  1077. else
  1078. cpu_relax();
  1079. mask &= 0x7fffffff;
  1080. if (mask & (mask - 1)) {
  1081. external_switch = 1;
  1082. mask = 0;
  1083. }
  1084. cpmac_mii.phy_mask = ~(mask | 0x80000000);
  1085. snprintf(cpmac_mii.id, MII_BUS_ID_SIZE, "0");
  1086. res = mdiobus_register(&cpmac_mii);
  1087. if (res)
  1088. goto fail_mii;
  1089. res = platform_driver_register(&cpmac_driver);
  1090. if (res)
  1091. goto fail_cpmac;
  1092. return 0;
  1093. fail_cpmac:
  1094. mdiobus_unregister(&cpmac_mii);
  1095. fail_mii:
  1096. iounmap(cpmac_mii.priv);
  1097. return res;
  1098. }
  1099. void __devexit cpmac_exit(void)
  1100. {
  1101. platform_driver_unregister(&cpmac_driver);
  1102. mdiobus_unregister(&cpmac_mii);
  1103. iounmap(cpmac_mii.priv);
  1104. }
  1105. module_init(cpmac_init);
  1106. module_exit(cpmac_exit);