fs_enet-main.c 26 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159
  1. /*
  2. * Combined Ethernet driver for Motorola MPC8xx and MPC82xx.
  3. *
  4. * Copyright (c) 2003 Intracom S.A.
  5. * by Pantelis Antoniou <panto@intracom.gr>
  6. *
  7. * 2005 (c) MontaVista Software, Inc.
  8. * Vitaly Bordug <vbordug@ru.mvista.com>
  9. *
  10. * Heavily based on original FEC driver by Dan Malek <dan@embeddededge.com>
  11. * and modifications by Joakim Tjernlund <joakim.tjernlund@lumentis.se>
  12. *
  13. * This file is licensed under the terms of the GNU General Public License
  14. * version 2. This program is licensed "as is" without any warranty of any
  15. * kind, whether express or implied.
  16. */
  17. #include <linux/module.h>
  18. #include <linux/kernel.h>
  19. #include <linux/types.h>
  20. #include <linux/string.h>
  21. #include <linux/ptrace.h>
  22. #include <linux/errno.h>
  23. #include <linux/ioport.h>
  24. #include <linux/slab.h>
  25. #include <linux/interrupt.h>
  26. #include <linux/delay.h>
  27. #include <linux/netdevice.h>
  28. #include <linux/etherdevice.h>
  29. #include <linux/skbuff.h>
  30. #include <linux/spinlock.h>
  31. #include <linux/mii.h>
  32. #include <linux/ethtool.h>
  33. #include <linux/bitops.h>
  34. #include <linux/fs.h>
  35. #include <linux/platform_device.h>
  36. #include <linux/phy.h>
  37. #include <linux/of.h>
  38. #include <linux/of_mdio.h>
  39. #include <linux/of_platform.h>
  40. #include <linux/of_gpio.h>
  41. #include <linux/of_net.h>
  42. #include <linux/vmalloc.h>
  43. #include <asm/pgtable.h>
  44. #include <asm/irq.h>
  45. #include <asm/uaccess.h>
  46. #include "fs_enet.h"
  47. /*************************************************/
  48. MODULE_AUTHOR("Pantelis Antoniou <panto@intracom.gr>");
  49. MODULE_DESCRIPTION("Freescale Ethernet Driver");
  50. MODULE_LICENSE("GPL");
  51. MODULE_VERSION(DRV_MODULE_VERSION);
  52. static int fs_enet_debug = -1; /* -1 == use FS_ENET_DEF_MSG_ENABLE as value */
  53. module_param(fs_enet_debug, int, 0);
  54. MODULE_PARM_DESC(fs_enet_debug,
  55. "Freescale bitmapped debugging message enable value");
  56. #ifdef CONFIG_NET_POLL_CONTROLLER
  57. static void fs_enet_netpoll(struct net_device *dev);
  58. #endif
  59. static void fs_set_multicast_list(struct net_device *dev)
  60. {
  61. struct fs_enet_private *fep = netdev_priv(dev);
  62. (*fep->ops->set_multicast_list)(dev);
  63. }
  64. static void skb_align(struct sk_buff *skb, int align)
  65. {
  66. int off = ((unsigned long)skb->data) & (align - 1);
  67. if (off)
  68. skb_reserve(skb, align - off);
  69. }
  70. /* NAPI receive function */
  71. static int fs_enet_rx_napi(struct napi_struct *napi, int budget)
  72. {
  73. struct fs_enet_private *fep = container_of(napi, struct fs_enet_private, napi);
  74. struct net_device *dev = fep->ndev;
  75. const struct fs_platform_info *fpi = fep->fpi;
  76. cbd_t __iomem *bdp;
  77. struct sk_buff *skb, *skbn;
  78. int received = 0;
  79. u16 pkt_len, sc;
  80. int curidx;
  81. if (budget <= 0)
  82. return received;
  83. /*
  84. * First, grab all of the stats for the incoming packet.
  85. * These get messed up if we get called due to a busy condition.
  86. */
  87. bdp = fep->cur_rx;
  88. /* clear RX status bits for napi*/
  89. (*fep->ops->napi_clear_rx_event)(dev);
  90. while (((sc = CBDR_SC(bdp)) & BD_ENET_RX_EMPTY) == 0) {
  91. curidx = bdp - fep->rx_bd_base;
  92. /*
  93. * Since we have allocated space to hold a complete frame,
  94. * the last indicator should be set.
  95. */
  96. if ((sc & BD_ENET_RX_LAST) == 0)
  97. dev_warn(fep->dev, "rcv is not +last\n");
  98. /*
  99. * Check for errors.
  100. */
  101. if (sc & (BD_ENET_RX_LG | BD_ENET_RX_SH | BD_ENET_RX_CL |
  102. BD_ENET_RX_NO | BD_ENET_RX_CR | BD_ENET_RX_OV)) {
  103. fep->stats.rx_errors++;
  104. /* Frame too long or too short. */
  105. if (sc & (BD_ENET_RX_LG | BD_ENET_RX_SH))
  106. fep->stats.rx_length_errors++;
  107. /* Frame alignment */
  108. if (sc & (BD_ENET_RX_NO | BD_ENET_RX_CL))
  109. fep->stats.rx_frame_errors++;
  110. /* CRC Error */
  111. if (sc & BD_ENET_RX_CR)
  112. fep->stats.rx_crc_errors++;
  113. /* FIFO overrun */
  114. if (sc & BD_ENET_RX_OV)
  115. fep->stats.rx_crc_errors++;
  116. skb = fep->rx_skbuff[curidx];
  117. dma_unmap_single(fep->dev, CBDR_BUFADDR(bdp),
  118. L1_CACHE_ALIGN(PKT_MAXBUF_SIZE),
  119. DMA_FROM_DEVICE);
  120. skbn = skb;
  121. } else {
  122. skb = fep->rx_skbuff[curidx];
  123. dma_unmap_single(fep->dev, CBDR_BUFADDR(bdp),
  124. L1_CACHE_ALIGN(PKT_MAXBUF_SIZE),
  125. DMA_FROM_DEVICE);
  126. /*
  127. * Process the incoming frame.
  128. */
  129. fep->stats.rx_packets++;
  130. pkt_len = CBDR_DATLEN(bdp) - 4; /* remove CRC */
  131. fep->stats.rx_bytes += pkt_len + 4;
  132. if (pkt_len <= fpi->rx_copybreak) {
  133. /* +2 to make IP header L1 cache aligned */
  134. skbn = netdev_alloc_skb(dev, pkt_len + 2);
  135. if (skbn != NULL) {
  136. skb_reserve(skbn, 2); /* align IP header */
  137. skb_copy_from_linear_data(skb,
  138. skbn->data, pkt_len);
  139. swap(skb, skbn);
  140. }
  141. } else {
  142. skbn = netdev_alloc_skb(dev, ENET_RX_FRSIZE);
  143. if (skbn)
  144. skb_align(skbn, ENET_RX_ALIGN);
  145. }
  146. if (skbn != NULL) {
  147. skb_put(skb, pkt_len); /* Make room */
  148. skb->protocol = eth_type_trans(skb, dev);
  149. received++;
  150. netif_receive_skb(skb);
  151. } else {
  152. fep->stats.rx_dropped++;
  153. skbn = skb;
  154. }
  155. }
  156. fep->rx_skbuff[curidx] = skbn;
  157. CBDW_BUFADDR(bdp, dma_map_single(fep->dev, skbn->data,
  158. L1_CACHE_ALIGN(PKT_MAXBUF_SIZE),
  159. DMA_FROM_DEVICE));
  160. CBDW_DATLEN(bdp, 0);
  161. CBDW_SC(bdp, (sc & ~BD_ENET_RX_STATS) | BD_ENET_RX_EMPTY);
  162. /*
  163. * Update BD pointer to next entry.
  164. */
  165. if ((sc & BD_ENET_RX_WRAP) == 0)
  166. bdp++;
  167. else
  168. bdp = fep->rx_bd_base;
  169. (*fep->ops->rx_bd_done)(dev);
  170. if (received >= budget)
  171. break;
  172. }
  173. fep->cur_rx = bdp;
  174. if (received < budget) {
  175. /* done */
  176. napi_complete(napi);
  177. (*fep->ops->napi_enable_rx)(dev);
  178. }
  179. return received;
  180. }
  181. static int fs_enet_tx_napi(struct napi_struct *napi, int budget)
  182. {
  183. struct fs_enet_private *fep = container_of(napi, struct fs_enet_private,
  184. napi_tx);
  185. struct net_device *dev = fep->ndev;
  186. cbd_t __iomem *bdp;
  187. struct sk_buff *skb;
  188. int dirtyidx, do_wake, do_restart;
  189. u16 sc;
  190. int has_tx_work = 0;
  191. spin_lock(&fep->tx_lock);
  192. bdp = fep->dirty_tx;
  193. /* clear TX status bits for napi*/
  194. (*fep->ops->napi_clear_tx_event)(dev);
  195. do_wake = do_restart = 0;
  196. while (((sc = CBDR_SC(bdp)) & BD_ENET_TX_READY) == 0) {
  197. dirtyidx = bdp - fep->tx_bd_base;
  198. if (fep->tx_free == fep->tx_ring)
  199. break;
  200. skb = fep->tx_skbuff[dirtyidx];
  201. /*
  202. * Check for errors.
  203. */
  204. if (sc & (BD_ENET_TX_HB | BD_ENET_TX_LC |
  205. BD_ENET_TX_RL | BD_ENET_TX_UN | BD_ENET_TX_CSL)) {
  206. if (sc & BD_ENET_TX_HB) /* No heartbeat */
  207. fep->stats.tx_heartbeat_errors++;
  208. if (sc & BD_ENET_TX_LC) /* Late collision */
  209. fep->stats.tx_window_errors++;
  210. if (sc & BD_ENET_TX_RL) /* Retrans limit */
  211. fep->stats.tx_aborted_errors++;
  212. if (sc & BD_ENET_TX_UN) /* Underrun */
  213. fep->stats.tx_fifo_errors++;
  214. if (sc & BD_ENET_TX_CSL) /* Carrier lost */
  215. fep->stats.tx_carrier_errors++;
  216. if (sc & (BD_ENET_TX_LC | BD_ENET_TX_RL | BD_ENET_TX_UN)) {
  217. fep->stats.tx_errors++;
  218. do_restart = 1;
  219. }
  220. } else
  221. fep->stats.tx_packets++;
  222. if (sc & BD_ENET_TX_READY) {
  223. dev_warn(fep->dev,
  224. "HEY! Enet xmit interrupt and TX_READY.\n");
  225. }
  226. /*
  227. * Deferred means some collisions occurred during transmit,
  228. * but we eventually sent the packet OK.
  229. */
  230. if (sc & BD_ENET_TX_DEF)
  231. fep->stats.collisions++;
  232. /* unmap */
  233. if (fep->mapped_as_page[dirtyidx])
  234. dma_unmap_page(fep->dev, CBDR_BUFADDR(bdp),
  235. CBDR_DATLEN(bdp), DMA_TO_DEVICE);
  236. else
  237. dma_unmap_single(fep->dev, CBDR_BUFADDR(bdp),
  238. CBDR_DATLEN(bdp), DMA_TO_DEVICE);
  239. /*
  240. * Free the sk buffer associated with this last transmit.
  241. */
  242. if (skb) {
  243. dev_kfree_skb(skb);
  244. fep->tx_skbuff[dirtyidx] = NULL;
  245. }
  246. /*
  247. * Update pointer to next buffer descriptor to be transmitted.
  248. */
  249. if ((sc & BD_ENET_TX_WRAP) == 0)
  250. bdp++;
  251. else
  252. bdp = fep->tx_bd_base;
  253. /*
  254. * Since we have freed up a buffer, the ring is no longer
  255. * full.
  256. */
  257. if (++fep->tx_free >= MAX_SKB_FRAGS)
  258. do_wake = 1;
  259. has_tx_work = 1;
  260. }
  261. fep->dirty_tx = bdp;
  262. if (do_restart)
  263. (*fep->ops->tx_restart)(dev);
  264. if (!has_tx_work) {
  265. napi_complete(napi);
  266. (*fep->ops->napi_enable_tx)(dev);
  267. }
  268. spin_unlock(&fep->tx_lock);
  269. if (do_wake)
  270. netif_wake_queue(dev);
  271. if (has_tx_work)
  272. return budget;
  273. return 0;
  274. }
  275. /*
  276. * The interrupt handler.
  277. * This is called from the MPC core interrupt.
  278. */
  279. static irqreturn_t
  280. fs_enet_interrupt(int irq, void *dev_id)
  281. {
  282. struct net_device *dev = dev_id;
  283. struct fs_enet_private *fep;
  284. const struct fs_platform_info *fpi;
  285. u32 int_events;
  286. u32 int_clr_events;
  287. int nr, napi_ok;
  288. int handled;
  289. fep = netdev_priv(dev);
  290. fpi = fep->fpi;
  291. nr = 0;
  292. while ((int_events = (*fep->ops->get_int_events)(dev)) != 0) {
  293. nr++;
  294. int_clr_events = int_events;
  295. int_clr_events &= ~fep->ev_napi_rx;
  296. (*fep->ops->clear_int_events)(dev, int_clr_events);
  297. if (int_events & fep->ev_err)
  298. (*fep->ops->ev_error)(dev, int_events);
  299. if (int_events & fep->ev_rx) {
  300. napi_ok = napi_schedule_prep(&fep->napi);
  301. (*fep->ops->napi_disable_rx)(dev);
  302. (*fep->ops->clear_int_events)(dev, fep->ev_napi_rx);
  303. /* NOTE: it is possible for FCCs in NAPI mode */
  304. /* to submit a spurious interrupt while in poll */
  305. if (napi_ok)
  306. __napi_schedule(&fep->napi);
  307. }
  308. if (int_events & fep->ev_tx) {
  309. napi_ok = napi_schedule_prep(&fep->napi_tx);
  310. (*fep->ops->napi_disable_tx)(dev);
  311. (*fep->ops->clear_int_events)(dev, fep->ev_napi_tx);
  312. /* NOTE: it is possible for FCCs in NAPI mode */
  313. /* to submit a spurious interrupt while in poll */
  314. if (napi_ok)
  315. __napi_schedule(&fep->napi_tx);
  316. }
  317. }
  318. handled = nr > 0;
  319. return IRQ_RETVAL(handled);
  320. }
  321. void fs_init_bds(struct net_device *dev)
  322. {
  323. struct fs_enet_private *fep = netdev_priv(dev);
  324. cbd_t __iomem *bdp;
  325. struct sk_buff *skb;
  326. int i;
  327. fs_cleanup_bds(dev);
  328. fep->dirty_tx = fep->cur_tx = fep->tx_bd_base;
  329. fep->tx_free = fep->tx_ring;
  330. fep->cur_rx = fep->rx_bd_base;
  331. /*
  332. * Initialize the receive buffer descriptors.
  333. */
  334. for (i = 0, bdp = fep->rx_bd_base; i < fep->rx_ring; i++, bdp++) {
  335. skb = netdev_alloc_skb(dev, ENET_RX_FRSIZE);
  336. if (skb == NULL)
  337. break;
  338. skb_align(skb, ENET_RX_ALIGN);
  339. fep->rx_skbuff[i] = skb;
  340. CBDW_BUFADDR(bdp,
  341. dma_map_single(fep->dev, skb->data,
  342. L1_CACHE_ALIGN(PKT_MAXBUF_SIZE),
  343. DMA_FROM_DEVICE));
  344. CBDW_DATLEN(bdp, 0); /* zero */
  345. CBDW_SC(bdp, BD_ENET_RX_EMPTY |
  346. ((i < fep->rx_ring - 1) ? 0 : BD_SC_WRAP));
  347. }
  348. /*
  349. * if we failed, fillup remainder
  350. */
  351. for (; i < fep->rx_ring; i++, bdp++) {
  352. fep->rx_skbuff[i] = NULL;
  353. CBDW_SC(bdp, (i < fep->rx_ring - 1) ? 0 : BD_SC_WRAP);
  354. }
  355. /*
  356. * ...and the same for transmit.
  357. */
  358. for (i = 0, bdp = fep->tx_bd_base; i < fep->tx_ring; i++, bdp++) {
  359. fep->tx_skbuff[i] = NULL;
  360. CBDW_BUFADDR(bdp, 0);
  361. CBDW_DATLEN(bdp, 0);
  362. CBDW_SC(bdp, (i < fep->tx_ring - 1) ? 0 : BD_SC_WRAP);
  363. }
  364. }
  365. void fs_cleanup_bds(struct net_device *dev)
  366. {
  367. struct fs_enet_private *fep = netdev_priv(dev);
  368. struct sk_buff *skb;
  369. cbd_t __iomem *bdp;
  370. int i;
  371. /*
  372. * Reset SKB transmit buffers.
  373. */
  374. for (i = 0, bdp = fep->tx_bd_base; i < fep->tx_ring; i++, bdp++) {
  375. if ((skb = fep->tx_skbuff[i]) == NULL)
  376. continue;
  377. /* unmap */
  378. dma_unmap_single(fep->dev, CBDR_BUFADDR(bdp),
  379. skb->len, DMA_TO_DEVICE);
  380. fep->tx_skbuff[i] = NULL;
  381. dev_kfree_skb(skb);
  382. }
  383. /*
  384. * Reset SKB receive buffers
  385. */
  386. for (i = 0, bdp = fep->rx_bd_base; i < fep->rx_ring; i++, bdp++) {
  387. if ((skb = fep->rx_skbuff[i]) == NULL)
  388. continue;
  389. /* unmap */
  390. dma_unmap_single(fep->dev, CBDR_BUFADDR(bdp),
  391. L1_CACHE_ALIGN(PKT_MAXBUF_SIZE),
  392. DMA_FROM_DEVICE);
  393. fep->rx_skbuff[i] = NULL;
  394. dev_kfree_skb(skb);
  395. }
  396. }
  397. /**********************************************************************************/
  398. #ifdef CONFIG_FS_ENET_MPC5121_FEC
  399. /*
  400. * MPC5121 FEC requeries 4-byte alignment for TX data buffer!
  401. */
  402. static struct sk_buff *tx_skb_align_workaround(struct net_device *dev,
  403. struct sk_buff *skb)
  404. {
  405. struct sk_buff *new_skb;
  406. if (skb_linearize(skb))
  407. return NULL;
  408. /* Alloc new skb */
  409. new_skb = netdev_alloc_skb(dev, skb->len + 4);
  410. if (!new_skb)
  411. return NULL;
  412. /* Make sure new skb is properly aligned */
  413. skb_align(new_skb, 4);
  414. /* Copy data to new skb ... */
  415. skb_copy_from_linear_data(skb, new_skb->data, skb->len);
  416. skb_put(new_skb, skb->len);
  417. /* ... and free an old one */
  418. dev_kfree_skb_any(skb);
  419. return new_skb;
  420. }
  421. #endif
  422. static int fs_enet_start_xmit(struct sk_buff *skb, struct net_device *dev)
  423. {
  424. struct fs_enet_private *fep = netdev_priv(dev);
  425. cbd_t __iomem *bdp;
  426. int curidx;
  427. u16 sc;
  428. int nr_frags;
  429. skb_frag_t *frag;
  430. int len;
  431. #ifdef CONFIG_FS_ENET_MPC5121_FEC
  432. int is_aligned = 1;
  433. int i;
  434. if (!IS_ALIGNED((unsigned long)skb->data, 4)) {
  435. is_aligned = 0;
  436. } else {
  437. nr_frags = skb_shinfo(skb)->nr_frags;
  438. frag = skb_shinfo(skb)->frags;
  439. for (i = 0; i < nr_frags; i++, frag++) {
  440. if (!IS_ALIGNED(frag->page_offset, 4)) {
  441. is_aligned = 0;
  442. break;
  443. }
  444. }
  445. }
  446. if (!is_aligned) {
  447. skb = tx_skb_align_workaround(dev, skb);
  448. if (!skb) {
  449. /*
  450. * We have lost packet due to memory allocation error
  451. * in tx_skb_align_workaround(). Hopefully original
  452. * skb is still valid, so try transmit it later.
  453. */
  454. return NETDEV_TX_BUSY;
  455. }
  456. }
  457. #endif
  458. spin_lock(&fep->tx_lock);
  459. /*
  460. * Fill in a Tx ring entry
  461. */
  462. bdp = fep->cur_tx;
  463. nr_frags = skb_shinfo(skb)->nr_frags;
  464. if (fep->tx_free <= nr_frags || (CBDR_SC(bdp) & BD_ENET_TX_READY)) {
  465. netif_stop_queue(dev);
  466. spin_unlock(&fep->tx_lock);
  467. /*
  468. * Ooops. All transmit buffers are full. Bail out.
  469. * This should not happen, since the tx queue should be stopped.
  470. */
  471. dev_warn(fep->dev, "tx queue full!.\n");
  472. return NETDEV_TX_BUSY;
  473. }
  474. curidx = bdp - fep->tx_bd_base;
  475. len = skb->len;
  476. fep->stats.tx_bytes += len;
  477. if (nr_frags)
  478. len -= skb->data_len;
  479. fep->tx_free -= nr_frags + 1;
  480. /*
  481. * Push the data cache so the CPM does not get stale memory data.
  482. */
  483. CBDW_BUFADDR(bdp, dma_map_single(fep->dev,
  484. skb->data, len, DMA_TO_DEVICE));
  485. CBDW_DATLEN(bdp, len);
  486. fep->mapped_as_page[curidx] = 0;
  487. frag = skb_shinfo(skb)->frags;
  488. while (nr_frags) {
  489. CBDC_SC(bdp,
  490. BD_ENET_TX_STATS | BD_ENET_TX_INTR | BD_ENET_TX_LAST |
  491. BD_ENET_TX_TC);
  492. CBDS_SC(bdp, BD_ENET_TX_READY);
  493. if ((CBDR_SC(bdp) & BD_ENET_TX_WRAP) == 0)
  494. bdp++, curidx++;
  495. else
  496. bdp = fep->tx_bd_base, curidx = 0;
  497. len = skb_frag_size(frag);
  498. CBDW_BUFADDR(bdp, skb_frag_dma_map(fep->dev, frag, 0, len,
  499. DMA_TO_DEVICE));
  500. CBDW_DATLEN(bdp, len);
  501. fep->tx_skbuff[curidx] = NULL;
  502. fep->mapped_as_page[curidx] = 1;
  503. frag++;
  504. nr_frags--;
  505. }
  506. /* Trigger transmission start */
  507. sc = BD_ENET_TX_READY | BD_ENET_TX_INTR |
  508. BD_ENET_TX_LAST | BD_ENET_TX_TC;
  509. /* note that while FEC does not have this bit
  510. * it marks it as available for software use
  511. * yay for hw reuse :) */
  512. if (skb->len <= 60)
  513. sc |= BD_ENET_TX_PAD;
  514. CBDC_SC(bdp, BD_ENET_TX_STATS);
  515. CBDS_SC(bdp, sc);
  516. /* Save skb pointer. */
  517. fep->tx_skbuff[curidx] = skb;
  518. /* If this was the last BD in the ring, start at the beginning again. */
  519. if ((CBDR_SC(bdp) & BD_ENET_TX_WRAP) == 0)
  520. bdp++;
  521. else
  522. bdp = fep->tx_bd_base;
  523. fep->cur_tx = bdp;
  524. if (fep->tx_free < MAX_SKB_FRAGS)
  525. netif_stop_queue(dev);
  526. skb_tx_timestamp(skb);
  527. (*fep->ops->tx_kickstart)(dev);
  528. spin_unlock(&fep->tx_lock);
  529. return NETDEV_TX_OK;
  530. }
  531. static void fs_timeout(struct net_device *dev)
  532. {
  533. struct fs_enet_private *fep = netdev_priv(dev);
  534. unsigned long flags;
  535. int wake = 0;
  536. fep->stats.tx_errors++;
  537. spin_lock_irqsave(&fep->lock, flags);
  538. if (dev->flags & IFF_UP) {
  539. phy_stop(fep->phydev);
  540. (*fep->ops->stop)(dev);
  541. (*fep->ops->restart)(dev);
  542. phy_start(fep->phydev);
  543. }
  544. phy_start(fep->phydev);
  545. wake = fep->tx_free && !(CBDR_SC(fep->cur_tx) & BD_ENET_TX_READY);
  546. spin_unlock_irqrestore(&fep->lock, flags);
  547. if (wake)
  548. netif_wake_queue(dev);
  549. }
  550. /*-----------------------------------------------------------------------------
  551. * generic link-change handler - should be sufficient for most cases
  552. *-----------------------------------------------------------------------------*/
  553. static void generic_adjust_link(struct net_device *dev)
  554. {
  555. struct fs_enet_private *fep = netdev_priv(dev);
  556. struct phy_device *phydev = fep->phydev;
  557. int new_state = 0;
  558. if (phydev->link) {
  559. /* adjust to duplex mode */
  560. if (phydev->duplex != fep->oldduplex) {
  561. new_state = 1;
  562. fep->oldduplex = phydev->duplex;
  563. }
  564. if (phydev->speed != fep->oldspeed) {
  565. new_state = 1;
  566. fep->oldspeed = phydev->speed;
  567. }
  568. if (!fep->oldlink) {
  569. new_state = 1;
  570. fep->oldlink = 1;
  571. }
  572. if (new_state)
  573. fep->ops->restart(dev);
  574. } else if (fep->oldlink) {
  575. new_state = 1;
  576. fep->oldlink = 0;
  577. fep->oldspeed = 0;
  578. fep->oldduplex = -1;
  579. }
  580. if (new_state && netif_msg_link(fep))
  581. phy_print_status(phydev);
  582. }
  583. static void fs_adjust_link(struct net_device *dev)
  584. {
  585. struct fs_enet_private *fep = netdev_priv(dev);
  586. unsigned long flags;
  587. spin_lock_irqsave(&fep->lock, flags);
  588. if(fep->ops->adjust_link)
  589. fep->ops->adjust_link(dev);
  590. else
  591. generic_adjust_link(dev);
  592. spin_unlock_irqrestore(&fep->lock, flags);
  593. }
  594. static int fs_init_phy(struct net_device *dev)
  595. {
  596. struct fs_enet_private *fep = netdev_priv(dev);
  597. struct phy_device *phydev;
  598. phy_interface_t iface;
  599. fep->oldlink = 0;
  600. fep->oldspeed = 0;
  601. fep->oldduplex = -1;
  602. iface = fep->fpi->use_rmii ?
  603. PHY_INTERFACE_MODE_RMII : PHY_INTERFACE_MODE_MII;
  604. phydev = of_phy_connect(dev, fep->fpi->phy_node, &fs_adjust_link, 0,
  605. iface);
  606. if (!phydev) {
  607. dev_err(&dev->dev, "Could not attach to PHY\n");
  608. return -ENODEV;
  609. }
  610. fep->phydev = phydev;
  611. return 0;
  612. }
  613. static int fs_enet_open(struct net_device *dev)
  614. {
  615. struct fs_enet_private *fep = netdev_priv(dev);
  616. int r;
  617. int err;
  618. /* to initialize the fep->cur_rx,... */
  619. /* not doing this, will cause a crash in fs_enet_rx_napi */
  620. fs_init_bds(fep->ndev);
  621. napi_enable(&fep->napi);
  622. napi_enable(&fep->napi_tx);
  623. /* Install our interrupt handler. */
  624. r = request_irq(fep->interrupt, fs_enet_interrupt, IRQF_SHARED,
  625. "fs_enet-mac", dev);
  626. if (r != 0) {
  627. dev_err(fep->dev, "Could not allocate FS_ENET IRQ!");
  628. napi_disable(&fep->napi);
  629. napi_disable(&fep->napi_tx);
  630. return -EINVAL;
  631. }
  632. err = fs_init_phy(dev);
  633. if (err) {
  634. free_irq(fep->interrupt, dev);
  635. napi_disable(&fep->napi);
  636. napi_disable(&fep->napi_tx);
  637. return err;
  638. }
  639. phy_start(fep->phydev);
  640. netif_start_queue(dev);
  641. return 0;
  642. }
  643. static int fs_enet_close(struct net_device *dev)
  644. {
  645. struct fs_enet_private *fep = netdev_priv(dev);
  646. unsigned long flags;
  647. netif_stop_queue(dev);
  648. netif_carrier_off(dev);
  649. napi_disable(&fep->napi);
  650. napi_disable(&fep->napi_tx);
  651. phy_stop(fep->phydev);
  652. spin_lock_irqsave(&fep->lock, flags);
  653. spin_lock(&fep->tx_lock);
  654. (*fep->ops->stop)(dev);
  655. spin_unlock(&fep->tx_lock);
  656. spin_unlock_irqrestore(&fep->lock, flags);
  657. /* release any irqs */
  658. phy_disconnect(fep->phydev);
  659. fep->phydev = NULL;
  660. free_irq(fep->interrupt, dev);
  661. return 0;
  662. }
  663. static struct net_device_stats *fs_enet_get_stats(struct net_device *dev)
  664. {
  665. struct fs_enet_private *fep = netdev_priv(dev);
  666. return &fep->stats;
  667. }
  668. /*************************************************************************/
  669. static void fs_get_drvinfo(struct net_device *dev,
  670. struct ethtool_drvinfo *info)
  671. {
  672. strlcpy(info->driver, DRV_MODULE_NAME, sizeof(info->driver));
  673. strlcpy(info->version, DRV_MODULE_VERSION, sizeof(info->version));
  674. }
  675. static int fs_get_regs_len(struct net_device *dev)
  676. {
  677. struct fs_enet_private *fep = netdev_priv(dev);
  678. return (*fep->ops->get_regs_len)(dev);
  679. }
  680. static void fs_get_regs(struct net_device *dev, struct ethtool_regs *regs,
  681. void *p)
  682. {
  683. struct fs_enet_private *fep = netdev_priv(dev);
  684. unsigned long flags;
  685. int r, len;
  686. len = regs->len;
  687. spin_lock_irqsave(&fep->lock, flags);
  688. r = (*fep->ops->get_regs)(dev, p, &len);
  689. spin_unlock_irqrestore(&fep->lock, flags);
  690. if (r == 0)
  691. regs->version = 0;
  692. }
  693. static int fs_get_settings(struct net_device *dev, struct ethtool_cmd *cmd)
  694. {
  695. struct fs_enet_private *fep = netdev_priv(dev);
  696. if (!fep->phydev)
  697. return -ENODEV;
  698. return phy_ethtool_gset(fep->phydev, cmd);
  699. }
  700. static int fs_set_settings(struct net_device *dev, struct ethtool_cmd *cmd)
  701. {
  702. struct fs_enet_private *fep = netdev_priv(dev);
  703. if (!fep->phydev)
  704. return -ENODEV;
  705. return phy_ethtool_sset(fep->phydev, cmd);
  706. }
  707. static int fs_nway_reset(struct net_device *dev)
  708. {
  709. return 0;
  710. }
  711. static u32 fs_get_msglevel(struct net_device *dev)
  712. {
  713. struct fs_enet_private *fep = netdev_priv(dev);
  714. return fep->msg_enable;
  715. }
  716. static void fs_set_msglevel(struct net_device *dev, u32 value)
  717. {
  718. struct fs_enet_private *fep = netdev_priv(dev);
  719. fep->msg_enable = value;
  720. }
  721. static const struct ethtool_ops fs_ethtool_ops = {
  722. .get_drvinfo = fs_get_drvinfo,
  723. .get_regs_len = fs_get_regs_len,
  724. .get_settings = fs_get_settings,
  725. .set_settings = fs_set_settings,
  726. .nway_reset = fs_nway_reset,
  727. .get_link = ethtool_op_get_link,
  728. .get_msglevel = fs_get_msglevel,
  729. .set_msglevel = fs_set_msglevel,
  730. .get_regs = fs_get_regs,
  731. .get_ts_info = ethtool_op_get_ts_info,
  732. };
  733. static int fs_ioctl(struct net_device *dev, struct ifreq *rq, int cmd)
  734. {
  735. struct fs_enet_private *fep = netdev_priv(dev);
  736. if (!netif_running(dev))
  737. return -EINVAL;
  738. return phy_mii_ioctl(fep->phydev, rq, cmd);
  739. }
  740. extern int fs_mii_connect(struct net_device *dev);
  741. extern void fs_mii_disconnect(struct net_device *dev);
  742. /**************************************************************************************/
  743. #ifdef CONFIG_FS_ENET_HAS_FEC
  744. #define IS_FEC(match) ((match)->data == &fs_fec_ops)
  745. #else
  746. #define IS_FEC(match) 0
  747. #endif
  748. static const struct net_device_ops fs_enet_netdev_ops = {
  749. .ndo_open = fs_enet_open,
  750. .ndo_stop = fs_enet_close,
  751. .ndo_get_stats = fs_enet_get_stats,
  752. .ndo_start_xmit = fs_enet_start_xmit,
  753. .ndo_tx_timeout = fs_timeout,
  754. .ndo_set_rx_mode = fs_set_multicast_list,
  755. .ndo_do_ioctl = fs_ioctl,
  756. .ndo_validate_addr = eth_validate_addr,
  757. .ndo_set_mac_address = eth_mac_addr,
  758. .ndo_change_mtu = eth_change_mtu,
  759. #ifdef CONFIG_NET_POLL_CONTROLLER
  760. .ndo_poll_controller = fs_enet_netpoll,
  761. #endif
  762. };
  763. static const struct of_device_id fs_enet_match[];
  764. static int fs_enet_probe(struct platform_device *ofdev)
  765. {
  766. const struct of_device_id *match;
  767. struct net_device *ndev;
  768. struct fs_enet_private *fep;
  769. struct fs_platform_info *fpi;
  770. const u32 *data;
  771. struct clk *clk;
  772. int err;
  773. const u8 *mac_addr;
  774. const char *phy_connection_type;
  775. int privsize, len, ret = -ENODEV;
  776. match = of_match_device(fs_enet_match, &ofdev->dev);
  777. if (!match)
  778. return -EINVAL;
  779. fpi = kzalloc(sizeof(*fpi), GFP_KERNEL);
  780. if (!fpi)
  781. return -ENOMEM;
  782. if (!IS_FEC(match)) {
  783. data = of_get_property(ofdev->dev.of_node, "fsl,cpm-command", &len);
  784. if (!data || len != 4)
  785. goto out_free_fpi;
  786. fpi->cp_command = *data;
  787. }
  788. fpi->rx_ring = 32;
  789. fpi->tx_ring = 64;
  790. fpi->rx_copybreak = 240;
  791. fpi->napi_weight = 17;
  792. fpi->phy_node = of_parse_phandle(ofdev->dev.of_node, "phy-handle", 0);
  793. if (!fpi->phy_node && of_phy_is_fixed_link(ofdev->dev.of_node)) {
  794. err = of_phy_register_fixed_link(ofdev->dev.of_node);
  795. if (err)
  796. goto out_free_fpi;
  797. /* In the case of a fixed PHY, the DT node associated
  798. * to the PHY is the Ethernet MAC DT node.
  799. */
  800. fpi->phy_node = of_node_get(ofdev->dev.of_node);
  801. }
  802. if (of_device_is_compatible(ofdev->dev.of_node, "fsl,mpc5125-fec")) {
  803. phy_connection_type = of_get_property(ofdev->dev.of_node,
  804. "phy-connection-type", NULL);
  805. if (phy_connection_type && !strcmp("rmii", phy_connection_type))
  806. fpi->use_rmii = 1;
  807. }
  808. /* make clock lookup non-fatal (the driver is shared among platforms),
  809. * but require enable to succeed when a clock was specified/found,
  810. * keep a reference to the clock upon successful acquisition
  811. */
  812. clk = devm_clk_get(&ofdev->dev, "per");
  813. if (!IS_ERR(clk)) {
  814. err = clk_prepare_enable(clk);
  815. if (err) {
  816. ret = err;
  817. goto out_free_fpi;
  818. }
  819. fpi->clk_per = clk;
  820. }
  821. privsize = sizeof(*fep) +
  822. sizeof(struct sk_buff **) *
  823. (fpi->rx_ring + fpi->tx_ring) +
  824. sizeof(char) * fpi->tx_ring;
  825. ndev = alloc_etherdev(privsize);
  826. if (!ndev) {
  827. ret = -ENOMEM;
  828. goto out_put;
  829. }
  830. SET_NETDEV_DEV(ndev, &ofdev->dev);
  831. platform_set_drvdata(ofdev, ndev);
  832. fep = netdev_priv(ndev);
  833. fep->dev = &ofdev->dev;
  834. fep->ndev = ndev;
  835. fep->fpi = fpi;
  836. fep->ops = match->data;
  837. ret = fep->ops->setup_data(ndev);
  838. if (ret)
  839. goto out_free_dev;
  840. fep->rx_skbuff = (struct sk_buff **)&fep[1];
  841. fep->tx_skbuff = fep->rx_skbuff + fpi->rx_ring;
  842. fep->mapped_as_page = (char *)(fep->rx_skbuff + fpi->rx_ring +
  843. fpi->tx_ring);
  844. spin_lock_init(&fep->lock);
  845. spin_lock_init(&fep->tx_lock);
  846. mac_addr = of_get_mac_address(ofdev->dev.of_node);
  847. if (mac_addr)
  848. memcpy(ndev->dev_addr, mac_addr, ETH_ALEN);
  849. ret = fep->ops->allocate_bd(ndev);
  850. if (ret)
  851. goto out_cleanup_data;
  852. fep->rx_bd_base = fep->ring_base;
  853. fep->tx_bd_base = fep->rx_bd_base + fpi->rx_ring;
  854. fep->tx_ring = fpi->tx_ring;
  855. fep->rx_ring = fpi->rx_ring;
  856. ndev->netdev_ops = &fs_enet_netdev_ops;
  857. ndev->watchdog_timeo = 2 * HZ;
  858. netif_napi_add(ndev, &fep->napi, fs_enet_rx_napi, fpi->napi_weight);
  859. netif_napi_add(ndev, &fep->napi_tx, fs_enet_tx_napi, 2);
  860. ndev->ethtool_ops = &fs_ethtool_ops;
  861. init_timer(&fep->phy_timer_list);
  862. netif_carrier_off(ndev);
  863. ndev->features |= NETIF_F_SG;
  864. ret = register_netdev(ndev);
  865. if (ret)
  866. goto out_free_bd;
  867. pr_info("%s: fs_enet: %pM\n", ndev->name, ndev->dev_addr);
  868. return 0;
  869. out_free_bd:
  870. fep->ops->free_bd(ndev);
  871. out_cleanup_data:
  872. fep->ops->cleanup_data(ndev);
  873. out_free_dev:
  874. free_netdev(ndev);
  875. out_put:
  876. of_node_put(fpi->phy_node);
  877. if (fpi->clk_per)
  878. clk_disable_unprepare(fpi->clk_per);
  879. out_free_fpi:
  880. kfree(fpi);
  881. return ret;
  882. }
  883. static int fs_enet_remove(struct platform_device *ofdev)
  884. {
  885. struct net_device *ndev = platform_get_drvdata(ofdev);
  886. struct fs_enet_private *fep = netdev_priv(ndev);
  887. unregister_netdev(ndev);
  888. fep->ops->free_bd(ndev);
  889. fep->ops->cleanup_data(ndev);
  890. dev_set_drvdata(fep->dev, NULL);
  891. of_node_put(fep->fpi->phy_node);
  892. if (fep->fpi->clk_per)
  893. clk_disable_unprepare(fep->fpi->clk_per);
  894. free_netdev(ndev);
  895. return 0;
  896. }
  897. static const struct of_device_id fs_enet_match[] = {
  898. #ifdef CONFIG_FS_ENET_HAS_SCC
  899. {
  900. .compatible = "fsl,cpm1-scc-enet",
  901. .data = (void *)&fs_scc_ops,
  902. },
  903. {
  904. .compatible = "fsl,cpm2-scc-enet",
  905. .data = (void *)&fs_scc_ops,
  906. },
  907. #endif
  908. #ifdef CONFIG_FS_ENET_HAS_FCC
  909. {
  910. .compatible = "fsl,cpm2-fcc-enet",
  911. .data = (void *)&fs_fcc_ops,
  912. },
  913. #endif
  914. #ifdef CONFIG_FS_ENET_HAS_FEC
  915. #ifdef CONFIG_FS_ENET_MPC5121_FEC
  916. {
  917. .compatible = "fsl,mpc5121-fec",
  918. .data = (void *)&fs_fec_ops,
  919. },
  920. {
  921. .compatible = "fsl,mpc5125-fec",
  922. .data = (void *)&fs_fec_ops,
  923. },
  924. #else
  925. {
  926. .compatible = "fsl,pq1-fec-enet",
  927. .data = (void *)&fs_fec_ops,
  928. },
  929. #endif
  930. #endif
  931. {}
  932. };
  933. MODULE_DEVICE_TABLE(of, fs_enet_match);
  934. static struct platform_driver fs_enet_driver = {
  935. .driver = {
  936. .name = "fs_enet",
  937. .of_match_table = fs_enet_match,
  938. },
  939. .probe = fs_enet_probe,
  940. .remove = fs_enet_remove,
  941. };
  942. #ifdef CONFIG_NET_POLL_CONTROLLER
  943. static void fs_enet_netpoll(struct net_device *dev)
  944. {
  945. disable_irq(dev->irq);
  946. fs_enet_interrupt(dev->irq, dev);
  947. enable_irq(dev->irq);
  948. }
  949. #endif
  950. module_platform_driver(fs_enet_driver);