sata_dwc_460ex.c 36 KB

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