sunxi_emac.c 14 KB

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