summaryrefslogtreecommitdiff
path: root/arch/arm/mach-omap2/sata.c
blob: 95046ee84f3c42213846f574151287f95ef339a3 (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
341
342
343
344
345
346
347
348
349
350
351
352
353
/**
 * sata.c - The ahci sata device init functions
 *
 * Copyright (C) 2011 Texas Instruments Incorporated - http://www.ti.com
 * Author: Keshava Munegowda <keshava_mgowda@ti.com>
 *
 * This program is free software: you can redistribute it and/or modify
 * it under the terms of the GNU General Public License version 2  of
 * the License as published by the Free Software Foundation.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program.  If not, see <http://www.gnu.org/licenses/>.
 */
#include <linux/io.h>
#include <linux/kernel.h>
#include <linux/device.h>
#include <linux/platform_device.h>
#include <linux/pm_runtime.h>
#include <plat/omap_device.h>
#include <linux/dma-mapping.h>
#include <linux/ahci_platform.h>
#include <linux/clk.h>
#include <plat/sata.h>

#if defined(CONFIG_SATA_AHCI_PLATFORM) || \
	defined(CONFIG_SATA_AHCI_PLATFORM_MODULE)

#define OMAP_SATA_HWMODNAME	"sata"
#define OMAP_OCP2SCP3_HWMODNAME	"ocp2scp3"
#define AHCI_PLAT_DEVNAME	"ahci"

#define OMAP_SATA_PLL_CONTROL			0x00
#define OMAP_SATA_PLL_STATUS			0x04
#define OMAP_SATA_PLL_STATUS_LOCK_SHIFT		1
#define OMAP_SATA_PLL_STATUS_LOCK		1

#define OMAP_SATA_PLL_GO			0x08

#define OMAP_SATA_PLL_CFG1			0x0c
#define OMAP_SATA_PLL_CFG1_M_MASK		0xfff
#define OMAP_SATA_PLL_CFG1_M_SHIFT		9
#define OMAP_SATA_PLL_CFG1_N_MASK		0xf
#define OMAP_SATA_PLL_CFG1_N_SHIFT		1

#define OMAP_SATA_PLL_CFG2			0x10
#define OMAP_SATA_PLL_CFG2_SELFREQDCO_MASK	7
#define OMAP_SATA_PLL_CFG2_SELFREQDCO_SHIFT	1

#define OMAP_SATA_PLL_CFG3			0x14
#define OMAP_SATA_PLL_CFG3_SD_MASK		0xf
#define OMAP_SATA_PLL_CFG3_SD_SHIF		10

#define OMAP_SATA_PLL_CFG4			0x20
#define OMAP_SATA_PLL_CFG4_REGM_F_MASK		0x1ffff


/*
 * Enable the pll clk of 1.5 Ghz
 * since the sys_clk is 19.2 MHz, for 1.5 Ghz
 * following are the M and N values
 */
#define OMAP_SATA_PLL_CFG1_M			625
#define OMAP_SATA_PLL_CFG1_N			7

/*
 * for sata; the DCO frequencey range 1500 Mhz
 * hence the SELFREQDCO should be 4
 */
#define OMAP_SATA_PLL_CFG2_SELFREQDCO		4
#define OMAP_SATA_PLL_CFG3_SD			6
#define OMAP_SATA_PLL_CFG4_REGM_F		0

#define OMAP_OCP2SCP3_SYSCONFIG			0x10
#define OMAP_OCP2SCP3_SYSCONFIG_RESET_MASK	1
#define OMAP_OCP2SCP3_SYSCONFIG_RESET_SHIFT	1
#define OMAP_OCP2SCP3_SYSCONFIG_RESET		1

#define OMAP_OCP2SCP3_SYSSTATUS			0x14
#define OMAP_OCP2SCP3_SYSSTATUS_RESETDONE	1

#define OMAP_OCP2SCP3_TIMING			0x18
#define OMAP_OCP2SCP3_TIMING_SYNC2_MASK		7
#define OMAP_OCP2SCP3_TIMING_SYNC2		7


#define OMAP_SATA_PHY_PWR
/* - FIXME -
 * The sata phy power enable belongs to control module
 * for now it will be part of this driver; it should
 * seperated from the sata configuration.
 */
#define OMAP_CTRL_MODULE_CORE			0x4a002000
#define OMAP_CTRL_MODULE_CORE_SIZE		2048

#define OMAP_CTRL_SATA_PHY_POWER		0x374
#define SATA_PWRCTL_CLK_FREQ_SHIFT		22
#define SATA_PWRCTL_CLK_CMD_SHIFT		14

#define OMAP_CTRL_SATA_EXT_MODE			0x3ac

/* Enable the 19.2 Mhz frequency */
#define SATA_PWRCTL_CLK_FREQ			19

/* Enable Tx and Rx phys */
#define SATA_PWRCTL_CLK_CMD			3

static u64				sata_dmamask = DMA_BIT_MASK(32);
static struct ahci_platform_data	sata_pdata;
static struct clk			*sata_ref_clk;

static struct omap_device_pm_latency omap_sata_latency[] = {
	  {
		.deactivate_func = omap_device_idle_hwmods,
		.activate_func	 = omap_device_enable_hwmods,
		.flags = OMAP_DEVICE_LATENCY_AUTO_ADJUST,
	  },
};

static inline void omap_sata_writel(void __iomem *base, u32 reg, u32 val)
{
	__raw_writel(val, base + reg);
}

static inline u32 omap_sata_readl(void __iomem *base, u32 reg)
{
	return __raw_readl(base + reg);
}

#ifdef OMAP_SATA_PHY_PWR
static void __init sata_phy_pwr_on(void)
{
	void __iomem	*base;

	base = ioremap(OMAP_CTRL_MODULE_CORE, OMAP_CTRL_MODULE_CORE_SIZE);
	if (base) {
		omap_sata_writel(base, OMAP_CTRL_SATA_PHY_POWER,
			((SATA_PWRCTL_CLK_FREQ << SATA_PWRCTL_CLK_FREQ_SHIFT)|
			 (SATA_PWRCTL_CLK_CMD << SATA_PWRCTL_CLK_CMD_SHIFT)));
		omap_sata_writel(base, OMAP_CTRL_SATA_EXT_MODE, 1);
		iounmap(base);
	}
}
#endif

/*
 * - FIXME -
 * Following PLL configuration will be removed in future,
 * to a seperate platform driver
 */
static int __init sata_phy_init(struct device *dev)
{
	void __iomem		*pll;
	void __iomem		*ocp2scp3;
	struct resource		*res;
	struct platform_device	*pdev;
	u32			reg;
	int			ret;
	unsigned long		timeout;

	pdev =	container_of(dev, struct platform_device, dev);

	/*Enable the sata reference clock */
	sata_ref_clk = clk_get(dev, "ref_clk");
	if (IS_ERR(sata_ref_clk)) {
		ret = PTR_ERR(sata_ref_clk);
		dev_err(dev, "ref_clk failed:%d\n", ret);
		return ret;
	}
	clk_enable(sata_ref_clk);

#ifdef OMAP_SATA_PHY_PWR
	sata_phy_pwr_on();
#endif
	res =  platform_get_resource_byname(pdev, IORESOURCE_MEM, "ocp2scp3");
	if (!res) {
		dev_err(dev, "ocp2scp3 get resource failed\n");
		goto pll;
	}

	ocp2scp3 = ioremap(res->start, resource_size(res));
	if (!ocp2scp3) {
		dev_err(dev, "can't map ocp2scp3 0x%X\n", res->start);
		return -ENOMEM;
	}

	/*
	 * set the ocp2scp3 values;
	 * as per OMAP5 TRM, division ratio should not modified
	 * SYNC2 should use the value 0x6 or 7;
	 * SYNC1 is undefined; hence default value is used
	 */
	reg  = omap_sata_readl(ocp2scp3, OMAP_OCP2SCP3_TIMING);
	reg &= ~OMAP_OCP2SCP3_TIMING_SYNC2_MASK;
	reg |= OMAP_OCP2SCP3_TIMING_SYNC2;
	omap_sata_writel(ocp2scp3, OMAP_OCP2SCP3_TIMING, reg);

	/* do the soft reset of ocp2scp3 */
	reg  = omap_sata_readl(ocp2scp3, OMAP_OCP2SCP3_SYSCONFIG);
	reg &= ~(OMAP_OCP2SCP3_SYSCONFIG_RESET_MASK <<
		OMAP_OCP2SCP3_SYSCONFIG_RESET_SHIFT);
	reg |= (OMAP_OCP2SCP3_SYSCONFIG_RESET <<
		OMAP_OCP2SCP3_SYSCONFIG_RESET_SHIFT);
	omap_sata_writel(ocp2scp3, OMAP_OCP2SCP3_SYSCONFIG, reg);

	/* wait for the reset to complete */
	timeout = jiffies + msecs_to_jiffies(1000);
	while (!(omap_sata_readl(ocp2scp3, OMAP_OCP2SCP3_SYSSTATUS)
		& OMAP_OCP2SCP3_SYSSTATUS_RESETDONE)) {
		cpu_relax();

		if (time_after(jiffies, timeout)) {
			dev_err(dev, "ocp2scp3 reset timed out\n");
			break;
		}
	}
	iounmap(ocp2scp3);
pll:

	res =  platform_get_resource_byname(pdev, IORESOURCE_MEM, "pll");
	if (!res) {
		dev_err(dev, "pll get resource failed\n");
		goto end;
	}

	pll = ioremap(res->start, resource_size(res));
	if (!pll) {
		dev_err(dev, "can't map pll 0x%X\n", res->start);
		return -ENOMEM;
	}

	/*
	 * set the configuration 1; The phy clocks
	 * set M and N values for Phy clocks
	 */
	reg  = omap_sata_readl(pll, OMAP_SATA_PLL_CFG1);
	reg &= ~((OMAP_SATA_PLL_CFG1_M_MASK << OMAP_SATA_PLL_CFG1_M_SHIFT) |
		(OMAP_SATA_PLL_CFG1_N_MASK << OMAP_SATA_PLL_CFG1_N_SHIFT));
	reg |= ((OMAP_SATA_PLL_CFG1_M << OMAP_SATA_PLL_CFG1_M_SHIFT) |
		(OMAP_SATA_PLL_CFG1_N << OMAP_SATA_PLL_CFG1_N_SHIFT));
	omap_sata_writel(pll, OMAP_SATA_PLL_CFG1, reg);

	/* set the dco frequence range */
	reg  = omap_sata_readl(pll, OMAP_SATA_PLL_CFG2);
	reg &= ~(OMAP_SATA_PLL_CFG2_SELFREQDCO_MASK <<
		OMAP_SATA_PLL_CFG2_SELFREQDCO_SHIFT);
	reg |= (OMAP_SATA_PLL_CFG2_SELFREQDCO <<
		OMAP_SATA_PLL_CFG2_SELFREQDCO_SHIFT);
	omap_sata_writel(pll, OMAP_SATA_PLL_CFG2, reg);

	reg  = omap_sata_readl(pll, OMAP_SATA_PLL_CFG3);
	reg &= ~(OMAP_SATA_PLL_CFG3_SD_MASK << OMAP_SATA_PLL_CFG3_SD_SHIF);
	reg |= (OMAP_SATA_PLL_CFG3_SD << OMAP_SATA_PLL_CFG3_SD_SHIF);
	omap_sata_writel(pll, OMAP_SATA_PLL_CFG3, reg);


	reg  = omap_sata_readl(pll, OMAP_SATA_PLL_CFG4);
	reg &= ~OMAP_SATA_PLL_CFG4_REGM_F_MASK;
	reg |= OMAP_SATA_PLL_CFG4_REGM_F;
	omap_sata_writel(pll, OMAP_SATA_PLL_CFG4, reg);

	omap_sata_writel(pll, OMAP_SATA_PLL_GO, 1);

	/* Poll for the PLL lock */
	timeout = jiffies + msecs_to_jiffies(1000);
	while (!(omap_sata_readl(pll, OMAP_SATA_PLL_STATUS) &
		(OMAP_SATA_PLL_STATUS_LOCK <<
		OMAP_SATA_PLL_STATUS_LOCK_SHIFT))) {
		cpu_relax();

		if (time_after(jiffies, timeout)) {
			dev_err(dev, "sata phy pll lock timed out\n");
			break;
		}
	}
	iounmap(pll);
end:
	return 0;
}

static void sata_phy_exit(void)
{
	clk_disable(sata_ref_clk);
	clk_put(sata_ref_clk);
}

static int __init omap_ahci_plat_init(struct device *dev, void __iomem *base)
{
	pm_runtime_enable(dev);
	pm_runtime_get_sync(dev);
	sata_phy_init(dev);
	return 0;
}

static void omap_ahci_plat_exit(struct device *dev)
{
	pm_runtime_put_sync(dev);
	pm_runtime_disable(dev);
	sata_phy_exit();
}

void __init omap_sata_init(void)
{
	struct omap_hwmod	*hwmod[2];
	struct platform_device	*pdev;
	struct device		*dev;
	int			oh_cnt = 1;

	/* For now sata init works only for omap5 */
	if (!cpu_is_omap54xx())
		return;

	sata_pdata.init		= omap_ahci_plat_init;
	sata_pdata.exit		= omap_ahci_plat_exit;

	hwmod[0] = omap_hwmod_lookup(OMAP_SATA_HWMODNAME);
	if (!hwmod[0]) {
		pr_err("Could not look up %s\n", OMAP_SATA_HWMODNAME);
		return;
	}

	hwmod[1] = omap_hwmod_lookup(OMAP_OCP2SCP3_HWMODNAME);
	if (hwmod[1])
		oh_cnt++;
	else
		pr_err("Could not look up %s\n", OMAP_OCP2SCP3_HWMODNAME);

	pdev = omap_device_build_ss(AHCI_PLAT_DEVNAME, -1, hwmod, oh_cnt,
				(void *) &sata_pdata, sizeof(sata_pdata),
				omap_sata_latency,
				ARRAY_SIZE(omap_sata_latency), false);
	if (IS_ERR(pdev)) {
		pr_err("Could not build hwmod device %s\n",
					OMAP_SATA_HWMODNAME);
		return;
	}
	dev = &pdev->dev;
	get_device(dev);
	dev->dma_mask = &sata_dmamask;
	dev->coherent_dma_mask = DMA_BIT_MASK(32);
	put_device(dev);
}
#else

void __init omap_sata_init(void)
{
}

#endif