fec.c 23 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967
  1. /*
  2. * (C) Copyright 2000
  3. * Wolfgang Denk, DENX Software Engineering, wd@denx.de.
  4. *
  5. * SPDX-License-Identifier: GPL-2.0+
  6. */
  7. #include <common.h>
  8. #include <malloc.h>
  9. #include <commproc.h>
  10. #include <net.h>
  11. #include <command.h>
  12. DECLARE_GLOBAL_DATA_PTR;
  13. #undef ET_DEBUG
  14. #if defined(CONFIG_CMD_NET) && \
  15. (defined(FEC_ENET) || defined(CONFIG_ETHER_ON_FEC1) || defined(CONFIG_ETHER_ON_FEC2))
  16. /* compatibility test, if only FEC_ENET defined assume ETHER on FEC1 */
  17. #if defined(FEC_ENET) && !defined(CONFIG_ETHER_ON_FEC1) && !defined(CONFIG_ETHER_ON_FEC2)
  18. #define CONFIG_ETHER_ON_FEC1 1
  19. #endif
  20. /* define WANT_MII when MII support is required */
  21. #if defined(CONFIG_SYS_DISCOVER_PHY) || defined(CONFIG_FEC1_PHY) || defined(CONFIG_FEC2_PHY)
  22. #define WANT_MII
  23. #else
  24. #undef WANT_MII
  25. #endif
  26. #if defined(WANT_MII)
  27. #include <miiphy.h>
  28. #if !(defined(CONFIG_MII) || defined(CONFIG_CMD_MII))
  29. #error "CONFIG_MII has to be defined!"
  30. #endif
  31. #endif
  32. #if defined(CONFIG_RMII) && !defined(WANT_MII)
  33. #error RMII support is unusable without a working PHY.
  34. #endif
  35. #ifdef CONFIG_SYS_DISCOVER_PHY
  36. static int mii_discover_phy(struct eth_device *dev);
  37. #endif
  38. int fec8xx_miiphy_read(const char *devname, unsigned char addr,
  39. unsigned char reg, unsigned short *value);
  40. int fec8xx_miiphy_write(const char *devname, unsigned char addr,
  41. unsigned char reg, unsigned short value);
  42. static struct ether_fcc_info_s
  43. {
  44. int ether_index;
  45. int fecp_offset;
  46. int phy_addr;
  47. int actual_phy_addr;
  48. int initialized;
  49. }
  50. ether_fcc_info[] = {
  51. #if defined(CONFIG_ETHER_ON_FEC1)
  52. {
  53. 0,
  54. offsetof(immap_t, im_cpm.cp_fec1),
  55. #if defined(CONFIG_FEC1_PHY)
  56. CONFIG_FEC1_PHY,
  57. #else
  58. -1, /* discover */
  59. #endif
  60. -1,
  61. 0,
  62. },
  63. #endif
  64. #if defined(CONFIG_ETHER_ON_FEC2)
  65. {
  66. 1,
  67. offsetof(immap_t, im_cpm.cp_fec2),
  68. #if defined(CONFIG_FEC2_PHY)
  69. CONFIG_FEC2_PHY,
  70. #else
  71. -1,
  72. #endif
  73. -1,
  74. 0,
  75. },
  76. #endif
  77. };
  78. /* Ethernet Transmit and Receive Buffers */
  79. #define DBUF_LENGTH 1520
  80. #define TX_BUF_CNT 2
  81. #define TOUT_LOOP 100
  82. #define PKT_MAXBUF_SIZE 1518
  83. #define PKT_MINBUF_SIZE 64
  84. #define PKT_MAXBLR_SIZE 1520
  85. #ifdef __GNUC__
  86. static char txbuf[DBUF_LENGTH] __attribute__ ((aligned(8)));
  87. #else
  88. #error txbuf must be aligned.
  89. #endif
  90. static uint rxIdx; /* index of the current RX buffer */
  91. static uint txIdx; /* index of the current TX buffer */
  92. /*
  93. * FEC Ethernet Tx and Rx buffer descriptors allocated at the
  94. * immr->udata_bd address on Dual-Port RAM
  95. * Provide for Double Buffering
  96. */
  97. typedef volatile struct CommonBufferDescriptor {
  98. cbd_t rxbd[PKTBUFSRX]; /* Rx BD */
  99. cbd_t txbd[TX_BUF_CNT]; /* Tx BD */
  100. } RTXBD;
  101. static RTXBD *rtx = NULL;
  102. static int fec_send(struct eth_device *dev, void *packet, int length);
  103. static int fec_recv(struct eth_device* dev);
  104. static int fec_init(struct eth_device* dev, bd_t * bd);
  105. static void fec_halt(struct eth_device* dev);
  106. #if defined(CONFIG_MII) || defined(CONFIG_CMD_MII)
  107. static void __mii_init(void);
  108. #endif
  109. int fec_initialize(bd_t *bis)
  110. {
  111. struct eth_device* dev;
  112. struct ether_fcc_info_s *efis;
  113. int i;
  114. for (i = 0; i < sizeof(ether_fcc_info) / sizeof(ether_fcc_info[0]); i++) {
  115. dev = malloc(sizeof(*dev));
  116. if (dev == NULL)
  117. hang();
  118. memset(dev, 0, sizeof(*dev));
  119. /* for FEC1 make sure that the name of the interface is the same
  120. as the old one for compatibility reasons */
  121. if (i == 0) {
  122. sprintf (dev->name, "FEC");
  123. } else {
  124. sprintf (dev->name, "FEC%d",
  125. ether_fcc_info[i].ether_index + 1);
  126. }
  127. efis = &ether_fcc_info[i];
  128. /*
  129. * reset actual phy addr
  130. */
  131. efis->actual_phy_addr = -1;
  132. dev->priv = efis;
  133. dev->init = fec_init;
  134. dev->halt = fec_halt;
  135. dev->send = fec_send;
  136. dev->recv = fec_recv;
  137. eth_register(dev);
  138. #if defined(CONFIG_MII) || defined(CONFIG_CMD_MII)
  139. miiphy_register(dev->name,
  140. fec8xx_miiphy_read, fec8xx_miiphy_write);
  141. #endif
  142. }
  143. return 1;
  144. }
  145. static int fec_send(struct eth_device *dev, void *packet, int length)
  146. {
  147. int j, rc;
  148. struct ether_fcc_info_s *efis = dev->priv;
  149. volatile fec_t *fecp = (volatile fec_t *)(CONFIG_SYS_IMMR + efis->fecp_offset);
  150. /* section 16.9.23.3
  151. * Wait for ready
  152. */
  153. j = 0;
  154. while ((rtx->txbd[txIdx].cbd_sc & BD_ENET_TX_READY) && (j<TOUT_LOOP)) {
  155. udelay(1);
  156. j++;
  157. }
  158. if (j>=TOUT_LOOP) {
  159. printf("TX not ready\n");
  160. }
  161. rtx->txbd[txIdx].cbd_bufaddr = (uint)packet;
  162. rtx->txbd[txIdx].cbd_datlen = length;
  163. rtx->txbd[txIdx].cbd_sc |= BD_ENET_TX_READY | BD_ENET_TX_LAST;
  164. __asm__ ("eieio");
  165. /* Activate transmit Buffer Descriptor polling */
  166. fecp->fec_x_des_active = 0x01000000; /* Descriptor polling active */
  167. j = 0;
  168. while ((rtx->txbd[txIdx].cbd_sc & BD_ENET_TX_READY) && (j<TOUT_LOOP)) {
  169. #if defined(CONFIG_ICU862)
  170. udelay(10);
  171. #else
  172. udelay(1);
  173. #endif
  174. j++;
  175. }
  176. if (j>=TOUT_LOOP) {
  177. printf("TX timeout\n");
  178. }
  179. #ifdef ET_DEBUG
  180. printf("%s[%d] %s: cycles: %d status: %x retry cnt: %d\n",
  181. __FILE__,__LINE__,__FUNCTION__,j,rtx->txbd[txIdx].cbd_sc,
  182. (rtx->txbd[txIdx].cbd_sc & 0x003C)>>2);
  183. #endif
  184. /* return only status bits */;
  185. rc = (rtx->txbd[txIdx].cbd_sc & BD_ENET_TX_STATS);
  186. txIdx = (txIdx + 1) % TX_BUF_CNT;
  187. return rc;
  188. }
  189. static int fec_recv (struct eth_device *dev)
  190. {
  191. struct ether_fcc_info_s *efis = dev->priv;
  192. volatile fec_t *fecp =
  193. (volatile fec_t *) (CONFIG_SYS_IMMR + efis->fecp_offset);
  194. int length;
  195. for (;;) {
  196. /* section 16.9.23.2 */
  197. if (rtx->rxbd[rxIdx].cbd_sc & BD_ENET_RX_EMPTY) {
  198. length = -1;
  199. break; /* nothing received - leave for() loop */
  200. }
  201. length = rtx->rxbd[rxIdx].cbd_datlen;
  202. if (rtx->rxbd[rxIdx].cbd_sc & 0x003f) {
  203. #ifdef ET_DEBUG
  204. printf ("%s[%d] err: %x\n",
  205. __FUNCTION__, __LINE__,
  206. rtx->rxbd[rxIdx].cbd_sc);
  207. #endif
  208. } else {
  209. uchar *rx = NetRxPackets[rxIdx];
  210. length -= 4;
  211. #if defined(CONFIG_CMD_CDP)
  212. if ((rx[0] & 1) != 0
  213. && memcmp ((uchar *) rx, NetBcastAddr, 6) != 0
  214. && !is_cdp_packet((uchar *)rx))
  215. rx = NULL;
  216. #endif
  217. /*
  218. * Pass the packet up to the protocol layers.
  219. */
  220. if (rx != NULL)
  221. NetReceive (rx, length);
  222. }
  223. /* Give the buffer back to the FEC. */
  224. rtx->rxbd[rxIdx].cbd_datlen = 0;
  225. /* wrap around buffer index when necessary */
  226. if ((rxIdx + 1) >= PKTBUFSRX) {
  227. rtx->rxbd[PKTBUFSRX - 1].cbd_sc =
  228. (BD_ENET_RX_WRAP | BD_ENET_RX_EMPTY);
  229. rxIdx = 0;
  230. } else {
  231. rtx->rxbd[rxIdx].cbd_sc = BD_ENET_RX_EMPTY;
  232. rxIdx++;
  233. }
  234. __asm__ ("eieio");
  235. /* Try to fill Buffer Descriptors */
  236. fecp->fec_r_des_active = 0x01000000; /* Descriptor polling active */
  237. }
  238. return length;
  239. }
  240. /**************************************************************
  241. *
  242. * FEC Ethernet Initialization Routine
  243. *
  244. *************************************************************/
  245. #define FEC_ECNTRL_PINMUX 0x00000004
  246. #define FEC_ECNTRL_ETHER_EN 0x00000002
  247. #define FEC_ECNTRL_RESET 0x00000001
  248. #define FEC_RCNTRL_BC_REJ 0x00000010
  249. #define FEC_RCNTRL_PROM 0x00000008
  250. #define FEC_RCNTRL_MII_MODE 0x00000004
  251. #define FEC_RCNTRL_DRT 0x00000002
  252. #define FEC_RCNTRL_LOOP 0x00000001
  253. #define FEC_TCNTRL_FDEN 0x00000004
  254. #define FEC_TCNTRL_HBC 0x00000002
  255. #define FEC_TCNTRL_GTS 0x00000001
  256. #define FEC_RESET_DELAY 50
  257. #if defined(CONFIG_RMII)
  258. static inline void fec_10Mbps(struct eth_device *dev)
  259. {
  260. struct ether_fcc_info_s *efis = dev->priv;
  261. int fecidx = efis->ether_index;
  262. uint mask = (fecidx == 0) ? 0x0000010 : 0x0000008;
  263. if ((unsigned int)fecidx >= 2)
  264. hang();
  265. ((volatile immap_t *)CONFIG_SYS_IMMR)->im_cpm.cp_cptr |= mask;
  266. }
  267. static inline void fec_100Mbps(struct eth_device *dev)
  268. {
  269. struct ether_fcc_info_s *efis = dev->priv;
  270. int fecidx = efis->ether_index;
  271. uint mask = (fecidx == 0) ? 0x0000010 : 0x0000008;
  272. if ((unsigned int)fecidx >= 2)
  273. hang();
  274. ((volatile immap_t *)CONFIG_SYS_IMMR)->im_cpm.cp_cptr &= ~mask;
  275. }
  276. #endif
  277. static inline void fec_full_duplex(struct eth_device *dev)
  278. {
  279. struct ether_fcc_info_s *efis = dev->priv;
  280. volatile fec_t *fecp = (volatile fec_t *)(CONFIG_SYS_IMMR + efis->fecp_offset);
  281. fecp->fec_r_cntrl &= ~FEC_RCNTRL_DRT;
  282. fecp->fec_x_cntrl |= FEC_TCNTRL_FDEN; /* FD enable */
  283. }
  284. static inline void fec_half_duplex(struct eth_device *dev)
  285. {
  286. struct ether_fcc_info_s *efis = dev->priv;
  287. volatile fec_t *fecp = (volatile fec_t *)(CONFIG_SYS_IMMR + efis->fecp_offset);
  288. fecp->fec_r_cntrl |= FEC_RCNTRL_DRT;
  289. fecp->fec_x_cntrl &= ~FEC_TCNTRL_FDEN; /* FD disable */
  290. }
  291. static void fec_pin_init(int fecidx)
  292. {
  293. bd_t *bd = gd->bd;
  294. volatile immap_t *immr = (immap_t *) CONFIG_SYS_IMMR;
  295. /*
  296. * Set MII speed to 2.5 MHz or slightly below.
  297. *
  298. * According to the MPC860T (Rev. D) Fast ethernet controller user
  299. * manual (6.2.14),
  300. * the MII management interface clock must be less than or equal
  301. * to 2.5 MHz.
  302. * This MDC frequency is equal to system clock / (2 * MII_SPEED).
  303. * Then MII_SPEED = system_clock / 2 * 2,5 MHz.
  304. *
  305. * All MII configuration is done via FEC1 registers:
  306. */
  307. immr->im_cpm.cp_fec1.fec_mii_speed = ((bd->bi_intfreq + 4999999) / 5000000) << 1;
  308. #if defined(CONFIG_MPC885_FAMILY) && defined(WANT_MII)
  309. /* use MDC for MII */
  310. immr->im_ioport.iop_pdpar |= 0x0080;
  311. immr->im_ioport.iop_pddir &= ~0x0080;
  312. #endif
  313. if (fecidx == 0) {
  314. #if defined(CONFIG_ETHER_ON_FEC1)
  315. #if defined(CONFIG_MPC885_FAMILY) /* MPC87x/88x have got 2 FECs and different pinout */
  316. #if !defined(CONFIG_RMII)
  317. immr->im_ioport.iop_papar |= 0xf830;
  318. immr->im_ioport.iop_padir |= 0x0830;
  319. immr->im_ioport.iop_padir &= ~0xf000;
  320. immr->im_cpm.cp_pbpar |= 0x00001001;
  321. immr->im_cpm.cp_pbdir &= ~0x00001001;
  322. immr->im_ioport.iop_pcpar |= 0x000c;
  323. immr->im_ioport.iop_pcdir &= ~0x000c;
  324. immr->im_cpm.cp_pepar |= 0x00000003;
  325. immr->im_cpm.cp_pedir |= 0x00000003;
  326. immr->im_cpm.cp_peso &= ~0x00000003;
  327. immr->im_cpm.cp_cptr &= ~0x00000100;
  328. #else
  329. #if !defined(CONFIG_FEC1_PHY_NORXERR)
  330. immr->im_ioport.iop_papar |= 0x1000;
  331. immr->im_ioport.iop_padir &= ~0x1000;
  332. #endif
  333. immr->im_ioport.iop_papar |= 0xe810;
  334. immr->im_ioport.iop_padir |= 0x0810;
  335. immr->im_ioport.iop_padir &= ~0xe000;
  336. immr->im_cpm.cp_pbpar |= 0x00000001;
  337. immr->im_cpm.cp_pbdir &= ~0x00000001;
  338. immr->im_cpm.cp_cptr |= 0x00000100;
  339. immr->im_cpm.cp_cptr &= ~0x00000050;
  340. #endif /* !CONFIG_RMII */
  341. #elif !defined(CONFIG_ICU862)
  342. /*
  343. * Configure all of port D for MII.
  344. */
  345. immr->im_ioport.iop_pdpar = 0x1fff;
  346. /*
  347. * Bits moved from Rev. D onward
  348. */
  349. if ((get_immr(0) & 0xffff) < 0x0501)
  350. immr->im_ioport.iop_pddir = 0x1c58; /* Pre rev. D */
  351. else
  352. immr->im_ioport.iop_pddir = 0x1fff; /* Rev. D and later */
  353. #else
  354. /*
  355. * Configure port A for MII.
  356. */
  357. #if defined(CONFIG_ICU862) && defined(CONFIG_SYS_DISCOVER_PHY)
  358. /*
  359. * On the ICU862 board the MII-MDC pin is routed to PD8 pin
  360. * * of CPU, so for this board we need to configure Utopia and
  361. * * enable PD8 to MII-MDC function
  362. */
  363. immr->im_ioport.iop_pdpar |= 0x4080;
  364. #endif
  365. /*
  366. * Has Utopia been configured?
  367. */
  368. if (immr->im_ioport.iop_pdpar & (0x8000 >> 1)) {
  369. /*
  370. * YES - Use MUXED mode for UTOPIA bus.
  371. * This frees Port A for use by MII (see 862UM table 41-6).
  372. */
  373. immr->im_ioport.utmode &= ~0x80;
  374. } else {
  375. /*
  376. * NO - set SPLIT mode for UTOPIA bus.
  377. *
  378. * This doesn't really effect UTOPIA (which isn't
  379. * enabled anyway) but just tells the 862
  380. * to use port A for MII (see 862UM table 41-6).
  381. */
  382. immr->im_ioport.utmode |= 0x80;
  383. }
  384. #endif /* !defined(CONFIG_ICU862) */
  385. #endif /* CONFIG_ETHER_ON_FEC1 */
  386. } else if (fecidx == 1) {
  387. #if defined(CONFIG_ETHER_ON_FEC2)
  388. #if defined(CONFIG_MPC885_FAMILY) /* MPC87x/88x have got 2 FECs and different pinout */
  389. #if !defined(CONFIG_RMII)
  390. immr->im_cpm.cp_pepar |= 0x0003fffc;
  391. immr->im_cpm.cp_pedir |= 0x0003fffc;
  392. immr->im_cpm.cp_peso &= ~0x000087fc;
  393. immr->im_cpm.cp_peso |= 0x00037800;
  394. immr->im_cpm.cp_cptr &= ~0x00000080;
  395. #else
  396. #if !defined(CONFIG_FEC2_PHY_NORXERR)
  397. immr->im_cpm.cp_pepar |= 0x00000010;
  398. immr->im_cpm.cp_pedir |= 0x00000010;
  399. immr->im_cpm.cp_peso &= ~0x00000010;
  400. #endif
  401. immr->im_cpm.cp_pepar |= 0x00039620;
  402. immr->im_cpm.cp_pedir |= 0x00039620;
  403. immr->im_cpm.cp_peso |= 0x00031000;
  404. immr->im_cpm.cp_peso &= ~0x00008620;
  405. immr->im_cpm.cp_cptr |= 0x00000080;
  406. immr->im_cpm.cp_cptr &= ~0x00000028;
  407. #endif /* CONFIG_RMII */
  408. #endif /* CONFIG_MPC885_FAMILY */
  409. #endif /* CONFIG_ETHER_ON_FEC2 */
  410. }
  411. }
  412. static int fec_reset(volatile fec_t *fecp)
  413. {
  414. int i;
  415. /* Whack a reset.
  416. * A delay is required between a reset of the FEC block and
  417. * initialization of other FEC registers because the reset takes
  418. * some time to complete. If you don't delay, subsequent writes
  419. * to FEC registers might get killed by the reset routine which is
  420. * still in progress.
  421. */
  422. fecp->fec_ecntrl = FEC_ECNTRL_PINMUX | FEC_ECNTRL_RESET;
  423. for (i = 0;
  424. (fecp->fec_ecntrl & FEC_ECNTRL_RESET) && (i < FEC_RESET_DELAY);
  425. ++i) {
  426. udelay (1);
  427. }
  428. if (i == FEC_RESET_DELAY)
  429. return -1;
  430. return 0;
  431. }
  432. static int fec_init (struct eth_device *dev, bd_t * bd)
  433. {
  434. struct ether_fcc_info_s *efis = dev->priv;
  435. volatile immap_t *immr = (immap_t *) CONFIG_SYS_IMMR;
  436. volatile fec_t *fecp =
  437. (volatile fec_t *) (CONFIG_SYS_IMMR + efis->fecp_offset);
  438. int i;
  439. #if defined(CONFIG_MII) || defined(CONFIG_CMD_MII)
  440. /* the MII interface is connected to FEC1
  441. * so for the miiphy_xxx function to work we must
  442. * call mii_init since fec_halt messes the thing up
  443. */
  444. if (efis->ether_index != 0)
  445. __mii_init();
  446. #endif
  447. if (fec_reset(fecp) < 0)
  448. printf ("FEC_RESET_DELAY timeout\n");
  449. /* We use strictly polling mode only
  450. */
  451. fecp->fec_imask = 0;
  452. /* Clear any pending interrupt
  453. */
  454. fecp->fec_ievent = 0xffc0;
  455. /* No need to set the IVEC register */
  456. /* Set station address
  457. */
  458. #define ea dev->enetaddr
  459. fecp->fec_addr_low = (ea[0] << 24) | (ea[1] << 16) | (ea[2] << 8) | (ea[3]);
  460. fecp->fec_addr_high = (ea[4] << 8) | (ea[5]);
  461. #undef ea
  462. #if defined(CONFIG_CMD_CDP)
  463. /*
  464. * Turn on multicast address hash table
  465. */
  466. fecp->fec_hash_table_high = 0xffffffff;
  467. fecp->fec_hash_table_low = 0xffffffff;
  468. #else
  469. /* Clear multicast address hash table
  470. */
  471. fecp->fec_hash_table_high = 0;
  472. fecp->fec_hash_table_low = 0;
  473. #endif
  474. /* Set maximum receive buffer size.
  475. */
  476. fecp->fec_r_buff_size = PKT_MAXBLR_SIZE;
  477. /* Set maximum frame length
  478. */
  479. fecp->fec_r_hash = PKT_MAXBUF_SIZE;
  480. /*
  481. * Setup Buffers and Buffer Desriptors
  482. */
  483. rxIdx = 0;
  484. txIdx = 0;
  485. if (!rtx) {
  486. #ifdef CONFIG_SYS_ALLOC_DPRAM
  487. rtx = (RTXBD *) (immr->im_cpm.cp_dpmem +
  488. dpram_alloc_align (sizeof (RTXBD), 8));
  489. #else
  490. rtx = (RTXBD *) (immr->im_cpm.cp_dpmem + CPM_FEC_BASE);
  491. #endif
  492. }
  493. /*
  494. * Setup Receiver Buffer Descriptors (13.14.24.18)
  495. * Settings:
  496. * Empty, Wrap
  497. */
  498. for (i = 0; i < PKTBUFSRX; i++) {
  499. rtx->rxbd[i].cbd_sc = BD_ENET_RX_EMPTY;
  500. rtx->rxbd[i].cbd_datlen = 0; /* Reset */
  501. rtx->rxbd[i].cbd_bufaddr = (uint) NetRxPackets[i];
  502. }
  503. rtx->rxbd[PKTBUFSRX - 1].cbd_sc |= BD_ENET_RX_WRAP;
  504. /*
  505. * Setup Ethernet Transmitter Buffer Descriptors (13.14.24.19)
  506. * Settings:
  507. * Last, Tx CRC
  508. */
  509. for (i = 0; i < TX_BUF_CNT; i++) {
  510. rtx->txbd[i].cbd_sc = BD_ENET_TX_LAST | BD_ENET_TX_TC;
  511. rtx->txbd[i].cbd_datlen = 0; /* Reset */
  512. rtx->txbd[i].cbd_bufaddr = (uint) (&txbuf[0]);
  513. }
  514. rtx->txbd[TX_BUF_CNT - 1].cbd_sc |= BD_ENET_TX_WRAP;
  515. /* Set receive and transmit descriptor base
  516. */
  517. fecp->fec_r_des_start = (unsigned int) (&rtx->rxbd[0]);
  518. fecp->fec_x_des_start = (unsigned int) (&rtx->txbd[0]);
  519. /* Enable MII mode
  520. */
  521. #if 0 /* Full duplex mode */
  522. fecp->fec_r_cntrl = FEC_RCNTRL_MII_MODE;
  523. fecp->fec_x_cntrl = FEC_TCNTRL_FDEN;
  524. #else /* Half duplex mode */
  525. fecp->fec_r_cntrl = FEC_RCNTRL_MII_MODE | FEC_RCNTRL_DRT;
  526. fecp->fec_x_cntrl = 0;
  527. #endif
  528. /* Enable big endian and don't care about SDMA FC.
  529. */
  530. fecp->fec_fun_code = 0x78000000;
  531. /*
  532. * Setup the pin configuration of the FEC
  533. */
  534. fec_pin_init (efis->ether_index);
  535. rxIdx = 0;
  536. txIdx = 0;
  537. /*
  538. * Now enable the transmit and receive processing
  539. */
  540. fecp->fec_ecntrl = FEC_ECNTRL_PINMUX | FEC_ECNTRL_ETHER_EN;
  541. if (efis->phy_addr == -1) {
  542. #ifdef CONFIG_SYS_DISCOVER_PHY
  543. /*
  544. * wait for the PHY to wake up after reset
  545. */
  546. efis->actual_phy_addr = mii_discover_phy (dev);
  547. if (efis->actual_phy_addr == -1) {
  548. printf ("Unable to discover phy!\n");
  549. return -1;
  550. }
  551. #else
  552. efis->actual_phy_addr = -1;
  553. #endif
  554. } else {
  555. efis->actual_phy_addr = efis->phy_addr;
  556. }
  557. #if defined(CONFIG_MII) && defined(CONFIG_RMII)
  558. /*
  559. * adapt the RMII speed to the speed of the phy
  560. */
  561. if (miiphy_speed (dev->name, efis->actual_phy_addr) == _100BASET) {
  562. fec_100Mbps (dev);
  563. } else {
  564. fec_10Mbps (dev);
  565. }
  566. #endif
  567. #if defined(CONFIG_MII)
  568. /*
  569. * adapt to the half/full speed settings
  570. */
  571. if (miiphy_duplex (dev->name, efis->actual_phy_addr) == FULL) {
  572. fec_full_duplex (dev);
  573. } else {
  574. fec_half_duplex (dev);
  575. }
  576. #endif
  577. /* And last, try to fill Rx Buffer Descriptors */
  578. fecp->fec_r_des_active = 0x01000000; /* Descriptor polling active */
  579. efis->initialized = 1;
  580. return 0;
  581. }
  582. static void fec_halt(struct eth_device* dev)
  583. {
  584. struct ether_fcc_info_s *efis = dev->priv;
  585. volatile fec_t *fecp = (volatile fec_t *)(CONFIG_SYS_IMMR + efis->fecp_offset);
  586. int i;
  587. /* avoid halt if initialized; mii gets stuck otherwise */
  588. if (!efis->initialized)
  589. return;
  590. /* Whack a reset.
  591. * A delay is required between a reset of the FEC block and
  592. * initialization of other FEC registers because the reset takes
  593. * some time to complete. If you don't delay, subsequent writes
  594. * to FEC registers might get killed by the reset routine which is
  595. * still in progress.
  596. */
  597. fecp->fec_ecntrl = FEC_ECNTRL_PINMUX | FEC_ECNTRL_RESET;
  598. for (i = 0;
  599. (fecp->fec_ecntrl & FEC_ECNTRL_RESET) && (i < FEC_RESET_DELAY);
  600. ++i) {
  601. udelay (1);
  602. }
  603. if (i == FEC_RESET_DELAY) {
  604. printf ("FEC_RESET_DELAY timeout\n");
  605. return;
  606. }
  607. efis->initialized = 0;
  608. }
  609. #if defined(CONFIG_SYS_DISCOVER_PHY) || defined(CONFIG_MII) || defined(CONFIG_CMD_MII)
  610. /* Make MII read/write commands for the FEC.
  611. */
  612. #define mk_mii_read(ADDR, REG) (0x60020000 | ((ADDR << 23) | \
  613. (REG & 0x1f) << 18))
  614. #define mk_mii_write(ADDR, REG, VAL) (0x50020000 | ((ADDR << 23) | \
  615. (REG & 0x1f) << 18) | \
  616. (VAL & 0xffff))
  617. /* Interrupt events/masks.
  618. */
  619. #define FEC_ENET_HBERR ((uint)0x80000000) /* Heartbeat error */
  620. #define FEC_ENET_BABR ((uint)0x40000000) /* Babbling receiver */
  621. #define FEC_ENET_BABT ((uint)0x20000000) /* Babbling transmitter */
  622. #define FEC_ENET_GRA ((uint)0x10000000) /* Graceful stop complete */
  623. #define FEC_ENET_TXF ((uint)0x08000000) /* Full frame transmitted */
  624. #define FEC_ENET_TXB ((uint)0x04000000) /* A buffer was transmitted */
  625. #define FEC_ENET_RXF ((uint)0x02000000) /* Full frame received */
  626. #define FEC_ENET_RXB ((uint)0x01000000) /* A buffer was received */
  627. #define FEC_ENET_MII ((uint)0x00800000) /* MII interrupt */
  628. #define FEC_ENET_EBERR ((uint)0x00400000) /* SDMA bus error */
  629. /* PHY identification
  630. */
  631. #define PHY_ID_LXT970 0x78100000 /* LXT970 */
  632. #define PHY_ID_LXT971 0x001378e0 /* LXT971 and 972 */
  633. #define PHY_ID_82555 0x02a80150 /* Intel 82555 */
  634. #define PHY_ID_QS6612 0x01814400 /* QS6612 */
  635. #define PHY_ID_AMD79C784 0x00225610 /* AMD 79C784 */
  636. #define PHY_ID_LSI80225 0x0016f870 /* LSI 80225 */
  637. #define PHY_ID_LSI80225B 0x0016f880 /* LSI 80225/B */
  638. #define PHY_ID_DM9161 0x0181B880 /* Davicom DM9161 */
  639. #define PHY_ID_KSM8995M 0x00221450 /* MICREL KS8995MA */
  640. /* send command to phy using mii, wait for result */
  641. static uint
  642. mii_send(uint mii_cmd)
  643. {
  644. uint mii_reply;
  645. volatile fec_t *ep;
  646. int cnt;
  647. ep = &(((immap_t *)CONFIG_SYS_IMMR)->im_cpm.cp_fec);
  648. ep->fec_mii_data = mii_cmd; /* command to phy */
  649. /* wait for mii complete */
  650. cnt = 0;
  651. while (!(ep->fec_ievent & FEC_ENET_MII)) {
  652. if (++cnt > 1000) {
  653. printf("mii_send STUCK!\n");
  654. break;
  655. }
  656. }
  657. mii_reply = ep->fec_mii_data; /* result from phy */
  658. ep->fec_ievent = FEC_ENET_MII; /* clear MII complete */
  659. #if 0
  660. printf("%s[%d] %s: sent=0x%8.8x, reply=0x%8.8x\n",
  661. __FILE__,__LINE__,__FUNCTION__,mii_cmd,mii_reply);
  662. #endif
  663. return (mii_reply & 0xffff); /* data read from phy */
  664. }
  665. #endif
  666. #if defined(CONFIG_SYS_DISCOVER_PHY)
  667. static int mii_discover_phy(struct eth_device *dev)
  668. {
  669. #define MAX_PHY_PASSES 11
  670. uint phyno;
  671. int pass;
  672. uint phytype;
  673. int phyaddr;
  674. phyaddr = -1; /* didn't find a PHY yet */
  675. for (pass = 1; pass <= MAX_PHY_PASSES && phyaddr < 0; ++pass) {
  676. if (pass > 1) {
  677. /* PHY may need more time to recover from reset.
  678. * The LXT970 needs 50ms typical, no maximum is
  679. * specified, so wait 10ms before try again.
  680. * With 11 passes this gives it 100ms to wake up.
  681. */
  682. udelay(10000); /* wait 10ms */
  683. }
  684. for (phyno = 0; phyno < 32 && phyaddr < 0; ++phyno) {
  685. phytype = mii_send(mk_mii_read(phyno, MII_PHYSID2));
  686. #ifdef ET_DEBUG
  687. printf("PHY type 0x%x pass %d type ", phytype, pass);
  688. #endif
  689. if (phytype != 0xffff) {
  690. phyaddr = phyno;
  691. phytype |= mii_send(mk_mii_read(phyno,
  692. MII_PHYSID1)) << 16;
  693. #ifdef ET_DEBUG
  694. printf("PHY @ 0x%x pass %d type ",phyno,pass);
  695. switch (phytype & 0xfffffff0) {
  696. case PHY_ID_LXT970:
  697. printf("LXT970\n");
  698. break;
  699. case PHY_ID_LXT971:
  700. printf("LXT971\n");
  701. break;
  702. case PHY_ID_82555:
  703. printf("82555\n");
  704. break;
  705. case PHY_ID_QS6612:
  706. printf("QS6612\n");
  707. break;
  708. case PHY_ID_AMD79C784:
  709. printf("AMD79C784\n");
  710. break;
  711. case PHY_ID_LSI80225B:
  712. printf("LSI L80225/B\n");
  713. break;
  714. case PHY_ID_DM9161:
  715. printf("Davicom DM9161\n");
  716. break;
  717. case PHY_ID_KSM8995M:
  718. printf("MICREL KS8995M\n");
  719. break;
  720. default:
  721. printf("0x%08x\n", phytype);
  722. break;
  723. }
  724. #endif
  725. }
  726. }
  727. }
  728. if (phyaddr < 0) {
  729. printf("No PHY device found.\n");
  730. }
  731. return phyaddr;
  732. }
  733. #endif /* CONFIG_SYS_DISCOVER_PHY */
  734. #if (defined(CONFIG_MII) || defined(CONFIG_CMD_MII)) && !defined(CONFIG_BITBANGMII)
  735. /****************************************************************************
  736. * mii_init -- Initialize the MII via FEC 1 for MII command without ethernet
  737. * This function is a subset of eth_init
  738. ****************************************************************************
  739. */
  740. static void __mii_init(void)
  741. {
  742. volatile immap_t *immr = (immap_t *) CONFIG_SYS_IMMR;
  743. volatile fec_t *fecp = &(immr->im_cpm.cp_fec);
  744. if (fec_reset(fecp) < 0)
  745. printf ("FEC_RESET_DELAY timeout\n");
  746. /* We use strictly polling mode only
  747. */
  748. fecp->fec_imask = 0;
  749. /* Clear any pending interrupt
  750. */
  751. fecp->fec_ievent = 0xffc0;
  752. /* Now enable the transmit and receive processing
  753. */
  754. fecp->fec_ecntrl = FEC_ECNTRL_PINMUX | FEC_ECNTRL_ETHER_EN;
  755. }
  756. void mii_init (void)
  757. {
  758. int i;
  759. __mii_init();
  760. /* Setup the pin configuration of the FEC(s)
  761. */
  762. for (i = 0; i < sizeof(ether_fcc_info) / sizeof(ether_fcc_info[0]); i++)
  763. fec_pin_init(ether_fcc_info[i].ether_index);
  764. }
  765. /*****************************************************************************
  766. * Read and write a MII PHY register, routines used by MII Utilities
  767. *
  768. * FIXME: These routines are expected to return 0 on success, but mii_send
  769. * does _not_ return an error code. Maybe 0xFFFF means error, i.e.
  770. * no PHY connected...
  771. * For now always return 0.
  772. * FIXME: These routines only work after calling eth_init() at least once!
  773. * Otherwise they hang in mii_send() !!! Sorry!
  774. *****************************************************************************/
  775. int fec8xx_miiphy_read(const char *devname, unsigned char addr,
  776. unsigned char reg, unsigned short *value)
  777. {
  778. short rdreg; /* register working value */
  779. #ifdef MII_DEBUG
  780. printf ("miiphy_read(0x%x) @ 0x%x = ", reg, addr);
  781. #endif
  782. rdreg = mii_send(mk_mii_read(addr, reg));
  783. *value = rdreg;
  784. #ifdef MII_DEBUG
  785. printf ("0x%04x\n", *value);
  786. #endif
  787. return 0;
  788. }
  789. int fec8xx_miiphy_write(const char *devname, unsigned char addr,
  790. unsigned char reg, unsigned short value)
  791. {
  792. #ifdef MII_DEBUG
  793. printf ("miiphy_write(0x%x) @ 0x%x = ", reg, addr);
  794. #endif
  795. (void)mii_send(mk_mii_write(addr, reg, value));
  796. #ifdef MII_DEBUG
  797. printf ("0x%04x\n", value);
  798. #endif
  799. return 0;
  800. }
  801. #endif
  802. #endif