aboutsummaryrefslogtreecommitdiff
path: root/arch/arm/mach-exynos/setup-usb-phy.c
blob: 78100981b0be7ab085de5ec0183c8d7778717acf (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
/*
 * Copyright (C) 2011 Samsung Electronics Co.Ltd
 * Author: Joonyoung Shim <jy0922.shim@samsung.com>
 *
 *  This program is free software; you can redistribute  it and/or modify it
 *  under  the terms of  the GNU General  Public License as published by the
 *  Free Software Foundation;  either version 2 of the  License, or (at your
 *  option) any later version.
 *
 */

#include <linux/clk.h>
#include <linux/delay.h>
#include <linux/err.h>
#include <linux/io.h>
#include <linux/platform_device.h>
#include <linux/usb/samsung_usb_phy.h>
#include <linux/platform_data/samsung-usbphy.h>
#include <mach/regs-pmu.h>
#include <mach/regs-usb-phy.h>
#include <plat/cpu.h>
#include <plat/map-base.h>
#include <plat/usb-phy.h>

#define PHY_ENABLE	1
#define PHY_DISABLE	0
#define EXYNOS5_USB_CFG		(S3C_VA_SYS + 0x230)

static atomic_t host_usage;

static int exynos4_usb_host_phy_is_on(void)
{
	if (soc_is_exynos5250()) {
		return (readl(EXYNOS5_PHY_HOST_CTRL0) &
				HOST_CTRL0_PHYSWRSTALL) ? 0 : 1;
	} else {
		return (readl(EXYNOS4_PHYPWR) &
				PHY1_STD_ANALOG_POWERDOWN) ? 0 : 1;
	}
}

static void exynos_usb_mux_change(struct platform_device *pdev, int val)
{
	u32 is_host;
	if (soc_is_exynos5250()) {
		is_host = readl(EXYNOS5_USB_CFG);
		writel(val, EXYNOS5_USB_CFG);
	}
	if (is_host != val)
		dev_dbg(&pdev->dev, "Change USB MUX from %s to %s",
		is_host ? "Host" : "Device", val ? "Host" : "Device");
}

static struct clk *exynos_usb_clock_enable(struct platform_device *pdev)
{
	struct clk *usb_clk = NULL;
	int err = 0;

	if (soc_is_exynos5250())
		usb_clk = clk_get(&pdev->dev, "usbhost");
	else
		usb_clk = clk_get(&pdev->dev, "otg");
	if (IS_ERR(usb_clk)) {
		dev_err(&pdev->dev, "Failed to get otg clock\n");
		return NULL;
	}

	err = clk_enable(usb_clk);
	if (err) {
		clk_put(usb_clk);
		return NULL;
	}
	return usb_clk;
}

static int exynos4210_usb_phy_clkset(struct platform_device *pdev)
{
	struct clk *xusbxti_clk;
	u32 phyclk = 0;

	if (soc_is_exynos5250())
		xusbxti_clk = clk_get(&pdev->dev, "ext_xtal");
	else
		xusbxti_clk = clk_get(&pdev->dev, "xusbxti");

	if (xusbxti_clk && !IS_ERR(xusbxti_clk)) {
		if (soc_is_exynos4210()) {
			/* set clock frequency for PLL */
			phyclk = readl(EXYNOS4_PHYCLK) & ~EXYNOS4210_CLKSEL_MASK;

			switch (clk_get_rate(xusbxti_clk)) {
			case 12 * MHZ:
				phyclk |= EXYNOS4210_CLKSEL_12M;
				break;
			case 48 * MHZ:
				phyclk |= EXYNOS4210_CLKSEL_48M;
				break;
			default:
			case 24 * MHZ:
				phyclk |= EXYNOS4210_CLKSEL_24M;
				break;
			}
			writel(phyclk, EXYNOS4_PHYCLK);
		} else if (soc_is_exynos4212() || soc_is_exynos4412()) {
			/* set clock frequency for PLL */
			phyclk = readl(EXYNOS4_PHYCLK) & ~EXYNOS4X12_CLKSEL_MASK;

			switch (clk_get_rate(xusbxti_clk)) {
			case 9600 * KHZ:
				phyclk |= EXYNOS4X12_CLKSEL_9600K;
				break;
			case 10 * MHZ:
				phyclk |= EXYNOS4X12_CLKSEL_10M;
				break;
			case 12 * MHZ:
				phyclk |= EXYNOS4X12_CLKSEL_12M;
				break;
			case 19200 * KHZ:
				phyclk |= EXYNOS4X12_CLKSEL_19200K;
				break;
			case 20 * MHZ:
				phyclk |= EXYNOS4X12_CLKSEL_20M;
				break;
			default:
			case 24 * MHZ:
				/* default reference clock */
				phyclk |= EXYNOS4X12_CLKSEL_24M;
				break;
			}
			writel(phyclk, EXYNOS4_PHYCLK);
		} else if (soc_is_exynos5250()) {
			/* set clock frequency for PLL */
			switch (clk_get_rate(xusbxti_clk)) {
			case 96 * 100000:
				phyclk |= EXYNOS5_CLKSEL_9600K;
				break;
			case 10 * MHZ:
				phyclk |= EXYNOS5_CLKSEL_10M;
				break;
			case 12 * MHZ:
				phyclk |= EXYNOS5_CLKSEL_12M;
				break;
			case 192 * 100000:
				phyclk |= EXYNOS5_CLKSEL_19200K;
				break;
			case 20 * MHZ:
				phyclk |= EXYNOS5_CLKSEL_20M;
				break;
			case 50 * MHZ:
				phyclk |= EXYNOS5_CLKSEL_50M;
				break;
			case 24 * MHZ:
			default:
				/* default reference clock */
				phyclk |= EXYNOS5_CLKSEL_24M;
				break;
			}
		}
		clk_put(xusbxti_clk);
	}
	return phyclk;
}
#if 0
static void exynos_usb_phy_control(enum usb_phy_type phy_type , int on)
{
	if (soc_is_exynos5250()) {
		if (phy_type & USB_PHY)
			writel(on, S5P_USBHOST_PHY_CONTROL);
	}
}
#endif
static int exynos4210_usb_phy0_init(struct platform_device *pdev)
{
	u32 rstcon;

	writel(readl(S5P_USBDEVICE_PHY_CONTROL) | S5P_USBDEVICE_PHY_ENABLE,
			S5P_USBDEVICE_PHY_CONTROL);

	exynos4210_usb_phy_clkset(pdev);

	/* set to normal PHY0 */
	writel((readl(EXYNOS4_PHYPWR) & ~PHY0_NORMAL_MASK), EXYNOS4_PHYPWR);

	/* reset PHY0 and Link */
	rstcon = readl(EXYNOS4_RSTCON) | PHY0_SWRST_MASK;
	writel(rstcon, EXYNOS4_RSTCON);
	udelay(10);

	rstcon &= ~PHY0_SWRST_MASK;
	writel(rstcon, EXYNOS4_RSTCON);

	return 0;
}

static int exynos4210_usb_phy0_exit(struct platform_device *pdev)
{
	writel((readl(EXYNOS4_PHYPWR) | PHY0_ANALOG_POWERDOWN |
				PHY0_OTG_DISABLE), EXYNOS4_PHYPWR);

	writel(readl(S5P_USBDEVICE_PHY_CONTROL) & ~S5P_USBDEVICE_PHY_ENABLE,
			S5P_USBDEVICE_PHY_CONTROL);

	return 0;
}

static int exynos4210_usb_phy1_init(struct platform_device *pdev)
{
	struct clk *otg_clk;
	u32 rstcon;

	atomic_inc(&host_usage);

	otg_clk = exynos_usb_clock_enable(pdev);
	if (otg_clk == NULL)
		dev_err(&pdev->dev, "Failed to enable otg clock\n");

	if (exynos4_usb_host_phy_is_on())
		return 0;

	writel(readl(S5P_USBHOST_PHY_CONTROL) | S5P_USBHOST_PHY_ENABLE,
			S5P_USBHOST_PHY_CONTROL);

	exynos4210_usb_phy_clkset(pdev);

	/* floating prevention logic: disable */
	writel((readl(EXYNOS4_PHY1CON) | FPENABLEN), EXYNOS4_PHY1CON);

	/* set to normal HSIC 0 and 1 of PHY1 */
	writel((readl(EXYNOS4_PHYPWR) & ~PHY1_HSIC_NORMAL_MASK),
			EXYNOS4_PHYPWR);

	/* set to normal standard USB of PHY1 */
	writel((readl(EXYNOS4_PHYPWR) & ~PHY1_STD_NORMAL_MASK), EXYNOS4_PHYPWR);

	/* reset all ports of both PHY and Link */
	rstcon = readl(EXYNOS4_RSTCON) | HOST_LINK_PORT_SWRST_MASK |
		PHY1_SWRST_MASK;
	writel(rstcon, EXYNOS4_RSTCON);
	udelay(10);

	rstcon &= ~(HOST_LINK_PORT_SWRST_MASK | PHY1_SWRST_MASK);
	writel(rstcon, EXYNOS4_RSTCON);
	udelay(80);

	clk_disable(otg_clk);
	clk_put(otg_clk);

	return 0;
}

static int exynos4210_usb_phy1_exit(struct platform_device *pdev)
{
	struct clk *otg_clk;

	if (atomic_dec_return(&host_usage) > 0)
		return 0;

	otg_clk = exynos_usb_clock_enable(pdev);
	if (otg_clk == NULL)
		dev_err(&pdev->dev, "Failed to enable otg clock\n");

	writel((readl(EXYNOS4_PHYPWR) | PHY1_STD_ANALOG_POWERDOWN),
			EXYNOS4_PHYPWR);

	writel(readl(S5P_USBHOST_PHY_CONTROL) & ~S5P_USBHOST_PHY_ENABLE,
			S5P_USBHOST_PHY_CONTROL);

	clk_disable(otg_clk);
	clk_put(otg_clk);

	return 0;
}


int s5p_usb_phy_init(struct platform_device *pdev, int type)
{
	if (type == USB_PHY_TYPE_DEVICE)
		return exynos4210_usb_phy0_init(pdev);
	else if (type == USB_PHY_TYPE_HOST) {
			return exynos4210_usb_phy1_init(pdev);
	}

	return -EINVAL;
}

int s5p_usb_phy_exit(struct platform_device *pdev, int type)
{

	if (type == USB_PHY_TYPE_DEVICE)
		return exynos4210_usb_phy0_exit(pdev);
	else if (type == USB_PHY_TYPE_HOST) {
			return exynos4210_usb_phy1_exit(pdev);
	}
	return -EINVAL;
}

 void s5p_usb_phy_pmu_isolation(int on, int type)
 {
	if (type == USB_PHY_TYPE_HOST) {
		if (on)
			writel(readl(S5P_USBHOST_PHY_CONTROL)
				& ~S5P_USBHOST_PHY_ENABLE,
					S5P_USBHOST_PHY_CONTROL);
		else
			writel(readl(S5P_USBHOST_PHY_CONTROL)
				| S5P_USBHOST_PHY_ENABLE,
					S5P_USBHOST_PHY_CONTROL);
	}else if(type == USB_PHY_TYPE_DRD) {
		if (on)
			writel(readl(S5P_USBDRD_PHY_CONTROL)
				& ~S5P_USBDRD_PHY_ENABLE,
					S5P_USBDRD_PHY_CONTROL);
		else
			writel(readl(S5P_USBDRD_PHY_CONTROL)
                                 | S5P_USBDRD_PHY_ENABLE,
                                         S5P_USBDRD_PHY_CONTROL);
	} else {
		if (on)
			writel(readl(S5P_USBDEVICE_PHY_CONTROL)
				& ~S5P_USBDEVICE_PHY_ENABLE,
					S5P_USBDEVICE_PHY_CONTROL);
		else
			writel(readl(S5P_USBDEVICE_PHY_CONTROL)
				| S5P_USBDEVICE_PHY_ENABLE,
					S5P_USBDEVICE_PHY_CONTROL);
	}
 }

/* Switch between HOST and OTG link from PHY_CFG */
void s5p_usb_phy_cfg_sel(struct device *dev, int type)
{
	u32 is_host;

	is_host = readl(EXYNOS5_USB_CFG);
	writel(type, EXYNOS5_USB_CFG);

	if (is_host != type)
		dev_dbg(dev, "Changed USB MUX from %s to %s",
			is_host ? "Host" : "Device", type ? "Host" : "Device");
}