tsec.c 23 KB

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