PageRenderTime 46ms CodeModel.GetById 18ms RepoModel.GetById 0ms app.codeStats 0ms

/drivers/net/fm/eth.c

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