1 /* 2 * Copyright (c) 2014, NVIDIA CORPORATION. All rights reserved. 3 * 4 * SPDX-License-Identifier: GPL-2.0+ 5 */ 6 7 #include <common.h> 8 #include <malloc.h> 9 #include <errno.h> 10 #include <div64.h> 11 #include <dfu.h> 12 #include <spi_flash.h> 13 14 static long dfu_get_medium_size_sf(struct dfu_entity *dfu) 15 { 16 return dfu->data.sf.size; 17 } 18 19 static int dfu_read_medium_sf(struct dfu_entity *dfu, u64 offset, void *buf, 20 long *len) 21 { 22 return spi_flash_read(dfu->data.sf.dev, offset, *len, buf); 23 } 24 25 static int dfu_write_medium_sf(struct dfu_entity *dfu, 26 u64 offset, void *buf, long *len) 27 { 28 int ret; 29 30 ret = spi_flash_erase(dfu->data.sf.dev, offset, *len); 31 if (ret) 32 return ret; 33 34 ret = spi_flash_write(dfu->data.sf.dev, offset, *len, buf); 35 if (ret) 36 return ret; 37 38 return 0; 39 } 40 41 static int dfu_flush_medium_sf(struct dfu_entity *dfu) 42 { 43 return 0; 44 } 45 46 static unsigned int dfu_polltimeout_sf(struct dfu_entity *dfu) 47 { 48 return DFU_DEFAULT_POLL_TIMEOUT; 49 } 50 51 static void dfu_free_entity_sf(struct dfu_entity *dfu) 52 { 53 spi_flash_free(dfu->data.sf.dev); 54 } 55 56 static struct spi_flash *parse_dev(char *devstr) 57 { 58 unsigned int bus; 59 unsigned int cs; 60 unsigned int speed = CONFIG_SF_DEFAULT_SPEED; 61 unsigned int mode = CONFIG_SF_DEFAULT_MODE; 62 char *s, *endp; 63 struct spi_flash *dev; 64 65 s = strsep(&devstr, ":"); 66 if (!s || !*s || (bus = simple_strtoul(s, &endp, 0), *endp)) { 67 printf("Invalid SPI bus %s\n", s); 68 return NULL; 69 } 70 71 s = strsep(&devstr, ":"); 72 if (!s || !*s || (cs = simple_strtoul(s, &endp, 0), *endp)) { 73 printf("Invalid SPI chip-select %s\n", s); 74 return NULL; 75 } 76 77 s = strsep(&devstr, ":"); 78 if (s && *s) { 79 speed = simple_strtoul(s, &endp, 0); 80 if (*endp || !speed) { 81 printf("Invalid SPI speed %s\n", s); 82 return NULL; 83 } 84 } 85 86 s = strsep(&devstr, ":"); 87 if (s && *s) { 88 mode = simple_strtoul(s, &endp, 0); 89 if (*endp || mode > 3) { 90 printf("Invalid SPI mode %s\n", s); 91 return NULL; 92 } 93 } 94 95 dev = spi_flash_probe(bus, cs, speed, mode); 96 if (!dev) { 97 printf("Failed to create SPI flash at %d:%d:%d:%d\n", 98 bus, cs, speed, mode); 99 return NULL; 100 } 101 102 return dev; 103 } 104 105 int dfu_fill_entity_sf(struct dfu_entity *dfu, char *devstr, char *s) 106 { 107 char *st; 108 109 dfu->data.sf.dev = parse_dev(devstr); 110 if (!dfu->data.sf.dev) 111 return -ENODEV; 112 113 dfu->dev_type = DFU_DEV_SF; 114 dfu->max_buf_size = dfu->data.sf.dev->sector_size; 115 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); 120 s++; 121 dfu->data.sf.size = simple_strtoul(s, &s, 16); 122 } else { 123 printf("%s: Memory layout (%s) not supported!\n", __func__, st); 124 spi_flash_free(dfu->data.sf.dev); 125 return -1; 126 } 127 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; 134 135 /* initial state */ 136 dfu->inited = 0; 137 138 return 0; 139 } 140