phy-rockchip-usb.c 15 KB

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