xref: /netbsd-src/sys/arch/macppc/dev/mediabay.c (revision aaf4ece63a859a04e37cf3a7229b5fab0157cc06)
1 /*	$NetBSD: mediabay.c,v 1.11 2005/12/11 12:18:03 christos 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.11 2005/12/11 12:18:03 christos 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 	fcr |= FCR_MEDIABAY_ENABLE | FCR_MEDIABAY_RESET;
146 	out32rb(sc->sc_fcr, fcr);
147 	delay(50000);
148 
149 	fcr &= ~FCR_MEDIABAY_RESET;
150 	out32rb(sc->sc_fcr, fcr);
151 	delay(50000);
152 
153 	fcr |= FCR_MEDIABAY_IDE_ENABLE | FCR_MEDIABAY_CD_POWER;
154 	out32rb(sc->sc_fcr, fcr);
155 	delay(50000);
156 
157 	for (child = OF_child(sc->sc_node); child; child = OF_peer(child)) {
158 		memset(name, 0, sizeof(name));
159 		if (OF_getprop(child, "name", name, sizeof(name)) == -1)
160 			continue;
161 		ca.ca_name = name;
162 		ca.ca_node = child;
163 		ca.ca_baseaddr = sc->sc_baseaddr;
164 
165 		ca.ca_nreg  = OF_getprop(child, "reg", reg, sizeof(reg));
166 		ca.ca_nintr = OF_getprop(child, "AAPL,interrupts", intr,
167 				sizeof(intr));
168 		if (ca.ca_nintr == -1)
169 			ca.ca_nintr = OF_getprop(child, "interrupts", intr,
170 					sizeof(intr));
171 		ca.ca_reg = reg;
172 		ca.ca_intr = intr;
173 
174 		content = config_found(&sc->sc_dev, &ca, mediabay_print);
175 		if (content) {
176 			sc->sc_content = content;
177 			return;
178 		}
179 	}
180 
181 	/* No devices found.  Disable media-bay. */
182 	fcr &= ~(FCR_MEDIABAY_ENABLE | FCR_MEDIABAY_IDE_ENABLE |
183 		 FCR_MEDIABAY_CD_POWER | FCR_MEDIABAY_FD_ENABLE);
184 	out32rb(sc->sc_fcr, fcr);
185 }
186 
187 int
188 mediabay_print(aux, mediabay)
189 	void *aux;
190 	const char *mediabay;
191 {
192 	struct confargs *ca = aux;
193 
194 	if (mediabay == NULL && ca->ca_nreg > 0)
195 		aprint_normal(" offset 0x%x", ca->ca_reg[0]);
196 
197 	return QUIET;
198 }
199 
200 int
201 mediabay_intr(v)
202 	void *v;
203 {
204 	struct mediabay_softc *sc = v;
205 
206 	wakeup(&sc->sc_kthread);
207 
208 	return 1;
209 }
210 
211 void
212 mediabay_create_kthread(v)
213 	void *v;
214 {
215 	struct mediabay_softc *sc = v;
216 
217 	kthread_create1(mediabay_kthread, sc, &sc->sc_kthread, "media-bay");
218 }
219 
220 void
221 mediabay_kthread(v)
222 	void *v;
223 {
224 	struct mediabay_softc *sc = v;
225 	u_int x, fcr;
226 
227 	for (;;) {
228 		tsleep(&sc->sc_kthread, PRIBIO, "mbayev", 0);
229 
230 		/* sleep 0.25 sec */
231 		tsleep(mediabay_kthread, PRIBIO, "mbayev", hz/4);
232 
233 		DPRINTF("%s: ", sc->sc_dev.dv_xname);
234 		x = in32rb(sc->sc_addr);
235 
236 		switch (MEDIABAY_ID(x)) {
237 		case MEDIABAY_ID_NONE:
238 			DPRINTF("removed\n");
239 			if (sc->sc_content != NULL) {
240 				config_detach(sc->sc_content, DETACH_FORCE);
241 				DPRINTF("%s: detach done\n",
242 					sc->sc_dev.dv_xname);
243 				sc->sc_content = NULL;
244 
245 				/* disable media-bay */
246 				fcr = in32rb(sc->sc_fcr);
247 				fcr &= ~(FCR_MEDIABAY_ENABLE |
248 					 FCR_MEDIABAY_IDE_ENABLE |
249 					 FCR_MEDIABAY_CD_POWER |
250 					 FCR_MEDIABAY_FD_ENABLE);
251 				out32rb(sc->sc_fcr, fcr);
252 			}
253 			break;
254 		case MEDIABAY_ID_FD:
255 			DPRINTF("FD inserted\n");
256 			break;
257 		case MEDIABAY_ID_CD:
258 			DPRINTF("CD inserted\n");
259 
260 			if (sc->sc_content == NULL)
261 				mediabay_attach_content(sc);
262 			break;
263 		default:
264 			printf("unknown event (0x%x)\n", x);
265 		}
266 	}
267 }
268 
269 /* PBG3: 0x7025X0c0 */
270 /* 3400: 0x7070X080 */
271 /* 2400: 0x0070X0a8 */
272