greth.c 39 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578
  1. // SPDX-License-Identifier: GPL-2.0-or-later
  2. /*
  3. * Aeroflex Gaisler GRETH 10/100/1G Ethernet MAC.
  4. *
  5. * 2005-2010 (c) Aeroflex Gaisler AB
  6. *
  7. * This driver supports GRETH 10/100 and GRETH 10/100/1G Ethernet MACs
  8. * available in the GRLIB VHDL IP core library.
  9. *
  10. * Full documentation of both cores can be found here:
  11. * http://www.gaisler.com/products/grlib/grip.pdf
  12. *
  13. * The Gigabit version supports scatter/gather DMA, any alignment of
  14. * buffers and checksum offloading.
  15. *
  16. * Contributors: Kristoffer Glembo
  17. * Daniel Hellstrom
  18. * Marko Isomaki
  19. */
  20. #include <linux/dma-mapping.h>
  21. #include <linux/module.h>
  22. #include <linux/uaccess.h>
  23. #include <linux/interrupt.h>
  24. #include <linux/netdevice.h>
  25. #include <linux/etherdevice.h>
  26. #include <linux/ethtool.h>
  27. #include <linux/skbuff.h>
  28. #include <linux/io.h>
  29. #include <linux/crc32.h>
  30. #include <linux/mii.h>
  31. #include <linux/of_device.h>
  32. #include <linux/of_net.h>
  33. #include <linux/of_platform.h>
  34. #include <linux/slab.h>
  35. #include <asm/cacheflush.h>
  36. #include <asm/byteorder.h>
  37. #ifdef CONFIG_SPARC
  38. #include <asm/idprom.h>
  39. #endif
  40. #include "greth.h"
  41. #define GRETH_DEF_MSG_ENABLE \
  42. (NETIF_MSG_DRV | \
  43. NETIF_MSG_PROBE | \
  44. NETIF_MSG_LINK | \
  45. NETIF_MSG_IFDOWN | \
  46. NETIF_MSG_IFUP | \
  47. NETIF_MSG_RX_ERR | \
  48. NETIF_MSG_TX_ERR)
  49. static int greth_debug = -1; /* -1 == use GRETH_DEF_MSG_ENABLE as value */
  50. module_param(greth_debug, int, 0);
  51. MODULE_PARM_DESC(greth_debug, "GRETH bitmapped debugging message enable value");
  52. /* Accept MAC address of the form macaddr=0x08,0x00,0x20,0x30,0x40,0x50 */
  53. static int macaddr[6];
  54. module_param_array(macaddr, int, NULL, 0);
  55. MODULE_PARM_DESC(macaddr, "GRETH Ethernet MAC address");
  56. static int greth_edcl = 1;
  57. module_param(greth_edcl, int, 0);
  58. MODULE_PARM_DESC(greth_edcl, "GRETH EDCL usage indicator. Set to 1 if EDCL is used.");
  59. static int greth_open(struct net_device *dev);
  60. static netdev_tx_t greth_start_xmit(struct sk_buff *skb,
  61. struct net_device *dev);
  62. static netdev_tx_t greth_start_xmit_gbit(struct sk_buff *skb,
  63. struct net_device *dev);
  64. static int greth_rx(struct net_device *dev, int limit);
  65. static int greth_rx_gbit(struct net_device *dev, int limit);
  66. static void greth_clean_tx(struct net_device *dev);
  67. static void greth_clean_tx_gbit(struct net_device *dev);
  68. static irqreturn_t greth_interrupt(int irq, void *dev_id);
  69. static int greth_close(struct net_device *dev);
  70. static int greth_set_mac_add(struct net_device *dev, void *p);
  71. static void greth_set_multicast_list(struct net_device *dev);
  72. #define GRETH_REGLOAD(a) (be32_to_cpu(__raw_readl(&(a))))
  73. #define GRETH_REGSAVE(a, v) (__raw_writel(cpu_to_be32(v), &(a)))
  74. #define GRETH_REGORIN(a, v) (GRETH_REGSAVE(a, (GRETH_REGLOAD(a) | (v))))
  75. #define GRETH_REGANDIN(a, v) (GRETH_REGSAVE(a, (GRETH_REGLOAD(a) & (v))))
  76. #define NEXT_TX(N) (((N) + 1) & GRETH_TXBD_NUM_MASK)
  77. #define SKIP_TX(N, C) (((N) + C) & GRETH_TXBD_NUM_MASK)
  78. #define NEXT_RX(N) (((N) + 1) & GRETH_RXBD_NUM_MASK)
  79. static void greth_print_rx_packet(void *addr, int len)
  80. {
  81. print_hex_dump(KERN_DEBUG, "RX: ", DUMP_PREFIX_OFFSET, 16, 1,
  82. addr, len, true);
  83. }
  84. static void greth_print_tx_packet(struct sk_buff *skb)
  85. {
  86. int i;
  87. int length;
  88. if (skb_shinfo(skb)->nr_frags == 0)
  89. length = skb->len;
  90. else
  91. length = skb_headlen(skb);
  92. print_hex_dump(KERN_DEBUG, "TX: ", DUMP_PREFIX_OFFSET, 16, 1,
  93. skb->data, length, true);
  94. for (i = 0; i < skb_shinfo(skb)->nr_frags; i++) {
  95. print_hex_dump(KERN_DEBUG, "TX: ", DUMP_PREFIX_OFFSET, 16, 1,
  96. skb_frag_address(&skb_shinfo(skb)->frags[i]),
  97. skb_frag_size(&skb_shinfo(skb)->frags[i]), true);
  98. }
  99. }
  100. static inline void greth_enable_tx(struct greth_private *greth)
  101. {
  102. wmb();
  103. GRETH_REGORIN(greth->regs->control, GRETH_TXEN);
  104. }
  105. static inline void greth_enable_tx_and_irq(struct greth_private *greth)
  106. {
  107. wmb(); /* BDs must been written to memory before enabling TX */
  108. GRETH_REGORIN(greth->regs->control, GRETH_TXEN | GRETH_TXI);
  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. skb_frag_size(frag),
  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 inline u16 greth_num_free_bds(u16 tx_last, u16 tx_next)
  368. {
  369. if (tx_next < tx_last)
  370. return (tx_last - tx_next) - 1;
  371. else
  372. return GRETH_TXBD_NUM - (tx_next - tx_last) - 1;
  373. }
  374. static netdev_tx_t
  375. greth_start_xmit_gbit(struct sk_buff *skb, struct net_device *dev)
  376. {
  377. struct greth_private *greth = netdev_priv(dev);
  378. struct greth_bd *bdp;
  379. u32 status, dma_addr;
  380. int curr_tx, nr_frags, i, err = NETDEV_TX_OK;
  381. unsigned long flags;
  382. u16 tx_last;
  383. nr_frags = skb_shinfo(skb)->nr_frags;
  384. tx_last = greth->tx_last;
  385. rmb(); /* tx_last is updated by the poll task */
  386. if (greth_num_free_bds(tx_last, greth->tx_next) < nr_frags + 1) {
  387. netif_stop_queue(dev);
  388. err = NETDEV_TX_BUSY;
  389. goto out;
  390. }
  391. if (netif_msg_pktdata(greth))
  392. greth_print_tx_packet(skb);
  393. if (unlikely(skb->len > MAX_FRAME_SIZE)) {
  394. dev->stats.tx_errors++;
  395. goto out;
  396. }
  397. /* Save skb pointer. */
  398. greth->tx_skbuff[greth->tx_next] = skb;
  399. /* Linear buf */
  400. if (nr_frags != 0)
  401. status = GRETH_TXBD_MORE;
  402. else
  403. status = GRETH_BD_IE;
  404. if (skb->ip_summed == CHECKSUM_PARTIAL)
  405. status |= GRETH_TXBD_CSALL;
  406. status |= skb_headlen(skb) & GRETH_BD_LEN;
  407. if (greth->tx_next == GRETH_TXBD_NUM_MASK)
  408. status |= GRETH_BD_WR;
  409. bdp = greth->tx_bd_base + greth->tx_next;
  410. greth_write_bd(&bdp->stat, status);
  411. dma_addr = dma_map_single(greth->dev, skb->data, skb_headlen(skb), DMA_TO_DEVICE);
  412. if (unlikely(dma_mapping_error(greth->dev, dma_addr)))
  413. goto map_error;
  414. greth_write_bd(&bdp->addr, dma_addr);
  415. curr_tx = NEXT_TX(greth->tx_next);
  416. /* Frags */
  417. for (i = 0; i < nr_frags; i++) {
  418. skb_frag_t *frag = &skb_shinfo(skb)->frags[i];
  419. greth->tx_skbuff[curr_tx] = NULL;
  420. bdp = greth->tx_bd_base + curr_tx;
  421. status = GRETH_BD_EN;
  422. if (skb->ip_summed == CHECKSUM_PARTIAL)
  423. status |= GRETH_TXBD_CSALL;
  424. status |= skb_frag_size(frag) & GRETH_BD_LEN;
  425. /* Wrap around descriptor ring */
  426. if (curr_tx == GRETH_TXBD_NUM_MASK)
  427. status |= GRETH_BD_WR;
  428. /* More fragments left */
  429. if (i < nr_frags - 1)
  430. status |= GRETH_TXBD_MORE;
  431. else
  432. status |= GRETH_BD_IE; /* enable IRQ on last fragment */
  433. greth_write_bd(&bdp->stat, status);
  434. dma_addr = skb_frag_dma_map(greth->dev, frag, 0, skb_frag_size(frag),
  435. DMA_TO_DEVICE);
  436. if (unlikely(dma_mapping_error(greth->dev, dma_addr)))
  437. goto frag_map_error;
  438. greth_write_bd(&bdp->addr, dma_addr);
  439. curr_tx = NEXT_TX(curr_tx);
  440. }
  441. wmb();
  442. /* Enable the descriptor chain by enabling the first descriptor */
  443. bdp = greth->tx_bd_base + greth->tx_next;
  444. greth_write_bd(&bdp->stat,
  445. greth_read_bd(&bdp->stat) | GRETH_BD_EN);
  446. spin_lock_irqsave(&greth->devlock, flags); /*save from poll/irq*/
  447. greth->tx_next = curr_tx;
  448. greth_enable_tx_and_irq(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. spin_unlock(&greth->devlock);
  492. return retval;
  493. }
  494. static void greth_clean_tx(struct net_device *dev)
  495. {
  496. struct greth_private *greth;
  497. struct greth_bd *bdp;
  498. u32 stat;
  499. greth = netdev_priv(dev);
  500. while (1) {
  501. bdp = greth->tx_bd_base + greth->tx_last;
  502. GRETH_REGSAVE(greth->regs->status, GRETH_INT_TE | GRETH_INT_TX);
  503. mb();
  504. stat = greth_read_bd(&bdp->stat);
  505. if (unlikely(stat & GRETH_BD_EN))
  506. break;
  507. if (greth->tx_free == GRETH_TXBD_NUM)
  508. break;
  509. /* Check status for errors */
  510. if (unlikely(stat & GRETH_TXBD_STATUS)) {
  511. dev->stats.tx_errors++;
  512. if (stat & GRETH_TXBD_ERR_AL)
  513. dev->stats.tx_aborted_errors++;
  514. if (stat & GRETH_TXBD_ERR_UE)
  515. dev->stats.tx_fifo_errors++;
  516. }
  517. dev->stats.tx_packets++;
  518. dev->stats.tx_bytes += greth->tx_bufs_length[greth->tx_last];
  519. greth->tx_last = NEXT_TX(greth->tx_last);
  520. greth->tx_free++;
  521. }
  522. if (greth->tx_free > 0) {
  523. netif_wake_queue(dev);
  524. }
  525. }
  526. static inline void greth_update_tx_stats(struct net_device *dev, u32 stat)
  527. {
  528. /* Check status for errors */
  529. if (unlikely(stat & GRETH_TXBD_STATUS)) {
  530. dev->stats.tx_errors++;
  531. if (stat & GRETH_TXBD_ERR_AL)
  532. dev->stats.tx_aborted_errors++;
  533. if (stat & GRETH_TXBD_ERR_UE)
  534. dev->stats.tx_fifo_errors++;
  535. if (stat & GRETH_TXBD_ERR_LC)
  536. dev->stats.tx_aborted_errors++;
  537. }
  538. dev->stats.tx_packets++;
  539. }
  540. static void greth_clean_tx_gbit(struct net_device *dev)
  541. {
  542. struct greth_private *greth;
  543. struct greth_bd *bdp, *bdp_last_frag;
  544. struct sk_buff *skb = NULL;
  545. u32 stat;
  546. int nr_frags, i;
  547. u16 tx_last;
  548. greth = netdev_priv(dev);
  549. tx_last = greth->tx_last;
  550. while (tx_last != greth->tx_next) {
  551. skb = greth->tx_skbuff[tx_last];
  552. nr_frags = skb_shinfo(skb)->nr_frags;
  553. /* We only clean fully completed SKBs */
  554. bdp_last_frag = greth->tx_bd_base + SKIP_TX(tx_last, nr_frags);
  555. GRETH_REGSAVE(greth->regs->status, GRETH_INT_TE | GRETH_INT_TX);
  556. mb();
  557. stat = greth_read_bd(&bdp_last_frag->stat);
  558. if (stat & GRETH_BD_EN)
  559. break;
  560. greth->tx_skbuff[tx_last] = NULL;
  561. greth_update_tx_stats(dev, stat);
  562. dev->stats.tx_bytes += skb->len;
  563. bdp = greth->tx_bd_base + tx_last;
  564. tx_last = NEXT_TX(tx_last);
  565. dma_unmap_single(greth->dev,
  566. greth_read_bd(&bdp->addr),
  567. skb_headlen(skb),
  568. DMA_TO_DEVICE);
  569. for (i = 0; i < nr_frags; i++) {
  570. skb_frag_t *frag = &skb_shinfo(skb)->frags[i];
  571. bdp = greth->tx_bd_base + tx_last;
  572. dma_unmap_page(greth->dev,
  573. greth_read_bd(&bdp->addr),
  574. skb_frag_size(frag),
  575. DMA_TO_DEVICE);
  576. tx_last = NEXT_TX(tx_last);
  577. }
  578. dev_kfree_skb(skb);
  579. }
  580. if (skb) { /* skb is set only if the above while loop was entered */
  581. wmb();
  582. greth->tx_last = tx_last;
  583. if (netif_queue_stopped(dev) &&
  584. (greth_num_free_bds(tx_last, greth->tx_next) >
  585. (MAX_SKB_FRAGS+1)))
  586. netif_wake_queue(dev);
  587. }
  588. }
  589. static int greth_rx(struct net_device *dev, int limit)
  590. {
  591. struct greth_private *greth;
  592. struct greth_bd *bdp;
  593. struct sk_buff *skb;
  594. int pkt_len;
  595. int bad, count;
  596. u32 status, dma_addr;
  597. unsigned long flags;
  598. greth = netdev_priv(dev);
  599. for (count = 0; count < limit; ++count) {
  600. bdp = greth->rx_bd_base + greth->rx_cur;
  601. GRETH_REGSAVE(greth->regs->status, GRETH_INT_RE | GRETH_INT_RX);
  602. mb();
  603. status = greth_read_bd(&bdp->stat);
  604. if (unlikely(status & GRETH_BD_EN)) {
  605. break;
  606. }
  607. dma_addr = greth_read_bd(&bdp->addr);
  608. bad = 0;
  609. /* Check status for errors. */
  610. if (unlikely(status & GRETH_RXBD_STATUS)) {
  611. if (status & GRETH_RXBD_ERR_FT) {
  612. dev->stats.rx_length_errors++;
  613. bad = 1;
  614. }
  615. if (status & (GRETH_RXBD_ERR_AE | GRETH_RXBD_ERR_OE)) {
  616. dev->stats.rx_frame_errors++;
  617. bad = 1;
  618. }
  619. if (status & GRETH_RXBD_ERR_CRC) {
  620. dev->stats.rx_crc_errors++;
  621. bad = 1;
  622. }
  623. }
  624. if (unlikely(bad)) {
  625. dev->stats.rx_errors++;
  626. } else {
  627. pkt_len = status & GRETH_BD_LEN;
  628. skb = netdev_alloc_skb(dev, pkt_len + NET_IP_ALIGN);
  629. if (unlikely(skb == NULL)) {
  630. if (net_ratelimit())
  631. dev_warn(&dev->dev, "low on memory - " "packet dropped\n");
  632. dev->stats.rx_dropped++;
  633. } else {
  634. skb_reserve(skb, NET_IP_ALIGN);
  635. dma_sync_single_for_cpu(greth->dev,
  636. dma_addr,
  637. pkt_len,
  638. DMA_FROM_DEVICE);
  639. if (netif_msg_pktdata(greth))
  640. greth_print_rx_packet(phys_to_virt(dma_addr), pkt_len);
  641. skb_put_data(skb, phys_to_virt(dma_addr),
  642. pkt_len);
  643. skb->protocol = eth_type_trans(skb, dev);
  644. dev->stats.rx_bytes += pkt_len;
  645. dev->stats.rx_packets++;
  646. netif_receive_skb(skb);
  647. }
  648. }
  649. status = GRETH_BD_EN | GRETH_BD_IE;
  650. if (greth->rx_cur == GRETH_RXBD_NUM_MASK) {
  651. status |= GRETH_BD_WR;
  652. }
  653. wmb();
  654. greth_write_bd(&bdp->stat, status);
  655. dma_sync_single_for_device(greth->dev, dma_addr, MAX_FRAME_SIZE, DMA_FROM_DEVICE);
  656. spin_lock_irqsave(&greth->devlock, flags); /* save from XMIT */
  657. greth_enable_rx(greth);
  658. spin_unlock_irqrestore(&greth->devlock, flags);
  659. greth->rx_cur = NEXT_RX(greth->rx_cur);
  660. }
  661. return count;
  662. }
  663. static inline int hw_checksummed(u32 status)
  664. {
  665. if (status & GRETH_RXBD_IP_FRAG)
  666. return 0;
  667. if (status & GRETH_RXBD_IP && status & GRETH_RXBD_IP_CSERR)
  668. return 0;
  669. if (status & GRETH_RXBD_UDP && status & GRETH_RXBD_UDP_CSERR)
  670. return 0;
  671. if (status & GRETH_RXBD_TCP && status & GRETH_RXBD_TCP_CSERR)
  672. return 0;
  673. return 1;
  674. }
  675. static int greth_rx_gbit(struct net_device *dev, int limit)
  676. {
  677. struct greth_private *greth;
  678. struct greth_bd *bdp;
  679. struct sk_buff *skb, *newskb;
  680. int pkt_len;
  681. int bad, count = 0;
  682. u32 status, dma_addr;
  683. unsigned long flags;
  684. greth = netdev_priv(dev);
  685. for (count = 0; count < limit; ++count) {
  686. bdp = greth->rx_bd_base + greth->rx_cur;
  687. skb = greth->rx_skbuff[greth->rx_cur];
  688. GRETH_REGSAVE(greth->regs->status, GRETH_INT_RE | GRETH_INT_RX);
  689. mb();
  690. status = greth_read_bd(&bdp->stat);
  691. bad = 0;
  692. if (status & GRETH_BD_EN)
  693. break;
  694. /* Check status for errors. */
  695. if (unlikely(status & GRETH_RXBD_STATUS)) {
  696. if (status & GRETH_RXBD_ERR_FT) {
  697. dev->stats.rx_length_errors++;
  698. bad = 1;
  699. } else if (status &
  700. (GRETH_RXBD_ERR_AE | GRETH_RXBD_ERR_OE | GRETH_RXBD_ERR_LE)) {
  701. dev->stats.rx_frame_errors++;
  702. bad = 1;
  703. } else if (status & GRETH_RXBD_ERR_CRC) {
  704. dev->stats.rx_crc_errors++;
  705. bad = 1;
  706. }
  707. }
  708. /* Allocate new skb to replace current, not needed if the
  709. * current skb can be reused */
  710. if (!bad && (newskb=netdev_alloc_skb(dev, MAX_FRAME_SIZE + NET_IP_ALIGN))) {
  711. skb_reserve(newskb, NET_IP_ALIGN);
  712. dma_addr = dma_map_single(greth->dev,
  713. newskb->data,
  714. MAX_FRAME_SIZE + NET_IP_ALIGN,
  715. DMA_FROM_DEVICE);
  716. if (!dma_mapping_error(greth->dev, dma_addr)) {
  717. /* Process the incoming frame. */
  718. pkt_len = status & GRETH_BD_LEN;
  719. dma_unmap_single(greth->dev,
  720. greth_read_bd(&bdp->addr),
  721. MAX_FRAME_SIZE + NET_IP_ALIGN,
  722. DMA_FROM_DEVICE);
  723. if (netif_msg_pktdata(greth))
  724. greth_print_rx_packet(phys_to_virt(greth_read_bd(&bdp->addr)), pkt_len);
  725. skb_put(skb, pkt_len);
  726. if (dev->features & NETIF_F_RXCSUM && hw_checksummed(status))
  727. skb->ip_summed = CHECKSUM_UNNECESSARY;
  728. else
  729. skb_checksum_none_assert(skb);
  730. skb->protocol = eth_type_trans(skb, dev);
  731. dev->stats.rx_packets++;
  732. dev->stats.rx_bytes += pkt_len;
  733. netif_receive_skb(skb);
  734. greth->rx_skbuff[greth->rx_cur] = newskb;
  735. greth_write_bd(&bdp->addr, dma_addr);
  736. } else {
  737. if (net_ratelimit())
  738. dev_warn(greth->dev, "Could not create DMA mapping, dropping packet\n");
  739. dev_kfree_skb(newskb);
  740. /* reusing current skb, so it is a drop */
  741. dev->stats.rx_dropped++;
  742. }
  743. } else if (bad) {
  744. /* Bad Frame transfer, the skb is reused */
  745. dev->stats.rx_dropped++;
  746. } else {
  747. /* Failed Allocating a new skb. This is rather stupid
  748. * but the current "filled" skb is reused, as if
  749. * transfer failure. One could argue that RX descriptor
  750. * table handling should be divided into cleaning and
  751. * filling as the TX part of the driver
  752. */
  753. if (net_ratelimit())
  754. dev_warn(greth->dev, "Could not allocate SKB, dropping packet\n");
  755. /* reusing current skb, so it is a drop */
  756. dev->stats.rx_dropped++;
  757. }
  758. status = GRETH_BD_EN | GRETH_BD_IE;
  759. if (greth->rx_cur == GRETH_RXBD_NUM_MASK) {
  760. status |= GRETH_BD_WR;
  761. }
  762. wmb();
  763. greth_write_bd(&bdp->stat, status);
  764. spin_lock_irqsave(&greth->devlock, flags);
  765. greth_enable_rx(greth);
  766. spin_unlock_irqrestore(&greth->devlock, flags);
  767. greth->rx_cur = NEXT_RX(greth->rx_cur);
  768. }
  769. return count;
  770. }
  771. static int greth_poll(struct napi_struct *napi, int budget)
  772. {
  773. struct greth_private *greth;
  774. int work_done = 0;
  775. unsigned long flags;
  776. u32 mask, ctrl;
  777. greth = container_of(napi, struct greth_private, napi);
  778. restart_txrx_poll:
  779. if (greth->gbit_mac) {
  780. greth_clean_tx_gbit(greth->netdev);
  781. work_done += greth_rx_gbit(greth->netdev, budget - work_done);
  782. } else {
  783. if (netif_queue_stopped(greth->netdev))
  784. greth_clean_tx(greth->netdev);
  785. work_done += greth_rx(greth->netdev, budget - work_done);
  786. }
  787. if (work_done < budget) {
  788. spin_lock_irqsave(&greth->devlock, flags);
  789. ctrl = GRETH_REGLOAD(greth->regs->control);
  790. if ((greth->gbit_mac && (greth->tx_last != greth->tx_next)) ||
  791. (!greth->gbit_mac && netif_queue_stopped(greth->netdev))) {
  792. GRETH_REGSAVE(greth->regs->control,
  793. ctrl | GRETH_TXI | GRETH_RXI);
  794. mask = GRETH_INT_RX | GRETH_INT_RE |
  795. GRETH_INT_TX | GRETH_INT_TE;
  796. } else {
  797. GRETH_REGSAVE(greth->regs->control, ctrl | GRETH_RXI);
  798. mask = GRETH_INT_RX | GRETH_INT_RE;
  799. }
  800. if (GRETH_REGLOAD(greth->regs->status) & mask) {
  801. GRETH_REGSAVE(greth->regs->control, ctrl);
  802. spin_unlock_irqrestore(&greth->devlock, flags);
  803. goto restart_txrx_poll;
  804. } else {
  805. napi_complete_done(napi, work_done);
  806. spin_unlock_irqrestore(&greth->devlock, flags);
  807. }
  808. }
  809. return work_done;
  810. }
  811. static int greth_set_mac_add(struct net_device *dev, void *p)
  812. {
  813. struct sockaddr *addr = p;
  814. struct greth_private *greth;
  815. struct greth_regs *regs;
  816. greth = netdev_priv(dev);
  817. regs = greth->regs;
  818. if (!is_valid_ether_addr(addr->sa_data))
  819. return -EADDRNOTAVAIL;
  820. memcpy(dev->dev_addr, addr->sa_data, dev->addr_len);
  821. GRETH_REGSAVE(regs->esa_msb, dev->dev_addr[0] << 8 | dev->dev_addr[1]);
  822. GRETH_REGSAVE(regs->esa_lsb, dev->dev_addr[2] << 24 | dev->dev_addr[3] << 16 |
  823. dev->dev_addr[4] << 8 | dev->dev_addr[5]);
  824. return 0;
  825. }
  826. static u32 greth_hash_get_index(__u8 *addr)
  827. {
  828. return (ether_crc(6, addr)) & 0x3F;
  829. }
  830. static void greth_set_hash_filter(struct net_device *dev)
  831. {
  832. struct netdev_hw_addr *ha;
  833. struct greth_private *greth = netdev_priv(dev);
  834. struct greth_regs *regs = greth->regs;
  835. u32 mc_filter[2];
  836. unsigned int bitnr;
  837. mc_filter[0] = mc_filter[1] = 0;
  838. netdev_for_each_mc_addr(ha, dev) {
  839. bitnr = greth_hash_get_index(ha->addr);
  840. mc_filter[bitnr >> 5] |= 1 << (bitnr & 31);
  841. }
  842. GRETH_REGSAVE(regs->hash_msb, mc_filter[1]);
  843. GRETH_REGSAVE(regs->hash_lsb, mc_filter[0]);
  844. }
  845. static void greth_set_multicast_list(struct net_device *dev)
  846. {
  847. int cfg;
  848. struct greth_private *greth = netdev_priv(dev);
  849. struct greth_regs *regs = greth->regs;
  850. cfg = GRETH_REGLOAD(regs->control);
  851. if (dev->flags & IFF_PROMISC)
  852. cfg |= GRETH_CTRL_PR;
  853. else
  854. cfg &= ~GRETH_CTRL_PR;
  855. if (greth->multicast) {
  856. if (dev->flags & IFF_ALLMULTI) {
  857. GRETH_REGSAVE(regs->hash_msb, -1);
  858. GRETH_REGSAVE(regs->hash_lsb, -1);
  859. cfg |= GRETH_CTRL_MCEN;
  860. GRETH_REGSAVE(regs->control, cfg);
  861. return;
  862. }
  863. if (netdev_mc_empty(dev)) {
  864. cfg &= ~GRETH_CTRL_MCEN;
  865. GRETH_REGSAVE(regs->control, cfg);
  866. return;
  867. }
  868. /* Setup multicast filter */
  869. greth_set_hash_filter(dev);
  870. cfg |= GRETH_CTRL_MCEN;
  871. }
  872. GRETH_REGSAVE(regs->control, cfg);
  873. }
  874. static u32 greth_get_msglevel(struct net_device *dev)
  875. {
  876. struct greth_private *greth = netdev_priv(dev);
  877. return greth->msg_enable;
  878. }
  879. static void greth_set_msglevel(struct net_device *dev, u32 value)
  880. {
  881. struct greth_private *greth = netdev_priv(dev);
  882. greth->msg_enable = value;
  883. }
  884. static int greth_get_regs_len(struct net_device *dev)
  885. {
  886. return sizeof(struct greth_regs);
  887. }
  888. static void greth_get_drvinfo(struct net_device *dev, struct ethtool_drvinfo *info)
  889. {
  890. struct greth_private *greth = netdev_priv(dev);
  891. strlcpy(info->driver, dev_driver_string(greth->dev),
  892. sizeof(info->driver));
  893. strlcpy(info->version, "revision: 1.0", sizeof(info->version));
  894. strlcpy(info->bus_info, greth->dev->bus->name, sizeof(info->bus_info));
  895. strlcpy(info->fw_version, "N/A", sizeof(info->fw_version));
  896. }
  897. static void greth_get_regs(struct net_device *dev, struct ethtool_regs *regs, void *p)
  898. {
  899. int i;
  900. struct greth_private *greth = netdev_priv(dev);
  901. u32 __iomem *greth_regs = (u32 __iomem *) greth->regs;
  902. u32 *buff = p;
  903. for (i = 0; i < sizeof(struct greth_regs) / sizeof(u32); i++)
  904. buff[i] = greth_read_bd(&greth_regs[i]);
  905. }
  906. static const struct ethtool_ops greth_ethtool_ops = {
  907. .get_msglevel = greth_get_msglevel,
  908. .set_msglevel = greth_set_msglevel,
  909. .get_drvinfo = greth_get_drvinfo,
  910. .get_regs_len = greth_get_regs_len,
  911. .get_regs = greth_get_regs,
  912. .get_link = ethtool_op_get_link,
  913. .get_link_ksettings = phy_ethtool_get_link_ksettings,
  914. .set_link_ksettings = phy_ethtool_set_link_ksettings,
  915. };
  916. static struct net_device_ops greth_netdev_ops = {
  917. .ndo_open = greth_open,
  918. .ndo_stop = greth_close,
  919. .ndo_start_xmit = greth_start_xmit,
  920. .ndo_set_mac_address = greth_set_mac_add,
  921. .ndo_validate_addr = eth_validate_addr,
  922. };
  923. static inline int wait_for_mdio(struct greth_private *greth)
  924. {
  925. unsigned long timeout = jiffies + 4*HZ/100;
  926. while (GRETH_REGLOAD(greth->regs->mdio) & GRETH_MII_BUSY) {
  927. if (time_after(jiffies, timeout))
  928. return 0;
  929. }
  930. return 1;
  931. }
  932. static int greth_mdio_read(struct mii_bus *bus, int phy, int reg)
  933. {
  934. struct greth_private *greth = bus->priv;
  935. int data;
  936. if (!wait_for_mdio(greth))
  937. return -EBUSY;
  938. GRETH_REGSAVE(greth->regs->mdio, ((phy & 0x1F) << 11) | ((reg & 0x1F) << 6) | 2);
  939. if (!wait_for_mdio(greth))
  940. return -EBUSY;
  941. if (!(GRETH_REGLOAD(greth->regs->mdio) & GRETH_MII_NVALID)) {
  942. data = (GRETH_REGLOAD(greth->regs->mdio) >> 16) & 0xFFFF;
  943. return data;
  944. } else {
  945. return -1;
  946. }
  947. }
  948. static int greth_mdio_write(struct mii_bus *bus, int phy, int reg, u16 val)
  949. {
  950. struct greth_private *greth = bus->priv;
  951. if (!wait_for_mdio(greth))
  952. return -EBUSY;
  953. GRETH_REGSAVE(greth->regs->mdio,
  954. ((val & 0xFFFF) << 16) | ((phy & 0x1F) << 11) | ((reg & 0x1F) << 6) | 1);
  955. if (!wait_for_mdio(greth))
  956. return -EBUSY;
  957. return 0;
  958. }
  959. static void greth_link_change(struct net_device *dev)
  960. {
  961. struct greth_private *greth = netdev_priv(dev);
  962. struct phy_device *phydev = dev->phydev;
  963. unsigned long flags;
  964. int status_change = 0;
  965. u32 ctrl;
  966. spin_lock_irqsave(&greth->devlock, flags);
  967. if (phydev->link) {
  968. if ((greth->speed != phydev->speed) || (greth->duplex != phydev->duplex)) {
  969. ctrl = GRETH_REGLOAD(greth->regs->control) &
  970. ~(GRETH_CTRL_FD | GRETH_CTRL_SP | GRETH_CTRL_GB);
  971. if (phydev->duplex)
  972. ctrl |= GRETH_CTRL_FD;
  973. if (phydev->speed == SPEED_100)
  974. ctrl |= GRETH_CTRL_SP;
  975. else if (phydev->speed == SPEED_1000)
  976. ctrl |= GRETH_CTRL_GB;
  977. GRETH_REGSAVE(greth->regs->control, ctrl);
  978. greth->speed = phydev->speed;
  979. greth->duplex = phydev->duplex;
  980. status_change = 1;
  981. }
  982. }
  983. if (phydev->link != greth->link) {
  984. if (!phydev->link) {
  985. greth->speed = 0;
  986. greth->duplex = -1;
  987. }
  988. greth->link = phydev->link;
  989. status_change = 1;
  990. }
  991. spin_unlock_irqrestore(&greth->devlock, flags);
  992. if (status_change) {
  993. if (phydev->link)
  994. pr_debug("%s: link up (%d/%s)\n",
  995. dev->name, phydev->speed,
  996. DUPLEX_FULL == phydev->duplex ? "Full" : "Half");
  997. else
  998. pr_debug("%s: link down\n", dev->name);
  999. }
  1000. }
  1001. static int greth_mdio_probe(struct net_device *dev)
  1002. {
  1003. struct greth_private *greth = netdev_priv(dev);
  1004. struct phy_device *phy = NULL;
  1005. int ret;
  1006. /* Find the first PHY */
  1007. phy = phy_find_first(greth->mdio);
  1008. if (!phy) {
  1009. if (netif_msg_probe(greth))
  1010. dev_err(&dev->dev, "no PHY found\n");
  1011. return -ENXIO;
  1012. }
  1013. ret = phy_connect_direct(dev, phy, &greth_link_change,
  1014. greth->gbit_mac ? PHY_INTERFACE_MODE_GMII : PHY_INTERFACE_MODE_MII);
  1015. if (ret) {
  1016. if (netif_msg_ifup(greth))
  1017. dev_err(&dev->dev, "could not attach to PHY\n");
  1018. return ret;
  1019. }
  1020. if (greth->gbit_mac)
  1021. phy_set_max_speed(phy, SPEED_1000);
  1022. else
  1023. phy_set_max_speed(phy, SPEED_100);
  1024. linkmode_copy(phy->advertising, phy->supported);
  1025. greth->link = 0;
  1026. greth->speed = 0;
  1027. greth->duplex = -1;
  1028. return 0;
  1029. }
  1030. static int greth_mdio_init(struct greth_private *greth)
  1031. {
  1032. int ret;
  1033. unsigned long timeout;
  1034. struct net_device *ndev = greth->netdev;
  1035. greth->mdio = mdiobus_alloc();
  1036. if (!greth->mdio) {
  1037. return -ENOMEM;
  1038. }
  1039. greth->mdio->name = "greth-mdio";
  1040. snprintf(greth->mdio->id, MII_BUS_ID_SIZE, "%s-%d", greth->mdio->name, greth->irq);
  1041. greth->mdio->read = greth_mdio_read;
  1042. greth->mdio->write = greth_mdio_write;
  1043. greth->mdio->priv = greth;
  1044. ret = mdiobus_register(greth->mdio);
  1045. if (ret) {
  1046. goto error;
  1047. }
  1048. ret = greth_mdio_probe(greth->netdev);
  1049. if (ret) {
  1050. if (netif_msg_probe(greth))
  1051. dev_err(&greth->netdev->dev, "failed to probe MDIO bus\n");
  1052. goto unreg_mdio;
  1053. }
  1054. phy_start(ndev->phydev);
  1055. /* If Ethernet debug link is used make autoneg happen right away */
  1056. if (greth->edcl && greth_edcl == 1) {
  1057. phy_start_aneg(ndev->phydev);
  1058. timeout = jiffies + 6*HZ;
  1059. while (!phy_aneg_done(ndev->phydev) &&
  1060. time_before(jiffies, timeout)) {
  1061. }
  1062. phy_read_status(ndev->phydev);
  1063. greth_link_change(greth->netdev);
  1064. }
  1065. return 0;
  1066. unreg_mdio:
  1067. mdiobus_unregister(greth->mdio);
  1068. error:
  1069. mdiobus_free(greth->mdio);
  1070. return ret;
  1071. }
  1072. /* Initialize the GRETH MAC */
  1073. static int greth_of_probe(struct platform_device *ofdev)
  1074. {
  1075. struct net_device *dev;
  1076. struct greth_private *greth;
  1077. struct greth_regs *regs;
  1078. int i;
  1079. int err;
  1080. int tmp;
  1081. unsigned long timeout;
  1082. dev = alloc_etherdev(sizeof(struct greth_private));
  1083. if (dev == NULL)
  1084. return -ENOMEM;
  1085. greth = netdev_priv(dev);
  1086. greth->netdev = dev;
  1087. greth->dev = &ofdev->dev;
  1088. if (greth_debug > 0)
  1089. greth->msg_enable = greth_debug;
  1090. else
  1091. greth->msg_enable = GRETH_DEF_MSG_ENABLE;
  1092. spin_lock_init(&greth->devlock);
  1093. greth->regs = of_ioremap(&ofdev->resource[0], 0,
  1094. resource_size(&ofdev->resource[0]),
  1095. "grlib-greth regs");
  1096. if (greth->regs == NULL) {
  1097. if (netif_msg_probe(greth))
  1098. dev_err(greth->dev, "ioremap failure.\n");
  1099. err = -EIO;
  1100. goto error1;
  1101. }
  1102. regs = greth->regs;
  1103. greth->irq = ofdev->archdata.irqs[0];
  1104. dev_set_drvdata(greth->dev, dev);
  1105. SET_NETDEV_DEV(dev, greth->dev);
  1106. if (netif_msg_probe(greth))
  1107. dev_dbg(greth->dev, "resetting controller.\n");
  1108. /* Reset the controller. */
  1109. GRETH_REGSAVE(regs->control, GRETH_RESET);
  1110. /* Wait for MAC to reset itself */
  1111. timeout = jiffies + HZ/100;
  1112. while (GRETH_REGLOAD(regs->control) & GRETH_RESET) {
  1113. if (time_after(jiffies, timeout)) {
  1114. err = -EIO;
  1115. if (netif_msg_probe(greth))
  1116. dev_err(greth->dev, "timeout when waiting for reset.\n");
  1117. goto error2;
  1118. }
  1119. }
  1120. /* Get default PHY address */
  1121. greth->phyaddr = (GRETH_REGLOAD(regs->mdio) >> 11) & 0x1F;
  1122. /* Check if we have GBIT capable MAC */
  1123. tmp = GRETH_REGLOAD(regs->control);
  1124. greth->gbit_mac = (tmp >> 27) & 1;
  1125. /* Check for multicast capability */
  1126. greth->multicast = (tmp >> 25) & 1;
  1127. greth->edcl = (tmp >> 31) & 1;
  1128. /* If we have EDCL we disable the EDCL speed-duplex FSM so
  1129. * it doesn't interfere with the software */
  1130. if (greth->edcl != 0)
  1131. GRETH_REGORIN(regs->control, GRETH_CTRL_DISDUPLEX);
  1132. /* Check if MAC can handle MDIO interrupts */
  1133. greth->mdio_int_en = (tmp >> 26) & 1;
  1134. err = greth_mdio_init(greth);
  1135. if (err) {
  1136. if (netif_msg_probe(greth))
  1137. dev_err(greth->dev, "failed to register MDIO bus\n");
  1138. goto error2;
  1139. }
  1140. /* Allocate TX descriptor ring in coherent memory */
  1141. greth->tx_bd_base = dma_alloc_coherent(greth->dev, 1024,
  1142. &greth->tx_bd_base_phys,
  1143. GFP_KERNEL);
  1144. if (!greth->tx_bd_base) {
  1145. err = -ENOMEM;
  1146. goto error3;
  1147. }
  1148. /* Allocate RX descriptor ring in coherent memory */
  1149. greth->rx_bd_base = dma_alloc_coherent(greth->dev, 1024,
  1150. &greth->rx_bd_base_phys,
  1151. GFP_KERNEL);
  1152. if (!greth->rx_bd_base) {
  1153. err = -ENOMEM;
  1154. goto error4;
  1155. }
  1156. /* Get MAC address from: module param, OF property or ID prom */
  1157. for (i = 0; i < 6; i++) {
  1158. if (macaddr[i] != 0)
  1159. break;
  1160. }
  1161. if (i == 6) {
  1162. const u8 *addr;
  1163. addr = of_get_mac_address(ofdev->dev.of_node);
  1164. if (!IS_ERR(addr)) {
  1165. for (i = 0; i < 6; i++)
  1166. macaddr[i] = (unsigned int) addr[i];
  1167. } else {
  1168. #ifdef CONFIG_SPARC
  1169. for (i = 0; i < 6; i++)
  1170. macaddr[i] = (unsigned int) idprom->id_ethaddr[i];
  1171. #endif
  1172. }
  1173. }
  1174. for (i = 0; i < 6; i++)
  1175. dev->dev_addr[i] = macaddr[i];
  1176. macaddr[5]++;
  1177. if (!is_valid_ether_addr(&dev->dev_addr[0])) {
  1178. if (netif_msg_probe(greth))
  1179. dev_err(greth->dev, "no valid ethernet address, aborting.\n");
  1180. err = -EINVAL;
  1181. goto error5;
  1182. }
  1183. GRETH_REGSAVE(regs->esa_msb, dev->dev_addr[0] << 8 | dev->dev_addr[1]);
  1184. GRETH_REGSAVE(regs->esa_lsb, dev->dev_addr[2] << 24 | dev->dev_addr[3] << 16 |
  1185. dev->dev_addr[4] << 8 | dev->dev_addr[5]);
  1186. /* Clear all pending interrupts except PHY irq */
  1187. GRETH_REGSAVE(regs->status, 0xFF);
  1188. if (greth->gbit_mac) {
  1189. dev->hw_features = NETIF_F_SG | NETIF_F_IP_CSUM |
  1190. NETIF_F_RXCSUM;
  1191. dev->features = dev->hw_features | NETIF_F_HIGHDMA;
  1192. greth_netdev_ops.ndo_start_xmit = greth_start_xmit_gbit;
  1193. }
  1194. if (greth->multicast) {
  1195. greth_netdev_ops.ndo_set_rx_mode = greth_set_multicast_list;
  1196. dev->flags |= IFF_MULTICAST;
  1197. } else {
  1198. dev->flags &= ~IFF_MULTICAST;
  1199. }
  1200. dev->netdev_ops = &greth_netdev_ops;
  1201. dev->ethtool_ops = &greth_ethtool_ops;
  1202. err = register_netdev(dev);
  1203. if (err) {
  1204. if (netif_msg_probe(greth))
  1205. dev_err(greth->dev, "netdevice registration failed.\n");
  1206. goto error5;
  1207. }
  1208. /* setup NAPI */
  1209. netif_napi_add(dev, &greth->napi, greth_poll, 64);
  1210. return 0;
  1211. error5:
  1212. dma_free_coherent(greth->dev, 1024, greth->rx_bd_base, greth->rx_bd_base_phys);
  1213. error4:
  1214. dma_free_coherent(greth->dev, 1024, greth->tx_bd_base, greth->tx_bd_base_phys);
  1215. error3:
  1216. mdiobus_unregister(greth->mdio);
  1217. error2:
  1218. of_iounmap(&ofdev->resource[0], greth->regs, resource_size(&ofdev->resource[0]));
  1219. error1:
  1220. free_netdev(dev);
  1221. return err;
  1222. }
  1223. static int greth_of_remove(struct platform_device *of_dev)
  1224. {
  1225. struct net_device *ndev = platform_get_drvdata(of_dev);
  1226. struct greth_private *greth = netdev_priv(ndev);
  1227. /* Free descriptor areas */
  1228. dma_free_coherent(&of_dev->dev, 1024, greth->rx_bd_base, greth->rx_bd_base_phys);
  1229. dma_free_coherent(&of_dev->dev, 1024, greth->tx_bd_base, greth->tx_bd_base_phys);
  1230. if (ndev->phydev)
  1231. phy_stop(ndev->phydev);
  1232. mdiobus_unregister(greth->mdio);
  1233. unregister_netdev(ndev);
  1234. of_iounmap(&of_dev->resource[0], greth->regs, resource_size(&of_dev->resource[0]));
  1235. free_netdev(ndev);
  1236. return 0;
  1237. }
  1238. static const struct of_device_id greth_of_match[] = {
  1239. {
  1240. .name = "GAISLER_ETHMAC",
  1241. },
  1242. {
  1243. .name = "01_01d",
  1244. },
  1245. {},
  1246. };
  1247. MODULE_DEVICE_TABLE(of, greth_of_match);
  1248. static struct platform_driver greth_of_driver = {
  1249. .driver = {
  1250. .name = "grlib-greth",
  1251. .of_match_table = greth_of_match,
  1252. },
  1253. .probe = greth_of_probe,
  1254. .remove = greth_of_remove,
  1255. };
  1256. module_platform_driver(greth_of_driver);
  1257. MODULE_AUTHOR("Aeroflex Gaisler AB.");
  1258. MODULE_DESCRIPTION("Aeroflex Gaisler Ethernet MAC driver");
  1259. MODULE_LICENSE("GPL");