blob: 01a5400bd7c533dc3f44f0dc4ce1da4804758a61 [file] [log] [blame]
Li Yangfaf0b2e2007-10-16 20:58:38 +08001/*
2 * drivers/ata/sata_fsl.c
3 *
4 * Freescale 3.0Gbps SATA device driver
5 *
6 * Author: Ashish Kalra <ashish.kalra@freescale.com>
7 * Li Yang <leoli@freescale.com>
8 *
Xulei2f957fc2011-01-19 17:07:29 +08009 * Copyright (c) 2006-2007, 2011 Freescale Semiconductor, Inc.
Li Yangfaf0b2e2007-10-16 20:58:38 +080010 *
11 * This program is free software; you can redistribute it and/or modify it
12 * under the terms of the GNU General Public License as published by the
13 * Free Software Foundation; either version 2 of the License, or (at your
14 * option) any later version.
15 *
16 */
17
18#include <linux/kernel.h>
19#include <linux/module.h>
20#include <linux/platform_device.h>
Tejun Heo5a0e3ad2010-03-24 17:04:11 +090021#include <linux/slab.h>
Li Yangfaf0b2e2007-10-16 20:58:38 +080022
23#include <scsi/scsi_host.h>
24#include <scsi/scsi_cmnd.h>
25#include <linux/libata.h>
26#include <asm/io.h>
27#include <linux/of_platform.h>
28
29/* Controller information */
30enum {
31 SATA_FSL_QUEUE_DEPTH = 16,
32 SATA_FSL_MAX_PRD = 63,
33 SATA_FSL_MAX_PRD_USABLE = SATA_FSL_MAX_PRD - 1,
34 SATA_FSL_MAX_PRD_DIRECT = 16, /* Direct PRDT entries */
35
36 SATA_FSL_HOST_FLAGS = (ATA_FLAG_SATA | ATA_FLAG_NO_LEGACY |
37 ATA_FLAG_MMIO | ATA_FLAG_PIO_DMA |
ashish kalrafd6c29e2009-07-01 20:59:43 +053038 ATA_FLAG_PMP | ATA_FLAG_NCQ | ATA_FLAG_AN),
Li Yangfaf0b2e2007-10-16 20:58:38 +080039
40 SATA_FSL_MAX_CMDS = SATA_FSL_QUEUE_DEPTH,
41 SATA_FSL_CMD_HDR_SIZE = 16, /* 4 DWORDS */
42 SATA_FSL_CMD_SLOT_SIZE = (SATA_FSL_MAX_CMDS * SATA_FSL_CMD_HDR_SIZE),
43
44 /*
45 * SATA-FSL host controller supports a max. of (15+1) direct PRDEs, and
46 * chained indirect PRDEs upto a max count of 63.
André Goddard Rosaaf901ca2009-11-14 13:09:05 -020047 * We are allocating an array of 63 PRDEs contiguously, but PRDE#15 will
Li Yangfaf0b2e2007-10-16 20:58:38 +080048 * be setup as an indirect descriptor, pointing to it's next
André Goddard Rosaaf901ca2009-11-14 13:09:05 -020049 * (contiguous) PRDE. Though chained indirect PRDE arrays are
Li Yangfaf0b2e2007-10-16 20:58:38 +080050 * supported,it will be more efficient to use a direct PRDT and
51 * a single chain/link to indirect PRDE array/PRDT.
52 */
53
54 SATA_FSL_CMD_DESC_CFIS_SZ = 32,
55 SATA_FSL_CMD_DESC_SFIS_SZ = 32,
56 SATA_FSL_CMD_DESC_ACMD_SZ = 16,
57 SATA_FSL_CMD_DESC_RSRVD = 16,
58
59 SATA_FSL_CMD_DESC_SIZE = (SATA_FSL_CMD_DESC_CFIS_SZ +
60 SATA_FSL_CMD_DESC_SFIS_SZ +
61 SATA_FSL_CMD_DESC_ACMD_SZ +
62 SATA_FSL_CMD_DESC_RSRVD +
63 SATA_FSL_MAX_PRD * 16),
64
65 SATA_FSL_CMD_DESC_OFFSET_TO_PRDT =
66 (SATA_FSL_CMD_DESC_CFIS_SZ +
67 SATA_FSL_CMD_DESC_SFIS_SZ +
68 SATA_FSL_CMD_DESC_ACMD_SZ +
69 SATA_FSL_CMD_DESC_RSRVD),
70
71 SATA_FSL_CMD_DESC_AR_SZ = (SATA_FSL_CMD_DESC_SIZE * SATA_FSL_MAX_CMDS),
72 SATA_FSL_PORT_PRIV_DMA_SZ = (SATA_FSL_CMD_SLOT_SIZE +
73 SATA_FSL_CMD_DESC_AR_SZ),
74
75 /*
76 * MPC8315 has two SATA controllers, SATA1 & SATA2
77 * (one port per controller)
78 * MPC837x has 2/4 controllers, one port per controller
79 */
80
81 SATA_FSL_MAX_PORTS = 1,
82
83 SATA_FSL_IRQ_FLAG = IRQF_SHARED,
84};
85
86/*
87* Host Controller command register set - per port
88*/
89enum {
90 CQ = 0,
91 CA = 8,
92 CC = 0x10,
93 CE = 0x18,
94 DE = 0x20,
95 CHBA = 0x24,
96 HSTATUS = 0x28,
97 HCONTROL = 0x2C,
98 CQPMP = 0x30,
99 SIGNATURE = 0x34,
100 ICC = 0x38,
101
102 /*
103 * Host Status Register (HStatus) bitdefs
104 */
105 ONLINE = (1 << 31),
106 GOING_OFFLINE = (1 << 30),
107 BIST_ERR = (1 << 29),
108
109 FATAL_ERR_HC_MASTER_ERR = (1 << 18),
110 FATAL_ERR_PARITY_ERR_TX = (1 << 17),
111 FATAL_ERR_PARITY_ERR_RX = (1 << 16),
112 FATAL_ERR_DATA_UNDERRUN = (1 << 13),
113 FATAL_ERR_DATA_OVERRUN = (1 << 12),
114 FATAL_ERR_CRC_ERR_TX = (1 << 11),
115 FATAL_ERR_CRC_ERR_RX = (1 << 10),
116 FATAL_ERR_FIFO_OVRFL_TX = (1 << 9),
117 FATAL_ERR_FIFO_OVRFL_RX = (1 << 8),
118
119 FATAL_ERROR_DECODE = FATAL_ERR_HC_MASTER_ERR |
120 FATAL_ERR_PARITY_ERR_TX |
121 FATAL_ERR_PARITY_ERR_RX |
122 FATAL_ERR_DATA_UNDERRUN |
123 FATAL_ERR_DATA_OVERRUN |
124 FATAL_ERR_CRC_ERR_TX |
125 FATAL_ERR_CRC_ERR_RX |
126 FATAL_ERR_FIFO_OVRFL_TX | FATAL_ERR_FIFO_OVRFL_RX,
127
128 INT_ON_FATAL_ERR = (1 << 5),
129 INT_ON_PHYRDY_CHG = (1 << 4),
130
131 INT_ON_SIGNATURE_UPDATE = (1 << 3),
132 INT_ON_SNOTIFY_UPDATE = (1 << 2),
133 INT_ON_SINGL_DEVICE_ERR = (1 << 1),
134 INT_ON_CMD_COMPLETE = 1,
135
ashish kalrafd6c29e2009-07-01 20:59:43 +0530136 INT_ON_ERROR = INT_ON_FATAL_ERR | INT_ON_SNOTIFY_UPDATE |
Li Yangfaf0b2e2007-10-16 20:58:38 +0800137 INT_ON_PHYRDY_CHG | INT_ON_SINGL_DEVICE_ERR,
138
139 /*
140 * Host Control Register (HControl) bitdefs
141 */
142 HCONTROL_ONLINE_PHY_RST = (1 << 31),
143 HCONTROL_FORCE_OFFLINE = (1 << 30),
144 HCONTROL_PARITY_PROT_MOD = (1 << 14),
145 HCONTROL_DPATH_PARITY = (1 << 12),
146 HCONTROL_SNOOP_ENABLE = (1 << 10),
147 HCONTROL_PMP_ATTACHED = (1 << 9),
148 HCONTROL_COPYOUT_STATFIS = (1 << 8),
149 IE_ON_FATAL_ERR = (1 << 5),
150 IE_ON_PHYRDY_CHG = (1 << 4),
151 IE_ON_SIGNATURE_UPDATE = (1 << 3),
152 IE_ON_SNOTIFY_UPDATE = (1 << 2),
153 IE_ON_SINGL_DEVICE_ERR = (1 << 1),
154 IE_ON_CMD_COMPLETE = 1,
155
156 DEFAULT_PORT_IRQ_ENABLE_MASK = IE_ON_FATAL_ERR | IE_ON_PHYRDY_CHG |
ashish kalrafd6c29e2009-07-01 20:59:43 +0530157 IE_ON_SIGNATURE_UPDATE | IE_ON_SNOTIFY_UPDATE |
Li Yangfaf0b2e2007-10-16 20:58:38 +0800158 IE_ON_SINGL_DEVICE_ERR | IE_ON_CMD_COMPLETE,
159
160 EXT_INDIRECT_SEG_PRD_FLAG = (1 << 31),
Xulei2f957fc2011-01-19 17:07:29 +0800161 DATA_SNOOP_ENABLE_V1 = (1 << 22),
162 DATA_SNOOP_ENABLE_V2 = (1 << 28),
Li Yangfaf0b2e2007-10-16 20:58:38 +0800163};
164
165/*
166 * SATA Superset Registers
167 */
168enum {
169 SSTATUS = 0,
170 SERROR = 4,
171 SCONTROL = 8,
172 SNOTIFY = 0xC,
173};
174
175/*
176 * Control Status Register Set
177 */
178enum {
179 TRANSCFG = 0,
180 TRANSSTATUS = 4,
181 LINKCFG = 8,
182 LINKCFG1 = 0xC,
183 LINKCFG2 = 0x10,
184 LINKSTATUS = 0x14,
185 LINKSTATUS1 = 0x18,
186 PHYCTRLCFG = 0x1C,
187 COMMANDSTAT = 0x20,
188};
189
190/* PHY (link-layer) configuration control */
191enum {
192 PHY_BIST_ENABLE = 0x01,
193};
194
195/*
196 * Command Header Table entry, i.e, command slot
197 * 4 Dwords per command slot, command header size == 64 Dwords.
198 */
199struct cmdhdr_tbl_entry {
200 u32 cda;
201 u32 prde_fis_len;
202 u32 ttl;
203 u32 desc_info;
204};
205
206/*
207 * Description information bitdefs
208 */
209enum {
Dave Liud3587242009-05-14 09:47:07 -0500210 CMD_DESC_RES = (1 << 11),
Li Yangfaf0b2e2007-10-16 20:58:38 +0800211 VENDOR_SPECIFIC_BIST = (1 << 10),
212 CMD_DESC_SNOOP_ENABLE = (1 << 9),
213 FPDMA_QUEUED_CMD = (1 << 8),
214 SRST_CMD = (1 << 7),
215 BIST = (1 << 6),
216 ATAPI_CMD = (1 << 5),
217};
218
219/*
220 * Command Descriptor
221 */
222struct command_desc {
223 u8 cfis[8 * 4];
224 u8 sfis[8 * 4];
225 u8 acmd[4 * 4];
226 u8 fill[4 * 4];
227 u32 prdt[SATA_FSL_MAX_PRD_DIRECT * 4];
228 u32 prdt_indirect[(SATA_FSL_MAX_PRD - SATA_FSL_MAX_PRD_DIRECT) * 4];
229};
230
231/*
232 * Physical region table descriptor(PRD)
233 */
234
235struct prde {
236 u32 dba;
237 u8 fill[2 * 4];
238 u32 ddc_and_ext;
239};
240
241/*
242 * ata_port private data
243 * This is our per-port instance data.
244 */
245struct sata_fsl_port_priv {
246 struct cmdhdr_tbl_entry *cmdslot;
247 dma_addr_t cmdslot_paddr;
248 struct command_desc *cmdentry;
249 dma_addr_t cmdentry_paddr;
Li Yangfaf0b2e2007-10-16 20:58:38 +0800250};
251
252/*
253 * ata_port->host_set private data
254 */
255struct sata_fsl_host_priv {
256 void __iomem *hcr_base;
257 void __iomem *ssr_base;
258 void __iomem *csr_base;
Li Yang79b3edc2007-10-31 19:27:55 +0800259 int irq;
Xulei2f957fc2011-01-19 17:07:29 +0800260 int data_snoop;
Li Yangfaf0b2e2007-10-16 20:58:38 +0800261};
262
263static inline unsigned int sata_fsl_tag(unsigned int tag,
Li Yang520d3a12007-10-31 19:28:01 +0800264 void __iomem *hcr_base)
Li Yangfaf0b2e2007-10-16 20:58:38 +0800265{
266 /* We let libATA core do actual (queue) tag allocation */
267
268 /* all non NCQ/queued commands should have tag#0 */
269 if (ata_tag_internal(tag)) {
270 DPRINTK("mapping internal cmds to tag#0\n");
271 return 0;
272 }
273
274 if (unlikely(tag >= SATA_FSL_QUEUE_DEPTH)) {
275 DPRINTK("tag %d invalid : out of range\n", tag);
276 return 0;
277 }
278
279 if (unlikely((ioread32(hcr_base + CQ)) & (1 << tag))) {
280 DPRINTK("tag %d invalid : in use!!\n", tag);
281 return 0;
282 }
283
284 return tag;
285}
286
287static void sata_fsl_setup_cmd_hdr_entry(struct sata_fsl_port_priv *pp,
288 unsigned int tag, u32 desc_info,
289 u32 data_xfer_len, u8 num_prde,
290 u8 fis_len)
291{
292 dma_addr_t cmd_descriptor_address;
293
294 cmd_descriptor_address = pp->cmdentry_paddr +
295 tag * SATA_FSL_CMD_DESC_SIZE;
296
297 /* NOTE: both data_xfer_len & fis_len are Dword counts */
298
299 pp->cmdslot[tag].cda = cpu_to_le32(cmd_descriptor_address);
300 pp->cmdslot[tag].prde_fis_len =
301 cpu_to_le32((num_prde << 16) | (fis_len << 2));
302 pp->cmdslot[tag].ttl = cpu_to_le32(data_xfer_len & ~0x03);
Li Yang520d3a12007-10-31 19:28:01 +0800303 pp->cmdslot[tag].desc_info = cpu_to_le32(desc_info | (tag & 0x1F));
Li Yangfaf0b2e2007-10-16 20:58:38 +0800304
305 VPRINTK("cda=0x%x, prde_fis_len=0x%x, ttl=0x%x, di=0x%x\n",
306 pp->cmdslot[tag].cda,
307 pp->cmdslot[tag].prde_fis_len,
308 pp->cmdslot[tag].ttl, pp->cmdslot[tag].desc_info);
309
310}
311
312static unsigned int sata_fsl_fill_sg(struct ata_queued_cmd *qc, void *cmd_desc,
Xulei2f957fc2011-01-19 17:07:29 +0800313 u32 *ttl, dma_addr_t cmd_desc_paddr,
314 int data_snoop)
Li Yangfaf0b2e2007-10-16 20:58:38 +0800315{
316 struct scatterlist *sg;
317 unsigned int num_prde = 0;
318 u32 ttl_dwords = 0;
319
320 /*
André Goddard Rosaaf901ca2009-11-14 13:09:05 -0200321 * NOTE : direct & indirect prdt's are contiguously allocated
Li Yangfaf0b2e2007-10-16 20:58:38 +0800322 */
323 struct prde *prd = (struct prde *)&((struct command_desc *)
324 cmd_desc)->prdt;
325
326 struct prde *prd_ptr_to_indirect_ext = NULL;
327 unsigned indirect_ext_segment_sz = 0;
328 dma_addr_t indirect_ext_segment_paddr;
Tejun Heoff2aeb12007-12-05 16:43:11 +0900329 unsigned int si;
Li Yangfaf0b2e2007-10-16 20:58:38 +0800330
Anton Vorontsovb1f5dc42008-02-22 19:54:25 +0300331 VPRINTK("SATA FSL : cd = 0x%p, prd = 0x%p\n", cmd_desc, prd);
Li Yangfaf0b2e2007-10-16 20:58:38 +0800332
333 indirect_ext_segment_paddr = cmd_desc_paddr +
334 SATA_FSL_CMD_DESC_OFFSET_TO_PRDT + SATA_FSL_MAX_PRD_DIRECT * 16;
335
Tejun Heoff2aeb12007-12-05 16:43:11 +0900336 for_each_sg(qc->sg, sg, qc->n_elem, si) {
Li Yangfaf0b2e2007-10-16 20:58:38 +0800337 dma_addr_t sg_addr = sg_dma_address(sg);
338 u32 sg_len = sg_dma_len(sg);
339
Kumar Galaf48c0192009-05-13 22:10:50 -0500340 VPRINTK("SATA FSL : fill_sg, sg_addr = 0x%llx, sg_len = %d\n",
341 (unsigned long long)sg_addr, sg_len);
Li Yangfaf0b2e2007-10-16 20:58:38 +0800342
343 /* warn if each s/g element is not dword aligned */
344 if (sg_addr & 0x03)
345 ata_port_printk(qc->ap, KERN_ERR,
Kumar Galaf48c0192009-05-13 22:10:50 -0500346 "s/g addr unaligned : 0x%llx\n",
347 (unsigned long long)sg_addr);
Li Yangfaf0b2e2007-10-16 20:58:38 +0800348 if (sg_len & 0x03)
349 ata_port_printk(qc->ap, KERN_ERR,
350 "s/g len unaligned : 0x%x\n", sg_len);
351
James Bottomley37198e32008-02-05 14:06:27 +0900352 if (num_prde == (SATA_FSL_MAX_PRD_DIRECT - 1) &&
353 sg_next(sg) != NULL) {
Li Yangfaf0b2e2007-10-16 20:58:38 +0800354 VPRINTK("setting indirect prde\n");
355 prd_ptr_to_indirect_ext = prd;
356 prd->dba = cpu_to_le32(indirect_ext_segment_paddr);
357 indirect_ext_segment_sz = 0;
358 ++prd;
359 ++num_prde;
360 }
361
362 ttl_dwords += sg_len;
363 prd->dba = cpu_to_le32(sg_addr);
Xulei2f957fc2011-01-19 17:07:29 +0800364 prd->ddc_and_ext = cpu_to_le32(data_snoop | (sg_len & ~0x03));
Li Yangfaf0b2e2007-10-16 20:58:38 +0800365
366 VPRINTK("sg_fill, ttl=%d, dba=0x%x, ddc=0x%x\n",
367 ttl_dwords, prd->dba, prd->ddc_and_ext);
368
369 ++num_prde;
370 ++prd;
371 if (prd_ptr_to_indirect_ext)
372 indirect_ext_segment_sz += sg_len;
373 }
374
375 if (prd_ptr_to_indirect_ext) {
376 /* set indirect extension flag along with indirect ext. size */
377 prd_ptr_to_indirect_ext->ddc_and_ext =
378 cpu_to_le32((EXT_INDIRECT_SEG_PRD_FLAG |
Xulei2f957fc2011-01-19 17:07:29 +0800379 data_snoop |
Li Yangfaf0b2e2007-10-16 20:58:38 +0800380 (indirect_ext_segment_sz & ~0x03)));
381 }
382
383 *ttl = ttl_dwords;
384 return num_prde;
385}
386
387static void sata_fsl_qc_prep(struct ata_queued_cmd *qc)
388{
389 struct ata_port *ap = qc->ap;
390 struct sata_fsl_port_priv *pp = ap->private_data;
391 struct sata_fsl_host_priv *host_priv = ap->host->private_data;
392 void __iomem *hcr_base = host_priv->hcr_base;
393 unsigned int tag = sata_fsl_tag(qc->tag, hcr_base);
394 struct command_desc *cd;
Dave Liud3587242009-05-14 09:47:07 -0500395 u32 desc_info = CMD_DESC_RES | CMD_DESC_SNOOP_ENABLE;
Li Yangfaf0b2e2007-10-16 20:58:38 +0800396 u32 num_prde = 0;
397 u32 ttl_dwords = 0;
398 dma_addr_t cd_paddr;
399
400 cd = (struct command_desc *)pp->cmdentry + tag;
401 cd_paddr = pp->cmdentry_paddr + tag * SATA_FSL_CMD_DESC_SIZE;
402
Ashish Kalra034d8e82008-05-20 00:19:45 -0500403 ata_tf_to_fis(&qc->tf, qc->dev->link->pmp, 1, (u8 *) &cd->cfis);
Li Yangfaf0b2e2007-10-16 20:58:38 +0800404
405 VPRINTK("Dumping cfis : 0x%x, 0x%x, 0x%x\n",
406 cd->cfis[0], cd->cfis[1], cd->cfis[2]);
407
408 if (qc->tf.protocol == ATA_PROT_NCQ) {
409 VPRINTK("FPDMA xfer,Sctor cnt[0:7],[8:15] = %d,%d\n",
410 cd->cfis[3], cd->cfis[11]);
411 }
412
413 /* setup "ACMD - atapi command" in cmd. desc. if this is ATAPI cmd */
Tejun Heo405e66b2007-11-27 19:28:53 +0900414 if (ata_is_atapi(qc->tf.protocol)) {
Li Yangfaf0b2e2007-10-16 20:58:38 +0800415 desc_info |= ATAPI_CMD;
416 memset((void *)&cd->acmd, 0, 32);
417 memcpy((void *)&cd->acmd, qc->cdb, qc->dev->cdb_len);
418 }
419
420 if (qc->flags & ATA_QCFLAG_DMAMAP)
421 num_prde = sata_fsl_fill_sg(qc, (void *)cd,
Xulei2f957fc2011-01-19 17:07:29 +0800422 &ttl_dwords, cd_paddr,
423 host_priv->data_snoop);
Li Yangfaf0b2e2007-10-16 20:58:38 +0800424
425 if (qc->tf.protocol == ATA_PROT_NCQ)
426 desc_info |= FPDMA_QUEUED_CMD;
427
428 sata_fsl_setup_cmd_hdr_entry(pp, tag, desc_info, ttl_dwords,
429 num_prde, 5);
430
431 VPRINTK("SATA FSL : xx_qc_prep, di = 0x%x, ttl = %d, num_prde = %d\n",
432 desc_info, ttl_dwords, num_prde);
433}
434
435static unsigned int sata_fsl_qc_issue(struct ata_queued_cmd *qc)
436{
437 struct ata_port *ap = qc->ap;
438 struct sata_fsl_host_priv *host_priv = ap->host->private_data;
439 void __iomem *hcr_base = host_priv->hcr_base;
440 unsigned int tag = sata_fsl_tag(qc->tag, hcr_base);
441
442 VPRINTK("xx_qc_issue called,CQ=0x%x,CA=0x%x,CE=0x%x,CC=0x%x\n",
443 ioread32(CQ + hcr_base),
444 ioread32(CA + hcr_base),
445 ioread32(CE + hcr_base), ioread32(CC + hcr_base));
446
Ashish Kalra034d8e82008-05-20 00:19:45 -0500447 iowrite32(qc->dev->link->pmp, CQPMP + hcr_base);
448
Li Yangfaf0b2e2007-10-16 20:58:38 +0800449 /* Simply queue command to the controller/device */
450 iowrite32(1 << tag, CQ + hcr_base);
451
452 VPRINTK("xx_qc_issue called, tag=%d, CQ=0x%x, CA=0x%x\n",
453 tag, ioread32(CQ + hcr_base), ioread32(CA + hcr_base));
454
455 VPRINTK("CE=0x%x, DE=0x%x, CC=0x%x, CmdStat = 0x%x\n",
456 ioread32(CE + hcr_base),
457 ioread32(DE + hcr_base),
Anton Vorontsovb1f5dc42008-02-22 19:54:25 +0300458 ioread32(CC + hcr_base),
459 ioread32(COMMANDSTAT + host_priv->csr_base));
Li Yangfaf0b2e2007-10-16 20:58:38 +0800460
461 return 0;
462}
463
Tejun Heo4c9bf4e2008-04-07 22:47:20 +0900464static bool sata_fsl_qc_fill_rtf(struct ata_queued_cmd *qc)
465{
466 struct sata_fsl_port_priv *pp = qc->ap->private_data;
467 struct sata_fsl_host_priv *host_priv = qc->ap->host->private_data;
468 void __iomem *hcr_base = host_priv->hcr_base;
469 unsigned int tag = sata_fsl_tag(qc->tag, hcr_base);
470 struct command_desc *cd;
471
472 cd = pp->cmdentry + tag;
473
474 ata_tf_from_fis(cd->sfis, &qc->result_tf);
475 return true;
476}
477
Tejun Heo82ef04f2008-07-31 17:02:40 +0900478static int sata_fsl_scr_write(struct ata_link *link,
479 unsigned int sc_reg_in, u32 val)
Li Yangfaf0b2e2007-10-16 20:58:38 +0800480{
Tejun Heo82ef04f2008-07-31 17:02:40 +0900481 struct sata_fsl_host_priv *host_priv = link->ap->host->private_data;
Li Yangfaf0b2e2007-10-16 20:58:38 +0800482 void __iomem *ssr_base = host_priv->ssr_base;
483 unsigned int sc_reg;
484
485 switch (sc_reg_in) {
486 case SCR_STATUS:
Li Yangfaf0b2e2007-10-16 20:58:38 +0800487 case SCR_ERROR:
Li Yangfaf0b2e2007-10-16 20:58:38 +0800488 case SCR_CONTROL:
Li Yangfaf0b2e2007-10-16 20:58:38 +0800489 case SCR_ACTIVE:
Jeff Garzik9465d532007-10-31 19:27:57 +0800490 sc_reg = sc_reg_in;
Li Yangfaf0b2e2007-10-16 20:58:38 +0800491 break;
492 default:
493 return -EINVAL;
494 }
495
496 VPRINTK("xx_scr_write, reg_in = %d\n", sc_reg);
497
Jeff Garzik2a52e8d2007-10-31 19:27:58 +0800498 iowrite32(val, ssr_base + (sc_reg * 4));
Li Yangfaf0b2e2007-10-16 20:58:38 +0800499 return 0;
500}
501
Tejun Heo82ef04f2008-07-31 17:02:40 +0900502static int sata_fsl_scr_read(struct ata_link *link,
503 unsigned int sc_reg_in, u32 *val)
Li Yangfaf0b2e2007-10-16 20:58:38 +0800504{
Tejun Heo82ef04f2008-07-31 17:02:40 +0900505 struct sata_fsl_host_priv *host_priv = link->ap->host->private_data;
Li Yangfaf0b2e2007-10-16 20:58:38 +0800506 void __iomem *ssr_base = host_priv->ssr_base;
507 unsigned int sc_reg;
508
509 switch (sc_reg_in) {
510 case SCR_STATUS:
Li Yangfaf0b2e2007-10-16 20:58:38 +0800511 case SCR_ERROR:
Li Yangfaf0b2e2007-10-16 20:58:38 +0800512 case SCR_CONTROL:
Li Yangfaf0b2e2007-10-16 20:58:38 +0800513 case SCR_ACTIVE:
Jeff Garzik9465d532007-10-31 19:27:57 +0800514 sc_reg = sc_reg_in;
Li Yangfaf0b2e2007-10-16 20:58:38 +0800515 break;
516 default:
517 return -EINVAL;
518 }
519
520 VPRINTK("xx_scr_read, reg_in = %d\n", sc_reg);
521
Jeff Garzik2a52e8d2007-10-31 19:27:58 +0800522 *val = ioread32(ssr_base + (sc_reg * 4));
Li Yangfaf0b2e2007-10-16 20:58:38 +0800523 return 0;
524}
525
526static void sata_fsl_freeze(struct ata_port *ap)
527{
528 struct sata_fsl_host_priv *host_priv = ap->host->private_data;
529 void __iomem *hcr_base = host_priv->hcr_base;
530 u32 temp;
531
532 VPRINTK("xx_freeze, CQ=0x%x, CA=0x%x, CE=0x%x, DE=0x%x\n",
533 ioread32(CQ + hcr_base),
534 ioread32(CA + hcr_base),
535 ioread32(CE + hcr_base), ioread32(DE + hcr_base));
Anton Vorontsovb1f5dc42008-02-22 19:54:25 +0300536 VPRINTK("CmdStat = 0x%x\n",
537 ioread32(host_priv->csr_base + COMMANDSTAT));
Li Yangfaf0b2e2007-10-16 20:58:38 +0800538
539 /* disable interrupts on the controller/port */
540 temp = ioread32(hcr_base + HCONTROL);
541 iowrite32((temp & ~0x3F), hcr_base + HCONTROL);
542
543 VPRINTK("in xx_freeze : HControl = 0x%x, HStatus = 0x%x\n",
544 ioread32(hcr_base + HCONTROL), ioread32(hcr_base + HSTATUS));
545}
546
547static void sata_fsl_thaw(struct ata_port *ap)
548{
549 struct sata_fsl_host_priv *host_priv = ap->host->private_data;
550 void __iomem *hcr_base = host_priv->hcr_base;
551 u32 temp;
552
553 /* ack. any pending IRQs for this controller/port */
554 temp = ioread32(hcr_base + HSTATUS);
555
556 VPRINTK("xx_thaw, pending IRQs = 0x%x\n", (temp & 0x3F));
557
558 if (temp & 0x3F)
559 iowrite32((temp & 0x3F), hcr_base + HSTATUS);
560
561 /* enable interrupts on the controller/port */
562 temp = ioread32(hcr_base + HCONTROL);
563 iowrite32((temp | DEFAULT_PORT_IRQ_ENABLE_MASK), hcr_base + HCONTROL);
564
565 VPRINTK("xx_thaw : HControl = 0x%x, HStatus = 0x%x\n",
566 ioread32(hcr_base + HCONTROL), ioread32(hcr_base + HSTATUS));
567}
568
Ashish Kalra034d8e82008-05-20 00:19:45 -0500569static void sata_fsl_pmp_attach(struct ata_port *ap)
570{
571 struct sata_fsl_host_priv *host_priv = ap->host->private_data;
572 void __iomem *hcr_base = host_priv->hcr_base;
573 u32 temp;
574
575 temp = ioread32(hcr_base + HCONTROL);
576 iowrite32((temp | HCONTROL_PMP_ATTACHED), hcr_base + HCONTROL);
577}
578
579static void sata_fsl_pmp_detach(struct ata_port *ap)
580{
581 struct sata_fsl_host_priv *host_priv = ap->host->private_data;
582 void __iomem *hcr_base = host_priv->hcr_base;
583 u32 temp;
584
585 temp = ioread32(hcr_base + HCONTROL);
586 temp &= ~HCONTROL_PMP_ATTACHED;
587 iowrite32(temp, hcr_base + HCONTROL);
588
589 /* enable interrupts on the controller/port */
590 temp = ioread32(hcr_base + HCONTROL);
591 iowrite32((temp | DEFAULT_PORT_IRQ_ENABLE_MASK), hcr_base + HCONTROL);
592
593}
594
Li Yangfaf0b2e2007-10-16 20:58:38 +0800595static int sata_fsl_port_start(struct ata_port *ap)
596{
597 struct device *dev = ap->host->dev;
598 struct sata_fsl_port_priv *pp;
Li Yangfaf0b2e2007-10-16 20:58:38 +0800599 void *mem;
600 dma_addr_t mem_dma;
601 struct sata_fsl_host_priv *host_priv = ap->host->private_data;
602 void __iomem *hcr_base = host_priv->hcr_base;
603 u32 temp;
604
605 pp = kzalloc(sizeof(*pp), GFP_KERNEL);
606 if (!pp)
607 return -ENOMEM;
608
Li Yangfaf0b2e2007-10-16 20:58:38 +0800609 mem = dma_alloc_coherent(dev, SATA_FSL_PORT_PRIV_DMA_SZ, &mem_dma,
610 GFP_KERNEL);
611 if (!mem) {
Li Yangfaf0b2e2007-10-16 20:58:38 +0800612 kfree(pp);
613 return -ENOMEM;
614 }
615 memset(mem, 0, SATA_FSL_PORT_PRIV_DMA_SZ);
616
617 pp->cmdslot = mem;
618 pp->cmdslot_paddr = mem_dma;
619
620 mem += SATA_FSL_CMD_SLOT_SIZE;
621 mem_dma += SATA_FSL_CMD_SLOT_SIZE;
622
623 pp->cmdentry = mem;
624 pp->cmdentry_paddr = mem_dma;
625
626 ap->private_data = pp;
627
628 VPRINTK("CHBA = 0x%x, cmdentry_phys = 0x%x\n",
629 pp->cmdslot_paddr, pp->cmdentry_paddr);
630
631 /* Now, update the CHBA register in host controller cmd register set */
632 iowrite32(pp->cmdslot_paddr & 0xffffffff, hcr_base + CHBA);
633
634 /*
635 * Now, we can bring the controller on-line & also initiate
636 * the COMINIT sequence, we simply return here and the boot-probing
637 * & device discovery process is re-initiated by libATA using a
638 * Softreset EH (dummy) session. Hence, boot probing and device
639 * discovey will be part of sata_fsl_softreset() callback.
640 */
641
642 temp = ioread32(hcr_base + HCONTROL);
643 iowrite32((temp | HCONTROL_ONLINE_PHY_RST), hcr_base + HCONTROL);
644
645 VPRINTK("HStatus = 0x%x\n", ioread32(hcr_base + HSTATUS));
646 VPRINTK("HControl = 0x%x\n", ioread32(hcr_base + HCONTROL));
647 VPRINTK("CHBA = 0x%x\n", ioread32(hcr_base + CHBA));
648
ashish kalrae7eac962007-10-31 19:28:02 +0800649#ifdef CONFIG_MPC8315_DS
Li Yangfaf0b2e2007-10-16 20:58:38 +0800650 /*
651 * Workaround for 8315DS board 3gbps link-up issue,
652 * currently limit SATA port to GEN1 speed
653 */
Tejun Heo82ef04f2008-07-31 17:02:40 +0900654 sata_fsl_scr_read(&ap->link, SCR_CONTROL, &temp);
Li Yangfaf0b2e2007-10-16 20:58:38 +0800655 temp &= ~(0xF << 4);
656 temp |= (0x1 << 4);
Tejun Heo82ef04f2008-07-31 17:02:40 +0900657 sata_fsl_scr_write(&ap->link, SCR_CONTROL, temp);
Li Yangfaf0b2e2007-10-16 20:58:38 +0800658
Tejun Heo82ef04f2008-07-31 17:02:40 +0900659 sata_fsl_scr_read(&ap->link, SCR_CONTROL, &temp);
Li Yangfaf0b2e2007-10-16 20:58:38 +0800660 dev_printk(KERN_WARNING, dev, "scr_control, speed limited to %x\n",
661 temp);
ashish kalrae7eac962007-10-31 19:28:02 +0800662#endif
Li Yangfaf0b2e2007-10-16 20:58:38 +0800663
664 return 0;
665}
666
667static void sata_fsl_port_stop(struct ata_port *ap)
668{
669 struct device *dev = ap->host->dev;
670 struct sata_fsl_port_priv *pp = ap->private_data;
671 struct sata_fsl_host_priv *host_priv = ap->host->private_data;
672 void __iomem *hcr_base = host_priv->hcr_base;
673 u32 temp;
674
675 /*
676 * Force host controller to go off-line, aborting current operations
677 */
678 temp = ioread32(hcr_base + HCONTROL);
679 temp &= ~HCONTROL_ONLINE_PHY_RST;
680 temp |= HCONTROL_FORCE_OFFLINE;
681 iowrite32(temp, hcr_base + HCONTROL);
682
683 /* Poll for controller to go offline - should happen immediately */
Tejun Heo97750ce2010-09-06 17:56:29 +0200684 ata_wait_register(ap, hcr_base + HSTATUS, ONLINE, ONLINE, 1, 1);
Li Yangfaf0b2e2007-10-16 20:58:38 +0800685
686 ap->private_data = NULL;
687 dma_free_coherent(dev, SATA_FSL_PORT_PRIV_DMA_SZ,
688 pp->cmdslot, pp->cmdslot_paddr);
689
Li Yangfaf0b2e2007-10-16 20:58:38 +0800690 kfree(pp);
691}
692
693static unsigned int sata_fsl_dev_classify(struct ata_port *ap)
694{
695 struct sata_fsl_host_priv *host_priv = ap->host->private_data;
696 void __iomem *hcr_base = host_priv->hcr_base;
697 struct ata_taskfile tf;
698 u32 temp;
699
700 temp = ioread32(hcr_base + SIGNATURE);
701
702 VPRINTK("raw sig = 0x%x\n", temp);
703 VPRINTK("HStatus = 0x%x\n", ioread32(hcr_base + HSTATUS));
704 VPRINTK("HControl = 0x%x\n", ioread32(hcr_base + HCONTROL));
705
706 tf.lbah = (temp >> 24) & 0xff;
707 tf.lbam = (temp >> 16) & 0xff;
708 tf.lbal = (temp >> 8) & 0xff;
709 tf.nsect = temp & 0xff;
710
711 return ata_dev_classify(&tf);
712}
713
Jiang Yutanga0a74d12009-10-16 20:44:36 +0400714static int sata_fsl_hardreset(struct ata_link *link, unsigned int *class,
Ashish Kalra034d8e82008-05-20 00:19:45 -0500715 unsigned long deadline)
Li Yangfaf0b2e2007-10-16 20:58:38 +0800716{
Li Yang1bf617b2007-10-31 19:27:53 +0800717 struct ata_port *ap = link->ap;
Li Yangfaf0b2e2007-10-16 20:58:38 +0800718 struct sata_fsl_host_priv *host_priv = ap->host->private_data;
719 void __iomem *hcr_base = host_priv->hcr_base;
720 u32 temp;
Li Yangfaf0b2e2007-10-16 20:58:38 +0800721 int i = 0;
Li Yangfaf0b2e2007-10-16 20:58:38 +0800722 unsigned long start_jiffies;
723
Jiang Yutanga0a74d12009-10-16 20:44:36 +0400724 DPRINTK("in xx_hardreset\n");
Ashish Kalra034d8e82008-05-20 00:19:45 -0500725
Li Yangfaf0b2e2007-10-16 20:58:38 +0800726try_offline_again:
727 /*
728 * Force host controller to go off-line, aborting current operations
729 */
730 temp = ioread32(hcr_base + HCONTROL);
731 temp &= ~HCONTROL_ONLINE_PHY_RST;
732 iowrite32(temp, hcr_base + HCONTROL);
733
734 /* Poll for controller to go offline */
Tejun Heo97750ce2010-09-06 17:56:29 +0200735 temp = ata_wait_register(ap, hcr_base + HSTATUS, ONLINE, ONLINE,
736 1, 500);
Li Yangfaf0b2e2007-10-16 20:58:38 +0800737
738 if (temp & ONLINE) {
739 ata_port_printk(ap, KERN_ERR,
Jiang Yutanga0a74d12009-10-16 20:44:36 +0400740 "Hardreset failed, not off-lined %d\n", i);
Li Yangfaf0b2e2007-10-16 20:58:38 +0800741
742 /*
743 * Try to offline controller atleast twice
744 */
745 i++;
746 if (i == 2)
747 goto err;
748 else
749 goto try_offline_again;
750 }
751
Jiang Yutanga0a74d12009-10-16 20:44:36 +0400752 DPRINTK("hardreset, controller off-lined\n");
Li Yangfaf0b2e2007-10-16 20:58:38 +0800753 VPRINTK("HStatus = 0x%x\n", ioread32(hcr_base + HSTATUS));
754 VPRINTK("HControl = 0x%x\n", ioread32(hcr_base + HCONTROL));
755
756 /*
757 * PHY reset should remain asserted for atleast 1ms
758 */
Tejun Heo97750ce2010-09-06 17:56:29 +0200759 ata_msleep(ap, 1);
Li Yangfaf0b2e2007-10-16 20:58:38 +0800760
761 /*
762 * Now, bring the host controller online again, this can take time
763 * as PHY reset and communication establishment, 1st D2H FIS and
764 * device signature update is done, on safe side assume 500ms
765 * NOTE : Host online status may be indicated immediately!!
766 */
767
768 temp = ioread32(hcr_base + HCONTROL);
769 temp |= (HCONTROL_ONLINE_PHY_RST | HCONTROL_SNOOP_ENABLE);
Ashish Kalra034d8e82008-05-20 00:19:45 -0500770 temp |= HCONTROL_PMP_ATTACHED;
Li Yangfaf0b2e2007-10-16 20:58:38 +0800771 iowrite32(temp, hcr_base + HCONTROL);
772
Tejun Heo97750ce2010-09-06 17:56:29 +0200773 temp = ata_wait_register(ap, hcr_base + HSTATUS, ONLINE, 0, 1, 500);
Li Yangfaf0b2e2007-10-16 20:58:38 +0800774
775 if (!(temp & ONLINE)) {
776 ata_port_printk(ap, KERN_ERR,
Jiang Yutanga0a74d12009-10-16 20:44:36 +0400777 "Hardreset failed, not on-lined\n");
Li Yangfaf0b2e2007-10-16 20:58:38 +0800778 goto err;
779 }
780
Jiang Yutanga0a74d12009-10-16 20:44:36 +0400781 DPRINTK("hardreset, controller off-lined & on-lined\n");
Li Yangfaf0b2e2007-10-16 20:58:38 +0800782 VPRINTK("HStatus = 0x%x\n", ioread32(hcr_base + HSTATUS));
783 VPRINTK("HControl = 0x%x\n", ioread32(hcr_base + HCONTROL));
784
785 /*
786 * First, wait for the PHYRDY change to occur before waiting for
787 * the signature, and also verify if SStatus indicates device
788 * presence
789 */
790
Tejun Heo97750ce2010-09-06 17:56:29 +0200791 temp = ata_wait_register(ap, hcr_base + HSTATUS, 0xFF, 0, 1, 500);
Li Yang1bf617b2007-10-31 19:27:53 +0800792 if ((!(temp & 0x10)) || ata_link_offline(link)) {
Li Yangfaf0b2e2007-10-16 20:58:38 +0800793 ata_port_printk(ap, KERN_WARNING,
794 "No Device OR PHYRDY change,Hstatus = 0x%x\n",
795 ioread32(hcr_base + HSTATUS));
Ashish Kalra034d8e82008-05-20 00:19:45 -0500796 *class = ATA_DEV_NONE;
Jiang Yutanga0a74d12009-10-16 20:44:36 +0400797 return 0;
Li Yangfaf0b2e2007-10-16 20:58:38 +0800798 }
799
800 /*
801 * Wait for the first D2H from device,i.e,signature update notification
802 */
803 start_jiffies = jiffies;
Tejun Heo97750ce2010-09-06 17:56:29 +0200804 temp = ata_wait_register(ap, hcr_base + HSTATUS, 0xFF, 0x10,
Li Yangfaf0b2e2007-10-16 20:58:38 +0800805 500, jiffies_to_msecs(deadline - start_jiffies));
806
807 if ((temp & 0xFF) != 0x18) {
808 ata_port_printk(ap, KERN_WARNING, "No Signature Update\n");
Ashish Kalra034d8e82008-05-20 00:19:45 -0500809 *class = ATA_DEV_NONE;
Jiang Yutanga0a74d12009-10-16 20:44:36 +0400810 goto do_followup_srst;
Li Yangfaf0b2e2007-10-16 20:58:38 +0800811 } else {
812 ata_port_printk(ap, KERN_INFO,
813 "Signature Update detected @ %d msecs\n",
814 jiffies_to_msecs(jiffies - start_jiffies));
Jiang Yutanga0a74d12009-10-16 20:44:36 +0400815 *class = sata_fsl_dev_classify(ap);
816 return 0;
817 }
818
819do_followup_srst:
820 /*
821 * request libATA to perform follow-up softreset
822 */
823 return -EAGAIN;
824
825err:
826 return -EIO;
827}
828
829static int sata_fsl_softreset(struct ata_link *link, unsigned int *class,
830 unsigned long deadline)
831{
832 struct ata_port *ap = link->ap;
833 struct sata_fsl_port_priv *pp = ap->private_data;
834 struct sata_fsl_host_priv *host_priv = ap->host->private_data;
835 void __iomem *hcr_base = host_priv->hcr_base;
836 int pmp = sata_srst_pmp(link);
837 u32 temp;
838 struct ata_taskfile tf;
839 u8 *cfis;
840 u32 Serror;
841
842 DPRINTK("in xx_softreset\n");
843
844 if (ata_link_offline(link)) {
845 DPRINTK("PHY reports no device\n");
846 *class = ATA_DEV_NONE;
847 return 0;
Li Yangfaf0b2e2007-10-16 20:58:38 +0800848 }
849
850 /*
851 * Send a device reset (SRST) explicitly on command slot #0
852 * Check : will the command queue (reg) be cleared during offlining ??
853 * Also we will be online only if Phy commn. has been established
854 * and device presence has been detected, therefore if we have
855 * reached here, we can send a command to the target device
856 */
857
Li Yangfaf0b2e2007-10-16 20:58:38 +0800858 DPRINTK("Sending SRST/device reset\n");
859
Li Yang1bf617b2007-10-31 19:27:53 +0800860 ata_tf_init(link->device, &tf);
Li Yang520d3a12007-10-31 19:28:01 +0800861 cfis = (u8 *) &pp->cmdentry->cfis;
Li Yangfaf0b2e2007-10-16 20:58:38 +0800862
863 /* device reset/SRST is a control register update FIS, uses tag0 */
864 sata_fsl_setup_cmd_hdr_entry(pp, 0,
Dave Liud3587242009-05-14 09:47:07 -0500865 SRST_CMD | CMD_DESC_RES | CMD_DESC_SNOOP_ENABLE, 0, 0, 5);
Li Yangfaf0b2e2007-10-16 20:58:38 +0800866
867 tf.ctl |= ATA_SRST; /* setup SRST bit in taskfile control reg */
Ashish Kalra034d8e82008-05-20 00:19:45 -0500868 ata_tf_to_fis(&tf, pmp, 0, cfis);
Li Yangfaf0b2e2007-10-16 20:58:38 +0800869
870 DPRINTK("Dumping cfis : 0x%x, 0x%x, 0x%x, 0x%x\n",
871 cfis[0], cfis[1], cfis[2], cfis[3]);
872
873 /*
874 * Queue SRST command to the controller/device, ensure that no
875 * other commands are active on the controller/device
876 */
877
878 DPRINTK("@Softreset, CQ = 0x%x, CA = 0x%x, CC = 0x%x\n",
879 ioread32(CQ + hcr_base),
880 ioread32(CA + hcr_base), ioread32(CC + hcr_base));
881
882 iowrite32(0xFFFF, CC + hcr_base);
Jiang Yutanga0a74d12009-10-16 20:44:36 +0400883 if (pmp != SATA_PMP_CTRL_PORT)
884 iowrite32(pmp, CQPMP + hcr_base);
Li Yangfaf0b2e2007-10-16 20:58:38 +0800885 iowrite32(1, CQ + hcr_base);
886
Tejun Heo97750ce2010-09-06 17:56:29 +0200887 temp = ata_wait_register(ap, CQ + hcr_base, 0x1, 0x1, 1, 5000);
Li Yangfaf0b2e2007-10-16 20:58:38 +0800888 if (temp & 0x1) {
889 ata_port_printk(ap, KERN_WARNING, "ATA_SRST issue failed\n");
890
891 DPRINTK("Softreset@5000,CQ=0x%x,CA=0x%x,CC=0x%x\n",
892 ioread32(CQ + hcr_base),
893 ioread32(CA + hcr_base), ioread32(CC + hcr_base));
894
Tejun Heo82ef04f2008-07-31 17:02:40 +0900895 sata_fsl_scr_read(&ap->link, SCR_ERROR, &Serror);
Li Yangfaf0b2e2007-10-16 20:58:38 +0800896
897 DPRINTK("HStatus = 0x%x\n", ioread32(hcr_base + HSTATUS));
898 DPRINTK("HControl = 0x%x\n", ioread32(hcr_base + HCONTROL));
899 DPRINTK("Serror = 0x%x\n", Serror);
900 goto err;
901 }
902
Tejun Heo97750ce2010-09-06 17:56:29 +0200903 ata_msleep(ap, 1);
Li Yangfaf0b2e2007-10-16 20:58:38 +0800904
905 /*
906 * SATA device enters reset state after receving a Control register
907 * FIS with SRST bit asserted and it awaits another H2D Control reg.
908 * FIS with SRST bit cleared, then the device does internal diags &
909 * initialization, followed by indicating it's initialization status
910 * using ATA signature D2H register FIS to the host controller.
911 */
912
Dave Liud3587242009-05-14 09:47:07 -0500913 sata_fsl_setup_cmd_hdr_entry(pp, 0, CMD_DESC_RES | CMD_DESC_SNOOP_ENABLE,
914 0, 0, 5);
Li Yangfaf0b2e2007-10-16 20:58:38 +0800915
916 tf.ctl &= ~ATA_SRST; /* 2nd H2D Ctl. register FIS */
Ashish Kalra034d8e82008-05-20 00:19:45 -0500917 ata_tf_to_fis(&tf, pmp, 0, cfis);
Li Yangfaf0b2e2007-10-16 20:58:38 +0800918
Ashish Kalra034d8e82008-05-20 00:19:45 -0500919 if (pmp != SATA_PMP_CTRL_PORT)
920 iowrite32(pmp, CQPMP + hcr_base);
Li Yangfaf0b2e2007-10-16 20:58:38 +0800921 iowrite32(1, CQ + hcr_base);
Tejun Heo97750ce2010-09-06 17:56:29 +0200922 ata_msleep(ap, 150); /* ?? */
Li Yangfaf0b2e2007-10-16 20:58:38 +0800923
924 /*
925 * The above command would have signalled an interrupt on command
926 * complete, which needs special handling, by clearing the Nth
927 * command bit of the CCreg
928 */
929 iowrite32(0x01, CC + hcr_base); /* We know it will be cmd#0 always */
Li Yangfaf0b2e2007-10-16 20:58:38 +0800930
931 DPRINTK("SATA FSL : Now checking device signature\n");
932
933 *class = ATA_DEV_NONE;
934
935 /* Verify if SStatus indicates device presence */
Li Yang1bf617b2007-10-31 19:27:53 +0800936 if (ata_link_online(link)) {
Li Yangfaf0b2e2007-10-16 20:58:38 +0800937 /*
938 * if we are here, device presence has been detected,
939 * 1st D2H FIS would have been received, but sfis in
940 * command desc. is not updated, but signature register
941 * would have been updated
942 */
943
944 *class = sata_fsl_dev_classify(ap);
945
946 DPRINTK("class = %d\n", *class);
947 VPRINTK("ccreg = 0x%x\n", ioread32(hcr_base + CC));
948 VPRINTK("cereg = 0x%x\n", ioread32(hcr_base + CE));
949 }
950
951 return 0;
952
953err:
954 return -EIO;
955}
956
Ashish Kalra034d8e82008-05-20 00:19:45 -0500957static void sata_fsl_error_handler(struct ata_port *ap)
958{
959
960 DPRINTK("in xx_error_handler\n");
961 sata_pmp_error_handler(ap);
962
963}
964
Li Yangfaf0b2e2007-10-16 20:58:38 +0800965static void sata_fsl_post_internal_cmd(struct ata_queued_cmd *qc)
966{
967 if (qc->flags & ATA_QCFLAG_FAILED)
968 qc->err_mask |= AC_ERR_OTHER;
969
970 if (qc->err_mask) {
971 /* make DMA engine forget about the failed command */
972
973 }
974}
975
Li Yangfaf0b2e2007-10-16 20:58:38 +0800976static void sata_fsl_error_intr(struct ata_port *ap)
977{
Li Yangfaf0b2e2007-10-16 20:58:38 +0800978 struct sata_fsl_host_priv *host_priv = ap->host->private_data;
979 void __iomem *hcr_base = host_priv->hcr_base;
Ashish Kalra034d8e82008-05-20 00:19:45 -0500980 u32 hstatus, dereg=0, cereg = 0, SError = 0;
Li Yangfaf0b2e2007-10-16 20:58:38 +0800981 unsigned int err_mask = 0, action = 0;
Ashish Kalra034d8e82008-05-20 00:19:45 -0500982 int freeze = 0, abort=0;
983 struct ata_link *link = NULL;
984 struct ata_queued_cmd *qc = NULL;
985 struct ata_eh_info *ehi;
Li Yangfaf0b2e2007-10-16 20:58:38 +0800986
987 hstatus = ioread32(hcr_base + HSTATUS);
988 cereg = ioread32(hcr_base + CE);
989
Ashish Kalra034d8e82008-05-20 00:19:45 -0500990 /* first, analyze and record host port events */
991 link = &ap->link;
992 ehi = &link->eh_info;
Li Yangfaf0b2e2007-10-16 20:58:38 +0800993 ata_ehi_clear_desc(ehi);
994
995 /*
996 * Handle & Clear SError
997 */
998
Tejun Heo82ef04f2008-07-31 17:02:40 +0900999 sata_fsl_scr_read(&ap->link, SCR_ERROR, &SError);
ashish kalrafd6c29e2009-07-01 20:59:43 +05301000 if (unlikely(SError & 0xFFFF0000))
Tejun Heo82ef04f2008-07-31 17:02:40 +09001001 sata_fsl_scr_write(&ap->link, SCR_ERROR, SError);
Li Yangfaf0b2e2007-10-16 20:58:38 +08001002
1003 DPRINTK("error_intr,hStat=0x%x,CE=0x%x,DE =0x%x,SErr=0x%x\n",
1004 hstatus, cereg, ioread32(hcr_base + DE), SError);
1005
Ashish Kalra034d8e82008-05-20 00:19:45 -05001006 /* handle fatal errors */
1007 if (hstatus & FATAL_ERROR_DECODE) {
1008 ehi->err_mask |= AC_ERR_ATA_BUS;
1009 ehi->action |= ATA_EH_SOFTRESET;
1010
Ashish Kalra034d8e82008-05-20 00:19:45 -05001011 freeze = 1;
1012 }
1013
ashish kalrafd6c29e2009-07-01 20:59:43 +05301014 /* Handle SDB FIS receive & notify update */
1015 if (hstatus & INT_ON_SNOTIFY_UPDATE)
1016 sata_async_notification(ap);
1017
Ashish Kalra034d8e82008-05-20 00:19:45 -05001018 /* Handle PHYRDY change notification */
1019 if (hstatus & INT_ON_PHYRDY_CHG) {
1020 DPRINTK("SATA FSL: PHYRDY change indication\n");
1021
1022 /* Setup a soft-reset EH action */
1023 ata_ehi_hotplugged(ehi);
1024 ata_ehi_push_desc(ehi, "%s", "PHY RDY changed");
1025 freeze = 1;
1026 }
1027
Li Yangfaf0b2e2007-10-16 20:58:38 +08001028 /* handle single device errors */
1029 if (cereg) {
1030 /*
1031 * clear the command error, also clears queue to the device
1032 * in error, and we can (re)issue commands to this device.
1033 * When a device is in error all commands queued into the
1034 * host controller and at the device are considered aborted
1035 * and the queue for that device is stopped. Now, after
1036 * clearing the device error, we can issue commands to the
1037 * device to interrogate it to find the source of the error.
1038 */
Ashish Kalra034d8e82008-05-20 00:19:45 -05001039 abort = 1;
Li Yangfaf0b2e2007-10-16 20:58:38 +08001040
1041 DPRINTK("single device error, CE=0x%x, DE=0x%x\n",
1042 ioread32(hcr_base + CE), ioread32(hcr_base + DE));
Li Yangfaf0b2e2007-10-16 20:58:38 +08001043
Ashish Kalra034d8e82008-05-20 00:19:45 -05001044 /* find out the offending link and qc */
1045 if (ap->nr_pmp_links) {
1046 dereg = ioread32(hcr_base + DE);
1047 iowrite32(dereg, hcr_base + DE);
1048 iowrite32(cereg, hcr_base + CE);
Li Yangfaf0b2e2007-10-16 20:58:38 +08001049
Ashish Kalra034d8e82008-05-20 00:19:45 -05001050 if (dereg < ap->nr_pmp_links) {
1051 link = &ap->pmp_link[dereg];
1052 ehi = &link->eh_info;
1053 qc = ata_qc_from_tag(ap, link->active_tag);
1054 /*
1055 * We should consider this as non fatal error,
1056 * and TF must be updated as done below.
1057 */
Li Yangfaf0b2e2007-10-16 20:58:38 +08001058
Ashish Kalra034d8e82008-05-20 00:19:45 -05001059 err_mask |= AC_ERR_DEV;
Li Yangfaf0b2e2007-10-16 20:58:38 +08001060
Ashish Kalra034d8e82008-05-20 00:19:45 -05001061 } else {
1062 err_mask |= AC_ERR_HSM;
1063 action |= ATA_EH_HARDRESET;
1064 freeze = 1;
1065 }
1066 } else {
1067 dereg = ioread32(hcr_base + DE);
1068 iowrite32(dereg, hcr_base + DE);
1069 iowrite32(cereg, hcr_base + CE);
1070
1071 qc = ata_qc_from_tag(ap, link->active_tag);
1072 /*
1073 * We should consider this as non fatal error,
1074 * and TF must be updated as done below.
1075 */
1076 err_mask |= AC_ERR_DEV;
1077 }
Li Yangfaf0b2e2007-10-16 20:58:38 +08001078 }
1079
1080 /* record error info */
ashish kalrafd6c29e2009-07-01 20:59:43 +05301081 if (qc)
Li Yangfaf0b2e2007-10-16 20:58:38 +08001082 qc->err_mask |= err_mask;
ashish kalrafd6c29e2009-07-01 20:59:43 +05301083 else
Li Yangfaf0b2e2007-10-16 20:58:38 +08001084 ehi->err_mask |= err_mask;
1085
1086 ehi->action |= action;
Li Yangfaf0b2e2007-10-16 20:58:38 +08001087
1088 /* freeze or abort */
1089 if (freeze)
1090 ata_port_freeze(ap);
Ashish Kalra034d8e82008-05-20 00:19:45 -05001091 else if (abort) {
1092 if (qc)
1093 ata_link_abort(qc->dev->link);
1094 else
1095 ata_port_abort(ap);
1096 }
Li Yangfaf0b2e2007-10-16 20:58:38 +08001097}
1098
Li Yangfaf0b2e2007-10-16 20:58:38 +08001099static void sata_fsl_host_intr(struct ata_port *ap)
1100{
1101 struct sata_fsl_host_priv *host_priv = ap->host->private_data;
1102 void __iomem *hcr_base = host_priv->hcr_base;
Tejun Heo752e3862010-06-25 15:02:59 +02001103 u32 hstatus, done_mask = 0;
Li Yangfaf0b2e2007-10-16 20:58:38 +08001104 struct ata_queued_cmd *qc;
1105 u32 SError;
1106
1107 hstatus = ioread32(hcr_base + HSTATUS);
1108
Tejun Heo82ef04f2008-07-31 17:02:40 +09001109 sata_fsl_scr_read(&ap->link, SCR_ERROR, &SError);
Li Yangfaf0b2e2007-10-16 20:58:38 +08001110
1111 if (unlikely(SError & 0xFFFF0000)) {
1112 DPRINTK("serror @host_intr : 0x%x\n", SError);
1113 sata_fsl_error_intr(ap);
Li Yangfaf0b2e2007-10-16 20:58:38 +08001114 }
1115
1116 if (unlikely(hstatus & INT_ON_ERROR)) {
1117 DPRINTK("error interrupt!!\n");
1118 sata_fsl_error_intr(ap);
1119 return;
1120 }
1121
Ashish Kalra034d8e82008-05-20 00:19:45 -05001122 /* Read command completed register */
Tejun Heo752e3862010-06-25 15:02:59 +02001123 done_mask = ioread32(hcr_base + CC);
Ashish Kalra034d8e82008-05-20 00:19:45 -05001124
1125 VPRINTK("Status of all queues :\n");
Tejun Heo752e3862010-06-25 15:02:59 +02001126 VPRINTK("done_mask/CC = 0x%x, CA = 0x%x, CE=0x%x,CQ=0x%x,apqa=0x%x\n",
1127 done_mask,
Ashish Kalra034d8e82008-05-20 00:19:45 -05001128 ioread32(hcr_base + CA),
1129 ioread32(hcr_base + CE),
1130 ioread32(hcr_base + CQ),
1131 ap->qc_active);
1132
Tejun Heo752e3862010-06-25 15:02:59 +02001133 if (done_mask & ap->qc_active) {
Li Yangfaf0b2e2007-10-16 20:58:38 +08001134 int i;
Li Yangfaf0b2e2007-10-16 20:58:38 +08001135 /* clear CC bit, this will also complete the interrupt */
Tejun Heo752e3862010-06-25 15:02:59 +02001136 iowrite32(done_mask, hcr_base + CC);
Li Yangfaf0b2e2007-10-16 20:58:38 +08001137
1138 DPRINTK("Status of all queues :\n");
Tejun Heo752e3862010-06-25 15:02:59 +02001139 DPRINTK("done_mask/CC = 0x%x, CA = 0x%x, CE=0x%x\n",
1140 done_mask, ioread32(hcr_base + CA),
Li Yangfaf0b2e2007-10-16 20:58:38 +08001141 ioread32(hcr_base + CE));
1142
1143 for (i = 0; i < SATA_FSL_QUEUE_DEPTH; i++) {
Tejun Heo1aadf5c2010-06-25 15:03:34 +02001144 if (done_mask & (1 << i))
Li Yangfaf0b2e2007-10-16 20:58:38 +08001145 DPRINTK
1146 ("completing ncq cmd,tag=%d,CC=0x%x,CA=0x%x\n",
1147 i, ioread32(hcr_base + CC),
1148 ioread32(hcr_base + CA));
Li Yangfaf0b2e2007-10-16 20:58:38 +08001149 }
Tejun Heo1aadf5c2010-06-25 15:03:34 +02001150 ata_qc_complete_multiple(ap, ap->qc_active ^ done_mask);
Li Yangfaf0b2e2007-10-16 20:58:38 +08001151 return;
1152
Ashish Kalra034d8e82008-05-20 00:19:45 -05001153 } else if ((ap->qc_active & (1 << ATA_TAG_INTERNAL))) {
Li Yangfaf0b2e2007-10-16 20:58:38 +08001154 iowrite32(1, hcr_base + CC);
Ashish Kalra034d8e82008-05-20 00:19:45 -05001155 qc = ata_qc_from_tag(ap, ATA_TAG_INTERNAL);
Li Yangfaf0b2e2007-10-16 20:58:38 +08001156
Ashish Kalra034d8e82008-05-20 00:19:45 -05001157 DPRINTK("completing non-ncq cmd, CC=0x%x\n",
1158 ioread32(hcr_base + CC));
Li Yangfaf0b2e2007-10-16 20:58:38 +08001159
Ashish Kalra034d8e82008-05-20 00:19:45 -05001160 if (qc) {
Li Yangfaf0b2e2007-10-16 20:58:38 +08001161 ata_qc_complete(qc);
Ashish Kalra034d8e82008-05-20 00:19:45 -05001162 }
Li Yangfaf0b2e2007-10-16 20:58:38 +08001163 } else {
1164 /* Spurious Interrupt!! */
1165 DPRINTK("spurious interrupt!!, CC = 0x%x\n",
1166 ioread32(hcr_base + CC));
Tejun Heo752e3862010-06-25 15:02:59 +02001167 iowrite32(done_mask, hcr_base + CC);
Li Yangfaf0b2e2007-10-16 20:58:38 +08001168 return;
1169 }
1170}
1171
1172static irqreturn_t sata_fsl_interrupt(int irq, void *dev_instance)
1173{
1174 struct ata_host *host = dev_instance;
1175 struct sata_fsl_host_priv *host_priv = host->private_data;
1176 void __iomem *hcr_base = host_priv->hcr_base;
1177 u32 interrupt_enables;
1178 unsigned handled = 0;
1179 struct ata_port *ap;
1180
1181 /* ack. any pending IRQs for this controller/port */
1182 interrupt_enables = ioread32(hcr_base + HSTATUS);
1183 interrupt_enables &= 0x3F;
1184
1185 DPRINTK("interrupt status 0x%x\n", interrupt_enables);
1186
1187 if (!interrupt_enables)
1188 return IRQ_NONE;
1189
1190 spin_lock(&host->lock);
1191
1192 /* Assuming one port per host controller */
1193
1194 ap = host->ports[0];
1195 if (ap) {
1196 sata_fsl_host_intr(ap);
1197 } else {
1198 dev_printk(KERN_WARNING, host->dev,
1199 "interrupt on disabled port 0\n");
1200 }
1201
1202 iowrite32(interrupt_enables, hcr_base + HSTATUS);
1203 handled = 1;
1204
1205 spin_unlock(&host->lock);
1206
1207 return IRQ_RETVAL(handled);
1208}
1209
1210/*
1211 * Multiple ports are represented by multiple SATA controllers with
1212 * one port per controller
1213 */
1214static int sata_fsl_init_controller(struct ata_host *host)
1215{
1216 struct sata_fsl_host_priv *host_priv = host->private_data;
1217 void __iomem *hcr_base = host_priv->hcr_base;
1218 u32 temp;
1219
1220 /*
1221 * NOTE : We cannot bring the controller online before setting
1222 * the CHBA, hence main controller initialization is done as
1223 * part of the port_start() callback
1224 */
1225
1226 /* ack. any pending IRQs for this controller/port */
1227 temp = ioread32(hcr_base + HSTATUS);
1228 if (temp & 0x3F)
1229 iowrite32((temp & 0x3F), hcr_base + HSTATUS);
1230
1231 /* Keep interrupts disabled on the controller */
1232 temp = ioread32(hcr_base + HCONTROL);
1233 iowrite32((temp & ~0x3F), hcr_base + HCONTROL);
1234
1235 /* Disable interrupt coalescing control(icc), for the moment */
1236 DPRINTK("icc = 0x%x\n", ioread32(hcr_base + ICC));
1237 iowrite32(0x01000000, hcr_base + ICC);
1238
1239 /* clear error registers, SError is cleared by libATA */
1240 iowrite32(0x00000FFFF, hcr_base + CE);
1241 iowrite32(0x00000FFFF, hcr_base + DE);
1242
Li Yangfaf0b2e2007-10-16 20:58:38 +08001243 /*
1244 * host controller will be brought on-line, during xx_port_start()
1245 * callback, that should also initiate the OOB, COMINIT sequence
1246 */
1247
1248 DPRINTK("HStatus = 0x%x\n", ioread32(hcr_base + HSTATUS));
1249 DPRINTK("HControl = 0x%x\n", ioread32(hcr_base + HCONTROL));
1250
1251 return 0;
1252}
1253
1254/*
1255 * scsi mid-layer and libata interface structures
1256 */
1257static struct scsi_host_template sata_fsl_sht = {
Tejun Heo68d1d072008-03-25 12:22:49 +09001258 ATA_NCQ_SHT("sata_fsl"),
Li Yangfaf0b2e2007-10-16 20:58:38 +08001259 .can_queue = SATA_FSL_QUEUE_DEPTH,
Li Yangfaf0b2e2007-10-16 20:58:38 +08001260 .sg_tablesize = SATA_FSL_MAX_PRD_USABLE,
Li Yangfaf0b2e2007-10-16 20:58:38 +08001261 .dma_boundary = ATA_DMA_BOUNDARY,
Li Yangfaf0b2e2007-10-16 20:58:38 +08001262};
1263
Ashish Kalra034d8e82008-05-20 00:19:45 -05001264static struct ata_port_operations sata_fsl_ops = {
1265 .inherits = &sata_pmp_port_ops,
Tejun Heo029cfd62008-03-25 12:22:49 +09001266
Ashish Kalraf90f6132009-07-29 21:15:49 +05301267 .qc_defer = ata_std_qc_defer,
Li Yangfaf0b2e2007-10-16 20:58:38 +08001268 .qc_prep = sata_fsl_qc_prep,
1269 .qc_issue = sata_fsl_qc_issue,
Tejun Heo4c9bf4e2008-04-07 22:47:20 +09001270 .qc_fill_rtf = sata_fsl_qc_fill_rtf,
Li Yangfaf0b2e2007-10-16 20:58:38 +08001271
1272 .scr_read = sata_fsl_scr_read,
1273 .scr_write = sata_fsl_scr_write,
1274
1275 .freeze = sata_fsl_freeze,
1276 .thaw = sata_fsl_thaw,
Tejun Heoa1efdab2008-03-25 12:22:50 +09001277 .softreset = sata_fsl_softreset,
Jiang Yutanga0a74d12009-10-16 20:44:36 +04001278 .hardreset = sata_fsl_hardreset,
Ashish Kalra034d8e82008-05-20 00:19:45 -05001279 .pmp_softreset = sata_fsl_softreset,
1280 .error_handler = sata_fsl_error_handler,
Li Yangfaf0b2e2007-10-16 20:58:38 +08001281 .post_internal_cmd = sata_fsl_post_internal_cmd,
1282
1283 .port_start = sata_fsl_port_start,
1284 .port_stop = sata_fsl_port_stop,
Ashish Kalra034d8e82008-05-20 00:19:45 -05001285
1286 .pmp_attach = sata_fsl_pmp_attach,
1287 .pmp_detach = sata_fsl_pmp_detach,
Li Yangfaf0b2e2007-10-16 20:58:38 +08001288};
1289
1290static const struct ata_port_info sata_fsl_port_info[] = {
1291 {
1292 .flags = SATA_FSL_HOST_FLAGS,
Erik Inge Bolsø14bdef92009-03-14 21:38:24 +01001293 .pio_mask = ATA_PIO4,
1294 .udma_mask = ATA_UDMA6,
Li Yangfaf0b2e2007-10-16 20:58:38 +08001295 .port_ops = &sata_fsl_ops,
1296 },
1297};
1298
Grant Likely2dc11582010-08-06 09:25:50 -06001299static int sata_fsl_probe(struct platform_device *ofdev,
Li Yangfaf0b2e2007-10-16 20:58:38 +08001300 const struct of_device_id *match)
1301{
Michal Sojkae4ac5222009-01-14 14:02:38 +01001302 int retval = -ENXIO;
Li Yangfaf0b2e2007-10-16 20:58:38 +08001303 void __iomem *hcr_base = NULL;
1304 void __iomem *ssr_base = NULL;
1305 void __iomem *csr_base = NULL;
1306 struct sata_fsl_host_priv *host_priv = NULL;
Li Yangfaf0b2e2007-10-16 20:58:38 +08001307 int irq;
1308 struct ata_host *host;
1309
1310 struct ata_port_info pi = sata_fsl_port_info[0];
1311 const struct ata_port_info *ppi[] = { &pi, NULL };
1312
1313 dev_printk(KERN_INFO, &ofdev->dev,
1314 "Sata FSL Platform/CSB Driver init\n");
1315
Grant Likely61c7a082010-04-13 16:12:29 -07001316 hcr_base = of_iomap(ofdev->dev.of_node, 0);
Li Yangfaf0b2e2007-10-16 20:58:38 +08001317 if (!hcr_base)
1318 goto error_exit_with_cleanup;
1319
1320 ssr_base = hcr_base + 0x100;
1321 csr_base = hcr_base + 0x140;
1322
1323 DPRINTK("@reset i/o = 0x%x\n", ioread32(csr_base + TRANSCFG));
1324 DPRINTK("sizeof(cmd_desc) = %d\n", sizeof(struct command_desc));
1325 DPRINTK("sizeof(#define cmd_desc) = %d\n", SATA_FSL_CMD_DESC_SIZE);
1326
1327 host_priv = kzalloc(sizeof(struct sata_fsl_host_priv), GFP_KERNEL);
1328 if (!host_priv)
1329 goto error_exit_with_cleanup;
1330
1331 host_priv->hcr_base = hcr_base;
1332 host_priv->ssr_base = ssr_base;
1333 host_priv->csr_base = csr_base;
1334
Grant Likely61c7a082010-04-13 16:12:29 -07001335 irq = irq_of_parse_and_map(ofdev->dev.of_node, 0);
Li Yangfaf0b2e2007-10-16 20:58:38 +08001336 if (irq < 0) {
1337 dev_printk(KERN_ERR, &ofdev->dev, "invalid irq from platform\n");
1338 goto error_exit_with_cleanup;
1339 }
Li Yang79b3edc2007-10-31 19:27:55 +08001340 host_priv->irq = irq;
Li Yangfaf0b2e2007-10-16 20:58:38 +08001341
Xulei2f957fc2011-01-19 17:07:29 +08001342 if (of_device_is_compatible(ofdev->dev.of_node, "fsl,pq-sata-v2"))
1343 host_priv->data_snoop = DATA_SNOOP_ENABLE_V2;
1344 else
1345 host_priv->data_snoop = DATA_SNOOP_ENABLE_V1;
1346
Li Yangfaf0b2e2007-10-16 20:58:38 +08001347 /* allocate host structure */
1348 host = ata_host_alloc_pinfo(&ofdev->dev, ppi, SATA_FSL_MAX_PORTS);
1349
1350 /* host->iomap is not used currently */
1351 host->private_data = host_priv;
1352
Li Yangfaf0b2e2007-10-16 20:58:38 +08001353 /* initialize host controller */
1354 sata_fsl_init_controller(host);
1355
1356 /*
1357 * Now, register with libATA core, this will also initiate the
1358 * device discovery process, invoking our port_start() handler &
1359 * error_handler() to execute a dummy Softreset EH session
1360 */
1361 ata_host_activate(host, irq, sata_fsl_interrupt, SATA_FSL_IRQ_FLAG,
1362 &sata_fsl_sht);
1363
1364 dev_set_drvdata(&ofdev->dev, host);
1365
1366 return 0;
1367
1368error_exit_with_cleanup:
1369
1370 if (hcr_base)
1371 iounmap(hcr_base);
1372 if (host_priv)
1373 kfree(host_priv);
1374
1375 return retval;
1376}
1377
Grant Likely2dc11582010-08-06 09:25:50 -06001378static int sata_fsl_remove(struct platform_device *ofdev)
Li Yangfaf0b2e2007-10-16 20:58:38 +08001379{
1380 struct ata_host *host = dev_get_drvdata(&ofdev->dev);
1381 struct sata_fsl_host_priv *host_priv = host->private_data;
1382
1383 ata_host_detach(host);
1384
1385 dev_set_drvdata(&ofdev->dev, NULL);
1386
Li Yang79b3edc2007-10-31 19:27:55 +08001387 irq_dispose_mapping(host_priv->irq);
Li Yangfaf0b2e2007-10-16 20:58:38 +08001388 iounmap(host_priv->hcr_base);
1389 kfree(host_priv);
1390
1391 return 0;
1392}
1393
Dave Liudc77ad42009-06-10 22:53:37 -05001394#ifdef CONFIG_PM
Grant Likely2dc11582010-08-06 09:25:50 -06001395static int sata_fsl_suspend(struct platform_device *op, pm_message_t state)
Dave Liudc77ad42009-06-10 22:53:37 -05001396{
1397 struct ata_host *host = dev_get_drvdata(&op->dev);
1398 return ata_host_suspend(host, state);
1399}
1400
Grant Likely2dc11582010-08-06 09:25:50 -06001401static int sata_fsl_resume(struct platform_device *op)
Dave Liudc77ad42009-06-10 22:53:37 -05001402{
1403 struct ata_host *host = dev_get_drvdata(&op->dev);
1404 struct sata_fsl_host_priv *host_priv = host->private_data;
1405 int ret;
1406 void __iomem *hcr_base = host_priv->hcr_base;
1407 struct ata_port *ap = host->ports[0];
1408 struct sata_fsl_port_priv *pp = ap->private_data;
1409
1410 ret = sata_fsl_init_controller(host);
1411 if (ret) {
1412 dev_printk(KERN_ERR, &op->dev,
1413 "Error initialize hardware\n");
1414 return ret;
1415 }
1416
1417 /* Recovery the CHBA register in host controller cmd register set */
1418 iowrite32(pp->cmdslot_paddr & 0xffffffff, hcr_base + CHBA);
1419
1420 ata_host_resume(host);
1421 return 0;
1422}
1423#endif
1424
Li Yangfaf0b2e2007-10-16 20:58:38 +08001425static struct of_device_id fsl_sata_match[] = {
1426 {
Kim Phillips96ce1b62008-03-28 10:51:33 -05001427 .compatible = "fsl,pq-sata",
Li Yangfaf0b2e2007-10-16 20:58:38 +08001428 },
Xulei2f957fc2011-01-19 17:07:29 +08001429 {
1430 .compatible = "fsl,pq-sata-v2",
1431 },
Li Yangfaf0b2e2007-10-16 20:58:38 +08001432 {},
1433};
1434
1435MODULE_DEVICE_TABLE(of, fsl_sata_match);
1436
1437static struct of_platform_driver fsl_sata_driver = {
Grant Likely40182942010-04-13 16:13:02 -07001438 .driver = {
1439 .name = "fsl-sata",
1440 .owner = THIS_MODULE,
1441 .of_match_table = fsl_sata_match,
1442 },
Li Yangfaf0b2e2007-10-16 20:58:38 +08001443 .probe = sata_fsl_probe,
1444 .remove = sata_fsl_remove,
Dave Liudc77ad42009-06-10 22:53:37 -05001445#ifdef CONFIG_PM
1446 .suspend = sata_fsl_suspend,
1447 .resume = sata_fsl_resume,
1448#endif
Li Yangfaf0b2e2007-10-16 20:58:38 +08001449};
1450
1451static int __init sata_fsl_init(void)
1452{
1453 of_register_platform_driver(&fsl_sata_driver);
1454 return 0;
1455}
1456
1457static void __exit sata_fsl_exit(void)
1458{
1459 of_unregister_platform_driver(&fsl_sata_driver);
1460}
1461
1462MODULE_LICENSE("GPL");
1463MODULE_AUTHOR("Ashish Kalra, Freescale Semiconductor");
1464MODULE_DESCRIPTION("Freescale 3.0Gbps SATA controller low level driver");
1465MODULE_VERSION("1.10");
1466
1467module_init(sata_fsl_init);
1468module_exit(sata_fsl_exit);