fs_enet-main.c 27 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231
  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. netif_tx_schedule_all(dev);
  608. netif_carrier_on(dev);
  609. netif_start_queue(dev);
  610. }
  611. if (new_state)
  612. fep->ops->restart(dev);
  613. } else if (fep->oldlink) {
  614. new_state = 1;
  615. fep->oldlink = 0;
  616. fep->oldspeed = 0;
  617. fep->oldduplex = -1;
  618. netif_carrier_off(dev);
  619. netif_stop_queue(dev);
  620. }
  621. if (new_state && netif_msg_link(fep))
  622. phy_print_status(phydev);
  623. }
  624. static void fs_adjust_link(struct net_device *dev)
  625. {
  626. struct fs_enet_private *fep = netdev_priv(dev);
  627. unsigned long flags;
  628. spin_lock_irqsave(&fep->lock, flags);
  629. if(fep->ops->adjust_link)
  630. fep->ops->adjust_link(dev);
  631. else
  632. generic_adjust_link(dev);
  633. spin_unlock_irqrestore(&fep->lock, flags);
  634. }
  635. static int fs_init_phy(struct net_device *dev)
  636. {
  637. struct fs_enet_private *fep = netdev_priv(dev);
  638. struct phy_device *phydev;
  639. fep->oldlink = 0;
  640. fep->oldspeed = 0;
  641. fep->oldduplex = -1;
  642. if(fep->fpi->bus_id)
  643. phydev = phy_connect(dev, fep->fpi->bus_id, &fs_adjust_link, 0,
  644. PHY_INTERFACE_MODE_MII);
  645. else {
  646. printk("No phy bus ID specified in BSP code\n");
  647. return -EINVAL;
  648. }
  649. if (IS_ERR(phydev)) {
  650. printk(KERN_ERR "%s: Could not attach to PHY\n", dev->name);
  651. return PTR_ERR(phydev);
  652. }
  653. fep->phydev = phydev;
  654. return 0;
  655. }
  656. static int fs_enet_open(struct net_device *dev)
  657. {
  658. struct fs_enet_private *fep = netdev_priv(dev);
  659. int r;
  660. int err;
  661. if (fep->fpi->use_napi)
  662. napi_enable(&fep->napi);
  663. /* Install our interrupt handler. */
  664. r = fs_request_irq(dev, fep->interrupt, "fs_enet-mac", fs_enet_interrupt);
  665. if (r != 0) {
  666. printk(KERN_ERR DRV_MODULE_NAME
  667. ": %s Could not allocate FS_ENET IRQ!", dev->name);
  668. if (fep->fpi->use_napi)
  669. napi_disable(&fep->napi);
  670. return -EINVAL;
  671. }
  672. err = fs_init_phy(dev);
  673. if (err) {
  674. if (fep->fpi->use_napi)
  675. napi_disable(&fep->napi);
  676. return err;
  677. }
  678. phy_start(fep->phydev);
  679. return 0;
  680. }
  681. static int fs_enet_close(struct net_device *dev)
  682. {
  683. struct fs_enet_private *fep = netdev_priv(dev);
  684. unsigned long flags;
  685. netif_stop_queue(dev);
  686. netif_carrier_off(dev);
  687. if (fep->fpi->use_napi)
  688. napi_disable(&fep->napi);
  689. phy_stop(fep->phydev);
  690. spin_lock_irqsave(&fep->lock, flags);
  691. spin_lock(&fep->tx_lock);
  692. (*fep->ops->stop)(dev);
  693. spin_unlock(&fep->tx_lock);
  694. spin_unlock_irqrestore(&fep->lock, flags);
  695. /* release any irqs */
  696. phy_disconnect(fep->phydev);
  697. fep->phydev = NULL;
  698. fs_free_irq(dev, fep->interrupt);
  699. return 0;
  700. }
  701. static struct net_device_stats *fs_enet_get_stats(struct net_device *dev)
  702. {
  703. struct fs_enet_private *fep = netdev_priv(dev);
  704. return &fep->stats;
  705. }
  706. /*************************************************************************/
  707. static void fs_get_drvinfo(struct net_device *dev,
  708. struct ethtool_drvinfo *info)
  709. {
  710. strcpy(info->driver, DRV_MODULE_NAME);
  711. strcpy(info->version, DRV_MODULE_VERSION);
  712. }
  713. static int fs_get_regs_len(struct net_device *dev)
  714. {
  715. struct fs_enet_private *fep = netdev_priv(dev);
  716. return (*fep->ops->get_regs_len)(dev);
  717. }
  718. static void fs_get_regs(struct net_device *dev, struct ethtool_regs *regs,
  719. void *p)
  720. {
  721. struct fs_enet_private *fep = netdev_priv(dev);
  722. unsigned long flags;
  723. int r, len;
  724. len = regs->len;
  725. spin_lock_irqsave(&fep->lock, flags);
  726. r = (*fep->ops->get_regs)(dev, p, &len);
  727. spin_unlock_irqrestore(&fep->lock, flags);
  728. if (r == 0)
  729. regs->version = 0;
  730. }
  731. static int fs_get_settings(struct net_device *dev, struct ethtool_cmd *cmd)
  732. {
  733. struct fs_enet_private *fep = netdev_priv(dev);
  734. if (!fep->phydev)
  735. return -ENODEV;
  736. return phy_ethtool_gset(fep->phydev, cmd);
  737. }
  738. static int fs_set_settings(struct net_device *dev, struct ethtool_cmd *cmd)
  739. {
  740. struct fs_enet_private *fep = netdev_priv(dev);
  741. if (!fep->phydev)
  742. return -ENODEV;
  743. return phy_ethtool_sset(fep->phydev, cmd);
  744. }
  745. static int fs_nway_reset(struct net_device *dev)
  746. {
  747. return 0;
  748. }
  749. static u32 fs_get_msglevel(struct net_device *dev)
  750. {
  751. struct fs_enet_private *fep = netdev_priv(dev);
  752. return fep->msg_enable;
  753. }
  754. static void fs_set_msglevel(struct net_device *dev, u32 value)
  755. {
  756. struct fs_enet_private *fep = netdev_priv(dev);
  757. fep->msg_enable = value;
  758. }
  759. static const struct ethtool_ops fs_ethtool_ops = {
  760. .get_drvinfo = fs_get_drvinfo,
  761. .get_regs_len = fs_get_regs_len,
  762. .get_settings = fs_get_settings,
  763. .set_settings = fs_set_settings,
  764. .nway_reset = fs_nway_reset,
  765. .get_link = ethtool_op_get_link,
  766. .get_msglevel = fs_get_msglevel,
  767. .set_msglevel = fs_set_msglevel,
  768. .set_tx_csum = ethtool_op_set_tx_csum, /* local! */
  769. .set_sg = ethtool_op_set_sg,
  770. .get_regs = fs_get_regs,
  771. };
  772. static int fs_ioctl(struct net_device *dev, struct ifreq *rq, int cmd)
  773. {
  774. struct fs_enet_private *fep = netdev_priv(dev);
  775. struct mii_ioctl_data *mii = (struct mii_ioctl_data *)&rq->ifr_data;
  776. if (!netif_running(dev))
  777. return -EINVAL;
  778. return phy_mii_ioctl(fep->phydev, mii, cmd);
  779. }
  780. extern int fs_mii_connect(struct net_device *dev);
  781. extern void fs_mii_disconnect(struct net_device *dev);
  782. /**************************************************************************************/
  783. /* handy pointer to the immap */
  784. void __iomem *fs_enet_immap = NULL;
  785. static int setup_immap(void)
  786. {
  787. #ifdef CONFIG_CPM1
  788. fs_enet_immap = ioremap(IMAP_ADDR, 0x4000);
  789. WARN_ON(!fs_enet_immap);
  790. #elif defined(CONFIG_CPM2)
  791. fs_enet_immap = cpm2_immr;
  792. #endif
  793. return 0;
  794. }
  795. static void cleanup_immap(void)
  796. {
  797. #if defined(CONFIG_CPM1)
  798. iounmap(fs_enet_immap);
  799. #endif
  800. }
  801. /**************************************************************************************/
  802. static int __devinit find_phy(struct device_node *np,
  803. struct fs_platform_info *fpi)
  804. {
  805. struct device_node *phynode, *mdionode;
  806. int ret = 0, len, bus_id;
  807. const u32 *data;
  808. data = of_get_property(np, "fixed-link", NULL);
  809. if (data) {
  810. snprintf(fpi->bus_id, 16, "%x:%02x", 0, *data);
  811. return 0;
  812. }
  813. data = of_get_property(np, "phy-handle", &len);
  814. if (!data || len != 4)
  815. return -EINVAL;
  816. phynode = of_find_node_by_phandle(*data);
  817. if (!phynode)
  818. return -EINVAL;
  819. data = of_get_property(phynode, "reg", &len);
  820. if (!data || len != 4) {
  821. ret = -EINVAL;
  822. goto out_put_phy;
  823. }
  824. mdionode = of_get_parent(phynode);
  825. if (!mdionode) {
  826. ret = -EINVAL;
  827. goto out_put_phy;
  828. }
  829. bus_id = of_get_gpio(mdionode, 0);
  830. if (bus_id < 0) {
  831. struct resource res;
  832. ret = of_address_to_resource(mdionode, 0, &res);
  833. if (ret)
  834. goto out_put_mdio;
  835. bus_id = res.start;
  836. }
  837. snprintf(fpi->bus_id, 16, "%x:%02x", bus_id, *data);
  838. out_put_mdio:
  839. of_node_put(mdionode);
  840. out_put_phy:
  841. of_node_put(phynode);
  842. return ret;
  843. }
  844. #ifdef CONFIG_FS_ENET_HAS_FEC
  845. #define IS_FEC(match) ((match)->data == &fs_fec_ops)
  846. #else
  847. #define IS_FEC(match) 0
  848. #endif
  849. static int __devinit fs_enet_probe(struct of_device *ofdev,
  850. const struct of_device_id *match)
  851. {
  852. struct net_device *ndev;
  853. struct fs_enet_private *fep;
  854. struct fs_platform_info *fpi;
  855. const u32 *data;
  856. const u8 *mac_addr;
  857. int privsize, len, ret = -ENODEV;
  858. fpi = kzalloc(sizeof(*fpi), GFP_KERNEL);
  859. if (!fpi)
  860. return -ENOMEM;
  861. if (!IS_FEC(match)) {
  862. data = of_get_property(ofdev->node, "fsl,cpm-command", &len);
  863. if (!data || len != 4)
  864. goto out_free_fpi;
  865. fpi->cp_command = *data;
  866. }
  867. fpi->rx_ring = 32;
  868. fpi->tx_ring = 32;
  869. fpi->rx_copybreak = 240;
  870. fpi->use_napi = 1;
  871. fpi->napi_weight = 17;
  872. ret = find_phy(ofdev->node, fpi);
  873. if (ret)
  874. goto out_free_fpi;
  875. privsize = sizeof(*fep) +
  876. sizeof(struct sk_buff **) *
  877. (fpi->rx_ring + fpi->tx_ring);
  878. ndev = alloc_etherdev(privsize);
  879. if (!ndev) {
  880. ret = -ENOMEM;
  881. goto out_free_fpi;
  882. }
  883. dev_set_drvdata(&ofdev->dev, ndev);
  884. fep = netdev_priv(ndev);
  885. fep->dev = &ofdev->dev;
  886. fep->ndev = ndev;
  887. fep->fpi = fpi;
  888. fep->ops = match->data;
  889. ret = fep->ops->setup_data(ndev);
  890. if (ret)
  891. goto out_free_dev;
  892. fep->rx_skbuff = (struct sk_buff **)&fep[1];
  893. fep->tx_skbuff = fep->rx_skbuff + fpi->rx_ring;
  894. spin_lock_init(&fep->lock);
  895. spin_lock_init(&fep->tx_lock);
  896. mac_addr = of_get_mac_address(ofdev->node);
  897. if (mac_addr)
  898. memcpy(ndev->dev_addr, mac_addr, 6);
  899. ret = fep->ops->allocate_bd(ndev);
  900. if (ret)
  901. goto out_cleanup_data;
  902. fep->rx_bd_base = fep->ring_base;
  903. fep->tx_bd_base = fep->rx_bd_base + fpi->rx_ring;
  904. fep->tx_ring = fpi->tx_ring;
  905. fep->rx_ring = fpi->rx_ring;
  906. ndev->open = fs_enet_open;
  907. ndev->hard_start_xmit = fs_enet_start_xmit;
  908. ndev->tx_timeout = fs_timeout;
  909. ndev->watchdog_timeo = 2 * HZ;
  910. ndev->stop = fs_enet_close;
  911. ndev->get_stats = fs_enet_get_stats;
  912. ndev->set_multicast_list = fs_set_multicast_list;
  913. if (fpi->use_napi)
  914. netif_napi_add(ndev, &fep->napi, fs_enet_rx_napi,
  915. fpi->napi_weight);
  916. ndev->ethtool_ops = &fs_ethtool_ops;
  917. ndev->do_ioctl = fs_ioctl;
  918. init_timer(&fep->phy_timer_list);
  919. netif_carrier_off(ndev);
  920. ret = register_netdev(ndev);
  921. if (ret)
  922. goto out_free_bd;
  923. printk(KERN_INFO "%s: fs_enet: %02x:%02x:%02x:%02x:%02x:%02x\n",
  924. ndev->name,
  925. ndev->dev_addr[0], ndev->dev_addr[1], ndev->dev_addr[2],
  926. ndev->dev_addr[3], ndev->dev_addr[4], ndev->dev_addr[5]);
  927. return 0;
  928. out_free_bd:
  929. fep->ops->free_bd(ndev);
  930. out_cleanup_data:
  931. fep->ops->cleanup_data(ndev);
  932. out_free_dev:
  933. free_netdev(ndev);
  934. dev_set_drvdata(&ofdev->dev, NULL);
  935. out_free_fpi:
  936. kfree(fpi);
  937. return ret;
  938. }
  939. static int fs_enet_remove(struct of_device *ofdev)
  940. {
  941. struct net_device *ndev = dev_get_drvdata(&ofdev->dev);
  942. struct fs_enet_private *fep = netdev_priv(ndev);
  943. unregister_netdev(ndev);
  944. fep->ops->free_bd(ndev);
  945. fep->ops->cleanup_data(ndev);
  946. dev_set_drvdata(fep->dev, NULL);
  947. free_netdev(ndev);
  948. return 0;
  949. }
  950. static struct of_device_id fs_enet_match[] = {
  951. #ifdef CONFIG_FS_ENET_HAS_SCC
  952. {
  953. .compatible = "fsl,cpm1-scc-enet",
  954. .data = (void *)&fs_scc_ops,
  955. },
  956. #endif
  957. #ifdef CONFIG_FS_ENET_HAS_FCC
  958. {
  959. .compatible = "fsl,cpm2-fcc-enet",
  960. .data = (void *)&fs_fcc_ops,
  961. },
  962. #endif
  963. #ifdef CONFIG_FS_ENET_HAS_FEC
  964. {
  965. .compatible = "fsl,pq1-fec-enet",
  966. .data = (void *)&fs_fec_ops,
  967. },
  968. #endif
  969. {}
  970. };
  971. static struct of_platform_driver fs_enet_driver = {
  972. .name = "fs_enet",
  973. .match_table = fs_enet_match,
  974. .probe = fs_enet_probe,
  975. .remove = fs_enet_remove,
  976. };
  977. static int __init fs_init(void)
  978. {
  979. int r = setup_immap();
  980. if (r != 0)
  981. return r;
  982. r = of_register_platform_driver(&fs_enet_driver);
  983. if (r != 0)
  984. goto out;
  985. return 0;
  986. out:
  987. cleanup_immap();
  988. return r;
  989. }
  990. static void __exit fs_cleanup(void)
  991. {
  992. of_unregister_platform_driver(&fs_enet_driver);
  993. cleanup_immap();
  994. }
  995. #ifdef CONFIG_NET_POLL_CONTROLLER
  996. static void fs_enet_netpoll(struct net_device *dev)
  997. {
  998. disable_irq(dev->irq);
  999. fs_enet_interrupt(dev->irq, dev, NULL);
  1000. enable_irq(dev->irq);
  1001. }
  1002. #endif
  1003. /**************************************************************************************/
  1004. module_init(fs_init);
  1005. module_exit(fs_cleanup);