phy-samsung-usb2.c 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509
  1. /* linux/drivers/usb/phy/phy-samsung-usb2.c
  2. *
  3. * Copyright (c) 2012 Samsung Electronics Co., Ltd.
  4. * http://www.samsung.com
  5. *
  6. * Author: Praveen Paneri <p.paneri@samsung.com>
  7. *
  8. * Samsung USB2.0 PHY transceiver; talks to S3C HS OTG controller, EHCI-S5P and
  9. * OHCI-EXYNOS controllers.
  10. *
  11. * This program is free software; you can redistribute it and/or modify
  12. * it under the terms of the GNU General Public License version 2 as
  13. * published by the Free Software Foundation.
  14. *
  15. * This program is distributed in the hope that it will be useful,
  16. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  17. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  18. * GNU General Public License for more details.
  19. */
  20. #include <linux/module.h>
  21. #include <linux/platform_device.h>
  22. #include <linux/clk.h>
  23. #include <linux/delay.h>
  24. #include <linux/device.h>
  25. #include <linux/err.h>
  26. #include <linux/io.h>
  27. #include <linux/of.h>
  28. #include <linux/usb/otg.h>
  29. #include <linux/usb/samsung_usb_phy.h>
  30. #include <linux/platform_data/samsung-usbphy.h>
  31. #include "phy-samsung-usb.h"
  32. static int samsung_usbphy_set_host(struct usb_otg *otg, struct usb_bus *host)
  33. {
  34. if (!otg)
  35. return -ENODEV;
  36. if (!otg->host)
  37. otg->host = host;
  38. return 0;
  39. }
  40. static bool exynos5_phyhost_is_on(void __iomem *regs)
  41. {
  42. u32 reg;
  43. reg = readl(regs + EXYNOS5_PHY_HOST_CTRL0);
  44. return !(reg & HOST_CTRL0_SIDDQ);
  45. }
  46. static void samsung_exynos5_usb2phy_enable(struct samsung_usbphy *sphy)
  47. {
  48. void __iomem *regs = sphy->regs;
  49. u32 phyclk = sphy->ref_clk_freq;
  50. u32 phyhost;
  51. u32 phyotg;
  52. u32 phyhsic;
  53. u32 ehcictrl;
  54. u32 ohcictrl;
  55. /*
  56. * phy_usage helps in keeping usage count for phy
  57. * so that the first consumer enabling the phy is also
  58. * the last consumer to disable it.
  59. */
  60. atomic_inc(&sphy->phy_usage);
  61. if (exynos5_phyhost_is_on(regs)) {
  62. dev_info(sphy->dev, "Already power on PHY\n");
  63. return;
  64. }
  65. /* Host configuration */
  66. phyhost = readl(regs + EXYNOS5_PHY_HOST_CTRL0);
  67. /* phy reference clock configuration */
  68. phyhost &= ~HOST_CTRL0_FSEL_MASK;
  69. phyhost |= HOST_CTRL0_FSEL(phyclk);
  70. /* host phy reset */
  71. phyhost &= ~(HOST_CTRL0_PHYSWRST |
  72. HOST_CTRL0_PHYSWRSTALL |
  73. HOST_CTRL0_SIDDQ |
  74. /* Enable normal mode of operation */
  75. HOST_CTRL0_FORCESUSPEND |
  76. HOST_CTRL0_FORCESLEEP);
  77. /* Link reset */
  78. phyhost |= (HOST_CTRL0_LINKSWRST |
  79. HOST_CTRL0_UTMISWRST |
  80. /* COMMON Block configuration during suspend */
  81. HOST_CTRL0_COMMONON_N);
  82. writel(phyhost, regs + EXYNOS5_PHY_HOST_CTRL0);
  83. udelay(10);
  84. phyhost &= ~(HOST_CTRL0_LINKSWRST |
  85. HOST_CTRL0_UTMISWRST);
  86. writel(phyhost, regs + EXYNOS5_PHY_HOST_CTRL0);
  87. /* OTG configuration */
  88. phyotg = readl(regs + EXYNOS5_PHY_OTG_SYS);
  89. /* phy reference clock configuration */
  90. phyotg &= ~OTG_SYS_FSEL_MASK;
  91. phyotg |= OTG_SYS_FSEL(phyclk);
  92. /* Enable normal mode of operation */
  93. phyotg &= ~(OTG_SYS_FORCESUSPEND |
  94. OTG_SYS_SIDDQ_UOTG |
  95. OTG_SYS_FORCESLEEP |
  96. OTG_SYS_REFCLKSEL_MASK |
  97. /* COMMON Block configuration during suspend */
  98. OTG_SYS_COMMON_ON);
  99. /* OTG phy & link reset */
  100. phyotg |= (OTG_SYS_PHY0_SWRST |
  101. OTG_SYS_LINKSWRST_UOTG |
  102. OTG_SYS_PHYLINK_SWRESET |
  103. OTG_SYS_OTGDISABLE |
  104. /* Set phy refclk */
  105. OTG_SYS_REFCLKSEL_CLKCORE);
  106. writel(phyotg, regs + EXYNOS5_PHY_OTG_SYS);
  107. udelay(10);
  108. phyotg &= ~(OTG_SYS_PHY0_SWRST |
  109. OTG_SYS_LINKSWRST_UOTG |
  110. OTG_SYS_PHYLINK_SWRESET);
  111. writel(phyotg, regs + EXYNOS5_PHY_OTG_SYS);
  112. /* HSIC phy configuration */
  113. phyhsic = (HSIC_CTRL_REFCLKDIV_12 |
  114. HSIC_CTRL_REFCLKSEL |
  115. HSIC_CTRL_PHYSWRST);
  116. writel(phyhsic, regs + EXYNOS5_PHY_HSIC_CTRL1);
  117. writel(phyhsic, regs + EXYNOS5_PHY_HSIC_CTRL2);
  118. udelay(10);
  119. phyhsic &= ~HSIC_CTRL_PHYSWRST;
  120. writel(phyhsic, regs + EXYNOS5_PHY_HSIC_CTRL1);
  121. writel(phyhsic, regs + EXYNOS5_PHY_HSIC_CTRL2);
  122. udelay(80);
  123. /* enable EHCI DMA burst */
  124. ehcictrl = readl(regs + EXYNOS5_PHY_HOST_EHCICTRL);
  125. ehcictrl |= (HOST_EHCICTRL_ENAINCRXALIGN |
  126. HOST_EHCICTRL_ENAINCR4 |
  127. HOST_EHCICTRL_ENAINCR8 |
  128. HOST_EHCICTRL_ENAINCR16);
  129. writel(ehcictrl, regs + EXYNOS5_PHY_HOST_EHCICTRL);
  130. /* set ohci_suspend_on_n */
  131. ohcictrl = readl(regs + EXYNOS5_PHY_HOST_OHCICTRL);
  132. ohcictrl |= HOST_OHCICTRL_SUSPLGCY;
  133. writel(ohcictrl, regs + EXYNOS5_PHY_HOST_OHCICTRL);
  134. }
  135. static void samsung_usb2phy_enable(struct samsung_usbphy *sphy)
  136. {
  137. void __iomem *regs = sphy->regs;
  138. u32 phypwr;
  139. u32 phyclk;
  140. u32 rstcon;
  141. /* set clock frequency for PLL */
  142. phyclk = sphy->ref_clk_freq;
  143. phypwr = readl(regs + SAMSUNG_PHYPWR);
  144. rstcon = readl(regs + SAMSUNG_RSTCON);
  145. switch (sphy->drv_data->cpu_type) {
  146. case TYPE_S3C64XX:
  147. phyclk &= ~PHYCLK_COMMON_ON_N;
  148. phypwr &= ~PHYPWR_NORMAL_MASK;
  149. rstcon |= RSTCON_SWRST;
  150. break;
  151. case TYPE_EXYNOS4210:
  152. phypwr &= ~PHYPWR_NORMAL_MASK_PHY0;
  153. rstcon |= RSTCON_SWRST;
  154. default:
  155. break;
  156. }
  157. writel(phyclk, regs + SAMSUNG_PHYCLK);
  158. /* Configure PHY0 for normal operation*/
  159. writel(phypwr, regs + SAMSUNG_PHYPWR);
  160. /* reset all ports of PHY and Link */
  161. writel(rstcon, regs + SAMSUNG_RSTCON);
  162. udelay(10);
  163. rstcon &= ~RSTCON_SWRST;
  164. writel(rstcon, regs + SAMSUNG_RSTCON);
  165. }
  166. static void samsung_exynos5_usb2phy_disable(struct samsung_usbphy *sphy)
  167. {
  168. void __iomem *regs = sphy->regs;
  169. u32 phyhost;
  170. u32 phyotg;
  171. u32 phyhsic;
  172. if (atomic_dec_return(&sphy->phy_usage) > 0) {
  173. dev_info(sphy->dev, "still being used\n");
  174. return;
  175. }
  176. phyhsic = (HSIC_CTRL_REFCLKDIV_12 |
  177. HSIC_CTRL_REFCLKSEL |
  178. HSIC_CTRL_SIDDQ |
  179. HSIC_CTRL_FORCESLEEP |
  180. HSIC_CTRL_FORCESUSPEND);
  181. writel(phyhsic, regs + EXYNOS5_PHY_HSIC_CTRL1);
  182. writel(phyhsic, regs + EXYNOS5_PHY_HSIC_CTRL2);
  183. phyhost = readl(regs + EXYNOS5_PHY_HOST_CTRL0);
  184. phyhost |= (HOST_CTRL0_SIDDQ |
  185. HOST_CTRL0_FORCESUSPEND |
  186. HOST_CTRL0_FORCESLEEP |
  187. HOST_CTRL0_PHYSWRST |
  188. HOST_CTRL0_PHYSWRSTALL);
  189. writel(phyhost, regs + EXYNOS5_PHY_HOST_CTRL0);
  190. phyotg = readl(regs + EXYNOS5_PHY_OTG_SYS);
  191. phyotg |= (OTG_SYS_FORCESUSPEND |
  192. OTG_SYS_SIDDQ_UOTG |
  193. OTG_SYS_FORCESLEEP);
  194. writel(phyotg, regs + EXYNOS5_PHY_OTG_SYS);
  195. }
  196. static void samsung_usb2phy_disable(struct samsung_usbphy *sphy)
  197. {
  198. void __iomem *regs = sphy->regs;
  199. u32 phypwr;
  200. phypwr = readl(regs + SAMSUNG_PHYPWR);
  201. switch (sphy->drv_data->cpu_type) {
  202. case TYPE_S3C64XX:
  203. phypwr |= PHYPWR_NORMAL_MASK;
  204. break;
  205. case TYPE_EXYNOS4210:
  206. phypwr |= PHYPWR_NORMAL_MASK_PHY0;
  207. default:
  208. break;
  209. }
  210. /* Disable analog and otg block power */
  211. writel(phypwr, regs + SAMSUNG_PHYPWR);
  212. }
  213. /*
  214. * The function passed to the usb driver for phy initialization
  215. */
  216. static int samsung_usb2phy_init(struct usb_phy *phy)
  217. {
  218. struct samsung_usbphy *sphy;
  219. struct usb_bus *host = NULL;
  220. unsigned long flags;
  221. int ret = 0;
  222. sphy = phy_to_sphy(phy);
  223. host = phy->otg->host;
  224. /* Enable the phy clock */
  225. ret = clk_prepare_enable(sphy->clk);
  226. if (ret) {
  227. dev_err(sphy->dev, "%s: clk_prepare_enable failed\n", __func__);
  228. return ret;
  229. }
  230. spin_lock_irqsave(&sphy->lock, flags);
  231. if (host) {
  232. /* setting default phy-type for USB 2.0 */
  233. if (!strstr(dev_name(host->controller), "ehci") ||
  234. !strstr(dev_name(host->controller), "ohci"))
  235. samsung_usbphy_set_type(&sphy->phy, USB_PHY_TYPE_HOST);
  236. } else {
  237. samsung_usbphy_set_type(&sphy->phy, USB_PHY_TYPE_DEVICE);
  238. }
  239. /* Disable phy isolation */
  240. if (sphy->plat && sphy->plat->pmu_isolation)
  241. sphy->plat->pmu_isolation(false);
  242. else
  243. samsung_usbphy_set_isolation(sphy, false);
  244. /* Selecting Host/OTG mode; After reset USB2.0PHY_CFG: HOST */
  245. samsung_usbphy_cfg_sel(sphy);
  246. /* Initialize usb phy registers */
  247. if (sphy->drv_data->cpu_type == TYPE_EXYNOS5250)
  248. samsung_exynos5_usb2phy_enable(sphy);
  249. else
  250. samsung_usb2phy_enable(sphy);
  251. spin_unlock_irqrestore(&sphy->lock, flags);
  252. /* Disable the phy clock */
  253. clk_disable_unprepare(sphy->clk);
  254. return ret;
  255. }
  256. /*
  257. * The function passed to the usb driver for phy shutdown
  258. */
  259. static void samsung_usb2phy_shutdown(struct usb_phy *phy)
  260. {
  261. struct samsung_usbphy *sphy;
  262. struct usb_bus *host = NULL;
  263. unsigned long flags;
  264. sphy = phy_to_sphy(phy);
  265. host = phy->otg->host;
  266. if (clk_prepare_enable(sphy->clk)) {
  267. dev_err(sphy->dev, "%s: clk_prepare_enable failed\n", __func__);
  268. return;
  269. }
  270. spin_lock_irqsave(&sphy->lock, flags);
  271. if (host) {
  272. /* setting default phy-type for USB 2.0 */
  273. if (!strstr(dev_name(host->controller), "ehci") ||
  274. !strstr(dev_name(host->controller), "ohci"))
  275. samsung_usbphy_set_type(&sphy->phy, USB_PHY_TYPE_HOST);
  276. } else {
  277. samsung_usbphy_set_type(&sphy->phy, USB_PHY_TYPE_DEVICE);
  278. }
  279. /* De-initialize usb phy registers */
  280. if (sphy->drv_data->cpu_type == TYPE_EXYNOS5250)
  281. samsung_exynos5_usb2phy_disable(sphy);
  282. else
  283. samsung_usb2phy_disable(sphy);
  284. /* Enable phy isolation */
  285. if (sphy->plat && sphy->plat->pmu_isolation)
  286. sphy->plat->pmu_isolation(true);
  287. else
  288. samsung_usbphy_set_isolation(sphy, true);
  289. spin_unlock_irqrestore(&sphy->lock, flags);
  290. clk_disable_unprepare(sphy->clk);
  291. }
  292. static int samsung_usb2phy_probe(struct platform_device *pdev)
  293. {
  294. struct samsung_usbphy *sphy;
  295. struct usb_otg *otg;
  296. struct samsung_usbphy_data *pdata = pdev->dev.platform_data;
  297. const struct samsung_usbphy_drvdata *drv_data;
  298. struct device *dev = &pdev->dev;
  299. struct resource *phy_mem;
  300. void __iomem *phy_base;
  301. struct clk *clk;
  302. int ret;
  303. phy_mem = platform_get_resource(pdev, IORESOURCE_MEM, 0);
  304. if (!phy_mem) {
  305. dev_err(dev, "%s: missing mem resource\n", __func__);
  306. return -ENODEV;
  307. }
  308. phy_base = devm_ioremap_resource(dev, phy_mem);
  309. if (IS_ERR(phy_base))
  310. return PTR_ERR(phy_base);
  311. sphy = devm_kzalloc(dev, sizeof(*sphy), GFP_KERNEL);
  312. if (!sphy)
  313. return -ENOMEM;
  314. otg = devm_kzalloc(dev, sizeof(*otg), GFP_KERNEL);
  315. if (!otg)
  316. return -ENOMEM;
  317. drv_data = samsung_usbphy_get_driver_data(pdev);
  318. if (drv_data->cpu_type == TYPE_EXYNOS5250)
  319. clk = devm_clk_get(dev, "usbhost");
  320. else
  321. clk = devm_clk_get(dev, "otg");
  322. if (IS_ERR(clk)) {
  323. dev_err(dev, "Failed to get otg clock\n");
  324. return PTR_ERR(clk);
  325. }
  326. sphy->dev = dev;
  327. if (dev->of_node) {
  328. ret = samsung_usbphy_parse_dt(sphy);
  329. if (ret < 0)
  330. return ret;
  331. } else {
  332. if (!pdata) {
  333. dev_err(dev, "no platform data specified\n");
  334. return -EINVAL;
  335. }
  336. }
  337. sphy->plat = pdata;
  338. sphy->regs = phy_base;
  339. sphy->clk = clk;
  340. sphy->drv_data = drv_data;
  341. sphy->phy.dev = sphy->dev;
  342. sphy->phy.label = "samsung-usb2phy";
  343. sphy->phy.init = samsung_usb2phy_init;
  344. sphy->phy.shutdown = samsung_usb2phy_shutdown;
  345. sphy->ref_clk_freq = samsung_usbphy_get_refclk_freq(sphy);
  346. sphy->phy.otg = otg;
  347. sphy->phy.otg->phy = &sphy->phy;
  348. sphy->phy.otg->set_host = samsung_usbphy_set_host;
  349. spin_lock_init(&sphy->lock);
  350. platform_set_drvdata(pdev, sphy);
  351. return usb_add_phy(&sphy->phy, USB_PHY_TYPE_USB2);
  352. }
  353. static int samsung_usb2phy_remove(struct platform_device *pdev)
  354. {
  355. struct samsung_usbphy *sphy = platform_get_drvdata(pdev);
  356. usb_remove_phy(&sphy->phy);
  357. if (sphy->pmuregs)
  358. iounmap(sphy->pmuregs);
  359. if (sphy->sysreg)
  360. iounmap(sphy->sysreg);
  361. return 0;
  362. }
  363. static const struct samsung_usbphy_drvdata usb2phy_s3c64xx = {
  364. .cpu_type = TYPE_S3C64XX,
  365. .devphy_en_mask = S3C64XX_USBPHY_ENABLE,
  366. };
  367. static const struct samsung_usbphy_drvdata usb2phy_exynos4 = {
  368. .cpu_type = TYPE_EXYNOS4210,
  369. .devphy_en_mask = EXYNOS_USBPHY_ENABLE,
  370. .hostphy_en_mask = EXYNOS_USBPHY_ENABLE,
  371. };
  372. static struct samsung_usbphy_drvdata usb2phy_exynos5 = {
  373. .cpu_type = TYPE_EXYNOS5250,
  374. .hostphy_en_mask = EXYNOS_USBPHY_ENABLE,
  375. .hostphy_reg_offset = EXYNOS_USBHOST_PHY_CTRL_OFFSET,
  376. };
  377. #ifdef CONFIG_OF
  378. static const struct of_device_id samsung_usbphy_dt_match[] = {
  379. {
  380. .compatible = "samsung,s3c64xx-usb2phy",
  381. .data = &usb2phy_s3c64xx,
  382. }, {
  383. .compatible = "samsung,exynos4210-usb2phy",
  384. .data = &usb2phy_exynos4,
  385. }, {
  386. .compatible = "samsung,exynos5250-usb2phy",
  387. .data = &usb2phy_exynos5
  388. },
  389. {},
  390. };
  391. MODULE_DEVICE_TABLE(of, samsung_usbphy_dt_match);
  392. #endif
  393. static struct platform_device_id samsung_usbphy_driver_ids[] = {
  394. {
  395. .name = "s3c64xx-usb2phy",
  396. .driver_data = (unsigned long)&usb2phy_s3c64xx,
  397. }, {
  398. .name = "exynos4210-usb2phy",
  399. .driver_data = (unsigned long)&usb2phy_exynos4,
  400. }, {
  401. .name = "exynos5250-usb2phy",
  402. .driver_data = (unsigned long)&usb2phy_exynos5,
  403. },
  404. {},
  405. };
  406. MODULE_DEVICE_TABLE(platform, samsung_usbphy_driver_ids);
  407. static struct platform_driver samsung_usb2phy_driver = {
  408. .probe = samsung_usb2phy_probe,
  409. .remove = samsung_usb2phy_remove,
  410. .id_table = samsung_usbphy_driver_ids,
  411. .driver = {
  412. .name = "samsung-usb2phy",
  413. .owner = THIS_MODULE,
  414. .of_match_table = of_match_ptr(samsung_usbphy_dt_match),
  415. },
  416. };
  417. module_platform_driver(samsung_usb2phy_driver);
  418. MODULE_DESCRIPTION("Samsung USB 2.0 phy controller");
  419. MODULE_AUTHOR("Praveen Paneri <p.paneri@samsung.com>");
  420. MODULE_LICENSE("GPL");
  421. MODULE_ALIAS("platform:samsung-usb2phy");