mcffec.c 18 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617
  1. // SPDX-License-Identifier: GPL-2.0+
  2. /*
  3. * (C) Copyright 2000-2004
  4. * Wolfgang Denk, DENX Software Engineering, wd@denx.de.
  5. *
  6. * (C) Copyright 2007 Freescale Semiconductor, Inc.
  7. * TsiChung Liew (Tsi-Chung.Liew@freescale.com)
  8. */
  9. #include <common.h>
  10. #include <environment.h>
  11. #include <malloc.h>
  12. #include <command.h>
  13. #include <net.h>
  14. #include <netdev.h>
  15. #include <miiphy.h>
  16. #include <asm/fec.h>
  17. #include <asm/immap.h>
  18. #undef ET_DEBUG
  19. #undef MII_DEBUG
  20. /* Ethernet Transmit and Receive Buffers */
  21. #define DBUF_LENGTH 1520
  22. #define TX_BUF_CNT 2
  23. #define PKT_MAXBUF_SIZE 1518
  24. #define PKT_MINBUF_SIZE 64
  25. #define PKT_MAXBLR_SIZE 1520
  26. #define LAST_PKTBUFSRX PKTBUFSRX - 1
  27. #define BD_ENET_RX_W_E (BD_ENET_RX_WRAP | BD_ENET_RX_EMPTY)
  28. #define BD_ENET_TX_RDY_LST (BD_ENET_TX_READY | BD_ENET_TX_LAST)
  29. struct fec_info_s fec_info[] = {
  30. #ifdef CONFIG_SYS_FEC0_IOBASE
  31. {
  32. 0, /* index */
  33. CONFIG_SYS_FEC0_IOBASE, /* io base */
  34. CONFIG_SYS_FEC0_PINMUX, /* gpio pin muxing */
  35. CONFIG_SYS_FEC0_MIIBASE, /* mii base */
  36. -1, /* phy_addr */
  37. 0, /* duplex and speed */
  38. 0, /* phy name */
  39. 0, /* phyname init */
  40. 0, /* RX BD */
  41. 0, /* TX BD */
  42. 0, /* rx Index */
  43. 0, /* tx Index */
  44. 0, /* tx buffer */
  45. 0, /* initialized flag */
  46. (struct fec_info_s *)-1,
  47. },
  48. #endif
  49. #ifdef CONFIG_SYS_FEC1_IOBASE
  50. {
  51. 1, /* index */
  52. CONFIG_SYS_FEC1_IOBASE, /* io base */
  53. CONFIG_SYS_FEC1_PINMUX, /* gpio pin muxing */
  54. CONFIG_SYS_FEC1_MIIBASE, /* mii base */
  55. -1, /* phy_addr */
  56. 0, /* duplex and speed */
  57. 0, /* phy name */
  58. 0, /* phy name init */
  59. #ifdef CONFIG_SYS_FEC_BUF_USE_SRAM
  60. (cbd_t *)DBUF_LENGTH, /* RX BD */
  61. #else
  62. 0, /* RX BD */
  63. #endif
  64. 0, /* TX BD */
  65. 0, /* rx Index */
  66. 0, /* tx Index */
  67. 0, /* tx buffer */
  68. 0, /* initialized flag */
  69. (struct fec_info_s *)-1,
  70. }
  71. #endif
  72. };
  73. int fec_recv(struct eth_device *dev);
  74. int fec_init(struct eth_device *dev, bd_t * bd);
  75. void fec_halt(struct eth_device *dev);
  76. void fec_reset(struct eth_device *dev);
  77. void setFecDuplexSpeed(volatile fec_t * fecp, bd_t * bd, int dup_spd)
  78. {
  79. if ((dup_spd >> 16) == FULL) {
  80. /* Set maximum frame length */
  81. fecp->rcr = FEC_RCR_MAX_FL(PKT_MAXBUF_SIZE) | FEC_RCR_MII_MODE |
  82. FEC_RCR_PROM | 0x100;
  83. fecp->tcr = FEC_TCR_FDEN;
  84. } else {
  85. /* Half duplex mode */
  86. fecp->rcr = FEC_RCR_MAX_FL(PKT_MAXBUF_SIZE) |
  87. FEC_RCR_MII_MODE | FEC_RCR_DRT;
  88. fecp->tcr &= ~FEC_TCR_FDEN;
  89. }
  90. if ((dup_spd & 0xFFFF) == _100BASET) {
  91. #ifdef CONFIG_MCF5445x
  92. fecp->rcr &= ~0x200; /* disabled 10T base */
  93. #endif
  94. #ifdef MII_DEBUG
  95. printf("100Mbps\n");
  96. #endif
  97. bd->bi_ethspeed = 100;
  98. } else {
  99. #ifdef CONFIG_MCF5445x
  100. fecp->rcr |= 0x200; /* enabled 10T base */
  101. #endif
  102. #ifdef MII_DEBUG
  103. printf("10Mbps\n");
  104. #endif
  105. bd->bi_ethspeed = 10;
  106. }
  107. }
  108. static int fec_send(struct eth_device *dev, void *packet, int length)
  109. {
  110. struct fec_info_s *info = dev->priv;
  111. volatile fec_t *fecp = (fec_t *) (info->iobase);
  112. int j, rc;
  113. u16 phyStatus;
  114. miiphy_read(dev->name, info->phy_addr, MII_BMSR, &phyStatus);
  115. /* section 16.9.23.3
  116. * Wait for ready
  117. */
  118. j = 0;
  119. while ((info->txbd[info->txIdx].cbd_sc & BD_ENET_TX_READY) &&
  120. (j < MCFFEC_TOUT_LOOP)) {
  121. udelay(1);
  122. j++;
  123. }
  124. if (j >= MCFFEC_TOUT_LOOP) {
  125. printf("TX not ready\n");
  126. }
  127. info->txbd[info->txIdx].cbd_bufaddr = (uint) packet;
  128. info->txbd[info->txIdx].cbd_datlen = length;
  129. info->txbd[info->txIdx].cbd_sc |= BD_ENET_TX_RDY_LST;
  130. /* Activate transmit Buffer Descriptor polling */
  131. fecp->tdar = 0x01000000; /* Descriptor polling active */
  132. #ifndef CONFIG_SYS_FEC_BUF_USE_SRAM
  133. /*
  134. * FEC unable to initial transmit data packet.
  135. * A nop will ensure the descriptor polling active completed.
  136. * CF Internal RAM has shorter cycle access than DRAM. If use
  137. * DRAM as Buffer descriptor and data, a nop is a must.
  138. * Affect only V2 and V3.
  139. */
  140. __asm__ ("nop");
  141. #endif
  142. #ifdef CONFIG_SYS_UNIFY_CACHE
  143. icache_invalid();
  144. #endif
  145. j = 0;
  146. while ((info->txbd[info->txIdx].cbd_sc & BD_ENET_TX_READY) &&
  147. (j < MCFFEC_TOUT_LOOP)) {
  148. udelay(1);
  149. j++;
  150. }
  151. if (j >= MCFFEC_TOUT_LOOP) {
  152. printf("TX timeout\n");
  153. }
  154. #ifdef ET_DEBUG
  155. printf("%s[%d] %s: cycles: %d status: %x retry cnt: %d\n",
  156. __FILE__, __LINE__, __FUNCTION__, j,
  157. info->txbd[info->txIdx].cbd_sc,
  158. (info->txbd[info->txIdx].cbd_sc & 0x003C) >> 2);
  159. #endif
  160. /* return only status bits */
  161. rc = (info->txbd[info->txIdx].cbd_sc & BD_ENET_TX_STATS);
  162. info->txIdx = (info->txIdx + 1) % TX_BUF_CNT;
  163. return rc;
  164. }
  165. int fec_recv(struct eth_device *dev)
  166. {
  167. struct fec_info_s *info = dev->priv;
  168. volatile fec_t *fecp = (fec_t *) (info->iobase);
  169. int length;
  170. for (;;) {
  171. #ifndef CONFIG_SYS_FEC_BUF_USE_SRAM
  172. #endif
  173. #ifdef CONFIG_SYS_UNIFY_CACHE
  174. icache_invalid();
  175. #endif
  176. /* section 16.9.23.2 */
  177. if (info->rxbd[info->rxIdx].cbd_sc & BD_ENET_RX_EMPTY) {
  178. length = -1;
  179. break; /* nothing received - leave for() loop */
  180. }
  181. length = info->rxbd[info->rxIdx].cbd_datlen;
  182. if (info->rxbd[info->rxIdx].cbd_sc & 0x003f) {
  183. printf("%s[%d] err: %x\n",
  184. __FUNCTION__, __LINE__,
  185. info->rxbd[info->rxIdx].cbd_sc);
  186. #ifdef ET_DEBUG
  187. printf("%s[%d] err: %x\n",
  188. __FUNCTION__, __LINE__,
  189. info->rxbd[info->rxIdx].cbd_sc);
  190. #endif
  191. } else {
  192. length -= 4;
  193. /* Pass the packet up to the protocol layers. */
  194. net_process_received_packet(net_rx_packets[info->rxIdx],
  195. length);
  196. fecp->eir |= FEC_EIR_RXF;
  197. }
  198. /* Give the buffer back to the FEC. */
  199. info->rxbd[info->rxIdx].cbd_datlen = 0;
  200. /* wrap around buffer index when necessary */
  201. if (info->rxIdx == LAST_PKTBUFSRX) {
  202. info->rxbd[PKTBUFSRX - 1].cbd_sc = BD_ENET_RX_W_E;
  203. info->rxIdx = 0;
  204. } else {
  205. info->rxbd[info->rxIdx].cbd_sc = BD_ENET_RX_EMPTY;
  206. info->rxIdx++;
  207. }
  208. /* Try to fill Buffer Descriptors */
  209. fecp->rdar = 0x01000000; /* Descriptor polling active */
  210. }
  211. return length;
  212. }
  213. #ifdef ET_DEBUG
  214. void dbgFecRegs(struct eth_device *dev)
  215. {
  216. struct fec_info_s *info = dev->priv;
  217. volatile fec_t *fecp = (fec_t *) (info->iobase);
  218. printf("=====\n");
  219. printf("ievent %x - %x\n", (int)&fecp->eir, fecp->eir);
  220. printf("imask %x - %x\n", (int)&fecp->eimr, fecp->eimr);
  221. printf("r_des_active %x - %x\n", (int)&fecp->rdar, fecp->rdar);
  222. printf("x_des_active %x - %x\n", (int)&fecp->tdar, fecp->tdar);
  223. printf("ecntrl %x - %x\n", (int)&fecp->ecr, fecp->ecr);
  224. printf("mii_mframe %x - %x\n", (int)&fecp->mmfr, fecp->mmfr);
  225. printf("mii_speed %x - %x\n", (int)&fecp->mscr, fecp->mscr);
  226. printf("mii_ctrlstat %x - %x\n", (int)&fecp->mibc, fecp->mibc);
  227. printf("r_cntrl %x - %x\n", (int)&fecp->rcr, fecp->rcr);
  228. printf("x_cntrl %x - %x\n", (int)&fecp->tcr, fecp->tcr);
  229. printf("padr_l %x - %x\n", (int)&fecp->palr, fecp->palr);
  230. printf("padr_u %x - %x\n", (int)&fecp->paur, fecp->paur);
  231. printf("op_pause %x - %x\n", (int)&fecp->opd, fecp->opd);
  232. printf("iadr_u %x - %x\n", (int)&fecp->iaur, fecp->iaur);
  233. printf("iadr_l %x - %x\n", (int)&fecp->ialr, fecp->ialr);
  234. printf("gadr_u %x - %x\n", (int)&fecp->gaur, fecp->gaur);
  235. printf("gadr_l %x - %x\n", (int)&fecp->galr, fecp->galr);
  236. printf("x_wmrk %x - %x\n", (int)&fecp->tfwr, fecp->tfwr);
  237. printf("r_bound %x - %x\n", (int)&fecp->frbr, fecp->frbr);
  238. printf("r_fstart %x - %x\n", (int)&fecp->frsr, fecp->frsr);
  239. printf("r_drng %x - %x\n", (int)&fecp->erdsr, fecp->erdsr);
  240. printf("x_drng %x - %x\n", (int)&fecp->etdsr, fecp->etdsr);
  241. printf("r_bufsz %x - %x\n", (int)&fecp->emrbr, fecp->emrbr);
  242. printf("\n");
  243. printf("rmon_t_drop %x - %x\n", (int)&fecp->rmon_t_drop,
  244. fecp->rmon_t_drop);
  245. printf("rmon_t_packets %x - %x\n", (int)&fecp->rmon_t_packets,
  246. fecp->rmon_t_packets);
  247. printf("rmon_t_bc_pkt %x - %x\n", (int)&fecp->rmon_t_bc_pkt,
  248. fecp->rmon_t_bc_pkt);
  249. printf("rmon_t_mc_pkt %x - %x\n", (int)&fecp->rmon_t_mc_pkt,
  250. fecp->rmon_t_mc_pkt);
  251. printf("rmon_t_crc_align %x - %x\n", (int)&fecp->rmon_t_crc_align,
  252. fecp->rmon_t_crc_align);
  253. printf("rmon_t_undersize %x - %x\n", (int)&fecp->rmon_t_undersize,
  254. fecp->rmon_t_undersize);
  255. printf("rmon_t_oversize %x - %x\n", (int)&fecp->rmon_t_oversize,
  256. fecp->rmon_t_oversize);
  257. printf("rmon_t_frag %x - %x\n", (int)&fecp->rmon_t_frag,
  258. fecp->rmon_t_frag);
  259. printf("rmon_t_jab %x - %x\n", (int)&fecp->rmon_t_jab,
  260. fecp->rmon_t_jab);
  261. printf("rmon_t_col %x - %x\n", (int)&fecp->rmon_t_col,
  262. fecp->rmon_t_col);
  263. printf("rmon_t_p64 %x - %x\n", (int)&fecp->rmon_t_p64,
  264. fecp->rmon_t_p64);
  265. printf("rmon_t_p65to127 %x - %x\n", (int)&fecp->rmon_t_p65to127,
  266. fecp->rmon_t_p65to127);
  267. printf("rmon_t_p128to255 %x - %x\n", (int)&fecp->rmon_t_p128to255,
  268. fecp->rmon_t_p128to255);
  269. printf("rmon_t_p256to511 %x - %x\n", (int)&fecp->rmon_t_p256to511,
  270. fecp->rmon_t_p256to511);
  271. printf("rmon_t_p512to1023 %x - %x\n", (int)&fecp->rmon_t_p512to1023,
  272. fecp->rmon_t_p512to1023);
  273. printf("rmon_t_p1024to2047 %x - %x\n", (int)&fecp->rmon_t_p1024to2047,
  274. fecp->rmon_t_p1024to2047);
  275. printf("rmon_t_p_gte2048 %x - %x\n", (int)&fecp->rmon_t_p_gte2048,
  276. fecp->rmon_t_p_gte2048);
  277. printf("rmon_t_octets %x - %x\n", (int)&fecp->rmon_t_octets,
  278. fecp->rmon_t_octets);
  279. printf("\n");
  280. printf("ieee_t_drop %x - %x\n", (int)&fecp->ieee_t_drop,
  281. fecp->ieee_t_drop);
  282. printf("ieee_t_frame_ok %x - %x\n", (int)&fecp->ieee_t_frame_ok,
  283. fecp->ieee_t_frame_ok);
  284. printf("ieee_t_1col %x - %x\n", (int)&fecp->ieee_t_1col,
  285. fecp->ieee_t_1col);
  286. printf("ieee_t_mcol %x - %x\n", (int)&fecp->ieee_t_mcol,
  287. fecp->ieee_t_mcol);
  288. printf("ieee_t_def %x - %x\n", (int)&fecp->ieee_t_def,
  289. fecp->ieee_t_def);
  290. printf("ieee_t_lcol %x - %x\n", (int)&fecp->ieee_t_lcol,
  291. fecp->ieee_t_lcol);
  292. printf("ieee_t_excol %x - %x\n", (int)&fecp->ieee_t_excol,
  293. fecp->ieee_t_excol);
  294. printf("ieee_t_macerr %x - %x\n", (int)&fecp->ieee_t_macerr,
  295. fecp->ieee_t_macerr);
  296. printf("ieee_t_cserr %x - %x\n", (int)&fecp->ieee_t_cserr,
  297. fecp->ieee_t_cserr);
  298. printf("ieee_t_sqe %x - %x\n", (int)&fecp->ieee_t_sqe,
  299. fecp->ieee_t_sqe);
  300. printf("ieee_t_fdxfc %x - %x\n", (int)&fecp->ieee_t_fdxfc,
  301. fecp->ieee_t_fdxfc);
  302. printf("ieee_t_octets_ok %x - %x\n", (int)&fecp->ieee_t_octets_ok,
  303. fecp->ieee_t_octets_ok);
  304. printf("\n");
  305. printf("rmon_r_drop %x - %x\n", (int)&fecp->rmon_r_drop,
  306. fecp->rmon_r_drop);
  307. printf("rmon_r_packets %x - %x\n", (int)&fecp->rmon_r_packets,
  308. fecp->rmon_r_packets);
  309. printf("rmon_r_bc_pkt %x - %x\n", (int)&fecp->rmon_r_bc_pkt,
  310. fecp->rmon_r_bc_pkt);
  311. printf("rmon_r_mc_pkt %x - %x\n", (int)&fecp->rmon_r_mc_pkt,
  312. fecp->rmon_r_mc_pkt);
  313. printf("rmon_r_crc_align %x - %x\n", (int)&fecp->rmon_r_crc_align,
  314. fecp->rmon_r_crc_align);
  315. printf("rmon_r_undersize %x - %x\n", (int)&fecp->rmon_r_undersize,
  316. fecp->rmon_r_undersize);
  317. printf("rmon_r_oversize %x - %x\n", (int)&fecp->rmon_r_oversize,
  318. fecp->rmon_r_oversize);
  319. printf("rmon_r_frag %x - %x\n", (int)&fecp->rmon_r_frag,
  320. fecp->rmon_r_frag);
  321. printf("rmon_r_jab %x - %x\n", (int)&fecp->rmon_r_jab,
  322. fecp->rmon_r_jab);
  323. printf("rmon_r_p64 %x - %x\n", (int)&fecp->rmon_r_p64,
  324. fecp->rmon_r_p64);
  325. printf("rmon_r_p65to127 %x - %x\n", (int)&fecp->rmon_r_p65to127,
  326. fecp->rmon_r_p65to127);
  327. printf("rmon_r_p128to255 %x - %x\n", (int)&fecp->rmon_r_p128to255,
  328. fecp->rmon_r_p128to255);
  329. printf("rmon_r_p256to511 %x - %x\n", (int)&fecp->rmon_r_p256to511,
  330. fecp->rmon_r_p256to511);
  331. printf("rmon_r_p512to1023 %x - %x\n", (int)&fecp->rmon_r_p512to1023,
  332. fecp->rmon_r_p512to1023);
  333. printf("rmon_r_p1024to2047 %x - %x\n", (int)&fecp->rmon_r_p1024to2047,
  334. fecp->rmon_r_p1024to2047);
  335. printf("rmon_r_p_gte2048 %x - %x\n", (int)&fecp->rmon_r_p_gte2048,
  336. fecp->rmon_r_p_gte2048);
  337. printf("rmon_r_octets %x - %x\n", (int)&fecp->rmon_r_octets,
  338. fecp->rmon_r_octets);
  339. printf("\n");
  340. printf("ieee_r_drop %x - %x\n", (int)&fecp->ieee_r_drop,
  341. fecp->ieee_r_drop);
  342. printf("ieee_r_frame_ok %x - %x\n", (int)&fecp->ieee_r_frame_ok,
  343. fecp->ieee_r_frame_ok);
  344. printf("ieee_r_crc %x - %x\n", (int)&fecp->ieee_r_crc,
  345. fecp->ieee_r_crc);
  346. printf("ieee_r_align %x - %x\n", (int)&fecp->ieee_r_align,
  347. fecp->ieee_r_align);
  348. printf("ieee_r_macerr %x - %x\n", (int)&fecp->ieee_r_macerr,
  349. fecp->ieee_r_macerr);
  350. printf("ieee_r_fdxfc %x - %x\n", (int)&fecp->ieee_r_fdxfc,
  351. fecp->ieee_r_fdxfc);
  352. printf("ieee_r_octets_ok %x - %x\n", (int)&fecp->ieee_r_octets_ok,
  353. fecp->ieee_r_octets_ok);
  354. printf("\n\n\n");
  355. }
  356. #endif
  357. int fec_init(struct eth_device *dev, bd_t * bd)
  358. {
  359. struct fec_info_s *info = dev->priv;
  360. volatile fec_t *fecp = (fec_t *) (info->iobase);
  361. int i;
  362. uchar ea[6];
  363. fecpin_setclear(dev, 1);
  364. fec_reset(dev);
  365. #if defined(CONFIG_CMD_MII) || defined (CONFIG_MII) || \
  366. defined (CONFIG_SYS_DISCOVER_PHY)
  367. mii_init();
  368. setFecDuplexSpeed(fecp, bd, info->dup_spd);
  369. #else
  370. #ifndef CONFIG_SYS_DISCOVER_PHY
  371. setFecDuplexSpeed(fecp, bd, (FECDUPLEX << 16) | FECSPEED);
  372. #endif /* ifndef CONFIG_SYS_DISCOVER_PHY */
  373. #endif /* CONFIG_CMD_MII || CONFIG_MII */
  374. /* We use strictly polling mode only */
  375. fecp->eimr = 0;
  376. /* Clear any pending interrupt */
  377. fecp->eir = 0xffffffff;
  378. /* Set station address */
  379. if ((u32) fecp == CONFIG_SYS_FEC0_IOBASE) {
  380. #ifdef CONFIG_SYS_FEC1_IOBASE
  381. volatile fec_t *fecp1 = (fec_t *) (CONFIG_SYS_FEC1_IOBASE);
  382. eth_env_get_enetaddr("eth1addr", ea);
  383. fecp1->palr =
  384. (ea[0] << 24) | (ea[1] << 16) | (ea[2] << 8) | (ea[3]);
  385. fecp1->paur = (ea[4] << 24) | (ea[5] << 16);
  386. #endif
  387. eth_env_get_enetaddr("ethaddr", ea);
  388. fecp->palr =
  389. (ea[0] << 24) | (ea[1] << 16) | (ea[2] << 8) | (ea[3]);
  390. fecp->paur = (ea[4] << 24) | (ea[5] << 16);
  391. } else {
  392. #ifdef CONFIG_SYS_FEC0_IOBASE
  393. volatile fec_t *fecp0 = (fec_t *) (CONFIG_SYS_FEC0_IOBASE);
  394. eth_env_get_enetaddr("ethaddr", ea);
  395. fecp0->palr =
  396. (ea[0] << 24) | (ea[1] << 16) | (ea[2] << 8) | (ea[3]);
  397. fecp0->paur = (ea[4] << 24) | (ea[5] << 16);
  398. #endif
  399. #ifdef CONFIG_SYS_FEC1_IOBASE
  400. eth_env_get_enetaddr("eth1addr", ea);
  401. fecp->palr =
  402. (ea[0] << 24) | (ea[1] << 16) | (ea[2] << 8) | (ea[3]);
  403. fecp->paur = (ea[4] << 24) | (ea[5] << 16);
  404. #endif
  405. }
  406. /* Clear unicast address hash table */
  407. fecp->iaur = 0;
  408. fecp->ialr = 0;
  409. /* Clear multicast address hash table */
  410. fecp->gaur = 0;
  411. fecp->galr = 0;
  412. /* Set maximum receive buffer size. */
  413. fecp->emrbr = PKT_MAXBLR_SIZE;
  414. /*
  415. * Setup Buffers and Buffer Descriptors
  416. */
  417. info->rxIdx = 0;
  418. info->txIdx = 0;
  419. /*
  420. * Setup Receiver Buffer Descriptors (13.14.24.18)
  421. * Settings:
  422. * Empty, Wrap
  423. */
  424. for (i = 0; i < PKTBUFSRX; i++) {
  425. info->rxbd[i].cbd_sc = BD_ENET_RX_EMPTY;
  426. info->rxbd[i].cbd_datlen = 0; /* Reset */
  427. info->rxbd[i].cbd_bufaddr = (uint) net_rx_packets[i];
  428. }
  429. info->rxbd[PKTBUFSRX - 1].cbd_sc |= BD_ENET_RX_WRAP;
  430. /*
  431. * Setup Ethernet Transmitter Buffer Descriptors (13.14.24.19)
  432. * Settings:
  433. * Last, Tx CRC
  434. */
  435. for (i = 0; i < TX_BUF_CNT; i++) {
  436. info->txbd[i].cbd_sc = BD_ENET_TX_LAST | BD_ENET_TX_TC;
  437. info->txbd[i].cbd_datlen = 0; /* Reset */
  438. info->txbd[i].cbd_bufaddr = (uint) (&info->txbuf[0]);
  439. }
  440. info->txbd[TX_BUF_CNT - 1].cbd_sc |= BD_ENET_TX_WRAP;
  441. /* Set receive and transmit descriptor base */
  442. fecp->erdsr = (unsigned int)(&info->rxbd[0]);
  443. fecp->etdsr = (unsigned int)(&info->txbd[0]);
  444. /* Now enable the transmit and receive processing */
  445. fecp->ecr |= FEC_ECR_ETHER_EN;
  446. /* And last, try to fill Rx Buffer Descriptors */
  447. fecp->rdar = 0x01000000; /* Descriptor polling active */
  448. return 1;
  449. }
  450. void fec_reset(struct eth_device *dev)
  451. {
  452. struct fec_info_s *info = dev->priv;
  453. volatile fec_t *fecp = (fec_t *) (info->iobase);
  454. int i;
  455. fecp->ecr = FEC_ECR_RESET;
  456. for (i = 0; (fecp->ecr & FEC_ECR_RESET) && (i < FEC_RESET_DELAY); ++i) {
  457. udelay(1);
  458. }
  459. if (i == FEC_RESET_DELAY) {
  460. printf("FEC_RESET_DELAY timeout\n");
  461. }
  462. }
  463. void fec_halt(struct eth_device *dev)
  464. {
  465. struct fec_info_s *info = dev->priv;
  466. fec_reset(dev);
  467. fecpin_setclear(dev, 0);
  468. info->rxIdx = info->txIdx = 0;
  469. memset(info->rxbd, 0, PKTBUFSRX * sizeof(cbd_t));
  470. memset(info->txbd, 0, TX_BUF_CNT * sizeof(cbd_t));
  471. memset(info->txbuf, 0, DBUF_LENGTH);
  472. }
  473. int mcffec_initialize(bd_t * bis)
  474. {
  475. struct eth_device *dev;
  476. int i;
  477. #ifdef CONFIG_SYS_FEC_BUF_USE_SRAM
  478. u32 tmp = CONFIG_SYS_INIT_RAM_ADDR + 0x1000;
  479. #endif
  480. for (i = 0; i < ARRAY_SIZE(fec_info); i++) {
  481. dev =
  482. (struct eth_device *)memalign(CONFIG_SYS_CACHELINE_SIZE,
  483. sizeof *dev);
  484. if (dev == NULL)
  485. hang();
  486. memset(dev, 0, sizeof(*dev));
  487. sprintf(dev->name, "FEC%d", fec_info[i].index);
  488. dev->priv = &fec_info[i];
  489. dev->init = fec_init;
  490. dev->halt = fec_halt;
  491. dev->send = fec_send;
  492. dev->recv = fec_recv;
  493. /* setup Receive and Transmit buffer descriptor */
  494. #ifdef CONFIG_SYS_FEC_BUF_USE_SRAM
  495. fec_info[i].rxbd = (cbd_t *)((u32)fec_info[i].rxbd + tmp);
  496. tmp = (u32)fec_info[i].rxbd;
  497. fec_info[i].txbd =
  498. (cbd_t *)((u32)fec_info[i].txbd + tmp +
  499. (PKTBUFSRX * sizeof(cbd_t)));
  500. tmp = (u32)fec_info[i].txbd;
  501. fec_info[i].txbuf =
  502. (char *)((u32)fec_info[i].txbuf + tmp +
  503. (CONFIG_SYS_TX_ETH_BUFFER * sizeof(cbd_t)));
  504. tmp = (u32)fec_info[i].txbuf;
  505. #else
  506. fec_info[i].rxbd =
  507. (cbd_t *) memalign(CONFIG_SYS_CACHELINE_SIZE,
  508. (PKTBUFSRX * sizeof(cbd_t)));
  509. fec_info[i].txbd =
  510. (cbd_t *) memalign(CONFIG_SYS_CACHELINE_SIZE,
  511. (TX_BUF_CNT * sizeof(cbd_t)));
  512. fec_info[i].txbuf =
  513. (char *)memalign(CONFIG_SYS_CACHELINE_SIZE, DBUF_LENGTH);
  514. #endif
  515. #ifdef ET_DEBUG
  516. printf("rxbd %x txbd %x\n",
  517. (int)fec_info[i].rxbd, (int)fec_info[i].txbd);
  518. #endif
  519. fec_info[i].phy_name = (char *)memalign(CONFIG_SYS_CACHELINE_SIZE, 32);
  520. eth_register(dev);
  521. #if defined(CONFIG_MII) || defined(CONFIG_CMD_MII)
  522. int retval;
  523. struct mii_dev *mdiodev = mdio_alloc();
  524. if (!mdiodev)
  525. return -ENOMEM;
  526. strncpy(mdiodev->name, dev->name, MDIO_NAME_LEN);
  527. mdiodev->read = mcffec_miiphy_read;
  528. mdiodev->write = mcffec_miiphy_write;
  529. retval = mdio_register(mdiodev);
  530. if (retval < 0)
  531. return retval;
  532. #endif
  533. if (i > 0)
  534. fec_info[i - 1].next = &fec_info[i];
  535. }
  536. fec_info[i - 1].next = &fec_info[0];
  537. /* default speed */
  538. bis->bi_ethspeed = 10;
  539. return 0;
  540. }