kirkwood_egiga.c 19 KB

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