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