tizen 2.4 release
[profile/mobile/platform/kernel/u-boot-tm1.git] / drivers / mmc / sdhci.c
1 /*
2  * Copyright 2011, Marvell Semiconductor Inc.
3  * Lei Wen <leiwen@marvell.com>
4  *
5  * See file CREDITS for list of people who contributed to this
6  * project.
7  *
8  * This program is free software; you can redistribute it and/or
9  * modify it under the terms of the GNU General Public License as
10  * published by the Free Software Foundation; either version 2 of
11  * the License, or (at your option) any later version.
12  *
13  * This program is distributed in the hope that it will be useful,
14  * but WITHOUT ANY WARRANTY; without even the implied warranty of
15  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
16  * GNU General Public License for more details.
17  *
18  * You should have received a copy of the GNU General Public License
19  * along with this program; if not, write to the Free Software
20  * Foundation, Inc., 59 Temple Place, Suite 330, Boston,
21  * MA 02111-1307 USA
22  *
23  * Back ported to the 8xx platform (from the 8260 platform) by
24  * Murray.Jensen@cmst.csiro.au, 27-Jan-01.
25  */
26
27 #include <common.h>
28 #include <malloc.h>
29 #include <mmc.h>
30 #include <sdhci.h>
31
32 void *aligned_buffer;
33 void sdhci_dumpregs(struct sdhci_host *host);
34
35 static void sdhci_reset(struct sdhci_host *host, u8 mask)
36 {
37         unsigned long timeout;
38
39         /* Wait max 100 ms */
40         timeout = 100;
41         sdhci_writeb(host, mask|SDHCI_HW_RST, SDHCI_SOFTWARE_RESET);
42         while (sdhci_readb(host, SDHCI_SOFTWARE_RESET) & mask) {
43                 if (timeout == 0) {
44                         printf("Reset 0x%x never completed.\n", (int)mask);
45                         return;
46                 }
47                 timeout--;
48                 udelay(1000);
49         }
50 }
51
52 static void sdhci_cmd_done(struct sdhci_host *host, struct mmc_cmd *cmd)
53 {
54         int i;
55         if (cmd->resp_type & MMC_RSP_136) {
56                 /* CRC is stripped so we need to do some shifting. */
57                 for (i = 0; i < 4; i++) {
58                         cmd->response[i] = sdhci_readl(host,
59                                         SDHCI_RESPONSE + (3-i)*4) << 8;
60                         if (i != 3)
61                                 cmd->response[i] |= sdhci_readb(host,
62                                                 SDHCI_RESPONSE + (3-i)*4-1);
63                 }
64         } else {
65                 cmd->response[0] = sdhci_readl(host, SDHCI_RESPONSE);
66         }
67 }
68
69 static void sdhci_transfer_pio(struct sdhci_host *host, struct mmc_data *data)
70 {
71         int i;
72         char *offs;
73         for (i = 0; i < data->blocksize; i += 4) {
74                 offs = data->dest + i;
75                 if (data->flags == MMC_DATA_READ) {
76                         *(u32 *)offs = sdhci_readl(host, SDHCI_BUFFER);
77                 }
78                 else {
79                         sdhci_writel(host, *(u32 *)offs, SDHCI_BUFFER);
80                 }
81         }
82 }
83
84 static int sdhci_transfer_data(struct sdhci_host *host, struct mmc_data *data,
85                                 unsigned int start_addr)
86 {
87         unsigned int stat, rdy, mask, timeout, block = 0;
88
89         timeout = 10000;
90         rdy = SDHCI_INT_SPACE_AVAIL | SDHCI_INT_DATA_AVAIL;
91         mask = SDHCI_DATA_AVAILABLE | SDHCI_SPACE_AVAILABLE;
92
93         do {
94                 stat = sdhci_readl(host, SDHCI_INT_STATUS);
95                 if (stat & SDHCI_INT_ERROR) {
96                         printf("Error detected in status(0x%X)!\n", stat);
97                         return -1;
98                 }
99                 if (stat & rdy) {
100                         if (!(sdhci_readl(host, SDHCI_PRESENT_STATE) & mask))
101                                 continue;
102                         sdhci_writel(host, rdy, SDHCI_INT_STATUS);
103                         printf("%s start pio\n", __func__);
104                         sdhci_transfer_pio(host, data);
105                         data->dest += data->blocksize;
106                         if (++block >= data->blocks)
107                                 break;
108                 }
109 #ifdef CONFIG_MMC_SDMA
110                 if (stat & SDHCI_INT_DMA_END) {
111                         sdhci_writel(host, SDHCI_INT_DMA_END, SDHCI_INT_STATUS);
112                         start_addr &= ~(SDHCI_DEFAULT_BOUNDARY_SIZE - 1);
113                         start_addr += SDHCI_DEFAULT_BOUNDARY_SIZE;
114                         sdhci_writel(host, start_addr, SDHCI_DMA_ADDRESS);
115                 }
116 #endif
117                 if (timeout-- > 0)
118                         udelay(1000);
119                 else {
120                         printf("Transfer data timeout\n");
121                         return -1;
122                 }
123         } while (!(stat & SDHCI_INT_DATA_END));
124         return 0;
125 }
126
127 int sdhci_send_command(struct mmc *mmc, struct mmc_cmd *cmd,
128                        struct mmc_data *data)
129 {
130         struct sdhci_host *host = (struct sdhci_host *)mmc->priv;
131         unsigned int stat = 0;
132         int ret = 0;
133         int trans_bytes = 0, is_aligned = 1;
134         u32 mask, flags, mode;
135         unsigned int timeout, start_addr = 0;
136
137         /* Wait max 10 ms */
138         timeout = 10;
139
140         sdhci_writel(host, SDHCI_INT_ALL_MASK, SDHCI_INT_STATUS);
141         mask = SDHCI_CMD_INHIBIT | SDHCI_DATA_INHIBIT;
142
143         /* We shouldn't wait for data inihibit for stop commands, even
144            though they might use busy signaling */
145         if (cmd->cmdidx == MMC_CMD_STOP_TRANSMISSION)
146                 mask &= ~SDHCI_DATA_INHIBIT;
147
148         while (sdhci_readl(host, SDHCI_PRESENT_STATE) & mask) {
149                 if (timeout == 0) {
150                         printf("Controller never released inhibit bit(s).\n");
151                         return COMM_ERR;
152                 }
153                 timeout--;
154                 udelay(1000);
155         }
156
157         mask = SDHCI_INT_RESPONSE;
158         if (!(cmd->resp_type & MMC_RSP_PRESENT))
159                 flags = SDHCI_CMD_RESP_NONE;
160         else if (cmd->resp_type & MMC_RSP_136)
161                 flags = SDHCI_CMD_RESP_LONG;
162         else if (cmd->resp_type & MMC_RSP_BUSY) {
163                 flags = SDHCI_CMD_RESP_SHORT_BUSY;
164                 mask |= SDHCI_INT_DATA_END;
165         } else
166                 flags = SDHCI_CMD_RESP_SHORT;
167
168         if (cmd->resp_type & MMC_RSP_CRC)
169                 flags |= SDHCI_CMD_CRC;
170         if (cmd->resp_type & MMC_RSP_OPCODE)
171                 flags |= SDHCI_CMD_INDEX;
172         if (data)
173                 flags |= SDHCI_CMD_DATA;
174
175         /*Set Transfer mode regarding to data flag*/
176         if (data != 0) {
177                 sdhci_writeb(host, 0xe, SDHCI_TIMEOUT_CONTROL);
178                 mode = SDHCI_TRNS_BLK_CNT_EN;
179                 trans_bytes = data->blocks * data->blocksize;
180                 if (data->blocks > 1)
181                         mode |= SDHCI_TRNS_MULTI;
182
183                 if (data->flags == MMC_DATA_READ)
184                         mode |= SDHCI_TRNS_READ;
185
186 #ifdef CONFIG_MMC_SDMA
187                 if (data->flags == MMC_DATA_READ)
188                         start_addr = (unsigned int)data->dest;
189                 else
190                         start_addr = (unsigned int)data->src;
191                 if ((host->quirks & SDHCI_QUIRK_32BIT_DMA_ADDR) &&
192                                 (start_addr & 0x7) != 0x0) {
193                         is_aligned = 0;
194                         start_addr = (unsigned int)aligned_buffer;
195                         if (data->flags != MMC_DATA_READ)
196                                 memcpy(aligned_buffer, data->src, trans_bytes);
197                 }
198                 Dcache_CleanRegion(start_addr, trans_bytes);
199                 Dcache_InvalRegion(start_addr, trans_bytes);
200                 sdhci_writel(host, start_addr, SDHCI_DMA_ADDRESS);
201                 mode |= SDHCI_TRNS_DMA;
202 #endif
203                 sdhci_writew(host, SDHCI_MAKE_BLKSZ(SDHCI_DEFAULT_BOUNDARY_ARG,
204                                 data->blocksize),
205                                 SDHCI_BLOCK_SIZE);
206                 sdhci_writew(host, data->blocks, SDHCI_BLOCK_COUNT);
207                 sdhci_writew(host, mode, SDHCI_TRANSFER_MODE);
208         }
209
210         sdhci_writel(host, cmd->cmdarg, SDHCI_ARGUMENT);
211 #ifdef CONFIG_MMC_SDMA
212         //flush_cache(start_addr, trans_bytes);
213 #endif
214         sdhci_writew(host, SDHCI_MAKE_CMD(cmd->cmdidx, flags), SDHCI_COMMAND);
215         do {
216                 stat = sdhci_readl(host, SDHCI_INT_STATUS);
217                 if (stat & SDHCI_INT_ERROR)
218                         break;
219         } while ((stat & mask) != mask);
220
221         if ((stat & (SDHCI_INT_ERROR | mask)) == mask) {
222                 sdhci_cmd_done(host, cmd);
223                 sdhci_writel(host, mask, SDHCI_INT_STATUS);
224         } else
225                 ret = -1;
226
227         if (!ret && data)
228                 ret = sdhci_transfer_data(host, data, start_addr);
229
230         stat = sdhci_readl(host, SDHCI_INT_STATUS);
231         sdhci_writel(host, SDHCI_INT_ALL_MASK, SDHCI_INT_STATUS);
232         if (!ret) {
233                 if ((host->quirks & SDHCI_QUIRK_32BIT_DMA_ADDR) &&
234                                 !is_aligned && (data->flags == MMC_DATA_READ))
235                         memcpy(data->dest, aligned_buffer, trans_bytes);
236                 return 0;
237         }
238
239         sdhci_reset(host, SDHCI_RESET_CMD);
240         sdhci_reset(host, SDHCI_RESET_DATA);
241         if (stat & SDHCI_INT_TIMEOUT)
242                 return TIMEOUT;
243         else
244                 return COMM_ERR;
245 }
246
247 static int sdhci_set_clock(struct mmc *mmc, unsigned int clock)
248 {
249         struct sdhci_host *host = (struct sdhci_host *)mmc->priv;
250         unsigned int div, clk, timeout;
251
252         sdhci_sdclk_enable(host, 0);
253         udelay(200);
254
255         if (clock == 0)
256                 return 0;
257
258                 /* Version 2.00 divisors must be a power of 2. */
259         #if defined(CONFIG_TIGER) || defined (CONFIG_SC8830) || (defined CONFIG_SC9630)
260                 for (div = 1; div < 2046; div *= 2)
261         #else
262                 for (div = 1; div < 256; div *= 2)
263         #endif
264                 {
265                         if ((mmc->f_max / div) <= clock)
266                                 break;
267                 }
268         div >>= 1;
269
270 #if defined(CONFIG_TIGER) || defined (CONFIG_SC8830) || (defined CONFIG_SC9630)
271         if (div > 1)
272                 div--;
273 #endif
274 #if defined (CONFIG_SPX30G)
275     if(div < 3)
276             div = 3;
277 #endif
278         clk = (div & SDHCI_DIV_MASK) << SDHCI_DIVIDER_SHIFT;
279         clk |= SDHCI_CLOCK_INT_EN;
280         sdhci_writew(host, clk, SDHCI_CLOCK_CONTROL);
281
282         /* Wait max 20 ms */
283         timeout = 20;
284         while (!((clk = sdhci_readw(host, SDHCI_CLOCK_CONTROL))
285                 & SDHCI_CLOCK_INT_STABLE)) {
286                 if (timeout == 0) {
287                         printf("Internal clock never stabilised.\n");
288                         return -1;
289                 }
290                 timeout--;
291                 udelay(100);
292         }
293         udelay(200);
294         clk |= SDHCI_CLOCK_CARD_EN;
295         sdhci_writew(host, clk, SDHCI_CLOCK_CONTROL);
296         return 0;
297 }
298
299 static void sdhci_set_power(struct sdhci_host *host, unsigned short power)
300 {
301         u8 pwr = 0;
302
303         if (power != (unsigned short)-1) {
304                 switch (1 << power) {
305                 case MMC_VDD_165_195:
306                         pwr = SDHCI_POWER_180;
307                         break;
308                 case MMC_VDD_29_30:
309                 case MMC_VDD_30_31:
310                         pwr = SDHCI_POWER_300;
311                         break;
312                 case MMC_VDD_32_33:
313                 case MMC_VDD_33_34:
314                         pwr = SDHCI_POWER_330;
315                         break;
316                 }
317         }
318
319         if (pwr == 0) {
320                 sdhci_writeb(host, 0, SDHCI_POWER_CONTROL);
321                 return;
322         }
323
324         pwr |= SDHCI_POWER_ON;
325
326         sdhci_writeb(host, pwr, SDHCI_POWER_CONTROL);
327 }
328
329 void sdhci_set_ios(struct mmc *mmc)
330 {
331         u32 ctrl;
332         struct sdhci_host *host = (struct sdhci_host *)mmc->priv;
333
334         if (mmc->clock != host->clock)
335                 sdhci_set_clock(mmc, mmc->clock);
336
337         /* Set bus width */
338         ctrl = sdhci_readb(host, SDHCI_HOST_CONTROL);
339         if (mmc->bus_width == 8) {
340                 ctrl &= ~SDHCI_CTRL_4BITBUS;
341         } else {
342                 if (mmc->bus_width == 4)
343                         ctrl |= SDHCI_CTRL_4BITBUS;
344                 else
345                         ctrl &= ~SDHCI_CTRL_4BITBUS;
346         }
347
348         /* high speed config is not supported on sp8830 */
349 #ifndef CONFIG_SDHCI_CTRL_NO_HISPD
350         if (mmc->clock > 26000000)
351                 ctrl |= SDHCI_CTRL_HISPD;
352         else
353 #endif
354                 ctrl &= ~SDHCI_CTRL_HISPD;
355
356         sdhci_writeb(host, ctrl, SDHCI_HOST_CONTROL);
357 }
358
359
360 int sdhci_init(struct mmc *mmc)
361 {
362         struct sdhci_host *host = (struct sdhci_host *)mmc->priv;
363
364         if ((host->quirks & SDHCI_QUIRK_32BIT_DMA_ADDR) && !aligned_buffer) {
365                 aligned_buffer = memalign(8, 512*1024);
366                 if (!aligned_buffer) {
367                         printf("Aligned buffer alloc failed!!!");
368                         return -1;
369                 }
370         }
371
372
373         /* Eable all state */
374         sdhci_writel(host, SDHCI_INT_ALL_MASK, SDHCI_INT_ENABLE);
375         sdhci_writel(host, SDHCI_INT_ALL_MASK, SDHCI_SIGNAL_ENABLE);
376
377         sdhci_set_power(host, fls(mmc->voltages) - 1);
378
379         return 0;
380 }
381
382 int add_sdhci(struct sdhci_host *host, u32 max_clk, u32 min_clk)
383 {
384         struct mmc *mmc;
385         unsigned int caps;
386
387         mmc = malloc(sizeof(struct mmc));
388         if (!mmc) {
389                 printf("mmc malloc fail!\n");
390                 return -1;
391         }
392
393         mmc->priv = host;
394         host->mmc = mmc;
395
396         sprintf(mmc->name, "%s", host->name);
397         mmc->send_cmd = sdhci_send_command;
398         mmc->set_ios = sdhci_set_ios;
399         mmc->init = sdhci_init;
400
401         caps = sdhci_readl(host, SDHCI_CAPABILITIES);
402 #ifdef CONFIG_MMC_SDMA
403         if (!(caps & SDHCI_CAN_DO_SDMA)) {
404                 printf("Your controller don't support sdma!!\n");
405                 return -1;
406         }
407 #endif
408
409         if (max_clk)
410                 mmc->f_max = max_clk;
411         else {
412                         mmc->f_max = (caps & SDHCI_CLOCK_BASE_MASK)
413                                 >> SDHCI_CLOCK_BASE_SHIFT;
414                 mmc->f_max *= 1000000;
415         }
416         if (mmc->f_max == 0) {
417                 printf("Hardware doesn't specify base clock frequency\n");
418                 return -1;
419         }
420         if (min_clk)
421                 mmc->f_min = min_clk;
422
423         mmc->voltages = 0;
424         if (caps & SDHCI_CAN_VDD_330)
425                 mmc->voltages |= MMC_VDD_32_33 | MMC_VDD_33_34;
426         if (caps & SDHCI_CAN_VDD_300)
427                 mmc->voltages |= MMC_VDD_29_30 | MMC_VDD_30_31;
428         if (caps & SDHCI_CAN_VDD_180)
429                 mmc->voltages |= MMC_VDD_165_195;
430         mmc->host_caps = MMC_MODE_HS | MMC_MODE_HS_52MHz | MMC_MODE_4BIT;
431         sdhci_sdclk_enable(host, 0);
432         udelay(200);
433         sdhci_reset(host, SDHCI_RESET_ALL);
434         mmc_register(mmc);
435
436         return 0;
437 }