blob: 3ea7f4e173db74edbe7d69ea47cce3a2ebaed3dd [file] [log] [blame]
Sukumar Ghoraide941242010-09-18 20:32:33 -07001/*
2 * (C) Copyright 2008
3 * Texas Instruments, <www.ti.com>
4 * Sukumar Ghorai <s-ghorai@ti.com>
5 *
6 * See file CREDITS for list of people who contributed to this
7 * project.
8 *
9 * This program is free software; you can redistribute it and/or
10 * modify it under the terms of the GNU General Public License as
11 * published by the Free Software Foundation's version 2 of
12 * the License.
13 *
14 * This program is distributed in the hope that it will be useful,
15 * but WITHOUT ANY WARRANTY; without even the implied warranty of
16 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
17 * GNU General Public License for more details.
18 *
19 * You should have received a copy of the GNU General Public License
20 * along with this program; if not, write to the Free Software
21 * Foundation, Inc., 59 Temple Place, Suite 330, Boston,
22 * MA 02111-1307 USA
23 */
24
25#include <config.h>
26#include <common.h>
Pantelis Antoniou93bfd612014-03-11 19:34:20 +020027#include <malloc.h>
Kishon Vijay Abraham If0d53e82017-09-21 16:51:34 +020028#include <memalign.h>
Sukumar Ghoraide941242010-09-18 20:32:33 -070029#include <mmc.h>
30#include <part.h>
31#include <i2c.h>
Felix Brack339d5782017-10-11 17:05:28 +020032#if defined(CONFIG_OMAP54XX) || defined(CONFIG_OMAP44XX)
Nishanth Menoncb199102013-03-26 05:20:54 +000033#include <palmas.h>
Felix Brack339d5782017-10-11 17:05:28 +020034#endif
Sukumar Ghoraide941242010-09-18 20:32:33 -070035#include <asm/io.h>
36#include <asm/arch/mmc_host_def.h>
Kishon Vijay Abraham I33c1d772018-01-30 16:01:40 +010037#ifdef CONFIG_OMAP54XX
38#include <asm/arch/mux_dra7xx.h>
39#include <asm/arch/dra7xx_iodelay.h>
40#endif
Roger Quadros3b689392015-09-19 16:26:53 +053041#if !defined(CONFIG_SOC_KEYSTONE)
42#include <asm/gpio.h>
Dirk Behme96e0e7b2011-05-15 09:04:47 +000043#include <asm/arch/sys_proto.h>
Roger Quadros3b689392015-09-19 16:26:53 +053044#endif
Tom Rini2a48b3a2017-02-09 13:41:28 -050045#ifdef CONFIG_MMC_OMAP36XX_PINS
46#include <asm/arch/mux.h>
47#endif
Mugunthan V Na9d6a7e2015-09-28 12:56:30 +053048#include <dm.h>
Jean-Jacques Hiblot42182c92018-01-30 16:01:44 +010049#include <power/regulator.h>
Faiz Abbas351a4aa2019-01-30 18:08:42 +053050#include <thermal.h>
Mugunthan V Na9d6a7e2015-09-28 12:56:30 +053051
52DECLARE_GLOBAL_DATA_PTR;
Sukumar Ghoraide941242010-09-18 20:32:33 -070053
Pantelis Antoniouab769f22014-02-26 19:28:45 +020054/* simplify defines to OMAP_HSMMC_USE_GPIO */
55#if (defined(CONFIG_OMAP_GPIO) && !defined(CONFIG_SPL_BUILD)) || \
56 (defined(CONFIG_SPL_BUILD) && defined(CONFIG_SPL_GPIO_SUPPORT))
57#define OMAP_HSMMC_USE_GPIO
58#else
59#undef OMAP_HSMMC_USE_GPIO
60#endif
61
Grazvydas Ignotas25c719e2012-03-19 12:12:06 +000062/* common definitions for all OMAPs */
63#define SYSCTL_SRC (1 << 25)
64#define SYSCTL_SRD (1 << 26)
65
Kishon Vijay Abraham I33c1d772018-01-30 16:01:40 +010066#ifdef CONFIG_IODELAY_RECALIBRATION
67struct omap_hsmmc_pinctrl_state {
68 struct pad_conf_entry *padconf;
69 int npads;
70 struct iodelay_cfg_entry *iodelay;
71 int niodelays;
72};
73#endif
74
Nikita Kiryanovcc22b0c2012-12-03 02:19:43 +000075struct omap_hsmmc_data {
76 struct hsmmc *base_addr;
Simon Glassc4d660d2017-07-04 13:31:19 -060077#if !CONFIG_IS_ENABLED(DM_MMC)
Pantelis Antoniou93bfd612014-03-11 19:34:20 +020078 struct mmc_config cfg;
Jean-Jacques Hiblot3d673ff2017-03-22 16:00:33 +010079#endif
Kishon Vijay Abraham I48a2f112018-01-30 16:01:31 +010080 uint bus_width;
Jean-Jacques Hiblot5baf5432018-01-30 16:01:30 +010081 uint clock;
Jean-Jacques Hiblot04f9f8b2018-01-30 16:01:46 +010082 ushort last_cmd;
Pantelis Antoniouab769f22014-02-26 19:28:45 +020083#ifdef OMAP_HSMMC_USE_GPIO
Simon Glassc4d660d2017-07-04 13:31:19 -060084#if CONFIG_IS_ENABLED(DM_MMC)
Mugunthan V Na9d6a7e2015-09-28 12:56:30 +053085 struct gpio_desc cd_gpio; /* Change Detect GPIO */
86 struct gpio_desc wp_gpio; /* Write Protect GPIO */
Mugunthan V Na9d6a7e2015-09-28 12:56:30 +053087#else
Nikita Kiryanove874d5b2012-12-03 02:19:44 +000088 int cd_gpio;
Nikita Kiryanove3913f52012-12-03 02:19:47 +000089 int wp_gpio;
Pantelis Antoniouab769f22014-02-26 19:28:45 +020090#endif
Mugunthan V Na9d6a7e2015-09-28 12:56:30 +053091#endif
Kishon Vijay Abraham Ib5944812018-01-30 16:01:32 +010092#if CONFIG_IS_ENABLED(DM_MMC)
Jean-Jacques Hiblot8fc238b2018-01-30 16:01:33 +010093 enum bus_mode mode;
Kishon Vijay Abraham Ib5944812018-01-30 16:01:32 +010094#endif
Kishon Vijay Abraham If0d53e82017-09-21 16:51:34 +020095 u8 controller_flags;
Jean-Jacques Hiblot27a4b3b2018-02-23 10:40:18 +010096#ifdef CONFIG_MMC_OMAP_HS_ADMA
Kishon Vijay Abraham If0d53e82017-09-21 16:51:34 +020097 struct omap_hsmmc_adma_desc *adma_desc_table;
98 uint desc_slot;
99#endif
Kishon Vijay Abraham I2d28eed2018-01-30 16:01:41 +0100100 const char *hw_rev;
Jean-Jacques Hiblot04f9f8b2018-01-30 16:01:46 +0100101 struct udevice *pbias_supply;
102 uint signal_voltage;
Kishon Vijay Abraham I33c1d772018-01-30 16:01:40 +0100103#ifdef CONFIG_IODELAY_RECALIBRATION
104 struct omap_hsmmc_pinctrl_state *default_pinctrl_state;
105 struct omap_hsmmc_pinctrl_state *hs_pinctrl_state;
106 struct omap_hsmmc_pinctrl_state *hs200_1_8v_pinctrl_state;
107 struct omap_hsmmc_pinctrl_state *ddr_1_8v_pinctrl_state;
108 struct omap_hsmmc_pinctrl_state *sdr12_pinctrl_state;
109 struct omap_hsmmc_pinctrl_state *sdr25_pinctrl_state;
110 struct omap_hsmmc_pinctrl_state *ddr50_pinctrl_state;
111 struct omap_hsmmc_pinctrl_state *sdr50_pinctrl_state;
112 struct omap_hsmmc_pinctrl_state *sdr104_pinctrl_state;
113#endif
114};
115
116struct omap_mmc_of_data {
117 u8 controller_flags;
Nikita Kiryanovcc22b0c2012-12-03 02:19:43 +0000118};
119
Jean-Jacques Hiblot27a4b3b2018-02-23 10:40:18 +0100120#ifdef CONFIG_MMC_OMAP_HS_ADMA
Kishon Vijay Abraham If0d53e82017-09-21 16:51:34 +0200121struct omap_hsmmc_adma_desc {
122 u8 attr;
123 u8 reserved;
124 u16 len;
125 u32 addr;
126};
127
128#define ADMA_MAX_LEN 63488
129
130/* Decriptor table defines */
131#define ADMA_DESC_ATTR_VALID BIT(0)
132#define ADMA_DESC_ATTR_END BIT(1)
133#define ADMA_DESC_ATTR_INT BIT(2)
134#define ADMA_DESC_ATTR_ACT1 BIT(4)
135#define ADMA_DESC_ATTR_ACT2 BIT(5)
136
137#define ADMA_DESC_TRANSFER_DATA ADMA_DESC_ATTR_ACT2
138#define ADMA_DESC_LINK_DESC (ADMA_DESC_ATTR_ACT1 | ADMA_DESC_ATTR_ACT2)
139#endif
140
Nishanth Menoneb9a28f2010-11-19 11:18:12 -0500141/* If we fail after 1 second wait, something is really bad */
142#define MAX_RETRY_MS 1000
Jean-Jacques Hiblota4efd732018-01-30 16:01:37 +0100143#define MMC_TIMEOUT_MS 20
Nishanth Menoneb9a28f2010-11-19 11:18:12 -0500144
Kishon Vijay Abraham If0d53e82017-09-21 16:51:34 +0200145/* DMA transfers can take a long time if a lot a data is transferred.
146 * The timeout must take in account the amount of data. Let's assume
147 * that the time will never exceed 333 ms per MB (in other word we assume
148 * that the bandwidth is always above 3MB/s).
149 */
150#define DMA_TIMEOUT_PER_MB 333
Kishon Vijay Abraham Ib5944812018-01-30 16:01:32 +0100151#define OMAP_HSMMC_SUPPORTS_DUAL_VOLT BIT(0)
152#define OMAP_HSMMC_NO_1_8_V BIT(1)
Kishon Vijay Abraham If0d53e82017-09-21 16:51:34 +0200153#define OMAP_HSMMC_USE_ADMA BIT(2)
Kishon Vijay Abraham I33c1d772018-01-30 16:01:40 +0100154#define OMAP_HSMMC_REQUIRE_IODELAY BIT(3)
Kishon Vijay Abraham If0d53e82017-09-21 16:51:34 +0200155
Sricharan933efe62011-11-15 09:49:53 -0500156static int mmc_read_data(struct hsmmc *mmc_base, char *buf, unsigned int size);
157static int mmc_write_data(struct hsmmc *mmc_base, const char *buf,
158 unsigned int siz);
Jean-Jacques Hiblot5baf5432018-01-30 16:01:30 +0100159static void omap_hsmmc_start_clock(struct hsmmc *mmc_base);
160static void omap_hsmmc_stop_clock(struct hsmmc *mmc_base);
Jean-Jacques Hiblot14761ca2018-01-30 16:01:35 +0100161static void mmc_reset_controller_fsm(struct hsmmc *mmc_base, u32 bit);
Balaji T K14fa2dd2011-09-08 06:34:57 +0000162
Jean-Jacques Hiblotae000e22017-03-22 16:00:31 +0100163static inline struct omap_hsmmc_data *omap_hsmmc_get_data(struct mmc *mmc)
164{
Simon Glassc4d660d2017-07-04 13:31:19 -0600165#if CONFIG_IS_ENABLED(DM_MMC)
Jean-Jacques Hiblotae000e22017-03-22 16:00:31 +0100166 return dev_get_priv(mmc->dev);
167#else
168 return (struct omap_hsmmc_data *)mmc->priv;
169#endif
170}
Jean-Jacques Hiblot3d673ff2017-03-22 16:00:33 +0100171static inline struct mmc_config *omap_hsmmc_get_cfg(struct mmc *mmc)
172{
Simon Glassc4d660d2017-07-04 13:31:19 -0600173#if CONFIG_IS_ENABLED(DM_MMC)
Jean-Jacques Hiblot3d673ff2017-03-22 16:00:33 +0100174 struct omap_hsmmc_plat *plat = dev_get_platdata(mmc->dev);
175 return &plat->cfg;
176#else
177 return &((struct omap_hsmmc_data *)mmc->priv)->cfg;
178#endif
179}
Jean-Jacques Hiblotae000e22017-03-22 16:00:31 +0100180
Simon Glassc4d660d2017-07-04 13:31:19 -0600181#if defined(OMAP_HSMMC_USE_GPIO) && !CONFIG_IS_ENABLED(DM_MMC)
Nikita Kiryanove874d5b2012-12-03 02:19:44 +0000182static int omap_mmc_setup_gpio_in(int gpio, const char *label)
183{
Simon Glass5915a2a2014-10-22 21:37:09 -0600184 int ret;
185
186#ifndef CONFIG_DM_GPIO
Nikita Kiryanove874d5b2012-12-03 02:19:44 +0000187 if (!gpio_is_valid(gpio))
188 return -1;
Simon Glass5915a2a2014-10-22 21:37:09 -0600189#endif
190 ret = gpio_request(gpio, label);
191 if (ret)
192 return ret;
Nikita Kiryanove874d5b2012-12-03 02:19:44 +0000193
Simon Glass5915a2a2014-10-22 21:37:09 -0600194 ret = gpio_direction_input(gpio);
195 if (ret)
196 return ret;
Nikita Kiryanove874d5b2012-12-03 02:19:44 +0000197
198 return gpio;
199}
Nikita Kiryanove874d5b2012-12-03 02:19:44 +0000200#endif
201
Jeroen Hofstee750121c2014-07-12 21:24:08 +0200202static unsigned char mmc_board_init(struct mmc *mmc)
Sukumar Ghoraide941242010-09-18 20:32:33 -0700203{
Sukumar Ghoraide941242010-09-18 20:32:33 -0700204#if defined(CONFIG_OMAP34XX)
Jean-Jacques Hiblot3d673ff2017-03-22 16:00:33 +0100205 struct mmc_config *cfg = omap_hsmmc_get_cfg(mmc);
Sukumar Ghoraide941242010-09-18 20:32:33 -0700206 t2_t *t2_base = (t2_t *)T2_BASE;
207 struct prcm *prcm_base = (struct prcm *)PRCM_BASE;
Grazvydas Ignotasb1e725f2012-03-19 03:50:53 +0000208 u32 pbias_lite;
Adam Ford6aca17c2017-02-06 11:31:43 -0600209#ifdef CONFIG_MMC_OMAP36XX_PINS
210 u32 wkup_ctrl = readl(OMAP34XX_CTRL_WKUP_CTRL);
211#endif
Sukumar Ghoraide941242010-09-18 20:32:33 -0700212
Grazvydas Ignotasb1e725f2012-03-19 03:50:53 +0000213 pbias_lite = readl(&t2_base->pbias_lite);
214 pbias_lite &= ~(PBIASLITEPWRDNZ1 | PBIASLITEPWRDNZ0);
Albert ARIBAUD \(3ADEV\)5bfdd1f2015-01-16 09:09:50 +0100215#ifdef CONFIG_TARGET_OMAP3_CAIRO
216 /* for cairo board, we need to set up 1.8 Volt bias level on MMC1 */
217 pbias_lite &= ~PBIASLITEVMODE0;
218#endif
Adam Ford03190a72018-09-05 04:11:08 -0500219#ifdef CONFIG_TARGET_OMAP3_LOGIC
220 /* For Logic PD board, 1.8V bias to go enable gpio127 for mmc_cd */
221 pbias_lite &= ~PBIASLITEVMODE1;
222#endif
Adam Ford6aca17c2017-02-06 11:31:43 -0600223#ifdef CONFIG_MMC_OMAP36XX_PINS
224 if (get_cpu_family() == CPU_OMAP36XX) {
225 /* Disable extended drain IO before changing PBIAS */
226 wkup_ctrl &= ~OMAP34XX_CTRL_WKUP_CTRL_GPIO_IO_PWRDNZ;
227 writel(wkup_ctrl, OMAP34XX_CTRL_WKUP_CTRL);
228 }
229#endif
Grazvydas Ignotasb1e725f2012-03-19 03:50:53 +0000230 writel(pbias_lite, &t2_base->pbias_lite);
Paul Kocialkowskiaac54502014-11-08 20:55:47 +0100231
Grazvydas Ignotasb1e725f2012-03-19 03:50:53 +0000232 writel(pbias_lite | PBIASLITEPWRDNZ1 |
Sukumar Ghoraide941242010-09-18 20:32:33 -0700233 PBIASSPEEDCTRL0 | PBIASLITEPWRDNZ0,
234 &t2_base->pbias_lite);
235
Adam Ford6aca17c2017-02-06 11:31:43 -0600236#ifdef CONFIG_MMC_OMAP36XX_PINS
237 if (get_cpu_family() == CPU_OMAP36XX)
238 /* Enable extended drain IO after changing PBIAS */
239 writel(wkup_ctrl |
240 OMAP34XX_CTRL_WKUP_CTRL_GPIO_IO_PWRDNZ,
241 OMAP34XX_CTRL_WKUP_CTRL);
242#endif
Sukumar Ghoraide941242010-09-18 20:32:33 -0700243 writel(readl(&t2_base->devconf0) | MMCSDIO1ADPCLKISEL,
244 &t2_base->devconf0);
245
246 writel(readl(&t2_base->devconf1) | MMCSDIO2ADPCLKISEL,
247 &t2_base->devconf1);
248
Jonathan Solnitbbbc1ae2012-02-24 11:30:18 +0000249 /* Change from default of 52MHz to 26MHz if necessary */
Jean-Jacques Hiblot3d673ff2017-03-22 16:00:33 +0100250 if (!(cfg->host_caps & MMC_MODE_HS_52MHz))
Jonathan Solnitbbbc1ae2012-02-24 11:30:18 +0000251 writel(readl(&t2_base->ctl_prog_io1) & ~CTLPROGIO1SPEEDCTRL,
252 &t2_base->ctl_prog_io1);
253
Sukumar Ghoraide941242010-09-18 20:32:33 -0700254 writel(readl(&prcm_base->fclken1_core) |
255 EN_MMC1 | EN_MMC2 | EN_MMC3,
256 &prcm_base->fclken1_core);
257
258 writel(readl(&prcm_base->iclken1_core) |
259 EN_MMC1 | EN_MMC2 | EN_MMC3,
260 &prcm_base->iclken1_core);
261#endif
262
Jean-Jacques Hiblot04f9f8b2018-01-30 16:01:46 +0100263#if (defined(CONFIG_OMAP54XX) || defined(CONFIG_OMAP44XX)) &&\
264 !CONFIG_IS_ENABLED(DM_REGULATOR)
Balaji T K14fa2dd2011-09-08 06:34:57 +0000265 /* PBIAS config needed for MMC1 only */
Jean-Jacques Hiblotdc091272017-03-22 16:00:32 +0100266 if (mmc_get_blk_desc(mmc)->devnum == 0)
Faiz Abbasd2c05f52019-04-05 14:18:46 +0530267 vmmc_pbias_config(LDO_VOLT_3V3);
Balaji T Kdd23e592012-03-12 02:25:49 +0000268#endif
Sukumar Ghoraide941242010-09-18 20:32:33 -0700269
270 return 0;
271}
272
Sricharan933efe62011-11-15 09:49:53 -0500273void mmc_init_stream(struct hsmmc *mmc_base)
Sukumar Ghoraide941242010-09-18 20:32:33 -0700274{
Nishanth Menoneb9a28f2010-11-19 11:18:12 -0500275 ulong start;
Sukumar Ghoraide941242010-09-18 20:32:33 -0700276
277 writel(readl(&mmc_base->con) | INIT_INITSTREAM, &mmc_base->con);
278
279 writel(MMC_CMD0, &mmc_base->cmd);
Nishanth Menoneb9a28f2010-11-19 11:18:12 -0500280 start = get_timer(0);
281 while (!(readl(&mmc_base->stat) & CC_MASK)) {
282 if (get_timer(0) - start > MAX_RETRY_MS) {
283 printf("%s: timedout waiting for cc!\n", __func__);
284 return;
285 }
286 }
Sukumar Ghoraide941242010-09-18 20:32:33 -0700287 writel(CC_MASK, &mmc_base->stat)
288 ;
289 writel(MMC_CMD0, &mmc_base->cmd)
290 ;
Nishanth Menoneb9a28f2010-11-19 11:18:12 -0500291 start = get_timer(0);
292 while (!(readl(&mmc_base->stat) & CC_MASK)) {
293 if (get_timer(0) - start > MAX_RETRY_MS) {
294 printf("%s: timedout waiting for cc2!\n", __func__);
295 return;
296 }
297 }
Sukumar Ghoraide941242010-09-18 20:32:33 -0700298 writel(readl(&mmc_base->con) & ~INIT_INITSTREAM, &mmc_base->con);
299}
300
Kishon Vijay Abraham Ib5944812018-01-30 16:01:32 +0100301#if CONFIG_IS_ENABLED(DM_MMC)
Kishon Vijay Abraham I33c1d772018-01-30 16:01:40 +0100302#ifdef CONFIG_IODELAY_RECALIBRATION
303static void omap_hsmmc_io_recalibrate(struct mmc *mmc)
304{
305 struct omap_hsmmc_data *priv = omap_hsmmc_get_data(mmc);
306 struct omap_hsmmc_pinctrl_state *pinctrl_state;
307
308 switch (priv->mode) {
309 case MMC_HS_200:
310 pinctrl_state = priv->hs200_1_8v_pinctrl_state;
311 break;
312 case UHS_SDR104:
313 pinctrl_state = priv->sdr104_pinctrl_state;
314 break;
315 case UHS_SDR50:
316 pinctrl_state = priv->sdr50_pinctrl_state;
317 break;
318 case UHS_DDR50:
319 pinctrl_state = priv->ddr50_pinctrl_state;
320 break;
321 case UHS_SDR25:
322 pinctrl_state = priv->sdr25_pinctrl_state;
323 break;
324 case UHS_SDR12:
325 pinctrl_state = priv->sdr12_pinctrl_state;
326 break;
327 case SD_HS:
328 case MMC_HS:
329 case MMC_HS_52:
330 pinctrl_state = priv->hs_pinctrl_state;
331 break;
332 case MMC_DDR_52:
333 pinctrl_state = priv->ddr_1_8v_pinctrl_state;
334 default:
335 pinctrl_state = priv->default_pinctrl_state;
336 break;
337 }
338
Jean-Jacques Hiblotbcc6bd82018-01-30 16:01:42 +0100339 if (!pinctrl_state)
340 pinctrl_state = priv->default_pinctrl_state;
341
Kishon Vijay Abraham I33c1d772018-01-30 16:01:40 +0100342 if (priv->controller_flags & OMAP_HSMMC_REQUIRE_IODELAY) {
343 if (pinctrl_state->iodelay)
344 late_recalibrate_iodelay(pinctrl_state->padconf,
345 pinctrl_state->npads,
346 pinctrl_state->iodelay,
347 pinctrl_state->niodelays);
348 else
349 do_set_mux32((*ctrl)->control_padconf_core_base,
350 pinctrl_state->padconf,
351 pinctrl_state->npads);
352 }
353}
354#endif
Jean-Jacques Hiblot8fc238b2018-01-30 16:01:33 +0100355static void omap_hsmmc_set_timing(struct mmc *mmc)
356{
357 u32 val;
358 struct hsmmc *mmc_base;
359 struct omap_hsmmc_data *priv = omap_hsmmc_get_data(mmc);
360
361 mmc_base = priv->base_addr;
362
Kishon Vijay Abraham I33c1d772018-01-30 16:01:40 +0100363 omap_hsmmc_stop_clock(mmc_base);
Jean-Jacques Hiblot8fc238b2018-01-30 16:01:33 +0100364 val = readl(&mmc_base->ac12);
365 val &= ~AC12_UHSMC_MASK;
366 priv->mode = mmc->selected_mode;
367
Kishon Vijay Abraham I9b3fc212018-01-30 16:01:34 +0100368 if (mmc_is_mode_ddr(priv->mode))
369 writel(readl(&mmc_base->con) | DDR, &mmc_base->con);
370 else
371 writel(readl(&mmc_base->con) & ~DDR, &mmc_base->con);
372
Jean-Jacques Hiblot8fc238b2018-01-30 16:01:33 +0100373 switch (priv->mode) {
374 case MMC_HS_200:
375 case UHS_SDR104:
376 val |= AC12_UHSMC_SDR104;
377 break;
378 case UHS_SDR50:
379 val |= AC12_UHSMC_SDR50;
380 break;
381 case MMC_DDR_52:
382 case UHS_DDR50:
383 val |= AC12_UHSMC_DDR50;
384 break;
385 case SD_HS:
386 case MMC_HS_52:
387 case UHS_SDR25:
388 val |= AC12_UHSMC_SDR25;
389 break;
390 case MMC_LEGACY:
391 case MMC_HS:
392 case SD_LEGACY:
393 case UHS_SDR12:
394 val |= AC12_UHSMC_SDR12;
395 break;
396 default:
397 val |= AC12_UHSMC_RES;
398 break;
399 }
400 writel(val, &mmc_base->ac12);
Kishon Vijay Abraham I33c1d772018-01-30 16:01:40 +0100401
402#ifdef CONFIG_IODELAY_RECALIBRATION
403 omap_hsmmc_io_recalibrate(mmc);
404#endif
405 omap_hsmmc_start_clock(mmc_base);
Jean-Jacques Hiblot8fc238b2018-01-30 16:01:33 +0100406}
407
Jean-Jacques Hiblot04f9f8b2018-01-30 16:01:46 +0100408static void omap_hsmmc_conf_bus_power(struct mmc *mmc, uint signal_voltage)
Kishon Vijay Abraham Ib5944812018-01-30 16:01:32 +0100409{
410 struct hsmmc *mmc_base;
411 struct omap_hsmmc_data *priv = omap_hsmmc_get_data(mmc);
Jean-Jacques Hiblot04f9f8b2018-01-30 16:01:46 +0100412 u32 hctl, ac12;
Kishon Vijay Abraham Ib5944812018-01-30 16:01:32 +0100413
414 mmc_base = priv->base_addr;
415
Jean-Jacques Hiblot04f9f8b2018-01-30 16:01:46 +0100416 hctl = readl(&mmc_base->hctl) & ~SDVS_MASK;
417 ac12 = readl(&mmc_base->ac12) & ~AC12_V1V8_SIGEN;
Kishon Vijay Abraham Ib5944812018-01-30 16:01:32 +0100418
Jean-Jacques Hiblot04f9f8b2018-01-30 16:01:46 +0100419 switch (signal_voltage) {
420 case MMC_SIGNAL_VOLTAGE_330:
Faiz Abbasd2c05f52019-04-05 14:18:46 +0530421 hctl |= SDVS_3V3;
Kishon Vijay Abraham Ib5944812018-01-30 16:01:32 +0100422 break;
Jean-Jacques Hiblot04f9f8b2018-01-30 16:01:46 +0100423 case MMC_SIGNAL_VOLTAGE_180:
424 hctl |= SDVS_1V8;
425 ac12 |= AC12_V1V8_SIGEN;
Kishon Vijay Abraham Ib5944812018-01-30 16:01:32 +0100426 break;
427 }
428
Jean-Jacques Hiblot04f9f8b2018-01-30 16:01:46 +0100429 writel(hctl, &mmc_base->hctl);
430 writel(ac12, &mmc_base->ac12);
Kishon Vijay Abraham Ib5944812018-01-30 16:01:32 +0100431}
432
Jean-Jacques Hiblot04f9f8b2018-01-30 16:01:46 +0100433static int omap_hsmmc_wait_dat0(struct udevice *dev, int state, int timeout)
434{
435 int ret = -ETIMEDOUT;
436 u32 con;
437 bool dat0_high;
438 bool target_dat0_high = !!state;
439 struct omap_hsmmc_data *priv = dev_get_priv(dev);
440 struct hsmmc *mmc_base = priv->base_addr;
441
442 con = readl(&mmc_base->con);
443 writel(con | CON_CLKEXTFREE | CON_PADEN, &mmc_base->con);
444
445 timeout = DIV_ROUND_UP(timeout, 10); /* check every 10 us. */
446 while (timeout--) {
447 dat0_high = !!(readl(&mmc_base->pstate) & PSTATE_DLEV_DAT0);
448 if (dat0_high == target_dat0_high) {
449 ret = 0;
450 break;
451 }
452 udelay(10);
453 }
454 writel(con, &mmc_base->con);
455
456 return ret;
457}
Jean-Jacques Hiblot04f9f8b2018-01-30 16:01:46 +0100458
459#if CONFIG_IS_ENABLED(MMC_IO_VOLTAGE)
460#if CONFIG_IS_ENABLED(DM_REGULATOR)
461static int omap_hsmmc_set_io_regulator(struct mmc *mmc, int mV)
462{
463 int ret = 0;
464 int uV = mV * 1000;
465
466 struct omap_hsmmc_data *priv = omap_hsmmc_get_data(mmc);
467
468 if (!mmc->vqmmc_supply)
469 return 0;
470
471 /* Disable PBIAS */
Lokesh Vutlad3de3852019-01-11 15:15:52 +0530472 ret = regulator_set_enable_if_allowed(priv->pbias_supply, false);
473 if (ret)
Jean-Jacques Hiblot04f9f8b2018-01-30 16:01:46 +0100474 return ret;
475
476 /* Turn off IO voltage */
Lokesh Vutlad3de3852019-01-11 15:15:52 +0530477 ret = regulator_set_enable_if_allowed(mmc->vqmmc_supply, false);
478 if (ret)
Jean-Jacques Hiblot04f9f8b2018-01-30 16:01:46 +0100479 return ret;
480 /* Program a new IO voltage value */
481 ret = regulator_set_value(mmc->vqmmc_supply, uV);
482 if (ret)
483 return ret;
484 /* Turn on IO voltage */
Lokesh Vutlad3de3852019-01-11 15:15:52 +0530485 ret = regulator_set_enable_if_allowed(mmc->vqmmc_supply, true);
486 if (ret)
Jean-Jacques Hiblot04f9f8b2018-01-30 16:01:46 +0100487 return ret;
488
489 /* Program PBIAS voltage*/
490 ret = regulator_set_value(priv->pbias_supply, uV);
491 if (ret && ret != -ENOSYS)
492 return ret;
493 /* Enable PBIAS */
Lokesh Vutlad3de3852019-01-11 15:15:52 +0530494 ret = regulator_set_enable_if_allowed(priv->pbias_supply, true);
495 if (ret)
Jean-Jacques Hiblot04f9f8b2018-01-30 16:01:46 +0100496 return ret;
497
498 return 0;
499}
500#endif
501
502static int omap_hsmmc_set_signal_voltage(struct mmc *mmc)
503{
504 struct omap_hsmmc_data *priv = omap_hsmmc_get_data(mmc);
505 struct hsmmc *mmc_base = priv->base_addr;
506 int mv = mmc_voltage_to_mv(mmc->signal_voltage);
507 u32 capa_mask;
508 __maybe_unused u8 palmas_ldo_volt;
509 u32 val;
510
511 if (mv < 0)
512 return -EINVAL;
513
514 if (mmc->signal_voltage == MMC_SIGNAL_VOLTAGE_330) {
Faiz Abbasd2c05f52019-04-05 14:18:46 +0530515 mv = 3300;
516 capa_mask = VS33_3V3SUP;
517 palmas_ldo_volt = LDO_VOLT_3V3;
Jean-Jacques Hiblot04f9f8b2018-01-30 16:01:46 +0100518 } else if (mmc->signal_voltage == MMC_SIGNAL_VOLTAGE_180) {
519 capa_mask = VS18_1V8SUP;
520 palmas_ldo_volt = LDO_VOLT_1V8;
521 } else {
522 return -EOPNOTSUPP;
523 }
524
525 val = readl(&mmc_base->capa);
526 if (!(val & capa_mask))
527 return -EOPNOTSUPP;
528
529 priv->signal_voltage = mmc->signal_voltage;
530
531 omap_hsmmc_conf_bus_power(mmc, mmc->signal_voltage);
532
533#if CONFIG_IS_ENABLED(DM_REGULATOR)
534 return omap_hsmmc_set_io_regulator(mmc, mv);
535#elif (defined(CONFIG_OMAP54XX) || defined(CONFIG_OMAP44XX)) && \
536 defined(CONFIG_PALMAS_POWER)
537 if (mmc_get_blk_desc(mmc)->devnum == 0)
538 vmmc_pbias_config(palmas_ldo_volt);
539 return 0;
540#else
541 return 0;
542#endif
543}
544#endif
545
546static uint32_t omap_hsmmc_set_capabilities(struct mmc *mmc)
Kishon Vijay Abraham Ib5944812018-01-30 16:01:32 +0100547{
548 struct hsmmc *mmc_base;
549 struct omap_hsmmc_data *priv = omap_hsmmc_get_data(mmc);
550 u32 val;
551
552 mmc_base = priv->base_addr;
553 val = readl(&mmc_base->capa);
554
555 if (priv->controller_flags & OMAP_HSMMC_SUPPORTS_DUAL_VOLT) {
Faiz Abbasd2c05f52019-04-05 14:18:46 +0530556 val |= (VS33_3V3SUP | VS18_1V8SUP);
Kishon Vijay Abraham Ib5944812018-01-30 16:01:32 +0100557 } else if (priv->controller_flags & OMAP_HSMMC_NO_1_8_V) {
Faiz Abbasd2c05f52019-04-05 14:18:46 +0530558 val |= VS33_3V3SUP;
Kishon Vijay Abraham Ib5944812018-01-30 16:01:32 +0100559 val &= ~VS18_1V8SUP;
Kishon Vijay Abraham Ib5944812018-01-30 16:01:32 +0100560 } else {
561 val |= VS18_1V8SUP;
Faiz Abbasd2c05f52019-04-05 14:18:46 +0530562 val &= ~VS33_3V3SUP;
Kishon Vijay Abraham Ib5944812018-01-30 16:01:32 +0100563 }
564
565 writel(val, &mmc_base->capa);
Jean-Jacques Hiblot04f9f8b2018-01-30 16:01:46 +0100566
567 return val;
Kishon Vijay Abraham Ib5944812018-01-30 16:01:32 +0100568}
Jean-Jacques Hiblot14761ca2018-01-30 16:01:35 +0100569
570#ifdef MMC_SUPPORTS_TUNING
571static void omap_hsmmc_disable_tuning(struct mmc *mmc)
572{
573 struct hsmmc *mmc_base;
574 struct omap_hsmmc_data *priv = omap_hsmmc_get_data(mmc);
575 u32 val;
576
577 mmc_base = priv->base_addr;
578 val = readl(&mmc_base->ac12);
579 val &= ~(AC12_SCLK_SEL);
580 writel(val, &mmc_base->ac12);
581
582 val = readl(&mmc_base->dll);
583 val &= ~(DLL_FORCE_VALUE | DLL_SWT);
584 writel(val, &mmc_base->dll);
585}
586
587static void omap_hsmmc_set_dll(struct mmc *mmc, int count)
588{
589 int i;
590 struct hsmmc *mmc_base;
591 struct omap_hsmmc_data *priv = omap_hsmmc_get_data(mmc);
592 u32 val;
593
594 mmc_base = priv->base_addr;
595 val = readl(&mmc_base->dll);
596 val |= DLL_FORCE_VALUE;
597 val &= ~(DLL_FORCE_SR_C_MASK << DLL_FORCE_SR_C_SHIFT);
598 val |= (count << DLL_FORCE_SR_C_SHIFT);
599 writel(val, &mmc_base->dll);
600
601 val |= DLL_CALIB;
602 writel(val, &mmc_base->dll);
603 for (i = 0; i < 1000; i++) {
604 if (readl(&mmc_base->dll) & DLL_CALIB)
605 break;
606 }
607 val &= ~DLL_CALIB;
608 writel(val, &mmc_base->dll);
609}
610
611static int omap_hsmmc_execute_tuning(struct udevice *dev, uint opcode)
612{
613 struct omap_hsmmc_data *priv = dev_get_priv(dev);
614 struct mmc_uclass_priv *upriv = dev_get_uclass_priv(dev);
615 struct mmc *mmc = upriv->mmc;
616 struct hsmmc *mmc_base;
617 u32 val;
618 u8 cur_match, prev_match = 0;
619 int ret;
620 u32 phase_delay = 0;
621 u32 start_window = 0, max_window = 0;
622 u32 length = 0, max_len = 0;
Faiz Abbas351a4aa2019-01-30 18:08:42 +0530623 bool single_point_failure = false;
624 struct udevice *thermal_dev;
625 int temperature;
626 int i;
Jean-Jacques Hiblot14761ca2018-01-30 16:01:35 +0100627
628 mmc_base = priv->base_addr;
629 val = readl(&mmc_base->capa2);
630
631 /* clock tuning is not needed for upto 52MHz */
632 if (!((mmc->selected_mode == MMC_HS_200) ||
633 (mmc->selected_mode == UHS_SDR104) ||
634 ((mmc->selected_mode == UHS_SDR50) && (val & CAPA2_TSDR50))))
635 return 0;
636
Faiz Abbas351a4aa2019-01-30 18:08:42 +0530637 ret = uclass_first_device(UCLASS_THERMAL, &thermal_dev);
638 if (ret) {
639 printf("Couldn't get thermal device for tuning\n");
640 return ret;
641 }
642 ret = thermal_get_temp(thermal_dev, &temperature);
643 if (ret) {
644 printf("Couldn't get temperature for tuning\n");
645 return ret;
646 }
Jean-Jacques Hiblot14761ca2018-01-30 16:01:35 +0100647 val = readl(&mmc_base->dll);
648 val |= DLL_SWT;
649 writel(val, &mmc_base->dll);
Faiz Abbas351a4aa2019-01-30 18:08:42 +0530650
651 /*
652 * Stage 1: Search for a maximum pass window ignoring any
653 * any single point failures. If the tuning value ends up
654 * near it, move away from it in stage 2 below
655 */
Jean-Jacques Hiblot14761ca2018-01-30 16:01:35 +0100656 while (phase_delay <= MAX_PHASE_DELAY) {
657 omap_hsmmc_set_dll(mmc, phase_delay);
658
659 cur_match = !mmc_send_tuning(mmc, opcode, NULL);
660
661 if (cur_match) {
662 if (prev_match) {
663 length++;
Faiz Abbas351a4aa2019-01-30 18:08:42 +0530664 } else if (single_point_failure) {
665 /* ignore single point failure */
666 length++;
667 single_point_failure = false;
Jean-Jacques Hiblot14761ca2018-01-30 16:01:35 +0100668 } else {
669 start_window = phase_delay;
670 length = 1;
671 }
Faiz Abbas351a4aa2019-01-30 18:08:42 +0530672 } else {
673 single_point_failure = prev_match;
Jean-Jacques Hiblot14761ca2018-01-30 16:01:35 +0100674 }
675
676 if (length > max_len) {
677 max_window = start_window;
678 max_len = length;
679 }
680
681 prev_match = cur_match;
682 phase_delay += 4;
683 }
684
685 if (!max_len) {
686 ret = -EIO;
687 goto tuning_error;
688 }
689
690 val = readl(&mmc_base->ac12);
691 if (!(val & AC12_SCLK_SEL)) {
692 ret = -EIO;
693 goto tuning_error;
694 }
Faiz Abbas351a4aa2019-01-30 18:08:42 +0530695 /*
696 * Assign tuning value as a ratio of maximum pass window based
697 * on temperature
698 */
699 if (temperature < -20000)
700 phase_delay = min(max_window + 4 * max_len - 24,
701 max_window +
702 DIV_ROUND_UP(13 * max_len, 16) * 4);
703 else if (temperature < 20000)
704 phase_delay = max_window + DIV_ROUND_UP(9 * max_len, 16) * 4;
705 else if (temperature < 40000)
706 phase_delay = max_window + DIV_ROUND_UP(8 * max_len, 16) * 4;
707 else if (temperature < 70000)
708 phase_delay = max_window + DIV_ROUND_UP(7 * max_len, 16) * 4;
709 else if (temperature < 90000)
710 phase_delay = max_window + DIV_ROUND_UP(5 * max_len, 16) * 4;
711 else if (temperature < 120000)
712 phase_delay = max_window + DIV_ROUND_UP(4 * max_len, 16) * 4;
713 else
714 phase_delay = max_window + DIV_ROUND_UP(3 * max_len, 16) * 4;
Jean-Jacques Hiblot14761ca2018-01-30 16:01:35 +0100715
Faiz Abbas351a4aa2019-01-30 18:08:42 +0530716 /*
717 * Stage 2: Search for a single point failure near the chosen tuning
718 * value in two steps. First in the +3 to +10 range and then in the
719 * +2 to -10 range. If found, move away from it in the appropriate
720 * direction by the appropriate amount depending on the temperature.
721 */
722 for (i = 3; i <= 10; i++) {
723 omap_hsmmc_set_dll(mmc, phase_delay + i);
724 if (mmc_send_tuning(mmc, opcode, NULL)) {
725 if (temperature < 10000)
726 phase_delay += i + 6;
727 else if (temperature < 20000)
728 phase_delay += i - 12;
729 else if (temperature < 70000)
730 phase_delay += i - 8;
731 else if (temperature < 90000)
732 phase_delay += i - 6;
733 else
734 phase_delay += i - 6;
735
736 goto single_failure_found;
737 }
738 }
739
740 for (i = 2; i >= -10; i--) {
741 omap_hsmmc_set_dll(mmc, phase_delay + i);
742 if (mmc_send_tuning(mmc, opcode, NULL)) {
743 if (temperature < 10000)
744 phase_delay += i + 12;
745 else if (temperature < 20000)
746 phase_delay += i + 8;
747 else if (temperature < 70000)
748 phase_delay += i + 8;
749 else if (temperature < 90000)
750 phase_delay += i + 10;
751 else
752 phase_delay += i + 12;
753
754 goto single_failure_found;
755 }
756 }
757
758single_failure_found:
759
Jean-Jacques Hiblot14761ca2018-01-30 16:01:35 +0100760 omap_hsmmc_set_dll(mmc, phase_delay);
761
762 mmc_reset_controller_fsm(mmc_base, SYSCTL_SRD);
763 mmc_reset_controller_fsm(mmc_base, SYSCTL_SRC);
764
765 return 0;
766
767tuning_error:
768
769 omap_hsmmc_disable_tuning(mmc);
770 mmc_reset_controller_fsm(mmc_base, SYSCTL_SRD);
771 mmc_reset_controller_fsm(mmc_base, SYSCTL_SRC);
772
773 return ret;
774}
775#endif
Kishon Vijay Abraham Ib5944812018-01-30 16:01:32 +0100776#endif
777
Jean-Jacques Hiblot2faa1a32018-01-30 16:01:36 +0100778static void mmc_enable_irq(struct mmc *mmc, struct mmc_cmd *cmd)
779{
780 struct omap_hsmmc_data *priv = omap_hsmmc_get_data(mmc);
781 struct hsmmc *mmc_base = priv->base_addr;
782 u32 irq_mask = INT_EN_MASK;
783
784 /*
785 * TODO: Errata i802 indicates only DCRC interrupts can occur during
786 * tuning procedure and DCRC should be disabled. But see occurences
787 * of DEB, CIE, CEB, CCRC interupts during tuning procedure. These
788 * interrupts occur along with BRR, so the data is actually in the
789 * buffer. It has to be debugged why these interrutps occur
790 */
791 if (cmd && mmc_is_tuning_cmd(cmd->cmdidx))
792 irq_mask &= ~(IE_DEB | IE_DCRC | IE_CIE | IE_CEB | IE_CCRC);
793
794 writel(irq_mask, &mmc_base->ie);
795}
796
Pantelis Antoniouab769f22014-02-26 19:28:45 +0200797static int omap_hsmmc_init_setup(struct mmc *mmc)
Sukumar Ghoraide941242010-09-18 20:32:33 -0700798{
Jean-Jacques Hiblotae000e22017-03-22 16:00:31 +0100799 struct omap_hsmmc_data *priv = omap_hsmmc_get_data(mmc);
Nikita Kiryanovcc22b0c2012-12-03 02:19:43 +0000800 struct hsmmc *mmc_base;
Sukumar Ghoraide941242010-09-18 20:32:33 -0700801 unsigned int reg_val;
802 unsigned int dsor;
Nishanth Menoneb9a28f2010-11-19 11:18:12 -0500803 ulong start;
Sukumar Ghoraide941242010-09-18 20:32:33 -0700804
Jean-Jacques Hiblotae000e22017-03-22 16:00:31 +0100805 mmc_base = priv->base_addr;
Balaji T K14fa2dd2011-09-08 06:34:57 +0000806 mmc_board_init(mmc);
Sukumar Ghoraide941242010-09-18 20:32:33 -0700807
808 writel(readl(&mmc_base->sysconfig) | MMC_SOFTRESET,
809 &mmc_base->sysconfig);
Nishanth Menoneb9a28f2010-11-19 11:18:12 -0500810 start = get_timer(0);
811 while ((readl(&mmc_base->sysstatus) & RESETDONE) == 0) {
812 if (get_timer(0) - start > MAX_RETRY_MS) {
813 printf("%s: timedout waiting for cc2!\n", __func__);
Jaehoon Chung915ffa52016-07-19 16:33:36 +0900814 return -ETIMEDOUT;
Nishanth Menoneb9a28f2010-11-19 11:18:12 -0500815 }
816 }
Sukumar Ghoraide941242010-09-18 20:32:33 -0700817 writel(readl(&mmc_base->sysctl) | SOFTRESETALL, &mmc_base->sysctl);
Nishanth Menoneb9a28f2010-11-19 11:18:12 -0500818 start = get_timer(0);
819 while ((readl(&mmc_base->sysctl) & SOFTRESETALL) != 0x0) {
820 if (get_timer(0) - start > MAX_RETRY_MS) {
821 printf("%s: timedout waiting for softresetall!\n",
822 __func__);
Jaehoon Chung915ffa52016-07-19 16:33:36 +0900823 return -ETIMEDOUT;
Nishanth Menoneb9a28f2010-11-19 11:18:12 -0500824 }
825 }
Jean-Jacques Hiblot27a4b3b2018-02-23 10:40:18 +0100826#ifdef CONFIG_MMC_OMAP_HS_ADMA
Kishon Vijay Abraham If0d53e82017-09-21 16:51:34 +0200827 reg_val = readl(&mmc_base->hl_hwinfo);
828 if (reg_val & MADMA_EN)
829 priv->controller_flags |= OMAP_HSMMC_USE_ADMA;
830#endif
Kishon Vijay Abraham Ib5944812018-01-30 16:01:32 +0100831
832#if CONFIG_IS_ENABLED(DM_MMC)
Jean-Jacques Hiblot04f9f8b2018-01-30 16:01:46 +0100833 reg_val = omap_hsmmc_set_capabilities(mmc);
Faiz Abbasd2c05f52019-04-05 14:18:46 +0530834 omap_hsmmc_conf_bus_power(mmc, (reg_val & VS33_3V3SUP) ?
Jean-Jacques Hiblot04f9f8b2018-01-30 16:01:46 +0100835 MMC_SIGNAL_VOLTAGE_330 : MMC_SIGNAL_VOLTAGE_180);
Kishon Vijay Abraham Ib5944812018-01-30 16:01:32 +0100836#else
Sukumar Ghoraide941242010-09-18 20:32:33 -0700837 writel(DTW_1_BITMODE | SDBP_PWROFF | SDVS_3V0, &mmc_base->hctl);
Faiz Abbasd2c05f52019-04-05 14:18:46 +0530838 writel(readl(&mmc_base->capa) | VS33_3V3SUP | VS18_1V8SUP,
Sukumar Ghoraide941242010-09-18 20:32:33 -0700839 &mmc_base->capa);
Kishon Vijay Abraham Ib5944812018-01-30 16:01:32 +0100840#endif
Sukumar Ghoraide941242010-09-18 20:32:33 -0700841
842 reg_val = readl(&mmc_base->con) & RESERVED_MASK;
843
844 writel(CTPL_MMC_SD | reg_val | WPP_ACTIVEHIGH | CDP_ACTIVEHIGH |
845 MIT_CTO | DW8_1_4BITMODE | MODE_FUNC | STR_BLOCK |
846 HR_NOHOSTRESP | INIT_NOINIT | NOOPENDRAIN, &mmc_base->con);
847
848 dsor = 240;
849 mmc_reg_out(&mmc_base->sysctl, (ICE_MASK | DTO_MASK | CEN_MASK),
Kishon Vijay Abraham I29171dc2017-09-21 16:51:36 +0200850 (ICE_STOP | DTO_15THDTO));
Sukumar Ghoraide941242010-09-18 20:32:33 -0700851 mmc_reg_out(&mmc_base->sysctl, ICE_MASK | CLKD_MASK,
852 (dsor << CLKD_OFFSET) | ICE_OSCILLATE);
Nishanth Menoneb9a28f2010-11-19 11:18:12 -0500853 start = get_timer(0);
854 while ((readl(&mmc_base->sysctl) & ICS_MASK) == ICS_NOTREADY) {
855 if (get_timer(0) - start > MAX_RETRY_MS) {
856 printf("%s: timedout waiting for ics!\n", __func__);
Jaehoon Chung915ffa52016-07-19 16:33:36 +0900857 return -ETIMEDOUT;
Nishanth Menoneb9a28f2010-11-19 11:18:12 -0500858 }
859 }
Sukumar Ghoraide941242010-09-18 20:32:33 -0700860 writel(readl(&mmc_base->sysctl) | CEN_ENABLE, &mmc_base->sysctl);
861
862 writel(readl(&mmc_base->hctl) | SDBP_PWRON, &mmc_base->hctl);
863
Jean-Jacques Hiblot2faa1a32018-01-30 16:01:36 +0100864 mmc_enable_irq(mmc, NULL);
Jean-Jacques Hiblot42182c92018-01-30 16:01:44 +0100865
866#if !CONFIG_IS_ENABLED(DM_MMC)
Sukumar Ghoraide941242010-09-18 20:32:33 -0700867 mmc_init_stream(mmc_base);
Jean-Jacques Hiblot42182c92018-01-30 16:01:44 +0100868#endif
Sukumar Ghoraide941242010-09-18 20:32:33 -0700869
870 return 0;
871}
872
Grazvydas Ignotas25c719e2012-03-19 12:12:06 +0000873/*
874 * MMC controller internal finite state machine reset
875 *
876 * Used to reset command or data internal state machines, using respectively
877 * SRC or SRD bit of SYSCTL register
878 */
879static void mmc_reset_controller_fsm(struct hsmmc *mmc_base, u32 bit)
880{
881 ulong start;
882
883 mmc_reg_out(&mmc_base->sysctl, bit, bit);
884
Oleksandr Tyshchenko61a6cc22013-08-06 13:44:16 +0300885 /*
886 * CMD(DAT) lines reset procedures are slightly different
887 * for OMAP3 and OMAP4(AM335x,OMAP5,DRA7xx).
888 * According to OMAP3 TRM:
889 * Set SRC(SRD) bit in MMCHS_SYSCTL register to 0x1 and wait until it
890 * returns to 0x0.
891 * According to OMAP4(AM335x,OMAP5,DRA7xx) TRMs, CMD(DATA) lines reset
892 * procedure steps must be as follows:
893 * 1. Initiate CMD(DAT) line reset by writing 0x1 to SRC(SRD) bit in
894 * MMCHS_SYSCTL register (SD_SYSCTL for AM335x).
895 * 2. Poll the SRC(SRD) bit until it is set to 0x1.
896 * 3. Wait until the SRC (SRD) bit returns to 0x0
897 * (reset procedure is completed).
898 */
899#if defined(CONFIG_OMAP44XX) || defined(CONFIG_OMAP54XX) || \
Nikita Kiryanovdce55b92015-07-30 23:56:20 +0300900 defined(CONFIG_AM33XX) || defined(CONFIG_AM43XX)
Oleksandr Tyshchenko61a6cc22013-08-06 13:44:16 +0300901 if (!(readl(&mmc_base->sysctl) & bit)) {
902 start = get_timer(0);
903 while (!(readl(&mmc_base->sysctl) & bit)) {
Jean-Jacques Hiblota4efd732018-01-30 16:01:37 +0100904 if (get_timer(0) - start > MMC_TIMEOUT_MS)
Oleksandr Tyshchenko61a6cc22013-08-06 13:44:16 +0300905 return;
906 }
907 }
908#endif
Grazvydas Ignotas25c719e2012-03-19 12:12:06 +0000909 start = get_timer(0);
910 while ((readl(&mmc_base->sysctl) & bit) != 0) {
911 if (get_timer(0) - start > MAX_RETRY_MS) {
912 printf("%s: timedout waiting for sysctl %x to clear\n",
913 __func__, bit);
914 return;
915 }
916 }
917}
Kishon Vijay Abraham If0d53e82017-09-21 16:51:34 +0200918
Jean-Jacques Hiblot27a4b3b2018-02-23 10:40:18 +0100919#ifdef CONFIG_MMC_OMAP_HS_ADMA
Kishon Vijay Abraham If0d53e82017-09-21 16:51:34 +0200920static void omap_hsmmc_adma_desc(struct mmc *mmc, char *buf, u16 len, bool end)
921{
922 struct omap_hsmmc_data *priv = omap_hsmmc_get_data(mmc);
923 struct omap_hsmmc_adma_desc *desc;
924 u8 attr;
925
926 desc = &priv->adma_desc_table[priv->desc_slot];
927
928 attr = ADMA_DESC_ATTR_VALID | ADMA_DESC_TRANSFER_DATA;
929 if (!end)
930 priv->desc_slot++;
931 else
932 attr |= ADMA_DESC_ATTR_END;
933
934 desc->len = len;
935 desc->addr = (u32)buf;
936 desc->reserved = 0;
937 desc->attr = attr;
938}
939
940static void omap_hsmmc_prepare_adma_table(struct mmc *mmc,
941 struct mmc_data *data)
942{
943 uint total_len = data->blocksize * data->blocks;
944 uint desc_count = DIV_ROUND_UP(total_len, ADMA_MAX_LEN);
945 struct omap_hsmmc_data *priv = omap_hsmmc_get_data(mmc);
946 int i = desc_count;
947 char *buf;
948
949 priv->desc_slot = 0;
950 priv->adma_desc_table = (struct omap_hsmmc_adma_desc *)
951 memalign(ARCH_DMA_MINALIGN, desc_count *
952 sizeof(struct omap_hsmmc_adma_desc));
953
954 if (data->flags & MMC_DATA_READ)
955 buf = data->dest;
956 else
957 buf = (char *)data->src;
958
959 while (--i) {
960 omap_hsmmc_adma_desc(mmc, buf, ADMA_MAX_LEN, false);
961 buf += ADMA_MAX_LEN;
962 total_len -= ADMA_MAX_LEN;
963 }
964
965 omap_hsmmc_adma_desc(mmc, buf, total_len, true);
966
967 flush_dcache_range((long)priv->adma_desc_table,
968 (long)priv->adma_desc_table +
969 ROUND(desc_count *
970 sizeof(struct omap_hsmmc_adma_desc),
971 ARCH_DMA_MINALIGN));
972}
973
974static void omap_hsmmc_prepare_data(struct mmc *mmc, struct mmc_data *data)
975{
976 struct hsmmc *mmc_base;
977 struct omap_hsmmc_data *priv = omap_hsmmc_get_data(mmc);
978 u32 val;
979 char *buf;
980
981 mmc_base = priv->base_addr;
982 omap_hsmmc_prepare_adma_table(mmc, data);
983
984 if (data->flags & MMC_DATA_READ)
985 buf = data->dest;
986 else
987 buf = (char *)data->src;
988
989 val = readl(&mmc_base->hctl);
990 val |= DMA_SELECT;
991 writel(val, &mmc_base->hctl);
992
993 val = readl(&mmc_base->con);
994 val |= DMA_MASTER;
995 writel(val, &mmc_base->con);
996
997 writel((u32)priv->adma_desc_table, &mmc_base->admasal);
998
999 flush_dcache_range((u32)buf,
1000 (u32)buf +
1001 ROUND(data->blocksize * data->blocks,
1002 ARCH_DMA_MINALIGN));
1003}
1004
1005static void omap_hsmmc_dma_cleanup(struct mmc *mmc)
1006{
1007 struct hsmmc *mmc_base;
1008 struct omap_hsmmc_data *priv = omap_hsmmc_get_data(mmc);
1009 u32 val;
1010
1011 mmc_base = priv->base_addr;
1012
1013 val = readl(&mmc_base->con);
1014 val &= ~DMA_MASTER;
1015 writel(val, &mmc_base->con);
1016
1017 val = readl(&mmc_base->hctl);
1018 val &= ~DMA_SELECT;
1019 writel(val, &mmc_base->hctl);
1020
1021 kfree(priv->adma_desc_table);
1022}
1023#else
1024#define omap_hsmmc_adma_desc
1025#define omap_hsmmc_prepare_adma_table
1026#define omap_hsmmc_prepare_data
1027#define omap_hsmmc_dma_cleanup
1028#endif
1029
Simon Glassc4d660d2017-07-04 13:31:19 -06001030#if !CONFIG_IS_ENABLED(DM_MMC)
Pantelis Antoniouab769f22014-02-26 19:28:45 +02001031static int omap_hsmmc_send_cmd(struct mmc *mmc, struct mmc_cmd *cmd,
Sukumar Ghoraide941242010-09-18 20:32:33 -07001032 struct mmc_data *data)
1033{
Jean-Jacques Hiblotae000e22017-03-22 16:00:31 +01001034 struct omap_hsmmc_data *priv = omap_hsmmc_get_data(mmc);
Jean-Jacques Hiblotb5511d62017-04-14 19:50:02 +02001035#else
1036static int omap_hsmmc_send_cmd(struct udevice *dev, struct mmc_cmd *cmd,
1037 struct mmc_data *data)
1038{
1039 struct omap_hsmmc_data *priv = dev_get_priv(dev);
Kishon Vijay Abraham If0d53e82017-09-21 16:51:34 +02001040 struct mmc_uclass_priv *upriv = dev_get_uclass_priv(dev);
1041 struct mmc *mmc = upriv->mmc;
1042#endif
Nikita Kiryanovcc22b0c2012-12-03 02:19:43 +00001043 struct hsmmc *mmc_base;
Sukumar Ghoraide941242010-09-18 20:32:33 -07001044 unsigned int flags, mmc_stat;
Nishanth Menoneb9a28f2010-11-19 11:18:12 -05001045 ulong start;
Jean-Jacques Hiblot04f9f8b2018-01-30 16:01:46 +01001046 priv->last_cmd = cmd->cmdidx;
Sukumar Ghoraide941242010-09-18 20:32:33 -07001047
Jean-Jacques Hiblotae000e22017-03-22 16:00:31 +01001048 mmc_base = priv->base_addr;
Kishon Vijay Abraham I866bb982017-09-21 16:51:35 +02001049
1050 if (cmd->cmdidx == MMC_CMD_STOP_TRANSMISSION)
1051 return 0;
1052
Nishanth Menoneb9a28f2010-11-19 11:18:12 -05001053 start = get_timer(0);
Tom Rinia7778f82012-01-30 11:22:25 +00001054 while ((readl(&mmc_base->pstate) & (DATI_MASK | CMDI_MASK)) != 0) {
Nishanth Menoneb9a28f2010-11-19 11:18:12 -05001055 if (get_timer(0) - start > MAX_RETRY_MS) {
Tom Rinia7778f82012-01-30 11:22:25 +00001056 printf("%s: timedout waiting on cmd inhibit to clear\n",
1057 __func__);
Jean-Jacques Hiblotb2ffa332019-07-02 10:53:48 +02001058 mmc_reset_controller_fsm(mmc_base, SYSCTL_SRD);
1059 mmc_reset_controller_fsm(mmc_base, SYSCTL_SRC);
Jaehoon Chung915ffa52016-07-19 16:33:36 +09001060 return -ETIMEDOUT;
Nishanth Menoneb9a28f2010-11-19 11:18:12 -05001061 }
1062 }
Sukumar Ghoraide941242010-09-18 20:32:33 -07001063 writel(0xFFFFFFFF, &mmc_base->stat);
Jean-Jacques Hiblotb2ffa332019-07-02 10:53:48 +02001064 if (readl(&mmc_base->stat)) {
1065 mmc_reset_controller_fsm(mmc_base, SYSCTL_SRD);
1066 mmc_reset_controller_fsm(mmc_base, SYSCTL_SRC);
Nishanth Menoneb9a28f2010-11-19 11:18:12 -05001067 }
Jean-Jacques Hiblotb2ffa332019-07-02 10:53:48 +02001068
Sukumar Ghoraide941242010-09-18 20:32:33 -07001069 /*
1070 * CMDREG
1071 * CMDIDX[13:8] : Command index
1072 * DATAPRNT[5] : Data Present Select
1073 * ENCMDIDX[4] : Command Index Check Enable
1074 * ENCMDCRC[3] : Command CRC Check Enable
1075 * RSPTYP[1:0]
1076 * 00 = No Response
1077 * 01 = Length 136
1078 * 10 = Length 48
1079 * 11 = Length 48 Check busy after response
1080 */
1081 /* Delay added before checking the status of frq change
1082 * retry not supported by mmc.c(core file)
1083 */
1084 if (cmd->cmdidx == SD_CMD_APP_SEND_SCR)
1085 udelay(50000); /* wait 50 ms */
1086
1087 if (!(cmd->resp_type & MMC_RSP_PRESENT))
1088 flags = 0;
1089 else if (cmd->resp_type & MMC_RSP_136)
1090 flags = RSP_TYPE_LGHT136 | CICE_NOCHECK;
1091 else if (cmd->resp_type & MMC_RSP_BUSY)
1092 flags = RSP_TYPE_LGHT48B;
1093 else
1094 flags = RSP_TYPE_LGHT48;
1095
1096 /* enable default flags */
1097 flags = flags | (CMD_TYPE_NORMAL | CICE_NOCHECK | CCCE_NOCHECK |
Kishon Vijay Abraham I29171dc2017-09-21 16:51:36 +02001098 MSBS_SGLEBLK);
1099 flags &= ~(ACEN_ENABLE | BCE_ENABLE | DE_ENABLE);
Sukumar Ghoraide941242010-09-18 20:32:33 -07001100
1101 if (cmd->resp_type & MMC_RSP_CRC)
1102 flags |= CCCE_CHECK;
1103 if (cmd->resp_type & MMC_RSP_OPCODE)
1104 flags |= CICE_CHECK;
1105
1106 if (data) {
1107 if ((cmd->cmdidx == MMC_CMD_READ_MULTIPLE_BLOCK) ||
1108 (cmd->cmdidx == MMC_CMD_WRITE_MULTIPLE_BLOCK)) {
Kishon Vijay Abraham I866bb982017-09-21 16:51:35 +02001109 flags |= (MSBS_MULTIBLK | BCE_ENABLE | ACEN_ENABLE);
Sukumar Ghoraide941242010-09-18 20:32:33 -07001110 data->blocksize = 512;
1111 writel(data->blocksize | (data->blocks << 16),
1112 &mmc_base->blk);
1113 } else
1114 writel(data->blocksize | NBLK_STPCNT, &mmc_base->blk);
1115
1116 if (data->flags & MMC_DATA_READ)
1117 flags |= (DP_DATA | DDIR_READ);
1118 else
1119 flags |= (DP_DATA | DDIR_WRITE);
Kishon Vijay Abraham If0d53e82017-09-21 16:51:34 +02001120
Jean-Jacques Hiblot27a4b3b2018-02-23 10:40:18 +01001121#ifdef CONFIG_MMC_OMAP_HS_ADMA
Kishon Vijay Abraham If0d53e82017-09-21 16:51:34 +02001122 if ((priv->controller_flags & OMAP_HSMMC_USE_ADMA) &&
1123 !mmc_is_tuning_cmd(cmd->cmdidx)) {
1124 omap_hsmmc_prepare_data(mmc, data);
1125 flags |= DE_ENABLE;
1126 }
1127#endif
Sukumar Ghoraide941242010-09-18 20:32:33 -07001128 }
1129
Jean-Jacques Hiblot2faa1a32018-01-30 16:01:36 +01001130 mmc_enable_irq(mmc, cmd);
1131
Sukumar Ghoraide941242010-09-18 20:32:33 -07001132 writel(cmd->cmdarg, &mmc_base->arg);
Lubomir Popov152ba362013-08-14 18:59:18 +03001133 udelay(20); /* To fix "No status update" error on eMMC */
Sukumar Ghoraide941242010-09-18 20:32:33 -07001134 writel((cmd->cmdidx << 24) | flags, &mmc_base->cmd);
1135
Nishanth Menoneb9a28f2010-11-19 11:18:12 -05001136 start = get_timer(0);
Sukumar Ghoraide941242010-09-18 20:32:33 -07001137 do {
1138 mmc_stat = readl(&mmc_base->stat);
Kishon Vijay Abraham If0d53e82017-09-21 16:51:34 +02001139 if (get_timer(start) > MAX_RETRY_MS) {
Nishanth Menoneb9a28f2010-11-19 11:18:12 -05001140 printf("%s : timeout: No status update\n", __func__);
Jaehoon Chung915ffa52016-07-19 16:33:36 +09001141 return -ETIMEDOUT;
Nishanth Menoneb9a28f2010-11-19 11:18:12 -05001142 }
1143 } while (!mmc_stat);
Sukumar Ghoraide941242010-09-18 20:32:33 -07001144
Grazvydas Ignotas25c719e2012-03-19 12:12:06 +00001145 if ((mmc_stat & IE_CTO) != 0) {
1146 mmc_reset_controller_fsm(mmc_base, SYSCTL_SRC);
Jaehoon Chung915ffa52016-07-19 16:33:36 +09001147 return -ETIMEDOUT;
Grazvydas Ignotas25c719e2012-03-19 12:12:06 +00001148 } else if ((mmc_stat & ERRI_MASK) != 0)
Sukumar Ghoraide941242010-09-18 20:32:33 -07001149 return -1;
1150
1151 if (mmc_stat & CC_MASK) {
1152 writel(CC_MASK, &mmc_base->stat);
1153 if (cmd->resp_type & MMC_RSP_PRESENT) {
1154 if (cmd->resp_type & MMC_RSP_136) {
1155 /* response type 2 */
1156 cmd->response[3] = readl(&mmc_base->rsp10);
1157 cmd->response[2] = readl(&mmc_base->rsp32);
1158 cmd->response[1] = readl(&mmc_base->rsp54);
1159 cmd->response[0] = readl(&mmc_base->rsp76);
1160 } else
1161 /* response types 1, 1b, 3, 4, 5, 6 */
1162 cmd->response[0] = readl(&mmc_base->rsp10);
1163 }
1164 }
1165
Jean-Jacques Hiblot27a4b3b2018-02-23 10:40:18 +01001166#ifdef CONFIG_MMC_OMAP_HS_ADMA
Kishon Vijay Abraham If0d53e82017-09-21 16:51:34 +02001167 if ((priv->controller_flags & OMAP_HSMMC_USE_ADMA) && data &&
1168 !mmc_is_tuning_cmd(cmd->cmdidx)) {
1169 u32 sz_mb, timeout;
1170
1171 if (mmc_stat & IE_ADMAE) {
1172 omap_hsmmc_dma_cleanup(mmc);
1173 return -EIO;
1174 }
1175
1176 sz_mb = DIV_ROUND_UP(data->blocksize * data->blocks, 1 << 20);
1177 timeout = sz_mb * DMA_TIMEOUT_PER_MB;
1178 if (timeout < MAX_RETRY_MS)
1179 timeout = MAX_RETRY_MS;
1180
1181 start = get_timer(0);
1182 do {
1183 mmc_stat = readl(&mmc_base->stat);
1184 if (mmc_stat & TC_MASK) {
1185 writel(readl(&mmc_base->stat) | TC_MASK,
1186 &mmc_base->stat);
1187 break;
1188 }
1189 if (get_timer(start) > timeout) {
1190 printf("%s : DMA timeout: No status update\n",
1191 __func__);
1192 return -ETIMEDOUT;
1193 }
1194 } while (1);
1195
1196 omap_hsmmc_dma_cleanup(mmc);
1197 return 0;
1198 }
1199#endif
1200
Sukumar Ghoraide941242010-09-18 20:32:33 -07001201 if (data && (data->flags & MMC_DATA_READ)) {
1202 mmc_read_data(mmc_base, data->dest,
1203 data->blocksize * data->blocks);
1204 } else if (data && (data->flags & MMC_DATA_WRITE)) {
1205 mmc_write_data(mmc_base, data->src,
1206 data->blocksize * data->blocks);
1207 }
1208 return 0;
1209}
1210
Sricharan933efe62011-11-15 09:49:53 -05001211static int mmc_read_data(struct hsmmc *mmc_base, char *buf, unsigned int size)
Sukumar Ghoraide941242010-09-18 20:32:33 -07001212{
1213 unsigned int *output_buf = (unsigned int *)buf;
1214 unsigned int mmc_stat;
1215 unsigned int count;
1216
1217 /*
1218 * Start Polled Read
1219 */
1220 count = (size > MMCSD_SECTOR_SIZE) ? MMCSD_SECTOR_SIZE : size;
1221 count /= 4;
1222
1223 while (size) {
Nishanth Menoneb9a28f2010-11-19 11:18:12 -05001224 ulong start = get_timer(0);
Sukumar Ghoraide941242010-09-18 20:32:33 -07001225 do {
1226 mmc_stat = readl(&mmc_base->stat);
Nishanth Menoneb9a28f2010-11-19 11:18:12 -05001227 if (get_timer(0) - start > MAX_RETRY_MS) {
1228 printf("%s: timedout waiting for status!\n",
1229 __func__);
Jaehoon Chung915ffa52016-07-19 16:33:36 +09001230 return -ETIMEDOUT;
Nishanth Menoneb9a28f2010-11-19 11:18:12 -05001231 }
Sukumar Ghoraide941242010-09-18 20:32:33 -07001232 } while (mmc_stat == 0);
1233
Grazvydas Ignotas25c719e2012-03-19 12:12:06 +00001234 if ((mmc_stat & (IE_DTO | IE_DCRC | IE_DEB)) != 0)
1235 mmc_reset_controller_fsm(mmc_base, SYSCTL_SRD);
1236
Sukumar Ghoraide941242010-09-18 20:32:33 -07001237 if ((mmc_stat & ERRI_MASK) != 0)
1238 return 1;
1239
1240 if (mmc_stat & BRR_MASK) {
1241 unsigned int k;
1242
1243 writel(readl(&mmc_base->stat) | BRR_MASK,
1244 &mmc_base->stat);
1245 for (k = 0; k < count; k++) {
1246 *output_buf = readl(&mmc_base->data);
1247 output_buf++;
1248 }
1249 size -= (count*4);
1250 }
1251
1252 if (mmc_stat & BWR_MASK)
1253 writel(readl(&mmc_base->stat) | BWR_MASK,
1254 &mmc_base->stat);
1255
1256 if (mmc_stat & TC_MASK) {
1257 writel(readl(&mmc_base->stat) | TC_MASK,
1258 &mmc_base->stat);
1259 break;
1260 }
1261 }
1262 return 0;
1263}
1264
Jean-Jacques Hiblotc7d08d82018-02-23 10:40:17 +01001265#if CONFIG_IS_ENABLED(MMC_WRITE)
Sricharan933efe62011-11-15 09:49:53 -05001266static int mmc_write_data(struct hsmmc *mmc_base, const char *buf,
Jean-Jacques Hiblotc7d08d82018-02-23 10:40:17 +01001267 unsigned int size)
Sukumar Ghoraide941242010-09-18 20:32:33 -07001268{
1269 unsigned int *input_buf = (unsigned int *)buf;
1270 unsigned int mmc_stat;
1271 unsigned int count;
1272
1273 /*
Lubomir Popov152ba362013-08-14 18:59:18 +03001274 * Start Polled Write
Sukumar Ghoraide941242010-09-18 20:32:33 -07001275 */
1276 count = (size > MMCSD_SECTOR_SIZE) ? MMCSD_SECTOR_SIZE : size;
1277 count /= 4;
1278
1279 while (size) {
Nishanth Menoneb9a28f2010-11-19 11:18:12 -05001280 ulong start = get_timer(0);
Sukumar Ghoraide941242010-09-18 20:32:33 -07001281 do {
1282 mmc_stat = readl(&mmc_base->stat);
Nishanth Menoneb9a28f2010-11-19 11:18:12 -05001283 if (get_timer(0) - start > MAX_RETRY_MS) {
1284 printf("%s: timedout waiting for status!\n",
1285 __func__);
Jaehoon Chung915ffa52016-07-19 16:33:36 +09001286 return -ETIMEDOUT;
Nishanth Menoneb9a28f2010-11-19 11:18:12 -05001287 }
Sukumar Ghoraide941242010-09-18 20:32:33 -07001288 } while (mmc_stat == 0);
1289
Grazvydas Ignotas25c719e2012-03-19 12:12:06 +00001290 if ((mmc_stat & (IE_DTO | IE_DCRC | IE_DEB)) != 0)
1291 mmc_reset_controller_fsm(mmc_base, SYSCTL_SRD);
1292
Sukumar Ghoraide941242010-09-18 20:32:33 -07001293 if ((mmc_stat & ERRI_MASK) != 0)
1294 return 1;
1295
1296 if (mmc_stat & BWR_MASK) {
1297 unsigned int k;
1298
1299 writel(readl(&mmc_base->stat) | BWR_MASK,
1300 &mmc_base->stat);
1301 for (k = 0; k < count; k++) {
1302 writel(*input_buf, &mmc_base->data);
1303 input_buf++;
1304 }
1305 size -= (count*4);
1306 }
1307
1308 if (mmc_stat & BRR_MASK)
1309 writel(readl(&mmc_base->stat) | BRR_MASK,
1310 &mmc_base->stat);
1311
1312 if (mmc_stat & TC_MASK) {
1313 writel(readl(&mmc_base->stat) | TC_MASK,
1314 &mmc_base->stat);
1315 break;
1316 }
1317 }
1318 return 0;
1319}
Jean-Jacques Hiblotc7d08d82018-02-23 10:40:17 +01001320#else
1321static int mmc_write_data(struct hsmmc *mmc_base, const char *buf,
1322 unsigned int size)
1323{
1324 return -ENOTSUPP;
1325}
1326#endif
Jean-Jacques Hiblot5baf5432018-01-30 16:01:30 +01001327static void omap_hsmmc_stop_clock(struct hsmmc *mmc_base)
1328{
1329 writel(readl(&mmc_base->sysctl) & ~CEN_ENABLE, &mmc_base->sysctl);
1330}
1331
1332static void omap_hsmmc_start_clock(struct hsmmc *mmc_base)
1333{
1334 writel(readl(&mmc_base->sysctl) | CEN_ENABLE, &mmc_base->sysctl);
1335}
1336
1337static void omap_hsmmc_set_clock(struct mmc *mmc)
1338{
1339 struct omap_hsmmc_data *priv = omap_hsmmc_get_data(mmc);
1340 struct hsmmc *mmc_base;
1341 unsigned int dsor = 0;
1342 ulong start;
1343
1344 mmc_base = priv->base_addr;
1345 omap_hsmmc_stop_clock(mmc_base);
1346
1347 /* TODO: Is setting DTO required here? */
1348 mmc_reg_out(&mmc_base->sysctl, (ICE_MASK | DTO_MASK),
1349 (ICE_STOP | DTO_15THDTO));
1350
1351 if (mmc->clock != 0) {
1352 dsor = DIV_ROUND_UP(MMC_CLOCK_REFERENCE * 1000000, mmc->clock);
1353 if (dsor > CLKD_MAX)
1354 dsor = CLKD_MAX;
1355 } else {
1356 dsor = CLKD_MAX;
1357 }
1358
1359 mmc_reg_out(&mmc_base->sysctl, ICE_MASK | CLKD_MASK,
1360 (dsor << CLKD_OFFSET) | ICE_OSCILLATE);
1361
1362 start = get_timer(0);
1363 while ((readl(&mmc_base->sysctl) & ICS_MASK) == ICS_NOTREADY) {
1364 if (get_timer(0) - start > MAX_RETRY_MS) {
1365 printf("%s: timedout waiting for ics!\n", __func__);
1366 return;
1367 }
1368 }
1369
Jean-Jacques Hiblot3149c132018-01-30 16:01:43 +01001370 priv->clock = MMC_CLOCK_REFERENCE * 1000000 / dsor;
1371 mmc->clock = priv->clock;
Jean-Jacques Hiblot5baf5432018-01-30 16:01:30 +01001372 omap_hsmmc_start_clock(mmc_base);
1373}
1374
Kishon Vijay Abraham I48a2f112018-01-30 16:01:31 +01001375static void omap_hsmmc_set_bus_width(struct mmc *mmc)
Sukumar Ghoraide941242010-09-18 20:32:33 -07001376{
Jean-Jacques Hiblotae000e22017-03-22 16:00:31 +01001377 struct omap_hsmmc_data *priv = omap_hsmmc_get_data(mmc);
Nikita Kiryanovcc22b0c2012-12-03 02:19:43 +00001378 struct hsmmc *mmc_base;
Sukumar Ghoraide941242010-09-18 20:32:33 -07001379
Jean-Jacques Hiblotae000e22017-03-22 16:00:31 +01001380 mmc_base = priv->base_addr;
Sukumar Ghoraide941242010-09-18 20:32:33 -07001381 /* configue bus width */
1382 switch (mmc->bus_width) {
1383 case 8:
1384 writel(readl(&mmc_base->con) | DTW_8_BITMODE,
1385 &mmc_base->con);
1386 break;
1387
1388 case 4:
1389 writel(readl(&mmc_base->con) & ~DTW_8_BITMODE,
1390 &mmc_base->con);
1391 writel(readl(&mmc_base->hctl) | DTW_4_BITMODE,
1392 &mmc_base->hctl);
1393 break;
1394
1395 case 1:
1396 default:
1397 writel(readl(&mmc_base->con) & ~DTW_8_BITMODE,
1398 &mmc_base->con);
1399 writel(readl(&mmc_base->hctl) & ~DTW_4_BITMODE,
1400 &mmc_base->hctl);
1401 break;
1402 }
1403
Kishon Vijay Abraham I48a2f112018-01-30 16:01:31 +01001404 priv->bus_width = mmc->bus_width;
1405}
1406
1407#if !CONFIG_IS_ENABLED(DM_MMC)
1408static int omap_hsmmc_set_ios(struct mmc *mmc)
1409{
1410 struct omap_hsmmc_data *priv = omap_hsmmc_get_data(mmc);
1411#else
1412static int omap_hsmmc_set_ios(struct udevice *dev)
1413{
1414 struct omap_hsmmc_data *priv = dev_get_priv(dev);
1415 struct mmc_uclass_priv *upriv = dev_get_uclass_priv(dev);
1416 struct mmc *mmc = upriv->mmc;
1417#endif
Kishon Vijay Abraham I90321dc2018-01-30 16:01:45 +01001418 struct hsmmc *mmc_base = priv->base_addr;
Jean-Jacques Hiblot04f9f8b2018-01-30 16:01:46 +01001419 int ret = 0;
Kishon Vijay Abraham I48a2f112018-01-30 16:01:31 +01001420
1421 if (priv->bus_width != mmc->bus_width)
1422 omap_hsmmc_set_bus_width(mmc);
1423
Jean-Jacques Hiblot5baf5432018-01-30 16:01:30 +01001424 if (priv->clock != mmc->clock)
1425 omap_hsmmc_set_clock(mmc);
Jaehoon Chung07b0b9c2016-12-30 15:30:16 +09001426
Kishon Vijay Abraham I90321dc2018-01-30 16:01:45 +01001427 if (mmc->clk_disable)
1428 omap_hsmmc_stop_clock(mmc_base);
1429 else
1430 omap_hsmmc_start_clock(mmc_base);
1431
Jean-Jacques Hiblot8fc238b2018-01-30 16:01:33 +01001432#if CONFIG_IS_ENABLED(DM_MMC)
1433 if (priv->mode != mmc->selected_mode)
1434 omap_hsmmc_set_timing(mmc);
Jean-Jacques Hiblot04f9f8b2018-01-30 16:01:46 +01001435
1436#if CONFIG_IS_ENABLED(MMC_IO_VOLTAGE)
1437 if (priv->signal_voltage != mmc->signal_voltage)
1438 ret = omap_hsmmc_set_signal_voltage(mmc);
Jean-Jacques Hiblot8fc238b2018-01-30 16:01:33 +01001439#endif
Jean-Jacques Hiblot04f9f8b2018-01-30 16:01:46 +01001440#endif
1441 return ret;
Sukumar Ghoraide941242010-09-18 20:32:33 -07001442}
1443
Pantelis Antoniouab769f22014-02-26 19:28:45 +02001444#ifdef OMAP_HSMMC_USE_GPIO
Simon Glassc4d660d2017-07-04 13:31:19 -06001445#if CONFIG_IS_ENABLED(DM_MMC)
Jean-Jacques Hiblotb5511d62017-04-14 19:50:02 +02001446static int omap_hsmmc_getcd(struct udevice *dev)
Mugunthan V Na9d6a7e2015-09-28 12:56:30 +05301447{
Adam Ford307a2142018-08-21 07:16:56 -05001448 int value = -1;
1449#if CONFIG_IS_ENABLED(DM_GPIO)
Adam Fordf4df4052018-09-08 08:16:23 -05001450 struct omap_hsmmc_data *priv = dev_get_priv(dev);
Mugunthan V Na9d6a7e2015-09-28 12:56:30 +05301451 value = dm_gpio_get_value(&priv->cd_gpio);
Adam Ford307a2142018-08-21 07:16:56 -05001452#endif
Mugunthan V Na9d6a7e2015-09-28 12:56:30 +05301453 /* if no CD return as 1 */
1454 if (value < 0)
1455 return 1;
1456
Mugunthan V Na9d6a7e2015-09-28 12:56:30 +05301457 return value;
1458}
1459
Jean-Jacques Hiblotb5511d62017-04-14 19:50:02 +02001460static int omap_hsmmc_getwp(struct udevice *dev)
Mugunthan V Na9d6a7e2015-09-28 12:56:30 +05301461{
Adam Ford307a2142018-08-21 07:16:56 -05001462 int value = 0;
1463#if CONFIG_IS_ENABLED(DM_GPIO)
Jean-Jacques Hiblotb5511d62017-04-14 19:50:02 +02001464 struct omap_hsmmc_data *priv = dev_get_priv(dev);
Mugunthan V Na9d6a7e2015-09-28 12:56:30 +05301465 value = dm_gpio_get_value(&priv->wp_gpio);
Adam Ford307a2142018-08-21 07:16:56 -05001466#endif
Mugunthan V Na9d6a7e2015-09-28 12:56:30 +05301467 /* if no WP return as 0 */
1468 if (value < 0)
1469 return 0;
1470 return value;
1471}
1472#else
Pantelis Antoniouab769f22014-02-26 19:28:45 +02001473static int omap_hsmmc_getcd(struct mmc *mmc)
1474{
Jean-Jacques Hiblotae000e22017-03-22 16:00:31 +01001475 struct omap_hsmmc_data *priv = omap_hsmmc_get_data(mmc);
Pantelis Antoniouab769f22014-02-26 19:28:45 +02001476 int cd_gpio;
1477
1478 /* if no CD return as 1 */
Jean-Jacques Hiblotae000e22017-03-22 16:00:31 +01001479 cd_gpio = priv->cd_gpio;
Pantelis Antoniouab769f22014-02-26 19:28:45 +02001480 if (cd_gpio < 0)
1481 return 1;
1482
Igor Grinberg0b03a932014-11-03 11:32:23 +02001483 /* NOTE: assumes card detect signal is active-low */
1484 return !gpio_get_value(cd_gpio);
Pantelis Antoniouab769f22014-02-26 19:28:45 +02001485}
1486
1487static int omap_hsmmc_getwp(struct mmc *mmc)
1488{
Jean-Jacques Hiblotae000e22017-03-22 16:00:31 +01001489 struct omap_hsmmc_data *priv = omap_hsmmc_get_data(mmc);
Pantelis Antoniouab769f22014-02-26 19:28:45 +02001490 int wp_gpio;
1491
1492 /* if no WP return as 0 */
Jean-Jacques Hiblotae000e22017-03-22 16:00:31 +01001493 wp_gpio = priv->wp_gpio;
Pantelis Antoniouab769f22014-02-26 19:28:45 +02001494 if (wp_gpio < 0)
1495 return 0;
1496
Igor Grinberg0b03a932014-11-03 11:32:23 +02001497 /* NOTE: assumes write protect signal is active-high */
Pantelis Antoniouab769f22014-02-26 19:28:45 +02001498 return gpio_get_value(wp_gpio);
1499}
1500#endif
Mugunthan V Na9d6a7e2015-09-28 12:56:30 +05301501#endif
Pantelis Antoniouab769f22014-02-26 19:28:45 +02001502
Simon Glassc4d660d2017-07-04 13:31:19 -06001503#if CONFIG_IS_ENABLED(DM_MMC)
Jean-Jacques Hiblotb5511d62017-04-14 19:50:02 +02001504static const struct dm_mmc_ops omap_hsmmc_ops = {
1505 .send_cmd = omap_hsmmc_send_cmd,
1506 .set_ios = omap_hsmmc_set_ios,
1507#ifdef OMAP_HSMMC_USE_GPIO
1508 .get_cd = omap_hsmmc_getcd,
1509 .get_wp = omap_hsmmc_getwp,
1510#endif
Jean-Jacques Hiblot14761ca2018-01-30 16:01:35 +01001511#ifdef MMC_SUPPORTS_TUNING
1512 .execute_tuning = omap_hsmmc_execute_tuning,
1513#endif
Jean-Jacques Hiblot04f9f8b2018-01-30 16:01:46 +01001514 .wait_dat0 = omap_hsmmc_wait_dat0,
Jean-Jacques Hiblotb5511d62017-04-14 19:50:02 +02001515};
1516#else
Pantelis Antoniouab769f22014-02-26 19:28:45 +02001517static const struct mmc_ops omap_hsmmc_ops = {
1518 .send_cmd = omap_hsmmc_send_cmd,
1519 .set_ios = omap_hsmmc_set_ios,
1520 .init = omap_hsmmc_init_setup,
1521#ifdef OMAP_HSMMC_USE_GPIO
1522 .getcd = omap_hsmmc_getcd,
1523 .getwp = omap_hsmmc_getwp,
1524#endif
1525};
Jean-Jacques Hiblotb5511d62017-04-14 19:50:02 +02001526#endif
Pantelis Antoniouab769f22014-02-26 19:28:45 +02001527
Simon Glassc4d660d2017-07-04 13:31:19 -06001528#if !CONFIG_IS_ENABLED(DM_MMC)
Nikita Kiryanove3913f52012-12-03 02:19:47 +00001529int omap_mmc_init(int dev_index, uint host_caps_mask, uint f_max, int cd_gpio,
1530 int wp_gpio)
Sukumar Ghoraide941242010-09-18 20:32:33 -07001531{
Pantelis Antoniou93bfd612014-03-11 19:34:20 +02001532 struct mmc *mmc;
Jean-Jacques Hiblotae000e22017-03-22 16:00:31 +01001533 struct omap_hsmmc_data *priv;
Pantelis Antoniou93bfd612014-03-11 19:34:20 +02001534 struct mmc_config *cfg;
1535 uint host_caps_val;
Sukumar Ghoraide941242010-09-18 20:32:33 -07001536
Alex Kiernan4a41fec2018-02-09 15:24:38 +00001537 priv = calloc(1, sizeof(*priv));
Jean-Jacques Hiblotae000e22017-03-22 16:00:31 +01001538 if (priv == NULL)
Pantelis Antoniou93bfd612014-03-11 19:34:20 +02001539 return -1;
1540
Rob Herring5a203972015-03-23 17:56:59 -05001541 host_caps_val = MMC_MODE_4BIT | MMC_MODE_HS_52MHz | MMC_MODE_HS;
Sukumar Ghoraide941242010-09-18 20:32:33 -07001542
1543 switch (dev_index) {
1544 case 0:
Jean-Jacques Hiblotae000e22017-03-22 16:00:31 +01001545 priv->base_addr = (struct hsmmc *)OMAP_HSMMC1_BASE;
Sukumar Ghoraide941242010-09-18 20:32:33 -07001546 break;
Tom Rini1037d582011-10-12 06:20:50 +00001547#ifdef OMAP_HSMMC2_BASE
Sukumar Ghoraide941242010-09-18 20:32:33 -07001548 case 1:
Jean-Jacques Hiblotae000e22017-03-22 16:00:31 +01001549 priv->base_addr = (struct hsmmc *)OMAP_HSMMC2_BASE;
Lubomir Popov152ba362013-08-14 18:59:18 +03001550#if (defined(CONFIG_OMAP44XX) || defined(CONFIG_OMAP54XX) || \
Nishanth Menon3891a542016-11-29 15:22:00 +05301551 defined(CONFIG_DRA7XX) || defined(CONFIG_AM33XX) || \
Roger Quadros3b689392015-09-19 16:26:53 +05301552 defined(CONFIG_AM43XX) || defined(CONFIG_SOC_KEYSTONE)) && \
1553 defined(CONFIG_HSMMC2_8BIT)
Lubomir Popov152ba362013-08-14 18:59:18 +03001554 /* Enable 8-bit interface for eMMC on OMAP4/5 or DRA7XX */
1555 host_caps_val |= MMC_MODE_8BIT;
1556#endif
Sukumar Ghoraide941242010-09-18 20:32:33 -07001557 break;
Tom Rini1037d582011-10-12 06:20:50 +00001558#endif
1559#ifdef OMAP_HSMMC3_BASE
Sukumar Ghoraide941242010-09-18 20:32:33 -07001560 case 2:
Jean-Jacques Hiblotae000e22017-03-22 16:00:31 +01001561 priv->base_addr = (struct hsmmc *)OMAP_HSMMC3_BASE;
Nishanth Menon3891a542016-11-29 15:22:00 +05301562#if defined(CONFIG_DRA7XX) && defined(CONFIG_HSMMC3_8BIT)
Lubomir Popov152ba362013-08-14 18:59:18 +03001563 /* Enable 8-bit interface for eMMC on DRA7XX */
1564 host_caps_val |= MMC_MODE_8BIT;
1565#endif
Sukumar Ghoraide941242010-09-18 20:32:33 -07001566 break;
Tom Rini1037d582011-10-12 06:20:50 +00001567#endif
Sukumar Ghoraide941242010-09-18 20:32:33 -07001568 default:
Jean-Jacques Hiblotae000e22017-03-22 16:00:31 +01001569 priv->base_addr = (struct hsmmc *)OMAP_HSMMC1_BASE;
Sukumar Ghoraide941242010-09-18 20:32:33 -07001570 return 1;
1571 }
Pantelis Antoniouab769f22014-02-26 19:28:45 +02001572#ifdef OMAP_HSMMC_USE_GPIO
1573 /* on error gpio values are set to -1, which is what we want */
Jean-Jacques Hiblotae000e22017-03-22 16:00:31 +01001574 priv->cd_gpio = omap_mmc_setup_gpio_in(cd_gpio, "mmc_cd");
1575 priv->wp_gpio = omap_mmc_setup_gpio_in(wp_gpio, "mmc_wp");
Pantelis Antoniouab769f22014-02-26 19:28:45 +02001576#endif
Peter Korsgaard173ddc52013-03-21 04:00:04 +00001577
Jean-Jacques Hiblotae000e22017-03-22 16:00:31 +01001578 cfg = &priv->cfg;
Sukumar Ghoraide941242010-09-18 20:32:33 -07001579
Pantelis Antoniou93bfd612014-03-11 19:34:20 +02001580 cfg->name = "OMAP SD/MMC";
1581 cfg->ops = &omap_hsmmc_ops;
1582
1583 cfg->voltages = MMC_VDD_32_33 | MMC_VDD_33_34 | MMC_VDD_165_195;
1584 cfg->host_caps = host_caps_val & ~host_caps_mask;
1585
1586 cfg->f_min = 400000;
Jonathan Solnitbbbc1ae2012-02-24 11:30:18 +00001587
1588 if (f_max != 0)
Pantelis Antoniou93bfd612014-03-11 19:34:20 +02001589 cfg->f_max = f_max;
Jonathan Solnitbbbc1ae2012-02-24 11:30:18 +00001590 else {
Pantelis Antoniou93bfd612014-03-11 19:34:20 +02001591 if (cfg->host_caps & MMC_MODE_HS) {
1592 if (cfg->host_caps & MMC_MODE_HS_52MHz)
1593 cfg->f_max = 52000000;
Jonathan Solnitbbbc1ae2012-02-24 11:30:18 +00001594 else
Pantelis Antoniou93bfd612014-03-11 19:34:20 +02001595 cfg->f_max = 26000000;
Jonathan Solnitbbbc1ae2012-02-24 11:30:18 +00001596 } else
Pantelis Antoniou93bfd612014-03-11 19:34:20 +02001597 cfg->f_max = 20000000;
Jonathan Solnitbbbc1ae2012-02-24 11:30:18 +00001598 }
Sukumar Ghoraide941242010-09-18 20:32:33 -07001599
Pantelis Antoniou93bfd612014-03-11 19:34:20 +02001600 cfg->b_max = CONFIG_SYS_MMC_MAX_BLK_COUNT;
John Rigby8feafcc2011-04-18 05:50:08 +00001601
John Rigby4ca92442011-04-19 05:48:14 +00001602#if defined(CONFIG_OMAP34XX)
1603 /*
1604 * Silicon revs 2.1 and older do not support multiblock transfers.
1605 */
1606 if ((get_cpu_family() == CPU_OMAP34XX) && (get_cpu_rev() <= CPU_3XX_ES21))
Pantelis Antoniou93bfd612014-03-11 19:34:20 +02001607 cfg->b_max = 1;
John Rigby4ca92442011-04-19 05:48:14 +00001608#endif
Kishon Vijay Abraham I2d28eed2018-01-30 16:01:41 +01001609
Jean-Jacques Hiblotae000e22017-03-22 16:00:31 +01001610 mmc = mmc_create(cfg, priv);
Pantelis Antoniou93bfd612014-03-11 19:34:20 +02001611 if (mmc == NULL)
1612 return -1;
Sukumar Ghoraide941242010-09-18 20:32:33 -07001613
1614 return 0;
1615}
Mugunthan V Na9d6a7e2015-09-28 12:56:30 +05301616#else
Kishon Vijay Abraham I33c1d772018-01-30 16:01:40 +01001617
1618#ifdef CONFIG_IODELAY_RECALIBRATION
1619static struct pad_conf_entry *
1620omap_hsmmc_get_pad_conf_entry(const fdt32_t *pinctrl, int count)
1621{
1622 int index = 0;
1623 struct pad_conf_entry *padconf;
1624
1625 padconf = (struct pad_conf_entry *)malloc(sizeof(*padconf) * count);
1626 if (!padconf) {
1627 debug("failed to allocate memory\n");
1628 return 0;
1629 }
1630
1631 while (index < count) {
1632 padconf[index].offset = fdt32_to_cpu(pinctrl[2 * index]);
1633 padconf[index].val = fdt32_to_cpu(pinctrl[2 * index + 1]);
1634 index++;
1635 }
1636
1637 return padconf;
1638}
1639
1640static struct iodelay_cfg_entry *
1641omap_hsmmc_get_iodelay_cfg_entry(const fdt32_t *pinctrl, int count)
1642{
1643 int index = 0;
1644 struct iodelay_cfg_entry *iodelay;
1645
1646 iodelay = (struct iodelay_cfg_entry *)malloc(sizeof(*iodelay) * count);
1647 if (!iodelay) {
1648 debug("failed to allocate memory\n");
1649 return 0;
1650 }
1651
1652 while (index < count) {
1653 iodelay[index].offset = fdt32_to_cpu(pinctrl[3 * index]);
1654 iodelay[index].a_delay = fdt32_to_cpu(pinctrl[3 * index + 1]);
1655 iodelay[index].g_delay = fdt32_to_cpu(pinctrl[3 * index + 2]);
1656 index++;
1657 }
1658
1659 return iodelay;
1660}
1661
1662static const fdt32_t *omap_hsmmc_get_pinctrl_entry(u32 phandle,
1663 const char *name, int *len)
1664{
1665 const void *fdt = gd->fdt_blob;
1666 int offset;
1667 const fdt32_t *pinctrl;
1668
1669 offset = fdt_node_offset_by_phandle(fdt, phandle);
1670 if (offset < 0) {
1671 debug("failed to get pinctrl node %s.\n",
1672 fdt_strerror(offset));
1673 return 0;
1674 }
1675
1676 pinctrl = fdt_getprop(fdt, offset, name, len);
1677 if (!pinctrl) {
1678 debug("failed to get property %s\n", name);
1679 return 0;
1680 }
1681
1682 return pinctrl;
1683}
1684
1685static uint32_t omap_hsmmc_get_pad_conf_phandle(struct mmc *mmc,
1686 char *prop_name)
1687{
1688 const void *fdt = gd->fdt_blob;
1689 const __be32 *phandle;
1690 int node = dev_of_offset(mmc->dev);
1691
1692 phandle = fdt_getprop(fdt, node, prop_name, NULL);
1693 if (!phandle) {
1694 debug("failed to get property %s\n", prop_name);
1695 return 0;
1696 }
1697
1698 return fdt32_to_cpu(*phandle);
1699}
1700
1701static uint32_t omap_hsmmc_get_iodelay_phandle(struct mmc *mmc,
1702 char *prop_name)
1703{
1704 const void *fdt = gd->fdt_blob;
1705 const __be32 *phandle;
1706 int len;
1707 int count;
1708 int node = dev_of_offset(mmc->dev);
1709
1710 phandle = fdt_getprop(fdt, node, prop_name, &len);
1711 if (!phandle) {
1712 debug("failed to get property %s\n", prop_name);
1713 return 0;
1714 }
1715
1716 /* No manual mode iodelay values if count < 2 */
1717 count = len / sizeof(*phandle);
1718 if (count < 2)
1719 return 0;
1720
1721 return fdt32_to_cpu(*(phandle + 1));
1722}
1723
1724static struct pad_conf_entry *
1725omap_hsmmc_get_pad_conf(struct mmc *mmc, char *prop_name, int *npads)
1726{
1727 int len;
1728 int count;
1729 struct pad_conf_entry *padconf;
1730 u32 phandle;
1731 const fdt32_t *pinctrl;
1732
1733 phandle = omap_hsmmc_get_pad_conf_phandle(mmc, prop_name);
1734 if (!phandle)
1735 return ERR_PTR(-EINVAL);
1736
1737 pinctrl = omap_hsmmc_get_pinctrl_entry(phandle, "pinctrl-single,pins",
1738 &len);
1739 if (!pinctrl)
1740 return ERR_PTR(-EINVAL);
1741
1742 count = (len / sizeof(*pinctrl)) / 2;
1743 padconf = omap_hsmmc_get_pad_conf_entry(pinctrl, count);
1744 if (!padconf)
1745 return ERR_PTR(-EINVAL);
1746
1747 *npads = count;
1748
1749 return padconf;
1750}
1751
1752static struct iodelay_cfg_entry *
1753omap_hsmmc_get_iodelay(struct mmc *mmc, char *prop_name, int *niodelay)
1754{
1755 int len;
1756 int count;
1757 struct iodelay_cfg_entry *iodelay;
1758 u32 phandle;
1759 const fdt32_t *pinctrl;
1760
1761 phandle = omap_hsmmc_get_iodelay_phandle(mmc, prop_name);
1762 /* Not all modes have manual mode iodelay values. So its not fatal */
1763 if (!phandle)
1764 return 0;
1765
1766 pinctrl = omap_hsmmc_get_pinctrl_entry(phandle, "pinctrl-pin-array",
1767 &len);
1768 if (!pinctrl)
1769 return ERR_PTR(-EINVAL);
1770
1771 count = (len / sizeof(*pinctrl)) / 3;
1772 iodelay = omap_hsmmc_get_iodelay_cfg_entry(pinctrl, count);
1773 if (!iodelay)
1774 return ERR_PTR(-EINVAL);
1775
1776 *niodelay = count;
1777
1778 return iodelay;
1779}
1780
1781static struct omap_hsmmc_pinctrl_state *
1782omap_hsmmc_get_pinctrl_by_mode(struct mmc *mmc, char *mode)
1783{
1784 int index;
1785 int npads = 0;
1786 int niodelays = 0;
1787 const void *fdt = gd->fdt_blob;
1788 int node = dev_of_offset(mmc->dev);
1789 char prop_name[11];
1790 struct omap_hsmmc_pinctrl_state *pinctrl_state;
1791
1792 pinctrl_state = (struct omap_hsmmc_pinctrl_state *)
1793 malloc(sizeof(*pinctrl_state));
1794 if (!pinctrl_state) {
1795 debug("failed to allocate memory\n");
1796 return 0;
1797 }
1798
1799 index = fdt_stringlist_search(fdt, node, "pinctrl-names", mode);
1800 if (index < 0) {
1801 debug("fail to find %s mode %s\n", mode, fdt_strerror(index));
1802 goto err_pinctrl_state;
1803 }
1804
1805 sprintf(prop_name, "pinctrl-%d", index);
1806
1807 pinctrl_state->padconf = omap_hsmmc_get_pad_conf(mmc, prop_name,
1808 &npads);
1809 if (IS_ERR(pinctrl_state->padconf))
1810 goto err_pinctrl_state;
1811 pinctrl_state->npads = npads;
1812
1813 pinctrl_state->iodelay = omap_hsmmc_get_iodelay(mmc, prop_name,
1814 &niodelays);
1815 if (IS_ERR(pinctrl_state->iodelay))
1816 goto err_padconf;
1817 pinctrl_state->niodelays = niodelays;
1818
1819 return pinctrl_state;
1820
1821err_padconf:
1822 kfree(pinctrl_state->padconf);
1823
1824err_pinctrl_state:
1825 kfree(pinctrl_state);
1826 return 0;
1827}
1828
Jean-Jacques Hiblotbcc6bd82018-01-30 16:01:42 +01001829#define OMAP_HSMMC_SETUP_PINCTRL(capmask, mode, optional) \
Kishon Vijay Abraham I2d28eed2018-01-30 16:01:41 +01001830 do { \
1831 struct omap_hsmmc_pinctrl_state *s = NULL; \
1832 char str[20]; \
1833 if (!(cfg->host_caps & capmask)) \
1834 break; \
1835 \
1836 if (priv->hw_rev) { \
1837 sprintf(str, "%s-%s", #mode, priv->hw_rev); \
1838 s = omap_hsmmc_get_pinctrl_by_mode(mmc, str); \
1839 } \
1840 \
1841 if (!s) \
1842 s = omap_hsmmc_get_pinctrl_by_mode(mmc, #mode); \
1843 \
Jean-Jacques Hiblotbcc6bd82018-01-30 16:01:42 +01001844 if (!s && !optional) { \
Kishon Vijay Abraham I2d28eed2018-01-30 16:01:41 +01001845 debug("%s: no pinctrl for %s\n", \
1846 mmc->dev->name, #mode); \
1847 cfg->host_caps &= ~(capmask); \
1848 } else { \
1849 priv->mode##_pinctrl_state = s; \
1850 } \
Kishon Vijay Abraham I33c1d772018-01-30 16:01:40 +01001851 } while (0)
1852
1853static int omap_hsmmc_get_pinctrl_state(struct mmc *mmc)
1854{
1855 struct omap_hsmmc_data *priv = omap_hsmmc_get_data(mmc);
1856 struct mmc_config *cfg = omap_hsmmc_get_cfg(mmc);
1857 struct omap_hsmmc_pinctrl_state *default_pinctrl;
1858
1859 if (!(priv->controller_flags & OMAP_HSMMC_REQUIRE_IODELAY))
1860 return 0;
1861
1862 default_pinctrl = omap_hsmmc_get_pinctrl_by_mode(mmc, "default");
1863 if (!default_pinctrl) {
1864 printf("no pinctrl state for default mode\n");
1865 return -EINVAL;
1866 }
1867
1868 priv->default_pinctrl_state = default_pinctrl;
1869
Jean-Jacques Hiblotbcc6bd82018-01-30 16:01:42 +01001870 OMAP_HSMMC_SETUP_PINCTRL(MMC_CAP(UHS_SDR104), sdr104, false);
1871 OMAP_HSMMC_SETUP_PINCTRL(MMC_CAP(UHS_SDR50), sdr50, false);
1872 OMAP_HSMMC_SETUP_PINCTRL(MMC_CAP(UHS_DDR50), ddr50, false);
1873 OMAP_HSMMC_SETUP_PINCTRL(MMC_CAP(UHS_SDR25), sdr25, false);
1874 OMAP_HSMMC_SETUP_PINCTRL(MMC_CAP(UHS_SDR12), sdr12, false);
Kishon Vijay Abraham I33c1d772018-01-30 16:01:40 +01001875
Jean-Jacques Hiblotbcc6bd82018-01-30 16:01:42 +01001876 OMAP_HSMMC_SETUP_PINCTRL(MMC_CAP(MMC_HS_200), hs200_1_8v, false);
1877 OMAP_HSMMC_SETUP_PINCTRL(MMC_CAP(MMC_DDR_52), ddr_1_8v, false);
1878 OMAP_HSMMC_SETUP_PINCTRL(MMC_MODE_HS, hs, true);
Kishon Vijay Abraham I33c1d772018-01-30 16:01:40 +01001879
1880 return 0;
1881}
1882#endif
1883
Lokesh Vutla2558c042017-04-26 13:37:05 +05301884#if CONFIG_IS_ENABLED(OF_CONTROL) && !CONFIG_IS_ENABLED(OF_PLATDATA)
Kishon Vijay Abraham I2d28eed2018-01-30 16:01:41 +01001885#ifdef CONFIG_OMAP54XX
1886__weak const struct mmc_platform_fixups *platform_fixups_mmc(uint32_t addr)
1887{
1888 return NULL;
1889}
1890#endif
1891
Mugunthan V Na9d6a7e2015-09-28 12:56:30 +05301892static int omap_hsmmc_ofdata_to_platdata(struct udevice *dev)
1893{
Jean-Jacques Hiblot3d673ff2017-03-22 16:00:33 +01001894 struct omap_hsmmc_plat *plat = dev_get_platdata(dev);
Kishon Vijay Abraham I33c1d772018-01-30 16:01:40 +01001895 struct omap_mmc_of_data *of_data = (void *)dev_get_driver_data(dev);
1896
Jean-Jacques Hiblot3d673ff2017-03-22 16:00:33 +01001897 struct mmc_config *cfg = &plat->cfg;
Kishon Vijay Abraham I2d28eed2018-01-30 16:01:41 +01001898#ifdef CONFIG_OMAP54XX
1899 const struct mmc_platform_fixups *fixups;
1900#endif
Mugunthan V Na9d6a7e2015-09-28 12:56:30 +05301901 const void *fdt = gd->fdt_blob;
Simon Glasse160f7d2017-01-17 16:52:55 -07001902 int node = dev_of_offset(dev);
Kishon Vijay Abraham I2d7482c2018-01-30 16:01:38 +01001903 int ret;
Mugunthan V Na9d6a7e2015-09-28 12:56:30 +05301904
Simon Glassa821c4a2017-05-17 17:18:05 -06001905 plat->base_addr = map_physmem(devfdt_get_addr(dev),
1906 sizeof(struct hsmmc *),
Jean-Jacques Hiblot741726a2017-09-21 16:51:32 +02001907 MAP_NOCACHE);
Mugunthan V Na9d6a7e2015-09-28 12:56:30 +05301908
Kishon Vijay Abraham I2d7482c2018-01-30 16:01:38 +01001909 ret = mmc_of_parse(dev, cfg);
1910 if (ret < 0)
1911 return ret;
Mugunthan V Na9d6a7e2015-09-28 12:56:30 +05301912
Jean-Jacques Hiblotbeac7d32018-02-23 10:40:19 +01001913 if (!cfg->f_max)
1914 cfg->f_max = 52000000;
Kishon Vijay Abraham I2d7482c2018-01-30 16:01:38 +01001915 cfg->host_caps |= MMC_MODE_HS_52MHz | MMC_MODE_HS;
Mugunthan V Na9d6a7e2015-09-28 12:56:30 +05301916 cfg->f_min = 400000;
Mugunthan V Na9d6a7e2015-09-28 12:56:30 +05301917 cfg->voltages = MMC_VDD_32_33 | MMC_VDD_33_34 | MMC_VDD_165_195;
1918 cfg->b_max = CONFIG_SYS_MMC_MAX_BLK_COUNT;
Kishon Vijay Abraham Ib5944812018-01-30 16:01:32 +01001919 if (fdtdec_get_bool(fdt, node, "ti,dual-volt"))
1920 plat->controller_flags |= OMAP_HSMMC_SUPPORTS_DUAL_VOLT;
1921 if (fdtdec_get_bool(fdt, node, "no-1-8-v"))
1922 plat->controller_flags |= OMAP_HSMMC_NO_1_8_V;
Kishon Vijay Abraham I33c1d772018-01-30 16:01:40 +01001923 if (of_data)
1924 plat->controller_flags |= of_data->controller_flags;
Mugunthan V Na9d6a7e2015-09-28 12:56:30 +05301925
Kishon Vijay Abraham I2d28eed2018-01-30 16:01:41 +01001926#ifdef CONFIG_OMAP54XX
1927 fixups = platform_fixups_mmc(devfdt_get_addr(dev));
1928 if (fixups) {
1929 plat->hw_rev = fixups->hw_rev;
1930 cfg->host_caps &= ~fixups->unsupported_caps;
1931 cfg->f_max = fixups->max_freq;
1932 }
1933#endif
1934
Mugunthan V Na9d6a7e2015-09-28 12:56:30 +05301935 return 0;
1936}
Lokesh Vutla2558c042017-04-26 13:37:05 +05301937#endif
Mugunthan V Na9d6a7e2015-09-28 12:56:30 +05301938
Jean-Jacques Hiblot17c9a1c2017-03-22 16:00:34 +01001939#ifdef CONFIG_BLK
1940
1941static int omap_hsmmc_bind(struct udevice *dev)
1942{
1943 struct omap_hsmmc_plat *plat = dev_get_platdata(dev);
Jean-Jacques Hiblot45530e32018-02-23 10:40:16 +01001944 plat->mmc = calloc(1, sizeof(struct mmc));
1945 return mmc_bind(dev, plat->mmc, &plat->cfg);
Jean-Jacques Hiblot17c9a1c2017-03-22 16:00:34 +01001946}
1947#endif
Mugunthan V Na9d6a7e2015-09-28 12:56:30 +05301948static int omap_hsmmc_probe(struct udevice *dev)
1949{
Jean-Jacques Hiblot3d673ff2017-03-22 16:00:33 +01001950 struct omap_hsmmc_plat *plat = dev_get_platdata(dev);
Mugunthan V Na9d6a7e2015-09-28 12:56:30 +05301951 struct mmc_uclass_priv *upriv = dev_get_uclass_priv(dev);
1952 struct omap_hsmmc_data *priv = dev_get_priv(dev);
Jean-Jacques Hiblot3d673ff2017-03-22 16:00:33 +01001953 struct mmc_config *cfg = &plat->cfg;
Mugunthan V Na9d6a7e2015-09-28 12:56:30 +05301954 struct mmc *mmc;
Kishon Vijay Abraham I33c1d772018-01-30 16:01:40 +01001955#ifdef CONFIG_IODELAY_RECALIBRATION
1956 int ret;
1957#endif
Mugunthan V Na9d6a7e2015-09-28 12:56:30 +05301958
Mugunthan V Na9d6a7e2015-09-28 12:56:30 +05301959 cfg->name = "OMAP SD/MMC";
Lokesh Vutla2558c042017-04-26 13:37:05 +05301960 priv->base_addr = plat->base_addr;
Kishon Vijay Abraham I33c1d772018-01-30 16:01:40 +01001961 priv->controller_flags = plat->controller_flags;
Kishon Vijay Abraham I2d28eed2018-01-30 16:01:41 +01001962 priv->hw_rev = plat->hw_rev;
Mugunthan V Na9d6a7e2015-09-28 12:56:30 +05301963
Jean-Jacques Hiblot17c9a1c2017-03-22 16:00:34 +01001964#ifdef CONFIG_BLK
Jean-Jacques Hiblot45530e32018-02-23 10:40:16 +01001965 mmc = plat->mmc;
Jean-Jacques Hiblot17c9a1c2017-03-22 16:00:34 +01001966#else
Mugunthan V Na9d6a7e2015-09-28 12:56:30 +05301967 mmc = mmc_create(cfg, priv);
1968 if (mmc == NULL)
1969 return -1;
Jean-Jacques Hiblot17c9a1c2017-03-22 16:00:34 +01001970#endif
Jean-Jacques Hiblot04f9f8b2018-01-30 16:01:46 +01001971#if CONFIG_IS_ENABLED(DM_REGULATOR)
1972 device_get_supply_regulator(dev, "pbias-supply",
1973 &priv->pbias_supply);
1974#endif
Adam Ford307a2142018-08-21 07:16:56 -05001975#if defined(OMAP_HSMMC_USE_GPIO)
1976#if CONFIG_IS_ENABLED(OF_CONTROL) && CONFIG_IS_ENABLED(DM_GPIO)
Mugunthan V N5cc6a242016-04-04 17:28:01 +05301977 gpio_request_by_name(dev, "cd-gpios", 0, &priv->cd_gpio, GPIOD_IS_IN);
1978 gpio_request_by_name(dev, "wp-gpios", 0, &priv->wp_gpio, GPIOD_IS_IN);
1979#endif
Adam Ford307a2142018-08-21 07:16:56 -05001980#endif
Mugunthan V N5cc6a242016-04-04 17:28:01 +05301981
Simon Glasscffe5d82016-05-01 13:52:34 -06001982 mmc->dev = dev;
Mugunthan V Na9d6a7e2015-09-28 12:56:30 +05301983 upriv->mmc = mmc;
1984
Kishon Vijay Abraham I33c1d772018-01-30 16:01:40 +01001985#ifdef CONFIG_IODELAY_RECALIBRATION
1986 ret = omap_hsmmc_get_pinctrl_state(mmc);
1987 /*
1988 * disable high speed modes for the platforms that require IO delay
1989 * and for which we don't have this information
1990 */
1991 if ((ret < 0) &&
1992 (priv->controller_flags & OMAP_HSMMC_REQUIRE_IODELAY)) {
1993 priv->controller_flags &= ~OMAP_HSMMC_REQUIRE_IODELAY;
1994 cfg->host_caps &= ~(MMC_CAP(MMC_HS_200) | MMC_CAP(MMC_DDR_52) |
1995 UHS_CAPS);
1996 }
1997#endif
1998
Jean-Jacques Hiblotb5511d62017-04-14 19:50:02 +02001999 return omap_hsmmc_init_setup(mmc);
Mugunthan V Na9d6a7e2015-09-28 12:56:30 +05302000}
2001
Lokesh Vutla2558c042017-04-26 13:37:05 +05302002#if CONFIG_IS_ENABLED(OF_CONTROL) && !CONFIG_IS_ENABLED(OF_PLATDATA)
Kishon Vijay Abraham I33c1d772018-01-30 16:01:40 +01002003
2004static const struct omap_mmc_of_data dra7_mmc_of_data = {
2005 .controller_flags = OMAP_HSMMC_REQUIRE_IODELAY,
2006};
2007
Mugunthan V Na9d6a7e2015-09-28 12:56:30 +05302008static const struct udevice_id omap_hsmmc_ids[] = {
Jean-Jacques Hiblot741726a2017-09-21 16:51:32 +02002009 { .compatible = "ti,omap3-hsmmc" },
2010 { .compatible = "ti,omap4-hsmmc" },
2011 { .compatible = "ti,am33xx-hsmmc" },
Kishon Vijay Abraham I33c1d772018-01-30 16:01:40 +01002012 { .compatible = "ti,dra7-hsmmc", .data = (ulong)&dra7_mmc_of_data },
Mugunthan V Na9d6a7e2015-09-28 12:56:30 +05302013 { }
2014};
Lokesh Vutla2558c042017-04-26 13:37:05 +05302015#endif
Mugunthan V Na9d6a7e2015-09-28 12:56:30 +05302016
2017U_BOOT_DRIVER(omap_hsmmc) = {
2018 .name = "omap_hsmmc",
2019 .id = UCLASS_MMC,
Lokesh Vutla2558c042017-04-26 13:37:05 +05302020#if CONFIG_IS_ENABLED(OF_CONTROL) && !CONFIG_IS_ENABLED(OF_PLATDATA)
Mugunthan V Na9d6a7e2015-09-28 12:56:30 +05302021 .of_match = omap_hsmmc_ids,
2022 .ofdata_to_platdata = omap_hsmmc_ofdata_to_platdata,
Lokesh Vutla2558c042017-04-26 13:37:05 +05302023 .platdata_auto_alloc_size = sizeof(struct omap_hsmmc_plat),
2024#endif
Jean-Jacques Hiblot17c9a1c2017-03-22 16:00:34 +01002025#ifdef CONFIG_BLK
2026 .bind = omap_hsmmc_bind,
2027#endif
Jean-Jacques Hiblotb5511d62017-04-14 19:50:02 +02002028 .ops = &omap_hsmmc_ops,
Mugunthan V Na9d6a7e2015-09-28 12:56:30 +05302029 .probe = omap_hsmmc_probe,
2030 .priv_auto_alloc_size = sizeof(struct omap_hsmmc_data),
Bin Meng223b10c2018-10-24 06:36:32 -07002031#if !CONFIG_IS_ENABLED(OF_CONTROL)
Lokesh Vutlacbcb1702017-04-26 13:37:06 +05302032 .flags = DM_FLAG_PRE_RELOC,
Bin Meng223b10c2018-10-24 06:36:32 -07002033#endif
Mugunthan V Na9d6a7e2015-09-28 12:56:30 +05302034};
2035#endif