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