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