ether_fcc.c 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461
  1. /*
  2. * MPC8560 FCC Fast Ethernet
  3. * Copyright (c) 2003 Motorola,Inc.
  4. * Xianghua Xiao, (X.Xiao@motorola.com)
  5. *
  6. * Copyright (c) 2000 MontaVista Software, Inc. Dan Malek (dmalek@jlc.net)
  7. *
  8. * (C) Copyright 2000 Sysgo Real-Time Solutions, GmbH <www.elinos.com>
  9. * Marius Groeger <mgroeger@sysgo.de>
  10. *
  11. * See file CREDITS for list of people who contributed to this
  12. * project.
  13. *
  14. * This program is free software; you can redistribute it and/or
  15. * modify it under the terms of the GNU General Public License as
  16. * published by the Free Software Foundation; either version 2 of
  17. * the License, or (at your option) any later version.
  18. *
  19. * This program is distributed in the hope that it will be useful,
  20. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  21. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  22. * GNU General Public License for more details.
  23. *
  24. * You should have received a copy of the GNU General Public License
  25. * along with this program; if not, write to the Free Software
  26. * Foundation, Inc., 59 Temple Place, Suite 330, Boston,
  27. * MA 02111-1307 USA
  28. */
  29. /*
  30. * MPC8560 FCC Fast Ethernet
  31. * Basic ET HW initialization and packet RX/TX routines
  32. *
  33. * This code will not perform the IO port configuration. This should be
  34. * done in the iop_conf_t structure specific for the board.
  35. *
  36. * TODO:
  37. * add a PHY driver to do the negotiation
  38. * reflect negotiation results in FPSMR
  39. * look for ways to configure the board specific stuff elsewhere, eg.
  40. * config_xxx.h or the board directory
  41. */
  42. #include <common.h>
  43. #include <malloc.h>
  44. #include <asm/cpm_85xx.h>
  45. #include <command.h>
  46. #include <config.h>
  47. #include <net.h>
  48. #if defined(CONFIG_CPM2)
  49. #if defined(CONFIG_ETHER_ON_FCC) && (CONFIG_COMMANDS & CFG_CMD_NET) && \
  50. defined(CONFIG_NET_MULTI)
  51. static struct ether_fcc_info_s
  52. {
  53. int ether_index;
  54. int proff_enet;
  55. ulong cpm_cr_enet_sblock;
  56. ulong cpm_cr_enet_page;
  57. ulong cmxfcr_mask;
  58. ulong cmxfcr_value;
  59. }
  60. ether_fcc_info[] =
  61. {
  62. #ifdef CONFIG_ETHER_ON_FCC1
  63. {
  64. 0,
  65. PROFF_FCC1,
  66. CPM_CR_FCC1_SBLOCK,
  67. CPM_CR_FCC1_PAGE,
  68. CFG_CMXFCR_MASK1,
  69. CFG_CMXFCR_VALUE1
  70. },
  71. #endif
  72. #ifdef CONFIG_ETHER_ON_FCC2
  73. {
  74. 1,
  75. PROFF_FCC2,
  76. CPM_CR_FCC2_SBLOCK,
  77. CPM_CR_FCC2_PAGE,
  78. CFG_CMXFCR_MASK2,
  79. CFG_CMXFCR_VALUE2
  80. },
  81. #endif
  82. #ifdef CONFIG_ETHER_ON_FCC3
  83. {
  84. 2,
  85. PROFF_FCC3,
  86. CPM_CR_FCC3_SBLOCK,
  87. CPM_CR_FCC3_PAGE,
  88. CFG_CMXFCR_MASK3,
  89. CFG_CMXFCR_VALUE3
  90. },
  91. #endif
  92. };
  93. /*---------------------------------------------------------------------*/
  94. /* Maximum input DMA size. Must be a should(?) be a multiple of 4. */
  95. #define PKT_MAXDMA_SIZE 1520
  96. /* The FCC stores dest/src/type, data, and checksum for receive packets. */
  97. #define PKT_MAXBUF_SIZE 1518
  98. #define PKT_MINBUF_SIZE 64
  99. /* Maximum input buffer size. Must be a multiple of 32. */
  100. #define PKT_MAXBLR_SIZE 1536
  101. #define TOUT_LOOP 1000000
  102. #define TX_BUF_CNT 2
  103. static uint rxIdx; /* index of the current RX buffer */
  104. static uint txIdx; /* index of the current TX buffer */
  105. /*
  106. * FCC Ethernet Tx and Rx buffer descriptors.
  107. * Provide for Double Buffering
  108. * Note: PKTBUFSRX is defined in net.h
  109. */
  110. typedef volatile struct rtxbd {
  111. cbd_t rxbd[PKTBUFSRX];
  112. cbd_t txbd[TX_BUF_CNT];
  113. } RTXBD;
  114. /* Good news: the FCC supports external BDs! */
  115. #ifdef __GNUC__
  116. static RTXBD rtx __attribute__ ((aligned(8)));
  117. #else
  118. #error "rtx must be 64-bit aligned"
  119. #endif
  120. #undef ET_DEBUG
  121. static int fec_send(struct eth_device* dev, volatile void *packet, int length)
  122. {
  123. int i = 0;
  124. int result = 0;
  125. if (length <= 0) {
  126. printf("fec: bad packet size: %d\n", length);
  127. goto out;
  128. }
  129. for(i=0; rtx.txbd[txIdx].cbd_sc & BD_ENET_TX_READY; i++) {
  130. if (i >= TOUT_LOOP) {
  131. printf("fec: tx buffer not ready\n");
  132. goto out;
  133. }
  134. }
  135. rtx.txbd[txIdx].cbd_bufaddr = (uint)packet;
  136. rtx.txbd[txIdx].cbd_datlen = length;
  137. rtx.txbd[txIdx].cbd_sc |= (BD_ENET_TX_READY | BD_ENET_TX_LAST | \
  138. BD_ENET_TX_TC | BD_ENET_TX_PAD);
  139. for(i=0; rtx.txbd[txIdx].cbd_sc & BD_ENET_TX_READY; i++) {
  140. if (i >= TOUT_LOOP) {
  141. printf("fec: tx error\n");
  142. goto out;
  143. }
  144. }
  145. #ifdef ET_DEBUG
  146. printf("cycles: 0x%x txIdx=0x%04x status: 0x%04x\n", i, txIdx,rtx.txbd[txIdx].cbd_sc);
  147. printf("packets at 0x%08x, length_in_bytes=0x%x\n",(uint)packet,length);
  148. for(i=0;i<(length/16 + 1);i++) {
  149. printf("%08x %08x %08x %08x\n",*((uint *)rtx.txbd[txIdx].cbd_bufaddr+i*4),\
  150. *((uint *)rtx.txbd[txIdx].cbd_bufaddr + i*4 + 1),*((uint *)rtx.txbd[txIdx].cbd_bufaddr + i*4 + 2), \
  151. *((uint *)rtx.txbd[txIdx].cbd_bufaddr + i*4 + 3));
  152. }
  153. #endif
  154. /* return only status bits */
  155. result = rtx.txbd[txIdx].cbd_sc & BD_ENET_TX_STATS;
  156. txIdx = (txIdx + 1) % TX_BUF_CNT;
  157. out:
  158. return result;
  159. }
  160. static int fec_recv(struct eth_device* dev)
  161. {
  162. int length;
  163. for (;;)
  164. {
  165. if (rtx.rxbd[rxIdx].cbd_sc & BD_ENET_RX_EMPTY) {
  166. length = -1;
  167. break; /* nothing received - leave for() loop */
  168. }
  169. length = rtx.rxbd[rxIdx].cbd_datlen;
  170. if (rtx.rxbd[rxIdx].cbd_sc & 0x003f) {
  171. printf("fec: rx error %04x\n", rtx.rxbd[rxIdx].cbd_sc);
  172. }
  173. else {
  174. /* Pass the packet up to the protocol layers. */
  175. NetReceive(NetRxPackets[rxIdx], length - 4);
  176. }
  177. /* Give the buffer back to the FCC. */
  178. rtx.rxbd[rxIdx].cbd_datlen = 0;
  179. /* wrap around buffer index when necessary */
  180. if ((rxIdx + 1) >= PKTBUFSRX) {
  181. rtx.rxbd[PKTBUFSRX - 1].cbd_sc = (BD_ENET_RX_WRAP | BD_ENET_RX_EMPTY);
  182. rxIdx = 0;
  183. }
  184. else {
  185. rtx.rxbd[rxIdx].cbd_sc = BD_ENET_RX_EMPTY;
  186. rxIdx++;
  187. }
  188. }
  189. return length;
  190. }
  191. static int fec_init(struct eth_device* dev, bd_t *bis)
  192. {
  193. struct ether_fcc_info_s * info = dev->priv;
  194. int i;
  195. volatile immap_t *immr = (immap_t *)CFG_IMMR;
  196. volatile ccsr_cpm_cp_t *cp = &(immr->im_cpm.im_cpm_cp);
  197. fcc_enet_t *pram_ptr;
  198. unsigned long mem_addr;
  199. #if 0
  200. mii_discover_phy();
  201. #endif
  202. /* 28.9 - (1-2): ioports have been set up already */
  203. /* 28.9 - (3): connect FCC's tx and rx clocks */
  204. immr->im_cpm.im_cpm_mux.cmxuar = 0; /* ATM */
  205. immr->im_cpm.im_cpm_mux.cmxfcr = (immr->im_cpm.im_cpm_mux.cmxfcr & ~info->cmxfcr_mask) |
  206. info->cmxfcr_value;
  207. /* 28.9 - (4): GFMR: disable tx/rx, CCITT CRC, set Mode Ethernet */
  208. if(info->ether_index == 0) {
  209. immr->im_cpm.im_cpm_fcc1.gfmr = FCC_GFMR_MODE_ENET | FCC_GFMR_TCRC_32;
  210. } else if (info->ether_index == 1) {
  211. immr->im_cpm.im_cpm_fcc2.gfmr = FCC_GFMR_MODE_ENET | FCC_GFMR_TCRC_32;
  212. } else if (info->ether_index == 2) {
  213. immr->im_cpm.im_cpm_fcc3.gfmr = FCC_GFMR_MODE_ENET | FCC_GFMR_TCRC_32;
  214. }
  215. /* 28.9 - (5): FPSMR: enable full duplex, select CCITT CRC for Ethernet,MII */
  216. if(info->ether_index == 0) {
  217. immr->im_cpm.im_cpm_fcc1.fpsmr = CFG_FCC_PSMR | FCC_PSMR_ENCRC;
  218. } else if (info->ether_index == 1){
  219. immr->im_cpm.im_cpm_fcc2.fpsmr = CFG_FCC_PSMR | FCC_PSMR_ENCRC;
  220. } else if (info->ether_index == 2){
  221. immr->im_cpm.im_cpm_fcc3.fpsmr = CFG_FCC_PSMR | FCC_PSMR_ENCRC;
  222. }
  223. /* 28.9 - (6): FDSR: Ethernet Syn */
  224. if(info->ether_index == 0) {
  225. immr->im_cpm.im_cpm_fcc1.fdsr = 0xD555;
  226. } else if (info->ether_index == 1) {
  227. immr->im_cpm.im_cpm_fcc2.fdsr = 0xD555;
  228. } else if (info->ether_index == 2) {
  229. immr->im_cpm.im_cpm_fcc3.fdsr = 0xD555;
  230. }
  231. /* reset indeces to current rx/tx bd (see eth_send()/eth_rx()) */
  232. rxIdx = 0;
  233. txIdx = 0;
  234. /* Setup Receiver Buffer Descriptors */
  235. for (i = 0; i < PKTBUFSRX; i++)
  236. {
  237. rtx.rxbd[i].cbd_sc = BD_ENET_RX_EMPTY;
  238. rtx.rxbd[i].cbd_datlen = 0;
  239. rtx.rxbd[i].cbd_bufaddr = (uint)NetRxPackets[i];
  240. }
  241. rtx.rxbd[PKTBUFSRX - 1].cbd_sc |= BD_ENET_RX_WRAP;
  242. /* Setup Ethernet Transmitter Buffer Descriptors */
  243. for (i = 0; i < TX_BUF_CNT; i++)
  244. {
  245. rtx.txbd[i].cbd_sc = 0;
  246. rtx.txbd[i].cbd_datlen = 0;
  247. rtx.txbd[i].cbd_bufaddr = 0;
  248. }
  249. rtx.txbd[TX_BUF_CNT - 1].cbd_sc |= BD_ENET_TX_WRAP;
  250. /* 28.9 - (7): initialize parameter ram */
  251. pram_ptr = (fcc_enet_t *)&(immr->im_cpm.im_dprambase[info->proff_enet]);
  252. /* clear whole structure to make sure all reserved fields are zero */
  253. memset((void*)pram_ptr, 0, sizeof(fcc_enet_t));
  254. /*
  255. * common Parameter RAM area
  256. *
  257. * Allocate space in the reserved FCC area of DPRAM for the
  258. * internal buffers. No one uses this space (yet), so we
  259. * can do this. Later, we will add resource management for
  260. * this area. CPM_FCC_SPECIAL_BASE: 0xb000.
  261. */
  262. mem_addr = CPM_FCC_SPECIAL_BASE + ((info->ether_index) * 64);
  263. pram_ptr->fen_genfcc.fcc_riptr = mem_addr;
  264. pram_ptr->fen_genfcc.fcc_tiptr = mem_addr+32;
  265. /*
  266. * Set maximum bytes per receive buffer.
  267. * It must be a multiple of 32.
  268. */
  269. pram_ptr->fen_genfcc.fcc_mrblr = PKT_MAXBLR_SIZE; /* 1536 */
  270. /* localbus SDRAM should be preferred */
  271. pram_ptr->fen_genfcc.fcc_rstate = (CPMFCR_GBL | CPMFCR_EB |
  272. CFG_CPMFCR_RAMTYPE) << 24;
  273. pram_ptr->fen_genfcc.fcc_rbase = (unsigned int)(&rtx.rxbd[rxIdx]);
  274. pram_ptr->fen_genfcc.fcc_rbdstat = 0;
  275. pram_ptr->fen_genfcc.fcc_rbdlen = 0;
  276. pram_ptr->fen_genfcc.fcc_rdptr = 0;
  277. /* localbus SDRAM should be preferred */
  278. pram_ptr->fen_genfcc.fcc_tstate = (CPMFCR_GBL | CPMFCR_EB |
  279. CFG_CPMFCR_RAMTYPE) << 24;
  280. pram_ptr->fen_genfcc.fcc_tbase = (unsigned int)(&rtx.txbd[txIdx]);
  281. pram_ptr->fen_genfcc.fcc_tbdstat = 0;
  282. pram_ptr->fen_genfcc.fcc_tbdlen = 0;
  283. pram_ptr->fen_genfcc.fcc_tdptr = 0;
  284. /* protocol-specific area */
  285. pram_ptr->fen_statbuf = 0x0;
  286. pram_ptr->fen_cmask = 0xdebb20e3; /* CRC mask */
  287. pram_ptr->fen_cpres = 0xffffffff; /* CRC preset */
  288. pram_ptr->fen_crcec = 0;
  289. pram_ptr->fen_alec = 0;
  290. pram_ptr->fen_disfc = 0;
  291. pram_ptr->fen_retlim = 15; /* Retry limit threshold */
  292. pram_ptr->fen_retcnt = 0;
  293. pram_ptr->fen_pper = 0;
  294. pram_ptr->fen_boffcnt = 0;
  295. pram_ptr->fen_gaddrh = 0;
  296. pram_ptr->fen_gaddrl = 0;
  297. pram_ptr->fen_mflr = PKT_MAXBUF_SIZE; /* maximum frame length register */
  298. /*
  299. * Set Ethernet station address.
  300. *
  301. * This is supplied in the board information structure, so we
  302. * copy that into the controller.
  303. * So far we have only been given one Ethernet address. We make
  304. * it unique by setting a few bits in the upper byte of the
  305. * non-static part of the address.
  306. */
  307. #define ea eth_get_dev()->enetaddr
  308. pram_ptr->fen_paddrh = (ea[5] << 8) + ea[4];
  309. pram_ptr->fen_paddrm = (ea[3] << 8) + ea[2];
  310. pram_ptr->fen_paddrl = (ea[1] << 8) + ea[0];
  311. #undef ea
  312. pram_ptr->fen_ibdcount = 0;
  313. pram_ptr->fen_ibdstart = 0;
  314. pram_ptr->fen_ibdend = 0;
  315. pram_ptr->fen_txlen = 0;
  316. pram_ptr->fen_iaddrh = 0; /* disable hash */
  317. pram_ptr->fen_iaddrl = 0;
  318. pram_ptr->fen_minflr = PKT_MINBUF_SIZE; /* minimum frame length register: 64 */
  319. /* pad pointer. use tiptr since we don't need a specific padding char */
  320. pram_ptr->fen_padptr = pram_ptr->fen_genfcc.fcc_tiptr;
  321. pram_ptr->fen_maxd1 = PKT_MAXDMA_SIZE; /* maximum DMA1 length:1520 */
  322. pram_ptr->fen_maxd2 = PKT_MAXDMA_SIZE; /* maximum DMA2 length:1520 */
  323. #if defined(ET_DEBUG)
  324. printf("parm_ptr(0xff788500) = %p\n",pram_ptr);
  325. printf("pram_ptr->fen_genfcc.fcc_rbase %08x\n",
  326. pram_ptr->fen_genfcc.fcc_rbase);
  327. printf("pram_ptr->fen_genfcc.fcc_tbase %08x\n",
  328. pram_ptr->fen_genfcc.fcc_tbase);
  329. #endif
  330. /* 28.9 - (8)(9): clear out events in FCCE */
  331. /* 28.9 - (9): FCCM: mask all events */
  332. if(info->ether_index == 0) {
  333. immr->im_cpm.im_cpm_fcc1.fcce = ~0x0;
  334. immr->im_cpm.im_cpm_fcc1.fccm = 0;
  335. } else if (info->ether_index == 1) {
  336. immr->im_cpm.im_cpm_fcc2.fcce = ~0x0;
  337. immr->im_cpm.im_cpm_fcc2.fccm = 0;
  338. } else if (info->ether_index == 2) {
  339. immr->im_cpm.im_cpm_fcc3.fcce = ~0x0;
  340. immr->im_cpm.im_cpm_fcc3.fccm = 0;
  341. }
  342. /* 28.9 - (10-12): we don't use ethernet interrupts */
  343. /* 28.9 - (13)
  344. *
  345. * Let's re-initialize the channel now. We have to do it later
  346. * than the manual describes because we have just now finished
  347. * the BD initialization.
  348. */
  349. cp->cpcr = mk_cr_cmd(info->cpm_cr_enet_page,
  350. info->cpm_cr_enet_sblock,
  351. 0x0c,
  352. CPM_CR_INIT_TRX) | CPM_CR_FLG;
  353. do {
  354. __asm__ __volatile__ ("eieio");
  355. } while (cp->cpcr & CPM_CR_FLG);
  356. /* 28.9 - (14): enable tx/rx in gfmr */
  357. if(info->ether_index == 0) {
  358. immr->im_cpm.im_cpm_fcc1.gfmr |= FCC_GFMR_ENT | FCC_GFMR_ENR;
  359. } else if (info->ether_index == 1) {
  360. immr->im_cpm.im_cpm_fcc2.gfmr |= FCC_GFMR_ENT | FCC_GFMR_ENR;
  361. } else if (info->ether_index == 2) {
  362. immr->im_cpm.im_cpm_fcc3.gfmr |= FCC_GFMR_ENT | FCC_GFMR_ENR;
  363. }
  364. return 1;
  365. }
  366. static void fec_halt(struct eth_device* dev)
  367. {
  368. struct ether_fcc_info_s * info = dev->priv;
  369. volatile immap_t *immr = (immap_t *)CFG_IMMR;
  370. /* write GFMR: disable tx/rx */
  371. if(info->ether_index == 0) {
  372. immr->im_cpm.im_cpm_fcc1.gfmr &= ~(FCC_GFMR_ENT | FCC_GFMR_ENR);
  373. } else if(info->ether_index == 1) {
  374. immr->im_cpm.im_cpm_fcc2.gfmr &= ~(FCC_GFMR_ENT | FCC_GFMR_ENR);
  375. } else if(info->ether_index == 2) {
  376. immr->im_cpm.im_cpm_fcc3.gfmr &= ~(FCC_GFMR_ENT | FCC_GFMR_ENR);
  377. }
  378. }
  379. int fec_initialize(bd_t *bis)
  380. {
  381. struct eth_device* dev;
  382. int i;
  383. for (i = 0; i < sizeof(ether_fcc_info) / sizeof(ether_fcc_info[0]); i++)
  384. {
  385. dev = (struct eth_device*) malloc(sizeof *dev);
  386. memset(dev, 0, sizeof *dev);
  387. sprintf(dev->name, "FCC%d ETHERNET",
  388. ether_fcc_info[i].ether_index + 1);
  389. dev->priv = &ether_fcc_info[i];
  390. dev->init = fec_init;
  391. dev->halt = fec_halt;
  392. dev->send = fec_send;
  393. dev->recv = fec_recv;
  394. eth_register(dev);
  395. }
  396. return 1;
  397. }
  398. #endif /* CONFIG_ETHER_ON_FCC && CFG_CMD_NET && CONFIG_NET_MULTI */
  399. #endif /* CONFIG_CPM2 */