blob: 713faab11095cea84cb803b6270357d63b382965 [file] [log] [blame]
Sukumar Ghoraic53f5e52010-09-18 20:32:33 -07001/*
2 * (C) Copyright 2008
3 * Texas Instruments, <www.ti.com>
4 * Sukumar Ghorai <s-ghorai@ti.com>
5 *
6 * See file CREDITS for list of people who contributed to this
7 * project.
8 *
9 * This program is free software; you can redistribute it and/or
10 * modify it under the terms of the GNU General Public License as
11 * published by the Free Software Foundation's version 2 of
12 * the License.
13 *
14 * This program is distributed in the hope that it will be useful,
15 * but WITHOUT ANY WARRANTY; without even the implied warranty of
16 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
17 * GNU General Public License for more details.
18 *
19 * You should have received a copy of the GNU General Public License
20 * along with this program; if not, write to the Free Software
21 * Foundation, Inc., 59 Temple Place, Suite 330, Boston,
22 * MA 02111-1307 USA
23 */
24
25#include <config.h>
26#include <common.h>
Pantelis Antoniou2c850462014-03-11 19:34:20 +020027#include <malloc.h>
Kishon Vijay Abraham I826be2a2017-09-21 16:51:34 +020028#include <memalign.h>
Sukumar Ghoraic53f5e52010-09-18 20:32:33 -070029#include <mmc.h>
30#include <part.h>
31#include <i2c.h>
Felix Brack419eed22017-10-11 17:05:28 +020032#if defined(CONFIG_OMAP54XX) || defined(CONFIG_OMAP44XX)
Nishanth Menon627612c2013-03-26 05:20:54 +000033#include <palmas.h>
Felix Brack419eed22017-10-11 17:05:28 +020034#endif
Sukumar Ghoraic53f5e52010-09-18 20:32:33 -070035#include <asm/io.h>
36#include <asm/arch/mmc_host_def.h>
Roger Quadros44157de2015-09-19 16:26:53 +053037#if !defined(CONFIG_SOC_KEYSTONE)
38#include <asm/gpio.h>
Dirk Behme74140232011-05-15 09:04:47 +000039#include <asm/arch/sys_proto.h>
Roger Quadros44157de2015-09-19 16:26:53 +053040#endif
Tom Rinidf5338c2017-02-09 13:41:28 -050041#ifdef CONFIG_MMC_OMAP36XX_PINS
42#include <asm/arch/mux.h>
43#endif
Mugunthan V Nd97631a2015-09-28 12:56:30 +053044#include <dm.h>
45
46DECLARE_GLOBAL_DATA_PTR;
Sukumar Ghoraic53f5e52010-09-18 20:32:33 -070047
Pantelis Antoniouc9e75912014-02-26 19:28:45 +020048/* simplify defines to OMAP_HSMMC_USE_GPIO */
49#if (defined(CONFIG_OMAP_GPIO) && !defined(CONFIG_SPL_BUILD)) || \
50 (defined(CONFIG_SPL_BUILD) && defined(CONFIG_SPL_GPIO_SUPPORT))
51#define OMAP_HSMMC_USE_GPIO
52#else
53#undef OMAP_HSMMC_USE_GPIO
54#endif
55
Grazvydas Ignotasddde1882012-03-19 12:12:06 +000056/* common definitions for all OMAPs */
57#define SYSCTL_SRC (1 << 25)
58#define SYSCTL_SRD (1 << 26)
59
Nikita Kiryanov13822862012-12-03 02:19:43 +000060struct omap_hsmmc_data {
61 struct hsmmc *base_addr;
Simon Glass5f4bd8c2017-07-04 13:31:19 -060062#if !CONFIG_IS_ENABLED(DM_MMC)
Pantelis Antoniou2c850462014-03-11 19:34:20 +020063 struct mmc_config cfg;
Jean-Jacques Hiblotae51a662017-03-22 16:00:33 +010064#endif
Kishon Vijay Abraham I2e18c9b2018-01-30 16:01:31 +010065 uint bus_width;
Jean-Jacques Hiblot7fe2f192018-01-30 16:01:30 +010066 uint clock;
Pantelis Antoniouc9e75912014-02-26 19:28:45 +020067#ifdef OMAP_HSMMC_USE_GPIO
Simon Glass5f4bd8c2017-07-04 13:31:19 -060068#if CONFIG_IS_ENABLED(DM_MMC)
Mugunthan V Nd97631a2015-09-28 12:56:30 +053069 struct gpio_desc cd_gpio; /* Change Detect GPIO */
70 struct gpio_desc wp_gpio; /* Write Protect GPIO */
71 bool cd_inverted;
72#else
Nikita Kiryanov4eae05c2012-12-03 02:19:44 +000073 int cd_gpio;
Nikita Kiryanov4be9dbc2012-12-03 02:19:47 +000074 int wp_gpio;
Pantelis Antoniouc9e75912014-02-26 19:28:45 +020075#endif
Mugunthan V Nd97631a2015-09-28 12:56:30 +053076#endif
Kishon Vijay Abraham I826be2a2017-09-21 16:51:34 +020077 u8 controller_flags;
78#ifndef CONFIG_OMAP34XX
79 struct omap_hsmmc_adma_desc *adma_desc_table;
80 uint desc_slot;
81#endif
82};
83
84#ifndef CONFIG_OMAP34XX
85struct omap_hsmmc_adma_desc {
86 u8 attr;
87 u8 reserved;
88 u16 len;
89 u32 addr;
Nikita Kiryanov13822862012-12-03 02:19:43 +000090};
91
Kishon Vijay Abraham I826be2a2017-09-21 16:51:34 +020092#define ADMA_MAX_LEN 63488
93
94/* Decriptor table defines */
95#define ADMA_DESC_ATTR_VALID BIT(0)
96#define ADMA_DESC_ATTR_END BIT(1)
97#define ADMA_DESC_ATTR_INT BIT(2)
98#define ADMA_DESC_ATTR_ACT1 BIT(4)
99#define ADMA_DESC_ATTR_ACT2 BIT(5)
100
101#define ADMA_DESC_TRANSFER_DATA ADMA_DESC_ATTR_ACT2
102#define ADMA_DESC_LINK_DESC (ADMA_DESC_ATTR_ACT1 | ADMA_DESC_ATTR_ACT2)
103#endif
104
Nishanth Menond3bfaac2010-11-19 11:18:12 -0500105/* If we fail after 1 second wait, something is really bad */
106#define MAX_RETRY_MS 1000
107
Kishon Vijay Abraham I826be2a2017-09-21 16:51:34 +0200108/* DMA transfers can take a long time if a lot a data is transferred.
109 * The timeout must take in account the amount of data. Let's assume
110 * that the time will never exceed 333 ms per MB (in other word we assume
111 * that the bandwidth is always above 3MB/s).
112 */
113#define DMA_TIMEOUT_PER_MB 333
114#define OMAP_HSMMC_USE_ADMA BIT(2)
115
Sricharanf72611f2011-11-15 09:49:53 -0500116static int mmc_read_data(struct hsmmc *mmc_base, char *buf, unsigned int size);
117static int mmc_write_data(struct hsmmc *mmc_base, const char *buf,
118 unsigned int siz);
Jean-Jacques Hiblot7fe2f192018-01-30 16:01:30 +0100119static void omap_hsmmc_start_clock(struct hsmmc *mmc_base);
120static void omap_hsmmc_stop_clock(struct hsmmc *mmc_base);
Balaji T Kf843d332011-09-08 06:34:57 +0000121
Jean-Jacques Hiblotd58ef8e2017-03-22 16:00:31 +0100122static inline struct omap_hsmmc_data *omap_hsmmc_get_data(struct mmc *mmc)
123{
Simon Glass5f4bd8c2017-07-04 13:31:19 -0600124#if CONFIG_IS_ENABLED(DM_MMC)
Jean-Jacques Hiblotd58ef8e2017-03-22 16:00:31 +0100125 return dev_get_priv(mmc->dev);
126#else
127 return (struct omap_hsmmc_data *)mmc->priv;
128#endif
129}
Jean-Jacques Hiblotae51a662017-03-22 16:00:33 +0100130static inline struct mmc_config *omap_hsmmc_get_cfg(struct mmc *mmc)
131{
Simon Glass5f4bd8c2017-07-04 13:31:19 -0600132#if CONFIG_IS_ENABLED(DM_MMC)
Jean-Jacques Hiblotae51a662017-03-22 16:00:33 +0100133 struct omap_hsmmc_plat *plat = dev_get_platdata(mmc->dev);
134 return &plat->cfg;
135#else
136 return &((struct omap_hsmmc_data *)mmc->priv)->cfg;
137#endif
138}
Jean-Jacques Hiblotd58ef8e2017-03-22 16:00:31 +0100139
Simon Glass5f4bd8c2017-07-04 13:31:19 -0600140#if defined(OMAP_HSMMC_USE_GPIO) && !CONFIG_IS_ENABLED(DM_MMC)
Nikita Kiryanov4eae05c2012-12-03 02:19:44 +0000141static int omap_mmc_setup_gpio_in(int gpio, const char *label)
142{
Simon Glass1a96d7f2014-10-22 21:37:09 -0600143 int ret;
Nikita Kiryanov4eae05c2012-12-03 02:19:44 +0000144
Simon Glass1a96d7f2014-10-22 21:37:09 -0600145#ifndef CONFIG_DM_GPIO
146 if (!gpio_is_valid(gpio))
Nikita Kiryanov4eae05c2012-12-03 02:19:44 +0000147 return -1;
Simon Glass1a96d7f2014-10-22 21:37:09 -0600148#endif
149 ret = gpio_request(gpio, label);
150 if (ret)
151 return ret;
Nikita Kiryanov4eae05c2012-12-03 02:19:44 +0000152
Simon Glass1a96d7f2014-10-22 21:37:09 -0600153 ret = gpio_direction_input(gpio);
154 if (ret)
155 return ret;
Nikita Kiryanov4eae05c2012-12-03 02:19:44 +0000156
157 return gpio;
158}
Nikita Kiryanov4eae05c2012-12-03 02:19:44 +0000159#endif
160
Jeroen Hofsteeaedeeaa2014-07-12 21:24:08 +0200161static unsigned char mmc_board_init(struct mmc *mmc)
Sukumar Ghoraic53f5e52010-09-18 20:32:33 -0700162{
Sukumar Ghoraic53f5e52010-09-18 20:32:33 -0700163#if defined(CONFIG_OMAP34XX)
Jean-Jacques Hiblotae51a662017-03-22 16:00:33 +0100164 struct mmc_config *cfg = omap_hsmmc_get_cfg(mmc);
Sukumar Ghoraic53f5e52010-09-18 20:32:33 -0700165 t2_t *t2_base = (t2_t *)T2_BASE;
166 struct prcm *prcm_base = (struct prcm *)PRCM_BASE;
Grazvydas Ignotasef2b7292012-03-19 03:50:53 +0000167 u32 pbias_lite;
Adam Fordef354962017-02-06 11:31:43 -0600168#ifdef CONFIG_MMC_OMAP36XX_PINS
169 u32 wkup_ctrl = readl(OMAP34XX_CTRL_WKUP_CTRL);
170#endif
Sukumar Ghoraic53f5e52010-09-18 20:32:33 -0700171
Grazvydas Ignotasef2b7292012-03-19 03:50:53 +0000172 pbias_lite = readl(&t2_base->pbias_lite);
173 pbias_lite &= ~(PBIASLITEPWRDNZ1 | PBIASLITEPWRDNZ0);
Albert ARIBAUD \(3ADEV\)6ad09812015-01-16 09:09:50 +0100174#ifdef CONFIG_TARGET_OMAP3_CAIRO
175 /* for cairo board, we need to set up 1.8 Volt bias level on MMC1 */
176 pbias_lite &= ~PBIASLITEVMODE0;
177#endif
Adam Fordef354962017-02-06 11:31:43 -0600178#ifdef CONFIG_MMC_OMAP36XX_PINS
179 if (get_cpu_family() == CPU_OMAP36XX) {
180 /* Disable extended drain IO before changing PBIAS */
181 wkup_ctrl &= ~OMAP34XX_CTRL_WKUP_CTRL_GPIO_IO_PWRDNZ;
182 writel(wkup_ctrl, OMAP34XX_CTRL_WKUP_CTRL);
183 }
184#endif
Grazvydas Ignotasef2b7292012-03-19 03:50:53 +0000185 writel(pbias_lite, &t2_base->pbias_lite);
Paul Kocialkowski69559892014-11-08 20:55:47 +0100186
Grazvydas Ignotasef2b7292012-03-19 03:50:53 +0000187 writel(pbias_lite | PBIASLITEPWRDNZ1 |
Sukumar Ghoraic53f5e52010-09-18 20:32:33 -0700188 PBIASSPEEDCTRL0 | PBIASLITEPWRDNZ0,
189 &t2_base->pbias_lite);
190
Adam Fordef354962017-02-06 11:31:43 -0600191#ifdef CONFIG_MMC_OMAP36XX_PINS
192 if (get_cpu_family() == CPU_OMAP36XX)
193 /* Enable extended drain IO after changing PBIAS */
194 writel(wkup_ctrl |
195 OMAP34XX_CTRL_WKUP_CTRL_GPIO_IO_PWRDNZ,
196 OMAP34XX_CTRL_WKUP_CTRL);
197#endif
Sukumar Ghoraic53f5e52010-09-18 20:32:33 -0700198 writel(readl(&t2_base->devconf0) | MMCSDIO1ADPCLKISEL,
199 &t2_base->devconf0);
200
201 writel(readl(&t2_base->devconf1) | MMCSDIO2ADPCLKISEL,
202 &t2_base->devconf1);
203
Jonathan Solnita9b05562012-02-24 11:30:18 +0000204 /* Change from default of 52MHz to 26MHz if necessary */
Jean-Jacques Hiblotae51a662017-03-22 16:00:33 +0100205 if (!(cfg->host_caps & MMC_MODE_HS_52MHz))
Jonathan Solnita9b05562012-02-24 11:30:18 +0000206 writel(readl(&t2_base->ctl_prog_io1) & ~CTLPROGIO1SPEEDCTRL,
207 &t2_base->ctl_prog_io1);
208
Sukumar Ghoraic53f5e52010-09-18 20:32:33 -0700209 writel(readl(&prcm_base->fclken1_core) |
210 EN_MMC1 | EN_MMC2 | EN_MMC3,
211 &prcm_base->fclken1_core);
212
213 writel(readl(&prcm_base->iclken1_core) |
214 EN_MMC1 | EN_MMC2 | EN_MMC3,
215 &prcm_base->iclken1_core);
216#endif
217
Lokesh Vutlad999d052016-11-23 13:25:28 +0530218#if defined(CONFIG_OMAP54XX) || defined(CONFIG_OMAP44XX)
Balaji T Kf843d332011-09-08 06:34:57 +0000219 /* PBIAS config needed for MMC1 only */
Jean-Jacques Hiblot26319b12017-03-22 16:00:32 +0100220 if (mmc_get_blk_desc(mmc)->devnum == 0)
Lokesh Vutlad999d052016-11-23 13:25:28 +0530221 vmmc_pbias_config(LDO_VOLT_3V0);
Balaji T Kd9cf8362012-03-12 02:25:49 +0000222#endif
Sukumar Ghoraic53f5e52010-09-18 20:32:33 -0700223
224 return 0;
225}
226
Sricharanf72611f2011-11-15 09:49:53 -0500227void mmc_init_stream(struct hsmmc *mmc_base)
Sukumar Ghoraic53f5e52010-09-18 20:32:33 -0700228{
Nishanth Menond3bfaac2010-11-19 11:18:12 -0500229 ulong start;
Sukumar Ghoraic53f5e52010-09-18 20:32:33 -0700230
231 writel(readl(&mmc_base->con) | INIT_INITSTREAM, &mmc_base->con);
232
233 writel(MMC_CMD0, &mmc_base->cmd);
Nishanth Menond3bfaac2010-11-19 11:18:12 -0500234 start = get_timer(0);
235 while (!(readl(&mmc_base->stat) & CC_MASK)) {
236 if (get_timer(0) - start > MAX_RETRY_MS) {
237 printf("%s: timedout waiting for cc!\n", __func__);
238 return;
239 }
240 }
Sukumar Ghoraic53f5e52010-09-18 20:32:33 -0700241 writel(CC_MASK, &mmc_base->stat)
242 ;
243 writel(MMC_CMD0, &mmc_base->cmd)
244 ;
Nishanth Menond3bfaac2010-11-19 11:18:12 -0500245 start = get_timer(0);
246 while (!(readl(&mmc_base->stat) & CC_MASK)) {
247 if (get_timer(0) - start > MAX_RETRY_MS) {
248 printf("%s: timedout waiting for cc2!\n", __func__);
249 return;
250 }
251 }
Sukumar Ghoraic53f5e52010-09-18 20:32:33 -0700252 writel(readl(&mmc_base->con) & ~INIT_INITSTREAM, &mmc_base->con);
253}
254
Pantelis Antoniouc9e75912014-02-26 19:28:45 +0200255static int omap_hsmmc_init_setup(struct mmc *mmc)
Sukumar Ghoraic53f5e52010-09-18 20:32:33 -0700256{
Jean-Jacques Hiblotd58ef8e2017-03-22 16:00:31 +0100257 struct omap_hsmmc_data *priv = omap_hsmmc_get_data(mmc);
Nikita Kiryanov13822862012-12-03 02:19:43 +0000258 struct hsmmc *mmc_base;
Sukumar Ghoraic53f5e52010-09-18 20:32:33 -0700259 unsigned int reg_val;
260 unsigned int dsor;
Nishanth Menond3bfaac2010-11-19 11:18:12 -0500261 ulong start;
Sukumar Ghoraic53f5e52010-09-18 20:32:33 -0700262
Jean-Jacques Hiblotd58ef8e2017-03-22 16:00:31 +0100263 mmc_base = priv->base_addr;
Balaji T Kf843d332011-09-08 06:34:57 +0000264 mmc_board_init(mmc);
Sukumar Ghoraic53f5e52010-09-18 20:32:33 -0700265
266 writel(readl(&mmc_base->sysconfig) | MMC_SOFTRESET,
267 &mmc_base->sysconfig);
Nishanth Menond3bfaac2010-11-19 11:18:12 -0500268 start = get_timer(0);
269 while ((readl(&mmc_base->sysstatus) & RESETDONE) == 0) {
270 if (get_timer(0) - start > MAX_RETRY_MS) {
271 printf("%s: timedout waiting for cc2!\n", __func__);
Jaehoon Chung7825d202016-07-19 16:33:36 +0900272 return -ETIMEDOUT;
Nishanth Menond3bfaac2010-11-19 11:18:12 -0500273 }
274 }
Sukumar Ghoraic53f5e52010-09-18 20:32:33 -0700275 writel(readl(&mmc_base->sysctl) | SOFTRESETALL, &mmc_base->sysctl);
Nishanth Menond3bfaac2010-11-19 11:18:12 -0500276 start = get_timer(0);
277 while ((readl(&mmc_base->sysctl) & SOFTRESETALL) != 0x0) {
278 if (get_timer(0) - start > MAX_RETRY_MS) {
279 printf("%s: timedout waiting for softresetall!\n",
280 __func__);
Jaehoon Chung7825d202016-07-19 16:33:36 +0900281 return -ETIMEDOUT;
Nishanth Menond3bfaac2010-11-19 11:18:12 -0500282 }
283 }
Kishon Vijay Abraham I826be2a2017-09-21 16:51:34 +0200284#ifndef CONFIG_OMAP34XX
285 reg_val = readl(&mmc_base->hl_hwinfo);
286 if (reg_val & MADMA_EN)
287 priv->controller_flags |= OMAP_HSMMC_USE_ADMA;
288#endif
Sukumar Ghoraic53f5e52010-09-18 20:32:33 -0700289 writel(DTW_1_BITMODE | SDBP_PWROFF | SDVS_3V0, &mmc_base->hctl);
290 writel(readl(&mmc_base->capa) | VS30_3V0SUP | VS18_1V8SUP,
291 &mmc_base->capa);
292
293 reg_val = readl(&mmc_base->con) & RESERVED_MASK;
294
295 writel(CTPL_MMC_SD | reg_val | WPP_ACTIVEHIGH | CDP_ACTIVEHIGH |
296 MIT_CTO | DW8_1_4BITMODE | MODE_FUNC | STR_BLOCK |
297 HR_NOHOSTRESP | INIT_NOINIT | NOOPENDRAIN, &mmc_base->con);
298
299 dsor = 240;
300 mmc_reg_out(&mmc_base->sysctl, (ICE_MASK | DTO_MASK | CEN_MASK),
Kishon Vijay Abraham I6e543812017-09-21 16:51:36 +0200301 (ICE_STOP | DTO_15THDTO));
Sukumar Ghoraic53f5e52010-09-18 20:32:33 -0700302 mmc_reg_out(&mmc_base->sysctl, ICE_MASK | CLKD_MASK,
303 (dsor << CLKD_OFFSET) | ICE_OSCILLATE);
Nishanth Menond3bfaac2010-11-19 11:18:12 -0500304 start = get_timer(0);
305 while ((readl(&mmc_base->sysctl) & ICS_MASK) == ICS_NOTREADY) {
306 if (get_timer(0) - start > MAX_RETRY_MS) {
307 printf("%s: timedout waiting for ics!\n", __func__);
Jaehoon Chung7825d202016-07-19 16:33:36 +0900308 return -ETIMEDOUT;
Nishanth Menond3bfaac2010-11-19 11:18:12 -0500309 }
310 }
Sukumar Ghoraic53f5e52010-09-18 20:32:33 -0700311 writel(readl(&mmc_base->sysctl) | CEN_ENABLE, &mmc_base->sysctl);
312
313 writel(readl(&mmc_base->hctl) | SDBP_PWRON, &mmc_base->hctl);
314
315 writel(IE_BADA | IE_CERR | IE_DEB | IE_DCRC | IE_DTO | IE_CIE |
Kishon Vijay Abraham I826be2a2017-09-21 16:51:34 +0200316 IE_CEB | IE_CCRC | IE_ADMAE | IE_CTO | IE_BRR | IE_BWR | IE_TC |
317 IE_CC, &mmc_base->ie);
Sukumar Ghoraic53f5e52010-09-18 20:32:33 -0700318
319 mmc_init_stream(mmc_base);
320
321 return 0;
322}
323
Grazvydas Ignotasddde1882012-03-19 12:12:06 +0000324/*
325 * MMC controller internal finite state machine reset
326 *
327 * Used to reset command or data internal state machines, using respectively
328 * SRC or SRD bit of SYSCTL register
329 */
330static void mmc_reset_controller_fsm(struct hsmmc *mmc_base, u32 bit)
331{
332 ulong start;
333
334 mmc_reg_out(&mmc_base->sysctl, bit, bit);
335
Oleksandr Tyshchenko06640ca2013-08-06 13:44:16 +0300336 /*
337 * CMD(DAT) lines reset procedures are slightly different
338 * for OMAP3 and OMAP4(AM335x,OMAP5,DRA7xx).
339 * According to OMAP3 TRM:
340 * Set SRC(SRD) bit in MMCHS_SYSCTL register to 0x1 and wait until it
341 * returns to 0x0.
342 * According to OMAP4(AM335x,OMAP5,DRA7xx) TRMs, CMD(DATA) lines reset
343 * procedure steps must be as follows:
344 * 1. Initiate CMD(DAT) line reset by writing 0x1 to SRC(SRD) bit in
345 * MMCHS_SYSCTL register (SD_SYSCTL for AM335x).
346 * 2. Poll the SRC(SRD) bit until it is set to 0x1.
347 * 3. Wait until the SRC (SRD) bit returns to 0x0
348 * (reset procedure is completed).
349 */
350#if defined(CONFIG_OMAP44XX) || defined(CONFIG_OMAP54XX) || \
Nikita Kiryanov5ffdd852015-07-30 23:56:20 +0300351 defined(CONFIG_AM33XX) || defined(CONFIG_AM43XX)
Oleksandr Tyshchenko06640ca2013-08-06 13:44:16 +0300352 if (!(readl(&mmc_base->sysctl) & bit)) {
353 start = get_timer(0);
354 while (!(readl(&mmc_base->sysctl) & bit)) {
355 if (get_timer(0) - start > MAX_RETRY_MS)
356 return;
357 }
358 }
359#endif
Grazvydas Ignotasddde1882012-03-19 12:12:06 +0000360 start = get_timer(0);
361 while ((readl(&mmc_base->sysctl) & bit) != 0) {
362 if (get_timer(0) - start > MAX_RETRY_MS) {
363 printf("%s: timedout waiting for sysctl %x to clear\n",
364 __func__, bit);
365 return;
366 }
367 }
368}
Kishon Vijay Abraham I826be2a2017-09-21 16:51:34 +0200369
370#ifndef CONFIG_OMAP34XX
371static void omap_hsmmc_adma_desc(struct mmc *mmc, char *buf, u16 len, bool end)
372{
373 struct omap_hsmmc_data *priv = omap_hsmmc_get_data(mmc);
374 struct omap_hsmmc_adma_desc *desc;
375 u8 attr;
376
377 desc = &priv->adma_desc_table[priv->desc_slot];
378
379 attr = ADMA_DESC_ATTR_VALID | ADMA_DESC_TRANSFER_DATA;
380 if (!end)
381 priv->desc_slot++;
382 else
383 attr |= ADMA_DESC_ATTR_END;
384
385 desc->len = len;
386 desc->addr = (u32)buf;
387 desc->reserved = 0;
388 desc->attr = attr;
389}
390
391static void omap_hsmmc_prepare_adma_table(struct mmc *mmc,
392 struct mmc_data *data)
393{
394 uint total_len = data->blocksize * data->blocks;
395 uint desc_count = DIV_ROUND_UP(total_len, ADMA_MAX_LEN);
396 struct omap_hsmmc_data *priv = omap_hsmmc_get_data(mmc);
397 int i = desc_count;
398 char *buf;
399
400 priv->desc_slot = 0;
401 priv->adma_desc_table = (struct omap_hsmmc_adma_desc *)
402 memalign(ARCH_DMA_MINALIGN, desc_count *
403 sizeof(struct omap_hsmmc_adma_desc));
404
405 if (data->flags & MMC_DATA_READ)
406 buf = data->dest;
407 else
408 buf = (char *)data->src;
409
410 while (--i) {
411 omap_hsmmc_adma_desc(mmc, buf, ADMA_MAX_LEN, false);
412 buf += ADMA_MAX_LEN;
413 total_len -= ADMA_MAX_LEN;
414 }
415
416 omap_hsmmc_adma_desc(mmc, buf, total_len, true);
417
418 flush_dcache_range((long)priv->adma_desc_table,
419 (long)priv->adma_desc_table +
420 ROUND(desc_count *
421 sizeof(struct omap_hsmmc_adma_desc),
422 ARCH_DMA_MINALIGN));
423}
424
425static void omap_hsmmc_prepare_data(struct mmc *mmc, struct mmc_data *data)
426{
427 struct hsmmc *mmc_base;
428 struct omap_hsmmc_data *priv = omap_hsmmc_get_data(mmc);
429 u32 val;
430 char *buf;
431
432 mmc_base = priv->base_addr;
433 omap_hsmmc_prepare_adma_table(mmc, data);
434
435 if (data->flags & MMC_DATA_READ)
436 buf = data->dest;
437 else
438 buf = (char *)data->src;
439
440 val = readl(&mmc_base->hctl);
441 val |= DMA_SELECT;
442 writel(val, &mmc_base->hctl);
443
444 val = readl(&mmc_base->con);
445 val |= DMA_MASTER;
446 writel(val, &mmc_base->con);
447
448 writel((u32)priv->adma_desc_table, &mmc_base->admasal);
449
450 flush_dcache_range((u32)buf,
451 (u32)buf +
452 ROUND(data->blocksize * data->blocks,
453 ARCH_DMA_MINALIGN));
454}
455
456static void omap_hsmmc_dma_cleanup(struct mmc *mmc)
457{
458 struct hsmmc *mmc_base;
459 struct omap_hsmmc_data *priv = omap_hsmmc_get_data(mmc);
460 u32 val;
461
462 mmc_base = priv->base_addr;
463
464 val = readl(&mmc_base->con);
465 val &= ~DMA_MASTER;
466 writel(val, &mmc_base->con);
467
468 val = readl(&mmc_base->hctl);
469 val &= ~DMA_SELECT;
470 writel(val, &mmc_base->hctl);
471
472 kfree(priv->adma_desc_table);
473}
474#else
475#define omap_hsmmc_adma_desc
476#define omap_hsmmc_prepare_adma_table
477#define omap_hsmmc_prepare_data
478#define omap_hsmmc_dma_cleanup
479#endif
480
Simon Glass5f4bd8c2017-07-04 13:31:19 -0600481#if !CONFIG_IS_ENABLED(DM_MMC)
Pantelis Antoniouc9e75912014-02-26 19:28:45 +0200482static int omap_hsmmc_send_cmd(struct mmc *mmc, struct mmc_cmd *cmd,
Sukumar Ghoraic53f5e52010-09-18 20:32:33 -0700483 struct mmc_data *data)
484{
Jean-Jacques Hiblotd58ef8e2017-03-22 16:00:31 +0100485 struct omap_hsmmc_data *priv = omap_hsmmc_get_data(mmc);
Jean-Jacques Hiblot8fc9d3a2017-04-14 19:50:02 +0200486#else
487static int omap_hsmmc_send_cmd(struct udevice *dev, struct mmc_cmd *cmd,
488 struct mmc_data *data)
489{
490 struct omap_hsmmc_data *priv = dev_get_priv(dev);
Kishon Vijay Abraham I826be2a2017-09-21 16:51:34 +0200491#ifndef CONFIG_OMAP34XX
492 struct mmc_uclass_priv *upriv = dev_get_uclass_priv(dev);
493 struct mmc *mmc = upriv->mmc;
494#endif
Jean-Jacques Hiblot8fc9d3a2017-04-14 19:50:02 +0200495#endif
Nikita Kiryanov13822862012-12-03 02:19:43 +0000496 struct hsmmc *mmc_base;
Sukumar Ghoraic53f5e52010-09-18 20:32:33 -0700497 unsigned int flags, mmc_stat;
Nishanth Menond3bfaac2010-11-19 11:18:12 -0500498 ulong start;
Sukumar Ghoraic53f5e52010-09-18 20:32:33 -0700499
Jean-Jacques Hiblotd58ef8e2017-03-22 16:00:31 +0100500 mmc_base = priv->base_addr;
Kishon Vijay Abraham I316e7ae2017-09-21 16:51:35 +0200501
502 if (cmd->cmdidx == MMC_CMD_STOP_TRANSMISSION)
503 return 0;
504
Nishanth Menond3bfaac2010-11-19 11:18:12 -0500505 start = get_timer(0);
Tom Rini32ec3252012-01-30 11:22:25 +0000506 while ((readl(&mmc_base->pstate) & (DATI_MASK | CMDI_MASK)) != 0) {
Nishanth Menond3bfaac2010-11-19 11:18:12 -0500507 if (get_timer(0) - start > MAX_RETRY_MS) {
Tom Rini32ec3252012-01-30 11:22:25 +0000508 printf("%s: timedout waiting on cmd inhibit to clear\n",
509 __func__);
Jaehoon Chung7825d202016-07-19 16:33:36 +0900510 return -ETIMEDOUT;
Nishanth Menond3bfaac2010-11-19 11:18:12 -0500511 }
512 }
Sukumar Ghoraic53f5e52010-09-18 20:32:33 -0700513 writel(0xFFFFFFFF, &mmc_base->stat);
Nishanth Menond3bfaac2010-11-19 11:18:12 -0500514 start = get_timer(0);
515 while (readl(&mmc_base->stat)) {
516 if (get_timer(0) - start > MAX_RETRY_MS) {
Grazvydas Ignotas8927ac92012-03-19 12:11:43 +0000517 printf("%s: timedout waiting for STAT (%x) to clear\n",
518 __func__, readl(&mmc_base->stat));
Jaehoon Chung7825d202016-07-19 16:33:36 +0900519 return -ETIMEDOUT;
Nishanth Menond3bfaac2010-11-19 11:18:12 -0500520 }
521 }
Sukumar Ghoraic53f5e52010-09-18 20:32:33 -0700522 /*
523 * CMDREG
524 * CMDIDX[13:8] : Command index
525 * DATAPRNT[5] : Data Present Select
526 * ENCMDIDX[4] : Command Index Check Enable
527 * ENCMDCRC[3] : Command CRC Check Enable
528 * RSPTYP[1:0]
529 * 00 = No Response
530 * 01 = Length 136
531 * 10 = Length 48
532 * 11 = Length 48 Check busy after response
533 */
534 /* Delay added before checking the status of frq change
535 * retry not supported by mmc.c(core file)
536 */
537 if (cmd->cmdidx == SD_CMD_APP_SEND_SCR)
538 udelay(50000); /* wait 50 ms */
539
540 if (!(cmd->resp_type & MMC_RSP_PRESENT))
541 flags = 0;
542 else if (cmd->resp_type & MMC_RSP_136)
543 flags = RSP_TYPE_LGHT136 | CICE_NOCHECK;
544 else if (cmd->resp_type & MMC_RSP_BUSY)
545 flags = RSP_TYPE_LGHT48B;
546 else
547 flags = RSP_TYPE_LGHT48;
548
549 /* enable default flags */
550 flags = flags | (CMD_TYPE_NORMAL | CICE_NOCHECK | CCCE_NOCHECK |
Kishon Vijay Abraham I6e543812017-09-21 16:51:36 +0200551 MSBS_SGLEBLK);
552 flags &= ~(ACEN_ENABLE | BCE_ENABLE | DE_ENABLE);
Sukumar Ghoraic53f5e52010-09-18 20:32:33 -0700553
554 if (cmd->resp_type & MMC_RSP_CRC)
555 flags |= CCCE_CHECK;
556 if (cmd->resp_type & MMC_RSP_OPCODE)
557 flags |= CICE_CHECK;
558
559 if (data) {
560 if ((cmd->cmdidx == MMC_CMD_READ_MULTIPLE_BLOCK) ||
561 (cmd->cmdidx == MMC_CMD_WRITE_MULTIPLE_BLOCK)) {
Kishon Vijay Abraham I316e7ae2017-09-21 16:51:35 +0200562 flags |= (MSBS_MULTIBLK | BCE_ENABLE | ACEN_ENABLE);
Sukumar Ghoraic53f5e52010-09-18 20:32:33 -0700563 data->blocksize = 512;
564 writel(data->blocksize | (data->blocks << 16),
565 &mmc_base->blk);
566 } else
567 writel(data->blocksize | NBLK_STPCNT, &mmc_base->blk);
568
569 if (data->flags & MMC_DATA_READ)
570 flags |= (DP_DATA | DDIR_READ);
571 else
572 flags |= (DP_DATA | DDIR_WRITE);
Kishon Vijay Abraham I826be2a2017-09-21 16:51:34 +0200573
574#ifndef CONFIG_OMAP34XX
575 if ((priv->controller_flags & OMAP_HSMMC_USE_ADMA) &&
576 !mmc_is_tuning_cmd(cmd->cmdidx)) {
577 omap_hsmmc_prepare_data(mmc, data);
578 flags |= DE_ENABLE;
579 }
580#endif
Sukumar Ghoraic53f5e52010-09-18 20:32:33 -0700581 }
582
583 writel(cmd->cmdarg, &mmc_base->arg);
Lubomir Popov19df4122013-08-14 18:59:18 +0300584 udelay(20); /* To fix "No status update" error on eMMC */
Sukumar Ghoraic53f5e52010-09-18 20:32:33 -0700585 writel((cmd->cmdidx << 24) | flags, &mmc_base->cmd);
586
Nishanth Menond3bfaac2010-11-19 11:18:12 -0500587 start = get_timer(0);
Sukumar Ghoraic53f5e52010-09-18 20:32:33 -0700588 do {
589 mmc_stat = readl(&mmc_base->stat);
Kishon Vijay Abraham I826be2a2017-09-21 16:51:34 +0200590 if (get_timer(start) > MAX_RETRY_MS) {
Nishanth Menond3bfaac2010-11-19 11:18:12 -0500591 printf("%s : timeout: No status update\n", __func__);
Jaehoon Chung7825d202016-07-19 16:33:36 +0900592 return -ETIMEDOUT;
Nishanth Menond3bfaac2010-11-19 11:18:12 -0500593 }
594 } while (!mmc_stat);
Sukumar Ghoraic53f5e52010-09-18 20:32:33 -0700595
Grazvydas Ignotasddde1882012-03-19 12:12:06 +0000596 if ((mmc_stat & IE_CTO) != 0) {
597 mmc_reset_controller_fsm(mmc_base, SYSCTL_SRC);
Jaehoon Chung7825d202016-07-19 16:33:36 +0900598 return -ETIMEDOUT;
Grazvydas Ignotasddde1882012-03-19 12:12:06 +0000599 } else if ((mmc_stat & ERRI_MASK) != 0)
Sukumar Ghoraic53f5e52010-09-18 20:32:33 -0700600 return -1;
601
602 if (mmc_stat & CC_MASK) {
603 writel(CC_MASK, &mmc_base->stat);
604 if (cmd->resp_type & MMC_RSP_PRESENT) {
605 if (cmd->resp_type & MMC_RSP_136) {
606 /* response type 2 */
607 cmd->response[3] = readl(&mmc_base->rsp10);
608 cmd->response[2] = readl(&mmc_base->rsp32);
609 cmd->response[1] = readl(&mmc_base->rsp54);
610 cmd->response[0] = readl(&mmc_base->rsp76);
611 } else
612 /* response types 1, 1b, 3, 4, 5, 6 */
613 cmd->response[0] = readl(&mmc_base->rsp10);
614 }
615 }
616
Kishon Vijay Abraham I826be2a2017-09-21 16:51:34 +0200617#ifndef CONFIG_OMAP34XX
618 if ((priv->controller_flags & OMAP_HSMMC_USE_ADMA) && data &&
619 !mmc_is_tuning_cmd(cmd->cmdidx)) {
620 u32 sz_mb, timeout;
621
622 if (mmc_stat & IE_ADMAE) {
623 omap_hsmmc_dma_cleanup(mmc);
624 return -EIO;
625 }
626
627 sz_mb = DIV_ROUND_UP(data->blocksize * data->blocks, 1 << 20);
628 timeout = sz_mb * DMA_TIMEOUT_PER_MB;
629 if (timeout < MAX_RETRY_MS)
630 timeout = MAX_RETRY_MS;
631
632 start = get_timer(0);
633 do {
634 mmc_stat = readl(&mmc_base->stat);
635 if (mmc_stat & TC_MASK) {
636 writel(readl(&mmc_base->stat) | TC_MASK,
637 &mmc_base->stat);
638 break;
639 }
640 if (get_timer(start) > timeout) {
641 printf("%s : DMA timeout: No status update\n",
642 __func__);
643 return -ETIMEDOUT;
644 }
645 } while (1);
646
647 omap_hsmmc_dma_cleanup(mmc);
648 return 0;
649 }
650#endif
651
Sukumar Ghoraic53f5e52010-09-18 20:32:33 -0700652 if (data && (data->flags & MMC_DATA_READ)) {
653 mmc_read_data(mmc_base, data->dest,
654 data->blocksize * data->blocks);
655 } else if (data && (data->flags & MMC_DATA_WRITE)) {
656 mmc_write_data(mmc_base, data->src,
657 data->blocksize * data->blocks);
658 }
659 return 0;
660}
661
Sricharanf72611f2011-11-15 09:49:53 -0500662static int mmc_read_data(struct hsmmc *mmc_base, char *buf, unsigned int size)
Sukumar Ghoraic53f5e52010-09-18 20:32:33 -0700663{
664 unsigned int *output_buf = (unsigned int *)buf;
665 unsigned int mmc_stat;
666 unsigned int count;
667
668 /*
669 * Start Polled Read
670 */
671 count = (size > MMCSD_SECTOR_SIZE) ? MMCSD_SECTOR_SIZE : size;
672 count /= 4;
673
674 while (size) {
Nishanth Menond3bfaac2010-11-19 11:18:12 -0500675 ulong start = get_timer(0);
Sukumar Ghoraic53f5e52010-09-18 20:32:33 -0700676 do {
677 mmc_stat = readl(&mmc_base->stat);
Nishanth Menond3bfaac2010-11-19 11:18:12 -0500678 if (get_timer(0) - start > MAX_RETRY_MS) {
679 printf("%s: timedout waiting for status!\n",
680 __func__);
Jaehoon Chung7825d202016-07-19 16:33:36 +0900681 return -ETIMEDOUT;
Nishanth Menond3bfaac2010-11-19 11:18:12 -0500682 }
Sukumar Ghoraic53f5e52010-09-18 20:32:33 -0700683 } while (mmc_stat == 0);
684
Grazvydas Ignotasddde1882012-03-19 12:12:06 +0000685 if ((mmc_stat & (IE_DTO | IE_DCRC | IE_DEB)) != 0)
686 mmc_reset_controller_fsm(mmc_base, SYSCTL_SRD);
687
Sukumar Ghoraic53f5e52010-09-18 20:32:33 -0700688 if ((mmc_stat & ERRI_MASK) != 0)
689 return 1;
690
691 if (mmc_stat & BRR_MASK) {
692 unsigned int k;
693
694 writel(readl(&mmc_base->stat) | BRR_MASK,
695 &mmc_base->stat);
696 for (k = 0; k < count; k++) {
697 *output_buf = readl(&mmc_base->data);
698 output_buf++;
699 }
700 size -= (count*4);
701 }
702
703 if (mmc_stat & BWR_MASK)
704 writel(readl(&mmc_base->stat) | BWR_MASK,
705 &mmc_base->stat);
706
707 if (mmc_stat & TC_MASK) {
708 writel(readl(&mmc_base->stat) | TC_MASK,
709 &mmc_base->stat);
710 break;
711 }
712 }
713 return 0;
714}
715
Sricharanf72611f2011-11-15 09:49:53 -0500716static int mmc_write_data(struct hsmmc *mmc_base, const char *buf,
717 unsigned int size)
Sukumar Ghoraic53f5e52010-09-18 20:32:33 -0700718{
719 unsigned int *input_buf = (unsigned int *)buf;
720 unsigned int mmc_stat;
721 unsigned int count;
722
723 /*
Lubomir Popov19df4122013-08-14 18:59:18 +0300724 * Start Polled Write
Sukumar Ghoraic53f5e52010-09-18 20:32:33 -0700725 */
726 count = (size > MMCSD_SECTOR_SIZE) ? MMCSD_SECTOR_SIZE : size;
727 count /= 4;
728
729 while (size) {
Nishanth Menond3bfaac2010-11-19 11:18:12 -0500730 ulong start = get_timer(0);
Sukumar Ghoraic53f5e52010-09-18 20:32:33 -0700731 do {
732 mmc_stat = readl(&mmc_base->stat);
Nishanth Menond3bfaac2010-11-19 11:18:12 -0500733 if (get_timer(0) - start > MAX_RETRY_MS) {
734 printf("%s: timedout waiting for status!\n",
735 __func__);
Jaehoon Chung7825d202016-07-19 16:33:36 +0900736 return -ETIMEDOUT;
Nishanth Menond3bfaac2010-11-19 11:18:12 -0500737 }
Sukumar Ghoraic53f5e52010-09-18 20:32:33 -0700738 } while (mmc_stat == 0);
739
Grazvydas Ignotasddde1882012-03-19 12:12:06 +0000740 if ((mmc_stat & (IE_DTO | IE_DCRC | IE_DEB)) != 0)
741 mmc_reset_controller_fsm(mmc_base, SYSCTL_SRD);
742
Sukumar Ghoraic53f5e52010-09-18 20:32:33 -0700743 if ((mmc_stat & ERRI_MASK) != 0)
744 return 1;
745
746 if (mmc_stat & BWR_MASK) {
747 unsigned int k;
748
749 writel(readl(&mmc_base->stat) | BWR_MASK,
750 &mmc_base->stat);
751 for (k = 0; k < count; k++) {
752 writel(*input_buf, &mmc_base->data);
753 input_buf++;
754 }
755 size -= (count*4);
756 }
757
758 if (mmc_stat & BRR_MASK)
759 writel(readl(&mmc_base->stat) | BRR_MASK,
760 &mmc_base->stat);
761
762 if (mmc_stat & TC_MASK) {
763 writel(readl(&mmc_base->stat) | TC_MASK,
764 &mmc_base->stat);
765 break;
766 }
767 }
768 return 0;
769}
770
Jean-Jacques Hiblot7fe2f192018-01-30 16:01:30 +0100771static void omap_hsmmc_stop_clock(struct hsmmc *mmc_base)
772{
773 writel(readl(&mmc_base->sysctl) & ~CEN_ENABLE, &mmc_base->sysctl);
774}
775
776static void omap_hsmmc_start_clock(struct hsmmc *mmc_base)
777{
778 writel(readl(&mmc_base->sysctl) | CEN_ENABLE, &mmc_base->sysctl);
779}
780
781static void omap_hsmmc_set_clock(struct mmc *mmc)
782{
783 struct omap_hsmmc_data *priv = omap_hsmmc_get_data(mmc);
784 struct hsmmc *mmc_base;
785 unsigned int dsor = 0;
786 ulong start;
787
788 mmc_base = priv->base_addr;
789 omap_hsmmc_stop_clock(mmc_base);
790
791 /* TODO: Is setting DTO required here? */
792 mmc_reg_out(&mmc_base->sysctl, (ICE_MASK | DTO_MASK),
793 (ICE_STOP | DTO_15THDTO));
794
795 if (mmc->clock != 0) {
796 dsor = DIV_ROUND_UP(MMC_CLOCK_REFERENCE * 1000000, mmc->clock);
797 if (dsor > CLKD_MAX)
798 dsor = CLKD_MAX;
799 } else {
800 dsor = CLKD_MAX;
801 }
802
803 mmc_reg_out(&mmc_base->sysctl, ICE_MASK | CLKD_MASK,
804 (dsor << CLKD_OFFSET) | ICE_OSCILLATE);
805
806 start = get_timer(0);
807 while ((readl(&mmc_base->sysctl) & ICS_MASK) == ICS_NOTREADY) {
808 if (get_timer(0) - start > MAX_RETRY_MS) {
809 printf("%s: timedout waiting for ics!\n", __func__);
810 return;
811 }
812 }
813
814 priv->clock = mmc->clock;
815 omap_hsmmc_start_clock(mmc_base);
816}
817
Kishon Vijay Abraham I2e18c9b2018-01-30 16:01:31 +0100818static void omap_hsmmc_set_bus_width(struct mmc *mmc)
Sukumar Ghoraic53f5e52010-09-18 20:32:33 -0700819{
Jean-Jacques Hiblotd58ef8e2017-03-22 16:00:31 +0100820 struct omap_hsmmc_data *priv = omap_hsmmc_get_data(mmc);
Nikita Kiryanov13822862012-12-03 02:19:43 +0000821 struct hsmmc *mmc_base;
Sukumar Ghoraic53f5e52010-09-18 20:32:33 -0700822
Jean-Jacques Hiblotd58ef8e2017-03-22 16:00:31 +0100823 mmc_base = priv->base_addr;
Sukumar Ghoraic53f5e52010-09-18 20:32:33 -0700824 /* configue bus width */
825 switch (mmc->bus_width) {
826 case 8:
827 writel(readl(&mmc_base->con) | DTW_8_BITMODE,
828 &mmc_base->con);
829 break;
830
831 case 4:
832 writel(readl(&mmc_base->con) & ~DTW_8_BITMODE,
833 &mmc_base->con);
834 writel(readl(&mmc_base->hctl) | DTW_4_BITMODE,
835 &mmc_base->hctl);
836 break;
837
838 case 1:
839 default:
840 writel(readl(&mmc_base->con) & ~DTW_8_BITMODE,
841 &mmc_base->con);
842 writel(readl(&mmc_base->hctl) & ~DTW_4_BITMODE,
843 &mmc_base->hctl);
844 break;
845 }
846
Kishon Vijay Abraham I2e18c9b2018-01-30 16:01:31 +0100847 priv->bus_width = mmc->bus_width;
848}
849
850#if !CONFIG_IS_ENABLED(DM_MMC)
851static int omap_hsmmc_set_ios(struct mmc *mmc)
852{
853 struct omap_hsmmc_data *priv = omap_hsmmc_get_data(mmc);
854#else
855static int omap_hsmmc_set_ios(struct udevice *dev)
856{
857 struct omap_hsmmc_data *priv = dev_get_priv(dev);
858 struct mmc_uclass_priv *upriv = dev_get_uclass_priv(dev);
859 struct mmc *mmc = upriv->mmc;
860#endif
861
862 if (priv->bus_width != mmc->bus_width)
863 omap_hsmmc_set_bus_width(mmc);
864
Jean-Jacques Hiblot7fe2f192018-01-30 16:01:30 +0100865 if (priv->clock != mmc->clock)
866 omap_hsmmc_set_clock(mmc);
Jaehoon Chungb6cd1d32016-12-30 15:30:16 +0900867
868 return 0;
Sukumar Ghoraic53f5e52010-09-18 20:32:33 -0700869}
870
Pantelis Antoniouc9e75912014-02-26 19:28:45 +0200871#ifdef OMAP_HSMMC_USE_GPIO
Simon Glass5f4bd8c2017-07-04 13:31:19 -0600872#if CONFIG_IS_ENABLED(DM_MMC)
Jean-Jacques Hiblot8fc9d3a2017-04-14 19:50:02 +0200873static int omap_hsmmc_getcd(struct udevice *dev)
Pantelis Antoniouc9e75912014-02-26 19:28:45 +0200874{
Jean-Jacques Hiblot8fc9d3a2017-04-14 19:50:02 +0200875 struct omap_hsmmc_data *priv = dev_get_priv(dev);
Mugunthan V Nd97631a2015-09-28 12:56:30 +0530876 int value;
877
878 value = dm_gpio_get_value(&priv->cd_gpio);
879 /* if no CD return as 1 */
880 if (value < 0)
881 return 1;
882
883 if (priv->cd_inverted)
884 return !value;
885 return value;
886}
887
Jean-Jacques Hiblot8fc9d3a2017-04-14 19:50:02 +0200888static int omap_hsmmc_getwp(struct udevice *dev)
Mugunthan V Nd97631a2015-09-28 12:56:30 +0530889{
Jean-Jacques Hiblot8fc9d3a2017-04-14 19:50:02 +0200890 struct omap_hsmmc_data *priv = dev_get_priv(dev);
Mugunthan V Nd97631a2015-09-28 12:56:30 +0530891 int value;
892
893 value = dm_gpio_get_value(&priv->wp_gpio);
894 /* if no WP return as 0 */
895 if (value < 0)
896 return 0;
897 return value;
898}
899#else
900static int omap_hsmmc_getcd(struct mmc *mmc)
901{
Jean-Jacques Hiblotd58ef8e2017-03-22 16:00:31 +0100902 struct omap_hsmmc_data *priv = omap_hsmmc_get_data(mmc);
Pantelis Antoniouc9e75912014-02-26 19:28:45 +0200903 int cd_gpio;
904
905 /* if no CD return as 1 */
Jean-Jacques Hiblotd58ef8e2017-03-22 16:00:31 +0100906 cd_gpio = priv->cd_gpio;
Pantelis Antoniouc9e75912014-02-26 19:28:45 +0200907 if (cd_gpio < 0)
908 return 1;
909
Igor Grinberg2f4e0952014-11-03 11:32:23 +0200910 /* NOTE: assumes card detect signal is active-low */
911 return !gpio_get_value(cd_gpio);
Pantelis Antoniouc9e75912014-02-26 19:28:45 +0200912}
913
914static int omap_hsmmc_getwp(struct mmc *mmc)
915{
Jean-Jacques Hiblotd58ef8e2017-03-22 16:00:31 +0100916 struct omap_hsmmc_data *priv = omap_hsmmc_get_data(mmc);
Pantelis Antoniouc9e75912014-02-26 19:28:45 +0200917 int wp_gpio;
918
919 /* if no WP return as 0 */
Jean-Jacques Hiblotd58ef8e2017-03-22 16:00:31 +0100920 wp_gpio = priv->wp_gpio;
Pantelis Antoniouc9e75912014-02-26 19:28:45 +0200921 if (wp_gpio < 0)
922 return 0;
923
Igor Grinberg2f4e0952014-11-03 11:32:23 +0200924 /* NOTE: assumes write protect signal is active-high */
Pantelis Antoniouc9e75912014-02-26 19:28:45 +0200925 return gpio_get_value(wp_gpio);
926}
927#endif
Mugunthan V Nd97631a2015-09-28 12:56:30 +0530928#endif
Pantelis Antoniouc9e75912014-02-26 19:28:45 +0200929
Simon Glass5f4bd8c2017-07-04 13:31:19 -0600930#if CONFIG_IS_ENABLED(DM_MMC)
Jean-Jacques Hiblot8fc9d3a2017-04-14 19:50:02 +0200931static const struct dm_mmc_ops omap_hsmmc_ops = {
932 .send_cmd = omap_hsmmc_send_cmd,
933 .set_ios = omap_hsmmc_set_ios,
934#ifdef OMAP_HSMMC_USE_GPIO
935 .get_cd = omap_hsmmc_getcd,
936 .get_wp = omap_hsmmc_getwp,
937#endif
938};
939#else
Pantelis Antoniouc9e75912014-02-26 19:28:45 +0200940static const struct mmc_ops omap_hsmmc_ops = {
941 .send_cmd = omap_hsmmc_send_cmd,
942 .set_ios = omap_hsmmc_set_ios,
943 .init = omap_hsmmc_init_setup,
944#ifdef OMAP_HSMMC_USE_GPIO
945 .getcd = omap_hsmmc_getcd,
946 .getwp = omap_hsmmc_getwp,
947#endif
948};
Jean-Jacques Hiblot8fc9d3a2017-04-14 19:50:02 +0200949#endif
Pantelis Antoniouc9e75912014-02-26 19:28:45 +0200950
Simon Glass5f4bd8c2017-07-04 13:31:19 -0600951#if !CONFIG_IS_ENABLED(DM_MMC)
Nikita Kiryanov4be9dbc2012-12-03 02:19:47 +0000952int omap_mmc_init(int dev_index, uint host_caps_mask, uint f_max, int cd_gpio,
953 int wp_gpio)
Sukumar Ghoraic53f5e52010-09-18 20:32:33 -0700954{
Pantelis Antoniou2c850462014-03-11 19:34:20 +0200955 struct mmc *mmc;
Jean-Jacques Hiblotd58ef8e2017-03-22 16:00:31 +0100956 struct omap_hsmmc_data *priv;
Pantelis Antoniou2c850462014-03-11 19:34:20 +0200957 struct mmc_config *cfg;
958 uint host_caps_val;
959
Jean-Jacques Hiblotd58ef8e2017-03-22 16:00:31 +0100960 priv = malloc(sizeof(*priv));
961 if (priv == NULL)
Pantelis Antoniou2c850462014-03-11 19:34:20 +0200962 return -1;
Sukumar Ghoraic53f5e52010-09-18 20:32:33 -0700963
Rob Herring5fd3edd2015-03-23 17:56:59 -0500964 host_caps_val = MMC_MODE_4BIT | MMC_MODE_HS_52MHz | MMC_MODE_HS;
Sukumar Ghoraic53f5e52010-09-18 20:32:33 -0700965
966 switch (dev_index) {
967 case 0:
Jean-Jacques Hiblotd58ef8e2017-03-22 16:00:31 +0100968 priv->base_addr = (struct hsmmc *)OMAP_HSMMC1_BASE;
Sukumar Ghoraic53f5e52010-09-18 20:32:33 -0700969 break;
Tom Rinifd6e2942011-10-12 06:20:50 +0000970#ifdef OMAP_HSMMC2_BASE
Sukumar Ghoraic53f5e52010-09-18 20:32:33 -0700971 case 1:
Jean-Jacques Hiblotd58ef8e2017-03-22 16:00:31 +0100972 priv->base_addr = (struct hsmmc *)OMAP_HSMMC2_BASE;
Lubomir Popov19df4122013-08-14 18:59:18 +0300973#if (defined(CONFIG_OMAP44XX) || defined(CONFIG_OMAP54XX) || \
Nishanth Menon813fe9d2016-11-29 15:22:00 +0530974 defined(CONFIG_DRA7XX) || defined(CONFIG_AM33XX) || \
Roger Quadros44157de2015-09-19 16:26:53 +0530975 defined(CONFIG_AM43XX) || defined(CONFIG_SOC_KEYSTONE)) && \
976 defined(CONFIG_HSMMC2_8BIT)
Lubomir Popov19df4122013-08-14 18:59:18 +0300977 /* Enable 8-bit interface for eMMC on OMAP4/5 or DRA7XX */
978 host_caps_val |= MMC_MODE_8BIT;
979#endif
Sukumar Ghoraic53f5e52010-09-18 20:32:33 -0700980 break;
Tom Rinifd6e2942011-10-12 06:20:50 +0000981#endif
982#ifdef OMAP_HSMMC3_BASE
Sukumar Ghoraic53f5e52010-09-18 20:32:33 -0700983 case 2:
Jean-Jacques Hiblotd58ef8e2017-03-22 16:00:31 +0100984 priv->base_addr = (struct hsmmc *)OMAP_HSMMC3_BASE;
Nishanth Menon813fe9d2016-11-29 15:22:00 +0530985#if defined(CONFIG_DRA7XX) && defined(CONFIG_HSMMC3_8BIT)
Lubomir Popov19df4122013-08-14 18:59:18 +0300986 /* Enable 8-bit interface for eMMC on DRA7XX */
987 host_caps_val |= MMC_MODE_8BIT;
988#endif
Sukumar Ghoraic53f5e52010-09-18 20:32:33 -0700989 break;
Tom Rinifd6e2942011-10-12 06:20:50 +0000990#endif
Sukumar Ghoraic53f5e52010-09-18 20:32:33 -0700991 default:
Jean-Jacques Hiblotd58ef8e2017-03-22 16:00:31 +0100992 priv->base_addr = (struct hsmmc *)OMAP_HSMMC1_BASE;
Sukumar Ghoraic53f5e52010-09-18 20:32:33 -0700993 return 1;
994 }
Pantelis Antoniouc9e75912014-02-26 19:28:45 +0200995#ifdef OMAP_HSMMC_USE_GPIO
996 /* on error gpio values are set to -1, which is what we want */
Jean-Jacques Hiblotd58ef8e2017-03-22 16:00:31 +0100997 priv->cd_gpio = omap_mmc_setup_gpio_in(cd_gpio, "mmc_cd");
998 priv->wp_gpio = omap_mmc_setup_gpio_in(wp_gpio, "mmc_wp");
Pantelis Antoniouc9e75912014-02-26 19:28:45 +0200999#endif
Peter Korsgaard47c6b2a2013-03-21 04:00:04 +00001000
Jean-Jacques Hiblotd58ef8e2017-03-22 16:00:31 +01001001 cfg = &priv->cfg;
Sukumar Ghoraic53f5e52010-09-18 20:32:33 -07001002
Pantelis Antoniou2c850462014-03-11 19:34:20 +02001003 cfg->name = "OMAP SD/MMC";
1004 cfg->ops = &omap_hsmmc_ops;
1005
1006 cfg->voltages = MMC_VDD_32_33 | MMC_VDD_33_34 | MMC_VDD_165_195;
1007 cfg->host_caps = host_caps_val & ~host_caps_mask;
1008
1009 cfg->f_min = 400000;
Jonathan Solnita9b05562012-02-24 11:30:18 +00001010
1011 if (f_max != 0)
Pantelis Antoniou2c850462014-03-11 19:34:20 +02001012 cfg->f_max = f_max;
Jonathan Solnita9b05562012-02-24 11:30:18 +00001013 else {
Pantelis Antoniou2c850462014-03-11 19:34:20 +02001014 if (cfg->host_caps & MMC_MODE_HS) {
1015 if (cfg->host_caps & MMC_MODE_HS_52MHz)
1016 cfg->f_max = 52000000;
Jonathan Solnita9b05562012-02-24 11:30:18 +00001017 else
Pantelis Antoniou2c850462014-03-11 19:34:20 +02001018 cfg->f_max = 26000000;
Jonathan Solnita9b05562012-02-24 11:30:18 +00001019 } else
Pantelis Antoniou2c850462014-03-11 19:34:20 +02001020 cfg->f_max = 20000000;
Jonathan Solnita9b05562012-02-24 11:30:18 +00001021 }
Sukumar Ghoraic53f5e52010-09-18 20:32:33 -07001022
Pantelis Antoniou2c850462014-03-11 19:34:20 +02001023 cfg->b_max = CONFIG_SYS_MMC_MAX_BLK_COUNT;
John Rigbyf2f43662011-04-18 05:50:08 +00001024
John Rigby91fcc4b2011-04-19 05:48:14 +00001025#if defined(CONFIG_OMAP34XX)
1026 /*
1027 * Silicon revs 2.1 and older do not support multiblock transfers.
1028 */
1029 if ((get_cpu_family() == CPU_OMAP34XX) && (get_cpu_rev() <= CPU_3XX_ES21))
Pantelis Antoniou2c850462014-03-11 19:34:20 +02001030 cfg->b_max = 1;
John Rigby91fcc4b2011-04-19 05:48:14 +00001031#endif
Jean-Jacques Hiblotd58ef8e2017-03-22 16:00:31 +01001032 mmc = mmc_create(cfg, priv);
Pantelis Antoniou2c850462014-03-11 19:34:20 +02001033 if (mmc == NULL)
1034 return -1;
Sukumar Ghoraic53f5e52010-09-18 20:32:33 -07001035
1036 return 0;
1037}
Mugunthan V Nd97631a2015-09-28 12:56:30 +05301038#else
Lokesh Vutla9a696fb2017-04-26 13:37:05 +05301039#if CONFIG_IS_ENABLED(OF_CONTROL) && !CONFIG_IS_ENABLED(OF_PLATDATA)
Mugunthan V Nd97631a2015-09-28 12:56:30 +05301040static int omap_hsmmc_ofdata_to_platdata(struct udevice *dev)
1041{
Jean-Jacques Hiblotae51a662017-03-22 16:00:33 +01001042 struct omap_hsmmc_plat *plat = dev_get_platdata(dev);
1043 struct mmc_config *cfg = &plat->cfg;
Mugunthan V Nd97631a2015-09-28 12:56:30 +05301044 const void *fdt = gd->fdt_blob;
Simon Glassdd79d6e2017-01-17 16:52:55 -07001045 int node = dev_of_offset(dev);
Mugunthan V Nd97631a2015-09-28 12:56:30 +05301046 int val;
1047
Simon Glassba1dea42017-05-17 17:18:05 -06001048 plat->base_addr = map_physmem(devfdt_get_addr(dev),
1049 sizeof(struct hsmmc *),
Jean-Jacques Hiblot3d45bb42017-09-21 16:51:32 +02001050 MAP_NOCACHE);
Mugunthan V Nd97631a2015-09-28 12:56:30 +05301051
1052 cfg->host_caps = MMC_MODE_HS_52MHz | MMC_MODE_HS;
1053 val = fdtdec_get_int(fdt, node, "bus-width", -1);
1054 if (val < 0) {
1055 printf("error: bus-width property missing\n");
1056 return -ENOENT;
1057 }
1058
1059 switch (val) {
1060 case 0x8:
1061 cfg->host_caps |= MMC_MODE_8BIT;
1062 case 0x4:
1063 cfg->host_caps |= MMC_MODE_4BIT;
1064 break;
1065 default:
1066 printf("error: invalid bus-width property\n");
1067 return -ENOENT;
1068 }
1069
1070 cfg->f_min = 400000;
1071 cfg->f_max = fdtdec_get_int(fdt, node, "max-frequency", 52000000);
1072 cfg->voltages = MMC_VDD_32_33 | MMC_VDD_33_34 | MMC_VDD_165_195;
1073 cfg->b_max = CONFIG_SYS_MMC_MAX_BLK_COUNT;
1074
Sekhar Nori640fd702016-08-10 19:24:03 +05301075#ifdef OMAP_HSMMC_USE_GPIO
Lokesh Vutla9a696fb2017-04-26 13:37:05 +05301076 plat->cd_inverted = fdtdec_get_bool(fdt, node, "cd-inverted");
Sekhar Nori640fd702016-08-10 19:24:03 +05301077#endif
Mugunthan V Nd97631a2015-09-28 12:56:30 +05301078
1079 return 0;
1080}
Lokesh Vutla9a696fb2017-04-26 13:37:05 +05301081#endif
Mugunthan V Nd97631a2015-09-28 12:56:30 +05301082
Jean-Jacques Hiblota3c556c2017-03-22 16:00:34 +01001083#ifdef CONFIG_BLK
1084
1085static int omap_hsmmc_bind(struct udevice *dev)
1086{
1087 struct omap_hsmmc_plat *plat = dev_get_platdata(dev);
1088
1089 return mmc_bind(dev, &plat->mmc, &plat->cfg);
1090}
1091#endif
Mugunthan V Nd97631a2015-09-28 12:56:30 +05301092static int omap_hsmmc_probe(struct udevice *dev)
1093{
Jean-Jacques Hiblotae51a662017-03-22 16:00:33 +01001094 struct omap_hsmmc_plat *plat = dev_get_platdata(dev);
Mugunthan V Nd97631a2015-09-28 12:56:30 +05301095 struct mmc_uclass_priv *upriv = dev_get_uclass_priv(dev);
1096 struct omap_hsmmc_data *priv = dev_get_priv(dev);
Jean-Jacques Hiblotae51a662017-03-22 16:00:33 +01001097 struct mmc_config *cfg = &plat->cfg;
Mugunthan V Nd97631a2015-09-28 12:56:30 +05301098 struct mmc *mmc;
1099
Mugunthan V Nd97631a2015-09-28 12:56:30 +05301100 cfg->name = "OMAP SD/MMC";
Lokesh Vutla9a696fb2017-04-26 13:37:05 +05301101 priv->base_addr = plat->base_addr;
1102#ifdef OMAP_HSMMC_USE_GPIO
1103 priv->cd_inverted = plat->cd_inverted;
1104#endif
Mugunthan V Nd97631a2015-09-28 12:56:30 +05301105
Jean-Jacques Hiblota3c556c2017-03-22 16:00:34 +01001106#ifdef CONFIG_BLK
1107 mmc = &plat->mmc;
1108#else
Mugunthan V Nd97631a2015-09-28 12:56:30 +05301109 mmc = mmc_create(cfg, priv);
1110 if (mmc == NULL)
1111 return -1;
Jean-Jacques Hiblota3c556c2017-03-22 16:00:34 +01001112#endif
Mugunthan V Nd97631a2015-09-28 12:56:30 +05301113
Lokesh Vutla9a696fb2017-04-26 13:37:05 +05301114#if defined(OMAP_HSMMC_USE_GPIO) && CONFIG_IS_ENABLED(OF_CONTROL)
Mugunthan V Na9a0aa72016-04-04 17:28:01 +05301115 gpio_request_by_name(dev, "cd-gpios", 0, &priv->cd_gpio, GPIOD_IS_IN);
1116 gpio_request_by_name(dev, "wp-gpios", 0, &priv->wp_gpio, GPIOD_IS_IN);
1117#endif
1118
Simon Glass77ca42b2016-05-01 13:52:34 -06001119 mmc->dev = dev;
Mugunthan V Nd97631a2015-09-28 12:56:30 +05301120 upriv->mmc = mmc;
1121
Jean-Jacques Hiblot8fc9d3a2017-04-14 19:50:02 +02001122 return omap_hsmmc_init_setup(mmc);
Mugunthan V Nd97631a2015-09-28 12:56:30 +05301123}
1124
Lokesh Vutla9a696fb2017-04-26 13:37:05 +05301125#if CONFIG_IS_ENABLED(OF_CONTROL) && !CONFIG_IS_ENABLED(OF_PLATDATA)
Mugunthan V Nd97631a2015-09-28 12:56:30 +05301126static const struct udevice_id omap_hsmmc_ids[] = {
Jean-Jacques Hiblot3d45bb42017-09-21 16:51:32 +02001127 { .compatible = "ti,omap3-hsmmc" },
1128 { .compatible = "ti,omap4-hsmmc" },
1129 { .compatible = "ti,am33xx-hsmmc" },
Mugunthan V Nd97631a2015-09-28 12:56:30 +05301130 { }
1131};
Lokesh Vutla9a696fb2017-04-26 13:37:05 +05301132#endif
Mugunthan V Nd97631a2015-09-28 12:56:30 +05301133
1134U_BOOT_DRIVER(omap_hsmmc) = {
1135 .name = "omap_hsmmc",
1136 .id = UCLASS_MMC,
Lokesh Vutla9a696fb2017-04-26 13:37:05 +05301137#if CONFIG_IS_ENABLED(OF_CONTROL) && !CONFIG_IS_ENABLED(OF_PLATDATA)
Mugunthan V Nd97631a2015-09-28 12:56:30 +05301138 .of_match = omap_hsmmc_ids,
1139 .ofdata_to_platdata = omap_hsmmc_ofdata_to_platdata,
Lokesh Vutla9a696fb2017-04-26 13:37:05 +05301140 .platdata_auto_alloc_size = sizeof(struct omap_hsmmc_plat),
1141#endif
Jean-Jacques Hiblota3c556c2017-03-22 16:00:34 +01001142#ifdef CONFIG_BLK
1143 .bind = omap_hsmmc_bind,
1144#endif
Jean-Jacques Hiblot8fc9d3a2017-04-14 19:50:02 +02001145 .ops = &omap_hsmmc_ops,
Mugunthan V Nd97631a2015-09-28 12:56:30 +05301146 .probe = omap_hsmmc_probe,
1147 .priv_auto_alloc_size = sizeof(struct omap_hsmmc_data),
Lokesh Vutlac38e6452017-04-26 13:37:06 +05301148 .flags = DM_FLAG_PRE_RELOC,
Mugunthan V Nd97631a2015-09-28 12:56:30 +05301149};
1150#endif