--- linux-2.0.32/drivers/block/cypress.c.orig Mon Mar 30 12:49:00 1998 +++ linux-2.0.32/drivers/block/cypress.c Mon Mar 30 12:53:52 1998 @@ -0,0 +1,521 @@ +/* + * linux/drivers/block/cypress.c Version 1.0 Mar 29, 1998 + * + * Copyright (c) 1998 Andreas Johansson + * May be copied or modified under the terms of the GNU General Public License + */ + +/* + * This module provides support for the Bus Master IDE DMA function + * of the Cypress CY82C693U. Most of this code was borrowed from the + * Intel triton Bus Master IDE DMA driver, triton.c by Mark Lord. + * + * DMA is currently supported only for hard disk drives (not cdroms). + * + * Support for cdroms will likely be added at a later date, + * after broader experience has been obtained with hard disks. + * + * Up to four drives may be enabled for DMA, and the Cypress chipset will + * (hopefully) arbitrate the PCI bus among them. + * + * By default, DMA support is prepared for use, but is currently enabled only + * for drives which support multi-word DMA mode2 (mword2), or which are + * recognized as "good" (see table below). Drives with only mode0 or mode1 + * (single or multi) DMA should also work with this chipset/driver (eg. MC2112A) + * but are not enabled by default. Use "hdparm -i" to view modes supported + * by a given drive. + * + * The hdparm-2.4 (or later) utility can be used for manually enabling/disabling + * DMA support, but must be (re-)compiled against this kernel version or later. + * + * To enable DMA, use "hdparm -d1 /dev/hd?" on a per-drive basis after booting. + * If problems arise, ide.c will disable DMA operation after a few retries. + * This error recovery mechanism works and has been extremely well exercised. + * + * IDE drives, depending on their vintage, may support several different modes + * of DMA operation. The boot-time modes are indicated with a "*" in + * the "hdparm -i" listing, and can be changed with *knowledgeable* use of + * the "hdparm -X" feature. There is seldom a need to do this, as drives + * normally power-up with their "best" PIO/DMA modes enabled. + * + * Testing was done with an AlphaPC 164SX motherboard, but I'd like help with + * some more testing. Also see triton.c for some drive information. + * + * IBM Ultrastar Deskstar 5 (6197MB, 476kB Cache) + * - DMA mode 2 works fine (6.7MB/sec). Unfortunately I haven't been able + * to test the Ultra-ATA DMA mode with this drive yet, so I can't say + * if this is the limit of the drive. + * + * If you have any drive models to add, email your results to: + * ajo@wopr.campus.luth.se Keep an eye on /var/adm/messages for "DMA disabled" + * messages. + * + */ +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include + +#include +#include + +#include "ide.h" +#include "ide_modes.h" + +/*#define CYDEBUG printk*/ +#define CYDEBUG(x...) + +/* + * good_dma_drives() lists the model names (from "hdparm -i") + * of drives which do not support mword2 DMA but which are + * known to work fine with this interface under Linux. + */ +const char *good_dma_drives[] = {"Micropolis 2112A", + "CONNER CTMA 4000", + "CONNER CTT8000-A", + NULL}; + +/* + * Our Physical Region Descriptor (PRD) table should be large enough + * to handle the biggest I/O request we are likely to see. Since requests + * can have no more than 256 sectors, and since the typical blocksize is + * two sectors, we could get by with a limit of 128 entries here for the + * usual worst case. Most requests seem to include some contiguous blocks, + * further reducing the number of table entries required. + * + * The driver reverts to PIO mode for individual requests that exceed + * this limit (possible with 512 byte blocksizes, eg. MSDOS f/s), so handling + * 100% of all crazy scenarios here is not necessary. + * + * As it turns out though, we must allocate a full 4KB page for this, + * so the two PRD tables (ide0 & ide1) will each get half of that, + * allowing each to have about 256 entries (8 bytes each) from this. + */ +#define PRD_BYTES 8 +#define PRD_ENTRIES (PAGE_SIZE / (2 * PRD_BYTES)) +#define DEFAULT_BMIBA 0xe800 /* in case BIOS did not init it */ +#define CY_MAX_PIO_MODE 4 + +/* + * dma_intr() is the handler for disk read/write DMA interrupts + */ +static void dma_intr (ide_drive_t *drive) +{ + byte stat, dma_stat; + int i; + struct request *rq = HWGROUP(drive)->rq; + unsigned short dma_base = HWIF(drive)->dma_base; + + dma_stat = inb(dma_base+2); /* get DMA status */ + outb(inb(dma_base)&~1, dma_base); /* stop DMA operation */ + stat = GET_STAT(); /* get drive status */ + if (OK_STAT(stat,DRIVE_READY,drive->bad_wstat|DRQ_STAT)) { + if ((dma_stat & 7) == 4) { /* verify good DMA status */ + for (i = rq->nr_sectors; i > 0;) { + i -= rq->current_nr_sectors; + ide_end_request(1, HWGROUP(drive)); + } + return; + } + printk("%s: bad DMA status: 0x%02x\n", drive->name, dma_stat); + } + sti(); + ide_error(drive, "dma_intr", stat); +} + +/* + * build_dmatable() prepares a dma request. + * Returns 0 if all went okay, returns 1 otherwise. + */ +static int build_dmatable (ide_drive_t *drive) +{ + struct request *rq = HWGROUP(drive)->rq; + struct buffer_head *bh = rq->bh; + unsigned int size, addr, *table = (unsigned int *) HWIF(drive)->dmatable; + unsigned int count = 0; + + do { + /* + * Determine addr and size of next buffer area. We assume that + * individual virtual buffers are always composed linearly in + * physical memory. For example, we assume that any 8kB buffer + * is always composed of two adjacent physical 4kB pages rather + * than two possibly non-adjacent physical 4kB pages. + */ + if (bh == NULL) { /* paging and tape requests have (rq->bh == NULL) */ + addr = virt_to_bus (rq->buffer); +#ifdef CONFIG_BLK_DEV_IDETAPE + if (drive->media == ide_tape) + size = drive->tape.pc->request_transfer; + else +#endif /* CONFIG_BLK_DEV_IDETAPE */ + size = rq->nr_sectors << 9; + } else { + /* group sequential buffers into one large buffer */ + addr = virt_to_bus (bh->b_data); + size = bh->b_size; + while ((bh = bh->b_reqnext) != NULL) { + if ((addr + size) != virt_to_bus (bh->b_data)) + break; + size += bh->b_size; + } + } + /* + * Fill in the dma table, without crossing any 64kB boundaries. + * We assume 16-bit alignment of all blocks. + */ + while (size) { + if (++count >= PRD_ENTRIES) { + printk("%s: DMA table too small\n", drive->name); + return 1; /* revert to PIO for this request */ + } else { + unsigned long bcount = 0x10000 - (addr & 0xffff); + if (bcount > size) + bcount = size; + *table++ = addr; + /* Generate interrupt after each transfer so we don't tie + * the pcibus up for a too long time (to play dma sound :) + */ + *table++ = bcount & 0xffff; + addr += bcount; + size -= bcount; + } + } + } while (bh != NULL); + if (count) { + *--table |= 0x80000000; /* set End-Of-Table (EOT) bit */ + return 0; + } + printk("%s: empty DMA table?\n", drive->name); + return 1; /* let the PIO routines handle this weirdness */ +} + +static int config_drive_for_dma (ide_drive_t *drive) +{ + const char **list; + + struct hd_driveid *id = drive->id; + if (id && (id->capability & 1)) { + /* Enable DMA on any drive that supports mword2 DMA */ + if ((id->field_valid & 2) && (id->dma_mword & 0x404) == 0x404) { + drive->using_dma = 1; + return 0; /* DMA enabled */ + } + /* Consult the list of known "good" drives */ + list = good_dma_drives; + while (*list) { + if (!strcmp(*list++,id->model)) { + drive->using_dma = 1; + return 0; /* DMA enabled */ + } + } + } + return 1; /* DMA not enabled */ +} + +/* + * cypress_dmaproc() initiates/aborts DMA read/write operations on a drive. + * + * The caller is assumed to have selected the drive and programmed the drive's + * sector address using CHS or LBA. All that remains is to prepare for DMA + * and then issue the actual read/write DMA/PIO command to the drive. + * + * For ATAPI devices, we just prepare for DMA and return. The caller should + * then issue the packet command to the drive and call us again with + * ide_dma_begin afterwards. + * + * Returns 0 if all went well. + * Returns 1 if DMA read/write could not be started, in which case + * the caller should revert to PIO for the current request. + */ +static int cypress_dmaproc (ide_dma_action_t func, ide_drive_t *drive) +{ + unsigned long dma_base = HWIF(drive)->dma_base; + unsigned int reading = (1 << 3); + + switch (func) { + case ide_dma_abort: + outb(inb(dma_base)&~1, dma_base); /* stop DMA */ + return 0; + case ide_dma_check: + return config_drive_for_dma (drive); + case ide_dma_write: + reading = 0; + case ide_dma_read: + break; + case ide_dma_status_bad: + return ((inb(dma_base+2) & 7) != 4); /* verify good DMA status */ + case ide_dma_transferred: +#if 0 + return (number of bytes actually transferred); +#else + return (0); +#endif + case ide_dma_begin: + outb(inb(dma_base)|1, dma_base); /* begin DMA */ + return 0; + default: + printk("cypress_dmaproc: unsupported func: %d\n", func); + return 1; + } + if (build_dmatable (drive)) + return 1; + + outl(virt_to_bus (HWIF(drive)->dmatable), dma_base + 4); /* PRD table */ + outb(reading, dma_base); /* specify r/w */ + outb(inb(dma_base+2)|0x06, dma_base+2); /* clear status bits */ +#ifdef CONFIG_BLK_DEV_IDEATAPI + if (drive->media != ide_disk) + return 0; +#endif /* CONFIG_BLK_DEV_IDEATAPI */ + ide_set_handler(drive, &dma_intr, WAIT_CMD); /* issue cmd to drive */ + OUT_BYTE(reading ? WIN_READDMA : WIN_WRITEDMA, IDE_COMMAND_REG); + outb(inb(dma_base)|1, dma_base); /* begin DMA */ + return 0; +} + +static void init_cypress_dma (ide_hwif_t *hwif, unsigned short base) +{ + static unsigned long dmatable = 0; + + printk(" %s: BM-DMA at 0x%04x-0x%04x", hwif->name, base, base+7); + if (check_region(base, 8)) { + printk(" -- ERROR, PORTS ALREADY IN USE"); + } else { + request_region(base, 8, "IDE DMA"); + hwif->dma_base = base; + if (!dmatable) { + /* + * The BM-DMA uses a full 32-bits, so we can + * safely use __get_free_page() here instead + * of __get_dma_pages() -- no ISA limitations. + */ + dmatable = __get_free_page(GFP_KERNEL); + } + if (dmatable) { + hwif->dmatable = (unsigned long *) dmatable; + dmatable += (PRD_ENTRIES * PRD_BYTES); + outl(virt_to_bus(hwif->dmatable), base + 4); + hwif->dmaproc = &cypress_dmaproc; + } + } + printk("\n"); +} + +static byte cypress_drive_pio_modes[4] = {0,0,0,0}; +static int cypress_bus = -1, cypress_fn = -1; + +/* + * Setup the pci registers concerning pio timing in the + * Cypress device for use with the desired mode + */ +static int setup_cypress_timing (int bus, int fn, byte m_mode, byte s_mode) +{ + int rc; + int bus_speed = 30; /* 33MHz pci bus speed in ns */ + /* Initialize below only to keep compiler happy. Shouldn't be needed */ + int m_setup_time = 0, m_active_time = 0, m_recovery_time = 0; + int s_setup_time = 0, s_active_time = 0, s_recovery_time = 0; + int iordy = 5; + + if (m_mode > CY_MAX_PIO_MODE) m_mode = CY_MAX_PIO_MODE; + if (s_mode > CY_MAX_PIO_MODE) s_mode = CY_MAX_PIO_MODE; + + m_setup_time = ide_pio_timings[m_mode].setup_time / bus_speed; + m_active_time = ide_pio_timings[m_mode].active_time / bus_speed; + m_recovery_time = (ide_pio_timings[m_mode].cycle_time - + ide_pio_timings[m_mode].active_time - + ide_pio_timings[m_mode].setup_time ) / bus_speed; + + s_setup_time = ide_pio_timings[s_mode].setup_time / bus_speed; + s_active_time = ide_pio_timings[s_mode].active_time / bus_speed; + s_recovery_time = (ide_pio_timings[s_mode].cycle_time - + ide_pio_timings[s_mode].active_time - + ide_pio_timings[s_mode].setup_time ) / bus_speed; + + CYDEBUG ("m: setup %d active %d recover %d\n", + m_setup_time, m_active_time, m_recovery_time); + CYDEBUG ("s: setup %d active %d recover %d\n", + s_setup_time, s_active_time, s_recovery_time); + + if (m_setup_time > 0xf) m_setup_time = 0xf; + if (m_active_time > 0xf) m_active_time = 0xf; + if (m_recovery_time > 0xf) m_recovery_time = 0xf; + + if (s_setup_time > 0xf) s_setup_time = 0xf; + if (s_active_time > 0xf) s_active_time = 0xf; + if (s_recovery_time > 0xf) s_recovery_time = 0xf; + + /* All of the below setup are added with one before use. */ + + /* Address setup time, slave/master (default 3/3) in pci clocks */ + if ((rc = pcibios_write_config_dword(bus, fn, 0x48, s_setup_time << 4 | m_setup_time))) + goto quit; + + /* Master 16 bit IOR Command pulse width/recover time (default 3/3) in pci clocks */ + if ((rc = pcibios_write_config_word(bus, fn, 0x4c, m_active_time << 4 | m_recovery_time))) + goto quit; + + /* Master 16 bit IOW Command pulse width/recover time (default 5/13) in pci clocks */ + if ((rc = pcibios_write_config_word(bus, fn, 0x4d, m_active_time << 4 | m_recovery_time))) + goto quit; + + /* Slave 16 bit IOR Command pulse width/recover time (default 3/3) in pci clocks */ + if ((rc = pcibios_write_config_word(bus, fn, 0x4e, s_active_time << 4 | s_recovery_time))) + goto quit; + + /* Slave 16 bit IOW Command pulse width/recover time (default 5/13) in pci clocks */ + if ((rc = pcibios_write_config_word(bus, fn, 0x4f, s_active_time << 4 | s_recovery_time))) + goto quit; + + + /* Master/slave 8 bit IOR Command pulse width/recover time + (default 9/9) in pci clocks */ + if ((rc = pcibios_write_config_word(bus, fn, 0x50, 0x99))) + goto quit; + + /* Master/slave 8 bit IOW Command pulse width/recover time + (default 9/9) in pci clocks */ + if ((rc = pcibios_write_config_word(bus, fn, 0x51, 0x99))) + goto quit; + + /* Primary IORDY Read/Write synch time (default 1/1) in pci clocks */ + if ((rc = pcibios_write_config_word(bus, fn, 0x52, iordy))) + goto quit; + + /* Slave IORDY Read/Write synch time (1/1) in pci clocks */ + if ((rc = pcibios_write_config_word(bus, fn, 0x53, iordy))) + goto quit; + + + quit: + return rc; +} + +static void cypress_tune_drive (ide_drive_t *drive, byte pio) +{ + int driveNum; + unsigned long flags; + ide_pio_data_t d; + + pio = ide_get_best_pio_mode(drive, pio, CY_MAX_PIO_MODE, &d); + + driveNum = (HWIF(drive)->index << 1) + drive->select.b.unit; + CYDEBUG ("Drive %d changed to pio %d\n", driveNum, pio); + cypress_drive_pio_modes[driveNum] = pio; + + save_flags (flags); + cli(); + if (driveNum < 2) + /* Setup timings for primary controller */ + setup_cypress_timing (cypress_bus, cypress_fn, cypress_drive_pio_modes[0], + cypress_drive_pio_modes[1]); + else + /* Setup timings for secondary controller */ + setup_cypress_timing (cypress_bus, cypress_fn+1, cypress_drive_pio_modes[2], + cypress_drive_pio_modes[3]); + + restore_flags (flags); +} + +/* + * ide_init_cypress() prepares the IDE driver for DMA operation. + * This routine is called once, from ide.c during driver initialization, + * for each cypress chipset which is found (unlikely to be more than one). + */ +void ide_init_cypress (byte bus, byte fn) +{ + int rc = 0, h; + int dma_enabled = 0; + unsigned short pcicmd; + unsigned int bmiba; + + /* + * Only try the primary IDE function (1) in the cypress device. + * Also only accept one Cypress ide device XXX + */ + if ((fn & 7) != 1 || cypress_bus != -1) + return; + + cypress_bus = bus; + cypress_fn = fn; + + printk("ide: Cypress CY82C693U on PCI bus %d function %d\n", bus, fn); + /* + * See if IDE and BM-DMA features are enabled: + */ + if ((rc = pcibios_read_config_word(bus, fn, 0x04, &pcicmd))) + goto quit; + if ((pcicmd & 1) == 0) { + printk("ide: ports are not enabled (BIOS)\n"); + goto quit; + } + if ((pcicmd & 4) == 0) { + printk("ide: BM-DMA feature is not enabled (BIOS)\n"); + } else { + /* + * Get the bmiba base address + */ + int try_again = 1; + + /* Always use default base address, this could be done better */ + rc = pcibios_write_config_dword(bus, fn, 0x20, DEFAULT_BMIBA|1); + do { + if ((rc = pcibios_read_config_dword(bus, fn, 0x20, &bmiba))) + goto quit; + bmiba &= 0xfff0; /* extract port base address */ + if (bmiba) { + dma_enabled = 1; + break; + } else { + printk("ide: BM-DMA base register is invalid (0x%04x, PnP BIOS problem)\n", bmiba); + if (inb(DEFAULT_BMIBA) != 0xff || !try_again) + break; + printk("ide: setting BM-DMA base register to 0x%04x\n", DEFAULT_BMIBA); + if ((rc = pcibios_write_config_word(bus, fn, 0x04, pcicmd&~1))) + goto quit; + rc = pcibios_write_config_dword(bus, fn, 0x20, DEFAULT_BMIBA|1); + if (pcibios_write_config_word(bus, fn, 0x04, pcicmd|5) || rc) + goto quit; + } + } while (try_again--); + } + + /* + * Save the dma_base port addr for each interface + */ + for (h = 0; h < MAX_HWIFS; ++h) { + ide_hwif_t *hwif = &ide_hwifs[h]; + if (hwif->io_base == 0x1f0) { + hwif->chipset = ide_cypress; + hwif->tuneproc = cypress_tune_drive; + if (dma_enabled) + init_cypress_dma(hwif, bmiba); + } else if (hwif->io_base == 0x170) { + hwif->chipset = ide_cypress; + hwif->tuneproc = cypress_tune_drive; + if (dma_enabled) + init_cypress_dma(hwif, bmiba + 8); + } else + continue; + } + + /* Setup timings for primary controller */ + if ((rc = setup_cypress_timing (bus, fn, cypress_drive_pio_modes[0], + cypress_drive_pio_modes[1]))) + goto quit; + /* Setup timings for secondary controller */ + if ((rc = setup_cypress_timing (bus, fn+1, cypress_drive_pio_modes[2], + cypress_drive_pio_modes[3]))) + goto quit; + +quit: if (rc) printk("ide: pcibios access failed - %s\n", pcibios_strerror(rc)); +} + --- linux-2.0.32/drivers/block/ide.c.orig Wed Mar 18 02:54:10 1998 +++ linux-2.0.32/drivers/block/ide.c Sun Mar 29 22:56:23 1998 @@ -1464,19 +1464,19 @@ } #endif /* CONFIG_BLK_DEV_PROMISE */ if (rq->cmd == READ) { -#ifdef CONFIG_BLK_DEV_TRITON +#if defined(CONFIG_BLK_DEV_TRITON) || defined (CONFIG_BLK_DEV_CYPRESS) if (drive->using_dma && !(HWIF(drive)->dmaproc(ide_dma_read, drive))) return; -#endif /* CONFIG_BLK_DEV_TRITON */ +#endif /* CONFIG_BLK_DEV_TRITON || CONFIG_BLK_DEV_CYPRESS */ ide_set_handler(drive, &read_intr, WAIT_CMD); OUT_BYTE(drive->mult_count ? WIN_MULTREAD : WIN_READ, io_base+IDE_COMMAND_OFFSET); return; } if (rq->cmd == WRITE) { -#ifdef CONFIG_BLK_DEV_TRITON +#if defined(CONFIG_BLK_DEV_TRITON) || defined (CONFIG_BLK_DEV_CYPRESS) if (drive->using_dma && !(HWIF(drive)->dmaproc(ide_dma_write, drive))) return; -#endif /* CONFIG_BLK_DEV_TRITON */ +#endif /* CONFIG_BLK_DEV_TRITON || CONFIG_BLK_DEV_CYPRESS */ OUT_BYTE(drive->mult_count ? WIN_MULTWRITE : WIN_WRITE, io_base+IDE_COMMAND_OFFSET); if (ide_wait_stat(drive, DATA_READY, drive->bad_wstat, WAIT_DRQ)) { printk("%s: no DRQ after issuing %s\n", drive->name, @@ -3475,7 +3475,7 @@ }; #ifdef CONFIG_PCI -#if defined(CONFIG_BLK_DEV_RZ1000) || defined(CONFIG_BLK_DEV_TRITON) +#if defined(CONFIG_BLK_DEV_RZ1000) || defined(CONFIG_BLK_DEV_TRITON) || defined(CONFIG_BLK_DEV_CYPRESS) typedef void (ide_pci_init_proc_t)(byte, byte); @@ -3497,7 +3497,7 @@ restore_flags(flags); } -#endif /* defined(CONFIG_BLK_DEV_RZ1000) || defined(CONFIG_BLK_DEV_TRITON) */ +#endif /* defined(CONFIG_BLK_DEV_RZ1000) || defined(CONFIG_BLK_DEV_TRITON) || defined(CONFIG_BLK_DEV_CYPRESS) */ #endif /* CONFIG_PCI */ /* @@ -3529,6 +3529,9 @@ ide_probe_pci (PCI_VENDOR_ID_INTEL, PCI_DEVICE_ID_INTEL_82371SB_1, &ide_init_triton, 0); ide_probe_pci (PCI_VENDOR_ID_INTEL, PCI_DEVICE_ID_INTEL_82371AB, &ide_init_triton, 0); #endif /* CONFIG_BLK_DEV_TRITON */ +#ifdef CONFIG_BLK_DEV_CYPRESS + ide_probe_pci (PCI_VENDOR_ID_CONTAQ, PCI_DEVICE_ID_CONTAQ_82C693, &ide_init_cypress, 0); +#endif /* CONFIG_BLK_DEV_CYPRESS */ } #endif /* CONFIG_PCI */ #ifdef CONFIG_BLK_DEV_CMD640 --- linux-2.0.32/drivers/block/Config.in.orig Mon Aug 4 20:45:55 1997 +++ linux-2.0.32/drivers/block/Config.in Sun Mar 29 22:38:45 1998 @@ -23,6 +23,7 @@ if [ "$CONFIG_PCI" = "y" ]; then bool ' RZ1000 chipset bugfix/support' CONFIG_BLK_DEV_RZ1000 bool ' Intel 82371 PIIX (Triton I/II) DMA support' CONFIG_BLK_DEV_TRITON + bool ' Cypress CY82C693U IDE support' CONFIG_BLK_DEV_CYPRESS fi bool ' Other IDE chipset support' CONFIG_IDE_CHIPSETS if [ "$CONFIG_IDE_CHIPSETS" = "y" ]; then --- linux-2.0.32/drivers/block/ide.h.orig Thu Mar 19 23:59:26 1998 +++ linux-2.0.32/drivers/block/ide.h Sun Mar 29 22:55:49 1998 @@ -424,7 +424,7 @@ typedef enum { ide_unknown, ide_generic, ide_triton, ide_cmd640, ide_dtc2278, ide_ali14xx, ide_qd6580, ide_umc8672, ide_ht6560b, - ide_promise } + ide_promise, ide_cypress } hwif_chipset_t; typedef struct hwif_s { @@ -739,3 +739,6 @@ #ifdef CONFIG_BLK_DEV_TRITON void ide_init_triton (byte, byte); #endif /* CONFIG_BLK_DEV_TRITON */ +#ifdef CONFIG_BLK_DEV_CYPRESS +void ide_init_cypress (byte, byte); +#endif /* CONFIG_BLK_DEV_CYPRESS */