fs_enet-main.c 27 KB

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