1 /*        $NetBSD: wdc_pioc.c,v 1.31 2023/12/20 06:13:59 thorpej Exp $          */
2 
3 /*
4  * Copyright (c) 1997-1998 Mark Brinicombe.
5  * Copyright (c) 1997 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 company nor the name of the author may be used to
20  *    endorse or promote products derived from this software without specific
21  *    prior written permission.
22  *
23  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR IMPLIED
24  * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
25  * MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
26  * IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT,
27  * INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
28  * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
29  * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
30  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
31  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
32  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
33  * SUCH DAMAGE.
34  */
35 
36 #include <sys/cdefs.h>
37 __KERNEL_RCSID(0, "$NetBSD: wdc_pioc.c,v 1.31 2023/12/20 06:13:59 thorpej Exp $");
38 
39 #include <sys/param.h>
40 #include <sys/systm.h>
41 #include <sys/device.h>
42 #include <sys/bus.h>
43 
44 #include <machine/intr.h>
45 
46 #include <acorn32/mainbus/piocvar.h>
47 
48 #include <dev/ata/atavar.h>
49 #include <dev/ic/wdcreg.h>
50 #include <dev/ic/wdcvar.h>
51 
52 #include "locators.h"
53 
54 #define WDC_PIOC_REG_NPORTS   8
55 #define WDC_PIOC_AUXREG_OFFSET          (0x206 * 4)
56 #define WDC_PIOC_AUXREG_NPORTS          1
57 
58 struct wdc_pioc_softc {
59           struct    wdc_softc sc_wdcdev;
60           struct    ata_channel *sc_chanlist[1];
61           struct    ata_channel sc_channel;
62           struct    wdc_regs sc_wdc_regs;
63           void      *sc_ih;
64 };
65 
66 /* prototypes for functions */
67 static int  wdc_pioc_probe  (device_t, cfdata_t, void *);
68 static void wdc_pioc_attach (device_t, device_t, void *);
69 
70 /* device attach structure */
71 CFATTACH_DECL_NEW(wdc_pioc, sizeof(struct wdc_pioc_softc),
72     wdc_pioc_probe, wdc_pioc_attach, NULL, NULL);
73 
74 /*
75  * int wdc_pioc_probe(device_t parent, cfdata_t cf, void *aux)
76  *
77  * Make sure we are trying to attach a wdc device and then
78  * probe for one.
79  */
80 
81 static int
wdc_pioc_probe(device_t parent,cfdata_t cf,void * aux)82 wdc_pioc_probe(device_t parent, cfdata_t cf, void *aux)
83 {
84           struct pioc_attach_args *pa = aux;
85           struct wdc_regs wdr;
86           int res, i;
87           u_int iobase;
88 
89           if (pa->pa_name && strcmp(pa->pa_name, "wdc") != 0)
90                     return(0);
91 
92           /* We need an offset */
93           if (pa->pa_offset == PIOCCF_OFFSET_DEFAULT)
94                     return(0);
95 
96           iobase = pa->pa_iobase + pa->pa_offset;
97           wdr.cmd_iot = pa->pa_iot;
98           wdr.ctl_iot = pa->pa_iot;
99 
100           if (bus_space_map(wdr.cmd_iot, iobase, WDC_PIOC_REG_NPORTS, 0,
101               &wdr.cmd_baseioh))
102                     return(0);
103           for (i = 0; i < WDC_PIOC_REG_NPORTS; i++) {
104                     if (bus_space_subregion(wdr.cmd_iot, wdr.cmd_baseioh, i,
105                               i == 0 ? 4 : 1, &wdr.cmd_iohs[i]) != 0) {
106                               bus_space_unmap(wdr.cmd_iot, wdr.cmd_baseioh,
107                                   WDC_PIOC_REG_NPORTS);
108                               return 0;
109                     }
110           }
111           wdc_init_shadow_regs(&wdr);
112 
113           if (bus_space_map(wdr.ctl_iot, iobase + WDC_PIOC_AUXREG_OFFSET,
114               WDC_PIOC_AUXREG_NPORTS, 0, &wdr.ctl_ioh)) {
115                     bus_space_unmap(wdr.cmd_iot, wdr.cmd_baseioh,
116                         WDC_PIOC_REG_NPORTS);
117                     return(0);
118           }
119 
120           res = wdcprobe(&wdr);
121 
122           bus_space_unmap(wdr.ctl_iot, wdr.ctl_ioh, WDC_PIOC_AUXREG_NPORTS);
123           bus_space_unmap(wdr.cmd_iot, wdr.cmd_baseioh, WDC_PIOC_REG_NPORTS);
124 
125           if (res)
126                      pa->pa_iosize = WDC_PIOC_REG_NPORTS;
127           return(res);
128 }
129 
130 /*
131  * void wdc_pioc_attach(device_t parent, device_t self, void *aux)
132  *
133  * attach the wdc device
134  */
135 
136 static void
wdc_pioc_attach(device_t parent,device_t self,void * aux)137 wdc_pioc_attach(device_t parent, device_t self, void *aux)
138 {
139           struct wdc_pioc_softc *sc = device_private(self);
140           struct wdc_regs *wdr;
141           struct pioc_attach_args *pa = aux;
142           u_int iobase;
143           int i;
144 
145           aprint_normal("\n");
146 
147           sc->sc_wdcdev.sc_atac.atac_dev = self;
148           sc->sc_wdcdev.regs = wdr = &sc->sc_wdc_regs;
149           iobase = pa->pa_iobase + pa->pa_offset;
150           wdr->cmd_iot = pa->pa_iot;
151           wdr->ctl_iot = pa->pa_iot;
152           if (bus_space_map(wdr->cmd_iot, iobase,
153               WDC_PIOC_REG_NPORTS, 0, &wdr->cmd_baseioh))
154                     panic("%s: couldn't map drive registers", device_xname(self));
155           for (i = 0; i < WDC_PIOC_REG_NPORTS; i++) {
156                     if (bus_space_subregion(wdr->cmd_iot,
157                               wdr->cmd_baseioh, i,          i == 0 ? 4 : 1,
158                               &wdr->cmd_iohs[i]) != 0)
159                               panic("%s: couldn't submap drive registers",
160                                   device_xname(self));
161           }
162 
163           if (bus_space_map(wdr->ctl_iot,
164               iobase + WDC_PIOC_AUXREG_OFFSET, WDC_PIOC_AUXREG_NPORTS, 0,
165               &wdr->ctl_ioh))
166                     panic("%s: couldn't map aux registers", device_xname(self));
167 
168           sc->sc_ih = intr_claim(pa->pa_irq, IPL_BIO, "wdc",  wdcintr,
169                &sc->sc_channel);
170           if (!sc->sc_ih)
171                     panic("%s: Cannot claim IRQ %d", device_xname(self),
172                         pa->pa_irq);
173           sc->sc_wdcdev.sc_atac.atac_cap |= ATAC_CAP_DATA16;
174           sc->sc_wdcdev.sc_atac.atac_pio_cap = 0;
175           sc->sc_chanlist[0] = &sc->sc_channel;
176           sc->sc_wdcdev.sc_atac.atac_channels = sc->sc_chanlist;
177           sc->sc_channel.ch_atac = &sc->sc_wdcdev.sc_atac;
178           sc->sc_wdcdev.sc_atac.atac_nchannels = 1;
179           sc->sc_wdcdev.wdc_maxdrives = 2;
180           sc->sc_channel.ch_channel = 0;
181 
182           wdc_init_shadow_regs(wdr);
183 
184           wdcattach(&sc->sc_channel);
185 }
186 
187 /* End of wdc_pioc.c */
188