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