ether.c 14 KB

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