mcffec.c 19 KB

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