satalink.c revision 1.3 1 1.3 thorpej /* $NetBSD: satalink.c,v 1.3 2003/12/15 00:37:38 thorpej Exp $ */
2 1.2 thorpej
3 1.2 thorpej /*-
4 1.2 thorpej * Copyright (c) 2003 The NetBSD Foundation, Inc.
5 1.2 thorpej * All rights reserved.
6 1.2 thorpej *
7 1.2 thorpej * This code is derived from software contributed to The NetBSD Foundation
8 1.2 thorpej * by Jason R. Thorpe of Wasabi Systems, Inc.
9 1.2 thorpej *
10 1.2 thorpej * Redistribution and use in source and binary forms, with or without
11 1.2 thorpej * modification, are permitted provided that the following conditions
12 1.2 thorpej * are met:
13 1.2 thorpej * 1. Redistributions of source code must retain the above copyright
14 1.2 thorpej * notice, this list of conditions and the following disclaimer.
15 1.2 thorpej * 2. Redistributions in binary form must reproduce the above copyright
16 1.2 thorpej * notice, this list of conditions and the following disclaimer in the
17 1.2 thorpej * documentation and/or other materials provided with the distribution.
18 1.2 thorpej * 3. All advertising materials mentioning features or use of this software
19 1.2 thorpej * must display the following acknowledgement:
20 1.2 thorpej * This product includes software developed by the NetBSD
21 1.2 thorpej * Foundation, Inc. and its contributors.
22 1.2 thorpej * 4. Neither the name of The NetBSD Foundation nor the names of its
23 1.2 thorpej * contributors may be used to endorse or promote products derived
24 1.2 thorpej * from this software without specific prior written permission.
25 1.2 thorpej *
26 1.2 thorpej * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
27 1.2 thorpej * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
28 1.2 thorpej * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
29 1.2 thorpej * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
30 1.2 thorpej * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
31 1.2 thorpej * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
32 1.2 thorpej * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
33 1.2 thorpej * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
34 1.2 thorpej * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
35 1.2 thorpej * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
36 1.2 thorpej * POSSIBILITY OF SUCH DAMAGE.
37 1.1 thorpej */
38 1.1 thorpej
39 1.1 thorpej #include <sys/param.h>
40 1.1 thorpej #include <sys/systm.h>
41 1.1 thorpej #include <sys/malloc.h>
42 1.1 thorpej
43 1.1 thorpej #include <dev/pci/pcivar.h>
44 1.1 thorpej #include <dev/pci/pcidevs.h>
45 1.1 thorpej #include <dev/pci/pciidereg.h>
46 1.1 thorpej #include <dev/pci/pciidevar.h>
47 1.1 thorpej #include <dev/pci/pciide_sii3112_reg.h>
48 1.1 thorpej
49 1.2 thorpej #include <dev/ata/satareg.h>
50 1.1 thorpej
51 1.1 thorpej static int satalink_match(struct device *, struct cfdata *, void *);
52 1.1 thorpej static void satalink_attach(struct device *, struct device *, void *);
53 1.1 thorpej
54 1.1 thorpej CFATTACH_DECL(satalink, sizeof(struct pciide_softc),
55 1.1 thorpej satalink_match, satalink_attach, NULL, NULL);
56 1.1 thorpej
57 1.1 thorpej static void sii3112_chip_map(struct pciide_softc*, struct pci_attach_args*);
58 1.2 thorpej static int sii3112_drv_probe(struct channel_softc*);
59 1.1 thorpej static void sii3112_setup_channel(struct channel_softc*);
60 1.1 thorpej
61 1.1 thorpej static const struct pciide_product_desc pciide_satalink_products[] = {
62 1.1 thorpej { PCI_PRODUCT_CMDTECH_3112,
63 1.1 thorpej 0,
64 1.1 thorpej "Silicon Image SATALink 3112",
65 1.1 thorpej sii3112_chip_map,
66 1.1 thorpej },
67 1.1 thorpej { 0,
68 1.1 thorpej 0,
69 1.1 thorpej NULL,
70 1.1 thorpej NULL
71 1.1 thorpej }
72 1.1 thorpej };
73 1.1 thorpej
74 1.1 thorpej static int
75 1.1 thorpej satalink_match(struct device *parent, struct cfdata *match, void *aux)
76 1.1 thorpej {
77 1.1 thorpej struct pci_attach_args *pa = aux;
78 1.1 thorpej
79 1.1 thorpej if (PCI_VENDOR(pa->pa_id) == PCI_VENDOR_CMDTECH) {
80 1.1 thorpej if (pciide_lookup_product(pa->pa_id, pciide_satalink_products))
81 1.1 thorpej return (2);
82 1.1 thorpej }
83 1.1 thorpej return (0);
84 1.1 thorpej }
85 1.1 thorpej
86 1.1 thorpej static void
87 1.1 thorpej satalink_attach(struct device *parent, struct device *self, void *aux)
88 1.1 thorpej {
89 1.1 thorpej struct pci_attach_args *pa = aux;
90 1.1 thorpej struct pciide_softc *sc = (struct pciide_softc *)self;
91 1.1 thorpej
92 1.1 thorpej pciide_common_attach(sc, pa,
93 1.1 thorpej pciide_lookup_product(pa->pa_id, pciide_satalink_products));
94 1.1 thorpej
95 1.1 thorpej }
96 1.1 thorpej
97 1.2 thorpej static __inline uint32_t
98 1.2 thorpej ba5_read_4(struct pciide_softc *sc, bus_addr_t reg)
99 1.2 thorpej {
100 1.2 thorpej
101 1.2 thorpej if (__predict_true(sc->sc_ba5_en != 0))
102 1.2 thorpej return (bus_space_read_4(sc->sc_ba5_st, sc->sc_ba5_sh, reg));
103 1.2 thorpej
104 1.2 thorpej pci_conf_write(sc->sc_pc, sc->sc_tag, SII3112_BA5_IND_ADDR, reg);
105 1.2 thorpej return (pci_conf_read(sc->sc_pc, sc->sc_tag, SII3112_BA5_IND_DATA));
106 1.2 thorpej }
107 1.2 thorpej
108 1.2 thorpej static __inline void
109 1.2 thorpej ba5_write_4(struct pciide_softc *sc, bus_addr_t reg, uint32_t val)
110 1.2 thorpej {
111 1.2 thorpej
112 1.2 thorpej if (__predict_true(sc->sc_ba5_en != 0))
113 1.2 thorpej bus_space_write_4(sc->sc_ba5_st, sc->sc_ba5_sh, reg, val);
114 1.2 thorpej else {
115 1.2 thorpej pci_conf_write(sc->sc_pc, sc->sc_tag, SII3112_BA5_IND_ADDR,
116 1.2 thorpej reg);
117 1.2 thorpej pci_conf_write(sc->sc_pc, sc->sc_tag, SII3112_BA5_IND_DATA,
118 1.2 thorpej val);
119 1.2 thorpej }
120 1.2 thorpej }
121 1.2 thorpej
122 1.1 thorpej static void
123 1.1 thorpej sii3112_chip_map(struct pciide_softc *sc, struct pci_attach_args *pa)
124 1.1 thorpej {
125 1.1 thorpej struct pciide_channel *cp;
126 1.1 thorpej bus_size_t cmdsize, ctlsize;
127 1.2 thorpej pcireg_t interface, scs_cmd, cfgctl;
128 1.1 thorpej int channel;
129 1.1 thorpej
130 1.1 thorpej if (pciide_chipen(sc, pa) == 0)
131 1.1 thorpej return;
132 1.1 thorpej
133 1.2 thorpej scs_cmd = pci_conf_read(pa->pa_pc, pa->pa_tag, SII3112_SCS_CMD);
134 1.2 thorpej pci_conf_write(pa->pa_pc, pa->pa_tag, SII3112_SCS_CMD,
135 1.2 thorpej scs_cmd & SCS_CMD_BA5_EN);
136 1.2 thorpej
137 1.2 thorpej if (scs_cmd & SCS_CMD_BA5_EN) {
138 1.2 thorpej aprint_verbose("%s: SATALink BA5 register space enabled\n",
139 1.2 thorpej sc->sc_wdcdev.sc_dev.dv_xname);
140 1.2 thorpej if (pci_mapreg_map(pa, PCI_MAPREG_START + 0x14,
141 1.2 thorpej PCI_MAPREG_TYPE_MEM|
142 1.2 thorpej PCI_MAPREG_MEM_TYPE_32BIT, 0,
143 1.2 thorpej &sc->sc_ba5_st, &sc->sc_ba5_sh,
144 1.2 thorpej NULL, NULL) != 0)
145 1.2 thorpej aprint_error("%s: unable to map SATALink BA5 "
146 1.2 thorpej "register space\n", sc->sc_wdcdev.sc_dev.dv_xname);
147 1.2 thorpej else
148 1.2 thorpej sc->sc_ba5_en = 1;
149 1.2 thorpej } else {
150 1.2 thorpej aprint_verbose("%s: SATALink BA5 register space disabled\n",
151 1.2 thorpej sc->sc_wdcdev.sc_dev.dv_xname);
152 1.2 thorpej
153 1.2 thorpej /* Enable indirect BA5 addressing. */
154 1.2 thorpej cfgctl = pci_conf_read(pa->pa_pc, pa->pa_tag,
155 1.2 thorpej SII3112_PCI_CFGCTL);
156 1.2 thorpej pci_conf_write(pa->pa_pc, pa->pa_tag, SII3112_PCI_CFGCTL,
157 1.2 thorpej cfgctl | CFGCTL_BA5INDEN);
158 1.2 thorpej }
159 1.2 thorpej
160 1.1 thorpej aprint_normal("%s: bus-master DMA support present",
161 1.1 thorpej sc->sc_wdcdev.sc_dev.dv_xname);
162 1.1 thorpej pciide_mapreg_dma(sc, pa);
163 1.1 thorpej aprint_normal("\n");
164 1.1 thorpej
165 1.1 thorpej /*
166 1.1 thorpej * Rev. <= 0x01 of the 3112 have a bug that can cause data
167 1.1 thorpej * corruption if DMA transfers cross an 8K boundary. This is
168 1.1 thorpej * apparently hard to tickle, but we'll go ahead and play it
169 1.1 thorpej * safe.
170 1.1 thorpej */
171 1.1 thorpej if (PCI_REVISION(pa->pa_class) <= 0x01) {
172 1.1 thorpej sc->sc_dma_maxsegsz = 8192;
173 1.1 thorpej sc->sc_dma_boundary = 8192;
174 1.1 thorpej }
175 1.1 thorpej
176 1.1 thorpej sc->sc_wdcdev.cap |= WDC_CAPABILITY_DATA16 | WDC_CAPABILITY_DATA32 |
177 1.1 thorpej WDC_CAPABILITY_MODE;
178 1.1 thorpej sc->sc_wdcdev.PIO_cap = 4;
179 1.1 thorpej if (sc->sc_dma_ok) {
180 1.1 thorpej sc->sc_wdcdev.cap |= WDC_CAPABILITY_DMA | WDC_CAPABILITY_UDMA;
181 1.1 thorpej sc->sc_wdcdev.cap |= WDC_CAPABILITY_IRQACK;
182 1.1 thorpej sc->sc_wdcdev.irqack = pciide_irqack;
183 1.1 thorpej sc->sc_wdcdev.DMA_cap = 2;
184 1.1 thorpej sc->sc_wdcdev.UDMA_cap = 6;
185 1.1 thorpej }
186 1.1 thorpej sc->sc_wdcdev.set_modes = sii3112_setup_channel;
187 1.1 thorpej
188 1.2 thorpej /* We can use SControl and SStatus to probe for drives. */
189 1.2 thorpej sc->sc_wdcdev.cap |= WDC_CAPABILITY_DRVPROBE;
190 1.2 thorpej sc->sc_wdcdev.drv_probe = sii3112_drv_probe;
191 1.2 thorpej
192 1.1 thorpej sc->sc_wdcdev.channels = sc->wdc_chanarray;
193 1.1 thorpej sc->sc_wdcdev.nchannels = PCIIDE_NUM_CHANNELS;
194 1.1 thorpej
195 1.1 thorpej /*
196 1.2 thorpej * The 3112 either identifies itself as a RAID storage device
197 1.2 thorpej * or a Misc storage device. Fake up the interface bits for
198 1.2 thorpej * what our driver expects.
199 1.1 thorpej */
200 1.1 thorpej if (PCI_SUBCLASS(pa->pa_class) == PCI_SUBCLASS_MASS_STORAGE_IDE) {
201 1.1 thorpej interface = PCI_INTERFACE(pa->pa_class);
202 1.1 thorpej } else {
203 1.1 thorpej interface = PCIIDE_INTERFACE_BUS_MASTER_DMA |
204 1.1 thorpej PCIIDE_INTERFACE_PCI(0) | PCIIDE_INTERFACE_PCI(1);
205 1.1 thorpej }
206 1.1 thorpej
207 1.1 thorpej for (channel = 0; channel < sc->sc_wdcdev.nchannels; channel++) {
208 1.1 thorpej cp = &sc->pciide_channels[channel];
209 1.1 thorpej if (pciide_chansetup(sc, channel, interface) == 0)
210 1.1 thorpej continue;
211 1.1 thorpej pciide_mapchan(pa, cp, interface, &cmdsize, &ctlsize,
212 1.1 thorpej pciide_pci_intr);
213 1.1 thorpej }
214 1.2 thorpej }
215 1.2 thorpej
216 1.2 thorpej static const char *sata_speed[] = {
217 1.2 thorpej "no negotiated speed",
218 1.2 thorpej "1.5Gb/s",
219 1.2 thorpej "<unknown 2>",
220 1.2 thorpej "<unknown 3>",
221 1.2 thorpej "<unknown 4>",
222 1.2 thorpej "<unknown 5>",
223 1.2 thorpej "<unknown 6>",
224 1.2 thorpej "<unknown 7>",
225 1.2 thorpej "<unknown 8>",
226 1.2 thorpej "<unknown 9>",
227 1.2 thorpej "<unknown 10>",
228 1.2 thorpej "<unknown 11>",
229 1.2 thorpej "<unknown 12>",
230 1.2 thorpej "<unknown 13>",
231 1.2 thorpej "<unknown 14>",
232 1.2 thorpej "<unknown 15>",
233 1.2 thorpej };
234 1.2 thorpej
235 1.2 thorpej static int
236 1.2 thorpej sii3112_drv_probe(struct channel_softc *chp)
237 1.2 thorpej {
238 1.2 thorpej struct pciide_channel *cp = (struct pciide_channel *)chp;
239 1.2 thorpej struct pciide_softc *sc = (struct pciide_softc *)cp->wdc_channel.wdc;
240 1.2 thorpej uint32_t scontrol, sstatus;
241 1.2 thorpej int rv = 0;
242 1.2 thorpej uint8_t scnt, sn, cl, ch;
243 1.2 thorpej
244 1.2 thorpej /*
245 1.2 thorpej * The 3112 is a 2-port part, and only has one drive per channel
246 1.2 thorpej * (each port emulates a master drive).
247 1.2 thorpej */
248 1.2 thorpej
249 1.2 thorpej /*
250 1.2 thorpej * Request communication initialization sequence, any speed.
251 1.2 thorpej * Performing this is the equivalent of an ATA Reset.
252 1.2 thorpej */
253 1.2 thorpej scontrol = SControl_DET_INIT | SControl_SPD_ANY;
254 1.2 thorpej
255 1.2 thorpej /*
256 1.2 thorpej * XXX We don't yet support SATA power management; disable all
257 1.2 thorpej * power management state transitions.
258 1.2 thorpej */
259 1.2 thorpej scontrol |= SControl_IPM_NONE;
260 1.2 thorpej
261 1.2 thorpej ba5_write_4(sc, chp->channel == 0 ? 0x100 : 0x180, scontrol);
262 1.2 thorpej delay(500);
263 1.2 thorpej scontrol &= ~SControl_DET_INIT;
264 1.2 thorpej ba5_write_4(sc, chp->channel == 0 ? 0x100 : 0x180, scontrol);
265 1.2 thorpej delay(500);
266 1.2 thorpej
267 1.2 thorpej sstatus = ba5_read_4(sc, chp->channel == 0 ? 0x104 : 0x184);
268 1.2 thorpej switch (sstatus & SStatus_DET_mask) {
269 1.2 thorpej case SStatus_DET_NODEV:
270 1.2 thorpej /* No device; be silent. */
271 1.2 thorpej break;
272 1.2 thorpej
273 1.2 thorpej case SStatus_DET_DEV_NE:
274 1.2 thorpej aprint_error("%s: port %d: device connected, but "
275 1.2 thorpej "communication not established\n",
276 1.2 thorpej sc->sc_wdcdev.sc_dev.dv_xname, chp->channel);
277 1.2 thorpej break;
278 1.2 thorpej
279 1.2 thorpej case SStatus_DET_OFFLINE:
280 1.2 thorpej aprint_error("%s: port %d: PHY offline\n",
281 1.2 thorpej sc->sc_wdcdev.sc_dev.dv_xname, chp->channel);
282 1.2 thorpej break;
283 1.2 thorpej
284 1.2 thorpej case SStatus_DET_DEV:
285 1.2 thorpej /*
286 1.2 thorpej * XXX ATAPI detection doesn't currently work. Don't
287 1.2 thorpej * XXX know why. But, it's not like the standard method
288 1.2 thorpej * XXX can detect an ATAPI device connected via a SATA/PATA
289 1.2 thorpej * XXX bridge, so at least this is no worse. --thorpej
290 1.2 thorpej */
291 1.2 thorpej bus_space_write_1(chp->cmd_iot, chp->cmd_iohs[wd_sdh], 0,
292 1.2 thorpej WDSD_IBM | (0 << 4));
293 1.2 thorpej delay(10); /* 400ns delay */
294 1.2 thorpej /* Save register contents. */
295 1.2 thorpej scnt = bus_space_read_1(chp->cmd_iot,
296 1.2 thorpej chp->cmd_iohs[wd_seccnt], 0);
297 1.2 thorpej sn = bus_space_read_1(chp->cmd_iot,
298 1.2 thorpej chp->cmd_iohs[wd_sector], 0);
299 1.2 thorpej cl = bus_space_read_1(chp->cmd_iot,
300 1.2 thorpej chp->cmd_iohs[wd_cyl_lo], 0);
301 1.2 thorpej ch = bus_space_read_1(chp->cmd_iot,
302 1.2 thorpej chp->cmd_iohs[wd_cyl_hi], 0);
303 1.2 thorpej #if 0
304 1.2 thorpej printf("%s: port %d: scnt=0x%x sn=0x%x cl=0x%x ch=0x%x\n",
305 1.2 thorpej sc->sc_wdcdev.sc_dev.dv_xname, chp->channel,
306 1.2 thorpej scnt, sn, cl, ch);
307 1.2 thorpej #endif
308 1.2 thorpej /*
309 1.2 thorpej * scnt and sn are supposed to be 0x1 for ATAPI, but in some
310 1.2 thorpej * cases we get wrong values here, so ignore it.
311 1.2 thorpej */
312 1.2 thorpej if (cl == 0x14 && ch == 0xeb)
313 1.2 thorpej chp->ch_drive[0].drive_flags |= DRIVE_ATAPI;
314 1.2 thorpej else
315 1.2 thorpej chp->ch_drive[0].drive_flags |= DRIVE_ATA;
316 1.2 thorpej
317 1.2 thorpej aprint_normal("%s: port %d: device present, speed: %s\n",
318 1.2 thorpej sc->sc_wdcdev.sc_dev.dv_xname, chp->channel,
319 1.2 thorpej sata_speed[(sstatus & SStatus_SPD_mask) >>
320 1.2 thorpej SStatus_SPD_shift]);
321 1.2 thorpej rv |= (1 << 0);
322 1.2 thorpej break;
323 1.2 thorpej
324 1.2 thorpej default:
325 1.2 thorpej aprint_error("%s: port %d: unknown SStatus: 0x%08x\n",
326 1.2 thorpej sc->sc_wdcdev.sc_dev.dv_xname, chp->channel, sstatus);
327 1.2 thorpej }
328 1.2 thorpej
329 1.2 thorpej return (rv);
330 1.1 thorpej }
331 1.1 thorpej
332 1.1 thorpej static void
333 1.1 thorpej sii3112_setup_channel(struct channel_softc *chp)
334 1.1 thorpej {
335 1.1 thorpej struct ata_drive_datas *drvp;
336 1.1 thorpej int drive;
337 1.1 thorpej u_int32_t idedma_ctl, dtm;
338 1.1 thorpej struct pciide_channel *cp = (struct pciide_channel*)chp;
339 1.1 thorpej struct pciide_softc *sc = (struct pciide_softc*)cp->wdc_channel.wdc;
340 1.1 thorpej
341 1.1 thorpej /* setup DMA if needed */
342 1.1 thorpej pciide_channel_dma_setup(cp);
343 1.1 thorpej
344 1.1 thorpej idedma_ctl = 0;
345 1.1 thorpej dtm = 0;
346 1.1 thorpej
347 1.1 thorpej for (drive = 0; drive < 2; drive++) {
348 1.1 thorpej drvp = &chp->ch_drive[drive];
349 1.1 thorpej /* If no drive, skip */
350 1.1 thorpej if ((drvp->drive_flags & DRIVE) == 0)
351 1.1 thorpej continue;
352 1.1 thorpej if (drvp->drive_flags & DRIVE_UDMA) {
353 1.1 thorpej /* use Ultra/DMA */
354 1.1 thorpej drvp->drive_flags &= ~DRIVE_DMA;
355 1.1 thorpej idedma_ctl |= IDEDMA_CTL_DRV_DMA(drive);
356 1.1 thorpej dtm |= DTM_IDEx_DMA;
357 1.1 thorpej } else if (drvp->drive_flags & DRIVE_DMA) {
358 1.1 thorpej idedma_ctl |= IDEDMA_CTL_DRV_DMA(drive);
359 1.1 thorpej dtm |= DTM_IDEx_DMA;
360 1.1 thorpej } else {
361 1.1 thorpej dtm |= DTM_IDEx_PIO;
362 1.1 thorpej }
363 1.1 thorpej }
364 1.1 thorpej
365 1.1 thorpej /*
366 1.1 thorpej * Nothing to do to setup modes; it is meaningless in S-ATA
367 1.1 thorpej * (but many S-ATA drives still want to get the SET_FEATURE
368 1.1 thorpej * command).
369 1.1 thorpej */
370 1.1 thorpej if (idedma_ctl != 0) {
371 1.1 thorpej /* Add software bits in status register */
372 1.1 thorpej bus_space_write_1(sc->sc_dma_iot, cp->dma_iohs[IDEDMA_CTL], 0,
373 1.1 thorpej idedma_ctl);
374 1.1 thorpej }
375 1.1 thorpej pci_conf_write(sc->sc_pc, sc->sc_tag,
376 1.1 thorpej chp->channel == 0 ? SII3112_DTM_IDE0 : SII3112_DTM_IDE1, dtm);
377 1.1 thorpej }
378