ether_fcc.c 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347
  1. /*
  2. * MPC8260 FCC Fast Ethernet
  3. *
  4. * Copyright (c) 2000 MontaVista Software, Inc. Dan Malek (dmalek@jlc.net)
  5. *
  6. * (C) Copyright 2000 Sysgo Real-Time Solutions, GmbH <www.elinos.com>
  7. * Marius Groeger <mgroeger@sysgo.de>
  8. *
  9. * See file CREDITS for list of people who contributed to this
  10. * project.
  11. *
  12. * This program is free software; you can redistribute it and/or
  13. * modify it under the terms of the GNU General Public License as
  14. * published by the Free Software Foundation; either version 2 of
  15. * the License, or (at your option) any later version.
  16. *
  17. * This program is distributed in the hope that it will be useful,
  18. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  19. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  20. * GNU General Public License for more details.
  21. *
  22. * You should have received a copy of the GNU General Public License
  23. * along with this program; if not, write to the Free Software
  24. * Foundation, Inc., 59 Temple Place, Suite 330, Boston,
  25. * MA 02111-1307 USA
  26. */
  27. /*
  28. * MPC8260 FCC Fast Ethernet
  29. * Basic ET HW initialization and packet RX/TX routines
  30. *
  31. * This code will not perform the IO port configuration. This should be
  32. * done in the iop_conf_t structure specific for the board.
  33. *
  34. * TODO:
  35. * add a PHY driver to do the negotiation
  36. * reflect negotiation results in FPSMR
  37. * look for ways to configure the board specific stuff elsewhere, eg.
  38. * config_xxx.h or the board directory
  39. */
  40. #include <common.h>
  41. #include <asm/cpm_8260.h>
  42. #include <mpc8260.h>
  43. #include <net.h>
  44. #include <command.h>
  45. #include <config.h>
  46. #if defined(CONFIG_ETHER_ON_FCC) && (CONFIG_COMMANDS & CFG_CMD_NET)
  47. /*---------------------------------------------------------------------*/
  48. #if (CONFIG_ETHER_INDEX == 1)
  49. #define PROFF_ENET PROFF_FCC1
  50. #define CPM_CR_ENET_SBLOCK CPM_CR_FCC1_SBLOCK
  51. #define CPM_CR_ENET_SBLOCK CPM_CR_FCC1_SBLOCK
  52. #define CPM_CR_ENET_PAGE CPM_CR_FCC1_PAGE
  53. /*---------------------------------------------------------------------*/
  54. #elif (CONFIG_ETHER_INDEX == 2)
  55. #define PROFF_ENET PROFF_FCC2
  56. #define CPM_CR_ENET_SBLOCK CPM_CR_FCC2_SBLOCK
  57. #define CPM_CR_ENET_PAGE CPM_CR_FCC2_PAGE
  58. /*---------------------------------------------------------------------*/
  59. #elif (CONFIG_ETHER_INDEX == 3)
  60. #define PROFF_ENET PROFF_FCC3
  61. #define CPM_CR_ENET_SBLOCK CPM_CR_FCC3_SBLOCK
  62. #define CPM_CR_ENET_PAGE CPM_CR_FCC3_PAGE
  63. /*---------------------------------------------------------------------*/
  64. #else
  65. #error "FCC Ethernet not correctly defined"
  66. #endif
  67. /*---------------------------------------------------------------------*/
  68. /* Maximum input DMA size. Must be a should(?) be a multiple of 4. */
  69. #define PKT_MAXDMA_SIZE 1520
  70. /* The FCC stores dest/src/type, data, and checksum for receive packets. */
  71. #define PKT_MAXBUF_SIZE 1518
  72. #define PKT_MINBUF_SIZE 64
  73. /* Maximum input buffer size. Must be a multiple of 32. */
  74. #define PKT_MAXBLR_SIZE 1536
  75. #define TOUT_LOOP 1000000
  76. #define TX_BUF_CNT 2
  77. #ifdef __GNUC__
  78. static char txbuf[TX_BUF_CNT][PKT_MAXBLR_SIZE] __attribute__ ((aligned(8)));
  79. #else
  80. #error "txbuf must be 64-bit aligned"
  81. #endif
  82. static uint rxIdx; /* index of the current RX buffer */
  83. static uint txIdx; /* index of the current TX buffer */
  84. /*
  85. * FCC Ethernet Tx and Rx buffer descriptors.
  86. * Provide for Double Buffering
  87. * Note: PKTBUFSRX is defined in net.h
  88. */
  89. typedef volatile struct rtxbd {
  90. cbd_t rxbd[PKTBUFSRX];
  91. cbd_t txbd[TX_BUF_CNT];
  92. } RTXBD;
  93. /* Good news: the FCC supports external BDs! */
  94. #ifdef __GNUC__
  95. static RTXBD rtx __attribute__ ((aligned(8)));
  96. #else
  97. #error "rtx must be 64-bit aligned"
  98. #endif
  99. int eth_send(volatile void *packet, int length)
  100. {
  101. int i;
  102. int result = 0;
  103. if (length <= 0) {
  104. printf("fec: bad packet size: %d\n", length);
  105. goto out;
  106. }
  107. for(i=0; rtx.txbd[txIdx].cbd_sc & BD_ENET_TX_READY; i++) {
  108. if (i >= TOUT_LOOP) {
  109. printf("fec: tx buffer not ready\n");
  110. goto out;
  111. }
  112. }
  113. rtx.txbd[txIdx].cbd_bufaddr = (uint)packet;
  114. rtx.txbd[txIdx].cbd_datlen = length;
  115. rtx.txbd[txIdx].cbd_sc |= (BD_ENET_TX_READY | BD_ENET_TX_LAST |
  116. BD_ENET_TX_WRAP);
  117. for(i=0; rtx.txbd[txIdx].cbd_sc & BD_ENET_TX_READY; i++) {
  118. if (i >= TOUT_LOOP) {
  119. printf("fec: tx error\n");
  120. goto out;
  121. }
  122. }
  123. #ifdef ET_DEBUG
  124. printf("cycles: %d status: %04x\n", i, rtx.txbd[txIdx].cbd_sc);
  125. #endif
  126. /* return only status bits */
  127. result = rtx.txbd[txIdx].cbd_sc & BD_ENET_TX_STATS;
  128. out:
  129. return result;
  130. }
  131. int eth_rx(void)
  132. {
  133. int length;
  134. for (;;)
  135. {
  136. if (rtx.rxbd[rxIdx].cbd_sc & BD_ENET_RX_EMPTY) {
  137. length = -1;
  138. break; /* nothing received - leave for() loop */
  139. }
  140. length = rtx.rxbd[rxIdx].cbd_datlen;
  141. if (rtx.rxbd[rxIdx].cbd_sc & 0x003f) {
  142. printf("fec: rx error %04x\n", rtx.rxbd[rxIdx].cbd_sc);
  143. }
  144. else {
  145. /* Pass the packet up to the protocol layers. */
  146. NetReceive(NetRxPackets[rxIdx], length - 4);
  147. }
  148. /* Give the buffer back to the FCC. */
  149. rtx.rxbd[rxIdx].cbd_datlen = 0;
  150. /* wrap around buffer index when necessary */
  151. if ((rxIdx + 1) >= PKTBUFSRX) {
  152. rtx.rxbd[PKTBUFSRX - 1].cbd_sc = (BD_ENET_RX_WRAP | BD_ENET_RX_EMPTY);
  153. rxIdx = 0;
  154. }
  155. else {
  156. rtx.rxbd[rxIdx].cbd_sc = BD_ENET_RX_EMPTY;
  157. rxIdx++;
  158. }
  159. }
  160. return length;
  161. }
  162. int eth_init(bd_t *bis)
  163. {
  164. int i;
  165. volatile immap_t *immr = (immap_t *)CFG_IMMR;
  166. volatile cpm8260_t *cp = &(immr->im_cpm);
  167. fcc_enet_t *pram_ptr;
  168. unsigned long mem_addr;
  169. #if 0
  170. mii_discover_phy();
  171. #endif
  172. /* 28.9 - (1-2): ioports have been set up already */
  173. /* 28.9 - (3): connect FCC's tx and rx clocks */
  174. immr->im_cpmux.cmx_uar = 0;
  175. immr->im_cpmux.cmx_fcr = (immr->im_cpmux.cmx_fcr & ~CFG_CMXFCR_MASK) |
  176. CFG_CMXFCR_VALUE;
  177. /* 28.9 - (4): GFMR: disable tx/rx, CCITT CRC, Mode Ethernet */
  178. immr->im_fcc[CONFIG_ETHER_INDEX-1].fcc_gfmr =
  179. FCC_GFMR_MODE_ENET | FCC_GFMR_TCRC_32;
  180. /* 28.9 - (5): FPSMR: enable full duplex, select CCITT CRC for Ethernet */
  181. immr->im_fcc[CONFIG_ETHER_INDEX-1].fcc_fpsmr = CFG_FCC_PSMR | FCC_PSMR_ENCRC;
  182. /* 28.9 - (6): FDSR: Ethernet Syn */
  183. immr->im_fcc[CONFIG_ETHER_INDEX-1].fcc_fdsr = 0xD555;
  184. /* reset indeces to current rx/tx bd (see eth_send()/eth_rx()) */
  185. rxIdx = 0;
  186. txIdx = 0;
  187. /* Setup Receiver Buffer Descriptors */
  188. for (i = 0; i < PKTBUFSRX; i++)
  189. {
  190. rtx.rxbd[i].cbd_sc = BD_ENET_RX_EMPTY;
  191. rtx.rxbd[i].cbd_datlen = 0;
  192. rtx.rxbd[i].cbd_bufaddr = (uint)NetRxPackets[i];
  193. }
  194. rtx.rxbd[PKTBUFSRX - 1].cbd_sc |= BD_ENET_RX_WRAP;
  195. /* Setup Ethernet Transmitter Buffer Descriptors */
  196. for (i = 0; i < TX_BUF_CNT; i++)
  197. {
  198. rtx.txbd[i].cbd_sc = (BD_ENET_TX_PAD | BD_ENET_TX_LAST | BD_ENET_TX_TC);
  199. rtx.txbd[i].cbd_datlen = 0;
  200. rtx.txbd[i].cbd_bufaddr = (uint)&txbuf[i][0];
  201. }
  202. rtx.txbd[TX_BUF_CNT - 1].cbd_sc |= BD_ENET_TX_WRAP;
  203. /* 28.9 - (7): initialise parameter ram */
  204. pram_ptr = (fcc_enet_t *)&(immr->im_dprambase[PROFF_ENET]);
  205. /* clear whole structure to make sure all reserved fields are zero */
  206. memset((void*)pram_ptr, 0, sizeof(fcc_enet_t));
  207. /*
  208. * common Parameter RAM area
  209. *
  210. * Allocate space in the reserved FCC area of DPRAM for the
  211. * internal buffers. No one uses this space (yet), so we
  212. * can do this. Later, we will add resource management for
  213. * this area.
  214. */
  215. mem_addr = CPM_FCC_SPECIAL_BASE + ((CONFIG_ETHER_INDEX-1) * 64);
  216. pram_ptr->fen_genfcc.fcc_riptr = mem_addr;
  217. pram_ptr->fen_genfcc.fcc_tiptr = mem_addr+32;
  218. /*
  219. * Set maximum bytes per receive buffer.
  220. * It must be a multiple of 32.
  221. */
  222. pram_ptr->fen_genfcc.fcc_mrblr = PKT_MAXBLR_SIZE;
  223. pram_ptr->fen_genfcc.fcc_rstate = (CPMFCR_GBL | CPMFCR_EB |
  224. CFG_CPMFCR_RAMTYPE) << 24;
  225. pram_ptr->fen_genfcc.fcc_rbase = (unsigned int)(&rtx.rxbd[rxIdx]);
  226. pram_ptr->fen_genfcc.fcc_tstate = (CPMFCR_GBL | CPMFCR_EB |
  227. CFG_CPMFCR_RAMTYPE) << 24;
  228. pram_ptr->fen_genfcc.fcc_tbase = (unsigned int)(&rtx.txbd[txIdx]);
  229. /* protocol-specific area */
  230. pram_ptr->fen_cmask = 0xdebb20e3; /* CRC mask */
  231. pram_ptr->fen_cpres = 0xffffffff; /* CRC preset */
  232. pram_ptr->fen_retlim = 15; /* Retry limit threshold */
  233. pram_ptr->fen_mflr = PKT_MAXBUF_SIZE; /* maximum frame length register */
  234. /*
  235. * Set Ethernet station address.
  236. *
  237. * This is supplied in the board information structure, so we
  238. * copy that into the controller.
  239. * So, far we have only been given one Ethernet address. We make
  240. * it unique by setting a few bits in the upper byte of the
  241. * non-static part of the address.
  242. */
  243. #define ea bis->bi_enetaddr
  244. pram_ptr->fen_paddrh = (ea[5] << 8) + ea[4];
  245. pram_ptr->fen_paddrm = (ea[3] << 8) + ea[2];
  246. pram_ptr->fen_paddrl = (ea[1] << 8) + ea[0];
  247. #undef ea
  248. pram_ptr->fen_minflr = PKT_MINBUF_SIZE; /* minimum frame length register */
  249. /* pad pointer. use tiptr since we don't need a specific padding char */
  250. pram_ptr->fen_padptr = pram_ptr->fen_genfcc.fcc_tiptr;
  251. pram_ptr->fen_maxd1 = PKT_MAXDMA_SIZE; /* maximum DMA1 length */
  252. pram_ptr->fen_maxd2 = PKT_MAXDMA_SIZE; /* maximum DMA2 length */
  253. pram_ptr->fen_rfthr = 1;
  254. pram_ptr->fen_rfcnt = 1;
  255. #if 0
  256. printf("pram_ptr->fen_genfcc.fcc_rbase %08lx\n",
  257. pram_ptr->fen_genfcc.fcc_rbase);
  258. printf("pram_ptr->fen_genfcc.fcc_tbase %08lx\n",
  259. pram_ptr->fen_genfcc.fcc_tbase);
  260. #endif
  261. /* 28.9 - (8): clear out events in FCCE */
  262. immr->im_fcc[CONFIG_ETHER_INDEX-1].fcc_fcce = ~0x0;
  263. /* 28.9 - (9): FCCM: mask all events */
  264. immr->im_fcc[CONFIG_ETHER_INDEX-1].fcc_fccm = 0;
  265. /* 28.9 - (10-12): we don't use ethernet interrupts */
  266. /* 28.9 - (13)
  267. *
  268. * Let's re-initialize the channel now. We have to do it later
  269. * than the manual describes because we have just now finished
  270. * the BD initialization.
  271. */
  272. cp->cp_cpcr = mk_cr_cmd(CPM_CR_ENET_PAGE,
  273. CPM_CR_ENET_SBLOCK,
  274. 0x0c,
  275. CPM_CR_INIT_TRX) | CPM_CR_FLG;
  276. do {
  277. __asm__ __volatile__ ("eieio");
  278. } while (cp->cp_cpcr & CPM_CR_FLG);
  279. /* 28.9 - (14): enable tx/rx in gfmr */
  280. immr->im_fcc[CONFIG_ETHER_INDEX-1].fcc_gfmr |= FCC_GFMR_ENT | FCC_GFMR_ENR;
  281. return 1;
  282. }
  283. void eth_halt(void)
  284. {
  285. volatile immap_t *immr = (immap_t *)CFG_IMMR;
  286. /* write GFMR: disable tx/rx */
  287. immr->im_fcc[CONFIG_ETHER_INDEX-1].fcc_gfmr &=
  288. ~(FCC_GFMR_ENT | FCC_GFMR_ENR);
  289. }
  290. #endif /* CONFIG_ETHER_ON_FCC && CFG_CMD_NET */