blob: f216a8879b5832275f2d2ed8d951e219d2cf9d0c [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/io.h>
Guennadi Liakhovetskia782d682010-11-24 10:05:22 +000018#include <linux/platform_device.h>
19#include <linux/sh_dma.h>
Magnus Damm487d9fc2010-05-18 14:42:51 +000020
Yusuke Godafdc50a92010-05-26 14:41:59 -070021/*
22 * MMCIF : CE_CLK_CTRL [19:16]
23 * 1000 : Peripheral clock / 512
24 * 0111 : Peripheral clock / 256
25 * 0110 : Peripheral clock / 128
26 * 0101 : Peripheral clock / 64
27 * 0100 : Peripheral clock / 32
28 * 0011 : Peripheral clock / 16
29 * 0010 : Peripheral clock / 8
30 * 0001 : Peripheral clock / 4
31 * 0000 : Peripheral clock / 2
32 * 1111 : Peripheral clock (sup_pclk set '1')
33 */
34
Guennadi Liakhovetskia782d682010-11-24 10:05:22 +000035struct sh_mmcif_dma {
36 struct sh_dmae_slave chan_priv_tx;
37 struct sh_dmae_slave chan_priv_rx;
38};
39
Yusuke Godafdc50a92010-05-26 14:41:59 -070040struct sh_mmcif_plat_data {
41 void (*set_pwr)(struct platform_device *pdev, int state);
42 void (*down_pwr)(struct platform_device *pdev);
Arnd Hannemann777271d2010-08-24 17:27:01 +020043 int (*get_cd)(struct platform_device *pdef);
Guennadi Liakhovetskia782d682010-11-24 10:05:22 +000044 struct sh_mmcif_dma *dma;
45 u8 sup_pclk; /* 1 :SH7757, 0: SH7724/SH7372 */
46 unsigned long caps;
47 u32 ocr;
Yusuke Godafdc50a92010-05-26 14:41:59 -070048};
49
Magnus Damm487d9fc2010-05-18 14:42:51 +000050#define MMCIF_CE_CMD_SET 0x00000000
51#define MMCIF_CE_ARG 0x00000008
52#define MMCIF_CE_ARG_CMD12 0x0000000C
53#define MMCIF_CE_CMD_CTRL 0x00000010
54#define MMCIF_CE_BLOCK_SET 0x00000014
55#define MMCIF_CE_CLK_CTRL 0x00000018
56#define MMCIF_CE_BUF_ACC 0x0000001C
57#define MMCIF_CE_RESP3 0x00000020
58#define MMCIF_CE_RESP2 0x00000024
59#define MMCIF_CE_RESP1 0x00000028
60#define MMCIF_CE_RESP0 0x0000002C
61#define MMCIF_CE_RESP_CMD12 0x00000030
62#define MMCIF_CE_DATA 0x00000034
63#define MMCIF_CE_INT 0x00000040
64#define MMCIF_CE_INT_MASK 0x00000044
65#define MMCIF_CE_HOST_STS1 0x00000048
66#define MMCIF_CE_HOST_STS2 0x0000004C
67#define MMCIF_CE_VERSION 0x0000007C
68
Simon Hormanda1d39e2010-11-09 17:47:02 +090069/* CE_BUF_ACC */
70#define BUF_ACC_DMAWEN (1 << 25)
71#define BUF_ACC_DMAREN (1 << 24)
72#define BUF_ACC_BUSW_32 (0 << 17)
73#define BUF_ACC_BUSW_16 (1 << 17)
74#define BUF_ACC_ATYP (1 << 16)
75
76/* CE_CLK_CTRL */
77#define CLK_ENABLE (1 << 24) /* 1: output mmc clock */
78#define CLK_CLEAR ((1 << 19) | (1 << 18) | (1 << 17) | (1 << 16))
79#define CLK_SUP_PCLK ((1 << 19) | (1 << 18) | (1 << 17) | (1 << 16))
80#define SRSPTO_256 ((1 << 13) | (0 << 12)) /* resp timeout */
81#define SRBSYTO_29 ((1 << 11) | (1 << 10) | \
82 (1 << 9) | (1 << 8)) /* resp busy timeout */
83#define SRWDTO_29 ((1 << 7) | (1 << 6) | \
84 (1 << 5) | (1 << 4)) /* read/write timeout */
85#define SCCSTO_29 ((1 << 3) | (1 << 2) | \
86 (1 << 1) | (1 << 0)) /* ccs timeout */
87
88/* CE_VERSION */
89#define SOFT_RST_ON (1 << 31)
90#define SOFT_RST_OFF ~SOFT_RST_ON
91
Paul Mundt2f6ba572010-11-04 12:21:25 +090092static inline u32 sh_mmcif_readl(void __iomem *addr, int reg)
Magnus Damm487d9fc2010-05-18 14:42:51 +000093{
94 return readl(addr + reg);
95}
96
Paul Mundt2f6ba572010-11-04 12:21:25 +090097static inline void sh_mmcif_writel(void __iomem *addr, int reg, u32 val)
Magnus Damm487d9fc2010-05-18 14:42:51 +000098{
99 writel(val, addr + reg);
100}
101
Magnus Damm8a768952010-05-18 14:43:04 +0000102#define SH_MMCIF_BBS 512 /* boot block size */
103
Paul Mundt2f6ba572010-11-04 12:21:25 +0900104static inline void sh_mmcif_boot_cmd_send(void __iomem *base,
Magnus Damm8a768952010-05-18 14:43:04 +0000105 unsigned long cmd, unsigned long arg)
106{
107 sh_mmcif_writel(base, MMCIF_CE_INT, 0);
108 sh_mmcif_writel(base, MMCIF_CE_ARG, arg);
109 sh_mmcif_writel(base, MMCIF_CE_CMD_SET, cmd);
110}
111
Paul Mundt2f6ba572010-11-04 12:21:25 +0900112static inline int sh_mmcif_boot_cmd_poll(void __iomem *base, unsigned long mask)
Magnus Damm8a768952010-05-18 14:43:04 +0000113{
114 unsigned long tmp;
115 int cnt;
116
117 for (cnt = 0; cnt < 1000000; cnt++) {
118 tmp = sh_mmcif_readl(base, MMCIF_CE_INT);
119 if (tmp & mask) {
120 sh_mmcif_writel(base, MMCIF_CE_INT, tmp & ~mask);
121 return 0;
122 }
123 }
124
125 return -1;
126}
127
Paul Mundt2f6ba572010-11-04 12:21:25 +0900128static inline int sh_mmcif_boot_cmd(void __iomem *base,
Magnus Damm8a768952010-05-18 14:43:04 +0000129 unsigned long cmd, unsigned long arg)
130{
131 sh_mmcif_boot_cmd_send(base, cmd, arg);
132 return sh_mmcif_boot_cmd_poll(base, 0x00010000);
133}
134
Paul Mundt2f6ba572010-11-04 12:21:25 +0900135static inline int sh_mmcif_boot_do_read_single(void __iomem *base,
Magnus Damm8a768952010-05-18 14:43:04 +0000136 unsigned int block_nr,
137 unsigned long *buf)
138{
139 int k;
140
141 /* CMD13 - Status */
142 sh_mmcif_boot_cmd(base, 0x0d400000, 0x00010000);
143
144 if (sh_mmcif_readl(base, MMCIF_CE_RESP0) != 0x0900)
145 return -1;
146
147 /* CMD17 - Read */
148 sh_mmcif_boot_cmd(base, 0x11480000, block_nr * SH_MMCIF_BBS);
149 if (sh_mmcif_boot_cmd_poll(base, 0x00100000) < 0)
150 return -1;
151
152 for (k = 0; k < (SH_MMCIF_BBS / 4); k++)
153 buf[k] = sh_mmcif_readl(base, MMCIF_CE_DATA);
154
155 return 0;
156}
157
Paul Mundt2f6ba572010-11-04 12:21:25 +0900158static inline int sh_mmcif_boot_do_read(void __iomem *base,
Magnus Damm8a768952010-05-18 14:43:04 +0000159 unsigned long first_block,
160 unsigned long nr_blocks,
161 void *buf)
162{
163 unsigned long k;
164 int ret = 0;
165
166 /* CMD16 - Set the block size */
167 sh_mmcif_boot_cmd(base, 0x10400000, SH_MMCIF_BBS);
168
169 for (k = 0; !ret && k < nr_blocks; k++)
170 ret = sh_mmcif_boot_do_read_single(base, first_block + k,
171 buf + (k * SH_MMCIF_BBS));
172
173 return ret;
174}
175
Paul Mundt2f6ba572010-11-04 12:21:25 +0900176static inline void sh_mmcif_boot_init(void __iomem *base)
Magnus Damm8a768952010-05-18 14:43:04 +0000177{
178 unsigned long tmp;
179
180 /* reset */
181 tmp = sh_mmcif_readl(base, MMCIF_CE_VERSION);
Simon Hormanda1d39e2010-11-09 17:47:02 +0900182 sh_mmcif_writel(base, MMCIF_CE_VERSION, tmp | SOFT_RST_ON);
183 sh_mmcif_writel(base, MMCIF_CE_VERSION, tmp & SOFT_RST_OFF);
Magnus Damm8a768952010-05-18 14:43:04 +0000184
185 /* byte swap */
Simon Hormanda1d39e2010-11-09 17:47:02 +0900186 sh_mmcif_writel(base, MMCIF_CE_BUF_ACC, BUF_ACC_ATYP);
Magnus Damm8a768952010-05-18 14:43:04 +0000187
188 /* Set block size in MMCIF hardware */
189 sh_mmcif_writel(base, MMCIF_CE_BLOCK_SET, SH_MMCIF_BBS);
190
Simon Hormanda1d39e2010-11-09 17:47:02 +0900191 /* Enable the clock, set it to Bus clock/256 (about 325Khz).
192 * It is unclear where 0x70000 comes from or if it is even needed.
193 * It is there for byte-compatibility with code that is known to
194 * work.
195 */
196 sh_mmcif_writel(base, MMCIF_CE_CLK_CTRL,
197 CLK_ENABLE | SRSPTO_256 | SRBSYTO_29 | SRWDTO_29 |
198 SCCSTO_29 | 0x70000);
Magnus Damm8a768952010-05-18 14:43:04 +0000199
200 /* CMD0 */
201 sh_mmcif_boot_cmd(base, 0x00000040, 0);
202
203 /* CMD1 - Get OCR */
204 do {
205 sh_mmcif_boot_cmd(base, 0x01405040, 0x40300000); /* CMD1 */
206 } while ((sh_mmcif_readl(base, MMCIF_CE_RESP0) & 0x80000000)
207 != 0x80000000);
208
209 /* CMD2 - Get CID */
210 sh_mmcif_boot_cmd(base, 0x02806040, 0);
211
212 /* CMD3 - Set card relative address */
213 sh_mmcif_boot_cmd(base, 0x03400040, 0x00010000);
214}
215
Paul Mundt2f6ba572010-11-04 12:21:25 +0900216static inline void sh_mmcif_boot_slurp(void __iomem *base,
Magnus Damm8a768952010-05-18 14:43:04 +0000217 unsigned char *buf,
218 unsigned long no_bytes)
219{
220 unsigned long tmp;
221
222 /* In data transfer mode: Set clock to Bus clock/4 (about 20Mhz) */
223 sh_mmcif_writel(base, MMCIF_CE_CLK_CTRL, 0x01012fff);
224
225 /* CMD9 - Get CSD */
226 sh_mmcif_boot_cmd(base, 0x09806000, 0x00010000);
227
228 /* CMD7 - Select the card */
229 sh_mmcif_boot_cmd(base, 0x07400000, 0x00010000);
230
231 tmp = no_bytes / SH_MMCIF_BBS;
232 tmp += (no_bytes % SH_MMCIF_BBS) ? 1 : 0;
233
234 sh_mmcif_boot_do_read(base, 512, tmp, buf);
235}
236
Yusuke Godafdc50a92010-05-26 14:41:59 -0700237#endif /* __SH_MMCIF_H__ */