21 #ifdef CONFIG_SATA_DWC_DEBUG
25 #ifdef CONFIG_SATA_DWC_VDEBUG
30 #include <linux/kernel.h>
31 #include <linux/module.h>
33 #include <linux/device.h>
37 #include <linux/slab.h>
46 #define DRV_NAME "sata-dwc"
47 #define DRV_VERSION "1.3"
50 #define DMA_NUM_CHANS 1
51 #define DMA_NUM_CHAN_REGS 8
54 #define AHB_DMA_BRST_DFLT 64
125 SATA_DWC_DMAC_LLI_NUM),
138 #define DMA_CTL_BLK_TS(size) ((size) & 0x000000FFF)
139 #define DMA_CHANNEL(ch) (0x00000001 << (ch))
141 #define DMA_ENABLE_CHAN(ch) ((0x00000001 << (ch)) | \
142 ((0x000000001 << (ch)) << 8))
144 #define DMA_DISABLE_CHAN(ch) (0x00000000 | ((0x000000001 << (ch)) << 8))
146 #define DMA_CTL_TTFC(type) (((type) & 0x7) << 20)
147 #define DMA_CTL_SMS(num) (((num) & 0x3) << 25)
148 #define DMA_CTL_DMS(num) (((num) & 0x3) << 23)
150 #define DMA_CTL_SRC_MSIZE(size) (((size) & 0x7) << 14)
152 #define DMA_CTL_DST_MSIZE(size) (((size) & 0x7) << 11)
154 #define DMA_CTL_SRC_TRWID(size) (((size) & 0x7) << 4)
156 #define DMA_CTL_DST_TRWID(size) (((size) & 0x7) << 1)
159 #define DMA_CFG_HW_HS_DEST(int_num) (((int_num) & 0xF) << 11)
160 #define DMA_CFG_HW_HS_SRC(int_num) (((int_num) & 0xF) << 7)
161 #define DMA_CFG_HW_CH_PRIOR(int_num) (((int_num) & 0xF) << 5)
162 #define DMA_LLP_LMS(addr, master) (((addr) & 0xfffffffc) | (master))
262 #define SATA_DWC_SCR0_SPD_GET(v) (((v) >> 4) & 0x0000000F)
263 #define SATA_DWC_DMACR_TX_CLEAR(v) (((v) & ~SATA_DWC_DMACR_TXCHEN) |\
264 SATA_DWC_DMACR_TMOD_TXCHEN)
265 #define SATA_DWC_DMACR_RX_CLEAR(v) (((v) & ~SATA_DWC_DMACR_RXCHEN) |\
266 SATA_DWC_DMACR_TMOD_TXCHEN)
267 #define SATA_DWC_DBTSR_MWR(size) (((size)/4) & SATA_DWC_TXFIFO_DEPTH)
268 #define SATA_DWC_DBTSR_MRD(size) ((((size)/4) & SATA_DWC_RXFIFO_DEPTH)\
272 struct ata_probe_ent *
pe;
279 #define SATA_DWC_QCMD_MAX 32
293 #define HSDEV_FROM_HOST(host) ((struct sata_dwc_device *)\
294 (host)->private_data)
295 #define HSDEV_FROM_AP(ap) ((struct sata_dwc_device *)\
296 (ap)->host->private_data)
297 #define HSDEVP_FROM_AP(ap) ((struct sata_dwc_device_port *)\
299 #define HSDEV_FROM_QC(qc) ((struct sata_dwc_device *)\
300 (qc)->ap->host->private_data)
301 #define HSDEV_FROM_HSDEVP(p) ((struct sata_dwc_device *)\
331 static void sata_dwc_dma_xfer_complete(
struct ata_port *ap,
u32 check_status);
332 static void sata_dwc_port_stop(
struct ata_port *ap);
339 static void dma_dwc_xfer_start(
int dma_ch);
341 static const char *get_prot_descript(
u8 protocol)
345 return "ATA no data";
353 return "ATAPI no data";
363 static const char *get_dma_dir_descript(
int dma_dir)
367 return "bidirectional";
371 return "from device";
380 "0x%lx device: %x\n", tf->
command,
386 "hob_lbal: 0x%x hob_lbam: 0x%x hob_lbah: 0x%x\n",
397 static int get_burst_length_encode(
int datalength)
399 int items = datalength >> 2;
419 static void clear_chan_interrupts(
int c)
457 static irqreturn_t dma_dwc_interrupt(
int irq,
void *hsdev_instance)
460 u32 tfr_reg, err_reg;
468 unsigned int port = 0;
473 tag = ap->link.active_tag;
480 dev_dbg(ap->dev,
"eot=0x%08x err=0x%08x pending=%d active port=%d\n",
481 tfr_reg, err_reg, hsdevp->dma_pending[
tag],
port);
493 sata_dwc_clear_dmacr(hsdevp,
tag);
495 if (hsdevp->dma_pending[
tag] ==
497 dev_err(ap->dev,
"DMA not pending eot=0x%08x "
498 "err=0x%08x tag=0x%02x pending=%d\n",
499 tfr_reg, err_reg,
tag,
500 hsdevp->dma_pending[
tag]);
503 if ((
host_pvt.dma_interrupt_count % 2) == 0)
504 sata_dwc_dma_xfer_complete(ap, 1);
515 dev_err(ap->dev,
"error interrupt err_reg=0x%08x\n",
524 spin_unlock_irqrestore(&host->
lock, flags);
534 static int dma_request_interrupts(
struct sata_dwc_device *hsdev,
int irq)
549 retval =
request_irq(irq, dma_dwc_interrupt, 0,
"SATA DMA", hsdev);
572 void __iomem *dmadr_addr,
int dir)
578 int sms_val, dms_val;
583 " dmadr=0x%08x\n", __func__, sg, num_elems, lli, (
u32)dma_lli,
596 "=%d\n", __func__, i, addr, sg_len);
599 if (idx >= SATA_DWC_DMAC_LLI_NUM) {
608 offset = addr & 0xffff;
609 if ((offset + sg_len) > 0x10000)
610 len = 0x10000 - offset;
619 if (fis_len + len > 8192) {
621 "%d(0x%x) len=%d(0x%x)\n", fis_len,
623 len = 8192 - fis_len;
670 "0x%08x val: 0x%08x\n", __func__,
680 next_llp = (dma_lli + ((idx + 1) *
sizeof(
struct \
700 lli[idx-1].
llp = 0x00000000;
717 static void dma_dwc_xfer_start(
int dma_ch)
725 static int dma_dwc_xfer_setup(
struct scatterlist *sg,
int num_elems,
740 num_lli = map_sg_to_lli(sg, num_elems, lli, dma_lli, addr, dir);
743 " 0x%0xlx addr: %p lli count: %d\n", __func__, sg, num_elems,
744 lli, (
u32)dma_lli, addr, num_lli);
746 clear_chan_interrupts(dma_ch);
795 err = dma_request_interrupts(hsdev, irq);
798 " %d\n", __func__, err);
805 dev_notice(
host_pvt.dwc_dev,
"DMA initialized\n");
819 dev_err(link->
ap->dev,
"%s: Incorrect SCR offset 0x%02x\n",
824 *val =
in_le32((
void *)link->
ap->ioaddr.scr_addr + (scr * 4));
825 dev_dbg(link->
ap->dev,
"%s: id=%d reg=%d val=val=0x%08x\n",
826 __func__, link->
ap->print_id, scr, *val);
831 static int sata_dwc_scr_write(
struct ata_link *link,
unsigned int scr,
u32 val)
833 dev_dbg(link->
ap->dev,
"%s: id=%d reg=%d val=val=0x%08x\n",
834 __func__, link->
ap->print_id, scr, val);
836 dev_err(link->
ap->dev,
"%s: Incorrect SCR offset 0x%02x\n",
840 out_le32((
void *)link->
ap->ioaddr.scr_addr + (scr * 4), val);
845 static u32 core_scr_read(
unsigned int scr)
851 static void core_scr_write(
unsigned int scr,
u32 val)
857 static void clear_serror(
void)
873 return 0x00000001 << (tag & 0x1f);
877 static void sata_dwc_error_intr(
struct ata_port *ap,
891 status = ap->
ops->sff_check_status(ap);
893 err_reg =
in_le32(&(
host_pvt.sata_dma_regs->interrupt_status.error.\
low));
894 tag = ap->
link.active_tag;
896 dev_err(ap->
dev,
"%s SCR_ERROR=0x%08x intpr=0x%08x status=0x%08x "
897 "dma_intp=%d pending=%d issued=%d dma_err_status=0x%08x\n",
898 __func__, serror, intpr, status,
host_pvt.dma_interrupt_count,
914 qc = ata_qc_from_tag(ap, tag);
930 static irqreturn_t sata_dwc_isr(
int irq,
void *dev_instance)
938 int handled, num_processed,
port = 0;
939 uint intpr, sactive, sactive2, tag_mask;
941 host_pvt.sata_dwc_sactive_issued = 0;
951 dev_dbg(ap->
dev,
"%s intpr=0x%08x active_tag=%d\n", __func__, intpr,
952 ap->
link.active_tag);
956 sata_dwc_error_intr(ap, hsdev, intpr);
963 clear_interrupt_bit(hsdev, SATA_DWC_INTPR_NEWFP);
966 dev_dbg(ap->
dev,
"%s: NEWFP tag=%d\n", __func__, tag);
968 dev_warn(ap->
dev,
"CMD tag=%d not pending?\n", tag);
970 host_pvt.sata_dwc_sactive_issued |= qcmd_tag_to_mask(tag);
972 qc = ata_qc_from_tag(ap, tag);
978 qc->
ap->link.active_tag =
tag;
979 sata_dwc_bmdma_start_by_tag(qc, tag);
985 tag_mask = (
host_pvt.sata_dwc_sactive_issued | sactive) ^ sactive;
988 if (
host_pvt.sata_dwc_sactive_issued == 0 && tag_mask == 0) {
992 tag = ap->
link.active_tag;
993 qc = ata_qc_from_tag(ap, tag);
997 dev_err(ap->
dev,
"%s interrupt with no active qc "
998 "qc=%p\n", __func__, qc);
999 ap->
ops->sff_check_status(ap);
1003 status = ap->
ops->sff_check_status(ap);
1005 qc->
ap->link.active_tag =
tag;
1009 dev_dbg(ap->
dev,
"interrupt ATA_ERR (0x%x)\n", status);
1010 sata_dwc_qc_complete(ap, qc, 1);
1015 dev_dbg(ap->
dev,
"%s non-NCQ cmd interrupt, protocol: %s\n",
1016 __func__, get_prot_descript(qc->
tf.protocol));
1018 if (ata_is_dma(qc->
tf.protocol)) {
1029 "intpr=0x%08x status=0x%08x pending"
1030 "=%d\n", __func__, intpr, status,
1034 if ((
host_pvt.dma_interrupt_count % 2) == 0)
1035 sata_dwc_dma_xfer_complete(ap, 1);
1036 }
else if (ata_is_pio(qc->
tf.protocol)) {
1041 if (
unlikely(sata_dwc_qc_complete(ap, qc, 1)))
1058 tag_mask = (
host_pvt.sata_dwc_sactive_issued | sactive) ^ sactive;
1060 if (sactive != 0 || (
host_pvt.sata_dwc_sactive_issued) > 1 || \
1062 dev_dbg(ap->
dev,
"%s NCQ:sactive=0x%08x sactive_issued=0x%08x"
1063 "tag_mask=0x%08x\n", __func__, sactive,
1064 host_pvt.sata_dwc_sactive_issued, tag_mask);
1067 if ((tag_mask | (
host_pvt.sata_dwc_sactive_issued)) != \
1068 (
host_pvt.sata_dwc_sactive_issued)) {
1070 "(host_pvt.sata_dwc_sactive_issued)=0x%08x tag_mask"
1071 "=0x%08x\n", sactive,
host_pvt.sata_dwc_sactive_issued,
1076 status = ap->
ops->sff_check_status(ap);
1077 dev_dbg(ap->
dev,
"%s ATA status register=0x%x\n", __func__, status);
1083 while (!(tag_mask & 0x00000001)) {
1088 tag_mask &= (~0x00000001);
1089 qc = ata_qc_from_tag(ap, tag);
1092 qc->
ap->link.active_tag =
tag;
1096 if (status & ATA_ERR) {
1097 dev_dbg(ap->
dev,
"%s ATA_ERR (0x%x)\n", __func__,
1099 sata_dwc_qc_complete(ap, qc, 1);
1105 dev_dbg(ap->
dev,
"%s NCQ command, protocol: %s\n", __func__,
1106 get_prot_descript(qc->
tf.protocol));
1107 if (ata_is_dma(qc->
tf.protocol)) {
1113 if ((
host_pvt.dma_interrupt_count % 2) == 0)
1114 sata_dwc_dma_xfer_complete(ap, 1);
1116 if (
unlikely(sata_dwc_qc_complete(ap, qc, 1)))
1122 ap->
stats.idle_irq++;
1123 dev_warn(ap->
dev,
"STILL BUSY IRQ ata%d: irq trap\n",
1135 if (sactive2 != sactive) {
1136 dev_dbg(ap->
dev,
"More completed - sactive=0x%x sactive2"
1137 "=0x%x\n", sactive, sactive2);
1142 spin_unlock_irqrestore(&host->
lock, flags);
1164 "TX DMA not pending tag=0x%02x pending=%d"
1165 " dmacr: 0x%08x\n", __func__, tag,
1173 static void sata_dwc_dma_xfer_complete(
struct ata_port *ap,
u32 check_status)
1180 tag = ap->
link.active_tag;
1181 qc = ata_qc_from_tag(ap, tag);
1189 dev_info(ap->
dev,
"%s tag=%u cmd=0x%02x dma dir=%s proto=%s "
1190 "dmacr=0x%08x\n", __func__, qc->
tag, qc->
tf.command,
1191 get_dma_dir_descript(qc->
dma_dir),
1192 get_prot_descript(qc->
tf.protocol),
1197 if (ata_is_dma(qc->
tf.protocol)) {
1199 dev_err(ap->
dev,
"%s DMA protocol RX and TX DMA not "
1200 "pending dmacr: 0x%08x\n", __func__,
1205 sata_dwc_qc_complete(ap, qc, check_status);
1208 sata_dwc_qc_complete(ap, qc, check_status);
1219 host_pvt.sata_dwc_sactive_queued = 0;
1220 dev_dbg(ap->
dev,
"%s checkstatus? %x\n", __func__, check_status);
1226 dev_dbg(ap->
dev,
"QC complete cmd=0x%02x status=0x%02x ata%u:"
1227 " protocol=%d\n", qc->
tf.command, status, ap->
print_id,
1231 mask = (~(qcmd_tag_to_mask(tag)));
1259 static void sata_dwc_setup_port(
struct ata_ioports *port,
unsigned long base)
1261 port->cmd_addr = (
void *)base + 0x00;
1262 port->data_addr = (
void *)base + 0x00;
1264 port->error_addr = (
void *)base + 0x04;
1265 port->feature_addr = (
void *)base + 0x04;
1267 port->nsect_addr = (
void *)base + 0x08;
1269 port->lbal_addr = (
void *)base + 0x0c;
1270 port->lbam_addr = (
void *)base + 0x10;
1271 port->lbah_addr = (
void *)base + 0x14;
1273 port->device_addr = (
void *)base + 0x18;
1274 port->command_addr = (
void *)base + 0x1c;
1275 port->status_addr = (
void *)base + 0x1c;
1277 port->altstatus_addr = (
void *)base + 0x20;
1278 port->ctl_addr = (
void *)base + 0x20;
1287 static int sata_dwc_port_start(
struct ata_port *ap)
1300 pdev = ap->
host->dev;
1302 dev_err(ap->
dev,
"%s: no ap->host->dev\n", __func__);
1308 hsdevp = kzalloc(
sizeof(*hsdevp),
GFP_KERNEL);
1310 dev_err(ap->
dev,
"%s: kmalloc failed for hsdevp\n", __func__);
1314 hsdevp->
hsdev = hsdev;
1320 ap->bmdma_prd_dma = 0;
1331 if (!hsdevp->
llit[i]) {
1332 dev_err(ap->
dev,
"%s: dma_alloc_coherent failed\n",
1340 dev_dbg(ap->
dev,
"%s: clearing TXCHEN, RXCHEN in DMAC\n",
1345 dev_dbg(ap->
dev,
"%s: setting burst size in DBTSR\n",
1365 static void sata_dwc_port_stop(
struct ata_port *ap)
1373 if (hsdevp && hsdev) {
1393 static void sata_dwc_exec_command_by_tag(
struct ata_port *ap,
1397 unsigned long flags;
1405 spin_unlock_irqrestore(&ap->
host->lock, flags);
1418 sata_dwc_exec_command_by_tag(qc->
ap, &qc->
tf, tag,
1426 if (ata_is_ncq(qc->
tf.protocol)) {
1427 dev_dbg(qc->
ap->dev,
"%s: ap->link.sactive=0x%08x tag=%d\n",
1428 __func__, qc->
ap->link.sactive, tag);
1432 sata_dwc_bmdma_setup_by_tag(qc, tag);
1452 dev_err(ap->
dev,
"%s: Command not pending cmd_issued=%d "
1453 "(tag=%d) DMA NOT started\n", __func__,
1458 dev_dbg(ap->
dev,
"%s qc=%p tag: %x cmd: 0x%02x dma_dir: %s "
1459 "start_dma? %x\n", __func__, qc, tag, qc->
tf.command,
1460 get_dma_dir_descript(qc->
dma_dir), start_dma);
1461 sata_dwc_tf_dump(&(qc->
tf));
1466 dev_err(ap->
dev,
"%s: ****** SError=0x%08x ******\n",
1478 dma_dwc_xfer_start(dma_chan);
1486 if (ata_is_ncq(qc->
tf.protocol)) {
1487 dev_dbg(qc->
ap->dev,
"%s: ap->link.sactive=0x%08x tag=%d\n",
1488 __func__, qc->
ap->link.sactive, tag);
1493 sata_dwc_bmdma_start_by_tag(qc, tag);
1510 dev_dbg(ap->
dev,
"%s: port=%d dma dir=%s n_elem=%d\n",
1514 dma_chan = dma_dwc_xfer_setup(sg, qc->
n_elem, hsdevp->
llit[tag],
1518 dev_err(ap->
dev,
"%s: dma_dwc_xfer_setup returns err %d\n",
1519 __func__, dma_chan);
1532 if (qc->
tag > 0 || ap->
link.sactive > 1)
1533 dev_info(ap->
dev,
"%s ap id=%d cmd(0x%02x)=%s qc tag=%d "
1534 "prot=%s ap active_tag=0x%08x ap sactive=0x%08x\n",
1537 qc->
tag, get_prot_descript(qc->
tf.protocol),
1538 ap->
link.active_tag, ap->
link.sactive);
1541 if (!ata_is_ncq(qc->
tf.protocol))
1543 sata_dwc_qc_prep_by_tag(qc, tag);
1545 if (ata_is_ncq(qc->
tf.protocol)) {
1547 sactive |= (0x00000001 <<
tag);
1550 dev_dbg(qc->
ap->dev,
"%s: tag=%d ap->link.sactive = 0x%08x "
1551 "sactive=0x%08x\n", __func__, tag, qc->
ap->link.sactive,
1554 ap->
ops->sff_tf_load(ap, &qc->
tf);
1555 sata_dwc_exec_command_by_tag(ap, &qc->
tf, qc->
tag,
1577 dev_info(qc->
ap->dev,
"%s: qc->tag=%d ap->active_tag=0x%08x\n",
1578 __func__, qc->
tag, qc->
ap->link.active_tag);
1584 static void sata_dwc_error_handler(
struct ata_port *ap)
1590 unsigned long deadline)
1597 sata_dwc_enable_interrupts(hsdev);
1629 .error_handler = sata_dwc_error_handler,
1632 .qc_prep = sata_dwc_qc_prep,
1633 .qc_issue = sata_dwc_qc_issue,
1635 .scr_read = sata_dwc_scr_read,
1636 .scr_write = sata_dwc_scr_write,
1638 .port_start = sata_dwc_port_start,
1639 .port_stop = sata_dwc_port_stop,
1641 .bmdma_setup = sata_dwc_bmdma_setup,
1642 .bmdma_start = sata_dwc_bmdma_start,
1650 .port_ops = &sata_dwc_ops,
1658 char *
ver = (
char *)&versionr;
1670 if (hsdev ==
NULL) {
1671 dev_err(&ofdev->
dev,
"kmalloc failed for hsdev\n");
1676 if (of_property_read_u32(np,
"dma-channel", &dma_chan)) {
1677 dev_warn(&ofdev->
dev,
"no dma-channel property set."
1678 " Use channel 0\n");
1686 dev_err(&ofdev->
dev,
"ioremap failed for SATA register"
1692 dev_dbg(&ofdev->
dev,
"ioremap done for SATA register address\n");
1700 dev_err(&ofdev->
dev,
"ata_host_alloc_pinfo failed\n");
1708 host->
ports[0]->ioaddr.cmd_addr = base;
1711 sata_dwc_setup_port(&host->
ports[0]->ioaddr, (
unsigned long)base);
1716 dev_notice(&ofdev->
dev,
"id %d, controller version %c.%c%c\n",
1717 idr, ver[0], ver[1], ver[2]);
1730 dev_err(&ofdev->
dev,
"ioremap failed for AHBDMA register"
1740 dma_dwc_init(hsdev, irq);
1743 sata_dwc_enable_interrupts(hsdev);
1761 dev_err(&ofdev->
dev,
"failed to activate host");
1768 dma_dwc_exit(hsdev);
1788 dma_dwc_exit(hsdev);
1798 { .compatible =
"amcc,sata-460ex", },
1807 .of_match_table = sata_dwc_match,
1809 .probe = sata_dwc_probe,
1810 .remove = sata_dwc_remove,