phy-rcar-gen3-usb2.c 9.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375
  1. /*
  2. * Renesas R-Car Gen3 for USB2.0 PHY driver
  3. *
  4. * Copyright (C) 2015 Renesas Electronics Corporation
  5. *
  6. * This is based on the phy-rcar-gen2 driver:
  7. * Copyright (C) 2014 Renesas Solutions Corp.
  8. * Copyright (C) 2014 Cogent Embedded, Inc.
  9. *
  10. * This program is free software; you can redistribute it and/or modify
  11. * it under the terms of the GNU General Public License version 2 as
  12. * published by the Free Software Foundation.
  13. */
  14. #include <linux/extcon.h>
  15. #include <linux/interrupt.h>
  16. #include <linux/io.h>
  17. #include <linux/module.h>
  18. #include <linux/of.h>
  19. #include <linux/of_address.h>
  20. #include <linux/phy/phy.h>
  21. #include <linux/platform_device.h>
  22. #include <linux/regulator/consumer.h>
  23. #include <linux/workqueue.h>
  24. /******* USB2.0 Host registers (original offset is +0x200) *******/
  25. #define USB2_INT_ENABLE 0x000
  26. #define USB2_USBCTR 0x00c
  27. #define USB2_SPD_RSM_TIMSET 0x10c
  28. #define USB2_OC_TIMSET 0x110
  29. #define USB2_COMMCTRL 0x600
  30. #define USB2_OBINTSTA 0x604
  31. #define USB2_OBINTEN 0x608
  32. #define USB2_VBCTRL 0x60c
  33. #define USB2_LINECTRL1 0x610
  34. #define USB2_ADPCTRL 0x630
  35. /* INT_ENABLE */
  36. #define USB2_INT_ENABLE_UCOM_INTEN BIT(3)
  37. #define USB2_INT_ENABLE_USBH_INTB_EN BIT(2)
  38. #define USB2_INT_ENABLE_USBH_INTA_EN BIT(1)
  39. #define USB2_INT_ENABLE_INIT (USB2_INT_ENABLE_UCOM_INTEN | \
  40. USB2_INT_ENABLE_USBH_INTB_EN | \
  41. USB2_INT_ENABLE_USBH_INTA_EN)
  42. /* USBCTR */
  43. #define USB2_USBCTR_DIRPD BIT(2)
  44. #define USB2_USBCTR_PLL_RST BIT(1)
  45. /* SPD_RSM_TIMSET */
  46. #define USB2_SPD_RSM_TIMSET_INIT 0x014e029b
  47. /* OC_TIMSET */
  48. #define USB2_OC_TIMSET_INIT 0x000209ab
  49. /* COMMCTRL */
  50. #define USB2_COMMCTRL_OTG_PERI BIT(31) /* 1 = Peripheral mode */
  51. /* OBINTSTA and OBINTEN */
  52. #define USB2_OBINT_SESSVLDCHG BIT(12)
  53. #define USB2_OBINT_IDDIGCHG BIT(11)
  54. #define USB2_OBINT_BITS (USB2_OBINT_SESSVLDCHG | \
  55. USB2_OBINT_IDDIGCHG)
  56. /* VBCTRL */
  57. #define USB2_VBCTRL_DRVVBUSSEL BIT(8)
  58. /* LINECTRL1 */
  59. #define USB2_LINECTRL1_DPRPD_EN BIT(19)
  60. #define USB2_LINECTRL1_DP_RPD BIT(18)
  61. #define USB2_LINECTRL1_DMRPD_EN BIT(17)
  62. #define USB2_LINECTRL1_DM_RPD BIT(16)
  63. /* ADPCTRL */
  64. #define USB2_ADPCTRL_OTGSESSVLD BIT(20)
  65. #define USB2_ADPCTRL_IDDIG BIT(19)
  66. #define USB2_ADPCTRL_IDPULLUP BIT(5) /* 1 = ID sampling is enabled */
  67. #define USB2_ADPCTRL_DRVVBUS BIT(4)
  68. struct rcar_gen3_chan {
  69. void __iomem *base;
  70. struct extcon_dev *extcon;
  71. struct phy *phy;
  72. struct regulator *vbus;
  73. struct work_struct work;
  74. bool extcon_host;
  75. bool has_otg;
  76. };
  77. static void rcar_gen3_phy_usb2_work(struct work_struct *work)
  78. {
  79. struct rcar_gen3_chan *ch = container_of(work, struct rcar_gen3_chan,
  80. work);
  81. if (ch->extcon_host) {
  82. extcon_set_cable_state_(ch->extcon, EXTCON_USB_HOST, true);
  83. extcon_set_cable_state_(ch->extcon, EXTCON_USB, false);
  84. } else {
  85. extcon_set_cable_state_(ch->extcon, EXTCON_USB_HOST, false);
  86. extcon_set_cable_state_(ch->extcon, EXTCON_USB, true);
  87. }
  88. }
  89. static void rcar_gen3_set_host_mode(struct rcar_gen3_chan *ch, int host)
  90. {
  91. void __iomem *usb2_base = ch->base;
  92. u32 val = readl(usb2_base + USB2_COMMCTRL);
  93. dev_vdbg(&ch->phy->dev, "%s: %08x, %d\n", __func__, val, host);
  94. if (host)
  95. val &= ~USB2_COMMCTRL_OTG_PERI;
  96. else
  97. val |= USB2_COMMCTRL_OTG_PERI;
  98. writel(val, usb2_base + USB2_COMMCTRL);
  99. }
  100. static void rcar_gen3_set_linectrl(struct rcar_gen3_chan *ch, int dp, int dm)
  101. {
  102. void __iomem *usb2_base = ch->base;
  103. u32 val = readl(usb2_base + USB2_LINECTRL1);
  104. dev_vdbg(&ch->phy->dev, "%s: %08x, %d, %d\n", __func__, val, dp, dm);
  105. val &= ~(USB2_LINECTRL1_DP_RPD | USB2_LINECTRL1_DM_RPD);
  106. if (dp)
  107. val |= USB2_LINECTRL1_DP_RPD;
  108. if (dm)
  109. val |= USB2_LINECTRL1_DM_RPD;
  110. writel(val, usb2_base + USB2_LINECTRL1);
  111. }
  112. static void rcar_gen3_enable_vbus_ctrl(struct rcar_gen3_chan *ch, int vbus)
  113. {
  114. void __iomem *usb2_base = ch->base;
  115. u32 val = readl(usb2_base + USB2_ADPCTRL);
  116. dev_vdbg(&ch->phy->dev, "%s: %08x, %d\n", __func__, val, vbus);
  117. if (vbus)
  118. val |= USB2_ADPCTRL_DRVVBUS;
  119. else
  120. val &= ~USB2_ADPCTRL_DRVVBUS;
  121. writel(val, usb2_base + USB2_ADPCTRL);
  122. }
  123. static void rcar_gen3_init_for_host(struct rcar_gen3_chan *ch)
  124. {
  125. rcar_gen3_set_linectrl(ch, 1, 1);
  126. rcar_gen3_set_host_mode(ch, 1);
  127. rcar_gen3_enable_vbus_ctrl(ch, 1);
  128. ch->extcon_host = true;
  129. schedule_work(&ch->work);
  130. }
  131. static void rcar_gen3_init_for_peri(struct rcar_gen3_chan *ch)
  132. {
  133. rcar_gen3_set_linectrl(ch, 0, 1);
  134. rcar_gen3_set_host_mode(ch, 0);
  135. rcar_gen3_enable_vbus_ctrl(ch, 0);
  136. ch->extcon_host = false;
  137. schedule_work(&ch->work);
  138. }
  139. static bool rcar_gen3_check_id(struct rcar_gen3_chan *ch)
  140. {
  141. return !!(readl(ch->base + USB2_ADPCTRL) & USB2_ADPCTRL_IDDIG);
  142. }
  143. static void rcar_gen3_device_recognition(struct rcar_gen3_chan *ch)
  144. {
  145. if (!rcar_gen3_check_id(ch))
  146. rcar_gen3_init_for_host(ch);
  147. else
  148. rcar_gen3_init_for_peri(ch);
  149. }
  150. static void rcar_gen3_init_otg(struct rcar_gen3_chan *ch)
  151. {
  152. void __iomem *usb2_base = ch->base;
  153. u32 val;
  154. val = readl(usb2_base + USB2_VBCTRL);
  155. writel(val | USB2_VBCTRL_DRVVBUSSEL, usb2_base + USB2_VBCTRL);
  156. writel(USB2_OBINT_BITS, usb2_base + USB2_OBINTSTA);
  157. val = readl(usb2_base + USB2_OBINTEN);
  158. writel(val | USB2_OBINT_BITS, usb2_base + USB2_OBINTEN);
  159. val = readl(usb2_base + USB2_ADPCTRL);
  160. writel(val | USB2_ADPCTRL_IDPULLUP, usb2_base + USB2_ADPCTRL);
  161. val = readl(usb2_base + USB2_LINECTRL1);
  162. rcar_gen3_set_linectrl(ch, 0, 0);
  163. writel(val | USB2_LINECTRL1_DPRPD_EN | USB2_LINECTRL1_DMRPD_EN,
  164. usb2_base + USB2_LINECTRL1);
  165. rcar_gen3_device_recognition(ch);
  166. }
  167. static int rcar_gen3_phy_usb2_init(struct phy *p)
  168. {
  169. struct rcar_gen3_chan *channel = phy_get_drvdata(p);
  170. void __iomem *usb2_base = channel->base;
  171. /* Initialize USB2 part */
  172. writel(USB2_INT_ENABLE_INIT, usb2_base + USB2_INT_ENABLE);
  173. writel(USB2_SPD_RSM_TIMSET_INIT, usb2_base + USB2_SPD_RSM_TIMSET);
  174. writel(USB2_OC_TIMSET_INIT, usb2_base + USB2_OC_TIMSET);
  175. /* Initialize otg part */
  176. if (channel->has_otg)
  177. rcar_gen3_init_otg(channel);
  178. return 0;
  179. }
  180. static int rcar_gen3_phy_usb2_exit(struct phy *p)
  181. {
  182. struct rcar_gen3_chan *channel = phy_get_drvdata(p);
  183. writel(0, channel->base + USB2_INT_ENABLE);
  184. return 0;
  185. }
  186. static int rcar_gen3_phy_usb2_power_on(struct phy *p)
  187. {
  188. struct rcar_gen3_chan *channel = phy_get_drvdata(p);
  189. void __iomem *usb2_base = channel->base;
  190. u32 val;
  191. int ret;
  192. if (channel->vbus) {
  193. ret = regulator_enable(channel->vbus);
  194. if (ret)
  195. return ret;
  196. }
  197. val = readl(usb2_base + USB2_USBCTR);
  198. val |= USB2_USBCTR_PLL_RST;
  199. writel(val, usb2_base + USB2_USBCTR);
  200. val &= ~USB2_USBCTR_PLL_RST;
  201. writel(val, usb2_base + USB2_USBCTR);
  202. return 0;
  203. }
  204. static int rcar_gen3_phy_usb2_power_off(struct phy *p)
  205. {
  206. struct rcar_gen3_chan *channel = phy_get_drvdata(p);
  207. int ret = 0;
  208. if (channel->vbus)
  209. ret = regulator_disable(channel->vbus);
  210. return ret;
  211. }
  212. static struct phy_ops rcar_gen3_phy_usb2_ops = {
  213. .init = rcar_gen3_phy_usb2_init,
  214. .exit = rcar_gen3_phy_usb2_exit,
  215. .power_on = rcar_gen3_phy_usb2_power_on,
  216. .power_off = rcar_gen3_phy_usb2_power_off,
  217. .owner = THIS_MODULE,
  218. };
  219. static irqreturn_t rcar_gen3_phy_usb2_irq(int irq, void *_ch)
  220. {
  221. struct rcar_gen3_chan *ch = _ch;
  222. void __iomem *usb2_base = ch->base;
  223. u32 status = readl(usb2_base + USB2_OBINTSTA);
  224. irqreturn_t ret = IRQ_NONE;
  225. if (status & USB2_OBINT_BITS) {
  226. dev_vdbg(&ch->phy->dev, "%s: %08x\n", __func__, status);
  227. writel(USB2_OBINT_BITS, usb2_base + USB2_OBINTSTA);
  228. rcar_gen3_device_recognition(ch);
  229. ret = IRQ_HANDLED;
  230. }
  231. return ret;
  232. }
  233. static const struct of_device_id rcar_gen3_phy_usb2_match_table[] = {
  234. { .compatible = "renesas,usb2-phy-r8a7795" },
  235. { .compatible = "renesas,usb2-phy-r8a7796" },
  236. { .compatible = "renesas,rcar-gen3-usb2-phy" },
  237. { }
  238. };
  239. MODULE_DEVICE_TABLE(of, rcar_gen3_phy_usb2_match_table);
  240. static const unsigned int rcar_gen3_phy_cable[] = {
  241. EXTCON_USB,
  242. EXTCON_USB_HOST,
  243. EXTCON_NONE,
  244. };
  245. static int rcar_gen3_phy_usb2_probe(struct platform_device *pdev)
  246. {
  247. struct device *dev = &pdev->dev;
  248. struct rcar_gen3_chan *channel;
  249. struct phy_provider *provider;
  250. struct resource *res;
  251. int irq;
  252. if (!dev->of_node) {
  253. dev_err(dev, "This driver needs device tree\n");
  254. return -EINVAL;
  255. }
  256. channel = devm_kzalloc(dev, sizeof(*channel), GFP_KERNEL);
  257. if (!channel)
  258. return -ENOMEM;
  259. res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
  260. channel->base = devm_ioremap_resource(dev, res);
  261. if (IS_ERR(channel->base))
  262. return PTR_ERR(channel->base);
  263. /* call request_irq for OTG */
  264. irq = platform_get_irq(pdev, 0);
  265. if (irq >= 0) {
  266. int ret;
  267. INIT_WORK(&channel->work, rcar_gen3_phy_usb2_work);
  268. irq = devm_request_irq(dev, irq, rcar_gen3_phy_usb2_irq,
  269. IRQF_SHARED, dev_name(dev), channel);
  270. if (irq < 0)
  271. dev_err(dev, "No irq handler (%d)\n", irq);
  272. channel->has_otg = true;
  273. channel->extcon = devm_extcon_dev_allocate(dev,
  274. rcar_gen3_phy_cable);
  275. if (IS_ERR(channel->extcon))
  276. return PTR_ERR(channel->extcon);
  277. ret = devm_extcon_dev_register(dev, channel->extcon);
  278. if (ret < 0) {
  279. dev_err(dev, "Failed to register extcon\n");
  280. return ret;
  281. }
  282. }
  283. /* devm_phy_create() will call pm_runtime_enable(dev); */
  284. channel->phy = devm_phy_create(dev, NULL, &rcar_gen3_phy_usb2_ops);
  285. if (IS_ERR(channel->phy)) {
  286. dev_err(dev, "Failed to create USB2 PHY\n");
  287. return PTR_ERR(channel->phy);
  288. }
  289. channel->vbus = devm_regulator_get_optional(dev, "vbus");
  290. if (IS_ERR(channel->vbus)) {
  291. if (PTR_ERR(channel->vbus) == -EPROBE_DEFER)
  292. return PTR_ERR(channel->vbus);
  293. channel->vbus = NULL;
  294. }
  295. phy_set_drvdata(channel->phy, channel);
  296. provider = devm_of_phy_provider_register(dev, of_phy_simple_xlate);
  297. if (IS_ERR(provider))
  298. dev_err(dev, "Failed to register PHY provider\n");
  299. return PTR_ERR_OR_ZERO(provider);
  300. }
  301. static struct platform_driver rcar_gen3_phy_usb2_driver = {
  302. .driver = {
  303. .name = "phy_rcar_gen3_usb2",
  304. .of_match_table = rcar_gen3_phy_usb2_match_table,
  305. },
  306. .probe = rcar_gen3_phy_usb2_probe,
  307. };
  308. module_platform_driver(rcar_gen3_phy_usb2_driver);
  309. MODULE_LICENSE("GPL v2");
  310. MODULE_DESCRIPTION("Renesas R-Car Gen3 USB 2.0 PHY");
  311. MODULE_AUTHOR("Yoshihiro Shimoda <yoshihiro.shimoda.uh@renesas.com>");