ether.c 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633
  1. /*
  2. * (C) Copyright 2002
  3. * Wolfgang Denk, DENX Software Engineering, wd@denx.de.
  4. *
  5. * See file CREDITS for list of people who contributed to this
  6. * project.
  7. *
  8. * This program is free software; you can redistribute it and/or
  9. * modify it under the terms of the GNU General Public License as
  10. * published by the Free Software Foundation; either version 2 of
  11. * the License, or (at your option) any later version.
  12. *
  13. * This program is distributed in the hope that it will be useful,
  14. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  15. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  16. * GNU General Public License for more details.
  17. *
  18. * You should have received a copy of the GNU General Public License
  19. * along with this program; if not, write to the Free Software
  20. * Foundation, Inc., 59 Temple Place, Suite 330, Boston,
  21. * MA 02111-1307 USA
  22. */
  23. #include <common.h>
  24. /*
  25. * Ethernet test
  26. *
  27. * The Serial Communication Controllers (SCC) listed in ctlr_list array below
  28. * are tested in the loopback ethernet mode.
  29. * The controllers are configured accordingly and several packets
  30. * are transmitted. The configurable test parameters are:
  31. * MIN_PACKET_LENGTH - minimum size of packet to transmit
  32. * MAX_PACKET_LENGTH - maximum size of packet to transmit
  33. * TEST_NUM - number of tests
  34. */
  35. #include <post.h>
  36. #if CONFIG_POST & CONFIG_SYS_POST_ETHER
  37. #if defined(CONFIG_8xx)
  38. #include <commproc.h>
  39. #elif defined(CONFIG_MPC8260)
  40. #include <asm/cpm_8260.h>
  41. #else
  42. #error "Apparently a bad configuration, please fix."
  43. #endif
  44. #include <command.h>
  45. #include <net.h>
  46. #include <serial.h>
  47. DECLARE_GLOBAL_DATA_PTR;
  48. #define MIN_PACKET_LENGTH 64
  49. #define MAX_PACKET_LENGTH 256
  50. #define TEST_NUM 1
  51. #define CTLR_SCC 0
  52. extern void spi_init_f (void);
  53. extern void spi_init_r (void);
  54. /* The list of controllers to test */
  55. #if defined(CONFIG_MPC823)
  56. static int ctlr_list[][2] = { {CTLR_SCC, 1} };
  57. #else
  58. static int ctlr_list[][2] = { };
  59. #endif
  60. #define CTRL_LIST_SIZE (sizeof(ctlr_list) / sizeof(ctlr_list[0]))
  61. static struct {
  62. void (*init) (int index);
  63. void (*halt) (int index);
  64. int (*send) (int index, volatile void *packet, int length);
  65. int (*recv) (int index, void *packet, int length);
  66. } ctlr_proc[1];
  67. static char *ctlr_name[1] = { "SCC" };
  68. /* Ethernet Transmit and Receive Buffers */
  69. #define DBUF_LENGTH 1520
  70. #define TX_BUF_CNT 2
  71. #define TOUT_LOOP 100
  72. static char txbuf[DBUF_LENGTH];
  73. static uint rxIdx; /* index of the current RX buffer */
  74. static uint txIdx; /* index of the current TX buffer */
  75. /*
  76. * SCC Ethernet Tx and Rx buffer descriptors allocated at the
  77. * immr->udata_bd address on Dual-Port RAM
  78. * Provide for Double Buffering
  79. */
  80. typedef volatile struct CommonBufferDescriptor {
  81. cbd_t rxbd[PKTBUFSRX]; /* Rx BD */
  82. cbd_t txbd[TX_BUF_CNT]; /* Tx BD */
  83. } RTXBD;
  84. static RTXBD *rtx;
  85. /*
  86. * SCC callbacks
  87. */
  88. static void scc_init (int scc_index)
  89. {
  90. uchar ea[6];
  91. static int proff[] = {
  92. PROFF_SCC1,
  93. PROFF_SCC2,
  94. PROFF_SCC3,
  95. PROFF_SCC4,
  96. };
  97. static unsigned int cpm_cr[] = {
  98. CPM_CR_CH_SCC1,
  99. CPM_CR_CH_SCC2,
  100. CPM_CR_CH_SCC3,
  101. CPM_CR_CH_SCC4,
  102. };
  103. int i;
  104. scc_enet_t *pram_ptr;
  105. volatile immap_t *immr = (immap_t *) CONFIG_SYS_IMMR;
  106. immr->im_cpm.cp_scc[scc_index].scc_gsmrl &=
  107. ~(SCC_GSMRL_ENR | SCC_GSMRL_ENT);
  108. #if defined(CONFIG_FADS)
  109. #if defined(CONFIG_MPC860T) || defined(CONFIG_MPC86xADS)
  110. /* The FADS860T and MPC86xADS don't use the MODEM_EN or DATA_VOICE signals. */
  111. *((uint *) BCSR4) &= ~BCSR4_ETHLOOP;
  112. *((uint *) BCSR4) |= BCSR4_TFPLDL | BCSR4_TPSQEL;
  113. *((uint *) BCSR1) &= ~BCSR1_ETHEN;
  114. #else
  115. *((uint *) BCSR4) &= ~(BCSR4_ETHLOOP | BCSR4_MODEM_EN);
  116. *((uint *) BCSR4) |= BCSR4_TFPLDL | BCSR4_TPSQEL | BCSR4_DATA_VOICE;
  117. *((uint *) BCSR1) &= ~BCSR1_ETHEN;
  118. #endif
  119. #endif
  120. pram_ptr = (scc_enet_t *) & (immr->im_cpm.cp_dparam[proff[scc_index]]);
  121. rxIdx = 0;
  122. txIdx = 0;
  123. #ifdef CONFIG_SYS_ALLOC_DPRAM
  124. rtx = (RTXBD *) (immr->im_cpm.cp_dpmem +
  125. dpram_alloc_align (sizeof (RTXBD), 8));
  126. #else
  127. rtx = (RTXBD *) (immr->im_cpm.cp_dpmem + CPM_SCC_BASE);
  128. #endif
  129. #if 0
  130. #if (defined(PA_ENET_RXD) && defined(PA_ENET_TXD))
  131. /* Configure port A pins for Txd and Rxd.
  132. */
  133. immr->im_ioport.iop_papar |= (PA_ENET_RXD | PA_ENET_TXD);
  134. immr->im_ioport.iop_padir &= ~(PA_ENET_RXD | PA_ENET_TXD);
  135. immr->im_ioport.iop_paodr &= ~PA_ENET_TXD;
  136. #elif (defined(PB_ENET_RXD) && defined(PB_ENET_TXD))
  137. /* Configure port B pins for Txd and Rxd.
  138. */
  139. immr->im_cpm.cp_pbpar |= (PB_ENET_RXD | PB_ENET_TXD);
  140. immr->im_cpm.cp_pbdir &= ~(PB_ENET_RXD | PB_ENET_TXD);
  141. immr->im_cpm.cp_pbodr &= ~PB_ENET_TXD;
  142. #else
  143. #error Configuration Error: exactly ONE of PA_ENET_[RT]XD, PB_ENET_[RT]XD must be defined
  144. #endif
  145. #if defined(PC_ENET_LBK)
  146. /* Configure port C pins to disable External Loopback
  147. */
  148. immr->im_ioport.iop_pcpar &= ~PC_ENET_LBK;
  149. immr->im_ioport.iop_pcdir |= PC_ENET_LBK;
  150. immr->im_ioport.iop_pcso &= ~PC_ENET_LBK;
  151. immr->im_ioport.iop_pcdat &= ~PC_ENET_LBK; /* Disable Loopback */
  152. #endif /* PC_ENET_LBK */
  153. /* Configure port C pins to enable CLSN and RENA.
  154. */
  155. immr->im_ioport.iop_pcpar &= ~(PC_ENET_CLSN | PC_ENET_RENA);
  156. immr->im_ioport.iop_pcdir &= ~(PC_ENET_CLSN | PC_ENET_RENA);
  157. immr->im_ioport.iop_pcso |= (PC_ENET_CLSN | PC_ENET_RENA);
  158. /* Configure port A for TCLK and RCLK.
  159. */
  160. immr->im_ioport.iop_papar |= (PA_ENET_TCLK | PA_ENET_RCLK);
  161. immr->im_ioport.iop_padir &= ~(PA_ENET_TCLK | PA_ENET_RCLK);
  162. /*
  163. * Configure Serial Interface clock routing -- see section 16.7.5.3
  164. * First, clear all SCC bits to zero, then set the ones we want.
  165. */
  166. immr->im_cpm.cp_sicr &= ~SICR_ENET_MASK;
  167. immr->im_cpm.cp_sicr |= SICR_ENET_CLKRT;
  168. #else
  169. /*
  170. * SCC2 receive clock is BRG2
  171. * SCC2 transmit clock is BRG3
  172. */
  173. immr->im_cpm.cp_brgc2 = 0x0001000C;
  174. immr->im_cpm.cp_brgc3 = 0x0001000C;
  175. immr->im_cpm.cp_sicr &= ~0x00003F00;
  176. immr->im_cpm.cp_sicr |= 0x00000a00;
  177. #endif /* 0 */
  178. /*
  179. * Initialize SDCR -- see section 16.9.23.7
  180. * SDMA configuration register
  181. */
  182. immr->im_siu_conf.sc_sdcr = 0x01;
  183. /*
  184. * Setup SCC Ethernet Parameter RAM
  185. */
  186. pram_ptr->sen_genscc.scc_rfcr = 0x18; /* Normal Operation and Mot byte ordering */
  187. pram_ptr->sen_genscc.scc_tfcr = 0x18; /* Mot byte ordering, Normal access */
  188. pram_ptr->sen_genscc.scc_mrblr = DBUF_LENGTH; /* max. ET package len 1520 */
  189. pram_ptr->sen_genscc.scc_rbase = (unsigned int) (&rtx->rxbd[0]); /* Set RXBD tbl start at Dual Port */
  190. pram_ptr->sen_genscc.scc_tbase = (unsigned int) (&rtx->txbd[0]); /* Set TXBD tbl start at Dual Port */
  191. /*
  192. * Setup Receiver Buffer Descriptors (13.14.24.18)
  193. * Settings:
  194. * Empty, Wrap
  195. */
  196. for (i = 0; i < PKTBUFSRX; i++) {
  197. rtx->rxbd[i].cbd_sc = BD_ENET_RX_EMPTY;
  198. rtx->rxbd[i].cbd_datlen = 0; /* Reset */
  199. rtx->rxbd[i].cbd_bufaddr = (uint) NetRxPackets[i];
  200. }
  201. rtx->rxbd[PKTBUFSRX - 1].cbd_sc |= BD_ENET_RX_WRAP;
  202. /*
  203. * Setup Ethernet Transmitter Buffer Descriptors (13.14.24.19)
  204. * Settings:
  205. * Add PADs to Short FRAMES, Wrap, Last, Tx CRC
  206. */
  207. for (i = 0; i < TX_BUF_CNT; i++) {
  208. rtx->txbd[i].cbd_sc =
  209. (BD_ENET_TX_PAD | BD_ENET_TX_LAST | BD_ENET_TX_TC);
  210. rtx->txbd[i].cbd_datlen = 0; /* Reset */
  211. rtx->txbd[i].cbd_bufaddr = (uint) (&txbuf[0]);
  212. }
  213. rtx->txbd[TX_BUF_CNT - 1].cbd_sc |= BD_ENET_TX_WRAP;
  214. /*
  215. * Enter Command: Initialize Rx Params for SCC
  216. */
  217. do { /* Spin until ready to issue command */
  218. __asm__ ("eieio");
  219. } while (immr->im_cpm.cp_cpcr & CPM_CR_FLG);
  220. /* Issue command */
  221. immr->im_cpm.cp_cpcr =
  222. ((CPM_CR_INIT_RX << 8) | (cpm_cr[scc_index] << 4) |
  223. CPM_CR_FLG);
  224. do { /* Spin until command processed */
  225. __asm__ ("eieio");
  226. } while (immr->im_cpm.cp_cpcr & CPM_CR_FLG);
  227. /*
  228. * Ethernet Specific Parameter RAM
  229. * see table 13-16, pg. 660,
  230. * pg. 681 (example with suggested settings)
  231. */
  232. pram_ptr->sen_cpres = ~(0x0); /* Preset CRC */
  233. pram_ptr->sen_cmask = 0xdebb20e3; /* Constant Mask for CRC */
  234. pram_ptr->sen_crcec = 0x0; /* Error Counter CRC (unused) */
  235. pram_ptr->sen_alec = 0x0; /* Alignment Error Counter (unused) */
  236. pram_ptr->sen_disfc = 0x0; /* Discard Frame Counter (unused) */
  237. pram_ptr->sen_pads = 0x8888; /* Short Frame PAD Characters */
  238. pram_ptr->sen_retlim = 15; /* Retry Limit Threshold */
  239. pram_ptr->sen_maxflr = 1518; /* MAX Frame Length Register */
  240. pram_ptr->sen_minflr = 64; /* MIN Frame Length Register */
  241. pram_ptr->sen_maxd1 = DBUF_LENGTH; /* MAX DMA1 Length Register */
  242. pram_ptr->sen_maxd2 = DBUF_LENGTH; /* MAX DMA2 Length Register */
  243. pram_ptr->sen_gaddr1 = 0x0; /* Group Address Filter 1 (unused) */
  244. pram_ptr->sen_gaddr2 = 0x0; /* Group Address Filter 2 (unused) */
  245. pram_ptr->sen_gaddr3 = 0x0; /* Group Address Filter 3 (unused) */
  246. pram_ptr->sen_gaddr4 = 0x0; /* Group Address Filter 4 (unused) */
  247. eth_getenv_enetaddr("ethaddr", ea);
  248. pram_ptr->sen_paddrh = (ea[5] << 8) + ea[4];
  249. pram_ptr->sen_paddrm = (ea[3] << 8) + ea[2];
  250. pram_ptr->sen_paddrl = (ea[1] << 8) + ea[0];
  251. pram_ptr->sen_pper = 0x0; /* Persistence (unused) */
  252. pram_ptr->sen_iaddr1 = 0x0; /* Individual Address Filter 1 (unused) */
  253. pram_ptr->sen_iaddr2 = 0x0; /* Individual Address Filter 2 (unused) */
  254. pram_ptr->sen_iaddr3 = 0x0; /* Individual Address Filter 3 (unused) */
  255. pram_ptr->sen_iaddr4 = 0x0; /* Individual Address Filter 4 (unused) */
  256. pram_ptr->sen_taddrh = 0x0; /* Tmp Address (MSB) (unused) */
  257. pram_ptr->sen_taddrm = 0x0; /* Tmp Address (unused) */
  258. pram_ptr->sen_taddrl = 0x0; /* Tmp Address (LSB) (unused) */
  259. /*
  260. * Enter Command: Initialize Tx Params for SCC
  261. */
  262. do { /* Spin until ready to issue command */
  263. __asm__ ("eieio");
  264. } while (immr->im_cpm.cp_cpcr & CPM_CR_FLG);
  265. /* Issue command */
  266. immr->im_cpm.cp_cpcr =
  267. ((CPM_CR_INIT_TX << 8) | (cpm_cr[scc_index] << 4) |
  268. CPM_CR_FLG);
  269. do { /* Spin until command processed */
  270. __asm__ ("eieio");
  271. } while (immr->im_cpm.cp_cpcr & CPM_CR_FLG);
  272. /*
  273. * Mask all Events in SCCM - we use polling mode
  274. */
  275. immr->im_cpm.cp_scc[scc_index].scc_sccm = 0;
  276. /*
  277. * Clear Events in SCCE -- Clear bits by writing 1's
  278. */
  279. immr->im_cpm.cp_scc[scc_index].scc_scce = ~(0x0);
  280. /*
  281. * Initialize GSMR High 32-Bits
  282. * Settings: Normal Mode
  283. */
  284. immr->im_cpm.cp_scc[scc_index].scc_gsmrh = 0;
  285. /*
  286. * Initialize GSMR Low 32-Bits, but do not Enable Transmit/Receive
  287. * Settings:
  288. * TCI = Invert
  289. * TPL = 48 bits
  290. * TPP = Repeating 10's
  291. * LOOP = Loopback
  292. * MODE = Ethernet
  293. */
  294. immr->im_cpm.cp_scc[scc_index].scc_gsmrl = (SCC_GSMRL_TCI |
  295. SCC_GSMRL_TPL_48 |
  296. SCC_GSMRL_TPP_10 |
  297. SCC_GSMRL_DIAG_LOOP |
  298. SCC_GSMRL_MODE_ENET);
  299. /*
  300. * Initialize the DSR -- see section 13.14.4 (pg. 513) v0.4
  301. */
  302. immr->im_cpm.cp_scc[scc_index].scc_dsr = 0xd555;
  303. /*
  304. * Initialize the PSMR
  305. * Settings:
  306. * CRC = 32-Bit CCITT
  307. * NIB = Begin searching for SFD 22 bits after RENA
  308. * LPB = Loopback Enable (Needed when FDE is set)
  309. */
  310. immr->im_cpm.cp_scc[scc_index].scc_psmr = SCC_PSMR_ENCRC |
  311. SCC_PSMR_NIB22 | SCC_PSMR_LPB;
  312. #if 0
  313. /*
  314. * Configure Ethernet TENA Signal
  315. */
  316. #if (defined(PC_ENET_TENA) && !defined(PB_ENET_TENA))
  317. immr->im_ioport.iop_pcpar |= PC_ENET_TENA;
  318. immr->im_ioport.iop_pcdir &= ~PC_ENET_TENA;
  319. #elif (defined(PB_ENET_TENA) && !defined(PC_ENET_TENA))
  320. immr->im_cpm.cp_pbpar |= PB_ENET_TENA;
  321. immr->im_cpm.cp_pbdir |= PB_ENET_TENA;
  322. #else
  323. #error Configuration Error: exactly ONE of PB_ENET_TENA, PC_ENET_TENA must be defined
  324. #endif
  325. #if defined(CONFIG_ADS) && defined(CONFIG_MPC860)
  326. /*
  327. * Port C is used to control the PHY,MC68160.
  328. */
  329. immr->im_ioport.iop_pcdir |=
  330. (PC_ENET_ETHLOOP | PC_ENET_TPFLDL | PC_ENET_TPSQEL);
  331. immr->im_ioport.iop_pcdat |= PC_ENET_TPFLDL;
  332. immr->im_ioport.iop_pcdat &= ~(PC_ENET_ETHLOOP | PC_ENET_TPSQEL);
  333. *((uint *) BCSR1) &= ~BCSR1_ETHEN;
  334. #endif /* MPC860ADS */
  335. #if defined(CONFIG_AMX860)
  336. /*
  337. * Port B is used to control the PHY,MC68160.
  338. */
  339. immr->im_cpm.cp_pbdir |=
  340. (PB_ENET_ETHLOOP | PB_ENET_TPFLDL | PB_ENET_TPSQEL);
  341. immr->im_cpm.cp_pbdat |= PB_ENET_TPFLDL;
  342. immr->im_cpm.cp_pbdat &= ~(PB_ENET_ETHLOOP | PB_ENET_TPSQEL);
  343. immr->im_ioport.iop_pddir |= PD_ENET_ETH_EN;
  344. immr->im_ioport.iop_pddat &= ~PD_ENET_ETH_EN;
  345. #endif /* AMX860 */
  346. #endif /* 0 */
  347. #ifdef CONFIG_RPXCLASSIC
  348. *((uchar *) BCSR0) &= ~BCSR0_ETHLPBK;
  349. *((uchar *) BCSR0) |= (BCSR0_ETHEN | BCSR0_COLTEST | BCSR0_FULLDPLX);
  350. #endif
  351. #ifdef CONFIG_RPXLITE
  352. *((uchar *) BCSR0) |= BCSR0_ETHEN;
  353. #endif
  354. #ifdef CONFIG_MBX
  355. board_ether_init ();
  356. #endif
  357. /*
  358. * Set the ENT/ENR bits in the GSMR Low -- Enable Transmit/Receive
  359. */
  360. immr->im_cpm.cp_scc[scc_index].scc_gsmrl |=
  361. (SCC_GSMRL_ENR | SCC_GSMRL_ENT);
  362. /*
  363. * Work around transmit problem with first eth packet
  364. */
  365. #if defined (CONFIG_FADS)
  366. udelay (10000); /* wait 10 ms */
  367. #elif defined (CONFIG_AMX860) || defined(CONFIG_RPXCLASSIC)
  368. udelay (100000); /* wait 100 ms */
  369. #endif
  370. }
  371. static void scc_halt (int scc_index)
  372. {
  373. volatile immap_t *immr = (immap_t *) CONFIG_SYS_IMMR;
  374. immr->im_cpm.cp_scc[scc_index].scc_gsmrl &=
  375. ~(SCC_GSMRL_ENR | SCC_GSMRL_ENT);
  376. immr->im_ioport.iop_pcso &= ~(PC_ENET_CLSN | PC_ENET_RENA);
  377. }
  378. static int scc_send (int index, volatile void *packet, int length)
  379. {
  380. int i, j = 0;
  381. while ((rtx->txbd[txIdx].cbd_sc & BD_ENET_TX_READY) && (j < TOUT_LOOP)) {
  382. udelay (1); /* will also trigger Wd if needed */
  383. j++;
  384. }
  385. if (j >= TOUT_LOOP)
  386. printf ("TX not ready\n");
  387. rtx->txbd[txIdx].cbd_bufaddr = (uint) packet;
  388. rtx->txbd[txIdx].cbd_datlen = length;
  389. rtx->txbd[txIdx].cbd_sc |=
  390. (BD_ENET_TX_READY | BD_ENET_TX_LAST | BD_ENET_TX_WRAP);
  391. while ((rtx->txbd[txIdx].cbd_sc & BD_ENET_TX_READY) && (j < TOUT_LOOP)) {
  392. udelay (1); /* will also trigger Wd if needed */
  393. j++;
  394. }
  395. if (j >= TOUT_LOOP)
  396. printf ("TX timeout\n");
  397. i = (rtx->txbd[txIdx].
  398. cbd_sc & BD_ENET_TX_STATS) /* return only status bits */ ;
  399. return i;
  400. }
  401. static int scc_recv (int index, void *packet, int max_length)
  402. {
  403. int length = -1;
  404. if (rtx->rxbd[rxIdx].cbd_sc & BD_ENET_RX_EMPTY) {
  405. goto Done; /* nothing received */
  406. }
  407. if (!(rtx->rxbd[rxIdx].cbd_sc & 0x003f)) {
  408. length = rtx->rxbd[rxIdx].cbd_datlen - 4;
  409. memcpy (packet,
  410. (void *) (NetRxPackets[rxIdx]),
  411. length < max_length ? length : max_length);
  412. }
  413. /* Give the buffer back to the SCC. */
  414. rtx->rxbd[rxIdx].cbd_datlen = 0;
  415. /* wrap around buffer index when necessary */
  416. if ((rxIdx + 1) >= PKTBUFSRX) {
  417. rtx->rxbd[PKTBUFSRX - 1].cbd_sc =
  418. (BD_ENET_RX_WRAP | BD_ENET_RX_EMPTY);
  419. rxIdx = 0;
  420. } else {
  421. rtx->rxbd[rxIdx].cbd_sc = BD_ENET_RX_EMPTY;
  422. rxIdx++;
  423. }
  424. Done:
  425. return length;
  426. }
  427. /*
  428. * Test routines
  429. */
  430. static void packet_fill (char *packet, int length)
  431. {
  432. char c = (char) length;
  433. int i;
  434. packet[0] = 0xFF;
  435. packet[1] = 0xFF;
  436. packet[2] = 0xFF;
  437. packet[3] = 0xFF;
  438. packet[4] = 0xFF;
  439. packet[5] = 0xFF;
  440. for (i = 6; i < length; i++) {
  441. packet[i] = c++;
  442. }
  443. }
  444. static int packet_check (char *packet, int length)
  445. {
  446. char c = (char) length;
  447. int i;
  448. for (i = 6; i < length; i++) {
  449. if (packet[i] != c++)
  450. return -1;
  451. }
  452. return 0;
  453. }
  454. static int test_ctlr (int ctlr, int index)
  455. {
  456. int res = -1;
  457. char packet_send[MAX_PACKET_LENGTH];
  458. char packet_recv[MAX_PACKET_LENGTH];
  459. int length;
  460. int i;
  461. int l;
  462. ctlr_proc[ctlr].init (index);
  463. for (i = 0; i < TEST_NUM; i++) {
  464. for (l = MIN_PACKET_LENGTH; l <= MAX_PACKET_LENGTH; l++) {
  465. packet_fill (packet_send, l);
  466. ctlr_proc[ctlr].send (index, packet_send, l);
  467. length = ctlr_proc[ctlr].recv (index, packet_recv,
  468. MAX_PACKET_LENGTH);
  469. if (length != l || packet_check (packet_recv, length) < 0) {
  470. goto Done;
  471. }
  472. }
  473. }
  474. res = 0;
  475. Done:
  476. ctlr_proc[ctlr].halt (index);
  477. /*
  478. * SCC2 Ethernet parameter RAM space overlaps
  479. * the SPI parameter RAM space. So we need to restore
  480. * the SPI configuration after SCC2 ethernet test.
  481. */
  482. #if defined(CONFIG_SPI)
  483. if (ctlr == CTLR_SCC && index == 1) {
  484. spi_init_f ();
  485. spi_init_r ();
  486. }
  487. #endif
  488. if (res != 0) {
  489. post_log ("ethernet %s%d test failed\n", ctlr_name[ctlr],
  490. index + 1);
  491. }
  492. return res;
  493. }
  494. int ether_post_test (int flags)
  495. {
  496. int res = 0;
  497. int i;
  498. ctlr_proc[CTLR_SCC].init = scc_init;
  499. ctlr_proc[CTLR_SCC].halt = scc_halt;
  500. ctlr_proc[CTLR_SCC].send = scc_send;
  501. ctlr_proc[CTLR_SCC].recv = scc_recv;
  502. for (i = 0; i < CTRL_LIST_SIZE; i++) {
  503. if (test_ctlr (ctlr_list[i][0], ctlr_list[i][1]) != 0) {
  504. res = -1;
  505. }
  506. }
  507. #if !defined(CONFIG_8xx_CONS_NONE)
  508. serial_reinit_all ();
  509. #endif
  510. return res;
  511. }
  512. #endif /* CONFIG_POST & CONFIG_SYS_POST_ETHER */