ether_fcc.c 28 KB

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