0001
0002
0003
0004
0005
0006
0007
0008
0009
0010 #include <linux/kernel.h>
0011 #include <linux/module.h>
0012 #include <linux/ata.h>
0013 #include <linux/libata.h>
0014 #include <linux/of_device.h>
0015 #include <linux/platform_device.h>
0016 #include <linux/pm_runtime.h>
0017 #include <linux/err.h>
0018
0019 #define DRV_NAME "sata_rcar"
0020
0021
0022 #define ATAPI_CONTROL1_REG 0x180
0023 #define ATAPI_STATUS_REG 0x184
0024 #define ATAPI_INT_ENABLE_REG 0x188
0025 #define ATAPI_DTB_ADR_REG 0x198
0026 #define ATAPI_DMA_START_ADR_REG 0x19C
0027 #define ATAPI_DMA_TRANS_CNT_REG 0x1A0
0028 #define ATAPI_CONTROL2_REG 0x1A4
0029 #define ATAPI_SIG_ST_REG 0x1B0
0030 #define ATAPI_BYTE_SWAP_REG 0x1BC
0031
0032
0033 #define ATAPI_CONTROL1_ISM BIT(16)
0034 #define ATAPI_CONTROL1_DTA32M BIT(11)
0035 #define ATAPI_CONTROL1_RESET BIT(7)
0036 #define ATAPI_CONTROL1_DESE BIT(3)
0037 #define ATAPI_CONTROL1_RW BIT(2)
0038 #define ATAPI_CONTROL1_STOP BIT(1)
0039 #define ATAPI_CONTROL1_START BIT(0)
0040
0041
0042 #define ATAPI_STATUS_SATAINT BIT(11)
0043 #define ATAPI_STATUS_DNEND BIT(6)
0044 #define ATAPI_STATUS_DEVTRM BIT(5)
0045 #define ATAPI_STATUS_DEVINT BIT(4)
0046 #define ATAPI_STATUS_ERR BIT(2)
0047 #define ATAPI_STATUS_NEND BIT(1)
0048 #define ATAPI_STATUS_ACT BIT(0)
0049
0050
0051 #define ATAPI_INT_ENABLE_SATAINT BIT(11)
0052 #define ATAPI_INT_ENABLE_DNEND BIT(6)
0053 #define ATAPI_INT_ENABLE_DEVTRM BIT(5)
0054 #define ATAPI_INT_ENABLE_DEVINT BIT(4)
0055 #define ATAPI_INT_ENABLE_ERR BIT(2)
0056 #define ATAPI_INT_ENABLE_NEND BIT(1)
0057 #define ATAPI_INT_ENABLE_ACT BIT(0)
0058
0059
0060 #define SATAPHYADDR_REG 0x200
0061 #define SATAPHYWDATA_REG 0x204
0062 #define SATAPHYACCEN_REG 0x208
0063 #define SATAPHYRESET_REG 0x20C
0064 #define SATAPHYRDATA_REG 0x210
0065 #define SATAPHYACK_REG 0x214
0066
0067
0068 #define SATAPHYADDR_PHYRATEMODE BIT(10)
0069 #define SATAPHYADDR_PHYCMD_READ BIT(9)
0070 #define SATAPHYADDR_PHYCMD_WRITE BIT(8)
0071
0072
0073 #define SATAPHYACCEN_PHYLANE BIT(0)
0074
0075
0076 #define SATAPHYRESET_PHYRST BIT(1)
0077 #define SATAPHYRESET_PHYSRES BIT(0)
0078
0079
0080 #define SATAPHYACK_PHYACK BIT(0)
0081
0082
0083 #define BISTCONF_REG 0x102C
0084 #define SDATA_REG 0x1100
0085 #define SSDEVCON_REG 0x1204
0086
0087 #define SCRSSTS_REG 0x1400
0088 #define SCRSERR_REG 0x1404
0089 #define SCRSCON_REG 0x1408
0090 #define SCRSACT_REG 0x140C
0091
0092 #define SATAINTSTAT_REG 0x1508
0093 #define SATAINTMASK_REG 0x150C
0094
0095
0096 #define SATAINTSTAT_SERR BIT(3)
0097 #define SATAINTSTAT_ATA BIT(0)
0098
0099
0100 #define SATAINTMASK_SERRMSK BIT(3)
0101 #define SATAINTMASK_ERRMSK BIT(2)
0102 #define SATAINTMASK_ERRCRTMSK BIT(1)
0103 #define SATAINTMASK_ATAMSK BIT(0)
0104 #define SATAINTMASK_ALL_GEN1 0x7ff
0105 #define SATAINTMASK_ALL_GEN2 0xfff
0106
0107 #define SATA_RCAR_INT_MASK (SATAINTMASK_SERRMSK | \
0108 SATAINTMASK_ATAMSK)
0109
0110
0111 #define SATAPCTLR1_REG 0x43
0112 #define SATAPCTLR2_REG 0x52
0113 #define SATAPCTLR3_REG 0x5A
0114 #define SATAPCTLR4_REG 0x60
0115
0116
0117 #define SATA_RCAR_DTEND BIT(0)
0118
0119 #define SATA_RCAR_DMA_BOUNDARY 0x1FFFFFFFUL
0120
0121
0122 #define RCAR_GEN2_PHY_CTL1_REG 0x1704
0123 #define RCAR_GEN2_PHY_CTL1 0x34180002
0124 #define RCAR_GEN2_PHY_CTL1_SS 0xC180
0125
0126 #define RCAR_GEN2_PHY_CTL2_REG 0x170C
0127 #define RCAR_GEN2_PHY_CTL2 0x00002303
0128
0129 #define RCAR_GEN2_PHY_CTL3_REG 0x171C
0130 #define RCAR_GEN2_PHY_CTL3 0x000B0194
0131
0132 #define RCAR_GEN2_PHY_CTL4_REG 0x1724
0133 #define RCAR_GEN2_PHY_CTL4 0x00030994
0134
0135 #define RCAR_GEN2_PHY_CTL5_REG 0x1740
0136 #define RCAR_GEN2_PHY_CTL5 0x03004001
0137 #define RCAR_GEN2_PHY_CTL5_DC BIT(1)
0138 #define RCAR_GEN2_PHY_CTL5_TR BIT(2)
0139
0140 enum sata_rcar_type {
0141 RCAR_GEN1_SATA,
0142 RCAR_GEN2_SATA,
0143 RCAR_GEN3_SATA,
0144 RCAR_R8A7790_ES1_SATA,
0145 };
0146
0147 struct sata_rcar_priv {
0148 void __iomem *base;
0149 u32 sataint_mask;
0150 enum sata_rcar_type type;
0151 };
0152
0153 static void sata_rcar_gen1_phy_preinit(struct sata_rcar_priv *priv)
0154 {
0155 void __iomem *base = priv->base;
0156
0157
0158 iowrite32(0, base + SATAPHYADDR_REG);
0159
0160 iowrite32(SATAPHYRESET_PHYRST, base + SATAPHYRESET_REG);
0161 udelay(10);
0162
0163 iowrite32(0, base + SATAPHYRESET_REG);
0164 }
0165
0166 static void sata_rcar_gen1_phy_write(struct sata_rcar_priv *priv, u16 reg,
0167 u32 val, int group)
0168 {
0169 void __iomem *base = priv->base;
0170 int timeout;
0171
0172
0173 iowrite32(0, base + SATAPHYRESET_REG);
0174
0175 iowrite32(SATAPHYACCEN_PHYLANE, base + SATAPHYACCEN_REG);
0176
0177 iowrite32(val, base + SATAPHYWDATA_REG);
0178
0179 if (group)
0180 reg |= SATAPHYADDR_PHYRATEMODE;
0181
0182 iowrite32(SATAPHYADDR_PHYCMD_WRITE | reg, base + SATAPHYADDR_REG);
0183
0184 for (timeout = 0; timeout < 100; timeout++) {
0185 val = ioread32(base + SATAPHYACK_REG);
0186 if (val & SATAPHYACK_PHYACK)
0187 break;
0188 }
0189 if (timeout >= 100)
0190 pr_err("%s timeout\n", __func__);
0191
0192 iowrite32(0, base + SATAPHYADDR_REG);
0193 }
0194
0195 static void sata_rcar_gen1_phy_init(struct sata_rcar_priv *priv)
0196 {
0197 sata_rcar_gen1_phy_preinit(priv);
0198 sata_rcar_gen1_phy_write(priv, SATAPCTLR1_REG, 0x00200188, 0);
0199 sata_rcar_gen1_phy_write(priv, SATAPCTLR1_REG, 0x00200188, 1);
0200 sata_rcar_gen1_phy_write(priv, SATAPCTLR3_REG, 0x0000A061, 0);
0201 sata_rcar_gen1_phy_write(priv, SATAPCTLR2_REG, 0x20000000, 0);
0202 sata_rcar_gen1_phy_write(priv, SATAPCTLR2_REG, 0x20000000, 1);
0203 sata_rcar_gen1_phy_write(priv, SATAPCTLR4_REG, 0x28E80000, 0);
0204 }
0205
0206 static void sata_rcar_gen2_phy_init(struct sata_rcar_priv *priv)
0207 {
0208 void __iomem *base = priv->base;
0209
0210 iowrite32(RCAR_GEN2_PHY_CTL1, base + RCAR_GEN2_PHY_CTL1_REG);
0211 iowrite32(RCAR_GEN2_PHY_CTL2, base + RCAR_GEN2_PHY_CTL2_REG);
0212 iowrite32(RCAR_GEN2_PHY_CTL3, base + RCAR_GEN2_PHY_CTL3_REG);
0213 iowrite32(RCAR_GEN2_PHY_CTL4, base + RCAR_GEN2_PHY_CTL4_REG);
0214 iowrite32(RCAR_GEN2_PHY_CTL5 | RCAR_GEN2_PHY_CTL5_DC |
0215 RCAR_GEN2_PHY_CTL5_TR, base + RCAR_GEN2_PHY_CTL5_REG);
0216 }
0217
0218 static void sata_rcar_freeze(struct ata_port *ap)
0219 {
0220 struct sata_rcar_priv *priv = ap->host->private_data;
0221
0222
0223 iowrite32(priv->sataint_mask, priv->base + SATAINTMASK_REG);
0224
0225 ata_sff_freeze(ap);
0226 }
0227
0228 static void sata_rcar_thaw(struct ata_port *ap)
0229 {
0230 struct sata_rcar_priv *priv = ap->host->private_data;
0231 void __iomem *base = priv->base;
0232
0233
0234 iowrite32(~(u32)SATA_RCAR_INT_MASK, base + SATAINTSTAT_REG);
0235
0236 ata_sff_thaw(ap);
0237
0238
0239 iowrite32(priv->sataint_mask & ~SATA_RCAR_INT_MASK, base + SATAINTMASK_REG);
0240 }
0241
0242 static void sata_rcar_ioread16_rep(void __iomem *reg, void *buffer, int count)
0243 {
0244 u16 *ptr = buffer;
0245
0246 while (count--) {
0247 u16 data = ioread32(reg);
0248
0249 *ptr++ = data;
0250 }
0251 }
0252
0253 static void sata_rcar_iowrite16_rep(void __iomem *reg, void *buffer, int count)
0254 {
0255 const u16 *ptr = buffer;
0256
0257 while (count--)
0258 iowrite32(*ptr++, reg);
0259 }
0260
0261 static u8 sata_rcar_check_status(struct ata_port *ap)
0262 {
0263 return ioread32(ap->ioaddr.status_addr);
0264 }
0265
0266 static u8 sata_rcar_check_altstatus(struct ata_port *ap)
0267 {
0268 return ioread32(ap->ioaddr.altstatus_addr);
0269 }
0270
0271 static void sata_rcar_set_devctl(struct ata_port *ap, u8 ctl)
0272 {
0273 iowrite32(ctl, ap->ioaddr.ctl_addr);
0274 }
0275
0276 static void sata_rcar_dev_select(struct ata_port *ap, unsigned int device)
0277 {
0278 iowrite32(ATA_DEVICE_OBS, ap->ioaddr.device_addr);
0279 ata_sff_pause(ap);
0280 }
0281
0282 static bool sata_rcar_ata_devchk(struct ata_port *ap, unsigned int device)
0283 {
0284 struct ata_ioports *ioaddr = &ap->ioaddr;
0285 u8 nsect, lbal;
0286
0287 sata_rcar_dev_select(ap, device);
0288
0289 iowrite32(0x55, ioaddr->nsect_addr);
0290 iowrite32(0xaa, ioaddr->lbal_addr);
0291
0292 iowrite32(0xaa, ioaddr->nsect_addr);
0293 iowrite32(0x55, ioaddr->lbal_addr);
0294
0295 iowrite32(0x55, ioaddr->nsect_addr);
0296 iowrite32(0xaa, ioaddr->lbal_addr);
0297
0298 nsect = ioread32(ioaddr->nsect_addr);
0299 lbal = ioread32(ioaddr->lbal_addr);
0300
0301 if (nsect == 0x55 && lbal == 0xaa)
0302 return true;
0303
0304 return false;
0305 }
0306
0307 static int sata_rcar_wait_after_reset(struct ata_link *link,
0308 unsigned long deadline)
0309 {
0310 struct ata_port *ap = link->ap;
0311
0312 ata_msleep(ap, ATA_WAIT_AFTER_RESET);
0313
0314 return ata_sff_wait_ready(link, deadline);
0315 }
0316
0317 static int sata_rcar_bus_softreset(struct ata_port *ap, unsigned long deadline)
0318 {
0319 struct ata_ioports *ioaddr = &ap->ioaddr;
0320
0321
0322 iowrite32(ap->ctl, ioaddr->ctl_addr);
0323 udelay(20);
0324 iowrite32(ap->ctl | ATA_SRST, ioaddr->ctl_addr);
0325 udelay(20);
0326 iowrite32(ap->ctl, ioaddr->ctl_addr);
0327 ap->last_ctl = ap->ctl;
0328
0329
0330 return sata_rcar_wait_after_reset(&ap->link, deadline);
0331 }
0332
0333 static int sata_rcar_softreset(struct ata_link *link, unsigned int *classes,
0334 unsigned long deadline)
0335 {
0336 struct ata_port *ap = link->ap;
0337 unsigned int devmask = 0;
0338 int rc;
0339 u8 err;
0340
0341
0342 if (sata_rcar_ata_devchk(ap, 0))
0343 devmask |= 1 << 0;
0344
0345
0346 rc = sata_rcar_bus_softreset(ap, deadline);
0347
0348 if (rc && (rc != -ENODEV || sata_scr_valid(link))) {
0349 ata_link_err(link, "SRST failed (errno=%d)\n", rc);
0350 return rc;
0351 }
0352
0353
0354 classes[0] = ata_sff_dev_classify(&link->device[0], devmask, &err);
0355
0356 return 0;
0357 }
0358
0359 static void sata_rcar_tf_load(struct ata_port *ap,
0360 const struct ata_taskfile *tf)
0361 {
0362 struct ata_ioports *ioaddr = &ap->ioaddr;
0363 unsigned int is_addr = tf->flags & ATA_TFLAG_ISADDR;
0364
0365 if (tf->ctl != ap->last_ctl) {
0366 iowrite32(tf->ctl, ioaddr->ctl_addr);
0367 ap->last_ctl = tf->ctl;
0368 ata_wait_idle(ap);
0369 }
0370
0371 if (is_addr && (tf->flags & ATA_TFLAG_LBA48)) {
0372 iowrite32(tf->hob_feature, ioaddr->feature_addr);
0373 iowrite32(tf->hob_nsect, ioaddr->nsect_addr);
0374 iowrite32(tf->hob_lbal, ioaddr->lbal_addr);
0375 iowrite32(tf->hob_lbam, ioaddr->lbam_addr);
0376 iowrite32(tf->hob_lbah, ioaddr->lbah_addr);
0377 }
0378
0379 if (is_addr) {
0380 iowrite32(tf->feature, ioaddr->feature_addr);
0381 iowrite32(tf->nsect, ioaddr->nsect_addr);
0382 iowrite32(tf->lbal, ioaddr->lbal_addr);
0383 iowrite32(tf->lbam, ioaddr->lbam_addr);
0384 iowrite32(tf->lbah, ioaddr->lbah_addr);
0385 }
0386
0387 if (tf->flags & ATA_TFLAG_DEVICE)
0388 iowrite32(tf->device, ioaddr->device_addr);
0389
0390 ata_wait_idle(ap);
0391 }
0392
0393 static void sata_rcar_tf_read(struct ata_port *ap, struct ata_taskfile *tf)
0394 {
0395 struct ata_ioports *ioaddr = &ap->ioaddr;
0396
0397 tf->status = sata_rcar_check_status(ap);
0398 tf->error = ioread32(ioaddr->error_addr);
0399 tf->nsect = ioread32(ioaddr->nsect_addr);
0400 tf->lbal = ioread32(ioaddr->lbal_addr);
0401 tf->lbam = ioread32(ioaddr->lbam_addr);
0402 tf->lbah = ioread32(ioaddr->lbah_addr);
0403 tf->device = ioread32(ioaddr->device_addr);
0404
0405 if (tf->flags & ATA_TFLAG_LBA48) {
0406 iowrite32(tf->ctl | ATA_HOB, ioaddr->ctl_addr);
0407 tf->hob_feature = ioread32(ioaddr->error_addr);
0408 tf->hob_nsect = ioread32(ioaddr->nsect_addr);
0409 tf->hob_lbal = ioread32(ioaddr->lbal_addr);
0410 tf->hob_lbam = ioread32(ioaddr->lbam_addr);
0411 tf->hob_lbah = ioread32(ioaddr->lbah_addr);
0412 iowrite32(tf->ctl, ioaddr->ctl_addr);
0413 ap->last_ctl = tf->ctl;
0414 }
0415 }
0416
0417 static void sata_rcar_exec_command(struct ata_port *ap,
0418 const struct ata_taskfile *tf)
0419 {
0420 iowrite32(tf->command, ap->ioaddr.command_addr);
0421 ata_sff_pause(ap);
0422 }
0423
0424 static unsigned int sata_rcar_data_xfer(struct ata_queued_cmd *qc,
0425 unsigned char *buf,
0426 unsigned int buflen, int rw)
0427 {
0428 struct ata_port *ap = qc->dev->link->ap;
0429 void __iomem *data_addr = ap->ioaddr.data_addr;
0430 unsigned int words = buflen >> 1;
0431
0432
0433 if (rw == READ)
0434 sata_rcar_ioread16_rep(data_addr, buf, words);
0435 else
0436 sata_rcar_iowrite16_rep(data_addr, buf, words);
0437
0438
0439 if (unlikely(buflen & 0x01)) {
0440 unsigned char pad[2] = { };
0441
0442
0443 buf += buflen - 1;
0444
0445
0446
0447
0448
0449 if (rw == READ) {
0450 sata_rcar_ioread16_rep(data_addr, pad, 1);
0451 *buf = pad[0];
0452 } else {
0453 pad[0] = *buf;
0454 sata_rcar_iowrite16_rep(data_addr, pad, 1);
0455 }
0456 words++;
0457 }
0458
0459 return words << 1;
0460 }
0461
0462 static void sata_rcar_drain_fifo(struct ata_queued_cmd *qc)
0463 {
0464 int count;
0465 struct ata_port *ap;
0466
0467
0468 if (qc == NULL || qc->dma_dir == DMA_TO_DEVICE)
0469 return;
0470
0471 ap = qc->ap;
0472
0473 for (count = 0; (ap->ops->sff_check_status(ap) & ATA_DRQ) &&
0474 count < 65536; count += 2)
0475 ioread32(ap->ioaddr.data_addr);
0476
0477 if (count)
0478 ata_port_dbg(ap, "drained %d bytes to clear DRQ\n", count);
0479 }
0480
0481 static int sata_rcar_scr_read(struct ata_link *link, unsigned int sc_reg,
0482 u32 *val)
0483 {
0484 if (sc_reg > SCR_ACTIVE)
0485 return -EINVAL;
0486
0487 *val = ioread32(link->ap->ioaddr.scr_addr + (sc_reg << 2));
0488 return 0;
0489 }
0490
0491 static int sata_rcar_scr_write(struct ata_link *link, unsigned int sc_reg,
0492 u32 val)
0493 {
0494 if (sc_reg > SCR_ACTIVE)
0495 return -EINVAL;
0496
0497 iowrite32(val, link->ap->ioaddr.scr_addr + (sc_reg << 2));
0498 return 0;
0499 }
0500
0501 static void sata_rcar_bmdma_fill_sg(struct ata_queued_cmd *qc)
0502 {
0503 struct ata_port *ap = qc->ap;
0504 struct ata_bmdma_prd *prd = ap->bmdma_prd;
0505 struct scatterlist *sg;
0506 unsigned int si;
0507
0508 for_each_sg(qc->sg, sg, qc->n_elem, si) {
0509 u32 addr, sg_len;
0510
0511
0512
0513
0514
0515 addr = (u32)sg_dma_address(sg);
0516 sg_len = sg_dma_len(sg);
0517
0518 prd[si].addr = cpu_to_le32(addr);
0519 prd[si].flags_len = cpu_to_le32(sg_len);
0520 }
0521
0522
0523 prd[si - 1].addr |= cpu_to_le32(SATA_RCAR_DTEND);
0524 }
0525
0526 static enum ata_completion_errors sata_rcar_qc_prep(struct ata_queued_cmd *qc)
0527 {
0528 if (!(qc->flags & ATA_QCFLAG_DMAMAP))
0529 return AC_ERR_OK;
0530
0531 sata_rcar_bmdma_fill_sg(qc);
0532
0533 return AC_ERR_OK;
0534 }
0535
0536 static void sata_rcar_bmdma_setup(struct ata_queued_cmd *qc)
0537 {
0538 struct ata_port *ap = qc->ap;
0539 unsigned int rw = qc->tf.flags & ATA_TFLAG_WRITE;
0540 struct sata_rcar_priv *priv = ap->host->private_data;
0541 void __iomem *base = priv->base;
0542 u32 dmactl;
0543
0544
0545 mb();
0546 iowrite32(ap->bmdma_prd_dma, base + ATAPI_DTB_ADR_REG);
0547
0548
0549 dmactl = ioread32(base + ATAPI_CONTROL1_REG);
0550 dmactl &= ~(ATAPI_CONTROL1_RW | ATAPI_CONTROL1_STOP);
0551 if (dmactl & ATAPI_CONTROL1_START) {
0552 dmactl &= ~ATAPI_CONTROL1_START;
0553 dmactl |= ATAPI_CONTROL1_STOP;
0554 }
0555 if (!rw)
0556 dmactl |= ATAPI_CONTROL1_RW;
0557 iowrite32(dmactl, base + ATAPI_CONTROL1_REG);
0558
0559
0560 ap->ops->sff_exec_command(ap, &qc->tf);
0561 }
0562
0563 static void sata_rcar_bmdma_start(struct ata_queued_cmd *qc)
0564 {
0565 struct ata_port *ap = qc->ap;
0566 struct sata_rcar_priv *priv = ap->host->private_data;
0567 void __iomem *base = priv->base;
0568 u32 dmactl;
0569
0570
0571 dmactl = ioread32(base + ATAPI_CONTROL1_REG);
0572 dmactl &= ~ATAPI_CONTROL1_STOP;
0573 dmactl |= ATAPI_CONTROL1_START;
0574 iowrite32(dmactl, base + ATAPI_CONTROL1_REG);
0575 }
0576
0577 static void sata_rcar_bmdma_stop(struct ata_queued_cmd *qc)
0578 {
0579 struct ata_port *ap = qc->ap;
0580 struct sata_rcar_priv *priv = ap->host->private_data;
0581 void __iomem *base = priv->base;
0582 u32 dmactl;
0583
0584
0585 dmactl = ioread32(base + ATAPI_CONTROL1_REG);
0586 if (dmactl & ATAPI_CONTROL1_START) {
0587 dmactl &= ~ATAPI_CONTROL1_START;
0588 dmactl |= ATAPI_CONTROL1_STOP;
0589 iowrite32(dmactl, base + ATAPI_CONTROL1_REG);
0590 }
0591
0592
0593 ata_sff_dma_pause(ap);
0594 }
0595
0596 static u8 sata_rcar_bmdma_status(struct ata_port *ap)
0597 {
0598 struct sata_rcar_priv *priv = ap->host->private_data;
0599 u8 host_stat = 0;
0600 u32 status;
0601
0602 status = ioread32(priv->base + ATAPI_STATUS_REG);
0603 if (status & ATAPI_STATUS_DEVINT)
0604 host_stat |= ATA_DMA_INTR;
0605 if (status & ATAPI_STATUS_ACT)
0606 host_stat |= ATA_DMA_ACTIVE;
0607
0608 return host_stat;
0609 }
0610
0611 static struct scsi_host_template sata_rcar_sht = {
0612 ATA_BASE_SHT(DRV_NAME),
0613
0614
0615
0616
0617
0618 .sg_tablesize = ATA_MAX_PRD,
0619 .dma_boundary = SATA_RCAR_DMA_BOUNDARY,
0620 };
0621
0622 static struct ata_port_operations sata_rcar_port_ops = {
0623 .inherits = &ata_bmdma_port_ops,
0624
0625 .freeze = sata_rcar_freeze,
0626 .thaw = sata_rcar_thaw,
0627 .softreset = sata_rcar_softreset,
0628
0629 .scr_read = sata_rcar_scr_read,
0630 .scr_write = sata_rcar_scr_write,
0631
0632 .sff_dev_select = sata_rcar_dev_select,
0633 .sff_set_devctl = sata_rcar_set_devctl,
0634 .sff_check_status = sata_rcar_check_status,
0635 .sff_check_altstatus = sata_rcar_check_altstatus,
0636 .sff_tf_load = sata_rcar_tf_load,
0637 .sff_tf_read = sata_rcar_tf_read,
0638 .sff_exec_command = sata_rcar_exec_command,
0639 .sff_data_xfer = sata_rcar_data_xfer,
0640 .sff_drain_fifo = sata_rcar_drain_fifo,
0641
0642 .qc_prep = sata_rcar_qc_prep,
0643
0644 .bmdma_setup = sata_rcar_bmdma_setup,
0645 .bmdma_start = sata_rcar_bmdma_start,
0646 .bmdma_stop = sata_rcar_bmdma_stop,
0647 .bmdma_status = sata_rcar_bmdma_status,
0648 };
0649
0650 static void sata_rcar_serr_interrupt(struct ata_port *ap)
0651 {
0652 struct sata_rcar_priv *priv = ap->host->private_data;
0653 struct ata_eh_info *ehi = &ap->link.eh_info;
0654 int freeze = 0;
0655 u32 serror;
0656
0657 serror = ioread32(priv->base + SCRSERR_REG);
0658 if (!serror)
0659 return;
0660
0661 ata_port_dbg(ap, "SError @host_intr: 0x%x\n", serror);
0662
0663
0664 ata_ehi_clear_desc(ehi);
0665
0666 if (serror & (SERR_DEV_XCHG | SERR_PHYRDY_CHG)) {
0667
0668 ata_ehi_hotplugged(ehi);
0669 ata_ehi_push_desc(ehi, "%s", "hotplug");
0670
0671 freeze = serror & SERR_COMM_WAKE ? 0 : 1;
0672 }
0673
0674
0675 if (freeze)
0676 ata_port_freeze(ap);
0677 else
0678 ata_port_abort(ap);
0679 }
0680
0681 static void sata_rcar_ata_interrupt(struct ata_port *ap)
0682 {
0683 struct ata_queued_cmd *qc;
0684 int handled = 0;
0685
0686 qc = ata_qc_from_tag(ap, ap->link.active_tag);
0687 if (qc)
0688 handled |= ata_bmdma_port_intr(ap, qc);
0689
0690
0691 if (!handled)
0692 sata_rcar_check_status(ap);
0693 }
0694
0695 static irqreturn_t sata_rcar_interrupt(int irq, void *dev_instance)
0696 {
0697 struct ata_host *host = dev_instance;
0698 struct sata_rcar_priv *priv = host->private_data;
0699 void __iomem *base = priv->base;
0700 unsigned int handled = 0;
0701 struct ata_port *ap;
0702 u32 sataintstat;
0703 unsigned long flags;
0704
0705 spin_lock_irqsave(&host->lock, flags);
0706
0707 sataintstat = ioread32(base + SATAINTSTAT_REG);
0708 sataintstat &= SATA_RCAR_INT_MASK;
0709 if (!sataintstat)
0710 goto done;
0711
0712 iowrite32(~sataintstat & priv->sataint_mask, base + SATAINTSTAT_REG);
0713
0714 ap = host->ports[0];
0715
0716 if (sataintstat & SATAINTSTAT_ATA)
0717 sata_rcar_ata_interrupt(ap);
0718
0719 if (sataintstat & SATAINTSTAT_SERR)
0720 sata_rcar_serr_interrupt(ap);
0721
0722 handled = 1;
0723 done:
0724 spin_unlock_irqrestore(&host->lock, flags);
0725
0726 return IRQ_RETVAL(handled);
0727 }
0728
0729 static void sata_rcar_setup_port(struct ata_host *host)
0730 {
0731 struct ata_port *ap = host->ports[0];
0732 struct ata_ioports *ioaddr = &ap->ioaddr;
0733 struct sata_rcar_priv *priv = host->private_data;
0734 void __iomem *base = priv->base;
0735
0736 ap->ops = &sata_rcar_port_ops;
0737 ap->pio_mask = ATA_PIO4;
0738 ap->udma_mask = ATA_UDMA6;
0739 ap->flags |= ATA_FLAG_SATA;
0740
0741 if (priv->type == RCAR_R8A7790_ES1_SATA)
0742 ap->flags |= ATA_FLAG_NO_DIPM;
0743
0744 ioaddr->cmd_addr = base + SDATA_REG;
0745 ioaddr->ctl_addr = base + SSDEVCON_REG;
0746 ioaddr->scr_addr = base + SCRSSTS_REG;
0747 ioaddr->altstatus_addr = ioaddr->ctl_addr;
0748
0749 ioaddr->data_addr = ioaddr->cmd_addr + (ATA_REG_DATA << 2);
0750 ioaddr->error_addr = ioaddr->cmd_addr + (ATA_REG_ERR << 2);
0751 ioaddr->feature_addr = ioaddr->cmd_addr + (ATA_REG_FEATURE << 2);
0752 ioaddr->nsect_addr = ioaddr->cmd_addr + (ATA_REG_NSECT << 2);
0753 ioaddr->lbal_addr = ioaddr->cmd_addr + (ATA_REG_LBAL << 2);
0754 ioaddr->lbam_addr = ioaddr->cmd_addr + (ATA_REG_LBAM << 2);
0755 ioaddr->lbah_addr = ioaddr->cmd_addr + (ATA_REG_LBAH << 2);
0756 ioaddr->device_addr = ioaddr->cmd_addr + (ATA_REG_DEVICE << 2);
0757 ioaddr->status_addr = ioaddr->cmd_addr + (ATA_REG_STATUS << 2);
0758 ioaddr->command_addr = ioaddr->cmd_addr + (ATA_REG_CMD << 2);
0759 }
0760
0761 static void sata_rcar_init_module(struct sata_rcar_priv *priv)
0762 {
0763 void __iomem *base = priv->base;
0764 u32 val;
0765
0766
0767 val = ioread32(base + ATAPI_CONTROL1_REG);
0768 val |= ATAPI_CONTROL1_RESET;
0769 iowrite32(val, base + ATAPI_CONTROL1_REG);
0770
0771
0772 val = ioread32(base + ATAPI_CONTROL1_REG);
0773 val |= ATAPI_CONTROL1_ISM;
0774 val |= ATAPI_CONTROL1_DESE;
0775 val |= ATAPI_CONTROL1_DTA32M;
0776 iowrite32(val, base + ATAPI_CONTROL1_REG);
0777
0778
0779 val = ioread32(base + ATAPI_CONTROL1_REG);
0780 val &= ~ATAPI_CONTROL1_RESET;
0781 iowrite32(val, base + ATAPI_CONTROL1_REG);
0782
0783
0784 iowrite32(0, base + SATAINTSTAT_REG);
0785 iowrite32(priv->sataint_mask, base + SATAINTMASK_REG);
0786
0787
0788 iowrite32(ATAPI_INT_ENABLE_SATAINT, base + ATAPI_INT_ENABLE_REG);
0789 }
0790
0791 static void sata_rcar_init_controller(struct ata_host *host)
0792 {
0793 struct sata_rcar_priv *priv = host->private_data;
0794
0795 priv->sataint_mask = SATAINTMASK_ALL_GEN2;
0796
0797
0798 switch (priv->type) {
0799 case RCAR_GEN1_SATA:
0800 priv->sataint_mask = SATAINTMASK_ALL_GEN1;
0801 sata_rcar_gen1_phy_init(priv);
0802 break;
0803 case RCAR_GEN2_SATA:
0804 case RCAR_R8A7790_ES1_SATA:
0805 sata_rcar_gen2_phy_init(priv);
0806 break;
0807 case RCAR_GEN3_SATA:
0808 break;
0809 default:
0810 dev_warn(host->dev, "SATA phy is not initialized\n");
0811 break;
0812 }
0813
0814 sata_rcar_init_module(priv);
0815 }
0816
0817 static const struct of_device_id sata_rcar_match[] = {
0818 {
0819
0820 .compatible = "renesas,rcar-sata",
0821 .data = (void *)RCAR_GEN1_SATA,
0822 },
0823 {
0824 .compatible = "renesas,sata-r8a7779",
0825 .data = (void *)RCAR_GEN1_SATA,
0826 },
0827 {
0828 .compatible = "renesas,sata-r8a7790",
0829 .data = (void *)RCAR_GEN2_SATA
0830 },
0831 {
0832 .compatible = "renesas,sata-r8a7790-es1",
0833 .data = (void *)RCAR_R8A7790_ES1_SATA
0834 },
0835 {
0836 .compatible = "renesas,sata-r8a7791",
0837 .data = (void *)RCAR_GEN2_SATA
0838 },
0839 {
0840 .compatible = "renesas,sata-r8a7793",
0841 .data = (void *)RCAR_GEN2_SATA
0842 },
0843 {
0844 .compatible = "renesas,sata-r8a7795",
0845 .data = (void *)RCAR_GEN3_SATA
0846 },
0847 {
0848 .compatible = "renesas,rcar-gen2-sata",
0849 .data = (void *)RCAR_GEN2_SATA
0850 },
0851 {
0852 .compatible = "renesas,rcar-gen3-sata",
0853 .data = (void *)RCAR_GEN3_SATA
0854 },
0855 { }
0856 };
0857 MODULE_DEVICE_TABLE(of, sata_rcar_match);
0858
0859 static int sata_rcar_probe(struct platform_device *pdev)
0860 {
0861 struct device *dev = &pdev->dev;
0862 struct ata_host *host;
0863 struct sata_rcar_priv *priv;
0864 struct resource *mem;
0865 int irq;
0866 int ret = 0;
0867
0868 irq = platform_get_irq(pdev, 0);
0869 if (irq < 0)
0870 return irq;
0871 if (!irq)
0872 return -EINVAL;
0873
0874 priv = devm_kzalloc(dev, sizeof(struct sata_rcar_priv), GFP_KERNEL);
0875 if (!priv)
0876 return -ENOMEM;
0877
0878 priv->type = (enum sata_rcar_type)of_device_get_match_data(dev);
0879
0880 pm_runtime_enable(dev);
0881 ret = pm_runtime_get_sync(dev);
0882 if (ret < 0)
0883 goto err_pm_put;
0884
0885 host = ata_host_alloc(dev, 1);
0886 if (!host) {
0887 ret = -ENOMEM;
0888 goto err_pm_put;
0889 }
0890
0891 host->private_data = priv;
0892
0893 mem = platform_get_resource(pdev, IORESOURCE_MEM, 0);
0894 priv->base = devm_ioremap_resource(dev, mem);
0895 if (IS_ERR(priv->base)) {
0896 ret = PTR_ERR(priv->base);
0897 goto err_pm_put;
0898 }
0899
0900
0901 sata_rcar_setup_port(host);
0902
0903
0904 sata_rcar_init_controller(host);
0905
0906 ret = ata_host_activate(host, irq, sata_rcar_interrupt, 0,
0907 &sata_rcar_sht);
0908 if (!ret)
0909 return 0;
0910
0911 err_pm_put:
0912 pm_runtime_put(dev);
0913 pm_runtime_disable(dev);
0914 return ret;
0915 }
0916
0917 static int sata_rcar_remove(struct platform_device *pdev)
0918 {
0919 struct ata_host *host = platform_get_drvdata(pdev);
0920 struct sata_rcar_priv *priv = host->private_data;
0921 void __iomem *base = priv->base;
0922
0923 ata_host_detach(host);
0924
0925
0926 iowrite32(0, base + ATAPI_INT_ENABLE_REG);
0927
0928 iowrite32(0, base + SATAINTSTAT_REG);
0929 iowrite32(priv->sataint_mask, base + SATAINTMASK_REG);
0930
0931 pm_runtime_put(&pdev->dev);
0932 pm_runtime_disable(&pdev->dev);
0933
0934 return 0;
0935 }
0936
0937 #ifdef CONFIG_PM_SLEEP
0938 static int sata_rcar_suspend(struct device *dev)
0939 {
0940 struct ata_host *host = dev_get_drvdata(dev);
0941 struct sata_rcar_priv *priv = host->private_data;
0942 void __iomem *base = priv->base;
0943
0944 ata_host_suspend(host, PMSG_SUSPEND);
0945
0946
0947 iowrite32(0, base + ATAPI_INT_ENABLE_REG);
0948
0949 iowrite32(priv->sataint_mask, base + SATAINTMASK_REG);
0950
0951 pm_runtime_put(dev);
0952
0953 return 0;
0954 }
0955
0956 static int sata_rcar_resume(struct device *dev)
0957 {
0958 struct ata_host *host = dev_get_drvdata(dev);
0959 struct sata_rcar_priv *priv = host->private_data;
0960 void __iomem *base = priv->base;
0961 int ret;
0962
0963 ret = pm_runtime_get_sync(dev);
0964 if (ret < 0) {
0965 pm_runtime_put(dev);
0966 return ret;
0967 }
0968
0969 if (priv->type == RCAR_GEN3_SATA) {
0970 sata_rcar_init_module(priv);
0971 } else {
0972
0973 iowrite32(0, base + SATAINTSTAT_REG);
0974 iowrite32(priv->sataint_mask, base + SATAINTMASK_REG);
0975
0976
0977 iowrite32(ATAPI_INT_ENABLE_SATAINT,
0978 base + ATAPI_INT_ENABLE_REG);
0979 }
0980
0981 ata_host_resume(host);
0982
0983 return 0;
0984 }
0985
0986 static int sata_rcar_restore(struct device *dev)
0987 {
0988 struct ata_host *host = dev_get_drvdata(dev);
0989 int ret;
0990
0991 ret = pm_runtime_get_sync(dev);
0992 if (ret < 0) {
0993 pm_runtime_put(dev);
0994 return ret;
0995 }
0996
0997 sata_rcar_setup_port(host);
0998
0999
1000 sata_rcar_init_controller(host);
1001
1002 ata_host_resume(host);
1003
1004 return 0;
1005 }
1006
1007 static const struct dev_pm_ops sata_rcar_pm_ops = {
1008 .suspend = sata_rcar_suspend,
1009 .resume = sata_rcar_resume,
1010 .freeze = sata_rcar_suspend,
1011 .thaw = sata_rcar_resume,
1012 .poweroff = sata_rcar_suspend,
1013 .restore = sata_rcar_restore,
1014 };
1015 #endif
1016
1017 static struct platform_driver sata_rcar_driver = {
1018 .probe = sata_rcar_probe,
1019 .remove = sata_rcar_remove,
1020 .driver = {
1021 .name = DRV_NAME,
1022 .of_match_table = sata_rcar_match,
1023 #ifdef CONFIG_PM_SLEEP
1024 .pm = &sata_rcar_pm_ops,
1025 #endif
1026 },
1027 };
1028
1029 module_platform_driver(sata_rcar_driver);
1030
1031 MODULE_LICENSE("GPL");
1032 MODULE_AUTHOR("Vladimir Barinov");
1033 MODULE_DESCRIPTION("Renesas R-Car SATA controller low level driver");