eth.c 18 KB

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