blob: 7367e7ab223126669a36643574dd43257f97009e [file] [log] [blame]
Reinhard Meyer52cb0a72010-10-05 16:56:40 +02001/*
2 * (C) Copyright 2010
3 * Reinhard Meyer, EMK Elektronik, reinhard.meyer@emk-elektronik.de
4 *
Wolfgang Denkd79de1d2013-07-08 09:37:19 +02005 * SPDX-License-Identifier: GPL-2.0+
Reinhard Meyer52cb0a72010-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>
38#include <spi_flash.h>
Jagannadha Sutradharudu Tekif3b2dd82013-10-07 19:34:56 +053039#include "sf_internal.h"
Reinhard Meyer52cb0a72010-10-05 16:56:40 +020040
Reinhard Meyer52cb0a72010-10-05 16:56:40 +020041/*
42 * Properties of supported FRAMs
43 * Note: speed is currently not used because we have no method to deliver that
44 * value to the upper layers
45 */
46struct ramtron_spi_fram_params {
47 u32 size; /* size in bytes */
48 u8 addr_len; /* number of address bytes */
49 u8 merge_cmd; /* some address bits are in the command byte */
50 u8 id1; /* device ID 1 (family, density) */
51 u8 id2; /* device ID 2 (sub, rev, rsvd) */
52 u32 speed; /* max. SPI clock in Hz */
53 const char *name; /* name for display and/or matching */
54};
55
56struct ramtron_spi_fram {
57 struct spi_flash flash;
58 const struct ramtron_spi_fram_params *params;
59};
60
61static inline struct ramtron_spi_fram *to_ramtron_spi_fram(struct spi_flash
62 *flash)
63{
64 return container_of(flash, struct ramtron_spi_fram, flash);
65}
66
67/*
68 * table describing supported FRAM chips:
69 * chips without RDID command must have the values 0xff for id1 and id2
70 */
71static const struct ramtron_spi_fram_params ramtron_spi_fram_table[] = {
72 {
73 .size = 32*1024,
74 .addr_len = 2,
75 .merge_cmd = 0,
76 .id1 = 0x22,
77 .id2 = 0x00,
78 .speed = 40000000,
79 .name = "FM25V02",
80 },
81 {
82 .size = 32*1024,
83 .addr_len = 2,
84 .merge_cmd = 0,
85 .id1 = 0x22,
86 .id2 = 0x01,
87 .speed = 40000000,
88 .name = "FM25VN02",
89 },
90 {
91 .size = 64*1024,
92 .addr_len = 2,
93 .merge_cmd = 0,
94 .id1 = 0x23,
95 .id2 = 0x00,
96 .speed = 40000000,
97 .name = "FM25V05",
98 },
99 {
100 .size = 64*1024,
101 .addr_len = 2,
102 .merge_cmd = 0,
103 .id1 = 0x23,
104 .id2 = 0x01,
105 .speed = 40000000,
106 .name = "FM25VN05",
107 },
108 {
109 .size = 128*1024,
110 .addr_len = 3,
111 .merge_cmd = 0,
112 .id1 = 0x24,
113 .id2 = 0x00,
114 .speed = 40000000,
115 .name = "FM25V10",
116 },
117 {
118 .size = 128*1024,
119 .addr_len = 3,
120 .merge_cmd = 0,
121 .id1 = 0x24,
122 .id2 = 0x01,
123 .speed = 40000000,
124 .name = "FM25VN10",
125 },
126#ifdef CONFIG_SPI_FRAM_RAMTRON_NON_JEDEC
127 {
128 .size = 256*1024,
129 .addr_len = 3,
130 .merge_cmd = 0,
131 .id1 = 0xff,
132 .id2 = 0xff,
133 .speed = 40000000,
134 .name = "FM25H20",
135 },
136#endif
137};
138
139static int ramtron_common(struct spi_flash *flash,
140 u32 offset, size_t len, void *buf, u8 command)
141{
142 struct ramtron_spi_fram *sn = to_ramtron_spi_fram(flash);
143 u8 cmd[4];
144 int cmd_len;
145 int ret;
146
147 if (sn->params->addr_len == 3 && sn->params->merge_cmd == 0) {
148 cmd[0] = command;
149 cmd[1] = offset >> 16;
150 cmd[2] = offset >> 8;
151 cmd[3] = offset;
152 cmd_len = 4;
153 } else if (sn->params->addr_len == 2 && sn->params->merge_cmd == 0) {
154 cmd[0] = command;
155 cmd[1] = offset >> 8;
156 cmd[2] = offset;
157 cmd_len = 3;
158 } else {
159 printf("SF: unsupported addr_len or merge_cmd\n");
160 return -1;
161 }
162
163 /* claim the bus */
164 ret = spi_claim_bus(flash->spi);
165 if (ret) {
166 debug("SF: Unable to claim SPI bus\n");
167 return ret;
168 }
169
Mike Frysinger1302bec2012-01-28 16:26:03 -0800170 if (command == CMD_PAGE_PROGRAM) {
Reinhard Meyer52cb0a72010-10-05 16:56:40 +0200171 /* send WREN */
Mike Frysinger8ec7f4c2011-04-23 23:05:55 +0000172 ret = spi_flash_cmd_write_enable(flash);
Reinhard Meyer52cb0a72010-10-05 16:56:40 +0200173 if (ret < 0) {
174 debug("SF: Enabling Write failed\n");
175 goto releasebus;
176 }
177 }
178
179 /* do the transaction */
Mike Frysinger1302bec2012-01-28 16:26:03 -0800180 if (command == CMD_PAGE_PROGRAM)
Reinhard Meyer52cb0a72010-10-05 16:56:40 +0200181 ret = spi_flash_cmd_write(flash->spi, cmd, cmd_len, buf, len);
182 else
183 ret = spi_flash_cmd_read(flash->spi, cmd, cmd_len, buf, len);
184 if (ret < 0)
185 debug("SF: Transaction failed\n");
186
187releasebus:
188 /* release the bus */
189 spi_release_bus(flash->spi);
190 return ret;
191}
192
193static int ramtron_read(struct spi_flash *flash,
194 u32 offset, size_t len, void *buf)
195{
196 return ramtron_common(flash, offset, len, buf,
Mike Frysinger1302bec2012-01-28 16:26:03 -0800197 CMD_READ_ARRAY_SLOW);
Reinhard Meyer52cb0a72010-10-05 16:56:40 +0200198}
199
200static int ramtron_write(struct spi_flash *flash,
201 u32 offset, size_t len, const void *buf)
202{
203 return ramtron_common(flash, offset, len, (void *)buf,
Mike Frysinger1302bec2012-01-28 16:26:03 -0800204 CMD_PAGE_PROGRAM);
Reinhard Meyer52cb0a72010-10-05 16:56:40 +0200205}
206
Mike Frysingeraea4e172011-04-12 01:51:29 -0400207static int ramtron_erase(struct spi_flash *flash, u32 offset, size_t len)
Reinhard Meyer52cb0a72010-10-05 16:56:40 +0200208{
209 debug("SF: Erase of RAMTRON FRAMs is pointless\n");
210 return -1;
211}
212
213/*
214 * nore: we are called here with idcode pointing to the first non-0x7f byte
215 * already!
216 */
Jagannadha Sutradharudu Tekifcc9f342013-09-24 16:03:45 +0530217static struct spi_flash *spi_fram_probe_ramtron(struct spi_slave *spi,
218 u8 *idcode)
Reinhard Meyer52cb0a72010-10-05 16:56:40 +0200219{
220 const struct ramtron_spi_fram_params *params;
221 struct ramtron_spi_fram *sn;
222 unsigned int i;
223#ifdef CONFIG_SPI_FRAM_RAMTRON_NON_JEDEC
224 int ret;
225 u8 sr;
226#endif
227
228 /* NOTE: the bus has been claimed before this function is called! */
229 switch (idcode[0]) {
230 case 0xc2:
231 /* JEDEC conformant RAMTRON id */
232 for (i = 0; i < ARRAY_SIZE(ramtron_spi_fram_table); i++) {
233 params = &ramtron_spi_fram_table[i];
Jagannadha Sutradharudu Tekib9acb8e2013-05-25 22:03:39 +0530234 if (idcode[1] == params->id1 &&
235 idcode[2] == params->id2)
Reinhard Meyer52cb0a72010-10-05 16:56:40 +0200236 goto found;
237 }
238 break;
239#ifdef CONFIG_SPI_FRAM_RAMTRON_NON_JEDEC
240 case 0xff:
241 /*
242 * probably open MISO line, pulled up.
243 * We COULD have a non JEDEC conformant FRAM here,
244 * read the status register to verify
245 */
Mike Frysinger1302bec2012-01-28 16:26:03 -0800246 ret = spi_flash_cmd(spi, CMD_READ_STATUS, &sr, 1);
Reinhard Meyer52cb0a72010-10-05 16:56:40 +0200247 if (ret)
248 return NULL;
249
250 /* Bits 5,4,0 are fixed 0 for all devices */
251 if ((sr & 0x31) != 0x00)
252 return NULL;
253 /* now find the device */
254 for (i = 0; i < ARRAY_SIZE(ramtron_spi_fram_table); i++) {
255 params = &ramtron_spi_fram_table[i];
Jagannadha Sutradharudu Tekib9acb8e2013-05-25 22:03:39 +0530256 if (!strcmp(params->name,
257 CONFIG_SPI_FRAM_RAMTRON_NON_JEDEC))
Reinhard Meyer52cb0a72010-10-05 16:56:40 +0200258 goto found;
259 }
260 debug("SF: Unsupported non-JEDEC RAMTRON device "
261 CONFIG_SPI_FRAM_RAMTRON_NON_JEDEC "\n");
262 break;
263#endif
264 default:
265 break;
266 }
267
268 /* arriving here means no method has found a device we can handle */
269 debug("SF/ramtron: unsupported device id0=%02x id1=%02x id2=%02x\n",
Jagannadha Sutradharudu Tekidc221ac2013-07-29 22:43:57 +0530270 idcode[0], idcode[1], idcode[2]);
Reinhard Meyer52cb0a72010-10-05 16:56:40 +0200271 return NULL;
272
273found:
Jagannadha Sutradharudu Tekifcc9f342013-09-24 16:03:45 +0530274 sn = malloc(sizeof(*sn));
Reinhard Meyer52cb0a72010-10-05 16:56:40 +0200275 if (!sn) {
276 debug("SF: Failed to allocate memory\n");
277 return NULL;
278 }
279
280 sn->params = params;
Reinhard Meyer52cb0a72010-10-05 16:56:40 +0200281
282 sn->flash.write = ramtron_write;
283 sn->flash.read = ramtron_read;
284 sn->flash.erase = ramtron_erase;
285 sn->flash.size = params->size;
286
Reinhard Meyer52cb0a72010-10-05 16:56:40 +0200287 return &sn->flash;
288}
Jagannadha Sutradharudu Tekifcc9f342013-09-24 16:03:45 +0530289
290/*
291 * The following table holds all device probe functions
292 * (All flashes are removed and implemented a common probe at
293 * spi_flash_probe.c)
294 *
295 * shift: number of continuation bytes before the ID
296 * idcode: the expected IDCODE or 0xff for non JEDEC devices
297 * probe: the function to call
298 *
299 * Non JEDEC devices should be ordered in the table such that
300 * the probe functions with best detection algorithms come first.
301 *
302 * Several matching entries are permitted, they will be tried
303 * in sequence until a probe function returns non NULL.
304 *
305 * IDCODE_CONT_LEN may be redefined if a device needs to declare a
306 * larger "shift" value. IDCODE_PART_LEN generally shouldn't be
307 * changed. This is the max number of bytes probe functions may
308 * examine when looking up part-specific identification info.
309 *
310 * Probe functions will be given the idcode buffer starting at their
311 * manu id byte (the "idcode" in the table below). In other words,
312 * all of the continuation bytes will be skipped (the "shift" below).
313 */
314#define IDCODE_CONT_LEN 0
315#define IDCODE_PART_LEN 5
316static const struct {
317 const u8 shift;
318 const u8 idcode;
319 struct spi_flash *(*probe) (struct spi_slave *spi, u8 *idcode);
320} flashes[] = {
321 /* Keep it sorted by define name */
322#ifdef CONFIG_SPI_FRAM_RAMTRON
323 { 6, 0xc2, spi_fram_probe_ramtron, },
324# undef IDCODE_CONT_LEN
325# define IDCODE_CONT_LEN 6
326#endif
327#ifdef CONFIG_SPI_FRAM_RAMTRON_NON_JEDEC
328 { 0, 0xff, spi_fram_probe_ramtron, },
329#endif
330};
331#define IDCODE_LEN (IDCODE_CONT_LEN + IDCODE_PART_LEN)
332
333struct spi_flash *spi_flash_probe(unsigned int bus, unsigned int cs,
334 unsigned int max_hz, unsigned int spi_mode)
335{
336 struct spi_slave *spi;
337 struct spi_flash *flash = NULL;
338 int ret, i, shift;
339 u8 idcode[IDCODE_LEN], *idp;
340
341 spi = spi_setup_slave(bus, cs, max_hz, spi_mode);
342 if (!spi) {
343 printf("SF: Failed to set up slave\n");
344 return NULL;
345 }
346
347 ret = spi_claim_bus(spi);
348 if (ret) {
349 debug("SF: Failed to claim SPI bus: %d\n", ret);
350 goto err_claim_bus;
351 }
352
353 /* Read the ID codes */
354 ret = spi_flash_cmd(spi, CMD_READ_ID, idcode, sizeof(idcode));
355 if (ret)
356 goto err_read_id;
357
358#ifdef DEBUG
359 printf("SF: Got idcodes\n");
360 print_buffer(0, idcode, 1, sizeof(idcode), 0);
361#endif
362
363 /* count the number of continuation bytes */
364 for (shift = 0, idp = idcode;
365 shift < IDCODE_CONT_LEN && *idp == 0x7f;
366 ++shift, ++idp)
367 continue;
368
369 /* search the table for matches in shift and id */
370 for (i = 0; i < ARRAY_SIZE(flashes); ++i)
371 if (flashes[i].shift == shift && flashes[i].idcode == *idp) {
372 /* we have a match, call probe */
373 flash = flashes[i].probe(spi, idp);
374 if (flash)
375 break;
376 }
377
378 if (!flash) {
379 printf("SF: Unsupported manufacturer %02x\n", *idp);
380 goto err_manufacturer_probe;
381 }
382
383 printf("SF: Detected %s with page size ", flash->name);
384 print_size(flash->sector_size, ", total ");
385 print_size(flash->size, "");
Jagannadha Sutradharudu Tekifcc9f342013-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}