blob: 97e231ca3fa3d97e37909ea1df6a7e084b4939fe [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>
Kyösti Mälkki13f66502019-03-03 08:01:05 +020017#include <device/mmio.h>
Barnali Sarkar89331cd2017-02-16 17:22:37 +053018#include <console/console.h>
19#include <fast_spi_def.h>
20#include <intelblocks/fast_spi.h>
Barnali Sarkar89331cd2017-02-16 17:22:37 +053021#include <soc/pci_devs.h>
22#include <spi_flash.h>
23#include <string.h>
24#include <timer.h>
25
26/* Helper to create a FAST_SPI context on API entry. */
27#define BOILERPLATE_CREATE_CTX(ctx) \
28 struct fast_spi_flash_ctx real_ctx; \
29 struct fast_spi_flash_ctx *ctx = &real_ctx; \
30 _fast_spi_flash_get_ctx(ctx)
31
32/*
33 * Anything that's not success is <0. Provided solely for readability, as these
34 * constants are not used outside this file.
35 */
36enum errors {
37 SUCCESS = 0,
38 E_TIMEOUT = -1,
39 E_HW_ERROR = -2,
40 E_ARGUMENT = -3,
41};
42
43/* Reduce data-passing burden by grouping transaction data in a context. */
44struct fast_spi_flash_ctx {
45 uintptr_t mmio_base;
46};
47
48static void _fast_spi_flash_get_ctx(struct fast_spi_flash_ctx *ctx)
49{
50 ctx->mmio_base = (uintptr_t)fast_spi_get_bar();
51}
52
53/* Read register from the FAST_SPI flash controller. */
54static uint32_t fast_spi_flash_ctrlr_reg_read(struct fast_spi_flash_ctx *ctx,
55 uint16_t reg)
56{
57 uintptr_t addr = ALIGN_DOWN(ctx->mmio_base + reg, sizeof(uint32_t));
58 return read32((void *)addr);
59}
60
61/* Write to register in FAST_SPI flash controller. */
62static void fast_spi_flash_ctrlr_reg_write(struct fast_spi_flash_ctx *ctx,
63 uint16_t reg, uint32_t val)
64{
65 uintptr_t addr = ALIGN_DOWN(ctx->mmio_base + reg, sizeof(uint32_t));
66 write32((void *)addr, val);
67}
68
69/*
70 * The hardware datasheet is not clear on what HORD values actually do. It
71 * seems that HORD_SFDP provides access to the first 8 bytes of the SFDP, which
72 * is the signature and revision fields. HORD_JEDEC provides access to the
73 * actual flash parameters, and is most likely what you want to use when
74 * probing the flash from software.
75 * It's okay to rely on SFDP, since the SPI flash controller requires an SFDP
76 * 1.5 or newer compliant FAST_SPI flash chip.
77 * NOTE: Due to the register layout of the hardware, all accesses will be
78 * aligned to a 4 byte boundary.
79 */
80static uint32_t fast_spi_flash_read_sfdp_param(struct fast_spi_flash_ctx *ctx,
81 uint16_t sfdp_reg)
82{
83 uint32_t ptinx_index = sfdp_reg & SPIBAR_PTINX_IDX_MASK;
84 fast_spi_flash_ctrlr_reg_write(ctx, SPIBAR_PTINX,
85 ptinx_index | SPIBAR_PTINX_HORD_JEDEC);
86 return fast_spi_flash_ctrlr_reg_read(ctx, SPIBAR_PTDATA);
87}
88
89/* Fill FDATAn FIFO in preparation for a write transaction. */
90static void fill_xfer_fifo(struct fast_spi_flash_ctx *ctx, const void *data,
91 size_t len)
92{
93 /* YES! memcpy() works. FDATAn does not require 32-bit accesses. */
94 memcpy((void *)(ctx->mmio_base + SPIBAR_FDATA(0)), data, len);
95}
96
97/* Drain FDATAn FIFO after a read transaction populates data. */
98static void drain_xfer_fifo(struct fast_spi_flash_ctx *ctx, void *dest,
99 size_t len)
100{
101 /* YES! memcpy() works. FDATAn does not require 32-bit accesses. */
102 memcpy(dest, (void *)(ctx->mmio_base + SPIBAR_FDATA(0)), len);
103}
104
105/* Fire up a transfer using the hardware sequencer. */
106static void start_hwseq_xfer(struct fast_spi_flash_ctx *ctx,
107 uint32_t hsfsts_cycle, uint32_t flash_addr, size_t len)
108{
109 /* Make sure all W1C status bits get cleared. */
110 uint32_t hsfsts = SPIBAR_HSFSTS_W1C_BITS;
111 /* Set up transaction parameters. */
112 hsfsts |= hsfsts_cycle & SPIBAR_HSFSTS_FCYCLE_MASK;
113 hsfsts |= SPIBAR_HSFSTS_FDBC(len - 1);
114
115 fast_spi_flash_ctrlr_reg_write(ctx, SPIBAR_FADDR, flash_addr);
116 fast_spi_flash_ctrlr_reg_write(ctx, SPIBAR_HSFSTS_CTL,
117 hsfsts | SPIBAR_HSFSTS_FGO);
118}
119
120static int wait_for_hwseq_xfer(struct fast_spi_flash_ctx *ctx,
121 uint32_t flash_addr)
122{
123 struct stopwatch sw;
124 uint32_t hsfsts;
125
126 stopwatch_init_msecs_expire(&sw, SPIBAR_HWSEQ_XFER_TIMEOUT);
127 do {
128 hsfsts = fast_spi_flash_ctrlr_reg_read(ctx, SPIBAR_HSFSTS_CTL);
129
130 if (hsfsts & SPIBAR_HSFSTS_FCERR) {
131 printk(BIOS_ERR, "SPI Transaction Error at Flash Offset %x HSFSTS = 0x%08x\n",
132 flash_addr, hsfsts);
133 return E_HW_ERROR;
134 }
135
136 if (hsfsts & SPIBAR_HSFSTS_FDONE)
137 return SUCCESS;
138 } while (!(stopwatch_expired(&sw)));
139
140 printk(BIOS_ERR, "SPI Transaction Timeout (Exceeded %d ms) at Flash Offset %x HSFSTS = 0x%08x\n",
141 SPIBAR_HWSEQ_XFER_TIMEOUT, flash_addr, hsfsts);
142 return E_TIMEOUT;
143}
144
145/* Execute FAST_SPI flash transfer. This is a blocking call. */
146static int exec_sync_hwseq_xfer(struct fast_spi_flash_ctx *ctx,
147 uint32_t hsfsts_cycle, uint32_t flash_addr,
148 size_t len)
149{
150 start_hwseq_xfer(ctx, hsfsts_cycle, flash_addr, len);
151 return wait_for_hwseq_xfer(ctx, flash_addr);
152}
153
154/*
155 * Ensure read/write xfer len is not greater than SPIBAR_FDATA_FIFO_SIZE and
Furquan Shaikhfc1a1232017-05-12 00:19:56 -0700156 * that the operation does not cross page boundary.
Barnali Sarkar89331cd2017-02-16 17:22:37 +0530157 */
Furquan Shaikhfc1a1232017-05-12 00:19:56 -0700158static size_t get_xfer_len(const struct spi_flash *flash, uint32_t addr,
159 size_t len)
Barnali Sarkar89331cd2017-02-16 17:22:37 +0530160{
161 size_t xfer_len = min(len, SPIBAR_FDATA_FIFO_SIZE);
Furquan Shaikhfc1a1232017-05-12 00:19:56 -0700162 size_t bytes_left = ALIGN_UP(addr, flash->page_size) - addr;
Barnali Sarkar89331cd2017-02-16 17:22:37 +0530163
164 if (bytes_left)
165 xfer_len = min(xfer_len, bytes_left);
166
167 return xfer_len;
168}
169
170
Barnali Sarkar89331cd2017-02-16 17:22:37 +0530171static int fast_spi_flash_erase(const struct spi_flash *flash,
172 uint32_t offset, size_t len)
173{
174 int ret;
175 size_t erase_size;
176 uint32_t erase_cycle;
177
178 BOILERPLATE_CREATE_CTX(ctx);
179
180 if (!IS_ALIGNED(offset, 4 * KiB) || !IS_ALIGNED(len, 4 * KiB)) {
181 printk(BIOS_ERR, "BUG! SPI erase region not sector aligned\n");
182 return E_ARGUMENT;
183 }
184
185 while (len) {
186 if (IS_ALIGNED(offset, 64 * KiB) && (len >= 64 * KiB)) {
187 erase_size = 64 * KiB;
188 erase_cycle = SPIBAR_HSFSTS_CYCLE_64K_ERASE;
189 } else {
190 erase_size = 4 * KiB;
191 erase_cycle = SPIBAR_HSFSTS_CYCLE_4K_ERASE;
192 }
193 printk(BIOS_SPEW, "Erasing flash addr %x + %zu KiB\n",
194 offset, erase_size / KiB);
195
196 ret = exec_sync_hwseq_xfer(ctx, erase_cycle, offset, 0);
197 if (ret != SUCCESS)
198 return ret;
199
200 offset += erase_size;
201 len -= erase_size;
202 }
203
204 return SUCCESS;
205}
206
207static int fast_spi_flash_read(const struct spi_flash *flash,
208 uint32_t addr, size_t len, void *buf)
209{
210 int ret;
211 size_t xfer_len;
212 uint8_t *data = buf;
213
214 BOILERPLATE_CREATE_CTX(ctx);
215
216 while (len) {
Furquan Shaikhfc1a1232017-05-12 00:19:56 -0700217 xfer_len = get_xfer_len(flash, addr, len);
Barnali Sarkar89331cd2017-02-16 17:22:37 +0530218
219 ret = exec_sync_hwseq_xfer(ctx, SPIBAR_HSFSTS_CYCLE_READ,
220 addr, xfer_len);
221 if (ret != SUCCESS)
222 return ret;
223
224 drain_xfer_fifo(ctx, data, xfer_len);
225
226 addr += xfer_len;
227 data += xfer_len;
228 len -= xfer_len;
229 }
230
231 return SUCCESS;
232}
233
234static int fast_spi_flash_write(const struct spi_flash *flash,
235 uint32_t addr, size_t len, const void *buf)
236{
237 int ret;
238 size_t xfer_len;
239 const uint8_t *data = buf;
240
241 BOILERPLATE_CREATE_CTX(ctx);
242
243 while (len) {
Furquan Shaikhfc1a1232017-05-12 00:19:56 -0700244 xfer_len = get_xfer_len(flash, addr, len);
Barnali Sarkar89331cd2017-02-16 17:22:37 +0530245 fill_xfer_fifo(ctx, data, xfer_len);
246
247 ret = exec_sync_hwseq_xfer(ctx, SPIBAR_HSFSTS_CYCLE_WRITE,
248 addr, xfer_len);
249 if (ret != SUCCESS)
250 return ret;
251
252 addr += xfer_len;
253 data += xfer_len;
254 len -= xfer_len;
255 }
256
257 return SUCCESS;
258}
259
260static int fast_spi_flash_status(const struct spi_flash *flash,
261 uint8_t *reg)
262{
263 int ret;
264 BOILERPLATE_CREATE_CTX(ctx);
265
266 ret = exec_sync_hwseq_xfer(ctx, SPIBAR_HSFSTS_CYCLE_RD_STATUS, 0,
267 sizeof(*reg));
268 if (ret != SUCCESS)
269 return ret;
270
271 drain_xfer_fifo(ctx, reg, sizeof(*reg));
272 return ret;
273}
274
Furquan Shaikhe2fc5e22017-05-17 17:26:01 -0700275const struct spi_flash_ops fast_spi_flash_ops = {
276 .read = fast_spi_flash_read,
277 .write = fast_spi_flash_write,
278 .erase = fast_spi_flash_erase,
279 .status = fast_spi_flash_status,
280};
281
Barnali Sarkar89331cd2017-02-16 17:22:37 +0530282/*
283 * We can't use FDOC and FDOD to read FLCOMP, as previous platforms did.
284 * For details see:
285 * Ch 31, SPI: p. 194
286 * The size of the flash component is always taken from density field in the
287 * SFDP table. FLCOMP.C0DEN is no longer used by the Flash Controller.
288 */
Furquan Shaikha1491572017-05-17 19:14:06 -0700289static int fast_spi_flash_probe(const struct spi_slave *dev,
290 struct spi_flash *flash)
Barnali Sarkar89331cd2017-02-16 17:22:37 +0530291{
292 BOILERPLATE_CREATE_CTX(ctx);
Barnali Sarkar89331cd2017-02-16 17:22:37 +0530293 uint32_t flash_bits;
294
Barnali Sarkar89331cd2017-02-16 17:22:37 +0530295 /*
296 * bytes = (bits + 1) / 8;
297 * But we need to do the addition in a way which doesn't overflow for
298 * 4 Gbit devices (flash_bits == 0xffffffff).
299 */
300 flash_bits = fast_spi_flash_read_sfdp_param(ctx, 0x04);
301 flash->size = (flash_bits >> 3) + 1;
302
303 memcpy(&flash->spi, dev, sizeof(*dev));
304 flash->name = "FAST_SPI Hardware Sequencer";
305
306 /* Can erase both 4 KiB and 64 KiB chunks. Declare the smaller size. */
307 flash->sector_size = 4 * KiB;
Furquan Shaikhfc1a1232017-05-12 00:19:56 -0700308 flash->page_size = 256;
Barnali Sarkar89331cd2017-02-16 17:22:37 +0530309 /*
310 * FIXME: Get erase+cmd, and status_cmd from SFDP.
311 *
312 * flash->erase_cmd = ???
313 * flash->status_cmd = ???
314 */
315
Furquan Shaikhe2fc5e22017-05-17 17:26:01 -0700316 flash->ops = &fast_spi_flash_ops;
Furquan Shaikh30221b42017-05-15 14:35:15 -0700317 return 0;
Barnali Sarkar89331cd2017-02-16 17:22:37 +0530318}
319
Barnali Sarkar89331cd2017-02-16 17:22:37 +0530320/*
321 * Minimal set of commands to read WPSR from FAST_SPI.
322 * Returns 0 on success, < 0 on failure.
323 */
324int fast_spi_flash_read_wpsr(u8 *sr)
325{
326 uint8_t rdsr;
327 int ret = 0;
328
329 fast_spi_init();
330
331 /* sending NULL for spiflash struct parameter since we are not
332 * calling HWSEQ read_status() call via Probe.
333 */
334 ret = fast_spi_flash_status(NULL, &rdsr);
335 if (ret) {
336 printk(BIOS_ERR, "SPI rdsr failed\n");
337 return ret;
338 }
339 *sr = rdsr & WPSR_MASK_SRP0_BIT;
340
341 return 0;
342}
Furquan Shaikhf1db5fd2017-05-02 19:43:20 -0700343
344static int fast_spi_flash_ctrlr_setup(const struct spi_slave *dev)
345{
346 if (dev->cs != 0) {
347 printk(BIOS_ERR, "%s: Invalid CS for fast SPI bus=0x%x,cs=0x%x!\n",
348 __func__, dev->bus, dev->cs);
349 return -1;
350 }
351
352 return 0;
353}
354
Aaron Durbin2b96f422017-12-14 15:32:37 -0700355#define SPI_FPR_SHIFT 12
356#define SPI_FPR_MASK 0x7fff
357#define SPI_FPR_BASE_SHIFT 0
358#define SPI_FPR_LIMIT_SHIFT 16
359#define SPI_FPR_RPE (1 << 15) /* Read Protect */
360#define SPI_FPR_WPE (1 << 31) /* Write Protect */
361#define SPI_FPR(base, limit) \
362 (((((limit) >> SPI_FPR_SHIFT) & SPI_FPR_MASK) << SPI_FPR_LIMIT_SHIFT) |\
363 ((((base) >> SPI_FPR_SHIFT) & SPI_FPR_MASK) << SPI_FPR_BASE_SHIFT))
364
365/*
366 * Protect range of SPI flash defined by [start, start+size-1] using Flash
367 * Protected Range (FPR) register if available.
368 */
369static int fast_spi_flash_protect(const struct spi_flash *flash,
Rizwan Qureshif9f50932018-12-31 15:19:16 +0530370 const struct region *region,
371 const enum ctrlr_prot_type type)
Aaron Durbin2b96f422017-12-14 15:32:37 -0700372{
373 u32 start = region_offset(region);
374 u32 end = start + region_sz(region) - 1;
375 u32 reg;
Rizwan Qureshif9f50932018-12-31 15:19:16 +0530376 u32 protect_mask = 0;
Aaron Durbin2b96f422017-12-14 15:32:37 -0700377 int fpr;
378 uintptr_t fpr_base;
379 BOILERPLATE_CREATE_CTX(ctx);
380
381 fpr_base = ctx->mmio_base + SPIBAR_FPR_BASE;
382
383 /* Find first empty FPR */
384 for (fpr = 0; fpr < SPIBAR_FPR_MAX; fpr++) {
385 reg = read32((void *)fpr_base);
386 if (reg == 0)
387 break;
388 fpr_base += sizeof(uint32_t);
389 }
390
391 if (fpr >= SPIBAR_FPR_MAX) {
392 printk(BIOS_ERR, "ERROR: No SPI FPR free!\n");
393 return -1;
394 }
395
Rizwan Qureshif9f50932018-12-31 15:19:16 +0530396 switch (type) {
397 case WRITE_PROTECT:
398 protect_mask |= SPI_FPR_WPE;
399 break;
400 case READ_PROTECT:
401 protect_mask |= SPI_FPR_RPE;
402 break;
403 case READ_WRITE_PROTECT:
404 protect_mask |= (SPI_FPR_RPE | SPI_FPR_WPE);
405 break;
406 default:
407 printk(BIOS_ERR, "ERROR: Seeking invalid protection!\n");
408 return -1;
409 }
410
Aaron Durbin2b96f422017-12-14 15:32:37 -0700411 /* Set protected range base and limit */
Rizwan Qureshif9f50932018-12-31 15:19:16 +0530412 reg = SPI_FPR(start, end) | protect_mask;
Aaron Durbin2b96f422017-12-14 15:32:37 -0700413
414 /* Set the FPR register and verify it is protected */
415 write32((void *)fpr_base, reg);
416 reg = read32((void *)fpr_base);
Rizwan Qureshif9f50932018-12-31 15:19:16 +0530417 if (!(reg & protect_mask)) {
Aaron Durbin2b96f422017-12-14 15:32:37 -0700418 printk(BIOS_ERR, "ERROR: Unable to set SPI FPR %d\n", fpr);
419 return -1;
420 }
421
422 printk(BIOS_INFO, "%s: FPR %d is enabled for range 0x%08x-0x%08x\n",
423 __func__, fpr, start, end);
424 return 0;
425}
426
Furquan Shaikhf1db5fd2017-05-02 19:43:20 -0700427const struct spi_ctrlr fast_spi_flash_ctrlr = {
428 .setup = fast_spi_flash_ctrlr_setup,
Furquan Shaikhde705fa2017-04-19 19:27:28 -0700429 .max_xfer_size = SPI_CTRLR_DEFAULT_MAX_XFER_SIZE,
Furquan Shaikha1491572017-05-17 19:14:06 -0700430 .flash_probe = fast_spi_flash_probe,
Aaron Durbin2b96f422017-12-14 15:32:37 -0700431 .flash_protect = fast_spi_flash_protect,
Furquan Shaikhf1db5fd2017-05-02 19:43:20 -0700432};