cpmac.c 33 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269
  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. char phy_name[MII_BUS_ID_SIZE + 3];
  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("%s: reg[%p]:", dev->name, 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("%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("%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("%s: data[%p]:", dev->name, skb->data + i);
  246. }
  247. printk(" %02x", ((u8 *)skb->data)[i]);
  248. }
  249. printk("\n");
  250. }
  251. static int cpmac_mdio_read(struct mii_bus *bus, int phy_id, int reg)
  252. {
  253. u32 val;
  254. while (cpmac_read(bus->priv, CPMAC_MDIO_ACCESS(0)) & MDIO_BUSY)
  255. cpu_relax();
  256. cpmac_write(bus->priv, CPMAC_MDIO_ACCESS(0), MDIO_BUSY | MDIO_REG(reg) |
  257. MDIO_PHY(phy_id));
  258. while ((val = cpmac_read(bus->priv, CPMAC_MDIO_ACCESS(0))) & MDIO_BUSY)
  259. cpu_relax();
  260. return MDIO_DATA(val);
  261. }
  262. static int cpmac_mdio_write(struct mii_bus *bus, int phy_id,
  263. int reg, u16 val)
  264. {
  265. while (cpmac_read(bus->priv, CPMAC_MDIO_ACCESS(0)) & MDIO_BUSY)
  266. cpu_relax();
  267. cpmac_write(bus->priv, CPMAC_MDIO_ACCESS(0), MDIO_BUSY | MDIO_WRITE |
  268. MDIO_REG(reg) | MDIO_PHY(phy_id) | MDIO_DATA(val));
  269. return 0;
  270. }
  271. static int cpmac_mdio_reset(struct mii_bus *bus)
  272. {
  273. struct clk *cpmac_clk;
  274. cpmac_clk = clk_get(&bus->dev, "cpmac");
  275. if (IS_ERR(cpmac_clk)) {
  276. pr_err("unable to get cpmac clock\n");
  277. return -1;
  278. }
  279. ar7_device_reset(AR7_RESET_BIT_MDIO);
  280. cpmac_write(bus->priv, CPMAC_MDIO_CONTROL, MDIOC_ENABLE |
  281. MDIOC_CLKDIV(clk_get_rate(cpmac_clk) / 2200000 - 1));
  282. return 0;
  283. }
  284. static struct mii_bus *cpmac_mii;
  285. static void cpmac_set_multicast_list(struct net_device *dev)
  286. {
  287. struct netdev_hw_addr *ha;
  288. u8 tmp;
  289. u32 mbp, bit, hash[2] = { 0, };
  290. struct cpmac_priv *priv = netdev_priv(dev);
  291. mbp = cpmac_read(priv->regs, CPMAC_MBP);
  292. if (dev->flags & IFF_PROMISC) {
  293. cpmac_write(priv->regs, CPMAC_MBP, (mbp & ~MBP_PROMISCCHAN(0)) |
  294. MBP_RXPROMISC);
  295. } else {
  296. cpmac_write(priv->regs, CPMAC_MBP, mbp & ~MBP_RXPROMISC);
  297. if (dev->flags & IFF_ALLMULTI) {
  298. /* enable all multicast mode */
  299. cpmac_write(priv->regs, CPMAC_MAC_HASH_LO, 0xffffffff);
  300. cpmac_write(priv->regs, CPMAC_MAC_HASH_HI, 0xffffffff);
  301. } else {
  302. /* cpmac uses some strange mac address hashing
  303. * (not crc32)
  304. */
  305. netdev_for_each_mc_addr(ha, dev) {
  306. bit = 0;
  307. tmp = ha->addr[0];
  308. bit ^= (tmp >> 2) ^ (tmp << 4);
  309. tmp = ha->addr[1];
  310. bit ^= (tmp >> 4) ^ (tmp << 2);
  311. tmp = ha->addr[2];
  312. bit ^= (tmp >> 6) ^ tmp;
  313. tmp = ha->addr[3];
  314. bit ^= (tmp >> 2) ^ (tmp << 4);
  315. tmp = ha->addr[4];
  316. bit ^= (tmp >> 4) ^ (tmp << 2);
  317. tmp = ha->addr[5];
  318. bit ^= (tmp >> 6) ^ tmp;
  319. bit &= 0x3f;
  320. hash[bit / 32] |= 1 << (bit % 32);
  321. }
  322. cpmac_write(priv->regs, CPMAC_MAC_HASH_LO, hash[0]);
  323. cpmac_write(priv->regs, CPMAC_MAC_HASH_HI, hash[1]);
  324. }
  325. }
  326. }
  327. static struct sk_buff *cpmac_rx_one(struct cpmac_priv *priv,
  328. struct cpmac_desc *desc)
  329. {
  330. struct sk_buff *skb, *result = NULL;
  331. if (unlikely(netif_msg_hw(priv)))
  332. cpmac_dump_desc(priv->dev, desc);
  333. cpmac_write(priv->regs, CPMAC_RX_ACK(0), (u32)desc->mapping);
  334. if (unlikely(!desc->datalen)) {
  335. if (netif_msg_rx_err(priv) && net_ratelimit())
  336. netdev_warn(priv->dev, "rx: spurious interrupt\n");
  337. return NULL;
  338. }
  339. skb = netdev_alloc_skb_ip_align(priv->dev, CPMAC_SKB_SIZE);
  340. if (likely(skb)) {
  341. skb_put(desc->skb, desc->datalen);
  342. desc->skb->protocol = eth_type_trans(desc->skb, priv->dev);
  343. skb_checksum_none_assert(desc->skb);
  344. priv->dev->stats.rx_packets++;
  345. priv->dev->stats.rx_bytes += desc->datalen;
  346. result = desc->skb;
  347. dma_unmap_single(&priv->dev->dev, desc->data_mapping,
  348. CPMAC_SKB_SIZE, DMA_FROM_DEVICE);
  349. desc->skb = skb;
  350. desc->data_mapping = dma_map_single(&priv->dev->dev, skb->data,
  351. CPMAC_SKB_SIZE,
  352. DMA_FROM_DEVICE);
  353. desc->hw_data = (u32)desc->data_mapping;
  354. if (unlikely(netif_msg_pktdata(priv))) {
  355. netdev_dbg(priv->dev, "received packet:\n");
  356. cpmac_dump_skb(priv->dev, result);
  357. }
  358. } else {
  359. if (netif_msg_rx_err(priv) && net_ratelimit())
  360. netdev_warn(priv->dev,
  361. "low on skbs, dropping packet\n");
  362. priv->dev->stats.rx_dropped++;
  363. }
  364. desc->buflen = CPMAC_SKB_SIZE;
  365. desc->dataflags = CPMAC_OWN;
  366. return result;
  367. }
  368. static int cpmac_poll(struct napi_struct *napi, int budget)
  369. {
  370. struct sk_buff *skb;
  371. struct cpmac_desc *desc, *restart;
  372. struct cpmac_priv *priv = container_of(napi, struct cpmac_priv, napi);
  373. int received = 0, processed = 0;
  374. spin_lock(&priv->rx_lock);
  375. if (unlikely(!priv->rx_head)) {
  376. if (netif_msg_rx_err(priv) && net_ratelimit())
  377. netdev_warn(priv->dev, "rx: polling, but no queue\n");
  378. spin_unlock(&priv->rx_lock);
  379. napi_complete(napi);
  380. return 0;
  381. }
  382. desc = priv->rx_head;
  383. restart = NULL;
  384. while (((desc->dataflags & CPMAC_OWN) == 0) && (received < budget)) {
  385. processed++;
  386. if ((desc->dataflags & CPMAC_EOQ) != 0) {
  387. /* The last update to eoq->hw_next didn't happen
  388. * soon enough, and the receiver stopped here.
  389. * Remember this descriptor so we can restart
  390. * the receiver after freeing some space.
  391. */
  392. if (unlikely(restart)) {
  393. if (netif_msg_rx_err(priv))
  394. netdev_err(priv->dev, "poll found a"
  395. " duplicate EOQ: %p and %p\n",
  396. restart, desc);
  397. goto fatal_error;
  398. }
  399. restart = desc->next;
  400. }
  401. skb = cpmac_rx_one(priv, desc);
  402. if (likely(skb)) {
  403. netif_receive_skb(skb);
  404. received++;
  405. }
  406. desc = desc->next;
  407. }
  408. if (desc != priv->rx_head) {
  409. /* We freed some buffers, but not the whole ring,
  410. * add what we did free to the rx list
  411. */
  412. desc->prev->hw_next = (u32)0;
  413. priv->rx_head->prev->hw_next = priv->rx_head->mapping;
  414. }
  415. /* Optimization: If we did not actually process an EOQ (perhaps because
  416. * of quota limits), check to see if the tail of the queue has EOQ set.
  417. * We should immediately restart in that case so that the receiver can
  418. * restart and run in parallel with more packet processing.
  419. * This lets us handle slightly larger bursts before running
  420. * out of ring space (assuming dev->weight < ring_size)
  421. */
  422. if (!restart &&
  423. (priv->rx_head->prev->dataflags & (CPMAC_OWN|CPMAC_EOQ))
  424. == CPMAC_EOQ &&
  425. (priv->rx_head->dataflags & CPMAC_OWN) != 0) {
  426. /* reset EOQ so the poll loop (above) doesn't try to
  427. * restart this when it eventually gets to this descriptor.
  428. */
  429. priv->rx_head->prev->dataflags &= ~CPMAC_EOQ;
  430. restart = priv->rx_head;
  431. }
  432. if (restart) {
  433. priv->dev->stats.rx_errors++;
  434. priv->dev->stats.rx_fifo_errors++;
  435. if (netif_msg_rx_err(priv) && net_ratelimit())
  436. netdev_warn(priv->dev, "rx dma ring overrun\n");
  437. if (unlikely((restart->dataflags & CPMAC_OWN) == 0)) {
  438. if (netif_msg_drv(priv))
  439. netdev_err(priv->dev, "cpmac_poll is trying "
  440. "to restart rx from a descriptor "
  441. "that's not free: %p\n", restart);
  442. goto fatal_error;
  443. }
  444. cpmac_write(priv->regs, CPMAC_RX_PTR(0), restart->mapping);
  445. }
  446. priv->rx_head = desc;
  447. spin_unlock(&priv->rx_lock);
  448. if (unlikely(netif_msg_rx_status(priv)))
  449. netdev_dbg(priv->dev, "poll processed %d packets\n", received);
  450. if (processed == 0) {
  451. /* we ran out of packets to read,
  452. * revert to interrupt-driven mode
  453. */
  454. napi_complete(napi);
  455. cpmac_write(priv->regs, CPMAC_RX_INT_ENABLE, 1);
  456. return 0;
  457. }
  458. return 1;
  459. fatal_error:
  460. /* Something went horribly wrong.
  461. * Reset hardware to try to recover rather than wedging.
  462. */
  463. if (netif_msg_drv(priv)) {
  464. netdev_err(priv->dev, "cpmac_poll is confused. "
  465. "Resetting hardware\n");
  466. cpmac_dump_all_desc(priv->dev);
  467. netdev_dbg(priv->dev, "RX_PTR(0)=0x%08x RX_ACK(0)=0x%08x\n",
  468. cpmac_read(priv->regs, CPMAC_RX_PTR(0)),
  469. cpmac_read(priv->regs, CPMAC_RX_ACK(0)));
  470. }
  471. spin_unlock(&priv->rx_lock);
  472. napi_complete(napi);
  473. netif_tx_stop_all_queues(priv->dev);
  474. napi_disable(&priv->napi);
  475. atomic_inc(&priv->reset_pending);
  476. cpmac_hw_stop(priv->dev);
  477. if (!schedule_work(&priv->reset_work))
  478. atomic_dec(&priv->reset_pending);
  479. return 0;
  480. }
  481. static int cpmac_start_xmit(struct sk_buff *skb, struct net_device *dev)
  482. {
  483. int queue, len;
  484. struct cpmac_desc *desc;
  485. struct cpmac_priv *priv = netdev_priv(dev);
  486. if (unlikely(atomic_read(&priv->reset_pending)))
  487. return NETDEV_TX_BUSY;
  488. if (unlikely(skb_padto(skb, ETH_ZLEN)))
  489. return NETDEV_TX_OK;
  490. len = max(skb->len, ETH_ZLEN);
  491. queue = skb_get_queue_mapping(skb);
  492. netif_stop_subqueue(dev, queue);
  493. desc = &priv->desc_ring[queue];
  494. if (unlikely(desc->dataflags & CPMAC_OWN)) {
  495. if (netif_msg_tx_err(priv) && net_ratelimit())
  496. netdev_warn(dev, "tx dma ring full\n");
  497. return NETDEV_TX_BUSY;
  498. }
  499. spin_lock(&priv->lock);
  500. spin_unlock(&priv->lock);
  501. desc->dataflags = CPMAC_SOP | CPMAC_EOP | CPMAC_OWN;
  502. desc->skb = skb;
  503. desc->data_mapping = dma_map_single(&dev->dev, skb->data, len,
  504. DMA_TO_DEVICE);
  505. desc->hw_data = (u32)desc->data_mapping;
  506. desc->datalen = len;
  507. desc->buflen = len;
  508. if (unlikely(netif_msg_tx_queued(priv)))
  509. netdev_dbg(dev, "sending 0x%p, len=%d\n", skb, skb->len);
  510. if (unlikely(netif_msg_hw(priv)))
  511. cpmac_dump_desc(dev, desc);
  512. if (unlikely(netif_msg_pktdata(priv)))
  513. cpmac_dump_skb(dev, skb);
  514. cpmac_write(priv->regs, CPMAC_TX_PTR(queue), (u32)desc->mapping);
  515. return NETDEV_TX_OK;
  516. }
  517. static void cpmac_end_xmit(struct net_device *dev, int queue)
  518. {
  519. struct cpmac_desc *desc;
  520. struct cpmac_priv *priv = netdev_priv(dev);
  521. desc = &priv->desc_ring[queue];
  522. cpmac_write(priv->regs, CPMAC_TX_ACK(queue), (u32)desc->mapping);
  523. if (likely(desc->skb)) {
  524. spin_lock(&priv->lock);
  525. dev->stats.tx_packets++;
  526. dev->stats.tx_bytes += desc->skb->len;
  527. spin_unlock(&priv->lock);
  528. dma_unmap_single(&dev->dev, desc->data_mapping, desc->skb->len,
  529. DMA_TO_DEVICE);
  530. if (unlikely(netif_msg_tx_done(priv)))
  531. netdev_dbg(dev, "sent 0x%p, len=%d\n",
  532. desc->skb, desc->skb->len);
  533. dev_kfree_skb_irq(desc->skb);
  534. desc->skb = NULL;
  535. if (__netif_subqueue_stopped(dev, queue))
  536. netif_wake_subqueue(dev, queue);
  537. } else {
  538. if (netif_msg_tx_err(priv) && net_ratelimit())
  539. netdev_warn(dev, "end_xmit: spurious interrupt\n");
  540. if (__netif_subqueue_stopped(dev, queue))
  541. netif_wake_subqueue(dev, queue);
  542. }
  543. }
  544. static void cpmac_hw_stop(struct net_device *dev)
  545. {
  546. int i;
  547. struct cpmac_priv *priv = netdev_priv(dev);
  548. struct plat_cpmac_data *pdata = dev_get_platdata(&priv->pdev->dev);
  549. ar7_device_reset(pdata->reset_bit);
  550. cpmac_write(priv->regs, CPMAC_RX_CONTROL,
  551. cpmac_read(priv->regs, CPMAC_RX_CONTROL) & ~1);
  552. cpmac_write(priv->regs, CPMAC_TX_CONTROL,
  553. cpmac_read(priv->regs, CPMAC_TX_CONTROL) & ~1);
  554. for (i = 0; i < 8; i++) {
  555. cpmac_write(priv->regs, CPMAC_TX_PTR(i), 0);
  556. cpmac_write(priv->regs, CPMAC_RX_PTR(i), 0);
  557. }
  558. cpmac_write(priv->regs, CPMAC_UNICAST_CLEAR, 0xff);
  559. cpmac_write(priv->regs, CPMAC_RX_INT_CLEAR, 0xff);
  560. cpmac_write(priv->regs, CPMAC_TX_INT_CLEAR, 0xff);
  561. cpmac_write(priv->regs, CPMAC_MAC_INT_CLEAR, 0xff);
  562. cpmac_write(priv->regs, CPMAC_MAC_CONTROL,
  563. cpmac_read(priv->regs, CPMAC_MAC_CONTROL) & ~MAC_MII);
  564. }
  565. static void cpmac_hw_start(struct net_device *dev)
  566. {
  567. int i;
  568. struct cpmac_priv *priv = netdev_priv(dev);
  569. struct plat_cpmac_data *pdata = dev_get_platdata(&priv->pdev->dev);
  570. ar7_device_reset(pdata->reset_bit);
  571. for (i = 0; i < 8; i++) {
  572. cpmac_write(priv->regs, CPMAC_TX_PTR(i), 0);
  573. cpmac_write(priv->regs, CPMAC_RX_PTR(i), 0);
  574. }
  575. cpmac_write(priv->regs, CPMAC_RX_PTR(0), priv->rx_head->mapping);
  576. cpmac_write(priv->regs, CPMAC_MBP, MBP_RXSHORT | MBP_RXBCAST |
  577. MBP_RXMCAST);
  578. cpmac_write(priv->regs, CPMAC_BUFFER_OFFSET, 0);
  579. for (i = 0; i < 8; i++)
  580. cpmac_write(priv->regs, CPMAC_MAC_ADDR_LO(i), dev->dev_addr[5]);
  581. cpmac_write(priv->regs, CPMAC_MAC_ADDR_MID, dev->dev_addr[4]);
  582. cpmac_write(priv->regs, CPMAC_MAC_ADDR_HI, dev->dev_addr[0] |
  583. (dev->dev_addr[1] << 8) | (dev->dev_addr[2] << 16) |
  584. (dev->dev_addr[3] << 24));
  585. cpmac_write(priv->regs, CPMAC_MAX_LENGTH, CPMAC_SKB_SIZE);
  586. cpmac_write(priv->regs, CPMAC_UNICAST_CLEAR, 0xff);
  587. cpmac_write(priv->regs, CPMAC_RX_INT_CLEAR, 0xff);
  588. cpmac_write(priv->regs, CPMAC_TX_INT_CLEAR, 0xff);
  589. cpmac_write(priv->regs, CPMAC_MAC_INT_CLEAR, 0xff);
  590. cpmac_write(priv->regs, CPMAC_UNICAST_ENABLE, 1);
  591. cpmac_write(priv->regs, CPMAC_RX_INT_ENABLE, 1);
  592. cpmac_write(priv->regs, CPMAC_TX_INT_ENABLE, 0xff);
  593. cpmac_write(priv->regs, CPMAC_MAC_INT_ENABLE, 3);
  594. cpmac_write(priv->regs, CPMAC_RX_CONTROL,
  595. cpmac_read(priv->regs, CPMAC_RX_CONTROL) | 1);
  596. cpmac_write(priv->regs, CPMAC_TX_CONTROL,
  597. cpmac_read(priv->regs, CPMAC_TX_CONTROL) | 1);
  598. cpmac_write(priv->regs, CPMAC_MAC_CONTROL,
  599. cpmac_read(priv->regs, CPMAC_MAC_CONTROL) | MAC_MII |
  600. MAC_FDX);
  601. }
  602. static void cpmac_clear_rx(struct net_device *dev)
  603. {
  604. struct cpmac_priv *priv = netdev_priv(dev);
  605. struct cpmac_desc *desc;
  606. int i;
  607. if (unlikely(!priv->rx_head))
  608. return;
  609. desc = priv->rx_head;
  610. for (i = 0; i < priv->ring_size; i++) {
  611. if ((desc->dataflags & CPMAC_OWN) == 0) {
  612. if (netif_msg_rx_err(priv) && net_ratelimit())
  613. netdev_warn(dev, "packet dropped\n");
  614. if (unlikely(netif_msg_hw(priv)))
  615. cpmac_dump_desc(dev, desc);
  616. desc->dataflags = CPMAC_OWN;
  617. dev->stats.rx_dropped++;
  618. }
  619. desc->hw_next = desc->next->mapping;
  620. desc = desc->next;
  621. }
  622. priv->rx_head->prev->hw_next = 0;
  623. }
  624. static void cpmac_clear_tx(struct net_device *dev)
  625. {
  626. struct cpmac_priv *priv = netdev_priv(dev);
  627. int i;
  628. if (unlikely(!priv->desc_ring))
  629. return;
  630. for (i = 0; i < CPMAC_QUEUES; i++) {
  631. priv->desc_ring[i].dataflags = 0;
  632. if (priv->desc_ring[i].skb) {
  633. dev_kfree_skb_any(priv->desc_ring[i].skb);
  634. priv->desc_ring[i].skb = NULL;
  635. }
  636. }
  637. }
  638. static void cpmac_hw_error(struct work_struct *work)
  639. {
  640. struct cpmac_priv *priv =
  641. container_of(work, struct cpmac_priv, reset_work);
  642. spin_lock(&priv->rx_lock);
  643. cpmac_clear_rx(priv->dev);
  644. spin_unlock(&priv->rx_lock);
  645. cpmac_clear_tx(priv->dev);
  646. cpmac_hw_start(priv->dev);
  647. barrier();
  648. atomic_dec(&priv->reset_pending);
  649. netif_tx_wake_all_queues(priv->dev);
  650. cpmac_write(priv->regs, CPMAC_MAC_INT_ENABLE, 3);
  651. }
  652. static void cpmac_check_status(struct net_device *dev)
  653. {
  654. struct cpmac_priv *priv = netdev_priv(dev);
  655. u32 macstatus = cpmac_read(priv->regs, CPMAC_MAC_STATUS);
  656. int rx_channel = (macstatus >> 8) & 7;
  657. int rx_code = (macstatus >> 12) & 15;
  658. int tx_channel = (macstatus >> 16) & 7;
  659. int tx_code = (macstatus >> 20) & 15;
  660. if (rx_code || tx_code) {
  661. if (netif_msg_drv(priv) && net_ratelimit()) {
  662. /* Can't find any documentation on what these
  663. * error codes actually are. So just log them and hope..
  664. */
  665. if (rx_code)
  666. netdev_warn(dev, "host error %d on rx "
  667. "channel %d (macstatus %08x), resetting\n",
  668. rx_code, rx_channel, macstatus);
  669. if (tx_code)
  670. netdev_warn(dev, "host error %d on tx "
  671. "channel %d (macstatus %08x), resetting\n",
  672. tx_code, tx_channel, macstatus);
  673. }
  674. netif_tx_stop_all_queues(dev);
  675. cpmac_hw_stop(dev);
  676. if (schedule_work(&priv->reset_work))
  677. atomic_inc(&priv->reset_pending);
  678. if (unlikely(netif_msg_hw(priv)))
  679. cpmac_dump_regs(dev);
  680. }
  681. cpmac_write(priv->regs, CPMAC_MAC_INT_CLEAR, 0xff);
  682. }
  683. static irqreturn_t cpmac_irq(int irq, void *dev_id)
  684. {
  685. struct net_device *dev = dev_id;
  686. struct cpmac_priv *priv;
  687. int queue;
  688. u32 status;
  689. priv = netdev_priv(dev);
  690. status = cpmac_read(priv->regs, CPMAC_MAC_INT_VECTOR);
  691. if (unlikely(netif_msg_intr(priv)))
  692. netdev_dbg(dev, "interrupt status: 0x%08x\n", status);
  693. if (status & MAC_INT_TX)
  694. cpmac_end_xmit(dev, (status & 7));
  695. if (status & MAC_INT_RX) {
  696. queue = (status >> 8) & 7;
  697. if (napi_schedule_prep(&priv->napi)) {
  698. cpmac_write(priv->regs, CPMAC_RX_INT_CLEAR, 1 << queue);
  699. __napi_schedule(&priv->napi);
  700. }
  701. }
  702. cpmac_write(priv->regs, CPMAC_MAC_EOI_VECTOR, 0);
  703. if (unlikely(status & (MAC_INT_HOST | MAC_INT_STATUS)))
  704. cpmac_check_status(dev);
  705. return IRQ_HANDLED;
  706. }
  707. static void cpmac_tx_timeout(struct net_device *dev)
  708. {
  709. struct cpmac_priv *priv = netdev_priv(dev);
  710. spin_lock(&priv->lock);
  711. dev->stats.tx_errors++;
  712. spin_unlock(&priv->lock);
  713. if (netif_msg_tx_err(priv) && net_ratelimit())
  714. netdev_warn(dev, "transmit timeout\n");
  715. atomic_inc(&priv->reset_pending);
  716. barrier();
  717. cpmac_clear_tx(dev);
  718. barrier();
  719. atomic_dec(&priv->reset_pending);
  720. netif_tx_wake_all_queues(priv->dev);
  721. }
  722. static int cpmac_ioctl(struct net_device *dev, struct ifreq *ifr, int cmd)
  723. {
  724. if (!(netif_running(dev)))
  725. return -EINVAL;
  726. if (!dev->phydev)
  727. return -EINVAL;
  728. return phy_mii_ioctl(dev->phydev, ifr, cmd);
  729. }
  730. static void cpmac_get_ringparam(struct net_device *dev,
  731. struct ethtool_ringparam *ring)
  732. {
  733. struct cpmac_priv *priv = netdev_priv(dev);
  734. ring->rx_max_pending = 1024;
  735. ring->rx_mini_max_pending = 1;
  736. ring->rx_jumbo_max_pending = 1;
  737. ring->tx_max_pending = 1;
  738. ring->rx_pending = priv->ring_size;
  739. ring->rx_mini_pending = 1;
  740. ring->rx_jumbo_pending = 1;
  741. ring->tx_pending = 1;
  742. }
  743. static int cpmac_set_ringparam(struct net_device *dev,
  744. struct ethtool_ringparam *ring)
  745. {
  746. struct cpmac_priv *priv = netdev_priv(dev);
  747. if (netif_running(dev))
  748. return -EBUSY;
  749. priv->ring_size = ring->rx_pending;
  750. return 0;
  751. }
  752. static void cpmac_get_drvinfo(struct net_device *dev,
  753. struct ethtool_drvinfo *info)
  754. {
  755. strlcpy(info->driver, "cpmac", sizeof(info->driver));
  756. strlcpy(info->version, CPMAC_VERSION, sizeof(info->version));
  757. snprintf(info->bus_info, sizeof(info->bus_info), "%s", "cpmac");
  758. }
  759. static const struct ethtool_ops cpmac_ethtool_ops = {
  760. .get_drvinfo = cpmac_get_drvinfo,
  761. .get_link = ethtool_op_get_link,
  762. .get_ringparam = cpmac_get_ringparam,
  763. .set_ringparam = cpmac_set_ringparam,
  764. .get_link_ksettings = phy_ethtool_get_link_ksettings,
  765. .set_link_ksettings = phy_ethtool_set_link_ksettings,
  766. };
  767. static void cpmac_adjust_link(struct net_device *dev)
  768. {
  769. struct cpmac_priv *priv = netdev_priv(dev);
  770. int new_state = 0;
  771. spin_lock(&priv->lock);
  772. if (dev->phydev->link) {
  773. netif_tx_start_all_queues(dev);
  774. if (dev->phydev->duplex != priv->oldduplex) {
  775. new_state = 1;
  776. priv->oldduplex = dev->phydev->duplex;
  777. }
  778. if (dev->phydev->speed != priv->oldspeed) {
  779. new_state = 1;
  780. priv->oldspeed = dev->phydev->speed;
  781. }
  782. if (!priv->oldlink) {
  783. new_state = 1;
  784. priv->oldlink = 1;
  785. }
  786. } else if (priv->oldlink) {
  787. new_state = 1;
  788. priv->oldlink = 0;
  789. priv->oldspeed = 0;
  790. priv->oldduplex = -1;
  791. }
  792. if (new_state && netif_msg_link(priv) && net_ratelimit())
  793. phy_print_status(dev->phydev);
  794. spin_unlock(&priv->lock);
  795. }
  796. static int cpmac_open(struct net_device *dev)
  797. {
  798. int i, size, res;
  799. struct cpmac_priv *priv = netdev_priv(dev);
  800. struct resource *mem;
  801. struct cpmac_desc *desc;
  802. struct sk_buff *skb;
  803. mem = platform_get_resource_byname(priv->pdev, IORESOURCE_MEM, "regs");
  804. if (!request_mem_region(mem->start, resource_size(mem), dev->name)) {
  805. if (netif_msg_drv(priv))
  806. netdev_err(dev, "failed to request registers\n");
  807. res = -ENXIO;
  808. goto fail_reserve;
  809. }
  810. priv->regs = ioremap(mem->start, resource_size(mem));
  811. if (!priv->regs) {
  812. if (netif_msg_drv(priv))
  813. netdev_err(dev, "failed to remap registers\n");
  814. res = -ENXIO;
  815. goto fail_remap;
  816. }
  817. size = priv->ring_size + CPMAC_QUEUES;
  818. priv->desc_ring = dma_alloc_coherent(&dev->dev,
  819. sizeof(struct cpmac_desc) * size,
  820. &priv->dma_ring,
  821. GFP_KERNEL);
  822. if (!priv->desc_ring) {
  823. res = -ENOMEM;
  824. goto fail_alloc;
  825. }
  826. for (i = 0; i < size; i++)
  827. priv->desc_ring[i].mapping = priv->dma_ring + sizeof(*desc) * i;
  828. priv->rx_head = &priv->desc_ring[CPMAC_QUEUES];
  829. for (i = 0, desc = priv->rx_head; i < priv->ring_size; i++, desc++) {
  830. skb = netdev_alloc_skb_ip_align(dev, CPMAC_SKB_SIZE);
  831. if (unlikely(!skb)) {
  832. res = -ENOMEM;
  833. goto fail_desc;
  834. }
  835. desc->skb = skb;
  836. desc->data_mapping = dma_map_single(&dev->dev, skb->data,
  837. CPMAC_SKB_SIZE,
  838. DMA_FROM_DEVICE);
  839. desc->hw_data = (u32)desc->data_mapping;
  840. desc->buflen = CPMAC_SKB_SIZE;
  841. desc->dataflags = CPMAC_OWN;
  842. desc->next = &priv->rx_head[(i + 1) % priv->ring_size];
  843. desc->next->prev = desc;
  844. desc->hw_next = (u32)desc->next->mapping;
  845. }
  846. priv->rx_head->prev->hw_next = (u32)0;
  847. res = request_irq(dev->irq, cpmac_irq, IRQF_SHARED, dev->name, dev);
  848. if (res) {
  849. if (netif_msg_drv(priv))
  850. netdev_err(dev, "failed to obtain irq\n");
  851. goto fail_irq;
  852. }
  853. atomic_set(&priv->reset_pending, 0);
  854. INIT_WORK(&priv->reset_work, cpmac_hw_error);
  855. cpmac_hw_start(dev);
  856. napi_enable(&priv->napi);
  857. dev->phydev->state = PHY_CHANGELINK;
  858. phy_start(dev->phydev);
  859. return 0;
  860. fail_irq:
  861. fail_desc:
  862. for (i = 0; i < priv->ring_size; i++) {
  863. if (priv->rx_head[i].skb) {
  864. dma_unmap_single(&dev->dev,
  865. priv->rx_head[i].data_mapping,
  866. CPMAC_SKB_SIZE,
  867. DMA_FROM_DEVICE);
  868. kfree_skb(priv->rx_head[i].skb);
  869. }
  870. }
  871. dma_free_coherent(&dev->dev, sizeof(struct cpmac_desc) * size,
  872. priv->desc_ring, priv->dma_ring);
  873. fail_alloc:
  874. iounmap(priv->regs);
  875. fail_remap:
  876. release_mem_region(mem->start, resource_size(mem));
  877. fail_reserve:
  878. return res;
  879. }
  880. static int cpmac_stop(struct net_device *dev)
  881. {
  882. int i;
  883. struct cpmac_priv *priv = netdev_priv(dev);
  884. struct resource *mem;
  885. netif_tx_stop_all_queues(dev);
  886. cancel_work_sync(&priv->reset_work);
  887. napi_disable(&priv->napi);
  888. phy_stop(dev->phydev);
  889. cpmac_hw_stop(dev);
  890. for (i = 0; i < 8; i++)
  891. cpmac_write(priv->regs, CPMAC_TX_PTR(i), 0);
  892. cpmac_write(priv->regs, CPMAC_RX_PTR(0), 0);
  893. cpmac_write(priv->regs, CPMAC_MBP, 0);
  894. free_irq(dev->irq, dev);
  895. iounmap(priv->regs);
  896. mem = platform_get_resource_byname(priv->pdev, IORESOURCE_MEM, "regs");
  897. release_mem_region(mem->start, resource_size(mem));
  898. priv->rx_head = &priv->desc_ring[CPMAC_QUEUES];
  899. for (i = 0; i < priv->ring_size; i++) {
  900. if (priv->rx_head[i].skb) {
  901. dma_unmap_single(&dev->dev,
  902. priv->rx_head[i].data_mapping,
  903. CPMAC_SKB_SIZE,
  904. DMA_FROM_DEVICE);
  905. kfree_skb(priv->rx_head[i].skb);
  906. }
  907. }
  908. dma_free_coherent(&dev->dev, sizeof(struct cpmac_desc) *
  909. (CPMAC_QUEUES + priv->ring_size),
  910. priv->desc_ring, priv->dma_ring);
  911. return 0;
  912. }
  913. static const struct net_device_ops cpmac_netdev_ops = {
  914. .ndo_open = cpmac_open,
  915. .ndo_stop = cpmac_stop,
  916. .ndo_start_xmit = cpmac_start_xmit,
  917. .ndo_tx_timeout = cpmac_tx_timeout,
  918. .ndo_set_rx_mode = cpmac_set_multicast_list,
  919. .ndo_do_ioctl = cpmac_ioctl,
  920. .ndo_change_mtu = eth_change_mtu,
  921. .ndo_validate_addr = eth_validate_addr,
  922. .ndo_set_mac_address = eth_mac_addr,
  923. };
  924. static int external_switch;
  925. static int cpmac_probe(struct platform_device *pdev)
  926. {
  927. int rc, phy_id;
  928. char mdio_bus_id[MII_BUS_ID_SIZE];
  929. struct resource *mem;
  930. struct cpmac_priv *priv;
  931. struct net_device *dev;
  932. struct plat_cpmac_data *pdata;
  933. struct phy_device *phydev = NULL;
  934. pdata = dev_get_platdata(&pdev->dev);
  935. if (external_switch || dumb_switch) {
  936. strncpy(mdio_bus_id, "fixed-0", MII_BUS_ID_SIZE); /* fixed phys bus */
  937. phy_id = pdev->id;
  938. } else {
  939. for (phy_id = 0; phy_id < PHY_MAX_ADDR; phy_id++) {
  940. if (!(pdata->phy_mask & (1 << phy_id)))
  941. continue;
  942. if (!mdiobus_get_phy(cpmac_mii, phy_id))
  943. continue;
  944. strncpy(mdio_bus_id, cpmac_mii->id, MII_BUS_ID_SIZE);
  945. break;
  946. }
  947. }
  948. if (phy_id == PHY_MAX_ADDR) {
  949. dev_err(&pdev->dev, "no PHY present, falling back "
  950. "to switch on MDIO bus 0\n");
  951. strncpy(mdio_bus_id, "fixed-0", MII_BUS_ID_SIZE); /* fixed phys bus */
  952. phy_id = pdev->id;
  953. }
  954. mdio_bus_id[sizeof(mdio_bus_id) - 1] = '\0';
  955. dev = alloc_etherdev_mq(sizeof(*priv), CPMAC_QUEUES);
  956. if (!dev)
  957. return -ENOMEM;
  958. platform_set_drvdata(pdev, dev);
  959. priv = netdev_priv(dev);
  960. priv->pdev = pdev;
  961. mem = platform_get_resource_byname(pdev, IORESOURCE_MEM, "regs");
  962. if (!mem) {
  963. rc = -ENODEV;
  964. goto fail;
  965. }
  966. dev->irq = platform_get_irq_byname(pdev, "irq");
  967. dev->netdev_ops = &cpmac_netdev_ops;
  968. dev->ethtool_ops = &cpmac_ethtool_ops;
  969. netif_napi_add(dev, &priv->napi, cpmac_poll, 64);
  970. spin_lock_init(&priv->lock);
  971. spin_lock_init(&priv->rx_lock);
  972. priv->dev = dev;
  973. priv->ring_size = 64;
  974. priv->msg_enable = netif_msg_init(debug_level, 0xff);
  975. memcpy(dev->dev_addr, pdata->dev_addr, sizeof(pdata->dev_addr));
  976. snprintf(priv->phy_name, MII_BUS_ID_SIZE, PHY_ID_FMT,
  977. mdio_bus_id, phy_id);
  978. phydev = phy_connect(dev, priv->phy_name, cpmac_adjust_link,
  979. PHY_INTERFACE_MODE_MII);
  980. if (IS_ERR(phydev)) {
  981. if (netif_msg_drv(priv))
  982. dev_err(&pdev->dev, "Could not attach to PHY\n");
  983. rc = PTR_ERR(phydev);
  984. goto fail;
  985. }
  986. rc = register_netdev(dev);
  987. if (rc) {
  988. dev_err(&pdev->dev, "Could not register net device\n");
  989. goto fail;
  990. }
  991. if (netif_msg_probe(priv)) {
  992. dev_info(&pdev->dev, "regs: %p, irq: %d, phy: %s, "
  993. "mac: %pM\n", (void *)mem->start, dev->irq,
  994. priv->phy_name, dev->dev_addr);
  995. }
  996. return 0;
  997. fail:
  998. free_netdev(dev);
  999. return rc;
  1000. }
  1001. static int cpmac_remove(struct platform_device *pdev)
  1002. {
  1003. struct net_device *dev = platform_get_drvdata(pdev);
  1004. unregister_netdev(dev);
  1005. free_netdev(dev);
  1006. return 0;
  1007. }
  1008. static struct platform_driver cpmac_driver = {
  1009. .driver = {
  1010. .name = "cpmac",
  1011. },
  1012. .probe = cpmac_probe,
  1013. .remove = cpmac_remove,
  1014. };
  1015. int cpmac_init(void)
  1016. {
  1017. u32 mask;
  1018. int i, res;
  1019. cpmac_mii = mdiobus_alloc();
  1020. if (cpmac_mii == NULL)
  1021. return -ENOMEM;
  1022. cpmac_mii->name = "cpmac-mii";
  1023. cpmac_mii->read = cpmac_mdio_read;
  1024. cpmac_mii->write = cpmac_mdio_write;
  1025. cpmac_mii->reset = cpmac_mdio_reset;
  1026. cpmac_mii->priv = ioremap(AR7_REGS_MDIO, 256);
  1027. if (!cpmac_mii->priv) {
  1028. pr_err("Can't ioremap mdio registers\n");
  1029. res = -ENXIO;
  1030. goto fail_alloc;
  1031. }
  1032. #warning FIXME: unhardcode gpio&reset bits
  1033. ar7_gpio_disable(26);
  1034. ar7_gpio_disable(27);
  1035. ar7_device_reset(AR7_RESET_BIT_CPMAC_LO);
  1036. ar7_device_reset(AR7_RESET_BIT_CPMAC_HI);
  1037. ar7_device_reset(AR7_RESET_BIT_EPHY);
  1038. cpmac_mii->reset(cpmac_mii);
  1039. for (i = 0; i < 300; i++) {
  1040. mask = cpmac_read(cpmac_mii->priv, CPMAC_MDIO_ALIVE);
  1041. if (mask)
  1042. break;
  1043. else
  1044. msleep(10);
  1045. }
  1046. mask &= 0x7fffffff;
  1047. if (mask & (mask - 1)) {
  1048. external_switch = 1;
  1049. mask = 0;
  1050. }
  1051. cpmac_mii->phy_mask = ~(mask | 0x80000000);
  1052. snprintf(cpmac_mii->id, MII_BUS_ID_SIZE, "cpmac-1");
  1053. res = mdiobus_register(cpmac_mii);
  1054. if (res)
  1055. goto fail_mii;
  1056. res = platform_driver_register(&cpmac_driver);
  1057. if (res)
  1058. goto fail_cpmac;
  1059. return 0;
  1060. fail_cpmac:
  1061. mdiobus_unregister(cpmac_mii);
  1062. fail_mii:
  1063. iounmap(cpmac_mii->priv);
  1064. fail_alloc:
  1065. mdiobus_free(cpmac_mii);
  1066. return res;
  1067. }
  1068. void cpmac_exit(void)
  1069. {
  1070. platform_driver_unregister(&cpmac_driver);
  1071. mdiobus_unregister(cpmac_mii);
  1072. iounmap(cpmac_mii->priv);
  1073. mdiobus_free(cpmac_mii);
  1074. }
  1075. module_init(cpmac_init);
  1076. module_exit(cpmac_exit);