blob: e08b0aac08b9e2f160c714d33904fe87bbd192aa [file] [log] [blame]
Thomas Gleixner09c434b2019-05-19 13:08:20 +01001// SPDX-License-Identifier: GPL-2.0-only
Linus Torvalds1da177e2005-04-16 15:20:36 -07002/*
Linus Torvalds1da177e2005-04-16 15:20:36 -07003 * Copyright (C) 2003 ATI Inc. <hyu@ati.com>
Bartlomiej Zolnierkiewicz485efc62007-07-20 01:11:54 +02004 * Copyright (C) 2004,2007 Bartlomiej Zolnierkiewicz
Linus Torvalds1da177e2005-04-16 15:20:36 -07005 */
6
Linus Torvalds1da177e2005-04-16 15:20:36 -07007#include <linux/types.h>
8#include <linux/module.h>
9#include <linux/kernel.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070010#include <linux/pci.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070011#include <linux/ide.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070012#include <linux/init.h>
13
Bartlomiej Zolnierkiewiczced3ec82008-07-24 22:53:32 +020014#define DRV_NAME "atiixp"
15
Linus Torvalds1da177e2005-04-16 15:20:36 -070016#define ATIIXP_IDE_PIO_TIMING 0x40
17#define ATIIXP_IDE_MDMA_TIMING 0x44
18#define ATIIXP_IDE_PIO_CONTROL 0x48
19#define ATIIXP_IDE_PIO_MODE 0x4a
20#define ATIIXP_IDE_UDMA_CONTROL 0x54
21#define ATIIXP_IDE_UDMA_MODE 0x56
22
Himangi Saraogi7546e522014-08-14 22:14:30 +053023struct atiixp_ide_timing {
Linus Torvalds1da177e2005-04-16 15:20:36 -070024 u8 command_width;
25 u8 recover_width;
Himangi Saraogi7546e522014-08-14 22:14:30 +053026};
Linus Torvalds1da177e2005-04-16 15:20:36 -070027
Himangi Saraogi7546e522014-08-14 22:14:30 +053028static struct atiixp_ide_timing pio_timing[] = {
Linus Torvalds1da177e2005-04-16 15:20:36 -070029 { 0x05, 0x0d },
30 { 0x04, 0x07 },
31 { 0x03, 0x04 },
32 { 0x02, 0x02 },
33 { 0x02, 0x00 },
34};
35
Himangi Saraogi7546e522014-08-14 22:14:30 +053036static struct atiixp_ide_timing mdma_timing[] = {
Linus Torvalds1da177e2005-04-16 15:20:36 -070037 { 0x07, 0x07 },
38 { 0x02, 0x01 },
39 { 0x02, 0x00 },
40};
41
Alan6c5f8cc2007-01-05 16:36:27 -080042static DEFINE_SPINLOCK(atiixp_lock);
43
Linus Torvalds1da177e2005-04-16 15:20:36 -070044/**
Bartlomiej Zolnierkiewicz88b2b322007-10-13 17:47:51 +020045 * atiixp_set_pio_mode - set host controller for PIO mode
Bartlomiej Zolnierkiewicze085b3c2010-01-19 01:44:41 -080046 * @hwif: port
Bartlomiej Zolnierkiewicz88b2b322007-10-13 17:47:51 +020047 * @drive: drive
Linus Torvalds1da177e2005-04-16 15:20:36 -070048 *
49 * Set the interface PIO mode.
50 */
51
Bartlomiej Zolnierkiewicze085b3c2010-01-19 01:44:41 -080052static void atiixp_set_pio_mode(ide_hwif_t *hwif, ide_drive_t *drive)
Linus Torvalds1da177e2005-04-16 15:20:36 -070053{
Bartlomiej Zolnierkiewicze085b3c2010-01-19 01:44:41 -080054 struct pci_dev *dev = to_pci_dev(hwif->dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -070055 unsigned long flags;
Roel Kluinf76bee12009-02-25 20:28:22 +010056 int timing_shift = (drive->dn ^ 1) * 8;
Linus Torvalds1da177e2005-04-16 15:20:36 -070057 u32 pio_timing_data;
58 u16 pio_mode_data;
Bartlomiej Zolnierkiewicze085b3c2010-01-19 01:44:41 -080059 const u8 pio = drive->pio_mode - XFER_PIO_0;
Linus Torvalds1da177e2005-04-16 15:20:36 -070060
Alan6c5f8cc2007-01-05 16:36:27 -080061 spin_lock_irqsave(&atiixp_lock, flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -070062
63 pci_read_config_word(dev, ATIIXP_IDE_PIO_MODE, &pio_mode_data);
64 pio_mode_data &= ~(0x07 << (drive->dn * 4));
65 pio_mode_data |= (pio << (drive->dn * 4));
66 pci_write_config_word(dev, ATIIXP_IDE_PIO_MODE, pio_mode_data);
67
68 pci_read_config_dword(dev, ATIIXP_IDE_PIO_TIMING, &pio_timing_data);
69 pio_timing_data &= ~(0xff << timing_shift);
70 pio_timing_data |= (pio_timing[pio].recover_width << timing_shift) |
71 (pio_timing[pio].command_width << (timing_shift + 4));
72 pci_write_config_dword(dev, ATIIXP_IDE_PIO_TIMING, pio_timing_data);
73
Alan6c5f8cc2007-01-05 16:36:27 -080074 spin_unlock_irqrestore(&atiixp_lock, flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -070075}
76
77/**
Bartlomiej Zolnierkiewicz88b2b322007-10-13 17:47:51 +020078 * atiixp_set_dma_mode - set host controller for DMA mode
Bartlomiej Zolnierkiewicz87761682010-01-19 01:45:29 -080079 * @hwif: port
Bartlomiej Zolnierkiewicz88b2b322007-10-13 17:47:51 +020080 * @drive: drive
Linus Torvalds1da177e2005-04-16 15:20:36 -070081 *
Bartlomiej Zolnierkiewicz88b2b322007-10-13 17:47:51 +020082 * Set a ATIIXP host controller to the desired DMA mode. This involves
83 * programming the right timing data into the PCI configuration space.
Linus Torvalds1da177e2005-04-16 15:20:36 -070084 */
85
Bartlomiej Zolnierkiewicz87761682010-01-19 01:45:29 -080086static void atiixp_set_dma_mode(ide_hwif_t *hwif, ide_drive_t *drive)
Linus Torvalds1da177e2005-04-16 15:20:36 -070087{
Bartlomiej Zolnierkiewicz87761682010-01-19 01:45:29 -080088 struct pci_dev *dev = to_pci_dev(hwif->dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -070089 unsigned long flags;
Roel Kluinf76bee12009-02-25 20:28:22 +010090 int timing_shift = (drive->dn ^ 1) * 8;
Linus Torvalds1da177e2005-04-16 15:20:36 -070091 u32 tmp32;
92 u16 tmp16;
Bartlomiej Zolnierkiewicz8ae60e32008-01-26 20:13:02 +010093 u16 udma_ctl = 0;
Bartlomiej Zolnierkiewicz87761682010-01-19 01:45:29 -080094 const u8 speed = drive->dma_mode;
Bartlomiej Zolnierkiewicz94c7fa02007-10-16 22:29:53 +020095
Alan6c5f8cc2007-01-05 16:36:27 -080096 spin_lock_irqsave(&atiixp_lock, flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -070097
Bartlomiej Zolnierkiewicz8ae60e32008-01-26 20:13:02 +010098 pci_read_config_word(dev, ATIIXP_IDE_UDMA_CONTROL, &udma_ctl);
99
Linus Torvalds1da177e2005-04-16 15:20:36 -0700100 if (speed >= XFER_UDMA_0) {
101 pci_read_config_word(dev, ATIIXP_IDE_UDMA_MODE, &tmp16);
102 tmp16 &= ~(0x07 << (drive->dn * 4));
103 tmp16 |= ((speed & 0x07) << (drive->dn * 4));
104 pci_write_config_word(dev, ATIIXP_IDE_UDMA_MODE, tmp16);
Bartlomiej Zolnierkiewicz8ae60e32008-01-26 20:13:02 +0100105
106 udma_ctl |= (1 << drive->dn);
107 } else if (speed >= XFER_MW_DMA_0) {
108 u8 i = speed & 0x03;
109
110 pci_read_config_dword(dev, ATIIXP_IDE_MDMA_TIMING, &tmp32);
111 tmp32 &= ~(0xff << timing_shift);
112 tmp32 |= (mdma_timing[i].recover_width << timing_shift) |
113 (mdma_timing[i].command_width << (timing_shift + 4));
114 pci_write_config_dword(dev, ATIIXP_IDE_MDMA_TIMING, tmp32);
115
116 udma_ctl &= ~(1 << drive->dn);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700117 }
118
Bartlomiej Zolnierkiewicz8ae60e32008-01-26 20:13:02 +0100119 pci_write_config_word(dev, ATIIXP_IDE_UDMA_CONTROL, udma_ctl);
120
Alan6c5f8cc2007-01-05 16:36:27 -0800121 spin_unlock_irqrestore(&atiixp_lock, flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700122}
123
Bartlomiej Zolnierkiewiczf454cbe2008-08-05 18:17:04 +0200124static u8 atiixp_cable_detect(ide_hwif_t *hwif)
Bartlomiej Zolnierkiewiczb4d1c732008-02-02 19:56:29 +0100125{
126 struct pci_dev *pdev = to_pci_dev(hwif->dev);
127 u8 udma_mode = 0, ch = hwif->channel;
128
129 pci_read_config_byte(pdev, ATIIXP_IDE_UDMA_MODE + ch, &udma_mode);
130
131 if ((udma_mode & 0x07) >= 0x04 || (udma_mode & 0x70) >= 0x40)
132 return ATA_CBL_PATA80;
133 else
134 return ATA_CBL_PATA40;
135}
136
Bartlomiej Zolnierkiewiczac95bee2008-04-26 22:25:14 +0200137static const struct ide_port_ops atiixp_port_ops = {
138 .set_pio_mode = atiixp_set_pio_mode,
139 .set_dma_mode = atiixp_set_dma_mode,
140 .cable_detect = atiixp_cable_detect,
141};
Linus Torvalds1da177e2005-04-16 15:20:36 -0700142
Greg Kroah-Hartmanfe31edc2012-12-21 13:21:03 -0800143static const struct ide_port_info atiixp_pci_info[] = {
Bartlomiej Zolnierkiewiczced3ec82008-07-24 22:53:32 +0200144 { /* 0: IXP200/300/400/700 */
145 .name = DRV_NAME,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700146 .enablebits = {{0x48,0x01,0x00}, {0x48,0x08,0x00}},
Bartlomiej Zolnierkiewiczac95bee2008-04-26 22:25:14 +0200147 .port_ops = &atiixp_port_ops,
Bartlomiej Zolnierkiewicz4099d142007-07-20 01:11:59 +0200148 .pio_mask = ATA_PIO4,
Bartlomiej Zolnierkiewicz5f8b6c32007-10-19 00:30:07 +0200149 .mwdma_mask = ATA_MWDMA2,
150 .udma_mask = ATA_UDMA5,
Bartlomiej Zolnierkiewiczced3ec82008-07-24 22:53:32 +0200151 },
152 { /* 1: IXP600 */
153 .name = DRV_NAME,
Conke Hub25168d2007-01-27 13:46:30 +0100154 .enablebits = {{0x48,0x01,0x00}, {0x00,0x00,0x00}},
Bartlomiej Zolnierkiewiczac95bee2008-04-26 22:25:14 +0200155 .port_ops = &atiixp_port_ops,
Bartlomiej Zolnierkiewicz24679222009-03-24 23:22:52 +0100156 .host_flags = IDE_HFLAG_SINGLE,
Bartlomiej Zolnierkiewicz4099d142007-07-20 01:11:59 +0200157 .pio_mask = ATA_PIO4,
Bartlomiej Zolnierkiewicz5f8b6c32007-10-19 00:30:07 +0200158 .mwdma_mask = ATA_MWDMA2,
159 .udma_mask = ATA_UDMA5,
Conke Hub25168d2007-01-27 13:46:30 +0100160 },
Linus Torvalds1da177e2005-04-16 15:20:36 -0700161};
162
163/**
164 * atiixp_init_one - called when a ATIIXP is found
165 * @dev: the atiixp device
166 * @id: the matching pci id
167 *
168 * Called when the PCI registration layer (or the IDE initialization)
169 * finds a device matching our IDE device tables.
170 */
171
Greg Kroah-Hartmanfe31edc2012-12-21 13:21:03 -0800172static int atiixp_init_one(struct pci_dev *dev, const struct pci_device_id *id)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700173{
Bartlomiej Zolnierkiewicz6cdf6eb2008-07-24 22:53:14 +0200174 return ide_pci_init_one(dev, &atiixp_pci_info[id->driver_data], NULL);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700175}
176
Bartlomiej Zolnierkiewicz9cbcc5e2007-10-16 22:29:56 +0200177static const struct pci_device_id atiixp_pci_tbl[] = {
178 { PCI_VDEVICE(ATI, PCI_DEVICE_ID_ATI_IXP200_IDE), 0 },
179 { PCI_VDEVICE(ATI, PCI_DEVICE_ID_ATI_IXP300_IDE), 0 },
180 { PCI_VDEVICE(ATI, PCI_DEVICE_ID_ATI_IXP400_IDE), 0 },
181 { PCI_VDEVICE(ATI, PCI_DEVICE_ID_ATI_IXP600_IDE), 1 },
182 { PCI_VDEVICE(ATI, PCI_DEVICE_ID_ATI_IXP700_IDE), 0 },
Shane Huang5deab532009-10-13 11:14:00 +0800183 { PCI_VDEVICE(AMD, PCI_DEVICE_ID_AMD_HUDSON2_IDE), 0 },
Linus Torvalds1da177e2005-04-16 15:20:36 -0700184 { 0, },
185};
186MODULE_DEVICE_TABLE(pci, atiixp_pci_tbl);
187
Bartlomiej Zolnierkiewicza9ab09e22008-10-13 21:39:41 +0200188static struct pci_driver atiixp_pci_driver = {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700189 .name = "ATIIXP_IDE",
190 .id_table = atiixp_pci_tbl,
191 .probe = atiixp_init_one,
Bartlomiej Zolnierkiewiczf354fbc2008-07-24 22:53:20 +0200192 .remove = ide_pci_remove,
Bartlomiej Zolnierkiewiczfeb22b72008-10-10 22:39:32 +0200193 .suspend = ide_pci_suspend,
194 .resume = ide_pci_resume,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700195};
196
Bartlomiej Zolnierkiewicz82ab1ee2007-01-27 13:46:56 +0100197static int __init atiixp_ide_init(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700198{
Bartlomiej Zolnierkiewicza9ab09e22008-10-13 21:39:41 +0200199 return ide_pci_register_driver(&atiixp_pci_driver);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700200}
201
Bartlomiej Zolnierkiewiczf354fbc2008-07-24 22:53:20 +0200202static void __exit atiixp_ide_exit(void)
203{
Bartlomiej Zolnierkiewicza9ab09e22008-10-13 21:39:41 +0200204 pci_unregister_driver(&atiixp_pci_driver);
Bartlomiej Zolnierkiewiczf354fbc2008-07-24 22:53:20 +0200205}
206
Linus Torvalds1da177e2005-04-16 15:20:36 -0700207module_init(atiixp_ide_init);
Bartlomiej Zolnierkiewiczf354fbc2008-07-24 22:53:20 +0200208module_exit(atiixp_ide_exit);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700209
210MODULE_AUTHOR("HUI YU");
211MODULE_DESCRIPTION("PCI driver module for ATI IXP IDE");
212MODULE_LICENSE("GPL");