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