imx31lk_pcic.c revision 1.3 1 1.3 perry /* $Id: imx31lk_pcic.c,v 1.3 2008/06/30 00:49:31 perry Exp $ */
2 1.3 perry /* $NetBSD: imx31lk_pcic.c,v 1.3 2008/06/30 00:49:31 perry Exp $ */
3 1.2 matt /* $OpenBSD: pxapcic.c,v 1.1 2005/07/01 23:51:55 uwe Exp $ */
4 1.2 matt
5 1.2 matt /*
6 1.2 matt * Copyright (c) 2005 Uwe Stuehler <uwe (at) bsdx.de>
7 1.2 matt *
8 1.2 matt * Permission to use, copy, modify, and distribute this software for any
9 1.2 matt * purpose with or without fee is hereby granted, provided that the above
10 1.2 matt * copyright notice and this permission notice appear in all copies.
11 1.2 matt *
12 1.2 matt * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
13 1.2 matt * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
14 1.2 matt * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
15 1.2 matt * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
16 1.2 matt * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
17 1.2 matt * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
18 1.2 matt * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
19 1.2 matt */
20 1.2 matt
21 1.2 matt #include <sys/cdefs.h>
22 1.3 perry __KERNEL_RCSID(0, "$Id: imx31lk_pcic.c,v 1.3 2008/06/30 00:49:31 perry Exp $");
23 1.2 matt
24 1.2 matt #include <sys/param.h>
25 1.2 matt #include <sys/systm.h>
26 1.2 matt #include <sys/device.h>
27 1.2 matt
28 1.2 matt #include <machine/intr.h>
29 1.2 matt #include <machine/bus.h>
30 1.2 matt
31 1.2 matt #include <uvm/uvm.h>
32 1.2 matt
33 1.2 matt #ifdef NOTYET
34 1.2 matt #include <arch/arm/imx/imx_emifs.h>
35 1.2 matt #include <arch/arm/imx/imx_gpio.h>
36 1.2 matt #endif
37 1.2 matt #include <arch/arm/imx/imx31var.h>
38 1.2 matt #include <arch/arm/imx/imx_pcic.h>
39 1.2 matt
40 1.2 matt
41 1.2 matt static int imx31lk_pcic_match(struct device *, struct cfdata *, void *);
42 1.2 matt static void imx31lk_pcic_attach(struct device *, struct device *, void *);
43 1.2 matt
44 1.2 matt CFATTACH_DECL(imx31lk_pcic, sizeof(struct imx_pcic_softc),
45 1.2 matt imx31lk_pcic_match, imx31lk_pcic_attach, NULL, NULL);
46 1.2 matt
47 1.2 matt static void imx31lk_pcic_socket_setup(struct imx_pcic_socket *);
48 1.2 matt static u_int imx31lk_pcic_read(struct imx_pcic_socket *, int);
49 1.2 matt static void imx31lk_pcic_write(struct imx_pcic_socket *, int, u_int);
50 1.2 matt static void imx31lk_pcic_set_power(struct imx_pcic_socket *, int);
51 1.2 matt static void imx31lk_pcic_clear_intr(struct imx_pcic_socket *);
52 1.2 matt static void *imx31lk_pcic_intr_establish(struct imx_pcic_socket *, int,
53 1.2 matt int (*)(void *), void *);
54 1.2 matt static void imx31lk_pcic_intr_disestablish(struct imx_pcic_socket *, void *);
55 1.2 matt
56 1.2 matt struct imx_pcic_tag imx31lk_pcic_functions = {
57 1.2 matt imx31lk_pcic_read,
58 1.2 matt imx31lk_pcic_write,
59 1.2 matt imx31lk_pcic_set_power,
60 1.2 matt imx31lk_pcic_clear_intr,
61 1.2 matt imx31lk_pcic_intr_establish,
62 1.2 matt imx31lk_pcic_intr_disestablish
63 1.2 matt };
64 1.2 matt
65 1.2 matt static int
66 1.2 matt imx31lk_pcic_match(struct device *parent, struct cfdata *cf, void *aux)
67 1.2 matt {
68 1.2 matt
69 1.2 matt return 1; /* XXX */
70 1.2 matt }
71 1.2 matt
72 1.2 matt static void
73 1.2 matt imx31lk_pcic_attach(struct device *parent, struct device *self, void *aux)
74 1.2 matt {
75 1.2 matt struct imx_pcic_softc *sc = (struct imx_pcic_softc *)self;
76 1.2 matt struct aips_attach_args * const aipsa = aux;
77 1.2 matt
78 1.2 matt printf("\n");
79 1.2 matt printf("imx_iot %p\n", aipsa->aipsa_memt);
80 1.2 matt printf("imx_addr %lx\n", aipsa->aipsa_addr);
81 1.2 matt printf("imx_size %lx\n", aipsa->aipsa_size);
82 1.2 matt printf("imx_intr %d\n", aipsa->aipsa_intr);
83 1.2 matt
84 1.2 matt sc->sc_pa = aipsa->aipsa_addr;
85 1.2 matt sc->sc_iot = aipsa->aipsa_memt;
86 1.2 matt
87 1.2 matt sc->sc_nslots = 1;
88 1.2 matt #ifdef NOTYET
89 1.2 matt sc->sc_irqpin[0] = aa->emifs_intr; /* XXX */
90 1.2 matt sc->sc_irqcfpin[0] = -1; /* XXX */
91 1.2 matt #endif
92 1.2 matt
93 1.2 matt sc->sc_flags |= PPF_REVERSE_ORDER;
94 1.2 matt
95 1.2 matt imx_pcic_attach_common(sc, &imx31lk_pcic_socket_setup);
96 1.2 matt }
97 1.2 matt
98 1.2 matt static void
99 1.2 matt imx31lk_pcic_socket_setup(struct imx_pcic_socket *so)
100 1.2 matt {
101 1.2 matt struct imx_pcic_softc *sc;
102 1.2 matt bus_addr_t pa;
103 1.2 matt bus_size_t size = 0x2000; /* XXX */
104 1.2 matt bus_space_tag_t iot;
105 1.2 matt bus_space_handle_t imx31lkh;
106 1.2 matt int error;
107 1.2 matt
108 1.2 matt sc = so->sc;
109 1.2 matt iot = sc->sc_iot;
110 1.2 matt
111 1.2 matt if (so->socket != 0)
112 1.2 matt panic("%s: CF slot %d not supported", sc->sc_dev.dv_xname, so->socket);
113 1.2 matt
114 1.2 matt pa = sc->sc_pa;
115 1.2 matt
116 1.2 matt error = bus_space_map(iot, trunc_page(pa), round_page(size),
117 1.2 matt 0, &imx31lkh);
118 1.2 matt if (error) {
119 1.2 matt panic("%s: failed to map memory %x for imx31lk",
120 1.2 matt sc->sc_dev.dv_xname, (uint32_t)pa);
121 1.2 matt }
122 1.2 matt imx31lkh += pa - trunc_page(pa);
123 1.2 matt
124 1.2 matt #ifdef NOTYET
125 1.2 matt /* setup */
126 1.2 matt #endif
127 1.2 matt
128 1.2 matt #ifdef NOTYET
129 1.2 matt so->power_capability = PXAPCIC_POWER_3V;
130 1.2 matt if (so->socket == 0)
131 1.2 matt so->power_capability |= PXAPCIC_POWER_5V;
132 1.2 matt #endif
133 1.2 matt
134 1.2 matt so->pcictag_cookie = (void *)imx31lkh;
135 1.2 matt so->pcictag = &imx31lk_pcic_functions;
136 1.2 matt }
137 1.2 matt
138 1.2 matt static u_int
139 1.2 matt imx31lk_pcic_read(struct imx_pcic_socket *so, int reg)
140 1.2 matt {
141 1.2 matt #ifdef NOTYET
142 1.2 matt bus_space_tag_t iot = so->sc->sc_iot;
143 1.2 matt bus_space_handle_t ioh = (bus_space_handle_t)so->pcictag_cookie;
144 1.2 matt uint16_t csr;
145 1.2 matt
146 1.2 matt csr = bus_space_read_2(iot, ioh, SCOOP_CSR);
147 1.2 matt
148 1.2 matt switch (reg) {
149 1.2 matt case PXAPCIC_CARD_STATUS:
150 1.2 matt if (csr & SCP_CSR_MISSING)
151 1.2 matt return (PXAPCIC_CARD_INVALID);
152 1.2 matt else
153 1.2 matt return (PXAPCIC_CARD_VALID);
154 1.2 matt
155 1.2 matt case PXAPCIC_CARD_READY:
156 1.2 matt return ((bus_space_read_2(iot, ioh, SCOOP_CSR) &
157 1.2 matt SCP_CSR_READY) != 0);
158 1.2 matt
159 1.2 matt default:
160 1.2 matt panic("imx31lk_pcic_read: bogus register");
161 1.2 matt }
162 1.2 matt /*NOTREACHED*/
163 1.2 matt #else
164 1.2 matt panic("imx31lk_pcic_read");
165 1.2 matt #endif
166 1.2 matt }
167 1.2 matt
168 1.2 matt static void
169 1.2 matt imx31lk_pcic_write(struct imx_pcic_socket *so, int reg, u_int val)
170 1.2 matt {
171 1.2 matt #ifdef NOTYET
172 1.2 matt bus_space_tag_t iot = so->sc->sc_iot;
173 1.2 matt bus_space_handle_t ioh = (bus_space_handle_t)so->pcictag_cookie;
174 1.2 matt uint16_t newval;
175 1.2 matt int s;
176 1.2 matt
177 1.2 matt s = splhigh();
178 1.2 matt
179 1.2 matt switch (reg) {
180 1.2 matt case PXAPCIC_CARD_POWER:
181 1.2 matt newval = bus_space_read_2(iot, ioh, SCOOP_CPR);
182 1.2 matt newval &= ~(SCP_CPR_PWR | SCP_CPR_3V | SCP_CPR_5V);
183 1.2 matt
184 1.2 matt if (val == PXAPCIC_POWER_3V)
185 1.2 matt newval |= (SCP_CPR_PWR | SCP_CPR_3V);
186 1.2 matt else if (val == PXAPCIC_POWER_5V)
187 1.2 matt newval |= (SCP_CPR_PWR | SCP_CPR_5V);
188 1.2 matt
189 1.2 matt bus_space_write_2(iot, ioh, SCOOP_CPR, newval);
190 1.2 matt break;
191 1.2 matt
192 1.2 matt case PXAPCIC_CARD_RESET:
193 1.2 matt bus_space_write_2(iot, ioh, SCOOP_CCR,
194 1.2 matt val ? SCP_CCR_RESET : 0);
195 1.2 matt break;
196 1.2 matt
197 1.2 matt default:
198 1.2 matt panic("imx31lk_pcic_write: bogus register");
199 1.2 matt }
200 1.2 matt
201 1.2 matt splx(s);
202 1.2 matt #else
203 1.2 matt panic("imx31lk_pcic_write");
204 1.2 matt #endif
205 1.2 matt }
206 1.2 matt
207 1.2 matt static void
208 1.2 matt imx31lk_pcic_set_power(struct imx_pcic_socket *so, int pwr)
209 1.2 matt {
210 1.2 matt #ifdef NOTYET
211 1.2 matt bus_space_tag_t iot = so->sc->sc_iot;
212 1.2 matt bus_space_handle_t ioh = (bus_space_handle_t)so->pcictag_cookie;
213 1.2 matt u_int16_t reg;
214 1.2 matt int s;
215 1.2 matt
216 1.2 matt s = splhigh();
217 1.2 matt
218 1.2 matt switch (pwr) {
219 1.2 matt case PXAPCIC_POWER_OFF:
220 1.2 matt #if 0
221 1.2 matt /* XXX does this disable power to both sockets? */
222 1.2 matt reg = bus_space_read_2(iot, ioh, SCOOP_GPWR);
223 1.2 matt bus_space_write_2(iot, ioh, SCOOP_GPWR,
224 1.2 matt reg & ~(1 << SCOOP0_CF_POWER_C3000));
225 1.2 matt #endif
226 1.2 matt break;
227 1.2 matt
228 1.2 matt case PXAPCIC_POWER_3V:
229 1.2 matt case PXAPCIC_POWER_5V:
230 1.2 matt /* XXX */
231 1.2 matt if (so->socket == 0) {
232 1.2 matt reg = bus_space_read_2(iot, ioh, SCOOP_GPWR);
233 1.2 matt bus_space_write_2(iot, ioh, SCOOP_GPWR,
234 1.2 matt reg | (1 << SCOOP0_CF_POWER_C3000));
235 1.2 matt }
236 1.2 matt break;
237 1.2 matt
238 1.2 matt default:
239 1.2 matt splx(s);
240 1.2 matt panic("imx31lk_pcic_set_power: bogus power state");
241 1.2 matt }
242 1.2 matt
243 1.2 matt splx(s);
244 1.2 matt #else
245 1.2 matt panic("imx31lk_pcic_set_power");
246 1.2 matt #endif
247 1.2 matt }
248 1.2 matt
249 1.2 matt static void
250 1.2 matt imx31lk_pcic_clear_intr(struct imx_pcic_socket *so)
251 1.2 matt {
252 1.2 matt #ifdef NOTYET
253 1.2 matt bus_space_tag_t iot = so->sc->sc_iot;
254 1.2 matt bus_space_handle_t ioh = (bus_space_handle_t)so->pcictag_cookie;
255 1.2 matt
256 1.2 matt bus_space_write_2(iot, ioh, SCOOP_IRM, 0x00ff);
257 1.2 matt bus_space_write_2(iot, ioh, SCOOP_ISR, 0x0000);
258 1.2 matt bus_space_write_2(iot, ioh, SCOOP_IRM, 0x0000);
259 1.2 matt #else
260 1.2 matt panic("imx31lk_pcic_clear_intr");
261 1.2 matt #endif
262 1.2 matt }
263 1.2 matt
264 1.2 matt static void *
265 1.2 matt imx31lk_pcic_intr_establish(struct imx_pcic_socket *so, int ipl,
266 1.2 matt int (*func)(void *), void *arg)
267 1.2 matt {
268 1.2 matt #ifdef NOTYET
269 1.3 perry printf("%s: irqpin %d\n", __func__, so->irqpin);
270 1.2 matt return (imx_gpio_intr_establish(so->irqpin, IST_EDGE_FALLING,
271 1.2 matt ipl, "pcic", func, arg));
272 1.2 matt #else
273 1.2 matt return 0; /* XXX */
274 1.2 matt #endif
275 1.2 matt }
276 1.2 matt
277 1.2 matt static void
278 1.2 matt imx31lk_pcic_intr_disestablish(struct imx_pcic_socket *so, void *ih)
279 1.2 matt {
280 1.2 matt
281 1.2 matt #ifdef NOTYET
282 1.2 matt imx_gpio_intr_disestablish(ih);
283 1.2 matt #endif
284 1.2 matt }
285