]> Git Repo - J-u-boot.git/blame - drivers/spi/ich.c
common: Drop log.h from common header
[J-u-boot.git] / drivers / spi / ich.c
CommitLineData
83d290c5 1// SPDX-License-Identifier: GPL-2.0+
1853030e
SG
2/*
3 * Copyright (c) 2011-12 The Chromium OS Authors.
4 *
1853030e
SG
5 * This file is derived from the flashrom project.
6 */
9eb4339b 7
a550662c
SG
8#define LOG_CATEGORY UCLASS_SPI
9
1853030e 10#include <common.h>
52f24238 11#include <bootstage.h>
b47aa26e 12#include <div64.h>
ba457562 13#include <dm.h>
0d3ee3e1 14#include <dt-structs.h>
5093badb 15#include <errno.h>
f7ae49fc 16#include <log.h>
1853030e 17#include <malloc.h>
f2b85ab5 18#include <pch.h>
1853030e
SG
19#include <pci.h>
20#include <pci_ids.h>
f2b85ab5 21#include <spi.h>
1facebd1 22#include <spi_flash.h>
0709ddb6 23#include <spi-mem.h>
636555a4 24#include <spl.h>
1facebd1 25#include <asm/fast_spi.h>
b47aa26e 26#include <asm/io.h>
636555a4
SG
27#include <asm/mtrr.h>
28#include <linux/sizes.h>
1853030e
SG
29
30#include "ich.h"
31
fffe25db
SG
32#ifdef DEBUG_TRACE
33#define debug_trace(fmt, args...) debug(fmt, ##args)
34#else
35#define debug_trace(x, args...)
36#endif
37
75214b05 38struct ich_spi_platdata {
0d3ee3e1
SG
39#if CONFIG_IS_ENABLED(OF_PLATDATA)
40 struct dtd_intel_fast_spi dtplat;
41#endif
75214b05
SG
42 enum ich_version ich_version; /* Controller version, 7 or 9 */
43 bool lockdown; /* lock down controller settings? */
44 ulong mmio_base; /* Base of MMIO registers */
0d3ee3e1 45 pci_dev_t bdf; /* PCI address used by of-platdata */
1facebd1 46 bool hwseq; /* Use hardware sequencing (not s/w) */
75214b05
SG
47};
48
ba457562 49static u8 ich_readb(struct ich_spi_priv *priv, int reg)
1853030e 50{
ba457562 51 u8 value = readb(priv->base + reg);
1853030e 52
fffe25db 53 debug_trace("read %2.2x from %4.4x\n", value, reg);
1853030e
SG
54
55 return value;
56}
57
ba457562 58static u16 ich_readw(struct ich_spi_priv *priv, int reg)
1853030e 59{
ba457562 60 u16 value = readw(priv->base + reg);
1853030e 61
fffe25db 62 debug_trace("read %4.4x from %4.4x\n", value, reg);
1853030e
SG
63
64 return value;
65}
66
ba457562 67static u32 ich_readl(struct ich_spi_priv *priv, int reg)
1853030e 68{
ba457562 69 u32 value = readl(priv->base + reg);
1853030e 70
fffe25db 71 debug_trace("read %8.8x from %4.4x\n", value, reg);
1853030e
SG
72
73 return value;
74}
75
ba457562 76static void ich_writeb(struct ich_spi_priv *priv, u8 value, int reg)
1853030e 77{
ba457562 78 writeb(value, priv->base + reg);
fffe25db 79 debug_trace("wrote %2.2x to %4.4x\n", value, reg);
1853030e
SG
80}
81
ba457562 82static void ich_writew(struct ich_spi_priv *priv, u16 value, int reg)
1853030e 83{
ba457562 84 writew(value, priv->base + reg);
fffe25db 85 debug_trace("wrote %4.4x to %4.4x\n", value, reg);
1853030e
SG
86}
87
ba457562 88static void ich_writel(struct ich_spi_priv *priv, u32 value, int reg)
1853030e 89{
ba457562 90 writel(value, priv->base + reg);
fffe25db 91 debug_trace("wrote %8.8x to %4.4x\n", value, reg);
1853030e
SG
92}
93
ba457562
SG
94static void write_reg(struct ich_spi_priv *priv, const void *value,
95 int dest_reg, uint32_t size)
1853030e 96{
ba457562 97 memcpy_toio(priv->base + dest_reg, value, size);
1853030e
SG
98}
99
ba457562
SG
100static void read_reg(struct ich_spi_priv *priv, int src_reg, void *value,
101 uint32_t size)
1853030e 102{
ba457562 103 memcpy_fromio(value, priv->base + src_reg, size);
1853030e
SG
104}
105
ba457562 106static void ich_set_bbar(struct ich_spi_priv *ctlr, uint32_t minaddr)
1853030e
SG
107{
108 const uint32_t bbar_mask = 0x00ffff00;
109 uint32_t ichspi_bbar;
110
3937df3d
SG
111 if (ctlr->bbar) {
112 minaddr &= bbar_mask;
113 ichspi_bbar = ich_readl(ctlr, ctlr->bbar) & ~bbar_mask;
114 ichspi_bbar |= minaddr;
115 ich_writel(ctlr, ichspi_bbar, ctlr->bbar);
116 }
1853030e
SG
117}
118
1853030e 119/* @return 1 if the SPI flash supports the 33MHz speed */
a550662c 120static bool ich9_can_do_33mhz(struct udevice *dev)
1853030e 121{
17e75449 122 struct ich_spi_priv *priv = dev_get_priv(dev);
1853030e
SG
123 u32 fdod, speed;
124
636555a4
SG
125 if (!CONFIG_IS_ENABLED(PCI))
126 return false;
1853030e 127 /* Observe SPI Descriptor Component Section 0 */
17e75449 128 dm_pci_write_config32(priv->pch, 0xb0, 0x1000);
1853030e
SG
129
130 /* Extract the Write/Erase SPI Frequency from descriptor */
17e75449 131 dm_pci_read_config32(priv->pch, 0xb4, &fdod);
1853030e
SG
132
133 /* Bits 23:21 have the fast read clock frequency, 0=20MHz, 1=33MHz */
134 speed = (fdod >> 21) & 7;
135
136 return speed == 1;
137}
138
ab201074
BM
139static void spi_lock_down(struct ich_spi_platdata *plat, void *sbase)
140{
141 if (plat->ich_version == ICHV_7) {
142 struct ich7_spi_regs *ich7_spi = sbase;
143
144 setbits_le16(&ich7_spi->spis, SPIS_LOCK);
145 } else if (plat->ich_version == ICHV_9) {
146 struct ich9_spi_regs *ich9_spi = sbase;
147
148 setbits_le16(&ich9_spi->hsfs, HSFS_FLOCKDN);
149 }
150}
151
3e791416
BM
152static bool spi_lock_status(struct ich_spi_platdata *plat, void *sbase)
153{
154 int lock = 0;
155
156 if (plat->ich_version == ICHV_7) {
157 struct ich7_spi_regs *ich7_spi = sbase;
158
159 lock = readw(&ich7_spi->spis) & SPIS_LOCK;
160 } else if (plat->ich_version == ICHV_9) {
161 struct ich9_spi_regs *ich9_spi = sbase;
162
163 lock = readw(&ich9_spi->hsfs) & HSFS_FLOCKDN;
164 }
165
166 return lock != 0;
167}
168
3e791416
BM
169static int spi_setup_opcode(struct ich_spi_priv *ctlr, struct spi_trans *trans,
170 bool lock)
1853030e
SG
171{
172 uint16_t optypes;
ba457562 173 uint8_t opmenu[ctlr->menubytes];
1853030e 174
3e791416 175 if (!lock) {
1853030e 176 /* The lock is off, so just use index 0. */
ba457562
SG
177 ich_writeb(ctlr, trans->opcode, ctlr->opmenu);
178 optypes = ich_readw(ctlr, ctlr->optype);
1853030e 179 optypes = (optypes & 0xfffc) | (trans->type & 0x3);
ba457562 180 ich_writew(ctlr, optypes, ctlr->optype);
1853030e
SG
181 return 0;
182 } else {
183 /* The lock is on. See if what we need is on the menu. */
184 uint8_t optype;
185 uint16_t opcode_index;
186
187 /* Write Enable is handled as atomic prefix */
188 if (trans->opcode == SPI_OPCODE_WREN)
189 return 0;
190
ba457562
SG
191 read_reg(ctlr, ctlr->opmenu, opmenu, sizeof(opmenu));
192 for (opcode_index = 0; opcode_index < ctlr->menubytes;
1853030e
SG
193 opcode_index++) {
194 if (opmenu[opcode_index] == trans->opcode)
195 break;
196 }
197
ba457562 198 if (opcode_index == ctlr->menubytes) {
a550662c 199 debug("ICH SPI: Opcode %x not found\n", trans->opcode);
ba457562 200 return -EINVAL;
1853030e
SG
201 }
202
ba457562 203 optypes = ich_readw(ctlr, ctlr->optype);
1853030e 204 optype = (optypes >> (opcode_index * 2)) & 0x3;
0709ddb6 205
1853030e 206 if (optype != trans->type) {
a550662c
SG
207 debug("ICH SPI: Transaction doesn't fit type %d\n",
208 optype);
ba457562 209 return -ENOSPC;
1853030e
SG
210 }
211 return opcode_index;
212 }
213}
214
1853030e
SG
215/*
216 * Wait for up to 6s til status register bit(s) turn 1 (in case wait_til_set
472d5460 217 * below is true) or 0. In case the wait was for the bit(s) to set - write
1853030e
SG
218 * those bits back, which would cause resetting them.
219 *
220 * Return the last read status value on success or -1 on failure.
221 */
ba457562
SG
222static int ich_status_poll(struct ich_spi_priv *ctlr, u16 bitmask,
223 int wait_til_set)
1853030e
SG
224{
225 int timeout = 600000; /* This will result in 6s */
226 u16 status = 0;
227
228 while (timeout--) {
ba457562 229 status = ich_readw(ctlr, ctlr->status);
1853030e 230 if (wait_til_set ^ ((status & bitmask) == 0)) {
ba457562
SG
231 if (wait_til_set) {
232 ich_writew(ctlr, status & bitmask,
233 ctlr->status);
234 }
1853030e
SG
235 return status;
236 }
237 udelay(10);
238 }
a550662c
SG
239 debug("ICH SPI: SCIP timeout, read %x, expected %x, wts %x %x\n",
240 status, bitmask, wait_til_set, status & bitmask);
1853030e 241
ba457562 242 return -ETIMEDOUT;
1853030e
SG
243}
244
0709ddb6 245static void ich_spi_config_opcode(struct udevice *dev)
b42711f9
BM
246{
247 struct ich_spi_priv *ctlr = dev_get_priv(dev);
248
249 /*
250 * PREOP, OPTYPE, OPMENU1/OPMENU2 registers can be locked down
251 * to prevent accidental or intentional writes. Before they get
252 * locked down, these registers should be initialized properly.
253 */
254 ich_writew(ctlr, SPI_OPPREFIX, ctlr->preop);
255 ich_writew(ctlr, SPI_OPTYPE, ctlr->optype);
256 ich_writel(ctlr, SPI_OPMENU_LOWER, ctlr->opmenu);
257 ich_writel(ctlr, SPI_OPMENU_UPPER, ctlr->opmenu + sizeof(u32));
258}
259
1facebd1
SG
260static int ich_spi_exec_op_swseq(struct spi_slave *slave,
261 const struct spi_mem_op *op)
1853030e 262{
0709ddb6 263 struct udevice *bus = dev_get_parent(slave->dev);
e1e332c8 264 struct ich_spi_platdata *plat = dev_get_platdata(bus);
ba457562 265 struct ich_spi_priv *ctlr = dev_get_priv(bus);
1853030e
SG
266 uint16_t control;
267 int16_t opcode_index;
268 int with_address;
269 int status;
ba457562 270 struct spi_trans *trans = &ctlr->trans;
3e791416 271 bool lock = spi_lock_status(plat, ctlr->base);
0709ddb6 272 int ret = 0;
1853030e 273
0709ddb6
BM
274 trans->in = NULL;
275 trans->out = NULL;
276 trans->type = 0xFF;
1853030e 277
0709ddb6
BM
278 if (op->data.nbytes) {
279 if (op->data.dir == SPI_MEM_DATA_IN) {
280 trans->in = op->data.buf.in;
281 trans->bytesin = op->data.nbytes;
282 } else {
283 trans->out = op->data.buf.out;
284 trans->bytesout = op->data.nbytes;
1853030e 285 }
1853030e
SG
286 }
287
0709ddb6
BM
288 if (trans->opcode != op->cmd.opcode)
289 trans->opcode = op->cmd.opcode;
1853030e 290
0709ddb6
BM
291 if (lock && trans->opcode == SPI_OPCODE_WRDIS)
292 return 0;
1853030e 293
0709ddb6
BM
294 if (trans->opcode == SPI_OPCODE_WREN) {
295 /*
296 * Treat Write Enable as Atomic Pre-Op if possible
297 * in order to prevent the Management Engine from
298 * issuing a transaction between WREN and DATA.
299 */
300 if (!lock)
301 ich_writew(ctlr, trans->opcode, ctlr->preop);
302 return 0;
1853030e
SG
303 }
304
ba457562
SG
305 ret = ich_status_poll(ctlr, SPIS_SCIP, 0);
306 if (ret < 0)
307 return ret;
1853030e 308
6e670b5c 309 if (plat->ich_version == ICHV_7)
e1e332c8
SG
310 ich_writew(ctlr, SPIS_CDS | SPIS_FCERR, ctlr->status);
311 else
312 ich_writeb(ctlr, SPIS_CDS | SPIS_FCERR, ctlr->status);
1853030e 313
0709ddb6
BM
314 /* Try to guess spi transaction type */
315 if (op->data.dir == SPI_MEM_DATA_OUT) {
316 if (op->addr.nbytes)
317 trans->type = SPI_OPCODE_TYPE_WRITE_WITH_ADDRESS;
318 else
319 trans->type = SPI_OPCODE_TYPE_WRITE_NO_ADDRESS;
320 } else {
321 if (op->addr.nbytes)
322 trans->type = SPI_OPCODE_TYPE_READ_WITH_ADDRESS;
323 else
324 trans->type = SPI_OPCODE_TYPE_READ_NO_ADDRESS;
325 }
326 /* Special erase case handling */
327 if (op->addr.nbytes && !op->data.buswidth)
328 trans->type = SPI_OPCODE_TYPE_WRITE_WITH_ADDRESS;
329
3e791416 330 opcode_index = spi_setup_opcode(ctlr, trans, lock);
1853030e 331 if (opcode_index < 0)
ba457562 332 return -EINVAL;
1853030e 333
0709ddb6
BM
334 if (op->addr.nbytes) {
335 trans->offset = op->addr.val;
336 with_address = 1;
1853030e
SG
337 }
338
ba457562 339 if (ctlr->speed && ctlr->max_speed >= 33000000) {
1853030e
SG
340 int byte;
341
ba457562
SG
342 byte = ich_readb(ctlr, ctlr->speed);
343 if (ctlr->cur_speed >= 33000000)
1853030e
SG
344 byte |= SSFC_SCF_33MHZ;
345 else
346 byte &= ~SSFC_SCF_33MHZ;
ba457562 347 ich_writeb(ctlr, byte, ctlr->speed);
1853030e
SG
348 }
349
1853030e 350 /* Preset control fields */
1853030e
SG
351 control = SPIC_SCGO | ((opcode_index & 0x07) << 4);
352
353 /* Issue atomic preop cycle if needed */
ba457562 354 if (ich_readw(ctlr, ctlr->preop))
1853030e
SG
355 control |= SPIC_ACS;
356
357 if (!trans->bytesout && !trans->bytesin) {
358 /* SPI addresses are 24 bit only */
ba457562
SG
359 if (with_address) {
360 ich_writel(ctlr, trans->offset & 0x00FFFFFF,
361 ctlr->addr);
362 }
1853030e
SG
363 /*
364 * This is a 'no data' command (like Write Enable), its
365 * bitesout size was 1, decremented to zero while executing
366 * spi_setup_opcode() above. Tell the chip to send the
367 * command.
368 */
ba457562 369 ich_writew(ctlr, control, ctlr->control);
1853030e
SG
370
371 /* wait for the result */
ba457562
SG
372 status = ich_status_poll(ctlr, SPIS_CDS | SPIS_FCERR, 1);
373 if (status < 0)
374 return status;
1853030e
SG
375
376 if (status & SPIS_FCERR) {
377 debug("ICH SPI: Command transaction error\n");
ba457562 378 return -EIO;
1853030e
SG
379 }
380
381 return 0;
382 }
383
1853030e
SG
384 while (trans->bytesout || trans->bytesin) {
385 uint32_t data_length;
1853030e
SG
386
387 /* SPI addresses are 24 bit only */
ba457562 388 ich_writel(ctlr, trans->offset & 0x00FFFFFF, ctlr->addr);
1853030e
SG
389
390 if (trans->bytesout)
ba457562 391 data_length = min(trans->bytesout, ctlr->databytes);
1853030e 392 else
ba457562 393 data_length = min(trans->bytesin, ctlr->databytes);
1853030e
SG
394
395 /* Program data into FDATA0 to N */
396 if (trans->bytesout) {
ba457562 397 write_reg(ctlr, trans->out, ctlr->data, data_length);
0709ddb6 398 trans->bytesout -= data_length;
1853030e
SG
399 }
400
401 /* Add proper control fields' values */
ba457562 402 control &= ~((ctlr->databytes - 1) << 8);
1853030e
SG
403 control |= SPIC_DS;
404 control |= (data_length - 1) << 8;
405
406 /* write it */
ba457562 407 ich_writew(ctlr, control, ctlr->control);
1853030e 408
9eb4339b 409 /* Wait for Cycle Done Status or Flash Cycle Error */
ba457562
SG
410 status = ich_status_poll(ctlr, SPIS_CDS | SPIS_FCERR, 1);
411 if (status < 0)
412 return status;
1853030e
SG
413
414 if (status & SPIS_FCERR) {
5d4a757c 415 debug("ICH SPI: Data transaction error %x\n", status);
ba457562 416 return -EIO;
1853030e
SG
417 }
418
419 if (trans->bytesin) {
ba457562 420 read_reg(ctlr, ctlr->data, trans->in, data_length);
0709ddb6 421 trans->bytesin -= data_length;
1853030e
SG
422 }
423 }
424
425 /* Clear atomic preop now that xfer is done */
d2ca80c3
BM
426 if (!lock)
427 ich_writew(ctlr, 0, ctlr->preop);
1853030e
SG
428
429 return 0;
430}
431
1facebd1
SG
432/*
433 * Ensure read/write xfer len is not greater than SPIBAR_FDATA_FIFO_SIZE and
434 * that the operation does not cross page boundary.
435 */
436static uint get_xfer_len(u32 offset, int len, int page_size)
437{
438 uint xfer_len = min(len, SPIBAR_FDATA_FIFO_SIZE);
439 uint bytes_left = ALIGN(offset, page_size) - offset;
440
441 if (bytes_left)
442 xfer_len = min(xfer_len, bytes_left);
443
444 return xfer_len;
445}
446
447/* Fill FDATAn FIFO in preparation for a write transaction */
448static void fill_xfer_fifo(struct fast_spi_regs *regs, const void *data,
449 uint len)
450{
451 memcpy(regs->fdata, data, len);
452}
453
454/* Drain FDATAn FIFO after a read transaction populates data */
455static void drain_xfer_fifo(struct fast_spi_regs *regs, void *dest, uint len)
456{
457 memcpy(dest, regs->fdata, len);
458}
459
460/* Fire up a transfer using the hardware sequencer */
461static void start_hwseq_xfer(struct fast_spi_regs *regs, uint hsfsts_cycle,
462 uint offset, uint len)
463{
464 /* Make sure all W1C status bits get cleared */
465 u32 hsfsts;
466
467 hsfsts = readl(&regs->hsfsts_ctl);
468 hsfsts &= ~(HSFSTS_FCYCLE_MASK | HSFSTS_FDBC_MASK);
469 hsfsts |= HSFSTS_AEL | HSFSTS_FCERR | HSFSTS_FDONE;
470
471 /* Set up transaction parameters */
472 hsfsts |= hsfsts_cycle << HSFSTS_FCYCLE_SHIFT;
473 hsfsts |= ((len - 1) << HSFSTS_FDBC_SHIFT) & HSFSTS_FDBC_MASK;
474 hsfsts |= HSFSTS_FGO;
475
476 writel(offset, &regs->faddr);
477 writel(hsfsts, &regs->hsfsts_ctl);
478}
479
480static int wait_for_hwseq_xfer(struct fast_spi_regs *regs, uint offset)
481{
482 ulong start;
483 u32 hsfsts;
484
485 start = get_timer(0);
486 do {
487 hsfsts = readl(&regs->hsfsts_ctl);
488 if (hsfsts & HSFSTS_FCERR) {
489 debug("SPI transaction error at offset %x HSFSTS = %08x\n",
490 offset, hsfsts);
491 return -EIO;
492 }
493 if (hsfsts & HSFSTS_AEL)
494 return -EPERM;
495
496 if (hsfsts & HSFSTS_FDONE)
497 return 0;
498 } while (get_timer(start) < SPIBAR_HWSEQ_XFER_TIMEOUT_MS);
499
500 debug("SPI transaction timeout at offset %x HSFSTS = %08x, timer %d\n",
501 offset, hsfsts, (uint)get_timer(start));
502
503 return -ETIMEDOUT;
504}
505
506/**
507 * exec_sync_hwseq_xfer() - Execute flash transfer by hardware sequencing
508 *
509 * This waits until complete or timeout
510 *
511 * @regs: SPI registers
512 * @hsfsts_cycle: Cycle type (enum hsfsts_cycle_t)
513 * @offset: Offset to access
514 * @len: Number of bytes to transfer (can be 0)
515 * @return 0 if OK, -EIO on flash-cycle error (FCERR), -EPERM on access error
516 * (AEL), -ETIMEDOUT on timeout
517 */
518static int exec_sync_hwseq_xfer(struct fast_spi_regs *regs, uint hsfsts_cycle,
519 uint offset, uint len)
520{
521 start_hwseq_xfer(regs, hsfsts_cycle, offset, len);
522
523 return wait_for_hwseq_xfer(regs, offset);
524}
525
526static int ich_spi_exec_op_hwseq(struct spi_slave *slave,
527 const struct spi_mem_op *op)
528{
529 struct spi_flash *flash = dev_get_uclass_priv(slave->dev);
530 struct udevice *bus = dev_get_parent(slave->dev);
531 struct ich_spi_priv *priv = dev_get_priv(bus);
532 struct fast_spi_regs *regs = priv->base;
533 uint page_size;
534 uint offset;
535 int cycle;
536 uint len;
537 bool out;
538 int ret;
539 u8 *buf;
540
541 offset = op->addr.val;
542 len = op->data.nbytes;
543
544 switch (op->cmd.opcode) {
545 case SPINOR_OP_RDID:
546 cycle = HSFSTS_CYCLE_RDID;
547 break;
548 case SPINOR_OP_READ_FAST:
549 cycle = HSFSTS_CYCLE_READ;
550 break;
551 case SPINOR_OP_PP:
552 cycle = HSFSTS_CYCLE_WRITE;
553 break;
554 case SPINOR_OP_WREN:
555 /* Nothing needs to be done */
556 return 0;
557 case SPINOR_OP_WRSR:
558 cycle = HSFSTS_CYCLE_WR_STATUS;
559 break;
560 case SPINOR_OP_RDSR:
561 cycle = HSFSTS_CYCLE_RD_STATUS;
562 break;
563 case SPINOR_OP_WRDI:
564 return 0; /* ignore */
565 case SPINOR_OP_BE_4K:
566 cycle = HSFSTS_CYCLE_4K_ERASE;
5e579cc0
WW
567 ret = exec_sync_hwseq_xfer(regs, cycle, offset, 0);
568 return ret;
1facebd1
SG
569 default:
570 debug("Unknown cycle %x\n", op->cmd.opcode);
571 return -EINVAL;
572 };
573
574 out = op->data.dir == SPI_MEM_DATA_OUT;
575 buf = out ? (u8 *)op->data.buf.out : op->data.buf.in;
576 page_size = flash->page_size ? : 256;
577
578 while (len) {
579 uint xfer_len = get_xfer_len(offset, len, page_size);
580
581 if (out)
582 fill_xfer_fifo(regs, buf, xfer_len);
583
584 ret = exec_sync_hwseq_xfer(regs, cycle, offset, xfer_len);
585 if (ret)
586 return ret;
587
588 if (!out)
589 drain_xfer_fifo(regs, buf, xfer_len);
590
591 offset += xfer_len;
592 buf += xfer_len;
593 len -= xfer_len;
594 }
595
596 return 0;
597}
598
599static int ich_spi_exec_op(struct spi_slave *slave, const struct spi_mem_op *op)
600{
601 struct udevice *bus = dev_get_parent(slave->dev);
602 struct ich_spi_platdata *plat = dev_get_platdata(bus);
603 int ret;
604
605 bootstage_start(BOOTSTAGE_ID_ACCUM_SPI, "fast_spi");
606 if (plat->hwseq)
607 ret = ich_spi_exec_op_hwseq(slave, op);
608 else
609 ret = ich_spi_exec_op_swseq(slave, op);
610 bootstage_accum(BOOTSTAGE_ID_ACCUM_SPI);
611
612 return ret;
613}
614
92842147
SG
615static int ich_get_mmap_bus(struct udevice *bus, ulong *map_basep,
616 uint *map_sizep, uint *offsetp)
617{
618 pci_dev_t spi_bdf;
619
620#if !CONFIG_IS_ENABLED(OF_PLATDATA)
621 struct pci_child_platdata *pplat = dev_get_parent_platdata(bus);
622
623 spi_bdf = pplat->devfn;
624#else
625 struct ich_spi_platdata *plat = dev_get_platdata(bus);
626
627 /*
628 * We cannot rely on plat->bdf being set up yet since this method can
629 * be called before the device is probed. Use the of-platdata directly
630 * instead.
631 */
632 spi_bdf = pci_ofplat_get_devfn(plat->dtplat.reg[0]);
633#endif
634
635 return fast_spi_get_bios_mmap(spi_bdf, map_basep, map_sizep, offsetp);
636}
637
638static int ich_get_mmap(struct udevice *dev, ulong *map_basep, uint *map_sizep,
639 uint *offsetp)
640{
641 struct udevice *bus = dev_get_parent(dev);
642
643 return ich_get_mmap_bus(bus, map_basep, map_sizep, offsetp);
644}
645
0709ddb6
BM
646static int ich_spi_adjust_size(struct spi_slave *slave, struct spi_mem_op *op)
647{
648 unsigned int page_offset;
649 int addr = op->addr.val;
650 unsigned int byte_count = op->data.nbytes;
651
652 if (hweight32(ICH_BOUNDARY) == 1) {
653 page_offset = addr & (ICH_BOUNDARY - 1);
654 } else {
655 u64 aux = addr;
656
657 page_offset = do_div(aux, ICH_BOUNDARY);
658 }
659
43c145b8
SG
660 if (op->data.dir == SPI_MEM_DATA_IN) {
661 if (slave->max_read_size) {
662 op->data.nbytes = min(ICH_BOUNDARY - page_offset,
663 slave->max_read_size);
664 }
0709ddb6
BM
665 } else if (slave->max_write_size) {
666 op->data.nbytes = min(ICH_BOUNDARY - page_offset,
667 slave->max_write_size);
668 }
669
670 op->data.nbytes = min(op->data.nbytes, byte_count);
671
672 return 0;
673}
674
17e75449
SG
675static int ich_protect_lockdown(struct udevice *dev)
676{
677 struct ich_spi_platdata *plat = dev_get_platdata(dev);
678 struct ich_spi_priv *priv = dev_get_priv(dev);
679 int ret = -ENOSYS;
680
681 /* Disable the BIOS write protect so write commands are allowed */
682 if (priv->pch)
683 ret = pch_set_spi_protect(priv->pch, false);
684 if (ret == -ENOSYS) {
685 u8 bios_cntl;
686
687 bios_cntl = ich_readb(priv, priv->bcr);
688 bios_cntl &= ~BIT(5); /* clear Enable InSMM_STS (EISS) */
689 bios_cntl |= 1; /* Write Protect Disable (WPD) */
690 ich_writeb(priv, bios_cntl, priv->bcr);
691 } else if (ret) {
692 debug("%s: Failed to disable write-protect: err=%d\n",
693 __func__, ret);
694 return ret;
695 }
696
697 /* Lock down SPI controller settings if required */
698 if (plat->lockdown) {
699 ich_spi_config_opcode(dev);
700 spi_lock_down(plat, priv->base);
701 }
702
703 return 0;
704}
705
674990c0
SG
706static int ich_init_controller(struct udevice *dev,
707 struct ich_spi_platdata *plat,
708 struct ich_spi_priv *ctlr)
709{
636555a4
SG
710 if (spl_phase() == PHASE_TPL) {
711 struct ich_spi_platdata *plat = dev_get_platdata(dev);
712 int ret;
713
714 ret = fast_spi_early_init(plat->bdf, plat->mmio_base);
715 if (ret)
716 return ret;
717 }
718
75214b05 719 ctlr->base = (void *)plat->mmio_base;
674990c0 720 if (plat->ich_version == ICHV_7) {
75214b05 721 struct ich7_spi_regs *ich7_spi = ctlr->base;
674990c0
SG
722
723 ctlr->opmenu = offsetof(struct ich7_spi_regs, opmenu);
724 ctlr->menubytes = sizeof(ich7_spi->opmenu);
725 ctlr->optype = offsetof(struct ich7_spi_regs, optype);
726 ctlr->addr = offsetof(struct ich7_spi_regs, spia);
727 ctlr->data = offsetof(struct ich7_spi_regs, spid);
728 ctlr->databytes = sizeof(ich7_spi->spid);
729 ctlr->status = offsetof(struct ich7_spi_regs, spis);
730 ctlr->control = offsetof(struct ich7_spi_regs, spic);
731 ctlr->bbar = offsetof(struct ich7_spi_regs, bbar);
732 ctlr->preop = offsetof(struct ich7_spi_regs, preop);
674990c0 733 } else if (plat->ich_version == ICHV_9) {
75214b05 734 struct ich9_spi_regs *ich9_spi = ctlr->base;
674990c0
SG
735
736 ctlr->opmenu = offsetof(struct ich9_spi_regs, opmenu);
737 ctlr->menubytes = sizeof(ich9_spi->opmenu);
738 ctlr->optype = offsetof(struct ich9_spi_regs, optype);
739 ctlr->addr = offsetof(struct ich9_spi_regs, faddr);
740 ctlr->data = offsetof(struct ich9_spi_regs, fdata);
741 ctlr->databytes = sizeof(ich9_spi->fdata);
742 ctlr->status = offsetof(struct ich9_spi_regs, ssfs);
743 ctlr->control = offsetof(struct ich9_spi_regs, ssfc);
744 ctlr->speed = ctlr->control + 2;
745 ctlr->bbar = offsetof(struct ich9_spi_regs, bbar);
746 ctlr->preop = offsetof(struct ich9_spi_regs, preop);
747 ctlr->bcr = offsetof(struct ich9_spi_regs, bcr);
748 ctlr->pr = &ich9_spi->pr[0];
3937df3d 749 } else if (plat->ich_version == ICHV_APL) {
674990c0
SG
750 } else {
751 debug("ICH SPI: Unrecognised ICH version %d\n",
752 plat->ich_version);
753 return -EINVAL;
754 }
755
756 /* Work out the maximum speed we can support */
757 ctlr->max_speed = 20000000;
758 if (plat->ich_version == ICHV_9 && ich9_can_do_33mhz(dev))
759 ctlr->max_speed = 33000000;
75214b05
SG
760 debug("ICH SPI: Version ID %d detected at %lx, speed %ld\n",
761 plat->ich_version, plat->mmio_base, ctlr->max_speed);
674990c0
SG
762
763 ich_set_bbar(ctlr, 0);
764
765 return 0;
766}
767
636555a4
SG
768static int ich_cache_bios_region(struct udevice *dev)
769{
770 ulong map_base;
771 uint map_size;
772 uint offset;
773 ulong base;
774 int ret;
775
776 ret = ich_get_mmap_bus(dev, &map_base, &map_size, &offset);
777 if (ret)
778 return ret;
779
780 /* Don't use WRBACK since we are not supposed to write to SPI flash */
781 base = SZ_4G - map_size;
782 mtrr_set_next_var(MTRR_TYPE_WRPROT, base, map_size);
783 log_debug("BIOS cache base=%lx, size=%x\n", base, (uint)map_size);
784
785 return 0;
786}
787
f2b85ab5 788static int ich_spi_probe(struct udevice *dev)
ba457562 789{
f2b85ab5
SG
790 struct ich_spi_platdata *plat = dev_get_platdata(dev);
791 struct ich_spi_priv *priv = dev_get_priv(dev);
ba457562
SG
792 int ret;
793
f2b85ab5 794 ret = ich_init_controller(dev, plat, priv);
ba457562
SG
795 if (ret)
796 return ret;
ba457562 797
636555a4
SG
798 if (spl_phase() == PHASE_TPL) {
799 /* Cache the BIOS to speed things up */
800 ret = ich_cache_bios_region(dev);
801 if (ret)
802 return ret;
803 } else {
804 ret = ich_protect_lockdown(dev);
805 if (ret)
806 return ret;
807 }
ba457562
SG
808 priv->cur_speed = priv->max_speed;
809
810 return 0;
811}
812
4759dffe
SR
813static int ich_spi_remove(struct udevice *bus)
814{
4759dffe
SR
815 /*
816 * Configure SPI controller so that the Linux MTD driver can fully
817 * access the SPI NOR chip
818 */
b42711f9 819 ich_spi_config_opcode(bus);
4759dffe
SR
820
821 return 0;
822}
823
ba457562
SG
824static int ich_spi_set_speed(struct udevice *bus, uint speed)
825{
826 struct ich_spi_priv *priv = dev_get_priv(bus);
827
828 priv->cur_speed = speed;
829
830 return 0;
831}
832
833static int ich_spi_set_mode(struct udevice *bus, uint mode)
834{
835 debug("%s: mode=%d\n", __func__, mode);
836
837 return 0;
838}
839
840static int ich_spi_child_pre_probe(struct udevice *dev)
841{
842 struct udevice *bus = dev_get_parent(dev);
843 struct ich_spi_platdata *plat = dev_get_platdata(bus);
844 struct ich_spi_priv *priv = dev_get_priv(bus);
bcbe3d15 845 struct spi_slave *slave = dev_get_parent_priv(dev);
ba457562
SG
846
847 /*
848 * Yes this controller can only write a small number of bytes at
1facebd1
SG
849 * once! The limit is typically 64 bytes. For hardware sequencing a
850 * a loop is used to get around this.
ba457562 851 */
1facebd1
SG
852 if (!plat->hwseq)
853 slave->max_write_size = priv->databytes;
ba457562
SG
854 /*
855 * ICH 7 SPI controller only supports array read command
856 * and byte program command for SST flash
857 */
08fe9c29
JT
858 if (plat->ich_version == ICHV_7)
859 slave->mode = SPI_RX_SLOW | SPI_TX_BYTE;
ba457562
SG
860
861 return 0;
862}
863
1f9eb59d
BM
864static int ich_spi_ofdata_to_platdata(struct udevice *dev)
865{
866 struct ich_spi_platdata *plat = dev_get_platdata(dev);
0d3ee3e1
SG
867
868#if !CONFIG_IS_ENABLED(OF_PLATDATA)
17e75449 869 struct ich_spi_priv *priv = dev_get_priv(dev);
1f9eb59d 870
17e75449
SG
871 /* Find a PCH if there is one */
872 uclass_first_device(UCLASS_PCH, &priv->pch);
873 if (!priv->pch)
874 priv->pch = dev_get_parent(dev);
875
702b28a1
SG
876 plat->ich_version = dev_get_driver_data(dev);
877 plat->lockdown = dev_read_bool(dev, "intel,spi-lock-down");
3937df3d
SG
878 if (plat->ich_version == ICHV_APL) {
879 plat->mmio_base = dm_pci_read_bar32(dev, 0);
880 } else {
881 /* SBASE is similar */
882 pch_get_spi_base(priv->pch, &plat->mmio_base);
883 }
1facebd1
SG
884 /*
885 * Use an int so that the property is present in of-platdata even
886 * when false.
887 */
888 plat->hwseq = dev_read_u32_default(dev, "intel,hardware-seq", 0);
0d3ee3e1
SG
889#else
890 plat->ich_version = ICHV_APL;
891 plat->mmio_base = plat->dtplat.early_regs[0];
892 plat->bdf = pci_ofplat_get_devfn(plat->dtplat.reg[0]);
1facebd1 893 plat->hwseq = plat->dtplat.intel_hardware_seq;
0d3ee3e1
SG
894#endif
895 debug("%s: mmio_base=%lx\n", __func__, plat->mmio_base);
75214b05 896
702b28a1 897 return 0;
1f9eb59d
BM
898}
899
0709ddb6
BM
900static const struct spi_controller_mem_ops ich_controller_mem_ops = {
901 .adjust_op_size = ich_spi_adjust_size,
902 .supports_op = NULL,
903 .exec_op = ich_spi_exec_op,
904};
905
ba457562 906static const struct dm_spi_ops ich_spi_ops = {
ccdabd89 907 /* xfer is not supported */
ba457562
SG
908 .set_speed = ich_spi_set_speed,
909 .set_mode = ich_spi_set_mode,
0709ddb6 910 .mem_ops = &ich_controller_mem_ops,
92842147 911 .get_mmap = ich_get_mmap,
ba457562
SG
912 /*
913 * cs_info is not needed, since we require all chip selects to be
914 * in the device tree explicitly
915 */
916};
917
918static const struct udevice_id ich_spi_ids[] = {
702b28a1
SG
919 { .compatible = "intel,ich7-spi", ICHV_7 },
920 { .compatible = "intel,ich9-spi", ICHV_9 },
3937df3d 921 { .compatible = "intel,fast-spi", ICHV_APL },
ba457562
SG
922 { }
923};
924
0d3ee3e1
SG
925U_BOOT_DRIVER(intel_fast_spi) = {
926 .name = "intel_fast_spi",
ba457562
SG
927 .id = UCLASS_SPI,
928 .of_match = ich_spi_ids,
929 .ops = &ich_spi_ops,
1f9eb59d 930 .ofdata_to_platdata = ich_spi_ofdata_to_platdata,
ba457562
SG
931 .platdata_auto_alloc_size = sizeof(struct ich_spi_platdata),
932 .priv_auto_alloc_size = sizeof(struct ich_spi_priv),
933 .child_pre_probe = ich_spi_child_pre_probe,
934 .probe = ich_spi_probe,
4759dffe
SR
935 .remove = ich_spi_remove,
936 .flags = DM_FLAG_OS_PREPARE,
ba457562 937};
This page took 0.503088 seconds and 4 git commands to generate.