sunxi_wemac.c 12 KB

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