diff options
| author | Beniamino Galvani <b.galvani@gmail.com> | 2014-11-22 10:21:41 -0500 |
|---|---|---|
| committer | Mark Brown <broonie@kernel.org> | 2014-11-24 13:54:48 -0500 |
| commit | c3e4bc5434d2999ec3b00eadd1945eadf13fd0de (patch) | |
| tree | 6f4b07ee0191e457d7ad2dcc53925640359bfd40 | |
| parent | 56da2aafe0a6db710c38a2fbf6aa9352a7b9fa4d (diff) | |
spi: meson: Add support for Amlogic Meson SPIFC
This is a driver for the Amlogic Meson SPIFC (SPI flash controller),
which is one of the two SPI controllers available on the SoC. It
doesn't support DMA and has a 64-byte unified transmit/receive buffer.
The device is optimized for interfacing with SPI NOR memories and
allows the execution of standard operations such as read, page
program, sector erase, etc. in a simplified way, toggling a bit in a
dedicated register. The driver doesn't use those predefined commands
and relies only on custom transfers.
Signed-off-by: Beniamino Galvani <b.galvani@gmail.com>
Signed-off-by: Mark Brown <broonie@kernel.org>
| -rw-r--r-- | drivers/spi/Kconfig | 7 | ||||
| -rw-r--r-- | drivers/spi/Makefile | 1 | ||||
| -rw-r--r-- | drivers/spi/spi-meson-spifc.c | 462 |
3 files changed, 470 insertions, 0 deletions
diff --git a/drivers/spi/Kconfig b/drivers/spi/Kconfig index 84e7c9e6ccef..70f20b99e5ff 100644 --- a/drivers/spi/Kconfig +++ b/drivers/spi/Kconfig | |||
| @@ -301,6 +301,13 @@ config SPI_FSL_ESPI | |||
| 301 | From MPC8536, 85xx platform uses the controller, and all P10xx, | 301 | From MPC8536, 85xx platform uses the controller, and all P10xx, |
| 302 | P20xx, P30xx,P40xx, P50xx uses this controller. | 302 | P20xx, P30xx,P40xx, P50xx uses this controller. |
| 303 | 303 | ||
| 304 | config SPI_MESON_SPIFC | ||
| 305 | tristate "Amlogic Meson SPIFC controller" | ||
| 306 | depends on ARCH_MESON || COMPILE_TEST | ||
| 307 | help | ||
| 308 | This enables master mode support for the SPIFC (SPI flash | ||
| 309 | controller) available in Amlogic Meson SoCs. | ||
| 310 | |||
| 304 | config SPI_OC_TINY | 311 | config SPI_OC_TINY |
| 305 | tristate "OpenCores tiny SPI" | 312 | tristate "OpenCores tiny SPI" |
| 306 | depends on GPIOLIB | 313 | depends on GPIOLIB |
diff --git a/drivers/spi/Makefile b/drivers/spi/Makefile index 78f24ca36fcf..9b8a7472b5b8 100644 --- a/drivers/spi/Makefile +++ b/drivers/spi/Makefile | |||
| @@ -42,6 +42,7 @@ obj-$(CONFIG_SPI_FSL_SPI) += spi-fsl-spi.o | |||
| 42 | obj-$(CONFIG_SPI_GPIO) += spi-gpio.o | 42 | obj-$(CONFIG_SPI_GPIO) += spi-gpio.o |
| 43 | obj-$(CONFIG_SPI_IMX) += spi-imx.o | 43 | obj-$(CONFIG_SPI_IMX) += spi-imx.o |
| 44 | obj-$(CONFIG_SPI_LM70_LLP) += spi-lm70llp.o | 44 | obj-$(CONFIG_SPI_LM70_LLP) += spi-lm70llp.o |
| 45 | obj-$(CONFIG_SPI_MESON_SPIFC) += spi-meson-spifc.o | ||
| 45 | obj-$(CONFIG_SPI_MPC512x_PSC) += spi-mpc512x-psc.o | 46 | obj-$(CONFIG_SPI_MPC512x_PSC) += spi-mpc512x-psc.o |
| 46 | obj-$(CONFIG_SPI_MPC52xx_PSC) += spi-mpc52xx-psc.o | 47 | obj-$(CONFIG_SPI_MPC52xx_PSC) += spi-mpc52xx-psc.o |
| 47 | obj-$(CONFIG_SPI_MPC52xx) += spi-mpc52xx.o | 48 | obj-$(CONFIG_SPI_MPC52xx) += spi-mpc52xx.o |
diff --git a/drivers/spi/spi-meson-spifc.c b/drivers/spi/spi-meson-spifc.c new file mode 100644 index 000000000000..f536476710ad --- /dev/null +++ b/drivers/spi/spi-meson-spifc.c | |||
| @@ -0,0 +1,462 @@ | |||
| 1 | /* | ||
| 2 | * Driver for Amlogic Meson SPI flash controller (SPIFC) | ||
| 3 | * | ||
| 4 | * Copyright (C) 2014 Beniamino Galvani <b.galvani@gmail.com> | ||
| 5 | * | ||
| 6 | * This program is free software; you can redistribute it and/or | ||
| 7 | * modify it under the terms of the GNU General Public License | ||
| 8 | * version 2 as published by the Free Software Foundation. | ||
| 9 | * | ||
| 10 | * You should have received a copy of the GNU General Public License | ||
| 11 | * along with this program. If not, see <http://www.gnu.org/licenses/>. | ||
| 12 | */ | ||
| 13 | |||
| 14 | #include <linux/clk.h> | ||
| 15 | #include <linux/delay.h> | ||
| 16 | #include <linux/device.h> | ||
| 17 | #include <linux/io.h> | ||
| 18 | #include <linux/kernel.h> | ||
| 19 | #include <linux/module.h> | ||
| 20 | #include <linux/of.h> | ||
| 21 | #include <linux/platform_device.h> | ||
| 22 | #include <linux/pm_runtime.h> | ||
| 23 | #include <linux/regmap.h> | ||
| 24 | #include <linux/spi/spi.h> | ||
| 25 | #include <linux/types.h> | ||
| 26 | |||
| 27 | /* register map */ | ||
| 28 | #define REG_CMD 0x00 | ||
| 29 | #define REG_ADDR 0x04 | ||
| 30 | #define REG_CTRL 0x08 | ||
| 31 | #define REG_CTRL1 0x0c | ||
| 32 | #define REG_STATUS 0x10 | ||
| 33 | #define REG_CTRL2 0x14 | ||
| 34 | #define REG_CLOCK 0x18 | ||
| 35 | #define REG_USER 0x1c | ||
| 36 | #define REG_USER1 0x20 | ||
| 37 | #define REG_USER2 0x24 | ||
| 38 | #define REG_USER3 0x28 | ||
| 39 | #define REG_USER4 0x2c | ||
| 40 | #define REG_SLAVE 0x30 | ||
| 41 | #define REG_SLAVE1 0x34 | ||
| 42 | #define REG_SLAVE2 0x38 | ||
| 43 | #define REG_SLAVE3 0x3c | ||
| 44 | #define REG_C0 0x40 | ||
| 45 | #define REG_B8 0x60 | ||
| 46 | #define REG_MAX 0x7c | ||
| 47 | |||
| 48 | /* register fields */ | ||
| 49 | #define CMD_USER BIT(18) | ||
| 50 | #define CTRL_ENABLE_AHB BIT(17) | ||
| 51 | #define CLOCK_SOURCE BIT(31) | ||
| 52 | #define CLOCK_DIV_SHIFT 12 | ||
| 53 | #define CLOCK_DIV_MASK (0x3f << CLOCK_DIV_SHIFT) | ||
| 54 | #define CLOCK_CNT_HIGH_SHIFT 6 | ||
| 55 | #define CLOCK_CNT_HIGH_MASK (0x3f << CLOCK_CNT_HIGH_SHIFT) | ||
| 56 | #define CLOCK_CNT_LOW_SHIFT 0 | ||
| 57 | #define CLOCK_CNT_LOW_MASK (0x3f << CLOCK_CNT_LOW_SHIFT) | ||
| 58 | #define USER_DIN_EN_MS BIT(0) | ||
| 59 | #define USER_CMP_MODE BIT(2) | ||
| 60 | #define USER_UC_DOUT_SEL BIT(27) | ||
| 61 | #define USER_UC_DIN_SEL BIT(28) | ||
| 62 | #define USER_UC_MASK ((BIT(5) - 1) << 27) | ||
| 63 | #define USER1_BN_UC_DOUT_SHIFT 17 | ||
| 64 | #define USER1_BN_UC_DOUT_MASK (0xff << 16) | ||
| 65 | #define USER1_BN_UC_DIN_SHIFT 8 | ||
| 66 | #define USER1_BN_UC_DIN_MASK (0xff << 8) | ||
| 67 | #define USER4_CS_ACT BIT(30) | ||
| 68 | #define SLAVE_TRST_DONE BIT(4) | ||
| 69 | #define SLAVE_OP_MODE BIT(30) | ||
| 70 | #define SLAVE_SW_RST BIT(31) | ||
| 71 | |||
| 72 | #define SPIFC_BUFFER_SIZE 64 | ||
| 73 | |||
| 74 | /** | ||
| 75 | * struct meson_spifc | ||
| 76 | * @master: the SPI master | ||
| 77 | * @regmap: regmap for device registers | ||
| 78 | * @clk: input clock of the built-in baud rate generator | ||
| 79 | * @device: the device structure | ||
| 80 | */ | ||
| 81 | struct meson_spifc { | ||
| 82 | struct spi_master *master; | ||
| 83 | struct regmap *regmap; | ||
| 84 | struct clk *clk; | ||
| 85 | struct device *dev; | ||
| 86 | }; | ||
| 87 | |||
| 88 | static struct regmap_config spifc_regmap_config = { | ||
| 89 | .reg_bits = 32, | ||
| 90 | .val_bits = 32, | ||
| 91 | .reg_stride = 4, | ||
| 92 | .max_register = REG_MAX, | ||
| 93 | }; | ||
| 94 | |||
| 95 | /** | ||
| 96 | * meson_spifc_wait_ready() - wait for the current operation to terminate | ||
| 97 | * @spifc: the Meson SPI device | ||
| 98 | * Return: 0 on success, a negative value on error | ||
| 99 | */ | ||
| 100 | static int meson_spifc_wait_ready(struct meson_spifc *spifc) | ||
| 101 | { | ||
| 102 | unsigned long deadline = jiffies + msecs_to_jiffies(5); | ||
| 103 | u32 data; | ||
| 104 | |||
| 105 | do { | ||
| 106 | regmap_read(spifc->regmap, REG_SLAVE, &data); | ||
| 107 | if (data & SLAVE_TRST_DONE) | ||
| 108 | return 0; | ||
| 109 | cond_resched(); | ||
| 110 | } while (!time_after(jiffies, deadline)); | ||
| 111 | |||
| 112 | return -ETIMEDOUT; | ||
| 113 | } | ||
| 114 | |||
| 115 | /** | ||
| 116 | * meson_spifc_drain_buffer() - copy data from device buffer to memory | ||
| 117 | * @spifc: the Meson SPI device | ||
| 118 | * @buf: the destination buffer | ||
| 119 | * @len: number of bytes to copy | ||
| 120 | */ | ||
| 121 | static void meson_spifc_drain_buffer(struct meson_spifc *spifc, u8 *buf, | ||
| 122 | int len) | ||
| 123 | { | ||
| 124 | u32 data; | ||
| 125 | int i = 0; | ||
| 126 | |||
| 127 | while (i < len) { | ||
| 128 | regmap_read(spifc->regmap, REG_C0 + i, &data); | ||
| 129 | |||
| 130 | if (len - i >= 4) { | ||
| 131 | *((u32 *)buf) = data; | ||
| 132 | buf += 4; | ||
| 133 | } else { | ||
| 134 | memcpy(buf, &data, len - i); | ||
| 135 | break; | ||
| 136 | } | ||
| 137 | i += 4; | ||
| 138 | } | ||
| 139 | } | ||
| 140 | |||
| 141 | /** | ||
| 142 | * meson_spifc_fill_buffer() - copy data from memory to device buffer | ||
| 143 | * @spifc: the Meson SPI device | ||
| 144 | * @buf: the source buffer | ||
| 145 | * @len: number of bytes to copy | ||
| 146 | */ | ||
| 147 | static void meson_spifc_fill_buffer(struct meson_spifc *spifc, const u8 *buf, | ||
| 148 | int len) | ||
| 149 | { | ||
| 150 | u32 data; | ||
| 151 | int i = 0; | ||
| 152 | |||
| 153 | while (i < len) { | ||
| 154 | if (len - i >= 4) | ||
| 155 | data = *(u32 *)buf; | ||
| 156 | else | ||
| 157 | memcpy(&data, buf, len - i); | ||
| 158 | |||
| 159 | regmap_write(spifc->regmap, REG_C0 + i, data); | ||
| 160 | |||
| 161 | buf += 4; | ||
| 162 | i += 4; | ||
| 163 | } | ||
| 164 | } | ||
| 165 | |||
| 166 | /** | ||
| 167 | * meson_spifc_setup_speed() - program the clock divider | ||
| 168 | * @spifc: the Meson SPI device | ||
| 169 | * @speed: desired speed in Hz | ||
| 170 | */ | ||
| 171 | void meson_spifc_setup_speed(struct meson_spifc *spifc, u32 speed) | ||
| 172 | { | ||
| 173 | unsigned long parent, value; | ||
| 174 | int n; | ||
| 175 | |||
| 176 | parent = clk_get_rate(spifc->clk); | ||
| 177 | n = max_t(int, parent / speed - 1, 1); | ||
| 178 | |||
