mvgbe.c 21 KB

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