blob: 8ec79385d3cc44b92cccc22cfb7f3a7a03970554 [file] [log] [blame]
Linus Torvalds1da177e2005-04-16 15:20:36 -07001/*
Linus Torvalds1da177e2005-04-16 15:20:36 -07002 * 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>
Tim Schmielau4e57b682005-10-30 15:03:48 -080017#include <linux/string.h>
Geert Uytterhoeven0d305462009-04-05 12:40:41 +020018#include <linux/platform_device.h>
Michael Schmitzb19d6762018-03-03 12:04:13 +130019#include <linux/dma-mapping.h>
Geert Uytterhoeven0d305462009-04-05 12:40:41 +020020#include <linux/slab.h>
Tim Schmielau4e57b682005-10-30 15:03:48 -080021
Geert Uytterhoevenbd9ba8f2013-10-04 09:38:53 +020022#include <asm/byteorder.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070023#include <asm/setup.h>
24#include <asm/amigahw.h>
25
26#include "zorro.h"
27
28
29 /*
30 * Zorro Expansion Devices
31 */
32
Geert Uytterhoeven0d305462009-04-05 12:40:41 +020033unsigned int zorro_num_autocon;
Geert Uytterhoevenc2937382013-06-29 10:40:20 +020034struct zorro_dev_init zorro_autocon_init[ZORRO_NUM_AUTO] __initdata;
35struct zorro_dev *zorro_autocon;
Linus Torvalds1da177e2005-04-16 15:20:36 -070036
37
38 /*
Geert Uytterhoeven0d305462009-04-05 12:40:41 +020039 * Zorro bus
Linus Torvalds1da177e2005-04-16 15:20:36 -070040 */
41
Geert Uytterhoeven0d305462009-04-05 12:40:41 +020042struct zorro_bus {
Geert Uytterhoeven0d305462009-04-05 12:40:41 +020043 struct device dev;
Geert Uytterhoevenc2937382013-06-29 10:40:20 +020044 struct zorro_dev devices[0];
Linus Torvalds1da177e2005-04-16 15:20:36 -070045};
46
47
48 /*
49 * Find Zorro Devices
50 */
51
52struct zorro_dev *zorro_find_device(zorro_id id, struct zorro_dev *from)
53{
Geert Uytterhoeven0d305462009-04-05 12:40:41 +020054 struct zorro_dev *z;
Linus Torvalds1da177e2005-04-16 15:20:36 -070055
Geert Uytterhoeven0d305462009-04-05 12:40:41 +020056 if (!zorro_num_autocon)
57 return NULL;
58
59 for (z = from ? from+1 : &zorro_autocon[0];
60 z < zorro_autocon+zorro_num_autocon;
61 z++)
62 if (id == ZORRO_WILDCARD || id == z->id)
63 return z;
Linus Torvalds1da177e2005-04-16 15:20:36 -070064 return NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -070065}
Geert Uytterhoeven0d305462009-04-05 12:40:41 +020066EXPORT_SYMBOL(zorro_find_device);
Linus Torvalds1da177e2005-04-16 15:20:36 -070067
68
69 /*
70 * Bitmask indicating portions of available Zorro II RAM that are unused
71 * by the system. Every bit represents a 64K chunk, for a maximum of 8MB
72 * (128 chunks, physical 0x00200000-0x009fffff).
73 *
74 * If you want to use (= allocate) portions of this RAM, you should clear
75 * the corresponding bits.
76 *
77 * Possible uses:
78 * - z2ram device
79 * - SCSI DMA bounce buffers
80 *
81 * FIXME: use the normal resource management
82 */
83
84DECLARE_BITMAP(zorro_unused_z2ram, 128);
Geert Uytterhoeven0d305462009-04-05 12:40:41 +020085EXPORT_SYMBOL(zorro_unused_z2ram);
Linus Torvalds1da177e2005-04-16 15:20:36 -070086
87
88static void __init mark_region(unsigned long start, unsigned long end,
89 int flag)
90{
Linus Torvalds1da177e2005-04-16 15:20:36 -070091 if (flag)
Geert Uytterhoeven0d305462009-04-05 12:40:41 +020092 start += Z2RAM_CHUNKMASK;
Linus Torvalds1da177e2005-04-16 15:20:36 -070093 else
Geert Uytterhoeven0d305462009-04-05 12:40:41 +020094 end += Z2RAM_CHUNKMASK;
95 start &= ~Z2RAM_CHUNKMASK;
96 end &= ~Z2RAM_CHUNKMASK;
Linus Torvalds1da177e2005-04-16 15:20:36 -070097
Geert Uytterhoeven0d305462009-04-05 12:40:41 +020098 if (end <= Z2RAM_START || start >= Z2RAM_END)
99 return;
100 start = start < Z2RAM_START ? 0x00000000 : start-Z2RAM_START;
101 end = end > Z2RAM_END ? Z2RAM_SIZE : end-Z2RAM_START;
102 while (start < end) {
103 u32 chunk = start>>Z2RAM_CHUNKSHIFT;
104 if (flag)
105 set_bit(chunk, zorro_unused_z2ram);
106 else
107 clear_bit(chunk, zorro_unused_z2ram);
108 start += Z2RAM_CHUNKSIZE;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700109 }
Geert Uytterhoeven0d305462009-04-05 12:40:41 +0200110}
111
112
113static struct resource __init *zorro_find_parent_resource(
114 struct platform_device *bridge, struct zorro_dev *z)
115{
116 int i;
117
118 for (i = 0; i < bridge->num_resources; i++) {
119 struct resource *r = &bridge->resource[i];
120 if (zorro_resource_start(z) >= r->start &&
121 zorro_resource_end(z) <= r->end)
122 return r;
123 }
124 return &iomem_resource;
125}
126
127
128
129static int __init amiga_zorro_probe(struct platform_device *pdev)
130{
131 struct zorro_bus *bus;
Geert Uytterhoevenc2937382013-06-29 10:40:20 +0200132 struct zorro_dev_init *zi;
Geert Uytterhoeven0d305462009-04-05 12:40:41 +0200133 struct zorro_dev *z;
134 struct resource *r;
135 unsigned int i;
136 int error;
137
138 /* Initialize the Zorro bus */
Geert Uytterhoevenc2937382013-06-29 10:40:20 +0200139 bus = kzalloc(sizeof(*bus) +
140 zorro_num_autocon * sizeof(bus->devices[0]),
141 GFP_KERNEL);
Geert Uytterhoeven0d305462009-04-05 12:40:41 +0200142 if (!bus)
143 return -ENOMEM;
144
Geert Uytterhoevenc2937382013-06-29 10:40:20 +0200145 zorro_autocon = bus->devices;
Geert Uytterhoeven0d305462009-04-05 12:40:41 +0200146 bus->dev.parent = &pdev->dev;
Geert Uytterhoeven83b7bce2013-10-16 11:28:54 +0200147 dev_set_name(&bus->dev, zorro_bus_type.name);
Geert Uytterhoeven0d305462009-04-05 12:40:41 +0200148 error = device_register(&bus->dev);
Geert Uytterhoeven11a8b2c2008-12-30 14:21:19 +0100149 if (error) {
Geert Uytterhoeven0d305462009-04-05 12:40:41 +0200150 pr_err("Zorro: Error registering zorro_bus\n");
Vasiliy Kulikov10b68792010-09-19 16:55:21 +0400151 put_device(&bus->dev);
Geert Uytterhoeven0d305462009-04-05 12:40:41 +0200152 kfree(bus);
153 return error;
Geert Uytterhoeven11a8b2c2008-12-30 14:21:19 +0100154 }
Geert Uytterhoeven0d305462009-04-05 12:40:41 +0200155 platform_set_drvdata(pdev, bus);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700156
Geert Uytterhoeven0d305462009-04-05 12:40:41 +0200157 pr_info("Zorro: Probing AutoConfig expansion devices: %u device%s\n",
158 zorro_num_autocon, zorro_num_autocon == 1 ? "" : "s");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700159
Geert Uytterhoevena7f4d002011-09-22 21:47:38 +0200160 /* First identify all devices ... */
Geert Uytterhoeven0d305462009-04-05 12:40:41 +0200161 for (i = 0; i < zorro_num_autocon; i++) {
Geert Uytterhoevenc2937382013-06-29 10:40:20 +0200162 zi = &zorro_autocon_init[i];
Geert Uytterhoeven0d305462009-04-05 12:40:41 +0200163 z = &zorro_autocon[i];
Geert Uytterhoevenc2937382013-06-29 10:40:20 +0200164
165 z->rom = zi->rom;
Geert Uytterhoevenbd9ba8f2013-10-04 09:38:53 +0200166 z->id = (be16_to_cpu(z->rom.er_Manufacturer) << 16) |
167 (z->rom.er_Product << 8);
Geert Uytterhoeven0d305462009-04-05 12:40:41 +0200168 if (z->id == ZORRO_PROD_GVP_EPC_BASE) {
169 /* GVP quirk */
Geert Uytterhoevenc2937382013-06-29 10:40:20 +0200170 unsigned long magic = zi->boardaddr + 0x8000;
Geert Uytterhoeven0d305462009-04-05 12:40:41 +0200171 z->id |= *(u16 *)ZTWO_VADDR(magic) & GVP_PRODMASK;
172 }
Geert Uytterhoevenc2937382013-06-29 10:40:20 +0200173 z->slotaddr = zi->slotaddr;
174 z->slotsize = zi->slotsize;
Geert Uytterhoeven0d305462009-04-05 12:40:41 +0200175 sprintf(z->name, "Zorro device %08x", z->id);
176 zorro_name_device(z);
Geert Uytterhoevenc2937382013-06-29 10:40:20 +0200177 z->resource.start = zi->boardaddr;
178 z->resource.end = zi->boardaddr + zi->boardsize - 1;
Geert Uytterhoeven0d305462009-04-05 12:40:41 +0200179 z->resource.name = z->name;
180 r = zorro_find_parent_resource(pdev, z);
181 error = request_resource(r, &z->resource);
182 if (error)
183 dev_err(&bus->dev,
184 "Address space collision on device %s %pR\n",
185 z->name, &z->resource);
Geert Uytterhoeven0d305462009-04-05 12:40:41 +0200186 z->dev.parent = &bus->dev;
187 z->dev.bus = &zorro_bus_type;
Geert Uytterhoeven83b7bce2013-10-16 11:28:54 +0200188 z->dev.id = i;
Michael Schmitzb19d6762018-03-03 12:04:13 +1300189 switch (z->rom.er_Type & ERT_TYPEMASK) {
190 case ERT_ZORROIII:
191 z->dev.coherent_dma_mask = DMA_BIT_MASK(32);
192 break;
193
194 case ERT_ZORROII:
195 default:
196 z->dev.coherent_dma_mask = DMA_BIT_MASK(24);
197 break;
198 }
199 z->dev.dma_mask = &z->dev.coherent_dma_mask;
Geert Uytterhoevena7f4d002011-09-22 21:47:38 +0200200 }
201
202 /* ... then register them */
203 for (i = 0; i < zorro_num_autocon; i++) {
204 z = &zorro_autocon[i];
Geert Uytterhoeven0d305462009-04-05 12:40:41 +0200205 error = device_register(&z->dev);
206 if (error) {
207 dev_err(&bus->dev, "Error registering device %s\n",
208 z->name);
Vasiliy Kulikov10b68792010-09-19 16:55:21 +0400209 put_device(&z->dev);
Geert Uytterhoeven0d305462009-04-05 12:40:41 +0200210 continue;
211 }
212 error = zorro_create_sysfs_dev_files(z);
213 if (error)
214 dev_err(&z->dev, "Error creating sysfs files\n");
215 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700216
Geert Uytterhoeven0d305462009-04-05 12:40:41 +0200217 /* Mark all available Zorro II memory */
218 zorro_for_each_dev(z) {
219 if (z->rom.er_Type & ERTF_MEMLIST)
220 mark_region(zorro_resource_start(z),
221 zorro_resource_end(z)+1, 1);
222 }
223
224 /* Unmark all used Zorro II memory */
225 for (i = 0; i < m68k_num_memory; i++)
226 if (m68k_memory[i].addr < 16*1024*1024)
227 mark_region(m68k_memory[i].addr,
228 m68k_memory[i].addr+m68k_memory[i].size,
229 0);
230
231 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700232}
233
Geert Uytterhoeven0d305462009-04-05 12:40:41 +0200234static struct platform_driver amiga_zorro_driver = {
235 .driver = {
236 .name = "amiga-zorro",
Geert Uytterhoeven0d305462009-04-05 12:40:41 +0200237 },
238};
Linus Torvalds1da177e2005-04-16 15:20:36 -0700239
Geert Uytterhoeven0d305462009-04-05 12:40:41 +0200240static int __init amiga_zorro_init(void)
241{
242 return platform_driver_probe(&amiga_zorro_driver, amiga_zorro_probe);
243}
244
245module_init(amiga_zorro_init);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700246
247MODULE_LICENSE("GPL");