2 * Copyright (c) 2014, NVIDIA CORPORATION. All rights reserved.
4 * SPDX-License-Identifier: GPL-2.0+
13 #include <spi_flash.h>
15 static long dfu_get_medium_size_sf(struct dfu_entity *dfu)
17 return dfu->data.sf.size;
20 static int dfu_read_medium_sf(struct dfu_entity *dfu, u64 offset, void *buf,
23 return spi_flash_read(dfu->data.sf.dev, offset, *len, buf);
26 static int dfu_write_medium_sf(struct dfu_entity *dfu,
27 u64 offset, void *buf, long *len)
31 ret = spi_flash_erase(dfu->data.sf.dev, offset, *len);
35 ret = spi_flash_write(dfu->data.sf.dev, offset, *len, buf);
42 static int dfu_flush_medium_sf(struct dfu_entity *dfu)
47 static unsigned int dfu_polltimeout_sf(struct dfu_entity *dfu)
49 return DFU_DEFAULT_POLL_TIMEOUT;
52 static void dfu_free_entity_sf(struct dfu_entity *dfu)
54 spi_flash_free(dfu->data.sf.dev);
57 static struct spi_flash *parse_dev(char *devstr)
61 unsigned int speed = CONFIG_SF_DEFAULT_SPEED;
62 unsigned int mode = CONFIG_SF_DEFAULT_MODE;
64 struct spi_flash *dev;
66 s = strsep(&devstr, ":");
67 if (!s || !*s || (bus = simple_strtoul(s, &endp, 0), *endp)) {
68 printf("Invalid SPI bus %s\n", s);
72 s = strsep(&devstr, ":");
73 if (!s || !*s || (cs = simple_strtoul(s, &endp, 0), *endp)) {
74 printf("Invalid SPI chip-select %s\n", s);
78 s = strsep(&devstr, ":");
80 speed = simple_strtoul(s, &endp, 0);
81 if (*endp || !speed) {
82 printf("Invalid SPI speed %s\n", s);
87 s = strsep(&devstr, ":");
89 mode = simple_strtoul(s, &endp, 0);
90 if (*endp || mode > 3) {
91 printf("Invalid SPI mode %s\n", s);
96 dev = spi_flash_probe(bus, cs, speed, mode);
98 printf("Failed to create SPI flash at %d:%d:%d:%d\n",
99 bus, cs, speed, mode);
106 int dfu_fill_entity_sf(struct dfu_entity *dfu, char *devstr, char *s)
110 dfu->data.sf.dev = parse_dev(devstr);
111 if (!dfu->data.sf.dev)
114 dfu->dev_type = DFU_DEV_SF;
115 dfu->max_buf_size = dfu->data.sf.dev->sector_size;
117 st = strsep(&s, " ");
118 if (!strcmp(st, "raw")) {
119 dfu->layout = DFU_RAW_ADDR;
120 dfu->data.sf.start = simple_strtoul(s, &s, 16);
122 dfu->data.sf.size = simple_strtoul(s, &s, 16);
124 printf("%s: Memory layout (%s) not supported!\n", __func__, st);
125 spi_flash_free(dfu->data.sf.dev);
129 dfu->get_medium_size = dfu_get_medium_size_sf;
130 dfu->read_medium = dfu_read_medium_sf;
131 dfu->write_medium = dfu_write_medium_sf;
132 dfu->flush_medium = dfu_flush_medium_sf;
133 dfu->poll_timeout = dfu_polltimeout_sf;
134 dfu->free_entity = dfu_free_entity_sf;