ether_fcc.c 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473
  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_MII) || defined(CONFIG_CMD_MII)
  49. #include <miiphy.h>
  50. #endif
  51. #if defined(CONFIG_CPM2)
  52. #if defined(CONFIG_ETHER_ON_FCC) && defined(CONFIG_CMD_NET) && \
  53. defined(CONFIG_NET_MULTI)
  54. static struct ether_fcc_info_s
  55. {
  56. int ether_index;
  57. int proff_enet;
  58. ulong cpm_cr_enet_sblock;
  59. ulong cpm_cr_enet_page;
  60. ulong cmxfcr_mask;
  61. ulong cmxfcr_value;
  62. }
  63. ether_fcc_info[] =
  64. {
  65. #ifdef CONFIG_ETHER_ON_FCC1
  66. {
  67. 0,
  68. PROFF_FCC1,
  69. CPM_CR_FCC1_SBLOCK,
  70. CPM_CR_FCC1_PAGE,
  71. CFG_CMXFCR_MASK1,
  72. CFG_CMXFCR_VALUE1
  73. },
  74. #endif
  75. #ifdef CONFIG_ETHER_ON_FCC2
  76. {
  77. 1,
  78. PROFF_FCC2,
  79. CPM_CR_FCC2_SBLOCK,
  80. CPM_CR_FCC2_PAGE,
  81. CFG_CMXFCR_MASK2,
  82. CFG_CMXFCR_VALUE2
  83. },
  84. #endif
  85. #ifdef CONFIG_ETHER_ON_FCC3
  86. {
  87. 2,
  88. PROFF_FCC3,
  89. CPM_CR_FCC3_SBLOCK,
  90. CPM_CR_FCC3_PAGE,
  91. CFG_CMXFCR_MASK3,
  92. CFG_CMXFCR_VALUE3
  93. },
  94. #endif
  95. };
  96. /*---------------------------------------------------------------------*/
  97. /* Maximum input DMA size. Must be a should(?) be a multiple of 4. */
  98. #define PKT_MAXDMA_SIZE 1520
  99. /* The FCC stores dest/src/type, data, and checksum for receive packets. */
  100. #define PKT_MAXBUF_SIZE 1518
  101. #define PKT_MINBUF_SIZE 64
  102. /* Maximum input buffer size. Must be a multiple of 32. */
  103. #define PKT_MAXBLR_SIZE 1536
  104. #define TOUT_LOOP 1000000
  105. #define TX_BUF_CNT 2
  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. #undef ET_DEBUG
  124. static int fec_send(struct eth_device* dev, volatile void *packet, int length)
  125. {
  126. int i = 0;
  127. int result = 0;
  128. if (length <= 0) {
  129. printf("fec: bad packet size: %d\n", length);
  130. goto out;
  131. }
  132. for(i=0; rtx.txbd[txIdx].cbd_sc & BD_ENET_TX_READY; i++) {
  133. if (i >= TOUT_LOOP) {
  134. printf("fec: tx buffer not ready\n");
  135. goto out;
  136. }
  137. }
  138. rtx.txbd[txIdx].cbd_bufaddr = (uint)packet;
  139. rtx.txbd[txIdx].cbd_datlen = length;
  140. rtx.txbd[txIdx].cbd_sc |= (BD_ENET_TX_READY | BD_ENET_TX_LAST | \
  141. BD_ENET_TX_TC | BD_ENET_TX_PAD);
  142. for(i=0; rtx.txbd[txIdx].cbd_sc & BD_ENET_TX_READY; i++) {
  143. if (i >= TOUT_LOOP) {
  144. printf("fec: tx error\n");
  145. goto out;
  146. }
  147. }
  148. #ifdef ET_DEBUG
  149. printf("cycles: 0x%x txIdx=0x%04x status: 0x%04x\n", i, txIdx,rtx.txbd[txIdx].cbd_sc);
  150. printf("packets at 0x%08x, length_in_bytes=0x%x\n",(uint)packet,length);
  151. for(i=0;i<(length/16 + 1);i++) {
  152. printf("%08x %08x %08x %08x\n",*((uint *)rtx.txbd[txIdx].cbd_bufaddr+i*4),\
  153. *((uint *)rtx.txbd[txIdx].cbd_bufaddr + i*4 + 1),*((uint *)rtx.txbd[txIdx].cbd_bufaddr + i*4 + 2), \
  154. *((uint *)rtx.txbd[txIdx].cbd_bufaddr + i*4 + 3));
  155. }
  156. #endif
  157. /* return only status bits */
  158. result = rtx.txbd[txIdx].cbd_sc & BD_ENET_TX_STATS;
  159. txIdx = (txIdx + 1) % TX_BUF_CNT;
  160. out:
  161. return result;
  162. }
  163. static int fec_recv(struct eth_device* dev)
  164. {
  165. int length;
  166. for (;;)
  167. {
  168. if (rtx.rxbd[rxIdx].cbd_sc & BD_ENET_RX_EMPTY) {
  169. length = -1;
  170. break; /* nothing received - leave for() loop */
  171. }
  172. length = rtx.rxbd[rxIdx].cbd_datlen;
  173. if (rtx.rxbd[rxIdx].cbd_sc & 0x003f) {
  174. printf("fec: rx error %04x\n", rtx.rxbd[rxIdx].cbd_sc);
  175. }
  176. else {
  177. /* Pass the packet up to the protocol layers. */
  178. NetReceive(NetRxPackets[rxIdx], length - 4);
  179. }
  180. /* Give the buffer back to the FCC. */
  181. rtx.rxbd[rxIdx].cbd_datlen = 0;
  182. /* wrap around buffer index when necessary */
  183. if ((rxIdx + 1) >= PKTBUFSRX) {
  184. rtx.rxbd[PKTBUFSRX - 1].cbd_sc = (BD_ENET_RX_WRAP | BD_ENET_RX_EMPTY);
  185. rxIdx = 0;
  186. }
  187. else {
  188. rtx.rxbd[rxIdx].cbd_sc = BD_ENET_RX_EMPTY;
  189. rxIdx++;
  190. }
  191. }
  192. return length;
  193. }
  194. static int fec_init(struct eth_device* dev, bd_t *bis)
  195. {
  196. struct ether_fcc_info_s * info = dev->priv;
  197. int i;
  198. volatile immap_t *immr = (immap_t *)CFG_IMMR;
  199. volatile ccsr_cpm_cp_t *cp = &(immr->im_cpm.im_cpm_cp);
  200. fcc_enet_t *pram_ptr;
  201. unsigned long mem_addr;
  202. #if 0
  203. mii_discover_phy();
  204. #endif
  205. /* 28.9 - (1-2): ioports have been set up already */
  206. /* 28.9 - (3): connect FCC's tx and rx clocks */
  207. immr->im_cpm.im_cpm_mux.cmxuar = 0; /* ATM */
  208. immr->im_cpm.im_cpm_mux.cmxfcr = (immr->im_cpm.im_cpm_mux.cmxfcr & ~info->cmxfcr_mask) |
  209. info->cmxfcr_value;
  210. /* 28.9 - (4): GFMR: disable tx/rx, CCITT CRC, set Mode Ethernet */
  211. if(info->ether_index == 0) {
  212. immr->im_cpm.im_cpm_fcc1.gfmr = FCC_GFMR_MODE_ENET | FCC_GFMR_TCRC_32;
  213. } else if (info->ether_index == 1) {
  214. immr->im_cpm.im_cpm_fcc2.gfmr = FCC_GFMR_MODE_ENET | FCC_GFMR_TCRC_32;
  215. } else if (info->ether_index == 2) {
  216. immr->im_cpm.im_cpm_fcc3.gfmr = FCC_GFMR_MODE_ENET | FCC_GFMR_TCRC_32;
  217. }
  218. /* 28.9 - (5): FPSMR: enable full duplex, select CCITT CRC for Ethernet,MII */
  219. if(info->ether_index == 0) {
  220. immr->im_cpm.im_cpm_fcc1.fpsmr = CFG_FCC_PSMR | FCC_PSMR_ENCRC;
  221. } else if (info->ether_index == 1){
  222. immr->im_cpm.im_cpm_fcc2.fpsmr = CFG_FCC_PSMR | FCC_PSMR_ENCRC;
  223. } else if (info->ether_index == 2){
  224. immr->im_cpm.im_cpm_fcc3.fpsmr = CFG_FCC_PSMR | FCC_PSMR_ENCRC;
  225. }
  226. /* 28.9 - (6): FDSR: Ethernet Syn */
  227. if(info->ether_index == 0) {
  228. immr->im_cpm.im_cpm_fcc1.fdsr = 0xD555;
  229. } else if (info->ether_index == 1) {
  230. immr->im_cpm.im_cpm_fcc2.fdsr = 0xD555;
  231. } else if (info->ether_index == 2) {
  232. immr->im_cpm.im_cpm_fcc3.fdsr = 0xD555;
  233. }
  234. /* reset indeces to current rx/tx bd (see eth_send()/eth_rx()) */
  235. rxIdx = 0;
  236. txIdx = 0;
  237. /* Setup Receiver Buffer Descriptors */
  238. for (i = 0; i < PKTBUFSRX; i++)
  239. {
  240. rtx.rxbd[i].cbd_sc = BD_ENET_RX_EMPTY;
  241. rtx.rxbd[i].cbd_datlen = 0;
  242. rtx.rxbd[i].cbd_bufaddr = (uint)NetRxPackets[i];
  243. }
  244. rtx.rxbd[PKTBUFSRX - 1].cbd_sc |= BD_ENET_RX_WRAP;
  245. /* Setup Ethernet Transmitter Buffer Descriptors */
  246. for (i = 0; i < TX_BUF_CNT; i++)
  247. {
  248. rtx.txbd[i].cbd_sc = 0;
  249. rtx.txbd[i].cbd_datlen = 0;
  250. rtx.txbd[i].cbd_bufaddr = 0;
  251. }
  252. rtx.txbd[TX_BUF_CNT - 1].cbd_sc |= BD_ENET_TX_WRAP;
  253. /* 28.9 - (7): initialize parameter ram */
  254. pram_ptr = (fcc_enet_t *)&(immr->im_cpm.im_dprambase[info->proff_enet]);
  255. /* clear whole structure to make sure all reserved fields are zero */
  256. memset((void*)pram_ptr, 0, sizeof(fcc_enet_t));
  257. /*
  258. * common Parameter RAM area
  259. *
  260. * Allocate space in the reserved FCC area of DPRAM for the
  261. * internal buffers. No one uses this space (yet), so we
  262. * can do this. Later, we will add resource management for
  263. * this area.
  264. * CPM_FCC_SPECIAL_BASE: 0xB000 for MPC8540, MPC8560
  265. * 0x9000 for MPC8541, MPC8555
  266. */
  267. mem_addr = CPM_FCC_SPECIAL_BASE + ((info->ether_index) * 64);
  268. pram_ptr->fen_genfcc.fcc_riptr = mem_addr;
  269. pram_ptr->fen_genfcc.fcc_tiptr = mem_addr+32;
  270. /*
  271. * Set maximum bytes per receive buffer.
  272. * It must be a multiple of 32.
  273. */
  274. pram_ptr->fen_genfcc.fcc_mrblr = PKT_MAXBLR_SIZE; /* 1536 */
  275. /* localbus SDRAM should be preferred */
  276. pram_ptr->fen_genfcc.fcc_rstate = (CPMFCR_GBL | CPMFCR_EB |
  277. CFG_CPMFCR_RAMTYPE) << 24;
  278. pram_ptr->fen_genfcc.fcc_rbase = (unsigned int)(&rtx.rxbd[rxIdx]);
  279. pram_ptr->fen_genfcc.fcc_rbdstat = 0;
  280. pram_ptr->fen_genfcc.fcc_rbdlen = 0;
  281. pram_ptr->fen_genfcc.fcc_rdptr = 0;
  282. /* localbus SDRAM should be preferred */
  283. pram_ptr->fen_genfcc.fcc_tstate = (CPMFCR_GBL | CPMFCR_EB |
  284. CFG_CPMFCR_RAMTYPE) << 24;
  285. pram_ptr->fen_genfcc.fcc_tbase = (unsigned int)(&rtx.txbd[txIdx]);
  286. pram_ptr->fen_genfcc.fcc_tbdstat = 0;
  287. pram_ptr->fen_genfcc.fcc_tbdlen = 0;
  288. pram_ptr->fen_genfcc.fcc_tdptr = 0;
  289. /* protocol-specific area */
  290. pram_ptr->fen_statbuf = 0x0;
  291. pram_ptr->fen_cmask = 0xdebb20e3; /* CRC mask */
  292. pram_ptr->fen_cpres = 0xffffffff; /* CRC preset */
  293. pram_ptr->fen_crcec = 0;
  294. pram_ptr->fen_alec = 0;
  295. pram_ptr->fen_disfc = 0;
  296. pram_ptr->fen_retlim = 15; /* Retry limit threshold */
  297. pram_ptr->fen_retcnt = 0;
  298. pram_ptr->fen_pper = 0;
  299. pram_ptr->fen_boffcnt = 0;
  300. pram_ptr->fen_gaddrh = 0;
  301. pram_ptr->fen_gaddrl = 0;
  302. pram_ptr->fen_mflr = PKT_MAXBUF_SIZE; /* maximum frame length register */
  303. /*
  304. * Set Ethernet station address.
  305. *
  306. * This is supplied in the board information structure, so we
  307. * copy that into the controller.
  308. * So far we have only been given one Ethernet address. We make
  309. * it unique by setting a few bits in the upper byte of the
  310. * non-static part of the address.
  311. */
  312. #define ea eth_get_dev()->enetaddr
  313. pram_ptr->fen_paddrh = (ea[5] << 8) + ea[4];
  314. pram_ptr->fen_paddrm = (ea[3] << 8) + ea[2];
  315. pram_ptr->fen_paddrl = (ea[1] << 8) + ea[0];
  316. #undef ea
  317. pram_ptr->fen_ibdcount = 0;
  318. pram_ptr->fen_ibdstart = 0;
  319. pram_ptr->fen_ibdend = 0;
  320. pram_ptr->fen_txlen = 0;
  321. pram_ptr->fen_iaddrh = 0; /* disable hash */
  322. pram_ptr->fen_iaddrl = 0;
  323. pram_ptr->fen_minflr = PKT_MINBUF_SIZE; /* minimum frame length register: 64 */
  324. /* pad pointer. use tiptr since we don't need a specific padding char */
  325. pram_ptr->fen_padptr = pram_ptr->fen_genfcc.fcc_tiptr;
  326. pram_ptr->fen_maxd1 = PKT_MAXDMA_SIZE; /* maximum DMA1 length:1520 */
  327. pram_ptr->fen_maxd2 = PKT_MAXDMA_SIZE; /* maximum DMA2 length:1520 */
  328. #if defined(ET_DEBUG)
  329. printf("parm_ptr(0xff788500) = %p\n",pram_ptr);
  330. printf("pram_ptr->fen_genfcc.fcc_rbase %08x\n",
  331. pram_ptr->fen_genfcc.fcc_rbase);
  332. printf("pram_ptr->fen_genfcc.fcc_tbase %08x\n",
  333. pram_ptr->fen_genfcc.fcc_tbase);
  334. #endif
  335. /* 28.9 - (8)(9): clear out events in FCCE */
  336. /* 28.9 - (9): FCCM: mask all events */
  337. if(info->ether_index == 0) {
  338. immr->im_cpm.im_cpm_fcc1.fcce = ~0x0;
  339. immr->im_cpm.im_cpm_fcc1.fccm = 0;
  340. } else if (info->ether_index == 1) {
  341. immr->im_cpm.im_cpm_fcc2.fcce = ~0x0;
  342. immr->im_cpm.im_cpm_fcc2.fccm = 0;
  343. } else if (info->ether_index == 2) {
  344. immr->im_cpm.im_cpm_fcc3.fcce = ~0x0;
  345. immr->im_cpm.im_cpm_fcc3.fccm = 0;
  346. }
  347. /* 28.9 - (10-12): we don't use ethernet interrupts */
  348. /* 28.9 - (13)
  349. *
  350. * Let's re-initialize the channel now. We have to do it later
  351. * than the manual describes because we have just now finished
  352. * the BD initialization.
  353. */
  354. cp->cpcr = mk_cr_cmd(info->cpm_cr_enet_page,
  355. info->cpm_cr_enet_sblock,
  356. 0x0c,
  357. CPM_CR_INIT_TRX) | CPM_CR_FLG;
  358. do {
  359. __asm__ __volatile__ ("eieio");
  360. } while (cp->cpcr & CPM_CR_FLG);
  361. /* 28.9 - (14): enable tx/rx in gfmr */
  362. if(info->ether_index == 0) {
  363. immr->im_cpm.im_cpm_fcc1.gfmr |= FCC_GFMR_ENT | FCC_GFMR_ENR;
  364. } else if (info->ether_index == 1) {
  365. immr->im_cpm.im_cpm_fcc2.gfmr |= FCC_GFMR_ENT | FCC_GFMR_ENR;
  366. } else if (info->ether_index == 2) {
  367. immr->im_cpm.im_cpm_fcc3.gfmr |= FCC_GFMR_ENT | FCC_GFMR_ENR;
  368. }
  369. return 1;
  370. }
  371. static void fec_halt(struct eth_device* dev)
  372. {
  373. struct ether_fcc_info_s * info = dev->priv;
  374. volatile immap_t *immr = (immap_t *)CFG_IMMR;
  375. /* write GFMR: disable tx/rx */
  376. if(info->ether_index == 0) {
  377. immr->im_cpm.im_cpm_fcc1.gfmr &= ~(FCC_GFMR_ENT | FCC_GFMR_ENR);
  378. } else if(info->ether_index == 1) {
  379. immr->im_cpm.im_cpm_fcc2.gfmr &= ~(FCC_GFMR_ENT | FCC_GFMR_ENR);
  380. } else if(info->ether_index == 2) {
  381. immr->im_cpm.im_cpm_fcc3.gfmr &= ~(FCC_GFMR_ENT | FCC_GFMR_ENR);
  382. }
  383. }
  384. int fec_initialize(bd_t *bis)
  385. {
  386. struct eth_device* dev;
  387. int i;
  388. for (i = 0; i < sizeof(ether_fcc_info) / sizeof(ether_fcc_info[0]); i++)
  389. {
  390. dev = (struct eth_device*) malloc(sizeof *dev);
  391. memset(dev, 0, sizeof *dev);
  392. sprintf(dev->name, "FCC%d ETHERNET",
  393. ether_fcc_info[i].ether_index + 1);
  394. dev->priv = &ether_fcc_info[i];
  395. dev->init = fec_init;
  396. dev->halt = fec_halt;
  397. dev->send = fec_send;
  398. dev->recv = fec_recv;
  399. eth_register(dev);
  400. #if (defined(CONFIG_MII) || defined(CONFIG_CMD_MII)) \
  401. && defined(CONFIG_BITBANGMII)
  402. miiphy_register(dev->name,
  403. bb_miiphy_read, bb_miiphy_write);
  404. #endif
  405. }
  406. return 1;
  407. }
  408. #endif
  409. #endif /* CONFIG_CPM2 */