blob: 1b2d4a0f5f743c14b01dbd0f4b9af20d4abef2fb [file] [log] [blame]
Jeff Garzik669a5db2006-08-29 18:12:40 -04001/*
2 * pata-cs5535.c - CS5535 PATA for new ATA layer
3 * (C) 2005-2006 Red Hat Inc
Alan Coxab771632008-10-27 15:09:10 +00004 * Alan Cox <alan@lxorguk.ukuu.org.uk>
Jeff Garzik669a5db2006-08-29 18:12:40 -04005 *
6 * based upon cs5535.c from AMD <Jens.Altmann@amd.com> as cleaned up and
7 * made readable and Linux style by Wolfgang Zuleger <wolfgang.zuleger@gmx.de
8 * and Alexander Kiausch <alex.kiausch@t-online.de>
9 *
10 * This program is free software; you can redistribute it and/or modify
11 * it under the terms of the GNU General Public License version 2 as
12 * published by the Free Software Foundation.
13 *
14 * This program is distributed in the hope that it will be useful,
15 * but WITHOUT ANY WARRANTY; without even the implied warranty of
16 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
17 * GNU General Public License for more details.
18 *
19 * You should have received a copy of the GNU General Public License
20 * along with this program; if not, write to the Free Software
21 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
22 *
23 * Loosely based on the piix & svwks drivers.
24 *
25 * Documentation:
26 * Available from AMD web site.
27 * TODO
Robert P. J. Day3a4fa0a2007-10-19 23:10:43 +020028 * Review errata to see if serializing is necessary
Jeff Garzik669a5db2006-08-29 18:12:40 -040029 */
30
31#include <linux/kernel.h>
32#include <linux/module.h>
33#include <linux/pci.h>
34#include <linux/init.h>
35#include <linux/blkdev.h>
36#include <linux/delay.h>
37#include <scsi/scsi_host.h>
38#include <linux/libata.h>
39#include <asm/msr.h>
40
41#define DRV_NAME "cs5535"
Jeff Garzik8bc3fc42007-05-21 20:26:38 -040042#define DRV_VERSION "0.2.12"
Jeff Garzik669a5db2006-08-29 18:12:40 -040043
44/*
45 * The Geode (Aka Athlon GX now) uses an internal MSR based
46 * bus system for control. Demented but there you go.
47 */
48
49#define MSR_ATAC_BASE 0x51300000
50#define ATAC_GLD_MSR_CAP (MSR_ATAC_BASE+0)
51#define ATAC_GLD_MSR_CONFIG (MSR_ATAC_BASE+0x01)
52#define ATAC_GLD_MSR_SMI (MSR_ATAC_BASE+0x02)
53#define ATAC_GLD_MSR_ERROR (MSR_ATAC_BASE+0x03)
54#define ATAC_GLD_MSR_PM (MSR_ATAC_BASE+0x04)
55#define ATAC_GLD_MSR_DIAG (MSR_ATAC_BASE+0x05)
56#define ATAC_IO_BAR (MSR_ATAC_BASE+0x08)
57#define ATAC_RESET (MSR_ATAC_BASE+0x10)
58#define ATAC_CH0D0_PIO (MSR_ATAC_BASE+0x20)
59#define ATAC_CH0D0_DMA (MSR_ATAC_BASE+0x21)
60#define ATAC_CH0D1_PIO (MSR_ATAC_BASE+0x22)
61#define ATAC_CH0D1_DMA (MSR_ATAC_BASE+0x23)
62#define ATAC_PCI_ABRTERR (MSR_ATAC_BASE+0x24)
63
64#define ATAC_BM0_CMD_PRIM 0x00
65#define ATAC_BM0_STS_PRIM 0x02
66#define ATAC_BM0_PRD 0x04
67
68#define CS5535_CABLE_DETECT 0x48
69
70#define CS5535_BAD_PIO(timings) ( (timings&~0x80000000UL)==0x00009172 )
71
72/**
Alan Coxeb4a2c72007-04-11 00:04:20 +010073 * cs5535_cable_detect - detect cable type
Jeff Garzik669a5db2006-08-29 18:12:40 -040074 * @ap: Port to detect on
Tejun Heod4b2bab2007-02-02 16:50:52 +090075 * @deadline: deadline jiffies for the operation
Jeff Garzik669a5db2006-08-29 18:12:40 -040076 *
77 * Perform cable detection for ATA66 capable cable. Return a libata
78 * cable type.
79 */
80
Alan Coxeb4a2c72007-04-11 00:04:20 +010081static int cs5535_cable_detect(struct ata_port *ap)
Jeff Garzik669a5db2006-08-29 18:12:40 -040082{
83 u8 cable;
84 struct pci_dev *pdev = to_pci_dev(ap->host->dev);
85
86 pci_read_config_byte(pdev, CS5535_CABLE_DETECT, &cable);
87 if (cable & 1)
Alan Coxeb4a2c72007-04-11 00:04:20 +010088 return ATA_CBL_PATA80;
Jeff Garzik669a5db2006-08-29 18:12:40 -040089 else
Alan Coxeb4a2c72007-04-11 00:04:20 +010090 return ATA_CBL_PATA40;
Jeff Garzik669a5db2006-08-29 18:12:40 -040091}
92
93/**
94 * cs5535_set_piomode - PIO setup
95 * @ap: ATA interface
96 * @adev: device on the interface
97 *
98 * Set our PIO requirements. The CS5535 is pretty clean about all this
99 */
100
101static void cs5535_set_piomode(struct ata_port *ap, struct ata_device *adev)
102{
103 static const u16 pio_timings[5] = {
104 0xF7F4, 0x53F3, 0x13F1, 0x5131, 0x1131
105 };
106 static const u16 pio_cmd_timings[5] = {
107 0xF7F4, 0x53F3, 0x13F1, 0x5131, 0x1131
108 };
109 u32 reg, dummy;
110 struct ata_device *pair = ata_dev_pair(adev);
111
112 int mode = adev->pio_mode - XFER_PIO_0;
113 int cmdmode = mode;
114
115 /* Command timing has to be for the lowest of the pair of devices */
116 if (pair) {
117 int pairmode = pair->pio_mode - XFER_PIO_0;
118 cmdmode = min(mode, pairmode);
119 /* Write the other drive timing register if it changed */
120 if (cmdmode < pairmode)
121 wrmsr(ATAC_CH0D0_PIO + 2 * pair->devno,
122 pio_cmd_timings[cmdmode] << 16 | pio_timings[pairmode], 0);
123 }
124 /* Write the drive timing register */
125 wrmsr(ATAC_CH0D0_PIO + 2 * adev->devno,
126 pio_cmd_timings[cmdmode] << 16 | pio_timings[mode], 0);
127
128 /* Set the PIO "format 1" bit in the DMA timing register */
129 rdmsr(ATAC_CH0D0_DMA + 2 * adev->devno, reg, dummy);
130 wrmsr(ATAC_CH0D0_DMA + 2 * adev->devno, reg | 0x80000000UL, 0);
131}
132
133/**
134 * cs5535_set_dmamode - DMA timing setup
135 * @ap: ATA interface
136 * @adev: Device being configured
137 *
138 */
139
140static void cs5535_set_dmamode(struct ata_port *ap, struct ata_device *adev)
141{
142 static const u32 udma_timings[5] = {
143 0x7F7436A1, 0x7F733481, 0x7F723261, 0x7F713161, 0x7F703061
144 };
145 static const u32 mwdma_timings[3] = {
146 0x7F0FFFF3, 0x7F035352, 0x7F024241
147 };
148 u32 reg, dummy;
149 int mode = adev->dma_mode;
150
151 rdmsr(ATAC_CH0D0_DMA + 2 * adev->devno, reg, dummy);
152 reg &= 0x80000000UL;
153 if (mode >= XFER_UDMA_0)
154 reg |= udma_timings[mode - XFER_UDMA_0];
155 else
156 reg |= mwdma_timings[mode - XFER_MW_DMA_0];
157 wrmsr(ATAC_CH0D0_DMA + 2 * adev->devno, reg, 0);
158}
159
160static struct scsi_host_template cs5535_sht = {
Tejun Heo68d1d072008-03-25 12:22:49 +0900161 ATA_BMDMA_SHT(DRV_NAME),
Jeff Garzik669a5db2006-08-29 18:12:40 -0400162};
163
164static struct ata_port_operations cs5535_port_ops = {
Tejun Heo029cfd62008-03-25 12:22:49 +0900165 .inherits = &ata_bmdma_port_ops,
166 .cable_detect = cs5535_cable_detect,
Jeff Garzik669a5db2006-08-29 18:12:40 -0400167 .set_piomode = cs5535_set_piomode,
168 .set_dmamode = cs5535_set_dmamode,
Jeff Garzik669a5db2006-08-29 18:12:40 -0400169};
170
171/**
172 * cs5535_init_one - Initialise a CS5530
173 * @dev: PCI device
174 * @id: Entry in match table
175 *
176 * Install a driver for the newly found CS5530 companion chip. Most of
177 * this is just housekeeping. We have to set the chip up correctly and
178 * turn off various bits of emulation magic.
179 */
180
181static int cs5535_init_one(struct pci_dev *dev, const struct pci_device_id *id)
182{
Tejun Heo1626aeb2007-05-04 12:43:58 +0200183 static const struct ata_port_info info = {
Jeff Garzik1d2808f2007-05-28 06:59:48 -0400184 .flags = ATA_FLAG_SLAVE_POSS,
Jeff Garzik669a5db2006-08-29 18:12:40 -0400185 .pio_mask = 0x1f,
186 .mwdma_mask = 0x07,
Jeff Garzikbf6263a2007-07-09 12:16:50 -0400187 .udma_mask = ATA_UDMA4,
Jeff Garzik669a5db2006-08-29 18:12:40 -0400188 .port_ops = &cs5535_port_ops
189 };
Tejun Heo1626aeb2007-05-04 12:43:58 +0200190 const struct ata_port_info *ppi[] = { &info, &ata_dummy_port_info };
Jeff Garzik669a5db2006-08-29 18:12:40 -0400191
192 u32 timings, dummy;
193
194 /* Check the BIOS set the initial timing clock. If not set the
195 timings for PIO0 */
196 rdmsr(ATAC_CH0D0_PIO, timings, dummy);
197 if (CS5535_BAD_PIO(timings))
198 wrmsr(ATAC_CH0D0_PIO, 0xF7F4F7F4UL, 0);
199 rdmsr(ATAC_CH0D1_PIO, timings, dummy);
200 if (CS5535_BAD_PIO(timings))
201 wrmsr(ATAC_CH0D1_PIO, 0xF7F4F7F4UL, 0);
Tejun Heo9363c382008-04-07 22:47:16 +0900202 return ata_pci_sff_init_one(dev, ppi, &cs5535_sht, NULL);
Jeff Garzik669a5db2006-08-29 18:12:40 -0400203}
204
Jeff Garzik2d2744f2006-09-28 20:21:59 -0400205static const struct pci_device_id cs5535[] = {
206 { PCI_VDEVICE(NS, 0x002D), },
207
208 { },
Jeff Garzik669a5db2006-08-29 18:12:40 -0400209};
210
211static struct pci_driver cs5535_pci_driver = {
212 .name = DRV_NAME,
213 .id_table = cs5535,
214 .probe = cs5535_init_one,
Jeff Garzik3ac551a2006-12-03 07:57:21 -0500215 .remove = ata_pci_remove_one,
Tejun Heo438ac6d2007-03-02 17:31:26 +0900216#ifdef CONFIG_PM
Alan30ced0f2006-11-22 16:57:36 +0000217 .suspend = ata_pci_device_suspend,
218 .resume = ata_pci_device_resume,
Tejun Heo438ac6d2007-03-02 17:31:26 +0900219#endif
Jeff Garzik669a5db2006-08-29 18:12:40 -0400220};
221
222static int __init cs5535_init(void)
223{
224 return pci_register_driver(&cs5535_pci_driver);
225}
226
Jeff Garzik669a5db2006-08-29 18:12:40 -0400227static void __exit cs5535_exit(void)
228{
229 pci_unregister_driver(&cs5535_pci_driver);
230}
231
Jeff Garzik669a5db2006-08-29 18:12:40 -0400232MODULE_AUTHOR("Alan Cox, Jens Altmann, Wolfgan Zuleger, Alexander Kiausch");
233MODULE_DESCRIPTION("low-level driver for the NS/AMD 5530");
234MODULE_LICENSE("GPL");
235MODULE_DEVICE_TABLE(pci, cs5535);
236MODULE_VERSION(DRV_VERSION);
237
238module_init(cs5535_init);
239module_exit(cs5535_exit);