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

Subversion Repositories or1k

[/] [or1k/] [trunk/] [linux/] [linux-2.4/] [drivers/] [ide/] [pci/] [siimage.c] - Blame information for rev 1765

Details | Compare with Previous | View Log

Line No. Rev Author Line
1 1275 phoenix
/*
2
 * linux/drivers/ide/pci/siimage.c              Version 1.06    June 11, 2003
3
 *
4
 * Copyright (C) 2001-2002      Andre Hedrick <andre@linux-ide.org>
5
 * Copyright (C) 2003           Red Hat <alan@redhat.com>
6
 *
7
 *  May be copied or modified under the terms of the GNU General Public License
8
 *
9
 *  Documentation available under NDA only
10
 *
11
 *
12
 *  FAQ Items:
13
 *      If you are using Marvell SATA-IDE adapters with Maxtor drives
14
 *      ensure the system is set up for ATA100/UDMA5 not UDMA6.
15
 *
16
 *      If you are using WD drives with SATA bridges you must set the
17
 *      drive to "Single". "Master" will hang
18
 *
19
 *      If you have strange problems with nVidia chipset systems please
20
 *      see the SI support documentation and update your system BIOS
21
 *      if neccessary
22
 */
23
 
24
#include <linux/config.h>
25
#include <linux/types.h>
26
#include <linux/module.h>
27
#include <linux/pci.h>
28
#include <linux/delay.h>
29
#include <linux/hdreg.h>
30
#include <linux/ide.h>
31
#include <linux/init.h>
32
 
33
#include <asm/io.h>
34
 
35
#include "ide_modes.h"
36
#include "siimage.h"
37
 
38
#if defined(DISPLAY_SIIMAGE_TIMINGS) && defined(CONFIG_PROC_FS)
39
#include <linux/stat.h>
40
#include <linux/proc_fs.h>
41
 
42
static u8 siimage_proc = 0;
43
#define SIIMAGE_MAX_DEVS                16
44
static struct pci_dev *siimage_devs[SIIMAGE_MAX_DEVS];
45
static int n_siimage_devs;
46
 
47
/**
48
 *      pdev_is_sata            -       check if device is SATA
49
 *      @pdev:  PCI device to check
50
 *
51
 *      Returns true if this is a SATA controller
52
 */
53
 
54
static int pdev_is_sata(struct pci_dev *pdev)
55
{
56
        switch(pdev->device)
57
        {
58
                case PCI_DEVICE_ID_SII_3112:
59
                case PCI_DEVICE_ID_SII_1210SA:
60
                        return 1;
61
                case PCI_DEVICE_ID_SII_680:
62
                        return 0;
63
        }
64
        BUG();
65
        return 0;
66
}
67
 
68
/**
69
 *      is_sata                 -       check if hwif is SATA
70
 *      @hwif:  interface to check
71
 *
72
 *      Returns true if this is a SATA controller
73
 */
74
 
75
static inline int is_sata(ide_hwif_t *hwif)
76
{
77
        return pdev_is_sata(hwif->pci_dev);
78
}
79
 
80
/**
81
 *      siimage_selreg          -       return register base
82
 *      @hwif: interface
83
 *      @r: config offset
84
 *
85
 *      Turn a config register offset into the right address in either
86
 *      PCI space or MMIO space to access the control register in question
87
 *      Thankfully this is a configuration operation so isnt performance
88
 *      criticial.
89
 */
90
 
91
static unsigned long siimage_selreg(ide_hwif_t *hwif, int r)
92
{
93
        unsigned long base = (unsigned long)hwif->hwif_data;
94
        base += 0xA0 + r;
95
        if(hwif->mmio)
96
                base += (hwif->channel << 6);
97
        else
98
                base += (hwif->channel << 4);
99
        return base;
100
}
101
 
102
/**
103
 *      siimage_seldev          -       return register base
104
 *      @hwif: interface
105
 *      @r: config offset
106
 *
107
 *      Turn a config register offset into the right address in either
108
 *      PCI space or MMIO space to access the control register in question
109
 *      including accounting for the unit shift.
110
 */
111
 
112
static inline unsigned long siimage_seldev(ide_drive_t *drive, int r)
113
{
114
        ide_hwif_t *hwif        = HWIF(drive);
115
        unsigned long base = (unsigned long)hwif->hwif_data;
116
        base += 0xA0 + r;
117
        if(hwif->mmio)
118
                base += (hwif->channel << 6);
119
        else
120
                base += (hwif->channel << 4);
121
        base |= drive->select.b.unit << drive->select.b.unit;
122
        return base;
123
}
124
 
125
/**
126
 *      print_siimage_get_info  -       print minimal proc information
127
 *      @buf: buffer to write into (kernel space)
128
 *      @dev: PCI device we are describing
129
 *      @index: Controller number
130
 *
131
 *      Print the basic information for the state of the CMD680/SI3112
132
 *      channel. We don't actually dump a lot of information out for
133
 *      this controller although we could expand it if we needed.
134
 */
135
 
136
static char *print_siimage_get_info (char *buf, struct pci_dev *dev, int index)
137
{
138
        char *p         = buf;
139
        u8 mmio         = (pci_get_drvdata(dev) != NULL) ? 1 : 0;
140
        unsigned long bmdma = pci_resource_start(dev, 4);
141
 
142
        if(mmio)
143
                bmdma = pci_resource_start(dev, 5);
144
 
145
        p += sprintf(p, "\nController: %d\n", index);
146
        p += sprintf(p, "SiI%x Chipset.\n", dev->device);
147
        if (mmio)
148
                p += sprintf(p, "MMIO Base 0x%lx\n", bmdma);
149
        p += sprintf(p, "%s-DMA Base 0x%lx\n", (mmio)?"MMIO":"BM", bmdma);
150
        p += sprintf(p, "%s-DMA Base 0x%lx\n", (mmio)?"MMIO":"BM", bmdma+8);
151
        return (char *)p;
152
}
153
 
154
/**
155
 *      siimage_get_info        -       proc callback
156
 *      @buffer: kernel buffer to complete
157
 *      @addr: written with base of data to return
158
 *      offset: seek offset
159
 *      count: bytes to fill in
160
 *
161
 *      Called when the user reads data from the virtual file for this
162
 *      controller from /proc
163
 */
164
 
165
static int siimage_get_info (char *buffer, char **addr, off_t offset, int count)
166
{
167
        char *p = buffer;
168
        int len;
169
        u16 i;
170
 
171
        p += sprintf(p, "\n");
172
        for (i = 0; i < n_siimage_devs; i++) {
173
                struct pci_dev *dev     = siimage_devs[i];
174
                p = print_siimage_get_info(p, dev, i);
175
        }
176
        /* p - buffer must be less than 4k! */
177
        len = (p - buffer) - offset;
178
        *addr = buffer + offset;
179
 
180
        return len > count ? count : len;
181
}
182
 
183
#endif  /* defined(DISPLAY_SIIMAGE_TIMINGS) && defined(CONFIG_PROC_FS) */
184
 
185
/**
186
 *      siimage_ratemask        -       Compute available modes
187
 *      @drive: IDE drive
188
 *
189
 *      Compute the available speeds for the devices on the interface.
190
 *      For the CMD680 this depends on the clocking mode (scsc), for the
191
 *      SI3312 SATA controller life is a bit simpler. Enforce UDMA33
192
 *      as a limit if there is no 80pin cable present.
193
 */
194
 
195
static byte siimage_ratemask (ide_drive_t *drive)
196
{
197
        ide_hwif_t *hwif        = HWIF(drive);
198
        u8 mode = 0, scsc = 0;
199
        unsigned long base = (unsigned long) hwif->hwif_data;
200
 
201
        if (hwif->mmio)
202
                scsc = hwif->INB(base + 0x4A);
203
        else
204
                pci_read_config_byte(hwif->pci_dev, 0x8A, &scsc);
205
 
206
        if(is_sata(hwif))
207
        {
208
                if(strstr(drive->id->model, "Maxtor"))
209
                        return 3;
210
                return 4;
211
        }
212
 
213
        if ((scsc & 0x30) == 0x10)      /* 133 */
214
                mode = 4;
215
        else if ((scsc & 0x30) == 0x20) /* 2xPCI */
216
                mode = 4;
217
        else if ((scsc & 0x30) == 0x00) /* 100 */
218
                mode = 3;
219
        else    /* Disabled ? */
220
                BUG();
221
 
222
        if (!eighty_ninty_three(drive))
223
                mode = min(mode, (u8)1);
224
        return mode;
225
}
226
 
227
/**
228
 *      siimage_taskfile_timing -       turn timing data to a mode
229
 *      @hwif: interface to query
230
 *
231
 *      Read the timing data for the interface and return the
232
 *      mode that is being used.
233
 */
234
 
235
static byte siimage_taskfile_timing (ide_hwif_t *hwif)
236
{
237
        u16 timing      = 0x328a;
238
        unsigned long addr = siimage_selreg(hwif, 2);
239
 
240
        if (hwif->mmio)
241
                timing = hwif->INW(addr);
242
        else
243
                pci_read_config_word(hwif->pci_dev, addr, &timing);
244
 
245
        switch (timing) {
246
                case 0x10c1:    return 4;
247
                case 0x10c3:    return 3;
248
                case 0x1104:
249
                case 0x1281:    return 2;
250
                case 0x2283:    return 1;
251
                case 0x328a:
252
                default:        return 0;
253
        }
254
}
255
 
256
/**
257
 *      simmage_tuneproc        -       tune a drive
258
 *      @drive: drive to tune
259
 *      @mode_wanted: the target operating mode
260
 *
261
 *      Load the timing settings for this device mode into the
262
 *      controller. If we are in PIO mode 3 or 4 turn on IORDY
263
 *      monitoring (bit 9). The TF timing is bits 31:16
264
 */
265
 
266
static void siimage_tuneproc (ide_drive_t *drive, byte mode_wanted)
267
{
268
        ide_hwif_t *hwif        = HWIF(drive);
269
        u32 speedt              = 0;
270
        u16 speedp              = 0;
271
        unsigned long addr      = siimage_seldev(drive, 0x04);
272
        unsigned long tfaddr    = siimage_selreg(hwif, 0x02);
273
 
274
        /* cheat for now and use the docs */
275
        switch(mode_wanted) {
276
                case 4:
277
                        speedp = 0x10c1;
278
                        speedt = 0x10c1;
279
                        break;
280
                case 3:
281
                        speedp = 0x10C3;
282
                        speedt = 0x10C3;
283
                        break;
284
                case 2:
285
                        speedp = 0x1104;
286
                        speedt = 0x1281;
287
                        break;
288
                case 1:
289
                        speedp = 0x2283;
290
                        speedt = 0x1281;
291
                        break;
292
                case 0:
293
                default:
294
                        speedp = 0x328A;
295
                        speedt = 0x328A;
296
                        break;
297
        }
298
        if (hwif->mmio)
299
        {
300
                hwif->OUTW(speedt, addr);
301
                hwif->OUTW(speedp, tfaddr);
302
                /* Now set up IORDY */
303
                if(mode_wanted == 3 || mode_wanted == 4)
304
                        hwif->OUTW(hwif->INW(tfaddr-2)|0x200, tfaddr-2);
305
                else
306
                        hwif->OUTW(hwif->INW(tfaddr-2)&~0x200, tfaddr-2);
307
        }
308
        else
309
        {
310
                pci_write_config_word(hwif->pci_dev, addr, speedp);
311
                pci_write_config_word(hwif->pci_dev, tfaddr, speedt);
312
                pci_read_config_word(hwif->pci_dev, tfaddr-2, &speedp);
313
                speedp &= ~0x200;
314
                /* Set IORDY for mode 3 or 4 */
315
                if(mode_wanted == 3 || mode_wanted == 4)
316
                        speedp |= 0x200;
317
                pci_write_config_word(hwif->pci_dev, tfaddr-2, speedp);
318
        }
319
}
320
 
321
/**
322
 *      config_siimage_chipset_for_pio  -       set drive timings
323
 *      @drive: drive to tune
324
 *      @speed we want
325
 *
326
 *      Compute the best pio mode we can for a given device. Also honour
327
 *      the timings for the driver when dealing with mixed devices. Some
328
 *      of this is ugly but its all wrapped up here
329
 *
330
 *      The SI680 can also do VDMA - we need to start using that
331
 *
332
 *      FIXME: we use the BIOS channel timings to avoid driving the task
333
 *      files too fast at the disk. We need to compute the master/slave
334
 *      drive PIO mode properly so that we can up the speed on a hotplug
335
 *      system.
336
 */
337
 
338
static void config_siimage_chipset_for_pio (ide_drive_t *drive, byte set_speed)
339
{
340
        u8 channel_timings      = siimage_taskfile_timing(HWIF(drive));
341
        u8 speed = 0, set_pio    = ide_get_best_pio_mode(drive, 4, 5, NULL);
342
 
343
        /* WARNING PIO timing mess is going to happen b/w devices, argh */
344
        if ((channel_timings != set_pio) && (set_pio > channel_timings))
345
                set_pio = channel_timings;
346
 
347
        siimage_tuneproc(drive, set_pio);
348
        speed = XFER_PIO_0 + set_pio;
349
        if (set_speed)
350
                (void) ide_config_drive_speed(drive, speed);
351
}
352
 
353
static void config_chipset_for_pio (ide_drive_t *drive, byte set_speed)
354
{
355
        config_siimage_chipset_for_pio(drive, set_speed);
356
}
357
 
358
/**
359
 *      siimage_tune_chipset    -       set controller timings
360
 *      @drive: Drive to set up
361
 *      @xferspeed: speed we want to achieve
362
 *
363
 *      Tune the SII chipset for the desired mode. If we can't achieve
364
 *      the desired mode then tune for a lower one, but ultimately
365
 *      make the thing work.
366
 */
367
 
368
static int siimage_tune_chipset (ide_drive_t *drive, byte xferspeed)
369
{
370
        u8 ultra6[]             = { 0x0F, 0x0B, 0x07, 0x05, 0x03, 0x02, 0x01 };
371
        u8 ultra5[]             = { 0x0C, 0x07, 0x05, 0x04, 0x02, 0x01 };
372
        u16 dma[]               = { 0x2208, 0x10C2, 0x10C1 };
373
 
374
        ide_hwif_t *hwif        = HWIF(drive);
375
        u16 ultra = 0, multi     = 0;
376
        u8 mode = 0, unit        = drive->select.b.unit;
377
        u8 speed                = ide_rate_filter(siimage_ratemask(drive), xferspeed);
378
        unsigned long base      = (unsigned long)hwif->hwif_data;
379
        u8 scsc = 0, addr_mask   = ((hwif->channel) ?
380
                                    ((hwif->mmio) ? 0xF4 : 0x84) :
381
                                    ((hwif->mmio) ? 0xB4 : 0x80));
382
 
383
        unsigned long ma        = siimage_seldev(drive, 0x08);
384
        unsigned long ua        = siimage_seldev(drive, 0x0C);
385
 
386
        if (hwif->mmio) {
387
                scsc = hwif->INB(base + 0x4A);
388
                mode = hwif->INB(base + addr_mask);
389
                multi = hwif->INW(ma);
390
                ultra = hwif->INW(ua);
391
        } else {
392
                pci_read_config_byte(hwif->pci_dev, 0x8A, &scsc);
393
                pci_read_config_byte(hwif->pci_dev, addr_mask, &mode);
394
                pci_read_config_word(hwif->pci_dev, ma, &multi);
395
                pci_read_config_word(hwif->pci_dev, ua, &ultra);
396
        }
397
 
398
        mode &= ~((unit) ? 0x30 : 0x03);
399
        ultra &= ~0x3F;
400
        scsc = ((scsc & 0x30) == 0x00) ? 0 : 1;
401
 
402
        scsc = is_sata(hwif) ? 1 : scsc;
403
 
404
        switch(speed) {
405
                case XFER_PIO_4:
406
                case XFER_PIO_3:
407
                case XFER_PIO_2:
408
                case XFER_PIO_1:
409
                case XFER_PIO_0:
410
                        siimage_tuneproc(drive, (speed - XFER_PIO_0));
411
                        mode |= ((unit) ? 0x10 : 0x01);
412
                        break;
413
                case XFER_MW_DMA_2:
414
                case XFER_MW_DMA_1:
415
                case XFER_MW_DMA_0:
416
                        multi = dma[speed - XFER_MW_DMA_0];
417
                        mode |= ((unit) ? 0x20 : 0x02);
418
                        config_siimage_chipset_for_pio(drive, 0);
419
                        break;
420
                case XFER_UDMA_6:
421
                case XFER_UDMA_5:
422
                case XFER_UDMA_4:
423
                case XFER_UDMA_3:
424
                case XFER_UDMA_2:
425
                case XFER_UDMA_1:
426
                case XFER_UDMA_0:
427
                        multi = dma[2];
428
                        ultra |= ((scsc) ? (ultra6[speed - XFER_UDMA_0]) :
429
                                           (ultra5[speed - XFER_UDMA_0]));
430
                        mode |= ((unit) ? 0x30 : 0x03);
431
                        config_siimage_chipset_for_pio(drive, 0);
432
                        break;
433
                default:
434
                        return 1;
435
        }
436
 
437
        if (hwif->mmio) {
438
                hwif->OUTB(mode, base + addr_mask);
439
                hwif->OUTW(multi, ma);
440
                hwif->OUTW(ultra, ua);
441
        } else {
442
                pci_write_config_byte(hwif->pci_dev, addr_mask, mode);
443
                pci_write_config_word(hwif->pci_dev, ma, multi);
444
                pci_write_config_word(hwif->pci_dev, ua, ultra);
445
        }
446
        return (ide_config_drive_speed(drive, speed));
447
}
448
 
449
/**
450
 *      config_chipset_for_dma  -       configure for DMA
451
 *      @drive: drive to configure
452
 *
453
 *      Called by the IDE layer when it wants the timings set up.
454
 *      For the CMD680 we also need to set up the PIO timings and
455
 *      enable DMA.
456
 */
457
 
458
static int config_chipset_for_dma (ide_drive_t *drive)
459
{
460
        u8 speed        = ide_dma_speed(drive, siimage_ratemask(drive));
461
 
462
        config_chipset_for_pio(drive, !speed);
463
 
464
        if (!speed)
465
                return 0;
466
 
467
        if (ide_set_xfer_rate(drive, speed))
468
                return 0;
469
 
470
        if (!drive->init_speed)
471
                drive->init_speed = speed;
472
 
473
        return ide_dma_enable(drive);
474
}
475
 
476
/**
477
 *      siimage_configure_drive_for_dma -       set up for DMA transfers
478
 *      @drive: drive we are going to set up
479
 *
480
 *      Set up the drive for DMA, tune the controller and drive as
481
 *      required. If the drive isn't suitable for DMA or we hit
482
 *      other problems then we will drop down to PIO and set up
483
 *      PIO appropriately
484
 */
485
 
486
static int siimage_config_drive_for_dma (ide_drive_t *drive)
487
{
488
        ide_hwif_t *hwif        = HWIF(drive);
489
        struct hd_driveid *id   = drive->id;
490
 
491
        if ((id->capability & 1) != 0 && drive->autodma) {
492
                /* Consult the list of known "bad" drives */
493
                if (hwif->ide_dma_bad_drive(drive))
494
                        goto fast_ata_pio;
495
 
496
                if ((id->field_valid & 4) && siimage_ratemask(drive)) {
497
                        if (id->dma_ultra & hwif->ultra_mask) {
498
                                /* Force if Capable UltraDMA */
499
                                int dma = config_chipset_for_dma(drive);
500
                                if ((id->field_valid & 2) && !dma)
501
                                        goto try_dma_modes;
502
                        }
503
                } else if (id->field_valid & 2) {
504
try_dma_modes:
505
                        if ((id->dma_mword & hwif->mwdma_mask) ||
506
                            (id->dma_1word & hwif->swdma_mask)) {
507
                                /* Force if Capable regular DMA modes */
508
                                if (!config_chipset_for_dma(drive))
509
                                        goto no_dma_set;
510
                        }
511
                } else if (hwif->ide_dma_good_drive(drive) &&
512
                           (id->eide_dma_time < 150)) {
513
                        /* Consult the list of known "good" drives */
514
                        if (!config_chipset_for_dma(drive))
515
                                goto no_dma_set;
516
                } else {
517
                        goto fast_ata_pio;
518
                }
519
        } else if ((id->capability & 8) || (id->field_valid & 2)) {
520
fast_ata_pio:
521
no_dma_set:
522
                config_chipset_for_pio(drive, 1);
523
                return hwif->ide_dma_off_quietly(drive);
524
        }
525
        return hwif->ide_dma_on(drive);
526
}
527
 
528
/* returns 1 if dma irq issued, 0 otherwise */
529
static int siimage_io_ide_dma_test_irq (ide_drive_t *drive)
530
{
531
        ide_hwif_t *hwif        = HWIF(drive);
532
        u8 dma_altstat          = 0;
533
        unsigned long addr      = siimage_selreg(hwif, 1);
534
 
535
        /* return 1 if INTR asserted */
536
        if ((hwif->INB(hwif->dma_status) & 4) == 4)
537
                return 1;
538
 
539
        /* return 1 if Device INTR asserted */
540
        pci_read_config_byte(hwif->pci_dev, addr, &dma_altstat);
541
        if (dma_altstat & 8)
542
                return 0;        //return 1;
543
        return 0;
544
}
545
 
546
/**
547
 *      siimage_mmio_ide_dma_count      -       DMA bytes done
548
 *      @drive
549
 *
550
 *      If we are doing VDMA the CMD680 requires a little bit
551
 *      of more careful handling and we have to read the counts
552
 *      off ourselves. For non VDMA life is normal.
553
 */
554
 
555
static int siimage_mmio_ide_dma_count (ide_drive_t *drive)
556
{
557
#ifdef SIIMAGE_VIRTUAL_DMAPIO
558
        struct request *rq      = HWGROUP(drive)->rq;
559
        ide_hwif_t *hwif        = HWIF(drive);
560
        u32 count               = (rq->nr_sectors * SECTOR_SIZE);
561
        u32 rcount              = 0;
562
        unsigned long addr      = siimage_selreg(hwif, 0x1C);
563
 
564
        hwif->OUTL(count, addr);
565
        rcount = hwif->INL(addr);
566
 
567
        printk("\n%s: count = %d, rcount = %d, nr_sectors = %lu\n",
568
                drive->name, count, rcount, rq->nr_sectors);
569
 
570
#endif /* SIIMAGE_VIRTUAL_DMAPIO */
571
        return __ide_dma_count(drive);
572
}
573
 
574
/**
575
 *      siimage_mmio_ide_dma_test_irq   -       check we caused an IRQ
576
 *      @drive: drive we are testing
577
 *
578
 *      Check if we caused an IDE DMA interrupt. We may also have caused
579
 *      SATA status interrupts, if so we clean them up and continue.
580
 */
581
 
582
static int siimage_mmio_ide_dma_test_irq (ide_drive_t *drive)
583
{
584
        ide_hwif_t *hwif        = HWIF(drive);
585
        unsigned long base      = (unsigned long)hwif->hwif_data;
586
        unsigned long addr      = siimage_selreg(hwif, 0x1);
587
 
588
        if (SATA_ERROR_REG) {
589
                u32 ext_stat = hwif->INL(base + 0x10);
590
                u8 watchdog = 0;
591
                if (ext_stat & ((hwif->channel) ? 0x40 : 0x10)) {
592
                        u32 sata_error = hwif->INL(SATA_ERROR_REG);
593
                        hwif->OUTL(sata_error, SATA_ERROR_REG);
594
                        watchdog = (sata_error & 0x00680000) ? 1 : 0;
595
#if 1
596
                        printk(KERN_WARNING "%s: sata_error = 0x%08x, "
597
                                "watchdog = %d, %s\n",
598
                                drive->name, sata_error, watchdog,
599
                                __FUNCTION__);
600
#endif
601
 
602
                } else {
603
                        watchdog = (ext_stat & 0x8000) ? 1 : 0;
604
                }
605
                ext_stat >>= 16;
606
 
607
                if (!(ext_stat & 0x0404) && !watchdog)
608
                        return 0;
609
        }
610
 
611
        /* return 1 if INTR asserted */
612
        if ((hwif->INB(hwif->dma_status) & 0x04) == 0x04)
613
                return 1;
614
 
615
        /* return 1 if Device INTR asserted */
616
        if ((hwif->INB(addr) & 8) == 8)
617
                return 0;        //return 1;
618
 
619
        return 0;
620
}
621
 
622
static int siimage_mmio_ide_dma_verbose (ide_drive_t *drive)
623
{
624
        int temp = __ide_dma_verbose(drive);
625
        return temp;
626
}
627
 
628
/**
629
 *      siimage_busproc         -       bus isolation ioctl
630
 *      @drive: drive to isolate/restore
631
 *      @state: bus state to set
632
 *
633
 *      Used by the SII3112 to handle bus isolation. As this is a
634
 *      SATA controller the work required is quite limited, we
635
 *      just have to clean up the statistics
636
 */
637
 
638
static int siimage_busproc (ide_drive_t * drive, int state)
639
{
640
        ide_hwif_t *hwif        = HWIF(drive);
641
        u32 stat_config         = 0;
642
        unsigned long addr      = siimage_selreg(hwif, 0);
643
 
644
        if (hwif->mmio) {
645
                stat_config = hwif->INL(addr);
646
        } else
647
                pci_read_config_dword(hwif->pci_dev, addr, &stat_config);
648
 
649
        switch (state) {
650
                case BUSSTATE_ON:
651
                        hwif->drives[0].failures = 0;
652
                        hwif->drives[1].failures = 0;
653
                        break;
654
                case BUSSTATE_OFF:
655
                        hwif->drives[0].failures = hwif->drives[0].max_failures + 1;
656
                        hwif->drives[1].failures = hwif->drives[1].max_failures + 1;
657
                        break;
658
                case BUSSTATE_TRISTATE:
659
                        hwif->drives[0].failures = hwif->drives[0].max_failures + 1;
660
                        hwif->drives[1].failures = hwif->drives[1].max_failures + 1;
661
                        break;
662
                default:
663
                        return -EINVAL;
664
        }
665
        hwif->bus_state = state;
666
        return 0;
667
}
668
 
669
/**
670
 *      siimage_reset_poll      -       wait for sata reset
671
 *      @drive: drive we are resetting
672
 *
673
 *      Poll the SATA phy and see whether it has come back from the dead
674
 *      yet.
675
 */
676
 
677
static int siimage_reset_poll (ide_drive_t *drive)
678
{
679
        if (SATA_STATUS_REG) {
680
                ide_hwif_t *hwif        = HWIF(drive);
681
 
682
                if ((hwif->INL(SATA_STATUS_REG) & 0x03) != 0x03) {
683
                        printk(KERN_WARNING "%s: reset phy dead, status=0x%08x\n",
684
                                hwif->name, hwif->INL(SATA_STATUS_REG));
685
                        HWGROUP(drive)->poll_timeout = 0;
686
                        return ide_started;
687
                }
688
                return 0;
689
        } else {
690
                return 0;
691
        }
692
}
693
 
694
/**
695
 *      siimage_pre_reset       -       reset hook
696
 *      @drive: IDE device being reset
697
 *
698
 *      For the SATA devices we need to handle recalibration/geometry
699
 *      differently
700
 */
701
 
702
static void siimage_pre_reset (ide_drive_t *drive)
703
{
704
        if (drive->media != ide_disk)
705
                return;
706
 
707
        if (is_sata(HWIF(drive)))
708
        {
709
                drive->special.b.set_geometry = 0;
710
                drive->special.b.recalibrate = 0;
711
        }
712
}
713
 
714
/**
715
 *      siimage_reset   -       reset a device on an siimage controller
716
 *      @drive: drive to reset
717
 *
718
 *      Perform a controller level reset fo the device. For
719
 *      SATA we must also check the PHY.
720
 */
721
 
722
static void siimage_reset (ide_drive_t *drive)
723
{
724
        ide_hwif_t *hwif        = HWIF(drive);
725
        u8 reset                = 0;
726
        unsigned long addr      = siimage_selreg(hwif, 0);
727
 
728
        if (hwif->mmio) {
729
                reset = hwif->INB(addr);
730
                hwif->OUTB((reset|0x03), addr);
731
                /* FIXME:posting */
732
                udelay(25);
733
                hwif->OUTB(reset, addr);
734
                (void) hwif->INB(addr);
735
        } else {
736
                pci_read_config_byte(hwif->pci_dev, addr, &reset);
737
                pci_write_config_byte(hwif->pci_dev, addr, reset|0x03);
738
                udelay(25);
739
                pci_write_config_byte(hwif->pci_dev, addr, reset);
740
                pci_read_config_byte(hwif->pci_dev, addr, &reset);
741
        }
742
 
743
        if (SATA_STATUS_REG) {
744
                u32 sata_stat = hwif->INL(SATA_STATUS_REG);
745
                printk(KERN_WARNING "%s: reset phy, status=0x%08x, %s\n",
746
                        hwif->name, sata_stat, __FUNCTION__);
747
                if (!(sata_stat)) {
748
                        printk(KERN_WARNING "%s: reset phy dead, status=0x%08x\n",
749
                                hwif->name, sata_stat);
750
                        drive->failures++;
751
                }
752
        }
753
 
754
}
755
 
756
/**
757
 *      proc_reports_siimage            -       add siimage controller to proc
758
 *      @dev: PCI device
759
 *      @clocking: SCSC value
760
 *      @name: controller name
761
 *
762
 *      Report the clocking mode of the controller and add it to
763
 *      the /proc interface layer
764
 */
765
 
766
static void proc_reports_siimage (struct pci_dev *dev, u8 clocking, const char *name)
767
{
768
        if(pdev_is_sata(dev))
769
                goto sata_skip;
770
 
771
        printk(KERN_INFO "%s: BASE CLOCK ", name);
772
        clocking &= 0x03;
773
        switch(clocking) {
774
                case 0x03: printk("DISABLED !\n"); break;
775
                case 0x02: printk("== 2X PCI \n"); break;
776
                case 0x01: printk("== 133 \n"); break;
777
                case 0x00: printk("== 100 \n"); break;
778
        }
779
 
780
sata_skip:
781
 
782
#if defined(DISPLAY_SIIMAGE_TIMINGS) && defined(CONFIG_PROC_FS)
783
        siimage_devs[n_siimage_devs++] = dev;
784
 
785
        if (!siimage_proc) {
786
                siimage_proc = 1;
787
                ide_pci_register_host_proc(&siimage_procs[0]);
788
        }
789
#endif /* DISPLAY_SIIMAGE_TIMINGS && CONFIG_PROC_FS */
790
}
791
 
792
/**
793
 *      setup_mmio_siimage      -       switch an SI controller into MMIO
794
 *      @dev: PCI device we are configuring
795
 *      @name: device name
796
 *
797
 *      Attempt to put the device into mmio mode. There are some slight
798
 *      complications here with certain systems where the mmio bar isnt
799
 *      mapped so we have to be sure we can fall back to I/O.
800
 */
801
 
802
static unsigned int setup_mmio_siimage (struct pci_dev *dev, const char *name)
803
{
804
        unsigned long bar5      = pci_resource_start(dev, 5);
805
        unsigned long barsize   = pci_resource_len(dev, 5);
806
        u8 tmpbyte      = 0;
807
        unsigned long addr;
808
        void *ioaddr;
809
 
810
        /*
811
         *      Drop back to PIO if we can't map the mmio. Some
812
         *      systems seem to get terminally confused in the PCI
813
         *      spaces.
814
         */
815
 
816
        if(!request_mem_region(bar5, barsize, name))
817
        {
818
                printk(KERN_WARNING "siimage: IDE controller MMIO ports not available.\n");
819
                return 0;
820
        }
821
 
822
        ioaddr = ioremap(bar5, barsize);
823
 
824
        if (ioaddr == NULL)
825
        {
826
                release_mem_region(bar5, barsize);
827
                return 0;
828
        }
829
 
830
        pci_set_master(dev);
831
        pci_set_drvdata(dev, ioaddr);
832
        addr = (unsigned long) ioaddr;
833
 
834
        if (pdev_is_sata(dev)) {
835
                writel(0, addr + 0x148);
836
                writel(0, addr + 0x1C8);
837
        }
838
 
839
        writeb(0, addr + 0xB4);
840
        writeb(0, addr + 0xF4);
841
        tmpbyte = readb(addr + 0x4A);
842
 
843
        switch(tmpbyte & 0x30) {
844
                case 0x00:
845
                        /* In 100 MHz clocking, try and switch to 133 */
846
                        writeb(tmpbyte|0x10, addr + 0x4A);
847
                        break;
848
                case 0x10:
849
                        /* On 133Mhz clocking */
850
                        break;
851
                case 0x20:
852
                        /* On PCIx2 clocking */
853
                        break;
854
                case 0x30:
855
                        /* Clocking is disabled */
856
                        /* 133 clock attempt to force it on */
857
                        writeb(tmpbyte & ~0x20, addr + 0x4A);
858
                        break;
859
        }
860
 
861
        writeb(      0x72, addr + 0xA1);
862
        writew(    0x328A, addr + 0xA2);
863
        writel(0x62DD62DD, addr + 0xA4);
864
        writel(0x43924392, addr + 0xA8);
865
        writel(0x40094009, addr + 0xAC);
866
        writeb(      0x72, addr + 0xE1);
867
        writew(    0x328A, addr + 0xE2);
868
        writel(0x62DD62DD, addr + 0xE4);
869
        writel(0x43924392, addr + 0xE8);
870
        writel(0x40094009, addr + 0xEC);
871
 
872
        if (pdev_is_sata(dev)) {
873
                writel(0xFFFF0000, addr + 0x108);
874
                writel(0xFFFF0000, addr + 0x188);
875
                writel(0x00680000, addr + 0x148);
876
                writel(0x00680000, addr + 0x1C8);
877
        }
878
 
879
        tmpbyte = readb(addr + 0x4A);
880
 
881
        proc_reports_siimage(dev, (tmpbyte>>4), name);
882
        return 1;
883
}
884
 
885
/**
886
 *      init_chipset_siimage    -       set up an SI device
887
 *      @dev: PCI device
888
 *      @name: device name
889
 *
890
 *      Perform the initial PCI set up for this device. Attempt to switch
891
 *      to 133MHz clocking if the system isn't already set up to do it.
892
 */
893
 
894
static unsigned int __init init_chipset_siimage (struct pci_dev *dev, const char *name)
895
{
896
        u32 class_rev   = 0;
897
        u8 tmpbyte      = 0;
898
        u8 BA5_EN       = 0;
899
 
900
        pci_read_config_dword(dev, PCI_CLASS_REVISION, &class_rev);
901
        class_rev &= 0xff;
902
        pci_write_config_byte(dev, PCI_CACHE_LINE_SIZE, (class_rev) ? 1 : 255);
903
 
904
        pci_read_config_byte(dev, 0x8A, &BA5_EN);
905
        if ((BA5_EN & 0x01) || (pci_resource_start(dev, 5))) {
906
                if (setup_mmio_siimage(dev, name)) {
907
                        return 0;
908
                }
909
        }
910
 
911
        pci_write_config_byte(dev, 0x80, 0x00);
912
        pci_write_config_byte(dev, 0x84, 0x00);
913
        pci_read_config_byte(dev, 0x8A, &tmpbyte);
914
        switch(tmpbyte & 0x30) {
915
                case 0x00:
916
                        /* 133 clock attempt to force it on */
917
                        pci_write_config_byte(dev, 0x8A, tmpbyte|0x10);
918
                case 0x30:
919
                        /* if clocking is disabled */
920
                        /* 133 clock attempt to force it on */
921
                        pci_write_config_byte(dev, 0x8A, tmpbyte & ~0x20);
922
                case 0x10:
923
                        /* 133 already */
924
                        break;
925
                case 0x20:
926
                        /* BIOS set PCI x2 clocking */
927
                        break;
928
        }
929
 
930
        pci_read_config_byte(dev,   0x8A, &tmpbyte);
931
 
932
        pci_write_config_byte(dev,  0xA1, 0x72);
933
        pci_write_config_word(dev,  0xA2, 0x328A);
934
        pci_write_config_dword(dev, 0xA4, 0x62DD62DD);
935
        pci_write_config_dword(dev, 0xA8, 0x43924392);
936
        pci_write_config_dword(dev, 0xAC, 0x40094009);
937
        pci_write_config_byte(dev,  0xB1, 0x72);
938
        pci_write_config_word(dev,  0xB2, 0x328A);
939
        pci_write_config_dword(dev, 0xB4, 0x62DD62DD);
940
        pci_write_config_dword(dev, 0xB8, 0x43924392);
941
        pci_write_config_dword(dev, 0xBC, 0x40094009);
942
 
943
        proc_reports_siimage(dev, (tmpbyte>>4), name);
944
        return 0;
945
}
946
 
947
/**
948
 *      init_mmio_iops_siimage  -       set up the iops for MMIO
949
 *      @hwif: interface to set up
950
 *
951
 *      The basic setup here is fairly simple, we can use standard MMIO
952
 *      operations. However we do have to set the taskfile register offsets
953
 *      by hand as there isnt a standard defined layout for them this
954
 *      time.
955
 *
956
 *      The hardware supports buffered taskfiles and also some rather nice
957
 *      extended PRD tables. Unfortunately right now we don't.
958
 */
959
 
960
static void __init init_mmio_iops_siimage (ide_hwif_t *hwif)
961
{
962
        struct pci_dev *dev     = hwif->pci_dev;
963
        void *addr              = pci_get_drvdata(dev);
964
        u8 ch                   = hwif->channel;
965
        hw_regs_t               hw;
966
        unsigned long           base;
967
 
968
        /*
969
         *      Fill in the basic HWIF bits
970
         */
971
 
972
        default_hwif_mmiops(hwif);
973
        hwif->hwif_data                 = addr;
974
 
975
        /*
976
         *      Now set up the hw. We have to do this ourselves as
977
         *      the MMIO layout isnt the same as the the standard port
978
         *      based I/O
979
         */
980
 
981
        memset(&hw, 0, sizeof(hw_regs_t));
982
        hw.priv                         = addr;
983
 
984
        base                            = (unsigned long)addr;
985
        if(ch)
986
                base += 0xC0;
987
        else
988
                base += 0x80;
989
 
990
        /*
991
         *      The buffered task file doesn't have status/control
992
         *      so we can't currently use it sanely since we want to
993
         *      use LBA48 mode.
994
         */
995
//      base += 0x10;
996
//      hwif->addressing = 1;
997
 
998
        hw.io_ports[IDE_DATA_OFFSET]    = base;
999
        hw.io_ports[IDE_ERROR_OFFSET]   = base + 1;
1000
        hw.io_ports[IDE_NSECTOR_OFFSET] = base + 2;
1001
        hw.io_ports[IDE_SECTOR_OFFSET]  = base + 3;
1002
        hw.io_ports[IDE_LCYL_OFFSET]    = base + 4;
1003
        hw.io_ports[IDE_HCYL_OFFSET]    = base + 5;
1004
        hw.io_ports[IDE_SELECT_OFFSET]  = base + 6;
1005
        hw.io_ports[IDE_STATUS_OFFSET]  = base + 7;
1006
        hw.io_ports[IDE_CONTROL_OFFSET] = base + 10;
1007
 
1008
        hw.io_ports[IDE_IRQ_OFFSET]     = 0;
1009
 
1010
        if (pdev_is_sata(dev)) {
1011
                base = (unsigned long) addr;
1012
                if(ch)
1013
                        base += 0x80;
1014
                hw.sata_scr[SATA_STATUS_OFFSET] = base + 0x104;
1015
                hw.sata_scr[SATA_ERROR_OFFSET]  = base + 0x108;
1016
                hw.sata_scr[SATA_CONTROL_OFFSET]= base + 0x100;
1017
                hw.sata_misc[SATA_MISC_OFFSET]  = base + 0x140;
1018
                hw.sata_misc[SATA_PHY_OFFSET]   = base + 0x144;
1019
                hw.sata_misc[SATA_IEN_OFFSET]   = base + 0x148;
1020
        }
1021
 
1022
        hw.irq                          = hwif->pci_dev->irq;
1023
 
1024
        memcpy(&hwif->hw, &hw, sizeof(hw));
1025
        memcpy(hwif->io_ports, hwif->hw.io_ports, sizeof(hwif->hw.io_ports));
1026
 
1027
        if (is_sata(hwif)) {
1028
                memcpy(hwif->sata_scr, hwif->hw.sata_scr, sizeof(hwif->hw.sata_scr));
1029
                memcpy(hwif->sata_misc, hwif->hw.sata_misc, sizeof(hwif->hw.sata_misc));
1030
        }
1031
 
1032
        hwif->irq                       = hw.irq;
1033
 
1034
        base = (unsigned long) addr;
1035
 
1036
#ifdef SIIMAGE_LARGE_DMA
1037
/* Watch the brackets - even Ken and Dennis get some language design wrong */
1038
        hwif->dma_base                  = base + (ch ? 0x18 : 0x10);
1039
        hwif->dma_base2                 = base + (ch ? 0x08 : 0x00);
1040
        hwif->dma_prdtable              = hwif->dma_base2 + 4;
1041
#else /* ! SIIMAGE_LARGE_DMA */
1042
        hwif->dma_base                  = base + (ch ? 0x08 : 0x00);
1043
        hwif->dma_base2                 = base + (ch ? 0x18 : 0x10);
1044
#endif /* SIIMAGE_LARGE_DMA */
1045
        hwif->mmio                      = 2;
1046
}
1047
 
1048
/**
1049
 *      init_iops_siimage       -       set up iops
1050
 *      @hwif: interface to set up
1051
 *
1052
 *      Do the basic setup for the SIIMAGE hardware interface
1053
 *      and then do the MMIO setup if we can. This is the first
1054
 *      look in we get for setting up the hwif so that we
1055
 *      can get the iops right before using them.
1056
 */
1057
 
1058
static void __init init_iops_siimage (ide_hwif_t *hwif)
1059
{
1060
        struct pci_dev *dev     = hwif->pci_dev;
1061
        u32 class_rev           = 0;
1062
 
1063
        pci_read_config_dword(dev, PCI_CLASS_REVISION, &class_rev);
1064
        class_rev &= 0xff;
1065
 
1066
        hwif->hwif_data = 0;
1067
 
1068
        hwif->rqsize = 128;
1069
        if (is_sata(hwif))
1070
                hwif->rqsize = 15;
1071
 
1072
        if (pci_get_drvdata(dev) == NULL)
1073
                return;
1074
        init_mmio_iops_siimage(hwif);
1075
}
1076
 
1077
/**
1078
 *      ata66_siimage   -       check for 80 pin cable
1079
 *      @hwif: interface to check
1080
 *
1081
 *      Check for the presence of an ATA66 capable cable on the
1082
 *      interface.
1083
 */
1084
 
1085
static unsigned int __init ata66_siimage (ide_hwif_t *hwif)
1086
{
1087
        unsigned long addr = siimage_selreg(hwif, 0);
1088
        if (pci_get_drvdata(hwif->pci_dev) == NULL) {
1089
                u8 ata66 = 0;
1090
                pci_read_config_byte(hwif->pci_dev, addr, &ata66);
1091
                return (ata66 & 0x01) ? 1 : 0;
1092
        }
1093
 
1094
        return (hwif->INB(addr) & 0x01) ? 1 : 0;
1095
}
1096
 
1097
/**
1098
 *      init_hwif_siimage       -       set up hwif structs
1099
 *      @hwif: interface to set up
1100
 *
1101
 *      We do the basic set up of the interface structure. The SIIMAGE
1102
 *      requires several custom handlers so we override the default
1103
 *      ide DMA handlers appropriately
1104
 */
1105
 
1106
static void __init init_hwif_siimage (ide_hwif_t *hwif)
1107
{
1108
        hwif->autodma = 0;
1109
 
1110
        hwif->resetproc = &siimage_reset;
1111
        hwif->speedproc = &siimage_tune_chipset;
1112
        hwif->tuneproc  = &siimage_tuneproc;
1113
        hwif->reset_poll = &siimage_reset_poll;
1114
        hwif->pre_reset = &siimage_pre_reset;
1115
 
1116
        if(is_sata(hwif))
1117
        {
1118
                hwif->busproc   = &siimage_busproc;
1119
                hwif->sata = 1;
1120
        }
1121
 
1122
        if (!hwif->dma_base) {
1123
                hwif->drives[0].autotune = 1;
1124
                hwif->drives[1].autotune = 1;
1125
                return;
1126
        }
1127
 
1128
        hwif->ultra_mask = 0x7f;
1129
        hwif->mwdma_mask = 0x07;
1130
        hwif->swdma_mask = 0x07;
1131
 
1132
        if (!is_sata(hwif))
1133
                hwif->atapi_dma = 1;
1134
 
1135
        hwif->ide_dma_check = &siimage_config_drive_for_dma;
1136
        if (!(hwif->udma_four))
1137
                hwif->udma_four = ata66_siimage(hwif);
1138
 
1139
        if (hwif->mmio) {
1140
                hwif->ide_dma_count = &siimage_mmio_ide_dma_count;
1141
                hwif->ide_dma_test_irq = &siimage_mmio_ide_dma_test_irq;
1142
                hwif->ide_dma_verbose = &siimage_mmio_ide_dma_verbose;
1143
        } else {
1144
                hwif->ide_dma_test_irq = & siimage_io_ide_dma_test_irq;
1145
        }
1146
 
1147
        /*
1148
         *      The BIOS often doesn't set up DMA on this controller
1149
         *      so we always do it.
1150
         */
1151
 
1152
        hwif->autodma = 1;
1153
        hwif->drives[0].autodma = hwif->autodma;
1154
        hwif->drives[1].autodma = hwif->autodma;
1155
}
1156
 
1157
/**
1158
 *      init_dma_siimage        -       set up IDE DMA
1159
 *      @hwif: interface
1160
 *      @dmabase: DMA base address to use
1161
 *
1162
 *      For the SI chips this requires no special set up so we can just
1163
 *      let the IDE DMA core do the usual work.
1164
 */
1165
 
1166
static void __init init_dma_siimage (ide_hwif_t *hwif, unsigned long dmabase)
1167
{
1168
        ide_setup_dma(hwif, dmabase, 8);
1169
}
1170
 
1171
extern void ide_setup_pci_device(struct pci_dev *, ide_pci_device_t *);
1172
 
1173
 
1174
/**
1175
 *      siimage_init_one        -       pci layer discovery entry
1176
 *      @dev: PCI device
1177
 *      @id: ident table entry
1178
 *
1179
 *      Called by the PCI code when it finds an SI680 or SI3112 controller.
1180
 *      We then use the IDE PCI generic helper to do most of the work.
1181
 */
1182
 
1183
static int __devinit siimage_init_one(struct pci_dev *dev, const struct pci_device_id *id)
1184
{
1185
        ide_pci_device_t *d = &siimage_chipsets[id->driver_data];
1186
        if (dev->device != d->device)
1187
                BUG();
1188
        ide_setup_pci_device(dev, d);
1189
        MOD_INC_USE_COUNT;
1190
        return 0;
1191
}
1192
 
1193
static struct pci_device_id siimage_pci_tbl[] __devinitdata = {
1194
        { PCI_VENDOR_ID_CMD, PCI_DEVICE_ID_SII_680,  PCI_ANY_ID, PCI_ANY_ID, 0, 0, 0},
1195
        { PCI_VENDOR_ID_CMD, PCI_DEVICE_ID_SII_3112, PCI_ANY_ID, PCI_ANY_ID, 0, 0, 1},
1196
        { PCI_VENDOR_ID_CMD, PCI_DEVICE_ID_SII_1210SA, PCI_ANY_ID, PCI_ANY_ID, 0, 0, 2},
1197
        { 0, },
1198
};
1199
 
1200
static struct pci_driver driver = {
1201
        .name           = "SiI IDE",
1202
        .id_table       = siimage_pci_tbl,
1203
        .probe          = siimage_init_one,
1204
};
1205
 
1206
static int siimage_ide_init(void)
1207
{
1208
        return ide_pci_register_driver(&driver);
1209
}
1210
 
1211
static void siimage_ide_exit(void)
1212
{
1213
        ide_pci_unregister_driver(&driver);
1214
}
1215
 
1216
module_init(siimage_ide_init);
1217
module_exit(siimage_ide_exit);
1218
 
1219
MODULE_AUTHOR("Andre Hedrick, Alan Cox");
1220
MODULE_DESCRIPTION("PCI driver module for SiI IDE");
1221
MODULE_LICENSE("GPL");
1222
 
1223
EXPORT_NO_SYMBOLS;

powered by: WebSVN 2.1.0

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