phy-rcar-gen2.c 8.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336
  1. /*
  2. * Renesas R-Car Gen2 PHY driver
  3. *
  4. * Copyright (C) 2014 Renesas Solutions Corp.
  5. * Copyright (C) 2014 Cogent Embedded, 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 version 2 as
  9. * published by the Free Software Foundation.
  10. */
  11. #include <linux/clk.h>
  12. #include <linux/delay.h>
  13. #include <linux/io.h>
  14. #include <linux/module.h>
  15. #include <linux/of.h>
  16. #include <linux/phy/phy.h>
  17. #include <linux/platform_device.h>
  18. #include <linux/spinlock.h>
  19. #include <linux/atomic.h>
  20. #define USBHS_LPSTS 0x02
  21. #define USBHS_UGCTRL 0x80
  22. #define USBHS_UGCTRL2 0x84
  23. #define USBHS_UGSTS 0x88 /* From technical update */
  24. /* Low Power Status register (LPSTS) */
  25. #define USBHS_LPSTS_SUSPM 0x4000
  26. /* USB General control register (UGCTRL) */
  27. #define USBHS_UGCTRL_CONNECT 0x00000004
  28. #define USBHS_UGCTRL_PLLRESET 0x00000001
  29. /* USB General control register 2 (UGCTRL2) */
  30. #define USBHS_UGCTRL2_USB2SEL 0x80000000
  31. #define USBHS_UGCTRL2_USB2SEL_PCI 0x00000000
  32. #define USBHS_UGCTRL2_USB2SEL_USB30 0x80000000
  33. #define USBHS_UGCTRL2_USB0SEL 0x00000030
  34. #define USBHS_UGCTRL2_USB0SEL_PCI 0x00000010
  35. #define USBHS_UGCTRL2_USB0SEL_HS_USB 0x00000030
  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. static int rcar_gen2_phy_init(struct phy *p)
  60. {
  61. struct rcar_gen2_phy *phy = phy_get_drvdata(p);
  62. struct rcar_gen2_channel *channel = phy->channel;
  63. struct rcar_gen2_phy_driver *drv = channel->drv;
  64. unsigned long flags;
  65. u32 ugctrl2;
  66. /*
  67. * Try to acquire exclusive access to PHY. The first driver calling
  68. * phy_init() on a given channel wins, and all attempts to use another
  69. * PHY on this channel will fail until phy_exit() is called by the first
  70. * driver. Achieving this with cmpxcgh() should be SMP-safe.
  71. */
  72. if (cmpxchg(&channel->selected_phy, -1, phy->number) != -1)
  73. return -EBUSY;
  74. clk_prepare_enable(drv->clk);
  75. spin_lock_irqsave(&drv->lock, flags);
  76. ugctrl2 = readl(drv->base + USBHS_UGCTRL2);
  77. ugctrl2 &= ~channel->select_mask;
  78. ugctrl2 |= phy->select_value;
  79. writel(ugctrl2, drv->base + USBHS_UGCTRL2);
  80. spin_unlock_irqrestore(&drv->lock, flags);
  81. return 0;
  82. }
  83. static int rcar_gen2_phy_exit(struct phy *p)
  84. {
  85. struct rcar_gen2_phy *phy = phy_get_drvdata(p);
  86. struct rcar_gen2_channel *channel = phy->channel;
  87. clk_disable_unprepare(channel->drv->clk);
  88. channel->selected_phy = -1;
  89. return 0;
  90. }
  91. static int rcar_gen2_phy_power_on(struct phy *p)
  92. {
  93. struct rcar_gen2_phy *phy = phy_get_drvdata(p);
  94. struct rcar_gen2_phy_driver *drv = phy->channel->drv;
  95. void __iomem *base = drv->base;
  96. unsigned long flags;
  97. u32 value;
  98. int err = 0, i;
  99. /* Skip if it's not USBHS */
  100. if (phy->select_value != USBHS_UGCTRL2_USB0SEL_HS_USB)
  101. return 0;
  102. spin_lock_irqsave(&drv->lock, flags);
  103. /* Power on USBHS PHY */
  104. value = readl(base + USBHS_UGCTRL);
  105. value &= ~USBHS_UGCTRL_PLLRESET;
  106. writel(value, base + USBHS_UGCTRL);
  107. value = readw(base + USBHS_LPSTS);
  108. value |= USBHS_LPSTS_SUSPM;
  109. writew(value, base + USBHS_LPSTS);
  110. for (i = 0; i < 20; i++) {
  111. value = readl(base + USBHS_UGSTS);
  112. if ((value & USBHS_UGSTS_LOCK) == USBHS_UGSTS_LOCK) {
  113. value = readl(base + USBHS_UGCTRL);
  114. value |= USBHS_UGCTRL_CONNECT;
  115. writel(value, base + USBHS_UGCTRL);
  116. goto out;
  117. }
  118. udelay(1);
  119. }
  120. /* Timed out waiting for the PLL lock */
  121. err = -ETIMEDOUT;
  122. out:
  123. spin_unlock_irqrestore(&drv->lock, flags);
  124. return err;
  125. }
  126. static int rcar_gen2_phy_power_off(struct phy *p)
  127. {
  128. struct rcar_gen2_phy *phy = phy_get_drvdata(p);
  129. struct rcar_gen2_phy_driver *drv = phy->channel->drv;
  130. void __iomem *base = drv->base;
  131. unsigned long flags;
  132. u32 value;
  133. /* Skip if it's not USBHS */
  134. if (phy->select_value != USBHS_UGCTRL2_USB0SEL_HS_USB)
  135. return 0;
  136. spin_lock_irqsave(&drv->lock, flags);
  137. /* Power off USBHS PHY */
  138. value = readl(base + USBHS_UGCTRL);
  139. value &= ~USBHS_UGCTRL_CONNECT;
  140. writel(value, base + USBHS_UGCTRL);
  141. value = readw(base + USBHS_LPSTS);
  142. value &= ~USBHS_LPSTS_SUSPM;
  143. writew(value, base + USBHS_LPSTS);
  144. value = readl(base + USBHS_UGCTRL);
  145. value |= USBHS_UGCTRL_PLLRESET;
  146. writel(value, base + USBHS_UGCTRL);
  147. spin_unlock_irqrestore(&drv->lock, flags);
  148. return 0;
  149. }
  150. static const struct phy_ops rcar_gen2_phy_ops = {
  151. .init = rcar_gen2_phy_init,
  152. .exit = rcar_gen2_phy_exit,
  153. .power_on = rcar_gen2_phy_power_on,
  154. .power_off = rcar_gen2_phy_power_off,
  155. .owner = THIS_MODULE,
  156. };
  157. static const struct of_device_id rcar_gen2_phy_match_table[] = {
  158. { .compatible = "renesas,usb-phy-r8a7790" },
  159. { .compatible = "renesas,usb-phy-r8a7791" },
  160. { .compatible = "renesas,usb-phy-r8a7794" },
  161. { }
  162. };
  163. MODULE_DEVICE_TABLE(of, rcar_gen2_phy_match_table);
  164. static struct phy *rcar_gen2_phy_xlate(struct device *dev,
  165. struct of_phandle_args *args)
  166. {
  167. struct rcar_gen2_phy_driver *drv;
  168. struct device_node *np = args->np;
  169. int i;
  170. drv = dev_get_drvdata(dev);
  171. if (!drv)
  172. return ERR_PTR(-EINVAL);
  173. for (i = 0; i < drv->num_channels; i++) {
  174. if (np == drv->channels[i].of_node)
  175. break;
  176. }
  177. if (i >= drv->num_channels || args->args[0] >= 2)
  178. return ERR_PTR(-ENODEV);
  179. return drv->channels[i].phys[args->args[0]].phy;
  180. }
  181. static const u32 select_mask[] = {
  182. [0] = USBHS_UGCTRL2_USB0SEL,
  183. [2] = USBHS_UGCTRL2_USB2SEL,
  184. };
  185. static const u32 select_value[][PHYS_PER_CHANNEL] = {
  186. [0] = { USBHS_UGCTRL2_USB0SEL_PCI, USBHS_UGCTRL2_USB0SEL_HS_USB },
  187. [2] = { USBHS_UGCTRL2_USB2SEL_PCI, USBHS_UGCTRL2_USB2SEL_USB30 },
  188. };
  189. static int rcar_gen2_phy_probe(struct platform_device *pdev)
  190. {
  191. struct device *dev = &pdev->dev;
  192. struct rcar_gen2_phy_driver *drv;
  193. struct phy_provider *provider;
  194. struct device_node *np;
  195. struct resource *res;
  196. void __iomem *base;
  197. struct clk *clk;
  198. int i = 0;
  199. if (!dev->of_node) {
  200. dev_err(dev,
  201. "This driver is required to be instantiated from device tree\n");
  202. return -EINVAL;
  203. }
  204. clk = devm_clk_get(dev, "usbhs");
  205. if (IS_ERR(clk)) {
  206. dev_err(dev, "Can't get USBHS clock\n");
  207. return PTR_ERR(clk);
  208. }
  209. res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
  210. base = devm_ioremap_resource(dev, res);
  211. if (IS_ERR(base))
  212. return PTR_ERR(base);
  213. drv = devm_kzalloc(dev, sizeof(*drv), GFP_KERNEL);
  214. if (!drv)
  215. return -ENOMEM;
  216. spin_lock_init(&drv->lock);
  217. drv->clk = clk;
  218. drv->base = base;
  219. drv->num_channels = of_get_child_count(dev->of_node);
  220. drv->channels = devm_kcalloc(dev, drv->num_channels,
  221. sizeof(struct rcar_gen2_channel),
  222. GFP_KERNEL);
  223. if (!drv->channels)
  224. return -ENOMEM;
  225. for_each_child_of_node(dev->of_node, np) {
  226. struct rcar_gen2_channel *channel = drv->channels + i;
  227. u32 channel_num;
  228. int error, n;
  229. channel->of_node = np;
  230. channel->drv = drv;
  231. channel->selected_phy = -1;
  232. error = of_property_read_u32(np, "reg", &channel_num);
  233. if (error || channel_num > 2) {
  234. dev_err(dev, "Invalid \"reg\" property\n");
  235. return error;
  236. }
  237. channel->select_mask = select_mask[channel_num];
  238. for (n = 0; n < PHYS_PER_CHANNEL; n++) {
  239. struct rcar_gen2_phy *phy = &channel->phys[n];
  240. phy->channel = channel;
  241. phy->number = n;
  242. phy->select_value = select_value[channel_num][n];
  243. phy->phy = devm_phy_create(dev, NULL,
  244. &rcar_gen2_phy_ops);
  245. if (IS_ERR(phy->phy)) {
  246. dev_err(dev, "Failed to create PHY\n");
  247. return PTR_ERR(phy->phy);
  248. }
  249. phy_set_drvdata(phy->phy, phy);
  250. }
  251. i++;
  252. }
  253. provider = devm_of_phy_provider_register(dev, rcar_gen2_phy_xlate);
  254. if (IS_ERR(provider)) {
  255. dev_err(dev, "Failed to register PHY provider\n");
  256. return PTR_ERR(provider);
  257. }
  258. dev_set_drvdata(dev, drv);
  259. return 0;
  260. }
  261. static struct platform_driver rcar_gen2_phy_driver = {
  262. .driver = {
  263. .name = "phy_rcar_gen2",
  264. .of_match_table = rcar_gen2_phy_match_table,
  265. },
  266. .probe = rcar_gen2_phy_probe,
  267. };
  268. module_platform_driver(rcar_gen2_phy_driver);
  269. MODULE_LICENSE("GPL v2");
  270. MODULE_DESCRIPTION("Renesas R-Car Gen2 PHY");
  271. MODULE_AUTHOR("Sergei Shtylyov <sergei.shtylyov@cogentembedded.com>");