blob: f11fb908ad7868aa0b2ddd3a77bfde96bb9b0d6d [file] [log] [blame]
Anton Vorontsov20b1597b2010-08-10 18:01:49 -07001/*
2 * SDHCI support for CNS3xxx SoC
3 *
4 * Copyright 2008 Cavium Networks
5 * Copyright 2010 MontaVista Software, LLC.
6 *
7 * Authors: Scott Shu
8 * Anton Vorontsov <avorontsov@mvista.com>
9 *
10 * This program is free software; you can redistribute it and/or modify
11 * it under the terms of the GNU General Public License version 2 as
12 * published by the Free Software Foundation.
13 */
14
15#include <linux/delay.h>
16#include <linux/device.h>
17#include <linux/mmc/host.h>
Chris Ball7833c662011-11-11 19:54:53 -050018#include <linux/module.h>
Anton Vorontsov20b1597b2010-08-10 18:01:49 -070019#include "sdhci-pltfm.h"
20
21static unsigned int sdhci_cns3xxx_get_max_clk(struct sdhci_host *host)
22{
23 return 150000000;
24}
25
26static void sdhci_cns3xxx_set_clock(struct sdhci_host *host, unsigned int clock)
27{
28 struct device *dev = mmc_dev(host->mmc);
29 int div = 1;
30 u16 clk;
31 unsigned long timeout;
32
Anton Vorontsov20b1597b2010-08-10 18:01:49 -070033 sdhci_writew(host, 0, SDHCI_CLOCK_CONTROL);
34
35 if (clock == 0)
Russell King373073e2014-04-25 12:58:45 +010036 return;
Anton Vorontsov20b1597b2010-08-10 18:01:49 -070037
38 while (host->max_clk / div > clock) {
39 /*
40 * On CNS3xxx divider grows linearly up to 4, and then
41 * exponentially up to 256.
42 */
43 if (div < 4)
44 div += 1;
45 else if (div < 256)
46 div *= 2;
47 else
48 break;
49 }
50
51 dev_dbg(dev, "desired SD clock: %d, actual: %d\n",
52 clock, host->max_clk / div);
53
54 /* Divide by 3 is special. */
55 if (div != 3)
56 div >>= 1;
57
58 clk = div << SDHCI_DIVIDER_SHIFT;
59 clk |= SDHCI_CLOCK_INT_EN;
60 sdhci_writew(host, clk, SDHCI_CLOCK_CONTROL);
61
62 timeout = 20;
63 while (!((clk = sdhci_readw(host, SDHCI_CLOCK_CONTROL))
64 & SDHCI_CLOCK_INT_STABLE)) {
65 if (timeout == 0) {
66 dev_warn(dev, "clock is unstable");
67 break;
68 }
69 timeout--;
70 mdelay(1);
71 }
72
73 clk |= SDHCI_CLOCK_CARD_EN;
74 sdhci_writew(host, clk, SDHCI_CLOCK_CONTROL);
Anton Vorontsov20b1597b2010-08-10 18:01:49 -070075}
76
Lars-Peter Clausenc9155682013-03-13 19:26:05 +010077static const struct sdhci_ops sdhci_cns3xxx_ops = {
Anton Vorontsov20b1597b2010-08-10 18:01:49 -070078 .get_max_clock = sdhci_cns3xxx_get_max_clk,
79 .set_clock = sdhci_cns3xxx_set_clock,
Russell King2317f562014-04-25 12:57:07 +010080 .set_bus_width = sdhci_set_bus_width,
Russell King03231f92014-04-25 12:57:12 +010081 .reset = sdhci_reset,
Anton Vorontsov20b1597b2010-08-10 18:01:49 -070082};
83
Lars-Peter Clausen1db5eeb2013-03-13 19:26:03 +010084static const struct sdhci_pltfm_data sdhci_cns3xxx_pdata = {
Anton Vorontsov20b1597b2010-08-10 18:01:49 -070085 .ops = &sdhci_cns3xxx_ops,
86 .quirks = SDHCI_QUIRK_BROKEN_DMA |
87 SDHCI_QUIRK_DATA_TIMEOUT_USES_SDCLK |
88 SDHCI_QUIRK_INVERTED_WRITE_PROTECT |
89 SDHCI_QUIRK_CAP_CLOCK_BASE_BROKEN |
90 SDHCI_QUIRK_BROKEN_TIMEOUT_VAL |
91 SDHCI_QUIRK_NONSTANDARD_CLOCK,
92};
Shawn Guo85d65092011-05-27 23:48:12 +080093
Bill Pembertonc3be1ef2012-11-19 13:23:06 -050094static int sdhci_cns3xxx_probe(struct platform_device *pdev)
Shawn Guo85d65092011-05-27 23:48:12 +080095{
Christian Daudt0e748232013-05-29 13:50:05 -070096 return sdhci_pltfm_register(pdev, &sdhci_cns3xxx_pdata, 0);
Shawn Guo85d65092011-05-27 23:48:12 +080097}
98
Bill Pemberton6e0ee712012-11-19 13:26:03 -050099static int sdhci_cns3xxx_remove(struct platform_device *pdev)
Shawn Guo85d65092011-05-27 23:48:12 +0800100{
101 return sdhci_pltfm_unregister(pdev);
102}
103
104static struct platform_driver sdhci_cns3xxx_driver = {
105 .driver = {
106 .name = "sdhci-cns3xxx",
107 .owner = THIS_MODULE,
Manuel Lauss29495aa2011-11-03 11:09:45 +0100108 .pm = SDHCI_PLTFM_PMOPS,
Shawn Guo85d65092011-05-27 23:48:12 +0800109 },
110 .probe = sdhci_cns3xxx_probe,
Bill Pemberton0433c142012-11-19 13:20:26 -0500111 .remove = sdhci_cns3xxx_remove,
Shawn Guo85d65092011-05-27 23:48:12 +0800112};
113
Axel Lind1f81a62011-11-26 12:55:43 +0800114module_platform_driver(sdhci_cns3xxx_driver);
Shawn Guo85d65092011-05-27 23:48:12 +0800115
116MODULE_DESCRIPTION("SDHCI driver for CNS3xxx");
117MODULE_AUTHOR("Scott Shu, "
118 "Anton Vorontsov <avorontsov@mvista.com>");
119MODULE_LICENSE("GPL v2");