sunxi.c 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397
  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 <asm/arch/cpu.h>
  21. #include <asm/arch/clock.h>
  22. #include <asm/arch/gpio.h>
  23. #include <asm/arch/usb_phy.h>
  24. #include <asm-generic/gpio.h>
  25. #include <dm/lists.h>
  26. #include <dm/root.h>
  27. #include <linux/usb/musb.h>
  28. #include "linux-compat.h"
  29. #include "musb_core.h"
  30. #include "musb_uboot.h"
  31. /******************************************************************************
  32. ******************************************************************************
  33. * From the Allwinner driver
  34. ******************************************************************************
  35. ******************************************************************************/
  36. /******************************************************************************
  37. * From include/sunxi_usb_bsp.h
  38. ******************************************************************************/
  39. /* reg offsets */
  40. #define USBC_REG_o_ISCR 0x0400
  41. #define USBC_REG_o_PHYCTL 0x0404
  42. #define USBC_REG_o_PHYBIST 0x0408
  43. #define USBC_REG_o_PHYTUNE 0x040c
  44. #define USBC_REG_o_VEND0 0x0043
  45. /* Interface Status and Control */
  46. #define USBC_BP_ISCR_VBUS_VALID_FROM_DATA 30
  47. #define USBC_BP_ISCR_VBUS_VALID_FROM_VBUS 29
  48. #define USBC_BP_ISCR_EXT_ID_STATUS 28
  49. #define USBC_BP_ISCR_EXT_DM_STATUS 27
  50. #define USBC_BP_ISCR_EXT_DP_STATUS 26
  51. #define USBC_BP_ISCR_MERGED_VBUS_STATUS 25
  52. #define USBC_BP_ISCR_MERGED_ID_STATUS 24
  53. #define USBC_BP_ISCR_ID_PULLUP_EN 17
  54. #define USBC_BP_ISCR_DPDM_PULLUP_EN 16
  55. #define USBC_BP_ISCR_FORCE_ID 14
  56. #define USBC_BP_ISCR_FORCE_VBUS_VALID 12
  57. #define USBC_BP_ISCR_VBUS_VALID_SRC 10
  58. #define USBC_BP_ISCR_HOSC_EN 7
  59. #define USBC_BP_ISCR_VBUS_CHANGE_DETECT 6
  60. #define USBC_BP_ISCR_ID_CHANGE_DETECT 5
  61. #define USBC_BP_ISCR_DPDM_CHANGE_DETECT 4
  62. #define USBC_BP_ISCR_IRQ_ENABLE 3
  63. #define USBC_BP_ISCR_VBUS_CHANGE_DETECT_EN 2
  64. #define USBC_BP_ISCR_ID_CHANGE_DETECT_EN 1
  65. #define USBC_BP_ISCR_DPDM_CHANGE_DETECT_EN 0
  66. /******************************************************************************
  67. * From usbc/usbc.c
  68. ******************************************************************************/
  69. struct sunxi_glue {
  70. struct musb_host_data mdata;
  71. struct sunxi_ccm_reg *ccm;
  72. struct device dev;
  73. };
  74. #define to_sunxi_glue(d) container_of(d, struct sunxi_glue, dev)
  75. static u32 USBC_WakeUp_ClearChangeDetect(u32 reg_val)
  76. {
  77. u32 temp = reg_val;
  78. temp &= ~(1 << USBC_BP_ISCR_VBUS_CHANGE_DETECT);
  79. temp &= ~(1 << USBC_BP_ISCR_ID_CHANGE_DETECT);
  80. temp &= ~(1 << USBC_BP_ISCR_DPDM_CHANGE_DETECT);
  81. return temp;
  82. }
  83. static void USBC_EnableIdPullUp(__iomem void *base)
  84. {
  85. u32 reg_val;
  86. reg_val = musb_readl(base, USBC_REG_o_ISCR);
  87. reg_val |= (1 << USBC_BP_ISCR_ID_PULLUP_EN);
  88. reg_val = USBC_WakeUp_ClearChangeDetect(reg_val);
  89. musb_writel(base, USBC_REG_o_ISCR, reg_val);
  90. }
  91. static void USBC_EnableDpDmPullUp(__iomem void *base)
  92. {
  93. u32 reg_val;
  94. reg_val = musb_readl(base, USBC_REG_o_ISCR);
  95. reg_val |= (1 << USBC_BP_ISCR_DPDM_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_ForceIdToLow(__iomem void *base)
  100. {
  101. u32 reg_val;
  102. reg_val = musb_readl(base, USBC_REG_o_ISCR);
  103. reg_val &= ~(0x03 << USBC_BP_ISCR_FORCE_ID);
  104. reg_val |= (0x02 << USBC_BP_ISCR_FORCE_ID);
  105. reg_val = USBC_WakeUp_ClearChangeDetect(reg_val);
  106. musb_writel(base, USBC_REG_o_ISCR, reg_val);
  107. }
  108. static void USBC_ForceIdToHigh(__iomem void *base)
  109. {
  110. u32 reg_val;
  111. reg_val = musb_readl(base, USBC_REG_o_ISCR);
  112. reg_val &= ~(0x03 << USBC_BP_ISCR_FORCE_ID);
  113. reg_val |= (0x03 << USBC_BP_ISCR_FORCE_ID);
  114. reg_val = USBC_WakeUp_ClearChangeDetect(reg_val);
  115. musb_writel(base, USBC_REG_o_ISCR, reg_val);
  116. }
  117. static void USBC_ForceVbusValidToLow(__iomem void *base)
  118. {
  119. u32 reg_val;
  120. reg_val = musb_readl(base, USBC_REG_o_ISCR);
  121. reg_val &= ~(0x03 << USBC_BP_ISCR_FORCE_VBUS_VALID);
  122. reg_val |= (0x02 << USBC_BP_ISCR_FORCE_VBUS_VALID);
  123. reg_val = USBC_WakeUp_ClearChangeDetect(reg_val);
  124. musb_writel(base, USBC_REG_o_ISCR, reg_val);
  125. }
  126. static void USBC_ForceVbusValidToHigh(__iomem void *base)
  127. {
  128. u32 reg_val;
  129. reg_val = musb_readl(base, USBC_REG_o_ISCR);
  130. reg_val &= ~(0x03 << USBC_BP_ISCR_FORCE_VBUS_VALID);
  131. reg_val |= (0x03 << USBC_BP_ISCR_FORCE_VBUS_VALID);
  132. reg_val = USBC_WakeUp_ClearChangeDetect(reg_val);
  133. musb_writel(base, USBC_REG_o_ISCR, reg_val);
  134. }
  135. static void USBC_ConfigFIFO_Base(void)
  136. {
  137. u32 reg_value;
  138. /* config usb fifo, 8kb mode */
  139. reg_value = readl(SUNXI_SRAMC_BASE + 0x04);
  140. reg_value &= ~(0x03 << 0);
  141. reg_value |= (1 << 0);
  142. writel(reg_value, SUNXI_SRAMC_BASE + 0x04);
  143. }
  144. /******************************************************************************
  145. * Needed for the DFU polling magic
  146. ******************************************************************************/
  147. static u8 last_int_usb;
  148. bool dfu_usb_get_reset(void)
  149. {
  150. return !!(last_int_usb & MUSB_INTR_RESET);
  151. }
  152. /******************************************************************************
  153. * MUSB Glue code
  154. ******************************************************************************/
  155. static irqreturn_t sunxi_musb_interrupt(int irq, void *__hci)
  156. {
  157. struct musb *musb = __hci;
  158. irqreturn_t retval = IRQ_NONE;
  159. /* read and flush interrupts */
  160. musb->int_usb = musb_readb(musb->mregs, MUSB_INTRUSB);
  161. last_int_usb = musb->int_usb;
  162. if (musb->int_usb)
  163. musb_writeb(musb->mregs, MUSB_INTRUSB, musb->int_usb);
  164. musb->int_tx = musb_readw(musb->mregs, MUSB_INTRTX);
  165. if (musb->int_tx)
  166. musb_writew(musb->mregs, MUSB_INTRTX, musb->int_tx);
  167. musb->int_rx = musb_readw(musb->mregs, MUSB_INTRRX);
  168. if (musb->int_rx)
  169. musb_writew(musb->mregs, MUSB_INTRRX, musb->int_rx);
  170. if (musb->int_usb || musb->int_tx || musb->int_rx)
  171. retval |= musb_interrupt(musb);
  172. return retval;
  173. }
  174. /* musb_core does not call enable / disable in a balanced manner <sigh> */
  175. static bool enabled = false;
  176. static int sunxi_musb_enable(struct musb *musb)
  177. {
  178. int ret;
  179. pr_debug("%s():\n", __func__);
  180. musb_ep_select(musb->mregs, 0);
  181. musb_writeb(musb->mregs, MUSB_FADDR, 0);
  182. if (enabled)
  183. return 0;
  184. /* select PIO mode */
  185. musb_writeb(musb->mregs, USBC_REG_o_VEND0, 0);
  186. if (is_host_enabled(musb)) {
  187. ret = sunxi_usb_phy_vbus_detect(0);
  188. if (ret == 1) {
  189. printf("A charger is plugged into the OTG: ");
  190. return -ENODEV;
  191. }
  192. ret = sunxi_usb_phy_id_detect(0);
  193. if (ret == 1) {
  194. printf("No host cable detected: ");
  195. return -ENODEV;
  196. }
  197. sunxi_usb_phy_power_on(0); /* port power on */
  198. }
  199. USBC_ForceVbusValidToHigh(musb->mregs);
  200. enabled = true;
  201. return 0;
  202. }
  203. static void sunxi_musb_disable(struct musb *musb)
  204. {
  205. pr_debug("%s():\n", __func__);
  206. if (!enabled)
  207. return;
  208. if (is_host_enabled(musb))
  209. sunxi_usb_phy_power_off(0); /* port power off */
  210. USBC_ForceVbusValidToLow(musb->mregs);
  211. mdelay(200); /* Wait for the current session to timeout */
  212. enabled = false;
  213. }
  214. static int sunxi_musb_init(struct musb *musb)
  215. {
  216. struct sunxi_glue *glue = to_sunxi_glue(musb->controller);
  217. pr_debug("%s():\n", __func__);
  218. musb->isr = sunxi_musb_interrupt;
  219. setbits_le32(&glue->ccm->ahb_gate0, 1 << AHB_GATE_OFFSET_USB0);
  220. #ifdef CONFIG_SUNXI_GEN_SUN6I
  221. setbits_le32(&glue->ccm->ahb_reset0_cfg, 1 << AHB_GATE_OFFSET_USB0);
  222. #endif
  223. sunxi_usb_phy_init(0);
  224. USBC_ConfigFIFO_Base();
  225. USBC_EnableDpDmPullUp(musb->mregs);
  226. USBC_EnableIdPullUp(musb->mregs);
  227. if (is_host_enabled(musb)) {
  228. /* Host mode */
  229. USBC_ForceIdToLow(musb->mregs);
  230. } else {
  231. /* Peripheral mode */
  232. USBC_ForceIdToHigh(musb->mregs);
  233. }
  234. USBC_ForceVbusValidToHigh(musb->mregs);
  235. return 0;
  236. }
  237. static const struct musb_platform_ops sunxi_musb_ops = {
  238. .init = sunxi_musb_init,
  239. .enable = sunxi_musb_enable,
  240. .disable = sunxi_musb_disable,
  241. };
  242. /* Allwinner OTG supports up to 5 endpoints */
  243. #define SUNXI_MUSB_MAX_EP_NUM 6
  244. #define SUNXI_MUSB_RAM_BITS 11
  245. static struct musb_hdrc_config musb_config = {
  246. .multipoint = true,
  247. .dyn_fifo = true,
  248. .num_eps = SUNXI_MUSB_MAX_EP_NUM,
  249. .ram_bits = SUNXI_MUSB_RAM_BITS,
  250. };
  251. static struct musb_hdrc_platform_data musb_plat = {
  252. #if defined(CONFIG_USB_MUSB_HOST)
  253. .mode = MUSB_HOST,
  254. #else
  255. .mode = MUSB_PERIPHERAL,
  256. #endif
  257. .config = &musb_config,
  258. .power = 250,
  259. .platform_ops = &sunxi_musb_ops,
  260. };
  261. static int musb_usb_probe(struct udevice *dev)
  262. {
  263. struct sunxi_glue *glue = dev_get_priv(dev);
  264. struct musb_host_data *host = &glue->mdata;
  265. struct usb_bus_priv *priv = dev_get_uclass_priv(dev);
  266. void *base = dev_read_addr_ptr(dev);
  267. int ret;
  268. if (!base)
  269. return -EINVAL;
  270. glue->ccm = (struct sunxi_ccm_reg *)SUNXI_CCM_BASE;
  271. if (IS_ERR(glue->ccm))
  272. return PTR_ERR(glue->ccm);
  273. priv->desc_before_addr = true;
  274. #ifdef CONFIG_USB_MUSB_HOST
  275. host->host = musb_init_controller(&musb_plat, &glue->dev, base);
  276. if (!host->host)
  277. return -EIO;
  278. ret = musb_lowlevel_init(host);
  279. if (!ret)
  280. printf("Allwinner mUSB OTG (Host)\n");
  281. #else
  282. ret = musb_register(&musb_plat, &glue->dev, base);
  283. if (!ret)
  284. printf("Allwinner mUSB OTG (Peripheral)\n");
  285. #endif
  286. return ret;
  287. }
  288. static int musb_usb_remove(struct udevice *dev)
  289. {
  290. struct sunxi_glue *glue = dev_get_priv(dev);
  291. struct musb_host_data *host = &glue->mdata;
  292. musb_stop(host->host);
  293. sunxi_usb_phy_exit(0);
  294. #ifdef CONFIG_SUNXI_GEN_SUN6I
  295. clrbits_le32(&glue->ccm->ahb_reset0_cfg, 1 << AHB_GATE_OFFSET_USB0);
  296. #endif
  297. clrbits_le32(&glue->ccm->ahb_gate0, 1 << AHB_GATE_OFFSET_USB0);
  298. free(host->host);
  299. host->host = NULL;
  300. return 0;
  301. }
  302. static const struct udevice_id sunxi_musb_ids[] = {
  303. { .compatible = "allwinner,sun4i-a10-musb" },
  304. { .compatible = "allwinner,sun6i-a31-musb" },
  305. { .compatible = "allwinner,sun8i-a33-musb" },
  306. { .compatible = "allwinner,sun8i-h3-musb" },
  307. { }
  308. };
  309. U_BOOT_DRIVER(usb_musb) = {
  310. .name = "sunxi-musb",
  311. #ifdef CONFIG_USB_MUSB_HOST
  312. .id = UCLASS_USB,
  313. #else
  314. .id = UCLASS_USB_DEV_GENERIC,
  315. #endif
  316. .of_match = sunxi_musb_ids,
  317. .probe = musb_usb_probe,
  318. .remove = musb_usb_remove,
  319. #ifdef CONFIG_USB_MUSB_HOST
  320. .ops = &musb_usb_ops,
  321. #endif
  322. .platdata_auto_alloc_size = sizeof(struct usb_platdata),
  323. .priv_auto_alloc_size = sizeof(struct sunxi_glue),
  324. };