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