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 u64 find_sector(struct dfu_entity *dfu, u64 start, u64 offset)
28 return (lldiv((start + offset), dfu->data.sf.dev->sector_size)) *
29 dfu->data.sf.dev->sector_size;
32 static int dfu_write_medium_sf(struct dfu_entity *dfu,
33 u64 offset, void *buf, long *len)
37 ret = spi_flash_erase(dfu->data.sf.dev,
38 find_sector(dfu, dfu->data.sf.start, offset),
39 dfu->data.sf.dev->sector_size);
43 ret = spi_flash_write(dfu->data.sf.dev, dfu->data.sf.start + offset,
51 static int dfu_flush_medium_sf(struct dfu_entity *dfu)
56 static unsigned int dfu_polltimeout_sf(struct dfu_entity *dfu)
58 return DFU_DEFAULT_POLL_TIMEOUT;
61 static void dfu_free_entity_sf(struct dfu_entity *dfu)
63 spi_flash_free(dfu->data.sf.dev);
66 static struct spi_flash *parse_dev(char *devstr)
70 unsigned int speed = CONFIG_SF_DEFAULT_SPEED;
71 unsigned int mode = CONFIG_SF_DEFAULT_MODE;
73 struct spi_flash *dev;
75 s = strsep(&devstr, ":");
76 if (!s || !*s || (bus = simple_strtoul(s, &endp, 0), *endp)) {
77 printf("Invalid SPI bus %s\n", s);
81 s = strsep(&devstr, ":");
82 if (!s || !*s || (cs = simple_strtoul(s, &endp, 0), *endp)) {
83 printf("Invalid SPI chip-select %s\n", s);
87 s = strsep(&devstr, ":");
89 speed = simple_strtoul(s, &endp, 0);
90 if (*endp || !speed) {
91 printf("Invalid SPI speed %s\n", s);
96 s = strsep(&devstr, ":");
98 mode = simple_strtoul(s, &endp, 0);
99 if (*endp || mode > 3) {
100 printf("Invalid SPI mode %s\n", s);
105 dev = spi_flash_probe(bus, cs, speed, mode);
107 printf("Failed to create SPI flash at %d:%d:%d:%d\n",
108 bus, cs, speed, mode);
115 int dfu_fill_entity_sf(struct dfu_entity *dfu, char *devstr, char *s)
118 char *devstr_bkup = strdup(devstr);
120 dfu->data.sf.dev = parse_dev(devstr_bkup);
122 if (!dfu->data.sf.dev)
125 dfu->dev_type = DFU_DEV_SF;
126 dfu->max_buf_size = dfu->data.sf.dev->sector_size;
128 st = strsep(&s, " ");
129 if (!strcmp(st, "raw")) {
130 dfu->layout = DFU_RAW_ADDR;
131 dfu->data.sf.start = simple_strtoul(s, &s, 16);
133 dfu->data.sf.size = simple_strtoul(s, &s, 16);
135 printf("%s: Memory layout (%s) not supported!\n", __func__, st);
136 spi_flash_free(dfu->data.sf.dev);
140 dfu->get_medium_size = dfu_get_medium_size_sf;
141 dfu->read_medium = dfu_read_medium_sf;
142 dfu->write_medium = dfu_write_medium_sf;
143 dfu->flush_medium = dfu_flush_medium_sf;
144 dfu->poll_timeout = dfu_polltimeout_sf;
145 dfu->free_entity = dfu_free_entity_sf;