sunxi_emac.c 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598
  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 <dm.h>
  10. #include <linux/err.h>
  11. #include <malloc.h>
  12. #include <miiphy.h>
  13. #include <net.h>
  14. #include <asm/io.h>
  15. #include <asm/arch/clock.h>
  16. #include <asm/arch/gpio.h>
  17. /* EMAC register */
  18. struct emac_regs {
  19. u32 ctl; /* 0x00 */
  20. u32 tx_mode; /* 0x04 */
  21. u32 tx_flow; /* 0x08 */
  22. u32 tx_ctl0; /* 0x0c */
  23. u32 tx_ctl1; /* 0x10 */
  24. u32 tx_ins; /* 0x14 */
  25. u32 tx_pl0; /* 0x18 */
  26. u32 tx_pl1; /* 0x1c */
  27. u32 tx_sta; /* 0x20 */
  28. u32 tx_io_data; /* 0x24 */
  29. u32 tx_io_data1;/* 0x28 */
  30. u32 tx_tsvl0; /* 0x2c */
  31. u32 tx_tsvh0; /* 0x30 */
  32. u32 tx_tsvl1; /* 0x34 */
  33. u32 tx_tsvh1; /* 0x38 */
  34. u32 rx_ctl; /* 0x3c */
  35. u32 rx_hash0; /* 0x40 */
  36. u32 rx_hash1; /* 0x44 */
  37. u32 rx_sta; /* 0x48 */
  38. u32 rx_io_data; /* 0x4c */
  39. u32 rx_fbc; /* 0x50 */
  40. u32 int_ctl; /* 0x54 */
  41. u32 int_sta; /* 0x58 */
  42. u32 mac_ctl0; /* 0x5c */
  43. u32 mac_ctl1; /* 0x60 */
  44. u32 mac_ipgt; /* 0x64 */
  45. u32 mac_ipgr; /* 0x68 */
  46. u32 mac_clrt; /* 0x6c */
  47. u32 mac_maxf; /* 0x70 */
  48. u32 mac_supp; /* 0x74 */
  49. u32 mac_test; /* 0x78 */
  50. u32 mac_mcfg; /* 0x7c */
  51. u32 mac_mcmd; /* 0x80 */
  52. u32 mac_madr; /* 0x84 */
  53. u32 mac_mwtd; /* 0x88 */
  54. u32 mac_mrdd; /* 0x8c */
  55. u32 mac_mind; /* 0x90 */
  56. u32 mac_ssrr; /* 0x94 */
  57. u32 mac_a0; /* 0x98 */
  58. u32 mac_a1; /* 0x9c */
  59. };
  60. /* SRAMC register */
  61. struct sunxi_sramc_regs {
  62. u32 ctrl0;
  63. u32 ctrl1;
  64. };
  65. /* 0: Disable 1: Aborted frame enable(default) */
  66. #define EMAC_TX_AB_M (0x1 << 0)
  67. /* 0: CPU 1: DMA(default) */
  68. #define EMAC_TX_TM (0x1 << 1)
  69. #define EMAC_TX_SETUP (0)
  70. /* 0: DRQ asserted 1: DRQ automatically(default) */
  71. #define EMAC_RX_DRQ_MODE (0x1 << 1)
  72. /* 0: CPU 1: DMA(default) */
  73. #define EMAC_RX_TM (0x1 << 2)
  74. /* 0: Normal(default) 1: Pass all Frames */
  75. #define EMAC_RX_PA (0x1 << 4)
  76. /* 0: Normal(default) 1: Pass Control Frames */
  77. #define EMAC_RX_PCF (0x1 << 5)
  78. /* 0: Normal(default) 1: Pass Frames with CRC Error */
  79. #define EMAC_RX_PCRCE (0x1 << 6)
  80. /* 0: Normal(default) 1: Pass Frames with Length Error */
  81. #define EMAC_RX_PLE (0x1 << 7)
  82. /* 0: Normal 1: Pass Frames length out of range(default) */
  83. #define EMAC_RX_POR (0x1 << 8)
  84. /* 0: Not accept 1: Accept unicast Packets(default) */
  85. #define EMAC_RX_UCAD (0x1 << 16)
  86. /* 0: Normal(default) 1: DA Filtering */
  87. #define EMAC_RX_DAF (0x1 << 17)
  88. /* 0: Not accept 1: Accept multicast Packets(default) */
  89. #define EMAC_RX_MCO (0x1 << 20)
  90. /* 0: Disable(default) 1: Enable Hash filter */
  91. #define EMAC_RX_MHF (0x1 << 21)
  92. /* 0: Not accept 1: Accept Broadcast Packets(default) */
  93. #define EMAC_RX_BCO (0x1 << 22)
  94. /* 0: Disable(default) 1: Enable SA Filtering */
  95. #define EMAC_RX_SAF (0x1 << 24)
  96. /* 0: Normal(default) 1: Inverse Filtering */
  97. #define EMAC_RX_SAIF (0x1 << 25)
  98. #define EMAC_RX_SETUP (EMAC_RX_POR | EMAC_RX_UCAD | EMAC_RX_DAF | \
  99. EMAC_RX_MCO | EMAC_RX_BCO)
  100. /* 0: Disable 1: Enable Receive Flow Control(default) */
  101. #define EMAC_MAC_CTL0_RFC (0x1 << 2)
  102. /* 0: Disable 1: Enable Transmit Flow Control(default) */
  103. #define EMAC_MAC_CTL0_TFC (0x1 << 3)
  104. #define EMAC_MAC_CTL0_SETUP (EMAC_MAC_CTL0_RFC | EMAC_MAC_CTL0_TFC)
  105. /* 0: Disable 1: Enable MAC Frame Length Checking(default) */
  106. #define EMAC_MAC_CTL1_FLC (0x1 << 1)
  107. /* 0: Disable(default) 1: Enable Huge Frame */
  108. #define EMAC_MAC_CTL1_HF (0x1 << 2)
  109. /* 0: Disable(default) 1: Enable MAC Delayed CRC */
  110. #define EMAC_MAC_CTL1_DCRC (0x1 << 3)
  111. /* 0: Disable 1: Enable MAC CRC(default) */
  112. #define EMAC_MAC_CTL1_CRC (0x1 << 4)
  113. /* 0: Disable 1: Enable MAC PAD Short frames(default) */
  114. #define EMAC_MAC_CTL1_PC (0x1 << 5)
  115. /* 0: Disable(default) 1: Enable MAC PAD Short frames and append CRC */
  116. #define EMAC_MAC_CTL1_VC (0x1 << 6)
  117. /* 0: Disable(default) 1: Enable MAC auto detect Short frames */
  118. #define EMAC_MAC_CTL1_ADP (0x1 << 7)
  119. /* 0: Disable(default) 1: Enable */
  120. #define EMAC_MAC_CTL1_PRE (0x1 << 8)
  121. /* 0: Disable(default) 1: Enable */
  122. #define EMAC_MAC_CTL1_LPE (0x1 << 9)
  123. /* 0: Disable(default) 1: Enable no back off */
  124. #define EMAC_MAC_CTL1_NB (0x1 << 12)
  125. /* 0: Disable(default) 1: Enable */
  126. #define EMAC_MAC_CTL1_BNB (0x1 << 13)
  127. /* 0: Disable(default) 1: Enable */
  128. #define EMAC_MAC_CTL1_ED (0x1 << 14)
  129. #define EMAC_MAC_CTL1_SETUP (EMAC_MAC_CTL1_FLC | EMAC_MAC_CTL1_CRC | \
  130. EMAC_MAC_CTL1_PC)
  131. #define EMAC_MAC_IPGT 0x15
  132. #define EMAC_MAC_NBTB_IPG1 0xc
  133. #define EMAC_MAC_NBTB_IPG2 0x12
  134. #define EMAC_MAC_CW 0x37
  135. #define EMAC_MAC_RM 0xf
  136. #define EMAC_MAC_MFL 0x0600
  137. /* Receive status */
  138. #define EMAC_CRCERR (0x1 << 4)
  139. #define EMAC_LENERR (0x3 << 5)
  140. #define EMAC_RX_BUFSIZE 2000
  141. struct emac_eth_dev {
  142. struct emac_regs *regs;
  143. struct mii_dev *bus;
  144. struct phy_device *phydev;
  145. int link_printed;
  146. #ifdef CONFIG_DM_ETH
  147. uchar rx_buf[EMAC_RX_BUFSIZE];
  148. #endif
  149. };
  150. struct emac_rxhdr {
  151. s16 rx_len;
  152. u16 rx_status;
  153. };
  154. static void emac_inblk_32bit(void *reg, void *data, int count)
  155. {
  156. int cnt = (count + 3) >> 2;
  157. if (cnt) {
  158. u32 *buf = data;
  159. do {
  160. u32 x = readl(reg);
  161. *buf++ = x;
  162. } while (--cnt);
  163. }
  164. }
  165. static void emac_outblk_32bit(void *reg, void *data, int count)
  166. {
  167. int cnt = (count + 3) >> 2;
  168. if (cnt) {
  169. const u32 *buf = data;
  170. do {
  171. writel(*buf++, reg);
  172. } while (--cnt);
  173. }
  174. }
  175. /* Read a word from phyxcer */
  176. static int emac_mdio_read(struct mii_dev *bus, int addr, int devad, int reg)
  177. {
  178. struct emac_eth_dev *priv = bus->priv;
  179. struct emac_regs *regs = priv->regs;
  180. /* issue the phy address and reg */
  181. writel(addr << 8 | reg, &regs->mac_madr);
  182. /* pull up the phy io line */
  183. writel(0x1, &regs->mac_mcmd);
  184. /* Wait read complete */
  185. mdelay(1);
  186. /* push down the phy io line */
  187. writel(0x0, &regs->mac_mcmd);
  188. /* And read data */
  189. return readl(&regs->mac_mrdd);
  190. }
  191. /* Write a word to phyxcer */
  192. static int emac_mdio_write(struct mii_dev *bus, int addr, int devad, int reg,
  193. u16 value)
  194. {
  195. struct emac_eth_dev *priv = bus->priv;
  196. struct emac_regs *regs = priv->regs;
  197. /* issue the phy address and reg */
  198. writel(addr << 8 | reg, &regs->mac_madr);
  199. /* pull up the phy io line */
  200. writel(0x1, &regs->mac_mcmd);
  201. /* Wait write complete */
  202. mdelay(1);
  203. /* push down the phy io line */
  204. writel(0x0, &regs->mac_mcmd);
  205. /* and write data */
  206. writel(value, &regs->mac_mwtd);
  207. return 0;
  208. }
  209. static int sunxi_emac_init_phy(struct emac_eth_dev *priv, void *dev)
  210. {
  211. int ret, mask = 0xffffffff;
  212. #ifdef CONFIG_PHY_ADDR
  213. mask = 1 << CONFIG_PHY_ADDR;
  214. #endif
  215. priv->bus = mdio_alloc();
  216. if (!priv->bus) {
  217. printf("Failed to allocate MDIO bus\n");
  218. return -ENOMEM;
  219. }
  220. priv->bus->read = emac_mdio_read;
  221. priv->bus->write = emac_mdio_write;
  222. priv->bus->priv = priv;
  223. strcpy(priv->bus->name, "emac");
  224. ret = mdio_register(priv->bus);
  225. if (ret)
  226. return ret;
  227. priv->phydev = phy_find_by_mask(priv->bus, mask,
  228. PHY_INTERFACE_MODE_MII);
  229. if (!priv->phydev)
  230. return -ENODEV;
  231. phy_connect_dev(priv->phydev, dev);
  232. phy_config(priv->phydev);
  233. return 0;
  234. }
  235. static void emac_setup(struct emac_eth_dev *priv)
  236. {
  237. struct emac_regs *regs = priv->regs;
  238. u32 reg_val;
  239. /* Set up TX */
  240. writel(EMAC_TX_SETUP, &regs->tx_mode);
  241. /* Set up RX */
  242. writel(EMAC_RX_SETUP, &regs->rx_ctl);
  243. /* Set MAC */
  244. /* Set MAC CTL0 */
  245. writel(EMAC_MAC_CTL0_SETUP, &regs->mac_ctl0);
  246. /* Set MAC CTL1 */
  247. reg_val = 0;
  248. if (priv->phydev->duplex == DUPLEX_FULL)
  249. reg_val = (0x1 << 0);
  250. writel(EMAC_MAC_CTL1_SETUP | reg_val, &regs->mac_ctl1);
  251. /* Set up IPGT */
  252. writel(EMAC_MAC_IPGT, &regs->mac_ipgt);
  253. /* Set up IPGR */
  254. writel(EMAC_MAC_NBTB_IPG2 | (EMAC_MAC_NBTB_IPG1 << 8), &regs->mac_ipgr);
  255. /* Set up Collison window */
  256. writel(EMAC_MAC_RM | (EMAC_MAC_CW << 8), &regs->mac_clrt);
  257. /* Set up Max Frame Length */
  258. writel(EMAC_MAC_MFL, &regs->mac_maxf);
  259. }
  260. static void emac_reset(struct emac_eth_dev *priv)
  261. {
  262. struct emac_regs *regs = priv->regs;
  263. debug("resetting device\n");
  264. /* RESET device */
  265. writel(0, &regs->ctl);
  266. udelay(200);
  267. writel(1, &regs->ctl);
  268. udelay(200);
  269. }
  270. static int _sunxi_write_hwaddr(struct emac_eth_dev *priv, u8 *enetaddr)
  271. {
  272. struct emac_regs *regs = priv->regs;
  273. u32 enetaddr_lo, enetaddr_hi;
  274. enetaddr_lo = enetaddr[2] | (enetaddr[1] << 8) | (enetaddr[0] << 16);
  275. enetaddr_hi = enetaddr[5] | (enetaddr[4] << 8) | (enetaddr[3] << 16);
  276. writel(enetaddr_hi, &regs->mac_a1);
  277. writel(enetaddr_lo, &regs->mac_a0);
  278. return 0;
  279. }
  280. static int _sunxi_emac_eth_init(struct emac_eth_dev *priv, u8 *enetaddr)
  281. {
  282. struct emac_regs *regs = priv->regs;
  283. int ret;
  284. /* Init EMAC */
  285. /* Flush RX FIFO */
  286. setbits_le32(&regs->rx_ctl, 0x8);
  287. udelay(1);
  288. /* Init MAC */
  289. /* Soft reset MAC */
  290. clrbits_le32(&regs->mac_ctl0, 0x1 << 15);
  291. /* Clear RX counter */
  292. writel(0x0, &regs->rx_fbc);
  293. udelay(1);
  294. /* Set up EMAC */
  295. emac_setup(priv);
  296. _sunxi_write_hwaddr(priv, enetaddr);
  297. mdelay(1);
  298. emac_reset(priv);
  299. /* PHY POWER UP */
  300. ret = phy_startup(priv->phydev);
  301. if (ret) {
  302. printf("Could not initialize PHY %s\n",
  303. priv->phydev->dev->name);
  304. return ret;
  305. }
  306. /* Print link status only once */
  307. if (!priv->link_printed) {
  308. printf("ENET Speed is %d Mbps - %s duplex connection\n",
  309. priv->phydev->speed,
  310. priv->phydev->duplex ? "FULL" : "HALF");
  311. priv->link_printed = 1;
  312. }
  313. /* Set EMAC SPEED depend on PHY */
  314. if (priv->phydev->speed == SPEED_100)
  315. setbits_le32(&regs->mac_supp, 1 << 8);
  316. else
  317. clrbits_le32(&regs->mac_supp, 1 << 8);
  318. /* Set duplex depend on phy */
  319. if (priv->phydev->duplex == DUPLEX_FULL)
  320. setbits_le32(&regs->mac_ctl1, 1 << 0);
  321. else
  322. clrbits_le32(&regs->mac_ctl1, 1 << 0);
  323. /* Enable RX/TX */
  324. setbits_le32(&regs->ctl, 0x7);
  325. return 0;
  326. }
  327. static int _sunxi_emac_eth_recv(struct emac_eth_dev *priv, void *packet)
  328. {
  329. struct emac_regs *regs = priv->regs;
  330. struct emac_rxhdr rxhdr;
  331. u32 rxcount;
  332. u32 reg_val;
  333. int rx_len;
  334. int rx_status;
  335. int good_packet;
  336. /* Check packet ready or not */
  337. /* Race warning: The first packet might arrive with
  338. * the interrupts disabled, but the second will fix
  339. */
  340. rxcount = readl(&regs->rx_fbc);
  341. if (!rxcount) {
  342. /* Had one stuck? */
  343. rxcount = readl(&regs->rx_fbc);
  344. if (!rxcount)
  345. return -EAGAIN;
  346. }
  347. reg_val = readl(&regs->rx_io_data);
  348. if (reg_val != 0x0143414d) {
  349. /* Disable RX */
  350. clrbits_le32(&regs->ctl, 0x1 << 2);
  351. /* Flush RX FIFO */
  352. setbits_le32(&regs->rx_ctl, 0x1 << 3);
  353. while (readl(&regs->rx_ctl) & (0x1 << 3))
  354. ;
  355. /* Enable RX */
  356. setbits_le32(&regs->ctl, 0x1 << 2);
  357. return -EAGAIN;
  358. }
  359. /* A packet ready now
  360. * Get status/length
  361. */
  362. good_packet = 1;
  363. emac_inblk_32bit(&regs->rx_io_data, &rxhdr, sizeof(rxhdr));
  364. rx_len = rxhdr.rx_len;
  365. rx_status = rxhdr.rx_status;
  366. /* Packet Status check */
  367. if (rx_len < 0x40) {
  368. good_packet = 0;
  369. debug("RX: Bad Packet (runt)\n");
  370. }
  371. /* rx_status is identical to RSR register. */
  372. if (0 & rx_status & (EMAC_CRCERR | EMAC_LENERR)) {
  373. good_packet = 0;
  374. if (rx_status & EMAC_CRCERR)
  375. printf("crc error\n");
  376. if (rx_status & EMAC_LENERR)
  377. printf("length error\n");
  378. }
  379. /* Move data from EMAC */
  380. if (good_packet) {
  381. if (rx_len > EMAC_RX_BUFSIZE) {
  382. printf("Received packet is too big (len=%d)\n", rx_len);
  383. return -EMSGSIZE;
  384. }
  385. emac_inblk_32bit((void *)&regs->rx_io_data, packet, rx_len);
  386. return rx_len;
  387. }
  388. return -EIO; /* Bad packet */
  389. }
  390. static int _sunxi_emac_eth_send(struct emac_eth_dev *priv, void *packet,
  391. int len)
  392. {
  393. struct emac_regs *regs = priv->regs;
  394. /* Select channel 0 */
  395. writel(0, &regs->tx_ins);
  396. /* Write packet */
  397. emac_outblk_32bit((void *)&regs->tx_io_data, packet, len);
  398. /* Set TX len */
  399. writel(len, &regs->tx_pl0);
  400. /* Start translate from fifo to phy */
  401. setbits_le32(&regs->tx_ctl0, 1);
  402. return 0;
  403. }
  404. static void sunxi_emac_board_setup(struct emac_eth_dev *priv)
  405. {
  406. struct sunxi_ccm_reg *const ccm =
  407. (struct sunxi_ccm_reg *)SUNXI_CCM_BASE;
  408. struct sunxi_sramc_regs *sram =
  409. (struct sunxi_sramc_regs *)SUNXI_SRAMC_BASE;
  410. struct emac_regs *regs = priv->regs;
  411. int pin;
  412. /* Map SRAM to EMAC */
  413. setbits_le32(&sram->ctrl1, 0x5 << 2);
  414. /* Configure pin mux settings for MII Ethernet */
  415. for (pin = SUNXI_GPA(0); pin <= SUNXI_GPA(17); pin++)
  416. sunxi_gpio_set_cfgpin(pin, SUNXI_GPA_EMAC);
  417. /* Set up clock gating */
  418. setbits_le32(&ccm->ahb_gate0, 0x1 << AHB_GATE_OFFSET_EMAC);
  419. /* Set MII clock */
  420. clrsetbits_le32(&regs->mac_mcfg, 0xf << 2, 0xd << 2);
  421. }
  422. static int sunxi_emac_eth_start(struct udevice *dev)
  423. {
  424. struct eth_pdata *pdata = dev_get_platdata(dev);
  425. return _sunxi_emac_eth_init(dev->priv, pdata->enetaddr);
  426. }
  427. static int sunxi_emac_eth_send(struct udevice *dev, void *packet, int length)
  428. {
  429. struct emac_eth_dev *priv = dev_get_priv(dev);
  430. return _sunxi_emac_eth_send(priv, packet, length);
  431. }
  432. static int sunxi_emac_eth_recv(struct udevice *dev, int flags, uchar **packetp)
  433. {
  434. struct emac_eth_dev *priv = dev_get_priv(dev);
  435. int rx_len;
  436. rx_len = _sunxi_emac_eth_recv(priv, priv->rx_buf);
  437. *packetp = priv->rx_buf;
  438. return rx_len;
  439. }
  440. static void sunxi_emac_eth_stop(struct udevice *dev)
  441. {
  442. /* Nothing to do here */
  443. }
  444. static int sunxi_emac_eth_probe(struct udevice *dev)
  445. {
  446. struct eth_pdata *pdata = dev_get_platdata(dev);
  447. struct emac_eth_dev *priv = dev_get_priv(dev);
  448. priv->regs = (struct emac_regs *)pdata->iobase;
  449. sunxi_emac_board_setup(priv);
  450. return sunxi_emac_init_phy(priv, dev);
  451. }
  452. static const struct eth_ops sunxi_emac_eth_ops = {
  453. .start = sunxi_emac_eth_start,
  454. .send = sunxi_emac_eth_send,
  455. .recv = sunxi_emac_eth_recv,
  456. .stop = sunxi_emac_eth_stop,
  457. };
  458. static int sunxi_emac_eth_ofdata_to_platdata(struct udevice *dev)
  459. {
  460. struct eth_pdata *pdata = dev_get_platdata(dev);
  461. pdata->iobase = devfdt_get_addr(dev);
  462. return 0;
  463. }
  464. static const struct udevice_id sunxi_emac_eth_ids[] = {
  465. { .compatible = "allwinner,sun4i-a10-emac" },
  466. { }
  467. };
  468. U_BOOT_DRIVER(eth_sunxi_emac) = {
  469. .name = "eth_sunxi_emac",
  470. .id = UCLASS_ETH,
  471. .of_match = sunxi_emac_eth_ids,
  472. .ofdata_to_platdata = sunxi_emac_eth_ofdata_to_platdata,
  473. .probe = sunxi_emac_eth_probe,
  474. .ops = &sunxi_emac_eth_ops,
  475. .priv_auto_alloc_size = sizeof(struct emac_eth_dev),
  476. .platdata_auto_alloc_size = sizeof(struct eth_pdata),
  477. };