fs_enet-main.c 25 KB

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