1 /* $NetBSD: sa11xx_pcic.c,v 1.12 2008/06/03 13:45:22 rafal Exp $ */
4 * Copyright (c) 2001 IWAMOTO Toshihiro. All rights reserved.
5 * Copyright (c) 1997 Marc Horowitz. All rights reserved.
7 * Redistribution and use in source and binary forms, with or without
8 * modification, are permitted provided that the following conditions
10 * 1. Redistributions of source code must retain the above copyright
11 * notice, this list of conditions and the following disclaimer.
12 * 2. Redistributions in binary form must reproduce the above copyright
13 * notice, this list of conditions and the following disclaimer in the
14 * documentation and/or other materials provided with the distribution.
15 * 3. All advertising materials mentioning features or use of this software
16 * must display the following acknowledgement:
17 * This product includes software developed by Marc Horowitz.
18 * 4. The name of the author may not be used to endorse or promote products
19 * derived from this software without specific prior written permission.
21 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
22 * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
23 * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
24 * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
25 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
26 * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
27 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
28 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
29 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
30 * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
34 * Common code for SA-11x0 based PCMCIA modules.
37 #include <sys/cdefs.h>
38 __KERNEL_RCSID(0, "$NetBSD: sa11xx_pcic.c,v 1.12 2008/06/03 13:45:22 rafal Exp $");
40 #include <sys/types.h>
41 #include <sys/param.h>
42 #include <sys/systm.h>
43 #include <sys/device.h>
44 #include <sys/callout.h>
45 #include <sys/kernel.h>
46 #include <sys/kthread.h>
47 #include <sys/malloc.h>
53 #include <dev/pcmcia/pcmciareg.h>
54 #include <dev/pcmcia/pcmciavar.h>
55 #include <dev/pcmcia/pcmciachip.h>
57 #include <arm/sa11x0/sa11x0_reg.h>
58 #include <arm/sa11x0/sa11x0_var.h>
59 #include <arm/sa11x0/sa11xx_pcicreg.h>
60 #include <arm/sa11x0/sa11xx_pcicvar.h>
62 static int sapcic_mem_alloc(pcmcia_chipset_handle_t
, bus_size_t
,
63 struct pcmcia_mem_handle
*);
64 static void sapcic_mem_free(pcmcia_chipset_handle_t
,
65 struct pcmcia_mem_handle
*);
66 static int sapcic_mem_map(pcmcia_chipset_handle_t
, int, bus_addr_t
,
67 bus_size_t
, struct pcmcia_mem_handle
*,
69 static void sapcic_mem_unmap(pcmcia_chipset_handle_t
, int);
70 static int sapcic_io_alloc(pcmcia_chipset_handle_t
, bus_addr_t
,
71 bus_size_t
, bus_size_t
,
72 struct pcmcia_io_handle
*);
73 static void sapcic_io_free(pcmcia_chipset_handle_t
,
74 struct pcmcia_io_handle
*);
75 static int sapcic_io_map(pcmcia_chipset_handle_t
, int,
76 bus_addr_t
, bus_size_t
,
77 struct pcmcia_io_handle
*, int *);
78 static void sapcic_io_unmap(pcmcia_chipset_handle_t
, int);
79 static void *sapcic_intr_establish(pcmcia_chipset_handle_t
,
80 struct pcmcia_function
*, int,
81 int (*)(void *), void *);
82 static void sapcic_intr_disestablish(pcmcia_chipset_handle_t
,
84 static void sapcic_socket_enable(pcmcia_chipset_handle_t
);
85 static void sapcic_socket_disable(pcmcia_chipset_handle_t
);
86 static void sapcic_socket_settype(pcmcia_chipset_handle_t
, int);
88 static void sapcic_event_thread(void *);
90 static void sapcic_delay(int, const char *);
93 #define DPRINTF(arg) aprint_normal arg
98 struct pcmcia_chip_functions sa11x0_pcmcia_functions
= {
109 sapcic_intr_establish
,
110 sapcic_intr_disestablish
,
112 sapcic_socket_enable
,
113 sapcic_socket_disable
,
114 sapcic_socket_settype
,
119 sapcic_kthread_create(void *arg
)
121 struct sapcic_socket
*so
= arg
;
123 /* XXX attach card if already present */
125 so
->laststatus
= (so
->pcictag
->read
)(so
, SAPCIC_STATUS_CARD
);
126 if (so
->laststatus
== SAPCIC_CARD_VALID
) {
127 aprint_normal_dev(so
->sc
->sc_dev
, "card present\n");
128 pcmcia_card_attach(so
->pcmcia
);
131 if (kthread_create(PRI_NONE
, 0, NULL
, sapcic_event_thread
, so
,
132 &so
->event_thread
, "%s,%d", device_xname(so
->sc
->sc_dev
), so
->socket
)) {
133 aprint_normal_dev(so
->sc
->sc_dev
, "unable to create event thread for socket %d\n",
135 panic("sapcic_kthread_create");
140 sapcic_event_thread(void *arg
)
142 struct sapcic_socket
*so
= arg
;
145 while (so
->shutdown
== 0) {
147 * Serialize event processing on the PCIC. We may
148 * sleep while we hold this lock.
150 mutex_enter(&so
->sc
->sc_lock
);
152 /* sleep .25s to be enqueued chatterling interrupts */
153 (void) tsleep(sapcic_event_thread
, PWAIT
, "pcicss", hz
/ 4);
158 /* we don't rely on interrupt type */
159 newstatus
= (so
->pcictag
->read
)(so
, SAPCIC_STATUS_CARD
);
162 if (so
->laststatus
== newstatus
) {
164 * No events to process; release the PCIC lock.
166 mutex_exit(&so
->sc
->sc_lock
);
167 (void) tsleep(&so
->event
, PWAIT
, "pcicev", hz
);
171 so
->laststatus
= newstatus
;
173 case SAPCIC_CARD_VALID
:
174 aprint_normal_dev(so
->sc
->sc_dev
, "insertion event\n");
176 pcmcia_card_attach(so
->pcmcia
);
179 case SAPCIC_CARD_INVALID
:
180 aprint_normal_dev(so
->sc
->sc_dev
, "removal event\n");
182 pcmcia_card_detach(so
->pcmcia
, DETACH_FORCE
);
186 panic("sapcic_event_thread: unknown status %d",
190 mutex_exit(&so
->sc
->sc_lock
);
193 so
->event_thread
= NULL
;
195 /* In case parent is waiting for us to exit. */
202 sapcic_delay(int timo
, const char *wmesg
)
206 panic("sapcic_delay: called in interrupt context");
209 tsleep(sapcic_delay
, PWAIT
, wmesg
, roundup(timo
* hz
, 1000) / 1000);
213 sapcic_intr(void *arg
)
215 struct sapcic_socket
*so
= arg
;
218 (so
->pcictag
->clear_intr
)(so
->socket
);
224 sapcic_mem_alloc(pcmcia_chipset_handle_t pch
, bus_size_t size
,
225 struct pcmcia_mem_handle
*pmh
)
227 struct sapcic_socket
*so
= pch
;
229 /* All we need is bus space tag */
230 memset(pmh
, 0, sizeof(*pmh
));
231 pmh
->memt
= so
->sc
->sc_iot
;
237 sapcic_mem_free(pcmcia_chipset_handle_t pch
, struct pcmcia_mem_handle
*pmh
)
242 sapcic_mem_map(pcmcia_chipset_handle_t pch
, int kind
, bus_addr_t card_addr
,
243 bus_size_t size
, struct pcmcia_mem_handle
*pmh
, bus_addr_t
*offsetp
,
246 struct sapcic_socket
*so
= pch
;
250 pa
= trunc_page(card_addr
);
251 *offsetp
= card_addr
- pa
;
252 size
= round_page(card_addr
+ size
) - pa
;
253 pmh
->realsize
= size
;
255 pa
+= SAPCIC_BASE_OFFSET
;
256 pa
+= SAPCIC_SOCKET_OFFSET
* so
->socket
;
258 switch (kind
& ~PCMCIA_WIDTH_MEM_MASK
) {
259 case PCMCIA_MEM_ATTR
:
260 pa
+= SAPCIC_ATTR_OFFSET
;
262 case PCMCIA_MEM_COMMON
:
263 pa
+= SAPCIC_COMMON_OFFSET
;
266 panic("sapcic_mem_map: bogus kind");
269 error
= bus_space_map(so
->sc
->sc_iot
, pa
, size
, 0, &pmh
->memh
);
271 *windowp
= (int)pmh
->memh
;
276 sapcic_mem_unmap(pcmcia_chipset_handle_t pch
, int window
)
278 struct sapcic_socket
*so
= pch
;
280 bus_space_unmap(so
->sc
->sc_iot
, (bus_addr_t
)window
, 4096); /* XXX */
284 sapcic_io_alloc(pcmcia_chipset_handle_t pch
, bus_addr_t start
,
285 bus_size_t size
, bus_size_t align
, struct pcmcia_io_handle
*pih
)
287 struct sapcic_socket
*so
= pch
;
291 memset(pih
, 0, sizeof(*pih
));
292 pih
->iot
= so
->sc
->sc_iot
;
297 pa
+= SAPCIC_BASE_OFFSET
;
298 pa
+= SAPCIC_SOCKET_OFFSET
* so
->socket
;
300 DPRINTF(("sapcic_io_alloc: %x %x\n", (unsigned int)pa
,
301 (unsigned int)size
));
302 /* XXX Are we ignoring alignment constraints? */
303 error
= bus_space_map(so
->sc
->sc_iot
, pa
, size
, 0, &pih
->ioh
);
309 sapcic_io_free(pcmcia_chipset_handle_t pch
, struct pcmcia_io_handle
*pih
)
311 struct sapcic_socket
*so
= pch
;
313 bus_space_unmap(so
->sc
->sc_iot
, pih
->ioh
, pih
->size
);
317 sapcic_io_map(pcmcia_chipset_handle_t pch
, int width
, bus_addr_t offset
,
318 bus_size_t size
, struct pcmcia_io_handle
*pih
, int *windowp
)
324 static void sapcic_io_unmap(pcmcia_chipset_handle_t pch
, int window
)
329 sapcic_intr_establish(pcmcia_chipset_handle_t pch
, struct pcmcia_function
*pf
,
330 int ipl
, int (*fct
)(void *), void *arg
)
332 struct sapcic_socket
*so
= pch
;
334 /* XXX need to check if something should be done here */
336 return (so
->pcictag
->intr_establish
)(so
, ipl
, fct
, arg
);
340 sapcic_intr_disestablish(pcmcia_chipset_handle_t pch
, void *ih
)
342 struct sapcic_socket
*so
= pch
;
344 (so
->pcictag
->intr_disestablish
)(so
, ih
);
348 sapcic_socket_enable(pcmcia_chipset_handle_t pch
)
350 struct sapcic_socket
*so
= pch
;
353 #if defined(DIAGNOSTIC) && defined(notyet)
354 if (so
->flags
& PCIC_FLAG_ENABLED
)
355 aprint_normal("sapcic_socket_enable: enabling twice\n");
358 /* disable interrupts */
360 /* power down the socket to reset it, clear the card reset pin */
361 (so
->pcictag
->set_power
)(so
, SAPCIC_POWER_OFF
);
364 * wait 300ms until power fails (Tpf). Then, wait 100ms since
365 * we are changing Vcc (Toff).
367 sapcic_delay(300 + 100, "pccen0");
369 /* power up the socket */
370 /* XXX voltage selection should be done in PCMCIA code */
371 if (so
->power_capability
& SAPCIC_POWER_5V
) {
372 (so
->pcictag
->set_power
)(so
, SAPCIC_POWER_5V
);
373 (so
->pcictag
->write
)(so
, SAPCIC_CONTROL_POWERSELECT
,
376 (so
->pcictag
->set_power
)(so
, SAPCIC_POWER_3V
);
377 (so
->pcictag
->write
)(so
, SAPCIC_CONTROL_POWERSELECT
,
381 /* enable PCMCIA control lines */
382 (so
->pcictag
->write
)(so
, SAPCIC_CONTROL_LINEENABLE
, 1);
385 * wait 100ms until power raise (Tpr) and 20ms to become
388 * some machines require some more time to be settled
389 * (300ms is added here).
391 sapcic_delay(100 + 20 + 300, "pccen1");
393 /* honor nWAIT signal */
394 (so
->pcictag
->write
)(so
, SAPCIC_CONTROL_WAITENABLE
, 1);
395 /* now make sure we have reset# active */
396 (so
->pcictag
->write
)(so
, SAPCIC_CONTROL_RESET
, 1);
399 * hold RESET at least 10us, this is a min allow for slop in
404 /* clear the reset flag */
405 (so
->pcictag
->write
)(so
, SAPCIC_CONTROL_RESET
, 0);
407 /* wait 20ms as per pc card standard (r2.01) section 4.3.6 */
408 sapcic_delay(20, "pccen2");
410 /* wait for the chip to finish initializing */
411 sapcic_delay(10, "pccen3");
412 for (i
= 100; i
; i
--) {
413 if ((so
->pcictag
->read
)(so
, SAPCIC_STATUS_READY
))
415 sapcic_delay(100, "pccen4");
417 DPRINTF(("sapcic_socket_enable: wait ready %d\n", 100 - i
));
419 /* finally enable the interrupt */
424 sapcic_socket_disable(pcmcia_chipset_handle_t pch
)
426 struct sapcic_socket
*so
= pch
;
428 /* XXX mask card interrupts */
430 /* power down the card */
431 (so
->pcictag
->set_power
)(so
, SAPCIC_POWER_OFF
);
433 /* float controller lines */
434 (so
->pcictag
->write
)(so
, SAPCIC_CONTROL_LINEENABLE
, 0);
438 sapcic_socket_settype(pcmcia_chipset_handle_t pch
, int type
)
441 /* XXX nothing to do */