blob: 4cccc71197f7c6fbcada4cc55911b97aa2d32be7 [file] [log] [blame]
Idwer Vollering73a10182014-02-16 00:32:13 +00001/*
2 * Copyright (C) 2014 Idwer Vollering <vidwer@gmail.com>
3 *
4 * Based on winbond.c
5 *
6 * Copyright 2008, Network Appliance Inc.
7 * Author: Jason McMullan <mcmullan <at> netapp.com>
8 * Licensed under the GPL-2 or later.
9 */
10
11#include <stdlib.h>
12#include <spi_flash.h>
13#include "spi_flash_internal.h"
14
15/* A25L-specific commands */
16#define CMD_A25_WREN 0x06 /* Write Enable */
17#define CMD_A25_WRDI 0x04 /* Write Disable */
18#define CMD_A25_RDSR 0x05 /* Read Status Register */
19#define CMD_A25_WRSR 0x01 /* Write Status Register */
20#define CMD_A25_READ 0x03 /* Read Data Bytes */
21#define CMD_A25_FAST_READ 0x0b /* Read Data Bytes at Higher Speed */
22#define CMD_A25_PP 0x02 /* Page Program */
23#define CMD_A25_SE 0x20 /* Sector (4K) Erase */
24#define CMD_A25_BE 0xd8 /* Block (64K) Erase */
25#define CMD_A25_CE 0xc7 /* Chip Erase */
26#define CMD_A25_DP 0xb9 /* Deep Power-down */
27#define CMD_A25_RES 0xab /* Release from DP, and Read Signature */
28
29struct amic_spi_flash_params {
30 uint16_t id;
31 /* Log2 of page size in power-of-two mode */
32 uint8_t l2_page_size;
33 uint16_t pages_per_sector;
34 uint16_t sectors_per_block;
35 uint16_t nr_blocks;
36 const char *name;
37};
38
39/* spi_flash needs to be first so upper layers can free() it */
40struct amic_spi_flash {
41 struct spi_flash flash;
42 const struct amic_spi_flash_params *params;
43};
44
45static inline struct amic_spi_flash *
46to_amic_spi_flash(struct spi_flash *flash)
47{
48 return container_of(flash, struct amic_spi_flash, flash);
49}
50
51static const struct amic_spi_flash_params amic_spi_flash_table[] = {
52 {
53 .id = 0x3016,
54 .l2_page_size = 8,
55 .pages_per_sector = 16,
56 .sectors_per_block = 16,
57 .nr_blocks = 64,
58 .name = "A25L032",
59 },
60};
61
62static int amic_write(struct spi_flash *flash,
63 u32 offset, size_t len, const void *buf)
64{
65 struct amic_spi_flash *amic = to_amic_spi_flash(flash);
66 unsigned long byte_addr;
67 unsigned long page_size;
68 size_t chunk_len;
69 size_t actual;
70 int ret;
71 u8 cmd[4];
72
Kyösti Mälkki77d12802014-06-29 16:15:39 +030073 page_size = 1 << amic->params->l2_page_size;
Idwer Vollering73a10182014-02-16 00:32:13 +000074 byte_addr = offset % page_size;
75
76 flash->spi->rw = SPI_WRITE_FLAG;
77 ret = spi_claim_bus(flash->spi);
78 if (ret) {
79 printk(BIOS_WARNING, "SF: Unable to claim SPI bus\n");
80 return ret;
81 }
82
83 for (actual = 0; actual < len; actual += chunk_len) {
84 chunk_len = min(len - actual, page_size - byte_addr);
Kyösti Mälkki77d12802014-06-29 16:15:39 +030085 chunk_len = min(chunk_len, CONTROLLER_PAGE_LIMIT);
Idwer Vollering73a10182014-02-16 00:32:13 +000086
87 cmd[0] = CMD_A25_PP;
88 cmd[1] = (offset >> 16) & 0xff;
89 cmd[2] = (offset >> 8) & 0xff;
90 cmd[3] = offset & 0xff;
91#if CONFIG_DEBUG_SPI_FLASH
92 printk(BIOS_SPEW, "PP: 0x%p => cmd = { 0x%02x 0x%02x%02x%02x }"
93 " chunk_len = %zu\n", buf + actual,
94 cmd[0], cmd[1], cmd[2], cmd[3], chunk_len);
95#endif
96
97 ret = spi_flash_cmd(flash->spi, CMD_A25_WREN, NULL, 0);
98 if (ret < 0) {
99 printk(BIOS_WARNING, "SF: Enabling Write failed\n");
100 goto out;
101 }
102
103 ret = spi_flash_cmd_write(flash->spi, cmd, 4,
104 buf + actual, chunk_len);
105 if (ret < 0) {
106 printk(BIOS_WARNING, "SF: AMIC Page Program failed\n");
107 goto out;
108 }
109
110 ret = spi_flash_cmd_wait_ready(flash, SPI_FLASH_PROG_TIMEOUT);
111 if (ret)
112 goto out;
113
114 offset += chunk_len;
115 byte_addr = 0;
116 }
117
118#if CONFIG_DEBUG_SPI_FLASH
119 printk(BIOS_SPEW, "SF: AMIC: Successfully programmed %zu bytes @"
120 " 0x%lx\n", len, (unsigned long)(offset - len));
121#endif
122 ret = 0;
123
124out:
125 spi_release_bus(flash->spi);
126 return ret;
127}
128
129static int amic_erase(struct spi_flash *flash, u32 offset, size_t len)
130{
131 return spi_flash_cmd_erase(flash, CMD_A25_SE, offset, len);
132}
133
134struct spi_flash *spi_flash_probe_amic(struct spi_slave *spi, u8 *idcode)
135{
136 const struct amic_spi_flash_params *params;
137 unsigned page_size;
138 struct amic_spi_flash *amic;
139 unsigned int i;
140
141 for (i = 0; i < ARRAY_SIZE(amic_spi_flash_table); i++) {
142 params = &amic_spi_flash_table[i];
143 if (params->id == ((idcode[1] << 8) | idcode[2]))
144 break;
145 }
146
147 if (i == ARRAY_SIZE(amic_spi_flash_table)) {
148 printk(BIOS_WARNING, "SF: Unsupported AMIC ID %02x%02x\n",
149 idcode[1], idcode[2]);
150 return NULL;
151 }
152
153 amic = malloc(sizeof(struct amic_spi_flash));
154 if (!amic) {
155 printk(BIOS_WARNING, "SF: Failed to allocate memory\n");
156 return NULL;
157 }
158
159 amic->params = params;
160 amic->flash.spi = spi;
161 amic->flash.name = params->name;
162
163 /* Assuming power-of-two page size initially. */
164 page_size = 1 << params->l2_page_size;
165
166 amic->flash.write = amic_write;
167 amic->flash.erase = amic_erase;
168#if CONFIG_SPI_FLASH_NO_FAST_READ
169 amic->flash.read = spi_flash_cmd_read_slow;
170#else
171 amic->flash.read = spi_flash_cmd_read_fast;
172#endif
173 amic->flash.sector_size = (1 << amic->params->l2_page_size) *
174 amic->params->pages_per_sector;
175 amic->flash.size = page_size * params->pages_per_sector
176 * params->sectors_per_block
177 * params->nr_blocks;
178
179 return &amic->flash;
180}