Home | History | Annotate | Line # | Download | only in dev
mediabay.c revision 1.2.12.1
      1  1.2.12.1      tv /*	$NetBSD: mediabay.c,v 1.2.12.1 2000/11/01 16:24:36 tv Exp $	*/
      2       1.1  tsubai 
      3       1.1  tsubai /*-
      4       1.1  tsubai  * Copyright (C) 1999 Tsubai Masanari.  All rights reserved.
      5       1.1  tsubai  *
      6       1.1  tsubai  * Redistribution and use in source and binary forms, with or without
      7       1.1  tsubai  * modification, are permitted provided that the following conditions
      8       1.1  tsubai  * are met:
      9       1.1  tsubai  * 1. Redistributions of source code must retain the above copyright
     10       1.1  tsubai  *    notice, this list of conditions and the following disclaimer.
     11       1.1  tsubai  * 2. Redistributions in binary form must reproduce the above copyright
     12       1.1  tsubai  *    notice, this list of conditions and the following disclaimer in the
     13       1.1  tsubai  *    documentation and/or other materials provided with the distribution.
     14       1.1  tsubai  * 3. The name of the author may not be used to endorse or promote products
     15       1.1  tsubai  *    derived from this software without specific prior written permission.
     16       1.1  tsubai  *
     17       1.1  tsubai  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
     18       1.1  tsubai  * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
     19       1.1  tsubai  * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
     20       1.1  tsubai  * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
     21       1.1  tsubai  * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
     22       1.1  tsubai  * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
     23       1.1  tsubai  * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
     24       1.1  tsubai  * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
     25       1.1  tsubai  * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
     26       1.1  tsubai  * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
     27       1.1  tsubai  */
     28       1.1  tsubai 
     29       1.1  tsubai #include <sys/param.h>
     30       1.2  tsubai #include <sys/device.h>
     31       1.2  tsubai #include <sys/kernel.h>
     32       1.2  tsubai #include <sys/kthread.h>
     33       1.1  tsubai #include <sys/systm.h>
     34       1.2  tsubai 
     35       1.2  tsubai #include <dev/ofw/openfirm.h>
     36       1.1  tsubai 
     37       1.1  tsubai #include <machine/autoconf.h>
     38       1.1  tsubai #include <machine/pio.h>
     39       1.1  tsubai 
     40       1.1  tsubai struct mediabay_softc {
     41       1.1  tsubai 	struct device sc_dev;
     42       1.1  tsubai 	int sc_node;
     43       1.1  tsubai 	u_int *sc_addr;
     44       1.1  tsubai 	u_int *sc_fcr;
     45       1.2  tsubai 	u_int sc_baseaddr;
     46       1.2  tsubai 	struct device *sc_content;
     47       1.2  tsubai 	struct proc *sc_kthread;
     48       1.1  tsubai };
     49       1.1  tsubai 
     50       1.2  tsubai void mediabay_attach __P((struct device *, struct device *, void *));
     51       1.2  tsubai int mediabay_match __P((struct device *, struct cfdata *, void *));
     52       1.2  tsubai int mediabay_print __P((void *, const char *));
     53       1.2  tsubai void mediabay_attach_content __P((struct mediabay_softc *));
     54       1.2  tsubai int mediabay_intr __P((void *));
     55       1.2  tsubai void mediabay_create_kthread __P((void *));
     56       1.2  tsubai void mediabay_kthread __P((void *));
     57       1.1  tsubai 
     58       1.1  tsubai struct cfattach mediabay_ca = {
     59       1.1  tsubai 	sizeof(struct mediabay_softc), mediabay_match, mediabay_attach
     60       1.1  tsubai };
     61       1.1  tsubai 
     62       1.2  tsubai #ifdef MEDIABAY_DEBUG
     63       1.2  tsubai # define DPRINTF printf
     64       1.2  tsubai #else
     65       1.2  tsubai # define DPRINTF while (0) printf
     66       1.2  tsubai #endif
     67       1.1  tsubai 
     68       1.1  tsubai #define FCR_MEDIABAY_RESET	0x00000002
     69       1.1  tsubai #define FCR_MEDIABAY_IDE_ENABLE	0x00000008
     70       1.1  tsubai #define FCR_MEDIABAY_FD_ENABLE	0x00000010
     71       1.1  tsubai #define FCR_MEDIABAY_ENABLE	0x00000080
     72       1.1  tsubai #define FCR_MEDIABAY_CD_POWER	0x00800000
     73       1.1  tsubai 
     74       1.2  tsubai #define MEDIABAY_ID(x)		(((x) >> 12) & 0xf)
     75       1.1  tsubai #define MEDIABAY_ID_FD		0
     76       1.1  tsubai #define MEDIABAY_ID_CD		3
     77       1.1  tsubai #define MEDIABAY_ID_NONE	7
     78       1.1  tsubai 
     79       1.1  tsubai int
     80       1.1  tsubai mediabay_match(parent, cf, aux)
     81       1.1  tsubai 	struct device *parent;
     82       1.1  tsubai 	struct cfdata *cf;
     83       1.1  tsubai 	void *aux;
     84       1.1  tsubai {
     85       1.1  tsubai 	struct confargs *ca = aux;
     86       1.1  tsubai 
     87       1.1  tsubai 	if (strcmp(ca->ca_name, "media-bay") == 0)
     88       1.1  tsubai 		return 1;
     89       1.1  tsubai 
     90       1.1  tsubai 	return 0;
     91       1.1  tsubai }
     92       1.1  tsubai 
     93       1.1  tsubai /*
     94       1.1  tsubai  * Attach all the sub-devices we can find
     95       1.1  tsubai  */
     96       1.1  tsubai void
     97       1.1  tsubai mediabay_attach(parent, self, aux)
     98       1.1  tsubai 	struct device *parent, *self;
     99       1.1  tsubai 	void *aux;
    100       1.1  tsubai {
    101       1.1  tsubai 	struct mediabay_softc *sc = (struct mediabay_softc *)self;
    102       1.2  tsubai 	struct confargs *ca = aux;
    103  1.2.12.1      tv 	int irq, type;
    104       1.2  tsubai 
    105       1.2  tsubai 	ca->ca_reg[0] += ca->ca_baseaddr;
    106       1.1  tsubai 
    107       1.2  tsubai 	sc->sc_addr = mapiodev(ca->ca_reg[0], NBPG);
    108       1.1  tsubai 	sc->sc_fcr = sc->sc_addr + 1;
    109       1.2  tsubai 	sc->sc_node = ca->ca_node;
    110       1.2  tsubai 	sc->sc_baseaddr = ca->ca_baseaddr;
    111       1.2  tsubai 	irq = ca->ca_intr[0];
    112  1.2.12.1      tv 	type = IST_LEVEL;
    113       1.1  tsubai 
    114  1.2.12.1      tv 	if (ca->ca_nintr == 8 && ca->ca_intr[1] == 0)
    115  1.2.12.1      tv 		type = IST_EDGE;
    116  1.2.12.1      tv 
    117  1.2.12.1      tv 	printf(" irq %d %s\n", irq, intr_typename(type));
    118  1.2.12.1      tv 	intr_establish(irq, type, IPL_BIO, mediabay_intr, sc);
    119       1.1  tsubai 
    120       1.2  tsubai 	kthread_create(mediabay_create_kthread, sc);
    121       1.2  tsubai 
    122       1.2  tsubai 	sc->sc_content = NULL;
    123       1.2  tsubai 
    124       1.2  tsubai 	if (MEDIABAY_ID(in32rb(sc->sc_addr)) != MEDIABAY_ID_NONE)
    125       1.2  tsubai 		mediabay_attach_content(sc);
    126       1.2  tsubai }
    127       1.2  tsubai 
    128       1.2  tsubai void
    129       1.2  tsubai mediabay_attach_content(sc)
    130       1.2  tsubai 	struct mediabay_softc *sc;
    131       1.2  tsubai {
    132       1.2  tsubai 	int child;
    133       1.2  tsubai 	u_int fcr;
    134       1.2  tsubai 	struct device *content;
    135       1.2  tsubai 	struct confargs ca;
    136       1.2  tsubai 	u_int reg[20], intr[5];
    137       1.2  tsubai 	char name[32];
    138       1.2  tsubai 
    139       1.1  tsubai 	fcr = in32rb(sc->sc_fcr);
    140       1.1  tsubai 	fcr |= FCR_MEDIABAY_ENABLE | FCR_MEDIABAY_RESET;
    141       1.1  tsubai 	out32rb(sc->sc_fcr, fcr);
    142       1.1  tsubai 	delay(50000);
    143       1.1  tsubai 
    144       1.1  tsubai 	fcr &= ~FCR_MEDIABAY_RESET;
    145       1.1  tsubai 	out32rb(sc->sc_fcr, fcr);
    146       1.1  tsubai 	delay(50000);
    147       1.1  tsubai 
    148       1.1  tsubai 	fcr |= FCR_MEDIABAY_IDE_ENABLE | FCR_MEDIABAY_CD_POWER;
    149       1.1  tsubai 	out32rb(sc->sc_fcr, fcr);
    150       1.1  tsubai 	delay(50000);
    151       1.1  tsubai 
    152       1.1  tsubai 	for (child = OF_child(sc->sc_node); child; child = OF_peer(child)) {
    153       1.1  tsubai 		bzero(name, sizeof(name));
    154       1.1  tsubai 		if (OF_getprop(child, "name", name, sizeof(name)) == -1)
    155       1.1  tsubai 			continue;
    156       1.1  tsubai 		ca.ca_name = name;
    157       1.1  tsubai 		ca.ca_node = child;
    158       1.2  tsubai 		ca.ca_baseaddr = sc->sc_baseaddr;
    159       1.1  tsubai 
    160       1.1  tsubai 		ca.ca_nreg  = OF_getprop(child, "reg", reg, sizeof(reg));
    161       1.1  tsubai 		ca.ca_nintr = OF_getprop(child, "AAPL,interrupts", intr,
    162       1.1  tsubai 				sizeof(intr));
    163       1.1  tsubai 		if (ca.ca_nintr == -1)
    164       1.1  tsubai 			ca.ca_nintr = OF_getprop(child, "interrupts", intr,
    165       1.1  tsubai 					sizeof(intr));
    166       1.1  tsubai 		ca.ca_reg = reg;
    167       1.1  tsubai 		ca.ca_intr = intr;
    168       1.1  tsubai 
    169       1.2  tsubai 		content = config_found(&sc->sc_dev, &ca, mediabay_print);
    170       1.2  tsubai 		if (content) {
    171       1.2  tsubai 			sc->sc_content = content;
    172       1.2  tsubai 			return;
    173       1.2  tsubai 		}
    174       1.1  tsubai 	}
    175       1.2  tsubai 
    176       1.2  tsubai 	/* No devices found.  Disable media-bay. */
    177       1.2  tsubai 	fcr &= ~(FCR_MEDIABAY_ENABLE | FCR_MEDIABAY_IDE_ENABLE |
    178       1.2  tsubai 		 FCR_MEDIABAY_CD_POWER | FCR_MEDIABAY_FD_ENABLE);
    179       1.2  tsubai 	out32rb(sc->sc_fcr, fcr);
    180       1.1  tsubai }
    181       1.1  tsubai 
    182       1.1  tsubai int
    183       1.1  tsubai mediabay_print(aux, mediabay)
    184       1.1  tsubai 	void *aux;
    185       1.1  tsubai 	const char *mediabay;
    186       1.1  tsubai {
    187       1.1  tsubai 	struct confargs *ca = aux;
    188       1.1  tsubai 
    189       1.2  tsubai 	if (mediabay == NULL && ca->ca_nreg > 0)
    190       1.1  tsubai 		printf(" offset 0x%x", ca->ca_reg[0]);
    191       1.1  tsubai 
    192       1.2  tsubai 	return QUIET;
    193       1.1  tsubai }
    194       1.1  tsubai 
    195       1.1  tsubai int
    196       1.1  tsubai mediabay_intr(v)
    197       1.1  tsubai 	void *v;
    198       1.1  tsubai {
    199       1.1  tsubai 	struct mediabay_softc *sc = v;
    200       1.1  tsubai 
    201       1.2  tsubai 	wakeup(&sc->sc_kthread);
    202       1.2  tsubai 	return 1;
    203       1.2  tsubai }
    204       1.2  tsubai 
    205       1.2  tsubai void
    206       1.2  tsubai mediabay_create_kthread(v)
    207       1.2  tsubai 	void *v;
    208       1.2  tsubai {
    209       1.2  tsubai 	struct mediabay_softc *sc = v;
    210       1.2  tsubai 
    211       1.2  tsubai 	kthread_create1(mediabay_kthread, sc, &sc->sc_kthread, "media-bay");
    212       1.2  tsubai }
    213       1.2  tsubai 
    214       1.2  tsubai void
    215       1.2  tsubai mediabay_kthread(v)
    216       1.2  tsubai 	void *v;
    217       1.2  tsubai {
    218       1.2  tsubai 	struct mediabay_softc *sc = v;
    219       1.2  tsubai 	u_int x, fcr;
    220       1.2  tsubai 
    221       1.2  tsubai sleep:
    222       1.2  tsubai 	tsleep(&sc->sc_kthread, PRIBIO, "mbayev", 0);
    223       1.2  tsubai 
    224       1.2  tsubai 	/* sleep 0.25 sec */
    225       1.2  tsubai 	tsleep(mediabay_kthread, PRIBIO, "mbayev", hz/4);
    226       1.2  tsubai 
    227       1.2  tsubai 	DPRINTF("%s: ", sc->sc_dev.dv_xname);
    228       1.1  tsubai 	x = in32rb(sc->sc_addr);
    229       1.1  tsubai 
    230       1.1  tsubai 	switch (MEDIABAY_ID(x)) {
    231       1.1  tsubai 	case MEDIABAY_ID_NONE:
    232       1.2  tsubai 		DPRINTF("removed\n");
    233       1.2  tsubai 		if (sc->sc_content != NULL) {
    234       1.2  tsubai 			config_detach(sc->sc_content, DETACH_FORCE);
    235       1.2  tsubai 			DPRINTF("%s: detach done\n", sc->sc_dev.dv_xname);
    236       1.2  tsubai 			sc->sc_content = NULL;
    237       1.2  tsubai 
    238       1.2  tsubai 			/* disable media-bay */
    239       1.2  tsubai 			fcr = in32rb(sc->sc_fcr);
    240       1.2  tsubai 			fcr &= ~(FCR_MEDIABAY_ENABLE |
    241       1.2  tsubai 				 FCR_MEDIABAY_IDE_ENABLE |
    242       1.2  tsubai 				 FCR_MEDIABAY_CD_POWER |
    243       1.2  tsubai 				 FCR_MEDIABAY_FD_ENABLE);
    244       1.2  tsubai 			out32rb(sc->sc_fcr, fcr);
    245       1.2  tsubai 		}
    246       1.1  tsubai 		break;
    247       1.1  tsubai 	case MEDIABAY_ID_FD:
    248       1.2  tsubai 		DPRINTF("FD inserted\n");
    249       1.1  tsubai 		break;
    250       1.1  tsubai 	case MEDIABAY_ID_CD:
    251       1.2  tsubai 		DPRINTF("CD inserted\n");
    252       1.2  tsubai 
    253       1.2  tsubai 		if (sc->sc_content == NULL)
    254       1.2  tsubai 			mediabay_attach_content(sc);
    255       1.1  tsubai 		break;
    256       1.1  tsubai 	default:
    257       1.1  tsubai 		printf("unknown event (0x%x)\n", x);
    258       1.1  tsubai 	}
    259       1.1  tsubai 
    260       1.2  tsubai 	goto sleep;
    261       1.1  tsubai }
    262       1.1  tsubai 
    263       1.1  tsubai /* PBG3: 0x7025X0c0 */
    264       1.1  tsubai /* 2400: 0x0070X0a8 */
    265