tegra_ahcisata.c revision 1.17 1 1.17 thorpej /* $NetBSD: tegra_ahcisata.c,v 1.17 2021/01/27 01:58:15 thorpej 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.17 thorpej __KERNEL_RCSID(0, "$NetBSD: tegra_ahcisata.c,v 1.17 2021/01/27 01:58:15 thorpej 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.15 thorpej const 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.15 thorpej static const struct device_compatible_entry compat_data[] = {
106 1.15 thorpej { .compat = "nvidia,tegra124-ahci", .data = &tegra124_ahcisata_data },
107 1.15 thorpej { .compat = "nvidia,tegra210-ahci", .data = &tegra210_ahcisata_data },
108 1.17 thorpej DEVICE_COMPAT_EOL,
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.15 thorpej sc->sc_tad = of_search_compatible(faa->faa_phandle, compat_data)->data;
173 1.12 skrll if (sc->sc_tad->tad_type == TEGRA124) {
174 1.12 skrll sc->sc_clk_cml1 = fdtbus_clock_get(phandle, "cml1");
175 1.12 skrll if (sc->sc_clk_cml1 == NULL) {
176 1.12 skrll aprint_error(": couldn't get clock cml1\n");
177 1.12 skrll return;
178 1.12 skrll }
179 1.12 skrll sc->sc_clk_pll_e = fdtbus_clock_get(phandle, "pll_e");
180 1.12 skrll if (sc->sc_clk_pll_e == NULL) {
181 1.12 skrll aprint_error(": couldn't get clock pll_e\n");
182 1.12 skrll return;
183 1.12 skrll }
184 1.12 skrll }
185 1.12 skrll
186 1.8 jmcneill sc->sc_bst = faa->faa_bst;
187 1.8 jmcneill error = bus_space_map(sc->sc_bst, sata_addr, sata_size, 0, &sc->sc_bsh);
188 1.8 jmcneill if (error) {
189 1.8 jmcneill aprint_error(": couldn't map sata registers: %d\n", error);
190 1.8 jmcneill return;
191 1.8 jmcneill }
192 1.3 jmcneill
193 1.12 skrll sc->sc_phandle = faa->faa_phandle;
194 1.1 jmcneill sc->sc.sc_atac.atac_dev = self;
195 1.8 jmcneill sc->sc.sc_dmat = faa->faa_dmat;
196 1.8 jmcneill sc->sc.sc_ahcit = faa->faa_bst;
197 1.8 jmcneill sc->sc.sc_ahcis = ahci_size;
198 1.8 jmcneill error = bus_space_map(sc->sc.sc_ahcit, ahci_addr, ahci_size, 0,
199 1.8 jmcneill &sc->sc.sc_ahcih);
200 1.8 jmcneill if (error) {
201 1.8 jmcneill aprint_error(": couldn't map ahci registers: %d\n", error);
202 1.8 jmcneill return;
203 1.8 jmcneill }
204 1.1 jmcneill
205 1.1 jmcneill aprint_naive("\n");
206 1.1 jmcneill aprint_normal(": SATA\n");
207 1.1 jmcneill
208 1.12 skrll for (n = 0; n < sc->sc_tad->tad_nsupplies; n++) {
209 1.12 skrll const char *supply = sc->sc_tad->tad_supplies[n];
210 1.8 jmcneill reg = fdtbus_regulator_acquire(phandle, supply);
211 1.8 jmcneill if (reg == NULL) {
212 1.8 jmcneill aprint_error_dev(self, "couldn't acquire %s\n", supply);
213 1.8 jmcneill continue;
214 1.8 jmcneill }
215 1.8 jmcneill if (fdtbus_regulator_enable(reg) != 0) {
216 1.8 jmcneill aprint_error_dev(self, "couldn't enable %s\n", supply);
217 1.8 jmcneill }
218 1.8 jmcneill fdtbus_regulator_release(reg);
219 1.4 jmcneill }
220 1.4 jmcneill
221 1.9 jmcneill if (tegra_ahcisata_init_clocks(sc) != 0)
222 1.9 jmcneill return;
223 1.3 jmcneill
224 1.5 jmcneill tegra_xusbpad_sata_enable();
225 1.5 jmcneill
226 1.3 jmcneill tegra_ahcisata_init(sc);
227 1.3 jmcneill
228 1.8 jmcneill if (!fdtbus_intr_str(phandle, 0, intrstr, sizeof(intrstr))) {
229 1.8 jmcneill aprint_error_dev(self, "failed to decode interrupt\n");
230 1.8 jmcneill return;
231 1.8 jmcneill }
232 1.8 jmcneill
233 1.14 jmcneill sc->sc_ih = fdtbus_intr_establish_xname(phandle, 0, IPL_BIO, 0,
234 1.14 jmcneill ahci_intr, &sc->sc, device_xname(self));
235 1.1 jmcneill if (sc->sc_ih == NULL) {
236 1.8 jmcneill aprint_error_dev(self, "failed to establish interrupt on %s\n",
237 1.8 jmcneill intrstr);
238 1.1 jmcneill return;
239 1.1 jmcneill }
240 1.8 jmcneill aprint_normal_dev(self, "interrupting on %s\n", intrstr);
241 1.1 jmcneill
242 1.1 jmcneill ahci_attach(&sc->sc);
243 1.1 jmcneill }
244 1.3 jmcneill
245 1.3 jmcneill static void
246 1.3 jmcneill tegra_ahcisata_init(struct tegra_ahcisata_softc *sc)
247 1.3 jmcneill {
248 1.3 jmcneill bus_space_tag_t bst = sc->sc_bst;
249 1.3 jmcneill bus_space_handle_t bsh = sc->sc_bsh;
250 1.3 jmcneill
251 1.7 jmcneill /* Set RX idle detection source and disable RX idle detection interrupt */
252 1.7 jmcneill tegra_reg_set_clear(bst, bsh, TEGRA_SATA_AUX_MISC_CNTL_1_REG,
253 1.7 jmcneill TEGRA_SATA_AUX_MISC_CNTL_1_AUX_OR_CORE_IDLE_STATUS_SEL, 0);
254 1.7 jmcneill tegra_reg_set_clear(bst, bsh, TEGRA_SATA_AUX_RX_STAT_INT_REG,
255 1.7 jmcneill TEGRA_SATA_AUX_RX_STAT_INT_SATA_RX_STAT_INT_DISABLE, 0);
256 1.7 jmcneill
257 1.7 jmcneill /* Prevent automatic OOB sequence when coming out of reset */
258 1.7 jmcneill tegra_reg_set_clear(bst, bsh, TEGRA_SATA_AUX_MISC_CNTL_1_REG,
259 1.7 jmcneill 0, TEGRA_SATA_AUX_MISC_CNTL_1_OOB_ON_POR);
260 1.7 jmcneill
261 1.7 jmcneill /* Disable device sleep */
262 1.7 jmcneill tegra_reg_set_clear(bst, bsh, TEGRA_SATA_AUX_MISC_CNTL_1_REG,
263 1.7 jmcneill 0, TEGRA_SATA_AUX_MISC_CNTL_1_SDS_SUPPORT);
264 1.7 jmcneill
265 1.3 jmcneill /* Enable IFPS device block */
266 1.3 jmcneill tegra_reg_set_clear(bst, bsh, TEGRA_SATA_CONFIGURATION_REG,
267 1.3 jmcneill TEGRA_SATA_CONFIGURATION_EN_FPCI, 0);
268 1.3 jmcneill
269 1.12 skrll /* Electrical settings for better link stability */
270 1.12 skrll bus_space_write_4(bst, bsh, TEGRA_T_SATA0_CHX_PHY_CTRL17_REG, 0x55010000);
271 1.12 skrll bus_space_write_4(bst, bsh, TEGRA_T_SATA0_CHX_PHY_CTRL18_REG, 0x55010000);
272 1.12 skrll bus_space_write_4(bst, bsh, TEGRA_T_SATA0_CHX_PHY_CTRL20_REG, 1);
273 1.12 skrll bus_space_write_4(bst, bsh, TEGRA_T_SATA0_CHX_PHY_CTRL21_REG, 1);
274 1.12 skrll
275 1.12 skrll tegra_reg_set_clear(bst, bsh, TEGRA_T_SATA0_CFG_PHY_0_REG,
276 1.12 skrll TEGRA_T_SATA0_CFG_PHY_0_MASK_SQUELCH,
277 1.12 skrll TEGRA_T_SATA0_CFG_PHY_0_USE_7BIT_ALIGN_DET_FOR_SPD);
278 1.12 skrll
279 1.12 skrll tegra_reg_set_clear(bst, bsh, TEGRA_T_SATA0_NVOOB_REG,
280 1.12 skrll __SHIFTIN(0x7, TEGRA_T_SATA0_NVOOB_COMMA_CNT) |
281 1.12 skrll __SHIFTIN(0x3, TEGRA_T_SATA0_NVOOB_SQUELCH_FILTER_LENGTH) |
282 1.12 skrll __SHIFTIN(0x1, TEGRA_T_SATA0_NVOOB_SQUELCH_FILTER_MODE),
283 1.12 skrll TEGRA_T_SATA0_NVOOB_COMMA_CNT |
284 1.12 skrll TEGRA_T_SATA0_NVOOB_SQUELCH_FILTER_LENGTH |
285 1.12 skrll TEGRA_T_SATA0_NVOOB_SQUELCH_FILTER_MODE);
286 1.12 skrll
287 1.12 skrll tegra_reg_set_clear(bst, bsh, TEGRA_T_SATA0_CFG_2NVOOB_2_REG,
288 1.12 skrll __SHIFTIN(0xc, TEGRA_T_SATA0_CFG_2NVOOB_2_COMWAKE_IDLE_CNT_LOW),
289 1.12 skrll TEGRA_T_SATA0_CFG_2NVOOB_2_COMWAKE_IDLE_CNT_LOW);
290 1.12 skrll
291 1.12 skrll if (sc->sc_tad->tad_type == TEGRA124) {
292 1.12 skrll const u_int gen1_tx_amp = 0x18;
293 1.12 skrll const u_int gen1_tx_peak = 0x04;
294 1.12 skrll const u_int gen2_tx_amp = 0x18;
295 1.12 skrll const u_int gen2_tx_peak = 0x0a;
296 1.12 skrll
297 1.12 skrll /* PHY config */
298 1.12 skrll bus_space_write_4(bst, bsh, TEGRA_T_SATA0_INDEX_REG,
299 1.12 skrll TEGRA_T_SATA0_INDEX_CH1);
300 1.12 skrll tegra_reg_set_clear(bst, bsh, TEGRA_T_SATA0_CHX_PHY_CTRL1_GEN1_REG,
301 1.12 skrll __SHIFTIN(gen1_tx_amp, TEGRA_T_SATA0_CHX_PHY_CTRL1_GEN1_TX_AMP) |
302 1.12 skrll __SHIFTIN(gen1_tx_peak, TEGRA_T_SATA0_CHX_PHY_CTRL1_GEN1_TX_PEAK),
303 1.12 skrll TEGRA_T_SATA0_CHX_PHY_CTRL1_GEN1_TX_AMP |
304 1.12 skrll TEGRA_T_SATA0_CHX_PHY_CTRL1_GEN1_TX_PEAK);
305 1.12 skrll tegra_reg_set_clear(bst, bsh, TEGRA_T_SATA0_CHX_PHY_CTRL1_GEN2_REG,
306 1.12 skrll __SHIFTIN(gen2_tx_amp, TEGRA_T_SATA0_CHX_PHY_CTRL1_GEN2_TX_AMP) |
307 1.12 skrll __SHIFTIN(gen2_tx_peak, TEGRA_T_SATA0_CHX_PHY_CTRL1_GEN2_TX_PEAK),
308 1.12 skrll TEGRA_T_SATA0_CHX_PHY_CTRL1_GEN2_TX_AMP |
309 1.12 skrll TEGRA_T_SATA0_CHX_PHY_CTRL1_GEN2_TX_PEAK);
310 1.12 skrll bus_space_write_4(bst, bsh, TEGRA_T_SATA0_CHX_PHY_CTRL11_REG,
311 1.12 skrll __SHIFTIN(0x2800, TEGRA_T_SATA0_CHX_PHY_CTRL11_GEN2_RX_EQ));
312 1.12 skrll bus_space_write_4(bst, bsh, TEGRA_T_SATA0_CHX_PHY_CTRL2_REG,
313 1.12 skrll __SHIFTIN(0x23, TEGRA_T_SATA0_CHX_PHY_CTRL2_CDR_CNTL_GEN1));
314 1.12 skrll bus_space_write_4(bst, bsh, TEGRA_T_SATA0_INDEX_REG, 0);
315 1.12 skrll }
316 1.5 jmcneill
317 1.3 jmcneill /* Backdoor update the programming interface field and class code */
318 1.3 jmcneill tegra_reg_set_clear(bst, bsh, TEGRA_T_SATA0_CFG_SATA_REG,
319 1.3 jmcneill TEGRA_T_SATA0_CFG_SATA_BACKDOOR_PROG_IF_EN, 0);
320 1.12 skrll
321 1.3 jmcneill bus_space_write_4(bst, bsh, TEGRA_T_SATA0_BKDOOR_CC_REG,
322 1.12 skrll __SHIFTIN(0x0106, TEGRA_T_SATA0_BKDOOR_CC_CLASS_CODE) |
323 1.3 jmcneill __SHIFTIN(0x1, TEGRA_T_SATA0_BKDOOR_CC_PROG_IF));
324 1.3 jmcneill tegra_reg_set_clear(bst, bsh, TEGRA_T_SATA0_CFG_SATA_REG,
325 1.3 jmcneill 0, TEGRA_T_SATA0_CFG_SATA_BACKDOOR_PROG_IF_EN);
326 1.3 jmcneill
327 1.12 skrll tegra_reg_set_clear(bst, bsh, TEGRA_T_SATA0_AHCI_HBA_CAP_BKDR_REG,
328 1.12 skrll TEGRA_T_SATA0_AHCI_HBA_CAP_BKDR_SALP |
329 1.12 skrll TEGRA_T_SATA0_AHCI_HBA_CAP_BKDR_SUPP_PM |
330 1.12 skrll TEGRA_T_SATA0_AHCI_HBA_CAP_BKDR_SLUMBER_ST_CAP |
331 1.12 skrll TEGRA_T_SATA0_AHCI_HBA_CAP_BKDR_PARTIAL_ST_CAP, 0);
332 1.12 skrll
333 1.12 skrll tegra_reg_set_clear(bst, bsh, TEGRA_T_SATA0_CFG_PHY_1_REG,
334 1.12 skrll TEGRA_T_SATA0_CFG_PHY_1_PADS_IDDQ_EN |
335 1.12 skrll TEGRA_T_SATA0_CFG_PHY_1_PAD_PLL_IDDQ_EN, 0);
336 1.12 skrll
337 1.12 skrll /* Enable IFPS device block */
338 1.12 skrll tegra_reg_set_clear(bst, bsh, TEGRA_SATA_CONFIGURATION_REG,
339 1.12 skrll 0, TEGRA_SATA_CONFIGURATION_CLKEN_OVERRIDE);
340 1.12 skrll
341 1.3 jmcneill /* Enable access and bus mastering */
342 1.3 jmcneill tegra_reg_set_clear(bst, bsh, TEGRA_T_SATA0_CFG1_REG,
343 1.3 jmcneill TEGRA_T_SATA0_CFG1_SERR |
344 1.3 jmcneill TEGRA_T_SATA0_CFG1_BUS_MASTER |
345 1.3 jmcneill TEGRA_T_SATA0_CFG1_MEM_SPACE |
346 1.3 jmcneill TEGRA_T_SATA0_CFG1_IO_SPACE,
347 1.3 jmcneill 0);
348 1.3 jmcneill
349 1.3 jmcneill /* MMIO setup */
350 1.3 jmcneill bus_space_write_4(bst, bsh, TEGRA_SATA_FPCI_BAR5_REG,
351 1.12 skrll __SHIFTIN(0x10000, TEGRA_SATA_FPCI_BAR_START) |
352 1.12 skrll TEGRA_SATA_FPCI_BAR_ACCESS_TYPE);
353 1.12 skrll
354 1.3 jmcneill bus_space_write_4(bst, bsh, TEGRA_T_SATA0_CFG9_REG,
355 1.3 jmcneill __SHIFTIN(0x8000, TEGRA_T_SATA0_CFG9_BASE_ADDRESS));
356 1.3 jmcneill
357 1.3 jmcneill /* Enable interrupts */
358 1.3 jmcneill tegra_reg_set_clear(bst, bsh, TEGRA_SATA_INTR_MASK_REG,
359 1.3 jmcneill TEGRA_SATA_INTR_MASK_IP_INT, 0);
360 1.3 jmcneill }
361 1.9 jmcneill
362 1.9 jmcneill static int
363 1.9 jmcneill tegra_ahcisata_init_clocks(struct tegra_ahcisata_softc *sc)
364 1.9 jmcneill {
365 1.9 jmcneill device_t self = sc->sc.sc_atac.atac_dev;
366 1.9 jmcneill int error;
367 1.9 jmcneill
368 1.9 jmcneill /* Assert resets */
369 1.9 jmcneill fdtbus_reset_assert(sc->sc_rst_sata);
370 1.9 jmcneill fdtbus_reset_assert(sc->sc_rst_sata_cold);
371 1.9 jmcneill
372 1.10 jmcneill /* Set SATA_OOB clock source to 204MHz */
373 1.9 jmcneill error = clk_set_rate(sc->sc_clk_sata_oob, 204000000);
374 1.9 jmcneill if (error) {
375 1.9 jmcneill aprint_error_dev(self, "couldn't set sata-oob rate: %d\n",
376 1.9 jmcneill error);
377 1.9 jmcneill return error;
378 1.9 jmcneill }
379 1.9 jmcneill
380 1.10 jmcneill /* Set SATA clock source to 102MHz */
381 1.9 jmcneill error = clk_set_rate(sc->sc_clk_sata, 102000000);
382 1.9 jmcneill if (error) {
383 1.9 jmcneill aprint_error_dev(self, "couldn't set sata rate: %d\n", error);
384 1.9 jmcneill return error;
385 1.9 jmcneill }
386 1.9 jmcneill
387 1.9 jmcneill /* Ungate SAX partition in the PMC */
388 1.9 jmcneill tegra_pmc_power(PMC_PARTID_SAX, true);
389 1.9 jmcneill delay(20);
390 1.9 jmcneill
391 1.9 jmcneill /* Remove clamping from SAX partition in the PMC */
392 1.9 jmcneill tegra_pmc_remove_clamping(PMC_PARTID_SAX);
393 1.9 jmcneill delay(20);
394 1.9 jmcneill
395 1.12 skrll /* Un-gate clocks for SATA */
396 1.9 jmcneill error = clk_enable(sc->sc_clk_sata);
397 1.9 jmcneill if (error) {
398 1.9 jmcneill aprint_error_dev(self, "couldn't enable sata: %d\n", error);
399 1.9 jmcneill return error;
400 1.9 jmcneill }
401 1.9 jmcneill error = clk_enable(sc->sc_clk_sata_oob);
402 1.9 jmcneill if (error) {
403 1.9 jmcneill aprint_error_dev(self, "couldn't enable sata-oob: %d\n", error);
404 1.9 jmcneill return error;
405 1.9 jmcneill }
406 1.12 skrll
407 1.12 skrll if (sc->sc_clk_cml1) {
408 1.12 skrll /* Enable CML clock for SATA */
409 1.12 skrll error = clk_enable(sc->sc_clk_cml1);
410 1.12 skrll if (error) {
411 1.12 skrll aprint_error_dev(self, "couldn't enable cml1: %d\n", error);
412 1.12 skrll return error;
413 1.12 skrll }
414 1.9 jmcneill }
415 1.9 jmcneill
416 1.12 skrll /* Enable PHYs */
417 1.12 skrll struct fdtbus_phy *phy;
418 1.12 skrll for (u_int n = 0; (phy = fdtbus_phy_get_index(sc->sc_phandle, n)) != NULL; n++)
419 1.12 skrll if (fdtbus_phy_enable(phy, true) != 0)
420 1.12 skrll aprint_error_dev(self, "failed to enable PHY #%d\n", n);
421 1.12 skrll
422 1.9 jmcneill /* De-assert resets */
423 1.9 jmcneill fdtbus_reset_deassert(sc->sc_rst_sata);
424 1.9 jmcneill fdtbus_reset_deassert(sc->sc_rst_sata_cold);
425 1.9 jmcneill
426 1.9 jmcneill return 0;
427 1.9 jmcneill }
428