1 /*
2  *    $Id: zorro.c,v 1.1.2.1 1998/06/07 23:21:02 geert Exp $
3  *
4  *    Zorro Bus Services
5  *
6  *    Copyright (C) 1995-2000 Geert Uytterhoeven
7  *
8  *    This file is subject to the terms and conditions of the GNU General Public
9  *    License.  See the file COPYING in the main directory of this archive
10  *    for more details.
11  */
12 
13 #include <linux/module.h>
14 #include <linux/types.h>
15 #include <linux/kernel.h>
16 #include <linux/init.h>
17 #include <linux/zorro.h>
18 #include <asm/setup.h>
19 #include <asm/bitops.h>
20 #include <asm/amigahw.h>
21 
22 
23     /*
24      *  Zorro Expansion Devices
25      */
26 
27 u_int zorro_num_autocon = 0;
28 struct zorro_dev zorro_autocon[ZORRO_NUM_AUTO];
29 
30 
31     /*
32      *  Zorro Bus Resources
33      *  Order _does_ matter! (see code below)
34      */
35 
36 static struct resource zorro_res[4] = {
37     /* Zorro II regions (on Zorro II/III) */
38     { "Zorro II exp", 0x00e80000, 0x00efffff },
39     { "Zorro II mem", 0x00200000, 0x009fffff },
40     /* Zorro III regions (on Zorro III only) */
41     { "Zorro III exp", 0xff000000, 0xffffffff },
42     { "Zorro III cfg", 0x40000000, 0x7fffffff }
43 };
44 
45 static u_int zorro_num_res __initdata = 0;
46 
47 
48     /*
49      *  Find Zorro Devices
50      */
51 
zorro_find_device(zorro_id id,struct zorro_dev * from)52 struct zorro_dev *zorro_find_device(zorro_id id, struct zorro_dev *from)
53 {
54     struct zorro_dev *dev;
55 
56     if (!MACH_IS_AMIGA || !AMIGAHW_PRESENT(ZORRO))
57 	return NULL;
58 
59     for (dev = from ? from+1 : &zorro_autocon[0];
60 	 dev < zorro_autocon+zorro_num_autocon;
61 	 dev++)
62 	if (id == ZORRO_WILDCARD || id == dev->id)
63 	    return dev;
64     return NULL;
65 }
66 
67 
68     /*
69      *  Bitmask indicating portions of available Zorro II RAM that are unused
70      *  by the system. Every bit represents a 64K chunk, for a maximum of 8MB
71      *  (128 chunks, physical 0x00200000-0x009fffff).
72      *
73      *  If you want to use (= allocate) portions of this RAM, you should clear
74      *  the corresponding bits.
75      *
76      *  Possible uses:
77      *      - z2ram device
78      *      - SCSI DMA bounce buffers
79      *
80      *  FIXME: use the normal resource management
81      */
82 
83 u32 zorro_unused_z2ram[4] = { 0, 0, 0, 0 };
84 
85 
mark_region(unsigned long start,unsigned long end,int flag)86 static void __init mark_region(unsigned long start, unsigned long end,
87 			       int flag)
88 {
89     if (flag)
90 	start += Z2RAM_CHUNKMASK;
91     else
92 	end += Z2RAM_CHUNKMASK;
93     start &= ~Z2RAM_CHUNKMASK;
94     end &= ~Z2RAM_CHUNKMASK;
95 
96     if (end <= Z2RAM_START || start >= Z2RAM_END)
97 	return;
98     start = start < Z2RAM_START ? 0x00000000 : start-Z2RAM_START;
99     end = end > Z2RAM_END ? Z2RAM_SIZE : end-Z2RAM_START;
100     while (start < end) {
101 	u32 chunk = start>>Z2RAM_CHUNKSHIFT;
102 	if (flag)
103 	    set_bit(chunk, zorro_unused_z2ram);
104 	else
105 	    clear_bit(chunk, zorro_unused_z2ram);
106 	start += Z2RAM_CHUNKSIZE;
107     }
108 }
109 
110 
zorro_find_parent_resource(struct zorro_dev * z)111 static struct resource __init *zorro_find_parent_resource(struct zorro_dev *z)
112 {
113     int i;
114 
115     for (i = 0; i < zorro_num_res; i++)
116 	if (z->resource.start >= zorro_res[i].start &&
117 	    z->resource.end <= zorro_res[i].end)
118 		return &zorro_res[i];
119     return &iomem_resource;
120 }
121 
122 
123     /*
124      *  Initialization
125      */
126 
zorro_init(void)127 void __init zorro_init(void)
128 {
129     struct zorro_dev *dev;
130     u_int i;
131 
132     if (!MACH_IS_AMIGA || !AMIGAHW_PRESENT(ZORRO))
133 	return;
134 
135     printk("Zorro: Probing AutoConfig expansion devices: %d device%s\n",
136 	   zorro_num_autocon, zorro_num_autocon == 1 ? "" : "s");
137 
138     /* Request the resources */
139     zorro_num_res = AMIGAHW_PRESENT(ZORRO3) ? 4 : 2;
140     for (i = 0; i < zorro_num_res; i++)
141 	request_resource(&iomem_resource, &zorro_res[i]);
142     for (i = 0; i < zorro_num_autocon; i++) {
143 	dev = &zorro_autocon[i];
144 	dev->id = (dev->rom.er_Manufacturer<<16) | (dev->rom.er_Product<<8);
145 	if (dev->id == ZORRO_PROD_GVP_EPC_BASE) {
146 	    /* GVP quirk */
147 	    unsigned long magic = dev->resource.start+0x8000;
148 	    dev->id |= *(u16 *)ZTWO_VADDR(magic) & GVP_PRODMASK;
149 	}
150 	sprintf(dev->name, "Zorro device %08x", dev->id);
151 	zorro_name_device(dev);
152 	dev->resource.name = dev->name;
153 	if (request_resource(zorro_find_parent_resource(dev), &dev->resource))
154 	    printk(KERN_ERR "Zorro: Address space collision on device %s "
155 		   "[%lx:%lx]\n",
156 		   dev->name, dev->resource.start, dev->resource.end);
157     }
158 
159     /* Mark all available Zorro II memory */
160     for (i = 0; i < zorro_num_autocon; i++) {
161 	dev = &zorro_autocon[i];
162 	if (dev->rom.er_Type & ERTF_MEMLIST)
163 	    mark_region(dev->resource.start, dev->resource.end+1, 1);
164     }
165 
166     /* Unmark all used Zorro II memory */
167     for (i = 0; i < m68k_num_memory; i++)
168 	if (m68k_memory[i].addr < 16*1024*1024)
169 	    mark_region(m68k_memory[i].addr,
170 			m68k_memory[i].addr+m68k_memory[i].size, 0);
171 }
172 
173 
174 EXPORT_SYMBOL(zorro_find_device);
175 EXPORT_SYMBOL(zorro_unused_z2ram);
176 
177 MODULE_LICENSE("GPL");
178