ether_fcc.c 13 KB

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