1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
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>
32#include <linux/init.h>
33#include <linux/device.h>
34#include <linux/of_platform.h>
35#include <linux/platform_device.h>
36#include <linux/libata.h>
37#include <linux/slab.h>
38#include "libata.h"
39
40#include <scsi/scsi_host.h>
41#include <scsi/scsi_cmnd.h>
42
43
44#undef DRV_NAME
45#undef DRV_VERSION
46
47#define DRV_NAME "sata-dwc"
48#define DRV_VERSION "1.3"
49
50
51#define DMA_NUM_CHANS 1
52#define DMA_NUM_CHAN_REGS 8
53
54
55#define AHB_DMA_BRST_DFLT 64
56
57struct dmareg {
58 u32 low;
59 u32 high;
60};
61
62
63struct dma_chan_regs {
64 struct dmareg sar;
65 struct dmareg dar;
66 struct dmareg llp;
67 struct dmareg ctl;
68 struct dmareg sstat;
69 struct dmareg dstat;
70 struct dmareg sstatar;
71 struct dmareg dstatar;
72 struct dmareg cfg;
73 struct dmareg sgr;
74 struct dmareg dsr;
75};
76
77
78struct dma_interrupt_regs {
79 struct dmareg tfr;
80 struct dmareg block;
81 struct dmareg srctran;
82 struct dmareg dsttran;
83 struct dmareg error;
84};
85
86struct ahb_dma_regs {
87 struct dma_chan_regs chan_regs[DMA_NUM_CHAN_REGS];
88 struct dma_interrupt_regs interrupt_raw;
89 struct dma_interrupt_regs interrupt_status;
90 struct dma_interrupt_regs interrupt_mask;
91 struct dma_interrupt_regs interrupt_clear;
92 struct dmareg statusInt;
93 struct dmareg rq_srcreg;
94 struct dmareg rq_dstreg;
95 struct dmareg rq_sgl_srcreg;
96 struct dmareg rq_sgl_dstreg;
97 struct dmareg rq_lst_srcreg;
98 struct dmareg rq_lst_dstreg;
99 struct dmareg dma_cfg;
100 struct dmareg dma_chan_en;
101 struct dmareg dma_id;
102 struct dmareg dma_test;
103 struct dmareg res1;
104 struct dmareg res2;
105
106
107
108
109
110 struct dmareg dma_params[6];
111};
112
113
114struct lli {
115 u32 sar;
116 u32 dar;
117 u32 llp;
118 struct dmareg ctl;
119 struct dmareg dstat;
120};
121
122enum {
123 SATA_DWC_DMAC_LLI_SZ = (sizeof(struct lli)),
124 SATA_DWC_DMAC_LLI_NUM = 256,
125 SATA_DWC_DMAC_LLI_TBL_SZ = (SATA_DWC_DMAC_LLI_SZ * \
126 SATA_DWC_DMAC_LLI_NUM),
127 SATA_DWC_DMAC_TWIDTH_BYTES = 4,
128 SATA_DWC_DMAC_CTRL_TSIZE_MAX = (0x00000800 * \
129 SATA_DWC_DMAC_TWIDTH_BYTES),
130};
131
132
133enum {
134 DMA_EN = 0x00000001,
135 DMA_CTL_LLP_SRCEN = 0x10000000,
136 DMA_CTL_LLP_DSTEN = 0x08000000,
137};
138
139#define DMA_CTL_BLK_TS(size) ((size) & 0x000000FFF)
140#define DMA_CHANNEL(ch) (0x00000001 << (ch))
141
142#define DMA_ENABLE_CHAN(ch) ((0x00000001 << (ch)) | \
143 ((0x000000001 << (ch)) << 8))
144
145#define DMA_DISABLE_CHAN(ch) (0x00000000 | ((0x000000001 << (ch)) << 8))
146
147#define DMA_CTL_TTFC(type) (((type) & 0x7) << 20)
148#define DMA_CTL_SMS(num) (((num) & 0x3) << 25)
149#define DMA_CTL_DMS(num) (((num) & 0x3) << 23)
150
151#define DMA_CTL_SRC_MSIZE(size) (((size) & 0x7) << 14)
152
153#define DMA_CTL_DST_MSIZE(size) (((size) & 0x7) << 11)
154
155#define DMA_CTL_SRC_TRWID(size) (((size) & 0x7) << 4)
156
157#define DMA_CTL_DST_TRWID(size) (((size) & 0x7) << 1)
158
159
160#define DMA_CFG_HW_HS_DEST(int_num) (((int_num) & 0xF) << 11)
161#define DMA_CFG_HW_HS_SRC(int_num) (((int_num) & 0xF) << 7)
162#define DMA_CFG_HW_CH_PRIOR(int_num) (((int_num) & 0xF) << 5)
163#define DMA_LLP_LMS(addr, master) (((addr) & 0xfffffffc) | (master))
164
165
166
167
168
169
170enum {
171 DMA_CTL_LLP_DISABLE_LE32 = 0xffffffe7,
172 DMA_CTL_TTFC_P2M_DMAC = 0x00000002,
173 DMA_CTL_TTFC_M2P_PER = 0x00000003,
174 DMA_CTL_SINC_INC = 0x00000000,
175 DMA_CTL_SINC_DEC = 0x00000200,
176 DMA_CTL_SINC_NOCHANGE = 0x00000400,
177 DMA_CTL_DINC_INC = 0x00000000,
178 DMA_CTL_DINC_DEC = 0x00000080,
179 DMA_CTL_DINC_NOCHANGE = 0x00000100,
180 DMA_CTL_INT_EN = 0x00000001,
181
182
183 DMA_CFG_FCMOD_REQ = 0x00000001,
184 DMA_CFG_PROTCTL = (0x00000003 << 2),
185
186
187 DMA_CFG_RELD_DST = 0x80000000,
188 DMA_CFG_RELD_SRC = 0x40000000,
189 DMA_CFG_HS_SELSRC = 0x00000800,
190 DMA_CFG_HS_SELDST = 0x00000400,
191 DMA_CFG_FIFOEMPTY = (0x00000001 << 9),
192
193
194 DMA_LLP_AHBMASTER1 = 0,
195 DMA_LLP_AHBMASTER2 = 1,
196
197 SATA_DWC_MAX_PORTS = 1,
198
199 SATA_DWC_SCR_OFFSET = 0x24,
200 SATA_DWC_REG_OFFSET = 0x64,
201};
202
203
204struct sata_dwc_regs {
205 u32 fptagr;
206 u32 fpbor;
207 u32 fptcr;
208 u32 dmacr;
209 u32 dbtsr;
210 u32 intpr;
211 u32 intmr;
212 u32 errmr;
213 u32 llcr;
214 u32 phycr;
215 u32 physr;
216 u32 rxbistpd;
217 u32 rxbistpd1;
218 u32 rxbistpd2;
219 u32 txbistpd;
220 u32 txbistpd1;
221 u32 txbistpd2;
222 u32 bistcr;
223 u32 bistfctr;
224 u32 bistsr;
225 u32 bistdecr;
226 u32 res[15];
227 u32 testr;
228 u32 versionr;
229 u32 idr;
230 u32 unimpl[192];
231 u32 dmadr[256];
232};
233
234enum {
235 SCR_SCONTROL_DET_ENABLE = 0x00000001,
236 SCR_SSTATUS_DET_PRESENT = 0x00000001,
237 SCR_SERROR_DIAG_X = 0x04000000,
238
239 SATA_DWC_TXFIFO_DEPTH = 0x01FF,
240 SATA_DWC_RXFIFO_DEPTH = 0x01FF,
241 SATA_DWC_DMACR_TMOD_TXCHEN = 0x00000004,
242 SATA_DWC_DMACR_TXCHEN = (0x00000001 | SATA_DWC_DMACR_TMOD_TXCHEN),
243 SATA_DWC_DMACR_RXCHEN = (0x00000002 | SATA_DWC_DMACR_TMOD_TXCHEN),
244 SATA_DWC_DMACR_TXRXCH_CLEAR = SATA_DWC_DMACR_TMOD_TXCHEN,
245 SATA_DWC_INTPR_DMAT = 0x00000001,
246 SATA_DWC_INTPR_NEWFP = 0x00000002,
247 SATA_DWC_INTPR_PMABRT = 0x00000004,
248 SATA_DWC_INTPR_ERR = 0x00000008,
249 SATA_DWC_INTPR_NEWBIST = 0x00000010,
250 SATA_DWC_INTPR_IPF = 0x10000000,
251 SATA_DWC_INTMR_DMATM = 0x00000001,
252 SATA_DWC_INTMR_NEWFPM = 0x00000002,
253 SATA_DWC_INTMR_PMABRTM = 0x00000004,
254 SATA_DWC_INTMR_ERRM = 0x00000008,
255 SATA_DWC_INTMR_NEWBISTM = 0x00000010,
256 SATA_DWC_LLCR_SCRAMEN = 0x00000001,
257 SATA_DWC_LLCR_DESCRAMEN = 0x00000002,
258 SATA_DWC_LLCR_RPDEN = 0x00000004,
259
260 SATA_DWC_SERROR_ERR_BITS = 0x0FFF0F03
261};
262
263#define SATA_DWC_SCR0_SPD_GET(v) (((v) >> 4) & 0x0000000F)
264#define SATA_DWC_DMACR_TX_CLEAR(v) (((v) & ~SATA_DWC_DMACR_TXCHEN) |\
265 SATA_DWC_DMACR_TMOD_TXCHEN)
266#define SATA_DWC_DMACR_RX_CLEAR(v) (((v) & ~SATA_DWC_DMACR_RXCHEN) |\
267 SATA_DWC_DMACR_TMOD_TXCHEN)
268#define SATA_DWC_DBTSR_MWR(size) (((size)/4) & SATA_DWC_TXFIFO_DEPTH)
269#define SATA_DWC_DBTSR_MRD(size) ((((size)/4) & SATA_DWC_RXFIFO_DEPTH)\
270 << 16)
271struct sata_dwc_device {
272 struct device *dev;
273 struct ata_probe_ent *pe;
274 struct ata_host *host;
275 u8 *reg_base;
276 struct sata_dwc_regs *sata_dwc_regs;
277 int irq_dma;
278};
279
280#define SATA_DWC_QCMD_MAX 32
281
282struct sata_dwc_device_port {
283 struct sata_dwc_device *hsdev;
284 int cmd_issued[SATA_DWC_QCMD_MAX];
285 struct lli *llit[SATA_DWC_QCMD_MAX];
286 dma_addr_t llit_dma[SATA_DWC_QCMD_MAX];
287 u32 dma_chan[SATA_DWC_QCMD_MAX];
288 int dma_pending[SATA_DWC_QCMD_MAX];
289};
290
291
292
293
294#define HSDEV_FROM_HOST(host) ((struct sata_dwc_device *)\
295 (host)->private_data)
296#define HSDEV_FROM_AP(ap) ((struct sata_dwc_device *)\
297 (ap)->host->private_data)
298#define HSDEVP_FROM_AP(ap) ((struct sata_dwc_device_port *)\
299 (ap)->private_data)
300#define HSDEV_FROM_QC(qc) ((struct sata_dwc_device *)\
301 (qc)->ap->host->private_data)
302#define HSDEV_FROM_HSDEVP(p) ((struct sata_dwc_device *)\
303 (hsdevp)->hsdev)
304
305enum {
306 SATA_DWC_CMD_ISSUED_NOT = 0,
307 SATA_DWC_CMD_ISSUED_PEND = 1,
308 SATA_DWC_CMD_ISSUED_EXEC = 2,
309 SATA_DWC_CMD_ISSUED_NODATA = 3,
310
311 SATA_DWC_DMA_PENDING_NONE = 0,
312 SATA_DWC_DMA_PENDING_TX = 1,
313 SATA_DWC_DMA_PENDING_RX = 2,
314};
315
316struct sata_dwc_host_priv {
317 void __iomem *scr_addr_sstatus;
318 u32 sata_dwc_sactive_issued ;
319 u32 sata_dwc_sactive_queued ;
320 u32 dma_interrupt_count;
321 struct ahb_dma_regs *sata_dma_regs;
322 struct device *dwc_dev;
323 int dma_channel;
324};
325struct sata_dwc_host_priv host_pvt;
326
327
328
329static void sata_dwc_bmdma_start_by_tag(struct ata_queued_cmd *qc, u8 tag);
330static int sata_dwc_qc_complete(struct ata_port *ap, struct ata_queued_cmd *qc,
331 u32 check_status);
332static void sata_dwc_dma_xfer_complete(struct ata_port *ap, u32 check_status);
333static void sata_dwc_port_stop(struct ata_port *ap);
334static void sata_dwc_clear_dmacr(struct sata_dwc_device_port *hsdevp, u8 tag);
335static int dma_dwc_init(struct sata_dwc_device *hsdev, int irq);
336static void dma_dwc_exit(struct sata_dwc_device *hsdev);
337static int dma_dwc_xfer_setup(struct scatterlist *sg, int num_elems,
338 struct lli *lli, dma_addr_t dma_lli,
339 void __iomem *addr, int dir);
340static void dma_dwc_xfer_start(int dma_ch);
341
342static const char *get_prot_descript(u8 protocol)
343{
344 switch ((enum ata_tf_protocols)protocol) {
345 case ATA_PROT_NODATA:
346 return "ATA no data";
347 case ATA_PROT_PIO:
348 return "ATA PIO";
349 case ATA_PROT_DMA:
350 return "ATA DMA";
351 case ATA_PROT_NCQ:
352 return "ATA NCQ";
353 case ATAPI_PROT_NODATA:
354 return "ATAPI no data";
355 case ATAPI_PROT_PIO:
356 return "ATAPI PIO";
357 case ATAPI_PROT_DMA:
358 return "ATAPI DMA";
359 default:
360 return "unknown";
361 }
362}
363
364static const char *get_dma_dir_descript(int dma_dir)
365{
366 switch ((enum dma_data_direction)dma_dir) {
367 case DMA_BIDIRECTIONAL:
368 return "bidirectional";
369 case DMA_TO_DEVICE:
370 return "to device";
371 case DMA_FROM_DEVICE:
372 return "from device";
373 default:
374 return "none";
375 }
376}
377
378static void sata_dwc_tf_dump(struct ata_taskfile *tf)
379{
380 dev_vdbg(host_pvt.dwc_dev, "taskfile cmd: 0x%02x protocol: %s flags:"
381 "0x%lx device: %x\n", tf->command,
382 get_prot_descript(tf->protocol), tf->flags, tf->device);
383 dev_vdbg(host_pvt.dwc_dev, "feature: 0x%02x nsect: 0x%x lbal: 0x%x "
384 "lbam: 0x%x lbah: 0x%x\n", tf->feature, tf->nsect, tf->lbal,
385 tf->lbam, tf->lbah);
386 dev_vdbg(host_pvt.dwc_dev, "hob_feature: 0x%02x hob_nsect: 0x%x "
387 "hob_lbal: 0x%x hob_lbam: 0x%x hob_lbah: 0x%x\n",
388 tf->hob_feature, tf->hob_nsect, tf->hob_lbal, tf->hob_lbam,
389 tf->hob_lbah);
390}
391
392
393
394
395
396
397
398static int get_burst_length_encode(int datalength)
399{
400 int items = datalength >> 2;
401
402 if (items >= 64)
403 return 5;
404
405 if (items >= 32)
406 return 4;
407
408 if (items >= 16)
409 return 3;
410
411 if (items >= 8)
412 return 2;
413
414 if (items >= 4)
415 return 1;
416
417 return 0;
418}
419
420static void clear_chan_interrupts(int c)
421{
422 out_le32(&(host_pvt.sata_dma_regs->interrupt_clear.tfr.low),
423 DMA_CHANNEL(c));
424 out_le32(&(host_pvt.sata_dma_regs->interrupt_clear.block.low),
425 DMA_CHANNEL(c));
426 out_le32(&(host_pvt.sata_dma_regs->interrupt_clear.srctran.low),
427 DMA_CHANNEL(c));
428 out_le32(&(host_pvt.sata_dma_regs->interrupt_clear.dsttran.low),
429 DMA_CHANNEL(c));
430 out_le32(&(host_pvt.sata_dma_regs->interrupt_clear.error.low),
431 DMA_CHANNEL(c));
432}
433
434
435
436
437
438
439
440
441static int dma_request_channel(void)
442{
443
444 if (!(in_le32(&(host_pvt.sata_dma_regs->dma_chan_en.low)) &
445 DMA_CHANNEL(host_pvt.dma_channel)))
446 return host_pvt.dma_channel;
447 dev_err(host_pvt.dwc_dev, "%s Channel %d is currently in use\n",
448 __func__, host_pvt.dma_channel);
449 return -1;
450}
451
452
453
454
455
456
457
458static irqreturn_t dma_dwc_interrupt(int irq, void *hsdev_instance)
459{
460 int chan;
461 u32 tfr_reg, err_reg;
462 unsigned long flags;
463 struct sata_dwc_device *hsdev =
464 (struct sata_dwc_device *)hsdev_instance;
465 struct ata_host *host = (struct ata_host *)hsdev->host;
466 struct ata_port *ap;
467 struct sata_dwc_device_port *hsdevp;
468 u8 tag = 0;
469 unsigned int port = 0;
470
471 spin_lock_irqsave(&host->lock, flags);
472 ap = host->ports[port];
473 hsdevp = HSDEVP_FROM_AP(ap);
474 tag = ap->link.active_tag;
475
476 tfr_reg = in_le32(&(host_pvt.sata_dma_regs->interrupt_status.tfr\
477 .low));
478 err_reg = in_le32(&(host_pvt.sata_dma_regs->interrupt_status.error\
479 .low));
480
481 dev_dbg(ap->dev, "eot=0x%08x err=0x%08x pending=%d active port=%d\n",
482 tfr_reg, err_reg, hsdevp->dma_pending[tag], port);
483
484 chan = host_pvt.dma_channel;
485 if (chan >= 0) {
486
487 if (tfr_reg & DMA_CHANNEL(chan)) {
488
489
490
491
492
493 host_pvt.dma_interrupt_count++;
494 sata_dwc_clear_dmacr(hsdevp, tag);
495
496 if (hsdevp->dma_pending[tag] ==
497 SATA_DWC_DMA_PENDING_NONE) {
498 dev_err(ap->dev, "DMA not pending eot=0x%08x "
499 "err=0x%08x tag=0x%02x pending=%d\n",
500 tfr_reg, err_reg, tag,
501 hsdevp->dma_pending[tag]);
502 }
503
504 if ((host_pvt.dma_interrupt_count % 2) == 0)
505 sata_dwc_dma_xfer_complete(ap, 1);
506
507
508 out_le32(&(host_pvt.sata_dma_regs->interrupt_clear\
509 .tfr.low),
510 DMA_CHANNEL(chan));
511 }
512
513
514 if (err_reg & DMA_CHANNEL(chan)) {
515
516 dev_err(ap->dev, "error interrupt err_reg=0x%08x\n",
517 err_reg);
518
519
520 out_le32(&(host_pvt.sata_dma_regs->interrupt_clear\
521 .error.low),
522 DMA_CHANNEL(chan));
523 }
524 }
525 spin_unlock_irqrestore(&host->lock, flags);
526 return IRQ_HANDLED;
527}
528
529
530
531
532
533
534
535static int dma_request_interrupts(struct sata_dwc_device *hsdev, int irq)
536{
537 int retval = 0;
538 int chan = host_pvt.dma_channel;
539
540 if (chan >= 0) {
541
542 out_le32(&(host_pvt.sata_dma_regs)->interrupt_mask.error.low,
543 DMA_ENABLE_CHAN(chan));
544
545
546 out_le32(&(host_pvt.sata_dma_regs)->interrupt_mask.tfr.low,
547 DMA_ENABLE_CHAN(chan));
548 }
549
550 retval = request_irq(irq, dma_dwc_interrupt, 0, "SATA DMA", hsdev);
551 if (retval) {
552 dev_err(host_pvt.dwc_dev, "%s: could not get IRQ %d\n",
553 __func__, irq);
554 return -ENODEV;
555 }
556
557
558 hsdev->irq_dma = irq;
559 return 0;
560}
561
562
563
564
565
566
567
568
569
570
571static int map_sg_to_lli(struct scatterlist *sg, int num_elems,
572 struct lli *lli, dma_addr_t dma_lli,
573 void __iomem *dmadr_addr, int dir)
574{
575 int i, idx = 0;
576 int fis_len = 0;
577 dma_addr_t next_llp;
578 int bl;
579 int sms_val, dms_val;
580
581 sms_val = 0;
582 dms_val = 1 + host_pvt.dma_channel;
583 dev_dbg(host_pvt.dwc_dev, "%s: sg=%p nelem=%d lli=%p dma_lli=0x%08x"
584 " dmadr=0x%08x\n", __func__, sg, num_elems, lli, (u32)dma_lli,
585 (u32)dmadr_addr);
586
587 bl = get_burst_length_encode(AHB_DMA_BRST_DFLT);
588
589 for (i = 0; i < num_elems; i++, sg++) {
590 u32 addr, offset;
591 u32 sg_len, len;
592
593 addr = (u32) sg_dma_address(sg);
594 sg_len = sg_dma_len(sg);
595
596 dev_dbg(host_pvt.dwc_dev, "%s: elem=%d sg_addr=0x%x sg_len"
597 "=%d\n", __func__, i, addr, sg_len);
598
599 while (sg_len) {
600 if (idx >= SATA_DWC_DMAC_LLI_NUM) {
601
602 dev_err(host_pvt.dwc_dev, "LLI table overrun "
603 "(idx=%d)\n", idx);
604 break;
605 }
606 len = (sg_len > SATA_DWC_DMAC_CTRL_TSIZE_MAX) ?
607 SATA_DWC_DMAC_CTRL_TSIZE_MAX : sg_len;
608
609 offset = addr & 0xffff;
610 if ((offset + sg_len) > 0x10000)
611 len = 0x10000 - offset;
612
613
614
615
616
617
618
619
620 if (fis_len + len > 8192) {
621 dev_dbg(host_pvt.dwc_dev, "SPLITTING: fis_len="
622 "%d(0x%x) len=%d(0x%x)\n", fis_len,
623 fis_len, len, len);
624 len = 8192 - fis_len;
625 fis_len = 0;
626 } else {
627 fis_len += len;
628 }
629 if (fis_len == 8192)
630 fis_len = 0;
631
632
633
634
635
636 if (dir == DMA_FROM_DEVICE) {
637 lli[idx].dar = cpu_to_le32(addr);
638 lli[idx].sar = cpu_to_le32((u32)dmadr_addr);
639
640 lli[idx].ctl.low = cpu_to_le32(
641 DMA_CTL_TTFC(DMA_CTL_TTFC_P2M_DMAC) |
642 DMA_CTL_SMS(sms_val) |
643 DMA_CTL_DMS(dms_val) |
644 DMA_CTL_SRC_MSIZE(bl) |
645 DMA_CTL_DST_MSIZE(bl) |
646 DMA_CTL_SINC_NOCHANGE |
647 DMA_CTL_SRC_TRWID(2) |
648 DMA_CTL_DST_TRWID(2) |
649 DMA_CTL_INT_EN |
650 DMA_CTL_LLP_SRCEN |
651 DMA_CTL_LLP_DSTEN);
652 } else {
653 lli[idx].sar = cpu_to_le32(addr);
654 lli[idx].dar = cpu_to_le32((u32)dmadr_addr);
655
656 lli[idx].ctl.low = cpu_to_le32(
657 DMA_CTL_TTFC(DMA_CTL_TTFC_M2P_PER) |
658 DMA_CTL_SMS(dms_val) |
659 DMA_CTL_DMS(sms_val) |
660 DMA_CTL_SRC_MSIZE(bl) |
661 DMA_CTL_DST_MSIZE(bl) |
662 DMA_CTL_DINC_NOCHANGE |
663 DMA_CTL_SRC_TRWID(2) |
664 DMA_CTL_DST_TRWID(2) |
665 DMA_CTL_INT_EN |
666 DMA_CTL_LLP_SRCEN |
667 DMA_CTL_LLP_DSTEN);
668 }
669
670 dev_dbg(host_pvt.dwc_dev, "%s setting ctl.high len: "
671 "0x%08x val: 0x%08x\n", __func__,
672 len, DMA_CTL_BLK_TS(len / 4));
673
674
675 lli[idx].ctl.high = cpu_to_le32(DMA_CTL_BLK_TS\
676 (len / 4));
677
678
679
680
681 next_llp = (dma_lli + ((idx + 1) * sizeof(struct \
682 lli)));
683
684
685 next_llp = DMA_LLP_LMS(next_llp, DMA_LLP_AHBMASTER2);
686
687 lli[idx].llp = cpu_to_le32(next_llp);
688 idx++;
689 sg_len -= len;
690 addr += len;
691 }
692 }
693
694
695
696
697
698
699
700 if (idx) {
701 lli[idx-1].llp = 0x00000000;
702 lli[idx-1].ctl.low &= DMA_CTL_LLP_DISABLE_LE32;
703
704
705 dma_cache_sync(NULL, lli, (sizeof(struct lli) * idx),
706 DMA_BIDIRECTIONAL);
707 }
708
709 return idx;
710}
711
712
713
714
715
716
717
718static void dma_dwc_xfer_start(int dma_ch)
719{
720
721 out_le32(&(host_pvt.sata_dma_regs->dma_chan_en.low),
722 in_le32(&(host_pvt.sata_dma_regs->dma_chan_en.low)) |
723 DMA_ENABLE_CHAN(dma_ch));
724}
725
726static int dma_dwc_xfer_setup(struct scatterlist *sg, int num_elems,
727 struct lli *lli, dma_addr_t dma_lli,
728 void __iomem *addr, int dir)
729{
730 int dma_ch;
731 int num_lli;
732
733 dma_ch = dma_request_channel();
734 if (dma_ch == -1) {
735 dev_err(host_pvt.dwc_dev, "%s: dma channel unavailable\n",
736 __func__);
737 return -EAGAIN;
738 }
739
740
741 num_lli = map_sg_to_lli(sg, num_elems, lli, dma_lli, addr, dir);
742
743 dev_dbg(host_pvt.dwc_dev, "%s sg: 0x%p, count: %d lli: %p dma_lli:"
744 " 0x%0xlx addr: %p lli count: %d\n", __func__, sg, num_elems,
745 lli, (u32)dma_lli, addr, num_lli);
746
747 clear_chan_interrupts(dma_ch);
748
749
750 out_le32(&(host_pvt.sata_dma_regs->chan_regs[dma_ch].cfg.high),
751 DMA_CFG_HW_HS_SRC(dma_ch) | DMA_CFG_HW_HS_DEST(dma_ch) |
752 DMA_CFG_PROTCTL | DMA_CFG_FCMOD_REQ);
753 out_le32(&(host_pvt.sata_dma_regs->chan_regs[dma_ch].cfg.low),
754 DMA_CFG_HW_CH_PRIOR(dma_ch));
755
756
757 out_le32(&(host_pvt.sata_dma_regs->chan_regs[dma_ch].llp.low),
758 DMA_LLP_LMS(dma_lli, DMA_LLP_AHBMASTER2));
759
760
761 out_le32(&(host_pvt.sata_dma_regs->chan_regs[dma_ch].ctl.low),
762 DMA_CTL_LLP_SRCEN | DMA_CTL_LLP_DSTEN);
763 return dma_ch;
764}
765
766
767
768
769
770
771
772static void dma_dwc_exit(struct sata_dwc_device *hsdev)
773{
774 dev_dbg(host_pvt.dwc_dev, "%s:\n", __func__);
775 if (host_pvt.sata_dma_regs) {
776 iounmap(host_pvt.sata_dma_regs);
777 host_pvt.sata_dma_regs = NULL;
778 }
779
780 if (hsdev->irq_dma) {
781 free_irq(hsdev->irq_dma, hsdev);
782 hsdev->irq_dma = 0;
783 }
784}
785
786
787
788
789
790
791
792static int dma_dwc_init(struct sata_dwc_device *hsdev, int irq)
793{
794 int err;
795
796 err = dma_request_interrupts(hsdev, irq);
797 if (err) {
798 dev_err(host_pvt.dwc_dev, "%s: dma_request_interrupts returns"
799 " %d\n", __func__, err);
800 goto error_out;
801 }
802
803
804 out_le32(&(host_pvt.sata_dma_regs->dma_cfg.low), DMA_EN);
805
806 dev_notice(host_pvt.dwc_dev, "DMA initialized\n");
807 dev_dbg(host_pvt.dwc_dev, "SATA DMA registers=0x%p\n", host_pvt.\
808 sata_dma_regs);
809
810 return 0;
811
812error_out:
813 dma_dwc_exit(hsdev);
814
815 return err;
816}
817
818static int sata_dwc_scr_read(struct ata_link *link, unsigned int scr, u32 *val)
819{
820 if (scr > SCR_NOTIFICATION) {
821 dev_err(link->ap->dev, "%s: Incorrect SCR offset 0x%02x\n",
822 __func__, scr);
823 return -EINVAL;
824 }
825
826 *val = in_le32((void *)link->ap->ioaddr.scr_addr + (scr * 4));
827 dev_dbg(link->ap->dev, "%s: id=%d reg=%d val=val=0x%08x\n",
828 __func__, link->ap->print_id, scr, *val);
829
830 return 0;
831}
832
833static int sata_dwc_scr_write(struct ata_link *link, unsigned int scr, u32 val)
834{
835 dev_dbg(link->ap->dev, "%s: id=%d reg=%d val=val=0x%08x\n",
836 __func__, link->ap->print_id, scr, val);
837 if (scr > SCR_NOTIFICATION) {
838 dev_err(link->ap->dev, "%s: Incorrect SCR offset 0x%02x\n",
839 __func__, scr);
840 return -EINVAL;
841 }
842 out_le32((void *)link->ap->ioaddr.scr_addr + (scr * 4), val);
843
844 return 0;
845}
846
847static u32 core_scr_read(unsigned int scr)
848{
849 return in_le32((void __iomem *)(host_pvt.scr_addr_sstatus) +\
850 (scr * 4));
851}
852
853static void core_scr_write(unsigned int scr, u32 val)
854{
855 out_le32((void __iomem *)(host_pvt.scr_addr_sstatus) + (scr * 4),
856 val);
857}
858
859static void clear_serror(void)
860{
861 u32 val;
862 val = core_scr_read(SCR_ERROR);
863 core_scr_write(SCR_ERROR, val);
864
865}
866
867static void clear_interrupt_bit(struct sata_dwc_device *hsdev, u32 bit)
868{
869 out_le32(&hsdev->sata_dwc_regs->intpr,
870 in_le32(&hsdev->sata_dwc_regs->intpr));
871}
872
873static u32 qcmd_tag_to_mask(u8 tag)
874{
875 return 0x00000001 << (tag & 0x1f);
876}
877
878
879static void sata_dwc_error_intr(struct ata_port *ap,
880 struct sata_dwc_device *hsdev, uint intpr)
881{
882 struct sata_dwc_device_port *hsdevp = HSDEVP_FROM_AP(ap);
883 struct ata_eh_info *ehi = &ap->link.eh_info;
884 unsigned int err_mask = 0, action = 0;
885 struct ata_queued_cmd *qc;
886 u32 serror;
887 u8 status, tag;
888 u32 err_reg;
889
890 ata_ehi_clear_desc(ehi);
891
892 serror = core_scr_read(SCR_ERROR);
893 status = ap->ops->sff_check_status(ap);
894
895 err_reg = in_le32(&(host_pvt.sata_dma_regs->interrupt_status.error.\
896 low));
897 tag = ap->link.active_tag;
898
899 dev_err(ap->dev, "%s SCR_ERROR=0x%08x intpr=0x%08x status=0x%08x "
900 "dma_intp=%d pending=%d issued=%d dma_err_status=0x%08x\n",
901 __func__, serror, intpr, status, host_pvt.dma_interrupt_count,
902 hsdevp->dma_pending[tag], hsdevp->cmd_issued[tag], err_reg);
903
904
905 clear_serror();
906 clear_interrupt_bit(hsdev, SATA_DWC_INTPR_ERR);
907
908
909
910 err_mask |= AC_ERR_HOST_BUS;
911 action |= ATA_EH_RESET;
912
913
914 ehi->serror |= serror;
915 ehi->action |= action;
916
917 qc = ata_qc_from_tag(ap, tag);
918 if (qc)
919 qc->err_mask |= err_mask;
920 else
921 ehi->err_mask |= err_mask;
922
923 ata_port_abort(ap);
924}
925
926
927
928
929
930
931
932
933static irqreturn_t sata_dwc_isr(int irq, void *dev_instance)
934{
935 struct ata_host *host = (struct ata_host *)dev_instance;
936 struct sata_dwc_device *hsdev = HSDEV_FROM_HOST(host);
937 struct ata_port *ap;
938 struct ata_queued_cmd *qc;
939 unsigned long flags;
940 u8 status, tag;
941 int handled, num_processed, port = 0;
942 uint intpr, sactive, sactive2, tag_mask;
943 struct sata_dwc_device_port *hsdevp;
944 host_pvt.sata_dwc_sactive_issued = 0;
945
946 spin_lock_irqsave(&host->lock, flags);
947
948
949 intpr = in_le32(&hsdev->sata_dwc_regs->intpr);
950
951 ap = host->ports[port];
952 hsdevp = HSDEVP_FROM_AP(ap);
953
954 dev_dbg(ap->dev, "%s intpr=0x%08x active_tag=%d\n", __func__, intpr,
955 ap->link.active_tag);
956
957
958 if (intpr & SATA_DWC_INTPR_ERR) {
959 sata_dwc_error_intr(ap, hsdev, intpr);
960 handled = 1;
961 goto DONE;
962 }
963
964
965 if (intpr & SATA_DWC_INTPR_NEWFP) {
966 clear_interrupt_bit(hsdev, SATA_DWC_INTPR_NEWFP);
967
968 tag = (u8)(in_le32(&hsdev->sata_dwc_regs->fptagr));
969 dev_dbg(ap->dev, "%s: NEWFP tag=%d\n", __func__, tag);
970 if (hsdevp->cmd_issued[tag] != SATA_DWC_CMD_ISSUED_PEND)
971 dev_warn(ap->dev, "CMD tag=%d not pending?\n", tag);
972
973 host_pvt.sata_dwc_sactive_issued |= qcmd_tag_to_mask(tag);
974
975 qc = ata_qc_from_tag(ap, tag);
976
977
978
979
980
981 qc->ap->link.active_tag = tag;
982 sata_dwc_bmdma_start_by_tag(qc, tag);
983
984 handled = 1;
985 goto DONE;
986 }
987 sactive = core_scr_read(SCR_ACTIVE);
988 tag_mask = (host_pvt.sata_dwc_sactive_issued | sactive) ^ sactive;
989
990
991 if (host_pvt.sata_dwc_sactive_issued == 0 && tag_mask == 0) {
992 if (ap->link.active_tag == ATA_TAG_POISON)
993 tag = 0;
994 else
995 tag = ap->link.active_tag;
996 qc = ata_qc_from_tag(ap, tag);
997
998
999 if (unlikely(!qc || (qc->tf.flags & ATA_TFLAG_POLLING))) {
1000 dev_err(ap->dev, "%s interrupt with no active qc "
1001 "qc=%p\n", __func__, qc);
1002 ap->ops->sff_check_status(ap);
1003 handled = 1;
1004 goto DONE;
1005 }
1006 status = ap->ops->sff_check_status(ap);
1007
1008 qc->ap->link.active_tag = tag;
1009 hsdevp->cmd_issued[tag] = SATA_DWC_CMD_ISSUED_NOT;
1010
1011 if (status & ATA_ERR) {
1012 dev_dbg(ap->dev, "interrupt ATA_ERR (0x%x)\n", status);
1013 sata_dwc_qc_complete(ap, qc, 1);
1014 handled = 1;
1015 goto DONE;
1016 }
1017
1018 dev_dbg(ap->dev, "%s non-NCQ cmd interrupt, protocol: %s\n",
1019 __func__, get_prot_descript(qc->tf.protocol));
1020DRVSTILLBUSY:
1021 if (ata_is_dma(qc->tf.protocol)) {
1022
1023
1024
1025
1026
1027
1028 host_pvt.dma_interrupt_count++;
1029 if (hsdevp->dma_pending[tag] == \
1030 SATA_DWC_DMA_PENDING_NONE) {
1031 dev_err(ap->dev, "%s: DMA not pending "
1032 "intpr=0x%08x status=0x%08x pending"
1033 "=%d\n", __func__, intpr, status,
1034 hsdevp->dma_pending[tag]);
1035 }
1036
1037 if ((host_pvt.dma_interrupt_count % 2) == 0)
1038 sata_dwc_dma_xfer_complete(ap, 1);
1039 } else if (ata_is_pio(qc->tf.protocol)) {
1040 ata_sff_hsm_move(ap, qc, status, 0);
1041 handled = 1;
1042 goto DONE;
1043 } else {
1044 if (unlikely(sata_dwc_qc_complete(ap, qc, 1)))
1045 goto DRVSTILLBUSY;
1046 }
1047
1048 handled = 1;
1049 goto DONE;
1050 }
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060 sactive = core_scr_read(SCR_ACTIVE);
1061 tag_mask = (host_pvt.sata_dwc_sactive_issued | sactive) ^ sactive;
1062
1063 if (sactive != 0 || (host_pvt.sata_dwc_sactive_issued) > 1 || \
1064 tag_mask > 1) {
1065 dev_dbg(ap->dev, "%s NCQ:sactive=0x%08x sactive_issued=0x%08x"
1066 "tag_mask=0x%08x\n", __func__, sactive,
1067 host_pvt.sata_dwc_sactive_issued, tag_mask);
1068 }
1069
1070 if ((tag_mask | (host_pvt.sata_dwc_sactive_issued)) != \
1071 (host_pvt.sata_dwc_sactive_issued)) {
1072 dev_warn(ap->dev, "Bad tag mask? sactive=0x%08x "
1073 "(host_pvt.sata_dwc_sactive_issued)=0x%08x tag_mask"
1074 "=0x%08x\n", sactive, host_pvt.sata_dwc_sactive_issued,
1075 tag_mask);
1076 }
1077
1078
1079 status = ap->ops->sff_check_status(ap);
1080 dev_dbg(ap->dev, "%s ATA status register=0x%x\n", __func__, status);
1081
1082 tag = 0;
1083 num_processed = 0;
1084 while (tag_mask) {
1085 num_processed++;
1086 while (!(tag_mask & 0x00000001)) {
1087 tag++;
1088 tag_mask <<= 1;
1089 }
1090
1091 tag_mask &= (~0x00000001);
1092 qc = ata_qc_from_tag(ap, tag);
1093
1094
1095 qc->ap->link.active_tag = tag;
1096 hsdevp->cmd_issued[tag] = SATA_DWC_CMD_ISSUED_NOT;
1097
1098
1099 if (status & ATA_ERR) {
1100 dev_dbg(ap->dev, "%s ATA_ERR (0x%x)\n", __func__,
1101 status);
1102 sata_dwc_qc_complete(ap, qc, 1);
1103 handled = 1;
1104 goto DONE;
1105 }
1106
1107
1108 dev_dbg(ap->dev, "%s NCQ command, protocol: %s\n", __func__,
1109 get_prot_descript(qc->tf.protocol));
1110 if (ata_is_dma(qc->tf.protocol)) {
1111 host_pvt.dma_interrupt_count++;
1112 if (hsdevp->dma_pending[tag] == \
1113 SATA_DWC_DMA_PENDING_NONE)
1114 dev_warn(ap->dev, "%s: DMA not pending?\n",
1115 __func__);
1116 if ((host_pvt.dma_interrupt_count % 2) == 0)
1117 sata_dwc_dma_xfer_complete(ap, 1);
1118 } else {
1119 if (unlikely(sata_dwc_qc_complete(ap, qc, 1)))
1120 goto STILLBUSY;
1121 }
1122 continue;
1123
1124STILLBUSY:
1125 ap->stats.idle_irq++;
1126 dev_warn(ap->dev, "STILL BUSY IRQ ata%d: irq trap\n",
1127 ap->print_id);
1128 }
1129
1130
1131
1132
1133
1134
1135
1136
1137 sactive2 = core_scr_read(SCR_ACTIVE);
1138 if (sactive2 != sactive) {
1139 dev_dbg(ap->dev, "More completed - sactive=0x%x sactive2"
1140 "=0x%x\n", sactive, sactive2);
1141 }
1142 handled = 1;
1143
1144DONE:
1145 spin_unlock_irqrestore(&host->lock, flags);
1146 return IRQ_RETVAL(handled);
1147}
1148
1149static void sata_dwc_clear_dmacr(struct sata_dwc_device_port *hsdevp, u8 tag)
1150{
1151 struct sata_dwc_device *hsdev = HSDEV_FROM_HSDEVP(hsdevp);
1152
1153 if (hsdevp->dma_pending[tag] == SATA_DWC_DMA_PENDING_RX) {
1154 out_le32(&(hsdev->sata_dwc_regs->dmacr),
1155 SATA_DWC_DMACR_RX_CLEAR(
1156 in_le32(&(hsdev->sata_dwc_regs->dmacr))));
1157 } else if (hsdevp->dma_pending[tag] == SATA_DWC_DMA_PENDING_TX) {
1158 out_le32(&(hsdev->sata_dwc_regs->dmacr),
1159 SATA_DWC_DMACR_TX_CLEAR(
1160 in_le32(&(hsdev->sata_dwc_regs->dmacr))));
1161 } else {
1162
1163
1164
1165
1166 dev_err(host_pvt.dwc_dev, "%s DMA protocol RX and"
1167 "TX DMA not pending tag=0x%02x pending=%d"
1168 " dmacr: 0x%08x\n", __func__, tag,
1169 hsdevp->dma_pending[tag],
1170 in_le32(&(hsdev->sata_dwc_regs->dmacr)));
1171 out_le32(&(hsdev->sata_dwc_regs->dmacr),
1172 SATA_DWC_DMACR_TXRXCH_CLEAR);
1173 }
1174}
1175
1176static void sata_dwc_dma_xfer_complete(struct ata_port *ap, u32 check_status)
1177{
1178 struct ata_queued_cmd *qc;
1179 struct sata_dwc_device_port *hsdevp = HSDEVP_FROM_AP(ap);
1180 struct sata_dwc_device *hsdev = HSDEV_FROM_AP(ap);
1181 u8 tag = 0;
1182
1183 tag = ap->link.active_tag;
1184 qc = ata_qc_from_tag(ap, tag);
1185 if (!qc) {
1186 dev_err(ap->dev, "failed to get qc");
1187 return;
1188 }
1189
1190#ifdef DEBUG_NCQ
1191 if (tag > 0) {
1192 dev_info(ap->dev, "%s tag=%u cmd=0x%02x dma dir=%s proto=%s "
1193 "dmacr=0x%08x\n", __func__, qc->tag, qc->tf.command,
1194 get_dma_dir_descript(qc->dma_dir),
1195 get_prot_descript(qc->tf.protocol),
1196 in_le32(&(hsdev->sata_dwc_regs->dmacr)));
1197 }
1198#endif
1199
1200 if (ata_is_dma(qc->tf.protocol)) {
1201 if (hsdevp->dma_pending[tag] == SATA_DWC_DMA_PENDING_NONE) {
1202 dev_err(ap->dev, "%s DMA protocol RX and TX DMA not "
1203 "pending dmacr: 0x%08x\n", __func__,
1204 in_le32(&(hsdev->sata_dwc_regs->dmacr)));
1205 }
1206
1207 hsdevp->dma_pending[tag] = SATA_DWC_DMA_PENDING_NONE;
1208 sata_dwc_qc_complete(ap, qc, check_status);
1209 ap->link.active_tag = ATA_TAG_POISON;
1210 } else {
1211 sata_dwc_qc_complete(ap, qc, check_status);
1212 }
1213}
1214
1215static int sata_dwc_qc_complete(struct ata_port *ap, struct ata_queued_cmd *qc,
1216 u32 check_status)
1217{
1218 u8 status = 0;
1219 u32 mask = 0x0;
1220 u8 tag = qc->tag;
1221 struct sata_dwc_device_port *hsdevp = HSDEVP_FROM_AP(ap);
1222 host_pvt.sata_dwc_sactive_queued = 0;
1223 dev_dbg(ap->dev, "%s checkstatus? %x\n", __func__, check_status);
1224
1225 if (hsdevp->dma_pending[tag] == SATA_DWC_DMA_PENDING_TX)
1226 dev_err(ap->dev, "TX DMA PENDING\n");
1227 else if (hsdevp->dma_pending[tag] == SATA_DWC_DMA_PENDING_RX)
1228 dev_err(ap->dev, "RX DMA PENDING\n");
1229 dev_dbg(ap->dev, "QC complete cmd=0x%02x status=0x%02x ata%u:"
1230 " protocol=%d\n", qc->tf.command, status, ap->print_id,
1231 qc->tf.protocol);
1232
1233
1234 mask = (~(qcmd_tag_to_mask(tag)));
1235 host_pvt.sata_dwc_sactive_queued = (host_pvt.sata_dwc_sactive_queued) \
1236 & mask;
1237 host_pvt.sata_dwc_sactive_issued = (host_pvt.sata_dwc_sactive_issued) \
1238 & mask;
1239 ata_qc_complete(qc);
1240 return 0;
1241}
1242
1243static void sata_dwc_enable_interrupts(struct sata_dwc_device *hsdev)
1244{
1245
1246 out_le32(&hsdev->sata_dwc_regs->intmr,
1247 SATA_DWC_INTMR_ERRM |
1248 SATA_DWC_INTMR_NEWFPM |
1249 SATA_DWC_INTMR_PMABRTM |
1250 SATA_DWC_INTMR_DMATM);
1251
1252
1253
1254
1255 out_le32(&hsdev->sata_dwc_regs->errmr, SATA_DWC_SERROR_ERR_BITS);
1256
1257 dev_dbg(host_pvt.dwc_dev, "%s: INTMR = 0x%08x, ERRMR = 0x%08x\n",
1258 __func__, in_le32(&hsdev->sata_dwc_regs->intmr),
1259 in_le32(&hsdev->sata_dwc_regs->errmr));
1260}
1261
1262static void sata_dwc_setup_port(struct ata_ioports *port, unsigned long base)
1263{
1264 port->cmd_addr = (void *)base + 0x00;
1265 port->data_addr = (void *)base + 0x00;
1266
1267 port->error_addr = (void *)base + 0x04;
1268 port->feature_addr = (void *)base + 0x04;
1269
1270 port->nsect_addr = (void *)base + 0x08;
1271
1272 port->lbal_addr = (void *)base + 0x0c;
1273 port->lbam_addr = (void *)base + 0x10;
1274 port->lbah_addr = (void *)base + 0x14;
1275
1276 port->device_addr = (void *)base + 0x18;
1277 port->command_addr = (void *)base + 0x1c;
1278 port->status_addr = (void *)base + 0x1c;
1279
1280 port->altstatus_addr = (void *)base + 0x20;
1281 port->ctl_addr = (void *)base + 0x20;
1282}
1283
1284
1285
1286
1287
1288
1289
1290static int sata_dwc_port_start(struct ata_port *ap)
1291{
1292 int err = 0;
1293 struct sata_dwc_device *hsdev;
1294 struct sata_dwc_device_port *hsdevp = NULL;
1295 struct device *pdev;
1296 int i;
1297
1298 hsdev = HSDEV_FROM_AP(ap);
1299
1300 dev_dbg(ap->dev, "%s: port_no=%d\n", __func__, ap->port_no);
1301
1302 hsdev->host = ap->host;
1303 pdev = ap->host->dev;
1304 if (!pdev) {
1305 dev_err(ap->dev, "%s: no ap->host->dev\n", __func__);
1306 err = -ENODEV;
1307 goto CLEANUP;
1308 }
1309
1310
1311 hsdevp = kzalloc(sizeof(*hsdevp), GFP_KERNEL);
1312 if (!hsdevp) {
1313 dev_err(ap->dev, "%s: kmalloc failed for hsdevp\n", __func__);
1314 err = -ENOMEM;
1315 goto CLEANUP;
1316 }
1317 hsdevp->hsdev = hsdev;
1318
1319 for (i = 0; i < SATA_DWC_QCMD_MAX; i++)
1320 hsdevp->cmd_issued[i] = SATA_DWC_CMD_ISSUED_NOT;
1321
1322 ap->bmdma_prd = 0;
1323 ap->bmdma_prd_dma = 0;
1324
1325
1326
1327
1328
1329 for (i = 0; i < SATA_DWC_QCMD_MAX; i++) {
1330 hsdevp->llit[i] = dma_alloc_coherent(pdev,
1331 SATA_DWC_DMAC_LLI_TBL_SZ,
1332 &(hsdevp->llit_dma[i]),
1333 GFP_ATOMIC);
1334 if (!hsdevp->llit[i]) {
1335 dev_err(ap->dev, "%s: dma_alloc_coherent failed\n",
1336 __func__);
1337 err = -ENOMEM;
1338 goto CLEANUP_ALLOC;
1339 }
1340 }
1341
1342 if (ap->port_no == 0) {
1343 dev_dbg(ap->dev, "%s: clearing TXCHEN, RXCHEN in DMAC\n",
1344 __func__);
1345 out_le32(&hsdev->sata_dwc_regs->dmacr,
1346 SATA_DWC_DMACR_TXRXCH_CLEAR);
1347
1348 dev_dbg(ap->dev, "%s: setting burst size in DBTSR\n",
1349 __func__);
1350 out_le32(&hsdev->sata_dwc_regs->dbtsr,
1351 (SATA_DWC_DBTSR_MWR(AHB_DMA_BRST_DFLT) |
1352 SATA_DWC_DBTSR_MRD(AHB_DMA_BRST_DFLT)));
1353 }
1354
1355
1356 clear_serror();
1357 ap->private_data = hsdevp;
1358 dev_dbg(ap->dev, "%s: done\n", __func__);
1359 return 0;
1360
1361CLEANUP_ALLOC:
1362 kfree(hsdevp);
1363CLEANUP:
1364 dev_dbg(ap->dev, "%s: fail. ap->id = %d\n", __func__, ap->print_id);
1365 return err;
1366}
1367
1368static void sata_dwc_port_stop(struct ata_port *ap)
1369{
1370 int i;
1371 struct sata_dwc_device *hsdev = HSDEV_FROM_AP(ap);
1372 struct sata_dwc_device_port *hsdevp = HSDEVP_FROM_AP(ap);
1373
1374 dev_dbg(ap->dev, "%s: ap->id = %d\n", __func__, ap->print_id);
1375
1376 if (hsdevp && hsdev) {
1377
1378 for (i = 0; i < SATA_DWC_QCMD_MAX; i++) {
1379 dma_free_coherent(ap->host->dev,
1380 SATA_DWC_DMAC_LLI_TBL_SZ,
1381 hsdevp->llit[i], hsdevp->llit_dma[i]);
1382 }
1383
1384 kfree(hsdevp);
1385 }
1386 ap->private_data = NULL;
1387}
1388
1389
1390
1391
1392
1393
1394
1395
1396static void sata_dwc_exec_command_by_tag(struct ata_port *ap,
1397 struct ata_taskfile *tf,
1398 u8 tag, u32 cmd_issued)
1399{
1400 unsigned long flags;
1401 struct sata_dwc_device_port *hsdevp = HSDEVP_FROM_AP(ap);
1402
1403 dev_dbg(ap->dev, "%s cmd(0x%02x): %s tag=%d\n", __func__, tf->command,
1404 ata_get_cmd_descript(tf->command), tag);
1405
1406 spin_lock_irqsave(&ap->host->lock, flags);
1407 hsdevp->cmd_issued[tag] = cmd_issued;
1408 spin_unlock_irqrestore(&ap->host->lock, flags);
1409
1410
1411
1412
1413
1414
1415 clear_serror();
1416 ata_sff_exec_command(ap, tf);
1417}
1418
1419static void sata_dwc_bmdma_setup_by_tag(struct ata_queued_cmd *qc, u8 tag)
1420{
1421 sata_dwc_exec_command_by_tag(qc->ap, &qc->tf, tag,
1422 SATA_DWC_CMD_ISSUED_PEND);
1423}
1424
1425static void sata_dwc_bmdma_setup(struct ata_queued_cmd *qc)
1426{
1427 u8 tag = qc->tag;
1428
1429 if (ata_is_ncq(qc->tf.protocol)) {
1430 dev_dbg(qc->ap->dev, "%s: ap->link.sactive=0x%08x tag=%d\n",
1431 __func__, qc->ap->link.sactive, tag);
1432 } else {
1433 tag = 0;
1434 }
1435 sata_dwc_bmdma_setup_by_tag(qc, tag);
1436}
1437
1438static void sata_dwc_bmdma_start_by_tag(struct ata_queued_cmd *qc, u8 tag)
1439{
1440 int start_dma;
1441 u32 reg, dma_chan;
1442 struct sata_dwc_device *hsdev = HSDEV_FROM_QC(qc);
1443 struct ata_port *ap = qc->ap;
1444 struct sata_dwc_device_port *hsdevp = HSDEVP_FROM_AP(ap);
1445 int dir = qc->dma_dir;
1446 dma_chan = hsdevp->dma_chan[tag];
1447
1448 if (hsdevp->cmd_issued[tag] != SATA_DWC_CMD_ISSUED_NOT) {
1449 start_dma = 1;
1450 if (dir == DMA_TO_DEVICE)
1451 hsdevp->dma_pending[tag] = SATA_DWC_DMA_PENDING_TX;
1452 else
1453 hsdevp->dma_pending[tag] = SATA_DWC_DMA_PENDING_RX;
1454 } else {
1455 dev_err(ap->dev, "%s: Command not pending cmd_issued=%d "
1456 "(tag=%d) DMA NOT started\n", __func__,
1457 hsdevp->cmd_issued[tag], tag);
1458 start_dma = 0;
1459 }
1460
1461 dev_dbg(ap->dev, "%s qc=%p tag: %x cmd: 0x%02x dma_dir: %s "
1462 "start_dma? %x\n", __func__, qc, tag, qc->tf.command,
1463 get_dma_dir_descript(qc->dma_dir), start_dma);
1464 sata_dwc_tf_dump(&(qc->tf));
1465
1466 if (start_dma) {
1467 reg = core_scr_read(SCR_ERROR);
1468 if (reg & SATA_DWC_SERROR_ERR_BITS) {
1469 dev_err(ap->dev, "%s: ****** SError=0x%08x ******\n",
1470 __func__, reg);
1471 }
1472
1473 if (dir == DMA_TO_DEVICE)
1474 out_le32(&hsdev->sata_dwc_regs->dmacr,
1475 SATA_DWC_DMACR_TXCHEN);
1476 else
1477 out_le32(&hsdev->sata_dwc_regs->dmacr,
1478 SATA_DWC_DMACR_RXCHEN);
1479
1480
1481 dma_dwc_xfer_start(dma_chan);
1482 }
1483}
1484
1485static void sata_dwc_bmdma_start(struct ata_queued_cmd *qc)
1486{
1487 u8 tag = qc->tag;
1488
1489 if (ata_is_ncq(qc->tf.protocol)) {
1490 dev_dbg(qc->ap->dev, "%s: ap->link.sactive=0x%08x tag=%d\n",
1491 __func__, qc->ap->link.sactive, tag);
1492 } else {
1493 tag = 0;
1494 }
1495 dev_dbg(qc->ap->dev, "%s\n", __func__);
1496 sata_dwc_bmdma_start_by_tag(qc, tag);
1497}
1498
1499
1500
1501
1502
1503
1504
1505static void sata_dwc_qc_prep_by_tag(struct ata_queued_cmd *qc, u8 tag)
1506{
1507 struct scatterlist *sg = qc->sg;
1508 struct ata_port *ap = qc->ap;
1509 int dma_chan;
1510 struct sata_dwc_device *hsdev = HSDEV_FROM_AP(ap);
1511 struct sata_dwc_device_port *hsdevp = HSDEVP_FROM_AP(ap);
1512
1513 dev_dbg(ap->dev, "%s: port=%d dma dir=%s n_elem=%d\n",
1514 __func__, ap->port_no, get_dma_dir_descript(qc->dma_dir),
1515 qc->n_elem);
1516
1517 dma_chan = dma_dwc_xfer_setup(sg, qc->n_elem, hsdevp->llit[tag],
1518 hsdevp->llit_dma[tag],
1519 (void *__iomem)(&hsdev->sata_dwc_regs->\
1520 dmadr), qc->dma_dir);
1521 if (dma_chan < 0) {
1522 dev_err(ap->dev, "%s: dma_dwc_xfer_setup returns err %d\n",
1523 __func__, dma_chan);
1524 return;
1525 }
1526 hsdevp->dma_chan[tag] = dma_chan;
1527}
1528
1529static unsigned int sata_dwc_qc_issue(struct ata_queued_cmd *qc)
1530{
1531 u32 sactive;
1532 u8 tag = qc->tag;
1533 struct ata_port *ap = qc->ap;
1534
1535#ifdef DEBUG_NCQ
1536 if (qc->tag > 0 || ap->link.sactive > 1)
1537 dev_info(ap->dev, "%s ap id=%d cmd(0x%02x)=%s qc tag=%d "
1538 "prot=%s ap active_tag=0x%08x ap sactive=0x%08x\n",
1539 __func__, ap->print_id, qc->tf.command,
1540 ata_get_cmd_descript(qc->tf.command),
1541 qc->tag, get_prot_descript(qc->tf.protocol),
1542 ap->link.active_tag, ap->link.sactive);
1543#endif
1544
1545 if (!ata_is_ncq(qc->tf.protocol))
1546 tag = 0;
1547 sata_dwc_qc_prep_by_tag(qc, tag);
1548
1549 if (ata_is_ncq(qc->tf.protocol)) {
1550 sactive = core_scr_read(SCR_ACTIVE);
1551 sactive |= (0x00000001 << tag);
1552 core_scr_write(SCR_ACTIVE, sactive);
1553
1554 dev_dbg(qc->ap->dev, "%s: tag=%d ap->link.sactive = 0x%08x "
1555 "sactive=0x%08x\n", __func__, tag, qc->ap->link.sactive,
1556 sactive);
1557
1558 ap->ops->sff_tf_load(ap, &qc->tf);
1559 sata_dwc_exec_command_by_tag(ap, &qc->tf, qc->tag,
1560 SATA_DWC_CMD_ISSUED_PEND);
1561 } else {
1562 ata_sff_qc_issue(qc);
1563 }
1564 return 0;
1565}
1566
1567
1568
1569
1570
1571
1572
1573
1574static void sata_dwc_qc_prep(struct ata_queued_cmd *qc)
1575{
1576 if ((qc->dma_dir == DMA_NONE) || (qc->tf.protocol == ATA_PROT_PIO))
1577 return;
1578
1579#ifdef DEBUG_NCQ
1580 if (qc->tag > 0)
1581 dev_info(qc->ap->dev, "%s: qc->tag=%d ap->active_tag=0x%08x\n",
1582 __func__, qc->tag, qc->ap->link.active_tag);
1583
1584 return ;
1585#endif
1586}
1587
1588static void sata_dwc_error_handler(struct ata_port *ap)
1589{
1590 ata_sff_error_handler(ap);
1591}
1592
1593int sata_dwc_hardreset(struct ata_link *link, unsigned int *class,
1594 unsigned long deadline)
1595{
1596 struct sata_dwc_device *hsdev = HSDEV_FROM_AP(link->ap);
1597 int ret;
1598
1599 ret = sata_sff_hardreset(link, class, deadline);
1600
1601 sata_dwc_enable_interrupts(hsdev);
1602
1603
1604 out_le32(&hsdev->sata_dwc_regs->dmacr,
1605 SATA_DWC_DMACR_TXRXCH_CLEAR);
1606
1607
1608 out_le32(&hsdev->sata_dwc_regs->dbtsr,
1609 SATA_DWC_DBTSR_MWR(AHB_DMA_BRST_DFLT) |
1610 SATA_DWC_DBTSR_MRD(AHB_DMA_BRST_DFLT));
1611
1612 return ret;
1613}
1614
1615
1616
1617
1618static struct scsi_host_template sata_dwc_sht = {
1619 ATA_NCQ_SHT(DRV_NAME),
1620
1621
1622
1623
1624
1625 .sg_tablesize = LIBATA_MAX_PRD,
1626 .can_queue = ATA_DEF_QUEUE,
1627 .dma_boundary = ATA_DMA_BOUNDARY,
1628};
1629
1630static struct ata_port_operations sata_dwc_ops = {
1631 .inherits = &ata_sff_port_ops,
1632
1633 .error_handler = sata_dwc_error_handler,
1634 .hardreset = sata_dwc_hardreset,
1635
1636 .qc_prep = sata_dwc_qc_prep,
1637 .qc_issue = sata_dwc_qc_issue,
1638
1639 .scr_read = sata_dwc_scr_read,
1640 .scr_write = sata_dwc_scr_write,
1641
1642 .port_start = sata_dwc_port_start,
1643 .port_stop = sata_dwc_port_stop,
1644
1645 .bmdma_setup = sata_dwc_bmdma_setup,
1646 .bmdma_start = sata_dwc_bmdma_start,
1647};
1648
1649static const struct ata_port_info sata_dwc_port_info[] = {
1650 {
1651 .flags = ATA_FLAG_SATA | ATA_FLAG_NCQ,
1652 .pio_mask = ATA_PIO4,
1653 .udma_mask = ATA_UDMA6,
1654 .port_ops = &sata_dwc_ops,
1655 },
1656};
1657
1658static int sata_dwc_probe(struct platform_device *ofdev)
1659{
1660 struct sata_dwc_device *hsdev;
1661 u32 idr, versionr;
1662 char *ver = (char *)&versionr;
1663 u8 *base = NULL;
1664 int err = 0;
1665 int irq, rc;
1666 struct ata_host *host;
1667 struct ata_port_info pi = sata_dwc_port_info[0];
1668 const struct ata_port_info *ppi[] = { &pi, NULL };
1669 struct device_node *np = ofdev->dev.of_node;
1670 u32 dma_chan;
1671
1672
1673 hsdev = kzalloc(sizeof(*hsdev), GFP_KERNEL);
1674 if (hsdev == NULL) {
1675 dev_err(&ofdev->dev, "kmalloc failed for hsdev\n");
1676 err = -ENOMEM;
1677 goto error;
1678 }
1679
1680 if (of_property_read_u32(np, "dma-channel", &dma_chan)) {
1681 dev_warn(&ofdev->dev, "no dma-channel property set."
1682 " Use channel 0\n");
1683 dma_chan = 0;
1684 }
1685 host_pvt.dma_channel = dma_chan;
1686
1687
1688 base = of_iomap(ofdev->dev.of_node, 0);
1689 if (!base) {
1690 dev_err(&ofdev->dev, "ioremap failed for SATA register"
1691 " address\n");
1692 err = -ENODEV;
1693 goto error_kmalloc;
1694 }
1695 hsdev->reg_base = base;
1696 dev_dbg(&ofdev->dev, "ioremap done for SATA register address\n");
1697
1698
1699 hsdev->sata_dwc_regs = (void *__iomem)(base + SATA_DWC_REG_OFFSET);
1700
1701
1702 host = ata_host_alloc_pinfo(&ofdev->dev, ppi, SATA_DWC_MAX_PORTS);
1703 if (!host) {
1704 dev_err(&ofdev->dev, "ata_host_alloc_pinfo failed\n");
1705 err = -ENOMEM;
1706 goto error_iomap;
1707 }
1708
1709 host->private_data = hsdev;
1710
1711
1712 host->ports[0]->ioaddr.cmd_addr = base;
1713 host->ports[0]->ioaddr.scr_addr = base + SATA_DWC_SCR_OFFSET;
1714 host_pvt.scr_addr_sstatus = base + SATA_DWC_SCR_OFFSET;
1715 sata_dwc_setup_port(&host->ports[0]->ioaddr, (unsigned long)base);
1716
1717
1718 idr = in_le32(&hsdev->sata_dwc_regs->idr);
1719 versionr = in_le32(&hsdev->sata_dwc_regs->versionr);
1720 dev_notice(&ofdev->dev, "id %d, controller version %c.%c%c\n",
1721 idr, ver[0], ver[1], ver[2]);
1722
1723
1724 irq = irq_of_parse_and_map(ofdev->dev.of_node, 1);
1725 if (irq == NO_IRQ) {
1726 dev_err(&ofdev->dev, "no SATA DMA irq\n");
1727 err = -ENODEV;
1728 goto error_out;
1729 }
1730
1731
1732 host_pvt.sata_dma_regs = of_iomap(ofdev->dev.of_node, 1);
1733 if (!(host_pvt.sata_dma_regs)) {
1734 dev_err(&ofdev->dev, "ioremap failed for AHBDMA register"
1735 " address\n");
1736 err = -ENODEV;
1737 goto error_out;
1738 }
1739
1740
1741 host_pvt.dwc_dev = &ofdev->dev;
1742
1743
1744 dma_dwc_init(hsdev, irq);
1745
1746
1747 sata_dwc_enable_interrupts(hsdev);
1748
1749
1750 irq = irq_of_parse_and_map(ofdev->dev.of_node, 0);
1751 if (irq == NO_IRQ) {
1752 dev_err(&ofdev->dev, "no SATA DMA irq\n");
1753 err = -ENODEV;
1754 goto error_out;
1755 }
1756
1757
1758
1759
1760
1761
1762 rc = ata_host_activate(host, irq, sata_dwc_isr, 0, &sata_dwc_sht);
1763
1764 if (rc != 0)
1765 dev_err(&ofdev->dev, "failed to activate host");
1766
1767 dev_set_drvdata(&ofdev->dev, host);
1768 return 0;
1769
1770error_out:
1771
1772 dma_dwc_exit(hsdev);
1773
1774error_iomap:
1775 iounmap(base);
1776error_kmalloc:
1777 kfree(hsdev);
1778error:
1779 return err;
1780}
1781
1782static int sata_dwc_remove(struct platform_device *ofdev)
1783{
1784 struct device *dev = &ofdev->dev;
1785 struct ata_host *host = dev_get_drvdata(dev);
1786 struct sata_dwc_device *hsdev = host->private_data;
1787
1788 ata_host_detach(host);
1789 dev_set_drvdata(dev, NULL);
1790
1791
1792 dma_dwc_exit(hsdev);
1793
1794 iounmap(hsdev->reg_base);
1795 kfree(hsdev);
1796 kfree(host);
1797 dev_dbg(&ofdev->dev, "done\n");
1798 return 0;
1799}
1800
1801static const struct of_device_id sata_dwc_match[] = {
1802 { .compatible = "amcc,sata-460ex", },
1803 {}
1804};
1805MODULE_DEVICE_TABLE(of, sata_dwc_match);
1806
1807static struct platform_driver sata_dwc_driver = {
1808 .driver = {
1809 .name = DRV_NAME,
1810 .owner = THIS_MODULE,
1811 .of_match_table = sata_dwc_match,
1812 },
1813 .probe = sata_dwc_probe,
1814 .remove = sata_dwc_remove,
1815};
1816
1817module_platform_driver(sata_dwc_driver);
1818
1819MODULE_LICENSE("GPL");
1820MODULE_AUTHOR("Mark Miesfeld <mmiesfeld@amcc.com>");
1821MODULE_DESCRIPTION("DesignWare Cores SATA controller low lever driver");
1822MODULE_VERSION(DRV_VERSION);
1823