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