blob: 8536040abffbd9a6ba34b1694dbe09fcfa76b6d7 [file] [log] [blame]
Sujit Reddy Thumma933df542012-12-31 15:37:16 +05301/*
2 * Copyright (c) 2012-2013, The Linux Foundation. All rights reserved.
3 *
4 * This program is free software; you can redistribute it and/or modify
5 * it under the terms of the GNU General Public License version 2 and
6 * only version 2 as published by the Free Software Foundation.
7 *
8 * This program is distributed in the hope that it will be useful,
9 * but WITHOUT ANY WARRANTY; without even the implied warranty of
10 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
11 * GNU General Public License for more details.
12 */
13
14/*
15 * SATA init module.
16 * To be used with SATA interface on MSM targets.
17 */
18#include <linux/kernel.h>
19#include <linux/module.h>
20#include <linux/slab.h>
21#include <linux/io.h>
22#include <linux/err.h>
23#include <linux/device.h>
24#include <linux/platform_device.h>
25#include <linux/dma-mapping.h>
26#include <linux/delay.h>
27#include <linux/clk.h>
28#include <linux/iopoll.h>
29#include <linux/regulator/consumer.h>
30#include <linux/ahci_platform.h>
31#include <mach/clk.h>
32
33/* PHY registers */
34#define UNIPHY_PLL_REFCLK_CFG 0x000
35#define UNIPHY_PLL_POSTDIV1_CFG 0x004
36#define UNIPHY_PLL_CHGPUMP_CFG 0x008
37#define UNIPHY_PLL_VCOLPF_CFG 0x00C
38#define UNIPHY_PLL_VREG_CFG 0x010
39#define UNIPHY_PLL_PWRGEN_CFG 0x014
40#define UNIPHY_PLL_DMUX_CFG 0x018
41#define UNIPHY_PLL_AMUX_CFG 0x01C
42#define UNIPHY_PLL_GLB_CFG 0x020
43#define UNIPHY_PLL_POSTDIV2_CFG 0x024
44#define UNIPHY_PLL_POSTDIV3_CFG 0x028
45#define UNIPHY_PLL_LPFR_CFG 0x02C
46#define UNIPHY_PLL_LPFC1_CFG 0x030
47#define UNIPHY_PLL_LPFC2_CFG 0x034
48#define UNIPHY_PLL_SDM_CFG0 0x038
49#define UNIPHY_PLL_SDM_CFG1 0x03C
50#define UNIPHY_PLL_SDM_CFG2 0x040
51#define UNIPHY_PLL_SDM_CFG3 0x044
52#define UNIPHY_PLL_SDM_CFG4 0x048
53#define UNIPHY_PLL_SSC_CFG0 0x04C
54#define UNIPHY_PLL_SSC_CFG1 0x050
55#define UNIPHY_PLL_SSC_CFG2 0x054
56#define UNIPHY_PLL_SSC_CFG3 0x058
57#define UNIPHY_PLL_LKDET_CFG0 0x05C
58#define UNIPHY_PLL_LKDET_CFG1 0x060
59#define UNIPHY_PLL_LKDET_CFG2 0x064
60#define UNIPHY_PLL_TEST_CFG 0x068
61#define UNIPHY_PLL_CAL_CFG0 0x06C
62#define UNIPHY_PLL_CAL_CFG1 0x070
63#define UNIPHY_PLL_CAL_CFG2 0x074
64#define UNIPHY_PLL_CAL_CFG3 0x078
65#define UNIPHY_PLL_CAL_CFG4 0x07C
66#define UNIPHY_PLL_CAL_CFG5 0x080
67#define UNIPHY_PLL_CAL_CFG6 0x084
68#define UNIPHY_PLL_CAL_CFG7 0x088
69#define UNIPHY_PLL_CAL_CFG8 0x08C
70#define UNIPHY_PLL_CAL_CFG9 0x090
71#define UNIPHY_PLL_CAL_CFG10 0x094
72#define UNIPHY_PLL_CAL_CFG11 0x098
73#define UNIPHY_PLL_EFUSE_CFG 0x09C
74#define UNIPHY_PLL_DEBUG_BUS_SEL 0x0A0
75#define UNIPHY_PLL_CTRL_42 0x0A4
76#define UNIPHY_PLL_CTRL_43 0x0A8
77#define UNIPHY_PLL_CTRL_44 0x0AC
78#define UNIPHY_PLL_CTRL_45 0x0B0
79#define UNIPHY_PLL_CTRL_46 0x0B4
80#define UNIPHY_PLL_CTRL_47 0x0B8
81#define UNIPHY_PLL_CTRL_48 0x0BC
82#define UNIPHY_PLL_STATUS 0x0C0
83#define UNIPHY_PLL_DEBUG_BUS0 0x0C4
84#define UNIPHY_PLL_DEBUG_BUS1 0x0C8
85#define UNIPHY_PLL_DEBUG_BUS2 0x0CC
86#define UNIPHY_PLL_DEBUG_BUS3 0x0D0
87#define UNIPHY_PLL_CTRL_54 0x0D4
88
89#define SATA_PHY_SER_CTRL 0x100
90#define SATA_PHY_TX_DRIV_CTRL0 0x104
91#define SATA_PHY_TX_DRIV_CTRL1 0x108
92#define SATA_PHY_TX_DRIV_CTRL2 0x10C
93#define SATA_PHY_TX_DRIV_CTRL3 0x110
94#define SATA_PHY_TX_RESV0 0x114
95#define SATA_PHY_TX_RESV1 0x118
96#define SATA_PHY_TX_IMCAL0 0x11C
97#define SATA_PHY_TX_IMCAL1 0x120
98#define SATA_PHY_TX_IMCAL2 0x124
99#define SATA_PHY_RX_IMCAL0 0x128
100#define SATA_PHY_RX_IMCAL1 0x12C
101#define SATA_PHY_RX_IMCAL2 0x130
102#define SATA_PHY_RX_TERM 0x134
103#define SATA_PHY_RX_TERM_RESV 0x138
104#define SATA_PHY_EQUAL 0x13C
105#define SATA_PHY_EQUAL_RESV 0x140
106#define SATA_PHY_OOB_TERM 0x144
107#define SATA_PHY_CDR_CTRL0 0x148
108#define SATA_PHY_CDR_CTRL1 0x14C
109#define SATA_PHY_CDR_CTRL2 0x150
110#define SATA_PHY_CDR_CTRL3 0x154
111#define SATA_PHY_CDR_CTRL4 0x158
112#define SATA_PHY_FA_LOAD0 0x15C
113#define SATA_PHY_FA_LOAD1 0x160
114#define SATA_PHY_CDR_CTRL_RESV 0x164
115#define SATA_PHY_PI_CTRL0 0x168
116#define SATA_PHY_PI_CTRL1 0x16C
117#define SATA_PHY_DESER_RESV 0x170
118#define SATA_PHY_RX_RESV0 0x174
119#define SATA_PHY_AD_TPA_CTRL 0x178
120#define SATA_PHY_REFCLK_CTRL 0x17C
121#define SATA_PHY_POW_DWN_CTRL0 0x180
122#define SATA_PHY_POW_DWN_CTRL1 0x184
123#define SATA_PHY_TX_DATA_CTRL 0x188
124#define SATA_PHY_BIST_GEN0 0x18C
125#define SATA_PHY_BIST_GEN1 0x190
126#define SATA_PHY_BIST_GEN2 0x194
127#define SATA_PHY_BIST_GEN3 0x198
128#define SATA_PHY_LBK_CTRL 0x19C
129#define SATA_PHY_TEST_DEBUG_CTRL 0x1A0
130#define SATA_PHY_ALIGNP 0x1A4
131#define SATA_PHY_PRBS_CFG0 0x1A8
132#define SATA_PHY_PRBS_CFG1 0x1AC
133#define SATA_PHY_PRBS_CFG2 0x1B0
134#define SATA_PHY_PRBS_CFG3 0x1B4
135#define SATA_PHY_CHAN_COMP_CHK_CNT 0x1B8
136#define SATA_PHY_RESET_CTRL 0x1BC
137#define SATA_PHY_RX_CLR 0x1C0
138#define SATA_PHY_RX_EBUF_CTRL 0x1C4
139#define SATA_PHY_ID0 0x1C8
140#define SATA_PHY_ID1 0x1CC
141#define SATA_PHY_ID2 0x1D0
142#define SATA_PHY_ID3 0x1D4
143#define SATA_PHY_RX_CHK_ERR_CNT0 0x1D8
144#define SATA_PHY_RX_CHK_ERR_CNT1 0x1DC
145#define SATA_PHY_RX_CHK_STAT 0x1E0
146#define SATA_PHY_TX_IMCAL_STAT 0x1E4
147#define SATA_PHY_RX_IMCAL_STAT 0x1E8
148#define SATA_PHY_RX_EBUF_STAT 0x1EC
149#define SATA_PHY_DEBUG_BUS_STAT0 0x1F0
150#define SATA_PHY_DEBUG_BUS_STAT1 0x1F4
151#define SATA_PHY_DEBUG_BUS_STAT2 0x1F8
152#define SATA_PHY_DEBUG_BUS_STAT3 0x1FC
153
154#define AHCI_HOST_CAP 0x00
155#define AHCI_HOST_CAP_MASK 0x1F
156#define AHCI_HOST_CAP_PMP (1 << 17)
157
158struct msm_sata_hba {
159 struct platform_device *ahci_pdev;
160 struct clk *slave_iface_clk;
161 struct clk *bus_clk;
162 struct clk *iface_clk;
163 struct clk *src_clk;
164 struct clk *rxoob_clk;
165 struct clk *pmalive_clk;
166 struct clk *cfg_clk;
167 struct regulator *clk_pwr;
168 struct regulator *pmp_pwr;
169 void __iomem *phy_base;
170 void __iomem *ahci_base;
171};
172
173static inline void msm_sata_delay_us(unsigned int delay)
174{
175 /* sleep for max. 50us more to combine processor wakeups */
176 usleep_range(delay, delay + 50);
177}
178
179static int msm_sata_clk_get_prepare_enable_set_rate(struct device *dev,
180 const char *name, struct clk **out_clk, int rate)
181{
182 int ret = 0;
183 struct clk *clk;
184
185 clk = devm_clk_get(dev, name);
186 if (IS_ERR(clk)) {
187 ret = PTR_ERR(clk);
188 dev_err(dev, "failed to get clk: %s err = %d\n", name, ret);
189 goto out;
190 }
191
192 if (rate >= 0) {
193 ret = clk_set_rate(clk, rate);
194 if (ret) {
195 dev_err(dev, "failed to set rate: %d clk: %s err = %d\n",
196 rate, name, ret);
197 goto out;
198 }
199 }
200
201 ret = clk_prepare_enable(clk);
202 if (ret)
203 dev_err(dev, "failed to enable clk: %s err = %d\n", name, ret);
204out:
205 if (!ret)
206 *out_clk = clk;
207
208 return ret;
209}
210
211static int msm_sata_clk_get_prepare_enable(struct device *dev,
212 const char *name, struct clk **out_clk)
213{
214 return msm_sata_clk_get_prepare_enable_set_rate(dev, name, out_clk, -1);
215}
216
217static void msm_sata_clk_put_unprepare_disable(struct clk **clk)
218{
219 if (*clk) {
220 clk_disable_unprepare(*clk);
221 clk_put(*clk);
222 *clk = NULL;
223 }
224}
225
226static int msm_sata_hard_reset(struct device *dev)
227{
228 int ret;
229 struct msm_sata_hba *hba = dev_get_drvdata(dev);
230
231 ret = clk_reset(hba->iface_clk, CLK_RESET_ASSERT);
232 if (ret) {
233 dev_err(dev, "iface_clk assert failed %d\n", ret);
234 goto out;
235 }
236
237 ret = clk_reset(hba->iface_clk, CLK_RESET_DEASSERT);
238 if (ret) {
239 dev_err(dev, "iface_clk de-assert failed %d\n", ret);
240 goto out;
241 }
242out:
243 return ret;
244}
245
246static int msm_sata_clk_init(struct device *dev)
247{
248 int ret = 0;
249 struct msm_sata_hba *hba = dev_get_drvdata(dev);
250
251 /* Enable AHB clock for system fabric slave port connected to SATA */
252 ret = msm_sata_clk_get_prepare_enable(dev,
253 "slave_iface_clk", &hba->slave_iface_clk);
254 if (ret)
255 goto out;
256
257 /* Enable AHB clock for system fabric and SATA core interface */
258 ret = msm_sata_clk_get_prepare_enable(dev,
259 "iface_clk", &hba->iface_clk);
260 if (ret)
261 goto put_dis_slave_iface_clk;
262
263 /* Enable AXI clock for SATA AXI master and slave interfaces */
264 ret = msm_sata_clk_get_prepare_enable(dev,
265 "bus_clk", &hba->bus_clk);
266 if (ret)
267 goto put_dis_iface_clk;
268
269 /* Enable the source clock for pmalive, rxoob and phy ref clocks */
270 ret = msm_sata_clk_get_prepare_enable_set_rate(dev,
271 "src_clk", &hba->src_clk, 100000000);
272 if (ret)
273 goto put_dis_bus_clk;
274
275 /*
276 * Enable RX OOB detection clock. The clock rate is
277 * same as PHY reference clock (100MHz).
278 */
279 ret = msm_sata_clk_get_prepare_enable(dev,
280 "core_rxoob_clk", &hba->rxoob_clk);
281 if (ret)
282 goto put_dis_src_clk;
283
284 /*
285 * Enable power management always-on clock. The clock rate
286 * is same as PHY reference clock (100MHz).
287 */
288 ret = msm_sata_clk_get_prepare_enable(dev,
289 "core_pmalive_clk", &hba->pmalive_clk);
290 if (ret)
291 goto put_dis_rxoob_clk;
292
293 /* Enable PHY configuration AHB clock, fixed 64MHz clock */
294 ret = msm_sata_clk_get_prepare_enable(dev,
295 "cfg_clk", &hba->cfg_clk);
296 if (ret)
297 goto put_dis_pmalive_clk;
298
299 return ret;
300
301put_dis_pmalive_clk:
302 msm_sata_clk_put_unprepare_disable(&hba->pmalive_clk);
303put_dis_rxoob_clk:
304 msm_sata_clk_put_unprepare_disable(&hba->rxoob_clk);
305put_dis_src_clk:
306 msm_sata_clk_put_unprepare_disable(&hba->src_clk);
307put_dis_bus_clk:
308 msm_sata_clk_put_unprepare_disable(&hba->bus_clk);
309put_dis_iface_clk:
310 msm_sata_clk_put_unprepare_disable(&hba->iface_clk);
311put_dis_slave_iface_clk:
312 msm_sata_clk_put_unprepare_disable(&hba->slave_iface_clk);
313out:
314 return ret;
315}
316
317static void msm_sata_clk_deinit(struct device *dev)
318{
319 struct msm_sata_hba *hba = dev_get_drvdata(dev);
320
321 msm_sata_clk_put_unprepare_disable(&hba->cfg_clk);
322 msm_sata_clk_put_unprepare_disable(&hba->pmalive_clk);
323 msm_sata_clk_put_unprepare_disable(&hba->rxoob_clk);
324 msm_sata_clk_put_unprepare_disable(&hba->src_clk);
325 msm_sata_clk_put_unprepare_disable(&hba->bus_clk);
326 msm_sata_clk_put_unprepare_disable(&hba->iface_clk);
327 msm_sata_clk_put_unprepare_disable(&hba->slave_iface_clk);
328}
329
330static int msm_sata_vreg_get_enable_set_vdd(struct device *dev,
331 const char *name, struct regulator **out_vreg,
332 int min_uV, int max_uV, int hpm_uA)
333{
334 int ret = 0;
335 struct regulator *vreg;
336
337 vreg = devm_regulator_get(dev, name);
338 if (IS_ERR(vreg)) {
339 ret = PTR_ERR(vreg);
340 dev_err(dev, "Regulator: %s get failed, err=%d\n", name, ret);
341 goto out;
342 }
343
344 if (regulator_count_voltages(vreg) > 0) {
345 ret = regulator_set_voltage(vreg, min_uV, max_uV);
346 if (ret) {
347 dev_err(dev, "Regulator: %s set voltage failed, err=%d\n",
348 name, ret);
349 goto err;
350 }
351
352 ret = regulator_set_optimum_mode(vreg, hpm_uA);
353 if (ret < 0) {
354 dev_err(dev, "Regulator: %s set optimum mode(uA_load=%d) failed, err=%d\n",
355 name, hpm_uA, ret);
356 goto err;
357 } else {
358 /*
359 * regulator_set_optimum_mode() can return non zero
360 * value even for success case.
361 */
362 ret = 0;
363 }
364 }
365
366 ret = regulator_enable(vreg);
367 if (ret)
368 dev_err(dev, "Regulator: %s enable failed, err=%d\n",
369 name, ret);
370err:
371 if (!ret)
372 *out_vreg = vreg;
373 else
374 devm_regulator_put(vreg);
375out:
376 return ret;
377}
378
379static int msm_sata_vreg_put_disable(struct device *dev,
380 struct regulator *reg, const char *name, int max_uV)
381{
382 int ret;
383
384 if (!reg)
385 return 0;
386
387 ret = regulator_disable(reg);
388 if (ret) {
389 dev_err(dev, "Regulator: %s disable failed err=%d\n",
390 name, ret);
391 goto err;
392 }
393
394 if (regulator_count_voltages(reg) > 0) {
395 ret = regulator_set_voltage(reg, 0, max_uV);
396 if (ret < 0) {
397 dev_err(dev, "Regulator: %s set voltage to 0 failed, err=%d\n",
398 name, ret);
399 goto err;
400 }
401
402 ret = regulator_set_optimum_mode(reg, 0);
403 if (ret < 0) {
404 dev_err(dev, "Regulator: %s set optimum mode(uA_load = 0) failed, err=%d\n",
405 name, ret);
406 goto err;
407 } else {
408 /*
409 * regulator_set_optimum_mode() can return non zero
410 * value even for success case.
411 */
412 ret = 0;
413 }
414 }
415
416err:
417 devm_regulator_put(reg);
418 return ret;
419}
420
421static int msm_sata_vreg_init(struct device *dev)
422{
423 int ret = 0;
424 struct msm_sata_hba *hba = dev_get_drvdata(dev);
425
426 /*
427 * The SATA clock generator needs 3.3V supply and can consume
428 * max. 850mA during functional mode.
429 */
430 ret = msm_sata_vreg_get_enable_set_vdd(dev, "sata_ext_3p3v",
431 &hba->clk_pwr, 3300000, 3300000, 850000);
432 if (ret)
433 goto out;
434
435 /* Add 1ms regulator ramp-up delay */
436 msm_sata_delay_us(1000);
437
438 /* Read AHCI capability register to check if PMP is supported.*/
439 if (readl_relaxed(hba->ahci_base +
440 AHCI_HOST_CAP) & AHCI_HOST_CAP_PMP) {
441 /* Power up port-multiplier */
442 ret = msm_sata_vreg_get_enable_set_vdd(dev, "sata_pmp_pwr",
443 &hba->pmp_pwr, 1800000, 1800000, 200000);
444 if (ret) {
445 msm_sata_vreg_put_disable(dev, hba->clk_pwr,
446 "sata_ext_3p3v", 3300000);
447 goto out;
448 }
449
450 /* Add 1ms regulator ramp-up delay */
451 msm_sata_delay_us(1000);
452 }
453
454out:
455 return ret;
456}
457
458static void msm_sata_vreg_deinit(struct device *dev)
459{
460 struct msm_sata_hba *hba = dev_get_drvdata(dev);
461
462 msm_sata_vreg_put_disable(dev, hba->clk_pwr,
463 "sata_ext_3p3v", 3300000);
464
465 if (hba->pmp_pwr)
466 msm_sata_vreg_put_disable(dev, hba->pmp_pwr,
467 "sata_pmp_pwr", 1800000);
468}
469
470static void msm_sata_phy_deinit(struct device *dev)
471{
472 struct msm_sata_hba *hba = dev_get_drvdata(dev);
473
474 /* Power down PHY */
475 writel_relaxed(0xF8, hba->phy_base + SATA_PHY_POW_DWN_CTRL0);
476 writel_relaxed(0xFE, hba->phy_base + SATA_PHY_POW_DWN_CTRL1);
477
478 /* Power down PLL block */
479 writel_relaxed(0x00, hba->phy_base + UNIPHY_PLL_GLB_CFG);
480 mb();
481
482 devm_iounmap(dev, hba->phy_base);
483}
484
485static int msm_sata_phy_init(struct device *dev)
486{
487 int ret = 0;
488 u32 reg = 0;
489 struct platform_device *pdev = to_platform_device(dev);
490 struct msm_sata_hba *hba = dev_get_drvdata(dev);
491 struct resource *mem;
492
493 mem = platform_get_resource_byname(pdev, IORESOURCE_MEM, "phy_mem");
494 if (!mem) {
495 dev_err(dev, "no mmio space\n");
496 return -EINVAL;
497 }
498
499 hba->phy_base = devm_ioremap(dev, mem->start, resource_size(mem));
500 if (!hba->phy_base) {
501 dev_err(dev, "failed to allocate memory for SATA PHY\n");
502 return -ENOMEM;
503 }
504
505 /* SATA phy initialization */
506
507 writel_relaxed(0x01, hba->phy_base + SATA_PHY_SER_CTRL);
508
509 writel_relaxed(0xB1, hba->phy_base + SATA_PHY_POW_DWN_CTRL0);
510 mb();
511 msm_sata_delay_us(10);
512
513 writel_relaxed(0x01, hba->phy_base + SATA_PHY_POW_DWN_CTRL0);
514 writel_relaxed(0x3E, hba->phy_base + SATA_PHY_POW_DWN_CTRL1);
515 writel_relaxed(0x01, hba->phy_base + SATA_PHY_RX_IMCAL0);
516 writel_relaxed(0x01, hba->phy_base + SATA_PHY_TX_IMCAL0);
517 writel_relaxed(0x02, hba->phy_base + SATA_PHY_TX_IMCAL2);
518
519 /* Write UNIPHYPLL registers to configure PLL */
520 writel_relaxed(0x04, hba->phy_base + UNIPHY_PLL_REFCLK_CFG);
521 writel_relaxed(0x00, hba->phy_base + UNIPHY_PLL_PWRGEN_CFG);
522
523 writel_relaxed(0x0A, hba->phy_base + UNIPHY_PLL_CAL_CFG0);
524 writel_relaxed(0xF3, hba->phy_base + UNIPHY_PLL_CAL_CFG8);
525 writel_relaxed(0x01, hba->phy_base + UNIPHY_PLL_CAL_CFG9);
526 writel_relaxed(0xED, hba->phy_base + UNIPHY_PLL_CAL_CFG10);
527 writel_relaxed(0x02, hba->phy_base + UNIPHY_PLL_CAL_CFG11);
528
529 writel_relaxed(0x36, hba->phy_base + UNIPHY_PLL_SDM_CFG0);
530 writel_relaxed(0x0D, hba->phy_base + UNIPHY_PLL_SDM_CFG1);
531 writel_relaxed(0xA3, hba->phy_base + UNIPHY_PLL_SDM_CFG2);
532 writel_relaxed(0xF0, hba->phy_base + UNIPHY_PLL_SDM_CFG3);
533 writel_relaxed(0x00, hba->phy_base + UNIPHY_PLL_SDM_CFG4);
534
535 writel_relaxed(0x19, hba->phy_base + UNIPHY_PLL_SSC_CFG0);
536 writel_relaxed(0xE1, hba->phy_base + UNIPHY_PLL_SSC_CFG1);
537 writel_relaxed(0x00, hba->phy_base + UNIPHY_PLL_SSC_CFG2);
538 writel_relaxed(0x11, hba->phy_base + UNIPHY_PLL_SSC_CFG3);
539
540 writel_relaxed(0x04, hba->phy_base + UNIPHY_PLL_LKDET_CFG0);
541 writel_relaxed(0xFF, hba->phy_base + UNIPHY_PLL_LKDET_CFG1);
542
543 writel_relaxed(0x02, hba->phy_base + UNIPHY_PLL_GLB_CFG);
544 mb();
545 msm_sata_delay_us(40);
546
547 writel_relaxed(0x03, hba->phy_base + UNIPHY_PLL_GLB_CFG);
548 mb();
549 msm_sata_delay_us(400);
550
551 writel_relaxed(0x05, hba->phy_base + UNIPHY_PLL_LKDET_CFG2);
552 mb();
553
554 /* poll for ready status, timeout after 1 sec */
555 ret = readl_poll_timeout(hba->phy_base + UNIPHY_PLL_STATUS, reg,
556 (reg & 1 << 0), 100, 1000000);
557 if (ret) {
558 dev_err(dev, "poll timeout UNIPHY_PLL_STATUS\n");
559 goto out;
560 }
561
562 ret = readl_poll_timeout(hba->phy_base + SATA_PHY_TX_IMCAL_STAT, reg,
563 (reg & 1 << 0), 100, 1000000);
564 if (ret) {
565 dev_err(dev, "poll timeout SATA_PHY_TX_IMCAL_STAT\n");
566 goto out;
567 }
568
569 ret = readl_poll_timeout(hba->phy_base + SATA_PHY_RX_IMCAL_STAT, reg,
570 (reg & 1 << 0), 100, 1000000);
571 if (ret) {
572 dev_err(dev, "poll timeout SATA_PHY_RX_IMCAL_STAT\n");
573 goto out;
574 }
575
576 /* SATA phy calibrated succesfully, power up to functional mode */
577 writel_relaxed(0x3E, hba->phy_base + SATA_PHY_POW_DWN_CTRL1);
578 writel_relaxed(0x01, hba->phy_base + SATA_PHY_RX_IMCAL0);
579 writel_relaxed(0x01, hba->phy_base + SATA_PHY_TX_IMCAL0);
580
581 writel_relaxed(0x00, hba->phy_base + SATA_PHY_POW_DWN_CTRL1);
582 writel_relaxed(0x59, hba->phy_base + SATA_PHY_CDR_CTRL0);
583 writel_relaxed(0x04, hba->phy_base + SATA_PHY_CDR_CTRL1);
584 writel_relaxed(0x00, hba->phy_base + SATA_PHY_CDR_CTRL2);
585 writel_relaxed(0x00, hba->phy_base + SATA_PHY_PI_CTRL0);
586 writel_relaxed(0x00, hba->phy_base + SATA_PHY_CDR_CTRL3);
587 writel_relaxed(0x01, hba->phy_base + SATA_PHY_POW_DWN_CTRL0);
588
589 writel_relaxed(0x11, hba->phy_base + SATA_PHY_TX_DATA_CTRL);
590 writel_relaxed(0x43, hba->phy_base + SATA_PHY_ALIGNP);
591 writel_relaxed(0x04, hba->phy_base + SATA_PHY_OOB_TERM);
592
593 writel_relaxed(0x01, hba->phy_base + SATA_PHY_EQUAL);
594 writel_relaxed(0x09, hba->phy_base + SATA_PHY_TX_DRIV_CTRL0);
595 writel_relaxed(0x09, hba->phy_base + SATA_PHY_TX_DRIV_CTRL1);
596 mb();
597
598 dev_dbg(dev, "SATA PHY powered up in functional mode\n");
599
600out:
601 /* power down PHY in case of failure */
602 if (ret)
603 msm_sata_phy_deinit(dev);
604
605 return ret;
606}
607
608int msm_sata_init(struct device *ahci_dev, void __iomem *mmio)
609{
610 int ret;
611 struct device *dev = ahci_dev->parent;
612 struct msm_sata_hba *hba = dev_get_drvdata(dev);
613
614 /* Save ahci mmio to access vendor specific registers */
615 hba->ahci_base = mmio;
616
617 ret = msm_sata_clk_init(dev);
618 if (ret) {
619 dev_err(dev, "SATA clk init failed with err=%d\n", ret);
620 goto out;
621 }
622
623 ret = msm_sata_vreg_init(dev);
624 if (ret) {
625 dev_err(dev, "SATA vreg init failed with err=%d\n", ret);
626 msm_sata_clk_deinit(dev);
627 goto out;
628 }
629
630 ret = msm_sata_phy_init(dev);
631 if (ret) {
632 dev_err(dev, "SATA PHY init failed with err=%d\n", ret);
633 msm_sata_vreg_deinit(dev);
634 msm_sata_clk_deinit(dev);
635 goto out;
636 }
637
638out:
639 return ret;
640}
641
642void msm_sata_deinit(struct device *ahci_dev)
643{
644 struct device *dev = ahci_dev->parent;
645
646 msm_sata_phy_deinit(dev);
647 msm_sata_vreg_deinit(dev);
648 msm_sata_clk_deinit(dev);
649}
650
651static int msm_sata_suspend(struct device *ahci_dev)
652{
653 msm_sata_deinit(ahci_dev);
654
655 return 0;
656}
657
658static int msm_sata_resume(struct device *ahci_dev)
659{
660 int ret;
661 struct device *dev = ahci_dev->parent;
662
663 ret = msm_sata_clk_init(dev);
664 if (ret) {
665 dev_err(dev, "SATA clk init failed with err=%d\n", ret);
666 /*
667 * If clock initialization failed, that means ahci driver
668 * cannot access any register going further. Since there is
669 * no check within ahci driver to check for clock failures,
670 * panic here instead of making an unclocked register access.
671 */
672 BUG();
673 }
674
675 /* Issue asynchronous reset to reset PHY */
676 ret = msm_sata_hard_reset(dev);
677 if (ret)
678 goto out;
679
680 ret = msm_sata_vreg_init(dev);
681 if (ret) {
682 dev_err(dev, "SATA vreg init failed with err=%d\n", ret);
683 /* Do not turn off clks, AHCI driver might do register access */
684 goto out;
685 }
686
687 ret = msm_sata_phy_init(dev);
688 if (ret) {
689 dev_err(dev, "SATA PHY init failed with err=%d\n", ret);
690 /* Do not turn off clks, AHCI driver might do register access */
691 msm_sata_vreg_deinit(dev);
692 goto out;
693 }
694out:
695 return ret;
696}
697
698static struct ahci_platform_data msm_ahci_pdata = {
699 .init = msm_sata_init,
700 .exit = msm_sata_deinit,
701 .suspend = msm_sata_suspend,
702 .resume = msm_sata_resume,
703};
704
705static int __devinit msm_sata_probe(struct platform_device *pdev)
706{
707 struct platform_device *ahci;
708 struct msm_sata_hba *hba;
709 int ret = 0;
710
711 hba = devm_kzalloc(&pdev->dev, sizeof(struct msm_sata_hba), GFP_KERNEL);
712 if (!hba) {
713 dev_err(&pdev->dev, "no memory\n");
714 ret = -ENOMEM;
715 goto err;
716 }
717
718 platform_set_drvdata(pdev, hba);
719
720 ahci = platform_device_alloc("ahci", pdev->id);
721 if (!ahci) {
722 dev_err(&pdev->dev, "couldn't allocate ahci device\n");
723 ret = -ENOMEM;
724 goto err_free;
725 }
726
727 dma_set_coherent_mask(&ahci->dev, pdev->dev.coherent_dma_mask);
728
729 ahci->dev.parent = &pdev->dev;
730 ahci->dev.dma_mask = pdev->dev.dma_mask;
731 ahci->dev.dma_parms = pdev->dev.dma_parms;
732 hba->ahci_pdev = ahci;
733
734 ret = platform_device_add_resources(ahci, pdev->resource,
735 pdev->num_resources);
736 if (ret) {
737 dev_err(&pdev->dev, "couldn't add resources to ahci device\n");
738 goto err_put_device;
739 }
740
741 ahci->dev.platform_data = &msm_ahci_pdata;
742 ret = platform_device_add(ahci);
743 if (ret) {
744 dev_err(&pdev->dev, "failed to register ahci device\n");
745 goto err_put_device;
746 }
747
748 return 0;
749
750err_put_device:
751 platform_device_put(ahci);
752err_free:
753 devm_kfree(&pdev->dev, hba);
754err:
755 return ret;
756}
757
758static int __devexit msm_sata_remove(struct platform_device *pdev)
759{
760 struct msm_sata_hba *hba = platform_get_drvdata(pdev);
761
762 platform_device_unregister(hba->ahci_pdev);
763
764 return 0;
765}
766
767static struct platform_driver msm_sata_driver = {
768 .probe = msm_sata_probe,
769 .remove = __devexit_p(msm_sata_remove),
770 .driver = {
771 .name = "msm_sata",
772 },
773};
774
775module_platform_driver(msm_sata_driver);
776
777MODULE_LICENSE("GPL v2");
778MODULE_DESCRIPTION("AHCI platform MSM Glue Layer");