blob: d07771e06b2d6fcf0395a24cea82ba5cd666c860 [file] [log] [blame]
Bartlomiej Zolnierkiewicz67881822007-02-07 18:19:26 +01001/*
2 * ITE 8213 IDE driver
3 *
4 * Copyright (C) 2006 Jack Lee
5 * Copyright (C) 2006 Alan Cox
6 * Copyright (C) 2007 Bartlomiej Zolnierkiewicz
7 */
8
Jack Lee9c6712c2007-02-07 18:19:09 +01009#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>
17
18#include <asm/io.h>
19
Jack Lee9c6712c2007-02-07 18:19:09 +010020/**
21 * it8213_dma_2_pio - return the PIO mode matching DMA
22 * @xfer_rate: transfer speed
23 *
Bartlomiej Zolnierkiewicz68aaf812007-08-01 23:46:46 +020024 * Returns the nearest equivalent PIO timing for the DMA
Jack Lee9c6712c2007-02-07 18:19:09 +010025 * mode requested by the controller.
26 */
27
28static u8 it8213_dma_2_pio (u8 xfer_rate) {
29 switch(xfer_rate) {
30 case XFER_UDMA_6:
31 case XFER_UDMA_5:
32 case XFER_UDMA_4:
33 case XFER_UDMA_3:
34 case XFER_UDMA_2:
35 case XFER_UDMA_1:
36 case XFER_UDMA_0:
37 case XFER_MW_DMA_2:
Jack Lee9c6712c2007-02-07 18:19:09 +010038 return 4;
39 case XFER_MW_DMA_1:
Jack Lee9c6712c2007-02-07 18:19:09 +010040 return 3;
41 case XFER_SW_DMA_2:
Jack Lee9c6712c2007-02-07 18:19:09 +010042 return 2;
43 case XFER_MW_DMA_0:
44 case XFER_SW_DMA_1:
45 case XFER_SW_DMA_0:
Jack Lee9c6712c2007-02-07 18:19:09 +010046 default:
47 return 0;
48 }
49}
50
Jack Lee9c6712c2007-02-07 18:19:09 +010051/*
Bartlomiej Zolnierkiewicz68aaf812007-08-01 23:46:46 +020052 * it8213_tune_pio - tune a drive
Jack Lee9c6712c2007-02-07 18:19:09 +010053 * @drive: drive to tune
Bartlomiej Zolnierkiewicz67881822007-02-07 18:19:26 +010054 * @pio: desired PIO mode
Jack Lee9c6712c2007-02-07 18:19:09 +010055 *
Bartlomiej Zolnierkiewicz67881822007-02-07 18:19:26 +010056 * Set the interface PIO mode.
Jack Lee9c6712c2007-02-07 18:19:09 +010057 */
58
Bartlomiej Zolnierkiewicz68aaf812007-08-01 23:46:46 +020059static void it8213_tune_pio(ide_drive_t *drive, const u8 pio)
Jack Lee9c6712c2007-02-07 18:19:09 +010060{
61 ide_hwif_t *hwif = HWIF(drive);
62 struct pci_dev *dev = hwif->pci_dev;
Bartlomiej Zolnierkiewicz67881822007-02-07 18:19:26 +010063 int is_slave = drive->dn & 1;
Jack Lee9c6712c2007-02-07 18:19:09 +010064 int master_port = 0x40;
65 int slave_port = 0x44;
66 unsigned long flags;
67 u16 master_data;
68 u8 slave_data;
Bartlomiej Zolnierkiewicz67881822007-02-07 18:19:26 +010069 static DEFINE_SPINLOCK(tune_lock);
70 int control = 0;
Jack Lee9c6712c2007-02-07 18:19:09 +010071
Bartlomiej Zolnierkiewicz67881822007-02-07 18:19:26 +010072 static const u8 timings[][2]= {
73 { 0, 0 },
74 { 0, 0 },
75 { 1, 0 },
76 { 2, 1 },
77 { 2, 3 }, };
Jack Lee9c6712c2007-02-07 18:19:09 +010078
Jack Lee9c6712c2007-02-07 18:19:09 +010079 spin_lock_irqsave(&tune_lock, flags);
80 pci_read_config_word(dev, master_port, &master_data);
Jack Lee9c6712c2007-02-07 18:19:09 +010081
Bartlomiej Zolnierkiewicz67881822007-02-07 18:19:26 +010082 if (pio > 1)
83 control |= 1; /* Programmable timing on */
84 if (drive->media != ide_disk)
85 control |= 4; /* ATAPI */
86 if (pio > 2)
87 control |= 2; /* IORDY */
88 if (is_slave) {
89 master_data |= 0x4000;
90 master_data &= ~0x0070;
91 if (pio > 1)
92 master_data = master_data | (control << 4);
Jack Lee9c6712c2007-02-07 18:19:09 +010093 pci_read_config_byte(dev, slave_port, &slave_data);
94 slave_data = slave_data & 0xf0;
Bartlomiej Zolnierkiewicz67881822007-02-07 18:19:26 +010095 slave_data = slave_data | (timings[pio][0] << 2) | timings[pio][1];
Jack Lee9c6712c2007-02-07 18:19:09 +010096 } else {
Bartlomiej Zolnierkiewicz67881822007-02-07 18:19:26 +010097 master_data &= ~0x3307;
Jack Lee9c6712c2007-02-07 18:19:09 +010098 if (pio > 1)
Bartlomiej Zolnierkiewicz67881822007-02-07 18:19:26 +010099 master_data = master_data | control;
Jack Lee9c6712c2007-02-07 18:19:09 +0100100 master_data = master_data | (timings[pio][0] << 12) | (timings[pio][1] << 8);
101 }
102 pci_write_config_word(dev, master_port, master_data);
103 if (is_slave)
104 pci_write_config_byte(dev, slave_port, slave_data);
105 spin_unlock_irqrestore(&tune_lock, flags);
106}
107
Bartlomiej Zolnierkiewicz26bcb872007-10-11 23:54:00 +0200108static void it8213_set_pio_mode(ide_drive_t *drive, const u8 pio)
Bartlomiej Zolnierkiewicz68aaf812007-08-01 23:46:46 +0200109{
Bartlomiej Zolnierkiewicz68aaf812007-08-01 23:46:46 +0200110 it8213_tune_pio(drive, pio);
111 ide_config_drive_speed(drive, XFER_PIO_0 + pio);
112}
113
Jack Lee9c6712c2007-02-07 18:19:09 +0100114/**
115 * it8213_tune_chipset - set controller timings
116 * @drive: Drive to set up
Bartlomiej Zolnierkiewiczf212ff22007-10-11 23:53:59 +0200117 * @speed: speed we want to achieve
Jack Lee9c6712c2007-02-07 18:19:09 +0100118 *
Bartlomiej Zolnierkiewiczf212ff22007-10-11 23:53:59 +0200119 * Tune the ITE chipset for the desired mode.
Jack Lee9c6712c2007-02-07 18:19:09 +0100120 */
121
Bartlomiej Zolnierkiewiczf212ff22007-10-11 23:53:59 +0200122static int it8213_tune_chipset(ide_drive_t *drive, const u8 speed)
Jack Lee9c6712c2007-02-07 18:19:09 +0100123{
Jack Lee9c6712c2007-02-07 18:19:09 +0100124 ide_hwif_t *hwif = HWIF(drive);
125 struct pci_dev *dev = hwif->pci_dev;
126 u8 maslave = 0x40;
Jack Lee9c6712c2007-02-07 18:19:09 +0100127 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;
131 int u_speed = 0;
132 u16 reg4042, reg4a;
133 u8 reg48, reg54, reg55;
134
135 pci_read_config_word(dev, maslave, &reg4042);
136 pci_read_config_byte(dev, 0x48, &reg48);
137 pci_read_config_word(dev, 0x4a, &reg4a);
138 pci_read_config_byte(dev, 0x54, &reg54);
139 pci_read_config_byte(dev, 0x55, &reg55);
140
141 switch(speed) {
142 case XFER_UDMA_6:
143 case XFER_UDMA_4:
Bartlomiej Zolnierkiewicz67881822007-02-07 18:19:26 +0100144 case XFER_UDMA_2: u_speed = 2 << (drive->dn * 4); break;
Jack Lee9c6712c2007-02-07 18:19:09 +0100145 case XFER_UDMA_5:
146 case XFER_UDMA_3:
Bartlomiej Zolnierkiewicz67881822007-02-07 18:19:26 +0100147 case XFER_UDMA_1: u_speed = 1 << (drive->dn * 4); break;
148 case XFER_UDMA_0: u_speed = 0 << (drive->dn * 4); break;
Jack Lee9c6712c2007-02-07 18:19:09 +0100149 break;
150 case XFER_MW_DMA_2:
151 case XFER_MW_DMA_1:
Bartlomiej Zolnierkiewicz67881822007-02-07 18:19:26 +0100152 case XFER_SW_DMA_2:
Jack Lee9c6712c2007-02-07 18:19:09 +0100153 break;
154 case XFER_PIO_4:
155 case XFER_PIO_3:
156 case XFER_PIO_2:
157 case XFER_PIO_1:
158 case XFER_PIO_0:
159 break;
160 default:
161 return -1;
162 }
163
Bartlomiej Zolnierkiewicz67881822007-02-07 18:19:26 +0100164 if (speed >= XFER_UDMA_0) {
Jack Lee9c6712c2007-02-07 18:19:09 +0100165 if (!(reg48 & u_flag))
166 pci_write_config_byte(dev, 0x48, reg48 | u_flag);
167 if (speed >= XFER_UDMA_5) {
168 pci_write_config_byte(dev, 0x55, (u8) reg55|w_flag);
169 } else {
170 pci_write_config_byte(dev, 0x55, (u8) reg55 & ~w_flag);
171 }
172
173 if ((reg4a & a_speed) != u_speed)
174 pci_write_config_word(dev, 0x4a, (reg4a & ~a_speed) | u_speed);
Bartlomiej Zolnierkiewicz67881822007-02-07 18:19:26 +0100175 if (speed > XFER_UDMA_2) {
Jack Lee9c6712c2007-02-07 18:19:09 +0100176 if (!(reg54 & v_flag))
177 pci_write_config_byte(dev, 0x54, reg54 | v_flag);
178 } else
179 pci_write_config_byte(dev, 0x54, reg54 & ~v_flag);
Bartlomiej Zolnierkiewicz67881822007-02-07 18:19:26 +0100180 } else {
Jack Lee9c6712c2007-02-07 18:19:09 +0100181 if (reg48 & u_flag)
182 pci_write_config_byte(dev, 0x48, reg48 & ~u_flag);
183 if (reg4a & a_speed)
184 pci_write_config_word(dev, 0x4a, reg4a & ~a_speed);
185 if (reg54 & v_flag)
186 pci_write_config_byte(dev, 0x54, reg54 & ~v_flag);
187 if (reg55 & w_flag)
188 pci_write_config_byte(dev, 0x55, (u8) reg55 & ~w_flag);
Bartlomiej Zolnierkiewicz67881822007-02-07 18:19:26 +0100189 }
Bartlomiej Zolnierkiewicz68aaf812007-08-01 23:46:46 +0200190
191 if (speed > XFER_PIO_4)
192 it8213_tune_pio(drive, it8213_dma_2_pio(speed));
193 else
194 it8213_tune_pio(drive, speed - XFER_PIO_0);
195
Jack Lee9c6712c2007-02-07 18:19:09 +0100196 return ide_config_drive_speed(drive, speed);
Bartlomiej Zolnierkiewicz67881822007-02-07 18:19:26 +0100197}
Jack Lee9c6712c2007-02-07 18:19:09 +0100198
Jack Lee9c6712c2007-02-07 18:19:09 +0100199/**
Jack Lee9c6712c2007-02-07 18:19:09 +0100200 * it8213_configure_drive_for_dma - set up for DMA transfers
201 * @drive: drive we are going to set up
202 *
203 * Set up the drive for DMA, tune the controller and drive as
204 * required. If the drive isn't suitable for DMA or we hit
205 * other problems then we will drop down to PIO and set up
206 * PIO appropriately
207 */
208
209static int it8213_config_drive_for_dma (ide_drive_t *drive)
210{
Bartlomiej Zolnierkiewicz29e744d2007-05-10 00:01:09 +0200211 if (ide_tune_dma(drive))
Bartlomiej Zolnierkiewicz3608b5d2007-02-17 02:40:26 +0100212 return 0;
Bartlomiej Zolnierkiewicz67881822007-02-07 18:19:26 +0100213
Bartlomiej Zolnierkiewicz26bcb872007-10-11 23:54:00 +0200214 ide_set_max_pio(drive);
Bartlomiej Zolnierkiewicz67881822007-02-07 18:19:26 +0100215
Bartlomiej Zolnierkiewicz3608b5d2007-02-17 02:40:26 +0100216 return -1;
Jack Lee9c6712c2007-02-07 18:19:09 +0100217}
218
Jack Lee9c6712c2007-02-07 18:19:09 +0100219/**
220 * init_hwif_it8213 - set up hwif structs
221 * @hwif: interface to set up
222 *
223 * We do the basic set up of the interface structure. The IT8212
224 * requires several custom handlers so we override the default
225 * ide DMA handlers appropriately
226 */
227
228static void __devinit init_hwif_it8213(ide_hwif_t *hwif)
229{
Bartlomiej Zolnierkiewicz49521f92007-07-09 23:17:58 +0200230 u8 reg42h = 0;
Jack Lee9c6712c2007-02-07 18:19:09 +0100231
232 hwif->speedproc = &it8213_tune_chipset;
Bartlomiej Zolnierkiewicz26bcb872007-10-11 23:54:00 +0200233 hwif->set_pio_mode = &it8213_set_pio_mode;
Jack Lee9c6712c2007-02-07 18:19:09 +0100234
235 hwif->autodma = 0;
236
237 hwif->drives[0].autotune = 1;
238 hwif->drives[1].autotune = 1;
239
240 if (!hwif->dma_base)
Bartlomiej Zolnierkiewicz67881822007-02-07 18:19:26 +0100241 return;
242
Jack Lee9c6712c2007-02-07 18:19:09 +0100243 hwif->atapi_dma = 1;
244 hwif->ultra_mask = 0x7f;
Bartlomiej Zolnierkiewicz67881822007-02-07 18:19:26 +0100245 hwif->mwdma_mask = 0x06;
246 hwif->swdma_mask = 0x04;
Jack Lee9c6712c2007-02-07 18:19:09 +0100247
248 pci_read_config_byte(hwif->pci_dev, 0x42, &reg42h);
Jack Lee9c6712c2007-02-07 18:19:09 +0100249
250 hwif->ide_dma_check = &it8213_config_drive_for_dma;
Bartlomiej Zolnierkiewicz49521f92007-07-09 23:17:58 +0200251
252 if (hwif->cbl != ATA_CBL_PATA40_SHORT)
253 hwif->cbl = (reg42h & 0x02) ? ATA_CBL_PATA40 : ATA_CBL_PATA80;
Jack Lee9c6712c2007-02-07 18:19:09 +0100254
255 /*
256 * The BIOS often doesn't set up DMA on this controller
257 * so we always do it.
258 */
259 if (!noautodma)
260 hwif->autodma = 1;
261
262 hwif->drives[0].autodma = hwif->autodma;
263 hwif->drives[1].autodma = hwif->autodma;
Jack Lee9c6712c2007-02-07 18:19:09 +0100264}
265
266
267#define DECLARE_ITE_DEV(name_str) \
268 { \
269 .name = name_str, \
Jack Lee9c6712c2007-02-07 18:19:09 +0100270 .init_hwif = init_hwif_it8213, \
Bartlomiej Zolnierkiewicz67881822007-02-07 18:19:26 +0100271 .autodma = AUTODMA, \
Jack Lee9c6712c2007-02-07 18:19:09 +0100272 .enablebits = {{0x41,0x80,0x80}}, \
273 .bootable = ON_BOARD, \
Bartlomiej Zolnierkiewicza5d8c5c2007-07-20 01:11:55 +0200274 .host_flags = IDE_HFLAG_SINGLE, \
Bartlomiej Zolnierkiewicz4099d142007-07-20 01:11:59 +0200275 .pio_mask = ATA_PIO4, \
Jack Lee9c6712c2007-02-07 18:19:09 +0100276 }
277
278static ide_pci_device_t it8213_chipsets[] __devinitdata = {
279 /* 0 */ DECLARE_ITE_DEV("IT8213"),
280};
281
282
283/**
284 * it8213_init_one - pci layer discovery entry
285 * @dev: PCI device
286 * @id: ident table entry
287 *
288 * Called by the PCI code when it finds an ITE8213 controller. As
289 * this device follows the standard interfaces we can use the
290 * standard helper functions to do almost all the work for us.
291 */
292
293static int __devinit it8213_init_one(struct pci_dev *dev, const struct pci_device_id *id)
294{
295 ide_setup_pci_device(dev, &it8213_chipsets[id->driver_data]);
296 return 0;
297}
298
299
300static struct pci_device_id it8213_pci_tbl[] = {
Bartlomiej Zolnierkiewicz67881822007-02-07 18:19:26 +0100301 { PCI_VENDOR_ID_ITE, 0x8213, PCI_ANY_ID, PCI_ANY_ID, 0, 0, 0 },
Jack Lee9c6712c2007-02-07 18:19:09 +0100302 { 0, },
303};
304
305MODULE_DEVICE_TABLE(pci, it8213_pci_tbl);
306
307static struct pci_driver driver = {
308 .name = "ITE8213_IDE",
309 .id_table = it8213_pci_tbl,
310 .probe = it8213_init_one,
311};
312
313static int __init it8213_ide_init(void)
314{
Bartlomiej Zolnierkiewicz67881822007-02-07 18:19:26 +0100315 return ide_pci_register_driver(&driver);
316}
Jack Lee9c6712c2007-02-07 18:19:09 +0100317
318module_init(it8213_ide_init);
319
Bartlomiej Zolnierkiewicz67881822007-02-07 18:19:26 +0100320MODULE_AUTHOR("Jack Lee, Alan Cox");
Jack Lee9c6712c2007-02-07 18:19:09 +0100321MODULE_DESCRIPTION("PCI driver module for the ITE 8213");
322MODULE_LICENSE("GPL");