2014-10-14 05:41:52 +00:00
|
|
|
/*
|
|
|
|
* Copyright (c) 2014 Google, Inc
|
|
|
|
*
|
|
|
|
* SPDX-License-Identifier: GPL-2.0+
|
|
|
|
*/
|
|
|
|
|
|
|
|
#include <common.h>
|
|
|
|
#include <dm.h>
|
|
|
|
#include <errno.h>
|
|
|
|
#include <fdtdec.h>
|
|
|
|
#include <malloc.h>
|
|
|
|
#include <spi.h>
|
|
|
|
#include <dm/device-internal.h>
|
|
|
|
#include <dm/uclass-internal.h>
|
|
|
|
#include <dm/root.h>
|
|
|
|
#include <dm/lists.h>
|
|
|
|
#include <dm/util.h>
|
|
|
|
|
|
|
|
DECLARE_GLOBAL_DATA_PTR;
|
|
|
|
|
|
|
|
static int spi_set_speed_mode(struct udevice *bus, int speed, int mode)
|
|
|
|
{
|
|
|
|
struct dm_spi_ops *ops;
|
|
|
|
int ret;
|
|
|
|
|
|
|
|
ops = spi_get_ops(bus);
|
|
|
|
if (ops->set_speed)
|
|
|
|
ret = ops->set_speed(bus, speed);
|
|
|
|
else
|
|
|
|
ret = -EINVAL;
|
|
|
|
if (ret) {
|
|
|
|
printf("Cannot set speed (err=%d)\n", ret);
|
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (ops->set_mode)
|
|
|
|
ret = ops->set_mode(bus, mode);
|
|
|
|
else
|
|
|
|
ret = -EINVAL;
|
|
|
|
if (ret) {
|
|
|
|
printf("Cannot set mode (err=%d)\n", ret);
|
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
int spi_claim_bus(struct spi_slave *slave)
|
|
|
|
{
|
|
|
|
struct udevice *dev = slave->dev;
|
|
|
|
struct udevice *bus = dev->parent;
|
|
|
|
struct dm_spi_ops *ops = spi_get_ops(bus);
|
2015-03-05 19:25:20 +00:00
|
|
|
struct dm_spi_bus *spi = dev_get_uclass_priv(bus);
|
2014-10-14 05:41:52 +00:00
|
|
|
int speed;
|
|
|
|
int ret;
|
|
|
|
|
|
|
|
speed = slave->max_hz;
|
|
|
|
if (spi->max_hz) {
|
|
|
|
if (speed)
|
linux/kernel.h: sync min, max, min3, max3 macros with Linux
U-Boot has never cared about the type when we get max/min of two
values, but Linux Kernel does. This commit gets min, max, min3, max3
macros synced with the kernel introducing type checks.
Many of references of those macros must be fixed to suppress warnings.
We have two options:
- Use min, max, min3, max3 only when the arguments have the same type
(or add casts to the arguments)
- Use min_t/max_t instead with the appropriate type for the first
argument
Signed-off-by: Masahiro Yamada <yamada.m@jp.panasonic.com>
Acked-by: Pavel Machek <pavel@denx.de>
Acked-by: Lukasz Majewski <l.majewski@samsung.com>
Tested-by: Lukasz Majewski <l.majewski@samsung.com>
[trini: Fixup arch/blackfin/lib/string.c]
Signed-off-by: Tom Rini <trini@ti.com>
2014-11-06 18:03:31 +00:00
|
|
|
speed = min(speed, (int)spi->max_hz);
|
2014-10-14 05:41:52 +00:00
|
|
|
else
|
|
|
|
speed = spi->max_hz;
|
|
|
|
}
|
|
|
|
if (!speed)
|
|
|
|
speed = 100000;
|
2015-02-17 22:29:35 +00:00
|
|
|
if (speed != slave->speed) {
|
|
|
|
ret = spi_set_speed_mode(bus, speed, slave->mode);
|
|
|
|
if (ret)
|
|
|
|
return ret;
|
|
|
|
slave->speed = speed;
|
|
|
|
}
|
2014-10-14 05:41:52 +00:00
|
|
|
|
2015-04-19 15:05:40 +00:00
|
|
|
return ops->claim_bus ? ops->claim_bus(dev) : 0;
|
2014-10-14 05:41:52 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
void spi_release_bus(struct spi_slave *slave)
|
|
|
|
{
|
|
|
|
struct udevice *dev = slave->dev;
|
|
|
|
struct udevice *bus = dev->parent;
|
|
|
|
struct dm_spi_ops *ops = spi_get_ops(bus);
|
|
|
|
|
|
|
|
if (ops->release_bus)
|
2015-04-19 15:05:40 +00:00
|
|
|
ops->release_bus(dev);
|
2014-10-14 05:41:52 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
int spi_xfer(struct spi_slave *slave, unsigned int bitlen,
|
|
|
|
const void *dout, void *din, unsigned long flags)
|
|
|
|
{
|
|
|
|
struct udevice *dev = slave->dev;
|
|
|
|
struct udevice *bus = dev->parent;
|
|
|
|
|
|
|
|
if (bus->uclass->uc_drv->id != UCLASS_SPI)
|
|
|
|
return -EOPNOTSUPP;
|
|
|
|
|
|
|
|
return spi_get_ops(bus)->xfer(dev, bitlen, dout, din, flags);
|
|
|
|
}
|
|
|
|
|
2015-06-23 21:39:05 +00:00
|
|
|
static int spi_post_bind(struct udevice *dev)
|
2014-10-14 05:41:52 +00:00
|
|
|
{
|
|
|
|
/* Scan the bus for devices */
|
|
|
|
return dm_scan_fdt_node(dev, gd->fdt_blob, dev->of_offset, false);
|
|
|
|
}
|
|
|
|
|
2015-06-23 21:39:05 +00:00
|
|
|
static int spi_child_post_bind(struct udevice *dev)
|
2014-10-14 05:41:52 +00:00
|
|
|
{
|
2015-01-25 15:27:12 +00:00
|
|
|
struct dm_spi_slave_platdata *plat = dev_get_parent_platdata(dev);
|
2014-10-14 05:41:52 +00:00
|
|
|
|
2015-01-25 15:27:12 +00:00
|
|
|
if (dev->of_offset == -1)
|
|
|
|
return 0;
|
|
|
|
|
|
|
|
return spi_slave_ofdata_to_platdata(gd->fdt_blob, dev->of_offset, plat);
|
|
|
|
}
|
|
|
|
|
2015-06-23 21:39:05 +00:00
|
|
|
static int spi_post_probe(struct udevice *bus)
|
2015-01-25 15:27:12 +00:00
|
|
|
{
|
2015-03-05 19:25:20 +00:00
|
|
|
struct dm_spi_bus *spi = dev_get_uclass_priv(bus);
|
2015-01-25 15:27:12 +00:00
|
|
|
|
|
|
|
spi->max_hz = fdtdec_get_int(gd->fdt_blob, bus->of_offset,
|
2014-10-14 05:41:52 +00:00
|
|
|
"spi-max-frequency", 0);
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2015-06-23 21:39:05 +00:00
|
|
|
static int spi_child_pre_probe(struct udevice *dev)
|
2015-01-25 15:27:11 +00:00
|
|
|
{
|
2015-01-25 15:27:12 +00:00
|
|
|
struct dm_spi_slave_platdata *plat = dev_get_parent_platdata(dev);
|
2015-01-25 15:27:11 +00:00
|
|
|
struct spi_slave *slave = dev_get_parentdata(dev);
|
|
|
|
|
2015-01-25 15:27:12 +00:00
|
|
|
/*
|
|
|
|
* This is needed because we pass struct spi_slave around the place
|
|
|
|
* instead slave->dev (a struct udevice). So we have to have some
|
|
|
|
* way to access the slave udevice given struct spi_slave. Once we
|
|
|
|
* change the SPI API to use udevice instead of spi_slave, we can
|
|
|
|
* drop this.
|
|
|
|
*/
|
2015-01-25 15:27:11 +00:00
|
|
|
slave->dev = dev;
|
|
|
|
|
2015-01-25 15:27:12 +00:00
|
|
|
slave->max_hz = plat->max_hz;
|
|
|
|
slave->mode = plat->mode;
|
|
|
|
|
2015-01-25 15:27:11 +00:00
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2014-10-14 05:41:52 +00:00
|
|
|
int spi_chip_select(struct udevice *dev)
|
|
|
|
{
|
2015-01-25 15:27:12 +00:00
|
|
|
struct dm_spi_slave_platdata *plat = dev_get_parent_platdata(dev);
|
2014-10-14 05:41:52 +00:00
|
|
|
|
2015-01-25 15:27:12 +00:00
|
|
|
return plat ? plat->cs : -ENOENT;
|
2014-10-14 05:41:52 +00:00
|
|
|
}
|
|
|
|
|
2014-11-11 17:46:22 +00:00
|
|
|
int spi_find_chip_select(struct udevice *bus, int cs, struct udevice **devp)
|
2014-10-14 05:41:52 +00:00
|
|
|
{
|
|
|
|
struct udevice *dev;
|
|
|
|
|
|
|
|
for (device_find_first_child(bus, &dev); dev;
|
|
|
|
device_find_next_child(&dev)) {
|
2015-01-25 15:27:12 +00:00
|
|
|
struct dm_spi_slave_platdata *plat;
|
2014-10-14 05:41:52 +00:00
|
|
|
|
2015-01-25 15:27:12 +00:00
|
|
|
plat = dev_get_parent_platdata(dev);
|
|
|
|
debug("%s: plat=%p, cs=%d\n", __func__, plat, plat->cs);
|
|
|
|
if (plat->cs == cs) {
|
2014-10-14 05:41:52 +00:00
|
|
|
*devp = dev;
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return -ENODEV;
|
|
|
|
}
|
|
|
|
|
|
|
|
int spi_cs_is_valid(unsigned int busnum, unsigned int cs)
|
|
|
|
{
|
|
|
|
struct spi_cs_info info;
|
|
|
|
struct udevice *bus;
|
|
|
|
int ret;
|
|
|
|
|
|
|
|
ret = uclass_find_device_by_seq(UCLASS_SPI, busnum, false, &bus);
|
|
|
|
if (ret) {
|
|
|
|
debug("%s: No bus %d\n", __func__, busnum);
|
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
|
|
|
return spi_cs_info(bus, cs, &info);
|
|
|
|
}
|
|
|
|
|
|
|
|
int spi_cs_info(struct udevice *bus, uint cs, struct spi_cs_info *info)
|
|
|
|
{
|
|
|
|
struct spi_cs_info local_info;
|
|
|
|
struct dm_spi_ops *ops;
|
|
|
|
int ret;
|
|
|
|
|
|
|
|
if (!info)
|
|
|
|
info = &local_info;
|
|
|
|
|
|
|
|
/* If there is a device attached, return it */
|
|
|
|
info->dev = NULL;
|
|
|
|
ret = spi_find_chip_select(bus, cs, &info->dev);
|
|
|
|
if (!ret)
|
|
|
|
return 0;
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Otherwise ask the driver. For the moment we don't have CS info.
|
|
|
|
* When we do we could provide the driver with a helper function
|
|
|
|
* to figure out what chip selects are valid, or just handle the
|
|
|
|
* request.
|
|
|
|
*/
|
|
|
|
ops = spi_get_ops(bus);
|
|
|
|
if (ops->cs_info)
|
|
|
|
return ops->cs_info(bus, cs, info);
|
|
|
|
|
|
|
|
/*
|
|
|
|
* We could assume there is at least one valid chip select, but best
|
|
|
|
* to be sure and return an error in this case. The driver didn't
|
|
|
|
* care enough to tell us.
|
|
|
|
*/
|
|
|
|
return -ENODEV;
|
|
|
|
}
|
|
|
|
|
|
|
|
int spi_find_bus_and_cs(int busnum, int cs, struct udevice **busp,
|
|
|
|
struct udevice **devp)
|
|
|
|
{
|
|
|
|
struct udevice *bus, *dev;
|
|
|
|
int ret;
|
|
|
|
|
|
|
|
ret = uclass_find_device_by_seq(UCLASS_SPI, busnum, false, &bus);
|
|
|
|
if (ret) {
|
|
|
|
debug("%s: No bus %d\n", __func__, busnum);
|
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
ret = spi_find_chip_select(bus, cs, &dev);
|
|
|
|
if (ret) {
|
|
|
|
debug("%s: No cs %d\n", __func__, cs);
|
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
*busp = bus;
|
|
|
|
*devp = dev;
|
|
|
|
|
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
|
|
|
int spi_get_bus_and_cs(int busnum, int cs, int speed, int mode,
|
|
|
|
const char *drv_name, const char *dev_name,
|
|
|
|
struct udevice **busp, struct spi_slave **devp)
|
|
|
|
{
|
|
|
|
struct udevice *bus, *dev;
|
|
|
|
bool created = false;
|
|
|
|
int ret;
|
|
|
|
|
|
|
|
ret = uclass_get_device_by_seq(UCLASS_SPI, busnum, &bus);
|
|
|
|
if (ret) {
|
|
|
|
printf("Invalid bus %d (err=%d)\n", busnum, ret);
|
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
ret = spi_find_chip_select(bus, cs, &dev);
|
|
|
|
|
|
|
|
/*
|
|
|
|
* If there is no such device, create one automatically. This means
|
|
|
|
* that we don't need a device tree node or platform data for the
|
|
|
|
* SPI flash chip - we will bind to the correct driver.
|
|
|
|
*/
|
|
|
|
if (ret == -ENODEV && drv_name) {
|
2015-01-25 15:27:12 +00:00
|
|
|
struct dm_spi_slave_platdata *plat;
|
|
|
|
|
2014-10-14 05:41:52 +00:00
|
|
|
debug("%s: Binding new device '%s', busnum=%d, cs=%d, driver=%s\n",
|
|
|
|
__func__, dev_name, busnum, cs, drv_name);
|
2014-11-11 17:46:23 +00:00
|
|
|
ret = device_bind_driver(bus, drv_name, dev_name, &dev);
|
2014-10-14 05:41:52 +00:00
|
|
|
if (ret)
|
|
|
|
return ret;
|
2015-01-25 15:27:12 +00:00
|
|
|
plat = dev_get_parent_platdata(dev);
|
|
|
|
plat->cs = cs;
|
|
|
|
plat->max_hz = speed;
|
|
|
|
plat->mode = mode;
|
2014-10-14 05:41:52 +00:00
|
|
|
created = true;
|
|
|
|
} else if (ret) {
|
|
|
|
printf("Invalid chip select %d:%d (err=%d)\n", busnum, cs,
|
|
|
|
ret);
|
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (!device_active(dev)) {
|
2015-01-25 15:27:12 +00:00
|
|
|
struct spi_slave *slave;
|
2014-10-14 05:41:52 +00:00
|
|
|
|
2015-01-25 15:27:12 +00:00
|
|
|
ret = device_probe(dev);
|
2014-10-14 05:41:52 +00:00
|
|
|
if (ret)
|
|
|
|
goto err;
|
2015-01-25 15:27:12 +00:00
|
|
|
slave = dev_get_parentdata(dev);
|
2014-10-14 05:41:52 +00:00
|
|
|
slave->dev = dev;
|
|
|
|
}
|
|
|
|
|
|
|
|
ret = spi_set_speed_mode(bus, speed, mode);
|
|
|
|
if (ret)
|
|
|
|
goto err;
|
|
|
|
|
|
|
|
*busp = bus;
|
|
|
|
*devp = dev_get_parentdata(dev);
|
|
|
|
debug("%s: bus=%p, slave=%p\n", __func__, bus, *devp);
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
|
|
|
|
err:
|
2015-01-25 15:27:12 +00:00
|
|
|
debug("%s: Error path, credted=%d, device '%s'\n", __func__,
|
|
|
|
created, dev->name);
|
2014-10-14 05:41:52 +00:00
|
|
|
if (created) {
|
|
|
|
device_remove(dev);
|
|
|
|
device_unbind(dev);
|
|
|
|
}
|
|
|
|
|
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Compatibility function - to be removed */
|
|
|
|
struct spi_slave *spi_setup_slave_fdt(const void *blob, int node,
|
|
|
|
int bus_node)
|
|
|
|
{
|
|
|
|
struct udevice *bus, *dev;
|
|
|
|
int ret;
|
|
|
|
|
|
|
|
ret = uclass_get_device_by_of_offset(UCLASS_SPI, bus_node, &bus);
|
|
|
|
if (ret)
|
|
|
|
return NULL;
|
|
|
|
ret = device_get_child_by_of_offset(bus, node, &dev);
|
|
|
|
if (ret)
|
|
|
|
return NULL;
|
|
|
|
return dev_get_parentdata(dev);
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Compatibility function - to be removed */
|
|
|
|
struct spi_slave *spi_setup_slave(unsigned int busnum, unsigned int cs,
|
|
|
|
unsigned int speed, unsigned int mode)
|
|
|
|
{
|
|
|
|
struct spi_slave *slave;
|
|
|
|
struct udevice *dev;
|
|
|
|
int ret;
|
|
|
|
|
|
|
|
ret = spi_get_bus_and_cs(busnum, cs, speed, mode, NULL, 0, &dev,
|
|
|
|
&slave);
|
|
|
|
if (ret)
|
|
|
|
return NULL;
|
|
|
|
|
|
|
|
return slave;
|
|
|
|
}
|
|
|
|
|
|
|
|
void spi_free_slave(struct spi_slave *slave)
|
|
|
|
{
|
|
|
|
device_remove(slave->dev);
|
|
|
|
slave->dev = NULL;
|
|
|
|
}
|
|
|
|
|
2015-01-25 15:27:12 +00:00
|
|
|
int spi_slave_ofdata_to_platdata(const void *blob, int node,
|
|
|
|
struct dm_spi_slave_platdata *plat)
|
2014-10-14 05:41:52 +00:00
|
|
|
{
|
|
|
|
int mode = 0;
|
|
|
|
|
2015-01-25 15:27:12 +00:00
|
|
|
plat->cs = fdtdec_get_int(blob, node, "reg", -1);
|
|
|
|
plat->max_hz = fdtdec_get_int(blob, node, "spi-max-frequency", 0);
|
2014-10-14 05:41:52 +00:00
|
|
|
if (fdtdec_get_bool(blob, node, "spi-cpol"))
|
|
|
|
mode |= SPI_CPOL;
|
|
|
|
if (fdtdec_get_bool(blob, node, "spi-cpha"))
|
|
|
|
mode |= SPI_CPHA;
|
|
|
|
if (fdtdec_get_bool(blob, node, "spi-cs-high"))
|
|
|
|
mode |= SPI_CS_HIGH;
|
|
|
|
if (fdtdec_get_bool(blob, node, "spi-half-duplex"))
|
|
|
|
mode |= SPI_PREAMBLE;
|
2015-01-25 15:27:12 +00:00
|
|
|
plat->mode = mode;
|
2014-10-14 05:41:52 +00:00
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
UCLASS_DRIVER(spi) = {
|
|
|
|
.id = UCLASS_SPI,
|
|
|
|
.name = "spi",
|
2015-01-25 15:27:05 +00:00
|
|
|
.flags = DM_UC_FLAG_SEQ_ALIAS,
|
2014-10-14 05:41:52 +00:00
|
|
|
.post_bind = spi_post_bind,
|
|
|
|
.post_probe = spi_post_probe,
|
2015-01-25 15:27:11 +00:00
|
|
|
.child_pre_probe = spi_child_pre_probe,
|
2014-10-14 05:41:52 +00:00
|
|
|
.per_device_auto_alloc_size = sizeof(struct dm_spi_bus),
|
2015-01-25 15:27:07 +00:00
|
|
|
.per_child_auto_alloc_size = sizeof(struct spi_slave),
|
2015-01-25 15:27:12 +00:00
|
|
|
.per_child_platdata_auto_alloc_size =
|
|
|
|
sizeof(struct dm_spi_slave_platdata),
|
|
|
|
.child_post_bind = spi_child_post_bind,
|
2014-10-14 05:41:52 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
UCLASS_DRIVER(spi_generic) = {
|
|
|
|
.id = UCLASS_SPI_GENERIC,
|
|
|
|
.name = "spi_generic",
|
|
|
|
};
|
|
|
|
|
|
|
|
U_BOOT_DRIVER(spi_generic_drv) = {
|
|
|
|
.name = "spi_generic_drv",
|
|
|
|
.id = UCLASS_SPI_GENERIC,
|
|
|
|
};
|