cpmac.c 34 KB

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