149b1153aSDominik Brodowski /* 249b1153aSDominik Brodowski * rsrc_iodyn.c -- Resource management routines for MEM-static sockets. 349b1153aSDominik Brodowski * 449b1153aSDominik Brodowski * This program is free software; you can redistribute it and/or modify 549b1153aSDominik Brodowski * it under the terms of the GNU General Public License version 2 as 649b1153aSDominik Brodowski * published by the Free Software Foundation. 749b1153aSDominik Brodowski * 849b1153aSDominik Brodowski * The initial developer of the original code is David A. Hinds 949b1153aSDominik Brodowski * <dahinds@users.sourceforge.net>. Portions created by David A. Hinds 1049b1153aSDominik Brodowski * are Copyright (C) 1999 David A. Hinds. All Rights Reserved. 1149b1153aSDominik Brodowski * 1249b1153aSDominik Brodowski * (C) 1999 David A. Hinds 1349b1153aSDominik Brodowski */ 1449b1153aSDominik Brodowski 156d59622eSTejun Heo #include <linux/slab.h> 1649b1153aSDominik Brodowski #include <linux/module.h> 1749b1153aSDominik Brodowski #include <linux/kernel.h> 1849b1153aSDominik Brodowski 1949b1153aSDominik Brodowski #include <pcmcia/ss.h> 2049b1153aSDominik Brodowski #include <pcmcia/cistpl.h> 2149b1153aSDominik Brodowski #include "cs_internal.h" 2249b1153aSDominik Brodowski 2349b1153aSDominik Brodowski 2449b1153aSDominik Brodowski struct pcmcia_align_data { 2549b1153aSDominik Brodowski unsigned long mask; 2649b1153aSDominik Brodowski unsigned long offset; 2749b1153aSDominik Brodowski }; 2849b1153aSDominik Brodowski 2949b1153aSDominik Brodowski static resource_size_t pcmcia_align(void *align_data, 3049b1153aSDominik Brodowski const struct resource *res, 3149b1153aSDominik Brodowski resource_size_t size, resource_size_t align) 3249b1153aSDominik Brodowski { 3349b1153aSDominik Brodowski struct pcmcia_align_data *data = align_data; 3449b1153aSDominik Brodowski resource_size_t start; 3549b1153aSDominik Brodowski 3649b1153aSDominik Brodowski start = (res->start & ~data->mask) + data->offset; 3749b1153aSDominik Brodowski if (start < res->start) 3849b1153aSDominik Brodowski start += data->mask + 1; 3949b1153aSDominik Brodowski 4049b1153aSDominik Brodowski #ifdef CONFIG_X86 4149b1153aSDominik Brodowski if (res->flags & IORESOURCE_IO) { 4249b1153aSDominik Brodowski if (start & 0x300) 4349b1153aSDominik Brodowski start = (start + 0x3ff) & ~0x3ff; 4449b1153aSDominik Brodowski } 4549b1153aSDominik Brodowski #endif 4649b1153aSDominik Brodowski 4749b1153aSDominik Brodowski #ifdef CONFIG_M68K 4849b1153aSDominik Brodowski if (res->flags & IORESOURCE_IO) { 4949b1153aSDominik Brodowski if ((res->start + size - 1) >= 1024) 5049b1153aSDominik Brodowski start = res->end; 5149b1153aSDominik Brodowski } 5249b1153aSDominik Brodowski #endif 5349b1153aSDominik Brodowski 5449b1153aSDominik Brodowski return start; 5549b1153aSDominik Brodowski } 5649b1153aSDominik Brodowski 5749b1153aSDominik Brodowski 58b19a7275SDominik Brodowski static struct resource *__iodyn_find_io_region(struct pcmcia_socket *s, 59b19a7275SDominik Brodowski unsigned long base, int num, 60b19a7275SDominik Brodowski unsigned long align) 6149b1153aSDominik Brodowski { 6249b1153aSDominik Brodowski struct resource *res = pcmcia_make_resource(0, num, IORESOURCE_IO, 6349b1153aSDominik Brodowski dev_name(&s->dev)); 6449b1153aSDominik Brodowski struct pcmcia_align_data data; 6549b1153aSDominik Brodowski unsigned long min = base; 6649b1153aSDominik Brodowski int ret; 6749b1153aSDominik Brodowski 6849b1153aSDominik Brodowski data.mask = align - 1; 6949b1153aSDominik Brodowski data.offset = base & data.mask; 7049b1153aSDominik Brodowski 7149b1153aSDominik Brodowski #ifdef CONFIG_PCI 7249b1153aSDominik Brodowski if (s->cb_dev) { 7349b1153aSDominik Brodowski ret = pci_bus_alloc_resource(s->cb_dev->bus, res, num, 1, 7449b1153aSDominik Brodowski min, 0, pcmcia_align, &data); 7549b1153aSDominik Brodowski } else 7649b1153aSDominik Brodowski #endif 7749b1153aSDominik Brodowski ret = allocate_resource(&ioport_resource, res, num, min, ~0UL, 7849b1153aSDominik Brodowski 1, pcmcia_align, &data); 7949b1153aSDominik Brodowski 8049b1153aSDominik Brodowski if (ret != 0) { 8149b1153aSDominik Brodowski kfree(res); 8249b1153aSDominik Brodowski res = NULL; 8349b1153aSDominik Brodowski } 8449b1153aSDominik Brodowski return res; 8549b1153aSDominik Brodowski } 8649b1153aSDominik Brodowski 87b19a7275SDominik Brodowski static int iodyn_find_io(struct pcmcia_socket *s, unsigned int attr, 88b19a7275SDominik Brodowski unsigned int *base, unsigned int num, 89ad0c7be2SDominik Brodowski unsigned int align, struct resource **parent) 90b19a7275SDominik Brodowski { 91b19a7275SDominik Brodowski int i, ret = 0; 92b19a7275SDominik Brodowski 93b19a7275SDominik Brodowski /* Check for an already-allocated window that must conflict with 94b19a7275SDominik Brodowski * what was asked for. It is a hack because it does not catch all 95b19a7275SDominik Brodowski * potential conflicts, just the most obvious ones. 96b19a7275SDominik Brodowski */ 97b19a7275SDominik Brodowski for (i = 0; i < MAX_IO_WIN; i++) { 98b19a7275SDominik Brodowski if (!s->io[i].res) 99b19a7275SDominik Brodowski continue; 100b19a7275SDominik Brodowski 101b19a7275SDominik Brodowski if (!*base) 102b19a7275SDominik Brodowski continue; 103b19a7275SDominik Brodowski 104b19a7275SDominik Brodowski if ((s->io[i].res->start & (align-1)) == *base) 105b19a7275SDominik Brodowski return -EBUSY; 106b19a7275SDominik Brodowski } 107b19a7275SDominik Brodowski 108b19a7275SDominik Brodowski for (i = 0; i < MAX_IO_WIN; i++) { 109b19a7275SDominik Brodowski struct resource *res = s->io[i].res; 110b19a7275SDominik Brodowski unsigned int try; 111b19a7275SDominik Brodowski 112b19a7275SDominik Brodowski if (res && (res->flags & IORESOURCE_BITS) != 113b19a7275SDominik Brodowski (attr & IORESOURCE_BITS)) 114b19a7275SDominik Brodowski continue; 115b19a7275SDominik Brodowski 116b19a7275SDominik Brodowski if (!res) { 117b19a7275SDominik Brodowski if (align == 0) 118b19a7275SDominik Brodowski align = 0x10000; 119b19a7275SDominik Brodowski 120b19a7275SDominik Brodowski res = s->io[i].res = __iodyn_find_io_region(s, *base, 121b19a7275SDominik Brodowski num, align); 122b19a7275SDominik Brodowski if (!res) 123b19a7275SDominik Brodowski return -EINVAL; 124b19a7275SDominik Brodowski 125b19a7275SDominik Brodowski *base = res->start; 126b19a7275SDominik Brodowski s->io[i].res->flags = 127b19a7275SDominik Brodowski ((res->flags & ~IORESOURCE_BITS) | 128b19a7275SDominik Brodowski (attr & IORESOURCE_BITS)); 129b19a7275SDominik Brodowski s->io[i].InUse = num; 130ad0c7be2SDominik Brodowski *parent = res; 131b19a7275SDominik Brodowski return 0; 132b19a7275SDominik Brodowski } 133b19a7275SDominik Brodowski 134b19a7275SDominik Brodowski /* Try to extend top of window */ 135b19a7275SDominik Brodowski try = res->end + 1; 136b19a7275SDominik Brodowski if ((*base == 0) || (*base == try)) { 137b19a7275SDominik Brodowski if (adjust_resource(s->io[i].res, res->start, 138*28f65c11SJoe Perches resource_size(res) + num)) 139b19a7275SDominik Brodowski continue; 140b19a7275SDominik Brodowski *base = try; 141b19a7275SDominik Brodowski s->io[i].InUse += num; 142ad0c7be2SDominik Brodowski *parent = res; 143b19a7275SDominik Brodowski return 0; 144b19a7275SDominik Brodowski } 145b19a7275SDominik Brodowski 146b19a7275SDominik Brodowski /* Try to extend bottom of window */ 147b19a7275SDominik Brodowski try = res->start - num; 148b19a7275SDominik Brodowski if ((*base == 0) || (*base == try)) { 149b19a7275SDominik Brodowski if (adjust_resource(s->io[i].res, 150b19a7275SDominik Brodowski res->start - num, 151*28f65c11SJoe Perches resource_size(res) + num)) 152b19a7275SDominik Brodowski continue; 153b19a7275SDominik Brodowski *base = try; 154b19a7275SDominik Brodowski s->io[i].InUse += num; 155ad0c7be2SDominik Brodowski *parent = res; 156b19a7275SDominik Brodowski return 0; 157b19a7275SDominik Brodowski } 158b19a7275SDominik Brodowski } 159b19a7275SDominik Brodowski 160b19a7275SDominik Brodowski return -EINVAL; 161b19a7275SDominik Brodowski } 162b19a7275SDominik Brodowski 163b19a7275SDominik Brodowski 16449b1153aSDominik Brodowski struct pccard_resource_ops pccard_iodyn_ops = { 16549b1153aSDominik Brodowski .validate_mem = NULL, 166b19a7275SDominik Brodowski .find_io = iodyn_find_io, 16749b1153aSDominik Brodowski .find_mem = NULL, 16849b1153aSDominik Brodowski .init = static_init, 16949b1153aSDominik Brodowski .exit = NULL, 17049b1153aSDominik Brodowski }; 17149b1153aSDominik Brodowski EXPORT_SYMBOL(pccard_iodyn_ops); 172