sunxi.c 9.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357
  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. int ret;
  168. pr_debug("%s():\n", __func__);
  169. if (enabled)
  170. return 0;
  171. /* select PIO mode */
  172. musb_writeb(musb->mregs, USBC_REG_o_VEND0, 0);
  173. if (is_host_enabled(musb)) {
  174. ret = sunxi_usb_phy_vbus_detect(0);
  175. if (ret) {
  176. printf("A charger is plugged into the OTG: ");
  177. return -ENODEV;
  178. }
  179. ret = sunxi_usb_phy_id_detect(0);
  180. if (ret == 1) {
  181. printf("No host cable detected: ");
  182. return -ENODEV;
  183. }
  184. sunxi_usb_phy_power_on(0); /* port power on */
  185. }
  186. USBC_ForceVbusValidToHigh(musb->mregs);
  187. enabled = true;
  188. return 0;
  189. }
  190. static void sunxi_musb_disable(struct musb *musb)
  191. {
  192. pr_debug("%s():\n", __func__);
  193. if (!enabled)
  194. return;
  195. if (is_host_enabled(musb))
  196. sunxi_usb_phy_power_off(0); /* port power off */
  197. USBC_ForceVbusValidToLow(musb->mregs);
  198. mdelay(200); /* Wait for the current session to timeout */
  199. enabled = false;
  200. }
  201. static int sunxi_musb_init(struct musb *musb)
  202. {
  203. struct sunxi_ccm_reg *ccm = (struct sunxi_ccm_reg *)SUNXI_CCM_BASE;
  204. pr_debug("%s():\n", __func__);
  205. musb->isr = sunxi_musb_interrupt;
  206. setbits_le32(&ccm->ahb_gate0, 1 << AHB_GATE_OFFSET_USB0);
  207. #ifdef CONFIG_SUNXI_GEN_SUN6I
  208. setbits_le32(&ccm->ahb_reset0_cfg, 1 << AHB_GATE_OFFSET_USB0);
  209. #endif
  210. sunxi_usb_phy_init(0);
  211. USBC_ConfigFIFO_Base();
  212. USBC_EnableDpDmPullUp(musb->mregs);
  213. USBC_EnableIdPullUp(musb->mregs);
  214. if (is_host_enabled(musb)) {
  215. /* Host mode */
  216. USBC_ForceIdToLow(musb->mregs);
  217. } else {
  218. /* Peripheral mode */
  219. USBC_ForceIdToHigh(musb->mregs);
  220. }
  221. USBC_ForceVbusValidToHigh(musb->mregs);
  222. return 0;
  223. }
  224. static const struct musb_platform_ops sunxi_musb_ops = {
  225. .init = sunxi_musb_init,
  226. .enable = sunxi_musb_enable,
  227. .disable = sunxi_musb_disable,
  228. };
  229. static struct musb_hdrc_config musb_config = {
  230. .multipoint = 1,
  231. .dyn_fifo = 1,
  232. .num_eps = 6,
  233. .ram_bits = 11,
  234. };
  235. static struct musb_hdrc_platform_data musb_plat = {
  236. #if defined(CONFIG_MUSB_HOST)
  237. .mode = MUSB_HOST,
  238. #else
  239. .mode = MUSB_PERIPHERAL,
  240. #endif
  241. .config = &musb_config,
  242. .power = 250,
  243. .platform_ops = &sunxi_musb_ops,
  244. };
  245. #ifdef CONFIG_MUSB_HOST
  246. int musb_usb_probe(struct udevice *dev)
  247. {
  248. struct musb_host_data *host = dev_get_priv(dev);
  249. struct usb_bus_priv *priv = dev_get_uclass_priv(dev);
  250. int ret;
  251. priv->desc_before_addr = true;
  252. if (!host->host) {
  253. host->host = musb_init_controller(&musb_plat, NULL,
  254. (void *)SUNXI_USB0_BASE);
  255. if (!host->host)
  256. return -EIO;
  257. }
  258. ret = musb_lowlevel_init(host);
  259. if (ret == 0)
  260. printf("MUSB OTG\n");
  261. return ret;
  262. }
  263. int musb_usb_remove(struct udevice *dev)
  264. {
  265. struct musb_host_data *host = dev_get_priv(dev);
  266. musb_stop(host->host);
  267. return 0;
  268. }
  269. U_BOOT_DRIVER(usb_musb) = {
  270. .name = "sunxi-musb",
  271. .id = UCLASS_USB,
  272. .probe = musb_usb_probe,
  273. .remove = musb_usb_remove,
  274. .ops = &musb_usb_ops,
  275. .platdata_auto_alloc_size = sizeof(struct usb_platdata),
  276. .priv_auto_alloc_size = sizeof(struct musb_host_data),
  277. };
  278. #endif
  279. void sunxi_musb_board_init(void)
  280. {
  281. #ifdef CONFIG_MUSB_HOST
  282. struct udevice *dev;
  283. /*
  284. * Bind the driver directly for now as musb linux kernel support is
  285. * still pending upstream so our dts files do not have the necessary
  286. * nodes yet. TODO: Remove this as soon as the dts nodes are in place
  287. * and bind by compatible instead.
  288. */
  289. device_bind_driver(dm_root(), "sunxi-musb", "sunxi-musb", &dev);
  290. #else
  291. musb_register(&musb_plat, NULL, (void *)SUNXI_USB0_BASE);
  292. #endif
  293. }