2 * Copyright (C) Excito Elektronik i Skåne AB, 2010.
3 * Author: Tor Krill <tor@excito.com>
5 * Copyright (C) 2015 Stefan Roese <sr@denx.de>
7 * SPDX-License-Identifier: GPL-2.0+
11 * This driver supports the SATA controller of some Mavell SoC's.
12 * Here a (most likely incomplete) list of the supported SoC's:
17 * This driver implementation is an alternative to the already available
18 * driver via the "ide" commands interface (drivers/block/mvsata_ide.c).
19 * But this driver only supports PIO mode and as this new driver also
20 * supports transfer via DMA, its much faster.
22 * Please note, that the newer SoC's (e.g. Armada 38x) are not supported
23 * by this driver. As they have an AHCI compatible SATA controller
29 * Better error recovery
30 * No support for using PRDs (Thus max 64KB transfers)
32 * No port multiplier support
40 #include <linux/errno.h>
42 #include <linux/mbus.h>
44 #if defined(CONFIG_KIRKWOOD)
45 #include <asm/arch/kirkwood.h>
46 #define SATAHC_BASE KW_SATA_BASE
48 #include <asm/arch/soc.h>
49 #define SATAHC_BASE MVEBU_AXP_SATA_BASE
52 #define SATA0_BASE (SATAHC_BASE + 0x2000)
53 #define SATA1_BASE (SATAHC_BASE + 0x4000)
56 #define EDMA_CFG 0x000
57 #define EDMA_CFG_NCQ (1 << 5)
58 #define EDMA_CFG_EQUE (1 << 9)
59 #define EDMA_TIMER 0x004
60 #define EDMA_IECR 0x008
61 #define EDMA_IEMR 0x00c
62 #define EDMA_RQBA_HI 0x010
63 #define EDMA_RQIPR 0x014
64 #define EDMA_RQIPR_IPMASK (0x1f << 5)
65 #define EDMA_RQIPR_IPSHIFT 5
66 #define EDMA_RQOPR 0x018
67 #define EDMA_RQOPR_OPMASK (0x1f << 5)
68 #define EDMA_RQOPR_OPSHIFT 5
69 #define EDMA_RSBA_HI 0x01c
70 #define EDMA_RSIPR 0x020
71 #define EDMA_RSIPR_IPMASK (0x1f << 3)
72 #define EDMA_RSIPR_IPSHIFT 3
73 #define EDMA_RSOPR 0x024
74 #define EDMA_RSOPR_OPMASK (0x1f << 3)
75 #define EDMA_RSOPR_OPSHIFT 3
76 #define EDMA_CMD 0x028
77 #define EDMA_CMD_ENEDMA (0x01 << 0)
78 #define EDMA_CMD_DISEDMA (0x01 << 1)
79 #define EDMA_CMD_ATARST (0x01 << 2)
80 #define EDMA_CMD_FREEZE (0x01 << 4)
81 #define EDMA_TEST_CTL 0x02c
82 #define EDMA_STATUS 0x030
83 #define EDMA_IORTO 0x034
84 #define EDMA_CDTR 0x040
85 #define EDMA_HLTCND 0x060
86 #define EDMA_NTSR 0x094
88 /* Basic DMA registers */
89 #define BDMA_CMD 0x224
90 #define BDMA_STATUS 0x228
91 #define BDMA_DTLB 0x22c
92 #define BDMA_DTHB 0x230
93 #define BDMA_DRL 0x234
94 #define BDMA_DRH 0x238
96 /* SATA Interface registers */
97 #define SIR_ICFG 0x050
98 #define SIR_CFG_GEN2EN (0x1 << 7)
99 #define SIR_PLL_CFG 0x054
100 #define SIR_SSTATUS 0x300
101 #define SSTATUS_DET_MASK (0x0f << 0)
102 #define SIR_SERROR 0x304
103 #define SIR_SCONTROL 0x308
104 #define SIR_SCONTROL_DETEN (0x01 << 0)
105 #define SIR_LTMODE 0x30c
106 #define SIR_LTMODE_NELBE (0x01 << 7)
107 #define SIR_PHYMODE3 0x310
108 #define SIR_PHYMODE4 0x314
109 #define SIR_PHYMODE1 0x32c
110 #define SIR_PHYMODE2 0x330
111 #define SIR_BIST_CTRL 0x334
112 #define SIR_BIST_DW1 0x338
113 #define SIR_BIST_DW2 0x33c
114 #define SIR_SERR_IRQ_MASK 0x340
115 #define SIR_SATA_IFCTRL 0x344
116 #define SIR_SATA_TESTCTRL 0x348
117 #define SIR_SATA_IFSTATUS 0x34c
118 #define SIR_VEND_UNIQ 0x35c
119 #define SIR_FIS_CFG 0x360
120 #define SIR_FIS_IRQ_CAUSE 0x364
121 #define SIR_FIS_IRQ_MASK 0x368
122 #define SIR_FIS_DWORD0 0x370
123 #define SIR_FIS_DWORD1 0x374
124 #define SIR_FIS_DWORD2 0x378
125 #define SIR_FIS_DWORD3 0x37c
126 #define SIR_FIS_DWORD4 0x380
127 #define SIR_FIS_DWORD5 0x384
128 #define SIR_FIS_DWORD6 0x388
129 #define SIR_PHYM9_GEN2 0x398
130 #define SIR_PHYM9_GEN1 0x39c
131 #define SIR_PHY_CFG 0x3a0
132 #define SIR_PHYCTL 0x3a4
133 #define SIR_PHYM10 0x3a8
134 #define SIR_PHYM12 0x3b0
136 /* Shadow registers */
137 #define PIO_DATA 0x100
138 #define PIO_ERR_FEATURES 0x104
139 #define PIO_SECTOR_COUNT 0x108
140 #define PIO_LBA_LOW 0x10c
141 #define PIO_LBA_MID 0x110
142 #define PIO_LBA_HI 0x114
143 #define PIO_DEVICE 0x118
144 #define PIO_CMD_STATUS 0x11c
145 #define PIO_STATUS_ERR (0x01 << 0)
146 #define PIO_STATUS_DRQ (0x01 << 3)
147 #define PIO_STATUS_DF (0x01 << 5)
148 #define PIO_STATUS_DRDY (0x01 << 6)
149 #define PIO_STATUS_BSY (0x01 << 7)
150 #define PIO_CTRL_ALTSTAT 0x120
152 /* SATAHC arbiter registers */
153 #define SATAHC_CFG 0x000
154 #define SATAHC_RQOP 0x004
155 #define SATAHC_RQIP 0x008
156 #define SATAHC_ICT 0x00c
157 #define SATAHC_ITT 0x010
158 #define SATAHC_ICR 0x014
159 #define SATAHC_ICR_PORT0 (0x01 << 0)
160 #define SATAHC_ICR_PORT1 (0x01 << 1)
161 #define SATAHC_MIC 0x020
162 #define SATAHC_MIM 0x024
163 #define SATAHC_LED_CFG 0x02c
165 #define REQUEST_QUEUE_SIZE 32
166 #define RESPONSE_QUEUE_SIZE REQUEST_QUEUE_SIZE
169 u32 dtb_low; /* DW0 */
170 u32 dtb_high; /* DW1 */
171 u32 control_flags; /* DW2 */
172 u32 drb_count; /* DW3 */
173 u32 ata_cmd_feat; /* DW4 */
174 u32 ata_addr; /* DW5 */
175 u32 ata_addr_exp; /* DW6 */
176 u32 ata_sect_count; /* DW7 */
179 #define CRQB_ALIGN 0x400
181 #define CRQB_CNTRLFLAGS_DIR (0x01 << 0)
182 #define CRQB_CNTRLFLAGS_DQTAGMASK (0x1f << 1)
183 #define CRQB_CNTRLFLAGS_DQTAGSHIFT 1
184 #define CRQB_CNTRLFLAGS_PMPORTMASK (0x0f << 12)
185 #define CRQB_CNTRLFLAGS_PMPORTSHIFT 12
186 #define CRQB_CNTRLFLAGS_PRDMODE (0x01 << 16)
187 #define CRQB_CNTRLFLAGS_HQTAGMASK (0x1f << 17)
188 #define CRQB_CNTRLFLAGS_HQTAGSHIFT 17
190 #define CRQB_CMDFEAT_CMDMASK (0xff << 16)
191 #define CRQB_CMDFEAT_CMDSHIFT 16
192 #define CRQB_CMDFEAT_FEATMASK (0xff << 16)
193 #define CRQB_CMDFEAT_FEATSHIFT 24
195 #define CRQB_ADDR_LBA_LOWMASK (0xff << 0)
196 #define CRQB_ADDR_LBA_LOWSHIFT 0
197 #define CRQB_ADDR_LBA_MIDMASK (0xff << 8)
198 #define CRQB_ADDR_LBA_MIDSHIFT 8
199 #define CRQB_ADDR_LBA_HIGHMASK (0xff << 16)
200 #define CRQB_ADDR_LBA_HIGHSHIFT 16
201 #define CRQB_ADDR_DEVICE_MASK (0xff << 24)
202 #define CRQB_ADDR_DEVICE_SHIFT 24
204 #define CRQB_ADDR_LBA_LOW_EXP_MASK (0xff << 0)
205 #define CRQB_ADDR_LBA_LOW_EXP_SHIFT 0
206 #define CRQB_ADDR_LBA_MID_EXP_MASK (0xff << 8)
207 #define CRQB_ADDR_LBA_MID_EXP_SHIFT 8
208 #define CRQB_ADDR_LBA_HIGH_EXP_MASK (0xff << 16)
209 #define CRQB_ADDR_LBA_HIGH_EXP_SHIFT 16
210 #define CRQB_ADDR_FEATURE_EXP_MASK (0xff << 24)
211 #define CRQB_ADDR_FEATURE_EXP_SHIFT 24
213 #define CRQB_SECTCOUNT_COUNT_MASK (0xff << 0)
214 #define CRQB_SECTCOUNT_COUNT_SHIFT 0
215 #define CRQB_SECTCOUNT_COUNT_EXP_MASK (0xff << 8)
216 #define CRQB_SECTCOUNT_COUNT_EXP_SHIFT 8
218 #define MVSATA_WIN_CONTROL(w) (MVEBU_AXP_SATA_BASE + 0x30 + ((w) << 4))
219 #define MVSATA_WIN_BASE(w) (MVEBU_AXP_SATA_BASE + 0x34 + ((w) << 4))
228 #define EPRD_PHYADDR_MASK 0xfffffffe
229 #define EPRD_BYTECOUNT_MASK 0x0000ffff
230 #define EPRD_EOT (0x01 << 31)
238 #define CRPB_ALIGN 0x100
244 * Since we don't use PRDs yet max transfer size
247 #define MV_ATA_MAX_SECTORS (65535 / ATA_SECT_SIZE)
249 /* Keep track if hw is initialized or not */
262 struct crqb *request;
265 struct crpb *response;
268 static int ata_wait_register(u32 *addr, u32 mask, u32 val, u32 timeout_msec)
272 start = get_timer(0);
274 if ((in_le32(addr) & mask) == val)
276 } while (get_timer(start) < timeout_msec);
281 /* Cut from sata_mv in linux kernel */
282 static int mv_stop_edma_engine(int port)
284 struct mv_priv *priv = (struct mv_priv *)sata_dev_desc[port].priv;
287 /* Disable eDMA. The disable bit auto clears. */
288 out_le32(priv->regbase + EDMA_CMD, EDMA_CMD_DISEDMA);
290 /* Wait for the chip to confirm eDMA is off. */
291 for (i = 10000; i > 0; i--) {
292 u32 reg = in_le32(priv->regbase + EDMA_CMD);
293 if (!(reg & EDMA_CMD_ENEDMA)) {
294 debug("EDMA stop on port %d succesful\n", port);
299 debug("EDMA stop on port %d failed\n", port);
303 static int mv_start_edma_engine(int port)
305 struct mv_priv *priv = (struct mv_priv *)sata_dev_desc[port].priv;
308 /* Check preconditions */
309 tmp = in_le32(priv->regbase + SIR_SSTATUS);
310 if ((tmp & SSTATUS_DET_MASK) != 0x03) {
311 printf("Device error on port: %d\n", port);
315 tmp = in_le32(priv->regbase + PIO_CMD_STATUS);
316 if (tmp & (ATA_BUSY | ATA_DRQ)) {
317 printf("Device not ready on port: %d\n", port);
321 /* Clear interrupt cause */
322 out_le32(priv->regbase + EDMA_IECR, 0x0);
324 tmp = in_le32(SATAHC_BASE + SATAHC_ICR);
325 tmp &= ~(port == 0 ? SATAHC_ICR_PORT0 : SATAHC_ICR_PORT1);
326 out_le32(SATAHC_BASE + SATAHC_ICR, tmp);
328 /* Configure edma operation */
329 tmp = in_le32(priv->regbase + EDMA_CFG);
330 tmp &= ~EDMA_CFG_NCQ; /* No NCQ */
331 tmp &= ~EDMA_CFG_EQUE; /* Dont queue operations */
332 out_le32(priv->regbase + EDMA_CFG, tmp);
334 out_le32(priv->regbase + SIR_FIS_IRQ_CAUSE, 0x0);
336 /* Configure fis, set all to no-wait for now */
337 out_le32(priv->regbase + SIR_FIS_CFG, 0x0);
339 /* Setup request queue */
340 out_le32(priv->regbase + EDMA_RQBA_HI, 0x0);
341 out_le32(priv->regbase + EDMA_RQIPR, priv->request);
342 out_le32(priv->regbase + EDMA_RQOPR, 0x0);
344 /* Setup response queue */
345 out_le32(priv->regbase + EDMA_RSBA_HI, 0x0);
346 out_le32(priv->regbase + EDMA_RSOPR, priv->response);
347 out_le32(priv->regbase + EDMA_RSIPR, 0x0);
350 out_le32(priv->regbase + EDMA_CMD, EDMA_CMD_ENEDMA);
355 static int mv_reset_channel(int port)
357 struct mv_priv *priv = (struct mv_priv *)sata_dev_desc[port].priv;
359 /* Make sure edma is stopped */
360 mv_stop_edma_engine(port);
362 out_le32(priv->regbase + EDMA_CMD, EDMA_CMD_ATARST);
363 udelay(25); /* allow reset propagation */
364 out_le32(priv->regbase + EDMA_CMD, 0);
370 static void mv_reset_port(int port)
372 struct mv_priv *priv = (struct mv_priv *)sata_dev_desc[port].priv;
374 mv_reset_channel(port);
376 out_le32(priv->regbase + EDMA_CMD, 0x0);
377 out_le32(priv->regbase + EDMA_CFG, 0x101f);
378 out_le32(priv->regbase + EDMA_IECR, 0x0);
379 out_le32(priv->regbase + EDMA_IEMR, 0x0);
380 out_le32(priv->regbase + EDMA_RQBA_HI, 0x0);
381 out_le32(priv->regbase + EDMA_RQIPR, 0x0);
382 out_le32(priv->regbase + EDMA_RQOPR, 0x0);
383 out_le32(priv->regbase + EDMA_RSBA_HI, 0x0);
384 out_le32(priv->regbase + EDMA_RSIPR, 0x0);
385 out_le32(priv->regbase + EDMA_RSOPR, 0x0);
386 out_le32(priv->regbase + EDMA_IORTO, 0xfa);
389 static void mv_reset_one_hc(void)
391 out_le32(SATAHC_BASE + SATAHC_ICT, 0x00);
392 out_le32(SATAHC_BASE + SATAHC_ITT, 0x00);
393 out_le32(SATAHC_BASE + SATAHC_ICR, 0x00);
396 static int probe_port(int port)
398 struct mv_priv *priv = (struct mv_priv *)sata_dev_desc[port].priv;
399 int tries, tries2, set15 = 0;
402 debug("Probe port: %d\n", port);
404 for (tries = 0; tries < 2; tries++) {
406 out_le32(priv->regbase + SIR_SERROR, 0x0);
408 /* trigger com-init */
409 tmp = in_le32(priv->regbase + SIR_SCONTROL);
410 tmp = (tmp & 0x0f0) | 0x300 | SIR_SCONTROL_DETEN;
411 out_le32(priv->regbase + SIR_SCONTROL, tmp);
415 tmp = in_le32(priv->regbase + SIR_SCONTROL);
418 tmp = (tmp & 0x0f0) | 0x300;
419 out_le32(priv->regbase + SIR_SCONTROL, tmp);
421 tmp = in_le32(priv->regbase + SIR_SCONTROL);
422 } while ((tmp & 0xf0f) != 0x300 && tries2--);
426 for (tries2 = 0; tries2 < 200; tries2++) {
427 tmp = in_le32(priv->regbase + SIR_SSTATUS);
428 if ((tmp & SSTATUS_DET_MASK) == 0x03) {
429 debug("Found device on port\n");
435 if ((tmp & SSTATUS_DET_MASK) == 0) {
436 debug("No device attached on port %d\n", port);
442 debug("Try 1.5Gb link\n");
444 out_le32(priv->regbase + SIR_SCONTROL, 0x304);
446 tmp = in_le32(priv->regbase + SIR_ICFG);
447 tmp &= ~SIR_CFG_GEN2EN;
448 out_le32(priv->regbase + SIR_ICFG, tmp);
450 mv_reset_channel(port);
454 debug("Failed to probe port\n");
458 /* Get request queue in pointer */
459 static int get_reqip(int port)
461 struct mv_priv *priv = (struct mv_priv *)sata_dev_desc[port].priv;
464 tmp = in_le32(priv->regbase + EDMA_RQIPR) & EDMA_RQIPR_IPMASK;
465 tmp = tmp >> EDMA_RQIPR_IPSHIFT;
470 static void set_reqip(int port, int reqin)
472 struct mv_priv *priv = (struct mv_priv *)sata_dev_desc[port].priv;
475 tmp = in_le32(priv->regbase + EDMA_RQIPR) & ~EDMA_RQIPR_IPMASK;
476 tmp |= ((reqin << EDMA_RQIPR_IPSHIFT) & EDMA_RQIPR_IPMASK);
477 out_le32(priv->regbase + EDMA_RQIPR, tmp);
480 /* Get next available slot, ignoring possible overwrite */
481 static int get_next_reqip(int port)
483 int slot = get_reqip(port);
484 slot = (slot + 1) % REQUEST_QUEUE_SIZE;
488 /* Get response queue in pointer */
489 static int get_rspip(int port)
491 struct mv_priv *priv = (struct mv_priv *)sata_dev_desc[port].priv;
494 tmp = in_le32(priv->regbase + EDMA_RSIPR) & EDMA_RSIPR_IPMASK;
495 tmp = tmp >> EDMA_RSIPR_IPSHIFT;
500 /* Get response queue out pointer */
501 static int get_rspop(int port)
503 struct mv_priv *priv = (struct mv_priv *)sata_dev_desc[port].priv;
506 tmp = in_le32(priv->regbase + EDMA_RSOPR) & EDMA_RSOPR_OPMASK;
507 tmp = tmp >> EDMA_RSOPR_OPSHIFT;
511 /* Get next response queue pointer */
512 static int get_next_rspop(int port)
514 return (get_rspop(port) + 1) % RESPONSE_QUEUE_SIZE;
517 /* Set response queue pointer */
518 static void set_rspop(int port, int reqin)
520 struct mv_priv *priv = (struct mv_priv *)sata_dev_desc[port].priv;
523 tmp = in_le32(priv->regbase + EDMA_RSOPR) & ~EDMA_RSOPR_OPMASK;
524 tmp |= ((reqin << EDMA_RSOPR_OPSHIFT) & EDMA_RSOPR_OPMASK);
526 out_le32(priv->regbase + EDMA_RSOPR, tmp);
529 static int wait_dma_completion(int port, int index, u32 timeout_msec)
533 tmp = port == 0 ? SATAHC_ICR_PORT0 : SATAHC_ICR_PORT1;
534 res = ata_wait_register((u32 *)(SATAHC_BASE + SATAHC_ICR), tmp,
537 printf("Failed to wait for completion on port %d\n", port);
542 static void process_responses(int port)
545 struct mv_priv *priv = (struct mv_priv *)sata_dev_desc[port].priv;
548 u32 outind = get_rspop(port);
551 tmp = in_le32(SATAHC_BASE + SATAHC_ICR);
553 tmp &= ~(BIT(0) | BIT(8));
555 tmp &= ~(BIT(1) | BIT(9));
557 out_le32(SATAHC_BASE + SATAHC_ICR, tmp);
559 while (get_rspip(port) != outind) {
561 debug("Response index %d flags %08x on port %d\n", outind,
562 priv->response[outind].flags, port);
564 outind = get_next_rspop(port);
565 set_rspop(port, outind);
569 static int mv_ata_exec_ata_cmd(int port, struct sata_fis_h2d *cfis,
570 u8 *buffer, u32 len, u32 iswrite)
572 struct mv_priv *priv = (struct mv_priv *)sata_dev_desc[port].priv;
577 if (len >= 64 * 1024) {
578 printf("We only support <64K transfers for now\n");
582 /* Initialize request */
583 slot = get_reqip(port);
584 memset(&priv->request[slot], 0, sizeof(struct crqb));
585 req = &priv->request[slot];
587 req->dtb_low = (u32)buffer;
590 req->control_flags = CRQB_CNTRLFLAGS_PRDMODE;
591 req->control_flags |= iswrite ? 0 : CRQB_CNTRLFLAGS_DIR;
592 req->control_flags |=
593 ((cfis->pm_port_c << CRQB_CNTRLFLAGS_PMPORTSHIFT)
594 & CRQB_CNTRLFLAGS_PMPORTMASK);
596 req->drb_count = len;
598 req->ata_cmd_feat = (cfis->command << CRQB_CMDFEAT_CMDSHIFT) &
599 CRQB_CMDFEAT_CMDMASK;
600 req->ata_cmd_feat |= (cfis->features << CRQB_CMDFEAT_FEATSHIFT) &
601 CRQB_CMDFEAT_FEATMASK;
603 req->ata_addr = (cfis->lba_low << CRQB_ADDR_LBA_LOWSHIFT) &
604 CRQB_ADDR_LBA_LOWMASK;
605 req->ata_addr |= (cfis->lba_mid << CRQB_ADDR_LBA_MIDSHIFT) &
606 CRQB_ADDR_LBA_MIDMASK;
607 req->ata_addr |= (cfis->lba_high << CRQB_ADDR_LBA_HIGHSHIFT) &
608 CRQB_ADDR_LBA_HIGHMASK;
609 req->ata_addr |= (cfis->device << CRQB_ADDR_DEVICE_SHIFT) &
610 CRQB_ADDR_DEVICE_MASK;
612 req->ata_addr_exp = (cfis->lba_low_exp << CRQB_ADDR_LBA_LOW_EXP_SHIFT) &
613 CRQB_ADDR_LBA_LOW_EXP_MASK;
615 (cfis->lba_mid_exp << CRQB_ADDR_LBA_MID_EXP_SHIFT) &
616 CRQB_ADDR_LBA_MID_EXP_MASK;
618 (cfis->lba_high_exp << CRQB_ADDR_LBA_HIGH_EXP_SHIFT) &
619 CRQB_ADDR_LBA_HIGH_EXP_MASK;
621 (cfis->features_exp << CRQB_ADDR_FEATURE_EXP_SHIFT) &
622 CRQB_ADDR_FEATURE_EXP_MASK;
624 req->ata_sect_count =
625 (cfis->sector_count << CRQB_SECTCOUNT_COUNT_SHIFT) &
626 CRQB_SECTCOUNT_COUNT_MASK;
627 req->ata_sect_count |=
628 (cfis->sector_count_exp << CRQB_SECTCOUNT_COUNT_EXP_SHIFT) &
629 CRQB_SECTCOUNT_COUNT_EXP_MASK;
632 start = (u32)req & ~(ARCH_DMA_MINALIGN - 1);
633 flush_dcache_range(start,
634 start + ALIGN(sizeof(*req), ARCH_DMA_MINALIGN));
636 /* Trigger operation */
637 slot = get_next_reqip(port);
638 set_reqip(port, slot);
640 /* Wait for completion */
641 if (wait_dma_completion(port, slot, 10000)) {
642 printf("ATA operation timed out\n");
646 process_responses(port);
648 /* Invalidate data on read */
650 start = (u32)buffer & ~(ARCH_DMA_MINALIGN - 1);
651 invalidate_dcache_range(start,
652 start + ALIGN(len, ARCH_DMA_MINALIGN));
658 static u32 mv_sata_rw_cmd_ext(int port, lbaint_t start, u32 blkcnt,
659 u8 *buffer, int is_write)
661 struct sata_fis_h2d cfis;
667 memset(&cfis, 0, sizeof(struct sata_fis_h2d));
669 cfis.fis_type = SATA_FIS_TYPE_REGISTER_H2D;
670 cfis.command = (is_write) ? ATA_CMD_WRITE_EXT : ATA_CMD_READ_EXT;
672 cfis.lba_high_exp = (block >> 40) & 0xff;
673 cfis.lba_mid_exp = (block >> 32) & 0xff;
674 cfis.lba_low_exp = (block >> 24) & 0xff;
675 cfis.lba_high = (block >> 16) & 0xff;
676 cfis.lba_mid = (block >> 8) & 0xff;
677 cfis.lba_low = block & 0xff;
678 cfis.device = ATA_LBA;
679 cfis.sector_count_exp = (blkcnt >> 8) & 0xff;
680 cfis.sector_count = blkcnt & 0xff;
682 res = mv_ata_exec_ata_cmd(port, &cfis, buffer, ATA_SECT_SIZE * blkcnt,
685 return res >= 0 ? blkcnt : res;
688 static u32 mv_sata_rw_cmd(int port, lbaint_t start, u32 blkcnt, u8 *buffer,
691 struct sata_fis_h2d cfis;
697 memset(&cfis, 0, sizeof(struct sata_fis_h2d));
699 cfis.fis_type = SATA_FIS_TYPE_REGISTER_H2D;
700 cfis.command = (is_write) ? ATA_CMD_WRITE : ATA_CMD_READ;
701 cfis.device = ATA_LBA;
703 cfis.device |= (block >> 24) & 0xf;
704 cfis.lba_high = (block >> 16) & 0xff;
705 cfis.lba_mid = (block >> 8) & 0xff;
706 cfis.lba_low = block & 0xff;
707 cfis.sector_count = (u8)(blkcnt & 0xff);
709 res = mv_ata_exec_ata_cmd(port, &cfis, buffer, ATA_SECT_SIZE * blkcnt,
712 return res >= 0 ? blkcnt : res;
715 static u32 ata_low_level_rw(int dev, lbaint_t blknr, lbaint_t blkcnt,
716 void *buffer, int is_write)
718 lbaint_t start, blks;
722 debug("%s: %ld %ld\n", __func__, blknr, blkcnt);
728 max_blks = MV_ATA_MAX_SECTORS;
730 if (blks > max_blks) {
731 if (sata_dev_desc[dev].lba48) {
732 mv_sata_rw_cmd_ext(dev, start, max_blks, addr,
735 mv_sata_rw_cmd(dev, start, max_blks, addr,
740 addr += ATA_SECT_SIZE * max_blks;
742 if (sata_dev_desc[dev].lba48) {
743 mv_sata_rw_cmd_ext(dev, start, blks, addr,
746 mv_sata_rw_cmd(dev, start, blks, addr,
751 addr += ATA_SECT_SIZE * blks;
758 static int mv_ata_exec_ata_cmd_nondma(int port,
759 struct sata_fis_h2d *cfis, u8 *buffer,
760 u32 len, u32 iswrite)
762 struct mv_priv *priv = (struct mv_priv *)sata_dev_desc[port].priv;
766 debug("%s\n", __func__);
768 out_le32(priv->regbase + PIO_SECTOR_COUNT, cfis->sector_count);
769 out_le32(priv->regbase + PIO_LBA_HI, cfis->lba_high);
770 out_le32(priv->regbase + PIO_LBA_MID, cfis->lba_mid);
771 out_le32(priv->regbase + PIO_LBA_LOW, cfis->lba_low);
772 out_le32(priv->regbase + PIO_ERR_FEATURES, cfis->features);
773 out_le32(priv->regbase + PIO_DEVICE, cfis->device);
774 out_le32(priv->regbase + PIO_CMD_STATUS, cfis->command);
776 if (ata_wait_register((u32 *)(priv->regbase + PIO_CMD_STATUS),
777 ATA_BUSY, 0x0, 10000)) {
778 debug("Failed to wait for completion\n");
784 for (i = 0; i < len / 2; i++) {
786 out_le16(priv->regbase + PIO_DATA, *tp++);
788 *tp++ = in_le16(priv->regbase + PIO_DATA);
795 static int mv_sata_identify(int port, u16 *id)
797 struct sata_fis_h2d h2d;
799 memset(&h2d, 0, sizeof(struct sata_fis_h2d));
801 h2d.fis_type = SATA_FIS_TYPE_REGISTER_H2D;
802 h2d.command = ATA_CMD_ID_ATA;
804 /* Give device time to get operational */
807 return mv_ata_exec_ata_cmd_nondma(port, &h2d, (u8 *)id,
808 ATA_ID_WORDS * 2, READ_CMD);
811 static void mv_sata_xfer_mode(int port, u16 *id)
813 struct mv_priv *priv = (struct mv_priv *)sata_dev_desc[port].priv;
815 priv->pio = id[ATA_ID_PIO_MODES];
816 priv->mwdma = id[ATA_ID_MWDMA_MODES];
817 priv->udma = id[ATA_ID_UDMA_MODES];
818 debug("pio %04x, mwdma %04x, udma %04x\n", priv->pio, priv->mwdma,
822 static void mv_sata_set_features(int port)
824 struct mv_priv *priv = (struct mv_priv *)sata_dev_desc[port].priv;
825 struct sata_fis_h2d cfis;
828 memset(&cfis, 0, sizeof(struct sata_fis_h2d));
830 cfis.fis_type = SATA_FIS_TYPE_REGISTER_H2D;
831 cfis.command = ATA_CMD_SET_FEATURES;
832 cfis.features = SETFEATURES_XFER;
834 /* First check the device capablity */
835 udma_cap = (u8) (priv->udma & 0xff);
837 if (udma_cap == ATA_UDMA6)
838 cfis.sector_count = XFER_UDMA_6;
839 if (udma_cap == ATA_UDMA5)
840 cfis.sector_count = XFER_UDMA_5;
841 if (udma_cap == ATA_UDMA4)
842 cfis.sector_count = XFER_UDMA_4;
843 if (udma_cap == ATA_UDMA3)
844 cfis.sector_count = XFER_UDMA_3;
846 mv_ata_exec_ata_cmd_nondma(port, &cfis, NULL, 0, READ_CMD);
849 int mv_sata_spin_down(int dev)
851 struct sata_fis_h2d cfis;
852 struct mv_priv *priv = (struct mv_priv *)sata_dev_desc[dev].priv;
854 if (priv->link == 0) {
855 debug("No device on port: %d\n", dev);
859 memset(&cfis, 0, sizeof(struct sata_fis_h2d));
861 cfis.fis_type = SATA_FIS_TYPE_REGISTER_H2D;
862 cfis.command = ATA_CMD_STANDBY;
864 return mv_ata_exec_ata_cmd_nondma(dev, &cfis, NULL, 0, READ_CMD);
867 int mv_sata_spin_up(int dev)
869 struct sata_fis_h2d cfis;
870 struct mv_priv *priv = (struct mv_priv *)sata_dev_desc[dev].priv;
872 if (priv->link == 0) {
873 debug("No device on port: %d\n", dev);
877 memset(&cfis, 0, sizeof(struct sata_fis_h2d));
879 cfis.fis_type = SATA_FIS_TYPE_REGISTER_H2D;
880 cfis.command = ATA_CMD_IDLE;
882 return mv_ata_exec_ata_cmd_nondma(dev, &cfis, NULL, 0, READ_CMD);
885 ulong sata_read(int dev, ulong blknr, lbaint_t blkcnt, void *buffer)
887 return ata_low_level_rw(dev, blknr, blkcnt, buffer, READ_CMD);
890 ulong sata_write(int dev, ulong blknr, lbaint_t blkcnt, const void *buffer)
892 return ata_low_level_rw(dev, blknr, blkcnt, (void *)buffer, WRITE_CMD);
896 * Initialize SATA memory windows
898 static void mvsata_ide_conf_mbus_windows(void)
900 const struct mbus_dram_target_info *dram;
903 dram = mvebu_mbus_dram_info();
905 /* Disable windows, Set Size/Base to 0 */
906 for (i = 0; i < 4; i++) {
907 writel(0, MVSATA_WIN_CONTROL(i));
908 writel(0, MVSATA_WIN_BASE(i));
911 for (i = 0; i < dram->num_cs; i++) {
912 const struct mbus_dram_window *cs = dram->cs + i;
913 writel(((cs->size - 1) & 0xffff0000) | (cs->mbus_attr << 8) |
914 (dram->mbus_dram_target_id << 4) | 1,
915 MVSATA_WIN_CONTROL(i));
916 writel(cs->base & 0xffff0000, MVSATA_WIN_BASE(i));
920 int init_sata(int dev)
922 struct mv_priv *priv;
924 debug("Initialize sata dev: %d\n", dev);
926 if (dev < 0 || dev >= CONFIG_SYS_SATA_MAX_DEVICE) {
927 printf("Invalid sata device %d\n", dev);
931 priv = (struct mv_priv *)malloc(sizeof(struct mv_priv));
933 printf("Failed to allocate memory for private sata data\n");
937 memset((void *)priv, 0, sizeof(struct mv_priv));
939 /* Allocate and align request buffer */
940 priv->crqb_alloc = malloc(sizeof(struct crqb) * REQUEST_QUEUE_SIZE +
942 if (!priv->crqb_alloc) {
943 printf("Unable to allocate memory for request queue\n");
946 memset(priv->crqb_alloc, 0,
947 sizeof(struct crqb) * REQUEST_QUEUE_SIZE + CRQB_ALIGN);
948 priv->request = (struct crqb *)(((u32) priv->crqb_alloc + CRQB_ALIGN) &
951 /* Allocate and align response buffer */
952 priv->crpb_alloc = malloc(sizeof(struct crpb) * REQUEST_QUEUE_SIZE +
954 if (!priv->crpb_alloc) {
955 printf("Unable to allocate memory for response queue\n");
958 memset(priv->crpb_alloc, 0,
959 sizeof(struct crpb) * REQUEST_QUEUE_SIZE + CRPB_ALIGN);
960 priv->response = (struct crpb *)(((u32) priv->crpb_alloc + CRPB_ALIGN) &
963 sata_dev_desc[dev].priv = (void *)priv;
965 sprintf(priv->name, "SATA%d", dev);
967 priv->regbase = dev == 0 ? SATA0_BASE : SATA1_BASE;
970 debug("Initialize sata hw\n");
973 mvsata_ide_conf_mbus_windows();
978 if (probe_port(dev)) {
987 int reset_sata(int dev)
992 int scan_sata(int port)
994 unsigned char serial[ATA_ID_SERNO_LEN + 1];
995 unsigned char firmware[ATA_ID_FW_REV_LEN + 1];
996 unsigned char product[ATA_ID_PROD_LEN + 1];
999 struct mv_priv *priv = (struct mv_priv *)sata_dev_desc[port].priv;
1004 id = (u16 *)malloc(ATA_ID_WORDS * 2);
1006 printf("Failed to malloc id data\n");
1010 mv_sata_identify(port, id);
1011 ata_swap_buf_le16(id, ATA_ID_WORDS);
1017 ata_id_c_string(id, serial, ATA_ID_SERNO, sizeof(serial));
1018 memcpy(sata_dev_desc[port].product, serial, sizeof(serial));
1020 /* Firmware version */
1021 ata_id_c_string(id, firmware, ATA_ID_FW_REV, sizeof(firmware));
1022 memcpy(sata_dev_desc[port].revision, firmware, sizeof(firmware));
1025 ata_id_c_string(id, product, ATA_ID_PROD, sizeof(product));
1026 memcpy(sata_dev_desc[port].vendor, product, sizeof(product));
1029 n_sectors = ata_id_n_sectors(id);
1030 sata_dev_desc[port].lba = n_sectors;
1032 /* Check if support LBA48 */
1033 if (ata_id_has_lba48(id)) {
1034 sata_dev_desc[port].lba48 = 1;
1035 debug("Device support LBA48\n");
1038 /* Get the NCQ queue depth from device */
1039 priv->queue_depth = ata_id_queue_depth(id);
1041 /* Get the xfer mode from device */
1042 mv_sata_xfer_mode(port, id);
1044 /* Set the xfer mode to highest speed */
1045 mv_sata_set_features(port);
1048 mv_start_edma_engine(port);