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