usb_phy.c 8.6 KB

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