usb_phy.c 7.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336
  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. #ifdef CONFIG_AXP152_POWER
  20. #include <axp152.h>
  21. #endif
  22. #ifdef CONFIG_AXP209_POWER
  23. #include <axp209.h>
  24. #endif
  25. #ifdef CONFIG_AXP221_POWER
  26. #include <axp221.h>
  27. #endif
  28. #define SUNXI_USB_PMU_IRQ_ENABLE 0x800
  29. #ifdef CONFIG_MACH_SUN8I_A33
  30. #define SUNXI_USB_CSR 0x410
  31. #else
  32. #define SUNXI_USB_CSR 0x404
  33. #endif
  34. #define SUNXI_USB_PASSBY_EN 1
  35. #define SUNXI_EHCI_AHB_ICHR8_EN (1 << 10)
  36. #define SUNXI_EHCI_AHB_INCR4_BURST_EN (1 << 9)
  37. #define SUNXI_EHCI_AHB_INCRX_ALIGN_EN (1 << 8)
  38. #define SUNXI_EHCI_ULPI_BYPASS_EN (1 << 0)
  39. static struct sunxi_usb_phy {
  40. int usb_rst_mask;
  41. int gpio_vbus;
  42. int gpio_vbus_det;
  43. int gpio_id_det;
  44. int id;
  45. int init_count;
  46. int power_on_count;
  47. } sunxi_usb_phy[] = {
  48. {
  49. .usb_rst_mask = CCM_USB_CTRL_PHY0_RST | CCM_USB_CTRL_PHY0_CLK,
  50. .id = 0,
  51. },
  52. {
  53. .usb_rst_mask = CCM_USB_CTRL_PHY1_RST | CCM_USB_CTRL_PHY1_CLK,
  54. .id = 1,
  55. },
  56. #if CONFIG_SUNXI_USB_PHYS >= 3
  57. {
  58. .usb_rst_mask = CCM_USB_CTRL_PHY2_RST | CCM_USB_CTRL_PHY2_CLK,
  59. .id = 2,
  60. }
  61. #endif
  62. };
  63. static int get_vbus_gpio(int index)
  64. {
  65. switch (index) {
  66. case 0: return sunxi_name_to_gpio(CONFIG_USB0_VBUS_PIN);
  67. case 1: return sunxi_name_to_gpio(CONFIG_USB1_VBUS_PIN);
  68. case 2: return sunxi_name_to_gpio(CONFIG_USB2_VBUS_PIN);
  69. }
  70. return -EINVAL;
  71. }
  72. static int get_vbus_detect_gpio(int index)
  73. {
  74. switch (index) {
  75. case 0: return sunxi_name_to_gpio(CONFIG_USB0_VBUS_DET);
  76. }
  77. return -EINVAL;
  78. }
  79. static int get_id_detect_gpio(int index)
  80. {
  81. switch (index) {
  82. case 0: return sunxi_name_to_gpio(CONFIG_USB0_ID_DET);
  83. }
  84. return -EINVAL;
  85. }
  86. static void usb_phy_write(struct sunxi_usb_phy *phy, int addr,
  87. int data, int len)
  88. {
  89. int j = 0, usbc_bit = 0;
  90. void *dest = (void *)SUNXI_USB0_BASE + SUNXI_USB_CSR;
  91. #ifdef CONFIG_MACH_SUN8I_A33
  92. /* CSR needs to be explicitly initialized to 0 on A33 */
  93. writel(0, dest);
  94. #endif
  95. usbc_bit = 1 << (phy->id * 2);
  96. for (j = 0; j < len; j++) {
  97. /* set the bit address to be written */
  98. clrbits_le32(dest, 0xff << 8);
  99. setbits_le32(dest, (addr + j) << 8);
  100. clrbits_le32(dest, usbc_bit);
  101. /* set data bit */
  102. if (data & 0x1)
  103. setbits_le32(dest, 1 << 7);
  104. else
  105. clrbits_le32(dest, 1 << 7);
  106. setbits_le32(dest, usbc_bit);
  107. clrbits_le32(dest, usbc_bit);
  108. data >>= 1;
  109. }
  110. }
  111. static void sunxi_usb_phy_config(struct sunxi_usb_phy *phy)
  112. {
  113. /* The following comments are machine
  114. * translated from Chinese, you have been warned!
  115. */
  116. /* Regulation 45 ohms */
  117. if (phy->id == 0)
  118. usb_phy_write(phy, 0x0c, 0x01, 1);
  119. /* adjust PHY's magnitude and rate */
  120. usb_phy_write(phy, 0x20, 0x14, 5);
  121. /* threshold adjustment disconnect */
  122. #if defined CONFIG_MACH_SUN5I || defined CONFIG_MACH_SUN7I
  123. usb_phy_write(phy, 0x2a, 2, 2);
  124. #else
  125. usb_phy_write(phy, 0x2a, 3, 2);
  126. #endif
  127. return;
  128. }
  129. static void sunxi_usb_phy_passby(int index, int enable)
  130. {
  131. unsigned long bits = 0;
  132. void *addr;
  133. if (index == 1)
  134. addr = (void *)SUNXI_USB1_BASE + SUNXI_USB_PMU_IRQ_ENABLE;
  135. else
  136. addr = (void *)SUNXI_USB2_BASE + SUNXI_USB_PMU_IRQ_ENABLE;
  137. bits = SUNXI_EHCI_AHB_ICHR8_EN |
  138. SUNXI_EHCI_AHB_INCR4_BURST_EN |
  139. SUNXI_EHCI_AHB_INCRX_ALIGN_EN |
  140. SUNXI_EHCI_ULPI_BYPASS_EN;
  141. if (enable)
  142. setbits_le32(addr, bits);
  143. else
  144. clrbits_le32(addr, bits);
  145. return;
  146. }
  147. void sunxi_usb_phy_enable_squelch_detect(int index, int enable)
  148. {
  149. struct sunxi_usb_phy *phy = &sunxi_usb_phy[index];
  150. usb_phy_write(phy, 0x3c, enable ? 0 : 2, 2);
  151. }
  152. void sunxi_usb_phy_init(int index)
  153. {
  154. struct sunxi_usb_phy *phy = &sunxi_usb_phy[index];
  155. struct sunxi_ccm_reg *ccm = (struct sunxi_ccm_reg *)SUNXI_CCM_BASE;
  156. phy->init_count++;
  157. if (phy->init_count != 1)
  158. return;
  159. setbits_le32(&ccm->usb_clk_cfg, phy->usb_rst_mask);
  160. sunxi_usb_phy_config(phy);
  161. if (phy->id != 0)
  162. sunxi_usb_phy_passby(index, SUNXI_USB_PASSBY_EN);
  163. }
  164. void sunxi_usb_phy_exit(int index)
  165. {
  166. struct sunxi_usb_phy *phy = &sunxi_usb_phy[index];
  167. struct sunxi_ccm_reg *ccm = (struct sunxi_ccm_reg *)SUNXI_CCM_BASE;
  168. phy->init_count--;
  169. if (phy->init_count != 0)
  170. return;
  171. if (phy->id != 0)
  172. sunxi_usb_phy_passby(index, !SUNXI_USB_PASSBY_EN);
  173. clrbits_le32(&ccm->usb_clk_cfg, phy->usb_rst_mask);
  174. }
  175. void sunxi_usb_phy_power_on(int index)
  176. {
  177. struct sunxi_usb_phy *phy = &sunxi_usb_phy[index];
  178. phy->power_on_count++;
  179. if (phy->power_on_count != 1)
  180. return;
  181. if (phy->gpio_vbus >= 0)
  182. gpio_set_value(phy->gpio_vbus, 1);
  183. }
  184. void sunxi_usb_phy_power_off(int index)
  185. {
  186. struct sunxi_usb_phy *phy = &sunxi_usb_phy[index];
  187. phy->power_on_count--;
  188. if (phy->power_on_count != 0)
  189. return;
  190. if (phy->gpio_vbus >= 0)
  191. gpio_set_value(phy->gpio_vbus, 0);
  192. }
  193. int sunxi_usb_phy_vbus_detect(int index)
  194. {
  195. struct sunxi_usb_phy *phy = &sunxi_usb_phy[index];
  196. int err, retries = 3;
  197. if (phy->gpio_vbus_det < 0)
  198. return phy->gpio_vbus_det;
  199. err = gpio_get_value(phy->gpio_vbus_det);
  200. /*
  201. * Vbus may have been provided by the board and just been turned of
  202. * some milliseconds ago on reset, what we're measuring then is a
  203. * residual charge on Vbus, sleep a bit and try again.
  204. */
  205. while (err > 0 && retries--) {
  206. mdelay(100);
  207. err = gpio_get_value(phy->gpio_vbus_det);
  208. }
  209. return err;
  210. }
  211. int sunxi_usb_phy_id_detect(int index)
  212. {
  213. struct sunxi_usb_phy *phy = &sunxi_usb_phy[index];
  214. if (phy->gpio_id_det < 0)
  215. return phy->gpio_id_det;
  216. return gpio_get_value(phy->gpio_id_det);
  217. }
  218. int sunxi_usb_phy_probe(void)
  219. {
  220. struct sunxi_ccm_reg *ccm = (struct sunxi_ccm_reg *)SUNXI_CCM_BASE;
  221. struct sunxi_usb_phy *phy;
  222. int i, ret = 0;
  223. for (i = 0; i < CONFIG_SUNXI_USB_PHYS; i++) {
  224. phy = &sunxi_usb_phy[i];
  225. phy->gpio_vbus = get_vbus_gpio(i);
  226. if (phy->gpio_vbus >= 0) {
  227. ret = gpio_request(phy->gpio_vbus, "usb_vbus");
  228. if (ret)
  229. return ret;
  230. ret = gpio_direction_output(phy->gpio_vbus, 0);
  231. if (ret)
  232. return ret;
  233. }
  234. phy->gpio_vbus_det = get_vbus_detect_gpio(i);
  235. if (phy->gpio_vbus_det >= 0) {
  236. ret = gpio_request(phy->gpio_vbus_det, "usb_vbus_det");
  237. if (ret)
  238. return ret;
  239. ret = gpio_direction_input(phy->gpio_vbus_det);
  240. if (ret)
  241. return ret;
  242. }
  243. phy->gpio_id_det = get_id_detect_gpio(i);
  244. if (phy->gpio_id_det >= 0) {
  245. ret = gpio_request(phy->gpio_id_det, "usb_id_det");
  246. if (ret)
  247. return ret;
  248. ret = gpio_direction_input(phy->gpio_id_det);
  249. if (ret)
  250. return ret;
  251. sunxi_gpio_set_pull(phy->gpio_id_det,
  252. SUNXI_GPIO_PULL_UP);
  253. }
  254. }
  255. setbits_le32(&ccm->usb_clk_cfg, CCM_USB_CTRL_PHYGATE);
  256. return 0;
  257. }
  258. int sunxi_usb_phy_remove(void)
  259. {
  260. struct sunxi_ccm_reg *ccm = (struct sunxi_ccm_reg *)SUNXI_CCM_BASE;
  261. struct sunxi_usb_phy *phy;
  262. int i;
  263. clrbits_le32(&ccm->usb_clk_cfg, CCM_USB_CTRL_PHYGATE);
  264. for (i = 0; i < CONFIG_SUNXI_USB_PHYS; i++) {
  265. phy = &sunxi_usb_phy[i];
  266. if (phy->gpio_vbus >= 0)
  267. gpio_free(phy->gpio_vbus);
  268. if (phy->gpio_vbus_det >= 0)
  269. gpio_free(phy->gpio_vbus_det);
  270. if (phy->gpio_id_det >= 0)
  271. gpio_free(phy->gpio_id_det);
  272. }
  273. return 0;
  274. }