phy-rockchip-usb.c 14 KB

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