1 /*        $NetBSD: wdc_obio.c,v 1.11 2023/12/20 15:00:08 thorpej Exp $          */
2 
3 /*-
4  * Copyright (c) 1998, 2003 The NetBSD Foundation, Inc.
5  * All rights reserved.
6  *
7  * This code is derived from software contributed to The NetBSD Foundation
8  * by Charles M. Hannum and by Onno van der Linden.
9  *
10  * Redistribution and use in source and binary forms, with or without
11  * modification, are permitted provided that the following conditions
12  * are met:
13  * 1. Redistributions of source code must retain the above copyright
14  *    notice, this list of conditions and the following disclaimer.
15  * 2. Redistributions in binary form must reproduce the above copyright
16  *    notice, this list of conditions and the following disclaimer in the
17  *    documentation and/or other materials provided with the distribution.
18  *
19  * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
20  * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
21  * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
22  * PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
23  * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
24  * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
25  * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
26  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
27  * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
28  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
29  * POSSIBILITY OF SUCH DAMAGE.
30  */
31 
32 #include <sys/cdefs.h>
33 __KERNEL_RCSID(0, "$NetBSD: wdc_obio.c,v 1.11 2023/12/20 15:00:08 thorpej Exp $");
34 
35 #include <sys/types.h>
36 #include <sys/param.h>
37 #include <sys/systm.h>
38 #include <sys/device.h>
39 
40 #include <sys/bus.h>
41 #include <machine/intr.h>
42 
43 #include <dev/ata/atavar.h>
44 #include <dev/ic/wdcvar.h>
45 
46 #include <landisk/dev/obiovar.h>
47 
48 struct wdc_obio_softc {
49           struct    wdc_softc sc_wdcdev;
50           struct    ata_channel *sc_chanlist[1];
51           struct    ata_channel sc_channel;
52           struct    wdc_regs sc_wdc_regs;
53           void      *sc_ih;
54 };
55 
56 static int          wdc_obio_probe(device_t, cfdata_t, void *);
57 static void         wdc_obio_attach(device_t, device_t, void *);
58 
59 CFATTACH_DECL_NEW(wdc_obio, sizeof(struct wdc_obio_softc),
60     wdc_obio_probe, wdc_obio_attach, NULL, NULL);
61 
62 #define   WDC_OBIO_REG_NPORTS WDC_NREG
63 #define   WDC_OBIO_REG_SIZE   (WDC_OBIO_REG_NPORTS * 2)
64 #define   WDC_OBIO_AUXREG_NPORTS        1
65 #define   WDC_OBIO_AUXREG_SIZE          (WDC_OBIO_AUXREG_NPORTS * 2)
66 #define   WDC_OBIO_AUXREG_OFFSET        0x2c
67 
68 static int
wdc_obio_probe(device_t parent,cfdata_t cfp,void * aux)69 wdc_obio_probe(device_t parent, cfdata_t cfp, void *aux)
70 {
71           struct obio_attach_args *oa = aux;
72           struct wdc_regs wdr;
73           int result = 0;
74           int i;
75 
76           if (oa->oa_nio < 1)
77                     return (0);
78           if (oa->oa_nirq < 1)
79                     return (0);
80 
81           if (oa->oa_io[0].or_addr == IOBASEUNK)
82                     return (0);
83           if (oa->oa_irq[0].or_irq == IRQUNK)
84                     return (0);
85 
86           wdr.cmd_iot = oa->oa_iot;
87           if (bus_space_map(wdr.cmd_iot, oa->oa_io[0].or_addr,
88               WDC_OBIO_REG_SIZE, 0, &wdr.cmd_baseioh)) {
89                     goto out;
90           }
91           for (i = 0; i < WDC_OBIO_REG_NPORTS; i++) {
92                     if (bus_space_subregion(wdr.cmd_iot, wdr.cmd_baseioh,
93                         i * 2, (i == 0) ? 2 : 1, &wdr.cmd_iohs[i])) {
94                               goto outunmap;
95                     }
96           }
97           wdc_init_shadow_regs(&wdr);
98 
99           wdr.ctl_iot = oa->oa_iot;
100           if (bus_space_map(wdr.ctl_iot,
101               oa->oa_io[0].or_addr + WDC_OBIO_AUXREG_OFFSET,
102               WDC_OBIO_AUXREG_SIZE, 0, &wdr.ctl_ioh)) {
103                     goto outunmap;
104           }
105 
106           result = wdcprobe(&wdr);
107           if (result) {
108                     oa->oa_nio = 1;
109                     oa->oa_io[0].or_size = WDC_OBIO_REG_SIZE;
110                     oa->oa_nirq = 1;
111                     oa->oa_niomem = 0;
112           }
113 
114           bus_space_unmap(wdr.ctl_iot, wdr.ctl_ioh, WDC_OBIO_AUXREG_SIZE);
115 outunmap:
116           bus_space_unmap(wdr.cmd_iot, wdr.cmd_baseioh, WDC_OBIO_REG_SIZE);
117 out:
118           return (result);
119 }
120 
121 static void
wdc_obio_attach(device_t parent,device_t self,void * aux)122 wdc_obio_attach(device_t parent, device_t self, void *aux)
123 {
124           struct wdc_obio_softc *sc = device_private(self);
125           struct obio_attach_args *oa = aux;
126           struct wdc_regs *wdr;
127           int i;
128 
129           aprint_naive("\n");
130           aprint_normal("\n");
131 
132           sc->sc_wdcdev.sc_atac.atac_dev = self;
133           sc->sc_wdcdev.regs = wdr = &sc->sc_wdc_regs;
134 
135           wdr->cmd_iot = oa->oa_iot;
136           wdr->ctl_iot = oa->oa_iot;
137           if (bus_space_map(wdr->cmd_iot, oa->oa_io[0].or_addr,
138               WDC_OBIO_REG_SIZE, 0, &wdr->cmd_baseioh)
139            || bus_space_map(wdr->ctl_iot,
140               oa->oa_io[0].or_addr + WDC_OBIO_AUXREG_OFFSET,
141               WDC_OBIO_AUXREG_SIZE, 0, &wdr->ctl_ioh)) {
142                     aprint_error_dev(self, "couldn't map registers\n");
143                     return;
144           }
145 
146           for (i = 0; i < WDC_OBIO_REG_NPORTS; i++) {
147                     if (bus_space_subregion(wdr->cmd_iot,
148                           wdr->cmd_baseioh, i * 2, (i == 0) ? 2 : 1,
149                           &wdr->cmd_iohs[i]) != 0) {
150                               aprint_error_dev(self,
151                                   "couldn't subregion registers\n");
152                               return;
153                     }
154           }
155 
156           sc->sc_ih = obio_intr_establish(oa->oa_irq[0].or_irq, IPL_BIO, wdcintr,
157               &sc->sc_channel);
158 
159           sc->sc_wdcdev.cap |= WDC_CAPABILITY_PREATA;
160           sc->sc_wdcdev.sc_atac.atac_cap |= ATAC_CAP_DATA16;
161           sc->sc_wdcdev.sc_atac.atac_pio_cap = 0;
162           sc->sc_chanlist[0] = &sc->sc_channel;
163           sc->sc_wdcdev.sc_atac.atac_channels = sc->sc_chanlist;
164           sc->sc_wdcdev.sc_atac.atac_nchannels = 1;
165           sc->sc_wdcdev.wdc_maxdrives = 2;
166           sc->sc_channel.ch_channel = 0;
167           sc->sc_channel.ch_atac = &sc->sc_wdcdev.sc_atac;
168 
169           wdc_init_shadow_regs(wdr);
170 
171           wdcattach(&sc->sc_channel);
172 }
173