tegra_ahcisata.c revision 1.12 1 1.12 skrll /* $NetBSD: tegra_ahcisata.c,v 1.12 2018/12/14 12:29:22 skrll Exp $ */
2 1.1 jmcneill
3 1.1 jmcneill /*-
4 1.1 jmcneill * Copyright (c) 2015 Jared D. McNeill <jmcneill (at) invisible.ca>
5 1.1 jmcneill * All rights reserved.
6 1.1 jmcneill *
7 1.1 jmcneill * Redistribution and use in source and binary forms, with or without
8 1.1 jmcneill * modification, are permitted provided that the following conditions
9 1.1 jmcneill * are met:
10 1.1 jmcneill * 1. Redistributions of source code must retain the above copyright
11 1.1 jmcneill * notice, this list of conditions and the following disclaimer.
12 1.1 jmcneill * 2. Redistributions in binary form must reproduce the above copyright
13 1.1 jmcneill * notice, this list of conditions and the following disclaimer in the
14 1.1 jmcneill * documentation and/or other materials provided with the distribution.
15 1.1 jmcneill *
16 1.1 jmcneill * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
17 1.1 jmcneill * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
18 1.1 jmcneill * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
19 1.1 jmcneill * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
20 1.1 jmcneill * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
21 1.1 jmcneill * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
22 1.1 jmcneill * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED
23 1.1 jmcneill * AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
24 1.1 jmcneill * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
25 1.1 jmcneill * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
26 1.1 jmcneill * SUCH DAMAGE.
27 1.1 jmcneill */
28 1.1 jmcneill
29 1.1 jmcneill #include <sys/cdefs.h>
30 1.12 skrll __KERNEL_RCSID(0, "$NetBSD: tegra_ahcisata.c,v 1.12 2018/12/14 12:29:22 skrll Exp $");
31 1.1 jmcneill
32 1.1 jmcneill #include <sys/param.h>
33 1.1 jmcneill #include <sys/bus.h>
34 1.1 jmcneill #include <sys/device.h>
35 1.1 jmcneill #include <sys/intr.h>
36 1.1 jmcneill #include <sys/systm.h>
37 1.1 jmcneill #include <sys/kernel.h>
38 1.1 jmcneill
39 1.1 jmcneill #include <dev/ata/atavar.h>
40 1.1 jmcneill #include <dev/ic/ahcisatavar.h>
41 1.1 jmcneill
42 1.1 jmcneill #include <arm/nvidia/tegra_var.h>
43 1.9 jmcneill #include <arm/nvidia/tegra_pmcreg.h>
44 1.3 jmcneill #include <arm/nvidia/tegra_ahcisatareg.h>
45 1.11 jmcneill #include <arm/nvidia/tegra_xusbpad.h>
46 1.3 jmcneill
47 1.8 jmcneill #include <dev/fdt/fdtvar.h>
48 1.8 jmcneill
49 1.3 jmcneill #define TEGRA_AHCISATA_OFFSET 0x7000
50 1.1 jmcneill
51 1.1 jmcneill static int tegra_ahcisata_match(device_t, cfdata_t, void *);
52 1.1 jmcneill static void tegra_ahcisata_attach(device_t, device_t, void *);
53 1.1 jmcneill
54 1.1 jmcneill struct tegra_ahcisata_softc {
55 1.1 jmcneill struct ahci_softc sc;
56 1.3 jmcneill bus_space_tag_t sc_bst;
57 1.3 jmcneill bus_space_handle_t sc_bsh;
58 1.1 jmcneill void *sc_ih;
59 1.12 skrll
60 1.12 skrll int sc_phandle;
61 1.9 jmcneill struct clk *sc_clk_sata;
62 1.9 jmcneill struct clk *sc_clk_sata_oob;
63 1.9 jmcneill struct clk *sc_clk_cml1;
64 1.9 jmcneill struct clk *sc_clk_pll_e;
65 1.9 jmcneill struct fdtbus_reset *sc_rst_sata;
66 1.9 jmcneill struct fdtbus_reset *sc_rst_sata_oob;
67 1.9 jmcneill struct fdtbus_reset *sc_rst_sata_cold;
68 1.4 jmcneill
69 1.4 jmcneill struct tegra_gpio_pin *sc_pin_power;
70 1.12 skrll
71 1.12 skrll struct tegra_ahcisata_data
72 1.12 skrll *sc_tad;
73 1.1 jmcneill };
74 1.1 jmcneill
75 1.12 skrll static const char * const tegra124_ahcisata_supplies[] = {
76 1.8 jmcneill "hvdd-supply",
77 1.8 jmcneill "vddio-supply",
78 1.8 jmcneill "avdd-supply",
79 1.8 jmcneill "target-5v-supply",
80 1.8 jmcneill "target-12v-supply"
81 1.8 jmcneill };
82 1.8 jmcneill
83 1.12 skrll enum tegra_ahcisata_type {
84 1.12 skrll TEGRA124,
85 1.12 skrll TEGRA210
86 1.12 skrll };
87 1.12 skrll
88 1.12 skrll struct tegra_ahcisata_data {
89 1.12 skrll enum tegra_ahcisata_type tad_type;
90 1.12 skrll const char * const * tad_supplies;
91 1.12 skrll size_t tad_nsupplies;
92 1.12 skrll };
93 1.12 skrll
94 1.12 skrll struct tegra_ahcisata_data tegra124_ahcisata_data = {
95 1.12 skrll .tad_type = TEGRA124,
96 1.12 skrll .tad_supplies = tegra124_ahcisata_supplies,
97 1.12 skrll .tad_nsupplies = __arraycount(tegra124_ahcisata_supplies),
98 1.12 skrll };
99 1.12 skrll
100 1.12 skrll struct tegra_ahcisata_data tegra210_ahcisata_data = {
101 1.12 skrll .tad_type = TEGRA210,
102 1.12 skrll };
103 1.12 skrll
104 1.12 skrll
105 1.12 skrll static const struct of_compat_data compat_data[] = {
106 1.12 skrll { "nvidia,tegra124-ahci", (uintptr_t)&tegra124_ahcisata_data },
107 1.12 skrll { "nvidia,tegra210-ahci", (uintptr_t)&tegra210_ahcisata_data },
108 1.12 skrll { NULL },
109 1.12 skrll };
110 1.12 skrll
111 1.12 skrll
112 1.3 jmcneill static void tegra_ahcisata_init(struct tegra_ahcisata_softc *);
113 1.9 jmcneill static int tegra_ahcisata_init_clocks(struct tegra_ahcisata_softc *);
114 1.3 jmcneill
115 1.1 jmcneill CFATTACH_DECL_NEW(tegra_ahcisata, sizeof(struct tegra_ahcisata_softc),
116 1.1 jmcneill tegra_ahcisata_match, tegra_ahcisata_attach, NULL, NULL);
117 1.1 jmcneill
118 1.1 jmcneill static int
119 1.1 jmcneill tegra_ahcisata_match(device_t parent, cfdata_t cf, void *aux)
120 1.1 jmcneill {
121 1.8 jmcneill struct fdt_attach_args * const faa = aux;
122 1.8 jmcneill
123 1.12 skrll return of_match_compat_data(faa->faa_phandle, compat_data);
124 1.1 jmcneill }
125 1.1 jmcneill
126 1.1 jmcneill static void
127 1.1 jmcneill tegra_ahcisata_attach(device_t parent, device_t self, void *aux)
128 1.1 jmcneill {
129 1.1 jmcneill struct tegra_ahcisata_softc * const sc = device_private(self);
130 1.8 jmcneill struct fdt_attach_args * const faa = aux;
131 1.8 jmcneill const int phandle = faa->faa_phandle;
132 1.8 jmcneill bus_addr_t ahci_addr, sata_addr;
133 1.8 jmcneill bus_size_t ahci_size, sata_size;
134 1.8 jmcneill struct fdtbus_regulator *reg;
135 1.8 jmcneill char intrstr[128];
136 1.8 jmcneill int error, n;
137 1.8 jmcneill
138 1.8 jmcneill if (fdtbus_get_reg(phandle, 0, &ahci_addr, &ahci_size) != 0) {
139 1.8 jmcneill aprint_error(": couldn't get ahci registers\n");
140 1.8 jmcneill return;
141 1.8 jmcneill }
142 1.8 jmcneill if (fdtbus_get_reg(phandle, 1, &sata_addr, &sata_size) != 0) {
143 1.8 jmcneill aprint_error(": couldn't get sata registers\n");
144 1.8 jmcneill return;
145 1.8 jmcneill }
146 1.9 jmcneill sc->sc_clk_sata = fdtbus_clock_get(phandle, "sata");
147 1.9 jmcneill if (sc->sc_clk_sata == NULL) {
148 1.9 jmcneill aprint_error(": couldn't get clock sata\n");
149 1.9 jmcneill return;
150 1.9 jmcneill }
151 1.9 jmcneill sc->sc_clk_sata_oob = fdtbus_clock_get(phandle, "sata-oob");
152 1.9 jmcneill if (sc->sc_clk_sata_oob == NULL) {
153 1.9 jmcneill aprint_error(": couldn't get clock sata-oob\n");
154 1.9 jmcneill return;
155 1.9 jmcneill }
156 1.9 jmcneill sc->sc_rst_sata = fdtbus_reset_get(phandle, "sata");
157 1.9 jmcneill if (sc->sc_rst_sata == NULL) {
158 1.9 jmcneill aprint_error(": couldn't get reset sata\n");
159 1.9 jmcneill return;
160 1.9 jmcneill }
161 1.9 jmcneill sc->sc_rst_sata_oob = fdtbus_reset_get(phandle, "sata-oob");
162 1.9 jmcneill if (sc->sc_rst_sata_oob == NULL) {
163 1.9 jmcneill aprint_error(": couldn't get reset sata-oob\n");
164 1.9 jmcneill return;
165 1.9 jmcneill }
166 1.9 jmcneill sc->sc_rst_sata_cold = fdtbus_reset_get(phandle, "sata-cold");
167 1.9 jmcneill if(sc->sc_rst_sata_cold == NULL) {
168 1.9 jmcneill aprint_error(": couldn't get reset sata-cold\n");
169 1.9 jmcneill return;
170 1.9 jmcneill }
171 1.8 jmcneill
172 1.12 skrll uintptr_t data = of_search_compatible(faa->faa_phandle, compat_data)->data;
173 1.12 skrll sc->sc_tad = (struct tegra_ahcisata_data *)data;
174 1.12 skrll if (sc->sc_tad->tad_type == TEGRA124) {
175 1.12 skrll sc->sc_clk_cml1 = fdtbus_clock_get(phandle, "cml1");
176 1.12 skrll if (sc->sc_clk_cml1 == NULL) {
177 1.12 skrll aprint_error(": couldn't get clock cml1\n");
178 1.12 skrll return;
179 1.12 skrll }
180 1.12 skrll sc->sc_clk_pll_e = fdtbus_clock_get(phandle, "pll_e");
181 1.12 skrll if (sc->sc_clk_pll_e == NULL) {
182 1.12 skrll aprint_error(": couldn't get clock pll_e\n");
183 1.12 skrll return;
184 1.12 skrll }
185 1.12 skrll }
186 1.12 skrll
187 1.8 jmcneill sc->sc_bst = faa->faa_bst;
188 1.8 jmcneill error = bus_space_map(sc->sc_bst, sata_addr, sata_size, 0, &sc->sc_bsh);
189 1.8 jmcneill if (error) {
190 1.8 jmcneill aprint_error(": couldn't map sata registers: %d\n", error);
191 1.8 jmcneill return;
192 1.8 jmcneill }
193 1.3 jmcneill
194 1.12 skrll sc->sc_phandle = faa->faa_phandle;
195 1.1 jmcneill sc->sc.sc_atac.atac_dev = self;
196 1.8 jmcneill sc->sc.sc_dmat = faa->faa_dmat;
197 1.8 jmcneill sc->sc.sc_ahcit = faa->faa_bst;
198 1.8 jmcneill sc->sc.sc_ahcis = ahci_size;
199 1.8 jmcneill error = bus_space_map(sc->sc.sc_ahcit, ahci_addr, ahci_size, 0,
200 1.8 jmcneill &sc->sc.sc_ahcih);
201 1.8 jmcneill if (error) {
202 1.8 jmcneill aprint_error(": couldn't map ahci registers: %d\n", error);
203 1.8 jmcneill return;
204 1.8 jmcneill }
205 1.6 jmcneill sc->sc.sc_ahci_quirks = AHCI_QUIRK_SKIP_RESET;
206 1.1 jmcneill
207 1.1 jmcneill aprint_naive("\n");
208 1.1 jmcneill aprint_normal(": SATA\n");
209 1.1 jmcneill
210 1.12 skrll for (n = 0; n < sc->sc_tad->tad_nsupplies; n++) {
211 1.12 skrll const char *supply = sc->sc_tad->tad_supplies[n];
212 1.8 jmcneill reg = fdtbus_regulator_acquire(phandle, supply);
213 1.8 jmcneill if (reg == NULL) {
214 1.8 jmcneill aprint_error_dev(self, "couldn't acquire %s\n", supply);
215 1.8 jmcneill continue;
216 1.8 jmcneill }
217 1.8 jmcneill if (fdtbus_regulator_enable(reg) != 0) {
218 1.8 jmcneill aprint_error_dev(self, "couldn't enable %s\n", supply);
219 1.8 jmcneill }
220 1.8 jmcneill fdtbus_regulator_release(reg);
221 1.4 jmcneill }
222 1.4 jmcneill
223 1.9 jmcneill if (tegra_ahcisata_init_clocks(sc) != 0)
224 1.9 jmcneill return;
225 1.3 jmcneill
226 1.5 jmcneill tegra_xusbpad_sata_enable();
227 1.5 jmcneill
228 1.3 jmcneill tegra_ahcisata_init(sc);
229 1.3 jmcneill
230 1.8 jmcneill if (!fdtbus_intr_str(phandle, 0, intrstr, sizeof(intrstr))) {
231 1.8 jmcneill aprint_error_dev(self, "failed to decode interrupt\n");
232 1.8 jmcneill return;
233 1.8 jmcneill }
234 1.8 jmcneill
235 1.8 jmcneill sc->sc_ih = fdtbus_intr_establish(phandle, 0, IPL_BIO, 0,
236 1.1 jmcneill ahci_intr, &sc->sc);
237 1.1 jmcneill if (sc->sc_ih == NULL) {
238 1.8 jmcneill aprint_error_dev(self, "failed to establish interrupt on %s\n",
239 1.8 jmcneill intrstr);
240 1.1 jmcneill return;
241 1.1 jmcneill }
242 1.8 jmcneill aprint_normal_dev(self, "interrupting on %s\n", intrstr);
243 1.1 jmcneill
244 1.1 jmcneill ahci_attach(&sc->sc);
245 1.1 jmcneill }
246 1.3 jmcneill
247 1.3 jmcneill static void
248 1.3 jmcneill tegra_ahcisata_init(struct tegra_ahcisata_softc *sc)
249 1.3 jmcneill {
250 1.3 jmcneill bus_space_tag_t bst = sc->sc_bst;
251 1.3 jmcneill bus_space_handle_t bsh = sc->sc_bsh;
252 1.3 jmcneill
253 1.7 jmcneill /* Set RX idle detection source and disable RX idle detection interrupt */
254 1.7 jmcneill tegra_reg_set_clear(bst, bsh, TEGRA_SATA_AUX_MISC_CNTL_1_REG,
255 1.7 jmcneill TEGRA_SATA_AUX_MISC_CNTL_1_AUX_OR_CORE_IDLE_STATUS_SEL, 0);
256 1.7 jmcneill tegra_reg_set_clear(bst, bsh, TEGRA_SATA_AUX_RX_STAT_INT_REG,
257 1.7 jmcneill TEGRA_SATA_AUX_RX_STAT_INT_SATA_RX_STAT_INT_DISABLE, 0);
258 1.7 jmcneill
259 1.7 jmcneill /* Prevent automatic OOB sequence when coming out of reset */
260 1.7 jmcneill tegra_reg_set_clear(bst, bsh, TEGRA_SATA_AUX_MISC_CNTL_1_REG,
261 1.7 jmcneill 0, TEGRA_SATA_AUX_MISC_CNTL_1_OOB_ON_POR);
262 1.7 jmcneill
263 1.7 jmcneill /* Disable device sleep */
264 1.7 jmcneill tegra_reg_set_clear(bst, bsh, TEGRA_SATA_AUX_MISC_CNTL_1_REG,
265 1.7 jmcneill 0, TEGRA_SATA_AUX_MISC_CNTL_1_SDS_SUPPORT);
266 1.7 jmcneill
267 1.3 jmcneill /* Enable IFPS device block */
268 1.3 jmcneill tegra_reg_set_clear(bst, bsh, TEGRA_SATA_CONFIGURATION_REG,
269 1.3 jmcneill TEGRA_SATA_CONFIGURATION_EN_FPCI, 0);
270 1.3 jmcneill
271 1.12 skrll /* Electrical settings for better link stability */
272 1.12 skrll bus_space_write_4(bst, bsh, TEGRA_T_SATA0_CHX_PHY_CTRL17_REG, 0x55010000);
273 1.12 skrll bus_space_write_4(bst, bsh, TEGRA_T_SATA0_CHX_PHY_CTRL18_REG, 0x55010000);
274 1.12 skrll bus_space_write_4(bst, bsh, TEGRA_T_SATA0_CHX_PHY_CTRL20_REG, 1);
275 1.12 skrll bus_space_write_4(bst, bsh, TEGRA_T_SATA0_CHX_PHY_CTRL21_REG, 1);
276 1.12 skrll
277 1.12 skrll tegra_reg_set_clear(bst, bsh, TEGRA_T_SATA0_CFG_PHY_0_REG,
278 1.12 skrll TEGRA_T_SATA0_CFG_PHY_0_MASK_SQUELCH,
279 1.12 skrll TEGRA_T_SATA0_CFG_PHY_0_USE_7BIT_ALIGN_DET_FOR_SPD);
280 1.12 skrll
281 1.12 skrll tegra_reg_set_clear(bst, bsh, TEGRA_T_SATA0_NVOOB_REG,
282 1.12 skrll __SHIFTIN(0x7, TEGRA_T_SATA0_NVOOB_COMMA_CNT) |
283 1.12 skrll __SHIFTIN(0x3, TEGRA_T_SATA0_NVOOB_SQUELCH_FILTER_LENGTH) |
284 1.12 skrll __SHIFTIN(0x1, TEGRA_T_SATA0_NVOOB_SQUELCH_FILTER_MODE),
285 1.12 skrll TEGRA_T_SATA0_NVOOB_COMMA_CNT |
286 1.12 skrll TEGRA_T_SATA0_NVOOB_SQUELCH_FILTER_LENGTH |
287 1.12 skrll TEGRA_T_SATA0_NVOOB_SQUELCH_FILTER_MODE);
288 1.12 skrll
289 1.12 skrll tegra_reg_set_clear(bst, bsh, TEGRA_T_SATA0_CFG_2NVOOB_2_REG,
290 1.12 skrll __SHIFTIN(0xc, TEGRA_T_SATA0_CFG_2NVOOB_2_COMWAKE_IDLE_CNT_LOW),
291 1.12 skrll TEGRA_T_SATA0_CFG_2NVOOB_2_COMWAKE_IDLE_CNT_LOW);
292 1.12 skrll
293 1.12 skrll if (sc->sc_tad->tad_type == TEGRA124) {
294 1.12 skrll const u_int gen1_tx_amp = 0x18;
295 1.12 skrll const u_int gen1_tx_peak = 0x04;
296 1.12 skrll const u_int gen2_tx_amp = 0x18;
297 1.12 skrll const u_int gen2_tx_peak = 0x0a;
298 1.12 skrll
299 1.12 skrll /* PHY config */
300 1.12 skrll bus_space_write_4(bst, bsh, TEGRA_T_SATA0_INDEX_REG,
301 1.12 skrll TEGRA_T_SATA0_INDEX_CH1);
302 1.12 skrll tegra_reg_set_clear(bst, bsh, TEGRA_T_SATA0_CHX_PHY_CTRL1_GEN1_REG,
303 1.12 skrll __SHIFTIN(gen1_tx_amp, TEGRA_T_SATA0_CHX_PHY_CTRL1_GEN1_TX_AMP) |
304 1.12 skrll __SHIFTIN(gen1_tx_peak, TEGRA_T_SATA0_CHX_PHY_CTRL1_GEN1_TX_PEAK),
305 1.12 skrll TEGRA_T_SATA0_CHX_PHY_CTRL1_GEN1_TX_AMP |
306 1.12 skrll TEGRA_T_SATA0_CHX_PHY_CTRL1_GEN1_TX_PEAK);
307 1.12 skrll tegra_reg_set_clear(bst, bsh, TEGRA_T_SATA0_CHX_PHY_CTRL1_GEN2_REG,
308 1.12 skrll __SHIFTIN(gen2_tx_amp, TEGRA_T_SATA0_CHX_PHY_CTRL1_GEN2_TX_AMP) |
309 1.12 skrll __SHIFTIN(gen2_tx_peak, TEGRA_T_SATA0_CHX_PHY_CTRL1_GEN2_TX_PEAK),
310 1.12 skrll TEGRA_T_SATA0_CHX_PHY_CTRL1_GEN2_TX_AMP |
311 1.12 skrll TEGRA_T_SATA0_CHX_PHY_CTRL1_GEN2_TX_PEAK);
312 1.12 skrll bus_space_write_4(bst, bsh, TEGRA_T_SATA0_CHX_PHY_CTRL11_REG,
313 1.12 skrll __SHIFTIN(0x2800, TEGRA_T_SATA0_CHX_PHY_CTRL11_GEN2_RX_EQ));
314 1.12 skrll bus_space_write_4(bst, bsh, TEGRA_T_SATA0_CHX_PHY_CTRL2_REG,
315 1.12 skrll __SHIFTIN(0x23, TEGRA_T_SATA0_CHX_PHY_CTRL2_CDR_CNTL_GEN1));
316 1.12 skrll bus_space_write_4(bst, bsh, TEGRA_T_SATA0_INDEX_REG, 0);
317 1.12 skrll }
318 1.5 jmcneill
319 1.3 jmcneill /* Backdoor update the programming interface field and class code */
320 1.3 jmcneill tegra_reg_set_clear(bst, bsh, TEGRA_T_SATA0_CFG_SATA_REG,
321 1.3 jmcneill TEGRA_T_SATA0_CFG_SATA_BACKDOOR_PROG_IF_EN, 0);
322 1.12 skrll
323 1.3 jmcneill bus_space_write_4(bst, bsh, TEGRA_T_SATA0_BKDOOR_CC_REG,
324 1.12 skrll __SHIFTIN(0x0106, TEGRA_T_SATA0_BKDOOR_CC_CLASS_CODE) |
325 1.3 jmcneill __SHIFTIN(0x1, TEGRA_T_SATA0_BKDOOR_CC_PROG_IF));
326 1.3 jmcneill tegra_reg_set_clear(bst, bsh, TEGRA_T_SATA0_CFG_SATA_REG,
327 1.3 jmcneill 0, TEGRA_T_SATA0_CFG_SATA_BACKDOOR_PROG_IF_EN);
328 1.3 jmcneill
329 1.12 skrll tegra_reg_set_clear(bst, bsh, TEGRA_T_SATA0_AHCI_HBA_CAP_BKDR_REG,
330 1.12 skrll TEGRA_T_SATA0_AHCI_HBA_CAP_BKDR_SALP |
331 1.12 skrll TEGRA_T_SATA0_AHCI_HBA_CAP_BKDR_SUPP_PM |
332 1.12 skrll TEGRA_T_SATA0_AHCI_HBA_CAP_BKDR_SLUMBER_ST_CAP |
333 1.12 skrll TEGRA_T_SATA0_AHCI_HBA_CAP_BKDR_PARTIAL_ST_CAP, 0);
334 1.12 skrll
335 1.12 skrll tegra_reg_set_clear(bst, bsh, TEGRA_T_SATA0_CFG_PHY_1_REG,
336 1.12 skrll TEGRA_T_SATA0_CFG_PHY_1_PADS_IDDQ_EN |
337 1.12 skrll TEGRA_T_SATA0_CFG_PHY_1_PAD_PLL_IDDQ_EN, 0);
338 1.12 skrll
339 1.12 skrll /* Enable IFPS device block */
340 1.12 skrll tegra_reg_set_clear(bst, bsh, TEGRA_SATA_CONFIGURATION_REG,
341 1.12 skrll 0, TEGRA_SATA_CONFIGURATION_CLKEN_OVERRIDE);
342 1.12 skrll
343 1.3 jmcneill /* Enable access and bus mastering */
344 1.3 jmcneill tegra_reg_set_clear(bst, bsh, TEGRA_T_SATA0_CFG1_REG,
345 1.3 jmcneill TEGRA_T_SATA0_CFG1_SERR |
346 1.3 jmcneill TEGRA_T_SATA0_CFG1_BUS_MASTER |
347 1.3 jmcneill TEGRA_T_SATA0_CFG1_MEM_SPACE |
348 1.3 jmcneill TEGRA_T_SATA0_CFG1_IO_SPACE,
349 1.3 jmcneill 0);
350 1.3 jmcneill
351 1.3 jmcneill /* MMIO setup */
352 1.3 jmcneill bus_space_write_4(bst, bsh, TEGRA_SATA_FPCI_BAR5_REG,
353 1.12 skrll __SHIFTIN(0x10000, TEGRA_SATA_FPCI_BAR_START) |
354 1.12 skrll TEGRA_SATA_FPCI_BAR_ACCESS_TYPE);
355 1.12 skrll
356 1.3 jmcneill bus_space_write_4(bst, bsh, TEGRA_T_SATA0_CFG9_REG,
357 1.3 jmcneill __SHIFTIN(0x8000, TEGRA_T_SATA0_CFG9_BASE_ADDRESS));
358 1.3 jmcneill
359 1.3 jmcneill /* Enable interrupts */
360 1.3 jmcneill tegra_reg_set_clear(bst, bsh, TEGRA_SATA_INTR_MASK_REG,
361 1.3 jmcneill TEGRA_SATA_INTR_MASK_IP_INT, 0);
362 1.3 jmcneill }
363 1.9 jmcneill
364 1.9 jmcneill static int
365 1.9 jmcneill tegra_ahcisata_init_clocks(struct tegra_ahcisata_softc *sc)
366 1.9 jmcneill {
367 1.9 jmcneill device_t self = sc->sc.sc_atac.atac_dev;
368 1.9 jmcneill int error;
369 1.9 jmcneill
370 1.9 jmcneill /* Assert resets */
371 1.9 jmcneill fdtbus_reset_assert(sc->sc_rst_sata);
372 1.9 jmcneill fdtbus_reset_assert(sc->sc_rst_sata_cold);
373 1.9 jmcneill
374 1.10 jmcneill /* Set SATA_OOB clock source to 204MHz */
375 1.9 jmcneill error = clk_set_rate(sc->sc_clk_sata_oob, 204000000);
376 1.9 jmcneill if (error) {
377 1.9 jmcneill aprint_error_dev(self, "couldn't set sata-oob rate: %d\n",
378 1.9 jmcneill error);
379 1.9 jmcneill return error;
380 1.9 jmcneill }
381 1.9 jmcneill
382 1.10 jmcneill /* Set SATA clock source to 102MHz */
383 1.9 jmcneill error = clk_set_rate(sc->sc_clk_sata, 102000000);
384 1.9 jmcneill if (error) {
385 1.9 jmcneill aprint_error_dev(self, "couldn't set sata rate: %d\n", error);
386 1.9 jmcneill return error;
387 1.9 jmcneill }
388 1.9 jmcneill
389 1.9 jmcneill /* Ungate SAX partition in the PMC */
390 1.9 jmcneill tegra_pmc_power(PMC_PARTID_SAX, true);
391 1.9 jmcneill delay(20);
392 1.9 jmcneill
393 1.9 jmcneill /* Remove clamping from SAX partition in the PMC */
394 1.9 jmcneill tegra_pmc_remove_clamping(PMC_PARTID_SAX);
395 1.9 jmcneill delay(20);
396 1.9 jmcneill
397 1.12 skrll /* Un-gate clocks for SATA */
398 1.9 jmcneill error = clk_enable(sc->sc_clk_sata);
399 1.9 jmcneill if (error) {
400 1.9 jmcneill aprint_error_dev(self, "couldn't enable sata: %d\n", error);
401 1.9 jmcneill return error;
402 1.9 jmcneill }
403 1.9 jmcneill error = clk_enable(sc->sc_clk_sata_oob);
404 1.9 jmcneill if (error) {
405 1.9 jmcneill aprint_error_dev(self, "couldn't enable sata-oob: %d\n", error);
406 1.9 jmcneill return error;
407 1.9 jmcneill }
408 1.12 skrll
409 1.12 skrll if (sc->sc_clk_cml1) {
410 1.12 skrll /* Enable CML clock for SATA */
411 1.12 skrll error = clk_enable(sc->sc_clk_cml1);
412 1.12 skrll if (error) {
413 1.12 skrll aprint_error_dev(self, "couldn't enable cml1: %d\n", error);
414 1.12 skrll return error;
415 1.12 skrll }
416 1.9 jmcneill }
417 1.9 jmcneill
418 1.12 skrll /* Enable PHYs */
419 1.12 skrll struct fdtbus_phy *phy;
420 1.12 skrll for (u_int n = 0; (phy = fdtbus_phy_get_index(sc->sc_phandle, n)) != NULL; n++)
421 1.12 skrll if (fdtbus_phy_enable(phy, true) != 0)
422 1.12 skrll aprint_error_dev(self, "failed to enable PHY #%d\n", n);
423 1.12 skrll
424 1.9 jmcneill /* De-assert resets */
425 1.9 jmcneill fdtbus_reset_deassert(sc->sc_rst_sata);
426 1.9 jmcneill fdtbus_reset_deassert(sc->sc_rst_sata_cold);
427 1.9 jmcneill
428 1.9 jmcneill return 0;
429 1.9 jmcneill }
430