mvgbe.c 21 KB

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