2 * Driver for Blackfin on-chip SDH controller
4 * Copyright (c) 2008-2009 Analog Devices Inc.
6 * Licensed under the GPL-2 or later.
15 #include <asm/errno.h>
16 #include <asm/byteorder.h>
17 #include <asm/blackfin.h>
18 #include <asm/clock.h>
19 #include <asm/portmux.h>
20 #include <asm/mach-common/bits/sdh.h>
21 #include <asm/mach-common/bits/dma.h>
23 #if defined(__ADSPBF50x__) || defined(__ADSPBF51x__) || defined(__ADSPBF60x__)
24 # define bfin_read_SDH_CLK_CTL bfin_read_RSI_CLK_CONTROL
25 # define bfin_write_SDH_CLK_CTL bfin_write_RSI_CLK_CONTROL
26 # define bfin_write_SDH_ARGUMENT bfin_write_RSI_ARGUMENT
27 # define bfin_write_SDH_COMMAND bfin_write_RSI_COMMAND
28 # define bfin_read_SDH_RESPONSE0 bfin_read_RSI_RESPONSE0
29 # define bfin_read_SDH_RESPONSE1 bfin_read_RSI_RESPONSE1
30 # define bfin_read_SDH_RESPONSE2 bfin_read_RSI_RESPONSE2
31 # define bfin_read_SDH_RESPONSE3 bfin_read_RSI_RESPONSE3
32 # define bfin_write_SDH_DATA_TIMER bfin_write_RSI_DATA_TIMER
33 # define bfin_write_SDH_DATA_LGTH bfin_write_RSI_DATA_LGTH
34 # define bfin_read_SDH_DATA_CTL bfin_read_RSI_DATA_CONTROL
35 # define bfin_write_SDH_DATA_CTL bfin_write_RSI_DATA_CONTROL
36 # define bfin_read_SDH_STATUS bfin_read_RSI_STATUS
37 # define bfin_write_SDH_STATUS_CLR bfin_write_RSI_STATUSCL
38 # define bfin_read_SDH_CFG bfin_read_RSI_CONFIG
39 # define bfin_write_SDH_CFG bfin_write_RSI_CONFIG
40 # if defined(__ADSPBF60x__)
41 # define bfin_read_SDH_BLK_SIZE bfin_read_RSI_BLKSZ
42 # define bfin_write_SDH_BLK_SIZE bfin_write_RSI_BLKSZ
43 # define bfin_write_DMA_START_ADDR bfin_write_DMA10_START_ADDR
44 # define bfin_write_DMA_X_COUNT bfin_write_DMA10_X_COUNT
45 # define bfin_write_DMA_X_MODIFY bfin_write_DMA10_X_MODIFY
46 # define bfin_write_DMA_CONFIG bfin_write_DMA10_CONFIG
48 # define bfin_read_SDH_PWR_CTL bfin_read_RSI_PWR_CONTROL
49 # define bfin_write_SDH_PWR_CTL bfin_write_RSI_PWR_CONTROL
50 # define bfin_write_DMA_START_ADDR bfin_write_DMA4_START_ADDR
51 # define bfin_write_DMA_X_COUNT bfin_write_DMA4_X_COUNT
52 # define bfin_write_DMA_X_MODIFY bfin_write_DMA4_X_MODIFY
53 # define bfin_write_DMA_CONFIG bfin_write_DMA4_CONFIG
55 # define PORTMUX_PINS \
56 { P_RSI_DATA0, P_RSI_DATA1, P_RSI_DATA2, P_RSI_DATA3, P_RSI_CMD, P_RSI_CLK, 0 }
57 #elif defined(__ADSPBF54x__)
58 # define bfin_write_DMA_START_ADDR bfin_write_DMA22_START_ADDR
59 # define bfin_write_DMA_X_COUNT bfin_write_DMA22_X_COUNT
60 # define bfin_write_DMA_X_MODIFY bfin_write_DMA22_X_MODIFY
61 # define bfin_write_DMA_CONFIG bfin_write_DMA22_CONFIG
62 # define PORTMUX_PINS \
63 { P_SD_D0, P_SD_D1, P_SD_D2, P_SD_D3, P_SD_CLK, P_SD_CMD, 0 }
65 # error no support for this proc yet
69 sdh_send_cmd(struct mmc *mmc, struct mmc_cmd *mmc_cmd)
71 unsigned int status, timeout;
72 int cmd = mmc_cmd->cmdidx;
73 int flags = mmc_cmd->resp_type;
74 int arg = mmc_cmd->cmdarg;
78 sdh_cmd = cmd | CMD_E;
79 if (flags & MMC_RSP_PRESENT)
81 if (flags & MMC_RSP_136)
84 sdh_cmd |= CMD_DATA0_BUSY;
87 bfin_write_SDH_ARGUMENT(arg);
88 bfin_write_SDH_COMMAND(sdh_cmd);
90 /* wait for a while */
93 if (++timeout > 1000000) {
94 status = CMD_TIME_OUT;
98 status = bfin_read_SDH_STATUS();
99 } while (!(status & (CMD_SENT | CMD_RESP_END | CMD_TIME_OUT |
102 if (flags & MMC_RSP_PRESENT) {
103 mmc_cmd->response[0] = bfin_read_SDH_RESPONSE0();
104 if (flags & MMC_RSP_136) {
105 mmc_cmd->response[1] = bfin_read_SDH_RESPONSE1();
106 mmc_cmd->response[2] = bfin_read_SDH_RESPONSE2();
107 mmc_cmd->response[3] = bfin_read_SDH_RESPONSE3();
111 if (status & CMD_TIME_OUT)
113 else if (status & CMD_CRC_FAIL && flags & MMC_RSP_CRC)
118 bfin_write_SDH_STATUS_CLR(CMD_SENT_STAT | CMD_RESP_END_STAT |
119 CMD_TIMEOUT_STAT | CMD_CRC_FAIL_STAT);
121 /* wait till card ready */
122 while (!(bfin_read_RSI_ESTAT() & SD_CARD_READY))
124 bfin_write_RSI_ESTAT(SD_CARD_READY);
130 /* set data for single block transfer */
131 static int sdh_setup_data(struct mmc *mmc, struct mmc_data *data)
135 unsigned long data_size = data->blocksize * data->blocks;
137 /* Don't support write yet. */
138 if (data->flags & MMC_DATA_WRITE)
141 data_ctl |= ((ffs(data_size) - 1) << 4);
143 bfin_write_SDH_BLK_SIZE(data_size);
146 bfin_write_SDH_DATA_CTL(data_ctl);
147 dma_cfg = WDSIZE_32 | PSIZE_32 | RESTART | WNR | DMAEN;
149 bfin_write_SDH_DATA_TIMER(-1);
151 blackfin_dcache_flush_invalidate_range(data->dest,
152 data->dest + data_size);
154 bfin_write_DMA_START_ADDR(data->dest);
155 bfin_write_DMA_X_COUNT(data_size / 4);
156 bfin_write_DMA_X_MODIFY(4);
157 bfin_write_DMA_CONFIG(dma_cfg);
158 bfin_write_SDH_DATA_LGTH(data_size);
159 /* kick off transfer */
160 bfin_write_SDH_DATA_CTL(bfin_read_SDH_DATA_CTL() | DTX_DMA_E | DTX_E);
166 static int bfin_sdh_request(struct mmc *mmc, struct mmc_cmd *cmd,
167 struct mmc_data *data)
173 ret = sdh_setup_data(mmc, data);
178 ret = sdh_send_cmd(mmc, cmd);
180 bfin_write_SDH_COMMAND(0);
181 bfin_write_DMA_CONFIG(0);
182 bfin_write_SDH_DATA_CTL(0);
184 printf("sending CMD%d failed\n", cmd->cmdidx);
191 status = bfin_read_SDH_STATUS();
192 } while (!(status & (DAT_BLK_END | DAT_END | DAT_TIME_OUT | DAT_CRC_FAIL | RX_OVERRUN)));
194 if (status & DAT_TIME_OUT) {
195 bfin_write_SDH_STATUS_CLR(DAT_TIMEOUT_STAT);
197 } else if (status & (DAT_CRC_FAIL | RX_OVERRUN)) {
198 bfin_write_SDH_STATUS_CLR(DAT_CRC_FAIL_STAT | RX_OVERRUN_STAT);
201 bfin_write_SDH_STATUS_CLR(DAT_BLK_END_STAT | DAT_END_STAT);
204 printf("tranfering data failed\n");
211 static void sdh_set_clk(unsigned long clk)
213 unsigned long sys_clk;
214 unsigned long clk_div;
217 clk_ctl = bfin_read_SDH_CLK_CTL();
220 sys_clk = get_sclk();
221 bfin_write_SDH_CLK_CTL(clk_ctl & ~CLK_E);
222 if (sys_clk % (2 * clk) == 0)
223 clk_div = sys_clk / (2 * clk) - 1;
225 clk_div = sys_clk / (2 * clk);
229 clk_ctl |= (clk_div & 0xff);
231 bfin_write_SDH_CLK_CTL(clk_ctl);
233 bfin_write_SDH_CLK_CTL(clk_ctl & ~CLK_E);
236 static void bfin_sdh_set_ios(struct mmc *mmc)
241 if (mmc->bus_width == 4) {
242 cfg = bfin_read_SDH_CFG();
247 bfin_write_SDH_CFG(cfg);
248 clk_ctl |= WIDE_BUS_4;
250 bfin_write_SDH_CLK_CTL(clk_ctl);
251 sdh_set_clk(mmc->clock);
254 static int bfin_sdh_init(struct mmc *mmc)
256 const unsigned short pins[] = PORTMUX_PINS;
259 /* Initialize sdh controller */
260 ret = peripheral_request_list(pins, "bfin_sdh");
263 #if defined(__ADSPBF54x__)
264 bfin_write_DMAC1_PERIMUX(bfin_read_DMAC1_PERIMUX() | 0x1);
266 bfin_write_SDH_CFG(bfin_read_SDH_CFG() | CLKS_EN);
267 /* Disable card detect pin */
268 bfin_write_SDH_CFG((bfin_read_SDH_CFG() & 0x1F) | 0x60);
270 bfin_write_SDH_PWR_CTL(PWR_ON | ROD_CTL);
272 bfin_write_SDH_CFG(bfin_read_SDH_CFG() | PWR_ON);
277 static const struct mmc_ops bfin_mmc_ops = {
278 .send_cmd = bfin_sdh_request,
279 .set_ios = bfin_sdh_set_ios,
280 .init = bfin_sdh_init,
283 static struct mmc_config bfin_mmc_cfg = {
284 .name = "Blackfin SDH",
285 .ops = &bfin_mmc_ops,
286 .host_caps = MMC_MODE_4BIT,
287 .voltages = MMC_VDD_32_33 | MMC_VDD_33_34,
288 .b_max = CONFIG_SYS_MMC_MAX_BLK_COUNT,
291 int bfin_mmc_init(bd_t *bis)
295 bfin_mmc_cfg.f_max = get_sclk();
296 bfin_mmc_cfg.f_min = bfin_mmc_cfg.f_max >> 9;
298 mmc = mmc_create(&bfin_mmc_cfg, NULL);