sata_rcar.c 27 KB

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