fs_enet-main.c 27 KB

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