phy-rcar-usb.c 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192
  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. /* REGS block */
  18. #define USBPCTRL0 0x00
  19. #define USBPCTRL1 0x04
  20. #define USBST 0x08
  21. #define USBEH0 0x0C
  22. #define USBOH0 0x1C
  23. #define USBCTL0 0x58
  24. /* USBPCTRL1 */
  25. #define PHY_RST (1 << 2)
  26. #define PLL_ENB (1 << 1)
  27. #define PHY_ENB (1 << 0)
  28. /* USBST */
  29. #define ST_ACT (1 << 31)
  30. #define ST_PLL (1 << 30)
  31. struct rcar_usb_phy_priv {
  32. struct usb_phy phy;
  33. spinlock_t lock;
  34. void __iomem *reg0;
  35. int counter;
  36. };
  37. #define usb_phy_to_priv(p) container_of(p, struct rcar_usb_phy_priv, phy)
  38. /*
  39. * USB initial/install operation.
  40. *
  41. * This function setup USB phy.
  42. * The used value and setting order came from
  43. * [USB :: Initial setting] on datasheet.
  44. */
  45. static int rcar_usb_phy_init(struct usb_phy *phy)
  46. {
  47. struct rcar_usb_phy_priv *priv = usb_phy_to_priv(phy);
  48. struct device *dev = phy->dev;
  49. void __iomem *reg0 = priv->reg0;
  50. int i;
  51. u32 val;
  52. unsigned long flags;
  53. spin_lock_irqsave(&priv->lock, flags);
  54. if (priv->counter++ == 0) {
  55. /*
  56. * USB phy start-up
  57. */
  58. /* (1) USB-PHY standby release */
  59. iowrite32(PHY_ENB, (reg0 + USBPCTRL1));
  60. /* (2) start USB-PHY internal PLL */
  61. iowrite32(PHY_ENB | PLL_ENB, (reg0 + USBPCTRL1));
  62. /* (3) USB module status check */
  63. for (i = 0; i < 1024; i++) {
  64. udelay(10);
  65. val = ioread32(reg0 + USBST);
  66. if (val == (ST_ACT | ST_PLL))
  67. break;
  68. }
  69. if (val != (ST_ACT | ST_PLL)) {
  70. dev_err(dev, "USB phy not ready\n");
  71. goto phy_init_end;
  72. }
  73. /* (4) USB-PHY reset clear */
  74. iowrite32(PHY_ENB | PLL_ENB | PHY_RST, (reg0 + USBPCTRL1));
  75. /* set platform specific port settings */
  76. iowrite32(0x00000000, (reg0 + USBPCTRL0));
  77. /*
  78. * Bus alignment settings
  79. */
  80. /* (1) EHCI bus alignment (little endian) */
  81. iowrite32(0x00000000, (reg0 + USBEH0));
  82. /* (1) OHCI bus alignment (little endian) */
  83. iowrite32(0x00000000, (reg0 + USBOH0));
  84. }
  85. phy_init_end:
  86. spin_unlock_irqrestore(&priv->lock, flags);
  87. return 0;
  88. }
  89. static void rcar_usb_phy_shutdown(struct usb_phy *phy)
  90. {
  91. struct rcar_usb_phy_priv *priv = usb_phy_to_priv(phy);
  92. void __iomem *reg0 = priv->reg0;
  93. unsigned long flags;
  94. spin_lock_irqsave(&priv->lock, flags);
  95. if (priv->counter-- == 1) { /* last user */
  96. iowrite32(0x00000000, (reg0 + USBPCTRL0));
  97. iowrite32(0x00000000, (reg0 + USBPCTRL1));
  98. }
  99. spin_unlock_irqrestore(&priv->lock, flags);
  100. }
  101. static int rcar_usb_phy_probe(struct platform_device *pdev)
  102. {
  103. struct rcar_usb_phy_priv *priv;
  104. struct resource *res0;
  105. struct device *dev = &pdev->dev;
  106. void __iomem *reg0;
  107. int ret;
  108. res0 = platform_get_resource(pdev, IORESOURCE_MEM, 0);
  109. if (!res0) {
  110. dev_err(dev, "Not enough platform resources\n");
  111. return -EINVAL;
  112. }
  113. reg0 = devm_ioremap_resource(dev, res0);
  114. if (IS_ERR(reg0))
  115. return PTR_ERR(reg0);
  116. priv = devm_kzalloc(dev, sizeof(*priv), GFP_KERNEL);
  117. if (!priv) {
  118. dev_err(dev, "priv data allocation error\n");
  119. return -ENOMEM;
  120. }
  121. priv->reg0 = reg0;
  122. priv->counter = 0;
  123. priv->phy.dev = dev;
  124. priv->phy.label = dev_name(dev);
  125. priv->phy.init = rcar_usb_phy_init;
  126. priv->phy.shutdown = rcar_usb_phy_shutdown;
  127. spin_lock_init(&priv->lock);
  128. ret = usb_add_phy(&priv->phy, USB_PHY_TYPE_USB2);
  129. if (ret < 0) {
  130. dev_err(dev, "usb phy addition error\n");
  131. return ret;
  132. }
  133. platform_set_drvdata(pdev, priv);
  134. return ret;
  135. }
  136. static int rcar_usb_phy_remove(struct platform_device *pdev)
  137. {
  138. struct rcar_usb_phy_priv *priv = platform_get_drvdata(pdev);
  139. usb_remove_phy(&priv->phy);
  140. return 0;
  141. }
  142. static struct platform_driver rcar_usb_phy_driver = {
  143. .driver = {
  144. .name = "rcar_usb_phy",
  145. },
  146. .probe = rcar_usb_phy_probe,
  147. .remove = rcar_usb_phy_remove,
  148. };
  149. module_platform_driver(rcar_usb_phy_driver);
  150. MODULE_LICENSE("GPL v2");
  151. MODULE_DESCRIPTION("Renesas R-Car USB phy");
  152. MODULE_AUTHOR("Kuninori Morimoto <kuninori.morimoto.gx@renesas.com>");