blob: 741e7cb69d8c269adc9d2f1f4815726cd8dee260 [file] [log] [blame]
Jeff Garzik669a5db2006-08-29 18:12:40 -04001/*
2 * pata_via.c - VIA PATA for new ATA layer
3 * (C) 2005-2006 Red Hat Inc
Jeff Garzik669a5db2006-08-29 18:12:40 -04004 *
5 * Documentation
6 * Most chipset documentation available under NDA only
7 *
8 * VIA version guide
9 * VIA VT82C561 - early design, uses ata_generic currently
10 * VIA VT82C576 - MWDMA, 33Mhz
11 * VIA VT82C586 - MWDMA, 33Mhz
12 * VIA VT82C586a - Added UDMA to 33Mhz
13 * VIA VT82C586b - UDMA33
14 * VIA VT82C596a - Nonfunctional UDMA66
15 * VIA VT82C596b - Working UDMA66
16 * VIA VT82C686 - Nonfunctional UDMA66
17 * VIA VT82C686a - Working UDMA66
18 * VIA VT82C686b - Updated to UDMA100
19 * VIA VT8231 - UDMA100
20 * VIA VT8233 - UDMA100
21 * VIA VT8233a - UDMA133
22 * VIA VT8233c - UDMA100
23 * VIA VT8235 - UDMA133
24 * VIA VT8237 - UDMA133
Bartlomiej Zolnierkiewicz460f5312010-01-18 18:17:12 +010025 * VIA VT8237A - UDMA133
Alan05c39e52007-01-31 17:14:38 +000026 * VIA VT8237S - UDMA133
Alan75f609d2006-12-04 16:38:25 +000027 * VIA VT8251 - UDMA133
Jeff Garzik669a5db2006-08-29 18:12:40 -040028 *
29 * Most registers remain compatible across chips. Others start reserved
30 * and acquire sensible semantics if set to 1 (eg cable detect). A few
31 * exceptions exist, notably around the FIFO settings.
32 *
33 * One additional quirk of the VIA design is that like ALi they use few
34 * PCI IDs for a lot of chips.
35 *
36 * Based heavily on:
37 *
38 * Version 3.38
39 *
40 * VIA IDE driver for Linux. Supported southbridges:
41 *
42 * vt82c576, vt82c586, vt82c586a, vt82c586b, vt82c596a, vt82c596b,
43 * vt82c686, vt82c686a, vt82c686b, vt8231, vt8233, vt8233c, vt8233a,
44 * vt8235, vt8237
45 *
46 * Copyright (c) 2000-2002 Vojtech Pavlik
47 *
48 * Based on the work of:
49 * Michel Aubry
50 * Jeff Garzik
51 * Andre Hedrick
52
53 */
54
55#include <linux/kernel.h>
56#include <linux/module.h>
57#include <linux/pci.h>
58#include <linux/init.h>
59#include <linux/blkdev.h>
60#include <linux/delay.h>
Tejun Heo5a0e3ad2010-03-24 17:04:11 +090061#include <linux/gfp.h>
Jeff Garzik669a5db2006-08-29 18:12:40 -040062#include <scsi/scsi_host.h>
63#include <linux/libata.h>
Alan Coxcf5792d2007-05-23 22:39:01 +010064#include <linux/dmi.h>
Jeff Garzik669a5db2006-08-29 18:12:40 -040065
66#define DRV_NAME "pata_via"
Alan Coxb4746ed2009-04-17 12:21:21 +010067#define DRV_VERSION "0.3.4"
Jeff Garzik669a5db2006-08-29 18:12:40 -040068
Jeff Garzik669a5db2006-08-29 18:12:40 -040069enum {
Bartlomiej Zolnierkiewicz460f5312010-01-18 18:17:12 +010070 VIA_BAD_PREQ = 0x01, /* Crashes if PREQ# till DDACK# set */
71 VIA_BAD_CLK66 = 0x02, /* 66 MHz clock doesn't work correctly */
72 VIA_SET_FIFO = 0x04, /* Needs to have FIFO split set */
73 VIA_NO_UNMASK = 0x08, /* Doesn't work with IRQ unmasking on */
74 VIA_BAD_ID = 0x10, /* Has wrong vendor ID (0x1107) */
75 VIA_BAD_AST = 0x20, /* Don't touch Address Setup Timing */
76 VIA_NO_ENABLES = 0x40, /* Has no enablebits */
77 VIA_SATA_PATA = 0x80, /* SATA/PATA combined configuration */
Jeff Garzik669a5db2006-08-29 18:12:40 -040078};
79
JosephChan@via.com.twe4d866c2009-01-23 15:37:39 +080080enum {
81 VIA_IDFLAG_SINGLE = (1 << 0), /* single channel controller) */
82};
83
Jeff Garzik669a5db2006-08-29 18:12:40 -040084/*
85 * VIA SouthBridge chips.
86 */
87
88static const struct via_isa_bridge {
89 const char *name;
90 u16 id;
91 u8 rev_min;
92 u8 rev_max;
Bartlomiej Zolnierkiewicz460f5312010-01-18 18:17:12 +010093 u8 udma_mask;
94 u8 flags;
Jeff Garzik669a5db2006-08-29 18:12:40 -040095} via_isa_bridges[] = {
Bartlomiej Zolnierkiewicz460f5312010-01-18 18:17:12 +010096 { "vx855", PCI_DEVICE_ID_VIA_VX855, 0x00, 0x2f, ATA_UDMA6, VIA_BAD_AST | VIA_SATA_PATA },
97 { "vx800", PCI_DEVICE_ID_VIA_VX800, 0x00, 0x2f, ATA_UDMA6, VIA_BAD_AST | VIA_SATA_PATA },
98 { "vt8261", PCI_DEVICE_ID_VIA_8261, 0x00, 0x2f, ATA_UDMA6, VIA_BAD_AST },
99 { "vt8237s", PCI_DEVICE_ID_VIA_8237S, 0x00, 0x2f, ATA_UDMA6, VIA_BAD_AST },
100 { "vt8251", PCI_DEVICE_ID_VIA_8251, 0x00, 0x2f, ATA_UDMA6, VIA_BAD_AST },
101 { "cx700", PCI_DEVICE_ID_VIA_CX700, 0x00, 0x2f, ATA_UDMA6, VIA_BAD_AST | VIA_SATA_PATA },
102 { "vt6410", PCI_DEVICE_ID_VIA_6410, 0x00, 0x2f, ATA_UDMA6, VIA_BAD_AST | VIA_NO_ENABLES },
103 { "vt6415", PCI_DEVICE_ID_VIA_6415, 0x00, 0xff, ATA_UDMA6, VIA_BAD_AST | VIA_NO_ENABLES },
104 { "vt8237a", PCI_DEVICE_ID_VIA_8237A, 0x00, 0x2f, ATA_UDMA6, VIA_BAD_AST },
105 { "vt8237", PCI_DEVICE_ID_VIA_8237, 0x00, 0x2f, ATA_UDMA6, VIA_BAD_AST },
106 { "vt8235", PCI_DEVICE_ID_VIA_8235, 0x00, 0x2f, ATA_UDMA6, VIA_BAD_AST },
107 { "vt8233a", PCI_DEVICE_ID_VIA_8233A, 0x00, 0x2f, ATA_UDMA6, VIA_BAD_AST },
108 { "vt8233c", PCI_DEVICE_ID_VIA_8233C_0, 0x00, 0x2f, ATA_UDMA5, },
109 { "vt8233", PCI_DEVICE_ID_VIA_8233_0, 0x00, 0x2f, ATA_UDMA5, },
110 { "vt8231", PCI_DEVICE_ID_VIA_8231, 0x00, 0x2f, ATA_UDMA5, },
111 { "vt82c686b", PCI_DEVICE_ID_VIA_82C686, 0x40, 0x4f, ATA_UDMA5, },
112 { "vt82c686a", PCI_DEVICE_ID_VIA_82C686, 0x10, 0x2f, ATA_UDMA4, },
113 { "vt82c686", PCI_DEVICE_ID_VIA_82C686, 0x00, 0x0f, ATA_UDMA2, VIA_BAD_CLK66 },
114 { "vt82c596b", PCI_DEVICE_ID_VIA_82C596, 0x10, 0x2f, ATA_UDMA4, },
115 { "vt82c596a", PCI_DEVICE_ID_VIA_82C596, 0x00, 0x0f, ATA_UDMA2, VIA_BAD_CLK66 },
116 { "vt82c586b", PCI_DEVICE_ID_VIA_82C586_0, 0x47, 0x4f, ATA_UDMA2, VIA_SET_FIFO },
117 { "vt82c586b", PCI_DEVICE_ID_VIA_82C586_0, 0x40, 0x46, ATA_UDMA2, VIA_SET_FIFO | VIA_BAD_PREQ },
118 { "vt82c586b", PCI_DEVICE_ID_VIA_82C586_0, 0x30, 0x3f, ATA_UDMA2, VIA_SET_FIFO },
119 { "vt82c586a", PCI_DEVICE_ID_VIA_82C586_0, 0x20, 0x2f, ATA_UDMA2, VIA_SET_FIFO },
120 { "vt82c586", PCI_DEVICE_ID_VIA_82C586_0, 0x00, 0x0f, 0x00, VIA_SET_FIFO },
121 { "vt82c576", PCI_DEVICE_ID_VIA_82C576, 0x00, 0x2f, 0x00, VIA_SET_FIFO | VIA_NO_UNMASK },
122 { "vt82c576", PCI_DEVICE_ID_VIA_82C576, 0x00, 0x2f, 0x00, VIA_SET_FIFO | VIA_NO_UNMASK | VIA_BAD_ID },
123 { "vtxxxx", PCI_DEVICE_ID_VIA_ANON, 0x00, 0x2f, ATA_UDMA6, VIA_BAD_AST },
Jeff Garzik669a5db2006-08-29 18:12:40 -0400124 { NULL }
125};
126
Alan Coxb4746ed2009-04-17 12:21:21 +0100127struct via_port {
128 u8 cached_device;
129};
Alan Coxcf5792d2007-05-23 22:39:01 +0100130
131/*
132 * Cable special cases
133 */
134
Jeff Garzik18552562007-10-03 15:15:40 -0400135static const struct dmi_system_id cable_dmi_table[] = {
Alan Coxcf5792d2007-05-23 22:39:01 +0100136 {
137 .ident = "Acer Ferrari 3400",
138 .matches = {
139 DMI_MATCH(DMI_BOARD_VENDOR, "Acer,Inc."),
140 DMI_MATCH(DMI_BOARD_NAME, "Ferrari 3400"),
141 },
142 },
143 { }
144};
145
146static int via_cable_override(struct pci_dev *pdev)
147{
148 /* Systems by DMI */
149 if (dmi_check_system(cable_dmi_table))
150 return 1;
Alan Cox9edbdbe2007-08-22 22:57:48 +0100151 /* Arima W730-K8/Targa Visionary 811/... */
152 if (pdev->subsystem_vendor == 0x161F && pdev->subsystem_device == 0x2032)
153 return 1;
Alan Coxcf5792d2007-05-23 22:39:01 +0100154 return 0;
155}
156
157
Jeff Garzik669a5db2006-08-29 18:12:40 -0400158/**
159 * via_cable_detect - cable detection
160 * @ap: ATA port
161 *
162 * Perform cable detection. Actually for the VIA case the BIOS
163 * already did this for us. We read the values provided by the
164 * BIOS. If you are using an 8235 in a non-PC configuration you
165 * may need to update this code.
166 *
167 * Hotplug also impacts on this.
168 */
169
170static int via_cable_detect(struct ata_port *ap) {
Alan Cox97cb81c2007-03-07 16:56:54 +0000171 const struct via_isa_bridge *config = ap->host->private_data;
Jeff Garzik669a5db2006-08-29 18:12:40 -0400172 struct pci_dev *pdev = to_pci_dev(ap->host->dev);
173 u32 ata66;
174
Alan Coxcf5792d2007-05-23 22:39:01 +0100175 if (via_cable_override(pdev))
176 return ATA_CBL_PATA40_SHORT;
177
Tejun Heo7585eb12008-02-07 10:18:53 +0900178 if ((config->flags & VIA_SATA_PATA) && ap->port_no == 0)
179 return ATA_CBL_SATA;
180
Alan Cox97cb81c2007-03-07 16:56:54 +0000181 /* Early chips are 40 wire */
Bartlomiej Zolnierkiewicz460f5312010-01-18 18:17:12 +0100182 if (config->udma_mask < ATA_UDMA4)
Alan Cox97cb81c2007-03-07 16:56:54 +0000183 return ATA_CBL_PATA40;
184 /* UDMA 66 chips have only drive side logic */
Bartlomiej Zolnierkiewicz460f5312010-01-18 18:17:12 +0100185 else if (config->udma_mask < ATA_UDMA5)
Alan Cox97cb81c2007-03-07 16:56:54 +0000186 return ATA_CBL_PATA_UNK;
187 /* UDMA 100 or later */
Jeff Garzik669a5db2006-08-29 18:12:40 -0400188 pci_read_config_dword(pdev, 0x50, &ata66);
189 /* Check both the drive cable reporting bits, we might not have
190 two drives */
191 if (ata66 & (0x10100000 >> (16 * ap->port_no)))
192 return ATA_CBL_PATA80;
Alan Cox7d73a362007-07-26 18:38:06 +0100193 /* Check with ACPI so we can spot BIOS reported SATA bridges */
Tejun Heo021ee9a2007-12-18 16:33:06 +0900194 if (ata_acpi_init_gtm(ap) &&
195 ata_acpi_cbl_80wire(ap, ata_acpi_init_gtm(ap)))
Alan Cox7d73a362007-07-26 18:38:06 +0100196 return ATA_CBL_PATA80;
Alan Cox97cb81c2007-03-07 16:56:54 +0000197 return ATA_CBL_PATA40;
Jeff Garzik669a5db2006-08-29 18:12:40 -0400198}
199
Tejun Heocc0680a2007-08-06 18:36:23 +0900200static int via_pre_reset(struct ata_link *link, unsigned long deadline)
Jeff Garzik669a5db2006-08-29 18:12:40 -0400201{
Tejun Heocc0680a2007-08-06 18:36:23 +0900202 struct ata_port *ap = link->ap;
Jeff Garzik669a5db2006-08-29 18:12:40 -0400203 const struct via_isa_bridge *config = ap->host->private_data;
204
205 if (!(config->flags & VIA_NO_ENABLES)) {
206 static const struct pci_bits via_enable_bits[] = {
207 { 0x40, 1, 0x02, 0x02 },
208 { 0x40, 1, 0x01, 0x01 }
209 };
Jeff Garzik669a5db2006-08-29 18:12:40 -0400210 struct pci_dev *pdev = to_pci_dev(ap->host->dev);
Alan Coxc9619222006-09-26 17:53:38 +0100211 if (!pci_test_config_bits(pdev, &via_enable_bits[ap->port_no]))
212 return -ENOENT;
Jeff Garzik669a5db2006-08-29 18:12:40 -0400213 }
Tejun Heod4b2bab2007-02-02 16:50:52 +0900214
Tejun Heo9363c382008-04-07 22:47:16 +0900215 return ata_sff_prereset(link, deadline);
Jeff Garzik669a5db2006-08-29 18:12:40 -0400216}
217
218
219/**
Bartlomiej Zolnierkiewiczf7775822010-01-18 18:17:03 +0100220 * via_do_set_mode - set transfer mode data
Jeff Garzik669a5db2006-08-29 18:12:40 -0400221 * @ap: ATA interface
222 * @adev: ATA device
223 * @mode: ATA mode being programmed
Jeff Garzik669a5db2006-08-29 18:12:40 -0400224 * @set_ast: Set to program address setup
225 * @udma_type: UDMA mode/format of registers
226 *
227 * Program the VIA registers for DMA and PIO modes. Uses the ata timing
228 * support in order to compute modes.
229 *
230 * FIXME: Hotplug will require we serialize multiple mode changes
231 * on the two channels.
232 */
233
Bartlomiej Zolnierkiewicz460f5312010-01-18 18:17:12 +0100234static void via_do_set_mode(struct ata_port *ap, struct ata_device *adev,
235 int mode, int set_ast, int udma_type)
Jeff Garzik669a5db2006-08-29 18:12:40 -0400236{
237 struct pci_dev *pdev = to_pci_dev(ap->host->dev);
238 struct ata_device *peer = ata_dev_pair(adev);
239 struct ata_timing t, p;
Bartlomiej Zolnierkiewicz460f5312010-01-18 18:17:12 +0100240 static int via_clock = 33333; /* Bus clock in kHZ */
Jeff Garzik669a5db2006-08-29 18:12:40 -0400241 unsigned long T = 1000000000 / via_clock;
Bartlomiej Zolnierkiewicz460f5312010-01-18 18:17:12 +0100242 unsigned long UT = T;
Jeff Garzik669a5db2006-08-29 18:12:40 -0400243 int ut;
244 int offset = 3 - (2*ap->port_no) - adev->devno;
245
Bartlomiej Zolnierkiewicz460f5312010-01-18 18:17:12 +0100246 switch (udma_type) {
247 case ATA_UDMA4:
248 UT = T / 2; break;
249 case ATA_UDMA5:
250 UT = T / 3; break;
251 case ATA_UDMA6:
252 UT = T / 4; break;
253 }
254
Jeff Garzik669a5db2006-08-29 18:12:40 -0400255 /* Calculate the timing values we require */
256 ata_timing_compute(adev, mode, &t, T, UT);
257
258 /* We share 8bit timing so we must merge the constraints */
259 if (peer) {
260 if (peer->pio_mode) {
261 ata_timing_compute(peer, peer->pio_mode, &p, T, UT);
262 ata_timing_merge(&p, &t, &t, ATA_TIMING_8BIT);
263 }
264 }
265
266 /* Address setup is programmable but breaks on UDMA133 setups */
267 if (set_ast) {
268 u8 setup; /* 2 bits per drive */
269 int shift = 2 * offset;
270
271 pci_read_config_byte(pdev, 0x4C, &setup);
272 setup &= ~(3 << shift);
Bartlomiej Zolnierkiewiczf7775822010-01-18 18:17:03 +0100273 setup |= (clamp_val(t.setup, 1, 4) - 1) << shift;
Jeff Garzik669a5db2006-08-29 18:12:40 -0400274 pci_write_config_byte(pdev, 0x4C, setup);
275 }
276
277 /* Load the PIO mode bits */
278 pci_write_config_byte(pdev, 0x4F - ap->port_no,
Harvey Harrison07633b52008-05-14 16:17:00 -0700279 ((clamp_val(t.act8b, 1, 16) - 1) << 4) | (clamp_val(t.rec8b, 1, 16) - 1));
Jeff Garzik669a5db2006-08-29 18:12:40 -0400280 pci_write_config_byte(pdev, 0x48 + offset,
Harvey Harrison07633b52008-05-14 16:17:00 -0700281 ((clamp_val(t.active, 1, 16) - 1) << 4) | (clamp_val(t.recover, 1, 16) - 1));
Jeff Garzik669a5db2006-08-29 18:12:40 -0400282
283 /* Load the UDMA bits according to type */
Bartlomiej Zolnierkiewicz460f5312010-01-18 18:17:12 +0100284 switch (udma_type) {
285 case ATA_UDMA2:
286 default:
287 ut = t.udma ? (0xe0 | (clamp_val(t.udma, 2, 5) - 2)) : 0x03;
288 break;
289 case ATA_UDMA4:
290 ut = t.udma ? (0xe8 | (clamp_val(t.udma, 2, 9) - 2)) : 0x0f;
291 break;
292 case ATA_UDMA5:
293 ut = t.udma ? (0xe0 | (clamp_val(t.udma, 2, 9) - 2)) : 0x07;
294 break;
295 case ATA_UDMA6:
296 ut = t.udma ? (0xe0 | (clamp_val(t.udma, 2, 9) - 2)) : 0x07;
297 break;
Jeff Garzik669a5db2006-08-29 18:12:40 -0400298 }
Laurent Riffard08ebd432007-09-02 21:01:32 +0200299
Jeff Garzik669a5db2006-08-29 18:12:40 -0400300 /* Set UDMA unless device is not UDMA capable */
Bartlomiej Zolnierkiewiczc4d8a202009-12-03 20:32:13 +0100301 if (udma_type) {
302 u8 udma_etc;
Laurent Riffard08ebd432007-09-02 21:01:32 +0200303
Bartlomiej Zolnierkiewiczc4d8a202009-12-03 20:32:13 +0100304 pci_read_config_byte(pdev, 0x50 + offset, &udma_etc);
Laurent Riffard08ebd432007-09-02 21:01:32 +0200305
Bartlomiej Zolnierkiewiczc4d8a202009-12-03 20:32:13 +0100306 /* clear transfer mode bit */
307 udma_etc &= ~0x20;
308
309 if (t.udma) {
310 /* preserve 80-wire cable detection bit */
311 udma_etc &= 0x10;
312 udma_etc |= ut;
313 }
314
315 pci_write_config_byte(pdev, 0x50 + offset, udma_etc);
Laurent Riffard08ebd432007-09-02 21:01:32 +0200316 }
Jeff Garzik669a5db2006-08-29 18:12:40 -0400317}
318
319static void via_set_piomode(struct ata_port *ap, struct ata_device *adev)
320{
321 const struct via_isa_bridge *config = ap->host->private_data;
322 int set_ast = (config->flags & VIA_BAD_AST) ? 0 : 1;
Jeff Garzik669a5db2006-08-29 18:12:40 -0400323
Bartlomiej Zolnierkiewicz460f5312010-01-18 18:17:12 +0100324 via_do_set_mode(ap, adev, adev->pio_mode, set_ast, config->udma_mask);
Jeff Garzik669a5db2006-08-29 18:12:40 -0400325}
326
327static void via_set_dmamode(struct ata_port *ap, struct ata_device *adev)
328{
329 const struct via_isa_bridge *config = ap->host->private_data;
330 int set_ast = (config->flags & VIA_BAD_AST) ? 0 : 1;
Jeff Garzik669a5db2006-08-29 18:12:40 -0400331
Bartlomiej Zolnierkiewicz460f5312010-01-18 18:17:12 +0100332 via_do_set_mode(ap, adev, adev->dma_mode, set_ast, config->udma_mask);
Jeff Garzik669a5db2006-08-29 18:12:40 -0400333}
334
JosephChan@via.com.twbfce5e02008-07-30 12:32:48 -0700335/**
Alan Cox10734fc2009-11-30 13:22:43 +0000336 * via_mode_filter - filter buggy device/mode pairs
337 * @dev: ATA device
338 * @mask: Mode bitmask
339 *
340 * We need to apply some minimal filtering for old controllers and at least
341 * one breed of Transcend SSD. Return the updated mask.
342 */
343
344static unsigned long via_mode_filter(struct ata_device *dev, unsigned long mask)
345{
346 struct ata_host *host = dev->link->ap->host;
347 const struct via_isa_bridge *config = host->private_data;
348 unsigned char model_num[ATA_ID_PROD_LEN + 1];
349
350 if (config->id == PCI_DEVICE_ID_VIA_82C586_0) {
351 ata_id_c_string(dev->id, model_num, ATA_ID_PROD, sizeof(model_num));
352 if (strcmp(model_num, "TS64GSSD25-M") == 0) {
353 ata_dev_printk(dev, KERN_WARNING,
354 "disabling UDMA mode due to reported lockups with this device.\n");
355 mask &= ~ ATA_MASK_UDMA;
356 }
357 }
358 return ata_bmdma_mode_filter(dev, mask);
359}
360
361/**
Tejun Heoff047152008-07-31 18:02:35 +0900362 * via_tf_load - send taskfile registers to host controller
JosephChan@via.com.twbfce5e02008-07-30 12:32:48 -0700363 * @ap: Port to which output is sent
364 * @tf: ATA taskfile register set
365 *
366 * Outputs ATA taskfile to standard ATA host controller.
367 *
368 * Note: This is to fix the internal bug of via chipsets, which
Tejun Heoff047152008-07-31 18:02:35 +0900369 * will reset the device register after changing the IEN bit on
370 * ctl register
JosephChan@via.com.twbfce5e02008-07-30 12:32:48 -0700371 */
Tejun Heoff047152008-07-31 18:02:35 +0900372static void via_tf_load(struct ata_port *ap, const struct ata_taskfile *tf)
JosephChan@via.com.twbfce5e02008-07-30 12:32:48 -0700373{
Alan Coxb4746ed2009-04-17 12:21:21 +0100374 struct ata_ioports *ioaddr = &ap->ioaddr;
375 struct via_port *vp = ap->private_data;
376 unsigned int is_addr = tf->flags & ATA_TFLAG_ISADDR;
377 int newctl = 0;
JosephChan@via.com.twbfce5e02008-07-30 12:32:48 -0700378
Alan Coxb4746ed2009-04-17 12:21:21 +0100379 if (tf->ctl != ap->last_ctl) {
380 iowrite8(tf->ctl, ioaddr->ctl_addr);
381 ap->last_ctl = tf->ctl;
382 ata_wait_idle(ap);
383 newctl = 1;
JosephChan@via.com.twbfce5e02008-07-30 12:32:48 -0700384 }
Alan Coxb4746ed2009-04-17 12:21:21 +0100385
386 if (tf->flags & ATA_TFLAG_DEVICE) {
387 iowrite8(tf->device, ioaddr->device_addr);
388 vp->cached_device = tf->device;
389 } else if (newctl)
390 iowrite8(vp->cached_device, ioaddr->device_addr);
391
392 if (is_addr && (tf->flags & ATA_TFLAG_LBA48)) {
393 WARN_ON_ONCE(!ioaddr->ctl_addr);
394 iowrite8(tf->hob_feature, ioaddr->feature_addr);
395 iowrite8(tf->hob_nsect, ioaddr->nsect_addr);
396 iowrite8(tf->hob_lbal, ioaddr->lbal_addr);
397 iowrite8(tf->hob_lbam, ioaddr->lbam_addr);
398 iowrite8(tf->hob_lbah, ioaddr->lbah_addr);
399 VPRINTK("hob: feat 0x%X nsect 0x%X, lba 0x%X 0x%X 0x%X\n",
400 tf->hob_feature,
401 tf->hob_nsect,
402 tf->hob_lbal,
403 tf->hob_lbam,
404 tf->hob_lbah);
405 }
406
407 if (is_addr) {
408 iowrite8(tf->feature, ioaddr->feature_addr);
409 iowrite8(tf->nsect, ioaddr->nsect_addr);
410 iowrite8(tf->lbal, ioaddr->lbal_addr);
411 iowrite8(tf->lbam, ioaddr->lbam_addr);
412 iowrite8(tf->lbah, ioaddr->lbah_addr);
413 VPRINTK("feat 0x%X nsect 0x%X lba 0x%X 0x%X 0x%X\n",
414 tf->feature,
415 tf->nsect,
416 tf->lbal,
417 tf->lbam,
418 tf->lbah);
419 }
420
421 ata_wait_idle(ap);
422}
423
424static int via_port_start(struct ata_port *ap)
425{
426 struct via_port *vp;
427 struct pci_dev *pdev = to_pci_dev(ap->host->dev);
428
429 int ret = ata_sff_port_start(ap);
430 if (ret < 0)
431 return ret;
432
433 vp = devm_kzalloc(&pdev->dev, sizeof(struct via_port), GFP_KERNEL);
434 if (vp == NULL)
435 return -ENOMEM;
436 ap->private_data = vp;
437 return 0;
JosephChan@via.com.twbfce5e02008-07-30 12:32:48 -0700438}
439
Jeff Garzik669a5db2006-08-29 18:12:40 -0400440static struct scsi_host_template via_sht = {
Tejun Heo68d1d072008-03-25 12:22:49 +0900441 ATA_BMDMA_SHT(DRV_NAME),
Jeff Garzik669a5db2006-08-29 18:12:40 -0400442};
443
444static struct ata_port_operations via_port_ops = {
Tejun Heo029cfd62008-03-25 12:22:49 +0900445 .inherits = &ata_bmdma_port_ops,
446 .cable_detect = via_cable_detect,
Jeff Garzik669a5db2006-08-29 18:12:40 -0400447 .set_piomode = via_set_piomode,
448 .set_dmamode = via_set_dmamode,
Tejun Heoa1efdab2008-03-25 12:22:50 +0900449 .prereset = via_pre_reset,
Tejun Heoff047152008-07-31 18:02:35 +0900450 .sff_tf_load = via_tf_load,
Alan Coxb4746ed2009-04-17 12:21:21 +0100451 .port_start = via_port_start,
Alan Cox10734fc2009-11-30 13:22:43 +0000452 .mode_filter = via_mode_filter,
Jeff Garzik669a5db2006-08-29 18:12:40 -0400453};
454
455static struct ata_port_operations via_port_ops_noirq = {
Tejun Heo029cfd62008-03-25 12:22:49 +0900456 .inherits = &via_port_ops,
Tejun Heo5682ed32008-04-07 22:47:16 +0900457 .sff_data_xfer = ata_sff_data_xfer_noirq,
Jeff Garzik669a5db2006-08-29 18:12:40 -0400458};
459
460/**
Alan627d2d32006-11-27 16:19:36 +0000461 * via_config_fifo - set up the FIFO
462 * @pdev: PCI device
463 * @flags: configuration flags
464 *
Robert P. J. Day3a4fa0a2007-10-19 23:10:43 +0200465 * Set the FIFO properties for this device if necessary. Used both on
Alan627d2d32006-11-27 16:19:36 +0000466 * set up and on and the resume path
467 */
468
469static void via_config_fifo(struct pci_dev *pdev, unsigned int flags)
470{
471 u8 enable;
Jeff Garzikf20b16f2006-12-11 11:14:06 -0500472
Alan627d2d32006-11-27 16:19:36 +0000473 /* 0x40 low bits indicate enabled channels */
474 pci_read_config_byte(pdev, 0x40 , &enable);
475 enable &= 3;
Jeff Garzikf20b16f2006-12-11 11:14:06 -0500476
Alan627d2d32006-11-27 16:19:36 +0000477 if (flags & VIA_SET_FIFO) {
Andrew Morton73720862006-12-20 13:09:10 -0500478 static const u8 fifo_setting[4] = {0x00, 0x60, 0x00, 0x20};
Alan627d2d32006-11-27 16:19:36 +0000479 u8 fifo;
480
481 pci_read_config_byte(pdev, 0x43, &fifo);
482
483 /* Clear PREQ# until DDACK# for errata */
484 if (flags & VIA_BAD_PREQ)
485 fifo &= 0x7F;
486 else
487 fifo &= 0x9f;
488 /* Turn on FIFO for enabled channels */
489 fifo |= fifo_setting[enable];
490 pci_write_config_byte(pdev, 0x43, fifo);
491 }
492}
493
494/**
Jeff Garzik669a5db2006-08-29 18:12:40 -0400495 * via_init_one - discovery callback
Alan627d2d32006-11-27 16:19:36 +0000496 * @pdev: PCI device
Jeff Garzik669a5db2006-08-29 18:12:40 -0400497 * @id: PCI table info
498 *
499 * A VIA IDE interface has been discovered. Figure out what revision
500 * and perform configuration work before handing it to the ATA layer
501 */
502
503static int via_init_one(struct pci_dev *pdev, const struct pci_device_id *id)
504{
505 /* Early VIA without UDMA support */
Tejun Heo1626aeb2007-05-04 12:43:58 +0200506 static const struct ata_port_info via_mwdma_info = {
Tejun Heo464cf172007-05-27 15:10:40 +0200507 .flags = ATA_FLAG_SLAVE_POSS,
Erik Inge Bolsø14bdef92009-03-14 21:38:24 +0100508 .pio_mask = ATA_PIO4,
509 .mwdma_mask = ATA_MWDMA2,
Jeff Garzik669a5db2006-08-29 18:12:40 -0400510 .port_ops = &via_port_ops
511 };
512 /* Ditto with IRQ masking required */
Tejun Heo1626aeb2007-05-04 12:43:58 +0200513 static const struct ata_port_info via_mwdma_info_borked = {
Tejun Heo464cf172007-05-27 15:10:40 +0200514 .flags = ATA_FLAG_SLAVE_POSS,
Erik Inge Bolsø14bdef92009-03-14 21:38:24 +0100515 .pio_mask = ATA_PIO4,
516 .mwdma_mask = ATA_MWDMA2,
Jeff Garzik669a5db2006-08-29 18:12:40 -0400517 .port_ops = &via_port_ops_noirq,
518 };
519 /* VIA UDMA 33 devices (and borked 66) */
Tejun Heo1626aeb2007-05-04 12:43:58 +0200520 static const struct ata_port_info via_udma33_info = {
Tejun Heo464cf172007-05-27 15:10:40 +0200521 .flags = ATA_FLAG_SLAVE_POSS,
Erik Inge Bolsø14bdef92009-03-14 21:38:24 +0100522 .pio_mask = ATA_PIO4,
523 .mwdma_mask = ATA_MWDMA2,
Jeff Garzikbf6263a2007-07-09 12:16:50 -0400524 .udma_mask = ATA_UDMA2,
Jeff Garzik669a5db2006-08-29 18:12:40 -0400525 .port_ops = &via_port_ops
526 };
527 /* VIA UDMA 66 devices */
Tejun Heo1626aeb2007-05-04 12:43:58 +0200528 static const struct ata_port_info via_udma66_info = {
Tejun Heo464cf172007-05-27 15:10:40 +0200529 .flags = ATA_FLAG_SLAVE_POSS,
Erik Inge Bolsø14bdef92009-03-14 21:38:24 +0100530 .pio_mask = ATA_PIO4,
531 .mwdma_mask = ATA_MWDMA2,
Jeff Garzikbf6263a2007-07-09 12:16:50 -0400532 .udma_mask = ATA_UDMA4,
Jeff Garzik669a5db2006-08-29 18:12:40 -0400533 .port_ops = &via_port_ops
534 };
535 /* VIA UDMA 100 devices */
Tejun Heo1626aeb2007-05-04 12:43:58 +0200536 static const struct ata_port_info via_udma100_info = {
Tejun Heo464cf172007-05-27 15:10:40 +0200537 .flags = ATA_FLAG_SLAVE_POSS,
Erik Inge Bolsø14bdef92009-03-14 21:38:24 +0100538 .pio_mask = ATA_PIO4,
539 .mwdma_mask = ATA_MWDMA2,
Jeff Garzikbf6263a2007-07-09 12:16:50 -0400540 .udma_mask = ATA_UDMA5,
Jeff Garzik669a5db2006-08-29 18:12:40 -0400541 .port_ops = &via_port_ops
542 };
543 /* UDMA133 with bad AST (All current 133) */
Tejun Heo1626aeb2007-05-04 12:43:58 +0200544 static const struct ata_port_info via_udma133_info = {
Tejun Heo464cf172007-05-27 15:10:40 +0200545 .flags = ATA_FLAG_SLAVE_POSS,
Erik Inge Bolsø14bdef92009-03-14 21:38:24 +0100546 .pio_mask = ATA_PIO4,
547 .mwdma_mask = ATA_MWDMA2,
Jeff Garzikbf6263a2007-07-09 12:16:50 -0400548 .udma_mask = ATA_UDMA6, /* FIXME: should check north bridge */
Jeff Garzik669a5db2006-08-29 18:12:40 -0400549 .port_ops = &via_port_ops
550 };
Tejun Heo887125e2008-03-25 12:22:49 +0900551 const struct ata_port_info *ppi[] = { NULL, NULL };
Jiri Slaby7095e3e2009-11-04 17:11:03 +0100552 struct pci_dev *isa;
Jeff Garzik669a5db2006-08-29 18:12:40 -0400553 const struct via_isa_bridge *config;
554 static int printed_version;
Jeff Garzik669a5db2006-08-29 18:12:40 -0400555 u8 enable;
556 u32 timing;
JosephChan@via.com.twe4d866c2009-01-23 15:37:39 +0800557 unsigned long flags = id->driver_data;
Tejun Heof08048e2008-03-25 12:22:47 +0900558 int rc;
Jeff Garzik669a5db2006-08-29 18:12:40 -0400559
560 if (!printed_version++)
561 dev_printk(KERN_DEBUG, &pdev->dev, "version " DRV_VERSION "\n");
562
Tejun Heof08048e2008-03-25 12:22:47 +0900563 rc = pcim_enable_device(pdev);
564 if (rc)
565 return rc;
566
JosephChan@via.com.twe4d866c2009-01-23 15:37:39 +0800567 if (flags & VIA_IDFLAG_SINGLE)
568 ppi[1] = &ata_dummy_port_info;
569
Jeff Garzik669a5db2006-08-29 18:12:40 -0400570 /* To find out how the IDE will behave and what features we
571 actually have to look at the bridge not the IDE controller */
JosephChan@via.com.twe4d866c2009-01-23 15:37:39 +0800572 for (config = via_isa_bridges; config->id != PCI_DEVICE_ID_VIA_ANON;
573 config++)
Jeff Garzik669a5db2006-08-29 18:12:40 -0400574 if ((isa = pci_get_device(PCI_VENDOR_ID_VIA +
575 !!(config->flags & VIA_BAD_ID),
576 config->id, NULL))) {
Jiri Slaby7095e3e2009-11-04 17:11:03 +0100577 u8 rev = isa->revision;
Jeff Garzik669a5db2006-08-29 18:12:40 -0400578 pci_dev_put(isa);
Jeff Garzik669a5db2006-08-29 18:12:40 -0400579
JosephChan@via.com.twbc8a6732010-03-25 20:51:47 +0800580 if ((id->device == 0x0415 || id->device == 0x3164) &&
581 (config->id != id->device))
582 continue;
583
Jiri Slaby7095e3e2009-11-04 17:11:03 +0100584 if (rev >= config->rev_min && rev <= config->rev_max)
585 break;
586 }
Jeff Garzik669a5db2006-08-29 18:12:40 -0400587
Alan Cox11f64002008-04-29 14:10:57 +0100588 if (!(config->flags & VIA_NO_ENABLES)) {
589 /* 0x40 low bits indicate enabled channels */
590 pci_read_config_byte(pdev, 0x40 , &enable);
591 enable &= 3;
592 if (enable == 0)
593 return -ENODEV;
Jeff Garzik669a5db2006-08-29 18:12:40 -0400594 }
595
596 /* Initialise the FIFO for the enabled channels. */
Alan627d2d32006-11-27 16:19:36 +0000597 via_config_fifo(pdev, config->flags);
Jeff Garzikf20b16f2006-12-11 11:14:06 -0500598
Jeff Garzik669a5db2006-08-29 18:12:40 -0400599 /* Clock set up */
Bartlomiej Zolnierkiewicz460f5312010-01-18 18:17:12 +0100600 switch (config->udma_mask) {
601 case 0x00:
602 if (config->flags & VIA_NO_UNMASK)
603 ppi[0] = &via_mwdma_info_borked;
604 else
605 ppi[0] = &via_mwdma_info;
606 break;
607 case ATA_UDMA2:
608 ppi[0] = &via_udma33_info;
609 break;
610 case ATA_UDMA4:
611 ppi[0] = &via_udma66_info;
612 break;
613 case ATA_UDMA5:
614 ppi[0] = &via_udma100_info;
615 break;
616 case ATA_UDMA6:
617 ppi[0] = &via_udma133_info;
618 break;
619 default:
620 WARN_ON(1);
621 return -ENODEV;
622 }
Jeff Garzik669a5db2006-08-29 18:12:40 -0400623
624 if (config->flags & VIA_BAD_CLK66) {
625 /* Disable the 66MHz clock on problem devices */
626 pci_read_config_dword(pdev, 0x50, &timing);
627 timing &= ~0x80008;
628 pci_write_config_dword(pdev, 0x50, timing);
629 }
630
631 /* We have established the device type, now fire it up */
Alan Cox16ea0fc2010-02-23 02:26:06 -0500632 return ata_pci_sff_init_one(pdev, ppi, &via_sht, (void *)config, 0);
Jeff Garzik669a5db2006-08-29 18:12:40 -0400633}
634
Tejun Heo438ac6d2007-03-02 17:31:26 +0900635#ifdef CONFIG_PM
Alan627d2d32006-11-27 16:19:36 +0000636/**
637 * via_reinit_one - reinit after resume
638 * @pdev; PCI device
639 *
640 * Called when the VIA PATA device is resumed. We must then
641 * reconfigure the fifo and other setup we may have altered. In
642 * addition the kernel needs to have the resume methods on PCI
643 * quirk supported.
644 */
645
646static int via_reinit_one(struct pci_dev *pdev)
647{
648 u32 timing;
649 struct ata_host *host = dev_get_drvdata(&pdev->dev);
650 const struct via_isa_bridge *config = host->private_data;
Tejun Heof08048e2008-03-25 12:22:47 +0900651 int rc;
652
653 rc = ata_pci_device_do_resume(pdev);
654 if (rc)
655 return rc;
Jeff Garzikf20b16f2006-12-11 11:14:06 -0500656
Alan627d2d32006-11-27 16:19:36 +0000657 via_config_fifo(pdev, config->flags);
658
Bartlomiej Zolnierkiewicz460f5312010-01-18 18:17:12 +0100659 if (config->udma_mask == ATA_UDMA4) {
Alan627d2d32006-11-27 16:19:36 +0000660 /* The 66 MHz devices require we enable the clock */
661 pci_read_config_dword(pdev, 0x50, &timing);
662 timing |= 0x80008;
663 pci_write_config_dword(pdev, 0x50, timing);
664 }
665 if (config->flags & VIA_BAD_CLK66) {
666 /* Disable the 66MHz clock on problem devices */
667 pci_read_config_dword(pdev, 0x50, &timing);
668 timing &= ~0x80008;
669 pci_write_config_dword(pdev, 0x50, timing);
670 }
Tejun Heof08048e2008-03-25 12:22:47 +0900671
672 ata_host_resume(host);
673 return 0;
Alan627d2d32006-11-27 16:19:36 +0000674}
Tejun Heo438ac6d2007-03-02 17:31:26 +0900675#endif
Alan627d2d32006-11-27 16:19:36 +0000676
Jeff Garzik669a5db2006-08-29 18:12:40 -0400677static const struct pci_device_id via[] = {
Zlatko Calusic5955c7a2009-02-18 01:33:34 +0100678 { PCI_VDEVICE(VIA, 0x0415), },
Jeff Garzik52df0ee2007-05-25 05:02:06 -0400679 { PCI_VDEVICE(VIA, 0x0571), },
680 { PCI_VDEVICE(VIA, 0x0581), },
681 { PCI_VDEVICE(VIA, 0x1571), },
682 { PCI_VDEVICE(VIA, 0x3164), },
683 { PCI_VDEVICE(VIA, 0x5324), },
JosephChan@via.com.twe4d866c2009-01-23 15:37:39 +0800684 { PCI_VDEVICE(VIA, 0xC409), VIA_IDFLAG_SINGLE },
JosephChan@via.com.tw4f1deba2010-03-19 14:08:11 +0800685 { PCI_VDEVICE(VIA, 0x9001), VIA_IDFLAG_SINGLE },
Jeff Garzik2d2744f2006-09-28 20:21:59 -0400686
687 { },
Jeff Garzik669a5db2006-08-29 18:12:40 -0400688};
689
690static struct pci_driver via_pci_driver = {
Jeff Garzik2d2744f2006-09-28 20:21:59 -0400691 .name = DRV_NAME,
Jeff Garzik669a5db2006-08-29 18:12:40 -0400692 .id_table = via,
693 .probe = via_init_one,
Alan627d2d32006-11-27 16:19:36 +0000694 .remove = ata_pci_remove_one,
Tejun Heo438ac6d2007-03-02 17:31:26 +0900695#ifdef CONFIG_PM
Alan627d2d32006-11-27 16:19:36 +0000696 .suspend = ata_pci_device_suspend,
697 .resume = via_reinit_one,
Tejun Heo438ac6d2007-03-02 17:31:26 +0900698#endif
Jeff Garzik669a5db2006-08-29 18:12:40 -0400699};
700
701static int __init via_init(void)
702{
703 return pci_register_driver(&via_pci_driver);
704}
705
Jeff Garzik669a5db2006-08-29 18:12:40 -0400706static void __exit via_exit(void)
707{
708 pci_unregister_driver(&via_pci_driver);
709}
710
Jeff Garzik669a5db2006-08-29 18:12:40 -0400711MODULE_AUTHOR("Alan Cox");
712MODULE_DESCRIPTION("low-level driver for VIA PATA");
713MODULE_LICENSE("GPL");
714MODULE_DEVICE_TABLE(pci, via);
715MODULE_VERSION(DRV_VERSION);
716
717module_init(via_init);
718module_exit(via_exit);