mvgbe.c 20 KB

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