blob: 03180c06bfca088968dd5e345642797a0b722bba [file] [log] [blame]
Vivek Gautamdc2377d2013-03-14 15:59:10 +05301/* 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
21#include <linux/module.h>
22#include <linux/platform_device.h>
23#include <linux/clk.h>
24#include <linux/delay.h>
25#include <linux/device.h>
26#include <linux/err.h>
27#include <linux/io.h>
28#include <linux/of.h>
29#include <linux/usb/otg.h>
30#include <linux/usb/samsung_usb_phy.h>
31#include <linux/platform_data/samsung-usbphy.h>
32
33#include "phy-samsung-usb.h"
34
35static int samsung_usbphy_set_host(struct usb_otg *otg, struct usb_bus *host)
36{
37 if (!otg)
38 return -ENODEV;
39
40 if (!otg->host)
41 otg->host = host;
42
43 return 0;
44}
45
Felipe Balbib64a1592013-03-19 10:14:35 +020046static bool exynos5_phyhost_is_on(void __iomem *regs)
Vivek Gautamdc2377d2013-03-14 15:59:10 +053047{
48 u32 reg;
49
50 reg = readl(regs + EXYNOS5_PHY_HOST_CTRL0);
51
52 return !(reg & HOST_CTRL0_SIDDQ);
53}
54
55static void samsung_exynos5_usb2phy_enable(struct samsung_usbphy *sphy)
56{
57 void __iomem *regs = sphy->regs;
58 u32 phyclk = sphy->ref_clk_freq;
59 u32 phyhost;
60 u32 phyotg;
61 u32 phyhsic;
62 u32 ehcictrl;
63 u32 ohcictrl;
64
65 /*
66 * phy_usage helps in keeping usage count for phy
67 * so that the first consumer enabling the phy is also
68 * the last consumer to disable it.
69 */
70
71 atomic_inc(&sphy->phy_usage);
72
73 if (exynos5_phyhost_is_on(regs)) {
74 dev_info(sphy->dev, "Already power on PHY\n");
75 return;
76 }
77
78 /* Host configuration */
79 phyhost = readl(regs + EXYNOS5_PHY_HOST_CTRL0);
80
81 /* phy reference clock configuration */
82 phyhost &= ~HOST_CTRL0_FSEL_MASK;
83 phyhost |= HOST_CTRL0_FSEL(phyclk);
84
85 /* host phy reset */
86 phyhost &= ~(HOST_CTRL0_PHYSWRST |
87 HOST_CTRL0_PHYSWRSTALL |
88 HOST_CTRL0_SIDDQ |
89 /* Enable normal mode of operation */
90 HOST_CTRL0_FORCESUSPEND |
91 HOST_CTRL0_FORCESLEEP);
92
93 /* Link reset */
94 phyhost |= (HOST_CTRL0_LINKSWRST |
95 HOST_CTRL0_UTMISWRST |
96 /* COMMON Block configuration during suspend */
97 HOST_CTRL0_COMMONON_N);
98 writel(phyhost, regs + EXYNOS5_PHY_HOST_CTRL0);
99 udelay(10);
100 phyhost &= ~(HOST_CTRL0_LINKSWRST |
101 HOST_CTRL0_UTMISWRST);
102 writel(phyhost, regs + EXYNOS5_PHY_HOST_CTRL0);
103
104 /* OTG configuration */
105 phyotg = readl(regs + EXYNOS5_PHY_OTG_SYS);
106
107 /* phy reference clock configuration */
108 phyotg &= ~OTG_SYS_FSEL_MASK;
109 phyotg |= OTG_SYS_FSEL(phyclk);
110
111 /* Enable normal mode of operation */
112 phyotg &= ~(OTG_SYS_FORCESUSPEND |
113 OTG_SYS_SIDDQ_UOTG |
114 OTG_SYS_FORCESLEEP |
115 OTG_SYS_REFCLKSEL_MASK |
116 /* COMMON Block configuration during suspend */
117 OTG_SYS_COMMON_ON);
118
119 /* OTG phy & link reset */
120 phyotg |= (OTG_SYS_PHY0_SWRST |
121 OTG_SYS_LINKSWRST_UOTG |
122 OTG_SYS_PHYLINK_SWRESET |
123 OTG_SYS_OTGDISABLE |
124 /* Set phy refclk */
125 OTG_SYS_REFCLKSEL_CLKCORE);
126
127 writel(phyotg, regs + EXYNOS5_PHY_OTG_SYS);
128 udelay(10);
129 phyotg &= ~(OTG_SYS_PHY0_SWRST |
130 OTG_SYS_LINKSWRST_UOTG |
131 OTG_SYS_PHYLINK_SWRESET);
132 writel(phyotg, regs + EXYNOS5_PHY_OTG_SYS);
133
134 /* HSIC phy configuration */
135 phyhsic = (HSIC_CTRL_REFCLKDIV_12 |
136 HSIC_CTRL_REFCLKSEL |
137 HSIC_CTRL_PHYSWRST);
138 writel(phyhsic, regs + EXYNOS5_PHY_HSIC_CTRL1);
139 writel(phyhsic, regs + EXYNOS5_PHY_HSIC_CTRL2);
140 udelay(10);
141 phyhsic &= ~HSIC_CTRL_PHYSWRST;
142 writel(phyhsic, regs + EXYNOS5_PHY_HSIC_CTRL1);
143 writel(phyhsic, regs + EXYNOS5_PHY_HSIC_CTRL2);
144
145 udelay(80);
146
147 /* enable EHCI DMA burst */
148 ehcictrl = readl(regs + EXYNOS5_PHY_HOST_EHCICTRL);
149 ehcictrl |= (HOST_EHCICTRL_ENAINCRXALIGN |
150 HOST_EHCICTRL_ENAINCR4 |
151 HOST_EHCICTRL_ENAINCR8 |
152 HOST_EHCICTRL_ENAINCR16);
153 writel(ehcictrl, regs + EXYNOS5_PHY_HOST_EHCICTRL);
154
155 /* set ohci_suspend_on_n */
156 ohcictrl = readl(regs + EXYNOS5_PHY_HOST_OHCICTRL);
157 ohcictrl |= HOST_OHCICTRL_SUSPLGCY;
158 writel(ohcictrl, regs + EXYNOS5_PHY_HOST_OHCICTRL);
159}
160
161static void samsung_usb2phy_enable(struct samsung_usbphy *sphy)
162{
163 void __iomem *regs = sphy->regs;
164 u32 phypwr;
165 u32 phyclk;
166 u32 rstcon;
167
168 /* set clock frequency for PLL */
169 phyclk = sphy->ref_clk_freq;
170 phypwr = readl(regs + SAMSUNG_PHYPWR);
171 rstcon = readl(regs + SAMSUNG_RSTCON);
172
173 switch (sphy->drv_data->cpu_type) {
174 case TYPE_S3C64XX:
175 phyclk &= ~PHYCLK_COMMON_ON_N;
176 phypwr &= ~PHYPWR_NORMAL_MASK;
177 rstcon |= RSTCON_SWRST;
178 break;
179 case TYPE_EXYNOS4210:
Tomasz Figa1b635f02013-05-16 11:57:11 +0200180 case TYPE_EXYNOS4X12:
Vivek Gautamdc2377d2013-03-14 15:59:10 +0530181 phypwr &= ~PHYPWR_NORMAL_MASK_PHY0;
182 rstcon |= RSTCON_SWRST;
183 default:
184 break;
185 }
186
187 writel(phyclk, regs + SAMSUNG_PHYCLK);
188 /* Configure PHY0 for normal operation*/
189 writel(phypwr, regs + SAMSUNG_PHYPWR);
190 /* reset all ports of PHY and Link */
191 writel(rstcon, regs + SAMSUNG_RSTCON);
192 udelay(10);
193 rstcon &= ~RSTCON_SWRST;
194 writel(rstcon, regs + SAMSUNG_RSTCON);
195}
196
197static void samsung_exynos5_usb2phy_disable(struct samsung_usbphy *sphy)
198{
199 void __iomem *regs = sphy->regs;
200 u32 phyhost;
201 u32 phyotg;
202 u32 phyhsic;
203
204 if (atomic_dec_return(&sphy->phy_usage) > 0) {
205 dev_info(sphy->dev, "still being used\n");
206 return;
207 }
208
209 phyhsic = (HSIC_CTRL_REFCLKDIV_12 |
210 HSIC_CTRL_REFCLKSEL |
211 HSIC_CTRL_SIDDQ |
212 HSIC_CTRL_FORCESLEEP |
213 HSIC_CTRL_FORCESUSPEND);
214 writel(phyhsic, regs + EXYNOS5_PHY_HSIC_CTRL1);
215 writel(phyhsic, regs + EXYNOS5_PHY_HSIC_CTRL2);
216
217 phyhost = readl(regs + EXYNOS5_PHY_HOST_CTRL0);
218 phyhost |= (HOST_CTRL0_SIDDQ |
219 HOST_CTRL0_FORCESUSPEND |
220 HOST_CTRL0_FORCESLEEP |
221 HOST_CTRL0_PHYSWRST |
222 HOST_CTRL0_PHYSWRSTALL);
223 writel(phyhost, regs + EXYNOS5_PHY_HOST_CTRL0);
224
225 phyotg = readl(regs + EXYNOS5_PHY_OTG_SYS);
226 phyotg |= (OTG_SYS_FORCESUSPEND |
227 OTG_SYS_SIDDQ_UOTG |
228 OTG_SYS_FORCESLEEP);
229 writel(phyotg, regs + EXYNOS5_PHY_OTG_SYS);
230}
231
232static void samsung_usb2phy_disable(struct samsung_usbphy *sphy)
233{
234 void __iomem *regs = sphy->regs;
235 u32 phypwr;
236
237 phypwr = readl(regs + SAMSUNG_PHYPWR);
238
239 switch (sphy->drv_data->cpu_type) {
240 case TYPE_S3C64XX:
241 phypwr |= PHYPWR_NORMAL_MASK;
242 break;
243 case TYPE_EXYNOS4210:
Tomasz Figa1b635f02013-05-16 11:57:11 +0200244 case TYPE_EXYNOS4X12:
Vivek Gautamdc2377d2013-03-14 15:59:10 +0530245 phypwr |= PHYPWR_NORMAL_MASK_PHY0;
246 default:
247 break;
248 }
249
250 /* Disable analog and otg block power */
251 writel(phypwr, regs + SAMSUNG_PHYPWR);
252}
253
254/*
255 * The function passed to the usb driver for phy initialization
256 */
257static int samsung_usb2phy_init(struct usb_phy *phy)
258{
259 struct samsung_usbphy *sphy;
260 struct usb_bus *host = NULL;
261 unsigned long flags;
262 int ret = 0;
263
264 sphy = phy_to_sphy(phy);
265
266 host = phy->otg->host;
267
268 /* Enable the phy clock */
269 ret = clk_prepare_enable(sphy->clk);
270 if (ret) {
271 dev_err(sphy->dev, "%s: clk_prepare_enable failed\n", __func__);
272 return ret;
273 }
274
275 spin_lock_irqsave(&sphy->lock, flags);
276
277 if (host) {
278 /* setting default phy-type for USB 2.0 */
279 if (!strstr(dev_name(host->controller), "ehci") ||
280 !strstr(dev_name(host->controller), "ohci"))
281 samsung_usbphy_set_type(&sphy->phy, USB_PHY_TYPE_HOST);
282 } else {
283 samsung_usbphy_set_type(&sphy->phy, USB_PHY_TYPE_DEVICE);
284 }
285
286 /* Disable phy isolation */
287 if (sphy->plat && sphy->plat->pmu_isolation)
288 sphy->plat->pmu_isolation(false);
Tomasz Figa3f339072013-05-16 11:57:09 +0200289 else if (sphy->drv_data->set_isolation)
290 sphy->drv_data->set_isolation(sphy, false);
Vivek Gautamdc2377d2013-03-14 15:59:10 +0530291
292 /* Selecting Host/OTG mode; After reset USB2.0PHY_CFG: HOST */
293 samsung_usbphy_cfg_sel(sphy);
294
295 /* Initialize usb phy registers */
Tomasz Figa84035f02013-05-16 11:57:10 +0200296 sphy->drv_data->phy_enable(sphy);
Vivek Gautamdc2377d2013-03-14 15:59:10 +0530297
298 spin_unlock_irqrestore(&sphy->lock, flags);
299
300 /* Disable the phy clock */
301 clk_disable_unprepare(sphy->clk);
302
303 return ret;
304}
305
306/*
307 * The function passed to the usb driver for phy shutdown
308 */
309static void samsung_usb2phy_shutdown(struct usb_phy *phy)
310{
311 struct samsung_usbphy *sphy;
312 struct usb_bus *host = NULL;
313 unsigned long flags;
314
315 sphy = phy_to_sphy(phy);
316
317 host = phy->otg->host;
318
319 if (clk_prepare_enable(sphy->clk)) {
320 dev_err(sphy->dev, "%s: clk_prepare_enable failed\n", __func__);
321 return;
322 }
323
324 spin_lock_irqsave(&sphy->lock, flags);
325
326 if (host) {
327 /* setting default phy-type for USB 2.0 */
328 if (!strstr(dev_name(host->controller), "ehci") ||
329 !strstr(dev_name(host->controller), "ohci"))
330 samsung_usbphy_set_type(&sphy->phy, USB_PHY_TYPE_HOST);
331 } else {
332 samsung_usbphy_set_type(&sphy->phy, USB_PHY_TYPE_DEVICE);
333 }
334
335 /* De-initialize usb phy registers */
Tomasz Figa84035f02013-05-16 11:57:10 +0200336 sphy->drv_data->phy_disable(sphy);
Vivek Gautamdc2377d2013-03-14 15:59:10 +0530337
338 /* Enable phy isolation */
339 if (sphy->plat && sphy->plat->pmu_isolation)
340 sphy->plat->pmu_isolation(true);
Tomasz Figa3f339072013-05-16 11:57:09 +0200341 else if (sphy->drv_data->set_isolation)
342 sphy->drv_data->set_isolation(sphy, true);
Vivek Gautamdc2377d2013-03-14 15:59:10 +0530343
344 spin_unlock_irqrestore(&sphy->lock, flags);
345
346 clk_disable_unprepare(sphy->clk);
347}
348
349static int samsung_usb2phy_probe(struct platform_device *pdev)
350{
351 struct samsung_usbphy *sphy;
352 struct usb_otg *otg;
353 struct samsung_usbphy_data *pdata = pdev->dev.platform_data;
354 const struct samsung_usbphy_drvdata *drv_data;
355 struct device *dev = &pdev->dev;
356 struct resource *phy_mem;
357 void __iomem *phy_base;
358 struct clk *clk;
359 int ret;
360
361 phy_mem = platform_get_resource(pdev, IORESOURCE_MEM, 0);
Vivek Gautamdc2377d2013-03-14 15:59:10 +0530362 phy_base = devm_ioremap_resource(dev, phy_mem);
363 if (IS_ERR(phy_base))
364 return PTR_ERR(phy_base);
365
366 sphy = devm_kzalloc(dev, sizeof(*sphy), GFP_KERNEL);
367 if (!sphy)
368 return -ENOMEM;
369
370 otg = devm_kzalloc(dev, sizeof(*otg), GFP_KERNEL);
371 if (!otg)
372 return -ENOMEM;
373
374 drv_data = samsung_usbphy_get_driver_data(pdev);
375
376 if (drv_data->cpu_type == TYPE_EXYNOS5250)
377 clk = devm_clk_get(dev, "usbhost");
378 else
379 clk = devm_clk_get(dev, "otg");
380
381 if (IS_ERR(clk)) {
382 dev_err(dev, "Failed to get otg clock\n");
383 return PTR_ERR(clk);
384 }
385
386 sphy->dev = dev;
387
388 if (dev->of_node) {
389 ret = samsung_usbphy_parse_dt(sphy);
390 if (ret < 0)
391 return ret;
392 } else {
393 if (!pdata) {
394 dev_err(dev, "no platform data specified\n");
395 return -EINVAL;
396 }
397 }
398
399 sphy->plat = pdata;
400 sphy->regs = phy_base;
401 sphy->clk = clk;
402 sphy->drv_data = drv_data;
403 sphy->phy.dev = sphy->dev;
404 sphy->phy.label = "samsung-usb2phy";
405 sphy->phy.init = samsung_usb2phy_init;
406 sphy->phy.shutdown = samsung_usb2phy_shutdown;
Tomasz Figa0aa823a2013-05-16 11:57:08 +0200407
408 sphy->ref_clk_freq = samsung_usbphy_get_refclk_freq(sphy);
409 if (sphy->ref_clk_freq < 0)
410 return -EINVAL;
Vivek Gautamdc2377d2013-03-14 15:59:10 +0530411
412 sphy->phy.otg = otg;
413 sphy->phy.otg->phy = &sphy->phy;
414 sphy->phy.otg->set_host = samsung_usbphy_set_host;
415
416 spin_lock_init(&sphy->lock);
417
418 platform_set_drvdata(pdev, sphy);
419
420 return usb_add_phy(&sphy->phy, USB_PHY_TYPE_USB2);
421}
422
423static int samsung_usb2phy_remove(struct platform_device *pdev)
424{
425 struct samsung_usbphy *sphy = platform_get_drvdata(pdev);
426
427 usb_remove_phy(&sphy->phy);
428
429 if (sphy->pmuregs)
430 iounmap(sphy->pmuregs);
431 if (sphy->sysreg)
432 iounmap(sphy->sysreg);
433
434 return 0;
435}
436
437static const struct samsung_usbphy_drvdata usb2phy_s3c64xx = {
438 .cpu_type = TYPE_S3C64XX,
439 .devphy_en_mask = S3C64XX_USBPHY_ENABLE,
Tomasz Figa0aa823a2013-05-16 11:57:08 +0200440 .rate_to_clksel = samsung_usbphy_rate_to_clksel_64xx,
Tomasz Figa3f339072013-05-16 11:57:09 +0200441 .set_isolation = NULL, /* TODO */
Tomasz Figa84035f02013-05-16 11:57:10 +0200442 .phy_enable = samsung_usb2phy_enable,
443 .phy_disable = samsung_usb2phy_disable,
Vivek Gautamdc2377d2013-03-14 15:59:10 +0530444};
445
446static const struct samsung_usbphy_drvdata usb2phy_exynos4 = {
447 .cpu_type = TYPE_EXYNOS4210,
448 .devphy_en_mask = EXYNOS_USBPHY_ENABLE,
449 .hostphy_en_mask = EXYNOS_USBPHY_ENABLE,
Tomasz Figa0aa823a2013-05-16 11:57:08 +0200450 .rate_to_clksel = samsung_usbphy_rate_to_clksel_64xx,
Tomasz Figa3f339072013-05-16 11:57:09 +0200451 .set_isolation = samsung_usbphy_set_isolation_4210,
Tomasz Figa84035f02013-05-16 11:57:10 +0200452 .phy_enable = samsung_usb2phy_enable,
453 .phy_disable = samsung_usb2phy_disable,
Vivek Gautamdc2377d2013-03-14 15:59:10 +0530454};
455
Tomasz Figa1b635f02013-05-16 11:57:11 +0200456static const struct samsung_usbphy_drvdata usb2phy_exynos4x12 = {
457 .cpu_type = TYPE_EXYNOS4X12,
458 .devphy_en_mask = EXYNOS_USBPHY_ENABLE,
459 .hostphy_en_mask = EXYNOS_USBPHY_ENABLE,
460 .rate_to_clksel = samsung_usbphy_rate_to_clksel_4x12,
461 .set_isolation = samsung_usbphy_set_isolation_4210,
462 .phy_enable = samsung_usb2phy_enable,
463 .phy_disable = samsung_usb2phy_disable,
464};
465
Vivek Gautamdc2377d2013-03-14 15:59:10 +0530466static struct samsung_usbphy_drvdata usb2phy_exynos5 = {
467 .cpu_type = TYPE_EXYNOS5250,
468 .hostphy_en_mask = EXYNOS_USBPHY_ENABLE,
469 .hostphy_reg_offset = EXYNOS_USBHOST_PHY_CTRL_OFFSET,
Tomasz Figa0aa823a2013-05-16 11:57:08 +0200470 .rate_to_clksel = samsung_usbphy_rate_to_clksel_4x12,
Tomasz Figa3f339072013-05-16 11:57:09 +0200471 .set_isolation = samsung_usbphy_set_isolation_4210,
Tomasz Figa84035f02013-05-16 11:57:10 +0200472 .phy_enable = samsung_exynos5_usb2phy_enable,
473 .phy_disable = samsung_exynos5_usb2phy_disable,
Vivek Gautamdc2377d2013-03-14 15:59:10 +0530474};
475
476#ifdef CONFIG_OF
477static const struct of_device_id samsung_usbphy_dt_match[] = {
478 {
479 .compatible = "samsung,s3c64xx-usb2phy",
480 .data = &usb2phy_s3c64xx,
481 }, {
482 .compatible = "samsung,exynos4210-usb2phy",
483 .data = &usb2phy_exynos4,
484 }, {
Tomasz Figa1b635f02013-05-16 11:57:11 +0200485 .compatible = "samsung,exynos4x12-usb2phy",
486 .data = &usb2phy_exynos4x12,
487 }, {
Vivek Gautamdc2377d2013-03-14 15:59:10 +0530488 .compatible = "samsung,exynos5250-usb2phy",
489 .data = &usb2phy_exynos5
490 },
491 {},
492};
493MODULE_DEVICE_TABLE(of, samsung_usbphy_dt_match);
494#endif
495
496static struct platform_device_id samsung_usbphy_driver_ids[] = {
497 {
498 .name = "s3c64xx-usb2phy",
499 .driver_data = (unsigned long)&usb2phy_s3c64xx,
500 }, {
501 .name = "exynos4210-usb2phy",
502 .driver_data = (unsigned long)&usb2phy_exynos4,
503 }, {
Tomasz Figa1b635f02013-05-16 11:57:11 +0200504 .name = "exynos4x12-usb2phy",
505 .driver_data = (unsigned long)&usb2phy_exynos4x12,
506 }, {
Vivek Gautamdc2377d2013-03-14 15:59:10 +0530507 .name = "exynos5250-usb2phy",
508 .driver_data = (unsigned long)&usb2phy_exynos5,
509 },
510 {},
511};
512
513MODULE_DEVICE_TABLE(platform, samsung_usbphy_driver_ids);
514
515static struct platform_driver samsung_usb2phy_driver = {
516 .probe = samsung_usb2phy_probe,
517 .remove = samsung_usb2phy_remove,
518 .id_table = samsung_usbphy_driver_ids,
519 .driver = {
520 .name = "samsung-usb2phy",
521 .owner = THIS_MODULE,
522 .of_match_table = of_match_ptr(samsung_usbphy_dt_match),
523 },
524};
525
526module_platform_driver(samsung_usb2phy_driver);
527
528MODULE_DESCRIPTION("Samsung USB 2.0 phy controller");
529MODULE_AUTHOR("Praveen Paneri <p.paneri@samsung.com>");
530MODULE_LICENSE("GPL");
531MODULE_ALIAS("platform:samsung-usb2phy");