sunxi_emac.c 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521
  1. /*
  2. * sunxi_emac.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 <linux/err.h>
  10. #include <malloc.h>
  11. #include <miiphy.h>
  12. #include <net.h>
  13. #include <asm/io.h>
  14. #include <asm/arch/clock.h>
  15. #include <asm/arch/gpio.h>
  16. /* EMAC register */
  17. struct emac_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 (0x1 << 4)
  138. #define EMAC_LENERR (0x3 << 5)
  139. #define DMA_CPU_TRRESHOLD 2000
  140. struct emac_eth_dev {
  141. u32 speed;
  142. u32 duplex;
  143. u32 phy_configured;
  144. int link_printed;
  145. };
  146. struct emac_rxhdr {
  147. s16 rx_len;
  148. u16 rx_status;
  149. };
  150. static void emac_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 emac_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. /* Read a word from phyxcer */
  172. static int emac_phy_read(const char *devname, unsigned char addr,
  173. unsigned char reg, unsigned short *value)
  174. {
  175. struct eth_device *dev = eth_get_dev_by_name(devname);
  176. struct emac_regs *regs = (struct emac_regs *)dev->iobase;
  177. /* issue the phy address and reg */
  178. writel(addr << 8 | reg, &regs->mac_madr);
  179. /* pull up the phy io line */
  180. writel(0x1, &regs->mac_mcmd);
  181. /* Wait read complete */
  182. mdelay(1);
  183. /* push down the phy io line */
  184. writel(0x0, &regs->mac_mcmd);
  185. /* and write data */
  186. *value = readl(&regs->mac_mrdd);
  187. return 0;
  188. }
  189. /* Write a word to phyxcer */
  190. static int emac_phy_write(const char *devname, unsigned char addr,
  191. unsigned char reg, unsigned short value)
  192. {
  193. struct eth_device *dev = eth_get_dev_by_name(devname);
  194. struct emac_regs *regs = (struct emac_regs *)dev->iobase;
  195. /* issue the phy address and reg */
  196. writel(addr << 8 | reg, &regs->mac_madr);
  197. /* pull up the phy io line */
  198. writel(0x1, &regs->mac_mcmd);
  199. /* Wait write complete */
  200. mdelay(1);
  201. /* push down the phy io line */
  202. writel(0x0, &regs->mac_mcmd);
  203. /* and write data */
  204. writel(value, &regs->mac_mwtd);
  205. return 0;
  206. }
  207. static void emac_setup(struct eth_device *dev)
  208. {
  209. struct emac_regs *regs = (struct emac_regs *)dev->iobase;
  210. u32 reg_val;
  211. u16 phy_val;
  212. u32 duplex_flag;
  213. /* Set up TX */
  214. writel(EMAC_TX_SETUP, &regs->tx_mode);
  215. /* Set up RX */
  216. writel(EMAC_RX_SETUP, &regs->rx_ctl);
  217. /* Set MAC */
  218. /* Set MAC CTL0 */
  219. writel(EMAC_MAC_CTL0_SETUP, &regs->mac_ctl0);
  220. /* Set MAC CTL1 */
  221. emac_phy_read(dev->name, 1, 0, &phy_val);
  222. debug("PHY SETUP, reg 0 value: %x\n", phy_val);
  223. duplex_flag = !!(phy_val & (1 << 8));
  224. reg_val = 0;
  225. if (duplex_flag)
  226. reg_val = (0x1 << 0);
  227. writel(EMAC_MAC_CTL1_SETUP | reg_val, &regs->mac_ctl1);
  228. /* Set up IPGT */
  229. writel(EMAC_MAC_IPGT, &regs->mac_ipgt);
  230. /* Set up IPGR */
  231. writel(EMAC_MAC_NBTB_IPG2 | (EMAC_MAC_NBTB_IPG1 << 8), &regs->mac_ipgr);
  232. /* Set up Collison window */
  233. writel(EMAC_MAC_RM | (EMAC_MAC_CW << 8), &regs->mac_clrt);
  234. /* Set up Max Frame Length */
  235. writel(EMAC_MAC_MFL, &regs->mac_maxf);
  236. }
  237. static void emac_reset(struct eth_device *dev)
  238. {
  239. struct emac_regs *regs = (struct emac_regs *)dev->iobase;
  240. debug("resetting device\n");
  241. /* RESET device */
  242. writel(0, &regs->ctl);
  243. udelay(200);
  244. writel(1, &regs->ctl);
  245. udelay(200);
  246. }
  247. static int sunxi_emac_eth_init(struct eth_device *dev, bd_t *bd)
  248. {
  249. struct emac_regs *regs = (struct emac_regs *)dev->iobase;
  250. struct emac_eth_dev *priv = dev->priv;
  251. u16 phy_reg;
  252. /* Init EMAC */
  253. /* Flush RX FIFO */
  254. setbits_le32(&regs->rx_ctl, 0x8);
  255. udelay(1);
  256. /* Init MAC */
  257. /* Soft reset MAC */
  258. clrbits_le32(&regs->mac_ctl0, 0x1 << 15);
  259. /* Clear RX counter */
  260. writel(0x0, &regs->rx_fbc);
  261. udelay(1);
  262. /* Set up EMAC */
  263. emac_setup(dev);
  264. writel(dev->enetaddr[0] << 16 | dev->enetaddr[1] << 8 |
  265. dev->enetaddr[2], &regs->mac_a1);
  266. writel(dev->enetaddr[3] << 16 | dev->enetaddr[4] << 8 |
  267. dev->enetaddr[5], &regs->mac_a0);
  268. mdelay(1);
  269. emac_reset(dev);
  270. /* PHY POWER UP */
  271. emac_phy_read(dev->name, 1, 0, &phy_reg);
  272. emac_phy_write(dev->name, 1, 0, phy_reg & (~(0x1 << 11)));
  273. mdelay(1);
  274. emac_phy_read(dev->name, 1, 0, &phy_reg);
  275. priv->speed = miiphy_speed(dev->name, 0);
  276. priv->duplex = miiphy_duplex(dev->name, 0);
  277. /* Print link status only once */
  278. if (!priv->link_printed) {
  279. printf("ENET Speed is %d Mbps - %s duplex connection\n",
  280. priv->speed, (priv->duplex == HALF) ? "HALF" : "FULL");
  281. priv->link_printed = 1;
  282. }
  283. /* Set EMAC SPEED depend on PHY */
  284. clrsetbits_le32(&regs->mac_supp, 1 << 8,
  285. ((phy_reg & (0x1 << 13)) >> 13) << 8);
  286. /* Set duplex depend on phy */
  287. clrsetbits_le32(&regs->mac_ctl1, 1 << 0,
  288. ((phy_reg & (0x1 << 8)) >> 8) << 0);
  289. /* Enable RX/TX */
  290. setbits_le32(&regs->ctl, 0x7);
  291. return 0;
  292. }
  293. static void sunxi_emac_eth_halt(struct eth_device *dev)
  294. {
  295. /* Nothing to do here */
  296. }
  297. static int sunxi_emac_eth_recv(struct eth_device *dev)
  298. {
  299. struct emac_regs *regs = (struct emac_regs *)dev->iobase;
  300. struct emac_rxhdr rxhdr;
  301. u32 rxcount;
  302. u32 reg_val;
  303. int rx_len;
  304. int rx_status;
  305. int good_packet;
  306. /* Check packet ready or not */
  307. /* Race warning: The first packet might arrive with
  308. * the interrupts disabled, but the second will fix
  309. */
  310. rxcount = readl(&regs->rx_fbc);
  311. if (!rxcount) {
  312. /* Had one stuck? */
  313. rxcount = readl(&regs->rx_fbc);
  314. if (!rxcount)
  315. return 0;
  316. }
  317. reg_val = readl(&regs->rx_io_data);
  318. if (reg_val != 0x0143414d) {
  319. /* Disable RX */
  320. clrbits_le32(&regs->ctl, 0x1 << 2);
  321. /* Flush RX FIFO */
  322. setbits_le32(&regs->rx_ctl, 0x1 << 3);
  323. while (readl(&regs->rx_ctl) & (0x1 << 3))
  324. ;
  325. /* Enable RX */
  326. setbits_le32(&regs->ctl, 0x1 << 2);
  327. return 0;
  328. }
  329. /* A packet ready now
  330. * Get status/length
  331. */
  332. good_packet = 1;
  333. emac_inblk_32bit(&regs->rx_io_data, &rxhdr, sizeof(rxhdr));
  334. rx_len = rxhdr.rx_len;
  335. rx_status = rxhdr.rx_status;
  336. /* Packet Status check */
  337. if (rx_len < 0x40) {
  338. good_packet = 0;
  339. debug("RX: Bad Packet (runt)\n");
  340. }
  341. /* rx_status is identical to RSR register. */
  342. if (0 & rx_status & (EMAC_CRCERR | EMAC_LENERR)) {
  343. good_packet = 0;
  344. if (rx_status & EMAC_CRCERR)
  345. printf("crc error\n");
  346. if (rx_status & EMAC_LENERR)
  347. printf("length error\n");
  348. }
  349. /* Move data from EMAC */
  350. if (good_packet) {
  351. if (rx_len > DMA_CPU_TRRESHOLD) {
  352. printf("Received packet is too big (len=%d)\n", rx_len);
  353. } else {
  354. emac_inblk_32bit((void *)&regs->rx_io_data,
  355. net_rx_packets[0], rx_len);
  356. /* Pass to upper layer */
  357. net_process_received_packet(net_rx_packets[0], rx_len);
  358. return rx_len;
  359. }
  360. }
  361. return 0;
  362. }
  363. static int sunxi_emac_eth_send(struct eth_device *dev, void *packet, int len)
  364. {
  365. struct emac_regs *regs = (struct emac_regs *)dev->iobase;
  366. /* Select channel 0 */
  367. writel(0, &regs->tx_ins);
  368. /* Write packet */
  369. emac_outblk_32bit((void *)&regs->tx_io_data, packet, len);
  370. /* Set TX len */
  371. writel(len, &regs->tx_pl0);
  372. /* Start translate from fifo to phy */
  373. setbits_le32(&regs->tx_ctl0, 1);
  374. return 0;
  375. }
  376. int sunxi_emac_initialize(void)
  377. {
  378. struct sunxi_ccm_reg *const ccm =
  379. (struct sunxi_ccm_reg *)SUNXI_CCM_BASE;
  380. struct sunxi_sramc_regs *sram =
  381. (struct sunxi_sramc_regs *)SUNXI_SRAMC_BASE;
  382. struct emac_regs *regs =
  383. (struct emac_regs *)SUNXI_EMAC_BASE;
  384. struct eth_device *dev;
  385. struct emac_eth_dev *priv;
  386. int pin;
  387. dev = malloc(sizeof(*dev));
  388. if (dev == NULL)
  389. return -ENOMEM;
  390. priv = (struct emac_eth_dev *)malloc(sizeof(struct emac_eth_dev));
  391. if (!priv) {
  392. free(dev);
  393. return -ENOMEM;
  394. }
  395. memset(dev, 0, sizeof(*dev));
  396. memset(priv, 0, sizeof(struct emac_eth_dev));
  397. /* Map SRAM to EMAC */
  398. setbits_le32(&sram->ctrl1, 0x5 << 2);
  399. /* Configure pin mux settings for MII Ethernet */
  400. for (pin = SUNXI_GPA(0); pin <= SUNXI_GPA(17); pin++)
  401. sunxi_gpio_set_cfgpin(pin, SUNXI_GPA_EMAC);
  402. /* Set up clock gating */
  403. setbits_le32(&ccm->ahb_gate0, 0x1 << AHB_GATE_OFFSET_EMAC);
  404. /* Set MII clock */
  405. clrsetbits_le32(&regs->mac_mcfg, 0xf << 2, 0xd << 2);
  406. dev->iobase = (int)regs;
  407. dev->priv = priv;
  408. dev->init = sunxi_emac_eth_init;
  409. dev->halt = sunxi_emac_eth_halt;
  410. dev->send = sunxi_emac_eth_send;
  411. dev->recv = sunxi_emac_eth_recv;
  412. strcpy(dev->name, "emac");
  413. eth_register(dev);
  414. miiphy_register(dev->name, emac_phy_read, emac_phy_write);
  415. return 0;
  416. }