Magnus Damm | a87d563 | 2009-10-02 02:22:09 +0000 | [diff] [blame] | 1 | /* |
| 2 | * SuperH Mobile SDHI |
| 3 | * |
Wolfram Sang | a72e8b1 | 2016-02-15 16:01:48 +0100 | [diff] [blame] | 4 | * Copyright (C) 2016 Sang Engineering, Wolfram Sang |
| 5 | * Copyright (C) 2015-16 Renesas Electronics Corporation |
Magnus Damm | a87d563 | 2009-10-02 02:22:09 +0000 | [diff] [blame] | 6 | * Copyright (C) 2009 Magnus Damm |
| 7 | * |
| 8 | * This program is free software; you can redistribute it and/or modify |
| 9 | * it under the terms of the GNU General Public License version 2 as |
| 10 | * published by the Free Software Foundation. |
| 11 | * |
| 12 | * Based on "Compaq ASIC3 support": |
| 13 | * |
| 14 | * Copyright 2001 Compaq Computer Corporation. |
| 15 | * Copyright 2004-2005 Phil Blundell |
| 16 | * Copyright 2007-2008 OpenedHand Ltd. |
| 17 | * |
| 18 | * Authors: Phil Blundell <pb@handhelds.org>, |
| 19 | * Samuel Ortiz <sameo@openedhand.com> |
| 20 | * |
| 21 | */ |
| 22 | |
| 23 | #include <linux/kernel.h> |
| 24 | #include <linux/clk.h> |
Tejun Heo | 5a0e3ad | 2010-03-24 17:04:11 +0900 | [diff] [blame] | 25 | #include <linux/slab.h> |
Guennadi Liakhovetski | c7bb448 | 2012-05-01 18:03:43 +0200 | [diff] [blame] | 26 | #include <linux/mod_devicetable.h> |
Paul Gortmaker | 88b4767 | 2011-07-03 15:15:51 -0400 | [diff] [blame] | 27 | #include <linux/module.h> |
Guennadi Liakhovetski | 5a00a97 | 2013-02-15 16:13:56 +0100 | [diff] [blame] | 28 | #include <linux/of_device.h> |
Magnus Damm | a87d563 | 2009-10-02 02:22:09 +0000 | [diff] [blame] | 29 | #include <linux/platform_device.h> |
Magnus Damm | 3c49e81 | 2010-02-22 13:37:09 +0900 | [diff] [blame] | 30 | #include <linux/mmc/host.h> |
Magnus Damm | a87d563 | 2009-10-02 02:22:09 +0000 | [diff] [blame] | 31 | #include <linux/mfd/tmio.h> |
Guennadi Liakhovetski | 056676d | 2010-05-19 18:34:16 +0000 | [diff] [blame] | 32 | #include <linux/sh_dma.h> |
Simon Horman | 973ed3a | 2011-06-21 08:00:10 +0900 | [diff] [blame] | 33 | #include <linux/delay.h> |
Wolfram Sang | 057a459 | 2016-04-01 17:44:37 +0200 | [diff] [blame] | 34 | #include <linux/pinctrl/consumer.h> |
| 35 | #include <linux/pinctrl/pinctrl-state.h> |
| 36 | #include <linux/regulator/consumer.h> |
Magnus Damm | a87d563 | 2009-10-02 02:22:09 +0000 | [diff] [blame] | 37 | |
Guennadi Liakhovetski | 42051e8 | 2011-03-14 09:52:33 +0100 | [diff] [blame] | 38 | #include "tmio_mmc.h" |
| 39 | |
Kuninori Morimoto | e3c418f | 2013-11-20 00:31:13 -0800 | [diff] [blame] | 40 | #define EXT_ACC 0xe4 |
| 41 | |
Kuninori Morimoto | 1693525 | 2015-01-13 04:59:43 +0000 | [diff] [blame] | 42 | #define host_to_priv(host) container_of((host)->pdata, struct sh_mobile_sdhi, mmc_data) |
| 43 | |
Guennadi Liakhovetski | 5a00a97 | 2013-02-15 16:13:56 +0100 | [diff] [blame] | 44 | struct sh_mobile_sdhi_of_data { |
| 45 | unsigned long tmio_flags; |
Kuninori Morimoto | b3a5d4c | 2014-02-20 16:54:58 -0800 | [diff] [blame] | 46 | unsigned long capabilities; |
Kuninori Morimoto | 423f6c2 | 2014-02-20 16:55:27 -0800 | [diff] [blame] | 47 | unsigned long capabilities2; |
Kuninori Morimoto | f45394d | 2015-01-13 05:00:07 +0000 | [diff] [blame] | 48 | enum dma_slave_buswidth dma_buswidth; |
Kuninori Morimoto | 384b2cb | 2014-08-24 19:58:48 -0700 | [diff] [blame] | 49 | dma_addr_t dma_rx_offset; |
Wolfram Sang | a72e8b1 | 2016-02-15 16:01:48 +0100 | [diff] [blame] | 50 | unsigned bus_shift; |
Guennadi Liakhovetski | 5a00a97 | 2013-02-15 16:13:56 +0100 | [diff] [blame] | 51 | }; |
| 52 | |
Wolfram Sang | 13bbd8a | 2016-04-26 17:55:22 +0200 | [diff] [blame] | 53 | static const struct sh_mobile_sdhi_of_data of_default_cfg = { |
| 54 | .tmio_flags = TMIO_MMC_HAS_IDLE_WAIT, |
Guennadi Liakhovetski | 5a00a97 | 2013-02-15 16:13:56 +0100 | [diff] [blame] | 55 | }; |
| 56 | |
Kuninori Morimoto | b3a5d4c | 2014-02-20 16:54:58 -0800 | [diff] [blame] | 57 | static const struct sh_mobile_sdhi_of_data of_rcar_gen1_compatible = { |
Shinobu Uehara | da29fe2 | 2014-08-24 20:03:00 -0700 | [diff] [blame] | 58 | .tmio_flags = TMIO_MMC_HAS_IDLE_WAIT | TMIO_MMC_WRPROTECT_DISABLE | |
| 59 | TMIO_MMC_CLK_ACTUAL, |
Kuninori Morimoto | b3a5d4c | 2014-02-20 16:54:58 -0800 | [diff] [blame] | 60 | .capabilities = MMC_CAP_SD_HIGHSPEED | MMC_CAP_SDIO_IRQ, |
| 61 | }; |
| 62 | |
Kuninori Morimoto | 423f6c2 | 2014-02-20 16:55:27 -0800 | [diff] [blame] | 63 | static const struct sh_mobile_sdhi_of_data of_rcar_gen2_compatible = { |
Shinobu Uehara | da29fe2 | 2014-08-24 20:03:00 -0700 | [diff] [blame] | 64 | .tmio_flags = TMIO_MMC_HAS_IDLE_WAIT | TMIO_MMC_WRPROTECT_DISABLE | |
Wolfram Sang | 3d376fb | 2016-05-02 22:25:40 +0200 | [diff] [blame^] | 65 | TMIO_MMC_CLK_ACTUAL | TMIO_MMC_MIN_RCAR2, |
Kuninori Morimoto | 423f6c2 | 2014-02-20 16:55:27 -0800 | [diff] [blame] | 66 | .capabilities = MMC_CAP_SD_HIGHSPEED | MMC_CAP_SDIO_IRQ, |
Kuninori Morimoto | f45394d | 2015-01-13 05:00:07 +0000 | [diff] [blame] | 67 | .dma_buswidth = DMA_SLAVE_BUSWIDTH_4_BYTES, |
Kuninori Morimoto | 384b2cb | 2014-08-24 19:58:48 -0700 | [diff] [blame] | 68 | .dma_rx_offset = 0x2000, |
Kuninori Morimoto | 423f6c2 | 2014-02-20 16:55:27 -0800 | [diff] [blame] | 69 | }; |
| 70 | |
Wolfram Sang | a72e8b1 | 2016-02-15 16:01:48 +0100 | [diff] [blame] | 71 | static const struct sh_mobile_sdhi_of_data of_rcar_gen3_compatible = { |
| 72 | .tmio_flags = TMIO_MMC_HAS_IDLE_WAIT | TMIO_MMC_WRPROTECT_DISABLE | |
Wolfram Sang | 3d376fb | 2016-05-02 22:25:40 +0200 | [diff] [blame^] | 73 | TMIO_MMC_CLK_ACTUAL | TMIO_MMC_MIN_RCAR2, |
Wolfram Sang | a72e8b1 | 2016-02-15 16:01:48 +0100 | [diff] [blame] | 74 | .capabilities = MMC_CAP_SD_HIGHSPEED, |
| 75 | .bus_shift = 2, |
| 76 | }; |
| 77 | |
Kuninori Morimoto | 2772ef3 | 2014-02-20 16:54:44 -0800 | [diff] [blame] | 78 | static const struct of_device_id sh_mobile_sdhi_of_match[] = { |
| 79 | { .compatible = "renesas,sdhi-shmobile" }, |
Wolfram Sang | 13bbd8a | 2016-04-26 17:55:22 +0200 | [diff] [blame] | 80 | { .compatible = "renesas,sdhi-sh73a0", .data = &of_default_cfg, }, |
| 81 | { .compatible = "renesas,sdhi-r8a73a4", .data = &of_default_cfg, }, |
| 82 | { .compatible = "renesas,sdhi-r8a7740", .data = &of_default_cfg, }, |
Kuninori Morimoto | b3a5d4c | 2014-02-20 16:54:58 -0800 | [diff] [blame] | 83 | { .compatible = "renesas,sdhi-r8a7778", .data = &of_rcar_gen1_compatible, }, |
Kuninori Morimoto | 81bbbc7 | 2014-02-20 16:55:12 -0800 | [diff] [blame] | 84 | { .compatible = "renesas,sdhi-r8a7779", .data = &of_rcar_gen1_compatible, }, |
Kuninori Morimoto | 423f6c2 | 2014-02-20 16:55:27 -0800 | [diff] [blame] | 85 | { .compatible = "renesas,sdhi-r8a7790", .data = &of_rcar_gen2_compatible, }, |
Kuninori Morimoto | 81918d2 | 2014-02-20 16:55:40 -0800 | [diff] [blame] | 86 | { .compatible = "renesas,sdhi-r8a7791", .data = &of_rcar_gen2_compatible, }, |
Geert Uytterhoeven | a638640 | 2014-08-28 10:07:19 +0200 | [diff] [blame] | 87 | { .compatible = "renesas,sdhi-r8a7792", .data = &of_rcar_gen2_compatible, }, |
| 88 | { .compatible = "renesas,sdhi-r8a7793", .data = &of_rcar_gen2_compatible, }, |
| 89 | { .compatible = "renesas,sdhi-r8a7794", .data = &of_rcar_gen2_compatible, }, |
Wolfram Sang | a72e8b1 | 2016-02-15 16:01:48 +0100 | [diff] [blame] | 90 | { .compatible = "renesas,sdhi-r8a7795", .data = &of_rcar_gen3_compatible, }, |
Kuninori Morimoto | 2772ef3 | 2014-02-20 16:54:44 -0800 | [diff] [blame] | 91 | {}, |
| 92 | }; |
| 93 | MODULE_DEVICE_TABLE(of, sh_mobile_sdhi_of_match); |
| 94 | |
Magnus Damm | a87d563 | 2009-10-02 02:22:09 +0000 | [diff] [blame] | 95 | struct sh_mobile_sdhi { |
| 96 | struct clk *clk; |
| 97 | struct tmio_mmc_data mmc_data; |
Guennadi Liakhovetski | 056676d | 2010-05-19 18:34:16 +0000 | [diff] [blame] | 98 | struct tmio_mmc_dma dma_priv; |
Wolfram Sang | 057a459 | 2016-04-01 17:44:37 +0200 | [diff] [blame] | 99 | struct pinctrl *pinctrl; |
| 100 | struct pinctrl_state *pins_default, *pins_uhs; |
Magnus Damm | a87d563 | 2009-10-02 02:22:09 +0000 | [diff] [blame] | 101 | }; |
| 102 | |
Kuninori Morimoto | f45394d | 2015-01-13 05:00:07 +0000 | [diff] [blame] | 103 | static void sh_mobile_sdhi_sdbuf_width(struct tmio_mmc_host *host, int width) |
| 104 | { |
| 105 | u32 val; |
| 106 | |
| 107 | /* |
| 108 | * see also |
| 109 | * sh_mobile_sdhi_of_data :: dma_buswidth |
| 110 | */ |
| 111 | switch (sd_ctrl_read16(host, CTL_VERSION)) { |
| 112 | case 0x490C: |
| 113 | val = (width == 32) ? 0x0001 : 0x0000; |
| 114 | break; |
| 115 | case 0xCB0D: |
| 116 | val = (width == 32) ? 0x0000 : 0x0001; |
| 117 | break; |
Wolfram Sang | a72e8b1 | 2016-02-15 16:01:48 +0100 | [diff] [blame] | 118 | case 0xCC10: /* Gen3, SD only */ |
| 119 | case 0xCD10: /* Gen3, SD + MMC */ |
| 120 | if (width == 64) |
| 121 | val = 0x0000; |
| 122 | else if (width == 32) |
| 123 | val = 0x0101; |
| 124 | else |
| 125 | val = 0x0001; |
| 126 | break; |
Kuninori Morimoto | f45394d | 2015-01-13 05:00:07 +0000 | [diff] [blame] | 127 | default: |
| 128 | /* nothing to do */ |
| 129 | return; |
| 130 | } |
| 131 | |
| 132 | sd_ctrl_write16(host, EXT_ACC, val); |
| 133 | } |
| 134 | |
Ben Hutchings | 0ea2821 | 2016-04-01 17:44:31 +0200 | [diff] [blame] | 135 | static int sh_mobile_sdhi_clk_enable(struct tmio_mmc_host *host) |
Guennadi Liakhovetski | 56c4928 | 2012-06-20 19:10:32 +0200 | [diff] [blame] | 136 | { |
Ben Hutchings | 0ea2821 | 2016-04-01 17:44:31 +0200 | [diff] [blame] | 137 | struct mmc_host *mmc = host->mmc; |
Kuninori Morimoto | 1693525 | 2015-01-13 04:59:43 +0000 | [diff] [blame] | 138 | struct sh_mobile_sdhi *priv = host_to_priv(host); |
Laurent Pinchart | 00fb3d2 | 2013-10-28 23:49:25 +0100 | [diff] [blame] | 139 | int ret = clk_prepare_enable(priv->clk); |
Guennadi Liakhovetski | 56c4928 | 2012-06-20 19:10:32 +0200 | [diff] [blame] | 140 | if (ret < 0) |
| 141 | return ret; |
| 142 | |
Ben Hutchings | 2fb5595 | 2016-04-01 17:44:32 +0200 | [diff] [blame] | 143 | /* |
| 144 | * The clock driver may not know what maximum frequency |
| 145 | * actually works, so it should be set with the max-frequency |
| 146 | * property which will already have been read to f_max. If it |
| 147 | * was missing, assume the current frequency is the maximum. |
| 148 | */ |
| 149 | if (!mmc->f_max) |
| 150 | mmc->f_max = clk_get_rate(priv->clk); |
| 151 | |
| 152 | /* |
| 153 | * Minimum frequency is the minimum input clock frequency |
| 154 | * divided by our maximum divider. |
| 155 | */ |
| 156 | mmc->f_min = max(clk_round_rate(priv->clk, 1) / 512, 1L); |
Kuninori Morimoto | f45394d | 2015-01-13 05:00:07 +0000 | [diff] [blame] | 157 | |
| 158 | /* enable 16bit data access on SDBUF as default */ |
| 159 | sh_mobile_sdhi_sdbuf_width(host, 16); |
| 160 | |
Guennadi Liakhovetski | 56c4928 | 2012-06-20 19:10:32 +0200 | [diff] [blame] | 161 | return 0; |
| 162 | } |
| 163 | |
Ben Hutchings | 2fb5595 | 2016-04-01 17:44:32 +0200 | [diff] [blame] | 164 | static unsigned int sh_mobile_sdhi_clk_update(struct tmio_mmc_host *host, |
| 165 | unsigned int new_clock) |
| 166 | { |
| 167 | struct sh_mobile_sdhi *priv = host_to_priv(host); |
Wolfram Sang | 3072ba8 | 2016-05-02 22:25:39 +0200 | [diff] [blame] | 168 | unsigned int freq, diff, best_freq = 0, diff_min = ~0; |
Ben Hutchings | 2fb5595 | 2016-04-01 17:44:32 +0200 | [diff] [blame] | 169 | int i; |
| 170 | |
Ben Hutchings | 2fb5595 | 2016-04-01 17:44:32 +0200 | [diff] [blame] | 171 | /* |
| 172 | * We want the bus clock to be as close as possible to, but no |
| 173 | * greater than, new_clock. As we can divide by 1 << i for |
| 174 | * any i in [0, 9] we want the input clock to be as close as |
| 175 | * possible, but no greater than, new_clock << i. |
| 176 | */ |
| 177 | for (i = min(9, ilog2(UINT_MAX / new_clock)); i >= 0; i--) { |
| 178 | freq = clk_round_rate(priv->clk, new_clock << i); |
| 179 | if (freq > (new_clock << i)) { |
| 180 | /* Too fast; look for a slightly slower option */ |
| 181 | freq = clk_round_rate(priv->clk, |
| 182 | (new_clock << i) / 4 * 3); |
| 183 | if (freq > (new_clock << i)) |
| 184 | continue; |
| 185 | } |
| 186 | |
| 187 | diff = new_clock - (freq >> i); |
| 188 | if (diff <= diff_min) { |
| 189 | best_freq = freq; |
| 190 | diff_min = diff; |
| 191 | } |
| 192 | } |
| 193 | |
| 194 | clk_set_rate(priv->clk, best_freq); |
| 195 | |
| 196 | return best_freq; |
| 197 | } |
| 198 | |
Ben Hutchings | 0ea2821 | 2016-04-01 17:44:31 +0200 | [diff] [blame] | 199 | static void sh_mobile_sdhi_clk_disable(struct tmio_mmc_host *host) |
Guennadi Liakhovetski | 56c4928 | 2012-06-20 19:10:32 +0200 | [diff] [blame] | 200 | { |
Kuninori Morimoto | 1693525 | 2015-01-13 04:59:43 +0000 | [diff] [blame] | 201 | struct sh_mobile_sdhi *priv = host_to_priv(host); |
Ben Hutchings | 0ea2821 | 2016-04-01 17:44:31 +0200 | [diff] [blame] | 202 | |
Laurent Pinchart | 00fb3d2 | 2013-10-28 23:49:25 +0100 | [diff] [blame] | 203 | clk_disable_unprepare(priv->clk); |
Guennadi Liakhovetski | 56c4928 | 2012-06-20 19:10:32 +0200 | [diff] [blame] | 204 | } |
| 205 | |
Wolfram Sang | 057a459 | 2016-04-01 17:44:37 +0200 | [diff] [blame] | 206 | static int sh_mobile_sdhi_start_signal_voltage_switch(struct mmc_host *mmc, |
| 207 | struct mmc_ios *ios) |
| 208 | { |
| 209 | struct tmio_mmc_host *host = mmc_priv(mmc); |
| 210 | struct sh_mobile_sdhi *priv = host_to_priv(host); |
| 211 | struct pinctrl_state *pin_state; |
| 212 | int ret; |
| 213 | |
| 214 | switch (ios->signal_voltage) { |
| 215 | case MMC_SIGNAL_VOLTAGE_330: |
| 216 | pin_state = priv->pins_default; |
| 217 | break; |
| 218 | case MMC_SIGNAL_VOLTAGE_180: |
| 219 | pin_state = priv->pins_uhs; |
| 220 | break; |
| 221 | default: |
| 222 | return -EINVAL; |
| 223 | } |
| 224 | |
| 225 | /* |
| 226 | * If anything is missing, assume signal voltage is fixed at |
| 227 | * 3.3V and succeed/fail accordingly. |
| 228 | */ |
| 229 | if (IS_ERR(priv->pinctrl) || IS_ERR(pin_state)) |
| 230 | return ios->signal_voltage == |
| 231 | MMC_SIGNAL_VOLTAGE_330 ? 0 : -EINVAL; |
| 232 | |
| 233 | ret = mmc_regulator_set_vqmmc(host->mmc, ios); |
| 234 | if (ret) |
| 235 | return ret; |
| 236 | |
Wolfram Sang | 2272c84 | 2016-04-26 17:55:27 +0200 | [diff] [blame] | 237 | return pinctrl_select_state(priv->pinctrl, pin_state); |
Wolfram Sang | 057a459 | 2016-04-01 17:44:37 +0200 | [diff] [blame] | 238 | } |
| 239 | |
Simon Horman | 973ed3a | 2011-06-21 08:00:10 +0900 | [diff] [blame] | 240 | static int sh_mobile_sdhi_wait_idle(struct tmio_mmc_host *host) |
| 241 | { |
| 242 | int timeout = 1000; |
| 243 | |
Wolfram Sang | a21553c | 2016-04-27 18:51:26 +0200 | [diff] [blame] | 244 | while (--timeout && !(sd_ctrl_read16_and_16_as_32(host, CTL_STATUS) |
| 245 | & TMIO_STAT_SCLKDIVEN)) |
Simon Horman | 973ed3a | 2011-06-21 08:00:10 +0900 | [diff] [blame] | 246 | udelay(1); |
| 247 | |
| 248 | if (!timeout) { |
Kuninori Morimoto | 94b110a | 2015-01-13 04:57:22 +0000 | [diff] [blame] | 249 | dev_warn(&host->pdev->dev, "timeout waiting for SD bus idle\n"); |
Simon Horman | 973ed3a | 2011-06-21 08:00:10 +0900 | [diff] [blame] | 250 | return -EBUSY; |
| 251 | } |
| 252 | |
| 253 | return 0; |
| 254 | } |
| 255 | |
| 256 | static int sh_mobile_sdhi_write16_hook(struct tmio_mmc_host *host, int addr) |
| 257 | { |
| 258 | switch (addr) |
| 259 | { |
| 260 | case CTL_SD_CMD: |
| 261 | case CTL_STOP_INTERNAL_ACTION: |
| 262 | case CTL_XFER_BLK_COUNT: |
| 263 | case CTL_SD_CARD_CLK_CTL: |
| 264 | case CTL_SD_XFER_LEN: |
| 265 | case CTL_SD_MEM_CARD_OPT: |
| 266 | case CTL_TRANSACTION_CTL: |
| 267 | case CTL_DMA_ENABLE: |
Shinobu Uehara | ff741cf | 2014-04-25 18:14:17 +0900 | [diff] [blame] | 268 | case EXT_ACC: |
Simon Horman | 973ed3a | 2011-06-21 08:00:10 +0900 | [diff] [blame] | 269 | return sh_mobile_sdhi_wait_idle(host); |
| 270 | } |
| 271 | |
| 272 | return 0; |
| 273 | } |
| 274 | |
Kuninori Morimoto | 8b4efe2 | 2014-09-08 23:45:46 -0700 | [diff] [blame] | 275 | static int sh_mobile_sdhi_multi_io_quirk(struct mmc_card *card, |
| 276 | unsigned int direction, int blk_size) |
| 277 | { |
| 278 | /* |
| 279 | * In Renesas controllers, when performing a |
| 280 | * multiple block read of one or two blocks, |
| 281 | * depending on the timing with which the |
| 282 | * response register is read, the response |
| 283 | * value may not be read properly. |
| 284 | * Use single block read for this HW bug |
| 285 | */ |
| 286 | if ((direction == MMC_DATA_READ) && |
| 287 | blk_size == 2) |
| 288 | return 1; |
| 289 | |
| 290 | return blk_size; |
| 291 | } |
| 292 | |
Kuninori Morimoto | 0c47f6a | 2015-01-13 04:59:56 +0000 | [diff] [blame] | 293 | static void sh_mobile_sdhi_enable_dma(struct tmio_mmc_host *host, bool enable) |
| 294 | { |
| 295 | sd_ctrl_write16(host, CTL_DMA_ENABLE, enable ? 2 : 0); |
Kuninori Morimoto | f45394d | 2015-01-13 05:00:07 +0000 | [diff] [blame] | 296 | |
| 297 | /* enable 32bit access if DMA mode if possibile */ |
| 298 | sh_mobile_sdhi_sdbuf_width(host, enable ? 32 : 16); |
Kuninori Morimoto | 0c47f6a | 2015-01-13 04:59:56 +0000 | [diff] [blame] | 299 | } |
| 300 | |
Bill Pemberton | c3be1ef | 2012-11-19 13:23:06 -0500 | [diff] [blame] | 301 | static int sh_mobile_sdhi_probe(struct platform_device *pdev) |
Magnus Damm | a87d563 | 2009-10-02 02:22:09 +0000 | [diff] [blame] | 302 | { |
Guennadi Liakhovetski | 5a00a97 | 2013-02-15 16:13:56 +0100 | [diff] [blame] | 303 | const struct of_device_id *of_id = |
| 304 | of_match_device(sh_mobile_sdhi_of_match, &pdev->dev); |
Magnus Damm | a87d563 | 2009-10-02 02:22:09 +0000 | [diff] [blame] | 305 | struct sh_mobile_sdhi *priv; |
Guennadi Liakhovetski | 056676d | 2010-05-19 18:34:16 +0000 | [diff] [blame] | 306 | struct tmio_mmc_data *mmc_data; |
Kuninori Morimoto | f33c9d6 | 2015-02-24 02:06:43 +0000 | [diff] [blame] | 307 | struct tmio_mmc_data *mmd = pdev->dev.platform_data; |
Guennadi Liakhovetski | 42051e8 | 2011-03-14 09:52:33 +0100 | [diff] [blame] | 308 | struct tmio_mmc_host *host; |
Kuninori Morimoto | 3b159a6 | 2013-11-20 00:30:55 -0800 | [diff] [blame] | 309 | struct resource *res; |
Simon Horman | d5098cb | 2011-08-26 10:42:39 +0200 | [diff] [blame] | 310 | int irq, ret, i = 0; |
Guennadi Liakhovetski | 87ae7bb | 2013-04-26 17:47:19 +0200 | [diff] [blame] | 311 | struct tmio_mmc_dma *dma_priv; |
Magnus Damm | a87d563 | 2009-10-02 02:22:09 +0000 | [diff] [blame] | 312 | |
Kuninori Morimoto | 3b159a6 | 2013-11-20 00:30:55 -0800 | [diff] [blame] | 313 | res = platform_get_resource(pdev, IORESOURCE_MEM, 0); |
| 314 | if (!res) |
| 315 | return -EINVAL; |
| 316 | |
Guennadi Liakhovetski | ac51b96 | 2013-02-15 16:13:58 +0100 | [diff] [blame] | 317 | priv = devm_kzalloc(&pdev->dev, sizeof(struct sh_mobile_sdhi), GFP_KERNEL); |
Wolfram Sang | 2bf8ab6 | 2016-01-19 11:42:22 +0100 | [diff] [blame] | 318 | if (!priv) |
Magnus Damm | a87d563 | 2009-10-02 02:22:09 +0000 | [diff] [blame] | 319 | return -ENOMEM; |
Magnus Damm | a87d563 | 2009-10-02 02:22:09 +0000 | [diff] [blame] | 320 | |
Guennadi Liakhovetski | 056676d | 2010-05-19 18:34:16 +0000 | [diff] [blame] | 321 | mmc_data = &priv->mmc_data; |
Guennadi Liakhovetski | 87ae7bb | 2013-04-26 17:47:19 +0200 | [diff] [blame] | 322 | dma_priv = &priv->dma_priv; |
Guennadi Liakhovetski | 056676d | 2010-05-19 18:34:16 +0000 | [diff] [blame] | 323 | |
Guennadi Liakhovetski | ac51b96 | 2013-02-15 16:13:58 +0100 | [diff] [blame] | 324 | priv->clk = devm_clk_get(&pdev->dev, NULL); |
Magnus Damm | a87d563 | 2009-10-02 02:22:09 +0000 | [diff] [blame] | 325 | if (IS_ERR(priv->clk)) { |
Magnus Damm | a87d563 | 2009-10-02 02:22:09 +0000 | [diff] [blame] | 326 | ret = PTR_ERR(priv->clk); |
Guennadi Liakhovetski | 56ae1ad | 2012-11-28 10:24:21 +0100 | [diff] [blame] | 327 | dev_err(&pdev->dev, "cannot get clock: %d\n", ret); |
Kuninori Morimoto | 010f4aa | 2015-01-13 04:59:24 +0000 | [diff] [blame] | 328 | goto eprobe; |
Magnus Damm | a87d563 | 2009-10-02 02:22:09 +0000 | [diff] [blame] | 329 | } |
| 330 | |
Wolfram Sang | 057a459 | 2016-04-01 17:44:37 +0200 | [diff] [blame] | 331 | priv->pinctrl = devm_pinctrl_get(&pdev->dev); |
| 332 | if (!IS_ERR(priv->pinctrl)) { |
| 333 | priv->pins_default = pinctrl_lookup_state(priv->pinctrl, |
| 334 | PINCTRL_STATE_DEFAULT); |
| 335 | priv->pins_uhs = pinctrl_lookup_state(priv->pinctrl, |
| 336 | "state_uhs"); |
| 337 | } |
| 338 | |
Kuninori Morimoto | 94b110a | 2015-01-13 04:57:22 +0000 | [diff] [blame] | 339 | host = tmio_mmc_host_alloc(pdev); |
| 340 | if (!host) { |
| 341 | ret = -ENOMEM; |
| 342 | goto eprobe; |
| 343 | } |
| 344 | |
Wolfram Sang | a72e8b1 | 2016-02-15 16:01:48 +0100 | [diff] [blame] | 345 | if (of_id && of_id->data) { |
| 346 | const struct sh_mobile_sdhi_of_data *of_data = of_id->data; |
| 347 | |
| 348 | mmc_data->flags |= of_data->tmio_flags; |
| 349 | mmc_data->capabilities |= of_data->capabilities; |
| 350 | mmc_data->capabilities2 |= of_data->capabilities2; |
| 351 | mmc_data->dma_rx_offset = of_data->dma_rx_offset; |
| 352 | dma_priv->dma_buswidth = of_data->dma_buswidth; |
| 353 | host->bus_shift = of_data->bus_shift; |
| 354 | } |
| 355 | |
Kuninori Morimoto | 7ecc09b | 2015-01-13 04:57:33 +0000 | [diff] [blame] | 356 | host->dma = dma_priv; |
Kuninori Morimoto | dfe9a22 | 2015-01-13 04:57:42 +0000 | [diff] [blame] | 357 | host->write16_hook = sh_mobile_sdhi_write16_hook; |
Kuninori Morimoto | 4fe2ec5 | 2015-01-13 04:57:52 +0000 | [diff] [blame] | 358 | host->clk_enable = sh_mobile_sdhi_clk_enable; |
Ben Hutchings | 2fb5595 | 2016-04-01 17:44:32 +0200 | [diff] [blame] | 359 | host->clk_update = sh_mobile_sdhi_clk_update; |
Kuninori Morimoto | 00452c1 | 2015-01-13 04:58:01 +0000 | [diff] [blame] | 360 | host->clk_disable = sh_mobile_sdhi_clk_disable; |
Kuninori Morimoto | 85c02dd | 2015-01-13 04:58:10 +0000 | [diff] [blame] | 361 | host->multi_io_quirk = sh_mobile_sdhi_multi_io_quirk; |
Wolfram Sang | 057a459 | 2016-04-01 17:44:37 +0200 | [diff] [blame] | 362 | host->start_signal_voltage_switch = sh_mobile_sdhi_start_signal_voltage_switch; |
Wolfram Sang | a72e8b1 | 2016-02-15 16:01:48 +0100 | [diff] [blame] | 363 | |
| 364 | /* Orginally registers were 16 bit apart, could be 32 or 64 nowadays */ |
| 365 | if (!host->bus_shift && resource_size(res) > 0x100) /* old way to determine the shift */ |
Kuninori Morimoto | 95a7dc3 | 2015-01-13 04:59:33 +0000 | [diff] [blame] | 366 | host->bus_shift = 1; |
Kuninori Morimoto | 7ecc09b | 2015-01-13 04:57:33 +0000 | [diff] [blame] | 367 | |
Kuninori Morimoto | 84f11d5 | 2015-02-24 02:07:07 +0000 | [diff] [blame] | 368 | if (mmd) |
Kuninori Morimoto | f33c9d6 | 2015-02-24 02:06:43 +0000 | [diff] [blame] | 369 | *mmc_data = *mmd; |
Kuninori Morimoto | 84f11d5 | 2015-02-24 02:07:07 +0000 | [diff] [blame] | 370 | |
Guennadi Liakhovetski | 87ae7bb | 2013-04-26 17:47:19 +0200 | [diff] [blame] | 371 | dma_priv->filter = shdma_chan_filter; |
Kuninori Morimoto | 0c47f6a | 2015-01-13 04:59:56 +0000 | [diff] [blame] | 372 | dma_priv->enable = sh_mobile_sdhi_enable_dma; |
Guennadi Liakhovetski | 87ae7bb | 2013-04-26 17:47:19 +0200 | [diff] [blame] | 373 | |
Kuninori Morimoto | e471df0 | 2015-01-13 04:58:46 +0000 | [diff] [blame] | 374 | mmc_data->alignment_shift = 1; /* 2-byte alignment */ |
Kuninori Morimoto | f33c9d6 | 2015-02-24 02:06:43 +0000 | [diff] [blame] | 375 | mmc_data->capabilities |= MMC_CAP_MMC_HIGHSPEED; |
Kuninori Morimoto | e471df0 | 2015-01-13 04:58:46 +0000 | [diff] [blame] | 376 | |
Yusuke Goda | f1334fb | 2010-08-30 11:50:19 +0100 | [diff] [blame] | 377 | /* |
| 378 | * All SDHI blocks support 2-byte and larger block sizes in 4-bit |
| 379 | * bus width mode. |
| 380 | */ |
| 381 | mmc_data->flags |= TMIO_MMC_BLKSZ_2BYTES; |
| 382 | |
Arnd Hannemann | 23b6607 | 2010-12-28 23:22:32 +0100 | [diff] [blame] | 383 | /* |
| 384 | * All SDHI blocks support SDIO IRQ signalling. |
| 385 | */ |
| 386 | mmc_data->flags |= TMIO_MMC_SDIO_IRQ; |
| 387 | |
Shinobu Uehara | b8d1196 | 2014-08-24 20:00:25 -0700 | [diff] [blame] | 388 | /* |
| 389 | * All SDHI have CMD12 controll bit |
| 390 | */ |
| 391 | mmc_data->flags |= TMIO_MMC_HAVE_CMD12_CTRL; |
| 392 | |
Shinobu Uehara | 6b98757 | 2014-08-24 20:00:52 -0700 | [diff] [blame] | 393 | /* |
| 394 | * All SDHI need SDIO_INFO1 reserved bit |
| 395 | */ |
| 396 | mmc_data->flags |= TMIO_MMC_SDIO_STATUS_QUIRK; |
| 397 | |
Kuninori Morimoto | 94b110a | 2015-01-13 04:57:22 +0000 | [diff] [blame] | 398 | ret = tmio_mmc_host_probe(host, mmc_data); |
Guennadi Liakhovetski | 42051e8 | 2011-03-14 09:52:33 +0100 | [diff] [blame] | 399 | if (ret < 0) |
Kuninori Morimoto | 94b110a | 2015-01-13 04:57:22 +0000 | [diff] [blame] | 400 | goto efree; |
Magnus Damm | a87d563 | 2009-10-02 02:22:09 +0000 | [diff] [blame] | 401 | |
Wolfram Sang | adcbc94 | 2016-04-26 17:55:23 +0200 | [diff] [blame] | 402 | while (1) { |
| 403 | irq = platform_get_irq(pdev, i); |
| 404 | if (irq < 0) |
| 405 | break; |
| 406 | i++; |
| 407 | ret = devm_request_irq(&pdev->dev, irq, tmio_mmc_irq, 0, |
Magnus Damm | d6a1f86 | 2011-05-06 11:02:45 +0000 | [diff] [blame] | 408 | dev_name(&pdev->dev), host); |
Simon Horman | d5098cb | 2011-08-26 10:42:39 +0200 | [diff] [blame] | 409 | if (ret) |
Guennadi Liakhovetski | ac51b96 | 2013-02-15 16:13:58 +0100 | [diff] [blame] | 410 | goto eirq; |
Magnus Damm | 8e7bfdb | 2011-05-06 11:02:33 +0000 | [diff] [blame] | 411 | } |
Simon Horman | d5098cb | 2011-08-26 10:42:39 +0200 | [diff] [blame] | 412 | |
Wolfram Sang | adcbc94 | 2016-04-26 17:55:23 +0200 | [diff] [blame] | 413 | /* There must be at least one IRQ source */ |
| 414 | if (!i) { |
Simon Horman | d5098cb | 2011-08-26 10:42:39 +0200 | [diff] [blame] | 415 | ret = irq; |
Guennadi Liakhovetski | ac51b96 | 2013-02-15 16:13:58 +0100 | [diff] [blame] | 416 | goto eirq; |
Simon Horman | d5098cb | 2011-08-26 10:42:39 +0200 | [diff] [blame] | 417 | } |
| 418 | |
Ben Hutchings | 2fb5595 | 2016-04-01 17:44:32 +0200 | [diff] [blame] | 419 | dev_info(&pdev->dev, "%s base at 0x%08lx max clock rate %u MHz\n", |
Magnus Damm | 1f7d681 | 2011-05-06 11:02:41 +0000 | [diff] [blame] | 420 | mmc_hostname(host->mmc), (unsigned long) |
Guennadi Liakhovetski | 58126c8 | 2012-02-09 22:57:10 +0100 | [diff] [blame] | 421 | (platform_get_resource(pdev, IORESOURCE_MEM, 0)->start), |
Guennadi Liakhovetski | 369213b | 2012-11-28 10:24:13 +0100 | [diff] [blame] | 422 | host->mmc->f_max / 1000000); |
Magnus Damm | a87d563 | 2009-10-02 02:22:09 +0000 | [diff] [blame] | 423 | |
Guennadi Liakhovetski | 42051e8 | 2011-03-14 09:52:33 +0100 | [diff] [blame] | 424 | return ret; |
Magnus Damm | a87d563 | 2009-10-02 02:22:09 +0000 | [diff] [blame] | 425 | |
Guennadi Liakhovetski | ac51b96 | 2013-02-15 16:13:58 +0100 | [diff] [blame] | 426 | eirq: |
Magnus Damm | 8e7bfdb | 2011-05-06 11:02:33 +0000 | [diff] [blame] | 427 | tmio_mmc_host_remove(host); |
Kuninori Morimoto | 94b110a | 2015-01-13 04:57:22 +0000 | [diff] [blame] | 428 | efree: |
| 429 | tmio_mmc_host_free(host); |
Guennadi Liakhovetski | 42051e8 | 2011-03-14 09:52:33 +0100 | [diff] [blame] | 430 | eprobe: |
Magnus Damm | a87d563 | 2009-10-02 02:22:09 +0000 | [diff] [blame] | 431 | return ret; |
| 432 | } |
| 433 | |
| 434 | static int sh_mobile_sdhi_remove(struct platform_device *pdev) |
| 435 | { |
Guennadi Liakhovetski | 42051e8 | 2011-03-14 09:52:33 +0100 | [diff] [blame] | 436 | struct mmc_host *mmc = platform_get_drvdata(pdev); |
| 437 | struct tmio_mmc_host *host = mmc_priv(mmc); |
Magnus Damm | a87d563 | 2009-10-02 02:22:09 +0000 | [diff] [blame] | 438 | |
Guennadi Liakhovetski | 742a0c7 | 2011-05-27 16:54:05 +0200 | [diff] [blame] | 439 | tmio_mmc_host_remove(host); |
| 440 | |
Magnus Damm | a87d563 | 2009-10-02 02:22:09 +0000 | [diff] [blame] | 441 | return 0; |
| 442 | } |
| 443 | |
Guennadi Liakhovetski | e6ee718 | 2011-05-05 16:13:12 +0000 | [diff] [blame] | 444 | static const struct dev_pm_ops tmio_mmc_dev_pm_ops = { |
Ulf Hansson | 753a688 | 2014-08-25 12:18:26 +0200 | [diff] [blame] | 445 | SET_SYSTEM_SLEEP_PM_OPS(pm_runtime_force_suspend, |
| 446 | pm_runtime_force_resume) |
Rafael J. Wysocki | 6ed23b8 | 2014-12-04 00:34:11 +0100 | [diff] [blame] | 447 | SET_RUNTIME_PM_OPS(tmio_mmc_host_runtime_suspend, |
Ulf Hansson | 4e262d7 | 2013-10-23 14:51:31 +0200 | [diff] [blame] | 448 | tmio_mmc_host_runtime_resume, |
| 449 | NULL) |
Guennadi Liakhovetski | e6ee718 | 2011-05-05 16:13:12 +0000 | [diff] [blame] | 450 | }; |
| 451 | |
Magnus Damm | a87d563 | 2009-10-02 02:22:09 +0000 | [diff] [blame] | 452 | static struct platform_driver sh_mobile_sdhi_driver = { |
| 453 | .driver = { |
| 454 | .name = "sh_mobile_sdhi", |
Guennadi Liakhovetski | e6ee718 | 2011-05-05 16:13:12 +0000 | [diff] [blame] | 455 | .pm = &tmio_mmc_dev_pm_ops, |
Guennadi Liakhovetski | c7bb448 | 2012-05-01 18:03:43 +0200 | [diff] [blame] | 456 | .of_match_table = sh_mobile_sdhi_of_match, |
Magnus Damm | a87d563 | 2009-10-02 02:22:09 +0000 | [diff] [blame] | 457 | }, |
| 458 | .probe = sh_mobile_sdhi_probe, |
Bill Pemberton | 0433c14 | 2012-11-19 13:20:26 -0500 | [diff] [blame] | 459 | .remove = sh_mobile_sdhi_remove, |
Magnus Damm | a87d563 | 2009-10-02 02:22:09 +0000 | [diff] [blame] | 460 | }; |
| 461 | |
Axel Lin | d1f81a6 | 2011-11-26 12:55:43 +0800 | [diff] [blame] | 462 | module_platform_driver(sh_mobile_sdhi_driver); |
Magnus Damm | a87d563 | 2009-10-02 02:22:09 +0000 | [diff] [blame] | 463 | |
| 464 | MODULE_DESCRIPTION("SuperH Mobile SDHI driver"); |
| 465 | MODULE_AUTHOR("Magnus Damm"); |
| 466 | MODULE_LICENSE("GPL v2"); |
Guennadi Liakhovetski | 42051e8 | 2011-03-14 09:52:33 +0100 | [diff] [blame] | 467 | MODULE_ALIAS("platform:sh_mobile_sdhi"); |