blob: 5334723a9f0d7d230a0be63d5df1724134afa8b2 [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:
395 case SD_LEGACY:
396 case UHS_SDR12:
397 val |= AC12_UHSMC_SDR12;
398 break;
399 default:
400 val |= AC12_UHSMC_RES;
401 break;
402 }
403 writel(val, &mmc_base->ac12);
Kishon Vijay Abraham Ie7da6ac2018-01-30 16:01:40 +0100404
405#ifdef CONFIG_IODELAY_RECALIBRATION
406 omap_hsmmc_io_recalibrate(mmc);
407#endif
408 omap_hsmmc_start_clock(mmc_base);
Jean-Jacques Hiblotcf38d4e2018-01-30 16:01:33 +0100409}
410
Jean-Jacques Hiblot7a41bb42018-01-30 16:01:46 +0100411static void omap_hsmmc_conf_bus_power(struct mmc *mmc, uint signal_voltage)
Kishon Vijay Abraham I73897ed2018-01-30 16:01:32 +0100412{
413 struct hsmmc *mmc_base;
414 struct omap_hsmmc_data *priv = omap_hsmmc_get_data(mmc);
Jean-Jacques Hiblot7a41bb42018-01-30 16:01:46 +0100415 u32 hctl, ac12;
Kishon Vijay Abraham I73897ed2018-01-30 16:01:32 +0100416
417 mmc_base = priv->base_addr;
418
Jean-Jacques Hiblot7a41bb42018-01-30 16:01:46 +0100419 hctl = readl(&mmc_base->hctl) & ~SDVS_MASK;
420 ac12 = readl(&mmc_base->ac12) & ~AC12_V1V8_SIGEN;
Kishon Vijay Abraham I73897ed2018-01-30 16:01:32 +0100421
Jean-Jacques Hiblot7a41bb42018-01-30 16:01:46 +0100422 switch (signal_voltage) {
423 case MMC_SIGNAL_VOLTAGE_330:
Faiz Abbasfc1ad622019-04-05 14:18:46 +0530424 hctl |= SDVS_3V3;
Kishon Vijay Abraham I73897ed2018-01-30 16:01:32 +0100425 break;
Jean-Jacques Hiblot7a41bb42018-01-30 16:01:46 +0100426 case MMC_SIGNAL_VOLTAGE_180:
427 hctl |= SDVS_1V8;
428 ac12 |= AC12_V1V8_SIGEN;
Kishon Vijay Abraham I73897ed2018-01-30 16:01:32 +0100429 break;
430 }
431
Jean-Jacques Hiblot7a41bb42018-01-30 16:01:46 +0100432 writel(hctl, &mmc_base->hctl);
433 writel(ac12, &mmc_base->ac12);
434}
435
Sam Protsenkodb174c62019-08-14 22:52:51 +0300436static int omap_hsmmc_wait_dat0(struct udevice *dev, int state, int timeout_us)
Jean-Jacques Hiblot7a41bb42018-01-30 16:01:46 +0100437{
438 int ret = -ETIMEDOUT;
439 u32 con;
440 bool dat0_high;
441 bool target_dat0_high = !!state;
442 struct omap_hsmmc_data *priv = dev_get_priv(dev);
443 struct hsmmc *mmc_base = priv->base_addr;
444
445 con = readl(&mmc_base->con);
446 writel(con | CON_CLKEXTFREE | CON_PADEN, &mmc_base->con);
447
Sam Protsenkodb174c62019-08-14 22:52:51 +0300448 timeout_us = DIV_ROUND_UP(timeout_us, 10); /* check every 10 us. */
449 while (timeout_us--) {
Jean-Jacques Hiblot7a41bb42018-01-30 16:01:46 +0100450 dat0_high = !!(readl(&mmc_base->pstate) & PSTATE_DLEV_DAT0);
451 if (dat0_high == target_dat0_high) {
452 ret = 0;
453 break;
454 }
455 udelay(10);
456 }
457 writel(con, &mmc_base->con);
458
459 return ret;
460}
Jean-Jacques Hiblot7a41bb42018-01-30 16:01:46 +0100461
462#if CONFIG_IS_ENABLED(MMC_IO_VOLTAGE)
463#if CONFIG_IS_ENABLED(DM_REGULATOR)
464static int omap_hsmmc_set_io_regulator(struct mmc *mmc, int mV)
465{
466 int ret = 0;
467 int uV = mV * 1000;
468
469 struct omap_hsmmc_data *priv = omap_hsmmc_get_data(mmc);
470
471 if (!mmc->vqmmc_supply)
472 return 0;
473
474 /* Disable PBIAS */
Lokesh Vutlab2691972019-01-11 15:15:52 +0530475 ret = regulator_set_enable_if_allowed(priv->pbias_supply, false);
476 if (ret)
Jean-Jacques Hiblot7a41bb42018-01-30 16:01:46 +0100477 return ret;
478
479 /* Turn off IO voltage */
Lokesh Vutlab2691972019-01-11 15:15:52 +0530480 ret = regulator_set_enable_if_allowed(mmc->vqmmc_supply, false);
481 if (ret)
Jean-Jacques Hiblot7a41bb42018-01-30 16:01:46 +0100482 return ret;
483 /* Program a new IO voltage value */
484 ret = regulator_set_value(mmc->vqmmc_supply, uV);
485 if (ret)
486 return ret;
487 /* Turn on IO voltage */
Lokesh Vutlab2691972019-01-11 15:15:52 +0530488 ret = regulator_set_enable_if_allowed(mmc->vqmmc_supply, true);
489 if (ret)
Jean-Jacques Hiblot7a41bb42018-01-30 16:01:46 +0100490 return ret;
491
492 /* Program PBIAS voltage*/
493 ret = regulator_set_value(priv->pbias_supply, uV);
494 if (ret && ret != -ENOSYS)
495 return ret;
496 /* Enable PBIAS */
Lokesh Vutlab2691972019-01-11 15:15:52 +0530497 ret = regulator_set_enable_if_allowed(priv->pbias_supply, true);
498 if (ret)
Jean-Jacques Hiblot7a41bb42018-01-30 16:01:46 +0100499 return ret;
500
501 return 0;
Kishon Vijay Abraham I73897ed2018-01-30 16:01:32 +0100502}
Jean-Jacques Hiblot7a41bb42018-01-30 16:01:46 +0100503#endif
Kishon Vijay Abraham I73897ed2018-01-30 16:01:32 +0100504
Jean-Jacques Hiblot7a41bb42018-01-30 16:01:46 +0100505static int omap_hsmmc_set_signal_voltage(struct mmc *mmc)
506{
507 struct omap_hsmmc_data *priv = omap_hsmmc_get_data(mmc);
508 struct hsmmc *mmc_base = priv->base_addr;
509 int mv = mmc_voltage_to_mv(mmc->signal_voltage);
510 u32 capa_mask;
511 __maybe_unused u8 palmas_ldo_volt;
512 u32 val;
513
514 if (mv < 0)
515 return -EINVAL;
516
517 if (mmc->signal_voltage == MMC_SIGNAL_VOLTAGE_330) {
Faiz Abbasfc1ad622019-04-05 14:18:46 +0530518 mv = 3300;
519 capa_mask = VS33_3V3SUP;
520 palmas_ldo_volt = LDO_VOLT_3V3;
Jean-Jacques Hiblot7a41bb42018-01-30 16:01:46 +0100521 } else if (mmc->signal_voltage == MMC_SIGNAL_VOLTAGE_180) {
522 capa_mask = VS18_1V8SUP;
523 palmas_ldo_volt = LDO_VOLT_1V8;
524 } else {
525 return -EOPNOTSUPP;
526 }
527
528 val = readl(&mmc_base->capa);
529 if (!(val & capa_mask))
530 return -EOPNOTSUPP;
531
532 priv->signal_voltage = mmc->signal_voltage;
533
534 omap_hsmmc_conf_bus_power(mmc, mmc->signal_voltage);
535
536#if CONFIG_IS_ENABLED(DM_REGULATOR)
537 return omap_hsmmc_set_io_regulator(mmc, mv);
538#elif (defined(CONFIG_OMAP54XX) || defined(CONFIG_OMAP44XX)) && \
539 defined(CONFIG_PALMAS_POWER)
540 if (mmc_get_blk_desc(mmc)->devnum == 0)
541 vmmc_pbias_config(palmas_ldo_volt);
542 return 0;
543#else
544 return 0;
545#endif
546}
547#endif
548
549static uint32_t omap_hsmmc_set_capabilities(struct mmc *mmc)
Kishon Vijay Abraham I73897ed2018-01-30 16:01:32 +0100550{
551 struct hsmmc *mmc_base;
552 struct omap_hsmmc_data *priv = omap_hsmmc_get_data(mmc);
553 u32 val;
554
555 mmc_base = priv->base_addr;
556 val = readl(&mmc_base->capa);
557
558 if (priv->controller_flags & OMAP_HSMMC_SUPPORTS_DUAL_VOLT) {
Faiz Abbasfc1ad622019-04-05 14:18:46 +0530559 val |= (VS33_3V3SUP | VS18_1V8SUP);
Kishon Vijay Abraham I73897ed2018-01-30 16:01:32 +0100560 } else if (priv->controller_flags & OMAP_HSMMC_NO_1_8_V) {
Faiz Abbasfc1ad622019-04-05 14:18:46 +0530561 val |= VS33_3V3SUP;
Kishon Vijay Abraham I73897ed2018-01-30 16:01:32 +0100562 val &= ~VS18_1V8SUP;
Kishon Vijay Abraham I73897ed2018-01-30 16:01:32 +0100563 } else {
564 val |= VS18_1V8SUP;
Faiz Abbasfc1ad622019-04-05 14:18:46 +0530565 val &= ~VS33_3V3SUP;
Kishon Vijay Abraham I73897ed2018-01-30 16:01:32 +0100566 }
567
568 writel(val, &mmc_base->capa);
Jean-Jacques Hiblot7a41bb42018-01-30 16:01:46 +0100569
570 return val;
Kishon Vijay Abraham I73897ed2018-01-30 16:01:32 +0100571}
Jean-Jacques Hiblotf0f821b2018-01-30 16:01:35 +0100572
573#ifdef MMC_SUPPORTS_TUNING
574static void omap_hsmmc_disable_tuning(struct mmc *mmc)
575{
576 struct hsmmc *mmc_base;
577 struct omap_hsmmc_data *priv = omap_hsmmc_get_data(mmc);
578 u32 val;
579
580 mmc_base = priv->base_addr;
581 val = readl(&mmc_base->ac12);
582 val &= ~(AC12_SCLK_SEL);
583 writel(val, &mmc_base->ac12);
584
585 val = readl(&mmc_base->dll);
586 val &= ~(DLL_FORCE_VALUE | DLL_SWT);
587 writel(val, &mmc_base->dll);
588}
589
590static void omap_hsmmc_set_dll(struct mmc *mmc, int count)
591{
592 int i;
593 struct hsmmc *mmc_base;
594 struct omap_hsmmc_data *priv = omap_hsmmc_get_data(mmc);
595 u32 val;
596
597 mmc_base = priv->base_addr;
598 val = readl(&mmc_base->dll);
599 val |= DLL_FORCE_VALUE;
600 val &= ~(DLL_FORCE_SR_C_MASK << DLL_FORCE_SR_C_SHIFT);
601 val |= (count << DLL_FORCE_SR_C_SHIFT);
602 writel(val, &mmc_base->dll);
603
604 val |= DLL_CALIB;
605 writel(val, &mmc_base->dll);
606 for (i = 0; i < 1000; i++) {
607 if (readl(&mmc_base->dll) & DLL_CALIB)
608 break;
609 }
610 val &= ~DLL_CALIB;
611 writel(val, &mmc_base->dll);
612}
613
614static int omap_hsmmc_execute_tuning(struct udevice *dev, uint opcode)
615{
616 struct omap_hsmmc_data *priv = dev_get_priv(dev);
617 struct mmc_uclass_priv *upriv = dev_get_uclass_priv(dev);
618 struct mmc *mmc = upriv->mmc;
619 struct hsmmc *mmc_base;
620 u32 val;
621 u8 cur_match, prev_match = 0;
622 int ret;
623 u32 phase_delay = 0;
624 u32 start_window = 0, max_window = 0;
625 u32 length = 0, max_len = 0;
Faiz Abbase4d30562019-01-30 18:08:42 +0530626 bool single_point_failure = false;
627 struct udevice *thermal_dev;
628 int temperature;
629 int i;
Jean-Jacques Hiblotf0f821b2018-01-30 16:01:35 +0100630
631 mmc_base = priv->base_addr;
632 val = readl(&mmc_base->capa2);
633
634 /* clock tuning is not needed for upto 52MHz */
635 if (!((mmc->selected_mode == MMC_HS_200) ||
636 (mmc->selected_mode == UHS_SDR104) ||
637 ((mmc->selected_mode == UHS_SDR50) && (val & CAPA2_TSDR50))))
638 return 0;
639
Faiz Abbase4d30562019-01-30 18:08:42 +0530640 ret = uclass_first_device(UCLASS_THERMAL, &thermal_dev);
641 if (ret) {
642 printf("Couldn't get thermal device for tuning\n");
643 return ret;
644 }
645 ret = thermal_get_temp(thermal_dev, &temperature);
646 if (ret) {
647 printf("Couldn't get temperature for tuning\n");
648 return ret;
649 }
Jean-Jacques Hiblotf0f821b2018-01-30 16:01:35 +0100650 val = readl(&mmc_base->dll);
651 val |= DLL_SWT;
652 writel(val, &mmc_base->dll);
Faiz Abbase4d30562019-01-30 18:08:42 +0530653
654 /*
655 * Stage 1: Search for a maximum pass window ignoring any
656 * any single point failures. If the tuning value ends up
657 * near it, move away from it in stage 2 below
658 */
Jean-Jacques Hiblotf0f821b2018-01-30 16:01:35 +0100659 while (phase_delay <= MAX_PHASE_DELAY) {
660 omap_hsmmc_set_dll(mmc, phase_delay);
661
662 cur_match = !mmc_send_tuning(mmc, opcode, NULL);
663
664 if (cur_match) {
665 if (prev_match) {
666 length++;
Faiz Abbase4d30562019-01-30 18:08:42 +0530667 } else if (single_point_failure) {
668 /* ignore single point failure */
669 length++;
670 single_point_failure = false;
Jean-Jacques Hiblotf0f821b2018-01-30 16:01:35 +0100671 } else {
672 start_window = phase_delay;
673 length = 1;
674 }
Faiz Abbase4d30562019-01-30 18:08:42 +0530675 } else {
676 single_point_failure = prev_match;
Jean-Jacques Hiblotf0f821b2018-01-30 16:01:35 +0100677 }
678
679 if (length > max_len) {
680 max_window = start_window;
681 max_len = length;
682 }
683
684 prev_match = cur_match;
685 phase_delay += 4;
686 }
687
688 if (!max_len) {
689 ret = -EIO;
690 goto tuning_error;
691 }
692
693 val = readl(&mmc_base->ac12);
694 if (!(val & AC12_SCLK_SEL)) {
695 ret = -EIO;
696 goto tuning_error;
697 }
Faiz Abbase4d30562019-01-30 18:08:42 +0530698 /*
699 * Assign tuning value as a ratio of maximum pass window based
700 * on temperature
701 */
702 if (temperature < -20000)
703 phase_delay = min(max_window + 4 * max_len - 24,
704 max_window +
705 DIV_ROUND_UP(13 * max_len, 16) * 4);
706 else if (temperature < 20000)
707 phase_delay = max_window + DIV_ROUND_UP(9 * max_len, 16) * 4;
708 else if (temperature < 40000)
709 phase_delay = max_window + DIV_ROUND_UP(8 * max_len, 16) * 4;
710 else if (temperature < 70000)
711 phase_delay = max_window + DIV_ROUND_UP(7 * max_len, 16) * 4;
712 else if (temperature < 90000)
713 phase_delay = max_window + DIV_ROUND_UP(5 * max_len, 16) * 4;
714 else if (temperature < 120000)
715 phase_delay = max_window + DIV_ROUND_UP(4 * max_len, 16) * 4;
716 else
717 phase_delay = max_window + DIV_ROUND_UP(3 * max_len, 16) * 4;
718
719 /*
720 * Stage 2: Search for a single point failure near the chosen tuning
721 * value in two steps. First in the +3 to +10 range and then in the
722 * +2 to -10 range. If found, move away from it in the appropriate
723 * direction by the appropriate amount depending on the temperature.
724 */
725 for (i = 3; i <= 10; i++) {
726 omap_hsmmc_set_dll(mmc, phase_delay + i);
727 if (mmc_send_tuning(mmc, opcode, NULL)) {
728 if (temperature < 10000)
729 phase_delay += i + 6;
730 else if (temperature < 20000)
731 phase_delay += i - 12;
732 else if (temperature < 70000)
733 phase_delay += i - 8;
734 else if (temperature < 90000)
735 phase_delay += i - 6;
736 else
737 phase_delay += i - 6;
738
739 goto single_failure_found;
740 }
741 }
742
743 for (i = 2; i >= -10; i--) {
744 omap_hsmmc_set_dll(mmc, phase_delay + i);
745 if (mmc_send_tuning(mmc, opcode, NULL)) {
746 if (temperature < 10000)
747 phase_delay += i + 12;
748 else if (temperature < 20000)
749 phase_delay += i + 8;
750 else if (temperature < 70000)
751 phase_delay += i + 8;
752 else if (temperature < 90000)
753 phase_delay += i + 10;
754 else
755 phase_delay += i + 12;
756
757 goto single_failure_found;
758 }
759 }
760
761single_failure_found:
Jean-Jacques Hiblotf0f821b2018-01-30 16:01:35 +0100762
Jean-Jacques Hiblotf0f821b2018-01-30 16:01:35 +0100763 omap_hsmmc_set_dll(mmc, phase_delay);
764
765 mmc_reset_controller_fsm(mmc_base, SYSCTL_SRD);
766 mmc_reset_controller_fsm(mmc_base, SYSCTL_SRC);
767
768 return 0;
769
770tuning_error:
771
772 omap_hsmmc_disable_tuning(mmc);
773 mmc_reset_controller_fsm(mmc_base, SYSCTL_SRD);
774 mmc_reset_controller_fsm(mmc_base, SYSCTL_SRC);
775
776 return ret;
777}
778#endif
Kishon Vijay Abraham I73897ed2018-01-30 16:01:32 +0100779#endif
Sukumar Ghoraic53f5e52010-09-18 20:32:33 -0700780
Jean-Jacques Hiblota420d7d2018-01-30 16:01:36 +0100781static void mmc_enable_irq(struct mmc *mmc, struct mmc_cmd *cmd)
782{
783 struct omap_hsmmc_data *priv = omap_hsmmc_get_data(mmc);
784 struct hsmmc *mmc_base = priv->base_addr;
785 u32 irq_mask = INT_EN_MASK;
786
787 /*
788 * TODO: Errata i802 indicates only DCRC interrupts can occur during
789 * tuning procedure and DCRC should be disabled. But see occurences
790 * of DEB, CIE, CEB, CCRC interupts during tuning procedure. These
791 * interrupts occur along with BRR, so the data is actually in the
792 * buffer. It has to be debugged why these interrutps occur
793 */
794 if (cmd && mmc_is_tuning_cmd(cmd->cmdidx))
795 irq_mask &= ~(IE_DEB | IE_DCRC | IE_CIE | IE_CEB | IE_CCRC);
796
797 writel(irq_mask, &mmc_base->ie);
798}
799
Pantelis Antoniouc9e75912014-02-26 19:28:45 +0200800static int omap_hsmmc_init_setup(struct mmc *mmc)
Sukumar Ghoraic53f5e52010-09-18 20:32:33 -0700801{
Jean-Jacques Hiblotd58ef8e2017-03-22 16:00:31 +0100802 struct omap_hsmmc_data *priv = omap_hsmmc_get_data(mmc);
Nikita Kiryanov13822862012-12-03 02:19:43 +0000803 struct hsmmc *mmc_base;
Sukumar Ghoraic53f5e52010-09-18 20:32:33 -0700804 unsigned int reg_val;
805 unsigned int dsor;
Nishanth Menond3bfaac2010-11-19 11:18:12 -0500806 ulong start;
Sukumar Ghoraic53f5e52010-09-18 20:32:33 -0700807
Jean-Jacques Hiblotd58ef8e2017-03-22 16:00:31 +0100808 mmc_base = priv->base_addr;
Balaji T Kf843d332011-09-08 06:34:57 +0000809 mmc_board_init(mmc);
Sukumar Ghoraic53f5e52010-09-18 20:32:33 -0700810
811 writel(readl(&mmc_base->sysconfig) | MMC_SOFTRESET,
812 &mmc_base->sysconfig);
Nishanth Menond3bfaac2010-11-19 11:18:12 -0500813 start = get_timer(0);
814 while ((readl(&mmc_base->sysstatus) & RESETDONE) == 0) {
815 if (get_timer(0) - start > MAX_RETRY_MS) {
816 printf("%s: timedout waiting for cc2!\n", __func__);
Jaehoon Chung7825d202016-07-19 16:33:36 +0900817 return -ETIMEDOUT;
Nishanth Menond3bfaac2010-11-19 11:18:12 -0500818 }
819 }
Sukumar Ghoraic53f5e52010-09-18 20:32:33 -0700820 writel(readl(&mmc_base->sysctl) | SOFTRESETALL, &mmc_base->sysctl);
Nishanth Menond3bfaac2010-11-19 11:18:12 -0500821 start = get_timer(0);
822 while ((readl(&mmc_base->sysctl) & SOFTRESETALL) != 0x0) {
823 if (get_timer(0) - start > MAX_RETRY_MS) {
824 printf("%s: timedout waiting for softresetall!\n",
825 __func__);
Jaehoon Chung7825d202016-07-19 16:33:36 +0900826 return -ETIMEDOUT;
Nishanth Menond3bfaac2010-11-19 11:18:12 -0500827 }
828 }
Jean-Jacques Hiblotcebf0592018-02-23 10:40:18 +0100829#ifdef CONFIG_MMC_OMAP_HS_ADMA
Kishon Vijay Abraham I826be2a2017-09-21 16:51:34 +0200830 reg_val = readl(&mmc_base->hl_hwinfo);
831 if (reg_val & MADMA_EN)
832 priv->controller_flags |= OMAP_HSMMC_USE_ADMA;
833#endif
Kishon Vijay Abraham I73897ed2018-01-30 16:01:32 +0100834
835#if CONFIG_IS_ENABLED(DM_MMC)
Jean-Jacques Hiblot7a41bb42018-01-30 16:01:46 +0100836 reg_val = omap_hsmmc_set_capabilities(mmc);
Faiz Abbasfc1ad622019-04-05 14:18:46 +0530837 omap_hsmmc_conf_bus_power(mmc, (reg_val & VS33_3V3SUP) ?
Jean-Jacques Hiblot7a41bb42018-01-30 16:01:46 +0100838 MMC_SIGNAL_VOLTAGE_330 : MMC_SIGNAL_VOLTAGE_180);
Kishon Vijay Abraham I73897ed2018-01-30 16:01:32 +0100839#else
Sukumar Ghoraic53f5e52010-09-18 20:32:33 -0700840 writel(DTW_1_BITMODE | SDBP_PWROFF | SDVS_3V0, &mmc_base->hctl);
Faiz Abbasfc1ad622019-04-05 14:18:46 +0530841 writel(readl(&mmc_base->capa) | VS33_3V3SUP | VS18_1V8SUP,
Sukumar Ghoraic53f5e52010-09-18 20:32:33 -0700842 &mmc_base->capa);
Kishon Vijay Abraham I73897ed2018-01-30 16:01:32 +0100843#endif
Sukumar Ghoraic53f5e52010-09-18 20:32:33 -0700844
845 reg_val = readl(&mmc_base->con) & RESERVED_MASK;
846
847 writel(CTPL_MMC_SD | reg_val | WPP_ACTIVEHIGH | CDP_ACTIVEHIGH |
848 MIT_CTO | DW8_1_4BITMODE | MODE_FUNC | STR_BLOCK |
849 HR_NOHOSTRESP | INIT_NOINIT | NOOPENDRAIN, &mmc_base->con);
850
851 dsor = 240;
852 mmc_reg_out(&mmc_base->sysctl, (ICE_MASK | DTO_MASK | CEN_MASK),
Kishon Vijay Abraham I6e543812017-09-21 16:51:36 +0200853 (ICE_STOP | DTO_15THDTO));
Sukumar Ghoraic53f5e52010-09-18 20:32:33 -0700854 mmc_reg_out(&mmc_base->sysctl, ICE_MASK | CLKD_MASK,
855 (dsor << CLKD_OFFSET) | ICE_OSCILLATE);
Nishanth Menond3bfaac2010-11-19 11:18:12 -0500856 start = get_timer(0);
857 while ((readl(&mmc_base->sysctl) & ICS_MASK) == ICS_NOTREADY) {
858 if (get_timer(0) - start > MAX_RETRY_MS) {
859 printf("%s: timedout waiting for ics!\n", __func__);
Jaehoon Chung7825d202016-07-19 16:33:36 +0900860 return -ETIMEDOUT;
Nishanth Menond3bfaac2010-11-19 11:18:12 -0500861 }
862 }
Sukumar Ghoraic53f5e52010-09-18 20:32:33 -0700863 writel(readl(&mmc_base->sysctl) | CEN_ENABLE, &mmc_base->sysctl);
864
865 writel(readl(&mmc_base->hctl) | SDBP_PWRON, &mmc_base->hctl);
866
Jean-Jacques Hiblota420d7d2018-01-30 16:01:36 +0100867 mmc_enable_irq(mmc, NULL);
Jean-Jacques Hiblot20157d42018-01-30 16:01:44 +0100868
869#if !CONFIG_IS_ENABLED(DM_MMC)
Sukumar Ghoraic53f5e52010-09-18 20:32:33 -0700870 mmc_init_stream(mmc_base);
Jean-Jacques Hiblot20157d42018-01-30 16:01:44 +0100871#endif
Sukumar Ghoraic53f5e52010-09-18 20:32:33 -0700872
873 return 0;
874}
875
Grazvydas Ignotasddde1882012-03-19 12:12:06 +0000876/*
877 * MMC controller internal finite state machine reset
878 *
879 * Used to reset command or data internal state machines, using respectively
880 * SRC or SRD bit of SYSCTL register
881 */
882static void mmc_reset_controller_fsm(struct hsmmc *mmc_base, u32 bit)
883{
884 ulong start;
885
886 mmc_reg_out(&mmc_base->sysctl, bit, bit);
887
Oleksandr Tyshchenko06640ca2013-08-06 13:44:16 +0300888 /*
889 * CMD(DAT) lines reset procedures are slightly different
890 * for OMAP3 and OMAP4(AM335x,OMAP5,DRA7xx).
891 * According to OMAP3 TRM:
892 * Set SRC(SRD) bit in MMCHS_SYSCTL register to 0x1 and wait until it
893 * returns to 0x0.
894 * According to OMAP4(AM335x,OMAP5,DRA7xx) TRMs, CMD(DATA) lines reset
895 * procedure steps must be as follows:
896 * 1. Initiate CMD(DAT) line reset by writing 0x1 to SRC(SRD) bit in
897 * MMCHS_SYSCTL register (SD_SYSCTL for AM335x).
898 * 2. Poll the SRC(SRD) bit until it is set to 0x1.
899 * 3. Wait until the SRC (SRD) bit returns to 0x0
900 * (reset procedure is completed).
901 */
902#if defined(CONFIG_OMAP44XX) || defined(CONFIG_OMAP54XX) || \
Nikita Kiryanov5ffdd852015-07-30 23:56:20 +0300903 defined(CONFIG_AM33XX) || defined(CONFIG_AM43XX)
Oleksandr Tyshchenko06640ca2013-08-06 13:44:16 +0300904 if (!(readl(&mmc_base->sysctl) & bit)) {
905 start = get_timer(0);
906 while (!(readl(&mmc_base->sysctl) & bit)) {
Jean-Jacques Hiblot192e4302018-01-30 16:01:37 +0100907 if (get_timer(0) - start > MMC_TIMEOUT_MS)
Oleksandr Tyshchenko06640ca2013-08-06 13:44:16 +0300908 return;
909 }
910 }
911#endif
Grazvydas Ignotasddde1882012-03-19 12:12:06 +0000912 start = get_timer(0);
913 while ((readl(&mmc_base->sysctl) & bit) != 0) {
914 if (get_timer(0) - start > MAX_RETRY_MS) {
915 printf("%s: timedout waiting for sysctl %x to clear\n",
916 __func__, bit);
917 return;
918 }
919 }
920}
Kishon Vijay Abraham I826be2a2017-09-21 16:51:34 +0200921
Jean-Jacques Hiblotcebf0592018-02-23 10:40:18 +0100922#ifdef CONFIG_MMC_OMAP_HS_ADMA
Kishon Vijay Abraham I826be2a2017-09-21 16:51:34 +0200923static void omap_hsmmc_adma_desc(struct mmc *mmc, char *buf, u16 len, bool end)
924{
925 struct omap_hsmmc_data *priv = omap_hsmmc_get_data(mmc);
926 struct omap_hsmmc_adma_desc *desc;
927 u8 attr;
928
929 desc = &priv->adma_desc_table[priv->desc_slot];
930
931 attr = ADMA_DESC_ATTR_VALID | ADMA_DESC_TRANSFER_DATA;
932 if (!end)
933 priv->desc_slot++;
934 else
935 attr |= ADMA_DESC_ATTR_END;
936
937 desc->len = len;
938 desc->addr = (u32)buf;
939 desc->reserved = 0;
940 desc->attr = attr;
941}
942
943static void omap_hsmmc_prepare_adma_table(struct mmc *mmc,
944 struct mmc_data *data)
945{
946 uint total_len = data->blocksize * data->blocks;
947 uint desc_count = DIV_ROUND_UP(total_len, ADMA_MAX_LEN);
948 struct omap_hsmmc_data *priv = omap_hsmmc_get_data(mmc);
949 int i = desc_count;
950 char *buf;
951
952 priv->desc_slot = 0;
953 priv->adma_desc_table = (struct omap_hsmmc_adma_desc *)
954 memalign(ARCH_DMA_MINALIGN, desc_count *
955 sizeof(struct omap_hsmmc_adma_desc));
956
957 if (data->flags & MMC_DATA_READ)
958 buf = data->dest;
959 else
960 buf = (char *)data->src;
961
962 while (--i) {
963 omap_hsmmc_adma_desc(mmc, buf, ADMA_MAX_LEN, false);
964 buf += ADMA_MAX_LEN;
965 total_len -= ADMA_MAX_LEN;
966 }
967
968 omap_hsmmc_adma_desc(mmc, buf, total_len, true);
969
970 flush_dcache_range((long)priv->adma_desc_table,
971 (long)priv->adma_desc_table +
972 ROUND(desc_count *
973 sizeof(struct omap_hsmmc_adma_desc),
974 ARCH_DMA_MINALIGN));
975}
976
977static void omap_hsmmc_prepare_data(struct mmc *mmc, struct mmc_data *data)
978{
979 struct hsmmc *mmc_base;
980 struct omap_hsmmc_data *priv = omap_hsmmc_get_data(mmc);
981 u32 val;
982 char *buf;
983
984 mmc_base = priv->base_addr;
985 omap_hsmmc_prepare_adma_table(mmc, data);
986
987 if (data->flags & MMC_DATA_READ)
988 buf = data->dest;
989 else
990 buf = (char *)data->src;
991
992 val = readl(&mmc_base->hctl);
993 val |= DMA_SELECT;
994 writel(val, &mmc_base->hctl);
995
996 val = readl(&mmc_base->con);
997 val |= DMA_MASTER;
998 writel(val, &mmc_base->con);
999
1000 writel((u32)priv->adma_desc_table, &mmc_base->admasal);
1001
1002 flush_dcache_range((u32)buf,
1003 (u32)buf +
1004 ROUND(data->blocksize * data->blocks,
1005 ARCH_DMA_MINALIGN));
1006}
1007
1008static void omap_hsmmc_dma_cleanup(struct mmc *mmc)
1009{
1010 struct hsmmc *mmc_base;
1011 struct omap_hsmmc_data *priv = omap_hsmmc_get_data(mmc);
1012 u32 val;
1013
1014 mmc_base = priv->base_addr;
1015
1016 val = readl(&mmc_base->con);
1017 val &= ~DMA_MASTER;
1018 writel(val, &mmc_base->con);
1019
1020 val = readl(&mmc_base->hctl);
1021 val &= ~DMA_SELECT;
1022 writel(val, &mmc_base->hctl);
1023
1024 kfree(priv->adma_desc_table);
1025}
1026#else
1027#define omap_hsmmc_adma_desc
1028#define omap_hsmmc_prepare_adma_table
1029#define omap_hsmmc_prepare_data
1030#define omap_hsmmc_dma_cleanup
1031#endif
1032
Simon Glass5f4bd8c2017-07-04 13:31:19 -06001033#if !CONFIG_IS_ENABLED(DM_MMC)
Pantelis Antoniouc9e75912014-02-26 19:28:45 +02001034static int omap_hsmmc_send_cmd(struct mmc *mmc, struct mmc_cmd *cmd,
Sukumar Ghoraic53f5e52010-09-18 20:32:33 -07001035 struct mmc_data *data)
1036{
Jean-Jacques Hiblotd58ef8e2017-03-22 16:00:31 +01001037 struct omap_hsmmc_data *priv = omap_hsmmc_get_data(mmc);
Jean-Jacques Hiblot8fc9d3a2017-04-14 19:50:02 +02001038#else
1039static int omap_hsmmc_send_cmd(struct udevice *dev, struct mmc_cmd *cmd,
1040 struct mmc_data *data)
1041{
1042 struct omap_hsmmc_data *priv = dev_get_priv(dev);
Kishon Vijay Abraham I826be2a2017-09-21 16:51:34 +02001043 struct mmc_uclass_priv *upriv = dev_get_uclass_priv(dev);
1044 struct mmc *mmc = upriv->mmc;
1045#endif
Nikita Kiryanov13822862012-12-03 02:19:43 +00001046 struct hsmmc *mmc_base;
Sukumar Ghoraic53f5e52010-09-18 20:32:33 -07001047 unsigned int flags, mmc_stat;
Nishanth Menond3bfaac2010-11-19 11:18:12 -05001048 ulong start;
Jean-Jacques Hiblot7a41bb42018-01-30 16:01:46 +01001049 priv->last_cmd = cmd->cmdidx;
Sukumar Ghoraic53f5e52010-09-18 20:32:33 -07001050
Jean-Jacques Hiblotd58ef8e2017-03-22 16:00:31 +01001051 mmc_base = priv->base_addr;
Kishon Vijay Abraham I316e7ae2017-09-21 16:51:35 +02001052
1053 if (cmd->cmdidx == MMC_CMD_STOP_TRANSMISSION)
1054 return 0;
1055
Nishanth Menond3bfaac2010-11-19 11:18:12 -05001056 start = get_timer(0);
Tom Rini32ec3252012-01-30 11:22:25 +00001057 while ((readl(&mmc_base->pstate) & (DATI_MASK | CMDI_MASK)) != 0) {
Nishanth Menond3bfaac2010-11-19 11:18:12 -05001058 if (get_timer(0) - start > MAX_RETRY_MS) {
Tom Rini32ec3252012-01-30 11:22:25 +00001059 printf("%s: timedout waiting on cmd inhibit to clear\n",
1060 __func__);
Jean-Jacques Hiblota1e7a4d2019-07-02 10:53:48 +02001061 mmc_reset_controller_fsm(mmc_base, SYSCTL_SRD);
1062 mmc_reset_controller_fsm(mmc_base, SYSCTL_SRC);
Jaehoon Chung7825d202016-07-19 16:33:36 +09001063 return -ETIMEDOUT;
Nishanth Menond3bfaac2010-11-19 11:18:12 -05001064 }
1065 }
Sukumar Ghoraic53f5e52010-09-18 20:32:33 -07001066 writel(0xFFFFFFFF, &mmc_base->stat);
Jean-Jacques Hiblota1e7a4d2019-07-02 10:53:48 +02001067 if (readl(&mmc_base->stat)) {
1068 mmc_reset_controller_fsm(mmc_base, SYSCTL_SRD);
1069 mmc_reset_controller_fsm(mmc_base, SYSCTL_SRC);
Nishanth Menond3bfaac2010-11-19 11:18:12 -05001070 }
Jean-Jacques Hiblota1e7a4d2019-07-02 10:53:48 +02001071
Sukumar Ghoraic53f5e52010-09-18 20:32:33 -07001072 /*
1073 * CMDREG
1074 * CMDIDX[13:8] : Command index
1075 * DATAPRNT[5] : Data Present Select
1076 * ENCMDIDX[4] : Command Index Check Enable
1077 * ENCMDCRC[3] : Command CRC Check Enable
1078 * RSPTYP[1:0]
1079 * 00 = No Response
1080 * 01 = Length 136
1081 * 10 = Length 48
1082 * 11 = Length 48 Check busy after response
1083 */
1084 /* Delay added before checking the status of frq change
1085 * retry not supported by mmc.c(core file)
1086 */
1087 if (cmd->cmdidx == SD_CMD_APP_SEND_SCR)
1088 udelay(50000); /* wait 50 ms */
1089
1090 if (!(cmd->resp_type & MMC_RSP_PRESENT))
1091 flags = 0;
1092 else if (cmd->resp_type & MMC_RSP_136)
1093 flags = RSP_TYPE_LGHT136 | CICE_NOCHECK;
1094 else if (cmd->resp_type & MMC_RSP_BUSY)
1095 flags = RSP_TYPE_LGHT48B;
1096 else
1097 flags = RSP_TYPE_LGHT48;
1098
1099 /* enable default flags */
1100 flags = flags | (CMD_TYPE_NORMAL | CICE_NOCHECK | CCCE_NOCHECK |
Kishon Vijay Abraham I6e543812017-09-21 16:51:36 +02001101 MSBS_SGLEBLK);
1102 flags &= ~(ACEN_ENABLE | BCE_ENABLE | DE_ENABLE);
Sukumar Ghoraic53f5e52010-09-18 20:32:33 -07001103
1104 if (cmd->resp_type & MMC_RSP_CRC)
1105 flags |= CCCE_CHECK;
1106 if (cmd->resp_type & MMC_RSP_OPCODE)
1107 flags |= CICE_CHECK;
1108
1109 if (data) {
1110 if ((cmd->cmdidx == MMC_CMD_READ_MULTIPLE_BLOCK) ||
1111 (cmd->cmdidx == MMC_CMD_WRITE_MULTIPLE_BLOCK)) {
Kishon Vijay Abraham I316e7ae2017-09-21 16:51:35 +02001112 flags |= (MSBS_MULTIBLK | BCE_ENABLE | ACEN_ENABLE);
Sukumar Ghoraic53f5e52010-09-18 20:32:33 -07001113 data->blocksize = 512;
1114 writel(data->blocksize | (data->blocks << 16),
1115 &mmc_base->blk);
1116 } else
1117 writel(data->blocksize | NBLK_STPCNT, &mmc_base->blk);
1118
1119 if (data->flags & MMC_DATA_READ)
1120 flags |= (DP_DATA | DDIR_READ);
1121 else
1122 flags |= (DP_DATA | DDIR_WRITE);
Kishon Vijay Abraham I826be2a2017-09-21 16:51:34 +02001123
Jean-Jacques Hiblotcebf0592018-02-23 10:40:18 +01001124#ifdef CONFIG_MMC_OMAP_HS_ADMA
Kishon Vijay Abraham I826be2a2017-09-21 16:51:34 +02001125 if ((priv->controller_flags & OMAP_HSMMC_USE_ADMA) &&
1126 !mmc_is_tuning_cmd(cmd->cmdidx)) {
1127 omap_hsmmc_prepare_data(mmc, data);
1128 flags |= DE_ENABLE;
1129 }
1130#endif
Sukumar Ghoraic53f5e52010-09-18 20:32:33 -07001131 }
1132
Jean-Jacques Hiblota420d7d2018-01-30 16:01:36 +01001133 mmc_enable_irq(mmc, cmd);
1134
Sukumar Ghoraic53f5e52010-09-18 20:32:33 -07001135 writel(cmd->cmdarg, &mmc_base->arg);
Lubomir Popov19df4122013-08-14 18:59:18 +03001136 udelay(20); /* To fix "No status update" error on eMMC */
Sukumar Ghoraic53f5e52010-09-18 20:32:33 -07001137 writel((cmd->cmdidx << 24) | flags, &mmc_base->cmd);
1138
Nishanth Menond3bfaac2010-11-19 11:18:12 -05001139 start = get_timer(0);
Sukumar Ghoraic53f5e52010-09-18 20:32:33 -07001140 do {
1141 mmc_stat = readl(&mmc_base->stat);
Kishon Vijay Abraham I826be2a2017-09-21 16:51:34 +02001142 if (get_timer(start) > MAX_RETRY_MS) {
Nishanth Menond3bfaac2010-11-19 11:18:12 -05001143 printf("%s : timeout: No status update\n", __func__);
Jaehoon Chung7825d202016-07-19 16:33:36 +09001144 return -ETIMEDOUT;
Nishanth Menond3bfaac2010-11-19 11:18:12 -05001145 }
1146 } while (!mmc_stat);
Sukumar Ghoraic53f5e52010-09-18 20:32:33 -07001147
Grazvydas Ignotasddde1882012-03-19 12:12:06 +00001148 if ((mmc_stat & IE_CTO) != 0) {
1149 mmc_reset_controller_fsm(mmc_base, SYSCTL_SRC);
Jaehoon Chung7825d202016-07-19 16:33:36 +09001150 return -ETIMEDOUT;
Grazvydas Ignotasddde1882012-03-19 12:12:06 +00001151 } else if ((mmc_stat & ERRI_MASK) != 0)
Sukumar Ghoraic53f5e52010-09-18 20:32:33 -07001152 return -1;
1153
1154 if (mmc_stat & CC_MASK) {
1155 writel(CC_MASK, &mmc_base->stat);
1156 if (cmd->resp_type & MMC_RSP_PRESENT) {
1157 if (cmd->resp_type & MMC_RSP_136) {
1158 /* response type 2 */
1159 cmd->response[3] = readl(&mmc_base->rsp10);
1160 cmd->response[2] = readl(&mmc_base->rsp32);
1161 cmd->response[1] = readl(&mmc_base->rsp54);
1162 cmd->response[0] = readl(&mmc_base->rsp76);
1163 } else
1164 /* response types 1, 1b, 3, 4, 5, 6 */
1165 cmd->response[0] = readl(&mmc_base->rsp10);
1166 }
1167 }
1168
Jean-Jacques Hiblotcebf0592018-02-23 10:40:18 +01001169#ifdef CONFIG_MMC_OMAP_HS_ADMA
Kishon Vijay Abraham I826be2a2017-09-21 16:51:34 +02001170 if ((priv->controller_flags & OMAP_HSMMC_USE_ADMA) && data &&
1171 !mmc_is_tuning_cmd(cmd->cmdidx)) {
1172 u32 sz_mb, timeout;
1173
1174 if (mmc_stat & IE_ADMAE) {
1175 omap_hsmmc_dma_cleanup(mmc);
1176 return -EIO;
1177 }
1178
1179 sz_mb = DIV_ROUND_UP(data->blocksize * data->blocks, 1 << 20);
1180 timeout = sz_mb * DMA_TIMEOUT_PER_MB;
1181 if (timeout < MAX_RETRY_MS)
1182 timeout = MAX_RETRY_MS;
1183
1184 start = get_timer(0);
1185 do {
1186 mmc_stat = readl(&mmc_base->stat);
1187 if (mmc_stat & TC_MASK) {
1188 writel(readl(&mmc_base->stat) | TC_MASK,
1189 &mmc_base->stat);
1190 break;
1191 }
1192 if (get_timer(start) > timeout) {
1193 printf("%s : DMA timeout: No status update\n",
1194 __func__);
1195 return -ETIMEDOUT;
1196 }
1197 } while (1);
1198
1199 omap_hsmmc_dma_cleanup(mmc);
1200 return 0;
1201 }
1202#endif
1203
Sukumar Ghoraic53f5e52010-09-18 20:32:33 -07001204 if (data && (data->flags & MMC_DATA_READ)) {
1205 mmc_read_data(mmc_base, data->dest,
1206 data->blocksize * data->blocks);
1207 } else if (data && (data->flags & MMC_DATA_WRITE)) {
1208 mmc_write_data(mmc_base, data->src,
1209 data->blocksize * data->blocks);
1210 }
1211 return 0;
1212}
1213
Sricharanf72611f2011-11-15 09:49:53 -05001214static int mmc_read_data(struct hsmmc *mmc_base, char *buf, unsigned int size)
Sukumar Ghoraic53f5e52010-09-18 20:32:33 -07001215{
1216 unsigned int *output_buf = (unsigned int *)buf;
1217 unsigned int mmc_stat;
1218 unsigned int count;
1219
1220 /*
1221 * Start Polled Read
1222 */
1223 count = (size > MMCSD_SECTOR_SIZE) ? MMCSD_SECTOR_SIZE : size;
1224 count /= 4;
1225
1226 while (size) {
Nishanth Menond3bfaac2010-11-19 11:18:12 -05001227 ulong start = get_timer(0);
Sukumar Ghoraic53f5e52010-09-18 20:32:33 -07001228 do {
1229 mmc_stat = readl(&mmc_base->stat);
Nishanth Menond3bfaac2010-11-19 11:18:12 -05001230 if (get_timer(0) - start > MAX_RETRY_MS) {
1231 printf("%s: timedout waiting for status!\n",
1232 __func__);
Jaehoon Chung7825d202016-07-19 16:33:36 +09001233 return -ETIMEDOUT;
Nishanth Menond3bfaac2010-11-19 11:18:12 -05001234 }
Sukumar Ghoraic53f5e52010-09-18 20:32:33 -07001235 } while (mmc_stat == 0);
1236
Grazvydas Ignotasddde1882012-03-19 12:12:06 +00001237 if ((mmc_stat & (IE_DTO | IE_DCRC | IE_DEB)) != 0)
1238 mmc_reset_controller_fsm(mmc_base, SYSCTL_SRD);
1239
Sukumar Ghoraic53f5e52010-09-18 20:32:33 -07001240 if ((mmc_stat & ERRI_MASK) != 0)
1241 return 1;
1242
1243 if (mmc_stat & BRR_MASK) {
1244 unsigned int k;
1245
1246 writel(readl(&mmc_base->stat) | BRR_MASK,
1247 &mmc_base->stat);
1248 for (k = 0; k < count; k++) {
1249 *output_buf = readl(&mmc_base->data);
1250 output_buf++;
1251 }
1252 size -= (count*4);
1253 }
1254
1255 if (mmc_stat & BWR_MASK)
1256 writel(readl(&mmc_base->stat) | BWR_MASK,
1257 &mmc_base->stat);
1258
1259 if (mmc_stat & TC_MASK) {
1260 writel(readl(&mmc_base->stat) | TC_MASK,
1261 &mmc_base->stat);
1262 break;
1263 }
1264 }
1265 return 0;
1266}
1267
Jean-Jacques Hiblot98821552018-02-23 10:40:17 +01001268#if CONFIG_IS_ENABLED(MMC_WRITE)
Sricharanf72611f2011-11-15 09:49:53 -05001269static int mmc_write_data(struct hsmmc *mmc_base, const char *buf,
Jean-Jacques Hiblot98821552018-02-23 10:40:17 +01001270 unsigned int size)
Sukumar Ghoraic53f5e52010-09-18 20:32:33 -07001271{
1272 unsigned int *input_buf = (unsigned int *)buf;
1273 unsigned int mmc_stat;
1274 unsigned int count;
1275
1276 /*
Lubomir Popov19df4122013-08-14 18:59:18 +03001277 * Start Polled Write
Sukumar Ghoraic53f5e52010-09-18 20:32:33 -07001278 */
1279 count = (size > MMCSD_SECTOR_SIZE) ? MMCSD_SECTOR_SIZE : size;
1280 count /= 4;
1281
1282 while (size) {
Nishanth Menond3bfaac2010-11-19 11:18:12 -05001283 ulong start = get_timer(0);
Sukumar Ghoraic53f5e52010-09-18 20:32:33 -07001284 do {
1285 mmc_stat = readl(&mmc_base->stat);
Nishanth Menond3bfaac2010-11-19 11:18:12 -05001286 if (get_timer(0) - start > MAX_RETRY_MS) {
1287 printf("%s: timedout waiting for status!\n",
1288 __func__);
Jaehoon Chung7825d202016-07-19 16:33:36 +09001289 return -ETIMEDOUT;
Nishanth Menond3bfaac2010-11-19 11:18:12 -05001290 }
Sukumar Ghoraic53f5e52010-09-18 20:32:33 -07001291 } while (mmc_stat == 0);
1292
Grazvydas Ignotasddde1882012-03-19 12:12:06 +00001293 if ((mmc_stat & (IE_DTO | IE_DCRC | IE_DEB)) != 0)
1294 mmc_reset_controller_fsm(mmc_base, SYSCTL_SRD);
1295
Sukumar Ghoraic53f5e52010-09-18 20:32:33 -07001296 if ((mmc_stat & ERRI_MASK) != 0)
1297 return 1;
1298
1299 if (mmc_stat & BWR_MASK) {
1300 unsigned int k;
1301
1302 writel(readl(&mmc_base->stat) | BWR_MASK,
1303 &mmc_base->stat);
1304 for (k = 0; k < count; k++) {
1305 writel(*input_buf, &mmc_base->data);
1306 input_buf++;
1307 }
1308 size -= (count*4);
1309 }
1310
1311 if (mmc_stat & BRR_MASK)
1312 writel(readl(&mmc_base->stat) | BRR_MASK,
1313 &mmc_base->stat);
1314
1315 if (mmc_stat & TC_MASK) {
1316 writel(readl(&mmc_base->stat) | TC_MASK,
1317 &mmc_base->stat);
1318 break;
1319 }
1320 }
1321 return 0;
1322}
Jean-Jacques Hiblot98821552018-02-23 10:40:17 +01001323#else
1324static int mmc_write_data(struct hsmmc *mmc_base, const char *buf,
1325 unsigned int size)
1326{
1327 return -ENOTSUPP;
1328}
1329#endif
Jean-Jacques Hiblot7fe2f192018-01-30 16:01:30 +01001330static void omap_hsmmc_stop_clock(struct hsmmc *mmc_base)
1331{
1332 writel(readl(&mmc_base->sysctl) & ~CEN_ENABLE, &mmc_base->sysctl);
1333}
1334
1335static void omap_hsmmc_start_clock(struct hsmmc *mmc_base)
1336{
1337 writel(readl(&mmc_base->sysctl) | CEN_ENABLE, &mmc_base->sysctl);
1338}
1339
1340static void omap_hsmmc_set_clock(struct mmc *mmc)
1341{
1342 struct omap_hsmmc_data *priv = omap_hsmmc_get_data(mmc);
1343 struct hsmmc *mmc_base;
1344 unsigned int dsor = 0;
1345 ulong start;
1346
1347 mmc_base = priv->base_addr;
1348 omap_hsmmc_stop_clock(mmc_base);
1349
1350 /* TODO: Is setting DTO required here? */
1351 mmc_reg_out(&mmc_base->sysctl, (ICE_MASK | DTO_MASK),
1352 (ICE_STOP | DTO_15THDTO));
1353
1354 if (mmc->clock != 0) {
1355 dsor = DIV_ROUND_UP(MMC_CLOCK_REFERENCE * 1000000, mmc->clock);
1356 if (dsor > CLKD_MAX)
1357 dsor = CLKD_MAX;
1358 } else {
1359 dsor = CLKD_MAX;
1360 }
1361
1362 mmc_reg_out(&mmc_base->sysctl, ICE_MASK | CLKD_MASK,
1363 (dsor << CLKD_OFFSET) | ICE_OSCILLATE);
1364
1365 start = get_timer(0);
1366 while ((readl(&mmc_base->sysctl) & ICS_MASK) == ICS_NOTREADY) {
1367 if (get_timer(0) - start > MAX_RETRY_MS) {
1368 printf("%s: timedout waiting for ics!\n", __func__);
1369 return;
1370 }
1371 }
1372
Jean-Jacques Hiblot6ce31e42018-01-30 16:01:43 +01001373 priv->clock = MMC_CLOCK_REFERENCE * 1000000 / dsor;
1374 mmc->clock = priv->clock;
Jean-Jacques Hiblot7fe2f192018-01-30 16:01:30 +01001375 omap_hsmmc_start_clock(mmc_base);
1376}
1377
Kishon Vijay Abraham I2e18c9b2018-01-30 16:01:31 +01001378static void omap_hsmmc_set_bus_width(struct mmc *mmc)
Sukumar Ghoraic53f5e52010-09-18 20:32:33 -07001379{
Jean-Jacques Hiblotd58ef8e2017-03-22 16:00:31 +01001380 struct omap_hsmmc_data *priv = omap_hsmmc_get_data(mmc);
Nikita Kiryanov13822862012-12-03 02:19:43 +00001381 struct hsmmc *mmc_base;
Sukumar Ghoraic53f5e52010-09-18 20:32:33 -07001382
Jean-Jacques Hiblotd58ef8e2017-03-22 16:00:31 +01001383 mmc_base = priv->base_addr;
Sukumar Ghoraic53f5e52010-09-18 20:32:33 -07001384 /* configue bus width */
1385 switch (mmc->bus_width) {
1386 case 8:
1387 writel(readl(&mmc_base->con) | DTW_8_BITMODE,
1388 &mmc_base->con);
1389 break;
1390
1391 case 4:
1392 writel(readl(&mmc_base->con) & ~DTW_8_BITMODE,
1393 &mmc_base->con);
1394 writel(readl(&mmc_base->hctl) | DTW_4_BITMODE,
1395 &mmc_base->hctl);
1396 break;
1397
1398 case 1:
1399 default:
1400 writel(readl(&mmc_base->con) & ~DTW_8_BITMODE,
1401 &mmc_base->con);
1402 writel(readl(&mmc_base->hctl) & ~DTW_4_BITMODE,
1403 &mmc_base->hctl);
1404 break;
1405 }
1406
Kishon Vijay Abraham I2e18c9b2018-01-30 16:01:31 +01001407 priv->bus_width = mmc->bus_width;
1408}
1409
1410#if !CONFIG_IS_ENABLED(DM_MMC)
1411static int omap_hsmmc_set_ios(struct mmc *mmc)
1412{
1413 struct omap_hsmmc_data *priv = omap_hsmmc_get_data(mmc);
1414#else
1415static int omap_hsmmc_set_ios(struct udevice *dev)
1416{
1417 struct omap_hsmmc_data *priv = dev_get_priv(dev);
1418 struct mmc_uclass_priv *upriv = dev_get_uclass_priv(dev);
1419 struct mmc *mmc = upriv->mmc;
1420#endif
Kishon Vijay Abraham Ie1f25c02018-01-30 16:01:45 +01001421 struct hsmmc *mmc_base = priv->base_addr;
Jean-Jacques Hiblot7a41bb42018-01-30 16:01:46 +01001422 int ret = 0;
Kishon Vijay Abraham I2e18c9b2018-01-30 16:01:31 +01001423
1424 if (priv->bus_width != mmc->bus_width)
1425 omap_hsmmc_set_bus_width(mmc);
1426
Jean-Jacques Hiblot7fe2f192018-01-30 16:01:30 +01001427 if (priv->clock != mmc->clock)
1428 omap_hsmmc_set_clock(mmc);
Jaehoon Chungb6cd1d32016-12-30 15:30:16 +09001429
Kishon Vijay Abraham Ie1f25c02018-01-30 16:01:45 +01001430 if (mmc->clk_disable)
1431 omap_hsmmc_stop_clock(mmc_base);
1432 else
1433 omap_hsmmc_start_clock(mmc_base);
1434
Jean-Jacques Hiblotcf38d4e2018-01-30 16:01:33 +01001435#if CONFIG_IS_ENABLED(DM_MMC)
1436 if (priv->mode != mmc->selected_mode)
1437 omap_hsmmc_set_timing(mmc);
Jean-Jacques Hiblot7a41bb42018-01-30 16:01:46 +01001438
1439#if CONFIG_IS_ENABLED(MMC_IO_VOLTAGE)
1440 if (priv->signal_voltage != mmc->signal_voltage)
1441 ret = omap_hsmmc_set_signal_voltage(mmc);
Jean-Jacques Hiblotcf38d4e2018-01-30 16:01:33 +01001442#endif
Jean-Jacques Hiblot7a41bb42018-01-30 16:01:46 +01001443#endif
1444 return ret;
Sukumar Ghoraic53f5e52010-09-18 20:32:33 -07001445}
1446
Pantelis Antoniouc9e75912014-02-26 19:28:45 +02001447#ifdef OMAP_HSMMC_USE_GPIO
Simon Glass5f4bd8c2017-07-04 13:31:19 -06001448#if CONFIG_IS_ENABLED(DM_MMC)
Jean-Jacques Hiblot8fc9d3a2017-04-14 19:50:02 +02001449static int omap_hsmmc_getcd(struct udevice *dev)
Pantelis Antoniouc9e75912014-02-26 19:28:45 +02001450{
Adam Ford6122af42018-08-21 07:16:56 -05001451 int value = -1;
1452#if CONFIG_IS_ENABLED(DM_GPIO)
Adam Fordac740ff2018-09-08 08:16:23 -05001453 struct omap_hsmmc_data *priv = dev_get_priv(dev);
Mugunthan V Nd97631a2015-09-28 12:56:30 +05301454 value = dm_gpio_get_value(&priv->cd_gpio);
Adam Ford6122af42018-08-21 07:16:56 -05001455#endif
Mugunthan V Nd97631a2015-09-28 12:56:30 +05301456 /* if no CD return as 1 */
1457 if (value < 0)
1458 return 1;
1459
Mugunthan V Nd97631a2015-09-28 12:56:30 +05301460 return value;
1461}
1462
Jean-Jacques Hiblot8fc9d3a2017-04-14 19:50:02 +02001463static int omap_hsmmc_getwp(struct udevice *dev)
Mugunthan V Nd97631a2015-09-28 12:56:30 +05301464{
Adam Ford6122af42018-08-21 07:16:56 -05001465 int value = 0;
1466#if CONFIG_IS_ENABLED(DM_GPIO)
Jean-Jacques Hiblot8fc9d3a2017-04-14 19:50:02 +02001467 struct omap_hsmmc_data *priv = dev_get_priv(dev);
Mugunthan V Nd97631a2015-09-28 12:56:30 +05301468 value = dm_gpio_get_value(&priv->wp_gpio);
Adam Ford6122af42018-08-21 07:16:56 -05001469#endif
Mugunthan V Nd97631a2015-09-28 12:56:30 +05301470 /* if no WP return as 0 */
1471 if (value < 0)
1472 return 0;
1473 return value;
1474}
1475#else
1476static int omap_hsmmc_getcd(struct mmc *mmc)
1477{
Jean-Jacques Hiblotd58ef8e2017-03-22 16:00:31 +01001478 struct omap_hsmmc_data *priv = omap_hsmmc_get_data(mmc);
Pantelis Antoniouc9e75912014-02-26 19:28:45 +02001479 int cd_gpio;
1480
1481 /* if no CD return as 1 */
Jean-Jacques Hiblotd58ef8e2017-03-22 16:00:31 +01001482 cd_gpio = priv->cd_gpio;
Pantelis Antoniouc9e75912014-02-26 19:28:45 +02001483 if (cd_gpio < 0)
1484 return 1;
1485
Igor Grinberg2f4e0952014-11-03 11:32:23 +02001486 /* NOTE: assumes card detect signal is active-low */
1487 return !gpio_get_value(cd_gpio);
Pantelis Antoniouc9e75912014-02-26 19:28:45 +02001488}
1489
1490static int omap_hsmmc_getwp(struct mmc *mmc)
1491{
Jean-Jacques Hiblotd58ef8e2017-03-22 16:00:31 +01001492 struct omap_hsmmc_data *priv = omap_hsmmc_get_data(mmc);
Pantelis Antoniouc9e75912014-02-26 19:28:45 +02001493 int wp_gpio;
1494
1495 /* if no WP return as 0 */
Jean-Jacques Hiblotd58ef8e2017-03-22 16:00:31 +01001496 wp_gpio = priv->wp_gpio;
Pantelis Antoniouc9e75912014-02-26 19:28:45 +02001497 if (wp_gpio < 0)
1498 return 0;
1499
Igor Grinberg2f4e0952014-11-03 11:32:23 +02001500 /* NOTE: assumes write protect signal is active-high */
Pantelis Antoniouc9e75912014-02-26 19:28:45 +02001501 return gpio_get_value(wp_gpio);
1502}
1503#endif
Mugunthan V Nd97631a2015-09-28 12:56:30 +05301504#endif
Pantelis Antoniouc9e75912014-02-26 19:28:45 +02001505
Simon Glass5f4bd8c2017-07-04 13:31:19 -06001506#if CONFIG_IS_ENABLED(DM_MMC)
Jean-Jacques Hiblot8fc9d3a2017-04-14 19:50:02 +02001507static const struct dm_mmc_ops omap_hsmmc_ops = {
1508 .send_cmd = omap_hsmmc_send_cmd,
1509 .set_ios = omap_hsmmc_set_ios,
1510#ifdef OMAP_HSMMC_USE_GPIO
1511 .get_cd = omap_hsmmc_getcd,
1512 .get_wp = omap_hsmmc_getwp,
1513#endif
Jean-Jacques Hiblotf0f821b2018-01-30 16:01:35 +01001514#ifdef MMC_SUPPORTS_TUNING
1515 .execute_tuning = omap_hsmmc_execute_tuning,
1516#endif
Jean-Jacques Hiblot7a41bb42018-01-30 16:01:46 +01001517 .wait_dat0 = omap_hsmmc_wait_dat0,
Jean-Jacques Hiblot8fc9d3a2017-04-14 19:50:02 +02001518};
1519#else
Pantelis Antoniouc9e75912014-02-26 19:28:45 +02001520static const struct mmc_ops omap_hsmmc_ops = {
1521 .send_cmd = omap_hsmmc_send_cmd,
1522 .set_ios = omap_hsmmc_set_ios,
1523 .init = omap_hsmmc_init_setup,
1524#ifdef OMAP_HSMMC_USE_GPIO
1525 .getcd = omap_hsmmc_getcd,
1526 .getwp = omap_hsmmc_getwp,
1527#endif
1528};
Jean-Jacques Hiblot8fc9d3a2017-04-14 19:50:02 +02001529#endif
Pantelis Antoniouc9e75912014-02-26 19:28:45 +02001530
Simon Glass5f4bd8c2017-07-04 13:31:19 -06001531#if !CONFIG_IS_ENABLED(DM_MMC)
Nikita Kiryanov4be9dbc2012-12-03 02:19:47 +00001532int omap_mmc_init(int dev_index, uint host_caps_mask, uint f_max, int cd_gpio,
1533 int wp_gpio)
Sukumar Ghoraic53f5e52010-09-18 20:32:33 -07001534{
Pantelis Antoniou2c850462014-03-11 19:34:20 +02001535 struct mmc *mmc;
Jean-Jacques Hiblotd58ef8e2017-03-22 16:00:31 +01001536 struct omap_hsmmc_data *priv;
Pantelis Antoniou2c850462014-03-11 19:34:20 +02001537 struct mmc_config *cfg;
1538 uint host_caps_val;
1539
Alex Kiernan4b9cb772018-02-09 15:24:38 +00001540 priv = calloc(1, sizeof(*priv));
Jean-Jacques Hiblotd58ef8e2017-03-22 16:00:31 +01001541 if (priv == NULL)
Pantelis Antoniou2c850462014-03-11 19:34:20 +02001542 return -1;
Sukumar Ghoraic53f5e52010-09-18 20:32:33 -07001543
Rob Herring5fd3edd2015-03-23 17:56:59 -05001544 host_caps_val = MMC_MODE_4BIT | MMC_MODE_HS_52MHz | MMC_MODE_HS;
Sukumar Ghoraic53f5e52010-09-18 20:32:33 -07001545
1546 switch (dev_index) {
1547 case 0:
Jean-Jacques Hiblotd58ef8e2017-03-22 16:00:31 +01001548 priv->base_addr = (struct hsmmc *)OMAP_HSMMC1_BASE;
Sukumar Ghoraic53f5e52010-09-18 20:32:33 -07001549 break;
Tom Rinifd6e2942011-10-12 06:20:50 +00001550#ifdef OMAP_HSMMC2_BASE
Sukumar Ghoraic53f5e52010-09-18 20:32:33 -07001551 case 1:
Jean-Jacques Hiblotd58ef8e2017-03-22 16:00:31 +01001552 priv->base_addr = (struct hsmmc *)OMAP_HSMMC2_BASE;
Lubomir Popov19df4122013-08-14 18:59:18 +03001553#if (defined(CONFIG_OMAP44XX) || defined(CONFIG_OMAP54XX) || \
Nishanth Menon813fe9d2016-11-29 15:22:00 +05301554 defined(CONFIG_DRA7XX) || defined(CONFIG_AM33XX) || \
Roger Quadros44157de2015-09-19 16:26:53 +05301555 defined(CONFIG_AM43XX) || defined(CONFIG_SOC_KEYSTONE)) && \
1556 defined(CONFIG_HSMMC2_8BIT)
Lubomir Popov19df4122013-08-14 18:59:18 +03001557 /* Enable 8-bit interface for eMMC on OMAP4/5 or DRA7XX */
1558 host_caps_val |= MMC_MODE_8BIT;
1559#endif
Sukumar Ghoraic53f5e52010-09-18 20:32:33 -07001560 break;
Tom Rinifd6e2942011-10-12 06:20:50 +00001561#endif
1562#ifdef OMAP_HSMMC3_BASE
Sukumar Ghoraic53f5e52010-09-18 20:32:33 -07001563 case 2:
Jean-Jacques Hiblotd58ef8e2017-03-22 16:00:31 +01001564 priv->base_addr = (struct hsmmc *)OMAP_HSMMC3_BASE;
Nishanth Menon813fe9d2016-11-29 15:22:00 +05301565#if defined(CONFIG_DRA7XX) && defined(CONFIG_HSMMC3_8BIT)
Lubomir Popov19df4122013-08-14 18:59:18 +03001566 /* Enable 8-bit interface for eMMC on DRA7XX */
1567 host_caps_val |= MMC_MODE_8BIT;
1568#endif
Sukumar Ghoraic53f5e52010-09-18 20:32:33 -07001569 break;
Tom Rinifd6e2942011-10-12 06:20:50 +00001570#endif
Sukumar Ghoraic53f5e52010-09-18 20:32:33 -07001571 default:
Jean-Jacques Hiblotd58ef8e2017-03-22 16:00:31 +01001572 priv->base_addr = (struct hsmmc *)OMAP_HSMMC1_BASE;
Sukumar Ghoraic53f5e52010-09-18 20:32:33 -07001573 return 1;
1574 }
Pantelis Antoniouc9e75912014-02-26 19:28:45 +02001575#ifdef OMAP_HSMMC_USE_GPIO
1576 /* on error gpio values are set to -1, which is what we want */
Jean-Jacques Hiblotd58ef8e2017-03-22 16:00:31 +01001577 priv->cd_gpio = omap_mmc_setup_gpio_in(cd_gpio, "mmc_cd");
1578 priv->wp_gpio = omap_mmc_setup_gpio_in(wp_gpio, "mmc_wp");
Pantelis Antoniouc9e75912014-02-26 19:28:45 +02001579#endif
Peter Korsgaard47c6b2a2013-03-21 04:00:04 +00001580
Jean-Jacques Hiblotd58ef8e2017-03-22 16:00:31 +01001581 cfg = &priv->cfg;
Sukumar Ghoraic53f5e52010-09-18 20:32:33 -07001582
Pantelis Antoniou2c850462014-03-11 19:34:20 +02001583 cfg->name = "OMAP SD/MMC";
1584 cfg->ops = &omap_hsmmc_ops;
1585
1586 cfg->voltages = MMC_VDD_32_33 | MMC_VDD_33_34 | MMC_VDD_165_195;
1587 cfg->host_caps = host_caps_val & ~host_caps_mask;
1588
1589 cfg->f_min = 400000;
Jonathan Solnita9b05562012-02-24 11:30:18 +00001590
1591 if (f_max != 0)
Pantelis Antoniou2c850462014-03-11 19:34:20 +02001592 cfg->f_max = f_max;
Jonathan Solnita9b05562012-02-24 11:30:18 +00001593 else {
Pantelis Antoniou2c850462014-03-11 19:34:20 +02001594 if (cfg->host_caps & MMC_MODE_HS) {
1595 if (cfg->host_caps & MMC_MODE_HS_52MHz)
1596 cfg->f_max = 52000000;
Jonathan Solnita9b05562012-02-24 11:30:18 +00001597 else
Pantelis Antoniou2c850462014-03-11 19:34:20 +02001598 cfg->f_max = 26000000;
Jonathan Solnita9b05562012-02-24 11:30:18 +00001599 } else
Pantelis Antoniou2c850462014-03-11 19:34:20 +02001600 cfg->f_max = 20000000;
Jonathan Solnita9b05562012-02-24 11:30:18 +00001601 }
Sukumar Ghoraic53f5e52010-09-18 20:32:33 -07001602
Pantelis Antoniou2c850462014-03-11 19:34:20 +02001603 cfg->b_max = CONFIG_SYS_MMC_MAX_BLK_COUNT;
John Rigbyf2f43662011-04-18 05:50:08 +00001604
John Rigby91fcc4b2011-04-19 05:48:14 +00001605#if defined(CONFIG_OMAP34XX)
1606 /*
1607 * Silicon revs 2.1 and older do not support multiblock transfers.
1608 */
1609 if ((get_cpu_family() == CPU_OMAP34XX) && (get_cpu_rev() <= CPU_3XX_ES21))
Pantelis Antoniou2c850462014-03-11 19:34:20 +02001610 cfg->b_max = 1;
John Rigby91fcc4b2011-04-19 05:48:14 +00001611#endif
Kishon Vijay Abraham I8c2efe92018-01-30 16:01:41 +01001612
Jean-Jacques Hiblotd58ef8e2017-03-22 16:00:31 +01001613 mmc = mmc_create(cfg, priv);
Pantelis Antoniou2c850462014-03-11 19:34:20 +02001614 if (mmc == NULL)
1615 return -1;
Sukumar Ghoraic53f5e52010-09-18 20:32:33 -07001616
1617 return 0;
1618}
Mugunthan V Nd97631a2015-09-28 12:56:30 +05301619#else
Kishon Vijay Abraham Ie7da6ac2018-01-30 16:01:40 +01001620
1621#ifdef CONFIG_IODELAY_RECALIBRATION
1622static struct pad_conf_entry *
1623omap_hsmmc_get_pad_conf_entry(const fdt32_t *pinctrl, int count)
1624{
1625 int index = 0;
1626 struct pad_conf_entry *padconf;
1627
1628 padconf = (struct pad_conf_entry *)malloc(sizeof(*padconf) * count);
1629 if (!padconf) {
1630 debug("failed to allocate memory\n");
1631 return 0;
1632 }
1633
1634 while (index < count) {
1635 padconf[index].offset = fdt32_to_cpu(pinctrl[2 * index]);
1636 padconf[index].val = fdt32_to_cpu(pinctrl[2 * index + 1]);
1637 index++;
1638 }
1639
1640 return padconf;
1641}
1642
1643static struct iodelay_cfg_entry *
1644omap_hsmmc_get_iodelay_cfg_entry(const fdt32_t *pinctrl, int count)
1645{
1646 int index = 0;
1647 struct iodelay_cfg_entry *iodelay;
1648
1649 iodelay = (struct iodelay_cfg_entry *)malloc(sizeof(*iodelay) * count);
1650 if (!iodelay) {
1651 debug("failed to allocate memory\n");
1652 return 0;
1653 }
1654
1655 while (index < count) {
1656 iodelay[index].offset = fdt32_to_cpu(pinctrl[3 * index]);
1657 iodelay[index].a_delay = fdt32_to_cpu(pinctrl[3 * index + 1]);
1658 iodelay[index].g_delay = fdt32_to_cpu(pinctrl[3 * index + 2]);
1659 index++;
1660 }
1661
1662 return iodelay;
1663}
1664
1665static const fdt32_t *omap_hsmmc_get_pinctrl_entry(u32 phandle,
1666 const char *name, int *len)
1667{
1668 const void *fdt = gd->fdt_blob;
1669 int offset;
1670 const fdt32_t *pinctrl;
1671
1672 offset = fdt_node_offset_by_phandle(fdt, phandle);
1673 if (offset < 0) {
1674 debug("failed to get pinctrl node %s.\n",
1675 fdt_strerror(offset));
1676 return 0;
1677 }
1678
1679 pinctrl = fdt_getprop(fdt, offset, name, len);
1680 if (!pinctrl) {
1681 debug("failed to get property %s\n", name);
1682 return 0;
1683 }
1684
1685 return pinctrl;
1686}
1687
1688static uint32_t omap_hsmmc_get_pad_conf_phandle(struct mmc *mmc,
1689 char *prop_name)
1690{
1691 const void *fdt = gd->fdt_blob;
1692 const __be32 *phandle;
1693 int node = dev_of_offset(mmc->dev);
1694
1695 phandle = fdt_getprop(fdt, node, prop_name, NULL);
1696 if (!phandle) {
1697 debug("failed to get property %s\n", prop_name);
1698 return 0;
1699 }
1700
1701 return fdt32_to_cpu(*phandle);
1702}
1703
1704static uint32_t omap_hsmmc_get_iodelay_phandle(struct mmc *mmc,
1705 char *prop_name)
1706{
1707 const void *fdt = gd->fdt_blob;
1708 const __be32 *phandle;
1709 int len;
1710 int count;
1711 int node = dev_of_offset(mmc->dev);
1712
1713 phandle = fdt_getprop(fdt, node, prop_name, &len);
1714 if (!phandle) {
1715 debug("failed to get property %s\n", prop_name);
1716 return 0;
1717 }
1718
1719 /* No manual mode iodelay values if count < 2 */
1720 count = len / sizeof(*phandle);
1721 if (count < 2)
1722 return 0;
1723
1724 return fdt32_to_cpu(*(phandle + 1));
1725}
1726
1727static struct pad_conf_entry *
1728omap_hsmmc_get_pad_conf(struct mmc *mmc, char *prop_name, int *npads)
1729{
1730 int len;
1731 int count;
1732 struct pad_conf_entry *padconf;
1733 u32 phandle;
1734 const fdt32_t *pinctrl;
1735
1736 phandle = omap_hsmmc_get_pad_conf_phandle(mmc, prop_name);
1737 if (!phandle)
1738 return ERR_PTR(-EINVAL);
1739
1740 pinctrl = omap_hsmmc_get_pinctrl_entry(phandle, "pinctrl-single,pins",
1741 &len);
1742 if (!pinctrl)
1743 return ERR_PTR(-EINVAL);
1744
1745 count = (len / sizeof(*pinctrl)) / 2;
1746 padconf = omap_hsmmc_get_pad_conf_entry(pinctrl, count);
1747 if (!padconf)
1748 return ERR_PTR(-EINVAL);
1749
1750 *npads = count;
1751
1752 return padconf;
1753}
1754
1755static struct iodelay_cfg_entry *
1756omap_hsmmc_get_iodelay(struct mmc *mmc, char *prop_name, int *niodelay)
1757{
1758 int len;
1759 int count;
1760 struct iodelay_cfg_entry *iodelay;
1761 u32 phandle;
1762 const fdt32_t *pinctrl;
1763
1764 phandle = omap_hsmmc_get_iodelay_phandle(mmc, prop_name);
1765 /* Not all modes have manual mode iodelay values. So its not fatal */
1766 if (!phandle)
1767 return 0;
1768
1769 pinctrl = omap_hsmmc_get_pinctrl_entry(phandle, "pinctrl-pin-array",
1770 &len);
1771 if (!pinctrl)
1772 return ERR_PTR(-EINVAL);
1773
1774 count = (len / sizeof(*pinctrl)) / 3;
1775 iodelay = omap_hsmmc_get_iodelay_cfg_entry(pinctrl, count);
1776 if (!iodelay)
1777 return ERR_PTR(-EINVAL);
1778
1779 *niodelay = count;
1780
1781 return iodelay;
1782}
1783
1784static struct omap_hsmmc_pinctrl_state *
1785omap_hsmmc_get_pinctrl_by_mode(struct mmc *mmc, char *mode)
1786{
1787 int index;
1788 int npads = 0;
1789 int niodelays = 0;
1790 const void *fdt = gd->fdt_blob;
1791 int node = dev_of_offset(mmc->dev);
1792 char prop_name[11];
1793 struct omap_hsmmc_pinctrl_state *pinctrl_state;
1794
1795 pinctrl_state = (struct omap_hsmmc_pinctrl_state *)
1796 malloc(sizeof(*pinctrl_state));
1797 if (!pinctrl_state) {
1798 debug("failed to allocate memory\n");
1799 return 0;
1800 }
1801
1802 index = fdt_stringlist_search(fdt, node, "pinctrl-names", mode);
1803 if (index < 0) {
1804 debug("fail to find %s mode %s\n", mode, fdt_strerror(index));
1805 goto err_pinctrl_state;
1806 }
1807
1808 sprintf(prop_name, "pinctrl-%d", index);
1809
1810 pinctrl_state->padconf = omap_hsmmc_get_pad_conf(mmc, prop_name,
1811 &npads);
1812 if (IS_ERR(pinctrl_state->padconf))
1813 goto err_pinctrl_state;
1814 pinctrl_state->npads = npads;
1815
1816 pinctrl_state->iodelay = omap_hsmmc_get_iodelay(mmc, prop_name,
1817 &niodelays);
1818 if (IS_ERR(pinctrl_state->iodelay))
1819 goto err_padconf;
1820 pinctrl_state->niodelays = niodelays;
1821
1822 return pinctrl_state;
1823
1824err_padconf:
1825 kfree(pinctrl_state->padconf);
1826
1827err_pinctrl_state:
1828 kfree(pinctrl_state);
1829 return 0;
1830}
1831
Jean-Jacques Hiblotdae1ad42018-01-30 16:01:42 +01001832#define OMAP_HSMMC_SETUP_PINCTRL(capmask, mode, optional) \
Kishon Vijay Abraham I8c2efe92018-01-30 16:01:41 +01001833 do { \
1834 struct omap_hsmmc_pinctrl_state *s = NULL; \
1835 char str[20]; \
1836 if (!(cfg->host_caps & capmask)) \
1837 break; \
1838 \
1839 if (priv->hw_rev) { \
1840 sprintf(str, "%s-%s", #mode, priv->hw_rev); \
1841 s = omap_hsmmc_get_pinctrl_by_mode(mmc, str); \
1842 } \
1843 \
1844 if (!s) \
1845 s = omap_hsmmc_get_pinctrl_by_mode(mmc, #mode); \
1846 \
Jean-Jacques Hiblotdae1ad42018-01-30 16:01:42 +01001847 if (!s && !optional) { \
Kishon Vijay Abraham I8c2efe92018-01-30 16:01:41 +01001848 debug("%s: no pinctrl for %s\n", \
1849 mmc->dev->name, #mode); \
1850 cfg->host_caps &= ~(capmask); \
1851 } else { \
1852 priv->mode##_pinctrl_state = s; \
1853 } \
Kishon Vijay Abraham Ie7da6ac2018-01-30 16:01:40 +01001854 } while (0)
1855
1856static int omap_hsmmc_get_pinctrl_state(struct mmc *mmc)
1857{
1858 struct omap_hsmmc_data *priv = omap_hsmmc_get_data(mmc);
1859 struct mmc_config *cfg = omap_hsmmc_get_cfg(mmc);
1860 struct omap_hsmmc_pinctrl_state *default_pinctrl;
1861
1862 if (!(priv->controller_flags & OMAP_HSMMC_REQUIRE_IODELAY))
1863 return 0;
1864
1865 default_pinctrl = omap_hsmmc_get_pinctrl_by_mode(mmc, "default");
1866 if (!default_pinctrl) {
1867 printf("no pinctrl state for default mode\n");
1868 return -EINVAL;
1869 }
1870
1871 priv->default_pinctrl_state = default_pinctrl;
1872
Jean-Jacques Hiblotdae1ad42018-01-30 16:01:42 +01001873 OMAP_HSMMC_SETUP_PINCTRL(MMC_CAP(UHS_SDR104), sdr104, false);
1874 OMAP_HSMMC_SETUP_PINCTRL(MMC_CAP(UHS_SDR50), sdr50, false);
1875 OMAP_HSMMC_SETUP_PINCTRL(MMC_CAP(UHS_DDR50), ddr50, false);
1876 OMAP_HSMMC_SETUP_PINCTRL(MMC_CAP(UHS_SDR25), sdr25, false);
1877 OMAP_HSMMC_SETUP_PINCTRL(MMC_CAP(UHS_SDR12), sdr12, false);
Kishon Vijay Abraham Ie7da6ac2018-01-30 16:01:40 +01001878
Jean-Jacques Hiblotdae1ad42018-01-30 16:01:42 +01001879 OMAP_HSMMC_SETUP_PINCTRL(MMC_CAP(MMC_HS_200), hs200_1_8v, false);
1880 OMAP_HSMMC_SETUP_PINCTRL(MMC_CAP(MMC_DDR_52), ddr_1_8v, false);
1881 OMAP_HSMMC_SETUP_PINCTRL(MMC_MODE_HS, hs, true);
Kishon Vijay Abraham Ie7da6ac2018-01-30 16:01:40 +01001882
1883 return 0;
1884}
1885#endif
1886
Lokesh Vutla9a696fb2017-04-26 13:37:05 +05301887#if CONFIG_IS_ENABLED(OF_CONTROL) && !CONFIG_IS_ENABLED(OF_PLATDATA)
Kishon Vijay Abraham I8c2efe92018-01-30 16:01:41 +01001888#ifdef CONFIG_OMAP54XX
1889__weak const struct mmc_platform_fixups *platform_fixups_mmc(uint32_t addr)
1890{
1891 return NULL;
1892}
1893#endif
1894
Mugunthan V Nd97631a2015-09-28 12:56:30 +05301895static int omap_hsmmc_ofdata_to_platdata(struct udevice *dev)
1896{
Jean-Jacques Hiblotae51a662017-03-22 16:00:33 +01001897 struct omap_hsmmc_plat *plat = dev_get_platdata(dev);
Kishon Vijay Abraham Ie7da6ac2018-01-30 16:01:40 +01001898 struct omap_mmc_of_data *of_data = (void *)dev_get_driver_data(dev);
1899
Jean-Jacques Hiblotae51a662017-03-22 16:00:33 +01001900 struct mmc_config *cfg = &plat->cfg;
Kishon Vijay Abraham I8c2efe92018-01-30 16:01:41 +01001901#ifdef CONFIG_OMAP54XX
1902 const struct mmc_platform_fixups *fixups;
1903#endif
Mugunthan V Nd97631a2015-09-28 12:56:30 +05301904 const void *fdt = gd->fdt_blob;
Simon Glassdd79d6e2017-01-17 16:52:55 -07001905 int node = dev_of_offset(dev);
Kishon Vijay Abraham I569c3d52018-01-30 16:01:38 +01001906 int ret;
Mugunthan V Nd97631a2015-09-28 12:56:30 +05301907
Simon Glassba1dea42017-05-17 17:18:05 -06001908 plat->base_addr = map_physmem(devfdt_get_addr(dev),
1909 sizeof(struct hsmmc *),
Jean-Jacques Hiblot3d45bb42017-09-21 16:51:32 +02001910 MAP_NOCACHE);
Mugunthan V Nd97631a2015-09-28 12:56:30 +05301911
Kishon Vijay Abraham I569c3d52018-01-30 16:01:38 +01001912 ret = mmc_of_parse(dev, cfg);
1913 if (ret < 0)
1914 return ret;
Mugunthan V Nd97631a2015-09-28 12:56:30 +05301915
Jean-Jacques Hiblot8e2bdbd2018-02-23 10:40:19 +01001916 if (!cfg->f_max)
1917 cfg->f_max = 52000000;
Kishon Vijay Abraham I569c3d52018-01-30 16:01:38 +01001918 cfg->host_caps |= MMC_MODE_HS_52MHz | MMC_MODE_HS;
Mugunthan V Nd97631a2015-09-28 12:56:30 +05301919 cfg->f_min = 400000;
Mugunthan V Nd97631a2015-09-28 12:56:30 +05301920 cfg->voltages = MMC_VDD_32_33 | MMC_VDD_33_34 | MMC_VDD_165_195;
1921 cfg->b_max = CONFIG_SYS_MMC_MAX_BLK_COUNT;
Kishon Vijay Abraham I73897ed2018-01-30 16:01:32 +01001922 if (fdtdec_get_bool(fdt, node, "ti,dual-volt"))
1923 plat->controller_flags |= OMAP_HSMMC_SUPPORTS_DUAL_VOLT;
1924 if (fdtdec_get_bool(fdt, node, "no-1-8-v"))
1925 plat->controller_flags |= OMAP_HSMMC_NO_1_8_V;
Kishon Vijay Abraham Ie7da6ac2018-01-30 16:01:40 +01001926 if (of_data)
1927 plat->controller_flags |= of_data->controller_flags;
Mugunthan V Nd97631a2015-09-28 12:56:30 +05301928
Kishon Vijay Abraham I8c2efe92018-01-30 16:01:41 +01001929#ifdef CONFIG_OMAP54XX
1930 fixups = platform_fixups_mmc(devfdt_get_addr(dev));
1931 if (fixups) {
1932 plat->hw_rev = fixups->hw_rev;
1933 cfg->host_caps &= ~fixups->unsupported_caps;
1934 cfg->f_max = fixups->max_freq;
1935 }
1936#endif
1937
Mugunthan V Nd97631a2015-09-28 12:56:30 +05301938 return 0;
1939}
Lokesh Vutla9a696fb2017-04-26 13:37:05 +05301940#endif
Mugunthan V Nd97631a2015-09-28 12:56:30 +05301941
Jean-Jacques Hiblota3c556c2017-03-22 16:00:34 +01001942#ifdef CONFIG_BLK
1943
1944static int omap_hsmmc_bind(struct udevice *dev)
1945{
1946 struct omap_hsmmc_plat *plat = dev_get_platdata(dev);
Jean-Jacques Hiblot4cb36a22018-02-23 10:40:16 +01001947 plat->mmc = calloc(1, sizeof(struct mmc));
1948 return mmc_bind(dev, plat->mmc, &plat->cfg);
Jean-Jacques Hiblota3c556c2017-03-22 16:00:34 +01001949}
1950#endif
Mugunthan V Nd97631a2015-09-28 12:56:30 +05301951static int omap_hsmmc_probe(struct udevice *dev)
1952{
Jean-Jacques Hiblotae51a662017-03-22 16:00:33 +01001953 struct omap_hsmmc_plat *plat = dev_get_platdata(dev);
Mugunthan V Nd97631a2015-09-28 12:56:30 +05301954 struct mmc_uclass_priv *upriv = dev_get_uclass_priv(dev);
1955 struct omap_hsmmc_data *priv = dev_get_priv(dev);
Jean-Jacques Hiblotae51a662017-03-22 16:00:33 +01001956 struct mmc_config *cfg = &plat->cfg;
Mugunthan V Nd97631a2015-09-28 12:56:30 +05301957 struct mmc *mmc;
Kishon Vijay Abraham Ie7da6ac2018-01-30 16:01:40 +01001958#ifdef CONFIG_IODELAY_RECALIBRATION
1959 int ret;
1960#endif
Mugunthan V Nd97631a2015-09-28 12:56:30 +05301961
Mugunthan V Nd97631a2015-09-28 12:56:30 +05301962 cfg->name = "OMAP SD/MMC";
Lokesh Vutla9a696fb2017-04-26 13:37:05 +05301963 priv->base_addr = plat->base_addr;
Kishon Vijay Abraham Ie7da6ac2018-01-30 16:01:40 +01001964 priv->controller_flags = plat->controller_flags;
Kishon Vijay Abraham I8c2efe92018-01-30 16:01:41 +01001965 priv->hw_rev = plat->hw_rev;
Mugunthan V Nd97631a2015-09-28 12:56:30 +05301966
Jean-Jacques Hiblota3c556c2017-03-22 16:00:34 +01001967#ifdef CONFIG_BLK
Jean-Jacques Hiblot4cb36a22018-02-23 10:40:16 +01001968 mmc = plat->mmc;
Jean-Jacques Hiblota3c556c2017-03-22 16:00:34 +01001969#else
Mugunthan V Nd97631a2015-09-28 12:56:30 +05301970 mmc = mmc_create(cfg, priv);
1971 if (mmc == NULL)
1972 return -1;
Jean-Jacques Hiblota3c556c2017-03-22 16:00:34 +01001973#endif
Jean-Jacques Hiblot7a41bb42018-01-30 16:01:46 +01001974#if CONFIG_IS_ENABLED(DM_REGULATOR)
1975 device_get_supply_regulator(dev, "pbias-supply",
1976 &priv->pbias_supply);
1977#endif
Adam Ford6122af42018-08-21 07:16:56 -05001978#if defined(OMAP_HSMMC_USE_GPIO)
1979#if CONFIG_IS_ENABLED(OF_CONTROL) && CONFIG_IS_ENABLED(DM_GPIO)
Mugunthan V Na9a0aa72016-04-04 17:28:01 +05301980 gpio_request_by_name(dev, "cd-gpios", 0, &priv->cd_gpio, GPIOD_IS_IN);
1981 gpio_request_by_name(dev, "wp-gpios", 0, &priv->wp_gpio, GPIOD_IS_IN);
1982#endif
Adam Ford6122af42018-08-21 07:16:56 -05001983#endif
Mugunthan V Na9a0aa72016-04-04 17:28:01 +05301984
Simon Glass77ca42b2016-05-01 13:52:34 -06001985 mmc->dev = dev;
Mugunthan V Nd97631a2015-09-28 12:56:30 +05301986 upriv->mmc = mmc;
1987
Kishon Vijay Abraham Ie7da6ac2018-01-30 16:01:40 +01001988#ifdef CONFIG_IODELAY_RECALIBRATION
1989 ret = omap_hsmmc_get_pinctrl_state(mmc);
1990 /*
1991 * disable high speed modes for the platforms that require IO delay
1992 * and for which we don't have this information
1993 */
1994 if ((ret < 0) &&
1995 (priv->controller_flags & OMAP_HSMMC_REQUIRE_IODELAY)) {
1996 priv->controller_flags &= ~OMAP_HSMMC_REQUIRE_IODELAY;
1997 cfg->host_caps &= ~(MMC_CAP(MMC_HS_200) | MMC_CAP(MMC_DDR_52) |
1998 UHS_CAPS);
1999 }
2000#endif
2001
Jean-Jacques Hiblot8fc9d3a2017-04-14 19:50:02 +02002002 return omap_hsmmc_init_setup(mmc);
Mugunthan V Nd97631a2015-09-28 12:56:30 +05302003}
2004
Lokesh Vutla9a696fb2017-04-26 13:37:05 +05302005#if CONFIG_IS_ENABLED(OF_CONTROL) && !CONFIG_IS_ENABLED(OF_PLATDATA)
Kishon Vijay Abraham Ie7da6ac2018-01-30 16:01:40 +01002006
2007static const struct omap_mmc_of_data dra7_mmc_of_data = {
2008 .controller_flags = OMAP_HSMMC_REQUIRE_IODELAY,
2009};
2010
Mugunthan V Nd97631a2015-09-28 12:56:30 +05302011static const struct udevice_id omap_hsmmc_ids[] = {
Jean-Jacques Hiblot3d45bb42017-09-21 16:51:32 +02002012 { .compatible = "ti,omap3-hsmmc" },
2013 { .compatible = "ti,omap4-hsmmc" },
2014 { .compatible = "ti,am33xx-hsmmc" },
Kishon Vijay Abraham Ie7da6ac2018-01-30 16:01:40 +01002015 { .compatible = "ti,dra7-hsmmc", .data = (ulong)&dra7_mmc_of_data },
Mugunthan V Nd97631a2015-09-28 12:56:30 +05302016 { }
2017};
Lokesh Vutla9a696fb2017-04-26 13:37:05 +05302018#endif
Mugunthan V Nd97631a2015-09-28 12:56:30 +05302019
2020U_BOOT_DRIVER(omap_hsmmc) = {
2021 .name = "omap_hsmmc",
2022 .id = UCLASS_MMC,
Lokesh Vutla9a696fb2017-04-26 13:37:05 +05302023#if CONFIG_IS_ENABLED(OF_CONTROL) && !CONFIG_IS_ENABLED(OF_PLATDATA)
Mugunthan V Nd97631a2015-09-28 12:56:30 +05302024 .of_match = omap_hsmmc_ids,
2025 .ofdata_to_platdata = omap_hsmmc_ofdata_to_platdata,
Lokesh Vutla9a696fb2017-04-26 13:37:05 +05302026 .platdata_auto_alloc_size = sizeof(struct omap_hsmmc_plat),
2027#endif
Jean-Jacques Hiblota3c556c2017-03-22 16:00:34 +01002028#ifdef CONFIG_BLK
2029 .bind = omap_hsmmc_bind,
2030#endif
Jean-Jacques Hiblot8fc9d3a2017-04-14 19:50:02 +02002031 .ops = &omap_hsmmc_ops,
Mugunthan V Nd97631a2015-09-28 12:56:30 +05302032 .probe = omap_hsmmc_probe,
2033 .priv_auto_alloc_size = sizeof(struct omap_hsmmc_data),
Bin Meng793260a2018-10-24 06:36:32 -07002034#if !CONFIG_IS_ENABLED(OF_CONTROL)
Lokesh Vutlac38e6452017-04-26 13:37:06 +05302035 .flags = DM_FLAG_PRE_RELOC,
Bin Meng793260a2018-10-24 06:36:32 -07002036#endif
Mugunthan V Nd97631a2015-09-28 12:56:30 +05302037};
2038#endif