tsec.c 20 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772
  1. // SPDX-License-Identifier: GPL-2.0+
  2. /*
  3. * Freescale Three Speed Ethernet Controller driver
  4. *
  5. * Copyright 2004-2011, 2013 Freescale Semiconductor, Inc.
  6. * (C) Copyright 2003, Motorola, Inc.
  7. * author Andy Fleming
  8. */
  9. #include <config.h>
  10. #include <common.h>
  11. #include <dm.h>
  12. #include <malloc.h>
  13. #include <net.h>
  14. #include <command.h>
  15. #include <tsec.h>
  16. #include <fsl_mdio.h>
  17. #include <linux/bitops.h>
  18. #include <linux/delay.h>
  19. #include <linux/errno.h>
  20. #include <miiphy.h>
  21. #include <asm/processor.h>
  22. #include <asm/io.h>
  23. #define TBIANA_SETTINGS ( \
  24. TBIANA_ASYMMETRIC_PAUSE \
  25. | TBIANA_SYMMETRIC_PAUSE \
  26. | TBIANA_FULL_DUPLEX \
  27. )
  28. /* By default force the TBI PHY into 1000Mbps full duplex when in SGMII mode */
  29. #ifndef CFG_TSEC_TBICR_SETTINGS
  30. #define CFG_TSEC_TBICR_SETTINGS ( \
  31. TBICR_PHY_RESET \
  32. | TBICR_ANEG_ENABLE \
  33. | TBICR_FULL_DUPLEX \
  34. | TBICR_SPEED1_SET \
  35. )
  36. #endif /* CFG_TSEC_TBICR_SETTINGS */
  37. /* Configure the TBI for SGMII operation */
  38. static void tsec_configure_serdes(struct tsec_private *priv)
  39. {
  40. /*
  41. * Access TBI PHY registers at given TSEC register offset as opposed
  42. * to the register offset used for external PHY accesses
  43. */
  44. tsec_local_mdio_write(priv->phyregs_sgmii, in_be32(&priv->regs->tbipa),
  45. 0, TBI_ANA, TBIANA_SETTINGS);
  46. tsec_local_mdio_write(priv->phyregs_sgmii, in_be32(&priv->regs->tbipa),
  47. 0, TBI_TBICON, TBICON_CLK_SELECT);
  48. tsec_local_mdio_write(priv->phyregs_sgmii, in_be32(&priv->regs->tbipa),
  49. 0, TBI_CR, CFG_TSEC_TBICR_SETTINGS);
  50. }
  51. /* the 'way' for ethernet-CRC-32. Spliced in from Linux lib/crc32.c
  52. * and this is the ethernet-crc method needed for TSEC -- and perhaps
  53. * some other adapter -- hash tables
  54. */
  55. #define CRCPOLY_LE 0xedb88320
  56. static u32 ether_crc(size_t len, unsigned char const *p)
  57. {
  58. int i;
  59. u32 crc;
  60. crc = ~0;
  61. while (len--) {
  62. crc ^= *p++;
  63. for (i = 0; i < 8; i++)
  64. crc = (crc >> 1) ^ ((crc & 1) ? CRCPOLY_LE : 0);
  65. }
  66. /* an reverse the bits, cuz of way they arrive -- last-first */
  67. crc = (crc >> 16) | (crc << 16);
  68. crc = (crc >> 8 & 0x00ff00ff) | (crc << 8 & 0xff00ff00);
  69. crc = (crc >> 4 & 0x0f0f0f0f) | (crc << 4 & 0xf0f0f0f0);
  70. crc = (crc >> 2 & 0x33333333) | (crc << 2 & 0xcccccccc);
  71. crc = (crc >> 1 & 0x55555555) | (crc << 1 & 0xaaaaaaaa);
  72. return crc;
  73. }
  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 udevice *dev, const u8 *mcast_mac, int join)
  91. {
  92. struct tsec_private *priv;
  93. struct tsec __iomem *regs;
  94. u32 result, value;
  95. u8 whichbit, whichreg;
  96. priv = dev_get_priv(dev);
  97. regs = priv->regs;
  98. result = ether_crc(MAC_ADDR_LEN, mcast_mac);
  99. whichbit = (result >> 24) & 0x1f; /* the 5 LSB = which bit to set */
  100. whichreg = result >> 29; /* the 3 MSB = which reg to set it in */
  101. value = BIT(31 - whichbit);
  102. if (join)
  103. setbits_be32(&regs->hash.gaddr0 + whichreg, value);
  104. else
  105. clrbits_be32(&regs->hash.gaddr0 + whichreg, value);
  106. return 0;
  107. }
  108. static int __maybe_unused tsec_set_promisc(struct udevice *dev, bool enable)
  109. {
  110. struct tsec_private *priv = dev_get_priv(dev);
  111. struct tsec __iomem *regs = priv->regs;
  112. if (enable)
  113. setbits_be32(&regs->rctrl, RCTRL_PROM);
  114. else
  115. clrbits_be32(&regs->rctrl, RCTRL_PROM);
  116. return 0;
  117. }
  118. /*
  119. * Initialized required registers to appropriate values, zeroing
  120. * those we don't care about (unless zero is bad, in which case,
  121. * choose a more appropriate value)
  122. */
  123. static void init_registers(struct tsec __iomem *regs)
  124. {
  125. /* Clear IEVENT */
  126. out_be32(&regs->ievent, IEVENT_INIT_CLEAR);
  127. out_be32(&regs->imask, IMASK_INIT_CLEAR);
  128. out_be32(&regs->hash.iaddr0, 0);
  129. out_be32(&regs->hash.iaddr1, 0);
  130. out_be32(&regs->hash.iaddr2, 0);
  131. out_be32(&regs->hash.iaddr3, 0);
  132. out_be32(&regs->hash.iaddr4, 0);
  133. out_be32(&regs->hash.iaddr5, 0);
  134. out_be32(&regs->hash.iaddr6, 0);
  135. out_be32(&regs->hash.iaddr7, 0);
  136. out_be32(&regs->hash.gaddr0, 0);
  137. out_be32(&regs->hash.gaddr1, 0);
  138. out_be32(&regs->hash.gaddr2, 0);
  139. out_be32(&regs->hash.gaddr3, 0);
  140. out_be32(&regs->hash.gaddr4, 0);
  141. out_be32(&regs->hash.gaddr5, 0);
  142. out_be32(&regs->hash.gaddr6, 0);
  143. out_be32(&regs->hash.gaddr7, 0);
  144. /* Init RMON mib registers */
  145. memset((void *)&regs->rmon, 0, sizeof(regs->rmon));
  146. out_be32(&regs->rmon.cam1, 0xffffffff);
  147. out_be32(&regs->rmon.cam2, 0xffffffff);
  148. out_be32(&regs->mrblr, MRBLR_INIT_SETTINGS);
  149. out_be32(&regs->minflr, MINFLR_INIT_SETTINGS);
  150. out_be32(&regs->attr, ATTR_INIT_SETTINGS);
  151. out_be32(&regs->attreli, ATTRELI_INIT_SETTINGS);
  152. }
  153. /*
  154. * Configure maccfg2 based on negotiated speed and duplex
  155. * reported by PHY handling code
  156. */
  157. static void adjust_link(struct tsec_private *priv, struct phy_device *phydev)
  158. {
  159. struct tsec __iomem *regs = priv->regs;
  160. u32 ecntrl, maccfg2;
  161. if (!phydev->link) {
  162. printf("%s: No link.\n", phydev->dev->name);
  163. return;
  164. }
  165. /* clear all bits relative with interface mode */
  166. ecntrl = in_be32(&regs->ecntrl);
  167. ecntrl &= ~ECNTRL_R100;
  168. maccfg2 = in_be32(&regs->maccfg2);
  169. maccfg2 &= ~(MACCFG2_IF | MACCFG2_FULL_DUPLEX);
  170. if (phydev->duplex)
  171. maccfg2 |= MACCFG2_FULL_DUPLEX;
  172. switch (phydev->speed) {
  173. case 1000:
  174. maccfg2 |= MACCFG2_GMII;
  175. break;
  176. case 100:
  177. case 10:
  178. maccfg2 |= MACCFG2_MII;
  179. /*
  180. * Set R100 bit in all modes although
  181. * it is only used in RGMII mode
  182. */
  183. if (phydev->speed == 100)
  184. ecntrl |= ECNTRL_R100;
  185. break;
  186. default:
  187. printf("%s: Speed was bad\n", phydev->dev->name);
  188. break;
  189. }
  190. out_be32(&regs->ecntrl, ecntrl);
  191. out_be32(&regs->maccfg2, maccfg2);
  192. printf("Speed: %d, %s duplex%s\n", phydev->speed,
  193. (phydev->duplex) ? "full" : "half",
  194. (phydev->port == PORT_FIBRE) ? ", fiber mode" : "");
  195. }
  196. /*
  197. * This returns the status bits of the device. The return value
  198. * is never checked, and this is what the 8260 driver did, so we
  199. * do the same. Presumably, this would be zero if there were no
  200. * errors
  201. */
  202. static int tsec_send(struct udevice *dev, void *packet, int length)
  203. {
  204. struct tsec_private *priv;
  205. struct tsec __iomem *regs;
  206. int result = 0;
  207. u16 status;
  208. int i;
  209. priv = dev_get_priv(dev);
  210. regs = priv->regs;
  211. /* Find an empty buffer descriptor */
  212. for (i = 0;
  213. in_be16(&priv->txbd[priv->tx_idx].status) & TXBD_READY;
  214. i++) {
  215. if (i >= TOUT_LOOP) {
  216. printf("%s: tsec: tx buffers full\n", dev->name);
  217. return result;
  218. }
  219. }
  220. out_be32(&priv->txbd[priv->tx_idx].bufptr, (u32)packet);
  221. out_be16(&priv->txbd[priv->tx_idx].length, length);
  222. status = in_be16(&priv->txbd[priv->tx_idx].status);
  223. out_be16(&priv->txbd[priv->tx_idx].status, status |
  224. (TXBD_READY | TXBD_LAST | TXBD_CRC | TXBD_INTERRUPT));
  225. /* Tell the DMA to go */
  226. out_be32(&regs->tstat, TSTAT_CLEAR_THALT);
  227. /* Wait for buffer to be transmitted */
  228. for (i = 0;
  229. in_be16(&priv->txbd[priv->tx_idx].status) & TXBD_READY;
  230. i++) {
  231. if (i >= TOUT_LOOP) {
  232. printf("%s: tsec: tx error\n", dev->name);
  233. return result;
  234. }
  235. }
  236. priv->tx_idx = (priv->tx_idx + 1) % TX_BUF_CNT;
  237. result = in_be16(&priv->txbd[priv->tx_idx].status) & TXBD_STATS;
  238. return result;
  239. }
  240. static int tsec_recv(struct udevice *dev, int flags, uchar **packetp)
  241. {
  242. struct tsec_private *priv = (struct tsec_private *)dev_get_priv(dev);
  243. struct tsec __iomem *regs = priv->regs;
  244. int ret = -1;
  245. if (!(in_be16(&priv->rxbd[priv->rx_idx].status) & RXBD_EMPTY)) {
  246. int length = in_be16(&priv->rxbd[priv->rx_idx].length);
  247. u16 status = in_be16(&priv->rxbd[priv->rx_idx].status);
  248. u32 buf;
  249. /* Send the packet up if there were no errors */
  250. if (!(status & RXBD_STATS)) {
  251. buf = in_be32(&priv->rxbd[priv->rx_idx].bufptr);
  252. *packetp = (uchar *)buf;
  253. ret = length - 4;
  254. } else {
  255. printf("Got error %x\n", (status & RXBD_STATS));
  256. }
  257. }
  258. if (in_be32(&regs->ievent) & IEVENT_BSY) {
  259. out_be32(&regs->ievent, IEVENT_BSY);
  260. out_be32(&regs->rstat, RSTAT_CLEAR_RHALT);
  261. }
  262. return ret;
  263. }
  264. static int tsec_free_pkt(struct udevice *dev, uchar *packet, int length)
  265. {
  266. struct tsec_private *priv = (struct tsec_private *)dev_get_priv(dev);
  267. u16 status;
  268. out_be16(&priv->rxbd[priv->rx_idx].length, 0);
  269. status = RXBD_EMPTY;
  270. /* Set the wrap bit if this is the last element in the list */
  271. if ((priv->rx_idx + 1) == PKTBUFSRX)
  272. status |= RXBD_WRAP;
  273. out_be16(&priv->rxbd[priv->rx_idx].status, status);
  274. priv->rx_idx = (priv->rx_idx + 1) % PKTBUFSRX;
  275. return 0;
  276. }
  277. static void tsec_halt(struct udevice *dev)
  278. {
  279. struct tsec_private *priv;
  280. struct tsec __iomem *regs;
  281. priv = dev_get_priv(dev);
  282. regs = priv->regs;
  283. clrbits_be32(&regs->dmactrl, DMACTRL_GRS | DMACTRL_GTS);
  284. setbits_be32(&regs->dmactrl, DMACTRL_GRS | DMACTRL_GTS);
  285. while ((in_be32(&regs->ievent) & (IEVENT_GRSC | IEVENT_GTSC))
  286. != (IEVENT_GRSC | IEVENT_GTSC))
  287. ;
  288. clrbits_be32(&regs->maccfg1, MACCFG1_TX_EN | MACCFG1_RX_EN);
  289. /* Shut down the PHY, as needed */
  290. phy_shutdown(priv->phydev);
  291. }
  292. #ifdef CONFIG_SYS_FSL_ERRATUM_NMG_ETSEC129
  293. /*
  294. * When MACCFG1[Rx_EN] is enabled during system boot as part
  295. * of the eTSEC port initialization sequence,
  296. * the eTSEC Rx logic may not be properly initialized.
  297. */
  298. static void redundant_init(struct tsec_private *priv)
  299. {
  300. struct tsec __iomem *regs = priv->regs;
  301. uint t, count = 0;
  302. int fail = 1;
  303. static const u8 pkt[] = {
  304. 0x00, 0x1e, 0x4f, 0x12, 0xcb, 0x2c, 0x00, 0x25,
  305. 0x64, 0xbb, 0xd1, 0xab, 0x08, 0x00, 0x45, 0x00,
  306. 0x00, 0x5c, 0xdd, 0x22, 0x00, 0x00, 0x80, 0x01,
  307. 0x1f, 0x71, 0x0a, 0xc1, 0x14, 0x22, 0x0a, 0xc1,
  308. 0x14, 0x6a, 0x08, 0x00, 0xef, 0x7e, 0x02, 0x00,
  309. 0x94, 0x05, 0x61, 0x62, 0x63, 0x64, 0x65, 0x66,
  310. 0x67, 0x68, 0x69, 0x6a, 0x6b, 0x6c, 0x6d, 0x6e,
  311. 0x6f, 0x70, 0x71, 0x72, 0x73, 0x74, 0x75, 0x76,
  312. 0x77, 0x61, 0x62, 0x63, 0x64, 0x65, 0x66, 0x67,
  313. 0x68, 0x69, 0x6a, 0x6b, 0x6c, 0x6d, 0x6e, 0x6f,
  314. 0x70, 0x71, 0x72, 0x73, 0x74, 0x75, 0x76, 0x77,
  315. 0x61, 0x62, 0x63, 0x64, 0x65, 0x66, 0x67, 0x68,
  316. 0x69, 0x6a, 0x6b, 0x6c, 0x6d, 0x6e, 0x6f, 0x70,
  317. 0x71, 0x72};
  318. /* Enable promiscuous mode */
  319. setbits_be32(&regs->rctrl, RCTRL_PROM);
  320. /* Enable loopback mode */
  321. setbits_be32(&regs->maccfg1, MACCFG1_LOOPBACK);
  322. /* Enable transmit and receive */
  323. setbits_be32(&regs->maccfg1, MACCFG1_RX_EN | MACCFG1_TX_EN);
  324. /* Tell the DMA it is clear to go */
  325. setbits_be32(&regs->dmactrl, DMACTRL_INIT_SETTINGS);
  326. out_be32(&regs->tstat, TSTAT_CLEAR_THALT);
  327. out_be32(&regs->rstat, RSTAT_CLEAR_RHALT);
  328. clrbits_be32(&regs->dmactrl, DMACTRL_GRS | DMACTRL_GTS);
  329. do {
  330. u16 status;
  331. tsec_send(priv->dev, (void *)pkt, sizeof(pkt));
  332. /* Wait for buffer to be received */
  333. for (t = 0;
  334. in_be16(&priv->rxbd[priv->rx_idx].status) & RXBD_EMPTY;
  335. t++) {
  336. if (t >= 10 * TOUT_LOOP) {
  337. printf("%s: tsec: rx error\n", priv->dev->name);
  338. break;
  339. }
  340. }
  341. if (!memcmp(pkt, net_rx_packets[priv->rx_idx], sizeof(pkt)))
  342. fail = 0;
  343. out_be16(&priv->rxbd[priv->rx_idx].length, 0);
  344. status = RXBD_EMPTY;
  345. if ((priv->rx_idx + 1) == PKTBUFSRX)
  346. status |= RXBD_WRAP;
  347. out_be16(&priv->rxbd[priv->rx_idx].status, status);
  348. priv->rx_idx = (priv->rx_idx + 1) % PKTBUFSRX;
  349. if (in_be32(&regs->ievent) & IEVENT_BSY) {
  350. out_be32(&regs->ievent, IEVENT_BSY);
  351. out_be32(&regs->rstat, RSTAT_CLEAR_RHALT);
  352. }
  353. if (fail) {
  354. printf("loopback recv packet error!\n");
  355. clrbits_be32(&regs->maccfg1, MACCFG1_RX_EN);
  356. udelay(1000);
  357. setbits_be32(&regs->maccfg1, MACCFG1_RX_EN);
  358. }
  359. } while ((count++ < 4) && (fail == 1));
  360. if (fail)
  361. panic("eTSEC init fail!\n");
  362. /* Disable promiscuous mode */
  363. clrbits_be32(&regs->rctrl, RCTRL_PROM);
  364. /* Disable loopback mode */
  365. clrbits_be32(&regs->maccfg1, MACCFG1_LOOPBACK);
  366. }
  367. #endif
  368. /*
  369. * Set up the buffers and their descriptors, and bring up the
  370. * interface
  371. */
  372. static void startup_tsec(struct tsec_private *priv)
  373. {
  374. struct tsec __iomem *regs = priv->regs;
  375. u16 status;
  376. int i;
  377. /* reset the indices to zero */
  378. priv->rx_idx = 0;
  379. priv->tx_idx = 0;
  380. #ifdef CONFIG_SYS_FSL_ERRATUM_NMG_ETSEC129
  381. uint svr;
  382. #endif
  383. /* Point to the buffer descriptors */
  384. out_be32(&regs->tbase, (u32)&priv->txbd[0]);
  385. out_be32(&regs->rbase, (u32)&priv->rxbd[0]);
  386. /* Initialize the Rx Buffer descriptors */
  387. for (i = 0; i < PKTBUFSRX; i++) {
  388. out_be16(&priv->rxbd[i].status, RXBD_EMPTY);
  389. out_be16(&priv->rxbd[i].length, 0);
  390. out_be32(&priv->rxbd[i].bufptr, (u32)net_rx_packets[i]);
  391. }
  392. status = in_be16(&priv->rxbd[PKTBUFSRX - 1].status);
  393. out_be16(&priv->rxbd[PKTBUFSRX - 1].status, status | RXBD_WRAP);
  394. /* Initialize the TX Buffer Descriptors */
  395. for (i = 0; i < TX_BUF_CNT; i++) {
  396. out_be16(&priv->txbd[i].status, 0);
  397. out_be16(&priv->txbd[i].length, 0);
  398. out_be32(&priv->txbd[i].bufptr, 0);
  399. }
  400. status = in_be16(&priv->txbd[TX_BUF_CNT - 1].status);
  401. out_be16(&priv->txbd[TX_BUF_CNT - 1].status, status | TXBD_WRAP);
  402. #ifdef CONFIG_SYS_FSL_ERRATUM_NMG_ETSEC129
  403. svr = get_svr();
  404. if ((SVR_MAJ(svr) == 1) || IS_SVR_REV(svr, 2, 0))
  405. redundant_init(priv);
  406. #endif
  407. /* Enable Transmit and Receive */
  408. setbits_be32(&regs->maccfg1, MACCFG1_RX_EN | MACCFG1_TX_EN);
  409. /* Tell the DMA it is clear to go */
  410. setbits_be32(&regs->dmactrl, DMACTRL_INIT_SETTINGS);
  411. out_be32(&regs->tstat, TSTAT_CLEAR_THALT);
  412. out_be32(&regs->rstat, RSTAT_CLEAR_RHALT);
  413. clrbits_be32(&regs->dmactrl, DMACTRL_GRS | DMACTRL_GTS);
  414. }
  415. /*
  416. * Initializes data structures and registers for the controller,
  417. * and brings the interface up. Returns the link status, meaning
  418. * that it returns success if the link is up, failure otherwise.
  419. * This allows U-Boot to find the first active controller.
  420. */
  421. static int tsec_init(struct udevice *dev)
  422. {
  423. struct tsec_private *priv;
  424. struct tsec __iomem *regs;
  425. struct eth_pdata *pdata = dev_get_plat(dev);
  426. u32 tempval;
  427. int ret;
  428. priv = dev_get_priv(dev);
  429. regs = priv->regs;
  430. /* Make sure the controller is stopped */
  431. tsec_halt(dev);
  432. /* Init MACCFG2. Defaults to GMII */
  433. out_be32(&regs->maccfg2, MACCFG2_INIT_SETTINGS);
  434. /* Init ECNTRL */
  435. out_be32(&regs->ecntrl, ECNTRL_INIT_SETTINGS);
  436. /*
  437. * Copy the station address into the address registers.
  438. * For a station address of 0x12345678ABCD in transmission
  439. * order (BE), MACnADDR1 is set to 0xCDAB7856 and
  440. * MACnADDR2 is set to 0x34120000.
  441. */
  442. tempval = (pdata->enetaddr[5] << 24) | (pdata->enetaddr[4] << 16) |
  443. (pdata->enetaddr[3] << 8) | pdata->enetaddr[2];
  444. out_be32(&regs->macstnaddr1, tempval);
  445. tempval = (pdata->enetaddr[1] << 24) | (pdata->enetaddr[0] << 16);
  446. out_be32(&regs->macstnaddr2, tempval);
  447. /* Clear out (for the most part) the other registers */
  448. init_registers(regs);
  449. /* Ready the device for tx/rx */
  450. startup_tsec(priv);
  451. /* Start up the PHY */
  452. ret = phy_startup(priv->phydev);
  453. if (ret) {
  454. printf("Could not initialize PHY %s\n",
  455. priv->phydev->dev->name);
  456. return ret;
  457. }
  458. adjust_link(priv, priv->phydev);
  459. /* If there's no link, fail */
  460. return priv->phydev->link ? 0 : -1;
  461. }
  462. static phy_interface_t __maybe_unused tsec_get_interface(struct tsec_private *priv)
  463. {
  464. struct tsec __iomem *regs = priv->regs;
  465. u32 ecntrl;
  466. ecntrl = in_be32(&regs->ecntrl);
  467. if (ecntrl & ECNTRL_SGMII_MODE)
  468. return PHY_INTERFACE_MODE_SGMII;
  469. if (ecntrl & ECNTRL_TBI_MODE) {
  470. if (ecntrl & ECNTRL_REDUCED_MODE)
  471. return PHY_INTERFACE_MODE_RTBI;
  472. else
  473. return PHY_INTERFACE_MODE_TBI;
  474. }
  475. if (ecntrl & ECNTRL_REDUCED_MODE) {
  476. phy_interface_t interface;
  477. if (ecntrl & ECNTRL_REDUCED_MII_MODE)
  478. return PHY_INTERFACE_MODE_RMII;
  479. interface = priv->interface;
  480. /*
  481. * This isn't autodetected, so it must
  482. * be set by the platform code.
  483. */
  484. if (interface == PHY_INTERFACE_MODE_RGMII_ID ||
  485. interface == PHY_INTERFACE_MODE_RGMII_TXID ||
  486. interface == PHY_INTERFACE_MODE_RGMII_RXID)
  487. return interface;
  488. return PHY_INTERFACE_MODE_RGMII;
  489. }
  490. if (priv->flags & TSEC_GIGABIT)
  491. return PHY_INTERFACE_MODE_GMII;
  492. return PHY_INTERFACE_MODE_MII;
  493. }
  494. /*
  495. * Discover which PHY is attached to the device, and configure it
  496. * properly. If the PHY is not recognized, then return 0
  497. * (failure). Otherwise, return 1
  498. */
  499. static int init_phy(struct tsec_private *priv)
  500. {
  501. struct phy_device *phydev;
  502. struct tsec __iomem *regs = priv->regs;
  503. u32 supported = (SUPPORTED_10baseT_Half |
  504. SUPPORTED_10baseT_Full |
  505. SUPPORTED_100baseT_Half |
  506. SUPPORTED_100baseT_Full);
  507. if (priv->flags & TSEC_GIGABIT)
  508. supported |= SUPPORTED_1000baseT_Full;
  509. /* Assign a Physical address to the TBI */
  510. out_be32(&regs->tbipa, priv->tbiaddr);
  511. if (priv->interface == PHY_INTERFACE_MODE_SGMII)
  512. tsec_configure_serdes(priv);
  513. #if defined(CONFIG_DM_MDIO)
  514. phydev = dm_eth_phy_connect(priv->dev);
  515. #else
  516. phydev = phy_connect(priv->bus, priv->phyaddr, priv->dev,
  517. priv->interface);
  518. #endif
  519. if (!phydev)
  520. return 0;
  521. phydev->supported &= supported;
  522. phydev->advertising = phydev->supported;
  523. priv->phydev = phydev;
  524. phy_config(phydev);
  525. return 1;
  526. }
  527. int tsec_probe(struct udevice *dev)
  528. {
  529. struct eth_pdata *pdata = dev_get_plat(dev);
  530. struct tsec_private *priv = dev_get_priv(dev);
  531. struct ofnode_phandle_args phandle_args;
  532. u32 tbiaddr = CFG_SYS_TBIPA_VALUE;
  533. struct tsec_data *data;
  534. ofnode parent, child;
  535. fdt_addr_t reg;
  536. u32 max_speed;
  537. int ret;
  538. data = (struct tsec_data *)dev_get_driver_data(dev);
  539. pdata->iobase = (phys_addr_t)dev_read_addr(dev);
  540. if (pdata->iobase == FDT_ADDR_T_NONE) {
  541. ofnode_for_each_subnode(child, dev_ofnode(dev)) {
  542. if (strncmp(ofnode_get_name(child), "queue-group",
  543. strlen("queue-group")))
  544. continue;
  545. reg = ofnode_get_addr(child);
  546. if (reg == FDT_ADDR_T_NONE) {
  547. printf("No 'reg' property of <queue-group>\n");
  548. return -ENOENT;
  549. }
  550. pdata->iobase = reg;
  551. /*
  552. * if there are multiple queue groups,
  553. * only the first one is used.
  554. */
  555. break;
  556. }
  557. if (!ofnode_valid(child)) {
  558. printf("No child node for <queue-group>?\n");
  559. return -ENOENT;
  560. }
  561. }
  562. priv->regs = map_physmem(pdata->iobase, 0, MAP_NOCACHE);
  563. ret = dev_read_phandle_with_args(dev, "tbi-handle", NULL, 0, 0,
  564. &phandle_args);
  565. if (ret == 0) {
  566. ofnode_read_u32(phandle_args.node, "reg", &tbiaddr);
  567. parent = ofnode_get_parent(phandle_args.node);
  568. if (!ofnode_valid(parent)) {
  569. printf("No parent node for TBI PHY?\n");
  570. return -ENOENT;
  571. }
  572. reg = ofnode_get_addr_index(parent, 0);
  573. if (reg == FDT_ADDR_T_NONE) {
  574. printf("No 'reg' property of MII for TBI PHY\n");
  575. return -ENOENT;
  576. }
  577. priv->phyregs_sgmii = map_physmem(reg + data->mdio_regs_off,
  578. 0, MAP_NOCACHE);
  579. }
  580. priv->tbiaddr = tbiaddr;
  581. pdata->phy_interface = dev_read_phy_mode(dev);
  582. if (pdata->phy_interface == PHY_INTERFACE_MODE_NA)
  583. pdata->phy_interface = tsec_get_interface(priv);
  584. priv->interface = pdata->phy_interface;
  585. /* Check for speed limit, default is 1000Mbps */
  586. max_speed = dev_read_u32_default(dev, "max-speed", 1000);
  587. /* Initialize flags */
  588. if (max_speed == 1000)
  589. priv->flags = TSEC_GIGABIT;
  590. if (priv->interface == PHY_INTERFACE_MODE_SGMII)
  591. priv->flags |= TSEC_SGMII;
  592. /* Reset the MAC */
  593. setbits_be32(&priv->regs->maccfg1, MACCFG1_SOFT_RESET);
  594. udelay(2); /* Soft Reset must be asserted for 3 TX clocks */
  595. clrbits_be32(&priv->regs->maccfg1, MACCFG1_SOFT_RESET);
  596. priv->dev = dev;
  597. priv->bus = miiphy_get_dev_by_name(dev->name);
  598. /* Try to initialize PHY here, and return */
  599. return !init_phy(priv);
  600. }
  601. int tsec_remove(struct udevice *dev)
  602. {
  603. struct tsec_private *priv = dev_get_priv(dev);
  604. free(priv->phydev);
  605. mdio_unregister(priv->bus);
  606. mdio_free(priv->bus);
  607. return 0;
  608. }
  609. static const struct eth_ops tsec_ops = {
  610. .start = tsec_init,
  611. .send = tsec_send,
  612. .recv = tsec_recv,
  613. .free_pkt = tsec_free_pkt,
  614. .stop = tsec_halt,
  615. .mcast = tsec_mcast_addr,
  616. .set_promisc = tsec_set_promisc,
  617. };
  618. static struct tsec_data etsec2_data = {
  619. .mdio_regs_off = TSEC_MDIO_REGS_OFFSET,
  620. };
  621. static struct tsec_data gianfar_data = {
  622. .mdio_regs_off = 0x0,
  623. };
  624. static const struct udevice_id tsec_ids[] = {
  625. { .compatible = "fsl,etsec2", .data = (ulong)&etsec2_data },
  626. { .compatible = "gianfar", .data = (ulong)&gianfar_data },
  627. { }
  628. };
  629. U_BOOT_DRIVER(eth_tsec) = {
  630. .name = "tsec",
  631. .id = UCLASS_ETH,
  632. .of_match = tsec_ids,
  633. .probe = tsec_probe,
  634. .remove = tsec_remove,
  635. .ops = &tsec_ops,
  636. .priv_auto = sizeof(struct tsec_private),
  637. .plat_auto = sizeof(struct eth_pdata),
  638. .flags = DM_FLAG_ALLOC_PRIV_DMA,
  639. };