Commit | Line | Data |
---|---|---|
1a8d4663 DB |
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 | ||
1a8d4663 DB |
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> | |
91284224 | 23 | #include <linux/netdevice.h> |
5a0e3ad6 | 24 | #include <linux/slab.h> |
1a8d4663 | 25 | |
6f0f38c4 DB |
26 | #include <asm/irq.h> |
27 | ||
1a8d4663 DB |
28 | #include <pcmcia/ss.h> |
29 | #include <pcmcia/cs.h> | |
1a8d4663 DB |
30 | #include <pcmcia/cistpl.h> |
31 | #include <pcmcia/cisreg.h> | |
32 | #include <pcmcia/ds.h> | |
33 | ||
34 | #include "cs_internal.h" | |
1a8d4663 DB |
35 | |
36 | ||
1a8d4663 | 37 | /* Access speed for IO windows */ |
9fea84f4 | 38 | static int io_speed; |
1a8d4663 DB |
39 | module_param(io_speed, int, 0444); |
40 | ||
41 | ||
a3ac9af5 DB |
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 | ||
1a8d4663 | 58 | |
ad0c7be2 DB |
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 | ||
1a8d4663 DB |
86 | /** alloc_io_space |
87 | * | |
88 | * Special stuff for managing IO windows, because they are scarce | |
89 | */ | |
2ce4905e DB |
90 | static int alloc_io_space(struct pcmcia_socket *s, struct resource *res, |
91 | unsigned int lines) | |
1a8d4663 | 92 | { |
b19a7275 | 93 | unsigned int align; |
2ce4905e DB |
94 | unsigned int base = res->start; |
95 | unsigned int num = res->end; | |
96 | int ret; | |
97 | ||
98 | res->flags |= IORESOURCE_IO; | |
1a8d4663 | 99 | |
90abdc3b DB |
100 | dev_dbg(&s->dev, "alloc_io_space request for %pR, %d lines\n", |
101 | res, lines); | |
2ce4905e DB |
102 | |
103 | align = base ? (lines ? 1<<lines : 0) : 1; | |
1a8d4663 | 104 | if (align && (align < num)) { |
2ce4905e DB |
105 | if (base) { |
106 | dev_dbg(&s->dev, "odd IO request\n"); | |
1a8d4663 DB |
107 | align = 0; |
108 | } else | |
9fea84f4 DB |
109 | while (align && (align < num)) |
110 | align <<= 1; | |
1a8d4663 | 111 | } |
2ce4905e DB |
112 | if (base & ~(align-1)) { |
113 | dev_dbg(&s->dev, "odd IO request\n"); | |
1a8d4663 DB |
114 | align = 0; |
115 | } | |
b19a7275 | 116 | |
ad0c7be2 DB |
117 | ret = s->resource_ops->find_io(s, res->flags, &base, num, align, |
118 | &res->parent); | |
2ce4905e | 119 | if (ret) { |
ad0c7be2 | 120 | dev_dbg(&s->dev, "alloc_io_space request failed (%d)\n", ret); |
2ce4905e DB |
121 | return -EINVAL; |
122 | } | |
123 | ||
124 | res->start = base; | |
125 | res->end = res->start + num - 1; | |
1a8d4663 | 126 | |
ad0c7be2 DB |
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); | |
1a8d4663 DB |
134 | } |
135 | } | |
ad0c7be2 DB |
136 | dev_dbg(&s->dev, "alloc_io_space request result %d: %pR\n", ret, res); |
137 | return ret; | |
138 | } /* alloc_io_space */ | |
1a8d4663 DB |
139 | |
140 | ||
1d5cc192 DB |
141 | /** |
142 | * pcmcia_access_config() - read or write card configuration registers | |
1a8d4663 | 143 | * |
1d5cc192 DB |
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(). | |
1a8d4663 | 148 | */ |
1d5cc192 DB |
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)) | |
1a8d4663 | 154 | { |
855cdf13 | 155 | struct pcmcia_socket *s; |
1a8d4663 DB |
156 | config_t *c; |
157 | int addr; | |
059f667d | 158 | int ret = 0; |
1a8d4663 | 159 | |
855cdf13 | 160 | s = p_dev->socket; |
94a819f8 DB |
161 | |
162 | mutex_lock(&s->ops_mutex); | |
855cdf13 | 163 | c = p_dev->function_config; |
1a8d4663 | 164 | |
6d9a299f | 165 | if (!(c->state & CONFIG_LOCKED)) { |
eb838fe1 | 166 | dev_dbg(&p_dev->dev, "Configuration isnt't locked\n"); |
94a819f8 | 167 | mutex_unlock(&s->ops_mutex); |
943f70f1 | 168 | return -EACCES; |
6d9a299f | 169 | } |
1a8d4663 | 170 | |
1d5cc192 DB |
171 | addr = (c->ConfigBase + where) >> 1; |
172 | ||
173 | ret = accessf(s, 1, addr, 1, val); | |
174 | ||
059f667d | 175 | mutex_unlock(&s->ops_mutex); |
1d5cc192 | 176 | |
059f667d | 177 | return ret; |
1d5cc192 DB |
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); | |
3448139b | 205 | |
1a8d4663 | 206 | |
cdb13808 | 207 | int pcmcia_map_mem_page(struct pcmcia_device *p_dev, struct resource *res, |
b5cb259e | 208 | unsigned int offset) |
1a8d4663 | 209 | { |
0bdf9b3d | 210 | struct pcmcia_socket *s = p_dev->socket; |
0ca724d3 | 211 | unsigned int w; |
6b8e087b | 212 | int ret; |
868575d1 | 213 | |
0ca724d3 DB |
214 | w = ((res->flags & IORESOURCE_BITS & WIN_FLAGS_REQ) >> 2) - 1; |
215 | if (w >= MAX_WIN) | |
ffb8da20 | 216 | return -EINVAL; |
b5cb259e | 217 | |
6b8e087b | 218 | mutex_lock(&s->ops_mutex); |
0ca724d3 DB |
219 | s->win[w].card_start = offset; |
220 | ret = s->ops->set_mem_map(s, &s->win[w]); | |
6b8e087b | 221 | if (ret) |
eb838fe1 | 222 | dev_warn(&p_dev->dev, "failed to set_mem_map\n"); |
6b8e087b DB |
223 | mutex_unlock(&s->ops_mutex); |
224 | return ret; | |
1a8d4663 DB |
225 | } /* pcmcia_map_mem_page */ |
226 | EXPORT_SYMBOL(pcmcia_map_mem_page); | |
227 | ||
228 | ||
229 | /** pcmcia_modify_configuration | |
230 | * | |
231 | * Modify a locked socket configuration | |
232 | */ | |
2bc5a9bd | 233 | int pcmcia_modify_configuration(struct pcmcia_device *p_dev, |
1a8d4663 DB |
234 | modconf_t *mod) |
235 | { | |
236 | struct pcmcia_socket *s; | |
237 | config_t *c; | |
4e06e240 | 238 | int ret; |
1a8d4663 | 239 | |
2bc5a9bd | 240 | s = p_dev->socket; |
94a819f8 DB |
241 | |
242 | mutex_lock(&s->ops_mutex); | |
dbb22f0d DB |
243 | c = p_dev->function_config; |
244 | ||
6d9a299f | 245 | if (!(s->state & SOCKET_PRESENT)) { |
eb838fe1 | 246 | dev_dbg(&p_dev->dev, "No card present\n"); |
4e06e240 JS |
247 | ret = -ENODEV; |
248 | goto unlock; | |
6d9a299f DB |
249 | } |
250 | if (!(c->state & CONFIG_LOCKED)) { | |
eb838fe1 | 251 | dev_dbg(&p_dev->dev, "Configuration isnt't locked\n"); |
4e06e240 JS |
252 | ret = -EACCES; |
253 | goto unlock; | |
6d9a299f | 254 | } |
1a8d4663 | 255 | |
0cb3c49c | 256 | if (mod->Attributes & (CONF_IRQ_CHANGE_VALID | CONF_VCC_CHANGE_VALID)) { |
eb838fe1 | 257 | dev_dbg(&p_dev->dev, |
0cb3c49c | 258 | "changing Vcc or IRQ is not allowed at this time\n"); |
4e06e240 JS |
259 | ret = -EINVAL; |
260 | goto unlock; | |
d8b0a49d | 261 | } |
1a8d4663 DB |
262 | |
263 | /* We only allow changing Vpp1 and Vpp2 to the same value */ | |
264 | if ((mod->Attributes & CONF_VPP1_CHANGE_VALID) && | |
265 | (mod->Attributes & CONF_VPP2_CHANGE_VALID)) { | |
60df3de8 | 266 | if (mod->Vpp1 != mod->Vpp2) { |
eb838fe1 DB |
267 | dev_dbg(&p_dev->dev, |
268 | "Vpp1 and Vpp2 must be the same\n"); | |
4e06e240 JS |
269 | ret = -EINVAL; |
270 | goto unlock; | |
60df3de8 | 271 | } |
71ed90d8 | 272 | s->socket.Vpp = mod->Vpp1; |
d8b0a49d | 273 | if (s->ops->set_socket(s, &s->socket)) { |
eb838fe1 | 274 | dev_printk(KERN_WARNING, &p_dev->dev, |
d8b0a49d | 275 | "Unable to set VPP\n"); |
4e06e240 JS |
276 | ret = -EIO; |
277 | goto unlock; | |
d8b0a49d | 278 | } |
1a8d4663 | 279 | } else if ((mod->Attributes & CONF_VPP1_CHANGE_VALID) || |
d8b0a49d | 280 | (mod->Attributes & CONF_VPP2_CHANGE_VALID)) { |
eb838fe1 DB |
281 | dev_dbg(&p_dev->dev, |
282 | "changing Vcc is not allowed at this time\n"); | |
4e06e240 JS |
283 | ret = -EINVAL; |
284 | goto unlock; | |
d8b0a49d | 285 | } |
1a8d4663 | 286 | |
4bbed523 DB |
287 | if (mod->Attributes & CONF_IO_CHANGE_WIDTH) { |
288 | pccard_io_map io_off = { 0, 0, 0, 0, 1 }; | |
289 | pccard_io_map io_on; | |
290 | int i; | |
291 | ||
292 | io_on.speed = io_speed; | |
293 | for (i = 0; i < MAX_IO_WIN; i++) { | |
294 | if (!s->io[i].res) | |
295 | continue; | |
296 | io_off.map = i; | |
297 | io_on.map = i; | |
298 | ||
299 | io_on.flags = MAP_ACTIVE | IO_DATA_PATH_WIDTH_8; | |
300 | io_on.start = s->io[i].res->start; | |
301 | io_on.stop = s->io[i].res->end; | |
302 | ||
303 | s->ops->set_io_map(s, &io_off); | |
304 | mdelay(40); | |
305 | s->ops->set_io_map(s, &io_on); | |
306 | } | |
307 | } | |
4e06e240 JS |
308 | ret = 0; |
309 | unlock: | |
94a819f8 | 310 | mutex_unlock(&s->ops_mutex); |
4bbed523 | 311 | |
4e06e240 | 312 | return ret; |
1a8d4663 DB |
313 | } /* modify_configuration */ |
314 | EXPORT_SYMBOL(pcmcia_modify_configuration); | |
315 | ||
316 | ||
2bc5a9bd | 317 | int pcmcia_release_configuration(struct pcmcia_device *p_dev) |
1a8d4663 DB |
318 | { |
319 | pccard_io_map io = { 0, 0, 0, 0, 1 }; | |
2bc5a9bd | 320 | struct pcmcia_socket *s = p_dev->socket; |
94a819f8 | 321 | config_t *c; |
1a8d4663 DB |
322 | int i; |
323 | ||
9e86749c | 324 | mutex_lock(&s->ops_mutex); |
94a819f8 | 325 | c = p_dev->function_config; |
e2d40963 DB |
326 | if (p_dev->_locked) { |
327 | p_dev->_locked = 0; | |
1a8d4663 DB |
328 | if (--(s->lock_count) == 0) { |
329 | s->socket.flags = SS_OUTPUT_ENA; /* Is this correct? */ | |
330 | s->socket.Vpp = 0; | |
331 | s->socket.io_irq = 0; | |
332 | s->ops->set_socket(s, &s->socket); | |
333 | } | |
5f2a71fc DB |
334 | } |
335 | if (c->state & CONFIG_LOCKED) { | |
336 | c->state &= ~CONFIG_LOCKED; | |
1a8d4663 DB |
337 | if (c->state & CONFIG_IO_REQ) |
338 | for (i = 0; i < MAX_IO_WIN; i++) { | |
c7d00693 | 339 | if (!s->io[i].res) |
1a8d4663 DB |
340 | continue; |
341 | s->io[i].Config--; | |
342 | if (s->io[i].Config != 0) | |
343 | continue; | |
344 | io.map = i; | |
345 | s->ops->set_io_map(s, &io); | |
346 | } | |
1a8d4663 | 347 | } |
9e86749c | 348 | mutex_unlock(&s->ops_mutex); |
1a8d4663 | 349 | |
4c89e88b | 350 | return 0; |
1a8d4663 | 351 | } /* pcmcia_release_configuration */ |
1a8d4663 DB |
352 | |
353 | ||
354 | /** pcmcia_release_io | |
355 | * | |
356 | * Release_io() releases the I/O ranges allocated by a client. This | |
357 | * may be invoked some time after a card ejection has already dumped | |
358 | * the actual socket configuration, so if the client is "stale", we | |
359 | * don't bother checking the port ranges against the current socket | |
360 | * values. | |
361 | */ | |
2ce4905e | 362 | static int pcmcia_release_io(struct pcmcia_device *p_dev) |
1a8d4663 | 363 | { |
2bc5a9bd | 364 | struct pcmcia_socket *s = p_dev->socket; |
94a819f8 DB |
365 | int ret = -EINVAL; |
366 | config_t *c; | |
367 | ||
368 | mutex_lock(&s->ops_mutex); | |
9fea84f4 | 369 | if (!p_dev->_io) |
94a819f8 | 370 | goto out; |
5f2a71fc | 371 | |
2ce4905e | 372 | c = p_dev->function_config; |
1a8d4663 | 373 | |
2ce4905e | 374 | release_io_space(s, &c->io[0]); |
5f2a71fc | 375 | |
2ce4905e DB |
376 | if (c->io[1].end) |
377 | release_io_space(s, &c->io[1]); | |
1a8d4663 | 378 | |
2ce4905e DB |
379 | p_dev->_io = 0; |
380 | c->state &= ~CONFIG_IO_REQ; | |
1a8d4663 | 381 | |
94a819f8 DB |
382 | out: |
383 | mutex_unlock(&s->ops_mutex); | |
384 | ||
385 | return ret; | |
1a8d4663 | 386 | } /* pcmcia_release_io */ |
1a8d4663 | 387 | |
cdb13808 DB |
388 | /** |
389 | * pcmcia_release_window() - release reserved iomem for PCMCIA devices | |
390 | * | |
391 | * pcmcia_release_window() releases struct resource *res which was | |
392 | * previously reserved by calling pcmcia_request_window(). | |
393 | */ | |
0ca724d3 | 394 | int pcmcia_release_window(struct pcmcia_device *p_dev, struct resource *res) |
1a8d4663 | 395 | { |
0bdf9b3d | 396 | struct pcmcia_socket *s = p_dev->socket; |
82f88e36 | 397 | pccard_mem_map *win; |
0ca724d3 | 398 | unsigned int w; |
1a8d4663 | 399 | |
0ca724d3 DB |
400 | dev_dbg(&p_dev->dev, "releasing window %pR\n", res); |
401 | ||
402 | w = ((res->flags & IORESOURCE_BITS & WIN_FLAGS_REQ) >> 2) - 1; | |
403 | if (w >= MAX_WIN) | |
ffb8da20 | 404 | return -EINVAL; |
0bdf9b3d | 405 | |
6b8e087b | 406 | mutex_lock(&s->ops_mutex); |
0ca724d3 | 407 | win = &s->win[w]; |
0bdf9b3d | 408 | |
0ca724d3 | 409 | if (!(p_dev->_win & CLIENT_WIN_REQ(w))) { |
eb838fe1 | 410 | dev_dbg(&p_dev->dev, "not releasing unknown window\n"); |
6b8e087b | 411 | mutex_unlock(&s->ops_mutex); |
ffb8da20 | 412 | return -EINVAL; |
6d9a299f | 413 | } |
1a8d4663 DB |
414 | |
415 | /* Shut down memory window */ | |
82f88e36 DB |
416 | win->flags &= ~MAP_ACTIVE; |
417 | s->ops->set_mem_map(s, win); | |
0ca724d3 | 418 | s->state &= ~SOCKET_WIN_REQ(w); |
1a8d4663 DB |
419 | |
420 | /* Release system memory */ | |
82f88e36 | 421 | if (win->res) { |
ad0c7be2 | 422 | release_resource(res); |
82f88e36 DB |
423 | release_resource(win->res); |
424 | kfree(win->res); | |
425 | win->res = NULL; | |
1a8d4663 | 426 | } |
cdb13808 DB |
427 | res->start = res->end = 0; |
428 | res->flags = IORESOURCE_MEM; | |
0ca724d3 | 429 | p_dev->_win &= ~CLIENT_WIN_REQ(w); |
6b8e087b | 430 | mutex_unlock(&s->ops_mutex); |
1a8d4663 | 431 | |
4c89e88b | 432 | return 0; |
1a8d4663 DB |
433 | } /* pcmcia_release_window */ |
434 | EXPORT_SYMBOL(pcmcia_release_window); | |
435 | ||
436 | ||
2bc5a9bd | 437 | int pcmcia_request_configuration(struct pcmcia_device *p_dev, |
1a8d4663 DB |
438 | config_req_t *req) |
439 | { | |
440 | int i; | |
441 | u_int base; | |
2bc5a9bd | 442 | struct pcmcia_socket *s = p_dev->socket; |
1a8d4663 DB |
443 | config_t *c; |
444 | pccard_io_map iomap; | |
445 | ||
1a8d4663 | 446 | if (!(s->state & SOCKET_PRESENT)) |
d598de02 | 447 | return -ENODEV; |
1a8d4663 | 448 | |
de6405e9 | 449 | if (req->IntType & INT_CARDBUS) { |
eb838fe1 | 450 | dev_dbg(&p_dev->dev, "IntType may not be INT_CARDBUS\n"); |
de6405e9 DB |
451 | return -EINVAL; |
452 | } | |
94a819f8 DB |
453 | |
454 | mutex_lock(&s->ops_mutex); | |
dbb22f0d | 455 | c = p_dev->function_config; |
6d9a299f | 456 | if (c->state & CONFIG_LOCKED) { |
94a819f8 | 457 | mutex_unlock(&s->ops_mutex); |
eb838fe1 | 458 | dev_dbg(&p_dev->dev, "Configuration is locked\n"); |
943f70f1 | 459 | return -EACCES; |
6d9a299f | 460 | } |
1a8d4663 DB |
461 | |
462 | /* Do power control. We don't allow changes in Vcc. */ | |
70294b46 | 463 | s->socket.Vpp = req->Vpp; |
d8b0a49d | 464 | if (s->ops->set_socket(s, &s->socket)) { |
9e86749c | 465 | mutex_unlock(&s->ops_mutex); |
eb838fe1 | 466 | dev_printk(KERN_WARNING, &p_dev->dev, |
d8b0a49d DB |
467 | "Unable to set socket state\n"); |
468 | return -EINVAL; | |
469 | } | |
1a8d4663 | 470 | |
1a8d4663 DB |
471 | /* Pick memory or I/O card, DMA mode, interrupt */ |
472 | c->IntType = req->IntType; | |
473 | c->Attributes = req->Attributes; | |
474 | if (req->IntType & INT_MEMORY_AND_IO) | |
475 | s->socket.flags |= SS_IOCARD; | |
476 | if (req->IntType & INT_ZOOMED_VIDEO) | |
477 | s->socket.flags |= SS_ZVCARD | SS_IOCARD; | |
478 | if (req->Attributes & CONF_ENABLE_DMA) | |
479 | s->socket.flags |= SS_DMA_MODE; | |
480 | if (req->Attributes & CONF_ENABLE_SPKR) | |
481 | s->socket.flags |= SS_SPKR_ENA; | |
482 | if (req->Attributes & CONF_ENABLE_IRQ) | |
6f840afb | 483 | s->socket.io_irq = s->pcmcia_irq; |
1a8d4663 DB |
484 | else |
485 | s->socket.io_irq = 0; | |
486 | s->ops->set_socket(s, &s->socket); | |
487 | s->lock_count++; | |
488 | ||
489 | /* Set up CIS configuration registers */ | |
490 | base = c->ConfigBase = req->ConfigBase; | |
1ae9c7d8 | 491 | c->CardValues = req->Present; |
1a8d4663 DB |
492 | if (req->Present & PRESENT_COPY) { |
493 | c->Copy = req->Copy; | |
494 | pcmcia_write_cis_mem(s, 1, (base + CISREG_SCR)>>1, 1, &c->Copy); | |
495 | } | |
496 | if (req->Present & PRESENT_OPTION) { | |
497 | if (s->functions == 1) { | |
498 | c->Option = req->ConfigIndex & COR_CONFIG_MASK; | |
499 | } else { | |
500 | c->Option = req->ConfigIndex & COR_MFC_CONFIG_MASK; | |
501 | c->Option |= COR_FUNC_ENA|COR_IREQ_ENA; | |
502 | if (req->Present & PRESENT_IOBASE_0) | |
503 | c->Option |= COR_ADDR_DECODE; | |
504 | } | |
a7debe78 DB |
505 | if ((req->Attributes & CONF_ENABLE_IRQ) && |
506 | !(req->Attributes & CONF_ENABLE_PULSE_IRQ)) | |
507 | c->Option |= COR_LEVEL_REQ; | |
1a8d4663 DB |
508 | pcmcia_write_cis_mem(s, 1, (base + CISREG_COR)>>1, 1, &c->Option); |
509 | mdelay(40); | |
510 | } | |
511 | if (req->Present & PRESENT_STATUS) { | |
512 | c->Status = req->Status; | |
513 | pcmcia_write_cis_mem(s, 1, (base + CISREG_CCSR)>>1, 1, &c->Status); | |
514 | } | |
515 | if (req->Present & PRESENT_PIN_REPLACE) { | |
516 | c->Pin = req->Pin; | |
517 | pcmcia_write_cis_mem(s, 1, (base + CISREG_PRR)>>1, 1, &c->Pin); | |
518 | } | |
519 | if (req->Present & PRESENT_EXT_STATUS) { | |
520 | c->ExtStatus = req->ExtStatus; | |
521 | pcmcia_write_cis_mem(s, 1, (base + CISREG_ESR)>>1, 1, &c->ExtStatus); | |
522 | } | |
523 | if (req->Present & PRESENT_IOBASE_0) { | |
2ce4905e | 524 | u8 b = c->io[0].start & 0xff; |
1a8d4663 | 525 | pcmcia_write_cis_mem(s, 1, (base + CISREG_IOBASE_0)>>1, 1, &b); |
2ce4905e | 526 | b = (c->io[0].start >> 8) & 0xff; |
1a8d4663 DB |
527 | pcmcia_write_cis_mem(s, 1, (base + CISREG_IOBASE_1)>>1, 1, &b); |
528 | } | |
529 | if (req->Present & PRESENT_IOSIZE) { | |
2ce4905e | 530 | u8 b = resource_size(&c->io[0]) + resource_size(&c->io[1]) - 1; |
1a8d4663 DB |
531 | pcmcia_write_cis_mem(s, 1, (base + CISREG_IOSIZE)>>1, 1, &b); |
532 | } | |
533 | ||
534 | /* Configure I/O windows */ | |
535 | if (c->state & CONFIG_IO_REQ) { | |
536 | iomap.speed = io_speed; | |
537 | for (i = 0; i < MAX_IO_WIN; i++) | |
c7d00693 | 538 | if (s->io[i].res) { |
1a8d4663 DB |
539 | iomap.map = i; |
540 | iomap.flags = MAP_ACTIVE; | |
c7d00693 | 541 | switch (s->io[i].res->flags & IO_DATA_PATH_WIDTH) { |
1a8d4663 DB |
542 | case IO_DATA_PATH_WIDTH_16: |
543 | iomap.flags |= MAP_16BIT; break; | |
544 | case IO_DATA_PATH_WIDTH_AUTO: | |
545 | iomap.flags |= MAP_AUTOSZ; break; | |
546 | default: | |
547 | break; | |
548 | } | |
c7d00693 DB |
549 | iomap.start = s->io[i].res->start; |
550 | iomap.stop = s->io[i].res->end; | |
1a8d4663 DB |
551 | s->ops->set_io_map(s, &iomap); |
552 | s->io[i].Config++; | |
553 | } | |
554 | } | |
555 | ||
556 | c->state |= CONFIG_LOCKED; | |
e2d40963 | 557 | p_dev->_locked = 1; |
059f667d | 558 | mutex_unlock(&s->ops_mutex); |
4c89e88b | 559 | return 0; |
1a8d4663 DB |
560 | } /* pcmcia_request_configuration */ |
561 | EXPORT_SYMBOL(pcmcia_request_configuration); | |
562 | ||
563 | ||
2ce4905e DB |
564 | /** |
565 | * pcmcia_request_io() - attempt to reserve port ranges for PCMCIA devices | |
566 | * | |
567 | * pcmcia_request_io() attepts to reserve the IO port ranges specified in | |
90abdc3b | 568 | * &struct pcmcia_device @p_dev->resource[0] and @p_dev->resource[1]. The |
2ce4905e | 569 | * "start" value is the requested start of the IO port resource; "end" |
90abdc3b DB |
570 | * reflects the number of ports requested. The number of IO lines requested |
571 | * is specified in &struct pcmcia_device @p_dev->io_lines. | |
1a8d4663 | 572 | */ |
90abdc3b | 573 | int pcmcia_request_io(struct pcmcia_device *p_dev) |
1a8d4663 | 574 | { |
2bc5a9bd | 575 | struct pcmcia_socket *s = p_dev->socket; |
90abdc3b | 576 | config_t *c = p_dev->function_config; |
94a819f8 DB |
577 | int ret = -EINVAL; |
578 | ||
579 | mutex_lock(&s->ops_mutex); | |
eb838fe1 DB |
580 | dev_dbg(&p_dev->dev, "pcmcia_request_io: %pR , %pR", |
581 | &c->io[0], &c->io[1]); | |
1a8d4663 | 582 | |
6d9a299f | 583 | if (!(s->state & SOCKET_PRESENT)) { |
eb838fe1 | 584 | dev_dbg(&p_dev->dev, "pcmcia_request_io: No card present\n"); |
94a819f8 | 585 | goto out; |
6d9a299f | 586 | } |
1a8d4663 | 587 | |
6d9a299f | 588 | if (c->state & CONFIG_LOCKED) { |
eb838fe1 | 589 | dev_dbg(&p_dev->dev, "Configuration is locked\n"); |
94a819f8 | 590 | goto out; |
6d9a299f | 591 | } |
f958095e | 592 | if (c->state & CONFIG_IO_REQ) { |
eb838fe1 | 593 | dev_dbg(&p_dev->dev, "IO already configured\n"); |
94a819f8 | 594 | goto out; |
f958095e | 595 | } |
1a8d4663 | 596 | |
90abdc3b | 597 | ret = alloc_io_space(s, &c->io[0], p_dev->io_lines); |
2ce4905e | 598 | if (ret) |
94a819f8 | 599 | goto out; |
1a8d4663 | 600 | |
2ce4905e | 601 | if (c->io[1].end) { |
90abdc3b | 602 | ret = alloc_io_space(s, &c->io[1], p_dev->io_lines); |
94a819f8 | 603 | if (ret) { |
7cdffc86 DB |
604 | struct resource tmp = c->io[0]; |
605 | /* release the previously allocated resource */ | |
2ce4905e | 606 | release_io_space(s, &c->io[0]); |
7cdffc86 DB |
607 | /* but preserve the settings, for they worked... */ |
608 | c->io[0].end = resource_size(&tmp); | |
609 | c->io[0].start = tmp.start; | |
610 | c->io[0].flags = tmp.flags; | |
94a819f8 | 611 | goto out; |
1a8d4663 | 612 | } |
2ce4905e DB |
613 | } else |
614 | c->io[1].start = 0; | |
1a8d4663 | 615 | |
1a8d4663 | 616 | c->state |= CONFIG_IO_REQ; |
e2d40963 | 617 | p_dev->_io = 1; |
94a819f8 | 618 | |
eb838fe1 | 619 | dev_dbg(&p_dev->dev, "pcmcia_request_io succeeded: %pR , %pR", |
2ce4905e | 620 | &c->io[0], &c->io[1]); |
94a819f8 DB |
621 | out: |
622 | mutex_unlock(&s->ops_mutex); | |
623 | ||
624 | return ret; | |
1a8d4663 DB |
625 | } /* pcmcia_request_io */ |
626 | EXPORT_SYMBOL(pcmcia_request_io); | |
627 | ||
628 | ||
eb14120f DB |
629 | /** |
630 | * pcmcia_request_irq() - attempt to request a IRQ for a PCMCIA device | |
1a8d4663 | 631 | * |
eb14120f DB |
632 | * pcmcia_request_irq() is a wrapper around request_irq which will allow |
633 | * the PCMCIA core to clean up the registration in pcmcia_disable_device(). | |
634 | * Drivers are free to use request_irq() directly, but then they need to | |
635 | * call free_irq themselfves, too. Also, only IRQF_SHARED capable IRQ | |
636 | * handlers are allowed. | |
1a8d4663 | 637 | */ |
eb14120f DB |
638 | int __must_check pcmcia_request_irq(struct pcmcia_device *p_dev, |
639 | irq_handler_t handler) | |
1a8d4663 | 640 | { |
eb14120f | 641 | int ret; |
1a8d4663 | 642 | |
eb14120f DB |
643 | if (!p_dev->irq) |
644 | return -EINVAL; | |
94a819f8 | 645 | |
eb14120f DB |
646 | ret = request_irq(p_dev->irq, handler, IRQF_SHARED, |
647 | p_dev->devname, p_dev->priv); | |
648 | if (!ret) | |
649 | p_dev->_irq = 1; | |
1a8d4663 | 650 | |
eb14120f DB |
651 | return ret; |
652 | } | |
653 | EXPORT_SYMBOL(pcmcia_request_irq); | |
6f0f38c4 | 654 | |
1a8d4663 | 655 | |
eb14120f DB |
656 | /** |
657 | * pcmcia_request_exclusive_irq() - attempt to request an exclusive IRQ first | |
658 | * | |
659 | * pcmcia_request_exclusive_irq() is a wrapper around request_irq which | |
660 | * attempts first to request an exclusive IRQ. If it fails, it also accepts | |
661 | * a shared IRQ, but prints out a warning. PCMCIA drivers should allow for | |
662 | * IRQ sharing and either use request_irq directly (then they need to call | |
663 | * free_irq themselves, too), or the pcmcia_request_irq() function. | |
664 | */ | |
665 | int __must_check | |
b19a7275 DB |
666 | __pcmcia_request_exclusive_irq(struct pcmcia_device *p_dev, |
667 | irq_handler_t handler) | |
eb14120f DB |
668 | { |
669 | int ret; | |
1a8d4663 | 670 | |
eb14120f DB |
671 | if (!p_dev->irq) |
672 | return -EINVAL; | |
1a8d4663 | 673 | |
eb14120f DB |
674 | ret = request_irq(p_dev->irq, handler, 0, p_dev->devname, p_dev->priv); |
675 | if (ret) { | |
676 | ret = pcmcia_request_irq(p_dev, handler); | |
677 | dev_printk(KERN_WARNING, &p_dev->dev, "pcmcia: " | |
678 | "request for exclusive IRQ could not be fulfilled.\n"); | |
679 | dev_printk(KERN_WARNING, &p_dev->dev, "pcmcia: the driver " | |
680 | "needs updating to supported shared IRQ lines.\n"); | |
681 | } | |
682 | if (ret) | |
683 | dev_printk(KERN_INFO, &p_dev->dev, "request_irq() failed\n"); | |
684 | else | |
685 | p_dev->_irq = 1; | |
1a8d4663 | 686 | |
94a819f8 | 687 | return ret; |
eb14120f | 688 | } /* pcmcia_request_exclusive_irq */ |
b19a7275 | 689 | EXPORT_SYMBOL(__pcmcia_request_exclusive_irq); |
1a8d4663 DB |
690 | |
691 | ||
6f0f38c4 DB |
692 | #ifdef CONFIG_PCMCIA_PROBE |
693 | ||
694 | /* mask of IRQs already reserved by other cards, we should avoid using them */ | |
127c03cd | 695 | static u8 pcmcia_used_irq[32]; |
6f0f38c4 DB |
696 | |
697 | static irqreturn_t test_action(int cpl, void *dev_id) | |
698 | { | |
699 | return IRQ_NONE; | |
700 | } | |
701 | ||
702 | /** | |
703 | * pcmcia_setup_isa_irq() - determine whether an ISA IRQ can be used | |
704 | * @p_dev - the associated PCMCIA device | |
705 | * | |
706 | * locking note: must be called with ops_mutex locked. | |
707 | */ | |
708 | static int pcmcia_setup_isa_irq(struct pcmcia_device *p_dev, int type) | |
709 | { | |
710 | struct pcmcia_socket *s = p_dev->socket; | |
711 | unsigned int try, irq; | |
712 | u32 mask = s->irq_mask; | |
713 | int ret = -ENODEV; | |
714 | ||
715 | for (try = 0; try < 64; try++) { | |
716 | irq = try % 32; | |
717 | ||
127c03cd DB |
718 | if (irq > NR_IRQS) |
719 | continue; | |
720 | ||
6f0f38c4 DB |
721 | /* marked as available by driver, not blocked by userspace? */ |
722 | if (!((mask >> irq) & 1)) | |
723 | continue; | |
724 | ||
725 | /* avoid an IRQ which is already used by another PCMCIA card */ | |
726 | if ((try < 32) && pcmcia_used_irq[irq]) | |
727 | continue; | |
728 | ||
729 | /* register the correct driver, if possible, to check whether | |
730 | * registering a dummy handle works, i.e. if the IRQ isn't | |
731 | * marked as used by the kernel resource management core */ | |
732 | ret = request_irq(irq, test_action, type, p_dev->devname, | |
733 | p_dev); | |
734 | if (!ret) { | |
735 | free_irq(irq, p_dev); | |
eb14120f | 736 | p_dev->irq = s->pcmcia_irq = irq; |
6f0f38c4 DB |
737 | pcmcia_used_irq[irq]++; |
738 | break; | |
739 | } | |
740 | } | |
741 | ||
742 | return ret; | |
743 | } | |
744 | ||
745 | void pcmcia_cleanup_irq(struct pcmcia_socket *s) | |
746 | { | |
6f840afb DB |
747 | pcmcia_used_irq[s->pcmcia_irq]--; |
748 | s->pcmcia_irq = 0; | |
6f0f38c4 DB |
749 | } |
750 | ||
751 | #else /* CONFIG_PCMCIA_PROBE */ | |
752 | ||
753 | static int pcmcia_setup_isa_irq(struct pcmcia_device *p_dev, int type) | |
754 | { | |
755 | return -EINVAL; | |
756 | } | |
757 | ||
758 | void pcmcia_cleanup_irq(struct pcmcia_socket *s) | |
759 | { | |
6f840afb | 760 | s->pcmcia_irq = 0; |
6f0f38c4 DB |
761 | return; |
762 | } | |
763 | ||
764 | #endif /* CONFIG_PCMCIA_PROBE */ | |
765 | ||
766 | ||
767 | /** | |
768 | * pcmcia_setup_irq() - determine IRQ to be used for device | |
769 | * @p_dev - the associated PCMCIA device | |
770 | * | |
771 | * locking note: must be called with ops_mutex locked. | |
772 | */ | |
773 | int pcmcia_setup_irq(struct pcmcia_device *p_dev) | |
774 | { | |
775 | struct pcmcia_socket *s = p_dev->socket; | |
776 | ||
eb14120f | 777 | if (p_dev->irq) |
6f0f38c4 DB |
778 | return 0; |
779 | ||
780 | /* already assigned? */ | |
6f840afb | 781 | if (s->pcmcia_irq) { |
eb14120f | 782 | p_dev->irq = s->pcmcia_irq; |
6f0f38c4 DB |
783 | return 0; |
784 | } | |
785 | ||
786 | /* prefer an exclusive ISA irq */ | |
787 | if (!pcmcia_setup_isa_irq(p_dev, 0)) | |
788 | return 0; | |
789 | ||
790 | /* but accept a shared ISA irq */ | |
791 | if (!pcmcia_setup_isa_irq(p_dev, IRQF_SHARED)) | |
792 | return 0; | |
793 | ||
794 | /* but use the PCI irq otherwise */ | |
795 | if (s->pci_irq) { | |
eb14120f | 796 | p_dev->irq = s->pcmcia_irq = s->pci_irq; |
6f0f38c4 DB |
797 | return 0; |
798 | } | |
799 | ||
800 | return -EINVAL; | |
801 | } | |
802 | ||
803 | ||
cdb13808 DB |
804 | /** |
805 | * pcmcia_request_window() - attempt to reserve iomem for PCMCIA devices | |
1a8d4663 | 806 | * |
cdb13808 DB |
807 | * pcmcia_request_window() attepts to reserve an iomem ranges specified in |
808 | * struct resource *res pointing to one of the entries in | |
809 | * struct pcmcia_device *p_dev->resource[2..5]. The "start" value is the | |
810 | * requested start of the IO mem resource; "end" reflects the size | |
811 | * requested. | |
1a8d4663 | 812 | */ |
cdb13808 DB |
813 | int pcmcia_request_window(struct pcmcia_device *p_dev, struct resource *res, |
814 | unsigned int speed) | |
1a8d4663 | 815 | { |
6838b03f | 816 | struct pcmcia_socket *s = p_dev->socket; |
82f88e36 | 817 | pccard_mem_map *win; |
1a8d4663 DB |
818 | u_long align; |
819 | int w; | |
820 | ||
6d9a299f | 821 | if (!(s->state & SOCKET_PRESENT)) { |
eb838fe1 | 822 | dev_dbg(&p_dev->dev, "No card present\n"); |
3939c1ef | 823 | return -ENODEV; |
6d9a299f | 824 | } |
1a8d4663 DB |
825 | |
826 | /* Window size defaults to smallest available */ | |
cdb13808 DB |
827 | if (res->end == 0) |
828 | res->end = s->map_size; | |
829 | align = (s->features & SS_CAP_MEM_ALIGN) ? res->end : s->map_size; | |
830 | if (res->end & (s->map_size-1)) { | |
eb838fe1 | 831 | dev_dbg(&p_dev->dev, "invalid map size\n"); |
69ba4433 DB |
832 | return -EINVAL; |
833 | } | |
cdb13808 DB |
834 | if ((res->start && (s->features & SS_CAP_STATIC_MAP)) || |
835 | (res->start & (align-1))) { | |
eb838fe1 | 836 | dev_dbg(&p_dev->dev, "invalid base address\n"); |
69ba4433 DB |
837 | return -EINVAL; |
838 | } | |
cdb13808 | 839 | if (res->start) |
1a8d4663 DB |
840 | align = 0; |
841 | ||
842 | /* Allocate system memory window */ | |
0ca724d3 | 843 | mutex_lock(&s->ops_mutex); |
1a8d4663 | 844 | for (w = 0; w < MAX_WIN; w++) |
9fea84f4 DB |
845 | if (!(s->state & SOCKET_WIN_REQ(w))) |
846 | break; | |
f958095e | 847 | if (w == MAX_WIN) { |
eb838fe1 | 848 | dev_dbg(&p_dev->dev, "all windows are used already\n"); |
0ca724d3 | 849 | mutex_unlock(&s->ops_mutex); |
f958095e DB |
850 | return -EINVAL; |
851 | } | |
1a8d4663 DB |
852 | |
853 | win = &s->win[w]; | |
1a8d4663 DB |
854 | |
855 | if (!(s->features & SS_CAP_STATIC_MAP)) { | |
cdb13808 | 856 | win->res = pcmcia_find_mem_region(res->start, res->end, align, |
0ca724d3 | 857 | 0, s); |
82f88e36 | 858 | if (!win->res) { |
eb838fe1 | 859 | dev_dbg(&p_dev->dev, "allocating mem region failed\n"); |
6b8e087b | 860 | mutex_unlock(&s->ops_mutex); |
f958095e DB |
861 | return -EINVAL; |
862 | } | |
1a8d4663 | 863 | } |
6838b03f | 864 | p_dev->_win |= CLIENT_WIN_REQ(w); |
1a8d4663 DB |
865 | |
866 | /* Configure the socket controller */ | |
82f88e36 | 867 | win->map = w+1; |
cdb13808 DB |
868 | win->flags = res->flags & WIN_FLAGS_MAP; |
869 | win->speed = speed; | |
82f88e36 | 870 | win->card_start = 0; |
6b8e087b | 871 | |
82f88e36 | 872 | if (s->ops->set_mem_map(s, win) != 0) { |
eb838fe1 | 873 | dev_dbg(&p_dev->dev, "failed to set memory mapping\n"); |
6b8e087b | 874 | mutex_unlock(&s->ops_mutex); |
926c5402 DB |
875 | return -EIO; |
876 | } | |
1a8d4663 DB |
877 | s->state |= SOCKET_WIN_REQ(w); |
878 | ||
879 | /* Return window handle */ | |
9fea84f4 | 880 | if (s->features & SS_CAP_STATIC_MAP) |
cdb13808 | 881 | res->start = win->static_start; |
9fea84f4 | 882 | else |
cdb13808 | 883 | res->start = win->res->start; |
9fea84f4 | 884 | |
0ca724d3 | 885 | /* convert to new-style resources */ |
cdb13808 DB |
886 | res->end += res->start - 1; |
887 | res->flags &= ~WIN_FLAGS_REQ; | |
888 | res->flags |= (win->map << 2) | IORESOURCE_MEM; | |
ad0c7be2 DB |
889 | res->parent = win->res; |
890 | if (win->res) | |
891 | request_resource(&iomem_resource, res); | |
892 | ||
eb838fe1 | 893 | dev_dbg(&p_dev->dev, "request_window results in %pR\n", res); |
0ca724d3 | 894 | |
6b8e087b | 895 | mutex_unlock(&s->ops_mutex); |
1a8d4663 | 896 | |
4c89e88b | 897 | return 0; |
1a8d4663 DB |
898 | } /* pcmcia_request_window */ |
899 | EXPORT_SYMBOL(pcmcia_request_window); | |
5f2a71fc | 900 | |
9fea84f4 DB |
901 | void pcmcia_disable_device(struct pcmcia_device *p_dev) |
902 | { | |
0ca724d3 DB |
903 | int i; |
904 | for (i = 0; i < MAX_WIN; i++) { | |
905 | struct resource *res = p_dev->resource[MAX_IO_WIN + i]; | |
906 | if (res->flags & WIN_FLAGS_REQ) | |
907 | pcmcia_release_window(p_dev, res); | |
908 | } | |
909 | ||
5f2a71fc | 910 | pcmcia_release_configuration(p_dev); |
2ce4905e | 911 | pcmcia_release_io(p_dev); |
418c5278 | 912 | if (p_dev->_irq) { |
eb14120f | 913 | free_irq(p_dev->irq, p_dev->priv); |
418c5278 PM |
914 | p_dev->_irq = 0; |
915 | } | |
5f2a71fc DB |
916 | } |
917 | EXPORT_SYMBOL(pcmcia_disable_device); |