Lines Matching +full:spi +full:- +full:nor

1 // SPDX-License-Identifier: GPL-2.0-only
3 * SPI NOR driver for NXP SPI Flash Interface (SPIFI)
18 #include <linux/mtd/spi-nor.h>
22 #include <linux/spi/spi.h>
59 struct spi_nor nor; member
69 ret = readb_poll_timeout(spifi->io_base + SPIFI_STAT, stat, in nxp_spifi_wait_for_cmd()
72 dev_warn(spifi->dev, "command timed out\n"); in nxp_spifi_wait_for_cmd()
82 writel(SPIFI_STAT_RESET, spifi->io_base + SPIFI_STAT); in nxp_spifi_reset()
83 ret = readb_poll_timeout(spifi->io_base + SPIFI_STAT, stat, in nxp_spifi_reset()
86 dev_warn(spifi->dev, "state reset timed out\n"); in nxp_spifi_reset()
95 if (!spifi->memory_mode) in nxp_spifi_set_memory_mode_off()
100 dev_err(spifi->dev, "unable to enter command mode\n"); in nxp_spifi_set_memory_mode_off()
102 spifi->memory_mode = false; in nxp_spifi_set_memory_mode_off()
112 if (spifi->memory_mode) in nxp_spifi_set_memory_mode_on()
115 writel(spifi->mcmd, spifi->io_base + SPIFI_MCMD); in nxp_spifi_set_memory_mode_on()
116 ret = readb_poll_timeout(spifi->io_base + SPIFI_STAT, stat, in nxp_spifi_set_memory_mode_on()
119 dev_err(spifi->dev, "unable to enter memory mode\n"); in nxp_spifi_set_memory_mode_on()
121 spifi->memory_mode = true; in nxp_spifi_set_memory_mode_on()
126 static int nxp_spifi_read_reg(struct spi_nor *nor, u8 opcode, u8 *buf, in nxp_spifi_read_reg() argument
129 struct nxp_spifi *spifi = nor->priv; in nxp_spifi_read_reg()
141 writel(cmd, spifi->io_base + SPIFI_CMD); in nxp_spifi_read_reg()
143 while (len--) in nxp_spifi_read_reg()
144 *buf++ = readb(spifi->io_base + SPIFI_DATA); in nxp_spifi_read_reg()
149 static int nxp_spifi_write_reg(struct spi_nor *nor, u8 opcode, const u8 *buf, in nxp_spifi_write_reg() argument
152 struct nxp_spifi *spifi = nor->priv; in nxp_spifi_write_reg()
165 writel(cmd, spifi->io_base + SPIFI_CMD); in nxp_spifi_write_reg()
167 while (len--) in nxp_spifi_write_reg()
168 writeb(*buf++, spifi->io_base + SPIFI_DATA); in nxp_spifi_write_reg()
173 static ssize_t nxp_spifi_read(struct spi_nor *nor, loff_t from, size_t len, in nxp_spifi_read() argument
176 struct nxp_spifi *spifi = nor->priv; in nxp_spifi_read()
183 memcpy_fromio(buf, spifi->flash_base + from, len); in nxp_spifi_read()
188 static ssize_t nxp_spifi_write(struct spi_nor *nor, loff_t to, size_t len, in nxp_spifi_write() argument
191 struct nxp_spifi *spifi = nor->priv; in nxp_spifi_write()
200 writel(to, spifi->io_base + SPIFI_ADDR); in nxp_spifi_write()
205 SPIFI_CMD_OPCODE(nor->program_opcode) | in nxp_spifi_write()
206 SPIFI_CMD_FRAMEFORM(spifi->nor.addr_nbytes + 1); in nxp_spifi_write()
207 writel(cmd, spifi->io_base + SPIFI_CMD); in nxp_spifi_write()
210 writeb(buf[i], spifi->io_base + SPIFI_DATA); in nxp_spifi_write()
219 static int nxp_spifi_erase(struct spi_nor *nor, loff_t offs) in nxp_spifi_erase() argument
221 struct nxp_spifi *spifi = nor->priv; in nxp_spifi_erase()
229 writel(offs, spifi->io_base + SPIFI_ADDR); in nxp_spifi_erase()
232 SPIFI_CMD_OPCODE(nor->erase_opcode) | in nxp_spifi_erase()
233 SPIFI_CMD_FRAMEFORM(spifi->nor.addr_nbytes + 1); in nxp_spifi_erase()
234 writel(cmd, spifi->io_base + SPIFI_CMD); in nxp_spifi_erase()
241 switch (spifi->nor.read_proto) { in nxp_spifi_setup_memory_cmd()
243 spifi->mcmd = SPIFI_CMD_FIELDFORM_ALL_SERIAL; in nxp_spifi_setup_memory_cmd()
247 spifi->mcmd = SPIFI_CMD_FIELDFORM_QUAD_DUAL_DATA; in nxp_spifi_setup_memory_cmd()
250 dev_err(spifi->dev, "unsupported SPI read mode\n"); in nxp_spifi_setup_memory_cmd()
251 return -EINVAL; in nxp_spifi_setup_memory_cmd()
255 if (spifi->nor.addr_nbytes < 1 || spifi->nor.addr_nbytes > 4) in nxp_spifi_setup_memory_cmd()
256 return -EINVAL; in nxp_spifi_setup_memory_cmd()
258 spifi->mcmd |= SPIFI_CMD_OPCODE(spifi->nor.read_opcode) | in nxp_spifi_setup_memory_cmd()
259 SPIFI_CMD_INTLEN(spifi->nor.read_dummy / 8) | in nxp_spifi_setup_memory_cmd()
260 SPIFI_CMD_FRAMEFORM(spifi->nor.addr_nbytes + 1); in nxp_spifi_setup_memory_cmd()
265 static void nxp_spifi_dummy_id_read(struct spi_nor *nor) in nxp_spifi_dummy_id_read() argument
268 nor->controller_ops->read_reg(nor, SPINOR_OP_RDID, id, in nxp_spifi_dummy_id_read()
292 if (!of_property_read_u32(np, "spi-rx-bus-width", &property)) { in nxp_spifi_setup_flash()
303 dev_err(spifi->dev, "unsupported rx-bus-width\n"); in nxp_spifi_setup_flash()
304 return -EINVAL; in nxp_spifi_setup_flash()
308 if (of_find_property(np, "spi-cpha", NULL)) in nxp_spifi_setup_flash()
311 if (of_find_property(np, "spi-cpol", NULL)) in nxp_spifi_setup_flash()
337 dev_err(spifi->dev, "only mode 0 and 3 supported\n"); in nxp_spifi_setup_flash()
338 return -EINVAL; in nxp_spifi_setup_flash()
341 writel(ctrl, spifi->io_base + SPIFI_CTRL); in nxp_spifi_setup_flash()
343 spifi->nor.dev = spifi->dev; in nxp_spifi_setup_flash()
344 spi_nor_set_flash_node(&spifi->nor, np); in nxp_spifi_setup_flash()
345 spifi->nor.priv = spifi; in nxp_spifi_setup_flash()
346 spifi->nor.controller_ops = &nxp_spifi_controller_ops; in nxp_spifi_setup_flash()
357 nxp_spifi_dummy_id_read(&spifi->nor); in nxp_spifi_setup_flash()
359 ret = spi_nor_scan(&spifi->nor, NULL, &hwcaps); in nxp_spifi_setup_flash()
361 dev_err(spifi->dev, "device scan failed\n"); in nxp_spifi_setup_flash()
367 dev_err(spifi->dev, "memory command setup failed\n"); in nxp_spifi_setup_flash()
371 ret = mtd_device_register(&spifi->nor.mtd, NULL, 0); in nxp_spifi_setup_flash()
373 dev_err(spifi->dev, "mtd device parse failed\n"); in nxp_spifi_setup_flash()
386 spifi = devm_kzalloc(&pdev->dev, sizeof(*spifi), GFP_KERNEL); in nxp_spifi_probe()
388 return -ENOMEM; in nxp_spifi_probe()
390 spifi->io_base = devm_platform_ioremap_resource_byname(pdev, "spifi"); in nxp_spifi_probe()
391 if (IS_ERR(spifi->io_base)) in nxp_spifi_probe()
392 return PTR_ERR(spifi->io_base); in nxp_spifi_probe()
394 spifi->flash_base = devm_platform_ioremap_resource_byname(pdev, "flash"); in nxp_spifi_probe()
395 if (IS_ERR(spifi->flash_base)) in nxp_spifi_probe()
396 return PTR_ERR(spifi->flash_base); in nxp_spifi_probe()
398 spifi->clk_spifi = devm_clk_get(&pdev->dev, "spifi"); in nxp_spifi_probe()
399 if (IS_ERR(spifi->clk_spifi)) { in nxp_spifi_probe()
400 dev_err(&pdev->dev, "spifi clock not found\n"); in nxp_spifi_probe()
401 return PTR_ERR(spifi->clk_spifi); in nxp_spifi_probe()
404 spifi->clk_reg = devm_clk_get(&pdev->dev, "reg"); in nxp_spifi_probe()
405 if (IS_ERR(spifi->clk_reg)) { in nxp_spifi_probe()
406 dev_err(&pdev->dev, "reg clock not found\n"); in nxp_spifi_probe()
407 return PTR_ERR(spifi->clk_reg); in nxp_spifi_probe()
410 ret = clk_prepare_enable(spifi->clk_reg); in nxp_spifi_probe()
412 dev_err(&pdev->dev, "unable to enable reg clock\n"); in nxp_spifi_probe()
416 ret = clk_prepare_enable(spifi->clk_spifi); in nxp_spifi_probe()
418 dev_err(&pdev->dev, "unable to enable spifi clock\n"); in nxp_spifi_probe()
422 spifi->dev = &pdev->dev; in nxp_spifi_probe()
427 writel(0, spifi->io_base + SPIFI_IDATA); in nxp_spifi_probe()
428 writel(0, spifi->io_base + SPIFI_MCMD); in nxp_spifi_probe()
431 flash_np = of_get_next_available_child(pdev->dev.of_node, NULL); in nxp_spifi_probe()
433 dev_err(&pdev->dev, "no SPI flash device to configure\n"); in nxp_spifi_probe()
434 ret = -ENODEV; in nxp_spifi_probe()
441 dev_err(&pdev->dev, "unable to setup flash chip\n"); in nxp_spifi_probe()
448 clk_disable_unprepare(spifi->clk_spifi); in nxp_spifi_probe()
450 clk_disable_unprepare(spifi->clk_reg); in nxp_spifi_probe()
458 mtd_device_unregister(&spifi->nor.mtd); in nxp_spifi_remove()
459 clk_disable_unprepare(spifi->clk_spifi); in nxp_spifi_remove()
460 clk_disable_unprepare(spifi->clk_reg); in nxp_spifi_remove()
466 {.compatible = "nxp,lpc1773-spifi"},
475 .name = "nxp-spifi",
481 MODULE_DESCRIPTION("NXP SPI Flash Interface driver");