1*2874c5fdSThomas Gleixner // SPDX-License-Identifier: GPL-2.0-or-later 2ed80a13bSCarlo Caione /* 3ed80a13bSCarlo Caione * meson-mx-sdio.c - Meson6, Meson8 and Meson8b SDIO/MMC Host Controller 4ed80a13bSCarlo Caione * 5ed80a13bSCarlo Caione * Copyright (C) 2015 Endless Mobile, Inc. 6ed80a13bSCarlo Caione * Author: Carlo Caione <carlo@endlessm.com> 7ed80a13bSCarlo Caione * Copyright (C) 2017 Martin Blumenstingl <martin.blumenstingl@googlemail.com> 8ed80a13bSCarlo Caione */ 9ed80a13bSCarlo Caione 10ed80a13bSCarlo Caione #include <linux/bitfield.h> 11ed80a13bSCarlo Caione #include <linux/clk.h> 12ed80a13bSCarlo Caione #include <linux/clk-provider.h> 13ed80a13bSCarlo Caione #include <linux/delay.h> 14ed80a13bSCarlo Caione #include <linux/device.h> 15ed80a13bSCarlo Caione #include <linux/dma-mapping.h> 16ed80a13bSCarlo Caione #include <linux/module.h> 17ed80a13bSCarlo Caione #include <linux/interrupt.h> 1862e59c4eSStephen Boyd #include <linux/io.h> 19ed80a13bSCarlo Caione #include <linux/ioport.h> 20ed80a13bSCarlo Caione #include <linux/platform_device.h> 21ed80a13bSCarlo Caione #include <linux/of_platform.h> 22ed80a13bSCarlo Caione #include <linux/timer.h> 23ed80a13bSCarlo Caione #include <linux/types.h> 24ed80a13bSCarlo Caione 25ed80a13bSCarlo Caione #include <linux/mmc/host.h> 26ed80a13bSCarlo Caione #include <linux/mmc/mmc.h> 27ed80a13bSCarlo Caione #include <linux/mmc/sdio.h> 28ed80a13bSCarlo Caione #include <linux/mmc/slot-gpio.h> 29ed80a13bSCarlo Caione 30ed80a13bSCarlo Caione #define MESON_MX_SDIO_ARGU 0x00 31ed80a13bSCarlo Caione 32ed80a13bSCarlo Caione #define MESON_MX_SDIO_SEND 0x04 33ed80a13bSCarlo Caione #define MESON_MX_SDIO_SEND_COMMAND_INDEX_MASK GENMASK(7, 0) 34ed80a13bSCarlo Caione #define MESON_MX_SDIO_SEND_CMD_RESP_BITS_MASK GENMASK(15, 8) 35ed80a13bSCarlo Caione #define MESON_MX_SDIO_SEND_RESP_WITHOUT_CRC7 BIT(16) 36ed80a13bSCarlo Caione #define MESON_MX_SDIO_SEND_RESP_HAS_DATA BIT(17) 37ed80a13bSCarlo Caione #define MESON_MX_SDIO_SEND_RESP_CRC7_FROM_8 BIT(18) 38ed80a13bSCarlo Caione #define MESON_MX_SDIO_SEND_CHECK_DAT0_BUSY BIT(19) 39ed80a13bSCarlo Caione #define MESON_MX_SDIO_SEND_DATA BIT(20) 40ed80a13bSCarlo Caione #define MESON_MX_SDIO_SEND_USE_INT_WINDOW BIT(21) 41ed80a13bSCarlo Caione #define MESON_MX_SDIO_SEND_REPEAT_PACKAGE_TIMES_MASK GENMASK(31, 24) 42ed80a13bSCarlo Caione 43ed80a13bSCarlo Caione #define MESON_MX_SDIO_CONF 0x08 44ed80a13bSCarlo Caione #define MESON_MX_SDIO_CONF_CMD_CLK_DIV_SHIFT 0 45ed80a13bSCarlo Caione #define MESON_MX_SDIO_CONF_CMD_CLK_DIV_WIDTH 10 46ed80a13bSCarlo Caione #define MESON_MX_SDIO_CONF_CMD_DISABLE_CRC BIT(10) 47ed80a13bSCarlo Caione #define MESON_MX_SDIO_CONF_CMD_OUT_AT_POSITIVE_EDGE BIT(11) 48ed80a13bSCarlo Caione #define MESON_MX_SDIO_CONF_CMD_ARGUMENT_BITS_MASK GENMASK(17, 12) 49ed80a13bSCarlo Caione #define MESON_MX_SDIO_CONF_RESP_LATCH_AT_NEGATIVE_EDGE BIT(18) 50ed80a13bSCarlo Caione #define MESON_MX_SDIO_CONF_DATA_LATCH_AT_NEGATIVE_EDGE BIT(19) 51ed80a13bSCarlo Caione #define MESON_MX_SDIO_CONF_BUS_WIDTH BIT(20) 52ed80a13bSCarlo Caione #define MESON_MX_SDIO_CONF_M_ENDIAN_MASK GENMASK(22, 21) 53ed80a13bSCarlo Caione #define MESON_MX_SDIO_CONF_WRITE_NWR_MASK GENMASK(28, 23) 54ed80a13bSCarlo Caione #define MESON_MX_SDIO_CONF_WRITE_CRC_OK_STATUS_MASK GENMASK(31, 29) 55ed80a13bSCarlo Caione 56ed80a13bSCarlo Caione #define MESON_MX_SDIO_IRQS 0x0c 57ed80a13bSCarlo Caione #define MESON_MX_SDIO_IRQS_STATUS_STATE_MACHINE_MASK GENMASK(3, 0) 58ed80a13bSCarlo Caione #define MESON_MX_SDIO_IRQS_CMD_BUSY BIT(4) 59ed80a13bSCarlo Caione #define MESON_MX_SDIO_IRQS_RESP_CRC7_OK BIT(5) 60ed80a13bSCarlo Caione #define MESON_MX_SDIO_IRQS_DATA_READ_CRC16_OK BIT(6) 61ed80a13bSCarlo Caione #define MESON_MX_SDIO_IRQS_DATA_WRITE_CRC16_OK BIT(7) 62ed80a13bSCarlo Caione #define MESON_MX_SDIO_IRQS_IF_INT BIT(8) 63ed80a13bSCarlo Caione #define MESON_MX_SDIO_IRQS_CMD_INT BIT(9) 64ed80a13bSCarlo Caione #define MESON_MX_SDIO_IRQS_STATUS_INFO_MASK GENMASK(15, 12) 65ed80a13bSCarlo Caione #define MESON_MX_SDIO_IRQS_TIMING_OUT_INT BIT(16) 66ed80a13bSCarlo Caione #define MESON_MX_SDIO_IRQS_AMRISC_TIMING_OUT_INT_EN BIT(17) 67ed80a13bSCarlo Caione #define MESON_MX_SDIO_IRQS_ARC_TIMING_OUT_INT_EN BIT(18) 68ed80a13bSCarlo Caione #define MESON_MX_SDIO_IRQS_TIMING_OUT_COUNT_MASK GENMASK(31, 19) 69ed80a13bSCarlo Caione 70ed80a13bSCarlo Caione #define MESON_MX_SDIO_IRQC 0x10 71ed80a13bSCarlo Caione #define MESON_MX_SDIO_IRQC_ARC_IF_INT_EN BIT(3) 72ed80a13bSCarlo Caione #define MESON_MX_SDIO_IRQC_ARC_CMD_INT_EN BIT(4) 73ed80a13bSCarlo Caione #define MESON_MX_SDIO_IRQC_IF_CONFIG_MASK GENMASK(7, 6) 74ed80a13bSCarlo Caione #define MESON_MX_SDIO_IRQC_FORCE_DATA_CLK BIT(8) 75ed80a13bSCarlo Caione #define MESON_MX_SDIO_IRQC_FORCE_DATA_CMD BIT(9) 76ed80a13bSCarlo Caione #define MESON_MX_SDIO_IRQC_FORCE_DATA_DAT_MASK GENMASK(10, 13) 77ed80a13bSCarlo Caione #define MESON_MX_SDIO_IRQC_SOFT_RESET BIT(15) 78ed80a13bSCarlo Caione #define MESON_MX_SDIO_IRQC_FORCE_HALT BIT(30) 79ed80a13bSCarlo Caione #define MESON_MX_SDIO_IRQC_HALT_HOLE BIT(31) 80ed80a13bSCarlo Caione 81ed80a13bSCarlo Caione #define MESON_MX_SDIO_MULT 0x14 82ed80a13bSCarlo Caione #define MESON_MX_SDIO_MULT_PORT_SEL_MASK GENMASK(1, 0) 83ed80a13bSCarlo Caione #define MESON_MX_SDIO_MULT_MEMORY_STICK_ENABLE BIT(2) 84ed80a13bSCarlo Caione #define MESON_MX_SDIO_MULT_MEMORY_STICK_SCLK_ALWAYS BIT(3) 85ed80a13bSCarlo Caione #define MESON_MX_SDIO_MULT_STREAM_ENABLE BIT(4) 86ed80a13bSCarlo Caione #define MESON_MX_SDIO_MULT_STREAM_8BITS_MODE BIT(5) 87ed80a13bSCarlo Caione #define MESON_MX_SDIO_MULT_WR_RD_OUT_INDEX BIT(8) 88ed80a13bSCarlo Caione #define MESON_MX_SDIO_MULT_DAT0_DAT1_SWAPPED BIT(10) 89ed80a13bSCarlo Caione #define MESON_MX_SDIO_MULT_DAT1_DAT0_SWAPPED BIT(11) 90ed80a13bSCarlo Caione #define MESON_MX_SDIO_MULT_RESP_READ_INDEX_MASK GENMASK(15, 12) 91ed80a13bSCarlo Caione 92ed80a13bSCarlo Caione #define MESON_MX_SDIO_ADDR 0x18 93ed80a13bSCarlo Caione 94ed80a13bSCarlo Caione #define MESON_MX_SDIO_EXT 0x1c 95ed80a13bSCarlo Caione #define MESON_MX_SDIO_EXT_DATA_RW_NUMBER_MASK GENMASK(29, 16) 96ed80a13bSCarlo Caione 97ed80a13bSCarlo Caione #define MESON_MX_SDIO_BOUNCE_REQ_SIZE (128 * 1024) 98ed80a13bSCarlo Caione #define MESON_MX_SDIO_RESPONSE_CRC16_BITS (16 - 1) 99ed80a13bSCarlo Caione #define MESON_MX_SDIO_MAX_SLOTS 3 100ed80a13bSCarlo Caione 101ed80a13bSCarlo Caione struct meson_mx_mmc_host { 102ed80a13bSCarlo Caione struct device *controller_dev; 103ed80a13bSCarlo Caione 104ed80a13bSCarlo Caione struct clk *parent_clk; 105ed80a13bSCarlo Caione struct clk *core_clk; 106ed80a13bSCarlo Caione struct clk_divider cfg_div; 107ed80a13bSCarlo Caione struct clk *cfg_div_clk; 108ed80a13bSCarlo Caione struct clk_fixed_factor fixed_factor; 109ed80a13bSCarlo Caione struct clk *fixed_factor_clk; 110ed80a13bSCarlo Caione 111ed80a13bSCarlo Caione void __iomem *base; 112ed80a13bSCarlo Caione int irq; 113ed80a13bSCarlo Caione spinlock_t irq_lock; 114ed80a13bSCarlo Caione 115ed80a13bSCarlo Caione struct timer_list cmd_timeout; 116ed80a13bSCarlo Caione 117ed80a13bSCarlo Caione unsigned int slot_id; 118ed80a13bSCarlo Caione struct mmc_host *mmc; 119ed80a13bSCarlo Caione 120ed80a13bSCarlo Caione struct mmc_request *mrq; 121ed80a13bSCarlo Caione struct mmc_command *cmd; 122ed80a13bSCarlo Caione int error; 123ed80a13bSCarlo Caione }; 124ed80a13bSCarlo Caione 125ed80a13bSCarlo Caione static void meson_mx_mmc_mask_bits(struct mmc_host *mmc, char reg, u32 mask, 126ed80a13bSCarlo Caione u32 val) 127ed80a13bSCarlo Caione { 128ed80a13bSCarlo Caione struct meson_mx_mmc_host *host = mmc_priv(mmc); 129ed80a13bSCarlo Caione u32 regval; 130ed80a13bSCarlo Caione 131ed80a13bSCarlo Caione regval = readl(host->base + reg); 132ed80a13bSCarlo Caione regval &= ~mask; 133ed80a13bSCarlo Caione regval |= (val & mask); 134ed80a13bSCarlo Caione 135ed80a13bSCarlo Caione writel(regval, host->base + reg); 136ed80a13bSCarlo Caione } 137ed80a13bSCarlo Caione 138ed80a13bSCarlo Caione static void meson_mx_mmc_soft_reset(struct meson_mx_mmc_host *host) 139ed80a13bSCarlo Caione { 140ed80a13bSCarlo Caione writel(MESON_MX_SDIO_IRQC_SOFT_RESET, host->base + MESON_MX_SDIO_IRQC); 141ed80a13bSCarlo Caione udelay(2); 142ed80a13bSCarlo Caione } 143ed80a13bSCarlo Caione 144ed80a13bSCarlo Caione static struct mmc_command *meson_mx_mmc_get_next_cmd(struct mmc_command *cmd) 145ed80a13bSCarlo Caione { 146ed80a13bSCarlo Caione if (cmd->opcode == MMC_SET_BLOCK_COUNT && !cmd->error) 147ed80a13bSCarlo Caione return cmd->mrq->cmd; 148ed80a13bSCarlo Caione else if (mmc_op_multi(cmd->opcode) && 149ed80a13bSCarlo Caione (!cmd->mrq->sbc || cmd->error || cmd->data->error)) 150ed80a13bSCarlo Caione return cmd->mrq->stop; 151ed80a13bSCarlo Caione else 152ed80a13bSCarlo Caione return NULL; 153ed80a13bSCarlo Caione } 154ed80a13bSCarlo Caione 155ed80a13bSCarlo Caione static void meson_mx_mmc_start_cmd(struct mmc_host *mmc, 156ed80a13bSCarlo Caione struct mmc_command *cmd) 157ed80a13bSCarlo Caione { 158ed80a13bSCarlo Caione struct meson_mx_mmc_host *host = mmc_priv(mmc); 159ed80a13bSCarlo Caione unsigned int pack_size; 160ed80a13bSCarlo Caione unsigned long irqflags, timeout; 161ed80a13bSCarlo Caione u32 mult, send = 0, ext = 0; 162ed80a13bSCarlo Caione 163ed80a13bSCarlo Caione host->cmd = cmd; 164ed80a13bSCarlo Caione 165ed80a13bSCarlo Caione if (cmd->busy_timeout) 166ed80a13bSCarlo Caione timeout = msecs_to_jiffies(cmd->busy_timeout); 167ed80a13bSCarlo Caione else 168ed80a13bSCarlo Caione timeout = msecs_to_jiffies(1000); 169ed80a13bSCarlo Caione 170ed80a13bSCarlo Caione switch (mmc_resp_type(cmd)) { 171ed80a13bSCarlo Caione case MMC_RSP_R1: 172ed80a13bSCarlo Caione case MMC_RSP_R1B: 173ed80a13bSCarlo Caione case MMC_RSP_R3: 174ed80a13bSCarlo Caione /* 7 (CMD) + 32 (response) + 7 (CRC) -1 */ 175ed80a13bSCarlo Caione send |= FIELD_PREP(MESON_MX_SDIO_SEND_CMD_RESP_BITS_MASK, 45); 176ed80a13bSCarlo Caione break; 177ed80a13bSCarlo Caione case MMC_RSP_R2: 178ed80a13bSCarlo Caione /* 7 (CMD) + 120 (response) + 7 (CRC) -1 */ 179ed80a13bSCarlo Caione send |= FIELD_PREP(MESON_MX_SDIO_SEND_CMD_RESP_BITS_MASK, 133); 180ed80a13bSCarlo Caione send |= MESON_MX_SDIO_SEND_RESP_CRC7_FROM_8; 181ed80a13bSCarlo Caione break; 182ed80a13bSCarlo Caione default: 183ed80a13bSCarlo Caione break; 184ed80a13bSCarlo Caione } 185ed80a13bSCarlo Caione 186ed80a13bSCarlo Caione if (!(cmd->flags & MMC_RSP_CRC)) 187ed80a13bSCarlo Caione send |= MESON_MX_SDIO_SEND_RESP_WITHOUT_CRC7; 188ed80a13bSCarlo Caione 189ed80a13bSCarlo Caione if (cmd->flags & MMC_RSP_BUSY) 190ed80a13bSCarlo Caione send |= MESON_MX_SDIO_SEND_CHECK_DAT0_BUSY; 191ed80a13bSCarlo Caione 192ed80a13bSCarlo Caione if (cmd->data) { 193ed80a13bSCarlo Caione send |= FIELD_PREP(MESON_MX_SDIO_SEND_REPEAT_PACKAGE_TIMES_MASK, 194ed80a13bSCarlo Caione (cmd->data->blocks - 1)); 195ed80a13bSCarlo Caione 196ed80a13bSCarlo Caione pack_size = cmd->data->blksz * BITS_PER_BYTE; 197ed80a13bSCarlo Caione if (mmc->ios.bus_width == MMC_BUS_WIDTH_4) 198ed80a13bSCarlo Caione pack_size += MESON_MX_SDIO_RESPONSE_CRC16_BITS * 4; 199ed80a13bSCarlo Caione else 200ed80a13bSCarlo Caione pack_size += MESON_MX_SDIO_RESPONSE_CRC16_BITS * 1; 201ed80a13bSCarlo Caione 202ed80a13bSCarlo Caione ext |= FIELD_PREP(MESON_MX_SDIO_EXT_DATA_RW_NUMBER_MASK, 203ed80a13bSCarlo Caione pack_size); 204ed80a13bSCarlo Caione 205ed80a13bSCarlo Caione if (cmd->data->flags & MMC_DATA_WRITE) 206ed80a13bSCarlo Caione send |= MESON_MX_SDIO_SEND_DATA; 207ed80a13bSCarlo Caione else 208ed80a13bSCarlo Caione send |= MESON_MX_SDIO_SEND_RESP_HAS_DATA; 209ed80a13bSCarlo Caione 210ed80a13bSCarlo Caione cmd->data->bytes_xfered = 0; 211ed80a13bSCarlo Caione } 212ed80a13bSCarlo Caione 213ed80a13bSCarlo Caione send |= FIELD_PREP(MESON_MX_SDIO_SEND_COMMAND_INDEX_MASK, 214ed80a13bSCarlo Caione (0x40 | cmd->opcode)); 215ed80a13bSCarlo Caione 216ed80a13bSCarlo Caione spin_lock_irqsave(&host->irq_lock, irqflags); 217ed80a13bSCarlo Caione 218ed80a13bSCarlo Caione mult = readl(host->base + MESON_MX_SDIO_MULT); 219ed80a13bSCarlo Caione mult &= ~MESON_MX_SDIO_MULT_PORT_SEL_MASK; 220ed80a13bSCarlo Caione mult |= FIELD_PREP(MESON_MX_SDIO_MULT_PORT_SEL_MASK, host->slot_id); 221ed80a13bSCarlo Caione mult |= BIT(31); 222ed80a13bSCarlo Caione writel(mult, host->base + MESON_MX_SDIO_MULT); 223ed80a13bSCarlo Caione 224ed80a13bSCarlo Caione /* enable the CMD done interrupt */ 225ed80a13bSCarlo Caione meson_mx_mmc_mask_bits(mmc, MESON_MX_SDIO_IRQC, 226ed80a13bSCarlo Caione MESON_MX_SDIO_IRQC_ARC_CMD_INT_EN, 227ed80a13bSCarlo Caione MESON_MX_SDIO_IRQC_ARC_CMD_INT_EN); 228ed80a13bSCarlo Caione 229ed80a13bSCarlo Caione /* clear pending interrupts */ 230ed80a13bSCarlo Caione meson_mx_mmc_mask_bits(mmc, MESON_MX_SDIO_IRQS, 231ed80a13bSCarlo Caione MESON_MX_SDIO_IRQS_CMD_INT, 232ed80a13bSCarlo Caione MESON_MX_SDIO_IRQS_CMD_INT); 233ed80a13bSCarlo Caione 234ed80a13bSCarlo Caione writel(cmd->arg, host->base + MESON_MX_SDIO_ARGU); 235ed80a13bSCarlo Caione writel(ext, host->base + MESON_MX_SDIO_EXT); 236ed80a13bSCarlo Caione writel(send, host->base + MESON_MX_SDIO_SEND); 237ed80a13bSCarlo Caione 238ed80a13bSCarlo Caione spin_unlock_irqrestore(&host->irq_lock, irqflags); 239ed80a13bSCarlo Caione 240ed80a13bSCarlo Caione mod_timer(&host->cmd_timeout, jiffies + timeout); 241ed80a13bSCarlo Caione } 242ed80a13bSCarlo Caione 243ed80a13bSCarlo Caione static void meson_mx_mmc_request_done(struct meson_mx_mmc_host *host) 244ed80a13bSCarlo Caione { 245ed80a13bSCarlo Caione struct mmc_request *mrq; 246ed80a13bSCarlo Caione 247ed80a13bSCarlo Caione mrq = host->mrq; 248ed80a13bSCarlo Caione 249ed80a13bSCarlo Caione host->mrq = NULL; 250ed80a13bSCarlo Caione host->cmd = NULL; 251ed80a13bSCarlo Caione 252ed80a13bSCarlo Caione mmc_request_done(host->mmc, mrq); 253ed80a13bSCarlo Caione } 254ed80a13bSCarlo Caione 255ed80a13bSCarlo Caione static void meson_mx_mmc_set_ios(struct mmc_host *mmc, struct mmc_ios *ios) 256ed80a13bSCarlo Caione { 257ed80a13bSCarlo Caione struct meson_mx_mmc_host *host = mmc_priv(mmc); 258ed80a13bSCarlo Caione unsigned short vdd = ios->vdd; 259ed80a13bSCarlo Caione unsigned long clk_rate = ios->clock; 260ed80a13bSCarlo Caione 261ed80a13bSCarlo Caione switch (ios->bus_width) { 262ed80a13bSCarlo Caione case MMC_BUS_WIDTH_1: 263ed80a13bSCarlo Caione meson_mx_mmc_mask_bits(mmc, MESON_MX_SDIO_CONF, 264ed80a13bSCarlo Caione MESON_MX_SDIO_CONF_BUS_WIDTH, 0); 265ed80a13bSCarlo Caione break; 266ed80a13bSCarlo Caione 267ed80a13bSCarlo Caione case MMC_BUS_WIDTH_4: 268ed80a13bSCarlo Caione meson_mx_mmc_mask_bits(mmc, MESON_MX_SDIO_CONF, 269ed80a13bSCarlo Caione MESON_MX_SDIO_CONF_BUS_WIDTH, 270ed80a13bSCarlo Caione MESON_MX_SDIO_CONF_BUS_WIDTH); 271ed80a13bSCarlo Caione break; 272ed80a13bSCarlo Caione 273ed80a13bSCarlo Caione case MMC_BUS_WIDTH_8: 274ed80a13bSCarlo Caione default: 275ed80a13bSCarlo Caione dev_err(mmc_dev(mmc), "unsupported bus width: %d\n", 276ed80a13bSCarlo Caione ios->bus_width); 277ed80a13bSCarlo Caione host->error = -EINVAL; 278ed80a13bSCarlo Caione return; 279ed80a13bSCarlo Caione } 280ed80a13bSCarlo Caione 281ed80a13bSCarlo Caione host->error = clk_set_rate(host->cfg_div_clk, ios->clock); 282ed80a13bSCarlo Caione if (host->error) { 283ed80a13bSCarlo Caione dev_warn(mmc_dev(mmc), 284ed80a13bSCarlo Caione "failed to set MMC clock to %lu: %d\n", 285ed80a13bSCarlo Caione clk_rate, host->error); 286ed80a13bSCarlo Caione return; 287ed80a13bSCarlo Caione } 288ed80a13bSCarlo Caione 289ed80a13bSCarlo Caione mmc->actual_clock = clk_get_rate(host->cfg_div_clk); 290ed80a13bSCarlo Caione 291ed80a13bSCarlo Caione switch (ios->power_mode) { 292ed80a13bSCarlo Caione case MMC_POWER_OFF: 293ed80a13bSCarlo Caione vdd = 0; 294d2681cd8SGustavo A. R. Silva /* fall through */ 295ed80a13bSCarlo Caione case MMC_POWER_UP: 296ed80a13bSCarlo Caione if (!IS_ERR(mmc->supply.vmmc)) { 297ed80a13bSCarlo Caione host->error = mmc_regulator_set_ocr(mmc, 298ed80a13bSCarlo Caione mmc->supply.vmmc, 299ed80a13bSCarlo Caione vdd); 300ed80a13bSCarlo Caione if (host->error) 301ed80a13bSCarlo Caione return; 302ed80a13bSCarlo Caione } 303ed80a13bSCarlo Caione break; 304ed80a13bSCarlo Caione } 305ed80a13bSCarlo Caione } 306ed80a13bSCarlo Caione 307ed80a13bSCarlo Caione static int meson_mx_mmc_map_dma(struct mmc_host *mmc, struct mmc_request *mrq) 308ed80a13bSCarlo Caione { 309ed80a13bSCarlo Caione struct mmc_data *data = mrq->data; 310ed80a13bSCarlo Caione int dma_len; 311ed80a13bSCarlo Caione struct scatterlist *sg; 312ed80a13bSCarlo Caione 313ed80a13bSCarlo Caione if (!data) 314ed80a13bSCarlo Caione return 0; 315ed80a13bSCarlo Caione 316ed80a13bSCarlo Caione sg = data->sg; 317ed80a13bSCarlo Caione if (sg->offset & 3 || sg->length & 3) { 318ed80a13bSCarlo Caione dev_err(mmc_dev(mmc), 319ed80a13bSCarlo Caione "unaligned scatterlist: offset %x length %d\n", 320ed80a13bSCarlo Caione sg->offset, sg->length); 321ed80a13bSCarlo Caione return -EINVAL; 322ed80a13bSCarlo Caione } 323ed80a13bSCarlo Caione 324ed80a13bSCarlo Caione dma_len = dma_map_sg(mmc_dev(mmc), data->sg, data->sg_len, 325ed80a13bSCarlo Caione mmc_get_dma_dir(data)); 326ed80a13bSCarlo Caione if (dma_len <= 0) { 327ed80a13bSCarlo Caione dev_err(mmc_dev(mmc), "dma_map_sg failed\n"); 328ed80a13bSCarlo Caione return -ENOMEM; 329ed80a13bSCarlo Caione } 330ed80a13bSCarlo Caione 331ed80a13bSCarlo Caione return 0; 332ed80a13bSCarlo Caione } 333ed80a13bSCarlo Caione 334ed80a13bSCarlo Caione static void meson_mx_mmc_request(struct mmc_host *mmc, struct mmc_request *mrq) 335ed80a13bSCarlo Caione { 336ed80a13bSCarlo Caione struct meson_mx_mmc_host *host = mmc_priv(mmc); 337ed80a13bSCarlo Caione struct mmc_command *cmd = mrq->cmd; 338ed80a13bSCarlo Caione 339ed80a13bSCarlo Caione if (!host->error) 340ed80a13bSCarlo Caione host->error = meson_mx_mmc_map_dma(mmc, mrq); 341ed80a13bSCarlo Caione 342ed80a13bSCarlo Caione if (host->error) { 343ed80a13bSCarlo Caione cmd->error = host->error; 344ed80a13bSCarlo Caione mmc_request_done(mmc, mrq); 345ed80a13bSCarlo Caione return; 346ed80a13bSCarlo Caione } 347ed80a13bSCarlo Caione 348ed80a13bSCarlo Caione host->mrq = mrq; 349ed80a13bSCarlo Caione 350ed80a13bSCarlo Caione if (mrq->data) 351ed80a13bSCarlo Caione writel(sg_dma_address(mrq->data->sg), 352ed80a13bSCarlo Caione host->base + MESON_MX_SDIO_ADDR); 353ed80a13bSCarlo Caione 354ed80a13bSCarlo Caione if (mrq->sbc) 355ed80a13bSCarlo Caione meson_mx_mmc_start_cmd(mmc, mrq->sbc); 356ed80a13bSCarlo Caione else 357ed80a13bSCarlo Caione meson_mx_mmc_start_cmd(mmc, mrq->cmd); 358ed80a13bSCarlo Caione } 359ed80a13bSCarlo Caione 360ed80a13bSCarlo Caione static int meson_mx_mmc_card_busy(struct mmc_host *mmc) 361ed80a13bSCarlo Caione { 362ed80a13bSCarlo Caione struct meson_mx_mmc_host *host = mmc_priv(mmc); 363ed80a13bSCarlo Caione u32 irqc = readl(host->base + MESON_MX_SDIO_IRQC); 364ed80a13bSCarlo Caione 365ed80a13bSCarlo Caione return !!(irqc & MESON_MX_SDIO_IRQC_FORCE_DATA_DAT_MASK); 366ed80a13bSCarlo Caione } 367ed80a13bSCarlo Caione 368ed80a13bSCarlo Caione static void meson_mx_mmc_read_response(struct mmc_host *mmc, 369ed80a13bSCarlo Caione struct mmc_command *cmd) 370ed80a13bSCarlo Caione { 371ed80a13bSCarlo Caione struct meson_mx_mmc_host *host = mmc_priv(mmc); 372ed80a13bSCarlo Caione u32 mult; 373ed80a13bSCarlo Caione int i, resp[4]; 374ed80a13bSCarlo Caione 375ed80a13bSCarlo Caione mult = readl(host->base + MESON_MX_SDIO_MULT); 376ed80a13bSCarlo Caione mult |= MESON_MX_SDIO_MULT_WR_RD_OUT_INDEX; 377ed80a13bSCarlo Caione mult &= ~MESON_MX_SDIO_MULT_RESP_READ_INDEX_MASK; 378ed80a13bSCarlo Caione mult |= FIELD_PREP(MESON_MX_SDIO_MULT_RESP_READ_INDEX_MASK, 0); 379ed80a13bSCarlo Caione writel(mult, host->base + MESON_MX_SDIO_MULT); 380ed80a13bSCarlo Caione 381ed80a13bSCarlo Caione if (cmd->flags & MMC_RSP_136) { 382ed80a13bSCarlo Caione for (i = 0; i <= 3; i++) 383ed80a13bSCarlo Caione resp[3 - i] = readl(host->base + MESON_MX_SDIO_ARGU); 384ed80a13bSCarlo Caione cmd->resp[0] = (resp[0] << 8) | ((resp[1] >> 24) & 0xff); 385ed80a13bSCarlo Caione cmd->resp[1] = (resp[1] << 8) | ((resp[2] >> 24) & 0xff); 386ed80a13bSCarlo Caione cmd->resp[2] = (resp[2] << 8) | ((resp[3] >> 24) & 0xff); 387ed80a13bSCarlo Caione cmd->resp[3] = (resp[3] << 8); 388ed80a13bSCarlo Caione } else if (cmd->flags & MMC_RSP_PRESENT) { 389ed80a13bSCarlo Caione cmd->resp[0] = readl(host->base + MESON_MX_SDIO_ARGU); 390ed80a13bSCarlo Caione } 391ed80a13bSCarlo Caione } 392ed80a13bSCarlo Caione 393ed80a13bSCarlo Caione static irqreturn_t meson_mx_mmc_process_cmd_irq(struct meson_mx_mmc_host *host, 394ed80a13bSCarlo Caione u32 irqs, u32 send) 395ed80a13bSCarlo Caione { 396ed80a13bSCarlo Caione struct mmc_command *cmd = host->cmd; 397ed80a13bSCarlo Caione 398ed80a13bSCarlo Caione /* 399ed80a13bSCarlo Caione * NOTE: even though it shouldn't happen we sometimes get command 400ed80a13bSCarlo Caione * interrupts twice (at least this is what it looks like). Ideally 401ed80a13bSCarlo Caione * we find out why this happens and warn here as soon as it occurs. 402ed80a13bSCarlo Caione */ 403ed80a13bSCarlo Caione if (!cmd) 404ed80a13bSCarlo Caione return IRQ_HANDLED; 405ed80a13bSCarlo Caione 406ed80a13bSCarlo Caione cmd->error = 0; 407ed80a13bSCarlo Caione meson_mx_mmc_read_response(host->mmc, cmd); 408ed80a13bSCarlo Caione 409ed80a13bSCarlo Caione if (cmd->data) { 410ed80a13bSCarlo Caione if (!((irqs & MESON_MX_SDIO_IRQS_DATA_READ_CRC16_OK) || 411ed80a13bSCarlo Caione (irqs & MESON_MX_SDIO_IRQS_DATA_WRITE_CRC16_OK))) 412ed80a13bSCarlo Caione cmd->error = -EILSEQ; 413ed80a13bSCarlo Caione } else { 414ed80a13bSCarlo Caione if (!((irqs & MESON_MX_SDIO_IRQS_RESP_CRC7_OK) || 415ed80a13bSCarlo Caione (send & MESON_MX_SDIO_SEND_RESP_WITHOUT_CRC7))) 416ed80a13bSCarlo Caione cmd->error = -EILSEQ; 417ed80a13bSCarlo Caione } 418ed80a13bSCarlo Caione 419ed80a13bSCarlo Caione return IRQ_WAKE_THREAD; 420ed80a13bSCarlo Caione } 421ed80a13bSCarlo Caione 422ed80a13bSCarlo Caione static irqreturn_t meson_mx_mmc_irq(int irq, void *data) 423ed80a13bSCarlo Caione { 424ed80a13bSCarlo Caione struct meson_mx_mmc_host *host = (void *) data; 425ed80a13bSCarlo Caione u32 irqs, send; 426ed80a13bSCarlo Caione unsigned long irqflags; 427ed80a13bSCarlo Caione irqreturn_t ret; 428ed80a13bSCarlo Caione 429ed80a13bSCarlo Caione spin_lock_irqsave(&host->irq_lock, irqflags); 430ed80a13bSCarlo Caione 431ed80a13bSCarlo Caione irqs = readl(host->base + MESON_MX_SDIO_IRQS); 432ed80a13bSCarlo Caione send = readl(host->base + MESON_MX_SDIO_SEND); 433ed80a13bSCarlo Caione 434ed80a13bSCarlo Caione if (irqs & MESON_MX_SDIO_IRQS_CMD_INT) 435ed80a13bSCarlo Caione ret = meson_mx_mmc_process_cmd_irq(host, irqs, send); 436ed80a13bSCarlo Caione else 437ed80a13bSCarlo Caione ret = IRQ_HANDLED; 438ed80a13bSCarlo Caione 439ed80a13bSCarlo Caione /* finally ACK all pending interrupts */ 440ed80a13bSCarlo Caione writel(irqs, host->base + MESON_MX_SDIO_IRQS); 441ed80a13bSCarlo Caione 442ed80a13bSCarlo Caione spin_unlock_irqrestore(&host->irq_lock, irqflags); 443ed80a13bSCarlo Caione 444ed80a13bSCarlo Caione return ret; 445ed80a13bSCarlo Caione } 446ed80a13bSCarlo Caione 447ed80a13bSCarlo Caione static irqreturn_t meson_mx_mmc_irq_thread(int irq, void *irq_data) 448ed80a13bSCarlo Caione { 449ed80a13bSCarlo Caione struct meson_mx_mmc_host *host = (void *) irq_data; 450ed80a13bSCarlo Caione struct mmc_command *cmd = host->cmd, *next_cmd; 451ed80a13bSCarlo Caione 452ed80a13bSCarlo Caione if (WARN_ON(!cmd)) 453ed80a13bSCarlo Caione return IRQ_HANDLED; 454ed80a13bSCarlo Caione 455ed80a13bSCarlo Caione del_timer_sync(&host->cmd_timeout); 456ed80a13bSCarlo Caione 457ed80a13bSCarlo Caione if (cmd->data) { 458ed80a13bSCarlo Caione dma_unmap_sg(mmc_dev(host->mmc), cmd->data->sg, 459ed80a13bSCarlo Caione cmd->data->sg_len, 460ed80a13bSCarlo Caione mmc_get_dma_dir(cmd->data)); 461ed80a13bSCarlo Caione 462ed80a13bSCarlo Caione cmd->data->bytes_xfered = cmd->data->blksz * cmd->data->blocks; 463ed80a13bSCarlo Caione } 464ed80a13bSCarlo Caione 465ed80a13bSCarlo Caione next_cmd = meson_mx_mmc_get_next_cmd(cmd); 466ed80a13bSCarlo Caione if (next_cmd) 467ed80a13bSCarlo Caione meson_mx_mmc_start_cmd(host->mmc, next_cmd); 468ed80a13bSCarlo Caione else 469ed80a13bSCarlo Caione meson_mx_mmc_request_done(host); 470ed80a13bSCarlo Caione 471ed80a13bSCarlo Caione return IRQ_HANDLED; 472ed80a13bSCarlo Caione } 473ed80a13bSCarlo Caione 4742ee4f620SKees Cook static void meson_mx_mmc_timeout(struct timer_list *t) 475ed80a13bSCarlo Caione { 4762ee4f620SKees Cook struct meson_mx_mmc_host *host = from_timer(host, t, cmd_timeout); 477ed80a13bSCarlo Caione unsigned long irqflags; 478ed80a13bSCarlo Caione u32 irqc; 479ed80a13bSCarlo Caione 480ed80a13bSCarlo Caione spin_lock_irqsave(&host->irq_lock, irqflags); 481ed80a13bSCarlo Caione 482ed80a13bSCarlo Caione /* disable the CMD interrupt */ 483ed80a13bSCarlo Caione irqc = readl(host->base + MESON_MX_SDIO_IRQC); 484ed80a13bSCarlo Caione irqc &= ~MESON_MX_SDIO_IRQC_ARC_CMD_INT_EN; 485ed80a13bSCarlo Caione writel(irqc, host->base + MESON_MX_SDIO_IRQC); 486ed80a13bSCarlo Caione 487ed80a13bSCarlo Caione spin_unlock_irqrestore(&host->irq_lock, irqflags); 488ed80a13bSCarlo Caione 489ed80a13bSCarlo Caione /* 490ed80a13bSCarlo Caione * skip the timeout handling if the interrupt handler already processed 491ed80a13bSCarlo Caione * the command. 492ed80a13bSCarlo Caione */ 493ed80a13bSCarlo Caione if (!host->cmd) 494ed80a13bSCarlo Caione return; 495ed80a13bSCarlo Caione 496ed80a13bSCarlo Caione dev_dbg(mmc_dev(host->mmc), 497ed80a13bSCarlo Caione "Timeout on CMD%u (IRQS = 0x%08x, ARGU = 0x%08x)\n", 498ed80a13bSCarlo Caione host->cmd->opcode, readl(host->base + MESON_MX_SDIO_IRQS), 499ed80a13bSCarlo Caione readl(host->base + MESON_MX_SDIO_ARGU)); 500ed80a13bSCarlo Caione 501ed80a13bSCarlo Caione host->cmd->error = -ETIMEDOUT; 502ed80a13bSCarlo Caione 503ed80a13bSCarlo Caione meson_mx_mmc_request_done(host); 504ed80a13bSCarlo Caione } 505ed80a13bSCarlo Caione 506ed80a13bSCarlo Caione static struct mmc_host_ops meson_mx_mmc_ops = { 507ed80a13bSCarlo Caione .request = meson_mx_mmc_request, 508ed80a13bSCarlo Caione .set_ios = meson_mx_mmc_set_ios, 509ed80a13bSCarlo Caione .card_busy = meson_mx_mmc_card_busy, 510ed80a13bSCarlo Caione .get_cd = mmc_gpio_get_cd, 511ed80a13bSCarlo Caione .get_ro = mmc_gpio_get_ro, 512ed80a13bSCarlo Caione }; 513ed80a13bSCarlo Caione 514ed80a13bSCarlo Caione static struct platform_device *meson_mx_mmc_slot_pdev(struct device *parent) 515ed80a13bSCarlo Caione { 516ed80a13bSCarlo Caione struct device_node *slot_node; 517c483a5ccSJohan Hovold struct platform_device *pdev; 518ed80a13bSCarlo Caione 519ed80a13bSCarlo Caione /* 520ed80a13bSCarlo Caione * TODO: the MMC core framework currently does not support 521ed80a13bSCarlo Caione * controllers with multiple slots properly. So we only register 522ed80a13bSCarlo Caione * the first slot for now 523ed80a13bSCarlo Caione */ 524c483a5ccSJohan Hovold slot_node = of_get_compatible_child(parent->of_node, "mmc-slot"); 525ed80a13bSCarlo Caione if (!slot_node) { 526ed80a13bSCarlo Caione dev_warn(parent, "no 'mmc-slot' sub-node found\n"); 527ed80a13bSCarlo Caione return ERR_PTR(-ENOENT); 528ed80a13bSCarlo Caione } 529ed80a13bSCarlo Caione 530c483a5ccSJohan Hovold pdev = of_platform_device_create(slot_node, NULL, parent); 531c483a5ccSJohan Hovold of_node_put(slot_node); 532c483a5ccSJohan Hovold 533c483a5ccSJohan Hovold return pdev; 534ed80a13bSCarlo Caione } 535ed80a13bSCarlo Caione 536ed80a13bSCarlo Caione static int meson_mx_mmc_add_host(struct meson_mx_mmc_host *host) 537ed80a13bSCarlo Caione { 538ed80a13bSCarlo Caione struct mmc_host *mmc = host->mmc; 539ed80a13bSCarlo Caione struct device *slot_dev = mmc_dev(mmc); 540ed80a13bSCarlo Caione int ret; 541ed80a13bSCarlo Caione 542ed80a13bSCarlo Caione if (of_property_read_u32(slot_dev->of_node, "reg", &host->slot_id)) { 543ed80a13bSCarlo Caione dev_err(slot_dev, "missing 'reg' property\n"); 544ed80a13bSCarlo Caione return -EINVAL; 545ed80a13bSCarlo Caione } 546ed80a13bSCarlo Caione 547ed80a13bSCarlo Caione if (host->slot_id >= MESON_MX_SDIO_MAX_SLOTS) { 548ed80a13bSCarlo Caione dev_err(slot_dev, "invalid 'reg' property value %d\n", 549ed80a13bSCarlo Caione host->slot_id); 550ed80a13bSCarlo Caione return -EINVAL; 551ed80a13bSCarlo Caione } 552ed80a13bSCarlo Caione 553ed80a13bSCarlo Caione /* Get regulators and the supported OCR mask */ 554ed80a13bSCarlo Caione ret = mmc_regulator_get_supply(mmc); 555aa5754c7SWolfram Sang if (ret) 556ed80a13bSCarlo Caione return ret; 557ed80a13bSCarlo Caione 558ed80a13bSCarlo Caione mmc->max_req_size = MESON_MX_SDIO_BOUNCE_REQ_SIZE; 559ed80a13bSCarlo Caione mmc->max_seg_size = mmc->max_req_size; 560ed80a13bSCarlo Caione mmc->max_blk_count = 561ed80a13bSCarlo Caione FIELD_GET(MESON_MX_SDIO_SEND_REPEAT_PACKAGE_TIMES_MASK, 562ed80a13bSCarlo Caione 0xffffffff); 563ed80a13bSCarlo Caione mmc->max_blk_size = FIELD_GET(MESON_MX_SDIO_EXT_DATA_RW_NUMBER_MASK, 564ed80a13bSCarlo Caione 0xffffffff); 565ed80a13bSCarlo Caione mmc->max_blk_size -= (4 * MESON_MX_SDIO_RESPONSE_CRC16_BITS); 566ed80a13bSCarlo Caione mmc->max_blk_size /= BITS_PER_BYTE; 567ed80a13bSCarlo Caione 568ed80a13bSCarlo Caione /* Get the min and max supported clock rates */ 569ed80a13bSCarlo Caione mmc->f_min = clk_round_rate(host->cfg_div_clk, 1); 570ed80a13bSCarlo Caione mmc->f_max = clk_round_rate(host->cfg_div_clk, 571ed80a13bSCarlo Caione clk_get_rate(host->parent_clk)); 572ed80a13bSCarlo Caione 573ed80a13bSCarlo Caione mmc->caps |= MMC_CAP_ERASE | MMC_CAP_CMD23; 574ed80a13bSCarlo Caione mmc->ops = &meson_mx_mmc_ops; 575ed80a13bSCarlo Caione 576ed80a13bSCarlo Caione ret = mmc_of_parse(mmc); 577ed80a13bSCarlo Caione if (ret) 578ed80a13bSCarlo Caione return ret; 579ed80a13bSCarlo Caione 580ed80a13bSCarlo Caione ret = mmc_add_host(mmc); 581ed80a13bSCarlo Caione if (ret) 582ed80a13bSCarlo Caione return ret; 583ed80a13bSCarlo Caione 584ed80a13bSCarlo Caione return 0; 585ed80a13bSCarlo Caione } 586ed80a13bSCarlo Caione 587ed80a13bSCarlo Caione static int meson_mx_mmc_register_clks(struct meson_mx_mmc_host *host) 588ed80a13bSCarlo Caione { 589ed80a13bSCarlo Caione struct clk_init_data init; 590ed80a13bSCarlo Caione const char *clk_div_parent, *clk_fixed_factor_parent; 591ed80a13bSCarlo Caione 592ed80a13bSCarlo Caione clk_fixed_factor_parent = __clk_get_name(host->parent_clk); 593ed80a13bSCarlo Caione init.name = devm_kasprintf(host->controller_dev, GFP_KERNEL, 594ed80a13bSCarlo Caione "%s#fixed_factor", 595ed80a13bSCarlo Caione dev_name(host->controller_dev)); 596b0d06f1cSNicholas Mc Guire if (!init.name) 597b0d06f1cSNicholas Mc Guire return -ENOMEM; 598b0d06f1cSNicholas Mc Guire 599ed80a13bSCarlo Caione init.ops = &clk_fixed_factor_ops; 600ed80a13bSCarlo Caione init.flags = 0; 601ed80a13bSCarlo Caione init.parent_names = &clk_fixed_factor_parent; 602ed80a13bSCarlo Caione init.num_parents = 1; 603ed80a13bSCarlo Caione host->fixed_factor.div = 2; 604ed80a13bSCarlo Caione host->fixed_factor.mult = 1; 605ed80a13bSCarlo Caione host->fixed_factor.hw.init = &init; 606ed80a13bSCarlo Caione 607ed80a13bSCarlo Caione host->fixed_factor_clk = devm_clk_register(host->controller_dev, 608ed80a13bSCarlo Caione &host->fixed_factor.hw); 6092f129d39SDan Carpenter if (WARN_ON(IS_ERR(host->fixed_factor_clk))) 610ed80a13bSCarlo Caione return PTR_ERR(host->fixed_factor_clk); 611ed80a13bSCarlo Caione 612ed80a13bSCarlo Caione clk_div_parent = __clk_get_name(host->fixed_factor_clk); 613ed80a13bSCarlo Caione init.name = devm_kasprintf(host->controller_dev, GFP_KERNEL, 614ed80a13bSCarlo Caione "%s#div", dev_name(host->controller_dev)); 615b0d06f1cSNicholas Mc Guire if (!init.name) 616b0d06f1cSNicholas Mc Guire return -ENOMEM; 617b0d06f1cSNicholas Mc Guire 618ed80a13bSCarlo Caione init.ops = &clk_divider_ops; 619ed80a13bSCarlo Caione init.flags = CLK_SET_RATE_PARENT; 620ed80a13bSCarlo Caione init.parent_names = &clk_div_parent; 621ed80a13bSCarlo Caione init.num_parents = 1; 622ed80a13bSCarlo Caione host->cfg_div.reg = host->base + MESON_MX_SDIO_CONF; 623ed80a13bSCarlo Caione host->cfg_div.shift = MESON_MX_SDIO_CONF_CMD_CLK_DIV_SHIFT; 624ed80a13bSCarlo Caione host->cfg_div.width = MESON_MX_SDIO_CONF_CMD_CLK_DIV_WIDTH; 625ed80a13bSCarlo Caione host->cfg_div.hw.init = &init; 626ed80a13bSCarlo Caione host->cfg_div.flags = CLK_DIVIDER_ALLOW_ZERO; 627ed80a13bSCarlo Caione 628ed80a13bSCarlo Caione host->cfg_div_clk = devm_clk_register(host->controller_dev, 629ed80a13bSCarlo Caione &host->cfg_div.hw); 6302f129d39SDan Carpenter if (WARN_ON(IS_ERR(host->cfg_div_clk))) 6317599b849SDan Carpenter return PTR_ERR(host->cfg_div_clk); 632ed80a13bSCarlo Caione 633ed80a13bSCarlo Caione return 0; 634ed80a13bSCarlo Caione } 635ed80a13bSCarlo Caione 636ed80a13bSCarlo Caione static int meson_mx_mmc_probe(struct platform_device *pdev) 637ed80a13bSCarlo Caione { 638ed80a13bSCarlo Caione struct platform_device *slot_pdev; 639ed80a13bSCarlo Caione struct mmc_host *mmc; 640ed80a13bSCarlo Caione struct meson_mx_mmc_host *host; 641ed80a13bSCarlo Caione struct resource *res; 642ed80a13bSCarlo Caione int ret, irq; 643ed80a13bSCarlo Caione u32 conf; 644ed80a13bSCarlo Caione 645ed80a13bSCarlo Caione slot_pdev = meson_mx_mmc_slot_pdev(&pdev->dev); 646ed80a13bSCarlo Caione if (!slot_pdev) 647ed80a13bSCarlo Caione return -ENODEV; 648ed80a13bSCarlo Caione else if (IS_ERR(slot_pdev)) 649ed80a13bSCarlo Caione return PTR_ERR(slot_pdev); 650ed80a13bSCarlo Caione 651ed80a13bSCarlo Caione mmc = mmc_alloc_host(sizeof(*host), &slot_pdev->dev); 652ed80a13bSCarlo Caione if (!mmc) { 653ed80a13bSCarlo Caione ret = -ENOMEM; 654ed80a13bSCarlo Caione goto error_unregister_slot_pdev; 655ed80a13bSCarlo Caione } 656ed80a13bSCarlo Caione 657ed80a13bSCarlo Caione host = mmc_priv(mmc); 658ed80a13bSCarlo Caione host->mmc = mmc; 659ed80a13bSCarlo Caione host->controller_dev = &pdev->dev; 660ed80a13bSCarlo Caione 661ed80a13bSCarlo Caione spin_lock_init(&host->irq_lock); 6622ee4f620SKees Cook timer_setup(&host->cmd_timeout, meson_mx_mmc_timeout, 0); 663ed80a13bSCarlo Caione 664ed80a13bSCarlo Caione platform_set_drvdata(pdev, host); 665ed80a13bSCarlo Caione 666ed80a13bSCarlo Caione res = platform_get_resource(pdev, IORESOURCE_MEM, 0); 667ed80a13bSCarlo Caione host->base = devm_ioremap_resource(host->controller_dev, res); 668ed80a13bSCarlo Caione if (IS_ERR(host->base)) { 669ed80a13bSCarlo Caione ret = PTR_ERR(host->base); 670ed80a13bSCarlo Caione goto error_free_mmc; 671ed80a13bSCarlo Caione } 672ed80a13bSCarlo Caione 673ed80a13bSCarlo Caione irq = platform_get_irq(pdev, 0); 674ed80a13bSCarlo Caione ret = devm_request_threaded_irq(host->controller_dev, irq, 675ed80a13bSCarlo Caione meson_mx_mmc_irq, 676ed80a13bSCarlo Caione meson_mx_mmc_irq_thread, IRQF_ONESHOT, 677ed80a13bSCarlo Caione NULL, host); 678ed80a13bSCarlo Caione if (ret) 679ed80a13bSCarlo Caione goto error_free_mmc; 680ed80a13bSCarlo Caione 681ed80a13bSCarlo Caione host->core_clk = devm_clk_get(host->controller_dev, "core"); 682ed80a13bSCarlo Caione if (IS_ERR(host->core_clk)) { 683ed80a13bSCarlo Caione ret = PTR_ERR(host->core_clk); 684ed80a13bSCarlo Caione goto error_free_mmc; 685ed80a13bSCarlo Caione } 686ed80a13bSCarlo Caione 687ed80a13bSCarlo Caione host->parent_clk = devm_clk_get(host->controller_dev, "clkin"); 688ed80a13bSCarlo Caione if (IS_ERR(host->parent_clk)) { 689ed80a13bSCarlo Caione ret = PTR_ERR(host->parent_clk); 690ed80a13bSCarlo Caione goto error_free_mmc; 691ed80a13bSCarlo Caione } 692ed80a13bSCarlo Caione 693ed80a13bSCarlo Caione ret = meson_mx_mmc_register_clks(host); 694ed80a13bSCarlo Caione if (ret) 695ed80a13bSCarlo Caione goto error_free_mmc; 696ed80a13bSCarlo Caione 697ed80a13bSCarlo Caione ret = clk_prepare_enable(host->core_clk); 698ed80a13bSCarlo Caione if (ret) { 699ed80a13bSCarlo Caione dev_err(host->controller_dev, "Failed to enable core clock\n"); 700ed80a13bSCarlo Caione goto error_free_mmc; 701ed80a13bSCarlo Caione } 702ed80a13bSCarlo Caione 703ed80a13bSCarlo Caione ret = clk_prepare_enable(host->cfg_div_clk); 704ed80a13bSCarlo Caione if (ret) { 705ed80a13bSCarlo Caione dev_err(host->controller_dev, "Failed to enable MMC clock\n"); 706ed80a13bSCarlo Caione goto error_disable_core_clk; 707ed80a13bSCarlo Caione } 708ed80a13bSCarlo Caione 709ed80a13bSCarlo Caione conf = 0; 710ed80a13bSCarlo Caione conf |= FIELD_PREP(MESON_MX_SDIO_CONF_CMD_ARGUMENT_BITS_MASK, 39); 711ed80a13bSCarlo Caione conf |= FIELD_PREP(MESON_MX_SDIO_CONF_M_ENDIAN_MASK, 0x3); 712ed80a13bSCarlo Caione conf |= FIELD_PREP(MESON_MX_SDIO_CONF_WRITE_NWR_MASK, 0x2); 713ed80a13bSCarlo Caione conf |= FIELD_PREP(MESON_MX_SDIO_CONF_WRITE_CRC_OK_STATUS_MASK, 0x2); 714ed80a13bSCarlo Caione writel(conf, host->base + MESON_MX_SDIO_CONF); 715ed80a13bSCarlo Caione 716ed80a13bSCarlo Caione meson_mx_mmc_soft_reset(host); 717ed80a13bSCarlo Caione 718ed80a13bSCarlo Caione ret = meson_mx_mmc_add_host(host); 719ed80a13bSCarlo Caione if (ret) 720ed80a13bSCarlo Caione goto error_disable_clks; 721ed80a13bSCarlo Caione 722ed80a13bSCarlo Caione return 0; 723ed80a13bSCarlo Caione 724ed80a13bSCarlo Caione error_disable_clks: 725ed80a13bSCarlo Caione clk_disable_unprepare(host->cfg_div_clk); 726ed80a13bSCarlo Caione error_disable_core_clk: 727ed80a13bSCarlo Caione clk_disable_unprepare(host->core_clk); 728ed80a13bSCarlo Caione error_free_mmc: 729ed80a13bSCarlo Caione mmc_free_host(mmc); 730ed80a13bSCarlo Caione error_unregister_slot_pdev: 731ed80a13bSCarlo Caione of_platform_device_destroy(&slot_pdev->dev, NULL); 732ed80a13bSCarlo Caione return ret; 733ed80a13bSCarlo Caione } 734ed80a13bSCarlo Caione 735ed80a13bSCarlo Caione static int meson_mx_mmc_remove(struct platform_device *pdev) 736ed80a13bSCarlo Caione { 737ed80a13bSCarlo Caione struct meson_mx_mmc_host *host = platform_get_drvdata(pdev); 738ed80a13bSCarlo Caione struct device *slot_dev = mmc_dev(host->mmc); 739ed80a13bSCarlo Caione 740ed80a13bSCarlo Caione del_timer_sync(&host->cmd_timeout); 741ed80a13bSCarlo Caione 742ed80a13bSCarlo Caione mmc_remove_host(host->mmc); 743ed80a13bSCarlo Caione 744ed80a13bSCarlo Caione of_platform_device_destroy(slot_dev, NULL); 745ed80a13bSCarlo Caione 746ed80a13bSCarlo Caione clk_disable_unprepare(host->cfg_div_clk); 747ed80a13bSCarlo Caione clk_disable_unprepare(host->core_clk); 748ed80a13bSCarlo Caione 749ed80a13bSCarlo Caione mmc_free_host(host->mmc); 750ed80a13bSCarlo Caione 751ed80a13bSCarlo Caione return 0; 752ed80a13bSCarlo Caione } 753ed80a13bSCarlo Caione 754ed80a13bSCarlo Caione static const struct of_device_id meson_mx_mmc_of_match[] = { 755ed80a13bSCarlo Caione { .compatible = "amlogic,meson8-sdio", }, 756ed80a13bSCarlo Caione { .compatible = "amlogic,meson8b-sdio", }, 757ed80a13bSCarlo Caione { /* sentinel */ } 758ed80a13bSCarlo Caione }; 759ed80a13bSCarlo Caione MODULE_DEVICE_TABLE(of, meson_mx_mmc_of_match); 760ed80a13bSCarlo Caione 761ed80a13bSCarlo Caione static struct platform_driver meson_mx_mmc_driver = { 762ed80a13bSCarlo Caione .probe = meson_mx_mmc_probe, 763ed80a13bSCarlo Caione .remove = meson_mx_mmc_remove, 764ed80a13bSCarlo Caione .driver = { 765ed80a13bSCarlo Caione .name = "meson-mx-sdio", 766ed80a13bSCarlo Caione .of_match_table = of_match_ptr(meson_mx_mmc_of_match), 767ed80a13bSCarlo Caione }, 768ed80a13bSCarlo Caione }; 769ed80a13bSCarlo Caione 770ed80a13bSCarlo Caione module_platform_driver(meson_mx_mmc_driver); 771ed80a13bSCarlo Caione 772ed80a13bSCarlo Caione MODULE_DESCRIPTION("Meson6, Meson8 and Meson8b SDIO/MMC Host Driver"); 773ed80a13bSCarlo Caione MODULE_AUTHOR("Carlo Caione <carlo@endlessm.com>"); 774ed80a13bSCarlo Caione MODULE_AUTHOR("Martin Blumenstingl <martin.blumenstingl@googlemail.com>"); 775ed80a13bSCarlo Caione MODULE_LICENSE("GPL v2"); 776