kirkwood_egiga.c 18 KB

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