sata_dwc_460ex.c 50 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756
  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. const struct of_device_id *match)
  1374. {
  1375. struct sata_dwc_device *hsdev;
  1376. u32 idr, versionr;
  1377. char *ver = (char *)&versionr;
  1378. u8 *base = NULL;
  1379. int err = 0;
  1380. int irq, rc;
  1381. struct ata_host *host;
  1382. struct ata_port_info pi = sata_dwc_port_info[0];
  1383. const struct ata_port_info *ppi[] = { &pi, NULL };
  1384. /* Allocate DWC SATA device */
  1385. hsdev = kmalloc(sizeof(*hsdev), GFP_KERNEL);
  1386. if (hsdev == NULL) {
  1387. dev_err(&ofdev->dev, "kmalloc failed for hsdev\n");
  1388. err = -ENOMEM;
  1389. goto error_out;
  1390. }
  1391. memset(hsdev, 0, sizeof(*hsdev));
  1392. /* Ioremap SATA registers */
  1393. base = of_iomap(ofdev->dev.of_node, 0);
  1394. if (!base) {
  1395. dev_err(&ofdev->dev, "ioremap failed for SATA register"
  1396. " address\n");
  1397. err = -ENODEV;
  1398. goto error_out;
  1399. }
  1400. hsdev->reg_base = base;
  1401. dev_dbg(&ofdev->dev, "ioremap done for SATA register address\n");
  1402. /* Synopsys DWC SATA specific Registers */
  1403. hsdev->sata_dwc_regs = (void *__iomem)(base + SATA_DWC_REG_OFFSET);
  1404. /* Allocate and fill host */
  1405. host = ata_host_alloc_pinfo(&ofdev->dev, ppi, SATA_DWC_MAX_PORTS);
  1406. if (!host) {
  1407. dev_err(&ofdev->dev, "ata_host_alloc_pinfo failed\n");
  1408. err = -ENOMEM;
  1409. goto error_out;
  1410. }
  1411. host->private_data = hsdev;
  1412. /* Setup port */
  1413. host->ports[0]->ioaddr.cmd_addr = base;
  1414. host->ports[0]->ioaddr.scr_addr = base + SATA_DWC_SCR_OFFSET;
  1415. host_pvt.scr_addr_sstatus = base + SATA_DWC_SCR_OFFSET;
  1416. sata_dwc_setup_port(&host->ports[0]->ioaddr, (unsigned long)base);
  1417. /* Read the ID and Version Registers */
  1418. idr = in_le32(&hsdev->sata_dwc_regs->idr);
  1419. versionr = in_le32(&hsdev->sata_dwc_regs->versionr);
  1420. dev_notice(&ofdev->dev, "id %d, controller version %c.%c%c\n",
  1421. idr, ver[0], ver[1], ver[2]);
  1422. /* Get SATA DMA interrupt number */
  1423. irq = irq_of_parse_and_map(ofdev->dev.of_node, 1);
  1424. if (irq == NO_IRQ) {
  1425. dev_err(&ofdev->dev, "no SATA DMA irq\n");
  1426. err = -ENODEV;
  1427. goto error_out;
  1428. }
  1429. /* Get physical SATA DMA register base address */
  1430. host_pvt.sata_dma_regs = of_iomap(ofdev->dev.of_node, 1);
  1431. if (!(host_pvt.sata_dma_regs)) {
  1432. dev_err(&ofdev->dev, "ioremap failed for AHBDMA register"
  1433. " address\n");
  1434. err = -ENODEV;
  1435. goto error_out;
  1436. }
  1437. /* Save dev for later use in dev_xxx() routines */
  1438. host_pvt.dwc_dev = &ofdev->dev;
  1439. /* Initialize AHB DMAC */
  1440. dma_dwc_init(hsdev, irq);
  1441. /* Enable SATA Interrupts */
  1442. sata_dwc_enable_interrupts(hsdev);
  1443. /* Get SATA interrupt number */
  1444. irq = irq_of_parse_and_map(ofdev->dev.of_node, 0);
  1445. if (irq == NO_IRQ) {
  1446. dev_err(&ofdev->dev, "no SATA DMA irq\n");
  1447. err = -ENODEV;
  1448. goto error_out;
  1449. }
  1450. /*
  1451. * Now, register with libATA core, this will also initiate the
  1452. * device discovery process, invoking our port_start() handler &
  1453. * error_handler() to execute a dummy Softreset EH session
  1454. */
  1455. rc = ata_host_activate(host, irq, sata_dwc_isr, 0, &sata_dwc_sht);
  1456. if (rc != 0)
  1457. dev_err(&ofdev->dev, "failed to activate host");
  1458. dev_set_drvdata(&ofdev->dev, host);
  1459. return 0;
  1460. error_out:
  1461. /* Free SATA DMA resources */
  1462. dma_dwc_exit(hsdev);
  1463. if (base)
  1464. iounmap(base);
  1465. return err;
  1466. }
  1467. static int sata_dwc_remove(struct platform_device *ofdev)
  1468. {
  1469. struct device *dev = &ofdev->dev;
  1470. struct ata_host *host = dev_get_drvdata(dev);
  1471. struct sata_dwc_device *hsdev = host->private_data;
  1472. ata_host_detach(host);
  1473. dev_set_drvdata(dev, NULL);
  1474. /* Free SATA DMA resources */
  1475. dma_dwc_exit(hsdev);
  1476. iounmap(hsdev->reg_base);
  1477. kfree(hsdev);
  1478. kfree(host);
  1479. dev_dbg(&ofdev->dev, "done\n");
  1480. return 0;
  1481. }
  1482. static const struct of_device_id sata_dwc_match[] = {
  1483. { .compatible = "amcc,sata-460ex", },
  1484. {}
  1485. };
  1486. MODULE_DEVICE_TABLE(of, sata_dwc_match);
  1487. static struct of_platform_driver sata_dwc_driver = {
  1488. .driver = {
  1489. .name = DRV_NAME,
  1490. .owner = THIS_MODULE,
  1491. .of_match_table = sata_dwc_match,
  1492. },
  1493. .probe = sata_dwc_probe,
  1494. .remove = sata_dwc_remove,
  1495. };
  1496. static int __init sata_dwc_init(void)
  1497. {
  1498. return of_register_platform_driver(&sata_dwc_driver);
  1499. }
  1500. static void __exit sata_dwc_exit(void)
  1501. {
  1502. of_unregister_platform_driver(&sata_dwc_driver);
  1503. }
  1504. module_init(sata_dwc_init);
  1505. module_exit(sata_dwc_exit);
  1506. MODULE_LICENSE("GPL");
  1507. MODULE_AUTHOR("Mark Miesfeld <mmiesfeld@amcc.com>");
  1508. MODULE_DESCRIPTION("DesignWare Cores SATA controller low lever driver");
  1509. MODULE_VERSION(DRV_VERSION);