blob: 8e51453d2aee09667c81a36064cb08449f1a538d [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>
Simon Glass63334482019-11-14 12:57:39 -070026#include <cpu_func.h>
Simon Glass0f2af882020-05-10 11:40:05 -060027#include <log.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>
Tom Rini8c6b4372024-07-15 13:35:53 -060033#if defined(CONFIG_OMAP54XX)
Nishanth Menon627612c2013-03-26 05:20:54 +000034#include <palmas.h>
Felix Brack419eed22017-10-11 17:05:28 +020035#endif
Simon Glass274e0b02020-05-10 11:39:56 -060036#include <asm/cache.h>
Simon Glass3ba929a2020-10-30 21:38:53 -060037#include <asm/global_data.h>
Sukumar Ghoraic53f5e52010-09-18 20:32:33 -070038#include <asm/io.h>
39#include <asm/arch/mmc_host_def.h>
Kishon Vijay Abraham Ie7da6ac2018-01-30 16:01:40 +010040#ifdef CONFIG_OMAP54XX
41#include <asm/arch/mux_dra7xx.h>
42#include <asm/arch/dra7xx_iodelay.h>
43#endif
Tom Rini84c0f692021-09-12 20:32:32 -040044#if !defined(CONFIG_ARCH_KEYSTONE)
Roger Quadros44157de2015-09-19 16:26:53 +053045#include <asm/gpio.h>
Dirk Behme74140232011-05-15 09:04:47 +000046#include <asm/arch/sys_proto.h>
Roger Quadros44157de2015-09-19 16:26:53 +053047#endif
Tom Rinidf5338c2017-02-09 13:41:28 -050048#ifdef CONFIG_MMC_OMAP36XX_PINS
49#include <asm/arch/mux.h>
50#endif
Mugunthan V Nd97631a2015-09-28 12:56:30 +053051#include <dm.h>
Simon Glassd66c5f72020-02-03 07:36:15 -070052#include <dm/devres.h>
Simon Glass4dcacfc2020-05-10 11:40:13 -060053#include <linux/bitops.h>
Simon Glassdbd79542020-05-10 11:40:11 -060054#include <linux/delay.h>
Simon Glassd66c5f72020-02-03 07:36:15 -070055#include <linux/err.h>
Jean-Jacques Hiblot20157d42018-01-30 16:01:44 +010056#include <power/regulator.h>
Faiz Abbase4d30562019-01-30 18:08:42 +053057#include <thermal.h>
Mugunthan V Nd97631a2015-09-28 12:56:30 +053058
59DECLARE_GLOBAL_DATA_PTR;
Sukumar Ghoraic53f5e52010-09-18 20:32:33 -070060
Pantelis Antoniouc9e75912014-02-26 19:28:45 +020061/* simplify defines to OMAP_HSMMC_USE_GPIO */
62#if (defined(CONFIG_OMAP_GPIO) && !defined(CONFIG_SPL_BUILD)) || \
Simon Glass035939e2021-07-10 21:14:30 -060063 (defined(CONFIG_SPL_BUILD) && defined(CONFIG_SPL_GPIO))
Pantelis Antoniouc9e75912014-02-26 19:28:45 +020064#define OMAP_HSMMC_USE_GPIO
65#else
66#undef OMAP_HSMMC_USE_GPIO
67#endif
68
Grazvydas Ignotasddde1882012-03-19 12:12:06 +000069/* common definitions for all OMAPs */
70#define SYSCTL_SRC (1 << 25)
71#define SYSCTL_SRD (1 << 26)
72
Kishon Vijay Abraham Ie7da6ac2018-01-30 16:01:40 +010073#ifdef CONFIG_IODELAY_RECALIBRATION
74struct omap_hsmmc_pinctrl_state {
75 struct pad_conf_entry *padconf;
76 int npads;
77 struct iodelay_cfg_entry *iodelay;
78 int niodelays;
79};
80#endif
81
Nikita Kiryanov13822862012-12-03 02:19:43 +000082struct omap_hsmmc_data {
83 struct hsmmc *base_addr;
Simon Glass5f4bd8c2017-07-04 13:31:19 -060084#if !CONFIG_IS_ENABLED(DM_MMC)
Pantelis Antoniou2c850462014-03-11 19:34:20 +020085 struct mmc_config cfg;
Jean-Jacques Hiblotae51a662017-03-22 16:00:33 +010086#endif
Kishon Vijay Abraham I2e18c9b2018-01-30 16:01:31 +010087 uint bus_width;
Jean-Jacques Hiblot7fe2f192018-01-30 16:01:30 +010088 uint clock;
Jean-Jacques Hiblot7a41bb42018-01-30 16:01:46 +010089 ushort last_cmd;
Pantelis Antoniouc9e75912014-02-26 19:28:45 +020090#ifdef OMAP_HSMMC_USE_GPIO
Simon Glass5f4bd8c2017-07-04 13:31:19 -060091#if CONFIG_IS_ENABLED(DM_MMC)
Mugunthan V Nd97631a2015-09-28 12:56:30 +053092 struct gpio_desc cd_gpio; /* Change Detect GPIO */
93 struct gpio_desc wp_gpio; /* Write Protect GPIO */
Mugunthan V Nd97631a2015-09-28 12:56:30 +053094#else
Nikita Kiryanov4eae05c2012-12-03 02:19:44 +000095 int cd_gpio;
Nikita Kiryanov4be9dbc2012-12-03 02:19:47 +000096 int wp_gpio;
Pantelis Antoniouc9e75912014-02-26 19:28:45 +020097#endif
Mugunthan V Nd97631a2015-09-28 12:56:30 +053098#endif
Kishon Vijay Abraham I73897ed2018-01-30 16:01:32 +010099#if CONFIG_IS_ENABLED(DM_MMC)
Jean-Jacques Hiblotcf38d4e2018-01-30 16:01:33 +0100100 enum bus_mode mode;
Kishon Vijay Abraham I73897ed2018-01-30 16:01:32 +0100101#endif
Kishon Vijay Abraham I826be2a2017-09-21 16:51:34 +0200102 u8 controller_flags;
Jean-Jacques Hiblotcebf0592018-02-23 10:40:18 +0100103#ifdef CONFIG_MMC_OMAP_HS_ADMA
Kishon Vijay Abraham I826be2a2017-09-21 16:51:34 +0200104 struct omap_hsmmc_adma_desc *adma_desc_table;
105 uint desc_slot;
106#endif
Kishon Vijay Abraham I8c2efe92018-01-30 16:01:41 +0100107 const char *hw_rev;
Jean-Jacques Hiblot7a41bb42018-01-30 16:01:46 +0100108 struct udevice *pbias_supply;
109 uint signal_voltage;
Kishon Vijay Abraham Ie7da6ac2018-01-30 16:01:40 +0100110#ifdef CONFIG_IODELAY_RECALIBRATION
111 struct omap_hsmmc_pinctrl_state *default_pinctrl_state;
112 struct omap_hsmmc_pinctrl_state *hs_pinctrl_state;
113 struct omap_hsmmc_pinctrl_state *hs200_1_8v_pinctrl_state;
114 struct omap_hsmmc_pinctrl_state *ddr_1_8v_pinctrl_state;
115 struct omap_hsmmc_pinctrl_state *sdr12_pinctrl_state;
116 struct omap_hsmmc_pinctrl_state *sdr25_pinctrl_state;
117 struct omap_hsmmc_pinctrl_state *ddr50_pinctrl_state;
118 struct omap_hsmmc_pinctrl_state *sdr50_pinctrl_state;
119 struct omap_hsmmc_pinctrl_state *sdr104_pinctrl_state;
120#endif
Kishon Vijay Abraham I826be2a2017-09-21 16:51:34 +0200121};
122
Kishon Vijay Abraham Ie7da6ac2018-01-30 16:01:40 +0100123struct omap_mmc_of_data {
124 u8 controller_flags;
125};
126
Jean-Jacques Hiblotcebf0592018-02-23 10:40:18 +0100127#ifdef CONFIG_MMC_OMAP_HS_ADMA
Kishon Vijay Abraham I826be2a2017-09-21 16:51:34 +0200128struct omap_hsmmc_adma_desc {
129 u8 attr;
130 u8 reserved;
131 u16 len;
132 u32 addr;
Nikita Kiryanov13822862012-12-03 02:19:43 +0000133};
134
Kishon Vijay Abraham I826be2a2017-09-21 16:51:34 +0200135#define ADMA_MAX_LEN 63488
136
137/* Decriptor table defines */
138#define ADMA_DESC_ATTR_VALID BIT(0)
139#define ADMA_DESC_ATTR_END BIT(1)
140#define ADMA_DESC_ATTR_INT BIT(2)
141#define ADMA_DESC_ATTR_ACT1 BIT(4)
142#define ADMA_DESC_ATTR_ACT2 BIT(5)
143
144#define ADMA_DESC_TRANSFER_DATA ADMA_DESC_ATTR_ACT2
145#define ADMA_DESC_LINK_DESC (ADMA_DESC_ATTR_ACT1 | ADMA_DESC_ATTR_ACT2)
146#endif
147
Nishanth Menond3bfaac2010-11-19 11:18:12 -0500148/* If we fail after 1 second wait, something is really bad */
149#define MAX_RETRY_MS 1000
Jean-Jacques Hiblot192e4302018-01-30 16:01:37 +0100150#define MMC_TIMEOUT_MS 20
Nishanth Menond3bfaac2010-11-19 11:18:12 -0500151
Kishon Vijay Abraham I826be2a2017-09-21 16:51:34 +0200152/* DMA transfers can take a long time if a lot a data is transferred.
153 * The timeout must take in account the amount of data. Let's assume
154 * that the time will never exceed 333 ms per MB (in other word we assume
155 * that the bandwidth is always above 3MB/s).
156 */
157#define DMA_TIMEOUT_PER_MB 333
Kishon Vijay Abraham I73897ed2018-01-30 16:01:32 +0100158#define OMAP_HSMMC_SUPPORTS_DUAL_VOLT BIT(0)
159#define OMAP_HSMMC_NO_1_8_V BIT(1)
Kishon Vijay Abraham I826be2a2017-09-21 16:51:34 +0200160#define OMAP_HSMMC_USE_ADMA BIT(2)
Kishon Vijay Abraham Ie7da6ac2018-01-30 16:01:40 +0100161#define OMAP_HSMMC_REQUIRE_IODELAY BIT(3)
Kishon Vijay Abraham I826be2a2017-09-21 16:51:34 +0200162
Sricharanf72611f2011-11-15 09:49:53 -0500163static int mmc_read_data(struct hsmmc *mmc_base, char *buf, unsigned int size);
164static int mmc_write_data(struct hsmmc *mmc_base, const char *buf,
165 unsigned int siz);
Jean-Jacques Hiblot7fe2f192018-01-30 16:01:30 +0100166static void omap_hsmmc_start_clock(struct hsmmc *mmc_base);
167static void omap_hsmmc_stop_clock(struct hsmmc *mmc_base);
Jean-Jacques Hiblotf0f821b2018-01-30 16:01:35 +0100168static void mmc_reset_controller_fsm(struct hsmmc *mmc_base, u32 bit);
Balaji T Kf843d332011-09-08 06:34:57 +0000169
Jean-Jacques Hiblotd58ef8e2017-03-22 16:00:31 +0100170static inline struct omap_hsmmc_data *omap_hsmmc_get_data(struct mmc *mmc)
171{
Simon Glass5f4bd8c2017-07-04 13:31:19 -0600172#if CONFIG_IS_ENABLED(DM_MMC)
Jean-Jacques Hiblotd58ef8e2017-03-22 16:00:31 +0100173 return dev_get_priv(mmc->dev);
174#else
175 return (struct omap_hsmmc_data *)mmc->priv;
176#endif
177}
Tom Rinibf3e2462020-06-04 16:03:55 -0400178
179#if defined(CONFIG_OMAP34XX) || defined(CONFIG_IODELAY_RECALIBRATION)
Jean-Jacques Hiblotae51a662017-03-22 16:00:33 +0100180static inline struct mmc_config *omap_hsmmc_get_cfg(struct mmc *mmc)
181{
Simon Glass5f4bd8c2017-07-04 13:31:19 -0600182#if CONFIG_IS_ENABLED(DM_MMC)
Simon Glassfa20e932020-12-03 16:55:20 -0700183 struct omap_hsmmc_plat *plat = dev_get_plat(mmc->dev);
Jean-Jacques Hiblotae51a662017-03-22 16:00:33 +0100184 return &plat->cfg;
185#else
186 return &((struct omap_hsmmc_data *)mmc->priv)->cfg;
187#endif
188}
Tom Rinibf3e2462020-06-04 16:03:55 -0400189#endif
Jean-Jacques Hiblotd58ef8e2017-03-22 16:00:31 +0100190
Simon Glass5f4bd8c2017-07-04 13:31:19 -0600191#if defined(OMAP_HSMMC_USE_GPIO) && !CONFIG_IS_ENABLED(DM_MMC)
Nikita Kiryanov4eae05c2012-12-03 02:19:44 +0000192static int omap_mmc_setup_gpio_in(int gpio, const char *label)
193{
Simon Glass1a96d7f2014-10-22 21:37:09 -0600194 int ret;
Nikita Kiryanov4eae05c2012-12-03 02:19:44 +0000195
Simon Glassfa4689a2019-12-06 21:41:35 -0700196#if !CONFIG_IS_ENABLED(DM_GPIO)
Simon Glass1a96d7f2014-10-22 21:37:09 -0600197 if (!gpio_is_valid(gpio))
Nikita Kiryanov4eae05c2012-12-03 02:19:44 +0000198 return -1;
Simon Glass1a96d7f2014-10-22 21:37:09 -0600199#endif
200 ret = gpio_request(gpio, label);
201 if (ret)
202 return ret;
Nikita Kiryanov4eae05c2012-12-03 02:19:44 +0000203
Simon Glass1a96d7f2014-10-22 21:37:09 -0600204 ret = gpio_direction_input(gpio);
205 if (ret)
206 return ret;
Nikita Kiryanov4eae05c2012-12-03 02:19:44 +0000207
208 return gpio;
209}
Nikita Kiryanov4eae05c2012-12-03 02:19:44 +0000210#endif
211
Jeroen Hofsteeaedeeaa2014-07-12 21:24:08 +0200212static unsigned char mmc_board_init(struct mmc *mmc)
Sukumar Ghoraic53f5e52010-09-18 20:32:33 -0700213{
Sukumar Ghoraic53f5e52010-09-18 20:32:33 -0700214#if defined(CONFIG_OMAP34XX)
Jean-Jacques Hiblotae51a662017-03-22 16:00:33 +0100215 struct mmc_config *cfg = omap_hsmmc_get_cfg(mmc);
Sukumar Ghoraic53f5e52010-09-18 20:32:33 -0700216 t2_t *t2_base = (t2_t *)T2_BASE;
217 struct prcm *prcm_base = (struct prcm *)PRCM_BASE;
Grazvydas Ignotasef2b7292012-03-19 03:50:53 +0000218 u32 pbias_lite;
Adam Fordef354962017-02-06 11:31:43 -0600219#ifdef CONFIG_MMC_OMAP36XX_PINS
220 u32 wkup_ctrl = readl(OMAP34XX_CTRL_WKUP_CTRL);
221#endif
Sukumar Ghoraic53f5e52010-09-18 20:32:33 -0700222
Grazvydas Ignotasef2b7292012-03-19 03:50:53 +0000223 pbias_lite = readl(&t2_base->pbias_lite);
224 pbias_lite &= ~(PBIASLITEPWRDNZ1 | PBIASLITEPWRDNZ0);
Albert ARIBAUD \(3ADEV\)6ad09812015-01-16 09:09:50 +0100225#ifdef CONFIG_TARGET_OMAP3_CAIRO
226 /* for cairo board, we need to set up 1.8 Volt bias level on MMC1 */
227 pbias_lite &= ~PBIASLITEVMODE0;
228#endif
Adam Fordf2eb4322018-09-05 04:11:08 -0500229#ifdef CONFIG_TARGET_OMAP3_LOGIC
230 /* For Logic PD board, 1.8V bias to go enable gpio127 for mmc_cd */
231 pbias_lite &= ~PBIASLITEVMODE1;
232#endif
Adam Fordef354962017-02-06 11:31:43 -0600233#ifdef CONFIG_MMC_OMAP36XX_PINS
234 if (get_cpu_family() == CPU_OMAP36XX) {
235 /* Disable extended drain IO before changing PBIAS */
236 wkup_ctrl &= ~OMAP34XX_CTRL_WKUP_CTRL_GPIO_IO_PWRDNZ;
237 writel(wkup_ctrl, OMAP34XX_CTRL_WKUP_CTRL);
238 }
239#endif
Grazvydas Ignotasef2b7292012-03-19 03:50:53 +0000240 writel(pbias_lite, &t2_base->pbias_lite);
Paul Kocialkowski69559892014-11-08 20:55:47 +0100241
Grazvydas Ignotasef2b7292012-03-19 03:50:53 +0000242 writel(pbias_lite | PBIASLITEPWRDNZ1 |
Sukumar Ghoraic53f5e52010-09-18 20:32:33 -0700243 PBIASSPEEDCTRL0 | PBIASLITEPWRDNZ0,
244 &t2_base->pbias_lite);
245
Adam Fordef354962017-02-06 11:31:43 -0600246#ifdef CONFIG_MMC_OMAP36XX_PINS
247 if (get_cpu_family() == CPU_OMAP36XX)
248 /* Enable extended drain IO after changing PBIAS */
249 writel(wkup_ctrl |
250 OMAP34XX_CTRL_WKUP_CTRL_GPIO_IO_PWRDNZ,
251 OMAP34XX_CTRL_WKUP_CTRL);
252#endif
Sukumar Ghoraic53f5e52010-09-18 20:32:33 -0700253 writel(readl(&t2_base->devconf0) | MMCSDIO1ADPCLKISEL,
254 &t2_base->devconf0);
255
256 writel(readl(&t2_base->devconf1) | MMCSDIO2ADPCLKISEL,
257 &t2_base->devconf1);
258
Jonathan Solnita9b05562012-02-24 11:30:18 +0000259 /* Change from default of 52MHz to 26MHz if necessary */
Jean-Jacques Hiblotae51a662017-03-22 16:00:33 +0100260 if (!(cfg->host_caps & MMC_MODE_HS_52MHz))
Jonathan Solnita9b05562012-02-24 11:30:18 +0000261 writel(readl(&t2_base->ctl_prog_io1) & ~CTLPROGIO1SPEEDCTRL,
262 &t2_base->ctl_prog_io1);
263
Sukumar Ghoraic53f5e52010-09-18 20:32:33 -0700264 writel(readl(&prcm_base->fclken1_core) |
265 EN_MMC1 | EN_MMC2 | EN_MMC3,
266 &prcm_base->fclken1_core);
267
268 writel(readl(&prcm_base->iclken1_core) |
269 EN_MMC1 | EN_MMC2 | EN_MMC3,
270 &prcm_base->iclken1_core);
271#endif
272
Tom Rini8c6b4372024-07-15 13:35:53 -0600273#if defined(CONFIG_OMAP54XX) && !CONFIG_IS_ENABLED(DM_REGULATOR)
Balaji T Kf843d332011-09-08 06:34:57 +0000274 /* PBIAS config needed for MMC1 only */
Jean-Jacques Hiblot26319b12017-03-22 16:00:32 +0100275 if (mmc_get_blk_desc(mmc)->devnum == 0)
Faiz Abbasfc1ad622019-04-05 14:18:46 +0530276 vmmc_pbias_config(LDO_VOLT_3V3);
Balaji T Kd9cf8362012-03-12 02:25:49 +0000277#endif
Sukumar Ghoraic53f5e52010-09-18 20:32:33 -0700278
279 return 0;
280}
281
Sricharanf72611f2011-11-15 09:49:53 -0500282void mmc_init_stream(struct hsmmc *mmc_base)
Sukumar Ghoraic53f5e52010-09-18 20:32:33 -0700283{
Nishanth Menond3bfaac2010-11-19 11:18:12 -0500284 ulong start;
Sukumar Ghoraic53f5e52010-09-18 20:32:33 -0700285
286 writel(readl(&mmc_base->con) | INIT_INITSTREAM, &mmc_base->con);
287
288 writel(MMC_CMD0, &mmc_base->cmd);
Nishanth Menond3bfaac2010-11-19 11:18:12 -0500289 start = get_timer(0);
290 while (!(readl(&mmc_base->stat) & CC_MASK)) {
291 if (get_timer(0) - start > MAX_RETRY_MS) {
292 printf("%s: timedout waiting for cc!\n", __func__);
293 return;
294 }
295 }
Sukumar Ghoraic53f5e52010-09-18 20:32:33 -0700296 writel(CC_MASK, &mmc_base->stat)
297 ;
298 writel(MMC_CMD0, &mmc_base->cmd)
299 ;
Nishanth Menond3bfaac2010-11-19 11:18:12 -0500300 start = get_timer(0);
301 while (!(readl(&mmc_base->stat) & CC_MASK)) {
302 if (get_timer(0) - start > MAX_RETRY_MS) {
303 printf("%s: timedout waiting for cc2!\n", __func__);
304 return;
305 }
306 }
Sukumar Ghoraic53f5e52010-09-18 20:32:33 -0700307 writel(readl(&mmc_base->con) & ~INIT_INITSTREAM, &mmc_base->con);
308}
Kishon Vijay Abraham I73897ed2018-01-30 16:01:32 +0100309
310#if CONFIG_IS_ENABLED(DM_MMC)
Kishon Vijay Abraham Ie7da6ac2018-01-30 16:01:40 +0100311#ifdef CONFIG_IODELAY_RECALIBRATION
312static void omap_hsmmc_io_recalibrate(struct mmc *mmc)
313{
314 struct omap_hsmmc_data *priv = omap_hsmmc_get_data(mmc);
315 struct omap_hsmmc_pinctrl_state *pinctrl_state;
316
317 switch (priv->mode) {
318 case MMC_HS_200:
319 pinctrl_state = priv->hs200_1_8v_pinctrl_state;
320 break;
321 case UHS_SDR104:
322 pinctrl_state = priv->sdr104_pinctrl_state;
323 break;
324 case UHS_SDR50:
325 pinctrl_state = priv->sdr50_pinctrl_state;
326 break;
327 case UHS_DDR50:
328 pinctrl_state = priv->ddr50_pinctrl_state;
329 break;
330 case UHS_SDR25:
331 pinctrl_state = priv->sdr25_pinctrl_state;
332 break;
333 case UHS_SDR12:
334 pinctrl_state = priv->sdr12_pinctrl_state;
335 break;
336 case SD_HS:
337 case MMC_HS:
338 case MMC_HS_52:
339 pinctrl_state = priv->hs_pinctrl_state;
340 break;
341 case MMC_DDR_52:
342 pinctrl_state = priv->ddr_1_8v_pinctrl_state;
343 default:
344 pinctrl_state = priv->default_pinctrl_state;
345 break;
346 }
347
Jean-Jacques Hiblotdae1ad42018-01-30 16:01:42 +0100348 if (!pinctrl_state)
349 pinctrl_state = priv->default_pinctrl_state;
350
Kishon Vijay Abraham Ie7da6ac2018-01-30 16:01:40 +0100351 if (priv->controller_flags & OMAP_HSMMC_REQUIRE_IODELAY) {
352 if (pinctrl_state->iodelay)
353 late_recalibrate_iodelay(pinctrl_state->padconf,
354 pinctrl_state->npads,
355 pinctrl_state->iodelay,
356 pinctrl_state->niodelays);
357 else
358 do_set_mux32((*ctrl)->control_padconf_core_base,
359 pinctrl_state->padconf,
360 pinctrl_state->npads);
361 }
362}
363#endif
Jean-Jacques Hiblotcf38d4e2018-01-30 16:01:33 +0100364static void omap_hsmmc_set_timing(struct mmc *mmc)
365{
366 u32 val;
367 struct hsmmc *mmc_base;
368 struct omap_hsmmc_data *priv = omap_hsmmc_get_data(mmc);
369
370 mmc_base = priv->base_addr;
371
Kishon Vijay Abraham Ie7da6ac2018-01-30 16:01:40 +0100372 omap_hsmmc_stop_clock(mmc_base);
Jean-Jacques Hiblotcf38d4e2018-01-30 16:01:33 +0100373 val = readl(&mmc_base->ac12);
374 val &= ~AC12_UHSMC_MASK;
375 priv->mode = mmc->selected_mode;
376
Kishon Vijay Abraham I0c1f3d02018-01-30 16:01:34 +0100377 if (mmc_is_mode_ddr(priv->mode))
378 writel(readl(&mmc_base->con) | DDR, &mmc_base->con);
379 else
380 writel(readl(&mmc_base->con) & ~DDR, &mmc_base->con);
381
Jean-Jacques Hiblotcf38d4e2018-01-30 16:01:33 +0100382 switch (priv->mode) {
383 case MMC_HS_200:
384 case UHS_SDR104:
385 val |= AC12_UHSMC_SDR104;
386 break;
387 case UHS_SDR50:
388 val |= AC12_UHSMC_SDR50;
389 break;
390 case MMC_DDR_52:
391 case UHS_DDR50:
392 val |= AC12_UHSMC_DDR50;
393 break;
394 case SD_HS:
395 case MMC_HS_52:
396 case UHS_SDR25:
397 val |= AC12_UHSMC_SDR25;
398 break;
399 case MMC_LEGACY:
400 case MMC_HS:
Jean-Jacques Hiblotcf38d4e2018-01-30 16:01:33 +0100401 case UHS_SDR12:
402 val |= AC12_UHSMC_SDR12;
403 break;
404 default:
405 val |= AC12_UHSMC_RES;
406 break;
407 }
408 writel(val, &mmc_base->ac12);
Kishon Vijay Abraham Ie7da6ac2018-01-30 16:01:40 +0100409
410#ifdef CONFIG_IODELAY_RECALIBRATION
411 omap_hsmmc_io_recalibrate(mmc);
412#endif
413 omap_hsmmc_start_clock(mmc_base);
Jean-Jacques Hiblotcf38d4e2018-01-30 16:01:33 +0100414}
415
Jean-Jacques Hiblot7a41bb42018-01-30 16:01:46 +0100416static void omap_hsmmc_conf_bus_power(struct mmc *mmc, uint signal_voltage)
Kishon Vijay Abraham I73897ed2018-01-30 16:01:32 +0100417{
418 struct hsmmc *mmc_base;
419 struct omap_hsmmc_data *priv = omap_hsmmc_get_data(mmc);
Jean-Jacques Hiblot7a41bb42018-01-30 16:01:46 +0100420 u32 hctl, ac12;
Kishon Vijay Abraham I73897ed2018-01-30 16:01:32 +0100421
422 mmc_base = priv->base_addr;
423
Jean-Jacques Hiblot7a41bb42018-01-30 16:01:46 +0100424 hctl = readl(&mmc_base->hctl) & ~SDVS_MASK;
425 ac12 = readl(&mmc_base->ac12) & ~AC12_V1V8_SIGEN;
Kishon Vijay Abraham I73897ed2018-01-30 16:01:32 +0100426
Jean-Jacques Hiblot7a41bb42018-01-30 16:01:46 +0100427 switch (signal_voltage) {
428 case MMC_SIGNAL_VOLTAGE_330:
Faiz Abbasfc1ad622019-04-05 14:18:46 +0530429 hctl |= SDVS_3V3;
Kishon Vijay Abraham I73897ed2018-01-30 16:01:32 +0100430 break;
Jean-Jacques Hiblot7a41bb42018-01-30 16:01:46 +0100431 case MMC_SIGNAL_VOLTAGE_180:
432 hctl |= SDVS_1V8;
433 ac12 |= AC12_V1V8_SIGEN;
Kishon Vijay Abraham I73897ed2018-01-30 16:01:32 +0100434 break;
435 }
436
Jean-Jacques Hiblot7a41bb42018-01-30 16:01:46 +0100437 writel(hctl, &mmc_base->hctl);
438 writel(ac12, &mmc_base->ac12);
439}
440
Sam Protsenkodb174c62019-08-14 22:52:51 +0300441static int omap_hsmmc_wait_dat0(struct udevice *dev, int state, int timeout_us)
Jean-Jacques Hiblot7a41bb42018-01-30 16:01:46 +0100442{
443 int ret = -ETIMEDOUT;
444 u32 con;
445 bool dat0_high;
446 bool target_dat0_high = !!state;
447 struct omap_hsmmc_data *priv = dev_get_priv(dev);
448 struct hsmmc *mmc_base = priv->base_addr;
449
450 con = readl(&mmc_base->con);
451 writel(con | CON_CLKEXTFREE | CON_PADEN, &mmc_base->con);
452
Sam Protsenkodb174c62019-08-14 22:52:51 +0300453 timeout_us = DIV_ROUND_UP(timeout_us, 10); /* check every 10 us. */
454 while (timeout_us--) {
Jean-Jacques Hiblot7a41bb42018-01-30 16:01:46 +0100455 dat0_high = !!(readl(&mmc_base->pstate) & PSTATE_DLEV_DAT0);
456 if (dat0_high == target_dat0_high) {
457 ret = 0;
458 break;
459 }
460 udelay(10);
461 }
462 writel(con, &mmc_base->con);
463
464 return ret;
465}
Jean-Jacques Hiblot7a41bb42018-01-30 16:01:46 +0100466
467#if CONFIG_IS_ENABLED(MMC_IO_VOLTAGE)
468#if CONFIG_IS_ENABLED(DM_REGULATOR)
469static int omap_hsmmc_set_io_regulator(struct mmc *mmc, int mV)
470{
471 int ret = 0;
472 int uV = mV * 1000;
473
474 struct omap_hsmmc_data *priv = omap_hsmmc_get_data(mmc);
475
476 if (!mmc->vqmmc_supply)
477 return 0;
478
479 /* Disable PBIAS */
Lokesh Vutlab2691972019-01-11 15:15:52 +0530480 ret = regulator_set_enable_if_allowed(priv->pbias_supply, false);
481 if (ret)
Jean-Jacques Hiblot7a41bb42018-01-30 16:01:46 +0100482 return ret;
483
484 /* Turn off IO voltage */
Lokesh Vutlab2691972019-01-11 15:15:52 +0530485 ret = regulator_set_enable_if_allowed(mmc->vqmmc_supply, false);
486 if (ret)
Jean-Jacques Hiblot7a41bb42018-01-30 16:01:46 +0100487 return ret;
488 /* Program a new IO voltage value */
489 ret = regulator_set_value(mmc->vqmmc_supply, uV);
490 if (ret)
491 return ret;
492 /* Turn on IO voltage */
Lokesh Vutlab2691972019-01-11 15:15:52 +0530493 ret = regulator_set_enable_if_allowed(mmc->vqmmc_supply, true);
494 if (ret)
Jean-Jacques Hiblot7a41bb42018-01-30 16:01:46 +0100495 return ret;
496
497 /* Program PBIAS voltage*/
498 ret = regulator_set_value(priv->pbias_supply, uV);
499 if (ret && ret != -ENOSYS)
500 return ret;
501 /* Enable PBIAS */
Lokesh Vutlab2691972019-01-11 15:15:52 +0530502 ret = regulator_set_enable_if_allowed(priv->pbias_supply, true);
503 if (ret)
Jean-Jacques Hiblot7a41bb42018-01-30 16:01:46 +0100504 return ret;
505
506 return 0;
Kishon Vijay Abraham I73897ed2018-01-30 16:01:32 +0100507}
Jean-Jacques Hiblot7a41bb42018-01-30 16:01:46 +0100508#endif
Kishon Vijay Abraham I73897ed2018-01-30 16:01:32 +0100509
Jean-Jacques Hiblot7a41bb42018-01-30 16:01:46 +0100510static int omap_hsmmc_set_signal_voltage(struct mmc *mmc)
511{
512 struct omap_hsmmc_data *priv = omap_hsmmc_get_data(mmc);
513 struct hsmmc *mmc_base = priv->base_addr;
514 int mv = mmc_voltage_to_mv(mmc->signal_voltage);
515 u32 capa_mask;
516 __maybe_unused u8 palmas_ldo_volt;
517 u32 val;
518
519 if (mv < 0)
520 return -EINVAL;
521
522 if (mmc->signal_voltage == MMC_SIGNAL_VOLTAGE_330) {
Faiz Abbasfc1ad622019-04-05 14:18:46 +0530523 mv = 3300;
524 capa_mask = VS33_3V3SUP;
525 palmas_ldo_volt = LDO_VOLT_3V3;
Jean-Jacques Hiblot7a41bb42018-01-30 16:01:46 +0100526 } else if (mmc->signal_voltage == MMC_SIGNAL_VOLTAGE_180) {
527 capa_mask = VS18_1V8SUP;
528 palmas_ldo_volt = LDO_VOLT_1V8;
529 } else {
530 return -EOPNOTSUPP;
531 }
532
533 val = readl(&mmc_base->capa);
534 if (!(val & capa_mask))
535 return -EOPNOTSUPP;
536
537 priv->signal_voltage = mmc->signal_voltage;
538
539 omap_hsmmc_conf_bus_power(mmc, mmc->signal_voltage);
540
541#if CONFIG_IS_ENABLED(DM_REGULATOR)
542 return omap_hsmmc_set_io_regulator(mmc, mv);
Tom Rini8c6b4372024-07-15 13:35:53 -0600543#elif defined(CONFIG_OMAP54XX) && defined(CONFIG_PALMAS_POWER)
Jean-Jacques Hiblot7a41bb42018-01-30 16:01:46 +0100544 if (mmc_get_blk_desc(mmc)->devnum == 0)
545 vmmc_pbias_config(palmas_ldo_volt);
546 return 0;
547#else
548 return 0;
549#endif
550}
551#endif
552
553static uint32_t omap_hsmmc_set_capabilities(struct mmc *mmc)
Kishon Vijay Abraham I73897ed2018-01-30 16:01:32 +0100554{
555 struct hsmmc *mmc_base;
556 struct omap_hsmmc_data *priv = omap_hsmmc_get_data(mmc);
557 u32 val;
558
559 mmc_base = priv->base_addr;
560 val = readl(&mmc_base->capa);
561
562 if (priv->controller_flags & OMAP_HSMMC_SUPPORTS_DUAL_VOLT) {
Faiz Abbasfc1ad622019-04-05 14:18:46 +0530563 val |= (VS33_3V3SUP | VS18_1V8SUP);
Kishon Vijay Abraham I73897ed2018-01-30 16:01:32 +0100564 } else if (priv->controller_flags & OMAP_HSMMC_NO_1_8_V) {
Faiz Abbasfc1ad622019-04-05 14:18:46 +0530565 val |= VS33_3V3SUP;
Kishon Vijay Abraham I73897ed2018-01-30 16:01:32 +0100566 val &= ~VS18_1V8SUP;
Kishon Vijay Abraham I73897ed2018-01-30 16:01:32 +0100567 } else {
568 val |= VS18_1V8SUP;
Faiz Abbasfc1ad622019-04-05 14:18:46 +0530569 val &= ~VS33_3V3SUP;
Kishon Vijay Abraham I73897ed2018-01-30 16:01:32 +0100570 }
571
572 writel(val, &mmc_base->capa);
Jean-Jacques Hiblot7a41bb42018-01-30 16:01:46 +0100573
574 return val;
Kishon Vijay Abraham I73897ed2018-01-30 16:01:32 +0100575}
Jean-Jacques Hiblotf0f821b2018-01-30 16:01:35 +0100576
Tom Rinidec7ea02024-05-20 13:35:03 -0600577#if CONFIG_IS_ENABLED(MMC_SUPPORTS_TUNING)
Jean-Jacques Hiblotf0f821b2018-01-30 16:01:35 +0100578static void omap_hsmmc_disable_tuning(struct mmc *mmc)
579{
580 struct hsmmc *mmc_base;
581 struct omap_hsmmc_data *priv = omap_hsmmc_get_data(mmc);
582 u32 val;
583
584 mmc_base = priv->base_addr;
585 val = readl(&mmc_base->ac12);
586 val &= ~(AC12_SCLK_SEL);
587 writel(val, &mmc_base->ac12);
588
589 val = readl(&mmc_base->dll);
590 val &= ~(DLL_FORCE_VALUE | DLL_SWT);
591 writel(val, &mmc_base->dll);
592}
593
594static void omap_hsmmc_set_dll(struct mmc *mmc, int count)
595{
596 int i;
597 struct hsmmc *mmc_base;
598 struct omap_hsmmc_data *priv = omap_hsmmc_get_data(mmc);
599 u32 val;
600
601 mmc_base = priv->base_addr;
602 val = readl(&mmc_base->dll);
603 val |= DLL_FORCE_VALUE;
604 val &= ~(DLL_FORCE_SR_C_MASK << DLL_FORCE_SR_C_SHIFT);
605 val |= (count << DLL_FORCE_SR_C_SHIFT);
606 writel(val, &mmc_base->dll);
607
608 val |= DLL_CALIB;
609 writel(val, &mmc_base->dll);
610 for (i = 0; i < 1000; i++) {
611 if (readl(&mmc_base->dll) & DLL_CALIB)
612 break;
613 }
614 val &= ~DLL_CALIB;
615 writel(val, &mmc_base->dll);
616}
617
618static int omap_hsmmc_execute_tuning(struct udevice *dev, uint opcode)
619{
620 struct omap_hsmmc_data *priv = dev_get_priv(dev);
621 struct mmc_uclass_priv *upriv = dev_get_uclass_priv(dev);
622 struct mmc *mmc = upriv->mmc;
623 struct hsmmc *mmc_base;
624 u32 val;
625 u8 cur_match, prev_match = 0;
626 int ret;
627 u32 phase_delay = 0;
628 u32 start_window = 0, max_window = 0;
629 u32 length = 0, max_len = 0;
Faiz Abbase4d30562019-01-30 18:08:42 +0530630 bool single_point_failure = false;
631 struct udevice *thermal_dev;
632 int temperature;
633 int i;
Jean-Jacques Hiblotf0f821b2018-01-30 16:01:35 +0100634
635 mmc_base = priv->base_addr;
636 val = readl(&mmc_base->capa2);
637
638 /* clock tuning is not needed for upto 52MHz */
639 if (!((mmc->selected_mode == MMC_HS_200) ||
640 (mmc->selected_mode == UHS_SDR104) ||
641 ((mmc->selected_mode == UHS_SDR50) && (val & CAPA2_TSDR50))))
642 return 0;
643
Michal Suchanekac12a2f2022-10-12 21:57:59 +0200644 ret = uclass_first_device_err(UCLASS_THERMAL, &thermal_dev);
Faiz Abbase4d30562019-01-30 18:08:42 +0530645 if (ret) {
646 printf("Couldn't get thermal device for tuning\n");
647 return ret;
648 }
649 ret = thermal_get_temp(thermal_dev, &temperature);
650 if (ret) {
651 printf("Couldn't get temperature for tuning\n");
652 return ret;
653 }
Jean-Jacques Hiblotf0f821b2018-01-30 16:01:35 +0100654 val = readl(&mmc_base->dll);
655 val |= DLL_SWT;
656 writel(val, &mmc_base->dll);
Faiz Abbase4d30562019-01-30 18:08:42 +0530657
658 /*
659 * Stage 1: Search for a maximum pass window ignoring any
660 * any single point failures. If the tuning value ends up
661 * near it, move away from it in stage 2 below
662 */
Jean-Jacques Hiblotf0f821b2018-01-30 16:01:35 +0100663 while (phase_delay <= MAX_PHASE_DELAY) {
664 omap_hsmmc_set_dll(mmc, phase_delay);
665
Marek Vasutdad81fb2024-02-20 09:36:23 +0100666 cur_match = !mmc_send_tuning(mmc, opcode);
Jean-Jacques Hiblotf0f821b2018-01-30 16:01:35 +0100667
668 if (cur_match) {
669 if (prev_match) {
670 length++;
Faiz Abbase4d30562019-01-30 18:08:42 +0530671 } else if (single_point_failure) {
672 /* ignore single point failure */
673 length++;
674 single_point_failure = false;
Jean-Jacques Hiblotf0f821b2018-01-30 16:01:35 +0100675 } else {
676 start_window = phase_delay;
677 length = 1;
678 }
Faiz Abbase4d30562019-01-30 18:08:42 +0530679 } else {
680 single_point_failure = prev_match;
Jean-Jacques Hiblotf0f821b2018-01-30 16:01:35 +0100681 }
682
683 if (length > max_len) {
684 max_window = start_window;
685 max_len = length;
686 }
687
688 prev_match = cur_match;
689 phase_delay += 4;
690 }
691
692 if (!max_len) {
693 ret = -EIO;
694 goto tuning_error;
695 }
696
697 val = readl(&mmc_base->ac12);
698 if (!(val & AC12_SCLK_SEL)) {
699 ret = -EIO;
700 goto tuning_error;
701 }
Faiz Abbase4d30562019-01-30 18:08:42 +0530702 /*
703 * Assign tuning value as a ratio of maximum pass window based
704 * on temperature
705 */
706 if (temperature < -20000)
707 phase_delay = min(max_window + 4 * max_len - 24,
708 max_window +
709 DIV_ROUND_UP(13 * max_len, 16) * 4);
710 else if (temperature < 20000)
711 phase_delay = max_window + DIV_ROUND_UP(9 * max_len, 16) * 4;
712 else if (temperature < 40000)
713 phase_delay = max_window + DIV_ROUND_UP(8 * max_len, 16) * 4;
714 else if (temperature < 70000)
715 phase_delay = max_window + DIV_ROUND_UP(7 * max_len, 16) * 4;
716 else if (temperature < 90000)
717 phase_delay = max_window + DIV_ROUND_UP(5 * max_len, 16) * 4;
718 else if (temperature < 120000)
719 phase_delay = max_window + DIV_ROUND_UP(4 * max_len, 16) * 4;
720 else
721 phase_delay = max_window + DIV_ROUND_UP(3 * max_len, 16) * 4;
722
723 /*
724 * Stage 2: Search for a single point failure near the chosen tuning
725 * value in two steps. First in the +3 to +10 range and then in the
726 * +2 to -10 range. If found, move away from it in the appropriate
727 * direction by the appropriate amount depending on the temperature.
728 */
729 for (i = 3; i <= 10; i++) {
730 omap_hsmmc_set_dll(mmc, phase_delay + i);
Marek Vasutdad81fb2024-02-20 09:36:23 +0100731 if (mmc_send_tuning(mmc, opcode)) {
Faiz Abbase4d30562019-01-30 18:08:42 +0530732 if (temperature < 10000)
733 phase_delay += i + 6;
734 else if (temperature < 20000)
735 phase_delay += i - 12;
736 else if (temperature < 70000)
737 phase_delay += i - 8;
738 else if (temperature < 90000)
739 phase_delay += i - 6;
740 else
741 phase_delay += i - 6;
742
743 goto single_failure_found;
744 }
745 }
746
747 for (i = 2; i >= -10; i--) {
748 omap_hsmmc_set_dll(mmc, phase_delay + i);
Marek Vasutdad81fb2024-02-20 09:36:23 +0100749 if (mmc_send_tuning(mmc, opcode)) {
Faiz Abbase4d30562019-01-30 18:08:42 +0530750 if (temperature < 10000)
751 phase_delay += i + 12;
752 else if (temperature < 20000)
753 phase_delay += i + 8;
754 else if (temperature < 70000)
755 phase_delay += i + 8;
756 else if (temperature < 90000)
757 phase_delay += i + 10;
758 else
759 phase_delay += i + 12;
760
761 goto single_failure_found;
762 }
763 }
764
765single_failure_found:
Jean-Jacques Hiblotf0f821b2018-01-30 16:01:35 +0100766
Jean-Jacques Hiblotf0f821b2018-01-30 16:01:35 +0100767 omap_hsmmc_set_dll(mmc, phase_delay);
768
769 mmc_reset_controller_fsm(mmc_base, SYSCTL_SRD);
770 mmc_reset_controller_fsm(mmc_base, SYSCTL_SRC);
771
772 return 0;
773
774tuning_error:
775
776 omap_hsmmc_disable_tuning(mmc);
777 mmc_reset_controller_fsm(mmc_base, SYSCTL_SRD);
778 mmc_reset_controller_fsm(mmc_base, SYSCTL_SRC);
779
780 return ret;
781}
782#endif
Kishon Vijay Abraham I73897ed2018-01-30 16:01:32 +0100783#endif
Sukumar Ghoraic53f5e52010-09-18 20:32:33 -0700784
Jean-Jacques Hiblota420d7d2018-01-30 16:01:36 +0100785static void mmc_enable_irq(struct mmc *mmc, struct mmc_cmd *cmd)
786{
787 struct omap_hsmmc_data *priv = omap_hsmmc_get_data(mmc);
788 struct hsmmc *mmc_base = priv->base_addr;
789 u32 irq_mask = INT_EN_MASK;
790
791 /*
792 * TODO: Errata i802 indicates only DCRC interrupts can occur during
793 * tuning procedure and DCRC should be disabled. But see occurences
794 * of DEB, CIE, CEB, CCRC interupts during tuning procedure. These
795 * interrupts occur along with BRR, so the data is actually in the
796 * buffer. It has to be debugged why these interrutps occur
797 */
798 if (cmd && mmc_is_tuning_cmd(cmd->cmdidx))
799 irq_mask &= ~(IE_DEB | IE_DCRC | IE_CIE | IE_CEB | IE_CCRC);
800
801 writel(irq_mask, &mmc_base->ie);
802}
803
Pantelis Antoniouc9e75912014-02-26 19:28:45 +0200804static int omap_hsmmc_init_setup(struct mmc *mmc)
Sukumar Ghoraic53f5e52010-09-18 20:32:33 -0700805{
Jean-Jacques Hiblotd58ef8e2017-03-22 16:00:31 +0100806 struct omap_hsmmc_data *priv = omap_hsmmc_get_data(mmc);
Nikita Kiryanov13822862012-12-03 02:19:43 +0000807 struct hsmmc *mmc_base;
Sukumar Ghoraic53f5e52010-09-18 20:32:33 -0700808 unsigned int reg_val;
809 unsigned int dsor;
Nishanth Menond3bfaac2010-11-19 11:18:12 -0500810 ulong start;
Sukumar Ghoraic53f5e52010-09-18 20:32:33 -0700811
Jean-Jacques Hiblotd58ef8e2017-03-22 16:00:31 +0100812 mmc_base = priv->base_addr;
Balaji T Kf843d332011-09-08 06:34:57 +0000813 mmc_board_init(mmc);
Sukumar Ghoraic53f5e52010-09-18 20:32:33 -0700814
815 writel(readl(&mmc_base->sysconfig) | MMC_SOFTRESET,
816 &mmc_base->sysconfig);
Nishanth Menond3bfaac2010-11-19 11:18:12 -0500817 start = get_timer(0);
818 while ((readl(&mmc_base->sysstatus) & RESETDONE) == 0) {
819 if (get_timer(0) - start > MAX_RETRY_MS) {
820 printf("%s: timedout waiting for cc2!\n", __func__);
Jaehoon Chung7825d202016-07-19 16:33:36 +0900821 return -ETIMEDOUT;
Nishanth Menond3bfaac2010-11-19 11:18:12 -0500822 }
823 }
Sukumar Ghoraic53f5e52010-09-18 20:32:33 -0700824 writel(readl(&mmc_base->sysctl) | SOFTRESETALL, &mmc_base->sysctl);
Nishanth Menond3bfaac2010-11-19 11:18:12 -0500825 start = get_timer(0);
826 while ((readl(&mmc_base->sysctl) & SOFTRESETALL) != 0x0) {
827 if (get_timer(0) - start > MAX_RETRY_MS) {
828 printf("%s: timedout waiting for softresetall!\n",
829 __func__);
Jaehoon Chung7825d202016-07-19 16:33:36 +0900830 return -ETIMEDOUT;
Nishanth Menond3bfaac2010-11-19 11:18:12 -0500831 }
832 }
Jean-Jacques Hiblotcebf0592018-02-23 10:40:18 +0100833#ifdef CONFIG_MMC_OMAP_HS_ADMA
Kishon Vijay Abraham I826be2a2017-09-21 16:51:34 +0200834 reg_val = readl(&mmc_base->hl_hwinfo);
835 if (reg_val & MADMA_EN)
836 priv->controller_flags |= OMAP_HSMMC_USE_ADMA;
837#endif
Kishon Vijay Abraham I73897ed2018-01-30 16:01:32 +0100838
839#if CONFIG_IS_ENABLED(DM_MMC)
Jean-Jacques Hiblot7a41bb42018-01-30 16:01:46 +0100840 reg_val = omap_hsmmc_set_capabilities(mmc);
Faiz Abbasfc1ad622019-04-05 14:18:46 +0530841 omap_hsmmc_conf_bus_power(mmc, (reg_val & VS33_3V3SUP) ?
Jean-Jacques Hiblot7a41bb42018-01-30 16:01:46 +0100842 MMC_SIGNAL_VOLTAGE_330 : MMC_SIGNAL_VOLTAGE_180);
Kishon Vijay Abraham I73897ed2018-01-30 16:01:32 +0100843#else
Pali Rohárb2d1e562020-07-03 22:58:23 +0200844 writel(DTW_1_BITMODE | SDBP_PWROFF | SDVS_3V3, &mmc_base->hctl);
Faiz Abbasfc1ad622019-04-05 14:18:46 +0530845 writel(readl(&mmc_base->capa) | VS33_3V3SUP | VS18_1V8SUP,
Sukumar Ghoraic53f5e52010-09-18 20:32:33 -0700846 &mmc_base->capa);
Kishon Vijay Abraham I73897ed2018-01-30 16:01:32 +0100847#endif
Sukumar Ghoraic53f5e52010-09-18 20:32:33 -0700848
849 reg_val = readl(&mmc_base->con) & RESERVED_MASK;
850
851 writel(CTPL_MMC_SD | reg_val | WPP_ACTIVEHIGH | CDP_ACTIVEHIGH |
852 MIT_CTO | DW8_1_4BITMODE | MODE_FUNC | STR_BLOCK |
853 HR_NOHOSTRESP | INIT_NOINIT | NOOPENDRAIN, &mmc_base->con);
854
855 dsor = 240;
856 mmc_reg_out(&mmc_base->sysctl, (ICE_MASK | DTO_MASK | CEN_MASK),
Kishon Vijay Abraham I6e543812017-09-21 16:51:36 +0200857 (ICE_STOP | DTO_15THDTO));
Sukumar Ghoraic53f5e52010-09-18 20:32:33 -0700858 mmc_reg_out(&mmc_base->sysctl, ICE_MASK | CLKD_MASK,
859 (dsor << CLKD_OFFSET) | ICE_OSCILLATE);
Nishanth Menond3bfaac2010-11-19 11:18:12 -0500860 start = get_timer(0);
861 while ((readl(&mmc_base->sysctl) & ICS_MASK) == ICS_NOTREADY) {
862 if (get_timer(0) - start > MAX_RETRY_MS) {
863 printf("%s: timedout waiting for ics!\n", __func__);
Jaehoon Chung7825d202016-07-19 16:33:36 +0900864 return -ETIMEDOUT;
Nishanth Menond3bfaac2010-11-19 11:18:12 -0500865 }
866 }
Sukumar Ghoraic53f5e52010-09-18 20:32:33 -0700867 writel(readl(&mmc_base->sysctl) | CEN_ENABLE, &mmc_base->sysctl);
868
869 writel(readl(&mmc_base->hctl) | SDBP_PWRON, &mmc_base->hctl);
870
Jean-Jacques Hiblota420d7d2018-01-30 16:01:36 +0100871 mmc_enable_irq(mmc, NULL);
Jean-Jacques Hiblot20157d42018-01-30 16:01:44 +0100872
873#if !CONFIG_IS_ENABLED(DM_MMC)
Sukumar Ghoraic53f5e52010-09-18 20:32:33 -0700874 mmc_init_stream(mmc_base);
Jean-Jacques Hiblot20157d42018-01-30 16:01:44 +0100875#endif
Sukumar Ghoraic53f5e52010-09-18 20:32:33 -0700876
877 return 0;
878}
879
Grazvydas Ignotasddde1882012-03-19 12:12:06 +0000880/*
881 * MMC controller internal finite state machine reset
882 *
883 * Used to reset command or data internal state machines, using respectively
884 * SRC or SRD bit of SYSCTL register
885 */
886static void mmc_reset_controller_fsm(struct hsmmc *mmc_base, u32 bit)
887{
888 ulong start;
889
890 mmc_reg_out(&mmc_base->sysctl, bit, bit);
891
Oleksandr Tyshchenko06640ca2013-08-06 13:44:16 +0300892 /*
893 * CMD(DAT) lines reset procedures are slightly different
894 * for OMAP3 and OMAP4(AM335x,OMAP5,DRA7xx).
895 * According to OMAP3 TRM:
896 * Set SRC(SRD) bit in MMCHS_SYSCTL register to 0x1 and wait until it
897 * returns to 0x0.
898 * According to OMAP4(AM335x,OMAP5,DRA7xx) TRMs, CMD(DATA) lines reset
899 * procedure steps must be as follows:
900 * 1. Initiate CMD(DAT) line reset by writing 0x1 to SRC(SRD) bit in
901 * MMCHS_SYSCTL register (SD_SYSCTL for AM335x).
902 * 2. Poll the SRC(SRD) bit until it is set to 0x1.
903 * 3. Wait until the SRC (SRD) bit returns to 0x0
904 * (reset procedure is completed).
905 */
Tom Rini8c6b4372024-07-15 13:35:53 -0600906#if defined(CONFIG_OMAP54XX) || defined(CONFIG_AM33XX) || defined(CONFIG_AM43XX)
Oleksandr Tyshchenko06640ca2013-08-06 13:44:16 +0300907 if (!(readl(&mmc_base->sysctl) & bit)) {
908 start = get_timer(0);
909 while (!(readl(&mmc_base->sysctl) & bit)) {
Jean-Jacques Hiblot192e4302018-01-30 16:01:37 +0100910 if (get_timer(0) - start > MMC_TIMEOUT_MS)
Oleksandr Tyshchenko06640ca2013-08-06 13:44:16 +0300911 return;
912 }
913 }
914#endif
Grazvydas Ignotasddde1882012-03-19 12:12:06 +0000915 start = get_timer(0);
916 while ((readl(&mmc_base->sysctl) & bit) != 0) {
917 if (get_timer(0) - start > MAX_RETRY_MS) {
918 printf("%s: timedout waiting for sysctl %x to clear\n",
919 __func__, bit);
920 return;
921 }
922 }
923}
Kishon Vijay Abraham I826be2a2017-09-21 16:51:34 +0200924
Jean-Jacques Hiblotcebf0592018-02-23 10:40:18 +0100925#ifdef CONFIG_MMC_OMAP_HS_ADMA
Kishon Vijay Abraham I826be2a2017-09-21 16:51:34 +0200926static void omap_hsmmc_adma_desc(struct mmc *mmc, char *buf, u16 len, bool end)
927{
928 struct omap_hsmmc_data *priv = omap_hsmmc_get_data(mmc);
929 struct omap_hsmmc_adma_desc *desc;
930 u8 attr;
931
932 desc = &priv->adma_desc_table[priv->desc_slot];
933
934 attr = ADMA_DESC_ATTR_VALID | ADMA_DESC_TRANSFER_DATA;
935 if (!end)
936 priv->desc_slot++;
937 else
938 attr |= ADMA_DESC_ATTR_END;
939
940 desc->len = len;
941 desc->addr = (u32)buf;
942 desc->reserved = 0;
943 desc->attr = attr;
944}
945
946static void omap_hsmmc_prepare_adma_table(struct mmc *mmc,
947 struct mmc_data *data)
948{
949 uint total_len = data->blocksize * data->blocks;
950 uint desc_count = DIV_ROUND_UP(total_len, ADMA_MAX_LEN);
951 struct omap_hsmmc_data *priv = omap_hsmmc_get_data(mmc);
952 int i = desc_count;
953 char *buf;
954
955 priv->desc_slot = 0;
956 priv->adma_desc_table = (struct omap_hsmmc_adma_desc *)
957 memalign(ARCH_DMA_MINALIGN, desc_count *
958 sizeof(struct omap_hsmmc_adma_desc));
959
960 if (data->flags & MMC_DATA_READ)
961 buf = data->dest;
962 else
963 buf = (char *)data->src;
964
965 while (--i) {
966 omap_hsmmc_adma_desc(mmc, buf, ADMA_MAX_LEN, false);
967 buf += ADMA_MAX_LEN;
968 total_len -= ADMA_MAX_LEN;
969 }
970
971 omap_hsmmc_adma_desc(mmc, buf, total_len, true);
972
973 flush_dcache_range((long)priv->adma_desc_table,
974 (long)priv->adma_desc_table +
975 ROUND(desc_count *
976 sizeof(struct omap_hsmmc_adma_desc),
977 ARCH_DMA_MINALIGN));
978}
979
980static void omap_hsmmc_prepare_data(struct mmc *mmc, struct mmc_data *data)
981{
982 struct hsmmc *mmc_base;
983 struct omap_hsmmc_data *priv = omap_hsmmc_get_data(mmc);
984 u32 val;
985 char *buf;
986
987 mmc_base = priv->base_addr;
988 omap_hsmmc_prepare_adma_table(mmc, data);
989
990 if (data->flags & MMC_DATA_READ)
991 buf = data->dest;
992 else
993 buf = (char *)data->src;
994
995 val = readl(&mmc_base->hctl);
996 val |= DMA_SELECT;
997 writel(val, &mmc_base->hctl);
998
999 val = readl(&mmc_base->con);
1000 val |= DMA_MASTER;
1001 writel(val, &mmc_base->con);
1002
1003 writel((u32)priv->adma_desc_table, &mmc_base->admasal);
1004
1005 flush_dcache_range((u32)buf,
1006 (u32)buf +
1007 ROUND(data->blocksize * data->blocks,
1008 ARCH_DMA_MINALIGN));
1009}
1010
1011static void omap_hsmmc_dma_cleanup(struct mmc *mmc)
1012{
1013 struct hsmmc *mmc_base;
1014 struct omap_hsmmc_data *priv = omap_hsmmc_get_data(mmc);
1015 u32 val;
1016
1017 mmc_base = priv->base_addr;
1018
1019 val = readl(&mmc_base->con);
1020 val &= ~DMA_MASTER;
1021 writel(val, &mmc_base->con);
1022
1023 val = readl(&mmc_base->hctl);
1024 val &= ~DMA_SELECT;
1025 writel(val, &mmc_base->hctl);
1026
1027 kfree(priv->adma_desc_table);
1028}
1029#else
1030#define omap_hsmmc_adma_desc
1031#define omap_hsmmc_prepare_adma_table
1032#define omap_hsmmc_prepare_data
1033#define omap_hsmmc_dma_cleanup
1034#endif
1035
Simon Glass5f4bd8c2017-07-04 13:31:19 -06001036#if !CONFIG_IS_ENABLED(DM_MMC)
Pantelis Antoniouc9e75912014-02-26 19:28:45 +02001037static int omap_hsmmc_send_cmd(struct mmc *mmc, struct mmc_cmd *cmd,
Sukumar Ghoraic53f5e52010-09-18 20:32:33 -07001038 struct mmc_data *data)
1039{
Jean-Jacques Hiblotd58ef8e2017-03-22 16:00:31 +01001040 struct omap_hsmmc_data *priv = omap_hsmmc_get_data(mmc);
Jean-Jacques Hiblot8fc9d3a2017-04-14 19:50:02 +02001041#else
1042static int omap_hsmmc_send_cmd(struct udevice *dev, struct mmc_cmd *cmd,
1043 struct mmc_data *data)
1044{
1045 struct omap_hsmmc_data *priv = dev_get_priv(dev);
Kishon Vijay Abraham I826be2a2017-09-21 16:51:34 +02001046 struct mmc_uclass_priv *upriv = dev_get_uclass_priv(dev);
1047 struct mmc *mmc = upriv->mmc;
1048#endif
Nikita Kiryanov13822862012-12-03 02:19:43 +00001049 struct hsmmc *mmc_base;
Sukumar Ghoraic53f5e52010-09-18 20:32:33 -07001050 unsigned int flags, mmc_stat;
Nishanth Menond3bfaac2010-11-19 11:18:12 -05001051 ulong start;
Jean-Jacques Hiblot7a41bb42018-01-30 16:01:46 +01001052 priv->last_cmd = cmd->cmdidx;
Sukumar Ghoraic53f5e52010-09-18 20:32:33 -07001053
Jean-Jacques Hiblotd58ef8e2017-03-22 16:00:31 +01001054 mmc_base = priv->base_addr;
Kishon Vijay Abraham I316e7ae2017-09-21 16:51:35 +02001055
1056 if (cmd->cmdidx == MMC_CMD_STOP_TRANSMISSION)
1057 return 0;
1058
Nishanth Menond3bfaac2010-11-19 11:18:12 -05001059 start = get_timer(0);
Tom Rini32ec3252012-01-30 11:22:25 +00001060 while ((readl(&mmc_base->pstate) & (DATI_MASK | CMDI_MASK)) != 0) {
Nishanth Menond3bfaac2010-11-19 11:18:12 -05001061 if (get_timer(0) - start > MAX_RETRY_MS) {
Tom Rini32ec3252012-01-30 11:22:25 +00001062 printf("%s: timedout waiting on cmd inhibit to clear\n",
1063 __func__);
Jean-Jacques Hiblota1e7a4d2019-07-02 10:53:48 +02001064 mmc_reset_controller_fsm(mmc_base, SYSCTL_SRD);
1065 mmc_reset_controller_fsm(mmc_base, SYSCTL_SRC);
Jaehoon Chung7825d202016-07-19 16:33:36 +09001066 return -ETIMEDOUT;
Nishanth Menond3bfaac2010-11-19 11:18:12 -05001067 }
1068 }
Sukumar Ghoraic53f5e52010-09-18 20:32:33 -07001069 writel(0xFFFFFFFF, &mmc_base->stat);
Jean-Jacques Hiblota1e7a4d2019-07-02 10:53:48 +02001070 if (readl(&mmc_base->stat)) {
1071 mmc_reset_controller_fsm(mmc_base, SYSCTL_SRD);
1072 mmc_reset_controller_fsm(mmc_base, SYSCTL_SRC);
Nishanth Menond3bfaac2010-11-19 11:18:12 -05001073 }
Jean-Jacques Hiblota1e7a4d2019-07-02 10:53:48 +02001074
Sukumar Ghoraic53f5e52010-09-18 20:32:33 -07001075 /*
1076 * CMDREG
1077 * CMDIDX[13:8] : Command index
1078 * DATAPRNT[5] : Data Present Select
1079 * ENCMDIDX[4] : Command Index Check Enable
1080 * ENCMDCRC[3] : Command CRC Check Enable
1081 * RSPTYP[1:0]
1082 * 00 = No Response
1083 * 01 = Length 136
1084 * 10 = Length 48
1085 * 11 = Length 48 Check busy after response
1086 */
1087 /* Delay added before checking the status of frq change
1088 * retry not supported by mmc.c(core file)
1089 */
1090 if (cmd->cmdidx == SD_CMD_APP_SEND_SCR)
1091 udelay(50000); /* wait 50 ms */
1092
1093 if (!(cmd->resp_type & MMC_RSP_PRESENT))
1094 flags = 0;
1095 else if (cmd->resp_type & MMC_RSP_136)
1096 flags = RSP_TYPE_LGHT136 | CICE_NOCHECK;
1097 else if (cmd->resp_type & MMC_RSP_BUSY)
1098 flags = RSP_TYPE_LGHT48B;
1099 else
1100 flags = RSP_TYPE_LGHT48;
1101
1102 /* enable default flags */
1103 flags = flags | (CMD_TYPE_NORMAL | CICE_NOCHECK | CCCE_NOCHECK |
Kishon Vijay Abraham I6e543812017-09-21 16:51:36 +02001104 MSBS_SGLEBLK);
1105 flags &= ~(ACEN_ENABLE | BCE_ENABLE | DE_ENABLE);
Sukumar Ghoraic53f5e52010-09-18 20:32:33 -07001106
1107 if (cmd->resp_type & MMC_RSP_CRC)
1108 flags |= CCCE_CHECK;
1109 if (cmd->resp_type & MMC_RSP_OPCODE)
1110 flags |= CICE_CHECK;
1111
1112 if (data) {
1113 if ((cmd->cmdidx == MMC_CMD_READ_MULTIPLE_BLOCK) ||
1114 (cmd->cmdidx == MMC_CMD_WRITE_MULTIPLE_BLOCK)) {
Kishon Vijay Abraham I316e7ae2017-09-21 16:51:35 +02001115 flags |= (MSBS_MULTIBLK | BCE_ENABLE | ACEN_ENABLE);
Sukumar Ghoraic53f5e52010-09-18 20:32:33 -07001116 data->blocksize = 512;
1117 writel(data->blocksize | (data->blocks << 16),
1118 &mmc_base->blk);
1119 } else
1120 writel(data->blocksize | NBLK_STPCNT, &mmc_base->blk);
1121
1122 if (data->flags & MMC_DATA_READ)
1123 flags |= (DP_DATA | DDIR_READ);
1124 else
1125 flags |= (DP_DATA | DDIR_WRITE);
Kishon Vijay Abraham I826be2a2017-09-21 16:51:34 +02001126
Jean-Jacques Hiblotcebf0592018-02-23 10:40:18 +01001127#ifdef CONFIG_MMC_OMAP_HS_ADMA
Kishon Vijay Abraham I826be2a2017-09-21 16:51:34 +02001128 if ((priv->controller_flags & OMAP_HSMMC_USE_ADMA) &&
1129 !mmc_is_tuning_cmd(cmd->cmdidx)) {
1130 omap_hsmmc_prepare_data(mmc, data);
1131 flags |= DE_ENABLE;
1132 }
1133#endif
Sukumar Ghoraic53f5e52010-09-18 20:32:33 -07001134 }
1135
Jean-Jacques Hiblota420d7d2018-01-30 16:01:36 +01001136 mmc_enable_irq(mmc, cmd);
1137
Sukumar Ghoraic53f5e52010-09-18 20:32:33 -07001138 writel(cmd->cmdarg, &mmc_base->arg);
Lubomir Popov19df4122013-08-14 18:59:18 +03001139 udelay(20); /* To fix "No status update" error on eMMC */
Sukumar Ghoraic53f5e52010-09-18 20:32:33 -07001140 writel((cmd->cmdidx << 24) | flags, &mmc_base->cmd);
1141
Nishanth Menond3bfaac2010-11-19 11:18:12 -05001142 start = get_timer(0);
Sukumar Ghoraic53f5e52010-09-18 20:32:33 -07001143 do {
1144 mmc_stat = readl(&mmc_base->stat);
Kishon Vijay Abraham I826be2a2017-09-21 16:51:34 +02001145 if (get_timer(start) > MAX_RETRY_MS) {
Nishanth Menond3bfaac2010-11-19 11:18:12 -05001146 printf("%s : timeout: No status update\n", __func__);
Jaehoon Chung7825d202016-07-19 16:33:36 +09001147 return -ETIMEDOUT;
Nishanth Menond3bfaac2010-11-19 11:18:12 -05001148 }
1149 } while (!mmc_stat);
Sukumar Ghoraic53f5e52010-09-18 20:32:33 -07001150
Grazvydas Ignotasddde1882012-03-19 12:12:06 +00001151 if ((mmc_stat & IE_CTO) != 0) {
1152 mmc_reset_controller_fsm(mmc_base, SYSCTL_SRC);
Jaehoon Chung7825d202016-07-19 16:33:36 +09001153 return -ETIMEDOUT;
Grazvydas Ignotasddde1882012-03-19 12:12:06 +00001154 } else if ((mmc_stat & ERRI_MASK) != 0)
Sukumar Ghoraic53f5e52010-09-18 20:32:33 -07001155 return -1;
1156
1157 if (mmc_stat & CC_MASK) {
1158 writel(CC_MASK, &mmc_base->stat);
1159 if (cmd->resp_type & MMC_RSP_PRESENT) {
1160 if (cmd->resp_type & MMC_RSP_136) {
1161 /* response type 2 */
1162 cmd->response[3] = readl(&mmc_base->rsp10);
1163 cmd->response[2] = readl(&mmc_base->rsp32);
1164 cmd->response[1] = readl(&mmc_base->rsp54);
1165 cmd->response[0] = readl(&mmc_base->rsp76);
1166 } else
1167 /* response types 1, 1b, 3, 4, 5, 6 */
1168 cmd->response[0] = readl(&mmc_base->rsp10);
1169 }
1170 }
1171
Jean-Jacques Hiblotcebf0592018-02-23 10:40:18 +01001172#ifdef CONFIG_MMC_OMAP_HS_ADMA
Kishon Vijay Abraham I826be2a2017-09-21 16:51:34 +02001173 if ((priv->controller_flags & OMAP_HSMMC_USE_ADMA) && data &&
1174 !mmc_is_tuning_cmd(cmd->cmdidx)) {
1175 u32 sz_mb, timeout;
1176
1177 if (mmc_stat & IE_ADMAE) {
1178 omap_hsmmc_dma_cleanup(mmc);
1179 return -EIO;
1180 }
1181
1182 sz_mb = DIV_ROUND_UP(data->blocksize * data->blocks, 1 << 20);
1183 timeout = sz_mb * DMA_TIMEOUT_PER_MB;
1184 if (timeout < MAX_RETRY_MS)
1185 timeout = MAX_RETRY_MS;
1186
1187 start = get_timer(0);
1188 do {
1189 mmc_stat = readl(&mmc_base->stat);
1190 if (mmc_stat & TC_MASK) {
1191 writel(readl(&mmc_base->stat) | TC_MASK,
1192 &mmc_base->stat);
1193 break;
1194 }
1195 if (get_timer(start) > timeout) {
1196 printf("%s : DMA timeout: No status update\n",
1197 __func__);
1198 return -ETIMEDOUT;
1199 }
1200 } while (1);
1201
1202 omap_hsmmc_dma_cleanup(mmc);
1203 return 0;
1204 }
1205#endif
1206
Sukumar Ghoraic53f5e52010-09-18 20:32:33 -07001207 if (data && (data->flags & MMC_DATA_READ)) {
1208 mmc_read_data(mmc_base, data->dest,
1209 data->blocksize * data->blocks);
1210 } else if (data && (data->flags & MMC_DATA_WRITE)) {
1211 mmc_write_data(mmc_base, data->src,
1212 data->blocksize * data->blocks);
1213 }
1214 return 0;
1215}
1216
Sricharanf72611f2011-11-15 09:49:53 -05001217static int mmc_read_data(struct hsmmc *mmc_base, char *buf, unsigned int size)
Sukumar Ghoraic53f5e52010-09-18 20:32:33 -07001218{
1219 unsigned int *output_buf = (unsigned int *)buf;
1220 unsigned int mmc_stat;
1221 unsigned int count;
1222
1223 /*
1224 * Start Polled Read
1225 */
1226 count = (size > MMCSD_SECTOR_SIZE) ? MMCSD_SECTOR_SIZE : size;
1227 count /= 4;
1228
1229 while (size) {
Nishanth Menond3bfaac2010-11-19 11:18:12 -05001230 ulong start = get_timer(0);
Sukumar Ghoraic53f5e52010-09-18 20:32:33 -07001231 do {
1232 mmc_stat = readl(&mmc_base->stat);
Nishanth Menond3bfaac2010-11-19 11:18:12 -05001233 if (get_timer(0) - start > MAX_RETRY_MS) {
1234 printf("%s: timedout waiting for status!\n",
1235 __func__);
Jaehoon Chung7825d202016-07-19 16:33:36 +09001236 return -ETIMEDOUT;
Nishanth Menond3bfaac2010-11-19 11:18:12 -05001237 }
Sukumar Ghoraic53f5e52010-09-18 20:32:33 -07001238 } while (mmc_stat == 0);
1239
Grazvydas Ignotasddde1882012-03-19 12:12:06 +00001240 if ((mmc_stat & (IE_DTO | IE_DCRC | IE_DEB)) != 0)
1241 mmc_reset_controller_fsm(mmc_base, SYSCTL_SRD);
1242
Sukumar Ghoraic53f5e52010-09-18 20:32:33 -07001243 if ((mmc_stat & ERRI_MASK) != 0)
1244 return 1;
1245
1246 if (mmc_stat & BRR_MASK) {
1247 unsigned int k;
1248
1249 writel(readl(&mmc_base->stat) | BRR_MASK,
1250 &mmc_base->stat);
1251 for (k = 0; k < count; k++) {
1252 *output_buf = readl(&mmc_base->data);
1253 output_buf++;
1254 }
1255 size -= (count*4);
1256 }
1257
1258 if (mmc_stat & BWR_MASK)
1259 writel(readl(&mmc_base->stat) | BWR_MASK,
1260 &mmc_base->stat);
1261
1262 if (mmc_stat & TC_MASK) {
1263 writel(readl(&mmc_base->stat) | TC_MASK,
1264 &mmc_base->stat);
1265 break;
1266 }
1267 }
1268 return 0;
1269}
1270
Jean-Jacques Hiblot98821552018-02-23 10:40:17 +01001271#if CONFIG_IS_ENABLED(MMC_WRITE)
Sricharanf72611f2011-11-15 09:49:53 -05001272static int mmc_write_data(struct hsmmc *mmc_base, const char *buf,
Jean-Jacques Hiblot98821552018-02-23 10:40:17 +01001273 unsigned int size)
Sukumar Ghoraic53f5e52010-09-18 20:32:33 -07001274{
1275 unsigned int *input_buf = (unsigned int *)buf;
1276 unsigned int mmc_stat;
1277 unsigned int count;
1278
1279 /*
Lubomir Popov19df4122013-08-14 18:59:18 +03001280 * Start Polled Write
Sukumar Ghoraic53f5e52010-09-18 20:32:33 -07001281 */
1282 count = (size > MMCSD_SECTOR_SIZE) ? MMCSD_SECTOR_SIZE : size;
1283 count /= 4;
1284
1285 while (size) {
Nishanth Menond3bfaac2010-11-19 11:18:12 -05001286 ulong start = get_timer(0);
Sukumar Ghoraic53f5e52010-09-18 20:32:33 -07001287 do {
1288 mmc_stat = readl(&mmc_base->stat);
Nishanth Menond3bfaac2010-11-19 11:18:12 -05001289 if (get_timer(0) - start > MAX_RETRY_MS) {
1290 printf("%s: timedout waiting for status!\n",
1291 __func__);
Jaehoon Chung7825d202016-07-19 16:33:36 +09001292 return -ETIMEDOUT;
Nishanth Menond3bfaac2010-11-19 11:18:12 -05001293 }
Sukumar Ghoraic53f5e52010-09-18 20:32:33 -07001294 } while (mmc_stat == 0);
1295
Grazvydas Ignotasddde1882012-03-19 12:12:06 +00001296 if ((mmc_stat & (IE_DTO | IE_DCRC | IE_DEB)) != 0)
1297 mmc_reset_controller_fsm(mmc_base, SYSCTL_SRD);
1298
Sukumar Ghoraic53f5e52010-09-18 20:32:33 -07001299 if ((mmc_stat & ERRI_MASK) != 0)
1300 return 1;
1301
1302 if (mmc_stat & BWR_MASK) {
1303 unsigned int k;
1304
1305 writel(readl(&mmc_base->stat) | BWR_MASK,
1306 &mmc_base->stat);
1307 for (k = 0; k < count; k++) {
1308 writel(*input_buf, &mmc_base->data);
1309 input_buf++;
1310 }
1311 size -= (count*4);
1312 }
1313
1314 if (mmc_stat & BRR_MASK)
1315 writel(readl(&mmc_base->stat) | BRR_MASK,
1316 &mmc_base->stat);
1317
1318 if (mmc_stat & TC_MASK) {
1319 writel(readl(&mmc_base->stat) | TC_MASK,
1320 &mmc_base->stat);
1321 break;
1322 }
1323 }
1324 return 0;
1325}
Jean-Jacques Hiblot98821552018-02-23 10:40:17 +01001326#else
1327static int mmc_write_data(struct hsmmc *mmc_base, const char *buf,
1328 unsigned int size)
1329{
1330 return -ENOTSUPP;
1331}
1332#endif
Jean-Jacques Hiblot7fe2f192018-01-30 16:01:30 +01001333static void omap_hsmmc_stop_clock(struct hsmmc *mmc_base)
1334{
1335 writel(readl(&mmc_base->sysctl) & ~CEN_ENABLE, &mmc_base->sysctl);
1336}
1337
1338static void omap_hsmmc_start_clock(struct hsmmc *mmc_base)
1339{
1340 writel(readl(&mmc_base->sysctl) | CEN_ENABLE, &mmc_base->sysctl);
1341}
1342
1343static void omap_hsmmc_set_clock(struct mmc *mmc)
1344{
1345 struct omap_hsmmc_data *priv = omap_hsmmc_get_data(mmc);
1346 struct hsmmc *mmc_base;
1347 unsigned int dsor = 0;
1348 ulong start;
1349
1350 mmc_base = priv->base_addr;
1351 omap_hsmmc_stop_clock(mmc_base);
1352
1353 /* TODO: Is setting DTO required here? */
1354 mmc_reg_out(&mmc_base->sysctl, (ICE_MASK | DTO_MASK),
1355 (ICE_STOP | DTO_15THDTO));
1356
1357 if (mmc->clock != 0) {
1358 dsor = DIV_ROUND_UP(MMC_CLOCK_REFERENCE * 1000000, mmc->clock);
1359 if (dsor > CLKD_MAX)
1360 dsor = CLKD_MAX;
1361 } else {
1362 dsor = CLKD_MAX;
1363 }
1364
1365 mmc_reg_out(&mmc_base->sysctl, ICE_MASK | CLKD_MASK,
1366 (dsor << CLKD_OFFSET) | ICE_OSCILLATE);
1367
1368 start = get_timer(0);
1369 while ((readl(&mmc_base->sysctl) & ICS_MASK) == ICS_NOTREADY) {
1370 if (get_timer(0) - start > MAX_RETRY_MS) {
1371 printf("%s: timedout waiting for ics!\n", __func__);
1372 return;
1373 }
1374 }
1375
Jean-Jacques Hiblot6ce31e42018-01-30 16:01:43 +01001376 priv->clock = MMC_CLOCK_REFERENCE * 1000000 / dsor;
1377 mmc->clock = priv->clock;
Jean-Jacques Hiblot7fe2f192018-01-30 16:01:30 +01001378 omap_hsmmc_start_clock(mmc_base);
1379}
1380
Kishon Vijay Abraham I2e18c9b2018-01-30 16:01:31 +01001381static void omap_hsmmc_set_bus_width(struct mmc *mmc)
Sukumar Ghoraic53f5e52010-09-18 20:32:33 -07001382{
Jean-Jacques Hiblotd58ef8e2017-03-22 16:00:31 +01001383 struct omap_hsmmc_data *priv = omap_hsmmc_get_data(mmc);
Nikita Kiryanov13822862012-12-03 02:19:43 +00001384 struct hsmmc *mmc_base;
Sukumar Ghoraic53f5e52010-09-18 20:32:33 -07001385
Jean-Jacques Hiblotd58ef8e2017-03-22 16:00:31 +01001386 mmc_base = priv->base_addr;
Sukumar Ghoraic53f5e52010-09-18 20:32:33 -07001387 /* configue bus width */
1388 switch (mmc->bus_width) {
1389 case 8:
1390 writel(readl(&mmc_base->con) | DTW_8_BITMODE,
1391 &mmc_base->con);
1392 break;
1393
1394 case 4:
1395 writel(readl(&mmc_base->con) & ~DTW_8_BITMODE,
1396 &mmc_base->con);
1397 writel(readl(&mmc_base->hctl) | DTW_4_BITMODE,
1398 &mmc_base->hctl);
1399 break;
1400
1401 case 1:
1402 default:
1403 writel(readl(&mmc_base->con) & ~DTW_8_BITMODE,
1404 &mmc_base->con);
1405 writel(readl(&mmc_base->hctl) & ~DTW_4_BITMODE,
1406 &mmc_base->hctl);
1407 break;
1408 }
1409
Kishon Vijay Abraham I2e18c9b2018-01-30 16:01:31 +01001410 priv->bus_width = mmc->bus_width;
1411}
1412
1413#if !CONFIG_IS_ENABLED(DM_MMC)
1414static int omap_hsmmc_set_ios(struct mmc *mmc)
1415{
1416 struct omap_hsmmc_data *priv = omap_hsmmc_get_data(mmc);
1417#else
1418static int omap_hsmmc_set_ios(struct udevice *dev)
1419{
1420 struct omap_hsmmc_data *priv = dev_get_priv(dev);
1421 struct mmc_uclass_priv *upriv = dev_get_uclass_priv(dev);
1422 struct mmc *mmc = upriv->mmc;
1423#endif
Kishon Vijay Abraham Ie1f25c02018-01-30 16:01:45 +01001424 struct hsmmc *mmc_base = priv->base_addr;
Jean-Jacques Hiblot7a41bb42018-01-30 16:01:46 +01001425 int ret = 0;
Kishon Vijay Abraham I2e18c9b2018-01-30 16:01:31 +01001426
1427 if (priv->bus_width != mmc->bus_width)
1428 omap_hsmmc_set_bus_width(mmc);
1429
Jean-Jacques Hiblot7fe2f192018-01-30 16:01:30 +01001430 if (priv->clock != mmc->clock)
1431 omap_hsmmc_set_clock(mmc);
Jaehoon Chungb6cd1d32016-12-30 15:30:16 +09001432
Kishon Vijay Abraham Ie1f25c02018-01-30 16:01:45 +01001433 if (mmc->clk_disable)
1434 omap_hsmmc_stop_clock(mmc_base);
1435 else
1436 omap_hsmmc_start_clock(mmc_base);
1437
Jean-Jacques Hiblotcf38d4e2018-01-30 16:01:33 +01001438#if CONFIG_IS_ENABLED(DM_MMC)
1439 if (priv->mode != mmc->selected_mode)
1440 omap_hsmmc_set_timing(mmc);
Jean-Jacques Hiblot7a41bb42018-01-30 16:01:46 +01001441
1442#if CONFIG_IS_ENABLED(MMC_IO_VOLTAGE)
1443 if (priv->signal_voltage != mmc->signal_voltage)
1444 ret = omap_hsmmc_set_signal_voltage(mmc);
Jean-Jacques Hiblotcf38d4e2018-01-30 16:01:33 +01001445#endif
Jean-Jacques Hiblot7a41bb42018-01-30 16:01:46 +01001446#endif
1447 return ret;
Sukumar Ghoraic53f5e52010-09-18 20:32:33 -07001448}
1449
Pantelis Antoniouc9e75912014-02-26 19:28:45 +02001450#ifdef OMAP_HSMMC_USE_GPIO
Simon Glass5f4bd8c2017-07-04 13:31:19 -06001451#if CONFIG_IS_ENABLED(DM_MMC)
Jean-Jacques Hiblot8fc9d3a2017-04-14 19:50:02 +02001452static int omap_hsmmc_getcd(struct udevice *dev)
Pantelis Antoniouc9e75912014-02-26 19:28:45 +02001453{
Adam Ford6122af42018-08-21 07:16:56 -05001454 int value = -1;
1455#if CONFIG_IS_ENABLED(DM_GPIO)
Adam Fordac740ff2018-09-08 08:16:23 -05001456 struct omap_hsmmc_data *priv = dev_get_priv(dev);
Mugunthan V Nd97631a2015-09-28 12:56:30 +05301457 value = dm_gpio_get_value(&priv->cd_gpio);
Adam Ford6122af42018-08-21 07:16:56 -05001458#endif
Mugunthan V Nd97631a2015-09-28 12:56:30 +05301459 /* if no CD return as 1 */
1460 if (value < 0)
1461 return 1;
1462
Mugunthan V Nd97631a2015-09-28 12:56:30 +05301463 return value;
1464}
1465
Jean-Jacques Hiblot8fc9d3a2017-04-14 19:50:02 +02001466static int omap_hsmmc_getwp(struct udevice *dev)
Mugunthan V Nd97631a2015-09-28 12:56:30 +05301467{
Adam Ford6122af42018-08-21 07:16:56 -05001468 int value = 0;
1469#if CONFIG_IS_ENABLED(DM_GPIO)
Jean-Jacques Hiblot8fc9d3a2017-04-14 19:50:02 +02001470 struct omap_hsmmc_data *priv = dev_get_priv(dev);
Mugunthan V Nd97631a2015-09-28 12:56:30 +05301471 value = dm_gpio_get_value(&priv->wp_gpio);
Adam Ford6122af42018-08-21 07:16:56 -05001472#endif
Mugunthan V Nd97631a2015-09-28 12:56:30 +05301473 /* if no WP return as 0 */
1474 if (value < 0)
1475 return 0;
1476 return value;
1477}
1478#else
1479static int omap_hsmmc_getcd(struct mmc *mmc)
1480{
Jean-Jacques Hiblotd58ef8e2017-03-22 16:00:31 +01001481 struct omap_hsmmc_data *priv = omap_hsmmc_get_data(mmc);
Pantelis Antoniouc9e75912014-02-26 19:28:45 +02001482 int cd_gpio;
1483
1484 /* if no CD return as 1 */
Jean-Jacques Hiblotd58ef8e2017-03-22 16:00:31 +01001485 cd_gpio = priv->cd_gpio;
Pantelis Antoniouc9e75912014-02-26 19:28:45 +02001486 if (cd_gpio < 0)
1487 return 1;
1488
Igor Grinberg2f4e0952014-11-03 11:32:23 +02001489 /* NOTE: assumes card detect signal is active-low */
1490 return !gpio_get_value(cd_gpio);
Pantelis Antoniouc9e75912014-02-26 19:28:45 +02001491}
1492
1493static int omap_hsmmc_getwp(struct mmc *mmc)
1494{
Jean-Jacques Hiblotd58ef8e2017-03-22 16:00:31 +01001495 struct omap_hsmmc_data *priv = omap_hsmmc_get_data(mmc);
Pantelis Antoniouc9e75912014-02-26 19:28:45 +02001496 int wp_gpio;
1497
1498 /* if no WP return as 0 */
Jean-Jacques Hiblotd58ef8e2017-03-22 16:00:31 +01001499 wp_gpio = priv->wp_gpio;
Pantelis Antoniouc9e75912014-02-26 19:28:45 +02001500 if (wp_gpio < 0)
1501 return 0;
1502
Igor Grinberg2f4e0952014-11-03 11:32:23 +02001503 /* NOTE: assumes write protect signal is active-high */
Pantelis Antoniouc9e75912014-02-26 19:28:45 +02001504 return gpio_get_value(wp_gpio);
1505}
1506#endif
Mugunthan V Nd97631a2015-09-28 12:56:30 +05301507#endif
Pantelis Antoniouc9e75912014-02-26 19:28:45 +02001508
Simon Glass5f4bd8c2017-07-04 13:31:19 -06001509#if CONFIG_IS_ENABLED(DM_MMC)
Jean-Jacques Hiblot8fc9d3a2017-04-14 19:50:02 +02001510static const struct dm_mmc_ops omap_hsmmc_ops = {
1511 .send_cmd = omap_hsmmc_send_cmd,
1512 .set_ios = omap_hsmmc_set_ios,
1513#ifdef OMAP_HSMMC_USE_GPIO
1514 .get_cd = omap_hsmmc_getcd,
1515 .get_wp = omap_hsmmc_getwp,
1516#endif
Tom Rinidec7ea02024-05-20 13:35:03 -06001517#if CONFIG_IS_ENABLED(MMC_SUPPORTS_TUNING)
Jean-Jacques Hiblotf0f821b2018-01-30 16:01:35 +01001518 .execute_tuning = omap_hsmmc_execute_tuning,
1519#endif
Jean-Jacques Hiblot7a41bb42018-01-30 16:01:46 +01001520 .wait_dat0 = omap_hsmmc_wait_dat0,
Jean-Jacques Hiblot8fc9d3a2017-04-14 19:50:02 +02001521};
1522#else
Pantelis Antoniouc9e75912014-02-26 19:28:45 +02001523static const struct mmc_ops omap_hsmmc_ops = {
1524 .send_cmd = omap_hsmmc_send_cmd,
1525 .set_ios = omap_hsmmc_set_ios,
1526 .init = omap_hsmmc_init_setup,
1527#ifdef OMAP_HSMMC_USE_GPIO
1528 .getcd = omap_hsmmc_getcd,
1529 .getwp = omap_hsmmc_getwp,
1530#endif
1531};
Jean-Jacques Hiblot8fc9d3a2017-04-14 19:50:02 +02001532#endif
Pantelis Antoniouc9e75912014-02-26 19:28:45 +02001533
Simon Glass5f4bd8c2017-07-04 13:31:19 -06001534#if !CONFIG_IS_ENABLED(DM_MMC)
Nikita Kiryanov4be9dbc2012-12-03 02:19:47 +00001535int omap_mmc_init(int dev_index, uint host_caps_mask, uint f_max, int cd_gpio,
1536 int wp_gpio)
Sukumar Ghoraic53f5e52010-09-18 20:32:33 -07001537{
Pantelis Antoniou2c850462014-03-11 19:34:20 +02001538 struct mmc *mmc;
Jean-Jacques Hiblotd58ef8e2017-03-22 16:00:31 +01001539 struct omap_hsmmc_data *priv;
Pantelis Antoniou2c850462014-03-11 19:34:20 +02001540 struct mmc_config *cfg;
1541 uint host_caps_val;
1542
Alex Kiernan4b9cb772018-02-09 15:24:38 +00001543 priv = calloc(1, sizeof(*priv));
Jean-Jacques Hiblotd58ef8e2017-03-22 16:00:31 +01001544 if (priv == NULL)
Pantelis Antoniou2c850462014-03-11 19:34:20 +02001545 return -1;
Sukumar Ghoraic53f5e52010-09-18 20:32:33 -07001546
Rob Herring5fd3edd2015-03-23 17:56:59 -05001547 host_caps_val = MMC_MODE_4BIT | MMC_MODE_HS_52MHz | MMC_MODE_HS;
Sukumar Ghoraic53f5e52010-09-18 20:32:33 -07001548
1549 switch (dev_index) {
1550 case 0:
Jean-Jacques Hiblotd58ef8e2017-03-22 16:00:31 +01001551 priv->base_addr = (struct hsmmc *)OMAP_HSMMC1_BASE;
Sukumar Ghoraic53f5e52010-09-18 20:32:33 -07001552 break;
Tom Rinifd6e2942011-10-12 06:20:50 +00001553#ifdef OMAP_HSMMC2_BASE
Sukumar Ghoraic53f5e52010-09-18 20:32:33 -07001554 case 1:
Jean-Jacques Hiblotd58ef8e2017-03-22 16:00:31 +01001555 priv->base_addr = (struct hsmmc *)OMAP_HSMMC2_BASE;
Tom Rini8c6b4372024-07-15 13:35:53 -06001556#if (defined(CONFIG_OMAP54XX) || \
Nishanth Menon813fe9d2016-11-29 15:22:00 +05301557 defined(CONFIG_DRA7XX) || defined(CONFIG_AM33XX) || \
Tom Rini84c0f692021-09-12 20:32:32 -04001558 defined(CONFIG_AM43XX) || defined(CONFIG_ARCH_KEYSTONE)) && \
Roger Quadros44157de2015-09-19 16:26:53 +05301559 defined(CONFIG_HSMMC2_8BIT)
Lubomir Popov19df4122013-08-14 18:59:18 +03001560 /* Enable 8-bit interface for eMMC on OMAP4/5 or DRA7XX */
1561 host_caps_val |= MMC_MODE_8BIT;
1562#endif
Sukumar Ghoraic53f5e52010-09-18 20:32:33 -07001563 break;
Tom Rinifd6e2942011-10-12 06:20:50 +00001564#endif
1565#ifdef OMAP_HSMMC3_BASE
Sukumar Ghoraic53f5e52010-09-18 20:32:33 -07001566 case 2:
Jean-Jacques Hiblotd58ef8e2017-03-22 16:00:31 +01001567 priv->base_addr = (struct hsmmc *)OMAP_HSMMC3_BASE;
Nishanth Menon813fe9d2016-11-29 15:22:00 +05301568#if defined(CONFIG_DRA7XX) && defined(CONFIG_HSMMC3_8BIT)
Lubomir Popov19df4122013-08-14 18:59:18 +03001569 /* Enable 8-bit interface for eMMC on DRA7XX */
1570 host_caps_val |= MMC_MODE_8BIT;
1571#endif
Sukumar Ghoraic53f5e52010-09-18 20:32:33 -07001572 break;
Tom Rinifd6e2942011-10-12 06:20:50 +00001573#endif
Sukumar Ghoraic53f5e52010-09-18 20:32:33 -07001574 default:
Jean-Jacques Hiblotd58ef8e2017-03-22 16:00:31 +01001575 priv->base_addr = (struct hsmmc *)OMAP_HSMMC1_BASE;
Sukumar Ghoraic53f5e52010-09-18 20:32:33 -07001576 return 1;
1577 }
Pantelis Antoniouc9e75912014-02-26 19:28:45 +02001578#ifdef OMAP_HSMMC_USE_GPIO
1579 /* on error gpio values are set to -1, which is what we want */
Jean-Jacques Hiblotd58ef8e2017-03-22 16:00:31 +01001580 priv->cd_gpio = omap_mmc_setup_gpio_in(cd_gpio, "mmc_cd");
1581 priv->wp_gpio = omap_mmc_setup_gpio_in(wp_gpio, "mmc_wp");
Pantelis Antoniouc9e75912014-02-26 19:28:45 +02001582#endif
Peter Korsgaard47c6b2a2013-03-21 04:00:04 +00001583
Jean-Jacques Hiblotd58ef8e2017-03-22 16:00:31 +01001584 cfg = &priv->cfg;
Sukumar Ghoraic53f5e52010-09-18 20:32:33 -07001585
Pantelis Antoniou2c850462014-03-11 19:34:20 +02001586 cfg->name = "OMAP SD/MMC";
1587 cfg->ops = &omap_hsmmc_ops;
1588
1589 cfg->voltages = MMC_VDD_32_33 | MMC_VDD_33_34 | MMC_VDD_165_195;
1590 cfg->host_caps = host_caps_val & ~host_caps_mask;
1591
1592 cfg->f_min = 400000;
Jonathan Solnita9b05562012-02-24 11:30:18 +00001593
1594 if (f_max != 0)
Pantelis Antoniou2c850462014-03-11 19:34:20 +02001595 cfg->f_max = f_max;
Jonathan Solnita9b05562012-02-24 11:30:18 +00001596 else {
Pantelis Antoniou2c850462014-03-11 19:34:20 +02001597 if (cfg->host_caps & MMC_MODE_HS) {
1598 if (cfg->host_caps & MMC_MODE_HS_52MHz)
1599 cfg->f_max = 52000000;
Jonathan Solnita9b05562012-02-24 11:30:18 +00001600 else
Pantelis Antoniou2c850462014-03-11 19:34:20 +02001601 cfg->f_max = 26000000;
Jonathan Solnita9b05562012-02-24 11:30:18 +00001602 } else
Pantelis Antoniou2c850462014-03-11 19:34:20 +02001603 cfg->f_max = 20000000;
Jonathan Solnita9b05562012-02-24 11:30:18 +00001604 }
Sukumar Ghoraic53f5e52010-09-18 20:32:33 -07001605
Pantelis Antoniou2c850462014-03-11 19:34:20 +02001606 cfg->b_max = CONFIG_SYS_MMC_MAX_BLK_COUNT;
John Rigbyf2f43662011-04-18 05:50:08 +00001607
John Rigby91fcc4b2011-04-19 05:48:14 +00001608#if defined(CONFIG_OMAP34XX)
1609 /*
1610 * Silicon revs 2.1 and older do not support multiblock transfers.
1611 */
1612 if ((get_cpu_family() == CPU_OMAP34XX) && (get_cpu_rev() <= CPU_3XX_ES21))
Pantelis Antoniou2c850462014-03-11 19:34:20 +02001613 cfg->b_max = 1;
John Rigby91fcc4b2011-04-19 05:48:14 +00001614#endif
Kishon Vijay Abraham I8c2efe92018-01-30 16:01:41 +01001615
Jean-Jacques Hiblotd58ef8e2017-03-22 16:00:31 +01001616 mmc = mmc_create(cfg, priv);
Pantelis Antoniou2c850462014-03-11 19:34:20 +02001617 if (mmc == NULL)
1618 return -1;
Sukumar Ghoraic53f5e52010-09-18 20:32:33 -07001619
1620 return 0;
1621}
Mugunthan V Nd97631a2015-09-28 12:56:30 +05301622#else
Kishon Vijay Abraham Ie7da6ac2018-01-30 16:01:40 +01001623
1624#ifdef CONFIG_IODELAY_RECALIBRATION
1625static struct pad_conf_entry *
1626omap_hsmmc_get_pad_conf_entry(const fdt32_t *pinctrl, int count)
1627{
1628 int index = 0;
1629 struct pad_conf_entry *padconf;
1630
1631 padconf = (struct pad_conf_entry *)malloc(sizeof(*padconf) * count);
1632 if (!padconf) {
1633 debug("failed to allocate memory\n");
1634 return 0;
1635 }
1636
1637 while (index < count) {
1638 padconf[index].offset = fdt32_to_cpu(pinctrl[2 * index]);
1639 padconf[index].val = fdt32_to_cpu(pinctrl[2 * index + 1]);
1640 index++;
1641 }
1642
1643 return padconf;
1644}
1645
1646static struct iodelay_cfg_entry *
1647omap_hsmmc_get_iodelay_cfg_entry(const fdt32_t *pinctrl, int count)
1648{
1649 int index = 0;
1650 struct iodelay_cfg_entry *iodelay;
1651
1652 iodelay = (struct iodelay_cfg_entry *)malloc(sizeof(*iodelay) * count);
1653 if (!iodelay) {
1654 debug("failed to allocate memory\n");
1655 return 0;
1656 }
1657
1658 while (index < count) {
1659 iodelay[index].offset = fdt32_to_cpu(pinctrl[3 * index]);
1660 iodelay[index].a_delay = fdt32_to_cpu(pinctrl[3 * index + 1]);
1661 iodelay[index].g_delay = fdt32_to_cpu(pinctrl[3 * index + 2]);
1662 index++;
1663 }
1664
1665 return iodelay;
1666}
1667
1668static const fdt32_t *omap_hsmmc_get_pinctrl_entry(u32 phandle,
1669 const char *name, int *len)
1670{
1671 const void *fdt = gd->fdt_blob;
1672 int offset;
1673 const fdt32_t *pinctrl;
1674
1675 offset = fdt_node_offset_by_phandle(fdt, phandle);
1676 if (offset < 0) {
1677 debug("failed to get pinctrl node %s.\n",
1678 fdt_strerror(offset));
1679 return 0;
1680 }
1681
1682 pinctrl = fdt_getprop(fdt, offset, name, len);
1683 if (!pinctrl) {
1684 debug("failed to get property %s\n", name);
1685 return 0;
1686 }
1687
1688 return pinctrl;
1689}
1690
1691static uint32_t omap_hsmmc_get_pad_conf_phandle(struct mmc *mmc,
1692 char *prop_name)
1693{
1694 const void *fdt = gd->fdt_blob;
1695 const __be32 *phandle;
1696 int node = dev_of_offset(mmc->dev);
1697
1698 phandle = fdt_getprop(fdt, node, prop_name, NULL);
1699 if (!phandle) {
1700 debug("failed to get property %s\n", prop_name);
1701 return 0;
1702 }
1703
1704 return fdt32_to_cpu(*phandle);
1705}
1706
1707static uint32_t omap_hsmmc_get_iodelay_phandle(struct mmc *mmc,
1708 char *prop_name)
1709{
1710 const void *fdt = gd->fdt_blob;
1711 const __be32 *phandle;
1712 int len;
1713 int count;
1714 int node = dev_of_offset(mmc->dev);
1715
1716 phandle = fdt_getprop(fdt, node, prop_name, &len);
1717 if (!phandle) {
1718 debug("failed to get property %s\n", prop_name);
1719 return 0;
1720 }
1721
1722 /* No manual mode iodelay values if count < 2 */
1723 count = len / sizeof(*phandle);
1724 if (count < 2)
1725 return 0;
1726
1727 return fdt32_to_cpu(*(phandle + 1));
1728}
1729
1730static struct pad_conf_entry *
1731omap_hsmmc_get_pad_conf(struct mmc *mmc, char *prop_name, int *npads)
1732{
1733 int len;
1734 int count;
1735 struct pad_conf_entry *padconf;
1736 u32 phandle;
1737 const fdt32_t *pinctrl;
1738
1739 phandle = omap_hsmmc_get_pad_conf_phandle(mmc, prop_name);
1740 if (!phandle)
1741 return ERR_PTR(-EINVAL);
1742
1743 pinctrl = omap_hsmmc_get_pinctrl_entry(phandle, "pinctrl-single,pins",
1744 &len);
1745 if (!pinctrl)
1746 return ERR_PTR(-EINVAL);
1747
1748 count = (len / sizeof(*pinctrl)) / 2;
1749 padconf = omap_hsmmc_get_pad_conf_entry(pinctrl, count);
1750 if (!padconf)
1751 return ERR_PTR(-EINVAL);
1752
1753 *npads = count;
1754
1755 return padconf;
1756}
1757
1758static struct iodelay_cfg_entry *
1759omap_hsmmc_get_iodelay(struct mmc *mmc, char *prop_name, int *niodelay)
1760{
1761 int len;
1762 int count;
1763 struct iodelay_cfg_entry *iodelay;
1764 u32 phandle;
1765 const fdt32_t *pinctrl;
1766
1767 phandle = omap_hsmmc_get_iodelay_phandle(mmc, prop_name);
1768 /* Not all modes have manual mode iodelay values. So its not fatal */
1769 if (!phandle)
1770 return 0;
1771
1772 pinctrl = omap_hsmmc_get_pinctrl_entry(phandle, "pinctrl-pin-array",
1773 &len);
1774 if (!pinctrl)
1775 return ERR_PTR(-EINVAL);
1776
1777 count = (len / sizeof(*pinctrl)) / 3;
1778 iodelay = omap_hsmmc_get_iodelay_cfg_entry(pinctrl, count);
1779 if (!iodelay)
1780 return ERR_PTR(-EINVAL);
1781
1782 *niodelay = count;
1783
1784 return iodelay;
1785}
1786
1787static struct omap_hsmmc_pinctrl_state *
1788omap_hsmmc_get_pinctrl_by_mode(struct mmc *mmc, char *mode)
1789{
1790 int index;
1791 int npads = 0;
1792 int niodelays = 0;
1793 const void *fdt = gd->fdt_blob;
1794 int node = dev_of_offset(mmc->dev);
1795 char prop_name[11];
1796 struct omap_hsmmc_pinctrl_state *pinctrl_state;
1797
1798 pinctrl_state = (struct omap_hsmmc_pinctrl_state *)
1799 malloc(sizeof(*pinctrl_state));
1800 if (!pinctrl_state) {
1801 debug("failed to allocate memory\n");
1802 return 0;
1803 }
1804
1805 index = fdt_stringlist_search(fdt, node, "pinctrl-names", mode);
1806 if (index < 0) {
1807 debug("fail to find %s mode %s\n", mode, fdt_strerror(index));
1808 goto err_pinctrl_state;
1809 }
1810
1811 sprintf(prop_name, "pinctrl-%d", index);
1812
1813 pinctrl_state->padconf = omap_hsmmc_get_pad_conf(mmc, prop_name,
1814 &npads);
1815 if (IS_ERR(pinctrl_state->padconf))
1816 goto err_pinctrl_state;
1817 pinctrl_state->npads = npads;
1818
1819 pinctrl_state->iodelay = omap_hsmmc_get_iodelay(mmc, prop_name,
1820 &niodelays);
1821 if (IS_ERR(pinctrl_state->iodelay))
1822 goto err_padconf;
1823 pinctrl_state->niodelays = niodelays;
1824
1825 return pinctrl_state;
1826
1827err_padconf:
1828 kfree(pinctrl_state->padconf);
1829
1830err_pinctrl_state:
1831 kfree(pinctrl_state);
1832 return 0;
1833}
1834
Jean-Jacques Hiblotdae1ad42018-01-30 16:01:42 +01001835#define OMAP_HSMMC_SETUP_PINCTRL(capmask, mode, optional) \
Kishon Vijay Abraham I8c2efe92018-01-30 16:01:41 +01001836 do { \
1837 struct omap_hsmmc_pinctrl_state *s = NULL; \
1838 char str[20]; \
1839 if (!(cfg->host_caps & capmask)) \
1840 break; \
1841 \
1842 if (priv->hw_rev) { \
1843 sprintf(str, "%s-%s", #mode, priv->hw_rev); \
1844 s = omap_hsmmc_get_pinctrl_by_mode(mmc, str); \
1845 } \
1846 \
1847 if (!s) \
1848 s = omap_hsmmc_get_pinctrl_by_mode(mmc, #mode); \
1849 \
Jean-Jacques Hiblotdae1ad42018-01-30 16:01:42 +01001850 if (!s && !optional) { \
Kishon Vijay Abraham I8c2efe92018-01-30 16:01:41 +01001851 debug("%s: no pinctrl for %s\n", \
1852 mmc->dev->name, #mode); \
1853 cfg->host_caps &= ~(capmask); \
1854 } else { \
1855 priv->mode##_pinctrl_state = s; \
1856 } \
Kishon Vijay Abraham Ie7da6ac2018-01-30 16:01:40 +01001857 } while (0)
1858
1859static int omap_hsmmc_get_pinctrl_state(struct mmc *mmc)
1860{
1861 struct omap_hsmmc_data *priv = omap_hsmmc_get_data(mmc);
1862 struct mmc_config *cfg = omap_hsmmc_get_cfg(mmc);
1863 struct omap_hsmmc_pinctrl_state *default_pinctrl;
1864
1865 if (!(priv->controller_flags & OMAP_HSMMC_REQUIRE_IODELAY))
1866 return 0;
1867
1868 default_pinctrl = omap_hsmmc_get_pinctrl_by_mode(mmc, "default");
1869 if (!default_pinctrl) {
1870 printf("no pinctrl state for default mode\n");
1871 return -EINVAL;
1872 }
1873
1874 priv->default_pinctrl_state = default_pinctrl;
1875
Jean-Jacques Hiblotdae1ad42018-01-30 16:01:42 +01001876 OMAP_HSMMC_SETUP_PINCTRL(MMC_CAP(UHS_SDR104), sdr104, false);
1877 OMAP_HSMMC_SETUP_PINCTRL(MMC_CAP(UHS_SDR50), sdr50, false);
1878 OMAP_HSMMC_SETUP_PINCTRL(MMC_CAP(UHS_DDR50), ddr50, false);
1879 OMAP_HSMMC_SETUP_PINCTRL(MMC_CAP(UHS_SDR25), sdr25, false);
1880 OMAP_HSMMC_SETUP_PINCTRL(MMC_CAP(UHS_SDR12), sdr12, false);
Kishon Vijay Abraham Ie7da6ac2018-01-30 16:01:40 +01001881
Jean-Jacques Hiblotdae1ad42018-01-30 16:01:42 +01001882 OMAP_HSMMC_SETUP_PINCTRL(MMC_CAP(MMC_HS_200), hs200_1_8v, false);
1883 OMAP_HSMMC_SETUP_PINCTRL(MMC_CAP(MMC_DDR_52), ddr_1_8v, false);
1884 OMAP_HSMMC_SETUP_PINCTRL(MMC_MODE_HS, hs, true);
Kishon Vijay Abraham Ie7da6ac2018-01-30 16:01:40 +01001885
1886 return 0;
1887}
1888#endif
1889
Simon Glass3580f6d2021-08-07 07:24:03 -06001890#if CONFIG_IS_ENABLED(OF_REAL)
Kishon Vijay Abraham I8c2efe92018-01-30 16:01:41 +01001891#ifdef CONFIG_OMAP54XX
1892__weak const struct mmc_platform_fixups *platform_fixups_mmc(uint32_t addr)
1893{
1894 return NULL;
1895}
1896#endif
1897
Simon Glassaad29ae2020-12-03 16:55:21 -07001898static int omap_hsmmc_of_to_plat(struct udevice *dev)
Mugunthan V Nd97631a2015-09-28 12:56:30 +05301899{
Simon Glassfa20e932020-12-03 16:55:20 -07001900 struct omap_hsmmc_plat *plat = dev_get_plat(dev);
Kishon Vijay Abraham Ie7da6ac2018-01-30 16:01:40 +01001901 struct omap_mmc_of_data *of_data = (void *)dev_get_driver_data(dev);
1902
Jean-Jacques Hiblotae51a662017-03-22 16:00:33 +01001903 struct mmc_config *cfg = &plat->cfg;
Kishon Vijay Abraham I8c2efe92018-01-30 16:01:41 +01001904#ifdef CONFIG_OMAP54XX
1905 const struct mmc_platform_fixups *fixups;
1906#endif
Mugunthan V Nd97631a2015-09-28 12:56:30 +05301907 const void *fdt = gd->fdt_blob;
Simon Glassdd79d6e2017-01-17 16:52:55 -07001908 int node = dev_of_offset(dev);
Kishon Vijay Abraham I569c3d52018-01-30 16:01:38 +01001909 int ret;
Mugunthan V Nd97631a2015-09-28 12:56:30 +05301910
Masahiro Yamadaa89b4de2020-07-17 14:36:48 +09001911 plat->base_addr = map_physmem(dev_read_addr(dev),
Simon Glassba1dea42017-05-17 17:18:05 -06001912 sizeof(struct hsmmc *),
Jean-Jacques Hiblot3d45bb42017-09-21 16:51:32 +02001913 MAP_NOCACHE);
Mugunthan V Nd97631a2015-09-28 12:56:30 +05301914
Kishon Vijay Abraham I569c3d52018-01-30 16:01:38 +01001915 ret = mmc_of_parse(dev, cfg);
1916 if (ret < 0)
1917 return ret;
Mugunthan V Nd97631a2015-09-28 12:56:30 +05301918
Jean-Jacques Hiblot8e2bdbd2018-02-23 10:40:19 +01001919 if (!cfg->f_max)
1920 cfg->f_max = 52000000;
Kishon Vijay Abraham I569c3d52018-01-30 16:01:38 +01001921 cfg->host_caps |= MMC_MODE_HS_52MHz | MMC_MODE_HS;
Mugunthan V Nd97631a2015-09-28 12:56:30 +05301922 cfg->f_min = 400000;
Mugunthan V Nd97631a2015-09-28 12:56:30 +05301923 cfg->voltages = MMC_VDD_32_33 | MMC_VDD_33_34 | MMC_VDD_165_195;
1924 cfg->b_max = CONFIG_SYS_MMC_MAX_BLK_COUNT;
Kishon Vijay Abraham I73897ed2018-01-30 16:01:32 +01001925 if (fdtdec_get_bool(fdt, node, "ti,dual-volt"))
1926 plat->controller_flags |= OMAP_HSMMC_SUPPORTS_DUAL_VOLT;
1927 if (fdtdec_get_bool(fdt, node, "no-1-8-v"))
1928 plat->controller_flags |= OMAP_HSMMC_NO_1_8_V;
Kishon Vijay Abraham Ie7da6ac2018-01-30 16:01:40 +01001929 if (of_data)
1930 plat->controller_flags |= of_data->controller_flags;
Mugunthan V Nd97631a2015-09-28 12:56:30 +05301931
Kishon Vijay Abraham I8c2efe92018-01-30 16:01:41 +01001932#ifdef CONFIG_OMAP54XX
Masahiro Yamadaa89b4de2020-07-17 14:36:48 +09001933 fixups = platform_fixups_mmc(dev_read_addr(dev));
Kishon Vijay Abraham I8c2efe92018-01-30 16:01:41 +01001934 if (fixups) {
1935 plat->hw_rev = fixups->hw_rev;
1936 cfg->host_caps &= ~fixups->unsupported_caps;
1937 cfg->f_max = fixups->max_freq;
1938 }
1939#endif
1940
Mugunthan V Nd97631a2015-09-28 12:56:30 +05301941 return 0;
1942}
Lokesh Vutla9a696fb2017-04-26 13:37:05 +05301943#endif
Mugunthan V Nd97631a2015-09-28 12:56:30 +05301944
Jean-Jacques Hiblota3c556c2017-03-22 16:00:34 +01001945#ifdef CONFIG_BLK
1946
1947static int omap_hsmmc_bind(struct udevice *dev)
1948{
Simon Glassfa20e932020-12-03 16:55:20 -07001949 struct omap_hsmmc_plat *plat = dev_get_plat(dev);
Jean-Jacques Hiblot4cb36a22018-02-23 10:40:16 +01001950 plat->mmc = calloc(1, sizeof(struct mmc));
1951 return mmc_bind(dev, plat->mmc, &plat->cfg);
Jean-Jacques Hiblota3c556c2017-03-22 16:00:34 +01001952}
1953#endif
Mugunthan V Nd97631a2015-09-28 12:56:30 +05301954static int omap_hsmmc_probe(struct udevice *dev)
1955{
Simon Glassfa20e932020-12-03 16:55:20 -07001956 struct omap_hsmmc_plat *plat = dev_get_plat(dev);
Mugunthan V Nd97631a2015-09-28 12:56:30 +05301957 struct mmc_uclass_priv *upriv = dev_get_uclass_priv(dev);
1958 struct omap_hsmmc_data *priv = dev_get_priv(dev);
Jean-Jacques Hiblotae51a662017-03-22 16:00:33 +01001959 struct mmc_config *cfg = &plat->cfg;
Mugunthan V Nd97631a2015-09-28 12:56:30 +05301960 struct mmc *mmc;
Kishon Vijay Abraham Ie7da6ac2018-01-30 16:01:40 +01001961#ifdef CONFIG_IODELAY_RECALIBRATION
1962 int ret;
1963#endif
Mugunthan V Nd97631a2015-09-28 12:56:30 +05301964
Mugunthan V Nd97631a2015-09-28 12:56:30 +05301965 cfg->name = "OMAP SD/MMC";
Lokesh Vutla9a696fb2017-04-26 13:37:05 +05301966 priv->base_addr = plat->base_addr;
Kishon Vijay Abraham Ie7da6ac2018-01-30 16:01:40 +01001967 priv->controller_flags = plat->controller_flags;
Kishon Vijay Abraham I8c2efe92018-01-30 16:01:41 +01001968 priv->hw_rev = plat->hw_rev;
Mugunthan V Nd97631a2015-09-28 12:56:30 +05301969
Jean-Jacques Hiblota3c556c2017-03-22 16:00:34 +01001970#ifdef CONFIG_BLK
Jean-Jacques Hiblot4cb36a22018-02-23 10:40:16 +01001971 mmc = plat->mmc;
Jean-Jacques Hiblota3c556c2017-03-22 16:00:34 +01001972#else
Mugunthan V Nd97631a2015-09-28 12:56:30 +05301973 mmc = mmc_create(cfg, priv);
1974 if (mmc == NULL)
1975 return -1;
Jean-Jacques Hiblota3c556c2017-03-22 16:00:34 +01001976#endif
Jean-Jacques Hiblot7a41bb42018-01-30 16:01:46 +01001977#if CONFIG_IS_ENABLED(DM_REGULATOR)
1978 device_get_supply_regulator(dev, "pbias-supply",
1979 &priv->pbias_supply);
1980#endif
Adam Ford6122af42018-08-21 07:16:56 -05001981#if defined(OMAP_HSMMC_USE_GPIO)
1982#if CONFIG_IS_ENABLED(OF_CONTROL) && CONFIG_IS_ENABLED(DM_GPIO)
Mugunthan V Na9a0aa72016-04-04 17:28:01 +05301983 gpio_request_by_name(dev, "cd-gpios", 0, &priv->cd_gpio, GPIOD_IS_IN);
1984 gpio_request_by_name(dev, "wp-gpios", 0, &priv->wp_gpio, GPIOD_IS_IN);
1985#endif
Adam Ford6122af42018-08-21 07:16:56 -05001986#endif
Mugunthan V Na9a0aa72016-04-04 17:28:01 +05301987
Simon Glass77ca42b2016-05-01 13:52:34 -06001988 mmc->dev = dev;
Mugunthan V Nd97631a2015-09-28 12:56:30 +05301989 upriv->mmc = mmc;
1990
Kishon Vijay Abraham Ie7da6ac2018-01-30 16:01:40 +01001991#ifdef CONFIG_IODELAY_RECALIBRATION
1992 ret = omap_hsmmc_get_pinctrl_state(mmc);
1993 /*
1994 * disable high speed modes for the platforms that require IO delay
1995 * and for which we don't have this information
1996 */
1997 if ((ret < 0) &&
1998 (priv->controller_flags & OMAP_HSMMC_REQUIRE_IODELAY)) {
1999 priv->controller_flags &= ~OMAP_HSMMC_REQUIRE_IODELAY;
2000 cfg->host_caps &= ~(MMC_CAP(MMC_HS_200) | MMC_CAP(MMC_DDR_52) |
2001 UHS_CAPS);
2002 }
2003#endif
2004
Jean-Jacques Hiblot8fc9d3a2017-04-14 19:50:02 +02002005 return omap_hsmmc_init_setup(mmc);
Mugunthan V Nd97631a2015-09-28 12:56:30 +05302006}
2007
Simon Glass3580f6d2021-08-07 07:24:03 -06002008#if CONFIG_IS_ENABLED(OF_REAL)
Kishon Vijay Abraham Ie7da6ac2018-01-30 16:01:40 +01002009
2010static const struct omap_mmc_of_data dra7_mmc_of_data = {
2011 .controller_flags = OMAP_HSMMC_REQUIRE_IODELAY,
2012};
2013
Mugunthan V Nd97631a2015-09-28 12:56:30 +05302014static const struct udevice_id omap_hsmmc_ids[] = {
Jean-Jacques Hiblot3d45bb42017-09-21 16:51:32 +02002015 { .compatible = "ti,omap3-hsmmc" },
2016 { .compatible = "ti,omap4-hsmmc" },
2017 { .compatible = "ti,am33xx-hsmmc" },
Lukasz Majewski46e86692022-02-18 13:28:41 +01002018 { .compatible = "ti,am335-sdhci" },
Kishon Vijay Abraham Ie7da6ac2018-01-30 16:01:40 +01002019 { .compatible = "ti,dra7-hsmmc", .data = (ulong)&dra7_mmc_of_data },
Mugunthan V Nd97631a2015-09-28 12:56:30 +05302020 { }
2021};
Lokesh Vutla9a696fb2017-04-26 13:37:05 +05302022#endif
Mugunthan V Nd97631a2015-09-28 12:56:30 +05302023
2024U_BOOT_DRIVER(omap_hsmmc) = {
2025 .name = "omap_hsmmc",
2026 .id = UCLASS_MMC,
Simon Glass3580f6d2021-08-07 07:24:03 -06002027#if CONFIG_IS_ENABLED(OF_REAL)
Mugunthan V Nd97631a2015-09-28 12:56:30 +05302028 .of_match = omap_hsmmc_ids,
Simon Glassaad29ae2020-12-03 16:55:21 -07002029 .of_to_plat = omap_hsmmc_of_to_plat,
Simon Glass71fa5b42020-12-03 16:55:18 -07002030 .plat_auto = sizeof(struct omap_hsmmc_plat),
Lokesh Vutla9a696fb2017-04-26 13:37:05 +05302031#endif
Jean-Jacques Hiblota3c556c2017-03-22 16:00:34 +01002032#ifdef CONFIG_BLK
2033 .bind = omap_hsmmc_bind,
2034#endif
Jean-Jacques Hiblot8fc9d3a2017-04-14 19:50:02 +02002035 .ops = &omap_hsmmc_ops,
Mugunthan V Nd97631a2015-09-28 12:56:30 +05302036 .probe = omap_hsmmc_probe,
Simon Glass8a2b47f2020-12-03 16:55:17 -07002037 .priv_auto = sizeof(struct omap_hsmmc_data),
Bin Meng793260a2018-10-24 06:36:32 -07002038#if !CONFIG_IS_ENABLED(OF_CONTROL)
Lokesh Vutlac38e6452017-04-26 13:37:06 +05302039 .flags = DM_FLAG_PRE_RELOC,
Bin Meng793260a2018-10-24 06:36:32 -07002040#endif
Mugunthan V Nd97631a2015-09-28 12:56:30 +05302041};
2042#endif