tsec.c 18 KB

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