fs_enet-main.c 27 KB

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