2 * pccard.c - Interface code for PC-CARD controllers.
4 * June 1995, Andrew McRae (andrew@mega.com.au)
5 *-------------------------------------------------------------------------
7 * Copyright (c) 2001 M. Warner Losh. All rights reserved.
8 * Copyright (c) 1995 Andrew McRae. All rights reserved.
10 * Redistribution and use in source and binary forms, with or without
11 * modification, are permitted provided that the following conditions
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 * 3. 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.
32 * $FreeBSD: src/sys/pccard/pccard.c,v 1.106.2.15 2003/02/26 18:42:00 imp Exp $
35 #include <sys/param.h>
36 #include <sys/types.h>
37 #include <sys/systm.h>
38 #include <sys/kernel.h>
39 #include <sys/malloc.h>
40 #include <sys/sysctl.h>
46 #include <machine/bus.h>
48 #include <pccard/cardinfo.h>
49 #include <pccard/driver.h>
50 #include <pccard/slot.h>
51 #include <pccard/pccard_nbk.h>
53 #include <machine/md_var.h>
55 #define MIN(a,b) ((a)<(b)?(a):(b))
57 static int allocate_driver(struct slot *, struct dev_desc *);
58 static void inserted(void *);
59 static void disable_slot(struct slot *);
60 static void disable_slot_to(struct slot *);
61 static void power_off_slot(void *);
64 * The driver interface for read/write uses a block
65 * of memory in the ISA I/O memory space allocated via
68 * Now that we have different bus attachments, we should really
69 * use a better algorythm to allocate memory.
71 static unsigned long pccard_mem; /* Physical memory */
72 static unsigned char *pccard_kmem; /* Kernel virtual address */
73 static struct resource *pccard_mem_res;
74 static int pccard_mem_rid;
76 static d_open_t crdopen;
77 static d_close_t crdclose;
78 static d_read_t crdread;
79 static d_write_t crdwrite;
80 static d_ioctl_t crdioctl;
81 static d_poll_t crdpoll;
84 static struct cdevsw crd_cdevsw = {
92 /* strategy */ nostrategy,
101 * Power off the slot.
102 * (doing it immediately makes the removal of some cards unstable)
105 power_off_slot(void *arg)
107 struct slot *slt = (struct slot *)arg;
111 * The following will generate an interrupt. So, to hold off
112 * the interrupt unitl after disable runs so that we can get rid
113 * rid of the interrupt before it becomes unsafe to touch the
116 * XXX In current, the spl stuff is a nop.
119 /* Power off the slot. */
120 slt->pwr_off_pending = 0;
121 slt->ctrl->disable(slt);
126 * disable_slot - Disables the slot by removing
127 * the power and unmapping the I/O
130 disable_slot(struct slot *slt)
139 * Note that a race condition is possible here; if a
140 * driver is accessing the device and it is removed, then
141 * all bets are off...
143 pccarddev = slt->dev;
144 device_get_children(pccarddev, &kids, &nkids);
145 for (i = 0; i < nkids; i++) {
146 if ((ret = device_delete_child(pccarddev, kids[i])) != 0)
147 printf("pccard: delete of %s failed: %d\n",
148 device_get_nameunit(kids[i]), ret);
152 /* Power off the slot 1/2 second after removal of the card */
153 slt->poff_ch = timeout(power_off_slot, (caddr_t)slt, hz / 2);
154 slt->pwr_off_pending = 1;
158 disable_slot_to(struct slot *slt)
161 if (slt->state == empty)
162 printf("pccard: card removed, slot %d\n", slt->slotnum);
164 printf("pccard: card deactivated, slot %d\n", slt->slotnum);
165 pccard_remove_beep();
166 selwakeup(&slt->selp);
170 * pccard_init_slot - Initialize the slot controller and attach various
171 * things to it. We also make the device for it. We create the device that
172 * will be exported to devfs.
175 pccard_init_slot(device_t dev, struct slot_ctrl *ctrl)
180 slt = PCCARD_DEVICE2SOFTC(dev);
181 slotno = device_get_unit(dev);
183 slt->d = make_dev(&crd_cdevsw, slotno, 0, 0, 0600, "card%d", slotno);
184 slt->d->si_drv1 = slt;
186 slt->slotnum = slotno;
187 callout_handle_init(&slt->insert_ch);
188 callout_handle_init(&slt->poff_ch);
194 * allocate_driver - Create a new device entry for this
195 * slot, and attach a driver to it.
198 allocate_driver(struct slot *slt, struct dev_desc *desc)
200 struct pccard_devinfo *devi;
207 pccarddev = slt->dev;
208 err = device_get_children(pccarddev, &devs, &count);
213 device_printf(pccarddev,
214 "Can not attach more than one child.\n");
217 irq = ffs(desc->irqmask) - 1;
218 MALLOC(devi, struct pccard_devinfo *, sizeof(*devi), M_DEVBUF,
220 strcpy(devi->name, desc->name);
222 * Create an entry for the device under this slot.
226 bcopy(desc->misc, devi->misc, sizeof(desc->misc));
227 strcpy(devi->manufstr, desc->manufstr);
228 strcpy(devi->versstr, desc->versstr);
229 devi->manufacturer = desc->manufacturer;
230 devi->product = desc->product;
231 devi->prodext = desc->prodext;
232 resource_list_init(&devi->resources);
233 child = device_add_child(pccarddev, devi->name, desc->unit);
235 if (desc->unit != -1)
236 device_printf(pccarddev,
237 "Unit %d failed for %s, try a different unit\n",
238 desc->unit, devi->name);
240 device_printf(pccarddev,
241 "No units available for %s. Impossible?\n",
245 device_set_flags(child, desc->flags);
246 device_set_ivars(child, devi);
248 device_printf(pccarddev, "Assigning %s:",
249 device_get_nameunit(child));
251 printf(" io 0x%x-0x%x",
252 desc->iobase, desc->iobase + desc->iosize - 1);
254 printf(" irq %d", irq);
256 printf(" mem 0x%lx-0x%lx", desc->mem,
257 desc->mem + desc->memsize - 1);
258 printf(" flags 0x%x\n", desc->flags);
260 err = bus_set_resource(child, SYS_RES_IOPORT, 0, desc->iobase,
265 err = bus_set_resource(child, SYS_RES_IRQ, 0, irq, 1);
269 err = bus_set_resource(child, SYS_RES_MEMORY, 0, desc->mem,
274 err = device_probe_and_attach(child);
276 * XXX We unwisely assume that the detach code won't run while the
277 * XXX the attach code is attaching. Someone should put some
278 * XXX interlock code. This can happen if probe/attach takes a while
279 * XXX and the user ejects the card, which causes the detach
280 * XXX function to be called.
282 strncpy(desc->name, device_get_nameunit(child), sizeof(desc->name));
283 desc->name[sizeof(desc->name) - 1] = '\0';
286 device_delete_child(pccarddev, child);
291 * card insert routine - Called from a timeout to debounce
297 struct slot *slt = arg;
301 * Disable any pending timeouts for this slot, and explicitly
302 * power it off right now. Then, re-enable the power using
303 * the (possibly new) power settings.
305 untimeout(power_off_slot, (caddr_t)slt, slt->poff_ch);
309 * Enable 5V to the card so that the CIS can be read. Well,
310 * enable the most natural voltage so that the CIS can be read.
314 slt->ctrl->power(slt);
316 printf("pccard: card inserted, slot %d\n", slt->slotnum);
317 pccard_insert_beep();
318 slt->ctrl->reset(slt);
322 * Card event callback. Called at splhigh to prevent
323 * device interrupts from interceding.
326 pccard_event(struct slot *slt, enum card_event event)
328 if (slt->insert_seq) {
330 untimeout(inserted, (void *)slt, slt->insert_ch);
335 case card_deactivated:
336 if (slt->state == filled || slt->state == inactive) {
337 if (event == card_removed)
340 slt->state = inactive;
341 disable_slot_to(slt);
346 slt->insert_ch = timeout(inserted, (void *)slt, hz/4);
352 * Device driver interface.
355 crdopen(dev_t dev, int oflags, int devtype, d_thread_t *td)
357 struct slot *slt = PCCARD_DEV2SOFTC(dev);
362 slt->rwmem = MDF_ATTR;
367 * Close doesn't de-allocate any resources, since
368 * slots may be assigned to drivers already.
371 crdclose(dev_t dev, int fflag, int devtype, d_thread_t *td)
377 * read interface. Map memory at lseek offset,
378 * then transfer to user space.
381 crdread(dev_t dev, struct uio *uio, int ioflag)
383 struct slot *slt = PCCARD_DEV2SOFTC(dev);
384 struct mem_desc *mp, oldmap;
387 int error = 0, win, count;
389 if (slt == 0 || slt->state != filled)
393 for (win = slt->ctrl->maxmem - 1; win >= 0; win--)
394 if ((slt->mem[win].flags & MDF_ACTIVE) == 0)
400 mp->flags = slt->rwmem | MDF_ACTIVE;
401 while (uio->uio_resid && error == 0) {
402 mp->card = uio->uio_offset;
403 mp->size = PCCARD_MEMSIZE;
404 mp->start = (caddr_t)(void *)(uintptr_t)pccard_mem;
405 if ((error = slt->ctrl->mapmem(slt, win)) != 0)
407 offs = (unsigned int)uio->uio_offset & (PCCARD_MEMSIZE - 1);
408 p = pccard_kmem + offs;
409 count = MIN(PCCARD_MEMSIZE - offs, uio->uio_resid);
410 error = uiomove(p, count, uio);
413 * Restore original map.
416 slt->ctrl->mapmem(slt, win);
422 * crdwrite - Write data to card memory.
423 * Handles wrap around so that only one memory
427 crdwrite(dev_t dev, struct uio *uio, int ioflag)
429 struct slot *slt = PCCARD_DEV2SOFTC(dev);
430 struct mem_desc *mp, oldmap;
433 int error = 0, win, count;
435 if (slt == 0 || slt->state != filled)
439 for (win = slt->ctrl->maxmem - 1; win >= 0; win--)
440 if ((slt->mem[win].flags & MDF_ACTIVE) == 0)
446 mp->flags = slt->rwmem | MDF_ACTIVE;
447 while (uio->uio_resid && error == 0) {
448 mp->card = uio->uio_offset;
449 mp->size = PCCARD_MEMSIZE;
450 mp->start = (caddr_t)(void *)(uintptr_t)pccard_mem;
451 if ((error = slt->ctrl->mapmem(slt, win)) != 0)
453 offs = (unsigned int)uio->uio_offset & (PCCARD_MEMSIZE - 1);
454 p = pccard_kmem + offs;
455 count = MIN(PCCARD_MEMSIZE - offs, uio->uio_resid);
456 error = uiomove(p, count, uio);
459 * Restore original map.
462 slt->ctrl->mapmem(slt, win);
468 * ioctl calls - allows setting/getting of memory and I/O
469 * descriptors, and assignment of drivers.
472 crdioctl(dev_t dev, u_long cmd, caddr_t data, int fflag, d_thread_t *td)
481 struct slot *slt = PCCARD_DEV2SOFTC(dev);
483 if (slt == 0 && cmd != PIOCRWMEM)
487 if (slt->ctrl->ioctl)
488 return (slt->ctrl->ioctl(slt, cmd, data));
495 ((struct slotstate *)data)->state = slt->state;
496 ((struct slotstate *)data)->laststate = slt->laststate;
497 slt->laststate = slt->state;
499 ((struct slotstate *)data)->maxmem = slt->ctrl->maxmem;
500 ((struct slotstate *)data)->maxio = slt->ctrl->maxio;
501 ((struct slotstate *)data)->irqs = 0;
504 * Get memory context.
507 s = ((struct mem_desc *)data)->window;
508 if (s < 0 || s >= slt->ctrl->maxmem)
511 ((struct mem_desc *)data)->flags = mp->flags;
512 ((struct mem_desc *)data)->start = mp->start;
513 ((struct mem_desc *)data)->size = mp->size;
514 ((struct mem_desc *)data)->card = mp->card;
517 * Set memory context. If context already active, then unmap it.
518 * It is hard to see how the parameters can be checked.
519 * At the very least, we only allow root to set the context.
524 if (slt->state != filled)
526 s = ((struct mem_desc *)data)->window;
527 if (s < 0 || s >= slt->ctrl->maxmem)
529 slt->mem[s] = *((struct mem_desc *)data);
530 return (slt->ctrl->mapmem(slt, s));
532 * Get I/O port context.
535 s = ((struct io_desc *)data)->window;
536 if (s < 0 || s >= slt->ctrl->maxio)
539 ((struct io_desc *)data)->flags = ip->flags;
540 ((struct io_desc *)data)->start = ip->start;
541 ((struct io_desc *)data)->size = ip->size;
544 * Set I/O port context.
549 if (slt->state != filled)
551 s = ((struct io_desc *)data)->window;
552 if (s < 0 || s >= slt->ctrl->maxio)
554 slt->io[s] = *((struct io_desc *)data);
555 /* XXX Don't actually map */
559 * Set memory window flags for read/write interface.
562 slt->rwmem = *(int *)data;
565 * Set the memory window to be used for the read/write interface.
568 if (*(unsigned long *)data == 0) {
569 *(unsigned long *)data = pccard_mem;
575 * Validate the memory by checking it against the I/O
576 * memory range. It must also start on an aligned block size.
578 if (*(unsigned long *)data & (PCCARD_MEMSIZE-1))
580 pccarddev = PCCARD_DEV2SOFTC(dev)->dev;
582 addr = *(unsigned long *)data;
584 bus_release_resource(pccarddev, SYS_RES_MEMORY,
585 pccard_mem_rid, pccard_mem_res);
586 pccard_mem_res = bus_alloc_resource(pccarddev, SYS_RES_MEMORY,
587 &pccard_mem_rid, addr, addr, PCCARD_MEMSIZE,
588 RF_ACTIVE | rman_make_alignment_flags(PCCARD_MEMSIZE));
589 if (pccard_mem_res == NULL)
591 pccard_mem = rman_get_start(pccard_mem_res);
592 pccard_kmem = rman_get_virtual(pccard_mem_res);
598 slt->pwr = *(struct power *)data;
599 return (slt->ctrl->power(slt));
601 * Allocate a driver to this slot.
606 err = allocate_driver(slt, (struct dev_desc *)data);
608 pccard_success_beep();
610 pccard_failure_beep();
613 * Virtual removal/insertion
616 pwval = *(int *)data;
618 if (slt->state != filled)
620 pccard_event(slt, card_deactivated);
622 if (slt->state != empty && slt->state != inactive)
624 pccard_event(slt, card_inserted);
628 if (pccard_beep_select(*(int *)data)) {
637 * poll - Poll on exceptions will return true
638 * when a change in card status occurs.
641 crdpoll(dev_t dev, int events, d_thread_t *td)
645 struct slot *slt = PCCARD_DEV2SOFTC(dev);
647 if (events & (POLLIN | POLLRDNORM))
648 revents |= events & (POLLIN | POLLRDNORM);
650 if (events & (POLLOUT | POLLWRNORM))
651 revents |= events & (POLLIN | POLLRDNORM);
655 * select for exception - card event.
657 if (events & POLLRDBAND)
658 if (slt == 0 || slt->laststate != slt->state)
659 revents |= POLLRDBAND;
662 selrecord(td, &slt->selp);
669 * APM hooks for suspending and resuming.
672 pccard_suspend(device_t dev)
674 struct slot *slt = PCCARD_DEVICE2SOFTC(dev);
676 /* This code stolen from pccard_event:card_removed */
677 if (slt->state == filled) {
678 int s = splhigh(); /* nop on current */
680 slt->laststate = suspend; /* for pccardd */
683 printf("pccard: card disabled, slot %d\n", slt->slotnum);
686 * Disable any pending timeouts for this slot since we're
687 * powering it down/disabling now.
689 untimeout(power_off_slot, (caddr_t)slt, slt->poff_ch);
690 slt->ctrl->disable(slt);
695 pccard_resume(device_t dev)
697 struct slot *slt = PCCARD_DEVICE2SOFTC(dev);
699 slt->ctrl->resume(slt);