initial commit with v2.6.9
[linux-2.6.9-moxart.git] / drivers / pcmcia / cardbus.c
blob1b86c6d3a572f42423e52acc41ec5afe20737ed9
1 /*======================================================================
3 Cardbus device configuration
5 cardbus.c 1.87 2002/10/24 06:11:41
7 The contents of this file are subject to the Mozilla Public
8 License Version 1.1 (the "License"); you may not use this file
9 except in compliance with the License. You may obtain a copy of
10 the License at http://www.mozilla.org/MPL/
12 Software distributed under the License is distributed on an "AS
13 IS" basis, WITHOUT WARRANTY OF ANY KIND, either express or
14 implied. See the License for the specific language governing
15 rights and limitations under the License.
17 The initial developer of the original code is David A. Hinds
18 <dahinds@users.sourceforge.net>. Portions created by David A. Hinds
19 are Copyright (C) 1999 David A. Hinds. All Rights Reserved.
21 Alternatively, the contents of this file may be used under the
22 terms of the GNU General Public License version 2 (the "GPL"), in which
23 case the provisions of the GPL are applicable instead of the
24 above. If you wish to allow the use of your version of this file
25 only under the terms of the GPL and not to allow others to use
26 your version of this file under the MPL, indicate your decision
27 by deleting the provisions above and replace them with the notice
28 and other provisions required by the GPL. If you do not delete
29 the provisions above, a recipient may use your version of this
30 file under either the MPL or the GPL.
32 ======================================================================*/
35 * Cardbus handling has been re-written to be more of a PCI bridge thing,
36 * and the PCI code basically does all the resource handling.
38 * Linus, Jan 2000
42 #include <linux/module.h>
43 #include <linux/kernel.h>
44 #include <linux/string.h>
45 #include <linux/slab.h>
46 #include <linux/mm.h>
47 #include <linux/pci.h>
48 #include <linux/ioport.h>
49 #include <asm/irq.h>
50 #include <asm/io.h>
52 #define IN_CARD_SERVICES
53 #include <pcmcia/version.h>
54 #include <pcmcia/cs_types.h>
55 #include <pcmcia/ss.h>
56 #include <pcmcia/cs.h>
57 #include <pcmcia/bulkmem.h>
58 #include <pcmcia/cistpl.h>
59 #include "cs_internal.h"
61 /*====================================================================*/
63 #define FIND_FIRST_BIT(n) ((n) - ((n) & ((n)-1)))
65 /* Offsets in the Expansion ROM Image Header */
66 #define ROM_SIGNATURE 0x0000 /* 2 bytes */
67 #define ROM_DATA_PTR 0x0018 /* 2 bytes */
69 /* Offsets in the CardBus PC Card Data Structure */
70 #define PCDATA_SIGNATURE 0x0000 /* 4 bytes */
71 #define PCDATA_VPD_PTR 0x0008 /* 2 bytes */
72 #define PCDATA_LENGTH 0x000a /* 2 bytes */
73 #define PCDATA_REVISION 0x000c
74 #define PCDATA_IMAGE_SZ 0x0010 /* 2 bytes */
75 #define PCDATA_ROM_LEVEL 0x0012 /* 2 bytes */
76 #define PCDATA_CODE_TYPE 0x0014
77 #define PCDATA_INDICATOR 0x0015
79 /*=====================================================================
81 Expansion ROM's have a special layout, and pointers specify an
82 image number and an offset within that image. xlate_rom_addr()
83 converts an image/offset address to an absolute offset from the
84 ROM's base address.
86 =====================================================================*/
88 static u_int xlate_rom_addr(void __iomem *b, u_int addr)
90 u_int img = 0, ofs = 0, sz;
91 u_short data;
92 while ((readb(b) == 0x55) && (readb(b + 1) == 0xaa)) {
93 if (img == (addr >> 28))
94 return (addr & 0x0fffffff) + ofs;
95 data = readb(b + ROM_DATA_PTR) + (readb(b + ROM_DATA_PTR + 1) << 8);
96 sz = 512 * (readb(b + data + PCDATA_IMAGE_SZ) +
97 (readb(b + data + PCDATA_IMAGE_SZ + 1) << 8));
98 if ((sz == 0) || (readb(b + data + PCDATA_INDICATOR) & 0x80))
99 break;
100 b += sz;
101 ofs += sz;
102 img++;
104 return 0;
107 /*=====================================================================
109 These are similar to setup_cis_mem and release_cis_mem for 16-bit
110 cards. The "result" that is used externally is the cb_cis_virt
111 pointer in the struct pcmcia_socket structure.
113 =====================================================================*/
115 static void cb_release_cis_mem(struct pcmcia_socket * s)
117 if (s->cb_cis_virt) {
118 cs_dbg(s, 1, "cb_release_cis_mem()\n");
119 iounmap(s->cb_cis_virt);
120 s->cb_cis_virt = NULL;
121 s->cb_cis_res = NULL;
125 static int cb_setup_cis_mem(struct pcmcia_socket * s, struct resource *res)
127 unsigned int start, size;
129 if (res == s->cb_cis_res)
130 return 0;
132 if (s->cb_cis_res)
133 cb_release_cis_mem(s);
135 start = res->start;
136 size = res->end - start + 1;
137 s->cb_cis_virt = ioremap(start, size);
139 if (!s->cb_cis_virt)
140 return -1;
142 s->cb_cis_res = res;
144 return 0;
147 /*=====================================================================
149 This is used by the CIS processing code to read CIS information
150 from a CardBus device.
152 =====================================================================*/
154 int read_cb_mem(struct pcmcia_socket * s, int space, u_int addr, u_int len, void *ptr)
156 struct pci_dev *dev;
157 struct resource *res;
159 cs_dbg(s, 3, "read_cb_mem(%d, %#x, %u)\n", space, addr, len);
161 dev = pci_find_slot(s->cb_dev->subordinate->number, 0);
162 if (!dev)
163 goto fail;
165 /* Config space? */
166 if (space == 0) {
167 if (addr + len > 0x100)
168 goto fail;
169 for (; len; addr++, ptr++, len--)
170 pci_read_config_byte(dev, addr, ptr);
171 return 0;
174 res = dev->resource + space - 1;
175 if (!res->flags)
176 goto fail;
178 if (cb_setup_cis_mem(s, res) != 0)
179 goto fail;
181 if (space == 7) {
182 addr = xlate_rom_addr(s->cb_cis_virt, addr);
183 if (addr == 0)
184 goto fail;
187 if (addr + len > res->end - res->start)
188 goto fail;
190 memcpy_fromio(ptr, s->cb_cis_virt + addr, len);
191 return 0;
193 fail:
194 memset(ptr, 0xff, len);
195 return -1;
198 /*=====================================================================
200 cb_alloc() and cb_free() allocate and free the kernel data
201 structures for a Cardbus device, and handle the lowest level PCI
202 device setup issues.
204 =====================================================================*/
207 * Since there is only one interrupt available to CardBus
208 * devices, all devices downstream of this device must
209 * be using this IRQ.
211 static void cardbus_assign_irqs(struct pci_bus *bus, int irq)
213 struct pci_dev *dev;
215 list_for_each_entry(dev, &bus->devices, bus_list) {
216 u8 irq_pin;
218 pci_read_config_byte(dev, PCI_INTERRUPT_PIN, &irq_pin);
219 if (irq_pin) {
220 dev->irq = irq;
221 pci_write_config_byte(dev, PCI_INTERRUPT_LINE, dev->irq);
224 if (dev->subordinate)
225 cardbus_assign_irqs(dev->subordinate, irq);
229 int cb_alloc(struct pcmcia_socket * s)
231 struct pci_bus *bus = s->cb_dev->subordinate;
232 struct pci_dev *dev;
233 unsigned int max, pass;
235 s->functions = pci_scan_slot(bus, PCI_DEVFN(0, 0));
236 // pcibios_fixup_bus(bus);
238 max = bus->secondary;
239 for (pass = 0; pass < 2; pass++)
240 list_for_each_entry(dev, &bus->devices, bus_list)
241 if (dev->hdr_type == PCI_HEADER_TYPE_BRIDGE ||
242 dev->hdr_type == PCI_HEADER_TYPE_CARDBUS)
243 max = pci_scan_bridge(bus, dev, max, pass);
246 * Size all resources below the CardBus controller.
248 pci_bus_size_bridges(bus);
249 pci_bus_assign_resources(bus);
250 cardbus_assign_irqs(bus, s->pci_irq);
251 pci_enable_bridges(bus);
252 pci_bus_add_devices(bus);
254 s->irq.AssignedIRQ = s->pci_irq;
255 return CS_SUCCESS;
258 void cb_free(struct pcmcia_socket * s)
260 struct pci_dev *bridge = s->cb_dev;
262 cb_release_cis_mem(s);
264 if (bridge)
265 pci_remove_behind_bridge(bridge);