0001
0002
0003
0004
0005
0006
0007
0008
0009
0010
0011
0012
0013
0014
0015
0016
0017
0018
0019
0020
0021
0022
0023
0024
0025 #include <linux/kernel.h>
0026 #include <linux/module.h>
0027 #include <linux/pci.h>
0028 #include <linux/blkdev.h>
0029 #include <linux/delay.h>
0030 #include <linux/device.h>
0031 #include <scsi/scsi_host.h>
0032 #include <linux/libata.h>
0033 #include <linux/ata.h>
0034
0035 #define DRV_NAME "pata_ns87415"
0036 #define DRV_VERSION "0.0.1"
0037
0038
0039
0040
0041
0042
0043
0044
0045
0046
0047
0048
0049
0050
0051
0052 static void ns87415_set_mode(struct ata_port *ap, struct ata_device *adev, u8 mode)
0053 {
0054 struct pci_dev *dev = to_pci_dev(ap->host->dev);
0055 int unit = 2 * ap->port_no + adev->devno;
0056 int timing = 0x44 + 2 * unit;
0057 unsigned long T = 1000000000 / 33333;
0058 struct ata_timing t;
0059 u16 clocking;
0060 u8 iordy;
0061 u8 status;
0062
0063
0064
0065
0066 ata_timing_compute(adev, adev->pio_mode, &t, T, 0);
0067
0068 clocking = 17 - clamp_val(t.active, 2, 17);
0069 clocking |= (16 - clamp_val(t.recover, 1, 16)) << 4;
0070
0071 clocking |= (clocking << 8);
0072 pci_write_config_word(dev, timing, clocking);
0073
0074
0075 pci_read_config_byte(dev, 0x42, &iordy);
0076 iordy &= ~(1 << (4 + unit));
0077 if (mode >= XFER_MW_DMA_0 || !ata_pio_need_iordy(adev))
0078 iordy |= (1 << (4 + unit));
0079
0080
0081
0082
0083 pci_read_config_byte(dev, 0x43, &status);
0084 while (status & 0x03) {
0085 udelay(1);
0086 pci_read_config_byte(dev, 0x43, &status);
0087 }
0088
0089
0090 pci_write_config_byte(dev, 0x42, iordy);
0091
0092
0093
0094 }
0095
0096
0097
0098
0099
0100
0101
0102
0103
0104
0105
0106
0107 static void ns87415_set_piomode(struct ata_port *ap, struct ata_device *adev)
0108 {
0109 ns87415_set_mode(ap, adev, adev->pio_mode);
0110 }
0111
0112
0113
0114
0115
0116
0117
0118
0119
0120 static void ns87415_bmdma_setup(struct ata_queued_cmd *qc)
0121 {
0122 struct ata_port *ap = qc->ap;
0123 unsigned int rw = (qc->tf.flags & ATA_TFLAG_WRITE);
0124 u8 dmactl;
0125
0126
0127 mb();
0128 iowrite32(ap->bmdma_prd_dma, ap->ioaddr.bmdma_addr + ATA_DMA_TABLE_OFS);
0129
0130
0131 dmactl = ioread8(ap->ioaddr.bmdma_addr + ATA_DMA_CMD);
0132 dmactl &= ~(ATA_DMA_WR | ATA_DMA_START);
0133
0134
0135 dmactl |= ATA_DMA_INTR | ATA_DMA_ERR;
0136 if (!rw)
0137 dmactl |= ATA_DMA_WR;
0138 iowrite8(dmactl, ap->ioaddr.bmdma_addr + ATA_DMA_CMD);
0139
0140 ap->ops->sff_exec_command(ap, &qc->tf);
0141 }
0142
0143
0144
0145
0146
0147
0148
0149
0150
0151
0152
0153
0154 static void ns87415_bmdma_start(struct ata_queued_cmd *qc)
0155 {
0156 ns87415_set_mode(qc->ap, qc->dev, qc->dev->dma_mode);
0157 ata_bmdma_start(qc);
0158 }
0159
0160
0161
0162
0163
0164
0165
0166
0167 static void ns87415_bmdma_stop(struct ata_queued_cmd *qc)
0168 {
0169 ata_bmdma_stop(qc);
0170 ns87415_set_mode(qc->ap, qc->dev, qc->dev->pio_mode);
0171 }
0172
0173
0174
0175
0176
0177
0178
0179
0180
0181 static void ns87415_irq_clear(struct ata_port *ap)
0182 {
0183 void __iomem *mmio = ap->ioaddr.bmdma_addr;
0184
0185 if (!mmio)
0186 return;
0187 iowrite8((ioread8(mmio + ATA_DMA_CMD) | ATA_DMA_INTR | ATA_DMA_ERR),
0188 mmio + ATA_DMA_CMD);
0189 }
0190
0191
0192
0193
0194
0195
0196
0197
0198
0199 static int ns87415_check_atapi_dma(struct ata_queued_cmd *qc)
0200 {
0201 return -EOPNOTSUPP;
0202 }
0203
0204 #if defined(CONFIG_SUPERIO)
0205
0206
0207
0208
0209
0210
0211
0212
0213
0214
0215 #include <asm/superio.h>
0216
0217 #define SUPERIO_IDE_MAX_RETRIES 25
0218
0219
0220
0221
0222
0223
0224
0225
0226 static u8 ns87560_read_buggy(void __iomem *port)
0227 {
0228 u8 tmp;
0229 int retries = SUPERIO_IDE_MAX_RETRIES;
0230 do {
0231 tmp = ioread8(port);
0232 if (tmp != 0)
0233 return tmp;
0234 udelay(50);
0235 } while(retries-- > 0);
0236 return tmp;
0237 }
0238
0239
0240
0241
0242
0243
0244
0245
0246
0247 static u8 ns87560_check_status(struct ata_port *ap)
0248 {
0249 return ns87560_read_buggy(ap->ioaddr.status_addr);
0250 }
0251
0252
0253
0254
0255
0256
0257
0258
0259
0260
0261
0262
0263 void ns87560_tf_read(struct ata_port *ap, struct ata_taskfile *tf)
0264 {
0265 struct ata_ioports *ioaddr = &ap->ioaddr;
0266
0267 tf->status = ns87560_check_status(ap);
0268 tf->error = ioread8(ioaddr->error_addr);
0269 tf->nsect = ioread8(ioaddr->nsect_addr);
0270 tf->lbal = ioread8(ioaddr->lbal_addr);
0271 tf->lbam = ioread8(ioaddr->lbam_addr);
0272 tf->lbah = ioread8(ioaddr->lbah_addr);
0273 tf->device = ns87560_read_buggy(ioaddr->device_addr);
0274
0275 if (tf->flags & ATA_TFLAG_LBA48) {
0276 iowrite8(tf->ctl | ATA_HOB, ioaddr->ctl_addr);
0277 tf->hob_feature = ioread8(ioaddr->error_addr);
0278 tf->hob_nsect = ioread8(ioaddr->nsect_addr);
0279 tf->hob_lbal = ioread8(ioaddr->lbal_addr);
0280 tf->hob_lbam = ioread8(ioaddr->lbam_addr);
0281 tf->hob_lbah = ioread8(ioaddr->lbah_addr);
0282 iowrite8(tf->ctl, ioaddr->ctl_addr);
0283 ap->last_ctl = tf->ctl;
0284 }
0285 }
0286
0287
0288
0289
0290
0291
0292
0293
0294
0295 static u8 ns87560_bmdma_status(struct ata_port *ap)
0296 {
0297 return ns87560_read_buggy(ap->ioaddr.bmdma_addr + ATA_DMA_STATUS);
0298 }
0299 #endif
0300
0301 static struct ata_port_operations ns87415_pata_ops = {
0302 .inherits = &ata_bmdma_port_ops,
0303
0304 .check_atapi_dma = ns87415_check_atapi_dma,
0305 .bmdma_setup = ns87415_bmdma_setup,
0306 .bmdma_start = ns87415_bmdma_start,
0307 .bmdma_stop = ns87415_bmdma_stop,
0308 .sff_irq_clear = ns87415_irq_clear,
0309
0310 .cable_detect = ata_cable_40wire,
0311 .set_piomode = ns87415_set_piomode,
0312 };
0313
0314 #if defined(CONFIG_SUPERIO)
0315 static struct ata_port_operations ns87560_pata_ops = {
0316 .inherits = &ns87415_pata_ops,
0317 .sff_tf_read = ns87560_tf_read,
0318 .sff_check_status = ns87560_check_status,
0319 .bmdma_status = ns87560_bmdma_status,
0320 };
0321 #endif
0322
0323 static struct scsi_host_template ns87415_sht = {
0324 ATA_BMDMA_SHT(DRV_NAME),
0325 };
0326
0327 static void ns87415_fixup(struct pci_dev *pdev)
0328 {
0329
0330 pci_write_config_byte(pdev, 0x55, 0xEE);
0331
0332 pci_write_config_byte(pdev, 0x54, 0xB7);
0333 }
0334
0335
0336
0337
0338
0339
0340
0341
0342
0343
0344
0345
0346
0347
0348
0349
0350 static int ns87415_init_one (struct pci_dev *pdev, const struct pci_device_id *ent)
0351 {
0352 static const struct ata_port_info info = {
0353 .flags = ATA_FLAG_SLAVE_POSS,
0354 .pio_mask = ATA_PIO4,
0355 .mwdma_mask = ATA_MWDMA2,
0356 .port_ops = &ns87415_pata_ops,
0357 };
0358 const struct ata_port_info *ppi[] = { &info, NULL };
0359 int rc;
0360 #if defined(CONFIG_SUPERIO)
0361 static const struct ata_port_info info87560 = {
0362 .flags = ATA_FLAG_SLAVE_POSS,
0363 .pio_mask = ATA_PIO4,
0364 .mwdma_mask = ATA_MWDMA2,
0365 .port_ops = &ns87560_pata_ops,
0366 };
0367
0368 if (PCI_SLOT(pdev->devfn) == 0x0E)
0369 ppi[0] = &info87560;
0370 #endif
0371 ata_print_version_once(&pdev->dev, DRV_VERSION);
0372
0373 rc = pcim_enable_device(pdev);
0374 if (rc)
0375 return rc;
0376
0377 ns87415_fixup(pdev);
0378
0379 return ata_pci_bmdma_init_one(pdev, ppi, &ns87415_sht, NULL, 0);
0380 }
0381
0382 static const struct pci_device_id ns87415_pci_tbl[] = {
0383 { PCI_VDEVICE(NS, PCI_DEVICE_ID_NS_87415), },
0384
0385 { }
0386 };
0387
0388 #ifdef CONFIG_PM_SLEEP
0389 static int ns87415_reinit_one(struct pci_dev *pdev)
0390 {
0391 struct ata_host *host = pci_get_drvdata(pdev);
0392 int rc;
0393
0394 rc = ata_pci_device_do_resume(pdev);
0395 if (rc)
0396 return rc;
0397
0398 ns87415_fixup(pdev);
0399
0400 ata_host_resume(host);
0401 return 0;
0402 }
0403 #endif
0404
0405 static struct pci_driver ns87415_pci_driver = {
0406 .name = DRV_NAME,
0407 .id_table = ns87415_pci_tbl,
0408 .probe = ns87415_init_one,
0409 .remove = ata_pci_remove_one,
0410 #ifdef CONFIG_PM_SLEEP
0411 .suspend = ata_pci_device_suspend,
0412 .resume = ns87415_reinit_one,
0413 #endif
0414 };
0415
0416 module_pci_driver(ns87415_pci_driver);
0417
0418 MODULE_AUTHOR("Alan Cox");
0419 MODULE_DESCRIPTION("ATA low-level driver for NS87415 controllers");
0420 MODULE_LICENSE("GPL");
0421 MODULE_DEVICE_TABLE(pci, ns87415_pci_tbl);
0422 MODULE_VERSION(DRV_VERSION);