0001
0002
0003
0004
0005
0006
0007
0008
0009
0010
0011 #include <linux/kernel.h>
0012 #include <linux/module.h>
0013 #include <linux/pci.h>
0014 #include <linux/blkdev.h>
0015 #include <linux/delay.h>
0016 #include <linux/device.h>
0017 #include <linux/gfp.h>
0018 #include <scsi/scsi_host.h>
0019 #include <linux/libata.h>
0020 #include <linux/dmi.h>
0021
0022 #define DRV_NAME "pata_rdc"
0023 #define DRV_VERSION "0.01"
0024
0025 struct rdc_host_priv {
0026 u32 saved_iocfg;
0027 };
0028
0029
0030
0031
0032
0033
0034
0035
0036
0037
0038
0039
0040 static int rdc_pata_cable_detect(struct ata_port *ap)
0041 {
0042 struct rdc_host_priv *hpriv = ap->host->private_data;
0043 u8 mask;
0044
0045
0046 mask = 0x30 << (2 * ap->port_no);
0047 if ((hpriv->saved_iocfg & mask) == 0)
0048 return ATA_CBL_PATA40;
0049 return ATA_CBL_PATA80;
0050 }
0051
0052
0053
0054
0055
0056
0057
0058
0059
0060 static int rdc_pata_prereset(struct ata_link *link, unsigned long deadline)
0061 {
0062 struct ata_port *ap = link->ap;
0063 struct pci_dev *pdev = to_pci_dev(ap->host->dev);
0064
0065 static const struct pci_bits rdc_enable_bits[] = {
0066 { 0x41U, 1U, 0x80UL, 0x80UL },
0067 { 0x43U, 1U, 0x80UL, 0x80UL },
0068 };
0069
0070 if (!pci_test_config_bits(pdev, &rdc_enable_bits[ap->port_no]))
0071 return -ENOENT;
0072 return ata_sff_prereset(link, deadline);
0073 }
0074
0075 static DEFINE_SPINLOCK(rdc_lock);
0076
0077
0078
0079
0080
0081
0082
0083
0084
0085
0086
0087
0088 static void rdc_set_piomode(struct ata_port *ap, struct ata_device *adev)
0089 {
0090 unsigned int pio = adev->pio_mode - XFER_PIO_0;
0091 struct pci_dev *dev = to_pci_dev(ap->host->dev);
0092 unsigned long flags;
0093 unsigned int is_slave = (adev->devno != 0);
0094 unsigned int master_port= ap->port_no ? 0x42 : 0x40;
0095 unsigned int slave_port = 0x44;
0096 u16 master_data;
0097 u8 slave_data;
0098 u8 udma_enable;
0099 int control = 0;
0100
0101 static const
0102 u8 timings[][2] = { { 0, 0 },
0103 { 0, 0 },
0104 { 1, 0 },
0105 { 2, 1 },
0106 { 2, 3 }, };
0107
0108 if (pio >= 2)
0109 control |= 1;
0110 if (ata_pio_need_iordy(adev))
0111 control |= 2;
0112
0113 if (adev->class == ATA_DEV_ATA)
0114 control |= 4;
0115
0116 spin_lock_irqsave(&rdc_lock, flags);
0117
0118
0119
0120
0121
0122 pci_read_config_word(dev, master_port, &master_data);
0123 if (is_slave) {
0124
0125 master_data &= 0xff0f;
0126
0127 master_data |= 0x4000;
0128
0129 master_data |= (control << 4);
0130 pci_read_config_byte(dev, slave_port, &slave_data);
0131 slave_data &= (ap->port_no ? 0x0f : 0xf0);
0132
0133 slave_data |= ((timings[pio][0] << 2) | timings[pio][1])
0134 << (ap->port_no ? 4 : 0);
0135 } else {
0136
0137 master_data &= 0xccf0;
0138
0139 master_data |= control;
0140
0141 master_data |=
0142 (timings[pio][0] << 12) |
0143 (timings[pio][1] << 8);
0144 }
0145 pci_write_config_word(dev, master_port, master_data);
0146 if (is_slave)
0147 pci_write_config_byte(dev, slave_port, slave_data);
0148
0149
0150
0151
0152 pci_read_config_byte(dev, 0x48, &udma_enable);
0153 udma_enable &= ~(1 << (2 * ap->port_no + adev->devno));
0154 pci_write_config_byte(dev, 0x48, udma_enable);
0155
0156 spin_unlock_irqrestore(&rdc_lock, flags);
0157 }
0158
0159
0160
0161
0162
0163
0164
0165
0166
0167
0168
0169
0170 static void rdc_set_dmamode(struct ata_port *ap, struct ata_device *adev)
0171 {
0172 struct pci_dev *dev = to_pci_dev(ap->host->dev);
0173 unsigned long flags;
0174 u8 master_port = ap->port_no ? 0x42 : 0x40;
0175 u16 master_data;
0176 u8 speed = adev->dma_mode;
0177 int devid = adev->devno + 2 * ap->port_no;
0178 u8 udma_enable = 0;
0179
0180 static const
0181 u8 timings[][2] = { { 0, 0 },
0182 { 0, 0 },
0183 { 1, 0 },
0184 { 2, 1 },
0185 { 2, 3 }, };
0186
0187 spin_lock_irqsave(&rdc_lock, flags);
0188
0189 pci_read_config_word(dev, master_port, &master_data);
0190 pci_read_config_byte(dev, 0x48, &udma_enable);
0191
0192 if (speed >= XFER_UDMA_0) {
0193 unsigned int udma = adev->dma_mode - XFER_UDMA_0;
0194 u16 udma_timing;
0195 u16 ideconf;
0196 int u_clock, u_speed;
0197
0198
0199
0200
0201
0202
0203
0204
0205 u_speed = min(2 - (udma & 1), udma);
0206 if (udma == 5)
0207 u_clock = 0x1000;
0208 else if (udma > 2)
0209 u_clock = 1;
0210 else
0211 u_clock = 0;
0212
0213 udma_enable |= (1 << devid);
0214
0215
0216 pci_read_config_word(dev, 0x4A, &udma_timing);
0217 udma_timing &= ~(3 << (4 * devid));
0218 udma_timing |= u_speed << (4 * devid);
0219 pci_write_config_word(dev, 0x4A, udma_timing);
0220
0221
0222 pci_read_config_word(dev, 0x54, &ideconf);
0223 ideconf &= ~(0x1001 << devid);
0224 ideconf |= u_clock << devid;
0225 pci_write_config_word(dev, 0x54, ideconf);
0226 } else {
0227
0228
0229
0230
0231
0232 unsigned int mwdma = adev->dma_mode - XFER_MW_DMA_0;
0233 unsigned int control;
0234 u8 slave_data;
0235 const unsigned int needed_pio[3] = {
0236 XFER_PIO_0, XFER_PIO_3, XFER_PIO_4
0237 };
0238 int pio = needed_pio[mwdma] - XFER_PIO_0;
0239
0240 control = 3;
0241
0242
0243
0244
0245 if (adev->pio_mode < needed_pio[mwdma])
0246
0247 control |= 8;
0248
0249 if (adev->devno) {
0250 master_data &= 0xFF4F;
0251 master_data |= control << 4;
0252 pci_read_config_byte(dev, 0x44, &slave_data);
0253 slave_data &= (ap->port_no ? 0x0f : 0xf0);
0254
0255 slave_data |= ((timings[pio][0] << 2) | timings[pio][1]) << (ap->port_no ? 4 : 0);
0256 pci_write_config_byte(dev, 0x44, slave_data);
0257 } else {
0258 master_data &= 0xCCF4;
0259
0260 master_data |= control;
0261 master_data |=
0262 (timings[pio][0] << 12) |
0263 (timings[pio][1] << 8);
0264 }
0265
0266 udma_enable &= ~(1 << devid);
0267 pci_write_config_word(dev, master_port, master_data);
0268 }
0269 pci_write_config_byte(dev, 0x48, udma_enable);
0270
0271 spin_unlock_irqrestore(&rdc_lock, flags);
0272 }
0273
0274 static struct ata_port_operations rdc_pata_ops = {
0275 .inherits = &ata_bmdma32_port_ops,
0276 .cable_detect = rdc_pata_cable_detect,
0277 .set_piomode = rdc_set_piomode,
0278 .set_dmamode = rdc_set_dmamode,
0279 .prereset = rdc_pata_prereset,
0280 };
0281
0282 static const struct ata_port_info rdc_port_info = {
0283
0284 .flags = ATA_FLAG_SLAVE_POSS,
0285 .pio_mask = ATA_PIO4,
0286 .mwdma_mask = ATA_MWDMA12_ONLY,
0287 .udma_mask = ATA_UDMA5,
0288 .port_ops = &rdc_pata_ops,
0289 };
0290
0291 static struct scsi_host_template rdc_sht = {
0292 ATA_BMDMA_SHT(DRV_NAME),
0293 };
0294
0295
0296
0297
0298
0299
0300
0301
0302
0303
0304
0305
0306
0307
0308
0309
0310 static int rdc_init_one(struct pci_dev *pdev, const struct pci_device_id *ent)
0311 {
0312 struct device *dev = &pdev->dev;
0313 struct ata_port_info port_info[2];
0314 const struct ata_port_info *ppi[] = { &port_info[0], &port_info[1] };
0315 struct ata_host *host;
0316 struct rdc_host_priv *hpriv;
0317 int rc;
0318
0319 ata_print_version_once(&pdev->dev, DRV_VERSION);
0320
0321 port_info[0] = rdc_port_info;
0322 port_info[1] = rdc_port_info;
0323
0324
0325 rc = pcim_enable_device(pdev);
0326 if (rc)
0327 return rc;
0328
0329 hpriv = devm_kzalloc(dev, sizeof(*hpriv), GFP_KERNEL);
0330 if (!hpriv)
0331 return -ENOMEM;
0332
0333
0334
0335
0336 pci_read_config_dword(pdev, 0x54, &hpriv->saved_iocfg);
0337
0338 rc = ata_pci_bmdma_prepare_host(pdev, ppi, &host);
0339 if (rc)
0340 return rc;
0341 host->private_data = hpriv;
0342
0343 pci_intx(pdev, 1);
0344
0345 host->flags |= ATA_HOST_PARALLEL_SCAN;
0346
0347 pci_set_master(pdev);
0348 return ata_pci_sff_activate_host(host, ata_bmdma_interrupt, &rdc_sht);
0349 }
0350
0351 static void rdc_remove_one(struct pci_dev *pdev)
0352 {
0353 struct ata_host *host = pci_get_drvdata(pdev);
0354 struct rdc_host_priv *hpriv = host->private_data;
0355
0356 pci_write_config_dword(pdev, 0x54, hpriv->saved_iocfg);
0357
0358 ata_pci_remove_one(pdev);
0359 }
0360
0361 static const struct pci_device_id rdc_pci_tbl[] = {
0362 { PCI_DEVICE(0x17F3, 0x1011), },
0363 { PCI_DEVICE(0x17F3, 0x1012), },
0364 { }
0365 };
0366
0367 static struct pci_driver rdc_pci_driver = {
0368 .name = DRV_NAME,
0369 .id_table = rdc_pci_tbl,
0370 .probe = rdc_init_one,
0371 .remove = rdc_remove_one,
0372 #ifdef CONFIG_PM_SLEEP
0373 .suspend = ata_pci_device_suspend,
0374 .resume = ata_pci_device_resume,
0375 #endif
0376 };
0377
0378
0379 module_pci_driver(rdc_pci_driver);
0380
0381 MODULE_AUTHOR("Alan Cox (based on ata_piix)");
0382 MODULE_DESCRIPTION("SCSI low-level driver for RDC PATA controllers");
0383 MODULE_LICENSE("GPL");
0384 MODULE_DEVICE_TABLE(pci, rdc_pci_tbl);
0385 MODULE_VERSION(DRV_VERSION);