blob: 55232103e08af8946e110ea539fabfef310abc85 [file] [log] [blame]
Sukumar Ghoraide941242010-09-18 20:32:33 -07001/*
2 * (C) Copyright 2008
3 * Texas Instruments, <www.ti.com>
4 * Sukumar Ghorai <s-ghorai@ti.com>
5 *
6 * See file CREDITS for list of people who contributed to this
7 * project.
8 *
9 * This program is free software; you can redistribute it and/or
10 * modify it under the terms of the GNU General Public License as
11 * published by the Free Software Foundation's version 2 of
12 * the License.
13 *
14 * This program is distributed in the hope that it will be useful,
15 * but WITHOUT ANY WARRANTY; without even the implied warranty of
16 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
17 * GNU General Public License for more details.
18 *
19 * You should have received a copy of the GNU General Public License
20 * along with this program; if not, write to the Free Software
21 * Foundation, Inc., 59 Temple Place, Suite 330, Boston,
22 * MA 02111-1307 USA
23 */
24
25#include <config.h>
26#include <common.h>
Pantelis Antoniou93bfd612014-03-11 19:34:20 +020027#include <malloc.h>
Kishon Vijay Abraham If0d53e82017-09-21 16:51:34 +020028#include <memalign.h>
Sukumar Ghoraide941242010-09-18 20:32:33 -070029#include <mmc.h>
30#include <part.h>
31#include <i2c.h>
Felix Brack339d5782017-10-11 17:05:28 +020032#if defined(CONFIG_OMAP54XX) || defined(CONFIG_OMAP44XX)
Nishanth Menoncb199102013-03-26 05:20:54 +000033#include <palmas.h>
Felix Brack339d5782017-10-11 17:05:28 +020034#endif
Sukumar Ghoraide941242010-09-18 20:32:33 -070035#include <asm/io.h>
36#include <asm/arch/mmc_host_def.h>
Roger Quadros3b689392015-09-19 16:26:53 +053037#if !defined(CONFIG_SOC_KEYSTONE)
38#include <asm/gpio.h>
Dirk Behme96e0e7b2011-05-15 09:04:47 +000039#include <asm/arch/sys_proto.h>
Roger Quadros3b689392015-09-19 16:26:53 +053040#endif
Tom Rini2a48b3a2017-02-09 13:41:28 -050041#ifdef CONFIG_MMC_OMAP36XX_PINS
42#include <asm/arch/mux.h>
43#endif
Mugunthan V Na9d6a7e2015-09-28 12:56:30 +053044#include <dm.h>
45
46DECLARE_GLOBAL_DATA_PTR;
Sukumar Ghoraide941242010-09-18 20:32:33 -070047
Pantelis Antoniouab769f22014-02-26 19:28:45 +020048/* simplify defines to OMAP_HSMMC_USE_GPIO */
49#if (defined(CONFIG_OMAP_GPIO) && !defined(CONFIG_SPL_BUILD)) || \
50 (defined(CONFIG_SPL_BUILD) && defined(CONFIG_SPL_GPIO_SUPPORT))
51#define OMAP_HSMMC_USE_GPIO
52#else
53#undef OMAP_HSMMC_USE_GPIO
54#endif
55
Grazvydas Ignotas25c719e2012-03-19 12:12:06 +000056/* common definitions for all OMAPs */
57#define SYSCTL_SRC (1 << 25)
58#define SYSCTL_SRD (1 << 26)
59
Nikita Kiryanovcc22b0c2012-12-03 02:19:43 +000060struct omap_hsmmc_data {
61 struct hsmmc *base_addr;
Simon Glassc4d660d2017-07-04 13:31:19 -060062#if !CONFIG_IS_ENABLED(DM_MMC)
Pantelis Antoniou93bfd612014-03-11 19:34:20 +020063 struct mmc_config cfg;
Jean-Jacques Hiblot3d673ff2017-03-22 16:00:33 +010064#endif
Kishon Vijay Abraham I48a2f112018-01-30 16:01:31 +010065 uint bus_width;
Jean-Jacques Hiblot5baf5432018-01-30 16:01:30 +010066 uint clock;
Pantelis Antoniouab769f22014-02-26 19:28:45 +020067#ifdef OMAP_HSMMC_USE_GPIO
Simon Glassc4d660d2017-07-04 13:31:19 -060068#if CONFIG_IS_ENABLED(DM_MMC)
Mugunthan V Na9d6a7e2015-09-28 12:56:30 +053069 struct gpio_desc cd_gpio; /* Change Detect GPIO */
70 struct gpio_desc wp_gpio; /* Write Protect GPIO */
71 bool cd_inverted;
72#else
Nikita Kiryanove874d5b2012-12-03 02:19:44 +000073 int cd_gpio;
Nikita Kiryanove3913f52012-12-03 02:19:47 +000074 int wp_gpio;
Pantelis Antoniouab769f22014-02-26 19:28:45 +020075#endif
Mugunthan V Na9d6a7e2015-09-28 12:56:30 +053076#endif
Kishon Vijay Abraham Ib5944812018-01-30 16:01:32 +010077#if CONFIG_IS_ENABLED(DM_MMC)
78 uint iov;
Jean-Jacques Hiblot8fc238b2018-01-30 16:01:33 +010079 enum bus_mode mode;
Kishon Vijay Abraham Ib5944812018-01-30 16:01:32 +010080#endif
Kishon Vijay Abraham If0d53e82017-09-21 16:51:34 +020081 u8 controller_flags;
82#ifndef CONFIG_OMAP34XX
83 struct omap_hsmmc_adma_desc *adma_desc_table;
84 uint desc_slot;
85#endif
Nikita Kiryanovcc22b0c2012-12-03 02:19:43 +000086};
87
Kishon Vijay Abraham If0d53e82017-09-21 16:51:34 +020088#ifndef CONFIG_OMAP34XX
89struct omap_hsmmc_adma_desc {
90 u8 attr;
91 u8 reserved;
92 u16 len;
93 u32 addr;
94};
95
96#define ADMA_MAX_LEN 63488
97
98/* Decriptor table defines */
99#define ADMA_DESC_ATTR_VALID BIT(0)
100#define ADMA_DESC_ATTR_END BIT(1)
101#define ADMA_DESC_ATTR_INT BIT(2)
102#define ADMA_DESC_ATTR_ACT1 BIT(4)
103#define ADMA_DESC_ATTR_ACT2 BIT(5)
104
105#define ADMA_DESC_TRANSFER_DATA ADMA_DESC_ATTR_ACT2
106#define ADMA_DESC_LINK_DESC (ADMA_DESC_ATTR_ACT1 | ADMA_DESC_ATTR_ACT2)
107#endif
108
Nishanth Menoneb9a28f2010-11-19 11:18:12 -0500109/* If we fail after 1 second wait, something is really bad */
110#define MAX_RETRY_MS 1000
111
Kishon Vijay Abraham If0d53e82017-09-21 16:51:34 +0200112/* DMA transfers can take a long time if a lot a data is transferred.
113 * The timeout must take in account the amount of data. Let's assume
114 * that the time will never exceed 333 ms per MB (in other word we assume
115 * that the bandwidth is always above 3MB/s).
116 */
117#define DMA_TIMEOUT_PER_MB 333
Kishon Vijay Abraham Ib5944812018-01-30 16:01:32 +0100118#define OMAP_HSMMC_SUPPORTS_DUAL_VOLT BIT(0)
119#define OMAP_HSMMC_NO_1_8_V BIT(1)
Kishon Vijay Abraham If0d53e82017-09-21 16:51:34 +0200120#define OMAP_HSMMC_USE_ADMA BIT(2)
121
Sricharan933efe62011-11-15 09:49:53 -0500122static int mmc_read_data(struct hsmmc *mmc_base, char *buf, unsigned int size);
123static int mmc_write_data(struct hsmmc *mmc_base, const char *buf,
124 unsigned int siz);
Jean-Jacques Hiblot5baf5432018-01-30 16:01:30 +0100125static void omap_hsmmc_start_clock(struct hsmmc *mmc_base);
126static void omap_hsmmc_stop_clock(struct hsmmc *mmc_base);
Jean-Jacques Hiblot14761ca2018-01-30 16:01:35 +0100127static void mmc_reset_controller_fsm(struct hsmmc *mmc_base, u32 bit);
Balaji T K14fa2dd2011-09-08 06:34:57 +0000128
Jean-Jacques Hiblotae000e22017-03-22 16:00:31 +0100129static inline struct omap_hsmmc_data *omap_hsmmc_get_data(struct mmc *mmc)
130{
Simon Glassc4d660d2017-07-04 13:31:19 -0600131#if CONFIG_IS_ENABLED(DM_MMC)
Jean-Jacques Hiblotae000e22017-03-22 16:00:31 +0100132 return dev_get_priv(mmc->dev);
133#else
134 return (struct omap_hsmmc_data *)mmc->priv;
135#endif
136}
Jean-Jacques Hiblot3d673ff2017-03-22 16:00:33 +0100137static inline struct mmc_config *omap_hsmmc_get_cfg(struct mmc *mmc)
138{
Simon Glassc4d660d2017-07-04 13:31:19 -0600139#if CONFIG_IS_ENABLED(DM_MMC)
Jean-Jacques Hiblot3d673ff2017-03-22 16:00:33 +0100140 struct omap_hsmmc_plat *plat = dev_get_platdata(mmc->dev);
141 return &plat->cfg;
142#else
143 return &((struct omap_hsmmc_data *)mmc->priv)->cfg;
144#endif
145}
Jean-Jacques Hiblotae000e22017-03-22 16:00:31 +0100146
Simon Glassc4d660d2017-07-04 13:31:19 -0600147#if defined(OMAP_HSMMC_USE_GPIO) && !CONFIG_IS_ENABLED(DM_MMC)
Nikita Kiryanove874d5b2012-12-03 02:19:44 +0000148static int omap_mmc_setup_gpio_in(int gpio, const char *label)
149{
Simon Glass5915a2a2014-10-22 21:37:09 -0600150 int ret;
151
152#ifndef CONFIG_DM_GPIO
Nikita Kiryanove874d5b2012-12-03 02:19:44 +0000153 if (!gpio_is_valid(gpio))
154 return -1;
Simon Glass5915a2a2014-10-22 21:37:09 -0600155#endif
156 ret = gpio_request(gpio, label);
157 if (ret)
158 return ret;
Nikita Kiryanove874d5b2012-12-03 02:19:44 +0000159
Simon Glass5915a2a2014-10-22 21:37:09 -0600160 ret = gpio_direction_input(gpio);
161 if (ret)
162 return ret;
Nikita Kiryanove874d5b2012-12-03 02:19:44 +0000163
164 return gpio;
165}
Nikita Kiryanove874d5b2012-12-03 02:19:44 +0000166#endif
167
Jeroen Hofstee750121c2014-07-12 21:24:08 +0200168static unsigned char mmc_board_init(struct mmc *mmc)
Sukumar Ghoraide941242010-09-18 20:32:33 -0700169{
Sukumar Ghoraide941242010-09-18 20:32:33 -0700170#if defined(CONFIG_OMAP34XX)
Jean-Jacques Hiblot3d673ff2017-03-22 16:00:33 +0100171 struct mmc_config *cfg = omap_hsmmc_get_cfg(mmc);
Sukumar Ghoraide941242010-09-18 20:32:33 -0700172 t2_t *t2_base = (t2_t *)T2_BASE;
173 struct prcm *prcm_base = (struct prcm *)PRCM_BASE;
Grazvydas Ignotasb1e725f2012-03-19 03:50:53 +0000174 u32 pbias_lite;
Adam Ford6aca17c2017-02-06 11:31:43 -0600175#ifdef CONFIG_MMC_OMAP36XX_PINS
176 u32 wkup_ctrl = readl(OMAP34XX_CTRL_WKUP_CTRL);
177#endif
Sukumar Ghoraide941242010-09-18 20:32:33 -0700178
Grazvydas Ignotasb1e725f2012-03-19 03:50:53 +0000179 pbias_lite = readl(&t2_base->pbias_lite);
180 pbias_lite &= ~(PBIASLITEPWRDNZ1 | PBIASLITEPWRDNZ0);
Albert ARIBAUD \(3ADEV\)5bfdd1f2015-01-16 09:09:50 +0100181#ifdef CONFIG_TARGET_OMAP3_CAIRO
182 /* for cairo board, we need to set up 1.8 Volt bias level on MMC1 */
183 pbias_lite &= ~PBIASLITEVMODE0;
184#endif
Adam Ford6aca17c2017-02-06 11:31:43 -0600185#ifdef CONFIG_MMC_OMAP36XX_PINS
186 if (get_cpu_family() == CPU_OMAP36XX) {
187 /* Disable extended drain IO before changing PBIAS */
188 wkup_ctrl &= ~OMAP34XX_CTRL_WKUP_CTRL_GPIO_IO_PWRDNZ;
189 writel(wkup_ctrl, OMAP34XX_CTRL_WKUP_CTRL);
190 }
191#endif
Grazvydas Ignotasb1e725f2012-03-19 03:50:53 +0000192 writel(pbias_lite, &t2_base->pbias_lite);
Paul Kocialkowskiaac54502014-11-08 20:55:47 +0100193
Grazvydas Ignotasb1e725f2012-03-19 03:50:53 +0000194 writel(pbias_lite | PBIASLITEPWRDNZ1 |
Sukumar Ghoraide941242010-09-18 20:32:33 -0700195 PBIASSPEEDCTRL0 | PBIASLITEPWRDNZ0,
196 &t2_base->pbias_lite);
197
Adam Ford6aca17c2017-02-06 11:31:43 -0600198#ifdef CONFIG_MMC_OMAP36XX_PINS
199 if (get_cpu_family() == CPU_OMAP36XX)
200 /* Enable extended drain IO after changing PBIAS */
201 writel(wkup_ctrl |
202 OMAP34XX_CTRL_WKUP_CTRL_GPIO_IO_PWRDNZ,
203 OMAP34XX_CTRL_WKUP_CTRL);
204#endif
Sukumar Ghoraide941242010-09-18 20:32:33 -0700205 writel(readl(&t2_base->devconf0) | MMCSDIO1ADPCLKISEL,
206 &t2_base->devconf0);
207
208 writel(readl(&t2_base->devconf1) | MMCSDIO2ADPCLKISEL,
209 &t2_base->devconf1);
210
Jonathan Solnitbbbc1ae2012-02-24 11:30:18 +0000211 /* Change from default of 52MHz to 26MHz if necessary */
Jean-Jacques Hiblot3d673ff2017-03-22 16:00:33 +0100212 if (!(cfg->host_caps & MMC_MODE_HS_52MHz))
Jonathan Solnitbbbc1ae2012-02-24 11:30:18 +0000213 writel(readl(&t2_base->ctl_prog_io1) & ~CTLPROGIO1SPEEDCTRL,
214 &t2_base->ctl_prog_io1);
215
Sukumar Ghoraide941242010-09-18 20:32:33 -0700216 writel(readl(&prcm_base->fclken1_core) |
217 EN_MMC1 | EN_MMC2 | EN_MMC3,
218 &prcm_base->fclken1_core);
219
220 writel(readl(&prcm_base->iclken1_core) |
221 EN_MMC1 | EN_MMC2 | EN_MMC3,
222 &prcm_base->iclken1_core);
223#endif
224
Lokesh Vutlab4b06002016-11-23 13:25:28 +0530225#if defined(CONFIG_OMAP54XX) || defined(CONFIG_OMAP44XX)
Balaji T K14fa2dd2011-09-08 06:34:57 +0000226 /* PBIAS config needed for MMC1 only */
Jean-Jacques Hiblotdc091272017-03-22 16:00:32 +0100227 if (mmc_get_blk_desc(mmc)->devnum == 0)
Lokesh Vutlab4b06002016-11-23 13:25:28 +0530228 vmmc_pbias_config(LDO_VOLT_3V0);
Balaji T Kdd23e592012-03-12 02:25:49 +0000229#endif
Sukumar Ghoraide941242010-09-18 20:32:33 -0700230
231 return 0;
232}
233
Sricharan933efe62011-11-15 09:49:53 -0500234void mmc_init_stream(struct hsmmc *mmc_base)
Sukumar Ghoraide941242010-09-18 20:32:33 -0700235{
Nishanth Menoneb9a28f2010-11-19 11:18:12 -0500236 ulong start;
Sukumar Ghoraide941242010-09-18 20:32:33 -0700237
238 writel(readl(&mmc_base->con) | INIT_INITSTREAM, &mmc_base->con);
239
240 writel(MMC_CMD0, &mmc_base->cmd);
Nishanth Menoneb9a28f2010-11-19 11:18:12 -0500241 start = get_timer(0);
242 while (!(readl(&mmc_base->stat) & CC_MASK)) {
243 if (get_timer(0) - start > MAX_RETRY_MS) {
244 printf("%s: timedout waiting for cc!\n", __func__);
245 return;
246 }
247 }
Sukumar Ghoraide941242010-09-18 20:32:33 -0700248 writel(CC_MASK, &mmc_base->stat)
249 ;
250 writel(MMC_CMD0, &mmc_base->cmd)
251 ;
Nishanth Menoneb9a28f2010-11-19 11:18:12 -0500252 start = get_timer(0);
253 while (!(readl(&mmc_base->stat) & CC_MASK)) {
254 if (get_timer(0) - start > MAX_RETRY_MS) {
255 printf("%s: timedout waiting for cc2!\n", __func__);
256 return;
257 }
258 }
Sukumar Ghoraide941242010-09-18 20:32:33 -0700259 writel(readl(&mmc_base->con) & ~INIT_INITSTREAM, &mmc_base->con);
260}
261
Kishon Vijay Abraham Ib5944812018-01-30 16:01:32 +0100262#if CONFIG_IS_ENABLED(DM_MMC)
Jean-Jacques Hiblot8fc238b2018-01-30 16:01:33 +0100263static void omap_hsmmc_set_timing(struct mmc *mmc)
264{
265 u32 val;
266 struct hsmmc *mmc_base;
267 struct omap_hsmmc_data *priv = omap_hsmmc_get_data(mmc);
268
269 mmc_base = priv->base_addr;
270
271 val = readl(&mmc_base->ac12);
272 val &= ~AC12_UHSMC_MASK;
273 priv->mode = mmc->selected_mode;
274
Kishon Vijay Abraham I9b3fc212018-01-30 16:01:34 +0100275 if (mmc_is_mode_ddr(priv->mode))
276 writel(readl(&mmc_base->con) | DDR, &mmc_base->con);
277 else
278 writel(readl(&mmc_base->con) & ~DDR, &mmc_base->con);
279
Jean-Jacques Hiblot8fc238b2018-01-30 16:01:33 +0100280 switch (priv->mode) {
281 case MMC_HS_200:
282 case UHS_SDR104:
283 val |= AC12_UHSMC_SDR104;
284 break;
285 case UHS_SDR50:
286 val |= AC12_UHSMC_SDR50;
287 break;
288 case MMC_DDR_52:
289 case UHS_DDR50:
290 val |= AC12_UHSMC_DDR50;
291 break;
292 case SD_HS:
293 case MMC_HS_52:
294 case UHS_SDR25:
295 val |= AC12_UHSMC_SDR25;
296 break;
297 case MMC_LEGACY:
298 case MMC_HS:
299 case SD_LEGACY:
300 case UHS_SDR12:
301 val |= AC12_UHSMC_SDR12;
302 break;
303 default:
304 val |= AC12_UHSMC_RES;
305 break;
306 }
307 writel(val, &mmc_base->ac12);
308}
309
Kishon Vijay Abraham Ib5944812018-01-30 16:01:32 +0100310static void omap_hsmmc_conf_bus_power(struct mmc *mmc)
311{
312 struct hsmmc *mmc_base;
313 struct omap_hsmmc_data *priv = omap_hsmmc_get_data(mmc);
314 u32 val;
315
316 mmc_base = priv->base_addr;
317
318 val = readl(&mmc_base->hctl) & ~SDVS_MASK;
319
320 switch (priv->iov) {
321 case IOV_3V3:
322 val |= SDVS_3V3;
323 break;
324 case IOV_3V0:
325 val |= SDVS_3V0;
326 break;
327 case IOV_1V8:
328 val |= SDVS_1V8;
329 break;
330 }
331
332 writel(val, &mmc_base->hctl);
333}
334
335static void omap_hsmmc_set_capabilities(struct mmc *mmc)
336{
337 struct hsmmc *mmc_base;
338 struct omap_hsmmc_data *priv = omap_hsmmc_get_data(mmc);
339 u32 val;
340
341 mmc_base = priv->base_addr;
342 val = readl(&mmc_base->capa);
343
344 if (priv->controller_flags & OMAP_HSMMC_SUPPORTS_DUAL_VOLT) {
345 val |= (VS30_3V0SUP | VS18_1V8SUP);
346 priv->iov = IOV_3V0;
347 } else if (priv->controller_flags & OMAP_HSMMC_NO_1_8_V) {
348 val |= VS30_3V0SUP;
349 val &= ~VS18_1V8SUP;
350 priv->iov = IOV_3V0;
351 } else {
352 val |= VS18_1V8SUP;
353 val &= ~VS30_3V0SUP;
354 priv->iov = IOV_1V8;
355 }
356
357 writel(val, &mmc_base->capa);
358}
Jean-Jacques Hiblot14761ca2018-01-30 16:01:35 +0100359
360#ifdef MMC_SUPPORTS_TUNING
361static void omap_hsmmc_disable_tuning(struct mmc *mmc)
362{
363 struct hsmmc *mmc_base;
364 struct omap_hsmmc_data *priv = omap_hsmmc_get_data(mmc);
365 u32 val;
366
367 mmc_base = priv->base_addr;
368 val = readl(&mmc_base->ac12);
369 val &= ~(AC12_SCLK_SEL);
370 writel(val, &mmc_base->ac12);
371
372 val = readl(&mmc_base->dll);
373 val &= ~(DLL_FORCE_VALUE | DLL_SWT);
374 writel(val, &mmc_base->dll);
375}
376
377static void omap_hsmmc_set_dll(struct mmc *mmc, int count)
378{
379 int i;
380 struct hsmmc *mmc_base;
381 struct omap_hsmmc_data *priv = omap_hsmmc_get_data(mmc);
382 u32 val;
383
384 mmc_base = priv->base_addr;
385 val = readl(&mmc_base->dll);
386 val |= DLL_FORCE_VALUE;
387 val &= ~(DLL_FORCE_SR_C_MASK << DLL_FORCE_SR_C_SHIFT);
388 val |= (count << DLL_FORCE_SR_C_SHIFT);
389 writel(val, &mmc_base->dll);
390
391 val |= DLL_CALIB;
392 writel(val, &mmc_base->dll);
393 for (i = 0; i < 1000; i++) {
394 if (readl(&mmc_base->dll) & DLL_CALIB)
395 break;
396 }
397 val &= ~DLL_CALIB;
398 writel(val, &mmc_base->dll);
399}
400
401static int omap_hsmmc_execute_tuning(struct udevice *dev, uint opcode)
402{
403 struct omap_hsmmc_data *priv = dev_get_priv(dev);
404 struct mmc_uclass_priv *upriv = dev_get_uclass_priv(dev);
405 struct mmc *mmc = upriv->mmc;
406 struct hsmmc *mmc_base;
407 u32 val;
408 u8 cur_match, prev_match = 0;
409 int ret;
410 u32 phase_delay = 0;
411 u32 start_window = 0, max_window = 0;
412 u32 length = 0, max_len = 0;
413
414 mmc_base = priv->base_addr;
415 val = readl(&mmc_base->capa2);
416
417 /* clock tuning is not needed for upto 52MHz */
418 if (!((mmc->selected_mode == MMC_HS_200) ||
419 (mmc->selected_mode == UHS_SDR104) ||
420 ((mmc->selected_mode == UHS_SDR50) && (val & CAPA2_TSDR50))))
421 return 0;
422
423 val = readl(&mmc_base->dll);
424 val |= DLL_SWT;
425 writel(val, &mmc_base->dll);
426 while (phase_delay <= MAX_PHASE_DELAY) {
427 omap_hsmmc_set_dll(mmc, phase_delay);
428
429 cur_match = !mmc_send_tuning(mmc, opcode, NULL);
430
431 if (cur_match) {
432 if (prev_match) {
433 length++;
434 } else {
435 start_window = phase_delay;
436 length = 1;
437 }
438 }
439
440 if (length > max_len) {
441 max_window = start_window;
442 max_len = length;
443 }
444
445 prev_match = cur_match;
446 phase_delay += 4;
447 }
448
449 if (!max_len) {
450 ret = -EIO;
451 goto tuning_error;
452 }
453
454 val = readl(&mmc_base->ac12);
455 if (!(val & AC12_SCLK_SEL)) {
456 ret = -EIO;
457 goto tuning_error;
458 }
459
460 phase_delay = max_window + 4 * ((3 * max_len) >> 2);
461 omap_hsmmc_set_dll(mmc, phase_delay);
462
463 mmc_reset_controller_fsm(mmc_base, SYSCTL_SRD);
464 mmc_reset_controller_fsm(mmc_base, SYSCTL_SRC);
465
466 return 0;
467
468tuning_error:
469
470 omap_hsmmc_disable_tuning(mmc);
471 mmc_reset_controller_fsm(mmc_base, SYSCTL_SRD);
472 mmc_reset_controller_fsm(mmc_base, SYSCTL_SRC);
473
474 return ret;
475}
476#endif
Kishon Vijay Abraham Ib5944812018-01-30 16:01:32 +0100477#endif
478
Jean-Jacques Hiblot2faa1a32018-01-30 16:01:36 +0100479static void mmc_enable_irq(struct mmc *mmc, struct mmc_cmd *cmd)
480{
481 struct omap_hsmmc_data *priv = omap_hsmmc_get_data(mmc);
482 struct hsmmc *mmc_base = priv->base_addr;
483 u32 irq_mask = INT_EN_MASK;
484
485 /*
486 * TODO: Errata i802 indicates only DCRC interrupts can occur during
487 * tuning procedure and DCRC should be disabled. But see occurences
488 * of DEB, CIE, CEB, CCRC interupts during tuning procedure. These
489 * interrupts occur along with BRR, so the data is actually in the
490 * buffer. It has to be debugged why these interrutps occur
491 */
492 if (cmd && mmc_is_tuning_cmd(cmd->cmdidx))
493 irq_mask &= ~(IE_DEB | IE_DCRC | IE_CIE | IE_CEB | IE_CCRC);
494
495 writel(irq_mask, &mmc_base->ie);
496}
497
Pantelis Antoniouab769f22014-02-26 19:28:45 +0200498static int omap_hsmmc_init_setup(struct mmc *mmc)
Sukumar Ghoraide941242010-09-18 20:32:33 -0700499{
Jean-Jacques Hiblotae000e22017-03-22 16:00:31 +0100500 struct omap_hsmmc_data *priv = omap_hsmmc_get_data(mmc);
Nikita Kiryanovcc22b0c2012-12-03 02:19:43 +0000501 struct hsmmc *mmc_base;
Sukumar Ghoraide941242010-09-18 20:32:33 -0700502 unsigned int reg_val;
503 unsigned int dsor;
Nishanth Menoneb9a28f2010-11-19 11:18:12 -0500504 ulong start;
Sukumar Ghoraide941242010-09-18 20:32:33 -0700505
Jean-Jacques Hiblotae000e22017-03-22 16:00:31 +0100506 mmc_base = priv->base_addr;
Balaji T K14fa2dd2011-09-08 06:34:57 +0000507 mmc_board_init(mmc);
Sukumar Ghoraide941242010-09-18 20:32:33 -0700508
509 writel(readl(&mmc_base->sysconfig) | MMC_SOFTRESET,
510 &mmc_base->sysconfig);
Nishanth Menoneb9a28f2010-11-19 11:18:12 -0500511 start = get_timer(0);
512 while ((readl(&mmc_base->sysstatus) & RESETDONE) == 0) {
513 if (get_timer(0) - start > MAX_RETRY_MS) {
514 printf("%s: timedout waiting for cc2!\n", __func__);
Jaehoon Chung915ffa52016-07-19 16:33:36 +0900515 return -ETIMEDOUT;
Nishanth Menoneb9a28f2010-11-19 11:18:12 -0500516 }
517 }
Sukumar Ghoraide941242010-09-18 20:32:33 -0700518 writel(readl(&mmc_base->sysctl) | SOFTRESETALL, &mmc_base->sysctl);
Nishanth Menoneb9a28f2010-11-19 11:18:12 -0500519 start = get_timer(0);
520 while ((readl(&mmc_base->sysctl) & SOFTRESETALL) != 0x0) {
521 if (get_timer(0) - start > MAX_RETRY_MS) {
522 printf("%s: timedout waiting for softresetall!\n",
523 __func__);
Jaehoon Chung915ffa52016-07-19 16:33:36 +0900524 return -ETIMEDOUT;
Nishanth Menoneb9a28f2010-11-19 11:18:12 -0500525 }
526 }
Kishon Vijay Abraham If0d53e82017-09-21 16:51:34 +0200527#ifndef CONFIG_OMAP34XX
528 reg_val = readl(&mmc_base->hl_hwinfo);
529 if (reg_val & MADMA_EN)
530 priv->controller_flags |= OMAP_HSMMC_USE_ADMA;
531#endif
Kishon Vijay Abraham Ib5944812018-01-30 16:01:32 +0100532
533#if CONFIG_IS_ENABLED(DM_MMC)
534 omap_hsmmc_set_capabilities(mmc);
535 omap_hsmmc_conf_bus_power(mmc);
536#else
Sukumar Ghoraide941242010-09-18 20:32:33 -0700537 writel(DTW_1_BITMODE | SDBP_PWROFF | SDVS_3V0, &mmc_base->hctl);
538 writel(readl(&mmc_base->capa) | VS30_3V0SUP | VS18_1V8SUP,
539 &mmc_base->capa);
Kishon Vijay Abraham Ib5944812018-01-30 16:01:32 +0100540#endif
Sukumar Ghoraide941242010-09-18 20:32:33 -0700541
542 reg_val = readl(&mmc_base->con) & RESERVED_MASK;
543
544 writel(CTPL_MMC_SD | reg_val | WPP_ACTIVEHIGH | CDP_ACTIVEHIGH |
545 MIT_CTO | DW8_1_4BITMODE | MODE_FUNC | STR_BLOCK |
546 HR_NOHOSTRESP | INIT_NOINIT | NOOPENDRAIN, &mmc_base->con);
547
548 dsor = 240;
549 mmc_reg_out(&mmc_base->sysctl, (ICE_MASK | DTO_MASK | CEN_MASK),
Kishon Vijay Abraham I29171dc2017-09-21 16:51:36 +0200550 (ICE_STOP | DTO_15THDTO));
Sukumar Ghoraide941242010-09-18 20:32:33 -0700551 mmc_reg_out(&mmc_base->sysctl, ICE_MASK | CLKD_MASK,
552 (dsor << CLKD_OFFSET) | ICE_OSCILLATE);
Nishanth Menoneb9a28f2010-11-19 11:18:12 -0500553 start = get_timer(0);
554 while ((readl(&mmc_base->sysctl) & ICS_MASK) == ICS_NOTREADY) {
555 if (get_timer(0) - start > MAX_RETRY_MS) {
556 printf("%s: timedout waiting for ics!\n", __func__);
Jaehoon Chung915ffa52016-07-19 16:33:36 +0900557 return -ETIMEDOUT;
Nishanth Menoneb9a28f2010-11-19 11:18:12 -0500558 }
559 }
Sukumar Ghoraide941242010-09-18 20:32:33 -0700560 writel(readl(&mmc_base->sysctl) | CEN_ENABLE, &mmc_base->sysctl);
561
562 writel(readl(&mmc_base->hctl) | SDBP_PWRON, &mmc_base->hctl);
563
Jean-Jacques Hiblot2faa1a32018-01-30 16:01:36 +0100564 mmc_enable_irq(mmc, NULL);
Sukumar Ghoraide941242010-09-18 20:32:33 -0700565 mmc_init_stream(mmc_base);
566
567 return 0;
568}
569
Grazvydas Ignotas25c719e2012-03-19 12:12:06 +0000570/*
571 * MMC controller internal finite state machine reset
572 *
573 * Used to reset command or data internal state machines, using respectively
574 * SRC or SRD bit of SYSCTL register
575 */
576static void mmc_reset_controller_fsm(struct hsmmc *mmc_base, u32 bit)
577{
578 ulong start;
579
580 mmc_reg_out(&mmc_base->sysctl, bit, bit);
581
Oleksandr Tyshchenko61a6cc22013-08-06 13:44:16 +0300582 /*
583 * CMD(DAT) lines reset procedures are slightly different
584 * for OMAP3 and OMAP4(AM335x,OMAP5,DRA7xx).
585 * According to OMAP3 TRM:
586 * Set SRC(SRD) bit in MMCHS_SYSCTL register to 0x1 and wait until it
587 * returns to 0x0.
588 * According to OMAP4(AM335x,OMAP5,DRA7xx) TRMs, CMD(DATA) lines reset
589 * procedure steps must be as follows:
590 * 1. Initiate CMD(DAT) line reset by writing 0x1 to SRC(SRD) bit in
591 * MMCHS_SYSCTL register (SD_SYSCTL for AM335x).
592 * 2. Poll the SRC(SRD) bit until it is set to 0x1.
593 * 3. Wait until the SRC (SRD) bit returns to 0x0
594 * (reset procedure is completed).
595 */
596#if defined(CONFIG_OMAP44XX) || defined(CONFIG_OMAP54XX) || \
Nikita Kiryanovdce55b92015-07-30 23:56:20 +0300597 defined(CONFIG_AM33XX) || defined(CONFIG_AM43XX)
Oleksandr Tyshchenko61a6cc22013-08-06 13:44:16 +0300598 if (!(readl(&mmc_base->sysctl) & bit)) {
599 start = get_timer(0);
600 while (!(readl(&mmc_base->sysctl) & bit)) {
601 if (get_timer(0) - start > MAX_RETRY_MS)
602 return;
603 }
604 }
605#endif
Grazvydas Ignotas25c719e2012-03-19 12:12:06 +0000606 start = get_timer(0);
607 while ((readl(&mmc_base->sysctl) & bit) != 0) {
608 if (get_timer(0) - start > MAX_RETRY_MS) {
609 printf("%s: timedout waiting for sysctl %x to clear\n",
610 __func__, bit);
611 return;
612 }
613 }
614}
Kishon Vijay Abraham If0d53e82017-09-21 16:51:34 +0200615
616#ifndef CONFIG_OMAP34XX
617static void omap_hsmmc_adma_desc(struct mmc *mmc, char *buf, u16 len, bool end)
618{
619 struct omap_hsmmc_data *priv = omap_hsmmc_get_data(mmc);
620 struct omap_hsmmc_adma_desc *desc;
621 u8 attr;
622
623 desc = &priv->adma_desc_table[priv->desc_slot];
624
625 attr = ADMA_DESC_ATTR_VALID | ADMA_DESC_TRANSFER_DATA;
626 if (!end)
627 priv->desc_slot++;
628 else
629 attr |= ADMA_DESC_ATTR_END;
630
631 desc->len = len;
632 desc->addr = (u32)buf;
633 desc->reserved = 0;
634 desc->attr = attr;
635}
636
637static void omap_hsmmc_prepare_adma_table(struct mmc *mmc,
638 struct mmc_data *data)
639{
640 uint total_len = data->blocksize * data->blocks;
641 uint desc_count = DIV_ROUND_UP(total_len, ADMA_MAX_LEN);
642 struct omap_hsmmc_data *priv = omap_hsmmc_get_data(mmc);
643 int i = desc_count;
644 char *buf;
645
646 priv->desc_slot = 0;
647 priv->adma_desc_table = (struct omap_hsmmc_adma_desc *)
648 memalign(ARCH_DMA_MINALIGN, desc_count *
649 sizeof(struct omap_hsmmc_adma_desc));
650
651 if (data->flags & MMC_DATA_READ)
652 buf = data->dest;
653 else
654 buf = (char *)data->src;
655
656 while (--i) {
657 omap_hsmmc_adma_desc(mmc, buf, ADMA_MAX_LEN, false);
658 buf += ADMA_MAX_LEN;
659 total_len -= ADMA_MAX_LEN;
660 }
661
662 omap_hsmmc_adma_desc(mmc, buf, total_len, true);
663
664 flush_dcache_range((long)priv->adma_desc_table,
665 (long)priv->adma_desc_table +
666 ROUND(desc_count *
667 sizeof(struct omap_hsmmc_adma_desc),
668 ARCH_DMA_MINALIGN));
669}
670
671static void omap_hsmmc_prepare_data(struct mmc *mmc, struct mmc_data *data)
672{
673 struct hsmmc *mmc_base;
674 struct omap_hsmmc_data *priv = omap_hsmmc_get_data(mmc);
675 u32 val;
676 char *buf;
677
678 mmc_base = priv->base_addr;
679 omap_hsmmc_prepare_adma_table(mmc, data);
680
681 if (data->flags & MMC_DATA_READ)
682 buf = data->dest;
683 else
684 buf = (char *)data->src;
685
686 val = readl(&mmc_base->hctl);
687 val |= DMA_SELECT;
688 writel(val, &mmc_base->hctl);
689
690 val = readl(&mmc_base->con);
691 val |= DMA_MASTER;
692 writel(val, &mmc_base->con);
693
694 writel((u32)priv->adma_desc_table, &mmc_base->admasal);
695
696 flush_dcache_range((u32)buf,
697 (u32)buf +
698 ROUND(data->blocksize * data->blocks,
699 ARCH_DMA_MINALIGN));
700}
701
702static void omap_hsmmc_dma_cleanup(struct mmc *mmc)
703{
704 struct hsmmc *mmc_base;
705 struct omap_hsmmc_data *priv = omap_hsmmc_get_data(mmc);
706 u32 val;
707
708 mmc_base = priv->base_addr;
709
710 val = readl(&mmc_base->con);
711 val &= ~DMA_MASTER;
712 writel(val, &mmc_base->con);
713
714 val = readl(&mmc_base->hctl);
715 val &= ~DMA_SELECT;
716 writel(val, &mmc_base->hctl);
717
718 kfree(priv->adma_desc_table);
719}
720#else
721#define omap_hsmmc_adma_desc
722#define omap_hsmmc_prepare_adma_table
723#define omap_hsmmc_prepare_data
724#define omap_hsmmc_dma_cleanup
725#endif
726
Simon Glassc4d660d2017-07-04 13:31:19 -0600727#if !CONFIG_IS_ENABLED(DM_MMC)
Pantelis Antoniouab769f22014-02-26 19:28:45 +0200728static int omap_hsmmc_send_cmd(struct mmc *mmc, struct mmc_cmd *cmd,
Sukumar Ghoraide941242010-09-18 20:32:33 -0700729 struct mmc_data *data)
730{
Jean-Jacques Hiblotae000e22017-03-22 16:00:31 +0100731 struct omap_hsmmc_data *priv = omap_hsmmc_get_data(mmc);
Jean-Jacques Hiblotb5511d62017-04-14 19:50:02 +0200732#else
733static int omap_hsmmc_send_cmd(struct udevice *dev, struct mmc_cmd *cmd,
734 struct mmc_data *data)
735{
736 struct omap_hsmmc_data *priv = dev_get_priv(dev);
Kishon Vijay Abraham If0d53e82017-09-21 16:51:34 +0200737 struct mmc_uclass_priv *upriv = dev_get_uclass_priv(dev);
738 struct mmc *mmc = upriv->mmc;
739#endif
Nikita Kiryanovcc22b0c2012-12-03 02:19:43 +0000740 struct hsmmc *mmc_base;
Sukumar Ghoraide941242010-09-18 20:32:33 -0700741 unsigned int flags, mmc_stat;
Nishanth Menoneb9a28f2010-11-19 11:18:12 -0500742 ulong start;
Sukumar Ghoraide941242010-09-18 20:32:33 -0700743
Jean-Jacques Hiblotae000e22017-03-22 16:00:31 +0100744 mmc_base = priv->base_addr;
Kishon Vijay Abraham I866bb982017-09-21 16:51:35 +0200745
746 if (cmd->cmdidx == MMC_CMD_STOP_TRANSMISSION)
747 return 0;
748
Nishanth Menoneb9a28f2010-11-19 11:18:12 -0500749 start = get_timer(0);
Tom Rinia7778f82012-01-30 11:22:25 +0000750 while ((readl(&mmc_base->pstate) & (DATI_MASK | CMDI_MASK)) != 0) {
Nishanth Menoneb9a28f2010-11-19 11:18:12 -0500751 if (get_timer(0) - start > MAX_RETRY_MS) {
Tom Rinia7778f82012-01-30 11:22:25 +0000752 printf("%s: timedout waiting on cmd inhibit to clear\n",
753 __func__);
Jaehoon Chung915ffa52016-07-19 16:33:36 +0900754 return -ETIMEDOUT;
Nishanth Menoneb9a28f2010-11-19 11:18:12 -0500755 }
756 }
Sukumar Ghoraide941242010-09-18 20:32:33 -0700757 writel(0xFFFFFFFF, &mmc_base->stat);
Nishanth Menoneb9a28f2010-11-19 11:18:12 -0500758 start = get_timer(0);
759 while (readl(&mmc_base->stat)) {
760 if (get_timer(0) - start > MAX_RETRY_MS) {
Grazvydas Ignotas15ceb1d2012-03-19 12:11:43 +0000761 printf("%s: timedout waiting for STAT (%x) to clear\n",
762 __func__, readl(&mmc_base->stat));
Jaehoon Chung915ffa52016-07-19 16:33:36 +0900763 return -ETIMEDOUT;
Nishanth Menoneb9a28f2010-11-19 11:18:12 -0500764 }
765 }
Sukumar Ghoraide941242010-09-18 20:32:33 -0700766 /*
767 * CMDREG
768 * CMDIDX[13:8] : Command index
769 * DATAPRNT[5] : Data Present Select
770 * ENCMDIDX[4] : Command Index Check Enable
771 * ENCMDCRC[3] : Command CRC Check Enable
772 * RSPTYP[1:0]
773 * 00 = No Response
774 * 01 = Length 136
775 * 10 = Length 48
776 * 11 = Length 48 Check busy after response
777 */
778 /* Delay added before checking the status of frq change
779 * retry not supported by mmc.c(core file)
780 */
781 if (cmd->cmdidx == SD_CMD_APP_SEND_SCR)
782 udelay(50000); /* wait 50 ms */
783
784 if (!(cmd->resp_type & MMC_RSP_PRESENT))
785 flags = 0;
786 else if (cmd->resp_type & MMC_RSP_136)
787 flags = RSP_TYPE_LGHT136 | CICE_NOCHECK;
788 else if (cmd->resp_type & MMC_RSP_BUSY)
789 flags = RSP_TYPE_LGHT48B;
790 else
791 flags = RSP_TYPE_LGHT48;
792
793 /* enable default flags */
794 flags = flags | (CMD_TYPE_NORMAL | CICE_NOCHECK | CCCE_NOCHECK |
Kishon Vijay Abraham I29171dc2017-09-21 16:51:36 +0200795 MSBS_SGLEBLK);
796 flags &= ~(ACEN_ENABLE | BCE_ENABLE | DE_ENABLE);
Sukumar Ghoraide941242010-09-18 20:32:33 -0700797
798 if (cmd->resp_type & MMC_RSP_CRC)
799 flags |= CCCE_CHECK;
800 if (cmd->resp_type & MMC_RSP_OPCODE)
801 flags |= CICE_CHECK;
802
803 if (data) {
804 if ((cmd->cmdidx == MMC_CMD_READ_MULTIPLE_BLOCK) ||
805 (cmd->cmdidx == MMC_CMD_WRITE_MULTIPLE_BLOCK)) {
Kishon Vijay Abraham I866bb982017-09-21 16:51:35 +0200806 flags |= (MSBS_MULTIBLK | BCE_ENABLE | ACEN_ENABLE);
Sukumar Ghoraide941242010-09-18 20:32:33 -0700807 data->blocksize = 512;
808 writel(data->blocksize | (data->blocks << 16),
809 &mmc_base->blk);
810 } else
811 writel(data->blocksize | NBLK_STPCNT, &mmc_base->blk);
812
813 if (data->flags & MMC_DATA_READ)
814 flags |= (DP_DATA | DDIR_READ);
815 else
816 flags |= (DP_DATA | DDIR_WRITE);
Kishon Vijay Abraham If0d53e82017-09-21 16:51:34 +0200817
818#ifndef CONFIG_OMAP34XX
819 if ((priv->controller_flags & OMAP_HSMMC_USE_ADMA) &&
820 !mmc_is_tuning_cmd(cmd->cmdidx)) {
821 omap_hsmmc_prepare_data(mmc, data);
822 flags |= DE_ENABLE;
823 }
824#endif
Sukumar Ghoraide941242010-09-18 20:32:33 -0700825 }
826
Jean-Jacques Hiblot2faa1a32018-01-30 16:01:36 +0100827 mmc_enable_irq(mmc, cmd);
828
Sukumar Ghoraide941242010-09-18 20:32:33 -0700829 writel(cmd->cmdarg, &mmc_base->arg);
Lubomir Popov152ba362013-08-14 18:59:18 +0300830 udelay(20); /* To fix "No status update" error on eMMC */
Sukumar Ghoraide941242010-09-18 20:32:33 -0700831 writel((cmd->cmdidx << 24) | flags, &mmc_base->cmd);
832
Nishanth Menoneb9a28f2010-11-19 11:18:12 -0500833 start = get_timer(0);
Sukumar Ghoraide941242010-09-18 20:32:33 -0700834 do {
835 mmc_stat = readl(&mmc_base->stat);
Kishon Vijay Abraham If0d53e82017-09-21 16:51:34 +0200836 if (get_timer(start) > MAX_RETRY_MS) {
Nishanth Menoneb9a28f2010-11-19 11:18:12 -0500837 printf("%s : timeout: No status update\n", __func__);
Jaehoon Chung915ffa52016-07-19 16:33:36 +0900838 return -ETIMEDOUT;
Nishanth Menoneb9a28f2010-11-19 11:18:12 -0500839 }
840 } while (!mmc_stat);
Sukumar Ghoraide941242010-09-18 20:32:33 -0700841
Grazvydas Ignotas25c719e2012-03-19 12:12:06 +0000842 if ((mmc_stat & IE_CTO) != 0) {
843 mmc_reset_controller_fsm(mmc_base, SYSCTL_SRC);
Jaehoon Chung915ffa52016-07-19 16:33:36 +0900844 return -ETIMEDOUT;
Grazvydas Ignotas25c719e2012-03-19 12:12:06 +0000845 } else if ((mmc_stat & ERRI_MASK) != 0)
Sukumar Ghoraide941242010-09-18 20:32:33 -0700846 return -1;
847
848 if (mmc_stat & CC_MASK) {
849 writel(CC_MASK, &mmc_base->stat);
850 if (cmd->resp_type & MMC_RSP_PRESENT) {
851 if (cmd->resp_type & MMC_RSP_136) {
852 /* response type 2 */
853 cmd->response[3] = readl(&mmc_base->rsp10);
854 cmd->response[2] = readl(&mmc_base->rsp32);
855 cmd->response[1] = readl(&mmc_base->rsp54);
856 cmd->response[0] = readl(&mmc_base->rsp76);
857 } else
858 /* response types 1, 1b, 3, 4, 5, 6 */
859 cmd->response[0] = readl(&mmc_base->rsp10);
860 }
861 }
862
Kishon Vijay Abraham If0d53e82017-09-21 16:51:34 +0200863#ifndef CONFIG_OMAP34XX
864 if ((priv->controller_flags & OMAP_HSMMC_USE_ADMA) && data &&
865 !mmc_is_tuning_cmd(cmd->cmdidx)) {
866 u32 sz_mb, timeout;
867
868 if (mmc_stat & IE_ADMAE) {
869 omap_hsmmc_dma_cleanup(mmc);
870 return -EIO;
871 }
872
873 sz_mb = DIV_ROUND_UP(data->blocksize * data->blocks, 1 << 20);
874 timeout = sz_mb * DMA_TIMEOUT_PER_MB;
875 if (timeout < MAX_RETRY_MS)
876 timeout = MAX_RETRY_MS;
877
878 start = get_timer(0);
879 do {
880 mmc_stat = readl(&mmc_base->stat);
881 if (mmc_stat & TC_MASK) {
882 writel(readl(&mmc_base->stat) | TC_MASK,
883 &mmc_base->stat);
884 break;
885 }
886 if (get_timer(start) > timeout) {
887 printf("%s : DMA timeout: No status update\n",
888 __func__);
889 return -ETIMEDOUT;
890 }
891 } while (1);
892
893 omap_hsmmc_dma_cleanup(mmc);
894 return 0;
895 }
896#endif
897
Sukumar Ghoraide941242010-09-18 20:32:33 -0700898 if (data && (data->flags & MMC_DATA_READ)) {
899 mmc_read_data(mmc_base, data->dest,
900 data->blocksize * data->blocks);
901 } else if (data && (data->flags & MMC_DATA_WRITE)) {
902 mmc_write_data(mmc_base, data->src,
903 data->blocksize * data->blocks);
904 }
905 return 0;
906}
907
Sricharan933efe62011-11-15 09:49:53 -0500908static int mmc_read_data(struct hsmmc *mmc_base, char *buf, unsigned int size)
Sukumar Ghoraide941242010-09-18 20:32:33 -0700909{
910 unsigned int *output_buf = (unsigned int *)buf;
911 unsigned int mmc_stat;
912 unsigned int count;
913
914 /*
915 * Start Polled Read
916 */
917 count = (size > MMCSD_SECTOR_SIZE) ? MMCSD_SECTOR_SIZE : size;
918 count /= 4;
919
920 while (size) {
Nishanth Menoneb9a28f2010-11-19 11:18:12 -0500921 ulong start = get_timer(0);
Sukumar Ghoraide941242010-09-18 20:32:33 -0700922 do {
923 mmc_stat = readl(&mmc_base->stat);
Nishanth Menoneb9a28f2010-11-19 11:18:12 -0500924 if (get_timer(0) - start > MAX_RETRY_MS) {
925 printf("%s: timedout waiting for status!\n",
926 __func__);
Jaehoon Chung915ffa52016-07-19 16:33:36 +0900927 return -ETIMEDOUT;
Nishanth Menoneb9a28f2010-11-19 11:18:12 -0500928 }
Sukumar Ghoraide941242010-09-18 20:32:33 -0700929 } while (mmc_stat == 0);
930
Grazvydas Ignotas25c719e2012-03-19 12:12:06 +0000931 if ((mmc_stat & (IE_DTO | IE_DCRC | IE_DEB)) != 0)
932 mmc_reset_controller_fsm(mmc_base, SYSCTL_SRD);
933
Sukumar Ghoraide941242010-09-18 20:32:33 -0700934 if ((mmc_stat & ERRI_MASK) != 0)
935 return 1;
936
937 if (mmc_stat & BRR_MASK) {
938 unsigned int k;
939
940 writel(readl(&mmc_base->stat) | BRR_MASK,
941 &mmc_base->stat);
942 for (k = 0; k < count; k++) {
943 *output_buf = readl(&mmc_base->data);
944 output_buf++;
945 }
946 size -= (count*4);
947 }
948
949 if (mmc_stat & BWR_MASK)
950 writel(readl(&mmc_base->stat) | BWR_MASK,
951 &mmc_base->stat);
952
953 if (mmc_stat & TC_MASK) {
954 writel(readl(&mmc_base->stat) | TC_MASK,
955 &mmc_base->stat);
956 break;
957 }
958 }
959 return 0;
960}
961
Sricharan933efe62011-11-15 09:49:53 -0500962static int mmc_write_data(struct hsmmc *mmc_base, const char *buf,
963 unsigned int size)
Sukumar Ghoraide941242010-09-18 20:32:33 -0700964{
965 unsigned int *input_buf = (unsigned int *)buf;
966 unsigned int mmc_stat;
967 unsigned int count;
968
969 /*
Lubomir Popov152ba362013-08-14 18:59:18 +0300970 * Start Polled Write
Sukumar Ghoraide941242010-09-18 20:32:33 -0700971 */
972 count = (size > MMCSD_SECTOR_SIZE) ? MMCSD_SECTOR_SIZE : size;
973 count /= 4;
974
975 while (size) {
Nishanth Menoneb9a28f2010-11-19 11:18:12 -0500976 ulong start = get_timer(0);
Sukumar Ghoraide941242010-09-18 20:32:33 -0700977 do {
978 mmc_stat = readl(&mmc_base->stat);
Nishanth Menoneb9a28f2010-11-19 11:18:12 -0500979 if (get_timer(0) - start > MAX_RETRY_MS) {
980 printf("%s: timedout waiting for status!\n",
981 __func__);
Jaehoon Chung915ffa52016-07-19 16:33:36 +0900982 return -ETIMEDOUT;
Nishanth Menoneb9a28f2010-11-19 11:18:12 -0500983 }
Sukumar Ghoraide941242010-09-18 20:32:33 -0700984 } while (mmc_stat == 0);
985
Grazvydas Ignotas25c719e2012-03-19 12:12:06 +0000986 if ((mmc_stat & (IE_DTO | IE_DCRC | IE_DEB)) != 0)
987 mmc_reset_controller_fsm(mmc_base, SYSCTL_SRD);
988
Sukumar Ghoraide941242010-09-18 20:32:33 -0700989 if ((mmc_stat & ERRI_MASK) != 0)
990 return 1;
991
992 if (mmc_stat & BWR_MASK) {
993 unsigned int k;
994
995 writel(readl(&mmc_base->stat) | BWR_MASK,
996 &mmc_base->stat);
997 for (k = 0; k < count; k++) {
998 writel(*input_buf, &mmc_base->data);
999 input_buf++;
1000 }
1001 size -= (count*4);
1002 }
1003
1004 if (mmc_stat & BRR_MASK)
1005 writel(readl(&mmc_base->stat) | BRR_MASK,
1006 &mmc_base->stat);
1007
1008 if (mmc_stat & TC_MASK) {
1009 writel(readl(&mmc_base->stat) | TC_MASK,
1010 &mmc_base->stat);
1011 break;
1012 }
1013 }
1014 return 0;
1015}
1016
Jean-Jacques Hiblot5baf5432018-01-30 16:01:30 +01001017static void omap_hsmmc_stop_clock(struct hsmmc *mmc_base)
1018{
1019 writel(readl(&mmc_base->sysctl) & ~CEN_ENABLE, &mmc_base->sysctl);
1020}
1021
1022static void omap_hsmmc_start_clock(struct hsmmc *mmc_base)
1023{
1024 writel(readl(&mmc_base->sysctl) | CEN_ENABLE, &mmc_base->sysctl);
1025}
1026
1027static void omap_hsmmc_set_clock(struct mmc *mmc)
1028{
1029 struct omap_hsmmc_data *priv = omap_hsmmc_get_data(mmc);
1030 struct hsmmc *mmc_base;
1031 unsigned int dsor = 0;
1032 ulong start;
1033
1034 mmc_base = priv->base_addr;
1035 omap_hsmmc_stop_clock(mmc_base);
1036
1037 /* TODO: Is setting DTO required here? */
1038 mmc_reg_out(&mmc_base->sysctl, (ICE_MASK | DTO_MASK),
1039 (ICE_STOP | DTO_15THDTO));
1040
1041 if (mmc->clock != 0) {
1042 dsor = DIV_ROUND_UP(MMC_CLOCK_REFERENCE * 1000000, mmc->clock);
1043 if (dsor > CLKD_MAX)
1044 dsor = CLKD_MAX;
1045 } else {
1046 dsor = CLKD_MAX;
1047 }
1048
1049 mmc_reg_out(&mmc_base->sysctl, ICE_MASK | CLKD_MASK,
1050 (dsor << CLKD_OFFSET) | ICE_OSCILLATE);
1051
1052 start = get_timer(0);
1053 while ((readl(&mmc_base->sysctl) & ICS_MASK) == ICS_NOTREADY) {
1054 if (get_timer(0) - start > MAX_RETRY_MS) {
1055 printf("%s: timedout waiting for ics!\n", __func__);
1056 return;
1057 }
1058 }
1059
1060 priv->clock = mmc->clock;
1061 omap_hsmmc_start_clock(mmc_base);
1062}
1063
Kishon Vijay Abraham I48a2f112018-01-30 16:01:31 +01001064static void omap_hsmmc_set_bus_width(struct mmc *mmc)
Sukumar Ghoraide941242010-09-18 20:32:33 -07001065{
Jean-Jacques Hiblotae000e22017-03-22 16:00:31 +01001066 struct omap_hsmmc_data *priv = omap_hsmmc_get_data(mmc);
Nikita Kiryanovcc22b0c2012-12-03 02:19:43 +00001067 struct hsmmc *mmc_base;
Sukumar Ghoraide941242010-09-18 20:32:33 -07001068
Jean-Jacques Hiblotae000e22017-03-22 16:00:31 +01001069 mmc_base = priv->base_addr;
Sukumar Ghoraide941242010-09-18 20:32:33 -07001070 /* configue bus width */
1071 switch (mmc->bus_width) {
1072 case 8:
1073 writel(readl(&mmc_base->con) | DTW_8_BITMODE,
1074 &mmc_base->con);
1075 break;
1076
1077 case 4:
1078 writel(readl(&mmc_base->con) & ~DTW_8_BITMODE,
1079 &mmc_base->con);
1080 writel(readl(&mmc_base->hctl) | DTW_4_BITMODE,
1081 &mmc_base->hctl);
1082 break;
1083
1084 case 1:
1085 default:
1086 writel(readl(&mmc_base->con) & ~DTW_8_BITMODE,
1087 &mmc_base->con);
1088 writel(readl(&mmc_base->hctl) & ~DTW_4_BITMODE,
1089 &mmc_base->hctl);
1090 break;
1091 }
1092
Kishon Vijay Abraham I48a2f112018-01-30 16:01:31 +01001093 priv->bus_width = mmc->bus_width;
1094}
1095
1096#if !CONFIG_IS_ENABLED(DM_MMC)
1097static int omap_hsmmc_set_ios(struct mmc *mmc)
1098{
1099 struct omap_hsmmc_data *priv = omap_hsmmc_get_data(mmc);
1100#else
1101static int omap_hsmmc_set_ios(struct udevice *dev)
1102{
1103 struct omap_hsmmc_data *priv = dev_get_priv(dev);
1104 struct mmc_uclass_priv *upriv = dev_get_uclass_priv(dev);
1105 struct mmc *mmc = upriv->mmc;
1106#endif
1107
1108 if (priv->bus_width != mmc->bus_width)
1109 omap_hsmmc_set_bus_width(mmc);
1110
Jean-Jacques Hiblot5baf5432018-01-30 16:01:30 +01001111 if (priv->clock != mmc->clock)
1112 omap_hsmmc_set_clock(mmc);
Jaehoon Chung07b0b9c2016-12-30 15:30:16 +09001113
Jean-Jacques Hiblot8fc238b2018-01-30 16:01:33 +01001114#if CONFIG_IS_ENABLED(DM_MMC)
1115 if (priv->mode != mmc->selected_mode)
1116 omap_hsmmc_set_timing(mmc);
1117#endif
Jaehoon Chung07b0b9c2016-12-30 15:30:16 +09001118 return 0;
Sukumar Ghoraide941242010-09-18 20:32:33 -07001119}
1120
Pantelis Antoniouab769f22014-02-26 19:28:45 +02001121#ifdef OMAP_HSMMC_USE_GPIO
Simon Glassc4d660d2017-07-04 13:31:19 -06001122#if CONFIG_IS_ENABLED(DM_MMC)
Jean-Jacques Hiblotb5511d62017-04-14 19:50:02 +02001123static int omap_hsmmc_getcd(struct udevice *dev)
Mugunthan V Na9d6a7e2015-09-28 12:56:30 +05301124{
Jean-Jacques Hiblotb5511d62017-04-14 19:50:02 +02001125 struct omap_hsmmc_data *priv = dev_get_priv(dev);
Mugunthan V Na9d6a7e2015-09-28 12:56:30 +05301126 int value;
1127
1128 value = dm_gpio_get_value(&priv->cd_gpio);
1129 /* if no CD return as 1 */
1130 if (value < 0)
1131 return 1;
1132
1133 if (priv->cd_inverted)
1134 return !value;
1135 return value;
1136}
1137
Jean-Jacques Hiblotb5511d62017-04-14 19:50:02 +02001138static int omap_hsmmc_getwp(struct udevice *dev)
Mugunthan V Na9d6a7e2015-09-28 12:56:30 +05301139{
Jean-Jacques Hiblotb5511d62017-04-14 19:50:02 +02001140 struct omap_hsmmc_data *priv = dev_get_priv(dev);
Mugunthan V Na9d6a7e2015-09-28 12:56:30 +05301141 int value;
1142
1143 value = dm_gpio_get_value(&priv->wp_gpio);
1144 /* if no WP return as 0 */
1145 if (value < 0)
1146 return 0;
1147 return value;
1148}
1149#else
Pantelis Antoniouab769f22014-02-26 19:28:45 +02001150static int omap_hsmmc_getcd(struct mmc *mmc)
1151{
Jean-Jacques Hiblotae000e22017-03-22 16:00:31 +01001152 struct omap_hsmmc_data *priv = omap_hsmmc_get_data(mmc);
Pantelis Antoniouab769f22014-02-26 19:28:45 +02001153 int cd_gpio;
1154
1155 /* if no CD return as 1 */
Jean-Jacques Hiblotae000e22017-03-22 16:00:31 +01001156 cd_gpio = priv->cd_gpio;
Pantelis Antoniouab769f22014-02-26 19:28:45 +02001157 if (cd_gpio < 0)
1158 return 1;
1159
Igor Grinberg0b03a932014-11-03 11:32:23 +02001160 /* NOTE: assumes card detect signal is active-low */
1161 return !gpio_get_value(cd_gpio);
Pantelis Antoniouab769f22014-02-26 19:28:45 +02001162}
1163
1164static int omap_hsmmc_getwp(struct mmc *mmc)
1165{
Jean-Jacques Hiblotae000e22017-03-22 16:00:31 +01001166 struct omap_hsmmc_data *priv = omap_hsmmc_get_data(mmc);
Pantelis Antoniouab769f22014-02-26 19:28:45 +02001167 int wp_gpio;
1168
1169 /* if no WP return as 0 */
Jean-Jacques Hiblotae000e22017-03-22 16:00:31 +01001170 wp_gpio = priv->wp_gpio;
Pantelis Antoniouab769f22014-02-26 19:28:45 +02001171 if (wp_gpio < 0)
1172 return 0;
1173
Igor Grinberg0b03a932014-11-03 11:32:23 +02001174 /* NOTE: assumes write protect signal is active-high */
Pantelis Antoniouab769f22014-02-26 19:28:45 +02001175 return gpio_get_value(wp_gpio);
1176}
1177#endif
Mugunthan V Na9d6a7e2015-09-28 12:56:30 +05301178#endif
Pantelis Antoniouab769f22014-02-26 19:28:45 +02001179
Simon Glassc4d660d2017-07-04 13:31:19 -06001180#if CONFIG_IS_ENABLED(DM_MMC)
Jean-Jacques Hiblotb5511d62017-04-14 19:50:02 +02001181static const struct dm_mmc_ops omap_hsmmc_ops = {
1182 .send_cmd = omap_hsmmc_send_cmd,
1183 .set_ios = omap_hsmmc_set_ios,
1184#ifdef OMAP_HSMMC_USE_GPIO
1185 .get_cd = omap_hsmmc_getcd,
1186 .get_wp = omap_hsmmc_getwp,
1187#endif
Jean-Jacques Hiblot14761ca2018-01-30 16:01:35 +01001188#ifdef MMC_SUPPORTS_TUNING
1189 .execute_tuning = omap_hsmmc_execute_tuning,
1190#endif
Jean-Jacques Hiblotb5511d62017-04-14 19:50:02 +02001191};
1192#else
Pantelis Antoniouab769f22014-02-26 19:28:45 +02001193static const struct mmc_ops omap_hsmmc_ops = {
1194 .send_cmd = omap_hsmmc_send_cmd,
1195 .set_ios = omap_hsmmc_set_ios,
1196 .init = omap_hsmmc_init_setup,
1197#ifdef OMAP_HSMMC_USE_GPIO
1198 .getcd = omap_hsmmc_getcd,
1199 .getwp = omap_hsmmc_getwp,
1200#endif
1201};
Jean-Jacques Hiblotb5511d62017-04-14 19:50:02 +02001202#endif
Pantelis Antoniouab769f22014-02-26 19:28:45 +02001203
Simon Glassc4d660d2017-07-04 13:31:19 -06001204#if !CONFIG_IS_ENABLED(DM_MMC)
Nikita Kiryanove3913f52012-12-03 02:19:47 +00001205int omap_mmc_init(int dev_index, uint host_caps_mask, uint f_max, int cd_gpio,
1206 int wp_gpio)
Sukumar Ghoraide941242010-09-18 20:32:33 -07001207{
Pantelis Antoniou93bfd612014-03-11 19:34:20 +02001208 struct mmc *mmc;
Jean-Jacques Hiblotae000e22017-03-22 16:00:31 +01001209 struct omap_hsmmc_data *priv;
Pantelis Antoniou93bfd612014-03-11 19:34:20 +02001210 struct mmc_config *cfg;
1211 uint host_caps_val;
Sukumar Ghoraide941242010-09-18 20:32:33 -07001212
Jean-Jacques Hiblotae000e22017-03-22 16:00:31 +01001213 priv = malloc(sizeof(*priv));
1214 if (priv == NULL)
Pantelis Antoniou93bfd612014-03-11 19:34:20 +02001215 return -1;
1216
Rob Herring5a203972015-03-23 17:56:59 -05001217 host_caps_val = MMC_MODE_4BIT | MMC_MODE_HS_52MHz | MMC_MODE_HS;
Sukumar Ghoraide941242010-09-18 20:32:33 -07001218
1219 switch (dev_index) {
1220 case 0:
Jean-Jacques Hiblotae000e22017-03-22 16:00:31 +01001221 priv->base_addr = (struct hsmmc *)OMAP_HSMMC1_BASE;
Sukumar Ghoraide941242010-09-18 20:32:33 -07001222 break;
Tom Rini1037d582011-10-12 06:20:50 +00001223#ifdef OMAP_HSMMC2_BASE
Sukumar Ghoraide941242010-09-18 20:32:33 -07001224 case 1:
Jean-Jacques Hiblotae000e22017-03-22 16:00:31 +01001225 priv->base_addr = (struct hsmmc *)OMAP_HSMMC2_BASE;
Lubomir Popov152ba362013-08-14 18:59:18 +03001226#if (defined(CONFIG_OMAP44XX) || defined(CONFIG_OMAP54XX) || \
Nishanth Menon3891a542016-11-29 15:22:00 +05301227 defined(CONFIG_DRA7XX) || defined(CONFIG_AM33XX) || \
Roger Quadros3b689392015-09-19 16:26:53 +05301228 defined(CONFIG_AM43XX) || defined(CONFIG_SOC_KEYSTONE)) && \
1229 defined(CONFIG_HSMMC2_8BIT)
Lubomir Popov152ba362013-08-14 18:59:18 +03001230 /* Enable 8-bit interface for eMMC on OMAP4/5 or DRA7XX */
1231 host_caps_val |= MMC_MODE_8BIT;
1232#endif
Sukumar Ghoraide941242010-09-18 20:32:33 -07001233 break;
Tom Rini1037d582011-10-12 06:20:50 +00001234#endif
1235#ifdef OMAP_HSMMC3_BASE
Sukumar Ghoraide941242010-09-18 20:32:33 -07001236 case 2:
Jean-Jacques Hiblotae000e22017-03-22 16:00:31 +01001237 priv->base_addr = (struct hsmmc *)OMAP_HSMMC3_BASE;
Nishanth Menon3891a542016-11-29 15:22:00 +05301238#if defined(CONFIG_DRA7XX) && defined(CONFIG_HSMMC3_8BIT)
Lubomir Popov152ba362013-08-14 18:59:18 +03001239 /* Enable 8-bit interface for eMMC on DRA7XX */
1240 host_caps_val |= MMC_MODE_8BIT;
1241#endif
Sukumar Ghoraide941242010-09-18 20:32:33 -07001242 break;
Tom Rini1037d582011-10-12 06:20:50 +00001243#endif
Sukumar Ghoraide941242010-09-18 20:32:33 -07001244 default:
Jean-Jacques Hiblotae000e22017-03-22 16:00:31 +01001245 priv->base_addr = (struct hsmmc *)OMAP_HSMMC1_BASE;
Sukumar Ghoraide941242010-09-18 20:32:33 -07001246 return 1;
1247 }
Pantelis Antoniouab769f22014-02-26 19:28:45 +02001248#ifdef OMAP_HSMMC_USE_GPIO
1249 /* on error gpio values are set to -1, which is what we want */
Jean-Jacques Hiblotae000e22017-03-22 16:00:31 +01001250 priv->cd_gpio = omap_mmc_setup_gpio_in(cd_gpio, "mmc_cd");
1251 priv->wp_gpio = omap_mmc_setup_gpio_in(wp_gpio, "mmc_wp");
Pantelis Antoniouab769f22014-02-26 19:28:45 +02001252#endif
Peter Korsgaard173ddc52013-03-21 04:00:04 +00001253
Jean-Jacques Hiblotae000e22017-03-22 16:00:31 +01001254 cfg = &priv->cfg;
Sukumar Ghoraide941242010-09-18 20:32:33 -07001255
Pantelis Antoniou93bfd612014-03-11 19:34:20 +02001256 cfg->name = "OMAP SD/MMC";
1257 cfg->ops = &omap_hsmmc_ops;
1258
1259 cfg->voltages = MMC_VDD_32_33 | MMC_VDD_33_34 | MMC_VDD_165_195;
1260 cfg->host_caps = host_caps_val & ~host_caps_mask;
1261
1262 cfg->f_min = 400000;
Jonathan Solnitbbbc1ae2012-02-24 11:30:18 +00001263
1264 if (f_max != 0)
Pantelis Antoniou93bfd612014-03-11 19:34:20 +02001265 cfg->f_max = f_max;
Jonathan Solnitbbbc1ae2012-02-24 11:30:18 +00001266 else {
Pantelis Antoniou93bfd612014-03-11 19:34:20 +02001267 if (cfg->host_caps & MMC_MODE_HS) {
1268 if (cfg->host_caps & MMC_MODE_HS_52MHz)
1269 cfg->f_max = 52000000;
Jonathan Solnitbbbc1ae2012-02-24 11:30:18 +00001270 else
Pantelis Antoniou93bfd612014-03-11 19:34:20 +02001271 cfg->f_max = 26000000;
Jonathan Solnitbbbc1ae2012-02-24 11:30:18 +00001272 } else
Pantelis Antoniou93bfd612014-03-11 19:34:20 +02001273 cfg->f_max = 20000000;
Jonathan Solnitbbbc1ae2012-02-24 11:30:18 +00001274 }
Sukumar Ghoraide941242010-09-18 20:32:33 -07001275
Pantelis Antoniou93bfd612014-03-11 19:34:20 +02001276 cfg->b_max = CONFIG_SYS_MMC_MAX_BLK_COUNT;
John Rigby8feafcc2011-04-18 05:50:08 +00001277
John Rigby4ca92442011-04-19 05:48:14 +00001278#if defined(CONFIG_OMAP34XX)
1279 /*
1280 * Silicon revs 2.1 and older do not support multiblock transfers.
1281 */
1282 if ((get_cpu_family() == CPU_OMAP34XX) && (get_cpu_rev() <= CPU_3XX_ES21))
Pantelis Antoniou93bfd612014-03-11 19:34:20 +02001283 cfg->b_max = 1;
John Rigby4ca92442011-04-19 05:48:14 +00001284#endif
Jean-Jacques Hiblotae000e22017-03-22 16:00:31 +01001285 mmc = mmc_create(cfg, priv);
Pantelis Antoniou93bfd612014-03-11 19:34:20 +02001286 if (mmc == NULL)
1287 return -1;
Sukumar Ghoraide941242010-09-18 20:32:33 -07001288
1289 return 0;
1290}
Mugunthan V Na9d6a7e2015-09-28 12:56:30 +05301291#else
Lokesh Vutla2558c042017-04-26 13:37:05 +05301292#if CONFIG_IS_ENABLED(OF_CONTROL) && !CONFIG_IS_ENABLED(OF_PLATDATA)
Mugunthan V Na9d6a7e2015-09-28 12:56:30 +05301293static int omap_hsmmc_ofdata_to_platdata(struct udevice *dev)
1294{
Jean-Jacques Hiblot3d673ff2017-03-22 16:00:33 +01001295 struct omap_hsmmc_plat *plat = dev_get_platdata(dev);
1296 struct mmc_config *cfg = &plat->cfg;
Mugunthan V Na9d6a7e2015-09-28 12:56:30 +05301297 const void *fdt = gd->fdt_blob;
Simon Glasse160f7d2017-01-17 16:52:55 -07001298 int node = dev_of_offset(dev);
Mugunthan V Na9d6a7e2015-09-28 12:56:30 +05301299 int val;
1300
Simon Glassa821c4a2017-05-17 17:18:05 -06001301 plat->base_addr = map_physmem(devfdt_get_addr(dev),
1302 sizeof(struct hsmmc *),
Jean-Jacques Hiblot741726a2017-09-21 16:51:32 +02001303 MAP_NOCACHE);
Mugunthan V Na9d6a7e2015-09-28 12:56:30 +05301304
1305 cfg->host_caps = MMC_MODE_HS_52MHz | MMC_MODE_HS;
1306 val = fdtdec_get_int(fdt, node, "bus-width", -1);
1307 if (val < 0) {
1308 printf("error: bus-width property missing\n");
1309 return -ENOENT;
1310 }
1311
1312 switch (val) {
1313 case 0x8:
1314 cfg->host_caps |= MMC_MODE_8BIT;
1315 case 0x4:
1316 cfg->host_caps |= MMC_MODE_4BIT;
1317 break;
1318 default:
1319 printf("error: invalid bus-width property\n");
1320 return -ENOENT;
1321 }
1322
1323 cfg->f_min = 400000;
1324 cfg->f_max = fdtdec_get_int(fdt, node, "max-frequency", 52000000);
1325 cfg->voltages = MMC_VDD_32_33 | MMC_VDD_33_34 | MMC_VDD_165_195;
1326 cfg->b_max = CONFIG_SYS_MMC_MAX_BLK_COUNT;
Kishon Vijay Abraham Ib5944812018-01-30 16:01:32 +01001327 if (fdtdec_get_bool(fdt, node, "ti,dual-volt"))
1328 plat->controller_flags |= OMAP_HSMMC_SUPPORTS_DUAL_VOLT;
1329 if (fdtdec_get_bool(fdt, node, "no-1-8-v"))
1330 plat->controller_flags |= OMAP_HSMMC_NO_1_8_V;
Mugunthan V Na9d6a7e2015-09-28 12:56:30 +05301331
Sekhar Nori4de2de52016-08-10 19:24:03 +05301332#ifdef OMAP_HSMMC_USE_GPIO
Lokesh Vutla2558c042017-04-26 13:37:05 +05301333 plat->cd_inverted = fdtdec_get_bool(fdt, node, "cd-inverted");
Sekhar Nori4de2de52016-08-10 19:24:03 +05301334#endif
Mugunthan V Na9d6a7e2015-09-28 12:56:30 +05301335
1336 return 0;
1337}
Lokesh Vutla2558c042017-04-26 13:37:05 +05301338#endif
Mugunthan V Na9d6a7e2015-09-28 12:56:30 +05301339
Jean-Jacques Hiblot17c9a1c2017-03-22 16:00:34 +01001340#ifdef CONFIG_BLK
1341
1342static int omap_hsmmc_bind(struct udevice *dev)
1343{
1344 struct omap_hsmmc_plat *plat = dev_get_platdata(dev);
1345
1346 return mmc_bind(dev, &plat->mmc, &plat->cfg);
1347}
1348#endif
Mugunthan V Na9d6a7e2015-09-28 12:56:30 +05301349static int omap_hsmmc_probe(struct udevice *dev)
1350{
Jean-Jacques Hiblot3d673ff2017-03-22 16:00:33 +01001351 struct omap_hsmmc_plat *plat = dev_get_platdata(dev);
Mugunthan V Na9d6a7e2015-09-28 12:56:30 +05301352 struct mmc_uclass_priv *upriv = dev_get_uclass_priv(dev);
1353 struct omap_hsmmc_data *priv = dev_get_priv(dev);
Jean-Jacques Hiblot3d673ff2017-03-22 16:00:33 +01001354 struct mmc_config *cfg = &plat->cfg;
Mugunthan V Na9d6a7e2015-09-28 12:56:30 +05301355 struct mmc *mmc;
1356
Mugunthan V Na9d6a7e2015-09-28 12:56:30 +05301357 cfg->name = "OMAP SD/MMC";
Lokesh Vutla2558c042017-04-26 13:37:05 +05301358 priv->base_addr = plat->base_addr;
1359#ifdef OMAP_HSMMC_USE_GPIO
1360 priv->cd_inverted = plat->cd_inverted;
1361#endif
Mugunthan V Na9d6a7e2015-09-28 12:56:30 +05301362
Jean-Jacques Hiblot17c9a1c2017-03-22 16:00:34 +01001363#ifdef CONFIG_BLK
1364 mmc = &plat->mmc;
1365#else
Mugunthan V Na9d6a7e2015-09-28 12:56:30 +05301366 mmc = mmc_create(cfg, priv);
1367 if (mmc == NULL)
1368 return -1;
Jean-Jacques Hiblot17c9a1c2017-03-22 16:00:34 +01001369#endif
Mugunthan V Na9d6a7e2015-09-28 12:56:30 +05301370
Lokesh Vutla2558c042017-04-26 13:37:05 +05301371#if defined(OMAP_HSMMC_USE_GPIO) && CONFIG_IS_ENABLED(OF_CONTROL)
Mugunthan V N5cc6a242016-04-04 17:28:01 +05301372 gpio_request_by_name(dev, "cd-gpios", 0, &priv->cd_gpio, GPIOD_IS_IN);
1373 gpio_request_by_name(dev, "wp-gpios", 0, &priv->wp_gpio, GPIOD_IS_IN);
1374#endif
1375
Simon Glasscffe5d82016-05-01 13:52:34 -06001376 mmc->dev = dev;
Mugunthan V Na9d6a7e2015-09-28 12:56:30 +05301377 upriv->mmc = mmc;
1378
Jean-Jacques Hiblotb5511d62017-04-14 19:50:02 +02001379 return omap_hsmmc_init_setup(mmc);
Mugunthan V Na9d6a7e2015-09-28 12:56:30 +05301380}
1381
Lokesh Vutla2558c042017-04-26 13:37:05 +05301382#if CONFIG_IS_ENABLED(OF_CONTROL) && !CONFIG_IS_ENABLED(OF_PLATDATA)
Mugunthan V Na9d6a7e2015-09-28 12:56:30 +05301383static const struct udevice_id omap_hsmmc_ids[] = {
Jean-Jacques Hiblot741726a2017-09-21 16:51:32 +02001384 { .compatible = "ti,omap3-hsmmc" },
1385 { .compatible = "ti,omap4-hsmmc" },
1386 { .compatible = "ti,am33xx-hsmmc" },
Mugunthan V Na9d6a7e2015-09-28 12:56:30 +05301387 { }
1388};
Lokesh Vutla2558c042017-04-26 13:37:05 +05301389#endif
Mugunthan V Na9d6a7e2015-09-28 12:56:30 +05301390
1391U_BOOT_DRIVER(omap_hsmmc) = {
1392 .name = "omap_hsmmc",
1393 .id = UCLASS_MMC,
Lokesh Vutla2558c042017-04-26 13:37:05 +05301394#if CONFIG_IS_ENABLED(OF_CONTROL) && !CONFIG_IS_ENABLED(OF_PLATDATA)
Mugunthan V Na9d6a7e2015-09-28 12:56:30 +05301395 .of_match = omap_hsmmc_ids,
1396 .ofdata_to_platdata = omap_hsmmc_ofdata_to_platdata,
Lokesh Vutla2558c042017-04-26 13:37:05 +05301397 .platdata_auto_alloc_size = sizeof(struct omap_hsmmc_plat),
1398#endif
Jean-Jacques Hiblot17c9a1c2017-03-22 16:00:34 +01001399#ifdef CONFIG_BLK
1400 .bind = omap_hsmmc_bind,
1401#endif
Jean-Jacques Hiblotb5511d62017-04-14 19:50:02 +02001402 .ops = &omap_hsmmc_ops,
Mugunthan V Na9d6a7e2015-09-28 12:56:30 +05301403 .probe = omap_hsmmc_probe,
1404 .priv_auto_alloc_size = sizeof(struct omap_hsmmc_data),
Lokesh Vutlacbcb1702017-04-26 13:37:06 +05301405 .flags = DM_FLAG_PRE_RELOC,
Mugunthan V Na9d6a7e2015-09-28 12:56:30 +05301406};
1407#endif