fec_main.c 29 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273
  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/commproc.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 net_device *dev, int *budget)
  406. {
  407. struct fec_enet_private *fep = netdev_priv(dev);
  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. int rx_work_limit;
  416. if (fpi->use_napi) {
  417. rx_work_limit = min(dev->quota, *budget);
  418. if (!netif_running(dev))
  419. return 0;
  420. }
  421. /*
  422. * First, grab all of the stats for the incoming packet.
  423. * These get messed up if we get called due to a busy condition.
  424. */
  425. bdp = fep->cur_rx;
  426. /* clear RX status bits for napi*/
  427. if (fpi->use_napi)
  428. FW(fecp, ievent, FEC_ENET_RXF | FEC_ENET_RXB);
  429. while (((sc = CBDR_SC(bdp)) & BD_ENET_RX_EMPTY) == 0) {
  430. curidx = bdp - fep->rx_bd_base;
  431. /*
  432. * Since we have allocated space to hold a complete frame,
  433. * the last indicator should be set.
  434. */
  435. if ((sc & BD_ENET_RX_LAST) == 0)
  436. printk(KERN_WARNING DRV_MODULE_NAME
  437. ": %s rcv is not +last\n",
  438. dev->name);
  439. /*
  440. * Check for errors.
  441. */
  442. if (sc & (BD_ENET_RX_LG | BD_ENET_RX_SH | BD_ENET_RX_CL |
  443. BD_ENET_RX_NO | BD_ENET_RX_CR | BD_ENET_RX_OV)) {
  444. fep->stats.rx_errors++;
  445. /* Frame too long or too short. */
  446. if (sc & (BD_ENET_RX_LG | BD_ENET_RX_SH))
  447. fep->stats.rx_length_errors++;
  448. /* Frame alignment */
  449. if (sc & (BD_ENET_RX_NO | BD_ENET_RX_CL))
  450. fep->stats.rx_frame_errors++;
  451. /* CRC Error */
  452. if (sc & BD_ENET_RX_CR)
  453. fep->stats.rx_crc_errors++;
  454. /* FIFO overrun */
  455. if (sc & BD_ENET_RX_OV)
  456. fep->stats.rx_crc_errors++;
  457. skbn = fep->rx_skbuff[curidx];
  458. BUG_ON(skbn == NULL);
  459. } else {
  460. /* napi, got packet but no quota */
  461. if (fpi->use_napi && --rx_work_limit < 0)
  462. break;
  463. skb = fep->rx_skbuff[curidx];
  464. BUG_ON(skb == NULL);
  465. /*
  466. * Process the incoming frame.
  467. */
  468. fep->stats.rx_packets++;
  469. pkt_len = CBDR_DATLEN(bdp) - 4; /* remove CRC */
  470. fep->stats.rx_bytes += pkt_len + 4;
  471. if (pkt_len <= fpi->rx_copybreak) {
  472. /* +2 to make IP header L1 cache aligned */
  473. skbn = dev_alloc_skb(pkt_len + 2);
  474. if (skbn != NULL) {
  475. skb_reserve(skbn, 2); /* align IP header */
  476. skb_copy_from_linear_data(skb
  477. skbn->data,
  478. pkt_len);
  479. /* swap */
  480. skbt = skb;
  481. skb = skbn;
  482. skbn = skbt;
  483. }
  484. } else
  485. skbn = dev_alloc_skb(ENET_RX_FRSIZE);
  486. if (skbn != NULL) {
  487. skb_put(skb, pkt_len); /* Make room */
  488. skb->protocol = eth_type_trans(skb, dev);
  489. received++;
  490. if (!fpi->use_napi)
  491. netif_rx(skb);
  492. else
  493. netif_receive_skb(skb);
  494. } else {
  495. printk(KERN_WARNING DRV_MODULE_NAME
  496. ": %s Memory squeeze, dropping packet.\n",
  497. dev->name);
  498. fep->stats.rx_dropped++;
  499. skbn = skb;
  500. }
  501. }
  502. fep->rx_skbuff[curidx] = skbn;
  503. CBDW_BUFADDR(bdp, dma_map_single(NULL, skbn->data,
  504. L1_CACHE_ALIGN(PKT_MAXBUF_SIZE),
  505. DMA_FROM_DEVICE));
  506. CBDW_DATLEN(bdp, 0);
  507. CBDW_SC(bdp, (sc & ~BD_ENET_RX_STATS) | BD_ENET_RX_EMPTY);
  508. /*
  509. * Update BD pointer to next entry.
  510. */
  511. if ((sc & BD_ENET_RX_WRAP) == 0)
  512. bdp++;
  513. else
  514. bdp = fep->rx_bd_base;
  515. /*
  516. * Doing this here will keep the FEC running while we process
  517. * incoming frames. On a heavily loaded network, we should be
  518. * able to keep up at the expense of system resources.
  519. */
  520. FW(fecp, r_des_active, 0x01000000);
  521. }
  522. fep->cur_rx = bdp;
  523. if (fpi->use_napi) {
  524. dev->quota -= received;
  525. *budget -= received;
  526. if (rx_work_limit < 0)
  527. return 1; /* not done */
  528. /* done */
  529. netif_rx_complete(dev);
  530. /* enable RX interrupt bits */
  531. FS(fecp, imask, FEC_ENET_RXF | FEC_ENET_RXB);
  532. }
  533. return 0;
  534. }
  535. static void fec_enet_tx(struct net_device *dev)
  536. {
  537. struct fec_enet_private *fep = netdev_priv(dev);
  538. cbd_t *bdp;
  539. struct sk_buff *skb;
  540. int dirtyidx, do_wake;
  541. __u16 sc;
  542. spin_lock(&fep->lock);
  543. bdp = fep->dirty_tx;
  544. do_wake = 0;
  545. while (((sc = CBDR_SC(bdp)) & BD_ENET_TX_READY) == 0) {
  546. dirtyidx = bdp - fep->tx_bd_base;
  547. if (fep->tx_free == fep->tx_ring)
  548. break;
  549. skb = fep->tx_skbuff[dirtyidx];
  550. /*
  551. * Check for errors.
  552. */
  553. if (sc & (BD_ENET_TX_HB | BD_ENET_TX_LC |
  554. BD_ENET_TX_RL | BD_ENET_TX_UN | BD_ENET_TX_CSL)) {
  555. fep->stats.tx_errors++;
  556. if (sc & BD_ENET_TX_HB) /* No heartbeat */
  557. fep->stats.tx_heartbeat_errors++;
  558. if (sc & BD_ENET_TX_LC) /* Late collision */
  559. fep->stats.tx_window_errors++;
  560. if (sc & BD_ENET_TX_RL) /* Retrans limit */
  561. fep->stats.tx_aborted_errors++;
  562. if (sc & BD_ENET_TX_UN) /* Underrun */
  563. fep->stats.tx_fifo_errors++;
  564. if (sc & BD_ENET_TX_CSL) /* Carrier lost */
  565. fep->stats.tx_carrier_errors++;
  566. } else
  567. fep->stats.tx_packets++;
  568. if (sc & BD_ENET_TX_READY)
  569. printk(KERN_WARNING DRV_MODULE_NAME
  570. ": %s HEY! Enet xmit interrupt and TX_READY.\n",
  571. dev->name);
  572. /*
  573. * Deferred means some collisions occurred during transmit,
  574. * but we eventually sent the packet OK.
  575. */
  576. if (sc & BD_ENET_TX_DEF)
  577. fep->stats.collisions++;
  578. /*
  579. * Free the sk buffer associated with this last transmit.
  580. */
  581. dev_kfree_skb_irq(skb);
  582. fep->tx_skbuff[dirtyidx] = NULL;
  583. /*
  584. * Update pointer to next buffer descriptor to be transmitted.
  585. */
  586. if ((sc & BD_ENET_TX_WRAP) == 0)
  587. bdp++;
  588. else
  589. bdp = fep->tx_bd_base;
  590. /*
  591. * Since we have freed up a buffer, the ring is no longer
  592. * full.
  593. */
  594. if (!fep->tx_free++)
  595. do_wake = 1;
  596. }
  597. fep->dirty_tx = bdp;
  598. spin_unlock(&fep->lock);
  599. if (do_wake && netif_queue_stopped(dev))
  600. netif_wake_queue(dev);
  601. }
  602. /*
  603. * The interrupt handler.
  604. * This is called from the MPC core interrupt.
  605. */
  606. static irqreturn_t
  607. fec_enet_interrupt(int irq, void *dev_id)
  608. {
  609. struct net_device *dev = dev_id;
  610. struct fec_enet_private *fep;
  611. const struct fec_platform_info *fpi;
  612. fec_t *fecp;
  613. __u32 int_events;
  614. __u32 int_events_napi;
  615. if (unlikely(dev == NULL))
  616. return IRQ_NONE;
  617. fep = netdev_priv(dev);
  618. fecp = fep->fecp;
  619. fpi = fep->fpi;
  620. /*
  621. * Get the interrupt events that caused us to be here.
  622. */
  623. while ((int_events = FR(fecp, ievent) & FR(fecp, imask)) != 0) {
  624. if (!fpi->use_napi)
  625. FW(fecp, ievent, int_events);
  626. else {
  627. int_events_napi = int_events & ~(FEC_ENET_RXF | FEC_ENET_RXB);
  628. FW(fecp, ievent, int_events_napi);
  629. }
  630. if ((int_events & (FEC_ENET_HBERR | FEC_ENET_BABR |
  631. FEC_ENET_BABT | FEC_ENET_EBERR)) != 0)
  632. printk(KERN_WARNING DRV_MODULE_NAME
  633. ": %s FEC ERROR(s) 0x%x\n",
  634. dev->name, int_events);
  635. if ((int_events & FEC_ENET_RXF) != 0) {
  636. if (!fpi->use_napi)
  637. fec_enet_rx_common(dev, NULL);
  638. else {
  639. if (netif_rx_schedule_prep(dev)) {
  640. /* disable rx interrupts */
  641. FC(fecp, imask, FEC_ENET_RXF | FEC_ENET_RXB);
  642. __netif_rx_schedule(dev);
  643. } else {
  644. printk(KERN_ERR DRV_MODULE_NAME
  645. ": %s driver bug! interrupt while in poll!\n",
  646. dev->name);
  647. FC(fecp, imask, FEC_ENET_RXF | FEC_ENET_RXB);
  648. }
  649. }
  650. }
  651. if ((int_events & FEC_ENET_TXF) != 0)
  652. fec_enet_tx(dev);
  653. }
  654. return IRQ_HANDLED;
  655. }
  656. /* This interrupt occurs when the PHY detects a link change. */
  657. static irqreturn_t
  658. fec_mii_link_interrupt(int irq, void *dev_id)
  659. {
  660. struct net_device *dev = dev_id;
  661. struct fec_enet_private *fep;
  662. const struct fec_platform_info *fpi;
  663. if (unlikely(dev == NULL))
  664. return IRQ_NONE;
  665. fep = netdev_priv(dev);
  666. fpi = fep->fpi;
  667. if (!fpi->use_mdio)
  668. return IRQ_NONE;
  669. /*
  670. * Acknowledge the interrupt if possible. If we have not
  671. * found the PHY yet we can't process or acknowledge the
  672. * interrupt now. Instead we ignore this interrupt for now,
  673. * which we can do since it is edge triggered. It will be
  674. * acknowledged later by fec_enet_open().
  675. */
  676. if (!fep->phy)
  677. return IRQ_NONE;
  678. fec_mii_ack_int(dev);
  679. fec_mii_link_status_change_check(dev, 0);
  680. return IRQ_HANDLED;
  681. }
  682. /**********************************************************************************/
  683. static int fec_enet_start_xmit(struct sk_buff *skb, struct net_device *dev)
  684. {
  685. struct fec_enet_private *fep = netdev_priv(dev);
  686. fec_t *fecp = fep->fecp;
  687. cbd_t *bdp;
  688. int curidx;
  689. unsigned long flags;
  690. spin_lock_irqsave(&fep->tx_lock, flags);
  691. /*
  692. * Fill in a Tx ring entry
  693. */
  694. bdp = fep->cur_tx;
  695. if (!fep->tx_free || (CBDR_SC(bdp) & BD_ENET_TX_READY)) {
  696. netif_stop_queue(dev);
  697. spin_unlock_irqrestore(&fep->tx_lock, flags);
  698. /*
  699. * Ooops. All transmit buffers are full. Bail out.
  700. * This should not happen, since the tx queue should be stopped.
  701. */
  702. printk(KERN_WARNING DRV_MODULE_NAME
  703. ": %s tx queue full!.\n", dev->name);
  704. return 1;
  705. }
  706. curidx = bdp - fep->tx_bd_base;
  707. /*
  708. * Clear all of the status flags.
  709. */
  710. CBDC_SC(bdp, BD_ENET_TX_STATS);
  711. /*
  712. * Save skb pointer.
  713. */
  714. fep->tx_skbuff[curidx] = skb;
  715. fep->stats.tx_bytes += skb->len;
  716. /*
  717. * Push the data cache so the CPM does not get stale memory data.
  718. */
  719. CBDW_BUFADDR(bdp, dma_map_single(NULL, skb->data,
  720. skb->len, DMA_TO_DEVICE));
  721. CBDW_DATLEN(bdp, skb->len);
  722. dev->trans_start = jiffies;
  723. /*
  724. * If this was the last BD in the ring, start at the beginning again.
  725. */
  726. if ((CBDR_SC(bdp) & BD_ENET_TX_WRAP) == 0)
  727. fep->cur_tx++;
  728. else
  729. fep->cur_tx = fep->tx_bd_base;
  730. if (!--fep->tx_free)
  731. netif_stop_queue(dev);
  732. /*
  733. * Trigger transmission start
  734. */
  735. CBDS_SC(bdp, BD_ENET_TX_READY | BD_ENET_TX_INTR |
  736. BD_ENET_TX_LAST | BD_ENET_TX_TC);
  737. FW(fecp, x_des_active, 0x01000000);
  738. spin_unlock_irqrestore(&fep->tx_lock, flags);
  739. return 0;
  740. }
  741. static void fec_timeout(struct net_device *dev)
  742. {
  743. struct fec_enet_private *fep = netdev_priv(dev);
  744. fep->stats.tx_errors++;
  745. if (fep->tx_free)
  746. netif_wake_queue(dev);
  747. /* check link status again */
  748. fec_mii_link_status_change_check(dev, 0);
  749. }
  750. static int fec_enet_open(struct net_device *dev)
  751. {
  752. struct fec_enet_private *fep = netdev_priv(dev);
  753. const struct fec_platform_info *fpi = fep->fpi;
  754. unsigned long flags;
  755. /* Install our interrupt handler. */
  756. if (request_irq(fpi->fec_irq, fec_enet_interrupt, 0, "fec", dev) != 0) {
  757. printk(KERN_ERR DRV_MODULE_NAME
  758. ": %s Could not allocate FEC IRQ!", dev->name);
  759. return -EINVAL;
  760. }
  761. /* Install our phy interrupt handler */
  762. if (fpi->phy_irq != -1 &&
  763. request_irq(fpi->phy_irq, fec_mii_link_interrupt, 0, "fec-phy",
  764. dev) != 0) {
  765. printk(KERN_ERR DRV_MODULE_NAME
  766. ": %s Could not allocate PHY IRQ!", dev->name);
  767. free_irq(fpi->fec_irq, dev);
  768. return -EINVAL;
  769. }
  770. if (fpi->use_mdio) {
  771. fec_mii_startup(dev);
  772. netif_carrier_off(dev);
  773. fec_mii_link_status_change_check(dev, 1);
  774. } else {
  775. spin_lock_irqsave(&fep->lock, flags);
  776. fec_restart(dev, 1, 100); /* XXX this sucks */
  777. spin_unlock_irqrestore(&fep->lock, flags);
  778. netif_carrier_on(dev);
  779. netif_start_queue(dev);
  780. }
  781. return 0;
  782. }
  783. static int fec_enet_close(struct net_device *dev)
  784. {
  785. struct fec_enet_private *fep = netdev_priv(dev);
  786. const struct fec_platform_info *fpi = fep->fpi;
  787. unsigned long flags;
  788. netif_stop_queue(dev);
  789. netif_carrier_off(dev);
  790. if (fpi->use_mdio)
  791. fec_mii_shutdown(dev);
  792. spin_lock_irqsave(&fep->lock, flags);
  793. fec_stop(dev);
  794. spin_unlock_irqrestore(&fep->lock, flags);
  795. /* release any irqs */
  796. if (fpi->phy_irq != -1)
  797. free_irq(fpi->phy_irq, dev);
  798. free_irq(fpi->fec_irq, dev);
  799. return 0;
  800. }
  801. static struct net_device_stats *fec_enet_get_stats(struct net_device *dev)
  802. {
  803. struct fec_enet_private *fep = netdev_priv(dev);
  804. return &fep->stats;
  805. }
  806. static int fec_enet_poll(struct net_device *dev, int *budget)
  807. {
  808. return fec_enet_rx_common(dev, budget);
  809. }
  810. /*************************************************************************/
  811. static void fec_get_drvinfo(struct net_device *dev,
  812. struct ethtool_drvinfo *info)
  813. {
  814. strcpy(info->driver, DRV_MODULE_NAME);
  815. strcpy(info->version, DRV_MODULE_VERSION);
  816. }
  817. static int fec_get_regs_len(struct net_device *dev)
  818. {
  819. return sizeof(fec_t);
  820. }
  821. static void fec_get_regs(struct net_device *dev, struct ethtool_regs *regs,
  822. void *p)
  823. {
  824. struct fec_enet_private *fep = netdev_priv(dev);
  825. unsigned long flags;
  826. if (regs->len < sizeof(fec_t))
  827. return;
  828. regs->version = 0;
  829. spin_lock_irqsave(&fep->lock, flags);
  830. memcpy_fromio(p, fep->fecp, sizeof(fec_t));
  831. spin_unlock_irqrestore(&fep->lock, flags);
  832. }
  833. static int fec_get_settings(struct net_device *dev, struct ethtool_cmd *cmd)
  834. {
  835. struct fec_enet_private *fep = netdev_priv(dev);
  836. unsigned long flags;
  837. int rc;
  838. spin_lock_irqsave(&fep->lock, flags);
  839. rc = mii_ethtool_gset(&fep->mii_if, cmd);
  840. spin_unlock_irqrestore(&fep->lock, flags);
  841. return rc;
  842. }
  843. static int fec_set_settings(struct net_device *dev, struct ethtool_cmd *cmd)
  844. {
  845. struct fec_enet_private *fep = netdev_priv(dev);
  846. unsigned long flags;
  847. int rc;
  848. spin_lock_irqsave(&fep->lock, flags);
  849. rc = mii_ethtool_sset(&fep->mii_if, cmd);
  850. spin_unlock_irqrestore(&fep->lock, flags);
  851. return rc;
  852. }
  853. static int fec_nway_reset(struct net_device *dev)
  854. {
  855. struct fec_enet_private *fep = netdev_priv(dev);
  856. return mii_nway_restart(&fep->mii_if);
  857. }
  858. static __u32 fec_get_msglevel(struct net_device *dev)
  859. {
  860. struct fec_enet_private *fep = netdev_priv(dev);
  861. return fep->msg_enable;
  862. }
  863. static void fec_set_msglevel(struct net_device *dev, __u32 value)
  864. {
  865. struct fec_enet_private *fep = netdev_priv(dev);
  866. fep->msg_enable = value;
  867. }
  868. static const struct ethtool_ops fec_ethtool_ops = {
  869. .get_drvinfo = fec_get_drvinfo,
  870. .get_regs_len = fec_get_regs_len,
  871. .get_settings = fec_get_settings,
  872. .set_settings = fec_set_settings,
  873. .nway_reset = fec_nway_reset,
  874. .get_link = ethtool_op_get_link,
  875. .get_msglevel = fec_get_msglevel,
  876. .set_msglevel = fec_set_msglevel,
  877. .get_tx_csum = ethtool_op_get_tx_csum,
  878. .set_tx_csum = ethtool_op_set_tx_csum, /* local! */
  879. .get_sg = ethtool_op_get_sg,
  880. .set_sg = ethtool_op_set_sg,
  881. .get_regs = fec_get_regs,
  882. };
  883. static int fec_ioctl(struct net_device *dev, struct ifreq *rq, int cmd)
  884. {
  885. struct fec_enet_private *fep = netdev_priv(dev);
  886. struct mii_ioctl_data *mii = (struct mii_ioctl_data *)&rq->ifr_data;
  887. unsigned long flags;
  888. int rc;
  889. if (!netif_running(dev))
  890. return -EINVAL;
  891. spin_lock_irqsave(&fep->lock, flags);
  892. rc = generic_mii_ioctl(&fep->mii_if, mii, cmd, NULL);
  893. spin_unlock_irqrestore(&fep->lock, flags);
  894. return rc;
  895. }
  896. int fec_8xx_init_one(const struct fec_platform_info *fpi,
  897. struct net_device **devp)
  898. {
  899. immap_t *immap = (immap_t *) IMAP_ADDR;
  900. static int fec_8xx_version_printed = 0;
  901. struct net_device *dev = NULL;
  902. struct fec_enet_private *fep = NULL;
  903. fec_t *fecp = NULL;
  904. int i;
  905. int err = 0;
  906. int registered = 0;
  907. __u32 siel;
  908. *devp = NULL;
  909. switch (fpi->fec_no) {
  910. case 0:
  911. fecp = &((immap_t *) IMAP_ADDR)->im_cpm.cp_fec;
  912. break;
  913. #ifdef CONFIG_DUET
  914. case 1:
  915. fecp = &((immap_t *) IMAP_ADDR)->im_cpm.cp_fec2;
  916. break;
  917. #endif
  918. default:
  919. return -EINVAL;
  920. }
  921. if (fec_8xx_version_printed++ == 0)
  922. printk(KERN_INFO "%s", version);
  923. i = sizeof(*fep) + (sizeof(struct sk_buff **) *
  924. (fpi->rx_ring + fpi->tx_ring));
  925. dev = alloc_etherdev(i);
  926. if (!dev) {
  927. err = -ENOMEM;
  928. goto err;
  929. }
  930. SET_MODULE_OWNER(dev);
  931. fep = netdev_priv(dev);
  932. /* partial reset of FEC */
  933. fec_whack_reset(fecp);
  934. /* point rx_skbuff, tx_skbuff */
  935. fep->rx_skbuff = (struct sk_buff **)&fep[1];
  936. fep->tx_skbuff = fep->rx_skbuff + fpi->rx_ring;
  937. fep->fecp = fecp;
  938. fep->fpi = fpi;
  939. /* init locks */
  940. spin_lock_init(&fep->lock);
  941. spin_lock_init(&fep->tx_lock);
  942. /*
  943. * Set the Ethernet address.
  944. */
  945. for (i = 0; i < 6; i++)
  946. dev->dev_addr[i] = fpi->macaddr[i];
  947. fep->ring_base = dma_alloc_coherent(NULL,
  948. (fpi->tx_ring + fpi->rx_ring) *
  949. sizeof(cbd_t), &fep->ring_mem_addr,
  950. GFP_KERNEL);
  951. if (fep->ring_base == NULL) {
  952. printk(KERN_ERR DRV_MODULE_NAME
  953. ": %s dma alloc failed.\n", dev->name);
  954. err = -ENOMEM;
  955. goto err;
  956. }
  957. /*
  958. * Set receive and transmit descriptor base.
  959. */
  960. fep->rx_bd_base = fep->ring_base;
  961. fep->tx_bd_base = fep->rx_bd_base + fpi->rx_ring;
  962. /* initialize ring size variables */
  963. fep->tx_ring = fpi->tx_ring;
  964. fep->rx_ring = fpi->rx_ring;
  965. /* SIU interrupt */
  966. if (fpi->phy_irq != -1 &&
  967. (fpi->phy_irq >= SIU_IRQ0 && fpi->phy_irq < SIU_LEVEL7)) {
  968. siel = in_be32(&immap->im_siu_conf.sc_siel);
  969. if ((fpi->phy_irq & 1) == 0)
  970. siel |= (0x80000000 >> fpi->phy_irq);
  971. else
  972. siel &= ~(0x80000000 >> (fpi->phy_irq & ~1));
  973. out_be32(&immap->im_siu_conf.sc_siel, siel);
  974. }
  975. /*
  976. * The FEC Ethernet specific entries in the device structure.
  977. */
  978. dev->open = fec_enet_open;
  979. dev->hard_start_xmit = fec_enet_start_xmit;
  980. dev->tx_timeout = fec_timeout;
  981. dev->watchdog_timeo = TX_TIMEOUT;
  982. dev->stop = fec_enet_close;
  983. dev->get_stats = fec_enet_get_stats;
  984. dev->set_multicast_list = fec_set_multicast_list;
  985. dev->set_mac_address = fec_set_mac_address;
  986. if (fpi->use_napi) {
  987. dev->poll = fec_enet_poll;
  988. dev->weight = fpi->napi_weight;
  989. }
  990. dev->ethtool_ops = &fec_ethtool_ops;
  991. dev->do_ioctl = fec_ioctl;
  992. fep->fec_phy_speed =
  993. ((((fpi->sys_clk + 4999999) / 2500000) / 2) & 0x3F) << 1;
  994. init_timer(&fep->phy_timer_list);
  995. /* partial reset of FEC so that only MII works */
  996. FW(fecp, mii_speed, fep->fec_phy_speed);
  997. FW(fecp, ievent, 0xffc0);
  998. FW(fecp, ivec, (fpi->fec_irq / 2) << 29);
  999. FW(fecp, imask, 0);
  1000. FW(fecp, r_cntrl, FEC_RCNTRL_MII_MODE); /* MII enable */
  1001. FW(fecp, ecntrl, FEC_ECNTRL_PINMUX | FEC_ECNTRL_ETHER_EN);
  1002. netif_carrier_off(dev);
  1003. err = register_netdev(dev);
  1004. if (err != 0)
  1005. goto err;
  1006. registered = 1;
  1007. if (fpi->use_mdio) {
  1008. fep->mii_if.dev = dev;
  1009. fep->mii_if.mdio_read = fec_mii_read;
  1010. fep->mii_if.mdio_write = fec_mii_write;
  1011. fep->mii_if.phy_id_mask = 0x1f;
  1012. fep->mii_if.reg_num_mask = 0x1f;
  1013. fep->mii_if.phy_id = fec_mii_phy_id_detect(dev);
  1014. }
  1015. *devp = dev;
  1016. return 0;
  1017. err:
  1018. if (dev != NULL) {
  1019. if (fecp != NULL)
  1020. fec_whack_reset(fecp);
  1021. if (registered)
  1022. unregister_netdev(dev);
  1023. if (fep != NULL) {
  1024. if (fep->ring_base)
  1025. dma_free_coherent(NULL,
  1026. (fpi->tx_ring +
  1027. fpi->rx_ring) *
  1028. sizeof(cbd_t), fep->ring_base,
  1029. fep->ring_mem_addr);
  1030. }
  1031. free_netdev(dev);
  1032. }
  1033. return err;
  1034. }
  1035. int fec_8xx_cleanup_one(struct net_device *dev)
  1036. {
  1037. struct fec_enet_private *fep = netdev_priv(dev);
  1038. fec_t *fecp = fep->fecp;
  1039. const struct fec_platform_info *fpi = fep->fpi;
  1040. fec_whack_reset(fecp);
  1041. unregister_netdev(dev);
  1042. dma_free_coherent(NULL, (fpi->tx_ring + fpi->rx_ring) * sizeof(cbd_t),
  1043. fep->ring_base, fep->ring_mem_addr);
  1044. free_netdev(dev);
  1045. return 0;
  1046. }
  1047. /**************************************************************************************/
  1048. /**************************************************************************************/
  1049. /**************************************************************************************/
  1050. static int __init fec_8xx_init(void)
  1051. {
  1052. return fec_8xx_platform_init();
  1053. }
  1054. static void __exit fec_8xx_cleanup(void)
  1055. {
  1056. fec_8xx_platform_cleanup();
  1057. }
  1058. /**************************************************************************************/
  1059. /**************************************************************************************/
  1060. /**************************************************************************************/
  1061. module_init(fec_8xx_init);
  1062. module_exit(fec_8xx_cleanup);