Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1 | /* |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 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> |
Tim Schmielau | 4e57b68 | 2005-10-30 15:03:48 -0800 | [diff] [blame] | 17 | #include <linux/string.h> |
Geert Uytterhoeven | 0d30546 | 2009-04-05 12:40:41 +0200 | [diff] [blame] | 18 | #include <linux/platform_device.h> |
Michael Schmitz | 55496d3 | 2018-03-03 12:04:13 +1300 | [diff] [blame] | 19 | #include <linux/dma-mapping.h> |
Geert Uytterhoeven | 0d30546 | 2009-04-05 12:40:41 +0200 | [diff] [blame] | 20 | #include <linux/slab.h> |
Tim Schmielau | 4e57b68 | 2005-10-30 15:03:48 -0800 | [diff] [blame] | 21 | |
Geert Uytterhoeven | bd9ba8f | 2013-10-04 09:38:53 +0200 | [diff] [blame] | 22 | #include <asm/byteorder.h> |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 23 | #include <asm/setup.h> |
| 24 | #include <asm/amigahw.h> |
| 25 | |
| 26 | #include "zorro.h" |
| 27 | |
| 28 | |
| 29 | /* |
| 30 | * Zorro Expansion Devices |
| 31 | */ |
| 32 | |
Geert Uytterhoeven | 0d30546 | 2009-04-05 12:40:41 +0200 | [diff] [blame] | 33 | unsigned int zorro_num_autocon; |
Geert Uytterhoeven | c293738 | 2013-06-29 10:40:20 +0200 | [diff] [blame] | 34 | struct zorro_dev_init zorro_autocon_init[ZORRO_NUM_AUTO] __initdata; |
| 35 | struct zorro_dev *zorro_autocon; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 36 | |
| 37 | |
| 38 | /* |
Geert Uytterhoeven | 0d30546 | 2009-04-05 12:40:41 +0200 | [diff] [blame] | 39 | * Zorro bus |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 40 | */ |
| 41 | |
Geert Uytterhoeven | 0d30546 | 2009-04-05 12:40:41 +0200 | [diff] [blame] | 42 | struct zorro_bus { |
Geert Uytterhoeven | 0d30546 | 2009-04-05 12:40:41 +0200 | [diff] [blame] | 43 | struct device dev; |
Gustavo A. R. Silva | 573a73a | 2020-02-27 12:37:48 -0600 | [diff] [blame] | 44 | struct zorro_dev devices[]; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 45 | }; |
| 46 | |
| 47 | |
| 48 | /* |
| 49 | * Find Zorro Devices |
| 50 | */ |
| 51 | |
| 52 | struct zorro_dev *zorro_find_device(zorro_id id, struct zorro_dev *from) |
| 53 | { |
Geert Uytterhoeven | 0d30546 | 2009-04-05 12:40:41 +0200 | [diff] [blame] | 54 | struct zorro_dev *z; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 55 | |
Geert Uytterhoeven | 0d30546 | 2009-04-05 12:40:41 +0200 | [diff] [blame] | 56 | 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 Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 64 | return NULL; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 65 | } |
Geert Uytterhoeven | 0d30546 | 2009-04-05 12:40:41 +0200 | [diff] [blame] | 66 | EXPORT_SYMBOL(zorro_find_device); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 67 | |
| 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 | |
| 84 | DECLARE_BITMAP(zorro_unused_z2ram, 128); |
Geert Uytterhoeven | 0d30546 | 2009-04-05 12:40:41 +0200 | [diff] [blame] | 85 | EXPORT_SYMBOL(zorro_unused_z2ram); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 86 | |
| 87 | |
| 88 | static void __init mark_region(unsigned long start, unsigned long end, |
| 89 | int flag) |
| 90 | { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 91 | if (flag) |
Geert Uytterhoeven | 0d30546 | 2009-04-05 12:40:41 +0200 | [diff] [blame] | 92 | start += Z2RAM_CHUNKMASK; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 93 | else |
Geert Uytterhoeven | 0d30546 | 2009-04-05 12:40:41 +0200 | [diff] [blame] | 94 | end += Z2RAM_CHUNKMASK; |
| 95 | start &= ~Z2RAM_CHUNKMASK; |
| 96 | end &= ~Z2RAM_CHUNKMASK; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 97 | |
Geert Uytterhoeven | 0d30546 | 2009-04-05 12:40:41 +0200 | [diff] [blame] | 98 | 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; |
Jacob | d49cbe7 | 2018-05-12 23:11:40 -0400 | [diff] [blame] | 104 | |
Geert Uytterhoeven | 0d30546 | 2009-04-05 12:40:41 +0200 | [diff] [blame] | 105 | if (flag) |
| 106 | set_bit(chunk, zorro_unused_z2ram); |
| 107 | else |
| 108 | clear_bit(chunk, zorro_unused_z2ram); |
| 109 | start += Z2RAM_CHUNKSIZE; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 110 | } |
Geert Uytterhoeven | 0d30546 | 2009-04-05 12:40:41 +0200 | [diff] [blame] | 111 | } |
| 112 | |
| 113 | |
| 114 | static struct resource __init *zorro_find_parent_resource( |
| 115 | struct platform_device *bridge, struct zorro_dev *z) |
| 116 | { |
| 117 | int i; |
| 118 | |
| 119 | for (i = 0; i < bridge->num_resources; i++) { |
| 120 | struct resource *r = &bridge->resource[i]; |
Jacob | d49cbe7 | 2018-05-12 23:11:40 -0400 | [diff] [blame] | 121 | |
Geert Uytterhoeven | 0d30546 | 2009-04-05 12:40:41 +0200 | [diff] [blame] | 122 | if (zorro_resource_start(z) >= r->start && |
| 123 | zorro_resource_end(z) <= r->end) |
| 124 | return r; |
| 125 | } |
| 126 | return &iomem_resource; |
| 127 | } |
| 128 | |
| 129 | |
| 130 | |
| 131 | static int __init amiga_zorro_probe(struct platform_device *pdev) |
| 132 | { |
| 133 | struct zorro_bus *bus; |
Geert Uytterhoeven | c293738 | 2013-06-29 10:40:20 +0200 | [diff] [blame] | 134 | struct zorro_dev_init *zi; |
Geert Uytterhoeven | 0d30546 | 2009-04-05 12:40:41 +0200 | [diff] [blame] | 135 | struct zorro_dev *z; |
| 136 | struct resource *r; |
| 137 | unsigned int i; |
| 138 | int error; |
| 139 | |
| 140 | /* Initialize the Zorro bus */ |
Kees Cook | acafe7e | 2018-05-08 13:45:50 -0700 | [diff] [blame] | 141 | bus = kzalloc(struct_size(bus, devices, zorro_num_autocon), |
Geert Uytterhoeven | c293738 | 2013-06-29 10:40:20 +0200 | [diff] [blame] | 142 | GFP_KERNEL); |
Geert Uytterhoeven | 0d30546 | 2009-04-05 12:40:41 +0200 | [diff] [blame] | 143 | if (!bus) |
| 144 | return -ENOMEM; |
| 145 | |
Geert Uytterhoeven | c293738 | 2013-06-29 10:40:20 +0200 | [diff] [blame] | 146 | zorro_autocon = bus->devices; |
Geert Uytterhoeven | 0d30546 | 2009-04-05 12:40:41 +0200 | [diff] [blame] | 147 | bus->dev.parent = &pdev->dev; |
Geert Uytterhoeven | 83b7bce | 2013-10-16 11:28:54 +0200 | [diff] [blame] | 148 | dev_set_name(&bus->dev, zorro_bus_type.name); |
Geert Uytterhoeven | 0d30546 | 2009-04-05 12:40:41 +0200 | [diff] [blame] | 149 | error = device_register(&bus->dev); |
Geert Uytterhoeven | 11a8b2c | 2008-12-30 14:21:19 +0100 | [diff] [blame] | 150 | if (error) { |
Geert Uytterhoeven | 0d30546 | 2009-04-05 12:40:41 +0200 | [diff] [blame] | 151 | pr_err("Zorro: Error registering zorro_bus\n"); |
Vasiliy Kulikov | 10b6879 | 2010-09-19 16:55:21 +0400 | [diff] [blame] | 152 | put_device(&bus->dev); |
Geert Uytterhoeven | 0d30546 | 2009-04-05 12:40:41 +0200 | [diff] [blame] | 153 | kfree(bus); |
| 154 | return error; |
Geert Uytterhoeven | 11a8b2c | 2008-12-30 14:21:19 +0100 | [diff] [blame] | 155 | } |
Geert Uytterhoeven | 0d30546 | 2009-04-05 12:40:41 +0200 | [diff] [blame] | 156 | platform_set_drvdata(pdev, bus); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 157 | |
Geert Uytterhoeven | 0d30546 | 2009-04-05 12:40:41 +0200 | [diff] [blame] | 158 | pr_info("Zorro: Probing AutoConfig expansion devices: %u device%s\n", |
| 159 | zorro_num_autocon, zorro_num_autocon == 1 ? "" : "s"); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 160 | |
Geert Uytterhoeven | a7f4d00 | 2011-09-22 21:47:38 +0200 | [diff] [blame] | 161 | /* First identify all devices ... */ |
Geert Uytterhoeven | 0d30546 | 2009-04-05 12:40:41 +0200 | [diff] [blame] | 162 | for (i = 0; i < zorro_num_autocon; i++) { |
Geert Uytterhoeven | c293738 | 2013-06-29 10:40:20 +0200 | [diff] [blame] | 163 | zi = &zorro_autocon_init[i]; |
Geert Uytterhoeven | 0d30546 | 2009-04-05 12:40:41 +0200 | [diff] [blame] | 164 | z = &zorro_autocon[i]; |
Geert Uytterhoeven | c293738 | 2013-06-29 10:40:20 +0200 | [diff] [blame] | 165 | |
| 166 | z->rom = zi->rom; |
Geert Uytterhoeven | bd9ba8f | 2013-10-04 09:38:53 +0200 | [diff] [blame] | 167 | z->id = (be16_to_cpu(z->rom.er_Manufacturer) << 16) | |
| 168 | (z->rom.er_Product << 8); |
Geert Uytterhoeven | 0d30546 | 2009-04-05 12:40:41 +0200 | [diff] [blame] | 169 | if (z->id == ZORRO_PROD_GVP_EPC_BASE) { |
| 170 | /* GVP quirk */ |
Geert Uytterhoeven | c293738 | 2013-06-29 10:40:20 +0200 | [diff] [blame] | 171 | unsigned long magic = zi->boardaddr + 0x8000; |
Jacob | d49cbe7 | 2018-05-12 23:11:40 -0400 | [diff] [blame] | 172 | |
Geert Uytterhoeven | 0d30546 | 2009-04-05 12:40:41 +0200 | [diff] [blame] | 173 | z->id |= *(u16 *)ZTWO_VADDR(magic) & GVP_PRODMASK; |
| 174 | } |
Geert Uytterhoeven | c293738 | 2013-06-29 10:40:20 +0200 | [diff] [blame] | 175 | z->slotaddr = zi->slotaddr; |
| 176 | z->slotsize = zi->slotsize; |
Geert Uytterhoeven | 0d30546 | 2009-04-05 12:40:41 +0200 | [diff] [blame] | 177 | sprintf(z->name, "Zorro device %08x", z->id); |
| 178 | zorro_name_device(z); |
Geert Uytterhoeven | c293738 | 2013-06-29 10:40:20 +0200 | [diff] [blame] | 179 | z->resource.start = zi->boardaddr; |
| 180 | z->resource.end = zi->boardaddr + zi->boardsize - 1; |
Geert Uytterhoeven | 0d30546 | 2009-04-05 12:40:41 +0200 | [diff] [blame] | 181 | z->resource.name = z->name; |
| 182 | r = zorro_find_parent_resource(pdev, z); |
| 183 | error = request_resource(r, &z->resource); |
Geert Uytterhoeven | ea2abe2 | 2020-07-13 09:24:29 +0200 | [diff] [blame] | 184 | if (error && !(z->rom.er_Type & ERTF_MEMLIST)) |
Geert Uytterhoeven | 0d30546 | 2009-04-05 12:40:41 +0200 | [diff] [blame] | 185 | dev_err(&bus->dev, |
| 186 | "Address space collision on device %s %pR\n", |
| 187 | z->name, &z->resource); |
Geert Uytterhoeven | 0d30546 | 2009-04-05 12:40:41 +0200 | [diff] [blame] | 188 | z->dev.parent = &bus->dev; |
| 189 | z->dev.bus = &zorro_bus_type; |
Geert Uytterhoeven | 83b7bce | 2013-10-16 11:28:54 +0200 | [diff] [blame] | 190 | z->dev.id = i; |
Michael Schmitz | 55496d3 | 2018-03-03 12:04:13 +1300 | [diff] [blame] | 191 | switch (z->rom.er_Type & ERT_TYPEMASK) { |
| 192 | case ERT_ZORROIII: |
| 193 | z->dev.coherent_dma_mask = DMA_BIT_MASK(32); |
| 194 | break; |
| 195 | |
| 196 | case ERT_ZORROII: |
| 197 | default: |
| 198 | z->dev.coherent_dma_mask = DMA_BIT_MASK(24); |
| 199 | break; |
| 200 | } |
| 201 | z->dev.dma_mask = &z->dev.coherent_dma_mask; |
Geert Uytterhoeven | a7f4d00 | 2011-09-22 21:47:38 +0200 | [diff] [blame] | 202 | } |
| 203 | |
| 204 | /* ... then register them */ |
| 205 | for (i = 0; i < zorro_num_autocon; i++) { |
| 206 | z = &zorro_autocon[i]; |
Geert Uytterhoeven | 0d30546 | 2009-04-05 12:40:41 +0200 | [diff] [blame] | 207 | error = device_register(&z->dev); |
| 208 | if (error) { |
| 209 | dev_err(&bus->dev, "Error registering device %s\n", |
| 210 | z->name); |
Vasiliy Kulikov | 10b6879 | 2010-09-19 16:55:21 +0400 | [diff] [blame] | 211 | put_device(&z->dev); |
Geert Uytterhoeven | 0d30546 | 2009-04-05 12:40:41 +0200 | [diff] [blame] | 212 | continue; |
| 213 | } |
Geert Uytterhoeven | 0d30546 | 2009-04-05 12:40:41 +0200 | [diff] [blame] | 214 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 215 | |
Geert Uytterhoeven | 0d30546 | 2009-04-05 12:40:41 +0200 | [diff] [blame] | 216 | /* Mark all available Zorro II memory */ |
| 217 | zorro_for_each_dev(z) { |
| 218 | if (z->rom.er_Type & ERTF_MEMLIST) |
| 219 | mark_region(zorro_resource_start(z), |
| 220 | zorro_resource_end(z)+1, 1); |
| 221 | } |
| 222 | |
| 223 | /* Unmark all used Zorro II memory */ |
| 224 | for (i = 0; i < m68k_num_memory; i++) |
| 225 | if (m68k_memory[i].addr < 16*1024*1024) |
| 226 | mark_region(m68k_memory[i].addr, |
| 227 | m68k_memory[i].addr+m68k_memory[i].size, |
| 228 | 0); |
| 229 | |
| 230 | return 0; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 231 | } |
| 232 | |
Geert Uytterhoeven | 0d30546 | 2009-04-05 12:40:41 +0200 | [diff] [blame] | 233 | static struct platform_driver amiga_zorro_driver = { |
| 234 | .driver = { |
| 235 | .name = "amiga-zorro", |
Geert Uytterhoeven | 0d30546 | 2009-04-05 12:40:41 +0200 | [diff] [blame] | 236 | }, |
| 237 | }; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 238 | |
Geert Uytterhoeven | 0d30546 | 2009-04-05 12:40:41 +0200 | [diff] [blame] | 239 | static int __init amiga_zorro_init(void) |
| 240 | { |
| 241 | return platform_driver_probe(&amiga_zorro_driver, amiga_zorro_probe); |
| 242 | } |
| 243 | |
| 244 | module_init(amiga_zorro_init); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 245 | |
| 246 | MODULE_LICENSE("GPL"); |