mediabay.c revision 1.9 1 /* $NetBSD: mediabay.c,v 1.9 2003/07/15 02:43:29 lukem Exp $ */
2
3 /*-
4 * Copyright (C) 1999 Tsubai Masanari. All rights reserved.
5 *
6 * Redistribution and use in source and binary forms, with or without
7 * modification, are permitted provided that the following conditions
8 * are met:
9 * 1. Redistributions of source code must retain the above copyright
10 * notice, this list of conditions and the following disclaimer.
11 * 2. Redistributions in binary form must reproduce the above copyright
12 * notice, this list of conditions and the following disclaimer in the
13 * documentation and/or other materials provided with the distribution.
14 * 3. The name of the author may not be used to endorse or promote products
15 * derived from this software without specific prior written permission.
16 *
17 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
18 * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
19 * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
20 * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
21 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
22 * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
23 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
24 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
25 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
26 * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
27 */
28
29 #include <sys/cdefs.h>
30 __KERNEL_RCSID(0, "$NetBSD: mediabay.c,v 1.9 2003/07/15 02:43:29 lukem Exp $");
31
32 #include <sys/param.h>
33 #include <sys/device.h>
34 #include <sys/kernel.h>
35 #include <sys/kthread.h>
36 #include <sys/systm.h>
37
38 #include <uvm/uvm_extern.h>
39
40 #include <dev/ofw/openfirm.h>
41
42 #include <machine/autoconf.h>
43 #include <machine/pio.h>
44
45 struct mediabay_softc {
46 struct device sc_dev;
47 int sc_node;
48 u_int *sc_addr;
49 u_int *sc_fcr;
50 u_int sc_baseaddr;
51 struct device *sc_content;
52 struct proc *sc_kthread;
53 };
54
55 void mediabay_attach __P((struct device *, struct device *, void *));
56 int mediabay_match __P((struct device *, struct cfdata *, void *));
57 int mediabay_print __P((void *, const char *));
58 void mediabay_attach_content __P((struct mediabay_softc *));
59 int mediabay_intr __P((void *));
60 void mediabay_create_kthread __P((void *));
61 void mediabay_kthread __P((void *));
62
63 CFATTACH_DECL(mediabay, sizeof(struct mediabay_softc),
64 mediabay_match, mediabay_attach, NULL, NULL);
65
66 #ifdef MEDIABAY_DEBUG
67 # define DPRINTF printf
68 #else
69 # define DPRINTF while (0) printf
70 #endif
71
72 #define FCR_MEDIABAY_RESET 0x00000002
73 #define FCR_MEDIABAY_IDE_ENABLE 0x00000008
74 #define FCR_MEDIABAY_FD_ENABLE 0x00000010
75 #define FCR_MEDIABAY_ENABLE 0x00000080
76 #define FCR_MEDIABAY_CD_POWER 0x00800000
77
78 #define MEDIABAY_ID(x) (((x) >> 12) & 0xf)
79 #define MEDIABAY_ID_FD 0
80 #define MEDIABAY_ID_CD 3
81 #define MEDIABAY_ID_NONE 7
82
83 int
84 mediabay_match(parent, cf, aux)
85 struct device *parent;
86 struct cfdata *cf;
87 void *aux;
88 {
89 struct confargs *ca = aux;
90
91 if (strcmp(ca->ca_name, "media-bay") == 0)
92 return 1;
93
94 return 0;
95 }
96
97 /*
98 * Attach all the sub-devices we can find
99 */
100 void
101 mediabay_attach(parent, self, aux)
102 struct device *parent, *self;
103 void *aux;
104 {
105 struct mediabay_softc *sc = (struct mediabay_softc *)self;
106 struct confargs *ca = aux;
107 int irq, type;
108
109 ca->ca_reg[0] += ca->ca_baseaddr;
110
111 sc->sc_addr = mapiodev(ca->ca_reg[0], PAGE_SIZE);
112 sc->sc_fcr = sc->sc_addr + 1;
113 sc->sc_node = ca->ca_node;
114 sc->sc_baseaddr = ca->ca_baseaddr;
115 irq = ca->ca_intr[0];
116 type = IST_LEVEL;
117
118 if (ca->ca_nintr == 8 && ca->ca_intr[1] == 0)
119 type = IST_EDGE;
120
121 printf(" irq %d %s\n", irq, intr_typename(type));
122 intr_establish(irq, type, IPL_BIO, mediabay_intr, sc);
123
124 kthread_create(mediabay_create_kthread, sc);
125
126 sc->sc_content = NULL;
127
128 if (MEDIABAY_ID(in32rb(sc->sc_addr)) != MEDIABAY_ID_NONE)
129 mediabay_attach_content(sc);
130 }
131
132 void
133 mediabay_attach_content(sc)
134 struct mediabay_softc *sc;
135 {
136 int child;
137 u_int fcr;
138 struct device *content;
139 struct confargs ca;
140 u_int reg[20], intr[5];
141 char name[32];
142
143 fcr = in32rb(sc->sc_fcr);
144 fcr |= FCR_MEDIABAY_ENABLE | FCR_MEDIABAY_RESET;
145 out32rb(sc->sc_fcr, fcr);
146 delay(50000);
147
148 fcr &= ~FCR_MEDIABAY_RESET;
149 out32rb(sc->sc_fcr, fcr);
150 delay(50000);
151
152 fcr |= FCR_MEDIABAY_IDE_ENABLE | FCR_MEDIABAY_CD_POWER;
153 out32rb(sc->sc_fcr, fcr);
154 delay(50000);
155
156 for (child = OF_child(sc->sc_node); child; child = OF_peer(child)) {
157 memset(name, 0, sizeof(name));
158 if (OF_getprop(child, "name", name, sizeof(name)) == -1)
159 continue;
160 ca.ca_name = name;
161 ca.ca_node = child;
162 ca.ca_baseaddr = sc->sc_baseaddr;
163
164 ca.ca_nreg = OF_getprop(child, "reg", reg, sizeof(reg));
165 ca.ca_nintr = OF_getprop(child, "AAPL,interrupts", intr,
166 sizeof(intr));
167 if (ca.ca_nintr == -1)
168 ca.ca_nintr = OF_getprop(child, "interrupts", intr,
169 sizeof(intr));
170 ca.ca_reg = reg;
171 ca.ca_intr = intr;
172
173 content = config_found(&sc->sc_dev, &ca, mediabay_print);
174 if (content) {
175 sc->sc_content = content;
176 return;
177 }
178 }
179
180 /* No devices found. Disable media-bay. */
181 fcr &= ~(FCR_MEDIABAY_ENABLE | FCR_MEDIABAY_IDE_ENABLE |
182 FCR_MEDIABAY_CD_POWER | FCR_MEDIABAY_FD_ENABLE);
183 out32rb(sc->sc_fcr, fcr);
184 }
185
186 int
187 mediabay_print(aux, mediabay)
188 void *aux;
189 const char *mediabay;
190 {
191 struct confargs *ca = aux;
192
193 if (mediabay == NULL && ca->ca_nreg > 0)
194 aprint_normal(" offset 0x%x", ca->ca_reg[0]);
195
196 return QUIET;
197 }
198
199 int
200 mediabay_intr(v)
201 void *v;
202 {
203 struct mediabay_softc *sc = v;
204
205 wakeup(&sc->sc_kthread);
206 return 1;
207 }
208
209 void
210 mediabay_create_kthread(v)
211 void *v;
212 {
213 struct mediabay_softc *sc = v;
214
215 kthread_create1(mediabay_kthread, sc, &sc->sc_kthread, "media-bay");
216 }
217
218 void
219 mediabay_kthread(v)
220 void *v;
221 {
222 struct mediabay_softc *sc = v;
223 u_int x, fcr;
224
225 sleep:
226 tsleep(&sc->sc_kthread, PRIBIO, "mbayev", 0);
227
228 /* sleep 0.25 sec */
229 tsleep(mediabay_kthread, PRIBIO, "mbayev", hz/4);
230
231 DPRINTF("%s: ", sc->sc_dev.dv_xname);
232 x = in32rb(sc->sc_addr);
233
234 switch (MEDIABAY_ID(x)) {
235 case MEDIABAY_ID_NONE:
236 DPRINTF("removed\n");
237 if (sc->sc_content != NULL) {
238 config_detach(sc->sc_content, DETACH_FORCE);
239 DPRINTF("%s: detach done\n", sc->sc_dev.dv_xname);
240 sc->sc_content = NULL;
241
242 /* disable media-bay */
243 fcr = in32rb(sc->sc_fcr);
244 fcr &= ~(FCR_MEDIABAY_ENABLE |
245 FCR_MEDIABAY_IDE_ENABLE |
246 FCR_MEDIABAY_CD_POWER |
247 FCR_MEDIABAY_FD_ENABLE);
248 out32rb(sc->sc_fcr, fcr);
249 }
250 break;
251 case MEDIABAY_ID_FD:
252 DPRINTF("FD inserted\n");
253 break;
254 case MEDIABAY_ID_CD:
255 DPRINTF("CD inserted\n");
256
257 if (sc->sc_content == NULL)
258 mediabay_attach_content(sc);
259 break;
260 default:
261 printf("unknown event (0x%x)\n", x);
262 }
263
264 goto sleep;
265 }
266
267 /* PBG3: 0x7025X0c0 */
268 /* 2400: 0x0070X0a8 */
269