mcffec.c 19 KB

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