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.
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 devi->manufacturer = desc->manufacturer;
228 devi->product = desc->product;
229 devi->prodext = desc->prodext;
230 resource_list_init(&devi->resources);
231 child = device_add_child(pccarddev, devi->name, desc->unit);
233 if (desc->unit != -1)
234 device_printf(pccarddev,
235 "Unit %d failed for %s, try a different unit\n",
236 desc->unit, devi->name);
238 device_printf(pccarddev,
239 "No units available for %s. Impossible?\n",
243 device_set_flags(child, desc->flags);
244 device_set_ivars(child, devi);
246 device_printf(pccarddev,
247 "Assigning %s: io 0x%x-0x%x irq %d mem 0x%lx-0x%lx\n",
248 device_get_nameunit(child),
249 desc->iobase, desc->iobase + desc->iosize - 1,
250 irq, desc->mem, desc->mem + desc->memsize - 1);
252 err = bus_set_resource(child, SYS_RES_IOPORT, 0, desc->iobase,
257 err = bus_set_resource(child, SYS_RES_IRQ, 0, irq, 1);
261 err = bus_set_resource(child, SYS_RES_MEMORY, 0, desc->mem,
266 err = device_probe_and_attach(child);
268 * XXX We unwisely assume that the detach code won't run while the
269 * XXX the attach code is attaching. Someone should put some
270 * XXX interlock code. This can happen if probe/attach takes a while
271 * XXX and the user ejects the card, which causes the detach
272 * XXX function to be called.
274 strncpy(desc->name, device_get_nameunit(child), sizeof(desc->name));
275 desc->name[sizeof(desc->name) - 1] = '\0';
278 device_delete_child(pccarddev, child);
283 * card insert routine - Called from a timeout to debounce
289 struct slot *slt = arg;
293 * Enable 5V to the card so that the CIS can be read.
299 * Disable any pending timeouts for this slot, and explicitly
300 * power it off right now. Then, re-enable the power using
301 * the (possibly new) power settings.
303 untimeout(power_off_slot, (caddr_t)slt, slt->poff_ch);
305 slt->ctrl->power(slt);
307 printf("pccard: card inserted, slot %d\n", slt->slotnum);
308 pccard_insert_beep();
310 * Now start resetting the card.
312 slt->ctrl->reset(slt);
316 * Card event callback. Called at splhigh to prevent
317 * device interrupts from interceding.
320 pccard_event(struct slot *slt, enum card_event event)
322 if (slt->insert_seq) {
324 untimeout(inserted, (void *)slt, slt->insert_ch);
329 case card_deactivated:
330 if (slt->state == filled || slt->state == inactive) {
331 if (event == card_removed)
334 slt->state = inactive;
335 disable_slot_to(slt);
340 slt->insert_ch = timeout(inserted, (void *)slt, hz/4);
346 * Device driver interface.
349 crdopen(dev_t dev, int oflags, int devtype, d_thread_t *td)
351 struct slot *slt = PCCARD_DEV2SOFTC(dev);
356 slt->rwmem = MDF_ATTR;
361 * Close doesn't de-allocate any resources, since
362 * slots may be assigned to drivers already.
365 crdclose(dev_t dev, int fflag, int devtype, d_thread_t *td)
371 * read interface. Map memory at lseek offset,
372 * then transfer to user space.
375 crdread(dev_t dev, struct uio *uio, int ioflag)
377 struct slot *slt = PCCARD_DEV2SOFTC(dev);
378 struct mem_desc *mp, oldmap;
381 int error = 0, win, count;
383 if (slt == 0 || slt->state != filled)
387 for (win = 0; win < slt->ctrl->maxmem; win++)
388 if ((slt->mem[win].flags & MDF_ACTIVE) == 0)
390 if (win >= slt->ctrl->maxmem)
394 mp->flags = slt->rwmem | MDF_ACTIVE;
395 while (uio->uio_resid && error == 0) {
396 mp->card = uio->uio_offset;
397 mp->size = PCCARD_MEMSIZE;
398 mp->start = (caddr_t)(void *)(uintptr_t)pccard_mem;
399 if ((error = slt->ctrl->mapmem(slt, win)) != 0)
401 offs = (unsigned int)uio->uio_offset & (PCCARD_MEMSIZE - 1);
402 p = pccard_kmem + offs;
403 count = MIN(PCCARD_MEMSIZE - offs, uio->uio_resid);
404 error = uiomove(p, count, uio);
407 * Restore original map.
410 slt->ctrl->mapmem(slt, win);
416 * crdwrite - Write data to card memory.
417 * Handles wrap around so that only one memory
421 crdwrite(dev_t dev, struct uio *uio, int ioflag)
423 struct slot *slt = PCCARD_DEV2SOFTC(dev);
424 struct mem_desc *mp, oldmap;
427 int error = 0, win, count;
429 if (slt == 0 || slt->state != filled)
433 for (win = 0; win < slt->ctrl->maxmem; win++)
434 if ((slt->mem[win].flags & MDF_ACTIVE) == 0)
436 if (win >= slt->ctrl->maxmem)
440 mp->flags = slt->rwmem | MDF_ACTIVE;
441 while (uio->uio_resid && error == 0) {
442 mp->card = uio->uio_offset;
443 mp->size = PCCARD_MEMSIZE;
444 mp->start = (caddr_t)(void *)(uintptr_t)pccard_mem;
445 if ((error = slt->ctrl->mapmem(slt, win)) != 0)
447 offs = (unsigned int)uio->uio_offset & (PCCARD_MEMSIZE - 1);
448 p = pccard_kmem + offs;
449 count = MIN(PCCARD_MEMSIZE - offs, uio->uio_resid);
450 error = uiomove(p, count, uio);
453 * Restore original map.
456 slt->ctrl->mapmem(slt, win);
462 * ioctl calls - allows setting/getting of memory and I/O
463 * descriptors, and assignment of drivers.
466 crdioctl(dev_t dev, u_long cmd, caddr_t data, int fflag, d_thread_t *td)
475 struct slot *slt = PCCARD_DEV2SOFTC(dev);
477 if (slt == 0 && cmd != PIOCRWMEM)
481 if (slt->ctrl->ioctl)
482 return (slt->ctrl->ioctl(slt, cmd, data));
489 ((struct slotstate *)data)->state = slt->state;
490 ((struct slotstate *)data)->laststate = slt->laststate;
491 slt->laststate = slt->state;
493 ((struct slotstate *)data)->maxmem = slt->ctrl->maxmem;
494 ((struct slotstate *)data)->maxio = slt->ctrl->maxio;
495 ((struct slotstate *)data)->irqs = 0;
498 * Get memory context.
501 s = ((struct mem_desc *)data)->window;
502 if (s < 0 || s >= slt->ctrl->maxmem)
505 ((struct mem_desc *)data)->flags = mp->flags;
506 ((struct mem_desc *)data)->start = mp->start;
507 ((struct mem_desc *)data)->size = mp->size;
508 ((struct mem_desc *)data)->card = mp->card;
511 * Set memory context. If context already active, then unmap it.
512 * It is hard to see how the parameters can be checked.
513 * At the very least, we only allow root to set the context.
518 if (slt->state != filled)
520 s = ((struct mem_desc *)data)->window;
521 if (s < 0 || s >= slt->ctrl->maxmem)
523 slt->mem[s] = *((struct mem_desc *)data);
524 return (slt->ctrl->mapmem(slt, s));
526 * Get I/O port context.
529 s = ((struct io_desc *)data)->window;
530 if (s < 0 || s >= slt->ctrl->maxio)
533 ((struct io_desc *)data)->flags = ip->flags;
534 ((struct io_desc *)data)->start = ip->start;
535 ((struct io_desc *)data)->size = ip->size;
538 * Set I/O port context.
543 if (slt->state != filled)
545 s = ((struct io_desc *)data)->window;
546 if (s < 0 || s >= slt->ctrl->maxio)
548 slt->io[s] = *((struct io_desc *)data);
549 /* XXX Don't actually map */
553 * Set memory window flags for read/write interface.
556 slt->rwmem = *(int *)data;
559 * Set the memory window to be used for the read/write interface.
560 * Not available on the alpha.
563 if (*(unsigned long *)data == 0) {
564 *(unsigned long *)data = pccard_mem;
570 * Validate the memory by checking it against the I/O
571 * memory range. It must also start on an aligned block size.
573 if (*(unsigned long *)data & (PCCARD_MEMSIZE-1))
575 pccarddev = PCCARD_DEV2SOFTC(dev)->dev;
577 addr = *(unsigned long *)data;
579 bus_release_resource(pccarddev, SYS_RES_MEMORY,
580 pccard_mem_rid, pccard_mem_res);
581 pccard_mem_res = bus_alloc_resource(pccarddev, SYS_RES_MEMORY,
582 &pccard_mem_rid, addr, addr, PCCARD_MEMSIZE,
583 RF_ACTIVE | rman_make_alignment_flags(PCCARD_MEMSIZE));
584 if (pccard_mem_res == NULL)
586 pccard_mem = rman_get_start(pccard_mem_res);
587 pccard_kmem = rman_get_virtual(pccard_mem_res);
593 slt->pwr = *(struct power *)data;
594 return (slt->ctrl->power(slt));
596 * Allocate a driver to this slot.
601 err = allocate_driver(slt, (struct dev_desc *)data);
603 pccard_success_beep();
605 pccard_failure_beep();
608 * Virtual removal/insertion
611 pwval = *(int *)data;
613 if (slt->state != filled)
615 pccard_event(slt, card_deactivated);
617 if (slt->state != empty && slt->state != inactive)
619 pccard_event(slt, card_inserted);
623 if (pccard_beep_select(*(int *)data)) {
632 * poll - Poll on exceptions will return true
633 * when a change in card status occurs.
636 crdpoll(dev_t dev, int events, d_thread_t *td)
640 struct slot *slt = PCCARD_DEV2SOFTC(dev);
642 if (events & (POLLIN | POLLRDNORM))
643 revents |= events & (POLLIN | POLLRDNORM);
645 if (events & (POLLOUT | POLLWRNORM))
646 revents |= events & (POLLIN | POLLRDNORM);
650 * select for exception - card event.
652 if (events & POLLRDBAND)
653 if (slt == 0 || slt->laststate != slt->state)
654 revents |= POLLRDBAND;
657 selrecord(td, &slt->selp);
664 * APM hooks for suspending and resuming.
667 pccard_suspend(device_t dev)
669 struct slot *slt = PCCARD_DEVICE2SOFTC(dev);
671 /* This code stolen from pccard_event:card_removed */
672 if (slt->state == filled) {
673 int s = splhigh(); /* nop on current */
675 slt->laststate = suspend; /* for pccardd */
678 printf("pccard: card disabled, slot %d\n", slt->slotnum);
681 * Disable any pending timeouts for this slot since we're
682 * powering it down/disabling now.
684 untimeout(power_off_slot, (caddr_t)slt, slt->poff_ch);
685 slt->ctrl->disable(slt);
690 pccard_resume(device_t dev)
692 struct slot *slt = PCCARD_DEVICE2SOFTC(dev);
694 slt->ctrl->resume(slt);