tsec.c 15 KB

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