mcffec.c 19 KB

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