2 * Copyright (c) 2014, NVIDIA CORPORATION. All rights reserved.
4 * SPDX-License-Identifier: GPL-2.0+
12 #include <spi_flash.h>
14 static long dfu_get_medium_size_sf(struct dfu_entity *dfu)
16 return dfu->data.sf.size;
19 static int dfu_read_medium_sf(struct dfu_entity *dfu, u64 offset, void *buf,
22 return spi_flash_read(dfu->data.sf.dev, offset, *len, buf);
25 static int dfu_write_medium_sf(struct dfu_entity *dfu,
26 u64 offset, void *buf, long *len)
30 ret = spi_flash_erase(dfu->data.sf.dev, offset, *len);
34 ret = spi_flash_write(dfu->data.sf.dev, offset, *len, buf);
41 static int dfu_flush_medium_sf(struct dfu_entity *dfu)
46 static unsigned int dfu_polltimeout_sf(struct dfu_entity *dfu)
48 return DFU_DEFAULT_POLL_TIMEOUT;
51 static void dfu_free_entity_sf(struct dfu_entity *dfu)
53 spi_flash_free(dfu->data.sf.dev);
56 static struct spi_flash *parse_dev(char *devstr)
60 unsigned int speed = CONFIG_SF_DEFAULT_SPEED;
61 unsigned int mode = CONFIG_SF_DEFAULT_MODE;
63 struct spi_flash *dev;
65 s = strsep(&devstr, ":");
66 if (!s || !*s || (bus = simple_strtoul(s, &endp, 0), *endp)) {
67 printf("Invalid SPI bus %s\n", s);
71 s = strsep(&devstr, ":");
72 if (!s || !*s || (cs = simple_strtoul(s, &endp, 0), *endp)) {
73 printf("Invalid SPI chip-select %s\n", s);
77 s = strsep(&devstr, ":");
79 speed = simple_strtoul(s, &endp, 0);
80 if (*endp || !speed) {
81 printf("Invalid SPI speed %s\n", s);
86 s = strsep(&devstr, ":");
88 mode = simple_strtoul(s, &endp, 0);
89 if (*endp || mode > 3) {
90 printf("Invalid SPI mode %s\n", s);
95 dev = spi_flash_probe(bus, cs, speed, mode);
97 printf("Failed to create SPI flash at %d:%d:%d:%d\n",
98 bus, cs, speed, mode);
105 int dfu_fill_entity_sf(struct dfu_entity *dfu, char *devstr, char *s)
109 dfu->data.sf.dev = parse_dev(devstr);
110 if (!dfu->data.sf.dev)
113 dfu->dev_type = DFU_DEV_SF;
114 dfu->max_buf_size = dfu->data.sf.dev->sector_size;
116 st = strsep(&s, " ");
117 if (!strcmp(st, "raw")) {
118 dfu->layout = DFU_RAW_ADDR;
119 dfu->data.sf.start = simple_strtoul(s, &s, 16);
121 dfu->data.sf.size = simple_strtoul(s, &s, 16);
123 printf("%s: Memory layout (%s) not supported!\n", __func__, st);
124 spi_flash_free(dfu->data.sf.dev);
128 dfu->get_medium_size = dfu_get_medium_size_sf;
129 dfu->read_medium = dfu_read_medium_sf;
130 dfu->write_medium = dfu_write_medium_sf;
131 dfu->flush_medium = dfu_flush_medium_sf;
132 dfu->poll_timeout = dfu_polltimeout_sf;
133 dfu->free_entity = dfu_free_entity_sf;