fec.c 25 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013
  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_NETTA) || defined(CONFIG_NETPHONE) || defined(CONFIG_NETTA2)
  309. {
  310. volatile fec_t *fecp;
  311. /*
  312. * only two FECs please
  313. */
  314. if ((unsigned int)fecidx >= 2)
  315. hang();
  316. if (fecidx == 0)
  317. fecp = &immr->im_cpm.cp_fec1;
  318. else
  319. fecp = &immr->im_cpm.cp_fec2;
  320. /* our PHYs are the limit at 2.5 MHz */
  321. fecp->fec_mii_speed <<= 1;
  322. }
  323. #endif
  324. #if defined(CONFIG_MPC885_FAMILY) && defined(WANT_MII)
  325. /* use MDC for MII */
  326. immr->im_ioport.iop_pdpar |= 0x0080;
  327. immr->im_ioport.iop_pddir &= ~0x0080;
  328. #endif
  329. if (fecidx == 0) {
  330. #if defined(CONFIG_ETHER_ON_FEC1)
  331. #if defined(CONFIG_MPC885_FAMILY) /* MPC87x/88x have got 2 FECs and different pinout */
  332. #if !defined(CONFIG_RMII)
  333. immr->im_ioport.iop_papar |= 0xf830;
  334. immr->im_ioport.iop_padir |= 0x0830;
  335. immr->im_ioport.iop_padir &= ~0xf000;
  336. immr->im_cpm.cp_pbpar |= 0x00001001;
  337. immr->im_cpm.cp_pbdir &= ~0x00001001;
  338. immr->im_ioport.iop_pcpar |= 0x000c;
  339. immr->im_ioport.iop_pcdir &= ~0x000c;
  340. immr->im_cpm.cp_pepar |= 0x00000003;
  341. immr->im_cpm.cp_pedir |= 0x00000003;
  342. immr->im_cpm.cp_peso &= ~0x00000003;
  343. immr->im_cpm.cp_cptr &= ~0x00000100;
  344. #else
  345. #if !defined(CONFIG_FEC1_PHY_NORXERR)
  346. immr->im_ioport.iop_papar |= 0x1000;
  347. immr->im_ioport.iop_padir &= ~0x1000;
  348. #endif
  349. immr->im_ioport.iop_papar |= 0xe810;
  350. immr->im_ioport.iop_padir |= 0x0810;
  351. immr->im_ioport.iop_padir &= ~0xe000;
  352. immr->im_cpm.cp_pbpar |= 0x00000001;
  353. immr->im_cpm.cp_pbdir &= ~0x00000001;
  354. immr->im_cpm.cp_cptr |= 0x00000100;
  355. immr->im_cpm.cp_cptr &= ~0x00000050;
  356. #endif /* !CONFIG_RMII */
  357. #elif !defined(CONFIG_ICU862)
  358. /*
  359. * Configure all of port D for MII.
  360. */
  361. immr->im_ioport.iop_pdpar = 0x1fff;
  362. /*
  363. * Bits moved from Rev. D onward
  364. */
  365. if ((get_immr(0) & 0xffff) < 0x0501)
  366. immr->im_ioport.iop_pddir = 0x1c58; /* Pre rev. D */
  367. else
  368. immr->im_ioport.iop_pddir = 0x1fff; /* Rev. D and later */
  369. #else
  370. /*
  371. * Configure port A for MII.
  372. */
  373. #if defined(CONFIG_ICU862) && defined(CONFIG_SYS_DISCOVER_PHY)
  374. /*
  375. * On the ICU862 board the MII-MDC pin is routed to PD8 pin
  376. * * of CPU, so for this board we need to configure Utopia and
  377. * * enable PD8 to MII-MDC function
  378. */
  379. immr->im_ioport.iop_pdpar |= 0x4080;
  380. #endif
  381. /*
  382. * Has Utopia been configured?
  383. */
  384. if (immr->im_ioport.iop_pdpar & (0x8000 >> 1)) {
  385. /*
  386. * YES - Use MUXED mode for UTOPIA bus.
  387. * This frees Port A for use by MII (see 862UM table 41-6).
  388. */
  389. immr->im_ioport.utmode &= ~0x80;
  390. } else {
  391. /*
  392. * NO - set SPLIT mode for UTOPIA bus.
  393. *
  394. * This doesn't really effect UTOPIA (which isn't
  395. * enabled anyway) but just tells the 862
  396. * to use port A for MII (see 862UM table 41-6).
  397. */
  398. immr->im_ioport.utmode |= 0x80;
  399. }
  400. #endif /* !defined(CONFIG_ICU862) */
  401. #endif /* CONFIG_ETHER_ON_FEC1 */
  402. } else if (fecidx == 1) {
  403. #if defined(CONFIG_ETHER_ON_FEC2)
  404. #if defined(CONFIG_MPC885_FAMILY) /* MPC87x/88x have got 2 FECs and different pinout */
  405. #if !defined(CONFIG_RMII)
  406. immr->im_cpm.cp_pepar |= 0x0003fffc;
  407. immr->im_cpm.cp_pedir |= 0x0003fffc;
  408. immr->im_cpm.cp_peso &= ~0x000087fc;
  409. immr->im_cpm.cp_peso |= 0x00037800;
  410. immr->im_cpm.cp_cptr &= ~0x00000080;
  411. #else
  412. #if !defined(CONFIG_FEC2_PHY_NORXERR)
  413. immr->im_cpm.cp_pepar |= 0x00000010;
  414. immr->im_cpm.cp_pedir |= 0x00000010;
  415. immr->im_cpm.cp_peso &= ~0x00000010;
  416. #endif
  417. immr->im_cpm.cp_pepar |= 0x00039620;
  418. immr->im_cpm.cp_pedir |= 0x00039620;
  419. immr->im_cpm.cp_peso |= 0x00031000;
  420. immr->im_cpm.cp_peso &= ~0x00008620;
  421. immr->im_cpm.cp_cptr |= 0x00000080;
  422. immr->im_cpm.cp_cptr &= ~0x00000028;
  423. #endif /* CONFIG_RMII */
  424. #endif /* CONFIG_MPC885_FAMILY */
  425. #endif /* CONFIG_ETHER_ON_FEC2 */
  426. }
  427. }
  428. static int fec_reset(volatile fec_t *fecp)
  429. {
  430. int i;
  431. /* Whack a reset.
  432. * A delay is required between a reset of the FEC block and
  433. * initialization of other FEC registers because the reset takes
  434. * some time to complete. If you don't delay, subsequent writes
  435. * to FEC registers might get killed by the reset routine which is
  436. * still in progress.
  437. */
  438. fecp->fec_ecntrl = FEC_ECNTRL_PINMUX | FEC_ECNTRL_RESET;
  439. for (i = 0;
  440. (fecp->fec_ecntrl & FEC_ECNTRL_RESET) && (i < FEC_RESET_DELAY);
  441. ++i) {
  442. udelay (1);
  443. }
  444. if (i == FEC_RESET_DELAY)
  445. return -1;
  446. return 0;
  447. }
  448. static int fec_init (struct eth_device *dev, bd_t * bd)
  449. {
  450. struct ether_fcc_info_s *efis = dev->priv;
  451. volatile immap_t *immr = (immap_t *) CONFIG_SYS_IMMR;
  452. volatile fec_t *fecp =
  453. (volatile fec_t *) (CONFIG_SYS_IMMR + efis->fecp_offset);
  454. int i;
  455. if (efis->ether_index == 0) {
  456. #if defined(CONFIG_FADS) /* FADS family uses FPGA (BCSR) to control PHYs */
  457. #if defined(CONFIG_MPC885ADS)
  458. *(vu_char *) BCSR5 &= ~(BCSR5_MII1_EN | BCSR5_MII1_RST);
  459. #else
  460. /* configure FADS for fast (FEC) ethernet, half-duplex */
  461. /* The LXT970 needs about 50ms to recover from reset, so
  462. * wait for it by discovering the PHY before leaving eth_init().
  463. */
  464. {
  465. volatile uint *bcsr4 = (volatile uint *) BCSR4;
  466. *bcsr4 = (*bcsr4 & ~(BCSR4_FETH_EN | BCSR4_FETHCFG1))
  467. | (BCSR4_FETHCFG0 | BCSR4_FETHFDE |
  468. BCSR4_FETHRST);
  469. /* reset the LXT970 PHY */
  470. *bcsr4 &= ~BCSR4_FETHRST;
  471. udelay (10);
  472. *bcsr4 |= BCSR4_FETHRST;
  473. udelay (10);
  474. }
  475. #endif /* CONFIG_MPC885ADS */
  476. #endif /* CONFIG_FADS */
  477. }
  478. #if defined(CONFIG_MII) || defined(CONFIG_CMD_MII)
  479. /* the MII interface is connected to FEC1
  480. * so for the miiphy_xxx function to work we must
  481. * call mii_init since fec_halt messes the thing up
  482. */
  483. if (efis->ether_index != 0)
  484. __mii_init();
  485. #endif
  486. if (fec_reset(fecp) < 0)
  487. printf ("FEC_RESET_DELAY timeout\n");
  488. /* We use strictly polling mode only
  489. */
  490. fecp->fec_imask = 0;
  491. /* Clear any pending interrupt
  492. */
  493. fecp->fec_ievent = 0xffc0;
  494. /* No need to set the IVEC register */
  495. /* Set station address
  496. */
  497. #define ea dev->enetaddr
  498. fecp->fec_addr_low = (ea[0] << 24) | (ea[1] << 16) | (ea[2] << 8) | (ea[3]);
  499. fecp->fec_addr_high = (ea[4] << 8) | (ea[5]);
  500. #undef ea
  501. #if defined(CONFIG_CMD_CDP)
  502. /*
  503. * Turn on multicast address hash table
  504. */
  505. fecp->fec_hash_table_high = 0xffffffff;
  506. fecp->fec_hash_table_low = 0xffffffff;
  507. #else
  508. /* Clear multicast address hash table
  509. */
  510. fecp->fec_hash_table_high = 0;
  511. fecp->fec_hash_table_low = 0;
  512. #endif
  513. /* Set maximum receive buffer size.
  514. */
  515. fecp->fec_r_buff_size = PKT_MAXBLR_SIZE;
  516. /* Set maximum frame length
  517. */
  518. fecp->fec_r_hash = PKT_MAXBUF_SIZE;
  519. /*
  520. * Setup Buffers and Buffer Desriptors
  521. */
  522. rxIdx = 0;
  523. txIdx = 0;
  524. if (!rtx) {
  525. #ifdef CONFIG_SYS_ALLOC_DPRAM
  526. rtx = (RTXBD *) (immr->im_cpm.cp_dpmem +
  527. dpram_alloc_align (sizeof (RTXBD), 8));
  528. #else
  529. rtx = (RTXBD *) (immr->im_cpm.cp_dpmem + CPM_FEC_BASE);
  530. #endif
  531. }
  532. /*
  533. * Setup Receiver Buffer Descriptors (13.14.24.18)
  534. * Settings:
  535. * Empty, Wrap
  536. */
  537. for (i = 0; i < PKTBUFSRX; i++) {
  538. rtx->rxbd[i].cbd_sc = BD_ENET_RX_EMPTY;
  539. rtx->rxbd[i].cbd_datlen = 0; /* Reset */
  540. rtx->rxbd[i].cbd_bufaddr = (uint) NetRxPackets[i];
  541. }
  542. rtx->rxbd[PKTBUFSRX - 1].cbd_sc |= BD_ENET_RX_WRAP;
  543. /*
  544. * Setup Ethernet Transmitter Buffer Descriptors (13.14.24.19)
  545. * Settings:
  546. * Last, Tx CRC
  547. */
  548. for (i = 0; i < TX_BUF_CNT; i++) {
  549. rtx->txbd[i].cbd_sc = BD_ENET_TX_LAST | BD_ENET_TX_TC;
  550. rtx->txbd[i].cbd_datlen = 0; /* Reset */
  551. rtx->txbd[i].cbd_bufaddr = (uint) (&txbuf[0]);
  552. }
  553. rtx->txbd[TX_BUF_CNT - 1].cbd_sc |= BD_ENET_TX_WRAP;
  554. /* Set receive and transmit descriptor base
  555. */
  556. fecp->fec_r_des_start = (unsigned int) (&rtx->rxbd[0]);
  557. fecp->fec_x_des_start = (unsigned int) (&rtx->txbd[0]);
  558. /* Enable MII mode
  559. */
  560. #if 0 /* Full duplex mode */
  561. fecp->fec_r_cntrl = FEC_RCNTRL_MII_MODE;
  562. fecp->fec_x_cntrl = FEC_TCNTRL_FDEN;
  563. #else /* Half duplex mode */
  564. fecp->fec_r_cntrl = FEC_RCNTRL_MII_MODE | FEC_RCNTRL_DRT;
  565. fecp->fec_x_cntrl = 0;
  566. #endif
  567. /* Enable big endian and don't care about SDMA FC.
  568. */
  569. fecp->fec_fun_code = 0x78000000;
  570. /*
  571. * Setup the pin configuration of the FEC
  572. */
  573. fec_pin_init (efis->ether_index);
  574. rxIdx = 0;
  575. txIdx = 0;
  576. /*
  577. * Now enable the transmit and receive processing
  578. */
  579. fecp->fec_ecntrl = FEC_ECNTRL_PINMUX | FEC_ECNTRL_ETHER_EN;
  580. if (efis->phy_addr == -1) {
  581. #ifdef CONFIG_SYS_DISCOVER_PHY
  582. /*
  583. * wait for the PHY to wake up after reset
  584. */
  585. efis->actual_phy_addr = mii_discover_phy (dev);
  586. if (efis->actual_phy_addr == -1) {
  587. printf ("Unable to discover phy!\n");
  588. return -1;
  589. }
  590. #else
  591. efis->actual_phy_addr = -1;
  592. #endif
  593. } else {
  594. efis->actual_phy_addr = efis->phy_addr;
  595. }
  596. #if defined(CONFIG_MII) && defined(CONFIG_RMII)
  597. /*
  598. * adapt the RMII speed to the speed of the phy
  599. */
  600. if (miiphy_speed (dev->name, efis->actual_phy_addr) == _100BASET) {
  601. fec_100Mbps (dev);
  602. } else {
  603. fec_10Mbps (dev);
  604. }
  605. #endif
  606. #if defined(CONFIG_MII)
  607. /*
  608. * adapt to the half/full speed settings
  609. */
  610. if (miiphy_duplex (dev->name, efis->actual_phy_addr) == FULL) {
  611. fec_full_duplex (dev);
  612. } else {
  613. fec_half_duplex (dev);
  614. }
  615. #endif
  616. /* And last, try to fill Rx Buffer Descriptors */
  617. fecp->fec_r_des_active = 0x01000000; /* Descriptor polling active */
  618. efis->initialized = 1;
  619. return 0;
  620. }
  621. static void fec_halt(struct eth_device* dev)
  622. {
  623. struct ether_fcc_info_s *efis = dev->priv;
  624. volatile fec_t *fecp = (volatile fec_t *)(CONFIG_SYS_IMMR + efis->fecp_offset);
  625. int i;
  626. /* avoid halt if initialized; mii gets stuck otherwise */
  627. if (!efis->initialized)
  628. return;
  629. /* Whack a reset.
  630. * A delay is required between a reset of the FEC block and
  631. * initialization of other FEC registers because the reset takes
  632. * some time to complete. If you don't delay, subsequent writes
  633. * to FEC registers might get killed by the reset routine which is
  634. * still in progress.
  635. */
  636. fecp->fec_ecntrl = FEC_ECNTRL_PINMUX | FEC_ECNTRL_RESET;
  637. for (i = 0;
  638. (fecp->fec_ecntrl & FEC_ECNTRL_RESET) && (i < FEC_RESET_DELAY);
  639. ++i) {
  640. udelay (1);
  641. }
  642. if (i == FEC_RESET_DELAY) {
  643. printf ("FEC_RESET_DELAY timeout\n");
  644. return;
  645. }
  646. efis->initialized = 0;
  647. }
  648. #if defined(CONFIG_SYS_DISCOVER_PHY) || defined(CONFIG_MII) || defined(CONFIG_CMD_MII)
  649. /* Make MII read/write commands for the FEC.
  650. */
  651. #define mk_mii_read(ADDR, REG) (0x60020000 | ((ADDR << 23) | \
  652. (REG & 0x1f) << 18))
  653. #define mk_mii_write(ADDR, REG, VAL) (0x50020000 | ((ADDR << 23) | \
  654. (REG & 0x1f) << 18) | \
  655. (VAL & 0xffff))
  656. /* Interrupt events/masks.
  657. */
  658. #define FEC_ENET_HBERR ((uint)0x80000000) /* Heartbeat error */
  659. #define FEC_ENET_BABR ((uint)0x40000000) /* Babbling receiver */
  660. #define FEC_ENET_BABT ((uint)0x20000000) /* Babbling transmitter */
  661. #define FEC_ENET_GRA ((uint)0x10000000) /* Graceful stop complete */
  662. #define FEC_ENET_TXF ((uint)0x08000000) /* Full frame transmitted */
  663. #define FEC_ENET_TXB ((uint)0x04000000) /* A buffer was transmitted */
  664. #define FEC_ENET_RXF ((uint)0x02000000) /* Full frame received */
  665. #define FEC_ENET_RXB ((uint)0x01000000) /* A buffer was received */
  666. #define FEC_ENET_MII ((uint)0x00800000) /* MII interrupt */
  667. #define FEC_ENET_EBERR ((uint)0x00400000) /* SDMA bus error */
  668. /* PHY identification
  669. */
  670. #define PHY_ID_LXT970 0x78100000 /* LXT970 */
  671. #define PHY_ID_LXT971 0x001378e0 /* LXT971 and 972 */
  672. #define PHY_ID_82555 0x02a80150 /* Intel 82555 */
  673. #define PHY_ID_QS6612 0x01814400 /* QS6612 */
  674. #define PHY_ID_AMD79C784 0x00225610 /* AMD 79C784 */
  675. #define PHY_ID_LSI80225 0x0016f870 /* LSI 80225 */
  676. #define PHY_ID_LSI80225B 0x0016f880 /* LSI 80225/B */
  677. #define PHY_ID_DM9161 0x0181B880 /* Davicom DM9161 */
  678. #define PHY_ID_KSM8995M 0x00221450 /* MICREL KS8995MA */
  679. /* send command to phy using mii, wait for result */
  680. static uint
  681. mii_send(uint mii_cmd)
  682. {
  683. uint mii_reply;
  684. volatile fec_t *ep;
  685. int cnt;
  686. ep = &(((immap_t *)CONFIG_SYS_IMMR)->im_cpm.cp_fec);
  687. ep->fec_mii_data = mii_cmd; /* command to phy */
  688. /* wait for mii complete */
  689. cnt = 0;
  690. while (!(ep->fec_ievent & FEC_ENET_MII)) {
  691. if (++cnt > 1000) {
  692. printf("mii_send STUCK!\n");
  693. break;
  694. }
  695. }
  696. mii_reply = ep->fec_mii_data; /* result from phy */
  697. ep->fec_ievent = FEC_ENET_MII; /* clear MII complete */
  698. #if 0
  699. printf("%s[%d] %s: sent=0x%8.8x, reply=0x%8.8x\n",
  700. __FILE__,__LINE__,__FUNCTION__,mii_cmd,mii_reply);
  701. #endif
  702. return (mii_reply & 0xffff); /* data read from phy */
  703. }
  704. #endif
  705. #if defined(CONFIG_SYS_DISCOVER_PHY)
  706. static int mii_discover_phy(struct eth_device *dev)
  707. {
  708. #define MAX_PHY_PASSES 11
  709. uint phyno;
  710. int pass;
  711. uint phytype;
  712. int phyaddr;
  713. phyaddr = -1; /* didn't find a PHY yet */
  714. for (pass = 1; pass <= MAX_PHY_PASSES && phyaddr < 0; ++pass) {
  715. if (pass > 1) {
  716. /* PHY may need more time to recover from reset.
  717. * The LXT970 needs 50ms typical, no maximum is
  718. * specified, so wait 10ms before try again.
  719. * With 11 passes this gives it 100ms to wake up.
  720. */
  721. udelay(10000); /* wait 10ms */
  722. }
  723. for (phyno = 0; phyno < 32 && phyaddr < 0; ++phyno) {
  724. phytype = mii_send(mk_mii_read(phyno, MII_PHYSID2));
  725. #ifdef ET_DEBUG
  726. printf("PHY type 0x%x pass %d type ", phytype, pass);
  727. #endif
  728. if (phytype != 0xffff) {
  729. phyaddr = phyno;
  730. phytype |= mii_send(mk_mii_read(phyno,
  731. MII_PHYSID1)) << 16;
  732. #ifdef ET_DEBUG
  733. printf("PHY @ 0x%x pass %d type ",phyno,pass);
  734. switch (phytype & 0xfffffff0) {
  735. case PHY_ID_LXT970:
  736. printf("LXT970\n");
  737. break;
  738. case PHY_ID_LXT971:
  739. printf("LXT971\n");
  740. break;
  741. case PHY_ID_82555:
  742. printf("82555\n");
  743. break;
  744. case PHY_ID_QS6612:
  745. printf("QS6612\n");
  746. break;
  747. case PHY_ID_AMD79C784:
  748. printf("AMD79C784\n");
  749. break;
  750. case PHY_ID_LSI80225B:
  751. printf("LSI L80225/B\n");
  752. break;
  753. case PHY_ID_DM9161:
  754. printf("Davicom DM9161\n");
  755. break;
  756. case PHY_ID_KSM8995M:
  757. printf("MICREL KS8995M\n");
  758. break;
  759. default:
  760. printf("0x%08x\n", phytype);
  761. break;
  762. }
  763. #endif
  764. }
  765. }
  766. }
  767. if (phyaddr < 0) {
  768. printf("No PHY device found.\n");
  769. }
  770. return phyaddr;
  771. }
  772. #endif /* CONFIG_SYS_DISCOVER_PHY */
  773. #if (defined(CONFIG_MII) || defined(CONFIG_CMD_MII)) && !defined(CONFIG_BITBANGMII)
  774. /****************************************************************************
  775. * mii_init -- Initialize the MII via FEC 1 for MII command without ethernet
  776. * This function is a subset of eth_init
  777. ****************************************************************************
  778. */
  779. static void __mii_init(void)
  780. {
  781. volatile immap_t *immr = (immap_t *) CONFIG_SYS_IMMR;
  782. volatile fec_t *fecp = &(immr->im_cpm.cp_fec);
  783. if (fec_reset(fecp) < 0)
  784. printf ("FEC_RESET_DELAY timeout\n");
  785. /* We use strictly polling mode only
  786. */
  787. fecp->fec_imask = 0;
  788. /* Clear any pending interrupt
  789. */
  790. fecp->fec_ievent = 0xffc0;
  791. /* Now enable the transmit and receive processing
  792. */
  793. fecp->fec_ecntrl = FEC_ECNTRL_PINMUX | FEC_ECNTRL_ETHER_EN;
  794. }
  795. void mii_init (void)
  796. {
  797. int i;
  798. __mii_init();
  799. /* Setup the pin configuration of the FEC(s)
  800. */
  801. for (i = 0; i < sizeof(ether_fcc_info) / sizeof(ether_fcc_info[0]); i++)
  802. fec_pin_init(ether_fcc_info[i].ether_index);
  803. }
  804. /*****************************************************************************
  805. * Read and write a MII PHY register, routines used by MII Utilities
  806. *
  807. * FIXME: These routines are expected to return 0 on success, but mii_send
  808. * does _not_ return an error code. Maybe 0xFFFF means error, i.e.
  809. * no PHY connected...
  810. * For now always return 0.
  811. * FIXME: These routines only work after calling eth_init() at least once!
  812. * Otherwise they hang in mii_send() !!! Sorry!
  813. *****************************************************************************/
  814. int fec8xx_miiphy_read(const char *devname, unsigned char addr,
  815. unsigned char reg, unsigned short *value)
  816. {
  817. short rdreg; /* register working value */
  818. #ifdef MII_DEBUG
  819. printf ("miiphy_read(0x%x) @ 0x%x = ", reg, addr);
  820. #endif
  821. rdreg = mii_send(mk_mii_read(addr, reg));
  822. *value = rdreg;
  823. #ifdef MII_DEBUG
  824. printf ("0x%04x\n", *value);
  825. #endif
  826. return 0;
  827. }
  828. int fec8xx_miiphy_write(const char *devname, unsigned char addr,
  829. unsigned char reg, unsigned short value)
  830. {
  831. #ifdef MII_DEBUG
  832. printf ("miiphy_write(0x%x) @ 0x%x = ", reg, addr);
  833. #endif
  834. (void)mii_send(mk_mii_write(addr, reg, value));
  835. #ifdef MII_DEBUG
  836. printf ("0x%04x\n", value);
  837. #endif
  838. return 0;
  839. }
  840. #endif
  841. #endif