ether_fcc.c 13 KB

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