2 * PCMCIA 16-bit resource management functions
4 * The initial developer of the original code is David A. Hinds
5 * <dahinds@users.sourceforge.net>. Portions created by David A. Hinds
6 * are Copyright (C) 1999 David A. Hinds. All Rights Reserved.
8 * Copyright (C) 1999 David A. Hinds
9 * Copyright (C) 2004-2005 Dominik Brodowski
11 * This program is free software; you can redistribute it and/or modify
12 * it under the terms of the GNU General Public License version 2 as
13 * published by the Free Software Foundation.
17 #include <linux/module.h>
18 #include <linux/kernel.h>
19 #include <linux/interrupt.h>
20 #include <linux/delay.h>
21 #include <linux/pci.h>
22 #include <linux/device.h>
23 #include <linux/netdevice.h>
24 #include <linux/slab.h>
28 #include <pcmcia/cs_types.h>
29 #include <pcmcia/ss.h>
30 #include <pcmcia/cs.h>
31 #include <pcmcia/cistpl.h>
32 #include <pcmcia/cisreg.h>
33 #include <pcmcia/ds.h>
35 #include "cs_internal.h"
38 /* Access speed for IO windows */
40 module_param(io_speed, int, 0444);
43 int pcmcia_validate_mem(struct pcmcia_socket *s)
45 if (s->resource_ops->validate_mem)
46 return s->resource_ops->validate_mem(s);
47 /* if there is no callback, we can assume that everything is OK */
51 struct resource *pcmcia_find_mem_region(u_long base, u_long num, u_long align,
52 int low, struct pcmcia_socket *s)
54 if (s->resource_ops->find_mem)
55 return s->resource_ops->find_mem(base, num, align, low, s);
62 * Special stuff for managing IO windows, because they are scarce
65 static int alloc_io_space(struct pcmcia_socket *s, u_int attr,
66 unsigned int *base, unsigned int num, u_int lines)
70 align = (*base) ? (lines ? 1<<lines : 0) : 1;
71 if (align && (align < num)) {
73 dev_dbg(&s->dev, "odd IO request: num %#x align %#x\n",
77 while (align && (align < num))
80 if (*base & ~(align-1)) {
81 dev_dbg(&s->dev, "odd IO request: base %#x align %#x\n",
86 return s->resource_ops->find_io(s, attr, base, num, align);
87 } /* alloc_io_space */
90 static void release_io_space(struct pcmcia_socket *s, unsigned int base,
95 for (i = 0; i < MAX_IO_WIN; i++) {
98 if ((s->io[i].res->start <= base) &&
99 (s->io[i].res->end >= base+num-1)) {
100 s->io[i].InUse -= num;
101 /* Free the window if no one else is using it */
102 if (s->io[i].InUse == 0) {
103 release_resource(s->io[i].res);
109 } /* release_io_space */
112 /** pccard_access_configuration_register
114 * Access_configuration_register() reads and writes configuration
115 * registers in attribute memory. Memory window 0 is reserved for
116 * this and the tuple reading services.
119 int pcmcia_access_configuration_register(struct pcmcia_device *p_dev,
122 struct pcmcia_socket *s;
127 if (!p_dev || !p_dev->function_config)
132 mutex_lock(&s->ops_mutex);
133 c = p_dev->function_config;
135 if (!(c->state & CONFIG_LOCKED)) {
136 dev_dbg(&s->dev, "Configuration isnt't locked\n");
137 mutex_unlock(&s->ops_mutex);
141 addr = (c->ConfigBase + reg->Offset) >> 1;
142 mutex_unlock(&s->ops_mutex);
144 switch (reg->Action) {
146 pcmcia_read_cis_mem(s, 1, addr, 1, &val);
151 pcmcia_write_cis_mem(s, 1, addr, 1, &val);
154 dev_dbg(&s->dev, "Invalid conf register request\n");
159 } /* pcmcia_access_configuration_register */
160 EXPORT_SYMBOL(pcmcia_access_configuration_register);
163 int pcmcia_map_mem_page(struct pcmcia_device *p_dev, window_handle_t wh,
166 struct pcmcia_socket *s = p_dev->socket;
172 if (req->Page != 0) {
173 dev_dbg(&s->dev, "failure: requested page is zero\n");
176 mutex_lock(&s->ops_mutex);
177 s->win[wh].card_start = req->CardOffset;
178 ret = s->ops->set_mem_map(s, &s->win[wh]);
180 dev_warn(&s->dev, "failed to set_mem_map\n");
181 mutex_unlock(&s->ops_mutex);
183 } /* pcmcia_map_mem_page */
184 EXPORT_SYMBOL(pcmcia_map_mem_page);
187 /** pcmcia_modify_configuration
189 * Modify a locked socket configuration
191 int pcmcia_modify_configuration(struct pcmcia_device *p_dev,
194 struct pcmcia_socket *s;
200 mutex_lock(&s->ops_mutex);
201 c = p_dev->function_config;
203 if (!(s->state & SOCKET_PRESENT)) {
204 dev_dbg(&s->dev, "No card present\n");
208 if (!(c->state & CONFIG_LOCKED)) {
209 dev_dbg(&s->dev, "Configuration isnt't locked\n");
214 if (mod->Attributes & (CONF_IRQ_CHANGE_VALID | CONF_VCC_CHANGE_VALID)) {
216 "changing Vcc or IRQ is not allowed at this time\n");
221 /* We only allow changing Vpp1 and Vpp2 to the same value */
222 if ((mod->Attributes & CONF_VPP1_CHANGE_VALID) &&
223 (mod->Attributes & CONF_VPP2_CHANGE_VALID)) {
224 if (mod->Vpp1 != mod->Vpp2) {
225 dev_dbg(&s->dev, "Vpp1 and Vpp2 must be the same\n");
229 s->socket.Vpp = mod->Vpp1;
230 if (s->ops->set_socket(s, &s->socket)) {
231 dev_printk(KERN_WARNING, &s->dev,
232 "Unable to set VPP\n");
236 } else if ((mod->Attributes & CONF_VPP1_CHANGE_VALID) ||
237 (mod->Attributes & CONF_VPP2_CHANGE_VALID)) {
238 dev_dbg(&s->dev, "changing Vcc is not allowed at this time\n");
243 if (mod->Attributes & CONF_IO_CHANGE_WIDTH) {
244 pccard_io_map io_off = { 0, 0, 0, 0, 1 };
248 io_on.speed = io_speed;
249 for (i = 0; i < MAX_IO_WIN; i++) {
255 io_on.flags = MAP_ACTIVE | IO_DATA_PATH_WIDTH_8;
256 io_on.start = s->io[i].res->start;
257 io_on.stop = s->io[i].res->end;
259 s->ops->set_io_map(s, &io_off);
261 s->ops->set_io_map(s, &io_on);
266 mutex_unlock(&s->ops_mutex);
269 } /* modify_configuration */
270 EXPORT_SYMBOL(pcmcia_modify_configuration);
273 int pcmcia_release_configuration(struct pcmcia_device *p_dev)
275 pccard_io_map io = { 0, 0, 0, 0, 1 };
276 struct pcmcia_socket *s = p_dev->socket;
280 mutex_lock(&s->ops_mutex);
281 c = p_dev->function_config;
282 if (p_dev->_locked) {
284 if (--(s->lock_count) == 0) {
285 s->socket.flags = SS_OUTPUT_ENA; /* Is this correct? */
287 s->socket.io_irq = 0;
288 s->ops->set_socket(s, &s->socket);
291 if (c->state & CONFIG_LOCKED) {
292 c->state &= ~CONFIG_LOCKED;
293 if (c->state & CONFIG_IO_REQ)
294 for (i = 0; i < MAX_IO_WIN; i++) {
298 if (s->io[i].Config != 0)
301 s->ops->set_io_map(s, &io);
304 mutex_unlock(&s->ops_mutex);
307 } /* pcmcia_release_configuration */
310 /** pcmcia_release_io
312 * Release_io() releases the I/O ranges allocated by a client. This
313 * may be invoked some time after a card ejection has already dumped
314 * the actual socket configuration, so if the client is "stale", we
315 * don't bother checking the port ranges against the current socket
318 static int pcmcia_release_io(struct pcmcia_device *p_dev, io_req_t *req)
320 struct pcmcia_socket *s = p_dev->socket;
324 mutex_lock(&s->ops_mutex);
325 c = p_dev->function_config;
332 if ((c->io.BasePort1 != req->BasePort1) ||
333 (c->io.NumPorts1 != req->NumPorts1) ||
334 (c->io.BasePort2 != req->BasePort2) ||
335 (c->io.NumPorts2 != req->NumPorts2))
338 c->state &= ~CONFIG_IO_REQ;
340 release_io_space(s, req->BasePort1, req->NumPorts1);
342 release_io_space(s, req->BasePort2, req->NumPorts2);
345 mutex_unlock(&s->ops_mutex);
348 } /* pcmcia_release_io */
351 int pcmcia_release_window(struct pcmcia_device *p_dev, window_handle_t wh)
353 struct pcmcia_socket *s = p_dev->socket;
360 mutex_lock(&s->ops_mutex);
363 if (!(p_dev->_win & CLIENT_WIN_REQ(wh))) {
364 dev_dbg(&s->dev, "not releasing unknown window\n");
365 mutex_unlock(&s->ops_mutex);
369 /* Shut down memory window */
370 win->flags &= ~MAP_ACTIVE;
371 s->ops->set_mem_map(s, win);
372 s->state &= ~SOCKET_WIN_REQ(wh);
374 /* Release system memory */
376 release_resource(win->res);
380 p_dev->_win &= ~CLIENT_WIN_REQ(wh);
381 mutex_unlock(&s->ops_mutex);
384 } /* pcmcia_release_window */
385 EXPORT_SYMBOL(pcmcia_release_window);
388 int pcmcia_request_configuration(struct pcmcia_device *p_dev,
393 struct pcmcia_socket *s = p_dev->socket;
397 if (!(s->state & SOCKET_PRESENT))
400 if (req->IntType & INT_CARDBUS) {
401 dev_dbg(&s->dev, "IntType may not be INT_CARDBUS\n");
405 mutex_lock(&s->ops_mutex);
406 c = p_dev->function_config;
407 if (c->state & CONFIG_LOCKED) {
408 mutex_unlock(&s->ops_mutex);
409 dev_dbg(&s->dev, "Configuration is locked\n");
413 /* Do power control. We don't allow changes in Vcc. */
414 s->socket.Vpp = req->Vpp;
415 if (s->ops->set_socket(s, &s->socket)) {
416 mutex_unlock(&s->ops_mutex);
417 dev_printk(KERN_WARNING, &s->dev,
418 "Unable to set socket state\n");
422 /* Pick memory or I/O card, DMA mode, interrupt */
423 c->IntType = req->IntType;
424 c->Attributes = req->Attributes;
425 if (req->IntType & INT_MEMORY_AND_IO)
426 s->socket.flags |= SS_IOCARD;
427 if (req->IntType & INT_ZOOMED_VIDEO)
428 s->socket.flags |= SS_ZVCARD | SS_IOCARD;
429 if (req->Attributes & CONF_ENABLE_DMA)
430 s->socket.flags |= SS_DMA_MODE;
431 if (req->Attributes & CONF_ENABLE_SPKR)
432 s->socket.flags |= SS_SPKR_ENA;
433 if (req->Attributes & CONF_ENABLE_IRQ)
434 s->socket.io_irq = s->pcmcia_irq;
436 s->socket.io_irq = 0;
437 s->ops->set_socket(s, &s->socket);
439 mutex_unlock(&s->ops_mutex);
441 /* Set up CIS configuration registers */
442 base = c->ConfigBase = req->ConfigBase;
443 c->CardValues = req->Present;
444 if (req->Present & PRESENT_COPY) {
446 pcmcia_write_cis_mem(s, 1, (base + CISREG_SCR)>>1, 1, &c->Copy);
448 if (req->Present & PRESENT_OPTION) {
449 if (s->functions == 1) {
450 c->Option = req->ConfigIndex & COR_CONFIG_MASK;
452 c->Option = req->ConfigIndex & COR_MFC_CONFIG_MASK;
453 c->Option |= COR_FUNC_ENA|COR_IREQ_ENA;
454 if (req->Present & PRESENT_IOBASE_0)
455 c->Option |= COR_ADDR_DECODE;
457 if ((req->Attributes & CONF_ENABLE_IRQ) &&
458 !(req->Attributes & CONF_ENABLE_PULSE_IRQ))
459 c->Option |= COR_LEVEL_REQ;
460 pcmcia_write_cis_mem(s, 1, (base + CISREG_COR)>>1, 1, &c->Option);
463 if (req->Present & PRESENT_STATUS) {
464 c->Status = req->Status;
465 pcmcia_write_cis_mem(s, 1, (base + CISREG_CCSR)>>1, 1, &c->Status);
467 if (req->Present & PRESENT_PIN_REPLACE) {
469 pcmcia_write_cis_mem(s, 1, (base + CISREG_PRR)>>1, 1, &c->Pin);
471 if (req->Present & PRESENT_EXT_STATUS) {
472 c->ExtStatus = req->ExtStatus;
473 pcmcia_write_cis_mem(s, 1, (base + CISREG_ESR)>>1, 1, &c->ExtStatus);
475 if (req->Present & PRESENT_IOBASE_0) {
476 u_char b = c->io.BasePort1 & 0xff;
477 pcmcia_write_cis_mem(s, 1, (base + CISREG_IOBASE_0)>>1, 1, &b);
478 b = (c->io.BasePort1 >> 8) & 0xff;
479 pcmcia_write_cis_mem(s, 1, (base + CISREG_IOBASE_1)>>1, 1, &b);
481 if (req->Present & PRESENT_IOSIZE) {
482 u_char b = c->io.NumPorts1 + c->io.NumPorts2 - 1;
483 pcmcia_write_cis_mem(s, 1, (base + CISREG_IOSIZE)>>1, 1, &b);
486 /* Configure I/O windows */
487 if (c->state & CONFIG_IO_REQ) {
488 mutex_lock(&s->ops_mutex);
489 iomap.speed = io_speed;
490 for (i = 0; i < MAX_IO_WIN; i++)
493 iomap.flags = MAP_ACTIVE;
494 switch (s->io[i].res->flags & IO_DATA_PATH_WIDTH) {
495 case IO_DATA_PATH_WIDTH_16:
496 iomap.flags |= MAP_16BIT; break;
497 case IO_DATA_PATH_WIDTH_AUTO:
498 iomap.flags |= MAP_AUTOSZ; break;
502 iomap.start = s->io[i].res->start;
503 iomap.stop = s->io[i].res->end;
504 s->ops->set_io_map(s, &iomap);
507 mutex_unlock(&s->ops_mutex);
510 c->state |= CONFIG_LOCKED;
513 } /* pcmcia_request_configuration */
514 EXPORT_SYMBOL(pcmcia_request_configuration);
517 /** pcmcia_request_io
519 * Request_io() reserves ranges of port addresses for a socket.
520 * I have not implemented range sharing or alias addressing.
522 int pcmcia_request_io(struct pcmcia_device *p_dev, io_req_t *req)
524 struct pcmcia_socket *s = p_dev->socket;
528 mutex_lock(&s->ops_mutex);
530 if (!(s->state & SOCKET_PRESENT)) {
531 dev_dbg(&s->dev, "No card present\n");
538 c = p_dev->function_config;
539 if (c->state & CONFIG_LOCKED) {
540 dev_dbg(&s->dev, "Configuration is locked\n");
543 if (c->state & CONFIG_IO_REQ) {
544 dev_dbg(&s->dev, "IO already configured\n");
547 if (req->Attributes1 & (IO_SHARED | IO_FORCE_ALIAS_ACCESS)) {
548 dev_dbg(&s->dev, "bad attribute setting for IO region 1\n");
551 if ((req->NumPorts2 > 0) &&
552 (req->Attributes2 & (IO_SHARED | IO_FORCE_ALIAS_ACCESS))) {
553 dev_dbg(&s->dev, "bad attribute setting for IO region 2\n");
557 dev_dbg(&s->dev, "trying to allocate resource 1\n");
558 ret = alloc_io_space(s, req->Attributes1, &req->BasePort1,
559 req->NumPorts1, req->IOAddrLines);
561 dev_dbg(&s->dev, "allocation of resource 1 failed\n");
565 if (req->NumPorts2) {
566 dev_dbg(&s->dev, "trying to allocate resource 2\n");
567 ret = alloc_io_space(s, req->Attributes2, &req->BasePort2,
568 req->NumPorts2, req->IOAddrLines);
570 dev_dbg(&s->dev, "allocation of resource 2 failed\n");
571 release_io_space(s, req->BasePort1, req->NumPorts1);
577 c->state |= CONFIG_IO_REQ;
579 dev_dbg(&s->dev, "allocating resources succeeded: %d\n", ret);
582 mutex_unlock(&s->ops_mutex);
585 } /* pcmcia_request_io */
586 EXPORT_SYMBOL(pcmcia_request_io);
590 * pcmcia_request_irq() - attempt to request a IRQ for a PCMCIA device
592 * pcmcia_request_irq() is a wrapper around request_irq which will allow
593 * the PCMCIA core to clean up the registration in pcmcia_disable_device().
594 * Drivers are free to use request_irq() directly, but then they need to
595 * call free_irq themselfves, too. Also, only IRQF_SHARED capable IRQ
596 * handlers are allowed.
598 int __must_check pcmcia_request_irq(struct pcmcia_device *p_dev,
599 irq_handler_t handler)
606 ret = request_irq(p_dev->irq, handler, IRQF_SHARED,
607 p_dev->devname, p_dev->priv);
613 EXPORT_SYMBOL(pcmcia_request_irq);
617 * pcmcia_request_exclusive_irq() - attempt to request an exclusive IRQ first
619 * pcmcia_request_exclusive_irq() is a wrapper around request_irq which
620 * attempts first to request an exclusive IRQ. If it fails, it also accepts
621 * a shared IRQ, but prints out a warning. PCMCIA drivers should allow for
622 * IRQ sharing and either use request_irq directly (then they need to call
623 * free_irq themselves, too), or the pcmcia_request_irq() function.
626 __pcmcia_request_exclusive_irq(struct pcmcia_device *p_dev,
627 irq_handler_t handler)
634 ret = request_irq(p_dev->irq, handler, 0, p_dev->devname, p_dev->priv);
636 ret = pcmcia_request_irq(p_dev, handler);
637 dev_printk(KERN_WARNING, &p_dev->dev, "pcmcia: "
638 "request for exclusive IRQ could not be fulfilled.\n");
639 dev_printk(KERN_WARNING, &p_dev->dev, "pcmcia: the driver "
640 "needs updating to supported shared IRQ lines.\n");
643 dev_printk(KERN_INFO, &p_dev->dev, "request_irq() failed\n");
648 } /* pcmcia_request_exclusive_irq */
649 EXPORT_SYMBOL(__pcmcia_request_exclusive_irq);
652 #ifdef CONFIG_PCMCIA_PROBE
654 /* mask of IRQs already reserved by other cards, we should avoid using them */
655 static u8 pcmcia_used_irq[NR_IRQS];
657 static irqreturn_t test_action(int cpl, void *dev_id)
663 * pcmcia_setup_isa_irq() - determine whether an ISA IRQ can be used
664 * @p_dev - the associated PCMCIA device
666 * locking note: must be called with ops_mutex locked.
668 static int pcmcia_setup_isa_irq(struct pcmcia_device *p_dev, int type)
670 struct pcmcia_socket *s = p_dev->socket;
671 unsigned int try, irq;
672 u32 mask = s->irq_mask;
675 for (try = 0; try < 64; try++) {
678 /* marked as available by driver, not blocked by userspace? */
679 if (!((mask >> irq) & 1))
682 /* avoid an IRQ which is already used by another PCMCIA card */
683 if ((try < 32) && pcmcia_used_irq[irq])
686 /* register the correct driver, if possible, to check whether
687 * registering a dummy handle works, i.e. if the IRQ isn't
688 * marked as used by the kernel resource management core */
689 ret = request_irq(irq, test_action, type, p_dev->devname,
692 free_irq(irq, p_dev);
693 p_dev->irq = s->pcmcia_irq = irq;
694 pcmcia_used_irq[irq]++;
702 void pcmcia_cleanup_irq(struct pcmcia_socket *s)
704 pcmcia_used_irq[s->pcmcia_irq]--;
708 #else /* CONFIG_PCMCIA_PROBE */
710 static int pcmcia_setup_isa_irq(struct pcmcia_device *p_dev, int type)
715 void pcmcia_cleanup_irq(struct pcmcia_socket *s)
721 #endif /* CONFIG_PCMCIA_PROBE */
725 * pcmcia_setup_irq() - determine IRQ to be used for device
726 * @p_dev - the associated PCMCIA device
728 * locking note: must be called with ops_mutex locked.
730 int pcmcia_setup_irq(struct pcmcia_device *p_dev)
732 struct pcmcia_socket *s = p_dev->socket;
737 /* already assigned? */
739 p_dev->irq = s->pcmcia_irq;
743 /* prefer an exclusive ISA irq */
744 if (!pcmcia_setup_isa_irq(p_dev, 0))
747 /* but accept a shared ISA irq */
748 if (!pcmcia_setup_isa_irq(p_dev, IRQF_SHARED))
751 /* but use the PCI irq otherwise */
753 p_dev->irq = s->pcmcia_irq = s->pci_irq;
761 /** pcmcia_request_window
763 * Request_window() establishes a mapping between card memory space
764 * and system memory space.
766 int pcmcia_request_window(struct pcmcia_device *p_dev, win_req_t *req, window_handle_t *wh)
768 struct pcmcia_socket *s = p_dev->socket;
773 if (!(s->state & SOCKET_PRESENT)) {
774 dev_dbg(&s->dev, "No card present\n");
777 if (req->Attributes & (WIN_PAGED | WIN_SHARED)) {
778 dev_dbg(&s->dev, "bad attribute setting for iomem region\n");
782 /* Window size defaults to smallest available */
784 req->Size = s->map_size;
785 align = (((s->features & SS_CAP_MEM_ALIGN) ||
786 (req->Attributes & WIN_STRICT_ALIGN)) ?
787 req->Size : s->map_size);
788 if (req->Size & (s->map_size-1)) {
789 dev_dbg(&s->dev, "invalid map size\n");
792 if ((req->Base && (s->features & SS_CAP_STATIC_MAP)) ||
793 (req->Base & (align-1))) {
794 dev_dbg(&s->dev, "invalid base address\n");
800 /* Allocate system memory window */
801 for (w = 0; w < MAX_WIN; w++)
802 if (!(s->state & SOCKET_WIN_REQ(w)))
805 dev_dbg(&s->dev, "all windows are used already\n");
809 mutex_lock(&s->ops_mutex);
812 if (!(s->features & SS_CAP_STATIC_MAP)) {
813 win->res = pcmcia_find_mem_region(req->Base, req->Size, align,
814 (req->Attributes & WIN_MAP_BELOW_1MB), s);
816 dev_dbg(&s->dev, "allocating mem region failed\n");
817 mutex_unlock(&s->ops_mutex);
821 p_dev->_win |= CLIENT_WIN_REQ(w);
823 /* Configure the socket controller */
826 win->speed = req->AccessSpeed;
827 if (req->Attributes & WIN_MEMORY_TYPE)
828 win->flags |= MAP_ATTRIB;
829 if (req->Attributes & WIN_ENABLE)
830 win->flags |= MAP_ACTIVE;
831 if (req->Attributes & WIN_DATA_WIDTH_16)
832 win->flags |= MAP_16BIT;
833 if (req->Attributes & WIN_USE_WAIT)
834 win->flags |= MAP_USE_WAIT;
837 if (s->ops->set_mem_map(s, win) != 0) {
838 dev_dbg(&s->dev, "failed to set memory mapping\n");
839 mutex_unlock(&s->ops_mutex);
842 s->state |= SOCKET_WIN_REQ(w);
844 /* Return window handle */
845 if (s->features & SS_CAP_STATIC_MAP)
846 req->Base = win->static_start;
848 req->Base = win->res->start;
850 mutex_unlock(&s->ops_mutex);
854 } /* pcmcia_request_window */
855 EXPORT_SYMBOL(pcmcia_request_window);
857 void pcmcia_disable_device(struct pcmcia_device *p_dev)
859 pcmcia_release_configuration(p_dev);
860 pcmcia_release_io(p_dev, &p_dev->io);
862 free_irq(p_dev->irq, p_dev->priv);
864 pcmcia_release_window(p_dev, p_dev->win);
866 EXPORT_SYMBOL(pcmcia_disable_device);