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_dma_2_pio - return the PIO mode matching DMA
22 * @xfer_rate: transfer speed
24 * Returns the nearest equivalent PIO timing for the DMA
25 * mode requested by the controller.
28 static u8
it8213_dma_2_pio (u8 xfer_rate
) {
52 * it8213_tune_pio - tune a drive
53 * @drive: drive to tune
54 * @pio: desired PIO mode
56 * Set the interface PIO mode.
59 static void it8213_tune_pio(ide_drive_t
*drive
, const u8 pio
)
61 ide_hwif_t
*hwif
= HWIF(drive
);
62 struct pci_dev
*dev
= hwif
->pci_dev
;
63 int is_slave
= drive
->dn
& 1;
64 int master_port
= 0x40;
65 int slave_port
= 0x44;
69 static DEFINE_SPINLOCK(tune_lock
);
72 static const u8 timings
[][2]= {
79 spin_lock_irqsave(&tune_lock
, flags
);
80 pci_read_config_word(dev
, master_port
, &master_data
);
83 control
|= 1; /* Programmable timing on */
84 if (drive
->media
!= ide_disk
)
85 control
|= 4; /* ATAPI */
87 control
|= 2; /* IORDY */
89 master_data
|= 0x4000;
90 master_data
&= ~0x0070;
92 master_data
= master_data
| (control
<< 4);
93 pci_read_config_byte(dev
, slave_port
, &slave_data
);
94 slave_data
= slave_data
& 0xf0;
95 slave_data
= slave_data
| (timings
[pio
][0] << 2) | timings
[pio
][1];
97 master_data
&= ~0x3307;
99 master_data
= master_data
| control
;
100 master_data
= master_data
| (timings
[pio
][0] << 12) | (timings
[pio
][1] << 8);
102 pci_write_config_word(dev
, master_port
, master_data
);
104 pci_write_config_byte(dev
, slave_port
, slave_data
);
105 spin_unlock_irqrestore(&tune_lock
, flags
);
108 static void it8213_set_pio_mode(ide_drive_t
*drive
, const u8 pio
)
110 it8213_tune_pio(drive
, pio
);
111 ide_config_drive_speed(drive
, XFER_PIO_0
+ pio
);
115 * it8213_tune_chipset - set controller timings
116 * @drive: Drive to set up
117 * @speed: speed we want to achieve
119 * Tune the ITE chipset for the desired mode.
122 static int it8213_tune_chipset(ide_drive_t
*drive
, const u8 speed
)
124 ide_hwif_t
*hwif
= HWIF(drive
);
125 struct pci_dev
*dev
= hwif
->pci_dev
;
127 int a_speed
= 3 << (drive
->dn
* 4);
128 int u_flag
= 1 << drive
->dn
;
129 int v_flag
= 0x01 << drive
->dn
;
130 int w_flag
= 0x10 << drive
->dn
;
133 u8 reg48
, reg54
, reg55
;
135 if (speed
>= XFER_PIO_0
&& speed
<= XFER_PIO_4
) {
136 it8213_tune_pio(drive
, speed
- XFER_PIO_0
);
137 return ide_config_drive_speed(drive
, speed
);
140 pci_read_config_word(dev
, maslave
, ®4042
);
141 pci_read_config_byte(dev
, 0x48, ®48
);
142 pci_read_config_word(dev
, 0x4a, ®4a
);
143 pci_read_config_byte(dev
, 0x54, ®54
);
144 pci_read_config_byte(dev
, 0x55, ®55
);
149 case XFER_UDMA_2
: u_speed
= 2 << (drive
->dn
* 4); break;
152 case XFER_UDMA_1
: u_speed
= 1 << (drive
->dn
* 4); break;
153 case XFER_UDMA_0
: u_speed
= 0 << (drive
->dn
* 4); break;
163 if (speed
>= XFER_UDMA_0
) {
164 if (!(reg48
& u_flag
))
165 pci_write_config_byte(dev
, 0x48, reg48
| u_flag
);
166 if (speed
>= XFER_UDMA_5
) {
167 pci_write_config_byte(dev
, 0x55, (u8
) reg55
|w_flag
);
169 pci_write_config_byte(dev
, 0x55, (u8
) reg55
& ~w_flag
);
172 if ((reg4a
& a_speed
) != u_speed
)
173 pci_write_config_word(dev
, 0x4a, (reg4a
& ~a_speed
) | u_speed
);
174 if (speed
> XFER_UDMA_2
) {
175 if (!(reg54
& v_flag
))
176 pci_write_config_byte(dev
, 0x54, reg54
| v_flag
);
178 pci_write_config_byte(dev
, 0x54, reg54
& ~v_flag
);
181 pci_write_config_byte(dev
, 0x48, reg48
& ~u_flag
);
183 pci_write_config_word(dev
, 0x4a, reg4a
& ~a_speed
);
185 pci_write_config_byte(dev
, 0x54, reg54
& ~v_flag
);
187 pci_write_config_byte(dev
, 0x55, (u8
) reg55
& ~w_flag
);
190 it8213_tune_pio(drive
, it8213_dma_2_pio(speed
));
192 return ide_config_drive_speed(drive
, speed
);
196 * it8213_configure_drive_for_dma - set up for DMA transfers
197 * @drive: drive we are going to set up
199 * Set up the drive for DMA, tune the controller and drive as
200 * required. If the drive isn't suitable for DMA or we hit
201 * other problems then we will drop down to PIO and set up
205 static int it8213_config_drive_for_dma (ide_drive_t
*drive
)
207 if (ide_tune_dma(drive
))
210 ide_set_max_pio(drive
);
216 * init_hwif_it8213 - set up hwif structs
217 * @hwif: interface to set up
219 * We do the basic set up of the interface structure. The IT8212
220 * requires several custom handlers so we override the default
221 * ide DMA handlers appropriately
224 static void __devinit
init_hwif_it8213(ide_hwif_t
*hwif
)
228 hwif
->speedproc
= &it8213_tune_chipset
;
229 hwif
->set_pio_mode
= &it8213_set_pio_mode
;
233 hwif
->drives
[0].autotune
= 1;
234 hwif
->drives
[1].autotune
= 1;
240 hwif
->ultra_mask
= 0x7f;
241 hwif
->mwdma_mask
= 0x06;
242 hwif
->swdma_mask
= 0x04;
244 pci_read_config_byte(hwif
->pci_dev
, 0x42, ®42h
);
246 hwif
->ide_dma_check
= &it8213_config_drive_for_dma
;
248 if (hwif
->cbl
!= ATA_CBL_PATA40_SHORT
)
249 hwif
->cbl
= (reg42h
& 0x02) ? ATA_CBL_PATA40
: ATA_CBL_PATA80
;
252 * The BIOS often doesn't set up DMA on this controller
253 * so we always do it.
258 hwif
->drives
[0].autodma
= hwif
->autodma
;
259 hwif
->drives
[1].autodma
= hwif
->autodma
;
263 #define DECLARE_ITE_DEV(name_str) \
266 .init_hwif = init_hwif_it8213, \
267 .autodma = AUTODMA, \
268 .enablebits = {{0x41,0x80,0x80}}, \
269 .bootable = ON_BOARD, \
270 .host_flags = IDE_HFLAG_SINGLE, \
271 .pio_mask = ATA_PIO4, \
274 static ide_pci_device_t it8213_chipsets
[] __devinitdata
= {
275 /* 0 */ DECLARE_ITE_DEV("IT8213"),
280 * it8213_init_one - pci layer discovery entry
282 * @id: ident table entry
284 * Called by the PCI code when it finds an ITE8213 controller. As
285 * this device follows the standard interfaces we can use the
286 * standard helper functions to do almost all the work for us.
289 static int __devinit
it8213_init_one(struct pci_dev
*dev
, const struct pci_device_id
*id
)
291 ide_setup_pci_device(dev
, &it8213_chipsets
[id
->driver_data
]);
296 static struct pci_device_id it8213_pci_tbl
[] = {
297 { PCI_VENDOR_ID_ITE
, 0x8213, PCI_ANY_ID
, PCI_ANY_ID
, 0, 0, 0 },
301 MODULE_DEVICE_TABLE(pci
, it8213_pci_tbl
);
303 static struct pci_driver driver
= {
304 .name
= "ITE8213_IDE",
305 .id_table
= it8213_pci_tbl
,
306 .probe
= it8213_init_one
,
309 static int __init
it8213_ide_init(void)
311 return ide_pci_register_driver(&driver
);
314 module_init(it8213_ide_init
);
316 MODULE_AUTHOR("Jack Lee, Alan Cox");
317 MODULE_DESCRIPTION("PCI driver module for the ITE 8213");
318 MODULE_LICENSE("GPL");