tsec.c 18 KB

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