fs_enet-main.c 26 KB

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