phy-rockchip-usb.c 14 KB

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