eth.c 18 KB

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