1 /*
2 * Zorro Bus Services
3 *
4 * Copyright (C) 1995-2003 Geert Uytterhoeven
5 *
6 * This file is subject to the terms and conditions of the GNU General Public
7 * License. See the file COPYING in the main directory of this archive
8 * for more details.
9 */
10
11 #include <linux/module.h>
12 #include <linux/types.h>
13 #include <linux/kernel.h>
14 #include <linux/init.h>
15 #include <linux/zorro.h>
16 #include <linux/bitops.h>
17 #include <linux/string.h>
18 #include <linux/platform_device.h>
19 #include <linux/dma-mapping.h>
20 #include <linux/slab.h>
21 #include <linux/string_choices.h>
22
23 #include <asm/byteorder.h>
24 #include <asm/setup.h>
25 #include <asm/amigahw.h>
26
27 #include "zorro.h"
28
29
30 /*
31 * Zorro Expansion Devices
32 */
33
34 unsigned int zorro_num_autocon;
35 struct zorro_dev_init zorro_autocon_init[ZORRO_NUM_AUTO] __initdata;
36 struct zorro_dev *zorro_autocon;
37
38
39 /*
40 * Zorro bus
41 */
42
43 struct zorro_bus {
44 struct device dev;
45 struct zorro_dev devices[];
46 };
47
48
49 /*
50 * Find Zorro Devices
51 */
52
zorro_find_device(zorro_id id,struct zorro_dev * from)53 struct zorro_dev *zorro_find_device(zorro_id id, struct zorro_dev *from)
54 {
55 struct zorro_dev *z;
56
57 if (!zorro_num_autocon)
58 return NULL;
59
60 for (z = from ? from+1 : &zorro_autocon[0];
61 z < zorro_autocon+zorro_num_autocon;
62 z++)
63 if (id == ZORRO_WILDCARD || id == z->id)
64 return z;
65 return NULL;
66 }
67 EXPORT_SYMBOL(zorro_find_device);
68
69
70 /*
71 * Bitmask indicating portions of available Zorro II RAM that are unused
72 * by the system. Every bit represents a 64K chunk, for a maximum of 8MB
73 * (128 chunks, physical 0x00200000-0x009fffff).
74 *
75 * If you want to use (= allocate) portions of this RAM, you should clear
76 * the corresponding bits.
77 *
78 * Possible uses:
79 * - z2ram device
80 * - SCSI DMA bounce buffers
81 *
82 * FIXME: use the normal resource management
83 */
84
85 DECLARE_BITMAP(zorro_unused_z2ram, 128);
86 EXPORT_SYMBOL(zorro_unused_z2ram);
87
88
mark_region(unsigned long start,unsigned long end,int flag)89 static void __init mark_region(unsigned long start, unsigned long end,
90 int flag)
91 {
92 if (flag)
93 start += Z2RAM_CHUNKMASK;
94 else
95 end += Z2RAM_CHUNKMASK;
96 start &= ~Z2RAM_CHUNKMASK;
97 end &= ~Z2RAM_CHUNKMASK;
98
99 if (end <= Z2RAM_START || start >= Z2RAM_END)
100 return;
101 start = start < Z2RAM_START ? 0x00000000 : start-Z2RAM_START;
102 end = end > Z2RAM_END ? Z2RAM_SIZE : end-Z2RAM_START;
103 while (start < end) {
104 u32 chunk = start>>Z2RAM_CHUNKSHIFT;
105
106 assign_bit(chunk, zorro_unused_z2ram, flag);
107 start += Z2RAM_CHUNKSIZE;
108 }
109 }
110
111
zorro_find_parent_resource(struct platform_device * bridge,struct zorro_dev * z)112 static struct resource __init *zorro_find_parent_resource(
113 struct platform_device *bridge, struct zorro_dev *z)
114 {
115 int i;
116
117 for (i = 0; i < bridge->num_resources; i++) {
118 if (resource_contains(&bridge->resource[i], &z->resource))
119 return &bridge->resource[i];
120 }
121
122 return &iomem_resource;
123 }
124
amiga_zorro_probe(struct platform_device * pdev)125 static int __init amiga_zorro_probe(struct platform_device *pdev)
126 {
127 struct zorro_bus *bus;
128 struct zorro_dev_init *zi;
129 struct zorro_dev *z;
130 struct resource *r;
131 unsigned int i;
132 int error;
133
134 /* Initialize the Zorro bus */
135 bus = kzalloc_flex(*bus, devices, zorro_num_autocon);
136 if (!bus)
137 return -ENOMEM;
138
139 zorro_autocon = bus->devices;
140 bus->dev.parent = &pdev->dev;
141 dev_set_name(&bus->dev, zorro_bus_type.name);
142 error = device_register(&bus->dev);
143 if (error) {
144 pr_err("Zorro: Error registering zorro_bus\n");
145 put_device(&bus->dev);
146 kfree(bus);
147 return error;
148 }
149 platform_set_drvdata(pdev, bus);
150
151 pr_info("Zorro: Probing AutoConfig expansion devices: %u device%s\n",
152 zorro_num_autocon, str_plural(zorro_num_autocon));
153
154 /* First identify all devices ... */
155 for (i = 0; i < zorro_num_autocon; i++) {
156 zi = &zorro_autocon_init[i];
157 z = &zorro_autocon[i];
158
159 z->rom = zi->rom;
160 z->id = (be16_to_cpu(z->rom.er_Manufacturer) << 16) |
161 (z->rom.er_Product << 8);
162 if (z->id == ZORRO_PROD_GVP_EPC_BASE) {
163 /* GVP quirk */
164 unsigned long magic = zi->boardaddr + 0x8000;
165
166 z->id |= *(u16 *)ZTWO_VADDR(magic) & GVP_PRODMASK;
167 }
168 z->slotaddr = zi->slotaddr;
169 z->slotsize = zi->slotsize;
170 sprintf(z->name, "Zorro device %08x", z->id);
171 zorro_name_device(z);
172 z->resource = DEFINE_RES_MEM_NAMED(zi->boardaddr, zi->boardsize, z->name);
173 r = zorro_find_parent_resource(pdev, z);
174 error = request_resource(r, &z->resource);
175 if (error && !(z->rom.er_Type & ERTF_MEMLIST))
176 dev_err(&bus->dev,
177 "Address space collision on device %s %pR\n",
178 z->name, &z->resource);
179 z->dev.parent = &bus->dev;
180 z->dev.bus = &zorro_bus_type;
181 z->dev.id = i;
182 switch (z->rom.er_Type & ERT_TYPEMASK) {
183 case ERT_ZORROIII:
184 z->dev.coherent_dma_mask = DMA_BIT_MASK(32);
185 break;
186
187 case ERT_ZORROII:
188 default:
189 z->dev.coherent_dma_mask = DMA_BIT_MASK(24);
190 break;
191 }
192 z->dev.dma_mask = &z->dev.coherent_dma_mask;
193 }
194
195 /* ... then register them */
196 for (i = 0; i < zorro_num_autocon; i++) {
197 z = &zorro_autocon[i];
198 error = device_register(&z->dev);
199 if (error) {
200 dev_err(&bus->dev, "Error registering device %s\n",
201 z->name);
202 put_device(&z->dev);
203 continue;
204 }
205 }
206
207 /* Mark all available Zorro II memory */
208 zorro_for_each_dev(z) {
209 if (z->rom.er_Type & ERTF_MEMLIST)
210 mark_region(zorro_resource_start(z),
211 zorro_resource_end(z)+1, 1);
212 }
213
214 /* Unmark all used Zorro II memory */
215 for (i = 0; i < m68k_num_memory; i++)
216 if (m68k_memory[i].addr < 16*1024*1024)
217 mark_region(m68k_memory[i].addr,
218 m68k_memory[i].addr+m68k_memory[i].size,
219 0);
220
221 return 0;
222 }
223
224 static struct platform_driver amiga_zorro_driver = {
225 .driver = {
226 .name = "amiga-zorro",
227 },
228 };
229
amiga_zorro_init(void)230 static int __init amiga_zorro_init(void)
231 {
232 return platform_driver_probe(&amiga_zorro_driver, amiga_zorro_probe);
233 }
234
235 module_init(amiga_zorro_init);
236
237 MODULE_LICENSE("GPL");
238