ether.c 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557
  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. /*
  296. * Set the ENT/ENR bits in the GSMR Low -- Enable Transmit/Receive
  297. */
  298. immr->im_cpm.cp_scc[scc_index].scc_gsmrl |=
  299. (SCC_GSMRL_ENR | SCC_GSMRL_ENT);
  300. /*
  301. * Work around transmit problem with first eth packet
  302. */
  303. #if defined (CONFIG_FADS)
  304. udelay (10000); /* wait 10 ms */
  305. #endif
  306. }
  307. static void scc_halt (int scc_index)
  308. {
  309. volatile immap_t *immr = (immap_t *) CONFIG_SYS_IMMR;
  310. immr->im_cpm.cp_scc[scc_index].scc_gsmrl &=
  311. ~(SCC_GSMRL_ENR | SCC_GSMRL_ENT);
  312. immr->im_ioport.iop_pcso &= ~(PC_ENET_CLSN | PC_ENET_RENA);
  313. }
  314. static int scc_send (int index, volatile void *packet, int length)
  315. {
  316. int i, j = 0;
  317. while ((rtx->txbd[txIdx].cbd_sc & BD_ENET_TX_READY) && (j < TOUT_LOOP)) {
  318. udelay (1); /* will also trigger Wd if needed */
  319. j++;
  320. }
  321. if (j >= TOUT_LOOP)
  322. printf ("TX not ready\n");
  323. rtx->txbd[txIdx].cbd_bufaddr = (uint) packet;
  324. rtx->txbd[txIdx].cbd_datlen = length;
  325. rtx->txbd[txIdx].cbd_sc |=
  326. (BD_ENET_TX_READY | BD_ENET_TX_LAST | BD_ENET_TX_WRAP);
  327. while ((rtx->txbd[txIdx].cbd_sc & BD_ENET_TX_READY) && (j < TOUT_LOOP)) {
  328. udelay (1); /* will also trigger Wd if needed */
  329. j++;
  330. }
  331. if (j >= TOUT_LOOP)
  332. printf ("TX timeout\n");
  333. i = (rtx->txbd[txIdx].
  334. cbd_sc & BD_ENET_TX_STATS) /* return only status bits */ ;
  335. return i;
  336. }
  337. static int scc_recv (int index, void *packet, int max_length)
  338. {
  339. int length = -1;
  340. if (rtx->rxbd[rxIdx].cbd_sc & BD_ENET_RX_EMPTY) {
  341. goto Done; /* nothing received */
  342. }
  343. if (!(rtx->rxbd[rxIdx].cbd_sc & 0x003f)) {
  344. length = rtx->rxbd[rxIdx].cbd_datlen - 4;
  345. memcpy (packet,
  346. (void *) (NetRxPackets[rxIdx]),
  347. length < max_length ? length : max_length);
  348. }
  349. /* Give the buffer back to the SCC. */
  350. rtx->rxbd[rxIdx].cbd_datlen = 0;
  351. /* wrap around buffer index when necessary */
  352. if ((rxIdx + 1) >= PKTBUFSRX) {
  353. rtx->rxbd[PKTBUFSRX - 1].cbd_sc =
  354. (BD_ENET_RX_WRAP | BD_ENET_RX_EMPTY);
  355. rxIdx = 0;
  356. } else {
  357. rtx->rxbd[rxIdx].cbd_sc = BD_ENET_RX_EMPTY;
  358. rxIdx++;
  359. }
  360. Done:
  361. return length;
  362. }
  363. /*
  364. * Test routines
  365. */
  366. static void packet_fill (char *packet, int length)
  367. {
  368. char c = (char) length;
  369. int i;
  370. packet[0] = 0xFF;
  371. packet[1] = 0xFF;
  372. packet[2] = 0xFF;
  373. packet[3] = 0xFF;
  374. packet[4] = 0xFF;
  375. packet[5] = 0xFF;
  376. for (i = 6; i < length; i++) {
  377. packet[i] = c++;
  378. }
  379. }
  380. static int packet_check (char *packet, int length)
  381. {
  382. char c = (char) length;
  383. int i;
  384. for (i = 6; i < length; i++) {
  385. if (packet[i] != c++)
  386. return -1;
  387. }
  388. return 0;
  389. }
  390. static int test_ctlr (int ctlr, int index)
  391. {
  392. int res = -1;
  393. char packet_send[MAX_PACKET_LENGTH];
  394. char packet_recv[MAX_PACKET_LENGTH];
  395. int length;
  396. int i;
  397. int l;
  398. ctlr_proc[ctlr].init (index);
  399. for (i = 0; i < TEST_NUM; i++) {
  400. for (l = MIN_PACKET_LENGTH; l <= MAX_PACKET_LENGTH; l++) {
  401. packet_fill (packet_send, l);
  402. ctlr_proc[ctlr].send (index, packet_send, l);
  403. length = ctlr_proc[ctlr].recv (index, packet_recv,
  404. MAX_PACKET_LENGTH);
  405. if (length != l || packet_check (packet_recv, length) < 0) {
  406. goto Done;
  407. }
  408. }
  409. }
  410. res = 0;
  411. Done:
  412. ctlr_proc[ctlr].halt (index);
  413. /*
  414. * SCC2 Ethernet parameter RAM space overlaps
  415. * the SPI parameter RAM space. So we need to restore
  416. * the SPI configuration after SCC2 ethernet test.
  417. */
  418. #if defined(CONFIG_SPI)
  419. if (ctlr == CTLR_SCC && index == 1) {
  420. spi_init_f ();
  421. spi_init_r ();
  422. }
  423. #endif
  424. if (res != 0) {
  425. post_log ("ethernet %s%d test failed\n", ctlr_name[ctlr],
  426. index + 1);
  427. }
  428. return res;
  429. }
  430. int ether_post_test (int flags)
  431. {
  432. int res = 0;
  433. int i;
  434. ctlr_proc[CTLR_SCC].init = scc_init;
  435. ctlr_proc[CTLR_SCC].halt = scc_halt;
  436. ctlr_proc[CTLR_SCC].send = scc_send;
  437. ctlr_proc[CTLR_SCC].recv = scc_recv;
  438. for (i = 0; i < ARRAY_SIZE(ctlr_list); i++) {
  439. if (test_ctlr (ctlr_list[i][0], ctlr_list[i][1]) != 0) {
  440. res = -1;
  441. }
  442. }
  443. #if !defined(CONFIG_8xx_CONS_NONE)
  444. serial_reinit_all ();
  445. #endif
  446. return res;
  447. }
  448. #endif /* CONFIG_POST & CONFIG_SYS_POST_ETHER */