2018-05-06 21:58:06 +00:00
|
|
|
// SPDX-License-Identifier: GPL-2.0+
|
2016-05-19 13:56:44 +00:00
|
|
|
/*
|
|
|
|
* Copyright (C) 2015 Marvell International Ltd.
|
|
|
|
*
|
|
|
|
* Copyright (C) 2016 Stefan Roese <sr@denx.de>
|
|
|
|
*/
|
|
|
|
|
|
|
|
#include <common.h>
|
|
|
|
#include <dm.h>
|
2020-05-10 17:40:05 +00:00
|
|
|
#include <log.h>
|
2016-05-19 13:56:44 +00:00
|
|
|
#include <malloc.h>
|
|
|
|
#include <spi.h>
|
2018-04-24 15:21:26 +00:00
|
|
|
#include <clk.h>
|
2016-05-19 13:56:44 +00:00
|
|
|
#include <wait_bit.h>
|
2020-10-31 03:38:53 +00:00
|
|
|
#include <asm/global_data.h>
|
2016-05-19 13:56:44 +00:00
|
|
|
#include <asm/io.h>
|
2020-02-03 14:36:16 +00:00
|
|
|
#include <dm/device_compat.h>
|
2020-05-10 17:40:13 +00:00
|
|
|
#include <linux/bitops.h>
|
2020-09-30 14:28:21 +00:00
|
|
|
#include <asm/gpio.h>
|
2016-05-19 13:56:44 +00:00
|
|
|
|
|
|
|
DECLARE_GLOBAL_DATA_PTR;
|
|
|
|
|
|
|
|
#define MVEBU_SPI_A3700_XFER_RDY BIT(1)
|
|
|
|
#define MVEBU_SPI_A3700_FIFO_FLUSH BIT(9)
|
|
|
|
#define MVEBU_SPI_A3700_BYTE_LEN BIT(5)
|
|
|
|
#define MVEBU_SPI_A3700_CLK_PHA BIT(6)
|
|
|
|
#define MVEBU_SPI_A3700_CLK_POL BIT(7)
|
|
|
|
#define MVEBU_SPI_A3700_FIFO_EN BIT(17)
|
|
|
|
#define MVEBU_SPI_A3700_SPI_EN_0 BIT(16)
|
2018-04-24 15:21:26 +00:00
|
|
|
#define MVEBU_SPI_A3700_CLK_PRESCALE_MASK 0x1f
|
|
|
|
|
2020-09-30 14:28:21 +00:00
|
|
|
#define MAX_CS_COUNT 4
|
2016-05-19 13:56:44 +00:00
|
|
|
|
|
|
|
/* SPI registers */
|
|
|
|
struct spi_reg {
|
|
|
|
u32 ctrl; /* 0x10600 */
|
|
|
|
u32 cfg; /* 0x10604 */
|
|
|
|
u32 dout; /* 0x10608 */
|
|
|
|
u32 din; /* 0x1060c */
|
|
|
|
};
|
|
|
|
|
2020-12-03 23:55:23 +00:00
|
|
|
struct mvebu_spi_plat {
|
2016-05-19 13:56:44 +00:00
|
|
|
struct spi_reg *spireg;
|
2018-04-24 15:21:26 +00:00
|
|
|
struct clk clk;
|
2020-09-30 14:28:21 +00:00
|
|
|
struct gpio_desc cs_gpios[MAX_CS_COUNT];
|
2016-05-19 13:56:44 +00:00
|
|
|
};
|
|
|
|
|
2020-12-03 23:55:23 +00:00
|
|
|
static void spi_cs_activate(struct mvebu_spi_plat *plat, int cs)
|
2016-05-19 13:56:44 +00:00
|
|
|
{
|
2020-09-30 14:28:21 +00:00
|
|
|
if (CONFIG_IS_ENABLED(DM_GPIO) && dm_gpio_is_valid(&plat->cs_gpios[cs]))
|
|
|
|
dm_gpio_set_value(&plat->cs_gpios[cs], 1);
|
|
|
|
else
|
|
|
|
setbits_le32(&plat->spireg->ctrl, MVEBU_SPI_A3700_SPI_EN_0 << cs);
|
2016-05-19 13:56:44 +00:00
|
|
|
}
|
|
|
|
|
2020-12-03 23:55:23 +00:00
|
|
|
static void spi_cs_deactivate(struct mvebu_spi_plat *plat, int cs)
|
2016-05-19 13:56:44 +00:00
|
|
|
{
|
2020-09-30 14:28:21 +00:00
|
|
|
if (CONFIG_IS_ENABLED(DM_GPIO) && dm_gpio_is_valid(&plat->cs_gpios[cs]))
|
|
|
|
dm_gpio_set_value(&plat->cs_gpios[cs], 0);
|
|
|
|
else
|
|
|
|
clrbits_le32(&plat->spireg->ctrl, MVEBU_SPI_A3700_SPI_EN_0 << cs);
|
2016-05-19 13:56:44 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* spi_legacy_shift_byte() - triggers the real SPI transfer
|
|
|
|
* @bytelen: Indicate how many bytes to transfer.
|
|
|
|
* @dout: Buffer address of what to send.
|
|
|
|
* @din: Buffer address of where to receive.
|
|
|
|
*
|
|
|
|
* This function triggers the real SPI transfer in legacy mode. It
|
|
|
|
* will shift out char buffer from @dout, and shift in char buffer to
|
|
|
|
* @din, if necessary.
|
|
|
|
*
|
|
|
|
* This function assumes that only one byte is shifted at one time.
|
|
|
|
* However, it is not its responisbility to set the transfer type to
|
|
|
|
* one-byte. Also, it does not guarantee that it will work if transfer
|
|
|
|
* type becomes two-byte. See spi_set_legacy() for details.
|
|
|
|
*
|
|
|
|
* In legacy mode, simply write to the SPI_DOUT register will trigger
|
|
|
|
* the transfer.
|
|
|
|
*
|
|
|
|
* If @dout == NULL, which means no actual data needs to be sent out,
|
|
|
|
* then the function will shift out 0x00 in order to shift in data.
|
|
|
|
* The XFER_RDY flag is checked every time before accessing SPI_DOUT
|
|
|
|
* and SPI_DIN register.
|
|
|
|
*
|
|
|
|
* The number of transfers to be triggerred is decided by @bytelen.
|
|
|
|
*
|
|
|
|
* Return: 0 - cool
|
|
|
|
* -ETIMEDOUT - XFER_RDY flag timeout
|
|
|
|
*/
|
|
|
|
static int spi_legacy_shift_byte(struct spi_reg *reg, unsigned int bytelen,
|
|
|
|
const void *dout, void *din)
|
|
|
|
{
|
|
|
|
const u8 *dout_8;
|
|
|
|
u8 *din_8;
|
|
|
|
int ret;
|
|
|
|
|
|
|
|
/* Use 0x00 as dummy dout */
|
|
|
|
const u8 dummy_dout = 0x0;
|
|
|
|
u32 pending_dout = 0x0;
|
|
|
|
|
|
|
|
/* dout_8: pointer of current dout */
|
|
|
|
dout_8 = dout;
|
|
|
|
/* din_8: pointer of current din */
|
|
|
|
din_8 = din;
|
|
|
|
|
|
|
|
while (bytelen) {
|
2018-01-23 16:14:55 +00:00
|
|
|
ret = wait_for_bit_le32(®->ctrl,
|
|
|
|
MVEBU_SPI_A3700_XFER_RDY,
|
|
|
|
true,100, false);
|
2016-05-19 13:56:44 +00:00
|
|
|
if (ret)
|
|
|
|
return ret;
|
|
|
|
|
|
|
|
if (dout)
|
|
|
|
pending_dout = (u32)*dout_8;
|
|
|
|
else
|
|
|
|
pending_dout = (u32)dummy_dout;
|
|
|
|
|
|
|
|
/* Trigger the xfer */
|
|
|
|
writel(pending_dout, ®->dout);
|
|
|
|
|
|
|
|
if (din) {
|
2018-01-23 16:14:55 +00:00
|
|
|
ret = wait_for_bit_le32(®->ctrl,
|
|
|
|
MVEBU_SPI_A3700_XFER_RDY,
|
|
|
|
true, 100, false);
|
2016-05-19 13:56:44 +00:00
|
|
|
if (ret)
|
|
|
|
return ret;
|
|
|
|
|
|
|
|
/* Read what is transferred in */
|
|
|
|
*din_8 = (u8)readl(®->din);
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Don't increment the current pointer if NULL */
|
|
|
|
if (dout)
|
|
|
|
dout_8++;
|
|
|
|
if (din)
|
|
|
|
din_8++;
|
|
|
|
|
|
|
|
bytelen--;
|
|
|
|
}
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
static int mvebu_spi_xfer(struct udevice *dev, unsigned int bitlen,
|
|
|
|
const void *dout, void *din, unsigned long flags)
|
|
|
|
{
|
|
|
|
struct udevice *bus = dev->parent;
|
2020-12-03 23:55:23 +00:00
|
|
|
struct mvebu_spi_plat *plat = dev_get_plat(bus);
|
2016-05-19 13:56:44 +00:00
|
|
|
struct spi_reg *reg = plat->spireg;
|
|
|
|
unsigned int bytelen;
|
|
|
|
int ret;
|
|
|
|
|
|
|
|
bytelen = bitlen / 8;
|
|
|
|
|
|
|
|
if (dout && din)
|
|
|
|
debug("This is a duplex transfer.\n");
|
|
|
|
|
|
|
|
/* Activate CS */
|
|
|
|
if (flags & SPI_XFER_BEGIN) {
|
|
|
|
debug("SPI: activate cs.\n");
|
2020-09-30 14:28:21 +00:00
|
|
|
spi_cs_activate(plat, spi_chip_select(dev));
|
2016-05-19 13:56:44 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/* Send and/or receive */
|
|
|
|
if (dout || din) {
|
|
|
|
ret = spi_legacy_shift_byte(reg, bytelen, dout, din);
|
|
|
|
if (ret)
|
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Deactivate CS */
|
|
|
|
if (flags & SPI_XFER_END) {
|
2018-01-23 16:14:55 +00:00
|
|
|
ret = wait_for_bit_le32(®->ctrl,
|
|
|
|
MVEBU_SPI_A3700_XFER_RDY,
|
|
|
|
true, 100, false);
|
2016-05-19 13:56:44 +00:00
|
|
|
if (ret)
|
|
|
|
return ret;
|
|
|
|
|
|
|
|
debug("SPI: deactivate cs.\n");
|
2020-09-30 14:28:21 +00:00
|
|
|
spi_cs_deactivate(plat, spi_chip_select(dev));
|
2016-05-19 13:56:44 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
static int mvebu_spi_set_speed(struct udevice *bus, uint hz)
|
|
|
|
{
|
2020-12-03 23:55:23 +00:00
|
|
|
struct mvebu_spi_plat *plat = dev_get_plat(bus);
|
2016-05-19 13:56:44 +00:00
|
|
|
struct spi_reg *reg = plat->spireg;
|
2018-04-24 15:21:26 +00:00
|
|
|
u32 data, prescale;
|
2016-05-19 13:56:44 +00:00
|
|
|
|
|
|
|
data = readl(®->cfg);
|
|
|
|
|
2018-04-24 15:21:26 +00:00
|
|
|
prescale = DIV_ROUND_UP(clk_get_rate(&plat->clk), hz);
|
2019-07-23 14:49:32 +00:00
|
|
|
if (prescale > 0xf)
|
2018-04-24 15:21:26 +00:00
|
|
|
prescale = 0x10 + (prescale + 1) / 2;
|
2019-07-23 14:49:32 +00:00
|
|
|
prescale = min(prescale, 0x1fu);
|
2016-05-19 13:56:44 +00:00
|
|
|
|
2018-04-24 15:21:26 +00:00
|
|
|
data &= ~MVEBU_SPI_A3700_CLK_PRESCALE_MASK;
|
|
|
|
data |= prescale & MVEBU_SPI_A3700_CLK_PRESCALE_MASK;
|
2016-05-19 13:56:44 +00:00
|
|
|
|
|
|
|
writel(data, ®->cfg);
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
static int mvebu_spi_set_mode(struct udevice *bus, uint mode)
|
|
|
|
{
|
2020-12-03 23:55:23 +00:00
|
|
|
struct mvebu_spi_plat *plat = dev_get_plat(bus);
|
2016-05-19 13:56:44 +00:00
|
|
|
struct spi_reg *reg = plat->spireg;
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Set SPI polarity
|
|
|
|
* 0: Serial interface clock is low when inactive
|
|
|
|
* 1: Serial interface clock is high when inactive
|
|
|
|
*/
|
|
|
|
if (mode & SPI_CPOL)
|
|
|
|
setbits_le32(®->cfg, MVEBU_SPI_A3700_CLK_POL);
|
|
|
|
else
|
|
|
|
clrbits_le32(®->cfg, MVEBU_SPI_A3700_CLK_POL);
|
|
|
|
if (mode & SPI_CPHA)
|
|
|
|
setbits_le32(®->cfg, MVEBU_SPI_A3700_CLK_PHA);
|
|
|
|
else
|
|
|
|
clrbits_le32(®->cfg, MVEBU_SPI_A3700_CLK_PHA);
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
static int mvebu_spi_probe(struct udevice *bus)
|
|
|
|
{
|
2020-12-03 23:55:23 +00:00
|
|
|
struct mvebu_spi_plat *plat = dev_get_plat(bus);
|
2016-05-19 13:56:44 +00:00
|
|
|
struct spi_reg *reg = plat->spireg;
|
|
|
|
u32 data;
|
|
|
|
int ret;
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Settings SPI controller to be working in legacy mode, which
|
|
|
|
* means use only DO pin (I/O 1) for Data Out, and DI pin (I/O 0)
|
|
|
|
* for Data In.
|
|
|
|
*/
|
|
|
|
|
|
|
|
/* Flush read/write FIFO */
|
|
|
|
data = readl(®->cfg);
|
|
|
|
writel(data | MVEBU_SPI_A3700_FIFO_FLUSH, ®->cfg);
|
2018-01-23 16:14:55 +00:00
|
|
|
ret = wait_for_bit_le32(®->cfg, MVEBU_SPI_A3700_FIFO_FLUSH,
|
|
|
|
false, 1000, false);
|
2016-05-19 13:56:44 +00:00
|
|
|
if (ret)
|
|
|
|
return ret;
|
|
|
|
|
|
|
|
/* Disable FIFO mode */
|
|
|
|
data &= ~MVEBU_SPI_A3700_FIFO_EN;
|
|
|
|
|
|
|
|
/* Always shift 1 byte at a time */
|
|
|
|
data &= ~MVEBU_SPI_A3700_BYTE_LEN;
|
|
|
|
|
|
|
|
writel(data, ®->cfg);
|
|
|
|
|
2020-09-30 14:28:21 +00:00
|
|
|
/* Set up CS GPIOs in device tree, if any */
|
|
|
|
if (CONFIG_IS_ENABLED(DM_GPIO) && gpio_get_list_count(bus, "cs-gpios") > 0) {
|
|
|
|
int i;
|
|
|
|
|
|
|
|
for (i = 0; i < ARRAY_SIZE(plat->cs_gpios); i++) {
|
|
|
|
ret = gpio_request_by_name(bus, "cs-gpios", i, &plat->cs_gpios[i], 0);
|
|
|
|
if (ret < 0 || !dm_gpio_is_valid(&plat->cs_gpios[i])) {
|
|
|
|
/* Use the native CS function for this line */
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
|
|
|
ret = dm_gpio_set_dir_flags(&plat->cs_gpios[i],
|
|
|
|
GPIOD_IS_OUT | GPIOD_ACTIVE_LOW);
|
|
|
|
if (ret) {
|
|
|
|
dev_err(bus, "Setting cs %d error\n", i);
|
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-05-19 13:56:44 +00:00
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2020-12-03 23:55:21 +00:00
|
|
|
static int mvebu_spi_of_to_plat(struct udevice *bus)
|
2016-05-19 13:56:44 +00:00
|
|
|
{
|
2020-12-03 23:55:23 +00:00
|
|
|
struct mvebu_spi_plat *plat = dev_get_plat(bus);
|
2018-04-24 15:21:26 +00:00
|
|
|
int ret;
|
2016-05-19 13:56:44 +00:00
|
|
|
|
2020-07-17 05:36:46 +00:00
|
|
|
plat->spireg = dev_read_addr_ptr(bus);
|
2016-05-19 13:56:44 +00:00
|
|
|
|
2018-04-24 15:21:26 +00:00
|
|
|
ret = clk_get_by_index(bus, 0, &plat->clk);
|
|
|
|
if (ret) {
|
|
|
|
dev_err(bus, "cannot get clock\n");
|
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
static int mvebu_spi_remove(struct udevice *bus)
|
|
|
|
{
|
2020-12-03 23:55:23 +00:00
|
|
|
struct mvebu_spi_plat *plat = dev_get_plat(bus);
|
2018-04-24 15:21:26 +00:00
|
|
|
|
|
|
|
clk_free(&plat->clk);
|
2016-05-19 13:56:44 +00:00
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
static const struct dm_spi_ops mvebu_spi_ops = {
|
|
|
|
.xfer = mvebu_spi_xfer,
|
|
|
|
.set_speed = mvebu_spi_set_speed,
|
|
|
|
.set_mode = mvebu_spi_set_mode,
|
|
|
|
/*
|
|
|
|
* cs_info is not needed, since we require all chip selects to be
|
|
|
|
* in the device tree explicitly
|
|
|
|
*/
|
|
|
|
};
|
|
|
|
|
|
|
|
static const struct udevice_id mvebu_spi_ids[] = {
|
|
|
|
{ .compatible = "marvell,armada-3700-spi" },
|
|
|
|
{ }
|
|
|
|
};
|
|
|
|
|
|
|
|
U_BOOT_DRIVER(mvebu_spi) = {
|
|
|
|
.name = "mvebu_spi",
|
|
|
|
.id = UCLASS_SPI,
|
|
|
|
.of_match = mvebu_spi_ids,
|
|
|
|
.ops = &mvebu_spi_ops,
|
2020-12-03 23:55:21 +00:00
|
|
|
.of_to_plat = mvebu_spi_of_to_plat,
|
2020-12-03 23:55:23 +00:00
|
|
|
.plat_auto = sizeof(struct mvebu_spi_plat),
|
2016-05-19 13:56:44 +00:00
|
|
|
.probe = mvebu_spi_probe,
|
2018-04-24 15:21:26 +00:00
|
|
|
.remove = mvebu_spi_remove,
|
2016-05-19 13:56:44 +00:00
|
|
|
};
|