armada100_fec.c 18 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738
  1. /*
  2. * (C) Copyright 2011
  3. * eInfochips Ltd. <www.einfochips.com>
  4. * Written-by: Ajay Bhargav <contact@8051projects.net>
  5. *
  6. * (C) Copyright 2010
  7. * Marvell Semiconductor <www.marvell.com>
  8. * Contributor: Mahavir Jain <mjain@marvell.com>
  9. *
  10. * SPDX-License-Identifier: GPL-2.0+
  11. */
  12. #include <common.h>
  13. #include <net.h>
  14. #include <malloc.h>
  15. #include <miiphy.h>
  16. #include <netdev.h>
  17. #include <asm/types.h>
  18. #include <asm/byteorder.h>
  19. #include <linux/err.h>
  20. #include <linux/mii.h>
  21. #include <asm/io.h>
  22. #include <asm/arch/armada100.h>
  23. #include "armada100_fec.h"
  24. #define PHY_ADR_REQ 0xFF /* Magic number to read/write PHY address */
  25. #ifdef DEBUG
  26. static int eth_dump_regs(struct eth_device *dev)
  27. {
  28. struct armdfec_device *darmdfec = to_darmdfec(dev);
  29. struct armdfec_reg *regs = darmdfec->regs;
  30. unsigned int i = 0;
  31. printf("\noffset: phy_adr, value: 0x%x\n", readl(&regs->phyadr));
  32. printf("offset: smi, value: 0x%x\n", readl(&regs->smi));
  33. for (i = 0x400; i <= 0x4e4; i += 4)
  34. printf("offset: 0x%x, value: 0x%x\n",
  35. i, readl(ARMD1_FEC_BASE + i));
  36. return 0;
  37. }
  38. #endif
  39. static int armdfec_phy_timeout(u32 *reg, u32 flag, int cond)
  40. {
  41. u32 timeout = PHY_WAIT_ITERATIONS;
  42. u32 reg_val;
  43. while (--timeout) {
  44. reg_val = readl(reg);
  45. if (cond && (reg_val & flag))
  46. break;
  47. else if (!cond && !(reg_val & flag))
  48. break;
  49. udelay(PHY_WAIT_MICRO_SECONDS);
  50. }
  51. return !timeout;
  52. }
  53. static int smi_reg_read(struct mii_dev *bus, int phy_addr, int devad,
  54. int phy_reg)
  55. {
  56. u16 value = 0;
  57. struct eth_device *dev = eth_get_dev_by_name(bus->name);
  58. struct armdfec_device *darmdfec = to_darmdfec(dev);
  59. struct armdfec_reg *regs = darmdfec->regs;
  60. u32 val;
  61. if (phy_addr == PHY_ADR_REQ && phy_reg == PHY_ADR_REQ) {
  62. val = readl(&regs->phyadr);
  63. value = val & 0x1f;
  64. return value;
  65. }
  66. /* check parameters */
  67. if (phy_addr > PHY_MASK) {
  68. printf("ARMD100 FEC: (%s) Invalid phy address: 0x%X\n",
  69. __func__, phy_addr);
  70. return -EINVAL;
  71. }
  72. if (phy_reg > PHY_MASK) {
  73. printf("ARMD100 FEC: (%s) Invalid register offset: 0x%X\n",
  74. __func__, phy_reg);
  75. return -EINVAL;
  76. }
  77. /* wait for the SMI register to become available */
  78. if (armdfec_phy_timeout(&regs->smi, SMI_BUSY, false)) {
  79. printf("ARMD100 FEC: (%s) PHY busy timeout\n", __func__);
  80. return -1;
  81. }
  82. writel((phy_addr << 16) | (phy_reg << 21) | SMI_OP_R, &regs->smi);
  83. /* now wait for the data to be valid */
  84. if (armdfec_phy_timeout(&regs->smi, SMI_R_VALID, true)) {
  85. val = readl(&regs->smi);
  86. printf("ARMD100 FEC: (%s) PHY Read timeout, val=0x%x\n",
  87. __func__, val);
  88. return -1;
  89. }
  90. val = readl(&regs->smi);
  91. value = val & 0xffff;
  92. return value;
  93. }
  94. static int smi_reg_write(struct mii_dev *bus, int phy_addr, int devad,
  95. int phy_reg, u16 value)
  96. {
  97. struct eth_device *dev = eth_get_dev_by_name(bus->name);
  98. struct armdfec_device *darmdfec = to_darmdfec(dev);
  99. struct armdfec_reg *regs = darmdfec->regs;
  100. if (phy_addr == PHY_ADR_REQ && phy_reg == PHY_ADR_REQ) {
  101. clrsetbits_le32(&regs->phyadr, 0x1f, value & 0x1f);
  102. return 0;
  103. }
  104. /* check parameters */
  105. if (phy_addr > PHY_MASK) {
  106. printf("ARMD100 FEC: (%s) Invalid phy address\n", __func__);
  107. return -EINVAL;
  108. }
  109. if (phy_reg > PHY_MASK) {
  110. printf("ARMD100 FEC: (%s) Invalid register offset\n", __func__);
  111. return -EINVAL;
  112. }
  113. /* wait for the SMI register to become available */
  114. if (armdfec_phy_timeout(&regs->smi, SMI_BUSY, false)) {
  115. printf("ARMD100 FEC: (%s) PHY busy timeout\n", __func__);
  116. return -1;
  117. }
  118. writel((phy_addr << 16) | (phy_reg << 21) | SMI_OP_W | (value & 0xffff),
  119. &regs->smi);
  120. return 0;
  121. }
  122. /*
  123. * Abort any transmit and receive operations and put DMA
  124. * in idle state. AT and AR bits are cleared upon entering
  125. * in IDLE state. So poll those bits to verify operation.
  126. */
  127. static void abortdma(struct eth_device *dev)
  128. {
  129. struct armdfec_device *darmdfec = to_darmdfec(dev);
  130. struct armdfec_reg *regs = darmdfec->regs;
  131. int delay;
  132. int maxretries = 40;
  133. u32 tmp;
  134. while (--maxretries) {
  135. writel(SDMA_CMD_AR | SDMA_CMD_AT, &regs->sdma_cmd);
  136. udelay(100);
  137. delay = 10;
  138. while (--delay) {
  139. tmp = readl(&regs->sdma_cmd);
  140. if (!(tmp & (SDMA_CMD_AR | SDMA_CMD_AT)))
  141. break;
  142. udelay(10);
  143. }
  144. if (delay)
  145. break;
  146. }
  147. if (!maxretries)
  148. printf("ARMD100 FEC: (%s) DMA Stuck\n", __func__);
  149. }
  150. static inline u32 nibble_swapping_32_bit(u32 x)
  151. {
  152. return ((x & 0xf0f0f0f0) >> 4) | ((x & 0x0f0f0f0f) << 4);
  153. }
  154. static inline u32 nibble_swapping_16_bit(u32 x)
  155. {
  156. return ((x & 0x0000f0f0) >> 4) | ((x & 0x00000f0f) << 4);
  157. }
  158. static inline u32 flip_4_bits(u32 x)
  159. {
  160. return ((x & 0x01) << 3) | ((x & 0x002) << 1)
  161. | ((x & 0x04) >> 1) | ((x & 0x008) >> 3);
  162. }
  163. /*
  164. * This function will calculate the hash function of the address.
  165. * depends on the hash mode and hash size.
  166. * Inputs
  167. * mach - the 2 most significant bytes of the MAC address.
  168. * macl - the 4 least significant bytes of the MAC address.
  169. * Outputs
  170. * return the calculated entry.
  171. */
  172. static u32 hash_function(u32 mach, u32 macl)
  173. {
  174. u32 hashresult;
  175. u32 addrh;
  176. u32 addrl;
  177. u32 addr0;
  178. u32 addr1;
  179. u32 addr2;
  180. u32 addr3;
  181. u32 addrhswapped;
  182. u32 addrlswapped;
  183. addrh = nibble_swapping_16_bit(mach);
  184. addrl = nibble_swapping_32_bit(macl);
  185. addrhswapped = flip_4_bits(addrh & 0xf)
  186. + ((flip_4_bits((addrh >> 4) & 0xf)) << 4)
  187. + ((flip_4_bits((addrh >> 8) & 0xf)) << 8)
  188. + ((flip_4_bits((addrh >> 12) & 0xf)) << 12);
  189. addrlswapped = flip_4_bits(addrl & 0xf)
  190. + ((flip_4_bits((addrl >> 4) & 0xf)) << 4)
  191. + ((flip_4_bits((addrl >> 8) & 0xf)) << 8)
  192. + ((flip_4_bits((addrl >> 12) & 0xf)) << 12)
  193. + ((flip_4_bits((addrl >> 16) & 0xf)) << 16)
  194. + ((flip_4_bits((addrl >> 20) & 0xf)) << 20)
  195. + ((flip_4_bits((addrl >> 24) & 0xf)) << 24)
  196. + ((flip_4_bits((addrl >> 28) & 0xf)) << 28);
  197. addrh = addrhswapped;
  198. addrl = addrlswapped;
  199. addr0 = (addrl >> 2) & 0x03f;
  200. addr1 = (addrl & 0x003) | (((addrl >> 8) & 0x7f) << 2);
  201. addr2 = (addrl >> 15) & 0x1ff;
  202. addr3 = ((addrl >> 24) & 0x0ff) | ((addrh & 1) << 8);
  203. hashresult = (addr0 << 9) | (addr1 ^ addr2 ^ addr3);
  204. hashresult = hashresult & 0x07ff;
  205. return hashresult;
  206. }
  207. /*
  208. * This function will add an entry to the address table.
  209. * depends on the hash mode and hash size that was initialized.
  210. * Inputs
  211. * mach - the 2 most significant bytes of the MAC address.
  212. * macl - the 4 least significant bytes of the MAC address.
  213. * skip - if 1, skip this address.
  214. * rd - the RD field in the address table.
  215. * Outputs
  216. * address table entry is added.
  217. * 0 if success.
  218. * -ENOSPC if table full
  219. */
  220. static int add_del_hash_entry(struct armdfec_device *darmdfec, u32 mach,
  221. u32 macl, u32 rd, u32 skip, int del)
  222. {
  223. struct addr_table_entry_t *entry, *start;
  224. u32 newhi;
  225. u32 newlo;
  226. u32 i;
  227. newlo = (((mach >> 4) & 0xf) << 15)
  228. | (((mach >> 0) & 0xf) << 11)
  229. | (((mach >> 12) & 0xf) << 7)
  230. | (((mach >> 8) & 0xf) << 3)
  231. | (((macl >> 20) & 0x1) << 31)
  232. | (((macl >> 16) & 0xf) << 27)
  233. | (((macl >> 28) & 0xf) << 23)
  234. | (((macl >> 24) & 0xf) << 19)
  235. | (skip << HTESKIP) | (rd << HTERDBIT)
  236. | HTEVALID;
  237. newhi = (((macl >> 4) & 0xf) << 15)
  238. | (((macl >> 0) & 0xf) << 11)
  239. | (((macl >> 12) & 0xf) << 7)
  240. | (((macl >> 8) & 0xf) << 3)
  241. | (((macl >> 21) & 0x7) << 0);
  242. /*
  243. * Pick the appropriate table, start scanning for free/reusable
  244. * entries at the index obtained by hashing the specified MAC address
  245. */
  246. start = (struct addr_table_entry_t *)(darmdfec->htpr);
  247. entry = start + hash_function(mach, macl);
  248. for (i = 0; i < HOP_NUMBER; i++) {
  249. if (!(entry->lo & HTEVALID)) {
  250. break;
  251. } else {
  252. /* if same address put in same position */
  253. if (((entry->lo & 0xfffffff8) == (newlo & 0xfffffff8))
  254. && (entry->hi == newhi))
  255. break;
  256. }
  257. if (entry == start + 0x7ff)
  258. entry = start;
  259. else
  260. entry++;
  261. }
  262. if (((entry->lo & 0xfffffff8) != (newlo & 0xfffffff8)) &&
  263. (entry->hi != newhi) && del)
  264. return 0;
  265. if (i == HOP_NUMBER) {
  266. if (!del) {
  267. printf("ARMD100 FEC: (%s) table section is full\n",
  268. __func__);
  269. return -ENOSPC;
  270. } else {
  271. return 0;
  272. }
  273. }
  274. /*
  275. * Update the selected entry
  276. */
  277. if (del) {
  278. entry->hi = 0;
  279. entry->lo = 0;
  280. } else {
  281. entry->hi = newhi;
  282. entry->lo = newlo;
  283. }
  284. return 0;
  285. }
  286. /*
  287. * Create an addressTable entry from MAC address info
  288. * found in the specifed net_device struct
  289. *
  290. * Input : pointer to ethernet interface network device structure
  291. * Output : N/A
  292. */
  293. static void update_hash_table_mac_address(struct armdfec_device *darmdfec,
  294. u8 *oaddr, u8 *addr)
  295. {
  296. u32 mach;
  297. u32 macl;
  298. /* Delete old entry */
  299. if (oaddr) {
  300. mach = (oaddr[0] << 8) | oaddr[1];
  301. macl = (oaddr[2] << 24) | (oaddr[3] << 16) |
  302. (oaddr[4] << 8) | oaddr[5];
  303. add_del_hash_entry(darmdfec, mach, macl, 1, 0, HASH_DELETE);
  304. }
  305. /* Add new entry */
  306. mach = (addr[0] << 8) | addr[1];
  307. macl = (addr[2] << 24) | (addr[3] << 16) | (addr[4] << 8) | addr[5];
  308. add_del_hash_entry(darmdfec, mach, macl, 1, 0, HASH_ADD);
  309. }
  310. /* Address Table Initialization */
  311. static void init_hashtable(struct eth_device *dev)
  312. {
  313. struct armdfec_device *darmdfec = to_darmdfec(dev);
  314. struct armdfec_reg *regs = darmdfec->regs;
  315. memset(darmdfec->htpr, 0, HASH_ADDR_TABLE_SIZE);
  316. writel((u32)darmdfec->htpr, &regs->htpr);
  317. }
  318. /*
  319. * This detects PHY chip from address 0-31 by reading PHY status
  320. * registers. PHY chip can be connected at any of this address.
  321. */
  322. static int ethernet_phy_detect(struct eth_device *dev)
  323. {
  324. u32 val;
  325. u16 tmp, mii_status;
  326. u8 addr;
  327. for (addr = 0; addr < 32; addr++) {
  328. if (miiphy_read(dev->name, addr, MII_BMSR, &mii_status) != 0)
  329. /* try next phy */
  330. continue;
  331. /* invalid MII status. More validation required here... */
  332. if (mii_status == 0 || mii_status == 0xffff)
  333. /* try next phy */
  334. continue;
  335. if (miiphy_read(dev->name, addr, MII_PHYSID1, &tmp) != 0)
  336. /* try next phy */
  337. continue;
  338. val = tmp << 16;
  339. if (miiphy_read(dev->name, addr, MII_PHYSID2, &tmp) != 0)
  340. /* try next phy */
  341. continue;
  342. val |= tmp;
  343. if ((val & 0xfffffff0) != 0)
  344. return addr;
  345. }
  346. return -1;
  347. }
  348. static void armdfec_init_rx_desc_ring(struct armdfec_device *darmdfec)
  349. {
  350. struct rx_desc *p_rx_desc;
  351. int i;
  352. /* initialize the Rx descriptors ring */
  353. p_rx_desc = darmdfec->p_rxdesc;
  354. for (i = 0; i < RINGSZ; i++) {
  355. p_rx_desc->cmd_sts = BUF_OWNED_BY_DMA | RX_EN_INT;
  356. p_rx_desc->buf_size = PKTSIZE_ALIGN;
  357. p_rx_desc->byte_cnt = 0;
  358. p_rx_desc->buf_ptr = darmdfec->p_rxbuf + i * PKTSIZE_ALIGN;
  359. if (i == (RINGSZ - 1)) {
  360. p_rx_desc->nxtdesc_p = darmdfec->p_rxdesc;
  361. } else {
  362. p_rx_desc->nxtdesc_p = (struct rx_desc *)
  363. ((u32)p_rx_desc + ARMDFEC_RXQ_DESC_ALIGNED_SIZE);
  364. p_rx_desc = p_rx_desc->nxtdesc_p;
  365. }
  366. }
  367. darmdfec->p_rxdesc_curr = darmdfec->p_rxdesc;
  368. }
  369. static int armdfec_init(struct eth_device *dev, bd_t *bd)
  370. {
  371. struct armdfec_device *darmdfec = to_darmdfec(dev);
  372. struct armdfec_reg *regs = darmdfec->regs;
  373. int phy_adr;
  374. u32 temp;
  375. armdfec_init_rx_desc_ring(darmdfec);
  376. /* Disable interrupts */
  377. writel(0, &regs->im);
  378. writel(0, &regs->ic);
  379. /* Write to ICR to clear interrupts. */
  380. writel(0, &regs->iwc);
  381. /*
  382. * Abort any transmit and receive operations and put DMA
  383. * in idle state.
  384. */
  385. abortdma(dev);
  386. /* Initialize address hash table */
  387. init_hashtable(dev);
  388. /* SDMA configuration */
  389. writel(SDCR_BSZ8 | /* Burst size = 32 bytes */
  390. SDCR_RIFB | /* Rx interrupt on frame */
  391. SDCR_BLMT | /* Little endian transmit */
  392. SDCR_BLMR | /* Little endian receive */
  393. SDCR_RC_MAX_RETRANS, /* Max retransmit count */
  394. &regs->sdma_conf);
  395. /* Port Configuration */
  396. writel(PCR_HS, &regs->pconf); /* Hash size is 1/2kb */
  397. /* Set extended port configuration */
  398. writel(PCXR_2BSM | /* Two byte suffix aligns IP hdr */
  399. PCXR_DSCP_EN | /* Enable DSCP in IP */
  400. PCXR_MFL_1536 | /* Set MTU = 1536 */
  401. PCXR_FLP | /* do not force link pass */
  402. PCXR_TX_HIGH_PRI, /* Transmit - high priority queue */
  403. &regs->pconf_ext);
  404. update_hash_table_mac_address(darmdfec, NULL, dev->enetaddr);
  405. /* Update TX and RX queue descriptor register */
  406. temp = (u32)&regs->txcdp[TXQ];
  407. writel((u32)darmdfec->p_txdesc, temp);
  408. temp = (u32)&regs->rxfdp[RXQ];
  409. writel((u32)darmdfec->p_rxdesc, temp);
  410. temp = (u32)&regs->rxcdp[RXQ];
  411. writel((u32)darmdfec->p_rxdesc_curr, temp);
  412. /* Enable Interrupts */
  413. writel(ALL_INTS, &regs->im);
  414. /* Enable Ethernet Port */
  415. setbits_le32(&regs->pconf, PCR_EN);
  416. /* Enable RX DMA engine */
  417. setbits_le32(&regs->sdma_cmd, SDMA_CMD_ERD);
  418. #ifdef DEBUG
  419. eth_dump_regs(dev);
  420. #endif
  421. #if (defined(CONFIG_MII) || defined(CONFIG_CMD_MII))
  422. #if defined(CONFIG_PHY_BASE_ADR)
  423. miiphy_write(dev->name, PHY_ADR_REQ, PHY_ADR_REQ, CONFIG_PHY_BASE_ADR);
  424. #else
  425. /* Search phy address from range 0-31 */
  426. phy_adr = ethernet_phy_detect(dev);
  427. if (phy_adr < 0) {
  428. printf("ARMD100 FEC: PHY not detected at address range 0-31\n");
  429. return -1;
  430. } else {
  431. debug("ARMD100 FEC: PHY detected at addr %d\n", phy_adr);
  432. miiphy_write(dev->name, PHY_ADR_REQ, PHY_ADR_REQ, phy_adr);
  433. }
  434. #endif
  435. #if defined(CONFIG_SYS_FAULT_ECHO_LINK_DOWN)
  436. /* Wait up to 5s for the link status */
  437. for (i = 0; i < 5; i++) {
  438. u16 phy_adr;
  439. miiphy_read(dev->name, 0xFF, 0xFF, &phy_adr);
  440. /* Return if we get link up */
  441. if (miiphy_link(dev->name, phy_adr))
  442. return 0;
  443. udelay(1000000);
  444. }
  445. printf("ARMD100 FEC: No link on %s\n", dev->name);
  446. return -1;
  447. #endif
  448. #endif
  449. return 0;
  450. }
  451. static void armdfec_halt(struct eth_device *dev)
  452. {
  453. struct armdfec_device *darmdfec = to_darmdfec(dev);
  454. struct armdfec_reg *regs = darmdfec->regs;
  455. /* Stop RX DMA */
  456. clrbits_le32(&regs->sdma_cmd, SDMA_CMD_ERD);
  457. /*
  458. * Abort any transmit and receive operations and put DMA
  459. * in idle state.
  460. */
  461. abortdma(dev);
  462. /* Disable interrupts */
  463. writel(0, &regs->im);
  464. writel(0, &regs->ic);
  465. writel(0, &regs->iwc);
  466. /* Disable Port */
  467. clrbits_le32(&regs->pconf, PCR_EN);
  468. }
  469. static int armdfec_send(struct eth_device *dev, void *dataptr, int datasize)
  470. {
  471. struct armdfec_device *darmdfec = to_darmdfec(dev);
  472. struct armdfec_reg *regs = darmdfec->regs;
  473. struct tx_desc *p_txdesc = darmdfec->p_txdesc;
  474. void *p = (void *)dataptr;
  475. int retry = PHY_WAIT_ITERATIONS * PHY_WAIT_MICRO_SECONDS;
  476. u32 cmd_sts, temp;
  477. /* Copy buffer if it's misaligned */
  478. if ((u32)dataptr & 0x07) {
  479. if (datasize > PKTSIZE_ALIGN) {
  480. printf("ARMD100 FEC: Non-aligned data too large (%d)\n",
  481. datasize);
  482. return -1;
  483. }
  484. memcpy(darmdfec->p_aligned_txbuf, p, datasize);
  485. p = darmdfec->p_aligned_txbuf;
  486. }
  487. p_txdesc->cmd_sts = TX_ZERO_PADDING | TX_GEN_CRC;
  488. p_txdesc->cmd_sts |= TX_FIRST_DESC | TX_LAST_DESC;
  489. p_txdesc->cmd_sts |= BUF_OWNED_BY_DMA;
  490. p_txdesc->cmd_sts |= TX_EN_INT;
  491. p_txdesc->buf_ptr = p;
  492. p_txdesc->byte_cnt = datasize;
  493. /* Apply send command using high priority TX queue */
  494. temp = (u32)&regs->txcdp[TXQ];
  495. writel((u32)p_txdesc, temp);
  496. writel(SDMA_CMD_TXDL | SDMA_CMD_TXDH | SDMA_CMD_ERD, &regs->sdma_cmd);
  497. /*
  498. * wait for packet xmit completion
  499. */
  500. cmd_sts = readl(&p_txdesc->cmd_sts);
  501. while (cmd_sts & BUF_OWNED_BY_DMA) {
  502. /* return fail if error is detected */
  503. if ((cmd_sts & (TX_ERROR | TX_LAST_DESC)) ==
  504. (TX_ERROR | TX_LAST_DESC)) {
  505. printf("ARMD100 FEC: (%s) in xmit packet\n", __func__);
  506. return -1;
  507. }
  508. cmd_sts = readl(&p_txdesc->cmd_sts);
  509. if (!(retry--)) {
  510. printf("ARMD100 FEC: (%s) xmit packet timeout!\n",
  511. __func__);
  512. return -1;
  513. }
  514. }
  515. return 0;
  516. }
  517. static int armdfec_recv(struct eth_device *dev)
  518. {
  519. struct armdfec_device *darmdfec = to_darmdfec(dev);
  520. struct rx_desc *p_rxdesc_curr = darmdfec->p_rxdesc_curr;
  521. u32 cmd_sts;
  522. u32 timeout = 0;
  523. u32 temp;
  524. /* wait untill rx packet available or timeout */
  525. do {
  526. if (timeout < PHY_WAIT_ITERATIONS * PHY_WAIT_MICRO_SECONDS) {
  527. timeout++;
  528. } else {
  529. debug("ARMD100 FEC: %s time out...\n", __func__);
  530. return -1;
  531. }
  532. } while (readl(&p_rxdesc_curr->cmd_sts) & BUF_OWNED_BY_DMA);
  533. if (p_rxdesc_curr->byte_cnt != 0) {
  534. debug("ARMD100 FEC: %s: Received %d byte Packet @ 0x%x"
  535. "(cmd_sts= %08x)\n", __func__,
  536. (u32)p_rxdesc_curr->byte_cnt,
  537. (u32)p_rxdesc_curr->buf_ptr,
  538. (u32)p_rxdesc_curr->cmd_sts);
  539. }
  540. /*
  541. * In case received a packet without first/last bits on
  542. * OR the error summary bit is on,
  543. * the packets needs to be dropeed.
  544. */
  545. cmd_sts = readl(&p_rxdesc_curr->cmd_sts);
  546. if ((cmd_sts & (RX_FIRST_DESC | RX_LAST_DESC)) !=
  547. (RX_FIRST_DESC | RX_LAST_DESC)) {
  548. printf("ARMD100 FEC: (%s) Dropping packet spread on"
  549. " multiple descriptors\n", __func__);
  550. } else if (cmd_sts & RX_ERROR) {
  551. printf("ARMD100 FEC: (%s) Dropping packet with errors\n",
  552. __func__);
  553. } else {
  554. /* !!! call higher layer processing */
  555. debug("ARMD100 FEC: (%s) Sending Received packet to"
  556. " upper layer (net_process_received_packet)\n", __func__);
  557. /*
  558. * let the upper layer handle the packet, subtract offset
  559. * as two dummy bytes are added in received buffer see
  560. * PORT_CONFIG_EXT register bit TWO_Byte_Stuff_Mode bit.
  561. */
  562. net_process_received_packet(
  563. p_rxdesc_curr->buf_ptr + RX_BUF_OFFSET,
  564. (int)(p_rxdesc_curr->byte_cnt - RX_BUF_OFFSET));
  565. }
  566. /*
  567. * free these descriptors and point next in the ring
  568. */
  569. p_rxdesc_curr->cmd_sts = BUF_OWNED_BY_DMA | RX_EN_INT;
  570. p_rxdesc_curr->buf_size = PKTSIZE_ALIGN;
  571. p_rxdesc_curr->byte_cnt = 0;
  572. temp = (u32)&darmdfec->p_rxdesc_curr;
  573. writel((u32)p_rxdesc_curr->nxtdesc_p, temp);
  574. return 0;
  575. }
  576. int armada100_fec_register(unsigned long base_addr)
  577. {
  578. struct armdfec_device *darmdfec;
  579. struct eth_device *dev;
  580. darmdfec = malloc(sizeof(struct armdfec_device));
  581. if (!darmdfec)
  582. goto error;
  583. memset(darmdfec, 0, sizeof(struct armdfec_device));
  584. darmdfec->htpr = memalign(8, HASH_ADDR_TABLE_SIZE);
  585. if (!darmdfec->htpr)
  586. goto error1;
  587. darmdfec->p_rxdesc = memalign(PKTALIGN,
  588. ARMDFEC_RXQ_DESC_ALIGNED_SIZE * RINGSZ + 1);
  589. if (!darmdfec->p_rxdesc)
  590. goto error1;
  591. darmdfec->p_rxbuf = memalign(PKTALIGN, RINGSZ * PKTSIZE_ALIGN + 1);
  592. if (!darmdfec->p_rxbuf)
  593. goto error1;
  594. darmdfec->p_aligned_txbuf = memalign(8, PKTSIZE_ALIGN);
  595. if (!darmdfec->p_aligned_txbuf)
  596. goto error1;
  597. darmdfec->p_txdesc = memalign(PKTALIGN, sizeof(struct tx_desc) + 1);
  598. if (!darmdfec->p_txdesc)
  599. goto error1;
  600. dev = &darmdfec->dev;
  601. /* Assign ARMADA100 Fast Ethernet Controller Base Address */
  602. darmdfec->regs = (void *)base_addr;
  603. /* must be less than sizeof(dev->name) */
  604. strcpy(dev->name, "armd-fec0");
  605. dev->init = armdfec_init;
  606. dev->halt = armdfec_halt;
  607. dev->send = armdfec_send;
  608. dev->recv = armdfec_recv;
  609. eth_register(dev);
  610. #if defined(CONFIG_MII) || defined(CONFIG_CMD_MII)
  611. int retval;
  612. struct mii_dev *mdiodev = mdio_alloc();
  613. if (!mdiodev)
  614. return -ENOMEM;
  615. strncpy(mdiodev->name, dev->name, MDIO_NAME_LEN);
  616. mdiodev->read = smi_reg_read;
  617. mdiodev->write = smi_reg_write;
  618. retval = mdio_register(mdiodev);
  619. if (retval < 0)
  620. return retval;
  621. #endif
  622. return 0;
  623. error1:
  624. free(darmdfec->p_aligned_txbuf);
  625. free(darmdfec->p_rxbuf);
  626. free(darmdfec->p_rxdesc);
  627. free(darmdfec->htpr);
  628. error:
  629. free(darmdfec);
  630. printf("AMD100 FEC: (%s) Failed to allocate memory\n", __func__);
  631. return -1;
  632. }