dm: sf: Make SST flash write op work again
[platform/kernel/u-boot.git] / drivers / mtd / spi / sf_probe.c
1 /*
2  * SPI flash probing
3  *
4  * Copyright (C) 2008 Atmel Corporation
5  * Copyright (C) 2010 Reinhard Meyer, EMK Elektronik
6  * Copyright (C) 2013 Jagannadha Sutradharudu Teki, Xilinx Inc.
7  *
8  * SPDX-License-Identifier:     GPL-2.0+
9  */
10
11 #include <common.h>
12 #include <dm.h>
13 #include <errno.h>
14 #include <fdtdec.h>
15 #include <malloc.h>
16 #include <mapmem.h>
17 #include <spi.h>
18 #include <spi_flash.h>
19 #include <asm/io.h>
20
21 #include "sf_internal.h"
22
23 DECLARE_GLOBAL_DATA_PTR;
24
25 /* Read commands array */
26 static u8 spi_read_cmds_array[] = {
27         CMD_READ_ARRAY_SLOW,
28         CMD_READ_ARRAY_FAST,
29         CMD_READ_DUAL_OUTPUT_FAST,
30         CMD_READ_DUAL_IO_FAST,
31         CMD_READ_QUAD_OUTPUT_FAST,
32         CMD_READ_QUAD_IO_FAST,
33 };
34
35 #ifdef CONFIG_SPI_FLASH_MACRONIX
36 static int spi_flash_set_qeb_mxic(struct spi_flash *flash)
37 {
38         u8 qeb_status;
39         int ret;
40
41         ret = spi_flash_cmd_read_status(flash, &qeb_status);
42         if (ret < 0)
43                 return ret;
44
45         if (qeb_status & STATUS_QEB_MXIC) {
46                 debug("SF: mxic: QEB is already set\n");
47         } else {
48                 ret = spi_flash_cmd_write_status(flash, STATUS_QEB_MXIC);
49                 if (ret < 0)
50                         return ret;
51         }
52
53         return ret;
54 }
55 #endif
56
57 #if defined(CONFIG_SPI_FLASH_SPANSION) || defined(CONFIG_SPI_FLASH_WINBOND)
58 static int spi_flash_set_qeb_winspan(struct spi_flash *flash)
59 {
60         u8 qeb_status;
61         int ret;
62
63         ret = spi_flash_cmd_read_config(flash, &qeb_status);
64         if (ret < 0)
65                 return ret;
66
67         if (qeb_status & STATUS_QEB_WINSPAN) {
68                 debug("SF: winspan: QEB is already set\n");
69         } else {
70                 ret = spi_flash_cmd_write_config(flash, STATUS_QEB_WINSPAN);
71                 if (ret < 0)
72                         return ret;
73         }
74
75         return ret;
76 }
77 #endif
78
79 static int spi_flash_set_qeb(struct spi_flash *flash, u8 idcode0)
80 {
81         switch (idcode0) {
82 #ifdef CONFIG_SPI_FLASH_MACRONIX
83         case SPI_FLASH_CFI_MFR_MACRONIX:
84                 return spi_flash_set_qeb_mxic(flash);
85 #endif
86 #if defined(CONFIG_SPI_FLASH_SPANSION) || defined(CONFIG_SPI_FLASH_WINBOND)
87         case SPI_FLASH_CFI_MFR_SPANSION:
88         case SPI_FLASH_CFI_MFR_WINBOND:
89                 return spi_flash_set_qeb_winspan(flash);
90 #endif
91 #ifdef CONFIG_SPI_FLASH_STMICRO
92         case SPI_FLASH_CFI_MFR_STMICRO:
93                 debug("SF: QEB is volatile for %02x flash\n", idcode0);
94                 return 0;
95 #endif
96         default:
97                 printf("SF: Need set QEB func for %02x flash\n", idcode0);
98                 return -1;
99         }
100 }
101
102 static int spi_flash_validate_params(struct spi_slave *spi, u8 *idcode,
103                                      struct spi_flash *flash)
104 {
105         const struct spi_flash_params *params;
106         u8 cmd;
107         u16 jedec = idcode[1] << 8 | idcode[2];
108         u16 ext_jedec = idcode[3] << 8 | idcode[4];
109
110         /* Validate params from spi_flash_params table */
111         params = spi_flash_params_table;
112         for (; params->name != NULL; params++) {
113                 if ((params->jedec >> 16) == idcode[0]) {
114                         if ((params->jedec & 0xFFFF) == jedec) {
115                                 if (params->ext_jedec == 0)
116                                         break;
117                                 else if (params->ext_jedec == ext_jedec)
118                                         break;
119                         }
120                 }
121         }
122
123         if (!params->name) {
124                 printf("SF: Unsupported flash IDs: ");
125                 printf("manuf %02x, jedec %04x, ext_jedec %04x\n",
126                        idcode[0], jedec, ext_jedec);
127                 return -EPROTONOSUPPORT;
128         }
129
130         /* Assign spi data */
131         flash->spi = spi;
132         flash->name = params->name;
133         flash->memory_map = spi->memory_map;
134         flash->dual_flash = flash->spi->option;
135 #ifdef CONFIG_DM_SPI_FLASH
136         flash->flags = params->flags;
137 #endif
138
139         /* Assign spi_flash ops */
140 #ifndef CONFIG_DM_SPI_FLASH
141         flash->write = spi_flash_cmd_write_ops;
142 #if defined(CONFIG_SPI_FLASH_SST)
143         if (params->flags & SST_WR) {
144                 if (flash->spi->op_mode_tx & SPI_OPM_TX_BP)
145                         flash->write = sst_write_bp;
146                 else
147                         flash->write = sst_write_wp;
148         }
149 #endif
150         flash->erase = spi_flash_cmd_erase_ops;
151         flash->read = spi_flash_cmd_read_ops;
152 #endif
153
154         /* Compute the flash size */
155         flash->shift = (flash->dual_flash & SF_DUAL_PARALLEL_FLASH) ? 1 : 0;
156         /*
157          * The Spansion S25FL032P and S25FL064P have 256b pages, yet use the
158          * 0x4d00 Extended JEDEC code. The rest of the Spansion flashes with
159          * the 0x4d00 Extended JEDEC code have 512b pages. All of the others
160          * have 256b pages.
161          */
162         if (ext_jedec == 0x4d00) {
163                 if ((jedec == 0x0215) || (jedec == 0x216))
164                         flash->page_size = 256;
165                 else
166                         flash->page_size = 512;
167         } else {
168                 flash->page_size = 256;
169         }
170         flash->page_size <<= flash->shift;
171         flash->sector_size = params->sector_size << flash->shift;
172         flash->size = flash->sector_size * params->nr_sectors << flash->shift;
173 #ifdef CONFIG_SF_DUAL_FLASH
174         if (flash->dual_flash & SF_DUAL_STACKED_FLASH)
175                 flash->size <<= 1;
176 #endif
177
178         /* Compute erase sector and command */
179         if (params->flags & SECT_4K) {
180                 flash->erase_cmd = CMD_ERASE_4K;
181                 flash->erase_size = 4096 << flash->shift;
182         } else if (params->flags & SECT_32K) {
183                 flash->erase_cmd = CMD_ERASE_32K;
184                 flash->erase_size = 32768 << flash->shift;
185         } else {
186                 flash->erase_cmd = CMD_ERASE_64K;
187                 flash->erase_size = flash->sector_size;
188         }
189
190         /* Look for the fastest read cmd */
191         cmd = fls(params->e_rd_cmd & flash->spi->op_mode_rx);
192         if (cmd) {
193                 cmd = spi_read_cmds_array[cmd - 1];
194                 flash->read_cmd = cmd;
195         } else {
196                 /* Go for default supported read cmd */
197                 flash->read_cmd = CMD_READ_ARRAY_FAST;
198         }
199
200         /* Not require to look for fastest only two write cmds yet */
201         if (params->flags & WR_QPP && flash->spi->op_mode_tx & SPI_OPM_TX_QPP)
202                 flash->write_cmd = CMD_QUAD_PAGE_PROGRAM;
203         else
204                 /* Go for default supported write cmd */
205                 flash->write_cmd = CMD_PAGE_PROGRAM;
206
207         /* Read dummy_byte: dummy byte is determined based on the
208          * dummy cycles of a particular command.
209          * Fast commands - dummy_byte = dummy_cycles/8
210          * I/O commands- dummy_byte = (dummy_cycles * no.of lines)/8
211          * For I/O commands except cmd[0] everything goes on no.of lines
212          * based on particular command but incase of fast commands except
213          * data all go on single line irrespective of command.
214          */
215         switch (flash->read_cmd) {
216         case CMD_READ_QUAD_IO_FAST:
217                 flash->dummy_byte = 2;
218                 break;
219         case CMD_READ_ARRAY_SLOW:
220                 flash->dummy_byte = 0;
221                 break;
222         default:
223                 flash->dummy_byte = 1;
224         }
225
226         /* Poll cmd selection */
227         flash->poll_cmd = CMD_READ_STATUS;
228 #ifdef CONFIG_SPI_FLASH_STMICRO
229         if (params->flags & E_FSR)
230                 flash->poll_cmd = CMD_FLAG_STATUS;
231 #endif
232
233         /* Configure the BAR - discover bank cmds and read current bank */
234 #ifdef CONFIG_SPI_FLASH_BAR
235         u8 curr_bank = 0;
236         if (flash->size > SPI_FLASH_16MB_BOUN) {
237                 int ret;
238
239                 flash->bank_read_cmd = (idcode[0] == 0x01) ?
240                                         CMD_BANKADDR_BRRD : CMD_EXTNADDR_RDEAR;
241                 flash->bank_write_cmd = (idcode[0] == 0x01) ?
242                                         CMD_BANKADDR_BRWR : CMD_EXTNADDR_WREAR;
243
244                 ret = spi_flash_read_common(flash, &flash->bank_read_cmd, 1,
245                                             &curr_bank, 1);
246                 if (ret) {
247                         debug("SF: fail to read bank addr register\n");
248                         return ret;
249                 }
250                 flash->bank_curr = curr_bank;
251         } else {
252                 flash->bank_curr = curr_bank;
253         }
254 #endif
255
256         /* Flash powers up read-only, so clear BP# bits */
257 #if defined(CONFIG_SPI_FLASH_ATMEL) || \
258         defined(CONFIG_SPI_FLASH_MACRONIX) || \
259         defined(CONFIG_SPI_FLASH_SST)
260                 spi_flash_cmd_write_status(flash, 0);
261 #endif
262
263         return 0;
264 }
265
266 #ifdef CONFIG_OF_CONTROL
267 int spi_flash_decode_fdt(const void *blob, struct spi_flash *flash)
268 {
269         fdt_addr_t addr;
270         fdt_size_t size;
271         int node;
272
273         /* If there is no node, do nothing */
274         node = fdtdec_next_compatible(blob, 0, COMPAT_GENERIC_SPI_FLASH);
275         if (node < 0)
276                 return 0;
277
278         addr = fdtdec_get_addr_size(blob, node, "memory-map", &size);
279         if (addr == FDT_ADDR_T_NONE) {
280                 debug("%s: Cannot decode address\n", __func__);
281                 return 0;
282         }
283
284         if (flash->size != size) {
285                 debug("%s: Memory map must cover entire device\n", __func__);
286                 return -1;
287         }
288         flash->memory_map = map_sysmem(addr, size);
289
290         return 0;
291 }
292 #endif /* CONFIG_OF_CONTROL */
293
294 /**
295  * spi_flash_probe_slave() - Probe for a SPI flash device on a bus
296  *
297  * @spi: Bus to probe
298  * @flashp: Pointer to place to put flash info, which may be NULL if the
299  * space should be allocated
300  */
301 int spi_flash_probe_slave(struct spi_slave *spi, struct spi_flash *flash)
302 {
303         u8 idcode[5];
304         int ret;
305
306         /* Setup spi_slave */
307         if (!spi) {
308                 printf("SF: Failed to set up slave\n");
309                 return -ENODEV;
310         }
311
312         /* Claim spi bus */
313         ret = spi_claim_bus(spi);
314         if (ret) {
315                 debug("SF: Failed to claim SPI bus: %d\n", ret);
316                 return ret;
317         }
318
319         /* Read the ID codes */
320         ret = spi_flash_cmd(spi, CMD_READ_ID, idcode, sizeof(idcode));
321         if (ret) {
322                 printf("SF: Failed to get idcodes\n");
323                 goto err_read_id;
324         }
325
326 #ifdef DEBUG
327         printf("SF: Got idcodes\n");
328         print_buffer(0, idcode, 1, sizeof(idcode), 0);
329 #endif
330
331         if (spi_flash_validate_params(spi, idcode, flash)) {
332                 ret = -EINVAL;
333                 goto err_read_id;
334         }
335
336         /* Set the quad enable bit - only for quad commands */
337         if ((flash->read_cmd == CMD_READ_QUAD_OUTPUT_FAST) ||
338             (flash->read_cmd == CMD_READ_QUAD_IO_FAST) ||
339             (flash->write_cmd == CMD_QUAD_PAGE_PROGRAM)) {
340                 if (spi_flash_set_qeb(flash, idcode[0])) {
341                         debug("SF: Fail to set QEB for %02x\n", idcode[0]);
342                         ret = -EINVAL;
343                         goto err_read_id;
344                 }
345         }
346
347 #ifdef CONFIG_OF_CONTROL
348         if (spi_flash_decode_fdt(gd->fdt_blob, flash)) {
349                 debug("SF: FDT decode error\n");
350                 ret = -EINVAL;
351                 goto err_read_id;
352         }
353 #endif
354 #ifndef CONFIG_SPL_BUILD
355         printf("SF: Detected %s with page size ", flash->name);
356         print_size(flash->page_size, ", erase size ");
357         print_size(flash->erase_size, ", total ");
358         print_size(flash->size, "");
359         if (flash->memory_map)
360                 printf(", mapped at %p", flash->memory_map);
361         puts("\n");
362 #endif
363 #ifndef CONFIG_SPI_FLASH_BAR
364         if (((flash->dual_flash == SF_SINGLE_FLASH) &&
365              (flash->size > SPI_FLASH_16MB_BOUN)) ||
366              ((flash->dual_flash > SF_SINGLE_FLASH) &&
367              (flash->size > SPI_FLASH_16MB_BOUN << 1))) {
368                 puts("SF: Warning - Only lower 16MiB accessible,");
369                 puts(" Full access #define CONFIG_SPI_FLASH_BAR\n");
370         }
371 #endif
372
373         /* Release spi bus */
374         spi_release_bus(spi);
375
376         return 0;
377
378 err_read_id:
379         spi_release_bus(spi);
380         return ret;
381 }
382
383 #ifndef CONFIG_DM_SPI_FLASH
384 struct spi_flash *spi_flash_probe_tail(struct spi_slave *bus)
385 {
386         struct spi_flash *flash;
387
388         /* Allocate space if needed (not used by sf-uclass */
389         flash = calloc(1, sizeof(*flash));
390         if (!flash) {
391                 debug("SF: Failed to allocate spi_flash\n");
392                 return NULL;
393         }
394
395         if (spi_flash_probe_slave(bus, flash)) {
396                 spi_free_slave(bus);
397                 free(flash);
398                 return NULL;
399         }
400
401         return flash;
402 }
403
404 struct spi_flash *spi_flash_probe(unsigned int busnum, unsigned int cs,
405                 unsigned int max_hz, unsigned int spi_mode)
406 {
407         struct spi_slave *bus;
408
409         bus = spi_setup_slave(busnum, cs, max_hz, spi_mode);
410         if (!bus)
411                 return NULL;
412         return spi_flash_probe_tail(bus);
413 }
414
415 #ifdef CONFIG_OF_SPI_FLASH
416 struct spi_flash *spi_flash_probe_fdt(const void *blob, int slave_node,
417                                       int spi_node)
418 {
419         struct spi_slave *bus;
420
421         bus = spi_setup_slave_fdt(blob, slave_node, spi_node);
422         if (!bus)
423                 return NULL;
424         return spi_flash_probe_tail(bus);
425 }
426 #endif
427
428 void spi_flash_free(struct spi_flash *flash)
429 {
430         spi_free_slave(flash->spi);
431         free(flash);
432 }
433
434 #else /* defined CONFIG_DM_SPI_FLASH */
435
436 static int spi_flash_std_read(struct udevice *dev, u32 offset, size_t len,
437                               void *buf)
438 {
439         struct spi_flash *flash = dev_get_uclass_priv(dev);
440
441         return spi_flash_cmd_read_ops(flash, offset, len, buf);
442 }
443
444 int spi_flash_std_write(struct udevice *dev, u32 offset, size_t len,
445                         const void *buf)
446 {
447         struct spi_flash *flash = dev_get_uclass_priv(dev);
448
449 #if defined(CONFIG_SPI_FLASH_SST)
450         if (flash->flags & SST_WR) {
451                 if (flash->spi->op_mode_tx & SPI_OPM_TX_BP)
452                         return sst_write_bp(flash, offset, len, buf);
453                 else
454                         return sst_write_wp(flash, offset, len, buf);
455         }
456 #endif
457
458         return spi_flash_cmd_write_ops(flash, offset, len, buf);
459 }
460
461 int spi_flash_std_erase(struct udevice *dev, u32 offset, size_t len)
462 {
463         struct spi_flash *flash = dev_get_uclass_priv(dev);
464
465         return spi_flash_cmd_erase_ops(flash, offset, len);
466 }
467
468 int spi_flash_std_probe(struct udevice *dev)
469 {
470         struct spi_slave *slave = dev_get_parentdata(dev);
471         struct dm_spi_slave_platdata *plat = dev_get_parent_platdata(dev);
472         struct spi_flash *flash;
473
474         flash = dev_get_uclass_priv(dev);
475         flash->dev = dev;
476         debug("%s: slave=%p, cs=%d\n", __func__, slave, plat->cs);
477         return spi_flash_probe_slave(slave, flash);
478 }
479
480 static const struct dm_spi_flash_ops spi_flash_std_ops = {
481         .read = spi_flash_std_read,
482         .write = spi_flash_std_write,
483         .erase = spi_flash_std_erase,
484 };
485
486 static const struct udevice_id spi_flash_std_ids[] = {
487         { .compatible = "spi-flash" },
488         { }
489 };
490
491 U_BOOT_DRIVER(spi_flash_std) = {
492         .name           = "spi_flash_std",
493         .id             = UCLASS_SPI_FLASH,
494         .of_match       = spi_flash_std_ids,
495         .probe          = spi_flash_std_probe,
496         .priv_auto_alloc_size = sizeof(struct spi_flash),
497         .ops            = &spi_flash_std_ops,
498 };
499
500 #endif /* CONFIG_DM_SPI_FLASH */