Dominik Brodowski | 1a8d466 | 2005-06-27 16:28:53 -0700 | [diff] [blame] | 1 | /* |
| 2 | * PCMCIA 16-bit resource management functions |
| 3 | * |
| 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. |
| 7 | * |
| 8 | * Copyright (C) 1999 David A. Hinds |
Dominik Brodowski | 9485ee1 | 2010-07-31 07:09:32 +0200 | [diff] [blame] | 9 | * Copyright (C) 2004-2010 Dominik Brodowski |
Dominik Brodowski | 1a8d466 | 2005-06-27 16:28:53 -0700 | [diff] [blame] | 10 | * |
| 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. |
| 14 | * |
| 15 | */ |
| 16 | |
Dominik Brodowski | 1a8d466 | 2005-06-27 16:28:53 -0700 | [diff] [blame] | 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> |
Dominik Brodowski | 9128422 | 2009-10-18 23:32:33 +0200 | [diff] [blame] | 23 | #include <linux/netdevice.h> |
Tejun Heo | 5a0e3ad | 2010-03-24 17:04:11 +0900 | [diff] [blame] | 24 | #include <linux/slab.h> |
Dominik Brodowski | 1a8d466 | 2005-06-27 16:28:53 -0700 | [diff] [blame] | 25 | |
Dominik Brodowski | 6f0f38c | 2010-04-08 20:33:16 +0200 | [diff] [blame] | 26 | #include <asm/irq.h> |
| 27 | |
Dominik Brodowski | 1a8d466 | 2005-06-27 16:28:53 -0700 | [diff] [blame] | 28 | #include <pcmcia/ss.h> |
Dominik Brodowski | 1a8d466 | 2005-06-27 16:28:53 -0700 | [diff] [blame] | 29 | #include <pcmcia/cistpl.h> |
| 30 | #include <pcmcia/cisreg.h> |
| 31 | #include <pcmcia/ds.h> |
| 32 | |
| 33 | #include "cs_internal.h" |
Dominik Brodowski | 1a8d466 | 2005-06-27 16:28:53 -0700 | [diff] [blame] | 34 | |
| 35 | |
Dominik Brodowski | 1a8d466 | 2005-06-27 16:28:53 -0700 | [diff] [blame] | 36 | /* Access speed for IO windows */ |
Dominik Brodowski | 9fea84f | 2009-12-07 22:11:45 +0100 | [diff] [blame] | 37 | static int io_speed; |
Dominik Brodowski | 1a8d466 | 2005-06-27 16:28:53 -0700 | [diff] [blame] | 38 | module_param(io_speed, int, 0444); |
| 39 | |
| 40 | |
Dominik Brodowski | a3ac9af | 2010-01-06 14:03:03 +0100 | [diff] [blame] | 41 | int pcmcia_validate_mem(struct pcmcia_socket *s) |
| 42 | { |
| 43 | if (s->resource_ops->validate_mem) |
| 44 | return s->resource_ops->validate_mem(s); |
| 45 | /* if there is no callback, we can assume that everything is OK */ |
| 46 | return 0; |
| 47 | } |
| 48 | |
| 49 | struct resource *pcmcia_find_mem_region(u_long base, u_long num, u_long align, |
| 50 | int low, struct pcmcia_socket *s) |
| 51 | { |
| 52 | if (s->resource_ops->find_mem) |
| 53 | return s->resource_ops->find_mem(base, num, align, low, s); |
| 54 | return NULL; |
| 55 | } |
| 56 | |
Dominik Brodowski | 1a8d466 | 2005-06-27 16:28:53 -0700 | [diff] [blame] | 57 | |
Dominik Brodowski | 9485ee1 | 2010-07-31 07:09:32 +0200 | [diff] [blame] | 58 | /** |
| 59 | * release_io_space() - release IO ports allocated with alloc_io_space() |
| 60 | * @s: pcmcia socket |
| 61 | * @res: resource to release |
| 62 | * |
| 63 | */ |
Dominik Brodowski | ad0c7be | 2010-07-25 13:10:22 +0200 | [diff] [blame] | 64 | static void release_io_space(struct pcmcia_socket *s, struct resource *res) |
| 65 | { |
| 66 | resource_size_t num = resource_size(res); |
| 67 | int i; |
| 68 | |
| 69 | dev_dbg(&s->dev, "release_io_space for %pR\n", res); |
| 70 | |
| 71 | for (i = 0; i < MAX_IO_WIN; i++) { |
| 72 | if (!s->io[i].res) |
| 73 | continue; |
| 74 | if ((s->io[i].res->start <= res->start) && |
| 75 | (s->io[i].res->end >= res->end)) { |
| 76 | s->io[i].InUse -= num; |
| 77 | if (res->parent) |
| 78 | release_resource(res); |
| 79 | res->start = res->end = 0; |
| 80 | res->flags = IORESOURCE_IO; |
| 81 | /* Free the window if no one else is using it */ |
| 82 | if (s->io[i].InUse == 0) { |
| 83 | release_resource(s->io[i].res); |
| 84 | kfree(s->io[i].res); |
| 85 | s->io[i].res = NULL; |
| 86 | } |
| 87 | } |
| 88 | } |
Dominik Brodowski | 9485ee1 | 2010-07-31 07:09:32 +0200 | [diff] [blame] | 89 | } |
Dominik Brodowski | ad0c7be | 2010-07-25 13:10:22 +0200 | [diff] [blame] | 90 | |
Dominik Brodowski | 9485ee1 | 2010-07-31 07:09:32 +0200 | [diff] [blame] | 91 | |
| 92 | /** |
| 93 | * alloc_io_space() - allocate IO ports for use by a PCMCIA device |
| 94 | * @s: pcmcia socket |
| 95 | * @res: resource to allocate (begin: begin, end: size) |
| 96 | * @lines: number of IO lines decoded by the PCMCIA card |
Dominik Brodowski | 1a8d466 | 2005-06-27 16:28:53 -0700 | [diff] [blame] | 97 | * |
| 98 | * Special stuff for managing IO windows, because they are scarce |
| 99 | */ |
Dominik Brodowski | 2ce4905 | 2010-07-24 13:14:44 +0200 | [diff] [blame] | 100 | static int alloc_io_space(struct pcmcia_socket *s, struct resource *res, |
| 101 | unsigned int lines) |
Dominik Brodowski | 1a8d466 | 2005-06-27 16:28:53 -0700 | [diff] [blame] | 102 | { |
Dominik Brodowski | b19a727 | 2010-03-20 13:10:47 +0100 | [diff] [blame] | 103 | unsigned int align; |
Dominik Brodowski | 2ce4905 | 2010-07-24 13:14:44 +0200 | [diff] [blame] | 104 | unsigned int base = res->start; |
| 105 | unsigned int num = res->end; |
| 106 | int ret; |
Dominik Brodowski | 1a8d466 | 2005-06-27 16:28:53 -0700 | [diff] [blame] | 107 | |
Dominik Brodowski | 2ce4905 | 2010-07-24 13:14:44 +0200 | [diff] [blame] | 108 | res->flags |= IORESOURCE_IO; |
| 109 | |
Dominik Brodowski | 90abdc3 | 2010-07-24 17:23:51 +0200 | [diff] [blame] | 110 | dev_dbg(&s->dev, "alloc_io_space request for %pR, %d lines\n", |
| 111 | res, lines); |
Dominik Brodowski | 2ce4905 | 2010-07-24 13:14:44 +0200 | [diff] [blame] | 112 | |
| 113 | align = base ? (lines ? 1<<lines : 0) : 1; |
Dominik Brodowski | 1a8d466 | 2005-06-27 16:28:53 -0700 | [diff] [blame] | 114 | if (align && (align < num)) { |
Dominik Brodowski | 2ce4905 | 2010-07-24 13:14:44 +0200 | [diff] [blame] | 115 | if (base) { |
| 116 | dev_dbg(&s->dev, "odd IO request\n"); |
Dominik Brodowski | 1a8d466 | 2005-06-27 16:28:53 -0700 | [diff] [blame] | 117 | align = 0; |
| 118 | } else |
Dominik Brodowski | 9fea84f | 2009-12-07 22:11:45 +0100 | [diff] [blame] | 119 | while (align && (align < num)) |
| 120 | align <<= 1; |
Dominik Brodowski | 1a8d466 | 2005-06-27 16:28:53 -0700 | [diff] [blame] | 121 | } |
Dominik Brodowski | 2ce4905 | 2010-07-24 13:14:44 +0200 | [diff] [blame] | 122 | if (base & ~(align-1)) { |
| 123 | dev_dbg(&s->dev, "odd IO request\n"); |
Dominik Brodowski | 1a8d466 | 2005-06-27 16:28:53 -0700 | [diff] [blame] | 124 | align = 0; |
| 125 | } |
Dominik Brodowski | b19a727 | 2010-03-20 13:10:47 +0100 | [diff] [blame] | 126 | |
Dominik Brodowski | ad0c7be | 2010-07-25 13:10:22 +0200 | [diff] [blame] | 127 | ret = s->resource_ops->find_io(s, res->flags, &base, num, align, |
| 128 | &res->parent); |
Dominik Brodowski | 2ce4905 | 2010-07-24 13:14:44 +0200 | [diff] [blame] | 129 | if (ret) { |
Dominik Brodowski | ad0c7be | 2010-07-25 13:10:22 +0200 | [diff] [blame] | 130 | dev_dbg(&s->dev, "alloc_io_space request failed (%d)\n", ret); |
Dominik Brodowski | 2ce4905 | 2010-07-24 13:14:44 +0200 | [diff] [blame] | 131 | return -EINVAL; |
| 132 | } |
| 133 | |
| 134 | res->start = base; |
| 135 | res->end = res->start + num - 1; |
Dominik Brodowski | 1a8d466 | 2005-06-27 16:28:53 -0700 | [diff] [blame] | 136 | |
Dominik Brodowski | ad0c7be | 2010-07-25 13:10:22 +0200 | [diff] [blame] | 137 | if (res->parent) { |
| 138 | ret = request_resource(res->parent, res); |
| 139 | if (ret) { |
| 140 | dev_warn(&s->dev, |
| 141 | "request_resource %pR failed: %d\n", res, ret); |
| 142 | res->parent = NULL; |
| 143 | release_io_space(s, res); |
Dominik Brodowski | 1a8d466 | 2005-06-27 16:28:53 -0700 | [diff] [blame] | 144 | } |
| 145 | } |
Dominik Brodowski | ad0c7be | 2010-07-25 13:10:22 +0200 | [diff] [blame] | 146 | dev_dbg(&s->dev, "alloc_io_space request result %d: %pR\n", ret, res); |
| 147 | return ret; |
Dominik Brodowski | 9485ee1 | 2010-07-31 07:09:32 +0200 | [diff] [blame] | 148 | } |
Dominik Brodowski | 1a8d466 | 2005-06-27 16:28:53 -0700 | [diff] [blame] | 149 | |
| 150 | |
Dominik Brodowski | 1d5cc19 | 2010-07-24 12:23:21 +0200 | [diff] [blame] | 151 | /** |
| 152 | * pcmcia_access_config() - read or write card configuration registers |
Dominik Brodowski | 1a8d466 | 2005-06-27 16:28:53 -0700 | [diff] [blame] | 153 | * |
Dominik Brodowski | 1d5cc19 | 2010-07-24 12:23:21 +0200 | [diff] [blame] | 154 | * pcmcia_access_config() reads and writes configuration registers in |
| 155 | * attribute memory. Memory window 0 is reserved for this and the tuple |
| 156 | * reading services. Drivers must use pcmcia_read_config_byte() or |
| 157 | * pcmcia_write_config_byte(). |
Dominik Brodowski | 1a8d466 | 2005-06-27 16:28:53 -0700 | [diff] [blame] | 158 | */ |
Dominik Brodowski | 1d5cc19 | 2010-07-24 12:23:21 +0200 | [diff] [blame] | 159 | static int pcmcia_access_config(struct pcmcia_device *p_dev, |
| 160 | off_t where, u8 *val, |
| 161 | int (*accessf) (struct pcmcia_socket *s, |
| 162 | int attr, unsigned int addr, |
| 163 | unsigned int len, void *ptr)) |
Dominik Brodowski | 1a8d466 | 2005-06-27 16:28:53 -0700 | [diff] [blame] | 164 | { |
Dominik Brodowski | 855cdf1 | 2006-01-10 20:48:59 +0100 | [diff] [blame] | 165 | struct pcmcia_socket *s; |
Dominik Brodowski | 1a8d466 | 2005-06-27 16:28:53 -0700 | [diff] [blame] | 166 | config_t *c; |
| 167 | int addr; |
Dominik Brodowski | 059f667 | 2010-03-30 18:07:50 +0200 | [diff] [blame] | 168 | int ret = 0; |
Dominik Brodowski | 1a8d466 | 2005-06-27 16:28:53 -0700 | [diff] [blame] | 169 | |
Dominik Brodowski | 855cdf1 | 2006-01-10 20:48:59 +0100 | [diff] [blame] | 170 | s = p_dev->socket; |
Dominik Brodowski | 94a819f | 2010-01-17 18:31:34 +0100 | [diff] [blame] | 171 | |
| 172 | mutex_lock(&s->ops_mutex); |
Dominik Brodowski | 855cdf1 | 2006-01-10 20:48:59 +0100 | [diff] [blame] | 173 | c = p_dev->function_config; |
Dominik Brodowski | 1a8d466 | 2005-06-27 16:28:53 -0700 | [diff] [blame] | 174 | |
Dominik Brodowski | 6d9a299 | 2009-10-24 12:20:18 +0200 | [diff] [blame] | 175 | if (!(c->state & CONFIG_LOCKED)) { |
Dominik Brodowski | eb838fe | 2010-09-13 16:51:36 +0200 | [diff] [blame] | 176 | dev_dbg(&p_dev->dev, "Configuration isnt't locked\n"); |
Dominik Brodowski | 94a819f | 2010-01-17 18:31:34 +0100 | [diff] [blame] | 177 | mutex_unlock(&s->ops_mutex); |
Dominik Brodowski | 943f70f | 2008-08-03 11:23:42 +0200 | [diff] [blame] | 178 | return -EACCES; |
Dominik Brodowski | 6d9a299 | 2009-10-24 12:20:18 +0200 | [diff] [blame] | 179 | } |
Dominik Brodowski | 1a8d466 | 2005-06-27 16:28:53 -0700 | [diff] [blame] | 180 | |
Dominik Brodowski | 7feabb6 | 2010-07-29 18:35:47 +0200 | [diff] [blame] | 181 | addr = (p_dev->config_base + where) >> 1; |
Dominik Brodowski | 1a8d466 | 2005-06-27 16:28:53 -0700 | [diff] [blame] | 182 | |
Dominik Brodowski | 1d5cc19 | 2010-07-24 12:23:21 +0200 | [diff] [blame] | 183 | ret = accessf(s, 1, addr, 1, val); |
| 184 | |
Dominik Brodowski | 059f667 | 2010-03-30 18:07:50 +0200 | [diff] [blame] | 185 | mutex_unlock(&s->ops_mutex); |
Dominik Brodowski | 1d5cc19 | 2010-07-24 12:23:21 +0200 | [diff] [blame] | 186 | |
Dominik Brodowski | 059f667 | 2010-03-30 18:07:50 +0200 | [diff] [blame] | 187 | return ret; |
Dominik Brodowski | 9485ee1 | 2010-07-31 07:09:32 +0200 | [diff] [blame] | 188 | } |
Dominik Brodowski | 1d5cc19 | 2010-07-24 12:23:21 +0200 | [diff] [blame] | 189 | |
| 190 | |
| 191 | /** |
| 192 | * pcmcia_read_config_byte() - read a byte from a card configuration register |
| 193 | * |
| 194 | * pcmcia_read_config_byte() reads a byte from a configuration register in |
| 195 | * attribute memory. |
| 196 | */ |
| 197 | int pcmcia_read_config_byte(struct pcmcia_device *p_dev, off_t where, u8 *val) |
| 198 | { |
| 199 | return pcmcia_access_config(p_dev, where, val, pcmcia_read_cis_mem); |
| 200 | } |
| 201 | EXPORT_SYMBOL(pcmcia_read_config_byte); |
| 202 | |
| 203 | |
| 204 | /** |
| 205 | * pcmcia_write_config_byte() - write a byte to a card configuration register |
| 206 | * |
| 207 | * pcmcia_write_config_byte() writes a byte to a configuration register in |
| 208 | * attribute memory. |
| 209 | */ |
| 210 | int pcmcia_write_config_byte(struct pcmcia_device *p_dev, off_t where, u8 val) |
| 211 | { |
| 212 | return pcmcia_access_config(p_dev, where, &val, pcmcia_write_cis_mem); |
| 213 | } |
| 214 | EXPORT_SYMBOL(pcmcia_write_config_byte); |
Dominik Brodowski | 3448139 | 2005-06-27 16:28:54 -0700 | [diff] [blame] | 215 | |
Dominik Brodowski | 1a8d466 | 2005-06-27 16:28:53 -0700 | [diff] [blame] | 216 | |
Dominik Brodowski | 9485ee1 | 2010-07-31 07:09:32 +0200 | [diff] [blame] | 217 | /** |
| 218 | * pcmcia_map_mem_page() - modify iomem window to point to a different offset |
| 219 | * @p_dev: pcmcia device |
| 220 | * @res: iomem resource already enabled by pcmcia_request_window() |
| 221 | * @offset: card_offset to map |
| 222 | * |
| 223 | * pcmcia_map_mem_page() modifies what can be read and written by accessing |
| 224 | * an iomem range previously enabled by pcmcia_request_window(), by setting |
| 225 | * the card_offset value to @offset. |
| 226 | */ |
Dominik Brodowski | cdb1380 | 2010-07-28 10:59:06 +0200 | [diff] [blame] | 227 | int pcmcia_map_mem_page(struct pcmcia_device *p_dev, struct resource *res, |
Dominik Brodowski | b5cb259 | 2010-07-24 18:46:42 +0200 | [diff] [blame] | 228 | unsigned int offset) |
Dominik Brodowski | 1a8d466 | 2005-06-27 16:28:53 -0700 | [diff] [blame] | 229 | { |
Magnus Damm | 0bdf9b3 | 2006-12-13 19:46:53 +0900 | [diff] [blame] | 230 | struct pcmcia_socket *s = p_dev->socket; |
Dominik Brodowski | 0ca724d | 2010-07-24 19:03:02 +0200 | [diff] [blame] | 231 | unsigned int w; |
Dominik Brodowski | 6b8e087 | 2010-01-12 21:42:51 +0100 | [diff] [blame] | 232 | int ret; |
Magnus Damm | 868575d | 2006-12-13 19:46:43 +0900 | [diff] [blame] | 233 | |
Dominik Brodowski | 0ca724d | 2010-07-24 19:03:02 +0200 | [diff] [blame] | 234 | w = ((res->flags & IORESOURCE_BITS & WIN_FLAGS_REQ) >> 2) - 1; |
| 235 | if (w >= MAX_WIN) |
Dominik Brodowski | ffb8da2 | 2008-08-03 11:18:00 +0200 | [diff] [blame] | 236 | return -EINVAL; |
Dominik Brodowski | b5cb259 | 2010-07-24 18:46:42 +0200 | [diff] [blame] | 237 | |
Dominik Brodowski | 6b8e087 | 2010-01-12 21:42:51 +0100 | [diff] [blame] | 238 | mutex_lock(&s->ops_mutex); |
Dominik Brodowski | 0ca724d | 2010-07-24 19:03:02 +0200 | [diff] [blame] | 239 | s->win[w].card_start = offset; |
| 240 | ret = s->ops->set_mem_map(s, &s->win[w]); |
Dominik Brodowski | 6b8e087 | 2010-01-12 21:42:51 +0100 | [diff] [blame] | 241 | if (ret) |
Dominik Brodowski | eb838fe | 2010-09-13 16:51:36 +0200 | [diff] [blame] | 242 | dev_warn(&p_dev->dev, "failed to set_mem_map\n"); |
Dominik Brodowski | 6b8e087 | 2010-01-12 21:42:51 +0100 | [diff] [blame] | 243 | mutex_unlock(&s->ops_mutex); |
| 244 | return ret; |
Dominik Brodowski | 9485ee1 | 2010-07-31 07:09:32 +0200 | [diff] [blame] | 245 | } |
Dominik Brodowski | 1a8d466 | 2005-06-27 16:28:53 -0700 | [diff] [blame] | 246 | EXPORT_SYMBOL(pcmcia_map_mem_page); |
| 247 | |
| 248 | |
Dominik Brodowski | fb49fa5 | 2010-07-29 14:06:42 +0200 | [diff] [blame] | 249 | /** |
| 250 | * pcmcia_fixup_iowidth() - reduce io width to 8bit |
Dominik Brodowski | 9485ee1 | 2010-07-31 07:09:32 +0200 | [diff] [blame] | 251 | * @p_dev: pcmcia device |
Dominik Brodowski | 1a8d466 | 2005-06-27 16:28:53 -0700 | [diff] [blame] | 252 | * |
Dominik Brodowski | fb49fa5 | 2010-07-29 14:06:42 +0200 | [diff] [blame] | 253 | * pcmcia_fixup_iowidth() allows a PCMCIA device driver to reduce the |
Dominik Brodowski | 1ac71e5 | 2010-07-29 19:27:09 +0200 | [diff] [blame] | 254 | * IO width to 8bit after having called pcmcia_enable_device() |
Dominik Brodowski | fb49fa5 | 2010-07-29 14:06:42 +0200 | [diff] [blame] | 255 | * previously. |
Dominik Brodowski | 1a8d466 | 2005-06-27 16:28:53 -0700 | [diff] [blame] | 256 | */ |
Dominik Brodowski | fb49fa5 | 2010-07-29 14:06:42 +0200 | [diff] [blame] | 257 | int pcmcia_fixup_iowidth(struct pcmcia_device *p_dev) |
Dominik Brodowski | 1a8d466 | 2005-06-27 16:28:53 -0700 | [diff] [blame] | 258 | { |
Dominik Brodowski | fb49fa5 | 2010-07-29 14:06:42 +0200 | [diff] [blame] | 259 | struct pcmcia_socket *s = p_dev->socket; |
| 260 | pccard_io_map io_off = { 0, 0, 0, 0, 1 }; |
| 261 | pccard_io_map io_on; |
| 262 | int i, ret = 0; |
Dominik Brodowski | 94a819f | 2010-01-17 18:31:34 +0100 | [diff] [blame] | 263 | |
| 264 | mutex_lock(&s->ops_mutex); |
Dominik Brodowski | dbb22f0 | 2006-01-10 20:41:27 +0100 | [diff] [blame] | 265 | |
Dominik Brodowski | fb49fa5 | 2010-07-29 14:06:42 +0200 | [diff] [blame] | 266 | dev_dbg(&p_dev->dev, "fixup iowidth to 8bit\n"); |
| 267 | |
| 268 | if (!(s->state & SOCKET_PRESENT) || |
| 269 | !(p_dev->function_config->state & CONFIG_LOCKED)) { |
| 270 | dev_dbg(&p_dev->dev, "No card? Config not locked?\n"); |
Jiri Slaby | 4e06e24 | 2010-03-16 15:57:44 +0100 | [diff] [blame] | 271 | ret = -EACCES; |
| 272 | goto unlock; |
Dominik Brodowski | 6d9a299 | 2009-10-24 12:20:18 +0200 | [diff] [blame] | 273 | } |
Dominik Brodowski | 1a8d466 | 2005-06-27 16:28:53 -0700 | [diff] [blame] | 274 | |
Dominik Brodowski | fb49fa5 | 2010-07-29 14:06:42 +0200 | [diff] [blame] | 275 | io_on.speed = io_speed; |
| 276 | for (i = 0; i < MAX_IO_WIN; i++) { |
| 277 | if (!s->io[i].res) |
| 278 | continue; |
| 279 | io_off.map = i; |
| 280 | io_on.map = i; |
| 281 | |
| 282 | io_on.flags = MAP_ACTIVE | IO_DATA_PATH_WIDTH_8; |
| 283 | io_on.start = s->io[i].res->start; |
| 284 | io_on.stop = s->io[i].res->end; |
| 285 | |
| 286 | s->ops->set_io_map(s, &io_off); |
| 287 | mdelay(40); |
| 288 | s->ops->set_io_map(s, &io_on); |
Dominik Brodowski | d8b0a49 | 2008-08-03 10:56:13 +0200 | [diff] [blame] | 289 | } |
Jiri Slaby | 4e06e24 | 2010-03-16 15:57:44 +0100 | [diff] [blame] | 290 | unlock: |
Dominik Brodowski | 94a819f | 2010-01-17 18:31:34 +0100 | [diff] [blame] | 291 | mutex_unlock(&s->ops_mutex); |
Dominik Brodowski | 4bbed52 | 2006-01-15 11:18:12 +0100 | [diff] [blame] | 292 | |
Jiri Slaby | 4e06e24 | 2010-03-16 15:57:44 +0100 | [diff] [blame] | 293 | return ret; |
Dominik Brodowski | fb49fa5 | 2010-07-29 14:06:42 +0200 | [diff] [blame] | 294 | } |
| 295 | EXPORT_SYMBOL(pcmcia_fixup_iowidth); |
| 296 | |
| 297 | |
| 298 | /** |
| 299 | * pcmcia_fixup_vpp() - set Vpp to a new voltage level |
Dominik Brodowski | 9485ee1 | 2010-07-31 07:09:32 +0200 | [diff] [blame] | 300 | * @p_dev: pcmcia device |
| 301 | * @new_vpp: new Vpp voltage |
Dominik Brodowski | fb49fa5 | 2010-07-29 14:06:42 +0200 | [diff] [blame] | 302 | * |
| 303 | * pcmcia_fixup_vpp() allows a PCMCIA device driver to set Vpp to |
Dominik Brodowski | 1ac71e5 | 2010-07-29 19:27:09 +0200 | [diff] [blame] | 304 | * a new voltage level between calls to pcmcia_enable_device() |
Dominik Brodowski | fb49fa5 | 2010-07-29 14:06:42 +0200 | [diff] [blame] | 305 | * and pcmcia_disable_device(). |
| 306 | */ |
| 307 | int pcmcia_fixup_vpp(struct pcmcia_device *p_dev, unsigned char new_vpp) |
| 308 | { |
| 309 | struct pcmcia_socket *s = p_dev->socket; |
| 310 | int ret = 0; |
| 311 | |
| 312 | mutex_lock(&s->ops_mutex); |
| 313 | |
| 314 | dev_dbg(&p_dev->dev, "fixup Vpp to %d\n", new_vpp); |
| 315 | |
| 316 | if (!(s->state & SOCKET_PRESENT) || |
| 317 | !(p_dev->function_config->state & CONFIG_LOCKED)) { |
| 318 | dev_dbg(&p_dev->dev, "No card? Config not locked?\n"); |
| 319 | ret = -EACCES; |
| 320 | goto unlock; |
| 321 | } |
| 322 | |
| 323 | s->socket.Vpp = new_vpp; |
| 324 | if (s->ops->set_socket(s, &s->socket)) { |
| 325 | dev_warn(&p_dev->dev, "Unable to set VPP\n"); |
| 326 | ret = -EIO; |
| 327 | goto unlock; |
| 328 | } |
Dominik Brodowski | e8405f0 | 2010-07-29 15:50:55 +0200 | [diff] [blame] | 329 | p_dev->vpp = new_vpp; |
Dominik Brodowski | fb49fa5 | 2010-07-29 14:06:42 +0200 | [diff] [blame] | 330 | |
| 331 | unlock: |
| 332 | mutex_unlock(&s->ops_mutex); |
| 333 | |
| 334 | return ret; |
| 335 | } |
| 336 | EXPORT_SYMBOL(pcmcia_fixup_vpp); |
Dominik Brodowski | 1a8d466 | 2005-06-27 16:28:53 -0700 | [diff] [blame] | 337 | |
| 338 | |
Dominik Brodowski | 9485ee1 | 2010-07-31 07:09:32 +0200 | [diff] [blame] | 339 | /** |
| 340 | * pcmcia_release_configuration() - physically disable a PCMCIA device |
| 341 | * @p_dev: pcmcia device |
| 342 | * |
| 343 | * pcmcia_release_configuration() is the 1:1 counterpart to |
| 344 | * pcmcia_enable_device(): If a PCMCIA device is no longer used by any |
| 345 | * driver, the Vpp voltage is set to 0, IRQs will no longer be generated, |
| 346 | * and I/O ranges will be disabled. As pcmcia_release_io() and |
| 347 | * pcmcia_release_window() still need to be called, device drivers are |
| 348 | * expected to call pcmcia_disable_device() instead. |
| 349 | */ |
Dominik Brodowski | 2bc5a9b | 2005-07-07 17:59:02 -0700 | [diff] [blame] | 350 | int pcmcia_release_configuration(struct pcmcia_device *p_dev) |
Dominik Brodowski | 1a8d466 | 2005-06-27 16:28:53 -0700 | [diff] [blame] | 351 | { |
| 352 | pccard_io_map io = { 0, 0, 0, 0, 1 }; |
Dominik Brodowski | 2bc5a9b | 2005-07-07 17:59:02 -0700 | [diff] [blame] | 353 | struct pcmcia_socket *s = p_dev->socket; |
Dominik Brodowski | 94a819f | 2010-01-17 18:31:34 +0100 | [diff] [blame] | 354 | config_t *c; |
Dominik Brodowski | 1a8d466 | 2005-06-27 16:28:53 -0700 | [diff] [blame] | 355 | int i; |
| 356 | |
Dominik Brodowski | 9e86749c | 2010-01-16 00:26:33 +0100 | [diff] [blame] | 357 | mutex_lock(&s->ops_mutex); |
Dominik Brodowski | 94a819f | 2010-01-17 18:31:34 +0100 | [diff] [blame] | 358 | c = p_dev->function_config; |
Dominik Brodowski | e2d4096 | 2006-03-02 00:09:29 +0100 | [diff] [blame] | 359 | if (p_dev->_locked) { |
| 360 | p_dev->_locked = 0; |
Dominik Brodowski | 1a8d466 | 2005-06-27 16:28:53 -0700 | [diff] [blame] | 361 | if (--(s->lock_count) == 0) { |
Dominik Brodowski | 9485ee1 | 2010-07-31 07:09:32 +0200 | [diff] [blame] | 362 | s->socket.flags = SS_OUTPUT_ENA; /* Is this correct? */ |
Dominik Brodowski | 1a8d466 | 2005-06-27 16:28:53 -0700 | [diff] [blame] | 363 | s->socket.Vpp = 0; |
| 364 | s->socket.io_irq = 0; |
| 365 | s->ops->set_socket(s, &s->socket); |
| 366 | } |
Dominik Brodowski | 5f2a71f | 2006-01-15 09:32:39 +0100 | [diff] [blame] | 367 | } |
| 368 | if (c->state & CONFIG_LOCKED) { |
| 369 | c->state &= ~CONFIG_LOCKED; |
Dominik Brodowski | 1a8d466 | 2005-06-27 16:28:53 -0700 | [diff] [blame] | 370 | if (c->state & CONFIG_IO_REQ) |
| 371 | for (i = 0; i < MAX_IO_WIN; i++) { |
Dominik Brodowski | c7d0069 | 2006-01-15 08:04:43 +0100 | [diff] [blame] | 372 | if (!s->io[i].res) |
Dominik Brodowski | 1a8d466 | 2005-06-27 16:28:53 -0700 | [diff] [blame] | 373 | continue; |
| 374 | s->io[i].Config--; |
| 375 | if (s->io[i].Config != 0) |
| 376 | continue; |
| 377 | io.map = i; |
| 378 | s->ops->set_io_map(s, &io); |
| 379 | } |
Dominik Brodowski | 1a8d466 | 2005-06-27 16:28:53 -0700 | [diff] [blame] | 380 | } |
Dominik Brodowski | 9e86749c | 2010-01-16 00:26:33 +0100 | [diff] [blame] | 381 | mutex_unlock(&s->ops_mutex); |
Dominik Brodowski | 1a8d466 | 2005-06-27 16:28:53 -0700 | [diff] [blame] | 382 | |
Dominik Brodowski | 4c89e88 | 2008-08-03 10:07:45 +0200 | [diff] [blame] | 383 | return 0; |
Dominik Brodowski | 9485ee1 | 2010-07-31 07:09:32 +0200 | [diff] [blame] | 384 | } |
Dominik Brodowski | 1a8d466 | 2005-06-27 16:28:53 -0700 | [diff] [blame] | 385 | |
| 386 | |
Dominik Brodowski | 9485ee1 | 2010-07-31 07:09:32 +0200 | [diff] [blame] | 387 | /** |
| 388 | * pcmcia_release_io() - release I/O allocated by a PCMCIA device |
| 389 | * @p_dev: pcmcia device |
Dominik Brodowski | 1a8d466 | 2005-06-27 16:28:53 -0700 | [diff] [blame] | 390 | * |
Dominik Brodowski | 9485ee1 | 2010-07-31 07:09:32 +0200 | [diff] [blame] | 391 | * pcmcia_release_io() releases the I/O ranges allocated by a PCMCIA |
| 392 | * device. This may be invoked some time after a card ejection has |
| 393 | * already dumped the actual socket configuration, so if the client is |
| 394 | * "stale", we don't bother checking the port ranges against the |
| 395 | * current socket values. |
Dominik Brodowski | 1a8d466 | 2005-06-27 16:28:53 -0700 | [diff] [blame] | 396 | */ |
Dominik Brodowski | 2ce4905 | 2010-07-24 13:14:44 +0200 | [diff] [blame] | 397 | static int pcmcia_release_io(struct pcmcia_device *p_dev) |
Dominik Brodowski | 1a8d466 | 2005-06-27 16:28:53 -0700 | [diff] [blame] | 398 | { |
Dominik Brodowski | 2bc5a9b | 2005-07-07 17:59:02 -0700 | [diff] [blame] | 399 | struct pcmcia_socket *s = p_dev->socket; |
Dominik Brodowski | 94a819f | 2010-01-17 18:31:34 +0100 | [diff] [blame] | 400 | int ret = -EINVAL; |
| 401 | config_t *c; |
| 402 | |
| 403 | mutex_lock(&s->ops_mutex); |
Dominik Brodowski | 9fea84f | 2009-12-07 22:11:45 +0100 | [diff] [blame] | 404 | if (!p_dev->_io) |
Dominik Brodowski | 94a819f | 2010-01-17 18:31:34 +0100 | [diff] [blame] | 405 | goto out; |
Dominik Brodowski | 5f2a71f | 2006-01-15 09:32:39 +0100 | [diff] [blame] | 406 | |
Dominik Brodowski | 2ce4905 | 2010-07-24 13:14:44 +0200 | [diff] [blame] | 407 | c = p_dev->function_config; |
| 408 | |
| 409 | release_io_space(s, &c->io[0]); |
| 410 | |
| 411 | if (c->io[1].end) |
| 412 | release_io_space(s, &c->io[1]); |
| 413 | |
Dominik Brodowski | e2d4096 | 2006-03-02 00:09:29 +0100 | [diff] [blame] | 414 | p_dev->_io = 0; |
Dominik Brodowski | 5f2a71f | 2006-01-15 09:32:39 +0100 | [diff] [blame] | 415 | c->state &= ~CONFIG_IO_REQ; |
Dominik Brodowski | 1a8d466 | 2005-06-27 16:28:53 -0700 | [diff] [blame] | 416 | |
Dominik Brodowski | 94a819f | 2010-01-17 18:31:34 +0100 | [diff] [blame] | 417 | out: |
| 418 | mutex_unlock(&s->ops_mutex); |
| 419 | |
| 420 | return ret; |
Dominik Brodowski | 1a8d466 | 2005-06-27 16:28:53 -0700 | [diff] [blame] | 421 | } /* pcmcia_release_io */ |
Dominik Brodowski | 1a8d466 | 2005-06-27 16:28:53 -0700 | [diff] [blame] | 422 | |
Dominik Brodowski | 9485ee1 | 2010-07-31 07:09:32 +0200 | [diff] [blame] | 423 | |
Dominik Brodowski | cdb1380 | 2010-07-28 10:59:06 +0200 | [diff] [blame] | 424 | /** |
| 425 | * pcmcia_release_window() - release reserved iomem for PCMCIA devices |
Dominik Brodowski | 9485ee1 | 2010-07-31 07:09:32 +0200 | [diff] [blame] | 426 | * @p_dev: pcmcia device |
| 427 | * @res: iomem resource to release |
Dominik Brodowski | cdb1380 | 2010-07-28 10:59:06 +0200 | [diff] [blame] | 428 | * |
Dominik Brodowski | 9485ee1 | 2010-07-31 07:09:32 +0200 | [diff] [blame] | 429 | * pcmcia_release_window() releases &struct resource *res which was |
Dominik Brodowski | cdb1380 | 2010-07-28 10:59:06 +0200 | [diff] [blame] | 430 | * previously reserved by calling pcmcia_request_window(). |
| 431 | */ |
Dominik Brodowski | 0ca724d | 2010-07-24 19:03:02 +0200 | [diff] [blame] | 432 | int pcmcia_release_window(struct pcmcia_device *p_dev, struct resource *res) |
Dominik Brodowski | 1a8d466 | 2005-06-27 16:28:53 -0700 | [diff] [blame] | 433 | { |
Magnus Damm | 0bdf9b3 | 2006-12-13 19:46:53 +0900 | [diff] [blame] | 434 | struct pcmcia_socket *s = p_dev->socket; |
Dominik Brodowski | 82f88e3 | 2009-11-03 01:16:12 +0100 | [diff] [blame] | 435 | pccard_mem_map *win; |
Dominik Brodowski | 0ca724d | 2010-07-24 19:03:02 +0200 | [diff] [blame] | 436 | unsigned int w; |
Dominik Brodowski | 1a8d466 | 2005-06-27 16:28:53 -0700 | [diff] [blame] | 437 | |
Dominik Brodowski | 0ca724d | 2010-07-24 19:03:02 +0200 | [diff] [blame] | 438 | dev_dbg(&p_dev->dev, "releasing window %pR\n", res); |
| 439 | |
| 440 | w = ((res->flags & IORESOURCE_BITS & WIN_FLAGS_REQ) >> 2) - 1; |
| 441 | if (w >= MAX_WIN) |
Dominik Brodowski | ffb8da2 | 2008-08-03 11:18:00 +0200 | [diff] [blame] | 442 | return -EINVAL; |
Magnus Damm | 0bdf9b3 | 2006-12-13 19:46:53 +0900 | [diff] [blame] | 443 | |
Dominik Brodowski | 6b8e087 | 2010-01-12 21:42:51 +0100 | [diff] [blame] | 444 | mutex_lock(&s->ops_mutex); |
Dominik Brodowski | 0ca724d | 2010-07-24 19:03:02 +0200 | [diff] [blame] | 445 | win = &s->win[w]; |
Magnus Damm | 0bdf9b3 | 2006-12-13 19:46:53 +0900 | [diff] [blame] | 446 | |
Dominik Brodowski | 0ca724d | 2010-07-24 19:03:02 +0200 | [diff] [blame] | 447 | if (!(p_dev->_win & CLIENT_WIN_REQ(w))) { |
Dominik Brodowski | eb838fe | 2010-09-13 16:51:36 +0200 | [diff] [blame] | 448 | dev_dbg(&p_dev->dev, "not releasing unknown window\n"); |
Dominik Brodowski | 6b8e087 | 2010-01-12 21:42:51 +0100 | [diff] [blame] | 449 | mutex_unlock(&s->ops_mutex); |
Dominik Brodowski | ffb8da2 | 2008-08-03 11:18:00 +0200 | [diff] [blame] | 450 | return -EINVAL; |
Dominik Brodowski | 6d9a299 | 2009-10-24 12:20:18 +0200 | [diff] [blame] | 451 | } |
Dominik Brodowski | 1a8d466 | 2005-06-27 16:28:53 -0700 | [diff] [blame] | 452 | |
| 453 | /* Shut down memory window */ |
Dominik Brodowski | 82f88e3 | 2009-11-03 01:16:12 +0100 | [diff] [blame] | 454 | win->flags &= ~MAP_ACTIVE; |
| 455 | s->ops->set_mem_map(s, win); |
Dominik Brodowski | 0ca724d | 2010-07-24 19:03:02 +0200 | [diff] [blame] | 456 | s->state &= ~SOCKET_WIN_REQ(w); |
Dominik Brodowski | 1a8d466 | 2005-06-27 16:28:53 -0700 | [diff] [blame] | 457 | |
| 458 | /* Release system memory */ |
Dominik Brodowski | 82f88e3 | 2009-11-03 01:16:12 +0100 | [diff] [blame] | 459 | if (win->res) { |
Dominik Brodowski | ad0c7be | 2010-07-25 13:10:22 +0200 | [diff] [blame] | 460 | release_resource(res); |
Dominik Brodowski | 82f88e3 | 2009-11-03 01:16:12 +0100 | [diff] [blame] | 461 | release_resource(win->res); |
| 462 | kfree(win->res); |
| 463 | win->res = NULL; |
Dominik Brodowski | 1a8d466 | 2005-06-27 16:28:53 -0700 | [diff] [blame] | 464 | } |
Dominik Brodowski | cdb1380 | 2010-07-28 10:59:06 +0200 | [diff] [blame] | 465 | res->start = res->end = 0; |
| 466 | res->flags = IORESOURCE_MEM; |
Dominik Brodowski | 0ca724d | 2010-07-24 19:03:02 +0200 | [diff] [blame] | 467 | p_dev->_win &= ~CLIENT_WIN_REQ(w); |
Dominik Brodowski | 6b8e087 | 2010-01-12 21:42:51 +0100 | [diff] [blame] | 468 | mutex_unlock(&s->ops_mutex); |
Dominik Brodowski | 1a8d466 | 2005-06-27 16:28:53 -0700 | [diff] [blame] | 469 | |
Dominik Brodowski | 4c89e88 | 2008-08-03 10:07:45 +0200 | [diff] [blame] | 470 | return 0; |
Dominik Brodowski | 1a8d466 | 2005-06-27 16:28:53 -0700 | [diff] [blame] | 471 | } /* pcmcia_release_window */ |
| 472 | EXPORT_SYMBOL(pcmcia_release_window); |
| 473 | |
Dominik Brodowski | 9485ee1 | 2010-07-31 07:09:32 +0200 | [diff] [blame] | 474 | |
Dominik Brodowski | 1ac71e5 | 2010-07-29 19:27:09 +0200 | [diff] [blame] | 475 | /** |
| 476 | * pcmcia_enable_device() - set up and activate a PCMCIA device |
Dominik Brodowski | 9485ee1 | 2010-07-31 07:09:32 +0200 | [diff] [blame] | 477 | * @p_dev: the associated PCMCIA device |
Dominik Brodowski | 1ac71e5 | 2010-07-29 19:27:09 +0200 | [diff] [blame] | 478 | * |
Dominik Brodowski | 9485ee1 | 2010-07-31 07:09:32 +0200 | [diff] [blame] | 479 | * pcmcia_enable_device() physically enables a PCMCIA device. It parses |
| 480 | * the flags passed to in @flags and stored in @p_dev->flags and sets up |
| 481 | * the Vpp voltage, enables the speaker line, I/O ports and store proper |
| 482 | * values to configuration registers. |
Dominik Brodowski | 1ac71e5 | 2010-07-29 19:27:09 +0200 | [diff] [blame] | 483 | */ |
| 484 | int pcmcia_enable_device(struct pcmcia_device *p_dev) |
Dominik Brodowski | 1a8d466 | 2005-06-27 16:28:53 -0700 | [diff] [blame] | 485 | { |
| 486 | int i; |
Dominik Brodowski | 1ac71e5 | 2010-07-29 19:27:09 +0200 | [diff] [blame] | 487 | unsigned int base; |
Dominik Brodowski | 2bc5a9b | 2005-07-07 17:59:02 -0700 | [diff] [blame] | 488 | struct pcmcia_socket *s = p_dev->socket; |
Dominik Brodowski | 1a8d466 | 2005-06-27 16:28:53 -0700 | [diff] [blame] | 489 | config_t *c; |
| 490 | pccard_io_map iomap; |
Dominik Brodowski | fc30110 | 2010-07-29 16:19:39 +0200 | [diff] [blame] | 491 | unsigned char status = 0; |
| 492 | unsigned char ext_status = 0; |
Dominik Brodowski | 7feabb6 | 2010-07-29 18:35:47 +0200 | [diff] [blame] | 493 | unsigned char option = 0; |
Dominik Brodowski | 1ac71e5 | 2010-07-29 19:27:09 +0200 | [diff] [blame] | 494 | unsigned int flags = p_dev->config_flags; |
Dominik Brodowski | 1a8d466 | 2005-06-27 16:28:53 -0700 | [diff] [blame] | 495 | |
Dominik Brodowski | 1a8d466 | 2005-06-27 16:28:53 -0700 | [diff] [blame] | 496 | if (!(s->state & SOCKET_PRESENT)) |
Joe Perches | d598de0 | 2009-06-28 09:26:41 -0700 | [diff] [blame] | 497 | return -ENODEV; |
Dominik Brodowski | 1a8d466 | 2005-06-27 16:28:53 -0700 | [diff] [blame] | 498 | |
Dominik Brodowski | 94a819f | 2010-01-17 18:31:34 +0100 | [diff] [blame] | 499 | mutex_lock(&s->ops_mutex); |
Dominik Brodowski | dbb22f0 | 2006-01-10 20:41:27 +0100 | [diff] [blame] | 500 | c = p_dev->function_config; |
Dominik Brodowski | 6d9a299 | 2009-10-24 12:20:18 +0200 | [diff] [blame] | 501 | if (c->state & CONFIG_LOCKED) { |
Dominik Brodowski | 94a819f | 2010-01-17 18:31:34 +0100 | [diff] [blame] | 502 | mutex_unlock(&s->ops_mutex); |
Dominik Brodowski | eb838fe | 2010-09-13 16:51:36 +0200 | [diff] [blame] | 503 | dev_dbg(&p_dev->dev, "Configuration is locked\n"); |
Dominik Brodowski | 943f70f | 2008-08-03 11:23:42 +0200 | [diff] [blame] | 504 | return -EACCES; |
Dominik Brodowski | 6d9a299 | 2009-10-24 12:20:18 +0200 | [diff] [blame] | 505 | } |
Dominik Brodowski | 1a8d466 | 2005-06-27 16:28:53 -0700 | [diff] [blame] | 506 | |
| 507 | /* Do power control. We don't allow changes in Vcc. */ |
Dominik Brodowski | e8405f0 | 2010-07-29 15:50:55 +0200 | [diff] [blame] | 508 | s->socket.Vpp = p_dev->vpp; |
Dominik Brodowski | d8b0a49 | 2008-08-03 10:56:13 +0200 | [diff] [blame] | 509 | if (s->ops->set_socket(s, &s->socket)) { |
Dominik Brodowski | 9e86749c | 2010-01-16 00:26:33 +0100 | [diff] [blame] | 510 | mutex_unlock(&s->ops_mutex); |
Dominik Brodowski | eb838fe | 2010-09-13 16:51:36 +0200 | [diff] [blame] | 511 | dev_printk(KERN_WARNING, &p_dev->dev, |
Dominik Brodowski | d8b0a49 | 2008-08-03 10:56:13 +0200 | [diff] [blame] | 512 | "Unable to set socket state\n"); |
| 513 | return -EINVAL; |
| 514 | } |
Dominik Brodowski | 1a8d466 | 2005-06-27 16:28:53 -0700 | [diff] [blame] | 515 | |
Dominik Brodowski | 1a8d466 | 2005-06-27 16:28:53 -0700 | [diff] [blame] | 516 | /* Pick memory or I/O card, DMA mode, interrupt */ |
Dominik Brodowski | 37979e1 | 2010-07-29 16:33:23 +0200 | [diff] [blame] | 517 | if (p_dev->_io) |
Dominik Brodowski | 1a8d466 | 2005-06-27 16:28:53 -0700 | [diff] [blame] | 518 | s->socket.flags |= SS_IOCARD; |
Dominik Brodowski | 1ac71e5 | 2010-07-29 19:27:09 +0200 | [diff] [blame] | 519 | if (flags & CONF_ENABLE_SPKR) { |
Dominik Brodowski | 1a8d466 | 2005-06-27 16:28:53 -0700 | [diff] [blame] | 520 | s->socket.flags |= SS_SPKR_ENA; |
Dominik Brodowski | fc30110 | 2010-07-29 16:19:39 +0200 | [diff] [blame] | 521 | status = CCSR_AUDIO_ENA; |
Dominik Brodowski | 7feabb6 | 2010-07-29 18:35:47 +0200 | [diff] [blame] | 522 | if (!(p_dev->config_regs & PRESENT_STATUS)) |
Dominik Brodowski | fc30110 | 2010-07-29 16:19:39 +0200 | [diff] [blame] | 523 | dev_warn(&p_dev->dev, "speaker requested, but " |
| 524 | "PRESENT_STATUS not set!\n"); |
| 525 | } |
Dominik Brodowski | 1ac71e5 | 2010-07-29 19:27:09 +0200 | [diff] [blame] | 526 | if (flags & CONF_ENABLE_IRQ) |
Dominik Brodowski | 6f840af | 2010-03-07 10:51:23 +0100 | [diff] [blame] | 527 | s->socket.io_irq = s->pcmcia_irq; |
Dominik Brodowski | 1a8d466 | 2005-06-27 16:28:53 -0700 | [diff] [blame] | 528 | else |
| 529 | s->socket.io_irq = 0; |
Dominik Brodowski | 1ac71e5 | 2010-07-29 19:27:09 +0200 | [diff] [blame] | 530 | if (flags & CONF_ENABLE_ESR) { |
Dominik Brodowski | 7feabb6 | 2010-07-29 18:35:47 +0200 | [diff] [blame] | 531 | p_dev->config_regs |= PRESENT_EXT_STATUS; |
Dominik Brodowski | fc30110 | 2010-07-29 16:19:39 +0200 | [diff] [blame] | 532 | ext_status = ESR_REQ_ATTN_ENA; |
| 533 | } |
Dominik Brodowski | 1a8d466 | 2005-06-27 16:28:53 -0700 | [diff] [blame] | 534 | s->ops->set_socket(s, &s->socket); |
| 535 | s->lock_count++; |
| 536 | |
Dominik Brodowski | 1c4a77b | 2010-09-18 23:21:09 +0200 | [diff] [blame] | 537 | dev_dbg(&p_dev->dev, |
| 538 | "enable_device: V %d, flags %x, base %x, regs %x, idx %x\n", |
| 539 | p_dev->vpp, flags, p_dev->config_base, p_dev->config_regs, |
| 540 | p_dev->config_index); |
| 541 | |
Dominik Brodowski | 1a8d466 | 2005-06-27 16:28:53 -0700 | [diff] [blame] | 542 | /* Set up CIS configuration registers */ |
Dominik Brodowski | 7feabb6 | 2010-07-29 18:35:47 +0200 | [diff] [blame] | 543 | base = p_dev->config_base; |
| 544 | if (p_dev->config_regs & PRESENT_COPY) { |
Dominik Brodowski | 1a4a046 | 2010-07-29 15:54:54 +0200 | [diff] [blame] | 545 | u16 tmp = 0; |
| 546 | dev_dbg(&p_dev->dev, "clearing CISREG_SCR\n"); |
| 547 | pcmcia_write_cis_mem(s, 1, (base + CISREG_SCR)>>1, 1, &tmp); |
| 548 | } |
Dominik Brodowski | 7feabb6 | 2010-07-29 18:35:47 +0200 | [diff] [blame] | 549 | if (p_dev->config_regs & PRESENT_PIN_REPLACE) { |
Dominik Brodowski | 1a4a046 | 2010-07-29 15:54:54 +0200 | [diff] [blame] | 550 | u16 tmp = 0; |
| 551 | dev_dbg(&p_dev->dev, "clearing CISREG_PRR\n"); |
| 552 | pcmcia_write_cis_mem(s, 1, (base + CISREG_PRR)>>1, 1, &tmp); |
Dominik Brodowski | 1a8d466 | 2005-06-27 16:28:53 -0700 | [diff] [blame] | 553 | } |
Dominik Brodowski | 7feabb6 | 2010-07-29 18:35:47 +0200 | [diff] [blame] | 554 | if (p_dev->config_regs & PRESENT_OPTION) { |
Dominik Brodowski | 1a8d466 | 2005-06-27 16:28:53 -0700 | [diff] [blame] | 555 | if (s->functions == 1) { |
Dominik Brodowski | 7feabb6 | 2010-07-29 18:35:47 +0200 | [diff] [blame] | 556 | option = p_dev->config_index & COR_CONFIG_MASK; |
Dominik Brodowski | 1a8d466 | 2005-06-27 16:28:53 -0700 | [diff] [blame] | 557 | } else { |
Dominik Brodowski | 7feabb6 | 2010-07-29 18:35:47 +0200 | [diff] [blame] | 558 | option = p_dev->config_index & COR_MFC_CONFIG_MASK; |
| 559 | option |= COR_FUNC_ENA|COR_IREQ_ENA; |
| 560 | if (p_dev->config_regs & PRESENT_IOBASE_0) |
| 561 | option |= COR_ADDR_DECODE; |
Dominik Brodowski | 1a8d466 | 2005-06-27 16:28:53 -0700 | [diff] [blame] | 562 | } |
Dominik Brodowski | 1ac71e5 | 2010-07-29 19:27:09 +0200 | [diff] [blame] | 563 | if ((flags & CONF_ENABLE_IRQ) && |
| 564 | !(flags & CONF_ENABLE_PULSE_IRQ)) |
Dominik Brodowski | 7feabb6 | 2010-07-29 18:35:47 +0200 | [diff] [blame] | 565 | option |= COR_LEVEL_REQ; |
| 566 | pcmcia_write_cis_mem(s, 1, (base + CISREG_COR)>>1, 1, &option); |
Dominik Brodowski | 1a8d466 | 2005-06-27 16:28:53 -0700 | [diff] [blame] | 567 | mdelay(40); |
| 568 | } |
Dominik Brodowski | 7feabb6 | 2010-07-29 18:35:47 +0200 | [diff] [blame] | 569 | if (p_dev->config_regs & PRESENT_STATUS) |
Dominik Brodowski | fc30110 | 2010-07-29 16:19:39 +0200 | [diff] [blame] | 570 | pcmcia_write_cis_mem(s, 1, (base + CISREG_CCSR)>>1, 1, &status); |
| 571 | |
Dominik Brodowski | 7feabb6 | 2010-07-29 18:35:47 +0200 | [diff] [blame] | 572 | if (p_dev->config_regs & PRESENT_EXT_STATUS) |
Dominik Brodowski | fc30110 | 2010-07-29 16:19:39 +0200 | [diff] [blame] | 573 | pcmcia_write_cis_mem(s, 1, (base + CISREG_ESR)>>1, 1, |
| 574 | &ext_status); |
| 575 | |
Dominik Brodowski | 7feabb6 | 2010-07-29 18:35:47 +0200 | [diff] [blame] | 576 | if (p_dev->config_regs & PRESENT_IOBASE_0) { |
Dominik Brodowski | 2ce4905 | 2010-07-24 13:14:44 +0200 | [diff] [blame] | 577 | u8 b = c->io[0].start & 0xff; |
Dominik Brodowski | 1a8d466 | 2005-06-27 16:28:53 -0700 | [diff] [blame] | 578 | pcmcia_write_cis_mem(s, 1, (base + CISREG_IOBASE_0)>>1, 1, &b); |
Dominik Brodowski | 2ce4905 | 2010-07-24 13:14:44 +0200 | [diff] [blame] | 579 | b = (c->io[0].start >> 8) & 0xff; |
Dominik Brodowski | 1a8d466 | 2005-06-27 16:28:53 -0700 | [diff] [blame] | 580 | pcmcia_write_cis_mem(s, 1, (base + CISREG_IOBASE_1)>>1, 1, &b); |
| 581 | } |
Dominik Brodowski | 7feabb6 | 2010-07-29 18:35:47 +0200 | [diff] [blame] | 582 | if (p_dev->config_regs & PRESENT_IOSIZE) { |
Dominik Brodowski | 2ce4905 | 2010-07-24 13:14:44 +0200 | [diff] [blame] | 583 | u8 b = resource_size(&c->io[0]) + resource_size(&c->io[1]) - 1; |
Dominik Brodowski | 1a8d466 | 2005-06-27 16:28:53 -0700 | [diff] [blame] | 584 | pcmcia_write_cis_mem(s, 1, (base + CISREG_IOSIZE)>>1, 1, &b); |
| 585 | } |
| 586 | |
| 587 | /* Configure I/O windows */ |
| 588 | if (c->state & CONFIG_IO_REQ) { |
| 589 | iomap.speed = io_speed; |
| 590 | for (i = 0; i < MAX_IO_WIN; i++) |
Dominik Brodowski | c7d0069 | 2006-01-15 08:04:43 +0100 | [diff] [blame] | 591 | if (s->io[i].res) { |
Dominik Brodowski | 1a8d466 | 2005-06-27 16:28:53 -0700 | [diff] [blame] | 592 | iomap.map = i; |
| 593 | iomap.flags = MAP_ACTIVE; |
Dominik Brodowski | c7d0069 | 2006-01-15 08:04:43 +0100 | [diff] [blame] | 594 | switch (s->io[i].res->flags & IO_DATA_PATH_WIDTH) { |
Dominik Brodowski | 1a8d466 | 2005-06-27 16:28:53 -0700 | [diff] [blame] | 595 | case IO_DATA_PATH_WIDTH_16: |
| 596 | iomap.flags |= MAP_16BIT; break; |
| 597 | case IO_DATA_PATH_WIDTH_AUTO: |
| 598 | iomap.flags |= MAP_AUTOSZ; break; |
| 599 | default: |
| 600 | break; |
| 601 | } |
Dominik Brodowski | c7d0069 | 2006-01-15 08:04:43 +0100 | [diff] [blame] | 602 | iomap.start = s->io[i].res->start; |
| 603 | iomap.stop = s->io[i].res->end; |
Dominik Brodowski | 1a8d466 | 2005-06-27 16:28:53 -0700 | [diff] [blame] | 604 | s->ops->set_io_map(s, &iomap); |
| 605 | s->io[i].Config++; |
| 606 | } |
| 607 | } |
| 608 | |
| 609 | c->state |= CONFIG_LOCKED; |
Dominik Brodowski | e2d4096 | 2006-03-02 00:09:29 +0100 | [diff] [blame] | 610 | p_dev->_locked = 1; |
Dominik Brodowski | 059f667 | 2010-03-30 18:07:50 +0200 | [diff] [blame] | 611 | mutex_unlock(&s->ops_mutex); |
Dominik Brodowski | 4c89e88 | 2008-08-03 10:07:45 +0200 | [diff] [blame] | 612 | return 0; |
Dominik Brodowski | 1ac71e5 | 2010-07-29 19:27:09 +0200 | [diff] [blame] | 613 | } /* pcmcia_enable_device */ |
| 614 | EXPORT_SYMBOL(pcmcia_enable_device); |
Dominik Brodowski | 1a8d466 | 2005-06-27 16:28:53 -0700 | [diff] [blame] | 615 | |
| 616 | |
Dominik Brodowski | 2ce4905 | 2010-07-24 13:14:44 +0200 | [diff] [blame] | 617 | /** |
| 618 | * pcmcia_request_io() - attempt to reserve port ranges for PCMCIA devices |
Dominik Brodowski | 9485ee1 | 2010-07-31 07:09:32 +0200 | [diff] [blame] | 619 | * @p_dev: the associated PCMCIA device |
Dominik Brodowski | 1a8d466 | 2005-06-27 16:28:53 -0700 | [diff] [blame] | 620 | * |
Dominik Brodowski | 9485ee1 | 2010-07-31 07:09:32 +0200 | [diff] [blame] | 621 | * pcmcia_request_io() attempts to reserve the IO port ranges specified in |
Dominik Brodowski | 90abdc3 | 2010-07-24 17:23:51 +0200 | [diff] [blame] | 622 | * &struct pcmcia_device @p_dev->resource[0] and @p_dev->resource[1]. The |
Dominik Brodowski | 2ce4905 | 2010-07-24 13:14:44 +0200 | [diff] [blame] | 623 | * "start" value is the requested start of the IO port resource; "end" |
Dominik Brodowski | 90abdc3 | 2010-07-24 17:23:51 +0200 | [diff] [blame] | 624 | * reflects the number of ports requested. The number of IO lines requested |
| 625 | * is specified in &struct pcmcia_device @p_dev->io_lines. |
Dominik Brodowski | 1a8d466 | 2005-06-27 16:28:53 -0700 | [diff] [blame] | 626 | */ |
Dominik Brodowski | 90abdc3 | 2010-07-24 17:23:51 +0200 | [diff] [blame] | 627 | int pcmcia_request_io(struct pcmcia_device *p_dev) |
Dominik Brodowski | 1a8d466 | 2005-06-27 16:28:53 -0700 | [diff] [blame] | 628 | { |
Dominik Brodowski | 2bc5a9b | 2005-07-07 17:59:02 -0700 | [diff] [blame] | 629 | struct pcmcia_socket *s = p_dev->socket; |
Dominik Brodowski | 90abdc3 | 2010-07-24 17:23:51 +0200 | [diff] [blame] | 630 | config_t *c = p_dev->function_config; |
Dominik Brodowski | 94a819f | 2010-01-17 18:31:34 +0100 | [diff] [blame] | 631 | int ret = -EINVAL; |
| 632 | |
| 633 | mutex_lock(&s->ops_mutex); |
Dominik Brodowski | eb838fe | 2010-09-13 16:51:36 +0200 | [diff] [blame] | 634 | dev_dbg(&p_dev->dev, "pcmcia_request_io: %pR , %pR", |
| 635 | &c->io[0], &c->io[1]); |
Dominik Brodowski | 1a8d466 | 2005-06-27 16:28:53 -0700 | [diff] [blame] | 636 | |
Dominik Brodowski | 6d9a299 | 2009-10-24 12:20:18 +0200 | [diff] [blame] | 637 | if (!(s->state & SOCKET_PRESENT)) { |
Dominik Brodowski | eb838fe | 2010-09-13 16:51:36 +0200 | [diff] [blame] | 638 | dev_dbg(&p_dev->dev, "pcmcia_request_io: No card present\n"); |
Dominik Brodowski | 94a819f | 2010-01-17 18:31:34 +0100 | [diff] [blame] | 639 | goto out; |
Dominik Brodowski | 6d9a299 | 2009-10-24 12:20:18 +0200 | [diff] [blame] | 640 | } |
Dominik Brodowski | 1a8d466 | 2005-06-27 16:28:53 -0700 | [diff] [blame] | 641 | |
Dominik Brodowski | 6d9a299 | 2009-10-24 12:20:18 +0200 | [diff] [blame] | 642 | if (c->state & CONFIG_LOCKED) { |
Dominik Brodowski | eb838fe | 2010-09-13 16:51:36 +0200 | [diff] [blame] | 643 | dev_dbg(&p_dev->dev, "Configuration is locked\n"); |
Dominik Brodowski | 94a819f | 2010-01-17 18:31:34 +0100 | [diff] [blame] | 644 | goto out; |
Dominik Brodowski | 6d9a299 | 2009-10-24 12:20:18 +0200 | [diff] [blame] | 645 | } |
Dominik Brodowski | f958095 | 2008-08-03 11:40:19 +0200 | [diff] [blame] | 646 | if (c->state & CONFIG_IO_REQ) { |
Dominik Brodowski | eb838fe | 2010-09-13 16:51:36 +0200 | [diff] [blame] | 647 | dev_dbg(&p_dev->dev, "IO already configured\n"); |
Dominik Brodowski | 94a819f | 2010-01-17 18:31:34 +0100 | [diff] [blame] | 648 | goto out; |
Dominik Brodowski | f958095 | 2008-08-03 11:40:19 +0200 | [diff] [blame] | 649 | } |
Dominik Brodowski | 1a8d466 | 2005-06-27 16:28:53 -0700 | [diff] [blame] | 650 | |
Dominik Brodowski | 90abdc3 | 2010-07-24 17:23:51 +0200 | [diff] [blame] | 651 | ret = alloc_io_space(s, &c->io[0], p_dev->io_lines); |
Dominik Brodowski | 2ce4905 | 2010-07-24 13:14:44 +0200 | [diff] [blame] | 652 | if (ret) |
Dominik Brodowski | 94a819f | 2010-01-17 18:31:34 +0100 | [diff] [blame] | 653 | goto out; |
Dominik Brodowski | 1a8d466 | 2005-06-27 16:28:53 -0700 | [diff] [blame] | 654 | |
Dominik Brodowski | 2ce4905 | 2010-07-24 13:14:44 +0200 | [diff] [blame] | 655 | if (c->io[1].end) { |
Dominik Brodowski | 90abdc3 | 2010-07-24 17:23:51 +0200 | [diff] [blame] | 656 | ret = alloc_io_space(s, &c->io[1], p_dev->io_lines); |
Dominik Brodowski | 94a819f | 2010-01-17 18:31:34 +0100 | [diff] [blame] | 657 | if (ret) { |
Dominik Brodowski | 7cdffc8 | 2010-09-18 10:19:13 +0200 | [diff] [blame] | 658 | struct resource tmp = c->io[0]; |
| 659 | /* release the previously allocated resource */ |
Dominik Brodowski | 2ce4905 | 2010-07-24 13:14:44 +0200 | [diff] [blame] | 660 | release_io_space(s, &c->io[0]); |
Dominik Brodowski | 7cdffc8 | 2010-09-18 10:19:13 +0200 | [diff] [blame] | 661 | /* but preserve the settings, for they worked... */ |
| 662 | c->io[0].end = resource_size(&tmp); |
| 663 | c->io[0].start = tmp.start; |
| 664 | c->io[0].flags = tmp.flags; |
Dominik Brodowski | 94a819f | 2010-01-17 18:31:34 +0100 | [diff] [blame] | 665 | goto out; |
Dominik Brodowski | 1a8d466 | 2005-06-27 16:28:53 -0700 | [diff] [blame] | 666 | } |
Dominik Brodowski | 2ce4905 | 2010-07-24 13:14:44 +0200 | [diff] [blame] | 667 | } else |
| 668 | c->io[1].start = 0; |
Dominik Brodowski | 1a8d466 | 2005-06-27 16:28:53 -0700 | [diff] [blame] | 669 | |
Dominik Brodowski | 1a8d466 | 2005-06-27 16:28:53 -0700 | [diff] [blame] | 670 | c->state |= CONFIG_IO_REQ; |
Dominik Brodowski | e2d4096 | 2006-03-02 00:09:29 +0100 | [diff] [blame] | 671 | p_dev->_io = 1; |
Dominik Brodowski | 94a819f | 2010-01-17 18:31:34 +0100 | [diff] [blame] | 672 | |
Dominik Brodowski | eb838fe | 2010-09-13 16:51:36 +0200 | [diff] [blame] | 673 | dev_dbg(&p_dev->dev, "pcmcia_request_io succeeded: %pR , %pR", |
Dominik Brodowski | 2ce4905 | 2010-07-24 13:14:44 +0200 | [diff] [blame] | 674 | &c->io[0], &c->io[1]); |
Dominik Brodowski | 94a819f | 2010-01-17 18:31:34 +0100 | [diff] [blame] | 675 | out: |
| 676 | mutex_unlock(&s->ops_mutex); |
| 677 | |
| 678 | return ret; |
Dominik Brodowski | 1a8d466 | 2005-06-27 16:28:53 -0700 | [diff] [blame] | 679 | } /* pcmcia_request_io */ |
| 680 | EXPORT_SYMBOL(pcmcia_request_io); |
| 681 | |
| 682 | |
Dominik Brodowski | eb14120 | 2010-03-07 12:21:16 +0100 | [diff] [blame] | 683 | /** |
| 684 | * pcmcia_request_irq() - attempt to request a IRQ for a PCMCIA device |
Dominik Brodowski | 9485ee1 | 2010-07-31 07:09:32 +0200 | [diff] [blame] | 685 | * @p_dev: the associated PCMCIA device |
| 686 | * @handler: IRQ handler to register |
Dominik Brodowski | 1a8d466 | 2005-06-27 16:28:53 -0700 | [diff] [blame] | 687 | * |
Dominik Brodowski | 9485ee1 | 2010-07-31 07:09:32 +0200 | [diff] [blame] | 688 | * pcmcia_request_irq() is a wrapper around request_irq() which allows |
Dominik Brodowski | eb14120 | 2010-03-07 12:21:16 +0100 | [diff] [blame] | 689 | * the PCMCIA core to clean up the registration in pcmcia_disable_device(). |
| 690 | * Drivers are free to use request_irq() directly, but then they need to |
Dominik Brodowski | 9485ee1 | 2010-07-31 07:09:32 +0200 | [diff] [blame] | 691 | * call free_irq() themselfves, too. Also, only %IRQF_SHARED capable IRQ |
Dominik Brodowski | eb14120 | 2010-03-07 12:21:16 +0100 | [diff] [blame] | 692 | * handlers are allowed. |
Dominik Brodowski | 1a8d466 | 2005-06-27 16:28:53 -0700 | [diff] [blame] | 693 | */ |
Dominik Brodowski | eb14120 | 2010-03-07 12:21:16 +0100 | [diff] [blame] | 694 | int __must_check pcmcia_request_irq(struct pcmcia_device *p_dev, |
| 695 | irq_handler_t handler) |
Dominik Brodowski | 1a8d466 | 2005-06-27 16:28:53 -0700 | [diff] [blame] | 696 | { |
Dominik Brodowski | eb14120 | 2010-03-07 12:21:16 +0100 | [diff] [blame] | 697 | int ret; |
Dominik Brodowski | 1a8d466 | 2005-06-27 16:28:53 -0700 | [diff] [blame] | 698 | |
Dominik Brodowski | eb14120 | 2010-03-07 12:21:16 +0100 | [diff] [blame] | 699 | if (!p_dev->irq) |
| 700 | return -EINVAL; |
Dominik Brodowski | 94a819f | 2010-01-17 18:31:34 +0100 | [diff] [blame] | 701 | |
Dominik Brodowski | eb14120 | 2010-03-07 12:21:16 +0100 | [diff] [blame] | 702 | ret = request_irq(p_dev->irq, handler, IRQF_SHARED, |
| 703 | p_dev->devname, p_dev->priv); |
| 704 | if (!ret) |
| 705 | p_dev->_irq = 1; |
Dominik Brodowski | 1a8d466 | 2005-06-27 16:28:53 -0700 | [diff] [blame] | 706 | |
Dominik Brodowski | eb14120 | 2010-03-07 12:21:16 +0100 | [diff] [blame] | 707 | return ret; |
| 708 | } |
| 709 | EXPORT_SYMBOL(pcmcia_request_irq); |
Dominik Brodowski | 1a8d466 | 2005-06-27 16:28:53 -0700 | [diff] [blame] | 710 | |
Dominik Brodowski | eb14120 | 2010-03-07 12:21:16 +0100 | [diff] [blame] | 711 | |
| 712 | /** |
| 713 | * pcmcia_request_exclusive_irq() - attempt to request an exclusive IRQ first |
Dominik Brodowski | 9485ee1 | 2010-07-31 07:09:32 +0200 | [diff] [blame] | 714 | * @p_dev: the associated PCMCIA device |
| 715 | * @handler: IRQ handler to register |
Dominik Brodowski | eb14120 | 2010-03-07 12:21:16 +0100 | [diff] [blame] | 716 | * |
Dominik Brodowski | 9485ee1 | 2010-07-31 07:09:32 +0200 | [diff] [blame] | 717 | * pcmcia_request_exclusive_irq() is a wrapper around request_irq() which |
Dominik Brodowski | eb14120 | 2010-03-07 12:21:16 +0100 | [diff] [blame] | 718 | * attempts first to request an exclusive IRQ. If it fails, it also accepts |
| 719 | * a shared IRQ, but prints out a warning. PCMCIA drivers should allow for |
| 720 | * IRQ sharing and either use request_irq directly (then they need to call |
Dominik Brodowski | 9485ee1 | 2010-07-31 07:09:32 +0200 | [diff] [blame] | 721 | * free_irq() themselves, too), or the pcmcia_request_irq() function. |
Dominik Brodowski | eb14120 | 2010-03-07 12:21:16 +0100 | [diff] [blame] | 722 | */ |
| 723 | int __must_check |
Dominik Brodowski | b19a727 | 2010-03-20 13:10:47 +0100 | [diff] [blame] | 724 | __pcmcia_request_exclusive_irq(struct pcmcia_device *p_dev, |
| 725 | irq_handler_t handler) |
Dominik Brodowski | eb14120 | 2010-03-07 12:21:16 +0100 | [diff] [blame] | 726 | { |
| 727 | int ret; |
| 728 | |
| 729 | if (!p_dev->irq) |
| 730 | return -EINVAL; |
| 731 | |
| 732 | ret = request_irq(p_dev->irq, handler, 0, p_dev->devname, p_dev->priv); |
| 733 | if (ret) { |
| 734 | ret = pcmcia_request_irq(p_dev, handler); |
| 735 | dev_printk(KERN_WARNING, &p_dev->dev, "pcmcia: " |
| 736 | "request for exclusive IRQ could not be fulfilled.\n"); |
Dominik Brodowski | 6f0f38c | 2010-04-08 20:33:16 +0200 | [diff] [blame] | 737 | dev_printk(KERN_WARNING, &p_dev->dev, "pcmcia: the driver " |
| 738 | "needs updating to supported shared IRQ lines.\n"); |
| 739 | } |
Dominik Brodowski | eb14120 | 2010-03-07 12:21:16 +0100 | [diff] [blame] | 740 | if (ret) |
| 741 | dev_printk(KERN_INFO, &p_dev->dev, "request_irq() failed\n"); |
| 742 | else |
| 743 | p_dev->_irq = 1; |
Dominik Brodowski | 6f0f38c | 2010-04-08 20:33:16 +0200 | [diff] [blame] | 744 | |
Dominik Brodowski | 94a819f | 2010-01-17 18:31:34 +0100 | [diff] [blame] | 745 | return ret; |
Dominik Brodowski | eb14120 | 2010-03-07 12:21:16 +0100 | [diff] [blame] | 746 | } /* pcmcia_request_exclusive_irq */ |
Dominik Brodowski | b19a727 | 2010-03-20 13:10:47 +0100 | [diff] [blame] | 747 | EXPORT_SYMBOL(__pcmcia_request_exclusive_irq); |
Dominik Brodowski | 1a8d466 | 2005-06-27 16:28:53 -0700 | [diff] [blame] | 748 | |
| 749 | |
Dominik Brodowski | 6f0f38c | 2010-04-08 20:33:16 +0200 | [diff] [blame] | 750 | #ifdef CONFIG_PCMCIA_PROBE |
| 751 | |
| 752 | /* mask of IRQs already reserved by other cards, we should avoid using them */ |
Dominik Brodowski | 127c03c | 2010-08-03 09:33:45 +0200 | [diff] [blame] | 753 | static u8 pcmcia_used_irq[32]; |
Dominik Brodowski | 6f0f38c | 2010-04-08 20:33:16 +0200 | [diff] [blame] | 754 | |
| 755 | static irqreturn_t test_action(int cpl, void *dev_id) |
| 756 | { |
| 757 | return IRQ_NONE; |
| 758 | } |
| 759 | |
| 760 | /** |
| 761 | * pcmcia_setup_isa_irq() - determine whether an ISA IRQ can be used |
| 762 | * @p_dev - the associated PCMCIA device |
| 763 | * |
| 764 | * locking note: must be called with ops_mutex locked. |
| 765 | */ |
| 766 | static int pcmcia_setup_isa_irq(struct pcmcia_device *p_dev, int type) |
| 767 | { |
| 768 | struct pcmcia_socket *s = p_dev->socket; |
| 769 | unsigned int try, irq; |
| 770 | u32 mask = s->irq_mask; |
| 771 | int ret = -ENODEV; |
| 772 | |
| 773 | for (try = 0; try < 64; try++) { |
| 774 | irq = try % 32; |
| 775 | |
Dominik Brodowski | 127c03c | 2010-08-03 09:33:45 +0200 | [diff] [blame] | 776 | if (irq > NR_IRQS) |
| 777 | continue; |
| 778 | |
Dominik Brodowski | 6f0f38c | 2010-04-08 20:33:16 +0200 | [diff] [blame] | 779 | /* marked as available by driver, not blocked by userspace? */ |
| 780 | if (!((mask >> irq) & 1)) |
| 781 | continue; |
| 782 | |
| 783 | /* avoid an IRQ which is already used by another PCMCIA card */ |
| 784 | if ((try < 32) && pcmcia_used_irq[irq]) |
| 785 | continue; |
| 786 | |
| 787 | /* register the correct driver, if possible, to check whether |
| 788 | * registering a dummy handle works, i.e. if the IRQ isn't |
| 789 | * marked as used by the kernel resource management core */ |
| 790 | ret = request_irq(irq, test_action, type, p_dev->devname, |
| 791 | p_dev); |
| 792 | if (!ret) { |
| 793 | free_irq(irq, p_dev); |
Dominik Brodowski | eb14120 | 2010-03-07 12:21:16 +0100 | [diff] [blame] | 794 | p_dev->irq = s->pcmcia_irq = irq; |
Dominik Brodowski | 6f0f38c | 2010-04-08 20:33:16 +0200 | [diff] [blame] | 795 | pcmcia_used_irq[irq]++; |
| 796 | break; |
| 797 | } |
| 798 | } |
| 799 | |
| 800 | return ret; |
| 801 | } |
| 802 | |
| 803 | void pcmcia_cleanup_irq(struct pcmcia_socket *s) |
| 804 | { |
Dominik Brodowski | 6f840af | 2010-03-07 10:51:23 +0100 | [diff] [blame] | 805 | pcmcia_used_irq[s->pcmcia_irq]--; |
| 806 | s->pcmcia_irq = 0; |
Dominik Brodowski | 6f0f38c | 2010-04-08 20:33:16 +0200 | [diff] [blame] | 807 | } |
| 808 | |
| 809 | #else /* CONFIG_PCMCIA_PROBE */ |
| 810 | |
| 811 | static int pcmcia_setup_isa_irq(struct pcmcia_device *p_dev, int type) |
| 812 | { |
| 813 | return -EINVAL; |
| 814 | } |
| 815 | |
| 816 | void pcmcia_cleanup_irq(struct pcmcia_socket *s) |
| 817 | { |
Dominik Brodowski | 6f840af | 2010-03-07 10:51:23 +0100 | [diff] [blame] | 818 | s->pcmcia_irq = 0; |
Dominik Brodowski | 6f0f38c | 2010-04-08 20:33:16 +0200 | [diff] [blame] | 819 | return; |
| 820 | } |
| 821 | |
| 822 | #endif /* CONFIG_PCMCIA_PROBE */ |
| 823 | |
| 824 | |
| 825 | /** |
| 826 | * pcmcia_setup_irq() - determine IRQ to be used for device |
| 827 | * @p_dev - the associated PCMCIA device |
| 828 | * |
| 829 | * locking note: must be called with ops_mutex locked. |
| 830 | */ |
| 831 | int pcmcia_setup_irq(struct pcmcia_device *p_dev) |
| 832 | { |
| 833 | struct pcmcia_socket *s = p_dev->socket; |
| 834 | |
Dominik Brodowski | eb14120 | 2010-03-07 12:21:16 +0100 | [diff] [blame] | 835 | if (p_dev->irq) |
Dominik Brodowski | 6f0f38c | 2010-04-08 20:33:16 +0200 | [diff] [blame] | 836 | return 0; |
| 837 | |
| 838 | /* already assigned? */ |
Dominik Brodowski | 6f840af | 2010-03-07 10:51:23 +0100 | [diff] [blame] | 839 | if (s->pcmcia_irq) { |
Dominik Brodowski | eb14120 | 2010-03-07 12:21:16 +0100 | [diff] [blame] | 840 | p_dev->irq = s->pcmcia_irq; |
Dominik Brodowski | 6f0f38c | 2010-04-08 20:33:16 +0200 | [diff] [blame] | 841 | return 0; |
| 842 | } |
| 843 | |
| 844 | /* prefer an exclusive ISA irq */ |
| 845 | if (!pcmcia_setup_isa_irq(p_dev, 0)) |
| 846 | return 0; |
| 847 | |
| 848 | /* but accept a shared ISA irq */ |
| 849 | if (!pcmcia_setup_isa_irq(p_dev, IRQF_SHARED)) |
| 850 | return 0; |
| 851 | |
| 852 | /* but use the PCI irq otherwise */ |
| 853 | if (s->pci_irq) { |
Dominik Brodowski | eb14120 | 2010-03-07 12:21:16 +0100 | [diff] [blame] | 854 | p_dev->irq = s->pcmcia_irq = s->pci_irq; |
Dominik Brodowski | 6f0f38c | 2010-04-08 20:33:16 +0200 | [diff] [blame] | 855 | return 0; |
| 856 | } |
| 857 | |
| 858 | return -EINVAL; |
| 859 | } |
| 860 | |
| 861 | |
Dominik Brodowski | cdb1380 | 2010-07-28 10:59:06 +0200 | [diff] [blame] | 862 | /** |
| 863 | * pcmcia_request_window() - attempt to reserve iomem for PCMCIA devices |
Dominik Brodowski | 9485ee1 | 2010-07-31 07:09:32 +0200 | [diff] [blame] | 864 | * @p_dev: the associated PCMCIA device |
| 865 | * @res: &struct resource pointing to p_dev->resource[2..5] |
| 866 | * @speed: access speed |
Dominik Brodowski | 1a8d466 | 2005-06-27 16:28:53 -0700 | [diff] [blame] | 867 | * |
Dominik Brodowski | cdb1380 | 2010-07-28 10:59:06 +0200 | [diff] [blame] | 868 | * pcmcia_request_window() attepts to reserve an iomem ranges specified in |
Dominik Brodowski | 9485ee1 | 2010-07-31 07:09:32 +0200 | [diff] [blame] | 869 | * &struct resource @res pointing to one of the entries in |
| 870 | * &struct pcmcia_device @p_dev->resource[2..5]. The "start" value is the |
Dominik Brodowski | cdb1380 | 2010-07-28 10:59:06 +0200 | [diff] [blame] | 871 | * requested start of the IO mem resource; "end" reflects the size |
| 872 | * requested. |
Dominik Brodowski | 1a8d466 | 2005-06-27 16:28:53 -0700 | [diff] [blame] | 873 | */ |
Dominik Brodowski | cdb1380 | 2010-07-28 10:59:06 +0200 | [diff] [blame] | 874 | int pcmcia_request_window(struct pcmcia_device *p_dev, struct resource *res, |
| 875 | unsigned int speed) |
Dominik Brodowski | 1a8d466 | 2005-06-27 16:28:53 -0700 | [diff] [blame] | 876 | { |
Dominik Brodowski | 6838b03 | 2009-11-03 01:31:52 +0100 | [diff] [blame] | 877 | struct pcmcia_socket *s = p_dev->socket; |
Dominik Brodowski | 82f88e3 | 2009-11-03 01:16:12 +0100 | [diff] [blame] | 878 | pccard_mem_map *win; |
Dominik Brodowski | 1a8d466 | 2005-06-27 16:28:53 -0700 | [diff] [blame] | 879 | u_long align; |
| 880 | int w; |
| 881 | |
Dominik Brodowski | 1c4a77b | 2010-09-18 23:21:09 +0200 | [diff] [blame] | 882 | dev_dbg(&p_dev->dev, "request_window %pR %d\n", res, speed); |
| 883 | |
Dominik Brodowski | 6d9a299 | 2009-10-24 12:20:18 +0200 | [diff] [blame] | 884 | if (!(s->state & SOCKET_PRESENT)) { |
Dominik Brodowski | eb838fe | 2010-09-13 16:51:36 +0200 | [diff] [blame] | 885 | dev_dbg(&p_dev->dev, "No card present\n"); |
Dominik Brodowski | 3939c1ef | 2008-08-03 11:10:56 +0200 | [diff] [blame] | 886 | return -ENODEV; |
Dominik Brodowski | 6d9a299 | 2009-10-24 12:20:18 +0200 | [diff] [blame] | 887 | } |
Dominik Brodowski | 1a8d466 | 2005-06-27 16:28:53 -0700 | [diff] [blame] | 888 | |
| 889 | /* Window size defaults to smallest available */ |
Dominik Brodowski | cdb1380 | 2010-07-28 10:59:06 +0200 | [diff] [blame] | 890 | if (res->end == 0) |
| 891 | res->end = s->map_size; |
| 892 | align = (s->features & SS_CAP_MEM_ALIGN) ? res->end : s->map_size; |
| 893 | if (res->end & (s->map_size-1)) { |
Dominik Brodowski | eb838fe | 2010-09-13 16:51:36 +0200 | [diff] [blame] | 894 | dev_dbg(&p_dev->dev, "invalid map size\n"); |
Dominik Brodowski | 69ba443 | 2008-08-03 12:10:53 +0200 | [diff] [blame] | 895 | return -EINVAL; |
| 896 | } |
Dominik Brodowski | cdb1380 | 2010-07-28 10:59:06 +0200 | [diff] [blame] | 897 | if ((res->start && (s->features & SS_CAP_STATIC_MAP)) || |
| 898 | (res->start & (align-1))) { |
Dominik Brodowski | eb838fe | 2010-09-13 16:51:36 +0200 | [diff] [blame] | 899 | dev_dbg(&p_dev->dev, "invalid base address\n"); |
Dominik Brodowski | 69ba443 | 2008-08-03 12:10:53 +0200 | [diff] [blame] | 900 | return -EINVAL; |
| 901 | } |
Dominik Brodowski | cdb1380 | 2010-07-28 10:59:06 +0200 | [diff] [blame] | 902 | if (res->start) |
Dominik Brodowski | 1a8d466 | 2005-06-27 16:28:53 -0700 | [diff] [blame] | 903 | align = 0; |
| 904 | |
| 905 | /* Allocate system memory window */ |
Dominik Brodowski | 0ca724d | 2010-07-24 19:03:02 +0200 | [diff] [blame] | 906 | mutex_lock(&s->ops_mutex); |
Dominik Brodowski | 1a8d466 | 2005-06-27 16:28:53 -0700 | [diff] [blame] | 907 | for (w = 0; w < MAX_WIN; w++) |
Dominik Brodowski | 9fea84f | 2009-12-07 22:11:45 +0100 | [diff] [blame] | 908 | if (!(s->state & SOCKET_WIN_REQ(w))) |
| 909 | break; |
Dominik Brodowski | f958095 | 2008-08-03 11:40:19 +0200 | [diff] [blame] | 910 | if (w == MAX_WIN) { |
Dominik Brodowski | eb838fe | 2010-09-13 16:51:36 +0200 | [diff] [blame] | 911 | dev_dbg(&p_dev->dev, "all windows are used already\n"); |
Dominik Brodowski | 0ca724d | 2010-07-24 19:03:02 +0200 | [diff] [blame] | 912 | mutex_unlock(&s->ops_mutex); |
Dominik Brodowski | f958095 | 2008-08-03 11:40:19 +0200 | [diff] [blame] | 913 | return -EINVAL; |
| 914 | } |
Dominik Brodowski | 1a8d466 | 2005-06-27 16:28:53 -0700 | [diff] [blame] | 915 | |
| 916 | win = &s->win[w]; |
Dominik Brodowski | 1a8d466 | 2005-06-27 16:28:53 -0700 | [diff] [blame] | 917 | |
| 918 | if (!(s->features & SS_CAP_STATIC_MAP)) { |
Dominik Brodowski | cdb1380 | 2010-07-28 10:59:06 +0200 | [diff] [blame] | 919 | win->res = pcmcia_find_mem_region(res->start, res->end, align, |
Dominik Brodowski | 0ca724d | 2010-07-24 19:03:02 +0200 | [diff] [blame] | 920 | 0, s); |
Dominik Brodowski | 82f88e3 | 2009-11-03 01:16:12 +0100 | [diff] [blame] | 921 | if (!win->res) { |
Dominik Brodowski | eb838fe | 2010-09-13 16:51:36 +0200 | [diff] [blame] | 922 | dev_dbg(&p_dev->dev, "allocating mem region failed\n"); |
Dominik Brodowski | 6b8e087 | 2010-01-12 21:42:51 +0100 | [diff] [blame] | 923 | mutex_unlock(&s->ops_mutex); |
Dominik Brodowski | f958095 | 2008-08-03 11:40:19 +0200 | [diff] [blame] | 924 | return -EINVAL; |
| 925 | } |
Dominik Brodowski | 1a8d466 | 2005-06-27 16:28:53 -0700 | [diff] [blame] | 926 | } |
Dominik Brodowski | 6838b03 | 2009-11-03 01:31:52 +0100 | [diff] [blame] | 927 | p_dev->_win |= CLIENT_WIN_REQ(w); |
Dominik Brodowski | 1a8d466 | 2005-06-27 16:28:53 -0700 | [diff] [blame] | 928 | |
| 929 | /* Configure the socket controller */ |
Dominik Brodowski | 82f88e3 | 2009-11-03 01:16:12 +0100 | [diff] [blame] | 930 | win->map = w+1; |
Dominik Brodowski | cdb1380 | 2010-07-28 10:59:06 +0200 | [diff] [blame] | 931 | win->flags = res->flags & WIN_FLAGS_MAP; |
| 932 | win->speed = speed; |
Dominik Brodowski | 82f88e3 | 2009-11-03 01:16:12 +0100 | [diff] [blame] | 933 | win->card_start = 0; |
Dominik Brodowski | 6b8e087 | 2010-01-12 21:42:51 +0100 | [diff] [blame] | 934 | |
Dominik Brodowski | 82f88e3 | 2009-11-03 01:16:12 +0100 | [diff] [blame] | 935 | if (s->ops->set_mem_map(s, win) != 0) { |
Dominik Brodowski | eb838fe | 2010-09-13 16:51:36 +0200 | [diff] [blame] | 936 | dev_dbg(&p_dev->dev, "failed to set memory mapping\n"); |
Dominik Brodowski | 6b8e087 | 2010-01-12 21:42:51 +0100 | [diff] [blame] | 937 | mutex_unlock(&s->ops_mutex); |
Dominik Brodowski | 926c540 | 2008-08-03 12:15:11 +0200 | [diff] [blame] | 938 | return -EIO; |
| 939 | } |
Dominik Brodowski | 1a8d466 | 2005-06-27 16:28:53 -0700 | [diff] [blame] | 940 | s->state |= SOCKET_WIN_REQ(w); |
| 941 | |
| 942 | /* Return window handle */ |
Dominik Brodowski | 9fea84f | 2009-12-07 22:11:45 +0100 | [diff] [blame] | 943 | if (s->features & SS_CAP_STATIC_MAP) |
Dominik Brodowski | cdb1380 | 2010-07-28 10:59:06 +0200 | [diff] [blame] | 944 | res->start = win->static_start; |
Dominik Brodowski | 9fea84f | 2009-12-07 22:11:45 +0100 | [diff] [blame] | 945 | else |
Dominik Brodowski | cdb1380 | 2010-07-28 10:59:06 +0200 | [diff] [blame] | 946 | res->start = win->res->start; |
Dominik Brodowski | 9fea84f | 2009-12-07 22:11:45 +0100 | [diff] [blame] | 947 | |
Dominik Brodowski | 0ca724d | 2010-07-24 19:03:02 +0200 | [diff] [blame] | 948 | /* convert to new-style resources */ |
Dominik Brodowski | cdb1380 | 2010-07-28 10:59:06 +0200 | [diff] [blame] | 949 | res->end += res->start - 1; |
| 950 | res->flags &= ~WIN_FLAGS_REQ; |
| 951 | res->flags |= (win->map << 2) | IORESOURCE_MEM; |
Dominik Brodowski | ad0c7be | 2010-07-25 13:10:22 +0200 | [diff] [blame] | 952 | res->parent = win->res; |
| 953 | if (win->res) |
| 954 | request_resource(&iomem_resource, res); |
| 955 | |
Dominik Brodowski | eb838fe | 2010-09-13 16:51:36 +0200 | [diff] [blame] | 956 | dev_dbg(&p_dev->dev, "request_window results in %pR\n", res); |
Dominik Brodowski | 0ca724d | 2010-07-24 19:03:02 +0200 | [diff] [blame] | 957 | |
Dominik Brodowski | 6b8e087 | 2010-01-12 21:42:51 +0100 | [diff] [blame] | 958 | mutex_unlock(&s->ops_mutex); |
Dominik Brodowski | 1a8d466 | 2005-06-27 16:28:53 -0700 | [diff] [blame] | 959 | |
Dominik Brodowski | 4c89e88 | 2008-08-03 10:07:45 +0200 | [diff] [blame] | 960 | return 0; |
Dominik Brodowski | 1a8d466 | 2005-06-27 16:28:53 -0700 | [diff] [blame] | 961 | } /* pcmcia_request_window */ |
| 962 | EXPORT_SYMBOL(pcmcia_request_window); |
Dominik Brodowski | 5f2a71f | 2006-01-15 09:32:39 +0100 | [diff] [blame] | 963 | |
Dominik Brodowski | 9485ee1 | 2010-07-31 07:09:32 +0200 | [diff] [blame] | 964 | |
| 965 | /** |
| 966 | * pcmcia_disable_device() - disable and clean up a PCMCIA device |
| 967 | * @p_dev: the associated PCMCIA device |
| 968 | * |
| 969 | * pcmcia_disable_device() is the driver-callable counterpart to |
| 970 | * pcmcia_enable_device(): If a PCMCIA device is no longer used, |
| 971 | * drivers are expected to clean up and disable the device by calling |
| 972 | * this function. Any I/O ranges (iomem and ioports) will be released, |
| 973 | * the Vpp voltage will be set to 0, and IRQs will no longer be |
| 974 | * generated -- at least if there is no other card function (of |
| 975 | * multifunction devices) being used. |
| 976 | */ |
Dominik Brodowski | 9fea84f | 2009-12-07 22:11:45 +0100 | [diff] [blame] | 977 | void pcmcia_disable_device(struct pcmcia_device *p_dev) |
| 978 | { |
Dominik Brodowski | 0ca724d | 2010-07-24 19:03:02 +0200 | [diff] [blame] | 979 | int i; |
Dominik Brodowski | 1c4a77b | 2010-09-18 23:21:09 +0200 | [diff] [blame] | 980 | |
| 981 | dev_dbg(&p_dev->dev, "disabling device\n"); |
| 982 | |
Dominik Brodowski | 0ca724d | 2010-07-24 19:03:02 +0200 | [diff] [blame] | 983 | for (i = 0; i < MAX_WIN; i++) { |
| 984 | struct resource *res = p_dev->resource[MAX_IO_WIN + i]; |
| 985 | if (res->flags & WIN_FLAGS_REQ) |
| 986 | pcmcia_release_window(p_dev, res); |
| 987 | } |
| 988 | |
Dominik Brodowski | 5f2a71f | 2006-01-15 09:32:39 +0100 | [diff] [blame] | 989 | pcmcia_release_configuration(p_dev); |
Dominik Brodowski | 2ce4905 | 2010-07-24 13:14:44 +0200 | [diff] [blame] | 990 | pcmcia_release_io(p_dev); |
Patrick McHardy | 418c527 | 2010-07-20 15:21:42 -0700 | [diff] [blame] | 991 | if (p_dev->_irq) { |
Dominik Brodowski | eb14120 | 2010-03-07 12:21:16 +0100 | [diff] [blame] | 992 | free_irq(p_dev->irq, p_dev->priv); |
Patrick McHardy | 418c527 | 2010-07-20 15:21:42 -0700 | [diff] [blame] | 993 | p_dev->_irq = 0; |
| 994 | } |
Dominik Brodowski | 5f2a71f | 2006-01-15 09:32:39 +0100 | [diff] [blame] | 995 | } |
| 996 | EXPORT_SYMBOL(pcmcia_disable_device); |