ether.c 13 KB

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