mvgbe.c 20 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788
  1. /*
  2. * (C) Copyright 2009
  3. * Marvell Semiconductor <www.marvell.com>
  4. * Written-by: Prafulla Wadaskar <prafulla@marvell.com>
  5. *
  6. * (C) Copyright 2003
  7. * Ingo Assmus <ingo.assmus@keymile.com>
  8. *
  9. * based on - Driver for MV64360X ethernet ports
  10. * Copyright (C) 2002 rabeeh@galileo.co.il
  11. *
  12. * SPDX-License-Identifier: GPL-2.0+
  13. */
  14. #include <common.h>
  15. #include <net.h>
  16. #include <malloc.h>
  17. #include <miiphy.h>
  18. #include <asm/io.h>
  19. #include <asm/errno.h>
  20. #include <asm/types.h>
  21. #include <asm/system.h>
  22. #include <asm/byteorder.h>
  23. #include <asm/arch/cpu.h>
  24. #if defined(CONFIG_KIRKWOOD)
  25. #include <asm/arch/soc.h>
  26. #elif defined(CONFIG_ORION5X)
  27. #include <asm/arch/orion5x.h>
  28. #elif defined(CONFIG_DOVE)
  29. #include <asm/arch/dove.h>
  30. #endif
  31. #include "mvgbe.h"
  32. DECLARE_GLOBAL_DATA_PTR;
  33. #define MV_PHY_ADR_REQUEST 0xee
  34. #define MVGBE_SMI_REG (((struct mvgbe_registers *)MVGBE0_BASE)->smi)
  35. #if defined(CONFIG_PHYLIB) || defined(CONFIG_MII) || defined(CONFIG_CMD_MII)
  36. /*
  37. * smi_reg_read - miiphy_read callback function.
  38. *
  39. * Returns 16bit phy register value, or 0xffff on error
  40. */
  41. static int smi_reg_read(const char *devname, u8 phy_adr, u8 reg_ofs, u16 * data)
  42. {
  43. struct eth_device *dev = eth_get_dev_by_name(devname);
  44. struct mvgbe_device *dmvgbe = to_mvgbe(dev);
  45. struct mvgbe_registers *regs = dmvgbe->regs;
  46. u32 smi_reg;
  47. u32 timeout;
  48. /* Phyadr read request */
  49. if (phy_adr == MV_PHY_ADR_REQUEST &&
  50. reg_ofs == MV_PHY_ADR_REQUEST) {
  51. /* */
  52. *data = (u16) (MVGBE_REG_RD(regs->phyadr) & PHYADR_MASK);
  53. return 0;
  54. }
  55. /* check parameters */
  56. if (phy_adr > PHYADR_MASK) {
  57. printf("Err..(%s) Invalid PHY address %d\n",
  58. __FUNCTION__, phy_adr);
  59. return -EFAULT;
  60. }
  61. if (reg_ofs > PHYREG_MASK) {
  62. printf("Err..(%s) Invalid register offset %d\n",
  63. __FUNCTION__, reg_ofs);
  64. return -EFAULT;
  65. }
  66. timeout = MVGBE_PHY_SMI_TIMEOUT;
  67. /* wait till the SMI is not busy */
  68. do {
  69. /* read smi register */
  70. smi_reg = MVGBE_REG_RD(MVGBE_SMI_REG);
  71. if (timeout-- == 0) {
  72. printf("Err..(%s) SMI busy timeout\n", __FUNCTION__);
  73. return -EFAULT;
  74. }
  75. } while (smi_reg & MVGBE_PHY_SMI_BUSY_MASK);
  76. /* fill the phy address and regiser offset and read opcode */
  77. smi_reg = (phy_adr << MVGBE_PHY_SMI_DEV_ADDR_OFFS)
  78. | (reg_ofs << MVGBE_SMI_REG_ADDR_OFFS)
  79. | MVGBE_PHY_SMI_OPCODE_READ;
  80. /* write the smi register */
  81. MVGBE_REG_WR(MVGBE_SMI_REG, smi_reg);
  82. /*wait till read value is ready */
  83. timeout = MVGBE_PHY_SMI_TIMEOUT;
  84. do {
  85. /* read smi register */
  86. smi_reg = MVGBE_REG_RD(MVGBE_SMI_REG);
  87. if (timeout-- == 0) {
  88. printf("Err..(%s) SMI read ready timeout\n",
  89. __FUNCTION__);
  90. return -EFAULT;
  91. }
  92. } while (!(smi_reg & MVGBE_PHY_SMI_READ_VALID_MASK));
  93. /* Wait for the data to update in the SMI register */
  94. for (timeout = 0; timeout < MVGBE_PHY_SMI_TIMEOUT; timeout++)
  95. ;
  96. *data = (u16) (MVGBE_REG_RD(MVGBE_SMI_REG) & MVGBE_PHY_SMI_DATA_MASK);
  97. debug("%s:(adr %d, off %d) value= %04x\n", __FUNCTION__, phy_adr,
  98. reg_ofs, *data);
  99. return 0;
  100. }
  101. /*
  102. * smi_reg_write - imiiphy_write callback function.
  103. *
  104. * Returns 0 if write succeed, -EINVAL on bad parameters
  105. * -ETIME on timeout
  106. */
  107. static int smi_reg_write(const char *devname, u8 phy_adr, u8 reg_ofs, u16 data)
  108. {
  109. struct eth_device *dev = eth_get_dev_by_name(devname);
  110. struct mvgbe_device *dmvgbe = to_mvgbe(dev);
  111. struct mvgbe_registers *regs = dmvgbe->regs;
  112. u32 smi_reg;
  113. u32 timeout;
  114. /* Phyadr write request*/
  115. if (phy_adr == MV_PHY_ADR_REQUEST &&
  116. reg_ofs == MV_PHY_ADR_REQUEST) {
  117. MVGBE_REG_WR(regs->phyadr, data);
  118. return 0;
  119. }
  120. /* check parameters */
  121. if (phy_adr > PHYADR_MASK) {
  122. printf("Err..(%s) Invalid phy address\n", __FUNCTION__);
  123. return -EINVAL;
  124. }
  125. if (reg_ofs > PHYREG_MASK) {
  126. printf("Err..(%s) Invalid register offset\n", __FUNCTION__);
  127. return -EINVAL;
  128. }
  129. /* wait till the SMI is not busy */
  130. timeout = MVGBE_PHY_SMI_TIMEOUT;
  131. do {
  132. /* read smi register */
  133. smi_reg = MVGBE_REG_RD(MVGBE_SMI_REG);
  134. if (timeout-- == 0) {
  135. printf("Err..(%s) SMI busy timeout\n", __FUNCTION__);
  136. return -ETIME;
  137. }
  138. } while (smi_reg & MVGBE_PHY_SMI_BUSY_MASK);
  139. /* fill the phy addr and reg offset and write opcode and data */
  140. smi_reg = (data << MVGBE_PHY_SMI_DATA_OFFS);
  141. smi_reg |= (phy_adr << MVGBE_PHY_SMI_DEV_ADDR_OFFS)
  142. | (reg_ofs << MVGBE_SMI_REG_ADDR_OFFS);
  143. smi_reg &= ~MVGBE_PHY_SMI_OPCODE_READ;
  144. /* write the smi register */
  145. MVGBE_REG_WR(MVGBE_SMI_REG, smi_reg);
  146. return 0;
  147. }
  148. #endif
  149. #if defined(CONFIG_PHYLIB)
  150. int mvgbe_phy_read(struct mii_dev *bus, int phy_addr, int dev_addr,
  151. int reg_addr)
  152. {
  153. u16 data;
  154. int ret;
  155. ret = smi_reg_read(bus->name, phy_addr, reg_addr, &data);
  156. if (ret)
  157. return ret;
  158. return data;
  159. }
  160. int mvgbe_phy_write(struct mii_dev *bus, int phy_addr, int dev_addr,
  161. int reg_addr, u16 data)
  162. {
  163. return smi_reg_write(bus->name, phy_addr, reg_addr, data);
  164. }
  165. #endif
  166. /* Stop and checks all queues */
  167. static void stop_queue(u32 * qreg)
  168. {
  169. u32 reg_data;
  170. reg_data = readl(qreg);
  171. if (reg_data & 0xFF) {
  172. /* Issue stop command for active channels only */
  173. writel((reg_data << 8), qreg);
  174. /* Wait for all queue activity to terminate. */
  175. do {
  176. /*
  177. * Check port cause register that all queues
  178. * are stopped
  179. */
  180. reg_data = readl(qreg);
  181. }
  182. while (reg_data & 0xFF);
  183. }
  184. }
  185. /*
  186. * set_access_control - Config address decode parameters for Ethernet unit
  187. *
  188. * This function configures the address decode parameters for the Gigabit
  189. * Ethernet Controller according the given parameters struct.
  190. *
  191. * @regs Register struct pointer.
  192. * @param Address decode parameter struct.
  193. */
  194. static void set_access_control(struct mvgbe_registers *regs,
  195. struct mvgbe_winparam *param)
  196. {
  197. u32 access_prot_reg;
  198. /* Set access control register */
  199. access_prot_reg = MVGBE_REG_RD(regs->epap);
  200. /* clear window permission */
  201. access_prot_reg &= (~(3 << (param->win * 2)));
  202. access_prot_reg |= (param->access_ctrl << (param->win * 2));
  203. MVGBE_REG_WR(regs->epap, access_prot_reg);
  204. /* Set window Size reg (SR) */
  205. MVGBE_REG_WR(regs->barsz[param->win].size,
  206. (((param->size / 0x10000) - 1) << 16));
  207. /* Set window Base address reg (BA) */
  208. MVGBE_REG_WR(regs->barsz[param->win].bar,
  209. (param->target | param->attrib | param->base_addr));
  210. /* High address remap reg (HARR) */
  211. if (param->win < 4)
  212. MVGBE_REG_WR(regs->ha_remap[param->win], param->high_addr);
  213. /* Base address enable reg (BARER) */
  214. if (param->enable == 1)
  215. MVGBE_REG_BITS_RESET(regs->bare, (1 << param->win));
  216. else
  217. MVGBE_REG_BITS_SET(regs->bare, (1 << param->win));
  218. }
  219. static void set_dram_access(struct mvgbe_registers *regs)
  220. {
  221. struct mvgbe_winparam win_param;
  222. int i;
  223. for (i = 0; i < CONFIG_NR_DRAM_BANKS; i++) {
  224. /* Set access parameters for DRAM bank i */
  225. win_param.win = i; /* Use Ethernet window i */
  226. /* Window target - DDR */
  227. win_param.target = MVGBE_TARGET_DRAM;
  228. /* Enable full access */
  229. win_param.access_ctrl = EWIN_ACCESS_FULL;
  230. win_param.high_addr = 0;
  231. /* Get bank base and size */
  232. win_param.base_addr = gd->bd->bi_dram[i].start;
  233. win_param.size = gd->bd->bi_dram[i].size;
  234. if (win_param.size == 0)
  235. win_param.enable = 0;
  236. else
  237. win_param.enable = 1; /* Enable the access */
  238. /* Enable DRAM bank */
  239. switch (i) {
  240. case 0:
  241. win_param.attrib = EBAR_DRAM_CS0;
  242. break;
  243. case 1:
  244. win_param.attrib = EBAR_DRAM_CS1;
  245. break;
  246. case 2:
  247. win_param.attrib = EBAR_DRAM_CS2;
  248. break;
  249. case 3:
  250. win_param.attrib = EBAR_DRAM_CS3;
  251. break;
  252. default:
  253. /* invalid bank, disable access */
  254. win_param.enable = 0;
  255. win_param.attrib = 0;
  256. break;
  257. }
  258. /* Set the access control for address window(EPAPR) RD/WR */
  259. set_access_control(regs, &win_param);
  260. }
  261. }
  262. /*
  263. * port_init_mac_tables - Clear all entrance in the UC, SMC and OMC tables
  264. *
  265. * Go through all the DA filter tables (Unicast, Special Multicast & Other
  266. * Multicast) and set each entry to 0.
  267. */
  268. static void port_init_mac_tables(struct mvgbe_registers *regs)
  269. {
  270. int table_index;
  271. /* Clear DA filter unicast table (Ex_dFUT) */
  272. for (table_index = 0; table_index < 4; ++table_index)
  273. MVGBE_REG_WR(regs->dfut[table_index], 0);
  274. for (table_index = 0; table_index < 64; ++table_index) {
  275. /* Clear DA filter special multicast table (Ex_dFSMT) */
  276. MVGBE_REG_WR(regs->dfsmt[table_index], 0);
  277. /* Clear DA filter other multicast table (Ex_dFOMT) */
  278. MVGBE_REG_WR(regs->dfomt[table_index], 0);
  279. }
  280. }
  281. /*
  282. * port_uc_addr - This function Set the port unicast address table
  283. *
  284. * This function locates the proper entry in the Unicast table for the
  285. * specified MAC nibble and sets its properties according to function
  286. * parameters.
  287. * This function add/removes MAC addresses from the port unicast address
  288. * table.
  289. *
  290. * @uc_nibble Unicast MAC Address last nibble.
  291. * @option 0 = Add, 1 = remove address.
  292. *
  293. * RETURN: 1 if output succeeded. 0 if option parameter is invalid.
  294. */
  295. static int port_uc_addr(struct mvgbe_registers *regs, u8 uc_nibble,
  296. int option)
  297. {
  298. u32 unicast_reg;
  299. u32 tbl_offset;
  300. u32 reg_offset;
  301. /* Locate the Unicast table entry */
  302. uc_nibble = (0xf & uc_nibble);
  303. /* Register offset from unicast table base */
  304. tbl_offset = (uc_nibble / 4);
  305. /* Entry offset within the above register */
  306. reg_offset = uc_nibble % 4;
  307. switch (option) {
  308. case REJECT_MAC_ADDR:
  309. /*
  310. * Clear accepts frame bit at specified unicast
  311. * DA table entry
  312. */
  313. unicast_reg = MVGBE_REG_RD(regs->dfut[tbl_offset]);
  314. unicast_reg &= (0xFF << (8 * reg_offset));
  315. MVGBE_REG_WR(regs->dfut[tbl_offset], unicast_reg);
  316. break;
  317. case ACCEPT_MAC_ADDR:
  318. /* Set accepts frame bit at unicast DA filter table entry */
  319. unicast_reg = MVGBE_REG_RD(regs->dfut[tbl_offset]);
  320. unicast_reg &= (0xFF << (8 * reg_offset));
  321. unicast_reg |= ((0x01 | (RXUQ << 1)) << (8 * reg_offset));
  322. MVGBE_REG_WR(regs->dfut[tbl_offset], unicast_reg);
  323. break;
  324. default:
  325. return 0;
  326. }
  327. return 1;
  328. }
  329. /*
  330. * port_uc_addr_set - This function Set the port Unicast address.
  331. */
  332. static void port_uc_addr_set(struct mvgbe_registers *regs, u8 * p_addr)
  333. {
  334. u32 mac_h;
  335. u32 mac_l;
  336. mac_l = (p_addr[4] << 8) | (p_addr[5]);
  337. mac_h = (p_addr[0] << 24) | (p_addr[1] << 16) | (p_addr[2] << 8) |
  338. (p_addr[3] << 0);
  339. MVGBE_REG_WR(regs->macal, mac_l);
  340. MVGBE_REG_WR(regs->macah, mac_h);
  341. /* Accept frames of this address */
  342. port_uc_addr(regs, p_addr[5], ACCEPT_MAC_ADDR);
  343. }
  344. /*
  345. * mvgbe_init_rx_desc_ring - Curve a Rx chain desc list and buffer in memory.
  346. */
  347. static void mvgbe_init_rx_desc_ring(struct mvgbe_device *dmvgbe)
  348. {
  349. struct mvgbe_rxdesc *p_rx_desc;
  350. int i;
  351. /* initialize the Rx descriptors ring */
  352. p_rx_desc = dmvgbe->p_rxdesc;
  353. for (i = 0; i < RINGSZ; i++) {
  354. p_rx_desc->cmd_sts =
  355. MVGBE_BUFFER_OWNED_BY_DMA | MVGBE_RX_EN_INTERRUPT;
  356. p_rx_desc->buf_size = PKTSIZE_ALIGN;
  357. p_rx_desc->byte_cnt = 0;
  358. p_rx_desc->buf_ptr = dmvgbe->p_rxbuf + i * PKTSIZE_ALIGN;
  359. if (i == (RINGSZ - 1))
  360. p_rx_desc->nxtdesc_p = dmvgbe->p_rxdesc;
  361. else {
  362. p_rx_desc->nxtdesc_p = (struct mvgbe_rxdesc *)
  363. ((u32) p_rx_desc + MV_RXQ_DESC_ALIGNED_SIZE);
  364. p_rx_desc = p_rx_desc->nxtdesc_p;
  365. }
  366. }
  367. dmvgbe->p_rxdesc_curr = dmvgbe->p_rxdesc;
  368. }
  369. static int mvgbe_init(struct eth_device *dev)
  370. {
  371. struct mvgbe_device *dmvgbe = to_mvgbe(dev);
  372. struct mvgbe_registers *regs = dmvgbe->regs;
  373. #if (defined(CONFIG_MII) || defined(CONFIG_CMD_MII)) && \
  374. !defined(CONFIG_PHYLIB) && \
  375. defined(CONFIG_SYS_FAULT_ECHO_LINK_DOWN)
  376. int i;
  377. #endif
  378. /* setup RX rings */
  379. mvgbe_init_rx_desc_ring(dmvgbe);
  380. /* Clear the ethernet port interrupts */
  381. MVGBE_REG_WR(regs->ic, 0);
  382. MVGBE_REG_WR(regs->ice, 0);
  383. /* Unmask RX buffer and TX end interrupt */
  384. MVGBE_REG_WR(regs->pim, INT_CAUSE_UNMASK_ALL);
  385. /* Unmask phy and link status changes interrupts */
  386. MVGBE_REG_WR(regs->peim, INT_CAUSE_UNMASK_ALL_EXT);
  387. set_dram_access(regs);
  388. port_init_mac_tables(regs);
  389. port_uc_addr_set(regs, dmvgbe->dev.enetaddr);
  390. /* Assign port configuration and command. */
  391. MVGBE_REG_WR(regs->pxc, PRT_CFG_VAL);
  392. MVGBE_REG_WR(regs->pxcx, PORT_CFG_EXTEND_VALUE);
  393. MVGBE_REG_WR(regs->psc0, PORT_SERIAL_CONTROL_VALUE);
  394. /* Assign port SDMA configuration */
  395. MVGBE_REG_WR(regs->sdc, PORT_SDMA_CFG_VALUE);
  396. MVGBE_REG_WR(regs->tqx[0].qxttbc, QTKNBKT_DEF_VAL);
  397. MVGBE_REG_WR(regs->tqx[0].tqxtbc,
  398. (QMTBS_DEF_VAL << 16) | QTKNRT_DEF_VAL);
  399. /* Turn off the port/RXUQ bandwidth limitation */
  400. MVGBE_REG_WR(regs->pmtu, 0);
  401. /* Set maximum receive buffer to 9700 bytes */
  402. MVGBE_REG_WR(regs->psc0, MVGBE_MAX_RX_PACKET_9700BYTE
  403. | (MVGBE_REG_RD(regs->psc0) & MRU_MASK));
  404. /* Enable port initially */
  405. MVGBE_REG_BITS_SET(regs->psc0, MVGBE_SERIAL_PORT_EN);
  406. /*
  407. * Set ethernet MTU for leaky bucket mechanism to 0 - this will
  408. * disable the leaky bucket mechanism .
  409. */
  410. MVGBE_REG_WR(regs->pmtu, 0);
  411. /* Assignment of Rx CRDB of given RXUQ */
  412. MVGBE_REG_WR(regs->rxcdp[RXUQ], (u32) dmvgbe->p_rxdesc_curr);
  413. /* ensure previous write is done before enabling Rx DMA */
  414. isb();
  415. /* Enable port Rx. */
  416. MVGBE_REG_WR(regs->rqc, (1 << RXUQ));
  417. #if (defined(CONFIG_MII) || defined(CONFIG_CMD_MII)) && \
  418. !defined(CONFIG_PHYLIB) && \
  419. defined(CONFIG_SYS_FAULT_ECHO_LINK_DOWN)
  420. /* Wait up to 5s for the link status */
  421. for (i = 0; i < 5; i++) {
  422. u16 phyadr;
  423. miiphy_read(dev->name, MV_PHY_ADR_REQUEST,
  424. MV_PHY_ADR_REQUEST, &phyadr);
  425. /* Return if we get link up */
  426. if (miiphy_link(dev->name, phyadr))
  427. return 0;
  428. udelay(1000000);
  429. }
  430. printf("No link on %s\n", dev->name);
  431. return -1;
  432. #endif
  433. return 0;
  434. }
  435. static int mvgbe_halt(struct eth_device *dev)
  436. {
  437. struct mvgbe_device *dmvgbe = to_mvgbe(dev);
  438. struct mvgbe_registers *regs = dmvgbe->regs;
  439. /* Disable all gigE address decoder */
  440. MVGBE_REG_WR(regs->bare, 0x3f);
  441. stop_queue(&regs->tqc);
  442. stop_queue(&regs->rqc);
  443. /* Disable port */
  444. MVGBE_REG_BITS_RESET(regs->psc0, MVGBE_SERIAL_PORT_EN);
  445. /* Set port is not reset */
  446. MVGBE_REG_BITS_RESET(regs->psc1, 1 << 4);
  447. #ifdef CONFIG_SYS_MII_MODE
  448. /* Set MMI interface up */
  449. MVGBE_REG_BITS_RESET(regs->psc1, 1 << 3);
  450. #endif
  451. /* Disable & mask ethernet port interrupts */
  452. MVGBE_REG_WR(regs->ic, 0);
  453. MVGBE_REG_WR(regs->ice, 0);
  454. MVGBE_REG_WR(regs->pim, 0);
  455. MVGBE_REG_WR(regs->peim, 0);
  456. return 0;
  457. }
  458. static int mvgbe_write_hwaddr(struct eth_device *dev)
  459. {
  460. struct mvgbe_device *dmvgbe = to_mvgbe(dev);
  461. struct mvgbe_registers *regs = dmvgbe->regs;
  462. /* Programs net device MAC address after initialization */
  463. port_uc_addr_set(regs, dmvgbe->dev.enetaddr);
  464. return 0;
  465. }
  466. static int mvgbe_send(struct eth_device *dev, void *dataptr, int datasize)
  467. {
  468. struct mvgbe_device *dmvgbe = to_mvgbe(dev);
  469. struct mvgbe_registers *regs = dmvgbe->regs;
  470. struct mvgbe_txdesc *p_txdesc = dmvgbe->p_txdesc;
  471. void *p = (void *)dataptr;
  472. u32 cmd_sts;
  473. u32 txuq0_reg_addr;
  474. /* Copy buffer if it's misaligned */
  475. if ((u32) dataptr & 0x07) {
  476. if (datasize > PKTSIZE_ALIGN) {
  477. printf("Non-aligned data too large (%d)\n",
  478. datasize);
  479. return -1;
  480. }
  481. memcpy(dmvgbe->p_aligned_txbuf, p, datasize);
  482. p = dmvgbe->p_aligned_txbuf;
  483. }
  484. p_txdesc->cmd_sts = MVGBE_ZERO_PADDING | MVGBE_GEN_CRC;
  485. p_txdesc->cmd_sts |= MVGBE_TX_FIRST_DESC | MVGBE_TX_LAST_DESC;
  486. p_txdesc->cmd_sts |= MVGBE_BUFFER_OWNED_BY_DMA;
  487. p_txdesc->cmd_sts |= MVGBE_TX_EN_INTERRUPT;
  488. p_txdesc->buf_ptr = (u8 *) p;
  489. p_txdesc->byte_cnt = datasize;
  490. /* Set this tc desc as zeroth TXUQ */
  491. txuq0_reg_addr = (u32)&regs->tcqdp[TXUQ];
  492. writel((u32) p_txdesc, txuq0_reg_addr);
  493. /* ensure tx desc writes above are performed before we start Tx DMA */
  494. isb();
  495. /* Apply send command using zeroth TXUQ */
  496. MVGBE_REG_WR(regs->tqc, (1 << TXUQ));
  497. /*
  498. * wait for packet xmit completion
  499. */
  500. cmd_sts = readl(&p_txdesc->cmd_sts);
  501. while (cmd_sts & MVGBE_BUFFER_OWNED_BY_DMA) {
  502. /* return fail if error is detected */
  503. if ((cmd_sts & (MVGBE_ERROR_SUMMARY | MVGBE_TX_LAST_FRAME)) ==
  504. (MVGBE_ERROR_SUMMARY | MVGBE_TX_LAST_FRAME) &&
  505. cmd_sts & (MVGBE_UR_ERROR | MVGBE_RL_ERROR)) {
  506. printf("Err..(%s) in xmit packet\n", __FUNCTION__);
  507. return -1;
  508. }
  509. cmd_sts = readl(&p_txdesc->cmd_sts);
  510. };
  511. return 0;
  512. }
  513. static int mvgbe_recv(struct eth_device *dev)
  514. {
  515. struct mvgbe_device *dmvgbe = to_mvgbe(dev);
  516. struct mvgbe_rxdesc *p_rxdesc_curr = dmvgbe->p_rxdesc_curr;
  517. u32 cmd_sts;
  518. u32 timeout = 0;
  519. u32 rxdesc_curr_addr;
  520. /* wait untill rx packet available or timeout */
  521. do {
  522. if (timeout < MVGBE_PHY_SMI_TIMEOUT)
  523. timeout++;
  524. else {
  525. debug("%s time out...\n", __FUNCTION__);
  526. return -1;
  527. }
  528. } while (readl(&p_rxdesc_curr->cmd_sts) & MVGBE_BUFFER_OWNED_BY_DMA);
  529. if (p_rxdesc_curr->byte_cnt != 0) {
  530. debug("%s: Received %d byte Packet @ 0x%x (cmd_sts= %08x)\n",
  531. __FUNCTION__, (u32) p_rxdesc_curr->byte_cnt,
  532. (u32) p_rxdesc_curr->buf_ptr,
  533. (u32) p_rxdesc_curr->cmd_sts);
  534. }
  535. /*
  536. * In case received a packet without first/last bits on
  537. * OR the error summary bit is on,
  538. * the packets needs to be dropeed.
  539. */
  540. cmd_sts = readl(&p_rxdesc_curr->cmd_sts);
  541. if ((cmd_sts &
  542. (MVGBE_RX_FIRST_DESC | MVGBE_RX_LAST_DESC))
  543. != (MVGBE_RX_FIRST_DESC | MVGBE_RX_LAST_DESC)) {
  544. printf("Err..(%s) Dropping packet spread on"
  545. " multiple descriptors\n", __FUNCTION__);
  546. } else if (cmd_sts & MVGBE_ERROR_SUMMARY) {
  547. printf("Err..(%s) Dropping packet with errors\n",
  548. __FUNCTION__);
  549. } else {
  550. /* !!! call higher layer processing */
  551. debug("%s: Sending Received packet to"
  552. " upper layer (NetReceive)\n", __FUNCTION__);
  553. /* let the upper layer handle the packet */
  554. NetReceive((p_rxdesc_curr->buf_ptr + RX_BUF_OFFSET),
  555. (int)(p_rxdesc_curr->byte_cnt - RX_BUF_OFFSET));
  556. }
  557. /*
  558. * free these descriptors and point next in the ring
  559. */
  560. p_rxdesc_curr->cmd_sts =
  561. MVGBE_BUFFER_OWNED_BY_DMA | MVGBE_RX_EN_INTERRUPT;
  562. p_rxdesc_curr->buf_size = PKTSIZE_ALIGN;
  563. p_rxdesc_curr->byte_cnt = 0;
  564. rxdesc_curr_addr = (u32)&dmvgbe->p_rxdesc_curr;
  565. writel((unsigned)p_rxdesc_curr->nxtdesc_p, rxdesc_curr_addr);
  566. return 0;
  567. }
  568. #if defined(CONFIG_PHYLIB)
  569. int mvgbe_phylib_init(struct eth_device *dev, int phyid)
  570. {
  571. struct mii_dev *bus;
  572. struct phy_device *phydev;
  573. int ret;
  574. bus = mdio_alloc();
  575. if (!bus) {
  576. printf("mdio_alloc failed\n");
  577. return -ENOMEM;
  578. }
  579. bus->read = mvgbe_phy_read;
  580. bus->write = mvgbe_phy_write;
  581. sprintf(bus->name, dev->name);
  582. ret = mdio_register(bus);
  583. if (ret) {
  584. printf("mdio_register failed\n");
  585. free(bus);
  586. return -ENOMEM;
  587. }
  588. /* Set phy address of the port */
  589. mvgbe_phy_write(bus, MV_PHY_ADR_REQUEST, 0, MV_PHY_ADR_REQUEST, phyid);
  590. phydev = phy_connect(bus, phyid, dev, PHY_INTERFACE_MODE_RGMII);
  591. if (!phydev) {
  592. printf("phy_connect failed\n");
  593. return -ENODEV;
  594. }
  595. phy_config(phydev);
  596. phy_startup(phydev);
  597. return 0;
  598. }
  599. #endif
  600. int mvgbe_initialize(bd_t *bis)
  601. {
  602. struct mvgbe_device *dmvgbe;
  603. struct eth_device *dev;
  604. int devnum;
  605. u8 used_ports[MAX_MVGBE_DEVS] = CONFIG_MVGBE_PORTS;
  606. for (devnum = 0; devnum < MAX_MVGBE_DEVS; devnum++) {
  607. /*skip if port is configured not to use */
  608. if (used_ports[devnum] == 0)
  609. continue;
  610. dmvgbe = malloc(sizeof(struct mvgbe_device));
  611. if (!dmvgbe)
  612. goto error1;
  613. memset(dmvgbe, 0, sizeof(struct mvgbe_device));
  614. dmvgbe->p_rxdesc =
  615. (struct mvgbe_rxdesc *)memalign(PKTALIGN,
  616. MV_RXQ_DESC_ALIGNED_SIZE*RINGSZ + 1);
  617. if (!dmvgbe->p_rxdesc)
  618. goto error2;
  619. dmvgbe->p_rxbuf = (u8 *) memalign(PKTALIGN,
  620. RINGSZ*PKTSIZE_ALIGN + 1);
  621. if (!dmvgbe->p_rxbuf)
  622. goto error3;
  623. dmvgbe->p_aligned_txbuf = memalign(8, PKTSIZE_ALIGN);
  624. if (!dmvgbe->p_aligned_txbuf)
  625. goto error4;
  626. dmvgbe->p_txdesc = (struct mvgbe_txdesc *) memalign(
  627. PKTALIGN, sizeof(struct mvgbe_txdesc) + 1);
  628. if (!dmvgbe->p_txdesc) {
  629. free(dmvgbe->p_aligned_txbuf);
  630. error4:
  631. free(dmvgbe->p_rxbuf);
  632. error3:
  633. free(dmvgbe->p_rxdesc);
  634. error2:
  635. free(dmvgbe);
  636. error1:
  637. printf("Err.. %s Failed to allocate memory\n",
  638. __FUNCTION__);
  639. return -1;
  640. }
  641. dev = &dmvgbe->dev;
  642. /* must be less than sizeof(dev->name) */
  643. sprintf(dev->name, "egiga%d", devnum);
  644. switch (devnum) {
  645. case 0:
  646. dmvgbe->regs = (void *)MVGBE0_BASE;
  647. break;
  648. #if defined(MVGBE1_BASE)
  649. case 1:
  650. dmvgbe->regs = (void *)MVGBE1_BASE;
  651. break;
  652. #endif
  653. default: /* this should never happen */
  654. printf("Err..(%s) Invalid device number %d\n",
  655. __FUNCTION__, devnum);
  656. return -1;
  657. }
  658. dev->init = (void *)mvgbe_init;
  659. dev->halt = (void *)mvgbe_halt;
  660. dev->send = (void *)mvgbe_send;
  661. dev->recv = (void *)mvgbe_recv;
  662. dev->write_hwaddr = (void *)mvgbe_write_hwaddr;
  663. eth_register(dev);
  664. #if defined(CONFIG_PHYLIB)
  665. mvgbe_phylib_init(dev, PHY_BASE_ADR + devnum);
  666. #elif defined(CONFIG_MII) || defined(CONFIG_CMD_MII)
  667. miiphy_register(dev->name, smi_reg_read, smi_reg_write);
  668. /* Set phy address of the port */
  669. miiphy_write(dev->name, MV_PHY_ADR_REQUEST,
  670. MV_PHY_ADR_REQUEST, PHY_BASE_ADR + devnum);
  671. #endif
  672. }
  673. return 0;
  674. }