phy-samsung-usb2.c 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540
  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_EXYNOS4X12:
  152. phypwr &= ~(PHYPWR_NORMAL_MASK_HSIC0 |
  153. PHYPWR_NORMAL_MASK_HSIC1 |
  154. PHYPWR_NORMAL_MASK_PHY1);
  155. rstcon |= RSTCON_HOSTPHY_SWRST;
  156. case TYPE_EXYNOS4210:
  157. phypwr &= ~PHYPWR_NORMAL_MASK_PHY0;
  158. rstcon |= RSTCON_SWRST;
  159. default:
  160. break;
  161. }
  162. writel(phyclk, regs + SAMSUNG_PHYCLK);
  163. /* Configure PHY0 for normal operation*/
  164. writel(phypwr, regs + SAMSUNG_PHYPWR);
  165. /* reset all ports of PHY and Link */
  166. writel(rstcon, regs + SAMSUNG_RSTCON);
  167. udelay(10);
  168. if (sphy->drv_data->cpu_type == TYPE_EXYNOS4X12)
  169. rstcon &= ~RSTCON_HOSTPHY_SWRST;
  170. rstcon &= ~RSTCON_SWRST;
  171. writel(rstcon, regs + SAMSUNG_RSTCON);
  172. }
  173. static void samsung_exynos5_usb2phy_disable(struct samsung_usbphy *sphy)
  174. {
  175. void __iomem *regs = sphy->regs;
  176. u32 phyhost;
  177. u32 phyotg;
  178. u32 phyhsic;
  179. if (atomic_dec_return(&sphy->phy_usage) > 0) {
  180. dev_info(sphy->dev, "still being used\n");
  181. return;
  182. }
  183. phyhsic = (HSIC_CTRL_REFCLKDIV_12 |
  184. HSIC_CTRL_REFCLKSEL |
  185. HSIC_CTRL_SIDDQ |
  186. HSIC_CTRL_FORCESLEEP |
  187. HSIC_CTRL_FORCESUSPEND);
  188. writel(phyhsic, regs + EXYNOS5_PHY_HSIC_CTRL1);
  189. writel(phyhsic, regs + EXYNOS5_PHY_HSIC_CTRL2);
  190. phyhost = readl(regs + EXYNOS5_PHY_HOST_CTRL0);
  191. phyhost |= (HOST_CTRL0_SIDDQ |
  192. HOST_CTRL0_FORCESUSPEND |
  193. HOST_CTRL0_FORCESLEEP |
  194. HOST_CTRL0_PHYSWRST |
  195. HOST_CTRL0_PHYSWRSTALL);
  196. writel(phyhost, regs + EXYNOS5_PHY_HOST_CTRL0);
  197. phyotg = readl(regs + EXYNOS5_PHY_OTG_SYS);
  198. phyotg |= (OTG_SYS_FORCESUSPEND |
  199. OTG_SYS_SIDDQ_UOTG |
  200. OTG_SYS_FORCESLEEP);
  201. writel(phyotg, regs + EXYNOS5_PHY_OTG_SYS);
  202. }
  203. static void samsung_usb2phy_disable(struct samsung_usbphy *sphy)
  204. {
  205. void __iomem *regs = sphy->regs;
  206. u32 phypwr;
  207. phypwr = readl(regs + SAMSUNG_PHYPWR);
  208. switch (sphy->drv_data->cpu_type) {
  209. case TYPE_S3C64XX:
  210. phypwr |= PHYPWR_NORMAL_MASK;
  211. break;
  212. case TYPE_EXYNOS4X12:
  213. phypwr |= (PHYPWR_NORMAL_MASK_HSIC0 |
  214. PHYPWR_NORMAL_MASK_HSIC1 |
  215. PHYPWR_NORMAL_MASK_PHY1);
  216. case TYPE_EXYNOS4210:
  217. phypwr |= PHYPWR_NORMAL_MASK_PHY0;
  218. default:
  219. break;
  220. }
  221. /* Disable analog and otg block power */
  222. writel(phypwr, regs + SAMSUNG_PHYPWR);
  223. }
  224. /*
  225. * The function passed to the usb driver for phy initialization
  226. */
  227. static int samsung_usb2phy_init(struct usb_phy *phy)
  228. {
  229. struct samsung_usbphy *sphy;
  230. struct usb_bus *host = NULL;
  231. unsigned long flags;
  232. int ret = 0;
  233. sphy = phy_to_sphy(phy);
  234. host = phy->otg->host;
  235. /* Enable the phy clock */
  236. ret = clk_prepare_enable(sphy->clk);
  237. if (ret) {
  238. dev_err(sphy->dev, "%s: clk_prepare_enable failed\n", __func__);
  239. return ret;
  240. }
  241. spin_lock_irqsave(&sphy->lock, flags);
  242. if (host) {
  243. /* setting default phy-type for USB 2.0 */
  244. if (!strstr(dev_name(host->controller), "ehci") ||
  245. !strstr(dev_name(host->controller), "ohci"))
  246. samsung_usbphy_set_type(&sphy->phy, USB_PHY_TYPE_HOST);
  247. } else {
  248. samsung_usbphy_set_type(&sphy->phy, USB_PHY_TYPE_DEVICE);
  249. }
  250. /* Disable phy isolation */
  251. if (sphy->plat && sphy->plat->pmu_isolation)
  252. sphy->plat->pmu_isolation(false);
  253. else if (sphy->drv_data->set_isolation)
  254. sphy->drv_data->set_isolation(sphy, false);
  255. /* Selecting Host/OTG mode; After reset USB2.0PHY_CFG: HOST */
  256. samsung_usbphy_cfg_sel(sphy);
  257. /* Initialize usb phy registers */
  258. sphy->drv_data->phy_enable(sphy);
  259. spin_unlock_irqrestore(&sphy->lock, flags);
  260. /* Disable the phy clock */
  261. clk_disable_unprepare(sphy->clk);
  262. return ret;
  263. }
  264. /*
  265. * The function passed to the usb driver for phy shutdown
  266. */
  267. static void samsung_usb2phy_shutdown(struct usb_phy *phy)
  268. {
  269. struct samsung_usbphy *sphy;
  270. struct usb_bus *host = NULL;
  271. unsigned long flags;
  272. sphy = phy_to_sphy(phy);
  273. host = phy->otg->host;
  274. if (clk_prepare_enable(sphy->clk)) {
  275. dev_err(sphy->dev, "%s: clk_prepare_enable failed\n", __func__);
  276. return;
  277. }
  278. spin_lock_irqsave(&sphy->lock, flags);
  279. if (host) {
  280. /* setting default phy-type for USB 2.0 */
  281. if (!strstr(dev_name(host->controller), "ehci") ||
  282. !strstr(dev_name(host->controller), "ohci"))
  283. samsung_usbphy_set_type(&sphy->phy, USB_PHY_TYPE_HOST);
  284. } else {
  285. samsung_usbphy_set_type(&sphy->phy, USB_PHY_TYPE_DEVICE);
  286. }
  287. /* De-initialize usb phy registers */
  288. sphy->drv_data->phy_disable(sphy);
  289. /* Enable phy isolation */
  290. if (sphy->plat && sphy->plat->pmu_isolation)
  291. sphy->plat->pmu_isolation(true);
  292. else if (sphy->drv_data->set_isolation)
  293. sphy->drv_data->set_isolation(sphy, true);
  294. spin_unlock_irqrestore(&sphy->lock, flags);
  295. clk_disable_unprepare(sphy->clk);
  296. }
  297. static int samsung_usb2phy_probe(struct platform_device *pdev)
  298. {
  299. struct samsung_usbphy *sphy;
  300. struct usb_otg *otg;
  301. struct samsung_usbphy_data *pdata = pdev->dev.platform_data;
  302. const struct samsung_usbphy_drvdata *drv_data;
  303. struct device *dev = &pdev->dev;
  304. struct resource *phy_mem;
  305. void __iomem *phy_base;
  306. struct clk *clk;
  307. int ret;
  308. phy_mem = platform_get_resource(pdev, IORESOURCE_MEM, 0);
  309. phy_base = devm_ioremap_resource(dev, phy_mem);
  310. if (IS_ERR(phy_base))
  311. return PTR_ERR(phy_base);
  312. sphy = devm_kzalloc(dev, sizeof(*sphy), GFP_KERNEL);
  313. if (!sphy)
  314. return -ENOMEM;
  315. otg = devm_kzalloc(dev, sizeof(*otg), GFP_KERNEL);
  316. if (!otg)
  317. return -ENOMEM;
  318. drv_data = samsung_usbphy_get_driver_data(pdev);
  319. if (drv_data->cpu_type == TYPE_EXYNOS5250)
  320. clk = devm_clk_get(dev, "usbhost");
  321. else
  322. clk = devm_clk_get(dev, "otg");
  323. if (IS_ERR(clk)) {
  324. dev_err(dev, "Failed to get otg clock\n");
  325. return PTR_ERR(clk);
  326. }
  327. sphy->dev = dev;
  328. if (dev->of_node) {
  329. ret = samsung_usbphy_parse_dt(sphy);
  330. if (ret < 0)
  331. return ret;
  332. } else {
  333. if (!pdata) {
  334. dev_err(dev, "no platform data specified\n");
  335. return -EINVAL;
  336. }
  337. }
  338. sphy->plat = pdata;
  339. sphy->regs = phy_base;
  340. sphy->clk = clk;
  341. sphy->drv_data = drv_data;
  342. sphy->phy.dev = sphy->dev;
  343. sphy->phy.label = "samsung-usb2phy";
  344. sphy->phy.init = samsung_usb2phy_init;
  345. sphy->phy.shutdown = samsung_usb2phy_shutdown;
  346. sphy->ref_clk_freq = samsung_usbphy_get_refclk_freq(sphy);
  347. if (sphy->ref_clk_freq < 0)
  348. return -EINVAL;
  349. sphy->phy.otg = otg;
  350. sphy->phy.otg->phy = &sphy->phy;
  351. sphy->phy.otg->set_host = samsung_usbphy_set_host;
  352. spin_lock_init(&sphy->lock);
  353. platform_set_drvdata(pdev, sphy);
  354. return usb_add_phy(&sphy->phy, USB_PHY_TYPE_USB2);
  355. }
  356. static int samsung_usb2phy_remove(struct platform_device *pdev)
  357. {
  358. struct samsung_usbphy *sphy = platform_get_drvdata(pdev);
  359. usb_remove_phy(&sphy->phy);
  360. if (sphy->pmuregs)
  361. iounmap(sphy->pmuregs);
  362. if (sphy->sysreg)
  363. iounmap(sphy->sysreg);
  364. return 0;
  365. }
  366. static const struct samsung_usbphy_drvdata usb2phy_s3c64xx = {
  367. .cpu_type = TYPE_S3C64XX,
  368. .devphy_en_mask = S3C64XX_USBPHY_ENABLE,
  369. .rate_to_clksel = samsung_usbphy_rate_to_clksel_64xx,
  370. .set_isolation = NULL, /* TODO */
  371. .phy_enable = samsung_usb2phy_enable,
  372. .phy_disable = samsung_usb2phy_disable,
  373. };
  374. static const struct samsung_usbphy_drvdata usb2phy_exynos4 = {
  375. .cpu_type = TYPE_EXYNOS4210,
  376. .devphy_en_mask = EXYNOS_USBPHY_ENABLE,
  377. .hostphy_en_mask = EXYNOS_USBPHY_ENABLE,
  378. .rate_to_clksel = samsung_usbphy_rate_to_clksel_64xx,
  379. .set_isolation = samsung_usbphy_set_isolation_4210,
  380. .phy_enable = samsung_usb2phy_enable,
  381. .phy_disable = samsung_usb2phy_disable,
  382. };
  383. static const struct samsung_usbphy_drvdata usb2phy_exynos4x12 = {
  384. .cpu_type = TYPE_EXYNOS4X12,
  385. .devphy_en_mask = EXYNOS_USBPHY_ENABLE,
  386. .hostphy_en_mask = EXYNOS_USBPHY_ENABLE,
  387. .rate_to_clksel = samsung_usbphy_rate_to_clksel_4x12,
  388. .set_isolation = samsung_usbphy_set_isolation_4210,
  389. .phy_enable = samsung_usb2phy_enable,
  390. .phy_disable = samsung_usb2phy_disable,
  391. };
  392. static struct samsung_usbphy_drvdata usb2phy_exynos5 = {
  393. .cpu_type = TYPE_EXYNOS5250,
  394. .hostphy_en_mask = EXYNOS_USBPHY_ENABLE,
  395. .hostphy_reg_offset = EXYNOS_USBHOST_PHY_CTRL_OFFSET,
  396. .rate_to_clksel = samsung_usbphy_rate_to_clksel_4x12,
  397. .set_isolation = samsung_usbphy_set_isolation_4210,
  398. .phy_enable = samsung_exynos5_usb2phy_enable,
  399. .phy_disable = samsung_exynos5_usb2phy_disable,
  400. };
  401. #ifdef CONFIG_OF
  402. static const struct of_device_id samsung_usbphy_dt_match[] = {
  403. {
  404. .compatible = "samsung,s3c64xx-usb2phy",
  405. .data = &usb2phy_s3c64xx,
  406. }, {
  407. .compatible = "samsung,exynos4210-usb2phy",
  408. .data = &usb2phy_exynos4,
  409. }, {
  410. .compatible = "samsung,exynos4x12-usb2phy",
  411. .data = &usb2phy_exynos4x12,
  412. }, {
  413. .compatible = "samsung,exynos5250-usb2phy",
  414. .data = &usb2phy_exynos5
  415. },
  416. {},
  417. };
  418. MODULE_DEVICE_TABLE(of, samsung_usbphy_dt_match);
  419. #endif
  420. static struct platform_device_id samsung_usbphy_driver_ids[] = {
  421. {
  422. .name = "s3c64xx-usb2phy",
  423. .driver_data = (unsigned long)&usb2phy_s3c64xx,
  424. }, {
  425. .name = "exynos4210-usb2phy",
  426. .driver_data = (unsigned long)&usb2phy_exynos4,
  427. }, {
  428. .name = "exynos4x12-usb2phy",
  429. .driver_data = (unsigned long)&usb2phy_exynos4x12,
  430. }, {
  431. .name = "exynos5250-usb2phy",
  432. .driver_data = (unsigned long)&usb2phy_exynos5,
  433. },
  434. {},
  435. };
  436. MODULE_DEVICE_TABLE(platform, samsung_usbphy_driver_ids);
  437. static struct platform_driver samsung_usb2phy_driver = {
  438. .probe = samsung_usb2phy_probe,
  439. .remove = samsung_usb2phy_remove,
  440. .id_table = samsung_usbphy_driver_ids,
  441. .driver = {
  442. .name = "samsung-usb2phy",
  443. .owner = THIS_MODULE,
  444. .of_match_table = of_match_ptr(samsung_usbphy_dt_match),
  445. },
  446. };
  447. module_platform_driver(samsung_usb2phy_driver);
  448. MODULE_DESCRIPTION("Samsung USB 2.0 phy controller");
  449. MODULE_AUTHOR("Praveen Paneri <p.paneri@samsung.com>");
  450. MODULE_LICENSE("GPL");
  451. MODULE_ALIAS("platform:samsung-usb2phy");