phy-rockchip-usb.c 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501
  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 = 0;
  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_or_reset(base->dev, rockchip_usb_phy_action,
  197. rk_phy);
  198. if (err)
  199. return err;
  200. rk_phy->phy = devm_phy_create(base->dev, child, &ops);
  201. if (IS_ERR(rk_phy->phy)) {
  202. dev_err(base->dev, "failed to create PHY\n");
  203. return PTR_ERR(rk_phy->phy);
  204. }
  205. phy_set_drvdata(rk_phy->phy, rk_phy);
  206. /*
  207. * When acting as uart-pipe, just keep clock on otherwise
  208. * only power up usb phy when it use, so disable it when init
  209. */
  210. if (rk_phy->uart_enabled)
  211. return clk_prepare_enable(rk_phy->clk);
  212. else
  213. return rockchip_usb_phy_power(rk_phy, 1);
  214. err_clk_prov:
  215. if (!rk_phy->uart_enabled)
  216. clk_unregister(rk_phy->clk480m);
  217. err_clk:
  218. if (rk_phy->clk)
  219. clk_put(rk_phy->clk);
  220. return err;
  221. }
  222. static const struct rockchip_usb_phy_pdata rk3066a_pdata = {
  223. .phys = (struct rockchip_usb_phys[]){
  224. { .reg = 0x17c, .pll_name = "sclk_otgphy0_480m" },
  225. { .reg = 0x188, .pll_name = "sclk_otgphy1_480m" },
  226. { /* sentinel */ }
  227. },
  228. };
  229. static const struct rockchip_usb_phy_pdata rk3188_pdata = {
  230. .phys = (struct rockchip_usb_phys[]){
  231. { .reg = 0x10c, .pll_name = "sclk_otgphy0_480m" },
  232. { .reg = 0x11c, .pll_name = "sclk_otgphy1_480m" },
  233. { /* sentinel */ }
  234. },
  235. };
  236. #define RK3288_UOC0_CON0 0x320
  237. #define RK3288_UOC0_CON0_COMMON_ON_N BIT(0)
  238. #define RK3288_UOC0_CON0_DISABLE BIT(4)
  239. #define RK3288_UOC0_CON2 0x328
  240. #define RK3288_UOC0_CON2_SOFT_CON_SEL BIT(2)
  241. #define RK3288_UOC0_CON3 0x32c
  242. #define RK3288_UOC0_CON3_UTMI_SUSPENDN BIT(0)
  243. #define RK3288_UOC0_CON3_UTMI_OPMODE_NODRIVING (1 << 1)
  244. #define RK3288_UOC0_CON3_UTMI_OPMODE_MASK (3 << 1)
  245. #define RK3288_UOC0_CON3_UTMI_XCVRSEELCT_FSTRANSC (1 << 3)
  246. #define RK3288_UOC0_CON3_UTMI_XCVRSEELCT_MASK (3 << 3)
  247. #define RK3288_UOC0_CON3_UTMI_TERMSEL_FULLSPEED BIT(5)
  248. #define RK3288_UOC0_CON3_BYPASSDMEN BIT(6)
  249. #define RK3288_UOC0_CON3_BYPASSSEL BIT(7)
  250. /*
  251. * Enable the bypass of uart2 data through the otg usb phy.
  252. * Original description in the TRM.
  253. * 1. Disable the OTG block by setting OTGDISABLE0 to 1’b1.
  254. * 2. Disable the pull-up resistance on the D+ line by setting
  255. * OPMODE0[1:0] to 2’b01.
  256. * 3. To ensure that the XO, Bias, and PLL blocks are powered down in Suspend
  257. * mode, set COMMONONN to 1’b1.
  258. * 4. Place the USB PHY in Suspend mode by setting SUSPENDM0 to 1’b0.
  259. * 5. Set BYPASSSEL0 to 1’b1.
  260. * 6. To transmit data, controls BYPASSDMEN0, and BYPASSDMDATA0.
  261. * To receive data, monitor FSVPLUS0.
  262. *
  263. * The actual code in the vendor kernel does some things differently.
  264. */
  265. static int __init rk3288_init_usb_uart(struct regmap *grf)
  266. {
  267. u32 val;
  268. int ret;
  269. /*
  270. * COMMON_ON and DISABLE settings are described in the TRM,
  271. * but were not present in the original code.
  272. * Also disable the analog phy components to save power.
  273. */
  274. val = HIWORD_UPDATE(RK3288_UOC0_CON0_COMMON_ON_N
  275. | RK3288_UOC0_CON0_DISABLE
  276. | UOC_CON0_SIDDQ,
  277. RK3288_UOC0_CON0_COMMON_ON_N
  278. | RK3288_UOC0_CON0_DISABLE
  279. | UOC_CON0_SIDDQ);
  280. ret = regmap_write(grf, RK3288_UOC0_CON0, val);
  281. if (ret)
  282. return ret;
  283. val = HIWORD_UPDATE(RK3288_UOC0_CON2_SOFT_CON_SEL,
  284. RK3288_UOC0_CON2_SOFT_CON_SEL);
  285. ret = regmap_write(grf, RK3288_UOC0_CON2, val);
  286. if (ret)
  287. return ret;
  288. val = HIWORD_UPDATE(RK3288_UOC0_CON3_UTMI_OPMODE_NODRIVING
  289. | RK3288_UOC0_CON3_UTMI_XCVRSEELCT_FSTRANSC
  290. | RK3288_UOC0_CON3_UTMI_TERMSEL_FULLSPEED,
  291. RK3288_UOC0_CON3_UTMI_SUSPENDN
  292. | RK3288_UOC0_CON3_UTMI_OPMODE_MASK
  293. | RK3288_UOC0_CON3_UTMI_XCVRSEELCT_MASK
  294. | RK3288_UOC0_CON3_UTMI_TERMSEL_FULLSPEED);
  295. ret = regmap_write(grf, RK3288_UOC0_CON3, val);
  296. if (ret)
  297. return ret;
  298. val = HIWORD_UPDATE(RK3288_UOC0_CON3_BYPASSSEL
  299. | RK3288_UOC0_CON3_BYPASSDMEN,
  300. RK3288_UOC0_CON3_BYPASSSEL
  301. | RK3288_UOC0_CON3_BYPASSDMEN);
  302. ret = regmap_write(grf, RK3288_UOC0_CON3, val);
  303. if (ret)
  304. return ret;
  305. return 0;
  306. }
  307. static const struct rockchip_usb_phy_pdata rk3288_pdata = {
  308. .phys = (struct rockchip_usb_phys[]){
  309. { .reg = 0x320, .pll_name = "sclk_otgphy0_480m" },
  310. { .reg = 0x334, .pll_name = "sclk_otgphy1_480m" },
  311. { .reg = 0x348, .pll_name = "sclk_otgphy2_480m" },
  312. { /* sentinel */ }
  313. },
  314. .init_usb_uart = rk3288_init_usb_uart,
  315. .usb_uart_phy = 0,
  316. };
  317. static int rockchip_usb_phy_probe(struct platform_device *pdev)
  318. {
  319. struct device *dev = &pdev->dev;
  320. struct rockchip_usb_phy_base *phy_base;
  321. struct phy_provider *phy_provider;
  322. const struct of_device_id *match;
  323. struct device_node *child;
  324. int err;
  325. phy_base = devm_kzalloc(dev, sizeof(*phy_base), GFP_KERNEL);
  326. if (!phy_base)
  327. return -ENOMEM;
  328. match = of_match_device(dev->driver->of_match_table, dev);
  329. if (!match || !match->data) {
  330. dev_err(dev, "missing phy data\n");
  331. return -EINVAL;
  332. }
  333. phy_base->pdata = match->data;
  334. phy_base->dev = dev;
  335. phy_base->reg_base = ERR_PTR(-ENODEV);
  336. if (dev->parent && dev->parent->of_node)
  337. phy_base->reg_base = syscon_node_to_regmap(
  338. dev->parent->of_node);
  339. if (IS_ERR(phy_base->reg_base))
  340. phy_base->reg_base = syscon_regmap_lookup_by_phandle(
  341. dev->of_node, "rockchip,grf");
  342. if (IS_ERR(phy_base->reg_base)) {
  343. dev_err(&pdev->dev, "Missing rockchip,grf property\n");
  344. return PTR_ERR(phy_base->reg_base);
  345. }
  346. for_each_available_child_of_node(dev->of_node, child) {
  347. err = rockchip_usb_phy_init(phy_base, child);
  348. if (err) {
  349. of_node_put(child);
  350. return err;
  351. }
  352. }
  353. phy_provider = devm_of_phy_provider_register(dev, of_phy_simple_xlate);
  354. return PTR_ERR_OR_ZERO(phy_provider);
  355. }
  356. static const struct of_device_id rockchip_usb_phy_dt_ids[] = {
  357. { .compatible = "rockchip,rk3066a-usb-phy", .data = &rk3066a_pdata },
  358. { .compatible = "rockchip,rk3188-usb-phy", .data = &rk3188_pdata },
  359. { .compatible = "rockchip,rk3288-usb-phy", .data = &rk3288_pdata },
  360. {}
  361. };
  362. MODULE_DEVICE_TABLE(of, rockchip_usb_phy_dt_ids);
  363. static struct platform_driver rockchip_usb_driver = {
  364. .probe = rockchip_usb_phy_probe,
  365. .driver = {
  366. .name = "rockchip-usb-phy",
  367. .of_match_table = rockchip_usb_phy_dt_ids,
  368. },
  369. };
  370. module_platform_driver(rockchip_usb_driver);
  371. #ifndef MODULE
  372. static int __init rockchip_init_usb_uart(void)
  373. {
  374. const struct of_device_id *match;
  375. const struct rockchip_usb_phy_pdata *data;
  376. struct device_node *np;
  377. struct regmap *grf;
  378. int ret;
  379. if (!enable_usb_uart)
  380. return 0;
  381. np = of_find_matching_node_and_match(NULL, rockchip_usb_phy_dt_ids,
  382. &match);
  383. if (!np) {
  384. pr_err("%s: failed to find usbphy node\n", __func__);
  385. return -ENOTSUPP;
  386. }
  387. pr_debug("%s: using settings for %s\n", __func__, match->compatible);
  388. data = match->data;
  389. if (!data->init_usb_uart) {
  390. pr_err("%s: usb-uart not available on %s\n",
  391. __func__, match->compatible);
  392. return -ENOTSUPP;
  393. }
  394. grf = ERR_PTR(-ENODEV);
  395. if (np->parent)
  396. grf = syscon_node_to_regmap(np->parent);
  397. if (IS_ERR(grf))
  398. grf = syscon_regmap_lookup_by_phandle(np, "rockchip,grf");
  399. if (IS_ERR(grf)) {
  400. pr_err("%s: Missing rockchip,grf property, %lu\n",
  401. __func__, PTR_ERR(grf));
  402. return PTR_ERR(grf);
  403. }
  404. ret = data->init_usb_uart(grf);
  405. if (ret) {
  406. pr_err("%s: could not init usb_uart, %d\n", __func__, ret);
  407. enable_usb_uart = 0;
  408. return ret;
  409. }
  410. return 0;
  411. }
  412. early_initcall(rockchip_init_usb_uart);
  413. static int __init rockchip_usb_uart(char *buf)
  414. {
  415. enable_usb_uart = true;
  416. return 0;
  417. }
  418. early_param("rockchip.usb_uart", rockchip_usb_uart);
  419. #endif
  420. MODULE_AUTHOR("Yunzhi Li <lyz@rock-chips.com>");
  421. MODULE_DESCRIPTION("Rockchip USB 2.0 PHY driver");
  422. MODULE_LICENSE("GPL v2");