fs_enet-main.c 26 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133
  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 <linux/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. dev->stats.tx_heartbeat_errors++;
  102. if (sc & BD_ENET_TX_LC) /* Late collision */
  103. dev->stats.tx_window_errors++;
  104. if (sc & BD_ENET_TX_RL) /* Retrans limit */
  105. dev->stats.tx_aborted_errors++;
  106. if (sc & BD_ENET_TX_UN) /* Underrun */
  107. dev->stats.tx_fifo_errors++;
  108. if (sc & BD_ENET_TX_CSL) /* Carrier lost */
  109. dev->stats.tx_carrier_errors++;
  110. if (sc & (BD_ENET_TX_LC | BD_ENET_TX_RL | BD_ENET_TX_UN)) {
  111. dev->stats.tx_errors++;
  112. do_restart = 1;
  113. }
  114. } else
  115. dev->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. dev->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. dev->stats.rx_errors++;
  181. /* Frame too long or too short. */
  182. if (sc & (BD_ENET_RX_LG | BD_ENET_RX_SH))
  183. dev->stats.rx_length_errors++;
  184. /* Frame alignment */
  185. if (sc & (BD_ENET_RX_NO | BD_ENET_RX_CL))
  186. dev->stats.rx_frame_errors++;
  187. /* CRC Error */
  188. if (sc & BD_ENET_RX_CR)
  189. dev->stats.rx_crc_errors++;
  190. /* FIFO overrun */
  191. if (sc & BD_ENET_RX_OV)
  192. dev->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. dev->stats.rx_packets++;
  200. pkt_len = CBDR_DATLEN(bdp) - 4; /* remove CRC */
  201. dev->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. dev->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_done(napi, received);
  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. dev->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. dev->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. /*************************************************************************/
  637. static void fs_get_drvinfo(struct net_device *dev,
  638. struct ethtool_drvinfo *info)
  639. {
  640. strlcpy(info->driver, DRV_MODULE_NAME, sizeof(info->driver));
  641. strlcpy(info->version, DRV_MODULE_VERSION, sizeof(info->version));
  642. }
  643. static int fs_get_regs_len(struct net_device *dev)
  644. {
  645. struct fs_enet_private *fep = netdev_priv(dev);
  646. return (*fep->ops->get_regs_len)(dev);
  647. }
  648. static void fs_get_regs(struct net_device *dev, struct ethtool_regs *regs,
  649. void *p)
  650. {
  651. struct fs_enet_private *fep = netdev_priv(dev);
  652. unsigned long flags;
  653. int r, len;
  654. len = regs->len;
  655. spin_lock_irqsave(&fep->lock, flags);
  656. r = (*fep->ops->get_regs)(dev, p, &len);
  657. spin_unlock_irqrestore(&fep->lock, flags);
  658. if (r == 0)
  659. regs->version = 0;
  660. }
  661. static u32 fs_get_msglevel(struct net_device *dev)
  662. {
  663. struct fs_enet_private *fep = netdev_priv(dev);
  664. return fep->msg_enable;
  665. }
  666. static void fs_set_msglevel(struct net_device *dev, u32 value)
  667. {
  668. struct fs_enet_private *fep = netdev_priv(dev);
  669. fep->msg_enable = value;
  670. }
  671. static int fs_get_tunable(struct net_device *dev,
  672. const struct ethtool_tunable *tuna, void *data)
  673. {
  674. struct fs_enet_private *fep = netdev_priv(dev);
  675. struct fs_platform_info *fpi = fep->fpi;
  676. int ret = 0;
  677. switch (tuna->id) {
  678. case ETHTOOL_RX_COPYBREAK:
  679. *(u32 *)data = fpi->rx_copybreak;
  680. break;
  681. default:
  682. ret = -EINVAL;
  683. break;
  684. }
  685. return ret;
  686. }
  687. static int fs_set_tunable(struct net_device *dev,
  688. const struct ethtool_tunable *tuna, const void *data)
  689. {
  690. struct fs_enet_private *fep = netdev_priv(dev);
  691. struct fs_platform_info *fpi = fep->fpi;
  692. int ret = 0;
  693. switch (tuna->id) {
  694. case ETHTOOL_RX_COPYBREAK:
  695. fpi->rx_copybreak = *(u32 *)data;
  696. break;
  697. default:
  698. ret = -EINVAL;
  699. break;
  700. }
  701. return ret;
  702. }
  703. static const struct ethtool_ops fs_ethtool_ops = {
  704. .get_drvinfo = fs_get_drvinfo,
  705. .get_regs_len = fs_get_regs_len,
  706. .nway_reset = phy_ethtool_nway_reset,
  707. .get_link = ethtool_op_get_link,
  708. .get_msglevel = fs_get_msglevel,
  709. .set_msglevel = fs_set_msglevel,
  710. .get_regs = fs_get_regs,
  711. .get_ts_info = ethtool_op_get_ts_info,
  712. .get_link_ksettings = phy_ethtool_get_link_ksettings,
  713. .set_link_ksettings = phy_ethtool_set_link_ksettings,
  714. .get_tunable = fs_get_tunable,
  715. .set_tunable = fs_set_tunable,
  716. };
  717. static int fs_ioctl(struct net_device *dev, struct ifreq *rq, int cmd)
  718. {
  719. if (!netif_running(dev))
  720. return -EINVAL;
  721. return phy_mii_ioctl(dev->phydev, rq, cmd);
  722. }
  723. extern int fs_mii_connect(struct net_device *dev);
  724. extern void fs_mii_disconnect(struct net_device *dev);
  725. /**************************************************************************************/
  726. #ifdef CONFIG_FS_ENET_HAS_FEC
  727. #define IS_FEC(match) ((match)->data == &fs_fec_ops)
  728. #else
  729. #define IS_FEC(match) 0
  730. #endif
  731. static const struct net_device_ops fs_enet_netdev_ops = {
  732. .ndo_open = fs_enet_open,
  733. .ndo_stop = fs_enet_close,
  734. .ndo_start_xmit = fs_enet_start_xmit,
  735. .ndo_tx_timeout = fs_timeout,
  736. .ndo_set_rx_mode = fs_set_multicast_list,
  737. .ndo_do_ioctl = fs_ioctl,
  738. .ndo_validate_addr = eth_validate_addr,
  739. .ndo_set_mac_address = eth_mac_addr,
  740. #ifdef CONFIG_NET_POLL_CONTROLLER
  741. .ndo_poll_controller = fs_enet_netpoll,
  742. #endif
  743. };
  744. static const struct of_device_id fs_enet_match[];
  745. static int fs_enet_probe(struct platform_device *ofdev)
  746. {
  747. const struct of_device_id *match;
  748. struct net_device *ndev;
  749. struct fs_enet_private *fep;
  750. struct fs_platform_info *fpi;
  751. const u32 *data;
  752. struct clk *clk;
  753. int err;
  754. const u8 *mac_addr;
  755. const char *phy_connection_type;
  756. int privsize, len, ret = -ENODEV;
  757. match = of_match_device(fs_enet_match, &ofdev->dev);
  758. if (!match)
  759. return -EINVAL;
  760. fpi = kzalloc(sizeof(*fpi), GFP_KERNEL);
  761. if (!fpi)
  762. return -ENOMEM;
  763. if (!IS_FEC(match)) {
  764. data = of_get_property(ofdev->dev.of_node, "fsl,cpm-command", &len);
  765. if (!data || len != 4)
  766. goto out_free_fpi;
  767. fpi->cp_command = *data;
  768. }
  769. fpi->rx_ring = RX_RING_SIZE;
  770. fpi->tx_ring = TX_RING_SIZE;
  771. fpi->rx_copybreak = 240;
  772. fpi->napi_weight = 17;
  773. fpi->phy_node = of_parse_phandle(ofdev->dev.of_node, "phy-handle", 0);
  774. if (!fpi->phy_node && of_phy_is_fixed_link(ofdev->dev.of_node)) {
  775. err = of_phy_register_fixed_link(ofdev->dev.of_node);
  776. if (err)
  777. goto out_free_fpi;
  778. /* In the case of a fixed PHY, the DT node associated
  779. * to the PHY is the Ethernet MAC DT node.
  780. */
  781. fpi->phy_node = of_node_get(ofdev->dev.of_node);
  782. }
  783. if (of_device_is_compatible(ofdev->dev.of_node, "fsl,mpc5125-fec")) {
  784. phy_connection_type = of_get_property(ofdev->dev.of_node,
  785. "phy-connection-type", NULL);
  786. if (phy_connection_type && !strcmp("rmii", phy_connection_type))
  787. fpi->use_rmii = 1;
  788. }
  789. /* make clock lookup non-fatal (the driver is shared among platforms),
  790. * but require enable to succeed when a clock was specified/found,
  791. * keep a reference to the clock upon successful acquisition
  792. */
  793. clk = devm_clk_get(&ofdev->dev, "per");
  794. if (!IS_ERR(clk)) {
  795. ret = clk_prepare_enable(clk);
  796. if (ret)
  797. goto out_deregister_fixed_link;
  798. fpi->clk_per = clk;
  799. }
  800. privsize = sizeof(*fep) +
  801. sizeof(struct sk_buff **) *
  802. (fpi->rx_ring + fpi->tx_ring) +
  803. sizeof(char) * fpi->tx_ring;
  804. ndev = alloc_etherdev(privsize);
  805. if (!ndev) {
  806. ret = -ENOMEM;
  807. goto out_put;
  808. }
  809. SET_NETDEV_DEV(ndev, &ofdev->dev);
  810. platform_set_drvdata(ofdev, ndev);
  811. fep = netdev_priv(ndev);
  812. fep->dev = &ofdev->dev;
  813. fep->ndev = ndev;
  814. fep->fpi = fpi;
  815. fep->ops = match->data;
  816. ret = fep->ops->setup_data(ndev);
  817. if (ret)
  818. goto out_free_dev;
  819. fep->rx_skbuff = (struct sk_buff **)&fep[1];
  820. fep->tx_skbuff = fep->rx_skbuff + fpi->rx_ring;
  821. fep->mapped_as_page = (char *)(fep->rx_skbuff + fpi->rx_ring +
  822. fpi->tx_ring);
  823. spin_lock_init(&fep->lock);
  824. spin_lock_init(&fep->tx_lock);
  825. mac_addr = of_get_mac_address(ofdev->dev.of_node);
  826. if (mac_addr)
  827. memcpy(ndev->dev_addr, mac_addr, ETH_ALEN);
  828. ret = fep->ops->allocate_bd(ndev);
  829. if (ret)
  830. goto out_cleanup_data;
  831. fep->rx_bd_base = fep->ring_base;
  832. fep->tx_bd_base = fep->rx_bd_base + fpi->rx_ring;
  833. fep->tx_ring = fpi->tx_ring;
  834. fep->rx_ring = fpi->rx_ring;
  835. ndev->netdev_ops = &fs_enet_netdev_ops;
  836. ndev->watchdog_timeo = 2 * HZ;
  837. netif_napi_add(ndev, &fep->napi, fs_enet_napi, fpi->napi_weight);
  838. ndev->ethtool_ops = &fs_ethtool_ops;
  839. init_timer(&fep->phy_timer_list);
  840. netif_carrier_off(ndev);
  841. ndev->features |= NETIF_F_SG;
  842. ret = register_netdev(ndev);
  843. if (ret)
  844. goto out_free_bd;
  845. pr_info("%s: fs_enet: %pM\n", ndev->name, ndev->dev_addr);
  846. return 0;
  847. out_free_bd:
  848. fep->ops->free_bd(ndev);
  849. out_cleanup_data:
  850. fep->ops->cleanup_data(ndev);
  851. out_free_dev:
  852. free_netdev(ndev);
  853. out_put:
  854. if (fpi->clk_per)
  855. clk_disable_unprepare(fpi->clk_per);
  856. out_deregister_fixed_link:
  857. of_node_put(fpi->phy_node);
  858. if (of_phy_is_fixed_link(ofdev->dev.of_node))
  859. of_phy_deregister_fixed_link(ofdev->dev.of_node);
  860. out_free_fpi:
  861. kfree(fpi);
  862. return ret;
  863. }
  864. static int fs_enet_remove(struct platform_device *ofdev)
  865. {
  866. struct net_device *ndev = platform_get_drvdata(ofdev);
  867. struct fs_enet_private *fep = netdev_priv(ndev);
  868. unregister_netdev(ndev);
  869. fep->ops->free_bd(ndev);
  870. fep->ops->cleanup_data(ndev);
  871. dev_set_drvdata(fep->dev, NULL);
  872. of_node_put(fep->fpi->phy_node);
  873. if (fep->fpi->clk_per)
  874. clk_disable_unprepare(fep->fpi->clk_per);
  875. if (of_phy_is_fixed_link(ofdev->dev.of_node))
  876. of_phy_deregister_fixed_link(ofdev->dev.of_node);
  877. free_netdev(ndev);
  878. return 0;
  879. }
  880. static const struct of_device_id fs_enet_match[] = {
  881. #ifdef CONFIG_FS_ENET_HAS_SCC
  882. {
  883. .compatible = "fsl,cpm1-scc-enet",
  884. .data = (void *)&fs_scc_ops,
  885. },
  886. {
  887. .compatible = "fsl,cpm2-scc-enet",
  888. .data = (void *)&fs_scc_ops,
  889. },
  890. #endif
  891. #ifdef CONFIG_FS_ENET_HAS_FCC
  892. {
  893. .compatible = "fsl,cpm2-fcc-enet",
  894. .data = (void *)&fs_fcc_ops,
  895. },
  896. #endif
  897. #ifdef CONFIG_FS_ENET_HAS_FEC
  898. #ifdef CONFIG_FS_ENET_MPC5121_FEC
  899. {
  900. .compatible = "fsl,mpc5121-fec",
  901. .data = (void *)&fs_fec_ops,
  902. },
  903. {
  904. .compatible = "fsl,mpc5125-fec",
  905. .data = (void *)&fs_fec_ops,
  906. },
  907. #else
  908. {
  909. .compatible = "fsl,pq1-fec-enet",
  910. .data = (void *)&fs_fec_ops,
  911. },
  912. #endif
  913. #endif
  914. {}
  915. };
  916. MODULE_DEVICE_TABLE(of, fs_enet_match);
  917. static struct platform_driver fs_enet_driver = {
  918. .driver = {
  919. .name = "fs_enet",
  920. .of_match_table = fs_enet_match,
  921. },
  922. .probe = fs_enet_probe,
  923. .remove = fs_enet_remove,
  924. };
  925. #ifdef CONFIG_NET_POLL_CONTROLLER
  926. static void fs_enet_netpoll(struct net_device *dev)
  927. {
  928. disable_irq(dev->irq);
  929. fs_enet_interrupt(dev->irq, dev);
  930. enable_irq(dev->irq);
  931. }
  932. #endif
  933. module_platform_driver(fs_enet_driver);