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