Anton Vorontsov | 3085e9c | 2009-03-17 00:14:05 +0300 | [diff] [blame] | 1 | /* |
| 2 | * OpenFirmware bindings for Secure Digital Host Controller Interface. |
| 3 | * |
| 4 | * Copyright (c) 2007 Freescale Semiconductor, Inc. |
| 5 | * Copyright (c) 2009 MontaVista Software, Inc. |
| 6 | * |
| 7 | * Authors: Xiaobo Xie <X.Xie@freescale.com> |
| 8 | * Anton Vorontsov <avorontsov@ru.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 as published by |
| 12 | * the Free Software Foundation; either version 2 of the License, or (at |
| 13 | * your option) any later version. |
| 14 | */ |
| 15 | |
| 16 | #include <linux/module.h> |
| 17 | #include <linux/init.h> |
| 18 | #include <linux/io.h> |
| 19 | #include <linux/interrupt.h> |
| 20 | #include <linux/delay.h> |
| 21 | #include <linux/of.h> |
| 22 | #include <linux/of_platform.h> |
| 23 | #include <linux/mmc/host.h> |
Anton Vorontsov | 8226a21 | 2009-09-22 16:45:15 -0700 | [diff] [blame] | 24 | #include <asm/machdep.h> |
Albert Herranz | 7657c3a | 2009-12-17 15:27:20 -0800 | [diff] [blame] | 25 | #include "sdhci-of.h" |
Anton Vorontsov | 3085e9c | 2009-03-17 00:14:05 +0300 | [diff] [blame] | 26 | #include "sdhci.h" |
| 27 | |
Albert Herranz | 7657c3a | 2009-12-17 15:27:20 -0800 | [diff] [blame] | 28 | #ifdef CONFIG_MMC_SDHCI_BIG_ENDIAN_32BIT_BYTE_SWAPPER |
Anton Vorontsov | 3085e9c | 2009-03-17 00:14:05 +0300 | [diff] [blame] | 29 | |
| 30 | /* |
Albert Herranz | 7657c3a | 2009-12-17 15:27:20 -0800 | [diff] [blame] | 31 | * These accessors are designed for big endian hosts doing I/O to |
| 32 | * little endian controllers incorporating a 32-bit hardware byte swapper. |
Anton Vorontsov | 3085e9c | 2009-03-17 00:14:05 +0300 | [diff] [blame] | 33 | */ |
| 34 | |
Albert Herranz | 7657c3a | 2009-12-17 15:27:20 -0800 | [diff] [blame] | 35 | u32 sdhci_be32bs_readl(struct sdhci_host *host, int reg) |
Anton Vorontsov | 3085e9c | 2009-03-17 00:14:05 +0300 | [diff] [blame] | 36 | { |
| 37 | return in_be32(host->ioaddr + reg); |
| 38 | } |
| 39 | |
Albert Herranz | 7657c3a | 2009-12-17 15:27:20 -0800 | [diff] [blame] | 40 | u16 sdhci_be32bs_readw(struct sdhci_host *host, int reg) |
Anton Vorontsov | 3085e9c | 2009-03-17 00:14:05 +0300 | [diff] [blame] | 41 | { |
Albert Herranz | 7657c3a | 2009-12-17 15:27:20 -0800 | [diff] [blame] | 42 | return in_be16(host->ioaddr + (reg ^ 0x2)); |
Anton Vorontsov | 3085e9c | 2009-03-17 00:14:05 +0300 | [diff] [blame] | 43 | } |
| 44 | |
Albert Herranz | 7657c3a | 2009-12-17 15:27:20 -0800 | [diff] [blame] | 45 | u8 sdhci_be32bs_readb(struct sdhci_host *host, int reg) |
Anton Vorontsov | 3085e9c | 2009-03-17 00:14:05 +0300 | [diff] [blame] | 46 | { |
| 47 | return in_8(host->ioaddr + (reg ^ 0x3)); |
| 48 | } |
| 49 | |
Albert Herranz | 7657c3a | 2009-12-17 15:27:20 -0800 | [diff] [blame] | 50 | void sdhci_be32bs_writel(struct sdhci_host *host, u32 val, int reg) |
Anton Vorontsov | 3085e9c | 2009-03-17 00:14:05 +0300 | [diff] [blame] | 51 | { |
| 52 | out_be32(host->ioaddr + reg, val); |
| 53 | } |
| 54 | |
Albert Herranz | 7657c3a | 2009-12-17 15:27:20 -0800 | [diff] [blame] | 55 | void sdhci_be32bs_writew(struct sdhci_host *host, u16 val, int reg) |
Anton Vorontsov | 3085e9c | 2009-03-17 00:14:05 +0300 | [diff] [blame] | 56 | { |
| 57 | struct sdhci_of_host *of_host = sdhci_priv(host); |
| 58 | int base = reg & ~0x3; |
| 59 | int shift = (reg & 0x2) * 8; |
| 60 | |
| 61 | switch (reg) { |
| 62 | case SDHCI_TRANSFER_MODE: |
| 63 | /* |
| 64 | * Postpone this write, we must do it together with a |
| 65 | * command write that is down below. |
| 66 | */ |
| 67 | of_host->xfer_mode_shadow = val; |
| 68 | return; |
| 69 | case SDHCI_COMMAND: |
Albert Herranz | 7657c3a | 2009-12-17 15:27:20 -0800 | [diff] [blame] | 70 | sdhci_be32bs_writel(host, val << 16 | of_host->xfer_mode_shadow, |
| 71 | SDHCI_TRANSFER_MODE); |
Anton Vorontsov | 3085e9c | 2009-03-17 00:14:05 +0300 | [diff] [blame] | 72 | return; |
Anton Vorontsov | 3085e9c | 2009-03-17 00:14:05 +0300 | [diff] [blame] | 73 | } |
| 74 | clrsetbits_be32(host->ioaddr + base, 0xffff << shift, val << shift); |
| 75 | } |
| 76 | |
Albert Herranz | 7657c3a | 2009-12-17 15:27:20 -0800 | [diff] [blame] | 77 | void sdhci_be32bs_writeb(struct sdhci_host *host, u8 val, int reg) |
Anton Vorontsov | 3085e9c | 2009-03-17 00:14:05 +0300 | [diff] [blame] | 78 | { |
| 79 | int base = reg & ~0x3; |
| 80 | int shift = (reg & 0x3) * 8; |
| 81 | |
| 82 | clrsetbits_be32(host->ioaddr + base , 0xff << shift, val << shift); |
| 83 | } |
Albert Herranz | 7657c3a | 2009-12-17 15:27:20 -0800 | [diff] [blame] | 84 | #endif /* CONFIG_MMC_SDHCI_BIG_ENDIAN_32BIT_BYTE_SWAPPER */ |
Anton Vorontsov | 3085e9c | 2009-03-17 00:14:05 +0300 | [diff] [blame] | 85 | |
| 86 | #ifdef CONFIG_PM |
| 87 | |
| 88 | static int sdhci_of_suspend(struct of_device *ofdev, pm_message_t state) |
| 89 | { |
| 90 | struct sdhci_host *host = dev_get_drvdata(&ofdev->dev); |
| 91 | |
| 92 | return mmc_suspend_host(host->mmc, state); |
| 93 | } |
| 94 | |
| 95 | static int sdhci_of_resume(struct of_device *ofdev) |
| 96 | { |
| 97 | struct sdhci_host *host = dev_get_drvdata(&ofdev->dev); |
| 98 | |
| 99 | return mmc_resume_host(host->mmc); |
| 100 | } |
| 101 | |
| 102 | #else |
| 103 | |
| 104 | #define sdhci_of_suspend NULL |
| 105 | #define sdhci_of_resume NULL |
| 106 | |
| 107 | #endif |
| 108 | |
Anton Vorontsov | 8226a21 | 2009-09-22 16:45:15 -0700 | [diff] [blame] | 109 | static bool __devinit sdhci_of_wp_inverted(struct device_node *np) |
| 110 | { |
| 111 | if (of_get_property(np, "sdhci,wp-inverted", NULL)) |
| 112 | return true; |
| 113 | |
| 114 | /* Old device trees don't have the wp-inverted property. */ |
| 115 | return machine_is(mpc837x_rdb) || machine_is(mpc837x_mds); |
| 116 | } |
| 117 | |
Anton Vorontsov | 3085e9c | 2009-03-17 00:14:05 +0300 | [diff] [blame] | 118 | static int __devinit sdhci_of_probe(struct of_device *ofdev, |
| 119 | const struct of_device_id *match) |
| 120 | { |
| 121 | struct device_node *np = ofdev->node; |
| 122 | struct sdhci_of_data *sdhci_of_data = match->data; |
| 123 | struct sdhci_host *host; |
| 124 | struct sdhci_of_host *of_host; |
| 125 | const u32 *clk; |
| 126 | int size; |
| 127 | int ret; |
| 128 | |
| 129 | if (!of_device_is_available(np)) |
| 130 | return -ENODEV; |
| 131 | |
| 132 | host = sdhci_alloc_host(&ofdev->dev, sizeof(*of_host)); |
Julia Lawall | 2198a64 | 2009-08-06 15:07:41 -0700 | [diff] [blame] | 133 | if (IS_ERR(host)) |
Anton Vorontsov | 3085e9c | 2009-03-17 00:14:05 +0300 | [diff] [blame] | 134 | return -ENOMEM; |
| 135 | |
| 136 | of_host = sdhci_priv(host); |
| 137 | dev_set_drvdata(&ofdev->dev, host); |
| 138 | |
| 139 | host->ioaddr = of_iomap(np, 0); |
| 140 | if (!host->ioaddr) { |
| 141 | ret = -ENOMEM; |
| 142 | goto err_addr_map; |
| 143 | } |
| 144 | |
| 145 | host->irq = irq_of_parse_and_map(np, 0); |
| 146 | if (!host->irq) { |
| 147 | ret = -EINVAL; |
| 148 | goto err_no_irq; |
| 149 | } |
| 150 | |
| 151 | host->hw_name = dev_name(&ofdev->dev); |
| 152 | if (sdhci_of_data) { |
| 153 | host->quirks = sdhci_of_data->quirks; |
| 154 | host->ops = &sdhci_of_data->ops; |
| 155 | } |
| 156 | |
Anton Vorontsov | 5fe23c7 | 2009-06-18 00:14:08 +0400 | [diff] [blame] | 157 | if (of_get_property(np, "sdhci,1-bit-only", NULL)) |
| 158 | host->quirks |= SDHCI_QUIRK_FORCE_1_BIT_DATA; |
| 159 | |
Anton Vorontsov | 8226a21 | 2009-09-22 16:45:15 -0700 | [diff] [blame] | 160 | if (sdhci_of_wp_inverted(np)) |
| 161 | host->quirks |= SDHCI_QUIRK_INVERTED_WRITE_PROTECT; |
| 162 | |
Anton Vorontsov | 3085e9c | 2009-03-17 00:14:05 +0300 | [diff] [blame] | 163 | clk = of_get_property(np, "clock-frequency", &size); |
| 164 | if (clk && size == sizeof(*clk) && *clk) |
| 165 | of_host->clock = *clk; |
| 166 | |
| 167 | ret = sdhci_add_host(host); |
| 168 | if (ret) |
| 169 | goto err_add_host; |
| 170 | |
| 171 | return 0; |
| 172 | |
| 173 | err_add_host: |
| 174 | irq_dispose_mapping(host->irq); |
| 175 | err_no_irq: |
| 176 | iounmap(host->ioaddr); |
| 177 | err_addr_map: |
| 178 | sdhci_free_host(host); |
| 179 | return ret; |
| 180 | } |
| 181 | |
| 182 | static int __devexit sdhci_of_remove(struct of_device *ofdev) |
| 183 | { |
| 184 | struct sdhci_host *host = dev_get_drvdata(&ofdev->dev); |
| 185 | |
| 186 | sdhci_remove_host(host, 0); |
| 187 | sdhci_free_host(host); |
| 188 | irq_dispose_mapping(host->irq); |
| 189 | iounmap(host->ioaddr); |
| 190 | return 0; |
| 191 | } |
| 192 | |
| 193 | static const struct of_device_id sdhci_of_match[] = { |
Albert Herranz | 7657c3a | 2009-12-17 15:27:20 -0800 | [diff] [blame] | 194 | #ifdef CONFIG_MMC_SDHCI_OF_ESDHC |
Anton Vorontsov | 3085e9c | 2009-03-17 00:14:05 +0300 | [diff] [blame] | 195 | { .compatible = "fsl,mpc8379-esdhc", .data = &sdhci_esdhc, }, |
| 196 | { .compatible = "fsl,mpc8536-esdhc", .data = &sdhci_esdhc, }, |
Kumar Gala | 992697e | 2009-05-08 08:52:49 -0500 | [diff] [blame] | 197 | { .compatible = "fsl,esdhc", .data = &sdhci_esdhc, }, |
Albert Herranz | 7657c3a | 2009-12-17 15:27:20 -0800 | [diff] [blame] | 198 | #endif |
Albert Herranz | 1144ab5 | 2009-12-17 15:27:20 -0800 | [diff] [blame] | 199 | #ifdef CONFIG_MMC_SDHCI_OF_HLWD |
| 200 | { .compatible = "nintendo,hollywood-sdhci", .data = &sdhci_hlwd, }, |
| 201 | #endif |
Anton Vorontsov | 3085e9c | 2009-03-17 00:14:05 +0300 | [diff] [blame] | 202 | { .compatible = "generic-sdhci", }, |
| 203 | {}, |
| 204 | }; |
| 205 | MODULE_DEVICE_TABLE(of, sdhci_of_match); |
| 206 | |
| 207 | static struct of_platform_driver sdhci_of_driver = { |
| 208 | .driver.name = "sdhci-of", |
| 209 | .match_table = sdhci_of_match, |
| 210 | .probe = sdhci_of_probe, |
| 211 | .remove = __devexit_p(sdhci_of_remove), |
| 212 | .suspend = sdhci_of_suspend, |
| 213 | .resume = sdhci_of_resume, |
| 214 | }; |
| 215 | |
| 216 | static int __init sdhci_of_init(void) |
| 217 | { |
| 218 | return of_register_platform_driver(&sdhci_of_driver); |
| 219 | } |
| 220 | module_init(sdhci_of_init); |
| 221 | |
| 222 | static void __exit sdhci_of_exit(void) |
| 223 | { |
| 224 | of_unregister_platform_driver(&sdhci_of_driver); |
| 225 | } |
| 226 | module_exit(sdhci_of_exit); |
| 227 | |
| 228 | MODULE_DESCRIPTION("Secure Digital Host Controller Interface OF driver"); |
| 229 | MODULE_AUTHOR("Xiaobo Xie <X.Xie@freescale.com>, " |
| 230 | "Anton Vorontsov <avorontsov@ru.mvista.com>"); |
| 231 | MODULE_LICENSE("GPL"); |