fec.c 26 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027
  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. #warning this configuration is not tested; please report if it works
  417. immr->im_cpm.cp_pepar |= 0x0003fffc;
  418. immr->im_cpm.cp_pedir |= 0x0003fffc;
  419. immr->im_cpm.cp_peso &= ~0x000087fc;
  420. immr->im_cpm.cp_peso |= 0x00037800;
  421. immr->im_cpm.cp_cptr &= ~0x00000080;
  422. #else
  423. #if !defined(CONFIG_FEC2_PHY_NORXERR)
  424. immr->im_cpm.cp_pepar |= 0x00000010;
  425. immr->im_cpm.cp_pedir |= 0x00000010;
  426. immr->im_cpm.cp_peso &= ~0x00000010;
  427. #endif
  428. immr->im_cpm.cp_pepar |= 0x00039620;
  429. immr->im_cpm.cp_pedir |= 0x00039620;
  430. immr->im_cpm.cp_peso |= 0x00031000;
  431. immr->im_cpm.cp_peso &= ~0x00008620;
  432. immr->im_cpm.cp_cptr |= 0x00000080;
  433. immr->im_cpm.cp_cptr &= ~0x00000028;
  434. #endif /* CONFIG_RMII */
  435. #endif /* CONFIG_MPC885_FAMILY */
  436. #endif /* CONFIG_ETHER_ON_FEC2 */
  437. }
  438. }
  439. static int fec_init (struct eth_device *dev, bd_t * bd)
  440. {
  441. struct ether_fcc_info_s *efis = dev->priv;
  442. volatile immap_t *immr = (immap_t *) CFG_IMMR;
  443. volatile fec_t *fecp =
  444. (volatile fec_t *) (CFG_IMMR + efis->fecp_offset);
  445. int i;
  446. if (efis->ether_index == 0) {
  447. #if defined(CONFIG_FADS) /* FADS family uses FPGA (BCSR) to control PHYs */
  448. #if defined(CONFIG_MPC885ADS)
  449. *(vu_char *) BCSR5 &= ~(BCSR5_MII1_EN | BCSR5_MII1_RST);
  450. #else
  451. /* configure FADS for fast (FEC) ethernet, half-duplex */
  452. /* The LXT970 needs about 50ms to recover from reset, so
  453. * wait for it by discovering the PHY before leaving eth_init().
  454. */
  455. {
  456. volatile uint *bcsr4 = (volatile uint *) BCSR4;
  457. *bcsr4 = (*bcsr4 & ~(BCSR4_FETH_EN | BCSR4_FETHCFG1))
  458. | (BCSR4_FETHCFG0 | BCSR4_FETHFDE |
  459. BCSR4_FETHRST);
  460. /* reset the LXT970 PHY */
  461. *bcsr4 &= ~BCSR4_FETHRST;
  462. udelay (10);
  463. *bcsr4 |= BCSR4_FETHRST;
  464. udelay (10);
  465. }
  466. #endif /* CONFIG_MPC885ADS */
  467. #endif /* CONFIG_FADS */
  468. }
  469. /* Whack a reset.
  470. * A delay is required between a reset of the FEC block and
  471. * initialization of other FEC registers because the reset takes
  472. * some time to complete. If you don't delay, subsequent writes
  473. * to FEC registers might get killed by the reset routine which is
  474. * still in progress.
  475. */
  476. fecp->fec_ecntrl = FEC_ECNTRL_PINMUX | FEC_ECNTRL_RESET;
  477. for (i = 0;
  478. (fecp->fec_ecntrl & FEC_ECNTRL_RESET) && (i < FEC_RESET_DELAY);
  479. ++i) {
  480. udelay (1);
  481. }
  482. if (i == FEC_RESET_DELAY) {
  483. printf ("FEC_RESET_DELAY timeout\n");
  484. return 0;
  485. }
  486. /* We use strictly polling mode only
  487. */
  488. fecp->fec_imask = 0;
  489. /* Clear any pending interrupt
  490. */
  491. fecp->fec_ievent = 0xffc0;
  492. /* No need to set the IVEC register */
  493. /* Set station address
  494. */
  495. #define ea eth_get_dev()->enetaddr
  496. fecp->fec_addr_low = (ea[0] << 24) | (ea[1] << 16) | (ea[2] << 8) | (ea[3]);
  497. fecp->fec_addr_high = (ea[4] << 8) | (ea[5]);
  498. #undef ea
  499. #if (CONFIG_COMMANDS & CFG_CMD_CDP)
  500. /*
  501. * Turn on multicast address hash table
  502. */
  503. fecp->fec_hash_table_high = 0xffffffff;
  504. fecp->fec_hash_table_low = 0xffffffff;
  505. #else
  506. /* Clear multicast address hash table
  507. */
  508. fecp->fec_hash_table_high = 0;
  509. fecp->fec_hash_table_low = 0;
  510. #endif
  511. /* Set maximum receive buffer size.
  512. */
  513. fecp->fec_r_buff_size = PKT_MAXBLR_SIZE;
  514. /* Set maximum frame length
  515. */
  516. fecp->fec_r_hash = PKT_MAXBUF_SIZE;
  517. /*
  518. * Setup Buffers and Buffer Desriptors
  519. */
  520. rxIdx = 0;
  521. txIdx = 0;
  522. if (!rtx) {
  523. #ifdef CFG_ALLOC_DPRAM
  524. rtx = (RTXBD *) (immr->im_cpm.cp_dpmem +
  525. dpram_alloc_align (sizeof (RTXBD), 8));
  526. #else
  527. rtx = (RTXBD *) (immr->im_cpm.cp_dpmem + CPM_FEC_BASE);
  528. #endif
  529. }
  530. /*
  531. * Setup Receiver Buffer Descriptors (13.14.24.18)
  532. * Settings:
  533. * Empty, Wrap
  534. */
  535. for (i = 0; i < PKTBUFSRX; i++) {
  536. rtx->rxbd[i].cbd_sc = BD_ENET_RX_EMPTY;
  537. rtx->rxbd[i].cbd_datlen = 0; /* Reset */
  538. rtx->rxbd[i].cbd_bufaddr = (uint) NetRxPackets[i];
  539. }
  540. rtx->rxbd[PKTBUFSRX - 1].cbd_sc |= BD_ENET_RX_WRAP;
  541. /*
  542. * Setup Ethernet Transmitter Buffer Descriptors (13.14.24.19)
  543. * Settings:
  544. * Last, Tx CRC
  545. */
  546. for (i = 0; i < TX_BUF_CNT; i++) {
  547. rtx->txbd[i].cbd_sc = BD_ENET_TX_LAST | BD_ENET_TX_TC;
  548. rtx->txbd[i].cbd_datlen = 0; /* Reset */
  549. rtx->txbd[i].cbd_bufaddr = (uint) (&txbuf[0]);
  550. }
  551. rtx->txbd[TX_BUF_CNT - 1].cbd_sc |= BD_ENET_TX_WRAP;
  552. /* Set receive and transmit descriptor base
  553. */
  554. fecp->fec_r_des_start = (unsigned int) (&rtx->rxbd[0]);
  555. fecp->fec_x_des_start = (unsigned int) (&rtx->txbd[0]);
  556. /* Enable MII mode
  557. */
  558. #if 0 /* Full duplex mode */
  559. fecp->fec_r_cntrl = FEC_RCNTRL_MII_MODE;
  560. fecp->fec_x_cntrl = FEC_TCNTRL_FDEN;
  561. #else /* Half duplex mode */
  562. fecp->fec_r_cntrl = FEC_RCNTRL_MII_MODE | FEC_RCNTRL_DRT;
  563. fecp->fec_x_cntrl = 0;
  564. #endif
  565. /* Enable big endian and don't care about SDMA FC.
  566. */
  567. fecp->fec_fun_code = 0x78000000;
  568. /*
  569. * Setup the pin configuration of the FEC
  570. */
  571. fec_pin_init (efis->ether_index);
  572. rxIdx = 0;
  573. txIdx = 0;
  574. /*
  575. * Now enable the transmit and receive processing
  576. */
  577. fecp->fec_ecntrl = FEC_ECNTRL_PINMUX | FEC_ECNTRL_ETHER_EN;
  578. if (efis->phy_addr == -1) {
  579. #ifdef CFG_DISCOVER_PHY
  580. /*
  581. * wait for the PHY to wake up after reset
  582. */
  583. efis->actual_phy_addr = mii_discover_phy (dev);
  584. if (efis->actual_phy_addr == -1) {
  585. printf ("Unable to discover phy!\n");
  586. return 0;
  587. }
  588. #else
  589. efis->actual_phy_addr = -1;
  590. #endif
  591. } else {
  592. efis->actual_phy_addr = efis->phy_addr;
  593. }
  594. #if defined(CONFIG_MII) && defined(CONFIG_RMII)
  595. /* the MII interface is connected to FEC1
  596. * so for the miiphy_xxx function to work we must
  597. * call mii_init since fec_halt messes the thing up
  598. */
  599. if (efis->ether_index != 0)
  600. mii_init();
  601. /*
  602. * adapt the RMII speed to the speed of the phy
  603. */
  604. if (miiphy_speed (dev->name, efis->actual_phy_addr) == _100BASET) {
  605. fec_100Mbps (dev);
  606. } else {
  607. fec_10Mbps (dev);
  608. }
  609. #endif
  610. #if defined(CONFIG_MII)
  611. /*
  612. * adapt to the half/full speed settings
  613. */
  614. if (miiphy_duplex (dev->name, efis->actual_phy_addr) == FULL) {
  615. fec_full_duplex (dev);
  616. } else {
  617. fec_half_duplex (dev);
  618. }
  619. #endif
  620. /* And last, try to fill Rx Buffer Descriptors */
  621. fecp->fec_r_des_active = 0x01000000; /* Descriptor polling active */
  622. efis->initialized = 1;
  623. return 1;
  624. }
  625. static void fec_halt(struct eth_device* dev)
  626. {
  627. struct ether_fcc_info_s *efis = dev->priv;
  628. volatile fec_t *fecp = (volatile fec_t *)(CFG_IMMR + efis->fecp_offset);
  629. int i;
  630. /* avoid halt if initialized; mii gets stuck otherwise */
  631. if (!efis->initialized)
  632. return;
  633. /* Whack a reset.
  634. * A delay is required between a reset of the FEC block and
  635. * initialization of other FEC registers because the reset takes
  636. * some time to complete. If you don't delay, subsequent writes
  637. * to FEC registers might get killed by the reset routine which is
  638. * still in progress.
  639. */
  640. fecp->fec_ecntrl = FEC_ECNTRL_PINMUX | FEC_ECNTRL_RESET;
  641. for (i = 0;
  642. (fecp->fec_ecntrl & FEC_ECNTRL_RESET) && (i < FEC_RESET_DELAY);
  643. ++i) {
  644. udelay (1);
  645. }
  646. if (i == FEC_RESET_DELAY) {
  647. printf ("FEC_RESET_DELAY timeout\n");
  648. return;
  649. }
  650. efis->initialized = 0;
  651. }
  652. #if defined(CFG_DISCOVER_PHY) || defined(CONFIG_MII) || (CONFIG_COMMANDS & CFG_CMD_MII)
  653. /* Make MII read/write commands for the FEC.
  654. */
  655. #define mk_mii_read(ADDR, REG) (0x60020000 | ((ADDR << 23) | \
  656. (REG & 0x1f) << 18))
  657. #define mk_mii_write(ADDR, REG, VAL) (0x50020000 | ((ADDR << 23) | \
  658. (REG & 0x1f) << 18) | \
  659. (VAL & 0xffff))
  660. /* Interrupt events/masks.
  661. */
  662. #define FEC_ENET_HBERR ((uint)0x80000000) /* Heartbeat error */
  663. #define FEC_ENET_BABR ((uint)0x40000000) /* Babbling receiver */
  664. #define FEC_ENET_BABT ((uint)0x20000000) /* Babbling transmitter */
  665. #define FEC_ENET_GRA ((uint)0x10000000) /* Graceful stop complete */
  666. #define FEC_ENET_TXF ((uint)0x08000000) /* Full frame transmitted */
  667. #define FEC_ENET_TXB ((uint)0x04000000) /* A buffer was transmitted */
  668. #define FEC_ENET_RXF ((uint)0x02000000) /* Full frame received */
  669. #define FEC_ENET_RXB ((uint)0x01000000) /* A buffer was received */
  670. #define FEC_ENET_MII ((uint)0x00800000) /* MII interrupt */
  671. #define FEC_ENET_EBERR ((uint)0x00400000) /* SDMA bus error */
  672. /* PHY identification
  673. */
  674. #define PHY_ID_LXT970 0x78100000 /* LXT970 */
  675. #define PHY_ID_LXT971 0x001378e0 /* LXT971 and 972 */
  676. #define PHY_ID_82555 0x02a80150 /* Intel 82555 */
  677. #define PHY_ID_QS6612 0x01814400 /* QS6612 */
  678. #define PHY_ID_AMD79C784 0x00225610 /* AMD 79C784 */
  679. #define PHY_ID_LSI80225 0x0016f870 /* LSI 80225 */
  680. #define PHY_ID_LSI80225B 0x0016f880 /* LSI 80225/B */
  681. #define PHY_ID_DM9161 0x0181B880 /* Davicom DM9161 */
  682. #define PHY_ID_KSM8995M 0x00221450 /* MICREL KS8995MA */
  683. /* send command to phy using mii, wait for result */
  684. static uint
  685. mii_send(uint mii_cmd)
  686. {
  687. uint mii_reply;
  688. volatile fec_t *ep;
  689. int cnt;
  690. ep = &(((immap_t *)CFG_IMMR)->im_cpm.cp_fec);
  691. ep->fec_mii_data = mii_cmd; /* command to phy */
  692. /* wait for mii complete */
  693. cnt = 0;
  694. while (!(ep->fec_ievent & FEC_ENET_MII)) {
  695. if (++cnt > 1000) {
  696. printf("mii_send STUCK!\n");
  697. break;
  698. }
  699. }
  700. mii_reply = ep->fec_mii_data; /* result from phy */
  701. ep->fec_ievent = FEC_ENET_MII; /* clear MII complete */
  702. #if 0
  703. printf("%s[%d] %s: sent=0x%8.8x, reply=0x%8.8x\n",
  704. __FILE__,__LINE__,__FUNCTION__,mii_cmd,mii_reply);
  705. #endif
  706. return (mii_reply & 0xffff); /* data read from phy */
  707. }
  708. #endif /* CFG_DISCOVER_PHY || (CONFIG_COMMANDS & CFG_CMD_MII) */
  709. #if defined(CFG_DISCOVER_PHY)
  710. static int mii_discover_phy(struct eth_device *dev)
  711. {
  712. #define MAX_PHY_PASSES 11
  713. uint phyno;
  714. int pass;
  715. uint phytype;
  716. int phyaddr;
  717. phyaddr = -1; /* didn't find a PHY yet */
  718. for (pass = 1; pass <= MAX_PHY_PASSES && phyaddr < 0; ++pass) {
  719. if (pass > 1) {
  720. /* PHY may need more time to recover from reset.
  721. * The LXT970 needs 50ms typical, no maximum is
  722. * specified, so wait 10ms before try again.
  723. * With 11 passes this gives it 100ms to wake up.
  724. */
  725. udelay(10000); /* wait 10ms */
  726. }
  727. for (phyno = 0; phyno < 32 && phyaddr < 0; ++phyno) {
  728. phytype = mii_send(mk_mii_read(phyno, PHY_PHYIDR1));
  729. #ifdef ET_DEBUG
  730. printf("PHY type 0x%x pass %d type ", phytype, pass);
  731. #endif
  732. if (phytype != 0xffff) {
  733. phyaddr = phyno;
  734. phytype <<= 16;
  735. phytype |= mii_send(mk_mii_read(phyno,
  736. PHY_PHYIDR2));
  737. #ifdef ET_DEBUG
  738. printf("PHY @ 0x%x pass %d type ",phyno,pass);
  739. switch (phytype & 0xfffffff0) {
  740. case PHY_ID_LXT970:
  741. printf("LXT970\n");
  742. break;
  743. case PHY_ID_LXT971:
  744. printf("LXT971\n");
  745. break;
  746. case PHY_ID_82555:
  747. printf("82555\n");
  748. break;
  749. case PHY_ID_QS6612:
  750. printf("QS6612\n");
  751. break;
  752. case PHY_ID_AMD79C784:
  753. printf("AMD79C784\n");
  754. break;
  755. case PHY_ID_LSI80225B:
  756. printf("LSI L80225/B\n");
  757. break;
  758. case PHY_ID_DM9161:
  759. printf("Davicom DM9161\n");
  760. break;
  761. case PHY_ID_KSM8995M:
  762. printf("MICREL KS8995M\n");
  763. break;
  764. default:
  765. printf("0x%08x\n", phytype);
  766. break;
  767. }
  768. #endif
  769. }
  770. }
  771. }
  772. if (phyaddr < 0) {
  773. printf("No PHY device found.\n");
  774. }
  775. return phyaddr;
  776. }
  777. #endif /* CFG_DISCOVER_PHY */
  778. #if (defined(CONFIG_MII) || (CONFIG_COMMANDS & CFG_CMD_MII)) && !defined(CONFIG_BITBANGMII)
  779. /****************************************************************************
  780. * mii_init -- Initialize the MII for MII command without ethernet
  781. * This function is a subset of eth_init
  782. ****************************************************************************
  783. */
  784. void mii_init (void)
  785. {
  786. volatile immap_t *immr = (immap_t *) CFG_IMMR;
  787. volatile fec_t *fecp = &(immr->im_cpm.cp_fec);
  788. int i, j;
  789. for (j = 0; j < sizeof(ether_fcc_info) / sizeof(ether_fcc_info[0]); j++) {
  790. /* Whack a reset.
  791. * A delay is required between a reset of the FEC block and
  792. * initialization of other FEC registers because the reset takes
  793. * some time to complete. If you don't delay, subsequent writes
  794. * to FEC registers might get killed by the reset routine which is
  795. * still in progress.
  796. */
  797. fecp->fec_ecntrl = FEC_ECNTRL_PINMUX | FEC_ECNTRL_RESET;
  798. for (i = 0;
  799. (fecp->fec_ecntrl & FEC_ECNTRL_RESET) && (i < FEC_RESET_DELAY);
  800. ++i) {
  801. udelay (1);
  802. }
  803. if (i == FEC_RESET_DELAY) {
  804. printf ("FEC_RESET_DELAY timeout\n");
  805. return;
  806. }
  807. /* We use strictly polling mode only
  808. */
  809. fecp->fec_imask = 0;
  810. /* Clear any pending interrupt
  811. */
  812. fecp->fec_ievent = 0xffc0;
  813. /* Setup the pin configuration of the FEC(s)
  814. */
  815. fec_pin_init(ether_fcc_info[i].ether_index);
  816. /* Now enable the transmit and receive processing
  817. */
  818. fecp->fec_ecntrl = FEC_ECNTRL_PINMUX | FEC_ECNTRL_ETHER_EN;
  819. }
  820. }
  821. /*****************************************************************************
  822. * Read and write a MII PHY register, routines used by MII Utilities
  823. *
  824. * FIXME: These routines are expected to return 0 on success, but mii_send
  825. * does _not_ return an error code. Maybe 0xFFFF means error, i.e.
  826. * no PHY connected...
  827. * For now always return 0.
  828. * FIXME: These routines only work after calling eth_init() at least once!
  829. * Otherwise they hang in mii_send() !!! Sorry!
  830. *****************************************************************************/
  831. int fec8xx_miiphy_read(char *devname, unsigned char addr,
  832. unsigned char reg, unsigned short *value)
  833. {
  834. short rdreg; /* register working value */
  835. #ifdef MII_DEBUG
  836. printf ("miiphy_read(0x%x) @ 0x%x = ", reg, addr);
  837. #endif
  838. rdreg = mii_send(mk_mii_read(addr, reg));
  839. *value = rdreg;
  840. #ifdef MII_DEBUG
  841. printf ("0x%04x\n", *value);
  842. #endif
  843. return 0;
  844. }
  845. int fec8xx_miiphy_write(char *devname, unsigned char addr,
  846. unsigned char reg, unsigned short value)
  847. {
  848. short rdreg; /* register working value */
  849. #ifdef MII_DEBUG
  850. printf ("miiphy_write(0x%x) @ 0x%x = ", reg, addr);
  851. #endif
  852. rdreg = mii_send(mk_mii_write(addr, reg, value));
  853. #ifdef MII_DEBUG
  854. printf ("0x%04x\n", value);
  855. #endif
  856. return 0;
  857. }
  858. #endif /* (CONFIG_COMMANDS & CFG_CMD_MII) && !defined(CONFIG_BITBANGMII)*/
  859. #endif /* CFG_CMD_NET, FEC_ENET */