pciide.c revision 1.1 1 /* $NetBSD: pciide.c,v 1.1 1998/03/04 06:35:11 cgd Exp $ */
2
3 /*
4 * Copyright (c) 1996, 1998 Christopher G. Demetriou. All rights reserved.
5 *
6 * Redistribution and use in source and binary forms, with or without
7 * modification, are permitted provided that the following conditions
8 * are met:
9 * 1. Redistributions of source code must retain the above copyright
10 * notice, this list of conditions and the following disclaimer.
11 * 2. Redistributions in binary form must reproduce the above copyright
12 * notice, this list of conditions and the following disclaimer in the
13 * documentation and/or other materials provided with the distribution.
14 * 3. All advertising materials mentioning features or use of this software
15 * must display the following acknowledgement:
16 * This product includes software developed by Christopher G. Demetriou
17 * for the NetBSD Project.
18 * 4. The name of the author may not be used to endorse or promote products
19 * derived from this software without specific prior written permission
20 *
21 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
22 * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
23 * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
24 * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
25 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
26 * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
27 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
28 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
29 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
30 * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
31 */
32
33 /*
34 * PCI IDE controller driver.
35 *
36 * Author: Christopher G. Demetriou, March 2, 1998 (derived from NetBSD
37 * sys/dev/pci/ppb.c, revision 1.16).
38 *
39 * See "PCI IDE Controller Specification, Revision 1.0 3/4/94" from the
40 * PCI SIG.
41 *
42 * XXX Does not yet support DMA.
43 *
44 * XXX Does not support serializing the two channels for broken (at least
45 * XXX according to linux and freebsd) controllers, e.g. CMD PCI0640.
46 */
47
48 #include <sys/param.h>
49 #include <sys/systm.h>
50 #include <sys/device.h>
51
52 #include <dev/pci/pcireg.h>
53 #include <dev/pci/pcivar.h>
54 #include <dev/pci/pciidereg.h>
55 #include <dev/pci/pciidevar.h>
56
57 struct pciide_softc {
58 struct device sc_dev;
59
60 void *sc_pci_ih; /* PCI interrupt handle */
61
62 struct pciide_channel { /* per-channel data */
63 /* internal bookkeeping */
64 struct device *dev; /* 'wdc' dev attached */
65 int compat; /* is it compat? */
66 void *ih; /* compat or pci handle */
67
68 /* used by wdc attachment (read-only after init) */
69 int cmd_ioh_valid, ctl_ioh_valid;
70 bus_space_tag_t cmd_iot, ctl_iot;
71 bus_space_handle_t cmd_ioh, ctl_ioh;
72
73 /* filled in by wdc attachment (written by wdc attach) */
74 int (*ihand) __P((void *));
75 void *ihandarg;
76 } channels[PCIIDE_NUM_CHANNELS];
77 };
78
79 #define PCIIDE_CHANNEL_NAME(chan) ((chan) == 0 ? "primary" : "secondary")
80
81 #ifdef __BROKEN_INDIRECT_CONFIG
82 int pciide_match __P((struct device *, void *, void *));
83 #else
84 int pciide_match __P((struct device *, struct cfdata *, void *));
85 #endif
86 void pciide_attach __P((struct device *, struct device *, void *));
87
88 struct cfattach pciide_ca = {
89 sizeof(struct pciide_softc), pciide_match, pciide_attach
90 };
91
92 int pciide_compat_intr __P((void *));
93 int pciide_pci_intr __P((void *));
94 int pciide_print __P((void *, const char *pnp));
95
96 int
97 pciide_match(parent, match, aux)
98 struct device *parent;
99 #ifdef __BROKEN_INDIRECT_CONFIG
100 void *match;
101 #else
102 struct cfdata *match;
103 #endif
104 void *aux;
105 {
106 struct pci_attach_args *pa = aux;
107
108 /*
109 * Check the ID register to see that it's a PCI IDE controller.
110 * If it is, we assume that we can deal with it; it _should_
111 * work in a standardized way...
112 */
113 if (PCI_CLASS(pa->pa_class) == PCI_CLASS_MASS_STORAGE &&
114 PCI_SUBCLASS(pa->pa_class) == PCI_SUBCLASS_MASS_STORAGE_IDE) {
115 return (1);
116 }
117
118 return (0);
119 }
120
121 void
122 pciide_attach(parent, self, aux)
123 struct device *parent, *self;
124 void *aux;
125 {
126 struct pci_attach_args *pa = aux;
127 pci_chipset_tag_t pc = pa->pa_pc;
128 struct pciide_softc *sc = (struct pciide_softc *)self;
129 struct pciide_attach_args aa;
130 struct pciide_channel *cp;
131 pcireg_t class, interface, csr;
132 pci_intr_handle_t intrhandle;
133 const char *intrstr;
134 char devinfo[256];
135 int i;
136
137 pci_devinfo(pa->pa_id, pa->pa_class, 0, devinfo);
138 printf(": %s (rev. 0x%02x)\n", devinfo, PCI_REVISION(pa->pa_class));
139
140 if ((pa->pa_flags & PCI_FLAGS_IO_ENABLED) == 0) {
141 csr = pci_conf_read(pc, pa->pa_tag, PCI_COMMAND_STATUS_REG);
142 printf("%s: device disabled (at %s)\n", sc->sc_dev.dv_xname,
143 (csr & PCI_COMMAND_IO_ENABLE) == 0 ? "device" : "bridge");
144 return;
145 }
146
147 class = pci_conf_read(pc, pa->pa_tag, PCI_CLASS_REG);
148 interface = PCI_INTERFACE(class);
149
150 /*
151 * Set up PCI interrupt.
152 *
153 * If mapping fails, that's (probably) because there's no pin
154 * set to intr, which is (probably) because it's a compat-only
155 * device (or hard-wired in compatibility-only mode). Native-PCI
156 * channels will complain later if the interrupt was needed.
157 *
158 * If establishment fails, that's (probably) some other problem.
159 */
160 if (pci_intr_map(pa->pa_pc, pa->pa_intrtag, pa->pa_intrpin,
161 pa->pa_intrline, &intrhandle) == 0) {
162 intrstr = pci_intr_string(pa->pa_pc, intrhandle);
163 sc->sc_pci_ih = pci_intr_establish(pa->pa_pc, intrhandle,
164 IPL_BIO, pciide_pci_intr, sc);
165
166 if (sc->sc_pci_ih != NULL) {
167 printf("%s: using %s for native-PCI interrupt\n",
168 sc->sc_dev.dv_xname,
169 intrstr ? intrstr : "unknown interrupt");
170 } else {
171 printf("%s: couldn't establish native-PCI interrupt",
172 sc->sc_dev.dv_xname);
173 if (intrstr != NULL)
174 printf(" at %s", intrstr);
175 printf("\n");
176 }
177 }
178
179 for (i = 0; i < PCIIDE_NUM_CHANNELS; i++) {
180 cp = &sc->channels[i];
181
182 printf("%s: %s channel %s to %s mode\n",
183 sc->sc_dev.dv_xname, PCIIDE_CHANNEL_NAME(i),
184 (interface & PCIIDE_INTERFACE_SETTABLE(i)) ?
185 "configured" : "wired",
186 (interface & PCIIDE_INTERFACE_PCI(i)) ? "native-PCI" :
187 "compatibility");
188
189 if (interface & PCIIDE_INTERFACE_PCI(i)) {
190 cp->compat = 0;
191 cp->ih = sc->sc_pci_ih;
192 cp->cmd_ioh_valid = (pci_mapreg_map(pa,
193 PCIIDE_REG_CMD_BASE(i), PCI_MAPREG_TYPE_IO, 0,
194 &cp->cmd_iot, &cp->cmd_ioh, NULL, NULL) == 0);
195 cp->ctl_ioh_valid = (pci_mapreg_map(pa,
196 PCIIDE_REG_CTL_BASE(i), PCI_MAPREG_TYPE_IO, 0,
197 &cp->ctl_iot, &cp->ctl_ioh, NULL, NULL) == 0);
198 } else {
199 cp->compat = 1;
200 cp->ih =
201 pciide_machdep_compat_intr_establish(&sc->sc_dev,
202 pa, i, pciide_compat_intr, cp);
203 cp->cmd_iot = pa->pa_iot;
204 cp->cmd_ioh_valid = (bus_space_map(cp->cmd_iot,
205 PCIIDE_COMPAT_CMD_BASE(i), PCIIDE_COMPAT_CMD_SIZE,
206 0, &cp->cmd_ioh) == 0);
207 cp->ctl_iot = pa->pa_iot;
208 cp->ctl_ioh_valid = (bus_space_map(cp->ctl_iot,
209 PCIIDE_COMPAT_CTL_BASE(i), PCIIDE_COMPAT_CTL_SIZE,
210 0, &cp->ctl_ioh) == 0);
211 }
212 }
213
214 for (i = 0; i < PCIIDE_NUM_CHANNELS; i++) {
215 cp = &sc->channels[i];
216
217 if (cp->cmd_ioh_valid && cp->ctl_ioh_valid && cp->ih != NULL) {
218 aa.channel = i;
219 aa.cmd_iot = cp->cmd_iot;
220 aa.cmd_ioh = cp->cmd_ioh;
221 aa.ctl_iot = cp->ctl_iot;
222 aa.ctl_ioh = cp->ctl_ioh;
223 aa.ihandp = &cp->ihand;
224 aa.ihandargp = &cp->ihandarg;
225 cp->dev = config_found(self, &aa, pciide_print);
226 /* XXX unmap compat intr if compat and not found? */
227 } else {
228 printf("%s: couldn't configure %s channel (cmd regs %s, ctl regs %s, (%s) intr %s)\n",
229 sc->sc_dev.dv_xname, PCIIDE_CHANNEL_NAME(i),
230 cp->cmd_ioh_valid ? "ok" : "unmapped",
231 cp->ctl_ioh_valid ? "ok" : "unmapped",
232 cp->compat ? "compat" : "native-PCI",
233 cp->ih != NULL ? "ok" : "broken");
234 }
235 }
236 }
237
238 int
239 pciide_print(aux, pnp)
240 void *aux;
241 const char *pnp;
242 {
243 struct pciide_attach_args *aa = aux;
244
245 /* only 'wdc's can attach to 'pciide's; easy. */
246 if (pnp)
247 printf("wdc at %s", pnp);
248 printf(" channel %d", aa->channel);
249 return (UNCONF);
250 }
251
252 int
253 pciide_compat_intr(arg)
254 void *arg;
255 {
256 struct pciide_channel *cp = arg;
257
258 #ifdef DIAGNOSTIC
259 /* should only be called for a compat channel */
260 if (cp->compat == 0)
261 panic("pciide compat intr called for non-compat chan %p\n", cp);
262 #endif
263 /* if there's no handler, that probably means no dev attached */
264 if (cp->ihand == NULL)
265 return (0);
266
267 return ((*cp->ihand)(cp->ihandarg));
268 }
269
270 int
271 pciide_pci_intr(arg)
272 void *arg;
273 {
274 struct pciide_softc *sc = arg;
275 struct pciide_channel *cp;
276 int i, rv, crv;
277
278 rv = 0;
279 for (i = 0; i < PCIIDE_NUM_CHANNELS; i++) {
280 cp = &sc->channels[i];
281
282 /* If a compat channel or there's no handler, skip. */
283 if (cp->compat || cp->ihand == NULL)
284 continue;
285
286 crv = ((*cp->ihand)(cp->ihandarg));
287 if (crv == 0)
288 ; /* leave rv alone */
289 else if (crv == 1)
290 rv = 1; /* claim the intr */
291 else if (rv == 0) /* crv should be -1 in this case */
292 rv = crv; /* if we've done no better, take it */
293 }
294 return (rv);
295 }
296