rcar-phy.c 5.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220
  1. /*
  2. * Renesas R-Car USB phy driver
  3. *
  4. * Copyright (C) 2012 Renesas Solutions Corp.
  5. * Kuninori Morimoto <kuninori.morimoto.gx@renesas.com>
  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/delay.h>
  12. #include <linux/io.h>
  13. #include <linux/usb/otg.h>
  14. #include <linux/platform_device.h>
  15. #include <linux/spinlock.h>
  16. #include <linux/module.h>
  17. /* USBH common register */
  18. #define USBPCTRL0 0x0800
  19. #define USBPCTRL1 0x0804
  20. #define USBST 0x0808
  21. #define USBEH0 0x080C
  22. #define USBOH0 0x081C
  23. #define USBCTL0 0x0858
  24. #define EIIBC1 0x0094
  25. #define EIIBC2 0x009C
  26. /* USBPCTRL1 */
  27. #define PHY_RST (1 << 2)
  28. #define PLL_ENB (1 << 1)
  29. #define PHY_ENB (1 << 0)
  30. /* USBST */
  31. #define ST_ACT (1 << 31)
  32. #define ST_PLL (1 << 30)
  33. struct rcar_usb_phy_priv {
  34. struct usb_phy phy;
  35. spinlock_t lock;
  36. void __iomem *reg0;
  37. void __iomem *reg1;
  38. int counter;
  39. };
  40. #define usb_phy_to_priv(p) container_of(p, struct rcar_usb_phy_priv, phy)
  41. /*
  42. * USB initial/install operation.
  43. *
  44. * This function setup USB phy.
  45. * The used value and setting order came from
  46. * [USB :: Initial setting] on datasheet.
  47. */
  48. static int rcar_usb_phy_init(struct usb_phy *phy)
  49. {
  50. struct rcar_usb_phy_priv *priv = usb_phy_to_priv(phy);
  51. struct device *dev = phy->dev;
  52. void __iomem *reg0 = priv->reg0;
  53. void __iomem *reg1 = priv->reg1;
  54. int i;
  55. u32 val;
  56. unsigned long flags;
  57. spin_lock_irqsave(&priv->lock, flags);
  58. if (priv->counter++ == 0) {
  59. /*
  60. * USB phy start-up
  61. */
  62. /* (1) USB-PHY standby release */
  63. iowrite32(PHY_ENB, (reg0 + USBPCTRL1));
  64. /* (2) start USB-PHY internal PLL */
  65. iowrite32(PHY_ENB | PLL_ENB, (reg0 + USBPCTRL1));
  66. /* (3) USB module status check */
  67. for (i = 0; i < 1024; i++) {
  68. udelay(10);
  69. val = ioread32(reg0 + USBST);
  70. if (val == (ST_ACT | ST_PLL))
  71. break;
  72. }
  73. if (val != (ST_ACT | ST_PLL)) {
  74. dev_err(dev, "USB phy not ready\n");
  75. goto phy_init_end;
  76. }
  77. /* (4) USB-PHY reset clear */
  78. iowrite32(PHY_ENB | PLL_ENB | PHY_RST, (reg0 + USBPCTRL1));
  79. /* set platform specific port settings */
  80. iowrite32(0x00000000, (reg0 + USBPCTRL0));
  81. /*
  82. * EHCI IP internal buffer setting
  83. * EHCI IP internal buffer enable
  84. *
  85. * These are recommended value of a datasheet
  86. * see [USB :: EHCI internal buffer setting]
  87. */
  88. iowrite32(0x00ff0040, (reg0 + EIIBC1));
  89. iowrite32(0x00ff0040, (reg1 + EIIBC1));
  90. iowrite32(0x00000001, (reg0 + EIIBC2));
  91. iowrite32(0x00000001, (reg1 + EIIBC2));
  92. /*
  93. * Bus alignment settings
  94. */
  95. /* (1) EHCI bus alignment (little endian) */
  96. iowrite32(0x00000000, (reg0 + USBEH0));
  97. /* (1) OHCI bus alignment (little endian) */
  98. iowrite32(0x00000000, (reg0 + USBOH0));
  99. }
  100. phy_init_end:
  101. spin_unlock_irqrestore(&priv->lock, flags);
  102. return 0;
  103. }
  104. static void rcar_usb_phy_shutdown(struct usb_phy *phy)
  105. {
  106. struct rcar_usb_phy_priv *priv = usb_phy_to_priv(phy);
  107. void __iomem *reg0 = priv->reg0;
  108. unsigned long flags;
  109. spin_lock_irqsave(&priv->lock, flags);
  110. if (priv->counter-- == 1) { /* last user */
  111. iowrite32(0x00000000, (reg0 + USBPCTRL0));
  112. iowrite32(0x00000000, (reg0 + USBPCTRL1));
  113. }
  114. spin_unlock_irqrestore(&priv->lock, flags);
  115. }
  116. static int rcar_usb_phy_probe(struct platform_device *pdev)
  117. {
  118. struct rcar_usb_phy_priv *priv;
  119. struct resource *res0, *res1;
  120. struct device *dev = &pdev->dev;
  121. void __iomem *reg0, *reg1;
  122. int ret;
  123. res0 = platform_get_resource(pdev, IORESOURCE_MEM, 0);
  124. res1 = platform_get_resource(pdev, IORESOURCE_MEM, 1);
  125. if (!res0 || !res1) {
  126. dev_err(dev, "Not enough platform resources\n");
  127. return -EINVAL;
  128. }
  129. /*
  130. * CAUTION
  131. *
  132. * Because this phy address is also mapped under OHCI/EHCI address area,
  133. * this driver can't use devm_request_and_ioremap(dev, res) here
  134. */
  135. reg0 = devm_ioremap_nocache(dev, res0->start, resource_size(res0));
  136. reg1 = devm_ioremap_nocache(dev, res1->start, resource_size(res1));
  137. if (!reg0 || !reg1) {
  138. dev_err(dev, "ioremap error\n");
  139. return -ENOMEM;
  140. }
  141. priv = devm_kzalloc(dev, sizeof(*priv), GFP_KERNEL);
  142. if (!priv) {
  143. dev_err(dev, "priv data allocation error\n");
  144. return -ENOMEM;
  145. }
  146. priv->reg0 = reg0;
  147. priv->reg1 = reg1;
  148. priv->counter = 0;
  149. priv->phy.dev = dev;
  150. priv->phy.label = dev_name(dev);
  151. priv->phy.init = rcar_usb_phy_init;
  152. priv->phy.shutdown = rcar_usb_phy_shutdown;
  153. spin_lock_init(&priv->lock);
  154. ret = usb_add_phy(&priv->phy, USB_PHY_TYPE_USB2);
  155. if (ret < 0) {
  156. dev_err(dev, "usb phy addition error\n");
  157. return ret;
  158. }
  159. platform_set_drvdata(pdev, priv);
  160. return ret;
  161. }
  162. static int rcar_usb_phy_remove(struct platform_device *pdev)
  163. {
  164. struct rcar_usb_phy_priv *priv = platform_get_drvdata(pdev);
  165. usb_remove_phy(&priv->phy);
  166. return 0;
  167. }
  168. static struct platform_driver rcar_usb_phy_driver = {
  169. .driver = {
  170. .name = "rcar_usb_phy",
  171. },
  172. .probe = rcar_usb_phy_probe,
  173. .remove = rcar_usb_phy_remove,
  174. };
  175. module_platform_driver(rcar_usb_phy_driver);
  176. MODULE_LICENSE("GPL v2");
  177. MODULE_DESCRIPTION("Renesas R-Car USB phy");
  178. MODULE_AUTHOR("Kuninori Morimoto <kuninori.morimoto.gx@renesas.com>");