eth.c 18 KB

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