sata_dwc_460ex.c 36 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335
  1. /*
  2. * drivers/ata/sata_dwc_460ex.c
  3. *
  4. * Synopsys DesignWare Cores (DWC) SATA host driver
  5. *
  6. * Author: Mark Miesfeld <mmiesfeld@amcc.com>
  7. *
  8. * Ported from 2.6.19.2 to 2.6.25/26 by Stefan Roese <sr@denx.de>
  9. * Copyright 2008 DENX Software Engineering
  10. *
  11. * Based on versions provided by AMCC and Synopsys which are:
  12. * Copyright 2006 Applied Micro Circuits Corporation
  13. * COPYRIGHT (C) 2005 SYNOPSYS, INC. ALL RIGHTS RESERVED
  14. *
  15. * This program is free software; you can redistribute it and/or modify it
  16. * under the terms of the GNU General Public License as published by the
  17. * Free Software Foundation; either version 2 of the License, or (at your
  18. * option) any later version.
  19. */
  20. #ifdef CONFIG_SATA_DWC_DEBUG
  21. #define DEBUG
  22. #endif
  23. #ifdef CONFIG_SATA_DWC_VDEBUG
  24. #define VERBOSE_DEBUG
  25. #define DEBUG_NCQ
  26. #endif
  27. #include <linux/kernel.h>
  28. #include <linux/module.h>
  29. #include <linux/device.h>
  30. #include <linux/dmaengine.h>
  31. #include <linux/of_address.h>
  32. #include <linux/of_irq.h>
  33. #include <linux/of_platform.h>
  34. #include <linux/platform_device.h>
  35. #include <linux/phy/phy.h>
  36. #include <linux/libata.h>
  37. #include <linux/slab.h>
  38. #include "libata.h"
  39. #include <scsi/scsi_host.h>
  40. #include <scsi/scsi_cmnd.h>
  41. /* These two are defined in "libata.h" */
  42. #undef DRV_NAME
  43. #undef DRV_VERSION
  44. #define DRV_NAME "sata-dwc"
  45. #define DRV_VERSION "1.3"
  46. #define sata_dwc_writel(a, v) writel_relaxed(v, a)
  47. #define sata_dwc_readl(a) readl_relaxed(a)
  48. #ifndef NO_IRQ
  49. #define NO_IRQ 0
  50. #endif
  51. #define AHB_DMA_BRST_DFLT 64 /* 16 data items burst length */
  52. enum {
  53. SATA_DWC_MAX_PORTS = 1,
  54. SATA_DWC_SCR_OFFSET = 0x24,
  55. SATA_DWC_REG_OFFSET = 0x64,
  56. };
  57. /* DWC SATA Registers */
  58. struct sata_dwc_regs {
  59. u32 fptagr; /* 1st party DMA tag */
  60. u32 fpbor; /* 1st party DMA buffer offset */
  61. u32 fptcr; /* 1st party DMA Xfr count */
  62. u32 dmacr; /* DMA Control */
  63. u32 dbtsr; /* DMA Burst Transac size */
  64. u32 intpr; /* Interrupt Pending */
  65. u32 intmr; /* Interrupt Mask */
  66. u32 errmr; /* Error Mask */
  67. u32 llcr; /* Link Layer Control */
  68. u32 phycr; /* PHY Control */
  69. u32 physr; /* PHY Status */
  70. u32 rxbistpd; /* Recvd BIST pattern def register */
  71. u32 rxbistpd1; /* Recvd BIST data dword1 */
  72. u32 rxbistpd2; /* Recvd BIST pattern data dword2 */
  73. u32 txbistpd; /* Trans BIST pattern def register */
  74. u32 txbistpd1; /* Trans BIST data dword1 */
  75. u32 txbistpd2; /* Trans BIST data dword2 */
  76. u32 bistcr; /* BIST Control Register */
  77. u32 bistfctr; /* BIST FIS Count Register */
  78. u32 bistsr; /* BIST Status Register */
  79. u32 bistdecr; /* BIST Dword Error count register */
  80. u32 res[15]; /* Reserved locations */
  81. u32 testr; /* Test Register */
  82. u32 versionr; /* Version Register */
  83. u32 idr; /* ID Register */
  84. u32 unimpl[192]; /* Unimplemented */
  85. u32 dmadr[256]; /* FIFO Locations in DMA Mode */
  86. };
  87. enum {
  88. SCR_SCONTROL_DET_ENABLE = 0x00000001,
  89. SCR_SSTATUS_DET_PRESENT = 0x00000001,
  90. SCR_SERROR_DIAG_X = 0x04000000,
  91. /* DWC SATA Register Operations */
  92. SATA_DWC_TXFIFO_DEPTH = 0x01FF,
  93. SATA_DWC_RXFIFO_DEPTH = 0x01FF,
  94. SATA_DWC_DMACR_TMOD_TXCHEN = 0x00000004,
  95. SATA_DWC_DMACR_TXCHEN = (0x00000001 | SATA_DWC_DMACR_TMOD_TXCHEN),
  96. SATA_DWC_DMACR_RXCHEN = (0x00000002 | SATA_DWC_DMACR_TMOD_TXCHEN),
  97. SATA_DWC_DMACR_TXRXCH_CLEAR = SATA_DWC_DMACR_TMOD_TXCHEN,
  98. SATA_DWC_INTPR_DMAT = 0x00000001,
  99. SATA_DWC_INTPR_NEWFP = 0x00000002,
  100. SATA_DWC_INTPR_PMABRT = 0x00000004,
  101. SATA_DWC_INTPR_ERR = 0x00000008,
  102. SATA_DWC_INTPR_NEWBIST = 0x00000010,
  103. SATA_DWC_INTPR_IPF = 0x10000000,
  104. SATA_DWC_INTMR_DMATM = 0x00000001,
  105. SATA_DWC_INTMR_NEWFPM = 0x00000002,
  106. SATA_DWC_INTMR_PMABRTM = 0x00000004,
  107. SATA_DWC_INTMR_ERRM = 0x00000008,
  108. SATA_DWC_INTMR_NEWBISTM = 0x00000010,
  109. SATA_DWC_LLCR_SCRAMEN = 0x00000001,
  110. SATA_DWC_LLCR_DESCRAMEN = 0x00000002,
  111. SATA_DWC_LLCR_RPDEN = 0x00000004,
  112. /* This is all error bits, zero's are reserved fields. */
  113. SATA_DWC_SERROR_ERR_BITS = 0x0FFF0F03
  114. };
  115. #define SATA_DWC_SCR0_SPD_GET(v) (((v) >> 4) & 0x0000000F)
  116. #define SATA_DWC_DMACR_TX_CLEAR(v) (((v) & ~SATA_DWC_DMACR_TXCHEN) |\
  117. SATA_DWC_DMACR_TMOD_TXCHEN)
  118. #define SATA_DWC_DMACR_RX_CLEAR(v) (((v) & ~SATA_DWC_DMACR_RXCHEN) |\
  119. SATA_DWC_DMACR_TMOD_TXCHEN)
  120. #define SATA_DWC_DBTSR_MWR(size) (((size)/4) & SATA_DWC_TXFIFO_DEPTH)
  121. #define SATA_DWC_DBTSR_MRD(size) ((((size)/4) & SATA_DWC_RXFIFO_DEPTH)\
  122. << 16)
  123. struct sata_dwc_device {
  124. struct device *dev; /* generic device struct */
  125. struct ata_probe_ent *pe; /* ptr to probe-ent */
  126. struct ata_host *host;
  127. struct sata_dwc_regs __iomem *sata_dwc_regs; /* DW SATA specific */
  128. u32 sactive_issued;
  129. u32 sactive_queued;
  130. struct phy *phy;
  131. phys_addr_t dmadr;
  132. #ifdef CONFIG_SATA_DWC_OLD_DMA
  133. struct dw_dma_chip *dma;
  134. #endif
  135. };
  136. #define SATA_DWC_QCMD_MAX 32
  137. struct sata_dwc_device_port {
  138. struct sata_dwc_device *hsdev;
  139. int cmd_issued[SATA_DWC_QCMD_MAX];
  140. int dma_pending[SATA_DWC_QCMD_MAX];
  141. /* DMA info */
  142. struct dma_chan *chan;
  143. struct dma_async_tx_descriptor *desc[SATA_DWC_QCMD_MAX];
  144. u32 dma_interrupt_count;
  145. };
  146. /*
  147. * Commonly used DWC SATA driver macros
  148. */
  149. #define HSDEV_FROM_HOST(host) ((struct sata_dwc_device *)(host)->private_data)
  150. #define HSDEV_FROM_AP(ap) ((struct sata_dwc_device *)(ap)->host->private_data)
  151. #define HSDEVP_FROM_AP(ap) ((struct sata_dwc_device_port *)(ap)->private_data)
  152. #define HSDEV_FROM_QC(qc) ((struct sata_dwc_device *)(qc)->ap->host->private_data)
  153. #define HSDEV_FROM_HSDEVP(p) ((struct sata_dwc_device *)(p)->hsdev)
  154. enum {
  155. SATA_DWC_CMD_ISSUED_NOT = 0,
  156. SATA_DWC_CMD_ISSUED_PEND = 1,
  157. SATA_DWC_CMD_ISSUED_EXEC = 2,
  158. SATA_DWC_CMD_ISSUED_NODATA = 3,
  159. SATA_DWC_DMA_PENDING_NONE = 0,
  160. SATA_DWC_DMA_PENDING_TX = 1,
  161. SATA_DWC_DMA_PENDING_RX = 2,
  162. };
  163. /*
  164. * Prototypes
  165. */
  166. static void sata_dwc_bmdma_start_by_tag(struct ata_queued_cmd *qc, u8 tag);
  167. static int sata_dwc_qc_complete(struct ata_port *ap, struct ata_queued_cmd *qc,
  168. u32 check_status);
  169. static void sata_dwc_dma_xfer_complete(struct ata_port *ap, u32 check_status);
  170. static void sata_dwc_port_stop(struct ata_port *ap);
  171. static void sata_dwc_clear_dmacr(struct sata_dwc_device_port *hsdevp, u8 tag);
  172. #ifdef CONFIG_SATA_DWC_OLD_DMA
  173. #include <linux/platform_data/dma-dw.h>
  174. #include <linux/dma/dw.h>
  175. static struct dw_dma_slave sata_dwc_dma_dws = {
  176. .src_id = 0,
  177. .dst_id = 0,
  178. .m_master = 1,
  179. .p_master = 0,
  180. };
  181. static bool sata_dwc_dma_filter(struct dma_chan *chan, void *param)
  182. {
  183. struct dw_dma_slave *dws = &sata_dwc_dma_dws;
  184. if (dws->dma_dev != chan->device->dev)
  185. return false;
  186. chan->private = dws;
  187. return true;
  188. }
  189. static int sata_dwc_dma_get_channel_old(struct sata_dwc_device_port *hsdevp)
  190. {
  191. struct sata_dwc_device *hsdev = hsdevp->hsdev;
  192. struct dw_dma_slave *dws = &sata_dwc_dma_dws;
  193. dma_cap_mask_t mask;
  194. dws->dma_dev = hsdev->dev;
  195. dma_cap_zero(mask);
  196. dma_cap_set(DMA_SLAVE, mask);
  197. /* Acquire DMA channel */
  198. hsdevp->chan = dma_request_channel(mask, sata_dwc_dma_filter, hsdevp);
  199. if (!hsdevp->chan) {
  200. dev_err(hsdev->dev, "%s: dma channel unavailable\n",
  201. __func__);
  202. return -EAGAIN;
  203. }
  204. return 0;
  205. }
  206. static int sata_dwc_dma_init_old(struct platform_device *pdev,
  207. struct sata_dwc_device *hsdev)
  208. {
  209. struct device_node *np = pdev->dev.of_node;
  210. struct resource *res;
  211. hsdev->dma = devm_kzalloc(&pdev->dev, sizeof(*hsdev->dma), GFP_KERNEL);
  212. if (!hsdev->dma)
  213. return -ENOMEM;
  214. hsdev->dma->dev = &pdev->dev;
  215. /* Get SATA DMA interrupt number */
  216. hsdev->dma->irq = irq_of_parse_and_map(np, 1);
  217. if (hsdev->dma->irq == NO_IRQ) {
  218. dev_err(&pdev->dev, "no SATA DMA irq\n");
  219. return -ENODEV;
  220. }
  221. /* Get physical SATA DMA register base address */
  222. res = platform_get_resource(pdev, IORESOURCE_MEM, 1);
  223. hsdev->dma->regs = devm_ioremap_resource(&pdev->dev, res);
  224. if (IS_ERR(hsdev->dma->regs))
  225. return PTR_ERR(hsdev->dma->regs);
  226. /* Initialize AHB DMAC */
  227. return dw_dma_probe(hsdev->dma);
  228. }
  229. static void sata_dwc_dma_exit_old(struct sata_dwc_device *hsdev)
  230. {
  231. if (!hsdev->dma)
  232. return;
  233. dw_dma_remove(hsdev->dma);
  234. }
  235. #endif
  236. static const char *get_prot_descript(u8 protocol)
  237. {
  238. switch (protocol) {
  239. case ATA_PROT_NODATA:
  240. return "ATA no data";
  241. case ATA_PROT_PIO:
  242. return "ATA PIO";
  243. case ATA_PROT_DMA:
  244. return "ATA DMA";
  245. case ATA_PROT_NCQ:
  246. return "ATA NCQ";
  247. case ATA_PROT_NCQ_NODATA:
  248. return "ATA NCQ no data";
  249. case ATAPI_PROT_NODATA:
  250. return "ATAPI no data";
  251. case ATAPI_PROT_PIO:
  252. return "ATAPI PIO";
  253. case ATAPI_PROT_DMA:
  254. return "ATAPI DMA";
  255. default:
  256. return "unknown";
  257. }
  258. }
  259. static const char *get_dma_dir_descript(int dma_dir)
  260. {
  261. switch ((enum dma_data_direction)dma_dir) {
  262. case DMA_BIDIRECTIONAL:
  263. return "bidirectional";
  264. case DMA_TO_DEVICE:
  265. return "to device";
  266. case DMA_FROM_DEVICE:
  267. return "from device";
  268. default:
  269. return "none";
  270. }
  271. }
  272. static void sata_dwc_tf_dump(struct ata_port *ap, struct ata_taskfile *tf)
  273. {
  274. dev_vdbg(ap->dev,
  275. "taskfile cmd: 0x%02x protocol: %s flags: 0x%lx device: %x\n",
  276. tf->command, get_prot_descript(tf->protocol), tf->flags,
  277. tf->device);
  278. dev_vdbg(ap->dev,
  279. "feature: 0x%02x nsect: 0x%x lbal: 0x%x lbam: 0x%x lbah: 0x%x\n",
  280. tf->feature, tf->nsect, tf->lbal, tf->lbam, tf->lbah);
  281. dev_vdbg(ap->dev,
  282. "hob_feature: 0x%02x hob_nsect: 0x%x hob_lbal: 0x%x hob_lbam: 0x%x hob_lbah: 0x%x\n",
  283. tf->hob_feature, tf->hob_nsect, tf->hob_lbal, tf->hob_lbam,
  284. tf->hob_lbah);
  285. }
  286. static void dma_dwc_xfer_done(void *hsdev_instance)
  287. {
  288. unsigned long flags;
  289. struct sata_dwc_device *hsdev = hsdev_instance;
  290. struct ata_host *host = (struct ata_host *)hsdev->host;
  291. struct ata_port *ap;
  292. struct sata_dwc_device_port *hsdevp;
  293. u8 tag = 0;
  294. unsigned int port = 0;
  295. spin_lock_irqsave(&host->lock, flags);
  296. ap = host->ports[port];
  297. hsdevp = HSDEVP_FROM_AP(ap);
  298. tag = ap->link.active_tag;
  299. /*
  300. * Each DMA command produces 2 interrupts. Only
  301. * complete the command after both interrupts have been
  302. * seen. (See sata_dwc_isr())
  303. */
  304. hsdevp->dma_interrupt_count++;
  305. sata_dwc_clear_dmacr(hsdevp, tag);
  306. if (hsdevp->dma_pending[tag] == SATA_DWC_DMA_PENDING_NONE) {
  307. dev_err(ap->dev, "DMA not pending tag=0x%02x pending=%d\n",
  308. tag, hsdevp->dma_pending[tag]);
  309. }
  310. if ((hsdevp->dma_interrupt_count % 2) == 0)
  311. sata_dwc_dma_xfer_complete(ap, 1);
  312. spin_unlock_irqrestore(&host->lock, flags);
  313. }
  314. static struct dma_async_tx_descriptor *dma_dwc_xfer_setup(struct ata_queued_cmd *qc)
  315. {
  316. struct ata_port *ap = qc->ap;
  317. struct sata_dwc_device_port *hsdevp = HSDEVP_FROM_AP(ap);
  318. struct sata_dwc_device *hsdev = HSDEV_FROM_AP(ap);
  319. struct dma_slave_config sconf;
  320. struct dma_async_tx_descriptor *desc;
  321. if (qc->dma_dir == DMA_DEV_TO_MEM) {
  322. sconf.src_addr = hsdev->dmadr;
  323. sconf.device_fc = false;
  324. } else { /* DMA_MEM_TO_DEV */
  325. sconf.dst_addr = hsdev->dmadr;
  326. sconf.device_fc = false;
  327. }
  328. sconf.direction = qc->dma_dir;
  329. sconf.src_maxburst = AHB_DMA_BRST_DFLT / 4; /* in items */
  330. sconf.dst_maxburst = AHB_DMA_BRST_DFLT / 4; /* in items */
  331. sconf.src_addr_width = DMA_SLAVE_BUSWIDTH_4_BYTES;
  332. sconf.dst_addr_width = DMA_SLAVE_BUSWIDTH_4_BYTES;
  333. dmaengine_slave_config(hsdevp->chan, &sconf);
  334. /* Convert SG list to linked list of items (LLIs) for AHB DMA */
  335. desc = dmaengine_prep_slave_sg(hsdevp->chan, qc->sg, qc->n_elem,
  336. qc->dma_dir,
  337. DMA_PREP_INTERRUPT | DMA_CTRL_ACK);
  338. if (!desc)
  339. return NULL;
  340. desc->callback = dma_dwc_xfer_done;
  341. desc->callback_param = hsdev;
  342. dev_dbg(hsdev->dev, "%s sg: 0x%p, count: %d addr: %pa\n", __func__,
  343. qc->sg, qc->n_elem, &hsdev->dmadr);
  344. return desc;
  345. }
  346. static int sata_dwc_scr_read(struct ata_link *link, unsigned int scr, u32 *val)
  347. {
  348. if (scr > SCR_NOTIFICATION) {
  349. dev_err(link->ap->dev, "%s: Incorrect SCR offset 0x%02x\n",
  350. __func__, scr);
  351. return -EINVAL;
  352. }
  353. *val = sata_dwc_readl(link->ap->ioaddr.scr_addr + (scr * 4));
  354. dev_dbg(link->ap->dev, "%s: id=%d reg=%d val=0x%08x\n", __func__,
  355. link->ap->print_id, scr, *val);
  356. return 0;
  357. }
  358. static int sata_dwc_scr_write(struct ata_link *link, unsigned int scr, u32 val)
  359. {
  360. dev_dbg(link->ap->dev, "%s: id=%d reg=%d val=0x%08x\n", __func__,
  361. link->ap->print_id, scr, val);
  362. if (scr > SCR_NOTIFICATION) {
  363. dev_err(link->ap->dev, "%s: Incorrect SCR offset 0x%02x\n",
  364. __func__, scr);
  365. return -EINVAL;
  366. }
  367. sata_dwc_writel(link->ap->ioaddr.scr_addr + (scr * 4), val);
  368. return 0;
  369. }
  370. static void clear_serror(struct ata_port *ap)
  371. {
  372. u32 val;
  373. sata_dwc_scr_read(&ap->link, SCR_ERROR, &val);
  374. sata_dwc_scr_write(&ap->link, SCR_ERROR, val);
  375. }
  376. static void clear_interrupt_bit(struct sata_dwc_device *hsdev, u32 bit)
  377. {
  378. sata_dwc_writel(&hsdev->sata_dwc_regs->intpr,
  379. sata_dwc_readl(&hsdev->sata_dwc_regs->intpr));
  380. }
  381. static u32 qcmd_tag_to_mask(u8 tag)
  382. {
  383. return 0x00000001 << (tag & 0x1f);
  384. }
  385. /* See ahci.c */
  386. static void sata_dwc_error_intr(struct ata_port *ap,
  387. struct sata_dwc_device *hsdev, uint intpr)
  388. {
  389. struct sata_dwc_device_port *hsdevp = HSDEVP_FROM_AP(ap);
  390. struct ata_eh_info *ehi = &ap->link.eh_info;
  391. unsigned int err_mask = 0, action = 0;
  392. struct ata_queued_cmd *qc;
  393. u32 serror;
  394. u8 status, tag;
  395. ata_ehi_clear_desc(ehi);
  396. sata_dwc_scr_read(&ap->link, SCR_ERROR, &serror);
  397. status = ap->ops->sff_check_status(ap);
  398. tag = ap->link.active_tag;
  399. dev_err(ap->dev,
  400. "%s SCR_ERROR=0x%08x intpr=0x%08x status=0x%08x dma_intp=%d pending=%d issued=%d",
  401. __func__, serror, intpr, status, hsdevp->dma_interrupt_count,
  402. hsdevp->dma_pending[tag], hsdevp->cmd_issued[tag]);
  403. /* Clear error register and interrupt bit */
  404. clear_serror(ap);
  405. clear_interrupt_bit(hsdev, SATA_DWC_INTPR_ERR);
  406. /* This is the only error happening now. TODO check for exact error */
  407. err_mask |= AC_ERR_HOST_BUS;
  408. action |= ATA_EH_RESET;
  409. /* Pass this on to EH */
  410. ehi->serror |= serror;
  411. ehi->action |= action;
  412. qc = ata_qc_from_tag(ap, tag);
  413. if (qc)
  414. qc->err_mask |= err_mask;
  415. else
  416. ehi->err_mask |= err_mask;
  417. ata_port_abort(ap);
  418. }
  419. /*
  420. * Function : sata_dwc_isr
  421. * arguments : irq, void *dev_instance, struct pt_regs *regs
  422. * Return value : irqreturn_t - status of IRQ
  423. * This Interrupt handler called via port ops registered function.
  424. * .irq_handler = sata_dwc_isr
  425. */
  426. static irqreturn_t sata_dwc_isr(int irq, void *dev_instance)
  427. {
  428. struct ata_host *host = (struct ata_host *)dev_instance;
  429. struct sata_dwc_device *hsdev = HSDEV_FROM_HOST(host);
  430. struct ata_port *ap;
  431. struct ata_queued_cmd *qc;
  432. unsigned long flags;
  433. u8 status, tag;
  434. int handled, num_processed, port = 0;
  435. uint intpr, sactive, sactive2, tag_mask;
  436. struct sata_dwc_device_port *hsdevp;
  437. hsdev->sactive_issued = 0;
  438. spin_lock_irqsave(&host->lock, flags);
  439. /* Read the interrupt register */
  440. intpr = sata_dwc_readl(&hsdev->sata_dwc_regs->intpr);
  441. ap = host->ports[port];
  442. hsdevp = HSDEVP_FROM_AP(ap);
  443. dev_dbg(ap->dev, "%s intpr=0x%08x active_tag=%d\n", __func__, intpr,
  444. ap->link.active_tag);
  445. /* Check for error interrupt */
  446. if (intpr & SATA_DWC_INTPR_ERR) {
  447. sata_dwc_error_intr(ap, hsdev, intpr);
  448. handled = 1;
  449. goto DONE;
  450. }
  451. /* Check for DMA SETUP FIS (FP DMA) interrupt */
  452. if (intpr & SATA_DWC_INTPR_NEWFP) {
  453. clear_interrupt_bit(hsdev, SATA_DWC_INTPR_NEWFP);
  454. tag = (u8)(sata_dwc_readl(&hsdev->sata_dwc_regs->fptagr));
  455. dev_dbg(ap->dev, "%s: NEWFP tag=%d\n", __func__, tag);
  456. if (hsdevp->cmd_issued[tag] != SATA_DWC_CMD_ISSUED_PEND)
  457. dev_warn(ap->dev, "CMD tag=%d not pending?\n", tag);
  458. hsdev->sactive_issued |= qcmd_tag_to_mask(tag);
  459. qc = ata_qc_from_tag(ap, tag);
  460. /*
  461. * Start FP DMA for NCQ command. At this point the tag is the
  462. * active tag. It is the tag that matches the command about to
  463. * be completed.
  464. */
  465. qc->ap->link.active_tag = tag;
  466. sata_dwc_bmdma_start_by_tag(qc, tag);
  467. handled = 1;
  468. goto DONE;
  469. }
  470. sata_dwc_scr_read(&ap->link, SCR_ACTIVE, &sactive);
  471. tag_mask = (hsdev->sactive_issued | sactive) ^ sactive;
  472. /* If no sactive issued and tag_mask is zero then this is not NCQ */
  473. if (hsdev->sactive_issued == 0 && tag_mask == 0) {
  474. if (ap->link.active_tag == ATA_TAG_POISON)
  475. tag = 0;
  476. else
  477. tag = ap->link.active_tag;
  478. qc = ata_qc_from_tag(ap, tag);
  479. /* DEV interrupt w/ no active qc? */
  480. if (unlikely(!qc || (qc->tf.flags & ATA_TFLAG_POLLING))) {
  481. dev_err(ap->dev,
  482. "%s interrupt with no active qc qc=%p\n",
  483. __func__, qc);
  484. ap->ops->sff_check_status(ap);
  485. handled = 1;
  486. goto DONE;
  487. }
  488. status = ap->ops->sff_check_status(ap);
  489. qc->ap->link.active_tag = tag;
  490. hsdevp->cmd_issued[tag] = SATA_DWC_CMD_ISSUED_NOT;
  491. if (status & ATA_ERR) {
  492. dev_dbg(ap->dev, "interrupt ATA_ERR (0x%x)\n", status);
  493. sata_dwc_qc_complete(ap, qc, 1);
  494. handled = 1;
  495. goto DONE;
  496. }
  497. dev_dbg(ap->dev, "%s non-NCQ cmd interrupt, protocol: %s\n",
  498. __func__, get_prot_descript(qc->tf.protocol));
  499. DRVSTILLBUSY:
  500. if (ata_is_dma(qc->tf.protocol)) {
  501. /*
  502. * Each DMA transaction produces 2 interrupts. The DMAC
  503. * transfer complete interrupt and the SATA controller
  504. * operation done interrupt. The command should be
  505. * completed only after both interrupts are seen.
  506. */
  507. hsdevp->dma_interrupt_count++;
  508. if (hsdevp->dma_pending[tag] == \
  509. SATA_DWC_DMA_PENDING_NONE) {
  510. dev_err(ap->dev,
  511. "%s: DMA not pending intpr=0x%08x status=0x%08x pending=%d\n",
  512. __func__, intpr, status,
  513. hsdevp->dma_pending[tag]);
  514. }
  515. if ((hsdevp->dma_interrupt_count % 2) == 0)
  516. sata_dwc_dma_xfer_complete(ap, 1);
  517. } else if (ata_is_pio(qc->tf.protocol)) {
  518. ata_sff_hsm_move(ap, qc, status, 0);
  519. handled = 1;
  520. goto DONE;
  521. } else {
  522. if (unlikely(sata_dwc_qc_complete(ap, qc, 1)))
  523. goto DRVSTILLBUSY;
  524. }
  525. handled = 1;
  526. goto DONE;
  527. }
  528. /*
  529. * This is a NCQ command. At this point we need to figure out for which
  530. * tags we have gotten a completion interrupt. One interrupt may serve
  531. * as completion for more than one operation when commands are queued
  532. * (NCQ). We need to process each completed command.
  533. */
  534. /* process completed commands */
  535. sata_dwc_scr_read(&ap->link, SCR_ACTIVE, &sactive);
  536. tag_mask = (hsdev->sactive_issued | sactive) ^ sactive;
  537. if (sactive != 0 || hsdev->sactive_issued > 1 || tag_mask > 1) {
  538. dev_dbg(ap->dev,
  539. "%s NCQ:sactive=0x%08x sactive_issued=0x%08x tag_mask=0x%08x\n",
  540. __func__, sactive, hsdev->sactive_issued, tag_mask);
  541. }
  542. if ((tag_mask | hsdev->sactive_issued) != hsdev->sactive_issued) {
  543. dev_warn(ap->dev,
  544. "Bad tag mask? sactive=0x%08x sactive_issued=0x%08x tag_mask=0x%08x\n",
  545. sactive, hsdev->sactive_issued, tag_mask);
  546. }
  547. /* read just to clear ... not bad if currently still busy */
  548. status = ap->ops->sff_check_status(ap);
  549. dev_dbg(ap->dev, "%s ATA status register=0x%x\n", __func__, status);
  550. tag = 0;
  551. num_processed = 0;
  552. while (tag_mask) {
  553. num_processed++;
  554. while (!(tag_mask & 0x00000001)) {
  555. tag++;
  556. tag_mask <<= 1;
  557. }
  558. tag_mask &= (~0x00000001);
  559. qc = ata_qc_from_tag(ap, tag);
  560. /* To be picked up by completion functions */
  561. qc->ap->link.active_tag = tag;
  562. hsdevp->cmd_issued[tag] = SATA_DWC_CMD_ISSUED_NOT;
  563. /* Let libata/scsi layers handle error */
  564. if (status & ATA_ERR) {
  565. dev_dbg(ap->dev, "%s ATA_ERR (0x%x)\n", __func__,
  566. status);
  567. sata_dwc_qc_complete(ap, qc, 1);
  568. handled = 1;
  569. goto DONE;
  570. }
  571. /* Process completed command */
  572. dev_dbg(ap->dev, "%s NCQ command, protocol: %s\n", __func__,
  573. get_prot_descript(qc->tf.protocol));
  574. if (ata_is_dma(qc->tf.protocol)) {
  575. hsdevp->dma_interrupt_count++;
  576. if (hsdevp->dma_pending[tag] == \
  577. SATA_DWC_DMA_PENDING_NONE)
  578. dev_warn(ap->dev, "%s: DMA not pending?\n",
  579. __func__);
  580. if ((hsdevp->dma_interrupt_count % 2) == 0)
  581. sata_dwc_dma_xfer_complete(ap, 1);
  582. } else {
  583. if (unlikely(sata_dwc_qc_complete(ap, qc, 1)))
  584. goto STILLBUSY;
  585. }
  586. continue;
  587. STILLBUSY:
  588. ap->stats.idle_irq++;
  589. dev_warn(ap->dev, "STILL BUSY IRQ ata%d: irq trap\n",
  590. ap->print_id);
  591. } /* while tag_mask */
  592. /*
  593. * Check to see if any commands completed while we were processing our
  594. * initial set of completed commands (read status clears interrupts,
  595. * so we might miss a completed command interrupt if one came in while
  596. * we were processing --we read status as part of processing a completed
  597. * command).
  598. */
  599. sata_dwc_scr_read(&ap->link, SCR_ACTIVE, &sactive2);
  600. if (sactive2 != sactive) {
  601. dev_dbg(ap->dev,
  602. "More completed - sactive=0x%x sactive2=0x%x\n",
  603. sactive, sactive2);
  604. }
  605. handled = 1;
  606. DONE:
  607. spin_unlock_irqrestore(&host->lock, flags);
  608. return IRQ_RETVAL(handled);
  609. }
  610. static void sata_dwc_clear_dmacr(struct sata_dwc_device_port *hsdevp, u8 tag)
  611. {
  612. struct sata_dwc_device *hsdev = HSDEV_FROM_HSDEVP(hsdevp);
  613. u32 dmacr = sata_dwc_readl(&hsdev->sata_dwc_regs->dmacr);
  614. if (hsdevp->dma_pending[tag] == SATA_DWC_DMA_PENDING_RX) {
  615. dmacr = SATA_DWC_DMACR_RX_CLEAR(dmacr);
  616. sata_dwc_writel(&hsdev->sata_dwc_regs->dmacr, dmacr);
  617. } else if (hsdevp->dma_pending[tag] == SATA_DWC_DMA_PENDING_TX) {
  618. dmacr = SATA_DWC_DMACR_TX_CLEAR(dmacr);
  619. sata_dwc_writel(&hsdev->sata_dwc_regs->dmacr, dmacr);
  620. } else {
  621. /*
  622. * This should not happen, it indicates the driver is out of
  623. * sync. If it does happen, clear dmacr anyway.
  624. */
  625. dev_err(hsdev->dev,
  626. "%s DMA protocol RX and TX DMA not pending tag=0x%02x pending=%d dmacr: 0x%08x\n",
  627. __func__, tag, hsdevp->dma_pending[tag], dmacr);
  628. sata_dwc_writel(&hsdev->sata_dwc_regs->dmacr,
  629. SATA_DWC_DMACR_TXRXCH_CLEAR);
  630. }
  631. }
  632. static void sata_dwc_dma_xfer_complete(struct ata_port *ap, u32 check_status)
  633. {
  634. struct ata_queued_cmd *qc;
  635. struct sata_dwc_device_port *hsdevp = HSDEVP_FROM_AP(ap);
  636. struct sata_dwc_device *hsdev = HSDEV_FROM_AP(ap);
  637. u8 tag = 0;
  638. tag = ap->link.active_tag;
  639. qc = ata_qc_from_tag(ap, tag);
  640. if (!qc) {
  641. dev_err(ap->dev, "failed to get qc");
  642. return;
  643. }
  644. #ifdef DEBUG_NCQ
  645. if (tag > 0) {
  646. dev_info(ap->dev,
  647. "%s tag=%u cmd=0x%02x dma dir=%s proto=%s dmacr=0x%08x\n",
  648. __func__, qc->tag, qc->tf.command,
  649. get_dma_dir_descript(qc->dma_dir),
  650. get_prot_descript(qc->tf.protocol),
  651. sata_dwc_readl(&hsdev->sata_dwc_regs->dmacr));
  652. }
  653. #endif
  654. if (ata_is_dma(qc->tf.protocol)) {
  655. if (hsdevp->dma_pending[tag] == SATA_DWC_DMA_PENDING_NONE) {
  656. dev_err(ap->dev,
  657. "%s DMA protocol RX and TX DMA not pending dmacr: 0x%08x\n",
  658. __func__,
  659. sata_dwc_readl(&hsdev->sata_dwc_regs->dmacr));
  660. }
  661. hsdevp->dma_pending[tag] = SATA_DWC_DMA_PENDING_NONE;
  662. sata_dwc_qc_complete(ap, qc, check_status);
  663. ap->link.active_tag = ATA_TAG_POISON;
  664. } else {
  665. sata_dwc_qc_complete(ap, qc, check_status);
  666. }
  667. }
  668. static int sata_dwc_qc_complete(struct ata_port *ap, struct ata_queued_cmd *qc,
  669. u32 check_status)
  670. {
  671. u8 status = 0;
  672. u32 mask = 0x0;
  673. u8 tag = qc->tag;
  674. struct sata_dwc_device *hsdev = HSDEV_FROM_AP(ap);
  675. struct sata_dwc_device_port *hsdevp = HSDEVP_FROM_AP(ap);
  676. hsdev->sactive_queued = 0;
  677. dev_dbg(ap->dev, "%s checkstatus? %x\n", __func__, check_status);
  678. if (hsdevp->dma_pending[tag] == SATA_DWC_DMA_PENDING_TX)
  679. dev_err(ap->dev, "TX DMA PENDING\n");
  680. else if (hsdevp->dma_pending[tag] == SATA_DWC_DMA_PENDING_RX)
  681. dev_err(ap->dev, "RX DMA PENDING\n");
  682. dev_dbg(ap->dev,
  683. "QC complete cmd=0x%02x status=0x%02x ata%u: protocol=%d\n",
  684. qc->tf.command, status, ap->print_id, qc->tf.protocol);
  685. /* clear active bit */
  686. mask = (~(qcmd_tag_to_mask(tag)));
  687. hsdev->sactive_queued = hsdev->sactive_queued & mask;
  688. hsdev->sactive_issued = hsdev->sactive_issued & mask;
  689. ata_qc_complete(qc);
  690. return 0;
  691. }
  692. static void sata_dwc_enable_interrupts(struct sata_dwc_device *hsdev)
  693. {
  694. /* Enable selective interrupts by setting the interrupt maskregister*/
  695. sata_dwc_writel(&hsdev->sata_dwc_regs->intmr,
  696. SATA_DWC_INTMR_ERRM |
  697. SATA_DWC_INTMR_NEWFPM |
  698. SATA_DWC_INTMR_PMABRTM |
  699. SATA_DWC_INTMR_DMATM);
  700. /*
  701. * Unmask the error bits that should trigger an error interrupt by
  702. * setting the error mask register.
  703. */
  704. sata_dwc_writel(&hsdev->sata_dwc_regs->errmr, SATA_DWC_SERROR_ERR_BITS);
  705. dev_dbg(hsdev->dev, "%s: INTMR = 0x%08x, ERRMR = 0x%08x\n",
  706. __func__, sata_dwc_readl(&hsdev->sata_dwc_regs->intmr),
  707. sata_dwc_readl(&hsdev->sata_dwc_regs->errmr));
  708. }
  709. static void sata_dwc_setup_port(struct ata_ioports *port, void __iomem *base)
  710. {
  711. port->cmd_addr = base + 0x00;
  712. port->data_addr = base + 0x00;
  713. port->error_addr = base + 0x04;
  714. port->feature_addr = base + 0x04;
  715. port->nsect_addr = base + 0x08;
  716. port->lbal_addr = base + 0x0c;
  717. port->lbam_addr = base + 0x10;
  718. port->lbah_addr = base + 0x14;
  719. port->device_addr = base + 0x18;
  720. port->command_addr = base + 0x1c;
  721. port->status_addr = base + 0x1c;
  722. port->altstatus_addr = base + 0x20;
  723. port->ctl_addr = base + 0x20;
  724. }
  725. static int sata_dwc_dma_get_channel(struct sata_dwc_device_port *hsdevp)
  726. {
  727. struct sata_dwc_device *hsdev = hsdevp->hsdev;
  728. struct device *dev = hsdev->dev;
  729. #ifdef CONFIG_SATA_DWC_OLD_DMA
  730. if (!of_find_property(dev->of_node, "dmas", NULL))
  731. return sata_dwc_dma_get_channel_old(hsdevp);
  732. #endif
  733. hsdevp->chan = dma_request_chan(dev, "sata-dma");
  734. if (IS_ERR(hsdevp->chan)) {
  735. dev_err(dev, "failed to allocate dma channel: %ld\n",
  736. PTR_ERR(hsdevp->chan));
  737. return PTR_ERR(hsdevp->chan);
  738. }
  739. return 0;
  740. }
  741. /*
  742. * Function : sata_dwc_port_start
  743. * arguments : struct ata_ioports *port
  744. * Return value : returns 0 if success, error code otherwise
  745. * This function allocates the scatter gather LLI table for AHB DMA
  746. */
  747. static int sata_dwc_port_start(struct ata_port *ap)
  748. {
  749. int err = 0;
  750. struct sata_dwc_device *hsdev;
  751. struct sata_dwc_device_port *hsdevp = NULL;
  752. struct device *pdev;
  753. int i;
  754. hsdev = HSDEV_FROM_AP(ap);
  755. dev_dbg(ap->dev, "%s: port_no=%d\n", __func__, ap->port_no);
  756. hsdev->host = ap->host;
  757. pdev = ap->host->dev;
  758. if (!pdev) {
  759. dev_err(ap->dev, "%s: no ap->host->dev\n", __func__);
  760. err = -ENODEV;
  761. goto CLEANUP;
  762. }
  763. /* Allocate Port Struct */
  764. hsdevp = kzalloc(sizeof(*hsdevp), GFP_KERNEL);
  765. if (!hsdevp) {
  766. dev_err(ap->dev, "%s: kmalloc failed for hsdevp\n", __func__);
  767. err = -ENOMEM;
  768. goto CLEANUP;
  769. }
  770. hsdevp->hsdev = hsdev;
  771. err = sata_dwc_dma_get_channel(hsdevp);
  772. if (err)
  773. goto CLEANUP_ALLOC;
  774. err = phy_power_on(hsdev->phy);
  775. if (err)
  776. goto CLEANUP_ALLOC;
  777. for (i = 0; i < SATA_DWC_QCMD_MAX; i++)
  778. hsdevp->cmd_issued[i] = SATA_DWC_CMD_ISSUED_NOT;
  779. ap->bmdma_prd = NULL; /* set these so libata doesn't use them */
  780. ap->bmdma_prd_dma = 0;
  781. if (ap->port_no == 0) {
  782. dev_dbg(ap->dev, "%s: clearing TXCHEN, RXCHEN in DMAC\n",
  783. __func__);
  784. sata_dwc_writel(&hsdev->sata_dwc_regs->dmacr,
  785. SATA_DWC_DMACR_TXRXCH_CLEAR);
  786. dev_dbg(ap->dev, "%s: setting burst size in DBTSR\n",
  787. __func__);
  788. sata_dwc_writel(&hsdev->sata_dwc_regs->dbtsr,
  789. (SATA_DWC_DBTSR_MWR(AHB_DMA_BRST_DFLT) |
  790. SATA_DWC_DBTSR_MRD(AHB_DMA_BRST_DFLT)));
  791. }
  792. /* Clear any error bits before libata starts issuing commands */
  793. clear_serror(ap);
  794. ap->private_data = hsdevp;
  795. dev_dbg(ap->dev, "%s: done\n", __func__);
  796. return 0;
  797. CLEANUP_ALLOC:
  798. kfree(hsdevp);
  799. CLEANUP:
  800. dev_dbg(ap->dev, "%s: fail. ap->id = %d\n", __func__, ap->print_id);
  801. return err;
  802. }
  803. static void sata_dwc_port_stop(struct ata_port *ap)
  804. {
  805. struct sata_dwc_device_port *hsdevp = HSDEVP_FROM_AP(ap);
  806. struct sata_dwc_device *hsdev = HSDEV_FROM_AP(ap);
  807. dev_dbg(ap->dev, "%s: ap->id = %d\n", __func__, ap->print_id);
  808. dmaengine_terminate_sync(hsdevp->chan);
  809. dma_release_channel(hsdevp->chan);
  810. phy_power_off(hsdev->phy);
  811. kfree(hsdevp);
  812. ap->private_data = NULL;
  813. }
  814. /*
  815. * Function : sata_dwc_exec_command_by_tag
  816. * arguments : ata_port *ap, ata_taskfile *tf, u8 tag, u32 cmd_issued
  817. * Return value : None
  818. * This function keeps track of individual command tag ids and calls
  819. * ata_exec_command in libata
  820. */
  821. static void sata_dwc_exec_command_by_tag(struct ata_port *ap,
  822. struct ata_taskfile *tf,
  823. u8 tag, u32 cmd_issued)
  824. {
  825. struct sata_dwc_device_port *hsdevp = HSDEVP_FROM_AP(ap);
  826. dev_dbg(ap->dev, "%s cmd(0x%02x): %s tag=%d\n", __func__, tf->command,
  827. ata_get_cmd_descript(tf->command), tag);
  828. hsdevp->cmd_issued[tag] = cmd_issued;
  829. /*
  830. * Clear SError before executing a new command.
  831. * sata_dwc_scr_write and read can not be used here. Clearing the PM
  832. * managed SError register for the disk needs to be done before the
  833. * task file is loaded.
  834. */
  835. clear_serror(ap);
  836. ata_sff_exec_command(ap, tf);
  837. }
  838. static void sata_dwc_bmdma_setup_by_tag(struct ata_queued_cmd *qc, u8 tag)
  839. {
  840. sata_dwc_exec_command_by_tag(qc->ap, &qc->tf, tag,
  841. SATA_DWC_CMD_ISSUED_PEND);
  842. }
  843. static void sata_dwc_bmdma_setup(struct ata_queued_cmd *qc)
  844. {
  845. u8 tag = qc->tag;
  846. if (ata_is_ncq(qc->tf.protocol)) {
  847. dev_dbg(qc->ap->dev, "%s: ap->link.sactive=0x%08x tag=%d\n",
  848. __func__, qc->ap->link.sactive, tag);
  849. } else {
  850. tag = 0;
  851. }
  852. sata_dwc_bmdma_setup_by_tag(qc, tag);
  853. }
  854. static void sata_dwc_bmdma_start_by_tag(struct ata_queued_cmd *qc, u8 tag)
  855. {
  856. int start_dma;
  857. u32 reg;
  858. struct sata_dwc_device *hsdev = HSDEV_FROM_QC(qc);
  859. struct ata_port *ap = qc->ap;
  860. struct sata_dwc_device_port *hsdevp = HSDEVP_FROM_AP(ap);
  861. struct dma_async_tx_descriptor *desc = hsdevp->desc[tag];
  862. int dir = qc->dma_dir;
  863. if (hsdevp->cmd_issued[tag] != SATA_DWC_CMD_ISSUED_NOT) {
  864. start_dma = 1;
  865. if (dir == DMA_TO_DEVICE)
  866. hsdevp->dma_pending[tag] = SATA_DWC_DMA_PENDING_TX;
  867. else
  868. hsdevp->dma_pending[tag] = SATA_DWC_DMA_PENDING_RX;
  869. } else {
  870. dev_err(ap->dev,
  871. "%s: Command not pending cmd_issued=%d (tag=%d) DMA NOT started\n",
  872. __func__, hsdevp->cmd_issued[tag], tag);
  873. start_dma = 0;
  874. }
  875. dev_dbg(ap->dev,
  876. "%s qc=%p tag: %x cmd: 0x%02x dma_dir: %s start_dma? %x\n",
  877. __func__, qc, tag, qc->tf.command,
  878. get_dma_dir_descript(qc->dma_dir), start_dma);
  879. sata_dwc_tf_dump(ap, &qc->tf);
  880. if (start_dma) {
  881. sata_dwc_scr_read(&ap->link, SCR_ERROR, &reg);
  882. if (reg & SATA_DWC_SERROR_ERR_BITS) {
  883. dev_err(ap->dev, "%s: ****** SError=0x%08x ******\n",
  884. __func__, reg);
  885. }
  886. if (dir == DMA_TO_DEVICE)
  887. sata_dwc_writel(&hsdev->sata_dwc_regs->dmacr,
  888. SATA_DWC_DMACR_TXCHEN);
  889. else
  890. sata_dwc_writel(&hsdev->sata_dwc_regs->dmacr,
  891. SATA_DWC_DMACR_RXCHEN);
  892. /* Enable AHB DMA transfer on the specified channel */
  893. dmaengine_submit(desc);
  894. dma_async_issue_pending(hsdevp->chan);
  895. }
  896. }
  897. static void sata_dwc_bmdma_start(struct ata_queued_cmd *qc)
  898. {
  899. u8 tag = qc->tag;
  900. if (ata_is_ncq(qc->tf.protocol)) {
  901. dev_dbg(qc->ap->dev, "%s: ap->link.sactive=0x%08x tag=%d\n",
  902. __func__, qc->ap->link.sactive, tag);
  903. } else {
  904. tag = 0;
  905. }
  906. dev_dbg(qc->ap->dev, "%s\n", __func__);
  907. sata_dwc_bmdma_start_by_tag(qc, tag);
  908. }
  909. static unsigned int sata_dwc_qc_issue(struct ata_queued_cmd *qc)
  910. {
  911. u32 sactive;
  912. u8 tag = qc->tag;
  913. struct ata_port *ap = qc->ap;
  914. struct sata_dwc_device_port *hsdevp = HSDEVP_FROM_AP(ap);
  915. #ifdef DEBUG_NCQ
  916. if (qc->tag > 0 || ap->link.sactive > 1)
  917. dev_info(ap->dev,
  918. "%s ap id=%d cmd(0x%02x)=%s qc tag=%d prot=%s ap active_tag=0x%08x ap sactive=0x%08x\n",
  919. __func__, ap->print_id, qc->tf.command,
  920. ata_get_cmd_descript(qc->tf.command),
  921. qc->tag, get_prot_descript(qc->tf.protocol),
  922. ap->link.active_tag, ap->link.sactive);
  923. #endif
  924. if (!ata_is_ncq(qc->tf.protocol))
  925. tag = 0;
  926. if (ata_is_dma(qc->tf.protocol)) {
  927. hsdevp->desc[tag] = dma_dwc_xfer_setup(qc);
  928. if (!hsdevp->desc[tag])
  929. return AC_ERR_SYSTEM;
  930. } else {
  931. hsdevp->desc[tag] = NULL;
  932. }
  933. if (ata_is_ncq(qc->tf.protocol)) {
  934. sata_dwc_scr_read(&ap->link, SCR_ACTIVE, &sactive);
  935. sactive |= (0x00000001 << tag);
  936. sata_dwc_scr_write(&ap->link, SCR_ACTIVE, sactive);
  937. dev_dbg(qc->ap->dev,
  938. "%s: tag=%d ap->link.sactive = 0x%08x sactive=0x%08x\n",
  939. __func__, tag, qc->ap->link.sactive, sactive);
  940. ap->ops->sff_tf_load(ap, &qc->tf);
  941. sata_dwc_exec_command_by_tag(ap, &qc->tf, tag,
  942. SATA_DWC_CMD_ISSUED_PEND);
  943. } else {
  944. return ata_bmdma_qc_issue(qc);
  945. }
  946. return 0;
  947. }
  948. static void sata_dwc_error_handler(struct ata_port *ap)
  949. {
  950. ata_sff_error_handler(ap);
  951. }
  952. static int sata_dwc_hardreset(struct ata_link *link, unsigned int *class,
  953. unsigned long deadline)
  954. {
  955. struct sata_dwc_device *hsdev = HSDEV_FROM_AP(link->ap);
  956. int ret;
  957. ret = sata_sff_hardreset(link, class, deadline);
  958. sata_dwc_enable_interrupts(hsdev);
  959. /* Reconfigure the DMA control register */
  960. sata_dwc_writel(&hsdev->sata_dwc_regs->dmacr,
  961. SATA_DWC_DMACR_TXRXCH_CLEAR);
  962. /* Reconfigure the DMA Burst Transaction Size register */
  963. sata_dwc_writel(&hsdev->sata_dwc_regs->dbtsr,
  964. SATA_DWC_DBTSR_MWR(AHB_DMA_BRST_DFLT) |
  965. SATA_DWC_DBTSR_MRD(AHB_DMA_BRST_DFLT));
  966. return ret;
  967. }
  968. static void sata_dwc_dev_select(struct ata_port *ap, unsigned int device)
  969. {
  970. /* SATA DWC is master only */
  971. }
  972. /*
  973. * scsi mid-layer and libata interface structures
  974. */
  975. static struct scsi_host_template sata_dwc_sht = {
  976. ATA_NCQ_SHT(DRV_NAME),
  977. /*
  978. * test-only: Currently this driver doesn't handle NCQ
  979. * correctly. We enable NCQ but set the queue depth to a
  980. * max of 1. This will get fixed in in a future release.
  981. */
  982. .sg_tablesize = LIBATA_MAX_PRD,
  983. /* .can_queue = ATA_MAX_QUEUE, */
  984. /*
  985. * Make sure a LLI block is not created that will span 8K max FIS
  986. * boundary. If the block spans such a FIS boundary, there is a chance
  987. * that a DMA burst will cross that boundary -- this results in an
  988. * error in the host controller.
  989. */
  990. .dma_boundary = 0x1fff /* ATA_DMA_BOUNDARY */,
  991. };
  992. static struct ata_port_operations sata_dwc_ops = {
  993. .inherits = &ata_sff_port_ops,
  994. .error_handler = sata_dwc_error_handler,
  995. .hardreset = sata_dwc_hardreset,
  996. .qc_issue = sata_dwc_qc_issue,
  997. .scr_read = sata_dwc_scr_read,
  998. .scr_write = sata_dwc_scr_write,
  999. .port_start = sata_dwc_port_start,
  1000. .port_stop = sata_dwc_port_stop,
  1001. .sff_dev_select = sata_dwc_dev_select,
  1002. .bmdma_setup = sata_dwc_bmdma_setup,
  1003. .bmdma_start = sata_dwc_bmdma_start,
  1004. };
  1005. static const struct ata_port_info sata_dwc_port_info[] = {
  1006. {
  1007. .flags = ATA_FLAG_SATA | ATA_FLAG_NCQ,
  1008. .pio_mask = ATA_PIO4,
  1009. .udma_mask = ATA_UDMA6,
  1010. .port_ops = &sata_dwc_ops,
  1011. },
  1012. };
  1013. static int sata_dwc_probe(struct platform_device *ofdev)
  1014. {
  1015. struct sata_dwc_device *hsdev;
  1016. u32 idr, versionr;
  1017. char *ver = (char *)&versionr;
  1018. void __iomem *base;
  1019. int err = 0;
  1020. int irq;
  1021. struct ata_host *host;
  1022. struct ata_port_info pi = sata_dwc_port_info[0];
  1023. const struct ata_port_info *ppi[] = { &pi, NULL };
  1024. struct device_node *np = ofdev->dev.of_node;
  1025. struct resource *res;
  1026. /* Allocate DWC SATA device */
  1027. host = ata_host_alloc_pinfo(&ofdev->dev, ppi, SATA_DWC_MAX_PORTS);
  1028. hsdev = devm_kzalloc(&ofdev->dev, sizeof(*hsdev), GFP_KERNEL);
  1029. if (!host || !hsdev)
  1030. return -ENOMEM;
  1031. host->private_data = hsdev;
  1032. /* Ioremap SATA registers */
  1033. res = platform_get_resource(ofdev, IORESOURCE_MEM, 0);
  1034. base = devm_ioremap_resource(&ofdev->dev, res);
  1035. if (IS_ERR(base))
  1036. return PTR_ERR(base);
  1037. dev_dbg(&ofdev->dev, "ioremap done for SATA register address\n");
  1038. /* Synopsys DWC SATA specific Registers */
  1039. hsdev->sata_dwc_regs = base + SATA_DWC_REG_OFFSET;
  1040. hsdev->dmadr = res->start + SATA_DWC_REG_OFFSET + offsetof(struct sata_dwc_regs, dmadr);
  1041. /* Setup port */
  1042. host->ports[0]->ioaddr.cmd_addr = base;
  1043. host->ports[0]->ioaddr.scr_addr = base + SATA_DWC_SCR_OFFSET;
  1044. sata_dwc_setup_port(&host->ports[0]->ioaddr, base);
  1045. /* Read the ID and Version Registers */
  1046. idr = sata_dwc_readl(&hsdev->sata_dwc_regs->idr);
  1047. versionr = sata_dwc_readl(&hsdev->sata_dwc_regs->versionr);
  1048. dev_notice(&ofdev->dev, "id %d, controller version %c.%c%c\n",
  1049. idr, ver[0], ver[1], ver[2]);
  1050. /* Save dev for later use in dev_xxx() routines */
  1051. hsdev->dev = &ofdev->dev;
  1052. /* Enable SATA Interrupts */
  1053. sata_dwc_enable_interrupts(hsdev);
  1054. /* Get SATA interrupt number */
  1055. irq = irq_of_parse_and_map(np, 0);
  1056. if (irq == NO_IRQ) {
  1057. dev_err(&ofdev->dev, "no SATA DMA irq\n");
  1058. err = -ENODEV;
  1059. goto error_out;
  1060. }
  1061. #ifdef CONFIG_SATA_DWC_OLD_DMA
  1062. if (!of_find_property(np, "dmas", NULL)) {
  1063. err = sata_dwc_dma_init_old(ofdev, hsdev);
  1064. if (err)
  1065. goto error_out;
  1066. }
  1067. #endif
  1068. hsdev->phy = devm_phy_optional_get(hsdev->dev, "sata-phy");
  1069. if (IS_ERR(hsdev->phy)) {
  1070. err = PTR_ERR(hsdev->phy);
  1071. hsdev->phy = NULL;
  1072. goto error_out;
  1073. }
  1074. err = phy_init(hsdev->phy);
  1075. if (err)
  1076. goto error_out;
  1077. /*
  1078. * Now, register with libATA core, this will also initiate the
  1079. * device discovery process, invoking our port_start() handler &
  1080. * error_handler() to execute a dummy Softreset EH session
  1081. */
  1082. err = ata_host_activate(host, irq, sata_dwc_isr, 0, &sata_dwc_sht);
  1083. if (err)
  1084. dev_err(&ofdev->dev, "failed to activate host");
  1085. return 0;
  1086. error_out:
  1087. phy_exit(hsdev->phy);
  1088. return err;
  1089. }
  1090. static int sata_dwc_remove(struct platform_device *ofdev)
  1091. {
  1092. struct device *dev = &ofdev->dev;
  1093. struct ata_host *host = dev_get_drvdata(dev);
  1094. struct sata_dwc_device *hsdev = host->private_data;
  1095. ata_host_detach(host);
  1096. phy_exit(hsdev->phy);
  1097. #ifdef CONFIG_SATA_DWC_OLD_DMA
  1098. /* Free SATA DMA resources */
  1099. sata_dwc_dma_exit_old(hsdev);
  1100. #endif
  1101. dev_dbg(&ofdev->dev, "done\n");
  1102. return 0;
  1103. }
  1104. static const struct of_device_id sata_dwc_match[] = {
  1105. { .compatible = "amcc,sata-460ex", },
  1106. {}
  1107. };
  1108. MODULE_DEVICE_TABLE(of, sata_dwc_match);
  1109. static struct platform_driver sata_dwc_driver = {
  1110. .driver = {
  1111. .name = DRV_NAME,
  1112. .of_match_table = sata_dwc_match,
  1113. },
  1114. .probe = sata_dwc_probe,
  1115. .remove = sata_dwc_remove,
  1116. };
  1117. module_platform_driver(sata_dwc_driver);
  1118. MODULE_LICENSE("GPL");
  1119. MODULE_AUTHOR("Mark Miesfeld <mmiesfeld@amcc.com>");
  1120. MODULE_DESCRIPTION("DesignWare Cores SATA controller low level driver");
  1121. MODULE_VERSION(DRV_VERSION);