fs_enet-main.c 27 KB

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