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