sata_rcar.c 27 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052
  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 0x1FFFFFFFUL
  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_R8A7790_ES1_SATA,
  123. };
  124. struct sata_rcar_priv {
  125. void __iomem *base;
  126. struct clk *clk;
  127. enum sata_rcar_type type;
  128. };
  129. static void sata_rcar_gen1_phy_preinit(struct sata_rcar_priv *priv)
  130. {
  131. void __iomem *base = priv->base;
  132. /* idle state */
  133. iowrite32(0, base + SATAPHYADDR_REG);
  134. /* reset */
  135. iowrite32(SATAPHYRESET_PHYRST, base + SATAPHYRESET_REG);
  136. udelay(10);
  137. /* deassert reset */
  138. iowrite32(0, base + SATAPHYRESET_REG);
  139. }
  140. static void sata_rcar_gen1_phy_write(struct sata_rcar_priv *priv, u16 reg,
  141. u32 val, int group)
  142. {
  143. void __iomem *base = priv->base;
  144. int timeout;
  145. /* deassert reset */
  146. iowrite32(0, base + SATAPHYRESET_REG);
  147. /* lane 1 */
  148. iowrite32(SATAPHYACCEN_PHYLANE, base + SATAPHYACCEN_REG);
  149. /* write phy register value */
  150. iowrite32(val, base + SATAPHYWDATA_REG);
  151. /* set register group */
  152. if (group)
  153. reg |= SATAPHYADDR_PHYRATEMODE;
  154. /* write command */
  155. iowrite32(SATAPHYADDR_PHYCMD_WRITE | reg, base + SATAPHYADDR_REG);
  156. /* wait for ack */
  157. for (timeout = 0; timeout < 100; timeout++) {
  158. val = ioread32(base + SATAPHYACK_REG);
  159. if (val & SATAPHYACK_PHYACK)
  160. break;
  161. }
  162. if (timeout >= 100)
  163. pr_err("%s timeout\n", __func__);
  164. /* idle state */
  165. iowrite32(0, base + SATAPHYADDR_REG);
  166. }
  167. static void sata_rcar_gen1_phy_init(struct sata_rcar_priv *priv)
  168. {
  169. sata_rcar_gen1_phy_preinit(priv);
  170. sata_rcar_gen1_phy_write(priv, SATAPCTLR1_REG, 0x00200188, 0);
  171. sata_rcar_gen1_phy_write(priv, SATAPCTLR1_REG, 0x00200188, 1);
  172. sata_rcar_gen1_phy_write(priv, SATAPCTLR3_REG, 0x0000A061, 0);
  173. sata_rcar_gen1_phy_write(priv, SATAPCTLR2_REG, 0x20000000, 0);
  174. sata_rcar_gen1_phy_write(priv, SATAPCTLR2_REG, 0x20000000, 1);
  175. sata_rcar_gen1_phy_write(priv, SATAPCTLR4_REG, 0x28E80000, 0);
  176. }
  177. static void sata_rcar_gen2_phy_init(struct sata_rcar_priv *priv)
  178. {
  179. void __iomem *base = priv->base;
  180. iowrite32(RCAR_GEN2_PHY_CTL1, base + RCAR_GEN2_PHY_CTL1_REG);
  181. iowrite32(RCAR_GEN2_PHY_CTL2, base + RCAR_GEN2_PHY_CTL2_REG);
  182. iowrite32(RCAR_GEN2_PHY_CTL3, base + RCAR_GEN2_PHY_CTL3_REG);
  183. iowrite32(RCAR_GEN2_PHY_CTL4, base + RCAR_GEN2_PHY_CTL4_REG);
  184. iowrite32(RCAR_GEN2_PHY_CTL5 | RCAR_GEN2_PHY_CTL5_DC |
  185. RCAR_GEN2_PHY_CTL5_TR, base + RCAR_GEN2_PHY_CTL5_REG);
  186. }
  187. static void sata_rcar_freeze(struct ata_port *ap)
  188. {
  189. struct sata_rcar_priv *priv = ap->host->private_data;
  190. /* mask */
  191. iowrite32(0x7ff, priv->base + SATAINTMASK_REG);
  192. ata_sff_freeze(ap);
  193. }
  194. static void sata_rcar_thaw(struct ata_port *ap)
  195. {
  196. struct sata_rcar_priv *priv = ap->host->private_data;
  197. void __iomem *base = priv->base;
  198. /* ack */
  199. iowrite32(~(u32)SATA_RCAR_INT_MASK, base + SATAINTSTAT_REG);
  200. ata_sff_thaw(ap);
  201. /* unmask */
  202. iowrite32(0x7ff & ~SATA_RCAR_INT_MASK, base + SATAINTMASK_REG);
  203. }
  204. static void sata_rcar_ioread16_rep(void __iomem *reg, void *buffer, int count)
  205. {
  206. u16 *ptr = buffer;
  207. while (count--) {
  208. u16 data = ioread32(reg);
  209. *ptr++ = data;
  210. }
  211. }
  212. static void sata_rcar_iowrite16_rep(void __iomem *reg, void *buffer, int count)
  213. {
  214. const u16 *ptr = buffer;
  215. while (count--)
  216. iowrite32(*ptr++, reg);
  217. }
  218. static u8 sata_rcar_check_status(struct ata_port *ap)
  219. {
  220. return ioread32(ap->ioaddr.status_addr);
  221. }
  222. static u8 sata_rcar_check_altstatus(struct ata_port *ap)
  223. {
  224. return ioread32(ap->ioaddr.altstatus_addr);
  225. }
  226. static void sata_rcar_set_devctl(struct ata_port *ap, u8 ctl)
  227. {
  228. iowrite32(ctl, ap->ioaddr.ctl_addr);
  229. }
  230. static void sata_rcar_dev_select(struct ata_port *ap, unsigned int device)
  231. {
  232. iowrite32(ATA_DEVICE_OBS, ap->ioaddr.device_addr);
  233. ata_sff_pause(ap); /* needed; also flushes, for mmio */
  234. }
  235. static unsigned int sata_rcar_ata_devchk(struct ata_port *ap,
  236. unsigned int device)
  237. {
  238. struct ata_ioports *ioaddr = &ap->ioaddr;
  239. u8 nsect, lbal;
  240. sata_rcar_dev_select(ap, device);
  241. iowrite32(0x55, ioaddr->nsect_addr);
  242. iowrite32(0xaa, ioaddr->lbal_addr);
  243. iowrite32(0xaa, ioaddr->nsect_addr);
  244. iowrite32(0x55, ioaddr->lbal_addr);
  245. iowrite32(0x55, ioaddr->nsect_addr);
  246. iowrite32(0xaa, ioaddr->lbal_addr);
  247. nsect = ioread32(ioaddr->nsect_addr);
  248. lbal = ioread32(ioaddr->lbal_addr);
  249. if (nsect == 0x55 && lbal == 0xaa)
  250. return 1; /* found a device */
  251. return 0; /* nothing found */
  252. }
  253. static int sata_rcar_wait_after_reset(struct ata_link *link,
  254. unsigned long deadline)
  255. {
  256. struct ata_port *ap = link->ap;
  257. ata_msleep(ap, ATA_WAIT_AFTER_RESET);
  258. return ata_sff_wait_ready(link, deadline);
  259. }
  260. static int sata_rcar_bus_softreset(struct ata_port *ap, unsigned long deadline)
  261. {
  262. struct ata_ioports *ioaddr = &ap->ioaddr;
  263. DPRINTK("ata%u: bus reset via SRST\n", ap->print_id);
  264. /* software reset. causes dev0 to be selected */
  265. iowrite32(ap->ctl, ioaddr->ctl_addr);
  266. udelay(20);
  267. iowrite32(ap->ctl | ATA_SRST, ioaddr->ctl_addr);
  268. udelay(20);
  269. iowrite32(ap->ctl, ioaddr->ctl_addr);
  270. ap->last_ctl = ap->ctl;
  271. /* wait the port to become ready */
  272. return sata_rcar_wait_after_reset(&ap->link, deadline);
  273. }
  274. static int sata_rcar_softreset(struct ata_link *link, unsigned int *classes,
  275. unsigned long deadline)
  276. {
  277. struct ata_port *ap = link->ap;
  278. unsigned int devmask = 0;
  279. int rc;
  280. u8 err;
  281. /* determine if device 0 is present */
  282. if (sata_rcar_ata_devchk(ap, 0))
  283. devmask |= 1 << 0;
  284. /* issue bus reset */
  285. DPRINTK("about to softreset, devmask=%x\n", devmask);
  286. rc = sata_rcar_bus_softreset(ap, deadline);
  287. /* if link is occupied, -ENODEV too is an error */
  288. if (rc && (rc != -ENODEV || sata_scr_valid(link))) {
  289. ata_link_err(link, "SRST failed (errno=%d)\n", rc);
  290. return rc;
  291. }
  292. /* determine by signature whether we have ATA or ATAPI devices */
  293. classes[0] = ata_sff_dev_classify(&link->device[0], devmask, &err);
  294. DPRINTK("classes[0]=%u\n", classes[0]);
  295. return 0;
  296. }
  297. static void sata_rcar_tf_load(struct ata_port *ap,
  298. const struct ata_taskfile *tf)
  299. {
  300. struct ata_ioports *ioaddr = &ap->ioaddr;
  301. unsigned int is_addr = tf->flags & ATA_TFLAG_ISADDR;
  302. if (tf->ctl != ap->last_ctl) {
  303. iowrite32(tf->ctl, ioaddr->ctl_addr);
  304. ap->last_ctl = tf->ctl;
  305. ata_wait_idle(ap);
  306. }
  307. if (is_addr && (tf->flags & ATA_TFLAG_LBA48)) {
  308. iowrite32(tf->hob_feature, ioaddr->feature_addr);
  309. iowrite32(tf->hob_nsect, ioaddr->nsect_addr);
  310. iowrite32(tf->hob_lbal, ioaddr->lbal_addr);
  311. iowrite32(tf->hob_lbam, ioaddr->lbam_addr);
  312. iowrite32(tf->hob_lbah, ioaddr->lbah_addr);
  313. VPRINTK("hob: feat 0x%X nsect 0x%X, lba 0x%X 0x%X 0x%X\n",
  314. tf->hob_feature,
  315. tf->hob_nsect,
  316. tf->hob_lbal,
  317. tf->hob_lbam,
  318. tf->hob_lbah);
  319. }
  320. if (is_addr) {
  321. iowrite32(tf->feature, ioaddr->feature_addr);
  322. iowrite32(tf->nsect, ioaddr->nsect_addr);
  323. iowrite32(tf->lbal, ioaddr->lbal_addr);
  324. iowrite32(tf->lbam, ioaddr->lbam_addr);
  325. iowrite32(tf->lbah, ioaddr->lbah_addr);
  326. VPRINTK("feat 0x%X nsect 0x%X lba 0x%X 0x%X 0x%X\n",
  327. tf->feature,
  328. tf->nsect,
  329. tf->lbal,
  330. tf->lbam,
  331. tf->lbah);
  332. }
  333. if (tf->flags & ATA_TFLAG_DEVICE) {
  334. iowrite32(tf->device, ioaddr->device_addr);
  335. VPRINTK("device 0x%X\n", tf->device);
  336. }
  337. ata_wait_idle(ap);
  338. }
  339. static void sata_rcar_tf_read(struct ata_port *ap, struct ata_taskfile *tf)
  340. {
  341. struct ata_ioports *ioaddr = &ap->ioaddr;
  342. tf->command = sata_rcar_check_status(ap);
  343. tf->feature = ioread32(ioaddr->error_addr);
  344. tf->nsect = ioread32(ioaddr->nsect_addr);
  345. tf->lbal = ioread32(ioaddr->lbal_addr);
  346. tf->lbam = ioread32(ioaddr->lbam_addr);
  347. tf->lbah = ioread32(ioaddr->lbah_addr);
  348. tf->device = ioread32(ioaddr->device_addr);
  349. if (tf->flags & ATA_TFLAG_LBA48) {
  350. iowrite32(tf->ctl | ATA_HOB, ioaddr->ctl_addr);
  351. tf->hob_feature = ioread32(ioaddr->error_addr);
  352. tf->hob_nsect = ioread32(ioaddr->nsect_addr);
  353. tf->hob_lbal = ioread32(ioaddr->lbal_addr);
  354. tf->hob_lbam = ioread32(ioaddr->lbam_addr);
  355. tf->hob_lbah = ioread32(ioaddr->lbah_addr);
  356. iowrite32(tf->ctl, ioaddr->ctl_addr);
  357. ap->last_ctl = tf->ctl;
  358. }
  359. }
  360. static void sata_rcar_exec_command(struct ata_port *ap,
  361. const struct ata_taskfile *tf)
  362. {
  363. DPRINTK("ata%u: cmd 0x%X\n", ap->print_id, tf->command);
  364. iowrite32(tf->command, ap->ioaddr.command_addr);
  365. ata_sff_pause(ap);
  366. }
  367. static unsigned int sata_rcar_data_xfer(struct ata_queued_cmd *qc,
  368. unsigned char *buf,
  369. unsigned int buflen, int rw)
  370. {
  371. struct ata_port *ap = qc->dev->link->ap;
  372. void __iomem *data_addr = ap->ioaddr.data_addr;
  373. unsigned int words = buflen >> 1;
  374. /* Transfer multiple of 2 bytes */
  375. if (rw == READ)
  376. sata_rcar_ioread16_rep(data_addr, buf, words);
  377. else
  378. sata_rcar_iowrite16_rep(data_addr, buf, words);
  379. /* Transfer trailing byte, if any. */
  380. if (unlikely(buflen & 0x01)) {
  381. unsigned char pad[2] = { };
  382. /* Point buf to the tail of buffer */
  383. buf += buflen - 1;
  384. /*
  385. * Use io*16_rep() accessors here as well to avoid pointlessly
  386. * swapping bytes to and from on the big endian machines...
  387. */
  388. if (rw == READ) {
  389. sata_rcar_ioread16_rep(data_addr, pad, 1);
  390. *buf = pad[0];
  391. } else {
  392. pad[0] = *buf;
  393. sata_rcar_iowrite16_rep(data_addr, pad, 1);
  394. }
  395. words++;
  396. }
  397. return words << 1;
  398. }
  399. static void sata_rcar_drain_fifo(struct ata_queued_cmd *qc)
  400. {
  401. int count;
  402. struct ata_port *ap;
  403. /* We only need to flush incoming data when a command was running */
  404. if (qc == NULL || qc->dma_dir == DMA_TO_DEVICE)
  405. return;
  406. ap = qc->ap;
  407. /* Drain up to 64K of data before we give up this recovery method */
  408. for (count = 0; (ap->ops->sff_check_status(ap) & ATA_DRQ) &&
  409. count < 65536; count += 2)
  410. ioread32(ap->ioaddr.data_addr);
  411. /* Can become DEBUG later */
  412. if (count)
  413. ata_port_dbg(ap, "drained %d bytes to clear DRQ\n", count);
  414. }
  415. static int sata_rcar_scr_read(struct ata_link *link, unsigned int sc_reg,
  416. u32 *val)
  417. {
  418. if (sc_reg > SCR_ACTIVE)
  419. return -EINVAL;
  420. *val = ioread32(link->ap->ioaddr.scr_addr + (sc_reg << 2));
  421. return 0;
  422. }
  423. static int sata_rcar_scr_write(struct ata_link *link, unsigned int sc_reg,
  424. u32 val)
  425. {
  426. if (sc_reg > SCR_ACTIVE)
  427. return -EINVAL;
  428. iowrite32(val, link->ap->ioaddr.scr_addr + (sc_reg << 2));
  429. return 0;
  430. }
  431. static void sata_rcar_bmdma_fill_sg(struct ata_queued_cmd *qc)
  432. {
  433. struct ata_port *ap = qc->ap;
  434. struct ata_bmdma_prd *prd = ap->bmdma_prd;
  435. struct scatterlist *sg;
  436. unsigned int si;
  437. for_each_sg(qc->sg, sg, qc->n_elem, si) {
  438. u32 addr, sg_len;
  439. /*
  440. * Note: h/w doesn't support 64-bit, so we unconditionally
  441. * truncate dma_addr_t to u32.
  442. */
  443. addr = (u32)sg_dma_address(sg);
  444. sg_len = sg_dma_len(sg);
  445. prd[si].addr = cpu_to_le32(addr);
  446. prd[si].flags_len = cpu_to_le32(sg_len);
  447. VPRINTK("PRD[%u] = (0x%X, 0x%X)\n", si, addr, sg_len);
  448. }
  449. /* end-of-table flag */
  450. prd[si - 1].addr |= cpu_to_le32(SATA_RCAR_DTEND);
  451. }
  452. static enum ata_completion_errors sata_rcar_qc_prep(struct ata_queued_cmd *qc)
  453. {
  454. if (!(qc->flags & ATA_QCFLAG_DMAMAP))
  455. return AC_ERR_OK;
  456. sata_rcar_bmdma_fill_sg(qc);
  457. return AC_ERR_OK;
  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_controller(struct ata_host *host)
  643. {
  644. struct sata_rcar_priv *priv = host->private_data;
  645. void __iomem *base = priv->base;
  646. u32 val;
  647. /* reset and setup phy */
  648. switch (priv->type) {
  649. case RCAR_GEN1_SATA:
  650. sata_rcar_gen1_phy_init(priv);
  651. break;
  652. case RCAR_GEN2_SATA:
  653. case RCAR_R8A7790_ES1_SATA:
  654. sata_rcar_gen2_phy_init(priv);
  655. break;
  656. default:
  657. dev_warn(host->dev, "SATA phy is not initialized\n");
  658. break;
  659. }
  660. /* SATA-IP reset state */
  661. val = ioread32(base + ATAPI_CONTROL1_REG);
  662. val |= ATAPI_CONTROL1_RESET;
  663. iowrite32(val, base + ATAPI_CONTROL1_REG);
  664. /* ISM mode, PRD mode, DTEND flag at bit 0 */
  665. val = ioread32(base + ATAPI_CONTROL1_REG);
  666. val |= ATAPI_CONTROL1_ISM;
  667. val |= ATAPI_CONTROL1_DESE;
  668. val |= ATAPI_CONTROL1_DTA32M;
  669. iowrite32(val, base + ATAPI_CONTROL1_REG);
  670. /* Release the SATA-IP from the reset state */
  671. val = ioread32(base + ATAPI_CONTROL1_REG);
  672. val &= ~ATAPI_CONTROL1_RESET;
  673. iowrite32(val, base + ATAPI_CONTROL1_REG);
  674. /* ack and mask */
  675. iowrite32(0, base + SATAINTSTAT_REG);
  676. iowrite32(0x7ff, base + SATAINTMASK_REG);
  677. /* enable interrupts */
  678. iowrite32(ATAPI_INT_ENABLE_SATAINT, base + ATAPI_INT_ENABLE_REG);
  679. }
  680. static const struct of_device_id sata_rcar_match[] = {
  681. {
  682. /* Deprecated by "renesas,sata-r8a7779" */
  683. .compatible = "renesas,rcar-sata",
  684. .data = (void *)RCAR_GEN1_SATA,
  685. },
  686. {
  687. .compatible = "renesas,sata-r8a7779",
  688. .data = (void *)RCAR_GEN1_SATA,
  689. },
  690. {
  691. .compatible = "renesas,sata-r8a7790",
  692. .data = (void *)RCAR_GEN2_SATA
  693. },
  694. {
  695. .compatible = "renesas,sata-r8a7790-es1",
  696. .data = (void *)RCAR_R8A7790_ES1_SATA
  697. },
  698. {
  699. .compatible = "renesas,sata-r8a7791",
  700. .data = (void *)RCAR_GEN2_SATA
  701. },
  702. {
  703. .compatible = "renesas,sata-r8a7793",
  704. .data = (void *)RCAR_GEN2_SATA
  705. },
  706. {
  707. .compatible = "renesas,sata-r8a7795",
  708. .data = (void *)RCAR_GEN2_SATA
  709. },
  710. {
  711. .compatible = "renesas,rcar-gen2-sata",
  712. .data = (void *)RCAR_GEN2_SATA
  713. },
  714. {
  715. .compatible = "renesas,rcar-gen3-sata",
  716. .data = (void *)RCAR_GEN2_SATA
  717. },
  718. { },
  719. };
  720. MODULE_DEVICE_TABLE(of, sata_rcar_match);
  721. static int sata_rcar_probe(struct platform_device *pdev)
  722. {
  723. const struct of_device_id *of_id;
  724. struct ata_host *host;
  725. struct sata_rcar_priv *priv;
  726. struct resource *mem;
  727. int irq;
  728. int ret = 0;
  729. irq = platform_get_irq(pdev, 0);
  730. if (irq < 0)
  731. return irq;
  732. if (!irq)
  733. return -EINVAL;
  734. priv = devm_kzalloc(&pdev->dev, sizeof(struct sata_rcar_priv),
  735. GFP_KERNEL);
  736. if (!priv)
  737. return -ENOMEM;
  738. of_id = of_match_device(sata_rcar_match, &pdev->dev);
  739. if (!of_id)
  740. return -ENODEV;
  741. priv->type = (enum sata_rcar_type)of_id->data;
  742. priv->clk = devm_clk_get(&pdev->dev, NULL);
  743. if (IS_ERR(priv->clk)) {
  744. dev_err(&pdev->dev, "failed to get access to sata clock\n");
  745. return PTR_ERR(priv->clk);
  746. }
  747. ret = clk_prepare_enable(priv->clk);
  748. if (ret)
  749. return ret;
  750. host = ata_host_alloc(&pdev->dev, 1);
  751. if (!host) {
  752. dev_err(&pdev->dev, "ata_host_alloc failed\n");
  753. ret = -ENOMEM;
  754. goto cleanup;
  755. }
  756. host->private_data = priv;
  757. mem = platform_get_resource(pdev, IORESOURCE_MEM, 0);
  758. priv->base = devm_ioremap_resource(&pdev->dev, mem);
  759. if (IS_ERR(priv->base)) {
  760. ret = PTR_ERR(priv->base);
  761. goto cleanup;
  762. }
  763. /* setup port */
  764. sata_rcar_setup_port(host);
  765. /* initialize host controller */
  766. sata_rcar_init_controller(host);
  767. ret = ata_host_activate(host, irq, sata_rcar_interrupt, 0,
  768. &sata_rcar_sht);
  769. if (!ret)
  770. return 0;
  771. cleanup:
  772. clk_disable_unprepare(priv->clk);
  773. return ret;
  774. }
  775. static int sata_rcar_remove(struct platform_device *pdev)
  776. {
  777. struct ata_host *host = platform_get_drvdata(pdev);
  778. struct sata_rcar_priv *priv = host->private_data;
  779. void __iomem *base = priv->base;
  780. ata_host_detach(host);
  781. /* disable interrupts */
  782. iowrite32(0, base + ATAPI_INT_ENABLE_REG);
  783. /* ack and mask */
  784. iowrite32(0, base + SATAINTSTAT_REG);
  785. iowrite32(0x7ff, base + SATAINTMASK_REG);
  786. clk_disable_unprepare(priv->clk);
  787. return 0;
  788. }
  789. #ifdef CONFIG_PM_SLEEP
  790. static int sata_rcar_suspend(struct device *dev)
  791. {
  792. struct ata_host *host = dev_get_drvdata(dev);
  793. struct sata_rcar_priv *priv = host->private_data;
  794. void __iomem *base = priv->base;
  795. int ret;
  796. ret = ata_host_suspend(host, PMSG_SUSPEND);
  797. if (!ret) {
  798. /* disable interrupts */
  799. iowrite32(0, base + ATAPI_INT_ENABLE_REG);
  800. /* mask */
  801. iowrite32(0x7ff, base + SATAINTMASK_REG);
  802. clk_disable_unprepare(priv->clk);
  803. }
  804. return ret;
  805. }
  806. static int sata_rcar_resume(struct device *dev)
  807. {
  808. struct ata_host *host = dev_get_drvdata(dev);
  809. struct sata_rcar_priv *priv = host->private_data;
  810. void __iomem *base = priv->base;
  811. int ret;
  812. ret = clk_prepare_enable(priv->clk);
  813. if (ret)
  814. return ret;
  815. /* ack and mask */
  816. iowrite32(0, base + SATAINTSTAT_REG);
  817. iowrite32(0x7ff, base + SATAINTMASK_REG);
  818. /* enable interrupts */
  819. iowrite32(ATAPI_INT_ENABLE_SATAINT, base + ATAPI_INT_ENABLE_REG);
  820. ata_host_resume(host);
  821. return 0;
  822. }
  823. static int sata_rcar_restore(struct device *dev)
  824. {
  825. struct ata_host *host = dev_get_drvdata(dev);
  826. struct sata_rcar_priv *priv = host->private_data;
  827. int ret;
  828. ret = clk_prepare_enable(priv->clk);
  829. if (ret)
  830. return ret;
  831. sata_rcar_setup_port(host);
  832. /* initialize host controller */
  833. sata_rcar_init_controller(host);
  834. ata_host_resume(host);
  835. return 0;
  836. }
  837. static const struct dev_pm_ops sata_rcar_pm_ops = {
  838. .suspend = sata_rcar_suspend,
  839. .resume = sata_rcar_resume,
  840. .freeze = sata_rcar_suspend,
  841. .thaw = sata_rcar_resume,
  842. .poweroff = sata_rcar_suspend,
  843. .restore = sata_rcar_restore,
  844. };
  845. #endif
  846. static struct platform_driver sata_rcar_driver = {
  847. .probe = sata_rcar_probe,
  848. .remove = sata_rcar_remove,
  849. .driver = {
  850. .name = DRV_NAME,
  851. .of_match_table = sata_rcar_match,
  852. #ifdef CONFIG_PM_SLEEP
  853. .pm = &sata_rcar_pm_ops,
  854. #endif
  855. },
  856. };
  857. module_platform_driver(sata_rcar_driver);
  858. MODULE_LICENSE("GPL");
  859. MODULE_AUTHOR("Vladimir Barinov");
  860. MODULE_DESCRIPTION("Renesas R-Car SATA controller low level driver");