2018-05-07 05:58:06 +08:00
|
|
|
// SPDX-License-Identifier: GPL-2.0+
|
2013-03-19 12:58:56 +08:00
|
|
|
/*
|
|
|
|
* Copyright (c) 2011-12 The Chromium OS Authors.
|
|
|
|
*
|
|
|
|
* This file is derived from the flashrom project.
|
|
|
|
*/
|
2016-02-01 17:40:36 +08:00
|
|
|
|
2019-12-07 12:42:41 +08:00
|
|
|
#define LOG_CATEGORY UCLASS_SPI
|
|
|
|
|
2020-05-11 01:40:00 +08:00
|
|
|
#include <bootstage.h>
|
2019-12-07 12:42:40 +08:00
|
|
|
#include <div64.h>
|
2015-03-26 23:29:26 +08:00
|
|
|
#include <dm.h>
|
2019-12-07 12:42:45 +08:00
|
|
|
#include <dt-structs.h>
|
2015-01-28 13:13:43 +08:00
|
|
|
#include <errno.h>
|
2020-05-11 01:40:05 +08:00
|
|
|
#include <log.h>
|
2013-03-19 12:58:56 +08:00
|
|
|
#include <malloc.h>
|
2016-01-19 11:19:21 +08:00
|
|
|
#include <pch.h>
|
2013-03-19 12:58:56 +08:00
|
|
|
#include <pci.h>
|
|
|
|
#include <pci_ids.h>
|
2016-01-19 11:19:21 +08:00
|
|
|
#include <spi.h>
|
2019-12-07 12:42:46 +08:00
|
|
|
#include <spi_flash.h>
|
2019-08-02 14:38:34 +08:00
|
|
|
#include <spi-mem.h>
|
2019-12-07 12:42:48 +08:00
|
|
|
#include <spl.h>
|
2019-12-07 12:42:46 +08:00
|
|
|
#include <asm/fast_spi.h>
|
2019-12-07 12:42:40 +08:00
|
|
|
#include <asm/io.h>
|
2020-05-27 20:58:47 +08:00
|
|
|
#include <dm/uclass-internal.h>
|
2019-12-07 12:42:48 +08:00
|
|
|
#include <asm/mtrr.h>
|
2020-05-11 01:40:13 +08:00
|
|
|
#include <linux/bitops.h>
|
2020-05-11 01:40:11 +08:00
|
|
|
#include <linux/delay.h>
|
2019-12-07 12:42:48 +08:00
|
|
|
#include <linux/sizes.h>
|
2013-03-19 12:58:56 +08:00
|
|
|
|
|
|
|
#include "ich.h"
|
|
|
|
|
2016-01-19 11:19:20 +08:00
|
|
|
#ifdef DEBUG_TRACE
|
|
|
|
#define debug_trace(fmt, args...) debug(fmt, ##args)
|
|
|
|
#else
|
|
|
|
#define debug_trace(x, args...)
|
|
|
|
#endif
|
|
|
|
|
2015-03-26 23:29:26 +08:00
|
|
|
static u8 ich_readb(struct ich_spi_priv *priv, int reg)
|
2013-03-19 12:58:56 +08:00
|
|
|
{
|
2015-03-26 23:29:26 +08:00
|
|
|
u8 value = readb(priv->base + reg);
|
2013-03-19 12:58:56 +08:00
|
|
|
|
2016-01-19 11:19:20 +08:00
|
|
|
debug_trace("read %2.2x from %4.4x\n", value, reg);
|
2013-03-19 12:58:56 +08:00
|
|
|
|
|
|
|
return value;
|
|
|
|
}
|
|
|
|
|
2015-03-26 23:29:26 +08:00
|
|
|
static u16 ich_readw(struct ich_spi_priv *priv, int reg)
|
2013-03-19 12:58:56 +08:00
|
|
|
{
|
2015-03-26 23:29:26 +08:00
|
|
|
u16 value = readw(priv->base + reg);
|
2013-03-19 12:58:56 +08:00
|
|
|
|
2016-01-19 11:19:20 +08:00
|
|
|
debug_trace("read %4.4x from %4.4x\n", value, reg);
|
2013-03-19 12:58:56 +08:00
|
|
|
|
|
|
|
return value;
|
|
|
|
}
|
|
|
|
|
2015-03-26 23:29:26 +08:00
|
|
|
static u32 ich_readl(struct ich_spi_priv *priv, int reg)
|
2013-03-19 12:58:56 +08:00
|
|
|
{
|
2015-03-26 23:29:26 +08:00
|
|
|
u32 value = readl(priv->base + reg);
|
2013-03-19 12:58:56 +08:00
|
|
|
|
2016-01-19 11:19:20 +08:00
|
|
|
debug_trace("read %8.8x from %4.4x\n", value, reg);
|
2013-03-19 12:58:56 +08:00
|
|
|
|
|
|
|
return value;
|
|
|
|
}
|
|
|
|
|
2015-03-26 23:29:26 +08:00
|
|
|
static void ich_writeb(struct ich_spi_priv *priv, u8 value, int reg)
|
2013-03-19 12:58:56 +08:00
|
|
|
{
|
2015-03-26 23:29:26 +08:00
|
|
|
writeb(value, priv->base + reg);
|
2016-01-19 11:19:20 +08:00
|
|
|
debug_trace("wrote %2.2x to %4.4x\n", value, reg);
|
2013-03-19 12:58:56 +08:00
|
|
|
}
|
|
|
|
|
2015-03-26 23:29:26 +08:00
|
|
|
static void ich_writew(struct ich_spi_priv *priv, u16 value, int reg)
|
2013-03-19 12:58:56 +08:00
|
|
|
{
|
2015-03-26 23:29:26 +08:00
|
|
|
writew(value, priv->base + reg);
|
2016-01-19 11:19:20 +08:00
|
|
|
debug_trace("wrote %4.4x to %4.4x\n", value, reg);
|
2013-03-19 12:58:56 +08:00
|
|
|
}
|
|
|
|
|
2015-03-26 23:29:26 +08:00
|
|
|
static void ich_writel(struct ich_spi_priv *priv, u32 value, int reg)
|
2013-03-19 12:58:56 +08:00
|
|
|
{
|
2015-03-26 23:29:26 +08:00
|
|
|
writel(value, priv->base + reg);
|
2016-01-19 11:19:20 +08:00
|
|
|
debug_trace("wrote %8.8x to %4.4x\n", value, reg);
|
2013-03-19 12:58:56 +08:00
|
|
|
}
|
|
|
|
|
2015-03-26 23:29:26 +08:00
|
|
|
static void write_reg(struct ich_spi_priv *priv, const void *value,
|
|
|
|
int dest_reg, uint32_t size)
|
2013-03-19 12:58:56 +08:00
|
|
|
{
|
2015-03-26 23:29:26 +08:00
|
|
|
memcpy_toio(priv->base + dest_reg, value, size);
|
2013-03-19 12:58:56 +08:00
|
|
|
}
|
|
|
|
|
2015-03-26 23:29:26 +08:00
|
|
|
static void read_reg(struct ich_spi_priv *priv, int src_reg, void *value,
|
|
|
|
uint32_t size)
|
2013-03-19 12:58:56 +08:00
|
|
|
{
|
2015-03-26 23:29:26 +08:00
|
|
|
memcpy_fromio(value, priv->base + src_reg, size);
|
2013-03-19 12:58:56 +08:00
|
|
|
}
|
|
|
|
|
2015-03-26 23:29:26 +08:00
|
|
|
static void ich_set_bbar(struct ich_spi_priv *ctlr, uint32_t minaddr)
|
2013-03-19 12:58:56 +08:00
|
|
|
{
|
|
|
|
const uint32_t bbar_mask = 0x00ffff00;
|
|
|
|
uint32_t ichspi_bbar;
|
|
|
|
|
2019-12-07 12:42:49 +08:00
|
|
|
if (ctlr->bbar) {
|
|
|
|
minaddr &= bbar_mask;
|
|
|
|
ichspi_bbar = ich_readl(ctlr, ctlr->bbar) & ~bbar_mask;
|
|
|
|
ichspi_bbar |= minaddr;
|
|
|
|
ich_writel(ctlr, ichspi_bbar, ctlr->bbar);
|
|
|
|
}
|
2013-03-19 12:58:56 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
/* @return 1 if the SPI flash supports the 33MHz speed */
|
2019-12-07 12:42:41 +08:00
|
|
|
static bool ich9_can_do_33mhz(struct udevice *dev)
|
2013-03-19 12:58:56 +08:00
|
|
|
{
|
2019-12-07 12:42:38 +08:00
|
|
|
struct ich_spi_priv *priv = dev_get_priv(dev);
|
2013-03-19 12:58:56 +08:00
|
|
|
u32 fdod, speed;
|
|
|
|
|
2021-06-28 07:50:59 +08:00
|
|
|
if (!CONFIG_IS_ENABLED(PCI) || !priv->pch)
|
2019-12-07 12:42:48 +08:00
|
|
|
return false;
|
2013-03-19 12:58:56 +08:00
|
|
|
/* Observe SPI Descriptor Component Section 0 */
|
2019-12-07 12:42:38 +08:00
|
|
|
dm_pci_write_config32(priv->pch, 0xb0, 0x1000);
|
2013-03-19 12:58:56 +08:00
|
|
|
|
|
|
|
/* Extract the Write/Erase SPI Frequency from descriptor */
|
2019-12-07 12:42:38 +08:00
|
|
|
dm_pci_read_config32(priv->pch, 0xb4, &fdod);
|
2013-03-19 12:58:56 +08:00
|
|
|
|
|
|
|
/* Bits 23:21 have the fast read clock frequency, 0=20MHz, 1=33MHz */
|
|
|
|
speed = (fdod >> 21) & 7;
|
|
|
|
|
|
|
|
return speed == 1;
|
|
|
|
}
|
|
|
|
|
2020-12-04 07:55:23 +08:00
|
|
|
static void spi_lock_down(struct ich_spi_plat *plat, void *sbase)
|
2017-10-19 09:20:57 +08:00
|
|
|
{
|
|
|
|
if (plat->ich_version == ICHV_7) {
|
|
|
|
struct ich7_spi_regs *ich7_spi = sbase;
|
|
|
|
|
|
|
|
setbits_le16(&ich7_spi->spis, SPIS_LOCK);
|
|
|
|
} else if (plat->ich_version == ICHV_9) {
|
|
|
|
struct ich9_spi_regs *ich9_spi = sbase;
|
|
|
|
|
|
|
|
setbits_le16(&ich9_spi->hsfs, HSFS_FLOCKDN);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-12-04 07:55:23 +08:00
|
|
|
static bool spi_lock_status(struct ich_spi_plat *plat, void *sbase)
|
2017-08-16 13:38:29 +08:00
|
|
|
{
|
|
|
|
int lock = 0;
|
|
|
|
|
|
|
|
if (plat->ich_version == ICHV_7) {
|
|
|
|
struct ich7_spi_regs *ich7_spi = sbase;
|
|
|
|
|
|
|
|
lock = readw(&ich7_spi->spis) & SPIS_LOCK;
|
|
|
|
} else if (plat->ich_version == ICHV_9) {
|
|
|
|
struct ich9_spi_regs *ich9_spi = sbase;
|
|
|
|
|
|
|
|
lock = readw(&ich9_spi->hsfs) & HSFS_FLOCKDN;
|
|
|
|
}
|
|
|
|
|
|
|
|
return lock != 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
static int spi_setup_opcode(struct ich_spi_priv *ctlr, struct spi_trans *trans,
|
|
|
|
bool lock)
|
2013-03-19 12:58:56 +08:00
|
|
|
{
|
|
|
|
uint16_t optypes;
|
2015-03-26 23:29:26 +08:00
|
|
|
uint8_t opmenu[ctlr->menubytes];
|
2013-03-19 12:58:56 +08:00
|
|
|
|
2017-08-16 13:38:29 +08:00
|
|
|
if (!lock) {
|
2013-03-19 12:58:56 +08:00
|
|
|
/* The lock is off, so just use index 0. */
|
2015-03-26 23:29:26 +08:00
|
|
|
ich_writeb(ctlr, trans->opcode, ctlr->opmenu);
|
|
|
|
optypes = ich_readw(ctlr, ctlr->optype);
|
2013-03-19 12:58:56 +08:00
|
|
|
optypes = (optypes & 0xfffc) | (trans->type & 0x3);
|
2015-03-26 23:29:26 +08:00
|
|
|
ich_writew(ctlr, optypes, ctlr->optype);
|
2013-03-19 12:58:56 +08:00
|
|
|
return 0;
|
|
|
|
} else {
|
|
|
|
/* The lock is on. See if what we need is on the menu. */
|
|
|
|
uint8_t optype;
|
|
|
|
uint16_t opcode_index;
|
|
|
|
|
|
|
|
/* Write Enable is handled as atomic prefix */
|
|
|
|
if (trans->opcode == SPI_OPCODE_WREN)
|
|
|
|
return 0;
|
|
|
|
|
2015-03-26 23:29:26 +08:00
|
|
|
read_reg(ctlr, ctlr->opmenu, opmenu, sizeof(opmenu));
|
|
|
|
for (opcode_index = 0; opcode_index < ctlr->menubytes;
|
2013-03-19 12:58:56 +08:00
|
|
|
opcode_index++) {
|
|
|
|
if (opmenu[opcode_index] == trans->opcode)
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
2015-03-26 23:29:26 +08:00
|
|
|
if (opcode_index == ctlr->menubytes) {
|
2019-12-07 12:42:41 +08:00
|
|
|
debug("ICH SPI: Opcode %x not found\n", trans->opcode);
|
2015-03-26 23:29:26 +08:00
|
|
|
return -EINVAL;
|
2013-03-19 12:58:56 +08:00
|
|
|
}
|
|
|
|
|
2015-03-26 23:29:26 +08:00
|
|
|
optypes = ich_readw(ctlr, ctlr->optype);
|
2013-03-19 12:58:56 +08:00
|
|
|
optype = (optypes >> (opcode_index * 2)) & 0x3;
|
2019-08-02 14:38:34 +08:00
|
|
|
|
2013-03-19 12:58:56 +08:00
|
|
|
if (optype != trans->type) {
|
2019-12-07 12:42:41 +08:00
|
|
|
debug("ICH SPI: Transaction doesn't fit type %d\n",
|
|
|
|
optype);
|
2015-03-26 23:29:26 +08:00
|
|
|
return -ENOSPC;
|
2013-03-19 12:58:56 +08:00
|
|
|
}
|
|
|
|
return opcode_index;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Wait for up to 6s til status register bit(s) turn 1 (in case wait_til_set
|
2013-04-02 02:29:11 +08:00
|
|
|
* below is true) or 0. In case the wait was for the bit(s) to set - write
|
2013-03-19 12:58:56 +08:00
|
|
|
* those bits back, which would cause resetting them.
|
|
|
|
*
|
|
|
|
* Return the last read status value on success or -1 on failure.
|
|
|
|
*/
|
2015-03-26 23:29:26 +08:00
|
|
|
static int ich_status_poll(struct ich_spi_priv *ctlr, u16 bitmask,
|
|
|
|
int wait_til_set)
|
2013-03-19 12:58:56 +08:00
|
|
|
{
|
|
|
|
int timeout = 600000; /* This will result in 6s */
|
|
|
|
u16 status = 0;
|
|
|
|
|
|
|
|
while (timeout--) {
|
2015-03-26 23:29:26 +08:00
|
|
|
status = ich_readw(ctlr, ctlr->status);
|
2013-03-19 12:58:56 +08:00
|
|
|
if (wait_til_set ^ ((status & bitmask) == 0)) {
|
2015-03-26 23:29:26 +08:00
|
|
|
if (wait_til_set) {
|
|
|
|
ich_writew(ctlr, status & bitmask,
|
|
|
|
ctlr->status);
|
|
|
|
}
|
2013-03-19 12:58:56 +08:00
|
|
|
return status;
|
|
|
|
}
|
|
|
|
udelay(10);
|
|
|
|
}
|
2019-12-07 12:42:41 +08:00
|
|
|
debug("ICH SPI: SCIP timeout, read %x, expected %x, wts %x %x\n",
|
|
|
|
status, bitmask, wait_til_set, status & bitmask);
|
2013-03-19 12:58:56 +08:00
|
|
|
|
2015-03-26 23:29:26 +08:00
|
|
|
return -ETIMEDOUT;
|
2013-03-19 12:58:56 +08:00
|
|
|
}
|
|
|
|
|
2019-08-02 14:38:34 +08:00
|
|
|
static void ich_spi_config_opcode(struct udevice *dev)
|
2017-08-16 13:38:30 +08:00
|
|
|
{
|
|
|
|
struct ich_spi_priv *ctlr = dev_get_priv(dev);
|
|
|
|
|
|
|
|
/*
|
|
|
|
* PREOP, OPTYPE, OPMENU1/OPMENU2 registers can be locked down
|
|
|
|
* to prevent accidental or intentional writes. Before they get
|
|
|
|
* locked down, these registers should be initialized properly.
|
|
|
|
*/
|
|
|
|
ich_writew(ctlr, SPI_OPPREFIX, ctlr->preop);
|
|
|
|
ich_writew(ctlr, SPI_OPTYPE, ctlr->optype);
|
|
|
|
ich_writel(ctlr, SPI_OPMENU_LOWER, ctlr->opmenu);
|
|
|
|
ich_writel(ctlr, SPI_OPMENU_UPPER, ctlr->opmenu + sizeof(u32));
|
|
|
|
}
|
|
|
|
|
2019-12-07 12:42:46 +08:00
|
|
|
static int ich_spi_exec_op_swseq(struct spi_slave *slave,
|
|
|
|
const struct spi_mem_op *op)
|
2013-03-19 12:58:56 +08:00
|
|
|
{
|
2019-08-02 14:38:34 +08:00
|
|
|
struct udevice *bus = dev_get_parent(slave->dev);
|
2020-12-04 07:55:23 +08:00
|
|
|
struct ich_spi_plat *plat = dev_get_plat(bus);
|
2015-03-26 23:29:26 +08:00
|
|
|
struct ich_spi_priv *ctlr = dev_get_priv(bus);
|
2013-03-19 12:58:56 +08:00
|
|
|
uint16_t control;
|
|
|
|
int16_t opcode_index;
|
|
|
|
int with_address;
|
|
|
|
int status;
|
2015-03-26 23:29:26 +08:00
|
|
|
struct spi_trans *trans = &ctlr->trans;
|
2017-08-16 13:38:29 +08:00
|
|
|
bool lock = spi_lock_status(plat, ctlr->base);
|
2019-08-02 14:38:34 +08:00
|
|
|
int ret = 0;
|
2013-03-19 12:58:56 +08:00
|
|
|
|
2019-08-02 14:38:34 +08:00
|
|
|
trans->in = NULL;
|
|
|
|
trans->out = NULL;
|
|
|
|
trans->type = 0xFF;
|
2013-03-19 12:58:56 +08:00
|
|
|
|
2019-08-02 14:38:34 +08:00
|
|
|
if (op->data.nbytes) {
|
|
|
|
if (op->data.dir == SPI_MEM_DATA_IN) {
|
|
|
|
trans->in = op->data.buf.in;
|
|
|
|
trans->bytesin = op->data.nbytes;
|
|
|
|
} else {
|
|
|
|
trans->out = op->data.buf.out;
|
|
|
|
trans->bytesout = op->data.nbytes;
|
2013-03-19 12:58:56 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-08-02 14:38:34 +08:00
|
|
|
if (trans->opcode != op->cmd.opcode)
|
|
|
|
trans->opcode = op->cmd.opcode;
|
2013-03-19 12:58:56 +08:00
|
|
|
|
2019-08-02 14:38:34 +08:00
|
|
|
if (lock && trans->opcode == SPI_OPCODE_WRDIS)
|
|
|
|
return 0;
|
2013-03-19 12:58:56 +08:00
|
|
|
|
2019-08-02 14:38:34 +08:00
|
|
|
if (trans->opcode == SPI_OPCODE_WREN) {
|
|
|
|
/*
|
|
|
|
* Treat Write Enable as Atomic Pre-Op if possible
|
|
|
|
* in order to prevent the Management Engine from
|
|
|
|
* issuing a transaction between WREN and DATA.
|
|
|
|
*/
|
|
|
|
if (!lock)
|
|
|
|
ich_writew(ctlr, trans->opcode, ctlr->preop);
|
|
|
|
return 0;
|
2013-03-19 12:58:56 +08:00
|
|
|
}
|
|
|
|
|
2015-03-26 23:29:26 +08:00
|
|
|
ret = ich_status_poll(ctlr, SPIS_SCIP, 0);
|
|
|
|
if (ret < 0)
|
|
|
|
return ret;
|
2013-03-19 12:58:56 +08:00
|
|
|
|
2016-02-01 17:40:38 +08:00
|
|
|
if (plat->ich_version == ICHV_7)
|
2015-07-04 08:28:21 +08:00
|
|
|
ich_writew(ctlr, SPIS_CDS | SPIS_FCERR, ctlr->status);
|
|
|
|
else
|
|
|
|
ich_writeb(ctlr, SPIS_CDS | SPIS_FCERR, ctlr->status);
|
2013-03-19 12:58:56 +08:00
|
|
|
|
2019-08-02 14:38:34 +08:00
|
|
|
/* Try to guess spi transaction type */
|
|
|
|
if (op->data.dir == SPI_MEM_DATA_OUT) {
|
|
|
|
if (op->addr.nbytes)
|
|
|
|
trans->type = SPI_OPCODE_TYPE_WRITE_WITH_ADDRESS;
|
|
|
|
else
|
|
|
|
trans->type = SPI_OPCODE_TYPE_WRITE_NO_ADDRESS;
|
|
|
|
} else {
|
|
|
|
if (op->addr.nbytes)
|
|
|
|
trans->type = SPI_OPCODE_TYPE_READ_WITH_ADDRESS;
|
|
|
|
else
|
|
|
|
trans->type = SPI_OPCODE_TYPE_READ_NO_ADDRESS;
|
|
|
|
}
|
|
|
|
/* Special erase case handling */
|
|
|
|
if (op->addr.nbytes && !op->data.buswidth)
|
|
|
|
trans->type = SPI_OPCODE_TYPE_WRITE_WITH_ADDRESS;
|
|
|
|
|
2017-08-16 13:38:29 +08:00
|
|
|
opcode_index = spi_setup_opcode(ctlr, trans, lock);
|
2013-03-19 12:58:56 +08:00
|
|
|
if (opcode_index < 0)
|
2015-03-26 23:29:26 +08:00
|
|
|
return -EINVAL;
|
2013-03-19 12:58:56 +08:00
|
|
|
|
2019-08-02 14:38:34 +08:00
|
|
|
if (op->addr.nbytes) {
|
|
|
|
trans->offset = op->addr.val;
|
|
|
|
with_address = 1;
|
2013-03-19 12:58:56 +08:00
|
|
|
}
|
|
|
|
|
2015-03-26 23:29:26 +08:00
|
|
|
if (ctlr->speed && ctlr->max_speed >= 33000000) {
|
2013-03-19 12:58:56 +08:00
|
|
|
int byte;
|
|
|
|
|
2015-03-26 23:29:26 +08:00
|
|
|
byte = ich_readb(ctlr, ctlr->speed);
|
|
|
|
if (ctlr->cur_speed >= 33000000)
|
2013-03-19 12:58:56 +08:00
|
|
|
byte |= SSFC_SCF_33MHZ;
|
|
|
|
else
|
|
|
|
byte &= ~SSFC_SCF_33MHZ;
|
2015-03-26 23:29:26 +08:00
|
|
|
ich_writeb(ctlr, byte, ctlr->speed);
|
2013-03-19 12:58:56 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
/* Preset control fields */
|
|
|
|
control = SPIC_SCGO | ((opcode_index & 0x07) << 4);
|
|
|
|
|
|
|
|
/* Issue atomic preop cycle if needed */
|
2015-03-26 23:29:26 +08:00
|
|
|
if (ich_readw(ctlr, ctlr->preop))
|
2013-03-19 12:58:56 +08:00
|
|
|
control |= SPIC_ACS;
|
|
|
|
|
|
|
|
if (!trans->bytesout && !trans->bytesin) {
|
|
|
|
/* SPI addresses are 24 bit only */
|
2015-03-26 23:29:26 +08:00
|
|
|
if (with_address) {
|
|
|
|
ich_writel(ctlr, trans->offset & 0x00FFFFFF,
|
|
|
|
ctlr->addr);
|
|
|
|
}
|
2013-03-19 12:58:56 +08:00
|
|
|
/*
|
|
|
|
* This is a 'no data' command (like Write Enable), its
|
|
|
|
* bitesout size was 1, decremented to zero while executing
|
|
|
|
* spi_setup_opcode() above. Tell the chip to send the
|
|
|
|
* command.
|
|
|
|
*/
|
2015-03-26 23:29:26 +08:00
|
|
|
ich_writew(ctlr, control, ctlr->control);
|
2013-03-19 12:58:56 +08:00
|
|
|
|
|
|
|
/* wait for the result */
|
2015-03-26 23:29:26 +08:00
|
|
|
status = ich_status_poll(ctlr, SPIS_CDS | SPIS_FCERR, 1);
|
|
|
|
if (status < 0)
|
|
|
|
return status;
|
2013-03-19 12:58:56 +08:00
|
|
|
|
|
|
|
if (status & SPIS_FCERR) {
|
|
|
|
debug("ICH SPI: Command transaction error\n");
|
2015-03-26 23:29:26 +08:00
|
|
|
return -EIO;
|
2013-03-19 12:58:56 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
while (trans->bytesout || trans->bytesin) {
|
|
|
|
uint32_t data_length;
|
|
|
|
|
|
|
|
/* SPI addresses are 24 bit only */
|
2015-03-26 23:29:26 +08:00
|
|
|
ich_writel(ctlr, trans->offset & 0x00FFFFFF, ctlr->addr);
|
2013-03-19 12:58:56 +08:00
|
|
|
|
|
|
|
if (trans->bytesout)
|
2015-03-26 23:29:26 +08:00
|
|
|
data_length = min(trans->bytesout, ctlr->databytes);
|
2013-03-19 12:58:56 +08:00
|
|
|
else
|
2015-03-26 23:29:26 +08:00
|
|
|
data_length = min(trans->bytesin, ctlr->databytes);
|
2013-03-19 12:58:56 +08:00
|
|
|
|
|
|
|
/* Program data into FDATA0 to N */
|
|
|
|
if (trans->bytesout) {
|
2015-03-26 23:29:26 +08:00
|
|
|
write_reg(ctlr, trans->out, ctlr->data, data_length);
|
2019-08-02 14:38:34 +08:00
|
|
|
trans->bytesout -= data_length;
|
2013-03-19 12:58:56 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
/* Add proper control fields' values */
|
2015-03-26 23:29:26 +08:00
|
|
|
control &= ~((ctlr->databytes - 1) << 8);
|
2013-03-19 12:58:56 +08:00
|
|
|
control |= SPIC_DS;
|
|
|
|
control |= (data_length - 1) << 8;
|
|
|
|
|
|
|
|
/* write it */
|
2015-03-26 23:29:26 +08:00
|
|
|
ich_writew(ctlr, control, ctlr->control);
|
2013-03-19 12:58:56 +08:00
|
|
|
|
2016-02-01 17:40:36 +08:00
|
|
|
/* Wait for Cycle Done Status or Flash Cycle Error */
|
2015-03-26 23:29:26 +08:00
|
|
|
status = ich_status_poll(ctlr, SPIS_CDS | SPIS_FCERR, 1);
|
|
|
|
if (status < 0)
|
|
|
|
return status;
|
2013-03-19 12:58:56 +08:00
|
|
|
|
|
|
|
if (status & SPIS_FCERR) {
|
2015-06-07 22:50:33 +08:00
|
|
|
debug("ICH SPI: Data transaction error %x\n", status);
|
2015-03-26 23:29:26 +08:00
|
|
|
return -EIO;
|
2013-03-19 12:58:56 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
if (trans->bytesin) {
|
2015-03-26 23:29:26 +08:00
|
|
|
read_reg(ctlr, ctlr->data, trans->in, data_length);
|
2019-08-02 14:38:34 +08:00
|
|
|
trans->bytesin -= data_length;
|
2013-03-19 12:58:56 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Clear atomic preop now that xfer is done */
|
2017-08-27 10:22:59 +08:00
|
|
|
if (!lock)
|
|
|
|
ich_writew(ctlr, 0, ctlr->preop);
|
2013-03-19 12:58:56 +08:00
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2019-12-07 12:42:46 +08:00
|
|
|
/*
|
|
|
|
* Ensure read/write xfer len is not greater than SPIBAR_FDATA_FIFO_SIZE and
|
|
|
|
* that the operation does not cross page boundary.
|
|
|
|
*/
|
|
|
|
static uint get_xfer_len(u32 offset, int len, int page_size)
|
|
|
|
{
|
|
|
|
uint xfer_len = min(len, SPIBAR_FDATA_FIFO_SIZE);
|
|
|
|
uint bytes_left = ALIGN(offset, page_size) - offset;
|
|
|
|
|
|
|
|
if (bytes_left)
|
|
|
|
xfer_len = min(xfer_len, bytes_left);
|
|
|
|
|
|
|
|
return xfer_len;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Fill FDATAn FIFO in preparation for a write transaction */
|
|
|
|
static void fill_xfer_fifo(struct fast_spi_regs *regs, const void *data,
|
|
|
|
uint len)
|
|
|
|
{
|
|
|
|
memcpy(regs->fdata, data, len);
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Drain FDATAn FIFO after a read transaction populates data */
|
|
|
|
static void drain_xfer_fifo(struct fast_spi_regs *regs, void *dest, uint len)
|
|
|
|
{
|
|
|
|
memcpy(dest, regs->fdata, len);
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Fire up a transfer using the hardware sequencer */
|
|
|
|
static void start_hwseq_xfer(struct fast_spi_regs *regs, uint hsfsts_cycle,
|
|
|
|
uint offset, uint len)
|
|
|
|
{
|
|
|
|
/* Make sure all W1C status bits get cleared */
|
|
|
|
u32 hsfsts;
|
|
|
|
|
|
|
|
hsfsts = readl(®s->hsfsts_ctl);
|
|
|
|
hsfsts &= ~(HSFSTS_FCYCLE_MASK | HSFSTS_FDBC_MASK);
|
|
|
|
hsfsts |= HSFSTS_AEL | HSFSTS_FCERR | HSFSTS_FDONE;
|
|
|
|
|
|
|
|
/* Set up transaction parameters */
|
|
|
|
hsfsts |= hsfsts_cycle << HSFSTS_FCYCLE_SHIFT;
|
|
|
|
hsfsts |= ((len - 1) << HSFSTS_FDBC_SHIFT) & HSFSTS_FDBC_MASK;
|
|
|
|
hsfsts |= HSFSTS_FGO;
|
|
|
|
|
|
|
|
writel(offset, ®s->faddr);
|
|
|
|
writel(hsfsts, ®s->hsfsts_ctl);
|
|
|
|
}
|
|
|
|
|
|
|
|
static int wait_for_hwseq_xfer(struct fast_spi_regs *regs, uint offset)
|
|
|
|
{
|
|
|
|
ulong start;
|
|
|
|
u32 hsfsts;
|
|
|
|
|
|
|
|
start = get_timer(0);
|
|
|
|
do {
|
|
|
|
hsfsts = readl(®s->hsfsts_ctl);
|
|
|
|
if (hsfsts & HSFSTS_FCERR) {
|
|
|
|
debug("SPI transaction error at offset %x HSFSTS = %08x\n",
|
|
|
|
offset, hsfsts);
|
|
|
|
return -EIO;
|
|
|
|
}
|
|
|
|
if (hsfsts & HSFSTS_AEL)
|
|
|
|
return -EPERM;
|
|
|
|
|
|
|
|
if (hsfsts & HSFSTS_FDONE)
|
|
|
|
return 0;
|
|
|
|
} while (get_timer(start) < SPIBAR_HWSEQ_XFER_TIMEOUT_MS);
|
|
|
|
|
|
|
|
debug("SPI transaction timeout at offset %x HSFSTS = %08x, timer %d\n",
|
|
|
|
offset, hsfsts, (uint)get_timer(start));
|
|
|
|
|
|
|
|
return -ETIMEDOUT;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* exec_sync_hwseq_xfer() - Execute flash transfer by hardware sequencing
|
|
|
|
*
|
|
|
|
* This waits until complete or timeout
|
|
|
|
*
|
|
|
|
* @regs: SPI registers
|
|
|
|
* @hsfsts_cycle: Cycle type (enum hsfsts_cycle_t)
|
|
|
|
* @offset: Offset to access
|
|
|
|
* @len: Number of bytes to transfer (can be 0)
|
2022-01-20 01:05:50 +08:00
|
|
|
* Return: 0 if OK, -EIO on flash-cycle error (FCERR), -EPERM on access error
|
2019-12-07 12:42:46 +08:00
|
|
|
* (AEL), -ETIMEDOUT on timeout
|
|
|
|
*/
|
|
|
|
static int exec_sync_hwseq_xfer(struct fast_spi_regs *regs, uint hsfsts_cycle,
|
|
|
|
uint offset, uint len)
|
|
|
|
{
|
|
|
|
start_hwseq_xfer(regs, hsfsts_cycle, offset, len);
|
|
|
|
|
|
|
|
return wait_for_hwseq_xfer(regs, offset);
|
|
|
|
}
|
|
|
|
|
|
|
|
static int ich_spi_exec_op_hwseq(struct spi_slave *slave,
|
|
|
|
const struct spi_mem_op *op)
|
|
|
|
{
|
|
|
|
struct spi_flash *flash = dev_get_uclass_priv(slave->dev);
|
|
|
|
struct udevice *bus = dev_get_parent(slave->dev);
|
|
|
|
struct ich_spi_priv *priv = dev_get_priv(bus);
|
|
|
|
struct fast_spi_regs *regs = priv->base;
|
|
|
|
uint page_size;
|
|
|
|
uint offset;
|
|
|
|
int cycle;
|
|
|
|
uint len;
|
|
|
|
bool out;
|
|
|
|
int ret;
|
|
|
|
u8 *buf;
|
|
|
|
|
|
|
|
offset = op->addr.val;
|
|
|
|
len = op->data.nbytes;
|
|
|
|
|
|
|
|
switch (op->cmd.opcode) {
|
|
|
|
case SPINOR_OP_RDID:
|
|
|
|
cycle = HSFSTS_CYCLE_RDID;
|
|
|
|
break;
|
|
|
|
case SPINOR_OP_READ_FAST:
|
|
|
|
cycle = HSFSTS_CYCLE_READ;
|
|
|
|
break;
|
|
|
|
case SPINOR_OP_PP:
|
|
|
|
cycle = HSFSTS_CYCLE_WRITE;
|
|
|
|
break;
|
|
|
|
case SPINOR_OP_WREN:
|
|
|
|
/* Nothing needs to be done */
|
|
|
|
return 0;
|
|
|
|
case SPINOR_OP_WRSR:
|
|
|
|
cycle = HSFSTS_CYCLE_WR_STATUS;
|
|
|
|
break;
|
|
|
|
case SPINOR_OP_RDSR:
|
|
|
|
cycle = HSFSTS_CYCLE_RD_STATUS;
|
|
|
|
break;
|
|
|
|
case SPINOR_OP_WRDI:
|
|
|
|
return 0; /* ignore */
|
|
|
|
case SPINOR_OP_BE_4K:
|
|
|
|
cycle = HSFSTS_CYCLE_4K_ERASE;
|
2020-01-14 21:05:48 +08:00
|
|
|
ret = exec_sync_hwseq_xfer(regs, cycle, offset, 0);
|
|
|
|
return ret;
|
2019-12-07 12:42:46 +08:00
|
|
|
default:
|
|
|
|
debug("Unknown cycle %x\n", op->cmd.opcode);
|
|
|
|
return -EINVAL;
|
|
|
|
};
|
|
|
|
|
|
|
|
out = op->data.dir == SPI_MEM_DATA_OUT;
|
|
|
|
buf = out ? (u8 *)op->data.buf.out : op->data.buf.in;
|
|
|
|
page_size = flash->page_size ? : 256;
|
|
|
|
|
|
|
|
while (len) {
|
|
|
|
uint xfer_len = get_xfer_len(offset, len, page_size);
|
|
|
|
|
|
|
|
if (out)
|
|
|
|
fill_xfer_fifo(regs, buf, xfer_len);
|
|
|
|
|
|
|
|
ret = exec_sync_hwseq_xfer(regs, cycle, offset, xfer_len);
|
|
|
|
if (ret)
|
|
|
|
return ret;
|
|
|
|
|
|
|
|
if (!out)
|
|
|
|
drain_xfer_fifo(regs, buf, xfer_len);
|
|
|
|
|
|
|
|
offset += xfer_len;
|
|
|
|
buf += xfer_len;
|
|
|
|
len -= xfer_len;
|
|
|
|
}
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
static int ich_spi_exec_op(struct spi_slave *slave, const struct spi_mem_op *op)
|
|
|
|
{
|
|
|
|
struct udevice *bus = dev_get_parent(slave->dev);
|
2020-12-04 07:55:23 +08:00
|
|
|
struct ich_spi_plat *plat = dev_get_plat(bus);
|
2019-12-07 12:42:46 +08:00
|
|
|
int ret;
|
|
|
|
|
|
|
|
bootstage_start(BOOTSTAGE_ID_ACCUM_SPI, "fast_spi");
|
|
|
|
if (plat->hwseq)
|
|
|
|
ret = ich_spi_exec_op_hwseq(slave, op);
|
|
|
|
else
|
|
|
|
ret = ich_spi_exec_op_swseq(slave, op);
|
|
|
|
bootstage_accum(BOOTSTAGE_ID_ACCUM_SPI);
|
|
|
|
|
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
2021-08-07 21:24:04 +08:00
|
|
|
#if CONFIG_IS_ENABLED(OF_REAL)
|
2020-05-27 20:58:47 +08:00
|
|
|
/**
|
|
|
|
* ich_spi_get_basics() - Get basic information about the ICH device
|
|
|
|
*
|
|
|
|
* This works without probing any devices if requested.
|
|
|
|
*
|
|
|
|
* @bus: SPI controller to use
|
|
|
|
* @can_probe: true if this function is allowed to probe the PCH
|
|
|
|
* @pchp: Returns a pointer to the pch, or NULL if not found
|
|
|
|
* @ich_versionp: Returns ICH version detected on success
|
|
|
|
* @mmio_basep: Returns the address of the SPI registers on success
|
2022-01-20 01:05:50 +08:00
|
|
|
* Return: 0 if OK, -EPROTOTYPE if the PCH could not be found, -EAGAIN if
|
2020-05-27 20:58:47 +08:00
|
|
|
* the function cannot success without probing, possible another error if
|
|
|
|
* pch_get_spi_base() fails
|
|
|
|
*/
|
|
|
|
static int ich_spi_get_basics(struct udevice *bus, bool can_probe,
|
|
|
|
struct udevice **pchp,
|
|
|
|
enum ich_version *ich_versionp, ulong *mmio_basep)
|
|
|
|
{
|
|
|
|
struct udevice *pch = NULL;
|
|
|
|
int ret = 0;
|
|
|
|
|
|
|
|
/* Find a PCH if there is one */
|
|
|
|
if (can_probe) {
|
|
|
|
pch = dev_get_parent(bus);
|
|
|
|
if (device_get_uclass_id(pch) != UCLASS_PCH) {
|
|
|
|
uclass_first_device(UCLASS_PCH, &pch);
|
|
|
|
if (!pch)
|
2021-06-28 07:50:59 +08:00
|
|
|
; /* ignore this error since we don't need it */
|
2020-05-27 20:58:47 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
*ich_versionp = dev_get_driver_data(bus);
|
|
|
|
if (*ich_versionp == ICHV_APL)
|
|
|
|
*mmio_basep = dm_pci_read_bar32(bus, 0);
|
|
|
|
else if (pch)
|
|
|
|
ret = pch_get_spi_base(pch, mmio_basep);
|
|
|
|
else
|
|
|
|
return -EAGAIN;
|
|
|
|
*pchp = pch;
|
|
|
|
|
|
|
|
return ret;
|
|
|
|
}
|
2020-10-04 01:31:38 +08:00
|
|
|
#endif
|
2020-05-27 20:58:47 +08:00
|
|
|
|
|
|
|
/**
|
|
|
|
* ich_get_mmap_bus() - Handle the get_mmap() method for a bus
|
|
|
|
*
|
|
|
|
* There are several cases to consider:
|
|
|
|
* 1. Using of-platdata, in which case we have the BDF and can access the
|
|
|
|
* registers by reading the BAR
|
|
|
|
* 2. Not using of-platdata, but still with a SPI controller that is on its own
|
2020-12-04 07:55:18 +08:00
|
|
|
* PCI PDF. In this case we read the BDF from the parent plat and again get
|
2020-05-27 20:58:47 +08:00
|
|
|
* the registers by reading the BAR
|
|
|
|
* 3. Using a SPI controller that is a child of the PCH, in which case we try
|
|
|
|
* to find the registers by asking the PCH. This only works if the PCH has
|
|
|
|
* been probed (which it will be if the bus is probed since parents are
|
|
|
|
* probed before children), since the PCH may not have a PCI address until
|
|
|
|
* its parent (the PCI bus itself) has been probed. If you are using this
|
|
|
|
* method then you should make sure the SPI bus is probed.
|
|
|
|
*
|
|
|
|
* The first two cases are useful in early init. The last one is more useful
|
|
|
|
* afterwards.
|
|
|
|
*/
|
2019-12-07 12:42:47 +08:00
|
|
|
static int ich_get_mmap_bus(struct udevice *bus, ulong *map_basep,
|
|
|
|
uint *map_sizep, uint *offsetp)
|
|
|
|
{
|
|
|
|
pci_dev_t spi_bdf;
|
2021-08-07 21:24:04 +08:00
|
|
|
#if CONFIG_IS_ENABLED(OF_REAL)
|
2020-05-27 20:58:47 +08:00
|
|
|
if (device_is_on_pci_bus(bus)) {
|
2020-12-04 07:55:23 +08:00
|
|
|
struct pci_child_plat *pplat;
|
2020-05-27 20:58:47 +08:00
|
|
|
|
2020-12-04 07:55:18 +08:00
|
|
|
pplat = dev_get_parent_plat(bus);
|
2020-05-27 20:58:47 +08:00
|
|
|
spi_bdf = pplat->devfn;
|
|
|
|
} else {
|
|
|
|
enum ich_version ich_version;
|
|
|
|
struct fast_spi_regs *regs;
|
|
|
|
struct udevice *pch;
|
|
|
|
ulong mmio_base;
|
|
|
|
int ret;
|
2019-12-07 12:42:47 +08:00
|
|
|
|
2020-05-27 20:58:47 +08:00
|
|
|
ret = ich_spi_get_basics(bus, device_active(bus), &pch,
|
|
|
|
&ich_version, &mmio_base);
|
|
|
|
if (ret)
|
|
|
|
return log_msg_ret("basics", ret);
|
|
|
|
regs = (struct fast_spi_regs *)mmio_base;
|
|
|
|
|
|
|
|
return fast_spi_get_bios_mmap_regs(regs, map_basep, map_sizep,
|
|
|
|
offsetp);
|
|
|
|
}
|
2019-12-07 12:42:47 +08:00
|
|
|
#else
|
2020-12-04 07:55:23 +08:00
|
|
|
struct ich_spi_plat *plat = dev_get_plat(bus);
|
2019-12-07 12:42:47 +08:00
|
|
|
|
|
|
|
/*
|
|
|
|
* We cannot rely on plat->bdf being set up yet since this method can
|
|
|
|
* be called before the device is probed. Use the of-platdata directly
|
|
|
|
* instead.
|
|
|
|
*/
|
|
|
|
spi_bdf = pci_ofplat_get_devfn(plat->dtplat.reg[0]);
|
|
|
|
#endif
|
|
|
|
|
|
|
|
return fast_spi_get_bios_mmap(spi_bdf, map_basep, map_sizep, offsetp);
|
|
|
|
}
|
|
|
|
|
|
|
|
static int ich_get_mmap(struct udevice *dev, ulong *map_basep, uint *map_sizep,
|
|
|
|
uint *offsetp)
|
|
|
|
{
|
|
|
|
struct udevice *bus = dev_get_parent(dev);
|
|
|
|
|
|
|
|
return ich_get_mmap_bus(bus, map_basep, map_sizep, offsetp);
|
|
|
|
}
|
|
|
|
|
2019-08-02 14:38:34 +08:00
|
|
|
static int ich_spi_adjust_size(struct spi_slave *slave, struct spi_mem_op *op)
|
|
|
|
{
|
|
|
|
unsigned int page_offset;
|
|
|
|
int addr = op->addr.val;
|
|
|
|
unsigned int byte_count = op->data.nbytes;
|
|
|
|
|
|
|
|
if (hweight32(ICH_BOUNDARY) == 1) {
|
|
|
|
page_offset = addr & (ICH_BOUNDARY - 1);
|
|
|
|
} else {
|
|
|
|
u64 aux = addr;
|
|
|
|
|
|
|
|
page_offset = do_div(aux, ICH_BOUNDARY);
|
|
|
|
}
|
|
|
|
|
2019-12-07 12:42:44 +08:00
|
|
|
if (op->data.dir == SPI_MEM_DATA_IN) {
|
|
|
|
if (slave->max_read_size) {
|
|
|
|
op->data.nbytes = min(ICH_BOUNDARY - page_offset,
|
|
|
|
slave->max_read_size);
|
|
|
|
}
|
2019-08-02 14:38:34 +08:00
|
|
|
} else if (slave->max_write_size) {
|
|
|
|
op->data.nbytes = min(ICH_BOUNDARY - page_offset,
|
|
|
|
slave->max_write_size);
|
|
|
|
}
|
|
|
|
|
|
|
|
op->data.nbytes = min(op->data.nbytes, byte_count);
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2019-12-07 12:42:38 +08:00
|
|
|
static int ich_protect_lockdown(struct udevice *dev)
|
|
|
|
{
|
2020-12-04 07:55:23 +08:00
|
|
|
struct ich_spi_plat *plat = dev_get_plat(dev);
|
2019-12-07 12:42:38 +08:00
|
|
|
struct ich_spi_priv *priv = dev_get_priv(dev);
|
|
|
|
int ret = -ENOSYS;
|
|
|
|
|
|
|
|
/* Disable the BIOS write protect so write commands are allowed */
|
|
|
|
if (priv->pch)
|
|
|
|
ret = pch_set_spi_protect(priv->pch, false);
|
|
|
|
if (ret == -ENOSYS) {
|
|
|
|
u8 bios_cntl;
|
|
|
|
|
|
|
|
bios_cntl = ich_readb(priv, priv->bcr);
|
|
|
|
bios_cntl &= ~BIT(5); /* clear Enable InSMM_STS (EISS) */
|
|
|
|
bios_cntl |= 1; /* Write Protect Disable (WPD) */
|
|
|
|
ich_writeb(priv, bios_cntl, priv->bcr);
|
|
|
|
} else if (ret) {
|
|
|
|
debug("%s: Failed to disable write-protect: err=%d\n",
|
|
|
|
__func__, ret);
|
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Lock down SPI controller settings if required */
|
|
|
|
if (plat->lockdown) {
|
|
|
|
ich_spi_config_opcode(dev);
|
|
|
|
spi_lock_down(plat, priv->base);
|
|
|
|
}
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2019-12-07 12:42:37 +08:00
|
|
|
static int ich_init_controller(struct udevice *dev,
|
2020-12-04 07:55:23 +08:00
|
|
|
struct ich_spi_plat *plat,
|
2019-12-07 12:42:37 +08:00
|
|
|
struct ich_spi_priv *ctlr)
|
|
|
|
{
|
2024-09-30 09:49:36 +08:00
|
|
|
if (xpl_phase() == PHASE_TPL) {
|
2020-12-04 07:55:23 +08:00
|
|
|
struct ich_spi_plat *plat = dev_get_plat(dev);
|
2019-12-07 12:42:48 +08:00
|
|
|
int ret;
|
|
|
|
|
|
|
|
ret = fast_spi_early_init(plat->bdf, plat->mmio_base);
|
|
|
|
if (ret)
|
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
2019-12-07 12:42:42 +08:00
|
|
|
ctlr->base = (void *)plat->mmio_base;
|
2019-12-07 12:42:37 +08:00
|
|
|
if (plat->ich_version == ICHV_7) {
|
2019-12-07 12:42:42 +08:00
|
|
|
struct ich7_spi_regs *ich7_spi = ctlr->base;
|
2019-12-07 12:42:37 +08:00
|
|
|
|
|
|
|
ctlr->opmenu = offsetof(struct ich7_spi_regs, opmenu);
|
|
|
|
ctlr->menubytes = sizeof(ich7_spi->opmenu);
|
|
|
|
ctlr->optype = offsetof(struct ich7_spi_regs, optype);
|
|
|
|
ctlr->addr = offsetof(struct ich7_spi_regs, spia);
|
|
|
|
ctlr->data = offsetof(struct ich7_spi_regs, spid);
|
|
|
|
ctlr->databytes = sizeof(ich7_spi->spid);
|
|
|
|
ctlr->status = offsetof(struct ich7_spi_regs, spis);
|
|
|
|
ctlr->control = offsetof(struct ich7_spi_regs, spic);
|
|
|
|
ctlr->bbar = offsetof(struct ich7_spi_regs, bbar);
|
|
|
|
ctlr->preop = offsetof(struct ich7_spi_regs, preop);
|
|
|
|
} else if (plat->ich_version == ICHV_9) {
|
2019-12-07 12:42:42 +08:00
|
|
|
struct ich9_spi_regs *ich9_spi = ctlr->base;
|
2019-12-07 12:42:37 +08:00
|
|
|
|
|
|
|
ctlr->opmenu = offsetof(struct ich9_spi_regs, opmenu);
|
|
|
|
ctlr->menubytes = sizeof(ich9_spi->opmenu);
|
|
|
|
ctlr->optype = offsetof(struct ich9_spi_regs, optype);
|
|
|
|
ctlr->addr = offsetof(struct ich9_spi_regs, faddr);
|
|
|
|
ctlr->data = offsetof(struct ich9_spi_regs, fdata);
|
|
|
|
ctlr->databytes = sizeof(ich9_spi->fdata);
|
|
|
|
ctlr->status = offsetof(struct ich9_spi_regs, ssfs);
|
|
|
|
ctlr->control = offsetof(struct ich9_spi_regs, ssfc);
|
|
|
|
ctlr->speed = ctlr->control + 2;
|
|
|
|
ctlr->bbar = offsetof(struct ich9_spi_regs, bbar);
|
|
|
|
ctlr->preop = offsetof(struct ich9_spi_regs, preop);
|
|
|
|
ctlr->bcr = offsetof(struct ich9_spi_regs, bcr);
|
|
|
|
ctlr->pr = &ich9_spi->pr[0];
|
2019-12-07 12:42:49 +08:00
|
|
|
} else if (plat->ich_version == ICHV_APL) {
|
2019-12-07 12:42:37 +08:00
|
|
|
} else {
|
|
|
|
debug("ICH SPI: Unrecognised ICH version %d\n",
|
|
|
|
plat->ich_version);
|
|
|
|
return -EINVAL;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Work out the maximum speed we can support */
|
|
|
|
ctlr->max_speed = 20000000;
|
|
|
|
if (plat->ich_version == ICHV_9 && ich9_can_do_33mhz(dev))
|
|
|
|
ctlr->max_speed = 33000000;
|
2019-12-07 12:42:42 +08:00
|
|
|
debug("ICH SPI: Version ID %d detected at %lx, speed %ld\n",
|
|
|
|
plat->ich_version, plat->mmio_base, ctlr->max_speed);
|
2019-12-07 12:42:37 +08:00
|
|
|
|
|
|
|
ich_set_bbar(ctlr, 0);
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2019-12-07 12:42:48 +08:00
|
|
|
static int ich_cache_bios_region(struct udevice *dev)
|
|
|
|
{
|
|
|
|
ulong map_base;
|
|
|
|
uint map_size;
|
|
|
|
uint offset;
|
|
|
|
ulong base;
|
|
|
|
int ret;
|
|
|
|
|
|
|
|
ret = ich_get_mmap_bus(dev, &map_base, &map_size, &offset);
|
|
|
|
if (ret)
|
|
|
|
return ret;
|
|
|
|
|
|
|
|
/* Don't use WRBACK since we are not supposed to write to SPI flash */
|
|
|
|
base = SZ_4G - map_size;
|
|
|
|
mtrr_set_next_var(MTRR_TYPE_WRPROT, base, map_size);
|
|
|
|
log_debug("BIOS cache base=%lx, size=%x\n", base, (uint)map_size);
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2016-01-19 11:19:21 +08:00
|
|
|
static int ich_spi_probe(struct udevice *dev)
|
2015-03-26 23:29:26 +08:00
|
|
|
{
|
2020-12-04 07:55:23 +08:00
|
|
|
struct ich_spi_plat *plat = dev_get_plat(dev);
|
2016-01-19 11:19:21 +08:00
|
|
|
struct ich_spi_priv *priv = dev_get_priv(dev);
|
2015-03-26 23:29:26 +08:00
|
|
|
int ret;
|
|
|
|
|
2016-01-19 11:19:21 +08:00
|
|
|
ret = ich_init_controller(dev, plat, priv);
|
2015-03-26 23:29:26 +08:00
|
|
|
if (ret)
|
|
|
|
return ret;
|
|
|
|
|
2024-09-30 09:49:36 +08:00
|
|
|
if (xpl_phase() == PHASE_TPL) {
|
2019-12-07 12:42:48 +08:00
|
|
|
/* Cache the BIOS to speed things up */
|
|
|
|
ret = ich_cache_bios_region(dev);
|
|
|
|
if (ret)
|
|
|
|
return ret;
|
|
|
|
} else {
|
|
|
|
ret = ich_protect_lockdown(dev);
|
|
|
|
if (ret)
|
|
|
|
return ret;
|
|
|
|
}
|
2015-03-26 23:29:26 +08:00
|
|
|
priv->cur_speed = priv->max_speed;
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2017-04-24 15:48:04 +08:00
|
|
|
static int ich_spi_remove(struct udevice *bus)
|
|
|
|
{
|
|
|
|
/*
|
|
|
|
* Configure SPI controller so that the Linux MTD driver can fully
|
|
|
|
* access the SPI NOR chip
|
|
|
|
*/
|
2017-08-16 13:38:30 +08:00
|
|
|
ich_spi_config_opcode(bus);
|
2017-04-24 15:48:04 +08:00
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2015-03-26 23:29:26 +08:00
|
|
|
static int ich_spi_set_speed(struct udevice *bus, uint speed)
|
|
|
|
{
|
|
|
|
struct ich_spi_priv *priv = dev_get_priv(bus);
|
|
|
|
|
|
|
|
priv->cur_speed = speed;
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
static int ich_spi_set_mode(struct udevice *bus, uint mode)
|
|
|
|
{
|
|
|
|
debug("%s: mode=%d\n", __func__, mode);
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
static int ich_spi_child_pre_probe(struct udevice *dev)
|
|
|
|
{
|
|
|
|
struct udevice *bus = dev_get_parent(dev);
|
2020-12-04 07:55:23 +08:00
|
|
|
struct ich_spi_plat *plat = dev_get_plat(bus);
|
2015-03-26 23:29:26 +08:00
|
|
|
struct ich_spi_priv *priv = dev_get_priv(bus);
|
2015-09-29 13:32:01 +08:00
|
|
|
struct spi_slave *slave = dev_get_parent_priv(dev);
|
2015-03-26 23:29:26 +08:00
|
|
|
|
|
|
|
/*
|
2021-07-28 18:28:56 +08:00
|
|
|
* Yes this controller can only transfer a small number of bytes at
|
2019-12-07 12:42:46 +08:00
|
|
|
* once! The limit is typically 64 bytes. For hardware sequencing a
|
|
|
|
* a loop is used to get around this.
|
2015-03-26 23:29:26 +08:00
|
|
|
*/
|
2021-07-28 18:28:56 +08:00
|
|
|
if (!plat->hwseq) {
|
|
|
|
slave->max_read_size = priv->databytes;
|
2019-12-07 12:42:46 +08:00
|
|
|
slave->max_write_size = priv->databytes;
|
2021-07-28 18:28:56 +08:00
|
|
|
}
|
2015-03-26 23:29:26 +08:00
|
|
|
/*
|
|
|
|
* ICH 7 SPI controller only supports array read command
|
|
|
|
* and byte program command for SST flash
|
|
|
|
*/
|
2016-08-08 19:42:12 +08:00
|
|
|
if (plat->ich_version == ICHV_7)
|
|
|
|
slave->mode = SPI_RX_SLOW | SPI_TX_BYTE;
|
2015-03-26 23:29:26 +08:00
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2020-12-04 07:55:21 +08:00
|
|
|
static int ich_spi_of_to_plat(struct udevice *dev)
|
2016-02-01 17:40:37 +08:00
|
|
|
{
|
2020-12-04 07:55:23 +08:00
|
|
|
struct ich_spi_plat *plat = dev_get_plat(dev);
|
2019-12-07 12:42:45 +08:00
|
|
|
|
2021-08-07 21:24:04 +08:00
|
|
|
#if CONFIG_IS_ENABLED(OF_REAL)
|
2019-12-07 12:42:38 +08:00
|
|
|
struct ich_spi_priv *priv = dev_get_priv(dev);
|
2020-10-04 01:31:38 +08:00
|
|
|
int ret;
|
2016-02-01 17:40:37 +08:00
|
|
|
|
2020-05-27 20:58:47 +08:00
|
|
|
ret = ich_spi_get_basics(dev, true, &priv->pch, &plat->ich_version,
|
|
|
|
&plat->mmio_base);
|
|
|
|
if (ret)
|
|
|
|
return log_msg_ret("basics", ret);
|
2019-12-07 12:42:39 +08:00
|
|
|
plat->lockdown = dev_read_bool(dev, "intel,spi-lock-down");
|
2019-12-07 12:42:46 +08:00
|
|
|
/*
|
|
|
|
* Use an int so that the property is present in of-platdata even
|
|
|
|
* when false.
|
|
|
|
*/
|
|
|
|
plat->hwseq = dev_read_u32_default(dev, "intel,hardware-seq", 0);
|
2019-12-07 12:42:45 +08:00
|
|
|
#else
|
|
|
|
plat->ich_version = ICHV_APL;
|
|
|
|
plat->mmio_base = plat->dtplat.early_regs[0];
|
|
|
|
plat->bdf = pci_ofplat_get_devfn(plat->dtplat.reg[0]);
|
2019-12-07 12:42:46 +08:00
|
|
|
plat->hwseq = plat->dtplat.intel_hardware_seq;
|
2019-12-07 12:42:45 +08:00
|
|
|
#endif
|
|
|
|
debug("%s: mmio_base=%lx\n", __func__, plat->mmio_base);
|
2019-12-07 12:42:42 +08:00
|
|
|
|
2019-12-07 12:42:39 +08:00
|
|
|
return 0;
|
2016-02-01 17:40:37 +08:00
|
|
|
}
|
|
|
|
|
2019-08-02 14:38:34 +08:00
|
|
|
static const struct spi_controller_mem_ops ich_controller_mem_ops = {
|
|
|
|
.adjust_op_size = ich_spi_adjust_size,
|
|
|
|
.supports_op = NULL,
|
|
|
|
.exec_op = ich_spi_exec_op,
|
|
|
|
};
|
|
|
|
|
2015-03-26 23:29:26 +08:00
|
|
|
static const struct dm_spi_ops ich_spi_ops = {
|
2019-12-07 12:42:35 +08:00
|
|
|
/* xfer is not supported */
|
2015-03-26 23:29:26 +08:00
|
|
|
.set_speed = ich_spi_set_speed,
|
|
|
|
.set_mode = ich_spi_set_mode,
|
2019-08-02 14:38:34 +08:00
|
|
|
.mem_ops = &ich_controller_mem_ops,
|
2019-12-07 12:42:47 +08:00
|
|
|
.get_mmap = ich_get_mmap,
|
2015-03-26 23:29:26 +08:00
|
|
|
/*
|
|
|
|
* cs_info is not needed, since we require all chip selects to be
|
|
|
|
* in the device tree explicitly
|
|
|
|
*/
|
|
|
|
};
|
|
|
|
|
|
|
|
static const struct udevice_id ich_spi_ids[] = {
|
2019-12-07 12:42:39 +08:00
|
|
|
{ .compatible = "intel,ich7-spi", ICHV_7 },
|
|
|
|
{ .compatible = "intel,ich9-spi", ICHV_9 },
|
2019-12-07 12:42:49 +08:00
|
|
|
{ .compatible = "intel,fast-spi", ICHV_APL },
|
2015-03-26 23:29:26 +08:00
|
|
|
{ }
|
|
|
|
};
|
|
|
|
|
2019-12-07 12:42:45 +08:00
|
|
|
U_BOOT_DRIVER(intel_fast_spi) = {
|
|
|
|
.name = "intel_fast_spi",
|
2015-03-26 23:29:26 +08:00
|
|
|
.id = UCLASS_SPI,
|
|
|
|
.of_match = ich_spi_ids,
|
|
|
|
.ops = &ich_spi_ops,
|
2020-12-04 07:55:21 +08:00
|
|
|
.of_to_plat = ich_spi_of_to_plat,
|
2020-12-04 07:55:23 +08:00
|
|
|
.plat_auto = sizeof(struct ich_spi_plat),
|
2020-12-04 07:55:17 +08:00
|
|
|
.priv_auto = sizeof(struct ich_spi_priv),
|
2015-03-26 23:29:26 +08:00
|
|
|
.child_pre_probe = ich_spi_child_pre_probe,
|
|
|
|
.probe = ich_spi_probe,
|
2017-04-24 15:48:04 +08:00
|
|
|
.remove = ich_spi_remove,
|
|
|
|
.flags = DM_FLAG_OS_PREPARE,
|
2015-03-26 23:29:26 +08:00
|
|
|
};
|