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