4 * Copyright (C) 2006 Jack Lee
5 * Copyright (C) 2006 Alan Cox
6 * Copyright (C) 2007 Bartlomiej Zolnierkiewicz
9 #include <linux/kernel.h>
10 #include <linux/types.h>
11 #include <linux/module.h>
12 #include <linux/pci.h>
13 #include <linux/delay.h>
14 #include <linux/hdreg.h>
15 #include <linux/ide.h>
16 #include <linux/init.h>
21 * it8213_ratemask - Compute available modes
24 * Compute the available speeds for the devices on the interface. This
25 * is all modes to ATA133 clipped by drive cable setup.
28 static u8 it8213_ratemask (ide_drive_t *drive)
31 if (!eighty_ninty_three(drive))
32 mode = min_t(u8, mode, 1);
37 * it8213_dma_2_pio - return the PIO mode matching DMA
38 * @xfer_rate: transfer speed
40 * Returns the nearest equivalent PIO timing for the PIO or DMA
41 * mode requested by the controller.
44 static u8 it8213_dma_2_pio (u8 xfer_rate) {
74 * it8213_tuneproc - tune a drive
75 * @drive: drive to tune
76 * @pio: desired PIO mode
78 * Set the interface PIO mode.
81 static void it8213_tuneproc (ide_drive_t *drive, u8 pio)
83 ide_hwif_t *hwif = HWIF(drive);
84 struct pci_dev *dev = hwif->pci_dev;
85 int is_slave = drive->dn & 1;
86 int master_port = 0x40;
87 int slave_port = 0x44;
91 static DEFINE_SPINLOCK(tune_lock);
94 static const u8 timings[][2]= {
101 pio = ide_get_best_pio_mode(drive, pio, 4, NULL);
103 spin_lock_irqsave(&tune_lock, flags);
104 pci_read_config_word(dev, master_port, &master_data);
107 control |= 1; /* Programmable timing on */
108 if (drive->media != ide_disk)
109 control |= 4; /* ATAPI */
111 control |= 2; /* IORDY */
113 master_data |= 0x4000;
114 master_data &= ~0x0070;
116 master_data = master_data | (control << 4);
117 pci_read_config_byte(dev, slave_port, &slave_data);
118 slave_data = slave_data & 0xf0;
119 slave_data = slave_data | (timings[pio][0] << 2) | timings[pio][1];
121 master_data &= ~0x3307;
123 master_data = master_data | control;
124 master_data = master_data | (timings[pio][0] << 12) | (timings[pio][1] << 8);
126 pci_write_config_word(dev, master_port, master_data);
128 pci_write_config_byte(dev, slave_port, slave_data);
129 spin_unlock_irqrestore(&tune_lock, flags);
133 * it8213_tune_chipset - set controller timings
134 * @drive: Drive to set up
135 * @xferspeed: speed we want to achieve
137 * Tune the ITE chipset for the desired mode. If we can't achieve
138 * the desired mode then tune for a lower one, but ultimately
139 * make the thing work.
142 static int it8213_tune_chipset (ide_drive_t *drive, u8 xferspeed)
145 ide_hwif_t *hwif = HWIF(drive);
146 struct pci_dev *dev = hwif->pci_dev;
148 u8 speed = ide_rate_filter(it8213_ratemask(drive), xferspeed);
149 int a_speed = 3 << (drive->dn * 4);
150 int u_flag = 1 << drive->dn;
151 int v_flag = 0x01 << drive->dn;
152 int w_flag = 0x10 << drive->dn;
155 u8 reg48, reg54, reg55;
157 pci_read_config_word(dev, maslave, ®4042);
158 pci_read_config_byte(dev, 0x48, ®48);
159 pci_read_config_word(dev, 0x4a, ®4a);
160 pci_read_config_byte(dev, 0x54, ®54);
161 pci_read_config_byte(dev, 0x55, ®55);
166 case XFER_UDMA_2: u_speed = 2 << (drive->dn * 4); break;
169 case XFER_UDMA_1: u_speed = 1 << (drive->dn * 4); break;
170 case XFER_UDMA_0: u_speed = 0 << (drive->dn * 4); break;
186 if (speed >= XFER_UDMA_0) {
187 if (!(reg48 & u_flag))
188 pci_write_config_byte(dev, 0x48, reg48 | u_flag);
189 if (speed >= XFER_UDMA_5) {
190 pci_write_config_byte(dev, 0x55, (u8) reg55|w_flag);
192 pci_write_config_byte(dev, 0x55, (u8) reg55 & ~w_flag);
195 if ((reg4a & a_speed) != u_speed)
196 pci_write_config_word(dev, 0x4a, (reg4a & ~a_speed) | u_speed);
197 if (speed > XFER_UDMA_2) {
198 if (!(reg54 & v_flag))
199 pci_write_config_byte(dev, 0x54, reg54 | v_flag);
201 pci_write_config_byte(dev, 0x54, reg54 & ~v_flag);
204 pci_write_config_byte(dev, 0x48, reg48 & ~u_flag);
206 pci_write_config_word(dev, 0x4a, reg4a & ~a_speed);
208 pci_write_config_byte(dev, 0x54, reg54 & ~v_flag);
210 pci_write_config_byte(dev, 0x55, (u8) reg55 & ~w_flag);
212 it8213_tuneproc(drive, it8213_dma_2_pio(speed));
213 return ide_config_drive_speed(drive, speed);
217 * config_chipset_for_dma - configure for DMA
218 * @drive: drive to configure
220 * Called by the IDE layer when it wants the timings set up.
223 static int config_chipset_for_dma (ide_drive_t *drive)
225 u8 speed = ide_dma_speed(drive, it8213_ratemask(drive));
230 it8213_tune_chipset(drive, speed);
232 return ide_dma_enable(drive);
236 * it8213_configure_drive_for_dma - set up for DMA transfers
237 * @drive: drive we are going to set up
239 * Set up the drive for DMA, tune the controller and drive as
240 * required. If the drive isn't suitable for DMA or we hit
241 * other problems then we will drop down to PIO and set up
245 static int it8213_config_drive_for_dma (ide_drive_t *drive)
249 if (ide_use_dma(drive) && config_chipset_for_dma(drive))
252 pio = ide_get_best_pio_mode(drive, 255, 4, NULL);
253 it8213_tune_chipset(drive, XFER_PIO_0 + pio);
259 * init_hwif_it8213 - set up hwif structs
260 * @hwif: interface to set up
262 * We do the basic set up of the interface structure. The IT8212
263 * requires several custom handlers so we override the default
264 * ide DMA handlers appropriately
267 static void __devinit init_hwif_it8213(ide_hwif_t *hwif)
269 u8 reg42h = 0, ata66 = 0;
271 hwif->speedproc = &it8213_tune_chipset;
272 hwif->tuneproc = &it8213_tuneproc;
276 hwif->drives[0].autotune = 1;
277 hwif->drives[1].autotune = 1;
283 hwif->ultra_mask = 0x7f;
284 hwif->mwdma_mask = 0x06;
285 hwif->swdma_mask = 0x04;
287 pci_read_config_byte(hwif->pci_dev, 0x42, ®42h);
288 ata66 = (reg42h & 0x02) ? 0 : 1;
290 hwif->ide_dma_check = &it8213_config_drive_for_dma;
291 if (!(hwif->udma_four))
292 hwif->udma_four = ata66;
295 * The BIOS often doesn't set up DMA on this controller
296 * so we always do it.
301 hwif->drives[0].autodma = hwif->autodma;
302 hwif->drives[1].autodma = hwif->autodma;
306 #define DECLARE_ITE_DEV(name_str) \
309 .init_hwif = init_hwif_it8213, \
311 .autodma = AUTODMA, \
312 .enablebits = {{0x41,0x80,0x80}}, \
313 .bootable = ON_BOARD, \
316 static ide_pci_device_t it8213_chipsets[] __devinitdata = {
317 /* 0 */ DECLARE_ITE_DEV("IT8213"),
322 * it8213_init_one - pci layer discovery entry
324 * @id: ident table entry
326 * Called by the PCI code when it finds an ITE8213 controller. As
327 * this device follows the standard interfaces we can use the
328 * standard helper functions to do almost all the work for us.
331 static int __devinit it8213_init_one(struct pci_dev *dev, const struct pci_device_id *id)
333 ide_setup_pci_device(dev, &it8213_chipsets[id->driver_data]);
338 static struct pci_device_id it8213_pci_tbl[] = {
339 { PCI_VENDOR_ID_ITE, 0x8213, PCI_ANY_ID, PCI_ANY_ID, 0, 0, 0 },
343 MODULE_DEVICE_TABLE(pci, it8213_pci_tbl);
345 static struct pci_driver driver = {
346 .name = "ITE8213_IDE",
347 .id_table = it8213_pci_tbl,
348 .probe = it8213_init_one,
351 static int __init it8213_ide_init(void)
353 return ide_pci_register_driver(&driver);
356 module_init(it8213_ide_init);
358 MODULE_AUTHOR("Jack Lee, Alan Cox");
359 MODULE_DESCRIPTION("PCI driver module for the ITE 8213");
360 MODULE_LICENSE("GPL");