sata_dwc_460ex.c 49 KB

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