phy-rcar-gen2.c 11 KB

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