xref: /netbsd-src/sys/arch/hpcarm/dev/ipaq_pcic.c (revision 404fbe5fb94ca1e054339640cabb2801ce52dd30)
1 /*      $NetBSD: ipaq_pcic.c,v 1.18 2008/04/28 20:23:21 martin Exp $        */
2 
3 /*-
4  * Copyright (c) 2001 The NetBSD Foundation, Inc.
5  * All rights reserved.
6  *
7  * This code is derived from software contributed to The NetBSD Foundation
8  * by Ichiro FUKUHARA (ichiro@ichiro.org).
9  *
10  * Redistribution and use in source and binary forms, with or without
11  * modification, are permitted provided that the following conditions
12  * are met:
13  * 1. Redistributions of source code must retain the above copyright
14  *    notice, this list of conditions and the following disclaimer.
15  * 2. Redistributions in binary form must reproduce the above copyright
16  *    notice, this list of conditions and the following disclaimer in the
17  *    documentation and/or other materials provided with the distribution.
18  *
19  * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
20  * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
21  * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
22  * PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
23  * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
24  * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
25  * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
26  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
27  * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
28  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
29  * POSSIBILITY OF SUCH DAMAGE.
30  */
31 
32 #include <sys/cdefs.h>
33 __KERNEL_RCSID(0, "$NetBSD: ipaq_pcic.c,v 1.18 2008/04/28 20:23:21 martin Exp $");
34 
35 #include <sys/param.h>
36 #include <sys/systm.h>
37 #include <sys/types.h>
38 #include <sys/conf.h>
39 #include <sys/file.h>
40 #include <sys/device.h>
41 #include <sys/kernel.h>
42 #include <sys/kthread.h>
43 #include <sys/malloc.h>
44 
45 #include <machine/bus.h>
46 #include <dev/pcmcia/pcmciachip.h>
47 #include <dev/pcmcia/pcmciavar.h>
48 
49 #include <hpcarm/dev/ipaq_saipvar.h>
50 #include <hpcarm/dev/ipaq_pcicreg.h>
51 #include <hpcarm/dev/ipaq_gpioreg.h>
52 
53 #include <arm/sa11x0/sa11x0_gpioreg.h>
54 #include <arm/sa11x0/sa11x0_var.h>
55 #include <arm/sa11x0/sa11xx_pcicvar.h>
56 
57 #include "ipaqpcic.h"
58 
59 static	int	ipaqpcic_match(struct device *, struct cfdata *, void *);
60 static	void	ipaqpcic_attach(struct device *, struct device *, void *);
61 static	int	ipaqpcic_print(void *, const char *);
62 
63 static	int	ipaqpcic_read(struct sapcic_socket *, int);
64 static	void	ipaqpcic_write(struct sapcic_socket *, int, int);
65 static	void	ipaqpcic_set_power(struct sapcic_socket *, int);
66 static	void	ipaqpcic_clear_intr(int);
67 static	void	*ipaqpcic_intr_establish(struct sapcic_socket *, int,
68 				       int (*)(void *), void *);
69 static	void	ipaqpcic_intr_disestablish(struct sapcic_socket *, void *);
70 
71 struct ipaqpcic_softc {
72 	struct sapcic_softc	sc_pc;
73 	bus_space_handle_t	sc_ioh;
74 	struct ipaq_softc	*sc_parent;
75 	struct sapcic_socket	sc_socket[2];
76 };
77 
78 static	void	ipaqpcic_init(struct ipaqpcic_softc *);
79 
80 static struct sapcic_tag ipaqpcic_functions = {
81 	ipaqpcic_read,
82 	ipaqpcic_write,
83 	ipaqpcic_set_power,
84 	ipaqpcic_clear_intr,
85 	ipaqpcic_intr_establish,
86 	ipaqpcic_intr_disestablish
87 };
88 
89 CFATTACH_DECL(ipaqpcic, sizeof(struct ipaqpcic_softc),
90     ipaqpcic_match, ipaqpcic_attach, NULL, NULL);
91 
92 static int
93 ipaqpcic_match(struct device *parent, struct cfdata *cf, void *aux)
94 {
95 	return (1);
96 }
97 
98 static void
99 ipaqpcic_attach(struct device *parent, struct device *self, void *aux)
100 {
101 	int i;
102 	struct pcmciabus_attach_args paa;
103 	struct ipaqpcic_softc *sc = (struct ipaqpcic_softc *)self;
104 	struct ipaq_softc *psc = (struct ipaq_softc *)parent;
105 
106 	printf("\n");
107 
108 	sc->sc_pc.sc_iot = psc->sc_iot;
109 	sc->sc_ioh = psc->sc_ioh;
110 	sc->sc_parent = (struct ipaq_softc *)parent;
111 
112 	ipaqpcic_init(sc);
113 
114 	for(i = 0; i < 2; i++) {
115 		sc->sc_socket[i].sc = (struct sapcic_softc *)sc;
116 		sc->sc_socket[i].socket = i;
117 		sc->sc_socket[i].pcictag_cookie = psc;
118 		sc->sc_socket[i].pcictag = &ipaqpcic_functions;
119 		sc->sc_socket[i].event_thread = NULL;
120 		sc->sc_socket[i].event = 0;
121 		sc->sc_socket[i].laststatus = SAPCIC_CARD_INVALID;
122 		sc->sc_socket[i].shutdown = 0;
123 
124 		paa.paa_busname = "pcmcia";
125 		paa.pct = (pcmcia_chipset_tag_t)&sa11x0_pcmcia_functions;
126 		paa.pch = (pcmcia_chipset_handle_t)&sc->sc_socket[i];
127 		paa.iobase = 0;
128 		paa.iosize = 0x4000000;
129 
130 		sc->sc_socket[i].pcmcia =
131 		    config_found_ia(&sc->sc_pc.sc_dev, "pcmciabus",
132 		    &paa, ipaqpcic_print);
133 
134 		sa11x0_intr_establish((sa11x0_chipset_tag_t)psc,
135 			    i ? IRQ_CD1 : IRQ_CD0,
136 			    1, IPL_BIO, sapcic_intr,
137 			    &sc->sc_socket[i]);
138 
139 		/* schedule kthread creation */
140 		sapcic_kthread_create(&sc->sc_socket[i]);
141 
142 #if 0 /* XXX */
143 		/* establish_intr should be after creating the kthread */
144 		config_interrupt(&sc->sc_socket[i], ipaqpcic_config_intr);
145 #endif
146 	}
147 }
148 
149 static int
150 ipaqpcic_print(void *aux, const char *name)
151 {
152 	return (UNCONF);
153 }
154 
155 static void
156 ipaqpcic_init(struct ipaqpcic_softc *sc)
157 {
158 	int cr;
159 
160 	/* All those are inputs */
161 	cr = bus_space_read_4(sc->sc_pc.sc_iot, sc->sc_parent->sc_gpioh, SAGPIO_PDR);
162 	cr &= ~(GPIO_H3600_PCMCIA_CD0 | GPIO_H3600_PCMCIA_CD1 | GPIO_H3600_PCMCIA_IRQ0 |
163 		GPIO_H3600_PCMCIA_IRQ1);
164 	bus_space_write_4(sc->sc_pc.sc_iot, sc->sc_parent->sc_gpioh, SAGPIO_PDR, cr);
165 
166 	sc->sc_parent->ipaq_egpio |=
167 		EGPIO_H3600_OPT_NVRAM_ON | EGPIO_H3600_OPT_ON ;
168 	sc->sc_parent->ipaq_egpio &=
169 		~(EGPIO_H3600_CARD_RESET | EGPIO_H3600_OPT_RESET);
170 	bus_space_write_2(sc->sc_pc.sc_iot, sc->sc_parent->sc_egpioh,
171 			  0, sc->sc_parent->ipaq_egpio);
172 }
173 
174 static int
175 ipaqpcic_read(struct sapcic_socket *so, int reg)
176 {
177 	int cr, bit;
178 	struct ipaqpcic_softc *sc = (struct ipaqpcic_softc *)so->sc;
179 
180 	cr = bus_space_read_4(sc->sc_pc.sc_iot, sc->sc_parent->sc_gpioh, SAGPIO_PLR);
181 
182 	switch (reg) {
183 	case SAPCIC_STATUS_CARD:
184 		bit = (so->socket ? GPIO_H3600_PCMCIA_CD0 :
185 				GPIO_H3600_PCMCIA_CD1) & cr;
186 		if (!bit)
187 			return SAPCIC_CARD_INVALID;
188 		else
189 			return SAPCIC_CARD_VALID;
190 	case SAPCIC_STATUS_VS1:
191         case SAPCIC_STATUS_VS2:
192 	case SAPCIC_STATUS_READY:
193 		bit = (so->socket ? GPIO_H3600_PCMCIA_IRQ0:
194 				GPIO_H3600_PCMCIA_IRQ1);
195 		return (bit & cr);
196 	default:
197 		panic("ipaqpcic_read: bogus register");
198 	}
199 }
200 
201 static void
202 ipaqpcic_write(struct sapcic_socket *so, int reg, int arg)
203 {
204 	int s;
205 	struct ipaqpcic_softc *sc = (struct ipaqpcic_softc *)so->sc;
206 
207 	s = splhigh();
208 	switch (reg) {
209 	case SAPCIC_CONTROL_RESET:
210 		sc->sc_parent->ipaq_egpio |= EGPIO_H3600_CARD_RESET;
211 		break;
212 	case SAPCIC_CONTROL_LINEENABLE:
213 	case SAPCIC_CONTROL_WAITENABLE:
214 	case SAPCIC_CONTROL_POWERSELECT:
215 		break;
216 
217 	default:
218 		splx(s);
219 		panic("ipaqpcic_write: bogus register");
220 	}
221 	bus_space_write_2(sc->sc_pc.sc_iot, sc->sc_parent->sc_egpioh, 0,
222 			  sc->sc_parent->ipaq_egpio);
223 	splx(s);
224 }
225 
226 static void
227 ipaqpcic_set_power(struct sapcic_socket *so, int arg)
228 {
229 	int s;
230 	struct ipaqpcic_softc *sc = (struct ipaqpcic_softc *)so->sc;
231 
232 	s = splbio();
233 	switch (arg) {
234 	case SAPCIC_POWER_OFF:
235 		sc->sc_parent->ipaq_egpio &=
236 			~(EGPIO_H3600_OPT_NVRAM_ON | EGPIO_H3600_OPT_ON);
237 		break;
238 	case SAPCIC_POWER_3V:
239 	case SAPCIC_POWER_5V:
240 		sc->sc_parent->ipaq_egpio |=
241 			EGPIO_H3600_OPT_NVRAM_ON | EGPIO_H3600_OPT_ON;
242 		break;
243 	default:
244 		panic("ipaqpcic_set_power: bogus arg");
245 	}
246 	bus_space_write_2(sc->sc_pc.sc_iot, sc->sc_parent->sc_egpioh,
247 			  0, sc->sc_parent->ipaq_egpio);
248 	splx(s);
249 }
250 
251 static void
252 ipaqpcic_clear_intr(int arg)
253 {
254 }
255 
256 static void *
257 ipaqpcic_intr_establish(struct sapcic_socket *so, int level,
258 			int (*ih_fun)(void *), void *ih_arg)
259 {
260 	int irq;
261 
262 	irq = so->socket ? IRQ_IRQ0 : IRQ_IRQ1;
263 	return (sa11x0_intr_establish((sa11x0_chipset_tag_t)so->pcictag_cookie,
264 				    irq-16, 1, level, ih_fun, ih_arg));
265 }
266 
267 static void
268 ipaqpcic_intr_disestablish(struct sapcic_socket *so, void *ih)
269 {
270 	sa11x0_intr_disestablish((sa11x0_chipset_tag_t)so->pcictag_cookie, ih);
271 }
272