summaryrefslogtreecommitdiff
path: root/drivers/mmc/host/sdhci-tegra.c
blob: 0d651879af468ac5a87029d3cd83afa948222f9d (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
/*
 * drivers/mmc/host/sdhci-tegra.c
 *
 * SDHCI-compatible driver for NVIDIA Tegra SoCs
 *
 * Copyright (c) 2009, NVIDIA Corporation.
 *
 * This program is free software; you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation; either version 2 of the License, or
 * (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful, but WITHOUT
 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
 * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License for
 * more details.
 *
 * You should have received a copy of the GNU General Public License along
 * with this program; if not, write to the Free Software Foundation, Inc.,
 * 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301, USA.
 */

#include <linux/mmc/host.h>
#include <linux/platform_device.h>
#include <linux/irq.h>
#include <linux/tegra_devices.h>
#include <linux/mmc/card.h>

#include "sdhci.h"
#include "mach/nvrm_linux.h"
#include "mach/io.h"
#include "nvodm_sdio.h"
#include "nvrm_module.h"
#include "nvrm_power.h"
#include "nvrm_interrupt.h"
#include "nvrm_pmu.h"
#include "nvos.h"
#include "nvodm_query_gpio.h"

#define DRIVER_DESC "Nvidia Tegra SDHCI compliant driver"
#define DRIVER_NAME "tegra_sdhci"

struct tegra_sdhci
{
	struct sdhci_host	*sdhost;
	struct platform_device	*pdev;
	NvOdmSdioHandle		hSdioHandle;
	NvU32			ModId;
	NvRmGpioPinHandle	wp_pin;
	NvU32			wp_polarity;
	NvRmGpioPinState	cd_pin;
	NvU32			cd_polarity;
	NvRmGpioInterruptHandle	cd_int;
	NvU32			MaxClock;
	NvBool			SdioControllerClkEnabled;
#ifdef CONFIG_EMBEDDED_MMC_START_OFFSET
	NvU32			StartOffset;
#endif
};

static NvError
tegra_sdhci_set_controller_clk(struct tegra_sdhci *t_sdhci, NvBool enable)
{
	NvError e = NvSuccess;
	if (t_sdhci->SdioControllerClkEnabled != enable) {
		t_sdhci->SdioControllerClkEnabled = enable;
		e = (NvRmPowerModuleClockControl(s_hRmGlobal, t_sdhci->ModId,
				0, enable));
	}
	return e;
}

static int tegra_sdhci_enable_dma(struct sdhci_host *host)
{
	return 0;
}

/*
 *  Return 0 if the card is removed.
 *  -ve if the feature is not supported.
 *  +ve if the card is inserted.
 */
static int tegra_sdhci_detect(struct tegra_sdhci *t_sdhci)
{
	NvRmGpioPinState val;

	if (t_sdhci->cd_pin) {
		NvRmGpioReadPins(s_hGpioGlobal, &(t_sdhci->cd_pin), &val, 1);
		return val == t_sdhci->cd_polarity;
	}

	return -1;
}

static void do_handle_cardetect(void *args)
{
	struct sdhci_host *sdhost = (struct sdhci_host *)args;
	struct tegra_sdhci *t_sdhci = sdhci_priv(sdhost);

	sdhost->card_present = tegra_sdhci_detect(t_sdhci);
	sdhci_card_detect_callback(sdhost);
	NvRmGpioInterruptDone(t_sdhci->cd_int);
}

/*
 * Return 
 * -ve value if not supported.
 * 0 if not write protected.
 * +ve value if write protected.
 */
static int tegra_sdhci_get_ro(struct sdhci_host *host)
{
	struct tegra_sdhci *t_sdhci;
	NvRmGpioPinState val = NvRmGpioPinState_Low;

	t_sdhci = sdhci_priv(host);

	if (tegra_sdhci_detect(t_sdhci) && t_sdhci->wp_pin) {
		NvRmGpioReadPins(s_hGpioGlobal, &(t_sdhci->wp_pin), &val, 1);
		return val == t_sdhci->wp_polarity;
	}

	return -1;
}

#ifdef CONFIG_EMBEDDED_MMC_START_OFFSET
static unsigned int tegra_sdhci_get_StartOffset(struct sdhci_host *host)
{
	struct tegra_sdhci *t_sdhci;

	t_sdhci = sdhci_priv(host);

	return t_sdhci->StartOffset;
}
#endif

static unsigned int tegra_sdhci_get_maxclock(struct sdhci_host *host)
{
	struct tegra_sdhci *t_sdhci;

	t_sdhci = sdhci_priv(host);

	return t_sdhci->MaxClock / 1000;
}

static int tegra_sdhci_set_clock(struct sdhci_host *host,
	unsigned int clock)
{
	NvError err;
	struct tegra_sdhci *t_sdhci;

	t_sdhci = sdhci_priv(host);
	if (clock) {
		NvU32 min, max, target, actual_freq;
		min = 400;
		max = clock / 1000;
		target = clock / 1000;

		if (max < min) max  = min;
		if (target < min) target  = min;

		/* enable clock to sdio controller */
		err = tegra_sdhci_set_controller_clk(t_sdhci, NV_TRUE);
		if (err) goto fail;

		err = NvRmPowerModuleClockConfig(s_hRmGlobal,
			t_sdhci->ModId, 0, min, max, &target, 1,
			&actual_freq, 0);
		if (err) goto fail;
	} else {
		/* disable clock to sdio controller */
		err = tegra_sdhci_set_controller_clk(t_sdhci, NV_FALSE);
		if (err) goto fail;
	}
	return 1; /* return success */
fail:
	pr_err("tegra_sdhci_set_clock failed with error %d\n", err);
	return 0; /* upper layer expects return value 0 in case of failure */
}

static struct sdhci_ops tegra_sdhci_ops = {
	.enable_dma		= tegra_sdhci_enable_dma,
	.get_ro			= tegra_sdhci_get_ro,
	.get_maxclock		= tegra_sdhci_get_maxclock,
	.set_clock		= tegra_sdhci_set_clock,
#ifdef CONFIG_EMBEDDED_MMC_START_OFFSET
	.get_startoffset	= tegra_sdhci_get_StartOffset,
#endif
};

int __init tegra_sdhci_probe(struct platform_device *pdev)
{
	struct sdhci_host *sdhost;
	struct tegra_sdhci *host;
	NvError err1, err2, err3;
	NvU32 irq = 0xffff;
	NvU32 addr;
	NvU32 size;
	NvU32 min, max, target;
	NvU32 ModId;
	int ret = -ENODEV;
#ifdef CONFIG_EMBEDDED_MMC_START_OFFSET
	struct tegra_sdio_platform_data *pdata = pdev->dev.platform_data;
#endif
	NvRmModuleSdmmcInterfaceCaps SdioInterfaceCaps;

	const NvOdmGpioPinInfo *gp_info;
	NvU32 PinCount;

	if (pdev->id == -1)
		return -ENODEV;
	ModId = NVRM_MODULE_ID(NvRmModuleID_Sdio, pdev->id);

	sdhost  = sdhci_alloc_host(&pdev->dev, sizeof(struct tegra_sdhci));
	if (!sdhost) {
		return -ENOMEM;
	}

	host = sdhci_priv(sdhost);

	host->pdev = pdev;
	host->sdhost = sdhost;
	host->ModId = ModId;
	host->SdioControllerClkEnabled = NV_FALSE;
#ifdef CONFIG_EMBEDDED_MMC_START_OFFSET
	if (pdata)
		host->StartOffset = pdata->StartOffset;
#endif

	host->hSdioHandle = NvOdmSdioOpen(pdev->id);
	if (!host->hSdioHandle) {
		goto fail;
	}

	/* Get the GPIOs for the card detect and the write protect */
	gp_info = NvOdmQueryGpioPinMap(NvOdmGpioPinGroup_Sdio,
		pdev->id, &PinCount);

	if (gp_info) {
		switch (PinCount) {
		/* Both write proect pin and card insert pin is specified */
		case 2:
			NvRmGpioAcquirePinHandle(s_hGpioGlobal, gp_info[1].Port,
				gp_info[1].Pin, &host->wp_pin);
			host->wp_polarity = gp_info[1].activeState;
			NvRmGpioConfigPins(s_hGpioGlobal, &host->wp_pin, 1,
				NvRmGpioPinMode_InputData);
				/*fall through*/
			/* only card detect pin is specified */
		case 1:
			NvRmGpioAcquirePinHandle(s_hGpioGlobal, gp_info[0].Port,
				gp_info[0].Pin, &host->cd_pin);
			host->cd_polarity = gp_info[0].activeState;
			NvRmGpioConfigPins(s_hGpioGlobal, &host->cd_pin, 1,
				NvRmGpioPinMode_InputData);
			break;
		default:
			dev_info(&pdev->dev, "Invalid pin count(%d) for SDIO "
				"instance %d\n", PinCount, pdev->id);
		}
	} else {
		dev_info(&pdev->dev, "No GPIO map for SDIO instance %d\n",
			pdev->id);
	}

	irq = NvRmGetIrqForLogicalInterrupt(s_hRmGlobal, ModId, 0);
	NvRmModuleGetBaseAddress(s_hRmGlobal, ModId, &addr, &size);
	if (irq == 0xffff || addr == 0x0 || size == 0) {
		goto fail;
	}

	min = 400;
	max = NvRmFreqMaximum;
	target = NvRmFreqMaximum;
	err1 = NvRmSetModuleTristate(s_hRmGlobal, ModId, NV_FALSE);
	err2 = NvRmPowerModuleClockConfig(s_hRmGlobal, ModId, 0,
		min, max, &target, 1, &host->MaxClock, 0);

	err3 = tegra_sdhci_set_controller_clk(host, NV_TRUE);
	if (err1 || err2 || err3) {
		goto fail;
	}
	NvRmModuleReset(s_hRmGlobal, ModId);
	NvOdmSdioResume(host->hSdioHandle);

	sdhost->hw_name = "tegra";
	sdhost->ops = &tegra_sdhci_ops;
	sdhost->irq = irq;

	/* Set the irq flags to irq valid, which is the default linux behaviour.
	 * For irqs used by Nv* APIs, IRQF_NOAUTOEN is also set */
	set_irq_flags(irq, IRQF_VALID);
	sdhost->ioaddr = IO_ADDRESS(addr);

	sdhost->quirks =
		SDHCI_QUIRK_BROKEN_TIMEOUT_VAL |
		SDHCI_QUIRK_SINGLE_POWER_WRITE |
	SDHCI_QUIRK_ENABLE_INTERRUPT_AT_BLOCK_GAP |
	SDHCI_QUIRK_BROKEN_WRITE_PROTECT |
	SDHCI_QUIRK_PRESENT_STATE_REGISTER_INVALID |
	SDHCI_QUIRK_BROKEN_CTRL_HISPD;

#ifdef CONFIG_ARCH_TEGRA_1x_SOC
	sdhost->quirks |= SDHCI_QUIRK_BROKEN_DMA;
#elif  CONFIG_ARCH_TEGRA_2x_SOC
	sdhost->quirks |= SDHCI_QUIRK_BROKEN_SPEC_VERSION |
		SDHCI_QUIRK_32KB_MAX_ADMA_SIZE;
#else
#error "Not defined for this processor"
#endif

	err1 = NvRmGetModuleInterfaceCapabilities(s_hRmGlobal,
		NVRM_MODULE_ID(NvRmModuleID_Sdio, pdev->id),
		sizeof(NvRmModuleSdmmcInterfaceCaps), &SdioInterfaceCaps);
	if (err1 == NvSuccess && SdioInterfaceCaps.MmcInterfaceWidth >= 8)
		sdhost->data_width = 8;

	ret = sdhci_add_host(sdhost);
	if (ret)
		goto fail;

	platform_set_drvdata(pdev, host);

	if (host->cd_pin) {
		/* Register interrupt handler for */
		err1 = NvRmGpioInterruptRegister(s_hGpioGlobal, s_hRmGlobal,
			host->cd_pin, do_handle_cardetect,
			NvRmGpioPinMode_InputInterruptAny, sdhost,
			&host->cd_int, 5);
		if (err1 != NvSuccess)
			goto fail;
		err1 = NvRmGpioInterruptEnable(host->cd_int);
		if (err1 != NvSuccess)
			goto fail;
	}

	sdhost->card_present = tegra_sdhci_detect(host);

	dev_info(&pdev->dev, "tegra_sdhci_probe: irq(%d) io(0x%x) phys(0x%x)",
		irq, (NvU32)sdhost->ioaddr, addr);

	return  0;

fail:
	dev_err(&pdev->dev, "tegra_sdhci_probe failed\n");

	if (host->hSdioHandle)
		NvOdmSdioClose(host->hSdioHandle);

	if (sdhost)
		sdhci_free_host(sdhost);

	return ret;
}


static int __devexit tegra_sdhci_remove(struct platform_device *pdev)
{
	struct tegra_sdhci *host = dev_get_drvdata(&pdev->dev);

	if (host->hSdioHandle)
		NvOdmSdioClose(host->hSdioHandle);
	(void)NvRmSetModuleTristate(s_hRmGlobal, host->ModId, NV_TRUE);
	(void)tegra_sdhci_set_controller_clk(host, NV_FALSE);

	if (host->wp_pin)
		NvRmGpioReleasePinHandles(s_hGpioGlobal, &host->wp_pin, 1);

	if (host->cd_int)
		NvRmGpioInterruptUnregister(s_hGpioGlobal, s_hRmGlobal,
			host->cd_int);

	sdhci_free_host(host->sdhost);

	return 0;
}

#if defined(CONFIG_PM)

static int tegra_sdhci_suspend(struct platform_device *pdev, pm_message_t state)
{
	int ret = 0;
	struct tegra_sdhci *t_sdhci;

	t_sdhci = platform_get_drvdata(pdev);

	if (t_sdhci->sdhost->card_type != MMC_TYPE_SDIO) {
		ret = sdhci_suspend_host(t_sdhci->sdhost,state);
		if (ret)
			pr_err("sdhci_suspend_host failed with error %d\n", ret);
	}

	return ret;
}

static int tegra_sdhci_resume(struct platform_device *pdev)
{
	int ret = 0;
	struct tegra_sdhci *t_sdhci;

	t_sdhci = platform_get_drvdata(pdev);
	if (t_sdhci->sdhost->card_type != MMC_TYPE_SDIO) {
		/* enable clock to sdio controller */
		ret = tegra_sdhci_set_controller_clk(t_sdhci, NV_TRUE);
		if (ret)
			pr_err("tegra_sdhci_resume:tegra_sdhci_set_clock failed with error %d\n", err);
		ret = sdhci_resume_host(t_sdhci->sdhost);
		if (ret)
			pr_err("sdhci_resume_host failed with error %d\n", ret);
	}

	return ret;
}
#endif

struct platform_driver tegra_sdhci_driver = {
	.probe		= tegra_sdhci_probe,
	.remove		= __devexit_p(tegra_sdhci_remove),
#if defined(CONFIG_PM)
	.suspend = tegra_sdhci_suspend,
	.resume = tegra_sdhci_resume,
#else
	.suspend = NULL,
	.resume = NULL,
#endif
	.driver		= {
		.name	= "tegra-sdhci",
		.owner	= THIS_MODULE,
	},
};

static int __init tegra_sdhci_init(void)
{
	return platform_driver_register(&tegra_sdhci_driver);
}

static void __exit tegra_sdhci_exit(void)
{
	platform_driver_unregister(&tegra_sdhci_driver);
}

module_init(tegra_sdhci_init);
module_exit(tegra_sdhci_exit);

MODULE_LICENSE("GPL");
MODULE_DESCRIPTION(DRIVER_DESC);