kirkwood_egiga.c 20 KB

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