OpenCores
URL https://opencores.org/ocsvn/or1k/or1k/trunk

Subversion Repositories or1k

[/] [or1k/] [trunk/] [linux/] [linux-2.4/] [drivers/] [scsi/] [oktagon_esp.c] - Blame information for rev 1765

Details | Compare with Previous | View Log

Line No. Rev Author Line
1 1275 phoenix
/*
2
 * Oktagon_esp.c -- Driver for bsc Oktagon
3
 *
4
 * Written by Carsten Pluntke 1998
5
 *
6
 * Based on cyber_esp.c
7
 */
8
 
9
#include <linux/config.h>
10
 
11
#if defined(CONFIG_AMIGA) || defined(CONFIG_APUS)
12
#define USE_BOTTOM_HALF
13
#endif
14
 
15
#define __KERNEL_SYSCALLS__
16
#include <linux/module.h>
17
 
18
#include <linux/kernel.h>
19
#include <linux/delay.h>
20
#include <linux/types.h>
21
#include <linux/string.h>
22
#include <linux/slab.h>
23
#include <linux/blk.h>
24
#include <linux/proc_fs.h>
25
#include <linux/stat.h>
26
#include <linux/reboot.h>
27
#include <asm/system.h>
28
#include <asm/ptrace.h>
29
#include <asm/pgtable.h>
30
 
31
 
32
#include "scsi.h"
33
#include "hosts.h"
34
#include "NCR53C9x.h"
35
#include "oktagon_esp.h"
36
 
37
#include <linux/zorro.h>
38
#include <asm/irq.h>
39
#include <asm/amigaints.h>
40
#include <asm/amigahw.h>
41
 
42
#ifdef USE_BOTTOM_HALF
43
#include <linux/tqueue.h>
44
#include <linux/interrupt.h>
45
#endif
46
 
47
#include <linux/unistd.h>
48
 
49
static int  dma_bytes_sent(struct NCR_ESP *esp, int fifo_count);
50
static int  dma_can_transfer(struct NCR_ESP *esp, Scsi_Cmnd *sp);
51
static void dma_dump_state(struct NCR_ESP *esp);
52
static void dma_init_read(struct NCR_ESP *esp, __u32 vaddress, int length);
53
static void dma_init_write(struct NCR_ESP *esp, __u32 vaddress, int length);
54
static void dma_ints_off(struct NCR_ESP *esp);
55
static void dma_ints_on(struct NCR_ESP *esp);
56
static int  dma_irq_p(struct NCR_ESP *esp);
57
static void dma_led_off(struct NCR_ESP *esp);
58
static void dma_led_on(struct NCR_ESP *esp);
59
static int  dma_ports_p(struct NCR_ESP *esp);
60
static void dma_setup(struct NCR_ESP *esp, __u32 addr, int count, int write);
61
 
62
static void dma_irq_exit(struct NCR_ESP *esp);
63
static void dma_invalidate(struct NCR_ESP *esp);
64
 
65
static void dma_mmu_get_scsi_one(struct NCR_ESP *,Scsi_Cmnd *);
66
static void dma_mmu_get_scsi_sgl(struct NCR_ESP *,Scsi_Cmnd *);
67
static void dma_mmu_release_scsi_one(struct NCR_ESP *,Scsi_Cmnd *);
68
static void dma_mmu_release_scsi_sgl(struct NCR_ESP *,Scsi_Cmnd *);
69
static void dma_advance_sg(Scsi_Cmnd *);
70
static int  oktagon_notify_reboot(struct notifier_block *this, unsigned long code, void *x);
71
 
72
void esp_bootup_reset(struct NCR_ESP *esp,struct ESP_regs *eregs);
73
 
74
#ifdef USE_BOTTOM_HALF
75
static void dma_commit(void *opaque);
76
 
77
long oktag_to_io(long *paddr, long *addr, long len);
78
long oktag_from_io(long *addr, long *paddr, long len);
79
 
80
static struct tq_struct tq_fake_dma = {
81
    routine:    dma_commit,
82
};
83
 
84
#define DMA_MAXTRANSFER 0x8000
85
 
86
#else
87
 
88
/*
89
 * No bottom half. Use transfer directly from IRQ. Find a narrow path
90
 * between too much IRQ overhead and clogging the IRQ for too long.
91
 */
92
 
93
#define DMA_MAXTRANSFER 0x1000
94
 
95
#endif
96
 
97
static struct notifier_block oktagon_notifier = {
98
        oktagon_notify_reboot,
99
        NULL,
100
 
101
};
102
 
103
static long *paddress;
104
static long *address;
105
static long len;
106
static long dma_on;
107
static int direction;
108
static struct NCR_ESP *current_esp;
109
 
110
 
111
static volatile unsigned char cmd_buffer[16];
112
                                /* This is where all commands are put
113
                                 * before they are trasfered to the ESP chip
114
                                 * via PIO.
115
                                 */
116
 
117
/***************************************************************** Detection */
118
int oktagon_esp_detect(Scsi_Host_Template *tpnt)
119
{
120
        struct NCR_ESP *esp;
121
        struct zorro_dev *z = NULL;
122
        unsigned long address;
123
        struct ESP_regs *eregs;
124
 
125
        while ((z = zorro_find_device(ZORRO_PROD_BSC_OKTAGON_2008, z))) {
126
            unsigned long board = z->resource.start;
127
            if (request_mem_region(board+OKTAGON_ESP_ADDR,
128
                                   sizeof(struct ESP_regs), "NCR53C9x")) {
129
                /*
130
                 * It is a SCSI controller.
131
                 * Hardwire Host adapter to SCSI ID 7
132
                 */
133
 
134
                address = (unsigned long)ZTWO_VADDR(board);
135
                eregs = (struct ESP_regs *)(address + OKTAGON_ESP_ADDR);
136
 
137
                /* This line was 5 lines lower */
138
                esp = esp_allocate(tpnt, (void *)board+OKTAGON_ESP_ADDR);
139
 
140
                /* we have to shift the registers only one bit for oktagon */
141
                esp->shift = 1;
142
 
143
                esp_write(eregs->esp_cfg1, (ESP_CONFIG1_PENABLE | 7));
144
                udelay(5);
145
                if (esp_read(eregs->esp_cfg1) != (ESP_CONFIG1_PENABLE | 7))
146
                        return 0; /* Bail out if address did not hold data */
147
 
148
                /* Do command transfer with programmed I/O */
149
                esp->do_pio_cmds = 1;
150
 
151
                /* Required functions */
152
                esp->dma_bytes_sent = &dma_bytes_sent;
153
                esp->dma_can_transfer = &dma_can_transfer;
154
                esp->dma_dump_state = &dma_dump_state;
155
                esp->dma_init_read = &dma_init_read;
156
                esp->dma_init_write = &dma_init_write;
157
                esp->dma_ints_off = &dma_ints_off;
158
                esp->dma_ints_on = &dma_ints_on;
159
                esp->dma_irq_p = &dma_irq_p;
160
                esp->dma_ports_p = &dma_ports_p;
161
                esp->dma_setup = &dma_setup;
162
 
163
                /* Optional functions */
164
                esp->dma_barrier = 0;
165
                esp->dma_drain = 0;
166
                esp->dma_invalidate = &dma_invalidate;
167
                esp->dma_irq_entry = 0;
168
                esp->dma_irq_exit = &dma_irq_exit;
169
                esp->dma_led_on = &dma_led_on;
170
                esp->dma_led_off = &dma_led_off;
171
                esp->dma_poll = 0;
172
                esp->dma_reset = 0;
173
 
174
                esp->dma_mmu_get_scsi_one = &dma_mmu_get_scsi_one;
175
                esp->dma_mmu_get_scsi_sgl = &dma_mmu_get_scsi_sgl;
176
                esp->dma_mmu_release_scsi_one = &dma_mmu_release_scsi_one;
177
                esp->dma_mmu_release_scsi_sgl = &dma_mmu_release_scsi_sgl;
178
                esp->dma_advance_sg = &dma_advance_sg;
179
 
180
                /* SCSI chip speed */
181
                /* Looking at the quartz of the SCSI board... */
182
                esp->cfreq = 25000000;
183
 
184
                /* The DMA registers on the CyberStorm are mapped
185
                 * relative to the device (i.e. in the same Zorro
186
                 * I/O block).
187
                 */
188
                esp->dregs = (void *)(address + OKTAGON_DMA_ADDR);
189
 
190
                paddress = (long *) esp->dregs;
191
 
192
                /* ESP register base */
193
                esp->eregs = eregs;
194
 
195
                /* Set the command buffer */
196
                esp->esp_command = (volatile unsigned char*) cmd_buffer;
197
 
198
                /* Yes, the virtual address. See below. */
199
                esp->esp_command_dvma = (__u32) cmd_buffer;
200
 
201
                esp->irq = IRQ_AMIGA_PORTS;
202
                request_irq(IRQ_AMIGA_PORTS, esp_intr, SA_SHIRQ,
203
                            "BSC Oktagon SCSI", esp_intr);
204
 
205
                /* Figure out our scsi ID on the bus */
206
                esp->scsi_id = 7;
207
 
208
                /* We don't have a differential SCSI-bus. */
209
                esp->diff = 0;
210
 
211
                esp_initialize(esp);
212
 
213
                printk("ESP_Oktagon Driver 1.1"
214
#ifdef USE_BOTTOM_HALF
215
                       " [BOTTOM_HALF]"
216
#else
217
                       " [IRQ]"
218
#endif
219
                       " registered.\n");
220
                printk("ESP: Total of %d ESP hosts found, %d actually in use.\n", nesps,esps_in_use);
221
                esps_running = esps_in_use;
222
                current_esp = esp;
223
                register_reboot_notifier(&oktagon_notifier);
224
                return esps_in_use;
225
            }
226
        }
227
        return 0;
228
}
229
 
230
 
231
/*
232
 * On certain configurations the SCSI equipment gets confused on reboot,
233
 * so we have to reset it then.
234
 */
235
 
236
static int
237
oktagon_notify_reboot(struct notifier_block *this, unsigned long code, void *x)
238
{
239
  struct NCR_ESP *esp;
240
 
241
  if((code == SYS_DOWN || code == SYS_HALT) && (esp = current_esp))
242
   {
243
    esp_bootup_reset(esp,esp->eregs);
244
    udelay(500); /* Settle time. Maybe unneccessary. */
245
   }
246
  return NOTIFY_DONE;
247
}
248
 
249
 
250
 
251
#ifdef USE_BOTTOM_HALF
252
 
253
 
254
/*
255
 * The bsc Oktagon controller has no real DMA, so we have to do the 'DMA
256
 * transfer' in the interrupt (Yikes!) or use a bottom half to not to clutter
257
 * IRQ's for longer-than-good.
258
 *
259
 * FIXME
260
 * BIG PROBLEM: 'len' is usually the buffer length, not the expected length
261
 * of the data. So DMA may finish prematurely, further reads lead to
262
 * 'machine check' on APUS systems (don't know about m68k systems, AmigaOS
263
 * deliberately ignores the bus faults) and a normal copy-loop can't
264
 * be exited prematurely just at the right moment by the dma_invalidate IRQ.
265
 * So do it the hard way, write an own copier in assembler and
266
 * catch the exception.
267
 *                                     -- Carsten
268
 */
269
 
270
 
271
static void dma_commit(void *opaque)
272
{
273
    long wait,len2,pos;
274
    struct NCR_ESP *esp;
275
 
276
    ESPDATA(("Transfer: %ld bytes, Address 0x%08lX, Direction: %d\n",
277
         len,(long) address,direction));
278
    dma_ints_off(current_esp);
279
 
280
    pos = 0;
281
    wait = 1;
282
    if(direction) /* write? (memory to device) */
283
     {
284
      while(len > 0)
285
       {
286
        len2 = oktag_to_io(paddress, address+pos, len);
287
        if(!len2)
288
         {
289
          if(wait > 1000)
290
           {
291
            printk("Expedited DMA exit (writing) %ld\n",len);
292
            break;
293
           }
294
          mdelay(wait);
295
          wait *= 2;
296
         }
297
        pos += len2;
298
        len -= len2*sizeof(long);
299
       }
300
     } else {
301
      while(len > 0)
302
       {
303
        len2 = oktag_from_io(address+pos, paddress, len);
304
        if(!len2)
305
         {
306
          if(wait > 1000)
307
           {
308
            printk("Expedited DMA exit (reading) %ld\n",len);
309
            break;
310
           }
311
          mdelay(wait);
312
          wait *= 2;
313
         }
314
        pos += len2;
315
        len -= len2*sizeof(long);
316
       }
317
     }
318
 
319
    /* to make esp->shift work */
320
    esp=current_esp;
321
 
322
#if 0
323
    len2 = (esp_read(current_esp->eregs->esp_tclow) & 0xff) |
324
           ((esp_read(current_esp->eregs->esp_tcmed) & 0xff) << 8);
325
 
326
    /*
327
     * Uh uh. If you see this, len and transfer count registers were out of
328
     * sync. That means really serious trouble.
329
     */
330
 
331
    if(len2)
332
      printk("Eeeek!! Transfer count still %ld!\n",len2);
333
#endif
334
 
335
    /*
336
     * Normally we just need to exit and wait for the interrupt to come.
337
     * But at least one device (my Microtek ScanMaker 630) regularly mis-
338
     * calculates the bytes it should send which is really ugly because
339
     * it locks up the SCSI bus if not accounted for.
340
     */
341
 
342
    if(!(esp_read(current_esp->eregs->esp_status) & ESP_STAT_INTR))
343
     {
344
      long len = 100;
345
      long trash[10];
346
 
347
      /*
348
       * Interrupt bit was not set. Either the device is just plain lazy
349
       * so we give it a 10 ms chance or...
350
       */
351
      while(len-- && (!(esp_read(current_esp->eregs->esp_status) & ESP_STAT_INTR)))
352
        udelay(100);
353
 
354
 
355
      if(!(esp_read(current_esp->eregs->esp_status) & ESP_STAT_INTR))
356
       {
357
        /*
358
         * So we think that the transfer count is out of sync. Since we
359
         * have all we want we are happy and can ditch the trash.
360
         */
361
 
362
        len = DMA_MAXTRANSFER;
363
 
364
        while(len-- && (!(esp_read(current_esp->eregs->esp_status) & ESP_STAT_INTR)))
365
          oktag_from_io(trash,paddress,2);
366
 
367
        if(!(esp_read(current_esp->eregs->esp_status) & ESP_STAT_INTR))
368
         {
369
          /*
370
           * Things really have gone wrong. If we leave the system in that
371
           * state, the SCSI bus is locked forever. I hope that this will
372
           * turn the system in a more or less running state.
373
           */
374
          printk("Device is bolixed, trying bus reset...\n");
375
          esp_bootup_reset(current_esp,current_esp->eregs);
376
         }
377
       }
378
     }
379
 
380
    ESPDATA(("Transfer_finale: do_data_finale should come\n"));
381
 
382
    len = 0;
383
    dma_on = 0;
384
    dma_ints_on(current_esp);
385
}
386
 
387
#endif
388
 
389
/************************************************************* DMA Functions */
390
static int dma_bytes_sent(struct NCR_ESP *esp, int fifo_count)
391
{
392
        /* Since the CyberStorm DMA is fully dedicated to the ESP chip,
393
         * the number of bytes sent (to the ESP chip) equals the number
394
         * of bytes in the FIFO - there is no buffering in the DMA controller.
395
         * XXXX Do I read this right? It is from host to ESP, right?
396
         */
397
        return fifo_count;
398
}
399
 
400
static int dma_can_transfer(struct NCR_ESP *esp, Scsi_Cmnd *sp)
401
{
402
        unsigned long sz = sp->SCp.this_residual;
403
        if(sz > DMA_MAXTRANSFER)
404
                sz = DMA_MAXTRANSFER;
405
        return sz;
406
}
407
 
408
static void dma_dump_state(struct NCR_ESP *esp)
409
{
410
}
411
 
412
/*
413
 * What the f$@& is this?
414
 *
415
 * Some SCSI devices (like my Microtek ScanMaker 630 scanner) want to transfer
416
 * more data than requested. How much? Dunno. So ditch the bogus data into
417
 * the sink, hoping the device will advance to the next phase sooner or later.
418
 *
419
 *                         -- Carsten
420
 */
421
 
422
static long oktag_eva_buffer[16]; /* The data sink */
423
 
424
static void oktag_check_dma(void)
425
{
426
  struct NCR_ESP *esp;
427
 
428
  esp=current_esp;
429
  if(!len)
430
   {
431
    address = oktag_eva_buffer;
432
    len = 2;
433
    /* esp_do_data sets them to zero like len */
434
    esp_write(current_esp->eregs->esp_tclow,2);
435
    esp_write(current_esp->eregs->esp_tcmed,0);
436
   }
437
}
438
 
439
static void dma_init_read(struct NCR_ESP *esp, __u32 vaddress, int length)
440
{
441
        /* Zorro is noncached, everything else done using processor. */
442
        /* cache_clear(addr, length); */
443
 
444
        if(dma_on)
445
          panic("dma_init_read while dma process is initialized/running!\n");
446
        direction = 0;
447
        address = (long *) vaddress;
448
        current_esp = esp;
449
        len = length;
450
        oktag_check_dma();
451
        dma_on = 1;
452
}
453
 
454
static void dma_init_write(struct NCR_ESP *esp, __u32 vaddress, int length)
455
{
456
        /* cache_push(addr, length); */
457
 
458
        if(dma_on)
459
          panic("dma_init_write while dma process is initialized/running!\n");
460
        direction = 1;
461
        address = (long *) vaddress;
462
        current_esp = esp;
463
        len = length;
464
        oktag_check_dma();
465
        dma_on = 1;
466
}
467
 
468
static void dma_ints_off(struct NCR_ESP *esp)
469
{
470
        disable_irq(esp->irq);
471
}
472
 
473
static void dma_ints_on(struct NCR_ESP *esp)
474
{
475
        enable_irq(esp->irq);
476
}
477
 
478
static int dma_irq_p(struct NCR_ESP *esp)
479
{
480
        /* It's important to check the DMA IRQ bit in the correct way! */
481
        return (esp_read(esp->eregs->esp_status) & ESP_STAT_INTR);
482
}
483
 
484
static void dma_led_off(struct NCR_ESP *esp)
485
{
486
}
487
 
488
static void dma_led_on(struct NCR_ESP *esp)
489
{
490
}
491
 
492
static int dma_ports_p(struct NCR_ESP *esp)
493
{
494
        return ((custom.intenar) & IF_PORTS);
495
}
496
 
497
static void dma_setup(struct NCR_ESP *esp, __u32 addr, int count, int write)
498
{
499
        /* On the Sparc, DMA_ST_WRITE means "move data from device to memory"
500
         * so when (write) is true, it actually means READ!
501
         */
502
        if(write){
503
                dma_init_read(esp, addr, count);
504
        } else {
505
                dma_init_write(esp, addr, count);
506
        }
507
}
508
 
509
/*
510
 * IRQ entry when DMA transfer is ready to be started
511
 */
512
 
513
static void dma_irq_exit(struct NCR_ESP *esp)
514
{
515
#ifdef USE_BOTTOM_HALF
516
        if(dma_on)
517
         {
518
          tq_fake_dma.sync = 0;
519
          queue_task(&tq_fake_dma,&tq_immediate);
520
          mark_bh(IMMEDIATE_BH);
521
         }
522
#else
523
        while(len && !dma_irq_p(esp))
524
         {
525
          if(direction)
526
            *paddress = *address++;
527
           else
528
            *address++ = *paddress;
529
          len -= (sizeof(long));
530
         }
531
        len = 0;
532
        dma_on = 0;
533
#endif
534
}
535
 
536
/*
537
 * IRQ entry when DMA has just finished
538
 */
539
 
540
static void dma_invalidate(struct NCR_ESP *esp)
541
{
542
}
543
 
544
/*
545
 * Since the processor does the data transfer we have to use the custom
546
 * mmu interface to pass the virtual address, not the physical.
547
 */
548
 
549
void dma_mmu_get_scsi_one(struct NCR_ESP *esp, Scsi_Cmnd *sp)
550
{
551
        sp->SCp.have_data_in = (int) sp->SCp.ptr =
552
                sp->request_buffer;
553
}
554
 
555
void dma_mmu_get_scsi_sgl(struct NCR_ESP *esp, Scsi_Cmnd *sp)
556
{
557
        sp->SCp.ptr =
558
                sp->SCp.buffer->address;
559
}
560
 
561
void dma_mmu_release_scsi_one(struct NCR_ESP *esp, Scsi_Cmnd *sp)
562
{
563
}
564
 
565
void dma_mmu_release_scsi_sgl(struct NCR_ESP *esp, Scsi_Cmnd *sp)
566
{
567
}
568
 
569
void dma_advance_sg(Scsi_Cmnd *sp)
570
{
571
  sp->SCp.ptr = sp->SCp.buffer->address;
572
}
573
 
574
 
575
#define HOSTS_C
576
 
577
#include "oktagon_esp.h"
578
 
579
static Scsi_Host_Template driver_template = SCSI_OKTAGON_ESP;
580
 
581
#include "scsi_module.c"
582
 
583
int oktagon_esp_release(struct Scsi_Host *instance)
584
{
585
#ifdef MODULE
586
        unsigned long address = (unsigned long)((struct NCR_ESP *)instance->hostdata)->edev;
587
        esp_release();
588
        release_mem_region(address, sizeof(struct ESP_regs));
589
        free_irq(IRQ_AMIGA_PORTS, esp_intr);
590
        unregister_reboot_notifier(&oktagon_notifier);
591
#endif
592
        return 1;
593
}
594
 
595
MODULE_LICENSE("GPL");

powered by: WebSVN 2.1.0

© copyright 1999-2024 OpenCores.org, equivalent to Oliscience, all rights reserved. OpenCores®, registered trademark.