Home | History | Annotate | Line # | Download | only in dev
mediabay.c revision 1.7
      1 /*	$NetBSD: mediabay.c,v 1.7 2003/01/01 01:47:30 thorpej 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/param.h>
     30 #include <sys/device.h>
     31 #include <sys/kernel.h>
     32 #include <sys/kthread.h>
     33 #include <sys/systm.h>
     34 
     35 #include <dev/ofw/openfirm.h>
     36 
     37 #include <machine/autoconf.h>
     38 #include <machine/pio.h>
     39 
     40 struct mediabay_softc {
     41 	struct device sc_dev;
     42 	int sc_node;
     43 	u_int *sc_addr;
     44 	u_int *sc_fcr;
     45 	u_int sc_baseaddr;
     46 	struct device *sc_content;
     47 	struct proc *sc_kthread;
     48 };
     49 
     50 void mediabay_attach __P((struct device *, struct device *, void *));
     51 int mediabay_match __P((struct device *, struct cfdata *, void *));
     52 int mediabay_print __P((void *, const char *));
     53 void mediabay_attach_content __P((struct mediabay_softc *));
     54 int mediabay_intr __P((void *));
     55 void mediabay_create_kthread __P((void *));
     56 void mediabay_kthread __P((void *));
     57 
     58 CFATTACH_DECL(mediabay, sizeof(struct mediabay_softc),
     59     mediabay_match, mediabay_attach, NULL, NULL);
     60 
     61 #ifdef MEDIABAY_DEBUG
     62 # define DPRINTF printf
     63 #else
     64 # define DPRINTF while (0) printf
     65 #endif
     66 
     67 #define FCR_MEDIABAY_RESET	0x00000002
     68 #define FCR_MEDIABAY_IDE_ENABLE	0x00000008
     69 #define FCR_MEDIABAY_FD_ENABLE	0x00000010
     70 #define FCR_MEDIABAY_ENABLE	0x00000080
     71 #define FCR_MEDIABAY_CD_POWER	0x00800000
     72 
     73 #define MEDIABAY_ID(x)		(((x) >> 12) & 0xf)
     74 #define MEDIABAY_ID_FD		0
     75 #define MEDIABAY_ID_CD		3
     76 #define MEDIABAY_ID_NONE	7
     77 
     78 int
     79 mediabay_match(parent, cf, aux)
     80 	struct device *parent;
     81 	struct cfdata *cf;
     82 	void *aux;
     83 {
     84 	struct confargs *ca = aux;
     85 
     86 	if (strcmp(ca->ca_name, "media-bay") == 0)
     87 		return 1;
     88 
     89 	return 0;
     90 }
     91 
     92 /*
     93  * Attach all the sub-devices we can find
     94  */
     95 void
     96 mediabay_attach(parent, self, aux)
     97 	struct device *parent, *self;
     98 	void *aux;
     99 {
    100 	struct mediabay_softc *sc = (struct mediabay_softc *)self;
    101 	struct confargs *ca = aux;
    102 	int irq, type;
    103 
    104 	ca->ca_reg[0] += ca->ca_baseaddr;
    105 
    106 	sc->sc_addr = mapiodev(ca->ca_reg[0], NBPG);
    107 	sc->sc_fcr = sc->sc_addr + 1;
    108 	sc->sc_node = ca->ca_node;
    109 	sc->sc_baseaddr = ca->ca_baseaddr;
    110 	irq = ca->ca_intr[0];
    111 	type = IST_LEVEL;
    112 
    113 	if (ca->ca_nintr == 8 && ca->ca_intr[1] == 0)
    114 		type = IST_EDGE;
    115 
    116 	printf(" irq %d %s\n", irq, intr_typename(type));
    117 	intr_establish(irq, type, IPL_BIO, mediabay_intr, sc);
    118 
    119 	kthread_create(mediabay_create_kthread, sc);
    120 
    121 	sc->sc_content = NULL;
    122 
    123 	if (MEDIABAY_ID(in32rb(sc->sc_addr)) != MEDIABAY_ID_NONE)
    124 		mediabay_attach_content(sc);
    125 }
    126 
    127 void
    128 mediabay_attach_content(sc)
    129 	struct mediabay_softc *sc;
    130 {
    131 	int child;
    132 	u_int fcr;
    133 	struct device *content;
    134 	struct confargs ca;
    135 	u_int reg[20], intr[5];
    136 	char name[32];
    137 
    138 	fcr = in32rb(sc->sc_fcr);
    139 	fcr |= FCR_MEDIABAY_ENABLE | FCR_MEDIABAY_RESET;
    140 	out32rb(sc->sc_fcr, fcr);
    141 	delay(50000);
    142 
    143 	fcr &= ~FCR_MEDIABAY_RESET;
    144 	out32rb(sc->sc_fcr, fcr);
    145 	delay(50000);
    146 
    147 	fcr |= FCR_MEDIABAY_IDE_ENABLE | FCR_MEDIABAY_CD_POWER;
    148 	out32rb(sc->sc_fcr, fcr);
    149 	delay(50000);
    150 
    151 	for (child = OF_child(sc->sc_node); child; child = OF_peer(child)) {
    152 		memset(name, 0, sizeof(name));
    153 		if (OF_getprop(child, "name", name, sizeof(name)) == -1)
    154 			continue;
    155 		ca.ca_name = name;
    156 		ca.ca_node = child;
    157 		ca.ca_baseaddr = sc->sc_baseaddr;
    158 
    159 		ca.ca_nreg  = OF_getprop(child, "reg", reg, sizeof(reg));
    160 		ca.ca_nintr = OF_getprop(child, "AAPL,interrupts", intr,
    161 				sizeof(intr));
    162 		if (ca.ca_nintr == -1)
    163 			ca.ca_nintr = OF_getprop(child, "interrupts", intr,
    164 					sizeof(intr));
    165 		ca.ca_reg = reg;
    166 		ca.ca_intr = intr;
    167 
    168 		content = config_found(&sc->sc_dev, &ca, mediabay_print);
    169 		if (content) {
    170 			sc->sc_content = content;
    171 			return;
    172 		}
    173 	}
    174 
    175 	/* No devices found.  Disable media-bay. */
    176 	fcr &= ~(FCR_MEDIABAY_ENABLE | FCR_MEDIABAY_IDE_ENABLE |
    177 		 FCR_MEDIABAY_CD_POWER | FCR_MEDIABAY_FD_ENABLE);
    178 	out32rb(sc->sc_fcr, fcr);
    179 }
    180 
    181 int
    182 mediabay_print(aux, mediabay)
    183 	void *aux;
    184 	const char *mediabay;
    185 {
    186 	struct confargs *ca = aux;
    187 
    188 	if (mediabay == NULL && ca->ca_nreg > 0)
    189 		aprint_normal(" offset 0x%x", ca->ca_reg[0]);
    190 
    191 	return QUIET;
    192 }
    193 
    194 int
    195 mediabay_intr(v)
    196 	void *v;
    197 {
    198 	struct mediabay_softc *sc = v;
    199 
    200 	wakeup(&sc->sc_kthread);
    201 	return 1;
    202 }
    203 
    204 void
    205 mediabay_create_kthread(v)
    206 	void *v;
    207 {
    208 	struct mediabay_softc *sc = v;
    209 
    210 	kthread_create1(mediabay_kthread, sc, &sc->sc_kthread, "media-bay");
    211 }
    212 
    213 void
    214 mediabay_kthread(v)
    215 	void *v;
    216 {
    217 	struct mediabay_softc *sc = v;
    218 	u_int x, fcr;
    219 
    220 sleep:
    221 	tsleep(&sc->sc_kthread, PRIBIO, "mbayev", 0);
    222 
    223 	/* sleep 0.25 sec */
    224 	tsleep(mediabay_kthread, PRIBIO, "mbayev", hz/4);
    225 
    226 	DPRINTF("%s: ", sc->sc_dev.dv_xname);
    227 	x = in32rb(sc->sc_addr);
    228 
    229 	switch (MEDIABAY_ID(x)) {
    230 	case MEDIABAY_ID_NONE:
    231 		DPRINTF("removed\n");
    232 		if (sc->sc_content != NULL) {
    233 			config_detach(sc->sc_content, DETACH_FORCE);
    234 			DPRINTF("%s: detach done\n", sc->sc_dev.dv_xname);
    235 			sc->sc_content = NULL;
    236 
    237 			/* disable media-bay */
    238 			fcr = in32rb(sc->sc_fcr);
    239 			fcr &= ~(FCR_MEDIABAY_ENABLE |
    240 				 FCR_MEDIABAY_IDE_ENABLE |
    241 				 FCR_MEDIABAY_CD_POWER |
    242 				 FCR_MEDIABAY_FD_ENABLE);
    243 			out32rb(sc->sc_fcr, fcr);
    244 		}
    245 		break;
    246 	case MEDIABAY_ID_FD:
    247 		DPRINTF("FD inserted\n");
    248 		break;
    249 	case MEDIABAY_ID_CD:
    250 		DPRINTF("CD inserted\n");
    251 
    252 		if (sc->sc_content == NULL)
    253 			mediabay_attach_content(sc);
    254 		break;
    255 	default:
    256 		printf("unknown event (0x%x)\n", x);
    257 	}
    258 
    259 	goto sleep;
    260 }
    261 
    262 /* PBG3: 0x7025X0c0 */
    263 /* 2400: 0x0070X0a8 */
    264