phy-exynos4x12-usb2.c 9.0 KB

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