ether_fcc.c 29 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177
  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. printf("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. printf("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. puts ("\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. puts ("\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. void
  526. eth_loopback_test (void)
  527. {
  528. DECLARE_GLOBAL_DATA_PTR;
  529. volatile immap_t *immr = (immap_t *)CFG_IMMR;
  530. volatile cpm8260_t *cp = &(immr->im_cpm);
  531. int c, nclosed;
  532. ulong runtime, nmsec;
  533. uchar *bases[3];
  534. puts ("FCC Ethernet External loopback test\n");
  535. memcpy (NetOurEther, gd->bd->bi_enetaddr, 6);
  536. /*
  537. * global initialisations for all FCC channels
  538. */
  539. /* 28.9 - (1-2): ioports have been set up already */
  540. #if defined(CONFIG_HYMOD)
  541. /*
  542. * Attention: this is board-specific
  543. * 0, FCC1
  544. * 1, FCC2
  545. * 2, FCC3
  546. */
  547. # define FCC_START_LOOP 0
  548. # define FCC_END_LOOP 2
  549. /*
  550. * Attention: this is board-specific
  551. * - FCC1 Rx-CLK is CLK10
  552. * - FCC1 Tx-CLK is CLK11
  553. * - FCC2 Rx-CLK is CLK13
  554. * - FCC2 Tx-CLK is CLK14
  555. * - FCC3 Rx-CLK is CLK15
  556. * - FCC3 Tx-CLK is CLK16
  557. */
  558. /* 28.9 - (3): connect FCC's tx and rx clocks */
  559. immr->im_cpmux.cmx_uar = 0;
  560. immr->im_cpmux.cmx_fcr = CMXFCR_RF1CS_CLK10|CMXFCR_TF1CS_CLK11|\
  561. CMXFCR_RF2CS_CLK13|CMXFCR_TF2CS_CLK14|\
  562. CMXFCR_RF3CS_CLK15|CMXFCR_TF3CS_CLK16;
  563. #elif defined(CONFIG_SBC8260) || defined(CONFIG_SACSng)
  564. /*
  565. * Attention: this is board-specific
  566. * 1, FCC2
  567. */
  568. # define FCC_START_LOOP 1
  569. # define FCC_END_LOOP 1
  570. /*
  571. * Attention: this is board-specific
  572. * - FCC2 Rx-CLK is CLK13
  573. * - FCC2 Tx-CLK is CLK14
  574. */
  575. /* 28.9 - (3): connect FCC's tx and rx clocks */
  576. immr->im_cpmux.cmx_uar = 0;
  577. immr->im_cpmux.cmx_fcr = CMXFCR_RF2CS_CLK13|CMXFCR_TF2CS_CLK14;
  578. #else
  579. #error "eth_loopback_test not supported on your board"
  580. #endif
  581. puts ("Initialise FCC channels:");
  582. for (c = FCC_START_LOOP; c <= FCC_END_LOOP; c++) {
  583. elbt_chan *ecp = &elbt_chans[c];
  584. volatile fcc_t *fcp = &immr->im_fcc[c];
  585. volatile fcc_enet_t *fpp;
  586. int i;
  587. ulong addr;
  588. /*
  589. * initialise channel data
  590. */
  591. printf (" %d", c);
  592. memset ((void *)ecp, 0, sizeof (*ecp));
  593. ecp->state = Idle;
  594. switch (c) {
  595. case 0: /* FCC1 */
  596. ecp->proff = PROFF_FCC1;
  597. ecp->page = CPM_CR_FCC1_PAGE;
  598. ecp->sblock = CPM_CR_FCC1_SBLOCK;
  599. break;
  600. case 1: /* FCC2 */
  601. ecp->proff = PROFF_FCC2;
  602. ecp->page = CPM_CR_FCC2_PAGE;
  603. ecp->sblock = CPM_CR_FCC2_SBLOCK;
  604. break;
  605. case 2: /* FCC3 */
  606. ecp->proff = PROFF_FCC3;
  607. ecp->page = CPM_CR_FCC3_PAGE;
  608. ecp->sblock = CPM_CR_FCC3_SBLOCK;
  609. break;
  610. }
  611. /*
  612. * set up tx buffers and bds
  613. */
  614. for (i = 0; i < ELBT_NTXBD; i++) {
  615. cbd_t *bdp = &ecp->txbd[i];
  616. uchar *bp = &ecp->txbufs[i][0];
  617. bdp->cbd_bufaddr = (uint)bp;
  618. /* room for crc */
  619. bdp->cbd_datlen = ELBT_BUFSZ - ELBT_CRCSZ;
  620. bdp->cbd_sc = BD_ENET_TX_READY | BD_ENET_TX_PAD | \
  621. BD_ENET_TX_LAST | BD_ENET_TX_TC;
  622. memset ((void *)bp, patbytes[i], ELBT_BUFSZ);
  623. NetSetEther (bp, NetBcastAddr, 0x8000);
  624. }
  625. ecp->txbd[ELBT_NTXBD - 1].cbd_sc |= BD_ENET_TX_WRAP;
  626. /*
  627. * set up rx buffers and bds
  628. */
  629. for (i = 0; i < ELBT_NRXBD; i++) {
  630. cbd_t *bdp = &ecp->rxbd[i];
  631. uchar *bp = &ecp->rxbufs[i][0];
  632. bdp->cbd_bufaddr = (uint)bp;
  633. bdp->cbd_datlen = 0;
  634. bdp->cbd_sc = BD_ENET_RX_EMPTY;
  635. memset ((void *)bp, 0, ELBT_BUFSZ);
  636. }
  637. ecp->rxbd[ELBT_NRXBD - 1].cbd_sc |= BD_ENET_RX_WRAP;
  638. /*
  639. * set up the FCC channel hardware
  640. */
  641. /* 28.9 - (4): GFMR: disable tx/rx, CCITT CRC, Mode Ethernet */
  642. fcp->fcc_gfmr = FCC_GFMR_MODE_ENET | FCC_GFMR_TCRC_32;
  643. /* 28.9 - (5): FPSMR: fd, enet CRC, Promis, RMON, Rx SHort */
  644. fcp->fcc_fpsmr = FCC_PSMR_FDE | FCC_PSMR_LPB | \
  645. FCC_PSMR_ENCRC | FCC_PSMR_PRO | \
  646. FCC_PSMR_MON | FCC_PSMR_RSH;
  647. /* 28.9 - (6): FDSR: Ethernet Syn */
  648. fcp->fcc_fdsr = 0xD555;
  649. /* 29.9 - (7): initialise parameter ram */
  650. fpp = (fcc_enet_t *)&(immr->im_dprambase[ecp->proff]);
  651. /* clear whole struct to make sure all resv fields are zero */
  652. memset ((void *)fpp, 0, sizeof (fcc_enet_t));
  653. /*
  654. * common Parameter RAM area
  655. *
  656. * Allocate space in the reserved FCC area of DPRAM for the
  657. * internal buffers. No one uses this space (yet), so we
  658. * can do this. Later, we will add resource management for
  659. * this area.
  660. */
  661. addr = CPM_FCC_SPECIAL_BASE + (c * 64);
  662. fpp->fen_genfcc.fcc_riptr = addr;
  663. fpp->fen_genfcc.fcc_tiptr = addr + 32;
  664. /*
  665. * Set maximum bytes per receive buffer.
  666. * It must be a multiple of 32.
  667. * buffers are in 60x bus memory.
  668. */
  669. fpp->fen_genfcc.fcc_mrblr = PKT_MAXBLR_SIZE;
  670. fpp->fen_genfcc.fcc_rstate = (CPMFCR_GBL | CPMFCR_EB) << 24;
  671. fpp->fen_genfcc.fcc_rbase = (unsigned int)(&ecp->rxbd[0]);
  672. fpp->fen_genfcc.fcc_tstate = (CPMFCR_GBL | CPMFCR_EB) << 24;
  673. fpp->fen_genfcc.fcc_tbase = (unsigned int)(&ecp->txbd[0]);
  674. /* protocol-specific area */
  675. fpp->fen_cmask = 0xdebb20e3; /* CRC mask */
  676. fpp->fen_cpres = 0xffffffff; /* CRC preset */
  677. fpp->fen_retlim = 15; /* Retry limit threshold */
  678. fpp->fen_mflr = PKT_MAXBUF_SIZE;/* max frame length register */
  679. /*
  680. * Set Ethernet station address.
  681. *
  682. * This is supplied in the board information structure, so we
  683. * copy that into the controller.
  684. * So, far we have only been given one Ethernet address. We use
  685. * the same address for all channels
  686. */
  687. #define ea gd->bd->bi_enetaddr
  688. fpp->fen_paddrh = (ea[5] << 8) + ea[4];
  689. fpp->fen_paddrm = (ea[3] << 8) + ea[2];
  690. fpp->fen_paddrl = (ea[1] << 8) + ea[0];
  691. #undef ea
  692. fpp->fen_minflr = PKT_MINBUF_SIZE; /* min frame len register */
  693. /*
  694. * pad pointer. use tiptr since we don't need
  695. * a specific padding char
  696. */
  697. fpp->fen_padptr = fpp->fen_genfcc.fcc_tiptr;
  698. fpp->fen_maxd1 = PKT_MAXDMA_SIZE; /* max DMA1 length */
  699. fpp->fen_maxd2 = PKT_MAXDMA_SIZE; /* max DMA2 length */
  700. fpp->fen_rfthr = 1;
  701. fpp->fen_rfcnt = 1;
  702. /* 28.9 - (8): clear out events in FCCE */
  703. fcp->fcc_fcce = ~0x0;
  704. /* 28.9 - (9): FCCM: mask all events */
  705. fcp->fcc_fccm = 0;
  706. /* 28.9 - (10-12): we don't use ethernet interrupts */
  707. /* 28.9 - (13)
  708. *
  709. * Let's re-initialize the channel now. We have to do it later
  710. * than the manual describes because we have just now finished
  711. * the BD initialization.
  712. */
  713. cp->cp_cpcr = mk_cr_cmd (ecp->page, ecp->sblock, \
  714. 0x0c, CPM_CR_INIT_TRX) | CPM_CR_FLG;
  715. do {
  716. __asm__ __volatile__ ("eieio");
  717. } while (cp->cp_cpcr & CPM_CR_FLG);
  718. }
  719. puts (" done\nStarting test... (Ctrl-C to Finish)\n");
  720. /*
  721. * Note: don't want serial output from here until the end of the
  722. * test - the delays would probably stuff things up.
  723. */
  724. clear_ctrlc ();
  725. runtime = get_timer (0);
  726. do {
  727. nclosed = 0;
  728. for (c = FCC_START_LOOP; c <= FCC_END_LOOP; c++) {
  729. volatile fcc_t *fcp = &immr->im_fcc[c];
  730. elbt_chan *ecp = &elbt_chans[c];
  731. int i;
  732. switch (ecp->state) {
  733. case Idle:
  734. /*
  735. * set the channel Running ...
  736. */
  737. /* 28.9 - (14): enable tx/rx in gfmr */
  738. fcp->fcc_gfmr |= FCC_GFMR_ENT | FCC_GFMR_ENR;
  739. ecp->state = Running;
  740. break;
  741. case Running:
  742. /*
  743. * (while Running only) check for
  744. * termination of the test
  745. */
  746. (void)ctrlc ();
  747. if (had_ctrlc ()) {
  748. /*
  749. * initiate a "graceful stop transmit"
  750. * on the channel
  751. */
  752. cp->cp_cpcr = mk_cr_cmd (ecp->page, \
  753. ecp->sblock, 0x0c, \
  754. CPM_CR_GRACEFUL_STOP_TX) | \
  755. CPM_CR_FLG;
  756. do {
  757. __asm__ __volatile__ ("eieio");
  758. } while (cp->cp_cpcr & CPM_CR_FLG);
  759. ecp->clstime = get_timer (0);
  760. ecp->state = Closing;
  761. }
  762. /* fall through ... */
  763. case Closing:
  764. /*
  765. * (while Running or Closing) poll the channel:
  766. * - check for any non-READY tx buffers and
  767. * make them ready
  768. * - check for any non-EMPTY rx buffers and
  769. * check that they were received correctly,
  770. * adjust counters etc, then make empty
  771. */
  772. for (i = 0; i < ELBT_NTXBD; i++) {
  773. cbd_t *bdp = &ecp->txbd[i];
  774. ushort sc = bdp->cbd_sc;
  775. if ((sc & BD_ENET_TX_READY) != 0)
  776. continue;
  777. /*
  778. * this frame has finished
  779. * transmitting
  780. */
  781. ecp->nsent++;
  782. if (sc & BD_ENET_TX_STATS) {
  783. ulong n;
  784. /*
  785. * we had an error on
  786. * the transmission
  787. */
  788. n = ecp->ntxerr++;
  789. if (n < ELBT_MAXTXERR)
  790. ecp->txerrs[n] = sc;
  791. if (sc & BD_ENET_TX_DEF)
  792. ecp->txeacc.def++;
  793. if (sc & BD_ENET_TX_HB)
  794. ecp->txeacc.hb++;
  795. if (sc & BD_ENET_TX_LC)
  796. ecp->txeacc.lc++;
  797. if (sc & BD_ENET_TX_RL)
  798. ecp->txeacc.rl++;
  799. if (sc & BD_ENET_TX_RCMASK)
  800. ecp->txeacc.rc++;
  801. if (sc & BD_ENET_TX_UN)
  802. ecp->txeacc.un++;
  803. if (sc & BD_ENET_TX_CSL)
  804. ecp->txeacc.csl++;
  805. bdp->cbd_sc &= \
  806. ~BD_ENET_TX_STATS;
  807. }
  808. if (ecp->state == Closing)
  809. ecp->clstime = get_timer (0);
  810. /* make it ready again */
  811. bdp->cbd_sc |= BD_ENET_TX_READY;
  812. }
  813. for (i = 0; i < ELBT_NRXBD; i++) {
  814. cbd_t *bdp = &ecp->rxbd[i];
  815. ushort sc = bdp->cbd_sc, mask;
  816. if ((sc & BD_ENET_RX_EMPTY) != 0)
  817. continue;
  818. /* we have a new frame in this buffer */
  819. ecp->nrcvd++;
  820. mask = BD_ENET_RX_LAST|BD_ENET_RX_FIRST;
  821. if ((sc & mask) != mask) {
  822. /* somethings wrong here ... */
  823. if (!(sc & BD_ENET_RX_LAST))
  824. ecp->rxeacc._l++;
  825. if (!(sc & BD_ENET_RX_FIRST))
  826. ecp->rxeacc._f++;
  827. }
  828. if (sc & BD_ENET_RX_STATS) {
  829. ulong n;
  830. /*
  831. * we had some sort of error
  832. * on the frame
  833. */
  834. n = ecp->nrxerr++;
  835. if (n < ELBT_MAXRXERR)
  836. ecp->rxerrs[n] = sc;
  837. if (sc & BD_ENET_RX_MISS)
  838. ecp->rxeacc.m++;
  839. if (sc & BD_ENET_RX_BC)
  840. ecp->rxeacc.bc++;
  841. if (sc & BD_ENET_RX_MC)
  842. ecp->rxeacc.mc++;
  843. if (sc & BD_ENET_RX_LG)
  844. ecp->rxeacc.lg++;
  845. if (sc & BD_ENET_RX_NO)
  846. ecp->rxeacc.no++;
  847. if (sc & BD_ENET_RX_SH)
  848. ecp->rxeacc.sh++;
  849. if (sc & BD_ENET_RX_CR)
  850. ecp->rxeacc.cr++;
  851. if (sc & BD_ENET_RX_OV)
  852. ecp->rxeacc.ov++;
  853. if (sc & BD_ENET_RX_CL)
  854. ecp->rxeacc.cl++;
  855. bdp->cbd_sc &= \
  856. ~BD_ENET_RX_STATS;
  857. }
  858. else {
  859. ushort datlen = bdp->cbd_datlen;
  860. Ethernet_t *ehp;
  861. ushort prot;
  862. int ours, tb, n, nbytes;
  863. ehp = (Ethernet_t *) \
  864. &ecp->rxbufs[i][0];
  865. ours = memcmp (ehp->et_src, \
  866. NetOurEther, 6);
  867. prot = swap16 (ehp->et_protlen);
  868. tb = prot & 0x8000;
  869. n = prot & 0x7fff;
  870. nbytes = ELBT_BUFSZ - \
  871. offsetof (Ethernet_t, \
  872. et_dsap) - \
  873. ELBT_CRCSZ;
  874. /* check the frame is correct */
  875. if (datlen != ELBT_BUFSZ)
  876. ecp->rxeacc.badlen++;
  877. else if (!ours)
  878. ecp->rxeacc.badsrc++;
  879. else if (!tb || n >= ELBT_NTXBD)
  880. ecp->rxeacc.badtyp++;
  881. else {
  882. ulong patword = \
  883. patwords[n];
  884. uint nbb;
  885. nbb = badbits ( \
  886. &ehp->et_dsap, \
  887. nbytes, \
  888. patword);
  889. ecp->rxeacc.badbit += \
  890. nbb;
  891. }
  892. }
  893. if (ecp->state == Closing)
  894. ecp->clstime = get_timer (0);
  895. /* make it empty again */
  896. bdp->cbd_sc |= BD_ENET_RX_EMPTY;
  897. }
  898. if (ecp->state != Closing)
  899. break;
  900. /*
  901. * (while Closing) check to see if
  902. * waited long enough
  903. */
  904. if (get_timer (ecp->clstime) >= ELBT_CLSWAIT) {
  905. /* write GFMR: disable tx/rx */
  906. fcp->fcc_gfmr &= \
  907. ~(FCC_GFMR_ENT | FCC_GFMR_ENR);
  908. ecp->state = Closed;
  909. }
  910. break;
  911. case Closed:
  912. nclosed++;
  913. break;
  914. }
  915. }
  916. } while (nclosed < (FCC_END_LOOP - FCC_START_LOOP + 1));
  917. runtime = get_timer (runtime);
  918. if (runtime <= ELBT_CLSWAIT) {
  919. printf ("Whoops! somehow elapsed time (%ld) is wrong (<= %d)\n",
  920. runtime, ELBT_CLSWAIT);
  921. return;
  922. }
  923. nmsec = runtime - ELBT_CLSWAIT;
  924. printf ("Test Finished in %ldms (plus %dms close wait period)!\n\n",
  925. nmsec, ELBT_CLSWAIT);
  926. /*
  927. * now print stats
  928. */
  929. for (c = FCC_START_LOOP; c <= FCC_END_LOOP; c++) {
  930. elbt_chan *ecp = &elbt_chans[c];
  931. uint rxpps, txpps, nerr;
  932. rxpps = (ecp->nrcvd * 1000) / nmsec;
  933. txpps = (ecp->nsent * 1000) / nmsec;
  934. printf ("Channel %d: %d rcvd (%d pps, %d rxerrs), "
  935. "%d sent (%d pps, %d txerrs)\n\n", c,
  936. ecp->nrcvd, rxpps, ecp->nrxerr,
  937. ecp->nsent, txpps, ecp->ntxerr);
  938. if ((nerr = ecp->nrxerr) > 0) {
  939. ulong i;
  940. printf ("\tFirst %d rx errs:", nerr);
  941. for (i = 0; i < nerr; i++)
  942. printf (" %04x", ecp->rxerrs[i]);
  943. puts ("\n");
  944. }
  945. if ((nerr = ecp->ntxerr) > 0) {
  946. ulong i;
  947. printf ("\tFirst %d tx errs:", nerr);
  948. for (i = 0; i < nerr; i++)
  949. printf (" %04x", ecp->txerrs[i]);
  950. puts ("\n");
  951. }
  952. }
  953. puts ("Receive Error Counts:\n");
  954. for (c = FCC_START_LOOP; c <= FCC_END_LOOP; c++)
  955. bases[c] = (uchar *)&elbt_chans[c].rxeacc;
  956. print_desc (rxeacc_descs, rxeacc_ndesc, bases, 3);
  957. puts ("\nTransmit Error Counts:\n");
  958. for (c = FCC_START_LOOP; c <= FCC_END_LOOP; c++)
  959. bases[c] = (uchar *)&elbt_chans[c].txeacc;
  960. print_desc (txeacc_descs, txeacc_ndesc, bases, 3);
  961. puts ("\nRMON(-like) Counters:\n");
  962. for (c = FCC_START_LOOP; c <= FCC_END_LOOP; c++)
  963. bases[c] = (uchar *)&immr->im_dprambase[elbt_chans[c].proff];
  964. print_desc (epram_descs, epram_ndesc, bases, 3);
  965. }
  966. #endif /* CONFIG_ETHER_LOOPBACK_TEST */
  967. #endif /* CONFIG_ETHER_ON_FCC && CFG_CMD_NET && CONFIG_NET_MULTI */