mvgbe.c 20 KB

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