sunxi.c 9.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380
  1. /*
  2. * Allwinner SUNXI "glue layer"
  3. *
  4. * Copyright © 2015 Hans de Goede <hdegoede@redhat.com>
  5. * Copyright © 2013 Jussi Kivilinna <jussi.kivilinna@iki.fi>
  6. *
  7. * Based on the sw_usb "Allwinner OTG Dual Role Controller" code.
  8. * Copyright 2007-2012 (C) Allwinner Technology Co., Ltd.
  9. * javen <javen@allwinnertech.com>
  10. *
  11. * Based on the DA8xx "glue layer" code.
  12. * Copyright (c) 2008-2009 MontaVista Software, Inc. <source@mvista.com>
  13. * Copyright (C) 2005-2006 by Texas Instruments
  14. *
  15. * This file is part of the Inventra Controller Driver for Linux.
  16. *
  17. * SPDX-License-Identifier: GPL-2.0
  18. */
  19. #include <common.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. static u32 USBC_WakeUp_ClearChangeDetect(u32 reg_val)
  70. {
  71. u32 temp = reg_val;
  72. temp &= ~(1 << USBC_BP_ISCR_VBUS_CHANGE_DETECT);
  73. temp &= ~(1 << USBC_BP_ISCR_ID_CHANGE_DETECT);
  74. temp &= ~(1 << USBC_BP_ISCR_DPDM_CHANGE_DETECT);
  75. return temp;
  76. }
  77. static void USBC_EnableIdPullUp(__iomem void *base)
  78. {
  79. u32 reg_val;
  80. reg_val = musb_readl(base, USBC_REG_o_ISCR);
  81. reg_val |= (1 << USBC_BP_ISCR_ID_PULLUP_EN);
  82. reg_val = USBC_WakeUp_ClearChangeDetect(reg_val);
  83. musb_writel(base, USBC_REG_o_ISCR, reg_val);
  84. }
  85. static void USBC_EnableDpDmPullUp(__iomem void *base)
  86. {
  87. u32 reg_val;
  88. reg_val = musb_readl(base, USBC_REG_o_ISCR);
  89. reg_val |= (1 << USBC_BP_ISCR_DPDM_PULLUP_EN);
  90. reg_val = USBC_WakeUp_ClearChangeDetect(reg_val);
  91. musb_writel(base, USBC_REG_o_ISCR, reg_val);
  92. }
  93. static void USBC_ForceIdToLow(__iomem void *base)
  94. {
  95. u32 reg_val;
  96. reg_val = musb_readl(base, USBC_REG_o_ISCR);
  97. reg_val &= ~(0x03 << USBC_BP_ISCR_FORCE_ID);
  98. reg_val |= (0x02 << USBC_BP_ISCR_FORCE_ID);
  99. reg_val = USBC_WakeUp_ClearChangeDetect(reg_val);
  100. musb_writel(base, USBC_REG_o_ISCR, reg_val);
  101. }
  102. static void USBC_ForceIdToHigh(__iomem void *base)
  103. {
  104. u32 reg_val;
  105. reg_val = musb_readl(base, USBC_REG_o_ISCR);
  106. reg_val &= ~(0x03 << USBC_BP_ISCR_FORCE_ID);
  107. reg_val |= (0x03 << USBC_BP_ISCR_FORCE_ID);
  108. reg_val = USBC_WakeUp_ClearChangeDetect(reg_val);
  109. musb_writel(base, USBC_REG_o_ISCR, reg_val);
  110. }
  111. static void USBC_ForceVbusValidToLow(__iomem void *base)
  112. {
  113. u32 reg_val;
  114. reg_val = musb_readl(base, USBC_REG_o_ISCR);
  115. reg_val &= ~(0x03 << USBC_BP_ISCR_FORCE_VBUS_VALID);
  116. reg_val |= (0x02 << USBC_BP_ISCR_FORCE_VBUS_VALID);
  117. reg_val = USBC_WakeUp_ClearChangeDetect(reg_val);
  118. musb_writel(base, USBC_REG_o_ISCR, reg_val);
  119. }
  120. static void USBC_ForceVbusValidToHigh(__iomem void *base)
  121. {
  122. u32 reg_val;
  123. reg_val = musb_readl(base, USBC_REG_o_ISCR);
  124. reg_val &= ~(0x03 << USBC_BP_ISCR_FORCE_VBUS_VALID);
  125. reg_val |= (0x03 << USBC_BP_ISCR_FORCE_VBUS_VALID);
  126. reg_val = USBC_WakeUp_ClearChangeDetect(reg_val);
  127. musb_writel(base, USBC_REG_o_ISCR, reg_val);
  128. }
  129. static void USBC_ConfigFIFO_Base(void)
  130. {
  131. u32 reg_value;
  132. /* config usb fifo, 8kb mode */
  133. reg_value = readl(SUNXI_SRAMC_BASE + 0x04);
  134. reg_value &= ~(0x03 << 0);
  135. reg_value |= (1 << 0);
  136. writel(reg_value, SUNXI_SRAMC_BASE + 0x04);
  137. }
  138. /******************************************************************************
  139. * Needed for the DFU polling magic
  140. ******************************************************************************/
  141. static u8 last_int_usb;
  142. bool dfu_usb_get_reset(void)
  143. {
  144. return !!(last_int_usb & MUSB_INTR_RESET);
  145. }
  146. /******************************************************************************
  147. * MUSB Glue code
  148. ******************************************************************************/
  149. static irqreturn_t sunxi_musb_interrupt(int irq, void *__hci)
  150. {
  151. struct musb *musb = __hci;
  152. irqreturn_t retval = IRQ_NONE;
  153. /* read and flush interrupts */
  154. musb->int_usb = musb_readb(musb->mregs, MUSB_INTRUSB);
  155. last_int_usb = musb->int_usb;
  156. if (musb->int_usb)
  157. musb_writeb(musb->mregs, MUSB_INTRUSB, musb->int_usb);
  158. musb->int_tx = musb_readw(musb->mregs, MUSB_INTRTX);
  159. if (musb->int_tx)
  160. musb_writew(musb->mregs, MUSB_INTRTX, musb->int_tx);
  161. musb->int_rx = musb_readw(musb->mregs, MUSB_INTRRX);
  162. if (musb->int_rx)
  163. musb_writew(musb->mregs, MUSB_INTRRX, musb->int_rx);
  164. if (musb->int_usb || musb->int_tx || musb->int_rx)
  165. retval |= musb_interrupt(musb);
  166. return retval;
  167. }
  168. /* musb_core does not call enable / disable in a balanced manner <sigh> */
  169. static bool enabled = false;
  170. static int sunxi_musb_enable(struct musb *musb)
  171. {
  172. int ret;
  173. pr_debug("%s():\n", __func__);
  174. musb_ep_select(musb->mregs, 0);
  175. musb_writeb(musb->mregs, MUSB_FADDR, 0);
  176. if (enabled)
  177. return 0;
  178. /* select PIO mode */
  179. musb_writeb(musb->mregs, USBC_REG_o_VEND0, 0);
  180. if (is_host_enabled(musb)) {
  181. ret = sunxi_usb_phy_vbus_detect(0);
  182. if (ret == 1) {
  183. printf("A charger is plugged into the OTG: ");
  184. return -ENODEV;
  185. }
  186. ret = sunxi_usb_phy_id_detect(0);
  187. if (ret == 1) {
  188. printf("No host cable detected: ");
  189. return -ENODEV;
  190. }
  191. sunxi_usb_phy_power_on(0); /* port power on */
  192. }
  193. USBC_ForceVbusValidToHigh(musb->mregs);
  194. enabled = true;
  195. return 0;
  196. }
  197. static void sunxi_musb_disable(struct musb *musb)
  198. {
  199. pr_debug("%s():\n", __func__);
  200. if (!enabled)
  201. return;
  202. if (is_host_enabled(musb))
  203. sunxi_usb_phy_power_off(0); /* port power off */
  204. USBC_ForceVbusValidToLow(musb->mregs);
  205. mdelay(200); /* Wait for the current session to timeout */
  206. enabled = false;
  207. }
  208. static int sunxi_musb_init(struct musb *musb)
  209. {
  210. struct sunxi_ccm_reg *ccm = (struct sunxi_ccm_reg *)SUNXI_CCM_BASE;
  211. pr_debug("%s():\n", __func__);
  212. musb->isr = sunxi_musb_interrupt;
  213. setbits_le32(&ccm->ahb_gate0, 1 << AHB_GATE_OFFSET_USB0);
  214. #ifdef CONFIG_SUNXI_GEN_SUN6I
  215. setbits_le32(&ccm->ahb_reset0_cfg, 1 << AHB_GATE_OFFSET_USB0);
  216. #endif
  217. sunxi_usb_phy_init(0);
  218. USBC_ConfigFIFO_Base();
  219. USBC_EnableDpDmPullUp(musb->mregs);
  220. USBC_EnableIdPullUp(musb->mregs);
  221. if (is_host_enabled(musb)) {
  222. /* Host mode */
  223. USBC_ForceIdToLow(musb->mregs);
  224. } else {
  225. /* Peripheral mode */
  226. USBC_ForceIdToHigh(musb->mregs);
  227. }
  228. USBC_ForceVbusValidToHigh(musb->mregs);
  229. return 0;
  230. }
  231. static const struct musb_platform_ops sunxi_musb_ops = {
  232. .init = sunxi_musb_init,
  233. .enable = sunxi_musb_enable,
  234. .disable = sunxi_musb_disable,
  235. };
  236. static struct musb_hdrc_config musb_config = {
  237. .multipoint = 1,
  238. .dyn_fifo = 1,
  239. .num_eps = 6,
  240. .ram_bits = 11,
  241. };
  242. static struct musb_hdrc_platform_data musb_plat = {
  243. #if defined(CONFIG_USB_MUSB_HOST)
  244. .mode = MUSB_HOST,
  245. #else
  246. .mode = MUSB_PERIPHERAL,
  247. #endif
  248. .config = &musb_config,
  249. .power = 250,
  250. .platform_ops = &sunxi_musb_ops,
  251. };
  252. #ifdef CONFIG_USB_MUSB_HOST
  253. static int musb_usb_remove(struct udevice *dev);
  254. static int musb_usb_probe(struct udevice *dev)
  255. {
  256. struct musb_host_data *host = dev_get_priv(dev);
  257. struct usb_bus_priv *priv = dev_get_uclass_priv(dev);
  258. int ret;
  259. priv->desc_before_addr = true;
  260. host->host = musb_init_controller(&musb_plat, NULL,
  261. (void *)SUNXI_USB0_BASE);
  262. if (!host->host)
  263. return -EIO;
  264. ret = musb_lowlevel_init(host);
  265. if (ret == 0)
  266. printf("MUSB OTG\n");
  267. else
  268. musb_usb_remove(dev);
  269. return ret;
  270. }
  271. static int musb_usb_remove(struct udevice *dev)
  272. {
  273. struct musb_host_data *host = dev_get_priv(dev);
  274. struct sunxi_ccm_reg *ccm = (struct sunxi_ccm_reg *)SUNXI_CCM_BASE;
  275. musb_stop(host->host);
  276. sunxi_usb_phy_exit(0);
  277. #ifdef CONFIG_SUNXI_GEN_SUN6I
  278. clrbits_le32(&ccm->ahb_reset0_cfg, 1 << AHB_GATE_OFFSET_USB0);
  279. #endif
  280. clrbits_le32(&ccm->ahb_gate0, 1 << AHB_GATE_OFFSET_USB0);
  281. free(host->host);
  282. host->host = NULL;
  283. return 0;
  284. }
  285. U_BOOT_DRIVER(usb_musb) = {
  286. .name = "sunxi-musb",
  287. .id = UCLASS_USB,
  288. .probe = musb_usb_probe,
  289. .remove = musb_usb_remove,
  290. .ops = &musb_usb_ops,
  291. .platdata_auto_alloc_size = sizeof(struct usb_platdata),
  292. .priv_auto_alloc_size = sizeof(struct musb_host_data),
  293. };
  294. #endif
  295. void sunxi_musb_board_init(void)
  296. {
  297. #ifdef CONFIG_USB_MUSB_HOST
  298. struct udevice *dev;
  299. /*
  300. * Bind the driver directly for now as musb linux kernel support is
  301. * still pending upstream so our dts files do not have the necessary
  302. * nodes yet. TODO: Remove this as soon as the dts nodes are in place
  303. * and bind by compatible instead.
  304. */
  305. device_bind_driver(dm_root(), "sunxi-musb", "sunxi-musb", &dev);
  306. #else
  307. musb_register(&musb_plat, NULL, (void *)SUNXI_USB0_BASE);
  308. #endif
  309. }