eth.c 20 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795
  1. // SPDX-License-Identifier: GPL-2.0+
  2. /*
  3. * Copyright 2009-2012 Freescale Semiconductor, Inc.
  4. * Dave Liu <daveliu@freescale.com>
  5. */
  6. #include <common.h>
  7. #include <asm/io.h>
  8. #include <malloc.h>
  9. #include <net.h>
  10. #include <hwconfig.h>
  11. #include <fm_eth.h>
  12. #include <fsl_mdio.h>
  13. #include <miiphy.h>
  14. #include <phy.h>
  15. #include <fsl_dtsec.h>
  16. #include <fsl_tgec.h>
  17. #include <fsl_memac.h>
  18. #include "fm.h"
  19. static struct eth_device *devlist[NUM_FM_PORTS];
  20. static int num_controllers;
  21. #if defined(CONFIG_MII) || defined(CONFIG_CMD_MII) && !defined(BITBANGMII)
  22. #define TBIANA_SETTINGS (TBIANA_ASYMMETRIC_PAUSE | TBIANA_SYMMETRIC_PAUSE | \
  23. TBIANA_FULL_DUPLEX)
  24. #define TBIANA_SGMII_ACK 0x4001
  25. #define TBICR_SETTINGS (TBICR_ANEG_ENABLE | TBICR_RESTART_ANEG | \
  26. TBICR_FULL_DUPLEX | TBICR_SPEED1_SET)
  27. /* Configure the TBI for SGMII operation */
  28. static void dtsec_configure_serdes(struct fm_eth *priv)
  29. {
  30. #ifdef CONFIG_SYS_FMAN_V3
  31. u32 value;
  32. struct mii_dev bus;
  33. bus.priv = priv->mac->phyregs;
  34. bool sgmii_2500 = (priv->enet_if ==
  35. PHY_INTERFACE_MODE_SGMII_2500) ? true : false;
  36. int i = 0;
  37. qsgmii_loop:
  38. /* SGMII IF mode + AN enable only for 1G SGMII, not for 2.5G */
  39. if (sgmii_2500)
  40. value = PHY_SGMII_CR_PHY_RESET |
  41. PHY_SGMII_IF_SPEED_GIGABIT |
  42. PHY_SGMII_IF_MODE_SGMII;
  43. else
  44. value = PHY_SGMII_IF_MODE_SGMII | PHY_SGMII_IF_MODE_AN;
  45. memac_mdio_write(&bus, i, MDIO_DEVAD_NONE, 0x14, value);
  46. /* Dev ability according to SGMII specification */
  47. value = PHY_SGMII_DEV_ABILITY_SGMII;
  48. memac_mdio_write(&bus, i, MDIO_DEVAD_NONE, 0x4, value);
  49. if (sgmii_2500) {
  50. /* Adjust link timer for 2.5G SGMII,
  51. * 1.6 ms in units of 3.2 ns:
  52. * 1.6ms / 3.2ns = 5 * 10^5 = 0x7a120.
  53. */
  54. memac_mdio_write(&bus, i, MDIO_DEVAD_NONE, 0x13, 0x0007);
  55. memac_mdio_write(&bus, i, MDIO_DEVAD_NONE, 0x12, 0xa120);
  56. } else {
  57. /* Adjust link timer for SGMII,
  58. * 1.6 ms in units of 8 ns:
  59. * 1.6ms / 8ns = 2 * 10^5 = 0x30d40.
  60. */
  61. memac_mdio_write(&bus, i, MDIO_DEVAD_NONE, 0x13, 0x0003);
  62. memac_mdio_write(&bus, i, MDIO_DEVAD_NONE, 0x12, 0x0d40);
  63. }
  64. /* Restart AN */
  65. value = PHY_SGMII_CR_DEF_VAL | PHY_SGMII_CR_RESET_AN;
  66. memac_mdio_write(&bus, i, MDIO_DEVAD_NONE, 0, value);
  67. if ((priv->enet_if == PHY_INTERFACE_MODE_QSGMII) && (i < 3)) {
  68. i++;
  69. goto qsgmii_loop;
  70. }
  71. #else
  72. struct dtsec *regs = priv->mac->base;
  73. struct tsec_mii_mng *phyregs = priv->mac->phyregs;
  74. /*
  75. * Access TBI PHY registers at given TSEC register offset as
  76. * opposed to the register offset used for external PHY accesses
  77. */
  78. tsec_local_mdio_write(phyregs, in_be32(&regs->tbipa), 0, TBI_TBICON,
  79. TBICON_CLK_SELECT);
  80. tsec_local_mdio_write(phyregs, in_be32(&regs->tbipa), 0, TBI_ANA,
  81. TBIANA_SGMII_ACK);
  82. tsec_local_mdio_write(phyregs, in_be32(&regs->tbipa), 0,
  83. TBI_CR, TBICR_SETTINGS);
  84. #endif
  85. }
  86. static void dtsec_init_phy(struct eth_device *dev)
  87. {
  88. struct fm_eth *fm_eth = dev->priv;
  89. #ifndef CONFIG_SYS_FMAN_V3
  90. struct dtsec *regs = (struct dtsec *)CONFIG_SYS_FSL_FM1_DTSEC1_ADDR;
  91. /* Assign a Physical address to the TBI */
  92. out_be32(&regs->tbipa, CONFIG_SYS_TBIPA_VALUE);
  93. #endif
  94. if (fm_eth->enet_if == PHY_INTERFACE_MODE_SGMII ||
  95. fm_eth->enet_if == PHY_INTERFACE_MODE_QSGMII ||
  96. fm_eth->enet_if == PHY_INTERFACE_MODE_SGMII_2500)
  97. dtsec_configure_serdes(fm_eth);
  98. }
  99. #ifdef CONFIG_PHYLIB
  100. static int tgec_is_fibre(struct eth_device *dev)
  101. {
  102. struct fm_eth *fm = dev->priv;
  103. char phyopt[20];
  104. sprintf(phyopt, "fsl_fm%d_xaui_phy", fm->fm_index + 1);
  105. return hwconfig_arg_cmp(phyopt, "xfi");
  106. }
  107. #endif
  108. #endif
  109. static u16 muram_readw(u16 *addr)
  110. {
  111. ulong base = (ulong)addr & ~0x3UL;
  112. u32 val32 = in_be32((void *)base);
  113. int byte_pos;
  114. u16 ret;
  115. byte_pos = (ulong)addr & 0x3UL;
  116. if (byte_pos)
  117. ret = (u16)(val32 & 0x0000ffff);
  118. else
  119. ret = (u16)((val32 & 0xffff0000) >> 16);
  120. return ret;
  121. }
  122. static void muram_writew(u16 *addr, u16 val)
  123. {
  124. ulong base = (ulong)addr & ~0x3UL;
  125. u32 org32 = in_be32((void *)base);
  126. u32 val32;
  127. int byte_pos;
  128. byte_pos = (ulong)addr & 0x3UL;
  129. if (byte_pos)
  130. val32 = (org32 & 0xffff0000) | val;
  131. else
  132. val32 = (org32 & 0x0000ffff) | ((u32)val << 16);
  133. out_be32((void *)base, val32);
  134. }
  135. static void bmi_rx_port_disable(struct fm_bmi_rx_port *rx_port)
  136. {
  137. int timeout = 1000000;
  138. clrbits_be32(&rx_port->fmbm_rcfg, FMBM_RCFG_EN);
  139. /* wait until the rx port is not busy */
  140. while ((in_be32(&rx_port->fmbm_rst) & FMBM_RST_BSY) && timeout--)
  141. ;
  142. }
  143. static void bmi_rx_port_init(struct fm_bmi_rx_port *rx_port)
  144. {
  145. /* set BMI to independent mode, Rx port disable */
  146. out_be32(&rx_port->fmbm_rcfg, FMBM_RCFG_IM);
  147. /* clear FOF in IM case */
  148. out_be32(&rx_port->fmbm_rim, 0);
  149. /* Rx frame next engine -RISC */
  150. out_be32(&rx_port->fmbm_rfne, NIA_ENG_RISC | NIA_RISC_AC_IM_RX);
  151. /* Rx command attribute - no order, MR[3] = 1 */
  152. clrbits_be32(&rx_port->fmbm_rfca, FMBM_RFCA_ORDER | FMBM_RFCA_MR_MASK);
  153. setbits_be32(&rx_port->fmbm_rfca, FMBM_RFCA_MR(4));
  154. /* enable Rx statistic counters */
  155. out_be32(&rx_port->fmbm_rstc, FMBM_RSTC_EN);
  156. /* disable Rx performance counters */
  157. out_be32(&rx_port->fmbm_rpc, 0);
  158. }
  159. static void bmi_tx_port_disable(struct fm_bmi_tx_port *tx_port)
  160. {
  161. int timeout = 1000000;
  162. clrbits_be32(&tx_port->fmbm_tcfg, FMBM_TCFG_EN);
  163. /* wait until the tx port is not busy */
  164. while ((in_be32(&tx_port->fmbm_tst) & FMBM_TST_BSY) && timeout--)
  165. ;
  166. }
  167. static void bmi_tx_port_init(struct fm_bmi_tx_port *tx_port)
  168. {
  169. /* set BMI to independent mode, Tx port disable */
  170. out_be32(&tx_port->fmbm_tcfg, FMBM_TCFG_IM);
  171. /* Tx frame next engine -RISC */
  172. out_be32(&tx_port->fmbm_tfne, NIA_ENG_RISC | NIA_RISC_AC_IM_TX);
  173. out_be32(&tx_port->fmbm_tfene, NIA_ENG_RISC | NIA_RISC_AC_IM_TX);
  174. /* Tx command attribute - no order, MR[3] = 1 */
  175. clrbits_be32(&tx_port->fmbm_tfca, FMBM_TFCA_ORDER | FMBM_TFCA_MR_MASK);
  176. setbits_be32(&tx_port->fmbm_tfca, FMBM_TFCA_MR(4));
  177. /* enable Tx statistic counters */
  178. out_be32(&tx_port->fmbm_tstc, FMBM_TSTC_EN);
  179. /* disable Tx performance counters */
  180. out_be32(&tx_port->fmbm_tpc, 0);
  181. }
  182. static int fm_eth_rx_port_parameter_init(struct fm_eth *fm_eth)
  183. {
  184. struct fm_port_global_pram *pram;
  185. u32 pram_page_offset;
  186. void *rx_bd_ring_base;
  187. void *rx_buf_pool;
  188. u32 bd_ring_base_lo, bd_ring_base_hi;
  189. u32 buf_lo, buf_hi;
  190. struct fm_port_bd *rxbd;
  191. struct fm_port_qd *rxqd;
  192. struct fm_bmi_rx_port *bmi_rx_port = fm_eth->rx_port;
  193. int i;
  194. /* alloc global parameter ram at MURAM */
  195. pram = (struct fm_port_global_pram *)fm_muram_alloc(fm_eth->fm_index,
  196. FM_PRAM_SIZE, FM_PRAM_ALIGN);
  197. if (!pram) {
  198. printf("%s: No muram for Rx global parameter\n", __func__);
  199. return -ENOMEM;
  200. }
  201. fm_eth->rx_pram = pram;
  202. /* parameter page offset to MURAM */
  203. pram_page_offset = (void *)pram - fm_muram_base(fm_eth->fm_index);
  204. /* enable global mode- snooping data buffers and BDs */
  205. out_be32(&pram->mode, PRAM_MODE_GLOBAL);
  206. /* init the Rx queue descriptor pionter */
  207. out_be32(&pram->rxqd_ptr, pram_page_offset + 0x20);
  208. /* set the max receive buffer length, power of 2 */
  209. muram_writew(&pram->mrblr, MAX_RXBUF_LOG2);
  210. /* alloc Rx buffer descriptors from main memory */
  211. rx_bd_ring_base = malloc(sizeof(struct fm_port_bd)
  212. * RX_BD_RING_SIZE);
  213. if (!rx_bd_ring_base)
  214. return -ENOMEM;
  215. memset(rx_bd_ring_base, 0, sizeof(struct fm_port_bd)
  216. * RX_BD_RING_SIZE);
  217. /* alloc Rx buffer from main memory */
  218. rx_buf_pool = malloc(MAX_RXBUF_LEN * RX_BD_RING_SIZE);
  219. if (!rx_buf_pool)
  220. return -ENOMEM;
  221. memset(rx_buf_pool, 0, MAX_RXBUF_LEN * RX_BD_RING_SIZE);
  222. debug("%s: rx_buf_pool = %p\n", __func__, rx_buf_pool);
  223. /* save them to fm_eth */
  224. fm_eth->rx_bd_ring = rx_bd_ring_base;
  225. fm_eth->cur_rxbd = rx_bd_ring_base;
  226. fm_eth->rx_buf = rx_buf_pool;
  227. /* init Rx BDs ring */
  228. rxbd = (struct fm_port_bd *)rx_bd_ring_base;
  229. for (i = 0; i < RX_BD_RING_SIZE; i++) {
  230. muram_writew(&rxbd->status, RxBD_EMPTY);
  231. muram_writew(&rxbd->len, 0);
  232. buf_hi = upper_32_bits(virt_to_phys(rx_buf_pool +
  233. i * MAX_RXBUF_LEN));
  234. buf_lo = lower_32_bits(virt_to_phys(rx_buf_pool +
  235. i * MAX_RXBUF_LEN));
  236. muram_writew(&rxbd->buf_ptr_hi, (u16)buf_hi);
  237. out_be32(&rxbd->buf_ptr_lo, buf_lo);
  238. rxbd++;
  239. }
  240. /* set the Rx queue descriptor */
  241. rxqd = &pram->rxqd;
  242. muram_writew(&rxqd->gen, 0);
  243. bd_ring_base_hi = upper_32_bits(virt_to_phys(rx_bd_ring_base));
  244. bd_ring_base_lo = lower_32_bits(virt_to_phys(rx_bd_ring_base));
  245. muram_writew(&rxqd->bd_ring_base_hi, (u16)bd_ring_base_hi);
  246. out_be32(&rxqd->bd_ring_base_lo, bd_ring_base_lo);
  247. muram_writew(&rxqd->bd_ring_size, sizeof(struct fm_port_bd)
  248. * RX_BD_RING_SIZE);
  249. muram_writew(&rxqd->offset_in, 0);
  250. muram_writew(&rxqd->offset_out, 0);
  251. /* set IM parameter ram pointer to Rx Frame Queue ID */
  252. out_be32(&bmi_rx_port->fmbm_rfqid, pram_page_offset);
  253. return 0;
  254. }
  255. static int fm_eth_tx_port_parameter_init(struct fm_eth *fm_eth)
  256. {
  257. struct fm_port_global_pram *pram;
  258. u32 pram_page_offset;
  259. void *tx_bd_ring_base;
  260. u32 bd_ring_base_lo, bd_ring_base_hi;
  261. struct fm_port_bd *txbd;
  262. struct fm_port_qd *txqd;
  263. struct fm_bmi_tx_port *bmi_tx_port = fm_eth->tx_port;
  264. int i;
  265. /* alloc global parameter ram at MURAM */
  266. pram = (struct fm_port_global_pram *)fm_muram_alloc(fm_eth->fm_index,
  267. FM_PRAM_SIZE, FM_PRAM_ALIGN);
  268. if (!pram) {
  269. printf("%s: No muram for Tx global parameter\n", __func__);
  270. return -ENOMEM;
  271. }
  272. fm_eth->tx_pram = pram;
  273. /* parameter page offset to MURAM */
  274. pram_page_offset = (void *)pram - fm_muram_base(fm_eth->fm_index);
  275. /* enable global mode- snooping data buffers and BDs */
  276. out_be32(&pram->mode, PRAM_MODE_GLOBAL);
  277. /* init the Tx queue descriptor pionter */
  278. out_be32(&pram->txqd_ptr, pram_page_offset + 0x40);
  279. /* alloc Tx buffer descriptors from main memory */
  280. tx_bd_ring_base = malloc(sizeof(struct fm_port_bd)
  281. * TX_BD_RING_SIZE);
  282. if (!tx_bd_ring_base)
  283. return -ENOMEM;
  284. memset(tx_bd_ring_base, 0, sizeof(struct fm_port_bd)
  285. * TX_BD_RING_SIZE);
  286. /* save it to fm_eth */
  287. fm_eth->tx_bd_ring = tx_bd_ring_base;
  288. fm_eth->cur_txbd = tx_bd_ring_base;
  289. /* init Tx BDs ring */
  290. txbd = (struct fm_port_bd *)tx_bd_ring_base;
  291. for (i = 0; i < TX_BD_RING_SIZE; i++) {
  292. muram_writew(&txbd->status, TxBD_LAST);
  293. muram_writew(&txbd->len, 0);
  294. muram_writew(&txbd->buf_ptr_hi, 0);
  295. out_be32(&txbd->buf_ptr_lo, 0);
  296. txbd++;
  297. }
  298. /* set the Tx queue decriptor */
  299. txqd = &pram->txqd;
  300. bd_ring_base_hi = upper_32_bits(virt_to_phys(tx_bd_ring_base));
  301. bd_ring_base_lo = lower_32_bits(virt_to_phys(tx_bd_ring_base));
  302. muram_writew(&txqd->bd_ring_base_hi, (u16)bd_ring_base_hi);
  303. out_be32(&txqd->bd_ring_base_lo, bd_ring_base_lo);
  304. muram_writew(&txqd->bd_ring_size, sizeof(struct fm_port_bd)
  305. * TX_BD_RING_SIZE);
  306. muram_writew(&txqd->offset_in, 0);
  307. muram_writew(&txqd->offset_out, 0);
  308. /* set IM parameter ram pointer to Tx Confirmation Frame Queue ID */
  309. out_be32(&bmi_tx_port->fmbm_tcfqid, pram_page_offset);
  310. return 0;
  311. }
  312. static int fm_eth_init(struct fm_eth *fm_eth)
  313. {
  314. int ret;
  315. ret = fm_eth_rx_port_parameter_init(fm_eth);
  316. if (ret)
  317. return ret;
  318. ret = fm_eth_tx_port_parameter_init(fm_eth);
  319. if (ret)
  320. return ret;
  321. return 0;
  322. }
  323. static int fm_eth_startup(struct fm_eth *fm_eth)
  324. {
  325. struct fsl_enet_mac *mac;
  326. int ret;
  327. mac = fm_eth->mac;
  328. /* Rx/TxBDs, Rx/TxQDs, Rx buff and parameter ram init */
  329. ret = fm_eth_init(fm_eth);
  330. if (ret)
  331. return ret;
  332. /* setup the MAC controller */
  333. mac->init_mac(mac);
  334. /* For some reason we need to set SPEED_100 */
  335. if (((fm_eth->enet_if == PHY_INTERFACE_MODE_SGMII) ||
  336. (fm_eth->enet_if == PHY_INTERFACE_MODE_SGMII_2500) ||
  337. (fm_eth->enet_if == PHY_INTERFACE_MODE_QSGMII)) &&
  338. mac->set_if_mode)
  339. mac->set_if_mode(mac, fm_eth->enet_if, SPEED_100);
  340. /* init bmi rx port, IM mode and disable */
  341. bmi_rx_port_init(fm_eth->rx_port);
  342. /* init bmi tx port, IM mode and disable */
  343. bmi_tx_port_init(fm_eth->tx_port);
  344. return 0;
  345. }
  346. static void fmc_tx_port_graceful_stop_enable(struct fm_eth *fm_eth)
  347. {
  348. struct fm_port_global_pram *pram;
  349. pram = fm_eth->tx_pram;
  350. /* graceful stop transmission of frames */
  351. setbits_be32(&pram->mode, PRAM_MODE_GRACEFUL_STOP);
  352. sync();
  353. }
  354. static void fmc_tx_port_graceful_stop_disable(struct fm_eth *fm_eth)
  355. {
  356. struct fm_port_global_pram *pram;
  357. pram = fm_eth->tx_pram;
  358. /* re-enable transmission of frames */
  359. clrbits_be32(&pram->mode, PRAM_MODE_GRACEFUL_STOP);
  360. sync();
  361. }
  362. static int fm_eth_open(struct eth_device *dev, bd_t *bd)
  363. {
  364. struct fm_eth *fm_eth;
  365. struct fsl_enet_mac *mac;
  366. #ifdef CONFIG_PHYLIB
  367. int ret;
  368. #endif
  369. fm_eth = (struct fm_eth *)dev->priv;
  370. mac = fm_eth->mac;
  371. /* setup the MAC address */
  372. if (dev->enetaddr[0] & 0x01) {
  373. printf("%s: MacAddress is multcast address\n", __func__);
  374. return 1;
  375. }
  376. mac->set_mac_addr(mac, dev->enetaddr);
  377. /* enable bmi Rx port */
  378. setbits_be32(&fm_eth->rx_port->fmbm_rcfg, FMBM_RCFG_EN);
  379. /* enable MAC rx/tx port */
  380. mac->enable_mac(mac);
  381. /* enable bmi Tx port */
  382. setbits_be32(&fm_eth->tx_port->fmbm_tcfg, FMBM_TCFG_EN);
  383. /* re-enable transmission of frame */
  384. fmc_tx_port_graceful_stop_disable(fm_eth);
  385. #ifdef CONFIG_PHYLIB
  386. if (fm_eth->phydev) {
  387. ret = phy_startup(fm_eth->phydev);
  388. if (ret) {
  389. printf("%s: Could not initialize\n",
  390. fm_eth->phydev->dev->name);
  391. return ret;
  392. }
  393. } else {
  394. return 0;
  395. }
  396. #else
  397. fm_eth->phydev->speed = SPEED_1000;
  398. fm_eth->phydev->link = 1;
  399. fm_eth->phydev->duplex = DUPLEX_FULL;
  400. #endif
  401. /* set the MAC-PHY mode */
  402. mac->set_if_mode(mac, fm_eth->enet_if, fm_eth->phydev->speed);
  403. if (!fm_eth->phydev->link)
  404. printf("%s: No link.\n", fm_eth->phydev->dev->name);
  405. return fm_eth->phydev->link ? 0 : -1;
  406. }
  407. static void fm_eth_halt(struct eth_device *dev)
  408. {
  409. struct fm_eth *fm_eth;
  410. struct fsl_enet_mac *mac;
  411. fm_eth = (struct fm_eth *)dev->priv;
  412. mac = fm_eth->mac;
  413. /* graceful stop the transmission of frames */
  414. fmc_tx_port_graceful_stop_enable(fm_eth);
  415. /* disable bmi Tx port */
  416. bmi_tx_port_disable(fm_eth->tx_port);
  417. /* disable MAC rx/tx port */
  418. mac->disable_mac(mac);
  419. /* disable bmi Rx port */
  420. bmi_rx_port_disable(fm_eth->rx_port);
  421. #ifdef CONFIG_PHYLIB
  422. if (fm_eth->phydev)
  423. phy_shutdown(fm_eth->phydev);
  424. #endif
  425. }
  426. static int fm_eth_send(struct eth_device *dev, void *buf, int len)
  427. {
  428. struct fm_eth *fm_eth;
  429. struct fm_port_global_pram *pram;
  430. struct fm_port_bd *txbd, *txbd_base;
  431. u16 offset_in;
  432. int i;
  433. fm_eth = (struct fm_eth *)dev->priv;
  434. pram = fm_eth->tx_pram;
  435. txbd = fm_eth->cur_txbd;
  436. /* find one empty TxBD */
  437. for (i = 0; muram_readw(&txbd->status) & TxBD_READY; i++) {
  438. udelay(100);
  439. if (i > 0x1000) {
  440. printf("%s: Tx buffer not ready, txbd->status = 0x%x\n",
  441. dev->name, muram_readw(&txbd->status));
  442. return 0;
  443. }
  444. }
  445. /* setup TxBD */
  446. muram_writew(&txbd->buf_ptr_hi, (u16)upper_32_bits(virt_to_phys(buf)));
  447. out_be32(&txbd->buf_ptr_lo, lower_32_bits(virt_to_phys(buf)));
  448. muram_writew(&txbd->len, len);
  449. sync();
  450. muram_writew(&txbd->status, TxBD_READY | TxBD_LAST);
  451. sync();
  452. /* update TxQD, let RISC to send the packet */
  453. offset_in = muram_readw(&pram->txqd.offset_in);
  454. offset_in += sizeof(struct fm_port_bd);
  455. if (offset_in >= muram_readw(&pram->txqd.bd_ring_size))
  456. offset_in = 0;
  457. muram_writew(&pram->txqd.offset_in, offset_in);
  458. sync();
  459. /* wait for buffer to be transmitted */
  460. for (i = 0; muram_readw(&txbd->status) & TxBD_READY; i++) {
  461. udelay(100);
  462. if (i > 0x10000) {
  463. printf("%s: Tx error, txbd->status = 0x%x\n",
  464. dev->name, muram_readw(&txbd->status));
  465. return 0;
  466. }
  467. }
  468. /* advance the TxBD */
  469. txbd++;
  470. txbd_base = (struct fm_port_bd *)fm_eth->tx_bd_ring;
  471. if (txbd >= (txbd_base + TX_BD_RING_SIZE))
  472. txbd = txbd_base;
  473. /* update current txbd */
  474. fm_eth->cur_txbd = (void *)txbd;
  475. return 1;
  476. }
  477. static int fm_eth_recv(struct eth_device *dev)
  478. {
  479. struct fm_eth *fm_eth;
  480. struct fm_port_global_pram *pram;
  481. struct fm_port_bd *rxbd, *rxbd_base;
  482. u16 status, len;
  483. u32 buf_lo, buf_hi;
  484. u8 *data;
  485. u16 offset_out;
  486. int ret = 1;
  487. fm_eth = (struct fm_eth *)dev->priv;
  488. pram = fm_eth->rx_pram;
  489. rxbd = fm_eth->cur_rxbd;
  490. status = muram_readw(&rxbd->status);
  491. while (!(status & RxBD_EMPTY)) {
  492. if (!(status & RxBD_ERROR)) {
  493. buf_hi = muram_readw(&rxbd->buf_ptr_hi);
  494. buf_lo = in_be32(&rxbd->buf_ptr_lo);
  495. data = (u8 *)((ulong)(buf_hi << 16) << 16 | buf_lo);
  496. len = muram_readw(&rxbd->len);
  497. net_process_received_packet(data, len);
  498. } else {
  499. printf("%s: Rx error\n", dev->name);
  500. ret = 0;
  501. }
  502. /* clear the RxBDs */
  503. muram_writew(&rxbd->status, RxBD_EMPTY);
  504. muram_writew(&rxbd->len, 0);
  505. sync();
  506. /* advance RxBD */
  507. rxbd++;
  508. rxbd_base = (struct fm_port_bd *)fm_eth->rx_bd_ring;
  509. if (rxbd >= (rxbd_base + RX_BD_RING_SIZE))
  510. rxbd = rxbd_base;
  511. /* read next status */
  512. status = muram_readw(&rxbd->status);
  513. /* update RxQD */
  514. offset_out = muram_readw(&pram->rxqd.offset_out);
  515. offset_out += sizeof(struct fm_port_bd);
  516. if (offset_out >= muram_readw(&pram->rxqd.bd_ring_size))
  517. offset_out = 0;
  518. muram_writew(&pram->rxqd.offset_out, offset_out);
  519. sync();
  520. }
  521. fm_eth->cur_rxbd = (void *)rxbd;
  522. return ret;
  523. }
  524. static int fm_eth_init_mac(struct fm_eth *fm_eth, struct ccsr_fman *reg)
  525. {
  526. struct fsl_enet_mac *mac;
  527. int num;
  528. void *base, *phyregs = NULL;
  529. num = fm_eth->num;
  530. #ifdef CONFIG_SYS_FMAN_V3
  531. #ifndef CONFIG_FSL_FM_10GEC_REGULAR_NOTATION
  532. if (fm_eth->type == FM_ETH_10G_E) {
  533. /* 10GEC1/10GEC2 use mEMAC9/mEMAC10 on T2080/T4240.
  534. * 10GEC3/10GEC4 use mEMAC1/mEMAC2 on T2080.
  535. * 10GEC1 uses mEMAC1 on T1024.
  536. * so it needs to change the num.
  537. */
  538. if (fm_eth->num >= 2)
  539. num -= 2;
  540. else
  541. num += 8;
  542. }
  543. #endif
  544. base = &reg->memac[num].fm_memac;
  545. phyregs = &reg->memac[num].fm_memac_mdio;
  546. #else
  547. /* Get the mac registers base address */
  548. if (fm_eth->type == FM_ETH_1G_E) {
  549. base = &reg->mac_1g[num].fm_dtesc;
  550. phyregs = &reg->mac_1g[num].fm_mdio.miimcfg;
  551. } else {
  552. base = &reg->mac_10g[num].fm_10gec;
  553. phyregs = &reg->mac_10g[num].fm_10gec_mdio;
  554. }
  555. #endif
  556. /* alloc mac controller */
  557. mac = malloc(sizeof(struct fsl_enet_mac));
  558. if (!mac)
  559. return -ENOMEM;
  560. memset(mac, 0, sizeof(struct fsl_enet_mac));
  561. /* save the mac to fm_eth struct */
  562. fm_eth->mac = mac;
  563. #ifdef CONFIG_SYS_FMAN_V3
  564. init_memac(mac, base, phyregs, MAX_RXBUF_LEN);
  565. #else
  566. if (fm_eth->type == FM_ETH_1G_E)
  567. init_dtsec(mac, base, phyregs, MAX_RXBUF_LEN);
  568. else
  569. init_tgec(mac, base, phyregs, MAX_RXBUF_LEN);
  570. #endif
  571. return 0;
  572. }
  573. static int init_phy(struct eth_device *dev)
  574. {
  575. struct fm_eth *fm_eth = dev->priv;
  576. #ifdef CONFIG_PHYLIB
  577. struct phy_device *phydev = NULL;
  578. u32 supported;
  579. #endif
  580. if (fm_eth->type == FM_ETH_1G_E)
  581. dtsec_init_phy(dev);
  582. #ifdef CONFIG_PHYLIB
  583. if (fm_eth->bus) {
  584. phydev = phy_connect(fm_eth->bus, fm_eth->phyaddr, dev,
  585. fm_eth->enet_if);
  586. if (!phydev) {
  587. printf("Failed to connect\n");
  588. return -1;
  589. }
  590. } else {
  591. return 0;
  592. }
  593. if (fm_eth->type == FM_ETH_1G_E) {
  594. supported = (SUPPORTED_10baseT_Half |
  595. SUPPORTED_10baseT_Full |
  596. SUPPORTED_100baseT_Half |
  597. SUPPORTED_100baseT_Full |
  598. SUPPORTED_1000baseT_Full);
  599. } else {
  600. supported = SUPPORTED_10000baseT_Full;
  601. if (tgec_is_fibre(dev))
  602. phydev->port = PORT_FIBRE;
  603. }
  604. phydev->supported &= supported;
  605. phydev->advertising = phydev->supported;
  606. fm_eth->phydev = phydev;
  607. phy_config(phydev);
  608. #endif
  609. return 0;
  610. }
  611. int fm_eth_initialize(struct ccsr_fman *reg, struct fm_eth_info *info)
  612. {
  613. struct eth_device *dev;
  614. struct fm_eth *fm_eth;
  615. int i, num = info->num;
  616. int ret;
  617. /* alloc eth device */
  618. dev = (struct eth_device *)malloc(sizeof(struct eth_device));
  619. if (!dev)
  620. return -ENOMEM;
  621. memset(dev, 0, sizeof(struct eth_device));
  622. /* alloc the FMan ethernet private struct */
  623. fm_eth = (struct fm_eth *)malloc(sizeof(struct fm_eth));
  624. if (!fm_eth)
  625. return -ENOMEM;
  626. memset(fm_eth, 0, sizeof(struct fm_eth));
  627. /* save off some things we need from the info struct */
  628. fm_eth->fm_index = info->index - 1; /* keep as 0 based for muram */
  629. fm_eth->num = num;
  630. fm_eth->type = info->type;
  631. fm_eth->rx_port = (void *)&reg->port[info->rx_port_id - 1].fm_bmi;
  632. fm_eth->tx_port = (void *)&reg->port[info->tx_port_id - 1].fm_bmi;
  633. /* set the ethernet max receive length */
  634. fm_eth->max_rx_len = MAX_RXBUF_LEN;
  635. /* init global mac structure */
  636. ret = fm_eth_init_mac(fm_eth, reg);
  637. if (ret)
  638. return ret;
  639. /* keep same as the manual, we call FMAN1, FMAN2, DTSEC1, DTSEC2, etc */
  640. if (fm_eth->type == FM_ETH_1G_E)
  641. sprintf(dev->name, "FM%d@DTSEC%d", info->index, num + 1);
  642. else
  643. sprintf(dev->name, "FM%d@TGEC%d", info->index, num + 1);
  644. devlist[num_controllers++] = dev;
  645. dev->iobase = 0;
  646. dev->priv = (void *)fm_eth;
  647. dev->init = fm_eth_open;
  648. dev->halt = fm_eth_halt;
  649. dev->send = fm_eth_send;
  650. dev->recv = fm_eth_recv;
  651. fm_eth->dev = dev;
  652. fm_eth->bus = info->bus;
  653. fm_eth->phyaddr = info->phy_addr;
  654. fm_eth->enet_if = info->enet_if;
  655. /* startup the FM im */
  656. ret = fm_eth_startup(fm_eth);
  657. if (ret)
  658. return ret;
  659. init_phy(dev);
  660. /* clear the ethernet address */
  661. for (i = 0; i < 6; i++)
  662. dev->enetaddr[i] = 0;
  663. eth_register(dev);
  664. return 0;
  665. }