blob: fa17036e85377d089ae86aec3a4136a8383061fa [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
73 page_size = min(1 << amic->params->l2_page_size, CONTROLLER_PAGE_LIMIT);
74 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);
85
86 cmd[0] = CMD_A25_PP;
87 cmd[1] = (offset >> 16) & 0xff;
88 cmd[2] = (offset >> 8) & 0xff;
89 cmd[3] = offset & 0xff;
90#if CONFIG_DEBUG_SPI_FLASH
91 printk(BIOS_SPEW, "PP: 0x%p => cmd = { 0x%02x 0x%02x%02x%02x }"
92 " chunk_len = %zu\n", buf + actual,
93 cmd[0], cmd[1], cmd[2], cmd[3], chunk_len);
94#endif
95
96 ret = spi_flash_cmd(flash->spi, CMD_A25_WREN, NULL, 0);
97 if (ret < 0) {
98 printk(BIOS_WARNING, "SF: Enabling Write failed\n");
99 goto out;
100 }
101
102 ret = spi_flash_cmd_write(flash->spi, cmd, 4,
103 buf + actual, chunk_len);
104 if (ret < 0) {
105 printk(BIOS_WARNING, "SF: AMIC Page Program failed\n");
106 goto out;
107 }
108
109 ret = spi_flash_cmd_wait_ready(flash, SPI_FLASH_PROG_TIMEOUT);
110 if (ret)
111 goto out;
112
113 offset += chunk_len;
114 byte_addr = 0;
115 }
116
117#if CONFIG_DEBUG_SPI_FLASH
118 printk(BIOS_SPEW, "SF: AMIC: Successfully programmed %zu bytes @"
119 " 0x%lx\n", len, (unsigned long)(offset - len));
120#endif
121 ret = 0;
122
123out:
124 spi_release_bus(flash->spi);
125 return ret;
126}
127
128static int amic_erase(struct spi_flash *flash, u32 offset, size_t len)
129{
130 return spi_flash_cmd_erase(flash, CMD_A25_SE, offset, len);
131}
132
133struct spi_flash *spi_flash_probe_amic(struct spi_slave *spi, u8 *idcode)
134{
135 const struct amic_spi_flash_params *params;
136 unsigned page_size;
137 struct amic_spi_flash *amic;
138 unsigned int i;
139
140 for (i = 0; i < ARRAY_SIZE(amic_spi_flash_table); i++) {
141 params = &amic_spi_flash_table[i];
142 if (params->id == ((idcode[1] << 8) | idcode[2]))
143 break;
144 }
145
146 if (i == ARRAY_SIZE(amic_spi_flash_table)) {
147 printk(BIOS_WARNING, "SF: Unsupported AMIC ID %02x%02x\n",
148 idcode[1], idcode[2]);
149 return NULL;
150 }
151
152 amic = malloc(sizeof(struct amic_spi_flash));
153 if (!amic) {
154 printk(BIOS_WARNING, "SF: Failed to allocate memory\n");
155 return NULL;
156 }
157
158 amic->params = params;
159 amic->flash.spi = spi;
160 amic->flash.name = params->name;
161
162 /* Assuming power-of-two page size initially. */
163 page_size = 1 << params->l2_page_size;
164
165 amic->flash.write = amic_write;
166 amic->flash.erase = amic_erase;
167#if CONFIG_SPI_FLASH_NO_FAST_READ
168 amic->flash.read = spi_flash_cmd_read_slow;
169#else
170 amic->flash.read = spi_flash_cmd_read_fast;
171#endif
172 amic->flash.sector_size = (1 << amic->params->l2_page_size) *
173 amic->params->pages_per_sector;
174 amic->flash.size = page_size * params->pages_per_sector
175 * params->sectors_per_block
176 * params->nr_blocks;
177
178 return &amic->flash;
179}