fs_enet-main.c 26 KB

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