cpmac.c 35 KB

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