sata_rcar.c 24 KB

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