blob: 8daf5d67dfe2fce3ec34bb1b6ba440999f3b71ea [file] [log] [blame]
Linus Torvalds1da177e2005-04-16 15:20:36 -07001/*
2 * sata_promise.c - Promise SATA
3 *
4 * Maintained by: Jeff Garzik <jgarzik@pobox.com>
5 * Please ALWAYS copy linux-ide@vger.kernel.org
6 * on emails.
7 *
8 * Copyright 2003-2004 Red Hat, Inc.
9 *
Linus Torvalds1da177e2005-04-16 15:20:36 -070010 *
Jeff Garzikaf36d7f2005-08-28 20:18:39 -040011 * This program is free software; you can redistribute it and/or modify
12 * it under the terms of the GNU General Public License as published by
13 * the Free Software Foundation; either version 2, or (at your option)
14 * any later version.
15 *
16 * This program is distributed in the hope that it will be useful,
17 * but WITHOUT ANY WARRANTY; without even the implied warranty of
18 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
19 * GNU General Public License for more details.
20 *
21 * You should have received a copy of the GNU General Public License
22 * along with this program; see the file COPYING. If not, write to
23 * the Free Software Foundation, 675 Mass Ave, Cambridge, MA 02139, USA.
24 *
25 *
26 * libata documentation is available via 'make {ps|pdf}docs',
27 * as Documentation/DocBook/libata.*
28 *
29 * Hardware information only available under NDA.
Linus Torvalds1da177e2005-04-16 15:20:36 -070030 *
31 */
32
33#include <linux/kernel.h>
34#include <linux/module.h>
35#include <linux/pci.h>
36#include <linux/init.h>
37#include <linux/blkdev.h>
38#include <linux/delay.h>
39#include <linux/interrupt.h>
40#include <linux/sched.h>
Jeff Garzika9524a72005-10-30 14:39:11 -050041#include <linux/device.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070042#include <scsi/scsi_host.h>
Jeff Garzik193515d2005-11-07 00:59:37 -050043#include <scsi/scsi_cmnd.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070044#include <linux/libata.h>
45#include <asm/io.h>
46#include "sata_promise.h"
47
48#define DRV_NAME "sata_promise"
Jeff Garzik46b027c2006-11-14 14:46:17 -050049#define DRV_VERSION "1.05"
Linus Torvalds1da177e2005-04-16 15:20:36 -070050
51
52enum {
53 PDC_PKT_SUBMIT = 0x40, /* Command packet pointer addr */
54 PDC_INT_SEQMASK = 0x40, /* Mask of asserted SEQ INTs */
Linus Torvalds1da177e2005-04-16 15:20:36 -070055 PDC_FLASH_CTL = 0x44, /* Flash control register */
Linus Torvalds1da177e2005-04-16 15:20:36 -070056 PDC_GLOBAL_CTL = 0x48, /* Global control/status (per port) */
57 PDC_CTLSTAT = 0x60, /* IDE control and status (per port) */
58 PDC_SATA_PLUG_CSR = 0x6C, /* SATA Plug control/status reg */
Luke Kosewski6340f012006-01-28 12:39:29 -050059 PDC2_SATA_PLUG_CSR = 0x60, /* SATAII Plug control/status reg */
Mikael Petterssonb2d1eee2006-11-22 22:00:15 +010060 PDC_TBG_MODE = 0x41C, /* TBG mode (not SATAII) */
61 PDC_SLEW_CTL = 0x470, /* slew rate control reg (not SATAII) */
Linus Torvalds1da177e2005-04-16 15:20:36 -070062
63 PDC_ERR_MASK = (1<<19) | (1<<20) | (1<<21) | (1<<22) |
64 (1<<8) | (1<<9) | (1<<10),
65
66 board_2037x = 0, /* FastTrak S150 TX2plus */
67 board_20319 = 1, /* FastTrak S150 TX4 */
Tobias Lorenzf497ba72005-05-12 15:51:01 -040068 board_20619 = 2, /* FastTrak TX4000 */
Yusuf Iskenderoglu5a46fe82006-01-17 08:06:21 -050069 board_20771 = 3, /* FastTrak TX2300 */
Luke Kosewski6340f012006-01-28 12:39:29 -050070 board_2057x = 4, /* SATAII150 Tx2plus */
71 board_40518 = 5, /* SATAII150 Tx4 */
Linus Torvalds1da177e2005-04-16 15:20:36 -070072
Luke Kosewski6340f012006-01-28 12:39:29 -050073 PDC_HAS_PATA = (1 << 1), /* PDC20375/20575 has PATA */
Linus Torvalds1da177e2005-04-16 15:20:36 -070074
75 PDC_RESET = (1 << 11), /* HDMA reset */
Jeff Garzik50630192005-12-13 02:29:45 -050076
77 PDC_COMMON_FLAGS = ATA_FLAG_NO_LEGACY | ATA_FLAG_SRST |
Jeff Garzik3d0a59c2005-12-13 22:28:19 -050078 ATA_FLAG_MMIO | ATA_FLAG_NO_ATAPI |
79 ATA_FLAG_PIO_POLLING,
Mikael Petterssonb2d1eee2006-11-22 22:00:15 +010080
81 /* hp->flags bits */
82 PDC_FLAG_GEN_II = (1 << 0),
Linus Torvalds1da177e2005-04-16 15:20:36 -070083};
84
85
86struct pdc_port_priv {
87 u8 *pkt;
88 dma_addr_t pkt_dma;
89};
90
Luke Kosewski6340f012006-01-28 12:39:29 -050091struct pdc_host_priv {
Mikael Petterssonb2d1eee2006-11-22 22:00:15 +010092 unsigned long flags;
Luke Kosewski6340f012006-01-28 12:39:29 -050093 int hotplug_offset;
94};
95
Linus Torvalds1da177e2005-04-16 15:20:36 -070096static u32 pdc_sata_scr_read (struct ata_port *ap, unsigned int sc_reg);
97static void pdc_sata_scr_write (struct ata_port *ap, unsigned int sc_reg, u32 val);
98static int pdc_ata_init_one (struct pci_dev *pdev, const struct pci_device_id *ent);
David Howells7d12e782006-10-05 14:55:46 +010099static irqreturn_t pdc_interrupt (int irq, void *dev_instance);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700100static void pdc_eng_timeout(struct ata_port *ap);
101static int pdc_port_start(struct ata_port *ap);
102static void pdc_port_stop(struct ata_port *ap);
Jeff Garzik2cba5822005-08-29 05:12:30 -0400103static void pdc_pata_phy_reset(struct ata_port *ap);
104static void pdc_sata_phy_reset(struct ata_port *ap);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700105static void pdc_qc_prep(struct ata_queued_cmd *qc);
Jeff Garzik057ace52005-10-22 14:27:05 -0400106static void pdc_tf_load_mmio(struct ata_port *ap, const struct ata_taskfile *tf);
107static void pdc_exec_command_mmio(struct ata_port *ap, const struct ata_taskfile *tf);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700108static void pdc_irq_clear(struct ata_port *ap);
Tejun Heo9a3d9eb2006-01-23 13:09:36 +0900109static unsigned int pdc_qc_issue_prot(struct ata_queued_cmd *qc);
Jeff Garzikcca39742006-08-24 03:19:22 -0400110static void pdc_host_stop(struct ata_host *host);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700111
Jeff Garzik374b1872005-08-30 05:42:52 -0400112
Jeff Garzik193515d2005-11-07 00:59:37 -0500113static struct scsi_host_template pdc_ata_sht = {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700114 .module = THIS_MODULE,
115 .name = DRV_NAME,
116 .ioctl = ata_scsi_ioctl,
117 .queuecommand = ata_scsi_queuecmd,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700118 .can_queue = ATA_DEF_QUEUE,
119 .this_id = ATA_SHT_THIS_ID,
120 .sg_tablesize = LIBATA_MAX_PRD,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700121 .cmd_per_lun = ATA_SHT_CMD_PER_LUN,
122 .emulated = ATA_SHT_EMULATED,
123 .use_clustering = ATA_SHT_USE_CLUSTERING,
124 .proc_name = DRV_NAME,
125 .dma_boundary = ATA_DMA_BOUNDARY,
126 .slave_configure = ata_scsi_slave_config,
Tejun Heoccf68c32006-05-31 18:28:09 +0900127 .slave_destroy = ata_scsi_slave_destroy,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700128 .bios_param = ata_std_bios_param,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700129};
130
Jeff Garzik057ace52005-10-22 14:27:05 -0400131static const struct ata_port_operations pdc_sata_ops = {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700132 .port_disable = ata_port_disable,
133 .tf_load = pdc_tf_load_mmio,
134 .tf_read = ata_tf_read,
135 .check_status = ata_check_status,
136 .exec_command = pdc_exec_command_mmio,
137 .dev_select = ata_std_dev_select,
Jeff Garzik2cba5822005-08-29 05:12:30 -0400138
139 .phy_reset = pdc_sata_phy_reset,
140
Linus Torvalds1da177e2005-04-16 15:20:36 -0700141 .qc_prep = pdc_qc_prep,
142 .qc_issue = pdc_qc_issue_prot,
143 .eng_timeout = pdc_eng_timeout,
Alan Coxa6b2c5d2006-05-22 16:59:59 +0100144 .data_xfer = ata_mmio_data_xfer,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700145 .irq_handler = pdc_interrupt,
146 .irq_clear = pdc_irq_clear,
Jeff Garzik2cba5822005-08-29 05:12:30 -0400147
Linus Torvalds1da177e2005-04-16 15:20:36 -0700148 .scr_read = pdc_sata_scr_read,
149 .scr_write = pdc_sata_scr_write,
150 .port_start = pdc_port_start,
151 .port_stop = pdc_port_stop,
Luke Kosewski6340f012006-01-28 12:39:29 -0500152 .host_stop = pdc_host_stop,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700153};
154
Jeff Garzik057ace52005-10-22 14:27:05 -0400155static const struct ata_port_operations pdc_pata_ops = {
Jeff Garzik2cba5822005-08-29 05:12:30 -0400156 .port_disable = ata_port_disable,
157 .tf_load = pdc_tf_load_mmio,
158 .tf_read = ata_tf_read,
159 .check_status = ata_check_status,
160 .exec_command = pdc_exec_command_mmio,
161 .dev_select = ata_std_dev_select,
162
163 .phy_reset = pdc_pata_phy_reset,
164
165 .qc_prep = pdc_qc_prep,
166 .qc_issue = pdc_qc_issue_prot,
Alan Coxa6b2c5d2006-05-22 16:59:59 +0100167 .data_xfer = ata_mmio_data_xfer,
Jeff Garzik2cba5822005-08-29 05:12:30 -0400168 .eng_timeout = pdc_eng_timeout,
169 .irq_handler = pdc_interrupt,
170 .irq_clear = pdc_irq_clear,
171
172 .port_start = pdc_port_start,
173 .port_stop = pdc_port_stop,
Luke Kosewski6340f012006-01-28 12:39:29 -0500174 .host_stop = pdc_host_stop,
Jeff Garzik2cba5822005-08-29 05:12:30 -0400175};
176
Arjan van de Ven98ac62d2005-11-28 10:06:23 +0100177static const struct ata_port_info pdc_port_info[] = {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700178 /* board_2037x */
179 {
180 .sht = &pdc_ata_sht,
Jeff Garzikcca39742006-08-24 03:19:22 -0400181 .flags = PDC_COMMON_FLAGS | ATA_FLAG_SATA,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700182 .pio_mask = 0x1f, /* pio0-4 */
183 .mwdma_mask = 0x07, /* mwdma0-2 */
184 .udma_mask = 0x7f, /* udma0-6 ; FIXME */
Jeff Garzik2cba5822005-08-29 05:12:30 -0400185 .port_ops = &pdc_sata_ops,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700186 },
187
188 /* board_20319 */
189 {
190 .sht = &pdc_ata_sht,
Jeff Garzikcca39742006-08-24 03:19:22 -0400191 .flags = PDC_COMMON_FLAGS | ATA_FLAG_SATA,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700192 .pio_mask = 0x1f, /* pio0-4 */
193 .mwdma_mask = 0x07, /* mwdma0-2 */
194 .udma_mask = 0x7f, /* udma0-6 ; FIXME */
Jeff Garzik2cba5822005-08-29 05:12:30 -0400195 .port_ops = &pdc_sata_ops,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700196 },
Tobias Lorenzf497ba72005-05-12 15:51:01 -0400197
198 /* board_20619 */
199 {
200 .sht = &pdc_ata_sht,
Jeff Garzikcca39742006-08-24 03:19:22 -0400201 .flags = PDC_COMMON_FLAGS | ATA_FLAG_SLAVE_POSS,
Tobias Lorenzf497ba72005-05-12 15:51:01 -0400202 .pio_mask = 0x1f, /* pio0-4 */
203 .mwdma_mask = 0x07, /* mwdma0-2 */
204 .udma_mask = 0x7f, /* udma0-6 ; FIXME */
Jeff Garzik2cba5822005-08-29 05:12:30 -0400205 .port_ops = &pdc_pata_ops,
Tobias Lorenzf497ba72005-05-12 15:51:01 -0400206 },
Yusuf Iskenderoglu5a46fe82006-01-17 08:06:21 -0500207
208 /* board_20771 */
209 {
210 .sht = &pdc_ata_sht,
Jeff Garzikcca39742006-08-24 03:19:22 -0400211 .flags = PDC_COMMON_FLAGS | ATA_FLAG_SATA,
Yusuf Iskenderoglu5a46fe82006-01-17 08:06:21 -0500212 .pio_mask = 0x1f, /* pio0-4 */
213 .mwdma_mask = 0x07, /* mwdma0-2 */
214 .udma_mask = 0x7f, /* udma0-6 ; FIXME */
215 .port_ops = &pdc_sata_ops,
216 },
Luke Kosewski6340f012006-01-28 12:39:29 -0500217
218 /* board_2057x */
219 {
220 .sht = &pdc_ata_sht,
Jeff Garzikcca39742006-08-24 03:19:22 -0400221 .flags = PDC_COMMON_FLAGS | ATA_FLAG_SATA,
Luke Kosewski6340f012006-01-28 12:39:29 -0500222 .pio_mask = 0x1f, /* pio0-4 */
223 .mwdma_mask = 0x07, /* mwdma0-2 */
224 .udma_mask = 0x7f, /* udma0-6 ; FIXME */
225 .port_ops = &pdc_sata_ops,
226 },
227
228 /* board_40518 */
229 {
230 .sht = &pdc_ata_sht,
Jeff Garzikcca39742006-08-24 03:19:22 -0400231 .flags = PDC_COMMON_FLAGS | ATA_FLAG_SATA,
Luke Kosewski6340f012006-01-28 12:39:29 -0500232 .pio_mask = 0x1f, /* pio0-4 */
233 .mwdma_mask = 0x07, /* mwdma0-2 */
234 .udma_mask = 0x7f, /* udma0-6 ; FIXME */
235 .port_ops = &pdc_sata_ops,
236 },
Linus Torvalds1da177e2005-04-16 15:20:36 -0700237};
238
Jeff Garzik3b7d6972005-11-10 11:04:11 -0500239static const struct pci_device_id pdc_ata_pci_tbl[] = {
Jeff Garzik54bb3a942006-09-27 22:20:11 -0400240 { PCI_VDEVICE(PROMISE, 0x3371), board_2037x },
Jeff Garzik54bb3a942006-09-27 22:20:11 -0400241 { PCI_VDEVICE(PROMISE, 0x3373), board_2037x },
242 { PCI_VDEVICE(PROMISE, 0x3375), board_2037x },
243 { PCI_VDEVICE(PROMISE, 0x3376), board_2037x },
Mikael Petterssonb2d1eee2006-11-22 22:00:15 +0100244 { PCI_VDEVICE(PROMISE, 0x3570), board_2057x },
245 { PCI_VDEVICE(PROMISE, 0x3571), board_2057x },
Jeff Garzik54bb3a942006-09-27 22:20:11 -0400246 { PCI_VDEVICE(PROMISE, 0x3574), board_2057x },
Mikael Petterssonb2d1eee2006-11-22 22:00:15 +0100247 { PCI_VDEVICE(PROMISE, 0x3d73), board_2057x },
Jeff Garzik54bb3a942006-09-27 22:20:11 -0400248 { PCI_VDEVICE(PROMISE, 0x3d75), board_2057x },
Linus Torvalds1da177e2005-04-16 15:20:36 -0700249
Jeff Garzik54bb3a942006-09-27 22:20:11 -0400250 { PCI_VDEVICE(PROMISE, 0x3318), board_20319 },
251 { PCI_VDEVICE(PROMISE, 0x3319), board_20319 },
252 { PCI_VDEVICE(PROMISE, 0x3515), board_20319 },
253 { PCI_VDEVICE(PROMISE, 0x3519), board_20319 },
Mikael Petterssonb2d1eee2006-11-22 22:00:15 +0100254 { PCI_VDEVICE(PROMISE, 0x3d17), board_40518 },
Jeff Garzik54bb3a942006-09-27 22:20:11 -0400255 { PCI_VDEVICE(PROMISE, 0x3d18), board_40518 },
Linus Torvalds1da177e2005-04-16 15:20:36 -0700256
Jeff Garzik54bb3a942006-09-27 22:20:11 -0400257 { PCI_VDEVICE(PROMISE, 0x6629), board_20619 },
Tobias Lorenzf497ba72005-05-12 15:51:01 -0400258
Jeff Garzik8419dc82006-07-24 03:37:52 -0400259/* TODO: remove all associated board_20771 code, as it completely
260 * duplicates board_2037x code, unless reason for separation can be
261 * divined.
262 */
263#if 0
Jeff Garzik54bb3a942006-09-27 22:20:11 -0400264 { PCI_VDEVICE(PROMISE, 0x3570), board_20771 },
Jeff Garzik8419dc82006-07-24 03:37:52 -0400265#endif
Jeff Garzika83068b2006-10-11 04:46:52 -0400266 { PCI_VDEVICE(PROMISE, 0x3577), board_20771 },
Jeff Garzik8419dc82006-07-24 03:37:52 -0400267
Linus Torvalds1da177e2005-04-16 15:20:36 -0700268 { } /* terminate list */
269};
270
271
272static struct pci_driver pdc_ata_pci_driver = {
273 .name = DRV_NAME,
274 .id_table = pdc_ata_pci_tbl,
275 .probe = pdc_ata_init_one,
276 .remove = ata_pci_remove_one,
277};
278
279
280static int pdc_port_start(struct ata_port *ap)
281{
Jeff Garzikcca39742006-08-24 03:19:22 -0400282 struct device *dev = ap->host->dev;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700283 struct pdc_port_priv *pp;
284 int rc;
285
286 rc = ata_port_start(ap);
287 if (rc)
288 return rc;
289
Luke Kosewski6340f012006-01-28 12:39:29 -0500290 pp = kzalloc(sizeof(*pp), GFP_KERNEL);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700291 if (!pp) {
292 rc = -ENOMEM;
293 goto err_out;
294 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700295
296 pp->pkt = dma_alloc_coherent(dev, 128, &pp->pkt_dma, GFP_KERNEL);
297 if (!pp->pkt) {
298 rc = -ENOMEM;
299 goto err_out_kfree;
300 }
301
302 ap->private_data = pp;
303
304 return 0;
305
306err_out_kfree:
307 kfree(pp);
308err_out:
309 ata_port_stop(ap);
310 return rc;
311}
312
313
314static void pdc_port_stop(struct ata_port *ap)
315{
Jeff Garzikcca39742006-08-24 03:19:22 -0400316 struct device *dev = ap->host->dev;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700317 struct pdc_port_priv *pp = ap->private_data;
318
319 ap->private_data = NULL;
320 dma_free_coherent(dev, 128, pp->pkt, pp->pkt_dma);
321 kfree(pp);
322 ata_port_stop(ap);
323}
324
325
Jeff Garzikcca39742006-08-24 03:19:22 -0400326static void pdc_host_stop(struct ata_host *host)
Luke Kosewski6340f012006-01-28 12:39:29 -0500327{
Jeff Garzikcca39742006-08-24 03:19:22 -0400328 struct pdc_host_priv *hp = host->private_data;
Luke Kosewski6340f012006-01-28 12:39:29 -0500329
Jeff Garzikcca39742006-08-24 03:19:22 -0400330 ata_pci_host_stop(host);
Luke Kosewski6340f012006-01-28 12:39:29 -0500331
332 kfree(hp);
333}
334
335
Linus Torvalds1da177e2005-04-16 15:20:36 -0700336static void pdc_reset_port(struct ata_port *ap)
337{
Jeff Garzikea6ba102005-08-30 05:18:18 -0400338 void __iomem *mmio = (void __iomem *) ap->ioaddr.cmd_addr + PDC_CTLSTAT;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700339 unsigned int i;
340 u32 tmp;
341
342 for (i = 11; i > 0; i--) {
343 tmp = readl(mmio);
344 if (tmp & PDC_RESET)
345 break;
346
347 udelay(100);
348
349 tmp |= PDC_RESET;
350 writel(tmp, mmio);
351 }
352
353 tmp &= ~PDC_RESET;
354 writel(tmp, mmio);
355 readl(mmio); /* flush */
356}
357
Jeff Garzik2cba5822005-08-29 05:12:30 -0400358static void pdc_sata_phy_reset(struct ata_port *ap)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700359{
360 pdc_reset_port(ap);
361 sata_phy_reset(ap);
362}
363
Jeff Garzikd3fb4e82006-05-24 01:43:25 -0400364static void pdc_pata_cbl_detect(struct ata_port *ap)
365{
366 u8 tmp;
Al Viro03dc5502006-10-10 22:48:07 +0100367 void __iomem *mmio = (void __iomem *) ap->ioaddr.cmd_addr + PDC_CTLSTAT + 0x03;
Jeff Garzikd3fb4e82006-05-24 01:43:25 -0400368
369 tmp = readb(mmio);
370
371 if (tmp & 0x01) {
372 ap->cbl = ATA_CBL_PATA40;
373 ap->udma_mask &= ATA_UDMA_MASK_40C;
374 } else
375 ap->cbl = ATA_CBL_PATA80;
376}
377
Jeff Garzik2cba5822005-08-29 05:12:30 -0400378static void pdc_pata_phy_reset(struct ata_port *ap)
379{
Jeff Garzikd3fb4e82006-05-24 01:43:25 -0400380 pdc_pata_cbl_detect(ap);
Jeff Garzik2cba5822005-08-29 05:12:30 -0400381 pdc_reset_port(ap);
382 ata_port_probe(ap);
383 ata_bus_reset(ap);
384}
385
Linus Torvalds1da177e2005-04-16 15:20:36 -0700386static u32 pdc_sata_scr_read (struct ata_port *ap, unsigned int sc_reg)
387{
388 if (sc_reg > SCR_CONTROL)
389 return 0xffffffffU;
Al Virob181d3b2005-10-21 06:46:02 +0100390 return readl((void __iomem *) ap->ioaddr.scr_addr + (sc_reg * 4));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700391}
392
393
394static void pdc_sata_scr_write (struct ata_port *ap, unsigned int sc_reg,
395 u32 val)
396{
397 if (sc_reg > SCR_CONTROL)
398 return;
Al Virob181d3b2005-10-21 06:46:02 +0100399 writel(val, (void __iomem *) ap->ioaddr.scr_addr + (sc_reg * 4));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700400}
401
402static void pdc_qc_prep(struct ata_queued_cmd *qc)
403{
404 struct pdc_port_priv *pp = qc->ap->private_data;
405 unsigned int i;
406
407 VPRINTK("ENTER\n");
408
409 switch (qc->tf.protocol) {
410 case ATA_PROT_DMA:
411 ata_qc_prep(qc);
412 /* fall through */
413
414 case ATA_PROT_NODATA:
415 i = pdc_pkt_header(&qc->tf, qc->ap->prd_dma,
416 qc->dev->devno, pp->pkt);
417
418 if (qc->tf.flags & ATA_TFLAG_LBA48)
419 i = pdc_prep_lba48(&qc->tf, pp->pkt, i);
420 else
421 i = pdc_prep_lba28(&qc->tf, pp->pkt, i);
422
423 pdc_pkt_footer(&qc->tf, pp->pkt, i);
424 break;
425
426 default:
427 break;
428 }
429}
430
431static void pdc_eng_timeout(struct ata_port *ap)
432{
Jeff Garzikcca39742006-08-24 03:19:22 -0400433 struct ata_host *host = ap->host;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700434 u8 drv_stat;
435 struct ata_queued_cmd *qc;
Jeff Garzikb8f61532005-08-25 22:01:20 -0400436 unsigned long flags;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700437
438 DPRINTK("ENTER\n");
439
Jeff Garzikcca39742006-08-24 03:19:22 -0400440 spin_lock_irqsave(&host->lock, flags);
Jeff Garzikb8f61532005-08-25 22:01:20 -0400441
Linus Torvalds1da177e2005-04-16 15:20:36 -0700442 qc = ata_qc_from_tag(ap, ap->active_tag);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700443
Linus Torvalds1da177e2005-04-16 15:20:36 -0700444 switch (qc->tf.protocol) {
445 case ATA_PROT_DMA:
446 case ATA_PROT_NODATA:
Tejun Heof15a1da2006-05-15 20:57:56 +0900447 ata_port_printk(ap, KERN_ERR, "command timeout\n");
Jeff Garzika7dac442005-10-30 04:44:42 -0500448 drv_stat = ata_wait_idle(ap);
Albert Leea22e2eb2005-12-05 15:38:02 +0800449 qc->err_mask |= __ac_err_mask(drv_stat);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700450 break;
451
452 default:
453 drv_stat = ata_busy_wait(ap, ATA_BUSY | ATA_DRQ, 1000);
454
Tejun Heof15a1da2006-05-15 20:57:56 +0900455 ata_port_printk(ap, KERN_ERR,
456 "unknown timeout, cmd 0x%x stat 0x%x\n",
457 qc->tf.command, drv_stat);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700458
Albert Leea22e2eb2005-12-05 15:38:02 +0800459 qc->err_mask |= ac_err_mask(drv_stat);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700460 break;
461 }
462
Jeff Garzikcca39742006-08-24 03:19:22 -0400463 spin_unlock_irqrestore(&host->lock, flags);
Tejun Heof6379022006-02-10 15:10:48 +0900464 ata_eh_qc_complete(qc);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700465 DPRINTK("EXIT\n");
466}
467
468static inline unsigned int pdc_host_intr( struct ata_port *ap,
469 struct ata_queued_cmd *qc)
470{
Albert Leea22e2eb2005-12-05 15:38:02 +0800471 unsigned int handled = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700472 u32 tmp;
Jeff Garzikea6ba102005-08-30 05:18:18 -0400473 void __iomem *mmio = (void __iomem *) ap->ioaddr.cmd_addr + PDC_GLOBAL_CTL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700474
475 tmp = readl(mmio);
476 if (tmp & PDC_ERR_MASK) {
Albert Leea22e2eb2005-12-05 15:38:02 +0800477 qc->err_mask |= AC_ERR_DEV;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700478 pdc_reset_port(ap);
479 }
480
481 switch (qc->tf.protocol) {
482 case ATA_PROT_DMA:
483 case ATA_PROT_NODATA:
Albert Leea22e2eb2005-12-05 15:38:02 +0800484 qc->err_mask |= ac_err_mask(ata_wait_idle(ap));
485 ata_qc_complete(qc);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700486 handled = 1;
487 break;
488
489 default:
Albert Leeee500aa2005-09-27 17:34:38 +0800490 ap->stats.idle_irq++;
491 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700492 }
493
Albert Leeee500aa2005-09-27 17:34:38 +0800494 return handled;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700495}
496
497static void pdc_irq_clear(struct ata_port *ap)
498{
Jeff Garzikcca39742006-08-24 03:19:22 -0400499 struct ata_host *host = ap->host;
500 void __iomem *mmio = host->mmio_base;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700501
502 readl(mmio + PDC_INT_SEQMASK);
503}
504
David Howells7d12e782006-10-05 14:55:46 +0100505static irqreturn_t pdc_interrupt (int irq, void *dev_instance)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700506{
Jeff Garzikcca39742006-08-24 03:19:22 -0400507 struct ata_host *host = dev_instance;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700508 struct ata_port *ap;
509 u32 mask = 0;
510 unsigned int i, tmp;
511 unsigned int handled = 0;
Jeff Garzikea6ba102005-08-30 05:18:18 -0400512 void __iomem *mmio_base;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700513
514 VPRINTK("ENTER\n");
515
Jeff Garzikcca39742006-08-24 03:19:22 -0400516 if (!host || !host->mmio_base) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700517 VPRINTK("QUICK EXIT\n");
518 return IRQ_NONE;
519 }
520
Jeff Garzikcca39742006-08-24 03:19:22 -0400521 mmio_base = host->mmio_base;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700522
523 /* reading should also clear interrupts */
524 mask = readl(mmio_base + PDC_INT_SEQMASK);
525
526 if (mask == 0xffffffff) {
527 VPRINTK("QUICK EXIT 2\n");
528 return IRQ_NONE;
529 }
Luke Kosewski6340f012006-01-28 12:39:29 -0500530
Jeff Garzikcca39742006-08-24 03:19:22 -0400531 spin_lock(&host->lock);
Luke Kosewski6340f012006-01-28 12:39:29 -0500532
Linus Torvalds1da177e2005-04-16 15:20:36 -0700533 mask &= 0xffff; /* only 16 tags possible */
534 if (!mask) {
535 VPRINTK("QUICK EXIT 3\n");
Luke Kosewski6340f012006-01-28 12:39:29 -0500536 goto done_irq;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700537 }
538
Linus Torvalds1da177e2005-04-16 15:20:36 -0700539 writel(mask, mmio_base + PDC_INT_SEQMASK);
540
Jeff Garzikcca39742006-08-24 03:19:22 -0400541 for (i = 0; i < host->n_ports; i++) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700542 VPRINTK("port %u\n", i);
Jeff Garzikcca39742006-08-24 03:19:22 -0400543 ap = host->ports[i];
Linus Torvalds1da177e2005-04-16 15:20:36 -0700544 tmp = mask & (1 << (i + 1));
Tejun Heoc1389502005-08-22 14:59:24 +0900545 if (tmp && ap &&
Jeff Garzik029f5462006-04-02 10:30:40 -0400546 !(ap->flags & ATA_FLAG_DISABLED)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700547 struct ata_queued_cmd *qc;
548
549 qc = ata_qc_from_tag(ap, ap->active_tag);
Albert Leee50362e2005-09-27 17:39:50 +0800550 if (qc && (!(qc->tf.flags & ATA_TFLAG_POLLING)))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700551 handled += pdc_host_intr(ap, qc);
552 }
553 }
554
Linus Torvalds1da177e2005-04-16 15:20:36 -0700555 VPRINTK("EXIT\n");
556
Luke Kosewski6340f012006-01-28 12:39:29 -0500557done_irq:
Jeff Garzikcca39742006-08-24 03:19:22 -0400558 spin_unlock(&host->lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700559 return IRQ_RETVAL(handled);
560}
561
562static inline void pdc_packet_start(struct ata_queued_cmd *qc)
563{
564 struct ata_port *ap = qc->ap;
565 struct pdc_port_priv *pp = ap->private_data;
566 unsigned int port_no = ap->port_no;
567 u8 seq = (u8) (port_no + 1);
568
569 VPRINTK("ENTER, ap %p\n", ap);
570
Jeff Garzikcca39742006-08-24 03:19:22 -0400571 writel(0x00000001, ap->host->mmio_base + (seq * 4));
572 readl(ap->host->mmio_base + (seq * 4)); /* flush */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700573
574 pp->pkt[2] = seq;
575 wmb(); /* flush PRD, pkt writes */
Al Virob181d3b2005-10-21 06:46:02 +0100576 writel(pp->pkt_dma, (void __iomem *) ap->ioaddr.cmd_addr + PDC_PKT_SUBMIT);
577 readl((void __iomem *) ap->ioaddr.cmd_addr + PDC_PKT_SUBMIT); /* flush */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700578}
579
Tejun Heo9a3d9eb2006-01-23 13:09:36 +0900580static unsigned int pdc_qc_issue_prot(struct ata_queued_cmd *qc)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700581{
582 switch (qc->tf.protocol) {
583 case ATA_PROT_DMA:
584 case ATA_PROT_NODATA:
585 pdc_packet_start(qc);
586 return 0;
587
588 case ATA_PROT_ATAPI_DMA:
589 BUG();
590 break;
591
592 default:
593 break;
594 }
595
596 return ata_qc_issue_prot(qc);
597}
598
Jeff Garzik057ace52005-10-22 14:27:05 -0400599static void pdc_tf_load_mmio(struct ata_port *ap, const struct ata_taskfile *tf)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700600{
601 WARN_ON (tf->protocol == ATA_PROT_DMA ||
602 tf->protocol == ATA_PROT_NODATA);
603 ata_tf_load(ap, tf);
604}
605
606
Jeff Garzik057ace52005-10-22 14:27:05 -0400607static void pdc_exec_command_mmio(struct ata_port *ap, const struct ata_taskfile *tf)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700608{
609 WARN_ON (tf->protocol == ATA_PROT_DMA ||
610 tf->protocol == ATA_PROT_NODATA);
611 ata_exec_command(ap, tf);
612}
613
614
615static void pdc_ata_setup_port(struct ata_ioports *port, unsigned long base)
616{
617 port->cmd_addr = base;
618 port->data_addr = base;
619 port->feature_addr =
620 port->error_addr = base + 0x4;
621 port->nsect_addr = base + 0x8;
622 port->lbal_addr = base + 0xc;
623 port->lbam_addr = base + 0x10;
624 port->lbah_addr = base + 0x14;
625 port->device_addr = base + 0x18;
626 port->command_addr =
627 port->status_addr = base + 0x1c;
628 port->altstatus_addr =
629 port->ctl_addr = base + 0x38;
630}
631
632
633static void pdc_host_init(unsigned int chip_id, struct ata_probe_ent *pe)
634{
Jeff Garzikea6ba102005-08-30 05:18:18 -0400635 void __iomem *mmio = pe->mmio_base;
Luke Kosewski6340f012006-01-28 12:39:29 -0500636 struct pdc_host_priv *hp = pe->private_data;
637 int hotplug_offset = hp->hotplug_offset;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700638 u32 tmp;
639
640 /*
641 * Except for the hotplug stuff, this is voodoo from the
642 * Promise driver. Label this entire section
643 * "TODO: figure out why we do this"
644 */
645
Mikael Petterssonb2d1eee2006-11-22 22:00:15 +0100646 /* enable BMR_BURST, maybe change FIFO_SHD to 8 dwords */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700647 tmp = readl(mmio + PDC_FLASH_CTL);
Mikael Petterssonb2d1eee2006-11-22 22:00:15 +0100648 tmp |= 0x02000; /* bit 13 (enable bmr burst) */
649 if (!(hp->flags & PDC_FLAG_GEN_II))
650 tmp |= 0x10000; /* bit 16 (fifo threshold at 8 dw) */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700651 writel(tmp, mmio + PDC_FLASH_CTL);
652
653 /* clear plug/unplug flags for all ports */
Luke Kosewski6340f012006-01-28 12:39:29 -0500654 tmp = readl(mmio + hotplug_offset);
655 writel(tmp | 0xff, mmio + hotplug_offset);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700656
657 /* mask plug/unplug ints */
Luke Kosewski6340f012006-01-28 12:39:29 -0500658 tmp = readl(mmio + hotplug_offset);
659 writel(tmp | 0xff0000, mmio + hotplug_offset);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700660
Mikael Petterssonb2d1eee2006-11-22 22:00:15 +0100661 /* don't initialise TBG or SLEW on 2nd generation chips */
662 if (hp->flags & PDC_FLAG_GEN_II)
663 return;
664
Linus Torvalds1da177e2005-04-16 15:20:36 -0700665 /* reduce TBG clock to 133 Mhz. */
666 tmp = readl(mmio + PDC_TBG_MODE);
667 tmp &= ~0x30000; /* clear bit 17, 16*/
668 tmp |= 0x10000; /* set bit 17:16 = 0:1 */
669 writel(tmp, mmio + PDC_TBG_MODE);
670
671 readl(mmio + PDC_TBG_MODE); /* flush */
672 msleep(10);
673
674 /* adjust slew rate control register. */
675 tmp = readl(mmio + PDC_SLEW_CTL);
676 tmp &= 0xFFFFF03F; /* clear bit 11 ~ 6 */
677 tmp |= 0x00000900; /* set bit 11-9 = 100b , bit 8-6 = 100 */
678 writel(tmp, mmio + PDC_SLEW_CTL);
679}
680
681static int pdc_ata_init_one (struct pci_dev *pdev, const struct pci_device_id *ent)
682{
683 static int printed_version;
684 struct ata_probe_ent *probe_ent = NULL;
Luke Kosewski6340f012006-01-28 12:39:29 -0500685 struct pdc_host_priv *hp;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700686 unsigned long base;
Jeff Garzikea6ba102005-08-30 05:18:18 -0400687 void __iomem *mmio_base;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700688 unsigned int board_idx = (unsigned int) ent->driver_data;
689 int pci_dev_busy = 0;
690 int rc;
691
692 if (!printed_version++)
Jeff Garzika9524a72005-10-30 14:39:11 -0500693 dev_printk(KERN_DEBUG, &pdev->dev, "version " DRV_VERSION "\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700694
Linus Torvalds1da177e2005-04-16 15:20:36 -0700695 rc = pci_enable_device(pdev);
696 if (rc)
697 return rc;
698
699 rc = pci_request_regions(pdev, DRV_NAME);
700 if (rc) {
701 pci_dev_busy = 1;
702 goto err_out;
703 }
704
705 rc = pci_set_dma_mask(pdev, ATA_DMA_MASK);
706 if (rc)
707 goto err_out_regions;
708 rc = pci_set_consistent_dma_mask(pdev, ATA_DMA_MASK);
709 if (rc)
710 goto err_out_regions;
711
Luke Kosewski6340f012006-01-28 12:39:29 -0500712 probe_ent = kzalloc(sizeof(*probe_ent), GFP_KERNEL);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700713 if (probe_ent == NULL) {
714 rc = -ENOMEM;
715 goto err_out_regions;
716 }
717
Linus Torvalds1da177e2005-04-16 15:20:36 -0700718 probe_ent->dev = pci_dev_to_dev(pdev);
719 INIT_LIST_HEAD(&probe_ent->node);
720
Jeff Garzik374b1872005-08-30 05:42:52 -0400721 mmio_base = pci_iomap(pdev, 3, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700722 if (mmio_base == NULL) {
723 rc = -ENOMEM;
724 goto err_out_free_ent;
725 }
726 base = (unsigned long) mmio_base;
727
Luke Kosewski6340f012006-01-28 12:39:29 -0500728 hp = kzalloc(sizeof(*hp), GFP_KERNEL);
729 if (hp == NULL) {
730 rc = -ENOMEM;
731 goto err_out_free_ent;
732 }
733
734 /* Set default hotplug offset */
735 hp->hotplug_offset = PDC_SATA_PLUG_CSR;
736 probe_ent->private_data = hp;
737
Linus Torvalds1da177e2005-04-16 15:20:36 -0700738 probe_ent->sht = pdc_port_info[board_idx].sht;
Jeff Garzikcca39742006-08-24 03:19:22 -0400739 probe_ent->port_flags = pdc_port_info[board_idx].flags;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700740 probe_ent->pio_mask = pdc_port_info[board_idx].pio_mask;
741 probe_ent->mwdma_mask = pdc_port_info[board_idx].mwdma_mask;
742 probe_ent->udma_mask = pdc_port_info[board_idx].udma_mask;
743 probe_ent->port_ops = pdc_port_info[board_idx].port_ops;
744
745 probe_ent->irq = pdev->irq;
Thomas Gleixner1d6f3592006-07-01 19:29:42 -0700746 probe_ent->irq_flags = IRQF_SHARED;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700747 probe_ent->mmio_base = mmio_base;
748
749 pdc_ata_setup_port(&probe_ent->port[0], base + 0x200);
750 pdc_ata_setup_port(&probe_ent->port[1], base + 0x280);
751
752 probe_ent->port[0].scr_addr = base + 0x400;
753 probe_ent->port[1].scr_addr = base + 0x500;
754
755 /* notice 4-port boards */
756 switch (board_idx) {
Luke Kosewski6340f012006-01-28 12:39:29 -0500757 case board_40518:
Mikael Petterssonb2d1eee2006-11-22 22:00:15 +0100758 hp->flags |= PDC_FLAG_GEN_II;
Luke Kosewski6340f012006-01-28 12:39:29 -0500759 /* Override hotplug offset for SATAII150 */
760 hp->hotplug_offset = PDC2_SATA_PLUG_CSR;
761 /* Fall through */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700762 case board_20319:
763 probe_ent->n_ports = 4;
764
765 pdc_ata_setup_port(&probe_ent->port[2], base + 0x300);
766 pdc_ata_setup_port(&probe_ent->port[3], base + 0x380);
767
768 probe_ent->port[2].scr_addr = base + 0x600;
769 probe_ent->port[3].scr_addr = base + 0x700;
770 break;
Luke Kosewski6340f012006-01-28 12:39:29 -0500771 case board_2057x:
Mikael Petterssonb2d1eee2006-11-22 22:00:15 +0100772 case board_20771:
773 hp->flags |= PDC_FLAG_GEN_II;
Luke Kosewski6340f012006-01-28 12:39:29 -0500774 /* Override hotplug offset for SATAII150 */
775 hp->hotplug_offset = PDC2_SATA_PLUG_CSR;
776 /* Fall through */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700777 case board_2037x:
Jeff Garzik6c9e5eb2005-11-30 16:42:55 -0500778 probe_ent->n_ports = 2;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700779 break;
Tobias Lorenzf497ba72005-05-12 15:51:01 -0400780 case board_20619:
781 probe_ent->n_ports = 4;
782
783 pdc_ata_setup_port(&probe_ent->port[2], base + 0x300);
784 pdc_ata_setup_port(&probe_ent->port[3], base + 0x380);
785
786 probe_ent->port[2].scr_addr = base + 0x600;
787 probe_ent->port[3].scr_addr = base + 0x700;
Jeff Garzik6c9e5eb2005-11-30 16:42:55 -0500788 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700789 default:
790 BUG();
791 break;
792 }
793
794 pci_set_master(pdev);
795
796 /* initialize adapter */
797 pdc_host_init(board_idx, probe_ent);
798
Luke Kosewski6340f012006-01-28 12:39:29 -0500799 /* FIXME: Need any other frees than hp? */
800 if (!ata_device_add(probe_ent))
801 kfree(hp);
802
Linus Torvalds1da177e2005-04-16 15:20:36 -0700803 kfree(probe_ent);
804
805 return 0;
806
807err_out_free_ent:
808 kfree(probe_ent);
809err_out_regions:
810 pci_release_regions(pdev);
811err_out:
812 if (!pci_dev_busy)
813 pci_disable_device(pdev);
814 return rc;
815}
816
817
818static int __init pdc_ata_init(void)
819{
Pavel Roskinb7887192006-08-10 18:13:18 +0900820 return pci_register_driver(&pdc_ata_pci_driver);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700821}
822
823
824static void __exit pdc_ata_exit(void)
825{
826 pci_unregister_driver(&pdc_ata_pci_driver);
827}
828
829
830MODULE_AUTHOR("Jeff Garzik");
Tobias Lorenzf497ba72005-05-12 15:51:01 -0400831MODULE_DESCRIPTION("Promise ATA TX2/TX4/TX4000 low-level driver");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700832MODULE_LICENSE("GPL");
833MODULE_DEVICE_TABLE(pci, pdc_ata_pci_tbl);
834MODULE_VERSION(DRV_VERSION);
835
836module_init(pdc_ata_init);
837module_exit(pdc_ata_exit);