blob: 10b6a59fe499619daae62acf997bcbade0f36c70 [file] [log] [blame]
Rupjyoti Sarmah62936002010-07-06 16:36:03 +05301/*
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
21#ifdef CONFIG_SATA_DWC_DEBUG
22#define DEBUG
23#endif
24
25#ifdef CONFIG_SATA_DWC_VDEBUG
26#define VERBOSE_DEBUG
27#define DEBUG_NCQ
28#endif
29
30#include <linux/kernel.h>
31#include <linux/module.h>
Rupjyoti Sarmah62936002010-07-06 16:36:03 +053032#include <linux/device.h>
Mans Rullgard50b43372016-04-26 12:03:10 +030033#include <linux/dmaengine.h>
Rob Herringc11eede2013-11-10 23:19:08 -060034#include <linux/of_address.h>
35#include <linux/of_irq.h>
Rupjyoti Sarmah62936002010-07-06 16:36:03 +053036#include <linux/of_platform.h>
37#include <linux/platform_device.h>
Mans Rullgard0f48deb2016-04-26 12:03:11 +030038#include <linux/phy/phy.h>
Rupjyoti Sarmah62936002010-07-06 16:36:03 +053039#include <linux/libata.h>
40#include <linux/slab.h>
Andy Shevchenko8b344482015-03-03 22:41:21 +020041
Rupjyoti Sarmah62936002010-07-06 16:36:03 +053042#include "libata.h"
43
44#include <scsi/scsi_host.h>
45#include <scsi/scsi_cmnd.h>
46
Sergei Shtylyovc2119622011-01-28 21:55:55 +030047/* These two are defined in "libata.h" */
48#undef DRV_NAME
49#undef DRV_VERSION
Jeff Garzik72d5f2d2012-12-14 09:43:39 -050050
Rupjyoti Sarmah62936002010-07-06 16:36:03 +053051#define DRV_NAME "sata-dwc"
Sergei Shtylyov84b47e32011-01-28 22:01:01 +030052#define DRV_VERSION "1.3"
Rupjyoti Sarmah62936002010-07-06 16:36:03 +053053
Andy Shevchenko84683a72015-01-07 15:24:21 +020054#ifndef out_le32
55#define out_le32(a, v) __raw_writel(__cpu_to_le32(v), (void __iomem *)(a))
56#endif
57
58#ifndef in_le32
59#define in_le32(a) __le32_to_cpu(__raw_readl((void __iomem *)(a)))
60#endif
61
62#ifndef NO_IRQ
63#define NO_IRQ 0
64#endif
65
Andy Shevchenko4ea8c202016-04-26 12:03:05 +030066#define AHB_DMA_BRST_DFLT 64 /* 16 data items burst length */
Rupjyoti Sarmah62936002010-07-06 16:36:03 +053067
Rupjyoti Sarmah62936002010-07-06 16:36:03 +053068enum {
Rupjyoti Sarmah62936002010-07-06 16:36:03 +053069 SATA_DWC_MAX_PORTS = 1,
70
71 SATA_DWC_SCR_OFFSET = 0x24,
72 SATA_DWC_REG_OFFSET = 0x64,
73};
74
75/* DWC SATA Registers */
76struct sata_dwc_regs {
77 u32 fptagr; /* 1st party DMA tag */
78 u32 fpbor; /* 1st party DMA buffer offset */
79 u32 fptcr; /* 1st party DMA Xfr count */
80 u32 dmacr; /* DMA Control */
81 u32 dbtsr; /* DMA Burst Transac size */
82 u32 intpr; /* Interrupt Pending */
83 u32 intmr; /* Interrupt Mask */
84 u32 errmr; /* Error Mask */
85 u32 llcr; /* Link Layer Control */
86 u32 phycr; /* PHY Control */
87 u32 physr; /* PHY Status */
88 u32 rxbistpd; /* Recvd BIST pattern def register */
89 u32 rxbistpd1; /* Recvd BIST data dword1 */
90 u32 rxbistpd2; /* Recvd BIST pattern data dword2 */
91 u32 txbistpd; /* Trans BIST pattern def register */
92 u32 txbistpd1; /* Trans BIST data dword1 */
93 u32 txbistpd2; /* Trans BIST data dword2 */
94 u32 bistcr; /* BIST Control Register */
95 u32 bistfctr; /* BIST FIS Count Register */
96 u32 bistsr; /* BIST Status Register */
97 u32 bistdecr; /* BIST Dword Error count register */
98 u32 res[15]; /* Reserved locations */
99 u32 testr; /* Test Register */
100 u32 versionr; /* Version Register */
101 u32 idr; /* ID Register */
102 u32 unimpl[192]; /* Unimplemented */
103 u32 dmadr[256]; /* FIFO Locations in DMA Mode */
104};
105
106enum {
107 SCR_SCONTROL_DET_ENABLE = 0x00000001,
108 SCR_SSTATUS_DET_PRESENT = 0x00000001,
109 SCR_SERROR_DIAG_X = 0x04000000,
110/* DWC SATA Register Operations */
111 SATA_DWC_TXFIFO_DEPTH = 0x01FF,
112 SATA_DWC_RXFIFO_DEPTH = 0x01FF,
113 SATA_DWC_DMACR_TMOD_TXCHEN = 0x00000004,
114 SATA_DWC_DMACR_TXCHEN = (0x00000001 | SATA_DWC_DMACR_TMOD_TXCHEN),
115 SATA_DWC_DMACR_RXCHEN = (0x00000002 | SATA_DWC_DMACR_TMOD_TXCHEN),
116 SATA_DWC_DMACR_TXRXCH_CLEAR = SATA_DWC_DMACR_TMOD_TXCHEN,
117 SATA_DWC_INTPR_DMAT = 0x00000001,
118 SATA_DWC_INTPR_NEWFP = 0x00000002,
119 SATA_DWC_INTPR_PMABRT = 0x00000004,
120 SATA_DWC_INTPR_ERR = 0x00000008,
121 SATA_DWC_INTPR_NEWBIST = 0x00000010,
122 SATA_DWC_INTPR_IPF = 0x10000000,
123 SATA_DWC_INTMR_DMATM = 0x00000001,
124 SATA_DWC_INTMR_NEWFPM = 0x00000002,
125 SATA_DWC_INTMR_PMABRTM = 0x00000004,
126 SATA_DWC_INTMR_ERRM = 0x00000008,
127 SATA_DWC_INTMR_NEWBISTM = 0x00000010,
128 SATA_DWC_LLCR_SCRAMEN = 0x00000001,
129 SATA_DWC_LLCR_DESCRAMEN = 0x00000002,
130 SATA_DWC_LLCR_RPDEN = 0x00000004,
131/* This is all error bits, zero's are reserved fields. */
132 SATA_DWC_SERROR_ERR_BITS = 0x0FFF0F03
133};
134
135#define SATA_DWC_SCR0_SPD_GET(v) (((v) >> 4) & 0x0000000F)
136#define SATA_DWC_DMACR_TX_CLEAR(v) (((v) & ~SATA_DWC_DMACR_TXCHEN) |\
137 SATA_DWC_DMACR_TMOD_TXCHEN)
138#define SATA_DWC_DMACR_RX_CLEAR(v) (((v) & ~SATA_DWC_DMACR_RXCHEN) |\
139 SATA_DWC_DMACR_TMOD_TXCHEN)
140#define SATA_DWC_DBTSR_MWR(size) (((size)/4) & SATA_DWC_TXFIFO_DEPTH)
141#define SATA_DWC_DBTSR_MRD(size) ((((size)/4) & SATA_DWC_RXFIFO_DEPTH)\
142 << 16)
143struct sata_dwc_device {
144 struct device *dev; /* generic device struct */
145 struct ata_probe_ent *pe; /* ptr to probe-ent */
146 struct ata_host *host;
Andy Shevchenkod7c256e2015-01-07 15:24:22 +0200147 u8 __iomem *reg_base;
Mans Rullgardadc64ec2016-04-26 12:03:17 +0300148 struct sata_dwc_regs __iomem *sata_dwc_regs; /* DW SATA specific */
Mans Rullgard2d20da02016-04-26 12:03:12 +0300149 u32 sactive_issued;
150 u32 sactive_queued;
Mans Rullgard0f48deb2016-04-26 12:03:11 +0300151 struct phy *phy;
Mans Rullgard50b43372016-04-26 12:03:10 +0300152#ifdef CONFIG_SATA_DWC_OLD_DMA
Andy Shevchenko8b344482015-03-03 22:41:21 +0200153 struct dw_dma_chip *dma;
Mans Rullgard50b43372016-04-26 12:03:10 +0300154#endif
Rupjyoti Sarmah62936002010-07-06 16:36:03 +0530155};
156
157#define SATA_DWC_QCMD_MAX 32
158
159struct sata_dwc_device_port {
160 struct sata_dwc_device *hsdev;
161 int cmd_issued[SATA_DWC_QCMD_MAX];
Rupjyoti Sarmah62936002010-07-06 16:36:03 +0530162 int dma_pending[SATA_DWC_QCMD_MAX];
Andy Shevchenko8b344482015-03-03 22:41:21 +0200163
164 /* DMA info */
Andy Shevchenko8b344482015-03-03 22:41:21 +0200165 struct dma_chan *chan;
166 struct dma_async_tx_descriptor *desc[SATA_DWC_QCMD_MAX];
167 u32 dma_interrupt_count;
Rupjyoti Sarmah62936002010-07-06 16:36:03 +0530168};
169
170/*
Andy Shevchenko4bb41be2016-04-26 12:03:13 +0300171 * Commonly used DWC SATA driver macros
Rupjyoti Sarmah62936002010-07-06 16:36:03 +0530172 */
Andy Shevchenko4bb41be2016-04-26 12:03:13 +0300173#define HSDEV_FROM_HOST(host) ((struct sata_dwc_device *)(host)->private_data)
174#define HSDEV_FROM_AP(ap) ((struct sata_dwc_device *)(ap)->host->private_data)
175#define HSDEVP_FROM_AP(ap) ((struct sata_dwc_device_port *)(ap)->private_data)
176#define HSDEV_FROM_QC(qc) ((struct sata_dwc_device *)(qc)->ap->host->private_data)
177#define HSDEV_FROM_HSDEVP(p) ((struct sata_dwc_device *)(p)->hsdev)
Rupjyoti Sarmah62936002010-07-06 16:36:03 +0530178
179enum {
180 SATA_DWC_CMD_ISSUED_NOT = 0,
181 SATA_DWC_CMD_ISSUED_PEND = 1,
182 SATA_DWC_CMD_ISSUED_EXEC = 2,
183 SATA_DWC_CMD_ISSUED_NODATA = 3,
184
185 SATA_DWC_DMA_PENDING_NONE = 0,
186 SATA_DWC_DMA_PENDING_TX = 1,
187 SATA_DWC_DMA_PENDING_RX = 2,
188};
189
Rupjyoti Sarmah62936002010-07-06 16:36:03 +0530190/*
191 * Prototypes
192 */
193static void sata_dwc_bmdma_start_by_tag(struct ata_queued_cmd *qc, u8 tag);
194static int sata_dwc_qc_complete(struct ata_port *ap, struct ata_queued_cmd *qc,
195 u32 check_status);
196static void sata_dwc_dma_xfer_complete(struct ata_port *ap, u32 check_status);
197static void sata_dwc_port_stop(struct ata_port *ap);
198static void sata_dwc_clear_dmacr(struct sata_dwc_device_port *hsdevp, u8 tag);
Rupjyoti Sarmah62936002010-07-06 16:36:03 +0530199
Mans Rullgard50b43372016-04-26 12:03:10 +0300200#ifdef CONFIG_SATA_DWC_OLD_DMA
201
202#include <linux/platform_data/dma-dw.h>
203#include <linux/dma/dw.h>
204
205static struct dw_dma_slave sata_dwc_dma_dws = {
206 .src_id = 0,
207 .dst_id = 0,
208 .m_master = 1,
209 .p_master = 0,
210};
211
212static bool sata_dwc_dma_filter(struct dma_chan *chan, void *param)
213{
214 struct dw_dma_slave *dws = &sata_dwc_dma_dws;
215
216 if (dws->dma_dev != chan->device->dev)
217 return false;
218
219 chan->private = dws;
220 return true;
221}
222
223static int sata_dwc_dma_get_channel_old(struct sata_dwc_device_port *hsdevp)
224{
225 struct sata_dwc_device *hsdev = hsdevp->hsdev;
226 struct dw_dma_slave *dws = &sata_dwc_dma_dws;
227 dma_cap_mask_t mask;
228
229 dws->dma_dev = hsdev->dev;
230
231 dma_cap_zero(mask);
232 dma_cap_set(DMA_SLAVE, mask);
233
234 /* Acquire DMA channel */
235 hsdevp->chan = dma_request_channel(mask, sata_dwc_dma_filter, hsdevp);
236 if (!hsdevp->chan) {
237 dev_err(hsdev->dev, "%s: dma channel unavailable\n",
238 __func__);
239 return -EAGAIN;
240 }
241
242 return 0;
243}
244
245static int sata_dwc_dma_init_old(struct platform_device *pdev,
246 struct sata_dwc_device *hsdev)
247{
248 struct device_node *np = pdev->dev.of_node;
249 int err;
250
251 hsdev->dma = devm_kzalloc(&pdev->dev, sizeof(*hsdev->dma), GFP_KERNEL);
252 if (!hsdev->dma)
253 return -ENOMEM;
254
255 hsdev->dma->dev = &pdev->dev;
256
257 /* Get SATA DMA interrupt number */
258 hsdev->dma->irq = irq_of_parse_and_map(np, 1);
259 if (hsdev->dma->irq == NO_IRQ) {
260 dev_err(&pdev->dev, "no SATA DMA irq\n");
261 return -ENODEV;
262 }
263
264 /* Get physical SATA DMA register base address */
265 hsdev->dma->regs = of_iomap(np, 1);
266 if (!hsdev->dma->regs) {
267 dev_err(&pdev->dev,
268 "ioremap failed for AHBDMA register address\n");
269 return -ENODEV;
270 }
271
272 /* Initialize AHB DMAC */
273 err = dw_dma_probe(hsdev->dma);
274 if (err) {
275 iounmap(hsdev->dma->regs);
276 return err;
277 }
278
279 return 0;
280}
281
282static void sata_dwc_dma_exit_old(struct sata_dwc_device *hsdev)
283{
284 if (!hsdev->dma)
285 return;
286
287 dw_dma_remove(hsdev->dma);
288 iounmap(hsdev->dma->regs);
289}
290
291#endif
292
Sergei Shtylyov84b47e32011-01-28 22:01:01 +0300293static const char *get_prot_descript(u8 protocol)
294{
295 switch ((enum ata_tf_protocols)protocol) {
296 case ATA_PROT_NODATA:
297 return "ATA no data";
298 case ATA_PROT_PIO:
299 return "ATA PIO";
300 case ATA_PROT_DMA:
301 return "ATA DMA";
302 case ATA_PROT_NCQ:
303 return "ATA NCQ";
304 case ATAPI_PROT_NODATA:
305 return "ATAPI no data";
306 case ATAPI_PROT_PIO:
307 return "ATAPI PIO";
308 case ATAPI_PROT_DMA:
309 return "ATAPI DMA";
310 default:
311 return "unknown";
312 }
313}
314
315static const char *get_dma_dir_descript(int dma_dir)
316{
317 switch ((enum dma_data_direction)dma_dir) {
318 case DMA_BIDIRECTIONAL:
319 return "bidirectional";
320 case DMA_TO_DEVICE:
321 return "to device";
322 case DMA_FROM_DEVICE:
323 return "from device";
324 default:
325 return "none";
326 }
327}
328
Andy Shevchenkodb7a6572015-03-03 22:41:22 +0200329static void sata_dwc_tf_dump(struct ata_port *ap, struct ata_taskfile *tf)
Rupjyoti Sarmah62936002010-07-06 16:36:03 +0530330{
Andy Shevchenkodb7a6572015-03-03 22:41:22 +0200331 dev_vdbg(ap->dev,
Andy Shevchenkod5785142015-03-03 21:21:58 +0200332 "taskfile cmd: 0x%02x protocol: %s flags: 0x%lx device: %x\n",
333 tf->command, get_prot_descript(tf->protocol), tf->flags,
334 tf->device);
Andy Shevchenkodb7a6572015-03-03 22:41:22 +0200335 dev_vdbg(ap->dev,
Andy Shevchenkod5785142015-03-03 21:21:58 +0200336 "feature: 0x%02x nsect: 0x%x lbal: 0x%x lbam: 0x%x lbah: 0x%x\n",
337 tf->feature, tf->nsect, tf->lbal, tf->lbam, tf->lbah);
Andy Shevchenkodb7a6572015-03-03 22:41:22 +0200338 dev_vdbg(ap->dev,
Andy Shevchenkod5785142015-03-03 21:21:58 +0200339 "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 +0530340 tf->hob_feature, tf->hob_nsect, tf->hob_lbal, tf->hob_lbam,
341 tf->hob_lbah);
342}
343
Andy Shevchenko8b344482015-03-03 22:41:21 +0200344static void dma_dwc_xfer_done(void *hsdev_instance)
Rupjyoti Sarmah62936002010-07-06 16:36:03 +0530345{
Rupjyoti Sarmah62936002010-07-06 16:36:03 +0530346 unsigned long flags;
Joe Perchesd5185d62014-03-26 09:34:49 -0700347 struct sata_dwc_device *hsdev = hsdev_instance;
Rupjyoti Sarmah62936002010-07-06 16:36:03 +0530348 struct ata_host *host = (struct ata_host *)hsdev->host;
349 struct ata_port *ap;
350 struct sata_dwc_device_port *hsdevp;
351 u8 tag = 0;
352 unsigned int port = 0;
353
354 spin_lock_irqsave(&host->lock, flags);
355 ap = host->ports[port];
356 hsdevp = HSDEVP_FROM_AP(ap);
357 tag = ap->link.active_tag;
358
Rupjyoti Sarmah62936002010-07-06 16:36:03 +0530359 /*
Andy Shevchenko8b344482015-03-03 22:41:21 +0200360 * Each DMA command produces 2 interrupts. Only
361 * complete the command after both interrupts have been
362 * seen. (See sata_dwc_isr())
Rupjyoti Sarmah62936002010-07-06 16:36:03 +0530363 */
Andy Shevchenko8b344482015-03-03 22:41:21 +0200364 hsdevp->dma_interrupt_count++;
365 sata_dwc_clear_dmacr(hsdevp, tag);
Rupjyoti Sarmah62936002010-07-06 16:36:03 +0530366
Andy Shevchenko8b344482015-03-03 22:41:21 +0200367 if (hsdevp->dma_pending[tag] == SATA_DWC_DMA_PENDING_NONE) {
368 dev_err(ap->dev, "DMA not pending tag=0x%02x pending=%d\n",
369 tag, hsdevp->dma_pending[tag]);
Rupjyoti Sarmah62936002010-07-06 16:36:03 +0530370 }
371
Andy Shevchenko8b344482015-03-03 22:41:21 +0200372 if ((hsdevp->dma_interrupt_count % 2) == 0)
Dan Carpenter8d5fe8d2015-03-30 13:30:25 +0300373 sata_dwc_dma_xfer_complete(ap, 1);
Andy Shevchenko8b344482015-03-03 22:41:21 +0200374
375 spin_unlock_irqrestore(&host->lock, flags);
Rupjyoti Sarmah62936002010-07-06 16:36:03 +0530376}
377
Andy Shevchenko8b344482015-03-03 22:41:21 +0200378static struct dma_async_tx_descriptor *dma_dwc_xfer_setup(struct ata_queued_cmd *qc)
Rupjyoti Sarmah62936002010-07-06 16:36:03 +0530379{
Andy Shevchenko8b344482015-03-03 22:41:21 +0200380 struct ata_port *ap = qc->ap;
381 struct sata_dwc_device_port *hsdevp = HSDEVP_FROM_AP(ap);
382 struct sata_dwc_device *hsdev = HSDEV_FROM_AP(ap);
383 dma_addr_t addr = (dma_addr_t)&hsdev->sata_dwc_regs->dmadr;
384 struct dma_slave_config sconf;
385 struct dma_async_tx_descriptor *desc;
Rupjyoti Sarmah62936002010-07-06 16:36:03 +0530386
Andy Shevchenko8b344482015-03-03 22:41:21 +0200387 if (qc->dma_dir == DMA_DEV_TO_MEM) {
388 sconf.src_addr = addr;
Andy Shevchenko59a75ce2016-04-26 12:03:06 +0300389 sconf.device_fc = false;
Andy Shevchenko8b344482015-03-03 22:41:21 +0200390 } else { /* DMA_MEM_TO_DEV */
391 sconf.dst_addr = addr;
392 sconf.device_fc = false;
Rupjyoti Sarmah62936002010-07-06 16:36:03 +0530393 }
394
Andy Shevchenko8b344482015-03-03 22:41:21 +0200395 sconf.direction = qc->dma_dir;
Andy Shevchenko4ea8c202016-04-26 12:03:05 +0300396 sconf.src_maxburst = AHB_DMA_BRST_DFLT / 4; /* in items */
397 sconf.dst_maxburst = AHB_DMA_BRST_DFLT / 4; /* in items */
Andy Shevchenko8b344482015-03-03 22:41:21 +0200398 sconf.src_addr_width = DMA_SLAVE_BUSWIDTH_4_BYTES;
399 sconf.dst_addr_width = DMA_SLAVE_BUSWIDTH_4_BYTES;
400
401 dmaengine_slave_config(hsdevp->chan, &sconf);
402
Rupjyoti Sarmah62936002010-07-06 16:36:03 +0530403 /* Convert SG list to linked list of items (LLIs) for AHB DMA */
Andy Shevchenko8b344482015-03-03 22:41:21 +0200404 desc = dmaengine_prep_slave_sg(hsdevp->chan, qc->sg, qc->n_elem,
405 qc->dma_dir,
406 DMA_PREP_INTERRUPT | DMA_CTRL_ACK);
Rupjyoti Sarmah62936002010-07-06 16:36:03 +0530407
Andy Shevchenko8b344482015-03-03 22:41:21 +0200408 if (!desc)
409 return NULL;
Rupjyoti Sarmah62936002010-07-06 16:36:03 +0530410
Andy Shevchenko8b344482015-03-03 22:41:21 +0200411 desc->callback = dma_dwc_xfer_done;
412 desc->callback_param = hsdev;
Rupjyoti Sarmah62936002010-07-06 16:36:03 +0530413
Andy Shevchenkodb7a6572015-03-03 22:41:22 +0200414 dev_dbg(hsdev->dev, "%s sg: 0x%p, count: %d addr: %pad\n",
Andy Shevchenko8b344482015-03-03 22:41:21 +0200415 __func__, qc->sg, qc->n_elem, &addr);
Rupjyoti Sarmah62936002010-07-06 16:36:03 +0530416
Andy Shevchenko8b344482015-03-03 22:41:21 +0200417 return desc;
Rupjyoti Sarmah62936002010-07-06 16:36:03 +0530418}
419
420static int sata_dwc_scr_read(struct ata_link *link, unsigned int scr, u32 *val)
421{
422 if (scr > SCR_NOTIFICATION) {
423 dev_err(link->ap->dev, "%s: Incorrect SCR offset 0x%02x\n",
424 __func__, scr);
425 return -EINVAL;
426 }
427
Andy Shevchenkod7c256e2015-01-07 15:24:22 +0200428 *val = in_le32(link->ap->ioaddr.scr_addr + (scr * 4));
Rupjyoti Sarmah62936002010-07-06 16:36:03 +0530429 dev_dbg(link->ap->dev, "%s: id=%d reg=%d val=val=0x%08x\n",
430 __func__, link->ap->print_id, scr, *val);
431
432 return 0;
433}
434
435static int sata_dwc_scr_write(struct ata_link *link, unsigned int scr, u32 val)
436{
437 dev_dbg(link->ap->dev, "%s: id=%d reg=%d val=val=0x%08x\n",
438 __func__, link->ap->print_id, scr, val);
439 if (scr > SCR_NOTIFICATION) {
440 dev_err(link->ap->dev, "%s: Incorrect SCR offset 0x%02x\n",
441 __func__, scr);
442 return -EINVAL;
443 }
Andy Shevchenkod7c256e2015-01-07 15:24:22 +0200444 out_le32(link->ap->ioaddr.scr_addr + (scr * 4), val);
Rupjyoti Sarmah62936002010-07-06 16:36:03 +0530445
446 return 0;
447}
448
Mans Rullgard2d20da02016-04-26 12:03:12 +0300449static void clear_serror(struct ata_port *ap)
Rupjyoti Sarmah62936002010-07-06 16:36:03 +0530450{
451 u32 val;
Mans Rullgard2d20da02016-04-26 12:03:12 +0300452 sata_dwc_scr_read(&ap->link, SCR_ERROR, &val);
453 sata_dwc_scr_write(&ap->link, SCR_ERROR, val);
Rupjyoti Sarmah62936002010-07-06 16:36:03 +0530454}
455
456static void clear_interrupt_bit(struct sata_dwc_device *hsdev, u32 bit)
457{
458 out_le32(&hsdev->sata_dwc_regs->intpr,
459 in_le32(&hsdev->sata_dwc_regs->intpr));
460}
461
462static u32 qcmd_tag_to_mask(u8 tag)
463{
464 return 0x00000001 << (tag & 0x1f);
465}
466
467/* See ahci.c */
468static void sata_dwc_error_intr(struct ata_port *ap,
469 struct sata_dwc_device *hsdev, uint intpr)
470{
471 struct sata_dwc_device_port *hsdevp = HSDEVP_FROM_AP(ap);
472 struct ata_eh_info *ehi = &ap->link.eh_info;
473 unsigned int err_mask = 0, action = 0;
474 struct ata_queued_cmd *qc;
475 u32 serror;
476 u8 status, tag;
Rupjyoti Sarmah62936002010-07-06 16:36:03 +0530477
478 ata_ehi_clear_desc(ehi);
479
Mans Rullgard2d20da02016-04-26 12:03:12 +0300480 sata_dwc_scr_read(&ap->link, SCR_ERROR, &serror);
Rupjyoti Sarmah62936002010-07-06 16:36:03 +0530481 status = ap->ops->sff_check_status(ap);
482
Rupjyoti Sarmah62936002010-07-06 16:36:03 +0530483 tag = ap->link.active_tag;
484
Andy Shevchenko8b344482015-03-03 22:41:21 +0200485 dev_err(ap->dev,
486 "%s SCR_ERROR=0x%08x intpr=0x%08x status=0x%08x dma_intp=%d pending=%d issued=%d",
487 __func__, serror, intpr, status, hsdevp->dma_interrupt_count,
488 hsdevp->dma_pending[tag], hsdevp->cmd_issued[tag]);
Rupjyoti Sarmah62936002010-07-06 16:36:03 +0530489
490 /* Clear error register and interrupt bit */
Mans Rullgard2d20da02016-04-26 12:03:12 +0300491 clear_serror(ap);
Rupjyoti Sarmah62936002010-07-06 16:36:03 +0530492 clear_interrupt_bit(hsdev, SATA_DWC_INTPR_ERR);
493
494 /* This is the only error happening now. TODO check for exact error */
495
496 err_mask |= AC_ERR_HOST_BUS;
497 action |= ATA_EH_RESET;
498
499 /* Pass this on to EH */
500 ehi->serror |= serror;
501 ehi->action |= action;
502
503 qc = ata_qc_from_tag(ap, tag);
504 if (qc)
505 qc->err_mask |= err_mask;
506 else
507 ehi->err_mask |= err_mask;
508
509 ata_port_abort(ap);
510}
511
512/*
513 * Function : sata_dwc_isr
514 * arguments : irq, void *dev_instance, struct pt_regs *regs
515 * Return value : irqreturn_t - status of IRQ
516 * This Interrupt handler called via port ops registered function.
517 * .irq_handler = sata_dwc_isr
518 */
519static irqreturn_t sata_dwc_isr(int irq, void *dev_instance)
520{
521 struct ata_host *host = (struct ata_host *)dev_instance;
522 struct sata_dwc_device *hsdev = HSDEV_FROM_HOST(host);
523 struct ata_port *ap;
524 struct ata_queued_cmd *qc;
525 unsigned long flags;
526 u8 status, tag;
527 int handled, num_processed, port = 0;
528 uint intpr, sactive, sactive2, tag_mask;
529 struct sata_dwc_device_port *hsdevp;
Mans Rullgard2d20da02016-04-26 12:03:12 +0300530 hsdev->sactive_issued = 0;
Rupjyoti Sarmah62936002010-07-06 16:36:03 +0530531
532 spin_lock_irqsave(&host->lock, flags);
533
534 /* Read the interrupt register */
535 intpr = in_le32(&hsdev->sata_dwc_regs->intpr);
536
537 ap = host->ports[port];
538 hsdevp = HSDEVP_FROM_AP(ap);
539
540 dev_dbg(ap->dev, "%s intpr=0x%08x active_tag=%d\n", __func__, intpr,
541 ap->link.active_tag);
542
543 /* Check for error interrupt */
544 if (intpr & SATA_DWC_INTPR_ERR) {
545 sata_dwc_error_intr(ap, hsdev, intpr);
546 handled = 1;
547 goto DONE;
548 }
549
550 /* Check for DMA SETUP FIS (FP DMA) interrupt */
551 if (intpr & SATA_DWC_INTPR_NEWFP) {
552 clear_interrupt_bit(hsdev, SATA_DWC_INTPR_NEWFP);
553
554 tag = (u8)(in_le32(&hsdev->sata_dwc_regs->fptagr));
555 dev_dbg(ap->dev, "%s: NEWFP tag=%d\n", __func__, tag);
556 if (hsdevp->cmd_issued[tag] != SATA_DWC_CMD_ISSUED_PEND)
557 dev_warn(ap->dev, "CMD tag=%d not pending?\n", tag);
558
Mans Rullgard2d20da02016-04-26 12:03:12 +0300559 hsdev->sactive_issued |= qcmd_tag_to_mask(tag);
Rupjyoti Sarmah62936002010-07-06 16:36:03 +0530560
561 qc = ata_qc_from_tag(ap, tag);
562 /*
563 * Start FP DMA for NCQ command. At this point the tag is the
564 * active tag. It is the tag that matches the command about to
565 * be completed.
566 */
567 qc->ap->link.active_tag = tag;
568 sata_dwc_bmdma_start_by_tag(qc, tag);
569
570 handled = 1;
571 goto DONE;
572 }
Mans Rullgard2d20da02016-04-26 12:03:12 +0300573 sata_dwc_scr_read(&ap->link, SCR_ACTIVE, &sactive);
574 tag_mask = (hsdev->sactive_issued | sactive) ^ sactive;
Rupjyoti Sarmah62936002010-07-06 16:36:03 +0530575
576 /* If no sactive issued and tag_mask is zero then this is not NCQ */
Mans Rullgard2d20da02016-04-26 12:03:12 +0300577 if (hsdev->sactive_issued == 0 && tag_mask == 0) {
Rupjyoti Sarmah62936002010-07-06 16:36:03 +0530578 if (ap->link.active_tag == ATA_TAG_POISON)
579 tag = 0;
580 else
581 tag = ap->link.active_tag;
582 qc = ata_qc_from_tag(ap, tag);
583
584 /* DEV interrupt w/ no active qc? */
585 if (unlikely(!qc || (qc->tf.flags & ATA_TFLAG_POLLING))) {
Andy Shevchenkod5785142015-03-03 21:21:58 +0200586 dev_err(ap->dev,
587 "%s interrupt with no active qc qc=%p\n",
588 __func__, qc);
Rupjyoti Sarmah62936002010-07-06 16:36:03 +0530589 ap->ops->sff_check_status(ap);
590 handled = 1;
591 goto DONE;
592 }
593 status = ap->ops->sff_check_status(ap);
594
595 qc->ap->link.active_tag = tag;
596 hsdevp->cmd_issued[tag] = SATA_DWC_CMD_ISSUED_NOT;
597
598 if (status & ATA_ERR) {
599 dev_dbg(ap->dev, "interrupt ATA_ERR (0x%x)\n", status);
600 sata_dwc_qc_complete(ap, qc, 1);
601 handled = 1;
602 goto DONE;
603 }
604
605 dev_dbg(ap->dev, "%s non-NCQ cmd interrupt, protocol: %s\n",
Sergei Shtylyov84b47e32011-01-28 22:01:01 +0300606 __func__, get_prot_descript(qc->tf.protocol));
Rupjyoti Sarmah62936002010-07-06 16:36:03 +0530607DRVSTILLBUSY:
608 if (ata_is_dma(qc->tf.protocol)) {
609 /*
610 * Each DMA transaction produces 2 interrupts. The DMAC
611 * transfer complete interrupt and the SATA controller
612 * operation done interrupt. The command should be
613 * completed only after both interrupts are seen.
614 */
Andy Shevchenko8b344482015-03-03 22:41:21 +0200615 hsdevp->dma_interrupt_count++;
Rupjyoti Sarmah62936002010-07-06 16:36:03 +0530616 if (hsdevp->dma_pending[tag] == \
617 SATA_DWC_DMA_PENDING_NONE) {
Andy Shevchenkod5785142015-03-03 21:21:58 +0200618 dev_err(ap->dev,
619 "%s: DMA not pending intpr=0x%08x status=0x%08x pending=%d\n",
620 __func__, intpr, status,
Rupjyoti Sarmah62936002010-07-06 16:36:03 +0530621 hsdevp->dma_pending[tag]);
622 }
623
Andy Shevchenko8b344482015-03-03 22:41:21 +0200624 if ((hsdevp->dma_interrupt_count % 2) == 0)
Rupjyoti Sarmah62936002010-07-06 16:36:03 +0530625 sata_dwc_dma_xfer_complete(ap, 1);
626 } else if (ata_is_pio(qc->tf.protocol)) {
627 ata_sff_hsm_move(ap, qc, status, 0);
628 handled = 1;
629 goto DONE;
630 } else {
631 if (unlikely(sata_dwc_qc_complete(ap, qc, 1)))
632 goto DRVSTILLBUSY;
633 }
634
635 handled = 1;
636 goto DONE;
637 }
638
639 /*
640 * This is a NCQ command. At this point we need to figure out for which
641 * tags we have gotten a completion interrupt. One interrupt may serve
642 * as completion for more than one operation when commands are queued
643 * (NCQ). We need to process each completed command.
644 */
645
646 /* process completed commands */
Mans Rullgard2d20da02016-04-26 12:03:12 +0300647 sata_dwc_scr_read(&ap->link, SCR_ACTIVE, &sactive);
648 tag_mask = (hsdev->sactive_issued | sactive) ^ sactive;
Rupjyoti Sarmah62936002010-07-06 16:36:03 +0530649
Mans Rullgard2d20da02016-04-26 12:03:12 +0300650 if (sactive != 0 || hsdev->sactive_issued > 1 || tag_mask > 1) {
Andy Shevchenkod5785142015-03-03 21:21:58 +0200651 dev_dbg(ap->dev,
652 "%s NCQ:sactive=0x%08x sactive_issued=0x%08x tag_mask=0x%08x\n",
Mans Rullgard2d20da02016-04-26 12:03:12 +0300653 __func__, sactive, hsdev->sactive_issued, tag_mask);
Rupjyoti Sarmah62936002010-07-06 16:36:03 +0530654 }
655
Mans Rullgard2d20da02016-04-26 12:03:12 +0300656 if ((tag_mask | hsdev->sactive_issued) != hsdev->sactive_issued) {
Andy Shevchenkod5785142015-03-03 21:21:58 +0200657 dev_warn(ap->dev,
Mans Rullgard2d20da02016-04-26 12:03:12 +0300658 "Bad tag mask? sactive=0x%08x sactive_issued=0x%08x tag_mask=0x%08x\n",
659 sactive, hsdev->sactive_issued, tag_mask);
Rupjyoti Sarmah62936002010-07-06 16:36:03 +0530660 }
661
662 /* read just to clear ... not bad if currently still busy */
663 status = ap->ops->sff_check_status(ap);
664 dev_dbg(ap->dev, "%s ATA status register=0x%x\n", __func__, status);
665
666 tag = 0;
667 num_processed = 0;
668 while (tag_mask) {
669 num_processed++;
670 while (!(tag_mask & 0x00000001)) {
671 tag++;
672 tag_mask <<= 1;
673 }
674
675 tag_mask &= (~0x00000001);
676 qc = ata_qc_from_tag(ap, tag);
677
678 /* To be picked up by completion functions */
679 qc->ap->link.active_tag = tag;
680 hsdevp->cmd_issued[tag] = SATA_DWC_CMD_ISSUED_NOT;
681
682 /* Let libata/scsi layers handle error */
683 if (status & ATA_ERR) {
684 dev_dbg(ap->dev, "%s ATA_ERR (0x%x)\n", __func__,
685 status);
686 sata_dwc_qc_complete(ap, qc, 1);
687 handled = 1;
688 goto DONE;
689 }
690
691 /* Process completed command */
692 dev_dbg(ap->dev, "%s NCQ command, protocol: %s\n", __func__,
Sergei Shtylyov84b47e32011-01-28 22:01:01 +0300693 get_prot_descript(qc->tf.protocol));
Rupjyoti Sarmah62936002010-07-06 16:36:03 +0530694 if (ata_is_dma(qc->tf.protocol)) {
Andy Shevchenko8b344482015-03-03 22:41:21 +0200695 hsdevp->dma_interrupt_count++;
Rupjyoti Sarmah62936002010-07-06 16:36:03 +0530696 if (hsdevp->dma_pending[tag] == \
697 SATA_DWC_DMA_PENDING_NONE)
698 dev_warn(ap->dev, "%s: DMA not pending?\n",
699 __func__);
Andy Shevchenko8b344482015-03-03 22:41:21 +0200700 if ((hsdevp->dma_interrupt_count % 2) == 0)
Rupjyoti Sarmah62936002010-07-06 16:36:03 +0530701 sata_dwc_dma_xfer_complete(ap, 1);
702 } else {
703 if (unlikely(sata_dwc_qc_complete(ap, qc, 1)))
704 goto STILLBUSY;
705 }
706 continue;
707
708STILLBUSY:
709 ap->stats.idle_irq++;
710 dev_warn(ap->dev, "STILL BUSY IRQ ata%d: irq trap\n",
711 ap->print_id);
712 } /* while tag_mask */
713
714 /*
715 * Check to see if any commands completed while we were processing our
716 * initial set of completed commands (read status clears interrupts,
717 * so we might miss a completed command interrupt if one came in while
718 * we were processing --we read status as part of processing a completed
719 * command).
720 */
Mans Rullgard2d20da02016-04-26 12:03:12 +0300721 sata_dwc_scr_read(&ap->link, SCR_ACTIVE, &sactive2);
Rupjyoti Sarmah62936002010-07-06 16:36:03 +0530722 if (sactive2 != sactive) {
Andy Shevchenkod5785142015-03-03 21:21:58 +0200723 dev_dbg(ap->dev,
724 "More completed - sactive=0x%x sactive2=0x%x\n",
725 sactive, sactive2);
Rupjyoti Sarmah62936002010-07-06 16:36:03 +0530726 }
727 handled = 1;
728
729DONE:
730 spin_unlock_irqrestore(&host->lock, flags);
731 return IRQ_RETVAL(handled);
732}
733
734static void sata_dwc_clear_dmacr(struct sata_dwc_device_port *hsdevp, u8 tag)
735{
736 struct sata_dwc_device *hsdev = HSDEV_FROM_HSDEVP(hsdevp);
737
738 if (hsdevp->dma_pending[tag] == SATA_DWC_DMA_PENDING_RX) {
739 out_le32(&(hsdev->sata_dwc_regs->dmacr),
740 SATA_DWC_DMACR_RX_CLEAR(
741 in_le32(&(hsdev->sata_dwc_regs->dmacr))));
742 } else if (hsdevp->dma_pending[tag] == SATA_DWC_DMA_PENDING_TX) {
743 out_le32(&(hsdev->sata_dwc_regs->dmacr),
744 SATA_DWC_DMACR_TX_CLEAR(
745 in_le32(&(hsdev->sata_dwc_regs->dmacr))));
746 } else {
747 /*
748 * This should not happen, it indicates the driver is out of
749 * sync. If it does happen, clear dmacr anyway.
750 */
Andy Shevchenkodb7a6572015-03-03 22:41:22 +0200751 dev_err(hsdev->dev,
Andy Shevchenkod5785142015-03-03 21:21:58 +0200752 "%s DMA protocol RX and TX DMA not pending tag=0x%02x pending=%d dmacr: 0x%08x\n",
753 __func__, tag, hsdevp->dma_pending[tag],
754 in_le32(&hsdev->sata_dwc_regs->dmacr));
Rupjyoti Sarmah62936002010-07-06 16:36:03 +0530755 out_le32(&(hsdev->sata_dwc_regs->dmacr),
756 SATA_DWC_DMACR_TXRXCH_CLEAR);
757 }
758}
759
760static void sata_dwc_dma_xfer_complete(struct ata_port *ap, u32 check_status)
761{
762 struct ata_queued_cmd *qc;
763 struct sata_dwc_device_port *hsdevp = HSDEVP_FROM_AP(ap);
764 struct sata_dwc_device *hsdev = HSDEV_FROM_AP(ap);
765 u8 tag = 0;
766
767 tag = ap->link.active_tag;
768 qc = ata_qc_from_tag(ap, tag);
769 if (!qc) {
770 dev_err(ap->dev, "failed to get qc");
771 return;
772 }
773
774#ifdef DEBUG_NCQ
775 if (tag > 0) {
Andy Shevchenkod5785142015-03-03 21:21:58 +0200776 dev_info(ap->dev,
777 "%s tag=%u cmd=0x%02x dma dir=%s proto=%s dmacr=0x%08x\n",
778 __func__, qc->tag, qc->tf.command,
Sergei Shtylyov84b47e32011-01-28 22:01:01 +0300779 get_dma_dir_descript(qc->dma_dir),
780 get_prot_descript(qc->tf.protocol),
Rupjyoti Sarmah62936002010-07-06 16:36:03 +0530781 in_le32(&(hsdev->sata_dwc_regs->dmacr)));
782 }
783#endif
784
785 if (ata_is_dma(qc->tf.protocol)) {
786 if (hsdevp->dma_pending[tag] == SATA_DWC_DMA_PENDING_NONE) {
Andy Shevchenkod5785142015-03-03 21:21:58 +0200787 dev_err(ap->dev,
788 "%s DMA protocol RX and TX DMA not pending dmacr: 0x%08x\n",
789 __func__,
Rupjyoti Sarmah62936002010-07-06 16:36:03 +0530790 in_le32(&(hsdev->sata_dwc_regs->dmacr)));
791 }
792
793 hsdevp->dma_pending[tag] = SATA_DWC_DMA_PENDING_NONE;
794 sata_dwc_qc_complete(ap, qc, check_status);
795 ap->link.active_tag = ATA_TAG_POISON;
796 } else {
797 sata_dwc_qc_complete(ap, qc, check_status);
798 }
799}
800
801static int sata_dwc_qc_complete(struct ata_port *ap, struct ata_queued_cmd *qc,
802 u32 check_status)
803{
804 u8 status = 0;
805 u32 mask = 0x0;
806 u8 tag = qc->tag;
Mans Rullgard2d20da02016-04-26 12:03:12 +0300807 struct sata_dwc_device *hsdev = HSDEV_FROM_AP(ap);
Rupjyoti Sarmah62936002010-07-06 16:36:03 +0530808 struct sata_dwc_device_port *hsdevp = HSDEVP_FROM_AP(ap);
Mans Rullgard2d20da02016-04-26 12:03:12 +0300809 hsdev->sactive_queued = 0;
Rupjyoti Sarmah62936002010-07-06 16:36:03 +0530810 dev_dbg(ap->dev, "%s checkstatus? %x\n", __func__, check_status);
811
812 if (hsdevp->dma_pending[tag] == SATA_DWC_DMA_PENDING_TX)
813 dev_err(ap->dev, "TX DMA PENDING\n");
814 else if (hsdevp->dma_pending[tag] == SATA_DWC_DMA_PENDING_RX)
815 dev_err(ap->dev, "RX DMA PENDING\n");
Andy Shevchenkod5785142015-03-03 21:21:58 +0200816 dev_dbg(ap->dev,
817 "QC complete cmd=0x%02x status=0x%02x ata%u: protocol=%d\n",
818 qc->tf.command, status, ap->print_id, qc->tf.protocol);
Rupjyoti Sarmah62936002010-07-06 16:36:03 +0530819
820 /* clear active bit */
821 mask = (~(qcmd_tag_to_mask(tag)));
Mans Rullgard2d20da02016-04-26 12:03:12 +0300822 hsdev->sactive_queued = hsdev->sactive_queued & mask;
823 hsdev->sactive_issued = hsdev->sactive_issued & mask;
Rupjyoti Sarmah62936002010-07-06 16:36:03 +0530824 ata_qc_complete(qc);
825 return 0;
826}
827
828static void sata_dwc_enable_interrupts(struct sata_dwc_device *hsdev)
829{
830 /* Enable selective interrupts by setting the interrupt maskregister*/
831 out_le32(&hsdev->sata_dwc_regs->intmr,
832 SATA_DWC_INTMR_ERRM |
833 SATA_DWC_INTMR_NEWFPM |
834 SATA_DWC_INTMR_PMABRTM |
835 SATA_DWC_INTMR_DMATM);
836 /*
837 * Unmask the error bits that should trigger an error interrupt by
838 * setting the error mask register.
839 */
840 out_le32(&hsdev->sata_dwc_regs->errmr, SATA_DWC_SERROR_ERR_BITS);
841
Andy Shevchenkodb7a6572015-03-03 22:41:22 +0200842 dev_dbg(hsdev->dev, "%s: INTMR = 0x%08x, ERRMR = 0x%08x\n",
Rupjyoti Sarmah62936002010-07-06 16:36:03 +0530843 __func__, in_le32(&hsdev->sata_dwc_regs->intmr),
844 in_le32(&hsdev->sata_dwc_regs->errmr));
845}
846
Mans Rullgardae95d952016-04-26 12:03:15 +0300847static void sata_dwc_setup_port(struct ata_ioports *port, void __iomem *base)
Rupjyoti Sarmah62936002010-07-06 16:36:03 +0530848{
Mans Rullgardae95d952016-04-26 12:03:15 +0300849 port->cmd_addr = base + 0x00;
850 port->data_addr = base + 0x00;
Rupjyoti Sarmah62936002010-07-06 16:36:03 +0530851
Mans Rullgardae95d952016-04-26 12:03:15 +0300852 port->error_addr = base + 0x04;
853 port->feature_addr = base + 0x04;
Rupjyoti Sarmah62936002010-07-06 16:36:03 +0530854
Mans Rullgardae95d952016-04-26 12:03:15 +0300855 port->nsect_addr = base + 0x08;
Rupjyoti Sarmah62936002010-07-06 16:36:03 +0530856
Mans Rullgardae95d952016-04-26 12:03:15 +0300857 port->lbal_addr = base + 0x0c;
858 port->lbam_addr = base + 0x10;
859 port->lbah_addr = base + 0x14;
Rupjyoti Sarmah62936002010-07-06 16:36:03 +0530860
Mans Rullgardae95d952016-04-26 12:03:15 +0300861 port->device_addr = base + 0x18;
862 port->command_addr = base + 0x1c;
863 port->status_addr = base + 0x1c;
Rupjyoti Sarmah62936002010-07-06 16:36:03 +0530864
Mans Rullgardae95d952016-04-26 12:03:15 +0300865 port->altstatus_addr = base + 0x20;
866 port->ctl_addr = base + 0x20;
Rupjyoti Sarmah62936002010-07-06 16:36:03 +0530867}
868
Mans Rullgard50b43372016-04-26 12:03:10 +0300869static int sata_dwc_dma_get_channel(struct sata_dwc_device_port *hsdevp)
870{
871 struct sata_dwc_device *hsdev = hsdevp->hsdev;
872 struct device *dev = hsdev->dev;
873
874#ifdef CONFIG_SATA_DWC_OLD_DMA
875 if (!of_find_property(dev->of_node, "dmas", NULL))
876 return sata_dwc_dma_get_channel_old(hsdevp);
877#endif
878
879 hsdevp->chan = dma_request_chan(dev, "sata-dma");
880 if (IS_ERR(hsdevp->chan)) {
881 dev_err(dev, "failed to allocate dma channel: %ld\n",
882 PTR_ERR(hsdevp->chan));
883 return PTR_ERR(hsdevp->chan);
884 }
885
886 return 0;
887}
888
Rupjyoti Sarmah62936002010-07-06 16:36:03 +0530889/*
890 * Function : sata_dwc_port_start
891 * arguments : struct ata_ioports *port
892 * Return value : returns 0 if success, error code otherwise
893 * This function allocates the scatter gather LLI table for AHB DMA
894 */
895static int sata_dwc_port_start(struct ata_port *ap)
896{
897 int err = 0;
898 struct sata_dwc_device *hsdev;
899 struct sata_dwc_device_port *hsdevp = NULL;
900 struct device *pdev;
901 int i;
902
903 hsdev = HSDEV_FROM_AP(ap);
904
905 dev_dbg(ap->dev, "%s: port_no=%d\n", __func__, ap->port_no);
906
907 hsdev->host = ap->host;
908 pdev = ap->host->dev;
909 if (!pdev) {
910 dev_err(ap->dev, "%s: no ap->host->dev\n", __func__);
911 err = -ENODEV;
912 goto CLEANUP;
913 }
914
915 /* Allocate Port Struct */
916 hsdevp = kzalloc(sizeof(*hsdevp), GFP_KERNEL);
917 if (!hsdevp) {
918 dev_err(ap->dev, "%s: kmalloc failed for hsdevp\n", __func__);
919 err = -ENOMEM;
920 goto CLEANUP;
921 }
922 hsdevp->hsdev = hsdev;
923
Mans Rullgard50b43372016-04-26 12:03:10 +0300924 err = sata_dwc_dma_get_channel(hsdevp);
925 if (err)
Andy Shevchenko8b344482015-03-03 22:41:21 +0200926 goto CLEANUP_ALLOC;
Andy Shevchenko8b344482015-03-03 22:41:21 +0200927
Mans Rullgard0f48deb2016-04-26 12:03:11 +0300928 err = phy_power_on(hsdev->phy);
929 if (err)
930 goto CLEANUP_ALLOC;
931
Rupjyoti Sarmah62936002010-07-06 16:36:03 +0530932 for (i = 0; i < SATA_DWC_QCMD_MAX; i++)
933 hsdevp->cmd_issued[i] = SATA_DWC_CMD_ISSUED_NOT;
934
Andy Shevchenkod7c256e2015-01-07 15:24:22 +0200935 ap->bmdma_prd = NULL; /* set these so libata doesn't use them */
Rupjyoti Sarmah62936002010-07-06 16:36:03 +0530936 ap->bmdma_prd_dma = 0;
937
Rupjyoti Sarmah62936002010-07-06 16:36:03 +0530938 if (ap->port_no == 0) {
939 dev_dbg(ap->dev, "%s: clearing TXCHEN, RXCHEN in DMAC\n",
940 __func__);
941 out_le32(&hsdev->sata_dwc_regs->dmacr,
942 SATA_DWC_DMACR_TXRXCH_CLEAR);
943
944 dev_dbg(ap->dev, "%s: setting burst size in DBTSR\n",
945 __func__);
946 out_le32(&hsdev->sata_dwc_regs->dbtsr,
947 (SATA_DWC_DBTSR_MWR(AHB_DMA_BRST_DFLT) |
948 SATA_DWC_DBTSR_MRD(AHB_DMA_BRST_DFLT)));
949 }
950
951 /* Clear any error bits before libata starts issuing commands */
Mans Rullgard2d20da02016-04-26 12:03:12 +0300952 clear_serror(ap);
Rupjyoti Sarmah62936002010-07-06 16:36:03 +0530953 ap->private_data = hsdevp;
Julia Lawalla081da62011-08-08 13:17:57 +0200954 dev_dbg(ap->dev, "%s: done\n", __func__);
955 return 0;
Rupjyoti Sarmah62936002010-07-06 16:36:03 +0530956
Julia Lawalla081da62011-08-08 13:17:57 +0200957CLEANUP_ALLOC:
958 kfree(hsdevp);
Rupjyoti Sarmah62936002010-07-06 16:36:03 +0530959CLEANUP:
Julia Lawalla081da62011-08-08 13:17:57 +0200960 dev_dbg(ap->dev, "%s: fail. ap->id = %d\n", __func__, ap->print_id);
Rupjyoti Sarmah62936002010-07-06 16:36:03 +0530961 return err;
962}
963
964static void sata_dwc_port_stop(struct ata_port *ap)
965{
Rupjyoti Sarmah62936002010-07-06 16:36:03 +0530966 struct sata_dwc_device_port *hsdevp = HSDEVP_FROM_AP(ap);
Mans Rullgard0f48deb2016-04-26 12:03:11 +0300967 struct sata_dwc_device *hsdev = HSDEV_FROM_AP(ap);
Rupjyoti Sarmah62936002010-07-06 16:36:03 +0530968
969 dev_dbg(ap->dev, "%s: ap->id = %d\n", __func__, ap->print_id);
970
Andy Shevchenko9e8b8552016-04-26 12:03:19 +0300971 dmaengine_terminate_sync(hsdevp->chan);
Andy Shevchenko8b344482015-03-03 22:41:21 +0200972 dma_release_channel(hsdevp->chan);
Mans Rullgard0f48deb2016-04-26 12:03:11 +0300973 phy_power_off(hsdev->phy);
Rupjyoti Sarmah62936002010-07-06 16:36:03 +0530974
Andy Shevchenko8b344482015-03-03 22:41:21 +0200975 kfree(hsdevp);
Rupjyoti Sarmah62936002010-07-06 16:36:03 +0530976 ap->private_data = NULL;
977}
978
979/*
980 * Function : sata_dwc_exec_command_by_tag
981 * arguments : ata_port *ap, ata_taskfile *tf, u8 tag, u32 cmd_issued
982 * Return value : None
983 * This function keeps track of individual command tag ids and calls
984 * ata_exec_command in libata
985 */
986static void sata_dwc_exec_command_by_tag(struct ata_port *ap,
987 struct ata_taskfile *tf,
988 u8 tag, u32 cmd_issued)
989{
Rupjyoti Sarmah62936002010-07-06 16:36:03 +0530990 struct sata_dwc_device_port *hsdevp = HSDEVP_FROM_AP(ap);
991
992 dev_dbg(ap->dev, "%s cmd(0x%02x): %s tag=%d\n", __func__, tf->command,
Sergei Shtylyovc2119622011-01-28 21:55:55 +0300993 ata_get_cmd_descript(tf->command), tag);
Rupjyoti Sarmah62936002010-07-06 16:36:03 +0530994
Rupjyoti Sarmah62936002010-07-06 16:36:03 +0530995 hsdevp->cmd_issued[tag] = cmd_issued;
Mans Rullgard55e610c2016-04-26 12:03:02 +0300996
Rupjyoti Sarmah62936002010-07-06 16:36:03 +0530997 /*
998 * Clear SError before executing a new command.
999 * sata_dwc_scr_write and read can not be used here. Clearing the PM
1000 * managed SError register for the disk needs to be done before the
1001 * task file is loaded.
1002 */
Mans Rullgard2d20da02016-04-26 12:03:12 +03001003 clear_serror(ap);
Rupjyoti Sarmah62936002010-07-06 16:36:03 +05301004 ata_sff_exec_command(ap, tf);
1005}
1006
1007static void sata_dwc_bmdma_setup_by_tag(struct ata_queued_cmd *qc, u8 tag)
1008{
1009 sata_dwc_exec_command_by_tag(qc->ap, &qc->tf, tag,
1010 SATA_DWC_CMD_ISSUED_PEND);
1011}
1012
1013static void sata_dwc_bmdma_setup(struct ata_queued_cmd *qc)
1014{
1015 u8 tag = qc->tag;
1016
1017 if (ata_is_ncq(qc->tf.protocol)) {
1018 dev_dbg(qc->ap->dev, "%s: ap->link.sactive=0x%08x tag=%d\n",
1019 __func__, qc->ap->link.sactive, tag);
1020 } else {
1021 tag = 0;
1022 }
1023 sata_dwc_bmdma_setup_by_tag(qc, tag);
1024}
1025
1026static void sata_dwc_bmdma_start_by_tag(struct ata_queued_cmd *qc, u8 tag)
1027{
1028 int start_dma;
Andy Shevchenko8b344482015-03-03 22:41:21 +02001029 u32 reg;
Rupjyoti Sarmah62936002010-07-06 16:36:03 +05301030 struct sata_dwc_device *hsdev = HSDEV_FROM_QC(qc);
1031 struct ata_port *ap = qc->ap;
1032 struct sata_dwc_device_port *hsdevp = HSDEVP_FROM_AP(ap);
Andy Shevchenko8b344482015-03-03 22:41:21 +02001033 struct dma_async_tx_descriptor *desc = hsdevp->desc[tag];
Rupjyoti Sarmah62936002010-07-06 16:36:03 +05301034 int dir = qc->dma_dir;
Rupjyoti Sarmah62936002010-07-06 16:36:03 +05301035
1036 if (hsdevp->cmd_issued[tag] != SATA_DWC_CMD_ISSUED_NOT) {
1037 start_dma = 1;
1038 if (dir == DMA_TO_DEVICE)
1039 hsdevp->dma_pending[tag] = SATA_DWC_DMA_PENDING_TX;
1040 else
1041 hsdevp->dma_pending[tag] = SATA_DWC_DMA_PENDING_RX;
1042 } else {
Andy Shevchenkod5785142015-03-03 21:21:58 +02001043 dev_err(ap->dev,
1044 "%s: Command not pending cmd_issued=%d (tag=%d) DMA NOT started\n",
1045 __func__, hsdevp->cmd_issued[tag], tag);
Rupjyoti Sarmah62936002010-07-06 16:36:03 +05301046 start_dma = 0;
1047 }
1048
Andy Shevchenkod5785142015-03-03 21:21:58 +02001049 dev_dbg(ap->dev,
1050 "%s qc=%p tag: %x cmd: 0x%02x dma_dir: %s start_dma? %x\n",
1051 __func__, qc, tag, qc->tf.command,
Sergei Shtylyov84b47e32011-01-28 22:01:01 +03001052 get_dma_dir_descript(qc->dma_dir), start_dma);
Andy Shevchenkodb7a6572015-03-03 22:41:22 +02001053 sata_dwc_tf_dump(ap, &qc->tf);
Rupjyoti Sarmah62936002010-07-06 16:36:03 +05301054
1055 if (start_dma) {
Mans Rullgard2d20da02016-04-26 12:03:12 +03001056 sata_dwc_scr_read(&ap->link, SCR_ERROR, &reg);
Rupjyoti Sarmah62936002010-07-06 16:36:03 +05301057 if (reg & SATA_DWC_SERROR_ERR_BITS) {
1058 dev_err(ap->dev, "%s: ****** SError=0x%08x ******\n",
1059 __func__, reg);
1060 }
1061
1062 if (dir == DMA_TO_DEVICE)
1063 out_le32(&hsdev->sata_dwc_regs->dmacr,
1064 SATA_DWC_DMACR_TXCHEN);
1065 else
1066 out_le32(&hsdev->sata_dwc_regs->dmacr,
1067 SATA_DWC_DMACR_RXCHEN);
1068
1069 /* Enable AHB DMA transfer on the specified channel */
Andy Shevchenko8b344482015-03-03 22:41:21 +02001070 dmaengine_submit(desc);
1071 dma_async_issue_pending(hsdevp->chan);
Rupjyoti Sarmah62936002010-07-06 16:36:03 +05301072 }
1073}
1074
1075static void sata_dwc_bmdma_start(struct ata_queued_cmd *qc)
1076{
1077 u8 tag = qc->tag;
1078
1079 if (ata_is_ncq(qc->tf.protocol)) {
1080 dev_dbg(qc->ap->dev, "%s: ap->link.sactive=0x%08x tag=%d\n",
1081 __func__, qc->ap->link.sactive, tag);
1082 } else {
1083 tag = 0;
1084 }
1085 dev_dbg(qc->ap->dev, "%s\n", __func__);
1086 sata_dwc_bmdma_start_by_tag(qc, tag);
1087}
1088
Rupjyoti Sarmah62936002010-07-06 16:36:03 +05301089static unsigned int sata_dwc_qc_issue(struct ata_queued_cmd *qc)
1090{
1091 u32 sactive;
1092 u8 tag = qc->tag;
1093 struct ata_port *ap = qc->ap;
Mans Rullgardae4c3482016-04-26 12:03:08 +03001094 struct sata_dwc_device_port *hsdevp = HSDEVP_FROM_AP(ap);
Rupjyoti Sarmah62936002010-07-06 16:36:03 +05301095
1096#ifdef DEBUG_NCQ
1097 if (qc->tag > 0 || ap->link.sactive > 1)
Andy Shevchenkod5785142015-03-03 21:21:58 +02001098 dev_info(ap->dev,
1099 "%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 +05301100 __func__, ap->print_id, qc->tf.command,
Sergei Shtylyovc2119622011-01-28 21:55:55 +03001101 ata_get_cmd_descript(qc->tf.command),
Sergei Shtylyov84b47e32011-01-28 22:01:01 +03001102 qc->tag, get_prot_descript(qc->tf.protocol),
Rupjyoti Sarmah62936002010-07-06 16:36:03 +05301103 ap->link.active_tag, ap->link.sactive);
1104#endif
1105
1106 if (!ata_is_ncq(qc->tf.protocol))
1107 tag = 0;
Mans Rullgardae4c3482016-04-26 12:03:08 +03001108
1109 if (ata_is_dma(qc->tf.protocol)) {
1110 hsdevp->desc[tag] = dma_dwc_xfer_setup(qc);
1111 if (!hsdevp->desc[tag])
1112 return AC_ERR_SYSTEM;
1113 } else {
1114 hsdevp->desc[tag] = NULL;
1115 }
Rupjyoti Sarmah62936002010-07-06 16:36:03 +05301116
1117 if (ata_is_ncq(qc->tf.protocol)) {
Mans Rullgard2d20da02016-04-26 12:03:12 +03001118 sata_dwc_scr_read(&ap->link, SCR_ACTIVE, &sactive);
Rupjyoti Sarmah62936002010-07-06 16:36:03 +05301119 sactive |= (0x00000001 << tag);
Mans Rullgard2d20da02016-04-26 12:03:12 +03001120 sata_dwc_scr_write(&ap->link, SCR_ACTIVE, sactive);
Rupjyoti Sarmah62936002010-07-06 16:36:03 +05301121
Andy Shevchenkod5785142015-03-03 21:21:58 +02001122 dev_dbg(qc->ap->dev,
1123 "%s: tag=%d ap->link.sactive = 0x%08x sactive=0x%08x\n",
1124 __func__, tag, qc->ap->link.sactive, sactive);
Rupjyoti Sarmah62936002010-07-06 16:36:03 +05301125
1126 ap->ops->sff_tf_load(ap, &qc->tf);
Andy Shevchenko077028e2016-04-26 12:03:09 +03001127 sata_dwc_exec_command_by_tag(ap, &qc->tf, tag,
Rupjyoti Sarmah62936002010-07-06 16:36:03 +05301128 SATA_DWC_CMD_ISSUED_PEND);
1129 } else {
Andy Shevchenko077028e2016-04-26 12:03:09 +03001130 return ata_bmdma_qc_issue(qc);
Rupjyoti Sarmah62936002010-07-06 16:36:03 +05301131 }
1132 return 0;
1133}
1134
Rupjyoti Sarmah62936002010-07-06 16:36:03 +05301135static void sata_dwc_error_handler(struct ata_port *ap)
1136{
Rupjyoti Sarmah62936002010-07-06 16:36:03 +05301137 ata_sff_error_handler(ap);
1138}
1139
Andy Shevchenkod7c256e2015-01-07 15:24:22 +02001140static int sata_dwc_hardreset(struct ata_link *link, unsigned int *class,
1141 unsigned long deadline)
Thang Q. Nguyen3a8b7882012-04-17 15:43:13 +07001142{
1143 struct sata_dwc_device *hsdev = HSDEV_FROM_AP(link->ap);
1144 int ret;
1145
1146 ret = sata_sff_hardreset(link, class, deadline);
1147
1148 sata_dwc_enable_interrupts(hsdev);
1149
1150 /* Reconfigure the DMA control register */
1151 out_le32(&hsdev->sata_dwc_regs->dmacr,
1152 SATA_DWC_DMACR_TXRXCH_CLEAR);
1153
1154 /* Reconfigure the DMA Burst Transaction Size register */
1155 out_le32(&hsdev->sata_dwc_regs->dbtsr,
1156 SATA_DWC_DBTSR_MWR(AHB_DMA_BRST_DFLT) |
1157 SATA_DWC_DBTSR_MRD(AHB_DMA_BRST_DFLT));
1158
1159 return ret;
1160}
1161
Christian Lampartera7e6de52016-04-26 12:03:03 +03001162static void sata_dwc_dev_select(struct ata_port *ap, unsigned int device)
1163{
1164 /* SATA DWC is master only */
1165}
1166
Rupjyoti Sarmah62936002010-07-06 16:36:03 +05301167/*
1168 * scsi mid-layer and libata interface structures
1169 */
1170static struct scsi_host_template sata_dwc_sht = {
1171 ATA_NCQ_SHT(DRV_NAME),
1172 /*
1173 * test-only: Currently this driver doesn't handle NCQ
1174 * correctly. We enable NCQ but set the queue depth to a
1175 * max of 1. This will get fixed in in a future release.
1176 */
1177 .sg_tablesize = LIBATA_MAX_PRD,
Andy Shevchenkod7c256e2015-01-07 15:24:22 +02001178 /* .can_queue = ATA_MAX_QUEUE, */
Andy Shevchenko6689dfa2016-04-26 12:03:04 +03001179 /*
1180 * Make sure a LLI block is not created that will span 8K max FIS
1181 * boundary. If the block spans such a FIS boundary, there is a chance
1182 * that a DMA burst will cross that boundary -- this results in an
1183 * error in the host controller.
1184 */
1185 .dma_boundary = 0x1fff /* ATA_DMA_BOUNDARY */,
Rupjyoti Sarmah62936002010-07-06 16:36:03 +05301186};
1187
1188static struct ata_port_operations sata_dwc_ops = {
1189 .inherits = &ata_sff_port_ops,
1190
1191 .error_handler = sata_dwc_error_handler,
Thang Q. Nguyen3a8b7882012-04-17 15:43:13 +07001192 .hardreset = sata_dwc_hardreset,
Rupjyoti Sarmah62936002010-07-06 16:36:03 +05301193
Rupjyoti Sarmah62936002010-07-06 16:36:03 +05301194 .qc_issue = sata_dwc_qc_issue,
1195
1196 .scr_read = sata_dwc_scr_read,
1197 .scr_write = sata_dwc_scr_write,
1198
1199 .port_start = sata_dwc_port_start,
1200 .port_stop = sata_dwc_port_stop,
1201
Christian Lampartera7e6de52016-04-26 12:03:03 +03001202 .sff_dev_select = sata_dwc_dev_select,
1203
Rupjyoti Sarmah62936002010-07-06 16:36:03 +05301204 .bmdma_setup = sata_dwc_bmdma_setup,
1205 .bmdma_start = sata_dwc_bmdma_start,
1206};
1207
1208static const struct ata_port_info sata_dwc_port_info[] = {
1209 {
Sergei Shtylyov9cbe0562011-02-04 22:05:48 +03001210 .flags = ATA_FLAG_SATA | ATA_FLAG_NCQ,
Sergei Shtylyovb83a4c32011-01-25 19:27:35 +03001211 .pio_mask = ATA_PIO4,
Rupjyoti Sarmah62936002010-07-06 16:36:03 +05301212 .udma_mask = ATA_UDMA6,
1213 .port_ops = &sata_dwc_ops,
1214 },
1215};
1216
Grant Likely1c48a5c2011-02-17 02:43:24 -07001217static int sata_dwc_probe(struct platform_device *ofdev)
Rupjyoti Sarmah62936002010-07-06 16:36:03 +05301218{
1219 struct sata_dwc_device *hsdev;
1220 u32 idr, versionr;
1221 char *ver = (char *)&versionr;
Mans Rullgard175553e2016-04-26 12:03:16 +03001222 void __iomem *base;
Rupjyoti Sarmah62936002010-07-06 16:36:03 +05301223 int err = 0;
Andy Shevchenko4aaa7182015-01-07 15:24:19 +02001224 int irq;
Rupjyoti Sarmah62936002010-07-06 16:36:03 +05301225 struct ata_host *host;
1226 struct ata_port_info pi = sata_dwc_port_info[0];
1227 const struct ata_port_info *ppi[] = { &pi, NULL };
Thang Q. Nguyendc7f71f2012-05-10 11:17:10 +07001228 struct device_node *np = ofdev->dev.of_node;
Rupjyoti Sarmah62936002010-07-06 16:36:03 +05301229
1230 /* Allocate DWC SATA device */
Andy Shevchenkod537fc02015-01-08 12:50:14 +02001231 host = ata_host_alloc_pinfo(&ofdev->dev, ppi, SATA_DWC_MAX_PORTS);
1232 hsdev = devm_kzalloc(&ofdev->dev, sizeof(*hsdev), GFP_KERNEL);
1233 if (!host || !hsdev)
Andy Shevchenkoc592b742015-01-08 12:50:13 +02001234 return -ENOMEM;
Rupjyoti Sarmah62936002010-07-06 16:36:03 +05301235
Andy Shevchenkod537fc02015-01-08 12:50:14 +02001236 host->private_data = hsdev;
1237
Rupjyoti Sarmah62936002010-07-06 16:36:03 +05301238 /* Ioremap SATA registers */
Andy Shevchenko90379082015-01-08 12:50:12 +02001239 base = of_iomap(np, 0);
Rupjyoti Sarmah62936002010-07-06 16:36:03 +05301240 if (!base) {
Andy Shevchenkod5785142015-03-03 21:21:58 +02001241 dev_err(&ofdev->dev,
1242 "ioremap failed for SATA register address\n");
Andy Shevchenkod537fc02015-01-08 12:50:14 +02001243 return -ENODEV;
Rupjyoti Sarmah62936002010-07-06 16:36:03 +05301244 }
1245 hsdev->reg_base = base;
1246 dev_dbg(&ofdev->dev, "ioremap done for SATA register address\n");
1247
1248 /* Synopsys DWC SATA specific Registers */
Mans Rullgard175553e2016-04-26 12:03:16 +03001249 hsdev->sata_dwc_regs = base + SATA_DWC_REG_OFFSET;
Rupjyoti Sarmah62936002010-07-06 16:36:03 +05301250
Rupjyoti Sarmah62936002010-07-06 16:36:03 +05301251 /* Setup port */
1252 host->ports[0]->ioaddr.cmd_addr = base;
1253 host->ports[0]->ioaddr.scr_addr = base + SATA_DWC_SCR_OFFSET;
Mans Rullgardae95d952016-04-26 12:03:15 +03001254 sata_dwc_setup_port(&host->ports[0]->ioaddr, base);
Rupjyoti Sarmah62936002010-07-06 16:36:03 +05301255
1256 /* Read the ID and Version Registers */
1257 idr = in_le32(&hsdev->sata_dwc_regs->idr);
1258 versionr = in_le32(&hsdev->sata_dwc_regs->versionr);
1259 dev_notice(&ofdev->dev, "id %d, controller version %c.%c%c\n",
1260 idr, ver[0], ver[1], ver[2]);
1261
Rupjyoti Sarmah62936002010-07-06 16:36:03 +05301262 /* Save dev for later use in dev_xxx() routines */
Andy Shevchenkodb7a6572015-03-03 22:41:22 +02001263 hsdev->dev = &ofdev->dev;
Rupjyoti Sarmah62936002010-07-06 16:36:03 +05301264
Rupjyoti Sarmah62936002010-07-06 16:36:03 +05301265 /* Enable SATA Interrupts */
1266 sata_dwc_enable_interrupts(hsdev);
1267
1268 /* Get SATA interrupt number */
Andy Shevchenko90379082015-01-08 12:50:12 +02001269 irq = irq_of_parse_and_map(np, 0);
Rupjyoti Sarmah62936002010-07-06 16:36:03 +05301270 if (irq == NO_IRQ) {
1271 dev_err(&ofdev->dev, "no SATA DMA irq\n");
1272 err = -ENODEV;
1273 goto error_out;
1274 }
1275
Mans Rullgard50b43372016-04-26 12:03:10 +03001276#ifdef CONFIG_SATA_DWC_OLD_DMA
1277 if (!of_find_property(np, "dmas", NULL)) {
1278 err = sata_dwc_dma_init_old(ofdev, hsdev);
1279 if (err)
1280 goto error_out;
1281 }
1282#endif
1283
Mans Rullgard0f48deb2016-04-26 12:03:11 +03001284 hsdev->phy = devm_phy_optional_get(hsdev->dev, "sata-phy");
1285 if (IS_ERR(hsdev->phy)) {
1286 err = PTR_ERR(hsdev->phy);
1287 hsdev->phy = NULL;
1288 goto error_out;
1289 }
1290
1291 err = phy_init(hsdev->phy);
1292 if (err)
1293 goto error_out;
1294
Rupjyoti Sarmah62936002010-07-06 16:36:03 +05301295 /*
1296 * Now, register with libATA core, this will also initiate the
1297 * device discovery process, invoking our port_start() handler &
1298 * error_handler() to execute a dummy Softreset EH session
1299 */
Andy Shevchenko4aaa7182015-01-07 15:24:19 +02001300 err = ata_host_activate(host, irq, sata_dwc_isr, 0, &sata_dwc_sht);
1301 if (err)
Rupjyoti Sarmah62936002010-07-06 16:36:03 +05301302 dev_err(&ofdev->dev, "failed to activate host");
1303
1304 dev_set_drvdata(&ofdev->dev, host);
1305 return 0;
1306
1307error_out:
Mans Rullgard0f48deb2016-04-26 12:03:11 +03001308 phy_exit(hsdev->phy);
Vasiliy Kulikov04e506b2011-07-03 15:26:47 +04001309 iounmap(base);
Rupjyoti Sarmah62936002010-07-06 16:36:03 +05301310 return err;
1311}
1312
Stephen Rothwell60652d02010-08-16 12:20:59 +10001313static int sata_dwc_remove(struct platform_device *ofdev)
Rupjyoti Sarmah62936002010-07-06 16:36:03 +05301314{
1315 struct device *dev = &ofdev->dev;
1316 struct ata_host *host = dev_get_drvdata(dev);
1317 struct sata_dwc_device *hsdev = host->private_data;
1318
1319 ata_host_detach(host);
Rupjyoti Sarmah62936002010-07-06 16:36:03 +05301320
Mans Rullgard0f48deb2016-04-26 12:03:11 +03001321 phy_exit(hsdev->phy);
1322
Mans Rullgard50b43372016-04-26 12:03:10 +03001323#ifdef CONFIG_SATA_DWC_OLD_DMA
Rupjyoti Sarmah62936002010-07-06 16:36:03 +05301324 /* Free SATA DMA resources */
Mans Rullgard50b43372016-04-26 12:03:10 +03001325 sata_dwc_dma_exit_old(hsdev);
1326#endif
Rupjyoti Sarmah62936002010-07-06 16:36:03 +05301327
1328 iounmap(hsdev->reg_base);
Rupjyoti Sarmah62936002010-07-06 16:36:03 +05301329 dev_dbg(&ofdev->dev, "done\n");
1330 return 0;
1331}
1332
1333static const struct of_device_id sata_dwc_match[] = {
1334 { .compatible = "amcc,sata-460ex", },
1335 {}
1336};
1337MODULE_DEVICE_TABLE(of, sata_dwc_match);
1338
Grant Likely1c48a5c2011-02-17 02:43:24 -07001339static struct platform_driver sata_dwc_driver = {
Rupjyoti Sarmah62936002010-07-06 16:36:03 +05301340 .driver = {
1341 .name = DRV_NAME,
Rupjyoti Sarmah62936002010-07-06 16:36:03 +05301342 .of_match_table = sata_dwc_match,
1343 },
1344 .probe = sata_dwc_probe,
1345 .remove = sata_dwc_remove,
1346};
1347
Axel Lin99c8ea32011-11-27 14:44:26 +08001348module_platform_driver(sata_dwc_driver);
Rupjyoti Sarmah62936002010-07-06 16:36:03 +05301349
1350MODULE_LICENSE("GPL");
1351MODULE_AUTHOR("Mark Miesfeld <mmiesfeld@amcc.com>");
Andy Shevchenkod5785142015-03-03 21:21:58 +02001352MODULE_DESCRIPTION("DesignWare Cores SATA controller low level driver");
Rupjyoti Sarmah62936002010-07-06 16:36:03 +05301353MODULE_VERSION(DRV_VERSION);