Tom Rini | 83d290c | 2018-05-06 17:58:06 -0400 | [diff] [blame] | 1 | // SPDX-License-Identifier: GPL-2.0+ |
Simon Glass | 4c2dbef | 2014-10-13 23:42:06 -0600 | [diff] [blame] | 2 | /* |
| 3 | * Copyright (c) 2014 Google, Inc |
Simon Glass | 4c2dbef | 2014-10-13 23:42:06 -0600 | [diff] [blame] | 4 | */ |
| 5 | |
| 6 | #include <common.h> |
| 7 | #include <dm.h> |
Simon Glass | f7ae49f | 2020-05-10 11:40:05 -0600 | [diff] [blame] | 8 | #include <log.h> |
Simon Glass | 336d461 | 2020-02-03 07:36:16 -0700 | [diff] [blame] | 9 | #include <malloc.h> |
Simon Glass | 4c2dbef | 2014-10-13 23:42:06 -0600 | [diff] [blame] | 10 | #include <spi.h> |
| 11 | #include <spi_flash.h> |
| 12 | #include <dm/device-internal.h> |
| 13 | #include "sf_internal.h" |
| 14 | |
Michal Simek | 2588f2d | 2015-10-27 13:52:47 +0100 | [diff] [blame] | 15 | DECLARE_GLOBAL_DATA_PTR; |
| 16 | |
Simon Glass | 8d987ab | 2015-03-26 09:29:25 -0600 | [diff] [blame] | 17 | int spi_flash_read_dm(struct udevice *dev, u32 offset, size_t len, void *buf) |
| 18 | { |
Simon Glass | 5e24a2e | 2018-10-01 12:22:24 -0600 | [diff] [blame] | 19 | return log_ret(sf_get_ops(dev)->read(dev, offset, len, buf)); |
Simon Glass | 8d987ab | 2015-03-26 09:29:25 -0600 | [diff] [blame] | 20 | } |
| 21 | |
| 22 | int spi_flash_write_dm(struct udevice *dev, u32 offset, size_t len, |
| 23 | const void *buf) |
| 24 | { |
Simon Glass | 5e24a2e | 2018-10-01 12:22:24 -0600 | [diff] [blame] | 25 | return log_ret(sf_get_ops(dev)->write(dev, offset, len, buf)); |
Simon Glass | 8d987ab | 2015-03-26 09:29:25 -0600 | [diff] [blame] | 26 | } |
| 27 | |
| 28 | int spi_flash_erase_dm(struct udevice *dev, u32 offset, size_t len) |
| 29 | { |
Simon Glass | 5e24a2e | 2018-10-01 12:22:24 -0600 | [diff] [blame] | 30 | return log_ret(sf_get_ops(dev)->erase(dev, offset, len)); |
Simon Glass | 8d987ab | 2015-03-26 09:29:25 -0600 | [diff] [blame] | 31 | } |
| 32 | |
Simon Glass | 4c2dbef | 2014-10-13 23:42:06 -0600 | [diff] [blame] | 33 | /* |
| 34 | * TODO(sjg@chromium.org): This is an old-style function. We should remove |
| 35 | * it when all SPI flash drivers use dm |
| 36 | */ |
| 37 | struct spi_flash *spi_flash_probe(unsigned int bus, unsigned int cs, |
| 38 | unsigned int max_hz, unsigned int spi_mode) |
| 39 | { |
| 40 | struct udevice *dev; |
| 41 | |
| 42 | if (spi_flash_probe_bus_cs(bus, cs, max_hz, spi_mode, &dev)) |
| 43 | return NULL; |
| 44 | |
Simon Glass | e564f05 | 2015-03-05 12:25:20 -0700 | [diff] [blame] | 45 | return dev_get_uclass_priv(dev); |
Simon Glass | 4c2dbef | 2014-10-13 23:42:06 -0600 | [diff] [blame] | 46 | } |
| 47 | |
| 48 | void spi_flash_free(struct spi_flash *flash) |
| 49 | { |
Stefan Roese | 706865a | 2017-03-20 12:51:48 +0100 | [diff] [blame] | 50 | device_remove(flash->spi->dev, DM_REMOVE_NORMAL); |
Simon Glass | 4c2dbef | 2014-10-13 23:42:06 -0600 | [diff] [blame] | 51 | } |
| 52 | |
| 53 | int spi_flash_probe_bus_cs(unsigned int busnum, unsigned int cs, |
| 54 | unsigned int max_hz, unsigned int spi_mode, |
| 55 | struct udevice **devp) |
| 56 | { |
| 57 | struct spi_slave *slave; |
| 58 | struct udevice *bus; |
Simon Glass | 3c8fb12 | 2015-12-29 05:22:47 -0700 | [diff] [blame] | 59 | char *str; |
Simon Glass | 4c2dbef | 2014-10-13 23:42:06 -0600 | [diff] [blame] | 60 | int ret; |
| 61 | |
Simon Glass | 27084c0 | 2019-09-25 08:56:27 -0600 | [diff] [blame] | 62 | #if defined(CONFIG_SPL_BUILD) && CONFIG_IS_ENABLED(USE_TINY_PRINTF) |
Simon Glass | 3c8fb12 | 2015-12-29 05:22:47 -0700 | [diff] [blame] | 63 | str = "spi_flash"; |
| 64 | #else |
| 65 | char name[30]; |
| 66 | |
Haikun.Wang@freescale.com | a5e1bcd | 2015-05-06 10:37:43 +0800 | [diff] [blame] | 67 | snprintf(name, sizeof(name), "spi_flash@%d:%d", busnum, cs); |
Simon Glass | 4c2dbef | 2014-10-13 23:42:06 -0600 | [diff] [blame] | 68 | str = strdup(name); |
Simon Glass | 3c8fb12 | 2015-12-29 05:22:47 -0700 | [diff] [blame] | 69 | #endif |
Simon Glass | 4c2dbef | 2014-10-13 23:42:06 -0600 | [diff] [blame] | 70 | ret = spi_get_bus_and_cs(busnum, cs, max_hz, spi_mode, |
Walter Lozano | e3e2470 | 2020-06-25 01:10:04 -0300 | [diff] [blame] | 71 | "jedec_spi_nor", str, &bus, &slave); |
Simon Glass | 4c2dbef | 2014-10-13 23:42:06 -0600 | [diff] [blame] | 72 | if (ret) |
| 73 | return ret; |
| 74 | |
| 75 | *devp = slave->dev; |
| 76 | return 0; |
| 77 | } |
| 78 | |
Michal Simek | 2588f2d | 2015-10-27 13:52:47 +0100 | [diff] [blame] | 79 | static int spi_flash_post_bind(struct udevice *dev) |
| 80 | { |
| 81 | #if defined(CONFIG_NEEDS_MANUAL_RELOC) |
| 82 | struct dm_spi_flash_ops *ops = sf_get_ops(dev); |
| 83 | static int reloc_done; |
| 84 | |
| 85 | if (!reloc_done) { |
| 86 | if (ops->read) |
| 87 | ops->read += gd->reloc_off; |
| 88 | if (ops->write) |
| 89 | ops->write += gd->reloc_off; |
| 90 | if (ops->erase) |
| 91 | ops->erase += gd->reloc_off; |
| 92 | |
| 93 | reloc_done++; |
| 94 | } |
| 95 | #endif |
| 96 | return 0; |
| 97 | } |
| 98 | |
Simon Glass | 4c2dbef | 2014-10-13 23:42:06 -0600 | [diff] [blame] | 99 | UCLASS_DRIVER(spi_flash) = { |
| 100 | .id = UCLASS_SPI_FLASH, |
| 101 | .name = "spi_flash", |
Michal Simek | 2588f2d | 2015-10-27 13:52:47 +0100 | [diff] [blame] | 102 | .post_bind = spi_flash_post_bind, |
Simon Glass | 4c2dbef | 2014-10-13 23:42:06 -0600 | [diff] [blame] | 103 | .per_device_auto_alloc_size = sizeof(struct spi_flash), |
| 104 | }; |