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