mediabay.c revision 1.1 1 1.1 tsubai /* $NetBSD: mediabay.c,v 1.1 1999/07/21 19:20:04 tsubai 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.1 tsubai #include <sys/systm.h>
31 1.1 tsubai #include <sys/device.h>
32 1.1 tsubai
33 1.1 tsubai #include <machine/autoconf.h>
34 1.1 tsubai #include <machine/pio.h>
35 1.1 tsubai
36 1.1 tsubai struct mediabay_softc {
37 1.1 tsubai struct device sc_dev;
38 1.1 tsubai int sc_node;
39 1.1 tsubai u_int *sc_addr;
40 1.1 tsubai u_int *sc_fcr;
41 1.1 tsubai };
42 1.1 tsubai
43 1.1 tsubai static void mediabay_attach __P((struct device *, struct device *, void *));
44 1.1 tsubai static int mediabay_match __P((struct device *, struct cfdata *, void *));
45 1.1 tsubai static int mediabay_print __P((void *, const char *));
46 1.1 tsubai
47 1.1 tsubai struct cfattach mediabay_ca = {
48 1.1 tsubai sizeof(struct mediabay_softc), mediabay_match, mediabay_attach
49 1.1 tsubai };
50 1.1 tsubai
51 1.1 tsubai int mediabay_intr __P((void *));
52 1.1 tsubai
53 1.1 tsubai #define FCR_MEDIABAY_RESET 0x00000002
54 1.1 tsubai #define FCR_MEDIABAY_IDE_ENABLE 0x00000008
55 1.1 tsubai #define FCR_MEDIABAY_FD_ENABLE 0x00000010
56 1.1 tsubai #define FCR_MEDIABAY_ENABLE 0x00000080
57 1.1 tsubai #define FCR_MEDIABAY_CD_POWER 0x00800000
58 1.1 tsubai
59 1.1 tsubai #define MEDIABAY_ID(x) ((x >> 12) & 0xf)
60 1.1 tsubai #define MEDIABAY_ID_FD 0
61 1.1 tsubai #define MEDIABAY_ID_CD 3
62 1.1 tsubai #define MEDIABAY_ID_NONE 7
63 1.1 tsubai
64 1.1 tsubai int
65 1.1 tsubai mediabay_match(parent, cf, aux)
66 1.1 tsubai struct device *parent;
67 1.1 tsubai struct cfdata *cf;
68 1.1 tsubai void *aux;
69 1.1 tsubai {
70 1.1 tsubai struct confargs *ca = aux;
71 1.1 tsubai
72 1.1 tsubai if (strcmp(ca->ca_name, "media-bay") == 0)
73 1.1 tsubai return 1;
74 1.1 tsubai
75 1.1 tsubai return 0;
76 1.1 tsubai }
77 1.1 tsubai
78 1.1 tsubai /*
79 1.1 tsubai * Attach all the sub-devices we can find
80 1.1 tsubai */
81 1.1 tsubai void
82 1.1 tsubai mediabay_attach(parent, self, aux)
83 1.1 tsubai struct device *parent, *self;
84 1.1 tsubai void *aux;
85 1.1 tsubai {
86 1.1 tsubai struct mediabay_softc *sc = (struct mediabay_softc *)self;
87 1.1 tsubai struct confargs ca;
88 1.1 tsubai int irq, child;
89 1.1 tsubai u_int fcr;
90 1.1 tsubai u_int reg[20], intr[5];
91 1.1 tsubai char name[32];
92 1.1 tsubai
93 1.1 tsubai bcopy(aux, &ca, sizeof(ca));
94 1.1 tsubai ca.ca_reg[0] += ca.ca_baseaddr;
95 1.1 tsubai sc->sc_addr = mapiodev(ca.ca_reg[0], NBPG);
96 1.1 tsubai sc->sc_fcr = sc->sc_addr + 1;
97 1.1 tsubai sc->sc_node = ca.ca_node;
98 1.1 tsubai irq = ca.ca_intr[0];
99 1.1 tsubai
100 1.1 tsubai printf(" irq %d\n", irq);
101 1.1 tsubai intr_establish(irq, IST_LEVEL, IPL_BIO, mediabay_intr, sc);
102 1.1 tsubai
103 1.1 tsubai fcr = in32rb(sc->sc_fcr);
104 1.1 tsubai fcr |= FCR_MEDIABAY_ENABLE | FCR_MEDIABAY_RESET;
105 1.1 tsubai out32rb(sc->sc_fcr, fcr);
106 1.1 tsubai delay(50000);
107 1.1 tsubai
108 1.1 tsubai fcr &= ~FCR_MEDIABAY_RESET;
109 1.1 tsubai out32rb(sc->sc_fcr, fcr);
110 1.1 tsubai delay(50000);
111 1.1 tsubai
112 1.1 tsubai fcr |= FCR_MEDIABAY_IDE_ENABLE | FCR_MEDIABAY_CD_POWER;
113 1.1 tsubai out32rb(sc->sc_fcr, fcr);
114 1.1 tsubai delay(50000);
115 1.1 tsubai
116 1.1 tsubai /* XXX */
117 1.1 tsubai if (MEDIABAY_ID(in32rb(sc->sc_addr)) == MEDIABAY_ID_NONE)
118 1.1 tsubai return;
119 1.1 tsubai
120 1.1 tsubai for (child = OF_child(sc->sc_node); child; child = OF_peer(child)) {
121 1.1 tsubai bzero(name, sizeof(name));
122 1.1 tsubai if (OF_getprop(child, "name", name, sizeof(name)) == -1)
123 1.1 tsubai continue;
124 1.1 tsubai ca.ca_name = name;
125 1.1 tsubai ca.ca_node = child;
126 1.1 tsubai
127 1.1 tsubai ca.ca_nreg = OF_getprop(child, "reg", reg, sizeof(reg));
128 1.1 tsubai ca.ca_nintr = OF_getprop(child, "AAPL,interrupts", intr,
129 1.1 tsubai sizeof(intr));
130 1.1 tsubai if (ca.ca_nintr == -1)
131 1.1 tsubai ca.ca_nintr = OF_getprop(child, "interrupts", intr,
132 1.1 tsubai sizeof(intr));
133 1.1 tsubai ca.ca_reg = reg;
134 1.1 tsubai ca.ca_intr = intr;
135 1.1 tsubai
136 1.1 tsubai config_found(self, &ca, mediabay_print);
137 1.1 tsubai }
138 1.1 tsubai }
139 1.1 tsubai
140 1.1 tsubai int
141 1.1 tsubai mediabay_print(aux, mediabay)
142 1.1 tsubai void *aux;
143 1.1 tsubai const char *mediabay;
144 1.1 tsubai {
145 1.1 tsubai struct confargs *ca = aux;
146 1.1 tsubai
147 1.1 tsubai if (mediabay)
148 1.1 tsubai printf("%s at %s", ca->ca_name, mediabay);
149 1.1 tsubai
150 1.1 tsubai if (ca->ca_nreg > 0)
151 1.1 tsubai printf(" offset 0x%x", ca->ca_reg[0]);
152 1.1 tsubai
153 1.1 tsubai return UNCONF;
154 1.1 tsubai }
155 1.1 tsubai
156 1.1 tsubai int
157 1.1 tsubai mediabay_intr(v)
158 1.1 tsubai void *v;
159 1.1 tsubai {
160 1.1 tsubai struct mediabay_softc *sc = v;
161 1.1 tsubai u_int x;
162 1.1 tsubai
163 1.1 tsubai printf("%s: ", sc->sc_dev.dv_xname);
164 1.1 tsubai x = in32rb(sc->sc_addr);
165 1.1 tsubai
166 1.1 tsubai switch (MEDIABAY_ID(x)) {
167 1.1 tsubai case MEDIABAY_ID_NONE:
168 1.1 tsubai printf("removed\n");
169 1.1 tsubai break;
170 1.1 tsubai case MEDIABAY_ID_FD:
171 1.1 tsubai printf("FD inserted\n");
172 1.1 tsubai break;
173 1.1 tsubai case MEDIABAY_ID_CD:
174 1.1 tsubai printf("CD inserted\n");
175 1.1 tsubai break;
176 1.1 tsubai default:
177 1.1 tsubai printf("unknown event (0x%x)\n", x);
178 1.1 tsubai }
179 1.1 tsubai
180 1.1 tsubai return 1;
181 1.1 tsubai }
182 1.1 tsubai
183 1.1 tsubai /* PBG3: 0x7025X0c0 */
184 1.1 tsubai /* 2400: 0x0070X0a8 */
185