blob: 923cbfe259d31969f603788dd9c80086b9d62940 [file] [log] [blame]
Linus Torvalds1da177e2005-04-16 15:20:36 -07001/*
Linus Torvalds1da177e2005-04-16 15:20:36 -07002 * Copyright (C) 2003 ATI Inc. <hyu@ati.com>
Bartlomiej Zolnierkiewicz485efc62007-07-20 01:11:54 +02003 * Copyright (C) 2004,2007 Bartlomiej Zolnierkiewicz
Linus Torvalds1da177e2005-04-16 15:20:36 -07004 */
5
Linus Torvalds1da177e2005-04-16 15:20:36 -07006#include <linux/types.h>
7#include <linux/module.h>
8#include <linux/kernel.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -07009#include <linux/pci.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070010#include <linux/ide.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070011#include <linux/init.h>
12
Bartlomiej Zolnierkiewiczced3ec82008-07-24 22:53:32 +020013#define DRV_NAME "atiixp"
14
Linus Torvalds1da177e2005-04-16 15:20:36 -070015#define ATIIXP_IDE_PIO_TIMING 0x40
16#define ATIIXP_IDE_MDMA_TIMING 0x44
17#define ATIIXP_IDE_PIO_CONTROL 0x48
18#define ATIIXP_IDE_PIO_MODE 0x4a
19#define ATIIXP_IDE_UDMA_CONTROL 0x54
20#define ATIIXP_IDE_UDMA_MODE 0x56
21
22typedef struct {
23 u8 command_width;
24 u8 recover_width;
25} atiixp_ide_timing;
26
27static atiixp_ide_timing pio_timing[] = {
28 { 0x05, 0x0d },
29 { 0x04, 0x07 },
30 { 0x03, 0x04 },
31 { 0x02, 0x02 },
32 { 0x02, 0x00 },
33};
34
35static atiixp_ide_timing mdma_timing[] = {
36 { 0x07, 0x07 },
37 { 0x02, 0x01 },
38 { 0x02, 0x00 },
39};
40
Alan6c5f8cc2007-01-05 16:36:27 -080041static DEFINE_SPINLOCK(atiixp_lock);
42
Linus Torvalds1da177e2005-04-16 15:20:36 -070043/**
Bartlomiej Zolnierkiewicz88b2b322007-10-13 17:47:51 +020044 * atiixp_set_pio_mode - set host controller for PIO mode
45 * @drive: drive
46 * @pio: PIO mode number
Linus Torvalds1da177e2005-04-16 15:20:36 -070047 *
48 * Set the interface PIO mode.
49 */
50
Bartlomiej Zolnierkiewicz88b2b322007-10-13 17:47:51 +020051static void atiixp_set_pio_mode(ide_drive_t *drive, const u8 pio)
Linus Torvalds1da177e2005-04-16 15:20:36 -070052{
Bartlomiej Zolnierkiewicz36501652008-02-01 23:09:31 +010053 struct pci_dev *dev = to_pci_dev(drive->hwif->dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -070054 unsigned long flags;
Roel Kluinf76bee12009-02-25 20:28:22 +010055 int timing_shift = (drive->dn ^ 1) * 8;
Linus Torvalds1da177e2005-04-16 15:20:36 -070056 u32 pio_timing_data;
57 u16 pio_mode_data;
58
Alan6c5f8cc2007-01-05 16:36:27 -080059 spin_lock_irqsave(&atiixp_lock, flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -070060
61 pci_read_config_word(dev, ATIIXP_IDE_PIO_MODE, &pio_mode_data);
62 pio_mode_data &= ~(0x07 << (drive->dn * 4));
63 pio_mode_data |= (pio << (drive->dn * 4));
64 pci_write_config_word(dev, ATIIXP_IDE_PIO_MODE, pio_mode_data);
65
66 pci_read_config_dword(dev, ATIIXP_IDE_PIO_TIMING, &pio_timing_data);
67 pio_timing_data &= ~(0xff << timing_shift);
68 pio_timing_data |= (pio_timing[pio].recover_width << timing_shift) |
69 (pio_timing[pio].command_width << (timing_shift + 4));
70 pci_write_config_dword(dev, ATIIXP_IDE_PIO_TIMING, pio_timing_data);
71
Alan6c5f8cc2007-01-05 16:36:27 -080072 spin_unlock_irqrestore(&atiixp_lock, flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -070073}
74
75/**
Bartlomiej Zolnierkiewicz88b2b322007-10-13 17:47:51 +020076 * atiixp_set_dma_mode - set host controller for DMA mode
77 * @drive: drive
78 * @speed: DMA mode
Linus Torvalds1da177e2005-04-16 15:20:36 -070079 *
Bartlomiej Zolnierkiewicz88b2b322007-10-13 17:47:51 +020080 * Set a ATIIXP host controller to the desired DMA mode. This involves
81 * programming the right timing data into the PCI configuration space.
Linus Torvalds1da177e2005-04-16 15:20:36 -070082 */
83
Bartlomiej Zolnierkiewicz88b2b322007-10-13 17:47:51 +020084static void atiixp_set_dma_mode(ide_drive_t *drive, const u8 speed)
Linus Torvalds1da177e2005-04-16 15:20:36 -070085{
Bartlomiej Zolnierkiewicz36501652008-02-01 23:09:31 +010086 struct pci_dev *dev = to_pci_dev(drive->hwif->dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -070087 unsigned long flags;
Roel Kluinf76bee12009-02-25 20:28:22 +010088 int timing_shift = (drive->dn ^ 1) * 8;
Linus Torvalds1da177e2005-04-16 15:20:36 -070089 u32 tmp32;
90 u16 tmp16;
Bartlomiej Zolnierkiewicz8ae60e32008-01-26 20:13:02 +010091 u16 udma_ctl = 0;
Bartlomiej Zolnierkiewicz94c7fa02007-10-16 22:29:53 +020092
Alan6c5f8cc2007-01-05 16:36:27 -080093 spin_lock_irqsave(&atiixp_lock, flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -070094
Bartlomiej Zolnierkiewicz8ae60e32008-01-26 20:13:02 +010095 pci_read_config_word(dev, ATIIXP_IDE_UDMA_CONTROL, &udma_ctl);
96
Linus Torvalds1da177e2005-04-16 15:20:36 -070097 if (speed >= XFER_UDMA_0) {
98 pci_read_config_word(dev, ATIIXP_IDE_UDMA_MODE, &tmp16);
99 tmp16 &= ~(0x07 << (drive->dn * 4));
100 tmp16 |= ((speed & 0x07) << (drive->dn * 4));
101 pci_write_config_word(dev, ATIIXP_IDE_UDMA_MODE, tmp16);
Bartlomiej Zolnierkiewicz8ae60e32008-01-26 20:13:02 +0100102
103 udma_ctl |= (1 << drive->dn);
104 } else if (speed >= XFER_MW_DMA_0) {
105 u8 i = speed & 0x03;
106
107 pci_read_config_dword(dev, ATIIXP_IDE_MDMA_TIMING, &tmp32);
108 tmp32 &= ~(0xff << timing_shift);
109 tmp32 |= (mdma_timing[i].recover_width << timing_shift) |
110 (mdma_timing[i].command_width << (timing_shift + 4));
111 pci_write_config_dword(dev, ATIIXP_IDE_MDMA_TIMING, tmp32);
112
113 udma_ctl &= ~(1 << drive->dn);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700114 }
115
Bartlomiej Zolnierkiewicz8ae60e32008-01-26 20:13:02 +0100116 pci_write_config_word(dev, ATIIXP_IDE_UDMA_CONTROL, udma_ctl);
117
Alan6c5f8cc2007-01-05 16:36:27 -0800118 spin_unlock_irqrestore(&atiixp_lock, flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700119}
120
Bartlomiej Zolnierkiewiczf454cbe2008-08-05 18:17:04 +0200121static u8 atiixp_cable_detect(ide_hwif_t *hwif)
Bartlomiej Zolnierkiewiczb4d1c732008-02-02 19:56:29 +0100122{
123 struct pci_dev *pdev = to_pci_dev(hwif->dev);
124 u8 udma_mode = 0, ch = hwif->channel;
125
126 pci_read_config_byte(pdev, ATIIXP_IDE_UDMA_MODE + ch, &udma_mode);
127
128 if ((udma_mode & 0x07) >= 0x04 || (udma_mode & 0x70) >= 0x40)
129 return ATA_CBL_PATA80;
130 else
131 return ATA_CBL_PATA40;
132}
133
Bartlomiej Zolnierkiewiczac95bee2008-04-26 22:25:14 +0200134static const struct ide_port_ops atiixp_port_ops = {
135 .set_pio_mode = atiixp_set_pio_mode,
136 .set_dma_mode = atiixp_set_dma_mode,
137 .cable_detect = atiixp_cable_detect,
138};
Linus Torvalds1da177e2005-04-16 15:20:36 -0700139
Bartlomiej Zolnierkiewicz85620432007-10-20 00:32:34 +0200140static const struct ide_port_info atiixp_pci_info[] __devinitdata = {
Bartlomiej Zolnierkiewiczced3ec82008-07-24 22:53:32 +0200141 { /* 0: IXP200/300/400/700 */
142 .name = DRV_NAME,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700143 .enablebits = {{0x48,0x01,0x00}, {0x48,0x08,0x00}},
Bartlomiej Zolnierkiewiczac95bee2008-04-26 22:25:14 +0200144 .port_ops = &atiixp_port_ops,
Bartlomiej Zolnierkiewicz4099d142007-07-20 01:11:59 +0200145 .pio_mask = ATA_PIO4,
Bartlomiej Zolnierkiewicz5f8b6c32007-10-19 00:30:07 +0200146 .mwdma_mask = ATA_MWDMA2,
147 .udma_mask = ATA_UDMA5,
Bartlomiej Zolnierkiewiczced3ec82008-07-24 22:53:32 +0200148 },
149 { /* 1: IXP600 */
150 .name = DRV_NAME,
Conke Hub25168d2007-01-27 13:46:30 +0100151 .enablebits = {{0x48,0x01,0x00}, {0x00,0x00,0x00}},
Bartlomiej Zolnierkiewiczac95bee2008-04-26 22:25:14 +0200152 .port_ops = &atiixp_port_ops,
Bartlomiej Zolnierkiewicz24679222009-03-24 23:22:52 +0100153 .host_flags = IDE_HFLAG_SINGLE,
Bartlomiej Zolnierkiewicz4099d142007-07-20 01:11:59 +0200154 .pio_mask = ATA_PIO4,
Bartlomiej Zolnierkiewicz5f8b6c32007-10-19 00:30:07 +0200155 .mwdma_mask = ATA_MWDMA2,
156 .udma_mask = ATA_UDMA5,
Conke Hub25168d2007-01-27 13:46:30 +0100157 },
Linus Torvalds1da177e2005-04-16 15:20:36 -0700158};
159
160/**
161 * atiixp_init_one - called when a ATIIXP is found
162 * @dev: the atiixp device
163 * @id: the matching pci id
164 *
165 * Called when the PCI registration layer (or the IDE initialization)
166 * finds a device matching our IDE device tables.
167 */
168
169static int __devinit atiixp_init_one(struct pci_dev *dev, const struct pci_device_id *id)
170{
Bartlomiej Zolnierkiewicz6cdf6eb2008-07-24 22:53:14 +0200171 return ide_pci_init_one(dev, &atiixp_pci_info[id->driver_data], NULL);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700172}
173
Bartlomiej Zolnierkiewicz9cbcc5e2007-10-16 22:29:56 +0200174static const struct pci_device_id atiixp_pci_tbl[] = {
175 { PCI_VDEVICE(ATI, PCI_DEVICE_ID_ATI_IXP200_IDE), 0 },
176 { PCI_VDEVICE(ATI, PCI_DEVICE_ID_ATI_IXP300_IDE), 0 },
177 { PCI_VDEVICE(ATI, PCI_DEVICE_ID_ATI_IXP400_IDE), 0 },
178 { PCI_VDEVICE(ATI, PCI_DEVICE_ID_ATI_IXP600_IDE), 1 },
179 { PCI_VDEVICE(ATI, PCI_DEVICE_ID_ATI_IXP700_IDE), 0 },
Linus Torvalds1da177e2005-04-16 15:20:36 -0700180 { 0, },
181};
182MODULE_DEVICE_TABLE(pci, atiixp_pci_tbl);
183
Bartlomiej Zolnierkiewicza9ab09e2008-10-13 21:39:41 +0200184static struct pci_driver atiixp_pci_driver = {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700185 .name = "ATIIXP_IDE",
186 .id_table = atiixp_pci_tbl,
187 .probe = atiixp_init_one,
Bartlomiej Zolnierkiewiczf354fbc2008-07-24 22:53:20 +0200188 .remove = ide_pci_remove,
Bartlomiej Zolnierkiewiczfeb22b72008-10-10 22:39:32 +0200189 .suspend = ide_pci_suspend,
190 .resume = ide_pci_resume,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700191};
192
Bartlomiej Zolnierkiewicz82ab1ee2007-01-27 13:46:56 +0100193static int __init atiixp_ide_init(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700194{
Bartlomiej Zolnierkiewicza9ab09e2008-10-13 21:39:41 +0200195 return ide_pci_register_driver(&atiixp_pci_driver);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700196}
197
Bartlomiej Zolnierkiewiczf354fbc2008-07-24 22:53:20 +0200198static void __exit atiixp_ide_exit(void)
199{
Bartlomiej Zolnierkiewicza9ab09e2008-10-13 21:39:41 +0200200 pci_unregister_driver(&atiixp_pci_driver);
Bartlomiej Zolnierkiewiczf354fbc2008-07-24 22:53:20 +0200201}
202
Linus Torvalds1da177e2005-04-16 15:20:36 -0700203module_init(atiixp_ide_init);
Bartlomiej Zolnierkiewiczf354fbc2008-07-24 22:53:20 +0200204module_exit(atiixp_ide_exit);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700205
206MODULE_AUTHOR("HUI YU");
207MODULE_DESCRIPTION("PCI driver module for ATI IXP IDE");
208MODULE_LICENSE("GPL");