wdc_spd.c revision 1.12 1 /* $NetBSD: wdc_spd.c,v 1.12 2004/01/03 22:56:53 thorpej Exp $ */
2
3 /*-
4 * Copyright (c) 2001, 2003 The NetBSD Foundation, Inc.
5 * All rights reserved.
6 *
7 * This code is derived from software contributed to The NetBSD Foundation
8 * by UCHIYAMA Yasushi.
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 * 3. All advertising materials mentioning features or use of this software
19 * must display the following acknowledgement:
20 * This product includes software developed by the NetBSD
21 * Foundation, Inc. and its contributors.
22 * 4. Neither the name of The NetBSD Foundation nor the names of its
23 * contributors may be used to endorse or promote products derived
24 * from this software without specific prior written permission.
25 *
26 * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
27 * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
28 * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
29 * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
30 * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
31 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
32 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
33 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
34 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
35 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
36 * POSSIBILITY OF SUCH DAMAGE.
37 */
38
39 #include <sys/cdefs.h>
40 __KERNEL_RCSID(0, "$NetBSD: wdc_spd.c,v 1.12 2004/01/03 22:56:53 thorpej Exp $");
41
42 #include <sys/param.h>
43 #include <sys/systm.h>
44 #include <sys/malloc.h>
45
46 #define __read_1(a) \
47 ({ \
48 u_int32_t a_ = (a); \
49 u_int8_t r = (*(__volatile__ u_int8_t *)a_); \
50 \
51 if (a_ == 0xb400004e) /* (wdc)STAT LED off */ \
52 SPD_LED_OFF(); \
53 \
54 (r); \
55 })
56 #define __write_1(a, v) \
57 { \
58 u_int32_t a_ = (a); \
59 (*(__volatile__ u_int8_t *)a_) = (v); \
60 \
61 if (a_ == 0xb400004e) /* (wdc)CMD LED on */ \
62 SPD_LED_ON(); \
63 }
64 #define _PLAYSTATION2_BUS_SPACE_PRIVATE
65 #include <machine/bus.h>
66
67 #include <dev/ata/atavar.h>
68 #include <dev/ic/wdcvar.h>
69
70 #include <playstation2/ee/eevar.h>
71 #include <playstation2/dev/spdvar.h>
72 #include <playstation2/dev/spdreg.h>
73
74 #define WDC_SPD_HDD_AUXREG_OFFSET 0x1c
75
76 struct wdc_spd_softc {
77 struct wdc_softc sc_wdcdev;
78 struct wdc_channel wdc_chanlist[1];
79 struct wdc_channel wdc_channel;
80 struct ata_queue wdc_chqueue;
81 void *sc_ih;
82 };
83
84 #ifdef DEBUG
85 #define STATIC
86 #else
87 #define STATIC static
88 #endif
89
90 STATIC int wdc_spd_match(struct device *, struct cfdata *, void *);
91 STATIC void wdc_spd_attach(struct device *, struct device *, void *);
92
93 CFATTACH_DECL(wdc_spd, sizeof (struct wdc_spd_softc),
94 wdc_spd_match, wdc_spd_attach, NULL, NULL);
95
96 extern struct cfdriver wdc_cd;
97
98 STATIC void __wdc_spd_enable(void);
99 STATIC void __wdc_spd_disable(void) __attribute__((__unused__));
100 STATIC void __wdc_spd_bus_space(struct wdc_channel *);
101
102 /*
103 * wdc register is 16 bit wide.
104 */
105 #define VADDR(h, o) ((h) + (o))
106 _BUS_SPACE_READ(_wdc_spd, 1, 8)
107 _BUS_SPACE_READ(_wdc_spd, 2, 16)
108 _BUS_SPACE_READ_MULTI(_wdc_spd, 1, 8)
109 _BUS_SPACE_READ_MULTI(_wdc_spd, 2, 16)
110 _BUS_SPACE_READ_REGION(_wdc_spd, 1, 8)
111 _BUS_SPACE_READ_REGION(_wdc_spd, 2, 16)
112 _BUS_SPACE_WRITE(_wdc_spd, 1, 8)
113 _BUS_SPACE_WRITE(_wdc_spd, 2, 16)
114 _BUS_SPACE_WRITE_MULTI(_wdc_spd, 1, 8)
115 _BUS_SPACE_WRITE_MULTI(_wdc_spd, 2, 16)
116 _BUS_SPACE_WRITE_REGION(_wdc_spd, 1, 8)
117 _BUS_SPACE_WRITE_REGION(_wdc_spd, 2, 16)
118 _BUS_SPACE_SET_MULTI(_wdc_spd, 1, 8)
119 _BUS_SPACE_SET_MULTI(_wdc_spd, 2, 16)
120 _BUS_SPACE_SET_REGION(_wdc_spd, 1, 8)
121 _BUS_SPACE_SET_REGION(_wdc_spd, 2, 16)
122 _BUS_SPACE_COPY_REGION(_wdc_spd, 1, 8)
123 _BUS_SPACE_COPY_REGION(_wdc_spd, 2, 16)
124 #undef VADDR
125
126 STATIC const struct playstation2_bus_space _wdc_spd_space = {
127 pbs_map : _BUS_SPACE_NO_MAP,
128 pbs_unmap : _BUS_SPACE_NO_UNMAP,
129 pbs_subregion : _BUS_SPACE_NO_SUBREGION,
130 pbs_alloc : _BUS_SPACE_NO_ALLOC,
131 pbs_free : _BUS_SPACE_NO_FREE,
132 pbs_vaddr : _BUS_SPACE_NO_VADDR,
133 pbs_r_1 : _wdc_spd_read_1,
134 pbs_r_2 : _wdc_spd_read_2,
135 pbs_r_4 : _BUS_SPACE_NO_READ(4, 32),
136 pbs_r_8 : _BUS_SPACE_NO_READ(8, 64),
137 pbs_rm_1 : _wdc_spd_read_multi_1,
138 pbs_rm_2 : _wdc_spd_read_multi_2,
139 pbs_rm_4 : _BUS_SPACE_NO_READ_MULTI(4, 32),
140 pbs_rm_8 : _BUS_SPACE_NO_READ_MULTI(8, 64),
141 pbs_rr_1 : _wdc_spd_read_region_1,
142 pbs_rr_2 : _wdc_spd_read_region_2,
143 pbs_rr_4 : _BUS_SPACE_NO_READ_REGION(4, 32),
144 pbs_rr_8 : _BUS_SPACE_NO_READ_REGION(8, 64),
145 pbs_w_1 : _wdc_spd_write_1,
146 pbs_w_2 : _wdc_spd_write_2,
147 pbs_w_4 : _BUS_SPACE_NO_WRITE(4, 32),
148 pbs_w_8 : _BUS_SPACE_NO_WRITE(8, 64),
149 pbs_wm_1 : _wdc_spd_write_multi_1,
150 pbs_wm_2 : _wdc_spd_write_multi_2,
151 pbs_wm_4 : _BUS_SPACE_NO_WRITE_MULTI(4, 32),
152 pbs_wm_8 : _BUS_SPACE_NO_WRITE_MULTI(8, 64),
153 pbs_wr_1 : _wdc_spd_write_region_1,
154 pbs_wr_2 : _wdc_spd_write_region_2,
155 pbs_wr_4 : _BUS_SPACE_NO_WRITE_REGION(4, 32),
156 pbs_wr_8 : _BUS_SPACE_NO_WRITE_REGION(8, 64),
157 pbs_sm_1 : _wdc_spd_set_multi_1,
158 pbs_sm_2 : _wdc_spd_set_multi_2,
159 pbs_sm_4 : _BUS_SPACE_NO_SET_MULTI(4, 32),
160 pbs_sm_8 : _BUS_SPACE_NO_SET_MULTI(8, 64),
161 pbs_sr_1 : _wdc_spd_set_region_1,
162 pbs_sr_2 : _wdc_spd_set_region_2,
163 pbs_sr_4 : _BUS_SPACE_NO_SET_REGION(4, 32),
164 pbs_sr_8 : _BUS_SPACE_NO_SET_REGION(8, 64),
165 pbs_c_1 : _wdc_spd_copy_region_1,
166 pbs_c_2 : _wdc_spd_copy_region_2,
167 pbs_c_4 : _BUS_SPACE_NO_COPY_REGION(4, 32),
168 pbs_c_8 : _BUS_SPACE_NO_COPY_REGION(8, 64),
169 };
170
171 int
172 wdc_spd_match(struct device *parent, struct cfdata *cf, void *aux)
173 {
174 struct spd_attach_args *spa = aux;
175 struct wdc_channel ch;
176 int i, result;
177
178 if (spa->spa_slot != SPD_HDD)
179 return (0);
180
181 memset(&ch, 0, sizeof(ch));
182 __wdc_spd_bus_space(&ch);
183
184 for (i = 0, result = 0; i < 8; i++) { /* 8 sec */
185 if (result == 0)
186 result = wdcprobe(&ch);
187 delay(1000000);
188 }
189
190 return (result);
191 }
192
193 void
194 wdc_spd_attach(struct device *parent, struct device *self, void *aux)
195 {
196 struct spd_attach_args *spa = aux;
197 struct wdc_spd_softc *sc = (void *)self;
198 struct wdc_softc *wdc = &sc->sc_wdcdev;
199 struct wdc_channel *ch = &sc->wdc_channel;
200
201 printf(": %s\n", spa->spa_product_name);
202
203 __wdc_spd_bus_space(ch);
204
205 wdc->cap =
206 WDC_CAPABILITY_DMA | WDC_CAPABILITY_UDMA | WDC_CAPABILITY_DATA16;
207 wdc->PIO_cap = 0;
208 sc->wdc_chanlist[0] = &sc->wdc_channel;
209 wdc->channels = sc->wdc_chanlist;
210 wdc->nchannels = 1;
211 ch->ch_channel = 0;
212 ch->ch_wdc = &sc->sc_wdcdev;
213 ch->ch_queue = &sc->wdc_chqueue;
214
215 spd_intr_establish(SPD_HDD, wdcintr, &sc->wdc_channel);
216
217 __wdc_spd_enable();
218
219 wdcattach(&sc->wdc_channel);
220 }
221
222 void
223 __wdc_spd_bus_space(struct wdc_channel *ch)
224 {
225 int i;
226
227 ch->cmd_iot = &_wdc_spd_space;
228 for (i = 0; i < 8; i++)
229 ch->cmd_iohs[i] = SPD_HDD_IO_BASE + i * 2; /* wdc register is 16 bit wide. */
230 ch->ctl_iot = &_wdc_spd_space;
231 ch->ctl_ioh = SPD_HDD_IO_BASE + WDC_SPD_HDD_AUXREG_OFFSET;
232 ch->data32iot = ch->cmd_iot;
233 ch->data32ioh = SPD_HDD_IO_BASE;
234 }
235
236 void
237 __wdc_spd_enable()
238 {
239 u_int16_t r;
240
241 r = _reg_read_2(SPD_INTR_ENABLE_REG16);
242 r |= SPD_INTR_HDD;
243 _reg_write_2(SPD_INTR_ENABLE_REG16, r);
244 }
245
246 void
247 __wdc_spd_disable()
248 {
249 u_int16_t r;
250
251 r = _reg_read_2(SPD_INTR_ENABLE_REG16);
252 r &= ~SPD_INTR_HDD;
253 _reg_write_2(SPD_INTR_ENABLE_REG16, r);
254 }
255