ether_fcc.c 28 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147
  1. /*
  2. * MPC8260 FCC Fast Ethernet
  3. *
  4. * Copyright (c) 2000 MontaVista Software, Inc. Dan Malek (dmalek@jlc.net)
  5. *
  6. * (C) Copyright 2000 Sysgo Real-Time Solutions, GmbH <www.elinos.com>
  7. * Marius Groeger <mgroeger@sysgo.de>
  8. *
  9. * SPDX-License-Identifier: GPL-2.0+
  10. */
  11. /*
  12. * MPC8260 FCC Fast Ethernet
  13. * Basic ET HW initialization and packet RX/TX routines
  14. *
  15. * This code will not perform the IO port configuration. This should be
  16. * done in the iop_conf_t structure specific for the board.
  17. *
  18. * TODO:
  19. * add a PHY driver to do the negotiation
  20. * reflect negotiation results in FPSMR
  21. * look for ways to configure the board specific stuff elsewhere, eg.
  22. * config_xxx.h or the board directory
  23. */
  24. #include <common.h>
  25. #include <malloc.h>
  26. #include <asm/cpm_8260.h>
  27. #include <mpc8260.h>
  28. #include <command.h>
  29. #include <config.h>
  30. #include <net.h>
  31. #if defined(CONFIG_MII) || defined(CONFIG_CMD_MII)
  32. #include <miiphy.h>
  33. #endif
  34. DECLARE_GLOBAL_DATA_PTR;
  35. #if defined(CONFIG_ETHER_ON_FCC) && defined(CONFIG_CMD_NET)
  36. static struct ether_fcc_info_s
  37. {
  38. int ether_index;
  39. int proff_enet;
  40. ulong cpm_cr_enet_sblock;
  41. ulong cpm_cr_enet_page;
  42. ulong cmxfcr_mask;
  43. ulong cmxfcr_value;
  44. }
  45. ether_fcc_info[] =
  46. {
  47. #ifdef CONFIG_ETHER_ON_FCC1
  48. {
  49. 0,
  50. PROFF_FCC1,
  51. CPM_CR_FCC1_SBLOCK,
  52. CPM_CR_FCC1_PAGE,
  53. CONFIG_SYS_CMXFCR_MASK1,
  54. CONFIG_SYS_CMXFCR_VALUE1
  55. },
  56. #endif
  57. #ifdef CONFIG_ETHER_ON_FCC2
  58. {
  59. 1,
  60. PROFF_FCC2,
  61. CPM_CR_FCC2_SBLOCK,
  62. CPM_CR_FCC2_PAGE,
  63. CONFIG_SYS_CMXFCR_MASK2,
  64. CONFIG_SYS_CMXFCR_VALUE2
  65. },
  66. #endif
  67. #ifdef CONFIG_ETHER_ON_FCC3
  68. {
  69. 2,
  70. PROFF_FCC3,
  71. CPM_CR_FCC3_SBLOCK,
  72. CPM_CR_FCC3_PAGE,
  73. CONFIG_SYS_CMXFCR_MASK3,
  74. CONFIG_SYS_CMXFCR_VALUE3
  75. },
  76. #endif
  77. };
  78. /*---------------------------------------------------------------------*/
  79. /* Maximum input DMA size. Must be a should(?) be a multiple of 4. */
  80. #define PKT_MAXDMA_SIZE 1520
  81. /* The FCC stores dest/src/type, data, and checksum for receive packets. */
  82. #define PKT_MAXBUF_SIZE 1518
  83. #define PKT_MINBUF_SIZE 64
  84. /* Maximum input buffer size. Must be a multiple of 32. */
  85. #define PKT_MAXBLR_SIZE 1536
  86. #define TOUT_LOOP 1000000
  87. #define TX_BUF_CNT 2
  88. #ifdef __GNUC__
  89. static char txbuf[TX_BUF_CNT][PKT_MAXBLR_SIZE] __attribute__ ((aligned(8)));
  90. #else
  91. #error "txbuf must be 64-bit aligned"
  92. #endif
  93. static uint rxIdx; /* index of the current RX buffer */
  94. static uint txIdx; /* index of the current TX buffer */
  95. /*
  96. * FCC Ethernet Tx and Rx buffer descriptors.
  97. * Provide for Double Buffering
  98. * Note: PKTBUFSRX is defined in net.h
  99. */
  100. typedef volatile struct rtxbd {
  101. cbd_t rxbd[PKTBUFSRX];
  102. cbd_t txbd[TX_BUF_CNT];
  103. } RTXBD;
  104. /* Good news: the FCC supports external BDs! */
  105. #ifdef __GNUC__
  106. static RTXBD rtx __attribute__ ((aligned(8)));
  107. #else
  108. #error "rtx must be 64-bit aligned"
  109. #endif
  110. static int fec_send(struct eth_device *dev, void *packet, int length)
  111. {
  112. int i;
  113. int result = 0;
  114. if (length <= 0) {
  115. printf("fec: bad packet size: %d\n", length);
  116. goto out;
  117. }
  118. for(i=0; rtx.txbd[txIdx].cbd_sc & BD_ENET_TX_READY; i++) {
  119. if (i >= TOUT_LOOP) {
  120. puts ("fec: tx buffer not ready\n");
  121. goto out;
  122. }
  123. }
  124. rtx.txbd[txIdx].cbd_bufaddr = (uint)packet;
  125. rtx.txbd[txIdx].cbd_datlen = length;
  126. rtx.txbd[txIdx].cbd_sc |= (BD_ENET_TX_READY | BD_ENET_TX_LAST |
  127. BD_ENET_TX_WRAP);
  128. for(i=0; rtx.txbd[txIdx].cbd_sc & BD_ENET_TX_READY; i++) {
  129. if (i >= TOUT_LOOP) {
  130. puts ("fec: tx error\n");
  131. goto out;
  132. }
  133. }
  134. #ifdef ET_DEBUG
  135. printf("cycles: %d status: %04x\n", i, rtx.txbd[txIdx].cbd_sc);
  136. #endif
  137. /* return only status bits */
  138. result = rtx.txbd[txIdx].cbd_sc & BD_ENET_TX_STATS;
  139. out:
  140. return result;
  141. }
  142. static int fec_recv(struct eth_device* dev)
  143. {
  144. int length;
  145. for (;;)
  146. {
  147. if (rtx.rxbd[rxIdx].cbd_sc & BD_ENET_RX_EMPTY) {
  148. length = -1;
  149. break; /* nothing received - leave for() loop */
  150. }
  151. length = rtx.rxbd[rxIdx].cbd_datlen;
  152. if (rtx.rxbd[rxIdx].cbd_sc & 0x003f) {
  153. printf("fec: rx error %04x\n", rtx.rxbd[rxIdx].cbd_sc);
  154. }
  155. else {
  156. /* Pass the packet up to the protocol layers. */
  157. NetReceive(NetRxPackets[rxIdx], length - 4);
  158. }
  159. /* Give the buffer back to the FCC. */
  160. rtx.rxbd[rxIdx].cbd_datlen = 0;
  161. /* wrap around buffer index when necessary */
  162. if ((rxIdx + 1) >= PKTBUFSRX) {
  163. rtx.rxbd[PKTBUFSRX - 1].cbd_sc = (BD_ENET_RX_WRAP | BD_ENET_RX_EMPTY);
  164. rxIdx = 0;
  165. }
  166. else {
  167. rtx.rxbd[rxIdx].cbd_sc = BD_ENET_RX_EMPTY;
  168. rxIdx++;
  169. }
  170. }
  171. return length;
  172. }
  173. static int fec_init(struct eth_device* dev, bd_t *bis)
  174. {
  175. struct ether_fcc_info_s * info = dev->priv;
  176. int i;
  177. volatile immap_t *immr = (immap_t *)CONFIG_SYS_IMMR;
  178. volatile cpm8260_t *cp = &(immr->im_cpm);
  179. fcc_enet_t *pram_ptr;
  180. unsigned long mem_addr;
  181. #if 0
  182. mii_discover_phy();
  183. #endif
  184. /* 28.9 - (1-2): ioports have been set up already */
  185. /* 28.9 - (3): connect FCC's tx and rx clocks */
  186. immr->im_cpmux.cmx_uar = 0;
  187. immr->im_cpmux.cmx_fcr = (immr->im_cpmux.cmx_fcr & ~info->cmxfcr_mask) |
  188. info->cmxfcr_value;
  189. /* 28.9 - (4): GFMR: disable tx/rx, CCITT CRC, Mode Ethernet */
  190. immr->im_fcc[info->ether_index].fcc_gfmr =
  191. FCC_GFMR_MODE_ENET | FCC_GFMR_TCRC_32;
  192. /* 28.9 - (5): FPSMR: enable full duplex, select CCITT CRC for Ethernet */
  193. immr->im_fcc[info->ether_index].fcc_fpsmr = CONFIG_SYS_FCC_PSMR | FCC_PSMR_ENCRC;
  194. /* 28.9 - (6): FDSR: Ethernet Syn */
  195. immr->im_fcc[info->ether_index].fcc_fdsr = 0xD555;
  196. /* reset indeces to current rx/tx bd (see eth_send()/eth_rx()) */
  197. rxIdx = 0;
  198. txIdx = 0;
  199. /* Setup Receiver Buffer Descriptors */
  200. for (i = 0; i < PKTBUFSRX; i++)
  201. {
  202. rtx.rxbd[i].cbd_sc = BD_ENET_RX_EMPTY;
  203. rtx.rxbd[i].cbd_datlen = 0;
  204. rtx.rxbd[i].cbd_bufaddr = (uint)NetRxPackets[i];
  205. }
  206. rtx.rxbd[PKTBUFSRX - 1].cbd_sc |= BD_ENET_RX_WRAP;
  207. /* Setup Ethernet Transmitter Buffer Descriptors */
  208. for (i = 0; i < TX_BUF_CNT; i++)
  209. {
  210. rtx.txbd[i].cbd_sc = (BD_ENET_TX_PAD | BD_ENET_TX_LAST | BD_ENET_TX_TC);
  211. rtx.txbd[i].cbd_datlen = 0;
  212. rtx.txbd[i].cbd_bufaddr = (uint)&txbuf[i][0];
  213. }
  214. rtx.txbd[TX_BUF_CNT - 1].cbd_sc |= BD_ENET_TX_WRAP;
  215. /* 28.9 - (7): initialise parameter ram */
  216. pram_ptr = (fcc_enet_t *)&(immr->im_dprambase[info->proff_enet]);
  217. /* clear whole structure to make sure all reserved fields are zero */
  218. memset((void*)pram_ptr, 0, sizeof(fcc_enet_t));
  219. /*
  220. * common Parameter RAM area
  221. *
  222. * Allocate space in the reserved FCC area of DPRAM for the
  223. * internal buffers. No one uses this space (yet), so we
  224. * can do this. Later, we will add resource management for
  225. * this area.
  226. */
  227. mem_addr = CPM_FCC_SPECIAL_BASE + ((info->ether_index) * 64);
  228. pram_ptr->fen_genfcc.fcc_riptr = mem_addr;
  229. pram_ptr->fen_genfcc.fcc_tiptr = mem_addr+32;
  230. /*
  231. * Set maximum bytes per receive buffer.
  232. * It must be a multiple of 32.
  233. */
  234. pram_ptr->fen_genfcc.fcc_mrblr = PKT_MAXBLR_SIZE;
  235. pram_ptr->fen_genfcc.fcc_rstate = (CPMFCR_GBL | CPMFCR_EB |
  236. CONFIG_SYS_CPMFCR_RAMTYPE) << 24;
  237. pram_ptr->fen_genfcc.fcc_rbase = (unsigned int)(&rtx.rxbd[rxIdx]);
  238. pram_ptr->fen_genfcc.fcc_tstate = (CPMFCR_GBL | CPMFCR_EB |
  239. CONFIG_SYS_CPMFCR_RAMTYPE) << 24;
  240. pram_ptr->fen_genfcc.fcc_tbase = (unsigned int)(&rtx.txbd[txIdx]);
  241. /* protocol-specific area */
  242. pram_ptr->fen_cmask = 0xdebb20e3; /* CRC mask */
  243. pram_ptr->fen_cpres = 0xffffffff; /* CRC preset */
  244. pram_ptr->fen_retlim = 15; /* Retry limit threshold */
  245. pram_ptr->fen_mflr = PKT_MAXBUF_SIZE; /* maximum frame length register */
  246. /*
  247. * Set Ethernet station address.
  248. *
  249. * This is supplied in the board information structure, so we
  250. * copy that into the controller.
  251. * So, far we have only been given one Ethernet address. We make
  252. * it unique by setting a few bits in the upper byte of the
  253. * non-static part of the address.
  254. */
  255. #define ea eth_get_dev()->enetaddr
  256. pram_ptr->fen_paddrh = (ea[5] << 8) + ea[4];
  257. pram_ptr->fen_paddrm = (ea[3] << 8) + ea[2];
  258. pram_ptr->fen_paddrl = (ea[1] << 8) + ea[0];
  259. #undef ea
  260. pram_ptr->fen_minflr = PKT_MINBUF_SIZE; /* minimum frame length register */
  261. /* pad pointer. use tiptr since we don't need a specific padding char */
  262. pram_ptr->fen_padptr = pram_ptr->fen_genfcc.fcc_tiptr;
  263. pram_ptr->fen_maxd1 = PKT_MAXDMA_SIZE; /* maximum DMA1 length */
  264. pram_ptr->fen_maxd2 = PKT_MAXDMA_SIZE; /* maximum DMA2 length */
  265. pram_ptr->fen_rfthr = 1;
  266. pram_ptr->fen_rfcnt = 1;
  267. #if 0
  268. printf("pram_ptr->fen_genfcc.fcc_rbase %08lx\n",
  269. pram_ptr->fen_genfcc.fcc_rbase);
  270. printf("pram_ptr->fen_genfcc.fcc_tbase %08lx\n",
  271. pram_ptr->fen_genfcc.fcc_tbase);
  272. #endif
  273. /* 28.9 - (8): clear out events in FCCE */
  274. immr->im_fcc[info->ether_index].fcc_fcce = ~0x0;
  275. /* 28.9 - (9): FCCM: mask all events */
  276. immr->im_fcc[info->ether_index].fcc_fccm = 0;
  277. /* 28.9 - (10-12): we don't use ethernet interrupts */
  278. /* 28.9 - (13)
  279. *
  280. * Let's re-initialize the channel now. We have to do it later
  281. * than the manual describes because we have just now finished
  282. * the BD initialization.
  283. */
  284. cp->cp_cpcr = mk_cr_cmd(info->cpm_cr_enet_page,
  285. info->cpm_cr_enet_sblock,
  286. 0x0c,
  287. CPM_CR_INIT_TRX) | CPM_CR_FLG;
  288. do {
  289. __asm__ __volatile__ ("eieio");
  290. } while (cp->cp_cpcr & CPM_CR_FLG);
  291. /* 28.9 - (14): enable tx/rx in gfmr */
  292. immr->im_fcc[info->ether_index].fcc_gfmr |= FCC_GFMR_ENT | FCC_GFMR_ENR;
  293. return 1;
  294. }
  295. static void fec_halt(struct eth_device* dev)
  296. {
  297. struct ether_fcc_info_s * info = dev->priv;
  298. volatile immap_t *immr = (immap_t *)CONFIG_SYS_IMMR;
  299. /* write GFMR: disable tx/rx */
  300. immr->im_fcc[info->ether_index].fcc_gfmr &=
  301. ~(FCC_GFMR_ENT | FCC_GFMR_ENR);
  302. }
  303. int fec_initialize(bd_t *bis)
  304. {
  305. struct eth_device* dev;
  306. int i;
  307. for (i = 0; i < sizeof(ether_fcc_info) / sizeof(ether_fcc_info[0]); i++)
  308. {
  309. dev = (struct eth_device*) malloc(sizeof *dev);
  310. memset(dev, 0, sizeof *dev);
  311. sprintf(dev->name, "FCC%d",
  312. ether_fcc_info[i].ether_index + 1);
  313. dev->priv = &ether_fcc_info[i];
  314. dev->init = fec_init;
  315. dev->halt = fec_halt;
  316. dev->send = fec_send;
  317. dev->recv = fec_recv;
  318. eth_register(dev);
  319. #if (defined(CONFIG_MII) || defined(CONFIG_CMD_MII)) \
  320. && defined(CONFIG_BITBANGMII)
  321. miiphy_register(dev->name,
  322. bb_miiphy_read, bb_miiphy_write);
  323. #endif
  324. }
  325. return 1;
  326. }
  327. #ifdef CONFIG_ETHER_LOOPBACK_TEST
  328. #define ELBT_BUFSZ 1024 /* must be multiple of 32 */
  329. #define ELBT_CRCSZ 4
  330. #define ELBT_NRXBD 4 /* must be at least 2 */
  331. #define ELBT_NTXBD 4
  332. #define ELBT_MAXRXERR 32
  333. #define ELBT_MAXTXERR 32
  334. #define ELBT_CLSWAIT 1000 /* msec to wait for further input frames */
  335. typedef
  336. struct {
  337. uint off;
  338. char *lab;
  339. }
  340. elbt_prdesc;
  341. typedef
  342. struct {
  343. uint _l, _f, m, bc, mc, lg, no, sh, cr, ov, cl;
  344. uint badsrc, badtyp, badlen, badbit;
  345. }
  346. elbt_rxeacc;
  347. static elbt_prdesc rxeacc_descs[] = {
  348. { offsetof(elbt_rxeacc, _l), "Not Last in Frame" },
  349. { offsetof(elbt_rxeacc, _f), "Not First in Frame" },
  350. { offsetof(elbt_rxeacc, m), "Address Miss" },
  351. { offsetof(elbt_rxeacc, bc), "Broadcast Address" },
  352. { offsetof(elbt_rxeacc, mc), "Multicast Address" },
  353. { offsetof(elbt_rxeacc, lg), "Frame Length Violation"},
  354. { offsetof(elbt_rxeacc, no), "Non-Octet Alignment" },
  355. { offsetof(elbt_rxeacc, sh), "Short Frame" },
  356. { offsetof(elbt_rxeacc, cr), "CRC Error" },
  357. { offsetof(elbt_rxeacc, ov), "Overrun" },
  358. { offsetof(elbt_rxeacc, cl), "Collision" },
  359. { offsetof(elbt_rxeacc, badsrc), "Bad Src Address" },
  360. { offsetof(elbt_rxeacc, badtyp), "Bad Frame Type" },
  361. { offsetof(elbt_rxeacc, badlen), "Bad Frame Length" },
  362. { offsetof(elbt_rxeacc, badbit), "Data Compare Errors" },
  363. };
  364. static int rxeacc_ndesc = sizeof (rxeacc_descs) / sizeof (rxeacc_descs[0]);
  365. typedef
  366. struct {
  367. uint def, hb, lc, rl, rc, un, csl;
  368. }
  369. elbt_txeacc;
  370. static elbt_prdesc txeacc_descs[] = {
  371. { offsetof(elbt_txeacc, def), "Defer Indication" },
  372. { offsetof(elbt_txeacc, hb), "Heartbeat" },
  373. { offsetof(elbt_txeacc, lc), "Late Collision" },
  374. { offsetof(elbt_txeacc, rl), "Retransmission Limit" },
  375. { offsetof(elbt_txeacc, rc), "Retry Count" },
  376. { offsetof(elbt_txeacc, un), "Underrun" },
  377. { offsetof(elbt_txeacc, csl), "Carrier Sense Lost" },
  378. };
  379. static int txeacc_ndesc = sizeof (txeacc_descs) / sizeof (txeacc_descs[0]);
  380. typedef
  381. struct {
  382. uchar rxbufs[ELBT_NRXBD][ELBT_BUFSZ];
  383. uchar txbufs[ELBT_NTXBD][ELBT_BUFSZ];
  384. cbd_t rxbd[ELBT_NRXBD];
  385. cbd_t txbd[ELBT_NTXBD];
  386. enum { Idle, Running, Closing, Closed } state;
  387. int proff, page, sblock;
  388. uint clstime, nsent, ntxerr, nrcvd, nrxerr;
  389. ushort rxerrs[ELBT_MAXRXERR], txerrs[ELBT_MAXTXERR];
  390. elbt_rxeacc rxeacc;
  391. elbt_txeacc txeacc;
  392. } __attribute__ ((aligned(8)))
  393. elbt_chan;
  394. static uchar patbytes[ELBT_NTXBD] = {
  395. 0xff, 0xaa, 0x55, 0x00
  396. };
  397. static uint patwords[ELBT_NTXBD] = {
  398. 0xffffffff, 0xaaaaaaaa, 0x55555555, 0x00000000
  399. };
  400. #ifdef __GNUC__
  401. static elbt_chan elbt_chans[3] __attribute__ ((aligned(8)));
  402. #else
  403. #error "elbt_chans must be 64-bit aligned"
  404. #endif
  405. #define CPM_CR_GRACEFUL_STOP_TX ((ushort)0x0005)
  406. static elbt_prdesc epram_descs[] = {
  407. { offsetof(fcc_enet_t, fen_crcec), "CRC Errors" },
  408. { offsetof(fcc_enet_t, fen_alec), "Alignment Errors" },
  409. { offsetof(fcc_enet_t, fen_disfc), "Discarded Frames" },
  410. { offsetof(fcc_enet_t, fen_octc), "Octets" },
  411. { offsetof(fcc_enet_t, fen_colc), "Collisions" },
  412. { offsetof(fcc_enet_t, fen_broc), "Broadcast Frames" },
  413. { offsetof(fcc_enet_t, fen_mulc), "Multicast Frames" },
  414. { offsetof(fcc_enet_t, fen_uspc), "Undersize Frames" },
  415. { offsetof(fcc_enet_t, fen_frgc), "Fragments" },
  416. { offsetof(fcc_enet_t, fen_ospc), "Oversize Frames" },
  417. { offsetof(fcc_enet_t, fen_jbrc), "Jabbers" },
  418. { offsetof(fcc_enet_t, fen_p64c), "64 Octet Frames" },
  419. { offsetof(fcc_enet_t, fen_p65c), "65-127 Octet Frames" },
  420. { offsetof(fcc_enet_t, fen_p128c), "128-255 Octet Frames" },
  421. { offsetof(fcc_enet_t, fen_p256c), "256-511 Octet Frames" },
  422. { offsetof(fcc_enet_t, fen_p512c), "512-1023 Octet Frames" },
  423. { offsetof(fcc_enet_t, fen_p1024c), "1024-1518 Octet Frames"},
  424. };
  425. static int epram_ndesc = sizeof (epram_descs) / sizeof (epram_descs[0]);
  426. /*
  427. * given an elbt_prdesc array and an array of base addresses, print
  428. * each prdesc down the screen with the values fetched from each
  429. * base address across the screen
  430. */
  431. static void
  432. print_desc (elbt_prdesc descs[], int ndesc, uchar *bases[], int nbase)
  433. {
  434. elbt_prdesc *dp = descs, *edp = dp + ndesc;
  435. int i;
  436. printf ("%32s", "");
  437. for (i = 0; i < nbase; i++)
  438. printf (" Channel %d", i);
  439. putc ('\n');
  440. while (dp < edp) {
  441. printf ("%-32s", dp->lab);
  442. for (i = 0; i < nbase; i++) {
  443. uint val = *(uint *)(bases[i] + dp->off);
  444. printf (" %10u", val);
  445. }
  446. putc ('\n');
  447. dp++;
  448. }
  449. }
  450. /*
  451. * return number of bits that are set in a value; value contains
  452. * nbits (right-justified) bits.
  453. */
  454. static uint __inline__
  455. nbs (uint value, uint nbits)
  456. {
  457. uint cnt = 0;
  458. #if 1
  459. uint pos = sizeof (uint) * 8;
  460. __asm__ __volatile__ ("\
  461. mtctr %2\n\
  462. 1: rlwnm. %2,%1,%4,31,31\n\
  463. beq 2f\n\
  464. addi %0,%0,1\n\
  465. 2: subi %4,%4,1\n\
  466. bdnz 1b"
  467. : "=r"(cnt)
  468. : "r"(value), "r"(nbits), "r"(cnt), "r"(pos)
  469. : "ctr", "cc" );
  470. #else
  471. uint mask = 1;
  472. do {
  473. if (value & mask)
  474. cnt++;
  475. mask <<= 1;
  476. } while (--nbits);
  477. #endif
  478. return (cnt);
  479. }
  480. static ulong
  481. badbits (uchar *bp, int n, ulong pat)
  482. {
  483. ulong *lp, cnt = 0;
  484. int nl;
  485. while (n > 0 && ((ulong)bp & (sizeof (ulong) - 1)) != 0) {
  486. uchar diff;
  487. diff = *bp++ ^ (uchar)pat;
  488. if (diff)
  489. cnt += nbs ((ulong)diff, 8);
  490. n--;
  491. }
  492. lp = (ulong *)bp;
  493. nl = n / sizeof (ulong);
  494. n -= nl * sizeof (ulong);
  495. while (nl > 0) {
  496. ulong diff;
  497. diff = *lp++ ^ pat;
  498. if (diff)
  499. cnt += nbs (diff, 32);
  500. nl--;
  501. }
  502. bp = (uchar *)lp;
  503. while (n > 0) {
  504. uchar diff;
  505. diff = *bp++ ^ (uchar)pat;
  506. if (diff)
  507. cnt += nbs ((ulong)diff, 8);
  508. n--;
  509. }
  510. return (cnt);
  511. }
  512. static inline unsigned short
  513. swap16 (unsigned short x)
  514. {
  515. return (((x & 0xff) << 8) | ((x & 0xff00) >> 8));
  516. }
  517. /* broadcast is not an error - we send them like that */
  518. #define BD_ENET_RX_ERRS (BD_ENET_RX_STATS & ~BD_ENET_RX_BC)
  519. void
  520. eth_loopback_test (void)
  521. {
  522. volatile immap_t *immr = (immap_t *)CONFIG_SYS_IMMR;
  523. volatile cpm8260_t *cp = &(immr->im_cpm);
  524. int c, nclosed;
  525. ulong runtime, nmsec;
  526. uchar *bases[3];
  527. puts ("FCC Ethernet External loopback test\n");
  528. eth_getenv_enetaddr("ethaddr", NetOurEther);
  529. /*
  530. * global initialisations for all FCC channels
  531. */
  532. /* 28.9 - (1-2): ioports have been set up already */
  533. #if defined(CONFIG_SACSng)
  534. /*
  535. * Attention: this is board-specific
  536. * 1, FCC2
  537. */
  538. # define FCC_START_LOOP 1
  539. # define FCC_END_LOOP 1
  540. /*
  541. * Attention: this is board-specific
  542. * - FCC2 Rx-CLK is CLK13
  543. * - FCC2 Tx-CLK is CLK14
  544. */
  545. /* 28.9 - (3): connect FCC's tx and rx clocks */
  546. immr->im_cpmux.cmx_uar = 0;
  547. immr->im_cpmux.cmx_fcr = CMXFCR_RF2CS_CLK13|CMXFCR_TF2CS_CLK14;
  548. #else
  549. #error "eth_loopback_test not supported on your board"
  550. #endif
  551. puts ("Initialise FCC channels:");
  552. for (c = FCC_START_LOOP; c <= FCC_END_LOOP; c++) {
  553. elbt_chan *ecp = &elbt_chans[c];
  554. volatile fcc_t *fcp = &immr->im_fcc[c];
  555. volatile fcc_enet_t *fpp;
  556. int i;
  557. ulong addr;
  558. /*
  559. * initialise channel data
  560. */
  561. printf (" %d", c);
  562. memset ((void *)ecp, 0, sizeof (*ecp));
  563. ecp->state = Idle;
  564. switch (c) {
  565. case 0: /* FCC1 */
  566. ecp->proff = PROFF_FCC1;
  567. ecp->page = CPM_CR_FCC1_PAGE;
  568. ecp->sblock = CPM_CR_FCC1_SBLOCK;
  569. break;
  570. case 1: /* FCC2 */
  571. ecp->proff = PROFF_FCC2;
  572. ecp->page = CPM_CR_FCC2_PAGE;
  573. ecp->sblock = CPM_CR_FCC2_SBLOCK;
  574. break;
  575. case 2: /* FCC3 */
  576. ecp->proff = PROFF_FCC3;
  577. ecp->page = CPM_CR_FCC3_PAGE;
  578. ecp->sblock = CPM_CR_FCC3_SBLOCK;
  579. break;
  580. }
  581. /*
  582. * set up tx buffers and bds
  583. */
  584. for (i = 0; i < ELBT_NTXBD; i++) {
  585. cbd_t *bdp = &ecp->txbd[i];
  586. uchar *bp = &ecp->txbufs[i][0];
  587. bdp->cbd_bufaddr = (uint)bp;
  588. /* room for crc */
  589. bdp->cbd_datlen = ELBT_BUFSZ - ELBT_CRCSZ;
  590. bdp->cbd_sc = BD_ENET_TX_READY | BD_ENET_TX_PAD | \
  591. BD_ENET_TX_LAST | BD_ENET_TX_TC;
  592. memset ((void *)bp, patbytes[i], ELBT_BUFSZ);
  593. NetSetEther (bp, NetBcastAddr, 0x8000);
  594. }
  595. ecp->txbd[ELBT_NTXBD - 1].cbd_sc |= BD_ENET_TX_WRAP;
  596. /*
  597. * set up rx buffers and bds
  598. */
  599. for (i = 0; i < ELBT_NRXBD; i++) {
  600. cbd_t *bdp = &ecp->rxbd[i];
  601. uchar *bp = &ecp->rxbufs[i][0];
  602. bdp->cbd_bufaddr = (uint)bp;
  603. bdp->cbd_datlen = 0;
  604. bdp->cbd_sc = BD_ENET_RX_EMPTY;
  605. memset ((void *)bp, 0, ELBT_BUFSZ);
  606. }
  607. ecp->rxbd[ELBT_NRXBD - 1].cbd_sc |= BD_ENET_RX_WRAP;
  608. /*
  609. * set up the FCC channel hardware
  610. */
  611. /* 28.9 - (4): GFMR: disable tx/rx, CCITT CRC, Mode Ethernet */
  612. fcp->fcc_gfmr = FCC_GFMR_MODE_ENET | FCC_GFMR_TCRC_32;
  613. /* 28.9 - (5): FPSMR: fd, enet CRC, Promis, RMON, Rx SHort */
  614. fcp->fcc_fpsmr = FCC_PSMR_FDE | FCC_PSMR_LPB | \
  615. FCC_PSMR_ENCRC | FCC_PSMR_PRO | \
  616. FCC_PSMR_MON | FCC_PSMR_RSH;
  617. /* 28.9 - (6): FDSR: Ethernet Syn */
  618. fcp->fcc_fdsr = 0xD555;
  619. /* 29.9 - (7): initialise parameter ram */
  620. fpp = (fcc_enet_t *)&(immr->im_dprambase[ecp->proff]);
  621. /* clear whole struct to make sure all resv fields are zero */
  622. memset ((void *)fpp, 0, sizeof (fcc_enet_t));
  623. /*
  624. * common Parameter RAM area
  625. *
  626. * Allocate space in the reserved FCC area of DPRAM for the
  627. * internal buffers. No one uses this space (yet), so we
  628. * can do this. Later, we will add resource management for
  629. * this area.
  630. */
  631. addr = CPM_FCC_SPECIAL_BASE + (c * 64);
  632. fpp->fen_genfcc.fcc_riptr = addr;
  633. fpp->fen_genfcc.fcc_tiptr = addr + 32;
  634. /*
  635. * Set maximum bytes per receive buffer.
  636. * It must be a multiple of 32.
  637. * buffers are in 60x bus memory.
  638. */
  639. fpp->fen_genfcc.fcc_mrblr = PKT_MAXBLR_SIZE;
  640. fpp->fen_genfcc.fcc_rstate = (CPMFCR_GBL | CPMFCR_EB) << 24;
  641. fpp->fen_genfcc.fcc_rbase = (unsigned int)(&ecp->rxbd[0]);
  642. fpp->fen_genfcc.fcc_tstate = (CPMFCR_GBL | CPMFCR_EB) << 24;
  643. fpp->fen_genfcc.fcc_tbase = (unsigned int)(&ecp->txbd[0]);
  644. /* protocol-specific area */
  645. fpp->fen_cmask = 0xdebb20e3; /* CRC mask */
  646. fpp->fen_cpres = 0xffffffff; /* CRC preset */
  647. fpp->fen_retlim = 15; /* Retry limit threshold */
  648. fpp->fen_mflr = PKT_MAXBUF_SIZE;/* max frame length register */
  649. /*
  650. * Set Ethernet station address.
  651. *
  652. * This is supplied in the board information structure, so we
  653. * copy that into the controller.
  654. * So, far we have only been given one Ethernet address. We use
  655. * the same address for all channels
  656. */
  657. #define ea NetOurEther
  658. fpp->fen_paddrh = (ea[5] << 8) + ea[4];
  659. fpp->fen_paddrm = (ea[3] << 8) + ea[2];
  660. fpp->fen_paddrl = (ea[1] << 8) + ea[0];
  661. #undef ea
  662. fpp->fen_minflr = PKT_MINBUF_SIZE; /* min frame len register */
  663. /*
  664. * pad pointer. use tiptr since we don't need
  665. * a specific padding char
  666. */
  667. fpp->fen_padptr = fpp->fen_genfcc.fcc_tiptr;
  668. fpp->fen_maxd1 = PKT_MAXDMA_SIZE; /* max DMA1 length */
  669. fpp->fen_maxd2 = PKT_MAXDMA_SIZE; /* max DMA2 length */
  670. fpp->fen_rfthr = 1;
  671. fpp->fen_rfcnt = 1;
  672. /* 28.9 - (8): clear out events in FCCE */
  673. fcp->fcc_fcce = ~0x0;
  674. /* 28.9 - (9): FCCM: mask all events */
  675. fcp->fcc_fccm = 0;
  676. /* 28.9 - (10-12): we don't use ethernet interrupts */
  677. /* 28.9 - (13)
  678. *
  679. * Let's re-initialize the channel now. We have to do it later
  680. * than the manual describes because we have just now finished
  681. * the BD initialization.
  682. */
  683. cp->cp_cpcr = mk_cr_cmd (ecp->page, ecp->sblock, \
  684. 0x0c, CPM_CR_INIT_TRX) | CPM_CR_FLG;
  685. do {
  686. __asm__ __volatile__ ("eieio");
  687. } while (cp->cp_cpcr & CPM_CR_FLG);
  688. }
  689. puts (" done\nStarting test... (Ctrl-C to Finish)\n");
  690. /*
  691. * Note: don't want serial output from here until the end of the
  692. * test - the delays would probably stuff things up.
  693. */
  694. clear_ctrlc ();
  695. runtime = get_timer (0);
  696. do {
  697. nclosed = 0;
  698. for (c = FCC_START_LOOP; c <= FCC_END_LOOP; c++) {
  699. volatile fcc_t *fcp = &immr->im_fcc[c];
  700. elbt_chan *ecp = &elbt_chans[c];
  701. int i;
  702. switch (ecp->state) {
  703. case Idle:
  704. /*
  705. * set the channel Running ...
  706. */
  707. /* 28.9 - (14): enable tx/rx in gfmr */
  708. fcp->fcc_gfmr |= FCC_GFMR_ENT | FCC_GFMR_ENR;
  709. ecp->state = Running;
  710. break;
  711. case Running:
  712. /*
  713. * (while Running only) check for
  714. * termination of the test
  715. */
  716. (void)ctrlc ();
  717. if (had_ctrlc ()) {
  718. /*
  719. * initiate a "graceful stop transmit"
  720. * on the channel
  721. */
  722. cp->cp_cpcr = mk_cr_cmd (ecp->page, \
  723. ecp->sblock, 0x0c, \
  724. CPM_CR_GRACEFUL_STOP_TX) | \
  725. CPM_CR_FLG;
  726. do {
  727. __asm__ __volatile__ ("eieio");
  728. } while (cp->cp_cpcr & CPM_CR_FLG);
  729. ecp->clstime = get_timer (0);
  730. ecp->state = Closing;
  731. }
  732. /* fall through ... */
  733. case Closing:
  734. /*
  735. * (while Running or Closing) poll the channel:
  736. * - check for any non-READY tx buffers and
  737. * make them ready
  738. * - check for any non-EMPTY rx buffers and
  739. * check that they were received correctly,
  740. * adjust counters etc, then make empty
  741. */
  742. for (i = 0; i < ELBT_NTXBD; i++) {
  743. cbd_t *bdp = &ecp->txbd[i];
  744. ushort sc = bdp->cbd_sc;
  745. if ((sc & BD_ENET_TX_READY) != 0)
  746. continue;
  747. /*
  748. * this frame has finished
  749. * transmitting
  750. */
  751. ecp->nsent++;
  752. if (sc & BD_ENET_TX_STATS) {
  753. ulong n;
  754. /*
  755. * we had an error on
  756. * the transmission
  757. */
  758. n = ecp->ntxerr++;
  759. if (n < ELBT_MAXTXERR)
  760. ecp->txerrs[n] = sc;
  761. if (sc & BD_ENET_TX_DEF)
  762. ecp->txeacc.def++;
  763. if (sc & BD_ENET_TX_HB)
  764. ecp->txeacc.hb++;
  765. if (sc & BD_ENET_TX_LC)
  766. ecp->txeacc.lc++;
  767. if (sc & BD_ENET_TX_RL)
  768. ecp->txeacc.rl++;
  769. if (sc & BD_ENET_TX_RCMASK)
  770. ecp->txeacc.rc++;
  771. if (sc & BD_ENET_TX_UN)
  772. ecp->txeacc.un++;
  773. if (sc & BD_ENET_TX_CSL)
  774. ecp->txeacc.csl++;
  775. bdp->cbd_sc &= \
  776. ~BD_ENET_TX_STATS;
  777. }
  778. if (ecp->state == Closing)
  779. ecp->clstime = get_timer (0);
  780. /* make it ready again */
  781. bdp->cbd_sc |= BD_ENET_TX_READY;
  782. }
  783. for (i = 0; i < ELBT_NRXBD; i++) {
  784. cbd_t *bdp = &ecp->rxbd[i];
  785. ushort sc = bdp->cbd_sc, mask;
  786. if ((sc & BD_ENET_RX_EMPTY) != 0)
  787. continue;
  788. /* we have a new frame in this buffer */
  789. ecp->nrcvd++;
  790. mask = BD_ENET_RX_LAST|BD_ENET_RX_FIRST;
  791. if ((sc & mask) != mask) {
  792. /* somethings wrong here ... */
  793. if (!(sc & BD_ENET_RX_LAST))
  794. ecp->rxeacc._l++;
  795. if (!(sc & BD_ENET_RX_FIRST))
  796. ecp->rxeacc._f++;
  797. }
  798. if (sc & BD_ENET_RX_ERRS) {
  799. ulong n;
  800. /*
  801. * we had some sort of error
  802. * on the frame
  803. */
  804. n = ecp->nrxerr++;
  805. if (n < ELBT_MAXRXERR)
  806. ecp->rxerrs[n] = sc;
  807. if (sc & BD_ENET_RX_MISS)
  808. ecp->rxeacc.m++;
  809. if (sc & BD_ENET_RX_BC)
  810. ecp->rxeacc.bc++;
  811. if (sc & BD_ENET_RX_MC)
  812. ecp->rxeacc.mc++;
  813. if (sc & BD_ENET_RX_LG)
  814. ecp->rxeacc.lg++;
  815. if (sc & BD_ENET_RX_NO)
  816. ecp->rxeacc.no++;
  817. if (sc & BD_ENET_RX_SH)
  818. ecp->rxeacc.sh++;
  819. if (sc & BD_ENET_RX_CR)
  820. ecp->rxeacc.cr++;
  821. if (sc & BD_ENET_RX_OV)
  822. ecp->rxeacc.ov++;
  823. if (sc & BD_ENET_RX_CL)
  824. ecp->rxeacc.cl++;
  825. bdp->cbd_sc &= \
  826. ~BD_ENET_RX_ERRS;
  827. }
  828. else {
  829. ushort datlen = bdp->cbd_datlen;
  830. struct ethernet_hdr *ehp;
  831. ushort prot;
  832. int ours, tb, n, nbytes;
  833. ehp = (struct ethernet_hdr *) \
  834. &ecp->rxbufs[i][0];
  835. ours = memcmp (ehp->et_src, \
  836. NetOurEther, 6);
  837. prot = swap16 (ehp->et_protlen);
  838. tb = prot & 0x8000;
  839. n = prot & 0x7fff;
  840. nbytes = ELBT_BUFSZ -
  841. ETHER_HDR_SIZE -
  842. ELBT_CRCSZ;
  843. /* check the frame is correct */
  844. if (datlen != ELBT_BUFSZ)
  845. ecp->rxeacc.badlen++;
  846. else if (!ours)
  847. ecp->rxeacc.badsrc++;
  848. else if (!tb || n >= ELBT_NTXBD)
  849. ecp->rxeacc.badtyp++;
  850. else {
  851. ulong patword = \
  852. patwords[n];
  853. uint nbb;
  854. nbb = badbits(
  855. ((uchar *)&ehp) +
  856. ETHER_HDR_SIZE,
  857. nbytes, patword);
  858. ecp->rxeacc.badbit += \
  859. nbb;
  860. }
  861. }
  862. if (ecp->state == Closing)
  863. ecp->clstime = get_timer (0);
  864. /* make it empty again */
  865. bdp->cbd_sc |= BD_ENET_RX_EMPTY;
  866. }
  867. if (ecp->state != Closing)
  868. break;
  869. /*
  870. * (while Closing) check to see if
  871. * waited long enough
  872. */
  873. if (get_timer (ecp->clstime) >= ELBT_CLSWAIT) {
  874. /* write GFMR: disable tx/rx */
  875. fcp->fcc_gfmr &= \
  876. ~(FCC_GFMR_ENT | FCC_GFMR_ENR);
  877. ecp->state = Closed;
  878. }
  879. break;
  880. case Closed:
  881. nclosed++;
  882. break;
  883. }
  884. }
  885. } while (nclosed < (FCC_END_LOOP - FCC_START_LOOP + 1));
  886. runtime = get_timer (runtime);
  887. if (runtime <= ELBT_CLSWAIT) {
  888. printf ("Whoops! somehow elapsed time (%ld) is wrong (<= %d)\n",
  889. runtime, ELBT_CLSWAIT);
  890. return;
  891. }
  892. nmsec = runtime - ELBT_CLSWAIT;
  893. printf ("Test Finished in %ldms (plus %dms close wait period)!\n\n",
  894. nmsec, ELBT_CLSWAIT);
  895. /*
  896. * now print stats
  897. */
  898. for (c = FCC_START_LOOP; c <= FCC_END_LOOP; c++) {
  899. elbt_chan *ecp = &elbt_chans[c];
  900. uint rxpps, txpps, nerr;
  901. rxpps = (ecp->nrcvd * 1000) / nmsec;
  902. txpps = (ecp->nsent * 1000) / nmsec;
  903. printf ("Channel %d: %d rcvd (%d pps, %d rxerrs), "
  904. "%d sent (%d pps, %d txerrs)\n\n", c,
  905. ecp->nrcvd, rxpps, ecp->nrxerr,
  906. ecp->nsent, txpps, ecp->ntxerr);
  907. if ((nerr = ecp->nrxerr) > 0) {
  908. ulong i;
  909. printf ("\tFirst %d rx errs:", nerr);
  910. for (i = 0; i < nerr; i++)
  911. printf (" %04x", ecp->rxerrs[i]);
  912. putc ('\n');
  913. }
  914. if ((nerr = ecp->ntxerr) > 0) {
  915. ulong i;
  916. printf ("\tFirst %d tx errs:", nerr);
  917. for (i = 0; i < nerr; i++)
  918. printf (" %04x", ecp->txerrs[i]);
  919. putc ('\n');
  920. }
  921. }
  922. puts ("Receive Error Counts:\n");
  923. for (c = FCC_START_LOOP; c <= FCC_END_LOOP; c++)
  924. bases[c] = (uchar *)&elbt_chans[c].rxeacc;
  925. print_desc (rxeacc_descs, rxeacc_ndesc, bases, 3);
  926. puts ("\nTransmit Error Counts:\n");
  927. for (c = FCC_START_LOOP; c <= FCC_END_LOOP; c++)
  928. bases[c] = (uchar *)&elbt_chans[c].txeacc;
  929. print_desc (txeacc_descs, txeacc_ndesc, bases, 3);
  930. puts ("\nRMON(-like) Counters:\n");
  931. for (c = FCC_START_LOOP; c <= FCC_END_LOOP; c++)
  932. bases[c] = (uchar *)&immr->im_dprambase[elbt_chans[c].proff];
  933. print_desc (epram_descs, epram_ndesc, bases, 3);
  934. }
  935. #endif /* CONFIG_ETHER_LOOPBACK_TEST */
  936. #endif