16b406782SAlan Cox /* 26b406782SAlan Cox * pata_rdc - Driver for later RDC PATA controllers 36b406782SAlan Cox * 46b406782SAlan Cox * This is actually a driver for hardware meeting 56b406782SAlan Cox * INCITS 370-2004 (1510D): ATA Host Adapter Standards 66b406782SAlan Cox * 76b406782SAlan Cox * Based on ata_piix. 86b406782SAlan Cox * 96b406782SAlan Cox * This program is free software; you can redistribute it and/or modify 106b406782SAlan Cox * it under the terms of the GNU General Public License as published by 116b406782SAlan Cox * the Free Software Foundation; either version 2, or (at your option) 126b406782SAlan Cox * any later version. 136b406782SAlan Cox * 146b406782SAlan Cox * This program is distributed in the hope that it will be useful, 156b406782SAlan Cox * but WITHOUT ANY WARRANTY; without even the implied warranty of 166b406782SAlan Cox * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 176b406782SAlan Cox * GNU General Public License for more details. 186b406782SAlan Cox * 196b406782SAlan Cox * You should have received a copy of the GNU General Public License 206b406782SAlan Cox * along with this program; see the file COPYING. If not, write to 216b406782SAlan Cox * the Free Software Foundation, 675 Mass Ave, Cambridge, MA 02139, USA. 226b406782SAlan Cox */ 236b406782SAlan Cox 246b406782SAlan Cox #include <linux/kernel.h> 256b406782SAlan Cox #include <linux/module.h> 266b406782SAlan Cox #include <linux/pci.h> 276b406782SAlan Cox #include <linux/init.h> 286b406782SAlan Cox #include <linux/blkdev.h> 296b406782SAlan Cox #include <linux/delay.h> 306b406782SAlan Cox #include <linux/device.h> 315a0e3ad6STejun Heo #include <linux/gfp.h> 326b406782SAlan Cox #include <scsi/scsi_host.h> 336b406782SAlan Cox #include <linux/libata.h> 346b406782SAlan Cox #include <linux/dmi.h> 356b406782SAlan Cox 366b406782SAlan Cox #define DRV_NAME "pata_rdc" 376b406782SAlan Cox #define DRV_VERSION "0.01" 386b406782SAlan Cox 396b406782SAlan Cox struct rdc_host_priv { 406b406782SAlan Cox u32 saved_iocfg; 416b406782SAlan Cox }; 426b406782SAlan Cox 436b406782SAlan Cox /** 446b406782SAlan Cox * rdc_pata_cable_detect - Probe host controller cable detect info 456b406782SAlan Cox * @ap: Port for which cable detect info is desired 466b406782SAlan Cox * 476b406782SAlan Cox * Read 80c cable indicator from ATA PCI device's PCI config 486b406782SAlan Cox * register. This register is normally set by firmware (BIOS). 496b406782SAlan Cox * 506b406782SAlan Cox * LOCKING: 516b406782SAlan Cox * None (inherited from caller). 526b406782SAlan Cox */ 536b406782SAlan Cox 546b406782SAlan Cox static int rdc_pata_cable_detect(struct ata_port *ap) 556b406782SAlan Cox { 566b406782SAlan Cox struct rdc_host_priv *hpriv = ap->host->private_data; 576b406782SAlan Cox u8 mask; 586b406782SAlan Cox 596b406782SAlan Cox /* check BIOS cable detect results */ 606b406782SAlan Cox mask = 0x30 << (2 * ap->port_no); 616b406782SAlan Cox if ((hpriv->saved_iocfg & mask) == 0) 626b406782SAlan Cox return ATA_CBL_PATA40; 636b406782SAlan Cox return ATA_CBL_PATA80; 646b406782SAlan Cox } 656b406782SAlan Cox 666b406782SAlan Cox /** 676b406782SAlan Cox * rdc_pata_prereset - prereset for PATA host controller 686b406782SAlan Cox * @link: Target link 696b406782SAlan Cox * @deadline: deadline jiffies for the operation 706b406782SAlan Cox * 716b406782SAlan Cox * LOCKING: 726b406782SAlan Cox * None (inherited from caller). 736b406782SAlan Cox */ 746b406782SAlan Cox static int rdc_pata_prereset(struct ata_link *link, unsigned long deadline) 756b406782SAlan Cox { 766b406782SAlan Cox struct ata_port *ap = link->ap; 776b406782SAlan Cox struct pci_dev *pdev = to_pci_dev(ap->host->dev); 786b406782SAlan Cox 796b406782SAlan Cox static const struct pci_bits rdc_enable_bits[] = { 806b406782SAlan Cox { 0x41U, 1U, 0x80UL, 0x80UL }, /* port 0 */ 816b406782SAlan Cox { 0x43U, 1U, 0x80UL, 0x80UL }, /* port 1 */ 826b406782SAlan Cox }; 836b406782SAlan Cox 846b406782SAlan Cox if (!pci_test_config_bits(pdev, &rdc_enable_bits[ap->port_no])) 856b406782SAlan Cox return -ENOENT; 866b406782SAlan Cox return ata_sff_prereset(link, deadline); 876b406782SAlan Cox } 886b406782SAlan Cox 896b406782SAlan Cox /** 906b406782SAlan Cox * rdc_set_piomode - Initialize host controller PATA PIO timings 916b406782SAlan Cox * @ap: Port whose timings we are configuring 926b406782SAlan Cox * @adev: um 936b406782SAlan Cox * 946b406782SAlan Cox * Set PIO mode for device, in host controller PCI config space. 956b406782SAlan Cox * 966b406782SAlan Cox * LOCKING: 976b406782SAlan Cox * None (inherited from caller). 986b406782SAlan Cox */ 996b406782SAlan Cox 1006b406782SAlan Cox static void rdc_set_piomode(struct ata_port *ap, struct ata_device *adev) 1016b406782SAlan Cox { 1026b406782SAlan Cox unsigned int pio = adev->pio_mode - XFER_PIO_0; 1036b406782SAlan Cox struct pci_dev *dev = to_pci_dev(ap->host->dev); 1046b406782SAlan Cox unsigned int is_slave = (adev->devno != 0); 1056b406782SAlan Cox unsigned int master_port= ap->port_no ? 0x42 : 0x40; 1066b406782SAlan Cox unsigned int slave_port = 0x44; 1076b406782SAlan Cox u16 master_data; 1086b406782SAlan Cox u8 slave_data; 1096b406782SAlan Cox u8 udma_enable; 1106b406782SAlan Cox int control = 0; 1116b406782SAlan Cox 1126b406782SAlan Cox static const /* ISP RTC */ 1136b406782SAlan Cox u8 timings[][2] = { { 0, 0 }, 1146b406782SAlan Cox { 0, 0 }, 1156b406782SAlan Cox { 1, 0 }, 1166b406782SAlan Cox { 2, 1 }, 1176b406782SAlan Cox { 2, 3 }, }; 1186b406782SAlan Cox 1196b406782SAlan Cox if (pio >= 2) 1206b406782SAlan Cox control |= 1; /* TIME1 enable */ 1216b406782SAlan Cox if (ata_pio_need_iordy(adev)) 1226b406782SAlan Cox control |= 2; /* IE enable */ 1236b406782SAlan Cox 1246b406782SAlan Cox if (adev->class == ATA_DEV_ATA) 1256b406782SAlan Cox control |= 4; /* PPE enable */ 1266b406782SAlan Cox 1276b406782SAlan Cox /* PIO configuration clears DTE unconditionally. It will be 1286b406782SAlan Cox * programmed in set_dmamode which is guaranteed to be called 1296b406782SAlan Cox * after set_piomode if any DMA mode is available. 1306b406782SAlan Cox */ 1316b406782SAlan Cox pci_read_config_word(dev, master_port, &master_data); 1326b406782SAlan Cox if (is_slave) { 1336b406782SAlan Cox /* clear TIME1|IE1|PPE1|DTE1 */ 1346b406782SAlan Cox master_data &= 0xff0f; 1356b406782SAlan Cox /* Enable SITRE (separate slave timing register) */ 1366b406782SAlan Cox master_data |= 0x4000; 1376b406782SAlan Cox /* enable PPE1, IE1 and TIME1 as needed */ 1386b406782SAlan Cox master_data |= (control << 4); 1396b406782SAlan Cox pci_read_config_byte(dev, slave_port, &slave_data); 1406b406782SAlan Cox slave_data &= (ap->port_no ? 0x0f : 0xf0); 1416b406782SAlan Cox /* Load the timing nibble for this slave */ 1426b406782SAlan Cox slave_data |= ((timings[pio][0] << 2) | timings[pio][1]) 1436b406782SAlan Cox << (ap->port_no ? 4 : 0); 1446b406782SAlan Cox } else { 1456b406782SAlan Cox /* clear ISP|RCT|TIME0|IE0|PPE0|DTE0 */ 1466b406782SAlan Cox master_data &= 0xccf0; 1476b406782SAlan Cox /* Enable PPE, IE and TIME as appropriate */ 1486b406782SAlan Cox master_data |= control; 1496b406782SAlan Cox /* load ISP and RCT */ 1506b406782SAlan Cox master_data |= 1516b406782SAlan Cox (timings[pio][0] << 12) | 1526b406782SAlan Cox (timings[pio][1] << 8); 1536b406782SAlan Cox } 1546b406782SAlan Cox pci_write_config_word(dev, master_port, master_data); 1556b406782SAlan Cox if (is_slave) 1566b406782SAlan Cox pci_write_config_byte(dev, slave_port, slave_data); 1576b406782SAlan Cox 1586b406782SAlan Cox /* Ensure the UDMA bit is off - it will be turned back on if 1596b406782SAlan Cox UDMA is selected */ 1606b406782SAlan Cox 1616b406782SAlan Cox pci_read_config_byte(dev, 0x48, &udma_enable); 1626b406782SAlan Cox udma_enable &= ~(1 << (2 * ap->port_no + adev->devno)); 1636b406782SAlan Cox pci_write_config_byte(dev, 0x48, udma_enable); 1646b406782SAlan Cox } 1656b406782SAlan Cox 1666b406782SAlan Cox /** 1676b406782SAlan Cox * rdc_set_dmamode - Initialize host controller PATA PIO timings 1686b406782SAlan Cox * @ap: Port whose timings we are configuring 1696b406782SAlan Cox * @adev: Drive in question 1706b406782SAlan Cox * 1716b406782SAlan Cox * Set UDMA mode for device, in host controller PCI config space. 1726b406782SAlan Cox * 1736b406782SAlan Cox * LOCKING: 1746b406782SAlan Cox * None (inherited from caller). 1756b406782SAlan Cox */ 1766b406782SAlan Cox 1776b406782SAlan Cox static void rdc_set_dmamode(struct ata_port *ap, struct ata_device *adev) 1786b406782SAlan Cox { 1796b406782SAlan Cox struct pci_dev *dev = to_pci_dev(ap->host->dev); 1806b406782SAlan Cox u8 master_port = ap->port_no ? 0x42 : 0x40; 1816b406782SAlan Cox u16 master_data; 1826b406782SAlan Cox u8 speed = adev->dma_mode; 1836b406782SAlan Cox int devid = adev->devno + 2 * ap->port_no; 1846b406782SAlan Cox u8 udma_enable = 0; 1856b406782SAlan Cox 1866b406782SAlan Cox static const /* ISP RTC */ 1876b406782SAlan Cox u8 timings[][2] = { { 0, 0 }, 1886b406782SAlan Cox { 0, 0 }, 1896b406782SAlan Cox { 1, 0 }, 1906b406782SAlan Cox { 2, 1 }, 1916b406782SAlan Cox { 2, 3 }, }; 1926b406782SAlan Cox 1936b406782SAlan Cox pci_read_config_word(dev, master_port, &master_data); 1946b406782SAlan Cox pci_read_config_byte(dev, 0x48, &udma_enable); 1956b406782SAlan Cox 1966b406782SAlan Cox if (speed >= XFER_UDMA_0) { 1976b406782SAlan Cox unsigned int udma = adev->dma_mode - XFER_UDMA_0; 1986b406782SAlan Cox u16 udma_timing; 1996b406782SAlan Cox u16 ideconf; 2006b406782SAlan Cox int u_clock, u_speed; 2016b406782SAlan Cox 2026b406782SAlan Cox /* 2036b406782SAlan Cox * UDMA is handled by a combination of clock switching and 2046b406782SAlan Cox * selection of dividers 2056b406782SAlan Cox * 2066b406782SAlan Cox * Handy rule: Odd modes are UDMATIMx 01, even are 02 2076b406782SAlan Cox * except UDMA0 which is 00 2086b406782SAlan Cox */ 2096b406782SAlan Cox u_speed = min(2 - (udma & 1), udma); 2106b406782SAlan Cox if (udma == 5) 2116b406782SAlan Cox u_clock = 0x1000; /* 100Mhz */ 2126b406782SAlan Cox else if (udma > 2) 2136b406782SAlan Cox u_clock = 1; /* 66Mhz */ 2146b406782SAlan Cox else 2156b406782SAlan Cox u_clock = 0; /* 33Mhz */ 2166b406782SAlan Cox 2176b406782SAlan Cox udma_enable |= (1 << devid); 2186b406782SAlan Cox 2196b406782SAlan Cox /* Load the CT/RP selection */ 2206b406782SAlan Cox pci_read_config_word(dev, 0x4A, &udma_timing); 2216b406782SAlan Cox udma_timing &= ~(3 << (4 * devid)); 2226b406782SAlan Cox udma_timing |= u_speed << (4 * devid); 2236b406782SAlan Cox pci_write_config_word(dev, 0x4A, udma_timing); 2246b406782SAlan Cox 2256b406782SAlan Cox /* Select a 33/66/100Mhz clock */ 2266b406782SAlan Cox pci_read_config_word(dev, 0x54, &ideconf); 2276b406782SAlan Cox ideconf &= ~(0x1001 << devid); 2286b406782SAlan Cox ideconf |= u_clock << devid; 2296b406782SAlan Cox pci_write_config_word(dev, 0x54, ideconf); 2306b406782SAlan Cox } else { 2316b406782SAlan Cox /* 2326b406782SAlan Cox * MWDMA is driven by the PIO timings. We must also enable 2336b406782SAlan Cox * IORDY unconditionally along with TIME1. PPE has already 2346b406782SAlan Cox * been set when the PIO timing was set. 2356b406782SAlan Cox */ 2366b406782SAlan Cox unsigned int mwdma = adev->dma_mode - XFER_MW_DMA_0; 2376b406782SAlan Cox unsigned int control; 2386b406782SAlan Cox u8 slave_data; 2396b406782SAlan Cox const unsigned int needed_pio[3] = { 2406b406782SAlan Cox XFER_PIO_0, XFER_PIO_3, XFER_PIO_4 2416b406782SAlan Cox }; 2426b406782SAlan Cox int pio = needed_pio[mwdma] - XFER_PIO_0; 2436b406782SAlan Cox 2446b406782SAlan Cox control = 3; /* IORDY|TIME1 */ 2456b406782SAlan Cox 2466b406782SAlan Cox /* If the drive MWDMA is faster than it can do PIO then 2476b406782SAlan Cox we must force PIO into PIO0 */ 2486b406782SAlan Cox 2496b406782SAlan Cox if (adev->pio_mode < needed_pio[mwdma]) 2506b406782SAlan Cox /* Enable DMA timing only */ 2516b406782SAlan Cox control |= 8; /* PIO cycles in PIO0 */ 2526b406782SAlan Cox 2536b406782SAlan Cox if (adev->devno) { /* Slave */ 2546b406782SAlan Cox master_data &= 0xFF4F; /* Mask out IORDY|TIME1|DMAONLY */ 2556b406782SAlan Cox master_data |= control << 4; 2566b406782SAlan Cox pci_read_config_byte(dev, 0x44, &slave_data); 2576b406782SAlan Cox slave_data &= (ap->port_no ? 0x0f : 0xf0); 2586b406782SAlan Cox /* Load the matching timing */ 2596b406782SAlan Cox slave_data |= ((timings[pio][0] << 2) | timings[pio][1]) << (ap->port_no ? 4 : 0); 2606b406782SAlan Cox pci_write_config_byte(dev, 0x44, slave_data); 2616b406782SAlan Cox } else { /* Master */ 2626b406782SAlan Cox master_data &= 0xCCF4; /* Mask out IORDY|TIME1|DMAONLY 2636b406782SAlan Cox and master timing bits */ 2646b406782SAlan Cox master_data |= control; 2656b406782SAlan Cox master_data |= 2666b406782SAlan Cox (timings[pio][0] << 12) | 2676b406782SAlan Cox (timings[pio][1] << 8); 2686b406782SAlan Cox } 2696b406782SAlan Cox 2706b406782SAlan Cox udma_enable &= ~(1 << devid); 2716b406782SAlan Cox pci_write_config_word(dev, master_port, master_data); 2726b406782SAlan Cox } 2736b406782SAlan Cox pci_write_config_byte(dev, 0x48, udma_enable); 2746b406782SAlan Cox } 2756b406782SAlan Cox 2766b406782SAlan Cox static struct ata_port_operations rdc_pata_ops = { 2776b406782SAlan Cox .inherits = &ata_bmdma32_port_ops, 2786b406782SAlan Cox .cable_detect = rdc_pata_cable_detect, 2796b406782SAlan Cox .set_piomode = rdc_set_piomode, 2806b406782SAlan Cox .set_dmamode = rdc_set_dmamode, 2816b406782SAlan Cox .prereset = rdc_pata_prereset, 2826b406782SAlan Cox }; 2836b406782SAlan Cox 2846b406782SAlan Cox static struct ata_port_info rdc_port_info = { 2856b406782SAlan Cox 2866b406782SAlan Cox .flags = ATA_FLAG_SLAVE_POSS, 2876b406782SAlan Cox .pio_mask = ATA_PIO4, 28882563232SBartlomiej Zolnierkiewicz .mwdma_mask = ATA_MWDMA12_ONLY, 2896b406782SAlan Cox .udma_mask = ATA_UDMA5, 2906b406782SAlan Cox .port_ops = &rdc_pata_ops, 2916b406782SAlan Cox }; 2926b406782SAlan Cox 2936b406782SAlan Cox static struct scsi_host_template rdc_sht = { 2946b406782SAlan Cox ATA_BMDMA_SHT(DRV_NAME), 2956b406782SAlan Cox }; 2966b406782SAlan Cox 2976b406782SAlan Cox /** 2986b406782SAlan Cox * rdc_init_one - Register PIIX ATA PCI device with kernel services 2996b406782SAlan Cox * @pdev: PCI device to register 3006b406782SAlan Cox * @ent: Entry in rdc_pci_tbl matching with @pdev 3016b406782SAlan Cox * 3026b406782SAlan Cox * Called from kernel PCI layer. We probe for combined mode (sigh), 3036b406782SAlan Cox * and then hand over control to libata, for it to do the rest. 3046b406782SAlan Cox * 3056b406782SAlan Cox * LOCKING: 3066b406782SAlan Cox * Inherited from PCI layer (may sleep). 3076b406782SAlan Cox * 3086b406782SAlan Cox * RETURNS: 3096b406782SAlan Cox * Zero on success, or -ERRNO value. 3106b406782SAlan Cox */ 3116b406782SAlan Cox 3126b406782SAlan Cox static int __devinit rdc_init_one(struct pci_dev *pdev, 3136b406782SAlan Cox const struct pci_device_id *ent) 3146b406782SAlan Cox { 3156b406782SAlan Cox struct device *dev = &pdev->dev; 3166b406782SAlan Cox struct ata_port_info port_info[2]; 3176b406782SAlan Cox const struct ata_port_info *ppi[] = { &port_info[0], &port_info[1] }; 3186b406782SAlan Cox unsigned long port_flags; 3196b406782SAlan Cox struct ata_host *host; 3206b406782SAlan Cox struct rdc_host_priv *hpriv; 3216b406782SAlan Cox int rc; 3226b406782SAlan Cox 323*06296a1eSJoe Perches ata_print_version_once(&pdev->dev, DRV_VERSION); 3246b406782SAlan Cox 3256b406782SAlan Cox port_info[0] = rdc_port_info; 3266b406782SAlan Cox port_info[1] = rdc_port_info; 3276b406782SAlan Cox 3286b406782SAlan Cox port_flags = port_info[0].flags; 3296b406782SAlan Cox 3306b406782SAlan Cox /* enable device and prepare host */ 3316b406782SAlan Cox rc = pcim_enable_device(pdev); 3326b406782SAlan Cox if (rc) 3336b406782SAlan Cox return rc; 3346b406782SAlan Cox 3356b406782SAlan Cox hpriv = devm_kzalloc(dev, sizeof(*hpriv), GFP_KERNEL); 3366b406782SAlan Cox if (!hpriv) 3376b406782SAlan Cox return -ENOMEM; 3386b406782SAlan Cox 3396b406782SAlan Cox /* Save IOCFG, this will be used for cable detection, quirk 3406b406782SAlan Cox * detection and restoration on detach. 3416b406782SAlan Cox */ 3426b406782SAlan Cox pci_read_config_dword(pdev, 0x54, &hpriv->saved_iocfg); 3436b406782SAlan Cox 3441c5afdf7STejun Heo rc = ata_pci_bmdma_prepare_host(pdev, ppi, &host); 3456b406782SAlan Cox if (rc) 3466b406782SAlan Cox return rc; 3476b406782SAlan Cox host->private_data = hpriv; 3486b406782SAlan Cox 3496b406782SAlan Cox pci_intx(pdev, 1); 3506b406782SAlan Cox 3516b406782SAlan Cox host->flags |= ATA_HOST_PARALLEL_SCAN; 3526b406782SAlan Cox 3536b406782SAlan Cox pci_set_master(pdev); 354c3b28894STejun Heo return ata_pci_sff_activate_host(host, ata_bmdma_interrupt, &rdc_sht); 3556b406782SAlan Cox } 3566b406782SAlan Cox 3576b406782SAlan Cox static void rdc_remove_one(struct pci_dev *pdev) 3586b406782SAlan Cox { 3596b406782SAlan Cox struct ata_host *host = dev_get_drvdata(&pdev->dev); 3606b406782SAlan Cox struct rdc_host_priv *hpriv = host->private_data; 3616b406782SAlan Cox 3626b406782SAlan Cox pci_write_config_dword(pdev, 0x54, hpriv->saved_iocfg); 3636b406782SAlan Cox 3646b406782SAlan Cox ata_pci_remove_one(pdev); 3656b406782SAlan Cox } 3666b406782SAlan Cox 3676b406782SAlan Cox static const struct pci_device_id rdc_pci_tbl[] = { 3686b406782SAlan Cox { PCI_DEVICE(0x17F3, 0x1011), }, 3696b406782SAlan Cox { PCI_DEVICE(0x17F3, 0x1012), }, 3706b406782SAlan Cox { } /* terminate list */ 3716b406782SAlan Cox }; 3726b406782SAlan Cox 3736b406782SAlan Cox static struct pci_driver rdc_pci_driver = { 3746b406782SAlan Cox .name = DRV_NAME, 3756b406782SAlan Cox .id_table = rdc_pci_tbl, 3766b406782SAlan Cox .probe = rdc_init_one, 3776b406782SAlan Cox .remove = rdc_remove_one, 3786b406782SAlan Cox }; 3796b406782SAlan Cox 3806b406782SAlan Cox 3816b406782SAlan Cox static int __init rdc_init(void) 3826b406782SAlan Cox { 3836b406782SAlan Cox return pci_register_driver(&rdc_pci_driver); 3846b406782SAlan Cox } 3856b406782SAlan Cox 3866b406782SAlan Cox static void __exit rdc_exit(void) 3876b406782SAlan Cox { 3886b406782SAlan Cox pci_unregister_driver(&rdc_pci_driver); 3896b406782SAlan Cox } 3906b406782SAlan Cox 3916b406782SAlan Cox module_init(rdc_init); 3926b406782SAlan Cox module_exit(rdc_exit); 3936b406782SAlan Cox 3946b406782SAlan Cox MODULE_AUTHOR("Alan Cox (based on ata_piix)"); 3956b406782SAlan Cox MODULE_DESCRIPTION("SCSI low-level driver for RDC PATA controllers"); 3966b406782SAlan Cox MODULE_LICENSE("GPL"); 3976b406782SAlan Cox MODULE_DEVICE_TABLE(pci, rdc_pci_tbl); 3986b406782SAlan Cox MODULE_VERSION(DRV_VERSION); 399