cpmac.c 35 KB

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