fs_enet-main.c 27 KB

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