sata_dwc_460ex.c 51 KB

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