wdc_g1.c revision 1.5 1 1.5 tsutsui /* $NetBSD: wdc_g1.c,v 1.5 2019/09/14 17:11:39 tsutsui 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 g1bus_attach_args *ga = aux;
75 1.1 tsutsui struct wdc_regs wdr;
76 1.1 tsutsui int result = 0, i;
77 1.1 tsutsui
78 1.1 tsutsui *((volatile uint32_t *)0xa05f74e4) = 0x1fffff;
79 1.1 tsutsui for (i = 0; i < 0x200000 / 4; i++)
80 1.1 tsutsui (void)((volatile uint32_t *)0xa0000000)[i];
81 1.1 tsutsui
82 1.1 tsutsui wdr.cmd_iot = ga->ga_memt;
83 1.1 tsutsui if (bus_space_map(wdr.cmd_iot, WDC_G1_CMD_ADDR,
84 1.1 tsutsui WDC_G1_REG_NPORTS * 4, 0, &wdr.cmd_baseioh))
85 1.1 tsutsui goto out;
86 1.1 tsutsui
87 1.1 tsutsui for (i = 0; i < WDC_G1_REG_NPORTS; i++) {
88 1.1 tsutsui if (bus_space_subregion(wdr.cmd_iot, wdr.cmd_baseioh, i * 4,
89 1.1 tsutsui i == 0 ? 2 : 1, &wdr.cmd_iohs[i]) != 0)
90 1.1 tsutsui goto outunmap;
91 1.1 tsutsui }
92 1.1 tsutsui
93 1.4 jdolecek wdc_init_shadow_regs(&wdr);
94 1.1 tsutsui
95 1.1 tsutsui wdr.ctl_iot = ga->ga_memt;
96 1.1 tsutsui if (bus_space_map(wdr.ctl_iot, WDC_G1_CTL_ADDR,
97 1.1 tsutsui WDC_G1_AUXREG_NPORTS, 0, &wdr.ctl_ioh))
98 1.1 tsutsui goto outunmap;
99 1.1 tsutsui
100 1.5 tsutsui result = wdcprobe_with_reset(&wdr, wdc_g1_do_reset);
101 1.1 tsutsui
102 1.1 tsutsui bus_space_unmap(wdr.ctl_iot, wdr.ctl_ioh, WDC_G1_AUXREG_NPORTS);
103 1.1 tsutsui outunmap:
104 1.1 tsutsui bus_space_unmap(wdr.cmd_iot, wdr.cmd_baseioh, WDC_G1_REG_NPORTS);
105 1.1 tsutsui out:
106 1.1 tsutsui return result;
107 1.1 tsutsui }
108 1.1 tsutsui
109 1.1 tsutsui static void
110 1.1 tsutsui wdc_g1_attach(struct device *parent, struct device *self, void *aux)
111 1.1 tsutsui {
112 1.1 tsutsui struct wdc_g1_softc *sc = device_private(self);
113 1.1 tsutsui struct wdc_regs *wdr;
114 1.1 tsutsui struct g1bus_attach_args *ga = aux;
115 1.1 tsutsui int i;
116 1.1 tsutsui
117 1.1 tsutsui sc->sc_wdcdev.sc_atac.atac_dev = self;
118 1.1 tsutsui sc->sc_wdcdev.regs = wdr = &sc->wdc_regs;
119 1.1 tsutsui
120 1.1 tsutsui wdr->cmd_iot = ga->ga_memt;
121 1.1 tsutsui wdr->ctl_iot = ga->ga_memt;
122 1.1 tsutsui if (bus_space_map(wdr->cmd_iot, WDC_G1_CMD_ADDR,
123 1.1 tsutsui WDC_G1_REG_NPORTS * 4, 0, &wdr->cmd_baseioh) ||
124 1.1 tsutsui bus_space_map(wdr->ctl_iot, WDC_G1_CTL_ADDR,
125 1.1 tsutsui WDC_G1_AUXREG_NPORTS, 0, &wdr->ctl_ioh)) {
126 1.1 tsutsui aprint_error(": couldn't map registers\n");
127 1.1 tsutsui return;
128 1.1 tsutsui }
129 1.1 tsutsui
130 1.1 tsutsui for (i = 0; i < WDC_G1_REG_NPORTS; i++) {
131 1.1 tsutsui if (bus_space_subregion(wdr->cmd_iot,
132 1.1 tsutsui wdr->cmd_baseioh, i * 4, i == 0 ? 2 : 1,
133 1.1 tsutsui &wdr->cmd_iohs[i]) != 0) {
134 1.1 tsutsui aprint_error(": couldn't subregion registers\n");
135 1.1 tsutsui return;
136 1.1 tsutsui }
137 1.1 tsutsui }
138 1.1 tsutsui
139 1.1 tsutsui sc->sc_wdcdev.cap |= WDC_CAPABILITY_PREATA;
140 1.1 tsutsui sc->sc_wdcdev.sc_atac.atac_cap |= ATAC_CAP_DATA16;
141 1.1 tsutsui sc->sc_wdcdev.sc_atac.atac_pio_cap = 0;
142 1.1 tsutsui sc->wdc_chanlist[0] = &sc->ata_channel;
143 1.1 tsutsui sc->sc_wdcdev.sc_atac.atac_channels = sc->wdc_chanlist;
144 1.1 tsutsui sc->sc_wdcdev.sc_atac.atac_nchannels = 1;
145 1.1 tsutsui sc->sc_wdcdev.wdc_maxdrives = 2;
146 1.1 tsutsui sc->sc_wdcdev.reset = wdc_g1_do_reset;
147 1.1 tsutsui sc->ata_channel.ch_channel = 0;
148 1.1 tsutsui sc->ata_channel.ch_atac = &sc->sc_wdcdev.sc_atac;
149 1.3 jdolecek
150 1.2 jdolecek wdc_init_shadow_regs(wdr);
151 1.1 tsutsui
152 1.1 tsutsui aprint_normal(": %s\n", sysasic_intr_string(SYSASIC_IRL9));
153 1.1 tsutsui
154 1.1 tsutsui sysasic_intr_establish(SYSASIC_EVENT_GDROM, IPL_BIO, SYSASIC_IRL9,
155 1.1 tsutsui wdc_g1_intr, &sc->ata_channel);
156 1.1 tsutsui
157 1.1 tsutsui wdcattach(&sc->ata_channel);
158 1.1 tsutsui }
159 1.1 tsutsui
160 1.1 tsutsui int
161 1.1 tsutsui wdc_g1_intr(void *arg)
162 1.1 tsutsui {
163 1.1 tsutsui
164 1.1 tsutsui return wdcintr(arg);
165 1.1 tsutsui }
166 1.1 tsutsui
167 1.4 jdolecek /*
168 1.5 tsutsui * This does what the generic wdc_do_reset() does, with additional
169 1.5 tsutsui * GD-ROM reset. GD-ROM is a very early ATAPI device appeared in 1998
170 1.5 tsutsui * and it doesn't reset itself by the WDCTL_RST in AUX_CTLR but requires
171 1.5 tsutsui * ATAPI_SOFT_RESET command to reset whole device as a master.
172 1.4 jdolecek */
173 1.1 tsutsui static void
174 1.1 tsutsui wdc_g1_do_reset(struct ata_channel *chp, int poll)
175 1.1 tsutsui {
176 1.1 tsutsui struct wdc_softc *wdc = CHAN_TO_WDC(chp);
177 1.1 tsutsui struct wdc_regs *wdr = &wdc->regs[chp->ch_channel];
178 1.1 tsutsui int s = 0;
179 1.1 tsutsui
180 1.1 tsutsui if (poll != 0)
181 1.1 tsutsui s = splbio();
182 1.1 tsutsui
183 1.1 tsutsui /* master */
184 1.1 tsutsui bus_space_write_1(wdr->cmd_iot, wdr->cmd_iohs[wd_sdh], 0,
185 1.1 tsutsui WDSD_IBM);
186 1.1 tsutsui delay(10); /* 400ns delay */
187 1.1 tsutsui /* assert SRST, wait for reset to complete */
188 1.1 tsutsui bus_space_write_1(wdr->ctl_iot, wdr->ctl_ioh, wd_aux_ctlr,
189 1.1 tsutsui WDCTL_RST | WDCTL_4BIT | WDCTL_IDS);
190 1.1 tsutsui delay(2000);
191 1.1 tsutsui (void) bus_space_read_1(wdr->cmd_iot, wdr->cmd_iohs[wd_error], 0);
192 1.1 tsutsui bus_space_write_1(wdr->ctl_iot, wdr->ctl_ioh, wd_aux_ctlr,
193 1.1 tsutsui WDCTL_4BIT | WDCTL_IDS);
194 1.1 tsutsui delay(10); /* 400ns delay */
195 1.1 tsutsui
196 1.1 tsutsui /* reset GD-ROM at master via ATAPI command */
197 1.1 tsutsui bus_space_write_1(wdr->cmd_iot, wdr->cmd_iohs[wd_sdh], 0,
198 1.1 tsutsui WDSD_IBM);
199 1.1 tsutsui bus_space_write_1(wdr->cmd_iot, wdr->cmd_iohs[wd_command], 0,
200 1.1 tsutsui ATAPI_SOFT_RESET);
201 1.1 tsutsui delay(100 * 1000);
202 1.1 tsutsui
203 1.1 tsutsui if (poll != 0)
204 1.1 tsutsui splx(s);
205 1.1 tsutsui }
206