2 * sata_sil.c - Silicon Image SATA
4 * Maintained by: Jeff Garzik <jgarzik@pobox.com>
5 * Please ALWAYS copy linux-ide@vger.kernel.org
8 * Copyright 2003 Red Hat, Inc.
9 * Copyright 2003 Benjamin Herrenschmidt
11 * The contents of this file are subject to the Open
12 * Software License version 1.1 that can be found at
13 * http://www.opensource.org/licenses/osl-1.1.txt and is included herein
16 * Alternatively, the contents of this file may be used under the terms
17 * of the GNU General Public License version 2 (the "GPL") as distributed
18 * in the kernel source COPYING file, in which case the provisions of
19 * the GPL are applicable instead of the above. If you wish to allow
20 * the use of your version of this file only under the terms of the
21 * GPL and not to allow others to use your version of this file under
22 * the OSL, indicate your decision by deleting the provisions above and
23 * replace them with the notice and other provisions required by the GPL.
24 * If you do not delete the provisions above, a recipient may use your
25 * version of this file under either the OSL or the GPL.
27 * Documentation for SiI 3112:
28 * http://gkernel.sourceforge.net/specs/sii/3112A_SiI-DS-0095-B2.pdf.bz2
30 * Other errata and documentation available under NDA.
34 #include <linux/kernel.h>
35 #include <linux/module.h>
36 #include <linux/pci.h>
37 #include <linux/init.h>
38 #include <linux/blkdev.h>
39 #include <linux/delay.h>
40 #include <linux/interrupt.h>
42 #include <scsi/scsi_host.h>
43 #include <linux/libata.h>
45 #define DRV_NAME "sata_sil"
46 #define DRV_VERSION "0.9"
49 SIL_FLAG_MOD15WRITE
= (1 << 30),
65 SIL_MASK_IDE0_INT
= (1 << 22),
66 SIL_MASK_IDE1_INT
= (1 << 23),
67 SIL_MASK_IDE2_INT
= (1 << 24),
68 SIL_MASK_IDE3_INT
= (1 << 25),
69 SIL_MASK_2PORT
= SIL_MASK_IDE0_INT
| SIL_MASK_IDE1_INT
,
70 SIL_MASK_4PORT
= SIL_MASK_2PORT
|
71 SIL_MASK_IDE2_INT
| SIL_MASK_IDE3_INT
,
73 SIL_IDE2_BMDMA
= 0x200,
75 SIL_INTR_STEERING
= (1 << 1),
76 SIL_QUIRK_MOD15WRITE
= (1 << 0),
77 SIL_QUIRK_UDMA5MAX
= (1 << 1),
80 static int sil_init_one (struct pci_dev
*pdev
, const struct pci_device_id
*ent
);
81 static void sil_dev_config(struct ata_port
*ap
, struct ata_device
*dev
);
82 static u32
sil_scr_read (struct ata_port
*ap
, unsigned int sc_reg
);
83 static void sil_scr_write (struct ata_port
*ap
, unsigned int sc_reg
, u32 val
);
84 static void sil_post_set_mode (struct ata_port
*ap
);
86 static struct pci_device_id sil_pci_tbl
[] = {
87 { 0x1095, 0x3112, PCI_ANY_ID
, PCI_ANY_ID
, 0, 0, sil_3112_m15w
},
88 { 0x1095, 0x0240, PCI_ANY_ID
, PCI_ANY_ID
, 0, 0, sil_3112_m15w
},
89 { 0x1095, 0x3512, PCI_ANY_ID
, PCI_ANY_ID
, 0, 0, sil_3112
},
90 { 0x1095, 0x3114, PCI_ANY_ID
, PCI_ANY_ID
, 0, 0, sil_3114
},
91 { 0x1002, 0x436e, PCI_ANY_ID
, PCI_ANY_ID
, 0, 0, sil_3112_m15w
},
92 { 0x1002, 0x4379, PCI_ANY_ID
, PCI_ANY_ID
, 0, 0, sil_3112_m15w
},
93 { 0x1002, 0x437a, PCI_ANY_ID
, PCI_ANY_ID
, 0, 0, sil_3112_m15w
},
94 { } /* terminate list */
98 /* TODO firmware versions should be added - eric */
99 static const struct sil_drivelist
{
100 const char * product
;
102 } sil_blacklist
[] = {
103 { "ST320012AS", SIL_QUIRK_MOD15WRITE
},
104 { "ST330013AS", SIL_QUIRK_MOD15WRITE
},
105 { "ST340017AS", SIL_QUIRK_MOD15WRITE
},
106 { "ST360015AS", SIL_QUIRK_MOD15WRITE
},
107 { "ST380013AS", SIL_QUIRK_MOD15WRITE
},
108 { "ST380023AS", SIL_QUIRK_MOD15WRITE
},
109 { "ST3120023AS", SIL_QUIRK_MOD15WRITE
},
110 { "ST3160023AS", SIL_QUIRK_MOD15WRITE
},
111 { "ST3120026AS", SIL_QUIRK_MOD15WRITE
},
112 { "ST3200822AS", SIL_QUIRK_MOD15WRITE
},
113 { "ST340014ASL", SIL_QUIRK_MOD15WRITE
},
114 { "ST360014ASL", SIL_QUIRK_MOD15WRITE
},
115 { "ST380011ASL", SIL_QUIRK_MOD15WRITE
},
116 { "ST3120022ASL", SIL_QUIRK_MOD15WRITE
},
117 { "ST3160021ASL", SIL_QUIRK_MOD15WRITE
},
118 { "Maxtor 4D060H3", SIL_QUIRK_UDMA5MAX
},
122 static struct pci_driver sil_pci_driver
= {
124 .id_table
= sil_pci_tbl
,
125 .probe
= sil_init_one
,
126 .remove
= ata_pci_remove_one
,
129 static Scsi_Host_Template sil_sht
= {
130 .module
= THIS_MODULE
,
132 .ioctl
= ata_scsi_ioctl
,
133 .queuecommand
= ata_scsi_queuecmd
,
134 .eh_strategy_handler
= ata_scsi_error
,
135 .can_queue
= ATA_DEF_QUEUE
,
136 .this_id
= ATA_SHT_THIS_ID
,
137 .sg_tablesize
= LIBATA_MAX_PRD
,
138 .max_sectors
= ATA_MAX_SECTORS
,
139 .cmd_per_lun
= ATA_SHT_CMD_PER_LUN
,
140 .emulated
= ATA_SHT_EMULATED
,
141 .use_clustering
= ATA_SHT_USE_CLUSTERING
,
142 .proc_name
= DRV_NAME
,
143 .dma_boundary
= ATA_DMA_BOUNDARY
,
144 .slave_configure
= ata_scsi_slave_config
,
145 .bios_param
= ata_std_bios_param
,
149 static struct ata_port_operations sil_ops
= {
150 .port_disable
= ata_port_disable
,
151 .dev_config
= sil_dev_config
,
152 .tf_load
= ata_tf_load
,
153 .tf_read
= ata_tf_read
,
154 .check_status
= ata_check_status
,
155 .exec_command
= ata_exec_command
,
156 .dev_select
= ata_std_dev_select
,
157 .phy_reset
= sata_phy_reset
,
158 .post_set_mode
= sil_post_set_mode
,
159 .bmdma_setup
= ata_bmdma_setup
,
160 .bmdma_start
= ata_bmdma_start
,
161 .bmdma_stop
= ata_bmdma_stop
,
162 .bmdma_status
= ata_bmdma_status
,
163 .qc_prep
= ata_qc_prep
,
164 .qc_issue
= ata_qc_issue_prot
,
165 .eng_timeout
= ata_eng_timeout
,
166 .irq_handler
= ata_interrupt
,
167 .irq_clear
= ata_bmdma_irq_clear
,
168 .scr_read
= sil_scr_read
,
169 .scr_write
= sil_scr_write
,
170 .port_start
= ata_port_start
,
171 .port_stop
= ata_port_stop
,
172 .host_stop
= ata_host_stop
,
175 static struct ata_port_info sil_port_info
[] = {
179 .host_flags
= ATA_FLAG_SATA
| ATA_FLAG_NO_LEGACY
|
180 ATA_FLAG_SRST
| ATA_FLAG_MMIO
,
181 .pio_mask
= 0x1f, /* pio0-4 */
182 .mwdma_mask
= 0x07, /* mwdma0-2 */
183 .udma_mask
= 0x3f, /* udma0-5 */
184 .port_ops
= &sil_ops
,
185 }, /* sil_3112_15w - keep it sync'd w/ sil_3112 */
188 .host_flags
= ATA_FLAG_SATA
| ATA_FLAG_NO_LEGACY
|
189 ATA_FLAG_SRST
| ATA_FLAG_MMIO
|
191 .pio_mask
= 0x1f, /* pio0-4 */
192 .mwdma_mask
= 0x07, /* mwdma0-2 */
193 .udma_mask
= 0x3f, /* udma0-5 */
194 .port_ops
= &sil_ops
,
198 .host_flags
= ATA_FLAG_SATA
| ATA_FLAG_NO_LEGACY
|
199 ATA_FLAG_SRST
| ATA_FLAG_MMIO
,
200 .pio_mask
= 0x1f, /* pio0-4 */
201 .mwdma_mask
= 0x07, /* mwdma0-2 */
202 .udma_mask
= 0x3f, /* udma0-5 */
203 .port_ops
= &sil_ops
,
207 /* per-port register offsets */
208 /* TODO: we can probably calculate rather than use a table */
209 static const struct {
210 unsigned long tf
; /* ATA taskfile register block */
211 unsigned long ctl
; /* ATA control/altstatus register block */
212 unsigned long bmdma
; /* DMA register block */
213 unsigned long scr
; /* SATA control register block */
214 unsigned long sien
; /* SATA Interrupt Enable register */
215 unsigned long xfer_mode
;/* data transfer mode register */
218 { 0x80, 0x8A, 0x00, 0x100, 0x148, 0xb4 },
219 { 0xC0, 0xCA, 0x08, 0x180, 0x1c8, 0xf4 },
220 { 0x280, 0x28A, 0x200, 0x300, 0x348, 0x2b4 },
221 { 0x2C0, 0x2CA, 0x208, 0x380, 0x3c8, 0x2f4 },
225 MODULE_AUTHOR("Jeff Garzik");
226 MODULE_DESCRIPTION("low-level driver for Silicon Image SATA controller");
227 MODULE_LICENSE("GPL");
228 MODULE_DEVICE_TABLE(pci
, sil_pci_tbl
);
229 MODULE_VERSION(DRV_VERSION
);
231 static unsigned char sil_get_device_cache_line(struct pci_dev
*pdev
)
234 pci_read_config_byte(pdev
, PCI_CACHE_LINE_SIZE
, &cache_line
);
238 static void sil_post_set_mode (struct ata_port
*ap
)
240 struct ata_host_set
*host_set
= ap
->host_set
;
241 struct ata_device
*dev
;
242 void *addr
= host_set
->mmio_base
+ sil_port
[ap
->port_no
].xfer_mode
;
243 u32 tmp
, dev_mode
[2];
246 for (i
= 0; i
< 2; i
++) {
247 dev
= &ap
->device
[i
];
248 if (!ata_dev_present(dev
))
249 dev_mode
[i
] = 0; /* PIO0/1/2 */
250 else if (dev
->flags
& ATA_DFLAG_PIO
)
251 dev_mode
[i
] = 1; /* PIO3/4 */
253 dev_mode
[i
] = 3; /* UDMA */
254 /* value 2 indicates MDMA */
258 tmp
&= ~((1<<5) | (1<<4) | (1<<1) | (1<<0));
260 tmp
|= (dev_mode
[1] << 4);
262 readl(addr
); /* flush */
265 static inline unsigned long sil_scr_addr(struct ata_port
*ap
, unsigned int sc_reg
)
267 unsigned long offset
= ap
->ioaddr
.scr_addr
;
284 static u32
sil_scr_read (struct ata_port
*ap
, unsigned int sc_reg
)
286 void *mmio
= (void *) sil_scr_addr(ap
, sc_reg
);
292 static void sil_scr_write (struct ata_port
*ap
, unsigned int sc_reg
, u32 val
)
294 void *mmio
= (void *) sil_scr_addr(ap
, sc_reg
);
300 * sil_dev_config - Apply device/host-specific errata fixups
301 * @ap: Port containing device to be examined
302 * @dev: Device to be examined
304 * After the IDENTIFY [PACKET] DEVICE step is complete, and a
305 * device is known to be present, this function is called.
306 * We apply two errata fixups which are specific to Silicon Image,
307 * a Seagate and a Maxtor fixup.
309 * For certain Seagate devices, we must limit the maximum sectors
312 * For certain Maxtor devices, we must not program the drive
315 * Both fixups are unfairly pessimistic. As soon as I get more
316 * information on these errata, I will create a more exhaustive
317 * list, and apply the fixups to only the specific
318 * devices/hosts/firmwares that need it.
320 * 20040111 - Seagate drives affected by the Mod15Write bug are blacklisted
321 * The Maxtor quirk is in the blacklist, but I'm keeping the original
322 * pessimistic fix for the following reasons...
323 * - There seems to be less info on it, only one device gleaned off the
324 * Windows driver, maybe only one is affected. More info would be greatly
326 * - But then again UDMA5 is hardly anything to complain about
328 static void sil_dev_config(struct ata_port
*ap
, struct ata_device
*dev
)
330 unsigned int n
, quirks
= 0;
331 unsigned char model_num
[40];
335 ata_dev_id_string(dev
->id
, model_num
, ATA_ID_PROD_OFS
,
338 len
= strnlen(s
, sizeof(model_num
));
340 /* ATAPI specifies that empty space is blank-filled; remove blanks */
341 while ((len
> 0) && (s
[len
- 1] == ' '))
344 for (n
= 0; sil_blacklist
[n
].product
; n
++)
345 if (!memcmp(sil_blacklist
[n
].product
, s
,
346 strlen(sil_blacklist
[n
].product
))) {
347 quirks
= sil_blacklist
[n
].quirk
;
351 /* limit requests to 15 sectors */
352 if ((ap
->flags
& SIL_FLAG_MOD15WRITE
) && (quirks
& SIL_QUIRK_MOD15WRITE
)) {
353 printk(KERN_INFO
"ata%u(%u): applying Seagate errata fix\n",
355 ap
->host
->max_sectors
= 15;
356 ap
->host
->hostt
->max_sectors
= 15;
357 dev
->flags
|= ATA_DFLAG_LOCK_SECTORS
;
362 if (quirks
& SIL_QUIRK_UDMA5MAX
) {
363 printk(KERN_INFO
"ata%u(%u): applying Maxtor errata fix %s\n",
364 ap
->id
, dev
->devno
, s
);
365 ap
->udma_mask
&= ATA_UDMA5
;
370 static int sil_init_one (struct pci_dev
*pdev
, const struct pci_device_id
*ent
)
372 static int printed_version
;
373 struct ata_probe_ent
*probe_ent
= NULL
;
378 int pci_dev_busy
= 0;
382 if (!printed_version
++)
383 printk(KERN_DEBUG DRV_NAME
" version " DRV_VERSION
"\n");
386 * If this driver happens to only be useful on Apple's K2, then
387 * we should check that here as it has a normal Serverworks ID
389 rc
= pci_enable_device(pdev
);
393 rc
= pci_request_regions(pdev
, DRV_NAME
);
399 rc
= pci_set_dma_mask(pdev
, ATA_DMA_MASK
);
401 goto err_out_regions
;
402 rc
= pci_set_consistent_dma_mask(pdev
, ATA_DMA_MASK
);
404 goto err_out_regions
;
406 probe_ent
= kmalloc(sizeof(*probe_ent
), GFP_KERNEL
);
407 if (probe_ent
== NULL
) {
409 goto err_out_regions
;
412 memset(probe_ent
, 0, sizeof(*probe_ent
));
413 INIT_LIST_HEAD(&probe_ent
->node
);
414 probe_ent
->dev
= pci_dev_to_dev(pdev
);
415 probe_ent
->port_ops
= sil_port_info
[ent
->driver_data
].port_ops
;
416 probe_ent
->sht
= sil_port_info
[ent
->driver_data
].sht
;
417 probe_ent
->n_ports
= (ent
->driver_data
== sil_3114
) ? 4 : 2;
418 probe_ent
->pio_mask
= sil_port_info
[ent
->driver_data
].pio_mask
;
419 probe_ent
->mwdma_mask
= sil_port_info
[ent
->driver_data
].mwdma_mask
;
420 probe_ent
->udma_mask
= sil_port_info
[ent
->driver_data
].udma_mask
;
421 probe_ent
->irq
= pdev
->irq
;
422 probe_ent
->irq_flags
= SA_SHIRQ
;
423 probe_ent
->host_flags
= sil_port_info
[ent
->driver_data
].host_flags
;
425 mmio_base
= ioremap(pci_resource_start(pdev
, 5),
426 pci_resource_len(pdev
, 5));
427 if (mmio_base
== NULL
) {
429 goto err_out_free_ent
;
432 probe_ent
->mmio_base
= mmio_base
;
434 base
= (unsigned long) mmio_base
;
436 for (i
= 0; i
< probe_ent
->n_ports
; i
++) {
437 probe_ent
->port
[i
].cmd_addr
= base
+ sil_port
[i
].tf
;
438 probe_ent
->port
[i
].altstatus_addr
=
439 probe_ent
->port
[i
].ctl_addr
= base
+ sil_port
[i
].ctl
;
440 probe_ent
->port
[i
].bmdma_addr
= base
+ sil_port
[i
].bmdma
;
441 probe_ent
->port
[i
].scr_addr
= base
+ sil_port
[i
].scr
;
442 ata_std_ports(&probe_ent
->port
[i
]);
445 /* Initialize FIFO PCI bus arbitration */
446 cls
= sil_get_device_cache_line(pdev
);
449 cls
++; /* cls = (line_size/8)+1 */
450 writeb(cls
, mmio_base
+ SIL_FIFO_R0
);
451 writeb(cls
, mmio_base
+ SIL_FIFO_W0
);
452 writeb(cls
, mmio_base
+ SIL_FIFO_R1
);
453 writeb(cls
, mmio_base
+ SIL_FIFO_W1
);
454 if (ent
->driver_data
== sil_3114
) {
455 writeb(cls
, mmio_base
+ SIL_FIFO_R2
);
456 writeb(cls
, mmio_base
+ SIL_FIFO_W2
);
457 writeb(cls
, mmio_base
+ SIL_FIFO_R3
);
458 writeb(cls
, mmio_base
+ SIL_FIFO_W3
);
461 printk(KERN_WARNING DRV_NAME
"(%s): cache line size not set. Driver may not function\n",
464 if (ent
->driver_data
== sil_3114
) {
465 irq_mask
= SIL_MASK_4PORT
;
467 /* flip the magic "make 4 ports work" bit */
468 tmp
= readl(mmio_base
+ SIL_IDE2_BMDMA
);
469 if ((tmp
& SIL_INTR_STEERING
) == 0)
470 writel(tmp
| SIL_INTR_STEERING
,
471 mmio_base
+ SIL_IDE2_BMDMA
);
474 irq_mask
= SIL_MASK_2PORT
;
477 /* make sure IDE0/1/2/3 interrupts are not masked */
478 tmp
= readl(mmio_base
+ SIL_SYSCFG
);
479 if (tmp
& irq_mask
) {
481 writel(tmp
, mmio_base
+ SIL_SYSCFG
);
482 readl(mmio_base
+ SIL_SYSCFG
); /* flush */
485 /* mask all SATA phy-related interrupts */
486 /* TODO: unmask bit 6 (SError N bit) for hotplug */
487 for (i
= 0; i
< probe_ent
->n_ports
; i
++)
488 writel(0, mmio_base
+ sil_port
[i
].sien
);
490 pci_set_master(pdev
);
492 /* FIXME: check ata_device_add return value */
493 ata_device_add(probe_ent
);
501 pci_release_regions(pdev
);
504 pci_disable_device(pdev
);
508 static int __init
sil_init(void)
510 return pci_module_init(&sil_pci_driver
);
513 static void __exit
sil_exit(void)
515 pci_unregister_driver(&sil_pci_driver
);
519 module_init(sil_init
);
520 module_exit(sil_exit
);