blob: c1c197292111452de7dbf0e69926ea248a85e3fb [file] [log] [blame]
Thomas Gleixnerd2912cb2019-06-04 10:11:33 +02001// SPDX-License-Identifier: GPL-2.0-only
Dominik Brodowski1a8d4662005-06-27 16:28:53 -07002/*
3 * PCMCIA 16-bit resource management functions
4 *
5 * The initial developer of the original code is David A. Hinds
6 * <dahinds@users.sourceforge.net>. Portions created by David A. Hinds
7 * are Copyright (C) 1999 David A. Hinds. All Rights Reserved.
8 *
9 * Copyright (C) 1999 David A. Hinds
Dominik Brodowski9485ee12010-07-31 07:09:32 +020010 * Copyright (C) 2004-2010 Dominik Brodowski
Dominik Brodowski1a8d4662005-06-27 16:28:53 -070011 */
12
Dominik Brodowski1a8d4662005-06-27 16:28:53 -070013#include <linux/module.h>
14#include <linux/kernel.h>
15#include <linux/interrupt.h>
16#include <linux/delay.h>
17#include <linux/pci.h>
18#include <linux/device.h>
Dominik Brodowski91284222009-10-18 23:32:33 +020019#include <linux/netdevice.h>
Tejun Heo5a0e3ad2010-03-24 17:04:11 +090020#include <linux/slab.h>
Dominik Brodowski1a8d4662005-06-27 16:28:53 -070021
Dominik Brodowski6f0f38c2010-04-08 20:33:16 +020022#include <asm/irq.h>
23
Dominik Brodowski1a8d4662005-06-27 16:28:53 -070024#include <pcmcia/ss.h>
Dominik Brodowski1a8d4662005-06-27 16:28:53 -070025#include <pcmcia/cistpl.h>
26#include <pcmcia/cisreg.h>
27#include <pcmcia/ds.h>
28
29#include "cs_internal.h"
Dominik Brodowski1a8d4662005-06-27 16:28:53 -070030
31
Dominik Brodowski1a8d4662005-06-27 16:28:53 -070032/* Access speed for IO windows */
Dominik Brodowski9fea84f2009-12-07 22:11:45 +010033static int io_speed;
Dominik Brodowski1a8d4662005-06-27 16:28:53 -070034module_param(io_speed, int, 0444);
35
36
Dominik Brodowskia3ac9af2010-01-06 14:03:03 +010037int pcmcia_validate_mem(struct pcmcia_socket *s)
38{
39 if (s->resource_ops->validate_mem)
40 return s->resource_ops->validate_mem(s);
41 /* if there is no callback, we can assume that everything is OK */
42 return 0;
43}
44
45struct resource *pcmcia_find_mem_region(u_long base, u_long num, u_long align,
46 int low, struct pcmcia_socket *s)
47{
48 if (s->resource_ops->find_mem)
49 return s->resource_ops->find_mem(base, num, align, low, s);
50 return NULL;
51}
52
Dominik Brodowski1a8d4662005-06-27 16:28:53 -070053
Dominik Brodowski9485ee12010-07-31 07:09:32 +020054/**
55 * release_io_space() - release IO ports allocated with alloc_io_space()
56 * @s: pcmcia socket
57 * @res: resource to release
58 *
59 */
Dominik Brodowskiad0c7be2010-07-25 13:10:22 +020060static void release_io_space(struct pcmcia_socket *s, struct resource *res)
61{
62 resource_size_t num = resource_size(res);
63 int i;
64
65 dev_dbg(&s->dev, "release_io_space for %pR\n", res);
66
67 for (i = 0; i < MAX_IO_WIN; i++) {
68 if (!s->io[i].res)
69 continue;
70 if ((s->io[i].res->start <= res->start) &&
71 (s->io[i].res->end >= res->end)) {
72 s->io[i].InUse -= num;
73 if (res->parent)
74 release_resource(res);
75 res->start = res->end = 0;
76 res->flags = IORESOURCE_IO;
77 /* Free the window if no one else is using it */
78 if (s->io[i].InUse == 0) {
79 release_resource(s->io[i].res);
80 kfree(s->io[i].res);
81 s->io[i].res = NULL;
82 }
83 }
84 }
Dominik Brodowski9485ee12010-07-31 07:09:32 +020085}
Dominik Brodowskiad0c7be2010-07-25 13:10:22 +020086
Dominik Brodowski9485ee12010-07-31 07:09:32 +020087
88/**
89 * alloc_io_space() - allocate IO ports for use by a PCMCIA device
90 * @s: pcmcia socket
91 * @res: resource to allocate (begin: begin, end: size)
92 * @lines: number of IO lines decoded by the PCMCIA card
Dominik Brodowski1a8d4662005-06-27 16:28:53 -070093 *
94 * Special stuff for managing IO windows, because they are scarce
95 */
Dominik Brodowski2ce49052010-07-24 13:14:44 +020096static int alloc_io_space(struct pcmcia_socket *s, struct resource *res,
97 unsigned int lines)
Dominik Brodowski1a8d4662005-06-27 16:28:53 -070098{
Dominik Brodowskib19a7272010-03-20 13:10:47 +010099 unsigned int align;
Dominik Brodowski2ce49052010-07-24 13:14:44 +0200100 unsigned int base = res->start;
101 unsigned int num = res->end;
102 int ret;
Dominik Brodowski1a8d4662005-06-27 16:28:53 -0700103
Dominik Brodowski2ce49052010-07-24 13:14:44 +0200104 res->flags |= IORESOURCE_IO;
105
Dominik Brodowski90abdc32010-07-24 17:23:51 +0200106 dev_dbg(&s->dev, "alloc_io_space request for %pR, %d lines\n",
107 res, lines);
Dominik Brodowski2ce49052010-07-24 13:14:44 +0200108
109 align = base ? (lines ? 1<<lines : 0) : 1;
Dominik Brodowski1a8d4662005-06-27 16:28:53 -0700110 if (align && (align < num)) {
Dominik Brodowski2ce49052010-07-24 13:14:44 +0200111 if (base) {
112 dev_dbg(&s->dev, "odd IO request\n");
Dominik Brodowski1a8d4662005-06-27 16:28:53 -0700113 align = 0;
114 } else
Dominik Brodowski9fea84f2009-12-07 22:11:45 +0100115 while (align && (align < num))
116 align <<= 1;
Dominik Brodowski1a8d4662005-06-27 16:28:53 -0700117 }
Dominik Brodowski2ce49052010-07-24 13:14:44 +0200118 if (base & ~(align-1)) {
119 dev_dbg(&s->dev, "odd IO request\n");
Dominik Brodowski1a8d4662005-06-27 16:28:53 -0700120 align = 0;
121 }
Dominik Brodowskib19a7272010-03-20 13:10:47 +0100122
Dominik Brodowskiad0c7be2010-07-25 13:10:22 +0200123 ret = s->resource_ops->find_io(s, res->flags, &base, num, align,
124 &res->parent);
Dominik Brodowski2ce49052010-07-24 13:14:44 +0200125 if (ret) {
Dominik Brodowskiad0c7be2010-07-25 13:10:22 +0200126 dev_dbg(&s->dev, "alloc_io_space request failed (%d)\n", ret);
Dominik Brodowski2ce49052010-07-24 13:14:44 +0200127 return -EINVAL;
128 }
129
130 res->start = base;
131 res->end = res->start + num - 1;
Dominik Brodowski1a8d4662005-06-27 16:28:53 -0700132
Dominik Brodowskiad0c7be2010-07-25 13:10:22 +0200133 if (res->parent) {
134 ret = request_resource(res->parent, res);
135 if (ret) {
136 dev_warn(&s->dev,
137 "request_resource %pR failed: %d\n", res, ret);
138 res->parent = NULL;
139 release_io_space(s, res);
Dominik Brodowski1a8d4662005-06-27 16:28:53 -0700140 }
141 }
Dominik Brodowskiad0c7be2010-07-25 13:10:22 +0200142 dev_dbg(&s->dev, "alloc_io_space request result %d: %pR\n", ret, res);
143 return ret;
Dominik Brodowski9485ee12010-07-31 07:09:32 +0200144}
Dominik Brodowski1a8d4662005-06-27 16:28:53 -0700145
146
Lee Jones1d26d6f2021-03-12 11:02:38 +0000147/*
Dominik Brodowski1d5cc192010-07-24 12:23:21 +0200148 * pcmcia_access_config() - read or write card configuration registers
Dominik Brodowski1a8d4662005-06-27 16:28:53 -0700149 *
Dominik Brodowski1d5cc192010-07-24 12:23:21 +0200150 * pcmcia_access_config() reads and writes configuration registers in
151 * attribute memory. Memory window 0 is reserved for this and the tuple
152 * reading services. Drivers must use pcmcia_read_config_byte() or
153 * pcmcia_write_config_byte().
Dominik Brodowski1a8d4662005-06-27 16:28:53 -0700154 */
Dominik Brodowski1d5cc192010-07-24 12:23:21 +0200155static int pcmcia_access_config(struct pcmcia_device *p_dev,
156 off_t where, u8 *val,
157 int (*accessf) (struct pcmcia_socket *s,
158 int attr, unsigned int addr,
159 unsigned int len, void *ptr))
Dominik Brodowski1a8d4662005-06-27 16:28:53 -0700160{
Dominik Brodowski855cdf12006-01-10 20:48:59 +0100161 struct pcmcia_socket *s;
Dominik Brodowski1a8d4662005-06-27 16:28:53 -0700162 config_t *c;
163 int addr;
Dominik Brodowski059f6672010-03-30 18:07:50 +0200164 int ret = 0;
Dominik Brodowski1a8d4662005-06-27 16:28:53 -0700165
Dominik Brodowski855cdf12006-01-10 20:48:59 +0100166 s = p_dev->socket;
Dominik Brodowski94a819f2010-01-17 18:31:34 +0100167
168 mutex_lock(&s->ops_mutex);
Dominik Brodowski855cdf12006-01-10 20:48:59 +0100169 c = p_dev->function_config;
Dominik Brodowski1a8d4662005-06-27 16:28:53 -0700170
Dominik Brodowski6d9a2992009-10-24 12:20:18 +0200171 if (!(c->state & CONFIG_LOCKED)) {
Lucas De Marchie9c54992011-04-26 23:28:26 -0700172 dev_dbg(&p_dev->dev, "Configuration isn't locked\n");
Dominik Brodowski94a819f2010-01-17 18:31:34 +0100173 mutex_unlock(&s->ops_mutex);
Dominik Brodowski943f70f2008-08-03 11:23:42 +0200174 return -EACCES;
Dominik Brodowski6d9a2992009-10-24 12:20:18 +0200175 }
Dominik Brodowski1a8d4662005-06-27 16:28:53 -0700176
Dominik Brodowski7feabb62010-07-29 18:35:47 +0200177 addr = (p_dev->config_base + where) >> 1;
Dominik Brodowski1a8d4662005-06-27 16:28:53 -0700178
Dominik Brodowski1d5cc192010-07-24 12:23:21 +0200179 ret = accessf(s, 1, addr, 1, val);
180
Dominik Brodowski059f6672010-03-30 18:07:50 +0200181 mutex_unlock(&s->ops_mutex);
Dominik Brodowski1d5cc192010-07-24 12:23:21 +0200182
Dominik Brodowski059f6672010-03-30 18:07:50 +0200183 return ret;
Dominik Brodowski9485ee12010-07-31 07:09:32 +0200184}
Dominik Brodowski1d5cc192010-07-24 12:23:21 +0200185
186
Lee Jones1d26d6f2021-03-12 11:02:38 +0000187/*
Dominik Brodowski1d5cc192010-07-24 12:23:21 +0200188 * pcmcia_read_config_byte() - read a byte from a card configuration register
189 *
190 * pcmcia_read_config_byte() reads a byte from a configuration register in
191 * attribute memory.
192 */
193int pcmcia_read_config_byte(struct pcmcia_device *p_dev, off_t where, u8 *val)
194{
195 return pcmcia_access_config(p_dev, where, val, pcmcia_read_cis_mem);
196}
197EXPORT_SYMBOL(pcmcia_read_config_byte);
198
199
Lee Jones1d26d6f2021-03-12 11:02:38 +0000200/*
Dominik Brodowski1d5cc192010-07-24 12:23:21 +0200201 * pcmcia_write_config_byte() - write a byte to a card configuration register
202 *
203 * pcmcia_write_config_byte() writes a byte to a configuration register in
204 * attribute memory.
205 */
206int pcmcia_write_config_byte(struct pcmcia_device *p_dev, off_t where, u8 val)
207{
208 return pcmcia_access_config(p_dev, where, &val, pcmcia_write_cis_mem);
209}
210EXPORT_SYMBOL(pcmcia_write_config_byte);
Dominik Brodowski34481392005-06-27 16:28:54 -0700211
Dominik Brodowski1a8d4662005-06-27 16:28:53 -0700212
Dominik Brodowski9485ee12010-07-31 07:09:32 +0200213/**
214 * pcmcia_map_mem_page() - modify iomem window to point to a different offset
215 * @p_dev: pcmcia device
216 * @res: iomem resource already enabled by pcmcia_request_window()
217 * @offset: card_offset to map
218 *
219 * pcmcia_map_mem_page() modifies what can be read and written by accessing
220 * an iomem range previously enabled by pcmcia_request_window(), by setting
221 * the card_offset value to @offset.
222 */
Dominik Brodowskicdb13802010-07-28 10:59:06 +0200223int pcmcia_map_mem_page(struct pcmcia_device *p_dev, struct resource *res,
Dominik Brodowskib5cb2592010-07-24 18:46:42 +0200224 unsigned int offset)
Dominik Brodowski1a8d4662005-06-27 16:28:53 -0700225{
Magnus Damm0bdf9b32006-12-13 19:46:53 +0900226 struct pcmcia_socket *s = p_dev->socket;
Dominik Brodowski0ca724d32010-07-24 19:03:02 +0200227 unsigned int w;
Dominik Brodowski6b8e0872010-01-12 21:42:51 +0100228 int ret;
Magnus Damm868575d2006-12-13 19:46:43 +0900229
Dominik Brodowski0ca724d32010-07-24 19:03:02 +0200230 w = ((res->flags & IORESOURCE_BITS & WIN_FLAGS_REQ) >> 2) - 1;
231 if (w >= MAX_WIN)
Dominik Brodowskiffb8da22008-08-03 11:18:00 +0200232 return -EINVAL;
Dominik Brodowskib5cb2592010-07-24 18:46:42 +0200233
Dominik Brodowski6b8e0872010-01-12 21:42:51 +0100234 mutex_lock(&s->ops_mutex);
Dominik Brodowski0ca724d32010-07-24 19:03:02 +0200235 s->win[w].card_start = offset;
236 ret = s->ops->set_mem_map(s, &s->win[w]);
Dominik Brodowski6b8e0872010-01-12 21:42:51 +0100237 if (ret)
Dominik Brodowskieb838fe2010-09-13 16:51:36 +0200238 dev_warn(&p_dev->dev, "failed to set_mem_map\n");
Dominik Brodowski6b8e0872010-01-12 21:42:51 +0100239 mutex_unlock(&s->ops_mutex);
240 return ret;
Dominik Brodowski9485ee12010-07-31 07:09:32 +0200241}
Dominik Brodowski1a8d4662005-06-27 16:28:53 -0700242EXPORT_SYMBOL(pcmcia_map_mem_page);
243
244
Dominik Brodowskifb49fa52010-07-29 14:06:42 +0200245/**
246 * pcmcia_fixup_iowidth() - reduce io width to 8bit
Dominik Brodowski9485ee12010-07-31 07:09:32 +0200247 * @p_dev: pcmcia device
Dominik Brodowski1a8d4662005-06-27 16:28:53 -0700248 *
Dominik Brodowskifb49fa52010-07-29 14:06:42 +0200249 * pcmcia_fixup_iowidth() allows a PCMCIA device driver to reduce the
Dominik Brodowski1ac71e52010-07-29 19:27:09 +0200250 * IO width to 8bit after having called pcmcia_enable_device()
Dominik Brodowskifb49fa52010-07-29 14:06:42 +0200251 * previously.
Dominik Brodowski1a8d4662005-06-27 16:28:53 -0700252 */
Dominik Brodowskifb49fa52010-07-29 14:06:42 +0200253int pcmcia_fixup_iowidth(struct pcmcia_device *p_dev)
Dominik Brodowski1a8d4662005-06-27 16:28:53 -0700254{
Dominik Brodowskifb49fa52010-07-29 14:06:42 +0200255 struct pcmcia_socket *s = p_dev->socket;
256 pccard_io_map io_off = { 0, 0, 0, 0, 1 };
257 pccard_io_map io_on;
258 int i, ret = 0;
Dominik Brodowski94a819f2010-01-17 18:31:34 +0100259
260 mutex_lock(&s->ops_mutex);
Dominik Brodowskidbb22f02006-01-10 20:41:27 +0100261
Dominik Brodowskifb49fa52010-07-29 14:06:42 +0200262 dev_dbg(&p_dev->dev, "fixup iowidth to 8bit\n");
263
264 if (!(s->state & SOCKET_PRESENT) ||
265 !(p_dev->function_config->state & CONFIG_LOCKED)) {
266 dev_dbg(&p_dev->dev, "No card? Config not locked?\n");
Jiri Slaby4e06e242010-03-16 15:57:44 +0100267 ret = -EACCES;
268 goto unlock;
Dominik Brodowski6d9a2992009-10-24 12:20:18 +0200269 }
Dominik Brodowski1a8d4662005-06-27 16:28:53 -0700270
Dominik Brodowskifb49fa52010-07-29 14:06:42 +0200271 io_on.speed = io_speed;
272 for (i = 0; i < MAX_IO_WIN; i++) {
273 if (!s->io[i].res)
274 continue;
275 io_off.map = i;
276 io_on.map = i;
277
278 io_on.flags = MAP_ACTIVE | IO_DATA_PATH_WIDTH_8;
279 io_on.start = s->io[i].res->start;
280 io_on.stop = s->io[i].res->end;
281
282 s->ops->set_io_map(s, &io_off);
Jia-Ju Bai1b22dcf2018-07-30 21:42:56 +0800283 msleep(40);
Dominik Brodowskifb49fa52010-07-29 14:06:42 +0200284 s->ops->set_io_map(s, &io_on);
Dominik Brodowskid8b0a492008-08-03 10:56:13 +0200285 }
Jiri Slaby4e06e242010-03-16 15:57:44 +0100286unlock:
Dominik Brodowski94a819f2010-01-17 18:31:34 +0100287 mutex_unlock(&s->ops_mutex);
Dominik Brodowski4bbed522006-01-15 11:18:12 +0100288
Jiri Slaby4e06e242010-03-16 15:57:44 +0100289 return ret;
Dominik Brodowskifb49fa52010-07-29 14:06:42 +0200290}
291EXPORT_SYMBOL(pcmcia_fixup_iowidth);
292
293
294/**
295 * pcmcia_fixup_vpp() - set Vpp to a new voltage level
Dominik Brodowski9485ee12010-07-31 07:09:32 +0200296 * @p_dev: pcmcia device
297 * @new_vpp: new Vpp voltage
Dominik Brodowskifb49fa52010-07-29 14:06:42 +0200298 *
299 * pcmcia_fixup_vpp() allows a PCMCIA device driver to set Vpp to
Dominik Brodowski1ac71e52010-07-29 19:27:09 +0200300 * a new voltage level between calls to pcmcia_enable_device()
Dominik Brodowskifb49fa52010-07-29 14:06:42 +0200301 * and pcmcia_disable_device().
302 */
303int pcmcia_fixup_vpp(struct pcmcia_device *p_dev, unsigned char new_vpp)
304{
305 struct pcmcia_socket *s = p_dev->socket;
306 int ret = 0;
307
308 mutex_lock(&s->ops_mutex);
309
310 dev_dbg(&p_dev->dev, "fixup Vpp to %d\n", new_vpp);
311
312 if (!(s->state & SOCKET_PRESENT) ||
313 !(p_dev->function_config->state & CONFIG_LOCKED)) {
314 dev_dbg(&p_dev->dev, "No card? Config not locked?\n");
315 ret = -EACCES;
316 goto unlock;
317 }
318
319 s->socket.Vpp = new_vpp;
320 if (s->ops->set_socket(s, &s->socket)) {
321 dev_warn(&p_dev->dev, "Unable to set VPP\n");
322 ret = -EIO;
323 goto unlock;
324 }
Dominik Brodowskie8405f02010-07-29 15:50:55 +0200325 p_dev->vpp = new_vpp;
Dominik Brodowskifb49fa52010-07-29 14:06:42 +0200326
327unlock:
328 mutex_unlock(&s->ops_mutex);
329
330 return ret;
331}
332EXPORT_SYMBOL(pcmcia_fixup_vpp);
Dominik Brodowski1a8d4662005-06-27 16:28:53 -0700333
334
Dominik Brodowski9485ee12010-07-31 07:09:32 +0200335/**
336 * pcmcia_release_configuration() - physically disable a PCMCIA device
337 * @p_dev: pcmcia device
338 *
339 * pcmcia_release_configuration() is the 1:1 counterpart to
340 * pcmcia_enable_device(): If a PCMCIA device is no longer used by any
341 * driver, the Vpp voltage is set to 0, IRQs will no longer be generated,
342 * and I/O ranges will be disabled. As pcmcia_release_io() and
343 * pcmcia_release_window() still need to be called, device drivers are
344 * expected to call pcmcia_disable_device() instead.
345 */
Dominik Brodowski2bc5a9b2005-07-07 17:59:02 -0700346int pcmcia_release_configuration(struct pcmcia_device *p_dev)
Dominik Brodowski1a8d4662005-06-27 16:28:53 -0700347{
348 pccard_io_map io = { 0, 0, 0, 0, 1 };
Dominik Brodowski2bc5a9b2005-07-07 17:59:02 -0700349 struct pcmcia_socket *s = p_dev->socket;
Dominik Brodowski94a819f2010-01-17 18:31:34 +0100350 config_t *c;
Dominik Brodowski1a8d4662005-06-27 16:28:53 -0700351 int i;
352
Dominik Brodowski9e86749c2010-01-16 00:26:33 +0100353 mutex_lock(&s->ops_mutex);
Dominik Brodowski94a819f2010-01-17 18:31:34 +0100354 c = p_dev->function_config;
Dominik Brodowskie2d40962006-03-02 00:09:29 +0100355 if (p_dev->_locked) {
356 p_dev->_locked = 0;
Dominik Brodowski1a8d4662005-06-27 16:28:53 -0700357 if (--(s->lock_count) == 0) {
Dominik Brodowski9485ee12010-07-31 07:09:32 +0200358 s->socket.flags = SS_OUTPUT_ENA; /* Is this correct? */
Dominik Brodowski1a8d4662005-06-27 16:28:53 -0700359 s->socket.Vpp = 0;
360 s->socket.io_irq = 0;
361 s->ops->set_socket(s, &s->socket);
362 }
Dominik Brodowski5f2a71f2006-01-15 09:32:39 +0100363 }
364 if (c->state & CONFIG_LOCKED) {
365 c->state &= ~CONFIG_LOCKED;
Dominik Brodowski1a8d4662005-06-27 16:28:53 -0700366 if (c->state & CONFIG_IO_REQ)
367 for (i = 0; i < MAX_IO_WIN; i++) {
Dominik Brodowskic7d00692006-01-15 08:04:43 +0100368 if (!s->io[i].res)
Dominik Brodowski1a8d4662005-06-27 16:28:53 -0700369 continue;
370 s->io[i].Config--;
371 if (s->io[i].Config != 0)
372 continue;
373 io.map = i;
374 s->ops->set_io_map(s, &io);
375 }
Dominik Brodowski1a8d4662005-06-27 16:28:53 -0700376 }
Dominik Brodowski9e86749c2010-01-16 00:26:33 +0100377 mutex_unlock(&s->ops_mutex);
Dominik Brodowski1a8d4662005-06-27 16:28:53 -0700378
Dominik Brodowski4c89e882008-08-03 10:07:45 +0200379 return 0;
Dominik Brodowski9485ee12010-07-31 07:09:32 +0200380}
Dominik Brodowski1a8d4662005-06-27 16:28:53 -0700381
382
Dominik Brodowski9485ee12010-07-31 07:09:32 +0200383/**
384 * pcmcia_release_io() - release I/O allocated by a PCMCIA device
385 * @p_dev: pcmcia device
Dominik Brodowski1a8d4662005-06-27 16:28:53 -0700386 *
Dominik Brodowski9485ee12010-07-31 07:09:32 +0200387 * pcmcia_release_io() releases the I/O ranges allocated by a PCMCIA
388 * device. This may be invoked some time after a card ejection has
389 * already dumped the actual socket configuration, so if the client is
390 * "stale", we don't bother checking the port ranges against the
391 * current socket values.
Dominik Brodowski1a8d4662005-06-27 16:28:53 -0700392 */
Dominik Brodowski2ce49052010-07-24 13:14:44 +0200393static int pcmcia_release_io(struct pcmcia_device *p_dev)
Dominik Brodowski1a8d4662005-06-27 16:28:53 -0700394{
Dominik Brodowski2bc5a9b2005-07-07 17:59:02 -0700395 struct pcmcia_socket *s = p_dev->socket;
Dominik Brodowski94a819f2010-01-17 18:31:34 +0100396 int ret = -EINVAL;
397 config_t *c;
398
399 mutex_lock(&s->ops_mutex);
Dominik Brodowski9fea84f2009-12-07 22:11:45 +0100400 if (!p_dev->_io)
Dominik Brodowski94a819f2010-01-17 18:31:34 +0100401 goto out;
Dominik Brodowski5f2a71f2006-01-15 09:32:39 +0100402
Dominik Brodowski2ce49052010-07-24 13:14:44 +0200403 c = p_dev->function_config;
404
405 release_io_space(s, &c->io[0]);
406
407 if (c->io[1].end)
408 release_io_space(s, &c->io[1]);
409
Dominik Brodowskie2d40962006-03-02 00:09:29 +0100410 p_dev->_io = 0;
Dominik Brodowski5f2a71f2006-01-15 09:32:39 +0100411 c->state &= ~CONFIG_IO_REQ;
Dominik Brodowski1a8d4662005-06-27 16:28:53 -0700412
Dominik Brodowski94a819f2010-01-17 18:31:34 +0100413out:
414 mutex_unlock(&s->ops_mutex);
415
416 return ret;
Dominik Brodowski1a8d4662005-06-27 16:28:53 -0700417} /* pcmcia_release_io */
Dominik Brodowski1a8d4662005-06-27 16:28:53 -0700418
Dominik Brodowski9485ee12010-07-31 07:09:32 +0200419
Dominik Brodowskicdb13802010-07-28 10:59:06 +0200420/**
421 * pcmcia_release_window() - release reserved iomem for PCMCIA devices
Dominik Brodowski9485ee12010-07-31 07:09:32 +0200422 * @p_dev: pcmcia device
423 * @res: iomem resource to release
Dominik Brodowskicdb13802010-07-28 10:59:06 +0200424 *
Dominik Brodowski9485ee12010-07-31 07:09:32 +0200425 * pcmcia_release_window() releases &struct resource *res which was
Dominik Brodowskicdb13802010-07-28 10:59:06 +0200426 * previously reserved by calling pcmcia_request_window().
427 */
Dominik Brodowski0ca724d32010-07-24 19:03:02 +0200428int pcmcia_release_window(struct pcmcia_device *p_dev, struct resource *res)
Dominik Brodowski1a8d4662005-06-27 16:28:53 -0700429{
Magnus Damm0bdf9b32006-12-13 19:46:53 +0900430 struct pcmcia_socket *s = p_dev->socket;
Dominik Brodowski82f88e32009-11-03 01:16:12 +0100431 pccard_mem_map *win;
Dominik Brodowski0ca724d32010-07-24 19:03:02 +0200432 unsigned int w;
Dominik Brodowski1a8d4662005-06-27 16:28:53 -0700433
Dominik Brodowski0ca724d32010-07-24 19:03:02 +0200434 dev_dbg(&p_dev->dev, "releasing window %pR\n", res);
435
436 w = ((res->flags & IORESOURCE_BITS & WIN_FLAGS_REQ) >> 2) - 1;
437 if (w >= MAX_WIN)
Dominik Brodowskiffb8da22008-08-03 11:18:00 +0200438 return -EINVAL;
Magnus Damm0bdf9b32006-12-13 19:46:53 +0900439
Dominik Brodowski6b8e0872010-01-12 21:42:51 +0100440 mutex_lock(&s->ops_mutex);
Dominik Brodowski0ca724d32010-07-24 19:03:02 +0200441 win = &s->win[w];
Magnus Damm0bdf9b32006-12-13 19:46:53 +0900442
Dominik Brodowski0ca724d32010-07-24 19:03:02 +0200443 if (!(p_dev->_win & CLIENT_WIN_REQ(w))) {
Dominik Brodowskieb838fe2010-09-13 16:51:36 +0200444 dev_dbg(&p_dev->dev, "not releasing unknown window\n");
Dominik Brodowski6b8e0872010-01-12 21:42:51 +0100445 mutex_unlock(&s->ops_mutex);
Dominik Brodowskiffb8da22008-08-03 11:18:00 +0200446 return -EINVAL;
Dominik Brodowski6d9a2992009-10-24 12:20:18 +0200447 }
Dominik Brodowski1a8d4662005-06-27 16:28:53 -0700448
449 /* Shut down memory window */
Dominik Brodowski82f88e32009-11-03 01:16:12 +0100450 win->flags &= ~MAP_ACTIVE;
451 s->ops->set_mem_map(s, win);
Dominik Brodowski0ca724d32010-07-24 19:03:02 +0200452 s->state &= ~SOCKET_WIN_REQ(w);
Dominik Brodowski1a8d4662005-06-27 16:28:53 -0700453
454 /* Release system memory */
Dominik Brodowski82f88e32009-11-03 01:16:12 +0100455 if (win->res) {
Dominik Brodowskiad0c7be2010-07-25 13:10:22 +0200456 release_resource(res);
Dominik Brodowski82f88e32009-11-03 01:16:12 +0100457 release_resource(win->res);
458 kfree(win->res);
459 win->res = NULL;
Dominik Brodowski1a8d4662005-06-27 16:28:53 -0700460 }
Dominik Brodowskicdb13802010-07-28 10:59:06 +0200461 res->start = res->end = 0;
462 res->flags = IORESOURCE_MEM;
Dominik Brodowski0ca724d32010-07-24 19:03:02 +0200463 p_dev->_win &= ~CLIENT_WIN_REQ(w);
Dominik Brodowski6b8e0872010-01-12 21:42:51 +0100464 mutex_unlock(&s->ops_mutex);
Dominik Brodowski1a8d4662005-06-27 16:28:53 -0700465
Dominik Brodowski4c89e882008-08-03 10:07:45 +0200466 return 0;
Dominik Brodowski1a8d4662005-06-27 16:28:53 -0700467} /* pcmcia_release_window */
468EXPORT_SYMBOL(pcmcia_release_window);
469
Dominik Brodowski9485ee12010-07-31 07:09:32 +0200470
Dominik Brodowski1ac71e52010-07-29 19:27:09 +0200471/**
472 * pcmcia_enable_device() - set up and activate a PCMCIA device
Dominik Brodowski9485ee12010-07-31 07:09:32 +0200473 * @p_dev: the associated PCMCIA device
Dominik Brodowski1ac71e52010-07-29 19:27:09 +0200474 *
Dominik Brodowski9485ee12010-07-31 07:09:32 +0200475 * pcmcia_enable_device() physically enables a PCMCIA device. It parses
476 * the flags passed to in @flags and stored in @p_dev->flags and sets up
477 * the Vpp voltage, enables the speaker line, I/O ports and store proper
478 * values to configuration registers.
Dominik Brodowski1ac71e52010-07-29 19:27:09 +0200479 */
480int pcmcia_enable_device(struct pcmcia_device *p_dev)
Dominik Brodowski1a8d4662005-06-27 16:28:53 -0700481{
482 int i;
Dominik Brodowski1ac71e52010-07-29 19:27:09 +0200483 unsigned int base;
Dominik Brodowski2bc5a9b2005-07-07 17:59:02 -0700484 struct pcmcia_socket *s = p_dev->socket;
Dominik Brodowski1a8d4662005-06-27 16:28:53 -0700485 config_t *c;
486 pccard_io_map iomap;
Dominik Brodowskifc301102010-07-29 16:19:39 +0200487 unsigned char status = 0;
488 unsigned char ext_status = 0;
Dominik Brodowski7feabb62010-07-29 18:35:47 +0200489 unsigned char option = 0;
Dominik Brodowski1ac71e52010-07-29 19:27:09 +0200490 unsigned int flags = p_dev->config_flags;
Dominik Brodowski1a8d4662005-06-27 16:28:53 -0700491
Dominik Brodowski1a8d4662005-06-27 16:28:53 -0700492 if (!(s->state & SOCKET_PRESENT))
Joe Perchesd598de02009-06-28 09:26:41 -0700493 return -ENODEV;
Dominik Brodowski1a8d4662005-06-27 16:28:53 -0700494
Dominik Brodowski94a819f2010-01-17 18:31:34 +0100495 mutex_lock(&s->ops_mutex);
Dominik Brodowskidbb22f02006-01-10 20:41:27 +0100496 c = p_dev->function_config;
Dominik Brodowski6d9a2992009-10-24 12:20:18 +0200497 if (c->state & CONFIG_LOCKED) {
Dominik Brodowski94a819f2010-01-17 18:31:34 +0100498 mutex_unlock(&s->ops_mutex);
Dominik Brodowskieb838fe2010-09-13 16:51:36 +0200499 dev_dbg(&p_dev->dev, "Configuration is locked\n");
Dominik Brodowski943f70f2008-08-03 11:23:42 +0200500 return -EACCES;
Dominik Brodowski6d9a2992009-10-24 12:20:18 +0200501 }
Dominik Brodowski1a8d4662005-06-27 16:28:53 -0700502
503 /* Do power control. We don't allow changes in Vcc. */
Dominik Brodowskie8405f02010-07-29 15:50:55 +0200504 s->socket.Vpp = p_dev->vpp;
Dominik Brodowskid8b0a492008-08-03 10:56:13 +0200505 if (s->ops->set_socket(s, &s->socket)) {
Dominik Brodowski9e86749c2010-01-16 00:26:33 +0100506 mutex_unlock(&s->ops_mutex);
Joe Perchesf2e6cf72014-10-10 09:12:47 -0700507 dev_warn(&p_dev->dev, "Unable to set socket state\n");
Dominik Brodowskid8b0a492008-08-03 10:56:13 +0200508 return -EINVAL;
509 }
Dominik Brodowski1a8d4662005-06-27 16:28:53 -0700510
Dominik Brodowski1a8d4662005-06-27 16:28:53 -0700511 /* Pick memory or I/O card, DMA mode, interrupt */
Dominik Brodowskiff10fca2010-10-22 08:46:36 +0200512 if (p_dev->_io || flags & CONF_ENABLE_IRQ)
513 flags |= CONF_ENABLE_IOCARD;
514 if (flags & CONF_ENABLE_IOCARD)
Dominik Brodowski1a8d4662005-06-27 16:28:53 -0700515 s->socket.flags |= SS_IOCARD;
Dominik Brodowski33619f02011-02-19 12:35:15 +0100516 if (flags & CONF_ENABLE_ZVCARD)
517 s->socket.flags |= SS_ZVCARD | SS_IOCARD;
Dominik Brodowski1ac71e52010-07-29 19:27:09 +0200518 if (flags & CONF_ENABLE_SPKR) {
Dominik Brodowski1a8d4662005-06-27 16:28:53 -0700519 s->socket.flags |= SS_SPKR_ENA;
Dominik Brodowskifc301102010-07-29 16:19:39 +0200520 status = CCSR_AUDIO_ENA;
Dominik Brodowski7feabb62010-07-29 18:35:47 +0200521 if (!(p_dev->config_regs & PRESENT_STATUS))
Dominik Brodowskifc301102010-07-29 16:19:39 +0200522 dev_warn(&p_dev->dev, "speaker requested, but "
523 "PRESENT_STATUS not set!\n");
524 }
Dominik Brodowski1ac71e52010-07-29 19:27:09 +0200525 if (flags & CONF_ENABLE_IRQ)
Dominik Brodowski6f840af2010-03-07 10:51:23 +0100526 s->socket.io_irq = s->pcmcia_irq;
Dominik Brodowski1a8d4662005-06-27 16:28:53 -0700527 else
528 s->socket.io_irq = 0;
Dominik Brodowski1ac71e52010-07-29 19:27:09 +0200529 if (flags & CONF_ENABLE_ESR) {
Dominik Brodowski7feabb62010-07-29 18:35:47 +0200530 p_dev->config_regs |= PRESENT_EXT_STATUS;
Dominik Brodowskifc301102010-07-29 16:19:39 +0200531 ext_status = ESR_REQ_ATTN_ENA;
532 }
Dominik Brodowski1a8d4662005-06-27 16:28:53 -0700533 s->ops->set_socket(s, &s->socket);
534 s->lock_count++;
535
Dominik Brodowski1c4a77b2010-09-18 23:21:09 +0200536 dev_dbg(&p_dev->dev,
537 "enable_device: V %d, flags %x, base %x, regs %x, idx %x\n",
538 p_dev->vpp, flags, p_dev->config_base, p_dev->config_regs,
539 p_dev->config_index);
540
Dominik Brodowski1a8d4662005-06-27 16:28:53 -0700541 /* Set up CIS configuration registers */
Dominik Brodowski7feabb62010-07-29 18:35:47 +0200542 base = p_dev->config_base;
543 if (p_dev->config_regs & PRESENT_COPY) {
Dominik Brodowski1a4a0462010-07-29 15:54:54 +0200544 u16 tmp = 0;
545 dev_dbg(&p_dev->dev, "clearing CISREG_SCR\n");
546 pcmcia_write_cis_mem(s, 1, (base + CISREG_SCR)>>1, 1, &tmp);
547 }
Dominik Brodowski7feabb62010-07-29 18:35:47 +0200548 if (p_dev->config_regs & PRESENT_PIN_REPLACE) {
Dominik Brodowski1a4a0462010-07-29 15:54:54 +0200549 u16 tmp = 0;
550 dev_dbg(&p_dev->dev, "clearing CISREG_PRR\n");
551 pcmcia_write_cis_mem(s, 1, (base + CISREG_PRR)>>1, 1, &tmp);
Dominik Brodowski1a8d4662005-06-27 16:28:53 -0700552 }
Dominik Brodowski7feabb62010-07-29 18:35:47 +0200553 if (p_dev->config_regs & PRESENT_OPTION) {
Dominik Brodowski1a8d4662005-06-27 16:28:53 -0700554 if (s->functions == 1) {
Dominik Brodowski7feabb62010-07-29 18:35:47 +0200555 option = p_dev->config_index & COR_CONFIG_MASK;
Dominik Brodowski1a8d4662005-06-27 16:28:53 -0700556 } else {
Dominik Brodowski7feabb62010-07-29 18:35:47 +0200557 option = p_dev->config_index & COR_MFC_CONFIG_MASK;
558 option |= COR_FUNC_ENA|COR_IREQ_ENA;
559 if (p_dev->config_regs & PRESENT_IOBASE_0)
560 option |= COR_ADDR_DECODE;
Dominik Brodowski1a8d4662005-06-27 16:28:53 -0700561 }
Dominik Brodowski1ac71e52010-07-29 19:27:09 +0200562 if ((flags & CONF_ENABLE_IRQ) &&
563 !(flags & CONF_ENABLE_PULSE_IRQ))
Dominik Brodowski7feabb62010-07-29 18:35:47 +0200564 option |= COR_LEVEL_REQ;
565 pcmcia_write_cis_mem(s, 1, (base + CISREG_COR)>>1, 1, &option);
Jia-Ju Bai1b22dcf2018-07-30 21:42:56 +0800566 msleep(40);
Dominik Brodowski1a8d4662005-06-27 16:28:53 -0700567 }
Dominik Brodowski7feabb62010-07-29 18:35:47 +0200568 if (p_dev->config_regs & PRESENT_STATUS)
Dominik Brodowskifc301102010-07-29 16:19:39 +0200569 pcmcia_write_cis_mem(s, 1, (base + CISREG_CCSR)>>1, 1, &status);
570
Dominik Brodowski7feabb62010-07-29 18:35:47 +0200571 if (p_dev->config_regs & PRESENT_EXT_STATUS)
Dominik Brodowskifc301102010-07-29 16:19:39 +0200572 pcmcia_write_cis_mem(s, 1, (base + CISREG_ESR)>>1, 1,
573 &ext_status);
574
Dominik Brodowski7feabb62010-07-29 18:35:47 +0200575 if (p_dev->config_regs & PRESENT_IOBASE_0) {
Dominik Brodowski2ce49052010-07-24 13:14:44 +0200576 u8 b = c->io[0].start & 0xff;
Dominik Brodowski1a8d4662005-06-27 16:28:53 -0700577 pcmcia_write_cis_mem(s, 1, (base + CISREG_IOBASE_0)>>1, 1, &b);
Dominik Brodowski2ce49052010-07-24 13:14:44 +0200578 b = (c->io[0].start >> 8) & 0xff;
Dominik Brodowski1a8d4662005-06-27 16:28:53 -0700579 pcmcia_write_cis_mem(s, 1, (base + CISREG_IOBASE_1)>>1, 1, &b);
580 }
Dominik Brodowski7feabb62010-07-29 18:35:47 +0200581 if (p_dev->config_regs & PRESENT_IOSIZE) {
Dominik Brodowski2ce49052010-07-24 13:14:44 +0200582 u8 b = resource_size(&c->io[0]) + resource_size(&c->io[1]) - 1;
Dominik Brodowski1a8d4662005-06-27 16:28:53 -0700583 pcmcia_write_cis_mem(s, 1, (base + CISREG_IOSIZE)>>1, 1, &b);
584 }
585
586 /* Configure I/O windows */
587 if (c->state & CONFIG_IO_REQ) {
588 iomap.speed = io_speed;
589 for (i = 0; i < MAX_IO_WIN; i++)
Dominik Brodowskic7d00692006-01-15 08:04:43 +0100590 if (s->io[i].res) {
Dominik Brodowski1a8d4662005-06-27 16:28:53 -0700591 iomap.map = i;
592 iomap.flags = MAP_ACTIVE;
Dominik Brodowskic7d00692006-01-15 08:04:43 +0100593 switch (s->io[i].res->flags & IO_DATA_PATH_WIDTH) {
Dominik Brodowski1a8d4662005-06-27 16:28:53 -0700594 case IO_DATA_PATH_WIDTH_16:
595 iomap.flags |= MAP_16BIT; break;
596 case IO_DATA_PATH_WIDTH_AUTO:
597 iomap.flags |= MAP_AUTOSZ; break;
598 default:
599 break;
600 }
Dominik Brodowskic7d00692006-01-15 08:04:43 +0100601 iomap.start = s->io[i].res->start;
602 iomap.stop = s->io[i].res->end;
Dominik Brodowski1a8d4662005-06-27 16:28:53 -0700603 s->ops->set_io_map(s, &iomap);
604 s->io[i].Config++;
605 }
606 }
607
608 c->state |= CONFIG_LOCKED;
Dominik Brodowskie2d40962006-03-02 00:09:29 +0100609 p_dev->_locked = 1;
Dominik Brodowski059f6672010-03-30 18:07:50 +0200610 mutex_unlock(&s->ops_mutex);
Dominik Brodowski4c89e882008-08-03 10:07:45 +0200611 return 0;
Dominik Brodowski1ac71e52010-07-29 19:27:09 +0200612} /* pcmcia_enable_device */
613EXPORT_SYMBOL(pcmcia_enable_device);
Dominik Brodowski1a8d4662005-06-27 16:28:53 -0700614
615
Dominik Brodowski2ce49052010-07-24 13:14:44 +0200616/**
617 * pcmcia_request_io() - attempt to reserve port ranges for PCMCIA devices
Dominik Brodowski9485ee12010-07-31 07:09:32 +0200618 * @p_dev: the associated PCMCIA device
Dominik Brodowski1a8d4662005-06-27 16:28:53 -0700619 *
Dominik Brodowski9485ee12010-07-31 07:09:32 +0200620 * pcmcia_request_io() attempts to reserve the IO port ranges specified in
Dominik Brodowski90abdc32010-07-24 17:23:51 +0200621 * &struct pcmcia_device @p_dev->resource[0] and @p_dev->resource[1]. The
Dominik Brodowski2ce49052010-07-24 13:14:44 +0200622 * "start" value is the requested start of the IO port resource; "end"
Dominik Brodowski90abdc32010-07-24 17:23:51 +0200623 * reflects the number of ports requested. The number of IO lines requested
624 * is specified in &struct pcmcia_device @p_dev->io_lines.
Dominik Brodowski1a8d4662005-06-27 16:28:53 -0700625 */
Dominik Brodowski90abdc32010-07-24 17:23:51 +0200626int pcmcia_request_io(struct pcmcia_device *p_dev)
Dominik Brodowski1a8d4662005-06-27 16:28:53 -0700627{
Dominik Brodowski2bc5a9b2005-07-07 17:59:02 -0700628 struct pcmcia_socket *s = p_dev->socket;
Dominik Brodowski90abdc32010-07-24 17:23:51 +0200629 config_t *c = p_dev->function_config;
Dominik Brodowski94a819f2010-01-17 18:31:34 +0100630 int ret = -EINVAL;
631
632 mutex_lock(&s->ops_mutex);
Dominik Brodowskieb838fe2010-09-13 16:51:36 +0200633 dev_dbg(&p_dev->dev, "pcmcia_request_io: %pR , %pR",
634 &c->io[0], &c->io[1]);
Dominik Brodowski1a8d4662005-06-27 16:28:53 -0700635
Dominik Brodowski6d9a2992009-10-24 12:20:18 +0200636 if (!(s->state & SOCKET_PRESENT)) {
Dominik Brodowskieb838fe2010-09-13 16:51:36 +0200637 dev_dbg(&p_dev->dev, "pcmcia_request_io: No card present\n");
Dominik Brodowski94a819f2010-01-17 18:31:34 +0100638 goto out;
Dominik Brodowski6d9a2992009-10-24 12:20:18 +0200639 }
Dominik Brodowski1a8d4662005-06-27 16:28:53 -0700640
Dominik Brodowski6d9a2992009-10-24 12:20:18 +0200641 if (c->state & CONFIG_LOCKED) {
Dominik Brodowskieb838fe2010-09-13 16:51:36 +0200642 dev_dbg(&p_dev->dev, "Configuration is locked\n");
Dominik Brodowski94a819f2010-01-17 18:31:34 +0100643 goto out;
Dominik Brodowski6d9a2992009-10-24 12:20:18 +0200644 }
Dominik Brodowskif9580952008-08-03 11:40:19 +0200645 if (c->state & CONFIG_IO_REQ) {
Dominik Brodowskieb838fe2010-09-13 16:51:36 +0200646 dev_dbg(&p_dev->dev, "IO already configured\n");
Dominik Brodowski94a819f2010-01-17 18:31:34 +0100647 goto out;
Dominik Brodowskif9580952008-08-03 11:40:19 +0200648 }
Dominik Brodowski1a8d4662005-06-27 16:28:53 -0700649
Dominik Brodowski90abdc32010-07-24 17:23:51 +0200650 ret = alloc_io_space(s, &c->io[0], p_dev->io_lines);
Dominik Brodowski2ce49052010-07-24 13:14:44 +0200651 if (ret)
Dominik Brodowski94a819f2010-01-17 18:31:34 +0100652 goto out;
Dominik Brodowski1a8d4662005-06-27 16:28:53 -0700653
Dominik Brodowski2ce49052010-07-24 13:14:44 +0200654 if (c->io[1].end) {
Dominik Brodowski90abdc32010-07-24 17:23:51 +0200655 ret = alloc_io_space(s, &c->io[1], p_dev->io_lines);
Dominik Brodowski94a819f2010-01-17 18:31:34 +0100656 if (ret) {
Dominik Brodowski7cdffc82010-09-18 10:19:13 +0200657 struct resource tmp = c->io[0];
658 /* release the previously allocated resource */
Dominik Brodowski2ce49052010-07-24 13:14:44 +0200659 release_io_space(s, &c->io[0]);
Dominik Brodowski7cdffc82010-09-18 10:19:13 +0200660 /* but preserve the settings, for they worked... */
661 c->io[0].end = resource_size(&tmp);
662 c->io[0].start = tmp.start;
663 c->io[0].flags = tmp.flags;
Dominik Brodowski94a819f2010-01-17 18:31:34 +0100664 goto out;
Dominik Brodowski1a8d4662005-06-27 16:28:53 -0700665 }
Dominik Brodowski2ce49052010-07-24 13:14:44 +0200666 } else
667 c->io[1].start = 0;
Dominik Brodowski1a8d4662005-06-27 16:28:53 -0700668
Dominik Brodowski1a8d4662005-06-27 16:28:53 -0700669 c->state |= CONFIG_IO_REQ;
Dominik Brodowskie2d40962006-03-02 00:09:29 +0100670 p_dev->_io = 1;
Dominik Brodowski94a819f2010-01-17 18:31:34 +0100671
Dominik Brodowskieb838fe2010-09-13 16:51:36 +0200672 dev_dbg(&p_dev->dev, "pcmcia_request_io succeeded: %pR , %pR",
Dominik Brodowski2ce49052010-07-24 13:14:44 +0200673 &c->io[0], &c->io[1]);
Dominik Brodowski94a819f2010-01-17 18:31:34 +0100674out:
675 mutex_unlock(&s->ops_mutex);
676
677 return ret;
Dominik Brodowski1a8d4662005-06-27 16:28:53 -0700678} /* pcmcia_request_io */
679EXPORT_SYMBOL(pcmcia_request_io);
680
681
Dominik Brodowskieb141202010-03-07 12:21:16 +0100682/**
683 * pcmcia_request_irq() - attempt to request a IRQ for a PCMCIA device
Dominik Brodowski9485ee12010-07-31 07:09:32 +0200684 * @p_dev: the associated PCMCIA device
685 * @handler: IRQ handler to register
Dominik Brodowski1a8d4662005-06-27 16:28:53 -0700686 *
Dominik Brodowski9485ee12010-07-31 07:09:32 +0200687 * pcmcia_request_irq() is a wrapper around request_irq() which allows
Dominik Brodowskieb141202010-03-07 12:21:16 +0100688 * the PCMCIA core to clean up the registration in pcmcia_disable_device().
689 * Drivers are free to use request_irq() directly, but then they need to
Dominik Brodowski9485ee12010-07-31 07:09:32 +0200690 * call free_irq() themselfves, too. Also, only %IRQF_SHARED capable IRQ
Dominik Brodowskieb141202010-03-07 12:21:16 +0100691 * handlers are allowed.
Dominik Brodowski1a8d4662005-06-27 16:28:53 -0700692 */
Dominik Brodowskieb141202010-03-07 12:21:16 +0100693int __must_check pcmcia_request_irq(struct pcmcia_device *p_dev,
694 irq_handler_t handler)
Dominik Brodowski1a8d4662005-06-27 16:28:53 -0700695{
Dominik Brodowskieb141202010-03-07 12:21:16 +0100696 int ret;
Dominik Brodowski1a8d4662005-06-27 16:28:53 -0700697
Dominik Brodowskieb141202010-03-07 12:21:16 +0100698 if (!p_dev->irq)
699 return -EINVAL;
Dominik Brodowski94a819f2010-01-17 18:31:34 +0100700
Dominik Brodowskieb141202010-03-07 12:21:16 +0100701 ret = request_irq(p_dev->irq, handler, IRQF_SHARED,
702 p_dev->devname, p_dev->priv);
703 if (!ret)
704 p_dev->_irq = 1;
Dominik Brodowski1a8d4662005-06-27 16:28:53 -0700705
Dominik Brodowskieb141202010-03-07 12:21:16 +0100706 return ret;
707}
708EXPORT_SYMBOL(pcmcia_request_irq);
Dominik Brodowski1a8d4662005-06-27 16:28:53 -0700709
Dominik Brodowskieb141202010-03-07 12:21:16 +0100710
Dominik Brodowski6f0f38c2010-04-08 20:33:16 +0200711#ifdef CONFIG_PCMCIA_PROBE
712
713/* mask of IRQs already reserved by other cards, we should avoid using them */
Dominik Brodowski127c03c2010-08-03 09:33:45 +0200714static u8 pcmcia_used_irq[32];
Dominik Brodowski6f0f38c2010-04-08 20:33:16 +0200715
716static irqreturn_t test_action(int cpl, void *dev_id)
717{
718 return IRQ_NONE;
719}
720
721/**
722 * pcmcia_setup_isa_irq() - determine whether an ISA IRQ can be used
Lee Jones1d26d6f2021-03-12 11:02:38 +0000723 * @p_dev: the associated PCMCIA device
724 * @type: IRQ type (flags)
Dominik Brodowski6f0f38c2010-04-08 20:33:16 +0200725 *
726 * locking note: must be called with ops_mutex locked.
727 */
728static int pcmcia_setup_isa_irq(struct pcmcia_device *p_dev, int type)
729{
730 struct pcmcia_socket *s = p_dev->socket;
731 unsigned int try, irq;
732 u32 mask = s->irq_mask;
733 int ret = -ENODEV;
734
735 for (try = 0; try < 64; try++) {
736 irq = try % 32;
737
Dominik Brodowski127c03c2010-08-03 09:33:45 +0200738 if (irq > NR_IRQS)
739 continue;
740
Dominik Brodowski6f0f38c2010-04-08 20:33:16 +0200741 /* marked as available by driver, not blocked by userspace? */
742 if (!((mask >> irq) & 1))
743 continue;
744
745 /* avoid an IRQ which is already used by another PCMCIA card */
746 if ((try < 32) && pcmcia_used_irq[irq])
747 continue;
748
749 /* register the correct driver, if possible, to check whether
750 * registering a dummy handle works, i.e. if the IRQ isn't
751 * marked as used by the kernel resource management core */
752 ret = request_irq(irq, test_action, type, p_dev->devname,
753 p_dev);
754 if (!ret) {
755 free_irq(irq, p_dev);
Dominik Brodowskieb141202010-03-07 12:21:16 +0100756 p_dev->irq = s->pcmcia_irq = irq;
Dominik Brodowski6f0f38c2010-04-08 20:33:16 +0200757 pcmcia_used_irq[irq]++;
758 break;
759 }
760 }
761
762 return ret;
763}
764
765void pcmcia_cleanup_irq(struct pcmcia_socket *s)
766{
Dominik Brodowski6f840af2010-03-07 10:51:23 +0100767 pcmcia_used_irq[s->pcmcia_irq]--;
768 s->pcmcia_irq = 0;
Dominik Brodowski6f0f38c2010-04-08 20:33:16 +0200769}
770
771#else /* CONFIG_PCMCIA_PROBE */
772
773static int pcmcia_setup_isa_irq(struct pcmcia_device *p_dev, int type)
774{
775 return -EINVAL;
776}
777
778void pcmcia_cleanup_irq(struct pcmcia_socket *s)
779{
Dominik Brodowski6f840af2010-03-07 10:51:23 +0100780 s->pcmcia_irq = 0;
Dominik Brodowski6f0f38c2010-04-08 20:33:16 +0200781 return;
782}
783
784#endif /* CONFIG_PCMCIA_PROBE */
785
786
787/**
788 * pcmcia_setup_irq() - determine IRQ to be used for device
Lee Jones1d26d6f2021-03-12 11:02:38 +0000789 * @p_dev: the associated PCMCIA device
Dominik Brodowski6f0f38c2010-04-08 20:33:16 +0200790 *
791 * locking note: must be called with ops_mutex locked.
792 */
793int pcmcia_setup_irq(struct pcmcia_device *p_dev)
794{
795 struct pcmcia_socket *s = p_dev->socket;
796
Dominik Brodowskieb141202010-03-07 12:21:16 +0100797 if (p_dev->irq)
Dominik Brodowski6f0f38c2010-04-08 20:33:16 +0200798 return 0;
799
800 /* already assigned? */
Dominik Brodowski6f840af2010-03-07 10:51:23 +0100801 if (s->pcmcia_irq) {
Dominik Brodowskieb141202010-03-07 12:21:16 +0100802 p_dev->irq = s->pcmcia_irq;
Dominik Brodowski6f0f38c2010-04-08 20:33:16 +0200803 return 0;
804 }
805
806 /* prefer an exclusive ISA irq */
807 if (!pcmcia_setup_isa_irq(p_dev, 0))
808 return 0;
809
810 /* but accept a shared ISA irq */
811 if (!pcmcia_setup_isa_irq(p_dev, IRQF_SHARED))
812 return 0;
813
814 /* but use the PCI irq otherwise */
815 if (s->pci_irq) {
Dominik Brodowskieb141202010-03-07 12:21:16 +0100816 p_dev->irq = s->pcmcia_irq = s->pci_irq;
Dominik Brodowski6f0f38c2010-04-08 20:33:16 +0200817 return 0;
818 }
819
820 return -EINVAL;
821}
822
823
Dominik Brodowskicdb13802010-07-28 10:59:06 +0200824/**
825 * pcmcia_request_window() - attempt to reserve iomem for PCMCIA devices
Dominik Brodowski9485ee12010-07-31 07:09:32 +0200826 * @p_dev: the associated PCMCIA device
827 * @res: &struct resource pointing to p_dev->resource[2..5]
828 * @speed: access speed
Dominik Brodowski1a8d4662005-06-27 16:28:53 -0700829 *
Dominik Brodowskicdb13802010-07-28 10:59:06 +0200830 * pcmcia_request_window() attepts to reserve an iomem ranges specified in
Dominik Brodowski9485ee12010-07-31 07:09:32 +0200831 * &struct resource @res pointing to one of the entries in
832 * &struct pcmcia_device @p_dev->resource[2..5]. The "start" value is the
Dominik Brodowskicdb13802010-07-28 10:59:06 +0200833 * requested start of the IO mem resource; "end" reflects the size
834 * requested.
Dominik Brodowski1a8d4662005-06-27 16:28:53 -0700835 */
Dominik Brodowskicdb13802010-07-28 10:59:06 +0200836int pcmcia_request_window(struct pcmcia_device *p_dev, struct resource *res,
837 unsigned int speed)
Dominik Brodowski1a8d4662005-06-27 16:28:53 -0700838{
Dominik Brodowski6838b032009-11-03 01:31:52 +0100839 struct pcmcia_socket *s = p_dev->socket;
Dominik Brodowski82f88e32009-11-03 01:16:12 +0100840 pccard_mem_map *win;
Dominik Brodowski1a8d4662005-06-27 16:28:53 -0700841 u_long align;
842 int w;
843
Dominik Brodowski1c4a77b2010-09-18 23:21:09 +0200844 dev_dbg(&p_dev->dev, "request_window %pR %d\n", res, speed);
845
Dominik Brodowski6d9a2992009-10-24 12:20:18 +0200846 if (!(s->state & SOCKET_PRESENT)) {
Dominik Brodowskieb838fe2010-09-13 16:51:36 +0200847 dev_dbg(&p_dev->dev, "No card present\n");
Dominik Brodowski3939c1ef2008-08-03 11:10:56 +0200848 return -ENODEV;
Dominik Brodowski6d9a2992009-10-24 12:20:18 +0200849 }
Dominik Brodowski1a8d4662005-06-27 16:28:53 -0700850
851 /* Window size defaults to smallest available */
Dominik Brodowskicdb13802010-07-28 10:59:06 +0200852 if (res->end == 0)
853 res->end = s->map_size;
854 align = (s->features & SS_CAP_MEM_ALIGN) ? res->end : s->map_size;
855 if (res->end & (s->map_size-1)) {
Dominik Brodowskieb838fe2010-09-13 16:51:36 +0200856 dev_dbg(&p_dev->dev, "invalid map size\n");
Dominik Brodowski69ba4432008-08-03 12:10:53 +0200857 return -EINVAL;
858 }
Dominik Brodowskicdb13802010-07-28 10:59:06 +0200859 if ((res->start && (s->features & SS_CAP_STATIC_MAP)) ||
860 (res->start & (align-1))) {
Dominik Brodowskieb838fe2010-09-13 16:51:36 +0200861 dev_dbg(&p_dev->dev, "invalid base address\n");
Dominik Brodowski69ba4432008-08-03 12:10:53 +0200862 return -EINVAL;
863 }
Dominik Brodowskicdb13802010-07-28 10:59:06 +0200864 if (res->start)
Dominik Brodowski1a8d4662005-06-27 16:28:53 -0700865 align = 0;
866
867 /* Allocate system memory window */
Dominik Brodowski0ca724d32010-07-24 19:03:02 +0200868 mutex_lock(&s->ops_mutex);
Dominik Brodowski1a8d4662005-06-27 16:28:53 -0700869 for (w = 0; w < MAX_WIN; w++)
Dominik Brodowski9fea84f2009-12-07 22:11:45 +0100870 if (!(s->state & SOCKET_WIN_REQ(w)))
871 break;
Dominik Brodowskif9580952008-08-03 11:40:19 +0200872 if (w == MAX_WIN) {
Dominik Brodowskieb838fe2010-09-13 16:51:36 +0200873 dev_dbg(&p_dev->dev, "all windows are used already\n");
Dominik Brodowski0ca724d32010-07-24 19:03:02 +0200874 mutex_unlock(&s->ops_mutex);
Dominik Brodowskif9580952008-08-03 11:40:19 +0200875 return -EINVAL;
876 }
Dominik Brodowski1a8d4662005-06-27 16:28:53 -0700877
878 win = &s->win[w];
Dominik Brodowski1a8d4662005-06-27 16:28:53 -0700879
880 if (!(s->features & SS_CAP_STATIC_MAP)) {
Dominik Brodowskicdb13802010-07-28 10:59:06 +0200881 win->res = pcmcia_find_mem_region(res->start, res->end, align,
Dominik Brodowski0ca724d32010-07-24 19:03:02 +0200882 0, s);
Dominik Brodowski82f88e32009-11-03 01:16:12 +0100883 if (!win->res) {
Dominik Brodowskieb838fe2010-09-13 16:51:36 +0200884 dev_dbg(&p_dev->dev, "allocating mem region failed\n");
Dominik Brodowski6b8e0872010-01-12 21:42:51 +0100885 mutex_unlock(&s->ops_mutex);
Dominik Brodowskif9580952008-08-03 11:40:19 +0200886 return -EINVAL;
887 }
Dominik Brodowski1a8d4662005-06-27 16:28:53 -0700888 }
Dominik Brodowski6838b032009-11-03 01:31:52 +0100889 p_dev->_win |= CLIENT_WIN_REQ(w);
Dominik Brodowski1a8d4662005-06-27 16:28:53 -0700890
891 /* Configure the socket controller */
Dominik Brodowski82f88e32009-11-03 01:16:12 +0100892 win->map = w+1;
Dominik Brodowskicdb13802010-07-28 10:59:06 +0200893 win->flags = res->flags & WIN_FLAGS_MAP;
894 win->speed = speed;
Dominik Brodowski82f88e32009-11-03 01:16:12 +0100895 win->card_start = 0;
Dominik Brodowski6b8e0872010-01-12 21:42:51 +0100896
Dominik Brodowski82f88e32009-11-03 01:16:12 +0100897 if (s->ops->set_mem_map(s, win) != 0) {
Dominik Brodowskieb838fe2010-09-13 16:51:36 +0200898 dev_dbg(&p_dev->dev, "failed to set memory mapping\n");
Dominik Brodowski6b8e0872010-01-12 21:42:51 +0100899 mutex_unlock(&s->ops_mutex);
Dominik Brodowski926c5402008-08-03 12:15:11 +0200900 return -EIO;
901 }
Dominik Brodowski1a8d4662005-06-27 16:28:53 -0700902 s->state |= SOCKET_WIN_REQ(w);
903
904 /* Return window handle */
Dominik Brodowski9fea84f2009-12-07 22:11:45 +0100905 if (s->features & SS_CAP_STATIC_MAP)
Dominik Brodowskicdb13802010-07-28 10:59:06 +0200906 res->start = win->static_start;
Dominik Brodowski9fea84f2009-12-07 22:11:45 +0100907 else
Dominik Brodowskicdb13802010-07-28 10:59:06 +0200908 res->start = win->res->start;
Dominik Brodowski9fea84f2009-12-07 22:11:45 +0100909
Dominik Brodowski0ca724d32010-07-24 19:03:02 +0200910 /* convert to new-style resources */
Dominik Brodowskicdb13802010-07-28 10:59:06 +0200911 res->end += res->start - 1;
912 res->flags &= ~WIN_FLAGS_REQ;
913 res->flags |= (win->map << 2) | IORESOURCE_MEM;
Dominik Brodowskiad0c7be2010-07-25 13:10:22 +0200914 res->parent = win->res;
915 if (win->res)
916 request_resource(&iomem_resource, res);
917
Dominik Brodowskieb838fe2010-09-13 16:51:36 +0200918 dev_dbg(&p_dev->dev, "request_window results in %pR\n", res);
Dominik Brodowski0ca724d32010-07-24 19:03:02 +0200919
Dominik Brodowski6b8e0872010-01-12 21:42:51 +0100920 mutex_unlock(&s->ops_mutex);
Dominik Brodowski1a8d4662005-06-27 16:28:53 -0700921
Dominik Brodowski4c89e882008-08-03 10:07:45 +0200922 return 0;
Dominik Brodowski1a8d4662005-06-27 16:28:53 -0700923} /* pcmcia_request_window */
924EXPORT_SYMBOL(pcmcia_request_window);
Dominik Brodowski5f2a71f2006-01-15 09:32:39 +0100925
Dominik Brodowski9485ee12010-07-31 07:09:32 +0200926
927/**
928 * pcmcia_disable_device() - disable and clean up a PCMCIA device
929 * @p_dev: the associated PCMCIA device
930 *
931 * pcmcia_disable_device() is the driver-callable counterpart to
932 * pcmcia_enable_device(): If a PCMCIA device is no longer used,
933 * drivers are expected to clean up and disable the device by calling
934 * this function. Any I/O ranges (iomem and ioports) will be released,
935 * the Vpp voltage will be set to 0, and IRQs will no longer be
936 * generated -- at least if there is no other card function (of
937 * multifunction devices) being used.
938 */
Dominik Brodowski9fea84f2009-12-07 22:11:45 +0100939void pcmcia_disable_device(struct pcmcia_device *p_dev)
940{
Dominik Brodowski0ca724d32010-07-24 19:03:02 +0200941 int i;
Dominik Brodowski1c4a77b2010-09-18 23:21:09 +0200942
943 dev_dbg(&p_dev->dev, "disabling device\n");
944
Dominik Brodowski0ca724d32010-07-24 19:03:02 +0200945 for (i = 0; i < MAX_WIN; i++) {
946 struct resource *res = p_dev->resource[MAX_IO_WIN + i];
947 if (res->flags & WIN_FLAGS_REQ)
948 pcmcia_release_window(p_dev, res);
949 }
950
Dominik Brodowski5f2a71f2006-01-15 09:32:39 +0100951 pcmcia_release_configuration(p_dev);
Dominik Brodowski2ce49052010-07-24 13:14:44 +0200952 pcmcia_release_io(p_dev);
Patrick McHardy418c5272010-07-20 15:21:42 -0700953 if (p_dev->_irq) {
Dominik Brodowskieb141202010-03-07 12:21:16 +0100954 free_irq(p_dev->irq, p_dev->priv);
Patrick McHardy418c5272010-07-20 15:21:42 -0700955 p_dev->_irq = 0;
956 }
Dominik Brodowski5f2a71f2006-01-15 09:32:39 +0100957}
958EXPORT_SYMBOL(pcmcia_disable_device);