fs_enet-main.c 27 KB

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