blob: 96a808d423f504b3091eaba69fb1d2f93edd58cd [file] [log] [blame]
Barnali Sarkar89331cd2017-02-16 17:22:37 +05301/*
2 * This file is part of the coreboot project.
3 *
4 * Copyright (C) 2017 Intel Corporation.
5 *
6 * This program is free software; you can redistribute it and/or modify
7 * it under the terms of the GNU General Public License as published by
8 * the Free Software Foundation; version 2 of the License.
9 *
10 * This program is distributed in the hope that it will be useful,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 * GNU General Public License for more details.
14 */
15
16#include <arch/early_variables.h>
17#include <arch/io.h>
18#include <console/console.h>
19#include <fast_spi_def.h>
20#include <intelblocks/fast_spi.h>
21#include <soc/intel/common/spi_flash.h>
22#include <soc/pci_devs.h>
23#include <spi_flash.h>
24#include <string.h>
25#include <timer.h>
26
27/* Helper to create a FAST_SPI context on API entry. */
28#define BOILERPLATE_CREATE_CTX(ctx) \
29 struct fast_spi_flash_ctx real_ctx; \
30 struct fast_spi_flash_ctx *ctx = &real_ctx; \
31 _fast_spi_flash_get_ctx(ctx)
32
33/*
34 * Anything that's not success is <0. Provided solely for readability, as these
35 * constants are not used outside this file.
36 */
37enum errors {
38 SUCCESS = 0,
39 E_TIMEOUT = -1,
40 E_HW_ERROR = -2,
41 E_ARGUMENT = -3,
42};
43
44/* Reduce data-passing burden by grouping transaction data in a context. */
45struct fast_spi_flash_ctx {
46 uintptr_t mmio_base;
47};
48
49static void _fast_spi_flash_get_ctx(struct fast_spi_flash_ctx *ctx)
50{
51 ctx->mmio_base = (uintptr_t)fast_spi_get_bar();
52}
53
54/* Read register from the FAST_SPI flash controller. */
55static uint32_t fast_spi_flash_ctrlr_reg_read(struct fast_spi_flash_ctx *ctx,
56 uint16_t reg)
57{
58 uintptr_t addr = ALIGN_DOWN(ctx->mmio_base + reg, sizeof(uint32_t));
59 return read32((void *)addr);
60}
61
62/* Write to register in FAST_SPI flash controller. */
63static void fast_spi_flash_ctrlr_reg_write(struct fast_spi_flash_ctx *ctx,
64 uint16_t reg, uint32_t val)
65{
66 uintptr_t addr = ALIGN_DOWN(ctx->mmio_base + reg, sizeof(uint32_t));
67 write32((void *)addr, val);
68}
69
70/*
71 * The hardware datasheet is not clear on what HORD values actually do. It
72 * seems that HORD_SFDP provides access to the first 8 bytes of the SFDP, which
73 * is the signature and revision fields. HORD_JEDEC provides access to the
74 * actual flash parameters, and is most likely what you want to use when
75 * probing the flash from software.
76 * It's okay to rely on SFDP, since the SPI flash controller requires an SFDP
77 * 1.5 or newer compliant FAST_SPI flash chip.
78 * NOTE: Due to the register layout of the hardware, all accesses will be
79 * aligned to a 4 byte boundary.
80 */
81static uint32_t fast_spi_flash_read_sfdp_param(struct fast_spi_flash_ctx *ctx,
82 uint16_t sfdp_reg)
83{
84 uint32_t ptinx_index = sfdp_reg & SPIBAR_PTINX_IDX_MASK;
85 fast_spi_flash_ctrlr_reg_write(ctx, SPIBAR_PTINX,
86 ptinx_index | SPIBAR_PTINX_HORD_JEDEC);
87 return fast_spi_flash_ctrlr_reg_read(ctx, SPIBAR_PTDATA);
88}
89
90/* Fill FDATAn FIFO in preparation for a write transaction. */
91static void fill_xfer_fifo(struct fast_spi_flash_ctx *ctx, const void *data,
92 size_t len)
93{
94 /* YES! memcpy() works. FDATAn does not require 32-bit accesses. */
95 memcpy((void *)(ctx->mmio_base + SPIBAR_FDATA(0)), data, len);
96}
97
98/* Drain FDATAn FIFO after a read transaction populates data. */
99static void drain_xfer_fifo(struct fast_spi_flash_ctx *ctx, void *dest,
100 size_t len)
101{
102 /* YES! memcpy() works. FDATAn does not require 32-bit accesses. */
103 memcpy(dest, (void *)(ctx->mmio_base + SPIBAR_FDATA(0)), len);
104}
105
106/* Fire up a transfer using the hardware sequencer. */
107static void start_hwseq_xfer(struct fast_spi_flash_ctx *ctx,
108 uint32_t hsfsts_cycle, uint32_t flash_addr, size_t len)
109{
110 /* Make sure all W1C status bits get cleared. */
111 uint32_t hsfsts = SPIBAR_HSFSTS_W1C_BITS;
112 /* Set up transaction parameters. */
113 hsfsts |= hsfsts_cycle & SPIBAR_HSFSTS_FCYCLE_MASK;
114 hsfsts |= SPIBAR_HSFSTS_FDBC(len - 1);
115
116 fast_spi_flash_ctrlr_reg_write(ctx, SPIBAR_FADDR, flash_addr);
117 fast_spi_flash_ctrlr_reg_write(ctx, SPIBAR_HSFSTS_CTL,
118 hsfsts | SPIBAR_HSFSTS_FGO);
119}
120
121static int wait_for_hwseq_xfer(struct fast_spi_flash_ctx *ctx,
122 uint32_t flash_addr)
123{
124 struct stopwatch sw;
125 uint32_t hsfsts;
126
127 stopwatch_init_msecs_expire(&sw, SPIBAR_HWSEQ_XFER_TIMEOUT);
128 do {
129 hsfsts = fast_spi_flash_ctrlr_reg_read(ctx, SPIBAR_HSFSTS_CTL);
130
131 if (hsfsts & SPIBAR_HSFSTS_FCERR) {
132 printk(BIOS_ERR, "SPI Transaction Error at Flash Offset %x HSFSTS = 0x%08x\n",
133 flash_addr, hsfsts);
134 return E_HW_ERROR;
135 }
136
137 if (hsfsts & SPIBAR_HSFSTS_FDONE)
138 return SUCCESS;
139 } while (!(stopwatch_expired(&sw)));
140
141 printk(BIOS_ERR, "SPI Transaction Timeout (Exceeded %d ms) at Flash Offset %x HSFSTS = 0x%08x\n",
142 SPIBAR_HWSEQ_XFER_TIMEOUT, flash_addr, hsfsts);
143 return E_TIMEOUT;
144}
145
146/* Execute FAST_SPI flash transfer. This is a blocking call. */
147static int exec_sync_hwseq_xfer(struct fast_spi_flash_ctx *ctx,
148 uint32_t hsfsts_cycle, uint32_t flash_addr,
149 size_t len)
150{
151 start_hwseq_xfer(ctx, hsfsts_cycle, flash_addr, len);
152 return wait_for_hwseq_xfer(ctx, flash_addr);
153}
154
155/*
156 * Ensure read/write xfer len is not greater than SPIBAR_FDATA_FIFO_SIZE and
Furquan Shaikhfc1a1232017-05-12 00:19:56 -0700157 * that the operation does not cross page boundary.
Barnali Sarkar89331cd2017-02-16 17:22:37 +0530158 */
Furquan Shaikhfc1a1232017-05-12 00:19:56 -0700159static size_t get_xfer_len(const struct spi_flash *flash, uint32_t addr,
160 size_t len)
Barnali Sarkar89331cd2017-02-16 17:22:37 +0530161{
162 size_t xfer_len = min(len, SPIBAR_FDATA_FIFO_SIZE);
Furquan Shaikhfc1a1232017-05-12 00:19:56 -0700163 size_t bytes_left = ALIGN_UP(addr, flash->page_size) - addr;
Barnali Sarkar89331cd2017-02-16 17:22:37 +0530164
165 if (bytes_left)
166 xfer_len = min(xfer_len, bytes_left);
167
168 return xfer_len;
169}
170
171
172/* Flash device operations. */
173static struct spi_flash boot_flash CAR_GLOBAL;
174
175static int fast_spi_flash_erase(const struct spi_flash *flash,
176 uint32_t offset, size_t len)
177{
178 int ret;
179 size_t erase_size;
180 uint32_t erase_cycle;
181
182 BOILERPLATE_CREATE_CTX(ctx);
183
184 if (!IS_ALIGNED(offset, 4 * KiB) || !IS_ALIGNED(len, 4 * KiB)) {
185 printk(BIOS_ERR, "BUG! SPI erase region not sector aligned\n");
186 return E_ARGUMENT;
187 }
188
189 while (len) {
190 if (IS_ALIGNED(offset, 64 * KiB) && (len >= 64 * KiB)) {
191 erase_size = 64 * KiB;
192 erase_cycle = SPIBAR_HSFSTS_CYCLE_64K_ERASE;
193 } else {
194 erase_size = 4 * KiB;
195 erase_cycle = SPIBAR_HSFSTS_CYCLE_4K_ERASE;
196 }
197 printk(BIOS_SPEW, "Erasing flash addr %x + %zu KiB\n",
198 offset, erase_size / KiB);
199
200 ret = exec_sync_hwseq_xfer(ctx, erase_cycle, offset, 0);
201 if (ret != SUCCESS)
202 return ret;
203
204 offset += erase_size;
205 len -= erase_size;
206 }
207
208 return SUCCESS;
209}
210
211static int fast_spi_flash_read(const struct spi_flash *flash,
212 uint32_t addr, size_t len, void *buf)
213{
214 int ret;
215 size_t xfer_len;
216 uint8_t *data = buf;
217
218 BOILERPLATE_CREATE_CTX(ctx);
219
220 while (len) {
Furquan Shaikhfc1a1232017-05-12 00:19:56 -0700221 xfer_len = get_xfer_len(flash, addr, len);
Barnali Sarkar89331cd2017-02-16 17:22:37 +0530222
223 ret = exec_sync_hwseq_xfer(ctx, SPIBAR_HSFSTS_CYCLE_READ,
224 addr, xfer_len);
225 if (ret != SUCCESS)
226 return ret;
227
228 drain_xfer_fifo(ctx, data, xfer_len);
229
230 addr += xfer_len;
231 data += xfer_len;
232 len -= xfer_len;
233 }
234
235 return SUCCESS;
236}
237
238static int fast_spi_flash_write(const struct spi_flash *flash,
239 uint32_t addr, size_t len, const void *buf)
240{
241 int ret;
242 size_t xfer_len;
243 const uint8_t *data = buf;
244
245 BOILERPLATE_CREATE_CTX(ctx);
246
247 while (len) {
Furquan Shaikhfc1a1232017-05-12 00:19:56 -0700248 xfer_len = get_xfer_len(flash, addr, len);
Barnali Sarkar89331cd2017-02-16 17:22:37 +0530249 fill_xfer_fifo(ctx, data, xfer_len);
250
251 ret = exec_sync_hwseq_xfer(ctx, SPIBAR_HSFSTS_CYCLE_WRITE,
252 addr, xfer_len);
253 if (ret != SUCCESS)
254 return ret;
255
256 addr += xfer_len;
257 data += xfer_len;
258 len -= xfer_len;
259 }
260
261 return SUCCESS;
262}
263
264static int fast_spi_flash_status(const struct spi_flash *flash,
265 uint8_t *reg)
266{
267 int ret;
268 BOILERPLATE_CREATE_CTX(ctx);
269
270 ret = exec_sync_hwseq_xfer(ctx, SPIBAR_HSFSTS_CYCLE_RD_STATUS, 0,
271 sizeof(*reg));
272 if (ret != SUCCESS)
273 return ret;
274
275 drain_xfer_fifo(ctx, reg, sizeof(*reg));
276 return ret;
277}
278
279/*
280 * We can't use FDOC and FDOD to read FLCOMP, as previous platforms did.
281 * For details see:
282 * Ch 31, SPI: p. 194
283 * The size of the flash component is always taken from density field in the
284 * SFDP table. FLCOMP.C0DEN is no longer used by the Flash Controller.
285 */
286struct spi_flash *spi_flash_programmer_probe(struct spi_slave *dev, int force)
287{
288 BOILERPLATE_CREATE_CTX(ctx);
289 struct spi_flash *flash;
290 uint32_t flash_bits;
291
292 flash = car_get_var_ptr(&boot_flash);
293
294 /*
295 * bytes = (bits + 1) / 8;
296 * But we need to do the addition in a way which doesn't overflow for
297 * 4 Gbit devices (flash_bits == 0xffffffff).
298 */
299 flash_bits = fast_spi_flash_read_sfdp_param(ctx, 0x04);
300 flash->size = (flash_bits >> 3) + 1;
301
302 memcpy(&flash->spi, dev, sizeof(*dev));
303 flash->name = "FAST_SPI Hardware Sequencer";
304
305 /* Can erase both 4 KiB and 64 KiB chunks. Declare the smaller size. */
306 flash->sector_size = 4 * KiB;
Furquan Shaikhfc1a1232017-05-12 00:19:56 -0700307 flash->page_size = 256;
Barnali Sarkar89331cd2017-02-16 17:22:37 +0530308 /*
309 * FIXME: Get erase+cmd, and status_cmd from SFDP.
310 *
311 * flash->erase_cmd = ???
312 * flash->status_cmd = ???
313 */
314
315 flash->internal_write = fast_spi_flash_write;
316 flash->internal_erase = fast_spi_flash_erase;
317 flash->internal_read = fast_spi_flash_read;
318 flash->internal_status = fast_spi_flash_status;
319
320 return flash;
321}
322
323int spi_flash_get_fpr_info(struct fpr_info *info)
324{
325 BOILERPLATE_CREATE_CTX(ctx);
326
327 info->base = ctx->mmio_base + SPIBAR_FPR_BASE;
328 info->max = SPIBAR_FPR_MAX;
329 return 0;
330}
331
332/*
333 * Minimal set of commands to read WPSR from FAST_SPI.
334 * Returns 0 on success, < 0 on failure.
335 */
336int fast_spi_flash_read_wpsr(u8 *sr)
337{
338 uint8_t rdsr;
339 int ret = 0;
340
341 fast_spi_init();
342
343 /* sending NULL for spiflash struct parameter since we are not
344 * calling HWSEQ read_status() call via Probe.
345 */
346 ret = fast_spi_flash_status(NULL, &rdsr);
347 if (ret) {
348 printk(BIOS_ERR, "SPI rdsr failed\n");
349 return ret;
350 }
351 *sr = rdsr & WPSR_MASK_SRP0_BIT;
352
353 return 0;
354}
Furquan Shaikhf1db5fd2017-05-02 19:43:20 -0700355
356static int fast_spi_flash_ctrlr_setup(const struct spi_slave *dev)
357{
358 if (dev->cs != 0) {
359 printk(BIOS_ERR, "%s: Invalid CS for fast SPI bus=0x%x,cs=0x%x!\n",
360 __func__, dev->bus, dev->cs);
361 return -1;
362 }
363
364 return 0;
365}
366
367const struct spi_ctrlr fast_spi_flash_ctrlr = {
368 .setup = fast_spi_flash_ctrlr_setup,
Furquan Shaikhde705fa2017-04-19 19:27:28 -0700369 .max_xfer_size = SPI_CTRLR_DEFAULT_MAX_XFER_SIZE,
Furquan Shaikhf1db5fd2017-05-02 19:43:20 -0700370};