blob: d286bd66f684ddb39a78eaa5685199bd9cb58543 [file] [log] [blame]
Chris Douglassb34739b2014-02-14 13:51:26 -05001/*
2 * adesto.c
3 * Driver for Adesto Technologies SPI flash
4 * Copyright 2014 Orion Technologies, LLC
5 * Author: Chris Douglass <cdouglass <at> oriontechnologies.com>
6 *
7 * based on winbond.c
8 * Copyright 2008, Network Appliance Inc.
9 * Author: Jason McMullan <mcmullan <at> netapp.com>
10 * Licensed under the GPL-2 or later.
11 */
12
Furquan Shaikhc28984d2016-11-20 21:04:00 -080013#include <console/console.h>
Chris Douglassb34739b2014-02-14 13:51:26 -050014#include <stdlib.h>
Furquan Shaikh810e2cd2016-12-05 20:32:24 -080015#include <string.h>
Chris Douglassb34739b2014-02-14 13:51:26 -050016#include <spi_flash.h>
Furquan Shaikhc28984d2016-11-20 21:04:00 -080017#include <spi-generic.h>
Edward O'Callaghanc4561e22014-06-26 15:02:40 +100018
Chris Douglassb34739b2014-02-14 13:51:26 -050019#include "spi_flash_internal.h"
20
21/* at25dfxx-specific commands */
22#define CMD_AT25DF_WREN 0x06 /* Write Enable */
23#define CMD_AT25DF_WRDI 0x04 /* Write Disable */
24#define CMD_AT25DF_RDSR 0x05 /* Read Status Register */
25#define CMD_AT25DF_WRSR 0x01 /* Write Status Register */
26#define CMD_AT25DF_READ 0x03 /* Read Data Bytes */
27#define CMD_AT25DF_FAST_READ 0x0b /* Read Data Bytes at Higher Speed */
28#define CMD_AT25DF_PP 0x02 /* Page Program */
29#define CMD_AT25DF_SE 0x20 /* Sector (4K) Erase */
30#define CMD_AT25DF_BE 0xd8 /* Block (64K) Erase */
31#define CMD_AT25DF_CE 0xc7 /* Chip Erase */
32#define CMD_AT25DF_DP 0xb9 /* Deep Power-down */
33#define CMD_AT25DF_RES 0xab /* Release from DP, and Read Signature */
34
35struct adesto_spi_flash_params {
36 uint16_t id;
37 /* Log2 of page size in power-of-two mode */
38 uint8_t l2_page_size;
39 uint16_t pages_per_sector;
40 uint16_t sectors_per_block;
41 uint16_t nr_blocks;
42 const char *name;
43};
44
45/* spi_flash needs to be first so upper layers can free() it */
46struct adesto_spi_flash {
47 struct spi_flash flash;
48 const struct adesto_spi_flash_params *params;
49};
50
51static inline struct adesto_spi_flash *
Furquan Shaikhc28984d2016-11-20 21:04:00 -080052to_adesto_spi_flash(const struct spi_flash *flash)
Chris Douglassb34739b2014-02-14 13:51:26 -050053{
54 return container_of(flash, struct adesto_spi_flash, flash);
55}
56
57static const struct adesto_spi_flash_params adesto_spi_flash_table[] = {
58 {
59 .id = 0x4501,
60 .l2_page_size = 8,
61 .pages_per_sector = 16,
62 .sectors_per_block = 16,
63 .nr_blocks = 16,
64 .name = "AT25DF081",
65 },
66 {
67 .id = 0x4701,
68 .l2_page_size = 8,
69 .pages_per_sector = 16,
70 .sectors_per_block = 16,
71 .nr_blocks = 64,
72 .name = "AT25DF321",
73 },
74 {
75 .id = 0x4800,
76 .l2_page_size = 8,
77 .pages_per_sector = 16,
78 .sectors_per_block = 16,
79 .nr_blocks = 128,
80 .name = "AT25DF641",
81 },
82};
83
Furquan Shaikhc28984d2016-11-20 21:04:00 -080084static int adesto_write(const struct spi_flash *flash, u32 offset, size_t len,
85 const void *buf)
Chris Douglassb34739b2014-02-14 13:51:26 -050086{
87 struct adesto_spi_flash *stm = to_adesto_spi_flash(flash);
88 unsigned long byte_addr;
89 unsigned long page_size;
90 size_t chunk_len;
91 size_t actual;
92 int ret;
93 u8 cmd[4];
94
Kyösti Mälkki77d12802014-06-29 16:15:39 +030095 page_size = 1 << stm->params->l2_page_size;
Chris Douglassb34739b2014-02-14 13:51:26 -050096
Chris Douglassb34739b2014-02-14 13:51:26 -050097 for (actual = 0; actual < len; actual += chunk_len) {
Aaron Durbin41f66902016-12-17 13:16:07 -060098 byte_addr = offset % page_size;
Chris Douglassb34739b2014-02-14 13:51:26 -050099 chunk_len = min(len - actual, page_size - byte_addr);
Kyösti Mälkki11104952014-06-29 16:17:33 +0300100 chunk_len = spi_crop_chunk(sizeof(cmd), chunk_len);
Chris Douglassb34739b2014-02-14 13:51:26 -0500101
102 cmd[0] = CMD_AT25DF_PP;
103 cmd[1] = (offset >> 16) & 0xff;
104 cmd[2] = (offset >> 8) & 0xff;
105 cmd[3] = offset & 0xff;
106#if CONFIG_DEBUG_SPI_FLASH
107 printk(BIOS_SPEW, "PP: 0x%p => cmd = { 0x%02x 0x%02x%02x%02x }"
108 " chunk_len = %zu\n", buf + actual,
109 cmd[0], cmd[1], cmd[2], cmd[3], chunk_len);
110#endif
111
Furquan Shaikh810e2cd2016-12-05 20:32:24 -0800112 ret = spi_flash_cmd(&flash->spi, CMD_AT25DF_WREN, NULL, 0);
Chris Douglassb34739b2014-02-14 13:51:26 -0500113 if (ret < 0) {
114 printk(BIOS_WARNING, "SF: Enabling Write failed\n");
115 goto out;
116 }
117
Furquan Shaikh810e2cd2016-12-05 20:32:24 -0800118 ret = spi_flash_cmd_write(&flash->spi, cmd, sizeof(cmd),
Chris Douglassb34739b2014-02-14 13:51:26 -0500119 buf + actual, chunk_len);
120 if (ret < 0) {
121 printk(BIOS_WARNING, "SF: adesto Page Program failed\n");
122 goto out;
123 }
124
125 ret = spi_flash_cmd_wait_ready(flash, SPI_FLASH_PROG_TIMEOUT);
126 if (ret)
127 goto out;
128
129 offset += chunk_len;
Chris Douglassb34739b2014-02-14 13:51:26 -0500130 }
131
132#if CONFIG_DEBUG_SPI_FLASH
133 printk(BIOS_SPEW, "SF: adesto: Successfully programmed %zu bytes @"
134 " 0x%lx\n", len, (unsigned long)(offset - len));
135#endif
136 ret = 0;
137
138out:
Chris Douglassb34739b2014-02-14 13:51:26 -0500139 return ret;
140}
141
Chris Douglassb34739b2014-02-14 13:51:26 -0500142struct spi_flash *spi_flash_probe_adesto(struct spi_slave *spi, u8 *idcode)
143{
144 const struct adesto_spi_flash_params *params;
145 unsigned page_size;
146 struct adesto_spi_flash *stm;
147 unsigned int i;
148
149 for (i = 0; i < ARRAY_SIZE(adesto_spi_flash_table); i++) {
150 params = &adesto_spi_flash_table[i];
151 if (params->id == ((idcode[1] << 8) | idcode[2]))
152 break;
153 }
154
155 if (i == ARRAY_SIZE(adesto_spi_flash_table)) {
156 printk(BIOS_WARNING, "SF: Unsupported adesto ID %02x%02x\n",
157 idcode[1], idcode[2]);
158 return NULL;
159 }
160
161 stm = malloc(sizeof(struct adesto_spi_flash));
162 if (!stm) {
163 printk(BIOS_WARNING, "SF: Failed to allocate memory\n");
164 return NULL;
165 }
166
167 stm->params = params;
Furquan Shaikh810e2cd2016-12-05 20:32:24 -0800168 memcpy(&stm->flash.spi, spi, sizeof(*spi));
Chris Douglassb34739b2014-02-14 13:51:26 -0500169 stm->flash.name = params->name;
170
171 /* Assuming power-of-two page size initially. */
172 page_size = 1 << params->l2_page_size;
173
Furquan Shaikhc28984d2016-11-20 21:04:00 -0800174 stm->flash.internal_write = adesto_write;
175 stm->flash.internal_erase = spi_flash_cmd_erase;
Chris Douglassb34739b2014-02-14 13:51:26 -0500176#if CONFIG_SPI_FLASH_NO_FAST_READ
Furquan Shaikhc28984d2016-11-20 21:04:00 -0800177 stm->flash.internal_read = spi_flash_cmd_read_slow;
Chris Douglassb34739b2014-02-14 13:51:26 -0500178#else
Furquan Shaikhc28984d2016-11-20 21:04:00 -0800179 stm->flash.internal_read = spi_flash_cmd_read_fast;
Chris Douglassb34739b2014-02-14 13:51:26 -0500180#endif
181 stm->flash.sector_size = (1 << stm->params->l2_page_size) *
182 stm->params->pages_per_sector;
183 stm->flash.size = page_size * params->pages_per_sector
184 * params->sectors_per_block
185 * params->nr_blocks;
Dan Ehrenberga5aac762015-01-08 10:29:19 -0800186 stm->flash.erase_cmd = CMD_AT25DF_SE;
Chris Douglassb34739b2014-02-14 13:51:26 -0500187
188 return &stm->flash;
189}