fs_enet-main.c 28 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236
  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/init.h>
  27. #include <linux/delay.h>
  28. #include <linux/netdevice.h>
  29. #include <linux/etherdevice.h>
  30. #include <linux/skbuff.h>
  31. #include <linux/spinlock.h>
  32. #include <linux/mii.h>
  33. #include <linux/ethtool.h>
  34. #include <linux/bitops.h>
  35. #include <linux/fs.h>
  36. #include <linux/platform_device.h>
  37. #include <linux/phy.h>
  38. #include <linux/of_platform.h>
  39. #include <linux/of_gpio.h>
  40. #include <linux/vmalloc.h>
  41. #include <asm/pgtable.h>
  42. #include <asm/irq.h>
  43. #include <asm/uaccess.h>
  44. #include "fs_enet.h"
  45. /*************************************************/
  46. MODULE_AUTHOR("Pantelis Antoniou <panto@intracom.gr>");
  47. MODULE_DESCRIPTION("Freescale Ethernet Driver");
  48. MODULE_LICENSE("GPL");
  49. MODULE_VERSION(DRV_MODULE_VERSION);
  50. static int fs_enet_debug = -1; /* -1 == use FS_ENET_DEF_MSG_ENABLE as value */
  51. module_param(fs_enet_debug, int, 0);
  52. MODULE_PARM_DESC(fs_enet_debug,
  53. "Freescale bitmapped debugging message enable value");
  54. #ifdef CONFIG_NET_POLL_CONTROLLER
  55. static void fs_enet_netpoll(struct net_device *dev);
  56. #endif
  57. static void fs_set_multicast_list(struct net_device *dev)
  58. {
  59. struct fs_enet_private *fep = netdev_priv(dev);
  60. (*fep->ops->set_multicast_list)(dev);
  61. }
  62. static void skb_align(struct sk_buff *skb, int align)
  63. {
  64. int off = ((unsigned long)skb->data) & (align - 1);
  65. if (off)
  66. skb_reserve(skb, align - off);
  67. }
  68. /* NAPI receive function */
  69. static int fs_enet_rx_napi(struct napi_struct *napi, int budget)
  70. {
  71. struct fs_enet_private *fep = container_of(napi, struct fs_enet_private, napi);
  72. struct net_device *dev = fep->ndev;
  73. const struct fs_platform_info *fpi = fep->fpi;
  74. cbd_t __iomem *bdp;
  75. struct sk_buff *skb, *skbn, *skbt;
  76. int received = 0;
  77. u16 pkt_len, sc;
  78. int curidx;
  79. /*
  80. * First, grab all of the stats for the incoming packet.
  81. * These get messed up if we get called due to a busy condition.
  82. */
  83. bdp = fep->cur_rx;
  84. /* clear RX status bits for napi*/
  85. (*fep->ops->napi_clear_rx_event)(dev);
  86. while (((sc = CBDR_SC(bdp)) & BD_ENET_RX_EMPTY) == 0) {
  87. curidx = bdp - fep->rx_bd_base;
  88. /*
  89. * Since we have allocated space to hold a complete frame,
  90. * the last indicator should be set.
  91. */
  92. if ((sc & BD_ENET_RX_LAST) == 0)
  93. printk(KERN_WARNING DRV_MODULE_NAME
  94. ": %s rcv is not +last\n",
  95. dev->name);
  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 = dev_alloc_skb(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 = dev_alloc_skb(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. printk(KERN_WARNING DRV_MODULE_NAME
  154. ": %s Memory squeeze, dropping packet.\n",
  155. dev->name);
  156. fep->stats.rx_dropped++;
  157. skbn = skb;
  158. }
  159. }
  160. fep->rx_skbuff[curidx] = skbn;
  161. CBDW_BUFADDR(bdp, dma_map_single(fep->dev, skbn->data,
  162. L1_CACHE_ALIGN(PKT_MAXBUF_SIZE),
  163. DMA_FROM_DEVICE));
  164. CBDW_DATLEN(bdp, 0);
  165. CBDW_SC(bdp, (sc & ~BD_ENET_RX_STATS) | BD_ENET_RX_EMPTY);
  166. /*
  167. * Update BD pointer to next entry.
  168. */
  169. if ((sc & BD_ENET_RX_WRAP) == 0)
  170. bdp++;
  171. else
  172. bdp = fep->rx_bd_base;
  173. (*fep->ops->rx_bd_done)(dev);
  174. if (received >= budget)
  175. break;
  176. }
  177. fep->cur_rx = bdp;
  178. if (received < budget) {
  179. /* done */
  180. netif_rx_complete(dev, napi);
  181. (*fep->ops->napi_enable_rx)(dev);
  182. }
  183. return received;
  184. }
  185. /* non NAPI receive function */
  186. static int fs_enet_rx_non_napi(struct net_device *dev)
  187. {
  188. struct fs_enet_private *fep = netdev_priv(dev);
  189. const struct fs_platform_info *fpi = fep->fpi;
  190. cbd_t __iomem *bdp;
  191. struct sk_buff *skb, *skbn, *skbt;
  192. int received = 0;
  193. u16 pkt_len, sc;
  194. int curidx;
  195. /*
  196. * First, grab all of the stats for the incoming packet.
  197. * These get messed up if we get called due to a busy condition.
  198. */
  199. bdp = fep->cur_rx;
  200. while (((sc = CBDR_SC(bdp)) & BD_ENET_RX_EMPTY) == 0) {
  201. curidx = bdp - fep->rx_bd_base;
  202. /*
  203. * Since we have allocated space to hold a complete frame,
  204. * the last indicator should be set.
  205. */
  206. if ((sc & BD_ENET_RX_LAST) == 0)
  207. printk(KERN_WARNING DRV_MODULE_NAME
  208. ": %s rcv is not +last\n",
  209. dev->name);
  210. /*
  211. * Check for errors.
  212. */
  213. if (sc & (BD_ENET_RX_LG | BD_ENET_RX_SH | BD_ENET_RX_CL |
  214. BD_ENET_RX_NO | BD_ENET_RX_CR | BD_ENET_RX_OV)) {
  215. fep->stats.rx_errors++;
  216. /* Frame too long or too short. */
  217. if (sc & (BD_ENET_RX_LG | BD_ENET_RX_SH))
  218. fep->stats.rx_length_errors++;
  219. /* Frame alignment */
  220. if (sc & (BD_ENET_RX_NO | BD_ENET_RX_CL))
  221. fep->stats.rx_frame_errors++;
  222. /* CRC Error */
  223. if (sc & BD_ENET_RX_CR)
  224. fep->stats.rx_crc_errors++;
  225. /* FIFO overrun */
  226. if (sc & BD_ENET_RX_OV)
  227. fep->stats.rx_crc_errors++;
  228. skb = fep->rx_skbuff[curidx];
  229. dma_unmap_single(fep->dev, CBDR_BUFADDR(bdp),
  230. L1_CACHE_ALIGN(PKT_MAXBUF_SIZE),
  231. DMA_FROM_DEVICE);
  232. skbn = skb;
  233. } else {
  234. skb = fep->rx_skbuff[curidx];
  235. dma_unmap_single(fep->dev, CBDR_BUFADDR(bdp),
  236. L1_CACHE_ALIGN(PKT_MAXBUF_SIZE),
  237. DMA_FROM_DEVICE);
  238. /*
  239. * Process the incoming frame.
  240. */
  241. fep->stats.rx_packets++;
  242. pkt_len = CBDR_DATLEN(bdp) - 4; /* remove CRC */
  243. fep->stats.rx_bytes += pkt_len + 4;
  244. if (pkt_len <= fpi->rx_copybreak) {
  245. /* +2 to make IP header L1 cache aligned */
  246. skbn = dev_alloc_skb(pkt_len + 2);
  247. if (skbn != NULL) {
  248. skb_reserve(skbn, 2); /* align IP header */
  249. skb_copy_from_linear_data(skb,
  250. skbn->data, pkt_len);
  251. /* swap */
  252. skbt = skb;
  253. skb = skbn;
  254. skbn = skbt;
  255. }
  256. } else {
  257. skbn = dev_alloc_skb(ENET_RX_FRSIZE);
  258. if (skbn)
  259. skb_align(skbn, ENET_RX_ALIGN);
  260. }
  261. if (skbn != NULL) {
  262. skb_put(skb, pkt_len); /* Make room */
  263. skb->protocol = eth_type_trans(skb, dev);
  264. received++;
  265. netif_rx(skb);
  266. } else {
  267. printk(KERN_WARNING DRV_MODULE_NAME
  268. ": %s Memory squeeze, dropping packet.\n",
  269. dev->name);
  270. fep->stats.rx_dropped++;
  271. skbn = skb;
  272. }
  273. }
  274. fep->rx_skbuff[curidx] = skbn;
  275. CBDW_BUFADDR(bdp, dma_map_single(fep->dev, skbn->data,
  276. L1_CACHE_ALIGN(PKT_MAXBUF_SIZE),
  277. DMA_FROM_DEVICE));
  278. CBDW_DATLEN(bdp, 0);
  279. CBDW_SC(bdp, (sc & ~BD_ENET_RX_STATS) | BD_ENET_RX_EMPTY);
  280. /*
  281. * Update BD pointer to next entry.
  282. */
  283. if ((sc & BD_ENET_RX_WRAP) == 0)
  284. bdp++;
  285. else
  286. bdp = fep->rx_bd_base;
  287. (*fep->ops->rx_bd_done)(dev);
  288. }
  289. fep->cur_rx = bdp;
  290. return 0;
  291. }
  292. static void fs_enet_tx(struct net_device *dev)
  293. {
  294. struct fs_enet_private *fep = netdev_priv(dev);
  295. cbd_t __iomem *bdp;
  296. struct sk_buff *skb;
  297. int dirtyidx, do_wake, do_restart;
  298. u16 sc;
  299. spin_lock(&fep->tx_lock);
  300. bdp = fep->dirty_tx;
  301. do_wake = do_restart = 0;
  302. while (((sc = CBDR_SC(bdp)) & BD_ENET_TX_READY) == 0) {
  303. dirtyidx = bdp - fep->tx_bd_base;
  304. if (fep->tx_free == fep->tx_ring)
  305. break;
  306. skb = fep->tx_skbuff[dirtyidx];
  307. /*
  308. * Check for errors.
  309. */
  310. if (sc & (BD_ENET_TX_HB | BD_ENET_TX_LC |
  311. BD_ENET_TX_RL | BD_ENET_TX_UN | BD_ENET_TX_CSL)) {
  312. if (sc & BD_ENET_TX_HB) /* No heartbeat */
  313. fep->stats.tx_heartbeat_errors++;
  314. if (sc & BD_ENET_TX_LC) /* Late collision */
  315. fep->stats.tx_window_errors++;
  316. if (sc & BD_ENET_TX_RL) /* Retrans limit */
  317. fep->stats.tx_aborted_errors++;
  318. if (sc & BD_ENET_TX_UN) /* Underrun */
  319. fep->stats.tx_fifo_errors++;
  320. if (sc & BD_ENET_TX_CSL) /* Carrier lost */
  321. fep->stats.tx_carrier_errors++;
  322. if (sc & (BD_ENET_TX_LC | BD_ENET_TX_RL | BD_ENET_TX_UN)) {
  323. fep->stats.tx_errors++;
  324. do_restart = 1;
  325. }
  326. } else
  327. fep->stats.tx_packets++;
  328. if (sc & BD_ENET_TX_READY)
  329. printk(KERN_WARNING DRV_MODULE_NAME
  330. ": %s HEY! Enet xmit interrupt and TX_READY.\n",
  331. dev->name);
  332. /*
  333. * Deferred means some collisions occurred during transmit,
  334. * but we eventually sent the packet OK.
  335. */
  336. if (sc & BD_ENET_TX_DEF)
  337. fep->stats.collisions++;
  338. /* unmap */
  339. dma_unmap_single(fep->dev, CBDR_BUFADDR(bdp),
  340. skb->len, DMA_TO_DEVICE);
  341. /*
  342. * Free the sk buffer associated with this last transmit.
  343. */
  344. dev_kfree_skb_irq(skb);
  345. fep->tx_skbuff[dirtyidx] = NULL;
  346. /*
  347. * Update pointer to next buffer descriptor to be transmitted.
  348. */
  349. if ((sc & BD_ENET_TX_WRAP) == 0)
  350. bdp++;
  351. else
  352. bdp = fep->tx_bd_base;
  353. /*
  354. * Since we have freed up a buffer, the ring is no longer
  355. * full.
  356. */
  357. if (!fep->tx_free++)
  358. do_wake = 1;
  359. }
  360. fep->dirty_tx = bdp;
  361. if (do_restart)
  362. (*fep->ops->tx_restart)(dev);
  363. spin_unlock(&fep->tx_lock);
  364. if (do_wake)
  365. netif_wake_queue(dev);
  366. }
  367. /*
  368. * The interrupt handler.
  369. * This is called from the MPC core interrupt.
  370. */
  371. static irqreturn_t
  372. fs_enet_interrupt(int irq, void *dev_id)
  373. {
  374. struct net_device *dev = dev_id;
  375. struct fs_enet_private *fep;
  376. const struct fs_platform_info *fpi;
  377. u32 int_events;
  378. u32 int_clr_events;
  379. int nr, napi_ok;
  380. int handled;
  381. fep = netdev_priv(dev);
  382. fpi = fep->fpi;
  383. nr = 0;
  384. while ((int_events = (*fep->ops->get_int_events)(dev)) != 0) {
  385. nr++;
  386. int_clr_events = int_events;
  387. if (fpi->use_napi)
  388. int_clr_events &= ~fep->ev_napi_rx;
  389. (*fep->ops->clear_int_events)(dev, int_clr_events);
  390. if (int_events & fep->ev_err)
  391. (*fep->ops->ev_error)(dev, int_events);
  392. if (int_events & fep->ev_rx) {
  393. if (!fpi->use_napi)
  394. fs_enet_rx_non_napi(dev);
  395. else {
  396. napi_ok = napi_schedule_prep(&fep->napi);
  397. (*fep->ops->napi_disable_rx)(dev);
  398. (*fep->ops->clear_int_events)(dev, fep->ev_napi_rx);
  399. /* NOTE: it is possible for FCCs in NAPI mode */
  400. /* to submit a spurious interrupt while in poll */
  401. if (napi_ok)
  402. __netif_rx_schedule(dev, &fep->napi);
  403. }
  404. }
  405. if (int_events & fep->ev_tx)
  406. fs_enet_tx(dev);
  407. }
  408. handled = nr > 0;
  409. return IRQ_RETVAL(handled);
  410. }
  411. void fs_init_bds(struct net_device *dev)
  412. {
  413. struct fs_enet_private *fep = netdev_priv(dev);
  414. cbd_t __iomem *bdp;
  415. struct sk_buff *skb;
  416. int i;
  417. fs_cleanup_bds(dev);
  418. fep->dirty_tx = fep->cur_tx = fep->tx_bd_base;
  419. fep->tx_free = fep->tx_ring;
  420. fep->cur_rx = fep->rx_bd_base;
  421. /*
  422. * Initialize the receive buffer descriptors.
  423. */
  424. for (i = 0, bdp = fep->rx_bd_base; i < fep->rx_ring; i++, bdp++) {
  425. skb = dev_alloc_skb(ENET_RX_FRSIZE);
  426. if (skb == NULL) {
  427. printk(KERN_WARNING DRV_MODULE_NAME
  428. ": %s Memory squeeze, unable to allocate skb\n",
  429. dev->name);
  430. break;
  431. }
  432. skb_align(skb, ENET_RX_ALIGN);
  433. fep->rx_skbuff[i] = skb;
  434. CBDW_BUFADDR(bdp,
  435. dma_map_single(fep->dev, skb->data,
  436. L1_CACHE_ALIGN(PKT_MAXBUF_SIZE),
  437. DMA_FROM_DEVICE));
  438. CBDW_DATLEN(bdp, 0); /* zero */
  439. CBDW_SC(bdp, BD_ENET_RX_EMPTY |
  440. ((i < fep->rx_ring - 1) ? 0 : BD_SC_WRAP));
  441. }
  442. /*
  443. * if we failed, fillup remainder
  444. */
  445. for (; i < fep->rx_ring; i++, bdp++) {
  446. fep->rx_skbuff[i] = NULL;
  447. CBDW_SC(bdp, (i < fep->rx_ring - 1) ? 0 : BD_SC_WRAP);
  448. }
  449. /*
  450. * ...and the same for transmit.
  451. */
  452. for (i = 0, bdp = fep->tx_bd_base; i < fep->tx_ring; i++, bdp++) {
  453. fep->tx_skbuff[i] = NULL;
  454. CBDW_BUFADDR(bdp, 0);
  455. CBDW_DATLEN(bdp, 0);
  456. CBDW_SC(bdp, (i < fep->tx_ring - 1) ? 0 : BD_SC_WRAP);
  457. }
  458. }
  459. void fs_cleanup_bds(struct net_device *dev)
  460. {
  461. struct fs_enet_private *fep = netdev_priv(dev);
  462. struct sk_buff *skb;
  463. cbd_t __iomem *bdp;
  464. int i;
  465. /*
  466. * Reset SKB transmit buffers.
  467. */
  468. for (i = 0, bdp = fep->tx_bd_base; i < fep->tx_ring; i++, bdp++) {
  469. if ((skb = fep->tx_skbuff[i]) == NULL)
  470. continue;
  471. /* unmap */
  472. dma_unmap_single(fep->dev, CBDR_BUFADDR(bdp),
  473. skb->len, DMA_TO_DEVICE);
  474. fep->tx_skbuff[i] = NULL;
  475. dev_kfree_skb(skb);
  476. }
  477. /*
  478. * Reset SKB receive buffers
  479. */
  480. for (i = 0, bdp = fep->rx_bd_base; i < fep->rx_ring; i++, bdp++) {
  481. if ((skb = fep->rx_skbuff[i]) == NULL)
  482. continue;
  483. /* unmap */
  484. dma_unmap_single(fep->dev, CBDR_BUFADDR(bdp),
  485. L1_CACHE_ALIGN(PKT_MAXBUF_SIZE),
  486. DMA_FROM_DEVICE);
  487. fep->rx_skbuff[i] = NULL;
  488. dev_kfree_skb(skb);
  489. }
  490. }
  491. /**********************************************************************************/
  492. static int fs_enet_start_xmit(struct sk_buff *skb, struct net_device *dev)
  493. {
  494. struct fs_enet_private *fep = netdev_priv(dev);
  495. cbd_t __iomem *bdp;
  496. int curidx;
  497. u16 sc;
  498. unsigned long flags;
  499. spin_lock_irqsave(&fep->tx_lock, flags);
  500. /*
  501. * Fill in a Tx ring entry
  502. */
  503. bdp = fep->cur_tx;
  504. if (!fep->tx_free || (CBDR_SC(bdp) & BD_ENET_TX_READY)) {
  505. netif_stop_queue(dev);
  506. spin_unlock_irqrestore(&fep->tx_lock, flags);
  507. /*
  508. * Ooops. All transmit buffers are full. Bail out.
  509. * This should not happen, since the tx queue should be stopped.
  510. */
  511. printk(KERN_WARNING DRV_MODULE_NAME
  512. ": %s tx queue full!.\n", dev->name);
  513. return NETDEV_TX_BUSY;
  514. }
  515. curidx = bdp - fep->tx_bd_base;
  516. /*
  517. * Clear all of the status flags.
  518. */
  519. CBDC_SC(bdp, BD_ENET_TX_STATS);
  520. /*
  521. * Save skb pointer.
  522. */
  523. fep->tx_skbuff[curidx] = skb;
  524. fep->stats.tx_bytes += skb->len;
  525. /*
  526. * Push the data cache so the CPM does not get stale memory data.
  527. */
  528. CBDW_BUFADDR(bdp, dma_map_single(fep->dev,
  529. skb->data, skb->len, DMA_TO_DEVICE));
  530. CBDW_DATLEN(bdp, skb->len);
  531. dev->trans_start = jiffies;
  532. /*
  533. * If this was the last BD in the ring, start at the beginning again.
  534. */
  535. if ((CBDR_SC(bdp) & BD_ENET_TX_WRAP) == 0)
  536. fep->cur_tx++;
  537. else
  538. fep->cur_tx = fep->tx_bd_base;
  539. if (!--fep->tx_free)
  540. netif_stop_queue(dev);
  541. /* Trigger transmission start */
  542. sc = BD_ENET_TX_READY | BD_ENET_TX_INTR |
  543. BD_ENET_TX_LAST | BD_ENET_TX_TC;
  544. /* note that while FEC does not have this bit
  545. * it marks it as available for software use
  546. * yay for hw reuse :) */
  547. if (skb->len <= 60)
  548. sc |= BD_ENET_TX_PAD;
  549. CBDS_SC(bdp, sc);
  550. (*fep->ops->tx_kickstart)(dev);
  551. spin_unlock_irqrestore(&fep->tx_lock, flags);
  552. return NETDEV_TX_OK;
  553. }
  554. static int fs_request_irq(struct net_device *dev, int irq, const char *name,
  555. irq_handler_t irqf)
  556. {
  557. struct fs_enet_private *fep = netdev_priv(dev);
  558. (*fep->ops->pre_request_irq)(dev, irq);
  559. return request_irq(irq, irqf, IRQF_SHARED, name, dev);
  560. }
  561. static void fs_free_irq(struct net_device *dev, int irq)
  562. {
  563. struct fs_enet_private *fep = netdev_priv(dev);
  564. free_irq(irq, dev);
  565. (*fep->ops->post_free_irq)(dev, irq);
  566. }
  567. static void fs_timeout(struct net_device *dev)
  568. {
  569. struct fs_enet_private *fep = netdev_priv(dev);
  570. unsigned long flags;
  571. int wake = 0;
  572. fep->stats.tx_errors++;
  573. spin_lock_irqsave(&fep->lock, flags);
  574. if (dev->flags & IFF_UP) {
  575. phy_stop(fep->phydev);
  576. (*fep->ops->stop)(dev);
  577. (*fep->ops->restart)(dev);
  578. phy_start(fep->phydev);
  579. }
  580. phy_start(fep->phydev);
  581. wake = fep->tx_free && !(CBDR_SC(fep->cur_tx) & BD_ENET_TX_READY);
  582. spin_unlock_irqrestore(&fep->lock, flags);
  583. if (wake)
  584. netif_wake_queue(dev);
  585. }
  586. /*-----------------------------------------------------------------------------
  587. * generic link-change handler - should be sufficient for most cases
  588. *-----------------------------------------------------------------------------*/
  589. static void generic_adjust_link(struct net_device *dev)
  590. {
  591. struct fs_enet_private *fep = netdev_priv(dev);
  592. struct phy_device *phydev = fep->phydev;
  593. int new_state = 0;
  594. if (phydev->link) {
  595. /* adjust to duplex mode */
  596. if (phydev->duplex != fep->oldduplex) {
  597. new_state = 1;
  598. fep->oldduplex = phydev->duplex;
  599. }
  600. if (phydev->speed != fep->oldspeed) {
  601. new_state = 1;
  602. fep->oldspeed = phydev->speed;
  603. }
  604. if (!fep->oldlink) {
  605. new_state = 1;
  606. fep->oldlink = 1;
  607. }
  608. if (new_state)
  609. fep->ops->restart(dev);
  610. } else if (fep->oldlink) {
  611. new_state = 1;
  612. fep->oldlink = 0;
  613. fep->oldspeed = 0;
  614. fep->oldduplex = -1;
  615. }
  616. if (new_state && netif_msg_link(fep))
  617. phy_print_status(phydev);
  618. }
  619. static void fs_adjust_link(struct net_device *dev)
  620. {
  621. struct fs_enet_private *fep = netdev_priv(dev);
  622. unsigned long flags;
  623. spin_lock_irqsave(&fep->lock, flags);
  624. if(fep->ops->adjust_link)
  625. fep->ops->adjust_link(dev);
  626. else
  627. generic_adjust_link(dev);
  628. spin_unlock_irqrestore(&fep->lock, flags);
  629. }
  630. static int fs_init_phy(struct net_device *dev)
  631. {
  632. struct fs_enet_private *fep = netdev_priv(dev);
  633. struct phy_device *phydev;
  634. fep->oldlink = 0;
  635. fep->oldspeed = 0;
  636. fep->oldduplex = -1;
  637. if(fep->fpi->bus_id)
  638. phydev = phy_connect(dev, fep->fpi->bus_id, &fs_adjust_link, 0,
  639. PHY_INTERFACE_MODE_MII);
  640. else {
  641. printk("No phy bus ID specified in BSP code\n");
  642. return -EINVAL;
  643. }
  644. if (IS_ERR(phydev)) {
  645. printk(KERN_ERR "%s: Could not attach to PHY\n", dev->name);
  646. return PTR_ERR(phydev);
  647. }
  648. fep->phydev = phydev;
  649. return 0;
  650. }
  651. static int fs_enet_open(struct net_device *dev)
  652. {
  653. struct fs_enet_private *fep = netdev_priv(dev);
  654. int r;
  655. int err;
  656. /* to initialize the fep->cur_rx,... */
  657. /* not doing this, will cause a crash in fs_enet_rx_napi */
  658. fs_init_bds(fep->ndev);
  659. if (fep->fpi->use_napi)
  660. napi_enable(&fep->napi);
  661. /* Install our interrupt handler. */
  662. r = fs_request_irq(dev, fep->interrupt, "fs_enet-mac", fs_enet_interrupt);
  663. if (r != 0) {
  664. printk(KERN_ERR DRV_MODULE_NAME
  665. ": %s Could not allocate FS_ENET IRQ!", dev->name);
  666. if (fep->fpi->use_napi)
  667. napi_disable(&fep->napi);
  668. return -EINVAL;
  669. }
  670. err = fs_init_phy(dev);
  671. if (err) {
  672. if (fep->fpi->use_napi)
  673. napi_disable(&fep->napi);
  674. return err;
  675. }
  676. phy_start(fep->phydev);
  677. netif_start_queue(dev);
  678. return 0;
  679. }
  680. static int fs_enet_close(struct net_device *dev)
  681. {
  682. struct fs_enet_private *fep = netdev_priv(dev);
  683. unsigned long flags;
  684. netif_stop_queue(dev);
  685. netif_carrier_off(dev);
  686. if (fep->fpi->use_napi)
  687. napi_disable(&fep->napi);
  688. phy_stop(fep->phydev);
  689. spin_lock_irqsave(&fep->lock, flags);
  690. spin_lock(&fep->tx_lock);
  691. (*fep->ops->stop)(dev);
  692. spin_unlock(&fep->tx_lock);
  693. spin_unlock_irqrestore(&fep->lock, flags);
  694. /* release any irqs */
  695. phy_disconnect(fep->phydev);
  696. fep->phydev = NULL;
  697. fs_free_irq(dev, fep->interrupt);
  698. return 0;
  699. }
  700. static struct net_device_stats *fs_enet_get_stats(struct net_device *dev)
  701. {
  702. struct fs_enet_private *fep = netdev_priv(dev);
  703. return &fep->stats;
  704. }
  705. /*************************************************************************/
  706. static void fs_get_drvinfo(struct net_device *dev,
  707. struct ethtool_drvinfo *info)
  708. {
  709. strcpy(info->driver, DRV_MODULE_NAME);
  710. strcpy(info->version, DRV_MODULE_VERSION);
  711. }
  712. static int fs_get_regs_len(struct net_device *dev)
  713. {
  714. struct fs_enet_private *fep = netdev_priv(dev);
  715. return (*fep->ops->get_regs_len)(dev);
  716. }
  717. static void fs_get_regs(struct net_device *dev, struct ethtool_regs *regs,
  718. void *p)
  719. {
  720. struct fs_enet_private *fep = netdev_priv(dev);
  721. unsigned long flags;
  722. int r, len;
  723. len = regs->len;
  724. spin_lock_irqsave(&fep->lock, flags);
  725. r = (*fep->ops->get_regs)(dev, p, &len);
  726. spin_unlock_irqrestore(&fep->lock, flags);
  727. if (r == 0)
  728. regs->version = 0;
  729. }
  730. static int fs_get_settings(struct net_device *dev, struct ethtool_cmd *cmd)
  731. {
  732. struct fs_enet_private *fep = netdev_priv(dev);
  733. if (!fep->phydev)
  734. return -ENODEV;
  735. return phy_ethtool_gset(fep->phydev, cmd);
  736. }
  737. static int fs_set_settings(struct net_device *dev, struct ethtool_cmd *cmd)
  738. {
  739. struct fs_enet_private *fep = netdev_priv(dev);
  740. if (!fep->phydev)
  741. return -ENODEV;
  742. return phy_ethtool_sset(fep->phydev, cmd);
  743. }
  744. static int fs_nway_reset(struct net_device *dev)
  745. {
  746. return 0;
  747. }
  748. static u32 fs_get_msglevel(struct net_device *dev)
  749. {
  750. struct fs_enet_private *fep = netdev_priv(dev);
  751. return fep->msg_enable;
  752. }
  753. static void fs_set_msglevel(struct net_device *dev, u32 value)
  754. {
  755. struct fs_enet_private *fep = netdev_priv(dev);
  756. fep->msg_enable = value;
  757. }
  758. static const struct ethtool_ops fs_ethtool_ops = {
  759. .get_drvinfo = fs_get_drvinfo,
  760. .get_regs_len = fs_get_regs_len,
  761. .get_settings = fs_get_settings,
  762. .set_settings = fs_set_settings,
  763. .nway_reset = fs_nway_reset,
  764. .get_link = ethtool_op_get_link,
  765. .get_msglevel = fs_get_msglevel,
  766. .set_msglevel = fs_set_msglevel,
  767. .set_tx_csum = ethtool_op_set_tx_csum, /* local! */
  768. .set_sg = ethtool_op_set_sg,
  769. .get_regs = fs_get_regs,
  770. };
  771. static int fs_ioctl(struct net_device *dev, struct ifreq *rq, int cmd)
  772. {
  773. struct fs_enet_private *fep = netdev_priv(dev);
  774. struct mii_ioctl_data *mii = (struct mii_ioctl_data *)&rq->ifr_data;
  775. if (!netif_running(dev))
  776. return -EINVAL;
  777. return phy_mii_ioctl(fep->phydev, mii, cmd);
  778. }
  779. extern int fs_mii_connect(struct net_device *dev);
  780. extern void fs_mii_disconnect(struct net_device *dev);
  781. /**************************************************************************************/
  782. /* handy pointer to the immap */
  783. void __iomem *fs_enet_immap = NULL;
  784. static int setup_immap(void)
  785. {
  786. #ifdef CONFIG_CPM1
  787. fs_enet_immap = ioremap(IMAP_ADDR, 0x4000);
  788. WARN_ON(!fs_enet_immap);
  789. #elif defined(CONFIG_CPM2)
  790. fs_enet_immap = cpm2_immr;
  791. #endif
  792. return 0;
  793. }
  794. static void cleanup_immap(void)
  795. {
  796. #if defined(CONFIG_CPM1)
  797. iounmap(fs_enet_immap);
  798. #endif
  799. }
  800. /**************************************************************************************/
  801. static int __devinit find_phy(struct device_node *np,
  802. struct fs_platform_info *fpi)
  803. {
  804. struct device_node *phynode, *mdionode;
  805. int ret = 0, len, bus_id;
  806. const u32 *data;
  807. data = of_get_property(np, "fixed-link", NULL);
  808. if (data) {
  809. snprintf(fpi->bus_id, 16, "%x:%02x", 0, *data);
  810. return 0;
  811. }
  812. data = of_get_property(np, "phy-handle", &len);
  813. if (!data || len != 4)
  814. return -EINVAL;
  815. phynode = of_find_node_by_phandle(*data);
  816. if (!phynode)
  817. return -EINVAL;
  818. data = of_get_property(phynode, "reg", &len);
  819. if (!data || len != 4) {
  820. ret = -EINVAL;
  821. goto out_put_phy;
  822. }
  823. mdionode = of_get_parent(phynode);
  824. if (!mdionode) {
  825. ret = -EINVAL;
  826. goto out_put_phy;
  827. }
  828. bus_id = of_get_gpio(mdionode, 0);
  829. if (bus_id < 0) {
  830. struct resource res;
  831. ret = of_address_to_resource(mdionode, 0, &res);
  832. if (ret)
  833. goto out_put_mdio;
  834. bus_id = res.start;
  835. }
  836. snprintf(fpi->bus_id, 16, "%x:%02x", bus_id, *data);
  837. out_put_mdio:
  838. of_node_put(mdionode);
  839. out_put_phy:
  840. of_node_put(phynode);
  841. return ret;
  842. }
  843. #ifdef CONFIG_FS_ENET_HAS_FEC
  844. #define IS_FEC(match) ((match)->data == &fs_fec_ops)
  845. #else
  846. #define IS_FEC(match) 0
  847. #endif
  848. static int __devinit fs_enet_probe(struct of_device *ofdev,
  849. const struct of_device_id *match)
  850. {
  851. struct net_device *ndev;
  852. struct fs_enet_private *fep;
  853. struct fs_platform_info *fpi;
  854. const u32 *data;
  855. const u8 *mac_addr;
  856. int privsize, len, ret = -ENODEV;
  857. fpi = kzalloc(sizeof(*fpi), GFP_KERNEL);
  858. if (!fpi)
  859. return -ENOMEM;
  860. if (!IS_FEC(match)) {
  861. data = of_get_property(ofdev->node, "fsl,cpm-command", &len);
  862. if (!data || len != 4)
  863. goto out_free_fpi;
  864. fpi->cp_command = *data;
  865. }
  866. fpi->rx_ring = 32;
  867. fpi->tx_ring = 32;
  868. fpi->rx_copybreak = 240;
  869. fpi->use_napi = 1;
  870. fpi->napi_weight = 17;
  871. ret = find_phy(ofdev->node, fpi);
  872. if (ret)
  873. goto out_free_fpi;
  874. privsize = sizeof(*fep) +
  875. sizeof(struct sk_buff **) *
  876. (fpi->rx_ring + fpi->tx_ring);
  877. ndev = alloc_etherdev(privsize);
  878. if (!ndev) {
  879. ret = -ENOMEM;
  880. goto out_free_fpi;
  881. }
  882. dev_set_drvdata(&ofdev->dev, ndev);
  883. fep = netdev_priv(ndev);
  884. fep->dev = &ofdev->dev;
  885. fep->ndev = ndev;
  886. fep->fpi = fpi;
  887. fep->ops = match->data;
  888. ret = fep->ops->setup_data(ndev);
  889. if (ret)
  890. goto out_free_dev;
  891. fep->rx_skbuff = (struct sk_buff **)&fep[1];
  892. fep->tx_skbuff = fep->rx_skbuff + fpi->rx_ring;
  893. spin_lock_init(&fep->lock);
  894. spin_lock_init(&fep->tx_lock);
  895. mac_addr = of_get_mac_address(ofdev->node);
  896. if (mac_addr)
  897. memcpy(ndev->dev_addr, mac_addr, 6);
  898. ret = fep->ops->allocate_bd(ndev);
  899. if (ret)
  900. goto out_cleanup_data;
  901. fep->rx_bd_base = fep->ring_base;
  902. fep->tx_bd_base = fep->rx_bd_base + fpi->rx_ring;
  903. fep->tx_ring = fpi->tx_ring;
  904. fep->rx_ring = fpi->rx_ring;
  905. ndev->open = fs_enet_open;
  906. ndev->hard_start_xmit = fs_enet_start_xmit;
  907. ndev->tx_timeout = fs_timeout;
  908. ndev->watchdog_timeo = 2 * HZ;
  909. ndev->stop = fs_enet_close;
  910. ndev->get_stats = fs_enet_get_stats;
  911. ndev->set_multicast_list = fs_set_multicast_list;
  912. if (fpi->use_napi)
  913. netif_napi_add(ndev, &fep->napi, fs_enet_rx_napi,
  914. fpi->napi_weight);
  915. ndev->ethtool_ops = &fs_ethtool_ops;
  916. ndev->do_ioctl = fs_ioctl;
  917. init_timer(&fep->phy_timer_list);
  918. netif_carrier_off(ndev);
  919. ret = register_netdev(ndev);
  920. if (ret)
  921. goto out_free_bd;
  922. printk(KERN_INFO "%s: fs_enet: %02x:%02x:%02x:%02x:%02x:%02x\n",
  923. ndev->name,
  924. ndev->dev_addr[0], ndev->dev_addr[1], ndev->dev_addr[2],
  925. ndev->dev_addr[3], ndev->dev_addr[4], ndev->dev_addr[5]);
  926. return 0;
  927. out_free_bd:
  928. fep->ops->free_bd(ndev);
  929. out_cleanup_data:
  930. fep->ops->cleanup_data(ndev);
  931. out_free_dev:
  932. free_netdev(ndev);
  933. dev_set_drvdata(&ofdev->dev, NULL);
  934. out_free_fpi:
  935. kfree(fpi);
  936. return ret;
  937. }
  938. static int fs_enet_remove(struct of_device *ofdev)
  939. {
  940. struct net_device *ndev = dev_get_drvdata(&ofdev->dev);
  941. struct fs_enet_private *fep = netdev_priv(ndev);
  942. unregister_netdev(ndev);
  943. fep->ops->free_bd(ndev);
  944. fep->ops->cleanup_data(ndev);
  945. dev_set_drvdata(fep->dev, NULL);
  946. free_netdev(ndev);
  947. return 0;
  948. }
  949. static struct of_device_id fs_enet_match[] = {
  950. #ifdef CONFIG_FS_ENET_HAS_SCC
  951. {
  952. .compatible = "fsl,cpm1-scc-enet",
  953. .data = (void *)&fs_scc_ops,
  954. },
  955. {
  956. .compatible = "fsl,cpm2-scc-enet",
  957. .data = (void *)&fs_scc_ops,
  958. },
  959. #endif
  960. #ifdef CONFIG_FS_ENET_HAS_FCC
  961. {
  962. .compatible = "fsl,cpm2-fcc-enet",
  963. .data = (void *)&fs_fcc_ops,
  964. },
  965. #endif
  966. #ifdef CONFIG_FS_ENET_HAS_FEC
  967. {
  968. .compatible = "fsl,pq1-fec-enet",
  969. .data = (void *)&fs_fec_ops,
  970. },
  971. #endif
  972. {}
  973. };
  974. static struct of_platform_driver fs_enet_driver = {
  975. .name = "fs_enet",
  976. .match_table = fs_enet_match,
  977. .probe = fs_enet_probe,
  978. .remove = fs_enet_remove,
  979. };
  980. static int __init fs_init(void)
  981. {
  982. int r = setup_immap();
  983. if (r != 0)
  984. return r;
  985. r = of_register_platform_driver(&fs_enet_driver);
  986. if (r != 0)
  987. goto out;
  988. return 0;
  989. out:
  990. cleanup_immap();
  991. return r;
  992. }
  993. static void __exit fs_cleanup(void)
  994. {
  995. of_unregister_platform_driver(&fs_enet_driver);
  996. cleanup_immap();
  997. }
  998. #ifdef CONFIG_NET_POLL_CONTROLLER
  999. static void fs_enet_netpoll(struct net_device *dev)
  1000. {
  1001. disable_irq(dev->irq);
  1002. fs_enet_interrupt(dev->irq, dev, NULL);
  1003. enable_irq(dev->irq);
  1004. }
  1005. #endif
  1006. /**************************************************************************************/
  1007. module_init(fs_init);
  1008. module_exit(fs_cleanup);