blob: 350e21aa7d65fd81d21378136b2b197a1eefc0a4 [file] [log] [blame]
Simon Glassd6eedf72014-10-13 23:42:06 -06001/*
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 Glass1c369232015-03-26 09:29:25 -060014int 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
19int 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
25int 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 Glassd6eedf72014-10-13 23:42:06 -060030/*
31 * TODO(sjg@chromium.org): This is an old-style function. We should remove
32 * it when all SPI flash drivers use dm
33 */
34struct 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 Glassde0977b2015-03-05 12:25:20 -070042 return dev_get_uclass_priv(dev);
Simon Glassd6eedf72014-10-13 23:42:06 -060043}
44
45void spi_flash_free(struct spi_flash *flash)
46{
47 spi_flash_remove(flash->spi->dev);
48}
49
50int 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.com16b7ddd2015-05-06 10:37:43 +080056 char name[30], *str;
Simon Glassd6eedf72014-10-13 23:42:06 -060057 int ret;
58
Haikun.Wang@freescale.com16b7ddd2015-05-06 10:37:43 +080059 snprintf(name, sizeof(name), "spi_flash@%d:%d", busnum, cs);
Simon Glassd6eedf72014-10-13 23:42:06 -060060 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
70int spi_flash_remove(struct udevice *dev)
71{
72 return device_remove(dev);
73}
74
75UCLASS_DRIVER(spi_flash) = {
76 .id = UCLASS_SPI_FLASH,
77 .name = "spi_flash",
78 .per_device_auto_alloc_size = sizeof(struct spi_flash),
79};