usb_phy.c 8.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406
  1. // SPDX-License-Identifier: GPL-2.0+
  2. /*
  3. * Sunxi usb-phy code
  4. *
  5. * Copyright (C) 2015 Hans de Goede <hdegoede@redhat.com>
  6. * Copyright (C) 2014 Roman Byshko <rbyshko@gmail.com>
  7. *
  8. * Based on code from
  9. * Allwinner Technology Co., Ltd. <www.allwinnertech.com>
  10. */
  11. #include <common.h>
  12. #include <asm/arch/clock.h>
  13. #include <asm/arch/cpu.h>
  14. #include <asm/arch/usb_phy.h>
  15. #include <asm/gpio.h>
  16. #include <asm/io.h>
  17. #include <errno.h>
  18. #if defined(CONFIG_MACH_SUN4I) || \
  19. defined(CONFIG_MACH_SUN5I) || \
  20. defined(CONFIG_MACH_SUN6I) || \
  21. defined(CONFIG_MACH_SUN7I) || \
  22. defined(CONFIG_MACH_SUN8I_A23) || \
  23. defined(CONFIG_MACH_SUN9I)
  24. #define SUNXI_USB_CSR 0x404
  25. #else
  26. #define SUNXI_USB_CSR 0x410
  27. #endif
  28. #define SUNXI_USB_PMU_IRQ_ENABLE 0x800
  29. #define SUNXI_USB_PASSBY_EN 1
  30. #define SUNXI_EHCI_AHB_ICHR8_EN (1 << 10)
  31. #define SUNXI_EHCI_AHB_INCR4_BURST_EN (1 << 9)
  32. #define SUNXI_EHCI_AHB_INCRX_ALIGN_EN (1 << 8)
  33. #define SUNXI_EHCI_ULPI_BYPASS_EN (1 << 0)
  34. #define REG_PHY_UNK_H3 0x420
  35. #define REG_PMU_UNK_H3 0x810
  36. /* A83T specific control bits for PHY0 */
  37. #define SUNXI_PHY_CTL_VBUSVLDEXT BIT(5)
  38. #define SUNXI_PHY_CTL_SIDDQ BIT(3)
  39. /* A83T HSIC specific bits */
  40. #define SUNXI_EHCI_HS_FORCE BIT(20)
  41. #define SUNXI_EHCI_CONNECT_DET BIT(17)
  42. #define SUNXI_EHCI_CONNECT_INT BIT(16)
  43. #define SUNXI_EHCI_HSIC BIT(1)
  44. static struct sunxi_usb_phy {
  45. int usb_rst_mask;
  46. int gpio_vbus;
  47. int gpio_vbus_det;
  48. int gpio_id_det;
  49. int id;
  50. int init_count;
  51. int power_on_count;
  52. ulong base;
  53. } sunxi_usb_phy[] = {
  54. {
  55. .usb_rst_mask = CCM_USB_CTRL_PHY0_RST | CCM_USB_CTRL_PHY0_CLK,
  56. .id = 0,
  57. .base = SUNXI_USB0_BASE,
  58. },
  59. {
  60. .usb_rst_mask = CCM_USB_CTRL_PHY1_RST | CCM_USB_CTRL_PHY1_CLK,
  61. .id = 1,
  62. .base = SUNXI_USB1_BASE,
  63. },
  64. #if CONFIG_SUNXI_USB_PHYS >= 3
  65. {
  66. #ifdef CONFIG_MACH_SUN8I_A83T
  67. .usb_rst_mask = CCM_USB_CTRL_HSIC_RST | CCM_USB_CTRL_HSIC_CLK |
  68. CCM_USB_CTRL_12M_CLK,
  69. #else
  70. .usb_rst_mask = CCM_USB_CTRL_PHY2_RST | CCM_USB_CTRL_PHY2_CLK,
  71. #endif
  72. .id = 2,
  73. .base = SUNXI_USB2_BASE,
  74. },
  75. #endif
  76. #if CONFIG_SUNXI_USB_PHYS >= 4
  77. {
  78. .usb_rst_mask = CCM_USB_CTRL_PHY3_RST | CCM_USB_CTRL_PHY3_CLK,
  79. .id = 3,
  80. .base = SUNXI_USB3_BASE,
  81. }
  82. #endif
  83. };
  84. static int initial_usb_scan_delay = CONFIG_INITIAL_USB_SCAN_DELAY;
  85. static int get_vbus_gpio(int index)
  86. {
  87. switch (index) {
  88. case 0: return sunxi_name_to_gpio(CONFIG_USB0_VBUS_PIN);
  89. case 1: return sunxi_name_to_gpio(CONFIG_USB1_VBUS_PIN);
  90. case 2: return sunxi_name_to_gpio(CONFIG_USB2_VBUS_PIN);
  91. case 3: return sunxi_name_to_gpio(CONFIG_USB3_VBUS_PIN);
  92. }
  93. return -EINVAL;
  94. }
  95. static int get_vbus_detect_gpio(int index)
  96. {
  97. switch (index) {
  98. case 0: return sunxi_name_to_gpio(CONFIG_USB0_VBUS_DET);
  99. }
  100. return -EINVAL;
  101. }
  102. static int get_id_detect_gpio(int index)
  103. {
  104. switch (index) {
  105. case 0: return sunxi_name_to_gpio(CONFIG_USB0_ID_DET);
  106. }
  107. return -EINVAL;
  108. }
  109. __maybe_unused static void usb_phy_write(struct sunxi_usb_phy *phy, int addr,
  110. int data, int len)
  111. {
  112. int j = 0, usbc_bit = 0;
  113. void *dest = (void *)SUNXI_USB0_BASE + SUNXI_USB_CSR;
  114. #ifdef CONFIG_MACH_SUN8I_A33
  115. /* CSR needs to be explicitly initialized to 0 on A33 */
  116. writel(0, dest);
  117. #endif
  118. usbc_bit = 1 << (phy->id * 2);
  119. for (j = 0; j < len; j++) {
  120. /* set the bit address to be written */
  121. clrbits_le32(dest, 0xff << 8);
  122. setbits_le32(dest, (addr + j) << 8);
  123. clrbits_le32(dest, usbc_bit);
  124. /* set data bit */
  125. if (data & 0x1)
  126. setbits_le32(dest, 1 << 7);
  127. else
  128. clrbits_le32(dest, 1 << 7);
  129. setbits_le32(dest, usbc_bit);
  130. clrbits_le32(dest, usbc_bit);
  131. data >>= 1;
  132. }
  133. }
  134. #if defined(CONFIG_MACH_SUNXI_H3_H5) || defined(CONFIG_MACH_SUN50I)
  135. static void sunxi_usb_phy_config(struct sunxi_usb_phy *phy)
  136. {
  137. #if defined CONFIG_MACH_SUNXI_H3_H5
  138. if (phy->id == 0)
  139. clrbits_le32(SUNXI_USBPHY_BASE + REG_PHY_UNK_H3, 0x01);
  140. #endif
  141. clrbits_le32(phy->base + REG_PMU_UNK_H3, 0x02);
  142. }
  143. #elif defined CONFIG_MACH_SUN8I_A83T
  144. static void sunxi_usb_phy_config(struct sunxi_usb_phy *phy)
  145. {
  146. }
  147. #else
  148. static void sunxi_usb_phy_config(struct sunxi_usb_phy *phy)
  149. {
  150. /* The following comments are machine
  151. * translated from Chinese, you have been warned!
  152. */
  153. /* Regulation 45 ohms */
  154. if (phy->id == 0)
  155. usb_phy_write(phy, 0x0c, 0x01, 1);
  156. /* adjust PHY's magnitude and rate */
  157. usb_phy_write(phy, 0x20, 0x14, 5);
  158. /* threshold adjustment disconnect */
  159. #if defined CONFIG_MACH_SUN5I || defined CONFIG_MACH_SUN7I
  160. usb_phy_write(phy, 0x2a, 2, 2);
  161. #else
  162. usb_phy_write(phy, 0x2a, 3, 2);
  163. #endif
  164. return;
  165. }
  166. #endif
  167. static void sunxi_usb_phy_passby(struct sunxi_usb_phy *phy, int enable)
  168. {
  169. unsigned long bits = 0;
  170. void *addr;
  171. addr = (void *)phy->base + SUNXI_USB_PMU_IRQ_ENABLE;
  172. bits = SUNXI_EHCI_AHB_ICHR8_EN |
  173. SUNXI_EHCI_AHB_INCR4_BURST_EN |
  174. SUNXI_EHCI_AHB_INCRX_ALIGN_EN |
  175. SUNXI_EHCI_ULPI_BYPASS_EN;
  176. #ifdef CONFIG_MACH_SUN8I_A83T
  177. if (phy->id == 2)
  178. bits |= SUNXI_EHCI_HS_FORCE |
  179. SUNXI_EHCI_CONNECT_INT |
  180. SUNXI_EHCI_HSIC;
  181. #endif
  182. if (enable)
  183. setbits_le32(addr, bits);
  184. else
  185. clrbits_le32(addr, bits);
  186. return;
  187. }
  188. void sunxi_usb_phy_enable_squelch_detect(int index, int enable)
  189. {
  190. #ifndef CONFIG_MACH_SUN8I_A83T
  191. struct sunxi_usb_phy *phy = &sunxi_usb_phy[index];
  192. usb_phy_write(phy, 0x3c, enable ? 0 : 2, 2);
  193. #endif
  194. }
  195. void sunxi_usb_phy_init(int index)
  196. {
  197. struct sunxi_usb_phy *phy = &sunxi_usb_phy[index];
  198. struct sunxi_ccm_reg *ccm = (struct sunxi_ccm_reg *)SUNXI_CCM_BASE;
  199. phy->init_count++;
  200. if (phy->init_count != 1)
  201. return;
  202. setbits_le32(&ccm->usb_clk_cfg, phy->usb_rst_mask);
  203. sunxi_usb_phy_config(phy);
  204. if (phy->id != 0)
  205. sunxi_usb_phy_passby(phy, SUNXI_USB_PASSBY_EN);
  206. #ifdef CONFIG_MACH_SUN8I_A83T
  207. if (phy->id == 0) {
  208. setbits_le32(SUNXI_USB0_BASE + SUNXI_USB_CSR,
  209. SUNXI_PHY_CTL_VBUSVLDEXT);
  210. clrbits_le32(SUNXI_USB0_BASE + SUNXI_USB_CSR,
  211. SUNXI_PHY_CTL_SIDDQ);
  212. }
  213. #endif
  214. }
  215. void sunxi_usb_phy_exit(int index)
  216. {
  217. struct sunxi_usb_phy *phy = &sunxi_usb_phy[index];
  218. struct sunxi_ccm_reg *ccm = (struct sunxi_ccm_reg *)SUNXI_CCM_BASE;
  219. phy->init_count--;
  220. if (phy->init_count != 0)
  221. return;
  222. if (phy->id != 0)
  223. sunxi_usb_phy_passby(phy, !SUNXI_USB_PASSBY_EN);
  224. #ifdef CONFIG_MACH_SUN8I_A83T
  225. if (phy->id == 0) {
  226. setbits_le32(SUNXI_USB0_BASE + SUNXI_USB_CSR,
  227. SUNXI_PHY_CTL_SIDDQ);
  228. }
  229. #endif
  230. clrbits_le32(&ccm->usb_clk_cfg, phy->usb_rst_mask);
  231. }
  232. void sunxi_usb_phy_power_on(int index)
  233. {
  234. struct sunxi_usb_phy *phy = &sunxi_usb_phy[index];
  235. if (initial_usb_scan_delay) {
  236. mdelay(initial_usb_scan_delay);
  237. initial_usb_scan_delay = 0;
  238. }
  239. phy->power_on_count++;
  240. if (phy->power_on_count != 1)
  241. return;
  242. if (phy->gpio_vbus >= 0)
  243. gpio_set_value(phy->gpio_vbus, 1);
  244. }
  245. void sunxi_usb_phy_power_off(int index)
  246. {
  247. struct sunxi_usb_phy *phy = &sunxi_usb_phy[index];
  248. phy->power_on_count--;
  249. if (phy->power_on_count != 0)
  250. return;
  251. if (phy->gpio_vbus >= 0)
  252. gpio_set_value(phy->gpio_vbus, 0);
  253. }
  254. int sunxi_usb_phy_vbus_detect(int index)
  255. {
  256. struct sunxi_usb_phy *phy = &sunxi_usb_phy[index];
  257. int err, retries = 3;
  258. if (phy->gpio_vbus_det < 0)
  259. return phy->gpio_vbus_det;
  260. err = gpio_get_value(phy->gpio_vbus_det);
  261. /*
  262. * Vbus may have been provided by the board and just been turned of
  263. * some milliseconds ago on reset, what we're measuring then is a
  264. * residual charge on Vbus, sleep a bit and try again.
  265. */
  266. while (err > 0 && retries--) {
  267. mdelay(100);
  268. err = gpio_get_value(phy->gpio_vbus_det);
  269. }
  270. return err;
  271. }
  272. int sunxi_usb_phy_id_detect(int index)
  273. {
  274. struct sunxi_usb_phy *phy = &sunxi_usb_phy[index];
  275. if (phy->gpio_id_det < 0)
  276. return phy->gpio_id_det;
  277. return gpio_get_value(phy->gpio_id_det);
  278. }
  279. int sunxi_usb_phy_probe(void)
  280. {
  281. struct sunxi_ccm_reg *ccm = (struct sunxi_ccm_reg *)SUNXI_CCM_BASE;
  282. struct sunxi_usb_phy *phy;
  283. int i, ret = 0;
  284. for (i = 0; i < CONFIG_SUNXI_USB_PHYS; i++) {
  285. phy = &sunxi_usb_phy[i];
  286. phy->gpio_vbus = get_vbus_gpio(i);
  287. if (phy->gpio_vbus >= 0) {
  288. ret = gpio_request(phy->gpio_vbus, "usb_vbus");
  289. if (ret)
  290. return ret;
  291. ret = gpio_direction_output(phy->gpio_vbus, 0);
  292. if (ret)
  293. return ret;
  294. }
  295. phy->gpio_vbus_det = get_vbus_detect_gpio(i);
  296. if (phy->gpio_vbus_det >= 0) {
  297. ret = gpio_request(phy->gpio_vbus_det, "usb_vbus_det");
  298. if (ret)
  299. return ret;
  300. ret = gpio_direction_input(phy->gpio_vbus_det);
  301. if (ret)
  302. return ret;
  303. }
  304. phy->gpio_id_det = get_id_detect_gpio(i);
  305. if (phy->gpio_id_det >= 0) {
  306. ret = gpio_request(phy->gpio_id_det, "usb_id_det");
  307. if (ret)
  308. return ret;
  309. ret = gpio_direction_input(phy->gpio_id_det);
  310. if (ret)
  311. return ret;
  312. sunxi_gpio_set_pull(phy->gpio_id_det,
  313. SUNXI_GPIO_PULL_UP);
  314. }
  315. }
  316. setbits_le32(&ccm->usb_clk_cfg, CCM_USB_CTRL_PHYGATE);
  317. return 0;
  318. }
  319. int sunxi_usb_phy_remove(void)
  320. {
  321. struct sunxi_ccm_reg *ccm = (struct sunxi_ccm_reg *)SUNXI_CCM_BASE;
  322. struct sunxi_usb_phy *phy;
  323. int i;
  324. clrbits_le32(&ccm->usb_clk_cfg, CCM_USB_CTRL_PHYGATE);
  325. for (i = 0; i < CONFIG_SUNXI_USB_PHYS; i++) {
  326. phy = &sunxi_usb_phy[i];
  327. if (phy->gpio_vbus >= 0)
  328. gpio_free(phy->gpio_vbus);
  329. if (phy->gpio_vbus_det >= 0)
  330. gpio_free(phy->gpio_vbus_det);
  331. if (phy->gpio_id_det >= 0)
  332. gpio_free(phy->gpio_id_det);
  333. }
  334. return 0;
  335. }