xref: /openbmc/u-boot/drivers/dfu/dfu_sf.c (revision 57efeb04)
1 // SPDX-License-Identifier: GPL-2.0+
2 /*
3  * Copyright (c) 2014, NVIDIA CORPORATION. All rights reserved.
4  */
5 
6 #include <common.h>
7 #include <malloc.h>
8 #include <errno.h>
9 #include <div64.h>
10 #include <dfu.h>
11 #include <spi.h>
12 #include <spi_flash.h>
13 
14 static int dfu_get_medium_size_sf(struct dfu_entity *dfu, u64 *size)
15 {
16 	*size = dfu->data.sf.size;
17 
18 	return 0;
19 }
20 
21 static int dfu_read_medium_sf(struct dfu_entity *dfu, u64 offset, void *buf,
22 		long *len)
23 {
24 	return spi_flash_read(dfu->data.sf.dev, dfu->data.sf.start + offset,
25 		*len, buf);
26 }
27 
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 
34 static int dfu_write_medium_sf(struct dfu_entity *dfu,
35 		u64 offset, void *buf, long *len)
36 {
37 	int ret;
38 
39 	ret = spi_flash_erase(dfu->data.sf.dev,
40 			      find_sector(dfu, dfu->data.sf.start, offset),
41 			      dfu->data.sf.dev->sector_size);
42 	if (ret)
43 		return ret;
44 
45 	ret = spi_flash_write(dfu->data.sf.dev, dfu->data.sf.start + offset,
46 			      *len, buf);
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;
120 	char *devstr_bkup = strdup(devstr);
121 
122 	dfu->data.sf.dev = parse_dev(devstr_bkup);
123 	free(devstr_bkup);
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 }
154