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