phy-rockchip-usb.c 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494
  1. /*
  2. * Rockchip usb PHY driver
  3. *
  4. * Copyright (C) 2014 Yunzhi Li <lyz@rock-chips.com>
  5. * Copyright (C) 2014 ROCKCHIP, Inc.
  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 as published by
  9. * the Free Software Foundation; either version 2 of the License.
  10. *
  11. * This program is distributed in the hope that it will be useful,
  12. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  13. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  14. * GNU General Public License for more details.
  15. */
  16. #include <linux/clk.h>
  17. #include <linux/clk-provider.h>
  18. #include <linux/io.h>
  19. #include <linux/kernel.h>
  20. #include <linux/module.h>
  21. #include <linux/mutex.h>
  22. #include <linux/of.h>
  23. #include <linux/of_address.h>
  24. #include <linux/of_platform.h>
  25. #include <linux/phy/phy.h>
  26. #include <linux/platform_device.h>
  27. #include <linux/regulator/consumer.h>
  28. #include <linux/reset.h>
  29. #include <linux/regmap.h>
  30. #include <linux/mfd/syscon.h>
  31. static int enable_usb_uart;
  32. #define HIWORD_UPDATE(val, mask) \
  33. ((val) | (mask) << 16)
  34. #define UOC_CON0_SIDDQ BIT(13)
  35. struct rockchip_usb_phys {
  36. int reg;
  37. const char *pll_name;
  38. };
  39. struct rockchip_usb_phy_base;
  40. struct rockchip_usb_phy_pdata {
  41. struct rockchip_usb_phys *phys;
  42. int (*init_usb_uart)(struct regmap *grf);
  43. int usb_uart_phy;
  44. };
  45. struct rockchip_usb_phy_base {
  46. struct device *dev;
  47. struct regmap *reg_base;
  48. const struct rockchip_usb_phy_pdata *pdata;
  49. };
  50. struct rockchip_usb_phy {
  51. struct rockchip_usb_phy_base *base;
  52. struct device_node *np;
  53. unsigned int reg_offset;
  54. struct clk *clk;
  55. struct clk *clk480m;
  56. struct clk_hw clk480m_hw;
  57. struct phy *phy;
  58. bool uart_enabled;
  59. };
  60. static int rockchip_usb_phy_power(struct rockchip_usb_phy *phy,
  61. bool siddq)
  62. {
  63. u32 val = HIWORD_UPDATE(siddq ? UOC_CON0_SIDDQ : 0, UOC_CON0_SIDDQ);
  64. return regmap_write(phy->base->reg_base, phy->reg_offset, val);
  65. }
  66. static unsigned long rockchip_usb_phy480m_recalc_rate(struct clk_hw *hw,
  67. unsigned long parent_rate)
  68. {
  69. return 480000000;
  70. }
  71. static void rockchip_usb_phy480m_disable(struct clk_hw *hw)
  72. {
  73. struct rockchip_usb_phy *phy = container_of(hw,
  74. struct rockchip_usb_phy,
  75. clk480m_hw);
  76. /* Power down usb phy analog blocks by set siddq 1 */
  77. rockchip_usb_phy_power(phy, 1);
  78. }
  79. static int rockchip_usb_phy480m_enable(struct clk_hw *hw)
  80. {
  81. struct rockchip_usb_phy *phy = container_of(hw,
  82. struct rockchip_usb_phy,
  83. clk480m_hw);
  84. /* Power up usb phy analog blocks by set siddq 0 */
  85. return rockchip_usb_phy_power(phy, 0);
  86. }
  87. static int rockchip_usb_phy480m_is_enabled(struct clk_hw *hw)
  88. {
  89. struct rockchip_usb_phy *phy = container_of(hw,
  90. struct rockchip_usb_phy,
  91. clk480m_hw);
  92. int ret;
  93. u32 val;
  94. ret = regmap_read(phy->base->reg_base, phy->reg_offset, &val);
  95. if (ret < 0)
  96. return ret;
  97. return (val & UOC_CON0_SIDDQ) ? 0 : 1;
  98. }
  99. static const struct clk_ops rockchip_usb_phy480m_ops = {
  100. .enable = rockchip_usb_phy480m_enable,
  101. .disable = rockchip_usb_phy480m_disable,
  102. .is_enabled = rockchip_usb_phy480m_is_enabled,
  103. .recalc_rate = rockchip_usb_phy480m_recalc_rate,
  104. };
  105. static int rockchip_usb_phy_power_off(struct phy *_phy)
  106. {
  107. struct rockchip_usb_phy *phy = phy_get_drvdata(_phy);
  108. if (phy->uart_enabled)
  109. return -EBUSY;
  110. clk_disable_unprepare(phy->clk480m);
  111. return 0;
  112. }
  113. static int rockchip_usb_phy_power_on(struct phy *_phy)
  114. {
  115. struct rockchip_usb_phy *phy = phy_get_drvdata(_phy);
  116. if (phy->uart_enabled)
  117. return -EBUSY;
  118. return clk_prepare_enable(phy->clk480m);
  119. }
  120. static const struct phy_ops ops = {
  121. .power_on = rockchip_usb_phy_power_on,
  122. .power_off = rockchip_usb_phy_power_off,
  123. .owner = THIS_MODULE,
  124. };
  125. static void rockchip_usb_phy_action(void *data)
  126. {
  127. struct rockchip_usb_phy *rk_phy = data;
  128. if (!rk_phy->uart_enabled) {
  129. of_clk_del_provider(rk_phy->np);
  130. clk_unregister(rk_phy->clk480m);
  131. }
  132. if (rk_phy->clk)
  133. clk_put(rk_phy->clk);
  134. }
  135. static int rockchip_usb_phy_init(struct rockchip_usb_phy_base *base,
  136. struct device_node *child)
  137. {
  138. struct rockchip_usb_phy *rk_phy;
  139. unsigned int reg_offset;
  140. const char *clk_name;
  141. struct clk_init_data init;
  142. int err, i;
  143. rk_phy = devm_kzalloc(base->dev, sizeof(*rk_phy), GFP_KERNEL);
  144. if (!rk_phy)
  145. return -ENOMEM;
  146. rk_phy->base = base;
  147. rk_phy->np = child;
  148. if (of_property_read_u32(child, "reg", &reg_offset)) {
  149. dev_err(base->dev, "missing reg property in node %s\n",
  150. child->name);
  151. return -EINVAL;
  152. }
  153. rk_phy->reg_offset = reg_offset;
  154. rk_phy->clk = of_clk_get_by_name(child, "phyclk");
  155. if (IS_ERR(rk_phy->clk))
  156. rk_phy->clk = NULL;
  157. i = 0;
  158. init.name = NULL;
  159. while (base->pdata->phys[i].reg) {
  160. if (base->pdata->phys[i].reg == reg_offset) {
  161. init.name = base->pdata->phys[i].pll_name;
  162. break;
  163. }
  164. i++;
  165. }
  166. if (!init.name) {
  167. dev_err(base->dev, "phy data not found\n");
  168. return -EINVAL;
  169. }
  170. if (enable_usb_uart && base->pdata->usb_uart_phy == i) {
  171. dev_dbg(base->dev, "phy%d used as uart output\n", i);
  172. rk_phy->uart_enabled = true;
  173. } else {
  174. if (rk_phy->clk) {
  175. clk_name = __clk_get_name(rk_phy->clk);
  176. init.flags = 0;
  177. init.parent_names = &clk_name;
  178. init.num_parents = 1;
  179. } else {
  180. init.flags = CLK_IS_ROOT;
  181. init.parent_names = NULL;
  182. init.num_parents = 0;
  183. }
  184. init.ops = &rockchip_usb_phy480m_ops;
  185. rk_phy->clk480m_hw.init = &init;
  186. rk_phy->clk480m = clk_register(base->dev, &rk_phy->clk480m_hw);
  187. if (IS_ERR(rk_phy->clk480m)) {
  188. err = PTR_ERR(rk_phy->clk480m);
  189. goto err_clk;
  190. }
  191. err = of_clk_add_provider(child, of_clk_src_simple_get,
  192. rk_phy->clk480m);
  193. if (err < 0)
  194. goto err_clk_prov;
  195. }
  196. err = devm_add_action(base->dev, rockchip_usb_phy_action, rk_phy);
  197. if (err)
  198. goto err_devm_action;
  199. rk_phy->phy = devm_phy_create(base->dev, child, &ops);
  200. if (IS_ERR(rk_phy->phy)) {
  201. dev_err(base->dev, "failed to create PHY\n");
  202. return PTR_ERR(rk_phy->phy);
  203. }
  204. phy_set_drvdata(rk_phy->phy, rk_phy);
  205. /*
  206. * When acting as uart-pipe, just keep clock on otherwise
  207. * only power up usb phy when it use, so disable it when init
  208. */
  209. if (rk_phy->uart_enabled)
  210. return clk_prepare_enable(rk_phy->clk);
  211. else
  212. return rockchip_usb_phy_power(rk_phy, 1);
  213. err_devm_action:
  214. if (!rk_phy->uart_enabled)
  215. of_clk_del_provider(child);
  216. err_clk_prov:
  217. if (!rk_phy->uart_enabled)
  218. clk_unregister(rk_phy->clk480m);
  219. err_clk:
  220. if (rk_phy->clk)
  221. clk_put(rk_phy->clk);
  222. return err;
  223. }
  224. static const struct rockchip_usb_phy_pdata rk3066a_pdata = {
  225. .phys = (struct rockchip_usb_phys[]){
  226. { .reg = 0x17c, .pll_name = "sclk_otgphy0_480m" },
  227. { .reg = 0x188, .pll_name = "sclk_otgphy1_480m" },
  228. { /* sentinel */ }
  229. },
  230. };
  231. static const struct rockchip_usb_phy_pdata rk3188_pdata = {
  232. .phys = (struct rockchip_usb_phys[]){
  233. { .reg = 0x10c, .pll_name = "sclk_otgphy0_480m" },
  234. { .reg = 0x11c, .pll_name = "sclk_otgphy1_480m" },
  235. { /* sentinel */ }
  236. },
  237. };
  238. #define RK3288_UOC0_CON0 0x320
  239. #define RK3288_UOC0_CON0_COMMON_ON_N BIT(0)
  240. #define RK3288_UOC0_CON0_DISABLE BIT(4)
  241. #define RK3288_UOC0_CON2 0x328
  242. #define RK3288_UOC0_CON2_SOFT_CON_SEL BIT(2)
  243. #define RK3288_UOC0_CON3 0x32c
  244. #define RK3288_UOC0_CON3_UTMI_SUSPENDN BIT(0)
  245. #define RK3288_UOC0_CON3_UTMI_OPMODE_NODRIVING (1 << 1)
  246. #define RK3288_UOC0_CON3_UTMI_OPMODE_MASK (3 << 1)
  247. #define RK3288_UOC0_CON3_UTMI_XCVRSEELCT_FSTRANSC (1 << 3)
  248. #define RK3288_UOC0_CON3_UTMI_XCVRSEELCT_MASK (3 << 3)
  249. #define RK3288_UOC0_CON3_UTMI_TERMSEL_FULLSPEED BIT(5)
  250. #define RK3288_UOC0_CON3_BYPASSDMEN BIT(6)
  251. #define RK3288_UOC0_CON3_BYPASSSEL BIT(7)
  252. /*
  253. * Enable the bypass of uart2 data through the otg usb phy.
  254. * Original description in the TRM.
  255. * 1. Disable the OTG block by setting OTGDISABLE0 to 1’b1.
  256. * 2. Disable the pull-up resistance on the D+ line by setting
  257. * OPMODE0[1:0] to 2’b01.
  258. * 3. To ensure that the XO, Bias, and PLL blocks are powered down in Suspend
  259. * mode, set COMMONONN to 1’b1.
  260. * 4. Place the USB PHY in Suspend mode by setting SUSPENDM0 to 1’b0.
  261. * 5. Set BYPASSSEL0 to 1’b1.
  262. * 6. To transmit data, controls BYPASSDMEN0, and BYPASSDMDATA0.
  263. * To receive data, monitor FSVPLUS0.
  264. *
  265. * The actual code in the vendor kernel does some things differently.
  266. */
  267. static int __init rk3288_init_usb_uart(struct regmap *grf)
  268. {
  269. u32 val;
  270. int ret;
  271. /*
  272. * COMMON_ON and DISABLE settings are described in the TRM,
  273. * but were not present in the original code.
  274. * Also disable the analog phy components to save power.
  275. */
  276. val = HIWORD_UPDATE(RK3288_UOC0_CON0_COMMON_ON_N
  277. | RK3288_UOC0_CON0_DISABLE
  278. | UOC_CON0_SIDDQ,
  279. RK3288_UOC0_CON0_COMMON_ON_N
  280. | RK3288_UOC0_CON0_DISABLE
  281. | UOC_CON0_SIDDQ);
  282. ret = regmap_write(grf, RK3288_UOC0_CON0, val);
  283. if (ret)
  284. return ret;
  285. val = HIWORD_UPDATE(RK3288_UOC0_CON2_SOFT_CON_SEL,
  286. RK3288_UOC0_CON2_SOFT_CON_SEL);
  287. ret = regmap_write(grf, RK3288_UOC0_CON2, val);
  288. if (ret)
  289. return ret;
  290. val = HIWORD_UPDATE(RK3288_UOC0_CON3_UTMI_OPMODE_NODRIVING
  291. | RK3288_UOC0_CON3_UTMI_XCVRSEELCT_FSTRANSC
  292. | RK3288_UOC0_CON3_UTMI_TERMSEL_FULLSPEED,
  293. RK3288_UOC0_CON3_UTMI_SUSPENDN
  294. | RK3288_UOC0_CON3_UTMI_OPMODE_MASK
  295. | RK3288_UOC0_CON3_UTMI_XCVRSEELCT_MASK
  296. | RK3288_UOC0_CON3_UTMI_TERMSEL_FULLSPEED);
  297. ret = regmap_write(grf, RK3288_UOC0_CON3, val);
  298. if (ret)
  299. return ret;
  300. val = HIWORD_UPDATE(RK3288_UOC0_CON3_BYPASSSEL
  301. | RK3288_UOC0_CON3_BYPASSDMEN,
  302. RK3288_UOC0_CON3_BYPASSSEL
  303. | RK3288_UOC0_CON3_BYPASSDMEN);
  304. ret = regmap_write(grf, RK3288_UOC0_CON3, val);
  305. if (ret)
  306. return ret;
  307. return 0;
  308. }
  309. static const struct rockchip_usb_phy_pdata rk3288_pdata = {
  310. .phys = (struct rockchip_usb_phys[]){
  311. { .reg = 0x320, .pll_name = "sclk_otgphy0_480m" },
  312. { .reg = 0x334, .pll_name = "sclk_otgphy1_480m" },
  313. { .reg = 0x348, .pll_name = "sclk_otgphy2_480m" },
  314. { /* sentinel */ }
  315. },
  316. .init_usb_uart = rk3288_init_usb_uart,
  317. .usb_uart_phy = 0,
  318. };
  319. static int rockchip_usb_phy_probe(struct platform_device *pdev)
  320. {
  321. struct device *dev = &pdev->dev;
  322. struct rockchip_usb_phy_base *phy_base;
  323. struct phy_provider *phy_provider;
  324. const struct of_device_id *match;
  325. struct device_node *child;
  326. int err;
  327. phy_base = devm_kzalloc(dev, sizeof(*phy_base), GFP_KERNEL);
  328. if (!phy_base)
  329. return -ENOMEM;
  330. match = of_match_device(dev->driver->of_match_table, dev);
  331. if (!match || !match->data) {
  332. dev_err(dev, "missing phy data\n");
  333. return -EINVAL;
  334. }
  335. phy_base->pdata = match->data;
  336. phy_base->dev = dev;
  337. phy_base->reg_base = syscon_regmap_lookup_by_phandle(dev->of_node,
  338. "rockchip,grf");
  339. if (IS_ERR(phy_base->reg_base)) {
  340. dev_err(&pdev->dev, "Missing rockchip,grf property\n");
  341. return PTR_ERR(phy_base->reg_base);
  342. }
  343. for_each_available_child_of_node(dev->of_node, child) {
  344. err = rockchip_usb_phy_init(phy_base, child);
  345. if (err) {
  346. of_node_put(child);
  347. return err;
  348. }
  349. }
  350. phy_provider = devm_of_phy_provider_register(dev, of_phy_simple_xlate);
  351. return PTR_ERR_OR_ZERO(phy_provider);
  352. }
  353. static const struct of_device_id rockchip_usb_phy_dt_ids[] = {
  354. { .compatible = "rockchip,rk3066a-usb-phy", .data = &rk3066a_pdata },
  355. { .compatible = "rockchip,rk3188-usb-phy", .data = &rk3188_pdata },
  356. { .compatible = "rockchip,rk3288-usb-phy", .data = &rk3288_pdata },
  357. {}
  358. };
  359. MODULE_DEVICE_TABLE(of, rockchip_usb_phy_dt_ids);
  360. static struct platform_driver rockchip_usb_driver = {
  361. .probe = rockchip_usb_phy_probe,
  362. .driver = {
  363. .name = "rockchip-usb-phy",
  364. .of_match_table = rockchip_usb_phy_dt_ids,
  365. },
  366. };
  367. module_platform_driver(rockchip_usb_driver);
  368. #ifndef MODULE
  369. static int __init rockchip_init_usb_uart(void)
  370. {
  371. const struct of_device_id *match;
  372. const struct rockchip_usb_phy_pdata *data;
  373. struct device_node *np;
  374. struct regmap *grf;
  375. int ret;
  376. if (!enable_usb_uart)
  377. return 0;
  378. np = of_find_matching_node_and_match(NULL, rockchip_usb_phy_dt_ids,
  379. &match);
  380. if (!np) {
  381. pr_err("%s: failed to find usbphy node\n", __func__);
  382. return -ENOTSUPP;
  383. }
  384. pr_debug("%s: using settings for %s\n", __func__, match->compatible);
  385. data = match->data;
  386. if (!data->init_usb_uart) {
  387. pr_err("%s: usb-uart not available on %s\n",
  388. __func__, match->compatible);
  389. return -ENOTSUPP;
  390. }
  391. grf = syscon_regmap_lookup_by_phandle(np, "rockchip,grf");
  392. if (IS_ERR(grf)) {
  393. pr_err("%s: Missing rockchip,grf property, %lu\n",
  394. __func__, PTR_ERR(grf));
  395. return PTR_ERR(grf);
  396. }
  397. ret = data->init_usb_uart(grf);
  398. if (ret) {
  399. pr_err("%s: could not init usb_uart, %d\n", __func__, ret);
  400. enable_usb_uart = 0;
  401. return ret;
  402. }
  403. return 0;
  404. }
  405. early_initcall(rockchip_init_usb_uart);
  406. static int __init rockchip_usb_uart(char *buf)
  407. {
  408. enable_usb_uart = true;
  409. return 0;
  410. }
  411. early_param("rockchip.usb_uart", rockchip_usb_uart);
  412. #endif
  413. MODULE_AUTHOR("Yunzhi Li <lyz@rock-chips.com>");
  414. MODULE_DESCRIPTION("Rockchip USB 2.0 PHY driver");
  415. MODULE_LICENSE("GPL v2");