mediabay.c revision 1.12 1 /* $NetBSD: mediabay.c,v 1.12 2006/12/10 02:41:30 macallan 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.12 2006/12/10 02:41:30 macallan 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, itype;
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 itype = IST_LEVEL;
117
118 if (ca->ca_nintr == 8 && ca->ca_intr[1] == 0)
119 itype = IST_EDGE;
120
121 printf(" irq %d %s\n", irq, intr_typename(itype));
122
123 intr_establish(irq, itype, IPL_BIO, mediabay_intr, sc);
124
125 kthread_create(mediabay_create_kthread, sc);
126
127 sc->sc_content = NULL;
128
129 if (MEDIABAY_ID(in32rb(sc->sc_addr)) != MEDIABAY_ID_NONE)
130 mediabay_attach_content(sc);
131 }
132
133 void
134 mediabay_attach_content(sc)
135 struct mediabay_softc *sc;
136 {
137 int child;
138 u_int fcr;
139 struct device *content;
140 struct confargs ca;
141 u_int reg[20], intr[5];
142 char name[32];
143
144 fcr = in32rb(sc->sc_fcr);
145
146 /*
147 * if the mediabay isn't powered up we need to wait a few seconds
148 * before probing devices
149 */
150 if ((fcr & (FCR_MEDIABAY_ENABLE | FCR_MEDIABAY_IDE_ENABLE
151 | FCR_MEDIABAY_CD_POWER)) != (FCR_MEDIABAY_ENABLE
152 | FCR_MEDIABAY_IDE_ENABLE | FCR_MEDIABAY_CD_POWER)) {
153 fcr |= FCR_MEDIABAY_ENABLE | FCR_MEDIABAY_RESET;
154 out32rb(sc->sc_fcr, fcr);
155 delay(50000);
156
157 fcr &= ~FCR_MEDIABAY_RESET;
158 out32rb(sc->sc_fcr, fcr);
159 delay(50000);
160
161 fcr |= FCR_MEDIABAY_IDE_ENABLE | FCR_MEDIABAY_CD_POWER;
162 out32rb(sc->sc_fcr, fcr);
163 delay(50000);
164 printf("%s: powering up...\n", sc->sc_dev.dv_xname);
165 delay(2000000);
166 }
167
168 for (child = OF_child(sc->sc_node); child; child = OF_peer(child)) {
169 memset(name, 0, sizeof(name));
170 if (OF_getprop(child, "name", name, sizeof(name)) == -1)
171 continue;
172 ca.ca_name = name;
173 ca.ca_node = child;
174 ca.ca_baseaddr = sc->sc_baseaddr;
175
176 ca.ca_nreg = OF_getprop(child, "reg", reg, sizeof(reg));
177 ca.ca_nintr = OF_getprop(child, "AAPL,interrupts", intr,
178 sizeof(intr));
179 if (ca.ca_nintr == -1)
180 ca.ca_nintr = OF_getprop(child, "interrupts", intr,
181 sizeof(intr));
182 ca.ca_reg = reg;
183 ca.ca_intr = intr;
184
185 content = config_found(&sc->sc_dev, &ca, mediabay_print);
186 if (content) {
187 sc->sc_content = content;
188 return;
189 }
190 }
191
192 /* No devices found. Disable media-bay. */
193 fcr &= ~(FCR_MEDIABAY_ENABLE | FCR_MEDIABAY_IDE_ENABLE |
194 FCR_MEDIABAY_CD_POWER | FCR_MEDIABAY_FD_ENABLE);
195 out32rb(sc->sc_fcr, fcr);
196 }
197
198 int
199 mediabay_print(aux, mediabay)
200 void *aux;
201 const char *mediabay;
202 {
203 struct confargs *ca = aux;
204
205 if (mediabay == NULL && ca->ca_nreg > 0)
206 aprint_normal(" offset 0x%x", ca->ca_reg[0]);
207
208 return QUIET;
209 }
210
211 int
212 mediabay_intr(v)
213 void *v;
214 {
215 struct mediabay_softc *sc = v;
216
217 wakeup(&sc->sc_kthread);
218
219 return 1;
220 }
221
222 void
223 mediabay_create_kthread(v)
224 void *v;
225 {
226 struct mediabay_softc *sc = v;
227
228 kthread_create1(mediabay_kthread, sc, &sc->sc_kthread, "media-bay");
229 }
230
231 void
232 mediabay_kthread(v)
233 void *v;
234 {
235 struct mediabay_softc *sc = v;
236 u_int x, fcr;
237
238 for (;;) {
239 tsleep(&sc->sc_kthread, PRIBIO, "mbayev", 0);
240
241 /* sleep 0.25 sec */
242 tsleep(mediabay_kthread, PRIBIO, "mbayev", hz/4);
243
244 DPRINTF("%s: ", sc->sc_dev.dv_xname);
245 x = in32rb(sc->sc_addr);
246
247 switch (MEDIABAY_ID(x)) {
248 case MEDIABAY_ID_NONE:
249 DPRINTF("removed\n");
250 if (sc->sc_content != NULL) {
251 config_detach(sc->sc_content, DETACH_FORCE);
252 DPRINTF("%s: detach done\n",
253 sc->sc_dev.dv_xname);
254 sc->sc_content = NULL;
255
256 /* disable media-bay */
257 fcr = in32rb(sc->sc_fcr);
258 fcr &= ~(FCR_MEDIABAY_ENABLE |
259 FCR_MEDIABAY_IDE_ENABLE |
260 FCR_MEDIABAY_CD_POWER |
261 FCR_MEDIABAY_FD_ENABLE);
262 out32rb(sc->sc_fcr, fcr);
263 }
264 break;
265 case MEDIABAY_ID_FD:
266 DPRINTF("FD inserted\n");
267 break;
268 case MEDIABAY_ID_CD:
269 DPRINTF("CD inserted\n");
270
271 if (sc->sc_content == NULL)
272 mediabay_attach_content(sc);
273 break;
274 default:
275 printf("unknown event (0x%x)\n", x);
276 }
277 }
278 }
279
280 /* PBG3: 0x7025X0c0 */
281 /* 3400: 0x7070X080 */
282 /* 2400: 0x0070X0a8 */
283