Anton Vorontsov | 20b1597b | 2010-08-10 18:01:49 -0700 | [diff] [blame] | 1 | /* |
| 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 Ball | 7833c66 | 2011-11-11 19:54:53 -0500 | [diff] [blame] | 18 | #include <linux/module.h> |
Anton Vorontsov | 20b1597b | 2010-08-10 18:01:49 -0700 | [diff] [blame] | 19 | #include "sdhci-pltfm.h" |
| 20 | |
| 21 | static unsigned int sdhci_cns3xxx_get_max_clk(struct sdhci_host *host) |
| 22 | { |
| 23 | return 150000000; |
| 24 | } |
| 25 | |
| 26 | static 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 | |
Russell King | 1650d0c | 2014-04-25 12:58:50 +0100 | [diff] [blame] | 33 | host->mmc->actual_clock = 0; |
| 34 | |
Anton Vorontsov | 20b1597b | 2010-08-10 18:01:49 -0700 | [diff] [blame] | 35 | sdhci_writew(host, 0, SDHCI_CLOCK_CONTROL); |
| 36 | |
| 37 | if (clock == 0) |
Russell King | 373073e | 2014-04-25 12:58:45 +0100 | [diff] [blame] | 38 | return; |
Anton Vorontsov | 20b1597b | 2010-08-10 18:01:49 -0700 | [diff] [blame] | 39 | |
| 40 | while (host->max_clk / div > clock) { |
| 41 | /* |
| 42 | * On CNS3xxx divider grows linearly up to 4, and then |
| 43 | * exponentially up to 256. |
| 44 | */ |
| 45 | if (div < 4) |
| 46 | div += 1; |
| 47 | else if (div < 256) |
| 48 | div *= 2; |
| 49 | else |
| 50 | break; |
| 51 | } |
| 52 | |
| 53 | dev_dbg(dev, "desired SD clock: %d, actual: %d\n", |
| 54 | clock, host->max_clk / div); |
| 55 | |
| 56 | /* Divide by 3 is special. */ |
| 57 | if (div != 3) |
| 58 | div >>= 1; |
| 59 | |
| 60 | clk = div << SDHCI_DIVIDER_SHIFT; |
| 61 | clk |= SDHCI_CLOCK_INT_EN; |
| 62 | sdhci_writew(host, clk, SDHCI_CLOCK_CONTROL); |
| 63 | |
| 64 | timeout = 20; |
| 65 | while (!((clk = sdhci_readw(host, SDHCI_CLOCK_CONTROL)) |
| 66 | & SDHCI_CLOCK_INT_STABLE)) { |
| 67 | if (timeout == 0) { |
| 68 | dev_warn(dev, "clock is unstable"); |
| 69 | break; |
| 70 | } |
| 71 | timeout--; |
| 72 | mdelay(1); |
| 73 | } |
| 74 | |
| 75 | clk |= SDHCI_CLOCK_CARD_EN; |
| 76 | sdhci_writew(host, clk, SDHCI_CLOCK_CONTROL); |
Anton Vorontsov | 20b1597b | 2010-08-10 18:01:49 -0700 | [diff] [blame] | 77 | } |
| 78 | |
Lars-Peter Clausen | c915568 | 2013-03-13 19:26:05 +0100 | [diff] [blame] | 79 | static const struct sdhci_ops sdhci_cns3xxx_ops = { |
Anton Vorontsov | 20b1597b | 2010-08-10 18:01:49 -0700 | [diff] [blame] | 80 | .get_max_clock = sdhci_cns3xxx_get_max_clk, |
| 81 | .set_clock = sdhci_cns3xxx_set_clock, |
Russell King | 2317f56 | 2014-04-25 12:57:07 +0100 | [diff] [blame] | 82 | .set_bus_width = sdhci_set_bus_width, |
Russell King | 03231f9 | 2014-04-25 12:57:12 +0100 | [diff] [blame] | 83 | .reset = sdhci_reset, |
Anton Vorontsov | 20b1597b | 2010-08-10 18:01:49 -0700 | [diff] [blame] | 84 | }; |
| 85 | |
Lars-Peter Clausen | 1db5eeb | 2013-03-13 19:26:03 +0100 | [diff] [blame] | 86 | static const struct sdhci_pltfm_data sdhci_cns3xxx_pdata = { |
Anton Vorontsov | 20b1597b | 2010-08-10 18:01:49 -0700 | [diff] [blame] | 87 | .ops = &sdhci_cns3xxx_ops, |
| 88 | .quirks = SDHCI_QUIRK_BROKEN_DMA | |
| 89 | SDHCI_QUIRK_DATA_TIMEOUT_USES_SDCLK | |
| 90 | SDHCI_QUIRK_INVERTED_WRITE_PROTECT | |
| 91 | SDHCI_QUIRK_CAP_CLOCK_BASE_BROKEN | |
Russell King | 1771059 | 2014-04-25 12:58:55 +0100 | [diff] [blame^] | 92 | SDHCI_QUIRK_BROKEN_TIMEOUT_VAL, |
Anton Vorontsov | 20b1597b | 2010-08-10 18:01:49 -0700 | [diff] [blame] | 93 | }; |
Shawn Guo | 85d6509 | 2011-05-27 23:48:12 +0800 | [diff] [blame] | 94 | |
Bill Pemberton | c3be1ef | 2012-11-19 13:23:06 -0500 | [diff] [blame] | 95 | static int sdhci_cns3xxx_probe(struct platform_device *pdev) |
Shawn Guo | 85d6509 | 2011-05-27 23:48:12 +0800 | [diff] [blame] | 96 | { |
Christian Daudt | 0e74823 | 2013-05-29 13:50:05 -0700 | [diff] [blame] | 97 | return sdhci_pltfm_register(pdev, &sdhci_cns3xxx_pdata, 0); |
Shawn Guo | 85d6509 | 2011-05-27 23:48:12 +0800 | [diff] [blame] | 98 | } |
| 99 | |
Bill Pemberton | 6e0ee71 | 2012-11-19 13:26:03 -0500 | [diff] [blame] | 100 | static int sdhci_cns3xxx_remove(struct platform_device *pdev) |
Shawn Guo | 85d6509 | 2011-05-27 23:48:12 +0800 | [diff] [blame] | 101 | { |
| 102 | return sdhci_pltfm_unregister(pdev); |
| 103 | } |
| 104 | |
| 105 | static struct platform_driver sdhci_cns3xxx_driver = { |
| 106 | .driver = { |
| 107 | .name = "sdhci-cns3xxx", |
| 108 | .owner = THIS_MODULE, |
Manuel Lauss | 29495aa | 2011-11-03 11:09:45 +0100 | [diff] [blame] | 109 | .pm = SDHCI_PLTFM_PMOPS, |
Shawn Guo | 85d6509 | 2011-05-27 23:48:12 +0800 | [diff] [blame] | 110 | }, |
| 111 | .probe = sdhci_cns3xxx_probe, |
Bill Pemberton | 0433c14 | 2012-11-19 13:20:26 -0500 | [diff] [blame] | 112 | .remove = sdhci_cns3xxx_remove, |
Shawn Guo | 85d6509 | 2011-05-27 23:48:12 +0800 | [diff] [blame] | 113 | }; |
| 114 | |
Axel Lin | d1f81a6 | 2011-11-26 12:55:43 +0800 | [diff] [blame] | 115 | module_platform_driver(sdhci_cns3xxx_driver); |
Shawn Guo | 85d6509 | 2011-05-27 23:48:12 +0800 | [diff] [blame] | 116 | |
| 117 | MODULE_DESCRIPTION("SDHCI driver for CNS3xxx"); |
| 118 | MODULE_AUTHOR("Scott Shu, " |
| 119 | "Anton Vorontsov <avorontsov@mvista.com>"); |
| 120 | MODULE_LICENSE("GPL v2"); |