xref: /linux/drivers/ata/sata_via.c (revision 96bc103f4c4e470d82ba5e372191d02ad715da45)
1c6fd2807SJeff Garzik /*
2c6fd2807SJeff Garzik  *  sata_via.c - VIA Serial ATA controllers
3c6fd2807SJeff Garzik  *
4c6fd2807SJeff Garzik  *  Maintained by:  Jeff Garzik <jgarzik@pobox.com>
5c6fd2807SJeff Garzik  * 		   Please ALWAYS copy linux-ide@vger.kernel.org
6c6fd2807SJeff Garzik  		   on emails.
7c6fd2807SJeff Garzik  *
8c6fd2807SJeff Garzik  *  Copyright 2003-2004 Red Hat, Inc.  All rights reserved.
9c6fd2807SJeff Garzik  *  Copyright 2003-2004 Jeff Garzik
10c6fd2807SJeff Garzik  *
11c6fd2807SJeff Garzik  *
12c6fd2807SJeff Garzik  *  This program is free software; you can redistribute it and/or modify
13c6fd2807SJeff Garzik  *  it under the terms of the GNU General Public License as published by
14c6fd2807SJeff Garzik  *  the Free Software Foundation; either version 2, or (at your option)
15c6fd2807SJeff Garzik  *  any later version.
16c6fd2807SJeff Garzik  *
17c6fd2807SJeff Garzik  *  This program is distributed in the hope that it will be useful,
18c6fd2807SJeff Garzik  *  but WITHOUT ANY WARRANTY; without even the implied warranty of
19c6fd2807SJeff Garzik  *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
20c6fd2807SJeff Garzik  *  GNU General Public License for more details.
21c6fd2807SJeff Garzik  *
22c6fd2807SJeff Garzik  *  You should have received a copy of the GNU General Public License
23c6fd2807SJeff Garzik  *  along with this program; see the file COPYING.  If not, write to
24c6fd2807SJeff Garzik  *  the Free Software Foundation, 675 Mass Ave, Cambridge, MA 02139, USA.
25c6fd2807SJeff Garzik  *
26c6fd2807SJeff Garzik  *
27c6fd2807SJeff Garzik  *  libata documentation is available via 'make {ps|pdf}docs',
28c6fd2807SJeff Garzik  *  as Documentation/DocBook/libata.*
29c6fd2807SJeff Garzik  *
30c6fd2807SJeff Garzik  *  Hardware documentation available under NDA.
31c6fd2807SJeff Garzik  *
32c6fd2807SJeff Garzik  *
33c6fd2807SJeff Garzik  *  To-do list:
34c6fd2807SJeff Garzik  *  - VT6421 PATA support
35c6fd2807SJeff Garzik  *
36c6fd2807SJeff Garzik  */
37c6fd2807SJeff Garzik 
38c6fd2807SJeff Garzik #include <linux/kernel.h>
39c6fd2807SJeff Garzik #include <linux/module.h>
40c6fd2807SJeff Garzik #include <linux/pci.h>
41c6fd2807SJeff Garzik #include <linux/init.h>
42c6fd2807SJeff Garzik #include <linux/blkdev.h>
43c6fd2807SJeff Garzik #include <linux/delay.h>
44c6fd2807SJeff Garzik #include <linux/device.h>
45c6fd2807SJeff Garzik #include <scsi/scsi_host.h>
46c6fd2807SJeff Garzik #include <linux/libata.h>
47c6fd2807SJeff Garzik #include <asm/io.h>
48c6fd2807SJeff Garzik 
49c6fd2807SJeff Garzik #define DRV_NAME	"sata_via"
50c6fd2807SJeff Garzik #define DRV_VERSION	"2.0"
51c6fd2807SJeff Garzik 
52c6fd2807SJeff Garzik enum board_ids_enum {
53c6fd2807SJeff Garzik 	vt6420,
54c6fd2807SJeff Garzik 	vt6421,
55c6fd2807SJeff Garzik };
56c6fd2807SJeff Garzik 
57c6fd2807SJeff Garzik enum {
58c6fd2807SJeff Garzik 	SATA_CHAN_ENAB		= 0x40, /* SATA channel enable */
59c6fd2807SJeff Garzik 	SATA_INT_GATE		= 0x41, /* SATA interrupt gating */
60c6fd2807SJeff Garzik 	SATA_NATIVE_MODE	= 0x42, /* Native mode enable */
61c6fd2807SJeff Garzik 	SATA_PATA_SHARING	= 0x49, /* PATA/SATA sharing func ctrl */
62c6fd2807SJeff Garzik 
63c6fd2807SJeff Garzik 	PORT0			= (1 << 1),
64c6fd2807SJeff Garzik 	PORT1			= (1 << 0),
65c6fd2807SJeff Garzik 	ALL_PORTS		= PORT0 | PORT1,
66c6fd2807SJeff Garzik 	N_PORTS			= 2,
67c6fd2807SJeff Garzik 
68c6fd2807SJeff Garzik 	NATIVE_MODE_ALL		= (1 << 7) | (1 << 6) | (1 << 5) | (1 << 4),
69c6fd2807SJeff Garzik 
70c6fd2807SJeff Garzik 	SATA_EXT_PHY		= (1 << 6), /* 0==use PATA, 1==ext phy */
71c6fd2807SJeff Garzik 	SATA_2DEV		= (1 << 5), /* SATA is master/slave */
72c6fd2807SJeff Garzik };
73c6fd2807SJeff Garzik 
74c6fd2807SJeff Garzik static int svia_init_one (struct pci_dev *pdev, const struct pci_device_id *ent);
75c6fd2807SJeff Garzik static u32 svia_scr_read (struct ata_port *ap, unsigned int sc_reg);
76c6fd2807SJeff Garzik static void svia_scr_write (struct ata_port *ap, unsigned int sc_reg, u32 val);
7754a86bfcSJeff Garzik static void vt6420_error_handler(struct ata_port *ap);
78c6fd2807SJeff Garzik 
79c6fd2807SJeff Garzik static const struct pci_device_id svia_pci_tbl[] = {
80*96bc103fSLuca Pedrielli 	{ PCI_VDEVICE(VIA, 0x5337), vt6420 },
812d2744fcSJeff Garzik 	{ PCI_VDEVICE(VIA, 0x0591), vt6420 },
822d2744fcSJeff Garzik 	{ PCI_VDEVICE(VIA, 0x3149), vt6420 },
832d2744fcSJeff Garzik 	{ PCI_VDEVICE(VIA, 0x3249), vt6421 },
84c6fd2807SJeff Garzik 
85c6fd2807SJeff Garzik 	{ }	/* terminate list */
86c6fd2807SJeff Garzik };
87c6fd2807SJeff Garzik 
88c6fd2807SJeff Garzik static struct pci_driver svia_pci_driver = {
89c6fd2807SJeff Garzik 	.name			= DRV_NAME,
90c6fd2807SJeff Garzik 	.id_table		= svia_pci_tbl,
91c6fd2807SJeff Garzik 	.probe			= svia_init_one,
92c6fd2807SJeff Garzik 	.remove			= ata_pci_remove_one,
93c6fd2807SJeff Garzik };
94c6fd2807SJeff Garzik 
95c6fd2807SJeff Garzik static struct scsi_host_template svia_sht = {
96c6fd2807SJeff Garzik 	.module			= THIS_MODULE,
97c6fd2807SJeff Garzik 	.name			= DRV_NAME,
98c6fd2807SJeff Garzik 	.ioctl			= ata_scsi_ioctl,
99c6fd2807SJeff Garzik 	.queuecommand		= ata_scsi_queuecmd,
100c6fd2807SJeff Garzik 	.can_queue		= ATA_DEF_QUEUE,
101c6fd2807SJeff Garzik 	.this_id		= ATA_SHT_THIS_ID,
102c6fd2807SJeff Garzik 	.sg_tablesize		= LIBATA_MAX_PRD,
103c6fd2807SJeff Garzik 	.cmd_per_lun		= ATA_SHT_CMD_PER_LUN,
104c6fd2807SJeff Garzik 	.emulated		= ATA_SHT_EMULATED,
105c6fd2807SJeff Garzik 	.use_clustering		= ATA_SHT_USE_CLUSTERING,
106c6fd2807SJeff Garzik 	.proc_name		= DRV_NAME,
107c6fd2807SJeff Garzik 	.dma_boundary		= ATA_DMA_BOUNDARY,
108c6fd2807SJeff Garzik 	.slave_configure	= ata_scsi_slave_config,
109c6fd2807SJeff Garzik 	.slave_destroy		= ata_scsi_slave_destroy,
110c6fd2807SJeff Garzik 	.bios_param		= ata_std_bios_param,
111c6fd2807SJeff Garzik };
112c6fd2807SJeff Garzik 
11354a86bfcSJeff Garzik static const struct ata_port_operations vt6420_sata_ops = {
11454a86bfcSJeff Garzik 	.port_disable		= ata_port_disable,
11554a86bfcSJeff Garzik 
11654a86bfcSJeff Garzik 	.tf_load		= ata_tf_load,
11754a86bfcSJeff Garzik 	.tf_read		= ata_tf_read,
11854a86bfcSJeff Garzik 	.check_status		= ata_check_status,
11954a86bfcSJeff Garzik 	.exec_command		= ata_exec_command,
12054a86bfcSJeff Garzik 	.dev_select		= ata_std_dev_select,
12154a86bfcSJeff Garzik 
12254a86bfcSJeff Garzik 	.bmdma_setup            = ata_bmdma_setup,
12354a86bfcSJeff Garzik 	.bmdma_start            = ata_bmdma_start,
12454a86bfcSJeff Garzik 	.bmdma_stop		= ata_bmdma_stop,
12554a86bfcSJeff Garzik 	.bmdma_status		= ata_bmdma_status,
12654a86bfcSJeff Garzik 
12754a86bfcSJeff Garzik 	.qc_prep		= ata_qc_prep,
12854a86bfcSJeff Garzik 	.qc_issue		= ata_qc_issue_prot,
12954a86bfcSJeff Garzik 	.data_xfer		= ata_pio_data_xfer,
13054a86bfcSJeff Garzik 
13154a86bfcSJeff Garzik 	.freeze			= ata_bmdma_freeze,
13254a86bfcSJeff Garzik 	.thaw			= ata_bmdma_thaw,
13354a86bfcSJeff Garzik 	.error_handler		= vt6420_error_handler,
13454a86bfcSJeff Garzik 	.post_internal_cmd	= ata_bmdma_post_internal_cmd,
13554a86bfcSJeff Garzik 
13654a86bfcSJeff Garzik 	.irq_handler		= ata_interrupt,
13754a86bfcSJeff Garzik 	.irq_clear		= ata_bmdma_irq_clear,
13854a86bfcSJeff Garzik 
13954a86bfcSJeff Garzik 	.port_start		= ata_port_start,
14054a86bfcSJeff Garzik 	.port_stop		= ata_port_stop,
14154a86bfcSJeff Garzik 	.host_stop		= ata_host_stop,
14254a86bfcSJeff Garzik };
14354a86bfcSJeff Garzik 
14454a86bfcSJeff Garzik static const struct ata_port_operations vt6421_sata_ops = {
145c6fd2807SJeff Garzik 	.port_disable		= ata_port_disable,
146c6fd2807SJeff Garzik 
147c6fd2807SJeff Garzik 	.tf_load		= ata_tf_load,
148c6fd2807SJeff Garzik 	.tf_read		= ata_tf_read,
149c6fd2807SJeff Garzik 	.check_status		= ata_check_status,
150c6fd2807SJeff Garzik 	.exec_command		= ata_exec_command,
151c6fd2807SJeff Garzik 	.dev_select		= ata_std_dev_select,
152c6fd2807SJeff Garzik 
153c6fd2807SJeff Garzik 	.bmdma_setup            = ata_bmdma_setup,
154c6fd2807SJeff Garzik 	.bmdma_start            = ata_bmdma_start,
155c6fd2807SJeff Garzik 	.bmdma_stop		= ata_bmdma_stop,
156c6fd2807SJeff Garzik 	.bmdma_status		= ata_bmdma_status,
157c6fd2807SJeff Garzik 
158c6fd2807SJeff Garzik 	.qc_prep		= ata_qc_prep,
159c6fd2807SJeff Garzik 	.qc_issue		= ata_qc_issue_prot,
160c6fd2807SJeff Garzik 	.data_xfer		= ata_pio_data_xfer,
161c6fd2807SJeff Garzik 
162c6fd2807SJeff Garzik 	.freeze			= ata_bmdma_freeze,
163c6fd2807SJeff Garzik 	.thaw			= ata_bmdma_thaw,
164c6fd2807SJeff Garzik 	.error_handler		= ata_bmdma_error_handler,
165c6fd2807SJeff Garzik 	.post_internal_cmd	= ata_bmdma_post_internal_cmd,
166c6fd2807SJeff Garzik 
167c6fd2807SJeff Garzik 	.irq_handler		= ata_interrupt,
168c6fd2807SJeff Garzik 	.irq_clear		= ata_bmdma_irq_clear,
169c6fd2807SJeff Garzik 
170c6fd2807SJeff Garzik 	.scr_read		= svia_scr_read,
171c6fd2807SJeff Garzik 	.scr_write		= svia_scr_write,
172c6fd2807SJeff Garzik 
173c6fd2807SJeff Garzik 	.port_start		= ata_port_start,
174c6fd2807SJeff Garzik 	.port_stop		= ata_port_stop,
175c6fd2807SJeff Garzik 	.host_stop		= ata_host_stop,
176c6fd2807SJeff Garzik };
177c6fd2807SJeff Garzik 
17854a86bfcSJeff Garzik static struct ata_port_info vt6420_port_info = {
179c6fd2807SJeff Garzik 	.sht		= &svia_sht,
180cca3974eSJeff Garzik 	.flags		= ATA_FLAG_SATA | ATA_FLAG_NO_LEGACY,
181c6fd2807SJeff Garzik 	.pio_mask	= 0x1f,
182c6fd2807SJeff Garzik 	.mwdma_mask	= 0x07,
183c6fd2807SJeff Garzik 	.udma_mask	= 0x7f,
18454a86bfcSJeff Garzik 	.port_ops	= &vt6420_sata_ops,
185c6fd2807SJeff Garzik };
186c6fd2807SJeff Garzik 
187c6fd2807SJeff Garzik MODULE_AUTHOR("Jeff Garzik");
188c6fd2807SJeff Garzik MODULE_DESCRIPTION("SCSI low-level driver for VIA SATA controllers");
189c6fd2807SJeff Garzik MODULE_LICENSE("GPL");
190c6fd2807SJeff Garzik MODULE_DEVICE_TABLE(pci, svia_pci_tbl);
191c6fd2807SJeff Garzik MODULE_VERSION(DRV_VERSION);
192c6fd2807SJeff Garzik 
193c6fd2807SJeff Garzik static u32 svia_scr_read (struct ata_port *ap, unsigned int sc_reg)
194c6fd2807SJeff Garzik {
195c6fd2807SJeff Garzik 	if (sc_reg > SCR_CONTROL)
196c6fd2807SJeff Garzik 		return 0xffffffffU;
197c6fd2807SJeff Garzik 	return inl(ap->ioaddr.scr_addr + (4 * sc_reg));
198c6fd2807SJeff Garzik }
199c6fd2807SJeff Garzik 
200c6fd2807SJeff Garzik static void svia_scr_write (struct ata_port *ap, unsigned int sc_reg, u32 val)
201c6fd2807SJeff Garzik {
202c6fd2807SJeff Garzik 	if (sc_reg > SCR_CONTROL)
203c6fd2807SJeff Garzik 		return;
204c6fd2807SJeff Garzik 	outl(val, ap->ioaddr.scr_addr + (4 * sc_reg));
205c6fd2807SJeff Garzik }
206c6fd2807SJeff Garzik 
20754a86bfcSJeff Garzik /**
20854a86bfcSJeff Garzik  *	vt6420_prereset - prereset for vt6420
20954a86bfcSJeff Garzik  *	@ap: target ATA port
21054a86bfcSJeff Garzik  *
21154a86bfcSJeff Garzik  *	SCR registers on vt6420 are pieces of shit and may hang the
21254a86bfcSJeff Garzik  *	whole machine completely if accessed with the wrong timing.
21354a86bfcSJeff Garzik  *	To avoid such catastrophe, vt6420 doesn't provide generic SCR
21454a86bfcSJeff Garzik  *	access operations, but uses SStatus and SControl only during
21554a86bfcSJeff Garzik  *	boot probing in controlled way.
21654a86bfcSJeff Garzik  *
21754a86bfcSJeff Garzik  *	As the old (pre EH update) probing code is proven to work, we
21854a86bfcSJeff Garzik  *	strictly follow the access pattern.
21954a86bfcSJeff Garzik  *
22054a86bfcSJeff Garzik  *	LOCKING:
22154a86bfcSJeff Garzik  *	Kernel thread context (may sleep)
22254a86bfcSJeff Garzik  *
22354a86bfcSJeff Garzik  *	RETURNS:
22454a86bfcSJeff Garzik  *	0 on success, -errno otherwise.
22554a86bfcSJeff Garzik  */
22654a86bfcSJeff Garzik static int vt6420_prereset(struct ata_port *ap)
22754a86bfcSJeff Garzik {
22854a86bfcSJeff Garzik 	struct ata_eh_context *ehc = &ap->eh_context;
22954a86bfcSJeff Garzik 	unsigned long timeout = jiffies + (HZ * 5);
23054a86bfcSJeff Garzik 	u32 sstatus, scontrol;
23154a86bfcSJeff Garzik 	int online;
23254a86bfcSJeff Garzik 
23354a86bfcSJeff Garzik 	/* don't do any SCR stuff if we're not loading */
23468ff6e8eSJeff Garzik 	if (!(ap->pflags & ATA_PFLAG_LOADING))
23554a86bfcSJeff Garzik 		goto skip_scr;
23654a86bfcSJeff Garzik 
23754a86bfcSJeff Garzik 	/* Resume phy.  This is the old resume sequence from
23854a86bfcSJeff Garzik 	 * __sata_phy_reset().
23954a86bfcSJeff Garzik 	 */
24054a86bfcSJeff Garzik 	svia_scr_write(ap, SCR_CONTROL, 0x300);
24154a86bfcSJeff Garzik 	svia_scr_read(ap, SCR_CONTROL); /* flush */
24254a86bfcSJeff Garzik 
24354a86bfcSJeff Garzik 	/* wait for phy to become ready, if necessary */
24454a86bfcSJeff Garzik 	do {
24554a86bfcSJeff Garzik 		msleep(200);
24654a86bfcSJeff Garzik 		if ((svia_scr_read(ap, SCR_STATUS) & 0xf) != 1)
24754a86bfcSJeff Garzik 			break;
24854a86bfcSJeff Garzik 	} while (time_before(jiffies, timeout));
24954a86bfcSJeff Garzik 
25054a86bfcSJeff Garzik 	/* open code sata_print_link_status() */
25154a86bfcSJeff Garzik 	sstatus = svia_scr_read(ap, SCR_STATUS);
25254a86bfcSJeff Garzik 	scontrol = svia_scr_read(ap, SCR_CONTROL);
25354a86bfcSJeff Garzik 
25454a86bfcSJeff Garzik 	online = (sstatus & 0xf) == 0x3;
25554a86bfcSJeff Garzik 
25654a86bfcSJeff Garzik 	ata_port_printk(ap, KERN_INFO,
25754a86bfcSJeff Garzik 			"SATA link %s 1.5 Gbps (SStatus %X SControl %X)\n",
25854a86bfcSJeff Garzik 			online ? "up" : "down", sstatus, scontrol);
25954a86bfcSJeff Garzik 
26054a86bfcSJeff Garzik 	/* SStatus is read one more time */
26154a86bfcSJeff Garzik 	svia_scr_read(ap, SCR_STATUS);
26254a86bfcSJeff Garzik 
26354a86bfcSJeff Garzik 	if (!online) {
26454a86bfcSJeff Garzik 		/* tell EH to bail */
26554a86bfcSJeff Garzik 		ehc->i.action &= ~ATA_EH_RESET_MASK;
26654a86bfcSJeff Garzik 		return 0;
26754a86bfcSJeff Garzik 	}
26854a86bfcSJeff Garzik 
26954a86bfcSJeff Garzik  skip_scr:
27054a86bfcSJeff Garzik 	/* wait for !BSY */
27154a86bfcSJeff Garzik 	ata_busy_sleep(ap, ATA_TMOUT_BOOT_QUICK, ATA_TMOUT_BOOT);
27254a86bfcSJeff Garzik 
27354a86bfcSJeff Garzik 	return 0;
27454a86bfcSJeff Garzik }
27554a86bfcSJeff Garzik 
27654a86bfcSJeff Garzik static void vt6420_error_handler(struct ata_port *ap)
27754a86bfcSJeff Garzik {
27854a86bfcSJeff Garzik 	return ata_bmdma_drive_eh(ap, vt6420_prereset, ata_std_softreset,
27954a86bfcSJeff Garzik 				  NULL, ata_std_postreset);
28054a86bfcSJeff Garzik }
28154a86bfcSJeff Garzik 
282c6fd2807SJeff Garzik static const unsigned int svia_bar_sizes[] = {
283c6fd2807SJeff Garzik 	8, 4, 8, 4, 16, 256
284c6fd2807SJeff Garzik };
285c6fd2807SJeff Garzik 
286c6fd2807SJeff Garzik static const unsigned int vt6421_bar_sizes[] = {
287c6fd2807SJeff Garzik 	16, 16, 16, 16, 32, 128
288c6fd2807SJeff Garzik };
289c6fd2807SJeff Garzik 
290c6fd2807SJeff Garzik static unsigned long svia_scr_addr(unsigned long addr, unsigned int port)
291c6fd2807SJeff Garzik {
292c6fd2807SJeff Garzik 	return addr + (port * 128);
293c6fd2807SJeff Garzik }
294c6fd2807SJeff Garzik 
295c6fd2807SJeff Garzik static unsigned long vt6421_scr_addr(unsigned long addr, unsigned int port)
296c6fd2807SJeff Garzik {
297c6fd2807SJeff Garzik 	return addr + (port * 64);
298c6fd2807SJeff Garzik }
299c6fd2807SJeff Garzik 
300c6fd2807SJeff Garzik static void vt6421_init_addrs(struct ata_probe_ent *probe_ent,
301c6fd2807SJeff Garzik 			      struct pci_dev *pdev,
302c6fd2807SJeff Garzik 			      unsigned int port)
303c6fd2807SJeff Garzik {
304c6fd2807SJeff Garzik 	unsigned long reg_addr = pci_resource_start(pdev, port);
305c6fd2807SJeff Garzik 	unsigned long bmdma_addr = pci_resource_start(pdev, 4) + (port * 8);
306c6fd2807SJeff Garzik 	unsigned long scr_addr;
307c6fd2807SJeff Garzik 
308c6fd2807SJeff Garzik 	probe_ent->port[port].cmd_addr = reg_addr;
309c6fd2807SJeff Garzik 	probe_ent->port[port].altstatus_addr =
310c6fd2807SJeff Garzik 	probe_ent->port[port].ctl_addr = (reg_addr + 8) | ATA_PCI_CTL_OFS;
311c6fd2807SJeff Garzik 	probe_ent->port[port].bmdma_addr = bmdma_addr;
312c6fd2807SJeff Garzik 
313c6fd2807SJeff Garzik 	scr_addr = vt6421_scr_addr(pci_resource_start(pdev, 5), port);
314c6fd2807SJeff Garzik 	probe_ent->port[port].scr_addr = scr_addr;
315c6fd2807SJeff Garzik 
316c6fd2807SJeff Garzik 	ata_std_ports(&probe_ent->port[port]);
317c6fd2807SJeff Garzik }
318c6fd2807SJeff Garzik 
319c6fd2807SJeff Garzik static struct ata_probe_ent *vt6420_init_probe_ent(struct pci_dev *pdev)
320c6fd2807SJeff Garzik {
321c6fd2807SJeff Garzik 	struct ata_probe_ent *probe_ent;
32229da9f6dSJeff Garzik 	struct ata_port_info *ppi[2];
323c6fd2807SJeff Garzik 
32429da9f6dSJeff Garzik 	ppi[0] = ppi[1] = &vt6420_port_info;
32529da9f6dSJeff Garzik 	probe_ent = ata_pci_init_native_mode(pdev, ppi, ATA_PORT_PRIMARY | ATA_PORT_SECONDARY);
326c6fd2807SJeff Garzik 	if (!probe_ent)
327c6fd2807SJeff Garzik 		return NULL;
328c6fd2807SJeff Garzik 
329c6fd2807SJeff Garzik 	probe_ent->port[0].scr_addr =
330c6fd2807SJeff Garzik 		svia_scr_addr(pci_resource_start(pdev, 5), 0);
331c6fd2807SJeff Garzik 	probe_ent->port[1].scr_addr =
332c6fd2807SJeff Garzik 		svia_scr_addr(pci_resource_start(pdev, 5), 1);
333c6fd2807SJeff Garzik 
334c6fd2807SJeff Garzik 	return probe_ent;
335c6fd2807SJeff Garzik }
336c6fd2807SJeff Garzik 
337c6fd2807SJeff Garzik static struct ata_probe_ent *vt6421_init_probe_ent(struct pci_dev *pdev)
338c6fd2807SJeff Garzik {
339c6fd2807SJeff Garzik 	struct ata_probe_ent *probe_ent;
340c6fd2807SJeff Garzik 	unsigned int i;
341c6fd2807SJeff Garzik 
342c6fd2807SJeff Garzik 	probe_ent = kmalloc(sizeof(*probe_ent), GFP_KERNEL);
343c6fd2807SJeff Garzik 	if (!probe_ent)
344c6fd2807SJeff Garzik 		return NULL;
345c6fd2807SJeff Garzik 
346c6fd2807SJeff Garzik 	memset(probe_ent, 0, sizeof(*probe_ent));
347c6fd2807SJeff Garzik 	probe_ent->dev = pci_dev_to_dev(pdev);
348c6fd2807SJeff Garzik 	INIT_LIST_HEAD(&probe_ent->node);
349c6fd2807SJeff Garzik 
350c6fd2807SJeff Garzik 	probe_ent->sht		= &svia_sht;
351cca3974eSJeff Garzik 	probe_ent->port_flags	= ATA_FLAG_SATA | ATA_FLAG_NO_LEGACY;
35254a86bfcSJeff Garzik 	probe_ent->port_ops	= &vt6421_sata_ops;
353c6fd2807SJeff Garzik 	probe_ent->n_ports	= N_PORTS;
354c6fd2807SJeff Garzik 	probe_ent->irq		= pdev->irq;
355c6fd2807SJeff Garzik 	probe_ent->irq_flags	= IRQF_SHARED;
356c6fd2807SJeff Garzik 	probe_ent->pio_mask	= 0x1f;
357c6fd2807SJeff Garzik 	probe_ent->mwdma_mask	= 0x07;
358c6fd2807SJeff Garzik 	probe_ent->udma_mask	= 0x7f;
359c6fd2807SJeff Garzik 
360c6fd2807SJeff Garzik 	for (i = 0; i < N_PORTS; i++)
361c6fd2807SJeff Garzik 		vt6421_init_addrs(probe_ent, pdev, i);
362c6fd2807SJeff Garzik 
363c6fd2807SJeff Garzik 	return probe_ent;
364c6fd2807SJeff Garzik }
365c6fd2807SJeff Garzik 
366c6fd2807SJeff Garzik static void svia_configure(struct pci_dev *pdev)
367c6fd2807SJeff Garzik {
368c6fd2807SJeff Garzik 	u8 tmp8;
369c6fd2807SJeff Garzik 
370c6fd2807SJeff Garzik 	pci_read_config_byte(pdev, PCI_INTERRUPT_LINE, &tmp8);
371c6fd2807SJeff Garzik 	dev_printk(KERN_INFO, &pdev->dev, "routed to hard irq line %d\n",
372c6fd2807SJeff Garzik 	       (int) (tmp8 & 0xf0) == 0xf0 ? 0 : tmp8 & 0x0f);
373c6fd2807SJeff Garzik 
374c6fd2807SJeff Garzik 	/* make sure SATA channels are enabled */
375c6fd2807SJeff Garzik 	pci_read_config_byte(pdev, SATA_CHAN_ENAB, &tmp8);
376c6fd2807SJeff Garzik 	if ((tmp8 & ALL_PORTS) != ALL_PORTS) {
377c6fd2807SJeff Garzik 		dev_printk(KERN_DEBUG, &pdev->dev,
378c6fd2807SJeff Garzik 			   "enabling SATA channels (0x%x)\n",
379c6fd2807SJeff Garzik 		           (int) tmp8);
380c6fd2807SJeff Garzik 		tmp8 |= ALL_PORTS;
381c6fd2807SJeff Garzik 		pci_write_config_byte(pdev, SATA_CHAN_ENAB, tmp8);
382c6fd2807SJeff Garzik 	}
383c6fd2807SJeff Garzik 
384c6fd2807SJeff Garzik 	/* make sure interrupts for each channel sent to us */
385c6fd2807SJeff Garzik 	pci_read_config_byte(pdev, SATA_INT_GATE, &tmp8);
386c6fd2807SJeff Garzik 	if ((tmp8 & ALL_PORTS) != ALL_PORTS) {
387c6fd2807SJeff Garzik 		dev_printk(KERN_DEBUG, &pdev->dev,
388c6fd2807SJeff Garzik 			   "enabling SATA channel interrupts (0x%x)\n",
389c6fd2807SJeff Garzik 		           (int) tmp8);
390c6fd2807SJeff Garzik 		tmp8 |= ALL_PORTS;
391c6fd2807SJeff Garzik 		pci_write_config_byte(pdev, SATA_INT_GATE, tmp8);
392c6fd2807SJeff Garzik 	}
393c6fd2807SJeff Garzik 
394c6fd2807SJeff Garzik 	/* make sure native mode is enabled */
395c6fd2807SJeff Garzik 	pci_read_config_byte(pdev, SATA_NATIVE_MODE, &tmp8);
396c6fd2807SJeff Garzik 	if ((tmp8 & NATIVE_MODE_ALL) != NATIVE_MODE_ALL) {
397c6fd2807SJeff Garzik 		dev_printk(KERN_DEBUG, &pdev->dev,
398c6fd2807SJeff Garzik 			   "enabling SATA channel native mode (0x%x)\n",
399c6fd2807SJeff Garzik 		           (int) tmp8);
400c6fd2807SJeff Garzik 		tmp8 |= NATIVE_MODE_ALL;
401c6fd2807SJeff Garzik 		pci_write_config_byte(pdev, SATA_NATIVE_MODE, tmp8);
402c6fd2807SJeff Garzik 	}
403c6fd2807SJeff Garzik }
404c6fd2807SJeff Garzik 
405c6fd2807SJeff Garzik static int svia_init_one (struct pci_dev *pdev, const struct pci_device_id *ent)
406c6fd2807SJeff Garzik {
407c6fd2807SJeff Garzik 	static int printed_version;
408c6fd2807SJeff Garzik 	unsigned int i;
409c6fd2807SJeff Garzik 	int rc;
410c6fd2807SJeff Garzik 	struct ata_probe_ent *probe_ent;
411c6fd2807SJeff Garzik 	int board_id = (int) ent->driver_data;
412c6fd2807SJeff Garzik 	const int *bar_sizes;
413c6fd2807SJeff Garzik 	int pci_dev_busy = 0;
414c6fd2807SJeff Garzik 	u8 tmp8;
415c6fd2807SJeff Garzik 
416c6fd2807SJeff Garzik 	if (!printed_version++)
417c6fd2807SJeff Garzik 		dev_printk(KERN_DEBUG, &pdev->dev, "version " DRV_VERSION "\n");
418c6fd2807SJeff Garzik 
419c6fd2807SJeff Garzik 	rc = pci_enable_device(pdev);
420c6fd2807SJeff Garzik 	if (rc)
421c6fd2807SJeff Garzik 		return rc;
422c6fd2807SJeff Garzik 
423c6fd2807SJeff Garzik 	rc = pci_request_regions(pdev, DRV_NAME);
424c6fd2807SJeff Garzik 	if (rc) {
425c6fd2807SJeff Garzik 		pci_dev_busy = 1;
426c6fd2807SJeff Garzik 		goto err_out;
427c6fd2807SJeff Garzik 	}
428c6fd2807SJeff Garzik 
429c6fd2807SJeff Garzik 	if (board_id == vt6420) {
430c6fd2807SJeff Garzik 		pci_read_config_byte(pdev, SATA_PATA_SHARING, &tmp8);
431c6fd2807SJeff Garzik 		if (tmp8 & SATA_2DEV) {
432c6fd2807SJeff Garzik 			dev_printk(KERN_ERR, &pdev->dev,
433c6fd2807SJeff Garzik 				   "SATA master/slave not supported (0x%x)\n",
434c6fd2807SJeff Garzik 		       		   (int) tmp8);
435c6fd2807SJeff Garzik 			rc = -EIO;
436c6fd2807SJeff Garzik 			goto err_out_regions;
437c6fd2807SJeff Garzik 		}
438c6fd2807SJeff Garzik 
439c6fd2807SJeff Garzik 		bar_sizes = &svia_bar_sizes[0];
440c6fd2807SJeff Garzik 	} else {
441c6fd2807SJeff Garzik 		bar_sizes = &vt6421_bar_sizes[0];
442c6fd2807SJeff Garzik 	}
443c6fd2807SJeff Garzik 
444c6fd2807SJeff Garzik 	for (i = 0; i < ARRAY_SIZE(svia_bar_sizes); i++)
445c6fd2807SJeff Garzik 		if ((pci_resource_start(pdev, i) == 0) ||
446c6fd2807SJeff Garzik 		    (pci_resource_len(pdev, i) < bar_sizes[i])) {
447c6fd2807SJeff Garzik 			dev_printk(KERN_ERR, &pdev->dev,
448c6fd2807SJeff Garzik 				"invalid PCI BAR %u (sz 0x%llx, val 0x%llx)\n",
449c6fd2807SJeff Garzik 				i,
450c6fd2807SJeff Garzik 			        (unsigned long long)pci_resource_start(pdev, i),
451c6fd2807SJeff Garzik 			        (unsigned long long)pci_resource_len(pdev, i));
452c6fd2807SJeff Garzik 			rc = -ENODEV;
453c6fd2807SJeff Garzik 			goto err_out_regions;
454c6fd2807SJeff Garzik 		}
455c6fd2807SJeff Garzik 
456c6fd2807SJeff Garzik 	rc = pci_set_dma_mask(pdev, ATA_DMA_MASK);
457c6fd2807SJeff Garzik 	if (rc)
458c6fd2807SJeff Garzik 		goto err_out_regions;
459c6fd2807SJeff Garzik 	rc = pci_set_consistent_dma_mask(pdev, ATA_DMA_MASK);
460c6fd2807SJeff Garzik 	if (rc)
461c6fd2807SJeff Garzik 		goto err_out_regions;
462c6fd2807SJeff Garzik 
463c6fd2807SJeff Garzik 	if (board_id == vt6420)
464c6fd2807SJeff Garzik 		probe_ent = vt6420_init_probe_ent(pdev);
465c6fd2807SJeff Garzik 	else
466c6fd2807SJeff Garzik 		probe_ent = vt6421_init_probe_ent(pdev);
467c6fd2807SJeff Garzik 
468c6fd2807SJeff Garzik 	if (!probe_ent) {
469c6fd2807SJeff Garzik 		dev_printk(KERN_ERR, &pdev->dev, "out of memory\n");
470c6fd2807SJeff Garzik 		rc = -ENOMEM;
471c6fd2807SJeff Garzik 		goto err_out_regions;
472c6fd2807SJeff Garzik 	}
473c6fd2807SJeff Garzik 
474c6fd2807SJeff Garzik 	svia_configure(pdev);
475c6fd2807SJeff Garzik 
476c6fd2807SJeff Garzik 	pci_set_master(pdev);
477c6fd2807SJeff Garzik 
478c6fd2807SJeff Garzik 	/* FIXME: check ata_device_add return value */
479c6fd2807SJeff Garzik 	ata_device_add(probe_ent);
480c6fd2807SJeff Garzik 	kfree(probe_ent);
481c6fd2807SJeff Garzik 
482c6fd2807SJeff Garzik 	return 0;
483c6fd2807SJeff Garzik 
484c6fd2807SJeff Garzik err_out_regions:
485c6fd2807SJeff Garzik 	pci_release_regions(pdev);
486c6fd2807SJeff Garzik err_out:
487c6fd2807SJeff Garzik 	if (!pci_dev_busy)
488c6fd2807SJeff Garzik 		pci_disable_device(pdev);
489c6fd2807SJeff Garzik 	return rc;
490c6fd2807SJeff Garzik }
491c6fd2807SJeff Garzik 
492c6fd2807SJeff Garzik static int __init svia_init(void)
493c6fd2807SJeff Garzik {
494c6fd2807SJeff Garzik 	return pci_register_driver(&svia_pci_driver);
495c6fd2807SJeff Garzik }
496c6fd2807SJeff Garzik 
497c6fd2807SJeff Garzik static void __exit svia_exit(void)
498c6fd2807SJeff Garzik {
499c6fd2807SJeff Garzik 	pci_unregister_driver(&svia_pci_driver);
500c6fd2807SJeff Garzik }
501c6fd2807SJeff Garzik 
502c6fd2807SJeff Garzik module_init(svia_init);
503c6fd2807SJeff Garzik module_exit(svia_exit);
504c6fd2807SJeff Garzik 
505