sata_dwc_460ex.c 51 KB

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