sata_rcar.c 27 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037
  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_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_device *dev,
  368. unsigned char *buf,
  369. unsigned int buflen, int rw)
  370. {
  371. struct ata_port *ap = 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 void sata_rcar_qc_prep(struct ata_queued_cmd *qc)
  453. {
  454. if (!(qc->flags & ATA_QCFLAG_DMAMAP))
  455. return;
  456. sata_rcar_bmdma_fill_sg(qc);
  457. }
  458. static void sata_rcar_bmdma_setup(struct ata_queued_cmd *qc)
  459. {
  460. struct ata_port *ap = qc->ap;
  461. unsigned int rw = qc->tf.flags & ATA_TFLAG_WRITE;
  462. struct sata_rcar_priv *priv = ap->host->private_data;
  463. void __iomem *base = priv->base;
  464. u32 dmactl;
  465. /* load PRD table addr. */
  466. mb(); /* make sure PRD table writes are visible to controller */
  467. iowrite32(ap->bmdma_prd_dma, base + ATAPI_DTB_ADR_REG);
  468. /* specify data direction, triple-check start bit is clear */
  469. dmactl = ioread32(base + ATAPI_CONTROL1_REG);
  470. dmactl &= ~(ATAPI_CONTROL1_RW | ATAPI_CONTROL1_STOP);
  471. if (dmactl & ATAPI_CONTROL1_START) {
  472. dmactl &= ~ATAPI_CONTROL1_START;
  473. dmactl |= ATAPI_CONTROL1_STOP;
  474. }
  475. if (!rw)
  476. dmactl |= ATAPI_CONTROL1_RW;
  477. iowrite32(dmactl, base + ATAPI_CONTROL1_REG);
  478. /* issue r/w command */
  479. ap->ops->sff_exec_command(ap, &qc->tf);
  480. }
  481. static void sata_rcar_bmdma_start(struct ata_queued_cmd *qc)
  482. {
  483. struct ata_port *ap = qc->ap;
  484. struct sata_rcar_priv *priv = ap->host->private_data;
  485. void __iomem *base = priv->base;
  486. u32 dmactl;
  487. /* start host DMA transaction */
  488. dmactl = ioread32(base + ATAPI_CONTROL1_REG);
  489. dmactl &= ~ATAPI_CONTROL1_STOP;
  490. dmactl |= ATAPI_CONTROL1_START;
  491. iowrite32(dmactl, base + ATAPI_CONTROL1_REG);
  492. }
  493. static void sata_rcar_bmdma_stop(struct ata_queued_cmd *qc)
  494. {
  495. struct ata_port *ap = qc->ap;
  496. struct sata_rcar_priv *priv = ap->host->private_data;
  497. void __iomem *base = priv->base;
  498. u32 dmactl;
  499. /* force termination of DMA transfer if active */
  500. dmactl = ioread32(base + ATAPI_CONTROL1_REG);
  501. if (dmactl & ATAPI_CONTROL1_START) {
  502. dmactl &= ~ATAPI_CONTROL1_START;
  503. dmactl |= ATAPI_CONTROL1_STOP;
  504. iowrite32(dmactl, base + ATAPI_CONTROL1_REG);
  505. }
  506. /* one-PIO-cycle guaranteed wait, per spec, for HDMA1:0 transition */
  507. ata_sff_dma_pause(ap);
  508. }
  509. static u8 sata_rcar_bmdma_status(struct ata_port *ap)
  510. {
  511. struct sata_rcar_priv *priv = ap->host->private_data;
  512. u8 host_stat = 0;
  513. u32 status;
  514. status = ioread32(priv->base + ATAPI_STATUS_REG);
  515. if (status & ATAPI_STATUS_DEVINT)
  516. host_stat |= ATA_DMA_INTR;
  517. if (status & ATAPI_STATUS_ACT)
  518. host_stat |= ATA_DMA_ACTIVE;
  519. return host_stat;
  520. }
  521. static struct scsi_host_template sata_rcar_sht = {
  522. ATA_BASE_SHT(DRV_NAME),
  523. /*
  524. * This controller allows transfer chunks up to 512MB which cross 64KB
  525. * boundaries, therefore the DMA limits are more relaxed than standard
  526. * ATA SFF.
  527. */
  528. .sg_tablesize = ATA_MAX_PRD,
  529. .dma_boundary = SATA_RCAR_DMA_BOUNDARY,
  530. };
  531. static struct ata_port_operations sata_rcar_port_ops = {
  532. .inherits = &ata_bmdma_port_ops,
  533. .freeze = sata_rcar_freeze,
  534. .thaw = sata_rcar_thaw,
  535. .softreset = sata_rcar_softreset,
  536. .scr_read = sata_rcar_scr_read,
  537. .scr_write = sata_rcar_scr_write,
  538. .sff_dev_select = sata_rcar_dev_select,
  539. .sff_set_devctl = sata_rcar_set_devctl,
  540. .sff_check_status = sata_rcar_check_status,
  541. .sff_check_altstatus = sata_rcar_check_altstatus,
  542. .sff_tf_load = sata_rcar_tf_load,
  543. .sff_tf_read = sata_rcar_tf_read,
  544. .sff_exec_command = sata_rcar_exec_command,
  545. .sff_data_xfer = sata_rcar_data_xfer,
  546. .sff_drain_fifo = sata_rcar_drain_fifo,
  547. .qc_prep = sata_rcar_qc_prep,
  548. .bmdma_setup = sata_rcar_bmdma_setup,
  549. .bmdma_start = sata_rcar_bmdma_start,
  550. .bmdma_stop = sata_rcar_bmdma_stop,
  551. .bmdma_status = sata_rcar_bmdma_status,
  552. };
  553. static void sata_rcar_serr_interrupt(struct ata_port *ap)
  554. {
  555. struct sata_rcar_priv *priv = ap->host->private_data;
  556. struct ata_eh_info *ehi = &ap->link.eh_info;
  557. int freeze = 0;
  558. u32 serror;
  559. serror = ioread32(priv->base + SCRSERR_REG);
  560. if (!serror)
  561. return;
  562. DPRINTK("SError @host_intr: 0x%x\n", serror);
  563. /* first, analyze and record host port events */
  564. ata_ehi_clear_desc(ehi);
  565. if (serror & (SERR_DEV_XCHG | SERR_PHYRDY_CHG)) {
  566. /* Setup a soft-reset EH action */
  567. ata_ehi_hotplugged(ehi);
  568. ata_ehi_push_desc(ehi, "%s", "hotplug");
  569. freeze = serror & SERR_COMM_WAKE ? 0 : 1;
  570. }
  571. /* freeze or abort */
  572. if (freeze)
  573. ata_port_freeze(ap);
  574. else
  575. ata_port_abort(ap);
  576. }
  577. static void sata_rcar_ata_interrupt(struct ata_port *ap)
  578. {
  579. struct ata_queued_cmd *qc;
  580. int handled = 0;
  581. qc = ata_qc_from_tag(ap, ap->link.active_tag);
  582. if (qc)
  583. handled |= ata_bmdma_port_intr(ap, qc);
  584. /* be sure to clear ATA interrupt */
  585. if (!handled)
  586. sata_rcar_check_status(ap);
  587. }
  588. static irqreturn_t sata_rcar_interrupt(int irq, void *dev_instance)
  589. {
  590. struct ata_host *host = dev_instance;
  591. struct sata_rcar_priv *priv = host->private_data;
  592. void __iomem *base = priv->base;
  593. unsigned int handled = 0;
  594. struct ata_port *ap;
  595. u32 sataintstat;
  596. unsigned long flags;
  597. spin_lock_irqsave(&host->lock, flags);
  598. sataintstat = ioread32(base + SATAINTSTAT_REG);
  599. sataintstat &= SATA_RCAR_INT_MASK;
  600. if (!sataintstat)
  601. goto done;
  602. /* ack */
  603. iowrite32(~sataintstat & 0x7ff, base + SATAINTSTAT_REG);
  604. ap = host->ports[0];
  605. if (sataintstat & SATAINTSTAT_ATA)
  606. sata_rcar_ata_interrupt(ap);
  607. if (sataintstat & SATAINTSTAT_SERR)
  608. sata_rcar_serr_interrupt(ap);
  609. handled = 1;
  610. done:
  611. spin_unlock_irqrestore(&host->lock, flags);
  612. return IRQ_RETVAL(handled);
  613. }
  614. static void sata_rcar_setup_port(struct ata_host *host)
  615. {
  616. struct ata_port *ap = host->ports[0];
  617. struct ata_ioports *ioaddr = &ap->ioaddr;
  618. struct sata_rcar_priv *priv = host->private_data;
  619. void __iomem *base = priv->base;
  620. ap->ops = &sata_rcar_port_ops;
  621. ap->pio_mask = ATA_PIO4;
  622. ap->udma_mask = ATA_UDMA6;
  623. ap->flags |= ATA_FLAG_SATA;
  624. if (priv->type == RCAR_R8A7790_ES1_SATA)
  625. ap->flags |= ATA_FLAG_NO_DIPM;
  626. ioaddr->cmd_addr = base + SDATA_REG;
  627. ioaddr->ctl_addr = base + SSDEVCON_REG;
  628. ioaddr->scr_addr = base + SCRSSTS_REG;
  629. ioaddr->altstatus_addr = ioaddr->ctl_addr;
  630. ioaddr->data_addr = ioaddr->cmd_addr + (ATA_REG_DATA << 2);
  631. ioaddr->error_addr = ioaddr->cmd_addr + (ATA_REG_ERR << 2);
  632. ioaddr->feature_addr = ioaddr->cmd_addr + (ATA_REG_FEATURE << 2);
  633. ioaddr->nsect_addr = ioaddr->cmd_addr + (ATA_REG_NSECT << 2);
  634. ioaddr->lbal_addr = ioaddr->cmd_addr + (ATA_REG_LBAL << 2);
  635. ioaddr->lbam_addr = ioaddr->cmd_addr + (ATA_REG_LBAM << 2);
  636. ioaddr->lbah_addr = ioaddr->cmd_addr + (ATA_REG_LBAH << 2);
  637. ioaddr->device_addr = ioaddr->cmd_addr + (ATA_REG_DEVICE << 2);
  638. ioaddr->status_addr = ioaddr->cmd_addr + (ATA_REG_STATUS << 2);
  639. ioaddr->command_addr = ioaddr->cmd_addr + (ATA_REG_CMD << 2);
  640. }
  641. static void sata_rcar_init_controller(struct ata_host *host)
  642. {
  643. struct sata_rcar_priv *priv = host->private_data;
  644. void __iomem *base = priv->base;
  645. u32 val;
  646. /* reset and setup phy */
  647. switch (priv->type) {
  648. case RCAR_GEN1_SATA:
  649. sata_rcar_gen1_phy_init(priv);
  650. break;
  651. case RCAR_GEN2_SATA:
  652. case RCAR_R8A7790_ES1_SATA:
  653. sata_rcar_gen2_phy_init(priv);
  654. break;
  655. default:
  656. dev_warn(host->dev, "SATA phy is not initialized\n");
  657. break;
  658. }
  659. /* SATA-IP reset state */
  660. val = ioread32(base + ATAPI_CONTROL1_REG);
  661. val |= ATAPI_CONTROL1_RESET;
  662. iowrite32(val, base + ATAPI_CONTROL1_REG);
  663. /* ISM mode, PRD mode, DTEND flag at bit 0 */
  664. val = ioread32(base + ATAPI_CONTROL1_REG);
  665. val |= ATAPI_CONTROL1_ISM;
  666. val |= ATAPI_CONTROL1_DESE;
  667. val |= ATAPI_CONTROL1_DTA32M;
  668. iowrite32(val, base + ATAPI_CONTROL1_REG);
  669. /* Release the SATA-IP from the reset state */
  670. val = ioread32(base + ATAPI_CONTROL1_REG);
  671. val &= ~ATAPI_CONTROL1_RESET;
  672. iowrite32(val, base + ATAPI_CONTROL1_REG);
  673. /* ack and mask */
  674. iowrite32(0, base + SATAINTSTAT_REG);
  675. iowrite32(0x7ff, base + SATAINTMASK_REG);
  676. /* enable interrupts */
  677. iowrite32(ATAPI_INT_ENABLE_SATAINT, base + ATAPI_INT_ENABLE_REG);
  678. }
  679. static struct of_device_id sata_rcar_match[] = {
  680. {
  681. /* Deprecated by "renesas,sata-r8a7779" */
  682. .compatible = "renesas,rcar-sata",
  683. .data = (void *)RCAR_GEN1_SATA,
  684. },
  685. {
  686. .compatible = "renesas,sata-r8a7779",
  687. .data = (void *)RCAR_GEN1_SATA,
  688. },
  689. {
  690. .compatible = "renesas,sata-r8a7790",
  691. .data = (void *)RCAR_GEN2_SATA
  692. },
  693. {
  694. .compatible = "renesas,sata-r8a7790-es1",
  695. .data = (void *)RCAR_R8A7790_ES1_SATA
  696. },
  697. {
  698. .compatible = "renesas,sata-r8a7791",
  699. .data = (void *)RCAR_GEN2_SATA
  700. },
  701. {
  702. .compatible = "renesas,sata-r8a7793",
  703. .data = (void *)RCAR_GEN2_SATA
  704. },
  705. { },
  706. };
  707. MODULE_DEVICE_TABLE(of, sata_rcar_match);
  708. static const struct platform_device_id sata_rcar_id_table[] = {
  709. { "sata_rcar", RCAR_GEN1_SATA }, /* Deprecated by "sata-r8a7779" */
  710. { "sata-r8a7779", RCAR_GEN1_SATA },
  711. { },
  712. };
  713. MODULE_DEVICE_TABLE(platform, sata_rcar_id_table);
  714. static int sata_rcar_probe(struct platform_device *pdev)
  715. {
  716. const struct of_device_id *of_id;
  717. struct ata_host *host;
  718. struct sata_rcar_priv *priv;
  719. struct resource *mem;
  720. int irq;
  721. int ret = 0;
  722. irq = platform_get_irq(pdev, 0);
  723. if (irq < 0)
  724. return irq;
  725. if (!irq)
  726. return -EINVAL;
  727. priv = devm_kzalloc(&pdev->dev, sizeof(struct sata_rcar_priv),
  728. GFP_KERNEL);
  729. if (!priv)
  730. return -ENOMEM;
  731. of_id = of_match_device(sata_rcar_match, &pdev->dev);
  732. if (of_id)
  733. priv->type = (enum sata_rcar_type)of_id->data;
  734. else
  735. priv->type = platform_get_device_id(pdev)->driver_data;
  736. priv->clk = devm_clk_get(&pdev->dev, NULL);
  737. if (IS_ERR(priv->clk)) {
  738. dev_err(&pdev->dev, "failed to get access to sata clock\n");
  739. return PTR_ERR(priv->clk);
  740. }
  741. clk_prepare_enable(priv->clk);
  742. host = ata_host_alloc(&pdev->dev, 1);
  743. if (!host) {
  744. dev_err(&pdev->dev, "ata_host_alloc failed\n");
  745. ret = -ENOMEM;
  746. goto cleanup;
  747. }
  748. host->private_data = priv;
  749. mem = platform_get_resource(pdev, IORESOURCE_MEM, 0);
  750. priv->base = devm_ioremap_resource(&pdev->dev, mem);
  751. if (IS_ERR(priv->base)) {
  752. ret = PTR_ERR(priv->base);
  753. goto cleanup;
  754. }
  755. /* setup port */
  756. sata_rcar_setup_port(host);
  757. /* initialize host controller */
  758. sata_rcar_init_controller(host);
  759. ret = ata_host_activate(host, irq, sata_rcar_interrupt, 0,
  760. &sata_rcar_sht);
  761. if (!ret)
  762. return 0;
  763. cleanup:
  764. clk_disable_unprepare(priv->clk);
  765. return ret;
  766. }
  767. static int sata_rcar_remove(struct platform_device *pdev)
  768. {
  769. struct ata_host *host = platform_get_drvdata(pdev);
  770. struct sata_rcar_priv *priv = host->private_data;
  771. void __iomem *base = priv->base;
  772. ata_host_detach(host);
  773. /* disable interrupts */
  774. iowrite32(0, base + ATAPI_INT_ENABLE_REG);
  775. /* ack and mask */
  776. iowrite32(0, base + SATAINTSTAT_REG);
  777. iowrite32(0x7ff, base + SATAINTMASK_REG);
  778. clk_disable_unprepare(priv->clk);
  779. return 0;
  780. }
  781. #ifdef CONFIG_PM_SLEEP
  782. static int sata_rcar_suspend(struct device *dev)
  783. {
  784. struct ata_host *host = dev_get_drvdata(dev);
  785. struct sata_rcar_priv *priv = host->private_data;
  786. void __iomem *base = priv->base;
  787. int ret;
  788. ret = ata_host_suspend(host, PMSG_SUSPEND);
  789. if (!ret) {
  790. /* disable interrupts */
  791. iowrite32(0, base + ATAPI_INT_ENABLE_REG);
  792. /* mask */
  793. iowrite32(0x7ff, base + SATAINTMASK_REG);
  794. clk_disable_unprepare(priv->clk);
  795. }
  796. return ret;
  797. }
  798. static int sata_rcar_resume(struct device *dev)
  799. {
  800. struct ata_host *host = dev_get_drvdata(dev);
  801. struct sata_rcar_priv *priv = host->private_data;
  802. void __iomem *base = priv->base;
  803. clk_prepare_enable(priv->clk);
  804. /* ack and mask */
  805. iowrite32(0, base + SATAINTSTAT_REG);
  806. iowrite32(0x7ff, base + SATAINTMASK_REG);
  807. /* enable interrupts */
  808. iowrite32(ATAPI_INT_ENABLE_SATAINT, base + ATAPI_INT_ENABLE_REG);
  809. ata_host_resume(host);
  810. return 0;
  811. }
  812. static int sata_rcar_restore(struct device *dev)
  813. {
  814. struct ata_host *host = dev_get_drvdata(dev);
  815. struct sata_rcar_priv *priv = host->private_data;
  816. clk_prepare_enable(priv->clk);
  817. sata_rcar_setup_port(host);
  818. /* initialize host controller */
  819. sata_rcar_init_controller(host);
  820. ata_host_resume(host);
  821. return 0;
  822. }
  823. static const struct dev_pm_ops sata_rcar_pm_ops = {
  824. .suspend = sata_rcar_suspend,
  825. .resume = sata_rcar_resume,
  826. .freeze = sata_rcar_suspend,
  827. .thaw = sata_rcar_resume,
  828. .poweroff = sata_rcar_suspend,
  829. .restore = sata_rcar_restore,
  830. };
  831. #endif
  832. static struct platform_driver sata_rcar_driver = {
  833. .probe = sata_rcar_probe,
  834. .remove = sata_rcar_remove,
  835. .id_table = sata_rcar_id_table,
  836. .driver = {
  837. .name = DRV_NAME,
  838. .of_match_table = sata_rcar_match,
  839. #ifdef CONFIG_PM_SLEEP
  840. .pm = &sata_rcar_pm_ops,
  841. #endif
  842. },
  843. };
  844. module_platform_driver(sata_rcar_driver);
  845. MODULE_LICENSE("GPL");
  846. MODULE_AUTHOR("Vladimir Barinov");
  847. MODULE_DESCRIPTION("Renesas R-Car SATA controller low level driver");