x86: optimize lock prefix switching to run less frequently
[linux-2.6/mini2440.git] / drivers / scsi / oktagon_esp.c
blob8e5eadbd5c5151183da96fceb9cc203ce98cc9cf
1 /*
2 * Oktagon_esp.c -- Driver for bsc Oktagon
4 * Written by Carsten Pluntke 1998
6 * Based on cyber_esp.c
7 */
10 #if defined(CONFIG_AMIGA) || defined(CONFIG_APUS)
11 #define USE_BOTTOM_HALF
12 #endif
14 #include <linux/module.h>
16 #include <linux/kernel.h>
17 #include <linux/delay.h>
18 #include <linux/types.h>
19 #include <linux/string.h>
20 #include <linux/slab.h>
21 #include <linux/blkdev.h>
22 #include <linux/proc_fs.h>
23 #include <linux/stat.h>
24 #include <linux/reboot.h>
25 #include <asm/system.h>
26 #include <asm/ptrace.h>
27 #include <asm/pgtable.h>
30 #include "scsi.h"
31 #include <scsi/scsi_host.h>
32 #include "NCR53C9x.h"
34 #include <linux/zorro.h>
35 #include <asm/irq.h>
36 #include <asm/amigaints.h>
37 #include <asm/amigahw.h>
39 #ifdef USE_BOTTOM_HALF
40 #include <linux/workqueue.h>
41 #include <linux/interrupt.h>
42 #endif
44 /* The controller registers can be found in the Z2 config area at these
45 * offsets:
47 #define OKTAGON_ESP_ADDR 0x03000
48 #define OKTAGON_DMA_ADDR 0x01000
51 static int dma_bytes_sent(struct NCR_ESP *esp, int fifo_count);
52 static int dma_can_transfer(struct NCR_ESP *esp, Scsi_Cmnd *sp);
53 static void dma_dump_state(struct NCR_ESP *esp);
54 static void dma_init_read(struct NCR_ESP *esp, __u32 vaddress, int length);
55 static void dma_init_write(struct NCR_ESP *esp, __u32 vaddress, int length);
56 static void dma_ints_off(struct NCR_ESP *esp);
57 static void dma_ints_on(struct NCR_ESP *esp);
58 static int dma_irq_p(struct NCR_ESP *esp);
59 static void dma_led_off(struct NCR_ESP *esp);
60 static void dma_led_on(struct NCR_ESP *esp);
61 static int dma_ports_p(struct NCR_ESP *esp);
62 static void dma_setup(struct NCR_ESP *esp, __u32 addr, int count, int write);
64 static void dma_irq_exit(struct NCR_ESP *esp);
65 static void dma_invalidate(struct NCR_ESP *esp);
67 static void dma_mmu_get_scsi_one(struct NCR_ESP *,Scsi_Cmnd *);
68 static void dma_mmu_get_scsi_sgl(struct NCR_ESP *,Scsi_Cmnd *);
69 static void dma_mmu_release_scsi_one(struct NCR_ESP *,Scsi_Cmnd *);
70 static void dma_mmu_release_scsi_sgl(struct NCR_ESP *,Scsi_Cmnd *);
71 static void dma_advance_sg(Scsi_Cmnd *);
72 static int oktagon_notify_reboot(struct notifier_block *this, unsigned long code, void *x);
74 #ifdef USE_BOTTOM_HALF
75 static void dma_commit(struct work_struct *unused);
77 long oktag_to_io(long *paddr, long *addr, long len);
78 long oktag_from_io(long *addr, long *paddr, long len);
80 static DECLARE_WORK(tq_fake_dma, dma_commit);
82 #define DMA_MAXTRANSFER 0x8000
84 #else
87 * No bottom half. Use transfer directly from IRQ. Find a narrow path
88 * between too much IRQ overhead and clogging the IRQ for too long.
91 #define DMA_MAXTRANSFER 0x1000
93 #endif
95 static struct notifier_block oktagon_notifier = {
96 oktagon_notify_reboot,
97 NULL,
101 static long *paddress;
102 static long *address;
103 static long len;
104 static long dma_on;
105 static int direction;
106 static struct NCR_ESP *current_esp;
109 static volatile unsigned char cmd_buffer[16];
110 /* This is where all commands are put
111 * before they are trasfered to the ESP chip
112 * via PIO.
115 /***************************************************************** Detection */
116 int oktagon_esp_detect(struct scsi_host_template *tpnt)
118 struct NCR_ESP *esp;
119 struct zorro_dev *z = NULL;
120 unsigned long address;
121 struct ESP_regs *eregs;
123 while ((z = zorro_find_device(ZORRO_PROD_BSC_OKTAGON_2008, z))) {
124 unsigned long board = z->resource.start;
125 if (request_mem_region(board+OKTAGON_ESP_ADDR,
126 sizeof(struct ESP_regs), "NCR53C9x")) {
128 * It is a SCSI controller.
129 * Hardwire Host adapter to SCSI ID 7
132 address = (unsigned long)ZTWO_VADDR(board);
133 eregs = (struct ESP_regs *)(address + OKTAGON_ESP_ADDR);
135 /* This line was 5 lines lower */
136 esp = esp_allocate(tpnt, (void *)board + OKTAGON_ESP_ADDR, 0);
138 /* we have to shift the registers only one bit for oktagon */
139 esp->shift = 1;
141 esp_write(eregs->esp_cfg1, (ESP_CONFIG1_PENABLE | 7));
142 udelay(5);
143 if (esp_read(eregs->esp_cfg1) != (ESP_CONFIG1_PENABLE | 7))
144 return 0; /* Bail out if address did not hold data */
146 /* Do command transfer with programmed I/O */
147 esp->do_pio_cmds = 1;
149 /* Required functions */
150 esp->dma_bytes_sent = &dma_bytes_sent;
151 esp->dma_can_transfer = &dma_can_transfer;
152 esp->dma_dump_state = &dma_dump_state;
153 esp->dma_init_read = &dma_init_read;
154 esp->dma_init_write = &dma_init_write;
155 esp->dma_ints_off = &dma_ints_off;
156 esp->dma_ints_on = &dma_ints_on;
157 esp->dma_irq_p = &dma_irq_p;
158 esp->dma_ports_p = &dma_ports_p;
159 esp->dma_setup = &dma_setup;
161 /* Optional functions */
162 esp->dma_barrier = 0;
163 esp->dma_drain = 0;
164 esp->dma_invalidate = &dma_invalidate;
165 esp->dma_irq_entry = 0;
166 esp->dma_irq_exit = &dma_irq_exit;
167 esp->dma_led_on = &dma_led_on;
168 esp->dma_led_off = &dma_led_off;
169 esp->dma_poll = 0;
170 esp->dma_reset = 0;
172 esp->dma_mmu_get_scsi_one = &dma_mmu_get_scsi_one;
173 esp->dma_mmu_get_scsi_sgl = &dma_mmu_get_scsi_sgl;
174 esp->dma_mmu_release_scsi_one = &dma_mmu_release_scsi_one;
175 esp->dma_mmu_release_scsi_sgl = &dma_mmu_release_scsi_sgl;
176 esp->dma_advance_sg = &dma_advance_sg;
178 /* SCSI chip speed */
179 /* Looking at the quartz of the SCSI board... */
180 esp->cfreq = 25000000;
182 /* The DMA registers on the CyberStorm are mapped
183 * relative to the device (i.e. in the same Zorro
184 * I/O block).
186 esp->dregs = (void *)(address + OKTAGON_DMA_ADDR);
188 paddress = (long *) esp->dregs;
190 /* ESP register base */
191 esp->eregs = eregs;
193 /* Set the command buffer */
194 esp->esp_command = (volatile unsigned char*) cmd_buffer;
196 /* Yes, the virtual address. See below. */
197 esp->esp_command_dvma = (__u32) cmd_buffer;
199 esp->irq = IRQ_AMIGA_PORTS;
200 request_irq(IRQ_AMIGA_PORTS, esp_intr, IRQF_SHARED,
201 "BSC Oktagon SCSI", esp->ehost);
203 /* Figure out our scsi ID on the bus */
204 esp->scsi_id = 7;
206 /* We don't have a differential SCSI-bus. */
207 esp->diff = 0;
209 esp_initialize(esp);
211 printk("ESP_Oktagon Driver 1.1"
212 #ifdef USE_BOTTOM_HALF
213 " [BOTTOM_HALF]"
214 #else
215 " [IRQ]"
216 #endif
217 " registered.\n");
218 printk("ESP: Total of %d ESP hosts found, %d actually in use.\n", nesps,esps_in_use);
219 esps_running = esps_in_use;
220 current_esp = esp;
221 register_reboot_notifier(&oktagon_notifier);
222 return esps_in_use;
225 return 0;
230 * On certain configurations the SCSI equipment gets confused on reboot,
231 * so we have to reset it then.
234 static int
235 oktagon_notify_reboot(struct notifier_block *this, unsigned long code, void *x)
237 struct NCR_ESP *esp;
239 if((code == SYS_DOWN || code == SYS_HALT) && (esp = current_esp))
241 esp_bootup_reset(esp,esp->eregs);
242 udelay(500); /* Settle time. Maybe unnecessary. */
244 return NOTIFY_DONE;
249 #ifdef USE_BOTTOM_HALF
253 * The bsc Oktagon controller has no real DMA, so we have to do the 'DMA
254 * transfer' in the interrupt (Yikes!) or use a bottom half to not to clutter
255 * IRQ's for longer-than-good.
257 * FIXME
258 * BIG PROBLEM: 'len' is usually the buffer length, not the expected length
259 * of the data. So DMA may finish prematurely, further reads lead to
260 * 'machine check' on APUS systems (don't know about m68k systems, AmigaOS
261 * deliberately ignores the bus faults) and a normal copy-loop can't
262 * be exited prematurely just at the right moment by the dma_invalidate IRQ.
263 * So do it the hard way, write an own copier in assembler and
264 * catch the exception.
265 * -- Carsten
269 static void dma_commit(struct work_struct *unused)
271 long wait,len2,pos;
272 struct NCR_ESP *esp;
274 ESPDATA(("Transfer: %ld bytes, Address 0x%08lX, Direction: %d\n",
275 len,(long) address,direction));
276 dma_ints_off(current_esp);
278 pos = 0;
279 wait = 1;
280 if(direction) /* write? (memory to device) */
282 while(len > 0)
284 len2 = oktag_to_io(paddress, address+pos, len);
285 if(!len2)
287 if(wait > 1000)
289 printk("Expedited DMA exit (writing) %ld\n",len);
290 break;
292 mdelay(wait);
293 wait *= 2;
295 pos += len2;
296 len -= len2*sizeof(long);
298 } else {
299 while(len > 0)
301 len2 = oktag_from_io(address+pos, paddress, len);
302 if(!len2)
304 if(wait > 1000)
306 printk("Expedited DMA exit (reading) %ld\n",len);
307 break;
309 mdelay(wait);
310 wait *= 2;
312 pos += len2;
313 len -= len2*sizeof(long);
317 /* to make esp->shift work */
318 esp=current_esp;
320 #if 0
321 len2 = (esp_read(current_esp->eregs->esp_tclow) & 0xff) |
322 ((esp_read(current_esp->eregs->esp_tcmed) & 0xff) << 8);
325 * Uh uh. If you see this, len and transfer count registers were out of
326 * sync. That means really serious trouble.
329 if(len2)
330 printk("Eeeek!! Transfer count still %ld!\n",len2);
331 #endif
334 * Normally we just need to exit and wait for the interrupt to come.
335 * But at least one device (my Microtek ScanMaker 630) regularly mis-
336 * calculates the bytes it should send which is really ugly because
337 * it locks up the SCSI bus if not accounted for.
340 if(!(esp_read(current_esp->eregs->esp_status) & ESP_STAT_INTR))
342 long len = 100;
343 long trash[10];
346 * Interrupt bit was not set. Either the device is just plain lazy
347 * so we give it a 10 ms chance or...
349 while(len-- && (!(esp_read(current_esp->eregs->esp_status) & ESP_STAT_INTR)))
350 udelay(100);
353 if(!(esp_read(current_esp->eregs->esp_status) & ESP_STAT_INTR))
356 * So we think that the transfer count is out of sync. Since we
357 * have all we want we are happy and can ditch the trash.
360 len = DMA_MAXTRANSFER;
362 while(len-- && (!(esp_read(current_esp->eregs->esp_status) & ESP_STAT_INTR)))
363 oktag_from_io(trash,paddress,2);
365 if(!(esp_read(current_esp->eregs->esp_status) & ESP_STAT_INTR))
368 * Things really have gone wrong. If we leave the system in that
369 * state, the SCSI bus is locked forever. I hope that this will
370 * turn the system in a more or less running state.
372 printk("Device is bolixed, trying bus reset...\n");
373 esp_bootup_reset(current_esp,current_esp->eregs);
378 ESPDATA(("Transfer_finale: do_data_finale should come\n"));
380 len = 0;
381 dma_on = 0;
382 dma_ints_on(current_esp);
385 #endif
387 /************************************************************* DMA Functions */
388 static int dma_bytes_sent(struct NCR_ESP *esp, int fifo_count)
390 /* Since the CyberStorm DMA is fully dedicated to the ESP chip,
391 * the number of bytes sent (to the ESP chip) equals the number
392 * of bytes in the FIFO - there is no buffering in the DMA controller.
393 * XXXX Do I read this right? It is from host to ESP, right?
395 return fifo_count;
398 static int dma_can_transfer(struct NCR_ESP *esp, Scsi_Cmnd *sp)
400 unsigned long sz = sp->SCp.this_residual;
401 if(sz > DMA_MAXTRANSFER)
402 sz = DMA_MAXTRANSFER;
403 return sz;
406 static void dma_dump_state(struct NCR_ESP *esp)
411 * What the f$@& is this?
413 * Some SCSI devices (like my Microtek ScanMaker 630 scanner) want to transfer
414 * more data than requested. How much? Dunno. So ditch the bogus data into
415 * the sink, hoping the device will advance to the next phase sooner or later.
417 * -- Carsten
420 static long oktag_eva_buffer[16]; /* The data sink */
422 static void oktag_check_dma(void)
424 struct NCR_ESP *esp;
426 esp=current_esp;
427 if(!len)
429 address = oktag_eva_buffer;
430 len = 2;
431 /* esp_do_data sets them to zero like len */
432 esp_write(current_esp->eregs->esp_tclow,2);
433 esp_write(current_esp->eregs->esp_tcmed,0);
437 static void dma_init_read(struct NCR_ESP *esp, __u32 vaddress, int length)
439 /* Zorro is noncached, everything else done using processor. */
440 /* cache_clear(addr, length); */
442 if(dma_on)
443 panic("dma_init_read while dma process is initialized/running!\n");
444 direction = 0;
445 address = (long *) vaddress;
446 current_esp = esp;
447 len = length;
448 oktag_check_dma();
449 dma_on = 1;
452 static void dma_init_write(struct NCR_ESP *esp, __u32 vaddress, int length)
454 /* cache_push(addr, length); */
456 if(dma_on)
457 panic("dma_init_write while dma process is initialized/running!\n");
458 direction = 1;
459 address = (long *) vaddress;
460 current_esp = esp;
461 len = length;
462 oktag_check_dma();
463 dma_on = 1;
466 static void dma_ints_off(struct NCR_ESP *esp)
468 disable_irq(esp->irq);
471 static void dma_ints_on(struct NCR_ESP *esp)
473 enable_irq(esp->irq);
476 static int dma_irq_p(struct NCR_ESP *esp)
478 /* It's important to check the DMA IRQ bit in the correct way! */
479 return (esp_read(esp->eregs->esp_status) & ESP_STAT_INTR);
482 static void dma_led_off(struct NCR_ESP *esp)
486 static void dma_led_on(struct NCR_ESP *esp)
490 static int dma_ports_p(struct NCR_ESP *esp)
492 return ((amiga_custom.intenar) & IF_PORTS);
495 static void dma_setup(struct NCR_ESP *esp, __u32 addr, int count, int write)
497 /* On the Sparc, DMA_ST_WRITE means "move data from device to memory"
498 * so when (write) is true, it actually means READ!
500 if(write){
501 dma_init_read(esp, addr, count);
502 } else {
503 dma_init_write(esp, addr, count);
508 * IRQ entry when DMA transfer is ready to be started
511 static void dma_irq_exit(struct NCR_ESP *esp)
513 #ifdef USE_BOTTOM_HALF
514 if(dma_on)
516 schedule_work(&tq_fake_dma);
518 #else
519 while(len && !dma_irq_p(esp))
521 if(direction)
522 *paddress = *address++;
523 else
524 *address++ = *paddress;
525 len -= (sizeof(long));
527 len = 0;
528 dma_on = 0;
529 #endif
533 * IRQ entry when DMA has just finished
536 static void dma_invalidate(struct NCR_ESP *esp)
541 * Since the processor does the data transfer we have to use the custom
542 * mmu interface to pass the virtual address, not the physical.
545 void dma_mmu_get_scsi_one(struct NCR_ESP *esp, Scsi_Cmnd *sp)
547 sp->SCp.ptr =
548 sp->request_buffer;
551 void dma_mmu_get_scsi_sgl(struct NCR_ESP *esp, Scsi_Cmnd *sp)
553 sp->SCp.ptr = sg_virt(sp->SCp.buffer);
556 void dma_mmu_release_scsi_one(struct NCR_ESP *esp, Scsi_Cmnd *sp)
560 void dma_mmu_release_scsi_sgl(struct NCR_ESP *esp, Scsi_Cmnd *sp)
564 void dma_advance_sg(Scsi_Cmnd *sp)
566 sp->SCp.ptr = sg_virt(sp->SCp.buffer);
570 #define HOSTS_C
572 int oktagon_esp_release(struct Scsi_Host *instance)
574 #ifdef MODULE
575 unsigned long address = (unsigned long)((struct NCR_ESP *)instance->hostdata)->edev;
576 esp_release();
577 release_mem_region(address, sizeof(struct ESP_regs));
578 free_irq(IRQ_AMIGA_PORTS, esp_intr);
579 unregister_reboot_notifier(&oktagon_notifier);
580 #endif
581 return 1;
585 static struct scsi_host_template driver_template = {
586 .proc_name = "esp-oktagon",
587 .proc_info = &esp_proc_info,
588 .name = "BSC Oktagon SCSI",
589 .detect = oktagon_esp_detect,
590 .slave_alloc = esp_slave_alloc,
591 .slave_destroy = esp_slave_destroy,
592 .release = oktagon_esp_release,
593 .queuecommand = esp_queue,
594 .eh_abort_handler = esp_abort,
595 .eh_bus_reset_handler = esp_reset,
596 .can_queue = 7,
597 .this_id = 7,
598 .sg_tablesize = SG_ALL,
599 .cmd_per_lun = 1,
600 .use_clustering = ENABLE_CLUSTERING
604 #include "scsi_module.c"
606 MODULE_LICENSE("GPL");