2018-05-07 05:58:06 +08:00
|
|
|
// SPDX-License-Identifier: GPL-2.0+
|
2010-04-30 11:34:16 +08:00
|
|
|
/*
|
|
|
|
* Altera SPI driver
|
|
|
|
*
|
|
|
|
* based on bfin_spi.c
|
|
|
|
* Copyright (c) 2005-2008 Analog Devices Inc.
|
|
|
|
* Copyright (C) 2010 Thomas Chou <thomas@wytron.com.tw>
|
|
|
|
*/
|
|
|
|
#include <common.h>
|
2015-10-14 08:33:34 +08:00
|
|
|
#include <dm.h>
|
|
|
|
#include <errno.h>
|
2020-05-11 01:40:05 +08:00
|
|
|
#include <log.h>
|
2010-04-30 11:34:16 +08:00
|
|
|
#include <malloc.h>
|
2015-10-14 08:33:34 +08:00
|
|
|
#include <fdtdec.h>
|
2015-10-28 01:41:11 +08:00
|
|
|
#include <spi.h>
|
2015-10-14 08:33:34 +08:00
|
|
|
#include <asm/io.h>
|
2020-05-11 01:40:13 +08:00
|
|
|
#include <linux/bitops.h>
|
2015-10-14 08:33:34 +08:00
|
|
|
|
2015-10-28 01:41:11 +08:00
|
|
|
#define ALTERA_SPI_STATUS_RRDY_MSK BIT(7)
|
|
|
|
#define ALTERA_SPI_CONTROL_SSO_MSK BIT(10)
|
|
|
|
|
2021-08-20 03:06:54 +08:00
|
|
|
#define ALTERA_SPI_IDLE_VAL 0xff
|
2014-10-23 03:56:04 +08:00
|
|
|
|
2014-10-23 03:55:58 +08:00
|
|
|
struct altera_spi_regs {
|
|
|
|
u32 rxdata;
|
|
|
|
u32 txdata;
|
|
|
|
u32 status;
|
|
|
|
u32 control;
|
|
|
|
u32 _reserved;
|
|
|
|
u32 slave_sel;
|
|
|
|
};
|
2010-04-30 11:34:16 +08:00
|
|
|
|
2020-12-04 07:55:23 +08:00
|
|
|
struct altera_spi_plat {
|
2015-10-14 08:33:34 +08:00
|
|
|
struct altera_spi_regs *regs;
|
|
|
|
};
|
2010-04-30 11:34:16 +08:00
|
|
|
|
2015-10-14 08:33:34 +08:00
|
|
|
struct altera_spi_priv {
|
|
|
|
struct altera_spi_regs *regs;
|
2010-04-30 11:34:16 +08:00
|
|
|
};
|
|
|
|
|
2015-10-14 08:33:34 +08:00
|
|
|
static void spi_cs_activate(struct udevice *dev, uint cs)
|
2010-04-30 11:34:16 +08:00
|
|
|
{
|
2015-10-14 08:33:34 +08:00
|
|
|
struct udevice *bus = dev->parent;
|
|
|
|
struct altera_spi_priv *priv = dev_get_priv(bus);
|
|
|
|
struct altera_spi_regs *const regs = priv->regs;
|
2010-04-30 11:34:16 +08:00
|
|
|
|
2015-10-14 08:33:34 +08:00
|
|
|
writel(1 << cs, ®s->slave_sel);
|
|
|
|
writel(ALTERA_SPI_CONTROL_SSO_MSK, ®s->control);
|
2010-04-30 11:34:16 +08:00
|
|
|
}
|
|
|
|
|
2015-10-14 08:33:34 +08:00
|
|
|
static void spi_cs_deactivate(struct udevice *dev)
|
2010-04-30 11:34:16 +08:00
|
|
|
{
|
2015-10-14 08:33:34 +08:00
|
|
|
struct udevice *bus = dev->parent;
|
|
|
|
struct altera_spi_priv *priv = dev_get_priv(bus);
|
|
|
|
struct altera_spi_regs *const regs = priv->regs;
|
2010-04-30 11:34:16 +08:00
|
|
|
|
2015-10-14 08:33:34 +08:00
|
|
|
writel(0, ®s->control);
|
|
|
|
writel(0, ®s->slave_sel);
|
2010-12-27 09:30:17 +08:00
|
|
|
}
|
|
|
|
|
2015-10-14 08:33:34 +08:00
|
|
|
static int altera_spi_claim_bus(struct udevice *dev)
|
2010-04-30 11:34:16 +08:00
|
|
|
{
|
2015-10-14 08:33:34 +08:00
|
|
|
struct udevice *bus = dev->parent;
|
|
|
|
struct altera_spi_priv *priv = dev_get_priv(bus);
|
|
|
|
struct altera_spi_regs *const regs = priv->regs;
|
2010-04-30 11:34:16 +08:00
|
|
|
|
2015-10-14 08:33:34 +08:00
|
|
|
writel(0, ®s->control);
|
|
|
|
writel(0, ®s->slave_sel);
|
2010-04-30 11:34:16 +08:00
|
|
|
|
2015-10-14 08:33:34 +08:00
|
|
|
return 0;
|
2010-04-30 11:34:16 +08:00
|
|
|
}
|
|
|
|
|
2015-10-14 08:33:34 +08:00
|
|
|
static int altera_spi_release_bus(struct udevice *dev)
|
2010-04-30 11:34:16 +08:00
|
|
|
{
|
2015-10-14 08:33:34 +08:00
|
|
|
struct udevice *bus = dev->parent;
|
|
|
|
struct altera_spi_priv *priv = dev_get_priv(bus);
|
|
|
|
struct altera_spi_regs *const regs = priv->regs;
|
2010-04-30 11:34:16 +08:00
|
|
|
|
2015-10-14 08:33:34 +08:00
|
|
|
writel(0, ®s->slave_sel);
|
2010-04-30 11:34:16 +08:00
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2015-10-14 08:33:34 +08:00
|
|
|
static int altera_spi_xfer(struct udevice *dev, unsigned int bitlen,
|
|
|
|
const void *dout, void *din, unsigned long flags)
|
2010-04-30 11:34:16 +08:00
|
|
|
{
|
2015-10-14 08:33:34 +08:00
|
|
|
struct udevice *bus = dev->parent;
|
|
|
|
struct altera_spi_priv *priv = dev_get_priv(bus);
|
|
|
|
struct altera_spi_regs *const regs = priv->regs;
|
2020-12-04 07:55:23 +08:00
|
|
|
struct dm_spi_slave_plat *slave_plat = dev_get_parent_plat(dev);
|
2010-04-30 11:34:16 +08:00
|
|
|
|
|
|
|
/* assume spi core configured to do 8 bit transfers */
|
2014-10-23 03:56:02 +08:00
|
|
|
unsigned int bytes = bitlen / 8;
|
|
|
|
const unsigned char *txp = dout;
|
|
|
|
unsigned char *rxp = din;
|
|
|
|
uint32_t reg, data, start;
|
2010-04-30 11:34:16 +08:00
|
|
|
|
|
|
|
debug("%s: bus:%i cs:%i bitlen:%i bytes:%i flags:%lx\n", __func__,
|
2020-12-17 12:20:07 +08:00
|
|
|
dev_seq(bus), slave_plat->cs, bitlen, bytes, flags);
|
2014-10-23 03:56:00 +08:00
|
|
|
|
2010-04-30 11:34:16 +08:00
|
|
|
if (bitlen == 0)
|
|
|
|
goto done;
|
|
|
|
|
|
|
|
if (bitlen % 8) {
|
|
|
|
flags |= SPI_XFER_END;
|
|
|
|
goto done;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* empty read buffer */
|
2015-10-14 08:33:34 +08:00
|
|
|
if (readl(®s->status) & ALTERA_SPI_STATUS_RRDY_MSK)
|
|
|
|
readl(®s->rxdata);
|
2014-10-23 03:56:00 +08:00
|
|
|
|
2010-04-30 11:34:16 +08:00
|
|
|
if (flags & SPI_XFER_BEGIN)
|
2015-10-14 08:33:34 +08:00
|
|
|
spi_cs_activate(dev, slave_plat->cs);
|
2010-04-30 11:34:16 +08:00
|
|
|
|
|
|
|
while (bytes--) {
|
2014-10-23 03:56:02 +08:00
|
|
|
if (txp)
|
|
|
|
data = *txp++;
|
|
|
|
else
|
2021-08-20 03:06:54 +08:00
|
|
|
data = ALTERA_SPI_IDLE_VAL;
|
2014-10-23 03:56:00 +08:00
|
|
|
|
2014-10-23 03:56:02 +08:00
|
|
|
debug("%s: tx:%x ", __func__, data);
|
2015-10-14 08:33:34 +08:00
|
|
|
writel(data, ®s->txdata);
|
2014-10-23 03:56:00 +08:00
|
|
|
|
2014-10-23 03:56:01 +08:00
|
|
|
start = get_timer(0);
|
|
|
|
while (1) {
|
2015-10-14 08:33:34 +08:00
|
|
|
reg = readl(®s->status);
|
2014-10-23 03:56:01 +08:00
|
|
|
if (reg & ALTERA_SPI_STATUS_RRDY_MSK)
|
|
|
|
break;
|
|
|
|
if (get_timer(start) > (CONFIG_SYS_HZ / 1000)) {
|
2015-10-14 08:33:34 +08:00
|
|
|
debug("%s: Transmission timed out!\n", __func__);
|
|
|
|
return -1;
|
2014-10-23 03:56:01 +08:00
|
|
|
}
|
|
|
|
}
|
2014-10-23 03:56:00 +08:00
|
|
|
|
2015-10-14 08:33:34 +08:00
|
|
|
data = readl(®s->rxdata);
|
2010-04-30 11:34:16 +08:00
|
|
|
if (rxp)
|
2014-10-23 03:56:02 +08:00
|
|
|
*rxp++ = data & 0xff;
|
2014-10-23 03:56:00 +08:00
|
|
|
|
2014-10-23 03:56:02 +08:00
|
|
|
debug("rx:%x\n", data);
|
2010-04-30 11:34:16 +08:00
|
|
|
}
|
2014-10-23 03:56:00 +08:00
|
|
|
|
|
|
|
done:
|
2010-04-30 11:34:16 +08:00
|
|
|
if (flags & SPI_XFER_END)
|
2015-10-14 08:33:34 +08:00
|
|
|
spi_cs_deactivate(dev);
|
2010-04-30 11:34:16 +08:00
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
2015-10-14 08:33:34 +08:00
|
|
|
|
|
|
|
static int altera_spi_set_speed(struct udevice *bus, uint speed)
|
|
|
|
{
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
static int altera_spi_set_mode(struct udevice *bus, uint mode)
|
|
|
|
{
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
static int altera_spi_probe(struct udevice *bus)
|
|
|
|
{
|
2020-12-04 07:55:23 +08:00
|
|
|
struct altera_spi_plat *plat = dev_get_plat(bus);
|
2015-10-14 08:33:34 +08:00
|
|
|
struct altera_spi_priv *priv = dev_get_priv(bus);
|
|
|
|
|
|
|
|
priv->regs = plat->regs;
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2020-12-04 07:55:21 +08:00
|
|
|
static int altera_spi_of_to_plat(struct udevice *bus)
|
2015-10-14 08:33:34 +08:00
|
|
|
{
|
2020-12-04 07:55:23 +08:00
|
|
|
struct altera_spi_plat *plat = dev_get_plat(bus);
|
2015-10-14 08:33:34 +08:00
|
|
|
|
2020-07-17 13:36:48 +08:00
|
|
|
plat->regs = map_physmem(dev_read_addr(bus),
|
2015-11-14 11:17:25 +08:00
|
|
|
sizeof(struct altera_spi_regs),
|
|
|
|
MAP_NOCACHE);
|
2015-10-14 08:33:34 +08:00
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
static const struct dm_spi_ops altera_spi_ops = {
|
|
|
|
.claim_bus = altera_spi_claim_bus,
|
|
|
|
.release_bus = altera_spi_release_bus,
|
|
|
|
.xfer = altera_spi_xfer,
|
|
|
|
.set_speed = altera_spi_set_speed,
|
|
|
|
.set_mode = altera_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 altera_spi_ids[] = {
|
2015-10-31 20:55:48 +08:00
|
|
|
{ .compatible = "altr,spi-1.0" },
|
|
|
|
{}
|
2015-10-14 08:33:34 +08:00
|
|
|
};
|
|
|
|
|
|
|
|
U_BOOT_DRIVER(altera_spi) = {
|
|
|
|
.name = "altera_spi",
|
|
|
|
.id = UCLASS_SPI,
|
|
|
|
.of_match = altera_spi_ids,
|
|
|
|
.ops = &altera_spi_ops,
|
2020-12-04 07:55:21 +08:00
|
|
|
.of_to_plat = altera_spi_of_to_plat,
|
2020-12-04 07:55:23 +08:00
|
|
|
.plat_auto = sizeof(struct altera_spi_plat),
|
2020-12-04 07:55:17 +08:00
|
|
|
.priv_auto = sizeof(struct altera_spi_priv),
|
2015-10-14 08:33:34 +08:00
|
|
|
.probe = altera_spi_probe,
|
|
|
|
};
|