Simon Glass | d6eedf7 | 2014-10-13 23:42:06 -0600 | [diff] [blame] | 1 | /* |
| 2 | * Copyright (c) 2014 Google, Inc |
| 3 | * |
| 4 | * SPDX-License-Identifier: GPL-2.0+ |
| 5 | */ |
| 6 | |
| 7 | #include <common.h> |
| 8 | #include <dm.h> |
| 9 | #include <spi.h> |
| 10 | #include <spi_flash.h> |
| 11 | #include <dm/device-internal.h> |
| 12 | #include "sf_internal.h" |
| 13 | |
Simon Glass | 1c36923 | 2015-03-26 09:29:25 -0600 | [diff] [blame] | 14 | int spi_flash_read_dm(struct udevice *dev, u32 offset, size_t len, void *buf) |
| 15 | { |
| 16 | return sf_get_ops(dev)->read(dev, offset, len, buf); |
| 17 | } |
| 18 | |
| 19 | int spi_flash_write_dm(struct udevice *dev, u32 offset, size_t len, |
| 20 | const void *buf) |
| 21 | { |
| 22 | return sf_get_ops(dev)->write(dev, offset, len, buf); |
| 23 | } |
| 24 | |
| 25 | int spi_flash_erase_dm(struct udevice *dev, u32 offset, size_t len) |
| 26 | { |
| 27 | return sf_get_ops(dev)->erase(dev, offset, len); |
| 28 | } |
| 29 | |
Simon Glass | d6eedf7 | 2014-10-13 23:42:06 -0600 | [diff] [blame] | 30 | /* |
| 31 | * TODO(sjg@chromium.org): This is an old-style function. We should remove |
| 32 | * it when all SPI flash drivers use dm |
| 33 | */ |
| 34 | struct spi_flash *spi_flash_probe(unsigned int bus, unsigned int cs, |
| 35 | unsigned int max_hz, unsigned int spi_mode) |
| 36 | { |
| 37 | struct udevice *dev; |
| 38 | |
| 39 | if (spi_flash_probe_bus_cs(bus, cs, max_hz, spi_mode, &dev)) |
| 40 | return NULL; |
| 41 | |
Simon Glass | de0977b | 2015-03-05 12:25:20 -0700 | [diff] [blame] | 42 | return dev_get_uclass_priv(dev); |
Simon Glass | d6eedf7 | 2014-10-13 23:42:06 -0600 | [diff] [blame] | 43 | } |
| 44 | |
| 45 | void spi_flash_free(struct spi_flash *flash) |
| 46 | { |
| 47 | spi_flash_remove(flash->spi->dev); |
| 48 | } |
| 49 | |
| 50 | int spi_flash_probe_bus_cs(unsigned int busnum, unsigned int cs, |
| 51 | unsigned int max_hz, unsigned int spi_mode, |
| 52 | struct udevice **devp) |
| 53 | { |
| 54 | struct spi_slave *slave; |
| 55 | struct udevice *bus; |
Haikun.Wang@freescale.com | 16b7ddd | 2015-05-06 10:37:43 +0800 | [diff] [blame^] | 56 | char name[30], *str; |
Simon Glass | d6eedf7 | 2014-10-13 23:42:06 -0600 | [diff] [blame] | 57 | int ret; |
| 58 | |
Haikun.Wang@freescale.com | 16b7ddd | 2015-05-06 10:37:43 +0800 | [diff] [blame^] | 59 | snprintf(name, sizeof(name), "spi_flash@%d:%d", busnum, cs); |
Simon Glass | d6eedf7 | 2014-10-13 23:42:06 -0600 | [diff] [blame] | 60 | str = strdup(name); |
| 61 | ret = spi_get_bus_and_cs(busnum, cs, max_hz, spi_mode, |
| 62 | "spi_flash_std", str, &bus, &slave); |
| 63 | if (ret) |
| 64 | return ret; |
| 65 | |
| 66 | *devp = slave->dev; |
| 67 | return 0; |
| 68 | } |
| 69 | |
| 70 | int spi_flash_remove(struct udevice *dev) |
| 71 | { |
| 72 | return device_remove(dev); |
| 73 | } |
| 74 | |
| 75 | UCLASS_DRIVER(spi_flash) = { |
| 76 | .id = UCLASS_SPI_FLASH, |
| 77 | .name = "spi_flash", |
| 78 | .per_device_auto_alloc_size = sizeof(struct spi_flash), |
| 79 | }; |