mvsata_pci.c revision 1.7 1 1.7 drochner /* $NetBSD: mvsata_pci.c,v 1.7 2012/01/30 19:41:22 drochner Exp $ */
2 1.1 kiyohara /*
3 1.1 kiyohara * Copyright (c) 2008 KIYOHARA Takashi
4 1.1 kiyohara * All rights reserved.
5 1.1 kiyohara *
6 1.1 kiyohara * Redistribution and use in source and binary forms, with or without
7 1.1 kiyohara * modification, are permitted provided that the following conditions
8 1.1 kiyohara * are met:
9 1.1 kiyohara * 1. Redistributions of source code must retain the above copyright
10 1.1 kiyohara * notice, this list of conditions and the following disclaimer.
11 1.1 kiyohara * 2. Redistributions in binary form must reproduce the above copyright
12 1.1 kiyohara * notice, this list of conditions and the following disclaimer in the
13 1.1 kiyohara * documentation and/or other materials provided with the distribution.
14 1.1 kiyohara *
15 1.1 kiyohara * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
16 1.1 kiyohara * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
17 1.1 kiyohara * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
18 1.1 kiyohara * DISCLAIMED. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT,
19 1.1 kiyohara * INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
20 1.1 kiyohara * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
21 1.1 kiyohara * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
22 1.1 kiyohara * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
23 1.1 kiyohara * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN
24 1.1 kiyohara * ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
25 1.1 kiyohara * POSSIBILITY OF SUCH DAMAGE.
26 1.1 kiyohara */
27 1.1 kiyohara
28 1.1 kiyohara #include <sys/cdefs.h>
29 1.7 drochner __KERNEL_RCSID(0, "$NetBSD: mvsata_pci.c,v 1.7 2012/01/30 19:41:22 drochner Exp $");
30 1.1 kiyohara
31 1.1 kiyohara #include <sys/param.h>
32 1.1 kiyohara #include <sys/bus.h>
33 1.1 kiyohara #include <sys/device.h>
34 1.1 kiyohara #include <sys/errno.h>
35 1.1 kiyohara #include <sys/pmf.h>
36 1.1 kiyohara
37 1.1 kiyohara #include <dev/pci/pcivar.h>
38 1.1 kiyohara #include <dev/pci/pcidevs.h>
39 1.1 kiyohara #include <dev/pci/pciidereg.h>
40 1.1 kiyohara #include <dev/pci/pciidevar.h>
41 1.1 kiyohara
42 1.1 kiyohara #include <dev/ic/mvsatareg.h>
43 1.1 kiyohara #include <dev/ic/mvsatavar.h>
44 1.1 kiyohara
45 1.1 kiyohara #define MVSATA_PCI_HCARBITER_SPACE_OFFSET 0x20000
46 1.1 kiyohara
47 1.1 kiyohara #define MVSATA_PCI_COMMAND 0x00c00
48 1.1 kiyohara #define MVSATA_PCI_COMMAND_MWRITECOMBINE (1 << 4)
49 1.1 kiyohara #define MVSATA_PCI_COMMAND_MREADCOMBINE (1 << 5)
50 1.1 kiyohara #define MVSATA_PCI_SERRMASK 0x00c28
51 1.1 kiyohara #define MVSATA_PCI_MSITRIGGER 0x00c38
52 1.1 kiyohara #define MVSATA_PCI_MODE 0x00d00
53 1.1 kiyohara #define MVSATA_PCI_DISCTIMER 0x00d04
54 1.1 kiyohara #define MVSATA_PCI_EROMBAR 0x00d2c
55 1.1 kiyohara #define MVSATA_PCI_MAINCS 0x00d30
56 1.1 kiyohara #define MVSATA_PCI_MAINCS_SPM (1 << 2) /* stop pci master */
57 1.1 kiyohara #define MVSATA_PCI_MAINCS_PME (1 << 3) /* pci master empty */
58 1.1 kiyohara #define MVSATA_PCI_MAINCS_GSR (1 << 4) /* glab soft reset */
59 1.1 kiyohara #define MVSATA_PCI_E_IRQCAUSE 0x01900
60 1.1 kiyohara #define MVSATA_PCI_E_IRQMASK 0x01910
61 1.1 kiyohara #define MVSATA_PCI_XBARTIMEOUT 0x01d04
62 1.1 kiyohara #define MVSATA_PCI_ERRLOWADDR 0x01d40
63 1.1 kiyohara #define MVSATA_PCI_ERRHIGHADDR 0x01d44
64 1.1 kiyohara #define MVSATA_PCI_ERRATTRIBUTE 0x01d48
65 1.1 kiyohara #define MVSATA_PCI_ERRCOMMAND 0x01d50
66 1.1 kiyohara #define MVSATA_PCI_IRQCAUSE 0x01d58
67 1.1 kiyohara #define MVSATA_PCI_IRQMASK 0x01d5c
68 1.1 kiyohara #define MVSATA_PCI_MAINIRQCAUSE 0x01d60
69 1.1 kiyohara #define MVSATA_PCI_MAINIRQMASK 0x01d64
70 1.1 kiyohara #define MVSATA_PCI_MAINIRQ_SATAERR(hc, port) \
71 1.1 kiyohara (1 << (((port) << 1) + (hc) * 9))
72 1.1 kiyohara #define MVSATA_PCI_MAINIRQ_SATADONE(hc, port) \
73 1.1 kiyohara (1 << (((port) << 1) + (hc) * 9 + 1))
74 1.1 kiyohara #define MVSATA_PCI_MAINIRQ_SATACOALDONE(hc) (1 << ((hc) * 9 + 8))
75 1.1 kiyohara #define MVSATA_PCI_MAINIRQ_PCI (1 << 18)
76 1.1 kiyohara #define MVSATA_PCI_FLASHCTL 0x1046c
77 1.1 kiyohara #define MVSATA_PCI_GPIOPORTCTL 0x104f0
78 1.1 kiyohara #define MVSATA_PCI_RESETCFG 0x180d8
79 1.1 kiyohara
80 1.1 kiyohara #define MVSATA_PCI_DEV(psc) (psc->psc_sc.sc_wdcdev.sc_atac.atac_dev)
81 1.1 kiyohara
82 1.1 kiyohara
83 1.1 kiyohara struct mvsata_pci_softc {
84 1.1 kiyohara struct mvsata_softc psc_sc;
85 1.1 kiyohara
86 1.1 kiyohara pci_chipset_tag_t psc_pc;
87 1.1 kiyohara pcitag_t psc_tag;
88 1.1 kiyohara
89 1.1 kiyohara bus_space_tag_t psc_iot;
90 1.1 kiyohara bus_space_handle_t psc_ioh;
91 1.1 kiyohara
92 1.1 kiyohara void *psc_ih;
93 1.1 kiyohara };
94 1.1 kiyohara
95 1.1 kiyohara
96 1.1 kiyohara static int mvsata_pci_match(device_t, struct cfdata *, void *);
97 1.1 kiyohara static void mvsata_pci_attach(device_t, device_t, void *);
98 1.1 kiyohara static int mvsata_pci_detach(device_t, int);
99 1.1 kiyohara
100 1.1 kiyohara static int mvsata_pci_intr(void *);
101 1.3 dyoung static bool mvsata_pci_resume(device_t, const pmf_qual_t *qual);
102 1.1 kiyohara
103 1.1 kiyohara static int mvsata_pci_sreset(struct mvsata_softc *);
104 1.1 kiyohara static int mvsata_pci_misc_reset(struct mvsata_softc *);
105 1.1 kiyohara static void mvsata_pci_enable_intr(struct mvsata_port *, int);
106 1.1 kiyohara
107 1.1 kiyohara
108 1.1 kiyohara CFATTACH_DECL_NEW(mvsata_pci, sizeof(struct mvsata_pci_softc),
109 1.1 kiyohara mvsata_pci_match, mvsata_pci_attach, mvsata_pci_detach, NULL);
110 1.1 kiyohara
111 1.4 kiyohara struct mvsata_product mvsata_pci_products[] = {
112 1.4 kiyohara #define PCI_VP(v, p) PCI_VENDOR_ ## v, PCI_PRODUCT_ ## v ## _ ## p
113 1.4 kiyohara { PCI_VP(MARVELL, 88SX5040), 1, 4, gen1, 0 },
114 1.4 kiyohara { PCI_VP(MARVELL, 88SX5041), 1, 4, gen1, 0 },
115 1.4 kiyohara { PCI_VP(MARVELL, 88SX5080), 2, 4, gen1, 0 },
116 1.4 kiyohara { PCI_VP(MARVELL, 88SX5081), 2, 4, gen1, 0 },
117 1.4 kiyohara { PCI_VP(MARVELL, 88SX6040), 1, 4, gen2, 0 },
118 1.4 kiyohara { PCI_VP(MARVELL, 88SX6041), 1, 4, gen2, 0 },
119 1.6 jakllsch { PCI_VP(ADP2, 1420SA), 1, 4, gen2, 0 }, /* 88SX6041 */
120 1.4 kiyohara { PCI_VP(MARVELL, 88SX6042), 1, 4, gen2e, 0 },
121 1.4 kiyohara { PCI_VP(MARVELL, 88SX6080), 2, 4, gen2, MVSATA_FLAGS_PCIE },
122 1.4 kiyohara { PCI_VP(MARVELL, 88SX6081), 2, 4, gen2, MVSATA_FLAGS_PCIE },
123 1.4 kiyohara { PCI_VP(MARVELL, 88SX7042), 1, 4, gen2e, 0 },
124 1.6 jakllsch { PCI_VP(ADP2, 1430SA), 1, 4, gen2e, 0 }, /* 88SX7042 */
125 1.4 kiyohara { PCI_VP(TRIONES, ROCKETRAID_2310), 1, 4, gen2e, 0 },
126 1.4 kiyohara #undef PCI_VP
127 1.4 kiyohara };
128 1.4 kiyohara
129 1.1 kiyohara
130 1.1 kiyohara /*
131 1.1 kiyohara * mvsata_pci_match()
132 1.1 kiyohara * This function returns 2, because mvsata is high priority more than pciide.
133 1.1 kiyohara */
134 1.1 kiyohara static int
135 1.1 kiyohara mvsata_pci_match(device_t parent, struct cfdata *match, void *aux)
136 1.1 kiyohara {
137 1.1 kiyohara struct pci_attach_args *pa = aux;
138 1.4 kiyohara int i;
139 1.1 kiyohara
140 1.4 kiyohara for (i = 0; i < __arraycount(mvsata_pci_products); i++)
141 1.4 kiyohara if (PCI_VENDOR(pa->pa_id) == mvsata_pci_products[i].vendor &&
142 1.4 kiyohara PCI_PRODUCT(pa->pa_id) == mvsata_pci_products[i].model)
143 1.1 kiyohara return 2;
144 1.1 kiyohara return 0;
145 1.1 kiyohara }
146 1.1 kiyohara
147 1.1 kiyohara static void
148 1.1 kiyohara mvsata_pci_attach(device_t parent, device_t self, void *aux)
149 1.1 kiyohara {
150 1.1 kiyohara struct pci_attach_args *pa = aux;
151 1.1 kiyohara struct mvsata_pci_softc *psc = device_private(self);
152 1.1 kiyohara struct mvsata_softc *sc = &psc->psc_sc;
153 1.1 kiyohara pci_intr_handle_t intrhandle;
154 1.1 kiyohara pcireg_t csr;
155 1.1 kiyohara bus_size_t size;
156 1.1 kiyohara uint32_t reg, mask;
157 1.4 kiyohara int read_pre_amps, hc, port, rv, i;
158 1.1 kiyohara const char *intrstr;
159 1.1 kiyohara
160 1.1 kiyohara sc->sc_wdcdev.sc_atac.atac_dev = self;
161 1.1 kiyohara sc->sc_model = PCI_PRODUCT(pa->pa_id);
162 1.1 kiyohara sc->sc_rev = PCI_REVISION(pa->pa_class);
163 1.1 kiyohara sc->sc_dmat = pa->pa_dmat;
164 1.1 kiyohara sc->sc_enable_intr = mvsata_pci_enable_intr;
165 1.1 kiyohara
166 1.7 drochner pci_aprint_devinfo(pa, "Marvell Serial-ATA Host Controller");
167 1.1 kiyohara
168 1.1 kiyohara /* Map I/O register */
169 1.1 kiyohara if (pci_mapreg_map(pa, PCI_MAPREG_START,
170 1.1 kiyohara PCI_MAPREG_TYPE_MEM | PCI_MAPREG_MEM_TYPE_32BIT, 0,
171 1.1 kiyohara &psc->psc_iot, &psc->psc_ioh, NULL, &size) != 0) {
172 1.1 kiyohara aprint_error_dev(self, "can't map registers\n");
173 1.1 kiyohara return;
174 1.1 kiyohara }
175 1.1 kiyohara psc->psc_pc = pa->pa_pc;
176 1.1 kiyohara psc->psc_tag = pa->pa_tag;
177 1.1 kiyohara
178 1.1 kiyohara if (bus_space_subregion(psc->psc_iot, psc->psc_ioh,
179 1.1 kiyohara MVSATA_PCI_HCARBITER_SPACE_OFFSET,
180 1.1 kiyohara size - MVSATA_PCI_HCARBITER_SPACE_OFFSET, &sc->sc_ioh)) {
181 1.1 kiyohara aprint_error_dev(self, "can't subregion registers\n");
182 1.1 kiyohara return;
183 1.1 kiyohara }
184 1.1 kiyohara sc->sc_iot = psc->psc_iot;
185 1.1 kiyohara
186 1.1 kiyohara /* Enable device */
187 1.1 kiyohara csr = pci_conf_read(psc->psc_pc, psc->psc_tag, PCI_COMMAND_STATUS_REG);
188 1.1 kiyohara csr |= PCI_COMMAND_MASTER_ENABLE;
189 1.1 kiyohara pci_conf_write(psc->psc_pc, psc->psc_tag, PCI_COMMAND_STATUS_REG, csr);
190 1.1 kiyohara
191 1.1 kiyohara if (pci_intr_map(pa, &intrhandle) != 0) {
192 1.1 kiyohara aprint_error_dev(self, "couldn't map interrupt\n");
193 1.1 kiyohara return;
194 1.1 kiyohara }
195 1.1 kiyohara intrstr = pci_intr_string(psc->psc_pc, intrhandle);
196 1.1 kiyohara psc->psc_ih = pci_intr_establish(psc->psc_pc, intrhandle, IPL_BIO,
197 1.1 kiyohara mvsata_pci_intr, sc);
198 1.1 kiyohara if (psc->psc_ih == NULL) {
199 1.1 kiyohara aprint_error_dev(self, "couldn't establish interrupt\n");
200 1.1 kiyohara return;
201 1.1 kiyohara }
202 1.1 kiyohara aprint_normal_dev(self, "interrupting at %s\n",
203 1.1 kiyohara intrstr ? intrstr : "unknown interrupt");
204 1.1 kiyohara
205 1.1 kiyohara /*
206 1.1 kiyohara * Check if TWSI serial ROM initialization was triggered.
207 1.1 kiyohara * If so, then PRE/AMP configuration probably are set after
208 1.1 kiyohara * reset by serial ROM. If not then override the PRE/AMP
209 1.1 kiyohara * values.
210 1.1 kiyohara */
211 1.1 kiyohara reg = bus_space_read_4(psc->psc_iot, psc->psc_ioh, MVSATA_PCI_RESETCFG);
212 1.1 kiyohara read_pre_amps = (reg & 0x00000001) ? 1 : 0;
213 1.1 kiyohara
214 1.4 kiyohara for (i = 0; i < __arraycount(mvsata_pci_products); i++)
215 1.4 kiyohara if (PCI_VENDOR(pa->pa_id) == mvsata_pci_products[i].vendor &&
216 1.4 kiyohara PCI_PRODUCT(pa->pa_id) == mvsata_pci_products[i].model)
217 1.4 kiyohara break;
218 1.4 kiyohara KASSERT(i < __arraycount(mvsata_pci_products));
219 1.4 kiyohara
220 1.4 kiyohara rv = mvsata_attach(sc, &mvsata_pci_products[i],
221 1.4 kiyohara mvsata_pci_sreset, mvsata_pci_misc_reset, read_pre_amps);
222 1.1 kiyohara if (rv != 0) {
223 1.1 kiyohara pci_intr_disestablish(psc->psc_pc, psc->psc_ih);
224 1.1 kiyohara return;
225 1.1 kiyohara }
226 1.1 kiyohara
227 1.1 kiyohara mask = MVSATA_PCI_MAINIRQ_PCI;
228 1.1 kiyohara for (hc = 0; hc < sc->sc_hc; hc++)
229 1.1 kiyohara for (port = 0; port < sc->sc_port; port++)
230 1.1 kiyohara mask |=
231 1.1 kiyohara MVSATA_PCI_MAINIRQ_SATAERR(hc, port) |
232 1.1 kiyohara MVSATA_PCI_MAINIRQ_SATADONE(hc, port);
233 1.1 kiyohara bus_space_write_4(psc->psc_iot, psc->psc_ioh, MVSATA_PCI_MAINIRQMASK,
234 1.1 kiyohara mask);
235 1.1 kiyohara
236 1.1 kiyohara if (!pmf_device_register(self, NULL, mvsata_pci_resume))
237 1.1 kiyohara aprint_error_dev(self, "couldn't establish power handler\n");
238 1.1 kiyohara }
239 1.1 kiyohara
240 1.1 kiyohara static int
241 1.1 kiyohara mvsata_pci_detach(device_t self, int flags)
242 1.1 kiyohara {
243 1.1 kiyohara struct mvsata_pci_softc *psc = device_private(self);
244 1.1 kiyohara
245 1.1 kiyohara /* XXXX: needs reset ? */
246 1.1 kiyohara
247 1.1 kiyohara pci_intr_disestablish(psc->psc_pc, psc->psc_ih);
248 1.1 kiyohara pmf_device_deregister(self);
249 1.1 kiyohara return 0;
250 1.1 kiyohara }
251 1.1 kiyohara
252 1.1 kiyohara static int
253 1.1 kiyohara mvsata_pci_intr(void *arg)
254 1.1 kiyohara {
255 1.1 kiyohara struct mvsata_pci_softc *psc = (struct mvsata_pci_softc *)arg;
256 1.1 kiyohara struct mvsata_softc *sc = &psc->psc_sc;
257 1.1 kiyohara uint32_t cause;
258 1.1 kiyohara int hc, port, handled = 0;
259 1.1 kiyohara
260 1.1 kiyohara cause = bus_space_read_4(psc->psc_iot, psc->psc_ioh,
261 1.1 kiyohara MVSATA_PCI_MAINIRQCAUSE);
262 1.1 kiyohara for (hc = 0; hc < sc->sc_hc; hc++)
263 1.1 kiyohara for (port = 0; port < sc->sc_port; port++)
264 1.1 kiyohara if (cause & MVSATA_PCI_MAINIRQ_SATAERR(hc, port)) {
265 1.1 kiyohara struct mvsata_port *mvport;
266 1.1 kiyohara
267 1.1 kiyohara mvport = sc->sc_hcs[hc].hc_ports[port];
268 1.1 kiyohara handled |= mvsata_error(mvport);
269 1.1 kiyohara }
270 1.1 kiyohara for (hc = 0; hc < sc->sc_hc; hc++)
271 1.1 kiyohara if (cause &
272 1.1 kiyohara (MVSATA_PCI_MAINIRQ_SATADONE(hc, 0) |
273 1.1 kiyohara MVSATA_PCI_MAINIRQ_SATADONE(hc, 1) |
274 1.1 kiyohara MVSATA_PCI_MAINIRQ_SATADONE(hc, 2) |
275 1.1 kiyohara MVSATA_PCI_MAINIRQ_SATADONE(hc, 3)))
276 1.1 kiyohara handled |= mvsata_intr(&sc->sc_hcs[hc]);
277 1.1 kiyohara
278 1.1 kiyohara if (cause & MVSATA_PCI_MAINIRQ_PCI) {
279 1.1 kiyohara uint32_t pe_cause;
280 1.1 kiyohara
281 1.1 kiyohara if (sc->sc_flags & MVSATA_FLAGS_PCIE) {
282 1.1 kiyohara pe_cause = bus_space_read_4(psc->psc_iot, psc->psc_ioh,
283 1.1 kiyohara MVSATA_PCI_E_IRQCAUSE);
284 1.1 kiyohara aprint_error_dev(MVSATA_PCI_DEV(psc),
285 1.1 kiyohara "PCIe error: 0x%x\n", pe_cause);
286 1.1 kiyohara bus_space_write_4(psc->psc_iot, psc->psc_ioh,
287 1.1 kiyohara MVSATA_PCI_E_IRQCAUSE, ~pe_cause);
288 1.1 kiyohara } else {
289 1.1 kiyohara pe_cause = bus_space_read_4(psc->psc_iot, psc->psc_ioh,
290 1.1 kiyohara MVSATA_PCI_IRQCAUSE);
291 1.1 kiyohara aprint_error_dev(MVSATA_PCI_DEV(psc),
292 1.1 kiyohara "PCI error: 0x%x\n", pe_cause);
293 1.1 kiyohara bus_space_write_4(psc->psc_iot, psc->psc_ioh,
294 1.1 kiyohara MVSATA_PCI_IRQCAUSE, ~pe_cause);
295 1.1 kiyohara }
296 1.1 kiyohara
297 1.1 kiyohara handled = 1; /* XXXXX */
298 1.1 kiyohara }
299 1.1 kiyohara
300 1.1 kiyohara return handled;
301 1.1 kiyohara }
302 1.1 kiyohara
303 1.1 kiyohara static bool
304 1.3 dyoung mvsata_pci_resume(device_t dev, const pmf_qual_t *qual)
305 1.1 kiyohara {
306 1.1 kiyohara
307 1.1 kiyohara /* not yet... */
308 1.1 kiyohara
309 1.1 kiyohara return true;
310 1.1 kiyohara }
311 1.1 kiyohara
312 1.1 kiyohara
313 1.1 kiyohara static int
314 1.1 kiyohara mvsata_pci_sreset(struct mvsata_softc *sc)
315 1.1 kiyohara {
316 1.1 kiyohara struct mvsata_pci_softc *psc = (struct mvsata_pci_softc *)sc;
317 1.1 kiyohara uint32_t val;
318 1.1 kiyohara int i;
319 1.1 kiyohara
320 1.1 kiyohara val = bus_space_read_4(psc->psc_iot, psc->psc_ioh, MVSATA_PCI_MAINCS);
321 1.1 kiyohara val |= MVSATA_PCI_MAINCS_SPM;
322 1.1 kiyohara bus_space_write_4(psc->psc_iot, psc->psc_ioh, MVSATA_PCI_MAINCS, val);
323 1.1 kiyohara
324 1.1 kiyohara for (i = 0; i < 1000; i++) {
325 1.1 kiyohara delay(1);
326 1.1 kiyohara val = bus_space_read_4(psc->psc_iot, psc->psc_ioh,
327 1.1 kiyohara MVSATA_PCI_MAINCS);
328 1.1 kiyohara if (val & MVSATA_PCI_MAINCS_PME)
329 1.1 kiyohara break;
330 1.1 kiyohara }
331 1.1 kiyohara if (!(val & MVSATA_PCI_MAINCS_PME)) {
332 1.1 kiyohara aprint_error_dev(MVSATA_PCI_DEV(psc),
333 1.1 kiyohara "PCI master won't flush\n");
334 1.1 kiyohara return -1;
335 1.1 kiyohara }
336 1.1 kiyohara
337 1.1 kiyohara /* reset */
338 1.1 kiyohara bus_space_write_4(psc->psc_iot, psc->psc_ioh, MVSATA_PCI_MAINCS,
339 1.1 kiyohara val | MVSATA_PCI_MAINCS_GSR);
340 1.1 kiyohara val = bus_space_read_4(psc->psc_iot, psc->psc_ioh, MVSATA_PCI_MAINCS);
341 1.1 kiyohara delay(5);
342 1.1 kiyohara if (!(val & MVSATA_PCI_MAINCS_GSR)) {
343 1.1 kiyohara aprint_error_dev(MVSATA_PCI_DEV(psc),
344 1.1 kiyohara "can't set global reset\n");
345 1.1 kiyohara return -1;
346 1.1 kiyohara }
347 1.1 kiyohara
348 1.1 kiyohara /* clear reset and *reenable the PCI master* (not mentioned in spec) */
349 1.1 kiyohara val &= ~(MVSATA_PCI_MAINCS_GSR | MVSATA_PCI_MAINCS_SPM);
350 1.1 kiyohara bus_space_write_4(psc->psc_iot, psc->psc_ioh, MVSATA_PCI_MAINCS, val);
351 1.1 kiyohara val = bus_space_read_4(psc->psc_iot, psc->psc_ioh, MVSATA_PCI_MAINCS);
352 1.1 kiyohara delay(5);
353 1.1 kiyohara if (val & MVSATA_PCI_MAINCS_GSR) {
354 1.1 kiyohara aprint_error_dev(MVSATA_PCI_DEV(psc),
355 1.1 kiyohara "can't set global reset\n");
356 1.1 kiyohara return -1;
357 1.1 kiyohara }
358 1.1 kiyohara
359 1.1 kiyohara return 0;
360 1.1 kiyohara }
361 1.1 kiyohara
362 1.1 kiyohara static int
363 1.1 kiyohara mvsata_pci_misc_reset(struct mvsata_softc *sc)
364 1.1 kiyohara {
365 1.1 kiyohara struct mvsata_pci_softc *psc = (struct mvsata_pci_softc *)sc;
366 1.1 kiyohara #define MVSATA_PCI_COMMAND_DEFAULT 0x0107e371
367 1.1 kiyohara #define MVSATA_PCI_COMMAND_PCI_CONVENTIONAL_ONLY 0x800003e0
368 1.1 kiyohara uint32_t val, pci_command = MVSATA_PCI_COMMAND_DEFAULT;
369 1.1 kiyohara
370 1.1 kiyohara bus_space_write_4(psc->psc_iot, psc->psc_ioh, MVSATA_PCI_FLASHCTL,
371 1.1 kiyohara 0x0fcfffff);
372 1.1 kiyohara
373 1.1 kiyohara if (sc->sc_gen == gen2 || sc->sc_gen == gen2e) {
374 1.1 kiyohara val = bus_space_read_4(psc->psc_iot, psc->psc_ioh,
375 1.1 kiyohara MVSATA_PCI_GPIOPORTCTL);
376 1.1 kiyohara val &= 0x3;
377 1.1 kiyohara #if 0
378 1.1 kiyohara val |= 0x00000060;
379 1.1 kiyohara #else /* XXXX */
380 1.1 kiyohara val |= 0x00000070;
381 1.1 kiyohara #endif
382 1.1 kiyohara bus_space_write_4(psc->psc_iot, psc->psc_ioh,
383 1.1 kiyohara MVSATA_PCI_GPIOPORTCTL, val);
384 1.1 kiyohara }
385 1.1 kiyohara
386 1.1 kiyohara if (sc->sc_gen == gen1) {
387 1.1 kiyohara /* Expansion ROM BAR Enable */
388 1.1 kiyohara val = bus_space_read_4(psc->psc_iot, psc->psc_ioh,
389 1.1 kiyohara MVSATA_PCI_EROMBAR);
390 1.1 kiyohara bus_space_write_4(psc->psc_iot, psc->psc_ioh,
391 1.1 kiyohara MVSATA_PCI_EROMBAR, val | 0x00000001);
392 1.1 kiyohara }
393 1.1 kiyohara
394 1.1 kiyohara if (sc->sc_flags & MVSATA_FLAGS_PCIE) {
395 1.1 kiyohara bus_space_write_4(psc->psc_iot, psc->psc_ioh,
396 1.1 kiyohara MVSATA_PCI_MAINIRQMASK, 0);
397 1.1 kiyohara bus_space_write_4(psc->psc_iot, psc->psc_ioh,
398 1.1 kiyohara MVSATA_PCI_E_IRQCAUSE, 0);
399 1.1 kiyohara bus_space_write_4(psc->psc_iot, psc->psc_ioh,
400 1.1 kiyohara MVSATA_PCI_E_IRQMASK, 0);
401 1.1 kiyohara } else {
402 1.1 kiyohara val = bus_space_read_4(psc->psc_iot, psc->psc_ioh,
403 1.1 kiyohara MVSATA_PCI_MODE);
404 1.1 kiyohara bus_space_write_4(psc->psc_iot, psc->psc_ioh,
405 1.1 kiyohara MVSATA_PCI_MODE, val & 0xff00ffff);
406 1.1 kiyohara bus_space_write_4(psc->psc_iot, psc->psc_ioh,
407 1.1 kiyohara MVSATA_PCI_DISCTIMER, 0);
408 1.1 kiyohara bus_space_write_4(psc->psc_iot, psc->psc_ioh,
409 1.1 kiyohara MVSATA_PCI_MSITRIGGER, 0);
410 1.1 kiyohara bus_space_write_4(psc->psc_iot, psc->psc_ioh,
411 1.1 kiyohara MVSATA_PCI_XBARTIMEOUT, 0x000100ff);
412 1.1 kiyohara bus_space_write_4(psc->psc_iot, psc->psc_ioh,
413 1.1 kiyohara MVSATA_PCI_MAINIRQMASK, 0);
414 1.1 kiyohara bus_space_write_4(psc->psc_iot, psc->psc_ioh,
415 1.1 kiyohara MVSATA_PCI_SERRMASK, 0);
416 1.1 kiyohara bus_space_write_4(psc->psc_iot, psc->psc_ioh,
417 1.1 kiyohara MVSATA_PCI_IRQCAUSE, 0);
418 1.1 kiyohara bus_space_write_4(psc->psc_iot, psc->psc_ioh,
419 1.1 kiyohara MVSATA_PCI_IRQMASK, 0);
420 1.1 kiyohara bus_space_write_4(psc->psc_iot, psc->psc_ioh,
421 1.1 kiyohara MVSATA_PCI_ERRLOWADDR, 0);
422 1.1 kiyohara bus_space_write_4(psc->psc_iot, psc->psc_ioh,
423 1.1 kiyohara MVSATA_PCI_ERRHIGHADDR, 0);
424 1.1 kiyohara bus_space_write_4(psc->psc_iot, psc->psc_ioh,
425 1.1 kiyohara MVSATA_PCI_ERRATTRIBUTE, 0);
426 1.1 kiyohara bus_space_write_4(psc->psc_iot, psc->psc_ioh,
427 1.1 kiyohara MVSATA_PCI_ERRCOMMAND, 0);
428 1.1 kiyohara }
429 1.1 kiyohara
430 1.1 kiyohara /* Enable LED */
431 1.1 kiyohara if (sc->sc_gen == gen1) {
432 1.1 kiyohara bus_space_write_4(psc->psc_iot, psc->psc_ioh,
433 1.1 kiyohara MVSATA_PCI_GPIOPORTCTL, 0);
434 1.1 kiyohara
435 1.1 kiyohara /* XXXX: 50xxB2 errata ? */
436 1.1 kiyohara #if 0
437 1.1 kiyohara if (sc->sc_rev == 3) {
438 1.1 kiyohara int port;
439 1.1 kiyohara
440 1.1 kiyohara val = bus_space_read_4(psc->psc_iot, psc->psc_ioh,
441 1.1 kiyohara MVSATA_PCI_GPIOPORTCTL);
442 1.1 kiyohara
443 1.1 kiyohara /* XXXX: check HDD connected */
444 1.1 kiyohara
445 1.1 kiyohara bus_space_write_4(psc->psc_iot, psc->psc_ioh,
446 1.1 kiyohara MVSATA_PCI_GPIOPORTCTL, val);
447 1.1 kiyohara }
448 1.1 kiyohara #endif
449 1.1 kiyohara
450 1.1 kiyohara /* Disable Flash controller clock */
451 1.1 kiyohara val = bus_space_read_4(psc->psc_iot, psc->psc_ioh,
452 1.1 kiyohara MVSATA_PCI_EROMBAR);
453 1.1 kiyohara bus_space_write_4(psc->psc_iot, psc->psc_ioh,
454 1.1 kiyohara MVSATA_PCI_EROMBAR, val & ~0x00000001);
455 1.1 kiyohara } else
456 1.1 kiyohara #if 0
457 1.1 kiyohara bus_space_write_4(psc->psc_iot, psc->psc_ioh,
458 1.1 kiyohara MVSATA_PCI_GPIOPORTCTL, 0x00000060);
459 1.1 kiyohara #else /* XXXX */
460 1.1 kiyohara bus_space_write_4(psc->psc_iot, psc->psc_ioh,
461 1.1 kiyohara MVSATA_PCI_GPIOPORTCTL, 0x00000070);
462 1.1 kiyohara #endif
463 1.1 kiyohara
464 1.1 kiyohara if (sc->sc_flags & MVSATA_FLAGS_PCIE)
465 1.1 kiyohara bus_space_write_4(psc->psc_iot, psc->psc_ioh,
466 1.1 kiyohara MVSATA_PCI_E_IRQMASK, 0x0000070a);
467 1.1 kiyohara else {
468 1.1 kiyohara val = bus_space_read_4(psc->psc_iot, psc->psc_ioh,
469 1.1 kiyohara MVSATA_PCI_MODE);
470 1.1 kiyohara if ((val & 0x30) >> 4) { /* PCI-X */
471 1.1 kiyohara int mv60x1b2 =
472 1.1 kiyohara ((sc->sc_model == PCI_PRODUCT_MARVELL_88SX6041 ||
473 1.1 kiyohara sc->sc_model == PCI_PRODUCT_MARVELL_88SX6081) &&
474 1.1 kiyohara sc->sc_rev == 7);
475 1.1 kiyohara
476 1.1 kiyohara pci_command &=
477 1.1 kiyohara ~MVSATA_PCI_COMMAND_PCI_CONVENTIONAL_ONLY;
478 1.1 kiyohara if (sc->sc_gen == gen1 || mv60x1b2)
479 1.1 kiyohara pci_command &=
480 1.1 kiyohara ~MVSATA_PCI_COMMAND_MWRITECOMBINE;
481 1.1 kiyohara } else
482 1.1 kiyohara if (sc->sc_gen == gen1)
483 1.1 kiyohara pci_command &=
484 1.1 kiyohara ~(MVSATA_PCI_COMMAND_MWRITECOMBINE |
485 1.1 kiyohara MVSATA_PCI_COMMAND_MREADCOMBINE);
486 1.1 kiyohara bus_space_write_4(psc->psc_iot, psc->psc_ioh,
487 1.1 kiyohara MVSATA_PCI_COMMAND, pci_command);
488 1.1 kiyohara
489 1.1 kiyohara #define MVSATA_PCI_INTERRUPT_MASK 0x00d77fe6
490 1.1 kiyohara bus_space_write_4(psc->psc_iot, psc->psc_ioh,
491 1.1 kiyohara MVSATA_PCI_SERRMASK, MVSATA_PCI_INTERRUPT_MASK);
492 1.1 kiyohara bus_space_write_4(psc->psc_iot, psc->psc_ioh,
493 1.1 kiyohara MVSATA_PCI_IRQMASK, MVSATA_PCI_INTERRUPT_MASK);
494 1.1 kiyohara }
495 1.1 kiyohara
496 1.1 kiyohara return 0;
497 1.1 kiyohara }
498 1.1 kiyohara
499 1.1 kiyohara static void
500 1.1 kiyohara mvsata_pci_enable_intr(struct mvsata_port *mvport, int on)
501 1.1 kiyohara {
502 1.1 kiyohara struct mvsata_pci_softc *psc =
503 1.1 kiyohara device_private(mvport->port_ata_channel.ch_atac->atac_dev);
504 1.1 kiyohara uint32_t mask;
505 1.1 kiyohara int hc = mvport->port_hc->hc, port = mvport->port;
506 1.1 kiyohara
507 1.1 kiyohara mask = bus_space_read_4(psc->psc_iot, psc->psc_ioh,
508 1.1 kiyohara MVSATA_PCI_MAINIRQMASK);
509 1.1 kiyohara if (on)
510 1.1 kiyohara mask |= MVSATA_PCI_MAINIRQ_SATADONE(hc, port);
511 1.1 kiyohara else
512 1.1 kiyohara mask &= ~MVSATA_PCI_MAINIRQ_SATADONE(hc, port);
513 1.1 kiyohara bus_space_write_4(psc->psc_iot, psc->psc_ioh, MVSATA_PCI_MAINIRQMASK,
514 1.1 kiyohara mask);
515 1.1 kiyohara }
516