icside.c revision 1.2.2.4 1 1.2.2.4 jdolecek /* $NetBSD: icside.c,v 1.2.2.4 2002/10/10 18:30:28 jdolecek 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.4 jdolecek
45 1.2.2.4 jdolecek __KERNEL_RCSID(0, "$NetBSD: icside.c,v 1.2.2.4 2002/10/10 18:30:28 jdolecek Exp $");
46 1.2.2.4 jdolecek
47 1.2.2.2 thorpej #include <sys/systm.h>
48 1.2.2.2 thorpej #include <sys/conf.h>
49 1.2.2.2 thorpej #include <sys/device.h>
50 1.2.2.2 thorpej #include <sys/malloc.h>
51 1.2.2.2 thorpej
52 1.2.2.2 thorpej #include <machine/intr.h>
53 1.2.2.2 thorpej #include <machine/io.h>
54 1.2.2.2 thorpej #include <machine/bus.h>
55 1.2.2.2 thorpej #include <acorn32/podulebus/podulebus.h>
56 1.2.2.2 thorpej #include <acorn32/podulebus/icsidereg.h>
57 1.2.2.2 thorpej
58 1.2.2.2 thorpej #include <dev/ata/atavar.h>
59 1.2.2.2 thorpej #include <dev/ic/wdcvar.h>
60 1.2.2.2 thorpej #include <dev/podulebus/podules.h>
61 1.2.2.2 thorpej
62 1.2.2.2 thorpej /*
63 1.2.2.2 thorpej * ICS IDE podule device.
64 1.2.2.2 thorpej *
65 1.2.2.2 thorpej * This probes and attaches the top level ICS IDE device to the podulebus.
66 1.2.2.2 thorpej * It then configures any children of the ICS IDE device.
67 1.2.2.2 thorpej * The child is expected to be a wdc device using icside attachments.
68 1.2.2.2 thorpej */
69 1.2.2.2 thorpej
70 1.2.2.2 thorpej /*
71 1.2.2.2 thorpej * ICS IDE card softc structure.
72 1.2.2.2 thorpej *
73 1.2.2.2 thorpej * Contains the device node and podule information.
74 1.2.2.2 thorpej */
75 1.2.2.2 thorpej
76 1.2.2.2 thorpej struct icside_softc {
77 1.2.2.2 thorpej struct wdc_softc sc_wdcdev; /* common wdc definitions */
78 1.2.2.2 thorpej podule_t *sc_podule; /* Our podule */
79 1.2.2.2 thorpej int sc_podule_number; /* Our podule number */
80 1.2.2.2 thorpej struct bus_space sc_tag; /* custom tag */
81 1.2.2.2 thorpej struct podule_attach_args *sc_pa; /* podule info */
82 1.2.2.4 jdolecek bus_space_tag_t sc_latchiot; /* EEPROM page latch etc */
83 1.2.2.4 jdolecek bus_space_handle_t sc_latchioh;
84 1.2.2.4 jdolecek void *sc_shutdownhook;
85 1.2.2.4 jdolecek struct channel_softc *sc_chp[ICSIDE_MAX_CHANNELS];
86 1.2.2.2 thorpej struct icside_channel {
87 1.2.2.2 thorpej struct channel_softc wdc_channel; /* generic part */
88 1.2.2.2 thorpej void *ic_ih; /* interrupt handler */
89 1.2.2.2 thorpej struct evcnt ic_intrcnt; /* interrupt count */
90 1.2.2.2 thorpej u_int ic_irqaddr; /* interrupt flag */
91 1.2.2.2 thorpej u_int ic_irqmask; /* location */
92 1.2.2.2 thorpej bus_space_tag_t ic_irqiot; /* Bus space tag */
93 1.2.2.2 thorpej bus_space_handle_t ic_irqioh; /* handle for IRQ */
94 1.2.2.4 jdolecek } sc_chan[ICSIDE_MAX_CHANNELS];
95 1.2.2.2 thorpej };
96 1.2.2.2 thorpej
97 1.2.2.4 jdolecek int icside_probe(struct device *, struct cfdata *, void *);
98 1.2.2.4 jdolecek void icside_attach(struct device *, struct device *, void *);
99 1.2.2.4 jdolecek int icside_intr(void *);
100 1.2.2.4 jdolecek void icside_v6_shutdown(void *);
101 1.2.2.2 thorpej
102 1.2.2.4 jdolecek CFATTACH_DECL(icside, sizeof(struct icside_softc),
103 1.2.2.4 jdolecek icside_probe, icside_attach, NULL, NULL);
104 1.2.2.2 thorpej
105 1.2.2.2 thorpej /*
106 1.2.2.2 thorpej * Define prototypes for custom bus space functions.
107 1.2.2.2 thorpej */
108 1.2.2.2 thorpej
109 1.2.2.2 thorpej bs_rm_2_proto(icside);
110 1.2.2.2 thorpej bs_wm_2_proto(icside);
111 1.2.2.2 thorpej
112 1.2.2.2 thorpej #define MAX_CHANNELS 2
113 1.2.2.2 thorpej
114 1.2.2.2 thorpej /*
115 1.2.2.2 thorpej * Define a structure for describing the different card versions
116 1.2.2.2 thorpej */
117 1.2.2.2 thorpej struct ide_version {
118 1.2.2.2 thorpej int id; /* IDE card ID */
119 1.2.2.2 thorpej int modspace; /* Type of podule space */
120 1.2.2.2 thorpej int channels; /* Number of channels */
121 1.2.2.2 thorpej const char *name; /* name */
122 1.2.2.4 jdolecek int latchreg; /* EEPROM latch register */
123 1.2.2.2 thorpej int ideregs[MAX_CHANNELS]; /* IDE registers */
124 1.2.2.2 thorpej int auxregs[MAX_CHANNELS]; /* AUXSTAT register */
125 1.2.2.2 thorpej int irqregs[MAX_CHANNELS]; /* IRQ register */
126 1.2.2.2 thorpej int irqstatregs[MAX_CHANNELS];
127 1.2.2.4 jdolecek } const ide_versions[] = {
128 1.2.2.2 thorpej /* A3IN - Unsupported */
129 1.2.2.2 thorpej /* { 0, 0, 0, NULL, { 0, 0 }, { 0, 0 }, { 0, 0 }, { 0, 0 } },*/
130 1.2.2.2 thorpej /* A3USER - Unsupported */
131 1.2.2.2 thorpej /* { 1, 0, 0, NULL, { 0, 0 }, { 0, 0 }, { 0, 0 }, { 0, 0 } },*/
132 1.2.2.2 thorpej /* ARCIN V6 - Supported */
133 1.2.2.4 jdolecek { 3, 0, 2, "ARCIN V6", V6_ADDRLATCH,
134 1.2.2.2 thorpej { V6_P_IDE_BASE, V6_S_IDE_BASE },
135 1.2.2.2 thorpej { V6_P_AUX_BASE, V6_S_AUX_BASE },
136 1.2.2.2 thorpej { V6_P_IRQ_BASE, V6_S_IRQ_BASE },
137 1.2.2.2 thorpej { V6_P_IRQSTAT_BASE, V6_S_IRQSTAT_BASE }
138 1.2.2.2 thorpej },
139 1.2.2.2 thorpej /* ARCIN V5 - Supported (ID reg not supported so reads as 15) */
140 1.2.2.4 jdolecek { 15, 1, 1, "ARCIN V5", -1,
141 1.2.2.4 jdolecek { V5_IDE_BASE, -1 },
142 1.2.2.4 jdolecek { V5_AUX_BASE, -1 },
143 1.2.2.4 jdolecek { V5_IRQ_BASE, -1 },
144 1.2.2.4 jdolecek { V5_IRQSTAT_BASE, -1 }
145 1.2.2.2 thorpej }
146 1.2.2.2 thorpej };
147 1.2.2.2 thorpej
148 1.2.2.2 thorpej /*
149 1.2.2.2 thorpej * Card probe function
150 1.2.2.2 thorpej *
151 1.2.2.2 thorpej * Just match the manufacturer and podule ID's
152 1.2.2.2 thorpej */
153 1.2.2.2 thorpej
154 1.2.2.2 thorpej int
155 1.2.2.4 jdolecek icside_probe(struct device *parent, struct cfdata *cf, void *aux)
156 1.2.2.2 thorpej {
157 1.2.2.2 thorpej struct podule_attach_args *pa = (void *)aux;
158 1.2.2.4 jdolecek
159 1.2.2.3 jdolecek return (pa->pa_product == PODULE_ICS_IDE);
160 1.2.2.2 thorpej }
161 1.2.2.2 thorpej
162 1.2.2.2 thorpej /*
163 1.2.2.2 thorpej * Card attach function
164 1.2.2.2 thorpej *
165 1.2.2.2 thorpej * Identify the card version and configure any children.
166 1.2.2.2 thorpej */
167 1.2.2.2 thorpej
168 1.2.2.2 thorpej void
169 1.2.2.4 jdolecek icside_attach(struct device *parent, struct device *self, void *aux)
170 1.2.2.2 thorpej {
171 1.2.2.2 thorpej struct icside_softc *sc = (void *)self;
172 1.2.2.2 thorpej struct podule_attach_args *pa = (void *)aux;
173 1.2.2.2 thorpej bus_space_tag_t iot;
174 1.2.2.2 thorpej bus_space_handle_t ioh;
175 1.2.2.4 jdolecek const struct ide_version *ide = NULL;
176 1.2.2.2 thorpej u_int iobase;
177 1.2.2.2 thorpej int channel;
178 1.2.2.2 thorpej struct icside_channel *icp;
179 1.2.2.2 thorpej struct channel_softc *cp;
180 1.2.2.2 thorpej int loop;
181 1.2.2.2 thorpej int id;
182 1.2.2.2 thorpej
183 1.2.2.2 thorpej /* Note the podule number and validate */
184 1.2.2.2 thorpej
185 1.2.2.2 thorpej if (pa->pa_podule_number == -1)
186 1.2.2.2 thorpej panic("Podule has disappeared !");
187 1.2.2.2 thorpej
188 1.2.2.2 thorpej sc->sc_podule_number = pa->pa_podule_number;
189 1.2.2.2 thorpej sc->sc_podule = pa->pa_podule;
190 1.2.2.2 thorpej podules[sc->sc_podule_number].attached = 1;
191 1.2.2.2 thorpej
192 1.2.2.2 thorpej /* The ID register if present is always in FAST podule space */
193 1.2.2.2 thorpej iot = pa->pa_iot;
194 1.2.2.2 thorpej if (bus_space_map(iot, pa->pa_podule->fast_base +
195 1.2.2.2 thorpej ID_REGISTER_OFFSET, ID_REGISTER_SPACE, 0, &ioh)) {
196 1.2.2.2 thorpej printf("%s: cannot map ID register\n", self->dv_xname);
197 1.2.2.2 thorpej return;
198 1.2.2.2 thorpej }
199 1.2.2.2 thorpej
200 1.2.2.2 thorpej for (id = 0, loop = 0; loop < 4; ++loop)
201 1.2.2.2 thorpej id |= (bus_space_read_1(iot, ioh, loop) & 1) << loop;
202 1.2.2.2 thorpej
203 1.2.2.2 thorpej /* Do we recognise the ID ? */
204 1.2.2.2 thorpej for (loop = 0; loop < sizeof(ide_versions) / sizeof(struct ide_version);
205 1.2.2.2 thorpej ++loop) {
206 1.2.2.2 thorpej if (ide_versions[loop].id == id) {
207 1.2.2.2 thorpej ide = &ide_versions[loop];
208 1.2.2.2 thorpej break;
209 1.2.2.2 thorpej }
210 1.2.2.2 thorpej }
211 1.2.2.2 thorpej
212 1.2.2.2 thorpej /* Report the version and name */
213 1.2.2.2 thorpej if (ide == NULL || ide->name == NULL) {
214 1.2.2.2 thorpej printf(": rev %d is unsupported\n", id);
215 1.2.2.2 thorpej return;
216 1.2.2.2 thorpej } else
217 1.2.2.2 thorpej printf(": %s\n", ide->name);
218 1.2.2.2 thorpej
219 1.2.2.4 jdolecek if (ide->latchreg != -1) {
220 1.2.2.4 jdolecek sc->sc_latchiot = pa->pa_iot;
221 1.2.2.4 jdolecek if (bus_space_map(iot, pa->pa_podule->fast_base +
222 1.2.2.4 jdolecek ide->latchreg, 1, 0, &sc->sc_latchioh)) {
223 1.2.2.4 jdolecek printf("%s: cannot map latch register\n",
224 1.2.2.4 jdolecek self->dv_xname);
225 1.2.2.4 jdolecek return;
226 1.2.2.4 jdolecek }
227 1.2.2.4 jdolecek sc->sc_shutdownhook =
228 1.2.2.4 jdolecek shutdownhook_establish(icside_v6_shutdown, sc);
229 1.2.2.4 jdolecek }
230 1.2.2.4 jdolecek
231 1.2.2.2 thorpej /*
232 1.2.2.2 thorpej * Ok we need our own bus tag as the register spacing
233 1.2.2.2 thorpej * is not the default.
234 1.2.2.2 thorpej *
235 1.2.2.2 thorpej * For the podulebus the bus tag cookie is the shift
236 1.2.2.2 thorpej * to apply to registers
237 1.2.2.2 thorpej * So duplicate the bus space tag and change the
238 1.2.2.2 thorpej * cookie.
239 1.2.2.2 thorpej *
240 1.2.2.2 thorpej * Also while we are at it replace the default
241 1.2.2.2 thorpej * read/write mulitple short functions with
242 1.2.2.2 thorpej * optimised versions
243 1.2.2.2 thorpej */
244 1.2.2.2 thorpej
245 1.2.2.2 thorpej sc->sc_tag = *pa->pa_iot;
246 1.2.2.2 thorpej sc->sc_tag.bs_cookie = (void *) REGISTER_SPACING_SHIFT;
247 1.2.2.2 thorpej sc->sc_tag.bs_rm_2 = icside_bs_rm_2;
248 1.2.2.2 thorpej sc->sc_tag.bs_wm_2 = icside_bs_wm_2;
249 1.2.2.2 thorpej
250 1.2.2.2 thorpej /* Initialize wdc struct */
251 1.2.2.4 jdolecek sc->sc_wdcdev.channels = sc->sc_chp;
252 1.2.2.2 thorpej sc->sc_wdcdev.nchannels = ide->channels;
253 1.2.2.2 thorpej sc->sc_wdcdev.cap |= WDC_CAPABILITY_DATA16;
254 1.2.2.2 thorpej sc->sc_wdcdev.PIO_cap = 0;
255 1.2.2.2 thorpej sc->sc_pa = pa;
256 1.2.2.2 thorpej
257 1.2.2.2 thorpej for (channel = 0; channel < ide->channels; ++channel) {
258 1.2.2.4 jdolecek icp = &sc->sc_chan[channel];
259 1.2.2.2 thorpej sc->sc_wdcdev.channels[channel] = &icp->wdc_channel;
260 1.2.2.2 thorpej cp = &icp->wdc_channel;
261 1.2.2.2 thorpej
262 1.2.2.2 thorpej cp->channel = channel;
263 1.2.2.2 thorpej cp->wdc = &sc->sc_wdcdev;
264 1.2.2.2 thorpej cp->ch_queue = malloc(sizeof(struct channel_queue), M_DEVBUF,
265 1.2.2.2 thorpej M_NOWAIT);
266 1.2.2.2 thorpej if (cp->ch_queue == NULL) {
267 1.2.2.4 jdolecek printf("%s:%d: "
268 1.2.2.2 thorpej "can't allocate memory for command queue",
269 1.2.2.4 jdolecek sc->sc_wdcdev.sc_dev.dv_xname, channel);
270 1.2.2.2 thorpej continue;
271 1.2.2.2 thorpej }
272 1.2.2.2 thorpej cp->cmd_iot = &sc->sc_tag;
273 1.2.2.2 thorpej cp->ctl_iot = &sc->sc_tag;
274 1.2.2.2 thorpej if (ide->modspace)
275 1.2.2.2 thorpej iobase = pa->pa_podule->mod_base;
276 1.2.2.2 thorpej else
277 1.2.2.2 thorpej iobase = pa->pa_podule->fast_base;
278 1.2.2.2 thorpej
279 1.2.2.2 thorpej if (bus_space_map(iot, iobase + ide->ideregs[channel],
280 1.2.2.2 thorpej IDE_REGISTER_SPACE, 0, &cp->cmd_ioh))
281 1.2.2.2 thorpej return;
282 1.2.2.2 thorpej if (bus_space_map(iot, iobase + ide->auxregs[channel],
283 1.2.2.2 thorpej AUX_REGISTER_SPACE, 0, &cp->ctl_ioh))
284 1.2.2.2 thorpej return;
285 1.2.2.2 thorpej icp->ic_irqiot = iot;
286 1.2.2.2 thorpej if (bus_space_map(iot, iobase + ide->irqregs[channel],
287 1.2.2.2 thorpej IRQ_REGISTER_SPACE, 0, &icp->ic_irqioh))
288 1.2.2.2 thorpej return;
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 /* Call common attach routines */
292 1.2.2.2 thorpej wdcattach(cp);
293 1.2.2.2 thorpej /* Disable interrupts */
294 1.2.2.2 thorpej (void)bus_space_read_1(iot, icp->ic_irqioh, 0);
295 1.2.2.2 thorpej pa->pa_podule->irq_addr = iobase + ide->irqstatregs[channel];
296 1.2.2.2 thorpej pa->pa_podule->irq_mask = IRQ_STATUS_REGISTER_MASK;
297 1.2.2.2 thorpej icp->ic_irqaddr = pa->pa_podule->irq_addr;
298 1.2.2.2 thorpej icp->ic_irqmask = pa->pa_podule->irq_mask;
299 1.2.2.2 thorpej evcnt_attach_dynamic(&icp->ic_intrcnt, EVCNT_TYPE_INTR, NULL,
300 1.2.2.2 thorpej self->dv_xname, "intr");
301 1.2.2.2 thorpej icp->ic_ih = podulebus_irq_establish(pa->pa_ih, IPL_BIO,
302 1.2.2.2 thorpej icside_intr, icp, &icp->ic_intrcnt);
303 1.2.2.2 thorpej if (icp->ic_ih == NULL) {
304 1.2.2.2 thorpej printf("%s: Cannot claim interrupt %d\n",
305 1.2.2.2 thorpej sc->sc_wdcdev.sc_dev.dv_xname,
306 1.2.2.2 thorpej pa->pa_podule->interrupt);
307 1.2.2.2 thorpej continue;
308 1.2.2.2 thorpej }
309 1.2.2.2 thorpej /* Enable interrupts */
310 1.2.2.2 thorpej bus_space_write_1(iot, icp->ic_irqioh, 0, 0);
311 1.2.2.2 thorpej }
312 1.2.2.2 thorpej }
313 1.2.2.2 thorpej
314 1.2.2.2 thorpej /*
315 1.2.2.4 jdolecek * Shutdown handler -- try to restore the card to a state where
316 1.2.2.4 jdolecek * RISC OS will see it.
317 1.2.2.4 jdolecek */
318 1.2.2.4 jdolecek void
319 1.2.2.4 jdolecek icside_v6_shutdown(void *arg)
320 1.2.2.4 jdolecek {
321 1.2.2.4 jdolecek struct icside_softc *sc = arg;
322 1.2.2.4 jdolecek
323 1.2.2.4 jdolecek bus_space_write_1(sc->sc_latchiot, sc->sc_latchioh, 0, 0);
324 1.2.2.4 jdolecek }
325 1.2.2.4 jdolecek
326 1.2.2.4 jdolecek /*
327 1.2.2.2 thorpej * Podule interrupt handler
328 1.2.2.2 thorpej *
329 1.2.2.2 thorpej * If the interrupt was from our card pass it on to the wdc interrupt handler
330 1.2.2.2 thorpej */
331 1.2.2.2 thorpej int
332 1.2.2.4 jdolecek icside_intr(void *arg)
333 1.2.2.2 thorpej {
334 1.2.2.2 thorpej struct icside_channel *icp = arg;
335 1.2.2.2 thorpej volatile u_char *intraddr = (volatile u_char *)icp->ic_irqaddr;
336 1.2.2.2 thorpej
337 1.2.2.2 thorpej /* XXX - not bus space yet - should really be handled by podulebus */
338 1.2.2.2 thorpej if ((*intraddr) & icp->ic_irqmask)
339 1.2.2.2 thorpej wdcintr(&icp->wdc_channel);
340 1.2.2.2 thorpej return(0);
341 1.2.2.2 thorpej }
342