blob: 08cccc9c659b9cebbbe7ff13fe2c1a1cdd292d2c [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
4 * Alan Cox <alan@redhat.com>
5 *
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
28 * Review errata to see if serializing is neccessary
29 */
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"
Alan30ced0f2006-11-22 16:57:36 +000042#define DRV_VERSION "0.2.11"
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
75 *
76 * Perform cable detection for ATA66 capable cable. Return a libata
77 * cable type.
78 */
79
Alan Coxeb4a2c72007-04-11 00:04:20 +010080static int cs5535_cable_detect(struct ata_port *ap)
Jeff Garzik669a5db2006-08-29 18:12:40 -040081{
82 u8 cable;
83 struct pci_dev *pdev = to_pci_dev(ap->host->dev);
84
85 pci_read_config_byte(pdev, CS5535_CABLE_DETECT, &cable);
86 if (cable & 1)
Alan Coxeb4a2c72007-04-11 00:04:20 +010087 return ATA_CBL_PATA80;
Jeff Garzik669a5db2006-08-29 18:12:40 -040088 else
Alan Coxeb4a2c72007-04-11 00:04:20 +010089 return ATA_CBL_PATA40;
Jeff Garzik669a5db2006-08-29 18:12:40 -040090}
91
92/**
93 * cs5535_set_piomode - PIO setup
94 * @ap: ATA interface
95 * @adev: device on the interface
96 *
97 * Set our PIO requirements. The CS5535 is pretty clean about all this
98 */
99
100static void cs5535_set_piomode(struct ata_port *ap, struct ata_device *adev)
101{
102 static const u16 pio_timings[5] = {
103 0xF7F4, 0x53F3, 0x13F1, 0x5131, 0x1131
104 };
105 static const u16 pio_cmd_timings[5] = {
106 0xF7F4, 0x53F3, 0x13F1, 0x5131, 0x1131
107 };
108 u32 reg, dummy;
109 struct ata_device *pair = ata_dev_pair(adev);
110
111 int mode = adev->pio_mode - XFER_PIO_0;
112 int cmdmode = mode;
113
114 /* Command timing has to be for the lowest of the pair of devices */
115 if (pair) {
116 int pairmode = pair->pio_mode - XFER_PIO_0;
117 cmdmode = min(mode, pairmode);
118 /* Write the other drive timing register if it changed */
119 if (cmdmode < pairmode)
120 wrmsr(ATAC_CH0D0_PIO + 2 * pair->devno,
121 pio_cmd_timings[cmdmode] << 16 | pio_timings[pairmode], 0);
122 }
123 /* Write the drive timing register */
124 wrmsr(ATAC_CH0D0_PIO + 2 * adev->devno,
125 pio_cmd_timings[cmdmode] << 16 | pio_timings[mode], 0);
126
127 /* Set the PIO "format 1" bit in the DMA timing register */
128 rdmsr(ATAC_CH0D0_DMA + 2 * adev->devno, reg, dummy);
129 wrmsr(ATAC_CH0D0_DMA + 2 * adev->devno, reg | 0x80000000UL, 0);
130}
131
132/**
133 * cs5535_set_dmamode - DMA timing setup
134 * @ap: ATA interface
135 * @adev: Device being configured
136 *
137 */
138
139static void cs5535_set_dmamode(struct ata_port *ap, struct ata_device *adev)
140{
141 static const u32 udma_timings[5] = {
142 0x7F7436A1, 0x7F733481, 0x7F723261, 0x7F713161, 0x7F703061
143 };
144 static const u32 mwdma_timings[3] = {
145 0x7F0FFFF3, 0x7F035352, 0x7F024241
146 };
147 u32 reg, dummy;
148 int mode = adev->dma_mode;
149
150 rdmsr(ATAC_CH0D0_DMA + 2 * adev->devno, reg, dummy);
151 reg &= 0x80000000UL;
152 if (mode >= XFER_UDMA_0)
153 reg |= udma_timings[mode - XFER_UDMA_0];
154 else
155 reg |= mwdma_timings[mode - XFER_MW_DMA_0];
156 wrmsr(ATAC_CH0D0_DMA + 2 * adev->devno, reg, 0);
157}
158
159static struct scsi_host_template cs5535_sht = {
160 .module = THIS_MODULE,
161 .name = DRV_NAME,
162 .ioctl = ata_scsi_ioctl,
163 .queuecommand = ata_scsi_queuecmd,
164 .can_queue = ATA_DEF_QUEUE,
165 .this_id = ATA_SHT_THIS_ID,
166 .sg_tablesize = LIBATA_MAX_PRD,
Jeff Garzik669a5db2006-08-29 18:12:40 -0400167 .cmd_per_lun = ATA_SHT_CMD_PER_LUN,
168 .emulated = ATA_SHT_EMULATED,
169 .use_clustering = ATA_SHT_USE_CLUSTERING,
170 .proc_name = DRV_NAME,
171 .dma_boundary = ATA_DMA_BOUNDARY,
172 .slave_configure = ata_scsi_slave_config,
Tejun Heoafdfe892006-11-29 11:26:47 +0900173 .slave_destroy = ata_scsi_slave_destroy,
Jeff Garzik669a5db2006-08-29 18:12:40 -0400174 .bios_param = ata_std_bios_param,
Tejun Heo438ac6d2007-03-02 17:31:26 +0900175#ifdef CONFIG_PM
Alan30ced0f2006-11-22 16:57:36 +0000176 .resume = ata_scsi_device_resume,
177 .suspend = ata_scsi_device_suspend,
Tejun Heo438ac6d2007-03-02 17:31:26 +0900178#endif
Jeff Garzik669a5db2006-08-29 18:12:40 -0400179};
180
181static struct ata_port_operations cs5535_port_ops = {
182 .port_disable = ata_port_disable,
183 .set_piomode = cs5535_set_piomode,
184 .set_dmamode = cs5535_set_dmamode,
185 .mode_filter = ata_pci_default_filter,
186
187 .tf_load = ata_tf_load,
188 .tf_read = ata_tf_read,
189 .check_status = ata_check_status,
190 .exec_command = ata_exec_command,
191 .dev_select = ata_std_dev_select,
192
193 .freeze = ata_bmdma_freeze,
194 .thaw = ata_bmdma_thaw,
Alan Coxeb4a2c72007-04-11 00:04:20 +0100195 .error_handler = ata_bmdma_error_handler,
Jeff Garzik669a5db2006-08-29 18:12:40 -0400196 .post_internal_cmd = ata_bmdma_post_internal_cmd,
Alan Coxeb4a2c72007-04-11 00:04:20 +0100197 .cable_detect = cs5535_cable_detect,
Jeff Garzik669a5db2006-08-29 18:12:40 -0400198
199 .bmdma_setup = ata_bmdma_setup,
200 .bmdma_start = ata_bmdma_start,
201 .bmdma_stop = ata_bmdma_stop,
202 .bmdma_status = ata_bmdma_status,
203
204 .qc_prep = ata_qc_prep,
205 .qc_issue = ata_qc_issue_prot,
Jeff Garzikbda30282006-09-27 05:41:13 -0400206
Tejun Heo0d5ff562007-02-01 15:06:36 +0900207 .data_xfer = ata_data_xfer,
Jeff Garzik669a5db2006-08-29 18:12:40 -0400208
209 .irq_handler = ata_interrupt,
210 .irq_clear = ata_bmdma_irq_clear,
Akira Iguchi246ce3b2007-01-26 16:27:58 +0900211 .irq_on = ata_irq_on,
212 .irq_ack = ata_irq_ack,
Jeff Garzik669a5db2006-08-29 18:12:40 -0400213
214 .port_start = ata_port_start,
Jeff Garzik669a5db2006-08-29 18:12:40 -0400215};
216
217/**
218 * cs5535_init_one - Initialise a CS5530
219 * @dev: PCI device
220 * @id: Entry in match table
221 *
222 * Install a driver for the newly found CS5530 companion chip. Most of
223 * this is just housekeeping. We have to set the chip up correctly and
224 * turn off various bits of emulation magic.
225 */
226
227static int cs5535_init_one(struct pci_dev *dev, const struct pci_device_id *id)
228{
229 static struct ata_port_info info = {
230 .sht = &cs5535_sht,
231 .flags = ATA_FLAG_SLAVE_POSS|ATA_FLAG_SRST,
232 .pio_mask = 0x1f,
233 .mwdma_mask = 0x07,
234 .udma_mask = 0x1f,
235 .port_ops = &cs5535_port_ops
236 };
237 struct ata_port_info *ports[1] = { &info };
238
239 u32 timings, dummy;
240
241 /* Check the BIOS set the initial timing clock. If not set the
242 timings for PIO0 */
243 rdmsr(ATAC_CH0D0_PIO, timings, dummy);
244 if (CS5535_BAD_PIO(timings))
245 wrmsr(ATAC_CH0D0_PIO, 0xF7F4F7F4UL, 0);
246 rdmsr(ATAC_CH0D1_PIO, timings, dummy);
247 if (CS5535_BAD_PIO(timings))
248 wrmsr(ATAC_CH0D1_PIO, 0xF7F4F7F4UL, 0);
249 return ata_pci_init_one(dev, ports, 1);
250}
251
Jeff Garzik2d2744f2006-09-28 20:21:59 -0400252static const struct pci_device_id cs5535[] = {
253 { PCI_VDEVICE(NS, 0x002D), },
254
255 { },
Jeff Garzik669a5db2006-08-29 18:12:40 -0400256};
257
258static struct pci_driver cs5535_pci_driver = {
259 .name = DRV_NAME,
260 .id_table = cs5535,
261 .probe = cs5535_init_one,
Jeff Garzik3ac551a2006-12-03 07:57:21 -0500262 .remove = ata_pci_remove_one,
Tejun Heo438ac6d2007-03-02 17:31:26 +0900263#ifdef CONFIG_PM
Alan30ced0f2006-11-22 16:57:36 +0000264 .suspend = ata_pci_device_suspend,
265 .resume = ata_pci_device_resume,
Tejun Heo438ac6d2007-03-02 17:31:26 +0900266#endif
Jeff Garzik669a5db2006-08-29 18:12:40 -0400267};
268
269static int __init cs5535_init(void)
270{
271 return pci_register_driver(&cs5535_pci_driver);
272}
273
Jeff Garzik669a5db2006-08-29 18:12:40 -0400274static void __exit cs5535_exit(void)
275{
276 pci_unregister_driver(&cs5535_pci_driver);
277}
278
Jeff Garzik669a5db2006-08-29 18:12:40 -0400279MODULE_AUTHOR("Alan Cox, Jens Altmann, Wolfgan Zuleger, Alexander Kiausch");
280MODULE_DESCRIPTION("low-level driver for the NS/AMD 5530");
281MODULE_LICENSE("GPL");
282MODULE_DEVICE_TABLE(pci, cs5535);
283MODULE_VERSION(DRV_VERSION);
284
285module_init(cs5535_init);
286module_exit(cs5535_exit);