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 * Disable any pending timeouts for this slot, and explicitly
294 * power it off right now. Then, re-enable the power using
295 * the (possibly new) power settings.
297 untimeout(power_off_slot, (caddr_t)slt, slt->poff_ch);
301 * Enable 5V to the card so that the CIS can be read. Well,
302 * enable the most natural voltage so that the CIS can be read.
306 slt->ctrl->power(slt);
308 printf("pccard: card inserted, slot %d\n", slt->slotnum);
309 pccard_insert_beep();
310 slt->ctrl->reset(slt);
314 * Card event callback. Called at splhigh to prevent
315 * device interrupts from interceding.
318 pccard_event(struct slot *slt, enum card_event event)
320 if (slt->insert_seq) {
322 untimeout(inserted, (void *)slt, slt->insert_ch);
327 case card_deactivated:
328 if (slt->state == filled || slt->state == inactive) {
329 if (event == card_removed)
332 slt->state = inactive;
333 disable_slot_to(slt);
338 slt->insert_ch = timeout(inserted, (void *)slt, hz/4);
344 * Device driver interface.
347 crdopen(dev_t dev, int oflags, int devtype, d_thread_t *td)
349 struct slot *slt = PCCARD_DEV2SOFTC(dev);
354 slt->rwmem = MDF_ATTR;
359 * Close doesn't de-allocate any resources, since
360 * slots may be assigned to drivers already.
363 crdclose(dev_t dev, int fflag, int devtype, d_thread_t *td)
369 * read interface. Map memory at lseek offset,
370 * then transfer to user space.
373 crdread(dev_t dev, struct uio *uio, int ioflag)
375 struct slot *slt = PCCARD_DEV2SOFTC(dev);
376 struct mem_desc *mp, oldmap;
379 int error = 0, win, count;
381 if (slt == 0 || slt->state != filled)
385 for (win = 0; win < slt->ctrl->maxmem; win++)
386 if ((slt->mem[win].flags & MDF_ACTIVE) == 0)
388 if (win >= slt->ctrl->maxmem)
392 mp->flags = slt->rwmem | MDF_ACTIVE;
393 while (uio->uio_resid && error == 0) {
394 mp->card = uio->uio_offset;
395 mp->size = PCCARD_MEMSIZE;
396 mp->start = (caddr_t)(void *)(uintptr_t)pccard_mem;
397 if ((error = slt->ctrl->mapmem(slt, win)) != 0)
399 offs = (unsigned int)uio->uio_offset & (PCCARD_MEMSIZE - 1);
400 p = pccard_kmem + offs;
401 count = MIN(PCCARD_MEMSIZE - offs, uio->uio_resid);
402 error = uiomove(p, count, uio);
405 * Restore original map.
408 slt->ctrl->mapmem(slt, win);
414 * crdwrite - Write data to card memory.
415 * Handles wrap around so that only one memory
419 crdwrite(dev_t dev, struct uio *uio, int ioflag)
421 struct slot *slt = PCCARD_DEV2SOFTC(dev);
422 struct mem_desc *mp, oldmap;
425 int error = 0, win, count;
427 if (slt == 0 || slt->state != filled)
431 for (win = 0; win < slt->ctrl->maxmem; win++)
432 if ((slt->mem[win].flags & MDF_ACTIVE) == 0)
434 if (win >= slt->ctrl->maxmem)
438 mp->flags = slt->rwmem | MDF_ACTIVE;
439 while (uio->uio_resid && error == 0) {
440 mp->card = uio->uio_offset;
441 mp->size = PCCARD_MEMSIZE;
442 mp->start = (caddr_t)(void *)(uintptr_t)pccard_mem;
443 if ((error = slt->ctrl->mapmem(slt, win)) != 0)
445 offs = (unsigned int)uio->uio_offset & (PCCARD_MEMSIZE - 1);
446 p = pccard_kmem + offs;
447 count = MIN(PCCARD_MEMSIZE - offs, uio->uio_resid);
448 error = uiomove(p, count, uio);
451 * Restore original map.
454 slt->ctrl->mapmem(slt, win);
460 * ioctl calls - allows setting/getting of memory and I/O
461 * descriptors, and assignment of drivers.
464 crdioctl(dev_t dev, u_long cmd, caddr_t data, int fflag, d_thread_t *td)
473 struct slot *slt = PCCARD_DEV2SOFTC(dev);
475 if (slt == 0 && cmd != PIOCRWMEM)
479 if (slt->ctrl->ioctl)
480 return (slt->ctrl->ioctl(slt, cmd, data));
487 ((struct slotstate *)data)->state = slt->state;
488 ((struct slotstate *)data)->laststate = slt->laststate;
489 slt->laststate = slt->state;
491 ((struct slotstate *)data)->maxmem = slt->ctrl->maxmem;
492 ((struct slotstate *)data)->maxio = slt->ctrl->maxio;
493 ((struct slotstate *)data)->irqs = 0;
496 * Get memory context.
499 s = ((struct mem_desc *)data)->window;
500 if (s < 0 || s >= slt->ctrl->maxmem)
503 ((struct mem_desc *)data)->flags = mp->flags;
504 ((struct mem_desc *)data)->start = mp->start;
505 ((struct mem_desc *)data)->size = mp->size;
506 ((struct mem_desc *)data)->card = mp->card;
509 * Set memory context. If context already active, then unmap it.
510 * It is hard to see how the parameters can be checked.
511 * At the very least, we only allow root to set the context.
516 if (slt->state != filled)
518 s = ((struct mem_desc *)data)->window;
519 if (s < 0 || s >= slt->ctrl->maxmem)
521 slt->mem[s] = *((struct mem_desc *)data);
522 return (slt->ctrl->mapmem(slt, s));
524 * Get I/O port context.
527 s = ((struct io_desc *)data)->window;
528 if (s < 0 || s >= slt->ctrl->maxio)
531 ((struct io_desc *)data)->flags = ip->flags;
532 ((struct io_desc *)data)->start = ip->start;
533 ((struct io_desc *)data)->size = ip->size;
536 * Set I/O port context.
541 if (slt->state != filled)
543 s = ((struct io_desc *)data)->window;
544 if (s < 0 || s >= slt->ctrl->maxio)
546 slt->io[s] = *((struct io_desc *)data);
547 /* XXX Don't actually map */
551 * Set memory window flags for read/write interface.
554 slt->rwmem = *(int *)data;
557 * Set the memory window to be used for the read/write interface.
560 if (*(unsigned long *)data == 0) {
561 *(unsigned long *)data = pccard_mem;
567 * Validate the memory by checking it against the I/O
568 * memory range. It must also start on an aligned block size.
570 if (*(unsigned long *)data & (PCCARD_MEMSIZE-1))
572 pccarddev = PCCARD_DEV2SOFTC(dev)->dev;
574 addr = *(unsigned long *)data;
576 bus_release_resource(pccarddev, SYS_RES_MEMORY,
577 pccard_mem_rid, pccard_mem_res);
578 pccard_mem_res = bus_alloc_resource(pccarddev, SYS_RES_MEMORY,
579 &pccard_mem_rid, addr, addr, PCCARD_MEMSIZE,
580 RF_ACTIVE | rman_make_alignment_flags(PCCARD_MEMSIZE));
581 if (pccard_mem_res == NULL)
583 pccard_mem = rman_get_start(pccard_mem_res);
584 pccard_kmem = rman_get_virtual(pccard_mem_res);
590 slt->pwr = *(struct power *)data;
591 return (slt->ctrl->power(slt));
593 * Allocate a driver to this slot.
598 err = allocate_driver(slt, (struct dev_desc *)data);
600 pccard_success_beep();
602 pccard_failure_beep();
605 * Virtual removal/insertion
608 pwval = *(int *)data;
610 if (slt->state != filled)
612 pccard_event(slt, card_deactivated);
614 if (slt->state != empty && slt->state != inactive)
616 pccard_event(slt, card_inserted);
620 if (pccard_beep_select(*(int *)data)) {
629 * poll - Poll on exceptions will return true
630 * when a change in card status occurs.
633 crdpoll(dev_t dev, int events, d_thread_t *td)
637 struct slot *slt = PCCARD_DEV2SOFTC(dev);
639 if (events & (POLLIN | POLLRDNORM))
640 revents |= events & (POLLIN | POLLRDNORM);
642 if (events & (POLLOUT | POLLWRNORM))
643 revents |= events & (POLLIN | POLLRDNORM);
647 * select for exception - card event.
649 if (events & POLLRDBAND)
650 if (slt == 0 || slt->laststate != slt->state)
651 revents |= POLLRDBAND;
654 selrecord(td, &slt->selp);
661 * APM hooks for suspending and resuming.
664 pccard_suspend(device_t dev)
666 struct slot *slt = PCCARD_DEVICE2SOFTC(dev);
668 /* This code stolen from pccard_event:card_removed */
669 if (slt->state == filled) {
670 int s = splhigh(); /* nop on current */
672 slt->laststate = suspend; /* for pccardd */
675 printf("pccard: card disabled, slot %d\n", slt->slotnum);
678 * Disable any pending timeouts for this slot since we're
679 * powering it down/disabling now.
681 untimeout(power_off_slot, (caddr_t)slt, slt->poff_ch);
682 slt->ctrl->disable(slt);
687 pccard_resume(device_t dev)
689 struct slot *slt = PCCARD_DEVICE2SOFTC(dev);
691 slt->ctrl->resume(slt);