blob: 81a134cca3cd9f79efc0041164d97eaac73ac152 [file] [log] [blame]
Jeff Garzik669a5db2006-08-29 18:12:40 -04001/*
2 * pata_pdc202xx_old.c - Promise PDC202xx PATA for new ATA layer
3 * (C) 2005 Red Hat Inc
4 * Alan Cox <alan@redhat.com>
5 *
6 * Based in part on linux/drivers/ide/pci/pdc202xx_old.c
7 *
8 * First cut with LBA48/ATAPI
9 *
10 * TODO:
11 * Channel interlock/reset on both required ?
12 */
Jeff Garzik85cd7252006-08-31 00:03:49 -040013
Jeff Garzik669a5db2006-08-29 18:12:40 -040014#include <linux/kernel.h>
15#include <linux/module.h>
16#include <linux/pci.h>
17#include <linux/init.h>
18#include <linux/blkdev.h>
19#include <linux/delay.h>
20#include <scsi/scsi_host.h>
21#include <linux/libata.h>
22
23#define DRV_NAME "pata_pdc202xx_old"
Alan62d64ae2006-11-27 16:27:20 +000024#define DRV_VERSION "0.2.3"
Jeff Garzik669a5db2006-08-29 18:12:40 -040025
26/**
27 * pdc2024x_pre_reset - probe begin
28 * @ap: ATA port
29 *
30 * Set up cable type and use generic probe init
31 */
Jeff Garzik85cd7252006-08-31 00:03:49 -040032
Jeff Garzik669a5db2006-08-29 18:12:40 -040033static int pdc2024x_pre_reset(struct ata_port *ap)
34{
35 ap->cbl = ATA_CBL_PATA40;
36 return ata_std_prereset(ap);
37}
38
39
40static void pdc2024x_error_handler(struct ata_port *ap)
41{
42 ata_bmdma_drive_eh(ap, pdc2024x_pre_reset, ata_std_softreset, NULL, ata_std_postreset);
43}
44
45
46static int pdc2026x_pre_reset(struct ata_port *ap)
47{
48 struct pci_dev *pdev = to_pci_dev(ap->host->dev);
49 u16 cis;
Jeff Garzik85cd7252006-08-31 00:03:49 -040050
Jeff Garzik669a5db2006-08-29 18:12:40 -040051 pci_read_config_word(pdev, 0x50, &cis);
52 if (cis & (1 << (10 + ap->port_no)))
53 ap->cbl = ATA_CBL_PATA80;
54 else
55 ap->cbl = ATA_CBL_PATA40;
56
57 return ata_std_prereset(ap);
58}
59
60static void pdc2026x_error_handler(struct ata_port *ap)
61{
62 ata_bmdma_drive_eh(ap, pdc2026x_pre_reset, ata_std_softreset, NULL, ata_std_postreset);
63}
64
65/**
Alan Coxada406c2006-11-03 13:18:06 +000066 * pdc202xx_configure_piomode - set chip PIO timing
Jeff Garzik669a5db2006-08-29 18:12:40 -040067 * @ap: ATA interface
68 * @adev: ATA device
69 * @pio: PIO mode
70 *
71 * Called to do the PIO mode setup. Our timing registers are shared
72 * so a configure_dmamode call will undo any work we do here and vice
73 * versa
74 */
Jeff Garzik85cd7252006-08-31 00:03:49 -040075
Alan Coxada406c2006-11-03 13:18:06 +000076static void pdc202xx_configure_piomode(struct ata_port *ap, struct ata_device *adev, int pio)
Jeff Garzik669a5db2006-08-29 18:12:40 -040077{
78 struct pci_dev *pdev = to_pci_dev(ap->host->dev);
79 int port = 0x60 + 4 * ap->port_no + 2 * adev->devno;
80 static u16 pio_timing[5] = {
81 0x0913, 0x050C , 0x0308, 0x0206, 0x0104
82 };
83 u8 r_ap, r_bp;
84
85 pci_read_config_byte(pdev, port, &r_ap);
86 pci_read_config_byte(pdev, port + 1, &r_bp);
87 r_ap &= ~0x3F; /* Preserve ERRDY_EN, SYNC_IN */
88 r_bp &= ~0x07;
89 r_ap |= (pio_timing[pio] >> 8);
90 r_bp |= (pio_timing[pio] & 0xFF);
Jeff Garzik85cd7252006-08-31 00:03:49 -040091
Jeff Garzik669a5db2006-08-29 18:12:40 -040092 if (ata_pio_need_iordy(adev))
93 r_ap |= 0x20; /* IORDY enable */
94 if (adev->class == ATA_DEV_ATA)
95 r_ap |= 0x10; /* FIFO enable */
96 pci_write_config_byte(pdev, port, r_ap);
97 pci_write_config_byte(pdev, port + 1, r_bp);
98}
99
100/**
Alan Coxada406c2006-11-03 13:18:06 +0000101 * pdc202xx_set_piomode - set initial PIO mode data
Jeff Garzik669a5db2006-08-29 18:12:40 -0400102 * @ap: ATA interface
103 * @adev: ATA device
104 *
105 * Called to do the PIO mode setup. Our timing registers are shared
106 * but we want to set the PIO timing by default.
107 */
Jeff Garzik85cd7252006-08-31 00:03:49 -0400108
Alan Coxada406c2006-11-03 13:18:06 +0000109static void pdc202xx_set_piomode(struct ata_port *ap, struct ata_device *adev)
Jeff Garzik669a5db2006-08-29 18:12:40 -0400110{
Alan Coxada406c2006-11-03 13:18:06 +0000111 pdc202xx_configure_piomode(ap, adev, adev->pio_mode - XFER_PIO_0);
Jeff Garzik669a5db2006-08-29 18:12:40 -0400112}
113
114/**
Alan Coxada406c2006-11-03 13:18:06 +0000115 * pdc202xx_configure_dmamode - set DMA mode in chip
Jeff Garzik669a5db2006-08-29 18:12:40 -0400116 * @ap: ATA interface
117 * @adev: ATA device
118 *
119 * Load DMA cycle times into the chip ready for a DMA transfer
120 * to occur.
121 */
Jeff Garzik85cd7252006-08-31 00:03:49 -0400122
Alan Coxada406c2006-11-03 13:18:06 +0000123static void pdc202xx_set_dmamode(struct ata_port *ap, struct ata_device *adev)
Jeff Garzik669a5db2006-08-29 18:12:40 -0400124{
125 struct pci_dev *pdev = to_pci_dev(ap->host->dev);
126 int port = 0x60 + 4 * ap->port_no + 2 * adev->devno;
127 static u8 udma_timing[6][2] = {
128 { 0x60, 0x03 }, /* 33 Mhz Clock */
129 { 0x40, 0x02 },
130 { 0x20, 0x01 },
131 { 0x40, 0x02 }, /* 66 Mhz Clock */
132 { 0x20, 0x01 },
Jeff Garzik85cd7252006-08-31 00:03:49 -0400133 { 0x20, 0x01 }
Jeff Garzik669a5db2006-08-29 18:12:40 -0400134 };
135 u8 r_bp, r_cp;
Jeff Garzik85cd7252006-08-31 00:03:49 -0400136
Jeff Garzik669a5db2006-08-29 18:12:40 -0400137 pci_read_config_byte(pdev, port + 1, &r_bp);
138 pci_read_config_byte(pdev, port + 2, &r_cp);
Jeff Garzik85cd7252006-08-31 00:03:49 -0400139
Jeff Garzik669a5db2006-08-29 18:12:40 -0400140 r_bp &= ~0xF0;
141 r_cp &= ~0x0F;
Jeff Garzik85cd7252006-08-31 00:03:49 -0400142
Jeff Garzik669a5db2006-08-29 18:12:40 -0400143 if (adev->dma_mode >= XFER_UDMA_0) {
144 int speed = adev->dma_mode - XFER_UDMA_0;
145 r_bp |= udma_timing[speed][0];
146 r_cp |= udma_timing[speed][1];
Jeff Garzik85cd7252006-08-31 00:03:49 -0400147
Jeff Garzik669a5db2006-08-29 18:12:40 -0400148 } else {
149 int speed = adev->dma_mode - XFER_MW_DMA_0;
150 r_bp |= 0x60;
151 r_cp |= (5 - speed);
152 }
153 pci_write_config_byte(pdev, port + 1, r_bp);
154 pci_write_config_byte(pdev, port + 2, r_cp);
Jeff Garzik85cd7252006-08-31 00:03:49 -0400155
Jeff Garzik669a5db2006-08-29 18:12:40 -0400156}
157
158/**
159 * pdc2026x_bmdma_start - DMA engine begin
160 * @qc: ATA command
161 *
162 * In UDMA3 or higher we have to clock switch for the duration of the
163 * DMA transfer sequence.
164 */
Jeff Garzik85cd7252006-08-31 00:03:49 -0400165
Jeff Garzik669a5db2006-08-29 18:12:40 -0400166static void pdc2026x_bmdma_start(struct ata_queued_cmd *qc)
167{
168 struct ata_port *ap = qc->ap;
169 struct ata_device *adev = qc->dev;
170 struct ata_taskfile *tf = &qc->tf;
171 int sel66 = ap->port_no ? 0x08: 0x02;
172
173 unsigned long master = ap->host->ports[0]->ioaddr.bmdma_addr;
174 unsigned long clock = master + 0x11;
175 unsigned long atapi_reg = master + 0x20 + (4 * ap->port_no);
Jeff Garzik85cd7252006-08-31 00:03:49 -0400176
Jeff Garzik669a5db2006-08-29 18:12:40 -0400177 u32 len;
Jeff Garzik85cd7252006-08-31 00:03:49 -0400178
Jeff Garzik669a5db2006-08-29 18:12:40 -0400179 /* Check we keep host level locking here */
180 if (adev->dma_mode >= XFER_UDMA_2)
181 outb(inb(clock) | sel66, clock);
182 else
183 outb(inb(clock) & ~sel66, clock);
184
Jeff Garzik85cd7252006-08-31 00:03:49 -0400185 /* The DMA clocks may have been trashed by a reset. FIXME: make conditional
Jeff Garzik669a5db2006-08-29 18:12:40 -0400186 and move to qc_issue ? */
Alan Coxada406c2006-11-03 13:18:06 +0000187 pdc202xx_set_dmamode(ap, qc->dev);
Jeff Garzik669a5db2006-08-29 18:12:40 -0400188
189 /* Cases the state machine will not complete correctly without help */
190 if ((tf->flags & ATA_TFLAG_LBA48) || tf->protocol == ATA_PROT_ATAPI_DMA)
191 {
192 if (tf->flags & ATA_TFLAG_LBA48)
193 len = qc->nsect * 512;
194 else
195 len = qc->nbytes;
Jeff Garzik85cd7252006-08-31 00:03:49 -0400196
Jeff Garzik669a5db2006-08-29 18:12:40 -0400197 if (tf->flags & ATA_TFLAG_WRITE)
198 len |= 0x06000000;
199 else
200 len |= 0x05000000;
Jeff Garzik85cd7252006-08-31 00:03:49 -0400201
Jeff Garzik669a5db2006-08-29 18:12:40 -0400202 outl(len, atapi_reg);
203 }
Jeff Garzik85cd7252006-08-31 00:03:49 -0400204
205 /* Activate DMA */
Jeff Garzik669a5db2006-08-29 18:12:40 -0400206 ata_bmdma_start(qc);
207}
208
209/**
210 * pdc2026x_bmdma_end - DMA engine stop
211 * @qc: ATA command
212 *
213 * After a DMA completes we need to put the clock back to 33MHz for
214 * PIO timings.
215 */
Jeff Garzik85cd7252006-08-31 00:03:49 -0400216
Jeff Garzik669a5db2006-08-29 18:12:40 -0400217static void pdc2026x_bmdma_stop(struct ata_queued_cmd *qc)
218{
219 struct ata_port *ap = qc->ap;
220 struct ata_device *adev = qc->dev;
221 struct ata_taskfile *tf = &qc->tf;
Jeff Garzik85cd7252006-08-31 00:03:49 -0400222
Jeff Garzik669a5db2006-08-29 18:12:40 -0400223 int sel66 = ap->port_no ? 0x08: 0x02;
224 /* The clock bits are in the same register for both channels */
225 unsigned long master = ap->host->ports[0]->ioaddr.bmdma_addr;
226 unsigned long clock = master + 0x11;
227 unsigned long atapi_reg = master + 0x20 + (4 * ap->port_no);
Jeff Garzik85cd7252006-08-31 00:03:49 -0400228
Jeff Garzik669a5db2006-08-29 18:12:40 -0400229 /* Cases the state machine will not complete correctly */
230 if (tf->protocol == ATA_PROT_ATAPI_DMA || ( tf->flags & ATA_TFLAG_LBA48)) {
231 outl(0, atapi_reg);
232 outb(inb(clock) & ~sel66, clock);
233 }
234 /* Check we keep host level locking here */
235 /* Flip back to 33Mhz for PIO */
236 if (adev->dma_mode >= XFER_UDMA_2)
237 outb(inb(clock) & ~sel66, clock);
238
239 ata_bmdma_stop(qc);
240}
241
242/**
243 * pdc2026x_dev_config - device setup hook
244 * @ap: ATA port
245 * @adev: newly found device
246 *
247 * Perform chip specific early setup. We need to lock the transfer
248 * sizes to 8bit to avoid making the state engine on the 2026x cards
249 * barf.
250 */
Jeff Garzik85cd7252006-08-31 00:03:49 -0400251
Jeff Garzik669a5db2006-08-29 18:12:40 -0400252static void pdc2026x_dev_config(struct ata_port *ap, struct ata_device *adev)
253{
254 adev->max_sectors = 256;
255}
256
Alan Coxada406c2006-11-03 13:18:06 +0000257static struct scsi_host_template pdc202xx_sht = {
Jeff Garzik669a5db2006-08-29 18:12:40 -0400258 .module = THIS_MODULE,
259 .name = DRV_NAME,
260 .ioctl = ata_scsi_ioctl,
261 .queuecommand = ata_scsi_queuecmd,
262 .can_queue = ATA_DEF_QUEUE,
263 .this_id = ATA_SHT_THIS_ID,
264 .sg_tablesize = LIBATA_MAX_PRD,
265 .max_sectors = ATA_MAX_SECTORS,
266 .cmd_per_lun = ATA_SHT_CMD_PER_LUN,
267 .emulated = ATA_SHT_EMULATED,
268 .use_clustering = ATA_SHT_USE_CLUSTERING,
269 .proc_name = DRV_NAME,
270 .dma_boundary = ATA_DMA_BOUNDARY,
271 .slave_configure = ata_scsi_slave_config,
Tejun Heoafdfe892006-11-29 11:26:47 +0900272 .slave_destroy = ata_scsi_slave_destroy,
Jeff Garzik669a5db2006-08-29 18:12:40 -0400273 .bios_param = ata_std_bios_param,
Alan62d64ae2006-11-27 16:27:20 +0000274 .resume = ata_scsi_device_resume,
275 .suspend = ata_scsi_device_suspend,
Jeff Garzik669a5db2006-08-29 18:12:40 -0400276};
277
278static struct ata_port_operations pdc2024x_port_ops = {
279 .port_disable = ata_port_disable,
Alan Coxada406c2006-11-03 13:18:06 +0000280 .set_piomode = pdc202xx_set_piomode,
281 .set_dmamode = pdc202xx_set_dmamode,
Jeff Garzik669a5db2006-08-29 18:12:40 -0400282 .mode_filter = ata_pci_default_filter,
283 .tf_load = ata_tf_load,
284 .tf_read = ata_tf_read,
285 .check_status = ata_check_status,
286 .exec_command = ata_exec_command,
287 .dev_select = ata_std_dev_select,
288
289 .freeze = ata_bmdma_freeze,
290 .thaw = ata_bmdma_thaw,
291 .error_handler = pdc2024x_error_handler,
292 .post_internal_cmd = ata_bmdma_post_internal_cmd,
293
294 .bmdma_setup = ata_bmdma_setup,
295 .bmdma_start = ata_bmdma_start,
296 .bmdma_stop = ata_bmdma_stop,
297 .bmdma_status = ata_bmdma_status,
298
299 .qc_prep = ata_qc_prep,
300 .qc_issue = ata_qc_issue_prot,
301 .data_xfer = ata_pio_data_xfer,
302
303 .irq_handler = ata_interrupt,
304 .irq_clear = ata_bmdma_irq_clear,
Jeff Garzik85cd7252006-08-31 00:03:49 -0400305
Jeff Garzik669a5db2006-08-29 18:12:40 -0400306 .port_start = ata_port_start,
307 .port_stop = ata_port_stop,
308 .host_stop = ata_host_stop
Jeff Garzik85cd7252006-08-31 00:03:49 -0400309};
Jeff Garzik669a5db2006-08-29 18:12:40 -0400310
311static struct ata_port_operations pdc2026x_port_ops = {
312 .port_disable = ata_port_disable,
Alan Coxada406c2006-11-03 13:18:06 +0000313 .set_piomode = pdc202xx_set_piomode,
314 .set_dmamode = pdc202xx_set_dmamode,
Jeff Garzik669a5db2006-08-29 18:12:40 -0400315 .mode_filter = ata_pci_default_filter,
316 .tf_load = ata_tf_load,
317 .tf_read = ata_tf_read,
318 .check_status = ata_check_status,
319 .exec_command = ata_exec_command,
320 .dev_select = ata_std_dev_select,
321 .dev_config = pdc2026x_dev_config,
322
323 .freeze = ata_bmdma_freeze,
324 .thaw = ata_bmdma_thaw,
325 .error_handler = pdc2026x_error_handler,
326 .post_internal_cmd = ata_bmdma_post_internal_cmd,
327
328 .bmdma_setup = ata_bmdma_setup,
329 .bmdma_start = pdc2026x_bmdma_start,
330 .bmdma_stop = pdc2026x_bmdma_stop,
331 .bmdma_status = ata_bmdma_status,
332
333 .qc_prep = ata_qc_prep,
334 .qc_issue = ata_qc_issue_prot,
335 .data_xfer = ata_pio_data_xfer,
336
337 .irq_handler = ata_interrupt,
338 .irq_clear = ata_bmdma_irq_clear,
Jeff Garzik85cd7252006-08-31 00:03:49 -0400339
Jeff Garzik669a5db2006-08-29 18:12:40 -0400340 .port_start = ata_port_start,
341 .port_stop = ata_port_stop,
342 .host_stop = ata_host_stop
Jeff Garzik85cd7252006-08-31 00:03:49 -0400343};
Jeff Garzik669a5db2006-08-29 18:12:40 -0400344
Alan Coxada406c2006-11-03 13:18:06 +0000345static int pdc202xx_init_one(struct pci_dev *dev, const struct pci_device_id *id)
Jeff Garzik669a5db2006-08-29 18:12:40 -0400346{
347 static struct ata_port_info info[3] = {
348 {
Alan Coxada406c2006-11-03 13:18:06 +0000349 .sht = &pdc202xx_sht,
Jeff Garzik669a5db2006-08-29 18:12:40 -0400350 .flags = ATA_FLAG_SLAVE_POSS | ATA_FLAG_SRST,
351 .pio_mask = 0x1f,
352 .mwdma_mask = 0x07,
353 .udma_mask = ATA_UDMA2,
354 .port_ops = &pdc2024x_port_ops
Jeff Garzik85cd7252006-08-31 00:03:49 -0400355 },
Jeff Garzik669a5db2006-08-29 18:12:40 -0400356 {
Alan Coxada406c2006-11-03 13:18:06 +0000357 .sht = &pdc202xx_sht,
Jeff Garzik669a5db2006-08-29 18:12:40 -0400358 .flags = ATA_FLAG_SLAVE_POSS | ATA_FLAG_SRST,
359 .pio_mask = 0x1f,
360 .mwdma_mask = 0x07,
361 .udma_mask = ATA_UDMA4,
362 .port_ops = &pdc2026x_port_ops
363 },
364 {
Alan Coxada406c2006-11-03 13:18:06 +0000365 .sht = &pdc202xx_sht,
Jeff Garzik669a5db2006-08-29 18:12:40 -0400366 .flags = ATA_FLAG_SLAVE_POSS | ATA_FLAG_SRST,
367 .pio_mask = 0x1f,
368 .mwdma_mask = 0x07,
369 .udma_mask = ATA_UDMA5,
370 .port_ops = &pdc2026x_port_ops
371 }
Jeff Garzik85cd7252006-08-31 00:03:49 -0400372
Jeff Garzik669a5db2006-08-29 18:12:40 -0400373 };
374 static struct ata_port_info *port_info[2];
375
376 port_info[0] = port_info[1] = &info[id->driver_data];
Jeff Garzik85cd7252006-08-31 00:03:49 -0400377
Jeff Garzik669a5db2006-08-29 18:12:40 -0400378 if (dev->device == PCI_DEVICE_ID_PROMISE_20265) {
379 struct pci_dev *bridge = dev->bus->self;
380 /* Don't grab anything behind a Promise I2O RAID */
381 if (bridge && bridge->vendor == PCI_VENDOR_ID_INTEL) {
382 if( bridge->device == PCI_DEVICE_ID_INTEL_I960)
383 return -ENODEV;
384 if( bridge->device == PCI_DEVICE_ID_INTEL_I960RM)
385 return -ENODEV;
386 }
387 }
388 return ata_pci_init_one(dev, port_info, 2);
389}
390
Alan Coxada406c2006-11-03 13:18:06 +0000391static const struct pci_device_id pdc202xx[] = {
Jeff Garzik2d2744f2006-09-28 20:21:59 -0400392 { PCI_VDEVICE(PROMISE, PCI_DEVICE_ID_PROMISE_20246), 0 },
393 { PCI_VDEVICE(PROMISE, PCI_DEVICE_ID_PROMISE_20262), 1 },
394 { PCI_VDEVICE(PROMISE, PCI_DEVICE_ID_PROMISE_20263), 1 },
395 { PCI_VDEVICE(PROMISE, PCI_DEVICE_ID_PROMISE_20265), 2 },
396 { PCI_VDEVICE(PROMISE, PCI_DEVICE_ID_PROMISE_20267), 2 },
397
398 { },
Jeff Garzik669a5db2006-08-29 18:12:40 -0400399};
400
Alan Coxada406c2006-11-03 13:18:06 +0000401static struct pci_driver pdc202xx_pci_driver = {
Jeff Garzik2d2744f2006-09-28 20:21:59 -0400402 .name = DRV_NAME,
Alan Coxada406c2006-11-03 13:18:06 +0000403 .id_table = pdc202xx,
404 .probe = pdc202xx_init_one,
Alan62d64ae2006-11-27 16:27:20 +0000405 .remove = ata_pci_remove_one,
406 .suspend = ata_pci_device_suspend,
407 .resume = ata_pci_device_resume,
Jeff Garzik669a5db2006-08-29 18:12:40 -0400408};
409
Alan Coxada406c2006-11-03 13:18:06 +0000410static int __init pdc202xx_init(void)
Jeff Garzik669a5db2006-08-29 18:12:40 -0400411{
Alan Coxada406c2006-11-03 13:18:06 +0000412 return pci_register_driver(&pdc202xx_pci_driver);
Jeff Garzik669a5db2006-08-29 18:12:40 -0400413}
414
Alan Coxada406c2006-11-03 13:18:06 +0000415static void __exit pdc202xx_exit(void)
Jeff Garzik669a5db2006-08-29 18:12:40 -0400416{
Alan Coxada406c2006-11-03 13:18:06 +0000417 pci_unregister_driver(&pdc202xx_pci_driver);
Jeff Garzik669a5db2006-08-29 18:12:40 -0400418}
419
Jeff Garzik669a5db2006-08-29 18:12:40 -0400420MODULE_AUTHOR("Alan Cox");
421MODULE_DESCRIPTION("low-level driver for Promise 2024x and 20262-20267");
422MODULE_LICENSE("GPL");
Alan Coxada406c2006-11-03 13:18:06 +0000423MODULE_DEVICE_TABLE(pci, pdc202xx);
Jeff Garzik669a5db2006-08-29 18:12:40 -0400424MODULE_VERSION(DRV_VERSION);
425
Alan Coxada406c2006-11-03 13:18:06 +0000426module_init(pdc202xx_init);
427module_exit(pdc202xx_exit);