xref: /openbmc/linux/drivers/fpga/ice40-spi.c (revision 4ba0b2c2)
1b886d83cSThomas Gleixner // SPDX-License-Identifier: GPL-2.0-only
221f8ba2eSJoel Holdsworth /*
321f8ba2eSJoel Holdsworth  * FPGA Manager Driver for Lattice iCE40.
421f8ba2eSJoel Holdsworth  *
521f8ba2eSJoel Holdsworth  *  Copyright (c) 2016 Joel Holdsworth
621f8ba2eSJoel Holdsworth  *
721f8ba2eSJoel Holdsworth  * This driver adds support to the FPGA manager for configuring the SRAM of
821f8ba2eSJoel Holdsworth  * Lattice iCE40 FPGAs through slave SPI.
921f8ba2eSJoel Holdsworth  */
1021f8ba2eSJoel Holdsworth 
1121f8ba2eSJoel Holdsworth #include <linux/fpga/fpga-mgr.h>
1221f8ba2eSJoel Holdsworth #include <linux/gpio/consumer.h>
1321f8ba2eSJoel Holdsworth #include <linux/module.h>
1421f8ba2eSJoel Holdsworth #include <linux/of_gpio.h>
1521f8ba2eSJoel Holdsworth #include <linux/spi/spi.h>
1621f8ba2eSJoel Holdsworth #include <linux/stringify.h>
1721f8ba2eSJoel Holdsworth 
1821f8ba2eSJoel Holdsworth #define ICE40_SPI_MAX_SPEED 25000000 /* Hz */
1921f8ba2eSJoel Holdsworth #define ICE40_SPI_MIN_SPEED 1000000 /* Hz */
2021f8ba2eSJoel Holdsworth 
2121f8ba2eSJoel Holdsworth #define ICE40_SPI_RESET_DELAY 1 /* us (>200ns) */
2221f8ba2eSJoel Holdsworth #define ICE40_SPI_HOUSEKEEPING_DELAY 1200 /* us */
2321f8ba2eSJoel Holdsworth 
2421f8ba2eSJoel Holdsworth #define ICE40_SPI_NUM_ACTIVATION_BYTES DIV_ROUND_UP(49, 8)
2521f8ba2eSJoel Holdsworth 
2621f8ba2eSJoel Holdsworth struct ice40_fpga_priv {
2721f8ba2eSJoel Holdsworth 	struct spi_device *dev;
2821f8ba2eSJoel Holdsworth 	struct gpio_desc *reset;
2921f8ba2eSJoel Holdsworth 	struct gpio_desc *cdone;
3021f8ba2eSJoel Holdsworth };
3121f8ba2eSJoel Holdsworth 
ice40_fpga_ops_state(struct fpga_manager * mgr)3221f8ba2eSJoel Holdsworth static enum fpga_mgr_states ice40_fpga_ops_state(struct fpga_manager *mgr)
3321f8ba2eSJoel Holdsworth {
3421f8ba2eSJoel Holdsworth 	struct ice40_fpga_priv *priv = mgr->priv;
3521f8ba2eSJoel Holdsworth 
3621f8ba2eSJoel Holdsworth 	return gpiod_get_value(priv->cdone) ? FPGA_MGR_STATE_OPERATING :
3721f8ba2eSJoel Holdsworth 		FPGA_MGR_STATE_UNKNOWN;
3821f8ba2eSJoel Holdsworth }
3921f8ba2eSJoel Holdsworth 
ice40_fpga_ops_write_init(struct fpga_manager * mgr,struct fpga_image_info * info,const char * buf,size_t count)4021f8ba2eSJoel Holdsworth static int ice40_fpga_ops_write_init(struct fpga_manager *mgr,
4121f8ba2eSJoel Holdsworth 				     struct fpga_image_info *info,
4221f8ba2eSJoel Holdsworth 				     const char *buf, size_t count)
4321f8ba2eSJoel Holdsworth {
4421f8ba2eSJoel Holdsworth 	struct ice40_fpga_priv *priv = mgr->priv;
4521f8ba2eSJoel Holdsworth 	struct spi_device *dev = priv->dev;
4621f8ba2eSJoel Holdsworth 	struct spi_message message;
4721f8ba2eSJoel Holdsworth 	struct spi_transfer assert_cs_then_reset_delay = {
4821f8ba2eSJoel Holdsworth 		.cs_change   = 1,
491493674bSSergiu Cuciurean 		.delay = {
501493674bSSergiu Cuciurean 			.value = ICE40_SPI_RESET_DELAY,
511493674bSSergiu Cuciurean 			.unit = SPI_DELAY_UNIT_USECS
521493674bSSergiu Cuciurean 		}
5321f8ba2eSJoel Holdsworth 	};
5421f8ba2eSJoel Holdsworth 	struct spi_transfer housekeeping_delay_then_release_cs = {
551493674bSSergiu Cuciurean 		.delay = {
561493674bSSergiu Cuciurean 			.value = ICE40_SPI_HOUSEKEEPING_DELAY,
571493674bSSergiu Cuciurean 			.unit = SPI_DELAY_UNIT_USECS
581493674bSSergiu Cuciurean 		}
5921f8ba2eSJoel Holdsworth 	};
6021f8ba2eSJoel Holdsworth 	int ret;
6121f8ba2eSJoel Holdsworth 
6221f8ba2eSJoel Holdsworth 	if ((info->flags & FPGA_MGR_PARTIAL_RECONFIG)) {
6321f8ba2eSJoel Holdsworth 		dev_err(&dev->dev,
6421f8ba2eSJoel Holdsworth 			"Partial reconfiguration is not supported\n");
6521f8ba2eSJoel Holdsworth 		return -ENOTSUPP;
6621f8ba2eSJoel Holdsworth 	}
6721f8ba2eSJoel Holdsworth 
6821f8ba2eSJoel Holdsworth 	/* Lock the bus, assert CRESET_B and SS_B and delay >200ns */
6921f8ba2eSJoel Holdsworth 	spi_bus_lock(dev->master);
7021f8ba2eSJoel Holdsworth 
7121f8ba2eSJoel Holdsworth 	gpiod_set_value(priv->reset, 1);
7221f8ba2eSJoel Holdsworth 
7321f8ba2eSJoel Holdsworth 	spi_message_init(&message);
7421f8ba2eSJoel Holdsworth 	spi_message_add_tail(&assert_cs_then_reset_delay, &message);
7521f8ba2eSJoel Holdsworth 	ret = spi_sync_locked(dev, &message);
7621f8ba2eSJoel Holdsworth 
7721f8ba2eSJoel Holdsworth 	/* Come out of reset */
7821f8ba2eSJoel Holdsworth 	gpiod_set_value(priv->reset, 0);
7921f8ba2eSJoel Holdsworth 
8021f8ba2eSJoel Holdsworth 	/* Abort if the chip-select failed */
8121f8ba2eSJoel Holdsworth 	if (ret)
8221f8ba2eSJoel Holdsworth 		goto fail;
8321f8ba2eSJoel Holdsworth 
8421f8ba2eSJoel Holdsworth 	/* Check CDONE is de-asserted i.e. the FPGA is reset */
8521f8ba2eSJoel Holdsworth 	if (gpiod_get_value(priv->cdone)) {
8621f8ba2eSJoel Holdsworth 		dev_err(&dev->dev, "Device reset failed, CDONE is asserted\n");
8721f8ba2eSJoel Holdsworth 		ret = -EIO;
8821f8ba2eSJoel Holdsworth 		goto fail;
8921f8ba2eSJoel Holdsworth 	}
9021f8ba2eSJoel Holdsworth 
9121f8ba2eSJoel Holdsworth 	/* Wait for the housekeeping to complete, and release SS_B */
9221f8ba2eSJoel Holdsworth 	spi_message_init(&message);
9321f8ba2eSJoel Holdsworth 	spi_message_add_tail(&housekeeping_delay_then_release_cs, &message);
9421f8ba2eSJoel Holdsworth 	ret = spi_sync_locked(dev, &message);
9521f8ba2eSJoel Holdsworth 
9621f8ba2eSJoel Holdsworth fail:
9721f8ba2eSJoel Holdsworth 	spi_bus_unlock(dev->master);
9821f8ba2eSJoel Holdsworth 
9921f8ba2eSJoel Holdsworth 	return ret;
10021f8ba2eSJoel Holdsworth }
10121f8ba2eSJoel Holdsworth 
ice40_fpga_ops_write(struct fpga_manager * mgr,const char * buf,size_t count)10221f8ba2eSJoel Holdsworth static int ice40_fpga_ops_write(struct fpga_manager *mgr,
10321f8ba2eSJoel Holdsworth 				const char *buf, size_t count)
10421f8ba2eSJoel Holdsworth {
10521f8ba2eSJoel Holdsworth 	struct ice40_fpga_priv *priv = mgr->priv;
10621f8ba2eSJoel Holdsworth 
10721f8ba2eSJoel Holdsworth 	return spi_write(priv->dev, buf, count);
10821f8ba2eSJoel Holdsworth }
10921f8ba2eSJoel Holdsworth 
ice40_fpga_ops_write_complete(struct fpga_manager * mgr,struct fpga_image_info * info)11021f8ba2eSJoel Holdsworth static int ice40_fpga_ops_write_complete(struct fpga_manager *mgr,
11121f8ba2eSJoel Holdsworth 					 struct fpga_image_info *info)
11221f8ba2eSJoel Holdsworth {
11321f8ba2eSJoel Holdsworth 	struct ice40_fpga_priv *priv = mgr->priv;
11421f8ba2eSJoel Holdsworth 	struct spi_device *dev = priv->dev;
11521f8ba2eSJoel Holdsworth 	const u8 padding[ICE40_SPI_NUM_ACTIVATION_BYTES] = {0};
11621f8ba2eSJoel Holdsworth 
11721f8ba2eSJoel Holdsworth 	/* Check CDONE is asserted */
11821f8ba2eSJoel Holdsworth 	if (!gpiod_get_value(priv->cdone)) {
11921f8ba2eSJoel Holdsworth 		dev_err(&dev->dev,
12021f8ba2eSJoel Holdsworth 			"CDONE was not asserted after firmware transfer\n");
12121f8ba2eSJoel Holdsworth 		return -EIO;
12221f8ba2eSJoel Holdsworth 	}
12321f8ba2eSJoel Holdsworth 
12421f8ba2eSJoel Holdsworth 	/* Send of zero-padding to activate the firmware */
12521f8ba2eSJoel Holdsworth 	return spi_write(dev, padding, sizeof(padding));
12621f8ba2eSJoel Holdsworth }
12721f8ba2eSJoel Holdsworth 
12821f8ba2eSJoel Holdsworth static const struct fpga_manager_ops ice40_fpga_ops = {
12921f8ba2eSJoel Holdsworth 	.state = ice40_fpga_ops_state,
13021f8ba2eSJoel Holdsworth 	.write_init = ice40_fpga_ops_write_init,
13121f8ba2eSJoel Holdsworth 	.write = ice40_fpga_ops_write,
13221f8ba2eSJoel Holdsworth 	.write_complete = ice40_fpga_ops_write_complete,
13321f8ba2eSJoel Holdsworth };
13421f8ba2eSJoel Holdsworth 
ice40_fpga_probe(struct spi_device * spi)13521f8ba2eSJoel Holdsworth static int ice40_fpga_probe(struct spi_device *spi)
13621f8ba2eSJoel Holdsworth {
13721f8ba2eSJoel Holdsworth 	struct device *dev = &spi->dev;
13821f8ba2eSJoel Holdsworth 	struct ice40_fpga_priv *priv;
1397085e2a9SAlan Tull 	struct fpga_manager *mgr;
14021f8ba2eSJoel Holdsworth 	int ret;
14121f8ba2eSJoel Holdsworth 
14221f8ba2eSJoel Holdsworth 	priv = devm_kzalloc(&spi->dev, sizeof(*priv), GFP_KERNEL);
14321f8ba2eSJoel Holdsworth 	if (!priv)
14421f8ba2eSJoel Holdsworth 		return -ENOMEM;
14521f8ba2eSJoel Holdsworth 
14621f8ba2eSJoel Holdsworth 	priv->dev = spi;
14721f8ba2eSJoel Holdsworth 
14821f8ba2eSJoel Holdsworth 	/* Check board setup data. */
14921f8ba2eSJoel Holdsworth 	if (spi->max_speed_hz > ICE40_SPI_MAX_SPEED) {
15021f8ba2eSJoel Holdsworth 		dev_err(dev, "SPI speed is too high, maximum speed is "
15121f8ba2eSJoel Holdsworth 			__stringify(ICE40_SPI_MAX_SPEED) "\n");
15221f8ba2eSJoel Holdsworth 		return -EINVAL;
15321f8ba2eSJoel Holdsworth 	}
15421f8ba2eSJoel Holdsworth 
15521f8ba2eSJoel Holdsworth 	if (spi->max_speed_hz < ICE40_SPI_MIN_SPEED) {
15621f8ba2eSJoel Holdsworth 		dev_err(dev, "SPI speed is too low, minimum speed is "
15721f8ba2eSJoel Holdsworth 			__stringify(ICE40_SPI_MIN_SPEED) "\n");
15821f8ba2eSJoel Holdsworth 		return -EINVAL;
15921f8ba2eSJoel Holdsworth 	}
16021f8ba2eSJoel Holdsworth 
16121f8ba2eSJoel Holdsworth 	if (spi->mode & SPI_CPHA) {
16221f8ba2eSJoel Holdsworth 		dev_err(dev, "Bad SPI mode, CPHA not supported\n");
16321f8ba2eSJoel Holdsworth 		return -EINVAL;
16421f8ba2eSJoel Holdsworth 	}
16521f8ba2eSJoel Holdsworth 
16621f8ba2eSJoel Holdsworth 	/* Set up the GPIOs */
16721f8ba2eSJoel Holdsworth 	priv->cdone = devm_gpiod_get(dev, "cdone", GPIOD_IN);
16821f8ba2eSJoel Holdsworth 	if (IS_ERR(priv->cdone)) {
16921f8ba2eSJoel Holdsworth 		ret = PTR_ERR(priv->cdone);
17021f8ba2eSJoel Holdsworth 		dev_err(dev, "Failed to get CDONE GPIO: %d\n", ret);
17121f8ba2eSJoel Holdsworth 		return ret;
17221f8ba2eSJoel Holdsworth 	}
17321f8ba2eSJoel Holdsworth 
17421f8ba2eSJoel Holdsworth 	priv->reset = devm_gpiod_get(dev, "reset", GPIOD_OUT_HIGH);
17521f8ba2eSJoel Holdsworth 	if (IS_ERR(priv->reset)) {
17621f8ba2eSJoel Holdsworth 		ret = PTR_ERR(priv->reset);
17721f8ba2eSJoel Holdsworth 		dev_err(dev, "Failed to get CRESET_B GPIO: %d\n", ret);
17821f8ba2eSJoel Holdsworth 		return ret;
17921f8ba2eSJoel Holdsworth 	}
18021f8ba2eSJoel Holdsworth 
181*4ba0b2c2SRuss Weight 	mgr = devm_fpga_mgr_register(dev, "Lattice iCE40 FPGA Manager",
18221f8ba2eSJoel Holdsworth 				     &ice40_fpga_ops, priv);
183*4ba0b2c2SRuss Weight 	return PTR_ERR_OR_ZERO(mgr);
18421f8ba2eSJoel Holdsworth }
18521f8ba2eSJoel Holdsworth 
18621f8ba2eSJoel Holdsworth static const struct of_device_id ice40_fpga_of_match[] = {
18721f8ba2eSJoel Holdsworth 	{ .compatible = "lattice,ice40-fpga-mgr", },
18821f8ba2eSJoel Holdsworth 	{},
18921f8ba2eSJoel Holdsworth };
19021f8ba2eSJoel Holdsworth MODULE_DEVICE_TABLE(of, ice40_fpga_of_match);
19121f8ba2eSJoel Holdsworth 
1922a2a7957SMark Brown static const struct spi_device_id ice40_fpga_spi_ids[] = {
1932a2a7957SMark Brown 	{ .name = "ice40-fpga-mgr", },
1942a2a7957SMark Brown 	{},
1952a2a7957SMark Brown };
1962a2a7957SMark Brown MODULE_DEVICE_TABLE(spi, ice40_fpga_spi_ids);
1972a2a7957SMark Brown 
19821f8ba2eSJoel Holdsworth static struct spi_driver ice40_fpga_driver = {
19921f8ba2eSJoel Holdsworth 	.probe = ice40_fpga_probe,
20021f8ba2eSJoel Holdsworth 	.driver = {
20121f8ba2eSJoel Holdsworth 		.name = "ice40spi",
20221f8ba2eSJoel Holdsworth 		.of_match_table = of_match_ptr(ice40_fpga_of_match),
20321f8ba2eSJoel Holdsworth 	},
2042a2a7957SMark Brown 	.id_table = ice40_fpga_spi_ids,
20521f8ba2eSJoel Holdsworth };
20621f8ba2eSJoel Holdsworth 
20721f8ba2eSJoel Holdsworth module_spi_driver(ice40_fpga_driver);
20821f8ba2eSJoel Holdsworth 
20921f8ba2eSJoel Holdsworth MODULE_AUTHOR("Joel Holdsworth <joel@airwebreathe.org.uk>");
21021f8ba2eSJoel Holdsworth MODULE_DESCRIPTION("Lattice iCE40 FPGA Manager");
21121f8ba2eSJoel Holdsworth MODULE_LICENSE("GPL v2");
212