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