2 * pata_ali.c - ALI 15x3 PATA for new ATA layer
4 * Alan Cox <alan@redhat.com>
7 * linux/drivers/ide/pci/alim15x3.c Version 0.17 2003/01/02
9 * Copyright (C) 1998-2000 Michel Aubry, Maintainer
10 * Copyright (C) 1998-2000 Andrzej Krzysztofowicz, Maintainer
11 * Copyright (C) 1999-2000 CJ, cjtsai@ali.com.tw, Maintainer
13 * Copyright (C) 1998-2000 Andre Hedrick (andre@linux-ide.org)
14 * May be copied or modified under the terms of the GNU General Public License
15 * Copyright (C) 2002 Alan Cox <alan@redhat.com>
16 * ALi (now ULi M5228) support by Clear Zhang <Clear.Zhang@ali.com.tw>
19 * Chipset documentation available under NDA only
22 * Cannot have ATAPI on both master & slave for rev < c2 (???) but
23 * otherwise should do atapi DMA.
26 #include <linux/kernel.h>
27 #include <linux/module.h>
28 #include <linux/pci.h>
29 #include <linux/init.h>
30 #include <linux/blkdev.h>
31 #include <linux/delay.h>
32 #include <scsi/scsi_host.h>
33 #include <linux/libata.h>
34 #include <linux/dmi.h>
36 #define DRV_NAME "pata_ali"
37 #define DRV_VERSION "0.7.2"
43 static struct dmi_system_id cable_dmi_table
[] = {
45 .ident
= "HP Pavilion N5430",
47 DMI_MATCH(DMI_BOARD_VENDOR
, "Hewlett-Packard"),
48 DMI_MATCH(DMI_BOARD_NAME
, "OmniBook N32N-736"),
54 static int ali_cable_override(struct pci_dev
*pdev
)
57 if (pdev
->subsystem_vendor
== 0x10CF && pdev
->subsystem_device
== 0x10AF)
60 if (dmi_check_system(cable_dmi_table
))
66 * ali_c2_cable_detect - cable detection
69 * Perform cable detection for C2 and later revisions
72 static int ali_c2_cable_detect(struct ata_port
*ap
)
74 struct pci_dev
*pdev
= to_pci_dev(ap
->host
->dev
);
77 /* Certain laptops use short but suitable cables and don't
78 implement the detect logic */
80 if (ali_cable_override(pdev
))
81 return ATA_CBL_PATA40_SHORT
;
83 /* Host view cable detect 0x4A bit 0 primary bit 1 secondary
85 pci_read_config_byte(pdev
, 0x4A, &ata66
);
86 if (ata66
& (1 << ap
->port_no
))
87 return ATA_CBL_PATA40
;
89 return ATA_CBL_PATA80
;
93 * ali_early_error_handler - reset for eary chip
96 * Handle the reset callback for the later chips with cable detect
99 static int ali_c2_pre_reset(struct ata_port
*ap
)
101 ap
->cbl
= ali_c2_cable_detect(ap
);
102 return ata_std_prereset(ap
);
105 static void ali_c2_error_handler(struct ata_port
*ap
)
107 ata_bmdma_drive_eh(ap
, ali_c2_pre_reset
,
108 ata_std_softreset
, NULL
,
113 * ali_early_cable_detect - cable detection
116 * Perform cable detection for older chipsets. This turns out to be
117 * rather easy to implement
120 static int ali_early_cable_detect(struct ata_port
*ap
)
122 return ATA_CBL_PATA40
;
126 * ali_early_probe_init - reset for early chip
129 * Handle the reset callback for the early (pre cable detect) chips.
132 static int ali_early_pre_reset(struct ata_port
*ap
)
134 ap
->cbl
= ali_early_cable_detect(ap
);
135 return ata_std_prereset(ap
);
138 static void ali_early_error_handler(struct ata_port
*ap
)
140 return ata_bmdma_drive_eh(ap
, ali_early_pre_reset
,
141 ata_std_softreset
, NULL
,
146 * ali_20_filter - filter for earlier ALI DMA
148 * @adev: attached device
150 * Ensure that we do not do DMA on CD devices. We may be able to
151 * fix that later on. Also ensure we do not do UDMA on WDC drives
154 static unsigned long ali_20_filter(const struct ata_port
*ap
, struct ata_device
*adev
, unsigned long mask
)
156 char model_num
[ATA_ID_PROD_LEN
+ 1];
157 /* No DMA on anything but a disk for now */
158 if (adev
->class != ATA_DEV_ATA
)
159 mask
&= ~(ATA_MASK_MWDMA
| ATA_MASK_UDMA
);
160 ata_id_c_string(adev
->id
, model_num
, ATA_ID_PROD
, sizeof(model_num
));
161 if (strstr(model_num
, "WDC"))
162 return mask
&= ~ATA_MASK_UDMA
;
163 return ata_pci_default_filter(ap
, adev
, mask
);
167 * ali_fifo_control - FIFO manager
168 * @ap: ALi channel to control
169 * @adev: device for FIFO control
170 * @on: 0 for off 1 for on
172 * Enable or disable the FIFO on a given device. Because of the way the
173 * ALi FIFO works it provides a boost on ATA disk but can be confused by
174 * ATAPI and we must therefore manage it.
177 static void ali_fifo_control(struct ata_port
*ap
, struct ata_device
*adev
, int on
)
179 struct pci_dev
*pdev
= to_pci_dev(ap
->host
->dev
);
180 int pio_fifo
= 0x54 + ap
->port_no
;
182 int shift
= 4 * adev
->devno
;
184 /* ATA - FIFO on set nibble to 0x05, ATAPI - FIFO off, set nibble to
185 0x00. Not all the docs agree but the behaviour we now use is the
186 one stated in the BIOS Programming Guide */
188 pci_read_config_byte(pdev
, pio_fifo
, &fifo
);
189 fifo
&= ~(0x0F << shift
);
191 fifo
|= (on
<< shift
);
192 pci_write_config_byte(pdev
, pio_fifo
, fifo
);
196 * ali_program_modes - load mode registers
197 * @ap: ALi channel to load
198 * @adev: Device the timing is for
199 * @cmd: Command timing
201 * @ultra: UDMA timing or zero for off
203 * Loads the timing registers for cmd/data and disable UDMA if
204 * ultra is zero. If ultra is set then load and enable the UDMA
205 * timing but do not touch the command/data timing.
208 static void ali_program_modes(struct ata_port
*ap
, struct ata_device
*adev
, struct ata_timing
*t
, u8 ultra
)
210 struct pci_dev
*pdev
= to_pci_dev(ap
->host
->dev
);
211 int cas
= 0x58 + 4 * ap
->port_no
; /* Command timing */
212 int cbt
= 0x59 + 4 * ap
->port_no
; /* Command timing */
213 int drwt
= 0x5A + 4 * ap
->port_no
+ adev
->devno
; /* R/W timing */
214 int udmat
= 0x56 + ap
->port_no
; /* UDMA timing */
215 int shift
= 4 * adev
->devno
;
219 t
->setup
= FIT(t
->setup
, 1, 8) & 7;
220 t
->act8b
= FIT(t
->act8b
, 1, 8) & 7;
221 t
->rec8b
= FIT(t
->rec8b
, 1, 16) & 15;
222 t
->active
= FIT(t
->active
, 1, 8) & 7;
223 t
->recover
= FIT(t
->recover
, 1, 16) & 15;
225 pci_write_config_byte(pdev
, cas
, t
->setup
);
226 pci_write_config_byte(pdev
, cbt
, (t
->act8b
<< 4) | t
->rec8b
);
227 pci_write_config_byte(pdev
, drwt
, (t
->active
<< 4) | t
->recover
);
230 /* Set up the UDMA enable */
231 pci_read_config_byte(pdev
, udmat
, &udma
);
232 udma
&= ~(0x0F << shift
);
233 udma
|= ultra
<< shift
;
234 pci_write_config_byte(pdev
, udmat
, udma
);
238 * ali_set_piomode - set initial PIO mode data
242 * Program the ALi registers for PIO mode. FIXME: add timings for
246 static void ali_set_piomode(struct ata_port
*ap
, struct ata_device
*adev
)
248 struct ata_device
*pair
= ata_dev_pair(adev
);
250 unsigned long T
= 1000000000 / 33333; /* PCI clock based */
252 ata_timing_compute(adev
, adev
->pio_mode
, &t
, T
, 1);
255 ata_timing_compute(pair
, pair
->pio_mode
, &p
, T
, 1);
256 ata_timing_merge(&p
, &t
, &t
, ATA_TIMING_SETUP
|ATA_TIMING_8BIT
);
257 if (pair
->dma_mode
) {
258 ata_timing_compute(pair
, pair
->dma_mode
, &p
, T
, 1);
259 ata_timing_merge(&p
, &t
, &t
, ATA_TIMING_SETUP
|ATA_TIMING_8BIT
);
263 /* PIO FIFO is only permitted on ATA disk */
264 if (adev
->class != ATA_DEV_ATA
)
265 ali_fifo_control(ap
, adev
, 0x00);
266 ali_program_modes(ap
, adev
, &t
, 0);
267 if (adev
->class == ATA_DEV_ATA
)
268 ali_fifo_control(ap
, adev
, 0x05);
273 * ali_set_dmamode - set initial DMA mode data
277 * FIXME: MWDMA timings
280 static void ali_set_dmamode(struct ata_port
*ap
, struct ata_device
*adev
)
282 static u8 udma_timing
[7] = { 0xC, 0xB, 0xA, 0x9, 0x8, 0xF, 0xD };
283 struct ata_device
*pair
= ata_dev_pair(adev
);
285 unsigned long T
= 1000000000 / 33333; /* PCI clock based */
286 struct pci_dev
*pdev
= to_pci_dev(ap
->host
->dev
);
289 if (adev
->class == ATA_DEV_ATA
)
290 ali_fifo_control(ap
, adev
, 0x08);
292 if (adev
->dma_mode
>= XFER_UDMA_0
) {
293 ali_program_modes(ap
, adev
, NULL
, udma_timing
[adev
->dma_mode
- XFER_UDMA_0
]);
294 if (adev
->dma_mode
>= XFER_UDMA_3
) {
296 pci_read_config_byte(pdev
, 0x4B, ®4b
);
298 pci_write_config_byte(pdev
, 0x4B, reg4b
);
301 ata_timing_compute(adev
, adev
->dma_mode
, &t
, T
, 1);
304 ata_timing_compute(pair
, pair
->pio_mode
, &p
, T
, 1);
305 ata_timing_merge(&p
, &t
, &t
, ATA_TIMING_SETUP
|ATA_TIMING_8BIT
);
306 if (pair
->dma_mode
) {
307 ata_timing_compute(pair
, pair
->dma_mode
, &p
, T
, 1);
308 ata_timing_merge(&p
, &t
, &t
, ATA_TIMING_SETUP
|ATA_TIMING_8BIT
);
311 ali_program_modes(ap
, adev
, &t
, 0);
316 * ali_lock_sectors - Keep older devices to 255 sector mode
320 * Called during the bus probe for each device that is found. We use
321 * this call to lock the sector count of the device to 255 or less on
322 * older ALi controllers. If we didn't do this then large I/O's would
323 * require LBA48 commands which the older ALi requires are issued by
327 static void ali_lock_sectors(struct ata_port
*ap
, struct ata_device
*adev
)
329 adev
->max_sectors
= 255;
332 static struct scsi_host_template ali_sht
= {
333 .module
= THIS_MODULE
,
335 .ioctl
= ata_scsi_ioctl
,
336 .queuecommand
= ata_scsi_queuecmd
,
337 .can_queue
= ATA_DEF_QUEUE
,
338 .this_id
= ATA_SHT_THIS_ID
,
339 .sg_tablesize
= LIBATA_MAX_PRD
,
340 .cmd_per_lun
= ATA_SHT_CMD_PER_LUN
,
341 .emulated
= ATA_SHT_EMULATED
,
342 .use_clustering
= ATA_SHT_USE_CLUSTERING
,
343 .proc_name
= DRV_NAME
,
344 .dma_boundary
= ATA_DMA_BOUNDARY
,
345 .slave_configure
= ata_scsi_slave_config
,
346 .slave_destroy
= ata_scsi_slave_destroy
,
347 .bios_param
= ata_std_bios_param
,
348 .resume
= ata_scsi_device_resume
,
349 .suspend
= ata_scsi_device_suspend
,
353 * Port operations for PIO only ALi
356 static struct ata_port_operations ali_early_port_ops
= {
357 .port_disable
= ata_port_disable
,
358 .set_piomode
= ali_set_piomode
,
359 .tf_load
= ata_tf_load
,
360 .tf_read
= ata_tf_read
,
361 .check_status
= ata_check_status
,
362 .exec_command
= ata_exec_command
,
363 .dev_select
= ata_std_dev_select
,
365 .freeze
= ata_bmdma_freeze
,
366 .thaw
= ata_bmdma_thaw
,
367 .error_handler
= ali_early_error_handler
,
368 .post_internal_cmd
= ata_bmdma_post_internal_cmd
,
370 .qc_prep
= ata_qc_prep
,
371 .qc_issue
= ata_qc_issue_prot
,
373 .data_xfer
= ata_pio_data_xfer
,
375 .irq_handler
= ata_interrupt
,
376 .irq_clear
= ata_bmdma_irq_clear
,
378 .port_start
= ata_port_start
,
382 * Port operations for DMA capable ALi without cable
385 static struct ata_port_operations ali_20_port_ops
= {
386 .port_disable
= ata_port_disable
,
388 .set_piomode
= ali_set_piomode
,
389 .set_dmamode
= ali_set_dmamode
,
390 .mode_filter
= ali_20_filter
,
392 .tf_load
= ata_tf_load
,
393 .tf_read
= ata_tf_read
,
394 .check_status
= ata_check_status
,
395 .exec_command
= ata_exec_command
,
396 .dev_select
= ata_std_dev_select
,
397 .dev_config
= ali_lock_sectors
,
399 .freeze
= ata_bmdma_freeze
,
400 .thaw
= ata_bmdma_thaw
,
401 .error_handler
= ali_early_error_handler
,
402 .post_internal_cmd
= ata_bmdma_post_internal_cmd
,
404 .bmdma_setup
= ata_bmdma_setup
,
405 .bmdma_start
= ata_bmdma_start
,
406 .bmdma_stop
= ata_bmdma_stop
,
407 .bmdma_status
= ata_bmdma_status
,
409 .qc_prep
= ata_qc_prep
,
410 .qc_issue
= ata_qc_issue_prot
,
412 .data_xfer
= ata_pio_data_xfer
,
414 .irq_handler
= ata_interrupt
,
415 .irq_clear
= ata_bmdma_irq_clear
,
417 .port_start
= ata_port_start
,
421 * Port operations for DMA capable ALi with cable detect
423 static struct ata_port_operations ali_c2_port_ops
= {
424 .port_disable
= ata_port_disable
,
425 .set_piomode
= ali_set_piomode
,
426 .set_dmamode
= ali_set_dmamode
,
427 .mode_filter
= ata_pci_default_filter
,
428 .tf_load
= ata_tf_load
,
429 .tf_read
= ata_tf_read
,
430 .check_status
= ata_check_status
,
431 .exec_command
= ata_exec_command
,
432 .dev_select
= ata_std_dev_select
,
433 .dev_config
= ali_lock_sectors
,
435 .freeze
= ata_bmdma_freeze
,
436 .thaw
= ata_bmdma_thaw
,
437 .error_handler
= ali_c2_error_handler
,
438 .post_internal_cmd
= ata_bmdma_post_internal_cmd
,
440 .bmdma_setup
= ata_bmdma_setup
,
441 .bmdma_start
= ata_bmdma_start
,
442 .bmdma_stop
= ata_bmdma_stop
,
443 .bmdma_status
= ata_bmdma_status
,
445 .qc_prep
= ata_qc_prep
,
446 .qc_issue
= ata_qc_issue_prot
,
448 .data_xfer
= ata_pio_data_xfer
,
450 .irq_handler
= ata_interrupt
,
451 .irq_clear
= ata_bmdma_irq_clear
,
453 .port_start
= ata_port_start
,
457 * Port operations for DMA capable ALi with cable detect and LBA48
459 static struct ata_port_operations ali_c5_port_ops
= {
460 .port_disable
= ata_port_disable
,
461 .set_piomode
= ali_set_piomode
,
462 .set_dmamode
= ali_set_dmamode
,
463 .mode_filter
= ata_pci_default_filter
,
464 .tf_load
= ata_tf_load
,
465 .tf_read
= ata_tf_read
,
466 .check_status
= ata_check_status
,
467 .exec_command
= ata_exec_command
,
468 .dev_select
= ata_std_dev_select
,
470 .freeze
= ata_bmdma_freeze
,
471 .thaw
= ata_bmdma_thaw
,
472 .error_handler
= ali_c2_error_handler
,
473 .post_internal_cmd
= ata_bmdma_post_internal_cmd
,
475 .bmdma_setup
= ata_bmdma_setup
,
476 .bmdma_start
= ata_bmdma_start
,
477 .bmdma_stop
= ata_bmdma_stop
,
478 .bmdma_status
= ata_bmdma_status
,
480 .qc_prep
= ata_qc_prep
,
481 .qc_issue
= ata_qc_issue_prot
,
483 .data_xfer
= ata_pio_data_xfer
,
485 .irq_handler
= ata_interrupt
,
486 .irq_clear
= ata_bmdma_irq_clear
,
488 .port_start
= ata_port_start
,
493 * ali_init_chipset - chip setup function
494 * @pdev: PCI device of ATA controller
496 * Perform the setup on the device that must be done both at boot
497 * and at resume time.
500 static void ali_init_chipset(struct pci_dev
*pdev
)
503 struct pci_dev
*north
, *isa_bridge
;
505 pci_read_config_byte(pdev
, PCI_REVISION_ID
, &rev
);
508 * The chipset revision selects the driver operations and
512 if (rev
>= 0x20 && rev
< 0xC2) {
513 /* 1543-E/F, 1543C-C, 1543C-D, 1543C-E */
514 pci_read_config_byte(pdev
, 0x4B, &tmp
);
515 /* Clear CD-ROM DMA write bit */
517 pci_write_config_byte(pdev
, 0x4B, tmp
);
518 } else if (rev
>= 0xC2) {
519 /* Enable cable detection logic */
520 pci_read_config_byte(pdev
, 0x4B, &tmp
);
521 pci_write_config_byte(pdev
, 0x4B, tmp
| 0x08);
523 north
= pci_get_bus_and_slot(0, PCI_DEVFN(0,0));
524 isa_bridge
= pci_get_device(PCI_VENDOR_ID_AL
, PCI_DEVICE_ID_AL_M1533
, NULL
);
526 if (north
&& north
->vendor
== PCI_VENDOR_ID_AL
&& isa_bridge
) {
527 /* Configure the ALi bridge logic. For non ALi rely on BIOS.
528 Set the south bridge enable bit */
529 pci_read_config_byte(isa_bridge
, 0x79, &tmp
);
531 pci_write_config_byte(isa_bridge
, 0x79, tmp
| 0x04);
532 else if (rev
> 0xC2 && rev
< 0xC5)
533 pci_write_config_byte(isa_bridge
, 0x79, tmp
| 0x02);
537 * CD_ROM DMA on (0x53 bit 0). Enable this even if we want
538 * to use PIO. 0x53 bit 1 (rev 20 only) - enable FIFO control
541 pci_read_config_byte(pdev
, 0x53, &tmp
);
547 tmp
|= 0x01; /* CD_ROM enable for DMA */
548 pci_write_config_byte(pdev
, 0x53, tmp
);
550 pci_dev_put(isa_bridge
);
552 ata_pci_clear_simplex(pdev
);
555 * ali_init_one - discovery callback
556 * @pdev: PCI device ID
557 * @id: PCI table info
559 * An ALi IDE interface has been discovered. Figure out what revision
560 * and perform configuration work before handing it to the ATA layer
563 static int ali_init_one(struct pci_dev
*pdev
, const struct pci_device_id
*id
)
565 static struct ata_port_info info_early
= {
567 .flags
= ATA_FLAG_SLAVE_POSS
| ATA_FLAG_SRST
,
569 .port_ops
= &ali_early_port_ops
571 /* Revision 0x20 added DMA */
572 static struct ata_port_info info_20
= {
574 .flags
= ATA_FLAG_SLAVE_POSS
| ATA_FLAG_SRST
| ATA_FLAG_PIO_LBA48
,
577 .port_ops
= &ali_20_port_ops
579 /* Revision 0x20 with support logic added UDMA */
580 static struct ata_port_info info_20_udma
= {
582 .flags
= ATA_FLAG_SLAVE_POSS
| ATA_FLAG_SRST
| ATA_FLAG_PIO_LBA48
,
585 .udma_mask
= 0x07, /* UDMA33 */
586 .port_ops
= &ali_20_port_ops
588 /* Revision 0xC2 adds UDMA66 */
589 static struct ata_port_info info_c2
= {
591 .flags
= ATA_FLAG_SLAVE_POSS
| ATA_FLAG_SRST
| ATA_FLAG_PIO_LBA48
,
595 .port_ops
= &ali_c2_port_ops
597 /* Revision 0xC3 is UDMA100 */
598 static struct ata_port_info info_c3
= {
600 .flags
= ATA_FLAG_SLAVE_POSS
| ATA_FLAG_SRST
| ATA_FLAG_PIO_LBA48
,
604 .port_ops
= &ali_c2_port_ops
606 /* Revision 0xC4 is UDMA133 */
607 static struct ata_port_info info_c4
= {
609 .flags
= ATA_FLAG_SLAVE_POSS
| ATA_FLAG_SRST
| ATA_FLAG_PIO_LBA48
,
613 .port_ops
= &ali_c2_port_ops
615 /* Revision 0xC5 is UDMA133 with LBA48 DMA */
616 static struct ata_port_info info_c5
= {
618 .flags
= ATA_FLAG_SLAVE_POSS
| ATA_FLAG_SRST
,
622 .port_ops
= &ali_c5_port_ops
625 static struct ata_port_info
*port_info
[2];
627 struct pci_dev
*isa_bridge
;
629 pci_read_config_byte(pdev
, PCI_REVISION_ID
, &rev
);
632 * The chipset revision selects the driver operations and
637 port_info
[0] = port_info
[1] = &info_early
;
638 } else if (rev
< 0xC2) {
639 port_info
[0] = port_info
[1] = &info_20
;
640 } else if (rev
== 0xC2) {
641 port_info
[0] = port_info
[1] = &info_c2
;
642 } else if (rev
== 0xC3) {
643 port_info
[0] = port_info
[1] = &info_c3
;
644 } else if (rev
== 0xC4) {
645 port_info
[0] = port_info
[1] = &info_c4
;
647 port_info
[0] = port_info
[1] = &info_c5
;
649 ali_init_chipset(pdev
);
651 isa_bridge
= pci_get_device(PCI_VENDOR_ID_AL
, PCI_DEVICE_ID_AL_M1533
, NULL
);
652 if (isa_bridge
&& rev
>= 0x20 && rev
< 0xC2) {
653 /* Are we paired with a UDMA capable chip */
654 pci_read_config_byte(isa_bridge
, 0x5E, &tmp
);
655 if ((tmp
& 0x1E) == 0x12)
656 port_info
[0] = port_info
[1] = &info_20_udma
;
657 pci_dev_put(isa_bridge
);
659 return ata_pci_init_one(pdev
, port_info
, 2);
662 static int ali_reinit_one(struct pci_dev
*pdev
)
664 ali_init_chipset(pdev
);
665 return ata_pci_device_resume(pdev
);
668 static const struct pci_device_id ali
[] = {
669 { PCI_VDEVICE(AL
, PCI_DEVICE_ID_AL_M5228
), },
670 { PCI_VDEVICE(AL
, PCI_DEVICE_ID_AL_M5229
), },
675 static struct pci_driver ali_pci_driver
= {
678 .probe
= ali_init_one
,
679 .remove
= ata_pci_remove_one
,
680 .suspend
= ata_pci_device_suspend
,
681 .resume
= ali_reinit_one
,
684 static int __init
ali_init(void)
686 return pci_register_driver(&ali_pci_driver
);
690 static void __exit
ali_exit(void)
692 pci_unregister_driver(&ali_pci_driver
);
696 MODULE_AUTHOR("Alan Cox");
697 MODULE_DESCRIPTION("low-level driver for ALi PATA");
698 MODULE_LICENSE("GPL");
699 MODULE_DEVICE_TABLE(pci
, ali
);
700 MODULE_VERSION(DRV_VERSION
);
702 module_init(ali_init
);
703 module_exit(ali_exit
);