blob: f53c237bda2f0324d75a37efe5bb43f891ff52ff [file] [log] [blame]
Dominik Brodowski49b11532010-03-07 16:41:57 +01001/*
2 * rsrc_iodyn.c -- Resource management routines for MEM-static sockets.
3 *
4 * This program is free software; you can redistribute it and/or modify
5 * it under the terms of the GNU General Public License version 2 as
6 * published by the Free Software Foundation.
7 *
8 * The initial developer of the original code is David A. Hinds
9 * <dahinds@users.sourceforge.net>. Portions created by David A. Hinds
10 * are Copyright (C) 1999 David A. Hinds. All Rights Reserved.
11 *
12 * (C) 1999 David A. Hinds
13 */
14
Tejun Heo6d596222010-03-30 02:52:37 +090015#include <linux/slab.h>
Dominik Brodowski49b11532010-03-07 16:41:57 +010016#include <linux/module.h>
17#include <linux/kernel.h>
18
Dominik Brodowski49b11532010-03-07 16:41:57 +010019#include <pcmcia/ss.h>
Dominik Brodowski49b11532010-03-07 16:41:57 +010020#include <pcmcia/cistpl.h>
21#include "cs_internal.h"
22
23
24struct pcmcia_align_data {
25 unsigned long mask;
26 unsigned long offset;
27};
28
29static resource_size_t pcmcia_align(void *align_data,
30 const struct resource *res,
31 resource_size_t size, resource_size_t align)
32{
33 struct pcmcia_align_data *data = align_data;
34 resource_size_t start;
35
36 start = (res->start & ~data->mask) + data->offset;
37 if (start < res->start)
38 start += data->mask + 1;
39
40#ifdef CONFIG_X86
41 if (res->flags & IORESOURCE_IO) {
42 if (start & 0x300)
43 start = (start + 0x3ff) & ~0x3ff;
44 }
45#endif
46
47#ifdef CONFIG_M68K
48 if (res->flags & IORESOURCE_IO) {
49 if ((res->start + size - 1) >= 1024)
50 start = res->end;
51 }
52#endif
53
54 return start;
55}
56
57
Dominik Brodowskib19a7272010-03-20 13:10:47 +010058static struct resource *__iodyn_find_io_region(struct pcmcia_socket *s,
59 unsigned long base, int num,
60 unsigned long align)
Dominik Brodowski49b11532010-03-07 16:41:57 +010061{
62 struct resource *res = pcmcia_make_resource(0, num, IORESOURCE_IO,
63 dev_name(&s->dev));
64 struct pcmcia_align_data data;
65 unsigned long min = base;
66 int ret;
67
Dominik Brodowski49b11532010-03-07 16:41:57 +010068 data.mask = align - 1;
69 data.offset = base & data.mask;
70
71#ifdef CONFIG_PCI
72 if (s->cb_dev) {
73 ret = pci_bus_alloc_resource(s->cb_dev->bus, res, num, 1,
74 min, 0, pcmcia_align, &data);
75 } else
76#endif
77 ret = allocate_resource(&ioport_resource, res, num, min, ~0UL,
78 1, pcmcia_align, &data);
79
80 if (ret != 0) {
81 kfree(res);
82 res = NULL;
83 }
84 return res;
85}
86
Dominik Brodowskib19a7272010-03-20 13:10:47 +010087static int iodyn_find_io(struct pcmcia_socket *s, unsigned int attr,
88 unsigned int *base, unsigned int num,
Dominik Brodowskiad0c7be2010-07-25 13:10:22 +020089 unsigned int align, struct resource **parent)
Dominik Brodowskib19a7272010-03-20 13:10:47 +010090{
91 int i, ret = 0;
92
93 /* Check for an already-allocated window that must conflict with
94 * what was asked for. It is a hack because it does not catch all
95 * potential conflicts, just the most obvious ones.
96 */
97 for (i = 0; i < MAX_IO_WIN; i++) {
98 if (!s->io[i].res)
99 continue;
100
101 if (!*base)
102 continue;
103
104 if ((s->io[i].res->start & (align-1)) == *base)
105 return -EBUSY;
106 }
107
108 for (i = 0; i < MAX_IO_WIN; i++) {
109 struct resource *res = s->io[i].res;
110 unsigned int try;
111
112 if (res && (res->flags & IORESOURCE_BITS) !=
113 (attr & IORESOURCE_BITS))
114 continue;
115
116 if (!res) {
117 if (align == 0)
118 align = 0x10000;
119
120 res = s->io[i].res = __iodyn_find_io_region(s, *base,
121 num, align);
122 if (!res)
123 return -EINVAL;
124
125 *base = res->start;
126 s->io[i].res->flags =
127 ((res->flags & ~IORESOURCE_BITS) |
128 (attr & IORESOURCE_BITS));
129 s->io[i].InUse = num;
Dominik Brodowskiad0c7be2010-07-25 13:10:22 +0200130 *parent = res;
Dominik Brodowskib19a7272010-03-20 13:10:47 +0100131 return 0;
132 }
133
134 /* Try to extend top of window */
135 try = res->end + 1;
136 if ((*base == 0) || (*base == try)) {
137 if (adjust_resource(s->io[i].res, res->start,
Joe Perches28f65c112011-06-09 09:13:32 -0700138 resource_size(res) + num))
Dominik Brodowskib19a7272010-03-20 13:10:47 +0100139 continue;
140 *base = try;
141 s->io[i].InUse += num;
Dominik Brodowskiad0c7be2010-07-25 13:10:22 +0200142 *parent = res;
Dominik Brodowskib19a7272010-03-20 13:10:47 +0100143 return 0;
144 }
145
146 /* Try to extend bottom of window */
147 try = res->start - num;
148 if ((*base == 0) || (*base == try)) {
149 if (adjust_resource(s->io[i].res,
Joe Perches28f65c112011-06-09 09:13:32 -0700150 res->start - num,
151 resource_size(res) + num))
Dominik Brodowskib19a7272010-03-20 13:10:47 +0100152 continue;
153 *base = try;
154 s->io[i].InUse += num;
Dominik Brodowskiad0c7be2010-07-25 13:10:22 +0200155 *parent = res;
Dominik Brodowskib19a7272010-03-20 13:10:47 +0100156 return 0;
157 }
158 }
159
160 return -EINVAL;
161}
162
163
Dominik Brodowski49b11532010-03-07 16:41:57 +0100164struct pccard_resource_ops pccard_iodyn_ops = {
165 .validate_mem = NULL,
Dominik Brodowskib19a7272010-03-20 13:10:47 +0100166 .find_io = iodyn_find_io,
Dominik Brodowski49b11532010-03-07 16:41:57 +0100167 .find_mem = NULL,
Dominik Brodowski49b11532010-03-07 16:41:57 +0100168 .init = static_init,
169 .exit = NULL,
170};
171EXPORT_SYMBOL(pccard_iodyn_ops);