greth.c 40 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645
  1. /*
  2. * Aeroflex Gaisler GRETH 10/100/1G Ethernet MAC.
  3. *
  4. * 2005-2010 (c) Aeroflex Gaisler AB
  5. *
  6. * This driver supports GRETH 10/100 and GRETH 10/100/1G Ethernet MACs
  7. * available in the GRLIB VHDL IP core library.
  8. *
  9. * Full documentation of both cores can be found here:
  10. * http://www.gaisler.com/products/grlib/grip.pdf
  11. *
  12. * The Gigabit version supports scatter/gather DMA, any alignment of
  13. * buffers and checksum offloading.
  14. *
  15. * This program is free software; you can redistribute it and/or modify it
  16. * under the terms of the GNU General Public License as published by the
  17. * Free Software Foundation; either version 2 of the License, or (at your
  18. * option) any later version.
  19. *
  20. * Contributors: Kristoffer Glembo
  21. * Daniel Hellstrom
  22. * Marko Isomaki
  23. */
  24. #include <linux/dma-mapping.h>
  25. #include <linux/module.h>
  26. #include <linux/uaccess.h>
  27. #include <linux/init.h>
  28. #include <linux/interrupt.h>
  29. #include <linux/netdevice.h>
  30. #include <linux/etherdevice.h>
  31. #include <linux/ethtool.h>
  32. #include <linux/skbuff.h>
  33. #include <linux/io.h>
  34. #include <linux/crc32.h>
  35. #include <linux/mii.h>
  36. #include <linux/of_device.h>
  37. #include <linux/of_platform.h>
  38. #include <linux/slab.h>
  39. #include <asm/cacheflush.h>
  40. #include <asm/byteorder.h>
  41. #ifdef CONFIG_SPARC
  42. #include <asm/idprom.h>
  43. #endif
  44. #include "greth.h"
  45. #define GRETH_DEF_MSG_ENABLE \
  46. (NETIF_MSG_DRV | \
  47. NETIF_MSG_PROBE | \
  48. NETIF_MSG_LINK | \
  49. NETIF_MSG_IFDOWN | \
  50. NETIF_MSG_IFUP | \
  51. NETIF_MSG_RX_ERR | \
  52. NETIF_MSG_TX_ERR)
  53. static int greth_debug = -1; /* -1 == use GRETH_DEF_MSG_ENABLE as value */
  54. module_param(greth_debug, int, 0);
  55. MODULE_PARM_DESC(greth_debug, "GRETH bitmapped debugging message enable value");
  56. /* Accept MAC address of the form macaddr=0x08,0x00,0x20,0x30,0x40,0x50 */
  57. static int macaddr[6];
  58. module_param_array(macaddr, int, NULL, 0);
  59. MODULE_PARM_DESC(macaddr, "GRETH Ethernet MAC address");
  60. static int greth_edcl = 1;
  61. module_param(greth_edcl, int, 0);
  62. MODULE_PARM_DESC(greth_edcl, "GRETH EDCL usage indicator. Set to 1 if EDCL is used.");
  63. static int greth_open(struct net_device *dev);
  64. static netdev_tx_t greth_start_xmit(struct sk_buff *skb,
  65. struct net_device *dev);
  66. static netdev_tx_t greth_start_xmit_gbit(struct sk_buff *skb,
  67. struct net_device *dev);
  68. static int greth_rx(struct net_device *dev, int limit);
  69. static int greth_rx_gbit(struct net_device *dev, int limit);
  70. static void greth_clean_tx(struct net_device *dev);
  71. static void greth_clean_tx_gbit(struct net_device *dev);
  72. static irqreturn_t greth_interrupt(int irq, void *dev_id);
  73. static int greth_close(struct net_device *dev);
  74. static int greth_set_mac_add(struct net_device *dev, void *p);
  75. static void greth_set_multicast_list(struct net_device *dev);
  76. #define GRETH_REGLOAD(a) (be32_to_cpu(__raw_readl(&(a))))
  77. #define GRETH_REGSAVE(a, v) (__raw_writel(cpu_to_be32(v), &(a)))
  78. #define GRETH_REGORIN(a, v) (GRETH_REGSAVE(a, (GRETH_REGLOAD(a) | (v))))
  79. #define GRETH_REGANDIN(a, v) (GRETH_REGSAVE(a, (GRETH_REGLOAD(a) & (v))))
  80. #define NEXT_TX(N) (((N) + 1) & GRETH_TXBD_NUM_MASK)
  81. #define SKIP_TX(N, C) (((N) + C) & GRETH_TXBD_NUM_MASK)
  82. #define NEXT_RX(N) (((N) + 1) & GRETH_RXBD_NUM_MASK)
  83. static void greth_print_rx_packet(void *addr, int len)
  84. {
  85. print_hex_dump(KERN_DEBUG, "RX: ", DUMP_PREFIX_OFFSET, 16, 1,
  86. addr, len, true);
  87. }
  88. static void greth_print_tx_packet(struct sk_buff *skb)
  89. {
  90. int i;
  91. int length;
  92. if (skb_shinfo(skb)->nr_frags == 0)
  93. length = skb->len;
  94. else
  95. length = skb_headlen(skb);
  96. print_hex_dump(KERN_DEBUG, "TX: ", DUMP_PREFIX_OFFSET, 16, 1,
  97. skb->data, length, true);
  98. for (i = 0; i < skb_shinfo(skb)->nr_frags; i++) {
  99. print_hex_dump(KERN_DEBUG, "TX: ", DUMP_PREFIX_OFFSET, 16, 1,
  100. phys_to_virt(page_to_phys(skb_shinfo(skb)->frags[i].page)) +
  101. skb_shinfo(skb)->frags[i].page_offset,
  102. length, true);
  103. }
  104. }
  105. static inline void greth_enable_tx(struct greth_private *greth)
  106. {
  107. wmb();
  108. GRETH_REGORIN(greth->regs->control, GRETH_TXEN);
  109. }
  110. static inline void greth_disable_tx(struct greth_private *greth)
  111. {
  112. GRETH_REGANDIN(greth->regs->control, ~GRETH_TXEN);
  113. }
  114. static inline void greth_enable_rx(struct greth_private *greth)
  115. {
  116. wmb();
  117. GRETH_REGORIN(greth->regs->control, GRETH_RXEN);
  118. }
  119. static inline void greth_disable_rx(struct greth_private *greth)
  120. {
  121. GRETH_REGANDIN(greth->regs->control, ~GRETH_RXEN);
  122. }
  123. static inline void greth_enable_irqs(struct greth_private *greth)
  124. {
  125. GRETH_REGORIN(greth->regs->control, GRETH_RXI | GRETH_TXI);
  126. }
  127. static inline void greth_disable_irqs(struct greth_private *greth)
  128. {
  129. GRETH_REGANDIN(greth->regs->control, ~(GRETH_RXI|GRETH_TXI));
  130. }
  131. static inline void greth_write_bd(u32 *bd, u32 val)
  132. {
  133. __raw_writel(cpu_to_be32(val), bd);
  134. }
  135. static inline u32 greth_read_bd(u32 *bd)
  136. {
  137. return be32_to_cpu(__raw_readl(bd));
  138. }
  139. static void greth_clean_rings(struct greth_private *greth)
  140. {
  141. int i;
  142. struct greth_bd *rx_bdp = greth->rx_bd_base;
  143. struct greth_bd *tx_bdp = greth->tx_bd_base;
  144. if (greth->gbit_mac) {
  145. /* Free and unmap RX buffers */
  146. for (i = 0; i < GRETH_RXBD_NUM; i++, rx_bdp++) {
  147. if (greth->rx_skbuff[i] != NULL) {
  148. dev_kfree_skb(greth->rx_skbuff[i]);
  149. dma_unmap_single(greth->dev,
  150. greth_read_bd(&rx_bdp->addr),
  151. MAX_FRAME_SIZE+NET_IP_ALIGN,
  152. DMA_FROM_DEVICE);
  153. }
  154. }
  155. /* TX buffers */
  156. while (greth->tx_free < GRETH_TXBD_NUM) {
  157. struct sk_buff *skb = greth->tx_skbuff[greth->tx_last];
  158. int nr_frags = skb_shinfo(skb)->nr_frags;
  159. tx_bdp = greth->tx_bd_base + greth->tx_last;
  160. greth->tx_last = NEXT_TX(greth->tx_last);
  161. dma_unmap_single(greth->dev,
  162. greth_read_bd(&tx_bdp->addr),
  163. skb_headlen(skb),
  164. DMA_TO_DEVICE);
  165. for (i = 0; i < nr_frags; i++) {
  166. skb_frag_t *frag = &skb_shinfo(skb)->frags[i];
  167. tx_bdp = greth->tx_bd_base + greth->tx_last;
  168. dma_unmap_page(greth->dev,
  169. greth_read_bd(&tx_bdp->addr),
  170. frag->size,
  171. DMA_TO_DEVICE);
  172. greth->tx_last = NEXT_TX(greth->tx_last);
  173. }
  174. greth->tx_free += nr_frags+1;
  175. dev_kfree_skb(skb);
  176. }
  177. } else { /* 10/100 Mbps MAC */
  178. for (i = 0; i < GRETH_RXBD_NUM; i++, rx_bdp++) {
  179. kfree(greth->rx_bufs[i]);
  180. dma_unmap_single(greth->dev,
  181. greth_read_bd(&rx_bdp->addr),
  182. MAX_FRAME_SIZE,
  183. DMA_FROM_DEVICE);
  184. }
  185. for (i = 0; i < GRETH_TXBD_NUM; i++, tx_bdp++) {
  186. kfree(greth->tx_bufs[i]);
  187. dma_unmap_single(greth->dev,
  188. greth_read_bd(&tx_bdp->addr),
  189. MAX_FRAME_SIZE,
  190. DMA_TO_DEVICE);
  191. }
  192. }
  193. }
  194. static int greth_init_rings(struct greth_private *greth)
  195. {
  196. struct sk_buff *skb;
  197. struct greth_bd *rx_bd, *tx_bd;
  198. u32 dma_addr;
  199. int i;
  200. rx_bd = greth->rx_bd_base;
  201. tx_bd = greth->tx_bd_base;
  202. /* Initialize descriptor rings and buffers */
  203. if (greth->gbit_mac) {
  204. for (i = 0; i < GRETH_RXBD_NUM; i++) {
  205. skb = netdev_alloc_skb(greth->netdev, MAX_FRAME_SIZE+NET_IP_ALIGN);
  206. if (skb == NULL) {
  207. if (netif_msg_ifup(greth))
  208. dev_err(greth->dev, "Error allocating DMA ring.\n");
  209. goto cleanup;
  210. }
  211. skb_reserve(skb, NET_IP_ALIGN);
  212. dma_addr = dma_map_single(greth->dev,
  213. skb->data,
  214. MAX_FRAME_SIZE+NET_IP_ALIGN,
  215. DMA_FROM_DEVICE);
  216. if (dma_mapping_error(greth->dev, dma_addr)) {
  217. if (netif_msg_ifup(greth))
  218. dev_err(greth->dev, "Could not create initial DMA mapping\n");
  219. goto cleanup;
  220. }
  221. greth->rx_skbuff[i] = skb;
  222. greth_write_bd(&rx_bd[i].addr, dma_addr);
  223. greth_write_bd(&rx_bd[i].stat, GRETH_BD_EN | GRETH_BD_IE);
  224. }
  225. } else {
  226. /* 10/100 MAC uses a fixed set of buffers and copy to/from SKBs */
  227. for (i = 0; i < GRETH_RXBD_NUM; i++) {
  228. greth->rx_bufs[i] = kmalloc(MAX_FRAME_SIZE, GFP_KERNEL);
  229. if (greth->rx_bufs[i] == NULL) {
  230. if (netif_msg_ifup(greth))
  231. dev_err(greth->dev, "Error allocating DMA ring.\n");
  232. goto cleanup;
  233. }
  234. dma_addr = dma_map_single(greth->dev,
  235. greth->rx_bufs[i],
  236. MAX_FRAME_SIZE,
  237. DMA_FROM_DEVICE);
  238. if (dma_mapping_error(greth->dev, dma_addr)) {
  239. if (netif_msg_ifup(greth))
  240. dev_err(greth->dev, "Could not create initial DMA mapping\n");
  241. goto cleanup;
  242. }
  243. greth_write_bd(&rx_bd[i].addr, dma_addr);
  244. greth_write_bd(&rx_bd[i].stat, GRETH_BD_EN | GRETH_BD_IE);
  245. }
  246. for (i = 0; i < GRETH_TXBD_NUM; i++) {
  247. greth->tx_bufs[i] = kmalloc(MAX_FRAME_SIZE, GFP_KERNEL);
  248. if (greth->tx_bufs[i] == NULL) {
  249. if (netif_msg_ifup(greth))
  250. dev_err(greth->dev, "Error allocating DMA ring.\n");
  251. goto cleanup;
  252. }
  253. dma_addr = dma_map_single(greth->dev,
  254. greth->tx_bufs[i],
  255. MAX_FRAME_SIZE,
  256. DMA_TO_DEVICE);
  257. if (dma_mapping_error(greth->dev, dma_addr)) {
  258. if (netif_msg_ifup(greth))
  259. dev_err(greth->dev, "Could not create initial DMA mapping\n");
  260. goto cleanup;
  261. }
  262. greth_write_bd(&tx_bd[i].addr, dma_addr);
  263. greth_write_bd(&tx_bd[i].stat, 0);
  264. }
  265. }
  266. greth_write_bd(&rx_bd[GRETH_RXBD_NUM - 1].stat,
  267. greth_read_bd(&rx_bd[GRETH_RXBD_NUM - 1].stat) | GRETH_BD_WR);
  268. /* Initialize pointers. */
  269. greth->rx_cur = 0;
  270. greth->tx_next = 0;
  271. greth->tx_last = 0;
  272. greth->tx_free = GRETH_TXBD_NUM;
  273. /* Initialize descriptor base address */
  274. GRETH_REGSAVE(greth->regs->tx_desc_p, greth->tx_bd_base_phys);
  275. GRETH_REGSAVE(greth->regs->rx_desc_p, greth->rx_bd_base_phys);
  276. return 0;
  277. cleanup:
  278. greth_clean_rings(greth);
  279. return -ENOMEM;
  280. }
  281. static int greth_open(struct net_device *dev)
  282. {
  283. struct greth_private *greth = netdev_priv(dev);
  284. int err;
  285. err = greth_init_rings(greth);
  286. if (err) {
  287. if (netif_msg_ifup(greth))
  288. dev_err(&dev->dev, "Could not allocate memory for DMA rings\n");
  289. return err;
  290. }
  291. err = request_irq(greth->irq, greth_interrupt, 0, "eth", (void *) dev);
  292. if (err) {
  293. if (netif_msg_ifup(greth))
  294. dev_err(&dev->dev, "Could not allocate interrupt %d\n", dev->irq);
  295. greth_clean_rings(greth);
  296. return err;
  297. }
  298. if (netif_msg_ifup(greth))
  299. dev_dbg(&dev->dev, " starting queue\n");
  300. netif_start_queue(dev);
  301. GRETH_REGSAVE(greth->regs->status, 0xFF);
  302. napi_enable(&greth->napi);
  303. greth_enable_irqs(greth);
  304. greth_enable_tx(greth);
  305. greth_enable_rx(greth);
  306. return 0;
  307. }
  308. static int greth_close(struct net_device *dev)
  309. {
  310. struct greth_private *greth = netdev_priv(dev);
  311. napi_disable(&greth->napi);
  312. greth_disable_irqs(greth);
  313. greth_disable_tx(greth);
  314. greth_disable_rx(greth);
  315. netif_stop_queue(dev);
  316. free_irq(greth->irq, (void *) dev);
  317. greth_clean_rings(greth);
  318. return 0;
  319. }
  320. static netdev_tx_t
  321. greth_start_xmit(struct sk_buff *skb, struct net_device *dev)
  322. {
  323. struct greth_private *greth = netdev_priv(dev);
  324. struct greth_bd *bdp;
  325. int err = NETDEV_TX_OK;
  326. u32 status, dma_addr, ctrl;
  327. unsigned long flags;
  328. /* Clean TX Ring */
  329. greth_clean_tx(greth->netdev);
  330. if (unlikely(greth->tx_free <= 0)) {
  331. spin_lock_irqsave(&greth->devlock, flags);/*save from poll/irq*/
  332. ctrl = GRETH_REGLOAD(greth->regs->control);
  333. /* Enable TX IRQ only if not already in poll() routine */
  334. if (ctrl & GRETH_RXI)
  335. GRETH_REGSAVE(greth->regs->control, ctrl | GRETH_TXI);
  336. netif_stop_queue(dev);
  337. spin_unlock_irqrestore(&greth->devlock, flags);
  338. return NETDEV_TX_BUSY;
  339. }
  340. if (netif_msg_pktdata(greth))
  341. greth_print_tx_packet(skb);
  342. if (unlikely(skb->len > MAX_FRAME_SIZE)) {
  343. dev->stats.tx_errors++;
  344. goto out;
  345. }
  346. bdp = greth->tx_bd_base + greth->tx_next;
  347. dma_addr = greth_read_bd(&bdp->addr);
  348. memcpy((unsigned char *) phys_to_virt(dma_addr), skb->data, skb->len);
  349. dma_sync_single_for_device(greth->dev, dma_addr, skb->len, DMA_TO_DEVICE);
  350. status = GRETH_BD_EN | GRETH_BD_IE | (skb->len & GRETH_BD_LEN);
  351. greth->tx_bufs_length[greth->tx_next] = skb->len & GRETH_BD_LEN;
  352. /* Wrap around descriptor ring */
  353. if (greth->tx_next == GRETH_TXBD_NUM_MASK) {
  354. status |= GRETH_BD_WR;
  355. }
  356. greth->tx_next = NEXT_TX(greth->tx_next);
  357. greth->tx_free--;
  358. /* Write descriptor control word and enable transmission */
  359. greth_write_bd(&bdp->stat, status);
  360. spin_lock_irqsave(&greth->devlock, flags); /*save from poll/irq*/
  361. greth_enable_tx(greth);
  362. spin_unlock_irqrestore(&greth->devlock, flags);
  363. out:
  364. dev_kfree_skb(skb);
  365. return err;
  366. }
  367. static netdev_tx_t
  368. greth_start_xmit_gbit(struct sk_buff *skb, struct net_device *dev)
  369. {
  370. struct greth_private *greth = netdev_priv(dev);
  371. struct greth_bd *bdp;
  372. u32 status = 0, dma_addr, ctrl;
  373. int curr_tx, nr_frags, i, err = NETDEV_TX_OK;
  374. unsigned long flags;
  375. nr_frags = skb_shinfo(skb)->nr_frags;
  376. /* Clean TX Ring */
  377. greth_clean_tx_gbit(dev);
  378. if (greth->tx_free < nr_frags + 1) {
  379. spin_lock_irqsave(&greth->devlock, flags);/*save from poll/irq*/
  380. ctrl = GRETH_REGLOAD(greth->regs->control);
  381. /* Enable TX IRQ only if not already in poll() routine */
  382. if (ctrl & GRETH_RXI)
  383. GRETH_REGSAVE(greth->regs->control, ctrl | GRETH_TXI);
  384. netif_stop_queue(dev);
  385. spin_unlock_irqrestore(&greth->devlock, flags);
  386. err = NETDEV_TX_BUSY;
  387. goto out;
  388. }
  389. if (netif_msg_pktdata(greth))
  390. greth_print_tx_packet(skb);
  391. if (unlikely(skb->len > MAX_FRAME_SIZE)) {
  392. dev->stats.tx_errors++;
  393. goto out;
  394. }
  395. /* Save skb pointer. */
  396. greth->tx_skbuff[greth->tx_next] = skb;
  397. /* Linear buf */
  398. if (nr_frags != 0)
  399. status = GRETH_TXBD_MORE;
  400. if (skb->ip_summed == CHECKSUM_PARTIAL)
  401. status |= GRETH_TXBD_CSALL;
  402. status |= skb_headlen(skb) & GRETH_BD_LEN;
  403. if (greth->tx_next == GRETH_TXBD_NUM_MASK)
  404. status |= GRETH_BD_WR;
  405. bdp = greth->tx_bd_base + greth->tx_next;
  406. greth_write_bd(&bdp->stat, status);
  407. dma_addr = dma_map_single(greth->dev, skb->data, skb_headlen(skb), DMA_TO_DEVICE);
  408. if (unlikely(dma_mapping_error(greth->dev, dma_addr)))
  409. goto map_error;
  410. greth_write_bd(&bdp->addr, dma_addr);
  411. curr_tx = NEXT_TX(greth->tx_next);
  412. /* Frags */
  413. for (i = 0; i < nr_frags; i++) {
  414. skb_frag_t *frag = &skb_shinfo(skb)->frags[i];
  415. greth->tx_skbuff[curr_tx] = NULL;
  416. bdp = greth->tx_bd_base + curr_tx;
  417. status = GRETH_BD_EN;
  418. if (skb->ip_summed == CHECKSUM_PARTIAL)
  419. status |= GRETH_TXBD_CSALL;
  420. status |= frag->size & GRETH_BD_LEN;
  421. /* Wrap around descriptor ring */
  422. if (curr_tx == GRETH_TXBD_NUM_MASK)
  423. status |= GRETH_BD_WR;
  424. /* More fragments left */
  425. if (i < nr_frags - 1)
  426. status |= GRETH_TXBD_MORE;
  427. else
  428. status |= GRETH_BD_IE; /* enable IRQ on last fragment */
  429. greth_write_bd(&bdp->stat, status);
  430. dma_addr = dma_map_page(greth->dev,
  431. frag->page,
  432. frag->page_offset,
  433. frag->size,
  434. DMA_TO_DEVICE);
  435. if (unlikely(dma_mapping_error(greth->dev, dma_addr)))
  436. goto frag_map_error;
  437. greth_write_bd(&bdp->addr, dma_addr);
  438. curr_tx = NEXT_TX(curr_tx);
  439. }
  440. wmb();
  441. /* Enable the descriptor chain by enabling the first descriptor */
  442. bdp = greth->tx_bd_base + greth->tx_next;
  443. greth_write_bd(&bdp->stat, greth_read_bd(&bdp->stat) | GRETH_BD_EN);
  444. greth->tx_next = curr_tx;
  445. greth->tx_free -= nr_frags + 1;
  446. wmb();
  447. spin_lock_irqsave(&greth->devlock, flags); /*save from poll/irq*/
  448. greth_enable_tx(greth);
  449. spin_unlock_irqrestore(&greth->devlock, flags);
  450. return NETDEV_TX_OK;
  451. frag_map_error:
  452. /* Unmap SKB mappings that succeeded and disable descriptor */
  453. for (i = 0; greth->tx_next + i != curr_tx; i++) {
  454. bdp = greth->tx_bd_base + greth->tx_next + i;
  455. dma_unmap_single(greth->dev,
  456. greth_read_bd(&bdp->addr),
  457. greth_read_bd(&bdp->stat) & GRETH_BD_LEN,
  458. DMA_TO_DEVICE);
  459. greth_write_bd(&bdp->stat, 0);
  460. }
  461. map_error:
  462. if (net_ratelimit())
  463. dev_warn(greth->dev, "Could not create TX DMA mapping\n");
  464. dev_kfree_skb(skb);
  465. out:
  466. return err;
  467. }
  468. static irqreturn_t greth_interrupt(int irq, void *dev_id)
  469. {
  470. struct net_device *dev = dev_id;
  471. struct greth_private *greth;
  472. u32 status, ctrl;
  473. irqreturn_t retval = IRQ_NONE;
  474. greth = netdev_priv(dev);
  475. spin_lock(&greth->devlock);
  476. /* Get the interrupt events that caused us to be here. */
  477. status = GRETH_REGLOAD(greth->regs->status);
  478. /* Must see if interrupts are enabled also, INT_TX|INT_RX flags may be
  479. * set regardless of whether IRQ is enabled or not. Especially
  480. * important when shared IRQ.
  481. */
  482. ctrl = GRETH_REGLOAD(greth->regs->control);
  483. /* Handle rx and tx interrupts through poll */
  484. if (((status & (GRETH_INT_RE | GRETH_INT_RX)) && (ctrl & GRETH_RXI)) ||
  485. ((status & (GRETH_INT_TE | GRETH_INT_TX)) && (ctrl & GRETH_TXI))) {
  486. retval = IRQ_HANDLED;
  487. /* Disable interrupts and schedule poll() */
  488. greth_disable_irqs(greth);
  489. napi_schedule(&greth->napi);
  490. }
  491. mmiowb();
  492. spin_unlock(&greth->devlock);
  493. return retval;
  494. }
  495. static void greth_clean_tx(struct net_device *dev)
  496. {
  497. struct greth_private *greth;
  498. struct greth_bd *bdp;
  499. u32 stat;
  500. greth = netdev_priv(dev);
  501. while (1) {
  502. bdp = greth->tx_bd_base + greth->tx_last;
  503. GRETH_REGSAVE(greth->regs->status, GRETH_INT_TE | GRETH_INT_TX);
  504. mb();
  505. stat = greth_read_bd(&bdp->stat);
  506. if (unlikely(stat & GRETH_BD_EN))
  507. break;
  508. if (greth->tx_free == GRETH_TXBD_NUM)
  509. break;
  510. /* Check status for errors */
  511. if (unlikely(stat & GRETH_TXBD_STATUS)) {
  512. dev->stats.tx_errors++;
  513. if (stat & GRETH_TXBD_ERR_AL)
  514. dev->stats.tx_aborted_errors++;
  515. if (stat & GRETH_TXBD_ERR_UE)
  516. dev->stats.tx_fifo_errors++;
  517. }
  518. dev->stats.tx_packets++;
  519. dev->stats.tx_bytes += greth->tx_bufs_length[greth->tx_last];
  520. greth->tx_last = NEXT_TX(greth->tx_last);
  521. greth->tx_free++;
  522. }
  523. if (greth->tx_free > 0) {
  524. netif_wake_queue(dev);
  525. }
  526. }
  527. static inline void greth_update_tx_stats(struct net_device *dev, u32 stat)
  528. {
  529. /* Check status for errors */
  530. if (unlikely(stat & GRETH_TXBD_STATUS)) {
  531. dev->stats.tx_errors++;
  532. if (stat & GRETH_TXBD_ERR_AL)
  533. dev->stats.tx_aborted_errors++;
  534. if (stat & GRETH_TXBD_ERR_UE)
  535. dev->stats.tx_fifo_errors++;
  536. if (stat & GRETH_TXBD_ERR_LC)
  537. dev->stats.tx_aborted_errors++;
  538. }
  539. dev->stats.tx_packets++;
  540. }
  541. static void greth_clean_tx_gbit(struct net_device *dev)
  542. {
  543. struct greth_private *greth;
  544. struct greth_bd *bdp, *bdp_last_frag;
  545. struct sk_buff *skb;
  546. u32 stat;
  547. int nr_frags, i;
  548. greth = netdev_priv(dev);
  549. while (greth->tx_free < GRETH_TXBD_NUM) {
  550. skb = greth->tx_skbuff[greth->tx_last];
  551. nr_frags = skb_shinfo(skb)->nr_frags;
  552. /* We only clean fully completed SKBs */
  553. bdp_last_frag = greth->tx_bd_base + SKIP_TX(greth->tx_last, nr_frags);
  554. GRETH_REGSAVE(greth->regs->status, GRETH_INT_TE | GRETH_INT_TX);
  555. mb();
  556. stat = greth_read_bd(&bdp_last_frag->stat);
  557. if (stat & GRETH_BD_EN)
  558. break;
  559. greth->tx_skbuff[greth->tx_last] = NULL;
  560. greth_update_tx_stats(dev, stat);
  561. dev->stats.tx_bytes += skb->len;
  562. bdp = greth->tx_bd_base + greth->tx_last;
  563. greth->tx_last = NEXT_TX(greth->tx_last);
  564. dma_unmap_single(greth->dev,
  565. greth_read_bd(&bdp->addr),
  566. skb_headlen(skb),
  567. DMA_TO_DEVICE);
  568. for (i = 0; i < nr_frags; i++) {
  569. skb_frag_t *frag = &skb_shinfo(skb)->frags[i];
  570. bdp = greth->tx_bd_base + greth->tx_last;
  571. dma_unmap_page(greth->dev,
  572. greth_read_bd(&bdp->addr),
  573. frag->size,
  574. DMA_TO_DEVICE);
  575. greth->tx_last = NEXT_TX(greth->tx_last);
  576. }
  577. greth->tx_free += nr_frags+1;
  578. dev_kfree_skb(skb);
  579. }
  580. if (netif_queue_stopped(dev) && (greth->tx_free > (MAX_SKB_FRAGS+1)))
  581. netif_wake_queue(dev);
  582. }
  583. static int greth_rx(struct net_device *dev, int limit)
  584. {
  585. struct greth_private *greth;
  586. struct greth_bd *bdp;
  587. struct sk_buff *skb;
  588. int pkt_len;
  589. int bad, count;
  590. u32 status, dma_addr;
  591. unsigned long flags;
  592. greth = netdev_priv(dev);
  593. for (count = 0; count < limit; ++count) {
  594. bdp = greth->rx_bd_base + greth->rx_cur;
  595. GRETH_REGSAVE(greth->regs->status, GRETH_INT_RE | GRETH_INT_RX);
  596. mb();
  597. status = greth_read_bd(&bdp->stat);
  598. if (unlikely(status & GRETH_BD_EN)) {
  599. break;
  600. }
  601. dma_addr = greth_read_bd(&bdp->addr);
  602. bad = 0;
  603. /* Check status for errors. */
  604. if (unlikely(status & GRETH_RXBD_STATUS)) {
  605. if (status & GRETH_RXBD_ERR_FT) {
  606. dev->stats.rx_length_errors++;
  607. bad = 1;
  608. }
  609. if (status & (GRETH_RXBD_ERR_AE | GRETH_RXBD_ERR_OE)) {
  610. dev->stats.rx_frame_errors++;
  611. bad = 1;
  612. }
  613. if (status & GRETH_RXBD_ERR_CRC) {
  614. dev->stats.rx_crc_errors++;
  615. bad = 1;
  616. }
  617. }
  618. if (unlikely(bad)) {
  619. dev->stats.rx_errors++;
  620. } else {
  621. pkt_len = status & GRETH_BD_LEN;
  622. skb = netdev_alloc_skb(dev, pkt_len + NET_IP_ALIGN);
  623. if (unlikely(skb == NULL)) {
  624. if (net_ratelimit())
  625. dev_warn(&dev->dev, "low on memory - " "packet dropped\n");
  626. dev->stats.rx_dropped++;
  627. } else {
  628. skb_reserve(skb, NET_IP_ALIGN);
  629. skb->dev = dev;
  630. dma_sync_single_for_cpu(greth->dev,
  631. dma_addr,
  632. pkt_len,
  633. DMA_FROM_DEVICE);
  634. if (netif_msg_pktdata(greth))
  635. greth_print_rx_packet(phys_to_virt(dma_addr), pkt_len);
  636. memcpy(skb_put(skb, pkt_len), phys_to_virt(dma_addr), pkt_len);
  637. skb->protocol = eth_type_trans(skb, dev);
  638. dev->stats.rx_bytes += pkt_len;
  639. dev->stats.rx_packets++;
  640. netif_receive_skb(skb);
  641. }
  642. }
  643. status = GRETH_BD_EN | GRETH_BD_IE;
  644. if (greth->rx_cur == GRETH_RXBD_NUM_MASK) {
  645. status |= GRETH_BD_WR;
  646. }
  647. wmb();
  648. greth_write_bd(&bdp->stat, status);
  649. dma_sync_single_for_device(greth->dev, dma_addr, MAX_FRAME_SIZE, DMA_FROM_DEVICE);
  650. spin_lock_irqsave(&greth->devlock, flags); /* save from XMIT */
  651. greth_enable_rx(greth);
  652. spin_unlock_irqrestore(&greth->devlock, flags);
  653. greth->rx_cur = NEXT_RX(greth->rx_cur);
  654. }
  655. return count;
  656. }
  657. static inline int hw_checksummed(u32 status)
  658. {
  659. if (status & GRETH_RXBD_IP_FRAG)
  660. return 0;
  661. if (status & GRETH_RXBD_IP && status & GRETH_RXBD_IP_CSERR)
  662. return 0;
  663. if (status & GRETH_RXBD_UDP && status & GRETH_RXBD_UDP_CSERR)
  664. return 0;
  665. if (status & GRETH_RXBD_TCP && status & GRETH_RXBD_TCP_CSERR)
  666. return 0;
  667. return 1;
  668. }
  669. static int greth_rx_gbit(struct net_device *dev, int limit)
  670. {
  671. struct greth_private *greth;
  672. struct greth_bd *bdp;
  673. struct sk_buff *skb, *newskb;
  674. int pkt_len;
  675. int bad, count = 0;
  676. u32 status, dma_addr;
  677. unsigned long flags;
  678. greth = netdev_priv(dev);
  679. for (count = 0; count < limit; ++count) {
  680. bdp = greth->rx_bd_base + greth->rx_cur;
  681. skb = greth->rx_skbuff[greth->rx_cur];
  682. GRETH_REGSAVE(greth->regs->status, GRETH_INT_RE | GRETH_INT_RX);
  683. mb();
  684. status = greth_read_bd(&bdp->stat);
  685. bad = 0;
  686. if (status & GRETH_BD_EN)
  687. break;
  688. /* Check status for errors. */
  689. if (unlikely(status & GRETH_RXBD_STATUS)) {
  690. if (status & GRETH_RXBD_ERR_FT) {
  691. dev->stats.rx_length_errors++;
  692. bad = 1;
  693. } else if (status &
  694. (GRETH_RXBD_ERR_AE | GRETH_RXBD_ERR_OE | GRETH_RXBD_ERR_LE)) {
  695. dev->stats.rx_frame_errors++;
  696. bad = 1;
  697. } else if (status & GRETH_RXBD_ERR_CRC) {
  698. dev->stats.rx_crc_errors++;
  699. bad = 1;
  700. }
  701. }
  702. /* Allocate new skb to replace current, not needed if the
  703. * current skb can be reused */
  704. if (!bad && (newskb=netdev_alloc_skb(dev, MAX_FRAME_SIZE + NET_IP_ALIGN))) {
  705. skb_reserve(newskb, NET_IP_ALIGN);
  706. dma_addr = dma_map_single(greth->dev,
  707. newskb->data,
  708. MAX_FRAME_SIZE + NET_IP_ALIGN,
  709. DMA_FROM_DEVICE);
  710. if (!dma_mapping_error(greth->dev, dma_addr)) {
  711. /* Process the incoming frame. */
  712. pkt_len = status & GRETH_BD_LEN;
  713. dma_unmap_single(greth->dev,
  714. greth_read_bd(&bdp->addr),
  715. MAX_FRAME_SIZE + NET_IP_ALIGN,
  716. DMA_FROM_DEVICE);
  717. if (netif_msg_pktdata(greth))
  718. greth_print_rx_packet(phys_to_virt(greth_read_bd(&bdp->addr)), pkt_len);
  719. skb_put(skb, pkt_len);
  720. if (dev->features & NETIF_F_RXCSUM && hw_checksummed(status))
  721. skb->ip_summed = CHECKSUM_UNNECESSARY;
  722. else
  723. skb_checksum_none_assert(skb);
  724. skb->protocol = eth_type_trans(skb, dev);
  725. dev->stats.rx_packets++;
  726. dev->stats.rx_bytes += pkt_len;
  727. netif_receive_skb(skb);
  728. greth->rx_skbuff[greth->rx_cur] = newskb;
  729. greth_write_bd(&bdp->addr, dma_addr);
  730. } else {
  731. if (net_ratelimit())
  732. dev_warn(greth->dev, "Could not create DMA mapping, dropping packet\n");
  733. dev_kfree_skb(newskb);
  734. /* reusing current skb, so it is a drop */
  735. dev->stats.rx_dropped++;
  736. }
  737. } else if (bad) {
  738. /* Bad Frame transfer, the skb is reused */
  739. dev->stats.rx_dropped++;
  740. } else {
  741. /* Failed Allocating a new skb. This is rather stupid
  742. * but the current "filled" skb is reused, as if
  743. * transfer failure. One could argue that RX descriptor
  744. * table handling should be divided into cleaning and
  745. * filling as the TX part of the driver
  746. */
  747. if (net_ratelimit())
  748. dev_warn(greth->dev, "Could not allocate SKB, dropping packet\n");
  749. /* reusing current skb, so it is a drop */
  750. dev->stats.rx_dropped++;
  751. }
  752. status = GRETH_BD_EN | GRETH_BD_IE;
  753. if (greth->rx_cur == GRETH_RXBD_NUM_MASK) {
  754. status |= GRETH_BD_WR;
  755. }
  756. wmb();
  757. greth_write_bd(&bdp->stat, status);
  758. spin_lock_irqsave(&greth->devlock, flags);
  759. greth_enable_rx(greth);
  760. spin_unlock_irqrestore(&greth->devlock, flags);
  761. greth->rx_cur = NEXT_RX(greth->rx_cur);
  762. }
  763. return count;
  764. }
  765. static int greth_poll(struct napi_struct *napi, int budget)
  766. {
  767. struct greth_private *greth;
  768. int work_done = 0;
  769. unsigned long flags;
  770. u32 mask, ctrl;
  771. greth = container_of(napi, struct greth_private, napi);
  772. restart_txrx_poll:
  773. if (netif_queue_stopped(greth->netdev)) {
  774. if (greth->gbit_mac)
  775. greth_clean_tx_gbit(greth->netdev);
  776. else
  777. greth_clean_tx(greth->netdev);
  778. }
  779. if (greth->gbit_mac) {
  780. work_done += greth_rx_gbit(greth->netdev, budget - work_done);
  781. } else {
  782. work_done += greth_rx(greth->netdev, budget - work_done);
  783. }
  784. if (work_done < budget) {
  785. spin_lock_irqsave(&greth->devlock, flags);
  786. ctrl = GRETH_REGLOAD(greth->regs->control);
  787. if (netif_queue_stopped(greth->netdev)) {
  788. GRETH_REGSAVE(greth->regs->control,
  789. ctrl | GRETH_TXI | GRETH_RXI);
  790. mask = GRETH_INT_RX | GRETH_INT_RE |
  791. GRETH_INT_TX | GRETH_INT_TE;
  792. } else {
  793. GRETH_REGSAVE(greth->regs->control, ctrl | GRETH_RXI);
  794. mask = GRETH_INT_RX | GRETH_INT_RE;
  795. }
  796. if (GRETH_REGLOAD(greth->regs->status) & mask) {
  797. GRETH_REGSAVE(greth->regs->control, ctrl);
  798. spin_unlock_irqrestore(&greth->devlock, flags);
  799. goto restart_txrx_poll;
  800. } else {
  801. __napi_complete(napi);
  802. spin_unlock_irqrestore(&greth->devlock, flags);
  803. }
  804. }
  805. return work_done;
  806. }
  807. static int greth_set_mac_add(struct net_device *dev, void *p)
  808. {
  809. struct sockaddr *addr = p;
  810. struct greth_private *greth;
  811. struct greth_regs *regs;
  812. greth = netdev_priv(dev);
  813. regs = (struct greth_regs *) greth->regs;
  814. if (!is_valid_ether_addr(addr->sa_data))
  815. return -EINVAL;
  816. memcpy(dev->dev_addr, addr->sa_data, dev->addr_len);
  817. GRETH_REGSAVE(regs->esa_msb, dev->dev_addr[0] << 8 | dev->dev_addr[1]);
  818. GRETH_REGSAVE(regs->esa_lsb, dev->dev_addr[2] << 24 | dev->dev_addr[3] << 16 |
  819. dev->dev_addr[4] << 8 | dev->dev_addr[5]);
  820. return 0;
  821. }
  822. static u32 greth_hash_get_index(__u8 *addr)
  823. {
  824. return (ether_crc(6, addr)) & 0x3F;
  825. }
  826. static void greth_set_hash_filter(struct net_device *dev)
  827. {
  828. struct netdev_hw_addr *ha;
  829. struct greth_private *greth = netdev_priv(dev);
  830. struct greth_regs *regs = (struct greth_regs *) greth->regs;
  831. u32 mc_filter[2];
  832. unsigned int bitnr;
  833. mc_filter[0] = mc_filter[1] = 0;
  834. netdev_for_each_mc_addr(ha, dev) {
  835. bitnr = greth_hash_get_index(ha->addr);
  836. mc_filter[bitnr >> 5] |= 1 << (bitnr & 31);
  837. }
  838. GRETH_REGSAVE(regs->hash_msb, mc_filter[1]);
  839. GRETH_REGSAVE(regs->hash_lsb, mc_filter[0]);
  840. }
  841. static void greth_set_multicast_list(struct net_device *dev)
  842. {
  843. int cfg;
  844. struct greth_private *greth = netdev_priv(dev);
  845. struct greth_regs *regs = (struct greth_regs *) greth->regs;
  846. cfg = GRETH_REGLOAD(regs->control);
  847. if (dev->flags & IFF_PROMISC)
  848. cfg |= GRETH_CTRL_PR;
  849. else
  850. cfg &= ~GRETH_CTRL_PR;
  851. if (greth->multicast) {
  852. if (dev->flags & IFF_ALLMULTI) {
  853. GRETH_REGSAVE(regs->hash_msb, -1);
  854. GRETH_REGSAVE(regs->hash_lsb, -1);
  855. cfg |= GRETH_CTRL_MCEN;
  856. GRETH_REGSAVE(regs->control, cfg);
  857. return;
  858. }
  859. if (netdev_mc_empty(dev)) {
  860. cfg &= ~GRETH_CTRL_MCEN;
  861. GRETH_REGSAVE(regs->control, cfg);
  862. return;
  863. }
  864. /* Setup multicast filter */
  865. greth_set_hash_filter(dev);
  866. cfg |= GRETH_CTRL_MCEN;
  867. }
  868. GRETH_REGSAVE(regs->control, cfg);
  869. }
  870. static u32 greth_get_msglevel(struct net_device *dev)
  871. {
  872. struct greth_private *greth = netdev_priv(dev);
  873. return greth->msg_enable;
  874. }
  875. static void greth_set_msglevel(struct net_device *dev, u32 value)
  876. {
  877. struct greth_private *greth = netdev_priv(dev);
  878. greth->msg_enable = value;
  879. }
  880. static int greth_get_settings(struct net_device *dev, struct ethtool_cmd *cmd)
  881. {
  882. struct greth_private *greth = netdev_priv(dev);
  883. struct phy_device *phy = greth->phy;
  884. if (!phy)
  885. return -ENODEV;
  886. return phy_ethtool_gset(phy, cmd);
  887. }
  888. static int greth_set_settings(struct net_device *dev, struct ethtool_cmd *cmd)
  889. {
  890. struct greth_private *greth = netdev_priv(dev);
  891. struct phy_device *phy = greth->phy;
  892. if (!phy)
  893. return -ENODEV;
  894. return phy_ethtool_sset(phy, cmd);
  895. }
  896. static int greth_get_regs_len(struct net_device *dev)
  897. {
  898. return sizeof(struct greth_regs);
  899. }
  900. static void greth_get_drvinfo(struct net_device *dev, struct ethtool_drvinfo *info)
  901. {
  902. struct greth_private *greth = netdev_priv(dev);
  903. strncpy(info->driver, dev_driver_string(greth->dev), 32);
  904. strncpy(info->version, "revision: 1.0", 32);
  905. strncpy(info->bus_info, greth->dev->bus->name, 32);
  906. strncpy(info->fw_version, "N/A", 32);
  907. info->eedump_len = 0;
  908. info->regdump_len = sizeof(struct greth_regs);
  909. }
  910. static void greth_get_regs(struct net_device *dev, struct ethtool_regs *regs, void *p)
  911. {
  912. int i;
  913. struct greth_private *greth = netdev_priv(dev);
  914. u32 __iomem *greth_regs = (u32 __iomem *) greth->regs;
  915. u32 *buff = p;
  916. for (i = 0; i < sizeof(struct greth_regs) / sizeof(u32); i++)
  917. buff[i] = greth_read_bd(&greth_regs[i]);
  918. }
  919. static const struct ethtool_ops greth_ethtool_ops = {
  920. .get_msglevel = greth_get_msglevel,
  921. .set_msglevel = greth_set_msglevel,
  922. .get_settings = greth_get_settings,
  923. .set_settings = greth_set_settings,
  924. .get_drvinfo = greth_get_drvinfo,
  925. .get_regs_len = greth_get_regs_len,
  926. .get_regs = greth_get_regs,
  927. .get_link = ethtool_op_get_link,
  928. };
  929. static struct net_device_ops greth_netdev_ops = {
  930. .ndo_open = greth_open,
  931. .ndo_stop = greth_close,
  932. .ndo_start_xmit = greth_start_xmit,
  933. .ndo_set_mac_address = greth_set_mac_add,
  934. .ndo_validate_addr = eth_validate_addr,
  935. };
  936. static inline int wait_for_mdio(struct greth_private *greth)
  937. {
  938. unsigned long timeout = jiffies + 4*HZ/100;
  939. while (GRETH_REGLOAD(greth->regs->mdio) & GRETH_MII_BUSY) {
  940. if (time_after(jiffies, timeout))
  941. return 0;
  942. }
  943. return 1;
  944. }
  945. static int greth_mdio_read(struct mii_bus *bus, int phy, int reg)
  946. {
  947. struct greth_private *greth = bus->priv;
  948. int data;
  949. if (!wait_for_mdio(greth))
  950. return -EBUSY;
  951. GRETH_REGSAVE(greth->regs->mdio, ((phy & 0x1F) << 11) | ((reg & 0x1F) << 6) | 2);
  952. if (!wait_for_mdio(greth))
  953. return -EBUSY;
  954. if (!(GRETH_REGLOAD(greth->regs->mdio) & GRETH_MII_NVALID)) {
  955. data = (GRETH_REGLOAD(greth->regs->mdio) >> 16) & 0xFFFF;
  956. return data;
  957. } else {
  958. return -1;
  959. }
  960. }
  961. static int greth_mdio_write(struct mii_bus *bus, int phy, int reg, u16 val)
  962. {
  963. struct greth_private *greth = bus->priv;
  964. if (!wait_for_mdio(greth))
  965. return -EBUSY;
  966. GRETH_REGSAVE(greth->regs->mdio,
  967. ((val & 0xFFFF) << 16) | ((phy & 0x1F) << 11) | ((reg & 0x1F) << 6) | 1);
  968. if (!wait_for_mdio(greth))
  969. return -EBUSY;
  970. return 0;
  971. }
  972. static int greth_mdio_reset(struct mii_bus *bus)
  973. {
  974. return 0;
  975. }
  976. static void greth_link_change(struct net_device *dev)
  977. {
  978. struct greth_private *greth = netdev_priv(dev);
  979. struct phy_device *phydev = greth->phy;
  980. unsigned long flags;
  981. int status_change = 0;
  982. u32 ctrl;
  983. spin_lock_irqsave(&greth->devlock, flags);
  984. if (phydev->link) {
  985. if ((greth->speed != phydev->speed) || (greth->duplex != phydev->duplex)) {
  986. ctrl = GRETH_REGLOAD(greth->regs->control) &
  987. ~(GRETH_CTRL_FD | GRETH_CTRL_SP | GRETH_CTRL_GB);
  988. if (phydev->duplex)
  989. ctrl |= GRETH_CTRL_FD;
  990. if (phydev->speed == SPEED_100)
  991. ctrl |= GRETH_CTRL_SP;
  992. else if (phydev->speed == SPEED_1000)
  993. ctrl |= GRETH_CTRL_GB;
  994. GRETH_REGSAVE(greth->regs->control, ctrl);
  995. greth->speed = phydev->speed;
  996. greth->duplex = phydev->duplex;
  997. status_change = 1;
  998. }
  999. }
  1000. if (phydev->link != greth->link) {
  1001. if (!phydev->link) {
  1002. greth->speed = 0;
  1003. greth->duplex = -1;
  1004. }
  1005. greth->link = phydev->link;
  1006. status_change = 1;
  1007. }
  1008. spin_unlock_irqrestore(&greth->devlock, flags);
  1009. if (status_change) {
  1010. if (phydev->link)
  1011. pr_debug("%s: link up (%d/%s)\n",
  1012. dev->name, phydev->speed,
  1013. DUPLEX_FULL == phydev->duplex ? "Full" : "Half");
  1014. else
  1015. pr_debug("%s: link down\n", dev->name);
  1016. }
  1017. }
  1018. static int greth_mdio_probe(struct net_device *dev)
  1019. {
  1020. struct greth_private *greth = netdev_priv(dev);
  1021. struct phy_device *phy = NULL;
  1022. int ret;
  1023. /* Find the first PHY */
  1024. phy = phy_find_first(greth->mdio);
  1025. if (!phy) {
  1026. if (netif_msg_probe(greth))
  1027. dev_err(&dev->dev, "no PHY found\n");
  1028. return -ENXIO;
  1029. }
  1030. ret = phy_connect_direct(dev, phy, &greth_link_change,
  1031. 0, greth->gbit_mac ?
  1032. PHY_INTERFACE_MODE_GMII :
  1033. PHY_INTERFACE_MODE_MII);
  1034. if (ret) {
  1035. if (netif_msg_ifup(greth))
  1036. dev_err(&dev->dev, "could not attach to PHY\n");
  1037. return ret;
  1038. }
  1039. if (greth->gbit_mac)
  1040. phy->supported &= PHY_GBIT_FEATURES;
  1041. else
  1042. phy->supported &= PHY_BASIC_FEATURES;
  1043. phy->advertising = phy->supported;
  1044. greth->link = 0;
  1045. greth->speed = 0;
  1046. greth->duplex = -1;
  1047. greth->phy = phy;
  1048. return 0;
  1049. }
  1050. static inline int phy_aneg_done(struct phy_device *phydev)
  1051. {
  1052. int retval;
  1053. retval = phy_read(phydev, MII_BMSR);
  1054. return (retval < 0) ? retval : (retval & BMSR_ANEGCOMPLETE);
  1055. }
  1056. static int greth_mdio_init(struct greth_private *greth)
  1057. {
  1058. int ret, phy;
  1059. unsigned long timeout;
  1060. greth->mdio = mdiobus_alloc();
  1061. if (!greth->mdio) {
  1062. return -ENOMEM;
  1063. }
  1064. greth->mdio->name = "greth-mdio";
  1065. snprintf(greth->mdio->id, MII_BUS_ID_SIZE, "%s-%d", greth->mdio->name, greth->irq);
  1066. greth->mdio->read = greth_mdio_read;
  1067. greth->mdio->write = greth_mdio_write;
  1068. greth->mdio->reset = greth_mdio_reset;
  1069. greth->mdio->priv = greth;
  1070. greth->mdio->irq = greth->mdio_irqs;
  1071. for (phy = 0; phy < PHY_MAX_ADDR; phy++)
  1072. greth->mdio->irq[phy] = PHY_POLL;
  1073. ret = mdiobus_register(greth->mdio);
  1074. if (ret) {
  1075. goto error;
  1076. }
  1077. ret = greth_mdio_probe(greth->netdev);
  1078. if (ret) {
  1079. if (netif_msg_probe(greth))
  1080. dev_err(&greth->netdev->dev, "failed to probe MDIO bus\n");
  1081. goto unreg_mdio;
  1082. }
  1083. phy_start(greth->phy);
  1084. /* If Ethernet debug link is used make autoneg happen right away */
  1085. if (greth->edcl && greth_edcl == 1) {
  1086. phy_start_aneg(greth->phy);
  1087. timeout = jiffies + 6*HZ;
  1088. while (!phy_aneg_done(greth->phy) && time_before(jiffies, timeout)) {
  1089. }
  1090. genphy_read_status(greth->phy);
  1091. greth_link_change(greth->netdev);
  1092. }
  1093. return 0;
  1094. unreg_mdio:
  1095. mdiobus_unregister(greth->mdio);
  1096. error:
  1097. mdiobus_free(greth->mdio);
  1098. return ret;
  1099. }
  1100. /* Initialize the GRETH MAC */
  1101. static int __devinit greth_of_probe(struct platform_device *ofdev)
  1102. {
  1103. struct net_device *dev;
  1104. struct greth_private *greth;
  1105. struct greth_regs *regs;
  1106. int i;
  1107. int err;
  1108. int tmp;
  1109. unsigned long timeout;
  1110. dev = alloc_etherdev(sizeof(struct greth_private));
  1111. if (dev == NULL)
  1112. return -ENOMEM;
  1113. greth = netdev_priv(dev);
  1114. greth->netdev = dev;
  1115. greth->dev = &ofdev->dev;
  1116. if (greth_debug > 0)
  1117. greth->msg_enable = greth_debug;
  1118. else
  1119. greth->msg_enable = GRETH_DEF_MSG_ENABLE;
  1120. spin_lock_init(&greth->devlock);
  1121. greth->regs = of_ioremap(&ofdev->resource[0], 0,
  1122. resource_size(&ofdev->resource[0]),
  1123. "grlib-greth regs");
  1124. if (greth->regs == NULL) {
  1125. if (netif_msg_probe(greth))
  1126. dev_err(greth->dev, "ioremap failure.\n");
  1127. err = -EIO;
  1128. goto error1;
  1129. }
  1130. regs = (struct greth_regs *) greth->regs;
  1131. greth->irq = ofdev->archdata.irqs[0];
  1132. dev_set_drvdata(greth->dev, dev);
  1133. SET_NETDEV_DEV(dev, greth->dev);
  1134. if (netif_msg_probe(greth))
  1135. dev_dbg(greth->dev, "reseting controller.\n");
  1136. /* Reset the controller. */
  1137. GRETH_REGSAVE(regs->control, GRETH_RESET);
  1138. /* Wait for MAC to reset itself */
  1139. timeout = jiffies + HZ/100;
  1140. while (GRETH_REGLOAD(regs->control) & GRETH_RESET) {
  1141. if (time_after(jiffies, timeout)) {
  1142. err = -EIO;
  1143. if (netif_msg_probe(greth))
  1144. dev_err(greth->dev, "timeout when waiting for reset.\n");
  1145. goto error2;
  1146. }
  1147. }
  1148. /* Get default PHY address */
  1149. greth->phyaddr = (GRETH_REGLOAD(regs->mdio) >> 11) & 0x1F;
  1150. /* Check if we have GBIT capable MAC */
  1151. tmp = GRETH_REGLOAD(regs->control);
  1152. greth->gbit_mac = (tmp >> 27) & 1;
  1153. /* Check for multicast capability */
  1154. greth->multicast = (tmp >> 25) & 1;
  1155. greth->edcl = (tmp >> 31) & 1;
  1156. /* If we have EDCL we disable the EDCL speed-duplex FSM so
  1157. * it doesn't interfere with the software */
  1158. if (greth->edcl != 0)
  1159. GRETH_REGORIN(regs->control, GRETH_CTRL_DISDUPLEX);
  1160. /* Check if MAC can handle MDIO interrupts */
  1161. greth->mdio_int_en = (tmp >> 26) & 1;
  1162. err = greth_mdio_init(greth);
  1163. if (err) {
  1164. if (netif_msg_probe(greth))
  1165. dev_err(greth->dev, "failed to register MDIO bus\n");
  1166. goto error2;
  1167. }
  1168. /* Allocate TX descriptor ring in coherent memory */
  1169. greth->tx_bd_base = (struct greth_bd *) dma_alloc_coherent(greth->dev,
  1170. 1024,
  1171. &greth->tx_bd_base_phys,
  1172. GFP_KERNEL);
  1173. if (!greth->tx_bd_base) {
  1174. if (netif_msg_probe(greth))
  1175. dev_err(&dev->dev, "could not allocate descriptor memory.\n");
  1176. err = -ENOMEM;
  1177. goto error3;
  1178. }
  1179. memset(greth->tx_bd_base, 0, 1024);
  1180. /* Allocate RX descriptor ring in coherent memory */
  1181. greth->rx_bd_base = (struct greth_bd *) dma_alloc_coherent(greth->dev,
  1182. 1024,
  1183. &greth->rx_bd_base_phys,
  1184. GFP_KERNEL);
  1185. if (!greth->rx_bd_base) {
  1186. if (netif_msg_probe(greth))
  1187. dev_err(greth->dev, "could not allocate descriptor memory.\n");
  1188. err = -ENOMEM;
  1189. goto error4;
  1190. }
  1191. memset(greth->rx_bd_base, 0, 1024);
  1192. /* Get MAC address from: module param, OF property or ID prom */
  1193. for (i = 0; i < 6; i++) {
  1194. if (macaddr[i] != 0)
  1195. break;
  1196. }
  1197. if (i == 6) {
  1198. const unsigned char *addr;
  1199. int len;
  1200. addr = of_get_property(ofdev->dev.of_node, "local-mac-address",
  1201. &len);
  1202. if (addr != NULL && len == 6) {
  1203. for (i = 0; i < 6; i++)
  1204. macaddr[i] = (unsigned int) addr[i];
  1205. } else {
  1206. #ifdef CONFIG_SPARC
  1207. for (i = 0; i < 6; i++)
  1208. macaddr[i] = (unsigned int) idprom->id_ethaddr[i];
  1209. #endif
  1210. }
  1211. }
  1212. for (i = 0; i < 6; i++)
  1213. dev->dev_addr[i] = macaddr[i];
  1214. macaddr[5]++;
  1215. if (!is_valid_ether_addr(&dev->dev_addr[0])) {
  1216. if (netif_msg_probe(greth))
  1217. dev_err(greth->dev, "no valid ethernet address, aborting.\n");
  1218. err = -EINVAL;
  1219. goto error5;
  1220. }
  1221. GRETH_REGSAVE(regs->esa_msb, dev->dev_addr[0] << 8 | dev->dev_addr[1]);
  1222. GRETH_REGSAVE(regs->esa_lsb, dev->dev_addr[2] << 24 | dev->dev_addr[3] << 16 |
  1223. dev->dev_addr[4] << 8 | dev->dev_addr[5]);
  1224. /* Clear all pending interrupts except PHY irq */
  1225. GRETH_REGSAVE(regs->status, 0xFF);
  1226. if (greth->gbit_mac) {
  1227. dev->hw_features = NETIF_F_SG | NETIF_F_IP_CSUM |
  1228. NETIF_F_RXCSUM;
  1229. dev->features = dev->hw_features | NETIF_F_HIGHDMA;
  1230. greth_netdev_ops.ndo_start_xmit = greth_start_xmit_gbit;
  1231. }
  1232. if (greth->multicast) {
  1233. greth_netdev_ops.ndo_set_multicast_list = greth_set_multicast_list;
  1234. dev->flags |= IFF_MULTICAST;
  1235. } else {
  1236. dev->flags &= ~IFF_MULTICAST;
  1237. }
  1238. dev->netdev_ops = &greth_netdev_ops;
  1239. dev->ethtool_ops = &greth_ethtool_ops;
  1240. err = register_netdev(dev);
  1241. if (err) {
  1242. if (netif_msg_probe(greth))
  1243. dev_err(greth->dev, "netdevice registration failed.\n");
  1244. goto error5;
  1245. }
  1246. /* setup NAPI */
  1247. netif_napi_add(dev, &greth->napi, greth_poll, 64);
  1248. return 0;
  1249. error5:
  1250. dma_free_coherent(greth->dev, 1024, greth->rx_bd_base, greth->rx_bd_base_phys);
  1251. error4:
  1252. dma_free_coherent(greth->dev, 1024, greth->tx_bd_base, greth->tx_bd_base_phys);
  1253. error3:
  1254. mdiobus_unregister(greth->mdio);
  1255. error2:
  1256. of_iounmap(&ofdev->resource[0], greth->regs, resource_size(&ofdev->resource[0]));
  1257. error1:
  1258. free_netdev(dev);
  1259. return err;
  1260. }
  1261. static int __devexit greth_of_remove(struct platform_device *of_dev)
  1262. {
  1263. struct net_device *ndev = dev_get_drvdata(&of_dev->dev);
  1264. struct greth_private *greth = netdev_priv(ndev);
  1265. /* Free descriptor areas */
  1266. dma_free_coherent(&of_dev->dev, 1024, greth->rx_bd_base, greth->rx_bd_base_phys);
  1267. dma_free_coherent(&of_dev->dev, 1024, greth->tx_bd_base, greth->tx_bd_base_phys);
  1268. dev_set_drvdata(&of_dev->dev, NULL);
  1269. if (greth->phy)
  1270. phy_stop(greth->phy);
  1271. mdiobus_unregister(greth->mdio);
  1272. unregister_netdev(ndev);
  1273. free_netdev(ndev);
  1274. of_iounmap(&of_dev->resource[0], greth->regs, resource_size(&of_dev->resource[0]));
  1275. return 0;
  1276. }
  1277. static struct of_device_id greth_of_match[] = {
  1278. {
  1279. .name = "GAISLER_ETHMAC",
  1280. },
  1281. {
  1282. .name = "01_01d",
  1283. },
  1284. {},
  1285. };
  1286. MODULE_DEVICE_TABLE(of, greth_of_match);
  1287. static struct platform_driver greth_of_driver = {
  1288. .driver = {
  1289. .name = "grlib-greth",
  1290. .owner = THIS_MODULE,
  1291. .of_match_table = greth_of_match,
  1292. },
  1293. .probe = greth_of_probe,
  1294. .remove = __devexit_p(greth_of_remove),
  1295. };
  1296. static int __init greth_init(void)
  1297. {
  1298. return platform_driver_register(&greth_of_driver);
  1299. }
  1300. static void __exit greth_cleanup(void)
  1301. {
  1302. platform_driver_unregister(&greth_of_driver);
  1303. }
  1304. module_init(greth_init);
  1305. module_exit(greth_cleanup);
  1306. MODULE_AUTHOR("Aeroflex Gaisler AB.");
  1307. MODULE_DESCRIPTION("Aeroflex Gaisler Ethernet MAC driver");
  1308. MODULE_LICENSE("GPL");