phy-exynos4x12-usb2.c 10 KB

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