fec.c 26 KB

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