sunxi.c 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525
  1. // SPDX-License-Identifier: GPL-2.0
  2. /*
  3. * Allwinner SUNXI "glue layer"
  4. *
  5. * Copyright © 2015 Hans de Goede <hdegoede@redhat.com>
  6. * Copyright © 2013 Jussi Kivilinna <jussi.kivilinna@iki.fi>
  7. *
  8. * Based on the sw_usb "Allwinner OTG Dual Role Controller" code.
  9. * Copyright 2007-2012 (C) Allwinner Technology Co., Ltd.
  10. * javen <javen@allwinnertech.com>
  11. *
  12. * Based on the DA8xx "glue layer" code.
  13. * Copyright (c) 2008-2009 MontaVista Software, Inc. <source@mvista.com>
  14. * Copyright (C) 2005-2006 by Texas Instruments
  15. *
  16. * This file is part of the Inventra Controller Driver for Linux.
  17. */
  18. #include <common.h>
  19. #include <dm.h>
  20. #include <generic-phy.h>
  21. #include <phy-sun4i-usb.h>
  22. #include <asm/arch/cpu.h>
  23. #include <asm/arch/clock.h>
  24. #include <asm/arch/gpio.h>
  25. #include <asm-generic/gpio.h>
  26. #include <dm/lists.h>
  27. #include <dm/root.h>
  28. #include <linux/usb/musb.h>
  29. #include "linux-compat.h"
  30. #include "musb_core.h"
  31. #include "musb_uboot.h"
  32. /******************************************************************************
  33. ******************************************************************************
  34. * From the Allwinner driver
  35. ******************************************************************************
  36. ******************************************************************************/
  37. /******************************************************************************
  38. * From include/sunxi_usb_bsp.h
  39. ******************************************************************************/
  40. /* reg offsets */
  41. #define USBC_REG_o_ISCR 0x0400
  42. #define USBC_REG_o_PHYCTL 0x0404
  43. #define USBC_REG_o_PHYBIST 0x0408
  44. #define USBC_REG_o_PHYTUNE 0x040c
  45. #define USBC_REG_o_VEND0 0x0043
  46. /* Interface Status and Control */
  47. #define USBC_BP_ISCR_VBUS_VALID_FROM_DATA 30
  48. #define USBC_BP_ISCR_VBUS_VALID_FROM_VBUS 29
  49. #define USBC_BP_ISCR_EXT_ID_STATUS 28
  50. #define USBC_BP_ISCR_EXT_DM_STATUS 27
  51. #define USBC_BP_ISCR_EXT_DP_STATUS 26
  52. #define USBC_BP_ISCR_MERGED_VBUS_STATUS 25
  53. #define USBC_BP_ISCR_MERGED_ID_STATUS 24
  54. #define USBC_BP_ISCR_ID_PULLUP_EN 17
  55. #define USBC_BP_ISCR_DPDM_PULLUP_EN 16
  56. #define USBC_BP_ISCR_FORCE_ID 14
  57. #define USBC_BP_ISCR_FORCE_VBUS_VALID 12
  58. #define USBC_BP_ISCR_VBUS_VALID_SRC 10
  59. #define USBC_BP_ISCR_HOSC_EN 7
  60. #define USBC_BP_ISCR_VBUS_CHANGE_DETECT 6
  61. #define USBC_BP_ISCR_ID_CHANGE_DETECT 5
  62. #define USBC_BP_ISCR_DPDM_CHANGE_DETECT 4
  63. #define USBC_BP_ISCR_IRQ_ENABLE 3
  64. #define USBC_BP_ISCR_VBUS_CHANGE_DETECT_EN 2
  65. #define USBC_BP_ISCR_ID_CHANGE_DETECT_EN 1
  66. #define USBC_BP_ISCR_DPDM_CHANGE_DETECT_EN 0
  67. /******************************************************************************
  68. * From usbc/usbc.c
  69. ******************************************************************************/
  70. struct sunxi_musb_config {
  71. struct musb_hdrc_config *config;
  72. u8 rst_bit;
  73. u8 clkgate_bit;
  74. };
  75. struct sunxi_glue {
  76. struct musb_host_data mdata;
  77. struct sunxi_ccm_reg *ccm;
  78. struct sunxi_musb_config *cfg;
  79. struct device dev;
  80. struct phy *phy;
  81. };
  82. #define to_sunxi_glue(d) container_of(d, struct sunxi_glue, dev)
  83. static u32 USBC_WakeUp_ClearChangeDetect(u32 reg_val)
  84. {
  85. u32 temp = reg_val;
  86. temp &= ~BIT(USBC_BP_ISCR_VBUS_CHANGE_DETECT);
  87. temp &= ~BIT(USBC_BP_ISCR_ID_CHANGE_DETECT);
  88. temp &= ~BIT(USBC_BP_ISCR_DPDM_CHANGE_DETECT);
  89. return temp;
  90. }
  91. static void USBC_EnableIdPullUp(__iomem void *base)
  92. {
  93. u32 reg_val;
  94. reg_val = musb_readl(base, USBC_REG_o_ISCR);
  95. reg_val |= BIT(USBC_BP_ISCR_ID_PULLUP_EN);
  96. reg_val = USBC_WakeUp_ClearChangeDetect(reg_val);
  97. musb_writel(base, USBC_REG_o_ISCR, reg_val);
  98. }
  99. static void USBC_EnableDpDmPullUp(__iomem void *base)
  100. {
  101. u32 reg_val;
  102. reg_val = musb_readl(base, USBC_REG_o_ISCR);
  103. reg_val |= BIT(USBC_BP_ISCR_DPDM_PULLUP_EN);
  104. reg_val = USBC_WakeUp_ClearChangeDetect(reg_val);
  105. musb_writel(base, USBC_REG_o_ISCR, reg_val);
  106. }
  107. static void USBC_ForceIdToLow(__iomem void *base)
  108. {
  109. u32 reg_val;
  110. reg_val = musb_readl(base, USBC_REG_o_ISCR);
  111. reg_val &= ~(0x03 << USBC_BP_ISCR_FORCE_ID);
  112. reg_val |= (0x02 << USBC_BP_ISCR_FORCE_ID);
  113. reg_val = USBC_WakeUp_ClearChangeDetect(reg_val);
  114. musb_writel(base, USBC_REG_o_ISCR, reg_val);
  115. }
  116. static void USBC_ForceIdToHigh(__iomem void *base)
  117. {
  118. u32 reg_val;
  119. reg_val = musb_readl(base, USBC_REG_o_ISCR);
  120. reg_val &= ~(0x03 << USBC_BP_ISCR_FORCE_ID);
  121. reg_val |= (0x03 << USBC_BP_ISCR_FORCE_ID);
  122. reg_val = USBC_WakeUp_ClearChangeDetect(reg_val);
  123. musb_writel(base, USBC_REG_o_ISCR, reg_val);
  124. }
  125. static void USBC_ForceVbusValidToLow(__iomem void *base)
  126. {
  127. u32 reg_val;
  128. reg_val = musb_readl(base, USBC_REG_o_ISCR);
  129. reg_val &= ~(0x03 << USBC_BP_ISCR_FORCE_VBUS_VALID);
  130. reg_val |= (0x02 << USBC_BP_ISCR_FORCE_VBUS_VALID);
  131. reg_val = USBC_WakeUp_ClearChangeDetect(reg_val);
  132. musb_writel(base, USBC_REG_o_ISCR, reg_val);
  133. }
  134. static void USBC_ForceVbusValidToHigh(__iomem void *base)
  135. {
  136. u32 reg_val;
  137. reg_val = musb_readl(base, USBC_REG_o_ISCR);
  138. reg_val &= ~(0x03 << USBC_BP_ISCR_FORCE_VBUS_VALID);
  139. reg_val |= (0x03 << USBC_BP_ISCR_FORCE_VBUS_VALID);
  140. reg_val = USBC_WakeUp_ClearChangeDetect(reg_val);
  141. musb_writel(base, USBC_REG_o_ISCR, reg_val);
  142. }
  143. static void USBC_ConfigFIFO_Base(void)
  144. {
  145. u32 reg_value;
  146. /* config usb fifo, 8kb mode */
  147. reg_value = readl(SUNXI_SRAMC_BASE + 0x04);
  148. reg_value &= ~(0x03 << 0);
  149. reg_value |= BIT(0);
  150. writel(reg_value, SUNXI_SRAMC_BASE + 0x04);
  151. }
  152. /******************************************************************************
  153. * Needed for the DFU polling magic
  154. ******************************************************************************/
  155. static u8 last_int_usb;
  156. bool dfu_usb_get_reset(void)
  157. {
  158. return !!(last_int_usb & MUSB_INTR_RESET);
  159. }
  160. /******************************************************************************
  161. * MUSB Glue code
  162. ******************************************************************************/
  163. static irqreturn_t sunxi_musb_interrupt(int irq, void *__hci)
  164. {
  165. struct musb *musb = __hci;
  166. irqreturn_t retval = IRQ_NONE;
  167. /* read and flush interrupts */
  168. musb->int_usb = musb_readb(musb->mregs, MUSB_INTRUSB);
  169. last_int_usb = musb->int_usb;
  170. if (musb->int_usb)
  171. musb_writeb(musb->mregs, MUSB_INTRUSB, musb->int_usb);
  172. musb->int_tx = musb_readw(musb->mregs, MUSB_INTRTX);
  173. if (musb->int_tx)
  174. musb_writew(musb->mregs, MUSB_INTRTX, musb->int_tx);
  175. musb->int_rx = musb_readw(musb->mregs, MUSB_INTRRX);
  176. if (musb->int_rx)
  177. musb_writew(musb->mregs, MUSB_INTRRX, musb->int_rx);
  178. if (musb->int_usb || musb->int_tx || musb->int_rx)
  179. retval |= musb_interrupt(musb);
  180. return retval;
  181. }
  182. /* musb_core does not call enable / disable in a balanced manner <sigh> */
  183. static bool enabled = false;
  184. static int sunxi_musb_enable(struct musb *musb)
  185. {
  186. struct sunxi_glue *glue = to_sunxi_glue(musb->controller);
  187. int ret;
  188. pr_debug("%s():\n", __func__);
  189. musb_ep_select(musb->mregs, 0);
  190. musb_writeb(musb->mregs, MUSB_FADDR, 0);
  191. if (enabled)
  192. return 0;
  193. /* select PIO mode */
  194. musb_writeb(musb->mregs, USBC_REG_o_VEND0, 0);
  195. if (is_host_enabled(musb)) {
  196. ret = sun4i_usb_phy_vbus_detect(glue->phy);
  197. if (ret == 1) {
  198. printf("A charger is plugged into the OTG: ");
  199. return -ENODEV;
  200. }
  201. ret = sun4i_usb_phy_id_detect(glue->phy);
  202. if (ret == 1) {
  203. printf("No host cable detected: ");
  204. return -ENODEV;
  205. }
  206. ret = generic_phy_power_on(glue->phy);
  207. if (ret) {
  208. pr_err("failed to power on USB PHY\n");
  209. return ret;
  210. }
  211. }
  212. USBC_ForceVbusValidToHigh(musb->mregs);
  213. enabled = true;
  214. return 0;
  215. }
  216. static void sunxi_musb_disable(struct musb *musb)
  217. {
  218. struct sunxi_glue *glue = to_sunxi_glue(musb->controller);
  219. int ret;
  220. pr_debug("%s():\n", __func__);
  221. if (!enabled)
  222. return;
  223. if (is_host_enabled(musb)) {
  224. ret = generic_phy_power_off(glue->phy);
  225. if (ret) {
  226. pr_err("failed to power off USB PHY\n");
  227. return;
  228. }
  229. }
  230. USBC_ForceVbusValidToLow(musb->mregs);
  231. mdelay(200); /* Wait for the current session to timeout */
  232. enabled = false;
  233. }
  234. static int sunxi_musb_init(struct musb *musb)
  235. {
  236. struct sunxi_glue *glue = to_sunxi_glue(musb->controller);
  237. int ret;
  238. pr_debug("%s():\n", __func__);
  239. ret = generic_phy_init(glue->phy);
  240. if (ret) {
  241. pr_err("failed to init USB PHY\n");
  242. return ret;
  243. }
  244. musb->isr = sunxi_musb_interrupt;
  245. setbits_le32(&glue->ccm->ahb_gate0, BIT(AHB_GATE_OFFSET_USB0));
  246. if (glue->cfg->clkgate_bit)
  247. setbits_le32(&glue->ccm->ahb_gate0,
  248. BIT(glue->cfg->clkgate_bit));
  249. #ifdef CONFIG_SUNXI_GEN_SUN6I
  250. setbits_le32(&glue->ccm->ahb_reset0_cfg, BIT(AHB_GATE_OFFSET_USB0));
  251. if (glue->cfg->rst_bit)
  252. setbits_le32(&glue->ccm->ahb_reset0_cfg,
  253. BIT(glue->cfg->rst_bit));
  254. #endif
  255. USBC_ConfigFIFO_Base();
  256. USBC_EnableDpDmPullUp(musb->mregs);
  257. USBC_EnableIdPullUp(musb->mregs);
  258. if (is_host_enabled(musb)) {
  259. /* Host mode */
  260. USBC_ForceIdToLow(musb->mregs);
  261. } else {
  262. /* Peripheral mode */
  263. USBC_ForceIdToHigh(musb->mregs);
  264. }
  265. USBC_ForceVbusValidToHigh(musb->mregs);
  266. return 0;
  267. }
  268. static void sunxi_musb_pre_root_reset_end(struct musb *musb)
  269. {
  270. struct sunxi_glue *glue = to_sunxi_glue(musb->controller);
  271. sun4i_usb_phy_set_squelch_detect(glue->phy, false);
  272. }
  273. static void sunxi_musb_post_root_reset_end(struct musb *musb)
  274. {
  275. struct sunxi_glue *glue = to_sunxi_glue(musb->controller);
  276. sun4i_usb_phy_set_squelch_detect(glue->phy, true);
  277. }
  278. static const struct musb_platform_ops sunxi_musb_ops = {
  279. .init = sunxi_musb_init,
  280. .enable = sunxi_musb_enable,
  281. .disable = sunxi_musb_disable,
  282. .pre_root_reset_end = sunxi_musb_pre_root_reset_end,
  283. .post_root_reset_end = sunxi_musb_post_root_reset_end,
  284. };
  285. /* Allwinner OTG supports up to 5 endpoints */
  286. #define SUNXI_MUSB_MAX_EP_NUM 6
  287. #define SUNXI_MUSB_RAM_BITS 11
  288. static struct musb_fifo_cfg sunxi_musb_mode_cfg[] = {
  289. MUSB_EP_FIFO_SINGLE(1, FIFO_TX, 512),
  290. MUSB_EP_FIFO_SINGLE(1, FIFO_RX, 512),
  291. MUSB_EP_FIFO_SINGLE(2, FIFO_TX, 512),
  292. MUSB_EP_FIFO_SINGLE(2, FIFO_RX, 512),
  293. MUSB_EP_FIFO_SINGLE(3, FIFO_TX, 512),
  294. MUSB_EP_FIFO_SINGLE(3, FIFO_RX, 512),
  295. MUSB_EP_FIFO_SINGLE(4, FIFO_TX, 512),
  296. MUSB_EP_FIFO_SINGLE(4, FIFO_RX, 512),
  297. MUSB_EP_FIFO_SINGLE(5, FIFO_TX, 512),
  298. MUSB_EP_FIFO_SINGLE(5, FIFO_RX, 512),
  299. };
  300. /* H3/V3s OTG supports only 4 endpoints */
  301. #define SUNXI_MUSB_MAX_EP_NUM_H3 5
  302. static struct musb_fifo_cfg sunxi_musb_mode_cfg_h3[] = {
  303. MUSB_EP_FIFO_SINGLE(1, FIFO_TX, 512),
  304. MUSB_EP_FIFO_SINGLE(1, FIFO_RX, 512),
  305. MUSB_EP_FIFO_SINGLE(2, FIFO_TX, 512),
  306. MUSB_EP_FIFO_SINGLE(2, FIFO_RX, 512),
  307. MUSB_EP_FIFO_SINGLE(3, FIFO_TX, 512),
  308. MUSB_EP_FIFO_SINGLE(3, FIFO_RX, 512),
  309. MUSB_EP_FIFO_SINGLE(4, FIFO_TX, 512),
  310. MUSB_EP_FIFO_SINGLE(4, FIFO_RX, 512),
  311. };
  312. static struct musb_hdrc_config musb_config = {
  313. .fifo_cfg = sunxi_musb_mode_cfg,
  314. .fifo_cfg_size = ARRAY_SIZE(sunxi_musb_mode_cfg),
  315. .multipoint = true,
  316. .dyn_fifo = true,
  317. .num_eps = SUNXI_MUSB_MAX_EP_NUM,
  318. .ram_bits = SUNXI_MUSB_RAM_BITS,
  319. };
  320. static struct musb_hdrc_config musb_config_h3 = {
  321. .fifo_cfg = sunxi_musb_mode_cfg_h3,
  322. .fifo_cfg_size = ARRAY_SIZE(sunxi_musb_mode_cfg_h3),
  323. .multipoint = true,
  324. .dyn_fifo = true,
  325. .soft_con = true,
  326. .num_eps = SUNXI_MUSB_MAX_EP_NUM_H3,
  327. .ram_bits = SUNXI_MUSB_RAM_BITS,
  328. };
  329. static int musb_usb_probe(struct udevice *dev)
  330. {
  331. struct sunxi_glue *glue = dev_get_priv(dev);
  332. struct musb_host_data *host = &glue->mdata;
  333. struct usb_bus_priv *priv = dev_get_uclass_priv(dev);
  334. struct musb_hdrc_platform_data pdata;
  335. void *base = dev_read_addr_ptr(dev);
  336. struct phy phy;
  337. int ret;
  338. if (!base)
  339. return -EINVAL;
  340. glue->cfg = (struct sunxi_musb_config *)dev_get_driver_data(dev);
  341. if (!glue->cfg)
  342. return -EINVAL;
  343. glue->ccm = (struct sunxi_ccm_reg *)SUNXI_CCM_BASE;
  344. if (IS_ERR(glue->ccm))
  345. return PTR_ERR(glue->ccm);
  346. ret = generic_phy_get_by_name(dev, "usb", &phy);
  347. if (ret) {
  348. pr_err("failed to get usb PHY\n");
  349. return ret;
  350. }
  351. glue->phy = &phy;
  352. priv->desc_before_addr = true;
  353. memset(&pdata, 0, sizeof(pdata));
  354. pdata.power = 250;
  355. pdata.platform_ops = &sunxi_musb_ops;
  356. pdata.config = glue->cfg->config;
  357. #ifdef CONFIG_USB_MUSB_HOST
  358. pdata.mode = MUSB_HOST;
  359. host->host = musb_init_controller(&pdata, &glue->dev, base);
  360. if (!host->host)
  361. return -EIO;
  362. ret = musb_lowlevel_init(host);
  363. if (!ret)
  364. printf("Allwinner mUSB OTG (Host)\n");
  365. #else
  366. pdata.mode = MUSB_PERIPHERAL;
  367. ret = musb_register(&pdata, &glue->dev, base);
  368. if (!ret)
  369. printf("Allwinner mUSB OTG (Peripheral)\n");
  370. #endif
  371. return ret;
  372. }
  373. static int musb_usb_remove(struct udevice *dev)
  374. {
  375. struct sunxi_glue *glue = dev_get_priv(dev);
  376. struct musb_host_data *host = &glue->mdata;
  377. int ret;
  378. if (generic_phy_valid(glue->phy)) {
  379. ret = generic_phy_exit(glue->phy);
  380. if (ret) {
  381. pr_err("failed to exit %s USB PHY\n", dev->name);
  382. return ret;
  383. }
  384. }
  385. musb_stop(host->host);
  386. #ifdef CONFIG_SUNXI_GEN_SUN6I
  387. clrbits_le32(&glue->ccm->ahb_reset0_cfg, BIT(AHB_GATE_OFFSET_USB0));
  388. if (glue->cfg->rst_bit)
  389. clrbits_le32(&glue->ccm->ahb_reset0_cfg,
  390. BIT(glue->cfg->rst_bit));
  391. #endif
  392. clrbits_le32(&glue->ccm->ahb_gate0, BIT(AHB_GATE_OFFSET_USB0));
  393. if (glue->cfg->clkgate_bit)
  394. clrbits_le32(&glue->ccm->ahb_gate0,
  395. BIT(glue->cfg->clkgate_bit));
  396. free(host->host);
  397. host->host = NULL;
  398. return 0;
  399. }
  400. static const struct sunxi_musb_config sun4i_a10_cfg = {
  401. .config = &musb_config,
  402. };
  403. static const struct sunxi_musb_config sun8i_h3_cfg = {
  404. .config = &musb_config_h3,
  405. .rst_bit = 23,
  406. .clkgate_bit = 23,
  407. };
  408. static const struct udevice_id sunxi_musb_ids[] = {
  409. { .compatible = "allwinner,sun4i-a10-musb",
  410. .data = (ulong)&sun4i_a10_cfg },
  411. { .compatible = "allwinner,sun6i-a31-musb",
  412. .data = (ulong)&sun4i_a10_cfg },
  413. { .compatible = "allwinner,sun8i-a33-musb",
  414. .data = (ulong)&sun4i_a10_cfg },
  415. { .compatible = "allwinner,sun8i-h3-musb",
  416. .data = (ulong)&sun8i_h3_cfg },
  417. { }
  418. };
  419. U_BOOT_DRIVER(usb_musb) = {
  420. .name = "sunxi-musb",
  421. #ifdef CONFIG_USB_MUSB_HOST
  422. .id = UCLASS_USB,
  423. #else
  424. .id = UCLASS_USB_DEV_GENERIC,
  425. #endif
  426. .of_match = sunxi_musb_ids,
  427. .probe = musb_usb_probe,
  428. .remove = musb_usb_remove,
  429. #ifdef CONFIG_USB_MUSB_HOST
  430. .ops = &musb_usb_ops,
  431. #endif
  432. .platdata_auto_alloc_size = sizeof(struct usb_platdata),
  433. .priv_auto_alloc_size = sizeof(struct sunxi_glue),
  434. };