phy-rcar-gen2.c 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442
  1. // SPDX-License-Identifier: GPL-2.0
  2. /*
  3. * Renesas R-Car Gen2 PHY driver
  4. *
  5. * Copyright (C) 2014 Renesas Solutions Corp.
  6. * Copyright (C) 2014 Cogent Embedded, Inc.
  7. * Copyright (C) 2019 Renesas Electronics Corp.
  8. */
  9. #include <linux/clk.h>
  10. #include <linux/delay.h>
  11. #include <linux/io.h>
  12. #include <linux/module.h>
  13. #include <linux/of.h>
  14. #include <linux/phy/phy.h>
  15. #include <linux/platform_device.h>
  16. #include <linux/spinlock.h>
  17. #include <linux/atomic.h>
  18. #define USBHS_LPSTS 0x02
  19. #define USBHS_UGCTRL 0x80
  20. #define USBHS_UGCTRL2 0x84
  21. #define USBHS_UGSTS 0x88 /* From technical update */
  22. /* Low Power Status register (LPSTS) */
  23. #define USBHS_LPSTS_SUSPM 0x4000
  24. /* USB General control register (UGCTRL) */
  25. #define USBHS_UGCTRL_CONNECT 0x00000004
  26. #define USBHS_UGCTRL_PLLRESET 0x00000001
  27. /* USB General control register 2 (UGCTRL2) */
  28. #define USBHS_UGCTRL2_USB2SEL 0x80000000
  29. #define USBHS_UGCTRL2_USB2SEL_PCI 0x00000000
  30. #define USBHS_UGCTRL2_USB2SEL_USB30 0x80000000
  31. #define USBHS_UGCTRL2_USB0SEL 0x00000030
  32. #define USBHS_UGCTRL2_USB0SEL_PCI 0x00000010
  33. #define USBHS_UGCTRL2_USB0SEL_HS_USB 0x00000030
  34. #define USBHS_UGCTRL2_USB0SEL_USB20 0x00000010
  35. #define USBHS_UGCTRL2_USB0SEL_HS_USB20 0x00000020
  36. /* USB General status register (UGSTS) */
  37. #define USBHS_UGSTS_LOCK 0x00000100 /* From technical update */
  38. #define PHYS_PER_CHANNEL 2
  39. struct rcar_gen2_phy {
  40. struct phy *phy;
  41. struct rcar_gen2_channel *channel;
  42. int number;
  43. u32 select_value;
  44. };
  45. struct rcar_gen2_channel {
  46. struct device_node *of_node;
  47. struct rcar_gen2_phy_driver *drv;
  48. struct rcar_gen2_phy phys[PHYS_PER_CHANNEL];
  49. int selected_phy;
  50. u32 select_mask;
  51. };
  52. struct rcar_gen2_phy_driver {
  53. void __iomem *base;
  54. struct clk *clk;
  55. spinlock_t lock;
  56. int num_channels;
  57. struct rcar_gen2_channel *channels;
  58. };
  59. struct rcar_gen2_phy_data {
  60. const struct phy_ops *gen2_phy_ops;
  61. const u32 (*select_value)[PHYS_PER_CHANNEL];
  62. const u32 num_channels;
  63. };
  64. static int rcar_gen2_phy_init(struct phy *p)
  65. {
  66. struct rcar_gen2_phy *phy = phy_get_drvdata(p);
  67. struct rcar_gen2_channel *channel = phy->channel;
  68. struct rcar_gen2_phy_driver *drv = channel->drv;
  69. unsigned long flags;
  70. u32 ugctrl2;
  71. /*
  72. * Try to acquire exclusive access to PHY. The first driver calling
  73. * phy_init() on a given channel wins, and all attempts to use another
  74. * PHY on this channel will fail until phy_exit() is called by the first
  75. * driver. Achieving this with cmpxcgh() should be SMP-safe.
  76. */
  77. if (cmpxchg(&channel->selected_phy, -1, phy->number) != -1)
  78. return -EBUSY;
  79. clk_prepare_enable(drv->clk);
  80. spin_lock_irqsave(&drv->lock, flags);
  81. ugctrl2 = readl(drv->base + USBHS_UGCTRL2);
  82. ugctrl2 &= ~channel->select_mask;
  83. ugctrl2 |= phy->select_value;
  84. writel(ugctrl2, drv->base + USBHS_UGCTRL2);
  85. spin_unlock_irqrestore(&drv->lock, flags);
  86. return 0;
  87. }
  88. static int rcar_gen2_phy_exit(struct phy *p)
  89. {
  90. struct rcar_gen2_phy *phy = phy_get_drvdata(p);
  91. struct rcar_gen2_channel *channel = phy->channel;
  92. clk_disable_unprepare(channel->drv->clk);
  93. channel->selected_phy = -1;
  94. return 0;
  95. }
  96. static int rcar_gen2_phy_power_on(struct phy *p)
  97. {
  98. struct rcar_gen2_phy *phy = phy_get_drvdata(p);
  99. struct rcar_gen2_phy_driver *drv = phy->channel->drv;
  100. void __iomem *base = drv->base;
  101. unsigned long flags;
  102. u32 value;
  103. int err = 0, i;
  104. /* Skip if it's not USBHS */
  105. if (phy->select_value != USBHS_UGCTRL2_USB0SEL_HS_USB)
  106. return 0;
  107. spin_lock_irqsave(&drv->lock, flags);
  108. /* Power on USBHS PHY */
  109. value = readl(base + USBHS_UGCTRL);
  110. value &= ~USBHS_UGCTRL_PLLRESET;
  111. writel(value, base + USBHS_UGCTRL);
  112. value = readw(base + USBHS_LPSTS);
  113. value |= USBHS_LPSTS_SUSPM;
  114. writew(value, base + USBHS_LPSTS);
  115. for (i = 0; i < 20; i++) {
  116. value = readl(base + USBHS_UGSTS);
  117. if ((value & USBHS_UGSTS_LOCK) == USBHS_UGSTS_LOCK) {
  118. value = readl(base + USBHS_UGCTRL);
  119. value |= USBHS_UGCTRL_CONNECT;
  120. writel(value, base + USBHS_UGCTRL);
  121. goto out;
  122. }
  123. udelay(1);
  124. }
  125. /* Timed out waiting for the PLL lock */
  126. err = -ETIMEDOUT;
  127. out:
  128. spin_unlock_irqrestore(&drv->lock, flags);
  129. return err;
  130. }
  131. static int rcar_gen2_phy_power_off(struct phy *p)
  132. {
  133. struct rcar_gen2_phy *phy = phy_get_drvdata(p);
  134. struct rcar_gen2_phy_driver *drv = phy->channel->drv;
  135. void __iomem *base = drv->base;
  136. unsigned long flags;
  137. u32 value;
  138. /* Skip if it's not USBHS */
  139. if (phy->select_value != USBHS_UGCTRL2_USB0SEL_HS_USB)
  140. return 0;
  141. spin_lock_irqsave(&drv->lock, flags);
  142. /* Power off USBHS PHY */
  143. value = readl(base + USBHS_UGCTRL);
  144. value &= ~USBHS_UGCTRL_CONNECT;
  145. writel(value, base + USBHS_UGCTRL);
  146. value = readw(base + USBHS_LPSTS);
  147. value &= ~USBHS_LPSTS_SUSPM;
  148. writew(value, base + USBHS_LPSTS);
  149. value = readl(base + USBHS_UGCTRL);
  150. value |= USBHS_UGCTRL_PLLRESET;
  151. writel(value, base + USBHS_UGCTRL);
  152. spin_unlock_irqrestore(&drv->lock, flags);
  153. return 0;
  154. }
  155. static int rz_g1c_phy_power_on(struct phy *p)
  156. {
  157. struct rcar_gen2_phy *phy = phy_get_drvdata(p);
  158. struct rcar_gen2_phy_driver *drv = phy->channel->drv;
  159. void __iomem *base = drv->base;
  160. unsigned long flags;
  161. u32 value;
  162. spin_lock_irqsave(&drv->lock, flags);
  163. /* Power on USBHS PHY */
  164. value = readl(base + USBHS_UGCTRL);
  165. value &= ~USBHS_UGCTRL_PLLRESET;
  166. writel(value, base + USBHS_UGCTRL);
  167. /* As per the data sheet wait 340 micro sec for power stable */
  168. udelay(340);
  169. if (phy->select_value == USBHS_UGCTRL2_USB0SEL_HS_USB20) {
  170. value = readw(base + USBHS_LPSTS);
  171. value |= USBHS_LPSTS_SUSPM;
  172. writew(value, base + USBHS_LPSTS);
  173. }
  174. spin_unlock_irqrestore(&drv->lock, flags);
  175. return 0;
  176. }
  177. static int rz_g1c_phy_power_off(struct phy *p)
  178. {
  179. struct rcar_gen2_phy *phy = phy_get_drvdata(p);
  180. struct rcar_gen2_phy_driver *drv = phy->channel->drv;
  181. void __iomem *base = drv->base;
  182. unsigned long flags;
  183. u32 value;
  184. spin_lock_irqsave(&drv->lock, flags);
  185. /* Power off USBHS PHY */
  186. if (phy->select_value == USBHS_UGCTRL2_USB0SEL_HS_USB20) {
  187. value = readw(base + USBHS_LPSTS);
  188. value &= ~USBHS_LPSTS_SUSPM;
  189. writew(value, base + USBHS_LPSTS);
  190. }
  191. value = readl(base + USBHS_UGCTRL);
  192. value |= USBHS_UGCTRL_PLLRESET;
  193. writel(value, base + USBHS_UGCTRL);
  194. spin_unlock_irqrestore(&drv->lock, flags);
  195. return 0;
  196. }
  197. static const struct phy_ops rcar_gen2_phy_ops = {
  198. .init = rcar_gen2_phy_init,
  199. .exit = rcar_gen2_phy_exit,
  200. .power_on = rcar_gen2_phy_power_on,
  201. .power_off = rcar_gen2_phy_power_off,
  202. .owner = THIS_MODULE,
  203. };
  204. static const struct phy_ops rz_g1c_phy_ops = {
  205. .init = rcar_gen2_phy_init,
  206. .exit = rcar_gen2_phy_exit,
  207. .power_on = rz_g1c_phy_power_on,
  208. .power_off = rz_g1c_phy_power_off,
  209. .owner = THIS_MODULE,
  210. };
  211. static const u32 pci_select_value[][PHYS_PER_CHANNEL] = {
  212. [0] = { USBHS_UGCTRL2_USB0SEL_PCI, USBHS_UGCTRL2_USB0SEL_HS_USB },
  213. [2] = { USBHS_UGCTRL2_USB2SEL_PCI, USBHS_UGCTRL2_USB2SEL_USB30 },
  214. };
  215. static const u32 usb20_select_value[][PHYS_PER_CHANNEL] = {
  216. { USBHS_UGCTRL2_USB0SEL_USB20, USBHS_UGCTRL2_USB0SEL_HS_USB20 },
  217. };
  218. static const struct rcar_gen2_phy_data rcar_gen2_usb_phy_data = {
  219. .gen2_phy_ops = &rcar_gen2_phy_ops,
  220. .select_value = pci_select_value,
  221. .num_channels = ARRAY_SIZE(pci_select_value),
  222. };
  223. static const struct rcar_gen2_phy_data rz_g1c_usb_phy_data = {
  224. .gen2_phy_ops = &rz_g1c_phy_ops,
  225. .select_value = usb20_select_value,
  226. .num_channels = ARRAY_SIZE(usb20_select_value),
  227. };
  228. static const struct of_device_id rcar_gen2_phy_match_table[] = {
  229. {
  230. .compatible = "renesas,usb-phy-r8a77470",
  231. .data = &rz_g1c_usb_phy_data,
  232. },
  233. {
  234. .compatible = "renesas,usb-phy-r8a7790",
  235. .data = &rcar_gen2_usb_phy_data,
  236. },
  237. {
  238. .compatible = "renesas,usb-phy-r8a7791",
  239. .data = &rcar_gen2_usb_phy_data,
  240. },
  241. {
  242. .compatible = "renesas,usb-phy-r8a7794",
  243. .data = &rcar_gen2_usb_phy_data,
  244. },
  245. {
  246. .compatible = "renesas,rcar-gen2-usb-phy",
  247. .data = &rcar_gen2_usb_phy_data,
  248. },
  249. { /* sentinel */ },
  250. };
  251. MODULE_DEVICE_TABLE(of, rcar_gen2_phy_match_table);
  252. static struct phy *rcar_gen2_phy_xlate(struct device *dev,
  253. const struct of_phandle_args *args)
  254. {
  255. struct rcar_gen2_phy_driver *drv;
  256. struct device_node *np = args->np;
  257. int i;
  258. drv = dev_get_drvdata(dev);
  259. if (!drv)
  260. return ERR_PTR(-EINVAL);
  261. for (i = 0; i < drv->num_channels; i++) {
  262. if (np == drv->channels[i].of_node)
  263. break;
  264. }
  265. if (i >= drv->num_channels || args->args[0] >= 2)
  266. return ERR_PTR(-ENODEV);
  267. return drv->channels[i].phys[args->args[0]].phy;
  268. }
  269. static const u32 select_mask[] = {
  270. [0] = USBHS_UGCTRL2_USB0SEL,
  271. [2] = USBHS_UGCTRL2_USB2SEL,
  272. };
  273. static int rcar_gen2_phy_probe(struct platform_device *pdev)
  274. {
  275. struct device *dev = &pdev->dev;
  276. struct rcar_gen2_phy_driver *drv;
  277. struct phy_provider *provider;
  278. struct device_node *np;
  279. void __iomem *base;
  280. struct clk *clk;
  281. const struct rcar_gen2_phy_data *data;
  282. int i = 0;
  283. if (!dev->of_node) {
  284. dev_err(dev,
  285. "This driver is required to be instantiated from device tree\n");
  286. return -EINVAL;
  287. }
  288. clk = devm_clk_get(dev, "usbhs");
  289. if (IS_ERR(clk)) {
  290. dev_err(dev, "Can't get USBHS clock\n");
  291. return PTR_ERR(clk);
  292. }
  293. base = devm_platform_ioremap_resource(pdev, 0);
  294. if (IS_ERR(base))
  295. return PTR_ERR(base);
  296. drv = devm_kzalloc(dev, sizeof(*drv), GFP_KERNEL);
  297. if (!drv)
  298. return -ENOMEM;
  299. spin_lock_init(&drv->lock);
  300. drv->clk = clk;
  301. drv->base = base;
  302. data = of_device_get_match_data(dev);
  303. if (!data)
  304. return -EINVAL;
  305. drv->num_channels = of_get_child_count(dev->of_node);
  306. drv->channels = devm_kcalloc(dev, drv->num_channels,
  307. sizeof(struct rcar_gen2_channel),
  308. GFP_KERNEL);
  309. if (!drv->channels)
  310. return -ENOMEM;
  311. for_each_child_of_node(dev->of_node, np) {
  312. struct rcar_gen2_channel *channel = drv->channels + i;
  313. u32 channel_num;
  314. int error, n;
  315. channel->of_node = np;
  316. channel->drv = drv;
  317. channel->selected_phy = -1;
  318. error = of_property_read_u32(np, "reg", &channel_num);
  319. if (error || channel_num >= data->num_channels) {
  320. dev_err(dev, "Invalid \"reg\" property\n");
  321. of_node_put(np);
  322. return error;
  323. }
  324. channel->select_mask = select_mask[channel_num];
  325. for (n = 0; n < PHYS_PER_CHANNEL; n++) {
  326. struct rcar_gen2_phy *phy = &channel->phys[n];
  327. phy->channel = channel;
  328. phy->number = n;
  329. phy->select_value = data->select_value[channel_num][n];
  330. phy->phy = devm_phy_create(dev, NULL,
  331. data->gen2_phy_ops);
  332. if (IS_ERR(phy->phy)) {
  333. dev_err(dev, "Failed to create PHY\n");
  334. of_node_put(np);
  335. return PTR_ERR(phy->phy);
  336. }
  337. phy_set_drvdata(phy->phy, phy);
  338. }
  339. i++;
  340. }
  341. provider = devm_of_phy_provider_register(dev, rcar_gen2_phy_xlate);
  342. if (IS_ERR(provider)) {
  343. dev_err(dev, "Failed to register PHY provider\n");
  344. return PTR_ERR(provider);
  345. }
  346. dev_set_drvdata(dev, drv);
  347. return 0;
  348. }
  349. static struct platform_driver rcar_gen2_phy_driver = {
  350. .driver = {
  351. .name = "phy_rcar_gen2",
  352. .of_match_table = rcar_gen2_phy_match_table,
  353. },
  354. .probe = rcar_gen2_phy_probe,
  355. };
  356. module_platform_driver(rcar_gen2_phy_driver);
  357. MODULE_LICENSE("GPL v2");
  358. MODULE_DESCRIPTION("Renesas R-Car Gen2 PHY");
  359. MODULE_AUTHOR("Sergei Shtylyov <sergei.shtylyov@cogentembedded.com>");