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