blob: 066e7670d1b939c55e4bff6af16a082ceeaf753e [file] [log] [blame]
Tom Rini83d290c2018-05-06 17:58:06 -04001// SPDX-License-Identifier: GPL-2.0+
Stephen Warren6f12ebf2014-06-11 16:03:36 -06002/*
3 * Copyright (c) 2014, NVIDIA CORPORATION. All rights reserved.
Stephen Warren6f12ebf2014-06-11 16:03:36 -06004 */
5
6#include <common.h>
7#include <malloc.h>
8#include <errno.h>
9#include <div64.h>
10#include <dfu.h>
Simon Glass843c9e82014-10-13 23:41:55 -060011#include <spi.h>
Stephen Warren6f12ebf2014-06-11 16:03:36 -060012#include <spi_flash.h>
13
Patrick Delaunay15970d82017-07-19 16:39:23 +020014static int dfu_get_medium_size_sf(struct dfu_entity *dfu, u64 *size)
Stephen Warren6f12ebf2014-06-11 16:03:36 -060015{
Patrick Delaunay4de51202017-07-19 16:39:22 +020016 *size = dfu->data.sf.size;
17
18 return 0;
Stephen Warren6f12ebf2014-06-11 16:03:36 -060019}
20
21static int dfu_read_medium_sf(struct dfu_entity *dfu, u64 offset, void *buf,
22 long *len)
23{
Phil Edworthye5f00f02017-05-24 15:09:49 +010024 return spi_flash_read(dfu->data.sf.dev, dfu->data.sf.start + offset,
25 *len, buf);
Stephen Warren6f12ebf2014-06-11 16:03:36 -060026}
27
Fabio Estevam2727f3b2015-09-23 00:50:39 -030028static 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 Warren6f12ebf2014-06-11 16:03:36 -060034static int dfu_write_medium_sf(struct dfu_entity *dfu,
35 u64 offset, void *buf, long *len)
36{
37 int ret;
38
Fabio Estevam2727f3b2015-09-23 00:50:39 -030039 ret = spi_flash_erase(dfu->data.sf.dev,
40 find_sector(dfu, dfu->data.sf.start, offset),
Fabio Estevamf4c92582015-09-22 00:55:00 -030041 dfu->data.sf.dev->sector_size);
Stephen Warren6f12ebf2014-06-11 16:03:36 -060042 if (ret)
43 return ret;
44
Fabio Estevam2727f3b2015-09-23 00:50:39 -030045 ret = spi_flash_write(dfu->data.sf.dev, dfu->data.sf.start + offset,
46 *len, buf);
Stephen Warren6f12ebf2014-06-11 16:03:36 -060047 if (ret)
48 return ret;
49
50 return 0;
51}
52
53static int dfu_flush_medium_sf(struct dfu_entity *dfu)
54{
55 return 0;
56}
57
58static unsigned int dfu_polltimeout_sf(struct dfu_entity *dfu)
59{
60 return DFU_DEFAULT_POLL_TIMEOUT;
61}
62
63static void dfu_free_entity_sf(struct dfu_entity *dfu)
64{
65 spi_flash_free(dfu->data.sf.dev);
66}
67
68static 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
117int dfu_fill_entity_sf(struct dfu_entity *dfu, char *devstr, char *s)
118{
119 char *st;
Vignesh R30e3ea42015-10-20 15:22:00 +0530120 char *devstr_bkup = strdup(devstr);
Stephen Warren6f12ebf2014-06-11 16:03:36 -0600121
Vignesh R30e3ea42015-10-20 15:22:00 +0530122 dfu->data.sf.dev = parse_dev(devstr_bkup);
123 free(devstr_bkup);
Stephen Warren6f12ebf2014-06-11 16:03:36 -0600124 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}