sunxi.c 9.5 KB

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