scc.c 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477
  1. /*
  2. * File: scc.c
  3. * Description:
  4. * Basic ET HW initialization and packet RX/TX routines
  5. *
  6. * NOTE <<<IMPORTANT: PLEASE READ>>>:
  7. * Do not cache Rx/Tx buffers!
  8. */
  9. /*
  10. * MPC823 <-> MC68160 Connections:
  11. *
  12. * Setup MPC823 to work with MC68160 Enhanced Ethernet
  13. * Serial Tranceiver as follows:
  14. *
  15. * MPC823 Signal MC68160 Comments
  16. * ------ ------ ------- --------
  17. * PA-12 ETHTX --------> TX Eth. Port Transmit Data
  18. * PB-18 E_TENA --------> TENA Eth. Transmit Port Enable
  19. * PA-5 ETHTCK <-------- TCLK Eth. Port Transmit Clock
  20. * PA-13 ETHRX <-------- RX Eth. Port Receive Data
  21. * PC-8 E_RENA <-------- RENA Eth. Receive Enable
  22. * PA-6 ETHRCK <-------- RCLK Eth. Port Receive Clock
  23. * PC-9 E_CLSN <-------- CLSN Eth. Port Collision Indication
  24. *
  25. * FADS Board Signal MC68160 Comments
  26. * ----------------- ------- --------
  27. * (BCSR1) ETHEN* --------> CS2 Eth. Port Enable
  28. * (BSCR4) TPSQEL* --------> TPSQEL Twisted Pair Signal Quality Error Test Enable
  29. * (BCSR4) TPFLDL* --------> TPFLDL Twisted Pair Full-Duplex
  30. * (BCSR4) ETHLOOP --------> LOOP Eth. Port Diagnostic Loop-Back
  31. *
  32. */
  33. #include <common.h>
  34. #include <malloc.h>
  35. #include <commproc.h>
  36. #include <net.h>
  37. #include <command.h>
  38. #if defined(CONFIG_CMD_NET) && defined(SCC_ENET)
  39. /* Ethernet Transmit and Receive Buffers */
  40. #define DBUF_LENGTH 1520
  41. #define TX_BUF_CNT 2
  42. #define TOUT_LOOP 10000 /* 10 ms to have a packet sent */
  43. static char txbuf[DBUF_LENGTH];
  44. static uint rxIdx; /* index of the current RX buffer */
  45. static uint txIdx; /* index of the current TX buffer */
  46. /*
  47. * SCC Ethernet Tx and Rx buffer descriptors allocated at the
  48. * immr->udata_bd address on Dual-Port RAM
  49. * Provide for Double Buffering
  50. */
  51. typedef volatile struct CommonBufferDescriptor {
  52. cbd_t rxbd[PKTBUFSRX]; /* Rx BD */
  53. cbd_t txbd[TX_BUF_CNT]; /* Tx BD */
  54. } RTXBD;
  55. static RTXBD *rtx;
  56. static int scc_send(struct eth_device *dev, void *packet, int length);
  57. static int scc_recv(struct eth_device* dev);
  58. static int scc_init (struct eth_device* dev, bd_t * bd);
  59. static void scc_halt(struct eth_device* dev);
  60. int scc_initialize(bd_t *bis)
  61. {
  62. struct eth_device* dev;
  63. dev = (struct eth_device*) malloc(sizeof *dev);
  64. memset(dev, 0, sizeof *dev);
  65. sprintf(dev->name, "SCC");
  66. dev->iobase = 0;
  67. dev->priv = 0;
  68. dev->init = scc_init;
  69. dev->halt = scc_halt;
  70. dev->send = scc_send;
  71. dev->recv = scc_recv;
  72. eth_register(dev);
  73. return 1;
  74. }
  75. static int scc_send(struct eth_device *dev, void *packet, int length)
  76. {
  77. int i, j=0;
  78. #if 0
  79. volatile char *in, *out;
  80. #endif
  81. /* section 16.9.23.3
  82. * Wait for ready
  83. */
  84. #if 0
  85. while (rtx->txbd[txIdx].cbd_sc & BD_ENET_TX_READY);
  86. out = (char *)(rtx->txbd[txIdx].cbd_bufaddr);
  87. in = packet;
  88. for(i = 0; i < length; i++) {
  89. *out++ = *in++;
  90. }
  91. rtx->txbd[txIdx].cbd_datlen = length;
  92. rtx->txbd[txIdx].cbd_sc |= (BD_ENET_TX_READY | BD_ENET_TX_LAST);
  93. while (rtx->txbd[txIdx].cbd_sc & BD_ENET_TX_READY) j++;
  94. #ifdef ET_DEBUG
  95. printf("cycles: %d status: %x\n", j, rtx->txbd[txIdx].cbd_sc);
  96. #endif
  97. i = (rtx->txbd[txIdx++].cbd_sc & BD_ENET_TX_STATS) /* return only status bits */;
  98. /* wrap around buffer index when necessary */
  99. if (txIdx >= TX_BUF_CNT) txIdx = 0;
  100. #endif
  101. while ((rtx->txbd[txIdx].cbd_sc & BD_ENET_TX_READY) && (j<TOUT_LOOP)) {
  102. udelay (1); /* will also trigger Wd if needed */
  103. j++;
  104. }
  105. if (j>=TOUT_LOOP) printf("TX not ready\n");
  106. rtx->txbd[txIdx].cbd_bufaddr = (uint)packet;
  107. rtx->txbd[txIdx].cbd_datlen = length;
  108. rtx->txbd[txIdx].cbd_sc |= (BD_ENET_TX_READY | BD_ENET_TX_LAST |BD_ENET_TX_WRAP);
  109. while ((rtx->txbd[txIdx].cbd_sc & BD_ENET_TX_READY) && (j<TOUT_LOOP)) {
  110. udelay (1); /* will also trigger Wd if needed */
  111. j++;
  112. }
  113. if (j>=TOUT_LOOP) printf("TX timeout\n");
  114. #ifdef ET_DEBUG
  115. printf("cycles: %d status: %x\n", j, rtx->txbd[txIdx].cbd_sc);
  116. #endif
  117. i = (rtx->txbd[txIdx].cbd_sc & BD_ENET_TX_STATS) /* return only status bits */;
  118. return i;
  119. }
  120. static int scc_recv (struct eth_device *dev)
  121. {
  122. int length;
  123. for (;;) {
  124. /* section 16.9.23.2 */
  125. if (rtx->rxbd[rxIdx].cbd_sc & BD_ENET_RX_EMPTY) {
  126. length = -1;
  127. break; /* nothing received - leave for() loop */
  128. }
  129. length = rtx->rxbd[rxIdx].cbd_datlen;
  130. if (rtx->rxbd[rxIdx].cbd_sc & 0x003f) {
  131. #ifdef ET_DEBUG
  132. printf ("err: %x\n", rtx->rxbd[rxIdx].cbd_sc);
  133. #endif
  134. } else {
  135. /* Pass the packet up to the protocol layers. */
  136. NetReceive (NetRxPackets[rxIdx], length - 4);
  137. }
  138. /* Give the buffer back to the SCC. */
  139. rtx->rxbd[rxIdx].cbd_datlen = 0;
  140. /* wrap around buffer index when necessary */
  141. if ((rxIdx + 1) >= PKTBUFSRX) {
  142. rtx->rxbd[PKTBUFSRX - 1].cbd_sc =
  143. (BD_ENET_RX_WRAP | BD_ENET_RX_EMPTY);
  144. rxIdx = 0;
  145. } else {
  146. rtx->rxbd[rxIdx].cbd_sc = BD_ENET_RX_EMPTY;
  147. rxIdx++;
  148. }
  149. }
  150. return length;
  151. }
  152. /**************************************************************
  153. *
  154. * SCC Ethernet Initialization Routine
  155. *
  156. *************************************************************/
  157. static int scc_init (struct eth_device *dev, bd_t * bis)
  158. {
  159. int i;
  160. scc_enet_t *pram_ptr;
  161. volatile immap_t *immr = (immap_t *) CONFIG_SYS_IMMR;
  162. pram_ptr = (scc_enet_t *) & (immr->im_cpm.cp_dparam[PROFF_ENET]);
  163. rxIdx = 0;
  164. txIdx = 0;
  165. if (!rtx) {
  166. #ifdef CONFIG_SYS_ALLOC_DPRAM
  167. rtx = (RTXBD *) (immr->im_cpm.cp_dpmem +
  168. dpram_alloc_align (sizeof (RTXBD), 8));
  169. #else
  170. rtx = (RTXBD *) (immr->im_cpm.cp_dpmem + CPM_SCC_BASE);
  171. #endif
  172. }
  173. #if (defined(PA_ENET_RXD) && defined(PA_ENET_TXD))
  174. /* Configure port A pins for Txd and Rxd.
  175. */
  176. immr->im_ioport.iop_papar |= (PA_ENET_RXD | PA_ENET_TXD);
  177. immr->im_ioport.iop_padir &= ~(PA_ENET_RXD | PA_ENET_TXD);
  178. immr->im_ioport.iop_paodr &= ~PA_ENET_TXD;
  179. #elif (defined(PB_ENET_RXD) && defined(PB_ENET_TXD))
  180. /* Configure port B pins for Txd and Rxd.
  181. */
  182. immr->im_cpm.cp_pbpar |= (PB_ENET_RXD | PB_ENET_TXD);
  183. immr->im_cpm.cp_pbdir &= ~(PB_ENET_RXD | PB_ENET_TXD);
  184. immr->im_cpm.cp_pbodr &= ~PB_ENET_TXD;
  185. #else
  186. #error Configuration Error: exactly ONE of PA_ENET_[RT]XD, PB_ENET_[RT]XD must be defined
  187. #endif
  188. #if defined(PC_ENET_LBK)
  189. /* Configure port C pins to disable External Loopback
  190. */
  191. immr->im_ioport.iop_pcpar &= ~PC_ENET_LBK;
  192. immr->im_ioport.iop_pcdir |= PC_ENET_LBK;
  193. immr->im_ioport.iop_pcso &= ~PC_ENET_LBK;
  194. immr->im_ioport.iop_pcdat &= ~PC_ENET_LBK; /* Disable Loopback */
  195. #endif /* PC_ENET_LBK */
  196. /* Configure port C pins to enable CLSN and RENA.
  197. */
  198. immr->im_ioport.iop_pcpar &= ~(PC_ENET_CLSN | PC_ENET_RENA);
  199. immr->im_ioport.iop_pcdir &= ~(PC_ENET_CLSN | PC_ENET_RENA);
  200. immr->im_ioport.iop_pcso |= (PC_ENET_CLSN | PC_ENET_RENA);
  201. /* Configure port A for TCLK and RCLK.
  202. */
  203. immr->im_ioport.iop_papar |= (PA_ENET_TCLK | PA_ENET_RCLK);
  204. immr->im_ioport.iop_padir &= ~(PA_ENET_TCLK | PA_ENET_RCLK);
  205. /*
  206. * Configure Serial Interface clock routing -- see section 16.7.5.3
  207. * First, clear all SCC bits to zero, then set the ones we want.
  208. */
  209. immr->im_cpm.cp_sicr &= ~SICR_ENET_MASK;
  210. immr->im_cpm.cp_sicr |= SICR_ENET_CLKRT;
  211. /*
  212. * Initialize SDCR -- see section 16.9.23.7
  213. * SDMA configuration register
  214. */
  215. immr->im_siu_conf.sc_sdcr = 0x01;
  216. /*
  217. * Setup SCC Ethernet Parameter RAM
  218. */
  219. pram_ptr->sen_genscc.scc_rfcr = 0x18; /* Normal Operation and Mot byte ordering */
  220. pram_ptr->sen_genscc.scc_tfcr = 0x18; /* Mot byte ordering, Normal access */
  221. pram_ptr->sen_genscc.scc_mrblr = DBUF_LENGTH; /* max. ET package len 1520 */
  222. pram_ptr->sen_genscc.scc_rbase = (unsigned int) (&rtx->rxbd[0]); /* Set RXBD tbl start at Dual Port */
  223. pram_ptr->sen_genscc.scc_tbase = (unsigned int) (&rtx->txbd[0]); /* Set TXBD tbl start at Dual Port */
  224. /*
  225. * Setup Receiver Buffer Descriptors (13.14.24.18)
  226. * Settings:
  227. * Empty, Wrap
  228. */
  229. for (i = 0; i < PKTBUFSRX; i++) {
  230. rtx->rxbd[i].cbd_sc = BD_ENET_RX_EMPTY;
  231. rtx->rxbd[i].cbd_datlen = 0; /* Reset */
  232. rtx->rxbd[i].cbd_bufaddr = (uint) NetRxPackets[i];
  233. }
  234. rtx->rxbd[PKTBUFSRX - 1].cbd_sc |= BD_ENET_RX_WRAP;
  235. /*
  236. * Setup Ethernet Transmitter Buffer Descriptors (13.14.24.19)
  237. * Settings:
  238. * Add PADs to Short FRAMES, Wrap, Last, Tx CRC
  239. */
  240. for (i = 0; i < TX_BUF_CNT; i++) {
  241. rtx->txbd[i].cbd_sc =
  242. (BD_ENET_TX_PAD | BD_ENET_TX_LAST | BD_ENET_TX_TC);
  243. rtx->txbd[i].cbd_datlen = 0; /* Reset */
  244. rtx->txbd[i].cbd_bufaddr = (uint) (&txbuf[0]);
  245. }
  246. rtx->txbd[TX_BUF_CNT - 1].cbd_sc |= BD_ENET_TX_WRAP;
  247. /*
  248. * Enter Command: Initialize Rx Params for SCC
  249. */
  250. do { /* Spin until ready to issue command */
  251. __asm__ ("eieio");
  252. } while (immr->im_cpm.cp_cpcr & CPM_CR_FLG);
  253. /* Issue command */
  254. immr->im_cpm.cp_cpcr =
  255. ((CPM_CR_INIT_RX << 8) | (CPM_CR_ENET << 4) | CPM_CR_FLG);
  256. do { /* Spin until command processed */
  257. __asm__ ("eieio");
  258. } while (immr->im_cpm.cp_cpcr & CPM_CR_FLG);
  259. /*
  260. * Ethernet Specific Parameter RAM
  261. * see table 13-16, pg. 660,
  262. * pg. 681 (example with suggested settings)
  263. */
  264. pram_ptr->sen_cpres = ~(0x0); /* Preset CRC */
  265. pram_ptr->sen_cmask = 0xdebb20e3; /* Constant Mask for CRC */
  266. pram_ptr->sen_crcec = 0x0; /* Error Counter CRC (unused) */
  267. pram_ptr->sen_alec = 0x0; /* Alignment Error Counter (unused) */
  268. pram_ptr->sen_disfc = 0x0; /* Discard Frame Counter (unused) */
  269. pram_ptr->sen_pads = 0x8888; /* Short Frame PAD Characters */
  270. pram_ptr->sen_retlim = 15; /* Retry Limit Threshold */
  271. pram_ptr->sen_maxflr = 1518; /* MAX Frame Length Register */
  272. pram_ptr->sen_minflr = 64; /* MIN Frame Length Register */
  273. pram_ptr->sen_maxd1 = DBUF_LENGTH; /* MAX DMA1 Length Register */
  274. pram_ptr->sen_maxd2 = DBUF_LENGTH; /* MAX DMA2 Length Register */
  275. pram_ptr->sen_gaddr1 = 0x0; /* Group Address Filter 1 (unused) */
  276. pram_ptr->sen_gaddr2 = 0x0; /* Group Address Filter 2 (unused) */
  277. pram_ptr->sen_gaddr3 = 0x0; /* Group Address Filter 3 (unused) */
  278. pram_ptr->sen_gaddr4 = 0x0; /* Group Address Filter 4 (unused) */
  279. #define ea eth_get_dev()->enetaddr
  280. pram_ptr->sen_paddrh = (ea[5] << 8) + ea[4];
  281. pram_ptr->sen_paddrm = (ea[3] << 8) + ea[2];
  282. pram_ptr->sen_paddrl = (ea[1] << 8) + ea[0];
  283. #undef ea
  284. pram_ptr->sen_pper = 0x0; /* Persistence (unused) */
  285. pram_ptr->sen_iaddr1 = 0x0; /* Individual Address Filter 1 (unused) */
  286. pram_ptr->sen_iaddr2 = 0x0; /* Individual Address Filter 2 (unused) */
  287. pram_ptr->sen_iaddr3 = 0x0; /* Individual Address Filter 3 (unused) */
  288. pram_ptr->sen_iaddr4 = 0x0; /* Individual Address Filter 4 (unused) */
  289. pram_ptr->sen_taddrh = 0x0; /* Tmp Address (MSB) (unused) */
  290. pram_ptr->sen_taddrm = 0x0; /* Tmp Address (unused) */
  291. pram_ptr->sen_taddrl = 0x0; /* Tmp Address (LSB) (unused) */
  292. /*
  293. * Enter Command: Initialize Tx Params for SCC
  294. */
  295. do { /* Spin until ready to issue command */
  296. __asm__ ("eieio");
  297. } while (immr->im_cpm.cp_cpcr & CPM_CR_FLG);
  298. /* Issue command */
  299. immr->im_cpm.cp_cpcr =
  300. ((CPM_CR_INIT_TX << 8) | (CPM_CR_ENET << 4) | CPM_CR_FLG);
  301. do { /* Spin until command processed */
  302. __asm__ ("eieio");
  303. } while (immr->im_cpm.cp_cpcr & CPM_CR_FLG);
  304. /*
  305. * Mask all Events in SCCM - we use polling mode
  306. */
  307. immr->im_cpm.cp_scc[SCC_ENET].scc_sccm = 0;
  308. /*
  309. * Clear Events in SCCE -- Clear bits by writing 1's
  310. */
  311. immr->im_cpm.cp_scc[SCC_ENET].scc_scce = ~(0x0);
  312. /*
  313. * Initialize GSMR High 32-Bits
  314. * Settings: Normal Mode
  315. */
  316. immr->im_cpm.cp_scc[SCC_ENET].scc_gsmrh = 0;
  317. /*
  318. * Initialize GSMR Low 32-Bits, but do not Enable Transmit/Receive
  319. * Settings:
  320. * TCI = Invert
  321. * TPL = 48 bits
  322. * TPP = Repeating 10's
  323. * MODE = Ethernet
  324. */
  325. immr->im_cpm.cp_scc[SCC_ENET].scc_gsmrl = (SCC_GSMRL_TCI |
  326. SCC_GSMRL_TPL_48 |
  327. SCC_GSMRL_TPP_10 |
  328. SCC_GSMRL_MODE_ENET);
  329. /*
  330. * Initialize the DSR -- see section 13.14.4 (pg. 513) v0.4
  331. */
  332. immr->im_cpm.cp_scc[SCC_ENET].scc_dsr = 0xd555;
  333. /*
  334. * Initialize the PSMR
  335. * Settings:
  336. * CRC = 32-Bit CCITT
  337. * NIB = Begin searching for SFD 22 bits after RENA
  338. * FDE = Full Duplex Enable
  339. * LPB = Loopback Enable (Needed when FDE is set)
  340. * BRO = Reject broadcast packets
  341. * PROMISCOUS = Catch all packets regardless of dest. MAC adress
  342. */
  343. immr->im_cpm.cp_scc[SCC_ENET].scc_psmr = SCC_PSMR_ENCRC |
  344. SCC_PSMR_NIB22 |
  345. #if defined(CONFIG_SCC_ENET_FULL_DUPLEX)
  346. SCC_PSMR_FDE | SCC_PSMR_LPB |
  347. #endif
  348. #if defined(CONFIG_SCC_ENET_NO_BROADCAST)
  349. SCC_PSMR_BRO |
  350. #endif
  351. #if defined(CONFIG_SCC_ENET_PROMISCOUS)
  352. SCC_PSMR_PRO |
  353. #endif
  354. 0;
  355. /*
  356. * Configure Ethernet TENA Signal
  357. */
  358. #if (defined(PC_ENET_TENA) && !defined(PB_ENET_TENA))
  359. immr->im_ioport.iop_pcpar |= PC_ENET_TENA;
  360. immr->im_ioport.iop_pcdir &= ~PC_ENET_TENA;
  361. #elif (defined(PB_ENET_TENA) && !defined(PC_ENET_TENA))
  362. immr->im_cpm.cp_pbpar |= PB_ENET_TENA;
  363. immr->im_cpm.cp_pbdir |= PB_ENET_TENA;
  364. #else
  365. #error Configuration Error: exactly ONE of PB_ENET_TENA, PC_ENET_TENA must be defined
  366. #endif
  367. /*
  368. * Set the ENT/ENR bits in the GSMR Low -- Enable Transmit/Receive
  369. */
  370. immr->im_cpm.cp_scc[SCC_ENET].scc_gsmrl |=
  371. (SCC_GSMRL_ENR | SCC_GSMRL_ENT);
  372. return 1;
  373. }
  374. static void scc_halt (struct eth_device *dev)
  375. {
  376. volatile immap_t *immr = (immap_t *) CONFIG_SYS_IMMR;
  377. immr->im_cpm.cp_scc[SCC_ENET].scc_gsmrl &=
  378. ~(SCC_GSMRL_ENR | SCC_GSMRL_ENT);
  379. immr->im_ioport.iop_pcso &= ~(PC_ENET_CLSN | PC_ENET_RENA);
  380. }
  381. #if 0
  382. void restart (void)
  383. {
  384. volatile immap_t *immr = (immap_t *) CONFIG_SYS_IMMR;
  385. immr->im_cpm.cp_scc[SCC_ENET].scc_gsmrl |=
  386. (SCC_GSMRL_ENR | SCC_GSMRL_ENT);
  387. }
  388. #endif
  389. #endif