Home | History | Annotate | Line # | Download | only in dev
mediabay.c revision 1.14.20.1
      1  1.14.20.1      yamt /*	$NetBSD: mediabay.c,v 1.14.20.1 2009/05/04 08:11:28 yamt 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.14.20.1      yamt __KERNEL_RCSID(0, "$NetBSD: mediabay.c,v 1.14.20.1 2009/05/04 08:11:28 yamt 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.14.20.1      yamt enum mediabay_controller {
     46  1.14.20.1      yamt 	MB_CONTROLLER_KEYLARGO,
     47  1.14.20.1      yamt 	MB_CONTROLLER_OTHER
     48  1.14.20.1      yamt };
     49  1.14.20.1      yamt 
     50        1.1    tsubai struct mediabay_softc {
     51        1.1    tsubai 	struct device sc_dev;
     52       1.14   garbled 	bus_space_tag_t sc_tag;
     53        1.1    tsubai 	int sc_node;
     54        1.1    tsubai 	u_int *sc_addr;
     55        1.1    tsubai 	u_int *sc_fcr;
     56        1.2    tsubai 	u_int sc_baseaddr;
     57        1.2    tsubai 	struct device *sc_content;
     58       1.13        ad 	lwp_t *sc_kthread;
     59  1.14.20.1      yamt 	enum mediabay_controller sc_type;
     60  1.14.20.1      yamt };
     61  1.14.20.1      yamt 
     62  1.14.20.1      yamt static const char *mediabay_keylargo[] = {
     63  1.14.20.1      yamt 	"keylargo-media-bay",
     64  1.14.20.1      yamt 	NULL
     65        1.1    tsubai };
     66        1.1    tsubai 
     67  1.14.20.1      yamt void mediabay_attach(struct device *, struct device *, void *);
     68  1.14.20.1      yamt int mediabay_match(struct device *, struct cfdata *, void *);
     69  1.14.20.1      yamt int mediabay_print(void *, const char *);
     70  1.14.20.1      yamt void mediabay_attach_content(struct mediabay_softc *);
     71  1.14.20.1      yamt int mediabay_intr(void *);
     72  1.14.20.1      yamt void mediabay_kthread(void *);
     73        1.1    tsubai 
     74        1.6   thorpej CFATTACH_DECL(mediabay, sizeof(struct mediabay_softc),
     75        1.6   thorpej     mediabay_match, mediabay_attach, NULL, NULL);
     76        1.1    tsubai 
     77        1.2    tsubai #ifdef MEDIABAY_DEBUG
     78        1.2    tsubai # define DPRINTF printf
     79        1.2    tsubai #else
     80        1.2    tsubai # define DPRINTF while (0) printf
     81        1.2    tsubai #endif
     82        1.1    tsubai 
     83        1.1    tsubai #define FCR_MEDIABAY_RESET	0x00000002
     84        1.1    tsubai #define FCR_MEDIABAY_IDE_ENABLE	0x00000008
     85        1.1    tsubai #define FCR_MEDIABAY_FD_ENABLE	0x00000010
     86        1.1    tsubai #define FCR_MEDIABAY_ENABLE	0x00000080
     87        1.1    tsubai #define FCR_MEDIABAY_CD_POWER	0x00800000
     88        1.1    tsubai 
     89  1.14.20.1      yamt #define MBCR_MEDIABAY0_ENABLE		0x00000100
     90  1.14.20.1      yamt #define MBCR_MEDIABAY0_RESET		0x00000200
     91  1.14.20.1      yamt #define MBCR_MEDIABAY0_POWER		0x00000400
     92  1.14.20.1      yamt #define MBCR_MEDIABAY0_IDE_ENABLE	0x00001000
     93  1.14.20.1      yamt #define MBCR_MEDIABAY0_DEVMASK		0x00007800
     94  1.14.20.1      yamt 
     95  1.14.20.1      yamt #define FCR1_EIDE0_ENABLE	0x00800000
     96  1.14.20.1      yamt #define FCR1_EIDE0_RESET	0x01000000
     97  1.14.20.1      yamt 
     98  1.14.20.1      yamt #define MEDIABAY_ID(sc, x)				\
     99  1.14.20.1      yamt 	((sc)->sc_type == MB_CONTROLLER_KEYLARGO ?	\
    100  1.14.20.1      yamt 	 (((x) >> 4) & 0xf) :				\
    101  1.14.20.1      yamt 	 (((x) >> 12) & 0xf))
    102        1.1    tsubai #define MEDIABAY_ID_FD		0
    103        1.1    tsubai #define MEDIABAY_ID_CD		3
    104        1.1    tsubai #define MEDIABAY_ID_NONE	7
    105        1.1    tsubai 
    106        1.1    tsubai int
    107  1.14.20.1      yamt mediabay_match(struct device *parent, struct cfdata *cf, void *aux)
    108        1.1    tsubai {
    109        1.1    tsubai 	struct confargs *ca = aux;
    110        1.1    tsubai 
    111        1.1    tsubai 	if (strcmp(ca->ca_name, "media-bay") == 0)
    112        1.1    tsubai 		return 1;
    113        1.1    tsubai 
    114        1.1    tsubai 	return 0;
    115        1.1    tsubai }
    116        1.1    tsubai 
    117        1.1    tsubai /*
    118        1.1    tsubai  * Attach all the sub-devices we can find
    119        1.1    tsubai  */
    120        1.1    tsubai void
    121  1.14.20.1      yamt mediabay_attach(struct device *parent, struct device *self, void *aux)
    122        1.1    tsubai {
    123        1.1    tsubai 	struct mediabay_softc *sc = (struct mediabay_softc *)self;
    124        1.2    tsubai 	struct confargs *ca = aux;
    125       1.10    briggs 	int irq, itype;
    126        1.2    tsubai 
    127        1.2    tsubai 	ca->ca_reg[0] += ca->ca_baseaddr;
    128        1.1    tsubai 
    129        1.8   thorpej 	sc->sc_addr = mapiodev(ca->ca_reg[0], PAGE_SIZE);
    130        1.2    tsubai 	sc->sc_node = ca->ca_node;
    131        1.2    tsubai 	sc->sc_baseaddr = ca->ca_baseaddr;
    132       1.14   garbled 	sc->sc_tag = ca->ca_tag;
    133        1.2    tsubai 	irq = ca->ca_intr[0];
    134       1.14   garbled 	itype = IST_EDGE;
    135        1.1    tsubai 
    136  1.14.20.1      yamt 	if (of_compatible(ca->ca_node, mediabay_keylargo) != -1) {
    137  1.14.20.1      yamt 		sc->sc_type = MB_CONTROLLER_KEYLARGO;
    138  1.14.20.1      yamt 		sc->sc_fcr = sc->sc_addr + 2;
    139  1.14.20.1      yamt 	} else {
    140  1.14.20.1      yamt 		sc->sc_type = MB_CONTROLLER_OTHER;
    141  1.14.20.1      yamt 		sc->sc_fcr = sc->sc_addr + 1;
    142  1.14.20.1      yamt 	}
    143  1.14.20.1      yamt 
    144       1.14   garbled 	if (ca->ca_nintr == 8 && ca->ca_intr[1] != 0)
    145       1.14   garbled 		itype = IST_LEVEL;
    146        1.3    tsubai 
    147       1.10    briggs 	printf(" irq %d %s\n", irq, intr_typename(itype));
    148       1.10    briggs 
    149       1.10    briggs 	intr_establish(irq, itype, IPL_BIO, mediabay_intr, sc);
    150        1.1    tsubai 
    151        1.2    tsubai 	sc->sc_content = NULL;
    152        1.2    tsubai 
    153  1.14.20.1      yamt 	if (MEDIABAY_ID(sc, in32rb(sc->sc_addr)) != MEDIABAY_ID_NONE)
    154        1.2    tsubai 		mediabay_attach_content(sc);
    155       1.13        ad 
    156       1.13        ad 	kthread_create(PRI_NONE, 0, NULL, mediabay_kthread, sc,
    157       1.13        ad 	    &sc->sc_kthread, "media-bay");
    158        1.2    tsubai }
    159        1.2    tsubai 
    160        1.2    tsubai void
    161  1.14.20.1      yamt mediabay_attach_content(struct mediabay_softc *sc)
    162        1.2    tsubai {
    163        1.2    tsubai 	int child;
    164  1.14.20.1      yamt 	u_int fcr = 0;
    165        1.2    tsubai 	struct device *content;
    166        1.2    tsubai 	struct confargs ca;
    167        1.2    tsubai 	u_int reg[20], intr[5];
    168        1.2    tsubai 	char name[32];
    169        1.2    tsubai 
    170  1.14.20.1      yamt 	if (sc->sc_type != MB_CONTROLLER_KEYLARGO) {
    171  1.14.20.1      yamt 		fcr = in32rb(sc->sc_fcr);
    172        1.1    tsubai 
    173  1.14.20.1      yamt 		/*
    174  1.14.20.1      yamt 		 * if the mediabay isn't powered up we need to wait a few seconds
    175  1.14.20.1      yamt 		 * before probing devices
    176  1.14.20.1      yamt 		 */
    177  1.14.20.1      yamt 		if ((fcr & (FCR_MEDIABAY_ENABLE | FCR_MEDIABAY_IDE_ENABLE
    178  1.14.20.1      yamt 		    | FCR_MEDIABAY_CD_POWER)) != (FCR_MEDIABAY_ENABLE
    179  1.14.20.1      yamt 		    | FCR_MEDIABAY_IDE_ENABLE | FCR_MEDIABAY_CD_POWER)) {
    180  1.14.20.1      yamt 			fcr |= FCR_MEDIABAY_ENABLE | FCR_MEDIABAY_RESET;
    181  1.14.20.1      yamt 			out32rb(sc->sc_fcr, fcr);
    182  1.14.20.1      yamt 			delay(50000);
    183  1.14.20.1      yamt 
    184  1.14.20.1      yamt 			fcr &= ~FCR_MEDIABAY_RESET;
    185  1.14.20.1      yamt 			out32rb(sc->sc_fcr, fcr);
    186  1.14.20.1      yamt 			delay(50000);
    187  1.14.20.1      yamt 
    188  1.14.20.1      yamt 			fcr |= FCR_MEDIABAY_IDE_ENABLE | FCR_MEDIABAY_CD_POWER;
    189  1.14.20.1      yamt 			out32rb(sc->sc_fcr, fcr);
    190  1.14.20.1      yamt 			delay(50000);
    191  1.14.20.1      yamt 			printf("%s: powering up...\n", sc->sc_dev.dv_xname);
    192  1.14.20.1      yamt 			delay(2000000);
    193  1.14.20.1      yamt 		}
    194  1.14.20.1      yamt 	} else {
    195  1.14.20.1      yamt 		printf("%s: powering up keylargo-media-bay..", sc->sc_dev.dv_xname);
    196  1.14.20.1      yamt 
    197  1.14.20.1      yamt 		out32rb(sc->sc_addr, in32rb(sc->sc_addr) & ~MBCR_MEDIABAY0_RESET);
    198  1.14.20.1      yamt 		out32rb(sc->sc_addr, in32rb(sc->sc_addr) & ~MBCR_MEDIABAY0_POWER);
    199       1.12  macallan 		delay(50000);
    200       1.12  macallan 
    201  1.14.20.1      yamt 		out32rb(sc->sc_addr, in32rb(sc->sc_addr) & ~MBCR_MEDIABAY0_ENABLE);
    202       1.12  macallan 		delay(50000);
    203       1.12  macallan 
    204  1.14.20.1      yamt 		out32rb(sc->sc_addr,
    205  1.14.20.1      yamt 		    in32rb(sc->sc_addr) | MBCR_MEDIABAY0_IDE_ENABLE);
    206       1.12  macallan 		delay(50000);
    207  1.14.20.1      yamt 
    208  1.14.20.1      yamt 		out32rb(sc->sc_addr, in32rb(sc->sc_addr) | MBCR_MEDIABAY0_RESET);
    209  1.14.20.1      yamt 		delay(50000);
    210  1.14.20.1      yamt 
    211  1.14.20.1      yamt 		out32rb(sc->sc_fcr, in32rb(sc->sc_fcr) | FCR1_EIDE0_RESET);
    212  1.14.20.1      yamt 		out32rb(sc->sc_fcr, in32rb(sc->sc_fcr) | FCR1_EIDE0_ENABLE);
    213  1.14.20.1      yamt 		delay(50000);
    214  1.14.20.1      yamt 
    215  1.14.20.1      yamt 		out32rb(sc->sc_addr, in32rb(sc->sc_addr) | MBCR_MEDIABAY0_ENABLE);
    216  1.14.20.1      yamt 		__asm volatile ("eieio");
    217  1.14.20.1      yamt 		delay(50000);
    218  1.14.20.1      yamt 
    219  1.14.20.1      yamt 		out32rb(sc->sc_addr, in32rb(sc->sc_addr) & ~0xf);
    220  1.14.20.1      yamt 		__asm volatile ("eieio");
    221  1.14.20.1      yamt 		delay(50000);
    222  1.14.20.1      yamt 
    223  1.14.20.1      yamt 		tsleep(sc, PRI_NONE, "mediabay", hz*1);
    224  1.14.20.1      yamt 
    225  1.14.20.1      yamt 		printf(" done.\n");
    226       1.12  macallan 	}
    227        1.1    tsubai 
    228        1.1    tsubai 	for (child = OF_child(sc->sc_node); child; child = OF_peer(child)) {
    229        1.4       wiz 		memset(name, 0, sizeof(name));
    230        1.1    tsubai 		if (OF_getprop(child, "name", name, sizeof(name)) == -1)
    231        1.1    tsubai 			continue;
    232        1.1    tsubai 		ca.ca_name = name;
    233        1.1    tsubai 		ca.ca_node = child;
    234        1.2    tsubai 		ca.ca_baseaddr = sc->sc_baseaddr;
    235       1.14   garbled 		ca.ca_tag = sc->sc_tag;
    236        1.1    tsubai 
    237        1.1    tsubai 		ca.ca_nreg  = OF_getprop(child, "reg", reg, sizeof(reg));
    238        1.1    tsubai 		ca.ca_nintr = OF_getprop(child, "AAPL,interrupts", intr,
    239        1.1    tsubai 				sizeof(intr));
    240        1.1    tsubai 		if (ca.ca_nintr == -1)
    241        1.1    tsubai 			ca.ca_nintr = OF_getprop(child, "interrupts", intr,
    242        1.1    tsubai 					sizeof(intr));
    243        1.1    tsubai 		ca.ca_reg = reg;
    244        1.1    tsubai 		ca.ca_intr = intr;
    245        1.1    tsubai 
    246        1.2    tsubai 		content = config_found(&sc->sc_dev, &ca, mediabay_print);
    247        1.2    tsubai 		if (content) {
    248        1.2    tsubai 			sc->sc_content = content;
    249        1.2    tsubai 			return;
    250        1.2    tsubai 		}
    251        1.1    tsubai 	}
    252        1.2    tsubai 
    253  1.14.20.1      yamt 	if (sc->sc_type != MB_CONTROLLER_KEYLARGO) {
    254  1.14.20.1      yamt 		/* No devices found.  Disable media-bay. */
    255  1.14.20.1      yamt 		fcr &= ~(FCR_MEDIABAY_ENABLE | FCR_MEDIABAY_IDE_ENABLE |
    256  1.14.20.1      yamt 			 FCR_MEDIABAY_CD_POWER | FCR_MEDIABAY_FD_ENABLE);
    257  1.14.20.1      yamt 		out32rb(sc->sc_fcr, fcr);
    258  1.14.20.1      yamt 	}
    259        1.1    tsubai }
    260        1.1    tsubai 
    261        1.1    tsubai int
    262  1.14.20.1      yamt mediabay_print(void *aux, const char *mediabay)
    263        1.1    tsubai {
    264        1.1    tsubai 	struct confargs *ca = aux;
    265        1.1    tsubai 
    266        1.2    tsubai 	if (mediabay == NULL && ca->ca_nreg > 0)
    267        1.7   thorpej 		aprint_normal(" offset 0x%x", ca->ca_reg[0]);
    268        1.1    tsubai 
    269        1.2    tsubai 	return QUIET;
    270        1.1    tsubai }
    271        1.1    tsubai 
    272        1.1    tsubai int
    273  1.14.20.1      yamt mediabay_intr(void *v)
    274        1.1    tsubai {
    275        1.1    tsubai 	struct mediabay_softc *sc = v;
    276        1.1    tsubai 
    277        1.2    tsubai 	wakeup(&sc->sc_kthread);
    278       1.10    briggs 
    279        1.2    tsubai 	return 1;
    280        1.2    tsubai }
    281        1.2    tsubai 
    282        1.2    tsubai void
    283  1.14.20.1      yamt mediabay_kthread(void *v)
    284        1.2    tsubai {
    285        1.2    tsubai 	struct mediabay_softc *sc = v;
    286        1.2    tsubai 	u_int x, fcr;
    287        1.2    tsubai 
    288       1.10    briggs 	for (;;) {
    289       1.10    briggs 		tsleep(&sc->sc_kthread, PRIBIO, "mbayev", 0);
    290        1.2    tsubai 
    291       1.10    briggs 		/* sleep 0.25 sec */
    292       1.10    briggs 		tsleep(mediabay_kthread, PRIBIO, "mbayev", hz/4);
    293        1.2    tsubai 
    294       1.10    briggs 		DPRINTF("%s: ", sc->sc_dev.dv_xname);
    295       1.10    briggs 		x = in32rb(sc->sc_addr);
    296        1.1    tsubai 
    297  1.14.20.1      yamt 		switch (MEDIABAY_ID(sc, x)) {
    298       1.10    briggs 		case MEDIABAY_ID_NONE:
    299       1.10    briggs 			DPRINTF("removed\n");
    300       1.10    briggs 			if (sc->sc_content != NULL) {
    301       1.10    briggs 				config_detach(sc->sc_content, DETACH_FORCE);
    302       1.10    briggs 				DPRINTF("%s: detach done\n",
    303       1.10    briggs 					sc->sc_dev.dv_xname);
    304       1.10    briggs 				sc->sc_content = NULL;
    305       1.10    briggs 
    306  1.14.20.1      yamt 				if (sc->sc_type != MB_CONTROLLER_KEYLARGO) {
    307  1.14.20.1      yamt 					/* disable media-bay */
    308  1.14.20.1      yamt 					fcr = in32rb(sc->sc_fcr);
    309  1.14.20.1      yamt 					fcr &= ~(FCR_MEDIABAY_ENABLE |
    310  1.14.20.1      yamt 						 FCR_MEDIABAY_IDE_ENABLE |
    311  1.14.20.1      yamt 						 FCR_MEDIABAY_CD_POWER |
    312  1.14.20.1      yamt 						 FCR_MEDIABAY_FD_ENABLE);
    313  1.14.20.1      yamt 					out32rb(sc->sc_fcr, fcr);
    314  1.14.20.1      yamt 				}
    315       1.10    briggs 			}
    316       1.10    briggs 			break;
    317       1.10    briggs 		case MEDIABAY_ID_FD:
    318       1.10    briggs 			DPRINTF("FD inserted\n");
    319       1.10    briggs 			break;
    320       1.10    briggs 		case MEDIABAY_ID_CD:
    321       1.10    briggs 			DPRINTF("CD inserted\n");
    322       1.10    briggs 
    323       1.10    briggs 			if (sc->sc_content == NULL)
    324       1.10    briggs 				mediabay_attach_content(sc);
    325       1.10    briggs 			break;
    326       1.10    briggs 		default:
    327       1.10    briggs 			printf("unknown event (0x%x)\n", x);
    328        1.2    tsubai 		}
    329        1.1    tsubai 	}
    330        1.1    tsubai }
    331        1.1    tsubai 
    332        1.1    tsubai /* PBG3: 0x7025X0c0 */
    333       1.10    briggs /* 3400: 0x7070X080 */
    334        1.1    tsubai /* 2400: 0x0070X0a8 */
    335