blob: 6a98e97c49f87e10ee54b056ffd29422018453fd [file] [log] [blame]
Yusuke Godafdc50a92010-05-26 14:41:59 -07001/*
2 * include/linux/mmc/sh_mmcif.h
3 *
4 * platform data for eMMC driver
5 *
6 * Copyright (C) 2010 Renesas Solutions Corp.
7 *
8 * This program is free software; you can redistribute it and/or modify
9 * it under the terms of the GNU General Public License as published by
10 * the Free Software Foundation; either version 2 of the License.
11 *
12 */
13
14#ifndef __SH_MMCIF_H__
15#define __SH_MMCIF_H__
16
Magnus Damm487d9fc2010-05-18 14:42:51 +000017#include <linux/platform_device.h>
18#include <linux/io.h>
19
Yusuke Godafdc50a92010-05-26 14:41:59 -070020/*
21 * MMCIF : CE_CLK_CTRL [19:16]
22 * 1000 : Peripheral clock / 512
23 * 0111 : Peripheral clock / 256
24 * 0110 : Peripheral clock / 128
25 * 0101 : Peripheral clock / 64
26 * 0100 : Peripheral clock / 32
27 * 0011 : Peripheral clock / 16
28 * 0010 : Peripheral clock / 8
29 * 0001 : Peripheral clock / 4
30 * 0000 : Peripheral clock / 2
31 * 1111 : Peripheral clock (sup_pclk set '1')
32 */
33
34struct sh_mmcif_plat_data {
35 void (*set_pwr)(struct platform_device *pdev, int state);
36 void (*down_pwr)(struct platform_device *pdev);
Arnd Hannemann777271d2010-08-24 17:27:01 +020037 int (*get_cd)(struct platform_device *pdef);
Yusuke Godafdc50a92010-05-26 14:41:59 -070038 u8 sup_pclk; /* 1 :SH7757, 0: SH7724/SH7372 */
39 unsigned long caps;
40 u32 ocr;
41};
42
Magnus Damm487d9fc2010-05-18 14:42:51 +000043#define MMCIF_CE_CMD_SET 0x00000000
44#define MMCIF_CE_ARG 0x00000008
45#define MMCIF_CE_ARG_CMD12 0x0000000C
46#define MMCIF_CE_CMD_CTRL 0x00000010
47#define MMCIF_CE_BLOCK_SET 0x00000014
48#define MMCIF_CE_CLK_CTRL 0x00000018
49#define MMCIF_CE_BUF_ACC 0x0000001C
50#define MMCIF_CE_RESP3 0x00000020
51#define MMCIF_CE_RESP2 0x00000024
52#define MMCIF_CE_RESP1 0x00000028
53#define MMCIF_CE_RESP0 0x0000002C
54#define MMCIF_CE_RESP_CMD12 0x00000030
55#define MMCIF_CE_DATA 0x00000034
56#define MMCIF_CE_INT 0x00000040
57#define MMCIF_CE_INT_MASK 0x00000044
58#define MMCIF_CE_HOST_STS1 0x00000048
59#define MMCIF_CE_HOST_STS2 0x0000004C
60#define MMCIF_CE_VERSION 0x0000007C
61
Simon Hormanda1d39e2010-11-09 17:47:02 +090062/* CE_BUF_ACC */
63#define BUF_ACC_DMAWEN (1 << 25)
64#define BUF_ACC_DMAREN (1 << 24)
65#define BUF_ACC_BUSW_32 (0 << 17)
66#define BUF_ACC_BUSW_16 (1 << 17)
67#define BUF_ACC_ATYP (1 << 16)
68
69/* CE_CLK_CTRL */
70#define CLK_ENABLE (1 << 24) /* 1: output mmc clock */
71#define CLK_CLEAR ((1 << 19) | (1 << 18) | (1 << 17) | (1 << 16))
72#define CLK_SUP_PCLK ((1 << 19) | (1 << 18) | (1 << 17) | (1 << 16))
Simon Horman22efa0f2010-11-27 00:11:55 +000073#define CLKDIV_4 (1<<16) /* mmc clock frequency.
74 * n: bus clock/(2^(n+1)) */
75#define CLKDIV_256 (7<<16) /* mmc clock frequency. (see above) */
Simon Hormanda1d39e2010-11-09 17:47:02 +090076#define SRSPTO_256 ((1 << 13) | (0 << 12)) /* resp timeout */
77#define SRBSYTO_29 ((1 << 11) | (1 << 10) | \
78 (1 << 9) | (1 << 8)) /* resp busy timeout */
79#define SRWDTO_29 ((1 << 7) | (1 << 6) | \
80 (1 << 5) | (1 << 4)) /* read/write timeout */
81#define SCCSTO_29 ((1 << 3) | (1 << 2) | \
82 (1 << 1) | (1 << 0)) /* ccs timeout */
83
84/* CE_VERSION */
85#define SOFT_RST_ON (1 << 31)
Simon Horman1ae0aff2010-11-26 23:02:58 +000086#define SOFT_RST_OFF 0
Simon Hormanda1d39e2010-11-09 17:47:02 +090087
Paul Mundt2f6ba572010-11-04 12:21:25 +090088static inline u32 sh_mmcif_readl(void __iomem *addr, int reg)
Magnus Damm487d9fc2010-05-18 14:42:51 +000089{
90 return readl(addr + reg);
91}
92
Paul Mundt2f6ba572010-11-04 12:21:25 +090093static inline void sh_mmcif_writel(void __iomem *addr, int reg, u32 val)
Magnus Damm487d9fc2010-05-18 14:42:51 +000094{
95 writel(val, addr + reg);
96}
97
Magnus Damm8a768952010-05-18 14:43:04 +000098#define SH_MMCIF_BBS 512 /* boot block size */
99
Simon Horman9f843702010-12-06 00:12:44 +0000100enum { MMCIF_PROGRESS_ENTER, MMCIF_PROGRESS_INIT,
101 MMCIF_PROGRESS_LOAD, MMCIF_PROGRESS_DONE };
102
Paul Mundt2f6ba572010-11-04 12:21:25 +0900103static inline void sh_mmcif_boot_cmd_send(void __iomem *base,
Magnus Damm8a768952010-05-18 14:43:04 +0000104 unsigned long cmd, unsigned long arg)
105{
106 sh_mmcif_writel(base, MMCIF_CE_INT, 0);
107 sh_mmcif_writel(base, MMCIF_CE_ARG, arg);
108 sh_mmcif_writel(base, MMCIF_CE_CMD_SET, cmd);
109}
110
Paul Mundt2f6ba572010-11-04 12:21:25 +0900111static inline int sh_mmcif_boot_cmd_poll(void __iomem *base, unsigned long mask)
Magnus Damm8a768952010-05-18 14:43:04 +0000112{
113 unsigned long tmp;
114 int cnt;
115
116 for (cnt = 0; cnt < 1000000; cnt++) {
117 tmp = sh_mmcif_readl(base, MMCIF_CE_INT);
118 if (tmp & mask) {
119 sh_mmcif_writel(base, MMCIF_CE_INT, tmp & ~mask);
120 return 0;
121 }
122 }
123
124 return -1;
125}
126
Paul Mundt2f6ba572010-11-04 12:21:25 +0900127static inline int sh_mmcif_boot_cmd(void __iomem *base,
Magnus Damm8a768952010-05-18 14:43:04 +0000128 unsigned long cmd, unsigned long arg)
129{
130 sh_mmcif_boot_cmd_send(base, cmd, arg);
131 return sh_mmcif_boot_cmd_poll(base, 0x00010000);
132}
133
Paul Mundt2f6ba572010-11-04 12:21:25 +0900134static inline int sh_mmcif_boot_do_read_single(void __iomem *base,
Magnus Damm8a768952010-05-18 14:43:04 +0000135 unsigned int block_nr,
136 unsigned long *buf)
137{
138 int k;
139
140 /* CMD13 - Status */
141 sh_mmcif_boot_cmd(base, 0x0d400000, 0x00010000);
142
143 if (sh_mmcif_readl(base, MMCIF_CE_RESP0) != 0x0900)
144 return -1;
145
146 /* CMD17 - Read */
147 sh_mmcif_boot_cmd(base, 0x11480000, block_nr * SH_MMCIF_BBS);
148 if (sh_mmcif_boot_cmd_poll(base, 0x00100000) < 0)
149 return -1;
150
151 for (k = 0; k < (SH_MMCIF_BBS / 4); k++)
152 buf[k] = sh_mmcif_readl(base, MMCIF_CE_DATA);
153
154 return 0;
155}
156
Paul Mundt2f6ba572010-11-04 12:21:25 +0900157static inline int sh_mmcif_boot_do_read(void __iomem *base,
Magnus Damm8a768952010-05-18 14:43:04 +0000158 unsigned long first_block,
159 unsigned long nr_blocks,
160 void *buf)
161{
162 unsigned long k;
163 int ret = 0;
164
165 /* CMD16 - Set the block size */
166 sh_mmcif_boot_cmd(base, 0x10400000, SH_MMCIF_BBS);
167
168 for (k = 0; !ret && k < nr_blocks; k++)
169 ret = sh_mmcif_boot_do_read_single(base, first_block + k,
170 buf + (k * SH_MMCIF_BBS));
171
172 return ret;
173}
174
Paul Mundt2f6ba572010-11-04 12:21:25 +0900175static inline void sh_mmcif_boot_init(void __iomem *base)
Magnus Damm8a768952010-05-18 14:43:04 +0000176{
Magnus Damm8a768952010-05-18 14:43:04 +0000177 /* reset */
Simon Horman1ae0aff2010-11-26 23:02:58 +0000178 sh_mmcif_writel(base, MMCIF_CE_VERSION, SOFT_RST_ON);
179 sh_mmcif_writel(base, MMCIF_CE_VERSION, SOFT_RST_OFF);
Magnus Damm8a768952010-05-18 14:43:04 +0000180
181 /* byte swap */
Simon Hormanda1d39e2010-11-09 17:47:02 +0900182 sh_mmcif_writel(base, MMCIF_CE_BUF_ACC, BUF_ACC_ATYP);
Magnus Damm8a768952010-05-18 14:43:04 +0000183
184 /* Set block size in MMCIF hardware */
185 sh_mmcif_writel(base, MMCIF_CE_BLOCK_SET, SH_MMCIF_BBS);
186
Simon Horman22efa0f2010-11-27 00:11:55 +0000187 /* Enable the clock, set it to Bus clock/256 (about 325Khz). */
Simon Hormanda1d39e2010-11-09 17:47:02 +0900188 sh_mmcif_writel(base, MMCIF_CE_CLK_CTRL,
Simon Horman22efa0f2010-11-27 00:11:55 +0000189 CLK_ENABLE | CLKDIV_256 | SRSPTO_256 |
190 SRBSYTO_29 | SRWDTO_29 | SCCSTO_29);
Magnus Damm8a768952010-05-18 14:43:04 +0000191
192 /* CMD0 */
193 sh_mmcif_boot_cmd(base, 0x00000040, 0);
194
195 /* CMD1 - Get OCR */
196 do {
197 sh_mmcif_boot_cmd(base, 0x01405040, 0x40300000); /* CMD1 */
198 } while ((sh_mmcif_readl(base, MMCIF_CE_RESP0) & 0x80000000)
199 != 0x80000000);
200
201 /* CMD2 - Get CID */
202 sh_mmcif_boot_cmd(base, 0x02806040, 0);
203
204 /* CMD3 - Set card relative address */
205 sh_mmcif_boot_cmd(base, 0x03400040, 0x00010000);
206}
207
Paul Mundt2f6ba572010-11-04 12:21:25 +0900208static inline void sh_mmcif_boot_slurp(void __iomem *base,
Magnus Damm8a768952010-05-18 14:43:04 +0000209 unsigned char *buf,
210 unsigned long no_bytes)
211{
212 unsigned long tmp;
213
214 /* In data transfer mode: Set clock to Bus clock/4 (about 20Mhz) */
Simon Horman22efa0f2010-11-27 00:11:55 +0000215 sh_mmcif_writel(base, MMCIF_CE_CLK_CTRL,
216 CLK_ENABLE | CLKDIV_4 | SRSPTO_256 |
217 SRBSYTO_29 | SRWDTO_29 | SCCSTO_29);
Magnus Damm8a768952010-05-18 14:43:04 +0000218
219 /* CMD9 - Get CSD */
220 sh_mmcif_boot_cmd(base, 0x09806000, 0x00010000);
221
222 /* CMD7 - Select the card */
223 sh_mmcif_boot_cmd(base, 0x07400000, 0x00010000);
224
225 tmp = no_bytes / SH_MMCIF_BBS;
226 tmp += (no_bytes % SH_MMCIF_BBS) ? 1 : 0;
227
228 sh_mmcif_boot_do_read(base, 512, tmp, buf);
229}
230
Yusuke Godafdc50a92010-05-26 14:41:59 -0700231#endif /* __SH_MMCIF_H__ */