]> rtime.felk.cvut.cz Git - sojka/nv-tegra/linux-3.10.git/blob - drivers/ata/ahci-tegra.c
ata: ahci_tegra: disable DIPM
[sojka/nv-tegra/linux-3.10.git] / drivers / ata / ahci-tegra.c
1 /*
2  * ahci-tegra.c - AHCI SATA support for TEGRA AHCI device
3  *
4  * Copyright (c) 2011-2015, NVIDIA Corporation.  All rights reserved.
5  *
6  * This program is free software; you can redistribute it and/or modify it
7  * under the terms and conditions of the GNU General Public License,
8  * version 2, as published by the Free Software Foundation.
9  *
10  * This program is distributed in the hope it will be useful, but WITHOUT
11  * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
12  * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License for
13  * more details.
14  *
15  * You should have received a copy of the GNU General Public License
16  * along with this program.  If not, see <http://www.gnu.org/licenses/>.
17  *
18  *
19  * libata documentation is available via 'make {ps|pdf}docs',
20  * as Documentation/DocBook/libata.*
21  *
22  * AHCI hardware documentation:
23  * http://www.intel.com/technology/serialata/pdf/rev1_0.pdf
24  * http://www.intel.com/technology/serialata/pdf/rev1_1.pdf
25  *
26  */
27
28 #include <linux/platform_device.h>
29 #include <linux/irq.h>
30 #include <linux/kernel.h>
31 #include <linux/module.h>
32 #include <linux/init.h>
33 #include <linux/blkdev.h>
34 #include <linux/delay.h>
35 #include <linux/interrupt.h>
36 #include <linux/dma-mapping.h>
37 #include <linux/device.h>
38 #include <linux/dmi.h>
39 #include <linux/gpio.h>
40 #include <linux/of_gpio.h>
41 #include <scsi/scsi_host.h>
42 #include <scsi/scsi_cmnd.h>
43 #include <linux/libata.h>
44 #include <linux/regulator/machine.h>
45 #include <linux/pm_runtime.h>
46 #include "ahci.h"
47
48 #include <linux/clk.h>
49 #include <linux/clk/tegra.h>
50 #include <linux/tegra-powergate.h>
51 #include <linux/platform_data/tegra_ahci.h>
52 #include <linux/tegra-soc.h>
53
54 #include "../../arch/arm/mach-tegra/iomap.h"
55
56 #define DRV_NAME        "tegra-sata"
57 #define DRV_VERSION     "1.0"
58
59 #define ENABLE_AHCI_DBG_PRINT                   0
60 #if ENABLE_AHCI_DBG_PRINT
61 #define AHCI_DBG_PRINT(fmt, arg...)  printk(KERN_ERR fmt, ## arg)
62 #else
63 #define AHCI_DBG_PRINT(fmt, arg...) do {} while (0)
64 #endif
65
66 /* number of AHCI ports */
67 #define TEGRA_AHCI_NUM_PORTS                    1
68
69 /* idle timeout for PM in msec */
70 #define TEGRA_AHCI_MIN_IDLE_TIME                1000
71 #define TEGRA_AHCI_DEFAULT_IDLE_TIME            2000
72
73 #ifdef CONFIG_TEGRA_SATA_IDLE_POWERGATE
74 static u32 tegra_ahci_idle_time = TEGRA_AHCI_DEFAULT_IDLE_TIME;
75 #endif
76
77 #define PORT_BASE       (TEGRA_SATA_BAR5_BASE + 0x100)
78
79 /* Bit 0 (EN_FPCI) to allow FPCI accesses to SATA */
80 #define SATA_CONFIGURATION_0_OFFSET             0x180
81 #define EN_FPCI                                 (1 << 0)
82 #define CLK_OVERRIDE                            (1 << 31)
83
84 #define SATA_INTR_MASK_0_OFFSET                 0x188
85 #define IP_INT_MASK                             (1 << 16)
86
87 /* Need to write 0x00400200 to 0x70020094 */
88 #define SATA_FPCI_BAR5_0_OFFSET                 0x094
89 #define PRI_ICTLR_CPU_IER_SET_0_OFFSET          0x024
90 #define CPU_IER_SATA_CTL                        (1 << 23)
91
92 #define AHCI_BAR5_CONFIG_LOCATION               0x24
93 #define TEGRA_SATA_BAR5_INIT_PROGRAM            0xFFFFFFFF
94 #define TEGRA_SATA_BAR5_FINAL_PROGRAM           0x40020000
95
96 #define FUSE_SATA_CALIB_OFFSET                  0x224
97 #define FUSE_SATA_CALIB_MASK                    0x3
98
99 #define T_SATA0_CFG_PHY_REG                     0x120
100 #define T_SATA0_CFG_PHY_SQUELCH_MASK            (1 << 24)
101 #define PHY_USE_7BIT_ALIGN_DET_FOR_SPD_MASK     (1 << 11)
102
103 #define T_SATA0_CFG_POWER_GATE                  0x4ac
104 #define POWER_GATE_SSTS_RESTORED_MASK           (1 << 23)
105 #define POWER_GATE_SSTS_RESTORED_YES            (1 << 23)
106 #define POWER_GATE_SSTS_RESTORED_NO             (0 << 23)
107
108 #define T_SATA0_DBG0_OFFSET                     0x550
109
110 #define T_SATA0_INDEX_OFFSET                    0x680
111 #define SATA0_NONE_SELECTED                     0
112 #define SATA0_CH1_SELECTED                      (1 << 0)
113
114 #define T_SATA0_CHX_PHY_CTRL1_GEN1_OFFSET       0x690
115 #define SATA0_CHX_PHY_CTRL1_GEN1_TX_AMP_SHIFT   0
116 #define SATA0_CHX_PHY_CTRL1_GEN1_TX_AMP_MASK    (0xff << 0)
117 #define SATA0_CHX_PHY_CTRL1_GEN1_TX_PEAK_SHIFT  8
118 #define SATA0_CHX_PHY_CTRL1_GEN1_TX_PEAK_MASK   (0xff << 8)
119
120 #define T_SATA0_CHX_PHY_CTRL1_GEN2_OFFSET       0x694
121 #define SATA0_CHX_PHY_CTRL1_GEN2_TX_AMP_SHIFT   0
122 #define SATA0_CHX_PHY_CTRL1_GEN2_TX_AMP_MASK    (0xff << 0)
123 #define SATA0_CHX_PHY_CTRL1_GEN2_TX_PEAK_SHIFT  12
124 #define SATA0_CHX_PHY_CTRL1_GEN2_TX_PEAK_MASK   (0xff << 12)
125 #define SATA0_CHX_PHY_CTRL1_GEN2_RX_EQ_SHIFT    24
126 #define SATA0_CHX_PHY_CTRL1_GEN2_RX_EQ_MASK     (0xf << 24)
127
128 /* AHCI config space defines */
129 #define TEGRA_PRIVATE_AHCI_CC_BKDR              0x4a4
130 #define TEGRA_PRIVATE_AHCI_CC_BKDR_OVERRIDE     0x54c
131 #define TEGRA_PRIVATE_AHCI_CC_BKDR_OVERRIDE_EN  (1 << 12)
132 #define TEGRA_PRIVATE_AHCI_CC_BKDR_PGM          0x01060100
133
134 /* AHCI HBA_CAP */
135 #define TEGRA_PRIVATE_AHCI_CAP_BKDR             0xa0
136 #define T_SATA0_AHCI_HBA_CAP_BKDR               0x300
137 #define AHCI_HBA_PLL_CTRL_0                     0xa8
138
139 #define CLAMP_TXCLK_ON_SLUMBER                  (1 << 13)
140 #define CLAMP_TXCLK_ON_DEVSLP                   (1 << 24)
141 #define NO_CLAMP_SHUT_DOWN                      (1 << 3)
142
143 #define TEGRA_SATA_IO_SPACE_OFFSET              4
144 #define TEGRA_SATA_ENABLE_IO_SPACE              (1 << 0)
145 #define TEGRA_SATA_ENABLE_MEM_SPACE             (1 << 1)
146 #define TEGRA_SATA_ENABLE_BUS_MASTER            (1 << 2)
147 #define TEGRA_SATA_ENABLE_SERR                  (1 << 8)
148 #define TEGRA_SATA_CORE_CLOCK_FREQ_HZ           (105*1000*1000)
149 #define TEGRA_SATA_OOB_CLOCK_FREQ_HZ            (272*1000*1000)
150
151 #define APB_PMC_SATA_PWRGT_0_REG                0x1ac
152 #define APBDEV_PMC_PWRGATE_TOGGLE_0             0x30
153 #define APBDEV_PMC_PWRGATE_STATUS_0             0x38
154 #define APBDEV_PMC_REMOVE_CLAMPING_CMD_0        0x34
155
156 #define CLK_RST_SATA_PLL_CFG0_REG               0x490
157 #define CLK_RST_SATA_PLL_CFG1_REG               0x494
158 #define CLK_RST_CONTROLLER_RST_DEVICES_V_0      0x358
159 #define CLK_RST_CONTROLLER_RST_DEVICES_W_0      0x35c
160 #define CLK_RST_CONTROLLER_RST_DEV_W_CLR_0      0x43c
161 #define CLK_RST_CONTROLLER_RST_DEV_V_CLR_0      0x434
162 #define CLK_RST_CONTROLLER_CLK_ENB_V_CLR_0      0x444
163 #define CLK_RST_CONTROLLER_CLK_ENB_V_SET_0      0x440
164 #define CLK_RST_CONTROLLER_CLK_ENB_V_0          0x360
165 #define CLK_RST_CONTROLLER_RST_DEV_W_SET        0x438
166 #define CLK_RST_CONTROLLER_RST_DEV_V_SET        0x430
167 #define SET_CEC_RESET                           0x100
168
169
170 #define CLR_CLK_ENB_SATA_OOB                    (1 << 27)
171 #define CLR_CLK_ENB_SATA                        (1 << 28)
172
173 #define CLK_RST_CONTROLLER_PLLE_MISC_0          0x0ec
174 #define CLK_RST_CONTROLLER_PLLE_MISC_0_VALUE    0x00070300
175 #define CLK_RST_CONTROLLER_PLLE_BASE_0          0xe8
176 #define PLLE_ENABLE                             (1 << 30)
177 #define CLK_RST_CONTROLLER_PLLE_AUX_0           0x48c
178 #define CLK_RST_CONTROLLER_PLLE_AUX_0_MASK      (1 << 1)
179
180 #define CLR_SATACOLD_RST                        (1 << 1)
181 #define SWR_SATACOLD_RST                        (1 << 1)
182 #define SWR_SATA_RST                            (1 << 28)
183 #define SWR_SATA_OOB_RST                        (1 << 27)
184 #define DEVSLP_OVERRIDE                         (1 << 17)
185 #define SDS_SUPPORT                             (1 << 13)
186 #define DESO_SUPPORT                            (1 << 15)
187 #define SATA_AUX_PAD_PLL_CNTL_1_REG             0x1100
188 #define SATA_AUX_MISC_CNTL_1_REG                0x1108
189 #define SATA_AUX_SPARE_CFG0_0                   0x1118
190
191 /* for APB_PMC_SATA_PWRGT_0_REG */
192 #define PG_INFO_MASK                            (1 << 6)
193 #define PG_INFO_ON                              (1 << 6)
194 #define PG_INFO_OFF                             (0 << 6)
195 #define PLLE_IDDQ_SWCTL_MASK                    (1 << 4)
196 #define PADPHY_IDDQ_OVERRIDE_VALUE_MASK         (1 << 3)
197 #define PADPHY_IDDQ_OVERRIDE_VALUE_ON           (1 << 3)
198 #define PADPHY_IDDQ_OVERRIDE_VALUE_OFF          (0 << 3)
199 #define PADPHY_IDDQ_SWCTL_MASK                  (1 << 2)
200 #define PADPHY_IDDQ_SWCTL_ON                    (1 << 2)
201 #define PADPHY_IDDQ_SWCTL_OFF                   (0 << 2)
202 #define PADPLL_IDDQ_OVERRIDE_VALUE_MASK         (1 << 1)
203 #define PADPLL_IDDQ_OVERRIDE_VALUE_ON           (1 << 1)
204 #define PADPLL_IDDQ_OVERRIDE_VALUE_OFF          (0 << 1)
205 #define PADPLL_IDDQ_SWCTL_MASK                  (1 << 0)
206 #define PADPLL_IDDQ_SWCTL_ON                    (1 << 0)
207 #define PADPLL_IDDQ_SWCTL_OFF                   (0 << 0)
208
209 #define START                                   (1 < 8)
210 #define PARTID_VALUE                            0x8
211
212 #define SAX_MASK                                (1 << 8)
213
214 /* for CLK_RST_SATA_PLL_CFG0_REG */
215 #define PADPLL_RESET_OVERRIDE_VALUE_MASK        (1 << 1)
216 #define PADPLL_RESET_OVERRIDE_VALUE_ON          (1 << 1)
217 #define PADPLL_RESET_OVERRIDE_VALUE_OFF         (0 << 1)
218 #define PADPLL_RESET_SWCTL_MASK                 (1 << 0)
219 #define PADPLL_RESET_SWCTL_ON                   (1 << 0)
220 #define PADPLL_RESET_SWCTL_OFF                  (0 << 0)
221 #define PLLE_IDDQ_SWCTL_ON                      (1 << 4)
222 #define PLLE_IDDQ_SWCTL_OFF                     (0 << 4)
223 #define PLLE_SATA_SEQ_ENABLE                    (1 << 24)
224 #define PLLE_SATA_SEQ_START_STATE               (1 << 25)
225 #define SATA_SEQ_PADPLL_PD_INPUT_VALUE          (1 << 5)
226 #define SATA_SEQ_LANE_PD_INPUT_VALUE            (1 << 6)
227 #define SATA_SEQ_RESET_INPUT_VALUE              (1 << 7)
228
229 /* for CLK_RST_SATA_PLL_CFG1_REG */
230 #define IDDQ2LANE_SLUMBER_DLY_MASK              (0xffL << 16)
231 #define IDDQ2LANE_SLUMBER_DLY_SHIFT             16
232 #define IDDQ2LANE_SLUMBER_DLY_3MS               (3 << 16)
233 #define IDDQ2LANE_IDDQ_DLY_SHIFT                0
234 #define IDDQ2LANE_IDDQ_DLY_MASK                 (0xffL << 0)
235
236 /* for SATA_AUX_PAD_PLL_CNTL_1_REG */
237 #define REFCLK_SEL_MASK                         (3 << 11)
238 #define REFCLK_SEL_INT_CML                      (0 << 11)
239 #define LOCKDET_FIELD                           (1 << 6)
240
241 /* for SATA_AUX_MISC_CNTL_1_REG */
242 #define NVA2SATA_OOB_ON_POR_MASK                (1 << 7)
243 #define NVA2SATA_OOB_ON_POR_YES                 (1 << 7)
244 #define NVA2SATA_OOB_ON_POR_NO                  (0 << 7)
245 #define L0_RX_IDLE_T_SAX_SHIFT                  5
246 #define L0_RX_IDLE_T_SAX_MASK                   (3 << 5)
247 #define L0_RX_IDLE_T_NPG_SHIFT                  3
248 #define L0_RX_IDLE_T_NPG_MASK                   (3 << 3)
249 #define L0_RX_IDLE_T_MUX_MASK                   (1 << 2)
250 #define L0_RX_IDLE_T_MUX_FROM_APB_MISC          (1 << 2)
251 #define L0_RX_IDLE_T_MUX_FROM_SATA              (0 << 2)
252
253 #define SSTAT_IPM_STATE_MASK                    0xF00
254 #define SSTAT_IPM_SLUMBER_STATE                 0x600
255 #define XUSB_PADCTL_USB3_PAD_MUX_0              0x134
256 #define FORCE_SATA_PAD_IDDQ_DISABLE_MASK0       (1 << 6)
257 #define XUSB_PADCTL_IOPHY_PLL_S0_CTL1_0         0x138
258 #define XUSB_PADCTL_PLL1_MODE                   (1 << 24)
259 #define XUSB_PADCTL_ELPG_PROGRAM_0              0x01c
260 #define AUX_ELPG_CLAMP_EN                       (1 << 24)
261 #define AUX_ELPG_CLAMP_EN_EARLY                 (1 << 25)
262 #define AUX_ELPG_VCORE_DOWN                     (1 << 26)
263 #define XUSB_PADCTL_IOPHY_MISC_PAD_S0_CTL_1_0   0x148
264 #define XUSB_PADCTL_IOPHY_MISC_IDDQ_OVRD        (1 << 1)
265 #define XUSB_PADCTL_IOPHY_MISC_IDDQ             (1 << 0)
266
267 #define SATA_AXI_BAR5_START_0                   0x54
268 #define SATA_AXI_BAR5_SZ_0                      0x14
269 #define SATA_AXI_BAR5_START_VALUE               0x70020
270 #define AXI_BAR5_SIZE_VALUE                     0x00008
271 #define FPCI_BAR5_0_START_VALUE                 0x0010000
272 #define FPCI_BAR5_0_FINAL_VALUE                 0x40020100
273 #define FPCI_BAR5_0_ACCESS_TYPE                 (1 << 0)
274
275 #define XUSB_PADCTL_IOPHY_MISC_PAD_S0_CTL_1_0   0x148
276 #define IDDQ_OVRD_MASK                          (1 << 1)
277 #define IDDQ_MASK                               (1 << 0)
278
279 /* for XUSB_PADCTL_IOPHY_PLL_S0_CTL1_0 */
280 #define PLL_PWR_OVRD_MASK                       (1 << 3)
281 #define PLL_RST_MASK                            (1 << 1)
282 #define PLL_IDDQ_MASK                           (1 << 0)
283
284 /* for CLK_RST_CONTROLLER_PLLE_MISC_0 */
285 #define T124_PLLE_IDDQ_SWCTL_MASK               (1 << 14)
286 #define PLLE_IDDQ_OVERRIDE_VALUE_MASK           (1 << 13)
287
288 /* Spread Settings */
289 #define SATA0_CHX_PHY_CTRL11_0                  0x6D0
290 #define SATA0_CHX_PHY_CTRL2_0                   0x69c
291 #define GEN2_RX_EQ                              (0x2800 << 16)
292 #define CDR_CNTL_GEN1                           0x23
293
294 #define CLK_RST_CONTROLLER_PLLE_SS_CNTL_0       0x68
295 #define PLLE_SSCCENTER                          (1 << 14)
296 #define PLLE_SSCINVERT                          (1 << 15)
297 #define PLLE_SSCMAX                             (0x25)
298 #define PLLE_SSCINCINTRV                        (0x20 << 24)
299 #define PLLE_SSCINC                             (1 << 16)
300 #define PLLE_BYPASS_SS                          (1 << 10)
301 #define PLLE_SSCBYP                             (1 << 12)
302 #define PLLE_INTERP_RESET                       (1 << 11)
303
304 #define SATA_AUX_RX_STAT_INT_0                  0x110c
305 #define SATA_RX_STAT_INT_DISABLE                (1 << 2)
306
307 #define T_SATA0_NVOOB                           0x114
308 #define T_SATA0_NVOOB_SQUELCH_FILTER_MODE_SHIFT 24
309 #define T_SATA0_NVOOB_SQUELCH_FILTER_MODE_MASK  (3 << 24)
310 #define T_SATA0_NVOOB_SQUELCH_FILTER_LENGTH_SHIFT       26
311 #define T_SATA0_NVOOB_SQUELCH_FILTER_LENGTH_MASK        (3 << 26)
312
313 #define T_SATA0_FIFO_0                          0x170
314 #define T_SATA0_FIFO_0_L2P_FIFO_DEPTH           (0x7 << 12)
315 #define T_SATA0_FIFO_0_L2P_FIFO_DEPTH_MASK      (0xf << 12)
316
317
318 #define PXSSTS_DEVICE_DETECTED                  (1 << 0)
319
320 #ifdef CONFIG_TEGRA_SATA_IDLE_POWERGATE
321
322 /* create a work for handling the async transfers */
323 static void tegra_ahci_work_handler(struct work_struct *work);
324 static DECLARE_WORK(tegra_ahci_work, tegra_ahci_work_handler);
325 static struct workqueue_struct *tegra_ahci_work_q;
326 #endif
327
328 enum {
329         AHCI_PCI_BAR = 5,
330 };
331
332 enum port_idle_status {
333         PORT_IS_NOT_IDLE,
334         PORT_IS_IDLE,
335         PORT_IS_IDLE_NOT_SLUMBER,
336         PORT_IS_SLUMBER,
337 };
338
339 enum sata_state {
340         SATA_ON,
341         SATA_OFF,
342         SATA_GOING_ON,
343         SATA_GOING_OFF,
344         SATA_ABORT_OFF,
345 };
346
347 enum clk_gate_state {
348         CLK_OFF,
349         CLK_ON,
350 };
351
352 enum sata_connectors {
353         MINI_SATA,
354         MICRO_SATA,
355         SLIMLINE_SATA,
356         E_SATA,
357         E_SATA_P,
358         SATA_EXPRESS,
359         STANDARD_SATA,
360 };
361
362 char *sata_power_rails[] = {
363         "avdd_sata",
364         "vdd_sata",
365         "hvdd_sata",
366         "avdd_sata_pll",
367         "vddio_pex_sata"
368 };
369
370 #define NUM_SATA_POWER_RAILS    ARRAY_SIZE(sata_power_rails)
371
372 struct tegra_qc_list {
373         struct list_head list;
374         struct ata_queued_cmd *qc;
375 };
376
377 /*
378  *  tegra_ahci_host_priv is the extension of ahci_host_priv
379  *  with extra fields: idle_timer, pg_save, pg_state, etc.
380  */
381 struct tegra_ahci_host_priv {
382         struct ahci_host_priv   ahci_host_priv;
383         struct regulator        *power_rails[NUM_SATA_POWER_RAILS];
384         void __iomem            *bars_table[6];
385         struct ata_host         *host;
386         struct timer_list       idle_timer;
387         struct device           *dev;
388         void                    *pg_save;
389         enum sata_state         pg_state;
390         enum sata_connectors    sata_connector;
391         struct list_head        qc_list;
392         struct clk              *clk_sata;
393         struct clk              *clk_sata_oob;
394         struct clk              *clk_pllp;
395         struct clk              *clk_cml1;
396         enum clk_gate_state     clk_state;
397         s16                     gen2_rx_eq;
398         int                     pexp_gpio;
399 };
400
401 static int tegra_ahci_init_one(struct platform_device *pdev);
402 static int tegra_ahci_remove_one(struct platform_device *pdev);
403 static void tegra_ahci_set_clk_rst_cnt_rst_dev(void);
404 static void tegra_ahci_clr_clk_rst_cnt_rst_dev(void);
405 static void tegra_ahci_pad_config(void);
406 static void tegra_ahci_put_sata_in_iddq(void);
407 static void tegra_ahci_iddqlane_config(void);
408
409 #ifdef CONFIG_PM
410 static bool tegra_ahci_power_un_gate(struct ata_host *host);
411 static bool tegra_ahci_power_gate(struct ata_host *host);
412 static void tegra_ahci_abort_power_gate(struct ata_host *host);
413 static int tegra_ahci_controller_suspend(struct platform_device *pdev);
414 static int tegra_ahci_controller_resume(struct platform_device *pdev);
415 static int tegra_ahci_suspend(struct platform_device *pdev, pm_message_t mesg);
416 static int tegra_ahci_resume(struct platform_device *pdev);
417 static enum port_idle_status tegra_ahci_is_port_idle(struct ata_port *ap);
418 static bool tegra_ahci_are_all_ports_idle(struct ata_host *host);
419 #ifdef CONFIG_TEGRA_SATA_IDLE_POWERGATE
420 static bool tegra_ahci_pad_resume(struct ata_host *host);
421 static bool tegra_ahci_pad_suspend(struct ata_host *host);
422 static void tegra_ahci_abort_pad_suspend(struct ata_host *host);
423 static unsigned int tegra_ahci_qc_issue(struct ata_queued_cmd *qc);
424 static int tegra_ahci_hardreset(struct ata_link *link, unsigned int *class,
425                                 unsigned long deadline);
426 static int tegra_ahci_runtime_suspend(struct device *dev);
427 static int tegra_ahci_runtime_resume(struct device *dev);
428 static void tegra_ahci_idle_timer(unsigned long arg);
429 static int tegra_ahci_queue_one_qc(struct tegra_ahci_host_priv *tegra_hpriv,
430                                    struct ata_queued_cmd *qc);
431 static void tegra_ahci_dequeue_qcs(struct tegra_ahci_host_priv *tegra_hpriv);
432 #endif
433 #else
434 #define tegra_ahci_controller_suspend   NULL
435 #define tegra_ahci_controller_resume    NULL
436 #define tegra_ahci_suspend              NULL
437 #define tegra_ahci_resume               NULL
438 #endif
439
440 static struct scsi_host_template ahci_sht = {
441         AHCI_SHT("tegra-sata"),
442 };
443
444 static struct ata_port_operations tegra_ahci_ops = {
445         .inherits       = &ahci_ops,
446 #ifdef CONFIG_PM
447 #ifdef CONFIG_TEGRA_SATA_IDLE_POWERGATE
448         .qc_issue       = tegra_ahci_qc_issue,
449         .hardreset      = tegra_ahci_hardreset,
450 #endif
451 #endif
452 };
453
454 static const struct ata_port_info ahci_port_info = {
455         .flags          = AHCI_FLAG_COMMON,
456         .pio_mask       = 0x1f, /* pio0-4 */
457         .udma_mask      = ATA_UDMA6,
458         .port_ops       = &tegra_ahci_ops,
459 };
460
461 #ifdef CONFIG_TEGRA_SATA_IDLE_POWERGATE
462 static const struct dev_pm_ops tegra_ahci_dev_rt_ops = {
463         .runtime_suspend = tegra_ahci_runtime_suspend,
464         .runtime_resume = tegra_ahci_runtime_resume,
465 };
466 #endif
467
468 static const struct of_device_id of_ahci_tegra_match[] = {
469         { .compatible = "nvidia,tegra114-ahci-sata", },
470         {},
471 };
472 MODULE_DEVICE_TABLE(of, of_ahci_tegra_match);
473
474 static struct platform_driver tegra_platform_ahci_driver = {
475         .probe          = tegra_ahci_init_one,
476         .remove         = tegra_ahci_remove_one,
477 #ifdef CONFIG_PM
478         .suspend        = tegra_ahci_suspend,
479         .resume         = tegra_ahci_resume,
480 #endif
481         .driver = {
482                 .name = DRV_NAME,
483                 .owner = THIS_MODULE,
484                 .of_match_table = of_match_ptr(of_ahci_tegra_match),
485         }
486 };
487
488 struct tegra_ahci_host_priv *g_tegra_hpriv;
489
490 static inline u32 port_readl(u32 offset)
491 {
492         u32 val;
493
494         val = readl(IO_ADDRESS(PORT_BASE + offset));
495         AHCI_DBG_PRINT("[0x%x] => 0x%08x\n", PORT_BASE + offset, val);
496         return val;
497 }
498
499 static inline void port_writel(u32 val, u32 offset)
500 {
501         AHCI_DBG_PRINT("[0x%x] => 0x%08x\n", PORT_BASE + offset, val);
502         writel(val, IO_ADDRESS(PORT_BASE + offset));
503         readl(IO_ADDRESS(PORT_BASE + offset));
504 }
505
506 static inline u32 bar5_readl(u32 offset)
507 {
508         u32 val;
509         val = readl(IO_ADDRESS(TEGRA_SATA_BAR5_BASE + offset));
510         AHCI_DBG_PRINT("[0x%x] => 0x%08x\n", TEGRA_SATA_BAR5_BASE+offset,
511                         val);
512         return val;
513 }
514
515 static inline void bar5_writel(u32 val, u32 offset)
516 {
517         AHCI_DBG_PRINT("[0x%x] <= 0x%08x\n", TEGRA_SATA_BAR5_BASE+offset,
518                         val);
519         writel(val, IO_ADDRESS(TEGRA_SATA_BAR5_BASE + offset));
520         readl(IO_ADDRESS(TEGRA_SATA_BAR5_BASE + offset));
521 }
522
523
524 static inline u32 xusb_readl(u32 offset)
525 {
526         u32 val;
527         val = readl(IO_ADDRESS(TEGRA_XUSB_PADCTL_BASE + offset));
528         AHCI_DBG_PRINT("[0x%x] => 0x%08x\n", TEGRA_XUSB_PADCTL_BASE+offset,
529                         val);
530         return val;
531 }
532
533 static inline void xusb_writel(u32 val, u32 offset)
534 {
535         AHCI_DBG_PRINT("[0x%x] <= 0x%08x\n", TEGRA_XUSB_PADCTL_BASE+offset,
536                         val);
537         writel(val, IO_ADDRESS(TEGRA_XUSB_PADCTL_BASE + offset));
538         readl(IO_ADDRESS(TEGRA_XUSB_PADCTL_BASE + offset));
539 }
540
541
542 static inline u32 pmc_readl(u32 offset)
543 {
544         u32 val;
545         val = readl(IO_ADDRESS(TEGRA_PMC_BASE + offset));
546         AHCI_DBG_PRINT("[0x%x] => 0x%08x\n", TEGRA_PMC_BASE+offset, val);
547         return val;
548 }
549
550 static inline void pmc_writel(u32 val, u32 offset)
551 {
552         AHCI_DBG_PRINT("[0x%x] <= 0x%08x\n", TEGRA_PMC_BASE+offset, val);
553         writel(val, IO_ADDRESS(TEGRA_PMC_BASE + offset));
554         readl(IO_ADDRESS(TEGRA_PMC_BASE + offset));
555 }
556
557 static inline u32 clk_readl(u32 offset)
558 {
559         u32 val;
560
561         val = readl(IO_ADDRESS(TEGRA_CLK_RESET_BASE + offset));
562         AHCI_DBG_PRINT("[0x%x] => 0x%08x\n", TEGRA_CLK_RESET_BASE+offset, val);
563         return val;
564 }
565
566 static inline void clk_writel(u32 val, u32 offset)
567 {
568         AHCI_DBG_PRINT("[0x%x] <= 0x%08x\n", TEGRA_CLK_RESET_BASE+offset, val);
569         writel(val, IO_ADDRESS(TEGRA_CLK_RESET_BASE + offset));
570         readl(IO_ADDRESS(TEGRA_CLK_RESET_BASE + offset));
571 }
572
573 static inline u32 misc_readl(u32 offset)
574 {
575         u32 val;
576
577         val = readl(IO_ADDRESS(TEGRA_APB_MISC_BASE + offset));
578         AHCI_DBG_PRINT("[0x%x] => 0x%08x\n", TEGRA_APB_MISC_BASE+offset, val);
579         return val;
580 }
581
582 static inline void misc_writel(u32 val, u32 offset)
583 {
584         AHCI_DBG_PRINT("[0x%x] <= 0x%08x\n", TEGRA_APB_MISC_BASE+offset, val);
585         writel(val, IO_ADDRESS(TEGRA_APB_MISC_BASE + offset));
586         readl(IO_ADDRESS(TEGRA_APB_MISC_BASE + offset));
587 }
588
589 static inline u32 sata_readl(u32 offset)
590 {
591         u32 val;
592
593         val = readl(IO_ADDRESS(TEGRA_SATA_BASE + offset));
594         AHCI_DBG_PRINT("[0x%x] => 0x%08x\n", TEGRA_SATA_BASE+offset, val);
595         return val;
596 }
597
598 static inline void sata_writel(u32 val, u32 offset)
599 {
600         AHCI_DBG_PRINT("[0x%x] <= 0x%08x\n", TEGRA_SATA_BASE+offset, val);
601         writel(val, IO_ADDRESS(TEGRA_SATA_BASE + offset));
602         readl(IO_ADDRESS(TEGRA_SATA_BASE + offset));
603 }
604
605 static inline u32 scfg_readl(u32 offset)
606 {
607         u32 val;
608
609         val = readl(IO_ADDRESS(TEGRA_SATA_CONFIG_BASE + offset));
610         AHCI_DBG_PRINT("[0x%x] => 0x%08x\n", TEGRA_SATA_CONFIG_BASE+offset,
611                                              val);
612         return val;
613 }
614
615 static inline void scfg_writel(u32 val, u32 offset)
616 {
617         AHCI_DBG_PRINT("[0x%x] <= 0x%08x\n", TEGRA_SATA_CONFIG_BASE+offset,
618                                              val);
619         writel(val, IO_ADDRESS(TEGRA_SATA_CONFIG_BASE + offset));
620         readl(IO_ADDRESS(TEGRA_SATA_CONFIG_BASE + offset));
621 }
622
623 static inline u32 pictlr_readl(u32 offset)
624 {
625         u32 val;
626
627         val = readl(IO_ADDRESS(TEGRA_PRIMARY_ICTLR_BASE + offset));
628         AHCI_DBG_PRINT("[0x%x] => 0x%08x\n", TEGRA_PRIMARY_ICTLR_BASE+offset,
629                                              val);
630         return val;
631 }
632
633 static inline void pictlr_writel(u32 val, u32 offset)
634 {
635         AHCI_DBG_PRINT("[0x%x] <= 0x%08x\n", TEGRA_PRIMARY_ICTLR_BASE+offset,
636                                              val);
637         writel(val, IO_ADDRESS(TEGRA_PRIMARY_ICTLR_BASE + offset));
638         readl(IO_ADDRESS(TEGRA_PRIMARY_ICTLR_BASE + offset));
639 }
640
641 static inline u32 fuse_readl(u32 offset)
642 {
643         u32 val;
644
645         val = readl(IO_ADDRESS(TEGRA_FUSE_BASE + offset));
646         AHCI_DBG_PRINT("[0x%x] => 0x%08x\n", TEGRA_FUSE_BASE+offset, val);
647
648         return val;
649 }
650
651 /* Sata Pad Cntrl Values */
652 struct sata_pad_cntrl {
653         u8 gen1_tx_amp;
654         u8 gen1_tx_peak;
655         u8 gen2_tx_amp;
656         u8 gen2_tx_peak;
657 };
658
659 static const struct sata_pad_cntrl sata_calib_pad_val[] = {
660         {       /* SATA_CALIB[1:0]  = 00 */
661                 0x18,
662                 0x04,
663                 0x18,
664                 0x0a
665         },
666         {       /* SATA_CALIB[1:0]  = 01 */
667                 0x0e,
668                 0x04,
669                 0x14,
670                 0x0a
671         },
672         {       /* SATA_CALIB[1:0]  = 10 */
673                 0x0e,
674                 0x07,
675                 0x1a,
676                 0x0e
677         },
678         {       /* SATA_CALIB[1:0]  = 11 */
679                 0x14,
680                 0x0e,
681                 0x1a,
682                 0x0e
683         }
684 };
685
686 #ifdef CONFIG_TEGRA_SATA_IDLE_POWERGATE
687 static void tegra_ahci_work_handler(struct work_struct *work)
688 {
689         tegra_ahci_runtime_resume(g_tegra_hpriv->dev);
690 }
691 #endif
692
693 static void tegra_ahci_set_pad_cntrl_regs(
694                         struct tegra_ahci_host_priv *tegra_hpriv)
695 {
696         int     calib_val;
697         int     val;
698         int     i;
699
700         calib_val = fuse_readl(FUSE_SATA_CALIB_OFFSET) & FUSE_SATA_CALIB_MASK;
701
702         val = clk_readl(CLK_RST_CONTROLLER_PLLE_SS_CNTL_0);
703         val &= ~(PLLE_SSCCENTER | PLLE_SSCINVERT);
704         val |= (PLLE_SSCMAX | PLLE_SSCINCINTRV | PLLE_SSCINC);
705         clk_writel(val, CLK_RST_CONTROLLER_PLLE_SS_CNTL_0);
706
707         val = clk_readl(CLK_RST_CONTROLLER_PLLE_SS_CNTL_0);
708         val &= ~(PLLE_BYPASS_SS | PLLE_SSCBYP);
709         clk_writel(val, CLK_RST_CONTROLLER_PLLE_SS_CNTL_0);
710
711         udelay(2);
712
713         val = clk_readl(CLK_RST_CONTROLLER_PLLE_SS_CNTL_0);
714         val &= ~(PLLE_INTERP_RESET);
715         clk_writel(val, CLK_RST_CONTROLLER_PLLE_SS_CNTL_0);
716
717         for (i = 0; i < TEGRA_AHCI_NUM_PORTS; ++i) {
718                 scfg_writel((1 << i), T_SATA0_INDEX_OFFSET);
719
720                 val = scfg_readl(T_SATA0_CHX_PHY_CTRL1_GEN1_OFFSET);
721                 val &= ~SATA0_CHX_PHY_CTRL1_GEN1_TX_AMP_MASK;
722                 val |= (sata_calib_pad_val[calib_val].gen1_tx_amp <<
723                         SATA0_CHX_PHY_CTRL1_GEN1_TX_AMP_SHIFT);
724                 scfg_writel(val, T_SATA0_CHX_PHY_CTRL1_GEN1_OFFSET);
725
726                 val = scfg_readl(T_SATA0_CHX_PHY_CTRL1_GEN1_OFFSET);
727                 val &= ~SATA0_CHX_PHY_CTRL1_GEN1_TX_PEAK_MASK;
728                 val |= (sata_calib_pad_val[calib_val].gen1_tx_peak <<
729                         SATA0_CHX_PHY_CTRL1_GEN1_TX_PEAK_SHIFT);
730                 scfg_writel(val, T_SATA0_CHX_PHY_CTRL1_GEN1_OFFSET);
731
732                 val = scfg_readl(T_SATA0_CHX_PHY_CTRL1_GEN2_OFFSET);
733                 val &= ~SATA0_CHX_PHY_CTRL1_GEN2_TX_AMP_MASK;
734                 val |= (sata_calib_pad_val[calib_val].gen2_tx_amp <<
735                         SATA0_CHX_PHY_CTRL1_GEN2_TX_AMP_SHIFT);
736                 scfg_writel(val, T_SATA0_CHX_PHY_CTRL1_GEN2_OFFSET);
737
738                 val = scfg_readl(T_SATA0_CHX_PHY_CTRL1_GEN2_OFFSET);
739                 val &= ~SATA0_CHX_PHY_CTRL1_GEN2_TX_PEAK_MASK;
740                 val |= (sata_calib_pad_val[calib_val].gen2_tx_peak <<
741                         SATA0_CHX_PHY_CTRL1_GEN2_TX_PEAK_SHIFT);
742                 scfg_writel(val, T_SATA0_CHX_PHY_CTRL1_GEN2_OFFSET);
743
744                 val = GEN2_RX_EQ;
745                 scfg_writel(val, SATA0_CHX_PHY_CTRL11_0);
746
747                 val = CDR_CNTL_GEN1;
748                 scfg_writel(val, SATA0_CHX_PHY_CTRL2_0);
749         }
750         scfg_writel(SATA0_NONE_SELECTED, T_SATA0_INDEX_OFFSET);
751 }
752
753 int tegra_ahci_get_rails(struct regulator *regulators[])
754 {
755         struct regulator *reg;
756         int i;
757         int ret = 0;
758
759         for (i = 0; i < NUM_SATA_POWER_RAILS; ++i) {
760                 reg = regulator_get(g_tegra_hpriv->dev, sata_power_rails[i]);
761                 if (IS_ERR_OR_NULL(reg)) {
762                         pr_err("%s: can't get regulator %s\n",
763                                 __func__, sata_power_rails[i]);
764                         WARN_ON(1);
765                         ret = PTR_ERR(reg);
766                         goto exit;
767                 }
768                 regulators[i] = reg;
769         }
770 exit:
771         return ret;
772 }
773
774 void tegra_ahci_put_rails(struct regulator *regulators[])
775 {
776         int i;
777
778         for (i = 0; i < NUM_SATA_POWER_RAILS; ++i)
779                 regulator_put(regulators[i]);
780 }
781
782 int tegra_ahci_power_on_rails(struct regulator *regulators[])
783 {
784         struct regulator *reg;
785         int i;
786         int ret = 0;
787
788         for (i = 0; i < NUM_SATA_POWER_RAILS; ++i) {
789                 reg = regulators[i];
790                 ret = regulator_enable(reg);
791                 if (ret) {
792                         pr_err("%s: can't enable regulator[%d]\n",
793                                 __func__, i);
794                         WARN_ON(1);
795                         goto exit;
796                 }
797         }
798
799 exit:
800         return ret;
801 }
802
803 int tegra_ahci_power_off_rails(struct regulator *regulators[])
804 {
805         struct regulator *reg;
806         int i;
807         int ret = 0;
808
809         for (i = 0; i < NUM_SATA_POWER_RAILS; ++i) {
810                 reg = regulators[i];
811                 if (!IS_ERR_OR_NULL(reg)) {
812                         ret = regulator_disable(reg);
813                         if (ret) {
814                                 pr_err("%s: can't disable regulator[%d]\n",
815                                         __func__, i);
816                                 WARN_ON(1);
817                                 goto exit;
818                         }
819                 }
820         }
821
822 exit:
823         return ret;
824 }
825
826 static void tegra_first_level_clk_gate(void)
827 {
828         if (g_tegra_hpriv->clk_state == CLK_OFF)
829                 return;
830
831         clk_disable_unprepare(g_tegra_hpriv->clk_sata);
832         clk_disable_unprepare(g_tegra_hpriv->clk_sata_oob);
833         clk_disable_unprepare(g_tegra_hpriv->clk_cml1);
834         g_tegra_hpriv->clk_state = CLK_OFF;
835 }
836
837 static int tegra_first_level_clk_ungate(void)
838 {
839         int ret = 0;
840
841         if (g_tegra_hpriv->clk_state == CLK_ON) {
842                 ret = -1;
843                 return ret;
844         }
845
846         if (clk_prepare_enable(g_tegra_hpriv->clk_sata)) {
847                 pr_err("%s: unable to enable SATA clock\n", __func__);
848                 ret = -ENODEV;
849                 return ret;
850         }
851         if (clk_prepare_enable(g_tegra_hpriv->clk_sata_oob)) {
852                 pr_err("%s: unable to enable SATA_OOB clock\n", __func__);
853                 clk_disable_unprepare(g_tegra_hpriv->clk_sata);
854                 ret = -ENODEV;
855                 return ret;
856         }
857         if (clk_prepare_enable(g_tegra_hpriv->clk_cml1)) {
858                 pr_err("%s: unable to enable cml1 clock\n", __func__);
859                 clk_disable_unprepare(g_tegra_hpriv->clk_sata);
860                 clk_disable_unprepare(g_tegra_hpriv->clk_sata_oob);
861                 ret = -ENODEV;
862                 return ret;
863         }
864         g_tegra_hpriv->clk_state = CLK_ON;
865
866         return ret;
867 }
868
869 static int tegra_ahci_controller_init(struct tegra_ahci_host_priv *tegra_hpriv,
870                 int lp0)
871 {
872         int err;
873         struct clk *clk_sata = NULL;
874         struct clk *clk_sata_oob = NULL;
875         struct clk *clk_sata_cold = NULL;
876         struct clk *clk_pllp = NULL;
877         struct clk *clk_cml1 = NULL;
878         u32 val;
879         u32 timeout;
880
881         if (!lp0) {
882                 err = tegra_ahci_get_rails(tegra_hpriv->power_rails);
883                 if (err) {
884                         pr_err("%s: fails to get rails (%d)\n", __func__, err);
885                         goto exit;
886                 }
887
888         }
889         err = tegra_ahci_power_on_rails(tegra_hpriv->power_rails);
890         if (err) {
891                 pr_err("%s: fails to power on rails (%d)\n", __func__, err);
892                 goto exit;
893         }
894
895         /* pll_p is the parent of tegra_sata and tegra_sata_oob */
896         clk_pllp = clk_get_sys(NULL, "pll_p");
897         if (IS_ERR_OR_NULL(clk_pllp)) {
898                 pr_err("%s: unable to get PLL_P clock\n", __func__);
899                 err = PTR_ERR(clk_pllp);
900                 goto exit;
901         }
902
903         clk_sata = clk_get_sys("tegra_sata", NULL);
904         if (IS_ERR_OR_NULL(clk_sata)) {
905                 pr_err("%s: unable to get SATA clock\n", __func__);
906                 err = PTR_ERR(clk_sata);
907                 goto exit;
908         }
909
910         clk_sata_oob = clk_get_sys("tegra_sata_oob", NULL);
911         if (IS_ERR_OR_NULL(clk_sata_oob)) {
912                 pr_err("%s: unable to get SATA OOB clock\n", __func__);
913                 err = PTR_ERR(clk_sata_oob);
914                 goto exit;
915         }
916
917         clk_sata_cold = clk_get_sys("tegra_sata_cold", NULL);
918         if (IS_ERR_OR_NULL(clk_sata_cold)) {
919                 pr_err("%s: unable to get SATA COLD clock\n", __func__);
920                 err = PTR_ERR(clk_sata_cold);
921                 goto exit;
922         }
923
924         tegra_hpriv->clk_sata = clk_sata;
925         tegra_hpriv->clk_sata_oob = clk_sata_oob;
926         tegra_hpriv->clk_pllp = clk_pllp;
927
928         tegra_periph_reset_assert(clk_sata);
929         tegra_periph_reset_assert(clk_sata_oob);
930         tegra_periph_reset_assert(clk_sata_cold);
931         udelay(10);
932
933         /* need to establish both clocks divisors before setting clk sources */
934         clk_set_rate(clk_sata, clk_get_rate(clk_sata)/10);
935         clk_set_rate(clk_sata_oob, clk_get_rate(clk_sata_oob)/10);
936
937         /* set SATA clk and SATA_OOB clk source */
938         clk_set_parent(clk_sata, clk_pllp);
939         clk_set_parent(clk_sata_oob, clk_pllp);
940
941         /* Configure SATA clocks */
942         /* Core clock runs at 108MHz */
943         if (clk_set_rate(clk_sata, TEGRA_SATA_CORE_CLOCK_FREQ_HZ)) {
944                 err = -ENODEV;
945                 goto exit;
946         }
947         /* OOB clock runs at 216MHz */
948         if (clk_set_rate(clk_sata_oob, TEGRA_SATA_OOB_CLOCK_FREQ_HZ)) {
949                 err = -ENODEV;
950                 goto exit;
951         }
952
953         if (clk_prepare_enable(clk_sata)) {
954                 pr_err("%s: unable to enable SATA clock\n", __func__);
955                 err = -ENODEV;
956                 goto exit;
957         }
958
959         if (clk_prepare_enable(clk_sata_oob)) {
960                 pr_err("%s: unable to enable SATA OOB clock\n", __func__);
961                 err = -ENODEV;
962                 goto exit;
963         }
964
965         tegra_periph_reset_deassert(clk_sata);
966         tegra_periph_reset_deassert(clk_sata_oob);
967         tegra_periph_reset_deassert(clk_sata_cold);
968
969         tegra_ahci_clr_clk_rst_cnt_rst_dev();
970
971         if (gpio_is_valid(tegra_hpriv->pexp_gpio)) {
972                 val = gpio_request(tegra_hpriv->pexp_gpio,
973                                 "ahci-tegra");
974                 if (val) {
975                         pr_err("failed to allocate Port expander gpio\n");
976                         err = -ENODEV;
977                         goto exit;
978                 }
979                 gpio_direction_output(tegra_hpriv->pexp_gpio, 1);
980         }
981
982         val = 0x100;
983         pmc_writel(val, APBDEV_PMC_REMOVE_CLAMPING_CMD_0);
984
985
986         /* SATA_PADPLL_RESET_OVERRIDE_VALUE=1 and SATA_PADPLL_RESET_SWCTL=1 */
987         val = clk_readl(CLK_RST_SATA_PLL_CFG0_REG);
988         val |= (PADPLL_RESET_OVERRIDE_VALUE_ON | PADPLL_RESET_SWCTL_ON);
989         clk_writel(val, CLK_RST_SATA_PLL_CFG0_REG);
990
991         /* select internal CML ref clk
992          * select PLLE as input to IO phy */
993         val = misc_readl(SATA_AUX_PAD_PLL_CNTL_1_REG);
994         val &= ~REFCLK_SEL_MASK;
995         val |= REFCLK_SEL_INT_CML;
996         misc_writel(val, SATA_AUX_PAD_PLL_CNTL_1_REG);
997
998         /* wait for SATA_PADPLL_IDDQ2LANE_SLUMBER_DLY = 3 microseconds. */
999         val = clk_readl(CLK_RST_SATA_PLL_CFG1_REG);
1000         val &= ~IDDQ2LANE_SLUMBER_DLY_MASK;
1001         val |= IDDQ2LANE_SLUMBER_DLY_3MS;
1002         clk_writel(val, CLK_RST_SATA_PLL_CFG1_REG);
1003         udelay(3);
1004
1005 #if defined(CONFIG_TEGRA_SILICON_PLATFORM)
1006         err = tegra_unpowergate_partition_with_clk_on(TEGRA_POWERGATE_SATA);
1007         if (err) {
1008                 pr_err("%s: ** failed to turn-on SATA (0x%x) **\n",
1009                                 __func__, err);
1010                 goto exit;
1011         }
1012 #endif
1013
1014         /*
1015          * place SATA Pad PLL out of reset by writing
1016          * SATA_PADPLL_RST_OVERRIDE_VALUE = 0
1017          */
1018         val = clk_readl(CLK_RST_SATA_PLL_CFG0_REG);
1019         val &= ~PADPLL_RESET_OVERRIDE_VALUE_MASK;
1020         clk_writel(val, CLK_RST_SATA_PLL_CFG0_REG);
1021
1022         /*
1023          * Wait for SATA_AUX_PAD_PLL_CNTL_1_0_LOCKDET to turn 1 with a timeout
1024          * of 15 us.
1025          */
1026         timeout = 15;
1027         while (timeout--) {
1028                 udelay(1);
1029                 val = xusb_readl(XUSB_PADCTL_IOPHY_PLL_S0_CTL1_0);
1030                 if (val & LOCKDET_FIELD)
1031                         break;
1032         }
1033         if (timeout == 0)
1034                 pr_err("%s: AUX_PAD_PLL_CNTL_1 (0x%x) is not locked in 15us.\n",
1035                         __func__, val);
1036
1037         tegra_ahci_pad_config();
1038
1039         clk_cml1 = clk_get_sys(NULL, "cml1");
1040         if (IS_ERR_OR_NULL(clk_cml1)) {
1041                 pr_err("%s: unable to get cml1 clock Errone is %d\n",
1042                                         __func__, (int) PTR_ERR(clk_cml1));
1043                 err = PTR_ERR(clk_cml1);
1044                 goto exit;
1045         }
1046         tegra_hpriv->clk_cml1 = clk_cml1;
1047         if (clk_prepare_enable(clk_cml1)) {
1048                 pr_err("%s: unable to enable cml1 clock\n", __func__);
1049                 err = -ENODEV;
1050                 goto exit;
1051         }
1052
1053         g_tegra_hpriv->clk_state = CLK_ON;
1054
1055         val = clk_readl(CLK_RST_SATA_PLL_CFG0_REG);
1056         val &= ~PADPLL_RESET_SWCTL_MASK;
1057         clk_writel(val, CLK_RST_SATA_PLL_CFG0_REG);
1058
1059         /* PLLE Programing for SATA */
1060
1061         val = clk_readl(CLK_RST_CONTROLLER_PLLE_AUX_0);
1062         val |= CLK_RST_CONTROLLER_PLLE_AUX_0_MASK;
1063         clk_writel(val, CLK_RST_CONTROLLER_PLLE_AUX_0);
1064
1065         /* bring SATA IOPHY out of IDDQ */
1066         val = xusb_readl(XUSB_PADCTL_USB3_PAD_MUX_0);
1067         val |= FORCE_SATA_PAD_IDDQ_DISABLE_MASK0;
1068         xusb_writel(val, XUSB_PADCTL_USB3_PAD_MUX_0);
1069
1070         val = xusb_readl(XUSB_PADCTL_ELPG_PROGRAM_0);
1071         val &= ~(AUX_ELPG_CLAMP_EN | AUX_ELPG_CLAMP_EN_EARLY |
1072                 AUX_ELPG_VCORE_DOWN);
1073         xusb_writel(val, XUSB_PADCTL_ELPG_PROGRAM_0);
1074
1075         val = xusb_readl(XUSB_PADCTL_IOPHY_PLL_S0_CTL1_0);
1076         val = val | XUSB_PADCTL_PLL1_MODE;
1077         xusb_writel(val, XUSB_PADCTL_IOPHY_PLL_S0_CTL1_0);
1078
1079         /* clear NVA2SATA_OOB_ON_POR in SATA_AUX_MISC_CNTL_1_REG */
1080         val = misc_readl(SATA_AUX_MISC_CNTL_1_REG);
1081         val &= ~NVA2SATA_OOB_ON_POR_MASK;
1082         misc_writel(val, SATA_AUX_MISC_CNTL_1_REG);
1083
1084         if (tegra_hpriv->sata_connector != MINI_SATA) {
1085                 /* Disable DEVSLP Feature */
1086                 val = misc_readl(SATA_AUX_MISC_CNTL_1_REG);
1087                 val &= ~SDS_SUPPORT;
1088                 misc_writel(val, SATA_AUX_MISC_CNTL_1_REG);
1089         }
1090
1091         val = sata_readl(SATA_CONFIGURATION_0_OFFSET);
1092         val |= EN_FPCI;
1093         sata_writel(val, SATA_CONFIGURATION_0_OFFSET);
1094
1095         val = sata_readl(SATA_CONFIGURATION_0_OFFSET);
1096         val |= CLK_OVERRIDE;
1097         sata_writel(val, SATA_CONFIGURATION_0_OFFSET);
1098
1099         /* program sata pad control based on the fuse */
1100         tegra_ahci_set_pad_cntrl_regs(tegra_hpriv);
1101
1102         /*
1103          * clear bit T_SATA0_CFG_PHY_0_USE_7BIT_ALIGN_DET_FOR_SPD of
1104          * T_SATA0_CFG_PHY_0
1105          */
1106         val = scfg_readl(T_SATA0_CFG_PHY_REG);
1107         val |= T_SATA0_CFG_PHY_SQUELCH_MASK;
1108         val &= ~PHY_USE_7BIT_ALIGN_DET_FOR_SPD_MASK;
1109         scfg_writel(val, T_SATA0_CFG_PHY_REG);
1110
1111         val = scfg_readl(T_SATA0_NVOOB);
1112         val |= (1 << T_SATA0_NVOOB_SQUELCH_FILTER_MODE_SHIFT);
1113         val |= (3 << T_SATA0_NVOOB_SQUELCH_FILTER_LENGTH_SHIFT);
1114         scfg_writel(val, T_SATA0_NVOOB);
1115
1116         val = scfg_readl(T_SATA0_FIFO_0);
1117         val = (val & ~T_SATA0_FIFO_0_L2P_FIFO_DEPTH_MASK) |
1118                         T_SATA0_FIFO_0_L2P_FIFO_DEPTH;
1119         scfg_writel(val, T_SATA0_FIFO_0);
1120
1121         /*
1122          * WAR: Before enabling SATA PLL shutdown, lockdet needs to be ignored.
1123          *      To ignore lockdet, T_SATA0_DBG0_OFFSET register bit 10 needs to
1124          *      be 1, and bit 8 needs to be 0.
1125          */
1126         val = scfg_readl(T_SATA0_DBG0_OFFSET);
1127         val |= (1 << 10);
1128         val &= ~(1 << 8);
1129         scfg_writel(val, T_SATA0_DBG0_OFFSET);
1130
1131         /* program class code and programming interface for AHCI */
1132         val = scfg_readl(TEGRA_PRIVATE_AHCI_CC_BKDR_OVERRIDE);
1133         val |= TEGRA_PRIVATE_AHCI_CC_BKDR_OVERRIDE_EN;
1134         scfg_writel(val, TEGRA_PRIVATE_AHCI_CC_BKDR_OVERRIDE);
1135         scfg_writel(TEGRA_PRIVATE_AHCI_CC_BKDR_PGM, TEGRA_PRIVATE_AHCI_CC_BKDR);
1136         val &= ~TEGRA_PRIVATE_AHCI_CC_BKDR_OVERRIDE_EN;
1137         scfg_writel(val, TEGRA_PRIVATE_AHCI_CC_BKDR_OVERRIDE);
1138
1139         /* Program config space registers: */
1140
1141         /* Enable BUS_MASTER+MEM+IO space, and SERR */
1142         val = scfg_readl(TEGRA_SATA_IO_SPACE_OFFSET);
1143         val |= TEGRA_SATA_ENABLE_IO_SPACE | TEGRA_SATA_ENABLE_MEM_SPACE |
1144                TEGRA_SATA_ENABLE_BUS_MASTER | TEGRA_SATA_ENABLE_SERR;
1145         scfg_writel(val, TEGRA_SATA_IO_SPACE_OFFSET);
1146
1147         /* program bar5 space, by first writing 1's to bar5 register */
1148         scfg_writel(TEGRA_SATA_BAR5_INIT_PROGRAM, AHCI_BAR5_CONFIG_LOCATION);
1149         /* flush */
1150         val = scfg_readl(AHCI_BAR5_CONFIG_LOCATION);
1151
1152         /* then, write the BAR5_FINAL_PROGRAM address */
1153         scfg_writel(TEGRA_SATA_BAR5_FINAL_PROGRAM, AHCI_BAR5_CONFIG_LOCATION);
1154         /* flush */
1155         scfg_readl(AHCI_BAR5_CONFIG_LOCATION);
1156
1157         sata_writel((FPCI_BAR5_0_FINAL_VALUE >> 8),
1158                         SATA_FPCI_BAR5_0_OFFSET);
1159
1160         val = scfg_readl(T_SATA0_AHCI_HBA_CAP_BKDR);
1161         val |= (HOST_CAP_ALPM | HOST_CAP_SSC | HOST_CAP_PART | HOST_CAP_PMP);
1162         scfg_writel(val, T_SATA0_AHCI_HBA_CAP_BKDR);
1163
1164         /* Second Level Clock Gating*/
1165         val = bar5_readl(AHCI_HBA_PLL_CTRL_0);
1166         val |= (CLAMP_TXCLK_ON_SLUMBER | CLAMP_TXCLK_ON_DEVSLP);
1167         val &= ~NO_CLAMP_SHUT_DOWN;
1168         bar5_writel(val, AHCI_HBA_PLL_CTRL_0);
1169
1170         /* set IP_INT_MASK */
1171         val = sata_readl(SATA_INTR_MASK_0_OFFSET);
1172         val |= IP_INT_MASK;
1173         sata_writel(val, SATA_INTR_MASK_0_OFFSET);
1174
1175 exit:
1176         if (!IS_ERR_OR_NULL(clk_pllp))
1177                 clk_put(clk_pllp);
1178         if (!IS_ERR_OR_NULL(clk_sata))
1179                 clk_put(clk_sata);
1180         if (!IS_ERR_OR_NULL(clk_sata_oob))
1181                 clk_put(clk_sata_oob);
1182         if (!IS_ERR_OR_NULL(clk_sata_cold))
1183                 clk_put(clk_sata_cold);
1184         if (!IS_ERR_OR_NULL(clk_cml1))
1185                 clk_put(clk_cml1);
1186
1187         if (err) {
1188                 /* turn off all SATA power rails; ignore returned status */
1189                 tegra_ahci_power_off_rails(tegra_hpriv->power_rails);
1190                 /* return regulators to system */
1191                 tegra_ahci_put_rails(tegra_hpriv->power_rails);
1192         }
1193         return err;
1194 }
1195
1196 static void tegra_ahci_save_initial_config(struct platform_device *pdev,
1197                                            struct ahci_host_priv *hpriv)
1198 {
1199         ahci_save_initial_config(&pdev->dev, hpriv, 0, 0);
1200 }
1201
1202 static void tegra_ahci_controller_remove(struct platform_device *pdev)
1203 {
1204         struct ata_host *host = dev_get_drvdata(&pdev->dev);
1205         struct tegra_ahci_host_priv *tegra_hpriv;
1206         int status;
1207
1208         tegra_hpriv = (struct tegra_ahci_host_priv *)host->private_data;
1209
1210 #ifdef CONFIG_PM
1211         /* call tegra_ahci_controller_suspend() to power-down the SATA */
1212         status = tegra_ahci_controller_suspend(pdev);
1213         if (status)
1214                 dev_err(host->dev, "remove: error suspend SATA (0x%x)\n",
1215                                    status);
1216 #else
1217         /* power off the sata */
1218         status = tegra_powergate_partition_with_clk_off(TEGRA_POWERGATE_SATA);
1219         if (status)
1220                 dev_err(host->dev, "remove: error turn-off SATA (0x%x)\n",
1221                                    status);
1222         tegra_ahci_power_off_rails(tegra_hpriv->power_rails);
1223 #endif
1224
1225         /* return system resources */
1226         tegra_ahci_put_rails(tegra_hpriv->power_rails);
1227 }
1228
1229 #ifdef CONFIG_PM
1230 static int tegra_ahci_controller_suspend(struct platform_device *pdev)
1231 {
1232         struct ata_host *host = dev_get_drvdata(&pdev->dev);
1233         struct tegra_ahci_host_priv *tegra_hpriv;
1234         unsigned long flags;
1235
1236         tegra_hpriv = (struct tegra_ahci_host_priv *)host->private_data;
1237
1238         /* stop the idle timer */
1239         if (timer_pending(&tegra_hpriv->idle_timer))
1240                 del_timer_sync(&tegra_hpriv->idle_timer);
1241
1242         spin_lock_irqsave(&host->lock, flags);
1243         if (tegra_hpriv->pg_state == SATA_OFF)
1244                 dev_dbg(host->dev, "suspend: SATA already power gated\n");
1245         else {
1246                 bool pg_ok;
1247
1248                 dev_dbg(host->dev, "suspend: power gating SATA...\n");
1249                 pg_ok = tegra_ahci_power_gate(host);
1250                 if (pg_ok) {
1251                         tegra_hpriv->pg_state = SATA_OFF;
1252                         dev_dbg(host->dev, "suspend: SATA is power gated\n");
1253                 } else {
1254                         tegra_ahci_abort_power_gate(host);
1255                         spin_unlock_irqrestore(&host->lock, flags);
1256                         return -EBUSY;
1257                 }
1258         }
1259
1260         if (gpio_is_valid(tegra_hpriv->pexp_gpio))
1261                 gpio_free(tegra_hpriv->pexp_gpio);
1262         spin_unlock_irqrestore(&host->lock, flags);
1263
1264         tegra_first_level_clk_gate();
1265
1266         return tegra_ahci_power_off_rails(tegra_hpriv->power_rails);
1267 }
1268
1269 static int tegra_ahci_controller_resume(struct platform_device *pdev)
1270 {
1271         struct ata_host *host = dev_get_drvdata(&pdev->dev);
1272         struct tegra_ahci_host_priv *tegra_hpriv;
1273         unsigned long flags;
1274         int err;
1275
1276         tegra_hpriv = (struct tegra_ahci_host_priv *)host->private_data;
1277
1278         err = tegra_ahci_power_on_rails(tegra_hpriv->power_rails);
1279         if (err) {
1280                 pr_err("%s: fails to power on rails (%d)\n", __func__, err);
1281                 return err;
1282         }
1283
1284         err = tegra_first_level_clk_ungate();
1285         if (err < 0) {
1286                 pr_err("%s: flcg ungate failed\n", __func__);
1287                 return err;
1288         }
1289
1290         spin_lock_irqsave(&host->lock, flags);
1291         if (tegra_hpriv->pg_state == SATA_ON) {
1292                 dev_dbg(host->dev, "resume: SATA already powered on\n");
1293         } else {
1294                 dev_dbg(host->dev, "resume: powering on SATA...\n");
1295                 tegra_ahci_power_un_gate(host);
1296                 tegra_hpriv->pg_state = SATA_ON;
1297         }
1298         spin_unlock_irqrestore(&host->lock, flags);
1299         tegra_first_level_clk_gate();
1300
1301         return 0;
1302 }
1303
1304 static int tegra_ahci_suspend(struct platform_device *pdev, pm_message_t mesg)
1305 {
1306         struct ata_host *host = dev_get_drvdata(&pdev->dev);
1307         void __iomem *mmio = host->iomap[AHCI_PCI_BAR];
1308         u32 ctl;
1309         int rc;
1310
1311         dev_dbg(host->dev, "** entering %s: **\n", __func__);
1312         if (mesg.event & PM_EVENT_SLEEP) {
1313                 /*
1314                  * AHCI spec rev1.1 section 8.3.3:
1315                  * Software must disable interrupts prior to requesting a
1316                  * transition of the HBA to D3 state.
1317                  */
1318                 ctl = readl(mmio + HOST_CTL);
1319                 ctl &= ~HOST_IRQ_EN;
1320                 writel(ctl, mmio + HOST_CTL);
1321                 readl(mmio + HOST_CTL); /* flush */
1322         }
1323
1324         rc = ata_host_suspend(host, mesg);
1325         if (rc)
1326                 return rc;
1327
1328         return tegra_ahci_controller_suspend(pdev);
1329 }
1330
1331 static int tegra_ahci_resume(struct platform_device *pdev)
1332 {
1333         struct ata_host *host = dev_get_drvdata(&pdev->dev);
1334         int rc;
1335         u32 val;
1336
1337         dev_dbg(host->dev, "** entering %s: **\n", __func__);
1338         rc = tegra_ahci_controller_resume(pdev);
1339         if (rc != 0)
1340                 return rc;
1341
1342         rc = tegra_ahci_controller_init(g_tegra_hpriv, 1);
1343         if (rc != 0) {
1344                 dev_err(host->dev, "TEGRA SATA init failed in resume\n");
1345                 return rc;
1346         }
1347
1348         if (pdev->dev.power.power_state.event == PM_EVENT_SUSPEND) {
1349                 rc = ahci_reset_controller(host);
1350                 if (rc)
1351                         return rc;
1352
1353                 val = misc_readl(SATA_AUX_RX_STAT_INT_0);
1354                 if (val && SATA_RX_STAT_INT_DISABLE) {
1355                         val &= ~SATA_RX_STAT_INT_DISABLE;
1356                         misc_writel(val, SATA_AUX_RX_STAT_INT_0);
1357                 }
1358
1359                 ahci_init_controller(host);
1360         }
1361
1362         ata_host_resume(host);
1363
1364         return 0;
1365 }
1366
1367 #ifdef CONFIG_TEGRA_SATA_IDLE_POWERGATE
1368 static int tegra_ahci_runtime_suspend(struct device *dev)
1369 {
1370         struct ata_host *host;
1371         struct tegra_ahci_host_priv *tegra_hpriv;
1372         bool pg_ok;
1373         unsigned long flags;
1374         int err = 0;
1375
1376         host = dev_get_drvdata(dev);
1377         tegra_hpriv = (struct tegra_ahci_host_priv *)host->private_data;
1378
1379         spin_lock_irqsave(&host->lock, flags);
1380
1381         switch (tegra_hpriv->pg_state) {
1382         case SATA_OFF:
1383                 dev_dbg(dev, "** rt-suspend: already power gated **\n");
1384                 break;
1385
1386         case SATA_ABORT_OFF:
1387                 dev_dbg(dev, "** rt-suspend: abort suspend **\n");
1388                 tegra_hpriv->pg_state = SATA_ON;
1389                 tegra_ahci_dequeue_qcs(tegra_hpriv);
1390                 err = -EBUSY;
1391                 break;
1392
1393         case SATA_ON:
1394         case SATA_GOING_OFF:
1395                 if (tegra_ahci_are_all_ports_idle(host)) {
1396                         /* if all ports are in idle, do power-gate */
1397                         dev_dbg(dev, "** rt-suspend: power-down sata (%u) **\n",
1398                                         tegra_hpriv->pg_state);
1399 #ifdef TEGRA_AHCI_CONTEXT_RESTORE
1400                         pg_ok = tegra_ahci_power_gate(host);
1401 #else
1402                         pg_ok = tegra_ahci_pad_suspend(host);
1403 #endif
1404                         dev_dbg(dev, "** rt-suspend: done **\n");
1405                         if (pg_ok) {
1406                                 tegra_hpriv->pg_state = SATA_OFF;
1407                         } else {
1408                                 dev_err(dev, "** rt-suspend: abort pg **\n");
1409 #ifdef TEGRA_AHCI_CONTEXT_RESTORE
1410                                 tegra_ahci_abort_power_gate(host);
1411 #else
1412                                 tegra_ahci_abort_pad_suspend(host);
1413 #endif
1414                                 tegra_hpriv->pg_state = SATA_ON;
1415                                 err = -EBUSY;
1416                         }
1417                 } else {
1418                         dev_dbg(dev, "** rt-suspend: port not idle (%u) **\n",
1419                                         tegra_hpriv->pg_state);
1420                         err = -EBUSY;
1421                 }
1422                 break;
1423
1424         case SATA_GOING_ON:
1425         default:
1426                 dev_err(dev, "** rt-suspend: bad state (%u) **\n",
1427                         tegra_hpriv->pg_state);
1428                 WARN_ON(1);
1429                 err = -EBUSY;
1430                 break;
1431
1432         }
1433
1434         spin_unlock_irqrestore(&host->lock, flags);
1435
1436         return err;
1437 }
1438
1439 static int tegra_ahci_runtime_resume(struct device *dev)
1440 {
1441         struct ata_host *host;
1442         struct tegra_ahci_host_priv *tegra_hpriv;
1443         int err = 0;
1444
1445         host = dev_get_drvdata(dev);
1446         tegra_hpriv = (struct tegra_ahci_host_priv *)host->private_data;
1447
1448         if (tegra_hpriv->pg_state == SATA_ON) {
1449                 dev_dbg(dev, "** rt-resume: already power ungated **\n");
1450                 goto exit;
1451         }
1452
1453         if ((tegra_hpriv->pg_state == SATA_OFF) ||
1454             (tegra_hpriv->pg_state == SATA_GOING_ON)) {
1455                 dev_dbg(dev, "** rt-resume: power-up sata (%u) **\n",
1456                                 tegra_hpriv->pg_state);
1457 #ifdef TEGRA_AHCI_CONTEXT_RESTORE
1458                 tegra_ahci_power_un_gate(host);
1459 #else
1460                 tegra_ahci_pad_resume(host);
1461 #endif
1462                 dev_dbg(dev, "** rt-resume: done **\n");
1463                 tegra_hpriv->pg_state = SATA_ON;
1464
1465                 /* now qc_issue all qcs in the qc_list */
1466                 tegra_ahci_dequeue_qcs(tegra_hpriv);
1467
1468         } else {
1469                 dev_err(dev, "** rt-resume: bad state (%u) **\n",
1470                                 tegra_hpriv->pg_state);
1471                 WARN_ON(1);
1472                 err = -EBUSY;
1473         }
1474
1475
1476 exit:
1477
1478         return err;
1479 }
1480
1481 #endif
1482
1483 #ifdef TEGRA_AHCI_CONTEXT_RESTORE
1484 static u16 pg_save_bar5_registers[] = {
1485         0x018,  /* T_AHCI_HBA_CCC_PORTS */
1486         0x004,  /* T_AHCI_HBA_GHC */
1487         0x014,  /* T_AHCI_HBA_CCC_CTL - OP (optional) */
1488         0x01C,  /* T_AHCI_HBA_EM_LOC */
1489         0x020   /* T_AHCI_HBA_EM_CTL - OP */
1490 };
1491
1492 static u16 pg_save_bar5_port_registers[] = {
1493         0x100,  /* T_AHCI_PORT_PXCLB */
1494         0x104,  /* T_AHCI_PORT_PXCLBU */
1495         0x108,  /* T_AHCI_PORT_PXFB */
1496         0x10C,  /* T_AHCI_PORT_PXFBU */
1497         0x114,  /* T_AHCI_PORT_PXIE */
1498         0x118,  /* T_AHCI_PORT_PXCMD */
1499         0x12C,  /* T_AHCI_PORT_PXSCTL */
1500         0x144   /* T_AHCI_PORT_PXDEVSLP */
1501 };
1502
1503 /*
1504  * pg_save_bar5_bkdr_registers:
1505  *    These registers in BAR5 are read only.
1506  * To restore back those register values, write the saved value
1507  *    to the registers specified in pg_restore_bar5_bkdr_registers[].
1508  *    These pg_restore_bar5_bkdr_registers[] are in SATA_CONFIG space.
1509  */
1510 static u16 pg_save_bar5_bkdr_registers[] = {
1511         /* Save and restore via bkdr writes */
1512         0x000,  /* T_AHCI_HBA_CAP */
1513         0x00C,  /* T_AHCI_HBA_PI */
1514         0x024   /* T_AHCI_HBA_CAP2 */
1515 };
1516
1517 static u16 pg_restore_bar5_bkdr_registers[] = {
1518         /* Save and restore via bkdr writes */
1519         0x300,  /* BKDR of T_AHCI_HBA_CAP */
1520         0x33c,  /* BKDR of T_AHCI_HBA_PI */
1521         0x330   /* BKDR of T_AHCI_HBA_CAP2 */
1522 };
1523
1524 /* These registers are saved for each port */
1525 static u16 pg_save_bar5_bkdr_port_registers[] = {
1526         0x120,  /* NV_PROJ__SATA0_CHX_AHCI_PORT_PXTFD  */
1527         0x124,  /* NV_PROJ__SATA0_CHX_AHCI_PORT_PXSIG */
1528         0x128   /* NV_PROJ__SATA0_CHX_AHCI_PORT_PXSSTS */
1529 };
1530
1531 static u16 pg_restore_bar5_bkdr_port_registers[] = {
1532         /* Save and restore via bkdr writes */
1533         0x790,  /* BKDR of NV_PROJ__SATA0_CHX_AHCI_PORT_PXTFD  */
1534         0x794,  /* BKDR of NV_PROJ__SATA0_CHX_AHCI_PORT_PXSIG */
1535         0x798   /* BKDR of NV_PROJ__SATA0_CHX_AHCI_PORT_PXSSTS */
1536 };
1537
1538 static u16 pg_save_config_registers[] = {
1539         0x004,  /* T_SATA0_CFG_1 */
1540         0x00C,  /* T_SATA0_CFG_3 */
1541         0x024,  /* T_SATA0_CFG_9 */
1542         0x028,  /* T_SATA0_CFG_10 */
1543         0x030,  /* T_SATA0_CFG_12 */
1544         0x034,  /* T_SATA0_CFG_13 */
1545         0x038,  /* T_SATA0_CFG_14 */
1546         0x03C,  /* T_SATA0_CFG_15 */
1547         0x040,  /* T_SATA0_CFG_16 */
1548         0x044,  /* T_SATA0_CFG_17 */
1549         0x048,  /* T_SATA0_CFG_18 */
1550         0x0B0,  /* T_SATA0_MSI_CTRL */
1551         0x0B4,  /* T_SATA0_MSI_ADDR1 */
1552         0x0B8,  /* T_SATA0_MSI_ADDR2 */
1553         0x0BC,  /* T_SATA0_MSI_DATA */
1554         0x0C0,  /* T_SATA0_MSI_QUEUE */
1555         0x0EC,  /* T_SATA0_MSI_MAP */
1556         0x124,  /* T_SATA0_CFG_PHY_POWER */
1557         0x128,  /* T_SATA0_CFG_PHY_POWER_1 */
1558         0x12C,  /* T_SATA0_CFG_PHY_1 */
1559         0x174,  /* T_SATA0_CFG_LINK_0 */
1560         0x178,  /* T_SATA0_CFG_LINK_1 */
1561         0x1D0,  /* MCP_SATA0_CFG_TRANS_0 */
1562         0x238,  /* T_SATA0_ALPM_CTRL */
1563         0x30C,  /* T_SATA0_AHCI_HBA_CYA_0 */
1564         0x320,  /* T_SATA0_AHCI_HBA_SPARE_1 */
1565         0x324,  /* T_SATA0_AHCI_HBA_SPARE_2 */
1566         0x328,  /* T_SATA0_AHCI_HBA_DYN_CLK_CLAMP */
1567         0x32C,  /* T_SATA0_AHCI_CFG_ERR_CTRL */
1568         0x338,  /* T_SATA0_AHCI_HBA_CYA_1 */
1569         0x340,  /* T_SATA0_AHCI_HBA_PRE_STAGING_CONTROL */
1570         0x430,  /* T_SATA0_CFG_FPCI_0 */
1571         0x494,  /* T_SATA0_CFG_ESATA_CTRL */
1572         0x4A0,  /* T_SATA0_CYA1 */
1573         0x4B0,  /* T_SATA0_CFG_GLUE */
1574         0x534,  /* T_SATA0_PHY_CTRL */
1575         0x540,  /* T_SATA0_CTRL */
1576         0x550,  /* T_SATA0_DBG0 */
1577         0x554   /* T_SATA0_LOW_POWER_COUNT */
1578 };
1579
1580 static u16 pg_save_config_port_registers[] = {
1581         /* Save and restore per port */
1582         /* need to have port selected */
1583         0x530,  /* T_SATA0_CHXCFG1 */
1584         0x684,  /* T_SATA0_CHX_MISC */
1585         0x700,  /* T_SATA0_CHXCFG3 */
1586         0x704,  /* T_SATA0_CHXCFG4_CHX */
1587         0x690,  /* T_SATA0_CHX_PHY_CTRL1_GEN1 */
1588         0x694,  /* T_SATA0_CHX_PHY_CTRL1_GEN2 */
1589         0x698,  /* T_SATA0_CHX_PHY_CTRL1_GEN3 */
1590         0x69C,  /* T_SATA0_CHX_PHY_CTRL_2 */
1591         0x6B0,  /* T_SATA0_CHX_PHY_CTRL_3 */
1592         0x6B4,  /* T_SATA0_CHX_PHY_CTRL_4 */
1593         0x6B8,  /* T_SATA0_CHX_PHY_CTRL_5 */
1594         0x6BC,  /* T_SATA0_CHX_PHY_CTRL_6 */
1595         0x714,  /* T_SATA0_PRBS_CHX - OP */
1596         0x750,  /* T_SATA0_CHX_LINK0 */
1597         0x7F0   /* T_SATA0_CHX_GLUE */
1598 };
1599
1600 static u16 pg_save_ipfs_registers[] = {
1601         0x094,  /* SATA_FPCI_BAR5_0 */
1602         0x0C0,  /* SATA_MSI_BAR_SZ_0 */
1603         0x0C4,  /* SATA_MSI_AXI_BAR_ST_0 */
1604         0x0C8,  /* SATA_MSI_FPCI_BAR_ST_0 */
1605         0x140,  /* SATA_MSI_EN_VEC0_0 */
1606         0x144,  /* SATA_MSI_EN_VEC1_0 */
1607         0x148,  /* SATA_MSI_EN_VEC2_0 */
1608         0x14C,  /* SATA_MSI_EN_VEC3_0 */
1609         0x150,  /* SATA_MSI_EN_VEC4_0 */
1610         0x154,  /* SATA_MSI_EN_VEC5_0 */
1611         0x158,  /* SATA_MSI_EN_VEC6_0 */
1612         0x15C,  /* SATA_MSI_EN_VEC7_0 */
1613         0x180,  /* SATA_CONFIGURATION_0 */
1614         0x184,  /* SATA_FPCI_ERROR_MASKS_0 */
1615         0x188,  /* SATA_INTR_MASK_0 */
1616         0x1A0,  /* SATA_CFG_REVID_0 */
1617         0x198,  /* SATA_IPFS_INTR_ENABLE_0 */
1618         0x1BC,  /* SATA_CLKGATE_HYSTERSIS_0 */
1619         0x1DC   /* SATA_SATA_MCCIF_FIFOCTRL_0 */
1620 };
1621
1622 static void tegra_ahci_save_regs(u32 **save_addr,
1623                                  u32 reg_base,
1624                                  u16 reg_array[],
1625                                  u32 regs)
1626 {
1627         u32 i;
1628         u32 *dest = *save_addr;
1629         void __iomem *base = IO_ADDRESS(reg_base);
1630
1631         for (i = 0; i < regs; ++i, ++dest) {
1632                 *dest = readl(base + (u32)reg_array[i]);
1633                 AHCI_DBG_PRINT("save: [0x%x]=0x%08x\n",
1634                                (reg_base+(u32)reg_array[i]), *dest);
1635         }
1636         *save_addr = dest;
1637 }
1638
1639 static void tegra_ahci_restore_regs(void **save_addr,
1640                                     u32 reg_base,
1641                                     u16 reg_array[],
1642                                     u32 regs)
1643 {
1644         u32 i;
1645         u32 *src = *save_addr;
1646         void __iomem *base = IO_ADDRESS(reg_base);
1647
1648         for (i = 0; i < regs; ++i, ++src) {
1649                 writel(*src, base + (u32)reg_array[i]);
1650                 AHCI_DBG_PRINT("restore: [0x%x]=0x%08x\n",
1651                                 (reg_base+(u32)reg_array[i]), *src);
1652         }
1653         *save_addr = src;
1654 }
1655
1656 static void tegra_ahci_pg_save_registers(struct ata_host *host)
1657 {
1658         struct tegra_ahci_host_priv *tegra_hpriv;
1659         u32 *pg_save;
1660         u32 regs;
1661         int i;
1662
1663         tegra_hpriv = (struct tegra_ahci_host_priv *)host->private_data;
1664         pg_save = tegra_hpriv->pg_save;
1665
1666         /*
1667          * Driver should save/restore the registers in the order of
1668          * IPFS, CFG, Ext CFG, BAR5.
1669          */
1670
1671         /* save IPFS registers */
1672         regs = ARRAY_SIZE(pg_save_ipfs_registers);
1673         tegra_ahci_save_regs(&pg_save, TEGRA_SATA_BASE,
1674                              pg_save_ipfs_registers, regs);
1675         /* after the call, pg_save should point to the next address to save */
1676
1677         /* save CONFIG registers */
1678         regs = ARRAY_SIZE(pg_save_config_registers);
1679         tegra_ahci_save_regs(&pg_save, TEGRA_SATA_CONFIG_BASE,
1680                              pg_save_config_registers, regs);
1681
1682         /* save CONFIG per port registers */
1683         for (i = 0; i < TEGRA_AHCI_NUM_PORTS; ++i) {
1684                 scfg_writel((1 << i), T_SATA0_INDEX_OFFSET);
1685                 regs = ARRAY_SIZE(pg_save_config_port_registers);
1686                 tegra_ahci_save_regs(&pg_save, TEGRA_SATA_CONFIG_BASE,
1687                                      pg_save_config_port_registers, regs);
1688         }
1689         scfg_writel(SATA0_NONE_SELECTED, T_SATA0_INDEX_OFFSET);
1690
1691         /* save BAR5 registers */
1692         regs = ARRAY_SIZE(pg_save_bar5_registers);
1693         tegra_ahci_save_regs(&pg_save, TEGRA_SATA_BAR5_BASE,
1694                              pg_save_bar5_registers, regs);
1695
1696         /* save BAR5 port_registers */
1697         regs = ARRAY_SIZE(pg_save_bar5_port_registers);
1698         for (i = 0; i < TEGRA_AHCI_NUM_PORTS; ++i)
1699                 tegra_ahci_save_regs(&pg_save, TEGRA_SATA_BAR5_BASE + (0x80*i),
1700                                      pg_save_bar5_port_registers, regs);
1701
1702         /* save bkdr registers */
1703         regs = ARRAY_SIZE(pg_save_bar5_bkdr_registers);
1704         tegra_ahci_save_regs(&pg_save, TEGRA_SATA_BAR5_BASE,
1705                              pg_save_bar5_bkdr_registers, regs);
1706
1707         /* and save bkdr per_port registers */
1708         for (i = 0; i < TEGRA_AHCI_NUM_PORTS; ++i) {
1709                 scfg_writel((1 << i), T_SATA0_INDEX_OFFSET);
1710                 regs = ARRAY_SIZE(pg_save_bar5_bkdr_port_registers);
1711                 tegra_ahci_save_regs(&pg_save, TEGRA_SATA_BAR5_BASE + (0x80*i),
1712                                      pg_save_bar5_bkdr_port_registers,
1713                                      regs);
1714         }
1715         scfg_writel(SATA0_NONE_SELECTED, T_SATA0_INDEX_OFFSET);
1716 }
1717
1718 static void tegra_ahci_pg_restore_registers(struct ata_host *host)
1719 {
1720         struct tegra_ahci_host_priv *tegra_hpriv;
1721         void *pg_save;
1722         u32 regs, val;
1723         int i;
1724
1725         tegra_hpriv = (struct tegra_ahci_host_priv *)host->private_data;
1726         pg_save = tegra_hpriv->pg_save;
1727
1728         /*
1729          * Driver should restore the registers in the order of
1730          * IPFS, CFG, Ext CFG, BAR5.
1731          */
1732
1733         /* restore IPFS registers */
1734         regs = ARRAY_SIZE(pg_save_ipfs_registers);
1735         tegra_ahci_restore_regs(&pg_save, TEGRA_SATA_BASE,
1736                                 pg_save_ipfs_registers, regs);
1737         /* after the call, pg_save should point to the next addr to restore */
1738
1739         /* restore CONFIG registers */
1740         regs = ARRAY_SIZE(pg_save_config_registers);
1741         tegra_ahci_restore_regs(&pg_save, TEGRA_SATA_CONFIG_BASE,
1742                                 pg_save_config_registers, regs);
1743
1744         /* restore CONFIG per port registers */
1745         for (i = 0; i < TEGRA_AHCI_NUM_PORTS; ++i) {
1746                 scfg_writel((1 << i), T_SATA0_INDEX_OFFSET);
1747                 regs = ARRAY_SIZE(pg_save_config_port_registers);
1748                 tegra_ahci_restore_regs(&pg_save, TEGRA_SATA_CONFIG_BASE,
1749                                         pg_save_config_port_registers,
1750                                         regs);
1751         }
1752         scfg_writel(SATA0_NONE_SELECTED, T_SATA0_INDEX_OFFSET);
1753
1754         /* restore BAR5 registers */
1755         regs = ARRAY_SIZE(pg_save_bar5_registers);
1756         tegra_ahci_restore_regs(&pg_save, TEGRA_SATA_BAR5_BASE,
1757                                 pg_save_bar5_registers, regs);
1758
1759         /* restore BAR5 port_registers */
1760         regs = ARRAY_SIZE(pg_save_bar5_port_registers);
1761         for (i = 0; i < TEGRA_AHCI_NUM_PORTS; ++i)
1762                 tegra_ahci_restore_regs(&pg_save, TEGRA_SATA_BAR5_BASE+(0x80*i),
1763                                         pg_save_bar5_port_registers, regs);
1764
1765         /* restore bkdr registers */
1766         regs = ARRAY_SIZE(pg_restore_bar5_bkdr_registers);
1767         tegra_ahci_restore_regs(&pg_save, TEGRA_SATA_CONFIG_BASE,
1768                              pg_restore_bar5_bkdr_registers, regs);
1769
1770         /* and restore BAR5 bkdr per_port registers */
1771         for (i = 0; i < TEGRA_AHCI_NUM_PORTS; ++i) {
1772                 scfg_writel((1 << i), T_SATA0_INDEX_OFFSET);
1773                 regs = ARRAY_SIZE(pg_restore_bar5_bkdr_port_registers);
1774                 tegra_ahci_restore_regs(&pg_save, TEGRA_SATA_CONFIG_BASE,
1775                                         pg_restore_bar5_bkdr_port_registers,
1776                                         regs);
1777         }
1778         scfg_writel(SATA0_NONE_SELECTED, T_SATA0_INDEX_OFFSET);
1779
1780         /* program class code and programming interface for AHCI */
1781         val = scfg_readl(TEGRA_PRIVATE_AHCI_CC_BKDR_OVERRIDE);
1782         val |= TEGRA_PRIVATE_AHCI_CC_BKDR_OVERRIDE_EN;
1783         scfg_writel(val, TEGRA_PRIVATE_AHCI_CC_BKDR_OVERRIDE);
1784         scfg_writel(TEGRA_PRIVATE_AHCI_CC_BKDR_PGM, TEGRA_PRIVATE_AHCI_CC_BKDR);
1785         val &= ~TEGRA_PRIVATE_AHCI_CC_BKDR_OVERRIDE_EN;
1786         scfg_writel(val, TEGRA_PRIVATE_AHCI_CC_BKDR_OVERRIDE);
1787 }
1788 #endif
1789 static u32 tegra_ahci_port_error(struct ata_port *ap)
1790 {
1791         void __iomem *port_mmio = ahci_port_base(ap);
1792         u32 err_status;
1793
1794         err_status = readl(port_mmio + PORT_IRQ_STAT);
1795         /* excludes PhyRdy and Connect Change status */
1796         err_status &= (PORT_IRQ_ERROR & (~(PORT_IRQ_PHYRDY|PORT_IRQ_CONNECT)));
1797         return err_status;
1798 }
1799
1800 static bool tegra_ahci_check_errors(struct ata_host *host)
1801 {       int i;
1802         struct ata_port *ap;
1803         u32 err;
1804
1805         for (i = 0; i < host->n_ports; i++) {
1806                 ap = host->ports[i];
1807                 err = tegra_ahci_port_error(ap);
1808                 if (err) {
1809                         dev_err(host->dev,
1810                                 "pg-chk-err = 0x%08x on port %d\n", err, i);
1811                         return true;
1812                 }
1813         }
1814         return false;
1815 }
1816
1817 void tegra_ahci_iddqlane_config(void)
1818 {
1819         u32 val;
1820         u32 dat;
1821
1822         /* wait for SATA_PADPLL_IDDQ2LANE_SLUMBER_DLY = 3 microseconds. */
1823         val = clk_readl(CLK_RST_SATA_PLL_CFG1_REG);
1824         val &= ~IDDQ2LANE_SLUMBER_DLY_MASK;
1825         val |= IDDQ2LANE_SLUMBER_DLY_3MS;
1826         clk_writel(val, CLK_RST_SATA_PLL_CFG1_REG);
1827
1828         /* wait for delay of IDDQ2LAND_SLUMBER_DLY */
1829         val = clk_readl(CLK_RST_SATA_PLL_CFG1_REG);
1830         dat = (val & IDDQ2LANE_SLUMBER_DLY_MASK) >> IDDQ2LANE_SLUMBER_DLY_SHIFT;
1831         udelay(dat);
1832
1833 }
1834 void tegra_ahci_put_sata_in_iddq()
1835 {
1836         u32 val;
1837         u32 dat;
1838
1839         /*
1840          * Hw wake up is not needed:
1841          * Driver/RM shall place the SATA PHY and SATA PADPLL in IDDQ.
1842          * SATA_PADPLL_RESET_SWCTL =1
1843          * SATA_PADPLL_RESET_OVERRIDE_VALUE=1
1844          * SATA_PADPHY_IDDQ_SWCTL=1
1845          * SATA_PADPHY_IDDQ_OVERRIDE_VALUE=1
1846          */
1847
1848         val = clk_readl(CLK_RST_SATA_PLL_CFG0_REG);
1849         val &= ~(PADPLL_RESET_SWCTL_MASK | PADPLL_RESET_OVERRIDE_VALUE_MASK);
1850         val |= (PADPLL_RESET_SWCTL_ON | PADPLL_RESET_OVERRIDE_VALUE_ON);
1851         clk_writel(val, CLK_RST_SATA_PLL_CFG0_REG);
1852
1853         /* Wait for time specified in SATA_LANE_IDDQ2_PADPLL_IDDQ */
1854         val = clk_readl(CLK_RST_SATA_PLL_CFG1_REG);
1855         dat = (val & IDDQ2LANE_IDDQ_DLY_MASK) >> IDDQ2LANE_IDDQ_DLY_SHIFT;
1856         udelay(dat);
1857
1858         val = xusb_readl(XUSB_PADCTL_IOPHY_PLL_S0_CTL1_0);
1859         val |= (PLL_PWR_OVRD_MASK | PLL_IDDQ_MASK | PLL_RST_MASK);
1860         xusb_writel(val, XUSB_PADCTL_IOPHY_PLL_S0_CTL1_0);
1861
1862 }
1863 void tegra_ahci_clr_clk_rst_cnt_rst_dev(void)
1864 {
1865         u32 val;
1866
1867         val = clk_readl(CLK_RST_CONTROLLER_RST_DEV_V_CLR_0);
1868         val |= (SWR_SATA_OOB_RST | SWR_SATA_RST);
1869         clk_writel(val, CLK_RST_CONTROLLER_RST_DEV_V_CLR_0);
1870
1871         val = clk_readl(CLK_RST_CONTROLLER_RST_DEV_W_CLR_0);
1872         val |= SWR_SATACOLD_RST;
1873         clk_writel(val, CLK_RST_CONTROLLER_RST_DEV_W_CLR_0);
1874
1875 }
1876 void tegra_ahci_set_clk_rst_cnt_rst_dev(void)
1877 {
1878
1879         u32 val;
1880
1881         val = clk_readl(CLK_RST_CONTROLLER_RST_DEV_V_CLR_0);
1882         val &= ~(SWR_SATA_OOB_RST | SWR_SATA_RST);
1883         clk_writel(val, CLK_RST_CONTROLLER_RST_DEV_V_CLR_0);
1884
1885         val = clk_readl(CLK_RST_CONTROLLER_RST_DEV_W_CLR_0);
1886         val &= ~SWR_SATACOLD_RST;
1887         clk_writel(val, CLK_RST_CONTROLLER_RST_DEV_W_CLR_0);
1888
1889 }
1890
1891 static void tegra_ahci_pad_config(void)
1892 {
1893         u32 val;
1894
1895         /* clear SW control of SATA PADPLL, SATA PHY and PLLE */
1896
1897         /* for SATA PHY IDDQ */
1898         val = xusb_readl(XUSB_PADCTL_IOPHY_MISC_PAD_S0_CTL_1_0);
1899         val &= ~(IDDQ_OVRD_MASK | IDDQ_MASK);
1900         xusb_writel(val, XUSB_PADCTL_IOPHY_MISC_PAD_S0_CTL_1_0);
1901
1902         /* for SATA PADPLL IDDQ */
1903         val = xusb_readl(XUSB_PADCTL_IOPHY_PLL_S0_CTL1_0);
1904         val &= ~(PLL_PWR_OVRD_MASK | PLL_IDDQ_MASK | PLL_RST_MASK);
1905         xusb_writel(val, XUSB_PADCTL_IOPHY_PLL_S0_CTL1_0);
1906
1907         /* PLLE related stuff*/
1908
1909         val = clk_readl(CLK_RST_CONTROLLER_PLLE_MISC_0);
1910         val &= ~(T124_PLLE_IDDQ_SWCTL_MASK | PLLE_IDDQ_OVERRIDE_VALUE_MASK);
1911         clk_writel(val, CLK_RST_CONTROLLER_PLLE_MISC_0);
1912
1913         clk_writel(CLK_RST_CONTROLLER_PLLE_MISC_0_VALUE,
1914                         CLK_RST_CONTROLLER_PLLE_MISC_0);
1915
1916         val = clk_readl(CLK_RST_CONTROLLER_PLLE_BASE_0);
1917         val |= PLLE_ENABLE;
1918         clk_writel(val, CLK_RST_CONTROLLER_PLLE_BASE_0);
1919
1920 }
1921
1922 static void tegra_ahci_abort_power_gate(struct ata_host *host)
1923 {
1924         u32 val;
1925         val = pmc_readl(APB_PMC_SATA_PWRGT_0_REG);
1926         val &= ~PG_INFO_MASK;
1927         pmc_writel(val, APB_PMC_SATA_PWRGT_0_REG);
1928 }
1929
1930 static bool tegra_ahci_power_gate(struct ata_host *host)
1931 {
1932         u32 val;
1933         u32 dat;
1934         struct tegra_ahci_host_priv *tegra_hpriv;
1935         int status;
1936
1937         tegra_hpriv = (struct tegra_ahci_host_priv *)host->private_data;
1938
1939         val = pmc_readl(APB_PMC_SATA_PWRGT_0_REG);
1940         val &= ~PG_INFO_MASK;
1941         val |= PG_INFO_ON;
1942         pmc_writel(val, APB_PMC_SATA_PWRGT_0_REG);
1943
1944 #ifdef TEGRA_AHCI_CONTEXT_RESTORE
1945         tegra_ahci_pg_save_registers(host);
1946 #endif
1947         val = xusb_readl(XUSB_PADCTL_IOPHY_MISC_PAD_S0_CTL_1_0);
1948         val |= XUSB_PADCTL_IOPHY_MISC_IDDQ |
1949                 XUSB_PADCTL_IOPHY_MISC_IDDQ_OVRD;
1950         xusb_writel(val, XUSB_PADCTL_IOPHY_MISC_PAD_S0_CTL_1_0);
1951
1952         /*
1953          * Read SATA_AUX_MISC_CNTL_1_0 register L0_RX_IDLE_T_SAX field and
1954          * write that value into same register L0_RX_IDLE_T_NPG field.
1955          * And write 1 to L0_RX_IDLE_T_MUX field.
1956          */
1957         val = misc_readl(SATA_AUX_MISC_CNTL_1_REG);
1958         dat = val;
1959         dat &= L0_RX_IDLE_T_SAX_MASK;
1960         dat >>= L0_RX_IDLE_T_SAX_SHIFT;
1961         dat <<= L0_RX_IDLE_T_NPG_SHIFT;
1962         val &= ~L0_RX_IDLE_T_NPG_MASK;
1963         val |= dat;
1964         val |= L0_RX_IDLE_T_MUX_FROM_APB_MISC;
1965         val |= DEVSLP_OVERRIDE;
1966         misc_writel(val, SATA_AUX_MISC_CNTL_1_REG);
1967
1968         tegra_ahci_set_clk_rst_cnt_rst_dev();
1969
1970         /* abort PG if there are errors occurred */
1971         if (tegra_ahci_check_errors(host)) {
1972                 dev_err(host->dev, "** pg: errors; abort power gating **\n");
1973                 return false;
1974         }
1975         /* make sure all ports have no outstanding commands and are idle. */
1976         if (!tegra_ahci_are_all_ports_idle(host)) {
1977                 dev_err(host->dev, "** pg: cmds; abort power gating **\n");
1978                 return false;
1979         }
1980         tegra_ahci_put_sata_in_iddq();
1981
1982         val = pmc_readl(APBDEV_PMC_PWRGATE_TOGGLE_0);
1983         val |= PARTID_VALUE;
1984         val |= START;
1985         pmc_writel(val, APBDEV_PMC_PWRGATE_TOGGLE_0);
1986
1987         val = pmc_readl(APBDEV_PMC_PWRGATE_STATUS_0);
1988         val &= ~SAX_MASK;
1989         pmc_writel(val, APBDEV_PMC_PWRGATE_STATUS_0);
1990
1991         /* power off the sata */
1992         status = tegra_powergate_partition_with_clk_off(TEGRA_POWERGATE_SATA);
1993         if (status) {
1994                 dev_err(host->dev, "** failed to turn-off SATA (0x%x) **\n",
1995                                    status);
1996                 return false;
1997         }
1998
1999         return true;
2000 }
2001
2002 static bool tegra_ahci_power_un_gate(struct ata_host *host)
2003 {
2004         u32 val;
2005         u32 timeout;
2006         struct tegra_ahci_host_priv *tegra_hpriv;
2007         int status;
2008
2009         tegra_hpriv = (struct tegra_ahci_host_priv *)host->private_data;
2010
2011         tegra_ahci_iddqlane_config();
2012
2013         val = pmc_readl(APBDEV_PMC_PWRGATE_TOGGLE_0);
2014         val |= PARTID_VALUE;
2015         val |= START;
2016         pmc_writel(val, APBDEV_PMC_PWRGATE_TOGGLE_0);
2017
2018         val = pmc_readl(APBDEV_PMC_PWRGATE_STATUS_0);
2019         val |= SAX_MASK;
2020         pmc_writel(val, APBDEV_PMC_PWRGATE_STATUS_0);
2021
2022         status = tegra_unpowergate_partition_with_clk_on(TEGRA_POWERGATE_SATA);
2023         if (status) {
2024                 dev_err(host->dev, "** failed to turn-on SATA (0x%x) **\n",
2025                                    status);
2026                 return false;
2027         }
2028
2029         /* deasset PADPLL and wait until it locks. */
2030         val = clk_readl(CLK_RST_SATA_PLL_CFG0_REG);
2031         val &= ~PADPLL_RESET_OVERRIDE_VALUE_MASK;
2032         clk_writel(val, CLK_RST_SATA_PLL_CFG0_REG);
2033
2034         tegra_ahci_clr_clk_rst_cnt_rst_dev();
2035
2036 #ifdef TEGRA_AHCI_CONTEXT_RESTORE
2037         /* restore registers */
2038         tegra_ahci_pg_restore_registers(host);
2039 #endif
2040         tegra_ahci_pad_config();
2041
2042         /*
2043          * Wait for SATA_AUX_PAD_PLL_CNTL_1_0_LOCKDET to turn 1 with a timeout
2044          * of 15 us.
2045          */
2046         timeout = 15;
2047         while (timeout--) {
2048                 udelay(1);
2049                 val = xusb_readl(XUSB_PADCTL_IOPHY_PLL_S0_CTL1_0);
2050                 if (val & LOCKDET_FIELD)
2051                         break;
2052         }
2053         if (timeout == 0)
2054                 pr_err("%s: SATA_PAD_PLL is not locked in 15us.\n", __func__);
2055
2056         /*
2057          * During the restoration of the registers, the driver would now need to
2058          * restore the register T_SATA0_CFG_POWER_GATE_SSTS_RESTORED after the
2059          * ssts_det, ssts_spd are restored. This register is used to tell the
2060          * controller whether a drive existed earlier or not and move the PHY
2061          * state machines into either HR_slumber or not.
2062          */
2063         val = scfg_readl(T_SATA0_CFG_POWER_GATE);
2064         val &= ~POWER_GATE_SSTS_RESTORED_MASK;
2065         val |= POWER_GATE_SSTS_RESTORED_YES;
2066         scfg_writel(val, T_SATA0_CFG_POWER_GATE);
2067
2068
2069         /*
2070          * Driver needs to switch the rx_idle_t driven source back to from
2071          * Sata controller after SAX is power-ungated.
2072          */
2073         val = misc_readl(SATA_AUX_MISC_CNTL_1_REG);
2074         val &= ~DEVSLP_OVERRIDE;
2075         val &= ~L0_RX_IDLE_T_MUX_MASK;
2076         val |= L0_RX_IDLE_T_MUX_FROM_SATA;
2077         misc_writel(val, SATA_AUX_MISC_CNTL_1_REG);
2078
2079         /* Set the bits in the CAR to allow HW based low power sequencing. */
2080         val = clk_readl(CLK_RST_SATA_PLL_CFG0_REG);
2081         val &= ~PADPLL_RESET_SWCTL_MASK;
2082         clk_writel(val, CLK_RST_SATA_PLL_CFG0_REG);
2083
2084         /*
2085          * power un-gating process is complete by clearing
2086          * APBDEV_PMC_SATA_PWRGT_0.Pmc2sata_pg_info = 0
2087          */
2088         val = pmc_readl(APB_PMC_SATA_PWRGT_0_REG);
2089         val &= ~PG_INFO_MASK;
2090         pmc_writel(val, APB_PMC_SATA_PWRGT_0_REG);
2091
2092         return true;
2093 }
2094
2095 static enum port_idle_status tegra_ahci_is_port_idle(struct ata_port *ap)
2096 {
2097         void __iomem *port_mmio = ahci_port_base(ap);
2098
2099         if (readl(port_mmio + PORT_CMD_ISSUE) ||
2100             readl(port_mmio + PORT_SCR_ACT))
2101                 return PORT_IS_NOT_IDLE;
2102         return PORT_IS_IDLE;
2103 }
2104
2105 /* check if all supported ports are idle (no outstanding commands) */
2106 static bool tegra_ahci_are_all_ports_idle(struct ata_host *host)
2107 {       int i;
2108         struct ata_port *ap;
2109
2110         for (i = 0; i < host->n_ports; i++) {
2111                 ap = host->ports[i];
2112                 if (ap && (tegra_ahci_is_port_idle(ap) == PORT_IS_NOT_IDLE))
2113                         return false;
2114         }
2115         return true;
2116 }
2117
2118 #ifdef CONFIG_TEGRA_SATA_IDLE_POWERGATE
2119 static void tegra_ahci_abort_pad_suspend(struct ata_host *host)
2120 {
2121         /*No implementation*/
2122 }
2123
2124 static bool tegra_ahci_pad_suspend(struct ata_host *host)
2125 {
2126         u32 val;
2127         struct tegra_ahci_host_priv *tegra_hpriv;
2128
2129         tegra_hpriv = (struct tegra_ahci_host_priv *)host->private_data;
2130
2131         /* Set the bits in the CAR to allow HW based low power sequencing. */
2132         val = clk_readl(CLK_RST_SATA_PLL_CFG0_REG);
2133         val |= PADPLL_RESET_SWCTL_MASK;
2134         clk_writel(val, CLK_RST_SATA_PLL_CFG0_REG);
2135
2136         val = xusb_readl(XUSB_PADCTL_IOPHY_MISC_PAD_S0_CTL_1_0);
2137         val |= XUSB_PADCTL_IOPHY_MISC_IDDQ |
2138                 XUSB_PADCTL_IOPHY_MISC_IDDQ_OVRD;
2139         xusb_writel(val, XUSB_PADCTL_IOPHY_MISC_PAD_S0_CTL_1_0);
2140
2141         /* abort PG if there are errors occurred */
2142         if (tegra_ahci_check_errors(host)) {
2143                 dev_err(host->dev, "** pg: errors; abort power gating **\n");
2144                 return false;
2145         }
2146         /* make sure all ports have no outstanding commands and are idle. */
2147         if (!tegra_ahci_are_all_ports_idle(host)) {
2148                 dev_err(host->dev, "** pg: cmds; abort power gating **\n");
2149                 return false;
2150         }
2151         tegra_ahci_put_sata_in_iddq();
2152
2153         val = clk_readl(CLK_RST_SATA_PLL_CFG0_REG);
2154         val |= (SATA_SEQ_PADPLL_PD_INPUT_VALUE |
2155                 SATA_SEQ_LANE_PD_INPUT_VALUE | SATA_SEQ_RESET_INPUT_VALUE);
2156         clk_writel(val, CLK_RST_SATA_PLL_CFG0_REG);
2157
2158         tegra_first_level_clk_gate();
2159
2160         return true;
2161 }
2162
2163 static bool tegra_ahci_pad_resume(struct ata_host *host)
2164 {
2165         u32 val;
2166         u32 timeout;
2167         struct tegra_ahci_host_priv *tegra_hpriv;
2168
2169         tegra_hpriv = (struct tegra_ahci_host_priv *)host->private_data;
2170
2171         val = clk_readl(CLK_RST_SATA_PLL_CFG0_REG);
2172         val &= ~(SATA_SEQ_PADPLL_PD_INPUT_VALUE |
2173                 SATA_SEQ_LANE_PD_INPUT_VALUE | SATA_SEQ_RESET_INPUT_VALUE);
2174         clk_writel(val, CLK_RST_SATA_PLL_CFG0_REG);
2175
2176         if (tegra_first_level_clk_ungate() < 0) {
2177                 pr_err("%s: flcg ungate failed\n", __func__);
2178                 return false;
2179         }
2180
2181         tegra_ahci_iddqlane_config();
2182
2183         /* deasset PADPLL and wait until it locks. */
2184         val = clk_readl(CLK_RST_SATA_PLL_CFG0_REG);
2185         val &= ~PADPLL_RESET_OVERRIDE_VALUE_MASK;
2186         clk_writel(val, CLK_RST_SATA_PLL_CFG0_REG);
2187
2188         tegra_ahci_pad_config();
2189
2190         /*
2191          * Wait for SATA_AUX_PAD_PLL_CNTL_1_0_LOCKDET to turn 1 with a timeout
2192          * of 15 us.
2193          */
2194         timeout = 15;
2195         while (timeout--) {
2196                 udelay(1);
2197                 val = xusb_readl(XUSB_PADCTL_IOPHY_PLL_S0_CTL1_0);
2198                 if (val & LOCKDET_FIELD)
2199                         break;
2200         }
2201         if (timeout == 0)
2202                 pr_err("%s: SATA_PAD_PLL is not locked in 15us.\n", __func__);
2203
2204         /* Set the bits in the CAR to allow HW based low power sequencing. */
2205         val = clk_readl(CLK_RST_SATA_PLL_CFG0_REG);
2206         val &= ~PADPLL_RESET_SWCTL_MASK;
2207         clk_writel(val, CLK_RST_SATA_PLL_CFG0_REG);
2208
2209         return true;
2210 }
2211
2212 static void tegra_ahci_to_add_idle_timer(struct ata_host *host)
2213 {
2214         struct tegra_ahci_host_priv *tegra_hpriv;
2215
2216         tegra_hpriv = (struct tegra_ahci_host_priv *)host->private_data;
2217
2218         /* note: the routine is called from interrupt context */
2219         spin_lock(&host->lock);
2220         /* start idle-timer if all ports have no outstanding commands */
2221         if (tegra_ahci_are_all_ports_idle(host)) {
2222                 /* adjust tegra_ahci_idle_time to minimum if it is too small */
2223                 tegra_ahci_idle_time = max((u32)TEGRA_AHCI_MIN_IDLE_TIME,
2224                                            tegra_ahci_idle_time);
2225                 tegra_hpriv->idle_timer.expires =
2226                         ata_deadline(jiffies, tegra_ahci_idle_time);
2227                 mod_timer(&tegra_hpriv->idle_timer,
2228                           tegra_hpriv->idle_timer.expires);
2229         }
2230         spin_unlock(&host->lock);
2231 }
2232
2233 static void tegra_ahci_idle_timer(unsigned long arg)
2234 {
2235         struct ata_host *host = (void *)arg;
2236         struct tegra_ahci_host_priv *tegra_hpriv;
2237         unsigned long flags;
2238
2239         tegra_hpriv = (struct tegra_ahci_host_priv *)host->private_data;
2240
2241         spin_lock_irqsave(&host->lock, flags);
2242         if (tegra_hpriv->pg_state == SATA_ON)
2243                 tegra_hpriv->pg_state = SATA_GOING_OFF;
2244         else {
2245                 dev_err(host->dev, "idle_timer: bad state (%u)\n",
2246                                 tegra_hpriv->pg_state);
2247                 WARN_ON(1);
2248                 spin_unlock_irqrestore(&host->lock, flags);
2249                 return;
2250         }
2251         spin_unlock_irqrestore(&host->lock, flags);
2252         tegra_ahci_runtime_suspend(tegra_hpriv->dev);
2253 }
2254
2255 static int tegra_ahci_queue_one_qc(struct tegra_ahci_host_priv *tegra_hpriv,
2256                                    struct ata_queued_cmd *qc)
2257 {
2258         struct tegra_qc_list *qc_list;
2259
2260         qc_list = kmalloc(sizeof(struct tegra_qc_list), GFP_ATOMIC);
2261         if (!qc_list) {
2262                 dev_err(tegra_hpriv->dev, "failed to alloc qc_list\n");
2263                 return AC_ERR_SYSTEM;
2264         }
2265         qc_list->qc = qc;
2266         list_add_tail(&(qc_list->list), &(tegra_hpriv->qc_list));
2267         return 0;
2268 }
2269
2270 static void tegra_ahci_dequeue_qcs(struct tegra_ahci_host_priv *tegra_hpriv)
2271 {
2272         struct list_head *list, *next;
2273         struct tegra_qc_list *qc_list;
2274         struct ata_queued_cmd *qc;
2275
2276         /* now qc_issue all qcs in the qc_list */
2277         list_for_each_safe(list, next, &tegra_hpriv->qc_list) {
2278                 qc_list = list_entry(list, struct tegra_qc_list, list);
2279                 qc = qc_list->qc;
2280                 ahci_ops.qc_issue(qc);
2281                 list_del(list);
2282                 kfree(qc_list);
2283         }
2284 }
2285
2286 static unsigned int tegra_ahci_qc_issue(struct ata_queued_cmd *qc)
2287 {
2288         struct ata_port *ap = qc->ap;
2289         struct ata_host *host = ap->host;
2290         struct tegra_ahci_host_priv *tegra_hpriv = host->private_data;
2291
2292         /* stop the idle timer */
2293         if (timer_pending(&tegra_hpriv->idle_timer))
2294                 del_timer_sync(&tegra_hpriv->idle_timer);
2295
2296         /* note: host->lock is locked */
2297         switch (tegra_hpriv->pg_state) {
2298         case SATA_ON:
2299                 /* normal case, issue the qc */
2300                 return ahci_ops.qc_issue(qc);
2301         case SATA_GOING_OFF:
2302         case SATA_ABORT_OFF:
2303                 /* SATA is going OFF, let's abort the suspend */
2304                 dev_dbg(host->dev, "** qc_issue: going OFF **\n");
2305                 tegra_hpriv->pg_state = SATA_ABORT_OFF;
2306                 return tegra_ahci_queue_one_qc(tegra_hpriv, qc);
2307         case SATA_OFF:
2308                 dev_dbg(host->dev, "** qc_issue: request power-up sata **\n");
2309                 queue_work(tegra_ahci_work_q, &tegra_ahci_work);
2310                 tegra_hpriv->pg_state = SATA_GOING_ON;
2311                 /* continue with the following code to queue the qc */
2312         case SATA_GOING_ON:
2313                 return tegra_ahci_queue_one_qc(tegra_hpriv, qc);
2314         default:
2315                 dev_err(host->dev, "** qc_issue: bad state (%u) **\n",
2316                                         tegra_hpriv->pg_state);
2317                 WARN_ON(1);
2318                 return AC_ERR_SYSTEM;
2319         }
2320 }
2321
2322 static int tegra_ahci_hardreset(struct ata_link *link, unsigned int *class,
2323                                 unsigned long deadline)
2324 {
2325         struct ata_port *ap = link->ap;
2326         struct ata_host *host = ap->host;
2327         struct tegra_ahci_host_priv *tegra_hpriv = host->private_data;
2328         unsigned long flags;
2329         int rc;
2330
2331         if (tegra_hpriv->pg_state == SATA_OFF) {
2332                 dev_dbg(host->dev, "** hreset: request power-up sata **\n");
2333                 spin_lock_irqsave(&host->lock, flags);
2334                 rc = tegra_ahci_runtime_resume(tegra_hpriv->dev);
2335                 spin_unlock_irqrestore(&host->lock, flags);
2336                 /* rc == 0 means the request has been run successfully */
2337                 if (rc) {
2338                         dev_err(host->dev, "** hreset: rt_get()=%d **\n", rc);
2339                         WARN_ON(1);
2340                         return AC_ERR_SYSTEM;
2341                 }
2342                 tegra_hpriv->pg_state = SATA_ON;
2343         }
2344
2345         return ahci_ops.hardreset(link, class, deadline);
2346 }
2347
2348 static irqreturn_t tegra_ahci_interrupt(int irq, void *dev_instance)
2349 {
2350         irqreturn_t irq_retval;
2351         u32 val;
2352
2353         val = misc_readl(SATA_AUX_RX_STAT_INT_0);
2354         if (!(val && SATA_RX_STAT_INT_DISABLE)) {
2355                 val |= SATA_RX_STAT_INT_DISABLE;
2356                 misc_writel(val, SATA_AUX_RX_STAT_INT_0);
2357         }
2358
2359         irq_retval = ahci_interrupt(irq, dev_instance);
2360         if (irq_retval == IRQ_NONE)
2361                 return IRQ_NONE;
2362
2363 #ifdef CONFIG_PM
2364         tegra_ahci_to_add_idle_timer((struct ata_host *)dev_instance);
2365 #endif
2366
2367         return irq_retval;
2368 }
2369 #endif
2370 #endif
2371
2372 static int tegra_ahci_remove_one(struct platform_device *pdev)
2373 {
2374         struct ata_host *host = dev_get_drvdata(&pdev->dev);
2375         struct ahci_host_priv *hpriv;
2376
2377         BUG_ON(host == NULL);
2378         BUG_ON(host->iomap[AHCI_PCI_BAR] == NULL);
2379         hpriv = host->private_data;
2380
2381         tegra_ahci_controller_remove(pdev);
2382
2383         devm_iounmap(&pdev->dev, host->iomap[AHCI_PCI_BAR]);
2384         ata_host_detach(host);
2385
2386 #ifdef TEGRA_AHCI_CONTEXT_RESTORE
2387         /* Free PG save/restore area */
2388         devm_kfree(&pdev->dev, ((struct tegra_ahci_host_priv *)hpriv)->pg_save);
2389
2390 #endif
2391 #ifdef CONFIG_TEGRA_SATA_IDLE_POWERGATE
2392         cancel_work_sync(&tegra_ahci_work);
2393         if (tegra_ahci_work_q)
2394                 destroy_workqueue(tegra_ahci_work_q);
2395 #endif
2396
2397         devm_kfree(&pdev->dev, hpriv);
2398
2399         return 0;
2400 }
2401 void tegra_ahci_sata_clk_gate(void)
2402 {
2403         u32 val;
2404
2405         if (!tegra_platform_is_silicon())
2406                 return;
2407
2408         val = clk_readl(CLK_RST_CONTROLLER_RST_DEV_W_SET);
2409         if (val & SET_CEC_RESET)
2410                 clk_writel(0x108, CLK_RST_CONTROLLER_RST_DEV_V_SET);
2411         val = clk_readl(CLK_RST_CONTROLLER_RST_DEV_W_SET);
2412         while (val & SET_CEC_RESET)
2413                 val = clk_readl(CLK_RST_CONTROLLER_RST_DEV_W_SET);
2414 }
2415
2416 static int tegra_ahci_init_one(struct platform_device *pdev)
2417 {
2418         struct device_node *np = pdev->dev.of_node;
2419         struct ata_port_info pi = ahci_port_info;
2420         const struct ata_port_info *ppi[] = { &pi, NULL };
2421         struct device *dev = &pdev->dev;
2422         struct ahci_host_priv *hpriv = NULL;
2423         struct tegra_ahci_host_priv *tegra_hpriv;
2424         struct tegra_ahci_platform_data *ahci_pdata;
2425         struct ata_host *host = NULL;
2426         int n_ports, i, rc = 0;
2427         struct resource *res, *irq_res;
2428         void __iomem *mmio;
2429
2430 #if defined(TEGRA_AHCI_CONTEXT_RESTORE)
2431         u32 save_size;
2432 #endif
2433         irq_handler_t irq_handler = ahci_interrupt;
2434
2435         VPRINTK("ENTER\n");
2436
2437         WARN_ON((int)ATA_MAX_QUEUE > AHCI_MAX_CMDS);
2438
2439         ata_print_version_once(&pdev->dev, DRV_VERSION);
2440
2441         /* Simple resource validation */
2442         if (pdev->num_resources != 3) {
2443                 dev_err(dev, "invalid number of resources\n");
2444                 dev_err(dev, "not enough SATA resources\n");
2445                 return -EINVAL;
2446         }
2447
2448         /* acquire bar resources */
2449         res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
2450         if (res == NULL)
2451                 return -EINVAL;
2452
2453         /* acquire IRQ resource */
2454         irq_res = platform_get_resource(pdev, IORESOURCE_IRQ, 0);
2455         if (irq_res == NULL)
2456                 return -EINVAL;
2457         if (irq_res->start <= 0)
2458                 return -EINVAL;
2459
2460         /* allocate sizeof tegra_ahci_host_priv, which contains extra fields */
2461         hpriv = devm_kzalloc(dev, sizeof(struct tegra_ahci_host_priv),
2462                              GFP_KERNEL);
2463         if (!hpriv) {
2464                 rc = -ENOMEM;
2465                 goto fail;
2466         }
2467
2468 #ifdef CONFIG_TEGRA_SATA_IDLE_POWERGATE
2469         tegra_ahci_work_q = alloc_workqueue("tegra_ahci_work_q",
2470                                         WQ_HIGHPRI | WQ_UNBOUND, 16);
2471         if (!tegra_ahci_work_q) {
2472                 dev_err(dev, "TEGRA WORKQUEUE SATA init failed\n");
2473                 goto fail;
2474         }
2475 #endif
2476
2477         hpriv->flags |= (unsigned long)pi.private_data;
2478         tegra_hpriv = (struct tegra_ahci_host_priv *)hpriv;
2479         tegra_hpriv->dev = dev;
2480         if (np) {
2481                 pdev->dev.coherent_dma_mask = DMA_BIT_MASK(64);
2482                 pdev->dev.dma_mask = &pdev->dev.coherent_dma_mask;
2483                 tegra_hpriv->pexp_gpio =
2484                         of_get_named_gpio(np, "nvidia,pexp-gpio", 0);
2485                 if (!of_property_read_bool(np, "nvidia,enable-sata-port")) {
2486                         dev_err(dev, "Not able to find enable-sata-port property\n");
2487                         tegra_ahci_sata_clk_gate();
2488                         goto fail;
2489                 }
2490                 if (of_property_read_u32(np, "nvidia,sata-connector-type",
2491                         &tegra_hpriv->sata_connector) < 0) {
2492                         tegra_hpriv->sata_connector = MINI_SATA;
2493                 }
2494
2495         } else {
2496                 ahci_pdata = tegra_hpriv->dev->platform_data;
2497                 tegra_hpriv->pexp_gpio = ahci_pdata->pexp_gpio;
2498         }
2499         g_tegra_hpriv = tegra_hpriv;
2500
2501         /* Call tegra init routine */
2502         rc = tegra_ahci_controller_init(tegra_hpriv, 0);
2503         if (rc != 0) {
2504                 dev_err(dev, "TEGRA SATA init failed\n");
2505                 goto fail;
2506         }
2507
2508         /*
2509          * We reserve a table of 6 BARs in tegra_hpriv to store BARs.
2510          * Save the mapped AHCI_PCI_BAR address to the table.
2511          */
2512         mmio = devm_ioremap(dev, res->start, (res->end-res->start+1));
2513         tegra_hpriv->bars_table[AHCI_PCI_BAR] = mmio;
2514         hpriv->mmio = mmio;
2515
2516         /* save initial config */
2517         tegra_ahci_save_initial_config(pdev, hpriv);
2518         dev_dbg(dev, "past save init config\n");
2519
2520         /* prepare host */
2521         if (hpriv->cap & HOST_CAP_NCQ) {
2522                 pi.flags |= ATA_FLAG_NCQ;
2523                 pi.flags |= ATA_FLAG_FPDMA_AA;
2524         }
2525         if (hpriv->cap & HOST_CAP_PMP)
2526                 pi.flags |= ATA_FLAG_PMP;
2527
2528         /* Disable DIPM */
2529         pi.flags |= ATA_FLAG_NO_DIPM;
2530
2531         /*
2532          * CAP.NP sometimes indicate the index of the last enabled
2533          * port, at other times, that of the last possible port, so
2534          * determining the maximum port number requires looking at
2535          * both CAP.NP and port_map.
2536          */
2537         n_ports = max(ahci_nr_ports(hpriv->cap), fls(hpriv->port_map));
2538         host = ata_host_alloc_pinfo(dev, ppi, n_ports);
2539         if (!host) {
2540                 rc = -ENOMEM;
2541                 goto fail;
2542         }
2543         host->private_data = hpriv;
2544         tegra_hpriv->host = host;
2545         host->iomap = tegra_hpriv->bars_table;
2546
2547         if (!(hpriv->cap & HOST_CAP_SSS))
2548                 host->flags |= ATA_HOST_PARALLEL_SCAN;
2549         else
2550                 pr_info("ahci: SSS flag set, parallel bus scan disabled\n");
2551
2552         for (i = 0; i < host->n_ports; i++) {
2553                 struct ata_port *ap = host->ports[i];
2554
2555                 /* set initial link pm policy */
2556                 ap->target_lpm_policy = ATA_LPM_UNKNOWN;
2557
2558                 /* disabled/not-implemented port */
2559                 if (!(hpriv->port_map & (1 << i)))
2560                         ap->ops = &ata_dummy_port_ops;
2561                 else
2562                         ap->target_lpm_policy = ATA_LPM_MIN_POWER;
2563         }
2564
2565         rc = ahci_reset_controller(host);
2566         if (rc) {
2567                 dev_err(dev, "Reset controller failed! (rc=%d)\n", rc);
2568                 goto fail;
2569         }
2570
2571         ahci_init_controller(host);
2572         ahci_print_info(host, "TEGRA-SATA");
2573         dev_dbg(dev, "controller init okay\n");
2574
2575 #if defined(TEGRA_AHCI_CONTEXT_RESTORE)
2576         /* Setup PG save/restore area: */
2577
2578         /* calculate the size */
2579         save_size = ARRAY_SIZE(pg_save_ipfs_registers) +
2580                             ARRAY_SIZE(pg_save_config_registers) +
2581                             ARRAY_SIZE(pg_save_bar5_registers) +
2582                             ARRAY_SIZE(pg_save_bar5_bkdr_registers);
2583
2584         /* and add save port_registers for all the ports */
2585         save_size += TEGRA_AHCI_NUM_PORTS *
2586                      (ARRAY_SIZE(pg_save_config_port_registers) +
2587                       ARRAY_SIZE(pg_save_bar5_port_registers) +
2588                       ARRAY_SIZE(pg_save_bar5_bkdr_port_registers));
2589
2590         /*
2591          * save_size is number of registers times number of bytes per
2592          * register to get total save size.
2593          */
2594         save_size *= sizeof(u32);
2595         tegra_hpriv->pg_save = devm_kzalloc(dev, save_size, GFP_KERNEL);
2596         if (!tegra_hpriv->pg_save) {
2597                 rc = -ENOMEM;
2598                 goto fail;
2599         }
2600 #endif
2601 #ifdef CONFIG_PM
2602 #ifdef CONFIG_TEGRA_SATA_IDLE_POWERGATE
2603         tegra_hpriv->pg_state = SATA_ON;
2604
2605         /* setup sata idle timer */
2606         init_timer_deferrable(&tegra_hpriv->idle_timer);
2607         tegra_hpriv->idle_timer.function = tegra_ahci_idle_timer;
2608         tegra_hpriv->idle_timer.data = (unsigned long)host;
2609
2610         INIT_LIST_HEAD(&tegra_hpriv->qc_list);
2611
2612         /* use our own irq handler */
2613         irq_handler = tegra_ahci_interrupt;
2614 #endif
2615
2616 #endif
2617
2618         rc = ata_host_activate(host, irq_res->start, irq_handler, 0, &ahci_sht);
2619         if (rc == 0)
2620                 return 0;
2621
2622 fail:
2623         if (host) {
2624                 if (host->iomap[AHCI_PCI_BAR])
2625                         devm_iounmap(dev, host->iomap[AHCI_PCI_BAR]);
2626                 ata_host_detach(host);
2627         }
2628         if (hpriv)
2629                 devm_kfree(dev, hpriv);
2630
2631 #ifdef CONFIG_TEGRA_SATA_IDLE_POWERGATE
2632         if (tegra_ahci_work_q)
2633                 destroy_workqueue(tegra_ahci_work_q);
2634 #endif
2635
2636         return rc;
2637 }
2638
2639 static int __init ahci_init(void)
2640 {
2641         return platform_driver_register(&tegra_platform_ahci_driver);
2642 }
2643
2644 static void __exit ahci_exit(void)
2645 {
2646         platform_driver_unregister(&tegra_platform_ahci_driver);
2647 }
2648
2649 #ifdef  CONFIG_DEBUG_FS
2650
2651 #include <linux/debugfs.h>
2652 #include <linux/seq_file.h>
2653
2654 static void dbg_ahci_dump_regs(struct seq_file *s, u32 *ptr, u32 base, u32 regs)
2655 {
2656 #define REGS_PER_LINE   4
2657
2658         u32 i, j;
2659         u32 lines = regs / REGS_PER_LINE;
2660
2661         for (i = 0; i < lines; i++) {
2662                 seq_printf(s, "0x%08x: ", base+(i*16));
2663                 for (j = 0; j < REGS_PER_LINE; ++j) {
2664                         seq_printf(s, "0x%08x ", readl(ptr));
2665                         ++ptr;
2666                 }
2667                 seq_puts(s, "\n");
2668         }
2669 #undef REGS_PER_LINE
2670 }
2671
2672 static int dbg_ahci_dump_show(struct seq_file *s, void *unused)
2673 {
2674         u32 base;
2675         u32 *ptr;
2676         u32 i;
2677
2678         if (!g_tegra_hpriv)
2679                 return 0;
2680
2681 #ifdef CONFIG_TEGRA_SATA_IDLE_POWERGATE
2682                 tegra_ahci_runtime_resume(g_tegra_hpriv->dev);
2683 #endif
2684
2685         base = TEGRA_SATA_CONFIG_BASE;
2686         ptr = (u32 *)IO_TO_VIRT(base);
2687         seq_puts(s, "SATA CONFIG Registers:\n");
2688         seq_puts(s, "----------------------\n");
2689         dbg_ahci_dump_regs(s, ptr, base, 0x200);
2690
2691         base = TEGRA_SATA_BAR5_BASE;
2692         ptr = (u32 *)IO_TO_VIRT(base);
2693         seq_puts(s, "\nAHCI HBA Registers:\n");
2694         seq_puts(s, "-------------------\n");
2695         dbg_ahci_dump_regs(s, ptr, base, 64);
2696
2697         for (i = 0; i < TEGRA_AHCI_NUM_PORTS; ++i) {
2698                 base = TEGRA_SATA_BAR5_BASE + 0x100 + (0x80*i);
2699                 ptr = (u32 *)IO_TO_VIRT(base);
2700                 seq_printf(s, "\nPort %u Registers:\n", i);
2701                 seq_puts(s, "---------------\n");
2702                 dbg_ahci_dump_regs(s, ptr, base, 20);
2703         }
2704
2705 #ifdef  CONFIG_TEGRA_SATA_IDLE_POWERGATE
2706         /* adjust tegra_ahci_idle_time to minimum if it is too small */
2707         tegra_ahci_idle_time = max((u32)TEGRA_AHCI_MIN_IDLE_TIME,
2708                                    tegra_ahci_idle_time);
2709         seq_printf(s, "\nIdle Timeout = %u milli-seconds.\n",
2710                       tegra_ahci_idle_time);
2711 #endif
2712
2713         if (tegra_powergate_is_powered(TEGRA_POWERGATE_SATA))
2714                 seq_puts(s, "\n=== SATA controller is powered on ===\n\n");
2715         else
2716                 seq_puts(s, "\n=== SATA controller is powered off ===\n\n");
2717 #ifdef CONFIG_TEGRA_SATA_IDLE_POWERGATE
2718         tegra_ahci_runtime_suspend(g_tegra_hpriv->dev);
2719 #endif
2720
2721
2722         return 0;
2723 }
2724
2725 static int dbg_ahci_dump_open(struct inode *inode, struct file *file)
2726 {
2727         return single_open(file, dbg_ahci_dump_show, &inode->i_private);
2728 }
2729
2730 static const struct file_operations debug_fops = {
2731         .open           = dbg_ahci_dump_open,
2732         .read           = seq_read,
2733         .llseek         = seq_lseek,
2734         .release        = single_release,
2735 };
2736
2737 static int __init tegra_ahci_dump_debuginit(void)
2738 {
2739         (void) debugfs_create_file("tegra_ahci", S_IRUGO,
2740                                    NULL, NULL, &debug_fops);
2741 #ifdef  CONFIG_TEGRA_SATA_IDLE_POWERGATE
2742         (void) debugfs_create_u32("tegra_ahci_idle_ms", S_IRUGO | S_IXUGO
2743                                         | S_IWUSR | S_IWGRP,
2744                                    NULL, &tegra_ahci_idle_time);
2745 #endif
2746         return 0;
2747 }
2748 late_initcall(tegra_ahci_dump_debuginit);
2749 #endif
2750
2751 MODULE_AUTHOR("NVIDIA");
2752 MODULE_DESCRIPTION("Tegra AHCI SATA low-level driver");
2753 MODULE_LICENSE("GPL v2");
2754 MODULE_VERSION(DRV_VERSION);
2755
2756 module_init(ahci_init);
2757 module_exit(ahci_exit);