blob: 42c177fe455b1976ecb13ca366549bfb51b11b4f [file] [log] [blame]
Albert Aribaud39419ce2010-08-08 05:17:06 +05301/*
Albert ARIBAUD57b4bce2011-04-22 19:41:02 +02002 * Copyright (C) 2010 Albert ARIBAUD <albert.u.boot@aribaud.net>
Albert Aribaud39419ce2010-08-08 05:17:06 +05303 *
Albert ARIBAUD57b4bce2011-04-22 19:41:02 +02004 * Written-by: Albert ARIBAUD <albert.u.boot@aribaud.net>
Albert Aribaud39419ce2010-08-08 05:17:06 +05305 *
6 * See file CREDITS for list of people who contributed to this
7 * project.
8 *
9 * This program is free software; you can redistribute it and/or
10 * modify it under the terms of the GNU General Public License as
11 * published by the Free Software Foundation; either version 2 of
12 * the License, or (at your option) any later version.
13 *
14 * This program is distributed in the hope that it will be useful,
15 * but WITHOUT ANY WARRANTY; without even the implied warranty of
16 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
17 * GNU General Public License for more details.
18 *
19 * You should have received a copy of the GNU General Public License
20 * along with this program; if not, write to the Free Software
21 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston,
22 * MA 02110-1301 USA
23 */
24
25#include <common.h>
26#include <asm/io.h>
27
28#if defined(CONFIG_ORION5X)
29#include <asm/arch/orion5x.h>
30#elif defined(CONFIG_KIRKWOOD)
31#include <asm/arch/kirkwood.h>
32#endif
33
34/* SATA port registers */
35struct mvsata_port_registers {
Michael Walle70c55f52011-05-11 12:22:46 +000036 u32 reserved0[10];
37 u32 edma_cmd;
38 u32 reserved1[181];
Albert Aribaud39419ce2010-08-08 05:17:06 +053039 /* offset 0x300 : ATA Interface registers */
40 u32 sstatus;
41 u32 serror;
42 u32 scontrol;
43 u32 ltmode;
44 u32 phymode3;
45 u32 phymode4;
46 u32 reserved2[5];
47 u32 phymode1;
48 u32 phymode2;
49 u32 bist_cr;
50 u32 bist_dw1;
51 u32 bist_dw2;
52 u32 serrorintrmask;
53};
54
55/*
56 * Sanity checks:
57 * - to compile at all, we need CONFIG_SYS_ATA_BASE_ADDR.
58 * - for ide_preinit to make sense, we need at least one of
Gray Remlinc08349e2013-02-06 10:59:38 +000059 * CONFIG_SYS_ATA_IDE0_OFFSET or CONFIG_SYS_ATA_IDE1_OFFSET;
60 * - for ide_preinit to be called, we need CONFIG_IDE_PREINIT.
Albert Aribaud39419ce2010-08-08 05:17:06 +053061 * Fail with an explanation message if these conditions are not met.
62 * This is particularly important for CONFIG_IDE_PREINIT, because
63 * its lack would not cause a build error.
64 */
65
66#if !defined(CONFIG_SYS_ATA_BASE_ADDR)
67#error CONFIG_SYS_ATA_BASE_ADDR must be defined
68#elif !defined(CONFIG_SYS_ATA_IDE0_OFFSET) \
69 && !defined(CONFIG_SYS_ATA_IDE1_OFFSET)
70#error CONFIG_SYS_ATA_IDE0_OFFSET or CONFIG_SYS_ATA_IDE1_OFFSET \
71 must be defined
72#elif !defined(CONFIG_IDE_PREINIT)
73#error CONFIG_IDE_PREINIT must be defined
74#endif
75
76/*
77 * Masks and values for SControl DETection and Interface Power Management,
78 * and for SStatus DETection.
79 */
80
Michael Walle70c55f52011-05-11 12:22:46 +000081#define MVSATA_EDMA_CMD_ATA_RST 0x00000004
Albert Aribaud39419ce2010-08-08 05:17:06 +053082#define MVSATA_SCONTROL_DET_MASK 0x0000000F
83#define MVSATA_SCONTROL_DET_NONE 0x00000000
84#define MVSATA_SCONTROL_DET_INIT 0x00000001
85#define MVSATA_SCONTROL_IPM_MASK 0x00000F00
86#define MVSATA_SCONTROL_IPM_NO_LP_ALLOWED 0x00000300
87#define MVSATA_SCONTROL_MASK \
88 (MVSATA_SCONTROL_DET_MASK|MVSATA_SCONTROL_IPM_MASK)
89#define MVSATA_PORT_INIT \
90 (MVSATA_SCONTROL_DET_INIT|MVSATA_SCONTROL_IPM_NO_LP_ALLOWED)
91#define MVSATA_PORT_USE \
92 (MVSATA_SCONTROL_DET_NONE|MVSATA_SCONTROL_IPM_NO_LP_ALLOWED)
93#define MVSATA_SSTATUS_DET_MASK 0x0000000F
94#define MVSATA_SSTATUS_DET_DEVCOMM 0x00000003
95
96/*
Albert Aribaudd3497132010-09-16 20:30:30 +053097 * Status codes to return to client callers. Currently, callers ignore
98 * exact value and only care for zero or nonzero, so no need to make this
99 * public, it is only #define'd for clarity.
100 * If/when standard negative codes are implemented in U-boot, then these
101 * #defines should be moved to, or replaced by ones from, the common list
102 * of status codes.
103 */
104
105#define MVSATA_STATUS_OK 0
106#define MVSATA_STATUS_TIMEOUT -1
107
108/*
Albert Aribaud39419ce2010-08-08 05:17:06 +0530109 * Initialize one MVSATAHC port: set SControl's IPM to "always active"
110 * and DET to "reset", then wait for SStatus's DET to become "device and
111 * comm ok" (or time out after 50 us if no device), then set SControl's
112 * DET back to "no action".
113 */
114
Albert Aribaudd3497132010-09-16 20:30:30 +0530115static int mvsata_ide_initialize_port(struct mvsata_port_registers *port)
Albert Aribaud39419ce2010-08-08 05:17:06 +0530116{
117 u32 control;
118 u32 status;
Albert Aribaudd3497132010-09-16 20:30:30 +0530119 u32 timeleft = 10000; /* wait at most 10 ms for SATA reset to complete */
Albert Aribaud39419ce2010-08-08 05:17:06 +0530120
Michael Walle70c55f52011-05-11 12:22:46 +0000121 /* Hard reset */
122 writel(MVSATA_EDMA_CMD_ATA_RST, &port->edma_cmd);
123 udelay(25); /* taken from original marvell port */
124 writel(0, &port->edma_cmd);
125
Albert Aribaudd3497132010-09-16 20:30:30 +0530126 /* Set control IPM to 3 (no low power) and DET to 1 (initialize) */
Albert Aribaud39419ce2010-08-08 05:17:06 +0530127 control = readl(&port->scontrol);
128 control = (control & ~MVSATA_SCONTROL_MASK) | MVSATA_PORT_INIT;
129 writel(control, &port->scontrol);
Albert Aribaudd3497132010-09-16 20:30:30 +0530130 /* Toggle control DET back to 0 (normal operation) */
131 control = (control & ~MVSATA_SCONTROL_MASK) | MVSATA_PORT_USE;
132 writel(control, &port->scontrol);
133 /* wait for status DET to become 3 (device and communication OK) */
134 while (--timeleft) {
Albert Aribaud39419ce2010-08-08 05:17:06 +0530135 status = readl(&port->sstatus) & MVSATA_SSTATUS_DET_MASK;
136 if (status == MVSATA_SSTATUS_DET_DEVCOMM)
137 break;
138 udelay(1);
139 }
Albert Aribaudd3497132010-09-16 20:30:30 +0530140 /* return success or time-out error depending on time left */
141 if (!timeleft)
142 return MVSATA_STATUS_TIMEOUT;
143 return MVSATA_STATUS_OK;
Albert Aribaud39419ce2010-08-08 05:17:06 +0530144}
145
146/*
147 * ide_preinit() will be called by ide_init in cmd_ide.c and will
148 * reset the MVSTATHC ports needed by the board.
149 */
150
151int ide_preinit(void)
152{
Simon Guinot2cb4fad2011-11-21 19:25:46 +0530153 int ret = MVSATA_STATUS_TIMEOUT;
Albert Aribaudd3497132010-09-16 20:30:30 +0530154 int status;
Simon Guinot2cb4fad2011-11-21 19:25:46 +0530155
Albert Aribaud39419ce2010-08-08 05:17:06 +0530156 /* Enable ATA port 0 (could be SATA port 0 or 1) if declared */
157#if defined(CONFIG_SYS_ATA_IDE0_OFFSET)
Albert Aribaudd3497132010-09-16 20:30:30 +0530158 status = mvsata_ide_initialize_port(
Albert Aribaud39419ce2010-08-08 05:17:06 +0530159 (struct mvsata_port_registers *)
160 (CONFIG_SYS_ATA_BASE_ADDR + CONFIG_SYS_ATA_IDE0_OFFSET));
Simon Guinot2cb4fad2011-11-21 19:25:46 +0530161 if (status == MVSATA_STATUS_OK)
162 ret = MVSATA_STATUS_OK;
Albert Aribaud39419ce2010-08-08 05:17:06 +0530163#endif
164 /* Enable ATA port 1 (could be SATA port 0 or 1) if declared */
165#if defined(CONFIG_SYS_ATA_IDE1_OFFSET)
Albert Aribaudd3497132010-09-16 20:30:30 +0530166 status = mvsata_ide_initialize_port(
Albert Aribaud39419ce2010-08-08 05:17:06 +0530167 (struct mvsata_port_registers *)
168 (CONFIG_SYS_ATA_BASE_ADDR + CONFIG_SYS_ATA_IDE1_OFFSET));
Simon Guinot2cb4fad2011-11-21 19:25:46 +0530169 if (status == MVSATA_STATUS_OK)
170 ret = MVSATA_STATUS_OK;
Albert Aribaud39419ce2010-08-08 05:17:06 +0530171#endif
Simon Guinot2cb4fad2011-11-21 19:25:46 +0530172 /* Return success if at least one port initialization succeeded */
173 return ret;
Albert Aribaud39419ce2010-08-08 05:17:06 +0530174}