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