fec.c 25 KB

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