sata_promise: mmio access cleanups
[firefly-linux-kernel-4.4.55.git] / drivers / ata / sata_promise.c
1 /*
2  *  sata_promise.c - Promise SATA
3  *
4  *  Maintained by:  Jeff Garzik <jgarzik@pobox.com>
5  *                  Mikael Pettersson <mikpe@it.uu.se>
6  *                  Please ALWAYS copy linux-ide@vger.kernel.org
7  *                  on emails.
8  *
9  *  Copyright 2003-2004 Red Hat, Inc.
10  *
11  *
12  *  This program is free software; you can redistribute it and/or modify
13  *  it under the terms of the GNU General Public License as published by
14  *  the Free Software Foundation; either version 2, or (at your option)
15  *  any later version.
16  *
17  *  This program is distributed in the hope that it will be useful,
18  *  but WITHOUT ANY WARRANTY; without even the implied warranty of
19  *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
20  *  GNU General Public License for more details.
21  *
22  *  You should have received a copy of the GNU General Public License
23  *  along with this program; see the file COPYING.  If not, write to
24  *  the Free Software Foundation, 675 Mass Ave, Cambridge, MA 02139, USA.
25  *
26  *
27  *  libata documentation is available via 'make {ps|pdf}docs',
28  *  as Documentation/DocBook/libata.*
29  *
30  *  Hardware information only available under NDA.
31  *
32  */
33
34 #include <linux/kernel.h>
35 #include <linux/module.h>
36 #include <linux/pci.h>
37 #include <linux/init.h>
38 #include <linux/blkdev.h>
39 #include <linux/delay.h>
40 #include <linux/interrupt.h>
41 #include <linux/device.h>
42 #include <scsi/scsi.h>
43 #include <scsi/scsi_host.h>
44 #include <scsi/scsi_cmnd.h>
45 #include <linux/libata.h>
46 #include "sata_promise.h"
47
48 #define DRV_NAME        "sata_promise"
49 #define DRV_VERSION     "2.12"
50
51 enum {
52         PDC_MAX_PORTS           = 4,
53         PDC_MMIO_BAR            = 3,
54         PDC_MAX_PRD             = LIBATA_MAX_PRD - 1, /* -1 for ASIC PRD bug workaround */
55
56         /* host register offsets (from host->iomap[PDC_MMIO_BAR]) */
57         PDC_INT_SEQMASK         = 0x40, /* Mask of asserted SEQ INTs */
58         PDC_FLASH_CTL           = 0x44, /* Flash control register */
59         PDC_SATA_PLUG_CSR       = 0x6C, /* SATA Plug control/status reg */
60         PDC2_SATA_PLUG_CSR      = 0x60, /* SATAII Plug control/status reg */
61         PDC_TBG_MODE            = 0x41C, /* TBG mode (not SATAII) */
62         PDC_SLEW_CTL            = 0x470, /* slew rate control reg (not SATAII) */
63
64         /* per-port ATA register offsets (from ap->ioaddr.cmd_addr) */
65         PDC_FEATURE             = 0x04, /* Feature/Error reg (per port) */
66         PDC_SECTOR_COUNT        = 0x08, /* Sector count reg (per port) */
67         PDC_SECTOR_NUMBER       = 0x0C, /* Sector number reg (per port) */
68         PDC_CYLINDER_LOW        = 0x10, /* Cylinder low reg (per port) */
69         PDC_CYLINDER_HIGH       = 0x14, /* Cylinder high reg (per port) */
70         PDC_DEVICE              = 0x18, /* Device/Head reg (per port) */
71         PDC_COMMAND             = 0x1C, /* Command/status reg (per port) */
72         PDC_ALTSTATUS           = 0x38, /* Alternate-status/device-control reg (per port) */
73         PDC_PKT_SUBMIT          = 0x40, /* Command packet pointer addr */
74         PDC_GLOBAL_CTL          = 0x48, /* Global control/status (per port) */
75         PDC_CTLSTAT             = 0x60, /* IDE control and status (per port) */
76
77         /* per-port SATA register offsets (from ap->ioaddr.scr_addr) */
78         PDC_PHYMODE4            = 0x14,
79
80         /* PDC_GLOBAL_CTL bit definitions */
81         PDC_PH_ERR              = (1 <<  8), /* PCI error while loading packet */
82         PDC_SH_ERR              = (1 <<  9), /* PCI error while loading S/G table */
83         PDC_DH_ERR              = (1 << 10), /* PCI error while loading data */
84         PDC2_HTO_ERR            = (1 << 12), /* host bus timeout */
85         PDC2_ATA_HBA_ERR        = (1 << 13), /* error during SATA DATA FIS transmission */
86         PDC2_ATA_DMA_CNT_ERR    = (1 << 14), /* DMA DATA FIS size differs from S/G count */
87         PDC_OVERRUN_ERR         = (1 << 19), /* S/G byte count larger than HD requires */
88         PDC_UNDERRUN_ERR        = (1 << 20), /* S/G byte count less than HD requires */
89         PDC_DRIVE_ERR           = (1 << 21), /* drive error */
90         PDC_PCI_SYS_ERR         = (1 << 22), /* PCI system error */
91         PDC1_PCI_PARITY_ERR     = (1 << 23), /* PCI parity error (from SATA150 driver) */
92         PDC1_ERR_MASK           = PDC1_PCI_PARITY_ERR,
93         PDC2_ERR_MASK           = PDC2_HTO_ERR | PDC2_ATA_HBA_ERR |
94                                   PDC2_ATA_DMA_CNT_ERR,
95         PDC_ERR_MASK            = PDC_PH_ERR | PDC_SH_ERR | PDC_DH_ERR |
96                                   PDC_OVERRUN_ERR | PDC_UNDERRUN_ERR |
97                                   PDC_DRIVE_ERR | PDC_PCI_SYS_ERR |
98                                   PDC1_ERR_MASK | PDC2_ERR_MASK,
99
100         board_2037x             = 0,    /* FastTrak S150 TX2plus */
101         board_2037x_pata        = 1,    /* FastTrak S150 TX2plus PATA port */
102         board_20319             = 2,    /* FastTrak S150 TX4 */
103         board_20619             = 3,    /* FastTrak TX4000 */
104         board_2057x             = 4,    /* SATAII150 Tx2plus */
105         board_2057x_pata        = 5,    /* SATAII150 Tx2plus PATA port */
106         board_40518             = 6,    /* SATAII150 Tx4 */
107
108         PDC_HAS_PATA            = (1 << 1), /* PDC20375/20575 has PATA */
109
110         /* Sequence counter control registers bit definitions */
111         PDC_SEQCNTRL_INT_MASK   = (1 << 5), /* Sequence Interrupt Mask */
112
113         /* Feature register values */
114         PDC_FEATURE_ATAPI_PIO   = 0x00, /* ATAPI data xfer by PIO */
115         PDC_FEATURE_ATAPI_DMA   = 0x01, /* ATAPI data xfer by DMA */
116
117         /* Device/Head register values */
118         PDC_DEVICE_SATA         = 0xE0, /* Device/Head value for SATA devices */
119
120         /* PDC_CTLSTAT bit definitions */
121         PDC_DMA_ENABLE          = (1 << 7),
122         PDC_IRQ_DISABLE         = (1 << 10),
123         PDC_RESET               = (1 << 11), /* HDMA reset */
124
125         PDC_COMMON_FLAGS        = ATA_FLAG_NO_LEGACY |
126                                   ATA_FLAG_MMIO |
127                                   ATA_FLAG_PIO_POLLING,
128
129         /* ap->flags bits */
130         PDC_FLAG_GEN_II         = (1 << 24),
131         PDC_FLAG_SATA_PATA      = (1 << 25), /* supports SATA + PATA */
132         PDC_FLAG_4_PORTS        = (1 << 26), /* 4 ports */
133 };
134
135 struct pdc_port_priv {
136         u8                      *pkt;
137         dma_addr_t              pkt_dma;
138 };
139
140 static int pdc_sata_scr_read(struct ata_port *ap, unsigned int sc_reg, u32 *val);
141 static int pdc_sata_scr_write(struct ata_port *ap, unsigned int sc_reg, u32 val);
142 static int pdc_ata_init_one (struct pci_dev *pdev, const struct pci_device_id *ent);
143 static int pdc_common_port_start(struct ata_port *ap);
144 static int pdc_sata_port_start(struct ata_port *ap);
145 static void pdc_qc_prep(struct ata_queued_cmd *qc);
146 static void pdc_tf_load_mmio(struct ata_port *ap, const struct ata_taskfile *tf);
147 static void pdc_exec_command_mmio(struct ata_port *ap, const struct ata_taskfile *tf);
148 static int pdc_check_atapi_dma(struct ata_queued_cmd *qc);
149 static int pdc_old_sata_check_atapi_dma(struct ata_queued_cmd *qc);
150 static void pdc_irq_clear(struct ata_port *ap);
151 static unsigned int pdc_qc_issue(struct ata_queued_cmd *qc);
152 static void pdc_freeze(struct ata_port *ap);
153 static void pdc_sata_freeze(struct ata_port *ap);
154 static void pdc_thaw(struct ata_port *ap);
155 static void pdc_sata_thaw(struct ata_port *ap);
156 static void pdc_error_handler(struct ata_port *ap);
157 static void pdc_post_internal_cmd(struct ata_queued_cmd *qc);
158 static int pdc_pata_cable_detect(struct ata_port *ap);
159 static int pdc_sata_cable_detect(struct ata_port *ap);
160
161 static struct scsi_host_template pdc_ata_sht = {
162         ATA_BASE_SHT(DRV_NAME),
163         .sg_tablesize           = PDC_MAX_PRD,
164         .dma_boundary           = ATA_DMA_BOUNDARY,
165 };
166
167 static const struct ata_port_operations pdc_common_ops = {
168         .inherits               = &ata_sff_port_ops,
169
170         .sff_tf_load            = pdc_tf_load_mmio,
171         .sff_exec_command       = pdc_exec_command_mmio,
172         .check_atapi_dma        = pdc_check_atapi_dma,
173         .qc_prep                = pdc_qc_prep,
174         .qc_issue               = pdc_qc_issue,
175         .sff_irq_clear          = pdc_irq_clear,
176
177         .post_internal_cmd      = pdc_post_internal_cmd,
178         .error_handler          = pdc_error_handler,
179 };
180
181 static struct ata_port_operations pdc_sata_ops = {
182         .inherits               = &pdc_common_ops,
183         .cable_detect           = pdc_sata_cable_detect,
184         .freeze                 = pdc_sata_freeze,
185         .thaw                   = pdc_sata_thaw,
186         .scr_read               = pdc_sata_scr_read,
187         .scr_write              = pdc_sata_scr_write,
188         .port_start             = pdc_sata_port_start,
189 };
190
191 /* First-generation chips need a more restrictive ->check_atapi_dma op */
192 static struct ata_port_operations pdc_old_sata_ops = {
193         .inherits               = &pdc_sata_ops,
194         .check_atapi_dma        = pdc_old_sata_check_atapi_dma,
195 };
196
197 static struct ata_port_operations pdc_pata_ops = {
198         .inherits               = &pdc_common_ops,
199         .cable_detect           = pdc_pata_cable_detect,
200         .freeze                 = pdc_freeze,
201         .thaw                   = pdc_thaw,
202         .port_start             = pdc_common_port_start,
203 };
204
205 static const struct ata_port_info pdc_port_info[] = {
206         [board_2037x] =
207         {
208                 .flags          = PDC_COMMON_FLAGS | ATA_FLAG_SATA |
209                                   PDC_FLAG_SATA_PATA,
210                 .pio_mask       = 0x1f, /* pio0-4 */
211                 .mwdma_mask     = 0x07, /* mwdma0-2 */
212                 .udma_mask      = ATA_UDMA6,
213                 .port_ops       = &pdc_old_sata_ops,
214         },
215
216         [board_2037x_pata] =
217         {
218                 .flags          = PDC_COMMON_FLAGS | ATA_FLAG_SLAVE_POSS,
219                 .pio_mask       = 0x1f, /* pio0-4 */
220                 .mwdma_mask     = 0x07, /* mwdma0-2 */
221                 .udma_mask      = ATA_UDMA6,
222                 .port_ops       = &pdc_pata_ops,
223         },
224
225         [board_20319] =
226         {
227                 .flags          = PDC_COMMON_FLAGS | ATA_FLAG_SATA |
228                                   PDC_FLAG_4_PORTS,
229                 .pio_mask       = 0x1f, /* pio0-4 */
230                 .mwdma_mask     = 0x07, /* mwdma0-2 */
231                 .udma_mask      = ATA_UDMA6,
232                 .port_ops       = &pdc_old_sata_ops,
233         },
234
235         [board_20619] =
236         {
237                 .flags          = PDC_COMMON_FLAGS | ATA_FLAG_SLAVE_POSS |
238                                   PDC_FLAG_4_PORTS,
239                 .pio_mask       = 0x1f, /* pio0-4 */
240                 .mwdma_mask     = 0x07, /* mwdma0-2 */
241                 .udma_mask      = ATA_UDMA6,
242                 .port_ops       = &pdc_pata_ops,
243         },
244
245         [board_2057x] =
246         {
247                 .flags          = PDC_COMMON_FLAGS | ATA_FLAG_SATA |
248                                   PDC_FLAG_GEN_II | PDC_FLAG_SATA_PATA,
249                 .pio_mask       = 0x1f, /* pio0-4 */
250                 .mwdma_mask     = 0x07, /* mwdma0-2 */
251                 .udma_mask      = ATA_UDMA6,
252                 .port_ops       = &pdc_sata_ops,
253         },
254
255         [board_2057x_pata] =
256         {
257                 .flags          = PDC_COMMON_FLAGS | ATA_FLAG_SLAVE_POSS |
258                                   PDC_FLAG_GEN_II,
259                 .pio_mask       = 0x1f, /* pio0-4 */
260                 .mwdma_mask     = 0x07, /* mwdma0-2 */
261                 .udma_mask      = ATA_UDMA6,
262                 .port_ops       = &pdc_pata_ops,
263         },
264
265         [board_40518] =
266         {
267                 .flags          = PDC_COMMON_FLAGS | ATA_FLAG_SATA |
268                                   PDC_FLAG_GEN_II | PDC_FLAG_4_PORTS,
269                 .pio_mask       = 0x1f, /* pio0-4 */
270                 .mwdma_mask     = 0x07, /* mwdma0-2 */
271                 .udma_mask      = ATA_UDMA6,
272                 .port_ops       = &pdc_sata_ops,
273         },
274 };
275
276 static const struct pci_device_id pdc_ata_pci_tbl[] = {
277         { PCI_VDEVICE(PROMISE, 0x3371), board_2037x },
278         { PCI_VDEVICE(PROMISE, 0x3373), board_2037x },
279         { PCI_VDEVICE(PROMISE, 0x3375), board_2037x },
280         { PCI_VDEVICE(PROMISE, 0x3376), board_2037x },
281         { PCI_VDEVICE(PROMISE, 0x3570), board_2057x },
282         { PCI_VDEVICE(PROMISE, 0x3571), board_2057x },
283         { PCI_VDEVICE(PROMISE, 0x3574), board_2057x },
284         { PCI_VDEVICE(PROMISE, 0x3577), board_2057x },
285         { PCI_VDEVICE(PROMISE, 0x3d73), board_2057x },
286         { PCI_VDEVICE(PROMISE, 0x3d75), board_2057x },
287
288         { PCI_VDEVICE(PROMISE, 0x3318), board_20319 },
289         { PCI_VDEVICE(PROMISE, 0x3319), board_20319 },
290         { PCI_VDEVICE(PROMISE, 0x3515), board_40518 },
291         { PCI_VDEVICE(PROMISE, 0x3519), board_40518 },
292         { PCI_VDEVICE(PROMISE, 0x3d17), board_40518 },
293         { PCI_VDEVICE(PROMISE, 0x3d18), board_40518 },
294
295         { PCI_VDEVICE(PROMISE, 0x6629), board_20619 },
296
297         { }     /* terminate list */
298 };
299
300 static struct pci_driver pdc_ata_pci_driver = {
301         .name                   = DRV_NAME,
302         .id_table               = pdc_ata_pci_tbl,
303         .probe                  = pdc_ata_init_one,
304         .remove                 = ata_pci_remove_one,
305 };
306
307 static int pdc_common_port_start(struct ata_port *ap)
308 {
309         struct device *dev = ap->host->dev;
310         struct pdc_port_priv *pp;
311         int rc;
312
313         rc = ata_port_start(ap);
314         if (rc)
315                 return rc;
316
317         pp = devm_kzalloc(dev, sizeof(*pp), GFP_KERNEL);
318         if (!pp)
319                 return -ENOMEM;
320
321         pp->pkt = dmam_alloc_coherent(dev, 128, &pp->pkt_dma, GFP_KERNEL);
322         if (!pp->pkt)
323                 return -ENOMEM;
324
325         ap->private_data = pp;
326
327         return 0;
328 }
329
330 static int pdc_sata_port_start(struct ata_port *ap)
331 {
332         int rc;
333
334         rc = pdc_common_port_start(ap);
335         if (rc)
336                 return rc;
337
338         /* fix up PHYMODE4 align timing */
339         if (ap->flags & PDC_FLAG_GEN_II) {
340                 void __iomem *sata_mmio = ap->ioaddr.scr_addr;
341                 unsigned int tmp;
342
343                 tmp = readl(sata_mmio + PDC_PHYMODE4);
344                 tmp = (tmp & ~3) | 1;   /* set bits 1:0 = 0:1 */
345                 writel(tmp, sata_mmio + PDC_PHYMODE4);
346         }
347
348         return 0;
349 }
350
351 static void pdc_reset_port(struct ata_port *ap)
352 {
353         void __iomem *ata_ctlstat_mmio = ap->ioaddr.cmd_addr + PDC_CTLSTAT;
354         unsigned int i;
355         u32 tmp;
356
357         for (i = 11; i > 0; i--) {
358                 tmp = readl(ata_ctlstat_mmio);
359                 if (tmp & PDC_RESET)
360                         break;
361
362                 udelay(100);
363
364                 tmp |= PDC_RESET;
365                 writel(tmp, ata_ctlstat_mmio);
366         }
367
368         tmp &= ~PDC_RESET;
369         writel(tmp, ata_ctlstat_mmio);
370         readl(ata_ctlstat_mmio);        /* flush */
371 }
372
373 static int pdc_pata_cable_detect(struct ata_port *ap)
374 {
375         u8 tmp;
376         void __iomem *ata_mmio = ap->ioaddr.cmd_addr;
377
378         tmp = readb(ata_mmio + PDC_CTLSTAT + 3);
379         if (tmp & 0x01)
380                 return ATA_CBL_PATA40;
381         return ATA_CBL_PATA80;
382 }
383
384 static int pdc_sata_cable_detect(struct ata_port *ap)
385 {
386         return ATA_CBL_SATA;
387 }
388
389 static int pdc_sata_scr_read(struct ata_port *ap, unsigned int sc_reg, u32 *val)
390 {
391         if (sc_reg > SCR_CONTROL)
392                 return -EINVAL;
393         *val = readl(ap->ioaddr.scr_addr + (sc_reg * 4));
394         return 0;
395 }
396
397 static int pdc_sata_scr_write(struct ata_port *ap, unsigned int sc_reg, u32 val)
398 {
399         if (sc_reg > SCR_CONTROL)
400                 return -EINVAL;
401         writel(val, ap->ioaddr.scr_addr + (sc_reg * 4));
402         return 0;
403 }
404
405 static void pdc_atapi_pkt(struct ata_queued_cmd *qc)
406 {
407         struct ata_port *ap = qc->ap;
408         dma_addr_t sg_table = ap->prd_dma;
409         unsigned int cdb_len = qc->dev->cdb_len;
410         u8 *cdb = qc->cdb;
411         struct pdc_port_priv *pp = ap->private_data;
412         u8 *buf = pp->pkt;
413         __le32 *buf32 = (__le32 *) buf;
414         unsigned int dev_sel, feature;
415
416         /* set control bits (byte 0), zero delay seq id (byte 3),
417          * and seq id (byte 2)
418          */
419         switch (qc->tf.protocol) {
420         case ATAPI_PROT_DMA:
421                 if (!(qc->tf.flags & ATA_TFLAG_WRITE))
422                         buf32[0] = cpu_to_le32(PDC_PKT_READ);
423                 else
424                         buf32[0] = 0;
425                 break;
426         case ATAPI_PROT_NODATA:
427                 buf32[0] = cpu_to_le32(PDC_PKT_NODATA);
428                 break;
429         default:
430                 BUG();
431                 break;
432         }
433         buf32[1] = cpu_to_le32(sg_table);       /* S/G table addr */
434         buf32[2] = 0;                           /* no next-packet */
435
436         /* select drive */
437         if (sata_scr_valid(&ap->link))
438                 dev_sel = PDC_DEVICE_SATA;
439         else
440                 dev_sel = qc->tf.device;
441
442         buf[12] = (1 << 5) | ATA_REG_DEVICE;
443         buf[13] = dev_sel;
444         buf[14] = (1 << 5) | ATA_REG_DEVICE | PDC_PKT_CLEAR_BSY;
445         buf[15] = dev_sel; /* once more, waiting for BSY to clear */
446
447         buf[16] = (1 << 5) | ATA_REG_NSECT;
448         buf[17] = qc->tf.nsect;
449         buf[18] = (1 << 5) | ATA_REG_LBAL;
450         buf[19] = qc->tf.lbal;
451
452         /* set feature and byte counter registers */
453         if (qc->tf.protocol != ATAPI_PROT_DMA)
454                 feature = PDC_FEATURE_ATAPI_PIO;
455         else
456                 feature = PDC_FEATURE_ATAPI_DMA;
457
458         buf[20] = (1 << 5) | ATA_REG_FEATURE;
459         buf[21] = feature;
460         buf[22] = (1 << 5) | ATA_REG_BYTEL;
461         buf[23] = qc->tf.lbam;
462         buf[24] = (1 << 5) | ATA_REG_BYTEH;
463         buf[25] = qc->tf.lbah;
464
465         /* send ATAPI packet command 0xA0 */
466         buf[26] = (1 << 5) | ATA_REG_CMD;
467         buf[27] = qc->tf.command;
468
469         /* select drive and check DRQ */
470         buf[28] = (1 << 5) | ATA_REG_DEVICE | PDC_PKT_WAIT_DRDY;
471         buf[29] = dev_sel;
472
473         /* we can represent cdb lengths 2/4/6/8/10/12/14/16 */
474         BUG_ON(cdb_len & ~0x1E);
475
476         /* append the CDB as the final part */
477         buf[30] = (((cdb_len >> 1) & 7) << 5) | ATA_REG_DATA | PDC_LAST_REG;
478         memcpy(buf+31, cdb, cdb_len);
479 }
480
481 /**
482  *      pdc_fill_sg - Fill PCI IDE PRD table
483  *      @qc: Metadata associated with taskfile to be transferred
484  *
485  *      Fill PCI IDE PRD (scatter-gather) table with segments
486  *      associated with the current disk command.
487  *      Make sure hardware does not choke on it.
488  *
489  *      LOCKING:
490  *      spin_lock_irqsave(host lock)
491  *
492  */
493 static void pdc_fill_sg(struct ata_queued_cmd *qc)
494 {
495         struct ata_port *ap = qc->ap;
496         struct scatterlist *sg;
497         const u32 SG_COUNT_ASIC_BUG = 41*4;
498         unsigned int si, idx;
499         u32 len;
500
501         if (!(qc->flags & ATA_QCFLAG_DMAMAP))
502                 return;
503
504         idx = 0;
505         for_each_sg(qc->sg, sg, qc->n_elem, si) {
506                 u32 addr, offset;
507                 u32 sg_len;
508
509                 /* determine if physical DMA addr spans 64K boundary.
510                  * Note h/w doesn't support 64-bit, so we unconditionally
511                  * truncate dma_addr_t to u32.
512                  */
513                 addr = (u32) sg_dma_address(sg);
514                 sg_len = sg_dma_len(sg);
515
516                 while (sg_len) {
517                         offset = addr & 0xffff;
518                         len = sg_len;
519                         if ((offset + sg_len) > 0x10000)
520                                 len = 0x10000 - offset;
521
522                         ap->prd[idx].addr = cpu_to_le32(addr);
523                         ap->prd[idx].flags_len = cpu_to_le32(len & 0xffff);
524                         VPRINTK("PRD[%u] = (0x%X, 0x%X)\n", idx, addr, len);
525
526                         idx++;
527                         sg_len -= len;
528                         addr += len;
529                 }
530         }
531
532         len = le32_to_cpu(ap->prd[idx - 1].flags_len);
533
534         if (len > SG_COUNT_ASIC_BUG) {
535                 u32 addr;
536
537                 VPRINTK("Splitting last PRD.\n");
538
539                 addr = le32_to_cpu(ap->prd[idx - 1].addr);
540                 ap->prd[idx - 1].flags_len = cpu_to_le32(len - SG_COUNT_ASIC_BUG);
541                 VPRINTK("PRD[%u] = (0x%X, 0x%X)\n", idx - 1, addr, SG_COUNT_ASIC_BUG);
542
543                 addr = addr + len - SG_COUNT_ASIC_BUG;
544                 len = SG_COUNT_ASIC_BUG;
545                 ap->prd[idx].addr = cpu_to_le32(addr);
546                 ap->prd[idx].flags_len = cpu_to_le32(len);
547                 VPRINTK("PRD[%u] = (0x%X, 0x%X)\n", idx, addr, len);
548
549                 idx++;
550         }
551
552         ap->prd[idx - 1].flags_len |= cpu_to_le32(ATA_PRD_EOT);
553 }
554
555 static void pdc_qc_prep(struct ata_queued_cmd *qc)
556 {
557         struct pdc_port_priv *pp = qc->ap->private_data;
558         unsigned int i;
559
560         VPRINTK("ENTER\n");
561
562         switch (qc->tf.protocol) {
563         case ATA_PROT_DMA:
564                 pdc_fill_sg(qc);
565                 /* fall through */
566
567         case ATA_PROT_NODATA:
568                 i = pdc_pkt_header(&qc->tf, qc->ap->prd_dma,
569                                    qc->dev->devno, pp->pkt);
570
571                 if (qc->tf.flags & ATA_TFLAG_LBA48)
572                         i = pdc_prep_lba48(&qc->tf, pp->pkt, i);
573                 else
574                         i = pdc_prep_lba28(&qc->tf, pp->pkt, i);
575
576                 pdc_pkt_footer(&qc->tf, pp->pkt, i);
577                 break;
578
579         case ATAPI_PROT_PIO:
580                 pdc_fill_sg(qc);
581                 break;
582
583         case ATAPI_PROT_DMA:
584                 pdc_fill_sg(qc);
585                 /*FALLTHROUGH*/
586         case ATAPI_PROT_NODATA:
587                 pdc_atapi_pkt(qc);
588                 break;
589
590         default:
591                 break;
592         }
593 }
594
595 static int pdc_is_sataii_tx4(unsigned long flags)
596 {
597         const unsigned long mask = PDC_FLAG_GEN_II | PDC_FLAG_4_PORTS;
598         return (flags & mask) == mask;
599 }
600
601 static unsigned int pdc_port_no_to_ata_no(unsigned int port_no,
602                                           int is_sataii_tx4)
603 {
604         static const unsigned char sataii_tx4_port_remap[4] = { 3, 1, 0, 2};
605         return is_sataii_tx4 ? sataii_tx4_port_remap[port_no] : port_no;
606 }
607
608 static unsigned int pdc_sata_nr_ports(const struct ata_port *ap)
609 {
610         return (ap->flags & PDC_FLAG_4_PORTS) ? 4 : 2;
611 }
612
613 static unsigned int pdc_sata_ata_port_to_ata_no(const struct ata_port *ap)
614 {
615         const struct ata_host *host = ap->host;
616         unsigned int nr_ports = pdc_sata_nr_ports(ap);
617         unsigned int i;
618
619         for(i = 0; i < nr_ports && host->ports[i] != ap; ++i)
620                 ;
621         BUG_ON(i >= nr_ports);
622         return pdc_port_no_to_ata_no(i, pdc_is_sataii_tx4(ap->flags));
623 }
624
625 static unsigned int pdc_sata_hotplug_offset(const struct ata_port *ap)
626 {
627         return (ap->flags & PDC_FLAG_GEN_II) ? PDC2_SATA_PLUG_CSR : PDC_SATA_PLUG_CSR;
628 }
629
630 static void pdc_freeze(struct ata_port *ap)
631 {
632         void __iomem *ata_mmio = ap->ioaddr.cmd_addr;
633         u32 tmp;
634
635         tmp = readl(ata_mmio + PDC_CTLSTAT);
636         tmp |= PDC_IRQ_DISABLE;
637         tmp &= ~PDC_DMA_ENABLE;
638         writel(tmp, ata_mmio + PDC_CTLSTAT);
639         readl(ata_mmio + PDC_CTLSTAT); /* flush */
640 }
641
642 static void pdc_sata_freeze(struct ata_port *ap)
643 {
644         struct ata_host *host = ap->host;
645         void __iomem *host_mmio = host->iomap[PDC_MMIO_BAR];
646         unsigned int hotplug_offset = pdc_sata_hotplug_offset(ap);
647         unsigned int ata_no = pdc_sata_ata_port_to_ata_no(ap);
648         u32 hotplug_status;
649
650         /* Disable hotplug events on this port.
651          *
652          * Locking:
653          * 1) hotplug register accesses must be serialised via host->lock
654          * 2) ap->lock == &ap->host->lock
655          * 3) ->freeze() and ->thaw() are called with ap->lock held
656          */
657         hotplug_status = readl(host_mmio + hotplug_offset);
658         hotplug_status |= 0x11 << (ata_no + 16);
659         writel(hotplug_status, host_mmio + hotplug_offset);
660         readl(host_mmio + hotplug_offset); /* flush */
661
662         pdc_freeze(ap);
663 }
664
665 static void pdc_thaw(struct ata_port *ap)
666 {
667         void __iomem *ata_mmio = ap->ioaddr.cmd_addr;
668         u32 tmp;
669
670         /* clear IRQ */
671         readl(ata_mmio + PDC_COMMAND);
672
673         /* turn IRQ back on */
674         tmp = readl(ata_mmio + PDC_CTLSTAT);
675         tmp &= ~PDC_IRQ_DISABLE;
676         writel(tmp, ata_mmio + PDC_CTLSTAT);
677         readl(ata_mmio + PDC_CTLSTAT); /* flush */
678 }
679
680 static void pdc_sata_thaw(struct ata_port *ap)
681 {
682         struct ata_host *host = ap->host;
683         void __iomem *host_mmio = host->iomap[PDC_MMIO_BAR];
684         unsigned int hotplug_offset = pdc_sata_hotplug_offset(ap);
685         unsigned int ata_no = pdc_sata_ata_port_to_ata_no(ap);
686         u32 hotplug_status;
687
688         pdc_thaw(ap);
689
690         /* Enable hotplug events on this port.
691          * Locking: see pdc_sata_freeze().
692          */
693         hotplug_status = readl(host_mmio + hotplug_offset);
694         hotplug_status |= 0x11 << ata_no;
695         hotplug_status &= ~(0x11 << (ata_no + 16));
696         writel(hotplug_status, host_mmio + hotplug_offset);
697         readl(host_mmio + hotplug_offset); /* flush */
698 }
699
700 static void pdc_error_handler(struct ata_port *ap)
701 {
702         if (!(ap->pflags & ATA_PFLAG_FROZEN))
703                 pdc_reset_port(ap);
704
705         ata_std_error_handler(ap);
706 }
707
708 static void pdc_post_internal_cmd(struct ata_queued_cmd *qc)
709 {
710         struct ata_port *ap = qc->ap;
711
712         /* make DMA engine forget about the failed command */
713         if (qc->flags & ATA_QCFLAG_FAILED)
714                 pdc_reset_port(ap);
715 }
716
717 static void pdc_error_intr(struct ata_port *ap, struct ata_queued_cmd *qc,
718                            u32 port_status, u32 err_mask)
719 {
720         struct ata_eh_info *ehi = &ap->link.eh_info;
721         unsigned int ac_err_mask = 0;
722
723         ata_ehi_clear_desc(ehi);
724         ata_ehi_push_desc(ehi, "port_status 0x%08x", port_status);
725         port_status &= err_mask;
726
727         if (port_status & PDC_DRIVE_ERR)
728                 ac_err_mask |= AC_ERR_DEV;
729         if (port_status & (PDC_OVERRUN_ERR | PDC_UNDERRUN_ERR))
730                 ac_err_mask |= AC_ERR_HSM;
731         if (port_status & (PDC2_ATA_HBA_ERR | PDC2_ATA_DMA_CNT_ERR))
732                 ac_err_mask |= AC_ERR_ATA_BUS;
733         if (port_status & (PDC_PH_ERR | PDC_SH_ERR | PDC_DH_ERR | PDC2_HTO_ERR
734                            | PDC_PCI_SYS_ERR | PDC1_PCI_PARITY_ERR))
735                 ac_err_mask |= AC_ERR_HOST_BUS;
736
737         if (sata_scr_valid(&ap->link)) {
738                 u32 serror;
739
740                 pdc_sata_scr_read(ap, SCR_ERROR, &serror);
741                 ehi->serror |= serror;
742         }
743
744         qc->err_mask |= ac_err_mask;
745
746         pdc_reset_port(ap);
747
748         ata_port_abort(ap);
749 }
750
751 static inline unsigned int pdc_host_intr(struct ata_port *ap,
752                                          struct ata_queued_cmd *qc)
753 {
754         unsigned int handled = 0;
755         void __iomem *ata_mmio = ap->ioaddr.cmd_addr;
756         u32 port_status, err_mask;
757
758         err_mask = PDC_ERR_MASK;
759         if (ap->flags & PDC_FLAG_GEN_II)
760                 err_mask &= ~PDC1_ERR_MASK;
761         else
762                 err_mask &= ~PDC2_ERR_MASK;
763         port_status = readl(ata_mmio + PDC_GLOBAL_CTL);
764         if (unlikely(port_status & err_mask)) {
765                 pdc_error_intr(ap, qc, port_status, err_mask);
766                 return 1;
767         }
768
769         switch (qc->tf.protocol) {
770         case ATA_PROT_DMA:
771         case ATA_PROT_NODATA:
772         case ATAPI_PROT_DMA:
773         case ATAPI_PROT_NODATA:
774                 qc->err_mask |= ac_err_mask(ata_wait_idle(ap));
775                 ata_qc_complete(qc);
776                 handled = 1;
777                 break;
778
779         default:
780                 ap->stats.idle_irq++;
781                 break;
782         }
783
784         return handled;
785 }
786
787 static void pdc_irq_clear(struct ata_port *ap)
788 {
789         void __iomem *ata_mmio = ap->ioaddr.cmd_addr;
790
791         readl(ata_mmio + PDC_COMMAND);
792 }
793
794 static irqreturn_t pdc_interrupt(int irq, void *dev_instance)
795 {
796         struct ata_host *host = dev_instance;
797         struct ata_port *ap;
798         u32 mask = 0;
799         unsigned int i, tmp;
800         unsigned int handled = 0;
801         void __iomem *host_mmio;
802         unsigned int hotplug_offset, ata_no;
803         u32 hotplug_status;
804         int is_sataii_tx4;
805
806         VPRINTK("ENTER\n");
807
808         if (!host || !host->iomap[PDC_MMIO_BAR]) {
809                 VPRINTK("QUICK EXIT\n");
810                 return IRQ_NONE;
811         }
812
813         host_mmio = host->iomap[PDC_MMIO_BAR];
814
815         spin_lock(&host->lock);
816
817         /* read and clear hotplug flags for all ports */
818         if (host->ports[0]->flags & PDC_FLAG_GEN_II)
819                 hotplug_offset = PDC2_SATA_PLUG_CSR;
820         else
821                 hotplug_offset = PDC_SATA_PLUG_CSR;
822         hotplug_status = readl(host_mmio + hotplug_offset);
823         if (hotplug_status & 0xff)
824                 writel(hotplug_status | 0xff, host_mmio + hotplug_offset);
825         hotplug_status &= 0xff; /* clear uninteresting bits */
826
827         /* reading should also clear interrupts */
828         mask = readl(host_mmio + PDC_INT_SEQMASK);
829
830         if (mask == 0xffffffff && hotplug_status == 0) {
831                 VPRINTK("QUICK EXIT 2\n");
832                 goto done_irq;
833         }
834
835         mask &= 0xffff;         /* only 16 tags possible */
836         if (mask == 0 && hotplug_status == 0) {
837                 VPRINTK("QUICK EXIT 3\n");
838                 goto done_irq;
839         }
840
841         writel(mask, host_mmio + PDC_INT_SEQMASK);
842
843         is_sataii_tx4 = pdc_is_sataii_tx4(host->ports[0]->flags);
844
845         for (i = 0; i < host->n_ports; i++) {
846                 VPRINTK("port %u\n", i);
847                 ap = host->ports[i];
848
849                 /* check for a plug or unplug event */
850                 ata_no = pdc_port_no_to_ata_no(i, is_sataii_tx4);
851                 tmp = hotplug_status & (0x11 << ata_no);
852                 if (tmp && ap &&
853                     !(ap->flags & ATA_FLAG_DISABLED)) {
854                         struct ata_eh_info *ehi = &ap->link.eh_info;
855                         ata_ehi_clear_desc(ehi);
856                         ata_ehi_hotplugged(ehi);
857                         ata_ehi_push_desc(ehi, "hotplug_status %#x", tmp);
858                         ata_port_freeze(ap);
859                         ++handled;
860                         continue;
861                 }
862
863                 /* check for a packet interrupt */
864                 tmp = mask & (1 << (i + 1));
865                 if (tmp && ap &&
866                     !(ap->flags & ATA_FLAG_DISABLED)) {
867                         struct ata_queued_cmd *qc;
868
869                         qc = ata_qc_from_tag(ap, ap->link.active_tag);
870                         if (qc && (!(qc->tf.flags & ATA_TFLAG_POLLING)))
871                                 handled += pdc_host_intr(ap, qc);
872                 }
873         }
874
875         VPRINTK("EXIT\n");
876
877 done_irq:
878         spin_unlock(&host->lock);
879         return IRQ_RETVAL(handled);
880 }
881
882 static inline void pdc_packet_start(struct ata_queued_cmd *qc)
883 {
884         struct ata_port *ap = qc->ap;
885         struct pdc_port_priv *pp = ap->private_data;
886         void __iomem *host_mmio = ap->host->iomap[PDC_MMIO_BAR];
887         void __iomem *ata_mmio = ap->ioaddr.cmd_addr;
888         unsigned int port_no = ap->port_no;
889         u8 seq = (u8) (port_no + 1);
890
891         VPRINTK("ENTER, ap %p\n", ap);
892
893         writel(0x00000001, host_mmio + (seq * 4));
894         readl(host_mmio + (seq * 4));   /* flush */
895
896         pp->pkt[2] = seq;
897         wmb();                  /* flush PRD, pkt writes */
898         writel(pp->pkt_dma, ata_mmio + PDC_PKT_SUBMIT);
899         readl(ata_mmio + PDC_PKT_SUBMIT); /* flush */
900 }
901
902 static unsigned int pdc_qc_issue(struct ata_queued_cmd *qc)
903 {
904         switch (qc->tf.protocol) {
905         case ATAPI_PROT_NODATA:
906                 if (qc->dev->flags & ATA_DFLAG_CDB_INTR)
907                         break;
908                 /*FALLTHROUGH*/
909         case ATA_PROT_NODATA:
910                 if (qc->tf.flags & ATA_TFLAG_POLLING)
911                         break;
912                 /*FALLTHROUGH*/
913         case ATAPI_PROT_DMA:
914         case ATA_PROT_DMA:
915                 pdc_packet_start(qc);
916                 return 0;
917
918         default:
919                 break;
920         }
921
922         return ata_sff_qc_issue(qc);
923 }
924
925 static void pdc_tf_load_mmio(struct ata_port *ap, const struct ata_taskfile *tf)
926 {
927         WARN_ON(tf->protocol == ATA_PROT_DMA || tf->protocol == ATAPI_PROT_DMA);
928         ata_sff_tf_load(ap, tf);
929 }
930
931 static void pdc_exec_command_mmio(struct ata_port *ap,
932                                   const struct ata_taskfile *tf)
933 {
934         WARN_ON(tf->protocol == ATA_PROT_DMA || tf->protocol == ATAPI_PROT_DMA);
935         ata_sff_exec_command(ap, tf);
936 }
937
938 static int pdc_check_atapi_dma(struct ata_queued_cmd *qc)
939 {
940         u8 *scsicmd = qc->scsicmd->cmnd;
941         int pio = 1; /* atapi dma off by default */
942
943         /* Whitelist commands that may use DMA. */
944         switch (scsicmd[0]) {
945         case WRITE_12:
946         case WRITE_10:
947         case WRITE_6:
948         case READ_12:
949         case READ_10:
950         case READ_6:
951         case 0xad: /* READ_DVD_STRUCTURE */
952         case 0xbe: /* READ_CD */
953                 pio = 0;
954         }
955         /* -45150 (FFFF4FA2) to -1 (FFFFFFFF) shall use PIO mode */
956         if (scsicmd[0] == WRITE_10) {
957                 unsigned int lba =
958                         (scsicmd[2] << 24) |
959                         (scsicmd[3] << 16) |
960                         (scsicmd[4] << 8) |
961                         scsicmd[5];
962                 if (lba >= 0xFFFF4FA2)
963                         pio = 1;
964         }
965         return pio;
966 }
967
968 static int pdc_old_sata_check_atapi_dma(struct ata_queued_cmd *qc)
969 {
970         /* First generation chips cannot use ATAPI DMA on SATA ports */
971         return 1;
972 }
973
974 static void pdc_ata_setup_port(struct ata_port *ap,
975                                void __iomem *base, void __iomem *scr_addr)
976 {
977         ap->ioaddr.cmd_addr             = base;
978         ap->ioaddr.data_addr            = base;
979         ap->ioaddr.feature_addr         =
980         ap->ioaddr.error_addr           = base + 0x4;
981         ap->ioaddr.nsect_addr           = base + 0x8;
982         ap->ioaddr.lbal_addr            = base + 0xc;
983         ap->ioaddr.lbam_addr            = base + 0x10;
984         ap->ioaddr.lbah_addr            = base + 0x14;
985         ap->ioaddr.device_addr          = base + 0x18;
986         ap->ioaddr.command_addr         =
987         ap->ioaddr.status_addr          = base + 0x1c;
988         ap->ioaddr.altstatus_addr       =
989         ap->ioaddr.ctl_addr             = base + 0x38;
990         ap->ioaddr.scr_addr             = scr_addr;
991 }
992
993 static void pdc_host_init(struct ata_host *host)
994 {
995         void __iomem *host_mmio = host->iomap[PDC_MMIO_BAR];
996         int is_gen2 = host->ports[0]->flags & PDC_FLAG_GEN_II;
997         int hotplug_offset;
998         u32 tmp;
999
1000         if (is_gen2)
1001                 hotplug_offset = PDC2_SATA_PLUG_CSR;
1002         else
1003                 hotplug_offset = PDC_SATA_PLUG_CSR;
1004
1005         /*
1006          * Except for the hotplug stuff, this is voodoo from the
1007          * Promise driver.  Label this entire section
1008          * "TODO: figure out why we do this"
1009          */
1010
1011         /* enable BMR_BURST, maybe change FIFO_SHD to 8 dwords */
1012         tmp = readl(host_mmio + PDC_FLASH_CTL);
1013         tmp |= 0x02000; /* bit 13 (enable bmr burst) */
1014         if (!is_gen2)
1015                 tmp |= 0x10000; /* bit 16 (fifo threshold at 8 dw) */
1016         writel(tmp, host_mmio + PDC_FLASH_CTL);
1017
1018         /* clear plug/unplug flags for all ports */
1019         tmp = readl(host_mmio + hotplug_offset);
1020         writel(tmp | 0xff, host_mmio + hotplug_offset);
1021
1022         /* unmask plug/unplug ints */
1023         tmp = readl(host_mmio + hotplug_offset);
1024         writel(tmp & ~0xff0000, host_mmio + hotplug_offset);
1025
1026         /* don't initialise TBG or SLEW on 2nd generation chips */
1027         if (is_gen2)
1028                 return;
1029
1030         /* reduce TBG clock to 133 Mhz. */
1031         tmp = readl(host_mmio + PDC_TBG_MODE);
1032         tmp &= ~0x30000; /* clear bit 17, 16*/
1033         tmp |= 0x10000;  /* set bit 17:16 = 0:1 */
1034         writel(tmp, host_mmio + PDC_TBG_MODE);
1035
1036         readl(host_mmio + PDC_TBG_MODE);        /* flush */
1037         msleep(10);
1038
1039         /* adjust slew rate control register. */
1040         tmp = readl(host_mmio + PDC_SLEW_CTL);
1041         tmp &= 0xFFFFF03F; /* clear bit 11 ~ 6 */
1042         tmp  |= 0x00000900; /* set bit 11-9 = 100b , bit 8-6 = 100 */
1043         writel(tmp, host_mmio + PDC_SLEW_CTL);
1044 }
1045
1046 static int pdc_ata_init_one(struct pci_dev *pdev,
1047                             const struct pci_device_id *ent)
1048 {
1049         static int printed_version;
1050         const struct ata_port_info *pi = &pdc_port_info[ent->driver_data];
1051         const struct ata_port_info *ppi[PDC_MAX_PORTS];
1052         struct ata_host *host;
1053         void __iomem *host_mmio;
1054         int n_ports, i, rc;
1055         int is_sataii_tx4;
1056
1057         if (!printed_version++)
1058                 dev_printk(KERN_DEBUG, &pdev->dev, "version " DRV_VERSION "\n");
1059
1060         /* enable and acquire resources */
1061         rc = pcim_enable_device(pdev);
1062         if (rc)
1063                 return rc;
1064
1065         rc = pcim_iomap_regions(pdev, 1 << PDC_MMIO_BAR, DRV_NAME);
1066         if (rc == -EBUSY)
1067                 pcim_pin_device(pdev);
1068         if (rc)
1069                 return rc;
1070         host_mmio = pcim_iomap_table(pdev)[PDC_MMIO_BAR];
1071
1072         /* determine port configuration and setup host */
1073         n_ports = 2;
1074         if (pi->flags & PDC_FLAG_4_PORTS)
1075                 n_ports = 4;
1076         for (i = 0; i < n_ports; i++)
1077                 ppi[i] = pi;
1078
1079         if (pi->flags & PDC_FLAG_SATA_PATA) {
1080                 u8 tmp = readb(host_mmio + PDC_FLASH_CTL + 1);
1081                 if (!(tmp & 0x80))
1082                         ppi[n_ports++] = pi + 1;
1083         }
1084
1085         host = ata_host_alloc_pinfo(&pdev->dev, ppi, n_ports);
1086         if (!host) {
1087                 dev_printk(KERN_ERR, &pdev->dev, "failed to allocate host\n");
1088                 return -ENOMEM;
1089         }
1090         host->iomap = pcim_iomap_table(pdev);
1091
1092         is_sataii_tx4 = pdc_is_sataii_tx4(pi->flags);
1093         for (i = 0; i < host->n_ports; i++) {
1094                 struct ata_port *ap = host->ports[i];
1095                 unsigned int ata_no = pdc_port_no_to_ata_no(i, is_sataii_tx4);
1096                 unsigned int ata_offset = 0x200 + ata_no * 0x80;
1097                 unsigned int scr_offset = 0x400 + ata_no * 0x100;
1098
1099                 pdc_ata_setup_port(ap, host_mmio + ata_offset, host_mmio + scr_offset);
1100
1101                 ata_port_pbar_desc(ap, PDC_MMIO_BAR, -1, "mmio");
1102                 ata_port_pbar_desc(ap, PDC_MMIO_BAR, ata_offset, "ata");
1103         }
1104
1105         /* initialize adapter */
1106         pdc_host_init(host);
1107
1108         rc = pci_set_dma_mask(pdev, ATA_DMA_MASK);
1109         if (rc)
1110                 return rc;
1111         rc = pci_set_consistent_dma_mask(pdev, ATA_DMA_MASK);
1112         if (rc)
1113                 return rc;
1114
1115         /* start host, request IRQ and attach */
1116         pci_set_master(pdev);
1117         return ata_host_activate(host, pdev->irq, pdc_interrupt, IRQF_SHARED,
1118                                  &pdc_ata_sht);
1119 }
1120
1121 static int __init pdc_ata_init(void)
1122 {
1123         return pci_register_driver(&pdc_ata_pci_driver);
1124 }
1125
1126 static void __exit pdc_ata_exit(void)
1127 {
1128         pci_unregister_driver(&pdc_ata_pci_driver);
1129 }
1130
1131 MODULE_AUTHOR("Jeff Garzik");
1132 MODULE_DESCRIPTION("Promise ATA TX2/TX4/TX4000 low-level driver");
1133 MODULE_LICENSE("GPL");
1134 MODULE_DEVICE_TABLE(pci, pdc_ata_pci_tbl);
1135 MODULE_VERSION(DRV_VERSION);
1136
1137 module_init(pdc_ata_init);
1138 module_exit(pdc_ata_exit);