blob: a23032cca58df0fdc2f304ce8aabaef253bd54fd [file] [log] [blame]
Reinhard Meyere0987e22010-10-05 16:56:40 +02001/*
2 * (C) Copyright 2010
3 * Reinhard Meyer, EMK Elektronik, reinhard.meyer@emk-elektronik.de
4 *
Wolfgang Denk1a459662013-07-08 09:37:19 +02005 * SPDX-License-Identifier: GPL-2.0+
Reinhard Meyere0987e22010-10-05 16:56:40 +02006 */
7
8/*
9 * Note: RAMTRON SPI FRAMs are ferroelectric, nonvolatile RAMs
10 * with an interface identical to SPI flash devices.
11 * However since they behave like RAM there are no delays or
12 * busy polls required. They can sustain read or write at the
13 * allowed SPI bus speed, which can be 40 MHz for some devices.
14 *
15 * Unfortunately some RAMTRON devices do not have a means of
16 * identifying them. They will leave the SO line undriven when
17 * the READ-ID command is issued. It is therefore mandatory
18 * that the MISO line has a proper pull-up, so that READ-ID
19 * will return a row of 0xff. This 0xff pseudo-id will cause
20 * probes by all vendor specific functions that are designed
21 * to handle it. If the MISO line is not pulled up, READ-ID
22 * could return any random noise, even mimicking another
23 * device.
24 *
25 * We use CONFIG_SPI_FRAM_RAMTRON_NON_JEDEC
26 * to define which device will be assumed after a simple status
27 * register verify. This method is prone to false positive
28 * detection and should therefore be the last to be tried.
29 * Enter it in the last position in the table in spi_flash.c!
30 *
31 * The define CONFIG_SPI_FRAM_RAMTRON_NON_JEDEC both activates
32 * compilation of the special handler and defines the device
33 * to assume.
34 */
35
36#include <common.h>
37#include <malloc.h>
Simon Glassff0960f2014-10-13 23:42:04 -060038#include <spi.h>
Reinhard Meyere0987e22010-10-05 16:56:40 +020039#include <spi_flash.h>
Jagannadha Sutradharudu Tekice22b922013-10-07 19:34:56 +053040#include "sf_internal.h"
Reinhard Meyere0987e22010-10-05 16:56:40 +020041
Reinhard Meyere0987e22010-10-05 16:56:40 +020042/*
43 * Properties of supported FRAMs
44 * Note: speed is currently not used because we have no method to deliver that
45 * value to the upper layers
46 */
47struct ramtron_spi_fram_params {
48 u32 size; /* size in bytes */
49 u8 addr_len; /* number of address bytes */
50 u8 merge_cmd; /* some address bits are in the command byte */
51 u8 id1; /* device ID 1 (family, density) */
52 u8 id2; /* device ID 2 (sub, rev, rsvd) */
53 u32 speed; /* max. SPI clock in Hz */
54 const char *name; /* name for display and/or matching */
55};
56
57struct ramtron_spi_fram {
58 struct spi_flash flash;
59 const struct ramtron_spi_fram_params *params;
60};
61
62static inline struct ramtron_spi_fram *to_ramtron_spi_fram(struct spi_flash
63 *flash)
64{
65 return container_of(flash, struct ramtron_spi_fram, flash);
66}
67
68/*
69 * table describing supported FRAM chips:
70 * chips without RDID command must have the values 0xff for id1 and id2
71 */
72static const struct ramtron_spi_fram_params ramtron_spi_fram_table[] = {
73 {
74 .size = 32*1024,
75 .addr_len = 2,
76 .merge_cmd = 0,
77 .id1 = 0x22,
78 .id2 = 0x00,
79 .speed = 40000000,
80 .name = "FM25V02",
81 },
82 {
83 .size = 32*1024,
84 .addr_len = 2,
85 .merge_cmd = 0,
86 .id1 = 0x22,
87 .id2 = 0x01,
88 .speed = 40000000,
89 .name = "FM25VN02",
90 },
91 {
92 .size = 64*1024,
93 .addr_len = 2,
94 .merge_cmd = 0,
95 .id1 = 0x23,
96 .id2 = 0x00,
97 .speed = 40000000,
98 .name = "FM25V05",
99 },
100 {
101 .size = 64*1024,
102 .addr_len = 2,
103 .merge_cmd = 0,
104 .id1 = 0x23,
105 .id2 = 0x01,
106 .speed = 40000000,
107 .name = "FM25VN05",
108 },
109 {
110 .size = 128*1024,
111 .addr_len = 3,
112 .merge_cmd = 0,
113 .id1 = 0x24,
114 .id2 = 0x00,
115 .speed = 40000000,
116 .name = "FM25V10",
117 },
118 {
119 .size = 128*1024,
120 .addr_len = 3,
121 .merge_cmd = 0,
122 .id1 = 0x24,
123 .id2 = 0x01,
124 .speed = 40000000,
125 .name = "FM25VN10",
126 },
127#ifdef CONFIG_SPI_FRAM_RAMTRON_NON_JEDEC
128 {
129 .size = 256*1024,
130 .addr_len = 3,
131 .merge_cmd = 0,
132 .id1 = 0xff,
133 .id2 = 0xff,
134 .speed = 40000000,
135 .name = "FM25H20",
136 },
137#endif
138};
139
140static int ramtron_common(struct spi_flash *flash,
141 u32 offset, size_t len, void *buf, u8 command)
142{
143 struct ramtron_spi_fram *sn = to_ramtron_spi_fram(flash);
144 u8 cmd[4];
145 int cmd_len;
146 int ret;
147
148 if (sn->params->addr_len == 3 && sn->params->merge_cmd == 0) {
149 cmd[0] = command;
150 cmd[1] = offset >> 16;
151 cmd[2] = offset >> 8;
152 cmd[3] = offset;
153 cmd_len = 4;
154 } else if (sn->params->addr_len == 2 && sn->params->merge_cmd == 0) {
155 cmd[0] = command;
156 cmd[1] = offset >> 8;
157 cmd[2] = offset;
158 cmd_len = 3;
159 } else {
160 printf("SF: unsupported addr_len or merge_cmd\n");
161 return -1;
162 }
163
164 /* claim the bus */
165 ret = spi_claim_bus(flash->spi);
166 if (ret) {
167 debug("SF: Unable to claim SPI bus\n");
168 return ret;
169 }
170
Mike Frysingerb4c87d62012-01-28 16:26:03 -0800171 if (command == CMD_PAGE_PROGRAM) {
Reinhard Meyere0987e22010-10-05 16:56:40 +0200172 /* send WREN */
Mike Frysinger2744a4e2011-04-23 23:05:55 +0000173 ret = spi_flash_cmd_write_enable(flash);
Reinhard Meyere0987e22010-10-05 16:56:40 +0200174 if (ret < 0) {
175 debug("SF: Enabling Write failed\n");
176 goto releasebus;
177 }
178 }
179
180 /* do the transaction */
Mike Frysingerb4c87d62012-01-28 16:26:03 -0800181 if (command == CMD_PAGE_PROGRAM)
Reinhard Meyere0987e22010-10-05 16:56:40 +0200182 ret = spi_flash_cmd_write(flash->spi, cmd, cmd_len, buf, len);
183 else
184 ret = spi_flash_cmd_read(flash->spi, cmd, cmd_len, buf, len);
185 if (ret < 0)
186 debug("SF: Transaction failed\n");
187
188releasebus:
189 /* release the bus */
190 spi_release_bus(flash->spi);
191 return ret;
192}
193
194static int ramtron_read(struct spi_flash *flash,
195 u32 offset, size_t len, void *buf)
196{
197 return ramtron_common(flash, offset, len, buf,
Mike Frysingerb4c87d62012-01-28 16:26:03 -0800198 CMD_READ_ARRAY_SLOW);
Reinhard Meyere0987e22010-10-05 16:56:40 +0200199}
200
201static int ramtron_write(struct spi_flash *flash,
202 u32 offset, size_t len, const void *buf)
203{
204 return ramtron_common(flash, offset, len, (void *)buf,
Mike Frysingerb4c87d62012-01-28 16:26:03 -0800205 CMD_PAGE_PROGRAM);
Reinhard Meyere0987e22010-10-05 16:56:40 +0200206}
207
Mike Frysingerf8f07572011-04-12 01:51:29 -0400208static int ramtron_erase(struct spi_flash *flash, u32 offset, size_t len)
Reinhard Meyere0987e22010-10-05 16:56:40 +0200209{
210 debug("SF: Erase of RAMTRON FRAMs is pointless\n");
211 return -1;
212}
213
214/*
215 * nore: we are called here with idcode pointing to the first non-0x7f byte
216 * already!
217 */
Jagannadha Sutradharudu Tekiaf1679b2013-09-24 16:03:45 +0530218static struct spi_flash *spi_fram_probe_ramtron(struct spi_slave *spi,
219 u8 *idcode)
Reinhard Meyere0987e22010-10-05 16:56:40 +0200220{
221 const struct ramtron_spi_fram_params *params;
222 struct ramtron_spi_fram *sn;
223 unsigned int i;
224#ifdef CONFIG_SPI_FRAM_RAMTRON_NON_JEDEC
225 int ret;
226 u8 sr;
227#endif
228
229 /* NOTE: the bus has been claimed before this function is called! */
230 switch (idcode[0]) {
231 case 0xc2:
232 /* JEDEC conformant RAMTRON id */
233 for (i = 0; i < ARRAY_SIZE(ramtron_spi_fram_table); i++) {
234 params = &ramtron_spi_fram_table[i];
Jagannadha Sutradharudu Teki5a9109f2013-05-25 22:03:39 +0530235 if (idcode[1] == params->id1 &&
236 idcode[2] == params->id2)
Reinhard Meyere0987e22010-10-05 16:56:40 +0200237 goto found;
238 }
239 break;
240#ifdef CONFIG_SPI_FRAM_RAMTRON_NON_JEDEC
241 case 0xff:
242 /*
243 * probably open MISO line, pulled up.
244 * We COULD have a non JEDEC conformant FRAM here,
245 * read the status register to verify
246 */
Mike Frysingerb4c87d62012-01-28 16:26:03 -0800247 ret = spi_flash_cmd(spi, CMD_READ_STATUS, &sr, 1);
Reinhard Meyere0987e22010-10-05 16:56:40 +0200248 if (ret)
249 return NULL;
250
251 /* Bits 5,4,0 are fixed 0 for all devices */
252 if ((sr & 0x31) != 0x00)
253 return NULL;
254 /* now find the device */
255 for (i = 0; i < ARRAY_SIZE(ramtron_spi_fram_table); i++) {
256 params = &ramtron_spi_fram_table[i];
Jagannadha Sutradharudu Teki5a9109f2013-05-25 22:03:39 +0530257 if (!strcmp(params->name,
258 CONFIG_SPI_FRAM_RAMTRON_NON_JEDEC))
Reinhard Meyere0987e22010-10-05 16:56:40 +0200259 goto found;
260 }
261 debug("SF: Unsupported non-JEDEC RAMTRON device "
262 CONFIG_SPI_FRAM_RAMTRON_NON_JEDEC "\n");
263 break;
264#endif
265 default:
266 break;
267 }
268
269 /* arriving here means no method has found a device we can handle */
270 debug("SF/ramtron: unsupported device id0=%02x id1=%02x id2=%02x\n",
Jagannadha Sutradharudu Teki5928b9a2013-07-29 22:43:57 +0530271 idcode[0], idcode[1], idcode[2]);
Reinhard Meyere0987e22010-10-05 16:56:40 +0200272 return NULL;
273
274found:
Jagannadha Sutradharudu Tekiaf1679b2013-09-24 16:03:45 +0530275 sn = malloc(sizeof(*sn));
Reinhard Meyere0987e22010-10-05 16:56:40 +0200276 if (!sn) {
277 debug("SF: Failed to allocate memory\n");
278 return NULL;
279 }
280
281 sn->params = params;
Reinhard Meyere0987e22010-10-05 16:56:40 +0200282
283 sn->flash.write = ramtron_write;
284 sn->flash.read = ramtron_read;
285 sn->flash.erase = ramtron_erase;
286 sn->flash.size = params->size;
287
Reinhard Meyere0987e22010-10-05 16:56:40 +0200288 return &sn->flash;
289}
Jagannadha Sutradharudu Tekiaf1679b2013-09-24 16:03:45 +0530290
291/*
292 * The following table holds all device probe functions
293 * (All flashes are removed and implemented a common probe at
294 * spi_flash_probe.c)
295 *
296 * shift: number of continuation bytes before the ID
297 * idcode: the expected IDCODE or 0xff for non JEDEC devices
298 * probe: the function to call
299 *
300 * Non JEDEC devices should be ordered in the table such that
301 * the probe functions with best detection algorithms come first.
302 *
303 * Several matching entries are permitted, they will be tried
304 * in sequence until a probe function returns non NULL.
305 *
306 * IDCODE_CONT_LEN may be redefined if a device needs to declare a
307 * larger "shift" value. IDCODE_PART_LEN generally shouldn't be
308 * changed. This is the max number of bytes probe functions may
309 * examine when looking up part-specific identification info.
310 *
311 * Probe functions will be given the idcode buffer starting at their
312 * manu id byte (the "idcode" in the table below). In other words,
313 * all of the continuation bytes will be skipped (the "shift" below).
314 */
315#define IDCODE_CONT_LEN 0
316#define IDCODE_PART_LEN 5
317static const struct {
318 const u8 shift;
319 const u8 idcode;
320 struct spi_flash *(*probe) (struct spi_slave *spi, u8 *idcode);
321} flashes[] = {
322 /* Keep it sorted by define name */
323#ifdef CONFIG_SPI_FRAM_RAMTRON
324 { 6, 0xc2, spi_fram_probe_ramtron, },
325# undef IDCODE_CONT_LEN
326# define IDCODE_CONT_LEN 6
327#endif
328#ifdef CONFIG_SPI_FRAM_RAMTRON_NON_JEDEC
329 { 0, 0xff, spi_fram_probe_ramtron, },
330#endif
331};
332#define IDCODE_LEN (IDCODE_CONT_LEN + IDCODE_PART_LEN)
333
334struct spi_flash *spi_flash_probe(unsigned int bus, unsigned int cs,
335 unsigned int max_hz, unsigned int spi_mode)
336{
337 struct spi_slave *spi;
338 struct spi_flash *flash = NULL;
339 int ret, i, shift;
340 u8 idcode[IDCODE_LEN], *idp;
341
342 spi = spi_setup_slave(bus, cs, max_hz, spi_mode);
343 if (!spi) {
344 printf("SF: Failed to set up slave\n");
345 return NULL;
346 }
347
348 ret = spi_claim_bus(spi);
349 if (ret) {
350 debug("SF: Failed to claim SPI bus: %d\n", ret);
351 goto err_claim_bus;
352 }
353
354 /* Read the ID codes */
355 ret = spi_flash_cmd(spi, CMD_READ_ID, idcode, sizeof(idcode));
356 if (ret)
357 goto err_read_id;
358
359#ifdef DEBUG
360 printf("SF: Got idcodes\n");
361 print_buffer(0, idcode, 1, sizeof(idcode), 0);
362#endif
363
364 /* count the number of continuation bytes */
365 for (shift = 0, idp = idcode;
366 shift < IDCODE_CONT_LEN && *idp == 0x7f;
367 ++shift, ++idp)
368 continue;
369
370 /* search the table for matches in shift and id */
371 for (i = 0; i < ARRAY_SIZE(flashes); ++i)
372 if (flashes[i].shift == shift && flashes[i].idcode == *idp) {
373 /* we have a match, call probe */
374 flash = flashes[i].probe(spi, idp);
375 if (flash)
376 break;
377 }
378
379 if (!flash) {
380 printf("SF: Unsupported manufacturer %02x\n", *idp);
381 goto err_manufacturer_probe;
382 }
383
Jagannadha Sutradharudu Tekiadbb5862013-10-07 17:21:20 +0530384 printf("SF: Detected %s with total size ", flash->name);
Jagannadha Sutradharudu Tekiaf1679b2013-09-24 16:03:45 +0530385 print_size(flash->size, "");
Jagannadha Sutradharudu Tekiaf1679b2013-09-24 16:03:45 +0530386 puts("\n");
387
388 spi_release_bus(spi);
389
390 return flash;
391
392err_manufacturer_probe:
393err_read_id:
394 spi_release_bus(spi);
395err_claim_bus:
396 spi_free_slave(spi);
397 return NULL;
398}
399
400void spi_flash_free(struct spi_flash *flash)
401{
402 spi_free_slave(flash->spi);
403 free(flash);
404}