Thomas Gleixner | d2912cb | 2019-06-04 10:11:33 +0200 | [diff] [blame] | 1 | // SPDX-License-Identifier: GPL-2.0-only |
Anton Vorontsov | 20b1597b | 2010-08-10 18:01:49 -0700 | [diff] [blame] | 2 | /* |
| 3 | * SDHCI support for CNS3xxx SoC |
| 4 | * |
| 5 | * Copyright 2008 Cavium Networks |
| 6 | * Copyright 2010 MontaVista Software, LLC. |
| 7 | * |
| 8 | * Authors: Scott Shu |
| 9 | * Anton Vorontsov <avorontsov@mvista.com> |
Anton Vorontsov | 20b1597b | 2010-08-10 18:01:49 -0700 | [diff] [blame] | 10 | */ |
| 11 | |
| 12 | #include <linux/delay.h> |
| 13 | #include <linux/device.h> |
| 14 | #include <linux/mmc/host.h> |
Chris Ball | 7833c66 | 2011-11-11 19:54:53 -0500 | [diff] [blame] | 15 | #include <linux/module.h> |
Anton Vorontsov | 20b1597b | 2010-08-10 18:01:49 -0700 | [diff] [blame] | 16 | #include "sdhci-pltfm.h" |
| 17 | |
| 18 | static unsigned int sdhci_cns3xxx_get_max_clk(struct sdhci_host *host) |
| 19 | { |
| 20 | return 150000000; |
| 21 | } |
| 22 | |
| 23 | static void sdhci_cns3xxx_set_clock(struct sdhci_host *host, unsigned int clock) |
| 24 | { |
| 25 | struct device *dev = mmc_dev(host->mmc); |
| 26 | int div = 1; |
| 27 | u16 clk; |
| 28 | unsigned long timeout; |
| 29 | |
Russell King | 1650d0c | 2014-04-25 12:58:50 +0100 | [diff] [blame] | 30 | host->mmc->actual_clock = 0; |
| 31 | |
Anton Vorontsov | 20b1597b | 2010-08-10 18:01:49 -0700 | [diff] [blame] | 32 | sdhci_writew(host, 0, SDHCI_CLOCK_CONTROL); |
| 33 | |
| 34 | if (clock == 0) |
Russell King | 373073e | 2014-04-25 12:58:45 +0100 | [diff] [blame] | 35 | return; |
Anton Vorontsov | 20b1597b | 2010-08-10 18:01:49 -0700 | [diff] [blame] | 36 | |
| 37 | while (host->max_clk / div > clock) { |
| 38 | /* |
| 39 | * On CNS3xxx divider grows linearly up to 4, and then |
| 40 | * exponentially up to 256. |
| 41 | */ |
| 42 | if (div < 4) |
| 43 | div += 1; |
| 44 | else if (div < 256) |
| 45 | div *= 2; |
| 46 | else |
| 47 | break; |
| 48 | } |
| 49 | |
| 50 | dev_dbg(dev, "desired SD clock: %d, actual: %d\n", |
| 51 | clock, host->max_clk / div); |
| 52 | |
| 53 | /* Divide by 3 is special. */ |
| 54 | if (div != 3) |
| 55 | div >>= 1; |
| 56 | |
| 57 | clk = div << SDHCI_DIVIDER_SHIFT; |
| 58 | clk |= SDHCI_CLOCK_INT_EN; |
| 59 | sdhci_writew(host, clk, SDHCI_CLOCK_CONTROL); |
| 60 | |
| 61 | timeout = 20; |
| 62 | while (!((clk = sdhci_readw(host, SDHCI_CLOCK_CONTROL)) |
| 63 | & SDHCI_CLOCK_INT_STABLE)) { |
| 64 | if (timeout == 0) { |
| 65 | dev_warn(dev, "clock is unstable"); |
| 66 | break; |
| 67 | } |
| 68 | timeout--; |
| 69 | mdelay(1); |
| 70 | } |
| 71 | |
| 72 | clk |= SDHCI_CLOCK_CARD_EN; |
| 73 | sdhci_writew(host, clk, SDHCI_CLOCK_CONTROL); |
Anton Vorontsov | 20b1597b | 2010-08-10 18:01:49 -0700 | [diff] [blame] | 74 | } |
| 75 | |
Lars-Peter Clausen | c915568 | 2013-03-13 19:26:05 +0100 | [diff] [blame] | 76 | static const struct sdhci_ops sdhci_cns3xxx_ops = { |
Anton Vorontsov | 20b1597b | 2010-08-10 18:01:49 -0700 | [diff] [blame] | 77 | .get_max_clock = sdhci_cns3xxx_get_max_clk, |
| 78 | .set_clock = sdhci_cns3xxx_set_clock, |
Russell King | 2317f56 | 2014-04-25 12:57:07 +0100 | [diff] [blame] | 79 | .set_bus_width = sdhci_set_bus_width, |
Russell King | 03231f9 | 2014-04-25 12:57:12 +0100 | [diff] [blame] | 80 | .reset = sdhci_reset, |
Russell King | 96d7b78 | 2014-04-25 12:59:26 +0100 | [diff] [blame] | 81 | .set_uhs_signaling = sdhci_set_uhs_signaling, |
Anton Vorontsov | 20b1597b | 2010-08-10 18:01:49 -0700 | [diff] [blame] | 82 | }; |
| 83 | |
Lars-Peter Clausen | 1db5eeb | 2013-03-13 19:26:03 +0100 | [diff] [blame] | 84 | static const struct sdhci_pltfm_data sdhci_cns3xxx_pdata = { |
Anton Vorontsov | 20b1597b | 2010-08-10 18:01:49 -0700 | [diff] [blame] | 85 | .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 | |
Russell King | 1771059 | 2014-04-25 12:58:55 +0100 | [diff] [blame] | 90 | SDHCI_QUIRK_BROKEN_TIMEOUT_VAL, |
Anton Vorontsov | 20b1597b | 2010-08-10 18:01:49 -0700 | [diff] [blame] | 91 | }; |
Shawn Guo | 85d6509 | 2011-05-27 23:48:12 +0800 | [diff] [blame] | 92 | |
Bill Pemberton | c3be1ef | 2012-11-19 13:23:06 -0500 | [diff] [blame] | 93 | static int sdhci_cns3xxx_probe(struct platform_device *pdev) |
Shawn Guo | 85d6509 | 2011-05-27 23:48:12 +0800 | [diff] [blame] | 94 | { |
Christian Daudt | 0e74823 | 2013-05-29 13:50:05 -0700 | [diff] [blame] | 95 | return sdhci_pltfm_register(pdev, &sdhci_cns3xxx_pdata, 0); |
Shawn Guo | 85d6509 | 2011-05-27 23:48:12 +0800 | [diff] [blame] | 96 | } |
| 97 | |
Shawn Guo | 85d6509 | 2011-05-27 23:48:12 +0800 | [diff] [blame] | 98 | static struct platform_driver sdhci_cns3xxx_driver = { |
| 99 | .driver = { |
| 100 | .name = "sdhci-cns3xxx", |
Ulf Hansson | fa243f6 | 2016-07-27 13:07:21 +0200 | [diff] [blame] | 101 | .pm = &sdhci_pltfm_pmops, |
Shawn Guo | 85d6509 | 2011-05-27 23:48:12 +0800 | [diff] [blame] | 102 | }, |
| 103 | .probe = sdhci_cns3xxx_probe, |
Kevin Hao | caebcae | 2015-02-27 15:47:31 +0800 | [diff] [blame] | 104 | .remove = sdhci_pltfm_unregister, |
Shawn Guo | 85d6509 | 2011-05-27 23:48:12 +0800 | [diff] [blame] | 105 | }; |
| 106 | |
Axel Lin | d1f81a6 | 2011-11-26 12:55:43 +0800 | [diff] [blame] | 107 | module_platform_driver(sdhci_cns3xxx_driver); |
Shawn Guo | 85d6509 | 2011-05-27 23:48:12 +0800 | [diff] [blame] | 108 | |
| 109 | MODULE_DESCRIPTION("SDHCI driver for CNS3xxx"); |
| 110 | MODULE_AUTHOR("Scott Shu, " |
| 111 | "Anton Vorontsov <avorontsov@mvista.com>"); |
| 112 | MODULE_LICENSE("GPL v2"); |