This commit was manufactured by cvs2svn to create tag
[linux-2.6.git] / drivers / scsi / sim710.c
1 /*
2  * sim710.c - Copyright (C) 1999 Richard Hirst <richard@sleepie.demon.co.uk>
3  *
4  *----------------------------------------------------------------------------
5  *  This program is free software; you can redistribute it and/or modify
6  *  it under the terms of the GNU General Public License as published by 
7  *  the Free Software Foundation; either version 2 of the License, or
8  *  (at your option) any later version.
9  *
10  *  This program is distributed in the hope that it will be useful,
11  *  but WITHOUT ANY WARRANTY; without even the implied warranty of
12  *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
13  *  GNU General Public License for more details.
14  *
15  *  You should have received a copy of the GNU General Public License
16  *  along with this program; if not, write to the Free Software
17  *  Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
18  *----------------------------------------------------------------------------
19  *
20  * MCA card detection code by Trent McNair.
21  * Fixes to not explicitly nul bss data from Xavier Bestel.
22  * Some multiboard fixes from Rolf Eike Beer.
23  * Auto probing of EISA config space from Trevor Hemsley.
24  *
25  * Rewritten to use 53c700.c by James.Bottomley@SteelEye.com
26  *
27  */
28
29 #include <linux/config.h>
30 #include <linux/module.h>
31
32 #include <linux/blkdev.h>
33 #include <linux/device.h>
34 #include <linux/init.h>
35 #include <linux/mca.h>
36 #include <linux/eisa.h>
37 #include <linux/interrupt.h>
38 #include <scsi/scsi_host.h>
39 #include <scsi/scsi_device.h>
40 #include <scsi/scsi_transport.h>
41 #include <scsi/scsi_transport_spi.h>
42
43 #include "53c700.h"
44
45
46 /* Must be enough for both EISA and MCA */
47 #define MAX_SLOTS 8
48 static __u8 __initdata id_array[MAX_SLOTS] = { [0 ... MAX_SLOTS-1] = 7 };
49
50 char *sim710;           /* command line passed by insmod */
51
52 MODULE_AUTHOR("Richard Hirst");
53 MODULE_DESCRIPTION("Simple NCR53C710 driver");
54 MODULE_LICENSE("GPL");
55
56 MODULE_PARM(sim710, "s");
57
58 #ifdef MODULE
59 #define ARG_SEP ' '
60 #else
61 #define ARG_SEP ','
62 #endif
63
64 __init int
65 param_setup(char *str)
66 {
67         char *pos = str, *next;
68         int slot = -1;
69
70         while(pos != NULL && (next = strchr(pos, ':')) != NULL) {
71                 int val = (int)simple_strtoul(++next, NULL, 0);
72
73                 if(!strncmp(pos, "slot:", 5))
74                         slot = val;
75                 else if(!strncmp(pos, "id:", 3)) {
76                         if(slot == -1) {
77                                 printk(KERN_WARNING "sim710: Must specify slot for id parameter\n");
78                         } else if(slot > MAX_SLOTS) {
79                                 printk(KERN_WARNING "sim710: Illegal slot %d for id %d\n", slot, val);
80                         } else {
81                                 id_array[slot] = val;
82                         }
83                 }
84                 if((pos = strchr(pos, ARG_SEP)) != NULL)
85                         pos++;
86         }
87         return 1;
88 }
89 __setup("sim710=", param_setup);
90
91 static struct scsi_host_template sim710_driver_template = {
92         .name                   = "LSI (Symbios) 710 MCA/EISA",
93         .proc_name              = "sim710",
94         .this_id                = 7,
95         .module                 = THIS_MODULE,
96 };
97
98 static __devinit int
99 sim710_probe_common(struct device *dev, unsigned long base_addr,
100                     int irq, int clock, int differential, int scsi_id)
101 {
102         struct Scsi_Host * host = NULL;
103         struct NCR_700_Host_Parameters *hostdata =
104                 kmalloc(sizeof(struct NCR_700_Host_Parameters), GFP_KERNEL);
105
106         printk(KERN_NOTICE "sim710: %s\n", dev->bus_id);
107         printk(KERN_NOTICE "sim710: irq = %d, clock = %d, base = 0x%lx, scsi_id = %d\n",
108                irq, clock, base_addr, scsi_id);
109
110         if(hostdata == NULL) {
111                 printk(KERN_ERR "sim710: Failed to allocate host data\n");
112                 goto out;
113         }
114         memset(hostdata, 0, sizeof(struct NCR_700_Host_Parameters));
115
116         if(request_region(base_addr, 64, "sim710") == NULL) {
117                 printk(KERN_ERR "sim710: Failed to reserve IO region 0x%lx\n",
118                        base_addr);
119                 goto out_free;
120         }
121
122         /* Fill in the three required pieces of hostdata */
123         hostdata->base = base_addr;
124         hostdata->differential = differential;
125         hostdata->clock = clock;
126         hostdata->chip710 = 1;
127         NCR_700_set_io_mapped(hostdata);
128
129         /* and register the chip */
130         if((host = NCR_700_detect(&sim710_driver_template, hostdata)) == NULL) {
131                 printk(KERN_ERR "sim710: No host detected; card configuration problem?\n");
132                 goto out_release;
133         }
134
135         host->irq = irq;
136         host->this_id = scsi_id;
137
138         if(request_irq(irq, NCR_700_intr, SA_SHIRQ, "sim710", host)) {
139                 printk(KERN_ERR "sim710: irq problem with %d, detaching\n",
140                        irq);
141                 goto out_unregister;
142         }
143
144         scsi_add_host(host, dev); /* XXX handle failure */
145         scsi_scan_host(host);
146         hostdata->dev = dev;
147
148         return 0;
149
150  out_unregister:
151         scsi_host_put(host);
152  out_release:
153         release_region(host->base, 64);
154  out_free:
155         kfree(hostdata);
156  out:
157         return -ENODEV;
158 }
159
160 static __devexit int
161 sim710_device_remove(struct device *dev)
162 {
163         struct Scsi_Host *host = dev_to_shost(dev);
164         struct NCR_700_Host_Parameters *hostdata =
165                 (struct NCR_700_Host_Parameters *)host->hostdata[0];
166
167         scsi_remove_host(host);
168         NCR_700_release(host);
169         kfree(hostdata);
170         free_irq(host->irq, host);
171         return 0;
172 }
173
174 #ifdef CONFIG_MCA
175
176 /* CARD ID 01BB and 01BA use the same pos values */
177 #define MCA_01BB_IO_PORTS { 0x0000, 0x0000, 0x0800, 0x0C00, 0x1000, 0x1400, \
178                             0x1800, 0x1C00, 0x2000, 0x2400, 0x2800, \
179                             0x2C00, 0x3000, 0x3400, 0x3800, 0x3C00, \
180                             0x4000, 0x4400, 0x4800, 0x4C00, 0x5000  }
181
182 #define MCA_01BB_IRQS { 3, 5, 11, 14 }
183
184 /* CARD ID 004f */
185 #define MCA_004F_IO_PORTS { 0x0000, 0x0200, 0x0300, 0x0400, 0x0500,  0x0600 }
186 #define MCA_004F_IRQS { 5, 9, 14 }
187
188 static short sim710_mca_id_table[] = { 0x01bb, 0x01ba, 0x004f, 0};
189
190 static __init int
191 sim710_mca_probe(struct device *dev)
192 {
193         struct mca_device *mca_dev = to_mca_device(dev);
194         int slot = mca_dev->slot;
195         int pos[3];
196         unsigned int base;
197         int irq_vector;
198         short id = sim710_mca_id_table[mca_dev->index];
199         static int io_004f_by_pos[] = MCA_004F_IO_PORTS;
200         static int irq_004f_by_pos[] = MCA_004F_IRQS;
201         static int io_01bb_by_pos[] = MCA_01BB_IO_PORTS;
202         static int irq_01bb_by_pos[] = MCA_01BB_IRQS;
203         char *name;
204         int clock;
205
206         pos[0] = mca_device_read_stored_pos(mca_dev, 2);
207         pos[1] = mca_device_read_stored_pos(mca_dev, 3);
208         pos[2] = mca_device_read_stored_pos(mca_dev, 4);
209
210         /*
211          * 01BB & 01BA port base by bits 7,6,5,4,3,2 in pos[2]
212          *
213          *    000000  <disabled>   001010  0x2800
214          *    000001  <invalid>    001011  0x2C00
215          *    000010  0x0800       001100  0x3000
216          *    000011  0x0C00       001101  0x3400
217          *    000100  0x1000       001110  0x3800
218          *    000101  0x1400       001111  0x3C00
219          *    000110  0x1800       010000  0x4000
220          *    000111  0x1C00       010001  0x4400
221          *    001000  0x2000       010010  0x4800
222          *    001001  0x2400       010011  0x4C00
223          *                         010100  0x5000
224          *
225          * 00F4 port base by bits 3,2,1 in pos[0]
226          *
227          *    000  <disabled>      001    0x200
228          *    010  0x300           011    0x400
229          *    100  0x500           101    0x600
230          *
231          * 01BB & 01BA IRQ is specified in pos[0] bits 7 and 6:
232          *
233          *    00   3               10   11
234          *    01   5               11   14
235          *
236          * 00F4 IRQ specified by bits 6,5,4 in pos[0]
237          *
238          *    100   5              101    9
239          *    110   14
240          */
241
242         if (id == 0x01bb || id == 0x01ba) {
243                 base = io_01bb_by_pos[(pos[2] & 0xFC) >> 2];
244                 irq_vector =
245                         irq_01bb_by_pos[((pos[0] & 0xC0) >> 6)];
246
247                 clock = 50;
248                 if (id == 0x01bb)
249                         name = "NCR 3360/3430 SCSI SubSystem";
250                 else
251                         name = "NCR Dual SIOP SCSI Host Adapter Board";
252         } else if ( id == 0x004f ) {
253                 base = io_004f_by_pos[((pos[0] & 0x0E) >> 1)];
254                 irq_vector =
255                         irq_004f_by_pos[((pos[0] & 0x70) >> 4) - 4];
256                 clock = 50;
257                 name = "NCR 53c710 SCSI Host Adapter Board";
258         } else {
259                 return -ENODEV;
260         }
261         mca_device_set_name(mca_dev, name);
262         mca_device_set_claim(mca_dev, 1);
263         base = mca_device_transform_ioport(mca_dev, base);
264         irq_vector = mca_device_transform_irq(mca_dev, irq_vector);
265
266         return sim710_probe_common(dev, base, irq_vector, clock,
267                                    0, id_array[slot]);
268 }
269
270 static struct mca_driver sim710_mca_driver = {
271         .id_table               = sim710_mca_id_table,
272         .driver = {
273                 .name           = "sim710",
274                 .bus            = &mca_bus_type,
275                 .probe          = sim710_mca_probe,
276                 .remove         = __devexit_p(sim710_device_remove),
277         },
278 };
279
280 #endif /* CONFIG_MCA */
281
282 #ifdef CONFIG_EISA
283 static struct eisa_device_id sim710_eisa_ids[] = {
284         { "CPQ4410" },
285         { "CPQ4411" },
286         { "HWP0C80" },
287         { "" }
288 };
289
290 static __init int
291 sim710_eisa_probe(struct device *dev)
292 {
293         struct eisa_device *edev = to_eisa_device(dev);
294         unsigned long io_addr = edev->base_addr;
295         char eisa_cpq_irqs[] = { 11, 14, 15, 10, 9, 0 };
296         char eisa_hwp_irqs[] = { 3, 4, 5, 7, 12, 10, 11, 0};
297         char *eisa_irqs;
298         unsigned char irq_index;
299         unsigned char irq, differential = 0, scsi_id = 7;
300
301         if(strcmp(edev->id.sig, "HWP0C80") == 0) {
302                 __u8 val;
303                 eisa_irqs =  eisa_hwp_irqs;
304                 irq_index = (inb(io_addr + 0xc85) & 0x7) - 1;
305
306                 val = inb(io_addr + 0x4);
307                 scsi_id = ffs(val) - 1;
308
309                 if(scsi_id > 7 || (val & ~(1<<scsi_id)) != 0) {
310                         printk(KERN_ERR "sim710.c, EISA card %s has incorrect scsi_id, setting to 7\n", dev->bus_id);
311                         scsi_id = 7;
312                 }
313         } else {
314                 eisa_irqs = eisa_cpq_irqs;
315                 irq_index = inb(io_addr + 0xc88) & 0x07;
316         }
317
318         if(irq_index >= strlen(eisa_irqs)) {
319                 printk("sim710.c: irq nasty\n");
320                 return -ENODEV;
321         }
322
323         irq = eisa_irqs[irq_index];
324                 
325         return sim710_probe_common(dev, io_addr, irq, 50,
326                                    differential, scsi_id);
327 }
328
329 struct eisa_driver sim710_eisa_driver = {
330         .id_table               = sim710_eisa_ids,
331         .driver = {
332                 .name           = "sim710",
333                 .probe          = sim710_eisa_probe,
334                 .remove         = __devexit_p(sim710_device_remove),
335         },
336 };
337 #endif /* CONFIG_EISA */
338
339 static int __init sim710_init(void)
340 {
341         int err = -ENODEV;
342
343 #ifdef MODULE
344         if (sim710)
345                 param_setup(sim710);
346 #endif
347
348 #ifdef CONFIG_MCA
349         err = mca_register_driver(&sim710_mca_driver);
350 #endif
351
352 #ifdef CONFIG_EISA
353         err = eisa_driver_register(&sim710_eisa_driver);
354 #endif
355         /* FIXME: what we'd really like to return here is -ENODEV if
356          * no devices have actually been found.  Instead, the err
357          * above actually only reports problems with kobject_register,
358          * so for the moment return success */
359
360         return 0;
361 }
362
363 static void __exit sim710_exit(void)
364 {
365 #ifdef CONFIG_MCA
366         if (MCA_bus)
367                 mca_unregister_driver(&sim710_mca_driver);
368 #endif
369
370 #ifdef CONFIG_EISA
371         eisa_driver_unregister(&sim710_eisa_driver);
372 #endif
373 }
374
375 module_init(sim710_init);
376 module_exit(sim710_exit);