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 $
33 * $DragonFly: src/sys/bus/pccard/pccard.c,v 1.7 2003/08/07 21:16:46 dillon Exp $
36 #include <sys/param.h>
37 #include <sys/types.h>
38 #include <sys/systm.h>
39 #include <sys/kernel.h>
40 #include <sys/malloc.h>
41 #include <sys/sysctl.h>
47 #include <machine/bus.h>
52 #include "pccard_nbk.h"
54 #include <machine/md_var.h>
56 static int allocate_driver(struct slot *, struct dev_desc *);
57 static void inserted(void *);
58 static void disable_slot(struct slot *);
59 static void disable_slot_to(struct slot *);
60 static void power_off_slot(void *);
63 * The driver interface for read/write uses a block
64 * of memory in the ISA I/O memory space allocated via
67 * Now that we have different bus attachments, we should really
68 * use a better algorythm to allocate memory.
70 static unsigned long pccard_mem; /* Physical memory */
71 static unsigned char *pccard_kmem; /* Kernel virtual address */
72 static struct resource *pccard_mem_res;
73 static int pccard_mem_rid;
75 static d_open_t crdopen;
76 static d_close_t crdclose;
77 static d_read_t crdread;
78 static d_write_t crdwrite;
79 static d_ioctl_t crdioctl;
80 static d_poll_t crdpoll;
83 static struct cdevsw crd_cdevsw = {
97 /* strategy */ nostrategy,
103 * Power off the slot.
104 * (doing it immediately makes the removal of some cards unstable)
107 power_off_slot(void *arg)
109 struct slot *slt = (struct slot *)arg;
113 * The following will generate an interrupt. So, to hold off
114 * the interrupt unitl after disable runs so that we can get rid
115 * rid of the interrupt before it becomes unsafe to touch the
118 * XXX In current, the spl stuff is a nop.
121 /* Power off the slot. */
122 slt->pwr_off_pending = 0;
123 slt->ctrl->disable(slt);
128 * disable_slot - Disables the slot by removing
129 * the power and unmapping the I/O
132 disable_slot(struct slot *slt)
141 * Note that a race condition is possible here; if a
142 * driver is accessing the device and it is removed, then
143 * all bets are off...
145 pccarddev = slt->dev;
146 device_get_children(pccarddev, &kids, &nkids);
147 for (i = 0; i < nkids; i++) {
148 if ((ret = device_delete_child(pccarddev, kids[i])) != 0)
149 printf("pccard: delete of %s failed: %d\n",
150 device_get_nameunit(kids[i]), ret);
154 /* Power off the slot 1/2 second after removal of the card */
155 slt->poff_ch = timeout(power_off_slot, (caddr_t)slt, hz / 2);
156 slt->pwr_off_pending = 1;
160 disable_slot_to(struct slot *slt)
163 if (slt->state == empty)
164 printf("pccard: card removed, slot %d\n", slt->slotnum);
166 printf("pccard: card deactivated, slot %d\n", slt->slotnum);
167 pccard_remove_beep();
168 selwakeup(&slt->selp);
172 * pccard_init_slot - Initialize the slot controller and attach various
173 * things to it. We also make the device for it. We create the device that
174 * will be exported to devfs.
177 pccard_init_slot(device_t dev, struct slot_ctrl *ctrl)
182 slt = PCCARD_DEVICE2SOFTC(dev);
183 slotno = device_get_unit(dev);
185 slt->d = make_dev(&crd_cdevsw, slotno, 0, 0, 0600, "card%d", slotno);
186 slt->d->si_drv1 = slt;
188 slt->slotnum = slotno;
189 callout_handle_init(&slt->insert_ch);
190 callout_handle_init(&slt->poff_ch);
196 * allocate_driver - Create a new device entry for this
197 * slot, and attach a driver to it.
200 allocate_driver(struct slot *slt, struct dev_desc *desc)
202 struct pccard_devinfo *devi;
209 pccarddev = slt->dev;
210 err = device_get_children(pccarddev, &devs, &count);
215 device_printf(pccarddev,
216 "Can not attach more than one child.\n");
219 irq = ffs(desc->irqmask) - 1;
220 MALLOC(devi, struct pccard_devinfo *, sizeof(*devi), M_DEVBUF,
222 strcpy(devi->name, desc->name);
224 * Create an entry for the device under this slot.
228 bcopy(desc->misc, devi->misc, sizeof(desc->misc));
229 strcpy(devi->manufstr, desc->manufstr);
230 strcpy(devi->versstr, desc->versstr);
231 devi->manufacturer = desc->manufacturer;
232 devi->product = desc->product;
233 devi->prodext = desc->prodext;
234 resource_list_init(&devi->resources);
235 child = device_add_child(pccarddev, devi->name, desc->unit);
237 if (desc->unit != -1)
238 device_printf(pccarddev,
239 "Unit %d failed for %s, try a different unit\n",
240 desc->unit, devi->name);
242 device_printf(pccarddev,
243 "No units available for %s. Impossible?\n",
247 device_set_flags(child, desc->flags);
248 device_set_ivars(child, devi);
250 device_printf(pccarddev, "Assigning %s:",
251 device_get_nameunit(child));
253 printf(" io 0x%x-0x%x",
254 desc->iobase, desc->iobase + desc->iosize - 1);
256 printf(" irq %d", irq);
258 printf(" mem 0x%lx-0x%lx", desc->mem,
259 desc->mem + desc->memsize - 1);
260 printf(" flags 0x%x\n", desc->flags);
262 err = bus_set_resource(child, SYS_RES_IOPORT, 0, desc->iobase,
267 err = bus_set_resource(child, SYS_RES_IRQ, 0, irq, 1);
271 err = bus_set_resource(child, SYS_RES_MEMORY, 0, desc->mem,
276 err = device_probe_and_attach(child);
278 * XXX We unwisely assume that the detach code won't run while the
279 * XXX the attach code is attaching. Someone should put some
280 * XXX interlock code. This can happen if probe/attach takes a while
281 * XXX and the user ejects the card, which causes the detach
282 * XXX function to be called.
284 strncpy(desc->name, device_get_nameunit(child), sizeof(desc->name));
285 desc->name[sizeof(desc->name) - 1] = '\0';
288 device_delete_child(pccarddev, child);
293 * card insert routine - Called from a timeout to debounce
299 struct slot *slt = arg;
303 * Disable any pending timeouts for this slot, and explicitly
304 * power it off right now. Then, re-enable the power using
305 * the (possibly new) power settings.
307 untimeout(power_off_slot, (caddr_t)slt, slt->poff_ch);
311 * Enable 5V to the card so that the CIS can be read. Well,
312 * enable the most natural voltage so that the CIS can be read.
316 slt->ctrl->power(slt);
318 printf("pccard: card inserted, slot %d\n", slt->slotnum);
319 pccard_insert_beep();
320 slt->ctrl->reset(slt);
324 * Card event callback. Called at splhigh to prevent
325 * device interrupts from interceding.
328 pccard_event(struct slot *slt, enum card_event event)
330 if (slt->insert_seq) {
332 untimeout(inserted, (void *)slt, slt->insert_ch);
337 case card_deactivated:
338 if (slt->state == filled || slt->state == inactive) {
339 if (event == card_removed)
342 slt->state = inactive;
343 disable_slot_to(slt);
348 slt->insert_ch = timeout(inserted, (void *)slt, hz/4);
354 * Device driver interface.
357 crdopen(dev_t dev, int oflags, int devtype, d_thread_t *td)
359 struct slot *slt = PCCARD_DEV2SOFTC(dev);
364 slt->rwmem = MDF_ATTR;
369 * Close doesn't de-allocate any resources, since
370 * slots may be assigned to drivers already.
373 crdclose(dev_t dev, int fflag, int devtype, d_thread_t *td)
379 * read interface. Map memory at lseek offset,
380 * then transfer to user space.
383 crdread(dev_t dev, struct uio *uio, int ioflag)
385 struct slot *slt = PCCARD_DEV2SOFTC(dev);
386 struct mem_desc *mp, oldmap;
389 int error = 0, win, count;
391 if (slt == 0 || slt->state != filled)
395 for (win = slt->ctrl->maxmem - 1; win >= 0; win--)
396 if ((slt->mem[win].flags & MDF_ACTIVE) == 0)
402 mp->flags = slt->rwmem | MDF_ACTIVE;
403 while (uio->uio_resid && error == 0) {
404 mp->card = uio->uio_offset;
405 mp->size = PCCARD_MEMSIZE;
406 mp->start = (caddr_t)(void *)(uintptr_t)pccard_mem;
407 if ((error = slt->ctrl->mapmem(slt, win)) != 0)
409 offs = (unsigned int)uio->uio_offset & (PCCARD_MEMSIZE - 1);
410 p = pccard_kmem + offs;
411 count = MIN(PCCARD_MEMSIZE - offs, uio->uio_resid);
412 error = uiomove(p, count, uio);
415 * Restore original map.
418 slt->ctrl->mapmem(slt, win);
424 * crdwrite - Write data to card memory.
425 * Handles wrap around so that only one memory
429 crdwrite(dev_t dev, struct uio *uio, int ioflag)
431 struct slot *slt = PCCARD_DEV2SOFTC(dev);
432 struct mem_desc *mp, oldmap;
435 int error = 0, win, count;
437 if (slt == 0 || slt->state != filled)
441 for (win = slt->ctrl->maxmem - 1; win >= 0; win--)
442 if ((slt->mem[win].flags & MDF_ACTIVE) == 0)
448 mp->flags = slt->rwmem | MDF_ACTIVE;
449 while (uio->uio_resid && error == 0) {
450 mp->card = uio->uio_offset;
451 mp->size = PCCARD_MEMSIZE;
452 mp->start = (caddr_t)(void *)(uintptr_t)pccard_mem;
453 if ((error = slt->ctrl->mapmem(slt, win)) != 0)
455 offs = (unsigned int)uio->uio_offset & (PCCARD_MEMSIZE - 1);
456 p = pccard_kmem + offs;
457 count = MIN(PCCARD_MEMSIZE - offs, uio->uio_resid);
458 error = uiomove(p, count, uio);
461 * Restore original map.
464 slt->ctrl->mapmem(slt, win);
470 * ioctl calls - allows setting/getting of memory and I/O
471 * descriptors, and assignment of drivers.
474 crdioctl(dev_t dev, u_long cmd, caddr_t data, int fflag, d_thread_t *td)
483 struct slot *slt = PCCARD_DEV2SOFTC(dev);
485 if (slt == 0 && cmd != PIOCRWMEM)
488 KKASSERT(td->td_proc != NULL);
492 if (slt->ctrl->ioctl)
493 return (slt->ctrl->ioctl(slt, cmd, data));
500 ((struct slotstate *)data)->state = slt->state;
501 ((struct slotstate *)data)->laststate = slt->laststate;
502 slt->laststate = slt->state;
504 ((struct slotstate *)data)->maxmem = slt->ctrl->maxmem;
505 ((struct slotstate *)data)->maxio = slt->ctrl->maxio;
506 ((struct slotstate *)data)->irqs = 0;
509 * Get memory context.
512 s = ((struct mem_desc *)data)->window;
513 if (s < 0 || s >= slt->ctrl->maxmem)
516 ((struct mem_desc *)data)->flags = mp->flags;
517 ((struct mem_desc *)data)->start = mp->start;
518 ((struct mem_desc *)data)->size = mp->size;
519 ((struct mem_desc *)data)->card = mp->card;
522 * Set memory context. If context already active, then unmap it.
523 * It is hard to see how the parameters can be checked.
524 * At the very least, we only allow root to set the context.
529 if (slt->state != filled)
531 s = ((struct mem_desc *)data)->window;
532 if (s < 0 || s >= slt->ctrl->maxmem)
534 slt->mem[s] = *((struct mem_desc *)data);
535 return (slt->ctrl->mapmem(slt, s));
537 * Get I/O port context.
540 s = ((struct io_desc *)data)->window;
541 if (s < 0 || s >= slt->ctrl->maxio)
544 ((struct io_desc *)data)->flags = ip->flags;
545 ((struct io_desc *)data)->start = ip->start;
546 ((struct io_desc *)data)->size = ip->size;
549 * Set I/O port context.
554 if (slt->state != filled)
556 s = ((struct io_desc *)data)->window;
557 if (s < 0 || s >= slt->ctrl->maxio)
559 slt->io[s] = *((struct io_desc *)data);
560 /* XXX Don't actually map */
564 * Set memory window flags for read/write interface.
567 slt->rwmem = *(int *)data;
570 * Set the memory window to be used for the read/write interface.
573 if (*(unsigned long *)data == 0) {
574 *(unsigned long *)data = pccard_mem;
580 * Validate the memory by checking it against the I/O
581 * memory range. It must also start on an aligned block size.
583 if (*(unsigned long *)data & (PCCARD_MEMSIZE-1))
585 pccarddev = PCCARD_DEV2SOFTC(dev)->dev;
587 addr = *(unsigned long *)data;
589 bus_release_resource(pccarddev, SYS_RES_MEMORY,
590 pccard_mem_rid, pccard_mem_res);
591 pccard_mem_res = bus_alloc_resource(pccarddev, SYS_RES_MEMORY,
592 &pccard_mem_rid, addr, addr, PCCARD_MEMSIZE,
593 RF_ACTIVE | rman_make_alignment_flags(PCCARD_MEMSIZE));
594 if (pccard_mem_res == NULL)
596 pccard_mem = rman_get_start(pccard_mem_res);
597 pccard_kmem = rman_get_virtual(pccard_mem_res);
603 slt->pwr = *(struct power *)data;
604 return (slt->ctrl->power(slt));
606 * Allocate a driver to this slot.
611 err = allocate_driver(slt, (struct dev_desc *)data);
613 pccard_success_beep();
615 pccard_failure_beep();
618 * Virtual removal/insertion
621 pwval = *(int *)data;
623 if (slt->state != filled)
625 pccard_event(slt, card_deactivated);
627 if (slt->state != empty && slt->state != inactive)
629 pccard_event(slt, card_inserted);
633 if (pccard_beep_select(*(int *)data)) {
642 * poll - Poll on exceptions will return true
643 * when a change in card status occurs.
646 crdpoll(dev_t dev, int events, d_thread_t *td)
650 struct slot *slt = PCCARD_DEV2SOFTC(dev);
652 if (events & (POLLIN | POLLRDNORM))
653 revents |= events & (POLLIN | POLLRDNORM);
655 if (events & (POLLOUT | POLLWRNORM))
656 revents |= events & (POLLIN | POLLRDNORM);
660 * select for exception - card event.
662 if (events & POLLRDBAND)
663 if (slt == 0 || slt->laststate != slt->state)
664 revents |= POLLRDBAND;
667 selrecord(td, &slt->selp);
674 * APM hooks for suspending and resuming.
677 pccard_suspend(device_t dev)
679 struct slot *slt = PCCARD_DEVICE2SOFTC(dev);
681 /* This code stolen from pccard_event:card_removed */
682 if (slt->state == filled) {
683 int s = splhigh(); /* nop on current */
685 slt->laststate = suspend; /* for pccardd */
688 printf("pccard: card disabled, slot %d\n", slt->slotnum);
691 * Disable any pending timeouts for this slot since we're
692 * powering it down/disabling now.
694 untimeout(power_off_slot, (caddr_t)slt, slt->poff_ch);
695 slt->ctrl->disable(slt);
700 pccard_resume(device_t dev)
702 struct slot *slt = PCCARD_DEVICE2SOFTC(dev);
704 slt->ctrl->resume(slt);