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