phy-rockchip-usb.c 13 KB

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