fs_enet-main.c 27 KB

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