tsec.c 23 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886
  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 <dm.h>
  13. #include <malloc.h>
  14. #include <net.h>
  15. #include <command.h>
  16. #include <tsec.h>
  17. #include <fsl_mdio.h>
  18. #include <linux/errno.h>
  19. #include <asm/processor.h>
  20. #include <asm/io.h>
  21. #ifndef CONFIG_DM_ETH
  22. /* Default initializations for TSEC controllers. */
  23. static struct tsec_info_struct tsec_info[] = {
  24. #ifdef CONFIG_TSEC1
  25. STD_TSEC_INFO(1), /* TSEC1 */
  26. #endif
  27. #ifdef CONFIG_TSEC2
  28. STD_TSEC_INFO(2), /* TSEC2 */
  29. #endif
  30. #ifdef CONFIG_MPC85XX_FEC
  31. {
  32. .regs = TSEC_GET_REGS(2, 0x2000),
  33. .devname = CONFIG_MPC85XX_FEC_NAME,
  34. .phyaddr = FEC_PHY_ADDR,
  35. .flags = FEC_FLAGS,
  36. .mii_devname = DEFAULT_MII_NAME
  37. }, /* FEC */
  38. #endif
  39. #ifdef CONFIG_TSEC3
  40. STD_TSEC_INFO(3), /* TSEC3 */
  41. #endif
  42. #ifdef CONFIG_TSEC4
  43. STD_TSEC_INFO(4), /* TSEC4 */
  44. #endif
  45. };
  46. #endif /* CONFIG_DM_ETH */
  47. #define TBIANA_SETTINGS ( \
  48. TBIANA_ASYMMETRIC_PAUSE \
  49. | TBIANA_SYMMETRIC_PAUSE \
  50. | TBIANA_FULL_DUPLEX \
  51. )
  52. /* By default force the TBI PHY into 1000Mbps full duplex when in SGMII mode */
  53. #ifndef CONFIG_TSEC_TBICR_SETTINGS
  54. #define CONFIG_TSEC_TBICR_SETTINGS ( \
  55. TBICR_PHY_RESET \
  56. | TBICR_ANEG_ENABLE \
  57. | TBICR_FULL_DUPLEX \
  58. | TBICR_SPEED1_SET \
  59. )
  60. #endif /* CONFIG_TSEC_TBICR_SETTINGS */
  61. /* Configure the TBI for SGMII operation */
  62. static void tsec_configure_serdes(struct tsec_private *priv)
  63. {
  64. /*
  65. * Access TBI PHY registers at given TSEC register offset as opposed
  66. * to the register offset used for external PHY accesses
  67. */
  68. tsec_local_mdio_write(priv->phyregs_sgmii, in_be32(&priv->regs->tbipa),
  69. 0, TBI_ANA, TBIANA_SETTINGS);
  70. tsec_local_mdio_write(priv->phyregs_sgmii, in_be32(&priv->regs->tbipa),
  71. 0, TBI_TBICON, TBICON_CLK_SELECT);
  72. tsec_local_mdio_write(priv->phyregs_sgmii, in_be32(&priv->regs->tbipa),
  73. 0, TBI_CR, CONFIG_TSEC_TBICR_SETTINGS);
  74. }
  75. #ifdef CONFIG_MCAST_TFTP
  76. /* CREDITS: linux gianfar driver, slightly adjusted... thanx. */
  77. /* Set the appropriate hash bit for the given addr */
  78. /*
  79. * The algorithm works like so:
  80. * 1) Take the Destination Address (ie the multicast address), and
  81. * do a CRC on it (little endian), and reverse the bits of the
  82. * result.
  83. * 2) Use the 8 most significant bits as a hash into a 256-entry
  84. * table. The table is controlled through 8 32-bit registers:
  85. * gaddr0-7. gaddr0's MSB is entry 0, and gaddr7's LSB is entry
  86. * 255. This means that the 3 most significant bits in the
  87. * hash index which gaddr register to use, and the 5 other bits
  88. * indicate which bit (assuming an IBM numbering scheme, which
  89. * for PowerPC (tm) is usually the case) in the register holds
  90. * the entry.
  91. */
  92. #ifndef CONFIG_DM_ETH
  93. static int tsec_mcast_addr(struct eth_device *dev, const u8 *mcast_mac, u8 set)
  94. #else
  95. static int tsec_mcast_addr(struct udevice *dev, const u8 *mcast_mac, int set)
  96. #endif
  97. {
  98. struct tsec_private *priv = (struct tsec_private *)dev->priv;
  99. struct tsec __iomem *regs = priv->regs;
  100. u32 result, value;
  101. u8 whichbit, whichreg;
  102. result = ether_crc(MAC_ADDR_LEN, mcast_mac);
  103. whichbit = (result >> 24) & 0x1f; /* the 5 LSB = which bit to set */
  104. whichreg = result >> 29; /* the 3 MSB = which reg to set it in */
  105. value = BIT(31 - whichbit);
  106. if (set)
  107. setbits_be32(&regs->hash.gaddr0 + whichreg, value);
  108. else
  109. clrbits_be32(&regs->hash.gaddr0 + whichreg, value);
  110. return 0;
  111. }
  112. #endif /* Multicast TFTP ? */
  113. /*
  114. * Initialized required registers to appropriate values, zeroing
  115. * those we don't care about (unless zero is bad, in which case,
  116. * choose a more appropriate value)
  117. */
  118. static void init_registers(struct tsec __iomem *regs)
  119. {
  120. /* Clear IEVENT */
  121. out_be32(&regs->ievent, IEVENT_INIT_CLEAR);
  122. out_be32(&regs->imask, IMASK_INIT_CLEAR);
  123. out_be32(&regs->hash.iaddr0, 0);
  124. out_be32(&regs->hash.iaddr1, 0);
  125. out_be32(&regs->hash.iaddr2, 0);
  126. out_be32(&regs->hash.iaddr3, 0);
  127. out_be32(&regs->hash.iaddr4, 0);
  128. out_be32(&regs->hash.iaddr5, 0);
  129. out_be32(&regs->hash.iaddr6, 0);
  130. out_be32(&regs->hash.iaddr7, 0);
  131. out_be32(&regs->hash.gaddr0, 0);
  132. out_be32(&regs->hash.gaddr1, 0);
  133. out_be32(&regs->hash.gaddr2, 0);
  134. out_be32(&regs->hash.gaddr3, 0);
  135. out_be32(&regs->hash.gaddr4, 0);
  136. out_be32(&regs->hash.gaddr5, 0);
  137. out_be32(&regs->hash.gaddr6, 0);
  138. out_be32(&regs->hash.gaddr7, 0);
  139. out_be32(&regs->rctrl, 0x00000000);
  140. /* Init RMON mib registers */
  141. memset((void *)&regs->rmon, 0, sizeof(regs->rmon));
  142. out_be32(&regs->rmon.cam1, 0xffffffff);
  143. out_be32(&regs->rmon.cam2, 0xffffffff);
  144. out_be32(&regs->mrblr, MRBLR_INIT_SETTINGS);
  145. out_be32(&regs->minflr, MINFLR_INIT_SETTINGS);
  146. out_be32(&regs->attr, ATTR_INIT_SETTINGS);
  147. out_be32(&regs->attreli, ATTRELI_INIT_SETTINGS);
  148. }
  149. /*
  150. * Configure maccfg2 based on negotiated speed and duplex
  151. * reported by PHY handling code
  152. */
  153. static void adjust_link(struct tsec_private *priv, struct phy_device *phydev)
  154. {
  155. struct tsec __iomem *regs = priv->regs;
  156. u32 ecntrl, maccfg2;
  157. if (!phydev->link) {
  158. printf("%s: No link.\n", phydev->dev->name);
  159. return;
  160. }
  161. /* clear all bits relative with interface mode */
  162. ecntrl = in_be32(&regs->ecntrl);
  163. ecntrl &= ~ECNTRL_R100;
  164. maccfg2 = in_be32(&regs->maccfg2);
  165. maccfg2 &= ~(MACCFG2_IF | MACCFG2_FULL_DUPLEX);
  166. if (phydev->duplex)
  167. maccfg2 |= MACCFG2_FULL_DUPLEX;
  168. switch (phydev->speed) {
  169. case 1000:
  170. maccfg2 |= MACCFG2_GMII;
  171. break;
  172. case 100:
  173. case 10:
  174. maccfg2 |= MACCFG2_MII;
  175. /*
  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. /*
  193. * This returns the status bits of the device. The return value
  194. * is never checked, and this is what the 8260 driver did, so we
  195. * do the same. Presumably, this would be zero if there were no
  196. * errors
  197. */
  198. #ifndef CONFIG_DM_ETH
  199. static int tsec_send(struct eth_device *dev, void *packet, int length)
  200. #else
  201. static int tsec_send(struct udevice *dev, void *packet, int length)
  202. #endif
  203. {
  204. struct tsec_private *priv = (struct tsec_private *)dev->priv;
  205. struct tsec __iomem *regs = priv->regs;
  206. u16 status;
  207. int result = 0;
  208. int i;
  209. /* Find an empty buffer descriptor */
  210. for (i = 0;
  211. in_be16(&priv->txbd[priv->tx_idx].status) & TXBD_READY;
  212. i++) {
  213. if (i >= TOUT_LOOP) {
  214. debug("%s: tsec: tx buffers full\n", dev->name);
  215. return result;
  216. }
  217. }
  218. out_be32(&priv->txbd[priv->tx_idx].bufptr, (u32)packet);
  219. out_be16(&priv->txbd[priv->tx_idx].length, length);
  220. status = in_be16(&priv->txbd[priv->tx_idx].status);
  221. out_be16(&priv->txbd[priv->tx_idx].status, status |
  222. (TXBD_READY | TXBD_LAST | TXBD_CRC | TXBD_INTERRUPT));
  223. /* Tell the DMA to go */
  224. out_be32(&regs->tstat, TSTAT_CLEAR_THALT);
  225. /* Wait for buffer to be transmitted */
  226. for (i = 0;
  227. in_be16(&priv->txbd[priv->tx_idx].status) & TXBD_READY;
  228. i++) {
  229. if (i >= TOUT_LOOP) {
  230. debug("%s: tsec: tx error\n", dev->name);
  231. return result;
  232. }
  233. }
  234. priv->tx_idx = (priv->tx_idx + 1) % TX_BUF_CNT;
  235. result = in_be16(&priv->txbd[priv->tx_idx].status) & TXBD_STATS;
  236. return result;
  237. }
  238. #ifndef CONFIG_DM_ETH
  239. static int tsec_recv(struct eth_device *dev)
  240. {
  241. struct tsec_private *priv = (struct tsec_private *)dev->priv;
  242. struct tsec __iomem *regs = priv->regs;
  243. while (!(in_be16(&priv->rxbd[priv->rx_idx].status) & RXBD_EMPTY)) {
  244. int length = in_be16(&priv->rxbd[priv->rx_idx].length);
  245. u16 status = in_be16(&priv->rxbd[priv->rx_idx].status);
  246. uchar *packet = net_rx_packets[priv->rx_idx];
  247. /* Send the packet up if there were no errors */
  248. if (!(status & RXBD_STATS))
  249. net_process_received_packet(packet, length - 4);
  250. else
  251. printf("Got error %x\n", (status & RXBD_STATS));
  252. out_be16(&priv->rxbd[priv->rx_idx].length, 0);
  253. status = RXBD_EMPTY;
  254. /* Set the wrap bit if this is the last element in the list */
  255. if ((priv->rx_idx + 1) == PKTBUFSRX)
  256. status |= RXBD_WRAP;
  257. out_be16(&priv->rxbd[priv->rx_idx].status, status);
  258. priv->rx_idx = (priv->rx_idx + 1) % PKTBUFSRX;
  259. }
  260. if (in_be32(&regs->ievent) & IEVENT_BSY) {
  261. out_be32(&regs->ievent, IEVENT_BSY);
  262. out_be32(&regs->rstat, RSTAT_CLEAR_RHALT);
  263. }
  264. return -1;
  265. }
  266. #else
  267. static int tsec_recv(struct udevice *dev, int flags, uchar **packetp)
  268. {
  269. struct tsec_private *priv = (struct tsec_private *)dev->priv;
  270. struct tsec __iomem *regs = priv->regs;
  271. int ret = -1;
  272. if (!(in_be16(&priv->rxbd[priv->rx_idx].status) & RXBD_EMPTY)) {
  273. int length = in_be16(&priv->rxbd[priv->rx_idx].length);
  274. u16 status = in_be16(&priv->rxbd[priv->rx_idx].status);
  275. u32 buf;
  276. /* Send the packet up if there were no errors */
  277. if (!(status & RXBD_STATS)) {
  278. buf = in_be32(&priv->rxbd[priv->rx_idx].bufptr);
  279. *packetp = (uchar *)buf;
  280. ret = length - 4;
  281. } else {
  282. printf("Got error %x\n", (status & RXBD_STATS));
  283. }
  284. }
  285. if (in_be32(&regs->ievent) & IEVENT_BSY) {
  286. out_be32(&regs->ievent, IEVENT_BSY);
  287. out_be32(&regs->rstat, RSTAT_CLEAR_RHALT);
  288. }
  289. return ret;
  290. }
  291. static int tsec_free_pkt(struct udevice *dev, uchar *packet, int length)
  292. {
  293. struct tsec_private *priv = (struct tsec_private *)dev->priv;
  294. u16 status;
  295. out_be16(&priv->rxbd[priv->rx_idx].length, 0);
  296. status = RXBD_EMPTY;
  297. /* Set the wrap bit if this is the last element in the list */
  298. if ((priv->rx_idx + 1) == PKTBUFSRX)
  299. status |= RXBD_WRAP;
  300. out_be16(&priv->rxbd[priv->rx_idx].status, status);
  301. priv->rx_idx = (priv->rx_idx + 1) % PKTBUFSRX;
  302. return 0;
  303. }
  304. #endif
  305. /* Stop the interface */
  306. #ifndef CONFIG_DM_ETH
  307. static void tsec_halt(struct eth_device *dev)
  308. #else
  309. static void tsec_halt(struct udevice *dev)
  310. #endif
  311. {
  312. struct tsec_private *priv = (struct tsec_private *)dev->priv;
  313. struct tsec __iomem *regs = priv->regs;
  314. clrbits_be32(&regs->dmactrl, DMACTRL_GRS | DMACTRL_GTS);
  315. setbits_be32(&regs->dmactrl, DMACTRL_GRS | DMACTRL_GTS);
  316. while ((in_be32(&regs->ievent) & (IEVENT_GRSC | IEVENT_GTSC))
  317. != (IEVENT_GRSC | IEVENT_GTSC))
  318. ;
  319. clrbits_be32(&regs->maccfg1, MACCFG1_TX_EN | MACCFG1_RX_EN);
  320. /* Shut down the PHY, as needed */
  321. phy_shutdown(priv->phydev);
  322. }
  323. #ifdef CONFIG_SYS_FSL_ERRATUM_NMG_ETSEC129
  324. /*
  325. * When MACCFG1[Rx_EN] is enabled during system boot as part
  326. * of the eTSEC port initialization sequence,
  327. * the eTSEC Rx logic may not be properly initialized.
  328. */
  329. void redundant_init(struct tsec_private *priv)
  330. {
  331. struct tsec __iomem *regs = priv->regs;
  332. uint t, count = 0;
  333. int fail = 1;
  334. static const u8 pkt[] = {
  335. 0x00, 0x1e, 0x4f, 0x12, 0xcb, 0x2c, 0x00, 0x25,
  336. 0x64, 0xbb, 0xd1, 0xab, 0x08, 0x00, 0x45, 0x00,
  337. 0x00, 0x5c, 0xdd, 0x22, 0x00, 0x00, 0x80, 0x01,
  338. 0x1f, 0x71, 0x0a, 0xc1, 0x14, 0x22, 0x0a, 0xc1,
  339. 0x14, 0x6a, 0x08, 0x00, 0xef, 0x7e, 0x02, 0x00,
  340. 0x94, 0x05, 0x61, 0x62, 0x63, 0x64, 0x65, 0x66,
  341. 0x67, 0x68, 0x69, 0x6a, 0x6b, 0x6c, 0x6d, 0x6e,
  342. 0x6f, 0x70, 0x71, 0x72, 0x73, 0x74, 0x75, 0x76,
  343. 0x77, 0x61, 0x62, 0x63, 0x64, 0x65, 0x66, 0x67,
  344. 0x68, 0x69, 0x6a, 0x6b, 0x6c, 0x6d, 0x6e, 0x6f,
  345. 0x70, 0x71, 0x72, 0x73, 0x74, 0x75, 0x76, 0x77,
  346. 0x61, 0x62, 0x63, 0x64, 0x65, 0x66, 0x67, 0x68,
  347. 0x69, 0x6a, 0x6b, 0x6c, 0x6d, 0x6e, 0x6f, 0x70,
  348. 0x71, 0x72};
  349. /* Enable promiscuous mode */
  350. setbits_be32(&regs->rctrl, 0x8);
  351. /* Enable loopback mode */
  352. setbits_be32(&regs->maccfg1, MACCFG1_LOOPBACK);
  353. /* Enable transmit and receive */
  354. setbits_be32(&regs->maccfg1, MACCFG1_RX_EN | MACCFG1_TX_EN);
  355. /* Tell the DMA it is clear to go */
  356. setbits_be32(&regs->dmactrl, DMACTRL_INIT_SETTINGS);
  357. out_be32(&regs->tstat, TSTAT_CLEAR_THALT);
  358. out_be32(&regs->rstat, RSTAT_CLEAR_RHALT);
  359. clrbits_be32(&regs->dmactrl, DMACTRL_GRS | DMACTRL_GTS);
  360. do {
  361. u16 status;
  362. tsec_send(priv->dev, (void *)pkt, sizeof(pkt));
  363. /* Wait for buffer to be received */
  364. for (t = 0;
  365. in_be16(&priv->rxbd[priv->rx_idx].status) & RXBD_EMPTY;
  366. t++) {
  367. if (t >= 10 * TOUT_LOOP) {
  368. printf("%s: tsec: rx error\n", priv->dev->name);
  369. break;
  370. }
  371. }
  372. if (!memcmp(pkt, net_rx_packets[priv->rx_idx], sizeof(pkt)))
  373. fail = 0;
  374. out_be16(&priv->rxbd[priv->rx_idx].length, 0);
  375. status = RXBD_EMPTY;
  376. if ((priv->rx_idx + 1) == PKTBUFSRX)
  377. status |= RXBD_WRAP;
  378. out_be16(&priv->rxbd[priv->rx_idx].status, status);
  379. priv->rx_idx = (priv->rx_idx + 1) % PKTBUFSRX;
  380. if (in_be32(&regs->ievent) & IEVENT_BSY) {
  381. out_be32(&regs->ievent, IEVENT_BSY);
  382. out_be32(&regs->rstat, RSTAT_CLEAR_RHALT);
  383. }
  384. if (fail) {
  385. printf("loopback recv packet error!\n");
  386. clrbits_be32(&regs->maccfg1, MACCFG1_RX_EN);
  387. udelay(1000);
  388. setbits_be32(&regs->maccfg1, MACCFG1_RX_EN);
  389. }
  390. } while ((count++ < 4) && (fail == 1));
  391. if (fail)
  392. panic("eTSEC init fail!\n");
  393. /* Disable promiscuous mode */
  394. clrbits_be32(&regs->rctrl, 0x8);
  395. /* Disable loopback mode */
  396. clrbits_be32(&regs->maccfg1, MACCFG1_LOOPBACK);
  397. }
  398. #endif
  399. /*
  400. * Set up the buffers and their descriptors, and bring up the
  401. * interface
  402. */
  403. static void startup_tsec(struct tsec_private *priv)
  404. {
  405. struct tsec __iomem *regs = priv->regs;
  406. u16 status;
  407. int i;
  408. /* reset the indices to zero */
  409. priv->rx_idx = 0;
  410. priv->tx_idx = 0;
  411. #ifdef CONFIG_SYS_FSL_ERRATUM_NMG_ETSEC129
  412. uint svr;
  413. #endif
  414. /* Point to the buffer descriptors */
  415. out_be32(&regs->tbase, (u32)&priv->txbd[0]);
  416. out_be32(&regs->rbase, (u32)&priv->rxbd[0]);
  417. /* Initialize the Rx Buffer descriptors */
  418. for (i = 0; i < PKTBUFSRX; i++) {
  419. out_be16(&priv->rxbd[i].status, RXBD_EMPTY);
  420. out_be16(&priv->rxbd[i].length, 0);
  421. out_be32(&priv->rxbd[i].bufptr, (u32)net_rx_packets[i]);
  422. }
  423. status = in_be16(&priv->rxbd[PKTBUFSRX - 1].status);
  424. out_be16(&priv->rxbd[PKTBUFSRX - 1].status, status | RXBD_WRAP);
  425. /* Initialize the TX Buffer Descriptors */
  426. for (i = 0; i < TX_BUF_CNT; i++) {
  427. out_be16(&priv->txbd[i].status, 0);
  428. out_be16(&priv->txbd[i].length, 0);
  429. out_be32(&priv->txbd[i].bufptr, 0);
  430. }
  431. status = in_be16(&priv->txbd[TX_BUF_CNT - 1].status);
  432. out_be16(&priv->txbd[TX_BUF_CNT - 1].status, status | TXBD_WRAP);
  433. #ifdef CONFIG_SYS_FSL_ERRATUM_NMG_ETSEC129
  434. svr = get_svr();
  435. if ((SVR_MAJ(svr) == 1) || IS_SVR_REV(svr, 2, 0))
  436. redundant_init(priv);
  437. #endif
  438. /* Enable Transmit and Receive */
  439. setbits_be32(&regs->maccfg1, MACCFG1_RX_EN | MACCFG1_TX_EN);
  440. /* Tell the DMA it is clear to go */
  441. setbits_be32(&regs->dmactrl, DMACTRL_INIT_SETTINGS);
  442. out_be32(&regs->tstat, TSTAT_CLEAR_THALT);
  443. out_be32(&regs->rstat, RSTAT_CLEAR_RHALT);
  444. clrbits_be32(&regs->dmactrl, DMACTRL_GRS | DMACTRL_GTS);
  445. }
  446. /*
  447. * Initializes data structures and registers for the controller,
  448. * and brings the interface up. Returns the link status, meaning
  449. * that it returns success if the link is up, failure otherwise.
  450. * This allows U-Boot to find the first active controller.
  451. */
  452. #ifndef CONFIG_DM_ETH
  453. static int tsec_init(struct eth_device *dev, bd_t *bd)
  454. #else
  455. static int tsec_init(struct udevice *dev)
  456. #endif
  457. {
  458. struct tsec_private *priv = (struct tsec_private *)dev->priv;
  459. #ifdef CONFIG_DM_ETH
  460. struct eth_pdata *pdata = dev_get_platdata(dev);
  461. #endif
  462. struct tsec __iomem *regs = priv->regs;
  463. u32 tempval;
  464. int ret;
  465. /* Make sure the controller is stopped */
  466. tsec_halt(dev);
  467. /* Init MACCFG2. Defaults to GMII */
  468. out_be32(&regs->maccfg2, MACCFG2_INIT_SETTINGS);
  469. /* Init ECNTRL */
  470. out_be32(&regs->ecntrl, ECNTRL_INIT_SETTINGS);
  471. /*
  472. * Copy the station address into the address registers.
  473. * For a station address of 0x12345678ABCD in transmission
  474. * order (BE), MACnADDR1 is set to 0xCDAB7856 and
  475. * MACnADDR2 is set to 0x34120000.
  476. */
  477. #ifndef CONFIG_DM_ETH
  478. tempval = (dev->enetaddr[5] << 24) | (dev->enetaddr[4] << 16) |
  479. (dev->enetaddr[3] << 8) | dev->enetaddr[2];
  480. #else
  481. tempval = (pdata->enetaddr[5] << 24) | (pdata->enetaddr[4] << 16) |
  482. (pdata->enetaddr[3] << 8) | pdata->enetaddr[2];
  483. #endif
  484. out_be32(&regs->macstnaddr1, tempval);
  485. #ifndef CONFIG_DM_ETH
  486. tempval = (dev->enetaddr[1] << 24) | (dev->enetaddr[0] << 16);
  487. #else
  488. tempval = (pdata->enetaddr[1] << 24) | (pdata->enetaddr[0] << 16);
  489. #endif
  490. out_be32(&regs->macstnaddr2, tempval);
  491. /* Clear out (for the most part) the other registers */
  492. init_registers(regs);
  493. /* Ready the device for tx/rx */
  494. startup_tsec(priv);
  495. /* Start up the PHY */
  496. ret = phy_startup(priv->phydev);
  497. if (ret) {
  498. printf("Could not initialize PHY %s\n",
  499. priv->phydev->dev->name);
  500. return ret;
  501. }
  502. adjust_link(priv, priv->phydev);
  503. /* If there's no link, fail */
  504. return priv->phydev->link ? 0 : -1;
  505. }
  506. static phy_interface_t tsec_get_interface(struct tsec_private *priv)
  507. {
  508. struct tsec __iomem *regs = priv->regs;
  509. u32 ecntrl;
  510. ecntrl = in_be32(&regs->ecntrl);
  511. if (ecntrl & ECNTRL_SGMII_MODE)
  512. return PHY_INTERFACE_MODE_SGMII;
  513. if (ecntrl & ECNTRL_TBI_MODE) {
  514. if (ecntrl & ECNTRL_REDUCED_MODE)
  515. return PHY_INTERFACE_MODE_RTBI;
  516. else
  517. return PHY_INTERFACE_MODE_TBI;
  518. }
  519. if (ecntrl & ECNTRL_REDUCED_MODE) {
  520. phy_interface_t interface;
  521. if (ecntrl & ECNTRL_REDUCED_MII_MODE)
  522. return PHY_INTERFACE_MODE_RMII;
  523. interface = priv->interface;
  524. /*
  525. * This isn't autodetected, so it must
  526. * be set by the platform code.
  527. */
  528. if (interface == PHY_INTERFACE_MODE_RGMII_ID ||
  529. interface == PHY_INTERFACE_MODE_RGMII_TXID ||
  530. interface == PHY_INTERFACE_MODE_RGMII_RXID)
  531. return interface;
  532. return PHY_INTERFACE_MODE_RGMII;
  533. }
  534. if (priv->flags & TSEC_GIGABIT)
  535. return PHY_INTERFACE_MODE_GMII;
  536. return PHY_INTERFACE_MODE_MII;
  537. }
  538. /*
  539. * Discover which PHY is attached to the device, and configure it
  540. * properly. If the PHY is not recognized, then return 0
  541. * (failure). Otherwise, return 1
  542. */
  543. static int init_phy(struct tsec_private *priv)
  544. {
  545. struct phy_device *phydev;
  546. struct tsec __iomem *regs = priv->regs;
  547. u32 supported = (SUPPORTED_10baseT_Half |
  548. SUPPORTED_10baseT_Full |
  549. SUPPORTED_100baseT_Half |
  550. SUPPORTED_100baseT_Full);
  551. if (priv->flags & TSEC_GIGABIT)
  552. supported |= SUPPORTED_1000baseT_Full;
  553. /* Assign a Physical address to the TBI */
  554. out_be32(&regs->tbipa, priv->tbiaddr);
  555. priv->interface = tsec_get_interface(priv);
  556. if (priv->interface == PHY_INTERFACE_MODE_SGMII)
  557. tsec_configure_serdes(priv);
  558. phydev = phy_connect(priv->bus, priv->phyaddr, priv->dev,
  559. priv->interface);
  560. if (!phydev)
  561. return 0;
  562. phydev->supported &= supported;
  563. phydev->advertising = phydev->supported;
  564. priv->phydev = phydev;
  565. phy_config(phydev);
  566. return 1;
  567. }
  568. #ifndef CONFIG_DM_ETH
  569. /*
  570. * Initialize device structure. Returns success if PHY
  571. * initialization succeeded (i.e. if it recognizes the PHY)
  572. */
  573. static int tsec_initialize(bd_t *bis, struct tsec_info_struct *tsec_info)
  574. {
  575. struct eth_device *dev;
  576. int i;
  577. struct tsec_private *priv;
  578. dev = (struct eth_device *)malloc(sizeof(*dev));
  579. if (!dev)
  580. return 0;
  581. memset(dev, 0, sizeof(*dev));
  582. priv = (struct tsec_private *)malloc(sizeof(*priv));
  583. if (!priv) {
  584. free(dev);
  585. return 0;
  586. }
  587. priv->regs = tsec_info->regs;
  588. priv->phyregs_sgmii = tsec_info->miiregs_sgmii;
  589. priv->phyaddr = tsec_info->phyaddr;
  590. priv->tbiaddr = CONFIG_SYS_TBIPA_VALUE;
  591. priv->flags = tsec_info->flags;
  592. strcpy(dev->name, tsec_info->devname);
  593. priv->interface = tsec_info->interface;
  594. priv->bus = miiphy_get_dev_by_name(tsec_info->mii_devname);
  595. priv->dev = dev;
  596. dev->iobase = 0;
  597. dev->priv = priv;
  598. dev->init = tsec_init;
  599. dev->halt = tsec_halt;
  600. dev->send = tsec_send;
  601. dev->recv = tsec_recv;
  602. #ifdef CONFIG_MCAST_TFTP
  603. dev->mcast = tsec_mcast_addr;
  604. #endif
  605. /* Tell U-Boot to get the addr from the env */
  606. for (i = 0; i < 6; i++)
  607. dev->enetaddr[i] = 0;
  608. eth_register(dev);
  609. /* Reset the MAC */
  610. setbits_be32(&priv->regs->maccfg1, MACCFG1_SOFT_RESET);
  611. udelay(2); /* Soft Reset must be asserted for 3 TX clocks */
  612. clrbits_be32(&priv->regs->maccfg1, MACCFG1_SOFT_RESET);
  613. /* Try to initialize PHY here, and return */
  614. return init_phy(priv);
  615. }
  616. /*
  617. * Initialize all the TSEC devices
  618. *
  619. * Returns the number of TSEC devices that were initialized
  620. */
  621. int tsec_eth_init(bd_t *bis, struct tsec_info_struct *tsecs, int num)
  622. {
  623. int i;
  624. int count = 0;
  625. for (i = 0; i < num; i++) {
  626. int ret = tsec_initialize(bis, &tsecs[i]);
  627. if (ret > 0)
  628. count += ret;
  629. }
  630. return count;
  631. }
  632. int tsec_standard_init(bd_t *bis)
  633. {
  634. struct fsl_pq_mdio_info info;
  635. info.regs = TSEC_GET_MDIO_REGS_BASE(1);
  636. info.name = DEFAULT_MII_NAME;
  637. fsl_pq_mdio_init(bis, &info);
  638. return tsec_eth_init(bis, tsec_info, ARRAY_SIZE(tsec_info));
  639. }
  640. #else /* CONFIG_DM_ETH */
  641. int tsec_probe(struct udevice *dev)
  642. {
  643. struct tsec_private *priv = dev_get_priv(dev);
  644. struct eth_pdata *pdata = dev_get_platdata(dev);
  645. struct fsl_pq_mdio_info mdio_info;
  646. struct ofnode_phandle_args phandle_args;
  647. ofnode parent;
  648. const char *phy_mode;
  649. int ret;
  650. pdata->iobase = (phys_addr_t)dev_read_addr(dev);
  651. priv->regs = (struct tsec *)pdata->iobase;
  652. if (dev_read_phandle_with_args(dev, "phy-handle", NULL, 0, 0,
  653. &phandle_args)) {
  654. debug("phy-handle does not exist under tsec %s\n", dev->name);
  655. return -ENOENT;
  656. } else {
  657. int reg = ofnode_read_u32_default(phandle_args.node, "reg", 0);
  658. priv->phyaddr = reg;
  659. }
  660. parent = ofnode_get_parent(phandle_args.node);
  661. if (ofnode_valid(parent)) {
  662. int reg = ofnode_read_u32_default(parent, "reg", 0);
  663. priv->phyregs_sgmii = (struct tsec_mii_mng *)(reg + 0x520);
  664. } else {
  665. debug("No parent node for PHY?\n");
  666. return -ENOENT;
  667. }
  668. if (dev_read_phandle_with_args(dev, "tbi-handle", NULL, 0, 0,
  669. &phandle_args)) {
  670. priv->tbiaddr = CONFIG_SYS_TBIPA_VALUE;
  671. } else {
  672. int reg = ofnode_read_u32_default(phandle_args.node, "reg",
  673. CONFIG_SYS_TBIPA_VALUE);
  674. priv->tbiaddr = reg;
  675. }
  676. phy_mode = dev_read_prop(dev, "phy-connection-type", NULL);
  677. if (phy_mode)
  678. pdata->phy_interface = phy_get_interface_by_name(phy_mode);
  679. if (pdata->phy_interface == -1) {
  680. debug("Invalid PHY interface '%s'\n", phy_mode);
  681. return -EINVAL;
  682. }
  683. priv->interface = pdata->phy_interface;
  684. /* Initialize flags */
  685. priv->flags = TSEC_GIGABIT;
  686. if (priv->interface == PHY_INTERFACE_MODE_SGMII)
  687. priv->flags |= TSEC_SGMII;
  688. mdio_info.regs = priv->phyregs_sgmii;
  689. mdio_info.name = (char *)dev->name;
  690. ret = fsl_pq_mdio_init(NULL, &mdio_info);
  691. if (ret)
  692. return ret;
  693. /* Reset the MAC */
  694. setbits_be32(&priv->regs->maccfg1, MACCFG1_SOFT_RESET);
  695. udelay(2); /* Soft Reset must be asserted for 3 TX clocks */
  696. clrbits_be32(&priv->regs->maccfg1, MACCFG1_SOFT_RESET);
  697. priv->dev = dev;
  698. priv->bus = miiphy_get_dev_by_name(dev->name);
  699. /* Try to initialize PHY here, and return */
  700. return !init_phy(priv);
  701. }
  702. int tsec_remove(struct udevice *dev)
  703. {
  704. struct tsec_private *priv = dev->priv;
  705. free(priv->phydev);
  706. mdio_unregister(priv->bus);
  707. mdio_free(priv->bus);
  708. return 0;
  709. }
  710. static const struct eth_ops tsec_ops = {
  711. .start = tsec_init,
  712. .send = tsec_send,
  713. .recv = tsec_recv,
  714. .free_pkt = tsec_free_pkt,
  715. .stop = tsec_halt,
  716. #ifdef CONFIG_MCAST_TFTP
  717. .mcast = tsec_mcast_addr,
  718. #endif
  719. };
  720. static const struct udevice_id tsec_ids[] = {
  721. { .compatible = "fsl,tsec" },
  722. { }
  723. };
  724. U_BOOT_DRIVER(eth_tsec) = {
  725. .name = "tsec",
  726. .id = UCLASS_ETH,
  727. .of_match = tsec_ids,
  728. .probe = tsec_probe,
  729. .remove = tsec_remove,
  730. .ops = &tsec_ops,
  731. .priv_auto_alloc_size = sizeof(struct tsec_private),
  732. .platdata_auto_alloc_size = sizeof(struct eth_pdata),
  733. .flags = DM_FLAG_ALLOC_PRIV_DMA,
  734. };
  735. #endif /* CONFIG_DM_ETH */