usb_phy.c 8.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401
  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) || defined(CONFIG_MACH_SUN50I)
  131. static void sunxi_usb_phy_config(struct sunxi_usb_phy *phy)
  132. {
  133. #if defined CONFIG_MACH_SUN8I_H3
  134. if (phy->id == 0)
  135. clrbits_le32(SUNXI_USBPHY_BASE + REG_PHY_UNK_H3, 0x01);
  136. #endif
  137. clrbits_le32(phy->base + REG_PMU_UNK_H3, 0x02);
  138. }
  139. #elif defined CONFIG_MACH_SUN8I_A83T
  140. static void sunxi_usb_phy_config(struct sunxi_usb_phy *phy)
  141. {
  142. }
  143. #else
  144. static void sunxi_usb_phy_config(struct sunxi_usb_phy *phy)
  145. {
  146. /* The following comments are machine
  147. * translated from Chinese, you have been warned!
  148. */
  149. /* Regulation 45 ohms */
  150. if (phy->id == 0)
  151. usb_phy_write(phy, 0x0c, 0x01, 1);
  152. /* adjust PHY's magnitude and rate */
  153. usb_phy_write(phy, 0x20, 0x14, 5);
  154. /* threshold adjustment disconnect */
  155. #if defined CONFIG_MACH_SUN5I || defined CONFIG_MACH_SUN7I
  156. usb_phy_write(phy, 0x2a, 2, 2);
  157. #else
  158. usb_phy_write(phy, 0x2a, 3, 2);
  159. #endif
  160. return;
  161. }
  162. #endif
  163. static void sunxi_usb_phy_passby(struct sunxi_usb_phy *phy, int enable)
  164. {
  165. unsigned long bits = 0;
  166. void *addr;
  167. addr = (void *)phy->base + SUNXI_USB_PMU_IRQ_ENABLE;
  168. bits = SUNXI_EHCI_AHB_ICHR8_EN |
  169. SUNXI_EHCI_AHB_INCR4_BURST_EN |
  170. SUNXI_EHCI_AHB_INCRX_ALIGN_EN |
  171. SUNXI_EHCI_ULPI_BYPASS_EN;
  172. #ifdef CONFIG_MACH_SUN8I_A83T
  173. if (phy->id == 2)
  174. bits |= SUNXI_EHCI_HS_FORCE |
  175. SUNXI_EHCI_CONNECT_INT |
  176. SUNXI_EHCI_HSIC;
  177. #endif
  178. if (enable)
  179. setbits_le32(addr, bits);
  180. else
  181. clrbits_le32(addr, bits);
  182. return;
  183. }
  184. void sunxi_usb_phy_enable_squelch_detect(int index, int enable)
  185. {
  186. #ifndef CONFIG_MACH_SUN8I_A83T
  187. struct sunxi_usb_phy *phy = &sunxi_usb_phy[index];
  188. usb_phy_write(phy, 0x3c, enable ? 0 : 2, 2);
  189. #endif
  190. }
  191. void sunxi_usb_phy_init(int index)
  192. {
  193. struct sunxi_usb_phy *phy = &sunxi_usb_phy[index];
  194. struct sunxi_ccm_reg *ccm = (struct sunxi_ccm_reg *)SUNXI_CCM_BASE;
  195. phy->init_count++;
  196. if (phy->init_count != 1)
  197. return;
  198. setbits_le32(&ccm->usb_clk_cfg, phy->usb_rst_mask);
  199. sunxi_usb_phy_config(phy);
  200. if (phy->id != 0)
  201. sunxi_usb_phy_passby(phy, SUNXI_USB_PASSBY_EN);
  202. #ifdef CONFIG_MACH_SUN8I_A83T
  203. if (phy->id == 0) {
  204. setbits_le32(SUNXI_USB0_BASE + SUNXI_USB_CSR,
  205. SUNXI_PHY_CTL_VBUSVLDEXT);
  206. clrbits_le32(SUNXI_USB0_BASE + SUNXI_USB_CSR,
  207. SUNXI_PHY_CTL_SIDDQ);
  208. }
  209. #endif
  210. }
  211. void sunxi_usb_phy_exit(int index)
  212. {
  213. struct sunxi_usb_phy *phy = &sunxi_usb_phy[index];
  214. struct sunxi_ccm_reg *ccm = (struct sunxi_ccm_reg *)SUNXI_CCM_BASE;
  215. phy->init_count--;
  216. if (phy->init_count != 0)
  217. return;
  218. if (phy->id != 0)
  219. sunxi_usb_phy_passby(phy, !SUNXI_USB_PASSBY_EN);
  220. #ifdef CONFIG_MACH_SUN8I_A83T
  221. if (phy->id == 0) {
  222. setbits_le32(SUNXI_USB0_BASE + SUNXI_USB_CSR,
  223. SUNXI_PHY_CTL_SIDDQ);
  224. }
  225. #endif
  226. clrbits_le32(&ccm->usb_clk_cfg, phy->usb_rst_mask);
  227. }
  228. void sunxi_usb_phy_power_on(int index)
  229. {
  230. struct sunxi_usb_phy *phy = &sunxi_usb_phy[index];
  231. if (initial_usb_scan_delay) {
  232. mdelay(initial_usb_scan_delay);
  233. initial_usb_scan_delay = 0;
  234. }
  235. phy->power_on_count++;
  236. if (phy->power_on_count != 1)
  237. return;
  238. if (phy->gpio_vbus >= 0)
  239. gpio_set_value(phy->gpio_vbus, 1);
  240. }
  241. void sunxi_usb_phy_power_off(int index)
  242. {
  243. struct sunxi_usb_phy *phy = &sunxi_usb_phy[index];
  244. phy->power_on_count--;
  245. if (phy->power_on_count != 0)
  246. return;
  247. if (phy->gpio_vbus >= 0)
  248. gpio_set_value(phy->gpio_vbus, 0);
  249. }
  250. int sunxi_usb_phy_vbus_detect(int index)
  251. {
  252. struct sunxi_usb_phy *phy = &sunxi_usb_phy[index];
  253. int err, retries = 3;
  254. if (phy->gpio_vbus_det < 0)
  255. return phy->gpio_vbus_det;
  256. err = gpio_get_value(phy->gpio_vbus_det);
  257. /*
  258. * Vbus may have been provided by the board and just been turned of
  259. * some milliseconds ago on reset, what we're measuring then is a
  260. * residual charge on Vbus, sleep a bit and try again.
  261. */
  262. while (err > 0 && retries--) {
  263. mdelay(100);
  264. err = gpio_get_value(phy->gpio_vbus_det);
  265. }
  266. return err;
  267. }
  268. int sunxi_usb_phy_id_detect(int index)
  269. {
  270. struct sunxi_usb_phy *phy = &sunxi_usb_phy[index];
  271. if (phy->gpio_id_det < 0)
  272. return phy->gpio_id_det;
  273. return gpio_get_value(phy->gpio_id_det);
  274. }
  275. int sunxi_usb_phy_probe(void)
  276. {
  277. struct sunxi_ccm_reg *ccm = (struct sunxi_ccm_reg *)SUNXI_CCM_BASE;
  278. struct sunxi_usb_phy *phy;
  279. int i, ret = 0;
  280. for (i = 0; i < CONFIG_SUNXI_USB_PHYS; i++) {
  281. phy = &sunxi_usb_phy[i];
  282. phy->gpio_vbus = get_vbus_gpio(i);
  283. if (phy->gpio_vbus >= 0) {
  284. ret = gpio_request(phy->gpio_vbus, "usb_vbus");
  285. if (ret)
  286. return ret;
  287. ret = gpio_direction_output(phy->gpio_vbus, 0);
  288. if (ret)
  289. return ret;
  290. }
  291. phy->gpio_vbus_det = get_vbus_detect_gpio(i);
  292. if (phy->gpio_vbus_det >= 0) {
  293. ret = gpio_request(phy->gpio_vbus_det, "usb_vbus_det");
  294. if (ret)
  295. return ret;
  296. ret = gpio_direction_input(phy->gpio_vbus_det);
  297. if (ret)
  298. return ret;
  299. }
  300. phy->gpio_id_det = get_id_detect_gpio(i);
  301. if (phy->gpio_id_det >= 0) {
  302. ret = gpio_request(phy->gpio_id_det, "usb_id_det");
  303. if (ret)
  304. return ret;
  305. ret = gpio_direction_input(phy->gpio_id_det);
  306. if (ret)
  307. return ret;
  308. sunxi_gpio_set_pull(phy->gpio_id_det,
  309. SUNXI_GPIO_PULL_UP);
  310. }
  311. }
  312. setbits_le32(&ccm->usb_clk_cfg, CCM_USB_CTRL_PHYGATE);
  313. return 0;
  314. }
  315. int sunxi_usb_phy_remove(void)
  316. {
  317. struct sunxi_ccm_reg *ccm = (struct sunxi_ccm_reg *)SUNXI_CCM_BASE;
  318. struct sunxi_usb_phy *phy;
  319. int i;
  320. clrbits_le32(&ccm->usb_clk_cfg, CCM_USB_CTRL_PHYGATE);
  321. for (i = 0; i < CONFIG_SUNXI_USB_PHYS; i++) {
  322. phy = &sunxi_usb_phy[i];
  323. if (phy->gpio_vbus >= 0)
  324. gpio_free(phy->gpio_vbus);
  325. if (phy->gpio_vbus_det >= 0)
  326. gpio_free(phy->gpio_vbus_det);
  327. if (phy->gpio_id_det >= 0)
  328. gpio_free(phy->gpio_id_det);
  329. }
  330. return 0;
  331. }