sa11x1_pcic.c revision 1.21 1 1.21 dyoung /* $NetBSD: sa11x1_pcic.c,v 1.21 2011/07/01 20:31:39 dyoung Exp $ */
2 1.1 rjs
3 1.1 rjs /*-
4 1.1 rjs * Copyright (c) 2001 The NetBSD Foundation, Inc.
5 1.1 rjs * All rights reserved.
6 1.1 rjs *
7 1.1 rjs * This code is derived from software contributed to The NetBSD Foundation
8 1.1 rjs * by IWAMOTO Toshihiro.
9 1.1 rjs *
10 1.1 rjs * Redistribution and use in source and binary forms, with or without
11 1.1 rjs * modification, are permitted provided that the following conditions
12 1.1 rjs * are met:
13 1.1 rjs * 1. Redistributions of source code must retain the above copyright
14 1.1 rjs * notice, this list of conditions and the following disclaimer.
15 1.1 rjs * 2. Redistributions in binary form must reproduce the above copyright
16 1.1 rjs * notice, this list of conditions and the following disclaimer in the
17 1.1 rjs * documentation and/or other materials provided with the distribution.
18 1.1 rjs *
19 1.1 rjs * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
20 1.1 rjs * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
21 1.1 rjs * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
22 1.1 rjs * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
23 1.1 rjs * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
24 1.1 rjs * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
25 1.1 rjs * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
26 1.1 rjs * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
27 1.1 rjs * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
28 1.1 rjs * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
29 1.1 rjs * POSSIBILITY OF SUCH DAMAGE.
30 1.1 rjs */
31 1.8 lukem
32 1.8 lukem #include <sys/cdefs.h>
33 1.21 dyoung __KERNEL_RCSID(0, "$NetBSD: sa11x1_pcic.c,v 1.21 2011/07/01 20:31:39 dyoung Exp $");
34 1.1 rjs
35 1.1 rjs #include <sys/param.h>
36 1.1 rjs #include <sys/systm.h>
37 1.1 rjs #include <sys/types.h>
38 1.1 rjs #include <sys/conf.h>
39 1.1 rjs #include <sys/file.h>
40 1.1 rjs #include <sys/device.h>
41 1.1 rjs #include <sys/kernel.h>
42 1.1 rjs #include <sys/kthread.h>
43 1.1 rjs #include <sys/malloc.h>
44 1.1 rjs
45 1.21 dyoung #include <sys/bus.h>
46 1.1 rjs
47 1.1 rjs #include <dev/pcmcia/pcmciachip.h>
48 1.1 rjs #include <dev/pcmcia/pcmciavar.h>
49 1.1 rjs #include <arm/sa11x0/sa11x0_reg.h>
50 1.1 rjs #include <arm/sa11x0/sa11x0_var.h>
51 1.1 rjs #include <arm/sa11x0/sa1111_reg.h>
52 1.1 rjs #include <arm/sa11x0/sa1111_var.h>
53 1.1 rjs #include <arm/sa11x0/sa11x1_pcicreg.h>
54 1.1 rjs #include <arm/sa11x0/sa11xx_pcicvar.h>
55 1.9 bsh #include <arm/sa11x0/sa11x1_pcicvar.h>
56 1.1 rjs
57 1.1 rjs #include "sacpcic.h"
58 1.1 rjs
59 1.9 bsh static int sacpcic_print(void *, const char *);
60 1.1 rjs
61 1.17 rafal static void
62 1.20 rjs sacpcic_config_deferred(device_t dev)
63 1.17 rafal {
64 1.20 rjs struct sacpcic_softc *sc = device_private(dev);
65 1.17 rafal struct sapcic_socket *so;
66 1.17 rafal int i;
67 1.17 rafal
68 1.17 rafal for (i = 0; i < 2; i++) {
69 1.17 rafal so = &sc->sc_socket[i];
70 1.17 rafal sapcic_kthread_create(so);
71 1.17 rafal
72 1.17 rafal sacc_intr_establish((sacc_chipset_tag_t)so->pcictag_cookie,
73 1.17 rafal i ? IRQ_S1_CDVALID : IRQ_S0_CDVALID,
74 1.17 rafal IST_EDGE_RAISE, IPL_BIO, sapcic_intr, so);
75 1.17 rafal }
76 1.17 rafal }
77 1.17 rafal
78 1.9 bsh void
79 1.9 bsh sacpcic_attach_common(struct sacc_softc *psc, struct sacpcic_softc *sc,
80 1.9 bsh void *aux, void (* socket_setup_hook)(struct sapcic_socket *))
81 1.1 rjs {
82 1.1 rjs int i;
83 1.1 rjs struct pcmciabus_attach_args paa;
84 1.1 rjs
85 1.20 rjs aprint_normal("\n");
86 1.1 rjs
87 1.1 rjs sc->sc_pc.sc_iot = psc->sc_iot;
88 1.1 rjs sc->sc_ioh = psc->sc_ioh;
89 1.1 rjs
90 1.19 rafal mutex_init(&sc->sc_pc.sc_lock, MUTEX_DEFAULT, IPL_NONE);
91 1.19 rafal
92 1.13 peter for (i = 0; i < 2; i++) {
93 1.1 rjs sc->sc_socket[i].sc = (struct sapcic_softc *)sc;
94 1.1 rjs sc->sc_socket[i].socket = i;
95 1.2 rjs sc->sc_socket[i].pcictag_cookie = psc;
96 1.9 bsh sc->sc_socket[i].pcictag = NULL;
97 1.1 rjs sc->sc_socket[i].event_thread = NULL;
98 1.1 rjs sc->sc_socket[i].event = 0;
99 1.1 rjs sc->sc_socket[i].laststatus = SAPCIC_CARD_INVALID;
100 1.1 rjs sc->sc_socket[i].shutdown = 0;
101 1.1 rjs
102 1.9 bsh socket_setup_hook(&sc->sc_socket[i]);
103 1.1 rjs
104 1.1 rjs paa.paa_busname = "pcmcia";
105 1.1 rjs paa.pct = (pcmcia_chipset_tag_t)&sa11x0_pcmcia_functions;
106 1.1 rjs paa.pch = (pcmcia_chipset_handle_t)&sc->sc_socket[i];
107 1.1 rjs paa.iobase = 0;
108 1.1 rjs paa.iosize = 0x4000000;
109 1.1 rjs
110 1.1 rjs sc->sc_socket[i].pcmcia =
111 1.20 rjs config_found_ia(sc->sc_pc.sc_dev, "pcmciabus", &paa,
112 1.10 drochner sacpcic_print);
113 1.17 rafal }
114 1.1 rjs
115 1.20 rjs config_interrupts(sc->sc_pc.sc_dev, sacpcic_config_deferred);
116 1.1 rjs }
117 1.1 rjs
118 1.9 bsh int
119 1.12 peter sacpcic_print(void *aux, const char *name)
120 1.1 rjs {
121 1.13 peter
122 1.13 peter return UNCONF;
123 1.1 rjs }
124 1.1 rjs
125 1.9 bsh int
126 1.9 bsh sacpcic_read(struct sapcic_socket *so, int reg)
127 1.1 rjs {
128 1.1 rjs int cr, bit;
129 1.1 rjs struct sacpcic_softc *sc = (struct sacpcic_softc *)so->sc;
130 1.1 rjs
131 1.1 rjs cr = bus_space_read_4(sc->sc_pc.sc_iot, sc->sc_ioh, SACPCIC_SR);
132 1.1 rjs
133 1.1 rjs switch (reg) {
134 1.1 rjs case SAPCIC_STATUS_CARD:
135 1.1 rjs bit = (so->socket ? SR_S1_CARDDETECT : SR_S0_CARDDETECT) & cr;
136 1.1 rjs if (bit)
137 1.1 rjs return SAPCIC_CARD_INVALID;
138 1.1 rjs else
139 1.1 rjs return SAPCIC_CARD_VALID;
140 1.1 rjs
141 1.1 rjs case SAPCIC_STATUS_VS1:
142 1.1 rjs bit = (so->socket ? SR_S1_VS1 : SR_S0_VS1);
143 1.1 rjs return (bit & cr);
144 1.1 rjs
145 1.1 rjs case SAPCIC_STATUS_VS2:
146 1.1 rjs bit = (so->socket ? SR_S1_VS2 : SR_S0_VS2);
147 1.1 rjs return (bit & cr);
148 1.1 rjs
149 1.1 rjs case SAPCIC_STATUS_READY:
150 1.1 rjs bit = (so->socket ? SR_S1_READY : SR_S0_READY);
151 1.1 rjs return (bit & cr);
152 1.1 rjs
153 1.1 rjs default:
154 1.5 provos panic("sacpcic_read: bogus register");
155 1.1 rjs }
156 1.1 rjs }
157 1.1 rjs
158 1.9 bsh void
159 1.9 bsh sacpcic_write(struct sapcic_socket *so, int reg, int arg)
160 1.1 rjs {
161 1.1 rjs int s, oldvalue, newvalue, mask;
162 1.1 rjs struct sacpcic_softc *sc = (struct sacpcic_softc *)so->sc;
163 1.1 rjs
164 1.1 rjs s = splhigh();
165 1.1 rjs oldvalue = bus_space_read_4(sc->sc_pc.sc_iot, sc->sc_ioh, SACPCIC_CR);
166 1.1 rjs
167 1.1 rjs switch (reg) {
168 1.1 rjs case SAPCIC_CONTROL_RESET:
169 1.1 rjs mask = so->socket ? CR_S1_RST : CR_S0_RST;
170 1.1 rjs
171 1.1 rjs newvalue = (oldvalue & ~mask) | (arg ? mask : 0);
172 1.1 rjs break;
173 1.1 rjs
174 1.1 rjs case SAPCIC_CONTROL_LINEENABLE:
175 1.1 rjs mask = so->socket ? CR_S1_FLT : CR_S0_FLT;
176 1.1 rjs
177 1.1 rjs newvalue = (oldvalue & ~mask) | (arg ? mask : 0);
178 1.1 rjs break;
179 1.1 rjs
180 1.1 rjs case SAPCIC_CONTROL_WAITENABLE:
181 1.1 rjs mask = so->socket ? CR_S1_PWAITEN : CR_S0_PWAITEN;
182 1.1 rjs
183 1.1 rjs newvalue = (oldvalue & ~mask) | (arg ? mask : 0);
184 1.1 rjs break;
185 1.1 rjs
186 1.1 rjs case SAPCIC_CONTROL_POWERSELECT:
187 1.1 rjs mask = so->socket ? CR_S1_PSE : CR_S0_PSE;
188 1.1 rjs newvalue = oldvalue & ~mask;
189 1.1 rjs
190 1.1 rjs switch (arg) {
191 1.1 rjs case SAPCIC_POWER_3V:
192 1.1 rjs break;
193 1.1 rjs case SAPCIC_POWER_5V:
194 1.1 rjs newvalue |= mask;
195 1.1 rjs break;
196 1.1 rjs default:
197 1.1 rjs splx(s);
198 1.1 rjs panic("sacpcic_write: bogus arg");
199 1.1 rjs }
200 1.1 rjs break;
201 1.1 rjs
202 1.1 rjs default:
203 1.1 rjs splx(s);
204 1.1 rjs panic("sacpcic_write: bogus register");
205 1.1 rjs }
206 1.1 rjs bus_space_write_4(sc->sc_pc.sc_iot, sc->sc_ioh, SACPCIC_CR, newvalue);
207 1.1 rjs splx(s);
208 1.1 rjs }
209 1.1 rjs
210 1.9 bsh void
211 1.12 peter sacpcic_clear_intr(int arg)
212 1.1 rjs {
213 1.1 rjs /* sacc_intr_dispatch takes care of intr status */
214 1.1 rjs }
215 1.1 rjs
216 1.9 bsh void *
217 1.12 peter sacpcic_intr_establish(struct sapcic_socket *so, int level,
218 1.12 peter int (*ih_fun)(void *), void *ih_arg)
219 1.1 rjs {
220 1.1 rjs int irq;
221 1.1 rjs
222 1.1 rjs irq = so->socket ? IRQ_S1_READY : IRQ_S0_READY;
223 1.13 peter return sacc_intr_establish((sacc_chipset_tag_t)so->pcictag_cookie, irq,
224 1.13 peter IST_EDGE_FALL, level, ih_fun, ih_arg);
225 1.1 rjs }
226 1.1 rjs
227 1.9 bsh void
228 1.12 peter sacpcic_intr_disestablish(struct sapcic_socket *so, void *ih)
229 1.1 rjs {
230 1.2 rjs sacc_intr_disestablish((sacc_chipset_tag_t)so->pcictag_cookie, ih);
231 1.1 rjs }
232