2 * ahci-tegra.c - AHCI SATA support for TEGRA AHCI device
4 * Copyright (c) 2011-2015, NVIDIA Corporation. All rights reserved.
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.
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
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/>.
19 * libata documentation is available via 'make {ps|pdf}docs',
20 * as Documentation/DocBook/libata.*
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
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>
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>
54 #include "../../arch/arm/mach-tegra/iomap.h"
56 #define DRV_NAME "tegra-sata"
57 #define DRV_VERSION "1.0"
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)
63 #define AHCI_DBG_PRINT(fmt, arg...) do {} while (0)
66 /* number of AHCI ports */
67 #define TEGRA_AHCI_NUM_PORTS 1
69 /* idle timeout for PM in msec */
70 #define TEGRA_AHCI_MIN_IDLE_TIME 1000
71 #define TEGRA_AHCI_DEFAULT_IDLE_TIME 2000
73 #ifdef CONFIG_TEGRA_SATA_IDLE_POWERGATE
74 static u32 tegra_ahci_idle_time = TEGRA_AHCI_DEFAULT_IDLE_TIME;
77 #define PORT_BASE (TEGRA_SATA_BAR5_BASE + 0x100)
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)
84 #define SATA_INTR_MASK_0_OFFSET 0x188
85 #define IP_INT_MASK (1 << 16)
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)
92 #define AHCI_BAR5_CONFIG_LOCATION 0x24
93 #define TEGRA_SATA_BAR5_INIT_PROGRAM 0xFFFFFFFF
94 #define TEGRA_SATA_BAR5_FINAL_PROGRAM 0x40020000
96 #define FUSE_SATA_CALIB_OFFSET 0x224
97 #define FUSE_SATA_CALIB_MASK 0x3
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)
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)
108 #define T_SATA0_DBG0_OFFSET 0x550
110 #define T_SATA0_INDEX_OFFSET 0x680
111 #define SATA0_NONE_SELECTED 0
112 #define SATA0_CH1_SELECTED (1 << 0)
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)
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)
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
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
139 #define CLAMP_TXCLK_ON_SLUMBER (1 << 13)
140 #define CLAMP_TXCLK_ON_DEVSLP (1 << 24)
141 #define NO_CLAMP_SHUT_DOWN (1 << 3)
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)
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
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
170 #define CLR_CLK_ENB_SATA_OOB (1 << 27)
171 #define CLR_CLK_ENB_SATA (1 << 28)
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)
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
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)
209 #define START (1 < 8)
210 #define PARTID_VALUE 0x8
212 #define SAX_MASK (1 << 8)
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)
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)
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)
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)
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)
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)
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)
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)
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)
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
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)
304 #define SATA_AUX_RX_STAT_INT_0 0x110c
305 #define SATA_RX_STAT_INT_DISABLE (1 << 2)
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)
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)
318 #define PXSSTS_DEVICE_DETECTED (1 << 0)
320 #ifdef CONFIG_TEGRA_SATA_IDLE_POWERGATE
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;
332 enum port_idle_status {
335 PORT_IS_IDLE_NOT_SLUMBER,
347 enum clk_gate_state {
352 enum sata_connectors {
362 char *sata_power_rails[] = {
370 #define NUM_SATA_POWER_RAILS ARRAY_SIZE(sata_power_rails)
372 struct tegra_qc_list {
373 struct list_head list;
374 struct ata_queued_cmd *qc;
378 * tegra_ahci_host_priv is the extension of ahci_host_priv
379 * with extra fields: idle_timer, pg_save, pg_state, etc.
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;
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;
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);
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);
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
440 static struct scsi_host_template ahci_sht = {
441 AHCI_SHT("tegra-sata"),
444 static struct ata_port_operations tegra_ahci_ops = {
445 .inherits = &ahci_ops,
447 #ifdef CONFIG_TEGRA_SATA_IDLE_POWERGATE
448 .qc_issue = tegra_ahci_qc_issue,
449 .hardreset = tegra_ahci_hardreset,
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,
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,
468 static const struct of_device_id of_ahci_tegra_match[] = {
469 { .compatible = "nvidia,tegra114-ahci-sata", },
472 MODULE_DEVICE_TABLE(of, of_ahci_tegra_match);
474 static struct platform_driver tegra_platform_ahci_driver = {
475 .probe = tegra_ahci_init_one,
476 .remove = tegra_ahci_remove_one,
478 .suspend = tegra_ahci_suspend,
479 .resume = tegra_ahci_resume,
483 .owner = THIS_MODULE,
484 .of_match_table = of_match_ptr(of_ahci_tegra_match),
488 struct tegra_ahci_host_priv *g_tegra_hpriv;
490 static inline u32 port_readl(u32 offset)
494 val = readl(IO_ADDRESS(PORT_BASE + offset));
495 AHCI_DBG_PRINT("[0x%x] => 0x%08x\n", PORT_BASE + offset, val);
499 static inline void port_writel(u32 val, u32 offset)
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));
506 static inline u32 bar5_readl(u32 offset)
509 val = readl(IO_ADDRESS(TEGRA_SATA_BAR5_BASE + offset));
510 AHCI_DBG_PRINT("[0x%x] => 0x%08x\n", TEGRA_SATA_BAR5_BASE+offset,
515 static inline void bar5_writel(u32 val, u32 offset)
517 AHCI_DBG_PRINT("[0x%x] <= 0x%08x\n", TEGRA_SATA_BAR5_BASE+offset,
519 writel(val, IO_ADDRESS(TEGRA_SATA_BAR5_BASE + offset));
520 readl(IO_ADDRESS(TEGRA_SATA_BAR5_BASE + offset));
524 static inline u32 xusb_readl(u32 offset)
527 val = readl(IO_ADDRESS(TEGRA_XUSB_PADCTL_BASE + offset));
528 AHCI_DBG_PRINT("[0x%x] => 0x%08x\n", TEGRA_XUSB_PADCTL_BASE+offset,
533 static inline void xusb_writel(u32 val, u32 offset)
535 AHCI_DBG_PRINT("[0x%x] <= 0x%08x\n", TEGRA_XUSB_PADCTL_BASE+offset,
537 writel(val, IO_ADDRESS(TEGRA_XUSB_PADCTL_BASE + offset));
538 readl(IO_ADDRESS(TEGRA_XUSB_PADCTL_BASE + offset));
542 static inline u32 pmc_readl(u32 offset)
545 val = readl(IO_ADDRESS(TEGRA_PMC_BASE + offset));
546 AHCI_DBG_PRINT("[0x%x] => 0x%08x\n", TEGRA_PMC_BASE+offset, val);
550 static inline void pmc_writel(u32 val, u32 offset)
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));
557 static inline u32 clk_readl(u32 offset)
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);
566 static inline void clk_writel(u32 val, u32 offset)
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));
573 static inline u32 misc_readl(u32 offset)
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);
582 static inline void misc_writel(u32 val, u32 offset)
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));
589 static inline u32 sata_readl(u32 offset)
593 val = readl(IO_ADDRESS(TEGRA_SATA_BASE + offset));
594 AHCI_DBG_PRINT("[0x%x] => 0x%08x\n", TEGRA_SATA_BASE+offset, val);
598 static inline void sata_writel(u32 val, u32 offset)
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));
605 static inline u32 scfg_readl(u32 offset)
609 val = readl(IO_ADDRESS(TEGRA_SATA_CONFIG_BASE + offset));
610 AHCI_DBG_PRINT("[0x%x] => 0x%08x\n", TEGRA_SATA_CONFIG_BASE+offset,
615 static inline void scfg_writel(u32 val, u32 offset)
617 AHCI_DBG_PRINT("[0x%x] <= 0x%08x\n", TEGRA_SATA_CONFIG_BASE+offset,
619 writel(val, IO_ADDRESS(TEGRA_SATA_CONFIG_BASE + offset));
620 readl(IO_ADDRESS(TEGRA_SATA_CONFIG_BASE + offset));
623 static inline u32 pictlr_readl(u32 offset)
627 val = readl(IO_ADDRESS(TEGRA_PRIMARY_ICTLR_BASE + offset));
628 AHCI_DBG_PRINT("[0x%x] => 0x%08x\n", TEGRA_PRIMARY_ICTLR_BASE+offset,
633 static inline void pictlr_writel(u32 val, u32 offset)
635 AHCI_DBG_PRINT("[0x%x] <= 0x%08x\n", TEGRA_PRIMARY_ICTLR_BASE+offset,
637 writel(val, IO_ADDRESS(TEGRA_PRIMARY_ICTLR_BASE + offset));
638 readl(IO_ADDRESS(TEGRA_PRIMARY_ICTLR_BASE + offset));
641 static inline u32 fuse_readl(u32 offset)
645 val = readl(IO_ADDRESS(TEGRA_FUSE_BASE + offset));
646 AHCI_DBG_PRINT("[0x%x] => 0x%08x\n", TEGRA_FUSE_BASE+offset, val);
651 /* Sata Pad Cntrl Values */
652 struct sata_pad_cntrl {
659 static const struct sata_pad_cntrl sata_calib_pad_val[] = {
660 { /* SATA_CALIB[1:0] = 00 */
666 { /* SATA_CALIB[1:0] = 01 */
672 { /* SATA_CALIB[1:0] = 10 */
678 { /* SATA_CALIB[1:0] = 11 */
686 #ifdef CONFIG_TEGRA_SATA_IDLE_POWERGATE
687 static void tegra_ahci_work_handler(struct work_struct *work)
689 tegra_ahci_runtime_resume(g_tegra_hpriv->dev);
693 static void tegra_ahci_set_pad_cntrl_regs(
694 struct tegra_ahci_host_priv *tegra_hpriv)
700 calib_val = fuse_readl(FUSE_SATA_CALIB_OFFSET) & FUSE_SATA_CALIB_MASK;
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);
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);
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);
717 for (i = 0; i < TEGRA_AHCI_NUM_PORTS; ++i) {
718 scfg_writel((1 << i), T_SATA0_INDEX_OFFSET);
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);
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);
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);
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);
745 scfg_writel(val, SATA0_CHX_PHY_CTRL11_0);
748 scfg_writel(val, SATA0_CHX_PHY_CTRL2_0);
750 scfg_writel(SATA0_NONE_SELECTED, T_SATA0_INDEX_OFFSET);
753 int tegra_ahci_get_rails(struct regulator *regulators[])
755 struct regulator *reg;
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]);
774 void tegra_ahci_put_rails(struct regulator *regulators[])
778 for (i = 0; i < NUM_SATA_POWER_RAILS; ++i)
779 regulator_put(regulators[i]);
782 int tegra_ahci_power_on_rails(struct regulator *regulators[])
784 struct regulator *reg;
788 for (i = 0; i < NUM_SATA_POWER_RAILS; ++i) {
790 ret = regulator_enable(reg);
792 pr_err("%s: can't enable regulator[%d]\n",
803 int tegra_ahci_power_off_rails(struct regulator *regulators[])
805 struct regulator *reg;
809 for (i = 0; i < NUM_SATA_POWER_RAILS; ++i) {
811 if (!IS_ERR_OR_NULL(reg)) {
812 ret = regulator_disable(reg);
814 pr_err("%s: can't disable regulator[%d]\n",
826 static void tegra_first_level_clk_gate(void)
828 if (g_tegra_hpriv->clk_state == CLK_OFF)
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;
837 static int tegra_first_level_clk_ungate(void)
841 if (g_tegra_hpriv->clk_state == CLK_ON) {
846 if (clk_prepare_enable(g_tegra_hpriv->clk_sata)) {
847 pr_err("%s: unable to enable SATA clock\n", __func__);
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);
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);
864 g_tegra_hpriv->clk_state = CLK_ON;
869 static int tegra_ahci_controller_init(struct tegra_ahci_host_priv *tegra_hpriv,
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;
882 err = tegra_ahci_get_rails(tegra_hpriv->power_rails);
884 pr_err("%s: fails to get rails (%d)\n", __func__, err);
889 err = tegra_ahci_power_on_rails(tegra_hpriv->power_rails);
891 pr_err("%s: fails to power on rails (%d)\n", __func__, err);
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);
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);
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);
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);
924 tegra_hpriv->clk_sata = clk_sata;
925 tegra_hpriv->clk_sata_oob = clk_sata_oob;
926 tegra_hpriv->clk_pllp = clk_pllp;
928 tegra_periph_reset_assert(clk_sata);
929 tegra_periph_reset_assert(clk_sata_oob);
930 tegra_periph_reset_assert(clk_sata_cold);
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);
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);
941 /* Configure SATA clocks */
942 /* Core clock runs at 108MHz */
943 if (clk_set_rate(clk_sata, TEGRA_SATA_CORE_CLOCK_FREQ_HZ)) {
947 /* OOB clock runs at 216MHz */
948 if (clk_set_rate(clk_sata_oob, TEGRA_SATA_OOB_CLOCK_FREQ_HZ)) {
953 if (clk_prepare_enable(clk_sata)) {
954 pr_err("%s: unable to enable SATA clock\n", __func__);
959 if (clk_prepare_enable(clk_sata_oob)) {
960 pr_err("%s: unable to enable SATA OOB clock\n", __func__);
965 tegra_periph_reset_deassert(clk_sata);
966 tegra_periph_reset_deassert(clk_sata_oob);
967 tegra_periph_reset_deassert(clk_sata_cold);
969 tegra_ahci_clr_clk_rst_cnt_rst_dev();
971 if (gpio_is_valid(tegra_hpriv->pexp_gpio)) {
972 val = gpio_request(tegra_hpriv->pexp_gpio,
975 pr_err("failed to allocate Port expander gpio\n");
979 gpio_direction_output(tegra_hpriv->pexp_gpio, 1);
983 pmc_writel(val, APBDEV_PMC_REMOVE_CLAMPING_CMD_0);
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);
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);
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);
1005 #if defined(CONFIG_TEGRA_SILICON_PLATFORM)
1006 err = tegra_unpowergate_partition_with_clk_on(TEGRA_POWERGATE_SATA);
1008 pr_err("%s: ** failed to turn-on SATA (0x%x) **\n",
1015 * place SATA Pad PLL out of reset by writing
1016 * SATA_PADPLL_RST_OVERRIDE_VALUE = 0
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);
1023 * Wait for SATA_AUX_PAD_PLL_CNTL_1_0_LOCKDET to turn 1 with a timeout
1029 val = xusb_readl(XUSB_PADCTL_IOPHY_PLL_S0_CTL1_0);
1030 if (val & LOCKDET_FIELD)
1034 pr_err("%s: AUX_PAD_PLL_CNTL_1 (0x%x) is not locked in 15us.\n",
1037 tegra_ahci_pad_config();
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);
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__);
1053 g_tegra_hpriv->clk_state = CLK_ON;
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);
1059 /* PLLE Programing for SATA */
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);
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);
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);
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);
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);
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);
1091 val = sata_readl(SATA_CONFIGURATION_0_OFFSET);
1093 sata_writel(val, SATA_CONFIGURATION_0_OFFSET);
1095 val = sata_readl(SATA_CONFIGURATION_0_OFFSET);
1096 val |= CLK_OVERRIDE;
1097 sata_writel(val, SATA_CONFIGURATION_0_OFFSET);
1099 /* program sata pad control based on the fuse */
1100 tegra_ahci_set_pad_cntrl_regs(tegra_hpriv);
1103 * clear bit T_SATA0_CFG_PHY_0_USE_7BIT_ALIGN_DET_FOR_SPD of
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);
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);
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);
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.
1126 val = scfg_readl(T_SATA0_DBG0_OFFSET);
1129 scfg_writel(val, T_SATA0_DBG0_OFFSET);
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);
1139 /* Program config space registers: */
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);
1147 /* program bar5 space, by first writing 1's to bar5 register */
1148 scfg_writel(TEGRA_SATA_BAR5_INIT_PROGRAM, AHCI_BAR5_CONFIG_LOCATION);
1150 val = scfg_readl(AHCI_BAR5_CONFIG_LOCATION);
1152 /* then, write the BAR5_FINAL_PROGRAM address */
1153 scfg_writel(TEGRA_SATA_BAR5_FINAL_PROGRAM, AHCI_BAR5_CONFIG_LOCATION);
1155 scfg_readl(AHCI_BAR5_CONFIG_LOCATION);
1157 sata_writel((FPCI_BAR5_0_FINAL_VALUE >> 8),
1158 SATA_FPCI_BAR5_0_OFFSET);
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);
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);
1170 /* set IP_INT_MASK */
1171 val = sata_readl(SATA_INTR_MASK_0_OFFSET);
1173 sata_writel(val, SATA_INTR_MASK_0_OFFSET);
1176 if (!IS_ERR_OR_NULL(clk_pllp))
1178 if (!IS_ERR_OR_NULL(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))
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);
1196 static void tegra_ahci_save_initial_config(struct platform_device *pdev,
1197 struct ahci_host_priv *hpriv)
1199 ahci_save_initial_config(&pdev->dev, hpriv, 0, 0);
1202 static void tegra_ahci_controller_remove(struct platform_device *pdev)
1204 struct ata_host *host = dev_get_drvdata(&pdev->dev);
1205 struct tegra_ahci_host_priv *tegra_hpriv;
1208 tegra_hpriv = (struct tegra_ahci_host_priv *)host->private_data;
1211 /* call tegra_ahci_controller_suspend() to power-down the SATA */
1212 status = tegra_ahci_controller_suspend(pdev);
1214 dev_err(host->dev, "remove: error suspend SATA (0x%x)\n",
1217 /* power off the sata */
1218 status = tegra_powergate_partition_with_clk_off(TEGRA_POWERGATE_SATA);
1220 dev_err(host->dev, "remove: error turn-off SATA (0x%x)\n",
1222 tegra_ahci_power_off_rails(tegra_hpriv->power_rails);
1225 /* return system resources */
1226 tegra_ahci_put_rails(tegra_hpriv->power_rails);
1230 static int tegra_ahci_controller_suspend(struct platform_device *pdev)
1232 struct ata_host *host = dev_get_drvdata(&pdev->dev);
1233 struct tegra_ahci_host_priv *tegra_hpriv;
1234 unsigned long flags;
1236 tegra_hpriv = (struct tegra_ahci_host_priv *)host->private_data;
1238 /* stop the idle timer */
1239 if (timer_pending(&tegra_hpriv->idle_timer))
1240 del_timer_sync(&tegra_hpriv->idle_timer);
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");
1248 dev_dbg(host->dev, "suspend: power gating SATA...\n");
1249 pg_ok = tegra_ahci_power_gate(host);
1251 tegra_hpriv->pg_state = SATA_OFF;
1252 dev_dbg(host->dev, "suspend: SATA is power gated\n");
1254 tegra_ahci_abort_power_gate(host);
1255 spin_unlock_irqrestore(&host->lock, flags);
1260 if (gpio_is_valid(tegra_hpriv->pexp_gpio))
1261 gpio_free(tegra_hpriv->pexp_gpio);
1262 spin_unlock_irqrestore(&host->lock, flags);
1264 tegra_first_level_clk_gate();
1266 return tegra_ahci_power_off_rails(tegra_hpriv->power_rails);
1269 static int tegra_ahci_controller_resume(struct platform_device *pdev)
1271 struct ata_host *host = dev_get_drvdata(&pdev->dev);
1272 struct tegra_ahci_host_priv *tegra_hpriv;
1273 unsigned long flags;
1276 tegra_hpriv = (struct tegra_ahci_host_priv *)host->private_data;
1278 err = tegra_ahci_power_on_rails(tegra_hpriv->power_rails);
1280 pr_err("%s: fails to power on rails (%d)\n", __func__, err);
1284 err = tegra_first_level_clk_ungate();
1286 pr_err("%s: flcg ungate failed\n", __func__);
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");
1294 dev_dbg(host->dev, "resume: powering on SATA...\n");
1295 tegra_ahci_power_un_gate(host);
1296 tegra_hpriv->pg_state = SATA_ON;
1298 spin_unlock_irqrestore(&host->lock, flags);
1299 tegra_first_level_clk_gate();
1304 static int tegra_ahci_suspend(struct platform_device *pdev, pm_message_t mesg)
1306 struct ata_host *host = dev_get_drvdata(&pdev->dev);
1307 void __iomem *mmio = host->iomap[AHCI_PCI_BAR];
1311 dev_dbg(host->dev, "** entering %s: **\n", __func__);
1312 if (mesg.event & PM_EVENT_SLEEP) {
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.
1318 ctl = readl(mmio + HOST_CTL);
1319 ctl &= ~HOST_IRQ_EN;
1320 writel(ctl, mmio + HOST_CTL);
1321 readl(mmio + HOST_CTL); /* flush */
1324 rc = ata_host_suspend(host, mesg);
1328 return tegra_ahci_controller_suspend(pdev);
1331 static int tegra_ahci_resume(struct platform_device *pdev)
1333 struct ata_host *host = dev_get_drvdata(&pdev->dev);
1337 dev_dbg(host->dev, "** entering %s: **\n", __func__);
1338 rc = tegra_ahci_controller_resume(pdev);
1342 rc = tegra_ahci_controller_init(g_tegra_hpriv, 1);
1344 dev_err(host->dev, "TEGRA SATA init failed in resume\n");
1348 if (pdev->dev.power.power_state.event == PM_EVENT_SUSPEND) {
1349 rc = ahci_reset_controller(host);
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);
1359 ahci_init_controller(host);
1362 ata_host_resume(host);
1367 #ifdef CONFIG_TEGRA_SATA_IDLE_POWERGATE
1368 static int tegra_ahci_runtime_suspend(struct device *dev)
1370 struct ata_host *host;
1371 struct tegra_ahci_host_priv *tegra_hpriv;
1373 unsigned long flags;
1376 host = dev_get_drvdata(dev);
1377 tegra_hpriv = (struct tegra_ahci_host_priv *)host->private_data;
1379 spin_lock_irqsave(&host->lock, flags);
1381 switch (tegra_hpriv->pg_state) {
1383 dev_dbg(dev, "** rt-suspend: already power gated **\n");
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);
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);
1402 pg_ok = tegra_ahci_pad_suspend(host);
1404 dev_dbg(dev, "** rt-suspend: done **\n");
1406 tegra_hpriv->pg_state = SATA_OFF;
1408 dev_err(dev, "** rt-suspend: abort pg **\n");
1409 #ifdef TEGRA_AHCI_CONTEXT_RESTORE
1410 tegra_ahci_abort_power_gate(host);
1412 tegra_ahci_abort_pad_suspend(host);
1414 tegra_hpriv->pg_state = SATA_ON;
1418 dev_dbg(dev, "** rt-suspend: port not idle (%u) **\n",
1419 tegra_hpriv->pg_state);
1426 dev_err(dev, "** rt-suspend: bad state (%u) **\n",
1427 tegra_hpriv->pg_state);
1434 spin_unlock_irqrestore(&host->lock, flags);
1439 static int tegra_ahci_runtime_resume(struct device *dev)
1441 struct ata_host *host;
1442 struct tegra_ahci_host_priv *tegra_hpriv;
1445 host = dev_get_drvdata(dev);
1446 tegra_hpriv = (struct tegra_ahci_host_priv *)host->private_data;
1448 if (tegra_hpriv->pg_state == SATA_ON) {
1449 dev_dbg(dev, "** rt-resume: already power ungated **\n");
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);
1460 tegra_ahci_pad_resume(host);
1462 dev_dbg(dev, "** rt-resume: done **\n");
1463 tegra_hpriv->pg_state = SATA_ON;
1465 /* now qc_issue all qcs in the qc_list */
1466 tegra_ahci_dequeue_qcs(tegra_hpriv);
1469 dev_err(dev, "** rt-resume: bad state (%u) **\n",
1470 tegra_hpriv->pg_state);
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 */
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 */
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.
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 */
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 */
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 */
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 */
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 */
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 */
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 */
1622 static void tegra_ahci_save_regs(u32 **save_addr,
1628 u32 *dest = *save_addr;
1629 void __iomem *base = IO_ADDRESS(reg_base);
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);
1639 static void tegra_ahci_restore_regs(void **save_addr,
1645 u32 *src = *save_addr;
1646 void __iomem *base = IO_ADDRESS(reg_base);
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);
1656 static void tegra_ahci_pg_save_registers(struct ata_host *host)
1658 struct tegra_ahci_host_priv *tegra_hpriv;
1663 tegra_hpriv = (struct tegra_ahci_host_priv *)host->private_data;
1664 pg_save = tegra_hpriv->pg_save;
1667 * Driver should save/restore the registers in the order of
1668 * IPFS, CFG, Ext CFG, BAR5.
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 */
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);
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);
1689 scfg_writel(SATA0_NONE_SELECTED, T_SATA0_INDEX_OFFSET);
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);
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);
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);
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,
1715 scfg_writel(SATA0_NONE_SELECTED, T_SATA0_INDEX_OFFSET);
1718 static void tegra_ahci_pg_restore_registers(struct ata_host *host)
1720 struct tegra_ahci_host_priv *tegra_hpriv;
1725 tegra_hpriv = (struct tegra_ahci_host_priv *)host->private_data;
1726 pg_save = tegra_hpriv->pg_save;
1729 * Driver should restore the registers in the order of
1730 * IPFS, CFG, Ext CFG, BAR5.
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 */
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);
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,
1752 scfg_writel(SATA0_NONE_SELECTED, T_SATA0_INDEX_OFFSET);
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);
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);
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);
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,
1778 scfg_writel(SATA0_NONE_SELECTED, T_SATA0_INDEX_OFFSET);
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);
1789 static u32 tegra_ahci_port_error(struct ata_port *ap)
1791 void __iomem *port_mmio = ahci_port_base(ap);
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)));
1800 static bool tegra_ahci_check_errors(struct ata_host *host)
1802 struct ata_port *ap;
1805 for (i = 0; i < host->n_ports; i++) {
1806 ap = host->ports[i];
1807 err = tegra_ahci_port_error(ap);
1810 "pg-chk-err = 0x%08x on port %d\n", err, i);
1817 void tegra_ahci_iddqlane_config(void)
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);
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;
1834 void tegra_ahci_put_sata_in_iddq()
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
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);
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;
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);
1863 void tegra_ahci_clr_clk_rst_cnt_rst_dev(void)
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);
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);
1876 void tegra_ahci_set_clk_rst_cnt_rst_dev(void)
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);
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);
1891 static void tegra_ahci_pad_config(void)
1895 /* clear SW control of SATA PADPLL, SATA PHY and PLLE */
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);
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);
1907 /* PLLE related stuff*/
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);
1913 clk_writel(CLK_RST_CONTROLLER_PLLE_MISC_0_VALUE,
1914 CLK_RST_CONTROLLER_PLLE_MISC_0);
1916 val = clk_readl(CLK_RST_CONTROLLER_PLLE_BASE_0);
1918 clk_writel(val, CLK_RST_CONTROLLER_PLLE_BASE_0);
1922 static void tegra_ahci_abort_power_gate(struct ata_host *host)
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);
1930 static bool tegra_ahci_power_gate(struct ata_host *host)
1934 struct tegra_ahci_host_priv *tegra_hpriv;
1937 tegra_hpriv = (struct tegra_ahci_host_priv *)host->private_data;
1939 val = pmc_readl(APB_PMC_SATA_PWRGT_0_REG);
1940 val &= ~PG_INFO_MASK;
1942 pmc_writel(val, APB_PMC_SATA_PWRGT_0_REG);
1944 #ifdef TEGRA_AHCI_CONTEXT_RESTORE
1945 tegra_ahci_pg_save_registers(host);
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);
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.
1957 val = misc_readl(SATA_AUX_MISC_CNTL_1_REG);
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;
1964 val |= L0_RX_IDLE_T_MUX_FROM_APB_MISC;
1965 val |= DEVSLP_OVERRIDE;
1966 misc_writel(val, SATA_AUX_MISC_CNTL_1_REG);
1968 tegra_ahci_set_clk_rst_cnt_rst_dev();
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");
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");
1980 tegra_ahci_put_sata_in_iddq();
1982 val = pmc_readl(APBDEV_PMC_PWRGATE_TOGGLE_0);
1983 val |= PARTID_VALUE;
1985 pmc_writel(val, APBDEV_PMC_PWRGATE_TOGGLE_0);
1987 val = pmc_readl(APBDEV_PMC_PWRGATE_STATUS_0);
1989 pmc_writel(val, APBDEV_PMC_PWRGATE_STATUS_0);
1991 /* power off the sata */
1992 status = tegra_powergate_partition_with_clk_off(TEGRA_POWERGATE_SATA);
1994 dev_err(host->dev, "** failed to turn-off SATA (0x%x) **\n",
2002 static bool tegra_ahci_power_un_gate(struct ata_host *host)
2006 struct tegra_ahci_host_priv *tegra_hpriv;
2009 tegra_hpriv = (struct tegra_ahci_host_priv *)host->private_data;
2011 tegra_ahci_iddqlane_config();
2013 val = pmc_readl(APBDEV_PMC_PWRGATE_TOGGLE_0);
2014 val |= PARTID_VALUE;
2016 pmc_writel(val, APBDEV_PMC_PWRGATE_TOGGLE_0);
2018 val = pmc_readl(APBDEV_PMC_PWRGATE_STATUS_0);
2020 pmc_writel(val, APBDEV_PMC_PWRGATE_STATUS_0);
2022 status = tegra_unpowergate_partition_with_clk_on(TEGRA_POWERGATE_SATA);
2024 dev_err(host->dev, "** failed to turn-on SATA (0x%x) **\n",
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);
2034 tegra_ahci_clr_clk_rst_cnt_rst_dev();
2036 #ifdef TEGRA_AHCI_CONTEXT_RESTORE
2037 /* restore registers */
2038 tegra_ahci_pg_restore_registers(host);
2040 tegra_ahci_pad_config();
2043 * Wait for SATA_AUX_PAD_PLL_CNTL_1_0_LOCKDET to turn 1 with a timeout
2049 val = xusb_readl(XUSB_PADCTL_IOPHY_PLL_S0_CTL1_0);
2050 if (val & LOCKDET_FIELD)
2054 pr_err("%s: SATA_PAD_PLL is not locked in 15us.\n", __func__);
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.
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);
2070 * Driver needs to switch the rx_idle_t driven source back to from
2071 * Sata controller after SAX is power-ungated.
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);
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);
2085 * power un-gating process is complete by clearing
2086 * APBDEV_PMC_SATA_PWRGT_0.Pmc2sata_pg_info = 0
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);
2095 static enum port_idle_status tegra_ahci_is_port_idle(struct ata_port *ap)
2097 void __iomem *port_mmio = ahci_port_base(ap);
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;
2105 /* check if all supported ports are idle (no outstanding commands) */
2106 static bool tegra_ahci_are_all_ports_idle(struct ata_host *host)
2108 struct ata_port *ap;
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))
2118 #ifdef CONFIG_TEGRA_SATA_IDLE_POWERGATE
2119 static void tegra_ahci_abort_pad_suspend(struct ata_host *host)
2121 /*No implementation*/
2124 static bool tegra_ahci_pad_suspend(struct ata_host *host)
2127 struct tegra_ahci_host_priv *tegra_hpriv;
2129 tegra_hpriv = (struct tegra_ahci_host_priv *)host->private_data;
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);
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);
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");
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");
2151 tegra_ahci_put_sata_in_iddq();
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);
2158 tegra_first_level_clk_gate();
2163 static bool tegra_ahci_pad_resume(struct ata_host *host)
2167 struct tegra_ahci_host_priv *tegra_hpriv;
2169 tegra_hpriv = (struct tegra_ahci_host_priv *)host->private_data;
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);
2176 if (tegra_first_level_clk_ungate() < 0) {
2177 pr_err("%s: flcg ungate failed\n", __func__);
2181 tegra_ahci_iddqlane_config();
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);
2188 tegra_ahci_pad_config();
2191 * Wait for SATA_AUX_PAD_PLL_CNTL_1_0_LOCKDET to turn 1 with a timeout
2197 val = xusb_readl(XUSB_PADCTL_IOPHY_PLL_S0_CTL1_0);
2198 if (val & LOCKDET_FIELD)
2202 pr_err("%s: SATA_PAD_PLL is not locked in 15us.\n", __func__);
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);
2212 static void tegra_ahci_to_add_idle_timer(struct ata_host *host)
2214 struct tegra_ahci_host_priv *tegra_hpriv;
2216 tegra_hpriv = (struct tegra_ahci_host_priv *)host->private_data;
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);
2230 spin_unlock(&host->lock);
2233 static void tegra_ahci_idle_timer(unsigned long arg)
2235 struct ata_host *host = (void *)arg;
2236 struct tegra_ahci_host_priv *tegra_hpriv;
2237 unsigned long flags;
2239 tegra_hpriv = (struct tegra_ahci_host_priv *)host->private_data;
2241 spin_lock_irqsave(&host->lock, flags);
2242 if (tegra_hpriv->pg_state == SATA_ON)
2243 tegra_hpriv->pg_state = SATA_GOING_OFF;
2245 dev_err(host->dev, "idle_timer: bad state (%u)\n",
2246 tegra_hpriv->pg_state);
2248 spin_unlock_irqrestore(&host->lock, flags);
2251 spin_unlock_irqrestore(&host->lock, flags);
2252 tegra_ahci_runtime_suspend(tegra_hpriv->dev);
2255 static int tegra_ahci_queue_one_qc(struct tegra_ahci_host_priv *tegra_hpriv,
2256 struct ata_queued_cmd *qc)
2258 struct tegra_qc_list *qc_list;
2260 qc_list = kmalloc(sizeof(struct tegra_qc_list), GFP_ATOMIC);
2262 dev_err(tegra_hpriv->dev, "failed to alloc qc_list\n");
2263 return AC_ERR_SYSTEM;
2266 list_add_tail(&(qc_list->list), &(tegra_hpriv->qc_list));
2270 static void tegra_ahci_dequeue_qcs(struct tegra_ahci_host_priv *tegra_hpriv)
2272 struct list_head *list, *next;
2273 struct tegra_qc_list *qc_list;
2274 struct ata_queued_cmd *qc;
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);
2280 ahci_ops.qc_issue(qc);
2286 static unsigned int tegra_ahci_qc_issue(struct ata_queued_cmd *qc)
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;
2292 /* stop the idle timer */
2293 if (timer_pending(&tegra_hpriv->idle_timer))
2294 del_timer_sync(&tegra_hpriv->idle_timer);
2296 /* note: host->lock is locked */
2297 switch (tegra_hpriv->pg_state) {
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);
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 */
2313 return tegra_ahci_queue_one_qc(tegra_hpriv, qc);
2315 dev_err(host->dev, "** qc_issue: bad state (%u) **\n",
2316 tegra_hpriv->pg_state);
2318 return AC_ERR_SYSTEM;
2322 static int tegra_ahci_hardreset(struct ata_link *link, unsigned int *class,
2323 unsigned long deadline)
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;
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 */
2338 dev_err(host->dev, "** hreset: rt_get()=%d **\n", rc);
2340 return AC_ERR_SYSTEM;
2342 tegra_hpriv->pg_state = SATA_ON;
2345 return ahci_ops.hardreset(link, class, deadline);
2348 static irqreturn_t tegra_ahci_interrupt(int irq, void *dev_instance)
2350 irqreturn_t irq_retval;
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);
2359 irq_retval = ahci_interrupt(irq, dev_instance);
2360 if (irq_retval == IRQ_NONE)
2364 tegra_ahci_to_add_idle_timer((struct ata_host *)dev_instance);
2372 static int tegra_ahci_remove_one(struct platform_device *pdev)
2374 struct ata_host *host = dev_get_drvdata(&pdev->dev);
2375 struct ahci_host_priv *hpriv;
2377 BUG_ON(host == NULL);
2378 BUG_ON(host->iomap[AHCI_PCI_BAR] == NULL);
2379 hpriv = host->private_data;
2381 tegra_ahci_controller_remove(pdev);
2383 devm_iounmap(&pdev->dev, host->iomap[AHCI_PCI_BAR]);
2384 ata_host_detach(host);
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);
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);
2397 devm_kfree(&pdev->dev, hpriv);
2401 void tegra_ahci_sata_clk_gate(void)
2405 if (!tegra_platform_is_silicon())
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);
2416 static int tegra_ahci_init_one(struct platform_device *pdev)
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;
2430 #if defined(TEGRA_AHCI_CONTEXT_RESTORE)
2433 irq_handler_t irq_handler = ahci_interrupt;
2437 WARN_ON((int)ATA_MAX_QUEUE > AHCI_MAX_CMDS);
2439 ata_print_version_once(&pdev->dev, DRV_VERSION);
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");
2448 /* acquire bar resources */
2449 res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
2453 /* acquire IRQ resource */
2454 irq_res = platform_get_resource(pdev, IORESOURCE_IRQ, 0);
2455 if (irq_res == NULL)
2457 if (irq_res->start <= 0)
2460 /* allocate sizeof tegra_ahci_host_priv, which contains extra fields */
2461 hpriv = devm_kzalloc(dev, sizeof(struct tegra_ahci_host_priv),
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");
2477 hpriv->flags |= (unsigned long)pi.private_data;
2478 tegra_hpriv = (struct tegra_ahci_host_priv *)hpriv;
2479 tegra_hpriv->dev = dev;
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();
2490 if (of_property_read_u32(np, "nvidia,sata-connector-type",
2491 &tegra_hpriv->sata_connector) < 0) {
2492 tegra_hpriv->sata_connector = MINI_SATA;
2496 ahci_pdata = tegra_hpriv->dev->platform_data;
2497 tegra_hpriv->pexp_gpio = ahci_pdata->pexp_gpio;
2499 g_tegra_hpriv = tegra_hpriv;
2501 /* Call tegra init routine */
2502 rc = tegra_ahci_controller_init(tegra_hpriv, 0);
2504 dev_err(dev, "TEGRA SATA init failed\n");
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.
2512 mmio = devm_ioremap(dev, res->start, (res->end-res->start+1));
2513 tegra_hpriv->bars_table[AHCI_PCI_BAR] = mmio;
2516 /* save initial config */
2517 tegra_ahci_save_initial_config(pdev, hpriv);
2518 dev_dbg(dev, "past save init config\n");
2521 if (hpriv->cap & HOST_CAP_NCQ) {
2522 pi.flags |= ATA_FLAG_NCQ;
2523 pi.flags |= ATA_FLAG_FPDMA_AA;
2525 if (hpriv->cap & HOST_CAP_PMP)
2526 pi.flags |= ATA_FLAG_PMP;
2529 pi.flags |= ATA_FLAG_NO_DIPM;
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.
2537 n_ports = max(ahci_nr_ports(hpriv->cap), fls(hpriv->port_map));
2538 host = ata_host_alloc_pinfo(dev, ppi, n_ports);
2543 host->private_data = hpriv;
2544 tegra_hpriv->host = host;
2545 host->iomap = tegra_hpriv->bars_table;
2547 if (!(hpriv->cap & HOST_CAP_SSS))
2548 host->flags |= ATA_HOST_PARALLEL_SCAN;
2550 pr_info("ahci: SSS flag set, parallel bus scan disabled\n");
2552 for (i = 0; i < host->n_ports; i++) {
2553 struct ata_port *ap = host->ports[i];
2555 /* set initial link pm policy */
2556 ap->target_lpm_policy = ATA_LPM_UNKNOWN;
2558 /* disabled/not-implemented port */
2559 if (!(hpriv->port_map & (1 << i)))
2560 ap->ops = &ata_dummy_port_ops;
2562 ap->target_lpm_policy = ATA_LPM_MIN_POWER;
2565 rc = ahci_reset_controller(host);
2567 dev_err(dev, "Reset controller failed! (rc=%d)\n", rc);
2571 ahci_init_controller(host);
2572 ahci_print_info(host, "TEGRA-SATA");
2573 dev_dbg(dev, "controller init okay\n");
2575 #if defined(TEGRA_AHCI_CONTEXT_RESTORE)
2576 /* Setup PG save/restore area: */
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);
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));
2591 * save_size is number of registers times number of bytes per
2592 * register to get total save size.
2594 save_size *= sizeof(u32);
2595 tegra_hpriv->pg_save = devm_kzalloc(dev, save_size, GFP_KERNEL);
2596 if (!tegra_hpriv->pg_save) {
2602 #ifdef CONFIG_TEGRA_SATA_IDLE_POWERGATE
2603 tegra_hpriv->pg_state = SATA_ON;
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;
2610 INIT_LIST_HEAD(&tegra_hpriv->qc_list);
2612 /* use our own irq handler */
2613 irq_handler = tegra_ahci_interrupt;
2618 rc = ata_host_activate(host, irq_res->start, irq_handler, 0, &ahci_sht);
2624 if (host->iomap[AHCI_PCI_BAR])
2625 devm_iounmap(dev, host->iomap[AHCI_PCI_BAR]);
2626 ata_host_detach(host);
2629 devm_kfree(dev, hpriv);
2631 #ifdef CONFIG_TEGRA_SATA_IDLE_POWERGATE
2632 if (tegra_ahci_work_q)
2633 destroy_workqueue(tegra_ahci_work_q);
2639 static int __init ahci_init(void)
2641 return platform_driver_register(&tegra_platform_ahci_driver);
2644 static void __exit ahci_exit(void)
2646 platform_driver_unregister(&tegra_platform_ahci_driver);
2649 #ifdef CONFIG_DEBUG_FS
2651 #include <linux/debugfs.h>
2652 #include <linux/seq_file.h>
2654 static void dbg_ahci_dump_regs(struct seq_file *s, u32 *ptr, u32 base, u32 regs)
2656 #define REGS_PER_LINE 4
2659 u32 lines = regs / REGS_PER_LINE;
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));
2669 #undef REGS_PER_LINE
2672 static int dbg_ahci_dump_show(struct seq_file *s, void *unused)
2681 #ifdef CONFIG_TEGRA_SATA_IDLE_POWERGATE
2682 tegra_ahci_runtime_resume(g_tegra_hpriv->dev);
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);
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);
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);
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);
2713 if (tegra_powergate_is_powered(TEGRA_POWERGATE_SATA))
2714 seq_puts(s, "\n=== SATA controller is powered on ===\n\n");
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);
2725 static int dbg_ahci_dump_open(struct inode *inode, struct file *file)
2727 return single_open(file, dbg_ahci_dump_show, &inode->i_private);
2730 static const struct file_operations debug_fops = {
2731 .open = dbg_ahci_dump_open,
2733 .llseek = seq_lseek,
2734 .release = single_release,
2737 static int __init tegra_ahci_dump_debuginit(void)
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);
2748 late_initcall(tegra_ahci_dump_debuginit);
2751 MODULE_AUTHOR("NVIDIA");
2752 MODULE_DESCRIPTION("Tegra AHCI SATA low-level driver");
2753 MODULE_LICENSE("GPL v2");
2754 MODULE_VERSION(DRV_VERSION);
2756 module_init(ahci_init);
2757 module_exit(ahci_exit);