ether_fcc.c 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394
  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 <malloc.h>
  42. #include <asm/cpm_8260.h>
  43. #include <mpc8260.h>
  44. #include <command.h>
  45. #include <config.h>
  46. #include <net.h>
  47. #if defined(CONFIG_ETHER_ON_FCC) && (CONFIG_COMMANDS & CFG_CMD_NET) && \
  48. defined(CONFIG_NET_MULTI)
  49. static struct ether_fcc_info_s
  50. {
  51. int ether_index;
  52. int proff_enet;
  53. ulong cpm_cr_enet_sblock;
  54. ulong cpm_cr_enet_page;
  55. ulong cmxfcr_mask;
  56. ulong cmxfcr_value;
  57. }
  58. ether_fcc_info[] =
  59. {
  60. #ifdef CONFIG_ETHER_ON_FCC1
  61. {
  62. 0,
  63. PROFF_FCC1,
  64. CPM_CR_FCC1_SBLOCK,
  65. CPM_CR_FCC1_PAGE,
  66. CFG_CMXFCR_MASK1,
  67. CFG_CMXFCR_VALUE1
  68. },
  69. #endif
  70. #ifdef CONFIG_ETHER_ON_FCC2
  71. {
  72. 1,
  73. PROFF_FCC2,
  74. CPM_CR_FCC2_SBLOCK,
  75. CPM_CR_FCC2_PAGE,
  76. CFG_CMXFCR_MASK2,
  77. CFG_CMXFCR_VALUE2
  78. },
  79. #endif
  80. #ifdef CONFIG_ETHER_ON_FCC3
  81. {
  82. 2,
  83. PROFF_FCC3,
  84. CPM_CR_FCC3_SBLOCK,
  85. CPM_CR_FCC3_PAGE,
  86. CFG_CMXFCR_MASK3,
  87. CFG_CMXFCR_VALUE3
  88. },
  89. #endif
  90. };
  91. /*---------------------------------------------------------------------*/
  92. /* Maximum input DMA size. Must be a should(?) be a multiple of 4. */
  93. #define PKT_MAXDMA_SIZE 1520
  94. /* The FCC stores dest/src/type, data, and checksum for receive packets. */
  95. #define PKT_MAXBUF_SIZE 1518
  96. #define PKT_MINBUF_SIZE 64
  97. /* Maximum input buffer size. Must be a multiple of 32. */
  98. #define PKT_MAXBLR_SIZE 1536
  99. #define TOUT_LOOP 1000000
  100. #define TX_BUF_CNT 2
  101. #ifdef __GNUC__
  102. static char txbuf[TX_BUF_CNT][PKT_MAXBLR_SIZE] __attribute__ ((aligned(8)));
  103. #else
  104. #error "txbuf must be 64-bit aligned"
  105. #endif
  106. static uint rxIdx; /* index of the current RX buffer */
  107. static uint txIdx; /* index of the current TX buffer */
  108. /*
  109. * FCC Ethernet Tx and Rx buffer descriptors.
  110. * Provide for Double Buffering
  111. * Note: PKTBUFSRX is defined in net.h
  112. */
  113. typedef volatile struct rtxbd {
  114. cbd_t rxbd[PKTBUFSRX];
  115. cbd_t txbd[TX_BUF_CNT];
  116. } RTXBD;
  117. /* Good news: the FCC supports external BDs! */
  118. #ifdef __GNUC__
  119. static RTXBD rtx __attribute__ ((aligned(8)));
  120. #else
  121. #error "rtx must be 64-bit aligned"
  122. #endif
  123. static int fec_send(struct eth_device* dev, volatile void *packet, int length)
  124. {
  125. int i;
  126. int result = 0;
  127. if (length <= 0) {
  128. printf("fec: bad packet size: %d\n", length);
  129. goto out;
  130. }
  131. for(i=0; rtx.txbd[txIdx].cbd_sc & BD_ENET_TX_READY; i++) {
  132. if (i >= TOUT_LOOP) {
  133. printf("fec: tx buffer not ready\n");
  134. goto out;
  135. }
  136. }
  137. rtx.txbd[txIdx].cbd_bufaddr = (uint)packet;
  138. rtx.txbd[txIdx].cbd_datlen = length;
  139. rtx.txbd[txIdx].cbd_sc |= (BD_ENET_TX_READY | BD_ENET_TX_LAST |
  140. BD_ENET_TX_WRAP);
  141. for(i=0; rtx.txbd[txIdx].cbd_sc & BD_ENET_TX_READY; i++) {
  142. if (i >= TOUT_LOOP) {
  143. printf("fec: tx error\n");
  144. goto out;
  145. }
  146. }
  147. #ifdef ET_DEBUG
  148. printf("cycles: %d status: %04x\n", i, rtx.txbd[txIdx].cbd_sc);
  149. #endif
  150. /* return only status bits */
  151. result = rtx.txbd[txIdx].cbd_sc & BD_ENET_TX_STATS;
  152. out:
  153. return result;
  154. }
  155. static int fec_recv(struct eth_device* dev)
  156. {
  157. int length;
  158. for (;;)
  159. {
  160. if (rtx.rxbd[rxIdx].cbd_sc & BD_ENET_RX_EMPTY) {
  161. length = -1;
  162. break; /* nothing received - leave for() loop */
  163. }
  164. length = rtx.rxbd[rxIdx].cbd_datlen;
  165. if (rtx.rxbd[rxIdx].cbd_sc & 0x003f) {
  166. printf("fec: rx error %04x\n", rtx.rxbd[rxIdx].cbd_sc);
  167. }
  168. else {
  169. /* Pass the packet up to the protocol layers. */
  170. NetReceive(NetRxPackets[rxIdx], length - 4);
  171. }
  172. /* Give the buffer back to the FCC. */
  173. rtx.rxbd[rxIdx].cbd_datlen = 0;
  174. /* wrap around buffer index when necessary */
  175. if ((rxIdx + 1) >= PKTBUFSRX) {
  176. rtx.rxbd[PKTBUFSRX - 1].cbd_sc = (BD_ENET_RX_WRAP | BD_ENET_RX_EMPTY);
  177. rxIdx = 0;
  178. }
  179. else {
  180. rtx.rxbd[rxIdx].cbd_sc = BD_ENET_RX_EMPTY;
  181. rxIdx++;
  182. }
  183. }
  184. return length;
  185. }
  186. static int fec_init(struct eth_device* dev, bd_t *bis)
  187. {
  188. struct ether_fcc_info_s * info = dev->priv;
  189. int i;
  190. volatile immap_t *immr = (immap_t *)CFG_IMMR;
  191. volatile cpm8260_t *cp = &(immr->im_cpm);
  192. fcc_enet_t *pram_ptr;
  193. unsigned long mem_addr;
  194. #if 0
  195. mii_discover_phy();
  196. #endif
  197. /* 28.9 - (1-2): ioports have been set up already */
  198. /* 28.9 - (3): connect FCC's tx and rx clocks */
  199. immr->im_cpmux.cmx_uar = 0;
  200. immr->im_cpmux.cmx_fcr = (immr->im_cpmux.cmx_fcr & ~info->cmxfcr_mask) |
  201. info->cmxfcr_value;
  202. /* 28.9 - (4): GFMR: disable tx/rx, CCITT CRC, Mode Ethernet */
  203. immr->im_fcc[info->ether_index].fcc_gfmr =
  204. FCC_GFMR_MODE_ENET | FCC_GFMR_TCRC_32;
  205. /* 28.9 - (5): FPSMR: enable full duplex, select CCITT CRC for Ethernet */
  206. immr->im_fcc[info->ether_index].fcc_fpsmr = CFG_FCC_PSMR | FCC_PSMR_ENCRC;
  207. /* 28.9 - (6): FDSR: Ethernet Syn */
  208. immr->im_fcc[info->ether_index].fcc_fdsr = 0xD555;
  209. /* reset indeces to current rx/tx bd (see eth_send()/eth_rx()) */
  210. rxIdx = 0;
  211. txIdx = 0;
  212. /* Setup Receiver Buffer Descriptors */
  213. for (i = 0; i < PKTBUFSRX; i++)
  214. {
  215. rtx.rxbd[i].cbd_sc = BD_ENET_RX_EMPTY;
  216. rtx.rxbd[i].cbd_datlen = 0;
  217. rtx.rxbd[i].cbd_bufaddr = (uint)NetRxPackets[i];
  218. }
  219. rtx.rxbd[PKTBUFSRX - 1].cbd_sc |= BD_ENET_RX_WRAP;
  220. /* Setup Ethernet Transmitter Buffer Descriptors */
  221. for (i = 0; i < TX_BUF_CNT; i++)
  222. {
  223. rtx.txbd[i].cbd_sc = (BD_ENET_TX_PAD | BD_ENET_TX_LAST | BD_ENET_TX_TC);
  224. rtx.txbd[i].cbd_datlen = 0;
  225. rtx.txbd[i].cbd_bufaddr = (uint)&txbuf[i][0];
  226. }
  227. rtx.txbd[TX_BUF_CNT - 1].cbd_sc |= BD_ENET_TX_WRAP;
  228. /* 28.9 - (7): initialise parameter ram */
  229. pram_ptr = (fcc_enet_t *)&(immr->im_dprambase[info->proff_enet]);
  230. /* clear whole structure to make sure all reserved fields are zero */
  231. memset((void*)pram_ptr, 0, sizeof(fcc_enet_t));
  232. /*
  233. * common Parameter RAM area
  234. *
  235. * Allocate space in the reserved FCC area of DPRAM for the
  236. * internal buffers. No one uses this space (yet), so we
  237. * can do this. Later, we will add resource management for
  238. * this area.
  239. */
  240. mem_addr = CPM_FCC_SPECIAL_BASE + ((info->ether_index) * 64);
  241. pram_ptr->fen_genfcc.fcc_riptr = mem_addr;
  242. pram_ptr->fen_genfcc.fcc_tiptr = mem_addr+32;
  243. /*
  244. * Set maximum bytes per receive buffer.
  245. * It must be a multiple of 32.
  246. */
  247. pram_ptr->fen_genfcc.fcc_mrblr = PKT_MAXBLR_SIZE;
  248. pram_ptr->fen_genfcc.fcc_rstate = (CPMFCR_GBL | CPMFCR_EB |
  249. CFG_CPMFCR_RAMTYPE) << 24;
  250. pram_ptr->fen_genfcc.fcc_rbase = (unsigned int)(&rtx.rxbd[rxIdx]);
  251. pram_ptr->fen_genfcc.fcc_tstate = (CPMFCR_GBL | CPMFCR_EB |
  252. CFG_CPMFCR_RAMTYPE) << 24;
  253. pram_ptr->fen_genfcc.fcc_tbase = (unsigned int)(&rtx.txbd[txIdx]);
  254. /* protocol-specific area */
  255. pram_ptr->fen_cmask = 0xdebb20e3; /* CRC mask */
  256. pram_ptr->fen_cpres = 0xffffffff; /* CRC preset */
  257. pram_ptr->fen_retlim = 15; /* Retry limit threshold */
  258. pram_ptr->fen_mflr = PKT_MAXBUF_SIZE; /* maximum frame length register */
  259. /*
  260. * Set Ethernet station address.
  261. *
  262. * This is supplied in the board information structure, so we
  263. * copy that into the controller.
  264. * So, far we have only been given one Ethernet address. We make
  265. * it unique by setting a few bits in the upper byte of the
  266. * non-static part of the address.
  267. */
  268. #define ea eth_get_dev()->enetaddr
  269. pram_ptr->fen_paddrh = (ea[5] << 8) + ea[4];
  270. pram_ptr->fen_paddrm = (ea[3] << 8) + ea[2];
  271. pram_ptr->fen_paddrl = (ea[1] << 8) + ea[0];
  272. #undef ea
  273. pram_ptr->fen_minflr = PKT_MINBUF_SIZE; /* minimum frame length register */
  274. /* pad pointer. use tiptr since we don't need a specific padding char */
  275. pram_ptr->fen_padptr = pram_ptr->fen_genfcc.fcc_tiptr;
  276. pram_ptr->fen_maxd1 = PKT_MAXDMA_SIZE; /* maximum DMA1 length */
  277. pram_ptr->fen_maxd2 = PKT_MAXDMA_SIZE; /* maximum DMA2 length */
  278. pram_ptr->fen_rfthr = 1;
  279. pram_ptr->fen_rfcnt = 1;
  280. #if 0
  281. printf("pram_ptr->fen_genfcc.fcc_rbase %08lx\n",
  282. pram_ptr->fen_genfcc.fcc_rbase);
  283. printf("pram_ptr->fen_genfcc.fcc_tbase %08lx\n",
  284. pram_ptr->fen_genfcc.fcc_tbase);
  285. #endif
  286. /* 28.9 - (8): clear out events in FCCE */
  287. immr->im_fcc[info->ether_index].fcc_fcce = ~0x0;
  288. /* 28.9 - (9): FCCM: mask all events */
  289. immr->im_fcc[info->ether_index].fcc_fccm = 0;
  290. /* 28.9 - (10-12): we don't use ethernet interrupts */
  291. /* 28.9 - (13)
  292. *
  293. * Let's re-initialize the channel now. We have to do it later
  294. * than the manual describes because we have just now finished
  295. * the BD initialization.
  296. */
  297. cp->cp_cpcr = mk_cr_cmd(info->cpm_cr_enet_page,
  298. info->cpm_cr_enet_sblock,
  299. 0x0c,
  300. CPM_CR_INIT_TRX) | CPM_CR_FLG;
  301. do {
  302. __asm__ __volatile__ ("eieio");
  303. } while (cp->cp_cpcr & CPM_CR_FLG);
  304. /* 28.9 - (14): enable tx/rx in gfmr */
  305. immr->im_fcc[info->ether_index].fcc_gfmr |= FCC_GFMR_ENT | FCC_GFMR_ENR;
  306. return 1;
  307. }
  308. static void fec_halt(struct eth_device* dev)
  309. {
  310. struct ether_fcc_info_s * info = dev->priv;
  311. volatile immap_t *immr = (immap_t *)CFG_IMMR;
  312. /* write GFMR: disable tx/rx */
  313. immr->im_fcc[info->ether_index].fcc_gfmr &=
  314. ~(FCC_GFMR_ENT | FCC_GFMR_ENR);
  315. }
  316. int fec_initialize(bd_t *bis)
  317. {
  318. struct eth_device* dev;
  319. int i;
  320. for (i = 0; i < sizeof(ether_fcc_info) / sizeof(ether_fcc_info[0]); i++)
  321. {
  322. dev = (struct eth_device*) malloc(sizeof *dev);
  323. memset(dev, 0, sizeof *dev);
  324. sprintf(dev->name, "FCC%d ETHERNET",
  325. ether_fcc_info[i].ether_index + 1);
  326. dev->priv = &ether_fcc_info[i];
  327. dev->init = fec_init;
  328. dev->halt = fec_halt;
  329. dev->send = fec_send;
  330. dev->recv = fec_recv;
  331. eth_register(dev);
  332. }
  333. return 1;
  334. }
  335. #endif /* CONFIG_ETHER_ON_FCC && CFG_CMD_NET && CONFIG_NET_MULTI */