blob: f0ef844428bb4dd794c985ef8fcb8706c1bde070 [file] [log] [blame]
Thomas Gleixner2874c5f2019-05-27 08:55:01 +02001// SPDX-License-Identifier: GPL-2.0-or-later
Rupjyoti Sarmah62936002010-07-06 16:36:03 +05302/*
3 * drivers/ata/sata_dwc_460ex.c
4 *
5 * Synopsys DesignWare Cores (DWC) SATA host driver
6 *
7 * Author: Mark Miesfeld <mmiesfeld@amcc.com>
8 *
9 * Ported from 2.6.19.2 to 2.6.25/26 by Stefan Roese <sr@denx.de>
10 * Copyright 2008 DENX Software Engineering
11 *
12 * Based on versions provided by AMCC and Synopsys which are:
13 * Copyright 2006 Applied Micro Circuits Corporation
14 * COPYRIGHT (C) 2005 SYNOPSYS, INC. ALL RIGHTS RESERVED
Rupjyoti Sarmah62936002010-07-06 16:36:03 +053015 */
16
17#ifdef CONFIG_SATA_DWC_DEBUG
18#define DEBUG
19#endif
20
21#ifdef CONFIG_SATA_DWC_VDEBUG
22#define VERBOSE_DEBUG
23#define DEBUG_NCQ
24#endif
25
26#include <linux/kernel.h>
27#include <linux/module.h>
Rupjyoti Sarmah62936002010-07-06 16:36:03 +053028#include <linux/device.h>
Mans Rullgard50b43372016-04-26 12:03:10 +030029#include <linux/dmaengine.h>
Rob Herringc11eede2013-11-10 23:19:08 -060030#include <linux/of_address.h>
31#include <linux/of_irq.h>
Rupjyoti Sarmah62936002010-07-06 16:36:03 +053032#include <linux/of_platform.h>
33#include <linux/platform_device.h>
Mans Rullgard0f48deb2016-04-26 12:03:11 +030034#include <linux/phy/phy.h>
Rupjyoti Sarmah62936002010-07-06 16:36:03 +053035#include <linux/libata.h>
36#include <linux/slab.h>
Andy Shevchenko8b344482015-03-03 22:41:21 +020037
Rupjyoti Sarmah62936002010-07-06 16:36:03 +053038#include "libata.h"
39
40#include <scsi/scsi_host.h>
41#include <scsi/scsi_cmnd.h>
42
Sergei Shtylyovc2119622011-01-28 21:55:55 +030043/* These two are defined in "libata.h" */
44#undef DRV_NAME
45#undef DRV_VERSION
Jeff Garzik72d5f2d2012-12-14 09:43:39 -050046
Rupjyoti Sarmah62936002010-07-06 16:36:03 +053047#define DRV_NAME "sata-dwc"
Sergei Shtylyov84b47e32011-01-28 22:01:01 +030048#define DRV_VERSION "1.3"
Rupjyoti Sarmah62936002010-07-06 16:36:03 +053049
Mans Rullgardee81d6c2016-04-26 12:03:20 +030050#define sata_dwc_writel(a, v) writel_relaxed(v, a)
51#define sata_dwc_readl(a) readl_relaxed(a)
Andy Shevchenko84683a72015-01-07 15:24:21 +020052
53#ifndef NO_IRQ
54#define NO_IRQ 0
55#endif
56
Andy Shevchenko4ea8c202016-04-26 12:03:05 +030057#define AHB_DMA_BRST_DFLT 64 /* 16 data items burst length */
Rupjyoti Sarmah62936002010-07-06 16:36:03 +053058
Rupjyoti Sarmah62936002010-07-06 16:36:03 +053059enum {
Rupjyoti Sarmah62936002010-07-06 16:36:03 +053060 SATA_DWC_MAX_PORTS = 1,
61
62 SATA_DWC_SCR_OFFSET = 0x24,
63 SATA_DWC_REG_OFFSET = 0x64,
64};
65
66/* DWC SATA Registers */
67struct sata_dwc_regs {
68 u32 fptagr; /* 1st party DMA tag */
69 u32 fpbor; /* 1st party DMA buffer offset */
70 u32 fptcr; /* 1st party DMA Xfr count */
71 u32 dmacr; /* DMA Control */
72 u32 dbtsr; /* DMA Burst Transac size */
73 u32 intpr; /* Interrupt Pending */
74 u32 intmr; /* Interrupt Mask */
75 u32 errmr; /* Error Mask */
76 u32 llcr; /* Link Layer Control */
77 u32 phycr; /* PHY Control */
78 u32 physr; /* PHY Status */
79 u32 rxbistpd; /* Recvd BIST pattern def register */
80 u32 rxbistpd1; /* Recvd BIST data dword1 */
81 u32 rxbistpd2; /* Recvd BIST pattern data dword2 */
82 u32 txbistpd; /* Trans BIST pattern def register */
83 u32 txbistpd1; /* Trans BIST data dword1 */
84 u32 txbistpd2; /* Trans BIST data dword2 */
85 u32 bistcr; /* BIST Control Register */
86 u32 bistfctr; /* BIST FIS Count Register */
87 u32 bistsr; /* BIST Status Register */
88 u32 bistdecr; /* BIST Dword Error count register */
89 u32 res[15]; /* Reserved locations */
90 u32 testr; /* Test Register */
91 u32 versionr; /* Version Register */
92 u32 idr; /* ID Register */
93 u32 unimpl[192]; /* Unimplemented */
Andy Shevchenkod6ecf0c2016-04-26 12:03:18 +030094 u32 dmadr[256]; /* FIFO Locations in DMA Mode */
Rupjyoti Sarmah62936002010-07-06 16:36:03 +053095};
96
97enum {
98 SCR_SCONTROL_DET_ENABLE = 0x00000001,
99 SCR_SSTATUS_DET_PRESENT = 0x00000001,
100 SCR_SERROR_DIAG_X = 0x04000000,
101/* DWC SATA Register Operations */
102 SATA_DWC_TXFIFO_DEPTH = 0x01FF,
103 SATA_DWC_RXFIFO_DEPTH = 0x01FF,
104 SATA_DWC_DMACR_TMOD_TXCHEN = 0x00000004,
105 SATA_DWC_DMACR_TXCHEN = (0x00000001 | SATA_DWC_DMACR_TMOD_TXCHEN),
106 SATA_DWC_DMACR_RXCHEN = (0x00000002 | SATA_DWC_DMACR_TMOD_TXCHEN),
107 SATA_DWC_DMACR_TXRXCH_CLEAR = SATA_DWC_DMACR_TMOD_TXCHEN,
108 SATA_DWC_INTPR_DMAT = 0x00000001,
109 SATA_DWC_INTPR_NEWFP = 0x00000002,
110 SATA_DWC_INTPR_PMABRT = 0x00000004,
111 SATA_DWC_INTPR_ERR = 0x00000008,
112 SATA_DWC_INTPR_NEWBIST = 0x00000010,
113 SATA_DWC_INTPR_IPF = 0x10000000,
114 SATA_DWC_INTMR_DMATM = 0x00000001,
115 SATA_DWC_INTMR_NEWFPM = 0x00000002,
116 SATA_DWC_INTMR_PMABRTM = 0x00000004,
117 SATA_DWC_INTMR_ERRM = 0x00000008,
118 SATA_DWC_INTMR_NEWBISTM = 0x00000010,
119 SATA_DWC_LLCR_SCRAMEN = 0x00000001,
120 SATA_DWC_LLCR_DESCRAMEN = 0x00000002,
121 SATA_DWC_LLCR_RPDEN = 0x00000004,
122/* This is all error bits, zero's are reserved fields. */
123 SATA_DWC_SERROR_ERR_BITS = 0x0FFF0F03
124};
125
126#define SATA_DWC_SCR0_SPD_GET(v) (((v) >> 4) & 0x0000000F)
127#define SATA_DWC_DMACR_TX_CLEAR(v) (((v) & ~SATA_DWC_DMACR_TXCHEN) |\
128 SATA_DWC_DMACR_TMOD_TXCHEN)
129#define SATA_DWC_DMACR_RX_CLEAR(v) (((v) & ~SATA_DWC_DMACR_RXCHEN) |\
130 SATA_DWC_DMACR_TMOD_TXCHEN)
131#define SATA_DWC_DBTSR_MWR(size) (((size)/4) & SATA_DWC_TXFIFO_DEPTH)
132#define SATA_DWC_DBTSR_MRD(size) ((((size)/4) & SATA_DWC_RXFIFO_DEPTH)\
133 << 16)
134struct sata_dwc_device {
135 struct device *dev; /* generic device struct */
136 struct ata_probe_ent *pe; /* ptr to probe-ent */
137 struct ata_host *host;
Mans Rullgardadc64ec2016-04-26 12:03:17 +0300138 struct sata_dwc_regs __iomem *sata_dwc_regs; /* DW SATA specific */
Mans Rullgard2d20da02016-04-26 12:03:12 +0300139 u32 sactive_issued;
140 u32 sactive_queued;
Mans Rullgard0f48deb2016-04-26 12:03:11 +0300141 struct phy *phy;
Andy Shevchenkod6ecf0c2016-04-26 12:03:18 +0300142 phys_addr_t dmadr;
Mans Rullgard50b43372016-04-26 12:03:10 +0300143#ifdef CONFIG_SATA_DWC_OLD_DMA
Andy Shevchenko8b344482015-03-03 22:41:21 +0200144 struct dw_dma_chip *dma;
Mans Rullgard50b43372016-04-26 12:03:10 +0300145#endif
Rupjyoti Sarmah62936002010-07-06 16:36:03 +0530146};
147
148#define SATA_DWC_QCMD_MAX 32
149
150struct sata_dwc_device_port {
151 struct sata_dwc_device *hsdev;
152 int cmd_issued[SATA_DWC_QCMD_MAX];
Rupjyoti Sarmah62936002010-07-06 16:36:03 +0530153 int dma_pending[SATA_DWC_QCMD_MAX];
Andy Shevchenko8b344482015-03-03 22:41:21 +0200154
155 /* DMA info */
Andy Shevchenko8b344482015-03-03 22:41:21 +0200156 struct dma_chan *chan;
157 struct dma_async_tx_descriptor *desc[SATA_DWC_QCMD_MAX];
158 u32 dma_interrupt_count;
Rupjyoti Sarmah62936002010-07-06 16:36:03 +0530159};
160
161/*
Andy Shevchenko4bb41be2016-04-26 12:03:13 +0300162 * Commonly used DWC SATA driver macros
Rupjyoti Sarmah62936002010-07-06 16:36:03 +0530163 */
Andy Shevchenko4bb41be2016-04-26 12:03:13 +0300164#define HSDEV_FROM_HOST(host) ((struct sata_dwc_device *)(host)->private_data)
165#define HSDEV_FROM_AP(ap) ((struct sata_dwc_device *)(ap)->host->private_data)
166#define HSDEVP_FROM_AP(ap) ((struct sata_dwc_device_port *)(ap)->private_data)
167#define HSDEV_FROM_QC(qc) ((struct sata_dwc_device *)(qc)->ap->host->private_data)
168#define HSDEV_FROM_HSDEVP(p) ((struct sata_dwc_device *)(p)->hsdev)
Rupjyoti Sarmah62936002010-07-06 16:36:03 +0530169
170enum {
171 SATA_DWC_CMD_ISSUED_NOT = 0,
172 SATA_DWC_CMD_ISSUED_PEND = 1,
173 SATA_DWC_CMD_ISSUED_EXEC = 2,
174 SATA_DWC_CMD_ISSUED_NODATA = 3,
175
176 SATA_DWC_DMA_PENDING_NONE = 0,
177 SATA_DWC_DMA_PENDING_TX = 1,
178 SATA_DWC_DMA_PENDING_RX = 2,
179};
180
Rupjyoti Sarmah62936002010-07-06 16:36:03 +0530181/*
182 * Prototypes
183 */
184static void sata_dwc_bmdma_start_by_tag(struct ata_queued_cmd *qc, u8 tag);
185static int sata_dwc_qc_complete(struct ata_port *ap, struct ata_queued_cmd *qc,
186 u32 check_status);
187static void sata_dwc_dma_xfer_complete(struct ata_port *ap, u32 check_status);
188static void sata_dwc_port_stop(struct ata_port *ap);
189static void sata_dwc_clear_dmacr(struct sata_dwc_device_port *hsdevp, u8 tag);
Rupjyoti Sarmah62936002010-07-06 16:36:03 +0530190
Mans Rullgard50b43372016-04-26 12:03:10 +0300191#ifdef CONFIG_SATA_DWC_OLD_DMA
192
193#include <linux/platform_data/dma-dw.h>
194#include <linux/dma/dw.h>
195
196static struct dw_dma_slave sata_dwc_dma_dws = {
197 .src_id = 0,
198 .dst_id = 0,
199 .m_master = 1,
200 .p_master = 0,
201};
202
203static bool sata_dwc_dma_filter(struct dma_chan *chan, void *param)
204{
205 struct dw_dma_slave *dws = &sata_dwc_dma_dws;
206
207 if (dws->dma_dev != chan->device->dev)
208 return false;
209
210 chan->private = dws;
211 return true;
212}
213
214static int sata_dwc_dma_get_channel_old(struct sata_dwc_device_port *hsdevp)
215{
216 struct sata_dwc_device *hsdev = hsdevp->hsdev;
217 struct dw_dma_slave *dws = &sata_dwc_dma_dws;
218 dma_cap_mask_t mask;
219
220 dws->dma_dev = hsdev->dev;
221
222 dma_cap_zero(mask);
223 dma_cap_set(DMA_SLAVE, mask);
224
225 /* Acquire DMA channel */
226 hsdevp->chan = dma_request_channel(mask, sata_dwc_dma_filter, hsdevp);
227 if (!hsdevp->chan) {
228 dev_err(hsdev->dev, "%s: dma channel unavailable\n",
229 __func__);
230 return -EAGAIN;
231 }
232
233 return 0;
234}
235
236static int sata_dwc_dma_init_old(struct platform_device *pdev,
237 struct sata_dwc_device *hsdev)
238{
239 struct device_node *np = pdev->dev.of_node;
Andy Shevchenko73ec1b52016-04-26 12:03:22 +0300240 struct resource *res;
Mans Rullgard50b43372016-04-26 12:03:10 +0300241
242 hsdev->dma = devm_kzalloc(&pdev->dev, sizeof(*hsdev->dma), GFP_KERNEL);
243 if (!hsdev->dma)
244 return -ENOMEM;
245
246 hsdev->dma->dev = &pdev->dev;
Andy Shevchenko47e46612017-11-10 19:59:37 +0200247 hsdev->dma->id = pdev->id;
Mans Rullgard50b43372016-04-26 12:03:10 +0300248
249 /* Get SATA DMA interrupt number */
250 hsdev->dma->irq = irq_of_parse_and_map(np, 1);
251 if (hsdev->dma->irq == NO_IRQ) {
252 dev_err(&pdev->dev, "no SATA DMA irq\n");
253 return -ENODEV;
254 }
255
256 /* Get physical SATA DMA register base address */
Andy Shevchenko73ec1b52016-04-26 12:03:22 +0300257 res = platform_get_resource(pdev, IORESOURCE_MEM, 1);
258 hsdev->dma->regs = devm_ioremap_resource(&pdev->dev, res);
Wei Yongjun01c29202016-07-19 11:27:53 +0000259 if (IS_ERR(hsdev->dma->regs))
Andy Shevchenko73ec1b52016-04-26 12:03:22 +0300260 return PTR_ERR(hsdev->dma->regs);
Mans Rullgard50b43372016-04-26 12:03:10 +0300261
262 /* Initialize AHB DMAC */
Andy Shevchenko73ec1b52016-04-26 12:03:22 +0300263 return dw_dma_probe(hsdev->dma);
Mans Rullgard50b43372016-04-26 12:03:10 +0300264}
265
266static void sata_dwc_dma_exit_old(struct sata_dwc_device *hsdev)
267{
268 if (!hsdev->dma)
269 return;
270
271 dw_dma_remove(hsdev->dma);
Mans Rullgard50b43372016-04-26 12:03:10 +0300272}
273
274#endif
275
Sergei Shtylyov84b47e32011-01-28 22:01:01 +0300276static const char *get_prot_descript(u8 protocol)
277{
Christoph Hellwig37f92d72016-07-16 22:16:43 +0900278 switch (protocol) {
Sergei Shtylyov84b47e32011-01-28 22:01:01 +0300279 case ATA_PROT_NODATA:
280 return "ATA no data";
281 case ATA_PROT_PIO:
282 return "ATA PIO";
283 case ATA_PROT_DMA:
284 return "ATA DMA";
285 case ATA_PROT_NCQ:
286 return "ATA NCQ";
Hannes Reinecke5b844b62016-07-14 09:05:48 +0900287 case ATA_PROT_NCQ_NODATA:
288 return "ATA NCQ no data";
Sergei Shtylyov84b47e32011-01-28 22:01:01 +0300289 case ATAPI_PROT_NODATA:
290 return "ATAPI no data";
291 case ATAPI_PROT_PIO:
292 return "ATAPI PIO";
293 case ATAPI_PROT_DMA:
294 return "ATAPI DMA";
295 default:
296 return "unknown";
297 }
298}
299
300static const char *get_dma_dir_descript(int dma_dir)
301{
302 switch ((enum dma_data_direction)dma_dir) {
303 case DMA_BIDIRECTIONAL:
304 return "bidirectional";
305 case DMA_TO_DEVICE:
306 return "to device";
307 case DMA_FROM_DEVICE:
308 return "from device";
309 default:
310 return "none";
311 }
312}
313
Andy Shevchenkodb7a6572015-03-03 22:41:22 +0200314static void sata_dwc_tf_dump(struct ata_port *ap, struct ata_taskfile *tf)
Rupjyoti Sarmah62936002010-07-06 16:36:03 +0530315{
Andy Shevchenkodb7a6572015-03-03 22:41:22 +0200316 dev_vdbg(ap->dev,
Andy Shevchenkod5785142015-03-03 21:21:58 +0200317 "taskfile cmd: 0x%02x protocol: %s flags: 0x%lx device: %x\n",
318 tf->command, get_prot_descript(tf->protocol), tf->flags,
319 tf->device);
Andy Shevchenkodb7a6572015-03-03 22:41:22 +0200320 dev_vdbg(ap->dev,
Andy Shevchenkod5785142015-03-03 21:21:58 +0200321 "feature: 0x%02x nsect: 0x%x lbal: 0x%x lbam: 0x%x lbah: 0x%x\n",
322 tf->feature, tf->nsect, tf->lbal, tf->lbam, tf->lbah);
Andy Shevchenkodb7a6572015-03-03 22:41:22 +0200323 dev_vdbg(ap->dev,
Andy Shevchenkod5785142015-03-03 21:21:58 +0200324 "hob_feature: 0x%02x hob_nsect: 0x%x hob_lbal: 0x%x hob_lbam: 0x%x hob_lbah: 0x%x\n",
Rupjyoti Sarmah62936002010-07-06 16:36:03 +0530325 tf->hob_feature, tf->hob_nsect, tf->hob_lbal, tf->hob_lbam,
326 tf->hob_lbah);
327}
328
Andy Shevchenko8b344482015-03-03 22:41:21 +0200329static void dma_dwc_xfer_done(void *hsdev_instance)
Rupjyoti Sarmah62936002010-07-06 16:36:03 +0530330{
Rupjyoti Sarmah62936002010-07-06 16:36:03 +0530331 unsigned long flags;
Joe Perchesd5185d62014-03-26 09:34:49 -0700332 struct sata_dwc_device *hsdev = hsdev_instance;
Rupjyoti Sarmah62936002010-07-06 16:36:03 +0530333 struct ata_host *host = (struct ata_host *)hsdev->host;
334 struct ata_port *ap;
335 struct sata_dwc_device_port *hsdevp;
336 u8 tag = 0;
337 unsigned int port = 0;
338
339 spin_lock_irqsave(&host->lock, flags);
340 ap = host->ports[port];
341 hsdevp = HSDEVP_FROM_AP(ap);
342 tag = ap->link.active_tag;
343
Rupjyoti Sarmah62936002010-07-06 16:36:03 +0530344 /*
Andy Shevchenko8b344482015-03-03 22:41:21 +0200345 * Each DMA command produces 2 interrupts. Only
346 * complete the command after both interrupts have been
347 * seen. (See sata_dwc_isr())
Rupjyoti Sarmah62936002010-07-06 16:36:03 +0530348 */
Andy Shevchenko8b344482015-03-03 22:41:21 +0200349 hsdevp->dma_interrupt_count++;
350 sata_dwc_clear_dmacr(hsdevp, tag);
Rupjyoti Sarmah62936002010-07-06 16:36:03 +0530351
Andy Shevchenko8b344482015-03-03 22:41:21 +0200352 if (hsdevp->dma_pending[tag] == SATA_DWC_DMA_PENDING_NONE) {
353 dev_err(ap->dev, "DMA not pending tag=0x%02x pending=%d\n",
354 tag, hsdevp->dma_pending[tag]);
Rupjyoti Sarmah62936002010-07-06 16:36:03 +0530355 }
356
Andy Shevchenko8b344482015-03-03 22:41:21 +0200357 if ((hsdevp->dma_interrupt_count % 2) == 0)
Dan Carpenter8d5fe8d2015-03-30 13:30:25 +0300358 sata_dwc_dma_xfer_complete(ap, 1);
Andy Shevchenko8b344482015-03-03 22:41:21 +0200359
360 spin_unlock_irqrestore(&host->lock, flags);
Rupjyoti Sarmah62936002010-07-06 16:36:03 +0530361}
362
Andy Shevchenko8b344482015-03-03 22:41:21 +0200363static struct dma_async_tx_descriptor *dma_dwc_xfer_setup(struct ata_queued_cmd *qc)
Rupjyoti Sarmah62936002010-07-06 16:36:03 +0530364{
Andy Shevchenko8b344482015-03-03 22:41:21 +0200365 struct ata_port *ap = qc->ap;
366 struct sata_dwc_device_port *hsdevp = HSDEVP_FROM_AP(ap);
367 struct sata_dwc_device *hsdev = HSDEV_FROM_AP(ap);
Andy Shevchenko8b344482015-03-03 22:41:21 +0200368 struct dma_slave_config sconf;
369 struct dma_async_tx_descriptor *desc;
Rupjyoti Sarmah62936002010-07-06 16:36:03 +0530370
Andy Shevchenko8b344482015-03-03 22:41:21 +0200371 if (qc->dma_dir == DMA_DEV_TO_MEM) {
Andy Shevchenkod6ecf0c2016-04-26 12:03:18 +0300372 sconf.src_addr = hsdev->dmadr;
Andy Shevchenko59a75ce2016-04-26 12:03:06 +0300373 sconf.device_fc = false;
Andy Shevchenko8b344482015-03-03 22:41:21 +0200374 } else { /* DMA_MEM_TO_DEV */
Andy Shevchenkod6ecf0c2016-04-26 12:03:18 +0300375 sconf.dst_addr = hsdev->dmadr;
Andy Shevchenko8b344482015-03-03 22:41:21 +0200376 sconf.device_fc = false;
Rupjyoti Sarmah62936002010-07-06 16:36:03 +0530377 }
378
Andy Shevchenko8b344482015-03-03 22:41:21 +0200379 sconf.direction = qc->dma_dir;
Andy Shevchenko4ea8c202016-04-26 12:03:05 +0300380 sconf.src_maxburst = AHB_DMA_BRST_DFLT / 4; /* in items */
381 sconf.dst_maxburst = AHB_DMA_BRST_DFLT / 4; /* in items */
Andy Shevchenko8b344482015-03-03 22:41:21 +0200382 sconf.src_addr_width = DMA_SLAVE_BUSWIDTH_4_BYTES;
383 sconf.dst_addr_width = DMA_SLAVE_BUSWIDTH_4_BYTES;
384
385 dmaengine_slave_config(hsdevp->chan, &sconf);
386
Rupjyoti Sarmah62936002010-07-06 16:36:03 +0530387 /* Convert SG list to linked list of items (LLIs) for AHB DMA */
Andy Shevchenko8b344482015-03-03 22:41:21 +0200388 desc = dmaengine_prep_slave_sg(hsdevp->chan, qc->sg, qc->n_elem,
389 qc->dma_dir,
390 DMA_PREP_INTERRUPT | DMA_CTRL_ACK);
Rupjyoti Sarmah62936002010-07-06 16:36:03 +0530391
Andy Shevchenko8b344482015-03-03 22:41:21 +0200392 if (!desc)
393 return NULL;
Rupjyoti Sarmah62936002010-07-06 16:36:03 +0530394
Andy Shevchenko8b344482015-03-03 22:41:21 +0200395 desc->callback = dma_dwc_xfer_done;
396 desc->callback_param = hsdev;
Rupjyoti Sarmah62936002010-07-06 16:36:03 +0530397
Andy Shevchenkod6ecf0c2016-04-26 12:03:18 +0300398 dev_dbg(hsdev->dev, "%s sg: 0x%p, count: %d addr: %pa\n", __func__,
399 qc->sg, qc->n_elem, &hsdev->dmadr);
Rupjyoti Sarmah62936002010-07-06 16:36:03 +0530400
Andy Shevchenko8b344482015-03-03 22:41:21 +0200401 return desc;
Rupjyoti Sarmah62936002010-07-06 16:36:03 +0530402}
403
404static int sata_dwc_scr_read(struct ata_link *link, unsigned int scr, u32 *val)
405{
406 if (scr > SCR_NOTIFICATION) {
407 dev_err(link->ap->dev, "%s: Incorrect SCR offset 0x%02x\n",
408 __func__, scr);
409 return -EINVAL;
410 }
411
Mans Rullgardee81d6c2016-04-26 12:03:20 +0300412 *val = sata_dwc_readl(link->ap->ioaddr.scr_addr + (scr * 4));
Andy Shevchenkobb08ab62016-04-26 12:03:23 +0300413 dev_dbg(link->ap->dev, "%s: id=%d reg=%d val=0x%08x\n", __func__,
414 link->ap->print_id, scr, *val);
Rupjyoti Sarmah62936002010-07-06 16:36:03 +0530415
416 return 0;
417}
418
419static int sata_dwc_scr_write(struct ata_link *link, unsigned int scr, u32 val)
420{
Andy Shevchenkobb08ab62016-04-26 12:03:23 +0300421 dev_dbg(link->ap->dev, "%s: id=%d reg=%d val=0x%08x\n", __func__,
422 link->ap->print_id, scr, val);
Rupjyoti Sarmah62936002010-07-06 16:36:03 +0530423 if (scr > SCR_NOTIFICATION) {
424 dev_err(link->ap->dev, "%s: Incorrect SCR offset 0x%02x\n",
425 __func__, scr);
426 return -EINVAL;
427 }
Mans Rullgardee81d6c2016-04-26 12:03:20 +0300428 sata_dwc_writel(link->ap->ioaddr.scr_addr + (scr * 4), val);
Rupjyoti Sarmah62936002010-07-06 16:36:03 +0530429
430 return 0;
431}
432
Mans Rullgard2d20da02016-04-26 12:03:12 +0300433static void clear_serror(struct ata_port *ap)
Rupjyoti Sarmah62936002010-07-06 16:36:03 +0530434{
435 u32 val;
Mans Rullgard2d20da02016-04-26 12:03:12 +0300436 sata_dwc_scr_read(&ap->link, SCR_ERROR, &val);
437 sata_dwc_scr_write(&ap->link, SCR_ERROR, val);
Rupjyoti Sarmah62936002010-07-06 16:36:03 +0530438}
439
440static void clear_interrupt_bit(struct sata_dwc_device *hsdev, u32 bit)
441{
Mans Rullgardee81d6c2016-04-26 12:03:20 +0300442 sata_dwc_writel(&hsdev->sata_dwc_regs->intpr,
443 sata_dwc_readl(&hsdev->sata_dwc_regs->intpr));
Rupjyoti Sarmah62936002010-07-06 16:36:03 +0530444}
445
446static u32 qcmd_tag_to_mask(u8 tag)
447{
448 return 0x00000001 << (tag & 0x1f);
449}
450
451/* See ahci.c */
452static void sata_dwc_error_intr(struct ata_port *ap,
453 struct sata_dwc_device *hsdev, uint intpr)
454{
455 struct sata_dwc_device_port *hsdevp = HSDEVP_FROM_AP(ap);
456 struct ata_eh_info *ehi = &ap->link.eh_info;
457 unsigned int err_mask = 0, action = 0;
458 struct ata_queued_cmd *qc;
459 u32 serror;
460 u8 status, tag;
Rupjyoti Sarmah62936002010-07-06 16:36:03 +0530461
462 ata_ehi_clear_desc(ehi);
463
Mans Rullgard2d20da02016-04-26 12:03:12 +0300464 sata_dwc_scr_read(&ap->link, SCR_ERROR, &serror);
Rupjyoti Sarmah62936002010-07-06 16:36:03 +0530465 status = ap->ops->sff_check_status(ap);
466
Rupjyoti Sarmah62936002010-07-06 16:36:03 +0530467 tag = ap->link.active_tag;
468
Andy Shevchenko8b344482015-03-03 22:41:21 +0200469 dev_err(ap->dev,
470 "%s SCR_ERROR=0x%08x intpr=0x%08x status=0x%08x dma_intp=%d pending=%d issued=%d",
471 __func__, serror, intpr, status, hsdevp->dma_interrupt_count,
472 hsdevp->dma_pending[tag], hsdevp->cmd_issued[tag]);
Rupjyoti Sarmah62936002010-07-06 16:36:03 +0530473
474 /* Clear error register and interrupt bit */
Mans Rullgard2d20da02016-04-26 12:03:12 +0300475 clear_serror(ap);
Rupjyoti Sarmah62936002010-07-06 16:36:03 +0530476 clear_interrupt_bit(hsdev, SATA_DWC_INTPR_ERR);
477
478 /* This is the only error happening now. TODO check for exact error */
479
480 err_mask |= AC_ERR_HOST_BUS;
481 action |= ATA_EH_RESET;
482
483 /* Pass this on to EH */
484 ehi->serror |= serror;
485 ehi->action |= action;
486
487 qc = ata_qc_from_tag(ap, tag);
488 if (qc)
489 qc->err_mask |= err_mask;
490 else
491 ehi->err_mask |= err_mask;
492
493 ata_port_abort(ap);
494}
495
496/*
497 * Function : sata_dwc_isr
498 * arguments : irq, void *dev_instance, struct pt_regs *regs
499 * Return value : irqreturn_t - status of IRQ
500 * This Interrupt handler called via port ops registered function.
501 * .irq_handler = sata_dwc_isr
502 */
503static irqreturn_t sata_dwc_isr(int irq, void *dev_instance)
504{
505 struct ata_host *host = (struct ata_host *)dev_instance;
506 struct sata_dwc_device *hsdev = HSDEV_FROM_HOST(host);
507 struct ata_port *ap;
508 struct ata_queued_cmd *qc;
509 unsigned long flags;
510 u8 status, tag;
511 int handled, num_processed, port = 0;
512 uint intpr, sactive, sactive2, tag_mask;
513 struct sata_dwc_device_port *hsdevp;
Mans Rullgard2d20da02016-04-26 12:03:12 +0300514 hsdev->sactive_issued = 0;
Rupjyoti Sarmah62936002010-07-06 16:36:03 +0530515
516 spin_lock_irqsave(&host->lock, flags);
517
518 /* Read the interrupt register */
Mans Rullgardee81d6c2016-04-26 12:03:20 +0300519 intpr = sata_dwc_readl(&hsdev->sata_dwc_regs->intpr);
Rupjyoti Sarmah62936002010-07-06 16:36:03 +0530520
521 ap = host->ports[port];
522 hsdevp = HSDEVP_FROM_AP(ap);
523
524 dev_dbg(ap->dev, "%s intpr=0x%08x active_tag=%d\n", __func__, intpr,
525 ap->link.active_tag);
526
527 /* Check for error interrupt */
528 if (intpr & SATA_DWC_INTPR_ERR) {
529 sata_dwc_error_intr(ap, hsdev, intpr);
530 handled = 1;
531 goto DONE;
532 }
533
534 /* Check for DMA SETUP FIS (FP DMA) interrupt */
535 if (intpr & SATA_DWC_INTPR_NEWFP) {
536 clear_interrupt_bit(hsdev, SATA_DWC_INTPR_NEWFP);
537
Mans Rullgardee81d6c2016-04-26 12:03:20 +0300538 tag = (u8)(sata_dwc_readl(&hsdev->sata_dwc_regs->fptagr));
Rupjyoti Sarmah62936002010-07-06 16:36:03 +0530539 dev_dbg(ap->dev, "%s: NEWFP tag=%d\n", __func__, tag);
540 if (hsdevp->cmd_issued[tag] != SATA_DWC_CMD_ISSUED_PEND)
541 dev_warn(ap->dev, "CMD tag=%d not pending?\n", tag);
542
Mans Rullgard2d20da02016-04-26 12:03:12 +0300543 hsdev->sactive_issued |= qcmd_tag_to_mask(tag);
Rupjyoti Sarmah62936002010-07-06 16:36:03 +0530544
545 qc = ata_qc_from_tag(ap, tag);
Dinghao Liu0ed02832021-03-03 15:34:08 +0800546 if (unlikely(!qc)) {
547 dev_err(ap->dev, "failed to get qc");
548 handled = 1;
549 goto DONE;
550 }
Rupjyoti Sarmah62936002010-07-06 16:36:03 +0530551 /*
552 * Start FP DMA for NCQ command. At this point the tag is the
553 * active tag. It is the tag that matches the command about to
554 * be completed.
555 */
556 qc->ap->link.active_tag = tag;
557 sata_dwc_bmdma_start_by_tag(qc, tag);
558
559 handled = 1;
560 goto DONE;
561 }
Mans Rullgard2d20da02016-04-26 12:03:12 +0300562 sata_dwc_scr_read(&ap->link, SCR_ACTIVE, &sactive);
563 tag_mask = (hsdev->sactive_issued | sactive) ^ sactive;
Rupjyoti Sarmah62936002010-07-06 16:36:03 +0530564
565 /* If no sactive issued and tag_mask is zero then this is not NCQ */
Mans Rullgard2d20da02016-04-26 12:03:12 +0300566 if (hsdev->sactive_issued == 0 && tag_mask == 0) {
Rupjyoti Sarmah62936002010-07-06 16:36:03 +0530567 if (ap->link.active_tag == ATA_TAG_POISON)
568 tag = 0;
569 else
570 tag = ap->link.active_tag;
571 qc = ata_qc_from_tag(ap, tag);
572
573 /* DEV interrupt w/ no active qc? */
574 if (unlikely(!qc || (qc->tf.flags & ATA_TFLAG_POLLING))) {
Andy Shevchenkod5785142015-03-03 21:21:58 +0200575 dev_err(ap->dev,
576 "%s interrupt with no active qc qc=%p\n",
577 __func__, qc);
Rupjyoti Sarmah62936002010-07-06 16:36:03 +0530578 ap->ops->sff_check_status(ap);
579 handled = 1;
580 goto DONE;
581 }
582 status = ap->ops->sff_check_status(ap);
583
584 qc->ap->link.active_tag = tag;
585 hsdevp->cmd_issued[tag] = SATA_DWC_CMD_ISSUED_NOT;
586
587 if (status & ATA_ERR) {
588 dev_dbg(ap->dev, "interrupt ATA_ERR (0x%x)\n", status);
589 sata_dwc_qc_complete(ap, qc, 1);
590 handled = 1;
591 goto DONE;
592 }
593
594 dev_dbg(ap->dev, "%s non-NCQ cmd interrupt, protocol: %s\n",
Sergei Shtylyov84b47e32011-01-28 22:01:01 +0300595 __func__, get_prot_descript(qc->tf.protocol));
Rupjyoti Sarmah62936002010-07-06 16:36:03 +0530596DRVSTILLBUSY:
597 if (ata_is_dma(qc->tf.protocol)) {
598 /*
599 * Each DMA transaction produces 2 interrupts. The DMAC
600 * transfer complete interrupt and the SATA controller
601 * operation done interrupt. The command should be
602 * completed only after both interrupts are seen.
603 */
Andy Shevchenko8b344482015-03-03 22:41:21 +0200604 hsdevp->dma_interrupt_count++;
Rupjyoti Sarmah62936002010-07-06 16:36:03 +0530605 if (hsdevp->dma_pending[tag] == \
606 SATA_DWC_DMA_PENDING_NONE) {
Andy Shevchenkod5785142015-03-03 21:21:58 +0200607 dev_err(ap->dev,
608 "%s: DMA not pending intpr=0x%08x status=0x%08x pending=%d\n",
609 __func__, intpr, status,
Rupjyoti Sarmah62936002010-07-06 16:36:03 +0530610 hsdevp->dma_pending[tag]);
611 }
612
Andy Shevchenko8b344482015-03-03 22:41:21 +0200613 if ((hsdevp->dma_interrupt_count % 2) == 0)
Rupjyoti Sarmah62936002010-07-06 16:36:03 +0530614 sata_dwc_dma_xfer_complete(ap, 1);
615 } else if (ata_is_pio(qc->tf.protocol)) {
616 ata_sff_hsm_move(ap, qc, status, 0);
617 handled = 1;
618 goto DONE;
619 } else {
620 if (unlikely(sata_dwc_qc_complete(ap, qc, 1)))
621 goto DRVSTILLBUSY;
622 }
623
624 handled = 1;
625 goto DONE;
626 }
627
628 /*
629 * This is a NCQ command. At this point we need to figure out for which
630 * tags we have gotten a completion interrupt. One interrupt may serve
631 * as completion for more than one operation when commands are queued
632 * (NCQ). We need to process each completed command.
633 */
634
635 /* process completed commands */
Mans Rullgard2d20da02016-04-26 12:03:12 +0300636 sata_dwc_scr_read(&ap->link, SCR_ACTIVE, &sactive);
637 tag_mask = (hsdev->sactive_issued | sactive) ^ sactive;
Rupjyoti Sarmah62936002010-07-06 16:36:03 +0530638
Mans Rullgard2d20da02016-04-26 12:03:12 +0300639 if (sactive != 0 || hsdev->sactive_issued > 1 || tag_mask > 1) {
Andy Shevchenkod5785142015-03-03 21:21:58 +0200640 dev_dbg(ap->dev,
641 "%s NCQ:sactive=0x%08x sactive_issued=0x%08x tag_mask=0x%08x\n",
Mans Rullgard2d20da02016-04-26 12:03:12 +0300642 __func__, sactive, hsdev->sactive_issued, tag_mask);
Rupjyoti Sarmah62936002010-07-06 16:36:03 +0530643 }
644
Mans Rullgard2d20da02016-04-26 12:03:12 +0300645 if ((tag_mask | hsdev->sactive_issued) != hsdev->sactive_issued) {
Andy Shevchenkod5785142015-03-03 21:21:58 +0200646 dev_warn(ap->dev,
Mans Rullgard2d20da02016-04-26 12:03:12 +0300647 "Bad tag mask? sactive=0x%08x sactive_issued=0x%08x tag_mask=0x%08x\n",
648 sactive, hsdev->sactive_issued, tag_mask);
Rupjyoti Sarmah62936002010-07-06 16:36:03 +0530649 }
650
651 /* read just to clear ... not bad if currently still busy */
652 status = ap->ops->sff_check_status(ap);
653 dev_dbg(ap->dev, "%s ATA status register=0x%x\n", __func__, status);
654
655 tag = 0;
656 num_processed = 0;
657 while (tag_mask) {
658 num_processed++;
659 while (!(tag_mask & 0x00000001)) {
660 tag++;
661 tag_mask <<= 1;
662 }
663
664 tag_mask &= (~0x00000001);
665 qc = ata_qc_from_tag(ap, tag);
Dinghao Liu0ed02832021-03-03 15:34:08 +0800666 if (unlikely(!qc)) {
667 dev_err(ap->dev, "failed to get qc");
668 handled = 1;
669 goto DONE;
670 }
Rupjyoti Sarmah62936002010-07-06 16:36:03 +0530671
672 /* To be picked up by completion functions */
673 qc->ap->link.active_tag = tag;
674 hsdevp->cmd_issued[tag] = SATA_DWC_CMD_ISSUED_NOT;
675
676 /* Let libata/scsi layers handle error */
677 if (status & ATA_ERR) {
678 dev_dbg(ap->dev, "%s ATA_ERR (0x%x)\n", __func__,
679 status);
680 sata_dwc_qc_complete(ap, qc, 1);
681 handled = 1;
682 goto DONE;
683 }
684
685 /* Process completed command */
686 dev_dbg(ap->dev, "%s NCQ command, protocol: %s\n", __func__,
Sergei Shtylyov84b47e32011-01-28 22:01:01 +0300687 get_prot_descript(qc->tf.protocol));
Rupjyoti Sarmah62936002010-07-06 16:36:03 +0530688 if (ata_is_dma(qc->tf.protocol)) {
Andy Shevchenko8b344482015-03-03 22:41:21 +0200689 hsdevp->dma_interrupt_count++;
Rupjyoti Sarmah62936002010-07-06 16:36:03 +0530690 if (hsdevp->dma_pending[tag] == \
691 SATA_DWC_DMA_PENDING_NONE)
692 dev_warn(ap->dev, "%s: DMA not pending?\n",
693 __func__);
Andy Shevchenko8b344482015-03-03 22:41:21 +0200694 if ((hsdevp->dma_interrupt_count % 2) == 0)
Rupjyoti Sarmah62936002010-07-06 16:36:03 +0530695 sata_dwc_dma_xfer_complete(ap, 1);
696 } else {
697 if (unlikely(sata_dwc_qc_complete(ap, qc, 1)))
698 goto STILLBUSY;
699 }
700 continue;
701
702STILLBUSY:
703 ap->stats.idle_irq++;
704 dev_warn(ap->dev, "STILL BUSY IRQ ata%d: irq trap\n",
705 ap->print_id);
706 } /* while tag_mask */
707
708 /*
709 * Check to see if any commands completed while we were processing our
710 * initial set of completed commands (read status clears interrupts,
711 * so we might miss a completed command interrupt if one came in while
712 * we were processing --we read status as part of processing a completed
713 * command).
714 */
Mans Rullgard2d20da02016-04-26 12:03:12 +0300715 sata_dwc_scr_read(&ap->link, SCR_ACTIVE, &sactive2);
Rupjyoti Sarmah62936002010-07-06 16:36:03 +0530716 if (sactive2 != sactive) {
Andy Shevchenkod5785142015-03-03 21:21:58 +0200717 dev_dbg(ap->dev,
718 "More completed - sactive=0x%x sactive2=0x%x\n",
719 sactive, sactive2);
Rupjyoti Sarmah62936002010-07-06 16:36:03 +0530720 }
721 handled = 1;
722
723DONE:
724 spin_unlock_irqrestore(&host->lock, flags);
725 return IRQ_RETVAL(handled);
726}
727
728static void sata_dwc_clear_dmacr(struct sata_dwc_device_port *hsdevp, u8 tag)
729{
730 struct sata_dwc_device *hsdev = HSDEV_FROM_HSDEVP(hsdevp);
Mans Rullgardaf50f3a2016-04-26 12:03:21 +0300731 u32 dmacr = sata_dwc_readl(&hsdev->sata_dwc_regs->dmacr);
Rupjyoti Sarmah62936002010-07-06 16:36:03 +0530732
733 if (hsdevp->dma_pending[tag] == SATA_DWC_DMA_PENDING_RX) {
Mans Rullgardaf50f3a2016-04-26 12:03:21 +0300734 dmacr = SATA_DWC_DMACR_RX_CLEAR(dmacr);
735 sata_dwc_writel(&hsdev->sata_dwc_regs->dmacr, dmacr);
Rupjyoti Sarmah62936002010-07-06 16:36:03 +0530736 } else if (hsdevp->dma_pending[tag] == SATA_DWC_DMA_PENDING_TX) {
Mans Rullgardaf50f3a2016-04-26 12:03:21 +0300737 dmacr = SATA_DWC_DMACR_TX_CLEAR(dmacr);
738 sata_dwc_writel(&hsdev->sata_dwc_regs->dmacr, dmacr);
Rupjyoti Sarmah62936002010-07-06 16:36:03 +0530739 } else {
740 /*
741 * This should not happen, it indicates the driver is out of
742 * sync. If it does happen, clear dmacr anyway.
743 */
Andy Shevchenkodb7a6572015-03-03 22:41:22 +0200744 dev_err(hsdev->dev,
Andy Shevchenkod5785142015-03-03 21:21:58 +0200745 "%s DMA protocol RX and TX DMA not pending tag=0x%02x pending=%d dmacr: 0x%08x\n",
Mans Rullgardaf50f3a2016-04-26 12:03:21 +0300746 __func__, tag, hsdevp->dma_pending[tag], dmacr);
Mans Rullgardee81d6c2016-04-26 12:03:20 +0300747 sata_dwc_writel(&hsdev->sata_dwc_regs->dmacr,
748 SATA_DWC_DMACR_TXRXCH_CLEAR);
Rupjyoti Sarmah62936002010-07-06 16:36:03 +0530749 }
750}
751
752static void sata_dwc_dma_xfer_complete(struct ata_port *ap, u32 check_status)
753{
754 struct ata_queued_cmd *qc;
755 struct sata_dwc_device_port *hsdevp = HSDEVP_FROM_AP(ap);
756 struct sata_dwc_device *hsdev = HSDEV_FROM_AP(ap);
757 u8 tag = 0;
758
759 tag = ap->link.active_tag;
760 qc = ata_qc_from_tag(ap, tag);
761 if (!qc) {
762 dev_err(ap->dev, "failed to get qc");
763 return;
764 }
765
766#ifdef DEBUG_NCQ
767 if (tag > 0) {
Andy Shevchenkod5785142015-03-03 21:21:58 +0200768 dev_info(ap->dev,
769 "%s tag=%u cmd=0x%02x dma dir=%s proto=%s dmacr=0x%08x\n",
Jens Axboe4e5b6262018-05-11 12:51:04 -0600770 __func__, qc->hw_tag, qc->tf.command,
Sergei Shtylyov84b47e32011-01-28 22:01:01 +0300771 get_dma_dir_descript(qc->dma_dir),
772 get_prot_descript(qc->tf.protocol),
Mans Rullgardee81d6c2016-04-26 12:03:20 +0300773 sata_dwc_readl(&hsdev->sata_dwc_regs->dmacr));
Rupjyoti Sarmah62936002010-07-06 16:36:03 +0530774 }
775#endif
776
777 if (ata_is_dma(qc->tf.protocol)) {
778 if (hsdevp->dma_pending[tag] == SATA_DWC_DMA_PENDING_NONE) {
Andy Shevchenkod5785142015-03-03 21:21:58 +0200779 dev_err(ap->dev,
780 "%s DMA protocol RX and TX DMA not pending dmacr: 0x%08x\n",
781 __func__,
Mans Rullgardee81d6c2016-04-26 12:03:20 +0300782 sata_dwc_readl(&hsdev->sata_dwc_regs->dmacr));
Rupjyoti Sarmah62936002010-07-06 16:36:03 +0530783 }
784
785 hsdevp->dma_pending[tag] = SATA_DWC_DMA_PENDING_NONE;
786 sata_dwc_qc_complete(ap, qc, check_status);
787 ap->link.active_tag = ATA_TAG_POISON;
788 } else {
789 sata_dwc_qc_complete(ap, qc, check_status);
790 }
791}
792
793static int sata_dwc_qc_complete(struct ata_port *ap, struct ata_queued_cmd *qc,
794 u32 check_status)
795{
796 u8 status = 0;
797 u32 mask = 0x0;
Jens Axboe4e5b6262018-05-11 12:51:04 -0600798 u8 tag = qc->hw_tag;
Mans Rullgard2d20da02016-04-26 12:03:12 +0300799 struct sata_dwc_device *hsdev = HSDEV_FROM_AP(ap);
Rupjyoti Sarmah62936002010-07-06 16:36:03 +0530800 struct sata_dwc_device_port *hsdevp = HSDEVP_FROM_AP(ap);
Mans Rullgard2d20da02016-04-26 12:03:12 +0300801 hsdev->sactive_queued = 0;
Rupjyoti Sarmah62936002010-07-06 16:36:03 +0530802 dev_dbg(ap->dev, "%s checkstatus? %x\n", __func__, check_status);
803
804 if (hsdevp->dma_pending[tag] == SATA_DWC_DMA_PENDING_TX)
805 dev_err(ap->dev, "TX DMA PENDING\n");
806 else if (hsdevp->dma_pending[tag] == SATA_DWC_DMA_PENDING_RX)
807 dev_err(ap->dev, "RX DMA PENDING\n");
Andy Shevchenkod5785142015-03-03 21:21:58 +0200808 dev_dbg(ap->dev,
809 "QC complete cmd=0x%02x status=0x%02x ata%u: protocol=%d\n",
810 qc->tf.command, status, ap->print_id, qc->tf.protocol);
Rupjyoti Sarmah62936002010-07-06 16:36:03 +0530811
812 /* clear active bit */
813 mask = (~(qcmd_tag_to_mask(tag)));
Mans Rullgard2d20da02016-04-26 12:03:12 +0300814 hsdev->sactive_queued = hsdev->sactive_queued & mask;
815 hsdev->sactive_issued = hsdev->sactive_issued & mask;
Rupjyoti Sarmah62936002010-07-06 16:36:03 +0530816 ata_qc_complete(qc);
817 return 0;
818}
819
820static void sata_dwc_enable_interrupts(struct sata_dwc_device *hsdev)
821{
822 /* Enable selective interrupts by setting the interrupt maskregister*/
Mans Rullgardee81d6c2016-04-26 12:03:20 +0300823 sata_dwc_writel(&hsdev->sata_dwc_regs->intmr,
824 SATA_DWC_INTMR_ERRM |
825 SATA_DWC_INTMR_NEWFPM |
826 SATA_DWC_INTMR_PMABRTM |
827 SATA_DWC_INTMR_DMATM);
Rupjyoti Sarmah62936002010-07-06 16:36:03 +0530828 /*
829 * Unmask the error bits that should trigger an error interrupt by
830 * setting the error mask register.
831 */
Mans Rullgardee81d6c2016-04-26 12:03:20 +0300832 sata_dwc_writel(&hsdev->sata_dwc_regs->errmr, SATA_DWC_SERROR_ERR_BITS);
Rupjyoti Sarmah62936002010-07-06 16:36:03 +0530833
Andy Shevchenkodb7a6572015-03-03 22:41:22 +0200834 dev_dbg(hsdev->dev, "%s: INTMR = 0x%08x, ERRMR = 0x%08x\n",
Mans Rullgardee81d6c2016-04-26 12:03:20 +0300835 __func__, sata_dwc_readl(&hsdev->sata_dwc_regs->intmr),
836 sata_dwc_readl(&hsdev->sata_dwc_regs->errmr));
Rupjyoti Sarmah62936002010-07-06 16:36:03 +0530837}
838
Mans Rullgardae95d952016-04-26 12:03:15 +0300839static void sata_dwc_setup_port(struct ata_ioports *port, void __iomem *base)
Rupjyoti Sarmah62936002010-07-06 16:36:03 +0530840{
Mans Rullgardae95d952016-04-26 12:03:15 +0300841 port->cmd_addr = base + 0x00;
842 port->data_addr = base + 0x00;
Rupjyoti Sarmah62936002010-07-06 16:36:03 +0530843
Mans Rullgardae95d952016-04-26 12:03:15 +0300844 port->error_addr = base + 0x04;
845 port->feature_addr = base + 0x04;
Rupjyoti Sarmah62936002010-07-06 16:36:03 +0530846
Mans Rullgardae95d952016-04-26 12:03:15 +0300847 port->nsect_addr = base + 0x08;
Rupjyoti Sarmah62936002010-07-06 16:36:03 +0530848
Mans Rullgardae95d952016-04-26 12:03:15 +0300849 port->lbal_addr = base + 0x0c;
850 port->lbam_addr = base + 0x10;
851 port->lbah_addr = base + 0x14;
Rupjyoti Sarmah62936002010-07-06 16:36:03 +0530852
Mans Rullgardae95d952016-04-26 12:03:15 +0300853 port->device_addr = base + 0x18;
854 port->command_addr = base + 0x1c;
855 port->status_addr = base + 0x1c;
Rupjyoti Sarmah62936002010-07-06 16:36:03 +0530856
Mans Rullgardae95d952016-04-26 12:03:15 +0300857 port->altstatus_addr = base + 0x20;
858 port->ctl_addr = base + 0x20;
Rupjyoti Sarmah62936002010-07-06 16:36:03 +0530859}
860
Mans Rullgard50b43372016-04-26 12:03:10 +0300861static int sata_dwc_dma_get_channel(struct sata_dwc_device_port *hsdevp)
862{
863 struct sata_dwc_device *hsdev = hsdevp->hsdev;
864 struct device *dev = hsdev->dev;
865
866#ifdef CONFIG_SATA_DWC_OLD_DMA
867 if (!of_find_property(dev->of_node, "dmas", NULL))
868 return sata_dwc_dma_get_channel_old(hsdevp);
869#endif
870
871 hsdevp->chan = dma_request_chan(dev, "sata-dma");
872 if (IS_ERR(hsdevp->chan)) {
873 dev_err(dev, "failed to allocate dma channel: %ld\n",
874 PTR_ERR(hsdevp->chan));
875 return PTR_ERR(hsdevp->chan);
876 }
877
878 return 0;
879}
880
Rupjyoti Sarmah62936002010-07-06 16:36:03 +0530881/*
882 * Function : sata_dwc_port_start
883 * arguments : struct ata_ioports *port
884 * Return value : returns 0 if success, error code otherwise
885 * This function allocates the scatter gather LLI table for AHB DMA
886 */
887static int sata_dwc_port_start(struct ata_port *ap)
888{
889 int err = 0;
890 struct sata_dwc_device *hsdev;
891 struct sata_dwc_device_port *hsdevp = NULL;
892 struct device *pdev;
893 int i;
894
895 hsdev = HSDEV_FROM_AP(ap);
896
897 dev_dbg(ap->dev, "%s: port_no=%d\n", __func__, ap->port_no);
898
899 hsdev->host = ap->host;
900 pdev = ap->host->dev;
901 if (!pdev) {
902 dev_err(ap->dev, "%s: no ap->host->dev\n", __func__);
903 err = -ENODEV;
904 goto CLEANUP;
905 }
906
907 /* Allocate Port Struct */
908 hsdevp = kzalloc(sizeof(*hsdevp), GFP_KERNEL);
909 if (!hsdevp) {
Rupjyoti Sarmah62936002010-07-06 16:36:03 +0530910 err = -ENOMEM;
911 goto CLEANUP;
912 }
913 hsdevp->hsdev = hsdev;
914
Mans Rullgard50b43372016-04-26 12:03:10 +0300915 err = sata_dwc_dma_get_channel(hsdevp);
916 if (err)
Andy Shevchenko8b344482015-03-03 22:41:21 +0200917 goto CLEANUP_ALLOC;
Andy Shevchenko8b344482015-03-03 22:41:21 +0200918
Mans Rullgard0f48deb2016-04-26 12:03:11 +0300919 err = phy_power_on(hsdev->phy);
920 if (err)
921 goto CLEANUP_ALLOC;
922
Rupjyoti Sarmah62936002010-07-06 16:36:03 +0530923 for (i = 0; i < SATA_DWC_QCMD_MAX; i++)
924 hsdevp->cmd_issued[i] = SATA_DWC_CMD_ISSUED_NOT;
925
Andy Shevchenkod7c256e2015-01-07 15:24:22 +0200926 ap->bmdma_prd = NULL; /* set these so libata doesn't use them */
Rupjyoti Sarmah62936002010-07-06 16:36:03 +0530927 ap->bmdma_prd_dma = 0;
928
Rupjyoti Sarmah62936002010-07-06 16:36:03 +0530929 if (ap->port_no == 0) {
930 dev_dbg(ap->dev, "%s: clearing TXCHEN, RXCHEN in DMAC\n",
931 __func__);
Mans Rullgardee81d6c2016-04-26 12:03:20 +0300932 sata_dwc_writel(&hsdev->sata_dwc_regs->dmacr,
933 SATA_DWC_DMACR_TXRXCH_CLEAR);
Rupjyoti Sarmah62936002010-07-06 16:36:03 +0530934
935 dev_dbg(ap->dev, "%s: setting burst size in DBTSR\n",
936 __func__);
Mans Rullgardee81d6c2016-04-26 12:03:20 +0300937 sata_dwc_writel(&hsdev->sata_dwc_regs->dbtsr,
938 (SATA_DWC_DBTSR_MWR(AHB_DMA_BRST_DFLT) |
939 SATA_DWC_DBTSR_MRD(AHB_DMA_BRST_DFLT)));
Rupjyoti Sarmah62936002010-07-06 16:36:03 +0530940 }
941
942 /* Clear any error bits before libata starts issuing commands */
Mans Rullgard2d20da02016-04-26 12:03:12 +0300943 clear_serror(ap);
Rupjyoti Sarmah62936002010-07-06 16:36:03 +0530944 ap->private_data = hsdevp;
Julia Lawalla081da62011-08-08 13:17:57 +0200945 dev_dbg(ap->dev, "%s: done\n", __func__);
946 return 0;
Rupjyoti Sarmah62936002010-07-06 16:36:03 +0530947
Julia Lawalla081da62011-08-08 13:17:57 +0200948CLEANUP_ALLOC:
949 kfree(hsdevp);
Rupjyoti Sarmah62936002010-07-06 16:36:03 +0530950CLEANUP:
Julia Lawalla081da62011-08-08 13:17:57 +0200951 dev_dbg(ap->dev, "%s: fail. ap->id = %d\n", __func__, ap->print_id);
Rupjyoti Sarmah62936002010-07-06 16:36:03 +0530952 return err;
953}
954
955static void sata_dwc_port_stop(struct ata_port *ap)
956{
Rupjyoti Sarmah62936002010-07-06 16:36:03 +0530957 struct sata_dwc_device_port *hsdevp = HSDEVP_FROM_AP(ap);
Mans Rullgard0f48deb2016-04-26 12:03:11 +0300958 struct sata_dwc_device *hsdev = HSDEV_FROM_AP(ap);
Rupjyoti Sarmah62936002010-07-06 16:36:03 +0530959
960 dev_dbg(ap->dev, "%s: ap->id = %d\n", __func__, ap->print_id);
961
Andy Shevchenko9e8b8552016-04-26 12:03:19 +0300962 dmaengine_terminate_sync(hsdevp->chan);
Andy Shevchenko8b344482015-03-03 22:41:21 +0200963 dma_release_channel(hsdevp->chan);
Mans Rullgard0f48deb2016-04-26 12:03:11 +0300964 phy_power_off(hsdev->phy);
Rupjyoti Sarmah62936002010-07-06 16:36:03 +0530965
Andy Shevchenko8b344482015-03-03 22:41:21 +0200966 kfree(hsdevp);
Rupjyoti Sarmah62936002010-07-06 16:36:03 +0530967 ap->private_data = NULL;
968}
969
970/*
971 * Function : sata_dwc_exec_command_by_tag
972 * arguments : ata_port *ap, ata_taskfile *tf, u8 tag, u32 cmd_issued
973 * Return value : None
974 * This function keeps track of individual command tag ids and calls
975 * ata_exec_command in libata
976 */
977static void sata_dwc_exec_command_by_tag(struct ata_port *ap,
978 struct ata_taskfile *tf,
979 u8 tag, u32 cmd_issued)
980{
Rupjyoti Sarmah62936002010-07-06 16:36:03 +0530981 struct sata_dwc_device_port *hsdevp = HSDEVP_FROM_AP(ap);
982
983 dev_dbg(ap->dev, "%s cmd(0x%02x): %s tag=%d\n", __func__, tf->command,
Sergei Shtylyovc2119622011-01-28 21:55:55 +0300984 ata_get_cmd_descript(tf->command), tag);
Rupjyoti Sarmah62936002010-07-06 16:36:03 +0530985
Rupjyoti Sarmah62936002010-07-06 16:36:03 +0530986 hsdevp->cmd_issued[tag] = cmd_issued;
Mans Rullgard55e610c2016-04-26 12:03:02 +0300987
Rupjyoti Sarmah62936002010-07-06 16:36:03 +0530988 /*
989 * Clear SError before executing a new command.
990 * sata_dwc_scr_write and read can not be used here. Clearing the PM
991 * managed SError register for the disk needs to be done before the
992 * task file is loaded.
993 */
Mans Rullgard2d20da02016-04-26 12:03:12 +0300994 clear_serror(ap);
Rupjyoti Sarmah62936002010-07-06 16:36:03 +0530995 ata_sff_exec_command(ap, tf);
996}
997
998static void sata_dwc_bmdma_setup_by_tag(struct ata_queued_cmd *qc, u8 tag)
999{
1000 sata_dwc_exec_command_by_tag(qc->ap, &qc->tf, tag,
1001 SATA_DWC_CMD_ISSUED_PEND);
1002}
1003
1004static void sata_dwc_bmdma_setup(struct ata_queued_cmd *qc)
1005{
Jens Axboe4e5b6262018-05-11 12:51:04 -06001006 u8 tag = qc->hw_tag;
Rupjyoti Sarmah62936002010-07-06 16:36:03 +05301007
1008 if (ata_is_ncq(qc->tf.protocol)) {
1009 dev_dbg(qc->ap->dev, "%s: ap->link.sactive=0x%08x tag=%d\n",
1010 __func__, qc->ap->link.sactive, tag);
1011 } else {
1012 tag = 0;
1013 }
1014 sata_dwc_bmdma_setup_by_tag(qc, tag);
1015}
1016
1017static void sata_dwc_bmdma_start_by_tag(struct ata_queued_cmd *qc, u8 tag)
1018{
1019 int start_dma;
Andy Shevchenko8b344482015-03-03 22:41:21 +02001020 u32 reg;
Rupjyoti Sarmah62936002010-07-06 16:36:03 +05301021 struct sata_dwc_device *hsdev = HSDEV_FROM_QC(qc);
1022 struct ata_port *ap = qc->ap;
1023 struct sata_dwc_device_port *hsdevp = HSDEVP_FROM_AP(ap);
Andy Shevchenko8b344482015-03-03 22:41:21 +02001024 struct dma_async_tx_descriptor *desc = hsdevp->desc[tag];
Rupjyoti Sarmah62936002010-07-06 16:36:03 +05301025 int dir = qc->dma_dir;
Rupjyoti Sarmah62936002010-07-06 16:36:03 +05301026
1027 if (hsdevp->cmd_issued[tag] != SATA_DWC_CMD_ISSUED_NOT) {
1028 start_dma = 1;
1029 if (dir == DMA_TO_DEVICE)
1030 hsdevp->dma_pending[tag] = SATA_DWC_DMA_PENDING_TX;
1031 else
1032 hsdevp->dma_pending[tag] = SATA_DWC_DMA_PENDING_RX;
1033 } else {
Andy Shevchenkod5785142015-03-03 21:21:58 +02001034 dev_err(ap->dev,
1035 "%s: Command not pending cmd_issued=%d (tag=%d) DMA NOT started\n",
1036 __func__, hsdevp->cmd_issued[tag], tag);
Rupjyoti Sarmah62936002010-07-06 16:36:03 +05301037 start_dma = 0;
1038 }
1039
Andy Shevchenkod5785142015-03-03 21:21:58 +02001040 dev_dbg(ap->dev,
1041 "%s qc=%p tag: %x cmd: 0x%02x dma_dir: %s start_dma? %x\n",
1042 __func__, qc, tag, qc->tf.command,
Sergei Shtylyov84b47e32011-01-28 22:01:01 +03001043 get_dma_dir_descript(qc->dma_dir), start_dma);
Andy Shevchenkodb7a6572015-03-03 22:41:22 +02001044 sata_dwc_tf_dump(ap, &qc->tf);
Rupjyoti Sarmah62936002010-07-06 16:36:03 +05301045
1046 if (start_dma) {
Mans Rullgard2d20da02016-04-26 12:03:12 +03001047 sata_dwc_scr_read(&ap->link, SCR_ERROR, &reg);
Rupjyoti Sarmah62936002010-07-06 16:36:03 +05301048 if (reg & SATA_DWC_SERROR_ERR_BITS) {
1049 dev_err(ap->dev, "%s: ****** SError=0x%08x ******\n",
1050 __func__, reg);
1051 }
1052
1053 if (dir == DMA_TO_DEVICE)
Mans Rullgardee81d6c2016-04-26 12:03:20 +03001054 sata_dwc_writel(&hsdev->sata_dwc_regs->dmacr,
1055 SATA_DWC_DMACR_TXCHEN);
Rupjyoti Sarmah62936002010-07-06 16:36:03 +05301056 else
Mans Rullgardee81d6c2016-04-26 12:03:20 +03001057 sata_dwc_writel(&hsdev->sata_dwc_regs->dmacr,
1058 SATA_DWC_DMACR_RXCHEN);
Rupjyoti Sarmah62936002010-07-06 16:36:03 +05301059
1060 /* Enable AHB DMA transfer on the specified channel */
Andy Shevchenko8b344482015-03-03 22:41:21 +02001061 dmaengine_submit(desc);
1062 dma_async_issue_pending(hsdevp->chan);
Rupjyoti Sarmah62936002010-07-06 16:36:03 +05301063 }
1064}
1065
1066static void sata_dwc_bmdma_start(struct ata_queued_cmd *qc)
1067{
Jens Axboe4e5b6262018-05-11 12:51:04 -06001068 u8 tag = qc->hw_tag;
Rupjyoti Sarmah62936002010-07-06 16:36:03 +05301069
1070 if (ata_is_ncq(qc->tf.protocol)) {
1071 dev_dbg(qc->ap->dev, "%s: ap->link.sactive=0x%08x tag=%d\n",
1072 __func__, qc->ap->link.sactive, tag);
1073 } else {
1074 tag = 0;
1075 }
1076 dev_dbg(qc->ap->dev, "%s\n", __func__);
1077 sata_dwc_bmdma_start_by_tag(qc, tag);
1078}
1079
Rupjyoti Sarmah62936002010-07-06 16:36:03 +05301080static unsigned int sata_dwc_qc_issue(struct ata_queued_cmd *qc)
1081{
1082 u32 sactive;
Jens Axboe4e5b6262018-05-11 12:51:04 -06001083 u8 tag = qc->hw_tag;
Rupjyoti Sarmah62936002010-07-06 16:36:03 +05301084 struct ata_port *ap = qc->ap;
Mans Rullgardae4c3482016-04-26 12:03:08 +03001085 struct sata_dwc_device_port *hsdevp = HSDEVP_FROM_AP(ap);
Rupjyoti Sarmah62936002010-07-06 16:36:03 +05301086
1087#ifdef DEBUG_NCQ
Jens Axboe4e5b6262018-05-11 12:51:04 -06001088 if (qc->hw_tag > 0 || ap->link.sactive > 1)
Andy Shevchenkod5785142015-03-03 21:21:58 +02001089 dev_info(ap->dev,
1090 "%s ap id=%d cmd(0x%02x)=%s qc tag=%d prot=%s ap active_tag=0x%08x ap sactive=0x%08x\n",
Rupjyoti Sarmah62936002010-07-06 16:36:03 +05301091 __func__, ap->print_id, qc->tf.command,
Sergei Shtylyovc2119622011-01-28 21:55:55 +03001092 ata_get_cmd_descript(qc->tf.command),
Jens Axboe4e5b6262018-05-11 12:51:04 -06001093 qc->hw_tag, get_prot_descript(qc->tf.protocol),
Rupjyoti Sarmah62936002010-07-06 16:36:03 +05301094 ap->link.active_tag, ap->link.sactive);
1095#endif
1096
1097 if (!ata_is_ncq(qc->tf.protocol))
1098 tag = 0;
Mans Rullgardae4c3482016-04-26 12:03:08 +03001099
1100 if (ata_is_dma(qc->tf.protocol)) {
1101 hsdevp->desc[tag] = dma_dwc_xfer_setup(qc);
1102 if (!hsdevp->desc[tag])
1103 return AC_ERR_SYSTEM;
1104 } else {
1105 hsdevp->desc[tag] = NULL;
1106 }
Rupjyoti Sarmah62936002010-07-06 16:36:03 +05301107
1108 if (ata_is_ncq(qc->tf.protocol)) {
Mans Rullgard2d20da02016-04-26 12:03:12 +03001109 sata_dwc_scr_read(&ap->link, SCR_ACTIVE, &sactive);
Rupjyoti Sarmah62936002010-07-06 16:36:03 +05301110 sactive |= (0x00000001 << tag);
Mans Rullgard2d20da02016-04-26 12:03:12 +03001111 sata_dwc_scr_write(&ap->link, SCR_ACTIVE, sactive);
Rupjyoti Sarmah62936002010-07-06 16:36:03 +05301112
Andy Shevchenkod5785142015-03-03 21:21:58 +02001113 dev_dbg(qc->ap->dev,
1114 "%s: tag=%d ap->link.sactive = 0x%08x sactive=0x%08x\n",
1115 __func__, tag, qc->ap->link.sactive, sactive);
Rupjyoti Sarmah62936002010-07-06 16:36:03 +05301116
1117 ap->ops->sff_tf_load(ap, &qc->tf);
Andy Shevchenko077028e2016-04-26 12:03:09 +03001118 sata_dwc_exec_command_by_tag(ap, &qc->tf, tag,
Rupjyoti Sarmah62936002010-07-06 16:36:03 +05301119 SATA_DWC_CMD_ISSUED_PEND);
1120 } else {
Andy Shevchenko077028e2016-04-26 12:03:09 +03001121 return ata_bmdma_qc_issue(qc);
Rupjyoti Sarmah62936002010-07-06 16:36:03 +05301122 }
1123 return 0;
1124}
1125
Rupjyoti Sarmah62936002010-07-06 16:36:03 +05301126static void sata_dwc_error_handler(struct ata_port *ap)
1127{
Rupjyoti Sarmah62936002010-07-06 16:36:03 +05301128 ata_sff_error_handler(ap);
1129}
1130
Andy Shevchenkod7c256e2015-01-07 15:24:22 +02001131static int sata_dwc_hardreset(struct ata_link *link, unsigned int *class,
1132 unsigned long deadline)
Thang Q. Nguyen3a8b7882012-04-17 15:43:13 +07001133{
1134 struct sata_dwc_device *hsdev = HSDEV_FROM_AP(link->ap);
1135 int ret;
1136
1137 ret = sata_sff_hardreset(link, class, deadline);
1138
1139 sata_dwc_enable_interrupts(hsdev);
1140
1141 /* Reconfigure the DMA control register */
Mans Rullgardee81d6c2016-04-26 12:03:20 +03001142 sata_dwc_writel(&hsdev->sata_dwc_regs->dmacr,
1143 SATA_DWC_DMACR_TXRXCH_CLEAR);
Thang Q. Nguyen3a8b7882012-04-17 15:43:13 +07001144
1145 /* Reconfigure the DMA Burst Transaction Size register */
Mans Rullgardee81d6c2016-04-26 12:03:20 +03001146 sata_dwc_writel(&hsdev->sata_dwc_regs->dbtsr,
1147 SATA_DWC_DBTSR_MWR(AHB_DMA_BRST_DFLT) |
1148 SATA_DWC_DBTSR_MRD(AHB_DMA_BRST_DFLT));
Thang Q. Nguyen3a8b7882012-04-17 15:43:13 +07001149
1150 return ret;
1151}
1152
Christian Lampartera7e6de52016-04-26 12:03:03 +03001153static void sata_dwc_dev_select(struct ata_port *ap, unsigned int device)
1154{
1155 /* SATA DWC is master only */
1156}
1157
Rupjyoti Sarmah62936002010-07-06 16:36:03 +05301158/*
1159 * scsi mid-layer and libata interface structures
1160 */
1161static struct scsi_host_template sata_dwc_sht = {
1162 ATA_NCQ_SHT(DRV_NAME),
1163 /*
1164 * test-only: Currently this driver doesn't handle NCQ
1165 * correctly. We enable NCQ but set the queue depth to a
1166 * max of 1. This will get fixed in in a future release.
1167 */
1168 .sg_tablesize = LIBATA_MAX_PRD,
Andy Shevchenkod7c256e2015-01-07 15:24:22 +02001169 /* .can_queue = ATA_MAX_QUEUE, */
Andy Shevchenko6689dfa2016-04-26 12:03:04 +03001170 /*
1171 * Make sure a LLI block is not created that will span 8K max FIS
1172 * boundary. If the block spans such a FIS boundary, there is a chance
1173 * that a DMA burst will cross that boundary -- this results in an
1174 * error in the host controller.
1175 */
1176 .dma_boundary = 0x1fff /* ATA_DMA_BOUNDARY */,
Rupjyoti Sarmah62936002010-07-06 16:36:03 +05301177};
1178
1179static struct ata_port_operations sata_dwc_ops = {
1180 .inherits = &ata_sff_port_ops,
1181
1182 .error_handler = sata_dwc_error_handler,
Thang Q. Nguyen3a8b7882012-04-17 15:43:13 +07001183 .hardreset = sata_dwc_hardreset,
Rupjyoti Sarmah62936002010-07-06 16:36:03 +05301184
Rupjyoti Sarmah62936002010-07-06 16:36:03 +05301185 .qc_issue = sata_dwc_qc_issue,
1186
1187 .scr_read = sata_dwc_scr_read,
1188 .scr_write = sata_dwc_scr_write,
1189
1190 .port_start = sata_dwc_port_start,
1191 .port_stop = sata_dwc_port_stop,
1192
Christian Lampartera7e6de52016-04-26 12:03:03 +03001193 .sff_dev_select = sata_dwc_dev_select,
1194
Rupjyoti Sarmah62936002010-07-06 16:36:03 +05301195 .bmdma_setup = sata_dwc_bmdma_setup,
1196 .bmdma_start = sata_dwc_bmdma_start,
1197};
1198
1199static const struct ata_port_info sata_dwc_port_info[] = {
1200 {
Sergei Shtylyov9cbe0562011-02-04 22:05:48 +03001201 .flags = ATA_FLAG_SATA | ATA_FLAG_NCQ,
Sergei Shtylyovb83a4c32011-01-25 19:27:35 +03001202 .pio_mask = ATA_PIO4,
Rupjyoti Sarmah62936002010-07-06 16:36:03 +05301203 .udma_mask = ATA_UDMA6,
1204 .port_ops = &sata_dwc_ops,
1205 },
1206};
1207
Grant Likely1c48a5c2011-02-17 02:43:24 -07001208static int sata_dwc_probe(struct platform_device *ofdev)
Rupjyoti Sarmah62936002010-07-06 16:36:03 +05301209{
1210 struct sata_dwc_device *hsdev;
1211 u32 idr, versionr;
1212 char *ver = (char *)&versionr;
Mans Rullgard175553e2016-04-26 12:03:16 +03001213 void __iomem *base;
Rupjyoti Sarmah62936002010-07-06 16:36:03 +05301214 int err = 0;
Andy Shevchenko4aaa7182015-01-07 15:24:19 +02001215 int irq;
Rupjyoti Sarmah62936002010-07-06 16:36:03 +05301216 struct ata_host *host;
1217 struct ata_port_info pi = sata_dwc_port_info[0];
1218 const struct ata_port_info *ppi[] = { &pi, NULL };
Thang Q. Nguyendc7f71f2012-05-10 11:17:10 +07001219 struct device_node *np = ofdev->dev.of_node;
Andy Shevchenko73ec1b52016-04-26 12:03:22 +03001220 struct resource *res;
Rupjyoti Sarmah62936002010-07-06 16:36:03 +05301221
1222 /* Allocate DWC SATA device */
Andy Shevchenkod537fc02015-01-08 12:50:14 +02001223 host = ata_host_alloc_pinfo(&ofdev->dev, ppi, SATA_DWC_MAX_PORTS);
1224 hsdev = devm_kzalloc(&ofdev->dev, sizeof(*hsdev), GFP_KERNEL);
1225 if (!host || !hsdev)
Andy Shevchenkoc592b742015-01-08 12:50:13 +02001226 return -ENOMEM;
Rupjyoti Sarmah62936002010-07-06 16:36:03 +05301227
Andy Shevchenkod537fc02015-01-08 12:50:14 +02001228 host->private_data = hsdev;
1229
Rupjyoti Sarmah62936002010-07-06 16:36:03 +05301230 /* Ioremap SATA registers */
Andy Shevchenko73ec1b52016-04-26 12:03:22 +03001231 res = platform_get_resource(ofdev, IORESOURCE_MEM, 0);
1232 base = devm_ioremap_resource(&ofdev->dev, res);
Wei Yongjun01c29202016-07-19 11:27:53 +00001233 if (IS_ERR(base))
Andy Shevchenko73ec1b52016-04-26 12:03:22 +03001234 return PTR_ERR(base);
Rupjyoti Sarmah62936002010-07-06 16:36:03 +05301235 dev_dbg(&ofdev->dev, "ioremap done for SATA register address\n");
1236
1237 /* Synopsys DWC SATA specific Registers */
Mans Rullgard175553e2016-04-26 12:03:16 +03001238 hsdev->sata_dwc_regs = base + SATA_DWC_REG_OFFSET;
Andy Shevchenkod6ecf0c2016-04-26 12:03:18 +03001239 hsdev->dmadr = res->start + SATA_DWC_REG_OFFSET + offsetof(struct sata_dwc_regs, dmadr);
Rupjyoti Sarmah62936002010-07-06 16:36:03 +05301240
Rupjyoti Sarmah62936002010-07-06 16:36:03 +05301241 /* Setup port */
1242 host->ports[0]->ioaddr.cmd_addr = base;
1243 host->ports[0]->ioaddr.scr_addr = base + SATA_DWC_SCR_OFFSET;
Mans Rullgardae95d952016-04-26 12:03:15 +03001244 sata_dwc_setup_port(&host->ports[0]->ioaddr, base);
Rupjyoti Sarmah62936002010-07-06 16:36:03 +05301245
1246 /* Read the ID and Version Registers */
Mans Rullgardee81d6c2016-04-26 12:03:20 +03001247 idr = sata_dwc_readl(&hsdev->sata_dwc_regs->idr);
1248 versionr = sata_dwc_readl(&hsdev->sata_dwc_regs->versionr);
Rupjyoti Sarmah62936002010-07-06 16:36:03 +05301249 dev_notice(&ofdev->dev, "id %d, controller version %c.%c%c\n",
1250 idr, ver[0], ver[1], ver[2]);
1251
Rupjyoti Sarmah62936002010-07-06 16:36:03 +05301252 /* Save dev for later use in dev_xxx() routines */
Andy Shevchenkodb7a6572015-03-03 22:41:22 +02001253 hsdev->dev = &ofdev->dev;
Rupjyoti Sarmah62936002010-07-06 16:36:03 +05301254
Rupjyoti Sarmah62936002010-07-06 16:36:03 +05301255 /* Enable SATA Interrupts */
1256 sata_dwc_enable_interrupts(hsdev);
1257
1258 /* Get SATA interrupt number */
Andy Shevchenko90379082015-01-08 12:50:12 +02001259 irq = irq_of_parse_and_map(np, 0);
Rupjyoti Sarmah62936002010-07-06 16:36:03 +05301260 if (irq == NO_IRQ) {
1261 dev_err(&ofdev->dev, "no SATA DMA irq\n");
1262 err = -ENODEV;
1263 goto error_out;
1264 }
1265
Mans Rullgard50b43372016-04-26 12:03:10 +03001266#ifdef CONFIG_SATA_DWC_OLD_DMA
1267 if (!of_find_property(np, "dmas", NULL)) {
1268 err = sata_dwc_dma_init_old(ofdev, hsdev);
1269 if (err)
1270 goto error_out;
1271 }
1272#endif
1273
Mans Rullgard0f48deb2016-04-26 12:03:11 +03001274 hsdev->phy = devm_phy_optional_get(hsdev->dev, "sata-phy");
1275 if (IS_ERR(hsdev->phy)) {
1276 err = PTR_ERR(hsdev->phy);
1277 hsdev->phy = NULL;
1278 goto error_out;
1279 }
1280
1281 err = phy_init(hsdev->phy);
1282 if (err)
1283 goto error_out;
1284
Rupjyoti Sarmah62936002010-07-06 16:36:03 +05301285 /*
1286 * Now, register with libATA core, this will also initiate the
1287 * device discovery process, invoking our port_start() handler &
1288 * error_handler() to execute a dummy Softreset EH session
1289 */
Andy Shevchenko4aaa7182015-01-07 15:24:19 +02001290 err = ata_host_activate(host, irq, sata_dwc_isr, 0, &sata_dwc_sht);
1291 if (err)
Rupjyoti Sarmah62936002010-07-06 16:36:03 +05301292 dev_err(&ofdev->dev, "failed to activate host");
1293
Rupjyoti Sarmah62936002010-07-06 16:36:03 +05301294 return 0;
1295
1296error_out:
Mans Rullgard0f48deb2016-04-26 12:03:11 +03001297 phy_exit(hsdev->phy);
Rupjyoti Sarmah62936002010-07-06 16:36:03 +05301298 return err;
1299}
1300
Stephen Rothwell60652d02010-08-16 12:20:59 +10001301static int sata_dwc_remove(struct platform_device *ofdev)
Rupjyoti Sarmah62936002010-07-06 16:36:03 +05301302{
1303 struct device *dev = &ofdev->dev;
1304 struct ata_host *host = dev_get_drvdata(dev);
1305 struct sata_dwc_device *hsdev = host->private_data;
1306
1307 ata_host_detach(host);
Rupjyoti Sarmah62936002010-07-06 16:36:03 +05301308
Mans Rullgard0f48deb2016-04-26 12:03:11 +03001309 phy_exit(hsdev->phy);
1310
Mans Rullgard50b43372016-04-26 12:03:10 +03001311#ifdef CONFIG_SATA_DWC_OLD_DMA
Rupjyoti Sarmah62936002010-07-06 16:36:03 +05301312 /* Free SATA DMA resources */
Mans Rullgard50b43372016-04-26 12:03:10 +03001313 sata_dwc_dma_exit_old(hsdev);
1314#endif
Rupjyoti Sarmah62936002010-07-06 16:36:03 +05301315
Rupjyoti Sarmah62936002010-07-06 16:36:03 +05301316 dev_dbg(&ofdev->dev, "done\n");
1317 return 0;
1318}
1319
1320static const struct of_device_id sata_dwc_match[] = {
1321 { .compatible = "amcc,sata-460ex", },
1322 {}
1323};
1324MODULE_DEVICE_TABLE(of, sata_dwc_match);
1325
Grant Likely1c48a5c2011-02-17 02:43:24 -07001326static struct platform_driver sata_dwc_driver = {
Rupjyoti Sarmah62936002010-07-06 16:36:03 +05301327 .driver = {
1328 .name = DRV_NAME,
Rupjyoti Sarmah62936002010-07-06 16:36:03 +05301329 .of_match_table = sata_dwc_match,
1330 },
1331 .probe = sata_dwc_probe,
1332 .remove = sata_dwc_remove,
1333};
1334
Axel Lin99c8ea32011-11-27 14:44:26 +08001335module_platform_driver(sata_dwc_driver);
Rupjyoti Sarmah62936002010-07-06 16:36:03 +05301336
1337MODULE_LICENSE("GPL");
1338MODULE_AUTHOR("Mark Miesfeld <mmiesfeld@amcc.com>");
Andy Shevchenkod5785142015-03-03 21:21:58 +02001339MODULE_DESCRIPTION("DesignWare Cores SATA controller low level driver");
Rupjyoti Sarmah62936002010-07-06 16:36:03 +05301340MODULE_VERSION(DRV_VERSION);