sunxi_wemac.c 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533
  1. /*
  2. * sunxi_wemac.c -- Allwinner A10 ethernet driver
  3. *
  4. * (C) Copyright 2012, Stefan Roese <sr@denx.de>
  5. *
  6. * This program is free software; you can redistribute it and/or modify
  7. * it under the terms of the GNU General Public License as published by
  8. * the Free Software Foundation; either version 2 of the License, or
  9. * (at your option) any later version.
  10. *
  11. * This program is distributed in the hope that it will be useful,
  12. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  13. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  14. * GNU General Public License for more details.
  15. */
  16. #include <common.h>
  17. #include <malloc.h>
  18. #include <net.h>
  19. #include <miiphy.h>
  20. #include <linux/err.h>
  21. #include <asm/io.h>
  22. #include <asm/arch/clock.h>
  23. #include <asm/arch/gpio.h>
  24. /* EMAC register */
  25. struct wemac_regs {
  26. u32 ctl; /* 0x00 */
  27. u32 tx_mode; /* 0x04 */
  28. u32 tx_flow; /* 0x08 */
  29. u32 tx_ctl0; /* 0x0c */
  30. u32 tx_ctl1; /* 0x10 */
  31. u32 tx_ins; /* 0x14 */
  32. u32 tx_pl0; /* 0x18 */
  33. u32 tx_pl1; /* 0x1c */
  34. u32 tx_sta; /* 0x20 */
  35. u32 tx_io_data; /* 0x24 */
  36. u32 tx_io_data1; /* 0x28 */
  37. u32 tx_tsvl0; /* 0x2c */
  38. u32 tx_tsvh0; /* 0x30 */
  39. u32 tx_tsvl1; /* 0x34 */
  40. u32 tx_tsvh1; /* 0x38 */
  41. u32 rx_ctl; /* 0x3c */
  42. u32 rx_hash0; /* 0x40 */
  43. u32 rx_hash1; /* 0x44 */
  44. u32 rx_sta; /* 0x48 */
  45. u32 rx_io_data; /* 0x4c */
  46. u32 rx_fbc; /* 0x50 */
  47. u32 int_ctl; /* 0x54 */
  48. u32 int_sta; /* 0x58 */
  49. u32 mac_ctl0; /* 0x5c */
  50. u32 mac_ctl1; /* 0x60 */
  51. u32 mac_ipgt; /* 0x64 */
  52. u32 mac_ipgr; /* 0x68 */
  53. u32 mac_clrt; /* 0x6c */
  54. u32 mac_maxf; /* 0x70 */
  55. u32 mac_supp; /* 0x74 */
  56. u32 mac_test; /* 0x78 */
  57. u32 mac_mcfg; /* 0x7c */
  58. u32 mac_mcmd; /* 0x80 */
  59. u32 mac_madr; /* 0x84 */
  60. u32 mac_mwtd; /* 0x88 */
  61. u32 mac_mrdd; /* 0x8c */
  62. u32 mac_mind; /* 0x90 */
  63. u32 mac_ssrr; /* 0x94 */
  64. u32 mac_a0; /* 0x98 */
  65. u32 mac_a1; /* 0x9c */
  66. };
  67. /* SRAMC register */
  68. struct sunxi_sramc_regs {
  69. u32 ctrl0;
  70. u32 ctrl1;
  71. };
  72. /* 0: Disable 1: Aborted frame enable(default) */
  73. #define EMAC_TX_AB_M (0x1 << 0)
  74. /* 0: CPU 1: DMA(default) */
  75. #define EMAC_TX_TM (0x1 << 1)
  76. #define EMAC_TX_SETUP (0)
  77. /* 0: DRQ asserted 1: DRQ automatically(default) */
  78. #define EMAC_RX_DRQ_MODE (0x1 << 1)
  79. /* 0: CPU 1: DMA(default) */
  80. #define EMAC_RX_TM (0x1 << 2)
  81. /* 0: Normal(default) 1: Pass all Frames */
  82. #define EMAC_RX_PA (0x1 << 4)
  83. /* 0: Normal(default) 1: Pass Control Frames */
  84. #define EMAC_RX_PCF (0x1 << 5)
  85. /* 0: Normal(default) 1: Pass Frames with CRC Error */
  86. #define EMAC_RX_PCRCE (0x1 << 6)
  87. /* 0: Normal(default) 1: Pass Frames with Length Error */
  88. #define EMAC_RX_PLE (0x1 << 7)
  89. /* 0: Normal 1: Pass Frames length out of range(default) */
  90. #define EMAC_RX_POR (0x1 << 8)
  91. /* 0: Not accept 1: Accept unicast Packets(default) */
  92. #define EMAC_RX_UCAD (0x1 << 16)
  93. /* 0: Normal(default) 1: DA Filtering */
  94. #define EMAC_RX_DAF (0x1 << 17)
  95. /* 0: Not accept 1: Accept multicast Packets(default) */
  96. #define EMAC_RX_MCO (0x1 << 20)
  97. /* 0: Disable(default) 1: Enable Hash filter */
  98. #define EMAC_RX_MHF (0x1 << 21)
  99. /* 0: Not accept 1: Accept Broadcast Packets(default) */
  100. #define EMAC_RX_BCO (0x1 << 22)
  101. /* 0: Disable(default) 1: Enable SA Filtering */
  102. #define EMAC_RX_SAF (0x1 << 24)
  103. /* 0: Normal(default) 1: Inverse Filtering */
  104. #define EMAC_RX_SAIF (0x1 << 25)
  105. #define EMAC_RX_SETUP (EMAC_RX_POR | EMAC_RX_UCAD | EMAC_RX_DAF | \
  106. EMAC_RX_MCO | EMAC_RX_BCO)
  107. /* 0: Disable 1: Enable Receive Flow Control(default) */
  108. #define EMAC_MAC_CTL0_RFC (0x1 << 2)
  109. /* 0: Disable 1: Enable Transmit Flow Control(default) */
  110. #define EMAC_MAC_CTL0_TFC (0x1 << 3)
  111. #define EMAC_MAC_CTL0_SETUP (EMAC_MAC_CTL0_RFC | EMAC_MAC_CTL0_TFC)
  112. /* 0: Disable 1: Enable MAC Frame Length Checking(default) */
  113. #define EMAC_MAC_CTL1_FLC (0x1 << 1)
  114. /* 0: Disable(default) 1: Enable Huge Frame */
  115. #define EMAC_MAC_CTL1_HF (0x1 << 2)
  116. /* 0: Disable(default) 1: Enable MAC Delayed CRC */
  117. #define EMAC_MAC_CTL1_DCRC (0x1 << 3)
  118. /* 0: Disable 1: Enable MAC CRC(default) */
  119. #define EMAC_MAC_CTL1_CRC (0x1 << 4)
  120. /* 0: Disable 1: Enable MAC PAD Short frames(default) */
  121. #define EMAC_MAC_CTL1_PC (0x1 << 5)
  122. /* 0: Disable(default) 1: Enable MAC PAD Short frames and append CRC */
  123. #define EMAC_MAC_CTL1_VC (0x1 << 6)
  124. /* 0: Disable(default) 1: Enable MAC auto detect Short frames */
  125. #define EMAC_MAC_CTL1_ADP (0x1 << 7)
  126. /* 0: Disable(default) 1: Enable */
  127. #define EMAC_MAC_CTL1_PRE (0x1 << 8)
  128. /* 0: Disable(default) 1: Enable */
  129. #define EMAC_MAC_CTL1_LPE (0x1 << 9)
  130. /* 0: Disable(default) 1: Enable no back off */
  131. #define EMAC_MAC_CTL1_NB (0x1 << 12)
  132. /* 0: Disable(default) 1: Enable */
  133. #define EMAC_MAC_CTL1_BNB (0x1 << 13)
  134. /* 0: Disable(default) 1: Enable */
  135. #define EMAC_MAC_CTL1_ED (0x1 << 14)
  136. #define EMAC_MAC_CTL1_SETUP (EMAC_MAC_CTL1_FLC | EMAC_MAC_CTL1_CRC | \
  137. EMAC_MAC_CTL1_PC)
  138. #define EMAC_MAC_IPGT 0x15
  139. #define EMAC_MAC_NBTB_IPG1 0xC
  140. #define EMAC_MAC_NBTB_IPG2 0x12
  141. #define EMAC_MAC_CW 0x37
  142. #define EMAC_MAC_RM 0xF
  143. #define EMAC_MAC_MFL 0x0600
  144. /* Receive status */
  145. #define EMAC_CRCERR (1 << 4)
  146. #define EMAC_LENERR (3 << 5)
  147. #define DMA_CPU_TRRESHOLD 2000
  148. struct wemac_eth_dev {
  149. u32 speed;
  150. u32 duplex;
  151. u32 phy_configured;
  152. int link_printed;
  153. };
  154. struct wemac_rxhdr {
  155. s16 rx_len;
  156. u16 rx_status;
  157. };
  158. static void wemac_inblk_32bit(void *reg, void *data, int count)
  159. {
  160. int cnt = (count + 3) >> 2;
  161. if (cnt) {
  162. u32 *buf = data;
  163. do {
  164. u32 x = readl(reg);
  165. *buf++ = x;
  166. } while (--cnt);
  167. }
  168. }
  169. static void wemac_outblk_32bit(void *reg, void *data, int count)
  170. {
  171. int cnt = (count + 3) >> 2;
  172. if (cnt) {
  173. const u32 *buf = data;
  174. do {
  175. writel(*buf++, reg);
  176. } while (--cnt);
  177. }
  178. }
  179. /*
  180. * Read a word from phyxcer
  181. */
  182. static int wemac_phy_read(const char *devname, unsigned char addr,
  183. unsigned char reg, unsigned short *value)
  184. {
  185. struct eth_device *dev = eth_get_dev_by_name(devname);
  186. struct wemac_regs *regs = (struct wemac_regs *)dev->iobase;
  187. /* issue the phy address and reg */
  188. writel(addr << 8 | reg, &regs->mac_madr);
  189. /* pull up the phy io line */
  190. writel(0x1, &regs->mac_mcmd);
  191. /* Wait read complete */
  192. mdelay(1);
  193. /* push down the phy io line */
  194. writel(0x0, &regs->mac_mcmd);
  195. /* and write data */
  196. *value = readl(&regs->mac_mrdd);
  197. return 0;
  198. }
  199. /*
  200. * Write a word to phyxcer
  201. */
  202. static int wemac_phy_write(const char *devname, unsigned char addr,
  203. unsigned char reg, unsigned short value)
  204. {
  205. struct eth_device *dev = eth_get_dev_by_name(devname);
  206. struct wemac_regs *regs = (struct wemac_regs *)dev->iobase;
  207. /* issue the phy address and reg */
  208. writel(addr << 8 | reg, &regs->mac_madr);
  209. /* pull up the phy io line */
  210. writel(0x1, &regs->mac_mcmd);
  211. /* Wait write complete */
  212. mdelay(1);
  213. /* push down the phy io line */
  214. writel(0x0, &regs->mac_mcmd);
  215. /* and write data */
  216. writel(value, &regs->mac_mwtd);
  217. return 0;
  218. }
  219. static void emac_setup(struct eth_device *dev)
  220. {
  221. struct wemac_regs *regs = (struct wemac_regs *)dev->iobase;
  222. u32 reg_val;
  223. u16 phy_val;
  224. u32 duplex_flag;
  225. /* Set up TX */
  226. writel(EMAC_TX_SETUP, &regs->tx_mode);
  227. /* Set up RX */
  228. writel(EMAC_RX_SETUP, &regs->rx_ctl);
  229. /* Set MAC */
  230. /* Set MAC CTL0 */
  231. writel(EMAC_MAC_CTL0_SETUP, &regs->mac_ctl0);
  232. /* Set MAC CTL1 */
  233. wemac_phy_read(dev->name, 1, 0, &phy_val);
  234. debug("PHY SETUP, reg 0 value: %x\n", phy_val);
  235. duplex_flag = !!(phy_val & (1 << 8));
  236. reg_val = 0;
  237. if (duplex_flag)
  238. reg_val = (0x1 << 0);
  239. writel(EMAC_MAC_CTL1_SETUP | reg_val, &regs->mac_ctl1);
  240. /* Set up IPGT */
  241. writel(EMAC_MAC_IPGT, &regs->mac_ipgt);
  242. /* Set up IPGR */
  243. writel(EMAC_MAC_NBTB_IPG2 | (EMAC_MAC_NBTB_IPG1 << 8), &regs->mac_ipgr);
  244. /* Set up Collison window */
  245. writel(EMAC_MAC_RM | (EMAC_MAC_CW << 8), &regs->mac_clrt);
  246. /* Set up Max Frame Length */
  247. writel(EMAC_MAC_MFL, &regs->mac_maxf);
  248. }
  249. static void wemac_reset(struct eth_device *dev)
  250. {
  251. struct wemac_regs *regs = (struct wemac_regs *)dev->iobase;
  252. debug("resetting device\n");
  253. /* RESET device */
  254. writel(0, &regs->ctl);
  255. udelay(200);
  256. writel(1, &regs->ctl);
  257. udelay(200);
  258. }
  259. static int sunxi_wemac_eth_init(struct eth_device *dev, bd_t *bd)
  260. {
  261. struct wemac_regs *regs = (struct wemac_regs *)dev->iobase;
  262. struct wemac_eth_dev *priv = dev->priv;
  263. u16 phy_reg;
  264. /* Init EMAC */
  265. /* Flush RX FIFO */
  266. setbits_le32(&regs->rx_ctl, 0x8);
  267. udelay(1);
  268. /* Init MAC */
  269. /* Soft reset MAC */
  270. clrbits_le32(&regs->mac_ctl0, 1 << 15);
  271. /* Set MII clock */
  272. clrsetbits_le32(&regs->mac_mcfg, 0xf << 2, 0xd << 2);
  273. /* Clear RX counter */
  274. writel(0x0, &regs->rx_fbc);
  275. udelay(1);
  276. /* Set up EMAC */
  277. emac_setup(dev);
  278. writel(dev->enetaddr[0] << 16 | dev->enetaddr[1] << 8 |
  279. dev->enetaddr[2], &regs->mac_a1);
  280. writel(dev->enetaddr[3] << 16 | dev->enetaddr[4] << 8 |
  281. dev->enetaddr[5], &regs->mac_a0);
  282. mdelay(1);
  283. wemac_reset(dev);
  284. /* PHY POWER UP */
  285. wemac_phy_read(dev->name, 1, 0, &phy_reg);
  286. wemac_phy_write(dev->name, 1, 0, phy_reg & (~(1 << 11)));
  287. mdelay(1);
  288. wemac_phy_read(dev->name, 1, 0, &phy_reg);
  289. priv->speed = miiphy_speed(dev->name, 0);
  290. priv->duplex = miiphy_duplex(dev->name, 0);
  291. /* Print link status only once */
  292. if (!priv->link_printed) {
  293. printf("ENET Speed is %d Mbps - %s duplex connection\n",
  294. priv->speed, (priv->duplex == HALF) ? "HALF" : "FULL");
  295. priv->link_printed = 1;
  296. }
  297. /* Set EMAC SPEED depend on PHY */
  298. clrsetbits_le32(&regs->mac_supp, 1 << 8,
  299. ((phy_reg & (1 << 13)) >> 13) << 8);
  300. /* Set duplex depend on phy */
  301. clrsetbits_le32(&regs->mac_ctl1, 1 << 0,
  302. ((phy_reg & (1 << 8)) >> 8) << 0);
  303. /* Enable RX/TX */
  304. setbits_le32(&regs->ctl, 0x7);
  305. return 0;
  306. }
  307. static void sunxi_wemac_eth_halt(struct eth_device *dev)
  308. {
  309. /* Nothing to do here */
  310. }
  311. static int sunxi_wemac_eth_recv(struct eth_device *dev)
  312. {
  313. struct wemac_regs *regs = (struct wemac_regs *)dev->iobase;
  314. struct wemac_rxhdr rxhdr;
  315. u32 rxcount;
  316. u32 reg_val;
  317. int rx_len;
  318. int rx_status;
  319. int good_packet;
  320. /* Check packet ready or not */
  321. /*
  322. * Race warning: The first packet might arrive with
  323. * the interrupts disabled, but the second will fix
  324. */
  325. rxcount = readl(&regs->rx_fbc);
  326. if (!rxcount) {
  327. /* Had one stuck? */
  328. rxcount = readl(&regs->rx_fbc);
  329. if (!rxcount)
  330. return 0;
  331. }
  332. reg_val = readl(&regs->rx_io_data);
  333. if (reg_val != 0x0143414d) {
  334. /* Disable RX */
  335. clrbits_le32(&regs->ctl, 1 << 2);
  336. /* Flush RX FIFO */
  337. setbits_le32(&regs->rx_ctl, 1 << 3);
  338. while (readl(&regs->rx_ctl) & (1 << 3))
  339. ;
  340. /* Enable RX */
  341. setbits_le32(&regs->ctl, 1 << 2);
  342. return 0;
  343. }
  344. /*
  345. * A packet ready now
  346. * Get status/length
  347. */
  348. good_packet = 1;
  349. wemac_inblk_32bit(&regs->rx_io_data, &rxhdr, sizeof(rxhdr));
  350. rx_len = rxhdr.rx_len;
  351. rx_status = rxhdr.rx_status;
  352. /* Packet Status check */
  353. if (rx_len < 0x40) {
  354. good_packet = 0;
  355. debug("RX: Bad Packet (runt)\n");
  356. }
  357. /* rx_status is identical to RSR register. */
  358. if (0 & rx_status & (EMAC_CRCERR | EMAC_LENERR)) {
  359. good_packet = 0;
  360. if (rx_status & EMAC_CRCERR)
  361. printf("crc error\n");
  362. if (rx_status & EMAC_LENERR)
  363. printf("length error\n");
  364. }
  365. /* Move data from WEMAC */
  366. if (good_packet) {
  367. if (rx_len > DMA_CPU_TRRESHOLD) {
  368. printf("Received packet is too big (len=%d)\n", rx_len);
  369. } else {
  370. wemac_inblk_32bit((void *)&regs->rx_io_data,
  371. NetRxPackets[0], rx_len);
  372. /* Pass to upper layer */
  373. NetReceive(NetRxPackets[0], rx_len);
  374. return rx_len;
  375. }
  376. }
  377. return 0;
  378. }
  379. static int sunxi_wemac_eth_send(struct eth_device *dev, void *packet, int len)
  380. {
  381. struct wemac_regs *regs = (struct wemac_regs *)dev->iobase;
  382. /* Select channel 0 */
  383. writel(0, &regs->tx_ins);
  384. /* Write packet */
  385. wemac_outblk_32bit((void *)&regs->tx_io_data, packet, len);
  386. /* Set TX len */
  387. writel(len, &regs->tx_pl0);
  388. /* Start translate from fifo to phy */
  389. setbits_le32(&regs->tx_ctl0, 1);
  390. return 0;
  391. }
  392. int sunxi_wemac_initialize(void)
  393. {
  394. struct sunxi_ccm_reg *const ccm =
  395. (struct sunxi_ccm_reg *)SUNXI_CCM_BASE;
  396. struct sunxi_sramc_regs *sram =
  397. (struct sunxi_sramc_regs *)SUNXI_SRAMC_BASE;
  398. struct eth_device *dev;
  399. struct wemac_eth_dev *priv;
  400. int pin;
  401. dev = malloc(sizeof(*dev));
  402. if (dev == NULL)
  403. return -ENOMEM;
  404. priv = (struct wemac_eth_dev *)malloc(sizeof(struct wemac_eth_dev));
  405. if (!priv) {
  406. free(dev);
  407. return -ENOMEM;
  408. }
  409. memset(dev, 0, sizeof(*dev));
  410. memset(priv, 0, sizeof(struct wemac_eth_dev));
  411. /* Map SRAM to EMAC */
  412. setbits_le32(&sram->ctrl1, 0x5 << 2);
  413. /* Configure pin mux settings for MII Ethernet */
  414. for (pin = SUNXI_GPA(0); pin <= SUNXI_GPA(17); pin++)
  415. sunxi_gpio_set_cfgpin(pin, 2);
  416. /* Set up clock gating */
  417. setbits_le32(&ccm->ahb_gate0, 1 << AHB_GATE_OFFSET_EMAC);
  418. dev->iobase = SUNXI_EMAC_BASE;
  419. dev->priv = priv;
  420. dev->init = sunxi_wemac_eth_init;
  421. dev->halt = sunxi_wemac_eth_halt;
  422. dev->send = sunxi_wemac_eth_send;
  423. dev->recv = sunxi_wemac_eth_recv;
  424. strcpy(dev->name, "wemac");
  425. eth_register(dev);
  426. miiphy_register(dev->name, wemac_phy_read, wemac_phy_write);
  427. return 0;
  428. }