wdc_g1.c revision 1.3 1 1.3 jdolecek /* $NetBSD: wdc_g1.c,v 1.3 2017/10/20 07:06:06 jdolecek Exp $ */
2 1.1 tsutsui
3 1.1 tsutsui /*-
4 1.1 tsutsui * Copyright (c) 1998 The NetBSD Foundation, Inc.
5 1.1 tsutsui * All rights reserved.
6 1.1 tsutsui *
7 1.1 tsutsui * This code is derived from software contributed to The NetBSD Foundation
8 1.1 tsutsui * by Charles M. Hannum and by Onno van der Linden.
9 1.1 tsutsui *
10 1.1 tsutsui * Redistribution and use in source and binary forms, with or without
11 1.1 tsutsui * modification, are permitted provided that the following conditions
12 1.1 tsutsui * are met:
13 1.1 tsutsui * 1. Redistributions of source code must retain the above copyright
14 1.1 tsutsui * notice, this list of conditions and the following disclaimer.
15 1.1 tsutsui * 2. Redistributions in binary form must reproduce the above copyright
16 1.1 tsutsui * notice, this list of conditions and the following disclaimer in the
17 1.1 tsutsui * documentation and/or other materials provided with the distribution.
18 1.1 tsutsui *
19 1.1 tsutsui * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
20 1.1 tsutsui * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
21 1.1 tsutsui * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
22 1.1 tsutsui * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
23 1.1 tsutsui * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
24 1.1 tsutsui * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
25 1.1 tsutsui * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
26 1.1 tsutsui * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
27 1.1 tsutsui * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
28 1.1 tsutsui * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
29 1.1 tsutsui * POSSIBILITY OF SUCH DAMAGE.
30 1.1 tsutsui */
31 1.1 tsutsui
32 1.1 tsutsui #include "opt_ata.h" /* for ATADEBUG */
33 1.1 tsutsui
34 1.1 tsutsui #include <sys/param.h>
35 1.1 tsutsui #include <sys/systm.h>
36 1.1 tsutsui #include <sys/device.h>
37 1.1 tsutsui #include <sys/malloc.h>
38 1.1 tsutsui #include <sys/bus.h>
39 1.1 tsutsui
40 1.1 tsutsui #include <machine/intr.h>
41 1.1 tsutsui #include <machine/sysasicvar.h>
42 1.1 tsutsui
43 1.1 tsutsui #include <arch/dreamcast/dev/g1/g1busvar.h>
44 1.1 tsutsui
45 1.1 tsutsui #include <dev/ata/atavar.h>
46 1.1 tsutsui #include <dev/ic/wdcvar.h>
47 1.1 tsutsui #include <dev/ata/atareg.h>
48 1.1 tsutsui
49 1.1 tsutsui #define WDC_G1_CMD_ADDR 0x005f7080
50 1.1 tsutsui #define WDC_G1_REG_NPORTS 8
51 1.1 tsutsui #define WDC_G1_CTL_ADDR 0x005f7018
52 1.1 tsutsui #define WDC_G1_AUXREG_NPORTS 1
53 1.1 tsutsui
54 1.1 tsutsui struct wdc_g1_softc {
55 1.1 tsutsui struct wdc_softc sc_wdcdev;
56 1.1 tsutsui struct ata_channel *wdc_chanlist[1];
57 1.1 tsutsui struct ata_channel ata_channel;
58 1.1 tsutsui struct wdc_regs wdc_regs;
59 1.1 tsutsui void *sc_ih;
60 1.1 tsutsui int sc_irq;
61 1.1 tsutsui };
62 1.1 tsutsui
63 1.1 tsutsui static int wdc_g1_probe(device_t, cfdata_t, void *);
64 1.1 tsutsui static void wdc_g1_attach(device_t, device_t, void *);
65 1.1 tsutsui static void wdc_g1_do_reset(struct ata_channel *, int);
66 1.1 tsutsui static int wdc_g1_intr(void *);
67 1.1 tsutsui
68 1.1 tsutsui CFATTACH_DECL_NEW(wdc_g1bus, sizeof(struct wdc_g1_softc),
69 1.1 tsutsui wdc_g1_probe, wdc_g1_attach, NULL, NULL);
70 1.1 tsutsui
71 1.1 tsutsui static int
72 1.1 tsutsui wdc_g1_probe(device_t parent, cfdata_t cf, void *aux)
73 1.1 tsutsui {
74 1.1 tsutsui struct ata_channel ch;
75 1.1 tsutsui struct g1bus_attach_args *ga = aux;
76 1.1 tsutsui struct wdc_softc wdc;
77 1.1 tsutsui struct wdc_regs wdr;
78 1.1 tsutsui int result = 0, i;
79 1.1 tsutsui #ifdef ATADEBUG
80 1.1 tsutsui struct device dev;
81 1.1 tsutsui #endif
82 1.1 tsutsui
83 1.1 tsutsui *((volatile uint32_t *)0xa05f74e4) = 0x1fffff;
84 1.1 tsutsui for (i = 0; i < 0x200000 / 4; i++)
85 1.1 tsutsui (void)((volatile uint32_t *)0xa0000000)[i];
86 1.1 tsutsui
87 1.1 tsutsui memset(&wdc, 0, sizeof(wdc));
88 1.1 tsutsui memset(&ch, 0, sizeof(ch));
89 1.1 tsutsui ch.ch_atac = &wdc.sc_atac;
90 1.1 tsutsui wdc.reset = wdc_g1_do_reset;
91 1.1 tsutsui wdc.regs = &wdr;
92 1.1 tsutsui
93 1.1 tsutsui wdr.cmd_iot = ga->ga_memt;
94 1.1 tsutsui if (bus_space_map(wdr.cmd_iot, WDC_G1_CMD_ADDR,
95 1.1 tsutsui WDC_G1_REG_NPORTS * 4, 0, &wdr.cmd_baseioh))
96 1.1 tsutsui goto out;
97 1.1 tsutsui
98 1.1 tsutsui for (i = 0; i < WDC_G1_REG_NPORTS; i++) {
99 1.1 tsutsui if (bus_space_subregion(wdr.cmd_iot, wdr.cmd_baseioh, i * 4,
100 1.1 tsutsui i == 0 ? 2 : 1, &wdr.cmd_iohs[i]) != 0)
101 1.1 tsutsui goto outunmap;
102 1.1 tsutsui }
103 1.1 tsutsui
104 1.1 tsutsui wdc_init_shadow_regs(&ch);
105 1.1 tsutsui
106 1.1 tsutsui wdr.ctl_iot = ga->ga_memt;
107 1.1 tsutsui if (bus_space_map(wdr.ctl_iot, WDC_G1_CTL_ADDR,
108 1.1 tsutsui WDC_G1_AUXREG_NPORTS, 0, &wdr.ctl_ioh))
109 1.1 tsutsui goto outunmap;
110 1.1 tsutsui
111 1.1 tsutsui #ifdef ATADEBUG
112 1.1 tsutsui /* fake up device name for ATADEBUG_PRINT() with DEBUG_PROBE */
113 1.1 tsutsui memset(&dev, 0, sizeof(dev));
114 1.1 tsutsui strncat(dev.dv_xname, "wdc(g1probe)", sizeof(dev.dv_xname));
115 1.1 tsutsui wdc.sc_atac.atac_dev = &dev;
116 1.1 tsutsui #endif
117 1.1 tsutsui result = wdcprobe(&ch);
118 1.1 tsutsui
119 1.1 tsutsui bus_space_unmap(wdr.ctl_iot, wdr.ctl_ioh, WDC_G1_AUXREG_NPORTS);
120 1.1 tsutsui outunmap:
121 1.1 tsutsui bus_space_unmap(wdr.cmd_iot, wdr.cmd_baseioh, WDC_G1_REG_NPORTS);
122 1.1 tsutsui out:
123 1.1 tsutsui return result;
124 1.1 tsutsui }
125 1.1 tsutsui
126 1.1 tsutsui static void
127 1.1 tsutsui wdc_g1_attach(struct device *parent, struct device *self, void *aux)
128 1.1 tsutsui {
129 1.1 tsutsui struct wdc_g1_softc *sc = device_private(self);
130 1.1 tsutsui struct wdc_regs *wdr;
131 1.1 tsutsui struct g1bus_attach_args *ga = aux;
132 1.1 tsutsui int i;
133 1.1 tsutsui
134 1.1 tsutsui sc->sc_wdcdev.sc_atac.atac_dev = self;
135 1.1 tsutsui sc->sc_wdcdev.regs = wdr = &sc->wdc_regs;
136 1.1 tsutsui
137 1.1 tsutsui wdr->cmd_iot = ga->ga_memt;
138 1.1 tsutsui wdr->ctl_iot = ga->ga_memt;
139 1.1 tsutsui if (bus_space_map(wdr->cmd_iot, WDC_G1_CMD_ADDR,
140 1.1 tsutsui WDC_G1_REG_NPORTS * 4, 0, &wdr->cmd_baseioh) ||
141 1.1 tsutsui bus_space_map(wdr->ctl_iot, WDC_G1_CTL_ADDR,
142 1.1 tsutsui WDC_G1_AUXREG_NPORTS, 0, &wdr->ctl_ioh)) {
143 1.1 tsutsui aprint_error(": couldn't map registers\n");
144 1.1 tsutsui return;
145 1.1 tsutsui }
146 1.1 tsutsui
147 1.1 tsutsui for (i = 0; i < WDC_G1_REG_NPORTS; i++) {
148 1.1 tsutsui if (bus_space_subregion(wdr->cmd_iot,
149 1.1 tsutsui wdr->cmd_baseioh, i * 4, i == 0 ? 2 : 1,
150 1.1 tsutsui &wdr->cmd_iohs[i]) != 0) {
151 1.1 tsutsui aprint_error(": couldn't subregion registers\n");
152 1.1 tsutsui return;
153 1.1 tsutsui }
154 1.1 tsutsui }
155 1.1 tsutsui
156 1.1 tsutsui sc->sc_wdcdev.cap |= WDC_CAPABILITY_PREATA;
157 1.1 tsutsui sc->sc_wdcdev.sc_atac.atac_cap |= ATAC_CAP_DATA16;
158 1.1 tsutsui sc->sc_wdcdev.sc_atac.atac_pio_cap = 0;
159 1.1 tsutsui sc->wdc_chanlist[0] = &sc->ata_channel;
160 1.1 tsutsui sc->sc_wdcdev.sc_atac.atac_channels = sc->wdc_chanlist;
161 1.1 tsutsui sc->sc_wdcdev.sc_atac.atac_nchannels = 1;
162 1.1 tsutsui sc->sc_wdcdev.wdc_maxdrives = 2;
163 1.1 tsutsui sc->sc_wdcdev.reset = wdc_g1_do_reset;
164 1.1 tsutsui sc->ata_channel.ch_channel = 0;
165 1.1 tsutsui sc->ata_channel.ch_atac = &sc->sc_wdcdev.sc_atac;
166 1.3 jdolecek
167 1.2 jdolecek wdc_init_shadow_regs(wdr);
168 1.1 tsutsui
169 1.1 tsutsui aprint_normal(": %s\n", sysasic_intr_string(SYSASIC_IRL9));
170 1.1 tsutsui
171 1.1 tsutsui sysasic_intr_establish(SYSASIC_EVENT_GDROM, IPL_BIO, SYSASIC_IRL9,
172 1.1 tsutsui wdc_g1_intr, &sc->ata_channel);
173 1.1 tsutsui
174 1.1 tsutsui wdcattach(&sc->ata_channel);
175 1.1 tsutsui }
176 1.1 tsutsui
177 1.1 tsutsui int
178 1.1 tsutsui wdc_g1_intr(void *arg)
179 1.1 tsutsui {
180 1.1 tsutsui
181 1.1 tsutsui return wdcintr(arg);
182 1.1 tsutsui }
183 1.1 tsutsui
184 1.1 tsutsui static void
185 1.1 tsutsui wdc_g1_do_reset(struct ata_channel *chp, int poll)
186 1.1 tsutsui {
187 1.1 tsutsui struct wdc_softc *wdc = CHAN_TO_WDC(chp);
188 1.1 tsutsui struct wdc_regs *wdr = &wdc->regs[chp->ch_channel];
189 1.1 tsutsui int s = 0;
190 1.1 tsutsui
191 1.1 tsutsui if (poll != 0)
192 1.1 tsutsui s = splbio();
193 1.1 tsutsui
194 1.1 tsutsui /* master */
195 1.1 tsutsui bus_space_write_1(wdr->cmd_iot, wdr->cmd_iohs[wd_sdh], 0,
196 1.1 tsutsui WDSD_IBM);
197 1.1 tsutsui delay(10); /* 400ns delay */
198 1.1 tsutsui /* assert SRST, wait for reset to complete */
199 1.1 tsutsui bus_space_write_1(wdr->ctl_iot, wdr->ctl_ioh, wd_aux_ctlr,
200 1.1 tsutsui WDCTL_RST | WDCTL_4BIT | WDCTL_IDS);
201 1.1 tsutsui delay(2000);
202 1.1 tsutsui (void) bus_space_read_1(wdr->cmd_iot, wdr->cmd_iohs[wd_error], 0);
203 1.1 tsutsui bus_space_write_1(wdr->ctl_iot, wdr->ctl_ioh, wd_aux_ctlr,
204 1.1 tsutsui WDCTL_4BIT | WDCTL_IDS);
205 1.1 tsutsui delay(10); /* 400ns delay */
206 1.1 tsutsui
207 1.1 tsutsui /* reset GD-ROM at master via ATAPI command */
208 1.1 tsutsui bus_space_write_1(wdr->cmd_iot, wdr->cmd_iohs[wd_sdh], 0,
209 1.1 tsutsui WDSD_IBM);
210 1.1 tsutsui bus_space_write_1(wdr->cmd_iot, wdr->cmd_iohs[wd_command], 0,
211 1.1 tsutsui ATAPI_SOFT_RESET);
212 1.1 tsutsui delay(100 * 1000);
213 1.1 tsutsui
214 1.1 tsutsui if (poll != 0)
215 1.1 tsutsui splx(s);
216 1.1 tsutsui }
217