sunxi.c 7.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280
  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. * The Inventra Controller Driver for Linux is free software; you
  18. * can redistribute it and/or modify it under the terms of the GNU
  19. * General Public License version 2 as published by the Free Software
  20. * Foundation.
  21. *
  22. */
  23. #include <common.h>
  24. #include <asm/arch/cpu.h>
  25. #include <asm/arch/clock.h>
  26. #include <asm/arch/gpio.h>
  27. #include <asm/arch/usb_phy.h>
  28. #include <asm-generic/gpio.h>
  29. #include "linux-compat.h"
  30. #include "musb_core.h"
  31. #ifdef CONFIG_AXP152_POWER
  32. #include <axp152.h>
  33. #endif
  34. #ifdef CONFIG_AXP209_POWER
  35. #include <axp209.h>
  36. #endif
  37. #ifdef CONFIG_AXP221_POWER
  38. #include <axp221.h>
  39. #endif
  40. /******************************************************************************
  41. ******************************************************************************
  42. * From the Allwinner driver
  43. ******************************************************************************
  44. ******************************************************************************/
  45. /******************************************************************************
  46. * From include/sunxi_usb_bsp.h
  47. ******************************************************************************/
  48. /* reg offsets */
  49. #define USBC_REG_o_ISCR 0x0400
  50. #define USBC_REG_o_PHYCTL 0x0404
  51. #define USBC_REG_o_PHYBIST 0x0408
  52. #define USBC_REG_o_PHYTUNE 0x040c
  53. #define USBC_REG_o_VEND0 0x0043
  54. /* Interface Status and Control */
  55. #define USBC_BP_ISCR_VBUS_VALID_FROM_DATA 30
  56. #define USBC_BP_ISCR_VBUS_VALID_FROM_VBUS 29
  57. #define USBC_BP_ISCR_EXT_ID_STATUS 28
  58. #define USBC_BP_ISCR_EXT_DM_STATUS 27
  59. #define USBC_BP_ISCR_EXT_DP_STATUS 26
  60. #define USBC_BP_ISCR_MERGED_VBUS_STATUS 25
  61. #define USBC_BP_ISCR_MERGED_ID_STATUS 24
  62. #define USBC_BP_ISCR_ID_PULLUP_EN 17
  63. #define USBC_BP_ISCR_DPDM_PULLUP_EN 16
  64. #define USBC_BP_ISCR_FORCE_ID 14
  65. #define USBC_BP_ISCR_FORCE_VBUS_VALID 12
  66. #define USBC_BP_ISCR_VBUS_VALID_SRC 10
  67. #define USBC_BP_ISCR_HOSC_EN 7
  68. #define USBC_BP_ISCR_VBUS_CHANGE_DETECT 6
  69. #define USBC_BP_ISCR_ID_CHANGE_DETECT 5
  70. #define USBC_BP_ISCR_DPDM_CHANGE_DETECT 4
  71. #define USBC_BP_ISCR_IRQ_ENABLE 3
  72. #define USBC_BP_ISCR_VBUS_CHANGE_DETECT_EN 2
  73. #define USBC_BP_ISCR_ID_CHANGE_DETECT_EN 1
  74. #define USBC_BP_ISCR_DPDM_CHANGE_DETECT_EN 0
  75. /******************************************************************************
  76. * From usbc/usbc.c
  77. ******************************************************************************/
  78. static u32 USBC_WakeUp_ClearChangeDetect(u32 reg_val)
  79. {
  80. u32 temp = reg_val;
  81. temp &= ~(1 << USBC_BP_ISCR_VBUS_CHANGE_DETECT);
  82. temp &= ~(1 << USBC_BP_ISCR_ID_CHANGE_DETECT);
  83. temp &= ~(1 << USBC_BP_ISCR_DPDM_CHANGE_DETECT);
  84. return temp;
  85. }
  86. static void USBC_EnableIdPullUp(__iomem void *base)
  87. {
  88. u32 reg_val;
  89. reg_val = musb_readl(base, USBC_REG_o_ISCR);
  90. reg_val |= (1 << USBC_BP_ISCR_ID_PULLUP_EN);
  91. reg_val = USBC_WakeUp_ClearChangeDetect(reg_val);
  92. musb_writel(base, USBC_REG_o_ISCR, reg_val);
  93. }
  94. static void USBC_EnableDpDmPullUp(__iomem void *base)
  95. {
  96. u32 reg_val;
  97. reg_val = musb_readl(base, USBC_REG_o_ISCR);
  98. reg_val |= (1 << USBC_BP_ISCR_DPDM_PULLUP_EN);
  99. reg_val = USBC_WakeUp_ClearChangeDetect(reg_val);
  100. musb_writel(base, USBC_REG_o_ISCR, reg_val);
  101. }
  102. static void USBC_ForceIdToLow(__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 |= (0x02 << 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_ForceIdToHigh(__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_ID);
  116. reg_val |= (0x03 << USBC_BP_ISCR_FORCE_ID);
  117. reg_val = USBC_WakeUp_ClearChangeDetect(reg_val);
  118. musb_writel(base, USBC_REG_o_ISCR, reg_val);
  119. }
  120. static void USBC_ForceVbusValidToLow(__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 |= (0x02 << 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_ForceVbusValidToHigh(__iomem void *base)
  130. {
  131. u32 reg_val;
  132. reg_val = musb_readl(base, USBC_REG_o_ISCR);
  133. reg_val &= ~(0x03 << USBC_BP_ISCR_FORCE_VBUS_VALID);
  134. reg_val |= (0x03 << USBC_BP_ISCR_FORCE_VBUS_VALID);
  135. reg_val = USBC_WakeUp_ClearChangeDetect(reg_val);
  136. musb_writel(base, USBC_REG_o_ISCR, reg_val);
  137. }
  138. static void USBC_ConfigFIFO_Base(void)
  139. {
  140. u32 reg_value;
  141. /* config usb fifo, 8kb mode */
  142. reg_value = readl(SUNXI_SRAMC_BASE + 0x04);
  143. reg_value &= ~(0x03 << 0);
  144. reg_value |= (1 << 0);
  145. writel(reg_value, SUNXI_SRAMC_BASE + 0x04);
  146. }
  147. /******************************************************************************
  148. * MUSB Glue code
  149. ******************************************************************************/
  150. static irqreturn_t sunxi_musb_interrupt(int irq, void *__hci)
  151. {
  152. struct musb *musb = __hci;
  153. irqreturn_t retval = IRQ_NONE;
  154. /* read and flush interrupts */
  155. musb->int_usb = musb_readb(musb->mregs, MUSB_INTRUSB);
  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. pr_debug("%s():\n", __func__);
  173. if (enabled)
  174. return 0;
  175. /* select PIO mode */
  176. musb_writeb(musb->mregs, USBC_REG_o_VEND0, 0);
  177. if (is_host_enabled(musb))
  178. sunxi_usb_phy_power_on(0); /* port power on */
  179. USBC_ForceVbusValidToHigh(musb->mregs);
  180. enabled = true;
  181. return 0;
  182. }
  183. static void sunxi_musb_disable(struct musb *musb)
  184. {
  185. pr_debug("%s():\n", __func__);
  186. if (!enabled)
  187. return;
  188. if (is_host_enabled(musb))
  189. sunxi_usb_phy_power_off(0); /* port power off */
  190. USBC_ForceVbusValidToLow(musb->mregs);
  191. mdelay(200); /* Wait for the current session to timeout */
  192. enabled = false;
  193. }
  194. static int sunxi_musb_init(struct musb *musb)
  195. {
  196. struct sunxi_ccm_reg *ccm = (struct sunxi_ccm_reg *)SUNXI_CCM_BASE;
  197. int err;
  198. pr_debug("%s():\n", __func__);
  199. if (is_host_enabled(musb)) {
  200. err = sunxi_usb_phy_vbus_detect(0);
  201. if (err) {
  202. eprintf("Error: A charger is plugged into the OTG\n");
  203. return -EIO;
  204. }
  205. }
  206. musb->isr = sunxi_musb_interrupt;
  207. setbits_le32(&ccm->ahb_gate0, 1 << AHB_GATE_OFFSET_USB0);
  208. #ifdef CONFIG_SUNXI_GEN_SUN6I
  209. setbits_le32(&ccm->ahb_reset0_cfg, 1 << AHB_GATE_OFFSET_USB0);
  210. #endif
  211. sunxi_usb_phy_init(0);
  212. USBC_ConfigFIFO_Base();
  213. USBC_EnableDpDmPullUp(musb->mregs);
  214. USBC_EnableIdPullUp(musb->mregs);
  215. if (is_host_enabled(musb)) {
  216. /* Host mode */
  217. USBC_ForceIdToLow(musb->mregs);
  218. } else {
  219. /* Peripheral mode */
  220. USBC_ForceIdToHigh(musb->mregs);
  221. }
  222. USBC_ForceVbusValidToHigh(musb->mregs);
  223. return 0;
  224. }
  225. const struct musb_platform_ops sunxi_musb_ops = {
  226. .init = sunxi_musb_init,
  227. .enable = sunxi_musb_enable,
  228. .disable = sunxi_musb_disable,
  229. };