ether.c 16 KB

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