xref: /openbsd/sys/arch/macppc/dev/mediabay.c (revision 89ed722c)
1 /*	$OpenBSD: mediabay.c,v 1.8 2022/03/13 12:33:01 mpi 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
89 mediabay_match(parent, v, aux)
90 	struct device *parent;
91 	void *v;
92 	void *aux;
93 {
94 	struct confargs *ca = aux;
95 
96 	if (strcmp(ca->ca_name, "media-bay") == 0 &&
97 	    ca->ca_nintr >= 4 && ca->ca_nreg >= 8)
98 		return 1;
99 
100 	return 0;
101 }
102 
103 /*
104  * Attach all the sub-devices we can find
105  */
106 void
107 mediabay_attach(parent, self, aux)
108 	struct device *parent, *self;
109 	void *aux;
110 {
111 	struct mediabay_softc *sc = (struct mediabay_softc *)self;
112 	struct confargs *ca = aux;
113 	int irq, type;
114 
115 	ca->ca_reg[0] += ca->ca_baseaddr;
116 
117 	sc->sc_addr = mapiodev(ca->ca_reg[0], PAGE_SIZE);
118 	sc->sc_fcr = sc->sc_addr + 1;
119 	sc->sc_node = ca->ca_node;
120 	sc->sc_baseaddr = ca->ca_baseaddr;
121 	sc->sc_iot = ca->ca_iot;
122 	sc->sc_dmat = ca->ca_dmat;
123 	irq = ca->ca_intr[0];
124 	type = IST_LEVEL;
125 
126 	if (ca->ca_nintr == 8 && ca->ca_intr[1] == 0)
127 		type = IST_EDGE;
128 
129 	printf(" irq %d\n", irq);
130 	mac_intr_establish(parent, irq, type, IPL_BIO, mediabay_intr, sc,
131 	    "mediabay");
132 
133 	kthread_create_deferred(mediabay_create_kthread, sc);
134 
135 	sc->sc_content = NULL;
136 
137 	if (MEDIABAY_ID(in32rb(sc->sc_addr)) != MEDIABAY_ID_NONE)
138 		mediabay_attach_content(sc);
139 }
140 
141 void
142 mediabay_attach_content(sc)
143 	struct mediabay_softc *sc;
144 {
145 	int child;
146 	u_int fcr;
147 	struct device *content;
148 	struct confargs ca;
149 	u_int reg[20], intr[5];
150 	char name[32];
151 
152 	fcr = in32rb(sc->sc_fcr);
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 
165 	for (child = OF_child(sc->sc_node); child; child = OF_peer(child)) {
166 		bzero(name, sizeof(name));
167 		if (OF_getprop(child, "name", name, sizeof(name)) == -1)
168 			continue;
169 		ca.ca_name = name;
170 		ca.ca_node = child;
171 		ca.ca_baseaddr = sc->sc_baseaddr;
172 		ca.ca_iot = sc->sc_iot;
173 		ca.ca_dmat = sc->sc_dmat;
174 
175 		ca.ca_nreg  = OF_getprop(child, "reg", reg, sizeof(reg));
176 		ca.ca_nintr = OF_getprop(child, "AAPL,interrupts", intr,
177 				sizeof(intr));
178 		if (ca.ca_nintr == -1)
179 			ca.ca_nintr = OF_getprop(child, "interrupts", intr,
180 					sizeof(intr));
181 		ca.ca_reg = reg;
182 		ca.ca_intr = intr;
183 
184 		content = config_found(&sc->sc_dev, &ca, mediabay_print);
185 		if (content) {
186 			sc->sc_content = content;
187 			return;
188 		}
189 	}
190 
191 	/* No devices found.  Disable media-bay. */
192 	fcr &= ~(FCR_MEDIABAY_ENABLE | FCR_MEDIABAY_IDE_ENABLE |
193 		 FCR_MEDIABAY_CD_POWER | FCR_MEDIABAY_FD_ENABLE);
194 	out32rb(sc->sc_fcr, fcr);
195 }
196 
197 int
198 mediabay_print(aux, mediabay)
199 	void *aux;
200 	const char *mediabay;
201 {
202 	struct confargs *ca = aux;
203 
204 	if (mediabay == NULL && ca->ca_nreg > 0)
205 		printf(" offset 0x%x", ca->ca_reg[0]);
206 
207 	return QUIET;
208 }
209 
210 int
211 mediabay_intr(v)
212 	void *v;
213 {
214 	struct mediabay_softc *sc = v;
215 
216 	wakeup(&sc->sc_kthread);
217 	return 1;
218 }
219 
220 void
221 mediabay_create_kthread(v)
222 	void *v;
223 {
224 	struct mediabay_softc *sc = v;
225 
226 	kthread_create(mediabay_kthread, sc, &sc->sc_kthread,
227 	    sc->sc_dev.dv_xname);
228 }
229 
230 void
231 mediabay_kthread(v)
232 	void *v;
233 {
234 	struct mediabay_softc *sc = v;
235 	u_int x, fcr;
236 
237 sleep:
238 	tsleep_nsec(&sc->sc_kthread, PRIBIO, "mbayev", INFSLP);
239 
240 	/* sleep 0.25 sec */
241 	tsleep_nsec(mediabay_kthread, PRIBIO, "mbayev", MSEC_TO_NSEC(250));
242 
243 	DPRINTF("%s: ", sc->sc_dev.dv_xname);
244 	x = in32rb(sc->sc_addr);
245 
246 	switch (MEDIABAY_ID(x)) {
247 	case MEDIABAY_ID_NONE:
248 		DPRINTF("removed\n");
249 		if (sc->sc_content != NULL) {
250 			config_detach(sc->sc_content, DETACH_FORCE);
251 			DPRINTF("%s: detach done\n", sc->sc_dev.dv_xname);
252 			sc->sc_content = NULL;
253 
254 			/* disable media-bay */
255 			fcr = in32rb(sc->sc_fcr);
256 			fcr &= ~(FCR_MEDIABAY_ENABLE |
257 				 FCR_MEDIABAY_IDE_ENABLE |
258 				 FCR_MEDIABAY_CD_POWER |
259 				 FCR_MEDIABAY_FD_ENABLE);
260 			out32rb(sc->sc_fcr, fcr);
261 		}
262 		break;
263 	case MEDIABAY_ID_FD:
264 		DPRINTF("FD inserted\n");
265 		break;
266 	case MEDIABAY_ID_CD:
267 		DPRINTF("CD inserted\n");
268 
269 		if (sc->sc_content == NULL)
270 			mediabay_attach_content(sc);
271 		break;
272 	default:
273 		printf("unknown event (0x%x)\n", x);
274 	}
275 
276 	goto sleep;
277 }
278 
279 /* PBG3: 0x7025X0c0 */
280 /* 2400: 0x0070X0a8 */
281