Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1 | /* |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2 | * Copyright (C) 1998-2000 Andreas S. Krebs (akrebs@altavista.net), Maintainer |
| 3 | * Copyright (C) 1998-2002 Andre Hedrick <andre@linux-ide.org>, Integrator |
Bartlomiej Zolnierkiewicz | 0ab3d8b | 2011-10-13 00:28:54 +0000 | [diff] [blame] | 4 | * Copyright (C) 2007-2011 Bartlomiej Zolnierkiewicz |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5 | * |
| 6 | * CYPRESS CY82C693 chipset IDE controller |
| 7 | * |
| 8 | * The CY82C693 chipset is used on Digital's PC-Alpha 164SX boards. |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 9 | */ |
| 10 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 11 | #include <linux/module.h> |
| 12 | #include <linux/types.h> |
| 13 | #include <linux/pci.h> |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 14 | #include <linux/ide.h> |
| 15 | #include <linux/init.h> |
| 16 | |
| 17 | #include <asm/io.h> |
| 18 | |
Bartlomiej Zolnierkiewicz | ced3ec8 | 2008-07-24 22:53:32 +0200 | [diff] [blame] | 19 | #define DRV_NAME "cy82c693" |
| 20 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 21 | /* |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 22 | * NOTE: the value for busmaster timeout is tricky and I got it by |
| 23 | * trial and error! By using a to low value will cause DMA timeouts |
| 24 | * and drop IDE performance, and by using a to high value will cause |
| 25 | * audio playback to scatter. |
| 26 | * If you know a better value or how to calc it, please let me know. |
| 27 | */ |
| 28 | |
| 29 | /* twice the value written in cy82c693ub datasheet */ |
| 30 | #define BUSMASTER_TIMEOUT 0x50 |
| 31 | /* |
| 32 | * the value above was tested on my machine and it seems to work okay |
| 33 | */ |
| 34 | |
| 35 | /* here are the offset definitions for the registers */ |
| 36 | #define CY82_IDE_CMDREG 0x04 |
| 37 | #define CY82_IDE_ADDRSETUP 0x48 |
| 38 | #define CY82_IDE_MASTER_IOR 0x4C |
| 39 | #define CY82_IDE_MASTER_IOW 0x4D |
| 40 | #define CY82_IDE_SLAVE_IOR 0x4E |
| 41 | #define CY82_IDE_SLAVE_IOW 0x4F |
| 42 | #define CY82_IDE_MASTER_8BIT 0x50 |
| 43 | #define CY82_IDE_SLAVE_8BIT 0x51 |
| 44 | |
| 45 | #define CY82_INDEX_PORT 0x22 |
| 46 | #define CY82_DATA_PORT 0x23 |
| 47 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 48 | #define CY82_INDEX_CHANNEL0 0x30 |
| 49 | #define CY82_INDEX_CHANNEL1 0x31 |
| 50 | #define CY82_INDEX_TIMEOUT 0x32 |
| 51 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 52 | /* |
| 53 | * set DMA mode a specific channel for CY82C693 |
| 54 | */ |
| 55 | |
Bartlomiej Zolnierkiewicz | 8776168 | 2010-01-19 01:45:29 -0800 | [diff] [blame] | 56 | static void cy82c693_set_dma_mode(ide_hwif_t *hwif, ide_drive_t *drive) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 57 | { |
Bartlomiej Zolnierkiewicz | 8776168 | 2010-01-19 01:45:29 -0800 | [diff] [blame] | 58 | const u8 mode = drive->dma_mode; |
Bartlomiej Zolnierkiewicz | 8704de8 | 2008-01-26 20:13:00 +0100 | [diff] [blame] | 59 | u8 single = (mode & 0x10) >> 4, index = 0, data = 0; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 60 | |
Bartlomiej Zolnierkiewicz | 8704de8 | 2008-01-26 20:13:00 +0100 | [diff] [blame] | 61 | index = hwif->channel ? CY82_INDEX_CHANNEL1 : CY82_INDEX_CHANNEL0; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 62 | |
Bartlomiej Zolnierkiewicz | 8704de8 | 2008-01-26 20:13:00 +0100 | [diff] [blame] | 63 | data = (mode & 3) | (single << 2); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 64 | |
Bartlomiej Zolnierkiewicz | 0ecdca2 | 2007-02-17 02:40:25 +0100 | [diff] [blame] | 65 | outb(index, CY82_INDEX_PORT); |
| 66 | outb(data, CY82_DATA_PORT); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 67 | |
Paolo Ciarrocchi | 175f354 | 2008-04-26 17:36:42 +0200 | [diff] [blame] | 68 | /* |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 69 | * note: below we set the value for Bus Master IDE TimeOut Register |
Lucas De Marchi | 25985ed | 2011-03-30 22:57:33 -0300 | [diff] [blame] | 70 | * I'm not absolutely sure what this does, but it solved my problem |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 71 | * with IDE DMA and sound, so I now can play sound and work with |
| 72 | * my IDE driver at the same time :-) |
| 73 | * |
| 74 | * If you know the correct (best) value for this register please |
| 75 | * let me know - ASK |
| 76 | */ |
| 77 | |
| 78 | data = BUSMASTER_TIMEOUT; |
Bartlomiej Zolnierkiewicz | 0ecdca2 | 2007-02-17 02:40:25 +0100 | [diff] [blame] | 79 | outb(CY82_INDEX_TIMEOUT, CY82_INDEX_PORT); |
| 80 | outb(data, CY82_DATA_PORT); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 81 | } |
| 82 | |
Bartlomiej Zolnierkiewicz | e085b3c | 2010-01-19 01:44:41 -0800 | [diff] [blame] | 83 | static void cy82c693_set_pio_mode(ide_hwif_t *hwif, ide_drive_t *drive) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 84 | { |
Bartlomiej Zolnierkiewicz | 3650165 | 2008-02-01 23:09:31 +0100 | [diff] [blame] | 85 | struct pci_dev *dev = to_pci_dev(hwif->dev); |
Bartlomiej Zolnierkiewicz | 4d6b328 | 2010-01-18 07:18:47 +0000 | [diff] [blame] | 86 | int bus_speed = ide_pci_clk ? ide_pci_clk : 33; |
| 87 | const unsigned long T = 1000000 / bus_speed; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 88 | unsigned int addrCtrl; |
Bartlomiej Zolnierkiewicz | 4d6b328 | 2010-01-18 07:18:47 +0000 | [diff] [blame] | 89 | struct ide_timing t; |
| 90 | u8 time_16, time_8; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 91 | |
| 92 | /* select primary or secondary channel */ |
Bartlomiej Zolnierkiewicz | 0ab3d8b | 2011-10-13 00:28:54 +0000 | [diff] [blame] | 93 | if (drive->dn > 1) { /* drive is on the secondary channel */ |
Alan Cox | 652aa16 | 2006-10-03 01:14:35 -0700 | [diff] [blame] | 94 | dev = pci_get_slot(dev->bus, dev->devfn+1); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 95 | if (!dev) { |
| 96 | printk(KERN_ERR "%s: tune_drive: " |
| 97 | "Cannot find secondary interface!\n", |
| 98 | drive->name); |
| 99 | return; |
| 100 | } |
| 101 | } |
| 102 | |
Bartlomiej Zolnierkiewicz | e085b3c | 2010-01-19 01:44:41 -0800 | [diff] [blame] | 103 | ide_timing_compute(drive, drive->pio_mode, &t, T, 1); |
Bartlomiej Zolnierkiewicz | 4d6b328 | 2010-01-18 07:18:47 +0000 | [diff] [blame] | 104 | |
| 105 | time_16 = clamp_val(t.recover - 1, 0, 15) | |
| 106 | (clamp_val(t.active - 1, 0, 15) << 4); |
| 107 | time_8 = clamp_val(t.act8b - 1, 0, 15) | |
| 108 | (clamp_val(t.rec8b - 1, 0, 15) << 4); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 109 | |
| 110 | /* now let's write the clocks registers */ |
Bartlomiej Zolnierkiewicz | 123995b | 2008-10-13 21:39:40 +0200 | [diff] [blame] | 111 | if ((drive->dn & 1) == 0) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 112 | /* |
| 113 | * set master drive |
| 114 | * address setup control register |
| 115 | * is 32 bit !!! |
Paolo Ciarrocchi | 175f354 | 2008-04-26 17:36:42 +0200 | [diff] [blame] | 116 | */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 117 | pci_read_config_dword(dev, CY82_IDE_ADDRSETUP, &addrCtrl); |
Paolo Ciarrocchi | 175f354 | 2008-04-26 17:36:42 +0200 | [diff] [blame] | 118 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 119 | addrCtrl &= (~0xF); |
Bartlomiej Zolnierkiewicz | 4d6b328 | 2010-01-18 07:18:47 +0000 | [diff] [blame] | 120 | addrCtrl |= clamp_val(t.setup - 1, 0, 15); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 121 | pci_write_config_dword(dev, CY82_IDE_ADDRSETUP, addrCtrl); |
| 122 | |
| 123 | /* now let's set the remaining registers */ |
Bartlomiej Zolnierkiewicz | 4d6b328 | 2010-01-18 07:18:47 +0000 | [diff] [blame] | 124 | pci_write_config_byte(dev, CY82_IDE_MASTER_IOR, time_16); |
| 125 | pci_write_config_byte(dev, CY82_IDE_MASTER_IOW, time_16); |
| 126 | pci_write_config_byte(dev, CY82_IDE_MASTER_8BIT, time_8); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 127 | } else { |
| 128 | /* |
| 129 | * set slave drive |
| 130 | * address setup control register |
| 131 | * is 32 bit !!! |
Paolo Ciarrocchi | 175f354 | 2008-04-26 17:36:42 +0200 | [diff] [blame] | 132 | */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 133 | pci_read_config_dword(dev, CY82_IDE_ADDRSETUP, &addrCtrl); |
| 134 | |
| 135 | addrCtrl &= (~0xF0); |
Bartlomiej Zolnierkiewicz | 4d6b328 | 2010-01-18 07:18:47 +0000 | [diff] [blame] | 136 | addrCtrl |= (clamp_val(t.setup - 1, 0, 15) << 4); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 137 | pci_write_config_dword(dev, CY82_IDE_ADDRSETUP, addrCtrl); |
| 138 | |
| 139 | /* now let's set the remaining registers */ |
Bartlomiej Zolnierkiewicz | 4d6b328 | 2010-01-18 07:18:47 +0000 | [diff] [blame] | 140 | pci_write_config_byte(dev, CY82_IDE_SLAVE_IOR, time_16); |
| 141 | pci_write_config_byte(dev, CY82_IDE_SLAVE_IOW, time_16); |
| 142 | pci_write_config_byte(dev, CY82_IDE_SLAVE_8BIT, time_8); |
Paolo Ciarrocchi | 175f354 | 2008-04-26 17:36:42 +0200 | [diff] [blame] | 143 | } |
Bartlomiej Zolnierkiewicz | 0ab3d8b | 2011-10-13 00:28:54 +0000 | [diff] [blame] | 144 | if (drive->dn > 1) |
Julia Lawall | 0302899 | 2011-08-04 01:30:34 -0700 | [diff] [blame] | 145 | pci_dev_put(dev); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 146 | } |
| 147 | |
Greg Kroah-Hartman | fe31edc | 2012-12-21 13:21:03 -0800 | [diff] [blame] | 148 | static void init_iops_cy82c693(ide_hwif_t *hwif) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 149 | { |
Bartlomiej Zolnierkiewicz | f32d26a | 2007-10-26 20:31:15 +0200 | [diff] [blame] | 150 | static ide_hwif_t *primary; |
Bartlomiej Zolnierkiewicz | 3650165 | 2008-02-01 23:09:31 +0100 | [diff] [blame] | 151 | struct pci_dev *dev = to_pci_dev(hwif->dev); |
Bartlomiej Zolnierkiewicz | f32d26a | 2007-10-26 20:31:15 +0200 | [diff] [blame] | 152 | |
Bartlomiej Zolnierkiewicz | 3650165 | 2008-02-01 23:09:31 +0100 | [diff] [blame] | 153 | if (PCI_FUNC(dev->devfn) == 1) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 154 | primary = hwif; |
| 155 | else { |
| 156 | hwif->mate = primary; |
| 157 | hwif->channel = 1; |
| 158 | } |
| 159 | } |
| 160 | |
Bartlomiej Zolnierkiewicz | ac95bee | 2008-04-26 22:25:14 +0200 | [diff] [blame] | 161 | static const struct ide_port_ops cy82c693_port_ops = { |
| 162 | .set_pio_mode = cy82c693_set_pio_mode, |
| 163 | .set_dma_mode = cy82c693_set_dma_mode, |
| 164 | }; |
| 165 | |
Greg Kroah-Hartman | fe31edc | 2012-12-21 13:21:03 -0800 | [diff] [blame] | 166 | static const struct ide_port_info cy82c693_chipset = { |
Bartlomiej Zolnierkiewicz | ced3ec8 | 2008-07-24 22:53:32 +0200 | [diff] [blame] | 167 | .name = DRV_NAME, |
Bartlomiej Zolnierkiewicz | 7b77d86 | 2007-02-17 02:40:24 +0100 | [diff] [blame] | 168 | .init_iops = init_iops_cy82c693, |
Bartlomiej Zolnierkiewicz | ac95bee | 2008-04-26 22:25:14 +0200 | [diff] [blame] | 169 | .port_ops = &cy82c693_port_ops, |
Bartlomiej Zolnierkiewicz | 951784b | 2008-04-26 17:36:38 +0200 | [diff] [blame] | 170 | .host_flags = IDE_HFLAG_SINGLE, |
Bartlomiej Zolnierkiewicz | 4099d14 | 2007-07-20 01:11:59 +0200 | [diff] [blame] | 171 | .pio_mask = ATA_PIO4, |
Bartlomiej Zolnierkiewicz | 8704de8 | 2008-01-26 20:13:00 +0100 | [diff] [blame] | 172 | .swdma_mask = ATA_SWDMA2, |
| 173 | .mwdma_mask = ATA_MWDMA2, |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 174 | }; |
| 175 | |
Greg Kroah-Hartman | fe31edc | 2012-12-21 13:21:03 -0800 | [diff] [blame] | 176 | static int cy82c693_init_one(struct pci_dev *dev, |
| 177 | const struct pci_device_id *id) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 178 | { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 179 | struct pci_dev *dev2; |
| 180 | int ret = -ENODEV; |
| 181 | |
| 182 | /* CY82C693 is more than only a IDE controller. |
| 183 | Function 1 is primary IDE channel, function 2 - secondary. */ |
Paolo Ciarrocchi | 175f354 | 2008-04-26 17:36:42 +0200 | [diff] [blame] | 184 | if ((dev->class >> 8) == PCI_CLASS_STORAGE_IDE && |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 185 | PCI_FUNC(dev->devfn) == 1) { |
Alan Cox | 652aa16 | 2006-10-03 01:14:35 -0700 | [diff] [blame] | 186 | dev2 = pci_get_slot(dev->bus, dev->devfn + 1); |
Bartlomiej Zolnierkiewicz | 6cdf6eb | 2008-07-24 22:53:14 +0200 | [diff] [blame] | 187 | ret = ide_pci_init_two(dev, dev2, &cy82c693_chipset, NULL); |
Bartlomiej Zolnierkiewicz | cd68841 | 2008-07-24 22:53:21 +0200 | [diff] [blame] | 188 | if (ret) |
| 189 | pci_dev_put(dev2); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 190 | } |
| 191 | return ret; |
| 192 | } |
| 193 | |
Greg Kroah-Hartman | fe31edc | 2012-12-21 13:21:03 -0800 | [diff] [blame] | 194 | static void cy82c693_remove(struct pci_dev *dev) |
Bartlomiej Zolnierkiewicz | cd68841 | 2008-07-24 22:53:21 +0200 | [diff] [blame] | 195 | { |
| 196 | struct ide_host *host = pci_get_drvdata(dev); |
| 197 | struct pci_dev *dev2 = host->dev[1] ? to_pci_dev(host->dev[1]) : NULL; |
| 198 | |
| 199 | ide_pci_remove(dev); |
| 200 | pci_dev_put(dev2); |
| 201 | } |
| 202 | |
Bartlomiej Zolnierkiewicz | 9cbcc5e | 2007-10-16 22:29:56 +0200 | [diff] [blame] | 203 | static const struct pci_device_id cy82c693_pci_tbl[] = { |
| 204 | { PCI_VDEVICE(CONTAQ, PCI_DEVICE_ID_CONTAQ_82C693), 0 }, |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 205 | { 0, }, |
| 206 | }; |
| 207 | MODULE_DEVICE_TABLE(pci, cy82c693_pci_tbl); |
| 208 | |
Bartlomiej Zolnierkiewicz | a9ab09e2 | 2008-10-13 21:39:41 +0200 | [diff] [blame] | 209 | static struct pci_driver cy82c693_pci_driver = { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 210 | .name = "Cypress_IDE", |
| 211 | .id_table = cy82c693_pci_tbl, |
| 212 | .probe = cy82c693_init_one, |
Greg Kroah-Hartman | fe31edc | 2012-12-21 13:21:03 -0800 | [diff] [blame] | 213 | .remove = cy82c693_remove, |
Bartlomiej Zolnierkiewicz | feb22b7 | 2008-10-10 22:39:32 +0200 | [diff] [blame] | 214 | .suspend = ide_pci_suspend, |
| 215 | .resume = ide_pci_resume, |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 216 | }; |
| 217 | |
Bartlomiej Zolnierkiewicz | 82ab1ee | 2007-01-27 13:46:56 +0100 | [diff] [blame] | 218 | static int __init cy82c693_ide_init(void) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 219 | { |
Bartlomiej Zolnierkiewicz | a9ab09e2 | 2008-10-13 21:39:41 +0200 | [diff] [blame] | 220 | return ide_pci_register_driver(&cy82c693_pci_driver); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 221 | } |
| 222 | |
Bartlomiej Zolnierkiewicz | cd68841 | 2008-07-24 22:53:21 +0200 | [diff] [blame] | 223 | static void __exit cy82c693_ide_exit(void) |
| 224 | { |
Bartlomiej Zolnierkiewicz | a9ab09e2 | 2008-10-13 21:39:41 +0200 | [diff] [blame] | 225 | pci_unregister_driver(&cy82c693_pci_driver); |
Bartlomiej Zolnierkiewicz | cd68841 | 2008-07-24 22:53:21 +0200 | [diff] [blame] | 226 | } |
| 227 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 228 | module_init(cy82c693_ide_init); |
Bartlomiej Zolnierkiewicz | cd68841 | 2008-07-24 22:53:21 +0200 | [diff] [blame] | 229 | module_exit(cy82c693_ide_exit); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 230 | |
Bartlomiej Zolnierkiewicz | 4d6b328 | 2010-01-18 07:18:47 +0000 | [diff] [blame] | 231 | MODULE_AUTHOR("Andreas Krebs, Andre Hedrick, Bartlomiej Zolnierkiewicz"); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 232 | MODULE_DESCRIPTION("PCI driver module for the Cypress CY82C693 IDE"); |
| 233 | MODULE_LICENSE("GPL"); |