1 // SPDX-License-Identifier: GPL-2.0-or-later
2 /* Copyright (C) 2022 Hewlett-Packard Development Company, L.P. */
4 #include <linux/iopoll.h>
6 #include <linux/of_device.h>
7 #include <linux/platform_device.h>
8 #include <linux/spi/spi.h>
9 #include <linux/spi/spi-mem.h>
11 #define GXP_SPI0_MAX_CHIPSELECT 2
12 #define GXP_SPI_SLEEP_TIME 1
13 #define GXP_SPI_TIMEOUT (130 * 1000000 / GXP_SPI_SLEEP_TIME)
17 #define SPILDAT_LEN 256
19 #define OFFSET_SPIMCFG 0x0
20 #define OFFSET_SPIMCTRL 0x4
21 #define OFFSET_SPICMD 0x5
22 #define OFFSET_SPIDCNT 0x6
23 #define OFFSET_SPIADDR 0x8
24 #define OFFSET_SPIINTSTS 0xc
26 #define SPIMCTRL_START 0x01
27 #define SPIMCTRL_BUSY 0x02
28 #define SPIMCTRL_DIR 0x08
33 struct gxp_spi *spifi;
43 const struct gxp_spi_data *data;
44 void __iomem *reg_base;
45 void __iomem *dat_base;
46 void __iomem *dir_base;
48 struct gxp_spi_chip chips[GXP_SPI0_MAX_CHIPSELECT];
51 static void gxp_spi_set_mode(struct gxp_spi *spifi, int mode)
54 void __iomem *reg_base = spifi->reg_base;
56 value = readb(reg_base + OFFSET_SPIMCTRL);
58 if (mode == MANUAL_MODE) {
59 writeb(0x55, reg_base + OFFSET_SPICMD);
60 writeb(0xaa, reg_base + OFFSET_SPICMD);
65 writeb(value, reg_base + OFFSET_SPIMCTRL);
68 static int gxp_spi_read_reg(struct gxp_spi_chip *chip, const struct spi_mem_op *op)
71 struct gxp_spi *spifi = chip->spifi;
72 void __iomem *reg_base = spifi->reg_base;
75 value = readl(reg_base + OFFSET_SPIMCFG);
77 value |= (chip->cs << 24);
78 value &= ~(0x07 << 16);
79 value &= ~(0x1f << 19);
80 writel(value, reg_base + OFFSET_SPIMCFG);
82 writel(0, reg_base + OFFSET_SPIADDR);
84 writeb(op->cmd.opcode, reg_base + OFFSET_SPICMD);
86 writew(op->data.nbytes, reg_base + OFFSET_SPIDCNT);
88 value = readb(reg_base + OFFSET_SPIMCTRL);
89 value &= ~SPIMCTRL_DIR;
90 value |= SPIMCTRL_START;
92 writeb(value, reg_base + OFFSET_SPIMCTRL);
94 ret = readb_poll_timeout(reg_base + OFFSET_SPIMCTRL, value,
95 !(value & SPIMCTRL_BUSY),
96 GXP_SPI_SLEEP_TIME, GXP_SPI_TIMEOUT);
98 dev_warn(spifi->dev, "read reg busy time out\n");
102 memcpy_fromio(op->data.buf.in, spifi->dat_base, op->data.nbytes);
106 static int gxp_spi_write_reg(struct gxp_spi_chip *chip, const struct spi_mem_op *op)
109 struct gxp_spi *spifi = chip->spifi;
110 void __iomem *reg_base = spifi->reg_base;
113 value = readl(reg_base + OFFSET_SPIMCFG);
115 value |= (chip->cs << 24);
116 value &= ~(0x07 << 16);
117 value &= ~(0x1f << 19);
118 writel(value, reg_base + OFFSET_SPIMCFG);
120 writel(0, reg_base + OFFSET_SPIADDR);
122 writeb(op->cmd.opcode, reg_base + OFFSET_SPICMD);
124 memcpy_toio(spifi->dat_base, op->data.buf.in, op->data.nbytes);
126 writew(op->data.nbytes, reg_base + OFFSET_SPIDCNT);
128 value = readb(reg_base + OFFSET_SPIMCTRL);
129 value |= SPIMCTRL_DIR;
130 value |= SPIMCTRL_START;
132 writeb(value, reg_base + OFFSET_SPIMCTRL);
134 ret = readb_poll_timeout(reg_base + OFFSET_SPIMCTRL, value,
135 !(value & SPIMCTRL_BUSY),
136 GXP_SPI_SLEEP_TIME, GXP_SPI_TIMEOUT);
138 dev_warn(spifi->dev, "write reg busy time out\n");
143 static ssize_t gxp_spi_read(struct gxp_spi_chip *chip, const struct spi_mem_op *op)
145 struct gxp_spi *spifi = chip->spifi;
146 u32 offset = op->addr.val;
151 memcpy_fromio(op->data.buf.in, spifi->dir_base + offset, op->data.nbytes);
156 static ssize_t gxp_spi_write(struct gxp_spi_chip *chip, const struct spi_mem_op *op)
158 struct gxp_spi *spifi = chip->spifi;
159 void __iomem *reg_base = spifi->reg_base;
164 write_len = op->data.nbytes;
165 if (write_len > SPILDAT_LEN)
166 write_len = SPILDAT_LEN;
168 value = readl(reg_base + OFFSET_SPIMCFG);
170 value |= (chip->cs << 24);
171 value &= ~(0x07 << 16);
172 value |= (op->addr.nbytes << 16);
173 value &= ~(0x1f << 19);
174 writel(value, reg_base + OFFSET_SPIMCFG);
176 writel(op->addr.val, reg_base + OFFSET_SPIADDR);
178 writeb(op->cmd.opcode, reg_base + OFFSET_SPICMD);
180 writew(write_len, reg_base + OFFSET_SPIDCNT);
182 memcpy_toio(spifi->dat_base, op->data.buf.in, write_len);
184 value = readb(reg_base + OFFSET_SPIMCTRL);
185 value |= SPIMCTRL_DIR;
186 value |= SPIMCTRL_START;
188 writeb(value, reg_base + OFFSET_SPIMCTRL);
190 ret = readb_poll_timeout(reg_base + OFFSET_SPIMCTRL, value,
191 !(value & SPIMCTRL_BUSY),
192 GXP_SPI_SLEEP_TIME, GXP_SPI_TIMEOUT);
194 dev_warn(spifi->dev, "write busy time out\n");
201 static int do_gxp_exec_mem_op(struct spi_mem *mem, const struct spi_mem_op *op)
203 struct gxp_spi *spifi = spi_controller_get_devdata(mem->spi->master);
204 struct gxp_spi_chip *chip = &spifi->chips[spi_get_chipselect(mem->spi, 0)];
207 if (op->data.dir == SPI_MEM_DATA_IN) {
208 if (!op->addr.nbytes)
209 ret = gxp_spi_read_reg(chip, op);
211 ret = gxp_spi_read(chip, op);
213 if (!op->addr.nbytes)
214 ret = gxp_spi_write_reg(chip, op);
216 ret = gxp_spi_write(chip, op);
222 static int gxp_exec_mem_op(struct spi_mem *mem, const struct spi_mem_op *op)
226 ret = do_gxp_exec_mem_op(mem, op);
228 dev_err(&mem->spi->dev, "operation failed: %d", ret);
233 static const struct spi_controller_mem_ops gxp_spi_mem_ops = {
234 .exec_op = gxp_exec_mem_op,
237 static int gxp_spi_setup(struct spi_device *spi)
239 struct gxp_spi *spifi = spi_controller_get_devdata(spi->master);
240 unsigned int cs = spi_get_chipselect(spi, 0);
241 struct gxp_spi_chip *chip = &spifi->chips[cs];
246 gxp_spi_set_mode(spifi, MANUAL_MODE);
251 static int gxp_spifi_probe(struct platform_device *pdev)
253 struct device *dev = &pdev->dev;
254 const struct gxp_spi_data *data;
255 struct spi_controller *ctlr;
256 struct gxp_spi *spifi;
259 data = of_device_get_match_data(&pdev->dev);
261 ctlr = devm_spi_alloc_master(dev, sizeof(*spifi));
265 spifi = spi_controller_get_devdata(ctlr);
267 platform_set_drvdata(pdev, spifi);
271 spifi->reg_base = devm_platform_ioremap_resource(pdev, 0);
272 if (IS_ERR(spifi->reg_base))
273 return PTR_ERR(spifi->reg_base);
275 spifi->dat_base = devm_platform_ioremap_resource(pdev, 1);
276 if (IS_ERR(spifi->dat_base))
277 return PTR_ERR(spifi->dat_base);
279 spifi->dir_base = devm_platform_ioremap_resource(pdev, 2);
280 if (IS_ERR(spifi->dir_base))
281 return PTR_ERR(spifi->dir_base);
283 ctlr->mode_bits = data->mode_bits;
284 ctlr->bus_num = pdev->id;
285 ctlr->mem_ops = &gxp_spi_mem_ops;
286 ctlr->setup = gxp_spi_setup;
287 ctlr->num_chipselect = data->max_cs;
288 ctlr->dev.of_node = dev->of_node;
290 ret = devm_spi_register_controller(dev, ctlr);
292 return dev_err_probe(&pdev->dev, ret,
293 "failed to register spi controller\n");
299 static const struct gxp_spi_data gxp_spifi_data = {
304 static const struct of_device_id gxp_spifi_match[] = {
305 {.compatible = "hpe,gxp-spifi", .data = &gxp_spifi_data },
308 MODULE_DEVICE_TABLE(of, gxp_spifi_match);
310 static struct platform_driver gxp_spifi_driver = {
311 .probe = gxp_spifi_probe,
314 .of_match_table = gxp_spifi_match,
317 module_platform_driver(gxp_spifi_driver);
319 MODULE_DESCRIPTION("HPE GXP SPI Flash Interface driver");
320 MODULE_AUTHOR("Nick Hawkins <nick.hawkins@hpe.com>");
321 MODULE_LICENSE("GPL");