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