mvgbe.c 20 KB

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