ether_fcc.c 29 KB

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