wdc_g1.c revision 1.1 1 1.1 tsutsui /* $NetBSD: wdc_g1.c,v 1.1 2016/12/29 11:49:05 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 ata_queue wdc_chqueue;
59 1.1 tsutsui struct wdc_regs wdc_regs;
60 1.1 tsutsui void *sc_ih;
61 1.1 tsutsui int sc_irq;
62 1.1 tsutsui };
63 1.1 tsutsui
64 1.1 tsutsui static int wdc_g1_probe(device_t, cfdata_t, void *);
65 1.1 tsutsui static void wdc_g1_attach(device_t, device_t, void *);
66 1.1 tsutsui static void wdc_g1_do_reset(struct ata_channel *, int);
67 1.1 tsutsui static int wdc_g1_intr(void *);
68 1.1 tsutsui
69 1.1 tsutsui CFATTACH_DECL_NEW(wdc_g1bus, sizeof(struct wdc_g1_softc),
70 1.1 tsutsui wdc_g1_probe, wdc_g1_attach, NULL, NULL);
71 1.1 tsutsui
72 1.1 tsutsui static int
73 1.1 tsutsui wdc_g1_probe(device_t parent, cfdata_t cf, void *aux)
74 1.1 tsutsui {
75 1.1 tsutsui struct ata_channel ch;
76 1.1 tsutsui struct g1bus_attach_args *ga = aux;
77 1.1 tsutsui struct wdc_softc wdc;
78 1.1 tsutsui struct wdc_regs wdr;
79 1.1 tsutsui int result = 0, i;
80 1.1 tsutsui #ifdef ATADEBUG
81 1.1 tsutsui struct device dev;
82 1.1 tsutsui #endif
83 1.1 tsutsui
84 1.1 tsutsui *((volatile uint32_t *)0xa05f74e4) = 0x1fffff;
85 1.1 tsutsui for (i = 0; i < 0x200000 / 4; i++)
86 1.1 tsutsui (void)((volatile uint32_t *)0xa0000000)[i];
87 1.1 tsutsui
88 1.1 tsutsui memset(&wdc, 0, sizeof(wdc));
89 1.1 tsutsui memset(&ch, 0, sizeof(ch));
90 1.1 tsutsui ch.ch_atac = &wdc.sc_atac;
91 1.1 tsutsui wdc.reset = wdc_g1_do_reset;
92 1.1 tsutsui wdc.regs = &wdr;
93 1.1 tsutsui
94 1.1 tsutsui wdr.cmd_iot = ga->ga_memt;
95 1.1 tsutsui if (bus_space_map(wdr.cmd_iot, WDC_G1_CMD_ADDR,
96 1.1 tsutsui WDC_G1_REG_NPORTS * 4, 0, &wdr.cmd_baseioh))
97 1.1 tsutsui goto out;
98 1.1 tsutsui
99 1.1 tsutsui for (i = 0; i < WDC_G1_REG_NPORTS; i++) {
100 1.1 tsutsui if (bus_space_subregion(wdr.cmd_iot, wdr.cmd_baseioh, i * 4,
101 1.1 tsutsui i == 0 ? 2 : 1, &wdr.cmd_iohs[i]) != 0)
102 1.1 tsutsui goto outunmap;
103 1.1 tsutsui }
104 1.1 tsutsui
105 1.1 tsutsui wdc_init_shadow_regs(&ch);
106 1.1 tsutsui
107 1.1 tsutsui wdr.ctl_iot = ga->ga_memt;
108 1.1 tsutsui if (bus_space_map(wdr.ctl_iot, WDC_G1_CTL_ADDR,
109 1.1 tsutsui WDC_G1_AUXREG_NPORTS, 0, &wdr.ctl_ioh))
110 1.1 tsutsui goto outunmap;
111 1.1 tsutsui
112 1.1 tsutsui #ifdef ATADEBUG
113 1.1 tsutsui /* fake up device name for ATADEBUG_PRINT() with DEBUG_PROBE */
114 1.1 tsutsui memset(&dev, 0, sizeof(dev));
115 1.1 tsutsui strncat(dev.dv_xname, "wdc(g1probe)", sizeof(dev.dv_xname));
116 1.1 tsutsui wdc.sc_atac.atac_dev = &dev;
117 1.1 tsutsui #endif
118 1.1 tsutsui result = wdcprobe(&ch);
119 1.1 tsutsui
120 1.1 tsutsui bus_space_unmap(wdr.ctl_iot, wdr.ctl_ioh, WDC_G1_AUXREG_NPORTS);
121 1.1 tsutsui outunmap:
122 1.1 tsutsui bus_space_unmap(wdr.cmd_iot, wdr.cmd_baseioh, WDC_G1_REG_NPORTS);
123 1.1 tsutsui out:
124 1.1 tsutsui return result;
125 1.1 tsutsui }
126 1.1 tsutsui
127 1.1 tsutsui static void
128 1.1 tsutsui wdc_g1_attach(struct device *parent, struct device *self, void *aux)
129 1.1 tsutsui {
130 1.1 tsutsui struct wdc_g1_softc *sc = device_private(self);
131 1.1 tsutsui struct wdc_regs *wdr;
132 1.1 tsutsui struct g1bus_attach_args *ga = aux;
133 1.1 tsutsui int i;
134 1.1 tsutsui
135 1.1 tsutsui sc->sc_wdcdev.sc_atac.atac_dev = self;
136 1.1 tsutsui sc->sc_wdcdev.regs = wdr = &sc->wdc_regs;
137 1.1 tsutsui
138 1.1 tsutsui wdr->cmd_iot = ga->ga_memt;
139 1.1 tsutsui wdr->ctl_iot = ga->ga_memt;
140 1.1 tsutsui if (bus_space_map(wdr->cmd_iot, WDC_G1_CMD_ADDR,
141 1.1 tsutsui WDC_G1_REG_NPORTS * 4, 0, &wdr->cmd_baseioh) ||
142 1.1 tsutsui bus_space_map(wdr->ctl_iot, WDC_G1_CTL_ADDR,
143 1.1 tsutsui WDC_G1_AUXREG_NPORTS, 0, &wdr->ctl_ioh)) {
144 1.1 tsutsui aprint_error(": couldn't map registers\n");
145 1.1 tsutsui return;
146 1.1 tsutsui }
147 1.1 tsutsui
148 1.1 tsutsui for (i = 0; i < WDC_G1_REG_NPORTS; i++) {
149 1.1 tsutsui if (bus_space_subregion(wdr->cmd_iot,
150 1.1 tsutsui wdr->cmd_baseioh, i * 4, i == 0 ? 2 : 1,
151 1.1 tsutsui &wdr->cmd_iohs[i]) != 0) {
152 1.1 tsutsui aprint_error(": couldn't subregion registers\n");
153 1.1 tsutsui return;
154 1.1 tsutsui }
155 1.1 tsutsui }
156 1.1 tsutsui
157 1.1 tsutsui sc->sc_wdcdev.cap |= WDC_CAPABILITY_PREATA;
158 1.1 tsutsui sc->sc_wdcdev.sc_atac.atac_cap |= ATAC_CAP_DATA16;
159 1.1 tsutsui sc->sc_wdcdev.sc_atac.atac_pio_cap = 0;
160 1.1 tsutsui sc->wdc_chanlist[0] = &sc->ata_channel;
161 1.1 tsutsui sc->sc_wdcdev.sc_atac.atac_channels = sc->wdc_chanlist;
162 1.1 tsutsui sc->sc_wdcdev.sc_atac.atac_nchannels = 1;
163 1.1 tsutsui sc->sc_wdcdev.wdc_maxdrives = 2;
164 1.1 tsutsui sc->sc_wdcdev.reset = wdc_g1_do_reset;
165 1.1 tsutsui sc->ata_channel.ch_channel = 0;
166 1.1 tsutsui sc->ata_channel.ch_atac = &sc->sc_wdcdev.sc_atac;
167 1.1 tsutsui sc->ata_channel.ch_queue = &sc->wdc_chqueue;
168 1.1 tsutsui wdc_init_shadow_regs(&sc->ata_channel);
169 1.1 tsutsui
170 1.1 tsutsui aprint_normal(": %s\n", sysasic_intr_string(SYSASIC_IRL9));
171 1.1 tsutsui
172 1.1 tsutsui sysasic_intr_establish(SYSASIC_EVENT_GDROM, IPL_BIO, SYSASIC_IRL9,
173 1.1 tsutsui wdc_g1_intr, &sc->ata_channel);
174 1.1 tsutsui
175 1.1 tsutsui wdcattach(&sc->ata_channel);
176 1.1 tsutsui }
177 1.1 tsutsui
178 1.1 tsutsui int
179 1.1 tsutsui wdc_g1_intr(void *arg)
180 1.1 tsutsui {
181 1.1 tsutsui
182 1.1 tsutsui return wdcintr(arg);
183 1.1 tsutsui }
184 1.1 tsutsui
185 1.1 tsutsui static void
186 1.1 tsutsui wdc_g1_do_reset(struct ata_channel *chp, int poll)
187 1.1 tsutsui {
188 1.1 tsutsui struct wdc_softc *wdc = CHAN_TO_WDC(chp);
189 1.1 tsutsui struct wdc_regs *wdr = &wdc->regs[chp->ch_channel];
190 1.1 tsutsui int s = 0;
191 1.1 tsutsui
192 1.1 tsutsui if (poll != 0)
193 1.1 tsutsui s = splbio();
194 1.1 tsutsui
195 1.1 tsutsui /* master */
196 1.1 tsutsui bus_space_write_1(wdr->cmd_iot, wdr->cmd_iohs[wd_sdh], 0,
197 1.1 tsutsui WDSD_IBM);
198 1.1 tsutsui delay(10); /* 400ns delay */
199 1.1 tsutsui /* assert SRST, wait for reset to complete */
200 1.1 tsutsui bus_space_write_1(wdr->ctl_iot, wdr->ctl_ioh, wd_aux_ctlr,
201 1.1 tsutsui WDCTL_RST | WDCTL_4BIT | WDCTL_IDS);
202 1.1 tsutsui delay(2000);
203 1.1 tsutsui (void) bus_space_read_1(wdr->cmd_iot, wdr->cmd_iohs[wd_error], 0);
204 1.1 tsutsui bus_space_write_1(wdr->ctl_iot, wdr->ctl_ioh, wd_aux_ctlr,
205 1.1 tsutsui WDCTL_4BIT | WDCTL_IDS);
206 1.1 tsutsui delay(10); /* 400ns delay */
207 1.1 tsutsui
208 1.1 tsutsui /* reset GD-ROM at master via ATAPI command */
209 1.1 tsutsui bus_space_write_1(wdr->cmd_iot, wdr->cmd_iohs[wd_sdh], 0,
210 1.1 tsutsui WDSD_IBM);
211 1.1 tsutsui bus_space_write_1(wdr->cmd_iot, wdr->cmd_iohs[wd_command], 0,
212 1.1 tsutsui ATAPI_SOFT_RESET);
213 1.1 tsutsui delay(100 * 1000);
214 1.1 tsutsui
215 1.1 tsutsui if (poll != 0)
216 1.1 tsutsui splx(s);
217 1.1 tsutsui }
218