fs_enet-main.c 26 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134
  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/delay.h>
  27. #include <linux/netdevice.h>
  28. #include <linux/etherdevice.h>
  29. #include <linux/skbuff.h>
  30. #include <linux/spinlock.h>
  31. #include <linux/mii.h>
  32. #include <linux/ethtool.h>
  33. #include <linux/bitops.h>
  34. #include <linux/fs.h>
  35. #include <linux/platform_device.h>
  36. #include <linux/phy.h>
  37. #include <linux/of.h>
  38. #include <linux/of_mdio.h>
  39. #include <linux/of_platform.h>
  40. #include <linux/of_gpio.h>
  41. #include <linux/of_net.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;
  78. int received = 0;
  79. u16 pkt_len, sc;
  80. int curidx;
  81. if (budget <= 0)
  82. return received;
  83. /*
  84. * First, grab all of the stats for the incoming packet.
  85. * These get messed up if we get called due to a busy condition.
  86. */
  87. bdp = fep->cur_rx;
  88. /* clear RX status bits for napi*/
  89. (*fep->ops->napi_clear_rx_event)(dev);
  90. while (((sc = CBDR_SC(bdp)) & BD_ENET_RX_EMPTY) == 0) {
  91. curidx = bdp - fep->rx_bd_base;
  92. /*
  93. * Since we have allocated space to hold a complete frame,
  94. * the last indicator should be set.
  95. */
  96. if ((sc & BD_ENET_RX_LAST) == 0)
  97. dev_warn(fep->dev, "rcv is not +last\n");
  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 = netdev_alloc_skb(dev, 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(skb, skbn);
  140. }
  141. } else {
  142. skbn = netdev_alloc_skb(dev, 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. fep->stats.rx_dropped++;
  153. skbn = skb;
  154. }
  155. }
  156. fep->rx_skbuff[curidx] = skbn;
  157. CBDW_BUFADDR(bdp, dma_map_single(fep->dev, skbn->data,
  158. L1_CACHE_ALIGN(PKT_MAXBUF_SIZE),
  159. DMA_FROM_DEVICE));
  160. CBDW_DATLEN(bdp, 0);
  161. CBDW_SC(bdp, (sc & ~BD_ENET_RX_STATS) | BD_ENET_RX_EMPTY);
  162. /*
  163. * Update BD pointer to next entry.
  164. */
  165. if ((sc & BD_ENET_RX_WRAP) == 0)
  166. bdp++;
  167. else
  168. bdp = fep->rx_bd_base;
  169. (*fep->ops->rx_bd_done)(dev);
  170. if (received >= budget)
  171. break;
  172. }
  173. fep->cur_rx = bdp;
  174. if (received < budget) {
  175. /* done */
  176. napi_complete(napi);
  177. (*fep->ops->napi_enable_rx)(dev);
  178. }
  179. return received;
  180. }
  181. static int fs_enet_tx_napi(struct napi_struct *napi, int budget)
  182. {
  183. struct fs_enet_private *fep = container_of(napi, struct fs_enet_private,
  184. napi_tx);
  185. struct net_device *dev = fep->ndev;
  186. cbd_t __iomem *bdp;
  187. struct sk_buff *skb;
  188. int dirtyidx, do_wake, do_restart;
  189. u16 sc;
  190. int has_tx_work = 0;
  191. spin_lock(&fep->tx_lock);
  192. bdp = fep->dirty_tx;
  193. /* clear TX status bits for napi*/
  194. (*fep->ops->napi_clear_tx_event)(dev);
  195. do_wake = do_restart = 0;
  196. while (((sc = CBDR_SC(bdp)) & BD_ENET_TX_READY) == 0) {
  197. dirtyidx = bdp - fep->tx_bd_base;
  198. if (fep->tx_free == fep->tx_ring)
  199. break;
  200. skb = fep->tx_skbuff[dirtyidx];
  201. /*
  202. * Check for errors.
  203. */
  204. if (sc & (BD_ENET_TX_HB | BD_ENET_TX_LC |
  205. BD_ENET_TX_RL | BD_ENET_TX_UN | BD_ENET_TX_CSL)) {
  206. if (sc & BD_ENET_TX_HB) /* No heartbeat */
  207. fep->stats.tx_heartbeat_errors++;
  208. if (sc & BD_ENET_TX_LC) /* Late collision */
  209. fep->stats.tx_window_errors++;
  210. if (sc & BD_ENET_TX_RL) /* Retrans limit */
  211. fep->stats.tx_aborted_errors++;
  212. if (sc & BD_ENET_TX_UN) /* Underrun */
  213. fep->stats.tx_fifo_errors++;
  214. if (sc & BD_ENET_TX_CSL) /* Carrier lost */
  215. fep->stats.tx_carrier_errors++;
  216. if (sc & (BD_ENET_TX_LC | BD_ENET_TX_RL | BD_ENET_TX_UN)) {
  217. fep->stats.tx_errors++;
  218. do_restart = 1;
  219. }
  220. } else
  221. fep->stats.tx_packets++;
  222. if (sc & BD_ENET_TX_READY) {
  223. dev_warn(fep->dev,
  224. "HEY! Enet xmit interrupt and TX_READY.\n");
  225. }
  226. /*
  227. * Deferred means some collisions occurred during transmit,
  228. * but we eventually sent the packet OK.
  229. */
  230. if (sc & BD_ENET_TX_DEF)
  231. fep->stats.collisions++;
  232. /* unmap */
  233. if (fep->mapped_as_page[dirtyidx])
  234. dma_unmap_page(fep->dev, CBDR_BUFADDR(bdp),
  235. CBDR_DATLEN(bdp), DMA_TO_DEVICE);
  236. else
  237. dma_unmap_single(fep->dev, CBDR_BUFADDR(bdp),
  238. CBDR_DATLEN(bdp), DMA_TO_DEVICE);
  239. /*
  240. * Free the sk buffer associated with this last transmit.
  241. */
  242. if (skb) {
  243. dev_kfree_skb(skb);
  244. fep->tx_skbuff[dirtyidx] = NULL;
  245. }
  246. /*
  247. * Update pointer to next buffer descriptor to be transmitted.
  248. */
  249. if ((sc & BD_ENET_TX_WRAP) == 0)
  250. bdp++;
  251. else
  252. bdp = fep->tx_bd_base;
  253. /*
  254. * Since we have freed up a buffer, the ring is no longer
  255. * full.
  256. */
  257. if (++fep->tx_free >= MAX_SKB_FRAGS)
  258. do_wake = 1;
  259. has_tx_work = 1;
  260. }
  261. fep->dirty_tx = bdp;
  262. if (do_restart)
  263. (*fep->ops->tx_restart)(dev);
  264. if (!has_tx_work) {
  265. napi_complete(napi);
  266. (*fep->ops->napi_enable_tx)(dev);
  267. }
  268. spin_unlock(&fep->tx_lock);
  269. if (do_wake)
  270. netif_wake_queue(dev);
  271. if (has_tx_work)
  272. return budget;
  273. return 0;
  274. }
  275. /*
  276. * The interrupt handler.
  277. * This is called from the MPC core interrupt.
  278. */
  279. static irqreturn_t
  280. fs_enet_interrupt(int irq, void *dev_id)
  281. {
  282. struct net_device *dev = dev_id;
  283. struct fs_enet_private *fep;
  284. const struct fs_platform_info *fpi;
  285. u32 int_events;
  286. u32 int_clr_events;
  287. int nr, napi_ok;
  288. int handled;
  289. fep = netdev_priv(dev);
  290. fpi = fep->fpi;
  291. nr = 0;
  292. while ((int_events = (*fep->ops->get_int_events)(dev)) != 0) {
  293. nr++;
  294. int_clr_events = int_events;
  295. int_clr_events &= ~fep->ev_napi_rx;
  296. (*fep->ops->clear_int_events)(dev, int_clr_events);
  297. if (int_events & fep->ev_err)
  298. (*fep->ops->ev_error)(dev, int_events);
  299. if (int_events & fep->ev_rx) {
  300. napi_ok = napi_schedule_prep(&fep->napi);
  301. (*fep->ops->napi_disable_rx)(dev);
  302. (*fep->ops->clear_int_events)(dev, fep->ev_napi_rx);
  303. /* NOTE: it is possible for FCCs in NAPI mode */
  304. /* to submit a spurious interrupt while in poll */
  305. if (napi_ok)
  306. __napi_schedule(&fep->napi);
  307. }
  308. if (int_events & fep->ev_tx) {
  309. napi_ok = napi_schedule_prep(&fep->napi_tx);
  310. (*fep->ops->napi_disable_tx)(dev);
  311. (*fep->ops->clear_int_events)(dev, fep->ev_napi_tx);
  312. /* NOTE: it is possible for FCCs in NAPI mode */
  313. /* to submit a spurious interrupt while in poll */
  314. if (napi_ok)
  315. __napi_schedule(&fep->napi_tx);
  316. }
  317. }
  318. handled = nr > 0;
  319. return IRQ_RETVAL(handled);
  320. }
  321. void fs_init_bds(struct net_device *dev)
  322. {
  323. struct fs_enet_private *fep = netdev_priv(dev);
  324. cbd_t __iomem *bdp;
  325. struct sk_buff *skb;
  326. int i;
  327. fs_cleanup_bds(dev);
  328. fep->dirty_tx = fep->cur_tx = fep->tx_bd_base;
  329. fep->tx_free = fep->tx_ring;
  330. fep->cur_rx = fep->rx_bd_base;
  331. /*
  332. * Initialize the receive buffer descriptors.
  333. */
  334. for (i = 0, bdp = fep->rx_bd_base; i < fep->rx_ring; i++, bdp++) {
  335. skb = netdev_alloc_skb(dev, ENET_RX_FRSIZE);
  336. if (skb == NULL)
  337. break;
  338. skb_align(skb, ENET_RX_ALIGN);
  339. fep->rx_skbuff[i] = skb;
  340. CBDW_BUFADDR(bdp,
  341. dma_map_single(fep->dev, skb->data,
  342. L1_CACHE_ALIGN(PKT_MAXBUF_SIZE),
  343. DMA_FROM_DEVICE));
  344. CBDW_DATLEN(bdp, 0); /* zero */
  345. CBDW_SC(bdp, BD_ENET_RX_EMPTY |
  346. ((i < fep->rx_ring - 1) ? 0 : BD_SC_WRAP));
  347. }
  348. /*
  349. * if we failed, fillup remainder
  350. */
  351. for (; i < fep->rx_ring; i++, bdp++) {
  352. fep->rx_skbuff[i] = NULL;
  353. CBDW_SC(bdp, (i < fep->rx_ring - 1) ? 0 : BD_SC_WRAP);
  354. }
  355. /*
  356. * ...and the same for transmit.
  357. */
  358. for (i = 0, bdp = fep->tx_bd_base; i < fep->tx_ring; i++, bdp++) {
  359. fep->tx_skbuff[i] = NULL;
  360. CBDW_BUFADDR(bdp, 0);
  361. CBDW_DATLEN(bdp, 0);
  362. CBDW_SC(bdp, (i < fep->tx_ring - 1) ? 0 : BD_SC_WRAP);
  363. }
  364. }
  365. void fs_cleanup_bds(struct net_device *dev)
  366. {
  367. struct fs_enet_private *fep = netdev_priv(dev);
  368. struct sk_buff *skb;
  369. cbd_t __iomem *bdp;
  370. int i;
  371. /*
  372. * Reset SKB transmit buffers.
  373. */
  374. for (i = 0, bdp = fep->tx_bd_base; i < fep->tx_ring; i++, bdp++) {
  375. if ((skb = fep->tx_skbuff[i]) == NULL)
  376. continue;
  377. /* unmap */
  378. dma_unmap_single(fep->dev, CBDR_BUFADDR(bdp),
  379. skb->len, DMA_TO_DEVICE);
  380. fep->tx_skbuff[i] = NULL;
  381. dev_kfree_skb(skb);
  382. }
  383. /*
  384. * Reset SKB receive buffers
  385. */
  386. for (i = 0, bdp = fep->rx_bd_base; i < fep->rx_ring; i++, bdp++) {
  387. if ((skb = fep->rx_skbuff[i]) == NULL)
  388. continue;
  389. /* unmap */
  390. dma_unmap_single(fep->dev, CBDR_BUFADDR(bdp),
  391. L1_CACHE_ALIGN(PKT_MAXBUF_SIZE),
  392. DMA_FROM_DEVICE);
  393. fep->rx_skbuff[i] = NULL;
  394. dev_kfree_skb(skb);
  395. }
  396. }
  397. /**********************************************************************************/
  398. #ifdef CONFIG_FS_ENET_MPC5121_FEC
  399. /*
  400. * MPC5121 FEC requeries 4-byte alignment for TX data buffer!
  401. */
  402. static struct sk_buff *tx_skb_align_workaround(struct net_device *dev,
  403. struct sk_buff *skb)
  404. {
  405. struct sk_buff *new_skb;
  406. if (skb_linearize(skb))
  407. return NULL;
  408. /* Alloc new skb */
  409. new_skb = netdev_alloc_skb(dev, skb->len + 4);
  410. if (!new_skb)
  411. return NULL;
  412. /* Make sure new skb is properly aligned */
  413. skb_align(new_skb, 4);
  414. /* Copy data to new skb ... */
  415. skb_copy_from_linear_data(skb, new_skb->data, skb->len);
  416. skb_put(new_skb, skb->len);
  417. /* ... and free an old one */
  418. dev_kfree_skb_any(skb);
  419. return new_skb;
  420. }
  421. #endif
  422. static int fs_enet_start_xmit(struct sk_buff *skb, struct net_device *dev)
  423. {
  424. struct fs_enet_private *fep = netdev_priv(dev);
  425. cbd_t __iomem *bdp;
  426. int curidx;
  427. u16 sc;
  428. int nr_frags;
  429. skb_frag_t *frag;
  430. int len;
  431. #ifdef CONFIG_FS_ENET_MPC5121_FEC
  432. int is_aligned = 1;
  433. int i;
  434. if (!IS_ALIGNED((unsigned long)skb->data, 4)) {
  435. is_aligned = 0;
  436. } else {
  437. nr_frags = skb_shinfo(skb)->nr_frags;
  438. frag = skb_shinfo(skb)->frags;
  439. for (i = 0; i < nr_frags; i++, frag++) {
  440. if (!IS_ALIGNED(frag->page_offset, 4)) {
  441. is_aligned = 0;
  442. break;
  443. }
  444. }
  445. }
  446. if (!is_aligned) {
  447. skb = tx_skb_align_workaround(dev, skb);
  448. if (!skb) {
  449. /*
  450. * We have lost packet due to memory allocation error
  451. * in tx_skb_align_workaround(). Hopefully original
  452. * skb is still valid, so try transmit it later.
  453. */
  454. return NETDEV_TX_BUSY;
  455. }
  456. }
  457. #endif
  458. spin_lock(&fep->tx_lock);
  459. /*
  460. * Fill in a Tx ring entry
  461. */
  462. bdp = fep->cur_tx;
  463. nr_frags = skb_shinfo(skb)->nr_frags;
  464. if (fep->tx_free <= nr_frags || (CBDR_SC(bdp) & BD_ENET_TX_READY)) {
  465. netif_stop_queue(dev);
  466. spin_unlock(&fep->tx_lock);
  467. /*
  468. * Ooops. All transmit buffers are full. Bail out.
  469. * This should not happen, since the tx queue should be stopped.
  470. */
  471. dev_warn(fep->dev, "tx queue full!.\n");
  472. return NETDEV_TX_BUSY;
  473. }
  474. curidx = bdp - fep->tx_bd_base;
  475. len = skb->len;
  476. fep->stats.tx_bytes += len;
  477. if (nr_frags)
  478. len -= skb->data_len;
  479. fep->tx_free -= nr_frags + 1;
  480. /*
  481. * Push the data cache so the CPM does not get stale memory data.
  482. */
  483. CBDW_BUFADDR(bdp, dma_map_single(fep->dev,
  484. skb->data, len, DMA_TO_DEVICE));
  485. CBDW_DATLEN(bdp, len);
  486. fep->mapped_as_page[curidx] = 0;
  487. frag = skb_shinfo(skb)->frags;
  488. while (nr_frags) {
  489. CBDC_SC(bdp,
  490. BD_ENET_TX_STATS | BD_ENET_TX_INTR | BD_ENET_TX_LAST |
  491. BD_ENET_TX_TC);
  492. CBDS_SC(bdp, BD_ENET_TX_READY);
  493. if ((CBDR_SC(bdp) & BD_ENET_TX_WRAP) == 0)
  494. bdp++, curidx++;
  495. else
  496. bdp = fep->tx_bd_base, curidx = 0;
  497. len = skb_frag_size(frag);
  498. CBDW_BUFADDR(bdp, skb_frag_dma_map(fep->dev, frag, 0, len,
  499. DMA_TO_DEVICE));
  500. CBDW_DATLEN(bdp, len);
  501. fep->tx_skbuff[curidx] = NULL;
  502. fep->mapped_as_page[curidx] = 1;
  503. frag++;
  504. nr_frags--;
  505. }
  506. /* Trigger transmission start */
  507. sc = BD_ENET_TX_READY | BD_ENET_TX_INTR |
  508. BD_ENET_TX_LAST | BD_ENET_TX_TC;
  509. /* note that while FEC does not have this bit
  510. * it marks it as available for software use
  511. * yay for hw reuse :) */
  512. if (skb->len <= 60)
  513. sc |= BD_ENET_TX_PAD;
  514. CBDC_SC(bdp, BD_ENET_TX_STATS);
  515. CBDS_SC(bdp, sc);
  516. /* Save skb pointer. */
  517. fep->tx_skbuff[curidx] = skb;
  518. /* If this was the last BD in the ring, start at the beginning again. */
  519. if ((CBDR_SC(bdp) & BD_ENET_TX_WRAP) == 0)
  520. bdp++;
  521. else
  522. bdp = fep->tx_bd_base;
  523. fep->cur_tx = bdp;
  524. if (fep->tx_free < MAX_SKB_FRAGS)
  525. netif_stop_queue(dev);
  526. skb_tx_timestamp(skb);
  527. (*fep->ops->tx_kickstart)(dev);
  528. spin_unlock(&fep->tx_lock);
  529. return NETDEV_TX_OK;
  530. }
  531. static void fs_timeout(struct net_device *dev)
  532. {
  533. struct fs_enet_private *fep = netdev_priv(dev);
  534. unsigned long flags;
  535. int wake = 0;
  536. fep->stats.tx_errors++;
  537. spin_lock_irqsave(&fep->lock, flags);
  538. if (dev->flags & IFF_UP) {
  539. phy_stop(dev->phydev);
  540. (*fep->ops->stop)(dev);
  541. (*fep->ops->restart)(dev);
  542. phy_start(dev->phydev);
  543. }
  544. phy_start(dev->phydev);
  545. wake = fep->tx_free && !(CBDR_SC(fep->cur_tx) & BD_ENET_TX_READY);
  546. spin_unlock_irqrestore(&fep->lock, flags);
  547. if (wake)
  548. netif_wake_queue(dev);
  549. }
  550. /*-----------------------------------------------------------------------------
  551. * generic link-change handler - should be sufficient for most cases
  552. *-----------------------------------------------------------------------------*/
  553. static void generic_adjust_link(struct net_device *dev)
  554. {
  555. struct fs_enet_private *fep = netdev_priv(dev);
  556. struct phy_device *phydev = dev->phydev;
  557. int new_state = 0;
  558. if (phydev->link) {
  559. /* adjust to duplex mode */
  560. if (phydev->duplex != fep->oldduplex) {
  561. new_state = 1;
  562. fep->oldduplex = phydev->duplex;
  563. }
  564. if (phydev->speed != fep->oldspeed) {
  565. new_state = 1;
  566. fep->oldspeed = phydev->speed;
  567. }
  568. if (!fep->oldlink) {
  569. new_state = 1;
  570. fep->oldlink = 1;
  571. }
  572. if (new_state)
  573. fep->ops->restart(dev);
  574. } else if (fep->oldlink) {
  575. new_state = 1;
  576. fep->oldlink = 0;
  577. fep->oldspeed = 0;
  578. fep->oldduplex = -1;
  579. }
  580. if (new_state && netif_msg_link(fep))
  581. phy_print_status(phydev);
  582. }
  583. static void fs_adjust_link(struct net_device *dev)
  584. {
  585. struct fs_enet_private *fep = netdev_priv(dev);
  586. unsigned long flags;
  587. spin_lock_irqsave(&fep->lock, flags);
  588. if(fep->ops->adjust_link)
  589. fep->ops->adjust_link(dev);
  590. else
  591. generic_adjust_link(dev);
  592. spin_unlock_irqrestore(&fep->lock, flags);
  593. }
  594. static int fs_init_phy(struct net_device *dev)
  595. {
  596. struct fs_enet_private *fep = netdev_priv(dev);
  597. struct phy_device *phydev;
  598. phy_interface_t iface;
  599. fep->oldlink = 0;
  600. fep->oldspeed = 0;
  601. fep->oldduplex = -1;
  602. iface = fep->fpi->use_rmii ?
  603. PHY_INTERFACE_MODE_RMII : PHY_INTERFACE_MODE_MII;
  604. phydev = of_phy_connect(dev, fep->fpi->phy_node, &fs_adjust_link, 0,
  605. iface);
  606. if (!phydev) {
  607. dev_err(&dev->dev, "Could not attach to PHY\n");
  608. return -ENODEV;
  609. }
  610. return 0;
  611. }
  612. static int fs_enet_open(struct net_device *dev)
  613. {
  614. struct fs_enet_private *fep = netdev_priv(dev);
  615. int r;
  616. int err;
  617. /* to initialize the fep->cur_rx,... */
  618. /* not doing this, will cause a crash in fs_enet_rx_napi */
  619. fs_init_bds(fep->ndev);
  620. napi_enable(&fep->napi);
  621. napi_enable(&fep->napi_tx);
  622. /* Install our interrupt handler. */
  623. r = request_irq(fep->interrupt, fs_enet_interrupt, IRQF_SHARED,
  624. "fs_enet-mac", dev);
  625. if (r != 0) {
  626. dev_err(fep->dev, "Could not allocate FS_ENET IRQ!");
  627. napi_disable(&fep->napi);
  628. napi_disable(&fep->napi_tx);
  629. return -EINVAL;
  630. }
  631. err = fs_init_phy(dev);
  632. if (err) {
  633. free_irq(fep->interrupt, dev);
  634. napi_disable(&fep->napi);
  635. napi_disable(&fep->napi_tx);
  636. return err;
  637. }
  638. phy_start(dev->phydev);
  639. netif_start_queue(dev);
  640. return 0;
  641. }
  642. static int fs_enet_close(struct net_device *dev)
  643. {
  644. struct fs_enet_private *fep = netdev_priv(dev);
  645. unsigned long flags;
  646. netif_stop_queue(dev);
  647. netif_carrier_off(dev);
  648. napi_disable(&fep->napi);
  649. napi_disable(&fep->napi_tx);
  650. phy_stop(dev->phydev);
  651. spin_lock_irqsave(&fep->lock, flags);
  652. spin_lock(&fep->tx_lock);
  653. (*fep->ops->stop)(dev);
  654. spin_unlock(&fep->tx_lock);
  655. spin_unlock_irqrestore(&fep->lock, flags);
  656. /* release any irqs */
  657. phy_disconnect(dev->phydev);
  658. free_irq(fep->interrupt, dev);
  659. return 0;
  660. }
  661. static struct net_device_stats *fs_enet_get_stats(struct net_device *dev)
  662. {
  663. struct fs_enet_private *fep = netdev_priv(dev);
  664. return &fep->stats;
  665. }
  666. /*************************************************************************/
  667. static void fs_get_drvinfo(struct net_device *dev,
  668. struct ethtool_drvinfo *info)
  669. {
  670. strlcpy(info->driver, DRV_MODULE_NAME, sizeof(info->driver));
  671. strlcpy(info->version, DRV_MODULE_VERSION, sizeof(info->version));
  672. }
  673. static int fs_get_regs_len(struct net_device *dev)
  674. {
  675. struct fs_enet_private *fep = netdev_priv(dev);
  676. return (*fep->ops->get_regs_len)(dev);
  677. }
  678. static void fs_get_regs(struct net_device *dev, struct ethtool_regs *regs,
  679. void *p)
  680. {
  681. struct fs_enet_private *fep = netdev_priv(dev);
  682. unsigned long flags;
  683. int r, len;
  684. len = regs->len;
  685. spin_lock_irqsave(&fep->lock, flags);
  686. r = (*fep->ops->get_regs)(dev, p, &len);
  687. spin_unlock_irqrestore(&fep->lock, flags);
  688. if (r == 0)
  689. regs->version = 0;
  690. }
  691. static int fs_nway_reset(struct net_device *dev)
  692. {
  693. return 0;
  694. }
  695. static u32 fs_get_msglevel(struct net_device *dev)
  696. {
  697. struct fs_enet_private *fep = netdev_priv(dev);
  698. return fep->msg_enable;
  699. }
  700. static void fs_set_msglevel(struct net_device *dev, u32 value)
  701. {
  702. struct fs_enet_private *fep = netdev_priv(dev);
  703. fep->msg_enable = value;
  704. }
  705. static const struct ethtool_ops fs_ethtool_ops = {
  706. .get_drvinfo = fs_get_drvinfo,
  707. .get_regs_len = fs_get_regs_len,
  708. .nway_reset = fs_nway_reset,
  709. .get_link = ethtool_op_get_link,
  710. .get_msglevel = fs_get_msglevel,
  711. .set_msglevel = fs_set_msglevel,
  712. .get_regs = fs_get_regs,
  713. .get_ts_info = ethtool_op_get_ts_info,
  714. .get_link_ksettings = phy_ethtool_get_link_ksettings,
  715. .set_link_ksettings = phy_ethtool_set_link_ksettings,
  716. };
  717. static int fs_ioctl(struct net_device *dev, struct ifreq *rq, int cmd)
  718. {
  719. if (!netif_running(dev))
  720. return -EINVAL;
  721. return phy_mii_ioctl(dev->phydev, rq, cmd);
  722. }
  723. extern int fs_mii_connect(struct net_device *dev);
  724. extern void fs_mii_disconnect(struct net_device *dev);
  725. /**************************************************************************************/
  726. #ifdef CONFIG_FS_ENET_HAS_FEC
  727. #define IS_FEC(match) ((match)->data == &fs_fec_ops)
  728. #else
  729. #define IS_FEC(match) 0
  730. #endif
  731. static const struct net_device_ops fs_enet_netdev_ops = {
  732. .ndo_open = fs_enet_open,
  733. .ndo_stop = fs_enet_close,
  734. .ndo_get_stats = fs_enet_get_stats,
  735. .ndo_start_xmit = fs_enet_start_xmit,
  736. .ndo_tx_timeout = fs_timeout,
  737. .ndo_set_rx_mode = fs_set_multicast_list,
  738. .ndo_do_ioctl = fs_ioctl,
  739. .ndo_validate_addr = eth_validate_addr,
  740. .ndo_set_mac_address = eth_mac_addr,
  741. .ndo_change_mtu = eth_change_mtu,
  742. #ifdef CONFIG_NET_POLL_CONTROLLER
  743. .ndo_poll_controller = fs_enet_netpoll,
  744. #endif
  745. };
  746. static const struct of_device_id fs_enet_match[];
  747. static int fs_enet_probe(struct platform_device *ofdev)
  748. {
  749. const struct of_device_id *match;
  750. struct net_device *ndev;
  751. struct fs_enet_private *fep;
  752. struct fs_platform_info *fpi;
  753. const u32 *data;
  754. struct clk *clk;
  755. int err;
  756. const u8 *mac_addr;
  757. const char *phy_connection_type;
  758. int privsize, len, ret = -ENODEV;
  759. match = of_match_device(fs_enet_match, &ofdev->dev);
  760. if (!match)
  761. return -EINVAL;
  762. fpi = kzalloc(sizeof(*fpi), GFP_KERNEL);
  763. if (!fpi)
  764. return -ENOMEM;
  765. if (!IS_FEC(match)) {
  766. data = of_get_property(ofdev->dev.of_node, "fsl,cpm-command", &len);
  767. if (!data || len != 4)
  768. goto out_free_fpi;
  769. fpi->cp_command = *data;
  770. }
  771. fpi->rx_ring = 32;
  772. fpi->tx_ring = 64;
  773. fpi->rx_copybreak = 240;
  774. fpi->napi_weight = 17;
  775. fpi->phy_node = of_parse_phandle(ofdev->dev.of_node, "phy-handle", 0);
  776. if (!fpi->phy_node && of_phy_is_fixed_link(ofdev->dev.of_node)) {
  777. err = of_phy_register_fixed_link(ofdev->dev.of_node);
  778. if (err)
  779. goto out_free_fpi;
  780. /* In the case of a fixed PHY, the DT node associated
  781. * to the PHY is the Ethernet MAC DT node.
  782. */
  783. fpi->phy_node = of_node_get(ofdev->dev.of_node);
  784. }
  785. if (of_device_is_compatible(ofdev->dev.of_node, "fsl,mpc5125-fec")) {
  786. phy_connection_type = of_get_property(ofdev->dev.of_node,
  787. "phy-connection-type", NULL);
  788. if (phy_connection_type && !strcmp("rmii", phy_connection_type))
  789. fpi->use_rmii = 1;
  790. }
  791. /* make clock lookup non-fatal (the driver is shared among platforms),
  792. * but require enable to succeed when a clock was specified/found,
  793. * keep a reference to the clock upon successful acquisition
  794. */
  795. clk = devm_clk_get(&ofdev->dev, "per");
  796. if (!IS_ERR(clk)) {
  797. err = clk_prepare_enable(clk);
  798. if (err) {
  799. ret = err;
  800. goto out_free_fpi;
  801. }
  802. fpi->clk_per = clk;
  803. }
  804. privsize = sizeof(*fep) +
  805. sizeof(struct sk_buff **) *
  806. (fpi->rx_ring + fpi->tx_ring) +
  807. sizeof(char) * fpi->tx_ring;
  808. ndev = alloc_etherdev(privsize);
  809. if (!ndev) {
  810. ret = -ENOMEM;
  811. goto out_put;
  812. }
  813. SET_NETDEV_DEV(ndev, &ofdev->dev);
  814. platform_set_drvdata(ofdev, ndev);
  815. fep = netdev_priv(ndev);
  816. fep->dev = &ofdev->dev;
  817. fep->ndev = ndev;
  818. fep->fpi = fpi;
  819. fep->ops = match->data;
  820. ret = fep->ops->setup_data(ndev);
  821. if (ret)
  822. goto out_free_dev;
  823. fep->rx_skbuff = (struct sk_buff **)&fep[1];
  824. fep->tx_skbuff = fep->rx_skbuff + fpi->rx_ring;
  825. fep->mapped_as_page = (char *)(fep->rx_skbuff + fpi->rx_ring +
  826. fpi->tx_ring);
  827. spin_lock_init(&fep->lock);
  828. spin_lock_init(&fep->tx_lock);
  829. mac_addr = of_get_mac_address(ofdev->dev.of_node);
  830. if (mac_addr)
  831. memcpy(ndev->dev_addr, mac_addr, ETH_ALEN);
  832. ret = fep->ops->allocate_bd(ndev);
  833. if (ret)
  834. goto out_cleanup_data;
  835. fep->rx_bd_base = fep->ring_base;
  836. fep->tx_bd_base = fep->rx_bd_base + fpi->rx_ring;
  837. fep->tx_ring = fpi->tx_ring;
  838. fep->rx_ring = fpi->rx_ring;
  839. ndev->netdev_ops = &fs_enet_netdev_ops;
  840. ndev->watchdog_timeo = 2 * HZ;
  841. netif_napi_add(ndev, &fep->napi, fs_enet_rx_napi, fpi->napi_weight);
  842. netif_tx_napi_add(ndev, &fep->napi_tx, fs_enet_tx_napi, 2);
  843. ndev->ethtool_ops = &fs_ethtool_ops;
  844. init_timer(&fep->phy_timer_list);
  845. netif_carrier_off(ndev);
  846. ndev->features |= NETIF_F_SG;
  847. ret = register_netdev(ndev);
  848. if (ret)
  849. goto out_free_bd;
  850. pr_info("%s: fs_enet: %pM\n", ndev->name, ndev->dev_addr);
  851. return 0;
  852. out_free_bd:
  853. fep->ops->free_bd(ndev);
  854. out_cleanup_data:
  855. fep->ops->cleanup_data(ndev);
  856. out_free_dev:
  857. free_netdev(ndev);
  858. out_put:
  859. of_node_put(fpi->phy_node);
  860. if (fpi->clk_per)
  861. clk_disable_unprepare(fpi->clk_per);
  862. out_free_fpi:
  863. kfree(fpi);
  864. return ret;
  865. }
  866. static int fs_enet_remove(struct platform_device *ofdev)
  867. {
  868. struct net_device *ndev = platform_get_drvdata(ofdev);
  869. struct fs_enet_private *fep = netdev_priv(ndev);
  870. unregister_netdev(ndev);
  871. fep->ops->free_bd(ndev);
  872. fep->ops->cleanup_data(ndev);
  873. dev_set_drvdata(fep->dev, NULL);
  874. of_node_put(fep->fpi->phy_node);
  875. if (fep->fpi->clk_per)
  876. clk_disable_unprepare(fep->fpi->clk_per);
  877. free_netdev(ndev);
  878. return 0;
  879. }
  880. static const struct of_device_id fs_enet_match[] = {
  881. #ifdef CONFIG_FS_ENET_HAS_SCC
  882. {
  883. .compatible = "fsl,cpm1-scc-enet",
  884. .data = (void *)&fs_scc_ops,
  885. },
  886. {
  887. .compatible = "fsl,cpm2-scc-enet",
  888. .data = (void *)&fs_scc_ops,
  889. },
  890. #endif
  891. #ifdef CONFIG_FS_ENET_HAS_FCC
  892. {
  893. .compatible = "fsl,cpm2-fcc-enet",
  894. .data = (void *)&fs_fcc_ops,
  895. },
  896. #endif
  897. #ifdef CONFIG_FS_ENET_HAS_FEC
  898. #ifdef CONFIG_FS_ENET_MPC5121_FEC
  899. {
  900. .compatible = "fsl,mpc5121-fec",
  901. .data = (void *)&fs_fec_ops,
  902. },
  903. {
  904. .compatible = "fsl,mpc5125-fec",
  905. .data = (void *)&fs_fec_ops,
  906. },
  907. #else
  908. {
  909. .compatible = "fsl,pq1-fec-enet",
  910. .data = (void *)&fs_fec_ops,
  911. },
  912. #endif
  913. #endif
  914. {}
  915. };
  916. MODULE_DEVICE_TABLE(of, fs_enet_match);
  917. static struct platform_driver fs_enet_driver = {
  918. .driver = {
  919. .name = "fs_enet",
  920. .of_match_table = fs_enet_match,
  921. },
  922. .probe = fs_enet_probe,
  923. .remove = fs_enet_remove,
  924. };
  925. #ifdef CONFIG_NET_POLL_CONTROLLER
  926. static void fs_enet_netpoll(struct net_device *dev)
  927. {
  928. disable_irq(dev->irq);
  929. fs_enet_interrupt(dev->irq, dev);
  930. enable_irq(dev->irq);
  931. }
  932. #endif
  933. module_platform_driver(fs_enet_driver);