mediabay.c revision 1.12.8.1 1 /* $NetBSD: mediabay.c,v 1.12.8.1 2007/07/11 20:00:38 mjf 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.8.1 2007/07/11 20:00:38 mjf 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 lwp_t *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_kthread __P((void *));
61
62 CFATTACH_DECL(mediabay, sizeof(struct mediabay_softc),
63 mediabay_match, mediabay_attach, NULL, NULL);
64
65 #ifdef MEDIABAY_DEBUG
66 # define DPRINTF printf
67 #else
68 # define DPRINTF while (0) printf
69 #endif
70
71 #define FCR_MEDIABAY_RESET 0x00000002
72 #define FCR_MEDIABAY_IDE_ENABLE 0x00000008
73 #define FCR_MEDIABAY_FD_ENABLE 0x00000010
74 #define FCR_MEDIABAY_ENABLE 0x00000080
75 #define FCR_MEDIABAY_CD_POWER 0x00800000
76
77 #define MEDIABAY_ID(x) (((x) >> 12) & 0xf)
78 #define MEDIABAY_ID_FD 0
79 #define MEDIABAY_ID_CD 3
80 #define MEDIABAY_ID_NONE 7
81
82 int
83 mediabay_match(parent, cf, aux)
84 struct device *parent;
85 struct cfdata *cf;
86 void *aux;
87 {
88 struct confargs *ca = aux;
89
90 if (strcmp(ca->ca_name, "media-bay") == 0)
91 return 1;
92
93 return 0;
94 }
95
96 /*
97 * Attach all the sub-devices we can find
98 */
99 void
100 mediabay_attach(parent, self, aux)
101 struct device *parent, *self;
102 void *aux;
103 {
104 struct mediabay_softc *sc = (struct mediabay_softc *)self;
105 struct confargs *ca = aux;
106 int irq, itype;
107
108 ca->ca_reg[0] += ca->ca_baseaddr;
109
110 sc->sc_addr = mapiodev(ca->ca_reg[0], PAGE_SIZE);
111 sc->sc_fcr = sc->sc_addr + 1;
112 sc->sc_node = ca->ca_node;
113 sc->sc_baseaddr = ca->ca_baseaddr;
114 irq = ca->ca_intr[0];
115 itype = IST_LEVEL;
116
117 if (ca->ca_nintr == 8 && ca->ca_intr[1] == 0)
118 itype = IST_EDGE;
119
120 printf(" irq %d %s\n", irq, intr_typename(itype));
121
122 intr_establish(irq, itype, IPL_BIO, mediabay_intr, sc);
123
124 sc->sc_content = NULL;
125
126 if (MEDIABAY_ID(in32rb(sc->sc_addr)) != MEDIABAY_ID_NONE)
127 mediabay_attach_content(sc);
128
129 kthread_create(PRI_NONE, 0, NULL, mediabay_kthread, sc,
130 &sc->sc_kthread, "media-bay");
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_kthread(v)
224 void *v;
225 {
226 struct mediabay_softc *sc = v;
227 u_int x, fcr;
228
229 for (;;) {
230 tsleep(&sc->sc_kthread, PRIBIO, "mbayev", 0);
231
232 /* sleep 0.25 sec */
233 tsleep(mediabay_kthread, PRIBIO, "mbayev", hz/4);
234
235 DPRINTF("%s: ", sc->sc_dev.dv_xname);
236 x = in32rb(sc->sc_addr);
237
238 switch (MEDIABAY_ID(x)) {
239 case MEDIABAY_ID_NONE:
240 DPRINTF("removed\n");
241 if (sc->sc_content != NULL) {
242 config_detach(sc->sc_content, DETACH_FORCE);
243 DPRINTF("%s: detach done\n",
244 sc->sc_dev.dv_xname);
245 sc->sc_content = NULL;
246
247 /* disable media-bay */
248 fcr = in32rb(sc->sc_fcr);
249 fcr &= ~(FCR_MEDIABAY_ENABLE |
250 FCR_MEDIABAY_IDE_ENABLE |
251 FCR_MEDIABAY_CD_POWER |
252 FCR_MEDIABAY_FD_ENABLE);
253 out32rb(sc->sc_fcr, fcr);
254 }
255 break;
256 case MEDIABAY_ID_FD:
257 DPRINTF("FD inserted\n");
258 break;
259 case MEDIABAY_ID_CD:
260 DPRINTF("CD inserted\n");
261
262 if (sc->sc_content == NULL)
263 mediabay_attach_content(sc);
264 break;
265 default:
266 printf("unknown event (0x%x)\n", x);
267 }
268 }
269 }
270
271 /* PBG3: 0x7025X0c0 */
272 /* 3400: 0x7070X080 */
273 /* 2400: 0x0070X0a8 */
274