blob: a14b3b5b27df158e24cf7f4d54ad5f18d2da4f80 [file] [log] [blame]
Channagoud Kadabie9168e82014-01-28 21:33:34 -08001/* Copyright (c) 2013-2014, The Linux Foundation. All rights reserved.
Channagoud Kadabi74ed8352013-03-11 13:12:05 -07002 *
3 * Redistribution and use in source and binary forms, with or without
4 * modification, are permitted provided that the following conditions are
5 * met:
6 * * Redistributions of source code must retain the above copyright
7 * notice, this list of conditions and the following disclaimer.
8 * * Redistributions in binary form must reproduce the above
9 * copyright notice, this list of conditions and the following
10 * disclaimer in the documentation and/or other materials provided
11 * with the distribution.
12 * * Neither the name of The Linux Foundation nor the names of its
13 * contributors may be used to endorse or promote products derived
14 * from this software without specific prior written permission.
15 *
16 * THIS SOFTWARE IS PROVIDED "AS IS" AND ANY EXPRESS OR IMPLIED
17 * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
18 * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NON-INFRINGEMENT
19 * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS
20 * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
21 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
22 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR
23 * BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
24 * WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE
25 * OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN
26 * IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
27 */
28
29#ifndef __PLATFORM_SDHCI_H_
30#define __PLATFORM_SDHCI_H_
31
32#include <reg.h>
33#include <bits.h>
Channagoud Kadabi89902512013-05-14 13:22:06 -070034#include <kernel/event.h>
Channagoud Kadabi74ed8352013-03-11 13:12:05 -070035
36/*
37 * Capabilities for the host controller
38 * These values are read from the capabilities
39 * register in the controller
40 */
41struct host_caps {
42 uint32_t base_clk_rate; /* Max clock rate supported */
43 uint32_t max_blk_len; /* Max block len supported */
44 uint8_t bus_width_8bit; /* 8 Bit mode supported */
45 uint8_t adma_support; /* Adma support */
46 uint8_t voltage; /* Supported voltage */
47 uint8_t sdr_support; /* Single Data rate */
48 uint8_t ddr_support; /* Dual Data rate */
49 uint8_t sdr50_support; /* UHS mode, with 100 MHZ clock */
Channagoud Kadabi9b8f8fc2013-07-26 12:02:49 -070050 uint8_t sdr104_support; /* UHS mode, with 200 MHZ clock */
Channagoud Kadabie9168e82014-01-28 21:33:34 -080051 uint8_t hs400_support; /* Hs400 mode, with 400 MHZ clock */
Channagoud Kadabi74ed8352013-03-11 13:12:05 -070052};
53
54/*
55 * sdhci host structure, holding information about host
56 * controller parameters
57 */
58struct sdhci_host {
Channagoud Kadabi9b8f8fc2013-07-26 12:02:49 -070059 uint32_t base; /* Base address for the host */
60 uint32_t cur_clk_rate; /* Running clock rate */
61 uint32_t timing; /* current timing for the host */
62 bool tuning_in_progress; /* Tuning is being executed */
63 event_t* sdhc_event; /* Event for power control irqs */
64 struct host_caps caps; /* Host capabilities */
65 struct sdhci_msm_data *msm_host; /* MSM specific host info */
Channagoud Kadabi74ed8352013-03-11 13:12:05 -070066};
67
68/*
69 * Data pointer to be read/written
70 */
71struct mmc_data {
72 void *data_ptr; /* Points to stream of data */
Channagoud Kadabi709ce1c2013-05-29 15:19:15 -070073 uint32_t blk_sz; /* Block size for the data */
Channagoud Kadabi74ed8352013-03-11 13:12:05 -070074 uint32_t num_blocks; /* num of blocks, each always of size SDHCI_MMC_BLK_SZ */
75};
76
77/*
78 * mmc command structure as per the spec
79 */
80struct mmc_command {
81 uint16_t cmd_index; /* Command index */
82 uint32_t argument; /* Command argument */
83 uint8_t data_present; /* Command has data */
84 uint8_t cmd_type; /* command type */
85 uint16_t resp_type; /* Response type of the command */
86 uint32_t resp[4]; /* 128 bit response value */
87 uint32_t trans_mode; /* Transfer mode, read/write */
88 uint32_t cmd_retry; /* Retry the command, if card is busy */
Channagoud Kadabi89902512013-05-14 13:22:06 -070089 uint32_t cmd23_support; /* If card supports cmd23 */
Channagoud Kadabi74ed8352013-03-11 13:12:05 -070090 struct mmc_data data; /* Data pointer */
91};
92
93/*
94 * Descriptor table for adma
95 */
96struct desc_entry {
97 uint16_t tran_att; /* Attribute for transfer data */
98 uint16_t len; /* Length of data */
Channagoud Kadabi2e233e72013-06-06 14:09:57 -070099 uint32_t addr; /* Address of the data */
Channagoud Kadabi74ed8352013-03-11 13:12:05 -0700100};
101
102/*
103 * Command types for sdhci
104 */
105enum {
106 SDHCI_CMD_TYPE_NORMAL = 0,
107 SDHCI_CMD_TYPE_SUSPEND,
108 SDHCI_CMD_TYPE_RESUME,
109 SDHCI_CMD_TYPE_ABORT,
110} sdhci_cmd_type;
111
112/*
113 * Response type values for sdhci
114 */
115enum {
116 SDHCI_CMD_RESP_NONE = 0,
117 SDHCI_CMD_RESP_136,
118 SDHCI_CMD_RESP_48,
119 SDHCI_CMD_RESP_48_BUSY,
120} sdhci_resp_type;
121
122
123/*
124 * Helper macros for writing byte, word & long registers
125 */
126#define REG_READ8(host, a) readb(host->base + a);
127#define REG_WRITE8(host, v, a) writeb(v, (host->base + a))
128
129#define REG_READ32(host, a) readl(host->base + a)
130#define REG_WRITE32(host, v, a) writel(v, (host->base + a))
Channagoud Kadabi9b8f8fc2013-07-26 12:02:49 -0700131#define REG_RMW32(host, a, s, w, v) RMWREG32((host->base + a), s, w, v)
Channagoud Kadabi74ed8352013-03-11 13:12:05 -0700132
133#define REG_READ16(host, a) readhw(host->base + a)
134#define REG_WRITE16(host, v, a) writehw(v, (host->base + a))
135
136/*
137 * SDHCI registers, as per the host controller spec v 3.0
138 */
139#define SDHCI_ARG2_REG (0x000)
140#define SDHCI_BLKSZ_REG (0x004)
141#define SDHCI_BLK_CNT_REG (0x006)
142#define SDHCI_ARGUMENT_REG (0x008)
143#define SDHCI_TRANS_MODE_REG (0x00C)
144#define SDHCI_CMD_REG (0x00E)
145#define SDHCI_RESP_REG (0x010)
146#define SDHCI_PRESENT_STATE_REG (0x024)
147#define SDHCI_HOST_CTRL1_REG (0x028)
148#define SDHCI_PWR_CTRL_REG (0x029)
149#define SDHCI_CLK_CTRL_REG (0x02C)
150#define SDHCI_TIMEOUT_REG (0x02E)
151#define SDHCI_RESET_REG (0x02F)
152#define SDHCI_NRML_INT_STS_REG (0x030)
153#define SDHCI_ERR_INT_STS_REG (0x032)
154#define SDHCI_NRML_INT_STS_EN_REG (0x034)
155#define SDHCI_ERR_INT_STS_EN_REG (0x036)
156#define SDHCI_NRML_INT_SIG_EN_REG (0x038)
157#define SDHCI_ERR_INT_SIG_EN_REG (0x03A)
158#define SDHCI_HOST_CTRL2_REG (0x03E)
159#define SDHCI_CAPS_REG1 (0x040)
160#define SDHCI_CAPS_REG2 (0x044)
161#define SDHCI_ADM_ADDR_REG (0x058)
162
163/*
164 * Helper macros for register writes
165 */
166#define SDHCI_SOFT_RESET BIT(0)
167#define SOFT_RESET_CMD BIT(1)
168#define SOFT_RESET_DATA BIT(2)
Channagoud Kadabi7ad70ea2013-08-08 13:51:04 -0700169#define SDHCI_RESET_MAX_TIMEOUT 0x64
Channagoud Kadabi74ed8352013-03-11 13:12:05 -0700170#define SDHCI_1_8_VOL_SET BIT(3)
171
172/*
173 * Interrupt related
174 */
175#define SDHCI_NRML_INT_STS_EN 0x000B
176#define SDHCI_ERR_INT_STS_EN 0xFFFF
177#define SDHCI_NRML_INT_SIG_EN 0x000B
178#define SDHCI_ERR_INT_SIG_EN 0xFFFF
179
180#define SDCC_HC_INT_CARD_REMOVE BIT(7)
181#define SDCC_HC_INT_CARD_INSERT BIT(6)
182
183/*
184 * HC mode enable/disable
185 */
186#define SDHCI_HC_MODE_EN BIT(0)
187#define SDHCI_HC_MODE_DIS (0 << 1)
188
189/*
190 * Clk control related
191 */
192#define SDHCI_CLK_MAX_DIV 2046
193#define SDHCI_SDCLK_FREQ_SEL 8
194#define SDHCI_SDCLK_UP_BIT_SEL 6
195#define SDHCI_SDCLK_FREQ_MASK 0xFF
196#define SDHC_SDCLK_UP_BIT_MASK 0x300
197#define SDHCI_INT_CLK_EN BIT(0)
198#define SDHCI_CLK_STABLE_MASK BIT(1)
199#define SDHCI_CLK_STABLE BIT(1)
200#define SDHCI_CLK_EN BIT(2)
201#define SDHCI_CLK_DIS (0 << 2)
202#define SDHCI_CLK_RATE_MASK 0x0000FF00
203#define SDHCI_CLK_RATE_BIT 8
204
205#define SDHCI_CMD_ACT BIT(0)
206#define SDHCI_DAT_ACT BIT(1)
207
208/*
209 * Bus voltage related macros
210 */
211#define SDHCI_BUS_VOL_SEL 1
212#define SDHCI_BUS_PWR_EN BIT(0)
213#define SDHCI_VOL_1_8 5
214#define SDHCI_VOL_3_0 6
215#define SDHCI_VOL_3_3 7
216#define SDHCI_3_3_VOL_MASK 0x01000000
217#define SDHCI_3_0_VOL_MASK 0x02000000
218#define SDHCI_1_8_VOL_MASK 0x04000000
219
220/*
221 * Bus width related macros
222 */
223#define SDHCI_8BIT_WIDTH_MASK 0x00040000
224
225#define SDHCI_BUS_WITDH_1BIT (0)
226#define SDHCI_BUS_WITDH_4BIT BIT(1)
227#define SDHCI_BUS_WITDH_8BIT BIT(5)
228
229/*
230 * Adma related macros
231 */
232#define SDHCI_BLK_LEN_MASK 0x00030000
233#define SDHCI_BLK_LEN_BIT 16
234#define SDHCI_BLK_ADMA_MASK 0x00080000
235#define SDHCI_INT_STS_TRANS_COMPLETE BIT(1)
236#define SDHCI_STATE_CMD_DAT_MASK 0x0003
237#define SDHCI_INT_STS_CMD_COMPLETE BIT(0)
238#define SDHCI_ERR_INT_STAT_MASK 0x8000
239#define SDHCI_ADMA_DESC_LINE_SZ 65536
240#define SDHCI_ADMA_MAX_TRANS_SZ (65535 * 512)
241#define SDHCI_ADMA_TRANS_VALID BIT(0)
242#define SDHCI_ADMA_TRANS_END BIT(1)
243#define SDHCI_ADMA_TRANS_DATA BIT(5)
244#define SDHCI_MMC_BLK_SZ 512
245#define SDHCI_MMC_CUR_BLK_CNT_BIT 16
246#define SDHCI_MMC_BLK_SZ_BIT 0
247#define SDHCI_TRANS_MULTI BIT(5)
248#define SDHCI_TRANS_SINGLE (0 << 5)
249#define SDHCI_BLK_CNT_EN BIT(1)
250#define SDHCI_DMA_EN BIT(0)
251#define SDHCI_AUTO_CMD23_EN BIT(3)
Channagoud Kadabi89902512013-05-14 13:22:06 -0700252#define SDHCI_AUTO_CMD12_EN BIT(2)
Channagoud Kadabi74ed8352013-03-11 13:12:05 -0700253#define SDHCI_ADMA_32BIT BIT(4)
254
255/*
256 * Command related macros
257 */
258#define SDHCI_CMD_RESP_TYPE_SEL_BIT 0
259#define SDHCI_CMD_CRC_CHECK_BIT 3
260#define SDHCI_CMD_IDX_CHECK_BIT 4
261#define SDHCI_CMD_DATA_PRESENT_BIT 5
262#define SDHCI_CMD_CMD_TYPE_BIT 6
263#define SDHCI_CMD_CMD_IDX_BIT 8
264#define SDHCI_CMD_TIMEOUT_MASK BIT(0)
265#define SDHCI_CMD_CRC_MASK BIT(1)
266#define SDHCI_CMD_END_BIT_MASK BIT(2)
267#define SDHCI_CMD_IDX_MASK BIT(3)
268#define SDHCI_DAT_TIMEOUT_MASK BIT(4)
269#define SDHCI_DAT_CRC_MASK BIT(5)
270#define SDHCI_DAT_END_BIT_MASK BIT(6)
271#define SDHCI_CUR_LIM_MASK BIT(7)
272#define SDHCI_AUTO_CMD12_MASK BIT(8)
273#define SDHCI_ADMA_MASK BIT(9)
274#define SDHCI_READ_MODE BIT(4)
275#define SDHCI_SWITCH_CMD 6
Channagoud Kadabi131b7172013-06-18 16:23:49 -0700276#define SDHCI_CMD_TIMEOUT 0xF
Channagoud Kadabi74ed8352013-03-11 13:12:05 -0700277#define SDHCI_MAX_CMD_RETRY 10000
Channagoud Kadabi9b8f8fc2013-07-26 12:02:49 -0700278#define SDHCI_MAX_TRANS_RETRY 10000
Channagoud Kadabi74ed8352013-03-11 13:12:05 -0700279
280#define SDHCI_PREP_CMD(c, f) ((((c) & 0xff) << 8) | ((f) & 0xff))
281
282/*
283 * command response related
284 */
285#define SDHCI_RESP_LSHIFT 8
286#define SDHCI_RESP_RSHIFT 24
287
288/*
289 * Power control relatd macros
290 */
Channagoud Kadabi74ed8352013-03-11 13:12:05 -0700291#define SDCC_HC_PWR_CTRL_INT 0xF
292#define SDCC_HC_BUS_ON BIT(0)
293#define SDCC_HC_BUS_OFF BIT(1)
294#define SDCC_HC_BUS_ON_OFF_SUCC BIT(0)
295#define SDCC_HC_IO_SIG_LOW BIT(2)
296#define SDCC_HC_IO_SIG_HIGH BIT(3)
297#define SDCC_HC_IO_SIG_SUCC BIT(2)
298
299/*
300 * Command response
301 */
302#define SDHCI_CMD_RESP_NONE 0
303#define SDHCI_CMD_RESP_R1 BIT(0)
304#define SDHCI_CMD_RESP_R1B BIT(1)
305#define SDHCI_CMD_RESP_R2 BIT(2)
306#define SDHCI_CMD_RESP_R3 BIT(3)
307#define SDHCI_CMD_RESP_R6 BIT(6)
308#define SDHCI_CMD_RESP_R7 BIT(7)
309
310/*
311 * Clock Divider values
312 */
313#define SDHCI_CLK_400KHZ 400000
314#define SDHCI_CLK_25MHZ 25000000
315#define SDHCI_CLK_50MHZ 50000000
316#define SDHCI_CLK_100MHZ 100000000
317#define SDHCI_CLK_200MHZ 200000000
Channagoud Kadabi9b8f8fc2013-07-26 12:02:49 -0700318#define SDHCI_CLK_400MHZ 400000000
319
320/* UHS macros */
321#define SDHCI_UHS_MODE_MASK 0x0007
Channagoud Kadabi74ed8352013-03-11 13:12:05 -0700322
323/* DDR mode related macros */
Channagoud Kadabi9b8f8fc2013-07-26 12:02:49 -0700324#define SDHCI_DDR50_MODE_EN 0x0004
325#define SDHCI_DDR50_MODE_MASK BIT(2)
Channagoud Kadabi74ed8352013-03-11 13:12:05 -0700326
327/* HS200/SDR50 mode related macros */
Channagoud Kadabi9b8f8fc2013-07-26 12:02:49 -0700328#define SDHCI_SDR25_MODE_EN 0x0001
329#define SDHCI_SDR12_MODE_EN 0x0000
Channagoud Kadabi74ed8352013-03-11 13:12:05 -0700330#define SDHCI_SDR50_MODE_MASK BIT(0)
331#define SDHCI_SDR50_MODE_EN 0x0002
332
Channagoud Kadabi9b8f8fc2013-07-26 12:02:49 -0700333#define SDHCI_SDR104_MODE_MASK BIT(1)
334#define SDHCI_SDR104_MODE_EN 0x0003
335
336#define SDHCI_SDR104_MODE 0x3
337#define SDHCI_SDR50_MODE 0x2
338#define SDHCI_DDR50_MODE 0x4
339#define SDHCI_SDR25_MODE 0x1
340#define SDHCI_SDR12_MODE 0x0
341
Channagoud Kadabi74ed8352013-03-11 13:12:05 -0700342/*
343 * APIs and macros exposed for mmc/sd drivers
344 */
345#define SDHCI_MMC_WRITE 0
346#define SDHCI_MMC_READ 1
347
348#define DATA_BUS_WIDTH_1BIT 0
349#define DATA_BUS_WIDTH_4BIT 1
350#define DATA_BUS_WIDTH_8BIT 2
351#define DATA_DDR_BUS_WIDTH_4BIT 5
352#define DATA_DDR_BUS_WIDTH_8BIT 6
353
354/* API: to initialize the controller */
355void sdhci_init(struct sdhci_host *);
356/* API: Send the command & transfer data using adma */
357uint32_t sdhci_send_command(struct sdhci_host *, struct mmc_command *);
358/* API: Set the bus width for the contoller */
359uint8_t sdhci_set_bus_width(struct sdhci_host *, uint16_t);
360/* API: Clock supply for the controller */
361uint32_t sdhci_clk_supply(struct sdhci_host *, uint32_t);
Channagoud Kadabi9b8f8fc2013-07-26 12:02:49 -0700362/* API: To enable SDR/DDR mode */
363void sdhci_set_uhs_mode(struct sdhci_host *, uint32_t);
364/* API: Soft reset for the controller */
365void sdhci_reset(struct sdhci_host *host, uint8_t mask);
Channagoud Kadabi74ed8352013-03-11 13:12:05 -0700366#endif