]> git.kernelconcepts.de Git - karo-tx-uboot.git/blob - drivers/mmc/mvebu_mmc.c
854bcc6c4f2b5443910178f903d370035510482c
[karo-tx-uboot.git] / drivers / mmc / mvebu_mmc.c
1 /*
2  * Marvell MMC/SD/SDIO driver
3  *
4  * (C) Copyright 2012-2014
5  * Marvell Semiconductor <www.marvell.com>
6  * Written-by: Maen Suleiman, Gerald Kerma
7  *
8  * SPDX-License-Identifier:     GPL-2.0+
9  */
10
11 #include <common.h>
12 #include <malloc.h>
13 #include <part.h>
14 #include <mmc.h>
15 #include <asm/io.h>
16 #include <asm/arch/cpu.h>
17 #include <asm/arch/soc.h>
18 #include <mvebu_mmc.h>
19
20 DECLARE_GLOBAL_DATA_PTR;
21
22 #define DRIVER_NAME "MVEBU_MMC"
23
24 #define MVEBU_TARGET_DRAM 0
25
26 #define TIMEOUT_DELAY   5*CONFIG_SYS_HZ         /* wait 5 seconds */
27
28 static void mvebu_mmc_write(u32 offs, u32 val)
29 {
30         writel(val, CONFIG_SYS_MMC_BASE + (offs));
31 }
32
33 static u32 mvebu_mmc_read(u32 offs)
34 {
35         return readl(CONFIG_SYS_MMC_BASE + (offs));
36 }
37
38 static int mvebu_mmc_setup_data(struct mmc_data *data)
39 {
40         u32 ctrl_reg;
41
42         debug("%s, data %s : blocks=%d blksz=%d\n", DRIVER_NAME,
43               (data->flags & MMC_DATA_READ) ? "read" : "write",
44               data->blocks, data->blocksize);
45
46         /* default to maximum timeout */
47         ctrl_reg = mvebu_mmc_read(SDIO_HOST_CTRL);
48         ctrl_reg |= SDIO_HOST_CTRL_TMOUT(SDIO_HOST_CTRL_TMOUT_MAX);
49         mvebu_mmc_write(SDIO_HOST_CTRL, ctrl_reg);
50
51         if (data->flags & MMC_DATA_READ) {
52                 mvebu_mmc_write(SDIO_SYS_ADDR_LOW, (u32)data->dest & 0xffff);
53                 mvebu_mmc_write(SDIO_SYS_ADDR_HI, (u32)data->dest >> 16);
54         } else {
55                 mvebu_mmc_write(SDIO_SYS_ADDR_LOW, (u32)data->src & 0xffff);
56                 mvebu_mmc_write(SDIO_SYS_ADDR_HI, (u32)data->src >> 16);
57         }
58
59         mvebu_mmc_write(SDIO_BLK_COUNT, data->blocks);
60         mvebu_mmc_write(SDIO_BLK_SIZE, data->blocksize);
61
62         return 0;
63 }
64
65 static int mvebu_mmc_send_cmd(struct mmc *mmc, struct mmc_cmd *cmd,
66                               struct mmc_data *data)
67 {
68         ulong start;
69         ushort waittype = 0;
70         ushort resptype = 0;
71         ushort xfertype = 0;
72         ushort resp_indx = 0;
73
74         debug("%s: cmdidx [0x%x] resp_type[0x%x] cmdarg[0x%x]\n",
75               DRIVER_NAME, cmd->cmdidx, cmd->resp_type, cmd->cmdarg);
76
77         debug("%s: cmd %d (hw state 0x%04x)\n", DRIVER_NAME,
78               cmd->cmdidx, mvebu_mmc_read(SDIO_HW_STATE));
79
80         /*
81          * Hardware weirdness.  The FIFO_EMPTY bit of the HW_STATE
82          * register is sometimes not set before a while when some
83          * "unusual" data block sizes are used (such as with the SWITCH
84          * command), even despite the fact that the XFER_DONE interrupt
85          * was raised.  And if another data transfer starts before
86          * this bit comes to good sense (which eventually happens by
87          * itself) then the new transfer simply fails with a timeout.
88          */
89         if (!(mvebu_mmc_read(SDIO_HW_STATE) & CMD_FIFO_EMPTY)) {
90                 ushort hw_state, count = 0;
91
92                 start = get_timer(0);
93                 do {
94                         hw_state = mvebu_mmc_read(SDIO_HW_STATE);
95                         if ((get_timer(0) - start) > TIMEOUT_DELAY) {
96                                 printf("%s : FIFO_EMPTY bit missing\n",
97                                        DRIVER_NAME);
98                                 break;
99                         }
100                         count++;
101                 } while (!(hw_state & CMD_FIFO_EMPTY));
102                 debug("%s *** wait for FIFO_EMPTY bit (hw=0x%04x, count=%d, jiffies=%ld)\n",
103                       DRIVER_NAME, hw_state, count, (get_timer(0) - (start)));
104         }
105
106         /* Set up for a data transfer if we have one */
107         if (data) {
108                 int err = mvebu_mmc_setup_data(data);
109
110                 if (err) {
111                         debug("%s: command DATA error :%x\n",
112                               DRIVER_NAME, err);
113                         return err;
114                 }
115         }
116
117         resptype = SDIO_CMD_INDEX(cmd->cmdidx);
118
119         /* Analyzing resptype/xfertype/waittype for the command */
120         if (cmd->resp_type & MMC_RSP_BUSY)
121                 resptype |= SDIO_CMD_RSP_48BUSY;
122         else if (cmd->resp_type & MMC_RSP_136)
123                 resptype |= SDIO_CMD_RSP_136;
124         else if (cmd->resp_type & MMC_RSP_PRESENT)
125                 resptype |= SDIO_CMD_RSP_48;
126         else
127                 resptype |= SDIO_CMD_RSP_NONE;
128
129         if (cmd->resp_type & MMC_RSP_CRC)
130                 resptype |= SDIO_CMD_CHECK_CMDCRC;
131
132         if (cmd->resp_type & MMC_RSP_OPCODE)
133                 resptype |= SDIO_CMD_INDX_CHECK;
134
135         if (cmd->resp_type & MMC_RSP_PRESENT) {
136                 resptype |= SDIO_UNEXPECTED_RESP;
137                 waittype |= SDIO_NOR_UNEXP_RSP;
138         }
139
140         if (data) {
141                 resptype |= SDIO_CMD_DATA_PRESENT | SDIO_CMD_CHECK_DATACRC16;
142                 xfertype |= SDIO_XFER_MODE_HW_WR_DATA_EN;
143                 if (data->flags & MMC_DATA_READ) {
144                         xfertype |= SDIO_XFER_MODE_TO_HOST;
145                         waittype = SDIO_NOR_DMA_INI;
146                 } else {
147                         waittype |= SDIO_NOR_XFER_DONE;
148                 }
149         } else {
150                 waittype |= SDIO_NOR_CMD_DONE;
151         }
152
153         /* Setting cmd arguments */
154         mvebu_mmc_write(SDIO_ARG_LOW, cmd->cmdarg & 0xffff);
155         mvebu_mmc_write(SDIO_ARG_HI, cmd->cmdarg >> 16);
156
157         /* Setting Xfer mode */
158         mvebu_mmc_write(SDIO_XFER_MODE, xfertype);
159
160         mvebu_mmc_write(SDIO_NOR_INTR_STATUS, ~SDIO_NOR_CARD_INT);
161         mvebu_mmc_write(SDIO_ERR_INTR_STATUS, SDIO_POLL_MASK);
162
163         /* Sending command */
164         mvebu_mmc_write(SDIO_CMD, resptype);
165
166         mvebu_mmc_write(SDIO_NOR_INTR_EN, SDIO_POLL_MASK);
167         mvebu_mmc_write(SDIO_ERR_INTR_EN, SDIO_POLL_MASK);
168
169         start = get_timer(0);
170
171         while (!((mvebu_mmc_read(SDIO_NOR_INTR_STATUS)) & waittype)) {
172                 if (mvebu_mmc_read(SDIO_NOR_INTR_STATUS) & SDIO_NOR_ERROR) {
173                         debug("%s: error! cmdidx : %d, err reg: %04x\n",
174                               DRIVER_NAME, cmd->cmdidx,
175                               mvebu_mmc_read(SDIO_ERR_INTR_STATUS));
176                         if (mvebu_mmc_read(SDIO_ERR_INTR_STATUS) &
177                             (SDIO_ERR_CMD_TIMEOUT | SDIO_ERR_DATA_TIMEOUT)) {
178                                 debug("%s: command READ timed out\n",
179                                       DRIVER_NAME);
180                                 return TIMEOUT;
181                         }
182                         debug("%s: command READ error\n", DRIVER_NAME);
183                         return COMM_ERR;
184                 }
185
186                 if ((get_timer(0) - start) > TIMEOUT_DELAY) {
187                         debug("%s: command timed out\n", DRIVER_NAME);
188                         return TIMEOUT;
189                 }
190         }
191
192         if (mvebu_mmc_read(SDIO_ERR_INTR_STATUS) &
193                 (SDIO_ERR_CMD_TIMEOUT | SDIO_ERR_DATA_TIMEOUT))
194                 return TIMEOUT;
195
196         /* Handling response */
197         if (cmd->resp_type & MMC_RSP_136) {
198                 uint response[8];
199
200                 for (resp_indx = 0; resp_indx < 8; resp_indx++)
201                         response[resp_indx]
202                                 = mvebu_mmc_read(SDIO_RSP(resp_indx));
203
204                 cmd->response[0] =      ((response[0] & 0x03ff) << 22) |
205                                         ((response[1] & 0xffff) << 6) |
206                                         ((response[2] & 0xfc00) >> 10);
207                 cmd->response[1] =      ((response[2] & 0x03ff) << 22) |
208                                         ((response[3] & 0xffff) << 6) |
209                                         ((response[4] & 0xfc00) >> 10);
210                 cmd->response[2] =      ((response[4] & 0x03ff) << 22) |
211                                         ((response[5] & 0xffff) << 6) |
212                                         ((response[6] & 0xfc00) >> 10);
213                 cmd->response[3] =      ((response[6] & 0x03ff) << 22) |
214                                         ((response[7] & 0x3fff) << 8);
215         } else if (cmd->resp_type & MMC_RSP_PRESENT) {
216                 uint response[3];
217
218                 for (resp_indx = 0; resp_indx < 3; resp_indx++)
219                         response[resp_indx]
220                                 = mvebu_mmc_read(SDIO_RSP(resp_indx));
221
222                 cmd->response[0] =      ((response[2] & 0x003f) << (8 - 8)) |
223                                         ((response[1] & 0xffff) << (14 - 8)) |
224                                         ((response[0] & 0x03ff) << (30 - 8));
225                 cmd->response[1] =      ((response[0] & 0xfc00) >> 10);
226                 cmd->response[2] =      0;
227                 cmd->response[3] =      0;
228         }
229
230         debug("%s: resp[0x%x] ", DRIVER_NAME, cmd->resp_type);
231         debug("[0x%x] ", cmd->response[0]);
232         debug("[0x%x] ", cmd->response[1]);
233         debug("[0x%x] ", cmd->response[2]);
234         debug("[0x%x] ", cmd->response[3]);
235         debug("\n");
236
237         return 0;
238 }
239
240 static void mvebu_mmc_power_up(void)
241 {
242         debug("%s: power up\n", DRIVER_NAME);
243
244         /* disable interrupts */
245         mvebu_mmc_write(SDIO_NOR_INTR_EN, 0);
246         mvebu_mmc_write(SDIO_ERR_INTR_EN, 0);
247
248         /* SW reset */
249         mvebu_mmc_write(SDIO_SW_RESET, SDIO_SW_RESET_NOW);
250
251         mvebu_mmc_write(SDIO_XFER_MODE, 0);
252
253         /* enable status */
254         mvebu_mmc_write(SDIO_NOR_STATUS_EN, SDIO_POLL_MASK);
255         mvebu_mmc_write(SDIO_ERR_STATUS_EN, SDIO_POLL_MASK);
256
257         /* enable interrupts status */
258         mvebu_mmc_write(SDIO_NOR_INTR_STATUS, SDIO_POLL_MASK);
259         mvebu_mmc_write(SDIO_ERR_INTR_STATUS, SDIO_POLL_MASK);
260 }
261
262 static void mvebu_mmc_set_clk(unsigned int clock)
263 {
264         unsigned int m;
265
266         if (clock == 0) {
267                 debug("%s: clock off\n", DRIVER_NAME);
268                 mvebu_mmc_write(SDIO_XFER_MODE, SDIO_XFER_MODE_STOP_CLK);
269                 mvebu_mmc_write(SDIO_CLK_DIV, MVEBU_MMC_BASE_DIV_MAX);
270         } else {
271                 m = MVEBU_MMC_BASE_FAST_CLOCK/(2*clock) - 1;
272                 if (m > MVEBU_MMC_BASE_DIV_MAX)
273                         m = MVEBU_MMC_BASE_DIV_MAX;
274                 mvebu_mmc_write(SDIO_CLK_DIV, m & MVEBU_MMC_BASE_DIV_MAX);
275                 debug("%s: clock (%d) div : %d\n", DRIVER_NAME, clock, m);
276         }
277
278         udelay(10*1000);
279 }
280
281 static void mvebu_mmc_set_bus(unsigned int bus)
282 {
283         u32 ctrl_reg = 0;
284
285         ctrl_reg = mvebu_mmc_read(SDIO_HOST_CTRL);
286         ctrl_reg &= ~SDIO_HOST_CTRL_DATA_WIDTH_4_BITS;
287
288         switch (bus) {
289         case 4:
290                 ctrl_reg |= SDIO_HOST_CTRL_DATA_WIDTH_4_BITS;
291                 break;
292         case 1:
293         default:
294                 ctrl_reg |= SDIO_HOST_CTRL_DATA_WIDTH_1_BIT;
295         }
296
297         /* default transfer mode */
298         ctrl_reg |= SDIO_HOST_CTRL_BIG_ENDIAN;
299         ctrl_reg &= ~SDIO_HOST_CTRL_LSB_FIRST;
300
301         /* default to maximum timeout */
302         ctrl_reg |= SDIO_HOST_CTRL_TMOUT(SDIO_HOST_CTRL_TMOUT_MAX);
303         ctrl_reg |= SDIO_HOST_CTRL_TMOUT_EN;
304
305         ctrl_reg |= SDIO_HOST_CTRL_PUSH_PULL_EN;
306
307         ctrl_reg |= SDIO_HOST_CTRL_CARD_TYPE_MEM_ONLY;
308
309         debug("%s: ctrl 0x%04x: %s %s %s\n", DRIVER_NAME, ctrl_reg,
310               (ctrl_reg & SDIO_HOST_CTRL_PUSH_PULL_EN) ?
311               "push-pull" : "open-drain",
312               (ctrl_reg & SDIO_HOST_CTRL_DATA_WIDTH_4_BITS) ?
313               "4bit-width" : "1bit-width",
314               (ctrl_reg & SDIO_HOST_CTRL_HI_SPEED_EN) ?
315               "high-speed" : "");
316
317         mvebu_mmc_write(SDIO_HOST_CTRL, ctrl_reg);
318         udelay(10*1000);
319 }
320
321 static void mvebu_mmc_set_ios(struct mmc *mmc)
322 {
323         debug("%s: bus[%d] clock[%d]\n", DRIVER_NAME,
324               mmc->bus_width, mmc->clock);
325         mvebu_mmc_set_bus(mmc->bus_width);
326         mvebu_mmc_set_clk(mmc->clock);
327 }
328
329 /*
330  * Set window register.
331  */
332 static void mvebu_window_setup(void)
333 {
334         int i;
335
336         for (i = 0; i < 4; i++) {
337                 mvebu_mmc_write(WINDOW_CTRL(i), 0);
338                 mvebu_mmc_write(WINDOW_BASE(i), 0);
339         }
340         for (i = 0; i < CONFIG_NR_DRAM_BANKS; i++) {
341                 u32 size, base, attrib;
342
343                 /* Enable DRAM bank */
344                 switch (i) {
345                 case 0:
346                         attrib = KWCPU_ATTR_DRAM_CS0;
347                         break;
348                 case 1:
349                         attrib = KWCPU_ATTR_DRAM_CS1;
350                         break;
351                 case 2:
352                         attrib = KWCPU_ATTR_DRAM_CS2;
353                         break;
354                 case 3:
355                         attrib = KWCPU_ATTR_DRAM_CS3;
356                         break;
357                 default:
358                         /* invalide bank, disable access */
359                         attrib = 0;
360                         break;
361                 }
362
363                 size = gd->bd->bi_dram[i].size;
364                 base = gd->bd->bi_dram[i].start;
365                 if (size && attrib) {
366                         mvebu_mmc_write(WINDOW_CTRL(i),
367                                         MVCPU_WIN_CTRL_DATA(size,
368                                                             MVEBU_TARGET_DRAM,
369                                                             attrib,
370                                                             MVCPU_WIN_ENABLE));
371                 } else {
372                         mvebu_mmc_write(WINDOW_CTRL(i), MVCPU_WIN_DISABLE);
373                 }
374                 mvebu_mmc_write(WINDOW_BASE(i), base);
375         }
376 }
377
378 static int mvebu_mmc_initialize(struct mmc *mmc)
379 {
380         debug("%s: mvebu_mmc_initialize\n", DRIVER_NAME);
381
382         /*
383          * Setting host parameters
384          * Initial Host Ctrl : Timeout : max , Normal Speed mode,
385          * 4-bit data mode, Big Endian, SD memory Card, Push_pull CMD Line
386          */
387         mvebu_mmc_write(SDIO_HOST_CTRL,
388                         SDIO_HOST_CTRL_TMOUT(SDIO_HOST_CTRL_TMOUT_MAX) |
389                         SDIO_HOST_CTRL_DATA_WIDTH_4_BITS |
390                         SDIO_HOST_CTRL_BIG_ENDIAN |
391                         SDIO_HOST_CTRL_PUSH_PULL_EN |
392                         SDIO_HOST_CTRL_CARD_TYPE_MEM_ONLY);
393
394         mvebu_mmc_write(SDIO_CLK_CTRL, 0);
395
396         /* enable status */
397         mvebu_mmc_write(SDIO_NOR_STATUS_EN, SDIO_POLL_MASK);
398         mvebu_mmc_write(SDIO_ERR_STATUS_EN, SDIO_POLL_MASK);
399
400         /* disable interrupts */
401         mvebu_mmc_write(SDIO_NOR_INTR_EN, 0);
402         mvebu_mmc_write(SDIO_ERR_INTR_EN, 0);
403
404         mvebu_window_setup();
405
406         /* SW reset */
407         mvebu_mmc_write(SDIO_SW_RESET, SDIO_SW_RESET_NOW);
408
409         udelay(10*1000);
410
411         return 0;
412 }
413
414 static const struct mmc_ops mvebu_mmc_ops = {
415         .send_cmd       = mvebu_mmc_send_cmd,
416         .set_ios        = mvebu_mmc_set_ios,
417         .init           = mvebu_mmc_initialize,
418 };
419
420 static struct mmc_config mvebu_mmc_cfg = {
421         .name           = DRIVER_NAME,
422         .ops            = &mvebu_mmc_ops,
423         .f_min          = MVEBU_MMC_BASE_FAST_CLOCK / MVEBU_MMC_BASE_DIV_MAX,
424         .f_max          = MVEBU_MMC_CLOCKRATE_MAX,
425         .voltages       = MMC_VDD_32_33 | MMC_VDD_33_34,
426         .host_caps      = MMC_MODE_4BIT | MMC_MODE_HS | MMC_MODE_HC |
427                           MMC_MODE_HS_52MHz,
428         .part_type      = PART_TYPE_DOS,
429         .b_max          = CONFIG_SYS_MMC_MAX_BLK_COUNT,
430 };
431
432 int mvebu_mmc_init(bd_t *bis)
433 {
434         struct mmc *mmc;
435
436         mvebu_mmc_power_up();
437
438         mmc = mmc_create(&mvebu_mmc_cfg, bis);
439         if (mmc == NULL)
440                 return -1;
441
442         return 0;
443 }