sata_dwc_460ex.c 50 KB

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