sata_dwc_460ex.c 50 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784
  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/init.h>
  30. #include <linux/device.h>
  31. #include <linux/of_platform.h>
  32. #include <linux/platform_device.h>
  33. #include <linux/libata.h>
  34. #include <linux/slab.h>
  35. #include "libata.h"
  36. #include <scsi/scsi_host.h>
  37. #include <scsi/scsi_cmnd.h>
  38. /* These two are defined in "libata.h" */
  39. #undef DRV_NAME
  40. #undef DRV_VERSION
  41. #define DRV_NAME "sata-dwc"
  42. #define DRV_VERSION "1.3"
  43. /* SATA DMA driver Globals */
  44. #define DMA_NUM_CHANS 1
  45. #define DMA_NUM_CHAN_REGS 8
  46. /* SATA DMA Register definitions */
  47. #define AHB_DMA_BRST_DFLT 64 /* 16 data items burst length*/
  48. struct dmareg {
  49. u32 low; /* Low bits 0-31 */
  50. u32 high; /* High bits 32-63 */
  51. };
  52. /* DMA Per Channel registers */
  53. struct dma_chan_regs {
  54. struct dmareg sar; /* Source Address */
  55. struct dmareg dar; /* Destination address */
  56. struct dmareg llp; /* Linked List Pointer */
  57. struct dmareg ctl; /* Control */
  58. struct dmareg sstat; /* Source Status not implemented in core */
  59. struct dmareg dstat; /* Destination Status not implemented in core*/
  60. struct dmareg sstatar; /* Source Status Address not impl in core */
  61. struct dmareg dstatar; /* Destination Status Address not implemente */
  62. struct dmareg cfg; /* Config */
  63. struct dmareg sgr; /* Source Gather */
  64. struct dmareg dsr; /* Destination Scatter */
  65. };
  66. /* Generic Interrupt Registers */
  67. struct dma_interrupt_regs {
  68. struct dmareg tfr; /* Transfer Interrupt */
  69. struct dmareg block; /* Block Interrupt */
  70. struct dmareg srctran; /* Source Transfer Interrupt */
  71. struct dmareg dsttran; /* Dest Transfer Interrupt */
  72. struct dmareg error; /* Error */
  73. };
  74. struct ahb_dma_regs {
  75. struct dma_chan_regs chan_regs[DMA_NUM_CHAN_REGS];
  76. struct dma_interrupt_regs interrupt_raw; /* Raw Interrupt */
  77. struct dma_interrupt_regs interrupt_status; /* Interrupt Status */
  78. struct dma_interrupt_regs interrupt_mask; /* Interrupt Mask */
  79. struct dma_interrupt_regs interrupt_clear; /* Interrupt Clear */
  80. struct dmareg statusInt; /* Interrupt combined*/
  81. struct dmareg rq_srcreg; /* Src Trans Req */
  82. struct dmareg rq_dstreg; /* Dst Trans Req */
  83. struct dmareg rq_sgl_srcreg; /* Sngl Src Trans Req*/
  84. struct dmareg rq_sgl_dstreg; /* Sngl Dst Trans Req*/
  85. struct dmareg rq_lst_srcreg; /* Last Src Trans Req*/
  86. struct dmareg rq_lst_dstreg; /* Last Dst Trans Req*/
  87. struct dmareg dma_cfg; /* DMA Config */
  88. struct dmareg dma_chan_en; /* DMA Channel Enable*/
  89. struct dmareg dma_id; /* DMA ID */
  90. struct dmareg dma_test; /* DMA Test */
  91. struct dmareg res1; /* reserved */
  92. struct dmareg res2; /* reserved */
  93. /*
  94. * DMA Comp Params
  95. * Param 6 = dma_param[0], Param 5 = dma_param[1],
  96. * Param 4 = dma_param[2] ...
  97. */
  98. struct dmareg dma_params[6];
  99. };
  100. /* Data structure for linked list item */
  101. struct lli {
  102. u32 sar; /* Source Address */
  103. u32 dar; /* Destination address */
  104. u32 llp; /* Linked List Pointer */
  105. struct dmareg ctl; /* Control */
  106. struct dmareg dstat; /* Destination Status */
  107. };
  108. enum {
  109. SATA_DWC_DMAC_LLI_SZ = (sizeof(struct lli)),
  110. SATA_DWC_DMAC_LLI_NUM = 256,
  111. SATA_DWC_DMAC_LLI_TBL_SZ = (SATA_DWC_DMAC_LLI_SZ * \
  112. SATA_DWC_DMAC_LLI_NUM),
  113. SATA_DWC_DMAC_TWIDTH_BYTES = 4,
  114. SATA_DWC_DMAC_CTRL_TSIZE_MAX = (0x00000800 * \
  115. SATA_DWC_DMAC_TWIDTH_BYTES),
  116. };
  117. /* DMA Register Operation Bits */
  118. enum {
  119. DMA_EN = 0x00000001, /* Enable AHB DMA */
  120. DMA_CTL_LLP_SRCEN = 0x10000000, /* Blk chain enable Src */
  121. DMA_CTL_LLP_DSTEN = 0x08000000, /* Blk chain enable Dst */
  122. };
  123. #define DMA_CTL_BLK_TS(size) ((size) & 0x000000FFF) /* Blk Transfer size */
  124. #define DMA_CHANNEL(ch) (0x00000001 << (ch)) /* Select channel */
  125. /* Enable channel */
  126. #define DMA_ENABLE_CHAN(ch) ((0x00000001 << (ch)) | \
  127. ((0x000000001 << (ch)) << 8))
  128. /* Disable channel */
  129. #define DMA_DISABLE_CHAN(ch) (0x00000000 | ((0x000000001 << (ch)) << 8))
  130. /* Transfer Type & Flow Controller */
  131. #define DMA_CTL_TTFC(type) (((type) & 0x7) << 20)
  132. #define DMA_CTL_SMS(num) (((num) & 0x3) << 25) /* Src Master Select */
  133. #define DMA_CTL_DMS(num) (((num) & 0x3) << 23)/* Dst Master Select */
  134. /* Src Burst Transaction Length */
  135. #define DMA_CTL_SRC_MSIZE(size) (((size) & 0x7) << 14)
  136. /* Dst Burst Transaction Length */
  137. #define DMA_CTL_DST_MSIZE(size) (((size) & 0x7) << 11)
  138. /* Source Transfer Width */
  139. #define DMA_CTL_SRC_TRWID(size) (((size) & 0x7) << 4)
  140. /* Destination Transfer Width */
  141. #define DMA_CTL_DST_TRWID(size) (((size) & 0x7) << 1)
  142. /* Assign HW handshaking interface (x) to destination / source peripheral */
  143. #define DMA_CFG_HW_HS_DEST(int_num) (((int_num) & 0xF) << 11)
  144. #define DMA_CFG_HW_HS_SRC(int_num) (((int_num) & 0xF) << 7)
  145. #define DMA_LLP_LMS(addr, master) (((addr) & 0xfffffffc) | (master))
  146. /*
  147. * This define is used to set block chaining disabled in the control low
  148. * register. It is already in little endian format so it can be &'d dirctly.
  149. * It is essentially: cpu_to_le32(~(DMA_CTL_LLP_SRCEN | DMA_CTL_LLP_DSTEN))
  150. */
  151. enum {
  152. DMA_CTL_LLP_DISABLE_LE32 = 0xffffffe7,
  153. DMA_CTL_TTFC_P2M_DMAC = 0x00000002, /* Per to mem, DMAC cntr */
  154. DMA_CTL_TTFC_M2P_PER = 0x00000003, /* Mem to per, peripheral cntr */
  155. DMA_CTL_SINC_INC = 0x00000000, /* Source Address Increment */
  156. DMA_CTL_SINC_DEC = 0x00000200,
  157. DMA_CTL_SINC_NOCHANGE = 0x00000400,
  158. DMA_CTL_DINC_INC = 0x00000000, /* Destination Address Increment */
  159. DMA_CTL_DINC_DEC = 0x00000080,
  160. DMA_CTL_DINC_NOCHANGE = 0x00000100,
  161. DMA_CTL_INT_EN = 0x00000001, /* Interrupt Enable */
  162. /* Channel Configuration Register high bits */
  163. DMA_CFG_FCMOD_REQ = 0x00000001, /* Flow Control - request based */
  164. DMA_CFG_PROTCTL = (0x00000003 << 2),/* Protection Control */
  165. /* Channel Configuration Register low bits */
  166. DMA_CFG_RELD_DST = 0x80000000, /* Reload Dest / Src Addr */
  167. DMA_CFG_RELD_SRC = 0x40000000,
  168. DMA_CFG_HS_SELSRC = 0x00000800, /* Software handshake Src/ Dest */
  169. DMA_CFG_HS_SELDST = 0x00000400,
  170. DMA_CFG_FIFOEMPTY = (0x00000001 << 9), /* FIFO Empty bit */
  171. /* Channel Linked List Pointer Register */
  172. DMA_LLP_AHBMASTER1 = 0, /* List Master Select */
  173. DMA_LLP_AHBMASTER2 = 1,
  174. SATA_DWC_MAX_PORTS = 1,
  175. SATA_DWC_SCR_OFFSET = 0x24,
  176. SATA_DWC_REG_OFFSET = 0x64,
  177. };
  178. /* DWC SATA Registers */
  179. struct sata_dwc_regs {
  180. u32 fptagr; /* 1st party DMA tag */
  181. u32 fpbor; /* 1st party DMA buffer offset */
  182. u32 fptcr; /* 1st party DMA Xfr count */
  183. u32 dmacr; /* DMA Control */
  184. u32 dbtsr; /* DMA Burst Transac size */
  185. u32 intpr; /* Interrupt Pending */
  186. u32 intmr; /* Interrupt Mask */
  187. u32 errmr; /* Error Mask */
  188. u32 llcr; /* Link Layer Control */
  189. u32 phycr; /* PHY Control */
  190. u32 physr; /* PHY Status */
  191. u32 rxbistpd; /* Recvd BIST pattern def register */
  192. u32 rxbistpd1; /* Recvd BIST data dword1 */
  193. u32 rxbistpd2; /* Recvd BIST pattern data dword2 */
  194. u32 txbistpd; /* Trans BIST pattern def register */
  195. u32 txbistpd1; /* Trans BIST data dword1 */
  196. u32 txbistpd2; /* Trans BIST data dword2 */
  197. u32 bistcr; /* BIST Control Register */
  198. u32 bistfctr; /* BIST FIS Count Register */
  199. u32 bistsr; /* BIST Status Register */
  200. u32 bistdecr; /* BIST Dword Error count register */
  201. u32 res[15]; /* Reserved locations */
  202. u32 testr; /* Test Register */
  203. u32 versionr; /* Version Register */
  204. u32 idr; /* ID Register */
  205. u32 unimpl[192]; /* Unimplemented */
  206. u32 dmadr[256]; /* FIFO Locations in DMA Mode */
  207. };
  208. enum {
  209. SCR_SCONTROL_DET_ENABLE = 0x00000001,
  210. SCR_SSTATUS_DET_PRESENT = 0x00000001,
  211. SCR_SERROR_DIAG_X = 0x04000000,
  212. /* DWC SATA Register Operations */
  213. SATA_DWC_TXFIFO_DEPTH = 0x01FF,
  214. SATA_DWC_RXFIFO_DEPTH = 0x01FF,
  215. SATA_DWC_DMACR_TMOD_TXCHEN = 0x00000004,
  216. SATA_DWC_DMACR_TXCHEN = (0x00000001 | SATA_DWC_DMACR_TMOD_TXCHEN),
  217. SATA_DWC_DMACR_RXCHEN = (0x00000002 | SATA_DWC_DMACR_TMOD_TXCHEN),
  218. SATA_DWC_DMACR_TXRXCH_CLEAR = SATA_DWC_DMACR_TMOD_TXCHEN,
  219. SATA_DWC_INTPR_DMAT = 0x00000001,
  220. SATA_DWC_INTPR_NEWFP = 0x00000002,
  221. SATA_DWC_INTPR_PMABRT = 0x00000004,
  222. SATA_DWC_INTPR_ERR = 0x00000008,
  223. SATA_DWC_INTPR_NEWBIST = 0x00000010,
  224. SATA_DWC_INTPR_IPF = 0x10000000,
  225. SATA_DWC_INTMR_DMATM = 0x00000001,
  226. SATA_DWC_INTMR_NEWFPM = 0x00000002,
  227. SATA_DWC_INTMR_PMABRTM = 0x00000004,
  228. SATA_DWC_INTMR_ERRM = 0x00000008,
  229. SATA_DWC_INTMR_NEWBISTM = 0x00000010,
  230. SATA_DWC_LLCR_SCRAMEN = 0x00000001,
  231. SATA_DWC_LLCR_DESCRAMEN = 0x00000002,
  232. SATA_DWC_LLCR_RPDEN = 0x00000004,
  233. /* This is all error bits, zero's are reserved fields. */
  234. SATA_DWC_SERROR_ERR_BITS = 0x0FFF0F03
  235. };
  236. #define SATA_DWC_SCR0_SPD_GET(v) (((v) >> 4) & 0x0000000F)
  237. #define SATA_DWC_DMACR_TX_CLEAR(v) (((v) & ~SATA_DWC_DMACR_TXCHEN) |\
  238. SATA_DWC_DMACR_TMOD_TXCHEN)
  239. #define SATA_DWC_DMACR_RX_CLEAR(v) (((v) & ~SATA_DWC_DMACR_RXCHEN) |\
  240. SATA_DWC_DMACR_TMOD_TXCHEN)
  241. #define SATA_DWC_DBTSR_MWR(size) (((size)/4) & SATA_DWC_TXFIFO_DEPTH)
  242. #define SATA_DWC_DBTSR_MRD(size) ((((size)/4) & SATA_DWC_RXFIFO_DEPTH)\
  243. << 16)
  244. struct sata_dwc_device {
  245. struct device *dev; /* generic device struct */
  246. struct ata_probe_ent *pe; /* ptr to probe-ent */
  247. struct ata_host *host;
  248. u8 *reg_base;
  249. struct sata_dwc_regs *sata_dwc_regs; /* DW Synopsys SATA specific */
  250. int irq_dma;
  251. };
  252. #define SATA_DWC_QCMD_MAX 32
  253. struct sata_dwc_device_port {
  254. struct sata_dwc_device *hsdev;
  255. int cmd_issued[SATA_DWC_QCMD_MAX];
  256. struct lli *llit[SATA_DWC_QCMD_MAX]; /* DMA LLI table */
  257. dma_addr_t llit_dma[SATA_DWC_QCMD_MAX];
  258. u32 dma_chan[SATA_DWC_QCMD_MAX];
  259. int dma_pending[SATA_DWC_QCMD_MAX];
  260. };
  261. /*
  262. * Commonly used DWC SATA driver Macros
  263. */
  264. #define HSDEV_FROM_HOST(host) ((struct sata_dwc_device *)\
  265. (host)->private_data)
  266. #define HSDEV_FROM_AP(ap) ((struct sata_dwc_device *)\
  267. (ap)->host->private_data)
  268. #define HSDEVP_FROM_AP(ap) ((struct sata_dwc_device_port *)\
  269. (ap)->private_data)
  270. #define HSDEV_FROM_QC(qc) ((struct sata_dwc_device *)\
  271. (qc)->ap->host->private_data)
  272. #define HSDEV_FROM_HSDEVP(p) ((struct sata_dwc_device *)\
  273. (hsdevp)->hsdev)
  274. enum {
  275. SATA_DWC_CMD_ISSUED_NOT = 0,
  276. SATA_DWC_CMD_ISSUED_PEND = 1,
  277. SATA_DWC_CMD_ISSUED_EXEC = 2,
  278. SATA_DWC_CMD_ISSUED_NODATA = 3,
  279. SATA_DWC_DMA_PENDING_NONE = 0,
  280. SATA_DWC_DMA_PENDING_TX = 1,
  281. SATA_DWC_DMA_PENDING_RX = 2,
  282. };
  283. struct sata_dwc_host_priv {
  284. void __iomem *scr_addr_sstatus;
  285. u32 sata_dwc_sactive_issued ;
  286. u32 sata_dwc_sactive_queued ;
  287. u32 dma_interrupt_count;
  288. struct ahb_dma_regs *sata_dma_regs;
  289. struct device *dwc_dev;
  290. };
  291. struct sata_dwc_host_priv host_pvt;
  292. /*
  293. * Prototypes
  294. */
  295. static void sata_dwc_bmdma_start_by_tag(struct ata_queued_cmd *qc, u8 tag);
  296. static int sata_dwc_qc_complete(struct ata_port *ap, struct ata_queued_cmd *qc,
  297. u32 check_status);
  298. static void sata_dwc_dma_xfer_complete(struct ata_port *ap, u32 check_status);
  299. static void sata_dwc_port_stop(struct ata_port *ap);
  300. static void sata_dwc_clear_dmacr(struct sata_dwc_device_port *hsdevp, u8 tag);
  301. static int dma_dwc_init(struct sata_dwc_device *hsdev, int irq);
  302. static void dma_dwc_exit(struct sata_dwc_device *hsdev);
  303. static int dma_dwc_xfer_setup(struct scatterlist *sg, int num_elems,
  304. struct lli *lli, dma_addr_t dma_lli,
  305. void __iomem *addr, int dir);
  306. static void dma_dwc_xfer_start(int dma_ch);
  307. static const char *get_prot_descript(u8 protocol)
  308. {
  309. switch ((enum ata_tf_protocols)protocol) {
  310. case ATA_PROT_NODATA:
  311. return "ATA no data";
  312. case ATA_PROT_PIO:
  313. return "ATA PIO";
  314. case ATA_PROT_DMA:
  315. return "ATA DMA";
  316. case ATA_PROT_NCQ:
  317. return "ATA NCQ";
  318. case ATAPI_PROT_NODATA:
  319. return "ATAPI no data";
  320. case ATAPI_PROT_PIO:
  321. return "ATAPI PIO";
  322. case ATAPI_PROT_DMA:
  323. return "ATAPI DMA";
  324. default:
  325. return "unknown";
  326. }
  327. }
  328. static const char *get_dma_dir_descript(int dma_dir)
  329. {
  330. switch ((enum dma_data_direction)dma_dir) {
  331. case DMA_BIDIRECTIONAL:
  332. return "bidirectional";
  333. case DMA_TO_DEVICE:
  334. return "to device";
  335. case DMA_FROM_DEVICE:
  336. return "from device";
  337. default:
  338. return "none";
  339. }
  340. }
  341. static void sata_dwc_tf_dump(struct ata_taskfile *tf)
  342. {
  343. dev_vdbg(host_pvt.dwc_dev, "taskfile cmd: 0x%02x protocol: %s flags:"
  344. "0x%lx device: %x\n", tf->command,
  345. get_prot_descript(tf->protocol), tf->flags, tf->device);
  346. dev_vdbg(host_pvt.dwc_dev, "feature: 0x%02x nsect: 0x%x lbal: 0x%x "
  347. "lbam: 0x%x lbah: 0x%x\n", tf->feature, tf->nsect, tf->lbal,
  348. tf->lbam, tf->lbah);
  349. dev_vdbg(host_pvt.dwc_dev, "hob_feature: 0x%02x hob_nsect: 0x%x "
  350. "hob_lbal: 0x%x hob_lbam: 0x%x hob_lbah: 0x%x\n",
  351. tf->hob_feature, tf->hob_nsect, tf->hob_lbal, tf->hob_lbam,
  352. tf->hob_lbah);
  353. }
  354. /*
  355. * Function: get_burst_length_encode
  356. * arguments: datalength: length in bytes of data
  357. * returns value to be programmed in register corresponding to data length
  358. * This value is effectively the log(base 2) of the length
  359. */
  360. static int get_burst_length_encode(int datalength)
  361. {
  362. int items = datalength >> 2; /* div by 4 to get lword count */
  363. if (items >= 64)
  364. return 5;
  365. if (items >= 32)
  366. return 4;
  367. if (items >= 16)
  368. return 3;
  369. if (items >= 8)
  370. return 2;
  371. if (items >= 4)
  372. return 1;
  373. return 0;
  374. }
  375. static void clear_chan_interrupts(int c)
  376. {
  377. out_le32(&(host_pvt.sata_dma_regs->interrupt_clear.tfr.low),
  378. DMA_CHANNEL(c));
  379. out_le32(&(host_pvt.sata_dma_regs->interrupt_clear.block.low),
  380. DMA_CHANNEL(c));
  381. out_le32(&(host_pvt.sata_dma_regs->interrupt_clear.srctran.low),
  382. DMA_CHANNEL(c));
  383. out_le32(&(host_pvt.sata_dma_regs->interrupt_clear.dsttran.low),
  384. DMA_CHANNEL(c));
  385. out_le32(&(host_pvt.sata_dma_regs->interrupt_clear.error.low),
  386. DMA_CHANNEL(c));
  387. }
  388. /*
  389. * Function: dma_request_channel
  390. * arguments: None
  391. * returns channel number if available else -1
  392. * This function assigns the next available DMA channel from the list to the
  393. * requester
  394. */
  395. static int dma_request_channel(void)
  396. {
  397. int i;
  398. for (i = 0; i < DMA_NUM_CHANS; i++) {
  399. if (!(in_le32(&(host_pvt.sata_dma_regs->dma_chan_en.low)) &\
  400. DMA_CHANNEL(i)))
  401. return i;
  402. }
  403. dev_err(host_pvt.dwc_dev, "%s NO channel chan_en: 0x%08x\n", __func__,
  404. in_le32(&(host_pvt.sata_dma_regs->dma_chan_en.low)));
  405. return -1;
  406. }
  407. /*
  408. * Function: dma_dwc_interrupt
  409. * arguments: irq, dev_id, pt_regs
  410. * returns channel number if available else -1
  411. * Interrupt Handler for DW AHB SATA DMA
  412. */
  413. static irqreturn_t dma_dwc_interrupt(int irq, void *hsdev_instance)
  414. {
  415. int chan;
  416. u32 tfr_reg, err_reg;
  417. unsigned long flags;
  418. struct sata_dwc_device *hsdev =
  419. (struct sata_dwc_device *)hsdev_instance;
  420. struct ata_host *host = (struct ata_host *)hsdev->host;
  421. struct ata_port *ap;
  422. struct sata_dwc_device_port *hsdevp;
  423. u8 tag = 0;
  424. unsigned int port = 0;
  425. spin_lock_irqsave(&host->lock, flags);
  426. ap = host->ports[port];
  427. hsdevp = HSDEVP_FROM_AP(ap);
  428. tag = ap->link.active_tag;
  429. tfr_reg = in_le32(&(host_pvt.sata_dma_regs->interrupt_status.tfr\
  430. .low));
  431. err_reg = in_le32(&(host_pvt.sata_dma_regs->interrupt_status.error\
  432. .low));
  433. dev_dbg(ap->dev, "eot=0x%08x err=0x%08x pending=%d active port=%d\n",
  434. tfr_reg, err_reg, hsdevp->dma_pending[tag], port);
  435. for (chan = 0; chan < DMA_NUM_CHANS; chan++) {
  436. /* Check for end-of-transfer interrupt. */
  437. if (tfr_reg & DMA_CHANNEL(chan)) {
  438. /*
  439. * Each DMA command produces 2 interrupts. Only
  440. * complete the command after both interrupts have been
  441. * seen. (See sata_dwc_isr())
  442. */
  443. host_pvt.dma_interrupt_count++;
  444. sata_dwc_clear_dmacr(hsdevp, tag);
  445. if (hsdevp->dma_pending[tag] ==
  446. SATA_DWC_DMA_PENDING_NONE) {
  447. dev_err(ap->dev, "DMA not pending eot=0x%08x "
  448. "err=0x%08x tag=0x%02x pending=%d\n",
  449. tfr_reg, err_reg, tag,
  450. hsdevp->dma_pending[tag]);
  451. }
  452. if ((host_pvt.dma_interrupt_count % 2) == 0)
  453. sata_dwc_dma_xfer_complete(ap, 1);
  454. /* Clear the interrupt */
  455. out_le32(&(host_pvt.sata_dma_regs->interrupt_clear\
  456. .tfr.low),
  457. DMA_CHANNEL(chan));
  458. }
  459. /* Check for error interrupt. */
  460. if (err_reg & DMA_CHANNEL(chan)) {
  461. /* TODO Need error handler ! */
  462. dev_err(ap->dev, "error interrupt err_reg=0x%08x\n",
  463. err_reg);
  464. /* Clear the interrupt. */
  465. out_le32(&(host_pvt.sata_dma_regs->interrupt_clear\
  466. .error.low),
  467. DMA_CHANNEL(chan));
  468. }
  469. }
  470. spin_unlock_irqrestore(&host->lock, flags);
  471. return IRQ_HANDLED;
  472. }
  473. /*
  474. * Function: dma_request_interrupts
  475. * arguments: hsdev
  476. * returns status
  477. * This function registers ISR for a particular DMA channel interrupt
  478. */
  479. static int dma_request_interrupts(struct sata_dwc_device *hsdev, int irq)
  480. {
  481. int retval = 0;
  482. int chan;
  483. for (chan = 0; chan < DMA_NUM_CHANS; chan++) {
  484. /* Unmask error interrupt */
  485. out_le32(&(host_pvt.sata_dma_regs)->interrupt_mask.error.low,
  486. DMA_ENABLE_CHAN(chan));
  487. /* Unmask end-of-transfer interrupt */
  488. out_le32(&(host_pvt.sata_dma_regs)->interrupt_mask.tfr.low,
  489. DMA_ENABLE_CHAN(chan));
  490. }
  491. retval = request_irq(irq, dma_dwc_interrupt, 0, "SATA DMA", hsdev);
  492. if (retval) {
  493. dev_err(host_pvt.dwc_dev, "%s: could not get IRQ %d\n",
  494. __func__, irq);
  495. return -ENODEV;
  496. }
  497. /* Mark this interrupt as requested */
  498. hsdev->irq_dma = irq;
  499. return 0;
  500. }
  501. /*
  502. * Function: map_sg_to_lli
  503. * The Synopsis driver has a comment proposing that better performance
  504. * is possible by only enabling interrupts on the last item in the linked list.
  505. * However, it seems that could be a problem if an error happened on one of the
  506. * first items. The transfer would halt, but no error interrupt would occur.
  507. * Currently this function sets interrupts enabled for each linked list item:
  508. * DMA_CTL_INT_EN.
  509. */
  510. static int map_sg_to_lli(struct scatterlist *sg, int num_elems,
  511. struct lli *lli, dma_addr_t dma_lli,
  512. void __iomem *dmadr_addr, int dir)
  513. {
  514. int i, idx = 0;
  515. int fis_len = 0;
  516. dma_addr_t next_llp;
  517. int bl;
  518. dev_dbg(host_pvt.dwc_dev, "%s: sg=%p nelem=%d lli=%p dma_lli=0x%08x"
  519. " dmadr=0x%08x\n", __func__, sg, num_elems, lli, (u32)dma_lli,
  520. (u32)dmadr_addr);
  521. bl = get_burst_length_encode(AHB_DMA_BRST_DFLT);
  522. for (i = 0; i < num_elems; i++, sg++) {
  523. u32 addr, offset;
  524. u32 sg_len, len;
  525. addr = (u32) sg_dma_address(sg);
  526. sg_len = sg_dma_len(sg);
  527. dev_dbg(host_pvt.dwc_dev, "%s: elem=%d sg_addr=0x%x sg_len"
  528. "=%d\n", __func__, i, addr, sg_len);
  529. while (sg_len) {
  530. if (idx >= SATA_DWC_DMAC_LLI_NUM) {
  531. /* The LLI table is not large enough. */
  532. dev_err(host_pvt.dwc_dev, "LLI table overrun "
  533. "(idx=%d)\n", idx);
  534. break;
  535. }
  536. len = (sg_len > SATA_DWC_DMAC_CTRL_TSIZE_MAX) ?
  537. SATA_DWC_DMAC_CTRL_TSIZE_MAX : sg_len;
  538. offset = addr & 0xffff;
  539. if ((offset + sg_len) > 0x10000)
  540. len = 0x10000 - offset;
  541. /*
  542. * Make sure a LLI block is not created that will span
  543. * 8K max FIS boundary. If the block spans such a FIS
  544. * boundary, there is a chance that a DMA burst will
  545. * cross that boundary -- this results in an error in
  546. * the host controller.
  547. */
  548. if (fis_len + len > 8192) {
  549. dev_dbg(host_pvt.dwc_dev, "SPLITTING: fis_len="
  550. "%d(0x%x) len=%d(0x%x)\n", fis_len,
  551. fis_len, len, len);
  552. len = 8192 - fis_len;
  553. fis_len = 0;
  554. } else {
  555. fis_len += len;
  556. }
  557. if (fis_len == 8192)
  558. fis_len = 0;
  559. /*
  560. * Set DMA addresses and lower half of control register
  561. * based on direction.
  562. */
  563. if (dir == DMA_FROM_DEVICE) {
  564. lli[idx].dar = cpu_to_le32(addr);
  565. lli[idx].sar = cpu_to_le32((u32)dmadr_addr);
  566. lli[idx].ctl.low = cpu_to_le32(
  567. DMA_CTL_TTFC(DMA_CTL_TTFC_P2M_DMAC) |
  568. DMA_CTL_SMS(0) |
  569. DMA_CTL_DMS(1) |
  570. DMA_CTL_SRC_MSIZE(bl) |
  571. DMA_CTL_DST_MSIZE(bl) |
  572. DMA_CTL_SINC_NOCHANGE |
  573. DMA_CTL_SRC_TRWID(2) |
  574. DMA_CTL_DST_TRWID(2) |
  575. DMA_CTL_INT_EN |
  576. DMA_CTL_LLP_SRCEN |
  577. DMA_CTL_LLP_DSTEN);
  578. } else { /* DMA_TO_DEVICE */
  579. lli[idx].sar = cpu_to_le32(addr);
  580. lli[idx].dar = cpu_to_le32((u32)dmadr_addr);
  581. lli[idx].ctl.low = cpu_to_le32(
  582. DMA_CTL_TTFC(DMA_CTL_TTFC_M2P_PER) |
  583. DMA_CTL_SMS(1) |
  584. DMA_CTL_DMS(0) |
  585. DMA_CTL_SRC_MSIZE(bl) |
  586. DMA_CTL_DST_MSIZE(bl) |
  587. DMA_CTL_DINC_NOCHANGE |
  588. DMA_CTL_SRC_TRWID(2) |
  589. DMA_CTL_DST_TRWID(2) |
  590. DMA_CTL_INT_EN |
  591. DMA_CTL_LLP_SRCEN |
  592. DMA_CTL_LLP_DSTEN);
  593. }
  594. dev_dbg(host_pvt.dwc_dev, "%s setting ctl.high len: "
  595. "0x%08x val: 0x%08x\n", __func__,
  596. len, DMA_CTL_BLK_TS(len / 4));
  597. /* Program the LLI CTL high register */
  598. lli[idx].ctl.high = cpu_to_le32(DMA_CTL_BLK_TS\
  599. (len / 4));
  600. /* Program the next pointer. The next pointer must be
  601. * the physical address, not the virtual address.
  602. */
  603. next_llp = (dma_lli + ((idx + 1) * sizeof(struct \
  604. lli)));
  605. /* The last 2 bits encode the list master select. */
  606. next_llp = DMA_LLP_LMS(next_llp, DMA_LLP_AHBMASTER2);
  607. lli[idx].llp = cpu_to_le32(next_llp);
  608. idx++;
  609. sg_len -= len;
  610. addr += len;
  611. }
  612. }
  613. /*
  614. * The last next ptr has to be zero and the last control low register
  615. * has to have LLP_SRC_EN and LLP_DST_EN (linked list pointer source
  616. * and destination enable) set back to 0 (disabled.) This is what tells
  617. * the core that this is the last item in the linked list.
  618. */
  619. if (idx) {
  620. lli[idx-1].llp = 0x00000000;
  621. lli[idx-1].ctl.low &= DMA_CTL_LLP_DISABLE_LE32;
  622. /* Flush cache to memory */
  623. dma_cache_sync(NULL, lli, (sizeof(struct lli) * idx),
  624. DMA_BIDIRECTIONAL);
  625. }
  626. return idx;
  627. }
  628. /*
  629. * Function: dma_dwc_xfer_start
  630. * arguments: Channel number
  631. * Return : None
  632. * Enables the DMA channel
  633. */
  634. static void dma_dwc_xfer_start(int dma_ch)
  635. {
  636. /* Enable the DMA channel */
  637. out_le32(&(host_pvt.sata_dma_regs->dma_chan_en.low),
  638. in_le32(&(host_pvt.sata_dma_regs->dma_chan_en.low)) |
  639. DMA_ENABLE_CHAN(dma_ch));
  640. }
  641. static int dma_dwc_xfer_setup(struct scatterlist *sg, int num_elems,
  642. struct lli *lli, dma_addr_t dma_lli,
  643. void __iomem *addr, int dir)
  644. {
  645. int dma_ch;
  646. int num_lli;
  647. /* Acquire DMA channel */
  648. dma_ch = dma_request_channel();
  649. if (dma_ch == -1) {
  650. dev_err(host_pvt.dwc_dev, "%s: dma channel unavailable\n",
  651. __func__);
  652. return -EAGAIN;
  653. }
  654. /* Convert SG list to linked list of items (LLIs) for AHB DMA */
  655. num_lli = map_sg_to_lli(sg, num_elems, lli, dma_lli, addr, dir);
  656. dev_dbg(host_pvt.dwc_dev, "%s sg: 0x%p, count: %d lli: %p dma_lli:"
  657. " 0x%0xlx addr: %p lli count: %d\n", __func__, sg, num_elems,
  658. lli, (u32)dma_lli, addr, num_lli);
  659. clear_chan_interrupts(dma_ch);
  660. /* Program the CFG register. */
  661. out_le32(&(host_pvt.sata_dma_regs->chan_regs[dma_ch].cfg.high),
  662. DMA_CFG_PROTCTL | DMA_CFG_FCMOD_REQ);
  663. out_le32(&(host_pvt.sata_dma_regs->chan_regs[dma_ch].cfg.low), 0);
  664. /* Program the address of the linked list */
  665. out_le32(&(host_pvt.sata_dma_regs->chan_regs[dma_ch].llp.low),
  666. DMA_LLP_LMS(dma_lli, DMA_LLP_AHBMASTER2));
  667. /* Program the CTL register with src enable / dst enable */
  668. out_le32(&(host_pvt.sata_dma_regs->chan_regs[dma_ch].ctl.low),
  669. DMA_CTL_LLP_SRCEN | DMA_CTL_LLP_DSTEN);
  670. return dma_ch;
  671. }
  672. /*
  673. * Function: dma_dwc_exit
  674. * arguments: None
  675. * returns status
  676. * This function exits the SATA DMA driver
  677. */
  678. static void dma_dwc_exit(struct sata_dwc_device *hsdev)
  679. {
  680. dev_dbg(host_pvt.dwc_dev, "%s:\n", __func__);
  681. if (host_pvt.sata_dma_regs) {
  682. iounmap(host_pvt.sata_dma_regs);
  683. host_pvt.sata_dma_regs = NULL;
  684. }
  685. if (hsdev->irq_dma) {
  686. free_irq(hsdev->irq_dma, hsdev);
  687. hsdev->irq_dma = 0;
  688. }
  689. }
  690. /*
  691. * Function: dma_dwc_init
  692. * arguments: hsdev
  693. * returns status
  694. * This function initializes the SATA DMA driver
  695. */
  696. static int dma_dwc_init(struct sata_dwc_device *hsdev, int irq)
  697. {
  698. int err;
  699. err = dma_request_interrupts(hsdev, irq);
  700. if (err) {
  701. dev_err(host_pvt.dwc_dev, "%s: dma_request_interrupts returns"
  702. " %d\n", __func__, err);
  703. return err;
  704. }
  705. /* Enabe DMA */
  706. out_le32(&(host_pvt.sata_dma_regs->dma_cfg.low), DMA_EN);
  707. dev_notice(host_pvt.dwc_dev, "DMA initialized\n");
  708. dev_dbg(host_pvt.dwc_dev, "SATA DMA registers=0x%p\n", host_pvt.\
  709. sata_dma_regs);
  710. return 0;
  711. }
  712. static int sata_dwc_scr_read(struct ata_link *link, unsigned int scr, u32 *val)
  713. {
  714. if (scr > SCR_NOTIFICATION) {
  715. dev_err(link->ap->dev, "%s: Incorrect SCR offset 0x%02x\n",
  716. __func__, scr);
  717. return -EINVAL;
  718. }
  719. *val = in_le32((void *)link->ap->ioaddr.scr_addr + (scr * 4));
  720. dev_dbg(link->ap->dev, "%s: id=%d reg=%d val=val=0x%08x\n",
  721. __func__, link->ap->print_id, scr, *val);
  722. return 0;
  723. }
  724. static int sata_dwc_scr_write(struct ata_link *link, unsigned int scr, u32 val)
  725. {
  726. dev_dbg(link->ap->dev, "%s: id=%d reg=%d val=val=0x%08x\n",
  727. __func__, link->ap->print_id, scr, val);
  728. if (scr > SCR_NOTIFICATION) {
  729. dev_err(link->ap->dev, "%s: Incorrect SCR offset 0x%02x\n",
  730. __func__, scr);
  731. return -EINVAL;
  732. }
  733. out_le32((void *)link->ap->ioaddr.scr_addr + (scr * 4), val);
  734. return 0;
  735. }
  736. static u32 core_scr_read(unsigned int scr)
  737. {
  738. return in_le32((void __iomem *)(host_pvt.scr_addr_sstatus) +\
  739. (scr * 4));
  740. }
  741. static void core_scr_write(unsigned int scr, u32 val)
  742. {
  743. out_le32((void __iomem *)(host_pvt.scr_addr_sstatus) + (scr * 4),
  744. val);
  745. }
  746. static void clear_serror(void)
  747. {
  748. u32 val;
  749. val = core_scr_read(SCR_ERROR);
  750. core_scr_write(SCR_ERROR, val);
  751. }
  752. static void clear_interrupt_bit(struct sata_dwc_device *hsdev, u32 bit)
  753. {
  754. out_le32(&hsdev->sata_dwc_regs->intpr,
  755. in_le32(&hsdev->sata_dwc_regs->intpr));
  756. }
  757. static u32 qcmd_tag_to_mask(u8 tag)
  758. {
  759. return 0x00000001 << (tag & 0x1f);
  760. }
  761. /* See ahci.c */
  762. static void sata_dwc_error_intr(struct ata_port *ap,
  763. struct sata_dwc_device *hsdev, uint intpr)
  764. {
  765. struct sata_dwc_device_port *hsdevp = HSDEVP_FROM_AP(ap);
  766. struct ata_eh_info *ehi = &ap->link.eh_info;
  767. unsigned int err_mask = 0, action = 0;
  768. struct ata_queued_cmd *qc;
  769. u32 serror;
  770. u8 status, tag;
  771. u32 err_reg;
  772. ata_ehi_clear_desc(ehi);
  773. serror = core_scr_read(SCR_ERROR);
  774. status = ap->ops->sff_check_status(ap);
  775. err_reg = in_le32(&(host_pvt.sata_dma_regs->interrupt_status.error.\
  776. low));
  777. tag = ap->link.active_tag;
  778. dev_err(ap->dev, "%s SCR_ERROR=0x%08x intpr=0x%08x status=0x%08x "
  779. "dma_intp=%d pending=%d issued=%d dma_err_status=0x%08x\n",
  780. __func__, serror, intpr, status, host_pvt.dma_interrupt_count,
  781. hsdevp->dma_pending[tag], hsdevp->cmd_issued[tag], err_reg);
  782. /* Clear error register and interrupt bit */
  783. clear_serror();
  784. clear_interrupt_bit(hsdev, SATA_DWC_INTPR_ERR);
  785. /* This is the only error happening now. TODO check for exact error */
  786. err_mask |= AC_ERR_HOST_BUS;
  787. action |= ATA_EH_RESET;
  788. /* Pass this on to EH */
  789. ehi->serror |= serror;
  790. ehi->action |= action;
  791. qc = ata_qc_from_tag(ap, tag);
  792. if (qc)
  793. qc->err_mask |= err_mask;
  794. else
  795. ehi->err_mask |= err_mask;
  796. ata_port_abort(ap);
  797. }
  798. /*
  799. * Function : sata_dwc_isr
  800. * arguments : irq, void *dev_instance, struct pt_regs *regs
  801. * Return value : irqreturn_t - status of IRQ
  802. * This Interrupt handler called via port ops registered function.
  803. * .irq_handler = sata_dwc_isr
  804. */
  805. static irqreturn_t sata_dwc_isr(int irq, void *dev_instance)
  806. {
  807. struct ata_host *host = (struct ata_host *)dev_instance;
  808. struct sata_dwc_device *hsdev = HSDEV_FROM_HOST(host);
  809. struct ata_port *ap;
  810. struct ata_queued_cmd *qc;
  811. unsigned long flags;
  812. u8 status, tag;
  813. int handled, num_processed, port = 0;
  814. uint intpr, sactive, sactive2, tag_mask;
  815. struct sata_dwc_device_port *hsdevp;
  816. host_pvt.sata_dwc_sactive_issued = 0;
  817. spin_lock_irqsave(&host->lock, flags);
  818. /* Read the interrupt register */
  819. intpr = in_le32(&hsdev->sata_dwc_regs->intpr);
  820. ap = host->ports[port];
  821. hsdevp = HSDEVP_FROM_AP(ap);
  822. dev_dbg(ap->dev, "%s intpr=0x%08x active_tag=%d\n", __func__, intpr,
  823. ap->link.active_tag);
  824. /* Check for error interrupt */
  825. if (intpr & SATA_DWC_INTPR_ERR) {
  826. sata_dwc_error_intr(ap, hsdev, intpr);
  827. handled = 1;
  828. goto DONE;
  829. }
  830. /* Check for DMA SETUP FIS (FP DMA) interrupt */
  831. if (intpr & SATA_DWC_INTPR_NEWFP) {
  832. clear_interrupt_bit(hsdev, SATA_DWC_INTPR_NEWFP);
  833. tag = (u8)(in_le32(&hsdev->sata_dwc_regs->fptagr));
  834. dev_dbg(ap->dev, "%s: NEWFP tag=%d\n", __func__, tag);
  835. if (hsdevp->cmd_issued[tag] != SATA_DWC_CMD_ISSUED_PEND)
  836. dev_warn(ap->dev, "CMD tag=%d not pending?\n", tag);
  837. host_pvt.sata_dwc_sactive_issued |= qcmd_tag_to_mask(tag);
  838. qc = ata_qc_from_tag(ap, tag);
  839. /*
  840. * Start FP DMA for NCQ command. At this point the tag is the
  841. * active tag. It is the tag that matches the command about to
  842. * be completed.
  843. */
  844. qc->ap->link.active_tag = tag;
  845. sata_dwc_bmdma_start_by_tag(qc, tag);
  846. handled = 1;
  847. goto DONE;
  848. }
  849. sactive = core_scr_read(SCR_ACTIVE);
  850. tag_mask = (host_pvt.sata_dwc_sactive_issued | sactive) ^ sactive;
  851. /* If no sactive issued and tag_mask is zero then this is not NCQ */
  852. if (host_pvt.sata_dwc_sactive_issued == 0 && tag_mask == 0) {
  853. if (ap->link.active_tag == ATA_TAG_POISON)
  854. tag = 0;
  855. else
  856. tag = ap->link.active_tag;
  857. qc = ata_qc_from_tag(ap, tag);
  858. /* DEV interrupt w/ no active qc? */
  859. if (unlikely(!qc || (qc->tf.flags & ATA_TFLAG_POLLING))) {
  860. dev_err(ap->dev, "%s interrupt with no active qc "
  861. "qc=%p\n", __func__, qc);
  862. ap->ops->sff_check_status(ap);
  863. handled = 1;
  864. goto DONE;
  865. }
  866. status = ap->ops->sff_check_status(ap);
  867. qc->ap->link.active_tag = tag;
  868. hsdevp->cmd_issued[tag] = SATA_DWC_CMD_ISSUED_NOT;
  869. if (status & ATA_ERR) {
  870. dev_dbg(ap->dev, "interrupt ATA_ERR (0x%x)\n", status);
  871. sata_dwc_qc_complete(ap, qc, 1);
  872. handled = 1;
  873. goto DONE;
  874. }
  875. dev_dbg(ap->dev, "%s non-NCQ cmd interrupt, protocol: %s\n",
  876. __func__, get_prot_descript(qc->tf.protocol));
  877. DRVSTILLBUSY:
  878. if (ata_is_dma(qc->tf.protocol)) {
  879. /*
  880. * Each DMA transaction produces 2 interrupts. The DMAC
  881. * transfer complete interrupt and the SATA controller
  882. * operation done interrupt. The command should be
  883. * completed only after both interrupts are seen.
  884. */
  885. host_pvt.dma_interrupt_count++;
  886. if (hsdevp->dma_pending[tag] == \
  887. SATA_DWC_DMA_PENDING_NONE) {
  888. dev_err(ap->dev, "%s: DMA not pending "
  889. "intpr=0x%08x status=0x%08x pending"
  890. "=%d\n", __func__, intpr, status,
  891. hsdevp->dma_pending[tag]);
  892. }
  893. if ((host_pvt.dma_interrupt_count % 2) == 0)
  894. sata_dwc_dma_xfer_complete(ap, 1);
  895. } else if (ata_is_pio(qc->tf.protocol)) {
  896. ata_sff_hsm_move(ap, qc, status, 0);
  897. handled = 1;
  898. goto DONE;
  899. } else {
  900. if (unlikely(sata_dwc_qc_complete(ap, qc, 1)))
  901. goto DRVSTILLBUSY;
  902. }
  903. handled = 1;
  904. goto DONE;
  905. }
  906. /*
  907. * This is a NCQ command. At this point we need to figure out for which
  908. * tags we have gotten a completion interrupt. One interrupt may serve
  909. * as completion for more than one operation when commands are queued
  910. * (NCQ). We need to process each completed command.
  911. */
  912. /* process completed commands */
  913. sactive = core_scr_read(SCR_ACTIVE);
  914. tag_mask = (host_pvt.sata_dwc_sactive_issued | sactive) ^ sactive;
  915. if (sactive != 0 || (host_pvt.sata_dwc_sactive_issued) > 1 || \
  916. tag_mask > 1) {
  917. dev_dbg(ap->dev, "%s NCQ:sactive=0x%08x sactive_issued=0x%08x"
  918. "tag_mask=0x%08x\n", __func__, sactive,
  919. host_pvt.sata_dwc_sactive_issued, tag_mask);
  920. }
  921. if ((tag_mask | (host_pvt.sata_dwc_sactive_issued)) != \
  922. (host_pvt.sata_dwc_sactive_issued)) {
  923. dev_warn(ap->dev, "Bad tag mask? sactive=0x%08x "
  924. "(host_pvt.sata_dwc_sactive_issued)=0x%08x tag_mask"
  925. "=0x%08x\n", sactive, host_pvt.sata_dwc_sactive_issued,
  926. tag_mask);
  927. }
  928. /* read just to clear ... not bad if currently still busy */
  929. status = ap->ops->sff_check_status(ap);
  930. dev_dbg(ap->dev, "%s ATA status register=0x%x\n", __func__, status);
  931. tag = 0;
  932. num_processed = 0;
  933. while (tag_mask) {
  934. num_processed++;
  935. while (!(tag_mask & 0x00000001)) {
  936. tag++;
  937. tag_mask <<= 1;
  938. }
  939. tag_mask &= (~0x00000001);
  940. qc = ata_qc_from_tag(ap, tag);
  941. /* To be picked up by completion functions */
  942. qc->ap->link.active_tag = tag;
  943. hsdevp->cmd_issued[tag] = SATA_DWC_CMD_ISSUED_NOT;
  944. /* Let libata/scsi layers handle error */
  945. if (status & ATA_ERR) {
  946. dev_dbg(ap->dev, "%s ATA_ERR (0x%x)\n", __func__,
  947. status);
  948. sata_dwc_qc_complete(ap, qc, 1);
  949. handled = 1;
  950. goto DONE;
  951. }
  952. /* Process completed command */
  953. dev_dbg(ap->dev, "%s NCQ command, protocol: %s\n", __func__,
  954. get_prot_descript(qc->tf.protocol));
  955. if (ata_is_dma(qc->tf.protocol)) {
  956. host_pvt.dma_interrupt_count++;
  957. if (hsdevp->dma_pending[tag] == \
  958. SATA_DWC_DMA_PENDING_NONE)
  959. dev_warn(ap->dev, "%s: DMA not pending?\n",
  960. __func__);
  961. if ((host_pvt.dma_interrupt_count % 2) == 0)
  962. sata_dwc_dma_xfer_complete(ap, 1);
  963. } else {
  964. if (unlikely(sata_dwc_qc_complete(ap, qc, 1)))
  965. goto STILLBUSY;
  966. }
  967. continue;
  968. STILLBUSY:
  969. ap->stats.idle_irq++;
  970. dev_warn(ap->dev, "STILL BUSY IRQ ata%d: irq trap\n",
  971. ap->print_id);
  972. } /* while tag_mask */
  973. /*
  974. * Check to see if any commands completed while we were processing our
  975. * initial set of completed commands (read status clears interrupts,
  976. * so we might miss a completed command interrupt if one came in while
  977. * we were processing --we read status as part of processing a completed
  978. * command).
  979. */
  980. sactive2 = core_scr_read(SCR_ACTIVE);
  981. if (sactive2 != sactive) {
  982. dev_dbg(ap->dev, "More completed - sactive=0x%x sactive2"
  983. "=0x%x\n", sactive, sactive2);
  984. }
  985. handled = 1;
  986. DONE:
  987. spin_unlock_irqrestore(&host->lock, flags);
  988. return IRQ_RETVAL(handled);
  989. }
  990. static void sata_dwc_clear_dmacr(struct sata_dwc_device_port *hsdevp, u8 tag)
  991. {
  992. struct sata_dwc_device *hsdev = HSDEV_FROM_HSDEVP(hsdevp);
  993. if (hsdevp->dma_pending[tag] == SATA_DWC_DMA_PENDING_RX) {
  994. out_le32(&(hsdev->sata_dwc_regs->dmacr),
  995. SATA_DWC_DMACR_RX_CLEAR(
  996. in_le32(&(hsdev->sata_dwc_regs->dmacr))));
  997. } else if (hsdevp->dma_pending[tag] == SATA_DWC_DMA_PENDING_TX) {
  998. out_le32(&(hsdev->sata_dwc_regs->dmacr),
  999. SATA_DWC_DMACR_TX_CLEAR(
  1000. in_le32(&(hsdev->sata_dwc_regs->dmacr))));
  1001. } else {
  1002. /*
  1003. * This should not happen, it indicates the driver is out of
  1004. * sync. If it does happen, clear dmacr anyway.
  1005. */
  1006. dev_err(host_pvt.dwc_dev, "%s DMA protocol RX and"
  1007. "TX DMA not pending tag=0x%02x pending=%d"
  1008. " dmacr: 0x%08x\n", __func__, tag,
  1009. hsdevp->dma_pending[tag],
  1010. in_le32(&(hsdev->sata_dwc_regs->dmacr)));
  1011. out_le32(&(hsdev->sata_dwc_regs->dmacr),
  1012. SATA_DWC_DMACR_TXRXCH_CLEAR);
  1013. }
  1014. }
  1015. static void sata_dwc_dma_xfer_complete(struct ata_port *ap, u32 check_status)
  1016. {
  1017. struct ata_queued_cmd *qc;
  1018. struct sata_dwc_device_port *hsdevp = HSDEVP_FROM_AP(ap);
  1019. struct sata_dwc_device *hsdev = HSDEV_FROM_AP(ap);
  1020. u8 tag = 0;
  1021. tag = ap->link.active_tag;
  1022. qc = ata_qc_from_tag(ap, tag);
  1023. if (!qc) {
  1024. dev_err(ap->dev, "failed to get qc");
  1025. return;
  1026. }
  1027. #ifdef DEBUG_NCQ
  1028. if (tag > 0) {
  1029. dev_info(ap->dev, "%s tag=%u cmd=0x%02x dma dir=%s proto=%s "
  1030. "dmacr=0x%08x\n", __func__, qc->tag, qc->tf.command,
  1031. get_dma_dir_descript(qc->dma_dir),
  1032. get_prot_descript(qc->tf.protocol),
  1033. in_le32(&(hsdev->sata_dwc_regs->dmacr)));
  1034. }
  1035. #endif
  1036. if (ata_is_dma(qc->tf.protocol)) {
  1037. if (hsdevp->dma_pending[tag] == SATA_DWC_DMA_PENDING_NONE) {
  1038. dev_err(ap->dev, "%s DMA protocol RX and TX DMA not "
  1039. "pending dmacr: 0x%08x\n", __func__,
  1040. in_le32(&(hsdev->sata_dwc_regs->dmacr)));
  1041. }
  1042. hsdevp->dma_pending[tag] = SATA_DWC_DMA_PENDING_NONE;
  1043. sata_dwc_qc_complete(ap, qc, check_status);
  1044. ap->link.active_tag = ATA_TAG_POISON;
  1045. } else {
  1046. sata_dwc_qc_complete(ap, qc, check_status);
  1047. }
  1048. }
  1049. static int sata_dwc_qc_complete(struct ata_port *ap, struct ata_queued_cmd *qc,
  1050. u32 check_status)
  1051. {
  1052. u8 status = 0;
  1053. u32 mask = 0x0;
  1054. u8 tag = qc->tag;
  1055. struct sata_dwc_device_port *hsdevp = HSDEVP_FROM_AP(ap);
  1056. host_pvt.sata_dwc_sactive_queued = 0;
  1057. dev_dbg(ap->dev, "%s checkstatus? %x\n", __func__, check_status);
  1058. if (hsdevp->dma_pending[tag] == SATA_DWC_DMA_PENDING_TX)
  1059. dev_err(ap->dev, "TX DMA PENDING\n");
  1060. else if (hsdevp->dma_pending[tag] == SATA_DWC_DMA_PENDING_RX)
  1061. dev_err(ap->dev, "RX DMA PENDING\n");
  1062. dev_dbg(ap->dev, "QC complete cmd=0x%02x status=0x%02x ata%u:"
  1063. " protocol=%d\n", qc->tf.command, status, ap->print_id,
  1064. qc->tf.protocol);
  1065. /* clear active bit */
  1066. mask = (~(qcmd_tag_to_mask(tag)));
  1067. host_pvt.sata_dwc_sactive_queued = (host_pvt.sata_dwc_sactive_queued) \
  1068. & mask;
  1069. host_pvt.sata_dwc_sactive_issued = (host_pvt.sata_dwc_sactive_issued) \
  1070. & mask;
  1071. ata_qc_complete(qc);
  1072. return 0;
  1073. }
  1074. static void sata_dwc_enable_interrupts(struct sata_dwc_device *hsdev)
  1075. {
  1076. /* Enable selective interrupts by setting the interrupt maskregister*/
  1077. out_le32(&hsdev->sata_dwc_regs->intmr,
  1078. SATA_DWC_INTMR_ERRM |
  1079. SATA_DWC_INTMR_NEWFPM |
  1080. SATA_DWC_INTMR_PMABRTM |
  1081. SATA_DWC_INTMR_DMATM);
  1082. /*
  1083. * Unmask the error bits that should trigger an error interrupt by
  1084. * setting the error mask register.
  1085. */
  1086. out_le32(&hsdev->sata_dwc_regs->errmr, SATA_DWC_SERROR_ERR_BITS);
  1087. dev_dbg(host_pvt.dwc_dev, "%s: INTMR = 0x%08x, ERRMR = 0x%08x\n",
  1088. __func__, in_le32(&hsdev->sata_dwc_regs->intmr),
  1089. in_le32(&hsdev->sata_dwc_regs->errmr));
  1090. }
  1091. static void sata_dwc_setup_port(struct ata_ioports *port, unsigned long base)
  1092. {
  1093. port->cmd_addr = (void *)base + 0x00;
  1094. port->data_addr = (void *)base + 0x00;
  1095. port->error_addr = (void *)base + 0x04;
  1096. port->feature_addr = (void *)base + 0x04;
  1097. port->nsect_addr = (void *)base + 0x08;
  1098. port->lbal_addr = (void *)base + 0x0c;
  1099. port->lbam_addr = (void *)base + 0x10;
  1100. port->lbah_addr = (void *)base + 0x14;
  1101. port->device_addr = (void *)base + 0x18;
  1102. port->command_addr = (void *)base + 0x1c;
  1103. port->status_addr = (void *)base + 0x1c;
  1104. port->altstatus_addr = (void *)base + 0x20;
  1105. port->ctl_addr = (void *)base + 0x20;
  1106. }
  1107. /*
  1108. * Function : sata_dwc_port_start
  1109. * arguments : struct ata_ioports *port
  1110. * Return value : returns 0 if success, error code otherwise
  1111. * This function allocates the scatter gather LLI table for AHB DMA
  1112. */
  1113. static int sata_dwc_port_start(struct ata_port *ap)
  1114. {
  1115. int err = 0;
  1116. struct sata_dwc_device *hsdev;
  1117. struct sata_dwc_device_port *hsdevp = NULL;
  1118. struct device *pdev;
  1119. int i;
  1120. hsdev = HSDEV_FROM_AP(ap);
  1121. dev_dbg(ap->dev, "%s: port_no=%d\n", __func__, ap->port_no);
  1122. hsdev->host = ap->host;
  1123. pdev = ap->host->dev;
  1124. if (!pdev) {
  1125. dev_err(ap->dev, "%s: no ap->host->dev\n", __func__);
  1126. err = -ENODEV;
  1127. goto CLEANUP;
  1128. }
  1129. /* Allocate Port Struct */
  1130. hsdevp = kzalloc(sizeof(*hsdevp), GFP_KERNEL);
  1131. if (!hsdevp) {
  1132. dev_err(ap->dev, "%s: kmalloc failed for hsdevp\n", __func__);
  1133. err = -ENOMEM;
  1134. goto CLEANUP;
  1135. }
  1136. hsdevp->hsdev = hsdev;
  1137. for (i = 0; i < SATA_DWC_QCMD_MAX; i++)
  1138. hsdevp->cmd_issued[i] = SATA_DWC_CMD_ISSUED_NOT;
  1139. ap->bmdma_prd = 0; /* set these so libata doesn't use them */
  1140. ap->bmdma_prd_dma = 0;
  1141. /*
  1142. * DMA - Assign scatter gather LLI table. We can't use the libata
  1143. * version since it's PRD is IDE PCI specific.
  1144. */
  1145. for (i = 0; i < SATA_DWC_QCMD_MAX; i++) {
  1146. hsdevp->llit[i] = dma_alloc_coherent(pdev,
  1147. SATA_DWC_DMAC_LLI_TBL_SZ,
  1148. &(hsdevp->llit_dma[i]),
  1149. GFP_ATOMIC);
  1150. if (!hsdevp->llit[i]) {
  1151. dev_err(ap->dev, "%s: dma_alloc_coherent failed\n",
  1152. __func__);
  1153. err = -ENOMEM;
  1154. goto CLEANUP_ALLOC;
  1155. }
  1156. }
  1157. if (ap->port_no == 0) {
  1158. dev_dbg(ap->dev, "%s: clearing TXCHEN, RXCHEN in DMAC\n",
  1159. __func__);
  1160. out_le32(&hsdev->sata_dwc_regs->dmacr,
  1161. SATA_DWC_DMACR_TXRXCH_CLEAR);
  1162. dev_dbg(ap->dev, "%s: setting burst size in DBTSR\n",
  1163. __func__);
  1164. out_le32(&hsdev->sata_dwc_regs->dbtsr,
  1165. (SATA_DWC_DBTSR_MWR(AHB_DMA_BRST_DFLT) |
  1166. SATA_DWC_DBTSR_MRD(AHB_DMA_BRST_DFLT)));
  1167. }
  1168. /* Clear any error bits before libata starts issuing commands */
  1169. clear_serror();
  1170. ap->private_data = hsdevp;
  1171. dev_dbg(ap->dev, "%s: done\n", __func__);
  1172. return 0;
  1173. CLEANUP_ALLOC:
  1174. kfree(hsdevp);
  1175. CLEANUP:
  1176. dev_dbg(ap->dev, "%s: fail. ap->id = %d\n", __func__, ap->print_id);
  1177. return err;
  1178. }
  1179. static void sata_dwc_port_stop(struct ata_port *ap)
  1180. {
  1181. int i;
  1182. struct sata_dwc_device *hsdev = HSDEV_FROM_AP(ap);
  1183. struct sata_dwc_device_port *hsdevp = HSDEVP_FROM_AP(ap);
  1184. dev_dbg(ap->dev, "%s: ap->id = %d\n", __func__, ap->print_id);
  1185. if (hsdevp && hsdev) {
  1186. /* deallocate LLI table */
  1187. for (i = 0; i < SATA_DWC_QCMD_MAX; i++) {
  1188. dma_free_coherent(ap->host->dev,
  1189. SATA_DWC_DMAC_LLI_TBL_SZ,
  1190. hsdevp->llit[i], hsdevp->llit_dma[i]);
  1191. }
  1192. kfree(hsdevp);
  1193. }
  1194. ap->private_data = NULL;
  1195. }
  1196. /*
  1197. * Function : sata_dwc_exec_command_by_tag
  1198. * arguments : ata_port *ap, ata_taskfile *tf, u8 tag, u32 cmd_issued
  1199. * Return value : None
  1200. * This function keeps track of individual command tag ids and calls
  1201. * ata_exec_command in libata
  1202. */
  1203. static void sata_dwc_exec_command_by_tag(struct ata_port *ap,
  1204. struct ata_taskfile *tf,
  1205. u8 tag, u32 cmd_issued)
  1206. {
  1207. unsigned long flags;
  1208. struct sata_dwc_device_port *hsdevp = HSDEVP_FROM_AP(ap);
  1209. dev_dbg(ap->dev, "%s cmd(0x%02x): %s tag=%d\n", __func__, tf->command,
  1210. ata_get_cmd_descript(tf->command), tag);
  1211. spin_lock_irqsave(&ap->host->lock, flags);
  1212. hsdevp->cmd_issued[tag] = cmd_issued;
  1213. spin_unlock_irqrestore(&ap->host->lock, flags);
  1214. /*
  1215. * Clear SError before executing a new command.
  1216. * sata_dwc_scr_write and read can not be used here. Clearing the PM
  1217. * managed SError register for the disk needs to be done before the
  1218. * task file is loaded.
  1219. */
  1220. clear_serror();
  1221. ata_sff_exec_command(ap, tf);
  1222. }
  1223. static void sata_dwc_bmdma_setup_by_tag(struct ata_queued_cmd *qc, u8 tag)
  1224. {
  1225. sata_dwc_exec_command_by_tag(qc->ap, &qc->tf, tag,
  1226. SATA_DWC_CMD_ISSUED_PEND);
  1227. }
  1228. static void sata_dwc_bmdma_setup(struct ata_queued_cmd *qc)
  1229. {
  1230. u8 tag = qc->tag;
  1231. if (ata_is_ncq(qc->tf.protocol)) {
  1232. dev_dbg(qc->ap->dev, "%s: ap->link.sactive=0x%08x tag=%d\n",
  1233. __func__, qc->ap->link.sactive, tag);
  1234. } else {
  1235. tag = 0;
  1236. }
  1237. sata_dwc_bmdma_setup_by_tag(qc, tag);
  1238. }
  1239. static void sata_dwc_bmdma_start_by_tag(struct ata_queued_cmd *qc, u8 tag)
  1240. {
  1241. int start_dma;
  1242. u32 reg, dma_chan;
  1243. struct sata_dwc_device *hsdev = HSDEV_FROM_QC(qc);
  1244. struct ata_port *ap = qc->ap;
  1245. struct sata_dwc_device_port *hsdevp = HSDEVP_FROM_AP(ap);
  1246. int dir = qc->dma_dir;
  1247. dma_chan = hsdevp->dma_chan[tag];
  1248. if (hsdevp->cmd_issued[tag] != SATA_DWC_CMD_ISSUED_NOT) {
  1249. start_dma = 1;
  1250. if (dir == DMA_TO_DEVICE)
  1251. hsdevp->dma_pending[tag] = SATA_DWC_DMA_PENDING_TX;
  1252. else
  1253. hsdevp->dma_pending[tag] = SATA_DWC_DMA_PENDING_RX;
  1254. } else {
  1255. dev_err(ap->dev, "%s: Command not pending cmd_issued=%d "
  1256. "(tag=%d) DMA NOT started\n", __func__,
  1257. hsdevp->cmd_issued[tag], tag);
  1258. start_dma = 0;
  1259. }
  1260. dev_dbg(ap->dev, "%s qc=%p tag: %x cmd: 0x%02x dma_dir: %s "
  1261. "start_dma? %x\n", __func__, qc, tag, qc->tf.command,
  1262. get_dma_dir_descript(qc->dma_dir), start_dma);
  1263. sata_dwc_tf_dump(&(qc->tf));
  1264. if (start_dma) {
  1265. reg = core_scr_read(SCR_ERROR);
  1266. if (reg & SATA_DWC_SERROR_ERR_BITS) {
  1267. dev_err(ap->dev, "%s: ****** SError=0x%08x ******\n",
  1268. __func__, reg);
  1269. }
  1270. if (dir == DMA_TO_DEVICE)
  1271. out_le32(&hsdev->sata_dwc_regs->dmacr,
  1272. SATA_DWC_DMACR_TXCHEN);
  1273. else
  1274. out_le32(&hsdev->sata_dwc_regs->dmacr,
  1275. SATA_DWC_DMACR_RXCHEN);
  1276. /* Enable AHB DMA transfer on the specified channel */
  1277. dma_dwc_xfer_start(dma_chan);
  1278. }
  1279. }
  1280. static void sata_dwc_bmdma_start(struct ata_queued_cmd *qc)
  1281. {
  1282. u8 tag = qc->tag;
  1283. if (ata_is_ncq(qc->tf.protocol)) {
  1284. dev_dbg(qc->ap->dev, "%s: ap->link.sactive=0x%08x tag=%d\n",
  1285. __func__, qc->ap->link.sactive, tag);
  1286. } else {
  1287. tag = 0;
  1288. }
  1289. dev_dbg(qc->ap->dev, "%s\n", __func__);
  1290. sata_dwc_bmdma_start_by_tag(qc, tag);
  1291. }
  1292. /*
  1293. * Function : sata_dwc_qc_prep_by_tag
  1294. * arguments : ata_queued_cmd *qc, u8 tag
  1295. * Return value : None
  1296. * qc_prep for a particular queued command based on tag
  1297. */
  1298. static void sata_dwc_qc_prep_by_tag(struct ata_queued_cmd *qc, u8 tag)
  1299. {
  1300. struct scatterlist *sg = qc->sg;
  1301. struct ata_port *ap = qc->ap;
  1302. int dma_chan;
  1303. struct sata_dwc_device *hsdev = HSDEV_FROM_AP(ap);
  1304. struct sata_dwc_device_port *hsdevp = HSDEVP_FROM_AP(ap);
  1305. dev_dbg(ap->dev, "%s: port=%d dma dir=%s n_elem=%d\n",
  1306. __func__, ap->port_no, get_dma_dir_descript(qc->dma_dir),
  1307. qc->n_elem);
  1308. dma_chan = dma_dwc_xfer_setup(sg, qc->n_elem, hsdevp->llit[tag],
  1309. hsdevp->llit_dma[tag],
  1310. (void *__iomem)(&hsdev->sata_dwc_regs->\
  1311. dmadr), qc->dma_dir);
  1312. if (dma_chan < 0) {
  1313. dev_err(ap->dev, "%s: dma_dwc_xfer_setup returns err %d\n",
  1314. __func__, dma_chan);
  1315. return;
  1316. }
  1317. hsdevp->dma_chan[tag] = dma_chan;
  1318. }
  1319. static unsigned int sata_dwc_qc_issue(struct ata_queued_cmd *qc)
  1320. {
  1321. u32 sactive;
  1322. u8 tag = qc->tag;
  1323. struct ata_port *ap = qc->ap;
  1324. #ifdef DEBUG_NCQ
  1325. if (qc->tag > 0 || ap->link.sactive > 1)
  1326. dev_info(ap->dev, "%s ap id=%d cmd(0x%02x)=%s qc tag=%d "
  1327. "prot=%s ap active_tag=0x%08x ap sactive=0x%08x\n",
  1328. __func__, ap->print_id, qc->tf.command,
  1329. ata_get_cmd_descript(qc->tf.command),
  1330. qc->tag, get_prot_descript(qc->tf.protocol),
  1331. ap->link.active_tag, ap->link.sactive);
  1332. #endif
  1333. if (!ata_is_ncq(qc->tf.protocol))
  1334. tag = 0;
  1335. sata_dwc_qc_prep_by_tag(qc, tag);
  1336. if (ata_is_ncq(qc->tf.protocol)) {
  1337. sactive = core_scr_read(SCR_ACTIVE);
  1338. sactive |= (0x00000001 << tag);
  1339. core_scr_write(SCR_ACTIVE, sactive);
  1340. dev_dbg(qc->ap->dev, "%s: tag=%d ap->link.sactive = 0x%08x "
  1341. "sactive=0x%08x\n", __func__, tag, qc->ap->link.sactive,
  1342. sactive);
  1343. ap->ops->sff_tf_load(ap, &qc->tf);
  1344. sata_dwc_exec_command_by_tag(ap, &qc->tf, qc->tag,
  1345. SATA_DWC_CMD_ISSUED_PEND);
  1346. } else {
  1347. ata_sff_qc_issue(qc);
  1348. }
  1349. return 0;
  1350. }
  1351. /*
  1352. * Function : sata_dwc_qc_prep
  1353. * arguments : ata_queued_cmd *qc
  1354. * Return value : None
  1355. * qc_prep for a particular queued command
  1356. */
  1357. static void sata_dwc_qc_prep(struct ata_queued_cmd *qc)
  1358. {
  1359. if ((qc->dma_dir == DMA_NONE) || (qc->tf.protocol == ATA_PROT_PIO))
  1360. return;
  1361. #ifdef DEBUG_NCQ
  1362. if (qc->tag > 0)
  1363. dev_info(qc->ap->dev, "%s: qc->tag=%d ap->active_tag=0x%08x\n",
  1364. __func__, qc->tag, qc->ap->link.active_tag);
  1365. return ;
  1366. #endif
  1367. }
  1368. static void sata_dwc_error_handler(struct ata_port *ap)
  1369. {
  1370. ap->link.flags |= ATA_LFLAG_NO_HRST;
  1371. ata_sff_error_handler(ap);
  1372. }
  1373. /*
  1374. * scsi mid-layer and libata interface structures
  1375. */
  1376. static struct scsi_host_template sata_dwc_sht = {
  1377. ATA_NCQ_SHT(DRV_NAME),
  1378. /*
  1379. * test-only: Currently this driver doesn't handle NCQ
  1380. * correctly. We enable NCQ but set the queue depth to a
  1381. * max of 1. This will get fixed in in a future release.
  1382. */
  1383. .sg_tablesize = LIBATA_MAX_PRD,
  1384. .can_queue = ATA_DEF_QUEUE, /* ATA_MAX_QUEUE */
  1385. .dma_boundary = ATA_DMA_BOUNDARY,
  1386. };
  1387. static struct ata_port_operations sata_dwc_ops = {
  1388. .inherits = &ata_sff_port_ops,
  1389. .error_handler = sata_dwc_error_handler,
  1390. .qc_prep = sata_dwc_qc_prep,
  1391. .qc_issue = sata_dwc_qc_issue,
  1392. .scr_read = sata_dwc_scr_read,
  1393. .scr_write = sata_dwc_scr_write,
  1394. .port_start = sata_dwc_port_start,
  1395. .port_stop = sata_dwc_port_stop,
  1396. .bmdma_setup = sata_dwc_bmdma_setup,
  1397. .bmdma_start = sata_dwc_bmdma_start,
  1398. };
  1399. static const struct ata_port_info sata_dwc_port_info[] = {
  1400. {
  1401. .flags = ATA_FLAG_SATA | ATA_FLAG_NCQ,
  1402. .pio_mask = ATA_PIO4,
  1403. .udma_mask = ATA_UDMA6,
  1404. .port_ops = &sata_dwc_ops,
  1405. },
  1406. };
  1407. static int sata_dwc_probe(struct platform_device *ofdev)
  1408. {
  1409. struct sata_dwc_device *hsdev;
  1410. u32 idr, versionr;
  1411. char *ver = (char *)&versionr;
  1412. u8 *base = NULL;
  1413. int err = 0;
  1414. int irq;
  1415. struct ata_host *host;
  1416. struct ata_port_info pi = sata_dwc_port_info[0];
  1417. const struct ata_port_info *ppi[] = { &pi, NULL };
  1418. /* Allocate DWC SATA device */
  1419. hsdev = kzalloc(sizeof(*hsdev), GFP_KERNEL);
  1420. if (hsdev == NULL) {
  1421. dev_err(&ofdev->dev, "kmalloc failed for hsdev\n");
  1422. err = -ENOMEM;
  1423. goto error;
  1424. }
  1425. /* Ioremap SATA registers */
  1426. base = of_iomap(ofdev->dev.of_node, 0);
  1427. if (!base) {
  1428. dev_err(&ofdev->dev, "ioremap failed for SATA register"
  1429. " address\n");
  1430. err = -ENODEV;
  1431. goto error_kmalloc;
  1432. }
  1433. hsdev->reg_base = base;
  1434. dev_dbg(&ofdev->dev, "ioremap done for SATA register address\n");
  1435. /* Synopsys DWC SATA specific Registers */
  1436. hsdev->sata_dwc_regs = (void *__iomem)(base + SATA_DWC_REG_OFFSET);
  1437. /* Allocate and fill host */
  1438. host = ata_host_alloc_pinfo(&ofdev->dev, ppi, SATA_DWC_MAX_PORTS);
  1439. if (!host) {
  1440. dev_err(&ofdev->dev, "ata_host_alloc_pinfo failed\n");
  1441. err = -ENOMEM;
  1442. goto error_iomap;
  1443. }
  1444. host->private_data = hsdev;
  1445. /* Setup port */
  1446. host->ports[0]->ioaddr.cmd_addr = base;
  1447. host->ports[0]->ioaddr.scr_addr = base + SATA_DWC_SCR_OFFSET;
  1448. host_pvt.scr_addr_sstatus = base + SATA_DWC_SCR_OFFSET;
  1449. sata_dwc_setup_port(&host->ports[0]->ioaddr, (unsigned long)base);
  1450. /* Read the ID and Version Registers */
  1451. idr = in_le32(&hsdev->sata_dwc_regs->idr);
  1452. versionr = in_le32(&hsdev->sata_dwc_regs->versionr);
  1453. dev_notice(&ofdev->dev, "id %d, controller version %c.%c%c\n",
  1454. idr, ver[0], ver[1], ver[2]);
  1455. /* Get SATA DMA interrupt number */
  1456. irq = irq_of_parse_and_map(ofdev->dev.of_node, 1);
  1457. if (irq == NO_IRQ) {
  1458. dev_err(&ofdev->dev, "no SATA DMA irq\n");
  1459. err = -ENODEV;
  1460. goto error_iomap;
  1461. }
  1462. /* Get physical SATA DMA register base address */
  1463. host_pvt.sata_dma_regs = of_iomap(ofdev->dev.of_node, 1);
  1464. if (!(host_pvt.sata_dma_regs)) {
  1465. dev_err(&ofdev->dev, "ioremap failed for AHBDMA register"
  1466. " address\n");
  1467. err = -ENODEV;
  1468. goto error_iomap;
  1469. }
  1470. /* Save dev for later use in dev_xxx() routines */
  1471. host_pvt.dwc_dev = &ofdev->dev;
  1472. /* Initialize AHB DMAC */
  1473. err = dma_dwc_init(hsdev, irq);
  1474. if (err)
  1475. goto error_dma_iomap;
  1476. /* Enable SATA Interrupts */
  1477. sata_dwc_enable_interrupts(hsdev);
  1478. /* Get SATA interrupt number */
  1479. irq = irq_of_parse_and_map(ofdev->dev.of_node, 0);
  1480. if (irq == NO_IRQ) {
  1481. dev_err(&ofdev->dev, "no SATA DMA irq\n");
  1482. err = -ENODEV;
  1483. goto error_out;
  1484. }
  1485. /*
  1486. * Now, register with libATA core, this will also initiate the
  1487. * device discovery process, invoking our port_start() handler &
  1488. * error_handler() to execute a dummy Softreset EH session
  1489. */
  1490. err = ata_host_activate(host, irq, sata_dwc_isr, 0, &sata_dwc_sht);
  1491. if (err)
  1492. dev_err(&ofdev->dev, "failed to activate host");
  1493. dev_set_drvdata(&ofdev->dev, host);
  1494. return 0;
  1495. error_out:
  1496. /* Free SATA DMA resources */
  1497. dma_dwc_exit(hsdev);
  1498. error_dma_iomap:
  1499. iounmap((void __iomem *)host_pvt.sata_dma_regs);
  1500. error_iomap:
  1501. iounmap(base);
  1502. error_kmalloc:
  1503. kfree(hsdev);
  1504. error:
  1505. return err;
  1506. }
  1507. static int sata_dwc_remove(struct platform_device *ofdev)
  1508. {
  1509. struct device *dev = &ofdev->dev;
  1510. struct ata_host *host = dev_get_drvdata(dev);
  1511. struct sata_dwc_device *hsdev = host->private_data;
  1512. ata_host_detach(host);
  1513. dev_set_drvdata(dev, NULL);
  1514. /* Free SATA DMA resources */
  1515. dma_dwc_exit(hsdev);
  1516. iounmap((void __iomem *)host_pvt.sata_dma_regs);
  1517. iounmap(hsdev->reg_base);
  1518. kfree(hsdev);
  1519. kfree(host);
  1520. dev_dbg(&ofdev->dev, "done\n");
  1521. return 0;
  1522. }
  1523. static const struct of_device_id sata_dwc_match[] = {
  1524. { .compatible = "amcc,sata-460ex", },
  1525. {}
  1526. };
  1527. MODULE_DEVICE_TABLE(of, sata_dwc_match);
  1528. static struct platform_driver sata_dwc_driver = {
  1529. .driver = {
  1530. .name = DRV_NAME,
  1531. .owner = THIS_MODULE,
  1532. .of_match_table = sata_dwc_match,
  1533. },
  1534. .probe = sata_dwc_probe,
  1535. .remove = sata_dwc_remove,
  1536. };
  1537. module_platform_driver(sata_dwc_driver);
  1538. MODULE_LICENSE("GPL");
  1539. MODULE_AUTHOR("Mark Miesfeld <mmiesfeld@amcc.com>");
  1540. MODULE_DESCRIPTION("DesignWare Cores SATA controller low lever driver");
  1541. MODULE_VERSION(DRV_VERSION);