sata_rcar.c 27 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063
  1. /*
  2. * Renesas R-Car SATA driver
  3. *
  4. * Author: Vladimir Barinov <source@cogentembedded.com>
  5. * Copyright (C) 2013-2015 Cogent Embedded, Inc.
  6. * Copyright (C) 2013-2015 Renesas Solutions Corp.
  7. *
  8. * This program is free software; you can redistribute it and/or modify it
  9. * under the terms of the GNU General Public License as published by the
  10. * Free Software Foundation; either version 2 of the License, or (at your
  11. * option) any later version.
  12. */
  13. #include <linux/kernel.h>
  14. #include <linux/module.h>
  15. #include <linux/ata.h>
  16. #include <linux/libata.h>
  17. #include <linux/of_device.h>
  18. #include <linux/platform_device.h>
  19. #include <linux/pm_runtime.h>
  20. #include <linux/err.h>
  21. #define DRV_NAME "sata_rcar"
  22. /* SH-Navi2G/ATAPI-ATA compatible task registers */
  23. #define DATA_REG 0x100
  24. #define SDEVCON_REG 0x138
  25. /* SH-Navi2G/ATAPI module compatible control registers */
  26. #define ATAPI_CONTROL1_REG 0x180
  27. #define ATAPI_STATUS_REG 0x184
  28. #define ATAPI_INT_ENABLE_REG 0x188
  29. #define ATAPI_DTB_ADR_REG 0x198
  30. #define ATAPI_DMA_START_ADR_REG 0x19C
  31. #define ATAPI_DMA_TRANS_CNT_REG 0x1A0
  32. #define ATAPI_CONTROL2_REG 0x1A4
  33. #define ATAPI_SIG_ST_REG 0x1B0
  34. #define ATAPI_BYTE_SWAP_REG 0x1BC
  35. /* ATAPI control 1 register (ATAPI_CONTROL1) bits */
  36. #define ATAPI_CONTROL1_ISM BIT(16)
  37. #define ATAPI_CONTROL1_DTA32M BIT(11)
  38. #define ATAPI_CONTROL1_RESET BIT(7)
  39. #define ATAPI_CONTROL1_DESE BIT(3)
  40. #define ATAPI_CONTROL1_RW BIT(2)
  41. #define ATAPI_CONTROL1_STOP BIT(1)
  42. #define ATAPI_CONTROL1_START BIT(0)
  43. /* ATAPI status register (ATAPI_STATUS) bits */
  44. #define ATAPI_STATUS_SATAINT BIT(11)
  45. #define ATAPI_STATUS_DNEND BIT(6)
  46. #define ATAPI_STATUS_DEVTRM BIT(5)
  47. #define ATAPI_STATUS_DEVINT BIT(4)
  48. #define ATAPI_STATUS_ERR BIT(2)
  49. #define ATAPI_STATUS_NEND BIT(1)
  50. #define ATAPI_STATUS_ACT BIT(0)
  51. /* Interrupt enable register (ATAPI_INT_ENABLE) bits */
  52. #define ATAPI_INT_ENABLE_SATAINT BIT(11)
  53. #define ATAPI_INT_ENABLE_DNEND BIT(6)
  54. #define ATAPI_INT_ENABLE_DEVTRM BIT(5)
  55. #define ATAPI_INT_ENABLE_DEVINT BIT(4)
  56. #define ATAPI_INT_ENABLE_ERR BIT(2)
  57. #define ATAPI_INT_ENABLE_NEND BIT(1)
  58. #define ATAPI_INT_ENABLE_ACT BIT(0)
  59. /* Access control registers for physical layer control register */
  60. #define SATAPHYADDR_REG 0x200
  61. #define SATAPHYWDATA_REG 0x204
  62. #define SATAPHYACCEN_REG 0x208
  63. #define SATAPHYRESET_REG 0x20C
  64. #define SATAPHYRDATA_REG 0x210
  65. #define SATAPHYACK_REG 0x214
  66. /* Physical layer control address command register (SATAPHYADDR) bits */
  67. #define SATAPHYADDR_PHYRATEMODE BIT(10)
  68. #define SATAPHYADDR_PHYCMD_READ BIT(9)
  69. #define SATAPHYADDR_PHYCMD_WRITE BIT(8)
  70. /* Physical layer control enable register (SATAPHYACCEN) bits */
  71. #define SATAPHYACCEN_PHYLANE BIT(0)
  72. /* Physical layer control reset register (SATAPHYRESET) bits */
  73. #define SATAPHYRESET_PHYRST BIT(1)
  74. #define SATAPHYRESET_PHYSRES BIT(0)
  75. /* Physical layer control acknowledge register (SATAPHYACK) bits */
  76. #define SATAPHYACK_PHYACK BIT(0)
  77. /* Serial-ATA HOST control registers */
  78. #define BISTCONF_REG 0x102C
  79. #define SDATA_REG 0x1100
  80. #define SSDEVCON_REG 0x1204
  81. #define SCRSSTS_REG 0x1400
  82. #define SCRSERR_REG 0x1404
  83. #define SCRSCON_REG 0x1408
  84. #define SCRSACT_REG 0x140C
  85. #define SATAINTSTAT_REG 0x1508
  86. #define SATAINTMASK_REG 0x150C
  87. /* SATA INT status register (SATAINTSTAT) bits */
  88. #define SATAINTSTAT_SERR BIT(3)
  89. #define SATAINTSTAT_ATA BIT(0)
  90. /* SATA INT mask register (SATAINTSTAT) bits */
  91. #define SATAINTMASK_SERRMSK BIT(3)
  92. #define SATAINTMASK_ERRMSK BIT(2)
  93. #define SATAINTMASK_ERRCRTMSK BIT(1)
  94. #define SATAINTMASK_ATAMSK BIT(0)
  95. #define SATAINTMASK_ALL_GEN1 0x7ff
  96. #define SATAINTMASK_ALL_GEN2 0xfff
  97. #define SATA_RCAR_INT_MASK (SATAINTMASK_SERRMSK | \
  98. SATAINTMASK_ATAMSK)
  99. /* Physical Layer Control Registers */
  100. #define SATAPCTLR1_REG 0x43
  101. #define SATAPCTLR2_REG 0x52
  102. #define SATAPCTLR3_REG 0x5A
  103. #define SATAPCTLR4_REG 0x60
  104. /* Descriptor table word 0 bit (when DTA32M = 1) */
  105. #define SATA_RCAR_DTEND BIT(0)
  106. #define SATA_RCAR_DMA_BOUNDARY 0x1FFFFFFEUL
  107. /* Gen2 Physical Layer Control Registers */
  108. #define RCAR_GEN2_PHY_CTL1_REG 0x1704
  109. #define RCAR_GEN2_PHY_CTL1 0x34180002
  110. #define RCAR_GEN2_PHY_CTL1_SS 0xC180 /* Spread Spectrum */
  111. #define RCAR_GEN2_PHY_CTL2_REG 0x170C
  112. #define RCAR_GEN2_PHY_CTL2 0x00002303
  113. #define RCAR_GEN2_PHY_CTL3_REG 0x171C
  114. #define RCAR_GEN2_PHY_CTL3 0x000B0194
  115. #define RCAR_GEN2_PHY_CTL4_REG 0x1724
  116. #define RCAR_GEN2_PHY_CTL4 0x00030994
  117. #define RCAR_GEN2_PHY_CTL5_REG 0x1740
  118. #define RCAR_GEN2_PHY_CTL5 0x03004001
  119. #define RCAR_GEN2_PHY_CTL5_DC BIT(1) /* DC connection */
  120. #define RCAR_GEN2_PHY_CTL5_TR BIT(2) /* Termination Resistor */
  121. enum sata_rcar_type {
  122. RCAR_GEN1_SATA,
  123. RCAR_GEN2_SATA,
  124. RCAR_GEN3_SATA,
  125. RCAR_R8A7790_ES1_SATA,
  126. };
  127. struct sata_rcar_priv {
  128. void __iomem *base;
  129. u32 sataint_mask;
  130. enum sata_rcar_type type;
  131. };
  132. static void sata_rcar_gen1_phy_preinit(struct sata_rcar_priv *priv)
  133. {
  134. void __iomem *base = priv->base;
  135. /* idle state */
  136. iowrite32(0, base + SATAPHYADDR_REG);
  137. /* reset */
  138. iowrite32(SATAPHYRESET_PHYRST, base + SATAPHYRESET_REG);
  139. udelay(10);
  140. /* deassert reset */
  141. iowrite32(0, base + SATAPHYRESET_REG);
  142. }
  143. static void sata_rcar_gen1_phy_write(struct sata_rcar_priv *priv, u16 reg,
  144. u32 val, int group)
  145. {
  146. void __iomem *base = priv->base;
  147. int timeout;
  148. /* deassert reset */
  149. iowrite32(0, base + SATAPHYRESET_REG);
  150. /* lane 1 */
  151. iowrite32(SATAPHYACCEN_PHYLANE, base + SATAPHYACCEN_REG);
  152. /* write phy register value */
  153. iowrite32(val, base + SATAPHYWDATA_REG);
  154. /* set register group */
  155. if (group)
  156. reg |= SATAPHYADDR_PHYRATEMODE;
  157. /* write command */
  158. iowrite32(SATAPHYADDR_PHYCMD_WRITE | reg, base + SATAPHYADDR_REG);
  159. /* wait for ack */
  160. for (timeout = 0; timeout < 100; timeout++) {
  161. val = ioread32(base + SATAPHYACK_REG);
  162. if (val & SATAPHYACK_PHYACK)
  163. break;
  164. }
  165. if (timeout >= 100)
  166. pr_err("%s timeout\n", __func__);
  167. /* idle state */
  168. iowrite32(0, base + SATAPHYADDR_REG);
  169. }
  170. static void sata_rcar_gen1_phy_init(struct sata_rcar_priv *priv)
  171. {
  172. sata_rcar_gen1_phy_preinit(priv);
  173. sata_rcar_gen1_phy_write(priv, SATAPCTLR1_REG, 0x00200188, 0);
  174. sata_rcar_gen1_phy_write(priv, SATAPCTLR1_REG, 0x00200188, 1);
  175. sata_rcar_gen1_phy_write(priv, SATAPCTLR3_REG, 0x0000A061, 0);
  176. sata_rcar_gen1_phy_write(priv, SATAPCTLR2_REG, 0x20000000, 0);
  177. sata_rcar_gen1_phy_write(priv, SATAPCTLR2_REG, 0x20000000, 1);
  178. sata_rcar_gen1_phy_write(priv, SATAPCTLR4_REG, 0x28E80000, 0);
  179. }
  180. static void sata_rcar_gen2_phy_init(struct sata_rcar_priv *priv)
  181. {
  182. void __iomem *base = priv->base;
  183. iowrite32(RCAR_GEN2_PHY_CTL1, base + RCAR_GEN2_PHY_CTL1_REG);
  184. iowrite32(RCAR_GEN2_PHY_CTL2, base + RCAR_GEN2_PHY_CTL2_REG);
  185. iowrite32(RCAR_GEN2_PHY_CTL3, base + RCAR_GEN2_PHY_CTL3_REG);
  186. iowrite32(RCAR_GEN2_PHY_CTL4, base + RCAR_GEN2_PHY_CTL4_REG);
  187. iowrite32(RCAR_GEN2_PHY_CTL5 | RCAR_GEN2_PHY_CTL5_DC |
  188. RCAR_GEN2_PHY_CTL5_TR, base + RCAR_GEN2_PHY_CTL5_REG);
  189. }
  190. static void sata_rcar_freeze(struct ata_port *ap)
  191. {
  192. struct sata_rcar_priv *priv = ap->host->private_data;
  193. /* mask */
  194. iowrite32(priv->sataint_mask, priv->base + SATAINTMASK_REG);
  195. ata_sff_freeze(ap);
  196. }
  197. static void sata_rcar_thaw(struct ata_port *ap)
  198. {
  199. struct sata_rcar_priv *priv = ap->host->private_data;
  200. void __iomem *base = priv->base;
  201. /* ack */
  202. iowrite32(~(u32)SATA_RCAR_INT_MASK, base + SATAINTSTAT_REG);
  203. ata_sff_thaw(ap);
  204. /* unmask */
  205. iowrite32(priv->sataint_mask & ~SATA_RCAR_INT_MASK, base + SATAINTMASK_REG);
  206. }
  207. static void sata_rcar_ioread16_rep(void __iomem *reg, void *buffer, int count)
  208. {
  209. u16 *ptr = buffer;
  210. while (count--) {
  211. u16 data = ioread32(reg);
  212. *ptr++ = data;
  213. }
  214. }
  215. static void sata_rcar_iowrite16_rep(void __iomem *reg, void *buffer, int count)
  216. {
  217. const u16 *ptr = buffer;
  218. while (count--)
  219. iowrite32(*ptr++, reg);
  220. }
  221. static u8 sata_rcar_check_status(struct ata_port *ap)
  222. {
  223. return ioread32(ap->ioaddr.status_addr);
  224. }
  225. static u8 sata_rcar_check_altstatus(struct ata_port *ap)
  226. {
  227. return ioread32(ap->ioaddr.altstatus_addr);
  228. }
  229. static void sata_rcar_set_devctl(struct ata_port *ap, u8 ctl)
  230. {
  231. iowrite32(ctl, ap->ioaddr.ctl_addr);
  232. }
  233. static void sata_rcar_dev_select(struct ata_port *ap, unsigned int device)
  234. {
  235. iowrite32(ATA_DEVICE_OBS, ap->ioaddr.device_addr);
  236. ata_sff_pause(ap); /* needed; also flushes, for mmio */
  237. }
  238. static unsigned int sata_rcar_ata_devchk(struct ata_port *ap,
  239. unsigned int device)
  240. {
  241. struct ata_ioports *ioaddr = &ap->ioaddr;
  242. u8 nsect, lbal;
  243. sata_rcar_dev_select(ap, device);
  244. iowrite32(0x55, ioaddr->nsect_addr);
  245. iowrite32(0xaa, ioaddr->lbal_addr);
  246. iowrite32(0xaa, ioaddr->nsect_addr);
  247. iowrite32(0x55, ioaddr->lbal_addr);
  248. iowrite32(0x55, ioaddr->nsect_addr);
  249. iowrite32(0xaa, ioaddr->lbal_addr);
  250. nsect = ioread32(ioaddr->nsect_addr);
  251. lbal = ioread32(ioaddr->lbal_addr);
  252. if (nsect == 0x55 && lbal == 0xaa)
  253. return 1; /* found a device */
  254. return 0; /* nothing found */
  255. }
  256. static int sata_rcar_wait_after_reset(struct ata_link *link,
  257. unsigned long deadline)
  258. {
  259. struct ata_port *ap = link->ap;
  260. ata_msleep(ap, ATA_WAIT_AFTER_RESET);
  261. return ata_sff_wait_ready(link, deadline);
  262. }
  263. static int sata_rcar_bus_softreset(struct ata_port *ap, unsigned long deadline)
  264. {
  265. struct ata_ioports *ioaddr = &ap->ioaddr;
  266. DPRINTK("ata%u: bus reset via SRST\n", ap->print_id);
  267. /* software reset. causes dev0 to be selected */
  268. iowrite32(ap->ctl, ioaddr->ctl_addr);
  269. udelay(20);
  270. iowrite32(ap->ctl | ATA_SRST, ioaddr->ctl_addr);
  271. udelay(20);
  272. iowrite32(ap->ctl, ioaddr->ctl_addr);
  273. ap->last_ctl = ap->ctl;
  274. /* wait the port to become ready */
  275. return sata_rcar_wait_after_reset(&ap->link, deadline);
  276. }
  277. static int sata_rcar_softreset(struct ata_link *link, unsigned int *classes,
  278. unsigned long deadline)
  279. {
  280. struct ata_port *ap = link->ap;
  281. unsigned int devmask = 0;
  282. int rc;
  283. u8 err;
  284. /* determine if device 0 is present */
  285. if (sata_rcar_ata_devchk(ap, 0))
  286. devmask |= 1 << 0;
  287. /* issue bus reset */
  288. DPRINTK("about to softreset, devmask=%x\n", devmask);
  289. rc = sata_rcar_bus_softreset(ap, deadline);
  290. /* if link is occupied, -ENODEV too is an error */
  291. if (rc && (rc != -ENODEV || sata_scr_valid(link))) {
  292. ata_link_err(link, "SRST failed (errno=%d)\n", rc);
  293. return rc;
  294. }
  295. /* determine by signature whether we have ATA or ATAPI devices */
  296. classes[0] = ata_sff_dev_classify(&link->device[0], devmask, &err);
  297. DPRINTK("classes[0]=%u\n", classes[0]);
  298. return 0;
  299. }
  300. static void sata_rcar_tf_load(struct ata_port *ap,
  301. const struct ata_taskfile *tf)
  302. {
  303. struct ata_ioports *ioaddr = &ap->ioaddr;
  304. unsigned int is_addr = tf->flags & ATA_TFLAG_ISADDR;
  305. if (tf->ctl != ap->last_ctl) {
  306. iowrite32(tf->ctl, ioaddr->ctl_addr);
  307. ap->last_ctl = tf->ctl;
  308. ata_wait_idle(ap);
  309. }
  310. if (is_addr && (tf->flags & ATA_TFLAG_LBA48)) {
  311. iowrite32(tf->hob_feature, ioaddr->feature_addr);
  312. iowrite32(tf->hob_nsect, ioaddr->nsect_addr);
  313. iowrite32(tf->hob_lbal, ioaddr->lbal_addr);
  314. iowrite32(tf->hob_lbam, ioaddr->lbam_addr);
  315. iowrite32(tf->hob_lbah, ioaddr->lbah_addr);
  316. VPRINTK("hob: feat 0x%X nsect 0x%X, lba 0x%X 0x%X 0x%X\n",
  317. tf->hob_feature,
  318. tf->hob_nsect,
  319. tf->hob_lbal,
  320. tf->hob_lbam,
  321. tf->hob_lbah);
  322. }
  323. if (is_addr) {
  324. iowrite32(tf->feature, ioaddr->feature_addr);
  325. iowrite32(tf->nsect, ioaddr->nsect_addr);
  326. iowrite32(tf->lbal, ioaddr->lbal_addr);
  327. iowrite32(tf->lbam, ioaddr->lbam_addr);
  328. iowrite32(tf->lbah, ioaddr->lbah_addr);
  329. VPRINTK("feat 0x%X nsect 0x%X lba 0x%X 0x%X 0x%X\n",
  330. tf->feature,
  331. tf->nsect,
  332. tf->lbal,
  333. tf->lbam,
  334. tf->lbah);
  335. }
  336. if (tf->flags & ATA_TFLAG_DEVICE) {
  337. iowrite32(tf->device, ioaddr->device_addr);
  338. VPRINTK("device 0x%X\n", tf->device);
  339. }
  340. ata_wait_idle(ap);
  341. }
  342. static void sata_rcar_tf_read(struct ata_port *ap, struct ata_taskfile *tf)
  343. {
  344. struct ata_ioports *ioaddr = &ap->ioaddr;
  345. tf->command = sata_rcar_check_status(ap);
  346. tf->feature = ioread32(ioaddr->error_addr);
  347. tf->nsect = ioread32(ioaddr->nsect_addr);
  348. tf->lbal = ioread32(ioaddr->lbal_addr);
  349. tf->lbam = ioread32(ioaddr->lbam_addr);
  350. tf->lbah = ioread32(ioaddr->lbah_addr);
  351. tf->device = ioread32(ioaddr->device_addr);
  352. if (tf->flags & ATA_TFLAG_LBA48) {
  353. iowrite32(tf->ctl | ATA_HOB, ioaddr->ctl_addr);
  354. tf->hob_feature = ioread32(ioaddr->error_addr);
  355. tf->hob_nsect = ioread32(ioaddr->nsect_addr);
  356. tf->hob_lbal = ioread32(ioaddr->lbal_addr);
  357. tf->hob_lbam = ioread32(ioaddr->lbam_addr);
  358. tf->hob_lbah = ioread32(ioaddr->lbah_addr);
  359. iowrite32(tf->ctl, ioaddr->ctl_addr);
  360. ap->last_ctl = tf->ctl;
  361. }
  362. }
  363. static void sata_rcar_exec_command(struct ata_port *ap,
  364. const struct ata_taskfile *tf)
  365. {
  366. DPRINTK("ata%u: cmd 0x%X\n", ap->print_id, tf->command);
  367. iowrite32(tf->command, ap->ioaddr.command_addr);
  368. ata_sff_pause(ap);
  369. }
  370. static unsigned int sata_rcar_data_xfer(struct ata_queued_cmd *qc,
  371. unsigned char *buf,
  372. unsigned int buflen, int rw)
  373. {
  374. struct ata_port *ap = qc->dev->link->ap;
  375. void __iomem *data_addr = ap->ioaddr.data_addr;
  376. unsigned int words = buflen >> 1;
  377. /* Transfer multiple of 2 bytes */
  378. if (rw == READ)
  379. sata_rcar_ioread16_rep(data_addr, buf, words);
  380. else
  381. sata_rcar_iowrite16_rep(data_addr, buf, words);
  382. /* Transfer trailing byte, if any. */
  383. if (unlikely(buflen & 0x01)) {
  384. unsigned char pad[2] = { };
  385. /* Point buf to the tail of buffer */
  386. buf += buflen - 1;
  387. /*
  388. * Use io*16_rep() accessors here as well to avoid pointlessly
  389. * swapping bytes to and from on the big endian machines...
  390. */
  391. if (rw == READ) {
  392. sata_rcar_ioread16_rep(data_addr, pad, 1);
  393. *buf = pad[0];
  394. } else {
  395. pad[0] = *buf;
  396. sata_rcar_iowrite16_rep(data_addr, pad, 1);
  397. }
  398. words++;
  399. }
  400. return words << 1;
  401. }
  402. static void sata_rcar_drain_fifo(struct ata_queued_cmd *qc)
  403. {
  404. int count;
  405. struct ata_port *ap;
  406. /* We only need to flush incoming data when a command was running */
  407. if (qc == NULL || qc->dma_dir == DMA_TO_DEVICE)
  408. return;
  409. ap = qc->ap;
  410. /* Drain up to 64K of data before we give up this recovery method */
  411. for (count = 0; (ap->ops->sff_check_status(ap) & ATA_DRQ) &&
  412. count < 65536; count += 2)
  413. ioread32(ap->ioaddr.data_addr);
  414. /* Can become DEBUG later */
  415. if (count)
  416. ata_port_dbg(ap, "drained %d bytes to clear DRQ\n", count);
  417. }
  418. static int sata_rcar_scr_read(struct ata_link *link, unsigned int sc_reg,
  419. u32 *val)
  420. {
  421. if (sc_reg > SCR_ACTIVE)
  422. return -EINVAL;
  423. *val = ioread32(link->ap->ioaddr.scr_addr + (sc_reg << 2));
  424. return 0;
  425. }
  426. static int sata_rcar_scr_write(struct ata_link *link, unsigned int sc_reg,
  427. u32 val)
  428. {
  429. if (sc_reg > SCR_ACTIVE)
  430. return -EINVAL;
  431. iowrite32(val, link->ap->ioaddr.scr_addr + (sc_reg << 2));
  432. return 0;
  433. }
  434. static void sata_rcar_bmdma_fill_sg(struct ata_queued_cmd *qc)
  435. {
  436. struct ata_port *ap = qc->ap;
  437. struct ata_bmdma_prd *prd = ap->bmdma_prd;
  438. struct scatterlist *sg;
  439. unsigned int si;
  440. for_each_sg(qc->sg, sg, qc->n_elem, si) {
  441. u32 addr, sg_len;
  442. /*
  443. * Note: h/w doesn't support 64-bit, so we unconditionally
  444. * truncate dma_addr_t to u32.
  445. */
  446. addr = (u32)sg_dma_address(sg);
  447. sg_len = sg_dma_len(sg);
  448. prd[si].addr = cpu_to_le32(addr);
  449. prd[si].flags_len = cpu_to_le32(sg_len);
  450. VPRINTK("PRD[%u] = (0x%X, 0x%X)\n", si, addr, sg_len);
  451. }
  452. /* end-of-table flag */
  453. prd[si - 1].addr |= cpu_to_le32(SATA_RCAR_DTEND);
  454. }
  455. static void sata_rcar_qc_prep(struct ata_queued_cmd *qc)
  456. {
  457. if (!(qc->flags & ATA_QCFLAG_DMAMAP))
  458. return;
  459. sata_rcar_bmdma_fill_sg(qc);
  460. }
  461. static void sata_rcar_bmdma_setup(struct ata_queued_cmd *qc)
  462. {
  463. struct ata_port *ap = qc->ap;
  464. unsigned int rw = qc->tf.flags & ATA_TFLAG_WRITE;
  465. struct sata_rcar_priv *priv = ap->host->private_data;
  466. void __iomem *base = priv->base;
  467. u32 dmactl;
  468. /* load PRD table addr. */
  469. mb(); /* make sure PRD table writes are visible to controller */
  470. iowrite32(ap->bmdma_prd_dma, base + ATAPI_DTB_ADR_REG);
  471. /* specify data direction, triple-check start bit is clear */
  472. dmactl = ioread32(base + ATAPI_CONTROL1_REG);
  473. dmactl &= ~(ATAPI_CONTROL1_RW | ATAPI_CONTROL1_STOP);
  474. if (dmactl & ATAPI_CONTROL1_START) {
  475. dmactl &= ~ATAPI_CONTROL1_START;
  476. dmactl |= ATAPI_CONTROL1_STOP;
  477. }
  478. if (!rw)
  479. dmactl |= ATAPI_CONTROL1_RW;
  480. iowrite32(dmactl, base + ATAPI_CONTROL1_REG);
  481. /* issue r/w command */
  482. ap->ops->sff_exec_command(ap, &qc->tf);
  483. }
  484. static void sata_rcar_bmdma_start(struct ata_queued_cmd *qc)
  485. {
  486. struct ata_port *ap = qc->ap;
  487. struct sata_rcar_priv *priv = ap->host->private_data;
  488. void __iomem *base = priv->base;
  489. u32 dmactl;
  490. /* start host DMA transaction */
  491. dmactl = ioread32(base + ATAPI_CONTROL1_REG);
  492. dmactl &= ~ATAPI_CONTROL1_STOP;
  493. dmactl |= ATAPI_CONTROL1_START;
  494. iowrite32(dmactl, base + ATAPI_CONTROL1_REG);
  495. }
  496. static void sata_rcar_bmdma_stop(struct ata_queued_cmd *qc)
  497. {
  498. struct ata_port *ap = qc->ap;
  499. struct sata_rcar_priv *priv = ap->host->private_data;
  500. void __iomem *base = priv->base;
  501. u32 dmactl;
  502. /* force termination of DMA transfer if active */
  503. dmactl = ioread32(base + ATAPI_CONTROL1_REG);
  504. if (dmactl & ATAPI_CONTROL1_START) {
  505. dmactl &= ~ATAPI_CONTROL1_START;
  506. dmactl |= ATAPI_CONTROL1_STOP;
  507. iowrite32(dmactl, base + ATAPI_CONTROL1_REG);
  508. }
  509. /* one-PIO-cycle guaranteed wait, per spec, for HDMA1:0 transition */
  510. ata_sff_dma_pause(ap);
  511. }
  512. static u8 sata_rcar_bmdma_status(struct ata_port *ap)
  513. {
  514. struct sata_rcar_priv *priv = ap->host->private_data;
  515. u8 host_stat = 0;
  516. u32 status;
  517. status = ioread32(priv->base + ATAPI_STATUS_REG);
  518. if (status & ATAPI_STATUS_DEVINT)
  519. host_stat |= ATA_DMA_INTR;
  520. if (status & ATAPI_STATUS_ACT)
  521. host_stat |= ATA_DMA_ACTIVE;
  522. return host_stat;
  523. }
  524. static struct scsi_host_template sata_rcar_sht = {
  525. ATA_BASE_SHT(DRV_NAME),
  526. /*
  527. * This controller allows transfer chunks up to 512MB which cross 64KB
  528. * boundaries, therefore the DMA limits are more relaxed than standard
  529. * ATA SFF.
  530. */
  531. .sg_tablesize = ATA_MAX_PRD,
  532. .dma_boundary = SATA_RCAR_DMA_BOUNDARY,
  533. };
  534. static struct ata_port_operations sata_rcar_port_ops = {
  535. .inherits = &ata_bmdma_port_ops,
  536. .freeze = sata_rcar_freeze,
  537. .thaw = sata_rcar_thaw,
  538. .softreset = sata_rcar_softreset,
  539. .scr_read = sata_rcar_scr_read,
  540. .scr_write = sata_rcar_scr_write,
  541. .sff_dev_select = sata_rcar_dev_select,
  542. .sff_set_devctl = sata_rcar_set_devctl,
  543. .sff_check_status = sata_rcar_check_status,
  544. .sff_check_altstatus = sata_rcar_check_altstatus,
  545. .sff_tf_load = sata_rcar_tf_load,
  546. .sff_tf_read = sata_rcar_tf_read,
  547. .sff_exec_command = sata_rcar_exec_command,
  548. .sff_data_xfer = sata_rcar_data_xfer,
  549. .sff_drain_fifo = sata_rcar_drain_fifo,
  550. .qc_prep = sata_rcar_qc_prep,
  551. .bmdma_setup = sata_rcar_bmdma_setup,
  552. .bmdma_start = sata_rcar_bmdma_start,
  553. .bmdma_stop = sata_rcar_bmdma_stop,
  554. .bmdma_status = sata_rcar_bmdma_status,
  555. };
  556. static void sata_rcar_serr_interrupt(struct ata_port *ap)
  557. {
  558. struct sata_rcar_priv *priv = ap->host->private_data;
  559. struct ata_eh_info *ehi = &ap->link.eh_info;
  560. int freeze = 0;
  561. u32 serror;
  562. serror = ioread32(priv->base + SCRSERR_REG);
  563. if (!serror)
  564. return;
  565. DPRINTK("SError @host_intr: 0x%x\n", serror);
  566. /* first, analyze and record host port events */
  567. ata_ehi_clear_desc(ehi);
  568. if (serror & (SERR_DEV_XCHG | SERR_PHYRDY_CHG)) {
  569. /* Setup a soft-reset EH action */
  570. ata_ehi_hotplugged(ehi);
  571. ata_ehi_push_desc(ehi, "%s", "hotplug");
  572. freeze = serror & SERR_COMM_WAKE ? 0 : 1;
  573. }
  574. /* freeze or abort */
  575. if (freeze)
  576. ata_port_freeze(ap);
  577. else
  578. ata_port_abort(ap);
  579. }
  580. static void sata_rcar_ata_interrupt(struct ata_port *ap)
  581. {
  582. struct ata_queued_cmd *qc;
  583. int handled = 0;
  584. qc = ata_qc_from_tag(ap, ap->link.active_tag);
  585. if (qc)
  586. handled |= ata_bmdma_port_intr(ap, qc);
  587. /* be sure to clear ATA interrupt */
  588. if (!handled)
  589. sata_rcar_check_status(ap);
  590. }
  591. static irqreturn_t sata_rcar_interrupt(int irq, void *dev_instance)
  592. {
  593. struct ata_host *host = dev_instance;
  594. struct sata_rcar_priv *priv = host->private_data;
  595. void __iomem *base = priv->base;
  596. unsigned int handled = 0;
  597. struct ata_port *ap;
  598. u32 sataintstat;
  599. unsigned long flags;
  600. spin_lock_irqsave(&host->lock, flags);
  601. sataintstat = ioread32(base + SATAINTSTAT_REG);
  602. sataintstat &= SATA_RCAR_INT_MASK;
  603. if (!sataintstat)
  604. goto done;
  605. /* ack */
  606. iowrite32(~sataintstat & priv->sataint_mask, base + SATAINTSTAT_REG);
  607. ap = host->ports[0];
  608. if (sataintstat & SATAINTSTAT_ATA)
  609. sata_rcar_ata_interrupt(ap);
  610. if (sataintstat & SATAINTSTAT_SERR)
  611. sata_rcar_serr_interrupt(ap);
  612. handled = 1;
  613. done:
  614. spin_unlock_irqrestore(&host->lock, flags);
  615. return IRQ_RETVAL(handled);
  616. }
  617. static void sata_rcar_setup_port(struct ata_host *host)
  618. {
  619. struct ata_port *ap = host->ports[0];
  620. struct ata_ioports *ioaddr = &ap->ioaddr;
  621. struct sata_rcar_priv *priv = host->private_data;
  622. void __iomem *base = priv->base;
  623. ap->ops = &sata_rcar_port_ops;
  624. ap->pio_mask = ATA_PIO4;
  625. ap->udma_mask = ATA_UDMA6;
  626. ap->flags |= ATA_FLAG_SATA;
  627. if (priv->type == RCAR_R8A7790_ES1_SATA)
  628. ap->flags |= ATA_FLAG_NO_DIPM;
  629. ioaddr->cmd_addr = base + SDATA_REG;
  630. ioaddr->ctl_addr = base + SSDEVCON_REG;
  631. ioaddr->scr_addr = base + SCRSSTS_REG;
  632. ioaddr->altstatus_addr = ioaddr->ctl_addr;
  633. ioaddr->data_addr = ioaddr->cmd_addr + (ATA_REG_DATA << 2);
  634. ioaddr->error_addr = ioaddr->cmd_addr + (ATA_REG_ERR << 2);
  635. ioaddr->feature_addr = ioaddr->cmd_addr + (ATA_REG_FEATURE << 2);
  636. ioaddr->nsect_addr = ioaddr->cmd_addr + (ATA_REG_NSECT << 2);
  637. ioaddr->lbal_addr = ioaddr->cmd_addr + (ATA_REG_LBAL << 2);
  638. ioaddr->lbam_addr = ioaddr->cmd_addr + (ATA_REG_LBAM << 2);
  639. ioaddr->lbah_addr = ioaddr->cmd_addr + (ATA_REG_LBAH << 2);
  640. ioaddr->device_addr = ioaddr->cmd_addr + (ATA_REG_DEVICE << 2);
  641. ioaddr->status_addr = ioaddr->cmd_addr + (ATA_REG_STATUS << 2);
  642. ioaddr->command_addr = ioaddr->cmd_addr + (ATA_REG_CMD << 2);
  643. }
  644. static void sata_rcar_init_module(struct sata_rcar_priv *priv)
  645. {
  646. void __iomem *base = priv->base;
  647. u32 val;
  648. /* SATA-IP reset state */
  649. val = ioread32(base + ATAPI_CONTROL1_REG);
  650. val |= ATAPI_CONTROL1_RESET;
  651. iowrite32(val, base + ATAPI_CONTROL1_REG);
  652. /* ISM mode, PRD mode, DTEND flag at bit 0 */
  653. val = ioread32(base + ATAPI_CONTROL1_REG);
  654. val |= ATAPI_CONTROL1_ISM;
  655. val |= ATAPI_CONTROL1_DESE;
  656. val |= ATAPI_CONTROL1_DTA32M;
  657. iowrite32(val, base + ATAPI_CONTROL1_REG);
  658. /* Release the SATA-IP from the reset state */
  659. val = ioread32(base + ATAPI_CONTROL1_REG);
  660. val &= ~ATAPI_CONTROL1_RESET;
  661. iowrite32(val, base + ATAPI_CONTROL1_REG);
  662. /* ack and mask */
  663. iowrite32(0, base + SATAINTSTAT_REG);
  664. iowrite32(priv->sataint_mask, base + SATAINTMASK_REG);
  665. /* enable interrupts */
  666. iowrite32(ATAPI_INT_ENABLE_SATAINT, base + ATAPI_INT_ENABLE_REG);
  667. }
  668. static void sata_rcar_init_controller(struct ata_host *host)
  669. {
  670. struct sata_rcar_priv *priv = host->private_data;
  671. priv->sataint_mask = SATAINTMASK_ALL_GEN2;
  672. /* reset and setup phy */
  673. switch (priv->type) {
  674. case RCAR_GEN1_SATA:
  675. priv->sataint_mask = SATAINTMASK_ALL_GEN1;
  676. sata_rcar_gen1_phy_init(priv);
  677. break;
  678. case RCAR_GEN2_SATA:
  679. case RCAR_R8A7790_ES1_SATA:
  680. sata_rcar_gen2_phy_init(priv);
  681. break;
  682. case RCAR_GEN3_SATA:
  683. break;
  684. default:
  685. dev_warn(host->dev, "SATA phy is not initialized\n");
  686. break;
  687. }
  688. sata_rcar_init_module(priv);
  689. }
  690. static const struct of_device_id sata_rcar_match[] = {
  691. {
  692. /* Deprecated by "renesas,sata-r8a7779" */
  693. .compatible = "renesas,rcar-sata",
  694. .data = (void *)RCAR_GEN1_SATA,
  695. },
  696. {
  697. .compatible = "renesas,sata-r8a7779",
  698. .data = (void *)RCAR_GEN1_SATA,
  699. },
  700. {
  701. .compatible = "renesas,sata-r8a7790",
  702. .data = (void *)RCAR_GEN2_SATA
  703. },
  704. {
  705. .compatible = "renesas,sata-r8a7790-es1",
  706. .data = (void *)RCAR_R8A7790_ES1_SATA
  707. },
  708. {
  709. .compatible = "renesas,sata-r8a7791",
  710. .data = (void *)RCAR_GEN2_SATA
  711. },
  712. {
  713. .compatible = "renesas,sata-r8a7793",
  714. .data = (void *)RCAR_GEN2_SATA
  715. },
  716. {
  717. .compatible = "renesas,sata-r8a7795",
  718. .data = (void *)RCAR_GEN3_SATA
  719. },
  720. {
  721. .compatible = "renesas,rcar-gen2-sata",
  722. .data = (void *)RCAR_GEN2_SATA
  723. },
  724. {
  725. .compatible = "renesas,rcar-gen3-sata",
  726. .data = (void *)RCAR_GEN3_SATA
  727. },
  728. { },
  729. };
  730. MODULE_DEVICE_TABLE(of, sata_rcar_match);
  731. static int sata_rcar_probe(struct platform_device *pdev)
  732. {
  733. struct device *dev = &pdev->dev;
  734. struct ata_host *host;
  735. struct sata_rcar_priv *priv;
  736. struct resource *mem;
  737. int irq;
  738. int ret = 0;
  739. irq = platform_get_irq(pdev, 0);
  740. if (irq < 0)
  741. return irq;
  742. if (!irq)
  743. return -EINVAL;
  744. priv = devm_kzalloc(dev, sizeof(struct sata_rcar_priv), GFP_KERNEL);
  745. if (!priv)
  746. return -ENOMEM;
  747. priv->type = (enum sata_rcar_type)of_device_get_match_data(dev);
  748. pm_runtime_enable(dev);
  749. ret = pm_runtime_get_sync(dev);
  750. if (ret < 0)
  751. goto err_pm_disable;
  752. host = ata_host_alloc(dev, 1);
  753. if (!host) {
  754. dev_err(dev, "ata_host_alloc failed\n");
  755. ret = -ENOMEM;
  756. goto err_pm_put;
  757. }
  758. host->private_data = priv;
  759. mem = platform_get_resource(pdev, IORESOURCE_MEM, 0);
  760. priv->base = devm_ioremap_resource(dev, mem);
  761. if (IS_ERR(priv->base)) {
  762. ret = PTR_ERR(priv->base);
  763. goto err_pm_put;
  764. }
  765. /* setup port */
  766. sata_rcar_setup_port(host);
  767. /* initialize host controller */
  768. sata_rcar_init_controller(host);
  769. ret = ata_host_activate(host, irq, sata_rcar_interrupt, 0,
  770. &sata_rcar_sht);
  771. if (!ret)
  772. return 0;
  773. err_pm_put:
  774. pm_runtime_put(dev);
  775. err_pm_disable:
  776. pm_runtime_disable(dev);
  777. return ret;
  778. }
  779. static int sata_rcar_remove(struct platform_device *pdev)
  780. {
  781. struct ata_host *host = platform_get_drvdata(pdev);
  782. struct sata_rcar_priv *priv = host->private_data;
  783. void __iomem *base = priv->base;
  784. ata_host_detach(host);
  785. /* disable interrupts */
  786. iowrite32(0, base + ATAPI_INT_ENABLE_REG);
  787. /* ack and mask */
  788. iowrite32(0, base + SATAINTSTAT_REG);
  789. iowrite32(priv->sataint_mask, base + SATAINTMASK_REG);
  790. pm_runtime_put(&pdev->dev);
  791. pm_runtime_disable(&pdev->dev);
  792. return 0;
  793. }
  794. #ifdef CONFIG_PM_SLEEP
  795. static int sata_rcar_suspend(struct device *dev)
  796. {
  797. struct ata_host *host = dev_get_drvdata(dev);
  798. struct sata_rcar_priv *priv = host->private_data;
  799. void __iomem *base = priv->base;
  800. int ret;
  801. ret = ata_host_suspend(host, PMSG_SUSPEND);
  802. if (!ret) {
  803. /* disable interrupts */
  804. iowrite32(0, base + ATAPI_INT_ENABLE_REG);
  805. /* mask */
  806. iowrite32(priv->sataint_mask, base + SATAINTMASK_REG);
  807. pm_runtime_put(dev);
  808. }
  809. return ret;
  810. }
  811. static int sata_rcar_resume(struct device *dev)
  812. {
  813. struct ata_host *host = dev_get_drvdata(dev);
  814. struct sata_rcar_priv *priv = host->private_data;
  815. void __iomem *base = priv->base;
  816. int ret;
  817. ret = pm_runtime_get_sync(dev);
  818. if (ret < 0)
  819. return ret;
  820. if (priv->type == RCAR_GEN3_SATA) {
  821. sata_rcar_init_module(priv);
  822. } else {
  823. /* ack and mask */
  824. iowrite32(0, base + SATAINTSTAT_REG);
  825. iowrite32(priv->sataint_mask, base + SATAINTMASK_REG);
  826. /* enable interrupts */
  827. iowrite32(ATAPI_INT_ENABLE_SATAINT,
  828. base + ATAPI_INT_ENABLE_REG);
  829. }
  830. ata_host_resume(host);
  831. return 0;
  832. }
  833. static int sata_rcar_restore(struct device *dev)
  834. {
  835. struct ata_host *host = dev_get_drvdata(dev);
  836. int ret;
  837. ret = pm_runtime_get_sync(dev);
  838. if (ret < 0)
  839. return ret;
  840. sata_rcar_setup_port(host);
  841. /* initialize host controller */
  842. sata_rcar_init_controller(host);
  843. ata_host_resume(host);
  844. return 0;
  845. }
  846. static const struct dev_pm_ops sata_rcar_pm_ops = {
  847. .suspend = sata_rcar_suspend,
  848. .resume = sata_rcar_resume,
  849. .freeze = sata_rcar_suspend,
  850. .thaw = sata_rcar_resume,
  851. .poweroff = sata_rcar_suspend,
  852. .restore = sata_rcar_restore,
  853. };
  854. #endif
  855. static struct platform_driver sata_rcar_driver = {
  856. .probe = sata_rcar_probe,
  857. .remove = sata_rcar_remove,
  858. .driver = {
  859. .name = DRV_NAME,
  860. .of_match_table = sata_rcar_match,
  861. #ifdef CONFIG_PM_SLEEP
  862. .pm = &sata_rcar_pm_ops,
  863. #endif
  864. },
  865. };
  866. module_platform_driver(sata_rcar_driver);
  867. MODULE_LICENSE("GPL");
  868. MODULE_AUTHOR("Vladimir Barinov");
  869. MODULE_DESCRIPTION("Renesas R-Car SATA controller low level driver");