ether_fcc.c 28 KB

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