fec_main.c 29 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264
  1. /*
  2. * Fast Ethernet Controller (FEC) driver for Motorola MPC8xx.
  3. *
  4. * Copyright (c) 2003 Intracom S.A.
  5. * by Pantelis Antoniou <panto@intracom.gr>
  6. *
  7. * Heavily based on original FEC driver by Dan Malek <dan@embeddededge.com>
  8. * and modifications by Joakim Tjernlund <joakim.tjernlund@lumentis.se>
  9. *
  10. * Released under the GPL
  11. */
  12. #include <linux/module.h>
  13. #include <linux/kernel.h>
  14. #include <linux/types.h>
  15. #include <linux/string.h>
  16. #include <linux/ptrace.h>
  17. #include <linux/errno.h>
  18. #include <linux/ioport.h>
  19. #include <linux/slab.h>
  20. #include <linux/interrupt.h>
  21. #include <linux/init.h>
  22. #include <linux/delay.h>
  23. #include <linux/netdevice.h>
  24. #include <linux/etherdevice.h>
  25. #include <linux/skbuff.h>
  26. #include <linux/spinlock.h>
  27. #include <linux/mii.h>
  28. #include <linux/ethtool.h>
  29. #include <linux/bitops.h>
  30. #include <linux/dma-mapping.h>
  31. #include <asm/8xx_immap.h>
  32. #include <asm/pgtable.h>
  33. #include <asm/mpc8xx.h>
  34. #include <asm/irq.h>
  35. #include <asm/uaccess.h>
  36. #include <asm/cpm1.h>
  37. #include "fec_8xx.h"
  38. /*************************************************/
  39. #define FEC_MAX_MULTICAST_ADDRS 64
  40. /*************************************************/
  41. static char version[] __devinitdata =
  42. DRV_MODULE_NAME ".c:v" DRV_MODULE_VERSION " (" DRV_MODULE_RELDATE ")" "\n";
  43. MODULE_AUTHOR("Pantelis Antoniou <panto@intracom.gr>");
  44. MODULE_DESCRIPTION("Motorola 8xx FEC ethernet driver");
  45. MODULE_LICENSE("GPL");
  46. int fec_8xx_debug = -1; /* -1 == use FEC_8XX_DEF_MSG_ENABLE as value */
  47. module_param(fec_8xx_debug, int, 0);
  48. MODULE_PARM_DESC(fec_8xx_debug,
  49. "FEC 8xx bitmapped debugging message enable value");
  50. /*************************************************/
  51. /*
  52. * Delay to wait for FEC reset command to complete (in us)
  53. */
  54. #define FEC_RESET_DELAY 50
  55. /*****************************************************************************************/
  56. static void fec_whack_reset(fec_t * fecp)
  57. {
  58. int i;
  59. /*
  60. * Whack a reset. We should wait for this.
  61. */
  62. FW(fecp, ecntrl, FEC_ECNTRL_PINMUX | FEC_ECNTRL_RESET);
  63. for (i = 0;
  64. (FR(fecp, ecntrl) & FEC_ECNTRL_RESET) != 0 && i < FEC_RESET_DELAY;
  65. i++)
  66. udelay(1);
  67. if (i == FEC_RESET_DELAY)
  68. printk(KERN_WARNING "FEC Reset timeout!\n");
  69. }
  70. /****************************************************************************/
  71. /*
  72. * Transmitter timeout.
  73. */
  74. #define TX_TIMEOUT (2*HZ)
  75. /****************************************************************************/
  76. /*
  77. * Returns the CRC needed when filling in the hash table for
  78. * multicast group filtering
  79. * pAddr must point to a MAC address (6 bytes)
  80. */
  81. static __u32 fec_mulicast_calc_crc(char *pAddr)
  82. {
  83. u8 byte;
  84. int byte_count;
  85. int bit_count;
  86. __u32 crc = 0xffffffff;
  87. u8 msb;
  88. for (byte_count = 0; byte_count < 6; byte_count++) {
  89. byte = pAddr[byte_count];
  90. for (bit_count = 0; bit_count < 8; bit_count++) {
  91. msb = crc >> 31;
  92. crc <<= 1;
  93. if (msb ^ (byte & 0x1)) {
  94. crc ^= FEC_CRC_POLY;
  95. }
  96. byte >>= 1;
  97. }
  98. }
  99. return (crc);
  100. }
  101. /*
  102. * Set or clear the multicast filter for this adaptor.
  103. * Skeleton taken from sunlance driver.
  104. * The CPM Ethernet implementation allows Multicast as well as individual
  105. * MAC address filtering. Some of the drivers check to make sure it is
  106. * a group multicast address, and discard those that are not. I guess I
  107. * will do the same for now, but just remove the test if you want
  108. * individual filtering as well (do the upper net layers want or support
  109. * this kind of feature?).
  110. */
  111. static void fec_set_multicast_list(struct net_device *dev)
  112. {
  113. struct fec_enet_private *fep = netdev_priv(dev);
  114. fec_t *fecp = fep->fecp;
  115. struct dev_mc_list *pmc;
  116. __u32 crc;
  117. int temp;
  118. __u32 csrVal;
  119. int hash_index;
  120. __u32 hthi, htlo;
  121. unsigned long flags;
  122. if ((dev->flags & IFF_PROMISC) != 0) {
  123. spin_lock_irqsave(&fep->lock, flags);
  124. FS(fecp, r_cntrl, FEC_RCNTRL_PROM);
  125. spin_unlock_irqrestore(&fep->lock, flags);
  126. /*
  127. * Log any net taps.
  128. */
  129. printk(KERN_WARNING DRV_MODULE_NAME
  130. ": %s: Promiscuous mode enabled.\n", dev->name);
  131. return;
  132. }
  133. if ((dev->flags & IFF_ALLMULTI) != 0 ||
  134. dev->mc_count > FEC_MAX_MULTICAST_ADDRS) {
  135. /*
  136. * Catch all multicast addresses, set the filter to all 1's.
  137. */
  138. hthi = 0xffffffffU;
  139. htlo = 0xffffffffU;
  140. } else {
  141. hthi = 0;
  142. htlo = 0;
  143. /*
  144. * Now populate the hash table
  145. */
  146. for (pmc = dev->mc_list; pmc != NULL; pmc = pmc->next) {
  147. crc = fec_mulicast_calc_crc(pmc->dmi_addr);
  148. temp = (crc & 0x3f) >> 1;
  149. hash_index = ((temp & 0x01) << 4) |
  150. ((temp & 0x02) << 2) |
  151. ((temp & 0x04)) |
  152. ((temp & 0x08) >> 2) |
  153. ((temp & 0x10) >> 4);
  154. csrVal = (1 << hash_index);
  155. if (crc & 1)
  156. hthi |= csrVal;
  157. else
  158. htlo |= csrVal;
  159. }
  160. }
  161. spin_lock_irqsave(&fep->lock, flags);
  162. FC(fecp, r_cntrl, FEC_RCNTRL_PROM);
  163. FW(fecp, hash_table_high, hthi);
  164. FW(fecp, hash_table_low, htlo);
  165. spin_unlock_irqrestore(&fep->lock, flags);
  166. }
  167. static int fec_set_mac_address(struct net_device *dev, void *addr)
  168. {
  169. struct sockaddr *mac = addr;
  170. struct fec_enet_private *fep = netdev_priv(dev);
  171. struct fec *fecp = fep->fecp;
  172. int i;
  173. __u32 addrhi, addrlo;
  174. unsigned long flags;
  175. /* Get pointer to SCC area in parameter RAM. */
  176. for (i = 0; i < 6; i++)
  177. dev->dev_addr[i] = mac->sa_data[i];
  178. /*
  179. * Set station address.
  180. */
  181. addrhi = ((__u32) dev->dev_addr[0] << 24) |
  182. ((__u32) dev->dev_addr[1] << 16) |
  183. ((__u32) dev->dev_addr[2] << 8) |
  184. (__u32) dev->dev_addr[3];
  185. addrlo = ((__u32) dev->dev_addr[4] << 24) |
  186. ((__u32) dev->dev_addr[5] << 16);
  187. spin_lock_irqsave(&fep->lock, flags);
  188. FW(fecp, addr_low, addrhi);
  189. FW(fecp, addr_high, addrlo);
  190. spin_unlock_irqrestore(&fep->lock, flags);
  191. return 0;
  192. }
  193. /*
  194. * This function is called to start or restart the FEC during a link
  195. * change. This only happens when switching between half and full
  196. * duplex.
  197. */
  198. void fec_restart(struct net_device *dev, int duplex, int speed)
  199. {
  200. #ifdef CONFIG_DUET
  201. immap_t *immap = (immap_t *) IMAP_ADDR;
  202. __u32 cptr;
  203. #endif
  204. struct fec_enet_private *fep = netdev_priv(dev);
  205. struct fec *fecp = fep->fecp;
  206. const struct fec_platform_info *fpi = fep->fpi;
  207. cbd_t *bdp;
  208. struct sk_buff *skb;
  209. int i;
  210. __u32 addrhi, addrlo;
  211. fec_whack_reset(fep->fecp);
  212. /*
  213. * Set station address.
  214. */
  215. addrhi = ((__u32) dev->dev_addr[0] << 24) |
  216. ((__u32) dev->dev_addr[1] << 16) |
  217. ((__u32) dev->dev_addr[2] << 8) |
  218. (__u32) dev->dev_addr[3];
  219. addrlo = ((__u32) dev->dev_addr[4] << 24) |
  220. ((__u32) dev->dev_addr[5] << 16);
  221. FW(fecp, addr_low, addrhi);
  222. FW(fecp, addr_high, addrlo);
  223. /*
  224. * Reset all multicast.
  225. */
  226. FW(fecp, hash_table_high, 0);
  227. FW(fecp, hash_table_low, 0);
  228. /*
  229. * Set maximum receive buffer size.
  230. */
  231. FW(fecp, r_buff_size, PKT_MAXBLR_SIZE);
  232. FW(fecp, r_hash, PKT_MAXBUF_SIZE);
  233. /*
  234. * Set receive and transmit descriptor base.
  235. */
  236. FW(fecp, r_des_start, iopa((__u32) (fep->rx_bd_base)));
  237. FW(fecp, x_des_start, iopa((__u32) (fep->tx_bd_base)));
  238. fep->dirty_tx = fep->cur_tx = fep->tx_bd_base;
  239. fep->tx_free = fep->tx_ring;
  240. fep->cur_rx = fep->rx_bd_base;
  241. /*
  242. * Reset SKB receive buffers
  243. */
  244. for (i = 0; i < fep->rx_ring; i++) {
  245. if ((skb = fep->rx_skbuff[i]) == NULL)
  246. continue;
  247. fep->rx_skbuff[i] = NULL;
  248. dev_kfree_skb(skb);
  249. }
  250. /*
  251. * Initialize the receive buffer descriptors.
  252. */
  253. for (i = 0, bdp = fep->rx_bd_base; i < fep->rx_ring; i++, bdp++) {
  254. skb = dev_alloc_skb(ENET_RX_FRSIZE);
  255. if (skb == NULL) {
  256. printk(KERN_WARNING DRV_MODULE_NAME
  257. ": %s Memory squeeze, unable to allocate skb\n",
  258. dev->name);
  259. fep->stats.rx_dropped++;
  260. break;
  261. }
  262. fep->rx_skbuff[i] = skb;
  263. skb->dev = dev;
  264. CBDW_BUFADDR(bdp, dma_map_single(NULL, skb->data,
  265. L1_CACHE_ALIGN(PKT_MAXBUF_SIZE),
  266. DMA_FROM_DEVICE));
  267. CBDW_DATLEN(bdp, 0); /* zero */
  268. CBDW_SC(bdp, BD_ENET_RX_EMPTY |
  269. ((i < fep->rx_ring - 1) ? 0 : BD_SC_WRAP));
  270. }
  271. /*
  272. * if we failed, fillup remainder
  273. */
  274. for (; i < fep->rx_ring; i++, bdp++) {
  275. fep->rx_skbuff[i] = NULL;
  276. CBDW_SC(bdp, (i < fep->rx_ring - 1) ? 0 : BD_SC_WRAP);
  277. }
  278. /*
  279. * Reset SKB transmit buffers.
  280. */
  281. for (i = 0; i < fep->tx_ring; i++) {
  282. if ((skb = fep->tx_skbuff[i]) == NULL)
  283. continue;
  284. fep->tx_skbuff[i] = NULL;
  285. dev_kfree_skb(skb);
  286. }
  287. /*
  288. * ...and the same for transmit.
  289. */
  290. for (i = 0, bdp = fep->tx_bd_base; i < fep->tx_ring; i++, bdp++) {
  291. fep->tx_skbuff[i] = NULL;
  292. CBDW_BUFADDR(bdp, virt_to_bus(NULL));
  293. CBDW_DATLEN(bdp, 0);
  294. CBDW_SC(bdp, (i < fep->tx_ring - 1) ? 0 : BD_SC_WRAP);
  295. }
  296. /*
  297. * Enable big endian and don't care about SDMA FC.
  298. */
  299. FW(fecp, fun_code, 0x78000000);
  300. /*
  301. * Set MII speed.
  302. */
  303. FW(fecp, mii_speed, fep->fec_phy_speed);
  304. /*
  305. * Clear any outstanding interrupt.
  306. */
  307. FW(fecp, ievent, 0xffc0);
  308. FW(fecp, ivec, (fpi->fec_irq / 2) << 29);
  309. /*
  310. * adjust to speed (only for DUET & RMII)
  311. */
  312. #ifdef CONFIG_DUET
  313. cptr = in_be32(&immap->im_cpm.cp_cptr);
  314. switch (fpi->fec_no) {
  315. case 0:
  316. /*
  317. * check if in RMII mode
  318. */
  319. if ((cptr & 0x100) == 0)
  320. break;
  321. if (speed == 10)
  322. cptr |= 0x0000010;
  323. else if (speed == 100)
  324. cptr &= ~0x0000010;
  325. break;
  326. case 1:
  327. /*
  328. * check if in RMII mode
  329. */
  330. if ((cptr & 0x80) == 0)
  331. break;
  332. if (speed == 10)
  333. cptr |= 0x0000008;
  334. else if (speed == 100)
  335. cptr &= ~0x0000008;
  336. break;
  337. default:
  338. break;
  339. }
  340. out_be32(&immap->im_cpm.cp_cptr, cptr);
  341. #endif
  342. FW(fecp, r_cntrl, FEC_RCNTRL_MII_MODE); /* MII enable */
  343. /*
  344. * adjust to duplex mode
  345. */
  346. if (duplex) {
  347. FC(fecp, r_cntrl, FEC_RCNTRL_DRT);
  348. FS(fecp, x_cntrl, FEC_TCNTRL_FDEN); /* FD enable */
  349. } else {
  350. FS(fecp, r_cntrl, FEC_RCNTRL_DRT);
  351. FC(fecp, x_cntrl, FEC_TCNTRL_FDEN); /* FD disable */
  352. }
  353. /*
  354. * Enable interrupts we wish to service.
  355. */
  356. FW(fecp, imask, FEC_ENET_TXF | FEC_ENET_TXB |
  357. FEC_ENET_RXF | FEC_ENET_RXB);
  358. /*
  359. * And last, enable the transmit and receive processing.
  360. */
  361. FW(fecp, ecntrl, FEC_ECNTRL_PINMUX | FEC_ECNTRL_ETHER_EN);
  362. FW(fecp, r_des_active, 0x01000000);
  363. }
  364. void fec_stop(struct net_device *dev)
  365. {
  366. struct fec_enet_private *fep = netdev_priv(dev);
  367. fec_t *fecp = fep->fecp;
  368. struct sk_buff *skb;
  369. int i;
  370. if ((FR(fecp, ecntrl) & FEC_ECNTRL_ETHER_EN) == 0)
  371. return; /* already down */
  372. FW(fecp, x_cntrl, 0x01); /* Graceful transmit stop */
  373. for (i = 0; ((FR(fecp, ievent) & 0x10000000) == 0) &&
  374. i < FEC_RESET_DELAY; i++)
  375. udelay(1);
  376. if (i == FEC_RESET_DELAY)
  377. printk(KERN_WARNING DRV_MODULE_NAME
  378. ": %s FEC timeout on graceful transmit stop\n",
  379. dev->name);
  380. /*
  381. * Disable FEC. Let only MII interrupts.
  382. */
  383. FW(fecp, imask, 0);
  384. FW(fecp, ecntrl, ~FEC_ECNTRL_ETHER_EN);
  385. /*
  386. * Reset SKB transmit buffers.
  387. */
  388. for (i = 0; i < fep->tx_ring; i++) {
  389. if ((skb = fep->tx_skbuff[i]) == NULL)
  390. continue;
  391. fep->tx_skbuff[i] = NULL;
  392. dev_kfree_skb(skb);
  393. }
  394. /*
  395. * Reset SKB receive buffers
  396. */
  397. for (i = 0; i < fep->rx_ring; i++) {
  398. if ((skb = fep->rx_skbuff[i]) == NULL)
  399. continue;
  400. fep->rx_skbuff[i] = NULL;
  401. dev_kfree_skb(skb);
  402. }
  403. }
  404. /* common receive function */
  405. static int fec_enet_rx_common(struct fec_enet_private *ep,
  406. struct net_device *dev, int budget)
  407. {
  408. fec_t *fecp = fep->fecp;
  409. const struct fec_platform_info *fpi = fep->fpi;
  410. cbd_t *bdp;
  411. struct sk_buff *skb, *skbn, *skbt;
  412. int received = 0;
  413. __u16 pkt_len, sc;
  414. int curidx;
  415. /*
  416. * First, grab all of the stats for the incoming packet.
  417. * These get messed up if we get called due to a busy condition.
  418. */
  419. bdp = fep->cur_rx;
  420. /* clear RX status bits for napi*/
  421. if (fpi->use_napi)
  422. FW(fecp, ievent, FEC_ENET_RXF | FEC_ENET_RXB);
  423. while (((sc = CBDR_SC(bdp)) & BD_ENET_RX_EMPTY) == 0) {
  424. curidx = bdp - fep->rx_bd_base;
  425. /*
  426. * Since we have allocated space to hold a complete frame,
  427. * the last indicator should be set.
  428. */
  429. if ((sc & BD_ENET_RX_LAST) == 0)
  430. printk(KERN_WARNING DRV_MODULE_NAME
  431. ": %s rcv is not +last\n",
  432. dev->name);
  433. /*
  434. * Check for errors.
  435. */
  436. if (sc & (BD_ENET_RX_LG | BD_ENET_RX_SH | BD_ENET_RX_CL |
  437. BD_ENET_RX_NO | BD_ENET_RX_CR | BD_ENET_RX_OV)) {
  438. fep->stats.rx_errors++;
  439. /* Frame too long or too short. */
  440. if (sc & (BD_ENET_RX_LG | BD_ENET_RX_SH))
  441. fep->stats.rx_length_errors++;
  442. /* Frame alignment */
  443. if (sc & (BD_ENET_RX_NO | BD_ENET_RX_CL))
  444. fep->stats.rx_frame_errors++;
  445. /* CRC Error */
  446. if (sc & BD_ENET_RX_CR)
  447. fep->stats.rx_crc_errors++;
  448. /* FIFO overrun */
  449. if (sc & BD_ENET_RX_OV)
  450. fep->stats.rx_crc_errors++;
  451. skbn = fep->rx_skbuff[curidx];
  452. BUG_ON(skbn == NULL);
  453. } else {
  454. skb = fep->rx_skbuff[curidx];
  455. BUG_ON(skb == NULL);
  456. /*
  457. * Process the incoming frame.
  458. */
  459. fep->stats.rx_packets++;
  460. pkt_len = CBDR_DATLEN(bdp) - 4; /* remove CRC */
  461. fep->stats.rx_bytes += pkt_len + 4;
  462. if (pkt_len <= fpi->rx_copybreak) {
  463. /* +2 to make IP header L1 cache aligned */
  464. skbn = dev_alloc_skb(pkt_len + 2);
  465. if (skbn != NULL) {
  466. skb_reserve(skbn, 2); /* align IP header */
  467. skb_copy_from_linear_data(skb,
  468. skbn->data,
  469. pkt_len);
  470. /* swap */
  471. skbt = skb;
  472. skb = skbn;
  473. skbn = skbt;
  474. }
  475. } else
  476. skbn = dev_alloc_skb(ENET_RX_FRSIZE);
  477. if (skbn != NULL) {
  478. skb_put(skb, pkt_len); /* Make room */
  479. skb->protocol = eth_type_trans(skb, dev);
  480. received++;
  481. if (!fpi->use_napi)
  482. netif_rx(skb);
  483. else
  484. netif_receive_skb(skb);
  485. } else {
  486. printk(KERN_WARNING DRV_MODULE_NAME
  487. ": %s Memory squeeze, dropping packet.\n",
  488. dev->name);
  489. fep->stats.rx_dropped++;
  490. skbn = skb;
  491. }
  492. }
  493. fep->rx_skbuff[curidx] = skbn;
  494. CBDW_BUFADDR(bdp, dma_map_single(NULL, skbn->data,
  495. L1_CACHE_ALIGN(PKT_MAXBUF_SIZE),
  496. DMA_FROM_DEVICE));
  497. CBDW_DATLEN(bdp, 0);
  498. CBDW_SC(bdp, (sc & ~BD_ENET_RX_STATS) | BD_ENET_RX_EMPTY);
  499. /*
  500. * Update BD pointer to next entry.
  501. */
  502. if ((sc & BD_ENET_RX_WRAP) == 0)
  503. bdp++;
  504. else
  505. bdp = fep->rx_bd_base;
  506. /*
  507. * Doing this here will keep the FEC running while we process
  508. * incoming frames. On a heavily loaded network, we should be
  509. * able to keep up at the expense of system resources.
  510. */
  511. FW(fecp, r_des_active, 0x01000000);
  512. if (received >= budget)
  513. break;
  514. }
  515. fep->cur_rx = bdp;
  516. if (fpi->use_napi) {
  517. if (received < budget) {
  518. netif_rx_complete(dev, &fep->napi);
  519. /* enable RX interrupt bits */
  520. FS(fecp, imask, FEC_ENET_RXF | FEC_ENET_RXB);
  521. }
  522. }
  523. return received;
  524. }
  525. static void fec_enet_tx(struct net_device *dev)
  526. {
  527. struct fec_enet_private *fep = netdev_priv(dev);
  528. cbd_t *bdp;
  529. struct sk_buff *skb;
  530. int dirtyidx, do_wake;
  531. __u16 sc;
  532. spin_lock(&fep->lock);
  533. bdp = fep->dirty_tx;
  534. do_wake = 0;
  535. while (((sc = CBDR_SC(bdp)) & BD_ENET_TX_READY) == 0) {
  536. dirtyidx = bdp - fep->tx_bd_base;
  537. if (fep->tx_free == fep->tx_ring)
  538. break;
  539. skb = fep->tx_skbuff[dirtyidx];
  540. /*
  541. * Check for errors.
  542. */
  543. if (sc & (BD_ENET_TX_HB | BD_ENET_TX_LC |
  544. BD_ENET_TX_RL | BD_ENET_TX_UN | BD_ENET_TX_CSL)) {
  545. fep->stats.tx_errors++;
  546. if (sc & BD_ENET_TX_HB) /* No heartbeat */
  547. fep->stats.tx_heartbeat_errors++;
  548. if (sc & BD_ENET_TX_LC) /* Late collision */
  549. fep->stats.tx_window_errors++;
  550. if (sc & BD_ENET_TX_RL) /* Retrans limit */
  551. fep->stats.tx_aborted_errors++;
  552. if (sc & BD_ENET_TX_UN) /* Underrun */
  553. fep->stats.tx_fifo_errors++;
  554. if (sc & BD_ENET_TX_CSL) /* Carrier lost */
  555. fep->stats.tx_carrier_errors++;
  556. } else
  557. fep->stats.tx_packets++;
  558. if (sc & BD_ENET_TX_READY)
  559. printk(KERN_WARNING DRV_MODULE_NAME
  560. ": %s HEY! Enet xmit interrupt and TX_READY.\n",
  561. dev->name);
  562. /*
  563. * Deferred means some collisions occurred during transmit,
  564. * but we eventually sent the packet OK.
  565. */
  566. if (sc & BD_ENET_TX_DEF)
  567. fep->stats.collisions++;
  568. /*
  569. * Free the sk buffer associated with this last transmit.
  570. */
  571. dev_kfree_skb_irq(skb);
  572. fep->tx_skbuff[dirtyidx] = NULL;
  573. /*
  574. * Update pointer to next buffer descriptor to be transmitted.
  575. */
  576. if ((sc & BD_ENET_TX_WRAP) == 0)
  577. bdp++;
  578. else
  579. bdp = fep->tx_bd_base;
  580. /*
  581. * Since we have freed up a buffer, the ring is no longer
  582. * full.
  583. */
  584. if (!fep->tx_free++)
  585. do_wake = 1;
  586. }
  587. fep->dirty_tx = bdp;
  588. spin_unlock(&fep->lock);
  589. if (do_wake && netif_queue_stopped(dev))
  590. netif_wake_queue(dev);
  591. }
  592. /*
  593. * The interrupt handler.
  594. * This is called from the MPC core interrupt.
  595. */
  596. static irqreturn_t
  597. fec_enet_interrupt(int irq, void *dev_id)
  598. {
  599. struct net_device *dev = dev_id;
  600. struct fec_enet_private *fep;
  601. const struct fec_platform_info *fpi;
  602. fec_t *fecp;
  603. __u32 int_events;
  604. __u32 int_events_napi;
  605. if (unlikely(dev == NULL))
  606. return IRQ_NONE;
  607. fep = netdev_priv(dev);
  608. fecp = fep->fecp;
  609. fpi = fep->fpi;
  610. /*
  611. * Get the interrupt events that caused us to be here.
  612. */
  613. while ((int_events = FR(fecp, ievent) & FR(fecp, imask)) != 0) {
  614. if (!fpi->use_napi)
  615. FW(fecp, ievent, int_events);
  616. else {
  617. int_events_napi = int_events & ~(FEC_ENET_RXF | FEC_ENET_RXB);
  618. FW(fecp, ievent, int_events_napi);
  619. }
  620. if ((int_events & (FEC_ENET_HBERR | FEC_ENET_BABR |
  621. FEC_ENET_BABT | FEC_ENET_EBERR)) != 0)
  622. printk(KERN_WARNING DRV_MODULE_NAME
  623. ": %s FEC ERROR(s) 0x%x\n",
  624. dev->name, int_events);
  625. if ((int_events & FEC_ENET_RXF) != 0) {
  626. if (!fpi->use_napi)
  627. fec_enet_rx_common(fep, dev, ~0);
  628. else {
  629. if (netif_rx_schedule_prep(dev, &fep->napi)) {
  630. /* disable rx interrupts */
  631. FC(fecp, imask, FEC_ENET_RXF | FEC_ENET_RXB);
  632. __netif_rx_schedule(dev, &fep->napi);
  633. } else {
  634. printk(KERN_ERR DRV_MODULE_NAME
  635. ": %s driver bug! interrupt while in poll!\n",
  636. dev->name);
  637. FC(fecp, imask, FEC_ENET_RXF | FEC_ENET_RXB);
  638. }
  639. }
  640. }
  641. if ((int_events & FEC_ENET_TXF) != 0)
  642. fec_enet_tx(dev);
  643. }
  644. return IRQ_HANDLED;
  645. }
  646. /* This interrupt occurs when the PHY detects a link change. */
  647. static irqreturn_t
  648. fec_mii_link_interrupt(int irq, void *dev_id)
  649. {
  650. struct net_device *dev = dev_id;
  651. struct fec_enet_private *fep;
  652. const struct fec_platform_info *fpi;
  653. if (unlikely(dev == NULL))
  654. return IRQ_NONE;
  655. fep = netdev_priv(dev);
  656. fpi = fep->fpi;
  657. if (!fpi->use_mdio)
  658. return IRQ_NONE;
  659. /*
  660. * Acknowledge the interrupt if possible. If we have not
  661. * found the PHY yet we can't process or acknowledge the
  662. * interrupt now. Instead we ignore this interrupt for now,
  663. * which we can do since it is edge triggered. It will be
  664. * acknowledged later by fec_enet_open().
  665. */
  666. if (!fep->phy)
  667. return IRQ_NONE;
  668. fec_mii_ack_int(dev);
  669. fec_mii_link_status_change_check(dev, 0);
  670. return IRQ_HANDLED;
  671. }
  672. /**********************************************************************************/
  673. static int fec_enet_start_xmit(struct sk_buff *skb, struct net_device *dev)
  674. {
  675. struct fec_enet_private *fep = netdev_priv(dev);
  676. fec_t *fecp = fep->fecp;
  677. cbd_t *bdp;
  678. int curidx;
  679. unsigned long flags;
  680. spin_lock_irqsave(&fep->tx_lock, flags);
  681. /*
  682. * Fill in a Tx ring entry
  683. */
  684. bdp = fep->cur_tx;
  685. if (!fep->tx_free || (CBDR_SC(bdp) & BD_ENET_TX_READY)) {
  686. netif_stop_queue(dev);
  687. spin_unlock_irqrestore(&fep->tx_lock, flags);
  688. /*
  689. * Ooops. All transmit buffers are full. Bail out.
  690. * This should not happen, since the tx queue should be stopped.
  691. */
  692. printk(KERN_WARNING DRV_MODULE_NAME
  693. ": %s tx queue full!.\n", dev->name);
  694. return 1;
  695. }
  696. curidx = bdp - fep->tx_bd_base;
  697. /*
  698. * Clear all of the status flags.
  699. */
  700. CBDC_SC(bdp, BD_ENET_TX_STATS);
  701. /*
  702. * Save skb pointer.
  703. */
  704. fep->tx_skbuff[curidx] = skb;
  705. fep->stats.tx_bytes += skb->len;
  706. /*
  707. * Push the data cache so the CPM does not get stale memory data.
  708. */
  709. CBDW_BUFADDR(bdp, dma_map_single(NULL, skb->data,
  710. skb->len, DMA_TO_DEVICE));
  711. CBDW_DATLEN(bdp, skb->len);
  712. dev->trans_start = jiffies;
  713. /*
  714. * If this was the last BD in the ring, start at the beginning again.
  715. */
  716. if ((CBDR_SC(bdp) & BD_ENET_TX_WRAP) == 0)
  717. fep->cur_tx++;
  718. else
  719. fep->cur_tx = fep->tx_bd_base;
  720. if (!--fep->tx_free)
  721. netif_stop_queue(dev);
  722. /*
  723. * Trigger transmission start
  724. */
  725. CBDS_SC(bdp, BD_ENET_TX_READY | BD_ENET_TX_INTR |
  726. BD_ENET_TX_LAST | BD_ENET_TX_TC);
  727. FW(fecp, x_des_active, 0x01000000);
  728. spin_unlock_irqrestore(&fep->tx_lock, flags);
  729. return 0;
  730. }
  731. static void fec_timeout(struct net_device *dev)
  732. {
  733. struct fec_enet_private *fep = netdev_priv(dev);
  734. fep->stats.tx_errors++;
  735. if (fep->tx_free)
  736. netif_wake_queue(dev);
  737. /* check link status again */
  738. fec_mii_link_status_change_check(dev, 0);
  739. }
  740. static int fec_enet_open(struct net_device *dev)
  741. {
  742. struct fec_enet_private *fep = netdev_priv(dev);
  743. const struct fec_platform_info *fpi = fep->fpi;
  744. unsigned long flags;
  745. napi_enable(&fep->napi);
  746. /* Install our interrupt handler. */
  747. if (request_irq(fpi->fec_irq, fec_enet_interrupt, 0, "fec", dev) != 0) {
  748. printk(KERN_ERR DRV_MODULE_NAME
  749. ": %s Could not allocate FEC IRQ!", dev->name);
  750. napi_disable(&fep->napi);
  751. return -EINVAL;
  752. }
  753. /* Install our phy interrupt handler */
  754. if (fpi->phy_irq != -1 &&
  755. request_irq(fpi->phy_irq, fec_mii_link_interrupt, 0, "fec-phy",
  756. dev) != 0) {
  757. printk(KERN_ERR DRV_MODULE_NAME
  758. ": %s Could not allocate PHY IRQ!", dev->name);
  759. free_irq(fpi->fec_irq, dev);
  760. napi_disable(&fep->napi);
  761. return -EINVAL;
  762. }
  763. if (fpi->use_mdio) {
  764. fec_mii_startup(dev);
  765. netif_carrier_off(dev);
  766. fec_mii_link_status_change_check(dev, 1);
  767. } else {
  768. spin_lock_irqsave(&fep->lock, flags);
  769. fec_restart(dev, 1, 100); /* XXX this sucks */
  770. spin_unlock_irqrestore(&fep->lock, flags);
  771. netif_carrier_on(dev);
  772. netif_start_queue(dev);
  773. }
  774. return 0;
  775. }
  776. static int fec_enet_close(struct net_device *dev)
  777. {
  778. struct fec_enet_private *fep = netdev_priv(dev);
  779. const struct fec_platform_info *fpi = fep->fpi;
  780. unsigned long flags;
  781. netif_stop_queue(dev);
  782. napi_disable(&fep->napi);
  783. netif_carrier_off(dev);
  784. if (fpi->use_mdio)
  785. fec_mii_shutdown(dev);
  786. spin_lock_irqsave(&fep->lock, flags);
  787. fec_stop(dev);
  788. spin_unlock_irqrestore(&fep->lock, flags);
  789. /* release any irqs */
  790. if (fpi->phy_irq != -1)
  791. free_irq(fpi->phy_irq, dev);
  792. free_irq(fpi->fec_irq, dev);
  793. return 0;
  794. }
  795. static struct net_device_stats *fec_enet_get_stats(struct net_device *dev)
  796. {
  797. struct fec_enet_private *fep = netdev_priv(dev);
  798. return &fep->stats;
  799. }
  800. static int fec_enet_poll(struct napi_struct *napi, int budget)
  801. {
  802. struct fec_enet_private *fep = container_of(napi, struct fec_enet_private, napi);
  803. struct net_device *dev = fep->dev;
  804. return fec_enet_rx_common(fep, dev, budget);
  805. }
  806. /*************************************************************************/
  807. static void fec_get_drvinfo(struct net_device *dev,
  808. struct ethtool_drvinfo *info)
  809. {
  810. strcpy(info->driver, DRV_MODULE_NAME);
  811. strcpy(info->version, DRV_MODULE_VERSION);
  812. }
  813. static int fec_get_regs_len(struct net_device *dev)
  814. {
  815. return sizeof(fec_t);
  816. }
  817. static void fec_get_regs(struct net_device *dev, struct ethtool_regs *regs,
  818. void *p)
  819. {
  820. struct fec_enet_private *fep = netdev_priv(dev);
  821. unsigned long flags;
  822. if (regs->len < sizeof(fec_t))
  823. return;
  824. regs->version = 0;
  825. spin_lock_irqsave(&fep->lock, flags);
  826. memcpy_fromio(p, fep->fecp, sizeof(fec_t));
  827. spin_unlock_irqrestore(&fep->lock, flags);
  828. }
  829. static int fec_get_settings(struct net_device *dev, struct ethtool_cmd *cmd)
  830. {
  831. struct fec_enet_private *fep = netdev_priv(dev);
  832. unsigned long flags;
  833. int rc;
  834. spin_lock_irqsave(&fep->lock, flags);
  835. rc = mii_ethtool_gset(&fep->mii_if, cmd);
  836. spin_unlock_irqrestore(&fep->lock, flags);
  837. return rc;
  838. }
  839. static int fec_set_settings(struct net_device *dev, struct ethtool_cmd *cmd)
  840. {
  841. struct fec_enet_private *fep = netdev_priv(dev);
  842. unsigned long flags;
  843. int rc;
  844. spin_lock_irqsave(&fep->lock, flags);
  845. rc = mii_ethtool_sset(&fep->mii_if, cmd);
  846. spin_unlock_irqrestore(&fep->lock, flags);
  847. return rc;
  848. }
  849. static int fec_nway_reset(struct net_device *dev)
  850. {
  851. struct fec_enet_private *fep = netdev_priv(dev);
  852. return mii_nway_restart(&fep->mii_if);
  853. }
  854. static __u32 fec_get_msglevel(struct net_device *dev)
  855. {
  856. struct fec_enet_private *fep = netdev_priv(dev);
  857. return fep->msg_enable;
  858. }
  859. static void fec_set_msglevel(struct net_device *dev, __u32 value)
  860. {
  861. struct fec_enet_private *fep = netdev_priv(dev);
  862. fep->msg_enable = value;
  863. }
  864. static const struct ethtool_ops fec_ethtool_ops = {
  865. .get_drvinfo = fec_get_drvinfo,
  866. .get_regs_len = fec_get_regs_len,
  867. .get_settings = fec_get_settings,
  868. .set_settings = fec_set_settings,
  869. .nway_reset = fec_nway_reset,
  870. .get_link = ethtool_op_get_link,
  871. .get_msglevel = fec_get_msglevel,
  872. .set_msglevel = fec_set_msglevel,
  873. .set_tx_csum = ethtool_op_set_tx_csum, /* local! */
  874. .set_sg = ethtool_op_set_sg,
  875. .get_regs = fec_get_regs,
  876. };
  877. static int fec_ioctl(struct net_device *dev, struct ifreq *rq, int cmd)
  878. {
  879. struct fec_enet_private *fep = netdev_priv(dev);
  880. struct mii_ioctl_data *mii = (struct mii_ioctl_data *)&rq->ifr_data;
  881. unsigned long flags;
  882. int rc;
  883. if (!netif_running(dev))
  884. return -EINVAL;
  885. spin_lock_irqsave(&fep->lock, flags);
  886. rc = generic_mii_ioctl(&fep->mii_if, mii, cmd, NULL);
  887. spin_unlock_irqrestore(&fep->lock, flags);
  888. return rc;
  889. }
  890. int fec_8xx_init_one(const struct fec_platform_info *fpi,
  891. struct net_device **devp)
  892. {
  893. immap_t *immap = (immap_t *) IMAP_ADDR;
  894. static int fec_8xx_version_printed = 0;
  895. struct net_device *dev = NULL;
  896. struct fec_enet_private *fep = NULL;
  897. fec_t *fecp = NULL;
  898. int i;
  899. int err = 0;
  900. int registered = 0;
  901. __u32 siel;
  902. *devp = NULL;
  903. switch (fpi->fec_no) {
  904. case 0:
  905. fecp = &((immap_t *) IMAP_ADDR)->im_cpm.cp_fec;
  906. break;
  907. #ifdef CONFIG_DUET
  908. case 1:
  909. fecp = &((immap_t *) IMAP_ADDR)->im_cpm.cp_fec2;
  910. break;
  911. #endif
  912. default:
  913. return -EINVAL;
  914. }
  915. if (fec_8xx_version_printed++ == 0)
  916. printk(KERN_INFO "%s", version);
  917. i = sizeof(*fep) + (sizeof(struct sk_buff **) *
  918. (fpi->rx_ring + fpi->tx_ring));
  919. dev = alloc_etherdev(i);
  920. if (!dev) {
  921. err = -ENOMEM;
  922. goto err;
  923. }
  924. fep = netdev_priv(dev);
  925. fep->dev = dev;
  926. /* partial reset of FEC */
  927. fec_whack_reset(fecp);
  928. /* point rx_skbuff, tx_skbuff */
  929. fep->rx_skbuff = (struct sk_buff **)&fep[1];
  930. fep->tx_skbuff = fep->rx_skbuff + fpi->rx_ring;
  931. fep->fecp = fecp;
  932. fep->fpi = fpi;
  933. /* init locks */
  934. spin_lock_init(&fep->lock);
  935. spin_lock_init(&fep->tx_lock);
  936. /*
  937. * Set the Ethernet address.
  938. */
  939. for (i = 0; i < 6; i++)
  940. dev->dev_addr[i] = fpi->macaddr[i];
  941. fep->ring_base = dma_alloc_coherent(NULL,
  942. (fpi->tx_ring + fpi->rx_ring) *
  943. sizeof(cbd_t), &fep->ring_mem_addr,
  944. GFP_KERNEL);
  945. if (fep->ring_base == NULL) {
  946. printk(KERN_ERR DRV_MODULE_NAME
  947. ": %s dma alloc failed.\n", dev->name);
  948. err = -ENOMEM;
  949. goto err;
  950. }
  951. /*
  952. * Set receive and transmit descriptor base.
  953. */
  954. fep->rx_bd_base = fep->ring_base;
  955. fep->tx_bd_base = fep->rx_bd_base + fpi->rx_ring;
  956. /* initialize ring size variables */
  957. fep->tx_ring = fpi->tx_ring;
  958. fep->rx_ring = fpi->rx_ring;
  959. /* SIU interrupt */
  960. if (fpi->phy_irq != -1 &&
  961. (fpi->phy_irq >= SIU_IRQ0 && fpi->phy_irq < SIU_LEVEL7)) {
  962. siel = in_be32(&immap->im_siu_conf.sc_siel);
  963. if ((fpi->phy_irq & 1) == 0)
  964. siel |= (0x80000000 >> fpi->phy_irq);
  965. else
  966. siel &= ~(0x80000000 >> (fpi->phy_irq & ~1));
  967. out_be32(&immap->im_siu_conf.sc_siel, siel);
  968. }
  969. /*
  970. * The FEC Ethernet specific entries in the device structure.
  971. */
  972. dev->open = fec_enet_open;
  973. dev->hard_start_xmit = fec_enet_start_xmit;
  974. dev->tx_timeout = fec_timeout;
  975. dev->watchdog_timeo = TX_TIMEOUT;
  976. dev->stop = fec_enet_close;
  977. dev->get_stats = fec_enet_get_stats;
  978. dev->set_multicast_list = fec_set_multicast_list;
  979. dev->set_mac_address = fec_set_mac_address;
  980. netif_napi_add(dev, &fec->napi,
  981. fec_enet_poll, fpi->napi_weight);
  982. dev->ethtool_ops = &fec_ethtool_ops;
  983. dev->do_ioctl = fec_ioctl;
  984. fep->fec_phy_speed =
  985. ((((fpi->sys_clk + 4999999) / 2500000) / 2) & 0x3F) << 1;
  986. init_timer(&fep->phy_timer_list);
  987. /* partial reset of FEC so that only MII works */
  988. FW(fecp, mii_speed, fep->fec_phy_speed);
  989. FW(fecp, ievent, 0xffc0);
  990. FW(fecp, ivec, (fpi->fec_irq / 2) << 29);
  991. FW(fecp, imask, 0);
  992. FW(fecp, r_cntrl, FEC_RCNTRL_MII_MODE); /* MII enable */
  993. FW(fecp, ecntrl, FEC_ECNTRL_PINMUX | FEC_ECNTRL_ETHER_EN);
  994. netif_carrier_off(dev);
  995. err = register_netdev(dev);
  996. if (err != 0)
  997. goto err;
  998. registered = 1;
  999. if (fpi->use_mdio) {
  1000. fep->mii_if.dev = dev;
  1001. fep->mii_if.mdio_read = fec_mii_read;
  1002. fep->mii_if.mdio_write = fec_mii_write;
  1003. fep->mii_if.phy_id_mask = 0x1f;
  1004. fep->mii_if.reg_num_mask = 0x1f;
  1005. fep->mii_if.phy_id = fec_mii_phy_id_detect(dev);
  1006. }
  1007. *devp = dev;
  1008. return 0;
  1009. err:
  1010. if (dev != NULL) {
  1011. if (fecp != NULL)
  1012. fec_whack_reset(fecp);
  1013. if (registered)
  1014. unregister_netdev(dev);
  1015. if (fep != NULL) {
  1016. if (fep->ring_base)
  1017. dma_free_coherent(NULL,
  1018. (fpi->tx_ring +
  1019. fpi->rx_ring) *
  1020. sizeof(cbd_t), fep->ring_base,
  1021. fep->ring_mem_addr);
  1022. }
  1023. free_netdev(dev);
  1024. }
  1025. return err;
  1026. }
  1027. int fec_8xx_cleanup_one(struct net_device *dev)
  1028. {
  1029. struct fec_enet_private *fep = netdev_priv(dev);
  1030. fec_t *fecp = fep->fecp;
  1031. const struct fec_platform_info *fpi = fep->fpi;
  1032. fec_whack_reset(fecp);
  1033. unregister_netdev(dev);
  1034. dma_free_coherent(NULL, (fpi->tx_ring + fpi->rx_ring) * sizeof(cbd_t),
  1035. fep->ring_base, fep->ring_mem_addr);
  1036. free_netdev(dev);
  1037. return 0;
  1038. }
  1039. /**************************************************************************************/
  1040. /**************************************************************************************/
  1041. /**************************************************************************************/
  1042. static int __init fec_8xx_init(void)
  1043. {
  1044. return fec_8xx_platform_init();
  1045. }
  1046. static void __exit fec_8xx_cleanup(void)
  1047. {
  1048. fec_8xx_platform_cleanup();
  1049. }
  1050. /**************************************************************************************/
  1051. /**************************************************************************************/
  1052. /**************************************************************************************/
  1053. module_init(fec_8xx_init);
  1054. module_exit(fec_8xx_cleanup);