fec.c 26 KB

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