Tom Rini | 83d290c | 2018-05-06 17:58:06 -0400 | [diff] [blame] | 1 | // SPDX-License-Identifier: GPL-2.0+ |
Stephen Warren | 6f12ebf | 2014-06-11 16:03:36 -0600 | [diff] [blame] | 2 | /* |
| 3 | * Copyright (c) 2014, NVIDIA CORPORATION. All rights reserved. |
Stephen Warren | 6f12ebf | 2014-06-11 16:03:36 -0600 | [diff] [blame] | 4 | */ |
| 5 | |
| 6 | #include <common.h> |
| 7 | #include <malloc.h> |
| 8 | #include <errno.h> |
| 9 | #include <div64.h> |
| 10 | #include <dfu.h> |
Simon Glass | 843c9e8 | 2014-10-13 23:41:55 -0600 | [diff] [blame] | 11 | #include <spi.h> |
Stephen Warren | 6f12ebf | 2014-06-11 16:03:36 -0600 | [diff] [blame] | 12 | #include <spi_flash.h> |
| 13 | |
Patrick Delaunay | 15970d8 | 2017-07-19 16:39:23 +0200 | [diff] [blame] | 14 | static int dfu_get_medium_size_sf(struct dfu_entity *dfu, u64 *size) |
Stephen Warren | 6f12ebf | 2014-06-11 16:03:36 -0600 | [diff] [blame] | 15 | { |
Patrick Delaunay | 4de5120 | 2017-07-19 16:39:22 +0200 | [diff] [blame] | 16 | *size = dfu->data.sf.size; |
| 17 | |
| 18 | return 0; |
Stephen Warren | 6f12ebf | 2014-06-11 16:03:36 -0600 | [diff] [blame] | 19 | } |
| 20 | |
| 21 | static int dfu_read_medium_sf(struct dfu_entity *dfu, u64 offset, void *buf, |
| 22 | long *len) |
| 23 | { |
Phil Edworthy | e5f00f0 | 2017-05-24 15:09:49 +0100 | [diff] [blame] | 24 | return spi_flash_read(dfu->data.sf.dev, dfu->data.sf.start + offset, |
| 25 | *len, buf); |
Stephen Warren | 6f12ebf | 2014-06-11 16:03:36 -0600 | [diff] [blame] | 26 | } |
| 27 | |
Fabio Estevam | 2727f3b | 2015-09-23 00:50:39 -0300 | [diff] [blame] | 28 | static u64 find_sector(struct dfu_entity *dfu, u64 start, u64 offset) |
| 29 | { |
| 30 | return (lldiv((start + offset), dfu->data.sf.dev->sector_size)) * |
| 31 | dfu->data.sf.dev->sector_size; |
| 32 | } |
| 33 | |
Stephen Warren | 6f12ebf | 2014-06-11 16:03:36 -0600 | [diff] [blame] | 34 | static int dfu_write_medium_sf(struct dfu_entity *dfu, |
| 35 | u64 offset, void *buf, long *len) |
| 36 | { |
| 37 | int ret; |
| 38 | |
Fabio Estevam | 2727f3b | 2015-09-23 00:50:39 -0300 | [diff] [blame] | 39 | ret = spi_flash_erase(dfu->data.sf.dev, |
| 40 | find_sector(dfu, dfu->data.sf.start, offset), |
Fabio Estevam | f4c9258 | 2015-09-22 00:55:00 -0300 | [diff] [blame] | 41 | dfu->data.sf.dev->sector_size); |
Stephen Warren | 6f12ebf | 2014-06-11 16:03:36 -0600 | [diff] [blame] | 42 | if (ret) |
| 43 | return ret; |
| 44 | |
Fabio Estevam | 2727f3b | 2015-09-23 00:50:39 -0300 | [diff] [blame] | 45 | ret = spi_flash_write(dfu->data.sf.dev, dfu->data.sf.start + offset, |
| 46 | *len, buf); |
Stephen Warren | 6f12ebf | 2014-06-11 16:03:36 -0600 | [diff] [blame] | 47 | if (ret) |
| 48 | return ret; |
| 49 | |
| 50 | return 0; |
| 51 | } |
| 52 | |
| 53 | static int dfu_flush_medium_sf(struct dfu_entity *dfu) |
| 54 | { |
| 55 | return 0; |
| 56 | } |
| 57 | |
| 58 | static unsigned int dfu_polltimeout_sf(struct dfu_entity *dfu) |
| 59 | { |
| 60 | return DFU_DEFAULT_POLL_TIMEOUT; |
| 61 | } |
| 62 | |
| 63 | static void dfu_free_entity_sf(struct dfu_entity *dfu) |
| 64 | { |
| 65 | spi_flash_free(dfu->data.sf.dev); |
| 66 | } |
| 67 | |
| 68 | static struct spi_flash *parse_dev(char *devstr) |
| 69 | { |
| 70 | unsigned int bus; |
| 71 | unsigned int cs; |
| 72 | unsigned int speed = CONFIG_SF_DEFAULT_SPEED; |
| 73 | unsigned int mode = CONFIG_SF_DEFAULT_MODE; |
| 74 | char *s, *endp; |
| 75 | struct spi_flash *dev; |
| 76 | |
| 77 | s = strsep(&devstr, ":"); |
| 78 | if (!s || !*s || (bus = simple_strtoul(s, &endp, 0), *endp)) { |
| 79 | printf("Invalid SPI bus %s\n", s); |
| 80 | return NULL; |
| 81 | } |
| 82 | |
| 83 | s = strsep(&devstr, ":"); |
| 84 | if (!s || !*s || (cs = simple_strtoul(s, &endp, 0), *endp)) { |
| 85 | printf("Invalid SPI chip-select %s\n", s); |
| 86 | return NULL; |
| 87 | } |
| 88 | |
| 89 | s = strsep(&devstr, ":"); |
| 90 | if (s && *s) { |
| 91 | speed = simple_strtoul(s, &endp, 0); |
| 92 | if (*endp || !speed) { |
| 93 | printf("Invalid SPI speed %s\n", s); |
| 94 | return NULL; |
| 95 | } |
| 96 | } |
| 97 | |
| 98 | s = strsep(&devstr, ":"); |
| 99 | if (s && *s) { |
| 100 | mode = simple_strtoul(s, &endp, 0); |
| 101 | if (*endp || mode > 3) { |
| 102 | printf("Invalid SPI mode %s\n", s); |
| 103 | return NULL; |
| 104 | } |
| 105 | } |
| 106 | |
| 107 | dev = spi_flash_probe(bus, cs, speed, mode); |
| 108 | if (!dev) { |
| 109 | printf("Failed to create SPI flash at %d:%d:%d:%d\n", |
| 110 | bus, cs, speed, mode); |
| 111 | return NULL; |
| 112 | } |
| 113 | |
| 114 | return dev; |
| 115 | } |
| 116 | |
| 117 | int dfu_fill_entity_sf(struct dfu_entity *dfu, char *devstr, char *s) |
| 118 | { |
| 119 | char *st; |
Vignesh R | 30e3ea4 | 2015-10-20 15:22:00 +0530 | [diff] [blame] | 120 | char *devstr_bkup = strdup(devstr); |
Stephen Warren | 6f12ebf | 2014-06-11 16:03:36 -0600 | [diff] [blame] | 121 | |
Vignesh R | 30e3ea4 | 2015-10-20 15:22:00 +0530 | [diff] [blame] | 122 | dfu->data.sf.dev = parse_dev(devstr_bkup); |
| 123 | free(devstr_bkup); |
Stephen Warren | 6f12ebf | 2014-06-11 16:03:36 -0600 | [diff] [blame] | 124 | if (!dfu->data.sf.dev) |
| 125 | return -ENODEV; |
| 126 | |
| 127 | dfu->dev_type = DFU_DEV_SF; |
| 128 | dfu->max_buf_size = dfu->data.sf.dev->sector_size; |
| 129 | |
| 130 | st = strsep(&s, " "); |
| 131 | if (!strcmp(st, "raw")) { |
| 132 | dfu->layout = DFU_RAW_ADDR; |
| 133 | dfu->data.sf.start = simple_strtoul(s, &s, 16); |
| 134 | s++; |
| 135 | dfu->data.sf.size = simple_strtoul(s, &s, 16); |
| 136 | } else { |
| 137 | printf("%s: Memory layout (%s) not supported!\n", __func__, st); |
| 138 | spi_flash_free(dfu->data.sf.dev); |
| 139 | return -1; |
| 140 | } |
| 141 | |
| 142 | dfu->get_medium_size = dfu_get_medium_size_sf; |
| 143 | dfu->read_medium = dfu_read_medium_sf; |
| 144 | dfu->write_medium = dfu_write_medium_sf; |
| 145 | dfu->flush_medium = dfu_flush_medium_sf; |
| 146 | dfu->poll_timeout = dfu_polltimeout_sf; |
| 147 | dfu->free_entity = dfu_free_entity_sf; |
| 148 | |
| 149 | /* initial state */ |
| 150 | dfu->inited = 0; |
| 151 | |
| 152 | return 0; |
| 153 | } |