phy-rockchip-usb.c 14 KB

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