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 if (flag)
107 set_bit(chunk, zorro_unused_z2ram);
108 else
109 clear_bit(chunk, zorro_unused_z2ram);
110 start += Z2RAM_CHUNKSIZE;
111 }
112 }
113
114
zorro_find_parent_resource(struct platform_device * bridge,struct zorro_dev * z)115 static struct resource __init *zorro_find_parent_resource(
116 struct platform_device *bridge, struct zorro_dev *z)
117 {
118 int i;
119
120 for (i = 0; i < bridge->num_resources; i++) {
121 if (resource_contains(&bridge->resource[i], &z->resource))
122 return &bridge->resource[i];
123 }
124
125 return &iomem_resource;
126 }
127
amiga_zorro_probe(struct platform_device * pdev)128 static int __init amiga_zorro_probe(struct platform_device *pdev)
129 {
130 struct zorro_bus *bus;
131 struct zorro_dev_init *zi;
132 struct zorro_dev *z;
133 struct resource *r;
134 unsigned int i;
135 int error;
136
137 /* Initialize the Zorro bus */
138 bus = kzalloc_flex(*bus, devices, zorro_num_autocon);
139 if (!bus)
140 return -ENOMEM;
141
142 zorro_autocon = bus->devices;
143 bus->dev.parent = &pdev->dev;
144 dev_set_name(&bus->dev, zorro_bus_type.name);
145 error = device_register(&bus->dev);
146 if (error) {
147 pr_err("Zorro: Error registering zorro_bus\n");
148 put_device(&bus->dev);
149 kfree(bus);
150 return error;
151 }
152 platform_set_drvdata(pdev, bus);
153
154 pr_info("Zorro: Probing AutoConfig expansion devices: %u device%s\n",
155 zorro_num_autocon, str_plural(zorro_num_autocon));
156
157 /* First identify all devices ... */
158 for (i = 0; i < zorro_num_autocon; i++) {
159 zi = &zorro_autocon_init[i];
160 z = &zorro_autocon[i];
161
162 z->rom = zi->rom;
163 z->id = (be16_to_cpu(z->rom.er_Manufacturer) << 16) |
164 (z->rom.er_Product << 8);
165 if (z->id == ZORRO_PROD_GVP_EPC_BASE) {
166 /* GVP quirk */
167 unsigned long magic = zi->boardaddr + 0x8000;
168
169 z->id |= *(u16 *)ZTWO_VADDR(magic) & GVP_PRODMASK;
170 }
171 z->slotaddr = zi->slotaddr;
172 z->slotsize = zi->slotsize;
173 sprintf(z->name, "Zorro device %08x", z->id);
174 zorro_name_device(z);
175 z->resource = DEFINE_RES_MEM_NAMED(zi->boardaddr, zi->boardsize, z->name);
176 r = zorro_find_parent_resource(pdev, z);
177 error = request_resource(r, &z->resource);
178 if (error && !(z->rom.er_Type & ERTF_MEMLIST))
179 dev_err(&bus->dev,
180 "Address space collision on device %s %pR\n",
181 z->name, &z->resource);
182 z->dev.parent = &bus->dev;
183 z->dev.bus = &zorro_bus_type;
184 z->dev.id = i;
185 switch (z->rom.er_Type & ERT_TYPEMASK) {
186 case ERT_ZORROIII:
187 z->dev.coherent_dma_mask = DMA_BIT_MASK(32);
188 break;
189
190 case ERT_ZORROII:
191 default:
192 z->dev.coherent_dma_mask = DMA_BIT_MASK(24);
193 break;
194 }
195 z->dev.dma_mask = &z->dev.coherent_dma_mask;
196 }
197
198 /* ... then register them */
199 for (i = 0; i < zorro_num_autocon; i++) {
200 z = &zorro_autocon[i];
201 error = device_register(&z->dev);
202 if (error) {
203 dev_err(&bus->dev, "Error registering device %s\n",
204 z->name);
205 put_device(&z->dev);
206 continue;
207 }
208 }
209
210 /* Mark all available Zorro II memory */
211 zorro_for_each_dev(z) {
212 if (z->rom.er_Type & ERTF_MEMLIST)
213 mark_region(zorro_resource_start(z),
214 zorro_resource_end(z)+1, 1);
215 }
216
217 /* Unmark all used Zorro II memory */
218 for (i = 0; i < m68k_num_memory; i++)
219 if (m68k_memory[i].addr < 16*1024*1024)
220 mark_region(m68k_memory[i].addr,
221 m68k_memory[i].addr+m68k_memory[i].size,
222 0);
223
224 return 0;
225 }
226
227 static struct platform_driver amiga_zorro_driver = {
228 .driver = {
229 .name = "amiga-zorro",
230 },
231 };
232
amiga_zorro_init(void)233 static int __init amiga_zorro_init(void)
234 {
235 return platform_driver_probe(&amiga_zorro_driver, amiga_zorro_probe);
236 }
237
238 module_init(amiga_zorro_init);
239
240 MODULE_LICENSE("GPL");
241