2012-10-15 19:10:31 +00:00
|
|
|
/*
|
|
|
|
* (C) Copyright 2012 SAMSUNG Electronics
|
|
|
|
* Jaehoon Chung <jh80.chung@samsung.com>
|
|
|
|
*
|
2013-07-08 07:37:19 +00:00
|
|
|
* SPDX-License-Identifier: GPL-2.0+
|
2012-10-15 19:10:31 +00:00
|
|
|
*/
|
|
|
|
|
|
|
|
#include <common.h>
|
|
|
|
#include <dwmmc.h>
|
2013-04-27 06:12:55 +00:00
|
|
|
#include <fdtdec.h>
|
|
|
|
#include <libfdt.h>
|
|
|
|
#include <malloc.h>
|
2012-10-15 19:10:31 +00:00
|
|
|
#include <asm/arch/dwmmc.h>
|
|
|
|
#include <asm/arch/clk.h>
|
2013-04-27 06:12:55 +00:00
|
|
|
#include <asm/arch/pinmux.h>
|
2014-05-16 04:59:52 +00:00
|
|
|
#include <asm/gpio.h>
|
|
|
|
#include <asm-generic/errno.h>
|
2012-10-15 19:10:31 +00:00
|
|
|
|
2013-04-27 06:12:55 +00:00
|
|
|
#define DWMMC_MAX_CH_NUM 4
|
|
|
|
#define DWMMC_MAX_FREQ 52000000
|
|
|
|
#define DWMMC_MIN_FREQ 400000
|
|
|
|
#define DWMMC_MMC0_CLKSEL_VAL 0x03030001
|
|
|
|
#define DWMMC_MMC2_CLKSEL_VAL 0x03020001
|
2012-10-15 19:10:31 +00:00
|
|
|
|
2013-04-27 06:12:55 +00:00
|
|
|
/*
|
|
|
|
* Function used as callback function to initialise the
|
|
|
|
* CLKSEL register for every mmc channel.
|
|
|
|
*/
|
2012-10-15 19:10:31 +00:00
|
|
|
static void exynos_dwmci_clksel(struct dwmci_host *host)
|
|
|
|
{
|
2013-04-27 06:12:55 +00:00
|
|
|
dwmci_writel(host, DWMCI_CLKSEL, host->clksel_val);
|
|
|
|
}
|
2012-10-15 19:10:31 +00:00
|
|
|
|
2014-02-05 05:18:15 +00:00
|
|
|
unsigned int exynos_dwmci_get_clk(struct dwmci_host *host)
|
2013-04-27 06:12:55 +00:00
|
|
|
{
|
2014-02-05 05:18:15 +00:00
|
|
|
unsigned long sclk;
|
|
|
|
int8_t clk_div;
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Since SDCLKIN is divided inside controller by the DIVRATIO
|
|
|
|
* value set in the CLKSEL register, we need to use the same output
|
|
|
|
* clock value to calculate the CLKDIV value.
|
|
|
|
* as per user manual:cclk_in = SDCLKIN / (DIVRATIO + 1)
|
|
|
|
*/
|
|
|
|
clk_div = ((dwmci_readl(host, DWMCI_CLKSEL) >> DWMCI_DIVRATIO_BIT)
|
|
|
|
& DWMCI_DIVRATIO_MASK) + 1;
|
|
|
|
sclk = get_mmc_clk(host->dev_index);
|
|
|
|
|
2014-05-16 04:59:52 +00:00
|
|
|
/*
|
|
|
|
* Assume to know divider value.
|
|
|
|
* When clock unit is broken, need to set "host->div"
|
|
|
|
*/
|
|
|
|
return sclk / clk_div / (host->div + 1);
|
2012-10-15 19:10:31 +00:00
|
|
|
}
|
|
|
|
|
2013-11-29 11:08:57 +00:00
|
|
|
static void exynos_dwmci_board_init(struct dwmci_host *host)
|
|
|
|
{
|
|
|
|
if (host->quirks & DWMCI_QUIRK_DISABLE_SMU) {
|
|
|
|
dwmci_writel(host, EMMCP_MPSBEGIN0, 0);
|
|
|
|
dwmci_writel(host, EMMCP_SEND0, 0);
|
|
|
|
dwmci_writel(host, EMMCP_CTRL0,
|
|
|
|
MPSCTRL_SECURE_READ_BIT |
|
|
|
|
MPSCTRL_SECURE_WRITE_BIT |
|
|
|
|
MPSCTRL_NON_SECURE_READ_BIT |
|
|
|
|
MPSCTRL_NON_SECURE_WRITE_BIT | MPSCTRL_VALID);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2014-05-16 04:59:52 +00:00
|
|
|
static int exynos_dwmci_core_init(struct dwmci_host *host, int index)
|
2012-10-15 19:10:31 +00:00
|
|
|
{
|
2013-04-27 06:12:55 +00:00
|
|
|
unsigned int div;
|
|
|
|
unsigned long freq, sclk;
|
2014-05-16 04:59:52 +00:00
|
|
|
|
|
|
|
if (host->bus_hz)
|
|
|
|
freq = host->bus_hz;
|
|
|
|
else
|
|
|
|
freq = DWMMC_MAX_FREQ;
|
|
|
|
|
2013-04-27 06:12:55 +00:00
|
|
|
/* request mmc clock vlaue of 52MHz. */
|
|
|
|
sclk = get_mmc_clk(index);
|
|
|
|
div = DIV_ROUND_UP(sclk, freq);
|
|
|
|
/* set the clock divisor for mmc */
|
|
|
|
set_mmc_clk(index, div);
|
2012-10-15 19:10:31 +00:00
|
|
|
|
2013-04-27 06:12:55 +00:00
|
|
|
host->name = "EXYNOS DWMMC";
|
2013-10-29 07:23:13 +00:00
|
|
|
#ifdef CONFIG_EXYNOS5420
|
|
|
|
host->quirks = DWMCI_QUIRK_DISABLE_SMU;
|
|
|
|
#endif
|
2013-11-29 11:08:57 +00:00
|
|
|
host->board_init = exynos_dwmci_board_init;
|
2013-04-27 06:12:55 +00:00
|
|
|
|
2014-05-16 04:59:52 +00:00
|
|
|
if (!host->clksel_val) {
|
|
|
|
if (index == 0)
|
2013-04-27 06:12:55 +00:00
|
|
|
host->clksel_val = DWMMC_MMC0_CLKSEL_VAL;
|
2014-05-16 04:59:52 +00:00
|
|
|
else if (index == 2)
|
2013-04-27 06:12:55 +00:00
|
|
|
host->clksel_val = DWMMC_MMC2_CLKSEL_VAL;
|
|
|
|
}
|
|
|
|
|
2014-05-16 04:59:57 +00:00
|
|
|
host->caps = MMC_MODE_DDR_52MHz;
|
2012-10-15 19:10:31 +00:00
|
|
|
host->clksel = exynos_dwmci_clksel;
|
|
|
|
host->dev_index = index;
|
2013-10-06 09:59:31 +00:00
|
|
|
host->get_mmc_clk = exynos_dwmci_get_clk;
|
2013-04-27 06:12:55 +00:00
|
|
|
/* Add the mmc channel to be registered with mmc core */
|
|
|
|
if (add_dwmci(host, DWMMC_MAX_FREQ, DWMMC_MIN_FREQ)) {
|
2014-11-28 11:42:33 +00:00
|
|
|
printf("DWMMC%d registration failed\n", index);
|
2013-04-27 06:12:55 +00:00
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2014-05-16 04:59:52 +00:00
|
|
|
/*
|
|
|
|
* This function adds the mmc channel to be registered with mmc core.
|
|
|
|
* index - mmc channel number.
|
|
|
|
* regbase - register base address of mmc channel specified in 'index'.
|
|
|
|
* bus_width - operating bus width of mmc channel specified in 'index'.
|
|
|
|
* clksel - value to be written into CLKSEL register in case of FDT.
|
|
|
|
* NULL in case od non-FDT.
|
|
|
|
*/
|
|
|
|
int exynos_dwmci_add_port(int index, u32 regbase, int bus_width, u32 clksel)
|
|
|
|
{
|
|
|
|
struct dwmci_host *host = NULL;
|
|
|
|
|
|
|
|
host = malloc(sizeof(struct dwmci_host));
|
|
|
|
if (!host) {
|
|
|
|
error("dwmci_host malloc fail!\n");
|
|
|
|
return -ENOMEM;
|
|
|
|
}
|
|
|
|
|
|
|
|
host->ioaddr = (void *)regbase;
|
|
|
|
host->buswidth = bus_width;
|
|
|
|
|
|
|
|
if (clksel)
|
|
|
|
host->clksel_val = clksel;
|
|
|
|
|
|
|
|
return exynos_dwmci_core_init(host, index);
|
|
|
|
}
|
|
|
|
|
2013-04-27 06:12:55 +00:00
|
|
|
#ifdef CONFIG_OF_CONTROL
|
2014-05-16 04:59:52 +00:00
|
|
|
static struct dwmci_host dwmci_host[DWMMC_MAX_CH_NUM];
|
|
|
|
|
|
|
|
static int do_dwmci_init(struct dwmci_host *host)
|
2013-04-27 06:12:55 +00:00
|
|
|
{
|
2014-05-16 04:59:52 +00:00
|
|
|
int index, flag, err;
|
2013-04-27 06:12:55 +00:00
|
|
|
|
2014-05-16 04:59:52 +00:00
|
|
|
index = host->dev_index;
|
2013-04-27 06:12:55 +00:00
|
|
|
|
2014-05-16 04:59:52 +00:00
|
|
|
flag = host->buswidth == 8 ? PINMUX_FLAG_8BIT_MODE : PINMUX_FLAG_NONE;
|
|
|
|
err = exynos_pinmux_config(host->dev_id, flag);
|
|
|
|
if (err) {
|
2014-11-28 11:42:33 +00:00
|
|
|
printf("DWMMC%d not configure\n", index);
|
2014-05-16 04:59:52 +00:00
|
|
|
return err;
|
|
|
|
}
|
2013-04-27 06:12:55 +00:00
|
|
|
|
2014-05-16 04:59:52 +00:00
|
|
|
return exynos_dwmci_core_init(host, index);
|
|
|
|
}
|
2012-10-15 19:10:31 +00:00
|
|
|
|
2014-05-16 04:59:52 +00:00
|
|
|
static int exynos_dwmci_get_config(const void *blob, int node,
|
|
|
|
struct dwmci_host *host)
|
|
|
|
{
|
|
|
|
int err = 0;
|
|
|
|
u32 base, clksel_val, timing[3];
|
2012-10-15 19:10:31 +00:00
|
|
|
|
2014-05-16 04:59:52 +00:00
|
|
|
/* Extract device id for each mmc channel */
|
|
|
|
host->dev_id = pinmux_decode_periph_id(blob, node);
|
2013-04-27 06:12:55 +00:00
|
|
|
|
2014-11-28 11:42:33 +00:00
|
|
|
host->dev_index = fdtdec_get_int(blob, node, "index", host->dev_id);
|
|
|
|
if (host->dev_index == host->dev_id)
|
|
|
|
host->dev_index = host->dev_id - PERIPH_ID_SDMMC0;
|
|
|
|
|
|
|
|
|
2014-05-16 04:59:52 +00:00
|
|
|
/* Get the bus width from the device node */
|
|
|
|
host->buswidth = fdtdec_get_int(blob, node, "samsung,bus-width", 0);
|
|
|
|
if (host->buswidth <= 0) {
|
2014-11-28 11:42:33 +00:00
|
|
|
printf("DWMMC%d: Can't get bus-width\n", host->dev_index);
|
2014-05-16 04:59:52 +00:00
|
|
|
return -EINVAL;
|
|
|
|
}
|
2013-04-27 06:12:55 +00:00
|
|
|
|
2014-05-16 04:59:52 +00:00
|
|
|
/* Set the base address from the device node */
|
|
|
|
base = fdtdec_get_addr(blob, node, "reg");
|
|
|
|
if (!base) {
|
2014-11-28 11:42:33 +00:00
|
|
|
printf("DWMMC%d: Can't get base address\n", host->dev_index);
|
2014-05-16 04:59:52 +00:00
|
|
|
return -EINVAL;
|
|
|
|
}
|
|
|
|
host->ioaddr = (void *)base;
|
|
|
|
|
|
|
|
/* Extract the timing info from the node */
|
|
|
|
err = fdtdec_get_int_array(blob, node, "samsung,timing", timing, 3);
|
|
|
|
if (err) {
|
2014-11-28 11:42:33 +00:00
|
|
|
printf("DWMMC%d: Can't get sdr-timings for devider\n",
|
|
|
|
host->dev_index);
|
2014-05-16 04:59:52 +00:00
|
|
|
return -EINVAL;
|
|
|
|
}
|
|
|
|
|
|
|
|
clksel_val = (DWMCI_SET_SAMPLE_CLK(timing[0]) |
|
|
|
|
DWMCI_SET_DRV_CLK(timing[1]) |
|
|
|
|
DWMCI_SET_DIV_RATIO(timing[2]));
|
|
|
|
if (clksel_val)
|
|
|
|
host->clksel_val = clksel_val;
|
|
|
|
|
|
|
|
host->fifoth_val = fdtdec_get_int(blob, node, "fifoth_val", 0);
|
|
|
|
host->bus_hz = fdtdec_get_int(blob, node, "bus_hz", 0);
|
|
|
|
host->div = fdtdec_get_int(blob, node, "div", 0);
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
static int exynos_dwmci_process_node(const void *blob,
|
|
|
|
int node_list[], int count)
|
|
|
|
{
|
|
|
|
struct dwmci_host *host;
|
|
|
|
int i, node, err;
|
|
|
|
|
|
|
|
for (i = 0; i < count; i++) {
|
|
|
|
node = node_list[i];
|
|
|
|
if (node <= 0)
|
|
|
|
continue;
|
|
|
|
host = &dwmci_host[i];
|
|
|
|
err = exynos_dwmci_get_config(blob, node, host);
|
2013-04-27 06:12:55 +00:00
|
|
|
if (err) {
|
2014-11-28 11:42:33 +00:00
|
|
|
printf("%s: failed to decode dev %d\n", __func__, i);
|
2014-05-16 04:59:52 +00:00
|
|
|
return err;
|
2013-04-27 06:12:55 +00:00
|
|
|
}
|
|
|
|
|
2014-05-16 04:59:52 +00:00
|
|
|
do_dwmci_init(host);
|
2013-04-27 06:12:55 +00:00
|
|
|
}
|
2012-10-15 19:10:31 +00:00
|
|
|
return 0;
|
|
|
|
}
|
2014-05-16 04:59:52 +00:00
|
|
|
|
|
|
|
int exynos_dwmmc_init(const void *blob)
|
|
|
|
{
|
|
|
|
int compat_id;
|
|
|
|
int node_list[DWMMC_MAX_CH_NUM];
|
|
|
|
int err = 0, count;
|
|
|
|
|
|
|
|
compat_id = COMPAT_SAMSUNG_EXYNOS_DWMMC;
|
|
|
|
|
|
|
|
count = fdtdec_find_aliases_for_id(blob, "mmc",
|
|
|
|
compat_id, node_list, DWMMC_MAX_CH_NUM);
|
|
|
|
err = exynos_dwmci_process_node(blob, node_list, count);
|
|
|
|
|
|
|
|
return err;
|
|
|
|
}
|
2013-04-27 06:12:55 +00:00
|
|
|
#endif
|