1 /* $FreeBSD: src/sys/dev/exca/exca.c,v 1.6 2002/10/07 06:18:50 imp Exp $ */
2 /* $DragonFly: src/sys/dev/pccard/exca/exca.c,v 1.1 2004/02/10 07:55:47 joerg Exp $ */
5 * Copyright (c) 2002 M Warner Losh. 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.
16 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
17 * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
18 * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
19 * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
20 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
21 * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
22 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
23 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
24 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
25 * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
27 * This software may be derived from NetBSD i82365.c and other files with
28 * the following copyright:
30 * Copyright (c) 1997 Marc Horowitz. All rights reserved.
32 * Redistribution and use in source and binary forms, with or without
33 * modification, are permitted provided that the following conditions
35 * 1. Redistributions of source code must retain the above copyright
36 * notice, this list of conditions and the following disclaimer.
37 * 2. Redistributions in binary form must reproduce the above copyright
38 * notice, this list of conditions and the following disclaimer in the
39 * documentation and/or other materials provided with the distribution.
40 * 3. All advertising materials mentioning features or use of this software
41 * must display the following acknowledgement:
42 * This product includes software developed by Marc Horowitz.
43 * 4. The name of the author may not be used to endorse or promote products
44 * derived from this software without specific prior written permission.
46 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
47 * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
48 * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
49 * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
50 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
51 * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
52 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
53 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
54 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
55 * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
58 #include <sys/param.h>
59 #include <sys/systm.h>
60 #include <sys/errno.h>
61 #include <sys/kernel.h>
62 #include <sys/malloc.h>
63 #include <sys/queue.h>
64 #include <sys/module.h>
68 #include <machine/bus.h>
70 #include <machine/resource.h>
72 #include <bus/pccard/pccardreg.h>
73 #include <bus/pccard/pccardvar.h>
75 #include <dev/pccard/exca/excareg.h>
76 #include <dev/pccard/exca/excavar.h>
79 #define DEVPRINTF(dev, fmt, args...) device_printf((dev), (fmt), ## args)
80 #define DPRINTF(fmt, args...) printf(fmt, ## args)
82 #define DEVPRINTF(dev, fmt, args...)
83 #define DPRINTF(fmt, args...)
88 #define EXCA_MEMINFO(NUM) { \
89 EXCA_SYSMEM_ADDR ## NUM ## _START_LSB, \
90 EXCA_SYSMEM_ADDR ## NUM ## _START_MSB, \
91 EXCA_SYSMEM_ADDR ## NUM ## _STOP_LSB, \
92 EXCA_SYSMEM_ADDR ## NUM ## _STOP_MSB, \
93 EXCA_SYSMEM_ADDR ## NUM ## _WIN, \
94 EXCA_CARDMEM_ADDR ## NUM ## _LSB, \
95 EXCA_CARDMEM_ADDR ## NUM ## _MSB, \
96 EXCA_ADDRWIN_ENABLE_MEM ## NUM, \
99 static struct mem_map_index_st {
100 int sysmem_start_lsb;
101 int sysmem_start_msb;
108 } mem_map_index[] = {
118 * Helper function. This will map the requested memory slot. We setup the
119 * map before we call this function. This is used to initially force the
120 * mapping, as well as later restore the mapping after it has been destroyed
121 * in some fashion (due to a power event typically).
124 exca_do_mem_map(struct exca_softc *sc, int win)
126 struct mem_map_index_st *map;
127 struct pccard_mem_handle *mem;
129 map = &mem_map_index[win];
131 exca_write(sc, map->sysmem_start_lsb,
132 (mem->addr >> EXCA_SYSMEM_ADDRX_SHIFT) & 0xff);
133 exca_write(sc, map->sysmem_start_msb,
134 ((mem->addr >> (EXCA_SYSMEM_ADDRX_SHIFT + 8)) &
135 EXCA_SYSMEM_ADDRX_START_MSB_ADDR_MASK) | 0x80);
137 exca_write(sc, map->sysmem_stop_lsb,
138 ((mem->addr + mem->realsize - 1) >>
139 EXCA_SYSMEM_ADDRX_SHIFT) & 0xff);
140 exca_write(sc, map->sysmem_stop_msb,
141 (((mem->addr + mem->realsize - 1) >>
142 (EXCA_SYSMEM_ADDRX_SHIFT + 8)) &
143 EXCA_SYSMEM_ADDRX_STOP_MSB_ADDR_MASK) |
144 EXCA_SYSMEM_ADDRX_STOP_MSB_WAIT2);
146 exca_write(sc, map->sysmem_win,
147 (mem->addr >> EXCA_MEMREG_WIN_SHIFT) & 0xff);
149 exca_write(sc, map->cardmem_lsb,
150 (mem->offset >> EXCA_CARDMEM_ADDRX_SHIFT) & 0xff);
151 exca_write(sc, map->cardmem_msb,
152 ((mem->offset >> (EXCA_CARDMEM_ADDRX_SHIFT + 8)) &
153 EXCA_CARDMEM_ADDRX_MSB_ADDR_MASK) |
154 ((mem->kind == PCCARD_MEM_ATTR) ?
155 EXCA_CARDMEM_ADDRX_MSB_REGACTIVE_ATTR : 0));
157 exca_setb(sc, EXCA_ADDRWIN_ENABLE, EXCA_ADDRWIN_ENABLE_MEMCS16 |
163 int r1, r2, r3, r4, r5, r6, r7;
164 r1 = exca_read(sc, map->sysmem_start_msb);
165 r2 = exca_read(sc, map->sysmem_start_lsb);
166 r3 = exca_read(sc, map->sysmem_stop_msb);
167 r4 = exca_read(sc, map->sysmem_stop_lsb);
168 r5 = exca_read(sc, map->cardmem_msb);
169 r6 = exca_read(sc, map->cardmem_lsb);
170 r7 = exca_read(sc, map->sysmem_win);
171 printf("exca_do_mem_map window %d: %02x%02x %02x%02x "
172 "%02x%02x %02x (%08x+%08x.%08x*%08lx)\n",
173 win, r1, r2, r3, r4, r5, r6, r7,
174 mem->addr, mem->size, mem->realsize,
181 * public interface to map a resource. kind is the type of memory to
182 * map (either common or attribute). Memory created via this interface
183 * starts out at card address 0. Since the only way to set this is
184 * to set it on a struct resource after it has been mapped, we're safe
185 * in maping this assumption. Note that resources can be remapped using
186 * exca_do_mem_map so that's how the card address can be set later.
189 exca_mem_map(struct exca_softc *sc, int kind, struct resource *res)
193 for (win = 0; win < EXCA_MEM_WINS; win++) {
194 if ((sc->memalloc & (1 << win)) == 0) {
195 sc->memalloc |= (1 << win);
199 if (win >= EXCA_MEM_WINS)
201 if (((rman_get_start(res) >> EXCA_CARDMEM_ADDRX_SHIFT) & 0xff) != 0 &&
202 (sc->flags & EXCA_HAS_MEMREG_WIN) == 0) {
203 device_printf(sc->dev, "Does not support mapping above 24M.");
207 sc->mem[win].cardaddr = 0;
208 sc->mem[win].memt = rman_get_bustag(res);
209 sc->mem[win].memh = rman_get_bushandle(res);
210 sc->mem[win].addr = rman_get_start(res);
211 sc->mem[win].size = rman_get_end(res) - sc->mem[win].addr + 1;
212 sc->mem[win].realsize = sc->mem[win].size + EXCA_MEM_PAGESIZE - 1;
213 sc->mem[win].realsize = sc->mem[win].realsize -
214 (sc->mem[win].realsize % EXCA_MEM_PAGESIZE);
215 sc->mem[win].offset = (long)(sc->mem[win].addr);
216 sc->mem[win].kind = kind;
217 DPRINTF("exca_mem_map window %d bus %x+%x+%lx card addr %x\n",
218 win, sc->mem[win].addr, sc->mem[win].size,
219 sc->mem[win].offset, sc->mem[win].cardaddr);
220 exca_do_mem_map(sc, win);
226 * Private helper function. This turns off a given memory map that is in
227 * use. We do this by just clearing the enable bit in the pcic. If we needed
228 * to make memory unmapping/mapping pairs faster, we would have to store
229 * more state information about the pcic and then use that to intelligently
230 * to the map/unmap. However, since we don't do that sort of thing often
231 * (generally just at configure time), it isn't a case worth optimizing.
234 exca_mem_unmap(struct exca_softc *sc, int window)
236 if (window < 0 || window >= EXCA_MEM_WINS)
237 panic("exca_mem_unmap: window out of range");
239 exca_clrb(sc, EXCA_ADDRWIN_ENABLE, mem_map_index[window].memenable);
240 sc->memalloc &= ~(1 << window);
244 * Find the map that we're using to hold the resoruce. This works well
245 * so long as the client drivers don't do silly things like map the same
246 * area mutliple times, or map both common and attribute memory at the
247 * same time. This latter restriction is a bug. We likely should just
248 * store a pointer to the res in the mem[x] data structure.
251 exca_mem_findmap(struct exca_softc *sc, struct resource *res)
255 for (win = 0; win < EXCA_MEM_WINS; win++) {
256 if (sc->mem[win].memt == rman_get_bustag(res) &&
257 sc->mem[win].addr == rman_get_start(res) &&
258 sc->mem[win].size == rman_get_size(res))
265 * Set the memory flag. This means that we are setting if the memory
266 * is coming from attribute memory or from common memory on the card.
267 * CIS entries are generally in attribute memory (although they can
268 * reside in common memory). Generally, this is the only use for attribute
269 * memory. However, some cards require their drivers to dance in both
270 * common and/or attribute memory and this interface (and setting the
271 * offset interface) exist for such cards.
274 exca_mem_set_flags(struct exca_softc *sc, struct resource *res, uint32_t flags)
278 win = exca_mem_findmap(sc, res);
280 device_printf(sc->dev,
281 "set_res_flags: specified resource not active\n");
285 sc->mem[win].kind = flags;
286 exca_do_mem_map(sc, win);
291 * Given a resource, go ahead and unmap it if we can find it in the
292 * resrouce list that's used.
295 exca_mem_unmap_res(struct exca_softc *sc, struct resource *res)
299 win = exca_mem_findmap(sc, res);
302 exca_mem_unmap(sc, win);
307 * Set the offset of the memory. We use this for reading the CIS and
308 * frobbing the pccard's pccard registers (POR, etc). Some drivers
309 * need to access this functionality as well, since they have receive
310 * buffers defined in the attribute memory. Thankfully, these cards
311 * are few and fare between. Some cards also have common memory that
312 * is large and only map a small portion of it at a time (but these cards
313 * are rare, the more common case being to have just a small amount
314 * of common memory that the driver needs to bcopy data from in order to
318 exca_mem_set_offset(struct exca_softc *sc, struct resource *res,
319 uint32_t cardaddr, uint32_t *deltap)
324 win = exca_mem_findmap(sc, res);
326 device_printf(sc->dev,
327 "set_memory_offset: specified resource not active\n");
330 sc->mem[win].cardaddr = cardaddr;
331 delta = cardaddr % EXCA_MEM_PAGESIZE;
335 sc->mem[win].realsize = sc->mem[win].size + delta +
336 EXCA_MEM_PAGESIZE - 1;
337 sc->mem[win].realsize = sc->mem[win].realsize -
338 (sc->mem[win].realsize % EXCA_MEM_PAGESIZE);
339 sc->mem[win].offset = cardaddr - sc->mem[win].addr;
340 exca_do_mem_map(sc, win);
347 #define EXCA_IOINFO(NUM) { \
348 EXCA_IOADDR ## NUM ## _START_LSB, \
349 EXCA_IOADDR ## NUM ## _START_MSB, \
350 EXCA_IOADDR ## NUM ## _STOP_LSB, \
351 EXCA_IOADDR ## NUM ## _STOP_MSB, \
352 EXCA_ADDRWIN_ENABLE_IO ## NUM, \
353 EXCA_IOCTL_IO ## NUM ## _WAITSTATE \
354 | EXCA_IOCTL_IO ## NUM ## _ZEROWAIT \
355 | EXCA_IOCTL_IO ## NUM ## _IOCS16SRC_MASK \
356 | EXCA_IOCTL_IO ## NUM ## _DATASIZE_MASK, \
358 EXCA_IOCTL_IO ## NUM ## _IOCS16SRC_CARD, \
359 EXCA_IOCTL_IO ## NUM ## _IOCS16SRC_DATASIZE \
360 | EXCA_IOCTL_IO ## NUM ## _DATASIZE_8BIT, \
361 EXCA_IOCTL_IO ## NUM ## _IOCS16SRC_DATASIZE \
362 | EXCA_IOCTL_IO ## NUM ## _DATASIZE_16BIT, \
366 static struct io_map_index_st {
373 int ioctlbits[3]; /* indexed by PCCARD_WIDTH_* */
381 exca_do_io_map(struct exca_softc *sc, int win)
383 struct io_map_index_st *map;
385 struct pccard_io_handle *io;
387 map = &io_map_index[win];
389 exca_write(sc, map->start_lsb, io->addr & 0xff);
390 exca_write(sc, map->start_msb, (io->addr >> 8) & 0xff);
392 exca_write(sc, map->stop_lsb, (io->addr + io->size - 1) & 0xff);
393 exca_write(sc, map->stop_msb, ((io->addr + io->size - 1) >> 8) & 0xff);
395 exca_clrb(sc, EXCA_IOCTL, map->ioctlmask);
396 exca_setb(sc, EXCA_IOCTL, map->ioctlbits[io->width]);
398 exca_setb(sc, EXCA_ADDRWIN_ENABLE, map->ioenable);
402 r1 = exca_read(sc, map->start_msb);
403 r2 = exca_read(sc, map->start_lsb);
404 r3 = exca_read(sc, map->stop_msb);
405 r4 = exca_read(sc, map->stop_lsb);
406 DPRINTF("exca_do_io_map window %d: %02x%02x %02x%02x "
407 "(%08x+%08x)\n", win, r1, r2, r3, r4,
414 exca_io_map(struct exca_softc *sc, int width, struct resource *r)
418 static char *width_names[] = { "auto", "io8", "io16"};
420 for (win=0; win < EXCA_IO_WINS; win++) {
421 if ((sc->ioalloc & (1 << win)) == 0) {
422 sc->ioalloc |= (1 << win);
426 if (win >= EXCA_IO_WINS)
429 sc->io[win].iot = rman_get_bustag(r);
430 sc->io[win].ioh = rman_get_bushandle(r);
431 sc->io[win].addr = rman_get_start(r);
432 sc->io[win].size = rman_get_end(r) - sc->io[win].addr + 1;
433 sc->io[win].flags = 0;
434 sc->io[win].width = width;
435 DPRINTF("exca_io_map window %d %s port %x+%x\n",
436 win, width_names[width], sc->io[win].addr,
438 exca_do_io_map(sc, win);
444 exca_io_unmap(struct exca_softc *sc, int window)
446 if (window >= EXCA_IO_WINS)
447 panic("exca_io_unmap: window out of range");
449 exca_clrb(sc, EXCA_ADDRWIN_ENABLE, io_map_index[window].ioenable);
451 sc->ioalloc &= ~(1 << window);
453 sc->io[window].iot = 0;
454 sc->io[window].ioh = 0;
455 sc->io[window].addr = 0;
456 sc->io[window].size = 0;
457 sc->io[window].flags = 0;
458 sc->io[window].width = 0;
462 exca_io_findmap(struct exca_softc *sc, struct resource *res)
466 for (win = 0; win < EXCA_IO_WINS; win++) {
467 if (sc->io[win].iot == rman_get_bustag(res) &&
468 sc->io[win].addr == rman_get_start(res) &&
469 sc->io[win].size == rman_get_size(res))
477 exca_io_unmap_res(struct exca_softc *sc, struct resource *res)
481 win = exca_io_findmap(sc, res);
484 exca_io_unmap(sc, win);
491 * If interrupts are enabled, then we should be able to just wait for
492 * an interrupt routine to wake us up. Busy waiting shouldn't be
493 * necessary. Sadly, not all legacy ISA cards support an interrupt
494 * for the busy state transitions, at least according to their datasheets,
495 * so we busy wait a while here..
498 exca_wait_ready(struct exca_softc *sc)
501 DEVPRINTF(sc->dev, "exca_wait_ready: status 0x%02x\n",
502 exca_read(sc, EXCA_IF_STATUS));
503 for (i = 0; i < 10000; i++) {
504 if (exca_read(sc, EXCA_IF_STATUS) & EXCA_IF_STATUS_READY)
508 device_printf(sc->dev, "ready never happened, status = %02x\n",
509 exca_read(sc, EXCA_IF_STATUS));
513 * Reset the card. Ideally, we'd do a lot of this via interrupts.
514 * However, many PC Cards will deassert the ready signal. This means
515 * that they are asserting an interrupt. This makes it hard to
516 * do anything but a busy wait here. One could argue that these
517 * such cards are broken, or that the bridge that allows this sort
518 * of interrupt through isn't quite what you'd want (and may be a standards
519 * violation). However, such arguing would leave a huge class of pc cards
520 * and bridges out of reach for use in the system.
522 * Maybe I should reevaluate the above based on the power bug I fixed
526 exca_reset(struct exca_softc *sc, device_t child)
531 /* enable socket i/o */
532 exca_setb(sc, EXCA_PWRCTL, EXCA_PWRCTL_OE);
534 exca_write(sc, EXCA_INTR, EXCA_INTR_ENABLE);
535 /* hold reset for 30ms */
537 /* clear the reset flag */
538 exca_setb(sc, EXCA_INTR, EXCA_INTR_RESET);
539 /* wait 20ms as per pc card standard (r2.01) section 4.3.6 */
544 /* disable all address windows */
545 exca_write(sc, EXCA_ADDRWIN_ENABLE, 0);
547 CARD_GET_TYPE(child, &cardtype);
548 exca_setb(sc, EXCA_INTR, (cardtype == PCCARD_IFTYPE_IO) ?
549 EXCA_INTR_CARDTYPE_IO : EXCA_INTR_CARDTYPE_MEM);
550 DEVPRINTF(sc->dev, "card type is %s\n",
551 (cardtype == PCCARD_IFTYPE_IO) ? "io" : "mem");
553 /* reinstall all the memory and io mappings */
554 for (win = 0; win < EXCA_MEM_WINS; ++win)
555 if (sc->memalloc & (1 << win))
556 exca_do_mem_map(sc, win);
557 for (win = 0; win < EXCA_IO_WINS; ++win)
558 if (sc->ioalloc & (1 << win))
559 exca_do_io_map(sc, win);
563 * Initialize the exca_softc data structure for the first time.
566 exca_init(struct exca_softc *sc, device_t dev,
567 bus_space_tag_t bst, bus_space_handle_t bsh, uint32_t offset)
579 * Probe the expected slots. We maybe should set the ID for each of these
580 * slots too while we're at it. But maybe that belongs to a separate
583 * Callers must charantee that there are at least EXCA_NSLOTS (4) in
584 * the array that they pass the address of the first element in the
588 exca_probe_slots(device_t dev, struct exca_softc *exca)
591 struct resource *res;
594 bus_space_handle_t ioh;
599 res = bus_alloc_resource(dev, SYS_RES_IOPORT, &rid, 0, ~0, EXCA_IOSIZE,
603 iot = rman_get_bustag(res);
604 ioh = rman_get_bushandle(res);
605 for (i = 0; i < EXCA_NSLOTS; i++) {
606 exca_init(&exca[i], dev, iot, ioh, i * EXCA_SOCKET_SIZE);
607 if (exca_is_pcic(&exca[i])) {
609 exca[i].flags |= EXCA_SOCKET_PRESENT;
612 bus_release_resource(dev, SYS_RES_IOPORT, rid, res);
617 exca_is_pcic(struct exca_softc *sc)
623 static int exca_modevent(module_t mod, int cmd, void *arg)
627 DEV_MODULE(exca, exca_modevent, NULL);
628 MODULE_VERSION(exca, 1);