blob: b6d5fe24dc82e56620460185c875243bb0c8ef63 [file] [log] [blame]
Stephen Warren0280af72014-06-11 16:03:36 -06001/*
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>
Simon Glassdef92152014-10-13 23:41:55 -060012#include <spi.h>
Stephen Warren0280af72014-06-11 16:03:36 -060013#include <spi_flash.h>
14
15static long dfu_get_medium_size_sf(struct dfu_entity *dfu)
16{
17 return dfu->data.sf.size;
18}
19
20static int dfu_read_medium_sf(struct dfu_entity *dfu, u64 offset, void *buf,
21 long *len)
22{
Phil Edworthye01036c2017-05-24 15:09:49 +010023 return spi_flash_read(dfu->data.sf.dev, dfu->data.sf.start + offset,
24 *len, buf);
Stephen Warren0280af72014-06-11 16:03:36 -060025}
26
Fabio Estevam4556b352015-09-23 00:50:39 -030027static u64 find_sector(struct dfu_entity *dfu, u64 start, u64 offset)
28{
29 return (lldiv((start + offset), dfu->data.sf.dev->sector_size)) *
30 dfu->data.sf.dev->sector_size;
31}
32
Stephen Warren0280af72014-06-11 16:03:36 -060033static int dfu_write_medium_sf(struct dfu_entity *dfu,
34 u64 offset, void *buf, long *len)
35{
36 int ret;
37
Fabio Estevam4556b352015-09-23 00:50:39 -030038 ret = spi_flash_erase(dfu->data.sf.dev,
39 find_sector(dfu, dfu->data.sf.start, offset),
Fabio Estevameae41e92015-09-22 00:55:00 -030040 dfu->data.sf.dev->sector_size);
Stephen Warren0280af72014-06-11 16:03:36 -060041 if (ret)
42 return ret;
43
Fabio Estevam4556b352015-09-23 00:50:39 -030044 ret = spi_flash_write(dfu->data.sf.dev, dfu->data.sf.start + offset,
45 *len, buf);
Stephen Warren0280af72014-06-11 16:03:36 -060046 if (ret)
47 return ret;
48
49 return 0;
50}
51
52static int dfu_flush_medium_sf(struct dfu_entity *dfu)
53{
54 return 0;
55}
56
57static unsigned int dfu_polltimeout_sf(struct dfu_entity *dfu)
58{
59 return DFU_DEFAULT_POLL_TIMEOUT;
60}
61
62static void dfu_free_entity_sf(struct dfu_entity *dfu)
63{
64 spi_flash_free(dfu->data.sf.dev);
65}
66
67static struct spi_flash *parse_dev(char *devstr)
68{
69 unsigned int bus;
70 unsigned int cs;
71 unsigned int speed = CONFIG_SF_DEFAULT_SPEED;
72 unsigned int mode = CONFIG_SF_DEFAULT_MODE;
73 char *s, *endp;
74 struct spi_flash *dev;
75
76 s = strsep(&devstr, ":");
77 if (!s || !*s || (bus = simple_strtoul(s, &endp, 0), *endp)) {
78 printf("Invalid SPI bus %s\n", s);
79 return NULL;
80 }
81
82 s = strsep(&devstr, ":");
83 if (!s || !*s || (cs = simple_strtoul(s, &endp, 0), *endp)) {
84 printf("Invalid SPI chip-select %s\n", s);
85 return NULL;
86 }
87
88 s = strsep(&devstr, ":");
89 if (s && *s) {
90 speed = simple_strtoul(s, &endp, 0);
91 if (*endp || !speed) {
92 printf("Invalid SPI speed %s\n", s);
93 return NULL;
94 }
95 }
96
97 s = strsep(&devstr, ":");
98 if (s && *s) {
99 mode = simple_strtoul(s, &endp, 0);
100 if (*endp || mode > 3) {
101 printf("Invalid SPI mode %s\n", s);
102 return NULL;
103 }
104 }
105
106 dev = spi_flash_probe(bus, cs, speed, mode);
107 if (!dev) {
108 printf("Failed to create SPI flash at %d:%d:%d:%d\n",
109 bus, cs, speed, mode);
110 return NULL;
111 }
112
113 return dev;
114}
115
116int dfu_fill_entity_sf(struct dfu_entity *dfu, char *devstr, char *s)
117{
118 char *st;
Vignesh R354e39e2015-10-20 15:22:00 +0530119 char *devstr_bkup = strdup(devstr);
Stephen Warren0280af72014-06-11 16:03:36 -0600120
Vignesh R354e39e2015-10-20 15:22:00 +0530121 dfu->data.sf.dev = parse_dev(devstr_bkup);
122 free(devstr_bkup);
Stephen Warren0280af72014-06-11 16:03:36 -0600123 if (!dfu->data.sf.dev)
124 return -ENODEV;
125
126 dfu->dev_type = DFU_DEV_SF;
127 dfu->max_buf_size = dfu->data.sf.dev->sector_size;
128
129 st = strsep(&s, " ");
130 if (!strcmp(st, "raw")) {
131 dfu->layout = DFU_RAW_ADDR;
132 dfu->data.sf.start = simple_strtoul(s, &s, 16);
133 s++;
134 dfu->data.sf.size = simple_strtoul(s, &s, 16);
135 } else {
136 printf("%s: Memory layout (%s) not supported!\n", __func__, st);
137 spi_flash_free(dfu->data.sf.dev);
138 return -1;
139 }
140
141 dfu->get_medium_size = dfu_get_medium_size_sf;
142 dfu->read_medium = dfu_read_medium_sf;
143 dfu->write_medium = dfu_write_medium_sf;
144 dfu->flush_medium = dfu_flush_medium_sf;
145 dfu->poll_timeout = dfu_polltimeout_sf;
146 dfu->free_entity = dfu_free_entity_sf;
147
148 /* initial state */
149 dfu->inited = 0;
150
151 return 0;
152}