34 #include <linux/kernel.h>
35 #include <linux/module.h>
37 #include <linux/pci.h>
42 #include <linux/device.h>
43 #include <scsi/scsi.h>
49 #define DRV_NAME "sata_promise"
50 #define DRV_VERSION "2.12"
153 static int pdc_common_port_start(
struct ata_port *ap);
154 static int pdc_sata_port_start(
struct ata_port *ap);
159 static int pdc_old_sata_check_atapi_dma(
struct ata_queued_cmd *qc);
160 static void pdc_irq_clear(
struct ata_port *ap);
162 static void pdc_freeze(
struct ata_port *ap);
163 static void pdc_sata_freeze(
struct ata_port *ap);
164 static void pdc_thaw(
struct ata_port *ap);
165 static void pdc_sata_thaw(
struct ata_port *ap);
166 static int pdc_pata_softreset(
struct ata_link *
link,
unsigned int *
class,
167 unsigned long deadline);
168 static int pdc_sata_hardreset(
struct ata_link *
link,
unsigned int *
class,
169 unsigned long deadline);
170 static void pdc_error_handler(
struct ata_port *ap);
172 static int pdc_pata_cable_detect(
struct ata_port *ap);
173 static int pdc_sata_cable_detect(
struct ata_port *ap);
184 .sff_tf_load = pdc_tf_load_mmio,
185 .sff_exec_command = pdc_exec_command_mmio,
186 .check_atapi_dma = pdc_check_atapi_dma,
187 .qc_prep = pdc_qc_prep,
188 .qc_issue = pdc_qc_issue,
190 .sff_irq_clear = pdc_irq_clear,
193 .post_internal_cmd = pdc_post_internal_cmd,
194 .error_handler = pdc_error_handler,
198 .inherits = &pdc_common_ops,
200 .freeze = pdc_sata_freeze,
201 .thaw = pdc_sata_thaw,
202 .scr_read = pdc_sata_scr_read,
203 .scr_write = pdc_sata_scr_write,
204 .port_start = pdc_sata_port_start,
205 .hardreset = pdc_sata_hardreset,
211 .inherits = &pdc_sata_ops,
214 .check_atapi_dma = pdc_old_sata_check_atapi_dma,
218 .inherits = &pdc_common_ops,
220 .freeze = pdc_freeze,
222 .port_start = pdc_common_port_start,
223 .softreset = pdc_pata_softreset,
234 .port_ops = &pdc_old_sata_ops,
243 .port_ops = &pdc_pata_ops,
253 .port_ops = &pdc_old_sata_ops,
263 .port_ops = &pdc_pata_ops,
273 .port_ops = &pdc_sata_ops,
283 .port_ops = &pdc_pata_ops,
293 .port_ops = &pdc_sata_ops,
321 static struct pci_driver pdc_ata_pci_driver = {
323 .id_table = pdc_ata_pci_tbl,
324 .probe = pdc_ata_init_one,
325 .remove = ata_pci_remove_one,
328 static int pdc_common_port_start(
struct ata_port *ap)
335 rc = ata_bmdma_port_start(ap);
352 static int pdc_sata_port_start(
struct ata_port *ap)
356 rc = pdc_common_port_start(ap);
362 void __iomem *sata_mmio = ap->ioaddr.scr_addr;
366 tmp = (tmp & ~3) | 1;
373 static void pdc_fpdma_clear_interrupt_flag(
struct ata_port *ap)
375 void __iomem *sata_mmio = ap->ioaddr.scr_addr;
388 static void pdc_fpdma_reset(
struct ata_port *ap)
390 void __iomem *sata_mmio = ap->ioaddr.scr_addr;
403 pdc_fpdma_clear_interrupt_flag(ap);
406 static void pdc_not_at_command_packet_phase(
struct ata_port *ap)
408 void __iomem *sata_mmio = ap->ioaddr.scr_addr;
413 for (i = 0; i < 100; ++
i) {
416 if ((tmp & 0xF) != 1)
422 static void pdc_clear_internal_debug_record_error_register(
struct ata_port *ap)
424 void __iomem *sata_mmio = ap->ioaddr.scr_addr;
430 static void pdc_reset_port(
struct ata_port *ap)
437 pdc_not_at_command_packet_phase(ap);
439 tmp =
readl(ata_ctlstat_mmio);
441 writel(tmp, ata_ctlstat_mmio);
443 for (i = 11; i > 0; i--) {
444 tmp =
readl(ata_ctlstat_mmio);
451 writel(tmp, ata_ctlstat_mmio);
455 writel(tmp, ata_ctlstat_mmio);
456 readl(ata_ctlstat_mmio);
460 pdc_clear_internal_debug_record_error_register(ap);
464 static int pdc_pata_cable_detect(
struct ata_port *ap)
467 void __iomem *ata_mmio = ap->ioaddr.cmd_addr;
475 static int pdc_sata_cable_detect(
struct ata_port *ap)
481 unsigned int sc_reg,
u32 *
val)
485 *val =
readl(link->
ap->ioaddr.scr_addr + (sc_reg * 4));
489 static int pdc_sata_scr_write(
struct ata_link *link,
490 unsigned int sc_reg,
u32 val)
494 writel(val, link->
ap->ioaddr.scr_addr + (sc_reg * 4));
512 switch (qc->
tf.protocol) {
533 dev_sel = qc->
tf.device;
541 buf[17] = qc->
tf.nsect;
543 buf[19] = qc->
tf.lbal;
554 buf[23] = qc->
tf.lbam;
556 buf[25] = qc->
tf.lbah;
560 buf[27] = qc->
tf.command;
571 memcpy(buf+31, cdb, cdb_len);
591 const u32 SG_COUNT_ASIC_BUG = 41*4;
592 unsigned int si,
idx;
611 offset = addr & 0xffff;
613 if ((offset + sg_len) > 0x10000)
618 VPRINTK(
"PRD[%u] = (0x%X, 0x%X)\n", idx, addr, len);
628 if (len > SG_COUNT_ASIC_BUG) {
631 VPRINTK(
"Splitting last PRD.\n");
635 VPRINTK(
"PRD[%u] = (0x%X, 0x%X)\n", idx - 1, addr, SG_COUNT_ASIC_BUG);
637 addr = addr + len - SG_COUNT_ASIC_BUG;
638 len = SG_COUNT_ASIC_BUG;
641 VPRINTK(
"PRD[%u] = (0x%X, 0x%X)\n", idx, addr, len);
656 switch (qc->
tf.protocol) {
661 i = pdc_pkt_header(&qc->
tf, qc->
ap->bmdma_prd_dma,
664 i = pdc_prep_lba48(&qc->
tf, pp->
pkt, i);
666 i = pdc_prep_lba28(&qc->
tf, pp->
pkt, i);
667 pdc_pkt_footer(&qc->
tf, pp->
pkt, i);
683 static int pdc_is_sataii_tx4(
unsigned long flags)
686 return (flags & mask) ==
mask;
689 static unsigned int pdc_port_no_to_ata_no(
unsigned int port_no,
692 static const unsigned char sataii_tx4_port_remap[4] = { 3, 1, 0, 2};
696 static unsigned int pdc_sata_nr_ports(
const struct ata_port *ap)
701 static unsigned int pdc_sata_ata_port_to_ata_no(
const struct ata_port *ap)
704 unsigned int nr_ports = pdc_sata_nr_ports(ap);
707 for (i = 0; i < nr_ports && host->
ports[
i] != ap; ++
i)
710 return pdc_port_no_to_ata_no(i, pdc_is_sataii_tx4(ap->
flags));
713 static void pdc_freeze(
struct ata_port *ap)
715 void __iomem *ata_mmio = ap->ioaddr.cmd_addr;
725 static void pdc_sata_freeze(
struct ata_port *ap)
730 unsigned int ata_no = pdc_sata_ata_port_to_ata_no(ap);
740 hotplug_status =
readl(host_mmio + hotplug_offset);
741 hotplug_status |= 0x11 << (ata_no + 16);
742 writel(hotplug_status, host_mmio + hotplug_offset);
743 readl(host_mmio + hotplug_offset);
748 static void pdc_thaw(
struct ata_port *ap)
750 void __iomem *ata_mmio = ap->ioaddr.cmd_addr;
763 static void pdc_sata_thaw(
struct ata_port *ap)
768 unsigned int ata_no = pdc_sata_ata_port_to_ata_no(ap);
776 hotplug_status =
readl(host_mmio + hotplug_offset);
777 hotplug_status |= 0x11 << ata_no;
778 hotplug_status &= ~(0x11 << (ata_no + 16));
779 writel(hotplug_status, host_mmio + hotplug_offset);
780 readl(host_mmio + hotplug_offset);
783 static int pdc_pata_softreset(
struct ata_link *link,
unsigned int *
class,
784 unsigned long deadline)
786 pdc_reset_port(link->
ap);
790 static unsigned int pdc_ata_port_to_ata_no(
const struct ata_port *ap)
792 void __iomem *ata_mmio = ap->ioaddr.cmd_addr;
796 return (ata_mmio - host_mmio - 0x200) / 0x80;
799 static void pdc_hard_reset_port(
struct ata_port *ap)
803 unsigned int ata_no = pdc_ata_port_to_ata_no(ap);
806 spin_lock(&ap->
host->lock);
808 tmp =
readb(pcictl_b1_mmio);
809 tmp &= ~(0x10 << ata_no);
810 writeb(tmp, pcictl_b1_mmio);
811 readb(pcictl_b1_mmio);
813 tmp |= (0x10 << ata_no);
814 writeb(tmp, pcictl_b1_mmio);
815 readb(pcictl_b1_mmio);
817 spin_unlock(&ap->
host->lock);
820 static int pdc_sata_hardreset(
struct ata_link *link,
unsigned int *
class,
821 unsigned long deadline)
824 pdc_not_at_command_packet_phase(link->
ap);
826 pdc_hard_reset_port(link->
ap);
827 pdc_reset_port(link->
ap);
836 static void pdc_error_handler(
struct ata_port *ap)
857 unsigned int ac_err_mask = 0;
887 static unsigned int pdc_host_intr(
struct ata_port *ap,
890 unsigned int handled = 0;
891 void __iomem *ata_mmio = ap->ioaddr.cmd_addr;
900 if (
unlikely(port_status & err_mask)) {
901 pdc_error_intr(ap, qc, port_status, err_mask);
905 switch (qc->
tf.protocol) {
910 qc->
err_mask |= ac_err_mask(ata_wait_idle(ap));
915 ap->
stats.idle_irq++;
922 static void pdc_irq_clear(
struct ata_port *ap)
924 void __iomem *ata_mmio = ap->ioaddr.cmd_addr;
929 static irqreturn_t pdc_interrupt(
int irq,
void *dev_instance)
931 struct ata_host *host = dev_instance;
935 unsigned int handled = 0;
937 unsigned int hotplug_offset, ata_no;
950 spin_lock(&host->
lock);
955 hotplug_status =
readl(host_mmio + hotplug_offset);
956 if (hotplug_status & 0xff)
957 writel(hotplug_status | 0xff, host_mmio + hotplug_offset);
958 hotplug_status &= 0xff;
965 if (mask == 0xffffffff && hotplug_status == 0) {
971 if (mask == 0 && hotplug_status == 0) {
978 is_sataii_tx4 = pdc_is_sataii_tx4(host->
ports[0]->flags);
980 for (i = 0; i < host->
n_ports; i++) {
985 ata_no = pdc_port_no_to_ata_no(i, is_sataii_tx4);
986 tmp = hotplug_status & (0x11 << ata_no);
990 ata_ehi_hotplugged(ehi);
998 tmp = mask & (1 << (i + 1));
1002 qc = ata_qc_from_tag(ap, ap->
link.active_tag);
1004 handled += pdc_host_intr(ap, qc);
1011 spin_unlock(&host->
lock);
1020 void __iomem *ata_mmio = ap->ioaddr.cmd_addr;
1024 VPRINTK(
"ENTER, ap %p\n", ap);
1026 writel(0x00000001, host_mmio + (seq * 4));
1027 readl(host_mmio + (seq * 4));
1037 switch (qc->
tf.protocol) {
1048 pdc_packet_start(qc);
1062 static void pdc_exec_command_mmio(
struct ata_port *ap,
1075 switch (scsicmd[0]) {
1089 (scsicmd[2] << 24) |
1090 (scsicmd[3] << 16) |
1093 if (lba >= 0xFFFF4FA2)
1099 static int pdc_old_sata_check_atapi_dma(
struct ata_queued_cmd *qc)
1105 static void pdc_ata_setup_port(
struct ata_port *ap,
1108 ap->ioaddr.cmd_addr = base;
1109 ap->ioaddr.data_addr = base;
1110 ap->ioaddr.feature_addr =
1111 ap->ioaddr.error_addr = base + 0x4;
1112 ap->ioaddr.nsect_addr = base + 0x8;
1113 ap->ioaddr.lbal_addr = base + 0xc;
1114 ap->ioaddr.lbam_addr = base + 0x10;
1115 ap->ioaddr.lbah_addr = base + 0x14;
1116 ap->ioaddr.device_addr = base + 0x18;
1117 ap->ioaddr.command_addr =
1118 ap->ioaddr.status_addr = base + 0x1c;
1119 ap->ioaddr.altstatus_addr =
1120 ap->ioaddr.ctl_addr = base + 0x38;
1121 ap->ioaddr.scr_addr = scr_addr;
1124 static void pdc_host_init(
struct ata_host *host)
1150 tmp =
readl(host_mmio + hotplug_offset);
1151 writel(tmp | 0xff, host_mmio + hotplug_offset);
1153 tmp =
readl(host_mmio + hotplug_offset);
1155 writel(tmp & ~0xff0000, host_mmio + hotplug_offset);
1157 writel(tmp | 0xff0000, host_mmio + hotplug_offset);
1179 static int pdc_ata_init_one(
struct pci_dev *pdev,
1213 ppi[n_ports++] = pi + 1;
1218 dev_err(&pdev->
dev,
"failed to allocate host\n");
1223 is_sataii_tx4 = pdc_is_sataii_tx4(pi->
flags);
1224 for (i = 0; i < host->
n_ports; i++) {
1226 unsigned int ata_no = pdc_port_no_to_ata_no(i, is_sataii_tx4);
1227 unsigned int ata_offset = 0x200 + ata_no * 0x80;
1228 unsigned int scr_offset = 0x400 + ata_no * 0x100;
1230 pdc_ata_setup_port(ap, host_mmio + ata_offset, host_mmio + scr_offset);
1233 ata_port_pbar_desc(ap,
PDC_MMIO_BAR, ata_offset,
"ata");
1237 pdc_host_init(host);