svwsata.c revision 1.4 1 1.4 christos /* $NetBSD: svwsata.c,v 1.4 2006/10/12 01:31:33 christos Exp $ */
2 1.1 bouyer
3 1.1 bouyer /*
4 1.1 bouyer * Copyright (c) 2005 Mark Kettenis
5 1.1 bouyer *
6 1.1 bouyer * Permission to use, copy, modify, and distribute this software for any
7 1.1 bouyer * purpose with or without fee is hereby granted, provided that the above
8 1.1 bouyer * copyright notice and this permission notice appear in all copies.
9 1.1 bouyer *
10 1.1 bouyer * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
11 1.1 bouyer * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
12 1.1 bouyer * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
13 1.1 bouyer * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
14 1.1 bouyer * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
15 1.1 bouyer * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
16 1.1 bouyer * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
17 1.1 bouyer */
18 1.1 bouyer
19 1.1 bouyer #include <sys/cdefs.h>
20 1.4 christos __KERNEL_RCSID(0, "$NetBSD: svwsata.c,v 1.4 2006/10/12 01:31:33 christos Exp $");
21 1.1 bouyer
22 1.1 bouyer #include <sys/param.h>
23 1.1 bouyer #include <sys/systm.h>
24 1.1 bouyer
25 1.1 bouyer #include <dev/ata/atareg.h>
26 1.1 bouyer #include <dev/ata/satareg.h>
27 1.1 bouyer #include <dev/ata/satavar.h>
28 1.1 bouyer #include <dev/pci/pcivar.h>
29 1.1 bouyer #include <dev/pci/pcidevs.h>
30 1.1 bouyer #include <dev/pci/pciidereg.h>
31 1.1 bouyer #include <dev/pci/pciidevar.h>
32 1.1 bouyer #include <dev/pci/pciide_svwsata_reg.h>
33 1.1 bouyer
34 1.1 bouyer static int svwsata_match(struct device *, struct cfdata *, void *);
35 1.1 bouyer static void svwsata_attach(struct device *, struct device *, void *);
36 1.1 bouyer
37 1.1 bouyer static void svwsata_chip_map(struct pciide_softc *, struct pci_attach_args *) __unused;
38 1.1 bouyer static void svwsata_mapreg_dma(struct pciide_softc *, struct pci_attach_args *);
39 1.1 bouyer static void svwsata_mapchan(struct pciide_channel *);
40 1.1 bouyer static void svwsata_drv_probe(struct ata_channel *chp);
41 1.1 bouyer
42 1.1 bouyer CFATTACH_DECL(svwsata, sizeof(struct pciide_softc),
43 1.1 bouyer svwsata_match, svwsata_attach, NULL, NULL);
44 1.1 bouyer
45 1.1 bouyer static const struct pciide_product_desc pciide_svwsata_products[] = {
46 1.1 bouyer { PCI_PRODUCT_SERVERWORKS_K2_SATA,
47 1.1 bouyer 0,
48 1.1 bouyer "ServerWorks K2 SATA Controller",
49 1.1 bouyer svwsata_chip_map
50 1.1 bouyer },
51 1.2 bouyer { PCI_PRODUCT_SERVERWORKS_FRODO4_SATA,
52 1.2 bouyer 0,
53 1.2 bouyer "ServerWorks Frodo4 SATA Controller",
54 1.2 bouyer svwsata_chip_map
55 1.2 bouyer },
56 1.2 bouyer { PCI_PRODUCT_SERVERWORKS_FRODO8_SATA,
57 1.2 bouyer 0,
58 1.2 bouyer "ServerWorks Frodo8 SATA Controller",
59 1.2 bouyer svwsata_chip_map
60 1.2 bouyer },
61 1.2 bouyer { PCI_PRODUCT_SERVERWORKS_HT1000_SATA,
62 1.2 bouyer 0,
63 1.2 bouyer "ServerWorks HT-1000 SATA Controller",
64 1.2 bouyer svwsata_chip_map
65 1.2 bouyer },
66 1.1 bouyer { 0,
67 1.1 bouyer 0,
68 1.1 bouyer NULL,
69 1.1 bouyer NULL,
70 1.1 bouyer }
71 1.1 bouyer };
72 1.1 bouyer
73 1.1 bouyer static int
74 1.4 christos svwsata_match(struct device *parent __unused, struct cfdata *match __unused,
75 1.4 christos void *aux)
76 1.1 bouyer {
77 1.1 bouyer struct pci_attach_args *pa = aux;
78 1.1 bouyer
79 1.1 bouyer if (PCI_VENDOR(pa->pa_id) == PCI_VENDOR_SERVERWORKS) {
80 1.1 bouyer if (pciide_lookup_product(pa->pa_id,
81 1.1 bouyer pciide_svwsata_products))
82 1.1 bouyer return (2);
83 1.1 bouyer }
84 1.1 bouyer return (0);
85 1.1 bouyer }
86 1.1 bouyer
87 1.1 bouyer static void
88 1.4 christos svwsata_attach(struct device *parent __unused, struct device *self, void *aux)
89 1.1 bouyer {
90 1.1 bouyer struct pci_attach_args *pa = aux;
91 1.1 bouyer struct pciide_softc *sc = (void *)self;
92 1.1 bouyer
93 1.1 bouyer pciide_common_attach(sc, pa,
94 1.1 bouyer pciide_lookup_product(pa->pa_id, pciide_svwsata_products));
95 1.1 bouyer }
96 1.1 bouyer
97 1.1 bouyer static void
98 1.1 bouyer svwsata_chip_map(struct pciide_softc *sc, struct pci_attach_args *pa)
99 1.1 bouyer {
100 1.1 bouyer struct pciide_channel *cp;
101 1.1 bouyer pci_intr_handle_t intrhandle;
102 1.1 bouyer pcireg_t interface;
103 1.1 bouyer const char *intrstr;
104 1.1 bouyer int channel;
105 1.1 bouyer
106 1.1 bouyer if (pciide_chipen(sc, pa) == 0)
107 1.1 bouyer return;
108 1.1 bouyer
109 1.2 bouyer /* The 4-port version has a dummy second function. */
110 1.2 bouyer if (pci_conf_read(sc->sc_pc, sc->sc_tag,
111 1.2 bouyer PCI_MAPREG_START + 0x14) == 0) {
112 1.2 bouyer aprint_normal("\n");
113 1.2 bouyer return;
114 1.2 bouyer }
115 1.2 bouyer
116 1.1 bouyer if (pci_mapreg_map(pa, PCI_MAPREG_START + 0x14,
117 1.1 bouyer PCI_MAPREG_TYPE_MEM |
118 1.1 bouyer PCI_MAPREG_MEM_TYPE_32BIT, 0,
119 1.1 bouyer &sc->sc_ba5_st, &sc->sc_ba5_sh,
120 1.1 bouyer NULL, NULL) != 0) {
121 1.1 bouyer aprint_error(": unable to map BA5 register space\n");
122 1.1 bouyer return;
123 1.1 bouyer }
124 1.1 bouyer
125 1.1 bouyer aprint_normal(": DMA");
126 1.1 bouyer svwsata_mapreg_dma(sc, pa);
127 1.1 bouyer aprint_normal("\n");
128 1.1 bouyer
129 1.1 bouyer sc->sc_wdcdev.sc_atac.atac_cap |= ATAC_CAP_DATA16 | ATAC_CAP_DATA32;
130 1.1 bouyer sc->sc_wdcdev.sc_atac.atac_pio_cap = 4;
131 1.1 bouyer if (sc->sc_dma_ok) {
132 1.1 bouyer sc->sc_wdcdev.sc_atac.atac_cap |= ATAC_CAP_DMA | ATAC_CAP_UDMA;
133 1.1 bouyer sc->sc_wdcdev.irqack = pciide_irqack;
134 1.1 bouyer sc->sc_wdcdev.sc_atac.atac_dma_cap = 2;
135 1.1 bouyer sc->sc_wdcdev.sc_atac.atac_udma_cap = 6;
136 1.1 bouyer }
137 1.1 bouyer
138 1.1 bouyer sc->sc_wdcdev.sc_atac.atac_channels = sc->wdc_chanarray;
139 1.1 bouyer sc->sc_wdcdev.sc_atac.atac_nchannels = 4;
140 1.1 bouyer sc->sc_wdcdev.sc_atac.atac_set_modes = sata_setup_channel;
141 1.1 bouyer
142 1.1 bouyer /* We can use SControl and SStatus to probe for drives. */
143 1.1 bouyer sc->sc_wdcdev.sc_atac.atac_probe = svwsata_drv_probe;
144 1.1 bouyer
145 1.1 bouyer wdc_allocate_regs(&sc->sc_wdcdev);
146 1.1 bouyer
147 1.1 bouyer /* Map and establish the interrupt handler. */
148 1.1 bouyer if(pci_intr_map(pa, &intrhandle) != 0) {
149 1.1 bouyer aprint_error("%s: couldn't map native-PCI interrupt\n",
150 1.1 bouyer sc->sc_wdcdev.sc_atac.atac_dev.dv_xname);
151 1.1 bouyer return;
152 1.1 bouyer }
153 1.1 bouyer intrstr = pci_intr_string(pa->pa_pc, intrhandle);
154 1.1 bouyer sc->sc_pci_ih = pci_intr_establish(pa->pa_pc, intrhandle, IPL_BIO,
155 1.1 bouyer pciide_pci_intr, sc);
156 1.1 bouyer if (sc->sc_pci_ih != NULL) {
157 1.1 bouyer aprint_normal("%s: using %s for native-PCI interrupt\n",
158 1.1 bouyer sc->sc_wdcdev.sc_atac.atac_dev.dv_xname,
159 1.1 bouyer intrstr ? intrstr : "unknown interrupt");
160 1.1 bouyer } else {
161 1.1 bouyer aprint_error("%s: couldn't establish native-PCI interrupt",
162 1.1 bouyer sc->sc_wdcdev.sc_atac.atac_dev.dv_xname);
163 1.1 bouyer if (intrstr != NULL)
164 1.1 bouyer aprint_normal(" at %s", intrstr);
165 1.1 bouyer aprint_normal("\n");
166 1.1 bouyer return;
167 1.1 bouyer }
168 1.1 bouyer
169 1.1 bouyer interface = PCIIDE_INTERFACE_BUS_MASTER_DMA |
170 1.1 bouyer PCIIDE_INTERFACE_PCI(0) | PCIIDE_INTERFACE_PCI(1);
171 1.1 bouyer
172 1.1 bouyer
173 1.1 bouyer for (channel = 0; channel < sc->sc_wdcdev.sc_atac.atac_nchannels;
174 1.1 bouyer channel++) {
175 1.1 bouyer cp = &sc->pciide_channels[channel];
176 1.1 bouyer
177 1.1 bouyer if (pciide_chansetup(sc, channel, interface) == 0)
178 1.1 bouyer continue;
179 1.1 bouyer svwsata_mapchan(cp);
180 1.1 bouyer }
181 1.1 bouyer }
182 1.1 bouyer
183 1.1 bouyer static void
184 1.1 bouyer svwsata_mapreg_dma(struct pciide_softc *sc, struct pci_attach_args *pa)
185 1.1 bouyer {
186 1.1 bouyer struct pciide_channel *pc;
187 1.1 bouyer int chan, reg;
188 1.1 bouyer bus_size_t size;
189 1.1 bouyer
190 1.1 bouyer sc->sc_wdcdev.dma_arg = sc;
191 1.1 bouyer sc->sc_wdcdev.dma_init = pciide_dma_init;
192 1.1 bouyer sc->sc_wdcdev.dma_start = pciide_dma_start;
193 1.1 bouyer sc->sc_wdcdev.dma_finish = pciide_dma_finish;
194 1.1 bouyer
195 1.3 thorpej if (device_cfdata(&sc->sc_wdcdev.sc_atac.atac_dev)->cf_flags &
196 1.1 bouyer PCIIDE_OPTIONS_NODMA) {
197 1.1 bouyer aprint_normal(
198 1.1 bouyer ", but unused (forced off by config file)");
199 1.1 bouyer sc->sc_dma_ok = 0;
200 1.1 bouyer return;
201 1.1 bouyer }
202 1.1 bouyer
203 1.1 bouyer /*
204 1.1 bouyer * Slice off a subregion of BA5 for each of the channel's DMA
205 1.1 bouyer * registers.
206 1.1 bouyer */
207 1.1 bouyer
208 1.1 bouyer sc->sc_dma_iot = sc->sc_ba5_st;
209 1.1 bouyer for (chan = 0; chan < 4; chan++) {
210 1.1 bouyer pc = &sc->pciide_channels[chan];
211 1.1 bouyer for (reg = 0; reg < IDEDMA_NREGS; reg++) {
212 1.1 bouyer size = 4;
213 1.1 bouyer if (size > (IDEDMA_SCH_OFFSET - reg))
214 1.1 bouyer size = IDEDMA_SCH_OFFSET - reg;
215 1.1 bouyer if (bus_space_subregion(sc->sc_ba5_st,
216 1.1 bouyer sc->sc_ba5_sh,
217 1.1 bouyer (chan << 8) + SVWSATA_DMA + reg,
218 1.1 bouyer size, &pc->dma_iohs[reg]) != 0) {
219 1.1 bouyer sc->sc_dma_ok = 0;
220 1.1 bouyer aprint_normal(", but can't subregion offset "
221 1.1 bouyer "%lu size %lu",
222 1.1 bouyer (u_long) (chan << 8) + SVWSATA_DMA + reg,
223 1.1 bouyer (u_long) size);
224 1.1 bouyer return;
225 1.1 bouyer }
226 1.1 bouyer }
227 1.1 bouyer }
228 1.1 bouyer
229 1.1 bouyer /* DMA registers all set up! */
230 1.1 bouyer sc->sc_dmat = pa->pa_dmat;
231 1.1 bouyer sc->sc_dma_ok = 1;
232 1.1 bouyer }
233 1.1 bouyer
234 1.1 bouyer static void
235 1.1 bouyer svwsata_mapchan(struct pciide_channel *cp)
236 1.1 bouyer {
237 1.1 bouyer struct ata_channel *wdc_cp = &cp->ata_channel;
238 1.1 bouyer struct pciide_softc *sc = CHAN_TO_PCIIDE(wdc_cp);
239 1.1 bouyer struct wdc_regs *wdr = CHAN_TO_WDC_REGS(wdc_cp);
240 1.1 bouyer int i;
241 1.1 bouyer
242 1.1 bouyer cp->compat = 0;
243 1.1 bouyer cp->ih = sc->sc_pci_ih;
244 1.1 bouyer
245 1.1 bouyer wdr->cmd_iot = sc->sc_ba5_st;
246 1.1 bouyer if (bus_space_subregion(sc->sc_ba5_st, sc->sc_ba5_sh,
247 1.1 bouyer (wdc_cp->ch_channel << 8) + SVWSATA_TF0,
248 1.1 bouyer SVWSATA_TF8 - SVWSATA_TF0, &wdr->cmd_baseioh) != 0) {
249 1.1 bouyer aprint_error("%s: couldn't map %s cmd regs\n",
250 1.1 bouyer sc->sc_wdcdev.sc_atac.atac_dev.dv_xname, cp->name);
251 1.1 bouyer goto bad;
252 1.1 bouyer }
253 1.1 bouyer
254 1.1 bouyer wdr->ctl_iot = sc->sc_ba5_st;
255 1.1 bouyer if (bus_space_subregion(sc->sc_ba5_st, sc->sc_ba5_sh,
256 1.1 bouyer (wdc_cp->ch_channel << 8) + SVWSATA_TF8, 4,
257 1.1 bouyer &cp->ctl_baseioh) != 0) {
258 1.1 bouyer aprint_error("%s: couldn't map %s ctl regs\n",
259 1.1 bouyer sc->sc_wdcdev.sc_atac.atac_dev.dv_xname, cp->name);
260 1.1 bouyer goto bad;
261 1.1 bouyer }
262 1.1 bouyer wdr->ctl_ioh = cp->ctl_baseioh;
263 1.1 bouyer
264 1.1 bouyer for (i = 0; i < WDC_NREG; i++) {
265 1.1 bouyer if (bus_space_subregion(wdr->cmd_iot, wdr->cmd_baseioh,
266 1.1 bouyer i << 2, i == 0 ? 4 : 1,
267 1.1 bouyer &wdr->cmd_iohs[i]) != 0) {
268 1.1 bouyer aprint_error("%s: couldn't subregion %s channel "
269 1.1 bouyer "cmd regs\n",
270 1.1 bouyer sc->sc_wdcdev.sc_atac.atac_dev.dv_xname, cp->name);
271 1.1 bouyer goto bad;
272 1.1 bouyer }
273 1.1 bouyer }
274 1.1 bouyer wdc_init_shadow_regs(wdc_cp);
275 1.1 bouyer wdr->data32iot = wdr->cmd_iot;
276 1.1 bouyer wdr->data32ioh = wdr->cmd_iohs[0];
277 1.1 bouyer
278 1.1 bouyer wdcattach(wdc_cp);
279 1.1 bouyer return;
280 1.1 bouyer
281 1.1 bouyer bad:
282 1.1 bouyer cp->ata_channel.ch_flags |= ATACH_DISABLED;
283 1.1 bouyer }
284 1.1 bouyer
285 1.1 bouyer static void
286 1.1 bouyer svwsata_drv_probe(struct ata_channel *chp)
287 1.1 bouyer {
288 1.1 bouyer struct pciide_softc *sc = CHAN_TO_PCIIDE(chp);
289 1.1 bouyer struct wdc_regs *wdr = CHAN_TO_WDC_REGS(chp);
290 1.1 bouyer int channel = chp->ch_channel;
291 1.1 bouyer uint32_t scontrol, sstatus;
292 1.1 bouyer uint8_t scnt, sn, cl, ch;
293 1.1 bouyer int i, s;
294 1.1 bouyer
295 1.1 bouyer /* XXX This should be done by other code. */
296 1.1 bouyer for (i = 0; i < 2; i++) {
297 1.1 bouyer chp->ch_drive[i].chnl_softc = chp;
298 1.1 bouyer chp->ch_drive[i].drive = i;
299 1.1 bouyer }
300 1.1 bouyer
301 1.1 bouyer /*
302 1.1 bouyer * Request communication initialization sequence, any speed.
303 1.1 bouyer * Performing this is the equivalent of an ATA Reset.
304 1.1 bouyer */
305 1.1 bouyer scontrol = SControl_DET_INIT | SControl_SPD_ANY;
306 1.1 bouyer
307 1.1 bouyer /*
308 1.1 bouyer * XXX We don't yet support SATA power management; disable all
309 1.1 bouyer * power management state transitions.
310 1.1 bouyer */
311 1.1 bouyer scontrol |= SControl_IPM_NONE;
312 1.1 bouyer
313 1.1 bouyer bus_space_write_4(sc->sc_ba5_st, sc->sc_ba5_sh,
314 1.1 bouyer (channel << 8) + SVWSATA_SCONTROL, scontrol);
315 1.1 bouyer delay(50 * 1000);
316 1.1 bouyer scontrol &= ~SControl_DET_INIT;
317 1.1 bouyer bus_space_write_4(sc->sc_ba5_st, sc->sc_ba5_sh,
318 1.1 bouyer (channel << 8) + SVWSATA_SCONTROL, scontrol);
319 1.1 bouyer delay(50 * 1000);
320 1.1 bouyer
321 1.1 bouyer sstatus = bus_space_read_4(sc->sc_ba5_st, sc->sc_ba5_sh,
322 1.1 bouyer (channel << 8) + SVWSATA_SSTATUS);
323 1.1 bouyer #if 0
324 1.1 bouyer printf("%s: port %d: SStatus=0x%08x, SControl=0x%08x\n",
325 1.1 bouyer sc->sc_wdcdev.sc_atac.atac_dev.dv_xname, chp->ch_channel, sstatus,
326 1.1 bouyer bus_space_read_4(sc->sc_ba5_st, sc->sc_ba5_sh,
327 1.1 bouyer (channel << 8) + SVWSATA_SSTATUS));
328 1.1 bouyer #endif
329 1.1 bouyer switch (sstatus & SStatus_DET_mask) {
330 1.1 bouyer case SStatus_DET_NODEV:
331 1.1 bouyer /* No device; be silent. */
332 1.1 bouyer break;
333 1.1 bouyer
334 1.1 bouyer case SStatus_DET_DEV_NE:
335 1.1 bouyer aprint_error("%s: port %d: device connected, but "
336 1.1 bouyer "communication not established\n",
337 1.1 bouyer sc->sc_wdcdev.sc_atac.atac_dev.dv_xname, chp->ch_channel);
338 1.1 bouyer break;
339 1.1 bouyer
340 1.1 bouyer case SStatus_DET_OFFLINE:
341 1.1 bouyer aprint_error("%s: port %d: PHY offline\n",
342 1.1 bouyer sc->sc_wdcdev.sc_atac.atac_dev.dv_xname, chp->ch_channel);
343 1.1 bouyer break;
344 1.1 bouyer
345 1.1 bouyer case SStatus_DET_DEV:
346 1.1 bouyer /*
347 1.1 bouyer * XXX ATAPI detection doesn't currently work. Don't
348 1.1 bouyer * XXX know why. But, it's not like the standard method
349 1.1 bouyer * XXX can detect an ATAPI device connected via a SATA/PATA
350 1.1 bouyer * XXX bridge, so at least this is no worse. --thorpej
351 1.1 bouyer */
352 1.1 bouyer bus_space_write_1(wdr->cmd_iot, wdr->cmd_iohs[wd_sdh], 0,
353 1.1 bouyer WDSD_IBM | (0 << 4));
354 1.1 bouyer delay(10); /* 400ns delay */
355 1.1 bouyer /* Save register contents. */
356 1.1 bouyer scnt = bus_space_read_1(wdr->cmd_iot,
357 1.1 bouyer wdr->cmd_iohs[wd_seccnt], 0);
358 1.1 bouyer sn = bus_space_read_1(wdr->cmd_iot,
359 1.1 bouyer wdr->cmd_iohs[wd_sector], 0);
360 1.1 bouyer cl = bus_space_read_1(wdr->cmd_iot,
361 1.1 bouyer wdr->cmd_iohs[wd_cyl_lo], 0);
362 1.1 bouyer ch = bus_space_read_1(wdr->cmd_iot,
363 1.1 bouyer wdr->cmd_iohs[wd_cyl_hi], 0);
364 1.1 bouyer #if 0
365 1.1 bouyer printf("%s: port %d: scnt=0x%x sn=0x%x cl=0x%x ch=0x%x\n",
366 1.1 bouyer sc->sc_wdcdev.sc_atac.atac_dev.dv_xname, chp->ch_channel,
367 1.1 bouyer scnt, sn, cl, ch);
368 1.1 bouyer #endif
369 1.1 bouyer /*
370 1.1 bouyer * scnt and sn are supposed to be 0x1 for ATAPI, but in some
371 1.1 bouyer * cases we get wrong values here, so ignore it.
372 1.1 bouyer */
373 1.1 bouyer s = splbio();
374 1.1 bouyer if (cl == 0x14 && ch == 0xeb)
375 1.1 bouyer chp->ch_drive[0].drive_flags |= DRIVE_ATAPI;
376 1.1 bouyer else
377 1.1 bouyer chp->ch_drive[0].drive_flags |= DRIVE_ATA;
378 1.1 bouyer splx(s);
379 1.1 bouyer
380 1.1 bouyer aprint_normal("%s: port %d: device present, speed: %s\n",
381 1.1 bouyer sc->sc_wdcdev.sc_atac.atac_dev.dv_xname, chp->ch_channel,
382 1.1 bouyer sata_speed(sstatus));
383 1.1 bouyer break;
384 1.1 bouyer
385 1.1 bouyer default:
386 1.1 bouyer aprint_error("%s: port %d: unknown SStatus: 0x%08x\n",
387 1.1 bouyer sc->sc_wdcdev.sc_atac.atac_dev.dv_xname, chp->ch_channel, sstatus);
388 1.1 bouyer }
389 1.1 bouyer }
390