phy-exynos4x12-usb2.c 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375
  1. // SPDX-License-Identifier: GPL-2.0-only
  2. /*
  3. * Samsung SoC USB 1.1/2.0 PHY driver - Exynos 4x12 support
  4. *
  5. * Copyright (C) 2013 Samsung Electronics Co., Ltd.
  6. * Author: Kamil Debski <[email protected]>
  7. */
  8. #include <linux/delay.h>
  9. #include <linux/io.h>
  10. #include <linux/phy/phy.h>
  11. #include <linux/regmap.h>
  12. #include "phy-samsung-usb2.h"
  13. /* Exynos USB PHY registers */
  14. /* PHY power control */
  15. #define EXYNOS_4x12_UPHYPWR 0x0
  16. #define EXYNOS_4x12_UPHYPWR_PHY0_SUSPEND BIT(0)
  17. #define EXYNOS_4x12_UPHYPWR_PHY0_PWR BIT(3)
  18. #define EXYNOS_4x12_UPHYPWR_PHY0_OTG_PWR BIT(4)
  19. #define EXYNOS_4x12_UPHYPWR_PHY0_SLEEP BIT(5)
  20. #define EXYNOS_4x12_UPHYPWR_PHY0 ( \
  21. EXYNOS_4x12_UPHYPWR_PHY0_SUSPEND | \
  22. EXYNOS_4x12_UPHYPWR_PHY0_PWR | \
  23. EXYNOS_4x12_UPHYPWR_PHY0_OTG_PWR | \
  24. EXYNOS_4x12_UPHYPWR_PHY0_SLEEP)
  25. #define EXYNOS_4x12_UPHYPWR_PHY1_SUSPEND BIT(6)
  26. #define EXYNOS_4x12_UPHYPWR_PHY1_PWR BIT(7)
  27. #define EXYNOS_4x12_UPHYPWR_PHY1_SLEEP BIT(8)
  28. #define EXYNOS_4x12_UPHYPWR_PHY1 ( \
  29. EXYNOS_4x12_UPHYPWR_PHY1_SUSPEND | \
  30. EXYNOS_4x12_UPHYPWR_PHY1_PWR | \
  31. EXYNOS_4x12_UPHYPWR_PHY1_SLEEP)
  32. #define EXYNOS_4x12_UPHYPWR_HSIC0_SUSPEND BIT(9)
  33. #define EXYNOS_4x12_UPHYPWR_HSIC0_PWR BIT(10)
  34. #define EXYNOS_4x12_UPHYPWR_HSIC0_SLEEP BIT(11)
  35. #define EXYNOS_4x12_UPHYPWR_HSIC0 ( \
  36. EXYNOS_4x12_UPHYPWR_HSIC0_SUSPEND | \
  37. EXYNOS_4x12_UPHYPWR_HSIC0_PWR | \
  38. EXYNOS_4x12_UPHYPWR_HSIC0_SLEEP)
  39. #define EXYNOS_4x12_UPHYPWR_HSIC1_SUSPEND BIT(12)
  40. #define EXYNOS_4x12_UPHYPWR_HSIC1_PWR BIT(13)
  41. #define EXYNOS_4x12_UPHYPWR_HSIC1_SLEEP BIT(14)
  42. #define EXYNOS_4x12_UPHYPWR_HSIC1 ( \
  43. EXYNOS_4x12_UPHYPWR_HSIC1_SUSPEND | \
  44. EXYNOS_4x12_UPHYPWR_HSIC1_PWR | \
  45. EXYNOS_4x12_UPHYPWR_HSIC1_SLEEP)
  46. /* PHY clock control */
  47. #define EXYNOS_4x12_UPHYCLK 0x4
  48. #define EXYNOS_4x12_UPHYCLK_PHYFSEL_MASK (0x7 << 0)
  49. #define EXYNOS_4x12_UPHYCLK_PHYFSEL_OFFSET 0
  50. #define EXYNOS_4x12_UPHYCLK_PHYFSEL_9MHZ6 (0x0 << 0)
  51. #define EXYNOS_4x12_UPHYCLK_PHYFSEL_10MHZ (0x1 << 0)
  52. #define EXYNOS_4x12_UPHYCLK_PHYFSEL_12MHZ (0x2 << 0)
  53. #define EXYNOS_4x12_UPHYCLK_PHYFSEL_19MHZ2 (0x3 << 0)
  54. #define EXYNOS_4x12_UPHYCLK_PHYFSEL_20MHZ (0x4 << 0)
  55. #define EXYNOS_4x12_UPHYCLK_PHYFSEL_24MHZ (0x5 << 0)
  56. #define EXYNOS_4x12_UPHYCLK_PHYFSEL_50MHZ (0x7 << 0)
  57. #define EXYNOS_3250_UPHYCLK_REFCLKSEL (0x2 << 8)
  58. #define EXYNOS_4x12_UPHYCLK_PHY0_ID_PULLUP BIT(3)
  59. #define EXYNOS_4x12_UPHYCLK_PHY0_COMMON_ON BIT(4)
  60. #define EXYNOS_4x12_UPHYCLK_PHY1_COMMON_ON BIT(7)
  61. #define EXYNOS_4x12_UPHYCLK_HSIC_REFCLK_MASK (0x7f << 10)
  62. #define EXYNOS_4x12_UPHYCLK_HSIC_REFCLK_OFFSET 10
  63. #define EXYNOS_4x12_UPHYCLK_HSIC_REFCLK_12MHZ (0x24 << 10)
  64. #define EXYNOS_4x12_UPHYCLK_HSIC_REFCLK_15MHZ (0x1c << 10)
  65. #define EXYNOS_4x12_UPHYCLK_HSIC_REFCLK_16MHZ (0x1a << 10)
  66. #define EXYNOS_4x12_UPHYCLK_HSIC_REFCLK_19MHZ2 (0x15 << 10)
  67. #define EXYNOS_4x12_UPHYCLK_HSIC_REFCLK_20MHZ (0x14 << 10)
  68. /* PHY reset control */
  69. #define EXYNOS_4x12_UPHYRST 0x8
  70. #define EXYNOS_4x12_URSTCON_PHY0 BIT(0)
  71. #define EXYNOS_4x12_URSTCON_OTG_HLINK BIT(1)
  72. #define EXYNOS_4x12_URSTCON_OTG_PHYLINK BIT(2)
  73. #define EXYNOS_4x12_URSTCON_HOST_PHY BIT(3)
  74. /* The following bit defines are presented in the
  75. * order taken from the Exynos4412 reference manual.
  76. *
  77. * During experiments with the hardware and debugging
  78. * it was determined that the hardware behaves contrary
  79. * to the manual.
  80. *
  81. * The following bit values were chaned accordingly to the
  82. * results of real hardware experiments.
  83. */
  84. #define EXYNOS_4x12_URSTCON_PHY1 BIT(4)
  85. #define EXYNOS_4x12_URSTCON_HSIC0 BIT(6)
  86. #define EXYNOS_4x12_URSTCON_HSIC1 BIT(5)
  87. #define EXYNOS_4x12_URSTCON_HOST_LINK_ALL BIT(7)
  88. #define EXYNOS_4x12_URSTCON_HOST_LINK_P0 BIT(10)
  89. #define EXYNOS_4x12_URSTCON_HOST_LINK_P1 BIT(9)
  90. #define EXYNOS_4x12_URSTCON_HOST_LINK_P2 BIT(8)
  91. /* Isolation, configured in the power management unit */
  92. #define EXYNOS_4x12_USB_ISOL_OFFSET 0x704
  93. #define EXYNOS_4x12_USB_ISOL_OTG BIT(0)
  94. #define EXYNOS_4x12_USB_ISOL_HSIC0_OFFSET 0x708
  95. #define EXYNOS_4x12_USB_ISOL_HSIC0 BIT(0)
  96. #define EXYNOS_4x12_USB_ISOL_HSIC1_OFFSET 0x70c
  97. #define EXYNOS_4x12_USB_ISOL_HSIC1 BIT(0)
  98. /* Mode switching SUB Device <-> Host */
  99. #define EXYNOS_4x12_MODE_SWITCH_OFFSET 0x21c
  100. #define EXYNOS_4x12_MODE_SWITCH_MASK 1
  101. #define EXYNOS_4x12_MODE_SWITCH_DEVICE 0
  102. #define EXYNOS_4x12_MODE_SWITCH_HOST 1
  103. enum exynos4x12_phy_id {
  104. EXYNOS4x12_DEVICE,
  105. EXYNOS4x12_HOST,
  106. EXYNOS4x12_HSIC0,
  107. EXYNOS4x12_HSIC1,
  108. EXYNOS4x12_NUM_PHYS,
  109. };
  110. /*
  111. * exynos4x12_rate_to_clk() converts the supplied clock rate to the value that
  112. * can be written to the phy register.
  113. */
  114. static int exynos4x12_rate_to_clk(unsigned long rate, u32 *reg)
  115. {
  116. /* EXYNOS_4x12_UPHYCLK_PHYFSEL_MASK */
  117. switch (rate) {
  118. case 9600 * KHZ:
  119. *reg = EXYNOS_4x12_UPHYCLK_PHYFSEL_9MHZ6;
  120. break;
  121. case 10 * MHZ:
  122. *reg = EXYNOS_4x12_UPHYCLK_PHYFSEL_10MHZ;
  123. break;
  124. case 12 * MHZ:
  125. *reg = EXYNOS_4x12_UPHYCLK_PHYFSEL_12MHZ;
  126. break;
  127. case 19200 * KHZ:
  128. *reg = EXYNOS_4x12_UPHYCLK_PHYFSEL_19MHZ2;
  129. break;
  130. case 20 * MHZ:
  131. *reg = EXYNOS_4x12_UPHYCLK_PHYFSEL_20MHZ;
  132. break;
  133. case 24 * MHZ:
  134. *reg = EXYNOS_4x12_UPHYCLK_PHYFSEL_24MHZ;
  135. break;
  136. case 50 * MHZ:
  137. *reg = EXYNOS_4x12_UPHYCLK_PHYFSEL_50MHZ;
  138. break;
  139. default:
  140. return -EINVAL;
  141. }
  142. return 0;
  143. }
  144. static void exynos4x12_isol(struct samsung_usb2_phy_instance *inst, bool on)
  145. {
  146. struct samsung_usb2_phy_driver *drv = inst->drv;
  147. u32 offset;
  148. u32 mask;
  149. switch (inst->cfg->id) {
  150. case EXYNOS4x12_DEVICE:
  151. case EXYNOS4x12_HOST:
  152. offset = EXYNOS_4x12_USB_ISOL_OFFSET;
  153. mask = EXYNOS_4x12_USB_ISOL_OTG;
  154. break;
  155. case EXYNOS4x12_HSIC0:
  156. offset = EXYNOS_4x12_USB_ISOL_HSIC0_OFFSET;
  157. mask = EXYNOS_4x12_USB_ISOL_HSIC0;
  158. break;
  159. case EXYNOS4x12_HSIC1:
  160. offset = EXYNOS_4x12_USB_ISOL_HSIC1_OFFSET;
  161. mask = EXYNOS_4x12_USB_ISOL_HSIC1;
  162. break;
  163. default:
  164. return;
  165. }
  166. regmap_update_bits(drv->reg_pmu, offset, mask, on ? 0 : mask);
  167. }
  168. static void exynos4x12_setup_clk(struct samsung_usb2_phy_instance *inst)
  169. {
  170. struct samsung_usb2_phy_driver *drv = inst->drv;
  171. u32 clk;
  172. clk = readl(drv->reg_phy + EXYNOS_4x12_UPHYCLK);
  173. clk &= ~EXYNOS_4x12_UPHYCLK_PHYFSEL_MASK;
  174. if (drv->cfg->has_refclk_sel)
  175. clk = EXYNOS_3250_UPHYCLK_REFCLKSEL;
  176. clk |= drv->ref_reg_val << EXYNOS_4x12_UPHYCLK_PHYFSEL_OFFSET;
  177. clk |= EXYNOS_4x12_UPHYCLK_PHY1_COMMON_ON;
  178. writel(clk, drv->reg_phy + EXYNOS_4x12_UPHYCLK);
  179. }
  180. static void exynos4x12_phy_pwr(struct samsung_usb2_phy_instance *inst, bool on)
  181. {
  182. struct samsung_usb2_phy_driver *drv = inst->drv;
  183. u32 rstbits = 0;
  184. u32 phypwr = 0;
  185. u32 rst;
  186. u32 pwr;
  187. switch (inst->cfg->id) {
  188. case EXYNOS4x12_DEVICE:
  189. phypwr = EXYNOS_4x12_UPHYPWR_PHY0;
  190. rstbits = EXYNOS_4x12_URSTCON_PHY0;
  191. break;
  192. case EXYNOS4x12_HOST:
  193. phypwr = EXYNOS_4x12_UPHYPWR_PHY1;
  194. rstbits = EXYNOS_4x12_URSTCON_HOST_PHY |
  195. EXYNOS_4x12_URSTCON_PHY1 |
  196. EXYNOS_4x12_URSTCON_HOST_LINK_P0;
  197. break;
  198. case EXYNOS4x12_HSIC0:
  199. phypwr = EXYNOS_4x12_UPHYPWR_HSIC0;
  200. rstbits = EXYNOS_4x12_URSTCON_HSIC0 |
  201. EXYNOS_4x12_URSTCON_HOST_LINK_P1;
  202. break;
  203. case EXYNOS4x12_HSIC1:
  204. phypwr = EXYNOS_4x12_UPHYPWR_HSIC1;
  205. rstbits = EXYNOS_4x12_URSTCON_HSIC1 |
  206. EXYNOS_4x12_URSTCON_HOST_LINK_P1;
  207. break;
  208. }
  209. if (on) {
  210. pwr = readl(drv->reg_phy + EXYNOS_4x12_UPHYPWR);
  211. pwr &= ~phypwr;
  212. writel(pwr, drv->reg_phy + EXYNOS_4x12_UPHYPWR);
  213. rst = readl(drv->reg_phy + EXYNOS_4x12_UPHYRST);
  214. rst |= rstbits;
  215. writel(rst, drv->reg_phy + EXYNOS_4x12_UPHYRST);
  216. udelay(10);
  217. rst &= ~rstbits;
  218. writel(rst, drv->reg_phy + EXYNOS_4x12_UPHYRST);
  219. /* The following delay is necessary for the reset sequence to be
  220. * completed */
  221. udelay(80);
  222. } else {
  223. pwr = readl(drv->reg_phy + EXYNOS_4x12_UPHYPWR);
  224. pwr |= phypwr;
  225. writel(pwr, drv->reg_phy + EXYNOS_4x12_UPHYPWR);
  226. }
  227. }
  228. static void exynos4x12_power_on_int(struct samsung_usb2_phy_instance *inst)
  229. {
  230. if (inst->int_cnt++ > 0)
  231. return;
  232. exynos4x12_setup_clk(inst);
  233. exynos4x12_isol(inst, 0);
  234. exynos4x12_phy_pwr(inst, 1);
  235. }
  236. static int exynos4x12_power_on(struct samsung_usb2_phy_instance *inst)
  237. {
  238. struct samsung_usb2_phy_driver *drv = inst->drv;
  239. if (inst->ext_cnt++ > 0)
  240. return 0;
  241. if (inst->cfg->id == EXYNOS4x12_HOST) {
  242. regmap_update_bits(drv->reg_sys, EXYNOS_4x12_MODE_SWITCH_OFFSET,
  243. EXYNOS_4x12_MODE_SWITCH_MASK,
  244. EXYNOS_4x12_MODE_SWITCH_HOST);
  245. exynos4x12_power_on_int(&drv->instances[EXYNOS4x12_DEVICE]);
  246. }
  247. if (inst->cfg->id == EXYNOS4x12_DEVICE && drv->cfg->has_mode_switch)
  248. regmap_update_bits(drv->reg_sys, EXYNOS_4x12_MODE_SWITCH_OFFSET,
  249. EXYNOS_4x12_MODE_SWITCH_MASK,
  250. EXYNOS_4x12_MODE_SWITCH_DEVICE);
  251. if (inst->cfg->id == EXYNOS4x12_HSIC0 ||
  252. inst->cfg->id == EXYNOS4x12_HSIC1) {
  253. exynos4x12_power_on_int(&drv->instances[EXYNOS4x12_DEVICE]);
  254. exynos4x12_power_on_int(&drv->instances[EXYNOS4x12_HOST]);
  255. }
  256. exynos4x12_power_on_int(inst);
  257. return 0;
  258. }
  259. static void exynos4x12_power_off_int(struct samsung_usb2_phy_instance *inst)
  260. {
  261. if (inst->int_cnt-- > 1)
  262. return;
  263. exynos4x12_isol(inst, 1);
  264. exynos4x12_phy_pwr(inst, 0);
  265. }
  266. static int exynos4x12_power_off(struct samsung_usb2_phy_instance *inst)
  267. {
  268. struct samsung_usb2_phy_driver *drv = inst->drv;
  269. if (inst->ext_cnt-- > 1)
  270. return 0;
  271. if (inst->cfg->id == EXYNOS4x12_DEVICE && drv->cfg->has_mode_switch)
  272. regmap_update_bits(drv->reg_sys, EXYNOS_4x12_MODE_SWITCH_OFFSET,
  273. EXYNOS_4x12_MODE_SWITCH_MASK,
  274. EXYNOS_4x12_MODE_SWITCH_HOST);
  275. if (inst->cfg->id == EXYNOS4x12_HOST)
  276. exynos4x12_power_off_int(&drv->instances[EXYNOS4x12_DEVICE]);
  277. if (inst->cfg->id == EXYNOS4x12_HSIC0 ||
  278. inst->cfg->id == EXYNOS4x12_HSIC1) {
  279. exynos4x12_power_off_int(&drv->instances[EXYNOS4x12_DEVICE]);
  280. exynos4x12_power_off_int(&drv->instances[EXYNOS4x12_HOST]);
  281. }
  282. exynos4x12_power_off_int(inst);
  283. return 0;
  284. }
  285. static const struct samsung_usb2_common_phy exynos4x12_phys[] = {
  286. {
  287. .label = "device",
  288. .id = EXYNOS4x12_DEVICE,
  289. .power_on = exynos4x12_power_on,
  290. .power_off = exynos4x12_power_off,
  291. },
  292. {
  293. .label = "host",
  294. .id = EXYNOS4x12_HOST,
  295. .power_on = exynos4x12_power_on,
  296. .power_off = exynos4x12_power_off,
  297. },
  298. {
  299. .label = "hsic0",
  300. .id = EXYNOS4x12_HSIC0,
  301. .power_on = exynos4x12_power_on,
  302. .power_off = exynos4x12_power_off,
  303. },
  304. {
  305. .label = "hsic1",
  306. .id = EXYNOS4x12_HSIC1,
  307. .power_on = exynos4x12_power_on,
  308. .power_off = exynos4x12_power_off,
  309. },
  310. };
  311. const struct samsung_usb2_phy_config exynos3250_usb2_phy_config = {
  312. .has_refclk_sel = 1,
  313. .num_phys = 1,
  314. .phys = exynos4x12_phys,
  315. .rate_to_clk = exynos4x12_rate_to_clk,
  316. };
  317. const struct samsung_usb2_phy_config exynos4x12_usb2_phy_config = {
  318. .has_mode_switch = 1,
  319. .num_phys = EXYNOS4x12_NUM_PHYS,
  320. .phys = exynos4x12_phys,
  321. .rate_to_clk = exynos4x12_rate_to_clk,
  322. };