fs_enet-main.c 27 KB

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