sdhci-s3c.c 21.5 KB
Newer Older
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17
/* linux/drivers/mmc/host/sdhci-s3c.c
 *
 * Copyright 2008 Openmoko Inc.
 * Copyright 2008 Simtec Electronics
 *      Ben Dooks <ben@simtec.co.uk>
 *      http://armlinux.simtec.co.uk/
 *
 * SDHCI (HSMMC) support for Samsung SoC
 *
 * This program is free software; you can redistribute it and/or modify
 * it under the terms of the GNU General Public License version 2 as
 * published by the Free Software Foundation.
 */

#include <linux/delay.h>
#include <linux/dma-mapping.h>
#include <linux/platform_device.h>
18
#include <linux/platform_data/mmc-sdhci-s3c.h>
19
#include <linux/slab.h>
20 21
#include <linux/clk.h>
#include <linux/io.h>
22
#include <linux/gpio.h>
23
#include <linux/module.h>
24 25 26
#include <linux/of.h>
#include <linux/of_gpio.h>
#include <linux/pm.h>
27
#include <linux/pm_runtime.h>
28 29 30

#include <linux/mmc/host.h>

31
#include "sdhci-s3c-regs.h"
32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50
#include "sdhci.h"

#define MAX_BUS_CLK	(4)

/**
 * struct sdhci_s3c - S3C SDHCI instance
 * @host: The SDHCI host created
 * @pdev: The platform device we where created from.
 * @ioarea: The resource created when we claimed the IO area.
 * @pdata: The platform data for this controller.
 * @cur_clk: The index of the current bus clock.
 * @clk_io: The clock for the internal bus interface.
 * @clk_bus: The clocks that are available for the SD/MMC bus clock.
 */
struct sdhci_s3c {
	struct sdhci_host	*host;
	struct platform_device	*pdev;
	struct resource		*ioarea;
	struct s3c_sdhci_platdata *pdata;
51
	int			cur_clk;
52 53
	int			ext_cd_irq;
	int			ext_cd_gpio;
54 55 56

	struct clk		*clk_io;
	struct clk		*clk_bus[MAX_BUS_CLK];
57
	unsigned long		clk_rates[MAX_BUS_CLK];
58 59

	bool			no_divider;
60 61
};

62 63 64 65 66 67 68 69 70 71
/**
 * struct sdhci_s3c_driver_data - S3C SDHCI platform specific driver data
 * @sdhci_quirks: sdhci host specific quirks.
 *
 * Specifies platform specific configuration of sdhci controller.
 * Note: A structure for driver specific platform data is used for future
 * expansion of its usage.
 */
struct sdhci_s3c_drv_data {
	unsigned int	sdhci_quirks;
72
	bool		no_divider;
73 74
};

75 76 77 78 79 80 81 82 83 84 85 86 87 88
static inline struct sdhci_s3c *to_s3c(struct sdhci_host *host)
{
	return sdhci_priv(host);
}

/**
 * sdhci_s3c_get_max_clk - callback to get maximum clock frequency.
 * @host: The SDHCI host instance.
 *
 * Callback to return the maximum clock rate acheivable by the controller.
*/
static unsigned int sdhci_s3c_get_max_clk(struct sdhci_host *host)
{
	struct sdhci_s3c *ourhost = to_s3c(host);
89 90
	unsigned long rate, max = 0;
	int src;
91

92 93
	for (src = 0; src < MAX_BUS_CLK; src++) {
		rate = ourhost->clk_rates[src];
94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112
		if (rate > max)
			max = rate;
	}

	return max;
}

/**
 * sdhci_s3c_consider_clock - consider one the bus clocks for current setting
 * @ourhost: Our SDHCI instance.
 * @src: The source clock index.
 * @wanted: The clock frequency wanted.
 */
static unsigned int sdhci_s3c_consider_clock(struct sdhci_s3c *ourhost,
					     unsigned int src,
					     unsigned int wanted)
{
	unsigned long rate;
	struct clk *clksrc = ourhost->clk_bus[src];
113
	int shift;
114

115
	if (IS_ERR(clksrc))
116 117
		return UINT_MAX;

118
	/*
119 120
	 * If controller uses a non-standard clock division, find the best clock
	 * speed possible with selected clock source and skip the division.
121
	 */
122
	if (ourhost->no_divider) {
123 124 125 126
		rate = clk_round_rate(clksrc, wanted);
		return wanted - rate;
	}

127
	rate = ourhost->clk_rates[src];
128

129
	for (shift = 0; shift <= 8; ++shift) {
130
		if ((rate >> shift) <= wanted)
131 132
			break;
	}
133 134 135 136 137 138 139

	if (shift > 8) {
		dev_dbg(&ourhost->pdev->dev,
			"clk %d: rate %ld, min rate %lu > wanted %u\n",
			src, rate, rate / 256, wanted);
		return UINT_MAX;
	}
140 141

	dev_dbg(&ourhost->pdev->dev, "clk %d: rate %ld, want %d, got %ld\n",
142
		src, rate, wanted, rate >> shift);
143

144
	return wanted - (rate >> shift);
145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163
}

/**
 * sdhci_s3c_set_clock - callback on clock change
 * @host: The SDHCI host being changed
 * @clock: The clock rate being requested.
 *
 * When the card's clock is going to be changed, look at the new frequency
 * and find the best clock source to go with it.
*/
static void sdhci_s3c_set_clock(struct sdhci_host *host, unsigned int clock)
{
	struct sdhci_s3c *ourhost = to_s3c(host);
	unsigned int best = UINT_MAX;
	unsigned int delta;
	int best_src = 0;
	int src;
	u32 ctrl;

164 165
	host->mmc->actual_clock = 0;

166
	/* don't bother if the clock is going off. */
167 168
	if (clock == 0) {
		sdhci_set_clock(host, clock);
169
		return;
170
	}
171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187

	for (src = 0; src < MAX_BUS_CLK; src++) {
		delta = sdhci_s3c_consider_clock(ourhost, src, clock);
		if (delta < best) {
			best = delta;
			best_src = src;
		}
	}

	dev_dbg(&ourhost->pdev->dev,
		"selected source %d, clock %d, delta %d\n",
		 best_src, clock, best);

	/* select the new clock source */
	if (ourhost->cur_clk != best_src) {
		struct clk *clk = ourhost->clk_bus[best_src];

188
		clk_prepare_enable(clk);
189 190 191
		if (ourhost->cur_clk >= 0)
			clk_disable_unprepare(
					ourhost->clk_bus[ourhost->cur_clk]);
192 193

		ourhost->cur_clk = best_src;
194
		host->max_clk = ourhost->clk_rates[best_src];
195 196
	}

197 198 199 200 201 202 203 204
	/* turn clock off to card before changing clock source */
	writew(0, host->ioaddr + SDHCI_CLOCK_CONTROL);

	ctrl = readl(host->ioaddr + S3C_SDHCI_CONTROL2);
	ctrl &= ~S3C_SDHCI_CTRL2_SELBASECLK_MASK;
	ctrl |= best_src << S3C_SDHCI_CTRL2_SELBASECLK_SHIFT;
	writel(ctrl, host->ioaddr + S3C_SDHCI_CONTROL2);

205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221
	/* reprogram default hardware configuration */
	writel(S3C64XX_SDHCI_CONTROL4_DRIVE_9mA,
		host->ioaddr + S3C64XX_SDHCI_CONTROL4);

	ctrl = readl(host->ioaddr + S3C_SDHCI_CONTROL2);
	ctrl |= (S3C64XX_SDHCI_CTRL2_ENSTAASYNCCLR |
		  S3C64XX_SDHCI_CTRL2_ENCMDCNFMSK |
		  S3C_SDHCI_CTRL2_ENFBCLKRX |
		  S3C_SDHCI_CTRL2_DFCNT_NONE |
		  S3C_SDHCI_CTRL2_ENCLKOUTHOLD);
	writel(ctrl, host->ioaddr + S3C_SDHCI_CONTROL2);

	/* reconfigure the controller for new clock rate */
	ctrl = (S3C_SDHCI_CTRL3_FCSEL1 | S3C_SDHCI_CTRL3_FCSEL0);
	if (clock < 25 * 1000000)
		ctrl |= (S3C_SDHCI_CTRL3_FCSEL3 | S3C_SDHCI_CTRL3_FCSEL2);
	writel(ctrl, host->ioaddr + S3C_SDHCI_CONTROL3);
222 223

	sdhci_set_clock(host, clock);
224 225
}

226 227 228 229 230 231 232 233 234 235 236 237
/**
 * sdhci_s3c_get_min_clock - callback to get minimal supported clock value
 * @host: The SDHCI host being queried
 *
 * To init mmc host properly a minimal clock value is needed. For high system
 * bus clock's values the standard formula gives values out of allowed range.
 * The clock still can be set to lower values, if clock source other then
 * system bus is selected.
*/
static unsigned int sdhci_s3c_get_min_clock(struct sdhci_host *host)
{
	struct sdhci_s3c *ourhost = to_s3c(host);
238
	unsigned long rate, min = ULONG_MAX;
239 240 241
	int src;

	for (src = 0; src < MAX_BUS_CLK; src++) {
242 243
		rate = ourhost->clk_rates[src] / 256;
		if (!rate)
244
			continue;
245 246
		if (rate < min)
			min = rate;
247
	}
248

249 250 251
	return min;
}

252 253 254 255
/* sdhci_cmu_get_max_clk - callback to get maximum clock frequency.*/
static unsigned int sdhci_cmu_get_max_clock(struct sdhci_host *host)
{
	struct sdhci_s3c *ourhost = to_s3c(host);
256 257 258 259 260 261 262 263 264 265 266 267 268 269
	unsigned long rate, max = 0;
	int src;

	for (src = 0; src < MAX_BUS_CLK; src++) {
		struct clk *clk;

		clk = ourhost->clk_bus[src];
		if (IS_ERR(clk))
			continue;

		rate = clk_round_rate(clk, ULONG_MAX);
		if (rate > max)
			max = rate;
	}
270

271
	return max;
272 273 274 275 276 277
}

/* sdhci_cmu_get_min_clock - callback to get minimal supported clock value. */
static unsigned int sdhci_cmu_get_min_clock(struct sdhci_host *host)
{
	struct sdhci_s3c *ourhost = to_s3c(host);
278 279
	unsigned long rate, min = ULONG_MAX;
	int src;
280

281 282 283 284 285 286 287 288 289 290 291 292 293
	for (src = 0; src < MAX_BUS_CLK; src++) {
		struct clk *clk;

		clk = ourhost->clk_bus[src];
		if (IS_ERR(clk))
			continue;

		rate = clk_round_rate(clk, 0);
		if (rate < min)
			min = rate;
	}

	return min;
294 295 296 297 298 299
}

/* sdhci_cmu_set_clock - callback on clock change.*/
static void sdhci_cmu_set_clock(struct sdhci_host *host, unsigned int clock)
{
	struct sdhci_s3c *ourhost = to_s3c(host);
300
	struct device *dev = &ourhost->pdev->dev;
301 302
	unsigned long timeout;
	u16 clk = 0;
303

304 305
	host->mmc->actual_clock = 0;

306 307 308
	/* If the clock is going off, set to 0 at clock control register */
	if (clock == 0) {
		sdhci_writew(host, 0, SDHCI_CLOCK_CONTROL);
309
		return;
310
	}
311 312 313 314 315

	sdhci_s3c_set_clock(host, clock);

	clk_set_rate(ourhost->clk_bus[ourhost->cur_clk], clock);

316 317 318 319 320 321 322 323
	clk = SDHCI_CLOCK_INT_EN;
	sdhci_writew(host, clk, SDHCI_CLOCK_CONTROL);

	/* Wait max 20 ms */
	timeout = 20;
	while (!((clk = sdhci_readw(host, SDHCI_CLOCK_CONTROL))
		& SDHCI_CLOCK_INT_STABLE)) {
		if (timeout == 0) {
324 325
			dev_err(dev, "%s: Internal clock never stabilised.\n",
				mmc_hostname(host->mmc));
326 327 328 329 330 331 332 333
			return;
		}
		timeout--;
		mdelay(1);
	}

	clk |= SDHCI_CLOCK_CARD_EN;
	sdhci_writew(host, clk, SDHCI_CLOCK_CONTROL);
334 335
}

336
/**
337
 * sdhci_s3c_set_bus_width - support 8bit buswidth
338 339 340 341
 * @host: The SDHCI host being queried
 * @width: MMC_BUS_WIDTH_ macro for the bus width being requested
 *
 * We have 8-bit width support but is not a v3 controller.
342
 * So we add platform_bus_width() and support 8bit width.
343
 */
344
static void sdhci_s3c_set_bus_width(struct sdhci_host *host, int width)
345 346 347 348 349 350 351 352 353 354 355 356 357 358 359
{
	u8 ctrl;

	ctrl = sdhci_readb(host, SDHCI_HOST_CONTROL);

	switch (width) {
	case MMC_BUS_WIDTH_8:
		ctrl |= SDHCI_CTRL_8BITBUS;
		ctrl &= ~SDHCI_CTRL_4BITBUS;
		break;
	case MMC_BUS_WIDTH_4:
		ctrl |= SDHCI_CTRL_4BITBUS;
		ctrl &= ~SDHCI_CTRL_8BITBUS;
		break;
	default:
360 361
		ctrl &= ~SDHCI_CTRL_4BITBUS;
		ctrl &= ~SDHCI_CTRL_8BITBUS;
362 363 364 365 366 367
		break;
	}

	sdhci_writeb(host, ctrl, SDHCI_HOST_CONTROL);
}

368 369 370
static struct sdhci_ops sdhci_s3c_ops = {
	.get_max_clock		= sdhci_s3c_get_max_clk,
	.set_clock		= sdhci_s3c_set_clock,
371
	.get_min_clock		= sdhci_s3c_get_min_clock,
372
	.set_bus_width		= sdhci_s3c_set_bus_width,
373
	.reset			= sdhci_reset,
374 375
};

376 377 378
static void sdhci_s3c_notify_change(struct platform_device *dev, int state)
{
	struct sdhci_host *host = platform_get_drvdata(dev);
379
#ifdef CONFIG_PM_RUNTIME
380
	struct sdhci_s3c *sc = sdhci_priv(host);
381
#endif
382 383
	unsigned long flags;

384
	if (host) {
385
		spin_lock_irqsave(&host->lock, flags);
386 387
		if (state) {
			dev_dbg(&dev->dev, "card inserted.\n");
388 389 390
#ifdef CONFIG_PM_RUNTIME
			clk_prepare_enable(sc->clk_io);
#endif
391 392 393 394 395 396
			host->flags &= ~SDHCI_DEVICE_DEAD;
			host->quirks |= SDHCI_QUIRK_BROKEN_CARD_DETECTION;
		} else {
			dev_dbg(&dev->dev, "card removed.\n");
			host->flags |= SDHCI_DEVICE_DEAD;
			host->quirks &= ~SDHCI_QUIRK_BROKEN_CARD_DETECTION;
397 398 399
#ifdef CONFIG_PM_RUNTIME
			clk_disable_unprepare(sc->clk_io);
#endif
400
		}
401
		tasklet_schedule(&host->card_tasklet);
402
		spin_unlock_irqrestore(&host->lock, flags);
403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420
	}
}

static irqreturn_t sdhci_s3c_gpio_card_detect_thread(int irq, void *dev_id)
{
	struct sdhci_s3c *sc = dev_id;
	int status = gpio_get_value(sc->ext_cd_gpio);
	if (sc->pdata->ext_cd_gpio_invert)
		status = !status;
	sdhci_s3c_notify_change(sc->pdev, status);
	return IRQ_HANDLED;
}

static void sdhci_s3c_setup_card_detect_gpio(struct sdhci_s3c *sc)
{
	struct s3c_sdhci_platdata *pdata = sc->pdata;
	struct device *dev = &sc->pdev->dev;

421
	if (devm_gpio_request(dev, pdata->ext_cd_gpio, "SDHCI EXT CD") == 0) {
422 423 424 425 426
		sc->ext_cd_gpio = pdata->ext_cd_gpio;
		sc->ext_cd_irq = gpio_to_irq(pdata->ext_cd_gpio);
		if (sc->ext_cd_irq &&
		    request_threaded_irq(sc->ext_cd_irq, NULL,
					 sdhci_s3c_gpio_card_detect_thread,
427 428 429
					 IRQF_TRIGGER_RISING |
					 IRQF_TRIGGER_FALLING |
					 IRQF_ONESHOT,
430 431 432 433 434 435 436 437 438 439 440 441 442 443
					 dev_name(dev), sc) == 0) {
			int status = gpio_get_value(sc->ext_cd_gpio);
			if (pdata->ext_cd_gpio_invert)
				status = !status;
			sdhci_s3c_notify_change(sc->pdev, status);
		} else {
			dev_warn(dev, "cannot request irq for card detect\n");
			sc->ext_cd_irq = 0;
		}
	} else {
		dev_err(dev, "cannot request gpio for card detect\n");
	}
}

444
#ifdef CONFIG_OF
B
Bill Pemberton 已提交
445
static int sdhci_s3c_parse_dt(struct device *dev,
446 447 448 449 450
		struct sdhci_host *host, struct s3c_sdhci_platdata *pdata)
{
	struct device_node *node = dev->of_node;
	struct sdhci_s3c *ourhost = to_s3c(host);
	u32 max_width;
451
	int gpio;
452 453 454 455 456 457 458

	/* if the bus-width property is not specified, assume width as 1 */
	if (of_property_read_u32(node, "bus-width", &max_width))
		max_width = 1;
	pdata->max_width = max_width;

	/* get the card detection method */
459
	if (of_get_property(node, "broken-cd", NULL)) {
460
		pdata->cd_type = S3C_SDHCI_CD_NONE;
461
		return 0;
462 463
	}

464
	if (of_get_property(node, "non-removable", NULL)) {
465
		pdata->cd_type = S3C_SDHCI_CD_PERMANENT;
466
		return 0;
467 468 469 470 471 472 473 474 475
	}

	gpio = of_get_named_gpio(node, "cd-gpios", 0);
	if (gpio_is_valid(gpio)) {
		pdata->cd_type = S3C_SDHCI_CD_GPIO;
		pdata->ext_cd_gpio = gpio;
		ourhost->ext_cd_gpio = -1;
		if (of_get_property(node, "cd-inverted", NULL))
			pdata->ext_cd_gpio_invert = 1;
476
		return 0;
477 478 479
	} else if (gpio != -ENOENT) {
		dev_err(dev, "invalid card detect gpio specified\n");
		return -EINVAL;
480 481
	}

482 483
	/* assuming internal card detect that will be configured by pinctrl */
	pdata->cd_type = S3C_SDHCI_CD_INTERNAL;
484 485 486
	return 0;
}
#else
B
Bill Pemberton 已提交
487
static int sdhci_s3c_parse_dt(struct device *dev,
488 489 490 491 492 493 494 495
		struct sdhci_host *host, struct s3c_sdhci_platdata *pdata)
{
	return -EINVAL;
}
#endif

static const struct of_device_id sdhci_s3c_dt_match[];

496 497 498
static inline struct sdhci_s3c_drv_data *sdhci_s3c_get_driver_data(
			struct platform_device *pdev)
{
499 500 501 502 503 504 505
#ifdef CONFIG_OF
	if (pdev->dev.of_node) {
		const struct of_device_id *match;
		match = of_match_node(sdhci_s3c_dt_match, pdev->dev.of_node);
		return (struct sdhci_s3c_drv_data *)match->data;
	}
#endif
506 507 508 509
	return (struct sdhci_s3c_drv_data *)
			platform_get_device_id(pdev)->driver_data;
}

B
Bill Pemberton 已提交
510
static int sdhci_s3c_probe(struct platform_device *pdev)
511
{
512
	struct s3c_sdhci_platdata *pdata;
513
	struct sdhci_s3c_drv_data *drv_data;
514 515 516 517 518 519
	struct device *dev = &pdev->dev;
	struct sdhci_host *host;
	struct sdhci_s3c *sc;
	struct resource *res;
	int ret, irq, ptr, clks;

520
	if (!pdev->dev.platform_data && !pdev->dev.of_node) {
521 522 523 524 525 526 527 528 529 530 531 532 533 534 535
		dev_err(dev, "no device data specified\n");
		return -ENOENT;
	}

	irq = platform_get_irq(pdev, 0);
	if (irq < 0) {
		dev_err(dev, "no irq specified\n");
		return irq;
	}

	host = sdhci_alloc_host(dev, sizeof(struct sdhci_s3c));
	if (IS_ERR(host)) {
		dev_err(dev, "sdhci_alloc_host() failed\n");
		return PTR_ERR(host);
	}
536
	sc = sdhci_priv(host);
537

538 539 540
	pdata = devm_kzalloc(&pdev->dev, sizeof(*pdata), GFP_KERNEL);
	if (!pdata) {
		ret = -ENOMEM;
541
		goto err_pdata_io_clk;
542 543 544 545 546
	}

	if (pdev->dev.of_node) {
		ret = sdhci_s3c_parse_dt(&pdev->dev, host, pdata);
		if (ret)
547
			goto err_pdata_io_clk;
548 549 550
	} else {
		memcpy(pdata, pdev->dev.platform_data, sizeof(*pdata));
		sc->ext_cd_gpio = -1; /* invalid gpio number */
551 552
	}

553
	drv_data = sdhci_s3c_get_driver_data(pdev);
554 555 556 557

	sc->host = host;
	sc->pdev = pdev;
	sc->pdata = pdata;
558
	sc->cur_clk = -1;
559 560 561

	platform_set_drvdata(pdev, host);

J
Jingoo Han 已提交
562
	sc->clk_io = devm_clk_get(dev, "hsmmc");
563 564 565
	if (IS_ERR(sc->clk_io)) {
		dev_err(dev, "failed to get io clock\n");
		ret = PTR_ERR(sc->clk_io);
566
		goto err_pdata_io_clk;
567 568 569
	}

	/* enable the local io clock and keep it running for the moment. */
570
	clk_prepare_enable(sc->clk_io);
571 572

	for (clks = 0, ptr = 0; ptr < MAX_BUS_CLK; ptr++) {
573
		char name[14];
574

575
		snprintf(name, 14, "mmc_busclk.%d", ptr);
576 577
		sc->clk_bus[ptr] = devm_clk_get(dev, name);
		if (IS_ERR(sc->clk_bus[ptr]))
578 579 580
			continue;

		clks++;
581 582
		sc->clk_rates[ptr] = clk_get_rate(sc->clk_bus[ptr]);

583
		dev_info(dev, "clock source %d: %s (%ld Hz)\n",
584
				ptr, name, sc->clk_rates[ptr]);
585 586 587 588 589 590 591 592
	}

	if (clks == 0) {
		dev_err(dev, "failed to find any bus clocks\n");
		ret = -ENOENT;
		goto err_no_busclks;
	}

593
	res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
594 595 596
	host->ioaddr = devm_ioremap_resource(&pdev->dev, res);
	if (IS_ERR(host->ioaddr)) {
		ret = PTR_ERR(host->ioaddr);
597 598 599 600 601 602 603 604 605 606
		goto err_req_regs;
	}

	/* Ensure we have minimal gpio selected CMD/CLK/Detect */
	if (pdata->cfg_gpio)
		pdata->cfg_gpio(pdev, pdata->max_width);

	host->hw_name = "samsung-hsmmc";
	host->ops = &sdhci_s3c_ops;
	host->quirks = 0;
607
	host->quirks2 = 0;
608 609 610
	host->irq = irq;

	/* Setup quirks for the controller */
611
	host->quirks |= SDHCI_QUIRK_NO_ENDATTR_IN_NOPDESC;
612
	host->quirks |= SDHCI_QUIRK_NO_HISPD_BIT;
613
	if (drv_data) {
614
		host->quirks |= drv_data->sdhci_quirks;
615 616
		sc->no_divider = drv_data->no_divider;
	}
617 618 619 620 621 622 623 624 625 626 627 628 629 630

#ifndef CONFIG_MMC_SDHCI_S3C_DMA

	/* we currently see overruns on errors, so disable the SDMA
	 * support as well. */
	host->quirks |= SDHCI_QUIRK_BROKEN_DMA;

#endif /* CONFIG_MMC_SDHCI_S3C_DMA */

	/* It seems we do not get an DATA transfer complete on non-busy
	 * transfers, not sure if this is a problem with this specific
	 * SDHCI block, or a missing configuration that needs to be set. */
	host->quirks |= SDHCI_QUIRK_NO_BUSY_IRQ;

631 632 633
	/* This host supports the Auto CMD12 */
	host->quirks |= SDHCI_QUIRK_MULTIBLOCK_READ_ACMD12;

634 635 636
	/* Samsung SoCs need BROKEN_ADMA_ZEROLEN_DESC */
	host->quirks |= SDHCI_QUIRK_BROKEN_ADMA_ZEROLEN_DESC;

637 638 639 640 641 642 643
	if (pdata->cd_type == S3C_SDHCI_CD_NONE ||
	    pdata->cd_type == S3C_SDHCI_CD_PERMANENT)
		host->quirks |= SDHCI_QUIRK_BROKEN_CARD_DETECTION;

	if (pdata->cd_type == S3C_SDHCI_CD_PERMANENT)
		host->mmc->caps = MMC_CAP_NONREMOVABLE;

644 645 646 647 648 649 650 651
	switch (pdata->max_width) {
	case 8:
		host->mmc->caps |= MMC_CAP_8_BIT_DATA;
	case 4:
		host->mmc->caps |= MMC_CAP_4_BIT_DATA;
		break;
	}

652 653 654
	if (pdata->pm_caps)
		host->mmc->pm_caps |= pdata->pm_caps;

655 656 657
	host->quirks |= (SDHCI_QUIRK_32BIT_DMA_ADDR |
			 SDHCI_QUIRK_32BIT_DMA_SIZE);

658 659 660
	/* HSMMC on Samsung SoCs uses SDCLK as timeout clock */
	host->quirks |= SDHCI_QUIRK_DATA_TIMEOUT_USES_SDCLK;

661 662 663 664
	/*
	 * If controller does not have internal clock divider,
	 * we can use overriding functions instead of default.
	 */
665
	if (sc->no_divider) {
666 667 668 669 670
		sdhci_s3c_ops.set_clock = sdhci_cmu_set_clock;
		sdhci_s3c_ops.get_min_clock = sdhci_cmu_get_min_clock;
		sdhci_s3c_ops.get_max_clock = sdhci_cmu_get_max_clock;
	}

671 672 673 674
	/* It supports additional host capabilities if needed */
	if (pdata->host_caps)
		host->mmc->caps |= pdata->host_caps;

675 676 677
	if (pdata->host_caps2)
		host->mmc->caps2 |= pdata->host_caps2;

678 679 680 681 682
	pm_runtime_enable(&pdev->dev);
	pm_runtime_set_autosuspend_delay(&pdev->dev, 50);
	pm_runtime_use_autosuspend(&pdev->dev);
	pm_suspend_ignore_children(&pdev->dev, 1);

683 684 685
	ret = sdhci_add_host(host);
	if (ret) {
		dev_err(dev, "sdhci_add_host() failed\n");
686 687
		pm_runtime_forbid(&pdev->dev);
		pm_runtime_get_noresume(&pdev->dev);
688
		goto err_req_regs;
689 690
	}

691 692 693 694 695 696 697 698 699
	/* The following two methods of card detection might call
	   sdhci_s3c_notify_change() immediately, so they can be called
	   only after sdhci_add_host(). Setup errors are ignored. */
	if (pdata->cd_type == S3C_SDHCI_CD_EXTERNAL && pdata->ext_cd_init)
		pdata->ext_cd_init(&sdhci_s3c_notify_change);
	if (pdata->cd_type == S3C_SDHCI_CD_GPIO &&
	    gpio_is_valid(pdata->ext_cd_gpio))
		sdhci_s3c_setup_card_detect_gpio(sc);

700
#ifdef CONFIG_PM_RUNTIME
701 702
	if (pdata->cd_type != S3C_SDHCI_CD_INTERNAL)
		clk_disable_unprepare(sc->clk_io);
703
#endif
704 705 706 707
	return 0;

 err_req_regs:
 err_no_busclks:
708
	clk_disable_unprepare(sc->clk_io);
709

710
 err_pdata_io_clk:
711 712 713 714 715
	sdhci_free_host(host);

	return ret;
}

B
Bill Pemberton 已提交
716
static int sdhci_s3c_remove(struct platform_device *pdev)
717
{
718 719
	struct sdhci_host *host =  platform_get_drvdata(pdev);
	struct sdhci_s3c *sc = sdhci_priv(host);
720
	struct s3c_sdhci_platdata *pdata = sc->pdata;
721

722 723 724 725 726 727
	if (pdata->cd_type == S3C_SDHCI_CD_EXTERNAL && pdata->ext_cd_cleanup)
		pdata->ext_cd_cleanup(&sdhci_s3c_notify_change);

	if (sc->ext_cd_irq)
		free_irq(sc->ext_cd_irq, sc);

728
#ifdef CONFIG_PM_RUNTIME
729 730
	if (pdata->cd_type != S3C_SDHCI_CD_INTERNAL)
		clk_prepare_enable(sc->clk_io);
731
#endif
732 733
	sdhci_remove_host(host, 1);

734
	pm_runtime_dont_use_autosuspend(&pdev->dev);
735 736
	pm_runtime_disable(&pdev->dev);

737
	clk_disable_unprepare(sc->clk_io);
738 739 740

	sdhci_free_host(host);

741 742 743
	return 0;
}

744
#ifdef CONFIG_PM_SLEEP
745
static int sdhci_s3c_suspend(struct device *dev)
746
{
747
	struct sdhci_host *host = dev_get_drvdata(dev);
748

749
	return sdhci_suspend_host(host);
750 751
}

752
static int sdhci_s3c_resume(struct device *dev)
753
{
754
	struct sdhci_host *host = dev_get_drvdata(dev);
755

756
	return sdhci_resume_host(host);
757
}
758
#endif
759

760 761 762 763
#ifdef CONFIG_PM_RUNTIME
static int sdhci_s3c_runtime_suspend(struct device *dev)
{
	struct sdhci_host *host = dev_get_drvdata(dev);
764 765 766 767 768
	struct sdhci_s3c *ourhost = to_s3c(host);
	struct clk *busclk = ourhost->clk_io;
	int ret;

	ret = sdhci_runtime_suspend_host(host);
769

770 771
	if (ourhost->cur_clk >= 0)
		clk_disable_unprepare(ourhost->clk_bus[ourhost->cur_clk]);
772
	clk_disable_unprepare(busclk);
773
	return ret;
774 775 776 777 778
}

static int sdhci_s3c_runtime_resume(struct device *dev)
{
	struct sdhci_host *host = dev_get_drvdata(dev);
779 780 781
	struct sdhci_s3c *ourhost = to_s3c(host);
	struct clk *busclk = ourhost->clk_io;
	int ret;
782

783
	clk_prepare_enable(busclk);
784 785
	if (ourhost->cur_clk >= 0)
		clk_prepare_enable(ourhost->clk_bus[ourhost->cur_clk]);
786 787
	ret = sdhci_runtime_resume_host(host);
	return ret;
788 789 790
}
#endif

791
#ifdef CONFIG_PM
792
static const struct dev_pm_ops sdhci_s3c_pmops = {
793
	SET_SYSTEM_SLEEP_PM_OPS(sdhci_s3c_suspend, sdhci_s3c_resume)
794 795
	SET_RUNTIME_PM_OPS(sdhci_s3c_runtime_suspend, sdhci_s3c_runtime_resume,
			   NULL)
796 797 798 799
};

#define SDHCI_S3C_PMOPS (&sdhci_s3c_pmops)

800
#else
801
#define SDHCI_S3C_PMOPS NULL
802 803
#endif

804 805
#if defined(CONFIG_CPU_EXYNOS4210) || defined(CONFIG_SOC_EXYNOS4212)
static struct sdhci_s3c_drv_data exynos4_sdhci_drv_data = {
806
	.no_divider = true,
807 808 809 810 811 812 813 814 815 816 817 818 819 820 821 822 823 824
};
#define EXYNOS4_SDHCI_DRV_DATA ((kernel_ulong_t)&exynos4_sdhci_drv_data)
#else
#define EXYNOS4_SDHCI_DRV_DATA ((kernel_ulong_t)NULL)
#endif

static struct platform_device_id sdhci_s3c_driver_ids[] = {
	{
		.name		= "s3c-sdhci",
		.driver_data	= (kernel_ulong_t)NULL,
	}, {
		.name		= "exynos4-sdhci",
		.driver_data	= EXYNOS4_SDHCI_DRV_DATA,
	},
	{ }
};
MODULE_DEVICE_TABLE(platform, sdhci_s3c_driver_ids);

825 826 827 828 829 830 831 832 833 834
#ifdef CONFIG_OF
static const struct of_device_id sdhci_s3c_dt_match[] = {
	{ .compatible = "samsung,s3c6410-sdhci", },
	{ .compatible = "samsung,exynos4210-sdhci",
		.data = (void *)EXYNOS4_SDHCI_DRV_DATA },
	{},
};
MODULE_DEVICE_TABLE(of, sdhci_s3c_dt_match);
#endif

835 836
static struct platform_driver sdhci_s3c_driver = {
	.probe		= sdhci_s3c_probe,
B
Bill Pemberton 已提交
837
	.remove		= sdhci_s3c_remove,
838
	.id_table	= sdhci_s3c_driver_ids,
839 840 841
	.driver		= {
		.owner	= THIS_MODULE,
		.name	= "s3c-sdhci",
842
		.of_match_table = of_match_ptr(sdhci_s3c_dt_match),
843
		.pm	= SDHCI_S3C_PMOPS,
844 845 846
	},
};

847
module_platform_driver(sdhci_s3c_driver);
848 849 850 851 852

MODULE_DESCRIPTION("Samsung SDHCI (HSMMC) glue");
MODULE_AUTHOR("Ben Dooks, <ben@simtec.co.uk>");
MODULE_LICENSE("GPL v2");
MODULE_ALIAS("platform:s3c-sdhci");