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