blob: 20b6ff5b4af1122f34eb24ebc83775794c8033bd [file] [log] [blame]
Olof Johansson03d2bfc2011-01-01 23:52:56 -05001/*
2 * Copyright (C) 2010 Google, Inc.
3 *
4 * This software is licensed under the terms of the GNU General Public
5 * License version 2, as published by the Free Software Foundation, and
6 * may be copied, distributed, and modified under those terms.
7 *
8 * This program is distributed in the hope that it will be useful,
9 * but WITHOUT ANY WARRANTY; without even the implied warranty of
10 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
11 * GNU General Public License for more details.
12 *
13 */
14
Lucas Stache5c63d92016-02-29 21:56:25 +010015#include <linux/delay.h>
Olof Johansson03d2bfc2011-01-01 23:52:56 -050016#include <linux/err.h>
Paul Gortmaker96547f52011-07-03 15:15:51 -040017#include <linux/module.h>
Olof Johansson03d2bfc2011-01-01 23:52:56 -050018#include <linux/init.h>
19#include <linux/platform_device.h>
20#include <linux/clk.h>
21#include <linux/io.h>
Stephen Warren55cd65e2011-08-30 13:17:16 -060022#include <linux/of.h>
Stephen Warren3e44a1a2012-02-01 16:30:55 -070023#include <linux/of_device.h>
Olof Johansson03d2bfc2011-01-01 23:52:56 -050024#include <linux/mmc/card.h>
25#include <linux/mmc/host.h>
Lucas Stachc3c23842015-12-22 19:41:02 +010026#include <linux/mmc/mmc.h>
Joseph Lo0aacd232013-03-11 14:44:11 -060027#include <linux/mmc/slot-gpio.h>
Mylene JOSSERAND2391b342015-03-30 23:39:25 +020028#include <linux/gpio/consumer.h>
Olof Johansson03d2bfc2011-01-01 23:52:56 -050029
Olof Johansson03d2bfc2011-01-01 23:52:56 -050030#include "sdhci-pltfm.h"
31
Pavan Kunapulica5879d2012-04-18 18:48:02 +053032/* Tegra SDHOST controller vendor register definitions */
Lucas Stach74cd42b2015-12-22 19:41:01 +010033#define SDHCI_TEGRA_VENDOR_CLOCK_CTRL 0x100
Lucas Stachc3c23842015-12-22 19:41:02 +010034#define SDHCI_CLOCK_CTRL_TAP_MASK 0x00ff0000
35#define SDHCI_CLOCK_CTRL_TAP_SHIFT 16
36#define SDHCI_CLOCK_CTRL_SDR50_TUNING_OVERRIDE BIT(5)
Lucas Stach74cd42b2015-12-22 19:41:01 +010037#define SDHCI_CLOCK_CTRL_PADPIPE_CLKEN_OVERRIDE BIT(3)
38#define SDHCI_CLOCK_CTRL_SPI_MODE_CLKEN_OVERRIDE BIT(2)
39
Pavan Kunapulica5879d2012-04-18 18:48:02 +053040#define SDHCI_TEGRA_VENDOR_MISC_CTRL 0x120
Andrew Bresticker31453512014-05-22 08:55:35 -070041#define SDHCI_MISC_CTRL_ENABLE_SDR104 0x8
42#define SDHCI_MISC_CTRL_ENABLE_SDR50 0x10
Pavan Kunapulica5879d2012-04-18 18:48:02 +053043#define SDHCI_MISC_CTRL_ENABLE_SDHCI_SPEC_300 0x20
Andrew Bresticker31453512014-05-22 08:55:35 -070044#define SDHCI_MISC_CTRL_ENABLE_DDR50 0x200
Pavan Kunapulica5879d2012-04-18 18:48:02 +053045
Lucas Stache5c63d92016-02-29 21:56:25 +010046#define SDHCI_TEGRA_AUTO_CAL_CONFIG 0x1e4
47#define SDHCI_AUTO_CAL_START BIT(31)
48#define SDHCI_AUTO_CAL_ENABLE BIT(29)
49
Stephen Warren3e44a1a2012-02-01 16:30:55 -070050#define NVQUIRK_FORCE_SDHCI_SPEC_200 BIT(0)
51#define NVQUIRK_ENABLE_BLOCK_GAP_DET BIT(1)
Pavan Kunapulica5879d2012-04-18 18:48:02 +053052#define NVQUIRK_ENABLE_SDHCI_SPEC_300 BIT(2)
Lucas Stach7ad2ed12015-12-22 19:41:03 +010053#define NVQUIRK_ENABLE_SDR50 BIT(3)
54#define NVQUIRK_ENABLE_SDR104 BIT(4)
55#define NVQUIRK_ENABLE_DDR50 BIT(5)
Lucas Stache5c63d92016-02-29 21:56:25 +010056#define NVQUIRK_HAS_PADCALIB BIT(6)
Stephen Warren3e44a1a2012-02-01 16:30:55 -070057
58struct sdhci_tegra_soc_data {
Lars-Peter Clausen1db5eeb2013-03-13 19:26:03 +010059 const struct sdhci_pltfm_data *pdata;
Stephen Warren3e44a1a2012-02-01 16:30:55 -070060 u32 nvquirks;
61};
62
63struct sdhci_tegra {
Stephen Warren3e44a1a2012-02-01 16:30:55 -070064 const struct sdhci_tegra_soc_data *soc_data;
Mylene JOSSERAND2391b342015-03-30 23:39:25 +020065 struct gpio_desc *power_gpio;
Lucas Stacha8e326a2015-12-22 19:41:00 +010066 bool ddr_signaling;
Lucas Stache5c63d92016-02-29 21:56:25 +010067 bool pad_calib_required;
Stephen Warren3e44a1a2012-02-01 16:30:55 -070068};
69
Olof Johansson03d2bfc2011-01-01 23:52:56 -050070static u16 tegra_sdhci_readw(struct sdhci_host *host, int reg)
71{
Stephen Warren3e44a1a2012-02-01 16:30:55 -070072 struct sdhci_pltfm_host *pltfm_host = sdhci_priv(host);
Jisheng Zhang0734e792016-02-16 21:08:29 +080073 struct sdhci_tegra *tegra_host = sdhci_pltfm_priv(pltfm_host);
Stephen Warren3e44a1a2012-02-01 16:30:55 -070074 const struct sdhci_tegra_soc_data *soc_data = tegra_host->soc_data;
75
76 if (unlikely((soc_data->nvquirks & NVQUIRK_FORCE_SDHCI_SPEC_200) &&
77 (reg == SDHCI_HOST_VERSION))) {
Olof Johansson03d2bfc2011-01-01 23:52:56 -050078 /* Erratum: Version register is invalid in HW. */
79 return SDHCI_SPEC_200;
80 }
81
82 return readw(host->ioaddr + reg);
83}
84
Pavan Kunapuli352ee862015-01-28 11:45:16 -050085static void tegra_sdhci_writew(struct sdhci_host *host, u16 val, int reg)
86{
87 struct sdhci_pltfm_host *pltfm_host = sdhci_priv(host);
Pavan Kunapuli352ee862015-01-28 11:45:16 -050088
Rhyland Klein01df7ec2015-02-11 12:55:51 -050089 switch (reg) {
90 case SDHCI_TRANSFER_MODE:
91 /*
92 * Postpone this write, we must do it together with a
93 * command write that is down below.
94 */
95 pltfm_host->xfer_mode_shadow = val;
96 return;
97 case SDHCI_COMMAND:
98 writel((val << 16) | pltfm_host->xfer_mode_shadow,
99 host->ioaddr + SDHCI_TRANSFER_MODE);
100 return;
Pavan Kunapuli352ee862015-01-28 11:45:16 -0500101 }
102
103 writew(val, host->ioaddr + reg);
104}
105
Olof Johansson03d2bfc2011-01-01 23:52:56 -0500106static void tegra_sdhci_writel(struct sdhci_host *host, u32 val, int reg)
107{
Stephen Warren3e44a1a2012-02-01 16:30:55 -0700108 struct sdhci_pltfm_host *pltfm_host = sdhci_priv(host);
Jisheng Zhang0734e792016-02-16 21:08:29 +0800109 struct sdhci_tegra *tegra_host = sdhci_pltfm_priv(pltfm_host);
Stephen Warren3e44a1a2012-02-01 16:30:55 -0700110 const struct sdhci_tegra_soc_data *soc_data = tegra_host->soc_data;
111
Olof Johansson03d2bfc2011-01-01 23:52:56 -0500112 /* Seems like we're getting spurious timeout and crc errors, so
113 * disable signalling of them. In case of real errors software
114 * timers should take care of eventually detecting them.
115 */
116 if (unlikely(reg == SDHCI_SIGNAL_ENABLE))
117 val &= ~(SDHCI_INT_TIMEOUT|SDHCI_INT_CRC);
118
119 writel(val, host->ioaddr + reg);
120
Stephen Warren3e44a1a2012-02-01 16:30:55 -0700121 if (unlikely((soc_data->nvquirks & NVQUIRK_ENABLE_BLOCK_GAP_DET) &&
122 (reg == SDHCI_INT_ENABLE))) {
Olof Johansson03d2bfc2011-01-01 23:52:56 -0500123 /* Erratum: Must enable block gap interrupt detection */
124 u8 gap_ctrl = readb(host->ioaddr + SDHCI_BLOCK_GAP_CONTROL);
125 if (val & SDHCI_INT_CARD_INT)
126 gap_ctrl |= 0x8;
127 else
128 gap_ctrl &= ~0x8;
129 writeb(gap_ctrl, host->ioaddr + SDHCI_BLOCK_GAP_CONTROL);
130 }
131}
132
Stephen Warren3e44a1a2012-02-01 16:30:55 -0700133static unsigned int tegra_sdhci_get_ro(struct sdhci_host *host)
Olof Johansson03d2bfc2011-01-01 23:52:56 -0500134{
Joseph Lo0aacd232013-03-11 14:44:11 -0600135 return mmc_gpio_get_ro(host->mmc);
Olof Johansson03d2bfc2011-01-01 23:52:56 -0500136}
137
Russell King03231f92014-04-25 12:57:12 +0100138static void tegra_sdhci_reset(struct sdhci_host *host, u8 mask)
Pavan Kunapulica5879d2012-04-18 18:48:02 +0530139{
140 struct sdhci_pltfm_host *pltfm_host = sdhci_priv(host);
Jisheng Zhang0734e792016-02-16 21:08:29 +0800141 struct sdhci_tegra *tegra_host = sdhci_pltfm_priv(pltfm_host);
Pavan Kunapulica5879d2012-04-18 18:48:02 +0530142 const struct sdhci_tegra_soc_data *soc_data = tegra_host->soc_data;
Lucas Stach74cd42b2015-12-22 19:41:01 +0100143 u32 misc_ctrl, clk_ctrl;
Pavan Kunapulica5879d2012-04-18 18:48:02 +0530144
Russell King03231f92014-04-25 12:57:12 +0100145 sdhci_reset(host, mask);
146
Pavan Kunapulica5879d2012-04-18 18:48:02 +0530147 if (!(mask & SDHCI_RESET_ALL))
148 return;
149
Lucas Stach1b84def2015-12-22 19:41:04 +0100150 misc_ctrl = sdhci_readl(host, SDHCI_TEGRA_VENDOR_MISC_CTRL);
Lucas Stach74cd42b2015-12-22 19:41:01 +0100151 clk_ctrl = sdhci_readl(host, SDHCI_TEGRA_VENDOR_CLOCK_CTRL);
Jon Hunter4f6aa322016-07-12 14:53:37 +0100152
153 misc_ctrl &= ~(SDHCI_MISC_CTRL_ENABLE_SDHCI_SPEC_300 |
154 SDHCI_MISC_CTRL_ENABLE_SDR50 |
155 SDHCI_MISC_CTRL_ENABLE_DDR50 |
156 SDHCI_MISC_CTRL_ENABLE_SDR104);
157
Lucas Stach74cd42b2015-12-22 19:41:01 +0100158 clk_ctrl &= ~SDHCI_CLOCK_CTRL_SPI_MODE_CLKEN_OVERRIDE;
Jon Hunter4f6aa322016-07-12 14:53:37 +0100159
160 /*
161 * If the board does not define a regulator for the SDHCI
162 * IO voltage, then don't advertise support for UHS modes
163 * even if the device supports it because the IO voltage
164 * cannot be configured.
165 */
166 if (!IS_ERR(host->mmc->supply.vqmmc)) {
167 /* Erratum: Enable SDHCI spec v3.00 support */
168 if (soc_data->nvquirks & NVQUIRK_ENABLE_SDHCI_SPEC_300)
169 misc_ctrl |= SDHCI_MISC_CTRL_ENABLE_SDHCI_SPEC_300;
170 /* Advertise UHS modes as supported by host */
171 if (soc_data->nvquirks & NVQUIRK_ENABLE_SDR50)
172 misc_ctrl |= SDHCI_MISC_CTRL_ENABLE_SDR50;
173 if (soc_data->nvquirks & NVQUIRK_ENABLE_DDR50)
174 misc_ctrl |= SDHCI_MISC_CTRL_ENABLE_DDR50;
175 if (soc_data->nvquirks & NVQUIRK_ENABLE_SDR104)
176 misc_ctrl |= SDHCI_MISC_CTRL_ENABLE_SDR104;
177 if (soc_data->nvquirks & SDHCI_MISC_CTRL_ENABLE_SDR50)
178 clk_ctrl |= SDHCI_CLOCK_CTRL_SDR50_TUNING_OVERRIDE;
179 }
180
181 sdhci_writel(host, misc_ctrl, SDHCI_TEGRA_VENDOR_MISC_CTRL);
Lucas Stach74cd42b2015-12-22 19:41:01 +0100182 sdhci_writel(host, clk_ctrl, SDHCI_TEGRA_VENDOR_CLOCK_CTRL);
183
Lucas Stache5c63d92016-02-29 21:56:25 +0100184 if (soc_data->nvquirks & NVQUIRK_HAS_PADCALIB)
185 tegra_host->pad_calib_required = true;
186
Lucas Stacha8e326a2015-12-22 19:41:00 +0100187 tegra_host->ddr_signaling = false;
Pavan Kunapulica5879d2012-04-18 18:48:02 +0530188}
189
Russell King2317f562014-04-25 12:57:07 +0100190static void tegra_sdhci_set_bus_width(struct sdhci_host *host, int bus_width)
Olof Johansson03d2bfc2011-01-01 23:52:56 -0500191{
Olof Johansson03d2bfc2011-01-01 23:52:56 -0500192 u32 ctrl;
193
Olof Johansson03d2bfc2011-01-01 23:52:56 -0500194 ctrl = sdhci_readb(host, SDHCI_HOST_CONTROL);
Joseph Lo0aacd232013-03-11 14:44:11 -0600195 if ((host->mmc->caps & MMC_CAP_8_BIT_DATA) &&
196 (bus_width == MMC_BUS_WIDTH_8)) {
Olof Johansson03d2bfc2011-01-01 23:52:56 -0500197 ctrl &= ~SDHCI_CTRL_4BITBUS;
198 ctrl |= SDHCI_CTRL_8BITBUS;
199 } else {
200 ctrl &= ~SDHCI_CTRL_8BITBUS;
201 if (bus_width == MMC_BUS_WIDTH_4)
202 ctrl |= SDHCI_CTRL_4BITBUS;
203 else
204 ctrl &= ~SDHCI_CTRL_4BITBUS;
205 }
206 sdhci_writeb(host, ctrl, SDHCI_HOST_CONTROL);
Olof Johansson03d2bfc2011-01-01 23:52:56 -0500207}
208
Lucas Stache5c63d92016-02-29 21:56:25 +0100209static void tegra_sdhci_pad_autocalib(struct sdhci_host *host)
210{
211 u32 val;
212
213 mdelay(1);
214
215 val = sdhci_readl(host, SDHCI_TEGRA_AUTO_CAL_CONFIG);
216 val |= SDHCI_AUTO_CAL_ENABLE | SDHCI_AUTO_CAL_START;
217 sdhci_writel(host,val, SDHCI_TEGRA_AUTO_CAL_CONFIG);
218}
219
Lucas Stacha8e326a2015-12-22 19:41:00 +0100220static void tegra_sdhci_set_clock(struct sdhci_host *host, unsigned int clock)
221{
222 struct sdhci_pltfm_host *pltfm_host = sdhci_priv(host);
Jisheng Zhang0734e792016-02-16 21:08:29 +0800223 struct sdhci_tegra *tegra_host = sdhci_pltfm_priv(pltfm_host);
Lucas Stacha8e326a2015-12-22 19:41:00 +0100224 unsigned long host_clk;
225
226 if (!clock)
Lucas Stach3491b692016-02-29 21:56:24 +0100227 return sdhci_set_clock(host, clock);
Lucas Stacha8e326a2015-12-22 19:41:00 +0100228
229 host_clk = tegra_host->ddr_signaling ? clock * 2 : clock;
230 clk_set_rate(pltfm_host->clk, host_clk);
231 host->max_clk = clk_get_rate(pltfm_host->clk);
232
Lucas Stache5c63d92016-02-29 21:56:25 +0100233 sdhci_set_clock(host, clock);
234
235 if (tegra_host->pad_calib_required) {
236 tegra_sdhci_pad_autocalib(host);
237 tegra_host->pad_calib_required = false;
238 }
Lucas Stacha8e326a2015-12-22 19:41:00 +0100239}
240
241static void tegra_sdhci_set_uhs_signaling(struct sdhci_host *host,
242 unsigned timing)
243{
244 struct sdhci_pltfm_host *pltfm_host = sdhci_priv(host);
Jisheng Zhang0734e792016-02-16 21:08:29 +0800245 struct sdhci_tegra *tegra_host = sdhci_pltfm_priv(pltfm_host);
Lucas Stacha8e326a2015-12-22 19:41:00 +0100246
247 if (timing == MMC_TIMING_UHS_DDR50)
248 tegra_host->ddr_signaling = true;
249
250 return sdhci_set_uhs_signaling(host, timing);
251}
252
253static unsigned int tegra_sdhci_get_max_clock(struct sdhci_host *host)
254{
255 struct sdhci_pltfm_host *pltfm_host = sdhci_priv(host);
256
257 /*
258 * DDR modes require the host to run at double the card frequency, so
259 * the maximum rate we can support is half of the module input clock.
260 */
261 return clk_round_rate(pltfm_host->clk, UINT_MAX) / 2;
262}
263
Lucas Stachc3c23842015-12-22 19:41:02 +0100264static void tegra_sdhci_set_tap(struct sdhci_host *host, unsigned int tap)
265{
266 u32 reg;
267
268 reg = sdhci_readl(host, SDHCI_TEGRA_VENDOR_CLOCK_CTRL);
269 reg &= ~SDHCI_CLOCK_CTRL_TAP_MASK;
270 reg |= tap << SDHCI_CLOCK_CTRL_TAP_SHIFT;
271 sdhci_writel(host, reg, SDHCI_TEGRA_VENDOR_CLOCK_CTRL);
272}
273
274static int tegra_sdhci_execute_tuning(struct sdhci_host *host, u32 opcode)
275{
276 unsigned int min, max;
277
278 /*
279 * Start search for minimum tap value at 10, as smaller values are
280 * may wrongly be reported as working but fail at higher speeds,
281 * according to the TRM.
282 */
283 min = 10;
284 while (min < 255) {
285 tegra_sdhci_set_tap(host, min);
286 if (!mmc_send_tuning(host->mmc, opcode, NULL))
287 break;
288 min++;
289 }
290
291 /* Find the maximum tap value that still passes. */
292 max = min + 1;
293 while (max < 255) {
294 tegra_sdhci_set_tap(host, max);
295 if (mmc_send_tuning(host->mmc, opcode, NULL)) {
296 max--;
297 break;
298 }
299 max++;
300 }
301
302 /* The TRM states the ideal tap value is at 75% in the passing range. */
303 tegra_sdhci_set_tap(host, min + ((max - min) * 3 / 4));
304
305 return mmc_send_tuning(host->mmc, opcode, NULL);
306}
307
Lucas Stache5c63d92016-02-29 21:56:25 +0100308static void tegra_sdhci_voltage_switch(struct sdhci_host *host)
309{
310 struct sdhci_pltfm_host *pltfm_host = sdhci_priv(host);
311 struct sdhci_tegra *tegra_host = sdhci_pltfm_priv(pltfm_host);
312 const struct sdhci_tegra_soc_data *soc_data = tegra_host->soc_data;
313
314 if (soc_data->nvquirks & NVQUIRK_HAS_PADCALIB)
315 tegra_host->pad_calib_required = true;
316}
317
Lars-Peter Clausenc9155682013-03-13 19:26:05 +0100318static const struct sdhci_ops tegra_sdhci_ops = {
Shawn Guo85d65092011-05-27 23:48:12 +0800319 .get_ro = tegra_sdhci_get_ro,
Shawn Guo85d65092011-05-27 23:48:12 +0800320 .read_w = tegra_sdhci_readw,
321 .write_l = tegra_sdhci_writel,
Lucas Stacha8e326a2015-12-22 19:41:00 +0100322 .set_clock = tegra_sdhci_set_clock,
Russell King2317f562014-04-25 12:57:07 +0100323 .set_bus_width = tegra_sdhci_set_bus_width,
Russell King03231f92014-04-25 12:57:12 +0100324 .reset = tegra_sdhci_reset,
Lucas Stachc3c23842015-12-22 19:41:02 +0100325 .platform_execute_tuning = tegra_sdhci_execute_tuning,
Lucas Stacha8e326a2015-12-22 19:41:00 +0100326 .set_uhs_signaling = tegra_sdhci_set_uhs_signaling,
Lucas Stache5c63d92016-02-29 21:56:25 +0100327 .voltage_switch = tegra_sdhci_voltage_switch,
Lucas Stacha8e326a2015-12-22 19:41:00 +0100328 .get_max_clock = tegra_sdhci_get_max_clock,
Shawn Guo85d65092011-05-27 23:48:12 +0800329};
Olof Johansson03d2bfc2011-01-01 23:52:56 -0500330
Lars-Peter Clausen1db5eeb2013-03-13 19:26:03 +0100331static const struct sdhci_pltfm_data sdhci_tegra20_pdata = {
Shawn Guo85d65092011-05-27 23:48:12 +0800332 .quirks = SDHCI_QUIRK_BROKEN_TIMEOUT_VAL |
333 SDHCI_QUIRK_SINGLE_POWER_WRITE |
334 SDHCI_QUIRK_NO_HISPD_BIT |
Andrew Brestickerf9260352014-05-22 08:55:36 -0700335 SDHCI_QUIRK_BROKEN_ADMA_ZEROLEN_DESC |
336 SDHCI_QUIRK_CAP_CLOCK_BASE_BROKEN,
Shawn Guo85d65092011-05-27 23:48:12 +0800337 .ops = &tegra_sdhci_ops,
338};
339
Thierry Redingd49d19c22015-11-16 10:27:14 +0100340static const struct sdhci_tegra_soc_data soc_data_tegra20 = {
Stephen Warren3e44a1a2012-02-01 16:30:55 -0700341 .pdata = &sdhci_tegra20_pdata,
342 .nvquirks = NVQUIRK_FORCE_SDHCI_SPEC_200 |
343 NVQUIRK_ENABLE_BLOCK_GAP_DET,
344};
Stephen Warren3e44a1a2012-02-01 16:30:55 -0700345
Lars-Peter Clausen1db5eeb2013-03-13 19:26:03 +0100346static const struct sdhci_pltfm_data sdhci_tegra30_pdata = {
Stephen Warren3e44a1a2012-02-01 16:30:55 -0700347 .quirks = SDHCI_QUIRK_BROKEN_TIMEOUT_VAL |
348 SDHCI_QUIRK_DATA_TIMEOUT_USES_SDCLK |
349 SDHCI_QUIRK_SINGLE_POWER_WRITE |
350 SDHCI_QUIRK_NO_HISPD_BIT |
Andrew Brestickerf9260352014-05-22 08:55:36 -0700351 SDHCI_QUIRK_BROKEN_ADMA_ZEROLEN_DESC |
352 SDHCI_QUIRK_CAP_CLOCK_BASE_BROKEN,
Lucas Stacha8e326a2015-12-22 19:41:00 +0100353 .quirks2 = SDHCI_QUIRK2_PRESET_VALUE_BROKEN,
Stephen Warren3e44a1a2012-02-01 16:30:55 -0700354 .ops = &tegra_sdhci_ops,
355};
356
Thierry Redingd49d19c22015-11-16 10:27:14 +0100357static const struct sdhci_tegra_soc_data soc_data_tegra30 = {
Stephen Warren3e44a1a2012-02-01 16:30:55 -0700358 .pdata = &sdhci_tegra30_pdata,
Andrew Bresticker31453512014-05-22 08:55:35 -0700359 .nvquirks = NVQUIRK_ENABLE_SDHCI_SPEC_300 |
Lucas Stach7ad2ed12015-12-22 19:41:03 +0100360 NVQUIRK_ENABLE_SDR50 |
Lucas Stache5c63d92016-02-29 21:56:25 +0100361 NVQUIRK_ENABLE_SDR104 |
362 NVQUIRK_HAS_PADCALIB,
Stephen Warren3e44a1a2012-02-01 16:30:55 -0700363};
Stephen Warren3e44a1a2012-02-01 16:30:55 -0700364
Rhyland Klein01df7ec2015-02-11 12:55:51 -0500365static const struct sdhci_ops tegra114_sdhci_ops = {
366 .get_ro = tegra_sdhci_get_ro,
367 .read_w = tegra_sdhci_readw,
368 .write_w = tegra_sdhci_writew,
369 .write_l = tegra_sdhci_writel,
Lucas Stacha8e326a2015-12-22 19:41:00 +0100370 .set_clock = tegra_sdhci_set_clock,
Rhyland Klein01df7ec2015-02-11 12:55:51 -0500371 .set_bus_width = tegra_sdhci_set_bus_width,
372 .reset = tegra_sdhci_reset,
Lucas Stachc3c23842015-12-22 19:41:02 +0100373 .platform_execute_tuning = tegra_sdhci_execute_tuning,
Lucas Stacha8e326a2015-12-22 19:41:00 +0100374 .set_uhs_signaling = tegra_sdhci_set_uhs_signaling,
Lucas Stache5c63d92016-02-29 21:56:25 +0100375 .voltage_switch = tegra_sdhci_voltage_switch,
Lucas Stacha8e326a2015-12-22 19:41:00 +0100376 .get_max_clock = tegra_sdhci_get_max_clock,
Rhyland Klein01df7ec2015-02-11 12:55:51 -0500377};
378
Lars-Peter Clausen1db5eeb2013-03-13 19:26:03 +0100379static const struct sdhci_pltfm_data sdhci_tegra114_pdata = {
Rhyland Klein5ebf2552013-02-20 13:35:17 -0500380 .quirks = SDHCI_QUIRK_BROKEN_TIMEOUT_VAL |
381 SDHCI_QUIRK_DATA_TIMEOUT_USES_SDCLK |
382 SDHCI_QUIRK_SINGLE_POWER_WRITE |
383 SDHCI_QUIRK_NO_HISPD_BIT |
Andrew Brestickerf9260352014-05-22 08:55:36 -0700384 SDHCI_QUIRK_BROKEN_ADMA_ZEROLEN_DESC |
385 SDHCI_QUIRK_CAP_CLOCK_BASE_BROKEN,
Lucas Stacha8e326a2015-12-22 19:41:00 +0100386 .quirks2 = SDHCI_QUIRK2_PRESET_VALUE_BROKEN,
Rhyland Klein01df7ec2015-02-11 12:55:51 -0500387 .ops = &tegra114_sdhci_ops,
Rhyland Klein5ebf2552013-02-20 13:35:17 -0500388};
389
Thierry Redingd49d19c22015-11-16 10:27:14 +0100390static const struct sdhci_tegra_soc_data soc_data_tegra114 = {
Rhyland Klein5ebf2552013-02-20 13:35:17 -0500391 .pdata = &sdhci_tegra114_pdata,
Jon Hunter7bf037d2016-02-26 09:34:17 +0000392};
393
Thierry Reding4ae12582016-09-01 13:46:17 +0200394static const struct sdhci_pltfm_data sdhci_tegra124_pdata = {
395 .quirks = SDHCI_QUIRK_BROKEN_TIMEOUT_VAL |
396 SDHCI_QUIRK_DATA_TIMEOUT_USES_SDCLK |
397 SDHCI_QUIRK_SINGLE_POWER_WRITE |
398 SDHCI_QUIRK_NO_HISPD_BIT |
399 SDHCI_QUIRK_BROKEN_ADMA_ZEROLEN_DESC |
400 SDHCI_QUIRK_CAP_CLOCK_BASE_BROKEN,
401 .quirks2 = SDHCI_QUIRK2_PRESET_VALUE_BROKEN |
402 /*
403 * The TRM states that the SD/MMC controller found on
404 * Tegra124 can address 34 bits (the maximum supported by
405 * the Tegra memory controller), but tests show that DMA
406 * to or from above 4 GiB doesn't work. This is possibly
407 * caused by missing programming, though it's not obvious
408 * what sequence is required. Mark 64-bit DMA broken for
409 * now to fix this for existing users (e.g. Nyan boards).
410 */
411 SDHCI_QUIRK2_BROKEN_64_BIT_DMA,
412 .ops = &tegra114_sdhci_ops,
413};
414
415static const struct sdhci_tegra_soc_data soc_data_tegra124 = {
416 .pdata = &sdhci_tegra124_pdata,
417};
418
Thierry Redingb5a84ec2015-11-16 10:27:15 +0100419static const struct sdhci_pltfm_data sdhci_tegra210_pdata = {
420 .quirks = SDHCI_QUIRK_BROKEN_TIMEOUT_VAL |
421 SDHCI_QUIRK_DATA_TIMEOUT_USES_SDCLK |
422 SDHCI_QUIRK_SINGLE_POWER_WRITE |
423 SDHCI_QUIRK_NO_HISPD_BIT |
Lucas Stacha8e326a2015-12-22 19:41:00 +0100424 SDHCI_QUIRK_BROKEN_ADMA_ZEROLEN_DESC |
425 SDHCI_QUIRK_CAP_CLOCK_BASE_BROKEN,
426 .quirks2 = SDHCI_QUIRK2_PRESET_VALUE_BROKEN,
Thierry Redingb5a84ec2015-11-16 10:27:15 +0100427 .ops = &tegra114_sdhci_ops,
428};
429
430static const struct sdhci_tegra_soc_data soc_data_tegra210 = {
431 .pdata = &sdhci_tegra210_pdata,
Thierry Redingb5a84ec2015-11-16 10:27:15 +0100432};
433
Bill Pemberton498d83e2012-11-19 13:24:22 -0500434static const struct of_device_id sdhci_tegra_dt_match[] = {
Thierry Redingb5a84ec2015-11-16 10:27:15 +0100435 { .compatible = "nvidia,tegra210-sdhci", .data = &soc_data_tegra210 },
Thierry Reding4ae12582016-09-01 13:46:17 +0200436 { .compatible = "nvidia,tegra124-sdhci", .data = &soc_data_tegra124 },
Rhyland Klein5ebf2552013-02-20 13:35:17 -0500437 { .compatible = "nvidia,tegra114-sdhci", .data = &soc_data_tegra114 },
Stephen Warren3e44a1a2012-02-01 16:30:55 -0700438 { .compatible = "nvidia,tegra30-sdhci", .data = &soc_data_tegra30 },
Stephen Warren3e44a1a2012-02-01 16:30:55 -0700439 { .compatible = "nvidia,tegra20-sdhci", .data = &soc_data_tegra20 },
Grant Likely275173b2011-08-23 12:15:33 -0600440 {}
441};
Arnd Bergmanne4404fa2013-04-23 15:05:57 -0400442MODULE_DEVICE_TABLE(of, sdhci_tegra_dt_match);
Grant Likely275173b2011-08-23 12:15:33 -0600443
Bill Pembertonc3be1ef2012-11-19 13:23:06 -0500444static int sdhci_tegra_probe(struct platform_device *pdev)
Olof Johansson03d2bfc2011-01-01 23:52:56 -0500445{
Stephen Warren3e44a1a2012-02-01 16:30:55 -0700446 const struct of_device_id *match;
447 const struct sdhci_tegra_soc_data *soc_data;
448 struct sdhci_host *host;
Shawn Guo85d65092011-05-27 23:48:12 +0800449 struct sdhci_pltfm_host *pltfm_host;
Stephen Warren3e44a1a2012-02-01 16:30:55 -0700450 struct sdhci_tegra *tegra_host;
Olof Johansson03d2bfc2011-01-01 23:52:56 -0500451 struct clk *clk;
452 int rc;
453
Stephen Warren3e44a1a2012-02-01 16:30:55 -0700454 match = of_match_device(sdhci_tegra_dt_match, &pdev->dev);
Joseph Lob37f9d92012-08-17 15:04:31 +0800455 if (!match)
456 return -EINVAL;
457 soc_data = match->data;
Stephen Warren3e44a1a2012-02-01 16:30:55 -0700458
Jisheng Zhang0734e792016-02-16 21:08:29 +0800459 host = sdhci_pltfm_init(pdev, soc_data->pdata, sizeof(*tegra_host));
Shawn Guo85d65092011-05-27 23:48:12 +0800460 if (IS_ERR(host))
461 return PTR_ERR(host);
Shawn Guo85d65092011-05-27 23:48:12 +0800462 pltfm_host = sdhci_priv(host);
463
Jisheng Zhang0734e792016-02-16 21:08:29 +0800464 tegra_host = sdhci_pltfm_priv(pltfm_host);
Lucas Stacha8e326a2015-12-22 19:41:00 +0100465 tegra_host->ddr_signaling = false;
Lucas Stache5c63d92016-02-29 21:56:25 +0100466 tegra_host->pad_calib_required = false;
Stephen Warren3e44a1a2012-02-01 16:30:55 -0700467 tegra_host->soc_data = soc_data;
Grant Likely275173b2011-08-23 12:15:33 -0600468
Mylene JOSSERAND2391b342015-03-30 23:39:25 +0200469 rc = mmc_of_parse(host->mmc);
Simon Baatz47caa842013-06-09 22:14:16 +0200470 if (rc)
471 goto err_parse_dt;
Stephen Warren0e786102013-02-15 15:07:19 -0700472
Lucas Stach7ad2ed12015-12-22 19:41:03 +0100473 if (tegra_host->soc_data->nvquirks & NVQUIRK_ENABLE_DDR50)
Lucas Stachc3c23842015-12-22 19:41:02 +0100474 host->mmc->caps |= MMC_CAP_1_8V_DDR;
475
Mylene JOSSERAND2391b342015-03-30 23:39:25 +0200476 tegra_host->power_gpio = devm_gpiod_get_optional(&pdev->dev, "power",
477 GPIOD_OUT_HIGH);
478 if (IS_ERR(tegra_host->power_gpio)) {
479 rc = PTR_ERR(tegra_host->power_gpio);
480 goto err_power_req;
Olof Johansson03d2bfc2011-01-01 23:52:56 -0500481 }
482
Kevin Haoe4f79d92015-02-27 15:47:27 +0800483 clk = devm_clk_get(mmc_dev(host->mmc), NULL);
Olof Johansson03d2bfc2011-01-01 23:52:56 -0500484 if (IS_ERR(clk)) {
485 dev_err(mmc_dev(host->mmc), "clk err\n");
486 rc = PTR_ERR(clk);
Shawn Guo85d65092011-05-27 23:48:12 +0800487 goto err_clk_get;
Olof Johansson03d2bfc2011-01-01 23:52:56 -0500488 }
Prashant Gaikwad1e674bc2012-06-05 09:59:37 +0530489 clk_prepare_enable(clk);
Olof Johansson03d2bfc2011-01-01 23:52:56 -0500490 pltfm_host->clk = clk;
491
Shawn Guo85d65092011-05-27 23:48:12 +0800492 rc = sdhci_add_host(host);
493 if (rc)
494 goto err_add_host;
495
Olof Johansson03d2bfc2011-01-01 23:52:56 -0500496 return 0;
497
Shawn Guo85d65092011-05-27 23:48:12 +0800498err_add_host:
Prashant Gaikwad1e674bc2012-06-05 09:59:37 +0530499 clk_disable_unprepare(pltfm_host->clk);
Shawn Guo85d65092011-05-27 23:48:12 +0800500err_clk_get:
Shawn Guo85d65092011-05-27 23:48:12 +0800501err_power_req:
Simon Baatz47caa842013-06-09 22:14:16 +0200502err_parse_dt:
Shawn Guo85d65092011-05-27 23:48:12 +0800503 sdhci_pltfm_free(pdev);
Olof Johansson03d2bfc2011-01-01 23:52:56 -0500504 return rc;
505}
506
Shawn Guo85d65092011-05-27 23:48:12 +0800507static struct platform_driver sdhci_tegra_driver = {
508 .driver = {
509 .name = "sdhci-tegra",
Grant Likely275173b2011-08-23 12:15:33 -0600510 .of_match_table = sdhci_tegra_dt_match,
Ulf Hanssonfa243f62016-07-27 13:07:21 +0200511 .pm = &sdhci_pltfm_pmops,
Shawn Guo85d65092011-05-27 23:48:12 +0800512 },
513 .probe = sdhci_tegra_probe,
Kevin Haocaebcae2015-02-27 15:47:31 +0800514 .remove = sdhci_pltfm_unregister,
Olof Johansson03d2bfc2011-01-01 23:52:56 -0500515};
516
Axel Lind1f81a62011-11-26 12:55:43 +0800517module_platform_driver(sdhci_tegra_driver);
Shawn Guo85d65092011-05-27 23:48:12 +0800518
519MODULE_DESCRIPTION("SDHCI driver for Tegra");
Stephen Warren3e44a1a2012-02-01 16:30:55 -0700520MODULE_AUTHOR("Google, Inc.");
Shawn Guo85d65092011-05-27 23:48:12 +0800521MODULE_LICENSE("GPL v2");