icside.c revision 1.2.2.2 1 1.2.2.2 thorpej /* $NetBSD: icside.c,v 1.2.2.2 2002/01/10 19:36:33 thorpej Exp $ */
2 1.2.2.2 thorpej
3 1.2.2.2 thorpej /*
4 1.2.2.2 thorpej * Copyright (c) 1997-1998 Mark Brinicombe
5 1.2.2.2 thorpej * Copyright (c) 1997-1998 Causality Limited
6 1.2.2.2 thorpej *
7 1.2.2.2 thorpej * Redistribution and use in source and binary forms, with or without
8 1.2.2.2 thorpej * modification, are permitted provided that the following conditions
9 1.2.2.2 thorpej * are met:
10 1.2.2.2 thorpej * 1. Redistributions of source code must retain the above copyright
11 1.2.2.2 thorpej * notice, this list of conditions and the following disclaimer.
12 1.2.2.2 thorpej * 2. Redistributions in binary form must reproduce the above copyright
13 1.2.2.2 thorpej * notice, this list of conditions and the following disclaimer in the
14 1.2.2.2 thorpej * documentation and/or other materials provided with the distribution.
15 1.2.2.2 thorpej * 3. All advertising materials mentioning features or use of this software
16 1.2.2.2 thorpej * must display the following acknowledgement:
17 1.2.2.2 thorpej * This product includes software developed by Mark Brinicombe
18 1.2.2.2 thorpej * for the NetBSD Project.
19 1.2.2.2 thorpej * 4. The name of the author may not be used to endorse or promote products
20 1.2.2.2 thorpej * derived from this software without specific prior written permission.
21 1.2.2.2 thorpej *
22 1.2.2.2 thorpej * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
23 1.2.2.2 thorpej * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
24 1.2.2.2 thorpej * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
25 1.2.2.2 thorpej * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
26 1.2.2.2 thorpej * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
27 1.2.2.2 thorpej * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
28 1.2.2.2 thorpej * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
29 1.2.2.2 thorpej * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
30 1.2.2.2 thorpej * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
31 1.2.2.2 thorpej * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
32 1.2.2.2 thorpej *
33 1.2.2.2 thorpej * Probe and attach functions to use generic IDE driver for the ICS IDE podule
34 1.2.2.2 thorpej */
35 1.2.2.2 thorpej
36 1.2.2.2 thorpej /*
37 1.2.2.2 thorpej * Thanks to David Baildon for loaning an IDE card for the development
38 1.2.2.2 thorpej * of this driver.
39 1.2.2.2 thorpej * Thanks to Ian Copestake and David Baildon for providing register mapping
40 1.2.2.2 thorpej * information
41 1.2.2.2 thorpej */
42 1.2.2.2 thorpej
43 1.2.2.2 thorpej #include <sys/param.h>
44 1.2.2.2 thorpej #include <sys/systm.h>
45 1.2.2.2 thorpej #include <sys/conf.h>
46 1.2.2.2 thorpej #include <sys/device.h>
47 1.2.2.2 thorpej #include <sys/malloc.h>
48 1.2.2.2 thorpej
49 1.2.2.2 thorpej #include <machine/intr.h>
50 1.2.2.2 thorpej #include <machine/io.h>
51 1.2.2.2 thorpej #include <machine/bus.h>
52 1.2.2.2 thorpej #include <acorn32/podulebus/podulebus.h>
53 1.2.2.2 thorpej #include <acorn32/podulebus/icsidereg.h>
54 1.2.2.2 thorpej
55 1.2.2.2 thorpej #include <dev/ata/atavar.h>
56 1.2.2.2 thorpej #include <dev/ic/wdcvar.h>
57 1.2.2.2 thorpej #include <dev/podulebus/podules.h>
58 1.2.2.2 thorpej
59 1.2.2.2 thorpej /*
60 1.2.2.2 thorpej * ICS IDE podule device.
61 1.2.2.2 thorpej *
62 1.2.2.2 thorpej * This probes and attaches the top level ICS IDE device to the podulebus.
63 1.2.2.2 thorpej * It then configures any children of the ICS IDE device.
64 1.2.2.2 thorpej * The child is expected to be a wdc device using icside attachments.
65 1.2.2.2 thorpej */
66 1.2.2.2 thorpej
67 1.2.2.2 thorpej /*
68 1.2.2.2 thorpej * ICS IDE card softc structure.
69 1.2.2.2 thorpej *
70 1.2.2.2 thorpej * Contains the device node and podule information.
71 1.2.2.2 thorpej */
72 1.2.2.2 thorpej
73 1.2.2.2 thorpej struct icside_softc {
74 1.2.2.2 thorpej struct wdc_softc sc_wdcdev; /* common wdc definitions */
75 1.2.2.2 thorpej podule_t *sc_podule; /* Our podule */
76 1.2.2.2 thorpej int sc_podule_number; /* Our podule number */
77 1.2.2.2 thorpej struct bus_space sc_tag; /* custom tag */
78 1.2.2.2 thorpej struct podule_attach_args *sc_pa; /* podule info */
79 1.2.2.2 thorpej struct icside_channel {
80 1.2.2.2 thorpej struct channel_softc wdc_channel; /* generic part */
81 1.2.2.2 thorpej void *ic_ih; /* interrupt handler */
82 1.2.2.2 thorpej struct evcnt ic_intrcnt; /* interrupt count */
83 1.2.2.2 thorpej u_int ic_irqaddr; /* interrupt flag */
84 1.2.2.2 thorpej u_int ic_irqmask; /* location */
85 1.2.2.2 thorpej bus_space_tag_t ic_irqiot; /* Bus space tag */
86 1.2.2.2 thorpej bus_space_handle_t ic_irqioh; /* handle for IRQ */
87 1.2.2.2 thorpej } *icside_channels;
88 1.2.2.2 thorpej };
89 1.2.2.2 thorpej
90 1.2.2.2 thorpej int icside_probe __P((struct device *, struct cfdata *, void *));
91 1.2.2.2 thorpej void icside_attach __P((struct device *, struct device *, void *));
92 1.2.2.2 thorpej int icside_intr __P((void *));
93 1.2.2.2 thorpej
94 1.2.2.2 thorpej struct cfattach icside_ca = {
95 1.2.2.2 thorpej sizeof(struct icside_softc), icside_probe, icside_attach
96 1.2.2.2 thorpej };
97 1.2.2.2 thorpej
98 1.2.2.2 thorpej /*
99 1.2.2.2 thorpej * Define prototypes for custom bus space functions.
100 1.2.2.2 thorpej */
101 1.2.2.2 thorpej
102 1.2.2.2 thorpej bs_rm_2_proto(icside);
103 1.2.2.2 thorpej bs_wm_2_proto(icside);
104 1.2.2.2 thorpej
105 1.2.2.2 thorpej #define MAX_CHANNELS 2
106 1.2.2.2 thorpej
107 1.2.2.2 thorpej /*
108 1.2.2.2 thorpej * Define a structure for describing the different card versions
109 1.2.2.2 thorpej */
110 1.2.2.2 thorpej struct ide_version {
111 1.2.2.2 thorpej int id; /* IDE card ID */
112 1.2.2.2 thorpej int modspace; /* Type of podule space */
113 1.2.2.2 thorpej int channels; /* Number of channels */
114 1.2.2.2 thorpej const char *name; /* name */
115 1.2.2.2 thorpej int ideregs[MAX_CHANNELS]; /* IDE registers */
116 1.2.2.2 thorpej int auxregs[MAX_CHANNELS]; /* AUXSTAT register */
117 1.2.2.2 thorpej int irqregs[MAX_CHANNELS]; /* IRQ register */
118 1.2.2.2 thorpej int irqstatregs[MAX_CHANNELS];
119 1.2.2.2 thorpej } ide_versions[] = {
120 1.2.2.2 thorpej /* A3IN - Unsupported */
121 1.2.2.2 thorpej /* { 0, 0, 0, NULL, { 0, 0 }, { 0, 0 }, { 0, 0 }, { 0, 0 } },*/
122 1.2.2.2 thorpej /* A3USER - Unsupported */
123 1.2.2.2 thorpej /* { 1, 0, 0, NULL, { 0, 0 }, { 0, 0 }, { 0, 0 }, { 0, 0 } },*/
124 1.2.2.2 thorpej /* ARCIN V6 - Supported */
125 1.2.2.2 thorpej { 3, 0, 2, "ARCIN V6",
126 1.2.2.2 thorpej { V6_P_IDE_BASE, V6_S_IDE_BASE },
127 1.2.2.2 thorpej { V6_P_AUX_BASE, V6_S_AUX_BASE },
128 1.2.2.2 thorpej { V6_P_IRQ_BASE, V6_S_IRQ_BASE },
129 1.2.2.2 thorpej { V6_P_IRQSTAT_BASE, V6_S_IRQSTAT_BASE }
130 1.2.2.2 thorpej },
131 1.2.2.2 thorpej /* ARCIN V5 - Supported (ID reg not supported so reads as 15) */
132 1.2.2.2 thorpej { 15, 1, 1, "ARCIN V5",
133 1.2.2.2 thorpej { V5_IDE_BASE, 0 },
134 1.2.2.2 thorpej { V5_AUX_BASE, 0 },
135 1.2.2.2 thorpej { V5_IRQ_BASE, 0 },
136 1.2.2.2 thorpej { V5_IRQSTAT_BASE, 0 }
137 1.2.2.2 thorpej }
138 1.2.2.2 thorpej };
139 1.2.2.2 thorpej
140 1.2.2.2 thorpej /*
141 1.2.2.2 thorpej * Card probe function
142 1.2.2.2 thorpej *
143 1.2.2.2 thorpej * Just match the manufacturer and podule ID's
144 1.2.2.2 thorpej */
145 1.2.2.2 thorpej
146 1.2.2.2 thorpej int
147 1.2.2.2 thorpej icside_probe(parent, cf, aux)
148 1.2.2.2 thorpej struct device *parent;
149 1.2.2.2 thorpej struct cfdata *cf;
150 1.2.2.2 thorpej void *aux;
151 1.2.2.2 thorpej {
152 1.2.2.2 thorpej struct podule_attach_args *pa = (void *)aux;
153 1.2.2.2 thorpej if (matchpodule(pa, MANUFACTURER_ICS, PODULE_ICS_IDE, -1) == 0)
154 1.2.2.2 thorpej return(0);
155 1.2.2.2 thorpej
156 1.2.2.2 thorpej return(1);
157 1.2.2.2 thorpej }
158 1.2.2.2 thorpej
159 1.2.2.2 thorpej /*
160 1.2.2.2 thorpej * Card attach function
161 1.2.2.2 thorpej *
162 1.2.2.2 thorpej * Identify the card version and configure any children.
163 1.2.2.2 thorpej */
164 1.2.2.2 thorpej
165 1.2.2.2 thorpej void
166 1.2.2.2 thorpej icside_attach(parent, self, aux)
167 1.2.2.2 thorpej struct device *parent, *self;
168 1.2.2.2 thorpej void *aux;
169 1.2.2.2 thorpej {
170 1.2.2.2 thorpej struct icside_softc *sc = (void *)self;
171 1.2.2.2 thorpej struct podule_attach_args *pa = (void *)aux;
172 1.2.2.2 thorpej bus_space_tag_t iot;
173 1.2.2.2 thorpej bus_space_handle_t ioh;
174 1.2.2.2 thorpej struct ide_version *ide = NULL;
175 1.2.2.2 thorpej u_int iobase;
176 1.2.2.2 thorpej int channel;
177 1.2.2.2 thorpej struct icside_channel *icp;
178 1.2.2.2 thorpej struct channel_softc *cp;
179 1.2.2.2 thorpej int loop;
180 1.2.2.2 thorpej int id;
181 1.2.2.2 thorpej
182 1.2.2.2 thorpej /* Note the podule number and validate */
183 1.2.2.2 thorpej
184 1.2.2.2 thorpej if (pa->pa_podule_number == -1)
185 1.2.2.2 thorpej panic("Podule has disappeared !");
186 1.2.2.2 thorpej
187 1.2.2.2 thorpej sc->sc_podule_number = pa->pa_podule_number;
188 1.2.2.2 thorpej sc->sc_podule = pa->pa_podule;
189 1.2.2.2 thorpej podules[sc->sc_podule_number].attached = 1;
190 1.2.2.2 thorpej
191 1.2.2.2 thorpej /* The ID register if present is always in FAST podule space */
192 1.2.2.2 thorpej iot = pa->pa_iot;
193 1.2.2.2 thorpej if (bus_space_map(iot, pa->pa_podule->fast_base +
194 1.2.2.2 thorpej ID_REGISTER_OFFSET, ID_REGISTER_SPACE, 0, &ioh)) {
195 1.2.2.2 thorpej printf("%s: cannot map ID register\n", self->dv_xname);
196 1.2.2.2 thorpej return;
197 1.2.2.2 thorpej }
198 1.2.2.2 thorpej
199 1.2.2.2 thorpej for (id = 0, loop = 0; loop < 4; ++loop)
200 1.2.2.2 thorpej id |= (bus_space_read_1(iot, ioh, loop) & 1) << loop;
201 1.2.2.2 thorpej
202 1.2.2.2 thorpej /* Do we recognise the ID ? */
203 1.2.2.2 thorpej for (loop = 0; loop < sizeof(ide_versions) / sizeof(struct ide_version);
204 1.2.2.2 thorpej ++loop) {
205 1.2.2.2 thorpej if (ide_versions[loop].id == id) {
206 1.2.2.2 thorpej ide = &ide_versions[loop];
207 1.2.2.2 thorpej break;
208 1.2.2.2 thorpej }
209 1.2.2.2 thorpej }
210 1.2.2.2 thorpej
211 1.2.2.2 thorpej /* Report the version and name */
212 1.2.2.2 thorpej if (ide == NULL || ide->name == NULL) {
213 1.2.2.2 thorpej printf(": rev %d is unsupported\n", id);
214 1.2.2.2 thorpej return;
215 1.2.2.2 thorpej } else
216 1.2.2.2 thorpej printf(": %s\n", ide->name);
217 1.2.2.2 thorpej
218 1.2.2.2 thorpej /*
219 1.2.2.2 thorpej * Ok we need our own bus tag as the register spacing
220 1.2.2.2 thorpej * is not the default.
221 1.2.2.2 thorpej *
222 1.2.2.2 thorpej * For the podulebus the bus tag cookie is the shift
223 1.2.2.2 thorpej * to apply to registers
224 1.2.2.2 thorpej * So duplicate the bus space tag and change the
225 1.2.2.2 thorpej * cookie.
226 1.2.2.2 thorpej *
227 1.2.2.2 thorpej * Also while we are at it replace the default
228 1.2.2.2 thorpej * read/write mulitple short functions with
229 1.2.2.2 thorpej * optimised versions
230 1.2.2.2 thorpej */
231 1.2.2.2 thorpej
232 1.2.2.2 thorpej sc->sc_tag = *pa->pa_iot;
233 1.2.2.2 thorpej sc->sc_tag.bs_cookie = (void *) REGISTER_SPACING_SHIFT;
234 1.2.2.2 thorpej sc->sc_tag.bs_rm_2 = icside_bs_rm_2;
235 1.2.2.2 thorpej sc->sc_tag.bs_wm_2 = icside_bs_wm_2;
236 1.2.2.2 thorpej
237 1.2.2.2 thorpej /* Initialize wdc struct */
238 1.2.2.2 thorpej sc->sc_wdcdev.channels = malloc(
239 1.2.2.2 thorpej sizeof(struct channel_softc *) * ide->channels, M_DEVBUF, M_NOWAIT);
240 1.2.2.2 thorpej sc->icside_channels = malloc(
241 1.2.2.2 thorpej sizeof(struct icside_channel) * ide->channels, M_DEVBUF, M_NOWAIT);
242 1.2.2.2 thorpej if (sc->sc_wdcdev.channels == NULL || sc->icside_channels == NULL) {
243 1.2.2.2 thorpej printf("%s: can't allocate channel infos\n",
244 1.2.2.2 thorpej sc->sc_wdcdev.sc_dev.dv_xname);
245 1.2.2.2 thorpej return;
246 1.2.2.2 thorpej }
247 1.2.2.2 thorpej sc->sc_wdcdev.nchannels = ide->channels;
248 1.2.2.2 thorpej sc->sc_wdcdev.cap |= WDC_CAPABILITY_DATA16;
249 1.2.2.2 thorpej sc->sc_wdcdev.PIO_cap = 0;
250 1.2.2.2 thorpej sc->sc_pa = pa;
251 1.2.2.2 thorpej
252 1.2.2.2 thorpej for (channel = 0; channel < ide->channels; ++channel) {
253 1.2.2.2 thorpej icp = &sc->icside_channels[channel];
254 1.2.2.2 thorpej sc->sc_wdcdev.channels[channel] = &icp->wdc_channel;
255 1.2.2.2 thorpej cp = &icp->wdc_channel;
256 1.2.2.2 thorpej
257 1.2.2.2 thorpej cp->channel = channel;
258 1.2.2.2 thorpej cp->wdc = &sc->sc_wdcdev;
259 1.2.2.2 thorpej cp->ch_queue = malloc(sizeof(struct channel_queue), M_DEVBUF,
260 1.2.2.2 thorpej M_NOWAIT);
261 1.2.2.2 thorpej if (cp->ch_queue == NULL) {
262 1.2.2.2 thorpej printf("%s %s channel: "
263 1.2.2.2 thorpej "can't allocate memory for command queue",
264 1.2.2.2 thorpej sc->sc_wdcdev.sc_dev.dv_xname,
265 1.2.2.2 thorpej (channel == 0) ? "primary" : "secondary");
266 1.2.2.2 thorpej continue;
267 1.2.2.2 thorpej }
268 1.2.2.2 thorpej cp->cmd_iot = &sc->sc_tag;
269 1.2.2.2 thorpej cp->ctl_iot = &sc->sc_tag;
270 1.2.2.2 thorpej if (ide->modspace)
271 1.2.2.2 thorpej iobase = pa->pa_podule->mod_base;
272 1.2.2.2 thorpej else
273 1.2.2.2 thorpej iobase = pa->pa_podule->fast_base;
274 1.2.2.2 thorpej
275 1.2.2.2 thorpej if (bus_space_map(iot, iobase + ide->ideregs[channel],
276 1.2.2.2 thorpej IDE_REGISTER_SPACE, 0, &cp->cmd_ioh))
277 1.2.2.2 thorpej return;
278 1.2.2.2 thorpej if (bus_space_map(iot, iobase + ide->auxregs[channel],
279 1.2.2.2 thorpej AUX_REGISTER_SPACE, 0, &cp->ctl_ioh))
280 1.2.2.2 thorpej return;
281 1.2.2.2 thorpej icp->ic_irqiot = iot;
282 1.2.2.2 thorpej if (bus_space_map(iot, iobase + ide->irqregs[channel],
283 1.2.2.2 thorpej IRQ_REGISTER_SPACE, 0, &icp->ic_irqioh))
284 1.2.2.2 thorpej return;
285 1.2.2.2 thorpej /* Disable interrupts */
286 1.2.2.2 thorpej (void)bus_space_read_1(iot, icp->ic_irqioh, 0);
287 1.2.2.2 thorpej /* Call common attach routines */
288 1.2.2.2 thorpej wdcattach(cp);
289 1.2.2.2 thorpej /* Disable interrupts */
290 1.2.2.2 thorpej (void)bus_space_read_1(iot, icp->ic_irqioh, 0);
291 1.2.2.2 thorpej pa->pa_podule->irq_addr = iobase + ide->irqstatregs[channel];
292 1.2.2.2 thorpej pa->pa_podule->irq_mask = IRQ_STATUS_REGISTER_MASK;
293 1.2.2.2 thorpej icp->ic_irqaddr = pa->pa_podule->irq_addr;
294 1.2.2.2 thorpej icp->ic_irqmask = pa->pa_podule->irq_mask;
295 1.2.2.2 thorpej evcnt_attach_dynamic(&icp->ic_intrcnt, EVCNT_TYPE_INTR, NULL,
296 1.2.2.2 thorpej self->dv_xname, "intr");
297 1.2.2.2 thorpej icp->ic_ih = podulebus_irq_establish(pa->pa_ih, IPL_BIO,
298 1.2.2.2 thorpej icside_intr, icp, &icp->ic_intrcnt);
299 1.2.2.2 thorpej if (icp->ic_ih == NULL) {
300 1.2.2.2 thorpej printf("%s: Cannot claim interrupt %d\n",
301 1.2.2.2 thorpej sc->sc_wdcdev.sc_dev.dv_xname,
302 1.2.2.2 thorpej pa->pa_podule->interrupt);
303 1.2.2.2 thorpej continue;
304 1.2.2.2 thorpej }
305 1.2.2.2 thorpej /* Enable interrupts */
306 1.2.2.2 thorpej bus_space_write_1(iot, icp->ic_irqioh, 0, 0);
307 1.2.2.2 thorpej }
308 1.2.2.2 thorpej }
309 1.2.2.2 thorpej
310 1.2.2.2 thorpej /*
311 1.2.2.2 thorpej * Podule interrupt handler
312 1.2.2.2 thorpej *
313 1.2.2.2 thorpej * If the interrupt was from our card pass it on to the wdc interrupt handler
314 1.2.2.2 thorpej */
315 1.2.2.2 thorpej int
316 1.2.2.2 thorpej icside_intr(arg)
317 1.2.2.2 thorpej void *arg;
318 1.2.2.2 thorpej {
319 1.2.2.2 thorpej struct icside_channel *icp = arg;
320 1.2.2.2 thorpej volatile u_char *intraddr = (volatile u_char *)icp->ic_irqaddr;
321 1.2.2.2 thorpej
322 1.2.2.2 thorpej /* XXX - not bus space yet - should really be handled by podulebus */
323 1.2.2.2 thorpej if ((*intraddr) & icp->ic_irqmask)
324 1.2.2.2 thorpej wdcintr(&icp->wdc_channel);
325 1.2.2.2 thorpej return(0);
326 1.2.2.2 thorpej }
327