blob: 4d0dc3393697363e5b1a813ba4b47fb1540f0bb3 [file] [log] [blame]
Sukumar Ghoraic53f5e52010-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>
Simon Glass63334482019-11-14 12:57:39 -070027#include <cpu_func.h>
Pantelis Antoniou2c850462014-03-11 19:34:20 +020028#include <malloc.h>
Kishon Vijay Abraham I826be2a2017-09-21 16:51:34 +020029#include <memalign.h>
Sukumar Ghoraic53f5e52010-09-18 20:32:33 -070030#include <mmc.h>
31#include <part.h>
32#include <i2c.h>
Felix Brack419eed22017-10-11 17:05:28 +020033#if defined(CONFIG_OMAP54XX) || defined(CONFIG_OMAP44XX)
Nishanth Menon627612c2013-03-26 05:20:54 +000034#include <palmas.h>
Felix Brack419eed22017-10-11 17:05:28 +020035#endif
Sukumar Ghoraic53f5e52010-09-18 20:32:33 -070036#include <asm/io.h>
37#include <asm/arch/mmc_host_def.h>
Kishon Vijay Abraham Ie7da6ac2018-01-30 16:01:40 +010038#ifdef CONFIG_OMAP54XX
39#include <asm/arch/mux_dra7xx.h>
40#include <asm/arch/dra7xx_iodelay.h>
41#endif
Roger Quadros44157de2015-09-19 16:26:53 +053042#if !defined(CONFIG_SOC_KEYSTONE)
43#include <asm/gpio.h>
Dirk Behme74140232011-05-15 09:04:47 +000044#include <asm/arch/sys_proto.h>
Roger Quadros44157de2015-09-19 16:26:53 +053045#endif
Tom Rinidf5338c2017-02-09 13:41:28 -050046#ifdef CONFIG_MMC_OMAP36XX_PINS
47#include <asm/arch/mux.h>
48#endif
Mugunthan V Nd97631a2015-09-28 12:56:30 +053049#include <dm.h>
Simon Glassd66c5f72020-02-03 07:36:15 -070050#include <dm/devres.h>
51#include <linux/err.h>
Jean-Jacques Hiblot20157d42018-01-30 16:01:44 +010052#include <power/regulator.h>
Faiz Abbase4d30562019-01-30 18:08:42 +053053#include <thermal.h>
Mugunthan V Nd97631a2015-09-28 12:56:30 +053054
55DECLARE_GLOBAL_DATA_PTR;
Sukumar Ghoraic53f5e52010-09-18 20:32:33 -070056
Pantelis Antoniouc9e75912014-02-26 19:28:45 +020057/* simplify defines to OMAP_HSMMC_USE_GPIO */
58#if (defined(CONFIG_OMAP_GPIO) && !defined(CONFIG_SPL_BUILD)) || \
59 (defined(CONFIG_SPL_BUILD) && defined(CONFIG_SPL_GPIO_SUPPORT))
60#define OMAP_HSMMC_USE_GPIO
61#else
62#undef OMAP_HSMMC_USE_GPIO
63#endif
64
Grazvydas Ignotasddde1882012-03-19 12:12:06 +000065/* common definitions for all OMAPs */
66#define SYSCTL_SRC (1 << 25)
67#define SYSCTL_SRD (1 << 26)
68
Kishon Vijay Abraham Ie7da6ac2018-01-30 16:01:40 +010069#ifdef CONFIG_IODELAY_RECALIBRATION
70struct omap_hsmmc_pinctrl_state {
71 struct pad_conf_entry *padconf;
72 int npads;
73 struct iodelay_cfg_entry *iodelay;
74 int niodelays;
75};
76#endif
77
Nikita Kiryanov13822862012-12-03 02:19:43 +000078struct omap_hsmmc_data {
79 struct hsmmc *base_addr;
Simon Glass5f4bd8c2017-07-04 13:31:19 -060080#if !CONFIG_IS_ENABLED(DM_MMC)
Pantelis Antoniou2c850462014-03-11 19:34:20 +020081 struct mmc_config cfg;
Jean-Jacques Hiblotae51a662017-03-22 16:00:33 +010082#endif
Kishon Vijay Abraham I2e18c9b2018-01-30 16:01:31 +010083 uint bus_width;
Jean-Jacques Hiblot7fe2f192018-01-30 16:01:30 +010084 uint clock;
Jean-Jacques Hiblot7a41bb42018-01-30 16:01:46 +010085 ushort last_cmd;
Pantelis Antoniouc9e75912014-02-26 19:28:45 +020086#ifdef OMAP_HSMMC_USE_GPIO
Simon Glass5f4bd8c2017-07-04 13:31:19 -060087#if CONFIG_IS_ENABLED(DM_MMC)
Mugunthan V Nd97631a2015-09-28 12:56:30 +053088 struct gpio_desc cd_gpio; /* Change Detect GPIO */
89 struct gpio_desc wp_gpio; /* Write Protect GPIO */
Mugunthan V Nd97631a2015-09-28 12:56:30 +053090#else
Nikita Kiryanov4eae05c2012-12-03 02:19:44 +000091 int cd_gpio;
Nikita Kiryanov4be9dbc2012-12-03 02:19:47 +000092 int wp_gpio;
Pantelis Antoniouc9e75912014-02-26 19:28:45 +020093#endif
Mugunthan V Nd97631a2015-09-28 12:56:30 +053094#endif
Kishon Vijay Abraham I73897ed2018-01-30 16:01:32 +010095#if CONFIG_IS_ENABLED(DM_MMC)
Jean-Jacques Hiblotcf38d4e2018-01-30 16:01:33 +010096 enum bus_mode mode;
Kishon Vijay Abraham I73897ed2018-01-30 16:01:32 +010097#endif
Kishon Vijay Abraham I826be2a2017-09-21 16:51:34 +020098 u8 controller_flags;
Jean-Jacques Hiblotcebf0592018-02-23 10:40:18 +010099#ifdef CONFIG_MMC_OMAP_HS_ADMA
Kishon Vijay Abraham I826be2a2017-09-21 16:51:34 +0200100 struct omap_hsmmc_adma_desc *adma_desc_table;
101 uint desc_slot;
102#endif
Kishon Vijay Abraham I8c2efe92018-01-30 16:01:41 +0100103 const char *hw_rev;
Jean-Jacques Hiblot7a41bb42018-01-30 16:01:46 +0100104 struct udevice *pbias_supply;
105 uint signal_voltage;
Kishon Vijay Abraham Ie7da6ac2018-01-30 16:01:40 +0100106#ifdef CONFIG_IODELAY_RECALIBRATION
107 struct omap_hsmmc_pinctrl_state *default_pinctrl_state;
108 struct omap_hsmmc_pinctrl_state *hs_pinctrl_state;
109 struct omap_hsmmc_pinctrl_state *hs200_1_8v_pinctrl_state;
110 struct omap_hsmmc_pinctrl_state *ddr_1_8v_pinctrl_state;
111 struct omap_hsmmc_pinctrl_state *sdr12_pinctrl_state;
112 struct omap_hsmmc_pinctrl_state *sdr25_pinctrl_state;
113 struct omap_hsmmc_pinctrl_state *ddr50_pinctrl_state;
114 struct omap_hsmmc_pinctrl_state *sdr50_pinctrl_state;
115 struct omap_hsmmc_pinctrl_state *sdr104_pinctrl_state;
116#endif
Kishon Vijay Abraham I826be2a2017-09-21 16:51:34 +0200117};
118
Kishon Vijay Abraham Ie7da6ac2018-01-30 16:01:40 +0100119struct omap_mmc_of_data {
120 u8 controller_flags;
121};
122
Jean-Jacques Hiblotcebf0592018-02-23 10:40:18 +0100123#ifdef CONFIG_MMC_OMAP_HS_ADMA
Kishon Vijay Abraham I826be2a2017-09-21 16:51:34 +0200124struct omap_hsmmc_adma_desc {
125 u8 attr;
126 u8 reserved;
127 u16 len;
128 u32 addr;
Nikita Kiryanov13822862012-12-03 02:19:43 +0000129};
130
Kishon Vijay Abraham I826be2a2017-09-21 16:51:34 +0200131#define ADMA_MAX_LEN 63488
132
133/* Decriptor table defines */
134#define ADMA_DESC_ATTR_VALID BIT(0)
135#define ADMA_DESC_ATTR_END BIT(1)
136#define ADMA_DESC_ATTR_INT BIT(2)
137#define ADMA_DESC_ATTR_ACT1 BIT(4)
138#define ADMA_DESC_ATTR_ACT2 BIT(5)
139
140#define ADMA_DESC_TRANSFER_DATA ADMA_DESC_ATTR_ACT2
141#define ADMA_DESC_LINK_DESC (ADMA_DESC_ATTR_ACT1 | ADMA_DESC_ATTR_ACT2)
142#endif
143
Nishanth Menond3bfaac2010-11-19 11:18:12 -0500144/* If we fail after 1 second wait, something is really bad */
145#define MAX_RETRY_MS 1000
Jean-Jacques Hiblot192e4302018-01-30 16:01:37 +0100146#define MMC_TIMEOUT_MS 20
Nishanth Menond3bfaac2010-11-19 11:18:12 -0500147
Kishon Vijay Abraham I826be2a2017-09-21 16:51:34 +0200148/* DMA transfers can take a long time if a lot a data is transferred.
149 * The timeout must take in account the amount of data. Let's assume
150 * that the time will never exceed 333 ms per MB (in other word we assume
151 * that the bandwidth is always above 3MB/s).
152 */
153#define DMA_TIMEOUT_PER_MB 333
Kishon Vijay Abraham I73897ed2018-01-30 16:01:32 +0100154#define OMAP_HSMMC_SUPPORTS_DUAL_VOLT BIT(0)
155#define OMAP_HSMMC_NO_1_8_V BIT(1)
Kishon Vijay Abraham I826be2a2017-09-21 16:51:34 +0200156#define OMAP_HSMMC_USE_ADMA BIT(2)
Kishon Vijay Abraham Ie7da6ac2018-01-30 16:01:40 +0100157#define OMAP_HSMMC_REQUIRE_IODELAY BIT(3)
Kishon Vijay Abraham I826be2a2017-09-21 16:51:34 +0200158
Sricharanf72611f2011-11-15 09:49:53 -0500159static int mmc_read_data(struct hsmmc *mmc_base, char *buf, unsigned int size);
160static int mmc_write_data(struct hsmmc *mmc_base, const char *buf,
161 unsigned int siz);
Jean-Jacques Hiblot7fe2f192018-01-30 16:01:30 +0100162static void omap_hsmmc_start_clock(struct hsmmc *mmc_base);
163static void omap_hsmmc_stop_clock(struct hsmmc *mmc_base);
Jean-Jacques Hiblotf0f821b2018-01-30 16:01:35 +0100164static void mmc_reset_controller_fsm(struct hsmmc *mmc_base, u32 bit);
Balaji T Kf843d332011-09-08 06:34:57 +0000165
Jean-Jacques Hiblotd58ef8e2017-03-22 16:00:31 +0100166static inline struct omap_hsmmc_data *omap_hsmmc_get_data(struct mmc *mmc)
167{
Simon Glass5f4bd8c2017-07-04 13:31:19 -0600168#if CONFIG_IS_ENABLED(DM_MMC)
Jean-Jacques Hiblotd58ef8e2017-03-22 16:00:31 +0100169 return dev_get_priv(mmc->dev);
170#else
171 return (struct omap_hsmmc_data *)mmc->priv;
172#endif
173}
Jean-Jacques Hiblotae51a662017-03-22 16:00:33 +0100174static inline struct mmc_config *omap_hsmmc_get_cfg(struct mmc *mmc)
175{
Simon Glass5f4bd8c2017-07-04 13:31:19 -0600176#if CONFIG_IS_ENABLED(DM_MMC)
Jean-Jacques Hiblotae51a662017-03-22 16:00:33 +0100177 struct omap_hsmmc_plat *plat = dev_get_platdata(mmc->dev);
178 return &plat->cfg;
179#else
180 return &((struct omap_hsmmc_data *)mmc->priv)->cfg;
181#endif
182}
Jean-Jacques Hiblotd58ef8e2017-03-22 16:00:31 +0100183
Simon Glass5f4bd8c2017-07-04 13:31:19 -0600184#if defined(OMAP_HSMMC_USE_GPIO) && !CONFIG_IS_ENABLED(DM_MMC)
Nikita Kiryanov4eae05c2012-12-03 02:19:44 +0000185static int omap_mmc_setup_gpio_in(int gpio, const char *label)
186{
Simon Glass1a96d7f2014-10-22 21:37:09 -0600187 int ret;
Nikita Kiryanov4eae05c2012-12-03 02:19:44 +0000188
Simon Glassfa4689a2019-12-06 21:41:35 -0700189#if !CONFIG_IS_ENABLED(DM_GPIO)
Simon Glass1a96d7f2014-10-22 21:37:09 -0600190 if (!gpio_is_valid(gpio))
Nikita Kiryanov4eae05c2012-12-03 02:19:44 +0000191 return -1;
Simon Glass1a96d7f2014-10-22 21:37:09 -0600192#endif
193 ret = gpio_request(gpio, label);
194 if (ret)
195 return ret;
Nikita Kiryanov4eae05c2012-12-03 02:19:44 +0000196
Simon Glass1a96d7f2014-10-22 21:37:09 -0600197 ret = gpio_direction_input(gpio);
198 if (ret)
199 return ret;
Nikita Kiryanov4eae05c2012-12-03 02:19:44 +0000200
201 return gpio;
202}
Nikita Kiryanov4eae05c2012-12-03 02:19:44 +0000203#endif
204
Jeroen Hofsteeaedeeaa2014-07-12 21:24:08 +0200205static unsigned char mmc_board_init(struct mmc *mmc)
Sukumar Ghoraic53f5e52010-09-18 20:32:33 -0700206{
Sukumar Ghoraic53f5e52010-09-18 20:32:33 -0700207#if defined(CONFIG_OMAP34XX)
Jean-Jacques Hiblotae51a662017-03-22 16:00:33 +0100208 struct mmc_config *cfg = omap_hsmmc_get_cfg(mmc);
Sukumar Ghoraic53f5e52010-09-18 20:32:33 -0700209 t2_t *t2_base = (t2_t *)T2_BASE;
210 struct prcm *prcm_base = (struct prcm *)PRCM_BASE;
Grazvydas Ignotasef2b7292012-03-19 03:50:53 +0000211 u32 pbias_lite;
Adam Fordef354962017-02-06 11:31:43 -0600212#ifdef CONFIG_MMC_OMAP36XX_PINS
213 u32 wkup_ctrl = readl(OMAP34XX_CTRL_WKUP_CTRL);
214#endif
Sukumar Ghoraic53f5e52010-09-18 20:32:33 -0700215
Grazvydas Ignotasef2b7292012-03-19 03:50:53 +0000216 pbias_lite = readl(&t2_base->pbias_lite);
217 pbias_lite &= ~(PBIASLITEPWRDNZ1 | PBIASLITEPWRDNZ0);
Albert ARIBAUD \(3ADEV\)6ad09812015-01-16 09:09:50 +0100218#ifdef CONFIG_TARGET_OMAP3_CAIRO
219 /* for cairo board, we need to set up 1.8 Volt bias level on MMC1 */
220 pbias_lite &= ~PBIASLITEVMODE0;
221#endif
Adam Fordf2eb4322018-09-05 04:11:08 -0500222#ifdef CONFIG_TARGET_OMAP3_LOGIC
223 /* For Logic PD board, 1.8V bias to go enable gpio127 for mmc_cd */
224 pbias_lite &= ~PBIASLITEVMODE1;
225#endif
Adam Fordef354962017-02-06 11:31:43 -0600226#ifdef CONFIG_MMC_OMAP36XX_PINS
227 if (get_cpu_family() == CPU_OMAP36XX) {
228 /* Disable extended drain IO before changing PBIAS */
229 wkup_ctrl &= ~OMAP34XX_CTRL_WKUP_CTRL_GPIO_IO_PWRDNZ;
230 writel(wkup_ctrl, OMAP34XX_CTRL_WKUP_CTRL);
231 }
232#endif
Grazvydas Ignotasef2b7292012-03-19 03:50:53 +0000233 writel(pbias_lite, &t2_base->pbias_lite);
Paul Kocialkowski69559892014-11-08 20:55:47 +0100234
Grazvydas Ignotasef2b7292012-03-19 03:50:53 +0000235 writel(pbias_lite | PBIASLITEPWRDNZ1 |
Sukumar Ghoraic53f5e52010-09-18 20:32:33 -0700236 PBIASSPEEDCTRL0 | PBIASLITEPWRDNZ0,
237 &t2_base->pbias_lite);
238
Adam Fordef354962017-02-06 11:31:43 -0600239#ifdef CONFIG_MMC_OMAP36XX_PINS
240 if (get_cpu_family() == CPU_OMAP36XX)
241 /* Enable extended drain IO after changing PBIAS */
242 writel(wkup_ctrl |
243 OMAP34XX_CTRL_WKUP_CTRL_GPIO_IO_PWRDNZ,
244 OMAP34XX_CTRL_WKUP_CTRL);
245#endif
Sukumar Ghoraic53f5e52010-09-18 20:32:33 -0700246 writel(readl(&t2_base->devconf0) | MMCSDIO1ADPCLKISEL,
247 &t2_base->devconf0);
248
249 writel(readl(&t2_base->devconf1) | MMCSDIO2ADPCLKISEL,
250 &t2_base->devconf1);
251
Jonathan Solnita9b05562012-02-24 11:30:18 +0000252 /* Change from default of 52MHz to 26MHz if necessary */
Jean-Jacques Hiblotae51a662017-03-22 16:00:33 +0100253 if (!(cfg->host_caps & MMC_MODE_HS_52MHz))
Jonathan Solnita9b05562012-02-24 11:30:18 +0000254 writel(readl(&t2_base->ctl_prog_io1) & ~CTLPROGIO1SPEEDCTRL,
255 &t2_base->ctl_prog_io1);
256
Sukumar Ghoraic53f5e52010-09-18 20:32:33 -0700257 writel(readl(&prcm_base->fclken1_core) |
258 EN_MMC1 | EN_MMC2 | EN_MMC3,
259 &prcm_base->fclken1_core);
260
261 writel(readl(&prcm_base->iclken1_core) |
262 EN_MMC1 | EN_MMC2 | EN_MMC3,
263 &prcm_base->iclken1_core);
264#endif
265
Jean-Jacques Hiblot7a41bb42018-01-30 16:01:46 +0100266#if (defined(CONFIG_OMAP54XX) || defined(CONFIG_OMAP44XX)) &&\
267 !CONFIG_IS_ENABLED(DM_REGULATOR)
Balaji T Kf843d332011-09-08 06:34:57 +0000268 /* PBIAS config needed for MMC1 only */
Jean-Jacques Hiblot26319b12017-03-22 16:00:32 +0100269 if (mmc_get_blk_desc(mmc)->devnum == 0)
Faiz Abbasfc1ad622019-04-05 14:18:46 +0530270 vmmc_pbias_config(LDO_VOLT_3V3);
Balaji T Kd9cf8362012-03-12 02:25:49 +0000271#endif
Sukumar Ghoraic53f5e52010-09-18 20:32:33 -0700272
273 return 0;
274}
275
Sricharanf72611f2011-11-15 09:49:53 -0500276void mmc_init_stream(struct hsmmc *mmc_base)
Sukumar Ghoraic53f5e52010-09-18 20:32:33 -0700277{
Nishanth Menond3bfaac2010-11-19 11:18:12 -0500278 ulong start;
Sukumar Ghoraic53f5e52010-09-18 20:32:33 -0700279
280 writel(readl(&mmc_base->con) | INIT_INITSTREAM, &mmc_base->con);
281
282 writel(MMC_CMD0, &mmc_base->cmd);
Nishanth Menond3bfaac2010-11-19 11:18:12 -0500283 start = get_timer(0);
284 while (!(readl(&mmc_base->stat) & CC_MASK)) {
285 if (get_timer(0) - start > MAX_RETRY_MS) {
286 printf("%s: timedout waiting for cc!\n", __func__);
287 return;
288 }
289 }
Sukumar Ghoraic53f5e52010-09-18 20:32:33 -0700290 writel(CC_MASK, &mmc_base->stat)
291 ;
292 writel(MMC_CMD0, &mmc_base->cmd)
293 ;
Nishanth Menond3bfaac2010-11-19 11:18:12 -0500294 start = get_timer(0);
295 while (!(readl(&mmc_base->stat) & CC_MASK)) {
296 if (get_timer(0) - start > MAX_RETRY_MS) {
297 printf("%s: timedout waiting for cc2!\n", __func__);
298 return;
299 }
300 }
Sukumar Ghoraic53f5e52010-09-18 20:32:33 -0700301 writel(readl(&mmc_base->con) & ~INIT_INITSTREAM, &mmc_base->con);
302}
Kishon Vijay Abraham I73897ed2018-01-30 16:01:32 +0100303
304#if CONFIG_IS_ENABLED(DM_MMC)
Kishon Vijay Abraham Ie7da6ac2018-01-30 16:01:40 +0100305#ifdef CONFIG_IODELAY_RECALIBRATION
306static void omap_hsmmc_io_recalibrate(struct mmc *mmc)
307{
308 struct omap_hsmmc_data *priv = omap_hsmmc_get_data(mmc);
309 struct omap_hsmmc_pinctrl_state *pinctrl_state;
310
311 switch (priv->mode) {
312 case MMC_HS_200:
313 pinctrl_state = priv->hs200_1_8v_pinctrl_state;
314 break;
315 case UHS_SDR104:
316 pinctrl_state = priv->sdr104_pinctrl_state;
317 break;
318 case UHS_SDR50:
319 pinctrl_state = priv->sdr50_pinctrl_state;
320 break;
321 case UHS_DDR50:
322 pinctrl_state = priv->ddr50_pinctrl_state;
323 break;
324 case UHS_SDR25:
325 pinctrl_state = priv->sdr25_pinctrl_state;
326 break;
327 case UHS_SDR12:
328 pinctrl_state = priv->sdr12_pinctrl_state;
329 break;
330 case SD_HS:
331 case MMC_HS:
332 case MMC_HS_52:
333 pinctrl_state = priv->hs_pinctrl_state;
334 break;
335 case MMC_DDR_52:
336 pinctrl_state = priv->ddr_1_8v_pinctrl_state;
337 default:
338 pinctrl_state = priv->default_pinctrl_state;
339 break;
340 }
341
Jean-Jacques Hiblotdae1ad42018-01-30 16:01:42 +0100342 if (!pinctrl_state)
343 pinctrl_state = priv->default_pinctrl_state;
344
Kishon Vijay Abraham Ie7da6ac2018-01-30 16:01:40 +0100345 if (priv->controller_flags & OMAP_HSMMC_REQUIRE_IODELAY) {
346 if (pinctrl_state->iodelay)
347 late_recalibrate_iodelay(pinctrl_state->padconf,
348 pinctrl_state->npads,
349 pinctrl_state->iodelay,
350 pinctrl_state->niodelays);
351 else
352 do_set_mux32((*ctrl)->control_padconf_core_base,
353 pinctrl_state->padconf,
354 pinctrl_state->npads);
355 }
356}
357#endif
Jean-Jacques Hiblotcf38d4e2018-01-30 16:01:33 +0100358static void omap_hsmmc_set_timing(struct mmc *mmc)
359{
360 u32 val;
361 struct hsmmc *mmc_base;
362 struct omap_hsmmc_data *priv = omap_hsmmc_get_data(mmc);
363
364 mmc_base = priv->base_addr;
365
Kishon Vijay Abraham Ie7da6ac2018-01-30 16:01:40 +0100366 omap_hsmmc_stop_clock(mmc_base);
Jean-Jacques Hiblotcf38d4e2018-01-30 16:01:33 +0100367 val = readl(&mmc_base->ac12);
368 val &= ~AC12_UHSMC_MASK;
369 priv->mode = mmc->selected_mode;
370
Kishon Vijay Abraham I0c1f3d02018-01-30 16:01:34 +0100371 if (mmc_is_mode_ddr(priv->mode))
372 writel(readl(&mmc_base->con) | DDR, &mmc_base->con);
373 else
374 writel(readl(&mmc_base->con) & ~DDR, &mmc_base->con);
375
Jean-Jacques Hiblotcf38d4e2018-01-30 16:01:33 +0100376 switch (priv->mode) {
377 case MMC_HS_200:
378 case UHS_SDR104:
379 val |= AC12_UHSMC_SDR104;
380 break;
381 case UHS_SDR50:
382 val |= AC12_UHSMC_SDR50;
383 break;
384 case MMC_DDR_52:
385 case UHS_DDR50:
386 val |= AC12_UHSMC_DDR50;
387 break;
388 case SD_HS:
389 case MMC_HS_52:
390 case UHS_SDR25:
391 val |= AC12_UHSMC_SDR25;
392 break;
393 case MMC_LEGACY:
394 case MMC_HS:
Jean-Jacques Hiblotcf38d4e2018-01-30 16:01:33 +0100395 case UHS_SDR12:
396 val |= AC12_UHSMC_SDR12;
397 break;
398 default:
399 val |= AC12_UHSMC_RES;
400 break;
401 }
402 writel(val, &mmc_base->ac12);
Kishon Vijay Abraham Ie7da6ac2018-01-30 16:01:40 +0100403
404#ifdef CONFIG_IODELAY_RECALIBRATION
405 omap_hsmmc_io_recalibrate(mmc);
406#endif
407 omap_hsmmc_start_clock(mmc_base);
Jean-Jacques Hiblotcf38d4e2018-01-30 16:01:33 +0100408}
409
Jean-Jacques Hiblot7a41bb42018-01-30 16:01:46 +0100410static void omap_hsmmc_conf_bus_power(struct mmc *mmc, uint signal_voltage)
Kishon Vijay Abraham I73897ed2018-01-30 16:01:32 +0100411{
412 struct hsmmc *mmc_base;
413 struct omap_hsmmc_data *priv = omap_hsmmc_get_data(mmc);
Jean-Jacques Hiblot7a41bb42018-01-30 16:01:46 +0100414 u32 hctl, ac12;
Kishon Vijay Abraham I73897ed2018-01-30 16:01:32 +0100415
416 mmc_base = priv->base_addr;
417
Jean-Jacques Hiblot7a41bb42018-01-30 16:01:46 +0100418 hctl = readl(&mmc_base->hctl) & ~SDVS_MASK;
419 ac12 = readl(&mmc_base->ac12) & ~AC12_V1V8_SIGEN;
Kishon Vijay Abraham I73897ed2018-01-30 16:01:32 +0100420
Jean-Jacques Hiblot7a41bb42018-01-30 16:01:46 +0100421 switch (signal_voltage) {
422 case MMC_SIGNAL_VOLTAGE_330:
Faiz Abbasfc1ad622019-04-05 14:18:46 +0530423 hctl |= SDVS_3V3;
Kishon Vijay Abraham I73897ed2018-01-30 16:01:32 +0100424 break;
Jean-Jacques Hiblot7a41bb42018-01-30 16:01:46 +0100425 case MMC_SIGNAL_VOLTAGE_180:
426 hctl |= SDVS_1V8;
427 ac12 |= AC12_V1V8_SIGEN;
Kishon Vijay Abraham I73897ed2018-01-30 16:01:32 +0100428 break;
429 }
430
Jean-Jacques Hiblot7a41bb42018-01-30 16:01:46 +0100431 writel(hctl, &mmc_base->hctl);
432 writel(ac12, &mmc_base->ac12);
433}
434
Sam Protsenkodb174c62019-08-14 22:52:51 +0300435static int omap_hsmmc_wait_dat0(struct udevice *dev, int state, int timeout_us)
Jean-Jacques Hiblot7a41bb42018-01-30 16:01:46 +0100436{
437 int ret = -ETIMEDOUT;
438 u32 con;
439 bool dat0_high;
440 bool target_dat0_high = !!state;
441 struct omap_hsmmc_data *priv = dev_get_priv(dev);
442 struct hsmmc *mmc_base = priv->base_addr;
443
444 con = readl(&mmc_base->con);
445 writel(con | CON_CLKEXTFREE | CON_PADEN, &mmc_base->con);
446
Sam Protsenkodb174c62019-08-14 22:52:51 +0300447 timeout_us = DIV_ROUND_UP(timeout_us, 10); /* check every 10 us. */
448 while (timeout_us--) {
Jean-Jacques Hiblot7a41bb42018-01-30 16:01:46 +0100449 dat0_high = !!(readl(&mmc_base->pstate) & PSTATE_DLEV_DAT0);
450 if (dat0_high == target_dat0_high) {
451 ret = 0;
452 break;
453 }
454 udelay(10);
455 }
456 writel(con, &mmc_base->con);
457
458 return ret;
459}
Jean-Jacques Hiblot7a41bb42018-01-30 16:01:46 +0100460
461#if CONFIG_IS_ENABLED(MMC_IO_VOLTAGE)
462#if CONFIG_IS_ENABLED(DM_REGULATOR)
463static int omap_hsmmc_set_io_regulator(struct mmc *mmc, int mV)
464{
465 int ret = 0;
466 int uV = mV * 1000;
467
468 struct omap_hsmmc_data *priv = omap_hsmmc_get_data(mmc);
469
470 if (!mmc->vqmmc_supply)
471 return 0;
472
473 /* Disable PBIAS */
Lokesh Vutlab2691972019-01-11 15:15:52 +0530474 ret = regulator_set_enable_if_allowed(priv->pbias_supply, false);
475 if (ret)
Jean-Jacques Hiblot7a41bb42018-01-30 16:01:46 +0100476 return ret;
477
478 /* Turn off IO voltage */
Lokesh Vutlab2691972019-01-11 15:15:52 +0530479 ret = regulator_set_enable_if_allowed(mmc->vqmmc_supply, false);
480 if (ret)
Jean-Jacques Hiblot7a41bb42018-01-30 16:01:46 +0100481 return ret;
482 /* Program a new IO voltage value */
483 ret = regulator_set_value(mmc->vqmmc_supply, uV);
484 if (ret)
485 return ret;
486 /* Turn on IO voltage */
Lokesh Vutlab2691972019-01-11 15:15:52 +0530487 ret = regulator_set_enable_if_allowed(mmc->vqmmc_supply, true);
488 if (ret)
Jean-Jacques Hiblot7a41bb42018-01-30 16:01:46 +0100489 return ret;
490
491 /* Program PBIAS voltage*/
492 ret = regulator_set_value(priv->pbias_supply, uV);
493 if (ret && ret != -ENOSYS)
494 return ret;
495 /* Enable PBIAS */
Lokesh Vutlab2691972019-01-11 15:15:52 +0530496 ret = regulator_set_enable_if_allowed(priv->pbias_supply, true);
497 if (ret)
Jean-Jacques Hiblot7a41bb42018-01-30 16:01:46 +0100498 return ret;
499
500 return 0;
Kishon Vijay Abraham I73897ed2018-01-30 16:01:32 +0100501}
Jean-Jacques Hiblot7a41bb42018-01-30 16:01:46 +0100502#endif
Kishon Vijay Abraham I73897ed2018-01-30 16:01:32 +0100503
Jean-Jacques Hiblot7a41bb42018-01-30 16:01:46 +0100504static int omap_hsmmc_set_signal_voltage(struct mmc *mmc)
505{
506 struct omap_hsmmc_data *priv = omap_hsmmc_get_data(mmc);
507 struct hsmmc *mmc_base = priv->base_addr;
508 int mv = mmc_voltage_to_mv(mmc->signal_voltage);
509 u32 capa_mask;
510 __maybe_unused u8 palmas_ldo_volt;
511 u32 val;
512
513 if (mv < 0)
514 return -EINVAL;
515
516 if (mmc->signal_voltage == MMC_SIGNAL_VOLTAGE_330) {
Faiz Abbasfc1ad622019-04-05 14:18:46 +0530517 mv = 3300;
518 capa_mask = VS33_3V3SUP;
519 palmas_ldo_volt = LDO_VOLT_3V3;
Jean-Jacques Hiblot7a41bb42018-01-30 16:01:46 +0100520 } else if (mmc->signal_voltage == MMC_SIGNAL_VOLTAGE_180) {
521 capa_mask = VS18_1V8SUP;
522 palmas_ldo_volt = LDO_VOLT_1V8;
523 } else {
524 return -EOPNOTSUPP;
525 }
526
527 val = readl(&mmc_base->capa);
528 if (!(val & capa_mask))
529 return -EOPNOTSUPP;
530
531 priv->signal_voltage = mmc->signal_voltage;
532
533 omap_hsmmc_conf_bus_power(mmc, mmc->signal_voltage);
534
535#if CONFIG_IS_ENABLED(DM_REGULATOR)
536 return omap_hsmmc_set_io_regulator(mmc, mv);
537#elif (defined(CONFIG_OMAP54XX) || defined(CONFIG_OMAP44XX)) && \
538 defined(CONFIG_PALMAS_POWER)
539 if (mmc_get_blk_desc(mmc)->devnum == 0)
540 vmmc_pbias_config(palmas_ldo_volt);
541 return 0;
542#else
543 return 0;
544#endif
545}
546#endif
547
548static uint32_t omap_hsmmc_set_capabilities(struct mmc *mmc)
Kishon Vijay Abraham I73897ed2018-01-30 16:01:32 +0100549{
550 struct hsmmc *mmc_base;
551 struct omap_hsmmc_data *priv = omap_hsmmc_get_data(mmc);
552 u32 val;
553
554 mmc_base = priv->base_addr;
555 val = readl(&mmc_base->capa);
556
557 if (priv->controller_flags & OMAP_HSMMC_SUPPORTS_DUAL_VOLT) {
Faiz Abbasfc1ad622019-04-05 14:18:46 +0530558 val |= (VS33_3V3SUP | VS18_1V8SUP);
Kishon Vijay Abraham I73897ed2018-01-30 16:01:32 +0100559 } else if (priv->controller_flags & OMAP_HSMMC_NO_1_8_V) {
Faiz Abbasfc1ad622019-04-05 14:18:46 +0530560 val |= VS33_3V3SUP;
Kishon Vijay Abraham I73897ed2018-01-30 16:01:32 +0100561 val &= ~VS18_1V8SUP;
Kishon Vijay Abraham I73897ed2018-01-30 16:01:32 +0100562 } else {
563 val |= VS18_1V8SUP;
Faiz Abbasfc1ad622019-04-05 14:18:46 +0530564 val &= ~VS33_3V3SUP;
Kishon Vijay Abraham I73897ed2018-01-30 16:01:32 +0100565 }
566
567 writel(val, &mmc_base->capa);
Jean-Jacques Hiblot7a41bb42018-01-30 16:01:46 +0100568
569 return val;
Kishon Vijay Abraham I73897ed2018-01-30 16:01:32 +0100570}
Jean-Jacques Hiblotf0f821b2018-01-30 16:01:35 +0100571
572#ifdef MMC_SUPPORTS_TUNING
573static void omap_hsmmc_disable_tuning(struct mmc *mmc)
574{
575 struct hsmmc *mmc_base;
576 struct omap_hsmmc_data *priv = omap_hsmmc_get_data(mmc);
577 u32 val;
578
579 mmc_base = priv->base_addr;
580 val = readl(&mmc_base->ac12);
581 val &= ~(AC12_SCLK_SEL);
582 writel(val, &mmc_base->ac12);
583
584 val = readl(&mmc_base->dll);
585 val &= ~(DLL_FORCE_VALUE | DLL_SWT);
586 writel(val, &mmc_base->dll);
587}
588
589static void omap_hsmmc_set_dll(struct mmc *mmc, int count)
590{
591 int i;
592 struct hsmmc *mmc_base;
593 struct omap_hsmmc_data *priv = omap_hsmmc_get_data(mmc);
594 u32 val;
595
596 mmc_base = priv->base_addr;
597 val = readl(&mmc_base->dll);
598 val |= DLL_FORCE_VALUE;
599 val &= ~(DLL_FORCE_SR_C_MASK << DLL_FORCE_SR_C_SHIFT);
600 val |= (count << DLL_FORCE_SR_C_SHIFT);
601 writel(val, &mmc_base->dll);
602
603 val |= DLL_CALIB;
604 writel(val, &mmc_base->dll);
605 for (i = 0; i < 1000; i++) {
606 if (readl(&mmc_base->dll) & DLL_CALIB)
607 break;
608 }
609 val &= ~DLL_CALIB;
610 writel(val, &mmc_base->dll);
611}
612
613static int omap_hsmmc_execute_tuning(struct udevice *dev, uint opcode)
614{
615 struct omap_hsmmc_data *priv = dev_get_priv(dev);
616 struct mmc_uclass_priv *upriv = dev_get_uclass_priv(dev);
617 struct mmc *mmc = upriv->mmc;
618 struct hsmmc *mmc_base;
619 u32 val;
620 u8 cur_match, prev_match = 0;
621 int ret;
622 u32 phase_delay = 0;
623 u32 start_window = 0, max_window = 0;
624 u32 length = 0, max_len = 0;
Faiz Abbase4d30562019-01-30 18:08:42 +0530625 bool single_point_failure = false;
626 struct udevice *thermal_dev;
627 int temperature;
628 int i;
Jean-Jacques Hiblotf0f821b2018-01-30 16:01:35 +0100629
630 mmc_base = priv->base_addr;
631 val = readl(&mmc_base->capa2);
632
633 /* clock tuning is not needed for upto 52MHz */
634 if (!((mmc->selected_mode == MMC_HS_200) ||
635 (mmc->selected_mode == UHS_SDR104) ||
636 ((mmc->selected_mode == UHS_SDR50) && (val & CAPA2_TSDR50))))
637 return 0;
638
Faiz Abbase4d30562019-01-30 18:08:42 +0530639 ret = uclass_first_device(UCLASS_THERMAL, &thermal_dev);
640 if (ret) {
641 printf("Couldn't get thermal device for tuning\n");
642 return ret;
643 }
644 ret = thermal_get_temp(thermal_dev, &temperature);
645 if (ret) {
646 printf("Couldn't get temperature for tuning\n");
647 return ret;
648 }
Jean-Jacques Hiblotf0f821b2018-01-30 16:01:35 +0100649 val = readl(&mmc_base->dll);
650 val |= DLL_SWT;
651 writel(val, &mmc_base->dll);
Faiz Abbase4d30562019-01-30 18:08:42 +0530652
653 /*
654 * Stage 1: Search for a maximum pass window ignoring any
655 * any single point failures. If the tuning value ends up
656 * near it, move away from it in stage 2 below
657 */
Jean-Jacques Hiblotf0f821b2018-01-30 16:01:35 +0100658 while (phase_delay <= MAX_PHASE_DELAY) {
659 omap_hsmmc_set_dll(mmc, phase_delay);
660
661 cur_match = !mmc_send_tuning(mmc, opcode, NULL);
662
663 if (cur_match) {
664 if (prev_match) {
665 length++;
Faiz Abbase4d30562019-01-30 18:08:42 +0530666 } else if (single_point_failure) {
667 /* ignore single point failure */
668 length++;
669 single_point_failure = false;
Jean-Jacques Hiblotf0f821b2018-01-30 16:01:35 +0100670 } else {
671 start_window = phase_delay;
672 length = 1;
673 }
Faiz Abbase4d30562019-01-30 18:08:42 +0530674 } else {
675 single_point_failure = prev_match;
Jean-Jacques Hiblotf0f821b2018-01-30 16:01:35 +0100676 }
677
678 if (length > max_len) {
679 max_window = start_window;
680 max_len = length;
681 }
682
683 prev_match = cur_match;
684 phase_delay += 4;
685 }
686
687 if (!max_len) {
688 ret = -EIO;
689 goto tuning_error;
690 }
691
692 val = readl(&mmc_base->ac12);
693 if (!(val & AC12_SCLK_SEL)) {
694 ret = -EIO;
695 goto tuning_error;
696 }
Faiz Abbase4d30562019-01-30 18:08:42 +0530697 /*
698 * Assign tuning value as a ratio of maximum pass window based
699 * on temperature
700 */
701 if (temperature < -20000)
702 phase_delay = min(max_window + 4 * max_len - 24,
703 max_window +
704 DIV_ROUND_UP(13 * max_len, 16) * 4);
705 else if (temperature < 20000)
706 phase_delay = max_window + DIV_ROUND_UP(9 * max_len, 16) * 4;
707 else if (temperature < 40000)
708 phase_delay = max_window + DIV_ROUND_UP(8 * max_len, 16) * 4;
709 else if (temperature < 70000)
710 phase_delay = max_window + DIV_ROUND_UP(7 * max_len, 16) * 4;
711 else if (temperature < 90000)
712 phase_delay = max_window + DIV_ROUND_UP(5 * max_len, 16) * 4;
713 else if (temperature < 120000)
714 phase_delay = max_window + DIV_ROUND_UP(4 * max_len, 16) * 4;
715 else
716 phase_delay = max_window + DIV_ROUND_UP(3 * max_len, 16) * 4;
717
718 /*
719 * Stage 2: Search for a single point failure near the chosen tuning
720 * value in two steps. First in the +3 to +10 range and then in the
721 * +2 to -10 range. If found, move away from it in the appropriate
722 * direction by the appropriate amount depending on the temperature.
723 */
724 for (i = 3; i <= 10; i++) {
725 omap_hsmmc_set_dll(mmc, phase_delay + i);
726 if (mmc_send_tuning(mmc, opcode, NULL)) {
727 if (temperature < 10000)
728 phase_delay += i + 6;
729 else if (temperature < 20000)
730 phase_delay += i - 12;
731 else if (temperature < 70000)
732 phase_delay += i - 8;
733 else if (temperature < 90000)
734 phase_delay += i - 6;
735 else
736 phase_delay += i - 6;
737
738 goto single_failure_found;
739 }
740 }
741
742 for (i = 2; i >= -10; i--) {
743 omap_hsmmc_set_dll(mmc, phase_delay + i);
744 if (mmc_send_tuning(mmc, opcode, NULL)) {
745 if (temperature < 10000)
746 phase_delay += i + 12;
747 else if (temperature < 20000)
748 phase_delay += i + 8;
749 else if (temperature < 70000)
750 phase_delay += i + 8;
751 else if (temperature < 90000)
752 phase_delay += i + 10;
753 else
754 phase_delay += i + 12;
755
756 goto single_failure_found;
757 }
758 }
759
760single_failure_found:
Jean-Jacques Hiblotf0f821b2018-01-30 16:01:35 +0100761
Jean-Jacques Hiblotf0f821b2018-01-30 16:01:35 +0100762 omap_hsmmc_set_dll(mmc, phase_delay);
763
764 mmc_reset_controller_fsm(mmc_base, SYSCTL_SRD);
765 mmc_reset_controller_fsm(mmc_base, SYSCTL_SRC);
766
767 return 0;
768
769tuning_error:
770
771 omap_hsmmc_disable_tuning(mmc);
772 mmc_reset_controller_fsm(mmc_base, SYSCTL_SRD);
773 mmc_reset_controller_fsm(mmc_base, SYSCTL_SRC);
774
775 return ret;
776}
777#endif
Kishon Vijay Abraham I73897ed2018-01-30 16:01:32 +0100778#endif
Sukumar Ghoraic53f5e52010-09-18 20:32:33 -0700779
Jean-Jacques Hiblota420d7d2018-01-30 16:01:36 +0100780static void mmc_enable_irq(struct mmc *mmc, struct mmc_cmd *cmd)
781{
782 struct omap_hsmmc_data *priv = omap_hsmmc_get_data(mmc);
783 struct hsmmc *mmc_base = priv->base_addr;
784 u32 irq_mask = INT_EN_MASK;
785
786 /*
787 * TODO: Errata i802 indicates only DCRC interrupts can occur during
788 * tuning procedure and DCRC should be disabled. But see occurences
789 * of DEB, CIE, CEB, CCRC interupts during tuning procedure. These
790 * interrupts occur along with BRR, so the data is actually in the
791 * buffer. It has to be debugged why these interrutps occur
792 */
793 if (cmd && mmc_is_tuning_cmd(cmd->cmdidx))
794 irq_mask &= ~(IE_DEB | IE_DCRC | IE_CIE | IE_CEB | IE_CCRC);
795
796 writel(irq_mask, &mmc_base->ie);
797}
798
Pantelis Antoniouc9e75912014-02-26 19:28:45 +0200799static int omap_hsmmc_init_setup(struct mmc *mmc)
Sukumar Ghoraic53f5e52010-09-18 20:32:33 -0700800{
Jean-Jacques Hiblotd58ef8e2017-03-22 16:00:31 +0100801 struct omap_hsmmc_data *priv = omap_hsmmc_get_data(mmc);
Nikita Kiryanov13822862012-12-03 02:19:43 +0000802 struct hsmmc *mmc_base;
Sukumar Ghoraic53f5e52010-09-18 20:32:33 -0700803 unsigned int reg_val;
804 unsigned int dsor;
Nishanth Menond3bfaac2010-11-19 11:18:12 -0500805 ulong start;
Sukumar Ghoraic53f5e52010-09-18 20:32:33 -0700806
Jean-Jacques Hiblotd58ef8e2017-03-22 16:00:31 +0100807 mmc_base = priv->base_addr;
Balaji T Kf843d332011-09-08 06:34:57 +0000808 mmc_board_init(mmc);
Sukumar Ghoraic53f5e52010-09-18 20:32:33 -0700809
810 writel(readl(&mmc_base->sysconfig) | MMC_SOFTRESET,
811 &mmc_base->sysconfig);
Nishanth Menond3bfaac2010-11-19 11:18:12 -0500812 start = get_timer(0);
813 while ((readl(&mmc_base->sysstatus) & RESETDONE) == 0) {
814 if (get_timer(0) - start > MAX_RETRY_MS) {
815 printf("%s: timedout waiting for cc2!\n", __func__);
Jaehoon Chung7825d202016-07-19 16:33:36 +0900816 return -ETIMEDOUT;
Nishanth Menond3bfaac2010-11-19 11:18:12 -0500817 }
818 }
Sukumar Ghoraic53f5e52010-09-18 20:32:33 -0700819 writel(readl(&mmc_base->sysctl) | SOFTRESETALL, &mmc_base->sysctl);
Nishanth Menond3bfaac2010-11-19 11:18:12 -0500820 start = get_timer(0);
821 while ((readl(&mmc_base->sysctl) & SOFTRESETALL) != 0x0) {
822 if (get_timer(0) - start > MAX_RETRY_MS) {
823 printf("%s: timedout waiting for softresetall!\n",
824 __func__);
Jaehoon Chung7825d202016-07-19 16:33:36 +0900825 return -ETIMEDOUT;
Nishanth Menond3bfaac2010-11-19 11:18:12 -0500826 }
827 }
Jean-Jacques Hiblotcebf0592018-02-23 10:40:18 +0100828#ifdef CONFIG_MMC_OMAP_HS_ADMA
Kishon Vijay Abraham I826be2a2017-09-21 16:51:34 +0200829 reg_val = readl(&mmc_base->hl_hwinfo);
830 if (reg_val & MADMA_EN)
831 priv->controller_flags |= OMAP_HSMMC_USE_ADMA;
832#endif
Kishon Vijay Abraham I73897ed2018-01-30 16:01:32 +0100833
834#if CONFIG_IS_ENABLED(DM_MMC)
Jean-Jacques Hiblot7a41bb42018-01-30 16:01:46 +0100835 reg_val = omap_hsmmc_set_capabilities(mmc);
Faiz Abbasfc1ad622019-04-05 14:18:46 +0530836 omap_hsmmc_conf_bus_power(mmc, (reg_val & VS33_3V3SUP) ?
Jean-Jacques Hiblot7a41bb42018-01-30 16:01:46 +0100837 MMC_SIGNAL_VOLTAGE_330 : MMC_SIGNAL_VOLTAGE_180);
Kishon Vijay Abraham I73897ed2018-01-30 16:01:32 +0100838#else
Sukumar Ghoraic53f5e52010-09-18 20:32:33 -0700839 writel(DTW_1_BITMODE | SDBP_PWROFF | SDVS_3V0, &mmc_base->hctl);
Faiz Abbasfc1ad622019-04-05 14:18:46 +0530840 writel(readl(&mmc_base->capa) | VS33_3V3SUP | VS18_1V8SUP,
Sukumar Ghoraic53f5e52010-09-18 20:32:33 -0700841 &mmc_base->capa);
Kishon Vijay Abraham I73897ed2018-01-30 16:01:32 +0100842#endif
Sukumar Ghoraic53f5e52010-09-18 20:32:33 -0700843
844 reg_val = readl(&mmc_base->con) & RESERVED_MASK;
845
846 writel(CTPL_MMC_SD | reg_val | WPP_ACTIVEHIGH | CDP_ACTIVEHIGH |
847 MIT_CTO | DW8_1_4BITMODE | MODE_FUNC | STR_BLOCK |
848 HR_NOHOSTRESP | INIT_NOINIT | NOOPENDRAIN, &mmc_base->con);
849
850 dsor = 240;
851 mmc_reg_out(&mmc_base->sysctl, (ICE_MASK | DTO_MASK | CEN_MASK),
Kishon Vijay Abraham I6e543812017-09-21 16:51:36 +0200852 (ICE_STOP | DTO_15THDTO));
Sukumar Ghoraic53f5e52010-09-18 20:32:33 -0700853 mmc_reg_out(&mmc_base->sysctl, ICE_MASK | CLKD_MASK,
854 (dsor << CLKD_OFFSET) | ICE_OSCILLATE);
Nishanth Menond3bfaac2010-11-19 11:18:12 -0500855 start = get_timer(0);
856 while ((readl(&mmc_base->sysctl) & ICS_MASK) == ICS_NOTREADY) {
857 if (get_timer(0) - start > MAX_RETRY_MS) {
858 printf("%s: timedout waiting for ics!\n", __func__);
Jaehoon Chung7825d202016-07-19 16:33:36 +0900859 return -ETIMEDOUT;
Nishanth Menond3bfaac2010-11-19 11:18:12 -0500860 }
861 }
Sukumar Ghoraic53f5e52010-09-18 20:32:33 -0700862 writel(readl(&mmc_base->sysctl) | CEN_ENABLE, &mmc_base->sysctl);
863
864 writel(readl(&mmc_base->hctl) | SDBP_PWRON, &mmc_base->hctl);
865
Jean-Jacques Hiblota420d7d2018-01-30 16:01:36 +0100866 mmc_enable_irq(mmc, NULL);
Jean-Jacques Hiblot20157d42018-01-30 16:01:44 +0100867
868#if !CONFIG_IS_ENABLED(DM_MMC)
Sukumar Ghoraic53f5e52010-09-18 20:32:33 -0700869 mmc_init_stream(mmc_base);
Jean-Jacques Hiblot20157d42018-01-30 16:01:44 +0100870#endif
Sukumar Ghoraic53f5e52010-09-18 20:32:33 -0700871
872 return 0;
873}
874
Grazvydas Ignotasddde1882012-03-19 12:12:06 +0000875/*
876 * MMC controller internal finite state machine reset
877 *
878 * Used to reset command or data internal state machines, using respectively
879 * SRC or SRD bit of SYSCTL register
880 */
881static void mmc_reset_controller_fsm(struct hsmmc *mmc_base, u32 bit)
882{
883 ulong start;
884
885 mmc_reg_out(&mmc_base->sysctl, bit, bit);
886
Oleksandr Tyshchenko06640ca2013-08-06 13:44:16 +0300887 /*
888 * CMD(DAT) lines reset procedures are slightly different
889 * for OMAP3 and OMAP4(AM335x,OMAP5,DRA7xx).
890 * According to OMAP3 TRM:
891 * Set SRC(SRD) bit in MMCHS_SYSCTL register to 0x1 and wait until it
892 * returns to 0x0.
893 * According to OMAP4(AM335x,OMAP5,DRA7xx) TRMs, CMD(DATA) lines reset
894 * procedure steps must be as follows:
895 * 1. Initiate CMD(DAT) line reset by writing 0x1 to SRC(SRD) bit in
896 * MMCHS_SYSCTL register (SD_SYSCTL for AM335x).
897 * 2. Poll the SRC(SRD) bit until it is set to 0x1.
898 * 3. Wait until the SRC (SRD) bit returns to 0x0
899 * (reset procedure is completed).
900 */
901#if defined(CONFIG_OMAP44XX) || defined(CONFIG_OMAP54XX) || \
Nikita Kiryanov5ffdd852015-07-30 23:56:20 +0300902 defined(CONFIG_AM33XX) || defined(CONFIG_AM43XX)
Oleksandr Tyshchenko06640ca2013-08-06 13:44:16 +0300903 if (!(readl(&mmc_base->sysctl) & bit)) {
904 start = get_timer(0);
905 while (!(readl(&mmc_base->sysctl) & bit)) {
Jean-Jacques Hiblot192e4302018-01-30 16:01:37 +0100906 if (get_timer(0) - start > MMC_TIMEOUT_MS)
Oleksandr Tyshchenko06640ca2013-08-06 13:44:16 +0300907 return;
908 }
909 }
910#endif
Grazvydas Ignotasddde1882012-03-19 12:12:06 +0000911 start = get_timer(0);
912 while ((readl(&mmc_base->sysctl) & bit) != 0) {
913 if (get_timer(0) - start > MAX_RETRY_MS) {
914 printf("%s: timedout waiting for sysctl %x to clear\n",
915 __func__, bit);
916 return;
917 }
918 }
919}
Kishon Vijay Abraham I826be2a2017-09-21 16:51:34 +0200920
Jean-Jacques Hiblotcebf0592018-02-23 10:40:18 +0100921#ifdef CONFIG_MMC_OMAP_HS_ADMA
Kishon Vijay Abraham I826be2a2017-09-21 16:51:34 +0200922static void omap_hsmmc_adma_desc(struct mmc *mmc, char *buf, u16 len, bool end)
923{
924 struct omap_hsmmc_data *priv = omap_hsmmc_get_data(mmc);
925 struct omap_hsmmc_adma_desc *desc;
926 u8 attr;
927
928 desc = &priv->adma_desc_table[priv->desc_slot];
929
930 attr = ADMA_DESC_ATTR_VALID | ADMA_DESC_TRANSFER_DATA;
931 if (!end)
932 priv->desc_slot++;
933 else
934 attr |= ADMA_DESC_ATTR_END;
935
936 desc->len = len;
937 desc->addr = (u32)buf;
938 desc->reserved = 0;
939 desc->attr = attr;
940}
941
942static void omap_hsmmc_prepare_adma_table(struct mmc *mmc,
943 struct mmc_data *data)
944{
945 uint total_len = data->blocksize * data->blocks;
946 uint desc_count = DIV_ROUND_UP(total_len, ADMA_MAX_LEN);
947 struct omap_hsmmc_data *priv = omap_hsmmc_get_data(mmc);
948 int i = desc_count;
949 char *buf;
950
951 priv->desc_slot = 0;
952 priv->adma_desc_table = (struct omap_hsmmc_adma_desc *)
953 memalign(ARCH_DMA_MINALIGN, desc_count *
954 sizeof(struct omap_hsmmc_adma_desc));
955
956 if (data->flags & MMC_DATA_READ)
957 buf = data->dest;
958 else
959 buf = (char *)data->src;
960
961 while (--i) {
962 omap_hsmmc_adma_desc(mmc, buf, ADMA_MAX_LEN, false);
963 buf += ADMA_MAX_LEN;
964 total_len -= ADMA_MAX_LEN;
965 }
966
967 omap_hsmmc_adma_desc(mmc, buf, total_len, true);
968
969 flush_dcache_range((long)priv->adma_desc_table,
970 (long)priv->adma_desc_table +
971 ROUND(desc_count *
972 sizeof(struct omap_hsmmc_adma_desc),
973 ARCH_DMA_MINALIGN));
974}
975
976static void omap_hsmmc_prepare_data(struct mmc *mmc, struct mmc_data *data)
977{
978 struct hsmmc *mmc_base;
979 struct omap_hsmmc_data *priv = omap_hsmmc_get_data(mmc);
980 u32 val;
981 char *buf;
982
983 mmc_base = priv->base_addr;
984 omap_hsmmc_prepare_adma_table(mmc, data);
985
986 if (data->flags & MMC_DATA_READ)
987 buf = data->dest;
988 else
989 buf = (char *)data->src;
990
991 val = readl(&mmc_base->hctl);
992 val |= DMA_SELECT;
993 writel(val, &mmc_base->hctl);
994
995 val = readl(&mmc_base->con);
996 val |= DMA_MASTER;
997 writel(val, &mmc_base->con);
998
999 writel((u32)priv->adma_desc_table, &mmc_base->admasal);
1000
1001 flush_dcache_range((u32)buf,
1002 (u32)buf +
1003 ROUND(data->blocksize * data->blocks,
1004 ARCH_DMA_MINALIGN));
1005}
1006
1007static void omap_hsmmc_dma_cleanup(struct mmc *mmc)
1008{
1009 struct hsmmc *mmc_base;
1010 struct omap_hsmmc_data *priv = omap_hsmmc_get_data(mmc);
1011 u32 val;
1012
1013 mmc_base = priv->base_addr;
1014
1015 val = readl(&mmc_base->con);
1016 val &= ~DMA_MASTER;
1017 writel(val, &mmc_base->con);
1018
1019 val = readl(&mmc_base->hctl);
1020 val &= ~DMA_SELECT;
1021 writel(val, &mmc_base->hctl);
1022
1023 kfree(priv->adma_desc_table);
1024}
1025#else
1026#define omap_hsmmc_adma_desc
1027#define omap_hsmmc_prepare_adma_table
1028#define omap_hsmmc_prepare_data
1029#define omap_hsmmc_dma_cleanup
1030#endif
1031
Simon Glass5f4bd8c2017-07-04 13:31:19 -06001032#if !CONFIG_IS_ENABLED(DM_MMC)
Pantelis Antoniouc9e75912014-02-26 19:28:45 +02001033static int omap_hsmmc_send_cmd(struct mmc *mmc, struct mmc_cmd *cmd,
Sukumar Ghoraic53f5e52010-09-18 20:32:33 -07001034 struct mmc_data *data)
1035{
Jean-Jacques Hiblotd58ef8e2017-03-22 16:00:31 +01001036 struct omap_hsmmc_data *priv = omap_hsmmc_get_data(mmc);
Jean-Jacques Hiblot8fc9d3a2017-04-14 19:50:02 +02001037#else
1038static int omap_hsmmc_send_cmd(struct udevice *dev, struct mmc_cmd *cmd,
1039 struct mmc_data *data)
1040{
1041 struct omap_hsmmc_data *priv = dev_get_priv(dev);
Kishon Vijay Abraham I826be2a2017-09-21 16:51:34 +02001042 struct mmc_uclass_priv *upriv = dev_get_uclass_priv(dev);
1043 struct mmc *mmc = upriv->mmc;
1044#endif
Nikita Kiryanov13822862012-12-03 02:19:43 +00001045 struct hsmmc *mmc_base;
Sukumar Ghoraic53f5e52010-09-18 20:32:33 -07001046 unsigned int flags, mmc_stat;
Nishanth Menond3bfaac2010-11-19 11:18:12 -05001047 ulong start;
Jean-Jacques Hiblot7a41bb42018-01-30 16:01:46 +01001048 priv->last_cmd = cmd->cmdidx;
Sukumar Ghoraic53f5e52010-09-18 20:32:33 -07001049
Jean-Jacques Hiblotd58ef8e2017-03-22 16:00:31 +01001050 mmc_base = priv->base_addr;
Kishon Vijay Abraham I316e7ae2017-09-21 16:51:35 +02001051
1052 if (cmd->cmdidx == MMC_CMD_STOP_TRANSMISSION)
1053 return 0;
1054
Nishanth Menond3bfaac2010-11-19 11:18:12 -05001055 start = get_timer(0);
Tom Rini32ec3252012-01-30 11:22:25 +00001056 while ((readl(&mmc_base->pstate) & (DATI_MASK | CMDI_MASK)) != 0) {
Nishanth Menond3bfaac2010-11-19 11:18:12 -05001057 if (get_timer(0) - start > MAX_RETRY_MS) {
Tom Rini32ec3252012-01-30 11:22:25 +00001058 printf("%s: timedout waiting on cmd inhibit to clear\n",
1059 __func__);
Jean-Jacques Hiblota1e7a4d2019-07-02 10:53:48 +02001060 mmc_reset_controller_fsm(mmc_base, SYSCTL_SRD);
1061 mmc_reset_controller_fsm(mmc_base, SYSCTL_SRC);
Jaehoon Chung7825d202016-07-19 16:33:36 +09001062 return -ETIMEDOUT;
Nishanth Menond3bfaac2010-11-19 11:18:12 -05001063 }
1064 }
Sukumar Ghoraic53f5e52010-09-18 20:32:33 -07001065 writel(0xFFFFFFFF, &mmc_base->stat);
Jean-Jacques Hiblota1e7a4d2019-07-02 10:53:48 +02001066 if (readl(&mmc_base->stat)) {
1067 mmc_reset_controller_fsm(mmc_base, SYSCTL_SRD);
1068 mmc_reset_controller_fsm(mmc_base, SYSCTL_SRC);
Nishanth Menond3bfaac2010-11-19 11:18:12 -05001069 }
Jean-Jacques Hiblota1e7a4d2019-07-02 10:53:48 +02001070
Sukumar Ghoraic53f5e52010-09-18 20:32:33 -07001071 /*
1072 * CMDREG
1073 * CMDIDX[13:8] : Command index
1074 * DATAPRNT[5] : Data Present Select
1075 * ENCMDIDX[4] : Command Index Check Enable
1076 * ENCMDCRC[3] : Command CRC Check Enable
1077 * RSPTYP[1:0]
1078 * 00 = No Response
1079 * 01 = Length 136
1080 * 10 = Length 48
1081 * 11 = Length 48 Check busy after response
1082 */
1083 /* Delay added before checking the status of frq change
1084 * retry not supported by mmc.c(core file)
1085 */
1086 if (cmd->cmdidx == SD_CMD_APP_SEND_SCR)
1087 udelay(50000); /* wait 50 ms */
1088
1089 if (!(cmd->resp_type & MMC_RSP_PRESENT))
1090 flags = 0;
1091 else if (cmd->resp_type & MMC_RSP_136)
1092 flags = RSP_TYPE_LGHT136 | CICE_NOCHECK;
1093 else if (cmd->resp_type & MMC_RSP_BUSY)
1094 flags = RSP_TYPE_LGHT48B;
1095 else
1096 flags = RSP_TYPE_LGHT48;
1097
1098 /* enable default flags */
1099 flags = flags | (CMD_TYPE_NORMAL | CICE_NOCHECK | CCCE_NOCHECK |
Kishon Vijay Abraham I6e543812017-09-21 16:51:36 +02001100 MSBS_SGLEBLK);
1101 flags &= ~(ACEN_ENABLE | BCE_ENABLE | DE_ENABLE);
Sukumar Ghoraic53f5e52010-09-18 20:32:33 -07001102
1103 if (cmd->resp_type & MMC_RSP_CRC)
1104 flags |= CCCE_CHECK;
1105 if (cmd->resp_type & MMC_RSP_OPCODE)
1106 flags |= CICE_CHECK;
1107
1108 if (data) {
1109 if ((cmd->cmdidx == MMC_CMD_READ_MULTIPLE_BLOCK) ||
1110 (cmd->cmdidx == MMC_CMD_WRITE_MULTIPLE_BLOCK)) {
Kishon Vijay Abraham I316e7ae2017-09-21 16:51:35 +02001111 flags |= (MSBS_MULTIBLK | BCE_ENABLE | ACEN_ENABLE);
Sukumar Ghoraic53f5e52010-09-18 20:32:33 -07001112 data->blocksize = 512;
1113 writel(data->blocksize | (data->blocks << 16),
1114 &mmc_base->blk);
1115 } else
1116 writel(data->blocksize | NBLK_STPCNT, &mmc_base->blk);
1117
1118 if (data->flags & MMC_DATA_READ)
1119 flags |= (DP_DATA | DDIR_READ);
1120 else
1121 flags |= (DP_DATA | DDIR_WRITE);
Kishon Vijay Abraham I826be2a2017-09-21 16:51:34 +02001122
Jean-Jacques Hiblotcebf0592018-02-23 10:40:18 +01001123#ifdef CONFIG_MMC_OMAP_HS_ADMA
Kishon Vijay Abraham I826be2a2017-09-21 16:51:34 +02001124 if ((priv->controller_flags & OMAP_HSMMC_USE_ADMA) &&
1125 !mmc_is_tuning_cmd(cmd->cmdidx)) {
1126 omap_hsmmc_prepare_data(mmc, data);
1127 flags |= DE_ENABLE;
1128 }
1129#endif
Sukumar Ghoraic53f5e52010-09-18 20:32:33 -07001130 }
1131
Jean-Jacques Hiblota420d7d2018-01-30 16:01:36 +01001132 mmc_enable_irq(mmc, cmd);
1133
Sukumar Ghoraic53f5e52010-09-18 20:32:33 -07001134 writel(cmd->cmdarg, &mmc_base->arg);
Lubomir Popov19df4122013-08-14 18:59:18 +03001135 udelay(20); /* To fix "No status update" error on eMMC */
Sukumar Ghoraic53f5e52010-09-18 20:32:33 -07001136 writel((cmd->cmdidx << 24) | flags, &mmc_base->cmd);
1137
Nishanth Menond3bfaac2010-11-19 11:18:12 -05001138 start = get_timer(0);
Sukumar Ghoraic53f5e52010-09-18 20:32:33 -07001139 do {
1140 mmc_stat = readl(&mmc_base->stat);
Kishon Vijay Abraham I826be2a2017-09-21 16:51:34 +02001141 if (get_timer(start) > MAX_RETRY_MS) {
Nishanth Menond3bfaac2010-11-19 11:18:12 -05001142 printf("%s : timeout: No status update\n", __func__);
Jaehoon Chung7825d202016-07-19 16:33:36 +09001143 return -ETIMEDOUT;
Nishanth Menond3bfaac2010-11-19 11:18:12 -05001144 }
1145 } while (!mmc_stat);
Sukumar Ghoraic53f5e52010-09-18 20:32:33 -07001146
Grazvydas Ignotasddde1882012-03-19 12:12:06 +00001147 if ((mmc_stat & IE_CTO) != 0) {
1148 mmc_reset_controller_fsm(mmc_base, SYSCTL_SRC);
Jaehoon Chung7825d202016-07-19 16:33:36 +09001149 return -ETIMEDOUT;
Grazvydas Ignotasddde1882012-03-19 12:12:06 +00001150 } else if ((mmc_stat & ERRI_MASK) != 0)
Sukumar Ghoraic53f5e52010-09-18 20:32:33 -07001151 return -1;
1152
1153 if (mmc_stat & CC_MASK) {
1154 writel(CC_MASK, &mmc_base->stat);
1155 if (cmd->resp_type & MMC_RSP_PRESENT) {
1156 if (cmd->resp_type & MMC_RSP_136) {
1157 /* response type 2 */
1158 cmd->response[3] = readl(&mmc_base->rsp10);
1159 cmd->response[2] = readl(&mmc_base->rsp32);
1160 cmd->response[1] = readl(&mmc_base->rsp54);
1161 cmd->response[0] = readl(&mmc_base->rsp76);
1162 } else
1163 /* response types 1, 1b, 3, 4, 5, 6 */
1164 cmd->response[0] = readl(&mmc_base->rsp10);
1165 }
1166 }
1167
Jean-Jacques Hiblotcebf0592018-02-23 10:40:18 +01001168#ifdef CONFIG_MMC_OMAP_HS_ADMA
Kishon Vijay Abraham I826be2a2017-09-21 16:51:34 +02001169 if ((priv->controller_flags & OMAP_HSMMC_USE_ADMA) && data &&
1170 !mmc_is_tuning_cmd(cmd->cmdidx)) {
1171 u32 sz_mb, timeout;
1172
1173 if (mmc_stat & IE_ADMAE) {
1174 omap_hsmmc_dma_cleanup(mmc);
1175 return -EIO;
1176 }
1177
1178 sz_mb = DIV_ROUND_UP(data->blocksize * data->blocks, 1 << 20);
1179 timeout = sz_mb * DMA_TIMEOUT_PER_MB;
1180 if (timeout < MAX_RETRY_MS)
1181 timeout = MAX_RETRY_MS;
1182
1183 start = get_timer(0);
1184 do {
1185 mmc_stat = readl(&mmc_base->stat);
1186 if (mmc_stat & TC_MASK) {
1187 writel(readl(&mmc_base->stat) | TC_MASK,
1188 &mmc_base->stat);
1189 break;
1190 }
1191 if (get_timer(start) > timeout) {
1192 printf("%s : DMA timeout: No status update\n",
1193 __func__);
1194 return -ETIMEDOUT;
1195 }
1196 } while (1);
1197
1198 omap_hsmmc_dma_cleanup(mmc);
1199 return 0;
1200 }
1201#endif
1202
Sukumar Ghoraic53f5e52010-09-18 20:32:33 -07001203 if (data && (data->flags & MMC_DATA_READ)) {
1204 mmc_read_data(mmc_base, data->dest,
1205 data->blocksize * data->blocks);
1206 } else if (data && (data->flags & MMC_DATA_WRITE)) {
1207 mmc_write_data(mmc_base, data->src,
1208 data->blocksize * data->blocks);
1209 }
1210 return 0;
1211}
1212
Sricharanf72611f2011-11-15 09:49:53 -05001213static int mmc_read_data(struct hsmmc *mmc_base, char *buf, unsigned int size)
Sukumar Ghoraic53f5e52010-09-18 20:32:33 -07001214{
1215 unsigned int *output_buf = (unsigned int *)buf;
1216 unsigned int mmc_stat;
1217 unsigned int count;
1218
1219 /*
1220 * Start Polled Read
1221 */
1222 count = (size > MMCSD_SECTOR_SIZE) ? MMCSD_SECTOR_SIZE : size;
1223 count /= 4;
1224
1225 while (size) {
Nishanth Menond3bfaac2010-11-19 11:18:12 -05001226 ulong start = get_timer(0);
Sukumar Ghoraic53f5e52010-09-18 20:32:33 -07001227 do {
1228 mmc_stat = readl(&mmc_base->stat);
Nishanth Menond3bfaac2010-11-19 11:18:12 -05001229 if (get_timer(0) - start > MAX_RETRY_MS) {
1230 printf("%s: timedout waiting for status!\n",
1231 __func__);
Jaehoon Chung7825d202016-07-19 16:33:36 +09001232 return -ETIMEDOUT;
Nishanth Menond3bfaac2010-11-19 11:18:12 -05001233 }
Sukumar Ghoraic53f5e52010-09-18 20:32:33 -07001234 } while (mmc_stat == 0);
1235
Grazvydas Ignotasddde1882012-03-19 12:12:06 +00001236 if ((mmc_stat & (IE_DTO | IE_DCRC | IE_DEB)) != 0)
1237 mmc_reset_controller_fsm(mmc_base, SYSCTL_SRD);
1238
Sukumar Ghoraic53f5e52010-09-18 20:32:33 -07001239 if ((mmc_stat & ERRI_MASK) != 0)
1240 return 1;
1241
1242 if (mmc_stat & BRR_MASK) {
1243 unsigned int k;
1244
1245 writel(readl(&mmc_base->stat) | BRR_MASK,
1246 &mmc_base->stat);
1247 for (k = 0; k < count; k++) {
1248 *output_buf = readl(&mmc_base->data);
1249 output_buf++;
1250 }
1251 size -= (count*4);
1252 }
1253
1254 if (mmc_stat & BWR_MASK)
1255 writel(readl(&mmc_base->stat) | BWR_MASK,
1256 &mmc_base->stat);
1257
1258 if (mmc_stat & TC_MASK) {
1259 writel(readl(&mmc_base->stat) | TC_MASK,
1260 &mmc_base->stat);
1261 break;
1262 }
1263 }
1264 return 0;
1265}
1266
Jean-Jacques Hiblot98821552018-02-23 10:40:17 +01001267#if CONFIG_IS_ENABLED(MMC_WRITE)
Sricharanf72611f2011-11-15 09:49:53 -05001268static int mmc_write_data(struct hsmmc *mmc_base, const char *buf,
Jean-Jacques Hiblot98821552018-02-23 10:40:17 +01001269 unsigned int size)
Sukumar Ghoraic53f5e52010-09-18 20:32:33 -07001270{
1271 unsigned int *input_buf = (unsigned int *)buf;
1272 unsigned int mmc_stat;
1273 unsigned int count;
1274
1275 /*
Lubomir Popov19df4122013-08-14 18:59:18 +03001276 * Start Polled Write
Sukumar Ghoraic53f5e52010-09-18 20:32:33 -07001277 */
1278 count = (size > MMCSD_SECTOR_SIZE) ? MMCSD_SECTOR_SIZE : size;
1279 count /= 4;
1280
1281 while (size) {
Nishanth Menond3bfaac2010-11-19 11:18:12 -05001282 ulong start = get_timer(0);
Sukumar Ghoraic53f5e52010-09-18 20:32:33 -07001283 do {
1284 mmc_stat = readl(&mmc_base->stat);
Nishanth Menond3bfaac2010-11-19 11:18:12 -05001285 if (get_timer(0) - start > MAX_RETRY_MS) {
1286 printf("%s: timedout waiting for status!\n",
1287 __func__);
Jaehoon Chung7825d202016-07-19 16:33:36 +09001288 return -ETIMEDOUT;
Nishanth Menond3bfaac2010-11-19 11:18:12 -05001289 }
Sukumar Ghoraic53f5e52010-09-18 20:32:33 -07001290 } while (mmc_stat == 0);
1291
Grazvydas Ignotasddde1882012-03-19 12:12:06 +00001292 if ((mmc_stat & (IE_DTO | IE_DCRC | IE_DEB)) != 0)
1293 mmc_reset_controller_fsm(mmc_base, SYSCTL_SRD);
1294
Sukumar Ghoraic53f5e52010-09-18 20:32:33 -07001295 if ((mmc_stat & ERRI_MASK) != 0)
1296 return 1;
1297
1298 if (mmc_stat & BWR_MASK) {
1299 unsigned int k;
1300
1301 writel(readl(&mmc_base->stat) | BWR_MASK,
1302 &mmc_base->stat);
1303 for (k = 0; k < count; k++) {
1304 writel(*input_buf, &mmc_base->data);
1305 input_buf++;
1306 }
1307 size -= (count*4);
1308 }
1309
1310 if (mmc_stat & BRR_MASK)
1311 writel(readl(&mmc_base->stat) | BRR_MASK,
1312 &mmc_base->stat);
1313
1314 if (mmc_stat & TC_MASK) {
1315 writel(readl(&mmc_base->stat) | TC_MASK,
1316 &mmc_base->stat);
1317 break;
1318 }
1319 }
1320 return 0;
1321}
Jean-Jacques Hiblot98821552018-02-23 10:40:17 +01001322#else
1323static int mmc_write_data(struct hsmmc *mmc_base, const char *buf,
1324 unsigned int size)
1325{
1326 return -ENOTSUPP;
1327}
1328#endif
Jean-Jacques Hiblot7fe2f192018-01-30 16:01:30 +01001329static void omap_hsmmc_stop_clock(struct hsmmc *mmc_base)
1330{
1331 writel(readl(&mmc_base->sysctl) & ~CEN_ENABLE, &mmc_base->sysctl);
1332}
1333
1334static void omap_hsmmc_start_clock(struct hsmmc *mmc_base)
1335{
1336 writel(readl(&mmc_base->sysctl) | CEN_ENABLE, &mmc_base->sysctl);
1337}
1338
1339static void omap_hsmmc_set_clock(struct mmc *mmc)
1340{
1341 struct omap_hsmmc_data *priv = omap_hsmmc_get_data(mmc);
1342 struct hsmmc *mmc_base;
1343 unsigned int dsor = 0;
1344 ulong start;
1345
1346 mmc_base = priv->base_addr;
1347 omap_hsmmc_stop_clock(mmc_base);
1348
1349 /* TODO: Is setting DTO required here? */
1350 mmc_reg_out(&mmc_base->sysctl, (ICE_MASK | DTO_MASK),
1351 (ICE_STOP | DTO_15THDTO));
1352
1353 if (mmc->clock != 0) {
1354 dsor = DIV_ROUND_UP(MMC_CLOCK_REFERENCE * 1000000, mmc->clock);
1355 if (dsor > CLKD_MAX)
1356 dsor = CLKD_MAX;
1357 } else {
1358 dsor = CLKD_MAX;
1359 }
1360
1361 mmc_reg_out(&mmc_base->sysctl, ICE_MASK | CLKD_MASK,
1362 (dsor << CLKD_OFFSET) | ICE_OSCILLATE);
1363
1364 start = get_timer(0);
1365 while ((readl(&mmc_base->sysctl) & ICS_MASK) == ICS_NOTREADY) {
1366 if (get_timer(0) - start > MAX_RETRY_MS) {
1367 printf("%s: timedout waiting for ics!\n", __func__);
1368 return;
1369 }
1370 }
1371
Jean-Jacques Hiblot6ce31e42018-01-30 16:01:43 +01001372 priv->clock = MMC_CLOCK_REFERENCE * 1000000 / dsor;
1373 mmc->clock = priv->clock;
Jean-Jacques Hiblot7fe2f192018-01-30 16:01:30 +01001374 omap_hsmmc_start_clock(mmc_base);
1375}
1376
Kishon Vijay Abraham I2e18c9b2018-01-30 16:01:31 +01001377static void omap_hsmmc_set_bus_width(struct mmc *mmc)
Sukumar Ghoraic53f5e52010-09-18 20:32:33 -07001378{
Jean-Jacques Hiblotd58ef8e2017-03-22 16:00:31 +01001379 struct omap_hsmmc_data *priv = omap_hsmmc_get_data(mmc);
Nikita Kiryanov13822862012-12-03 02:19:43 +00001380 struct hsmmc *mmc_base;
Sukumar Ghoraic53f5e52010-09-18 20:32:33 -07001381
Jean-Jacques Hiblotd58ef8e2017-03-22 16:00:31 +01001382 mmc_base = priv->base_addr;
Sukumar Ghoraic53f5e52010-09-18 20:32:33 -07001383 /* configue bus width */
1384 switch (mmc->bus_width) {
1385 case 8:
1386 writel(readl(&mmc_base->con) | DTW_8_BITMODE,
1387 &mmc_base->con);
1388 break;
1389
1390 case 4:
1391 writel(readl(&mmc_base->con) & ~DTW_8_BITMODE,
1392 &mmc_base->con);
1393 writel(readl(&mmc_base->hctl) | DTW_4_BITMODE,
1394 &mmc_base->hctl);
1395 break;
1396
1397 case 1:
1398 default:
1399 writel(readl(&mmc_base->con) & ~DTW_8_BITMODE,
1400 &mmc_base->con);
1401 writel(readl(&mmc_base->hctl) & ~DTW_4_BITMODE,
1402 &mmc_base->hctl);
1403 break;
1404 }
1405
Kishon Vijay Abraham I2e18c9b2018-01-30 16:01:31 +01001406 priv->bus_width = mmc->bus_width;
1407}
1408
1409#if !CONFIG_IS_ENABLED(DM_MMC)
1410static int omap_hsmmc_set_ios(struct mmc *mmc)
1411{
1412 struct omap_hsmmc_data *priv = omap_hsmmc_get_data(mmc);
1413#else
1414static int omap_hsmmc_set_ios(struct udevice *dev)
1415{
1416 struct omap_hsmmc_data *priv = dev_get_priv(dev);
1417 struct mmc_uclass_priv *upriv = dev_get_uclass_priv(dev);
1418 struct mmc *mmc = upriv->mmc;
1419#endif
Kishon Vijay Abraham Ie1f25c02018-01-30 16:01:45 +01001420 struct hsmmc *mmc_base = priv->base_addr;
Jean-Jacques Hiblot7a41bb42018-01-30 16:01:46 +01001421 int ret = 0;
Kishon Vijay Abraham I2e18c9b2018-01-30 16:01:31 +01001422
1423 if (priv->bus_width != mmc->bus_width)
1424 omap_hsmmc_set_bus_width(mmc);
1425
Jean-Jacques Hiblot7fe2f192018-01-30 16:01:30 +01001426 if (priv->clock != mmc->clock)
1427 omap_hsmmc_set_clock(mmc);
Jaehoon Chungb6cd1d32016-12-30 15:30:16 +09001428
Kishon Vijay Abraham Ie1f25c02018-01-30 16:01:45 +01001429 if (mmc->clk_disable)
1430 omap_hsmmc_stop_clock(mmc_base);
1431 else
1432 omap_hsmmc_start_clock(mmc_base);
1433
Jean-Jacques Hiblotcf38d4e2018-01-30 16:01:33 +01001434#if CONFIG_IS_ENABLED(DM_MMC)
1435 if (priv->mode != mmc->selected_mode)
1436 omap_hsmmc_set_timing(mmc);
Jean-Jacques Hiblot7a41bb42018-01-30 16:01:46 +01001437
1438#if CONFIG_IS_ENABLED(MMC_IO_VOLTAGE)
1439 if (priv->signal_voltage != mmc->signal_voltage)
1440 ret = omap_hsmmc_set_signal_voltage(mmc);
Jean-Jacques Hiblotcf38d4e2018-01-30 16:01:33 +01001441#endif
Jean-Jacques Hiblot7a41bb42018-01-30 16:01:46 +01001442#endif
1443 return ret;
Sukumar Ghoraic53f5e52010-09-18 20:32:33 -07001444}
1445
Pantelis Antoniouc9e75912014-02-26 19:28:45 +02001446#ifdef OMAP_HSMMC_USE_GPIO
Simon Glass5f4bd8c2017-07-04 13:31:19 -06001447#if CONFIG_IS_ENABLED(DM_MMC)
Jean-Jacques Hiblot8fc9d3a2017-04-14 19:50:02 +02001448static int omap_hsmmc_getcd(struct udevice *dev)
Pantelis Antoniouc9e75912014-02-26 19:28:45 +02001449{
Adam Ford6122af42018-08-21 07:16:56 -05001450 int value = -1;
1451#if CONFIG_IS_ENABLED(DM_GPIO)
Adam Fordac740ff2018-09-08 08:16:23 -05001452 struct omap_hsmmc_data *priv = dev_get_priv(dev);
Mugunthan V Nd97631a2015-09-28 12:56:30 +05301453 value = dm_gpio_get_value(&priv->cd_gpio);
Adam Ford6122af42018-08-21 07:16:56 -05001454#endif
Mugunthan V Nd97631a2015-09-28 12:56:30 +05301455 /* if no CD return as 1 */
1456 if (value < 0)
1457 return 1;
1458
Mugunthan V Nd97631a2015-09-28 12:56:30 +05301459 return value;
1460}
1461
Jean-Jacques Hiblot8fc9d3a2017-04-14 19:50:02 +02001462static int omap_hsmmc_getwp(struct udevice *dev)
Mugunthan V Nd97631a2015-09-28 12:56:30 +05301463{
Adam Ford6122af42018-08-21 07:16:56 -05001464 int value = 0;
1465#if CONFIG_IS_ENABLED(DM_GPIO)
Jean-Jacques Hiblot8fc9d3a2017-04-14 19:50:02 +02001466 struct omap_hsmmc_data *priv = dev_get_priv(dev);
Mugunthan V Nd97631a2015-09-28 12:56:30 +05301467 value = dm_gpio_get_value(&priv->wp_gpio);
Adam Ford6122af42018-08-21 07:16:56 -05001468#endif
Mugunthan V Nd97631a2015-09-28 12:56:30 +05301469 /* if no WP return as 0 */
1470 if (value < 0)
1471 return 0;
1472 return value;
1473}
1474#else
1475static int omap_hsmmc_getcd(struct mmc *mmc)
1476{
Jean-Jacques Hiblotd58ef8e2017-03-22 16:00:31 +01001477 struct omap_hsmmc_data *priv = omap_hsmmc_get_data(mmc);
Pantelis Antoniouc9e75912014-02-26 19:28:45 +02001478 int cd_gpio;
1479
1480 /* if no CD return as 1 */
Jean-Jacques Hiblotd58ef8e2017-03-22 16:00:31 +01001481 cd_gpio = priv->cd_gpio;
Pantelis Antoniouc9e75912014-02-26 19:28:45 +02001482 if (cd_gpio < 0)
1483 return 1;
1484
Igor Grinberg2f4e0952014-11-03 11:32:23 +02001485 /* NOTE: assumes card detect signal is active-low */
1486 return !gpio_get_value(cd_gpio);
Pantelis Antoniouc9e75912014-02-26 19:28:45 +02001487}
1488
1489static int omap_hsmmc_getwp(struct mmc *mmc)
1490{
Jean-Jacques Hiblotd58ef8e2017-03-22 16:00:31 +01001491 struct omap_hsmmc_data *priv = omap_hsmmc_get_data(mmc);
Pantelis Antoniouc9e75912014-02-26 19:28:45 +02001492 int wp_gpio;
1493
1494 /* if no WP return as 0 */
Jean-Jacques Hiblotd58ef8e2017-03-22 16:00:31 +01001495 wp_gpio = priv->wp_gpio;
Pantelis Antoniouc9e75912014-02-26 19:28:45 +02001496 if (wp_gpio < 0)
1497 return 0;
1498
Igor Grinberg2f4e0952014-11-03 11:32:23 +02001499 /* NOTE: assumes write protect signal is active-high */
Pantelis Antoniouc9e75912014-02-26 19:28:45 +02001500 return gpio_get_value(wp_gpio);
1501}
1502#endif
Mugunthan V Nd97631a2015-09-28 12:56:30 +05301503#endif
Pantelis Antoniouc9e75912014-02-26 19:28:45 +02001504
Simon Glass5f4bd8c2017-07-04 13:31:19 -06001505#if CONFIG_IS_ENABLED(DM_MMC)
Jean-Jacques Hiblot8fc9d3a2017-04-14 19:50:02 +02001506static const struct dm_mmc_ops omap_hsmmc_ops = {
1507 .send_cmd = omap_hsmmc_send_cmd,
1508 .set_ios = omap_hsmmc_set_ios,
1509#ifdef OMAP_HSMMC_USE_GPIO
1510 .get_cd = omap_hsmmc_getcd,
1511 .get_wp = omap_hsmmc_getwp,
1512#endif
Jean-Jacques Hiblotf0f821b2018-01-30 16:01:35 +01001513#ifdef MMC_SUPPORTS_TUNING
1514 .execute_tuning = omap_hsmmc_execute_tuning,
1515#endif
Jean-Jacques Hiblot7a41bb42018-01-30 16:01:46 +01001516 .wait_dat0 = omap_hsmmc_wait_dat0,
Jean-Jacques Hiblot8fc9d3a2017-04-14 19:50:02 +02001517};
1518#else
Pantelis Antoniouc9e75912014-02-26 19:28:45 +02001519static const struct mmc_ops omap_hsmmc_ops = {
1520 .send_cmd = omap_hsmmc_send_cmd,
1521 .set_ios = omap_hsmmc_set_ios,
1522 .init = omap_hsmmc_init_setup,
1523#ifdef OMAP_HSMMC_USE_GPIO
1524 .getcd = omap_hsmmc_getcd,
1525 .getwp = omap_hsmmc_getwp,
1526#endif
1527};
Jean-Jacques Hiblot8fc9d3a2017-04-14 19:50:02 +02001528#endif
Pantelis Antoniouc9e75912014-02-26 19:28:45 +02001529
Simon Glass5f4bd8c2017-07-04 13:31:19 -06001530#if !CONFIG_IS_ENABLED(DM_MMC)
Nikita Kiryanov4be9dbc2012-12-03 02:19:47 +00001531int omap_mmc_init(int dev_index, uint host_caps_mask, uint f_max, int cd_gpio,
1532 int wp_gpio)
Sukumar Ghoraic53f5e52010-09-18 20:32:33 -07001533{
Pantelis Antoniou2c850462014-03-11 19:34:20 +02001534 struct mmc *mmc;
Jean-Jacques Hiblotd58ef8e2017-03-22 16:00:31 +01001535 struct omap_hsmmc_data *priv;
Pantelis Antoniou2c850462014-03-11 19:34:20 +02001536 struct mmc_config *cfg;
1537 uint host_caps_val;
1538
Alex Kiernan4b9cb772018-02-09 15:24:38 +00001539 priv = calloc(1, sizeof(*priv));
Jean-Jacques Hiblotd58ef8e2017-03-22 16:00:31 +01001540 if (priv == NULL)
Pantelis Antoniou2c850462014-03-11 19:34:20 +02001541 return -1;
Sukumar Ghoraic53f5e52010-09-18 20:32:33 -07001542
Rob Herring5fd3edd2015-03-23 17:56:59 -05001543 host_caps_val = MMC_MODE_4BIT | MMC_MODE_HS_52MHz | MMC_MODE_HS;
Sukumar Ghoraic53f5e52010-09-18 20:32:33 -07001544
1545 switch (dev_index) {
1546 case 0:
Jean-Jacques Hiblotd58ef8e2017-03-22 16:00:31 +01001547 priv->base_addr = (struct hsmmc *)OMAP_HSMMC1_BASE;
Sukumar Ghoraic53f5e52010-09-18 20:32:33 -07001548 break;
Tom Rinifd6e2942011-10-12 06:20:50 +00001549#ifdef OMAP_HSMMC2_BASE
Sukumar Ghoraic53f5e52010-09-18 20:32:33 -07001550 case 1:
Jean-Jacques Hiblotd58ef8e2017-03-22 16:00:31 +01001551 priv->base_addr = (struct hsmmc *)OMAP_HSMMC2_BASE;
Lubomir Popov19df4122013-08-14 18:59:18 +03001552#if (defined(CONFIG_OMAP44XX) || defined(CONFIG_OMAP54XX) || \
Nishanth Menon813fe9d2016-11-29 15:22:00 +05301553 defined(CONFIG_DRA7XX) || defined(CONFIG_AM33XX) || \
Roger Quadros44157de2015-09-19 16:26:53 +05301554 defined(CONFIG_AM43XX) || defined(CONFIG_SOC_KEYSTONE)) && \
1555 defined(CONFIG_HSMMC2_8BIT)
Lubomir Popov19df4122013-08-14 18:59:18 +03001556 /* Enable 8-bit interface for eMMC on OMAP4/5 or DRA7XX */
1557 host_caps_val |= MMC_MODE_8BIT;
1558#endif
Sukumar Ghoraic53f5e52010-09-18 20:32:33 -07001559 break;
Tom Rinifd6e2942011-10-12 06:20:50 +00001560#endif
1561#ifdef OMAP_HSMMC3_BASE
Sukumar Ghoraic53f5e52010-09-18 20:32:33 -07001562 case 2:
Jean-Jacques Hiblotd58ef8e2017-03-22 16:00:31 +01001563 priv->base_addr = (struct hsmmc *)OMAP_HSMMC3_BASE;
Nishanth Menon813fe9d2016-11-29 15:22:00 +05301564#if defined(CONFIG_DRA7XX) && defined(CONFIG_HSMMC3_8BIT)
Lubomir Popov19df4122013-08-14 18:59:18 +03001565 /* Enable 8-bit interface for eMMC on DRA7XX */
1566 host_caps_val |= MMC_MODE_8BIT;
1567#endif
Sukumar Ghoraic53f5e52010-09-18 20:32:33 -07001568 break;
Tom Rinifd6e2942011-10-12 06:20:50 +00001569#endif
Sukumar Ghoraic53f5e52010-09-18 20:32:33 -07001570 default:
Jean-Jacques Hiblotd58ef8e2017-03-22 16:00:31 +01001571 priv->base_addr = (struct hsmmc *)OMAP_HSMMC1_BASE;
Sukumar Ghoraic53f5e52010-09-18 20:32:33 -07001572 return 1;
1573 }
Pantelis Antoniouc9e75912014-02-26 19:28:45 +02001574#ifdef OMAP_HSMMC_USE_GPIO
1575 /* on error gpio values are set to -1, which is what we want */
Jean-Jacques Hiblotd58ef8e2017-03-22 16:00:31 +01001576 priv->cd_gpio = omap_mmc_setup_gpio_in(cd_gpio, "mmc_cd");
1577 priv->wp_gpio = omap_mmc_setup_gpio_in(wp_gpio, "mmc_wp");
Pantelis Antoniouc9e75912014-02-26 19:28:45 +02001578#endif
Peter Korsgaard47c6b2a2013-03-21 04:00:04 +00001579
Jean-Jacques Hiblotd58ef8e2017-03-22 16:00:31 +01001580 cfg = &priv->cfg;
Sukumar Ghoraic53f5e52010-09-18 20:32:33 -07001581
Pantelis Antoniou2c850462014-03-11 19:34:20 +02001582 cfg->name = "OMAP SD/MMC";
1583 cfg->ops = &omap_hsmmc_ops;
1584
1585 cfg->voltages = MMC_VDD_32_33 | MMC_VDD_33_34 | MMC_VDD_165_195;
1586 cfg->host_caps = host_caps_val & ~host_caps_mask;
1587
1588 cfg->f_min = 400000;
Jonathan Solnita9b05562012-02-24 11:30:18 +00001589
1590 if (f_max != 0)
Pantelis Antoniou2c850462014-03-11 19:34:20 +02001591 cfg->f_max = f_max;
Jonathan Solnita9b05562012-02-24 11:30:18 +00001592 else {
Pantelis Antoniou2c850462014-03-11 19:34:20 +02001593 if (cfg->host_caps & MMC_MODE_HS) {
1594 if (cfg->host_caps & MMC_MODE_HS_52MHz)
1595 cfg->f_max = 52000000;
Jonathan Solnita9b05562012-02-24 11:30:18 +00001596 else
Pantelis Antoniou2c850462014-03-11 19:34:20 +02001597 cfg->f_max = 26000000;
Jonathan Solnita9b05562012-02-24 11:30:18 +00001598 } else
Pantelis Antoniou2c850462014-03-11 19:34:20 +02001599 cfg->f_max = 20000000;
Jonathan Solnita9b05562012-02-24 11:30:18 +00001600 }
Sukumar Ghoraic53f5e52010-09-18 20:32:33 -07001601
Pantelis Antoniou2c850462014-03-11 19:34:20 +02001602 cfg->b_max = CONFIG_SYS_MMC_MAX_BLK_COUNT;
John Rigbyf2f43662011-04-18 05:50:08 +00001603
John Rigby91fcc4b2011-04-19 05:48:14 +00001604#if defined(CONFIG_OMAP34XX)
1605 /*
1606 * Silicon revs 2.1 and older do not support multiblock transfers.
1607 */
1608 if ((get_cpu_family() == CPU_OMAP34XX) && (get_cpu_rev() <= CPU_3XX_ES21))
Pantelis Antoniou2c850462014-03-11 19:34:20 +02001609 cfg->b_max = 1;
John Rigby91fcc4b2011-04-19 05:48:14 +00001610#endif
Kishon Vijay Abraham I8c2efe92018-01-30 16:01:41 +01001611
Jean-Jacques Hiblotd58ef8e2017-03-22 16:00:31 +01001612 mmc = mmc_create(cfg, priv);
Pantelis Antoniou2c850462014-03-11 19:34:20 +02001613 if (mmc == NULL)
1614 return -1;
Sukumar Ghoraic53f5e52010-09-18 20:32:33 -07001615
1616 return 0;
1617}
Mugunthan V Nd97631a2015-09-28 12:56:30 +05301618#else
Kishon Vijay Abraham Ie7da6ac2018-01-30 16:01:40 +01001619
1620#ifdef CONFIG_IODELAY_RECALIBRATION
1621static struct pad_conf_entry *
1622omap_hsmmc_get_pad_conf_entry(const fdt32_t *pinctrl, int count)
1623{
1624 int index = 0;
1625 struct pad_conf_entry *padconf;
1626
1627 padconf = (struct pad_conf_entry *)malloc(sizeof(*padconf) * count);
1628 if (!padconf) {
1629 debug("failed to allocate memory\n");
1630 return 0;
1631 }
1632
1633 while (index < count) {
1634 padconf[index].offset = fdt32_to_cpu(pinctrl[2 * index]);
1635 padconf[index].val = fdt32_to_cpu(pinctrl[2 * index + 1]);
1636 index++;
1637 }
1638
1639 return padconf;
1640}
1641
1642static struct iodelay_cfg_entry *
1643omap_hsmmc_get_iodelay_cfg_entry(const fdt32_t *pinctrl, int count)
1644{
1645 int index = 0;
1646 struct iodelay_cfg_entry *iodelay;
1647
1648 iodelay = (struct iodelay_cfg_entry *)malloc(sizeof(*iodelay) * count);
1649 if (!iodelay) {
1650 debug("failed to allocate memory\n");
1651 return 0;
1652 }
1653
1654 while (index < count) {
1655 iodelay[index].offset = fdt32_to_cpu(pinctrl[3 * index]);
1656 iodelay[index].a_delay = fdt32_to_cpu(pinctrl[3 * index + 1]);
1657 iodelay[index].g_delay = fdt32_to_cpu(pinctrl[3 * index + 2]);
1658 index++;
1659 }
1660
1661 return iodelay;
1662}
1663
1664static const fdt32_t *omap_hsmmc_get_pinctrl_entry(u32 phandle,
1665 const char *name, int *len)
1666{
1667 const void *fdt = gd->fdt_blob;
1668 int offset;
1669 const fdt32_t *pinctrl;
1670
1671 offset = fdt_node_offset_by_phandle(fdt, phandle);
1672 if (offset < 0) {
1673 debug("failed to get pinctrl node %s.\n",
1674 fdt_strerror(offset));
1675 return 0;
1676 }
1677
1678 pinctrl = fdt_getprop(fdt, offset, name, len);
1679 if (!pinctrl) {
1680 debug("failed to get property %s\n", name);
1681 return 0;
1682 }
1683
1684 return pinctrl;
1685}
1686
1687static uint32_t omap_hsmmc_get_pad_conf_phandle(struct mmc *mmc,
1688 char *prop_name)
1689{
1690 const void *fdt = gd->fdt_blob;
1691 const __be32 *phandle;
1692 int node = dev_of_offset(mmc->dev);
1693
1694 phandle = fdt_getprop(fdt, node, prop_name, NULL);
1695 if (!phandle) {
1696 debug("failed to get property %s\n", prop_name);
1697 return 0;
1698 }
1699
1700 return fdt32_to_cpu(*phandle);
1701}
1702
1703static uint32_t omap_hsmmc_get_iodelay_phandle(struct mmc *mmc,
1704 char *prop_name)
1705{
1706 const void *fdt = gd->fdt_blob;
1707 const __be32 *phandle;
1708 int len;
1709 int count;
1710 int node = dev_of_offset(mmc->dev);
1711
1712 phandle = fdt_getprop(fdt, node, prop_name, &len);
1713 if (!phandle) {
1714 debug("failed to get property %s\n", prop_name);
1715 return 0;
1716 }
1717
1718 /* No manual mode iodelay values if count < 2 */
1719 count = len / sizeof(*phandle);
1720 if (count < 2)
1721 return 0;
1722
1723 return fdt32_to_cpu(*(phandle + 1));
1724}
1725
1726static struct pad_conf_entry *
1727omap_hsmmc_get_pad_conf(struct mmc *mmc, char *prop_name, int *npads)
1728{
1729 int len;
1730 int count;
1731 struct pad_conf_entry *padconf;
1732 u32 phandle;
1733 const fdt32_t *pinctrl;
1734
1735 phandle = omap_hsmmc_get_pad_conf_phandle(mmc, prop_name);
1736 if (!phandle)
1737 return ERR_PTR(-EINVAL);
1738
1739 pinctrl = omap_hsmmc_get_pinctrl_entry(phandle, "pinctrl-single,pins",
1740 &len);
1741 if (!pinctrl)
1742 return ERR_PTR(-EINVAL);
1743
1744 count = (len / sizeof(*pinctrl)) / 2;
1745 padconf = omap_hsmmc_get_pad_conf_entry(pinctrl, count);
1746 if (!padconf)
1747 return ERR_PTR(-EINVAL);
1748
1749 *npads = count;
1750
1751 return padconf;
1752}
1753
1754static struct iodelay_cfg_entry *
1755omap_hsmmc_get_iodelay(struct mmc *mmc, char *prop_name, int *niodelay)
1756{
1757 int len;
1758 int count;
1759 struct iodelay_cfg_entry *iodelay;
1760 u32 phandle;
1761 const fdt32_t *pinctrl;
1762
1763 phandle = omap_hsmmc_get_iodelay_phandle(mmc, prop_name);
1764 /* Not all modes have manual mode iodelay values. So its not fatal */
1765 if (!phandle)
1766 return 0;
1767
1768 pinctrl = omap_hsmmc_get_pinctrl_entry(phandle, "pinctrl-pin-array",
1769 &len);
1770 if (!pinctrl)
1771 return ERR_PTR(-EINVAL);
1772
1773 count = (len / sizeof(*pinctrl)) / 3;
1774 iodelay = omap_hsmmc_get_iodelay_cfg_entry(pinctrl, count);
1775 if (!iodelay)
1776 return ERR_PTR(-EINVAL);
1777
1778 *niodelay = count;
1779
1780 return iodelay;
1781}
1782
1783static struct omap_hsmmc_pinctrl_state *
1784omap_hsmmc_get_pinctrl_by_mode(struct mmc *mmc, char *mode)
1785{
1786 int index;
1787 int npads = 0;
1788 int niodelays = 0;
1789 const void *fdt = gd->fdt_blob;
1790 int node = dev_of_offset(mmc->dev);
1791 char prop_name[11];
1792 struct omap_hsmmc_pinctrl_state *pinctrl_state;
1793
1794 pinctrl_state = (struct omap_hsmmc_pinctrl_state *)
1795 malloc(sizeof(*pinctrl_state));
1796 if (!pinctrl_state) {
1797 debug("failed to allocate memory\n");
1798 return 0;
1799 }
1800
1801 index = fdt_stringlist_search(fdt, node, "pinctrl-names", mode);
1802 if (index < 0) {
1803 debug("fail to find %s mode %s\n", mode, fdt_strerror(index));
1804 goto err_pinctrl_state;
1805 }
1806
1807 sprintf(prop_name, "pinctrl-%d", index);
1808
1809 pinctrl_state->padconf = omap_hsmmc_get_pad_conf(mmc, prop_name,
1810 &npads);
1811 if (IS_ERR(pinctrl_state->padconf))
1812 goto err_pinctrl_state;
1813 pinctrl_state->npads = npads;
1814
1815 pinctrl_state->iodelay = omap_hsmmc_get_iodelay(mmc, prop_name,
1816 &niodelays);
1817 if (IS_ERR(pinctrl_state->iodelay))
1818 goto err_padconf;
1819 pinctrl_state->niodelays = niodelays;
1820
1821 return pinctrl_state;
1822
1823err_padconf:
1824 kfree(pinctrl_state->padconf);
1825
1826err_pinctrl_state:
1827 kfree(pinctrl_state);
1828 return 0;
1829}
1830
Jean-Jacques Hiblotdae1ad42018-01-30 16:01:42 +01001831#define OMAP_HSMMC_SETUP_PINCTRL(capmask, mode, optional) \
Kishon Vijay Abraham I8c2efe92018-01-30 16:01:41 +01001832 do { \
1833 struct omap_hsmmc_pinctrl_state *s = NULL; \
1834 char str[20]; \
1835 if (!(cfg->host_caps & capmask)) \
1836 break; \
1837 \
1838 if (priv->hw_rev) { \
1839 sprintf(str, "%s-%s", #mode, priv->hw_rev); \
1840 s = omap_hsmmc_get_pinctrl_by_mode(mmc, str); \
1841 } \
1842 \
1843 if (!s) \
1844 s = omap_hsmmc_get_pinctrl_by_mode(mmc, #mode); \
1845 \
Jean-Jacques Hiblotdae1ad42018-01-30 16:01:42 +01001846 if (!s && !optional) { \
Kishon Vijay Abraham I8c2efe92018-01-30 16:01:41 +01001847 debug("%s: no pinctrl for %s\n", \
1848 mmc->dev->name, #mode); \
1849 cfg->host_caps &= ~(capmask); \
1850 } else { \
1851 priv->mode##_pinctrl_state = s; \
1852 } \
Kishon Vijay Abraham Ie7da6ac2018-01-30 16:01:40 +01001853 } while (0)
1854
1855static int omap_hsmmc_get_pinctrl_state(struct mmc *mmc)
1856{
1857 struct omap_hsmmc_data *priv = omap_hsmmc_get_data(mmc);
1858 struct mmc_config *cfg = omap_hsmmc_get_cfg(mmc);
1859 struct omap_hsmmc_pinctrl_state *default_pinctrl;
1860
1861 if (!(priv->controller_flags & OMAP_HSMMC_REQUIRE_IODELAY))
1862 return 0;
1863
1864 default_pinctrl = omap_hsmmc_get_pinctrl_by_mode(mmc, "default");
1865 if (!default_pinctrl) {
1866 printf("no pinctrl state for default mode\n");
1867 return -EINVAL;
1868 }
1869
1870 priv->default_pinctrl_state = default_pinctrl;
1871
Jean-Jacques Hiblotdae1ad42018-01-30 16:01:42 +01001872 OMAP_HSMMC_SETUP_PINCTRL(MMC_CAP(UHS_SDR104), sdr104, false);
1873 OMAP_HSMMC_SETUP_PINCTRL(MMC_CAP(UHS_SDR50), sdr50, false);
1874 OMAP_HSMMC_SETUP_PINCTRL(MMC_CAP(UHS_DDR50), ddr50, false);
1875 OMAP_HSMMC_SETUP_PINCTRL(MMC_CAP(UHS_SDR25), sdr25, false);
1876 OMAP_HSMMC_SETUP_PINCTRL(MMC_CAP(UHS_SDR12), sdr12, false);
Kishon Vijay Abraham Ie7da6ac2018-01-30 16:01:40 +01001877
Jean-Jacques Hiblotdae1ad42018-01-30 16:01:42 +01001878 OMAP_HSMMC_SETUP_PINCTRL(MMC_CAP(MMC_HS_200), hs200_1_8v, false);
1879 OMAP_HSMMC_SETUP_PINCTRL(MMC_CAP(MMC_DDR_52), ddr_1_8v, false);
1880 OMAP_HSMMC_SETUP_PINCTRL(MMC_MODE_HS, hs, true);
Kishon Vijay Abraham Ie7da6ac2018-01-30 16:01:40 +01001881
1882 return 0;
1883}
1884#endif
1885
Lokesh Vutla9a696fb2017-04-26 13:37:05 +05301886#if CONFIG_IS_ENABLED(OF_CONTROL) && !CONFIG_IS_ENABLED(OF_PLATDATA)
Kishon Vijay Abraham I8c2efe92018-01-30 16:01:41 +01001887#ifdef CONFIG_OMAP54XX
1888__weak const struct mmc_platform_fixups *platform_fixups_mmc(uint32_t addr)
1889{
1890 return NULL;
1891}
1892#endif
1893
Mugunthan V Nd97631a2015-09-28 12:56:30 +05301894static int omap_hsmmc_ofdata_to_platdata(struct udevice *dev)
1895{
Jean-Jacques Hiblotae51a662017-03-22 16:00:33 +01001896 struct omap_hsmmc_plat *plat = dev_get_platdata(dev);
Kishon Vijay Abraham Ie7da6ac2018-01-30 16:01:40 +01001897 struct omap_mmc_of_data *of_data = (void *)dev_get_driver_data(dev);
1898
Jean-Jacques Hiblotae51a662017-03-22 16:00:33 +01001899 struct mmc_config *cfg = &plat->cfg;
Kishon Vijay Abraham I8c2efe92018-01-30 16:01:41 +01001900#ifdef CONFIG_OMAP54XX
1901 const struct mmc_platform_fixups *fixups;
1902#endif
Mugunthan V Nd97631a2015-09-28 12:56:30 +05301903 const void *fdt = gd->fdt_blob;
Simon Glassdd79d6e2017-01-17 16:52:55 -07001904 int node = dev_of_offset(dev);
Kishon Vijay Abraham I569c3d52018-01-30 16:01:38 +01001905 int ret;
Mugunthan V Nd97631a2015-09-28 12:56:30 +05301906
Simon Glassba1dea42017-05-17 17:18:05 -06001907 plat->base_addr = map_physmem(devfdt_get_addr(dev),
1908 sizeof(struct hsmmc *),
Jean-Jacques Hiblot3d45bb42017-09-21 16:51:32 +02001909 MAP_NOCACHE);
Mugunthan V Nd97631a2015-09-28 12:56:30 +05301910
Kishon Vijay Abraham I569c3d52018-01-30 16:01:38 +01001911 ret = mmc_of_parse(dev, cfg);
1912 if (ret < 0)
1913 return ret;
Mugunthan V Nd97631a2015-09-28 12:56:30 +05301914
Jean-Jacques Hiblot8e2bdbd2018-02-23 10:40:19 +01001915 if (!cfg->f_max)
1916 cfg->f_max = 52000000;
Kishon Vijay Abraham I569c3d52018-01-30 16:01:38 +01001917 cfg->host_caps |= MMC_MODE_HS_52MHz | MMC_MODE_HS;
Mugunthan V Nd97631a2015-09-28 12:56:30 +05301918 cfg->f_min = 400000;
Mugunthan V Nd97631a2015-09-28 12:56:30 +05301919 cfg->voltages = MMC_VDD_32_33 | MMC_VDD_33_34 | MMC_VDD_165_195;
1920 cfg->b_max = CONFIG_SYS_MMC_MAX_BLK_COUNT;
Kishon Vijay Abraham I73897ed2018-01-30 16:01:32 +01001921 if (fdtdec_get_bool(fdt, node, "ti,dual-volt"))
1922 plat->controller_flags |= OMAP_HSMMC_SUPPORTS_DUAL_VOLT;
1923 if (fdtdec_get_bool(fdt, node, "no-1-8-v"))
1924 plat->controller_flags |= OMAP_HSMMC_NO_1_8_V;
Kishon Vijay Abraham Ie7da6ac2018-01-30 16:01:40 +01001925 if (of_data)
1926 plat->controller_flags |= of_data->controller_flags;
Mugunthan V Nd97631a2015-09-28 12:56:30 +05301927
Kishon Vijay Abraham I8c2efe92018-01-30 16:01:41 +01001928#ifdef CONFIG_OMAP54XX
1929 fixups = platform_fixups_mmc(devfdt_get_addr(dev));
1930 if (fixups) {
1931 plat->hw_rev = fixups->hw_rev;
1932 cfg->host_caps &= ~fixups->unsupported_caps;
1933 cfg->f_max = fixups->max_freq;
1934 }
1935#endif
1936
Mugunthan V Nd97631a2015-09-28 12:56:30 +05301937 return 0;
1938}
Lokesh Vutla9a696fb2017-04-26 13:37:05 +05301939#endif
Mugunthan V Nd97631a2015-09-28 12:56:30 +05301940
Jean-Jacques Hiblota3c556c2017-03-22 16:00:34 +01001941#ifdef CONFIG_BLK
1942
1943static int omap_hsmmc_bind(struct udevice *dev)
1944{
1945 struct omap_hsmmc_plat *plat = dev_get_platdata(dev);
Jean-Jacques Hiblot4cb36a22018-02-23 10:40:16 +01001946 plat->mmc = calloc(1, sizeof(struct mmc));
1947 return mmc_bind(dev, plat->mmc, &plat->cfg);
Jean-Jacques Hiblota3c556c2017-03-22 16:00:34 +01001948}
1949#endif
Mugunthan V Nd97631a2015-09-28 12:56:30 +05301950static int omap_hsmmc_probe(struct udevice *dev)
1951{
Jean-Jacques Hiblotae51a662017-03-22 16:00:33 +01001952 struct omap_hsmmc_plat *plat = dev_get_platdata(dev);
Mugunthan V Nd97631a2015-09-28 12:56:30 +05301953 struct mmc_uclass_priv *upriv = dev_get_uclass_priv(dev);
1954 struct omap_hsmmc_data *priv = dev_get_priv(dev);
Jean-Jacques Hiblotae51a662017-03-22 16:00:33 +01001955 struct mmc_config *cfg = &plat->cfg;
Mugunthan V Nd97631a2015-09-28 12:56:30 +05301956 struct mmc *mmc;
Kishon Vijay Abraham Ie7da6ac2018-01-30 16:01:40 +01001957#ifdef CONFIG_IODELAY_RECALIBRATION
1958 int ret;
1959#endif
Mugunthan V Nd97631a2015-09-28 12:56:30 +05301960
Mugunthan V Nd97631a2015-09-28 12:56:30 +05301961 cfg->name = "OMAP SD/MMC";
Lokesh Vutla9a696fb2017-04-26 13:37:05 +05301962 priv->base_addr = plat->base_addr;
Kishon Vijay Abraham Ie7da6ac2018-01-30 16:01:40 +01001963 priv->controller_flags = plat->controller_flags;
Kishon Vijay Abraham I8c2efe92018-01-30 16:01:41 +01001964 priv->hw_rev = plat->hw_rev;
Mugunthan V Nd97631a2015-09-28 12:56:30 +05301965
Jean-Jacques Hiblota3c556c2017-03-22 16:00:34 +01001966#ifdef CONFIG_BLK
Jean-Jacques Hiblot4cb36a22018-02-23 10:40:16 +01001967 mmc = plat->mmc;
Jean-Jacques Hiblota3c556c2017-03-22 16:00:34 +01001968#else
Mugunthan V Nd97631a2015-09-28 12:56:30 +05301969 mmc = mmc_create(cfg, priv);
1970 if (mmc == NULL)
1971 return -1;
Jean-Jacques Hiblota3c556c2017-03-22 16:00:34 +01001972#endif
Jean-Jacques Hiblot7a41bb42018-01-30 16:01:46 +01001973#if CONFIG_IS_ENABLED(DM_REGULATOR)
1974 device_get_supply_regulator(dev, "pbias-supply",
1975 &priv->pbias_supply);
1976#endif
Adam Ford6122af42018-08-21 07:16:56 -05001977#if defined(OMAP_HSMMC_USE_GPIO)
1978#if CONFIG_IS_ENABLED(OF_CONTROL) && CONFIG_IS_ENABLED(DM_GPIO)
Mugunthan V Na9a0aa72016-04-04 17:28:01 +05301979 gpio_request_by_name(dev, "cd-gpios", 0, &priv->cd_gpio, GPIOD_IS_IN);
1980 gpio_request_by_name(dev, "wp-gpios", 0, &priv->wp_gpio, GPIOD_IS_IN);
1981#endif
Adam Ford6122af42018-08-21 07:16:56 -05001982#endif
Mugunthan V Na9a0aa72016-04-04 17:28:01 +05301983
Simon Glass77ca42b2016-05-01 13:52:34 -06001984 mmc->dev = dev;
Mugunthan V Nd97631a2015-09-28 12:56:30 +05301985 upriv->mmc = mmc;
1986
Kishon Vijay Abraham Ie7da6ac2018-01-30 16:01:40 +01001987#ifdef CONFIG_IODELAY_RECALIBRATION
1988 ret = omap_hsmmc_get_pinctrl_state(mmc);
1989 /*
1990 * disable high speed modes for the platforms that require IO delay
1991 * and for which we don't have this information
1992 */
1993 if ((ret < 0) &&
1994 (priv->controller_flags & OMAP_HSMMC_REQUIRE_IODELAY)) {
1995 priv->controller_flags &= ~OMAP_HSMMC_REQUIRE_IODELAY;
1996 cfg->host_caps &= ~(MMC_CAP(MMC_HS_200) | MMC_CAP(MMC_DDR_52) |
1997 UHS_CAPS);
1998 }
1999#endif
2000
Jean-Jacques Hiblot8fc9d3a2017-04-14 19:50:02 +02002001 return omap_hsmmc_init_setup(mmc);
Mugunthan V Nd97631a2015-09-28 12:56:30 +05302002}
2003
Lokesh Vutla9a696fb2017-04-26 13:37:05 +05302004#if CONFIG_IS_ENABLED(OF_CONTROL) && !CONFIG_IS_ENABLED(OF_PLATDATA)
Kishon Vijay Abraham Ie7da6ac2018-01-30 16:01:40 +01002005
2006static const struct omap_mmc_of_data dra7_mmc_of_data = {
2007 .controller_flags = OMAP_HSMMC_REQUIRE_IODELAY,
2008};
2009
Mugunthan V Nd97631a2015-09-28 12:56:30 +05302010static const struct udevice_id omap_hsmmc_ids[] = {
Jean-Jacques Hiblot3d45bb42017-09-21 16:51:32 +02002011 { .compatible = "ti,omap3-hsmmc" },
2012 { .compatible = "ti,omap4-hsmmc" },
2013 { .compatible = "ti,am33xx-hsmmc" },
Kishon Vijay Abraham Ie7da6ac2018-01-30 16:01:40 +01002014 { .compatible = "ti,dra7-hsmmc", .data = (ulong)&dra7_mmc_of_data },
Mugunthan V Nd97631a2015-09-28 12:56:30 +05302015 { }
2016};
Lokesh Vutla9a696fb2017-04-26 13:37:05 +05302017#endif
Mugunthan V Nd97631a2015-09-28 12:56:30 +05302018
2019U_BOOT_DRIVER(omap_hsmmc) = {
2020 .name = "omap_hsmmc",
2021 .id = UCLASS_MMC,
Lokesh Vutla9a696fb2017-04-26 13:37:05 +05302022#if CONFIG_IS_ENABLED(OF_CONTROL) && !CONFIG_IS_ENABLED(OF_PLATDATA)
Mugunthan V Nd97631a2015-09-28 12:56:30 +05302023 .of_match = omap_hsmmc_ids,
2024 .ofdata_to_platdata = omap_hsmmc_ofdata_to_platdata,
Lokesh Vutla9a696fb2017-04-26 13:37:05 +05302025 .platdata_auto_alloc_size = sizeof(struct omap_hsmmc_plat),
2026#endif
Jean-Jacques Hiblota3c556c2017-03-22 16:00:34 +01002027#ifdef CONFIG_BLK
2028 .bind = omap_hsmmc_bind,
2029#endif
Jean-Jacques Hiblot8fc9d3a2017-04-14 19:50:02 +02002030 .ops = &omap_hsmmc_ops,
Mugunthan V Nd97631a2015-09-28 12:56:30 +05302031 .probe = omap_hsmmc_probe,
2032 .priv_auto_alloc_size = sizeof(struct omap_hsmmc_data),
Bin Meng793260a2018-10-24 06:36:32 -07002033#if !CONFIG_IS_ENABLED(OF_CONTROL)
Lokesh Vutlac38e6452017-04-26 13:37:06 +05302034 .flags = DM_FLAG_PRE_RELOC,
Bin Meng793260a2018-10-24 06:36:32 -07002035#endif
Mugunthan V Nd97631a2015-09-28 12:56:30 +05302036};
2037#endif