blob: 85567eb0d643cbc4d487dbbe39a2f8d0c1d1e180 [file] [log] [blame]
Stefan Reinauer1c56d9b2012-05-10 11:27:32 -07001/*
2 * (C) Copyright 2010, ucRobotics Inc.
3 * Author: Chong Huang <chuang@ucrobotics.com>
4 * Licensed under the GPL-2 or later.
5 */
6
7#include <stdlib.h>
8#include <spi_flash.h>
Edward O'Callaghanc4561e22014-06-26 15:02:40 +10009
Stefan Reinauer1c56d9b2012-05-10 11:27:32 -070010#include "spi_flash_internal.h"
11
12/* EN25Q128-specific commands */
13#define CMD_EN25Q128_WREN 0x06 /* Write Enable */
14#define CMD_EN25Q128_WRDI 0x04 /* Write Disable */
15#define CMD_EN25Q128_RDSR 0x05 /* Read Status Register */
16#define CMD_EN25Q128_WRSR 0x01 /* Write Status Register */
17#define CMD_EN25Q128_READ 0x03 /* Read Data Bytes */
18#define CMD_EN25Q128_FAST_READ 0x0b /* Read Data Bytes at Higher Speed */
19#define CMD_EN25Q128_PP 0x02 /* Page Program */
20#define CMD_EN25Q128_SE 0x20 /* Sector Erase */
21#define CMD_EN25Q128_BE 0xd8 /* Block Erase */
22#define CMD_EN25Q128_DP 0xb9 /* Deep Power-down */
23#define CMD_EN25Q128_RES 0xab /* Release from DP, and Read Signature */
24
Vladimir Serbinenko066dcec2014-01-05 06:46:19 +010025#define EON_ID_EN25Q64 0x17
Stefan Reinauer1c56d9b2012-05-10 11:27:32 -070026#define EON_ID_EN25Q128 0x18
27
28struct eon_spi_flash_params {
29 u8 idcode1;
30 u16 page_size;
31 u16 pages_per_sector;
32 u16 sectors_per_block;
33 u16 nr_sectors;
34 const char *name;
35};
36
37/* spi_flash needs to be first so upper layers can free() it */
38struct eon_spi_flash {
39 struct spi_flash flash;
40 const struct eon_spi_flash_params *params;
41};
42
43static inline struct eon_spi_flash *to_eon_spi_flash(struct spi_flash *flash)
44{
45 return container_of(flash, struct eon_spi_flash, flash);
46}
47
48static const struct eon_spi_flash_params eon_spi_flash_table[] = {
49 {
50 .idcode1 = EON_ID_EN25Q128,
51 .page_size = 256,
52 .pages_per_sector = 16,
53 .sectors_per_block = 16,
54 .nr_sectors = 4096,
55 .name = "EN25Q128",
56 },
Vladimir Serbinenko066dcec2014-01-05 06:46:19 +010057 {
58 .idcode1 = EON_ID_EN25Q64,
59 .page_size = 256,
60 .pages_per_sector = 16,
61 .sectors_per_block = 16,
62 .nr_sectors = 2048,
63 .name = "EN25Q64",
64 },
Stefan Reinauer1c56d9b2012-05-10 11:27:32 -070065};
66
67static int eon_write(struct spi_flash *flash,
68 u32 offset, size_t len, const void *buf)
69{
70 struct eon_spi_flash *eon = to_eon_spi_flash(flash);
71 unsigned long page_addr;
72 unsigned long byte_addr;
73 unsigned long page_size;
74 size_t chunk_len;
75 size_t actual;
76 int ret;
77 u8 cmd[4];
78
79 page_size = eon->params->page_size;
80 page_addr = offset / page_size;
81 byte_addr = offset % page_size;
82
Martin Roth3316cf22012-12-05 16:22:54 -070083 flash->spi->rw = SPI_WRITE_FLAG;
Stefan Reinauer1c56d9b2012-05-10 11:27:32 -070084 ret = spi_claim_bus(flash->spi);
85 if (ret) {
86 printk(BIOS_WARNING, "SF: Unable to claim SPI bus\n");
87 return ret;
88 }
89
90 ret = 0;
91 for (actual = 0; actual < len; actual += chunk_len) {
92 chunk_len = min(len - actual, page_size - byte_addr);
93
94 cmd[0] = CMD_EN25Q128_PP;
95 cmd[1] = page_addr >> 8;
96 cmd[2] = page_addr;
97 cmd[3] = byte_addr;
98
Stefan Reinauer5649b082012-05-23 11:03:29 -070099#if CONFIG_DEBUG_SPI_FLASH
Stefan Reinauer1c56d9b2012-05-10 11:27:32 -0700100 printk(BIOS_SPEW,
Stefan Reinauer8ea5a342012-05-14 13:52:32 -0700101 "PP: 0x%p => cmd = { 0x%02x 0x%02x%02x%02x } chunk_len = %zu\n",
Stefan Reinauer1c56d9b2012-05-10 11:27:32 -0700102 buf + actual, cmd[0], cmd[1], cmd[2], cmd[3], chunk_len);
Stefan Reinauer5649b082012-05-23 11:03:29 -0700103#endif
Stefan Reinauer1c56d9b2012-05-10 11:27:32 -0700104
105 ret = spi_flash_cmd(flash->spi, CMD_EN25Q128_WREN, NULL, 0);
106 if (ret < 0) {
107 printk(BIOS_WARNING, "SF: Enabling Write failed\n");
108 break;
109 }
110
111 ret = spi_flash_cmd_write(flash->spi, cmd, 4,
112 buf + actual, chunk_len);
113 if (ret < 0) {
114 printk(BIOS_WARNING, "SF: EON Page Program failed\n");
115 break;
116 }
117
118 ret = spi_flash_cmd_wait_ready(flash, SPI_FLASH_PROG_TIMEOUT);
119 if (ret)
120 break;
121
122 page_addr++;
123 byte_addr = 0;
124 }
125
Marc Jones747127d2012-12-03 22:16:29 -0700126#if CONFIG_DEBUG_SPI_FLASH
127 printk(BIOS_SPEW, "SF: EON: Successfully programmed %zu bytes @ 0x%x\n",
Stefan Reinauer1c56d9b2012-05-10 11:27:32 -0700128 len, offset);
Marc Jones747127d2012-12-03 22:16:29 -0700129#endif
Stefan Reinauer1c56d9b2012-05-10 11:27:32 -0700130
131 spi_release_bus(flash->spi);
132 return ret;
133}
134
135static int eon_erase(struct spi_flash *flash, u32 offset, size_t len)
136{
137 return spi_flash_cmd_erase(flash, CMD_EN25Q128_BE, offset, len);
138}
139
140struct spi_flash *spi_flash_probe_eon(struct spi_slave *spi, u8 *idcode)
141{
142 const struct eon_spi_flash_params *params;
143 struct eon_spi_flash *eon;
144 unsigned int i;
145
146 for (i = 0; i < ARRAY_SIZE(eon_spi_flash_table); ++i) {
147 params = &eon_spi_flash_table[i];
148 if (params->idcode1 == idcode[2])
149 break;
150 }
151
152 if (i == ARRAY_SIZE(eon_spi_flash_table)) {
153 printk(BIOS_WARNING, "SF: Unsupported EON ID %02x\n", idcode[1]);
154 return NULL;
155 }
156
157 eon = malloc(sizeof(*eon));
158 if (!eon) {
159 printk(BIOS_WARNING, "SF: Failed to allocate memory\n");
160 return NULL;
161 }
162
163 eon->params = params;
164 eon->flash.spi = spi;
165 eon->flash.name = params->name;
166
167 eon->flash.write = eon_write;
168 eon->flash.erase = eon_erase;
169 eon->flash.read = spi_flash_cmd_read_fast;
170 eon->flash.sector_size = params->page_size * params->pages_per_sector
171 * params->sectors_per_block;
172 eon->flash.size = params->page_size * params->pages_per_sector
173 * params->nr_sectors;
174
175 return &eon->flash;
176}