Jeff Garzik | 669a5db | 2006-08-29 18:12:40 -0400 | [diff] [blame] | 1 | /* |
| 2 | * pata_sl82c105.c - SL82C105 PATA for new ATA layer |
| 3 | * (C) 2005 Red Hat Inc |
| 4 | * Alan Cox <alan@redhat.com> |
| 5 | * |
| 6 | * Based in part on linux/drivers/ide/pci/sl82c105.c |
| 7 | * SL82C105/Winbond 553 IDE driver |
| 8 | * |
| 9 | * and in part on the documentation and errata sheet |
| 10 | */ |
Jeff Garzik | 85cd725 | 2006-08-31 00:03:49 -0400 | [diff] [blame] | 11 | |
Jeff Garzik | 669a5db | 2006-08-29 18:12:40 -0400 | [diff] [blame] | 12 | #include <linux/kernel.h> |
| 13 | #include <linux/module.h> |
| 14 | #include <linux/pci.h> |
| 15 | #include <linux/init.h> |
| 16 | #include <linux/blkdev.h> |
| 17 | #include <linux/delay.h> |
| 18 | #include <scsi/scsi_host.h> |
| 19 | #include <linux/libata.h> |
| 20 | |
| 21 | #define DRV_NAME "pata_sl82c105" |
Alan Cox | c961922 | 2006-09-26 17:53:38 +0100 | [diff] [blame] | 22 | #define DRV_VERSION "0.2.3" |
Jeff Garzik | 669a5db | 2006-08-29 18:12:40 -0400 | [diff] [blame] | 23 | |
| 24 | enum { |
| 25 | /* |
| 26 | * SL82C105 PCI config register 0x40 bits. |
| 27 | */ |
| 28 | CTRL_IDE_IRQB = (1 << 30), |
| 29 | CTRL_IDE_IRQA = (1 << 28), |
| 30 | CTRL_LEGIRQ = (1 << 11), |
| 31 | CTRL_P1F16 = (1 << 5), |
| 32 | CTRL_P1EN = (1 << 4), |
| 33 | CTRL_P0F16 = (1 << 1), |
| 34 | CTRL_P0EN = (1 << 0) |
| 35 | }; |
| 36 | |
| 37 | /** |
| 38 | * sl82c105_pre_reset - probe begin |
| 39 | * @ap: ATA port |
| 40 | * |
| 41 | * Set up cable type and use generic probe init |
| 42 | */ |
Jeff Garzik | 85cd725 | 2006-08-31 00:03:49 -0400 | [diff] [blame] | 43 | |
Jeff Garzik | 669a5db | 2006-08-29 18:12:40 -0400 | [diff] [blame] | 44 | static int sl82c105_pre_reset(struct ata_port *ap) |
| 45 | { |
| 46 | static const struct pci_bits sl82c105_enable_bits[] = { |
| 47 | { 0x40, 1, 0x01, 0x01 }, |
| 48 | { 0x40, 1, 0x10, 0x10 } |
| 49 | }; |
| 50 | struct pci_dev *pdev = to_pci_dev(ap->host->dev); |
| 51 | |
Alan Cox | c961922 | 2006-09-26 17:53:38 +0100 | [diff] [blame] | 52 | if (ap->port_no && !pci_test_config_bits(pdev, &sl82c105_enable_bits[ap->port_no])) |
| 53 | return -ENOENT; |
Jeff Garzik | 669a5db | 2006-08-29 18:12:40 -0400 | [diff] [blame] | 54 | ap->cbl = ATA_CBL_PATA40; |
| 55 | return ata_std_prereset(ap); |
| 56 | } |
| 57 | |
| 58 | |
| 59 | static void sl82c105_error_handler(struct ata_port *ap) |
| 60 | { |
| 61 | ata_bmdma_drive_eh(ap, sl82c105_pre_reset, ata_std_softreset, NULL, ata_std_postreset); |
| 62 | } |
| 63 | |
| 64 | |
| 65 | /** |
| 66 | * sl82c105_configure_piomode - set chip PIO timing |
| 67 | * @ap: ATA interface |
| 68 | * @adev: ATA device |
| 69 | * @pio: PIO mode |
| 70 | * |
| 71 | * Called to do the PIO mode setup. Our timing registers are shared |
| 72 | * so a configure_dmamode call will undo any work we do here and vice |
| 73 | * versa |
| 74 | */ |
Jeff Garzik | 85cd725 | 2006-08-31 00:03:49 -0400 | [diff] [blame] | 75 | |
Jeff Garzik | 669a5db | 2006-08-29 18:12:40 -0400 | [diff] [blame] | 76 | static void sl82c105_configure_piomode(struct ata_port *ap, struct ata_device *adev, int pio) |
| 77 | { |
| 78 | struct pci_dev *pdev = to_pci_dev(ap->host->dev); |
| 79 | static u16 pio_timing[5] = { |
| 80 | 0x50D, 0x407, 0x304, 0x242, 0x240 |
| 81 | }; |
| 82 | u16 dummy; |
| 83 | int timing = 0x44 + (8 * ap->port_no) + (4 * adev->devno); |
Jeff Garzik | 85cd725 | 2006-08-31 00:03:49 -0400 | [diff] [blame] | 84 | |
Jeff Garzik | 669a5db | 2006-08-29 18:12:40 -0400 | [diff] [blame] | 85 | pci_write_config_word(pdev, timing, pio_timing[pio]); |
| 86 | /* Can we lose this oddity of the old driver */ |
| 87 | pci_read_config_word(pdev, timing, &dummy); |
| 88 | } |
| 89 | |
| 90 | /** |
| 91 | * sl82c105_set_piomode - set initial PIO mode data |
| 92 | * @ap: ATA interface |
| 93 | * @adev: ATA device |
| 94 | * |
| 95 | * Called to do the PIO mode setup. Our timing registers are shared |
| 96 | * but we want to set the PIO timing by default. |
| 97 | */ |
Jeff Garzik | 85cd725 | 2006-08-31 00:03:49 -0400 | [diff] [blame] | 98 | |
Jeff Garzik | 669a5db | 2006-08-29 18:12:40 -0400 | [diff] [blame] | 99 | static void sl82c105_set_piomode(struct ata_port *ap, struct ata_device *adev) |
| 100 | { |
| 101 | sl82c105_configure_piomode(ap, adev, adev->pio_mode - XFER_PIO_0); |
| 102 | } |
| 103 | |
| 104 | /** |
| 105 | * sl82c105_configure_dmamode - set DMA mode in chip |
| 106 | * @ap: ATA interface |
| 107 | * @adev: ATA device |
| 108 | * |
| 109 | * Load DMA cycle times into the chip ready for a DMA transfer |
| 110 | * to occur. |
| 111 | */ |
Jeff Garzik | 85cd725 | 2006-08-31 00:03:49 -0400 | [diff] [blame] | 112 | |
Jeff Garzik | 669a5db | 2006-08-29 18:12:40 -0400 | [diff] [blame] | 113 | static void sl82c105_configure_dmamode(struct ata_port *ap, struct ata_device *adev) |
| 114 | { |
| 115 | struct pci_dev *pdev = to_pci_dev(ap->host->dev); |
| 116 | static u16 dma_timing[3] = { |
| 117 | 0x707, 0x201, 0x200 |
| 118 | }; |
| 119 | u16 dummy; |
| 120 | int timing = 0x44 + (8 * ap->port_no) + (4 * adev->devno); |
| 121 | int dma = adev->dma_mode - XFER_MW_DMA_0; |
Jeff Garzik | 85cd725 | 2006-08-31 00:03:49 -0400 | [diff] [blame] | 122 | |
Jeff Garzik | 669a5db | 2006-08-29 18:12:40 -0400 | [diff] [blame] | 123 | pci_write_config_word(pdev, timing, dma_timing[dma]); |
| 124 | /* Can we lose this oddity of the old driver */ |
| 125 | pci_read_config_word(pdev, timing, &dummy); |
| 126 | } |
| 127 | |
| 128 | /** |
| 129 | * sl82c105_set_dmamode - set initial DMA mode data |
| 130 | * @ap: ATA interface |
| 131 | * @adev: ATA device |
| 132 | * |
| 133 | * Called to do the DMA mode setup. This replaces the PIO timings |
| 134 | * for the device in question. Set appropriate PIO timings not DMA |
| 135 | * timings at this point. |
| 136 | */ |
Jeff Garzik | 85cd725 | 2006-08-31 00:03:49 -0400 | [diff] [blame] | 137 | |
Jeff Garzik | 669a5db | 2006-08-29 18:12:40 -0400 | [diff] [blame] | 138 | static void sl82c105_set_dmamode(struct ata_port *ap, struct ata_device *adev) |
| 139 | { |
| 140 | switch(adev->dma_mode) { |
| 141 | case XFER_MW_DMA_0: |
| 142 | sl82c105_configure_piomode(ap, adev, 1); |
| 143 | break; |
| 144 | case XFER_MW_DMA_1: |
| 145 | sl82c105_configure_piomode(ap, adev, 3); |
| 146 | break; |
| 147 | case XFER_MW_DMA_2: |
| 148 | sl82c105_configure_piomode(ap, adev, 3); |
| 149 | break; |
| 150 | default: |
| 151 | BUG(); |
Jeff Garzik | 85cd725 | 2006-08-31 00:03:49 -0400 | [diff] [blame] | 152 | } |
Jeff Garzik | 669a5db | 2006-08-29 18:12:40 -0400 | [diff] [blame] | 153 | } |
| 154 | |
| 155 | /** |
| 156 | * sl82c105_reset_engine - Reset the DMA engine |
| 157 | * @ap: ATA interface |
| 158 | * |
| 159 | * The sl82c105 has some serious problems with the DMA engine |
Jeff Garzik | 85cd725 | 2006-08-31 00:03:49 -0400 | [diff] [blame] | 160 | * when transfers don't run as expected or ATAPI is used. The |
Jeff Garzik | 669a5db | 2006-08-29 18:12:40 -0400 | [diff] [blame] | 161 | * recommended fix is to reset the engine each use using a chip |
| 162 | * test register. |
| 163 | */ |
Jeff Garzik | 85cd725 | 2006-08-31 00:03:49 -0400 | [diff] [blame] | 164 | |
Jeff Garzik | 669a5db | 2006-08-29 18:12:40 -0400 | [diff] [blame] | 165 | static void sl82c105_reset_engine(struct ata_port *ap) |
| 166 | { |
| 167 | struct pci_dev *pdev = to_pci_dev(ap->host->dev); |
| 168 | u16 val; |
Jeff Garzik | 85cd725 | 2006-08-31 00:03:49 -0400 | [diff] [blame] | 169 | |
Jeff Garzik | 669a5db | 2006-08-29 18:12:40 -0400 | [diff] [blame] | 170 | pci_read_config_word(pdev, 0x7E, &val); |
| 171 | pci_write_config_word(pdev, 0x7E, val | 4); |
| 172 | pci_write_config_word(pdev, 0x7E, val & ~4); |
| 173 | } |
| 174 | |
| 175 | /** |
| 176 | * sl82c105_bmdma_start - DMA engine begin |
| 177 | * @qc: ATA command |
| 178 | * |
| 179 | * Reset the DMA engine each use as recommended by the errata |
Jeff Garzik | 85cd725 | 2006-08-31 00:03:49 -0400 | [diff] [blame] | 180 | * document. |
Jeff Garzik | 669a5db | 2006-08-29 18:12:40 -0400 | [diff] [blame] | 181 | * |
| 182 | * FIXME: if we switch clock at BMDMA start/end we might get better |
| 183 | * PIO performance on DMA capable devices. |
| 184 | */ |
Jeff Garzik | 85cd725 | 2006-08-31 00:03:49 -0400 | [diff] [blame] | 185 | |
Jeff Garzik | 669a5db | 2006-08-29 18:12:40 -0400 | [diff] [blame] | 186 | static void sl82c105_bmdma_start(struct ata_queued_cmd *qc) |
| 187 | { |
| 188 | struct ata_port *ap = qc->ap; |
| 189 | |
| 190 | sl82c105_reset_engine(ap); |
Jeff Garzik | 85cd725 | 2006-08-31 00:03:49 -0400 | [diff] [blame] | 191 | |
Jeff Garzik | 669a5db | 2006-08-29 18:12:40 -0400 | [diff] [blame] | 192 | /* Set the clocks for DMA */ |
| 193 | sl82c105_configure_dmamode(ap, qc->dev); |
Jeff Garzik | 85cd725 | 2006-08-31 00:03:49 -0400 | [diff] [blame] | 194 | /* Activate DMA */ |
Jeff Garzik | 669a5db | 2006-08-29 18:12:40 -0400 | [diff] [blame] | 195 | ata_bmdma_start(qc); |
| 196 | } |
| 197 | |
| 198 | /** |
| 199 | * sl82c105_bmdma_end - DMA engine stop |
| 200 | * @qc: ATA command |
| 201 | * |
| 202 | * Reset the DMA engine each use as recommended by the errata |
| 203 | * document. |
| 204 | * |
| 205 | * This function is also called to turn off DMA when a timeout occurs |
| 206 | * during DMA operation. In both cases we need to reset the engine, |
| 207 | * so no actual eng_timeout handler is required. |
| 208 | * |
| 209 | * We assume bmdma_stop is always called if bmdma_start as called. If |
| 210 | * not then we may need to wrap qc_issue. |
| 211 | */ |
Jeff Garzik | 85cd725 | 2006-08-31 00:03:49 -0400 | [diff] [blame] | 212 | |
Jeff Garzik | 669a5db | 2006-08-29 18:12:40 -0400 | [diff] [blame] | 213 | static void sl82c105_bmdma_stop(struct ata_queued_cmd *qc) |
| 214 | { |
| 215 | struct ata_port *ap = qc->ap; |
| 216 | |
| 217 | ata_bmdma_stop(qc); |
| 218 | sl82c105_reset_engine(ap); |
Jeff Garzik | 85cd725 | 2006-08-31 00:03:49 -0400 | [diff] [blame] | 219 | |
Jeff Garzik | 669a5db | 2006-08-29 18:12:40 -0400 | [diff] [blame] | 220 | /* This will redo the initial setup of the DMA device to matching |
| 221 | PIO timings */ |
| 222 | sl82c105_set_dmamode(ap, qc->dev); |
| 223 | } |
| 224 | |
| 225 | static struct scsi_host_template sl82c105_sht = { |
| 226 | .module = THIS_MODULE, |
| 227 | .name = DRV_NAME, |
| 228 | .ioctl = ata_scsi_ioctl, |
| 229 | .queuecommand = ata_scsi_queuecmd, |
| 230 | .can_queue = ATA_DEF_QUEUE, |
| 231 | .this_id = ATA_SHT_THIS_ID, |
| 232 | .sg_tablesize = LIBATA_MAX_PRD, |
| 233 | .max_sectors = ATA_MAX_SECTORS, |
| 234 | .cmd_per_lun = ATA_SHT_CMD_PER_LUN, |
| 235 | .emulated = ATA_SHT_EMULATED, |
| 236 | .use_clustering = ATA_SHT_USE_CLUSTERING, |
| 237 | .proc_name = DRV_NAME, |
| 238 | .dma_boundary = ATA_DMA_BOUNDARY, |
| 239 | .slave_configure = ata_scsi_slave_config, |
Tejun Heo | afdfe89 | 2006-11-29 11:26:47 +0900 | [diff] [blame^] | 240 | .slave_destroy = ata_scsi_slave_destroy, |
Jeff Garzik | 669a5db | 2006-08-29 18:12:40 -0400 | [diff] [blame] | 241 | .bios_param = ata_std_bios_param, |
| 242 | }; |
| 243 | |
| 244 | static struct ata_port_operations sl82c105_port_ops = { |
| 245 | .port_disable = ata_port_disable, |
| 246 | .set_piomode = sl82c105_set_piomode, |
| 247 | .set_dmamode = sl82c105_set_dmamode, |
| 248 | .mode_filter = ata_pci_default_filter, |
| 249 | |
| 250 | .tf_load = ata_tf_load, |
| 251 | .tf_read = ata_tf_read, |
| 252 | .check_status = ata_check_status, |
| 253 | .exec_command = ata_exec_command, |
| 254 | .dev_select = ata_std_dev_select, |
| 255 | |
| 256 | .error_handler = sl82c105_error_handler, |
| 257 | |
| 258 | .bmdma_setup = ata_bmdma_setup, |
| 259 | .bmdma_start = sl82c105_bmdma_start, |
| 260 | .bmdma_stop = sl82c105_bmdma_stop, |
| 261 | .bmdma_status = ata_bmdma_status, |
| 262 | |
| 263 | .qc_prep = ata_qc_prep, |
| 264 | .qc_issue = ata_qc_issue_prot, |
Jeff Garzik | bda3028 | 2006-09-27 05:41:13 -0400 | [diff] [blame] | 265 | |
Jeff Garzik | 669a5db | 2006-08-29 18:12:40 -0400 | [diff] [blame] | 266 | .data_xfer = ata_pio_data_xfer, |
| 267 | |
| 268 | .irq_handler = ata_interrupt, |
| 269 | .irq_clear = ata_bmdma_irq_clear, |
Jeff Garzik | 85cd725 | 2006-08-31 00:03:49 -0400 | [diff] [blame] | 270 | |
Jeff Garzik | 669a5db | 2006-08-29 18:12:40 -0400 | [diff] [blame] | 271 | .port_start = ata_port_start, |
| 272 | .port_stop = ata_port_stop, |
| 273 | .host_stop = ata_host_stop |
Jeff Garzik | 85cd725 | 2006-08-31 00:03:49 -0400 | [diff] [blame] | 274 | }; |
Jeff Garzik | 669a5db | 2006-08-29 18:12:40 -0400 | [diff] [blame] | 275 | |
| 276 | /** |
| 277 | * sl82c105_bridge_revision - find bridge version |
| 278 | * @pdev: PCI device for the ATA function |
| 279 | * |
| 280 | * Locates the PCI bridge associated with the ATA function and |
| 281 | * providing it is a Winbond 553 reports the revision. If it cannot |
| 282 | * find a revision or the right device it returns -1 |
| 283 | */ |
Jeff Garzik | 85cd725 | 2006-08-31 00:03:49 -0400 | [diff] [blame] | 284 | |
Jeff Garzik | 669a5db | 2006-08-29 18:12:40 -0400 | [diff] [blame] | 285 | static int sl82c105_bridge_revision(struct pci_dev *pdev) |
| 286 | { |
| 287 | struct pci_dev *bridge; |
| 288 | u8 rev; |
| 289 | |
| 290 | /* |
| 291 | * The bridge should be part of the same device, but function 0. |
| 292 | */ |
| 293 | bridge = pci_get_slot(pdev->bus, |
| 294 | PCI_DEVFN(PCI_SLOT(pdev->devfn), 0)); |
| 295 | if (!bridge) |
| 296 | return -1; |
| 297 | |
| 298 | /* |
| 299 | * Make sure it is a Winbond 553 and is an ISA bridge. |
| 300 | */ |
| 301 | if (bridge->vendor != PCI_VENDOR_ID_WINBOND || |
| 302 | bridge->device != PCI_DEVICE_ID_WINBOND_83C553 || |
| 303 | bridge->class >> 8 != PCI_CLASS_BRIDGE_ISA) { |
| 304 | pci_dev_put(bridge); |
| 305 | return -1; |
| 306 | } |
| 307 | /* |
| 308 | * We need to find function 0's revision, not function 1 |
| 309 | */ |
| 310 | pci_read_config_byte(bridge, PCI_REVISION_ID, &rev); |
| 311 | |
| 312 | pci_dev_put(bridge); |
| 313 | return rev; |
| 314 | } |
| 315 | |
Jeff Garzik | 85cd725 | 2006-08-31 00:03:49 -0400 | [diff] [blame] | 316 | |
Jeff Garzik | 669a5db | 2006-08-29 18:12:40 -0400 | [diff] [blame] | 317 | static int sl82c105_init_one(struct pci_dev *dev, const struct pci_device_id *id) |
| 318 | { |
| 319 | static struct ata_port_info info_dma = { |
| 320 | .sht = &sl82c105_sht, |
| 321 | .flags = ATA_FLAG_SLAVE_POSS | ATA_FLAG_SRST, |
| 322 | .pio_mask = 0x1f, |
| 323 | .mwdma_mask = 0x07, |
| 324 | .port_ops = &sl82c105_port_ops |
| 325 | }; |
| 326 | static struct ata_port_info info_early = { |
| 327 | .sht = &sl82c105_sht, |
| 328 | .flags = ATA_FLAG_SLAVE_POSS | ATA_FLAG_SRST, |
| 329 | .pio_mask = 0x1f, |
| 330 | .port_ops = &sl82c105_port_ops |
| 331 | }; |
| 332 | static struct ata_port_info *port_info[2] = { &info_early, &info_early }; |
| 333 | u32 val; |
| 334 | int rev; |
| 335 | |
| 336 | rev = sl82c105_bridge_revision(dev); |
Jeff Garzik | 85cd725 | 2006-08-31 00:03:49 -0400 | [diff] [blame] | 337 | |
Jeff Garzik | 669a5db | 2006-08-29 18:12:40 -0400 | [diff] [blame] | 338 | if (rev == -1) |
| 339 | dev_printk(KERN_WARNING, &dev->dev, "pata_sl82c105: Unable to find bridge, disabling DMA.\n"); |
| 340 | else if (rev <= 5) |
| 341 | dev_printk(KERN_WARNING, &dev->dev, "pata_sl82c105: Early bridge revision, no DMA available.\n"); |
| 342 | else { |
| 343 | port_info[0] = &info_dma; |
| 344 | port_info[1] = &info_dma; |
| 345 | } |
Jeff Garzik | 85cd725 | 2006-08-31 00:03:49 -0400 | [diff] [blame] | 346 | |
Jeff Garzik | 669a5db | 2006-08-29 18:12:40 -0400 | [diff] [blame] | 347 | pci_read_config_dword(dev, 0x40, &val); |
| 348 | val |= CTRL_P0EN | CTRL_P0F16 | CTRL_P1F16; |
| 349 | pci_write_config_dword(dev, 0x40, val); |
| 350 | |
| 351 | |
| 352 | return ata_pci_init_one(dev, port_info, 1); /* For now */ |
| 353 | } |
| 354 | |
Jeff Garzik | 2d2744f | 2006-09-28 20:21:59 -0400 | [diff] [blame] | 355 | static const struct pci_device_id sl82c105[] = { |
| 356 | { PCI_VDEVICE(WINBOND, PCI_DEVICE_ID_WINBOND_82C105), }, |
| 357 | |
| 358 | { }, |
Jeff Garzik | 669a5db | 2006-08-29 18:12:40 -0400 | [diff] [blame] | 359 | }; |
| 360 | |
| 361 | static struct pci_driver sl82c105_pci_driver = { |
| 362 | .name = DRV_NAME, |
| 363 | .id_table = sl82c105, |
| 364 | .probe = sl82c105_init_one, |
| 365 | .remove = ata_pci_remove_one |
| 366 | }; |
| 367 | |
| 368 | static int __init sl82c105_init(void) |
| 369 | { |
| 370 | return pci_register_driver(&sl82c105_pci_driver); |
| 371 | } |
| 372 | |
Jeff Garzik | 669a5db | 2006-08-29 18:12:40 -0400 | [diff] [blame] | 373 | static void __exit sl82c105_exit(void) |
| 374 | { |
| 375 | pci_unregister_driver(&sl82c105_pci_driver); |
| 376 | } |
| 377 | |
Jeff Garzik | 669a5db | 2006-08-29 18:12:40 -0400 | [diff] [blame] | 378 | MODULE_AUTHOR("Alan Cox"); |
| 379 | MODULE_DESCRIPTION("low-level driver for Sl82c105"); |
| 380 | MODULE_LICENSE("GPL"); |
| 381 | MODULE_DEVICE_TABLE(pci, sl82c105); |
| 382 | MODULE_VERSION(DRV_VERSION); |
| 383 | |
| 384 | module_init(sl82c105_init); |
| 385 | module_exit(sl82c105_exit); |