tsec.c 18 KB

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