tegra_ahcisata.c revision 1.10 1 1.10 jmcneill /* $NetBSD: tegra_ahcisata.c,v 1.10 2017/04/16 12:28:21 jmcneill 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.10 jmcneill __KERNEL_RCSID(0, "$NetBSD: tegra_ahcisata.c,v 1.10 2017/04/16 12:28:21 jmcneill 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.3 jmcneill
46 1.8 jmcneill #include <dev/fdt/fdtvar.h>
47 1.8 jmcneill
48 1.3 jmcneill #define TEGRA_AHCISATA_OFFSET 0x7000
49 1.1 jmcneill
50 1.1 jmcneill static int tegra_ahcisata_match(device_t, cfdata_t, void *);
51 1.1 jmcneill static void tegra_ahcisata_attach(device_t, device_t, void *);
52 1.1 jmcneill
53 1.1 jmcneill struct tegra_ahcisata_softc {
54 1.1 jmcneill struct ahci_softc sc;
55 1.3 jmcneill bus_space_tag_t sc_bst;
56 1.3 jmcneill bus_space_handle_t sc_bsh;
57 1.1 jmcneill void *sc_ih;
58 1.9 jmcneill struct clk *sc_clk_sata;
59 1.9 jmcneill struct clk *sc_clk_sata_oob;
60 1.9 jmcneill struct clk *sc_clk_cml1;
61 1.9 jmcneill struct clk *sc_clk_pll_e;
62 1.9 jmcneill struct fdtbus_reset *sc_rst_sata;
63 1.9 jmcneill struct fdtbus_reset *sc_rst_sata_oob;
64 1.9 jmcneill struct fdtbus_reset *sc_rst_sata_cold;
65 1.4 jmcneill
66 1.4 jmcneill struct tegra_gpio_pin *sc_pin_power;
67 1.1 jmcneill };
68 1.1 jmcneill
69 1.8 jmcneill static const char * const tegra_ahcisata_supplies[] = {
70 1.8 jmcneill "hvdd-supply",
71 1.8 jmcneill "vddio-supply",
72 1.8 jmcneill "avdd-supply",
73 1.8 jmcneill "target-5v-supply",
74 1.8 jmcneill "target-12v-supply"
75 1.8 jmcneill };
76 1.8 jmcneill
77 1.3 jmcneill static void tegra_ahcisata_init(struct tegra_ahcisata_softc *);
78 1.9 jmcneill static int tegra_ahcisata_init_clocks(struct tegra_ahcisata_softc *);
79 1.3 jmcneill
80 1.1 jmcneill CFATTACH_DECL_NEW(tegra_ahcisata, sizeof(struct tegra_ahcisata_softc),
81 1.1 jmcneill tegra_ahcisata_match, tegra_ahcisata_attach, NULL, NULL);
82 1.1 jmcneill
83 1.1 jmcneill static int
84 1.1 jmcneill tegra_ahcisata_match(device_t parent, cfdata_t cf, void *aux)
85 1.1 jmcneill {
86 1.8 jmcneill const char * const compatible[] = { "nvidia,tegra124-ahci", NULL };
87 1.8 jmcneill struct fdt_attach_args * const faa = aux;
88 1.8 jmcneill
89 1.8 jmcneill return of_match_compatible(faa->faa_phandle, compatible);
90 1.1 jmcneill }
91 1.1 jmcneill
92 1.1 jmcneill static void
93 1.1 jmcneill tegra_ahcisata_attach(device_t parent, device_t self, void *aux)
94 1.1 jmcneill {
95 1.1 jmcneill struct tegra_ahcisata_softc * const sc = device_private(self);
96 1.8 jmcneill struct fdt_attach_args * const faa = aux;
97 1.8 jmcneill const int phandle = faa->faa_phandle;
98 1.8 jmcneill bus_addr_t ahci_addr, sata_addr;
99 1.8 jmcneill bus_size_t ahci_size, sata_size;
100 1.8 jmcneill struct fdtbus_regulator *reg;
101 1.8 jmcneill char intrstr[128];
102 1.8 jmcneill int error, n;
103 1.8 jmcneill
104 1.8 jmcneill if (fdtbus_get_reg(phandle, 0, &ahci_addr, &ahci_size) != 0) {
105 1.8 jmcneill aprint_error(": couldn't get ahci registers\n");
106 1.8 jmcneill return;
107 1.8 jmcneill }
108 1.8 jmcneill if (fdtbus_get_reg(phandle, 1, &sata_addr, &sata_size) != 0) {
109 1.8 jmcneill aprint_error(": couldn't get sata registers\n");
110 1.8 jmcneill return;
111 1.8 jmcneill }
112 1.9 jmcneill sc->sc_clk_sata = fdtbus_clock_get(phandle, "sata");
113 1.9 jmcneill if (sc->sc_clk_sata == NULL) {
114 1.9 jmcneill aprint_error(": couldn't get clock sata\n");
115 1.9 jmcneill return;
116 1.9 jmcneill }
117 1.9 jmcneill sc->sc_clk_sata_oob = fdtbus_clock_get(phandle, "sata-oob");
118 1.9 jmcneill if (sc->sc_clk_sata_oob == NULL) {
119 1.9 jmcneill aprint_error(": couldn't get clock sata-oob\n");
120 1.9 jmcneill return;
121 1.9 jmcneill }
122 1.9 jmcneill sc->sc_clk_cml1 = fdtbus_clock_get(phandle, "cml1");
123 1.9 jmcneill if (sc->sc_clk_cml1 == NULL) {
124 1.9 jmcneill aprint_error(": couldn't get clock cml1\n");
125 1.9 jmcneill return;
126 1.9 jmcneill }
127 1.9 jmcneill sc->sc_clk_pll_e = fdtbus_clock_get(phandle, "pll_e");
128 1.9 jmcneill if (sc->sc_clk_pll_e == NULL) {
129 1.9 jmcneill aprint_error(": couldn't get clock pll_e\n");
130 1.9 jmcneill return;
131 1.9 jmcneill }
132 1.9 jmcneill sc->sc_rst_sata = fdtbus_reset_get(phandle, "sata");
133 1.9 jmcneill if (sc->sc_rst_sata == NULL) {
134 1.9 jmcneill aprint_error(": couldn't get reset sata\n");
135 1.9 jmcneill return;
136 1.9 jmcneill }
137 1.9 jmcneill sc->sc_rst_sata_oob = fdtbus_reset_get(phandle, "sata-oob");
138 1.9 jmcneill if (sc->sc_rst_sata_oob == NULL) {
139 1.9 jmcneill aprint_error(": couldn't get reset sata-oob\n");
140 1.9 jmcneill return;
141 1.9 jmcneill }
142 1.9 jmcneill sc->sc_rst_sata_cold = fdtbus_reset_get(phandle, "sata-cold");
143 1.9 jmcneill if(sc->sc_rst_sata_cold == NULL) {
144 1.9 jmcneill aprint_error(": couldn't get reset sata-cold\n");
145 1.9 jmcneill return;
146 1.9 jmcneill }
147 1.8 jmcneill
148 1.8 jmcneill sc->sc_bst = faa->faa_bst;
149 1.8 jmcneill error = bus_space_map(sc->sc_bst, sata_addr, sata_size, 0, &sc->sc_bsh);
150 1.8 jmcneill if (error) {
151 1.8 jmcneill aprint_error(": couldn't map sata registers: %d\n", error);
152 1.8 jmcneill return;
153 1.8 jmcneill }
154 1.3 jmcneill
155 1.1 jmcneill sc->sc.sc_atac.atac_dev = self;
156 1.8 jmcneill sc->sc.sc_dmat = faa->faa_dmat;
157 1.8 jmcneill sc->sc.sc_ahcit = faa->faa_bst;
158 1.8 jmcneill sc->sc.sc_ahcis = ahci_size;
159 1.8 jmcneill error = bus_space_map(sc->sc.sc_ahcit, ahci_addr, ahci_size, 0,
160 1.8 jmcneill &sc->sc.sc_ahcih);
161 1.8 jmcneill if (error) {
162 1.8 jmcneill aprint_error(": couldn't map ahci registers: %d\n", error);
163 1.8 jmcneill return;
164 1.8 jmcneill }
165 1.6 jmcneill sc->sc.sc_ahci_quirks = AHCI_QUIRK_SKIP_RESET;
166 1.1 jmcneill
167 1.1 jmcneill aprint_naive("\n");
168 1.1 jmcneill aprint_normal(": SATA\n");
169 1.1 jmcneill
170 1.8 jmcneill for (n = 0; n < __arraycount(tegra_ahcisata_supplies); n++) {
171 1.8 jmcneill const char *supply = tegra_ahcisata_supplies[n];
172 1.8 jmcneill reg = fdtbus_regulator_acquire(phandle, supply);
173 1.8 jmcneill if (reg == NULL) {
174 1.8 jmcneill aprint_error_dev(self, "couldn't acquire %s\n", supply);
175 1.8 jmcneill continue;
176 1.8 jmcneill }
177 1.8 jmcneill if (fdtbus_regulator_enable(reg) != 0) {
178 1.8 jmcneill aprint_error_dev(self, "couldn't enable %s\n", supply);
179 1.8 jmcneill }
180 1.8 jmcneill fdtbus_regulator_release(reg);
181 1.4 jmcneill }
182 1.4 jmcneill
183 1.9 jmcneill if (tegra_ahcisata_init_clocks(sc) != 0)
184 1.9 jmcneill return;
185 1.3 jmcneill
186 1.5 jmcneill tegra_xusbpad_sata_enable();
187 1.5 jmcneill
188 1.3 jmcneill tegra_ahcisata_init(sc);
189 1.3 jmcneill
190 1.8 jmcneill if (!fdtbus_intr_str(phandle, 0, intrstr, sizeof(intrstr))) {
191 1.8 jmcneill aprint_error_dev(self, "failed to decode interrupt\n");
192 1.8 jmcneill return;
193 1.8 jmcneill }
194 1.8 jmcneill
195 1.8 jmcneill sc->sc_ih = fdtbus_intr_establish(phandle, 0, IPL_BIO, 0,
196 1.1 jmcneill ahci_intr, &sc->sc);
197 1.1 jmcneill if (sc->sc_ih == NULL) {
198 1.8 jmcneill aprint_error_dev(self, "failed to establish interrupt on %s\n",
199 1.8 jmcneill intrstr);
200 1.1 jmcneill return;
201 1.1 jmcneill }
202 1.8 jmcneill aprint_normal_dev(self, "interrupting on %s\n", intrstr);
203 1.1 jmcneill
204 1.1 jmcneill ahci_attach(&sc->sc);
205 1.1 jmcneill }
206 1.3 jmcneill
207 1.3 jmcneill static void
208 1.3 jmcneill tegra_ahcisata_init(struct tegra_ahcisata_softc *sc)
209 1.3 jmcneill {
210 1.3 jmcneill bus_space_tag_t bst = sc->sc_bst;
211 1.3 jmcneill bus_space_handle_t bsh = sc->sc_bsh;
212 1.3 jmcneill
213 1.5 jmcneill const u_int gen1_tx_amp = 0x18;
214 1.5 jmcneill const u_int gen1_tx_peak = 0x04;
215 1.5 jmcneill const u_int gen2_tx_amp = 0x18;
216 1.5 jmcneill const u_int gen2_tx_peak = 0x0a;
217 1.5 jmcneill
218 1.7 jmcneill /* Set RX idle detection source and disable RX idle detection interrupt */
219 1.7 jmcneill tegra_reg_set_clear(bst, bsh, TEGRA_SATA_AUX_MISC_CNTL_1_REG,
220 1.7 jmcneill TEGRA_SATA_AUX_MISC_CNTL_1_AUX_OR_CORE_IDLE_STATUS_SEL, 0);
221 1.7 jmcneill tegra_reg_set_clear(bst, bsh, TEGRA_SATA_AUX_RX_STAT_INT_REG,
222 1.7 jmcneill TEGRA_SATA_AUX_RX_STAT_INT_SATA_RX_STAT_INT_DISABLE, 0);
223 1.7 jmcneill
224 1.7 jmcneill /* Prevent automatic OOB sequence when coming out of reset */
225 1.7 jmcneill tegra_reg_set_clear(bst, bsh, TEGRA_SATA_AUX_MISC_CNTL_1_REG,
226 1.7 jmcneill 0, TEGRA_SATA_AUX_MISC_CNTL_1_OOB_ON_POR);
227 1.7 jmcneill
228 1.7 jmcneill /* Disable device sleep */
229 1.7 jmcneill tegra_reg_set_clear(bst, bsh, TEGRA_SATA_AUX_MISC_CNTL_1_REG,
230 1.7 jmcneill 0, TEGRA_SATA_AUX_MISC_CNTL_1_SDS_SUPPORT);
231 1.7 jmcneill
232 1.3 jmcneill /* Enable IFPS device block */
233 1.3 jmcneill tegra_reg_set_clear(bst, bsh, TEGRA_SATA_CONFIGURATION_REG,
234 1.3 jmcneill TEGRA_SATA_CONFIGURATION_EN_FPCI, 0);
235 1.3 jmcneill
236 1.5 jmcneill /* PHY config */
237 1.5 jmcneill bus_space_write_4(bst, bsh, TEGRA_T_SATA0_INDEX_REG,
238 1.5 jmcneill TEGRA_T_SATA0_INDEX_CH1);
239 1.5 jmcneill tegra_reg_set_clear(bst, bsh, TEGRA_T_SATA0_CHX_PHY_CTRL1_GEN1_REG,
240 1.5 jmcneill __SHIFTIN(gen1_tx_amp, TEGRA_T_SATA0_CHX_PHY_CTRL1_GEN1_TX_AMP) |
241 1.5 jmcneill __SHIFTIN(gen1_tx_peak, TEGRA_T_SATA0_CHX_PHY_CTRL1_GEN1_TX_PEAK),
242 1.5 jmcneill TEGRA_T_SATA0_CHX_PHY_CTRL1_GEN1_TX_AMP |
243 1.5 jmcneill TEGRA_T_SATA0_CHX_PHY_CTRL1_GEN1_TX_PEAK);
244 1.5 jmcneill tegra_reg_set_clear(bst, bsh, TEGRA_T_SATA0_CHX_PHY_CTRL1_GEN2_REG,
245 1.5 jmcneill __SHIFTIN(gen2_tx_amp, TEGRA_T_SATA0_CHX_PHY_CTRL1_GEN2_TX_AMP) |
246 1.5 jmcneill __SHIFTIN(gen2_tx_peak, TEGRA_T_SATA0_CHX_PHY_CTRL1_GEN2_TX_PEAK),
247 1.5 jmcneill TEGRA_T_SATA0_CHX_PHY_CTRL1_GEN2_TX_AMP |
248 1.5 jmcneill TEGRA_T_SATA0_CHX_PHY_CTRL1_GEN2_TX_PEAK);
249 1.5 jmcneill bus_space_write_4(bst, bsh, TEGRA_T_SATA0_CHX_PHY_CTRL11_REG,
250 1.5 jmcneill __SHIFTIN(0x2800, TEGRA_T_SATA0_CHX_PHY_CTRL11_GEN2_RX_EQ));
251 1.5 jmcneill bus_space_write_4(bst, bsh, TEGRA_T_SATA0_CHX_PHY_CTRL2_REG,
252 1.5 jmcneill __SHIFTIN(0x23, TEGRA_T_SATA0_CHX_PHY_CTRL2_CDR_CNTL_GEN1));
253 1.5 jmcneill bus_space_write_4(bst, bsh, TEGRA_T_SATA0_INDEX_REG, 0);
254 1.5 jmcneill
255 1.3 jmcneill /* Backdoor update the programming interface field and class code */
256 1.3 jmcneill tegra_reg_set_clear(bst, bsh, TEGRA_T_SATA0_CFG_SATA_REG,
257 1.3 jmcneill TEGRA_T_SATA0_CFG_SATA_BACKDOOR_PROG_IF_EN, 0);
258 1.3 jmcneill bus_space_write_4(bst, bsh, TEGRA_T_SATA0_BKDOOR_CC_REG,
259 1.3 jmcneill __SHIFTIN(0x1016, TEGRA_T_SATA0_BKDOOR_CC_CLASS_CODE) |
260 1.3 jmcneill __SHIFTIN(0x1, TEGRA_T_SATA0_BKDOOR_CC_PROG_IF));
261 1.3 jmcneill tegra_reg_set_clear(bst, bsh, TEGRA_T_SATA0_CFG_SATA_REG,
262 1.3 jmcneill 0, TEGRA_T_SATA0_CFG_SATA_BACKDOOR_PROG_IF_EN);
263 1.3 jmcneill
264 1.3 jmcneill /* Enable access and bus mastering */
265 1.3 jmcneill tegra_reg_set_clear(bst, bsh, TEGRA_T_SATA0_CFG1_REG,
266 1.3 jmcneill TEGRA_T_SATA0_CFG1_SERR |
267 1.3 jmcneill TEGRA_T_SATA0_CFG1_BUS_MASTER |
268 1.3 jmcneill TEGRA_T_SATA0_CFG1_MEM_SPACE |
269 1.3 jmcneill TEGRA_T_SATA0_CFG1_IO_SPACE,
270 1.3 jmcneill 0);
271 1.3 jmcneill
272 1.3 jmcneill /* MMIO setup */
273 1.3 jmcneill bus_space_write_4(bst, bsh, TEGRA_SATA_FPCI_BAR5_REG,
274 1.3 jmcneill __SHIFTIN(0x10000, TEGRA_SATA_FPCI_BAR_START));
275 1.3 jmcneill bus_space_write_4(bst, bsh, TEGRA_T_SATA0_CFG9_REG,
276 1.3 jmcneill __SHIFTIN(0x8000, TEGRA_T_SATA0_CFG9_BASE_ADDRESS));
277 1.3 jmcneill
278 1.3 jmcneill /* Enable interrupts */
279 1.3 jmcneill tegra_reg_set_clear(bst, bsh, TEGRA_SATA_INTR_MASK_REG,
280 1.3 jmcneill TEGRA_SATA_INTR_MASK_IP_INT, 0);
281 1.3 jmcneill }
282 1.9 jmcneill
283 1.9 jmcneill static int
284 1.9 jmcneill tegra_ahcisata_init_clocks(struct tegra_ahcisata_softc *sc)
285 1.9 jmcneill {
286 1.9 jmcneill device_t self = sc->sc.sc_atac.atac_dev;
287 1.9 jmcneill int error;
288 1.9 jmcneill
289 1.9 jmcneill /* Assert resets */
290 1.9 jmcneill fdtbus_reset_assert(sc->sc_rst_sata);
291 1.9 jmcneill fdtbus_reset_assert(sc->sc_rst_sata_cold);
292 1.9 jmcneill
293 1.10 jmcneill /* Set SATA_OOB clock source to 204MHz */
294 1.9 jmcneill error = clk_set_rate(sc->sc_clk_sata_oob, 204000000);
295 1.9 jmcneill if (error) {
296 1.9 jmcneill aprint_error_dev(self, "couldn't set sata-oob rate: %d\n",
297 1.9 jmcneill error);
298 1.9 jmcneill return error;
299 1.9 jmcneill }
300 1.9 jmcneill
301 1.10 jmcneill /* Set SATA clock source to 102MHz */
302 1.9 jmcneill error = clk_set_rate(sc->sc_clk_sata, 102000000);
303 1.9 jmcneill if (error) {
304 1.9 jmcneill aprint_error_dev(self, "couldn't set sata rate: %d\n", error);
305 1.9 jmcneill return error;
306 1.9 jmcneill }
307 1.9 jmcneill
308 1.9 jmcneill /* Ungate SAX partition in the PMC */
309 1.9 jmcneill tegra_pmc_power(PMC_PARTID_SAX, true);
310 1.9 jmcneill delay(20);
311 1.9 jmcneill
312 1.9 jmcneill /* Remove clamping from SAX partition in the PMC */
313 1.9 jmcneill tegra_pmc_remove_clamping(PMC_PARTID_SAX);
314 1.9 jmcneill delay(20);
315 1.9 jmcneill
316 1.9 jmcneill /* Un-gate clocks and enable CML clock for SATA */
317 1.9 jmcneill error = clk_enable(sc->sc_clk_sata);
318 1.9 jmcneill if (error) {
319 1.9 jmcneill aprint_error_dev(self, "couldn't enable sata: %d\n", error);
320 1.9 jmcneill return error;
321 1.9 jmcneill }
322 1.9 jmcneill error = clk_enable(sc->sc_clk_sata_oob);
323 1.9 jmcneill if (error) {
324 1.9 jmcneill aprint_error_dev(self, "couldn't enable sata-oob: %d\n", error);
325 1.9 jmcneill return error;
326 1.9 jmcneill }
327 1.9 jmcneill error = clk_enable(sc->sc_clk_cml1);
328 1.9 jmcneill if (error) {
329 1.9 jmcneill aprint_error_dev(self, "couldn't enable cml1: %d\n", error);
330 1.9 jmcneill return error;
331 1.9 jmcneill }
332 1.9 jmcneill
333 1.9 jmcneill /* De-assert resets */
334 1.9 jmcneill fdtbus_reset_deassert(sc->sc_rst_sata);
335 1.9 jmcneill fdtbus_reset_deassert(sc->sc_rst_sata_cold);
336 1.9 jmcneill
337 1.9 jmcneill return 0;
338 1.9 jmcneill }
339