eth.c 23 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904
  1. // SPDX-License-Identifier: GPL-2.0+
  2. /*
  3. * Copyright 2009-2012 Freescale Semiconductor, Inc.
  4. * Copyright 2020 NXP
  5. * Dave Liu <daveliu@freescale.com>
  6. */
  7. #include <common.h>
  8. #include <log.h>
  9. #include <part.h>
  10. #include <asm/io.h>
  11. #include <dm.h>
  12. #include <dm/ofnode.h>
  13. #include <linux/compat.h>
  14. #include <phy_interface.h>
  15. #include <malloc.h>
  16. #include <net.h>
  17. #include <hwconfig.h>
  18. #include <fm_eth.h>
  19. #include <fsl_mdio.h>
  20. #include <miiphy.h>
  21. #include <phy.h>
  22. #include <fsl_dtsec.h>
  23. #include <fsl_tgec.h>
  24. #include <fsl_memac.h>
  25. #include <linux/delay.h>
  26. #include "fm.h"
  27. #if defined(CONFIG_MII) || defined(CONFIG_CMD_MII) && !defined(BITBANGMII)
  28. #define TBIANA_SETTINGS (TBIANA_ASYMMETRIC_PAUSE | TBIANA_SYMMETRIC_PAUSE | \
  29. TBIANA_FULL_DUPLEX)
  30. #define TBIANA_SGMII_ACK 0x4001
  31. #define TBICR_SETTINGS (TBICR_ANEG_ENABLE | TBICR_RESTART_ANEG | \
  32. TBICR_FULL_DUPLEX | TBICR_SPEED1_SET)
  33. /* Configure the TBI for SGMII operation */
  34. static void dtsec_configure_serdes(struct fm_eth *priv)
  35. {
  36. #ifdef CONFIG_SYS_FMAN_V3
  37. u32 value;
  38. struct mii_dev bus;
  39. bool sgmii_2500 = (priv->enet_if ==
  40. PHY_INTERFACE_MODE_2500BASEX) ? true : false;
  41. int i = 0, j;
  42. bus.priv = priv->pcs_mdio;
  43. bus.read = memac_mdio_read;
  44. bus.write = memac_mdio_write;
  45. bus.reset = memac_mdio_reset;
  46. qsgmii_loop:
  47. /* SGMII IF mode + AN enable only for 1G SGMII, not for 2.5G */
  48. if (sgmii_2500)
  49. value = PHY_SGMII_CR_PHY_RESET |
  50. PHY_SGMII_IF_SPEED_GIGABIT |
  51. PHY_SGMII_IF_MODE_SGMII;
  52. else
  53. value = PHY_SGMII_IF_MODE_SGMII | PHY_SGMII_IF_MODE_AN;
  54. for (j = 0; j <= 3; j++)
  55. debug("dump PCS reg %#x: %#x\n", j,
  56. memac_mdio_read(&bus, i, MDIO_DEVAD_NONE, j));
  57. memac_mdio_write(&bus, i, MDIO_DEVAD_NONE, 0x14, value);
  58. /* Dev ability according to SGMII specification */
  59. value = PHY_SGMII_DEV_ABILITY_SGMII;
  60. memac_mdio_write(&bus, i, MDIO_DEVAD_NONE, 0x4, value);
  61. if (sgmii_2500) {
  62. /* Adjust link timer for 2.5G SGMII,
  63. * 1.6 ms in units of 3.2 ns:
  64. * 1.6ms / 3.2ns = 5 * 10^5 = 0x7a120.
  65. */
  66. memac_mdio_write(&bus, i, MDIO_DEVAD_NONE, 0x13, 0x0007);
  67. memac_mdio_write(&bus, i, MDIO_DEVAD_NONE, 0x12, 0xa120);
  68. } else {
  69. /* Adjust link timer for SGMII,
  70. * 1.6 ms in units of 8 ns:
  71. * 1.6ms / 8ns = 2 * 10^5 = 0x30d40.
  72. */
  73. memac_mdio_write(&bus, i, MDIO_DEVAD_NONE, 0x13, 0x0003);
  74. memac_mdio_write(&bus, i, MDIO_DEVAD_NONE, 0x12, 0x0d40);
  75. }
  76. /* Restart AN */
  77. value = PHY_SGMII_CR_DEF_VAL | PHY_SGMII_CR_RESET_AN;
  78. memac_mdio_write(&bus, i, MDIO_DEVAD_NONE, 0, value);
  79. if ((priv->enet_if == PHY_INTERFACE_MODE_QSGMII) && (i < 3)) {
  80. i++;
  81. goto qsgmii_loop;
  82. }
  83. #else
  84. struct dtsec *regs = priv->mac->base;
  85. struct tsec_mii_mng *phyregs = priv->mac->phyregs;
  86. /*
  87. * Access TBI PHY registers at given TSEC register offset as
  88. * opposed to the register offset used for external PHY accesses
  89. */
  90. tsec_local_mdio_write(phyregs, in_be32(&regs->tbipa), 0, TBI_TBICON,
  91. TBICON_CLK_SELECT);
  92. tsec_local_mdio_write(phyregs, in_be32(&regs->tbipa), 0, TBI_ANA,
  93. TBIANA_SGMII_ACK);
  94. tsec_local_mdio_write(phyregs, in_be32(&regs->tbipa), 0,
  95. TBI_CR, TBICR_SETTINGS);
  96. #endif
  97. }
  98. static void dtsec_init_phy(struct fm_eth *fm_eth)
  99. {
  100. #ifndef CONFIG_SYS_FMAN_V3
  101. struct dtsec *regs = (struct dtsec *)CFG_SYS_FSL_FM1_DTSEC1_ADDR;
  102. /* Assign a Physical address to the TBI */
  103. out_be32(&regs->tbipa, CFG_SYS_TBIPA_VALUE);
  104. #endif
  105. if (fm_eth->enet_if == PHY_INTERFACE_MODE_SGMII ||
  106. fm_eth->enet_if == PHY_INTERFACE_MODE_QSGMII ||
  107. fm_eth->enet_if == PHY_INTERFACE_MODE_2500BASEX)
  108. dtsec_configure_serdes(fm_eth);
  109. }
  110. #endif
  111. static u16 muram_readw(u16 *addr)
  112. {
  113. ulong base = (ulong)addr & ~0x3UL;
  114. u32 val32 = in_be32((void *)base);
  115. int byte_pos;
  116. u16 ret;
  117. byte_pos = (ulong)addr & 0x3UL;
  118. if (byte_pos)
  119. ret = (u16)(val32 & 0x0000ffff);
  120. else
  121. ret = (u16)((val32 & 0xffff0000) >> 16);
  122. return ret;
  123. }
  124. static void muram_writew(u16 *addr, u16 val)
  125. {
  126. ulong base = (ulong)addr & ~0x3UL;
  127. u32 org32 = in_be32((void *)base);
  128. u32 val32;
  129. int byte_pos;
  130. byte_pos = (ulong)addr & 0x3UL;
  131. if (byte_pos)
  132. val32 = (org32 & 0xffff0000) | val;
  133. else
  134. val32 = (org32 & 0x0000ffff) | ((u32)val << 16);
  135. out_be32((void *)base, val32);
  136. }
  137. static void bmi_rx_port_disable(struct fm_bmi_rx_port *rx_port)
  138. {
  139. int timeout = 1000000;
  140. clrbits_be32(&rx_port->fmbm_rcfg, FMBM_RCFG_EN);
  141. /* wait until the rx port is not busy */
  142. while ((in_be32(&rx_port->fmbm_rst) & FMBM_RST_BSY) && timeout--)
  143. ;
  144. if (!timeout)
  145. printf("%s - timeout\n", __func__);
  146. }
  147. static void bmi_rx_port_init(struct fm_bmi_rx_port *rx_port)
  148. {
  149. /* set BMI to independent mode, Rx port disable */
  150. out_be32(&rx_port->fmbm_rcfg, FMBM_RCFG_IM);
  151. /* clear FOF in IM case */
  152. out_be32(&rx_port->fmbm_rim, 0);
  153. /* Rx frame next engine -RISC */
  154. out_be32(&rx_port->fmbm_rfne, NIA_ENG_RISC | NIA_RISC_AC_IM_RX);
  155. /* Rx command attribute - no order, MR[3] = 1 */
  156. clrbits_be32(&rx_port->fmbm_rfca, FMBM_RFCA_ORDER | FMBM_RFCA_MR_MASK);
  157. setbits_be32(&rx_port->fmbm_rfca, FMBM_RFCA_MR(4));
  158. /* enable Rx statistic counters */
  159. out_be32(&rx_port->fmbm_rstc, FMBM_RSTC_EN);
  160. /* disable Rx performance counters */
  161. out_be32(&rx_port->fmbm_rpc, 0);
  162. }
  163. static void bmi_tx_port_disable(struct fm_bmi_tx_port *tx_port)
  164. {
  165. int timeout = 1000000;
  166. clrbits_be32(&tx_port->fmbm_tcfg, FMBM_TCFG_EN);
  167. /* wait until the tx port is not busy */
  168. while ((in_be32(&tx_port->fmbm_tst) & FMBM_TST_BSY) && timeout--)
  169. ;
  170. if (!timeout)
  171. printf("%s - timeout\n", __func__);
  172. }
  173. static void bmi_tx_port_init(struct fm_bmi_tx_port *tx_port)
  174. {
  175. /* set BMI to independent mode, Tx port disable */
  176. out_be32(&tx_port->fmbm_tcfg, FMBM_TCFG_IM);
  177. /* Tx frame next engine -RISC */
  178. out_be32(&tx_port->fmbm_tfne, NIA_ENG_RISC | NIA_RISC_AC_IM_TX);
  179. out_be32(&tx_port->fmbm_tfene, NIA_ENG_RISC | NIA_RISC_AC_IM_TX);
  180. /* Tx command attribute - no order, MR[3] = 1 */
  181. clrbits_be32(&tx_port->fmbm_tfca, FMBM_TFCA_ORDER | FMBM_TFCA_MR_MASK);
  182. setbits_be32(&tx_port->fmbm_tfca, FMBM_TFCA_MR(4));
  183. /* enable Tx statistic counters */
  184. out_be32(&tx_port->fmbm_tstc, FMBM_TSTC_EN);
  185. /* disable Tx performance counters */
  186. out_be32(&tx_port->fmbm_tpc, 0);
  187. }
  188. static int fm_eth_rx_port_parameter_init(struct fm_eth *fm_eth)
  189. {
  190. struct fm_port_global_pram *pram;
  191. u32 pram_page_offset;
  192. void *rx_bd_ring_base;
  193. void *rx_buf_pool;
  194. u32 bd_ring_base_lo, bd_ring_base_hi;
  195. u32 buf_lo, buf_hi;
  196. struct fm_port_bd *rxbd;
  197. struct fm_port_qd *rxqd;
  198. struct fm_bmi_rx_port *bmi_rx_port = fm_eth->rx_port;
  199. int i;
  200. /* alloc global parameter ram at MURAM */
  201. pram = (struct fm_port_global_pram *)fm_muram_alloc(fm_eth->fm_index,
  202. FM_PRAM_SIZE, FM_PRAM_ALIGN);
  203. if (!pram) {
  204. printf("%s: No muram for Rx global parameter\n", __func__);
  205. return -ENOMEM;
  206. }
  207. fm_eth->rx_pram = pram;
  208. /* parameter page offset to MURAM */
  209. pram_page_offset = (void *)pram - fm_muram_base(fm_eth->fm_index);
  210. /* enable global mode- snooping data buffers and BDs */
  211. out_be32(&pram->mode, PRAM_MODE_GLOBAL);
  212. /* init the Rx queue descriptor pionter */
  213. out_be32(&pram->rxqd_ptr, pram_page_offset + 0x20);
  214. /* set the max receive buffer length, power of 2 */
  215. muram_writew(&pram->mrblr, MAX_RXBUF_LOG2);
  216. /* alloc Rx buffer descriptors from main memory */
  217. rx_bd_ring_base = malloc(sizeof(struct fm_port_bd)
  218. * RX_BD_RING_SIZE);
  219. if (!rx_bd_ring_base)
  220. return -ENOMEM;
  221. memset(rx_bd_ring_base, 0, sizeof(struct fm_port_bd)
  222. * RX_BD_RING_SIZE);
  223. /* alloc Rx buffer from main memory */
  224. rx_buf_pool = malloc(MAX_RXBUF_LEN * RX_BD_RING_SIZE);
  225. if (!rx_buf_pool) {
  226. free(rx_bd_ring_base);
  227. return -ENOMEM;
  228. }
  229. memset(rx_buf_pool, 0, MAX_RXBUF_LEN * RX_BD_RING_SIZE);
  230. debug("%s: rx_buf_pool = %p\n", __func__, rx_buf_pool);
  231. /* save them to fm_eth */
  232. fm_eth->rx_bd_ring = rx_bd_ring_base;
  233. fm_eth->cur_rxbd = rx_bd_ring_base;
  234. fm_eth->rx_buf = rx_buf_pool;
  235. /* init Rx BDs ring */
  236. rxbd = (struct fm_port_bd *)rx_bd_ring_base;
  237. for (i = 0; i < RX_BD_RING_SIZE; i++) {
  238. muram_writew(&rxbd->status, RxBD_EMPTY);
  239. muram_writew(&rxbd->len, 0);
  240. buf_hi = upper_32_bits(virt_to_phys(rx_buf_pool +
  241. i * MAX_RXBUF_LEN));
  242. buf_lo = lower_32_bits(virt_to_phys(rx_buf_pool +
  243. i * MAX_RXBUF_LEN));
  244. muram_writew(&rxbd->buf_ptr_hi, (u16)buf_hi);
  245. out_be32(&rxbd->buf_ptr_lo, buf_lo);
  246. rxbd++;
  247. }
  248. /* set the Rx queue descriptor */
  249. rxqd = &pram->rxqd;
  250. muram_writew(&rxqd->gen, 0);
  251. bd_ring_base_hi = upper_32_bits(virt_to_phys(rx_bd_ring_base));
  252. bd_ring_base_lo = lower_32_bits(virt_to_phys(rx_bd_ring_base));
  253. muram_writew(&rxqd->bd_ring_base_hi, (u16)bd_ring_base_hi);
  254. out_be32(&rxqd->bd_ring_base_lo, bd_ring_base_lo);
  255. muram_writew(&rxqd->bd_ring_size, sizeof(struct fm_port_bd)
  256. * RX_BD_RING_SIZE);
  257. muram_writew(&rxqd->offset_in, 0);
  258. muram_writew(&rxqd->offset_out, 0);
  259. /* set IM parameter ram pointer to Rx Frame Queue ID */
  260. out_be32(&bmi_rx_port->fmbm_rfqid, pram_page_offset);
  261. return 0;
  262. }
  263. static int fm_eth_tx_port_parameter_init(struct fm_eth *fm_eth)
  264. {
  265. struct fm_port_global_pram *pram;
  266. u32 pram_page_offset;
  267. void *tx_bd_ring_base;
  268. u32 bd_ring_base_lo, bd_ring_base_hi;
  269. struct fm_port_bd *txbd;
  270. struct fm_port_qd *txqd;
  271. struct fm_bmi_tx_port *bmi_tx_port = fm_eth->tx_port;
  272. int i;
  273. /* alloc global parameter ram at MURAM */
  274. pram = (struct fm_port_global_pram *)fm_muram_alloc(fm_eth->fm_index,
  275. FM_PRAM_SIZE, FM_PRAM_ALIGN);
  276. if (!pram) {
  277. printf("%s: No muram for Tx global parameter\n", __func__);
  278. return -ENOMEM;
  279. }
  280. fm_eth->tx_pram = pram;
  281. /* parameter page offset to MURAM */
  282. pram_page_offset = (void *)pram - fm_muram_base(fm_eth->fm_index);
  283. /* enable global mode- snooping data buffers and BDs */
  284. out_be32(&pram->mode, PRAM_MODE_GLOBAL);
  285. /* init the Tx queue descriptor pionter */
  286. out_be32(&pram->txqd_ptr, pram_page_offset + 0x40);
  287. /* alloc Tx buffer descriptors from main memory */
  288. tx_bd_ring_base = malloc(sizeof(struct fm_port_bd)
  289. * TX_BD_RING_SIZE);
  290. if (!tx_bd_ring_base)
  291. return -ENOMEM;
  292. memset(tx_bd_ring_base, 0, sizeof(struct fm_port_bd)
  293. * TX_BD_RING_SIZE);
  294. /* save it to fm_eth */
  295. fm_eth->tx_bd_ring = tx_bd_ring_base;
  296. fm_eth->cur_txbd = tx_bd_ring_base;
  297. /* init Tx BDs ring */
  298. txbd = (struct fm_port_bd *)tx_bd_ring_base;
  299. for (i = 0; i < TX_BD_RING_SIZE; i++) {
  300. muram_writew(&txbd->status, TxBD_LAST);
  301. muram_writew(&txbd->len, 0);
  302. muram_writew(&txbd->buf_ptr_hi, 0);
  303. out_be32(&txbd->buf_ptr_lo, 0);
  304. txbd++;
  305. }
  306. /* set the Tx queue decriptor */
  307. txqd = &pram->txqd;
  308. bd_ring_base_hi = upper_32_bits(virt_to_phys(tx_bd_ring_base));
  309. bd_ring_base_lo = lower_32_bits(virt_to_phys(tx_bd_ring_base));
  310. muram_writew(&txqd->bd_ring_base_hi, (u16)bd_ring_base_hi);
  311. out_be32(&txqd->bd_ring_base_lo, bd_ring_base_lo);
  312. muram_writew(&txqd->bd_ring_size, sizeof(struct fm_port_bd)
  313. * TX_BD_RING_SIZE);
  314. muram_writew(&txqd->offset_in, 0);
  315. muram_writew(&txqd->offset_out, 0);
  316. /* set IM parameter ram pointer to Tx Confirmation Frame Queue ID */
  317. out_be32(&bmi_tx_port->fmbm_tcfqid, pram_page_offset);
  318. return 0;
  319. }
  320. static int fm_eth_init(struct fm_eth *fm_eth)
  321. {
  322. int ret;
  323. ret = fm_eth_rx_port_parameter_init(fm_eth);
  324. if (ret)
  325. return ret;
  326. ret = fm_eth_tx_port_parameter_init(fm_eth);
  327. if (ret)
  328. return ret;
  329. return 0;
  330. }
  331. static int fm_eth_startup(struct fm_eth *fm_eth)
  332. {
  333. struct fsl_enet_mac *mac;
  334. int ret;
  335. mac = fm_eth->mac;
  336. /* Rx/TxBDs, Rx/TxQDs, Rx buff and parameter ram init */
  337. ret = fm_eth_init(fm_eth);
  338. if (ret)
  339. return ret;
  340. /* setup the MAC controller */
  341. mac->init_mac(mac);
  342. /* For some reason we need to set SPEED_100 */
  343. if (((fm_eth->enet_if == PHY_INTERFACE_MODE_SGMII) ||
  344. (fm_eth->enet_if == PHY_INTERFACE_MODE_2500BASEX) ||
  345. (fm_eth->enet_if == PHY_INTERFACE_MODE_QSGMII)) &&
  346. mac->set_if_mode)
  347. mac->set_if_mode(mac, fm_eth->enet_if, SPEED_100);
  348. /* init bmi rx port, IM mode and disable */
  349. bmi_rx_port_init(fm_eth->rx_port);
  350. /* init bmi tx port, IM mode and disable */
  351. bmi_tx_port_init(fm_eth->tx_port);
  352. return 0;
  353. }
  354. static void fmc_tx_port_graceful_stop_enable(struct fm_eth *fm_eth)
  355. {
  356. struct fm_port_global_pram *pram;
  357. pram = fm_eth->tx_pram;
  358. /* graceful stop transmission of frames */
  359. setbits_be32(&pram->mode, PRAM_MODE_GRACEFUL_STOP);
  360. sync();
  361. }
  362. static void fmc_tx_port_graceful_stop_disable(struct fm_eth *fm_eth)
  363. {
  364. struct fm_port_global_pram *pram;
  365. pram = fm_eth->tx_pram;
  366. /* re-enable transmission of frames */
  367. clrbits_be32(&pram->mode, PRAM_MODE_GRACEFUL_STOP);
  368. sync();
  369. }
  370. static int fm_eth_open(struct udevice *dev)
  371. {
  372. struct eth_pdata *pdata = dev_get_plat(dev);
  373. struct fm_eth *fm_eth = dev_get_priv(dev);
  374. unsigned char *enetaddr;
  375. struct fsl_enet_mac *mac;
  376. #ifdef CONFIG_PHYLIB
  377. int ret;
  378. #endif
  379. mac = fm_eth->mac;
  380. enetaddr = pdata->enetaddr;
  381. /* setup the MAC address */
  382. if (enetaddr[0] & 0x01) {
  383. printf("%s: MacAddress is multicast address\n", __func__);
  384. enetaddr[0] = 0;
  385. enetaddr[5] = fm_eth->num;
  386. }
  387. mac->set_mac_addr(mac, enetaddr);
  388. /* enable bmi Rx port */
  389. setbits_be32(&fm_eth->rx_port->fmbm_rcfg, FMBM_RCFG_EN);
  390. /* enable MAC rx/tx port */
  391. mac->enable_mac(mac);
  392. /* enable bmi Tx port */
  393. setbits_be32(&fm_eth->tx_port->fmbm_tcfg, FMBM_TCFG_EN);
  394. /* re-enable transmission of frame */
  395. fmc_tx_port_graceful_stop_disable(fm_eth);
  396. #ifdef CONFIG_PHYLIB
  397. if (fm_eth->phydev) {
  398. ret = phy_startup(fm_eth->phydev);
  399. if (ret) {
  400. printf("%s: Could not initialize\n", dev->name);
  401. return ret;
  402. }
  403. } else {
  404. return 0;
  405. }
  406. #else
  407. fm_eth->phydev->speed = SPEED_1000;
  408. fm_eth->phydev->link = 1;
  409. fm_eth->phydev->duplex = DUPLEX_FULL;
  410. #endif
  411. /* set the MAC-PHY mode */
  412. mac->set_if_mode(mac, fm_eth->enet_if, fm_eth->phydev->speed);
  413. debug("MAC IF mode %d, speed %d, link %d\n", fm_eth->enet_if,
  414. fm_eth->phydev->speed, fm_eth->phydev->link);
  415. if (!fm_eth->phydev->link)
  416. printf("%s: No link.\n", fm_eth->phydev->dev->name);
  417. return fm_eth->phydev->link ? 0 : -1;
  418. }
  419. static void fm_eth_halt(struct udevice *dev)
  420. {
  421. struct fm_eth *fm_eth;
  422. struct fsl_enet_mac *mac;
  423. fm_eth = dev_get_priv(dev);
  424. mac = fm_eth->mac;
  425. /* graceful stop the transmission of frames */
  426. fmc_tx_port_graceful_stop_enable(fm_eth);
  427. /* disable bmi Tx port */
  428. bmi_tx_port_disable(fm_eth->tx_port);
  429. /* disable MAC rx/tx port */
  430. mac->disable_mac(mac);
  431. /* disable bmi Rx port */
  432. bmi_rx_port_disable(fm_eth->rx_port);
  433. #ifdef CONFIG_PHYLIB
  434. if (fm_eth->phydev)
  435. phy_shutdown(fm_eth->phydev);
  436. #endif
  437. }
  438. static int fm_eth_send(struct udevice *dev, void *buf, int len)
  439. {
  440. struct fm_eth *fm_eth;
  441. struct fm_port_global_pram *pram;
  442. struct fm_port_bd *txbd, *txbd_base;
  443. u16 offset_in;
  444. int i;
  445. fm_eth = dev_get_priv(dev);
  446. pram = fm_eth->tx_pram;
  447. txbd = fm_eth->cur_txbd;
  448. /* find one empty TxBD */
  449. for (i = 0; muram_readw(&txbd->status) & TxBD_READY; i++) {
  450. udelay(100);
  451. if (i > 0x1000) {
  452. printf("%s: Tx buffer not ready, txbd->status = 0x%x\n",
  453. dev->name, muram_readw(&txbd->status));
  454. return 0;
  455. }
  456. }
  457. /* setup TxBD */
  458. muram_writew(&txbd->buf_ptr_hi, (u16)upper_32_bits(virt_to_phys(buf)));
  459. out_be32(&txbd->buf_ptr_lo, lower_32_bits(virt_to_phys(buf)));
  460. muram_writew(&txbd->len, len);
  461. sync();
  462. muram_writew(&txbd->status, TxBD_READY | TxBD_LAST);
  463. sync();
  464. /* update TxQD, let RISC to send the packet */
  465. offset_in = muram_readw(&pram->txqd.offset_in);
  466. offset_in += sizeof(struct fm_port_bd);
  467. if (offset_in >= muram_readw(&pram->txqd.bd_ring_size))
  468. offset_in = 0;
  469. muram_writew(&pram->txqd.offset_in, offset_in);
  470. sync();
  471. /* wait for buffer to be transmitted */
  472. for (i = 0; muram_readw(&txbd->status) & TxBD_READY; i++) {
  473. udelay(100);
  474. if (i > 0x10000) {
  475. printf("%s: Tx error, txbd->status = 0x%x\n",
  476. dev->name, muram_readw(&txbd->status));
  477. return 0;
  478. }
  479. }
  480. /* advance the TxBD */
  481. txbd++;
  482. txbd_base = (struct fm_port_bd *)fm_eth->tx_bd_ring;
  483. if (txbd >= (txbd_base + TX_BD_RING_SIZE))
  484. txbd = txbd_base;
  485. /* update current txbd */
  486. fm_eth->cur_txbd = (void *)txbd;
  487. return 1;
  488. }
  489. static struct fm_port_bd *fm_eth_free_one(struct fm_eth *fm_eth,
  490. struct fm_port_bd *rxbd)
  491. {
  492. struct fm_port_global_pram *pram;
  493. struct fm_port_bd *rxbd_base;
  494. u16 offset_out;
  495. pram = fm_eth->rx_pram;
  496. /* clear the RxBDs */
  497. muram_writew(&rxbd->status, RxBD_EMPTY);
  498. muram_writew(&rxbd->len, 0);
  499. sync();
  500. /* advance RxBD */
  501. rxbd++;
  502. rxbd_base = (struct fm_port_bd *)fm_eth->rx_bd_ring;
  503. if (rxbd >= (rxbd_base + RX_BD_RING_SIZE))
  504. rxbd = rxbd_base;
  505. /* update RxQD */
  506. offset_out = muram_readw(&pram->rxqd.offset_out);
  507. offset_out += sizeof(struct fm_port_bd);
  508. if (offset_out >= muram_readw(&pram->rxqd.bd_ring_size))
  509. offset_out = 0;
  510. muram_writew(&pram->rxqd.offset_out, offset_out);
  511. sync();
  512. return rxbd;
  513. }
  514. static int fm_eth_recv(struct udevice *dev, int flags, uchar **packetp)
  515. {
  516. struct fm_eth *fm_eth;
  517. struct fm_port_bd *rxbd;
  518. u32 buf_lo, buf_hi;
  519. u16 status, len;
  520. int ret = -1;
  521. u8 *data;
  522. fm_eth = dev_get_priv(dev);
  523. rxbd = fm_eth->cur_rxbd;
  524. status = muram_readw(&rxbd->status);
  525. while (!(status & RxBD_EMPTY)) {
  526. if (!(status & RxBD_ERROR)) {
  527. buf_hi = muram_readw(&rxbd->buf_ptr_hi);
  528. buf_lo = in_be32(&rxbd->buf_ptr_lo);
  529. data = (u8 *)((ulong)(buf_hi << 16) << 16 | buf_lo);
  530. len = muram_readw(&rxbd->len);
  531. *packetp = data;
  532. return len;
  533. } else {
  534. printf("%s: Rx error\n", dev->name);
  535. ret = 0;
  536. }
  537. /* free current bd, advance to next one */
  538. rxbd = fm_eth_free_one(fm_eth, rxbd);
  539. /* read next status */
  540. status = muram_readw(&rxbd->status);
  541. }
  542. fm_eth->cur_rxbd = (void *)rxbd;
  543. return ret;
  544. }
  545. static int fm_eth_free_pkt(struct udevice *dev, uchar *packet, int length)
  546. {
  547. struct fm_eth *fm_eth = (struct fm_eth *)dev_get_priv(dev);
  548. fm_eth->cur_rxbd = fm_eth_free_one(fm_eth, fm_eth->cur_rxbd);
  549. return 0;
  550. }
  551. static int fm_eth_init_mac(struct fm_eth *fm_eth, void *reg)
  552. {
  553. #ifndef CONFIG_SYS_FMAN_V3
  554. void *mdio;
  555. #endif
  556. fm_eth->mac = kzalloc(sizeof(*fm_eth->mac), GFP_KERNEL);
  557. if (!fm_eth->mac)
  558. return -ENOMEM;
  559. #ifndef CONFIG_SYS_FMAN_V3
  560. mdio = fman_mdio(fm_eth->dev->parent, fm_eth->mac_type, fm_eth->num);
  561. debug("MDIO %d @ %p\n", fm_eth->num, mdio);
  562. #endif
  563. switch (fm_eth->mac_type) {
  564. #ifdef CONFIG_SYS_FMAN_V3
  565. case FM_MEMAC:
  566. init_memac(fm_eth->mac, reg, NULL, MAX_RXBUF_LEN);
  567. break;
  568. #else
  569. case FM_DTSEC:
  570. init_dtsec(fm_eth->mac, reg, mdio, MAX_RXBUF_LEN);
  571. break;
  572. case FM_TGEC:
  573. init_tgec(fm_eth->mac, reg, mdio, MAX_RXBUF_LEN);
  574. break;
  575. #endif
  576. }
  577. return 0;
  578. }
  579. static int init_phy(struct fm_eth *fm_eth)
  580. {
  581. #ifdef CONFIG_PHYLIB
  582. u32 supported = PHY_GBIT_FEATURES;
  583. if (fm_eth->type == FM_ETH_10G_E)
  584. supported = PHY_10G_FEATURES;
  585. if (fm_eth->enet_if == PHY_INTERFACE_MODE_2500BASEX)
  586. supported |= SUPPORTED_2500baseX_Full;
  587. #endif
  588. if (fm_eth->type == FM_ETH_1G_E)
  589. dtsec_init_phy(fm_eth);
  590. #ifdef CONFIG_PHYLIB
  591. #ifdef CONFIG_DM_MDIO
  592. fm_eth->phydev = dm_eth_phy_connect(fm_eth->dev);
  593. if (!fm_eth->phydev)
  594. return -ENODEV;
  595. #endif
  596. fm_eth->phydev->advertising &= supported;
  597. fm_eth->phydev->supported &= supported;
  598. phy_config(fm_eth->phydev);
  599. #endif
  600. return 0;
  601. }
  602. static int fm_eth_bind(struct udevice *dev)
  603. {
  604. char mac_name[11];
  605. u32 fm, num;
  606. if (ofnode_read_u32(ofnode_get_parent(dev_ofnode(dev)), "cell-index", &fm)) {
  607. printf("FMan node property cell-index missing\n");
  608. return -EINVAL;
  609. }
  610. if (dev && dev_read_u32(dev, "cell-index", &num)) {
  611. printf("FMan MAC node property cell-index missing\n");
  612. return -EINVAL;
  613. }
  614. sprintf(mac_name, "fm%d-mac%d", fm + 1, num + 1);
  615. device_set_name(dev, mac_name);
  616. debug("%s - binding %s\n", __func__, mac_name);
  617. return 0;
  618. }
  619. static struct udevice *fm_get_internal_mdio(struct udevice *dev)
  620. {
  621. struct ofnode_phandle_args phandle = {.node = ofnode_null()};
  622. struct udevice *mdiodev;
  623. if (dev_read_phandle_with_args(dev, "pcsphy-handle", NULL,
  624. 0, 0, &phandle) ||
  625. !ofnode_valid(phandle.node)) {
  626. if (dev_read_phandle_with_args(dev, "tbi-handle", NULL,
  627. 0, 0, &phandle) ||
  628. !ofnode_valid(phandle.node)) {
  629. printf("Issue reading pcsphy-handle/tbi-handle for MAC %s\n",
  630. dev->name);
  631. return NULL;
  632. }
  633. }
  634. if (uclass_get_device_by_ofnode(UCLASS_MDIO,
  635. ofnode_get_parent(phandle.node),
  636. &mdiodev)) {
  637. printf("can't find MDIO bus for node %s\n",
  638. ofnode_get_name(ofnode_get_parent(phandle.node)));
  639. return NULL;
  640. }
  641. debug("Found internal MDIO bus %p\n", mdiodev);
  642. return mdiodev;
  643. }
  644. static int fm_eth_probe(struct udevice *dev)
  645. {
  646. struct fm_eth *fm_eth = (struct fm_eth *)dev_get_priv(dev);
  647. struct ofnode_phandle_args args;
  648. void *reg;
  649. int ret, index;
  650. debug("%s enter for dev %p fm_eth %p - %s\n", __func__, dev, fm_eth,
  651. (dev) ? dev->name : "-");
  652. if (fm_eth->dev) {
  653. printf("%s already probed, exit\n", (dev) ? dev->name : "-");
  654. return 0;
  655. }
  656. fm_eth->dev = dev;
  657. fm_eth->fm_index = fman_id(dev->parent);
  658. reg = (void *)(uintptr_t)dev_read_addr(dev);
  659. fm_eth->mac_type = dev_get_driver_data(dev);
  660. #ifdef CONFIG_PHYLIB
  661. fm_eth->enet_if = dev_read_phy_mode(dev);
  662. #else
  663. fm_eth->enet_if = PHY_INTERFACE_MODE_SGMII;
  664. printf("%s: warning - unable to determine interface type\n", __func__);
  665. #endif
  666. switch (fm_eth->mac_type) {
  667. #ifndef CONFIG_SYS_FMAN_V3
  668. case FM_TGEC:
  669. fm_eth->type = FM_ETH_10G_E;
  670. break;
  671. case FM_DTSEC:
  672. #else
  673. case FM_MEMAC:
  674. /* default to 1G, 10G is indicated by port property in dts */
  675. #endif
  676. fm_eth->type = FM_ETH_1G_E;
  677. break;
  678. }
  679. if (dev_read_u32(dev, "cell-index", &fm_eth->num)) {
  680. printf("FMan MAC node property cell-index missing\n");
  681. return -EINVAL;
  682. }
  683. if (dev_read_phandle_with_args(dev, "fsl,fman-ports", NULL,
  684. 0, 0, &args))
  685. goto ports_ref_failure;
  686. index = ofnode_read_u32_default(args.node, "cell-index", 0);
  687. if (index <= 0)
  688. goto ports_ref_failure;
  689. fm_eth->rx_port = fman_port(dev->parent, index);
  690. if (ofnode_read_bool(args.node, "fsl,fman-10g-port"))
  691. fm_eth->type = FM_ETH_10G_E;
  692. if (dev_read_phandle_with_args(dev, "fsl,fman-ports", NULL,
  693. 0, 1, &args))
  694. goto ports_ref_failure;
  695. index = ofnode_read_u32_default(args.node, "cell-index", 0);
  696. if (index <= 0)
  697. goto ports_ref_failure;
  698. fm_eth->tx_port = fman_port(dev->parent, index);
  699. /* set the ethernet max receive length */
  700. fm_eth->max_rx_len = MAX_RXBUF_LEN;
  701. switch (fm_eth->enet_if) {
  702. case PHY_INTERFACE_MODE_QSGMII:
  703. /* all PCS blocks are accessed on one controller */
  704. if (fm_eth->num != 0)
  705. break;
  706. case PHY_INTERFACE_MODE_SGMII:
  707. case PHY_INTERFACE_MODE_2500BASEX:
  708. fm_eth->pcs_mdio = fm_get_internal_mdio(dev);
  709. break;
  710. default:
  711. break;
  712. }
  713. /* init global mac structure */
  714. ret = fm_eth_init_mac(fm_eth, reg);
  715. if (ret)
  716. return ret;
  717. /* startup the FM im */
  718. ret = fm_eth_startup(fm_eth);
  719. if (!ret)
  720. ret = init_phy(fm_eth);
  721. return ret;
  722. ports_ref_failure:
  723. printf("Issue reading fsl,fman-ports for MAC %s\n", dev->name);
  724. return -ENOENT;
  725. }
  726. static int fm_eth_remove(struct udevice *dev)
  727. {
  728. return 0;
  729. }
  730. static const struct eth_ops fm_eth_ops = {
  731. .start = fm_eth_open,
  732. .send = fm_eth_send,
  733. .recv = fm_eth_recv,
  734. .free_pkt = fm_eth_free_pkt,
  735. .stop = fm_eth_halt,
  736. };
  737. static const struct udevice_id fm_eth_ids[] = {
  738. #ifdef CONFIG_SYS_FMAN_V3
  739. { .compatible = "fsl,fman-memac", .data = FM_MEMAC },
  740. #else
  741. { .compatible = "fsl,fman-dtsec", .data = FM_DTSEC },
  742. { .compatible = "fsl,fman-xgec", .data = FM_TGEC },
  743. #endif
  744. {}
  745. };
  746. U_BOOT_DRIVER(eth_fman) = {
  747. .name = "eth_fman",
  748. .id = UCLASS_ETH,
  749. .of_match = fm_eth_ids,
  750. .bind = fm_eth_bind,
  751. .probe = fm_eth_probe,
  752. .remove = fm_eth_remove,
  753. .ops = &fm_eth_ops,
  754. .priv_auto = sizeof(struct fm_eth),
  755. .plat_auto = sizeof(struct eth_pdata),
  756. .flags = DM_FLAG_ALLOC_PRIV_DMA,
  757. };