Bartlomiej Zolnierkiewicz | 6788182 | 2007-02-07 18:19:26 +0100 | [diff] [blame] | 1 | /* |
| 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 Lee | 9c6712c | 2007-02-07 18:19:09 +0100 | [diff] [blame] | 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> |
| 17 | |
| 18 | #include <asm/io.h> |
| 19 | |
Jack Lee | 9c6712c | 2007-02-07 18:19:09 +0100 | [diff] [blame] | 20 | /** |
| 21 | * it8213_dma_2_pio - return the PIO mode matching DMA |
| 22 | * @xfer_rate: transfer speed |
| 23 | * |
Bartlomiej Zolnierkiewicz | 68aaf81 | 2007-08-01 23:46:46 +0200 | [diff] [blame^] | 24 | * Returns the nearest equivalent PIO timing for the DMA |
Jack Lee | 9c6712c | 2007-02-07 18:19:09 +0100 | [diff] [blame] | 25 | * mode requested by the controller. |
| 26 | */ |
| 27 | |
| 28 | static 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 Lee | 9c6712c | 2007-02-07 18:19:09 +0100 | [diff] [blame] | 38 | return 4; |
| 39 | case XFER_MW_DMA_1: |
Jack Lee | 9c6712c | 2007-02-07 18:19:09 +0100 | [diff] [blame] | 40 | return 3; |
| 41 | case XFER_SW_DMA_2: |
Jack Lee | 9c6712c | 2007-02-07 18:19:09 +0100 | [diff] [blame] | 42 | return 2; |
| 43 | case XFER_MW_DMA_0: |
| 44 | case XFER_SW_DMA_1: |
| 45 | case XFER_SW_DMA_0: |
Jack Lee | 9c6712c | 2007-02-07 18:19:09 +0100 | [diff] [blame] | 46 | default: |
| 47 | return 0; |
| 48 | } |
| 49 | } |
| 50 | |
Jack Lee | 9c6712c | 2007-02-07 18:19:09 +0100 | [diff] [blame] | 51 | /* |
Bartlomiej Zolnierkiewicz | 68aaf81 | 2007-08-01 23:46:46 +0200 | [diff] [blame^] | 52 | * it8213_tune_pio - tune a drive |
Jack Lee | 9c6712c | 2007-02-07 18:19:09 +0100 | [diff] [blame] | 53 | * @drive: drive to tune |
Bartlomiej Zolnierkiewicz | 6788182 | 2007-02-07 18:19:26 +0100 | [diff] [blame] | 54 | * @pio: desired PIO mode |
Jack Lee | 9c6712c | 2007-02-07 18:19:09 +0100 | [diff] [blame] | 55 | * |
Bartlomiej Zolnierkiewicz | 6788182 | 2007-02-07 18:19:26 +0100 | [diff] [blame] | 56 | * Set the interface PIO mode. |
Jack Lee | 9c6712c | 2007-02-07 18:19:09 +0100 | [diff] [blame] | 57 | */ |
| 58 | |
Bartlomiej Zolnierkiewicz | 68aaf81 | 2007-08-01 23:46:46 +0200 | [diff] [blame^] | 59 | static void it8213_tune_pio(ide_drive_t *drive, const u8 pio) |
Jack Lee | 9c6712c | 2007-02-07 18:19:09 +0100 | [diff] [blame] | 60 | { |
| 61 | ide_hwif_t *hwif = HWIF(drive); |
| 62 | struct pci_dev *dev = hwif->pci_dev; |
Bartlomiej Zolnierkiewicz | 6788182 | 2007-02-07 18:19:26 +0100 | [diff] [blame] | 63 | int is_slave = drive->dn & 1; |
Jack Lee | 9c6712c | 2007-02-07 18:19:09 +0100 | [diff] [blame] | 64 | int master_port = 0x40; |
| 65 | int slave_port = 0x44; |
| 66 | unsigned long flags; |
| 67 | u16 master_data; |
| 68 | u8 slave_data; |
Bartlomiej Zolnierkiewicz | 6788182 | 2007-02-07 18:19:26 +0100 | [diff] [blame] | 69 | static DEFINE_SPINLOCK(tune_lock); |
| 70 | int control = 0; |
Jack Lee | 9c6712c | 2007-02-07 18:19:09 +0100 | [diff] [blame] | 71 | |
Bartlomiej Zolnierkiewicz | 6788182 | 2007-02-07 18:19:26 +0100 | [diff] [blame] | 72 | static const u8 timings[][2]= { |
| 73 | { 0, 0 }, |
| 74 | { 0, 0 }, |
| 75 | { 1, 0 }, |
| 76 | { 2, 1 }, |
| 77 | { 2, 3 }, }; |
Jack Lee | 9c6712c | 2007-02-07 18:19:09 +0100 | [diff] [blame] | 78 | |
Jack Lee | 9c6712c | 2007-02-07 18:19:09 +0100 | [diff] [blame] | 79 | spin_lock_irqsave(&tune_lock, flags); |
| 80 | pci_read_config_word(dev, master_port, &master_data); |
Jack Lee | 9c6712c | 2007-02-07 18:19:09 +0100 | [diff] [blame] | 81 | |
Bartlomiej Zolnierkiewicz | 6788182 | 2007-02-07 18:19:26 +0100 | [diff] [blame] | 82 | 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 Lee | 9c6712c | 2007-02-07 18:19:09 +0100 | [diff] [blame] | 93 | pci_read_config_byte(dev, slave_port, &slave_data); |
| 94 | slave_data = slave_data & 0xf0; |
Bartlomiej Zolnierkiewicz | 6788182 | 2007-02-07 18:19:26 +0100 | [diff] [blame] | 95 | slave_data = slave_data | (timings[pio][0] << 2) | timings[pio][1]; |
Jack Lee | 9c6712c | 2007-02-07 18:19:09 +0100 | [diff] [blame] | 96 | } else { |
Bartlomiej Zolnierkiewicz | 6788182 | 2007-02-07 18:19:26 +0100 | [diff] [blame] | 97 | master_data &= ~0x3307; |
Jack Lee | 9c6712c | 2007-02-07 18:19:09 +0100 | [diff] [blame] | 98 | if (pio > 1) |
Bartlomiej Zolnierkiewicz | 6788182 | 2007-02-07 18:19:26 +0100 | [diff] [blame] | 99 | master_data = master_data | control; |
Jack Lee | 9c6712c | 2007-02-07 18:19:09 +0100 | [diff] [blame] | 100 | 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 Zolnierkiewicz | 68aaf81 | 2007-08-01 23:46:46 +0200 | [diff] [blame^] | 108 | static void it8213_tuneproc(ide_drive_t *drive, u8 pio) |
| 109 | { |
| 110 | pio = ide_get_best_pio_mode(drive, pio, 4); |
| 111 | it8213_tune_pio(drive, pio); |
| 112 | ide_config_drive_speed(drive, XFER_PIO_0 + pio); |
| 113 | } |
| 114 | |
Jack Lee | 9c6712c | 2007-02-07 18:19:09 +0100 | [diff] [blame] | 115 | /** |
| 116 | * it8213_tune_chipset - set controller timings |
| 117 | * @drive: Drive to set up |
| 118 | * @xferspeed: speed we want to achieve |
| 119 | * |
| 120 | * Tune the ITE chipset for the desired mode. If we can't achieve |
| 121 | * the desired mode then tune for a lower one, but ultimately |
| 122 | * make the thing work. |
| 123 | */ |
| 124 | |
Bartlomiej Zolnierkiewicz | 6788182 | 2007-02-07 18:19:26 +0100 | [diff] [blame] | 125 | static int it8213_tune_chipset (ide_drive_t *drive, u8 xferspeed) |
Jack Lee | 9c6712c | 2007-02-07 18:19:09 +0100 | [diff] [blame] | 126 | { |
| 127 | |
| 128 | ide_hwif_t *hwif = HWIF(drive); |
| 129 | struct pci_dev *dev = hwif->pci_dev; |
| 130 | u8 maslave = 0x40; |
Bartlomiej Zolnierkiewicz | 2d5eaa6 | 2007-05-10 00:01:08 +0200 | [diff] [blame] | 131 | u8 speed = ide_rate_filter(drive, xferspeed); |
Jack Lee | 9c6712c | 2007-02-07 18:19:09 +0100 | [diff] [blame] | 132 | int a_speed = 3 << (drive->dn * 4); |
| 133 | int u_flag = 1 << drive->dn; |
| 134 | int v_flag = 0x01 << drive->dn; |
| 135 | int w_flag = 0x10 << drive->dn; |
| 136 | int u_speed = 0; |
| 137 | u16 reg4042, reg4a; |
| 138 | u8 reg48, reg54, reg55; |
| 139 | |
| 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); |
| 145 | |
| 146 | switch(speed) { |
| 147 | case XFER_UDMA_6: |
| 148 | case XFER_UDMA_4: |
Bartlomiej Zolnierkiewicz | 6788182 | 2007-02-07 18:19:26 +0100 | [diff] [blame] | 149 | case XFER_UDMA_2: u_speed = 2 << (drive->dn * 4); break; |
Jack Lee | 9c6712c | 2007-02-07 18:19:09 +0100 | [diff] [blame] | 150 | case XFER_UDMA_5: |
| 151 | case XFER_UDMA_3: |
Bartlomiej Zolnierkiewicz | 6788182 | 2007-02-07 18:19:26 +0100 | [diff] [blame] | 152 | case XFER_UDMA_1: u_speed = 1 << (drive->dn * 4); break; |
| 153 | case XFER_UDMA_0: u_speed = 0 << (drive->dn * 4); break; |
Jack Lee | 9c6712c | 2007-02-07 18:19:09 +0100 | [diff] [blame] | 154 | break; |
| 155 | case XFER_MW_DMA_2: |
| 156 | case XFER_MW_DMA_1: |
Bartlomiej Zolnierkiewicz | 6788182 | 2007-02-07 18:19:26 +0100 | [diff] [blame] | 157 | case XFER_SW_DMA_2: |
Jack Lee | 9c6712c | 2007-02-07 18:19:09 +0100 | [diff] [blame] | 158 | break; |
| 159 | case XFER_PIO_4: |
| 160 | case XFER_PIO_3: |
| 161 | case XFER_PIO_2: |
| 162 | case XFER_PIO_1: |
| 163 | case XFER_PIO_0: |
| 164 | break; |
| 165 | default: |
| 166 | return -1; |
| 167 | } |
| 168 | |
Bartlomiej Zolnierkiewicz | 6788182 | 2007-02-07 18:19:26 +0100 | [diff] [blame] | 169 | if (speed >= XFER_UDMA_0) { |
Jack Lee | 9c6712c | 2007-02-07 18:19:09 +0100 | [diff] [blame] | 170 | if (!(reg48 & u_flag)) |
| 171 | pci_write_config_byte(dev, 0x48, reg48 | u_flag); |
| 172 | if (speed >= XFER_UDMA_5) { |
| 173 | pci_write_config_byte(dev, 0x55, (u8) reg55|w_flag); |
| 174 | } else { |
| 175 | pci_write_config_byte(dev, 0x55, (u8) reg55 & ~w_flag); |
| 176 | } |
| 177 | |
| 178 | if ((reg4a & a_speed) != u_speed) |
| 179 | pci_write_config_word(dev, 0x4a, (reg4a & ~a_speed) | u_speed); |
Bartlomiej Zolnierkiewicz | 6788182 | 2007-02-07 18:19:26 +0100 | [diff] [blame] | 180 | if (speed > XFER_UDMA_2) { |
Jack Lee | 9c6712c | 2007-02-07 18:19:09 +0100 | [diff] [blame] | 181 | if (!(reg54 & v_flag)) |
| 182 | pci_write_config_byte(dev, 0x54, reg54 | v_flag); |
| 183 | } else |
| 184 | pci_write_config_byte(dev, 0x54, reg54 & ~v_flag); |
Bartlomiej Zolnierkiewicz | 6788182 | 2007-02-07 18:19:26 +0100 | [diff] [blame] | 185 | } else { |
Jack Lee | 9c6712c | 2007-02-07 18:19:09 +0100 | [diff] [blame] | 186 | if (reg48 & u_flag) |
| 187 | pci_write_config_byte(dev, 0x48, reg48 & ~u_flag); |
| 188 | if (reg4a & a_speed) |
| 189 | pci_write_config_word(dev, 0x4a, reg4a & ~a_speed); |
| 190 | if (reg54 & v_flag) |
| 191 | pci_write_config_byte(dev, 0x54, reg54 & ~v_flag); |
| 192 | if (reg55 & w_flag) |
| 193 | pci_write_config_byte(dev, 0x55, (u8) reg55 & ~w_flag); |
Bartlomiej Zolnierkiewicz | 6788182 | 2007-02-07 18:19:26 +0100 | [diff] [blame] | 194 | } |
Bartlomiej Zolnierkiewicz | 68aaf81 | 2007-08-01 23:46:46 +0200 | [diff] [blame^] | 195 | |
| 196 | if (speed > XFER_PIO_4) |
| 197 | it8213_tune_pio(drive, it8213_dma_2_pio(speed)); |
| 198 | else |
| 199 | it8213_tune_pio(drive, speed - XFER_PIO_0); |
| 200 | |
Jack Lee | 9c6712c | 2007-02-07 18:19:09 +0100 | [diff] [blame] | 201 | return ide_config_drive_speed(drive, speed); |
Bartlomiej Zolnierkiewicz | 6788182 | 2007-02-07 18:19:26 +0100 | [diff] [blame] | 202 | } |
Jack Lee | 9c6712c | 2007-02-07 18:19:09 +0100 | [diff] [blame] | 203 | |
Jack Lee | 9c6712c | 2007-02-07 18:19:09 +0100 | [diff] [blame] | 204 | /** |
Jack Lee | 9c6712c | 2007-02-07 18:19:09 +0100 | [diff] [blame] | 205 | * it8213_configure_drive_for_dma - set up for DMA transfers |
| 206 | * @drive: drive we are going to set up |
| 207 | * |
| 208 | * Set up the drive for DMA, tune the controller and drive as |
| 209 | * required. If the drive isn't suitable for DMA or we hit |
| 210 | * other problems then we will drop down to PIO and set up |
| 211 | * PIO appropriately |
| 212 | */ |
| 213 | |
| 214 | static int it8213_config_drive_for_dma (ide_drive_t *drive) |
| 215 | { |
Bartlomiej Zolnierkiewicz | 29e744d | 2007-05-10 00:01:09 +0200 | [diff] [blame] | 216 | if (ide_tune_dma(drive)) |
Bartlomiej Zolnierkiewicz | 3608b5d | 2007-02-17 02:40:26 +0100 | [diff] [blame] | 217 | return 0; |
Bartlomiej Zolnierkiewicz | 6788182 | 2007-02-07 18:19:26 +0100 | [diff] [blame] | 218 | |
Bartlomiej Zolnierkiewicz | 68aaf81 | 2007-08-01 23:46:46 +0200 | [diff] [blame^] | 219 | it8213_tuneproc(drive, 255); |
Bartlomiej Zolnierkiewicz | 6788182 | 2007-02-07 18:19:26 +0100 | [diff] [blame] | 220 | |
Bartlomiej Zolnierkiewicz | 3608b5d | 2007-02-17 02:40:26 +0100 | [diff] [blame] | 221 | return -1; |
Jack Lee | 9c6712c | 2007-02-07 18:19:09 +0100 | [diff] [blame] | 222 | } |
| 223 | |
Jack Lee | 9c6712c | 2007-02-07 18:19:09 +0100 | [diff] [blame] | 224 | /** |
| 225 | * init_hwif_it8213 - set up hwif structs |
| 226 | * @hwif: interface to set up |
| 227 | * |
| 228 | * We do the basic set up of the interface structure. The IT8212 |
| 229 | * requires several custom handlers so we override the default |
| 230 | * ide DMA handlers appropriately |
| 231 | */ |
| 232 | |
| 233 | static void __devinit init_hwif_it8213(ide_hwif_t *hwif) |
| 234 | { |
Bartlomiej Zolnierkiewicz | 49521f9 | 2007-07-09 23:17:58 +0200 | [diff] [blame] | 235 | u8 reg42h = 0; |
Jack Lee | 9c6712c | 2007-02-07 18:19:09 +0100 | [diff] [blame] | 236 | |
| 237 | hwif->speedproc = &it8213_tune_chipset; |
| 238 | hwif->tuneproc = &it8213_tuneproc; |
| 239 | |
| 240 | hwif->autodma = 0; |
| 241 | |
| 242 | hwif->drives[0].autotune = 1; |
| 243 | hwif->drives[1].autotune = 1; |
| 244 | |
| 245 | if (!hwif->dma_base) |
Bartlomiej Zolnierkiewicz | 6788182 | 2007-02-07 18:19:26 +0100 | [diff] [blame] | 246 | return; |
| 247 | |
Jack Lee | 9c6712c | 2007-02-07 18:19:09 +0100 | [diff] [blame] | 248 | hwif->atapi_dma = 1; |
| 249 | hwif->ultra_mask = 0x7f; |
Bartlomiej Zolnierkiewicz | 6788182 | 2007-02-07 18:19:26 +0100 | [diff] [blame] | 250 | hwif->mwdma_mask = 0x06; |
| 251 | hwif->swdma_mask = 0x04; |
Jack Lee | 9c6712c | 2007-02-07 18:19:09 +0100 | [diff] [blame] | 252 | |
| 253 | pci_read_config_byte(hwif->pci_dev, 0x42, ®42h); |
Jack Lee | 9c6712c | 2007-02-07 18:19:09 +0100 | [diff] [blame] | 254 | |
| 255 | hwif->ide_dma_check = &it8213_config_drive_for_dma; |
Bartlomiej Zolnierkiewicz | 49521f9 | 2007-07-09 23:17:58 +0200 | [diff] [blame] | 256 | |
| 257 | if (hwif->cbl != ATA_CBL_PATA40_SHORT) |
| 258 | hwif->cbl = (reg42h & 0x02) ? ATA_CBL_PATA40 : ATA_CBL_PATA80; |
Jack Lee | 9c6712c | 2007-02-07 18:19:09 +0100 | [diff] [blame] | 259 | |
| 260 | /* |
| 261 | * The BIOS often doesn't set up DMA on this controller |
| 262 | * so we always do it. |
| 263 | */ |
| 264 | if (!noautodma) |
| 265 | hwif->autodma = 1; |
| 266 | |
| 267 | hwif->drives[0].autodma = hwif->autodma; |
| 268 | hwif->drives[1].autodma = hwif->autodma; |
Jack Lee | 9c6712c | 2007-02-07 18:19:09 +0100 | [diff] [blame] | 269 | } |
| 270 | |
| 271 | |
| 272 | #define DECLARE_ITE_DEV(name_str) \ |
| 273 | { \ |
| 274 | .name = name_str, \ |
Jack Lee | 9c6712c | 2007-02-07 18:19:09 +0100 | [diff] [blame] | 275 | .init_hwif = init_hwif_it8213, \ |
Bartlomiej Zolnierkiewicz | 6788182 | 2007-02-07 18:19:26 +0100 | [diff] [blame] | 276 | .autodma = AUTODMA, \ |
Jack Lee | 9c6712c | 2007-02-07 18:19:09 +0100 | [diff] [blame] | 277 | .enablebits = {{0x41,0x80,0x80}}, \ |
| 278 | .bootable = ON_BOARD, \ |
Bartlomiej Zolnierkiewicz | a5d8c5c | 2007-07-20 01:11:55 +0200 | [diff] [blame] | 279 | .host_flags = IDE_HFLAG_SINGLE, \ |
Bartlomiej Zolnierkiewicz | 4099d14 | 2007-07-20 01:11:59 +0200 | [diff] [blame] | 280 | .pio_mask = ATA_PIO4, \ |
Jack Lee | 9c6712c | 2007-02-07 18:19:09 +0100 | [diff] [blame] | 281 | } |
| 282 | |
| 283 | static ide_pci_device_t it8213_chipsets[] __devinitdata = { |
| 284 | /* 0 */ DECLARE_ITE_DEV("IT8213"), |
| 285 | }; |
| 286 | |
| 287 | |
| 288 | /** |
| 289 | * it8213_init_one - pci layer discovery entry |
| 290 | * @dev: PCI device |
| 291 | * @id: ident table entry |
| 292 | * |
| 293 | * Called by the PCI code when it finds an ITE8213 controller. As |
| 294 | * this device follows the standard interfaces we can use the |
| 295 | * standard helper functions to do almost all the work for us. |
| 296 | */ |
| 297 | |
| 298 | static int __devinit it8213_init_one(struct pci_dev *dev, const struct pci_device_id *id) |
| 299 | { |
| 300 | ide_setup_pci_device(dev, &it8213_chipsets[id->driver_data]); |
| 301 | return 0; |
| 302 | } |
| 303 | |
| 304 | |
| 305 | static struct pci_device_id it8213_pci_tbl[] = { |
Bartlomiej Zolnierkiewicz | 6788182 | 2007-02-07 18:19:26 +0100 | [diff] [blame] | 306 | { PCI_VENDOR_ID_ITE, 0x8213, PCI_ANY_ID, PCI_ANY_ID, 0, 0, 0 }, |
Jack Lee | 9c6712c | 2007-02-07 18:19:09 +0100 | [diff] [blame] | 307 | { 0, }, |
| 308 | }; |
| 309 | |
| 310 | MODULE_DEVICE_TABLE(pci, it8213_pci_tbl); |
| 311 | |
| 312 | static struct pci_driver driver = { |
| 313 | .name = "ITE8213_IDE", |
| 314 | .id_table = it8213_pci_tbl, |
| 315 | .probe = it8213_init_one, |
| 316 | }; |
| 317 | |
| 318 | static int __init it8213_ide_init(void) |
| 319 | { |
Bartlomiej Zolnierkiewicz | 6788182 | 2007-02-07 18:19:26 +0100 | [diff] [blame] | 320 | return ide_pci_register_driver(&driver); |
| 321 | } |
Jack Lee | 9c6712c | 2007-02-07 18:19:09 +0100 | [diff] [blame] | 322 | |
| 323 | module_init(it8213_ide_init); |
| 324 | |
Bartlomiej Zolnierkiewicz | 6788182 | 2007-02-07 18:19:26 +0100 | [diff] [blame] | 325 | MODULE_AUTHOR("Jack Lee, Alan Cox"); |
Jack Lee | 9c6712c | 2007-02-07 18:19:09 +0100 | [diff] [blame] | 326 | MODULE_DESCRIPTION("PCI driver module for the ITE 8213"); |
| 327 | MODULE_LICENSE("GPL"); |