fs_enet-main.c 26 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143
  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. #ifdef CONFIG_FS_ENET_HAS_FEC
  775. #define IS_FEC(match) ((match)->data == &fs_fec_ops)
  776. #else
  777. #define IS_FEC(match) 0
  778. #endif
  779. static const struct net_device_ops fs_enet_netdev_ops = {
  780. .ndo_open = fs_enet_open,
  781. .ndo_stop = fs_enet_close,
  782. .ndo_get_stats = fs_enet_get_stats,
  783. .ndo_start_xmit = fs_enet_start_xmit,
  784. .ndo_tx_timeout = fs_timeout,
  785. .ndo_set_multicast_list = fs_set_multicast_list,
  786. .ndo_do_ioctl = fs_ioctl,
  787. .ndo_validate_addr = eth_validate_addr,
  788. .ndo_set_mac_address = eth_mac_addr,
  789. .ndo_change_mtu = eth_change_mtu,
  790. #ifdef CONFIG_NET_POLL_CONTROLLER
  791. .ndo_poll_controller = fs_enet_netpoll,
  792. #endif
  793. };
  794. static int __devinit fs_enet_probe(struct of_device *ofdev,
  795. const struct of_device_id *match)
  796. {
  797. struct net_device *ndev;
  798. struct fs_enet_private *fep;
  799. struct fs_platform_info *fpi;
  800. const u32 *data;
  801. const u8 *mac_addr;
  802. int privsize, len, ret = -ENODEV;
  803. fpi = kzalloc(sizeof(*fpi), GFP_KERNEL);
  804. if (!fpi)
  805. return -ENOMEM;
  806. if (!IS_FEC(match)) {
  807. data = of_get_property(ofdev->node, "fsl,cpm-command", &len);
  808. if (!data || len != 4)
  809. goto out_free_fpi;
  810. fpi->cp_command = *data;
  811. }
  812. fpi->rx_ring = 32;
  813. fpi->tx_ring = 32;
  814. fpi->rx_copybreak = 240;
  815. fpi->use_napi = 1;
  816. fpi->napi_weight = 17;
  817. fpi->phy_node = of_parse_phandle(ofdev->node, "phy-handle", 0);
  818. if ((!fpi->phy_node) && (!of_get_property(ofdev->node, "fixed-link",
  819. NULL)))
  820. goto out_free_fpi;
  821. privsize = sizeof(*fep) +
  822. sizeof(struct sk_buff **) *
  823. (fpi->rx_ring + fpi->tx_ring);
  824. ndev = alloc_etherdev(privsize);
  825. if (!ndev) {
  826. ret = -ENOMEM;
  827. goto out_free_fpi;
  828. }
  829. dev_set_drvdata(&ofdev->dev, ndev);
  830. fep = netdev_priv(ndev);
  831. fep->dev = &ofdev->dev;
  832. fep->ndev = ndev;
  833. fep->fpi = fpi;
  834. fep->ops = match->data;
  835. ret = fep->ops->setup_data(ndev);
  836. if (ret)
  837. goto out_free_dev;
  838. fep->rx_skbuff = (struct sk_buff **)&fep[1];
  839. fep->tx_skbuff = fep->rx_skbuff + fpi->rx_ring;
  840. spin_lock_init(&fep->lock);
  841. spin_lock_init(&fep->tx_lock);
  842. mac_addr = of_get_mac_address(ofdev->node);
  843. if (mac_addr)
  844. memcpy(ndev->dev_addr, mac_addr, 6);
  845. ret = fep->ops->allocate_bd(ndev);
  846. if (ret)
  847. goto out_cleanup_data;
  848. fep->rx_bd_base = fep->ring_base;
  849. fep->tx_bd_base = fep->rx_bd_base + fpi->rx_ring;
  850. fep->tx_ring = fpi->tx_ring;
  851. fep->rx_ring = fpi->rx_ring;
  852. ndev->netdev_ops = &fs_enet_netdev_ops;
  853. ndev->watchdog_timeo = 2 * HZ;
  854. if (fpi->use_napi)
  855. netif_napi_add(ndev, &fep->napi, fs_enet_rx_napi,
  856. fpi->napi_weight);
  857. ndev->ethtool_ops = &fs_ethtool_ops;
  858. init_timer(&fep->phy_timer_list);
  859. netif_carrier_off(ndev);
  860. ret = register_netdev(ndev);
  861. if (ret)
  862. goto out_free_bd;
  863. printk(KERN_INFO "%s: fs_enet: %pM\n", ndev->name, ndev->dev_addr);
  864. return 0;
  865. out_free_bd:
  866. fep->ops->free_bd(ndev);
  867. out_cleanup_data:
  868. fep->ops->cleanup_data(ndev);
  869. out_free_dev:
  870. free_netdev(ndev);
  871. dev_set_drvdata(&ofdev->dev, NULL);
  872. of_node_put(fpi->phy_node);
  873. out_free_fpi:
  874. kfree(fpi);
  875. return ret;
  876. }
  877. static int fs_enet_remove(struct of_device *ofdev)
  878. {
  879. struct net_device *ndev = dev_get_drvdata(&ofdev->dev);
  880. struct fs_enet_private *fep = netdev_priv(ndev);
  881. unregister_netdev(ndev);
  882. fep->ops->free_bd(ndev);
  883. fep->ops->cleanup_data(ndev);
  884. dev_set_drvdata(fep->dev, NULL);
  885. of_node_put(fep->fpi->phy_node);
  886. free_netdev(ndev);
  887. return 0;
  888. }
  889. static struct of_device_id fs_enet_match[] = {
  890. #ifdef CONFIG_FS_ENET_HAS_SCC
  891. {
  892. .compatible = "fsl,cpm1-scc-enet",
  893. .data = (void *)&fs_scc_ops,
  894. },
  895. {
  896. .compatible = "fsl,cpm2-scc-enet",
  897. .data = (void *)&fs_scc_ops,
  898. },
  899. #endif
  900. #ifdef CONFIG_FS_ENET_HAS_FCC
  901. {
  902. .compatible = "fsl,cpm2-fcc-enet",
  903. .data = (void *)&fs_fcc_ops,
  904. },
  905. #endif
  906. #ifdef CONFIG_FS_ENET_HAS_FEC
  907. {
  908. .compatible = "fsl,pq1-fec-enet",
  909. .data = (void *)&fs_fec_ops,
  910. },
  911. #endif
  912. {}
  913. };
  914. static struct of_platform_driver fs_enet_driver = {
  915. .name = "fs_enet",
  916. .match_table = fs_enet_match,
  917. .probe = fs_enet_probe,
  918. .remove = fs_enet_remove,
  919. };
  920. static int __init fs_init(void)
  921. {
  922. return of_register_platform_driver(&fs_enet_driver);
  923. }
  924. static void __exit fs_cleanup(void)
  925. {
  926. of_unregister_platform_driver(&fs_enet_driver);
  927. }
  928. #ifdef CONFIG_NET_POLL_CONTROLLER
  929. static void fs_enet_netpoll(struct net_device *dev)
  930. {
  931. disable_irq(dev->irq);
  932. fs_enet_interrupt(dev->irq, dev);
  933. enable_irq(dev->irq);
  934. }
  935. #endif
  936. /**************************************************************************************/
  937. module_init(fs_init);
  938. module_exit(fs_cleanup);