mvgbe.c 20 KB

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