sata_dwc_460ex.c 51 KB

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