armada100_fec.c 19 KB

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