tsec.c 18 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688
  1. /*
  2. * Freescale Three Speed Ethernet Controller driver
  3. *
  4. * This software may be used and distributed according to the
  5. * terms of the GNU Public License, Version 2, incorporated
  6. * herein by reference.
  7. *
  8. * Copyright 2004-2011, 2013 Freescale Semiconductor, Inc.
  9. * (C) Copyright 2003, Motorola, Inc.
  10. * author Andy Fleming
  11. *
  12. */
  13. #include <config.h>
  14. #include <common.h>
  15. #include <malloc.h>
  16. #include <net.h>
  17. #include <command.h>
  18. #include <tsec.h>
  19. #include <fsl_mdio.h>
  20. #include <asm/errno.h>
  21. #include <asm/processor.h>
  22. DECLARE_GLOBAL_DATA_PTR;
  23. #define TX_BUF_CNT 2
  24. static uint rx_idx; /* index of the current RX buffer */
  25. static uint tx_idx; /* index of the current TX buffer */
  26. #ifdef __GNUC__
  27. static struct txbd8 __iomem txbd[TX_BUF_CNT] __aligned(8);
  28. static struct rxbd8 __iomem rxbd[PKTBUFSRX] __aligned(8);
  29. #else
  30. #error "rtx must be 64-bit aligned"
  31. #endif
  32. static int tsec_send(struct eth_device *dev, void *packet, int length);
  33. /* Default initializations for TSEC controllers. */
  34. static struct tsec_info_struct tsec_info[] = {
  35. #ifdef CONFIG_TSEC1
  36. STD_TSEC_INFO(1), /* TSEC1 */
  37. #endif
  38. #ifdef CONFIG_TSEC2
  39. STD_TSEC_INFO(2), /* TSEC2 */
  40. #endif
  41. #ifdef CONFIG_MPC85XX_FEC
  42. {
  43. .regs = TSEC_GET_REGS(2, 0x2000),
  44. .devname = CONFIG_MPC85XX_FEC_NAME,
  45. .phyaddr = FEC_PHY_ADDR,
  46. .flags = FEC_FLAGS,
  47. .mii_devname = DEFAULT_MII_NAME
  48. }, /* FEC */
  49. #endif
  50. #ifdef CONFIG_TSEC3
  51. STD_TSEC_INFO(3), /* TSEC3 */
  52. #endif
  53. #ifdef CONFIG_TSEC4
  54. STD_TSEC_INFO(4), /* TSEC4 */
  55. #endif
  56. };
  57. #define TBIANA_SETTINGS ( \
  58. TBIANA_ASYMMETRIC_PAUSE \
  59. | TBIANA_SYMMETRIC_PAUSE \
  60. | TBIANA_FULL_DUPLEX \
  61. )
  62. /* By default force the TBI PHY into 1000Mbps full duplex when in SGMII mode */
  63. #ifndef CONFIG_TSEC_TBICR_SETTINGS
  64. #define CONFIG_TSEC_TBICR_SETTINGS ( \
  65. TBICR_PHY_RESET \
  66. | TBICR_ANEG_ENABLE \
  67. | TBICR_FULL_DUPLEX \
  68. | TBICR_SPEED1_SET \
  69. )
  70. #endif /* CONFIG_TSEC_TBICR_SETTINGS */
  71. /* Configure the TBI for SGMII operation */
  72. static void tsec_configure_serdes(struct tsec_private *priv)
  73. {
  74. /* Access TBI PHY registers at given TSEC register offset as opposed
  75. * to the register offset used for external PHY accesses */
  76. tsec_local_mdio_write(priv->phyregs_sgmii, in_be32(&priv->regs->tbipa),
  77. 0, TBI_ANA, TBIANA_SETTINGS);
  78. tsec_local_mdio_write(priv->phyregs_sgmii, in_be32(&priv->regs->tbipa),
  79. 0, TBI_TBICON, TBICON_CLK_SELECT);
  80. tsec_local_mdio_write(priv->phyregs_sgmii, in_be32(&priv->regs->tbipa),
  81. 0, TBI_CR, CONFIG_TSEC_TBICR_SETTINGS);
  82. }
  83. #ifdef CONFIG_MCAST_TFTP
  84. /* CREDITS: linux gianfar driver, slightly adjusted... thanx. */
  85. /* Set the appropriate hash bit for the given addr */
  86. /* The algorithm works like so:
  87. * 1) Take the Destination Address (ie the multicast address), and
  88. * do a CRC on it (little endian), and reverse the bits of the
  89. * result.
  90. * 2) Use the 8 most significant bits as a hash into a 256-entry
  91. * table. The table is controlled through 8 32-bit registers:
  92. * gaddr0-7. gaddr0's MSB is entry 0, and gaddr7's LSB is entry
  93. * 255. This means that the 3 most significant bits in the
  94. * hash index which gaddr register to use, and the 5 other bits
  95. * indicate which bit (assuming an IBM numbering scheme, which
  96. * for PowerPC (tm) is usually the case) in the register holds
  97. * the entry. */
  98. static int
  99. tsec_mcast_addr(struct eth_device *dev, const u8 *mcast_mac, u8 set)
  100. {
  101. struct tsec_private *priv = (struct tsec_private *)dev->priv;
  102. struct tsec __iomem *regs = priv->regs;
  103. u32 result, value;
  104. u8 whichbit, whichreg;
  105. result = ether_crc(MAC_ADDR_LEN, mcast_mac);
  106. whichbit = (result >> 24) & 0x1f; /* the 5 LSB = which bit to set */
  107. whichreg = result >> 29; /* the 3 MSB = which reg to set it in */
  108. value = 1 << (31-whichbit);
  109. if (set)
  110. setbits_be32(&regs->hash.gaddr0 + whichreg, value);
  111. else
  112. clrbits_be32(&regs->hash.gaddr0 + whichreg, value);
  113. return 0;
  114. }
  115. #endif /* Multicast TFTP ? */
  116. /* Initialized required registers to appropriate values, zeroing
  117. * those we don't care about (unless zero is bad, in which case,
  118. * choose a more appropriate value)
  119. */
  120. static void init_registers(struct tsec __iomem *regs)
  121. {
  122. /* Clear IEVENT */
  123. out_be32(&regs->ievent, IEVENT_INIT_CLEAR);
  124. out_be32(&regs->imask, IMASK_INIT_CLEAR);
  125. out_be32(&regs->hash.iaddr0, 0);
  126. out_be32(&regs->hash.iaddr1, 0);
  127. out_be32(&regs->hash.iaddr2, 0);
  128. out_be32(&regs->hash.iaddr3, 0);
  129. out_be32(&regs->hash.iaddr4, 0);
  130. out_be32(&regs->hash.iaddr5, 0);
  131. out_be32(&regs->hash.iaddr6, 0);
  132. out_be32(&regs->hash.iaddr7, 0);
  133. out_be32(&regs->hash.gaddr0, 0);
  134. out_be32(&regs->hash.gaddr1, 0);
  135. out_be32(&regs->hash.gaddr2, 0);
  136. out_be32(&regs->hash.gaddr3, 0);
  137. out_be32(&regs->hash.gaddr4, 0);
  138. out_be32(&regs->hash.gaddr5, 0);
  139. out_be32(&regs->hash.gaddr6, 0);
  140. out_be32(&regs->hash.gaddr7, 0);
  141. out_be32(&regs->rctrl, 0x00000000);
  142. /* Init RMON mib registers */
  143. memset((void *)&regs->rmon, 0, sizeof(regs->rmon));
  144. out_be32(&regs->rmon.cam1, 0xffffffff);
  145. out_be32(&regs->rmon.cam2, 0xffffffff);
  146. out_be32(&regs->mrblr, MRBLR_INIT_SETTINGS);
  147. out_be32(&regs->minflr, MINFLR_INIT_SETTINGS);
  148. out_be32(&regs->attr, ATTR_INIT_SETTINGS);
  149. out_be32(&regs->attreli, ATTRELI_INIT_SETTINGS);
  150. }
  151. /* Configure maccfg2 based on negotiated speed and duplex
  152. * reported by PHY handling code
  153. */
  154. static void adjust_link(struct tsec_private *priv, struct phy_device *phydev)
  155. {
  156. struct tsec __iomem *regs = priv->regs;
  157. u32 ecntrl, maccfg2;
  158. if (!phydev->link) {
  159. printf("%s: No link.\n", phydev->dev->name);
  160. return;
  161. }
  162. /* clear all bits relative with interface mode */
  163. ecntrl = in_be32(&regs->ecntrl);
  164. ecntrl &= ~ECNTRL_R100;
  165. maccfg2 = in_be32(&regs->maccfg2);
  166. maccfg2 &= ~(MACCFG2_IF | MACCFG2_FULL_DUPLEX);
  167. if (phydev->duplex)
  168. maccfg2 |= MACCFG2_FULL_DUPLEX;
  169. switch (phydev->speed) {
  170. case 1000:
  171. maccfg2 |= MACCFG2_GMII;
  172. break;
  173. case 100:
  174. case 10:
  175. maccfg2 |= MACCFG2_MII;
  176. /* Set R100 bit in all modes although
  177. * it is only used in RGMII mode
  178. */
  179. if (phydev->speed == 100)
  180. ecntrl |= ECNTRL_R100;
  181. break;
  182. default:
  183. printf("%s: Speed was bad\n", phydev->dev->name);
  184. break;
  185. }
  186. out_be32(&regs->ecntrl, ecntrl);
  187. out_be32(&regs->maccfg2, maccfg2);
  188. printf("Speed: %d, %s duplex%s\n", phydev->speed,
  189. (phydev->duplex) ? "full" : "half",
  190. (phydev->port == PORT_FIBRE) ? ", fiber mode" : "");
  191. }
  192. #ifdef CONFIG_SYS_FSL_ERRATUM_NMG_ETSEC129
  193. /*
  194. * When MACCFG1[Rx_EN] is enabled during system boot as part
  195. * of the eTSEC port initialization sequence,
  196. * the eTSEC Rx logic may not be properly initialized.
  197. */
  198. void redundant_init(struct eth_device *dev)
  199. {
  200. struct tsec_private *priv = dev->priv;
  201. struct tsec __iomem *regs = priv->regs;
  202. uint t, count = 0;
  203. int fail = 1;
  204. static const u8 pkt[] = {
  205. 0x00, 0x1e, 0x4f, 0x12, 0xcb, 0x2c, 0x00, 0x25,
  206. 0x64, 0xbb, 0xd1, 0xab, 0x08, 0x00, 0x45, 0x00,
  207. 0x00, 0x5c, 0xdd, 0x22, 0x00, 0x00, 0x80, 0x01,
  208. 0x1f, 0x71, 0x0a, 0xc1, 0x14, 0x22, 0x0a, 0xc1,
  209. 0x14, 0x6a, 0x08, 0x00, 0xef, 0x7e, 0x02, 0x00,
  210. 0x94, 0x05, 0x61, 0x62, 0x63, 0x64, 0x65, 0x66,
  211. 0x67, 0x68, 0x69, 0x6a, 0x6b, 0x6c, 0x6d, 0x6e,
  212. 0x6f, 0x70, 0x71, 0x72, 0x73, 0x74, 0x75, 0x76,
  213. 0x77, 0x61, 0x62, 0x63, 0x64, 0x65, 0x66, 0x67,
  214. 0x68, 0x69, 0x6a, 0x6b, 0x6c, 0x6d, 0x6e, 0x6f,
  215. 0x70, 0x71, 0x72, 0x73, 0x74, 0x75, 0x76, 0x77,
  216. 0x61, 0x62, 0x63, 0x64, 0x65, 0x66, 0x67, 0x68,
  217. 0x69, 0x6a, 0x6b, 0x6c, 0x6d, 0x6e, 0x6f, 0x70,
  218. 0x71, 0x72};
  219. /* Enable promiscuous mode */
  220. setbits_be32(&regs->rctrl, 0x8);
  221. /* Enable loopback mode */
  222. setbits_be32(&regs->maccfg1, MACCFG1_LOOPBACK);
  223. /* Enable transmit and receive */
  224. setbits_be32(&regs->maccfg1, MACCFG1_RX_EN | MACCFG1_TX_EN);
  225. /* Tell the DMA it is clear to go */
  226. setbits_be32(&regs->dmactrl, DMACTRL_INIT_SETTINGS);
  227. out_be32(&regs->tstat, TSTAT_CLEAR_THALT);
  228. out_be32(&regs->rstat, RSTAT_CLEAR_RHALT);
  229. clrbits_be32(&regs->dmactrl, DMACTRL_GRS | DMACTRL_GTS);
  230. do {
  231. uint16_t status;
  232. tsec_send(dev, (void *)pkt, sizeof(pkt));
  233. /* Wait for buffer to be received */
  234. for (t = 0; in_be16(&rxbd[rx_idx].status) & RXBD_EMPTY; t++) {
  235. if (t >= 10 * TOUT_LOOP) {
  236. printf("%s: tsec: rx error\n", dev->name);
  237. break;
  238. }
  239. }
  240. if (!memcmp(pkt, (void *)NetRxPackets[rx_idx], sizeof(pkt)))
  241. fail = 0;
  242. out_be16(&rxbd[rx_idx].length, 0);
  243. status = RXBD_EMPTY;
  244. if ((rx_idx + 1) == PKTBUFSRX)
  245. status |= RXBD_WRAP;
  246. out_be16(&rxbd[rx_idx].status, status);
  247. rx_idx = (rx_idx + 1) % PKTBUFSRX;
  248. if (in_be32(&regs->ievent) & IEVENT_BSY) {
  249. out_be32(&regs->ievent, IEVENT_BSY);
  250. out_be32(&regs->rstat, RSTAT_CLEAR_RHALT);
  251. }
  252. if (fail) {
  253. printf("loopback recv packet error!\n");
  254. clrbits_be32(&regs->maccfg1, MACCFG1_RX_EN);
  255. udelay(1000);
  256. setbits_be32(&regs->maccfg1, MACCFG1_RX_EN);
  257. }
  258. } while ((count++ < 4) && (fail == 1));
  259. if (fail)
  260. panic("eTSEC init fail!\n");
  261. /* Disable promiscuous mode */
  262. clrbits_be32(&regs->rctrl, 0x8);
  263. /* Disable loopback mode */
  264. clrbits_be32(&regs->maccfg1, MACCFG1_LOOPBACK);
  265. }
  266. #endif
  267. /* Set up the buffers and their descriptors, and bring up the
  268. * interface
  269. */
  270. static void startup_tsec(struct eth_device *dev)
  271. {
  272. struct tsec_private *priv = (struct tsec_private *)dev->priv;
  273. struct tsec __iomem *regs = priv->regs;
  274. uint16_t status;
  275. int i;
  276. /* reset the indices to zero */
  277. rx_idx = 0;
  278. tx_idx = 0;
  279. #ifdef CONFIG_SYS_FSL_ERRATUM_NMG_ETSEC129
  280. uint svr;
  281. #endif
  282. /* Point to the buffer descriptors */
  283. out_be32(&regs->tbase, (u32)&txbd[0]);
  284. out_be32(&regs->rbase, (u32)&rxbd[0]);
  285. /* Initialize the Rx Buffer descriptors */
  286. for (i = 0; i < PKTBUFSRX; i++) {
  287. out_be16(&rxbd[i].status, RXBD_EMPTY);
  288. out_be16(&rxbd[i].length, 0);
  289. out_be32(&rxbd[i].bufptr, (u32)NetRxPackets[i]);
  290. }
  291. status = in_be16(&rxbd[PKTBUFSRX - 1].status);
  292. out_be16(&rxbd[PKTBUFSRX - 1].status, status | RXBD_WRAP);
  293. /* Initialize the TX Buffer Descriptors */
  294. for (i = 0; i < TX_BUF_CNT; i++) {
  295. out_be16(&txbd[i].status, 0);
  296. out_be16(&txbd[i].length, 0);
  297. out_be32(&txbd[i].bufptr, 0);
  298. }
  299. status = in_be16(&txbd[TX_BUF_CNT - 1].status);
  300. out_be16(&txbd[TX_BUF_CNT - 1].status, status | TXBD_WRAP);
  301. #ifdef CONFIG_SYS_FSL_ERRATUM_NMG_ETSEC129
  302. svr = get_svr();
  303. if ((SVR_MAJ(svr) == 1) || IS_SVR_REV(svr, 2, 0))
  304. redundant_init(dev);
  305. #endif
  306. /* Enable Transmit and Receive */
  307. setbits_be32(&regs->maccfg1, MACCFG1_RX_EN | MACCFG1_TX_EN);
  308. /* Tell the DMA it is clear to go */
  309. setbits_be32(&regs->dmactrl, DMACTRL_INIT_SETTINGS);
  310. out_be32(&regs->tstat, TSTAT_CLEAR_THALT);
  311. out_be32(&regs->rstat, RSTAT_CLEAR_RHALT);
  312. clrbits_be32(&regs->dmactrl, DMACTRL_GRS | DMACTRL_GTS);
  313. }
  314. /* This returns the status bits of the device. The return value
  315. * is never checked, and this is what the 8260 driver did, so we
  316. * do the same. Presumably, this would be zero if there were no
  317. * errors
  318. */
  319. static int tsec_send(struct eth_device *dev, void *packet, int length)
  320. {
  321. struct tsec_private *priv = (struct tsec_private *)dev->priv;
  322. struct tsec __iomem *regs = priv->regs;
  323. uint16_t status;
  324. int result = 0;
  325. int i;
  326. /* Find an empty buffer descriptor */
  327. for (i = 0; in_be16(&txbd[tx_idx].status) & TXBD_READY; i++) {
  328. if (i >= TOUT_LOOP) {
  329. debug("%s: tsec: tx buffers full\n", dev->name);
  330. return result;
  331. }
  332. }
  333. out_be32(&txbd[tx_idx].bufptr, (u32)packet);
  334. out_be16(&txbd[tx_idx].length, length);
  335. status = in_be16(&txbd[tx_idx].status);
  336. out_be16(&txbd[tx_idx].status, status |
  337. (TXBD_READY | TXBD_LAST | TXBD_CRC | TXBD_INTERRUPT));
  338. /* Tell the DMA to go */
  339. out_be32(&regs->tstat, TSTAT_CLEAR_THALT);
  340. /* Wait for buffer to be transmitted */
  341. for (i = 0; in_be16(&txbd[tx_idx].status) & TXBD_READY; i++) {
  342. if (i >= TOUT_LOOP) {
  343. debug("%s: tsec: tx error\n", dev->name);
  344. return result;
  345. }
  346. }
  347. tx_idx = (tx_idx + 1) % TX_BUF_CNT;
  348. result = in_be16(&txbd[tx_idx].status) & TXBD_STATS;
  349. return result;
  350. }
  351. static int tsec_recv(struct eth_device *dev)
  352. {
  353. struct tsec_private *priv = (struct tsec_private *)dev->priv;
  354. struct tsec __iomem *regs = priv->regs;
  355. while (!(in_be16(&rxbd[rx_idx].status) & RXBD_EMPTY)) {
  356. int length = in_be16(&rxbd[rx_idx].length);
  357. uint16_t status = in_be16(&rxbd[rx_idx].status);
  358. /* Send the packet up if there were no errors */
  359. if (!(status & RXBD_STATS))
  360. NetReceive(NetRxPackets[rx_idx], length - 4);
  361. else
  362. printf("Got error %x\n", (status & RXBD_STATS));
  363. out_be16(&rxbd[rx_idx].length, 0);
  364. status = RXBD_EMPTY;
  365. /* Set the wrap bit if this is the last element in the list */
  366. if ((rx_idx + 1) == PKTBUFSRX)
  367. status |= RXBD_WRAP;
  368. out_be16(&rxbd[rx_idx].status, status);
  369. rx_idx = (rx_idx + 1) % PKTBUFSRX;
  370. }
  371. if (in_be32(&regs->ievent) & IEVENT_BSY) {
  372. out_be32(&regs->ievent, IEVENT_BSY);
  373. out_be32(&regs->rstat, RSTAT_CLEAR_RHALT);
  374. }
  375. return -1;
  376. }
  377. /* Stop the interface */
  378. static void tsec_halt(struct eth_device *dev)
  379. {
  380. struct tsec_private *priv = (struct tsec_private *)dev->priv;
  381. struct tsec __iomem *regs = priv->regs;
  382. clrbits_be32(&regs->dmactrl, DMACTRL_GRS | DMACTRL_GTS);
  383. setbits_be32(&regs->dmactrl, DMACTRL_GRS | DMACTRL_GTS);
  384. while ((in_be32(&regs->ievent) & (IEVENT_GRSC | IEVENT_GTSC))
  385. != (IEVENT_GRSC | IEVENT_GTSC))
  386. ;
  387. clrbits_be32(&regs->maccfg1, MACCFG1_TX_EN | MACCFG1_RX_EN);
  388. /* Shut down the PHY, as needed */
  389. phy_shutdown(priv->phydev);
  390. }
  391. /* Initializes data structures and registers for the controller,
  392. * and brings the interface up. Returns the link status, meaning
  393. * that it returns success if the link is up, failure otherwise.
  394. * This allows u-boot to find the first active controller.
  395. */
  396. static int tsec_init(struct eth_device *dev, bd_t * bd)
  397. {
  398. struct tsec_private *priv = (struct tsec_private *)dev->priv;
  399. struct tsec __iomem *regs = priv->regs;
  400. u32 tempval;
  401. int ret;
  402. /* Make sure the controller is stopped */
  403. tsec_halt(dev);
  404. /* Init MACCFG2. Defaults to GMII */
  405. out_be32(&regs->maccfg2, MACCFG2_INIT_SETTINGS);
  406. /* Init ECNTRL */
  407. out_be32(&regs->ecntrl, ECNTRL_INIT_SETTINGS);
  408. /* Copy the station address into the address registers.
  409. * For a station address of 0x12345678ABCD in transmission
  410. * order (BE), MACnADDR1 is set to 0xCDAB7856 and
  411. * MACnADDR2 is set to 0x34120000.
  412. */
  413. tempval = (dev->enetaddr[5] << 24) | (dev->enetaddr[4] << 16) |
  414. (dev->enetaddr[3] << 8) | dev->enetaddr[2];
  415. out_be32(&regs->macstnaddr1, tempval);
  416. tempval = (dev->enetaddr[1] << 24) | (dev->enetaddr[0] << 16);
  417. out_be32(&regs->macstnaddr2, tempval);
  418. /* Clear out (for the most part) the other registers */
  419. init_registers(regs);
  420. /* Ready the device for tx/rx */
  421. startup_tsec(dev);
  422. /* Start up the PHY */
  423. ret = phy_startup(priv->phydev);
  424. if (ret) {
  425. printf("Could not initialize PHY %s\n",
  426. priv->phydev->dev->name);
  427. return ret;
  428. }
  429. adjust_link(priv, priv->phydev);
  430. /* If there's no link, fail */
  431. return priv->phydev->link ? 0 : -1;
  432. }
  433. static phy_interface_t tsec_get_interface(struct tsec_private *priv)
  434. {
  435. struct tsec __iomem *regs = priv->regs;
  436. u32 ecntrl;
  437. ecntrl = in_be32(&regs->ecntrl);
  438. if (ecntrl & ECNTRL_SGMII_MODE)
  439. return PHY_INTERFACE_MODE_SGMII;
  440. if (ecntrl & ECNTRL_TBI_MODE) {
  441. if (ecntrl & ECNTRL_REDUCED_MODE)
  442. return PHY_INTERFACE_MODE_RTBI;
  443. else
  444. return PHY_INTERFACE_MODE_TBI;
  445. }
  446. if (ecntrl & ECNTRL_REDUCED_MODE) {
  447. if (ecntrl & ECNTRL_REDUCED_MII_MODE)
  448. return PHY_INTERFACE_MODE_RMII;
  449. else {
  450. phy_interface_t interface = priv->interface;
  451. /*
  452. * This isn't autodetected, so it must
  453. * be set by the platform code.
  454. */
  455. if ((interface == PHY_INTERFACE_MODE_RGMII_ID) ||
  456. (interface == PHY_INTERFACE_MODE_RGMII_TXID) ||
  457. (interface == PHY_INTERFACE_MODE_RGMII_RXID))
  458. return interface;
  459. return PHY_INTERFACE_MODE_RGMII;
  460. }
  461. }
  462. if (priv->flags & TSEC_GIGABIT)
  463. return PHY_INTERFACE_MODE_GMII;
  464. return PHY_INTERFACE_MODE_MII;
  465. }
  466. /* Discover which PHY is attached to the device, and configure it
  467. * properly. If the PHY is not recognized, then return 0
  468. * (failure). Otherwise, return 1
  469. */
  470. static int init_phy(struct eth_device *dev)
  471. {
  472. struct tsec_private *priv = (struct tsec_private *)dev->priv;
  473. struct phy_device *phydev;
  474. struct tsec __iomem *regs = priv->regs;
  475. u32 supported = (SUPPORTED_10baseT_Half |
  476. SUPPORTED_10baseT_Full |
  477. SUPPORTED_100baseT_Half |
  478. SUPPORTED_100baseT_Full);
  479. if (priv->flags & TSEC_GIGABIT)
  480. supported |= SUPPORTED_1000baseT_Full;
  481. /* Assign a Physical address to the TBI */
  482. out_be32(&regs->tbipa, CONFIG_SYS_TBIPA_VALUE);
  483. priv->interface = tsec_get_interface(priv);
  484. if (priv->interface == PHY_INTERFACE_MODE_SGMII)
  485. tsec_configure_serdes(priv);
  486. phydev = phy_connect(priv->bus, priv->phyaddr, dev, priv->interface);
  487. phydev->supported &= supported;
  488. phydev->advertising = phydev->supported;
  489. priv->phydev = phydev;
  490. phy_config(phydev);
  491. return 1;
  492. }
  493. /* Initialize device structure. Returns success if PHY
  494. * initialization succeeded (i.e. if it recognizes the PHY)
  495. */
  496. static int tsec_initialize(bd_t *bis, struct tsec_info_struct *tsec_info)
  497. {
  498. struct eth_device *dev;
  499. int i;
  500. struct tsec_private *priv;
  501. dev = (struct eth_device *)malloc(sizeof *dev);
  502. if (NULL == dev)
  503. return 0;
  504. memset(dev, 0, sizeof *dev);
  505. priv = (struct tsec_private *)malloc(sizeof(*priv));
  506. if (NULL == priv)
  507. return 0;
  508. priv->regs = tsec_info->regs;
  509. priv->phyregs_sgmii = tsec_info->miiregs_sgmii;
  510. priv->phyaddr = tsec_info->phyaddr;
  511. priv->flags = tsec_info->flags;
  512. sprintf(dev->name, tsec_info->devname);
  513. priv->interface = tsec_info->interface;
  514. priv->bus = miiphy_get_dev_by_name(tsec_info->mii_devname);
  515. dev->iobase = 0;
  516. dev->priv = priv;
  517. dev->init = tsec_init;
  518. dev->halt = tsec_halt;
  519. dev->send = tsec_send;
  520. dev->recv = tsec_recv;
  521. #ifdef CONFIG_MCAST_TFTP
  522. dev->mcast = tsec_mcast_addr;
  523. #endif
  524. /* Tell u-boot to get the addr from the env */
  525. for (i = 0; i < 6; i++)
  526. dev->enetaddr[i] = 0;
  527. eth_register(dev);
  528. /* Reset the MAC */
  529. setbits_be32(&priv->regs->maccfg1, MACCFG1_SOFT_RESET);
  530. udelay(2); /* Soft Reset must be asserted for 3 TX clocks */
  531. clrbits_be32(&priv->regs->maccfg1, MACCFG1_SOFT_RESET);
  532. /* Try to initialize PHY here, and return */
  533. return init_phy(dev);
  534. }
  535. /*
  536. * Initialize all the TSEC devices
  537. *
  538. * Returns the number of TSEC devices that were initialized
  539. */
  540. int tsec_eth_init(bd_t *bis, struct tsec_info_struct *tsecs, int num)
  541. {
  542. int i;
  543. int ret, count = 0;
  544. for (i = 0; i < num; i++) {
  545. ret = tsec_initialize(bis, &tsecs[i]);
  546. if (ret > 0)
  547. count += ret;
  548. }
  549. return count;
  550. }
  551. int tsec_standard_init(bd_t *bis)
  552. {
  553. struct fsl_pq_mdio_info info;
  554. info.regs = TSEC_GET_MDIO_REGS_BASE(1);
  555. info.name = DEFAULT_MII_NAME;
  556. fsl_pq_mdio_init(bis, &info);
  557. return tsec_eth_init(bis, tsec_info, ARRAY_SIZE(tsec_info));
  558. }