81 #include <linux/kernel.h>
82 #include <linux/module.h>
83 #include <linux/pci.h>
84 #include <linux/slab.h>
89 #include <linux/device.h>
95 #define DRV_NAME "sata_sx4"
96 #define DRV_VERSION "0.12"
197 #define ECC_ERASE_BUF_SZ (128 * 1024)
218 static void pdc_error_handler(
struct ata_port *
ap);
221 static int pdc_port_start(
struct ata_port *
ap);
225 static unsigned int pdc20621_dimm_init(
struct ata_host *
host);
227 static unsigned int pdc20621_i2c_read(
struct ata_host *
host,
230 static unsigned int pdc20621_prog_dimm_global(
struct ata_host *
host);
231 #ifdef ATA_VERBOSE_DEBUG
232 static void pdc20621_get_from_dimm(
struct ata_host *
host,
237 static void pdc20621_irq_clear(
struct ata_port *
ap);
239 static int pdc_softreset(
struct ata_link *
link,
unsigned int *
class,
240 unsigned long deadline);
255 .check_atapi_dma = pdc_check_atapi_dma,
256 .qc_prep = pdc20621_qc_prep,
257 .qc_issue = pdc20621_qc_issue,
259 .freeze = pdc_freeze,
261 .softreset = pdc_softreset,
262 .error_handler = pdc_error_handler,
264 .post_internal_cmd = pdc_post_internal_cmd,
266 .port_start = pdc_port_start,
268 .sff_tf_load = pdc_tf_load_mmio,
269 .sff_exec_command = pdc_exec_command_mmio,
270 .sff_irq_clear = pdc20621_irq_clear,
281 .port_ops = &pdc_20621_ops,
292 static struct pci_driver pdc_sata_pci_driver = {
294 .id_table = pdc_sata_pci_tbl,
295 .probe = pdc_sata_init_one,
296 .remove = ata_pci_remove_one,
300 static int pdc_port_start(
struct ata_port *ap)
329 VPRINTK(
"ATA sg addr 0x%x, %d\n", addr, addr);
333 VPRINTK(
"ATA PSG @ %x == (0x%x, 0x%x)\n",
337 buf32[dw], buf32[dw + 1]);
340 static inline void pdc20621_host_sg(
struct ata_taskfile *tf,
u8 *buf,
342 unsigned int total_len)
355 VPRINTK(
"HOST PSG @ %x == (0x%x, 0x%x)\n",
359 buf32[dw], buf32[dw + 1]);
362 static inline unsigned int pdc20621_ata_pkt(
struct ata_taskfile *tf,
373 VPRINTK(
"ENTER, dimm_sg == 0x%x, %d\n", dimm_sg, dimm_sg);
387 buf[i++] = portno + 1;
415 static inline void pdc20621_host_pkt(
struct ata_taskfile *tf,
u8 *buf,
428 VPRINTK(
"ENTER, dimm_sg == 0x%x, %d\n", dimm_sg, dimm_sg);
429 VPRINTK(
"host_sg == 0x%x, %d\n", host_sg, host_sg);
440 tmp |= ((portno + 1 + 4) << 16);
447 VPRINTK(
"HOST PKT @ %x == (0x%x 0x%x 0x%x 0x%x)\n",
463 unsigned int portno = ap->
port_no;
464 unsigned int i, si,
idx, total_len = 0, sgt_len;
489 pdc20621_host_sg(&qc->
tf, &pp->
dimm_buf[0], portno, total_len);
490 pdc20621_host_pkt(&qc->
tf, &pp->
dimm_buf[0], portno);
492 pdc20621_ata_sg(&qc->
tf, &pp->
dimm_buf[0], portno, total_len);
493 i = pdc20621_ata_pkt(&qc->
tf, qc->
dev->devno, &pp->
dimm_buf[0], portno);
496 i = pdc_prep_lba48(&qc->
tf, &pp->
dimm_buf[0], i);
498 i = pdc_prep_lba28(&qc->
tf, &pp->
dimm_buf[0], i);
514 VPRINTK(
"ata pkt buf ofs %u, prd size %u, mmio copied\n", i, sgt_len);
523 unsigned int portno = ap->
port_no;
531 i = pdc20621_ata_pkt(&qc->
tf, qc->
dev->devno, &pp->
dimm_buf[0], portno);
534 i = pdc_prep_lba48(&qc->
tf, &pp->
dimm_buf[0], i);
536 i = pdc_prep_lba28(&qc->
tf, &pp->
dimm_buf[0], i);
549 VPRINTK(
"ata pkt buf ofs %u, mmio copied\n", i);
554 switch (qc->
tf.protocol) {
556 pdc20621_dma_prep(qc);
559 pdc20621_nodata_prep(qc);
593 __pdc20621_push_hdma(qc, seq, pkt_ofs);
616 __pdc20621_push_hdma(pp->
hdma[idx].qc, pp->
hdma[idx].seq,
617 pp->
hdma[idx].pkt_ofs);
621 #ifdef ATA_VERBOSE_DEBUG
637 static inline void pdc20621_dump_hdma(
struct ata_queued_cmd *qc) { }
644 unsigned int port_no = ap->
port_no;
647 u8 seq = (
u8) (port_no + 1);
648 unsigned int port_ofs;
663 pdc20621_dump_hdma(qc);
665 VPRINTK(
"queued ofs 0x%x (%u), seq %u\n",
676 VPRINTK(
"submitted ofs 0x%x (%u), seq %u\n",
685 switch (qc->
tf.protocol) {
691 pdc20621_packet_start(qc);
705 static inline unsigned int pdc20621_host_intr(
struct ata_port *ap,
707 unsigned int doing_hdma,
710 unsigned int port_no = ap->
port_no;
711 unsigned int port_ofs =
714 unsigned int handled = 0;
726 qc->
err_mask |= ac_err_mask(ata_wait_idle(ap));
728 pdc20621_pop_hdma(qc);
733 u8 seq = (
u8) (port_no + 1 + 4);
738 pdc20621_dump_hdma(qc);
739 pdc20621_push_hdma(qc, seq,
748 u8 seq = (
u8) (port_no + 1);
765 qc->
err_mask |= ac_err_mask(ata_wait_idle(ap));
767 pdc20621_pop_hdma(qc);
775 DPRINTK(
"BUS_NODATA (drv_stat 0x%X)\n", status);
776 qc->
err_mask |= ac_err_mask(status);
781 ap->
stats.idle_irq++;
787 static void pdc20621_irq_clear(
struct ata_port *ap)
789 ioread8(ap->ioaddr.status_addr);
792 static irqreturn_t pdc20621_interrupt(
int irq,
void *dev_instance)
794 struct ata_host *host = dev_instance;
798 unsigned int handled = 0;
813 VPRINTK(
"mask == 0x%x\n", mask);
815 if (mask == 0xffffffff) {
825 spin_lock(&host->
lock);
827 for (i = 1; i < 9; i++) {
835 tmp = mask & (1 <<
i);
836 VPRINTK(
"seq %u, port_no %u, ap %p, tmp %x\n", i, port_no, ap, tmp);
840 qc = ata_qc_from_tag(ap, ap->
link.active_tag);
842 handled += pdc20621_host_intr(ap, qc, (i > 4),
847 spin_unlock(&host->
lock);
849 VPRINTK(
"mask == 0x%x\n", mask);
856 static void pdc_freeze(
struct ata_port *ap)
858 void __iomem *mmio = ap->ioaddr.cmd_addr;
870 static void pdc_thaw(
struct ata_port *ap)
872 void __iomem *mmio = ap->ioaddr.cmd_addr;
878 ioread8(ap->ioaddr.status_addr);
887 static void pdc_reset_port(
struct ata_port *ap)
895 for (i = 11; i > 0; i--) {
911 static int pdc_softreset(
struct ata_link *
link,
unsigned int *
class,
912 unsigned long deadline)
914 pdc_reset_port(link->
ap);
918 static void pdc_error_handler(
struct ata_port *ap)
941 switch (scsicmd[0]) {
959 if (lba >= 0xFFFF4FA2)
981 static void pdc_sata_setup_port(
struct ata_ioports *
port,
void __iomem *base)
983 port->cmd_addr = base;
984 port->data_addr = base;
986 port->error_addr = base + 0x4;
987 port->nsect_addr = base + 0x8;
988 port->lbal_addr = base + 0xc;
989 port->lbam_addr = base + 0x10;
990 port->lbah_addr = base + 0x14;
991 port->device_addr = base + 0x18;
993 port->status_addr = base + 0x1c;
994 port->altstatus_addr =
995 port->ctl_addr = base + 0x38;
999 #ifdef ATA_VERBOSE_DEBUG
1000 static void pdc20621_get_from_dimm(
struct ata_host *host,
void *psource,
1014 window_size = 0x2000 * 4;
1015 idx = (
u16) (offset / window_size);
1022 offset -= (idx * window_size);
1024 dist = ((
long) (window_size - (offset + size))) >= 0 ? size :
1025 (
long) (window_size - offset);
1026 memcpy_fromio((
char *) psource, (
char *) (dimm_mmio + offset / 4),
1031 for (; (
long) size >= (
long) window_size ;) {
1038 psource += window_size;
1039 size -= window_size;
1055 static void pdc20621_put_to_dimm(
struct ata_host *host,
void *psource,
1069 window_size = 0x2000 * 4;
1070 idx = (
u16) (offset / window_size);
1074 offset -= (idx * window_size);
1076 dist = ((
long)(
s32)(window_size - (offset +
size))) >= 0 ? size :
1077 (
long) (window_size -
offset);
1078 memcpy_toio(dimm_mmio + offset / 4, psource, dist);
1084 for (; (
long) size >= (
long) window_size ;) {
1090 psource += window_size;
1091 size -= window_size;
1116 i2creg |= device << 24;
1117 i2creg |= subaddr << 16;
1127 for (count = 0; count <= 1000; count ++) {
1132 }
else if (count == 1000)
1136 *pdata = (status >> 8) & 0x000000ff;
1141 static int pdc20621_detect_dimm(
struct ata_host *host)
1161 static int pdc20621_prog_dimm0(
struct ata_host *host)
1168 static const struct {
1171 } pdc_i2c_read_data [] = {
1189 for (i = 0; i <
ARRAY_SIZE(pdc_i2c_read_data); i++)
1191 pdc_i2c_read_data[i].
reg,
1192 &spd0[pdc_i2c_read_data[i].ofs]);
1194 data |= (spd0[4] - 8) | ((spd0[21] != 0) << 3) | ((spd0[3]-11) << 4);
1195 data |= ((spd0[17] / 4) << 6) | ((spd0[5] / 2) << 7) |
1196 ((((spd0[27] + 9) / 10) - 1) << 8) ;
1197 data |= (((((spd0[29] > spd0[28])
1198 ? spd0[29] : spd0[28]) + 9) / 10) - 1) << 10;
1199 data |= ((spd0[30] - spd0[29] + 9) / 10 - 2) << 12;
1201 if (spd0[18] & 0x08)
1202 data |= ((0x03) << 14);
1203 else if (spd0[18] & 0x04)
1204 data |= ((0x02) << 14);
1205 else if (spd0[18] & 0x01)
1206 data |= ((0x01) << 14);
1215 bdimmsize = spd0[4] + (spd0[5] / 2) + spd0[3] + (spd0[17] / 2) + 3;
1216 size = (1 << bdimmsize) >> 20;
1217 data |= (((size / 16) - 1) << 16);
1226 static unsigned int pdc20621_prog_dimm_global(
struct ata_host *host)
1250 data |= (0x01 << 16);
1262 for (i = 1; i <= 10; i++) {
1264 if (!(data & (1<<19))) {
1274 static unsigned int pdc20621_dimm_init(
struct ata_host *host)
1278 u32 time_period = 0;
1293 VPRINTK(
"Time Period Register (0x40): 0x%x\n", time_period);
1308 VPRINTK(
"Time Counter Register (0x44): 0x%x\n", tcount);
1315 ticks = (time_period - tcount);
1316 VPRINTK(
"Num counters 0x%x (%d)\n", ticks, ticks);
1318 clock = (ticks / 300000);
1319 VPRINTK(
"10 * Internal clk = 0x%x (%d)\n", clock, clock);
1321 clock = (clock * 33);
1322 VPRINTK(
"10 * Internal clk * 33 = 0x%x (%d)\n", clock, clock);
1325 fparam = (1400000 /
clock) - 2;
1326 VPRINTK(
"PLL F Param: 0x%x (%d)\n", fparam, fparam);
1329 pci_status = (0x8a001824 | (fparam << 16));
1334 VPRINTK(
"pci_status: 0x%x\n", pci_status);
1342 if (!(speed = pdc20621_detect_dimm(host))) {
1346 VPRINTK(
"Local DIMM Speed = %d\n", speed);
1349 size = pdc20621_prog_dimm0(host);
1350 VPRINTK(
"Local DIMM Size = %dMB\n", size);
1353 if (pdc20621_prog_dimm_global(host)) {
1354 printk(
KERN_ERR "Programming DIMM Module Global Control Register Fail\n");
1358 #ifdef ATA_VERBOSE_DEBUG
1360 u8 test_parttern1[40] =
1361 {0x55,0xAA,
'P',
'r',
'o',
'm',
'i',
's',
'e',
' ',
1362 'N',
'o',
't',
' ',
'Y',
'e',
't',
' ',
1363 'D',
'e',
'f',
'i',
'n',
'e',
'd',
' ',
1365 '9',
'8',
'0',
'3',
'1',
'6',
'1',
'2',0,0};
1366 u8 test_parttern2[40] = {0};
1368 pdc20621_put_to_dimm(host, test_parttern2, 0x10040, 40);
1369 pdc20621_put_to_dimm(host, test_parttern2, 0x40, 40);
1371 pdc20621_put_to_dimm(host, test_parttern1, 0x10040, 40);
1372 pdc20621_get_from_dimm(host, test_parttern2, 0x40, 40);
1374 test_parttern2[1], &(test_parttern2[2]));
1375 pdc20621_get_from_dimm(host, test_parttern2, 0x10040,
1378 test_parttern2[1], &(test_parttern2[2]));
1380 pdc20621_put_to_dimm(host, test_parttern1, 0x40, 40);
1381 pdc20621_get_from_dimm(host, test_parttern2, 0x40, 40);
1383 test_parttern2[1], &(test_parttern2[2]));
1393 VPRINTK(
"Start ECC initialization\n");
1395 length = size * 1024 * 1024;
1397 while (addr < length) {
1398 pdc20621_put_to_dimm(host, buf, addr,
1403 VPRINTK(
"Finish ECC initialization\n");
1409 static void pdc_20621_init(
struct ata_host *host)
1440 static int pdc_sata_init_one(
struct pci_dev *pdev,
1454 if (!host || !hpriv)
1472 for (i = 0; i < 4; i++) {
1475 unsigned int offset = 0x200 + i * 0x80;
1477 pdc_sata_setup_port(&ap->ioaddr, base + offset);
1492 if (pdc20621_dimm_init(host))
1494 pdc_20621_init(host);