2018-05-06 21:58:06 +00:00
|
|
|
// SPDX-License-Identifier: GPL-2.0+
|
2013-03-19 04:58:56 +00:00
|
|
|
/*
|
|
|
|
* Copyright (c) 2011-12 The Chromium OS Authors.
|
|
|
|
*
|
|
|
|
* This file is derived from the flashrom project.
|
|
|
|
*/
|
2016-02-01 09:40:36 +00:00
|
|
|
|
2013-03-19 04:58:56 +00:00
|
|
|
#include <common.h>
|
2015-03-26 15:29:26 +00:00
|
|
|
#include <dm.h>
|
2015-01-28 05:13:43 +00:00
|
|
|
#include <errno.h>
|
2013-03-19 04:58:56 +00:00
|
|
|
#include <malloc.h>
|
2016-01-19 03:19:21 +00:00
|
|
|
#include <pch.h>
|
2013-03-19 04:58:56 +00:00
|
|
|
#include <pci.h>
|
|
|
|
#include <pci_ids.h>
|
2016-01-19 03:19:21 +00:00
|
|
|
#include <spi.h>
|
2013-03-19 04:58:56 +00:00
|
|
|
#include <asm/io.h>
|
|
|
|
|
|
|
|
#include "ich.h"
|
|
|
|
|
2016-02-01 09:40:37 +00:00
|
|
|
DECLARE_GLOBAL_DATA_PTR;
|
|
|
|
|
2016-01-19 03:19:20 +00:00
|
|
|
#ifdef DEBUG_TRACE
|
|
|
|
#define debug_trace(fmt, args...) debug(fmt, ##args)
|
|
|
|
#else
|
|
|
|
#define debug_trace(x, args...)
|
|
|
|
#endif
|
|
|
|
|
2015-03-26 15:29:26 +00:00
|
|
|
static u8 ich_readb(struct ich_spi_priv *priv, int reg)
|
2013-03-19 04:58:56 +00:00
|
|
|
{
|
2015-03-26 15:29:26 +00:00
|
|
|
u8 value = readb(priv->base + reg);
|
2013-03-19 04:58:56 +00:00
|
|
|
|
2016-01-19 03:19:20 +00:00
|
|
|
debug_trace("read %2.2x from %4.4x\n", value, reg);
|
2013-03-19 04:58:56 +00:00
|
|
|
|
|
|
|
return value;
|
|
|
|
}
|
|
|
|
|
2015-03-26 15:29:26 +00:00
|
|
|
static u16 ich_readw(struct ich_spi_priv *priv, int reg)
|
2013-03-19 04:58:56 +00:00
|
|
|
{
|
2015-03-26 15:29:26 +00:00
|
|
|
u16 value = readw(priv->base + reg);
|
2013-03-19 04:58:56 +00:00
|
|
|
|
2016-01-19 03:19:20 +00:00
|
|
|
debug_trace("read %4.4x from %4.4x\n", value, reg);
|
2013-03-19 04:58:56 +00:00
|
|
|
|
|
|
|
return value;
|
|
|
|
}
|
|
|
|
|
2015-03-26 15:29:26 +00:00
|
|
|
static u32 ich_readl(struct ich_spi_priv *priv, int reg)
|
2013-03-19 04:58:56 +00:00
|
|
|
{
|
2015-03-26 15:29:26 +00:00
|
|
|
u32 value = readl(priv->base + reg);
|
2013-03-19 04:58:56 +00:00
|
|
|
|
2016-01-19 03:19:20 +00:00
|
|
|
debug_trace("read %8.8x from %4.4x\n", value, reg);
|
2013-03-19 04:58:56 +00:00
|
|
|
|
|
|
|
return value;
|
|
|
|
}
|
|
|
|
|
2015-03-26 15:29:26 +00:00
|
|
|
static void ich_writeb(struct ich_spi_priv *priv, u8 value, int reg)
|
2013-03-19 04:58:56 +00:00
|
|
|
{
|
2015-03-26 15:29:26 +00:00
|
|
|
writeb(value, priv->base + reg);
|
2016-01-19 03:19:20 +00:00
|
|
|
debug_trace("wrote %2.2x to %4.4x\n", value, reg);
|
2013-03-19 04:58:56 +00:00
|
|
|
}
|
|
|
|
|
2015-03-26 15:29:26 +00:00
|
|
|
static void ich_writew(struct ich_spi_priv *priv, u16 value, int reg)
|
2013-03-19 04:58:56 +00:00
|
|
|
{
|
2015-03-26 15:29:26 +00:00
|
|
|
writew(value, priv->base + reg);
|
2016-01-19 03:19:20 +00:00
|
|
|
debug_trace("wrote %4.4x to %4.4x\n", value, reg);
|
2013-03-19 04:58:56 +00:00
|
|
|
}
|
|
|
|
|
2015-03-26 15:29:26 +00:00
|
|
|
static void ich_writel(struct ich_spi_priv *priv, u32 value, int reg)
|
2013-03-19 04:58:56 +00:00
|
|
|
{
|
2015-03-26 15:29:26 +00:00
|
|
|
writel(value, priv->base + reg);
|
2016-01-19 03:19:20 +00:00
|
|
|
debug_trace("wrote %8.8x to %4.4x\n", value, reg);
|
2013-03-19 04:58:56 +00:00
|
|
|
}
|
|
|
|
|
2015-03-26 15:29:26 +00:00
|
|
|
static void write_reg(struct ich_spi_priv *priv, const void *value,
|
|
|
|
int dest_reg, uint32_t size)
|
2013-03-19 04:58:56 +00:00
|
|
|
{
|
2015-03-26 15:29:26 +00:00
|
|
|
memcpy_toio(priv->base + dest_reg, value, size);
|
2013-03-19 04:58:56 +00:00
|
|
|
}
|
|
|
|
|
2015-03-26 15:29:26 +00:00
|
|
|
static void read_reg(struct ich_spi_priv *priv, int src_reg, void *value,
|
|
|
|
uint32_t size)
|
2013-03-19 04:58:56 +00:00
|
|
|
{
|
2015-03-26 15:29:26 +00:00
|
|
|
memcpy_fromio(value, priv->base + src_reg, size);
|
2013-03-19 04:58:56 +00:00
|
|
|
}
|
|
|
|
|
2015-03-26 15:29:26 +00:00
|
|
|
static void ich_set_bbar(struct ich_spi_priv *ctlr, uint32_t minaddr)
|
2013-03-19 04:58:56 +00:00
|
|
|
{
|
|
|
|
const uint32_t bbar_mask = 0x00ffff00;
|
|
|
|
uint32_t ichspi_bbar;
|
|
|
|
|
|
|
|
minaddr &= bbar_mask;
|
2015-03-26 15:29:26 +00:00
|
|
|
ichspi_bbar = ich_readl(ctlr, ctlr->bbar) & ~bbar_mask;
|
2013-03-19 04:58:56 +00:00
|
|
|
ichspi_bbar |= minaddr;
|
2015-03-26 15:29:26 +00:00
|
|
|
ich_writel(ctlr, ichspi_bbar, ctlr->bbar);
|
2013-03-19 04:58:56 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/* @return 1 if the SPI flash supports the 33MHz speed */
|
2016-01-19 03:19:21 +00:00
|
|
|
static int ich9_can_do_33mhz(struct udevice *dev)
|
2013-03-19 04:58:56 +00:00
|
|
|
{
|
|
|
|
u32 fdod, speed;
|
|
|
|
|
|
|
|
/* Observe SPI Descriptor Component Section 0 */
|
2016-01-19 03:19:21 +00:00
|
|
|
dm_pci_write_config32(dev->parent, 0xb0, 0x1000);
|
2013-03-19 04:58:56 +00:00
|
|
|
|
|
|
|
/* Extract the Write/Erase SPI Frequency from descriptor */
|
2016-01-19 03:19:21 +00:00
|
|
|
dm_pci_read_config32(dev->parent, 0xb4, &fdod);
|
2013-03-19 04:58:56 +00:00
|
|
|
|
|
|
|
/* Bits 23:21 have the fast read clock frequency, 0=20MHz, 1=33MHz */
|
|
|
|
speed = (fdod >> 21) & 7;
|
|
|
|
|
|
|
|
return speed == 1;
|
|
|
|
}
|
|
|
|
|
2016-01-19 03:19:21 +00:00
|
|
|
static int ich_init_controller(struct udevice *dev,
|
|
|
|
struct ich_spi_platdata *plat,
|
2015-03-26 15:29:26 +00:00
|
|
|
struct ich_spi_priv *ctlr)
|
2013-03-19 04:58:56 +00:00
|
|
|
{
|
2016-01-19 03:19:21 +00:00
|
|
|
ulong sbase_addr;
|
|
|
|
void *sbase;
|
2015-01-28 05:13:43 +00:00
|
|
|
|
|
|
|
/* SBASE is similar */
|
2016-02-01 09:40:42 +00:00
|
|
|
pch_get_spi_base(dev->parent, &sbase_addr);
|
2016-01-19 03:19:21 +00:00
|
|
|
sbase = (void *)sbase_addr;
|
|
|
|
debug("%s: sbase=%p\n", __func__, sbase);
|
2015-01-28 05:13:43 +00:00
|
|
|
|
2016-02-01 09:40:38 +00:00
|
|
|
if (plat->ich_version == ICHV_7) {
|
2016-01-19 03:19:21 +00:00
|
|
|
struct ich7_spi_regs *ich7_spi = sbase;
|
2013-03-19 04:58:56 +00:00
|
|
|
|
2015-03-26 15:29:26 +00:00
|
|
|
ctlr->opmenu = offsetof(struct ich7_spi_regs, opmenu);
|
2013-03-19 04:58:56 +00:00
|
|
|
ctlr->menubytes = sizeof(ich7_spi->opmenu);
|
2015-03-26 15:29:26 +00:00
|
|
|
ctlr->optype = offsetof(struct ich7_spi_regs, optype);
|
|
|
|
ctlr->addr = offsetof(struct ich7_spi_regs, spia);
|
|
|
|
ctlr->data = offsetof(struct ich7_spi_regs, spid);
|
2013-03-19 04:58:56 +00:00
|
|
|
ctlr->databytes = sizeof(ich7_spi->spid);
|
2015-03-26 15:29:26 +00:00
|
|
|
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);
|
2013-03-19 04:58:56 +00:00
|
|
|
ctlr->base = ich7_spi;
|
2016-02-01 09:40:38 +00:00
|
|
|
} else if (plat->ich_version == ICHV_9) {
|
2016-01-19 03:19:21 +00:00
|
|
|
struct ich9_spi_regs *ich9_spi = sbase;
|
2013-03-19 04:58:56 +00:00
|
|
|
|
2015-03-26 15:29:26 +00:00
|
|
|
ctlr->opmenu = offsetof(struct ich9_spi_regs, opmenu);
|
2013-03-19 04:58:56 +00:00
|
|
|
ctlr->menubytes = sizeof(ich9_spi->opmenu);
|
2015-03-26 15:29:26 +00:00
|
|
|
ctlr->optype = offsetof(struct ich9_spi_regs, optype);
|
|
|
|
ctlr->addr = offsetof(struct ich9_spi_regs, faddr);
|
|
|
|
ctlr->data = offsetof(struct ich9_spi_regs, fdata);
|
2013-03-19 04:58:56 +00:00
|
|
|
ctlr->databytes = sizeof(ich9_spi->fdata);
|
2015-03-26 15:29:26 +00:00
|
|
|
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);
|
2015-07-04 00:28:22 +00:00
|
|
|
ctlr->bcr = offsetof(struct ich9_spi_regs, bcr);
|
2013-03-19 04:58:56 +00:00
|
|
|
ctlr->pr = &ich9_spi->pr[0];
|
|
|
|
ctlr->base = ich9_spi;
|
|
|
|
} else {
|
2015-03-26 15:29:26 +00:00
|
|
|
debug("ICH SPI: Unrecognised ICH version %d\n",
|
|
|
|
plat->ich_version);
|
|
|
|
return -EINVAL;
|
2013-03-19 04:58:56 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/* Work out the maximum speed we can support */
|
|
|
|
ctlr->max_speed = 20000000;
|
2016-02-01 09:40:38 +00:00
|
|
|
if (plat->ich_version == ICHV_9 && ich9_can_do_33mhz(dev))
|
2013-03-19 04:58:56 +00:00
|
|
|
ctlr->max_speed = 33000000;
|
2016-01-19 03:19:21 +00:00
|
|
|
debug("ICH SPI: Version ID %d detected at %p, speed %ld\n",
|
2015-03-26 15:29:26 +00:00
|
|
|
plat->ich_version, ctlr->base, ctlr->max_speed);
|
2013-03-19 04:58:56 +00:00
|
|
|
|
|
|
|
ich_set_bbar(ctlr, 0);
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
static inline void spi_use_out(struct spi_trans *trans, unsigned bytes)
|
|
|
|
{
|
|
|
|
trans->out += bytes;
|
|
|
|
trans->bytesout -= bytes;
|
|
|
|
}
|
|
|
|
|
|
|
|
static inline void spi_use_in(struct spi_trans *trans, unsigned bytes)
|
|
|
|
{
|
|
|
|
trans->in += bytes;
|
|
|
|
trans->bytesin -= bytes;
|
|
|
|
}
|
|
|
|
|
2017-10-19 01:20:57 +00:00
|
|
|
static void spi_lock_down(struct ich_spi_platdata *plat, void *sbase)
|
|
|
|
{
|
|
|
|
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);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-08-16 05:38:29 +00:00
|
|
|
static bool spi_lock_status(struct ich_spi_platdata *plat, void *sbase)
|
|
|
|
{
|
|
|
|
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;
|
|
|
|
}
|
|
|
|
|
2013-03-19 04:58:56 +00:00
|
|
|
static void spi_setup_type(struct spi_trans *trans, int data_bytes)
|
|
|
|
{
|
|
|
|
trans->type = 0xFF;
|
|
|
|
|
2016-02-01 09:40:36 +00:00
|
|
|
/* Try to guess spi type from read/write sizes */
|
2013-03-19 04:58:56 +00:00
|
|
|
if (trans->bytesin == 0) {
|
|
|
|
if (trans->bytesout + data_bytes > 4)
|
|
|
|
/*
|
|
|
|
* If bytesin = 0 and bytesout > 4, we presume this is
|
|
|
|
* a write data operation, which is accompanied by an
|
|
|
|
* address.
|
|
|
|
*/
|
|
|
|
trans->type = SPI_OPCODE_TYPE_WRITE_WITH_ADDRESS;
|
|
|
|
else
|
|
|
|
trans->type = SPI_OPCODE_TYPE_WRITE_NO_ADDRESS;
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (trans->bytesout == 1) { /* and bytesin is > 0 */
|
|
|
|
trans->type = SPI_OPCODE_TYPE_READ_NO_ADDRESS;
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (trans->bytesout == 4) /* and bytesin is > 0 */
|
|
|
|
trans->type = SPI_OPCODE_TYPE_READ_WITH_ADDRESS;
|
|
|
|
|
|
|
|
/* Fast read command is called with 5 bytes instead of 4 */
|
|
|
|
if (trans->out[0] == SPI_OPCODE_FAST_READ && trans->bytesout == 5) {
|
|
|
|
trans->type = SPI_OPCODE_TYPE_READ_WITH_ADDRESS;
|
|
|
|
--trans->bytesout;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-08-16 05:38:29 +00:00
|
|
|
static int spi_setup_opcode(struct ich_spi_priv *ctlr, struct spi_trans *trans,
|
|
|
|
bool lock)
|
2013-03-19 04:58:56 +00:00
|
|
|
{
|
|
|
|
uint16_t optypes;
|
2015-03-26 15:29:26 +00:00
|
|
|
uint8_t opmenu[ctlr->menubytes];
|
2013-03-19 04:58:56 +00:00
|
|
|
|
|
|
|
trans->opcode = trans->out[0];
|
|
|
|
spi_use_out(trans, 1);
|
2017-08-16 05:38:29 +00:00
|
|
|
if (!lock) {
|
2013-03-19 04:58:56 +00:00
|
|
|
/* The lock is off, so just use index 0. */
|
2015-03-26 15:29:26 +00:00
|
|
|
ich_writeb(ctlr, trans->opcode, ctlr->opmenu);
|
|
|
|
optypes = ich_readw(ctlr, ctlr->optype);
|
2013-03-19 04:58:56 +00:00
|
|
|
optypes = (optypes & 0xfffc) | (trans->type & 0x3);
|
2015-03-26 15:29:26 +00:00
|
|
|
ich_writew(ctlr, optypes, ctlr->optype);
|
2013-03-19 04:58:56 +00: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 15:29:26 +00:00
|
|
|
read_reg(ctlr, ctlr->opmenu, opmenu, sizeof(opmenu));
|
|
|
|
for (opcode_index = 0; opcode_index < ctlr->menubytes;
|
2013-03-19 04:58:56 +00:00
|
|
|
opcode_index++) {
|
|
|
|
if (opmenu[opcode_index] == trans->opcode)
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
2015-03-26 15:29:26 +00:00
|
|
|
if (opcode_index == ctlr->menubytes) {
|
2013-03-19 04:58:56 +00:00
|
|
|
printf("ICH SPI: Opcode %x not found\n",
|
|
|
|
trans->opcode);
|
2015-03-26 15:29:26 +00:00
|
|
|
return -EINVAL;
|
2013-03-19 04:58:56 +00:00
|
|
|
}
|
|
|
|
|
2015-03-26 15:29:26 +00:00
|
|
|
optypes = ich_readw(ctlr, ctlr->optype);
|
2013-03-19 04:58:56 +00:00
|
|
|
optype = (optypes >> (opcode_index * 2)) & 0x3;
|
|
|
|
if (trans->type == SPI_OPCODE_TYPE_WRITE_NO_ADDRESS &&
|
|
|
|
optype == SPI_OPCODE_TYPE_WRITE_WITH_ADDRESS &&
|
|
|
|
trans->bytesout >= 3) {
|
|
|
|
/* We guessed wrong earlier. Fix it up. */
|
|
|
|
trans->type = optype;
|
|
|
|
}
|
|
|
|
if (optype != trans->type) {
|
|
|
|
printf("ICH SPI: Transaction doesn't fit type %d\n",
|
|
|
|
optype);
|
2015-03-26 15:29:26 +00:00
|
|
|
return -ENOSPC;
|
2013-03-19 04:58:56 +00:00
|
|
|
}
|
|
|
|
return opcode_index;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
static int spi_setup_offset(struct spi_trans *trans)
|
|
|
|
{
|
2016-02-01 09:40:36 +00:00
|
|
|
/* Separate the SPI address and data */
|
2013-03-19 04:58:56 +00:00
|
|
|
switch (trans->type) {
|
|
|
|
case SPI_OPCODE_TYPE_READ_NO_ADDRESS:
|
|
|
|
case SPI_OPCODE_TYPE_WRITE_NO_ADDRESS:
|
|
|
|
return 0;
|
|
|
|
case SPI_OPCODE_TYPE_READ_WITH_ADDRESS:
|
|
|
|
case SPI_OPCODE_TYPE_WRITE_WITH_ADDRESS:
|
|
|
|
trans->offset = ((uint32_t)trans->out[0] << 16) |
|
|
|
|
((uint32_t)trans->out[1] << 8) |
|
|
|
|
((uint32_t)trans->out[2] << 0);
|
|
|
|
spi_use_out(trans, 3);
|
|
|
|
return 1;
|
|
|
|
default:
|
|
|
|
printf("Unrecognized SPI transaction type %#x\n", trans->type);
|
2015-03-26 15:29:26 +00:00
|
|
|
return -EPROTO;
|
2013-03-19 04:58:56 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Wait for up to 6s til status register bit(s) turn 1 (in case wait_til_set
|
2013-04-01 18:29:11 +00:00
|
|
|
* below is true) or 0. In case the wait was for the bit(s) to set - write
|
2013-03-19 04:58:56 +00:00
|
|
|
* those bits back, which would cause resetting them.
|
|
|
|
*
|
|
|
|
* Return the last read status value on success or -1 on failure.
|
|
|
|
*/
|
2015-03-26 15:29:26 +00:00
|
|
|
static int ich_status_poll(struct ich_spi_priv *ctlr, u16 bitmask,
|
|
|
|
int wait_til_set)
|
2013-03-19 04:58:56 +00:00
|
|
|
{
|
|
|
|
int timeout = 600000; /* This will result in 6s */
|
|
|
|
u16 status = 0;
|
|
|
|
|
|
|
|
while (timeout--) {
|
2015-03-26 15:29:26 +00:00
|
|
|
status = ich_readw(ctlr, ctlr->status);
|
2013-03-19 04:58:56 +00:00
|
|
|
if (wait_til_set ^ ((status & bitmask) == 0)) {
|
2015-03-26 15:29:26 +00:00
|
|
|
if (wait_til_set) {
|
|
|
|
ich_writew(ctlr, status & bitmask,
|
|
|
|
ctlr->status);
|
|
|
|
}
|
2013-03-19 04:58:56 +00:00
|
|
|
return status;
|
|
|
|
}
|
|
|
|
udelay(10);
|
|
|
|
}
|
|
|
|
|
|
|
|
printf("ICH SPI: SCIP timeout, read %x, expected %x\n",
|
|
|
|
status, bitmask);
|
2015-03-26 15:29:26 +00:00
|
|
|
return -ETIMEDOUT;
|
2013-03-19 04:58:56 +00:00
|
|
|
}
|
|
|
|
|
2017-08-16 05:38:30 +00:00
|
|
|
void ich_spi_config_opcode(struct udevice *dev)
|
|
|
|
{
|
|
|
|
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));
|
|
|
|
}
|
|
|
|
|
2015-03-26 15:29:26 +00:00
|
|
|
static int ich_spi_xfer(struct udevice *dev, unsigned int bitlen,
|
|
|
|
const void *dout, void *din, unsigned long flags)
|
2013-03-19 04:58:56 +00:00
|
|
|
{
|
2015-03-26 15:29:26 +00:00
|
|
|
struct udevice *bus = dev_get_parent(dev);
|
2015-07-04 00:28:21 +00:00
|
|
|
struct ich_spi_platdata *plat = dev_get_platdata(bus);
|
2015-03-26 15:29:26 +00:00
|
|
|
struct ich_spi_priv *ctlr = dev_get_priv(bus);
|
2013-03-19 04:58:56 +00:00
|
|
|
uint16_t control;
|
|
|
|
int16_t opcode_index;
|
|
|
|
int with_address;
|
|
|
|
int status;
|
|
|
|
int bytes = bitlen / 8;
|
2015-03-26 15:29:26 +00:00
|
|
|
struct spi_trans *trans = &ctlr->trans;
|
2013-03-19 04:58:56 +00:00
|
|
|
unsigned type = flags & (SPI_XFER_BEGIN | SPI_XFER_END);
|
|
|
|
int using_cmd = 0;
|
2017-08-16 05:38:29 +00:00
|
|
|
bool lock = spi_lock_status(plat, ctlr->base);
|
2015-03-26 15:29:26 +00:00
|
|
|
int ret;
|
2013-03-19 04:58:56 +00:00
|
|
|
|
2015-06-07 14:50:33 +00:00
|
|
|
/* We don't support writing partial bytes */
|
2013-03-19 04:58:56 +00:00
|
|
|
if (bitlen % 8) {
|
|
|
|
debug("ICH SPI: Accessing partial bytes not supported\n");
|
2015-03-26 15:29:26 +00:00
|
|
|
return -EPROTONOSUPPORT;
|
2013-03-19 04:58:56 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/* An empty end transaction can be ignored */
|
|
|
|
if (type == SPI_XFER_END && !dout && !din)
|
|
|
|
return 0;
|
|
|
|
|
|
|
|
if (type & SPI_XFER_BEGIN)
|
|
|
|
memset(trans, '\0', sizeof(*trans));
|
|
|
|
|
|
|
|
/* Dp we need to come back later to finish it? */
|
|
|
|
if (dout && type == SPI_XFER_BEGIN) {
|
|
|
|
if (bytes > ICH_MAX_CMD_LEN) {
|
|
|
|
debug("ICH SPI: Command length limit exceeded\n");
|
2015-03-26 15:29:26 +00:00
|
|
|
return -ENOSPC;
|
2013-03-19 04:58:56 +00:00
|
|
|
}
|
|
|
|
memcpy(trans->cmd, dout, bytes);
|
|
|
|
trans->cmd_len = bytes;
|
2016-01-19 03:19:20 +00:00
|
|
|
debug_trace("ICH SPI: Saved %d bytes\n", bytes);
|
2013-03-19 04:58:56 +00:00
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* We process a 'middle' spi_xfer() call, which has no
|
|
|
|
* SPI_XFER_BEGIN/END, as an independent transaction as if it had
|
|
|
|
* an end. We therefore repeat the command. This is because ICH
|
|
|
|
* seems to have no support for this, or because interest (in digging
|
|
|
|
* out the details and creating a special case in the code) is low.
|
|
|
|
*/
|
|
|
|
if (trans->cmd_len) {
|
|
|
|
trans->out = trans->cmd;
|
|
|
|
trans->bytesout = trans->cmd_len;
|
|
|
|
using_cmd = 1;
|
2016-01-19 03:19:20 +00:00
|
|
|
debug_trace("ICH SPI: Using %d bytes\n", trans->cmd_len);
|
2013-03-19 04:58:56 +00:00
|
|
|
} else {
|
|
|
|
trans->out = dout;
|
|
|
|
trans->bytesout = dout ? bytes : 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
trans->in = din;
|
|
|
|
trans->bytesin = din ? bytes : 0;
|
|
|
|
|
2016-02-01 09:40:36 +00:00
|
|
|
/* There has to always at least be an opcode */
|
2013-03-19 04:58:56 +00:00
|
|
|
if (!trans->bytesout) {
|
|
|
|
debug("ICH SPI: No opcode for transfer\n");
|
2015-03-26 15:29:26 +00:00
|
|
|
return -EPROTO;
|
2013-03-19 04:58:56 +00:00
|
|
|
}
|
|
|
|
|
2015-03-26 15:29:26 +00:00
|
|
|
ret = ich_status_poll(ctlr, SPIS_SCIP, 0);
|
|
|
|
if (ret < 0)
|
|
|
|
return ret;
|
2013-03-19 04:58:56 +00:00
|
|
|
|
2016-02-01 09:40:38 +00:00
|
|
|
if (plat->ich_version == ICHV_7)
|
2015-07-04 00:28:21 +00:00
|
|
|
ich_writew(ctlr, SPIS_CDS | SPIS_FCERR, ctlr->status);
|
|
|
|
else
|
|
|
|
ich_writeb(ctlr, SPIS_CDS | SPIS_FCERR, ctlr->status);
|
2013-03-19 04:58:56 +00:00
|
|
|
|
|
|
|
spi_setup_type(trans, using_cmd ? bytes : 0);
|
2017-08-16 05:38:29 +00:00
|
|
|
opcode_index = spi_setup_opcode(ctlr, trans, lock);
|
2013-03-19 04:58:56 +00:00
|
|
|
if (opcode_index < 0)
|
2015-03-26 15:29:26 +00:00
|
|
|
return -EINVAL;
|
2013-03-19 04:58:56 +00:00
|
|
|
with_address = spi_setup_offset(trans);
|
|
|
|
if (with_address < 0)
|
2015-03-26 15:29:26 +00:00
|
|
|
return -EINVAL;
|
2013-03-19 04:58:56 +00: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.
|
|
|
|
*/
|
2017-08-16 05:38:29 +00:00
|
|
|
if (!lock)
|
2015-03-26 15:29:26 +00:00
|
|
|
ich_writew(ctlr, trans->opcode, ctlr->preop);
|
2013-03-19 04:58:56 +00:00
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2015-03-26 15:29:26 +00:00
|
|
|
if (ctlr->speed && ctlr->max_speed >= 33000000) {
|
2013-03-19 04:58:56 +00:00
|
|
|
int byte;
|
|
|
|
|
2015-03-26 15:29:26 +00:00
|
|
|
byte = ich_readb(ctlr, ctlr->speed);
|
|
|
|
if (ctlr->cur_speed >= 33000000)
|
2013-03-19 04:58:56 +00:00
|
|
|
byte |= SSFC_SCF_33MHZ;
|
|
|
|
else
|
|
|
|
byte &= ~SSFC_SCF_33MHZ;
|
2015-03-26 15:29:26 +00:00
|
|
|
ich_writeb(ctlr, byte, ctlr->speed);
|
2013-03-19 04:58:56 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/* See if we have used up the command data */
|
|
|
|
if (using_cmd && dout && bytes) {
|
|
|
|
trans->out = dout;
|
|
|
|
trans->bytesout = bytes;
|
2016-01-19 03:19:20 +00:00
|
|
|
debug_trace("ICH SPI: Moving to data, %d bytes\n", bytes);
|
2013-03-19 04:58:56 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/* Preset control fields */
|
|
|
|
control = SPIC_SCGO | ((opcode_index & 0x07) << 4);
|
|
|
|
|
|
|
|
/* Issue atomic preop cycle if needed */
|
2015-03-26 15:29:26 +00:00
|
|
|
if (ich_readw(ctlr, ctlr->preop))
|
2013-03-19 04:58:56 +00:00
|
|
|
control |= SPIC_ACS;
|
|
|
|
|
|
|
|
if (!trans->bytesout && !trans->bytesin) {
|
|
|
|
/* SPI addresses are 24 bit only */
|
2015-03-26 15:29:26 +00:00
|
|
|
if (with_address) {
|
|
|
|
ich_writel(ctlr, trans->offset & 0x00FFFFFF,
|
|
|
|
ctlr->addr);
|
|
|
|
}
|
2013-03-19 04:58:56 +00: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 15:29:26 +00:00
|
|
|
ich_writew(ctlr, control, ctlr->control);
|
2013-03-19 04:58:56 +00:00
|
|
|
|
|
|
|
/* wait for the result */
|
2015-03-26 15:29:26 +00:00
|
|
|
status = ich_status_poll(ctlr, SPIS_CDS | SPIS_FCERR, 1);
|
|
|
|
if (status < 0)
|
|
|
|
return status;
|
2013-03-19 04:58:56 +00:00
|
|
|
|
|
|
|
if (status & SPIS_FCERR) {
|
|
|
|
debug("ICH SPI: Command transaction error\n");
|
2015-03-26 15:29:26 +00:00
|
|
|
return -EIO;
|
2013-03-19 04:58:56 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Check if this is a write command atempting to transfer more bytes
|
|
|
|
* than the controller can handle. Iterations for writes are not
|
|
|
|
* supported here because each SPI write command needs to be preceded
|
|
|
|
* and followed by other SPI commands, and this sequence is controlled
|
|
|
|
* by the SPI chip driver.
|
|
|
|
*/
|
2015-03-26 15:29:26 +00:00
|
|
|
if (trans->bytesout > ctlr->databytes) {
|
2013-03-19 04:58:56 +00:00
|
|
|
debug("ICH SPI: Too much to write. This should be prevented by the driver's max_write_size?\n");
|
2015-03-26 15:29:26 +00:00
|
|
|
return -EPROTO;
|
2013-03-19 04:58:56 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Read or write up to databytes bytes at a time until everything has
|
|
|
|
* been sent.
|
|
|
|
*/
|
|
|
|
while (trans->bytesout || trans->bytesin) {
|
|
|
|
uint32_t data_length;
|
|
|
|
|
|
|
|
/* SPI addresses are 24 bit only */
|
2015-03-26 15:29:26 +00:00
|
|
|
ich_writel(ctlr, trans->offset & 0x00FFFFFF, ctlr->addr);
|
2013-03-19 04:58:56 +00:00
|
|
|
|
|
|
|
if (trans->bytesout)
|
2015-03-26 15:29:26 +00:00
|
|
|
data_length = min(trans->bytesout, ctlr->databytes);
|
2013-03-19 04:58:56 +00:00
|
|
|
else
|
2015-03-26 15:29:26 +00:00
|
|
|
data_length = min(trans->bytesin, ctlr->databytes);
|
2013-03-19 04:58:56 +00:00
|
|
|
|
|
|
|
/* Program data into FDATA0 to N */
|
|
|
|
if (trans->bytesout) {
|
2015-03-26 15:29:26 +00:00
|
|
|
write_reg(ctlr, trans->out, ctlr->data, data_length);
|
2013-03-19 04:58:56 +00:00
|
|
|
spi_use_out(trans, data_length);
|
|
|
|
if (with_address)
|
|
|
|
trans->offset += data_length;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Add proper control fields' values */
|
2015-03-26 15:29:26 +00:00
|
|
|
control &= ~((ctlr->databytes - 1) << 8);
|
2013-03-19 04:58:56 +00:00
|
|
|
control |= SPIC_DS;
|
|
|
|
control |= (data_length - 1) << 8;
|
|
|
|
|
|
|
|
/* write it */
|
2015-03-26 15:29:26 +00:00
|
|
|
ich_writew(ctlr, control, ctlr->control);
|
2013-03-19 04:58:56 +00:00
|
|
|
|
2016-02-01 09:40:36 +00:00
|
|
|
/* Wait for Cycle Done Status or Flash Cycle Error */
|
2015-03-26 15:29:26 +00:00
|
|
|
status = ich_status_poll(ctlr, SPIS_CDS | SPIS_FCERR, 1);
|
|
|
|
if (status < 0)
|
|
|
|
return status;
|
2013-03-19 04:58:56 +00:00
|
|
|
|
|
|
|
if (status & SPIS_FCERR) {
|
2015-06-07 14:50:33 +00:00
|
|
|
debug("ICH SPI: Data transaction error %x\n", status);
|
2015-03-26 15:29:26 +00:00
|
|
|
return -EIO;
|
2013-03-19 04:58:56 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
if (trans->bytesin) {
|
2015-03-26 15:29:26 +00:00
|
|
|
read_reg(ctlr, ctlr->data, trans->in, data_length);
|
2013-03-19 04:58:56 +00:00
|
|
|
spi_use_in(trans, data_length);
|
|
|
|
if (with_address)
|
|
|
|
trans->offset += data_length;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Clear atomic preop now that xfer is done */
|
2017-08-27 02:22:59 +00:00
|
|
|
if (!lock)
|
|
|
|
ich_writew(ctlr, 0, ctlr->preop);
|
2013-03-19 04:58:56 +00:00
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2016-01-19 03:19:21 +00:00
|
|
|
static int ich_spi_probe(struct udevice *dev)
|
2015-03-26 15:29:26 +00:00
|
|
|
{
|
2016-01-19 03:19:21 +00:00
|
|
|
struct ich_spi_platdata *plat = dev_get_platdata(dev);
|
|
|
|
struct ich_spi_priv *priv = dev_get_priv(dev);
|
2015-03-26 15:29:26 +00:00
|
|
|
uint8_t bios_cntl;
|
|
|
|
int ret;
|
|
|
|
|
2016-01-19 03:19:21 +00:00
|
|
|
ret = ich_init_controller(dev, plat, priv);
|
2015-03-26 15:29:26 +00:00
|
|
|
if (ret)
|
|
|
|
return ret;
|
2016-01-19 03:19:21 +00:00
|
|
|
/* Disable the BIOS write protect so write commands are allowed */
|
|
|
|
ret = pch_set_spi_protect(dev->parent, false);
|
|
|
|
if (ret == -ENOSYS) {
|
2015-07-04 00:28:22 +00:00
|
|
|
bios_cntl = ich_readb(priv, priv->bcr);
|
2015-10-22 20:07:56 +00:00
|
|
|
bios_cntl &= ~BIT(5); /* clear Enable InSMM_STS (EISS) */
|
2015-03-26 15:29:26 +00:00
|
|
|
bios_cntl |= 1; /* Write Protect Disable (WPD) */
|
2015-07-04 00:28:22 +00:00
|
|
|
ich_writeb(priv, bios_cntl, priv->bcr);
|
2016-01-19 03:19:21 +00:00
|
|
|
} else if (ret) {
|
|
|
|
debug("%s: Failed to disable write-protect: err=%d\n",
|
|
|
|
__func__, ret);
|
|
|
|
return ret;
|
2015-03-26 15:29:26 +00:00
|
|
|
}
|
|
|
|
|
2017-10-19 01:20:57 +00:00
|
|
|
/* Lock down SPI controller settings if required */
|
|
|
|
if (plat->lockdown) {
|
|
|
|
ich_spi_config_opcode(dev);
|
|
|
|
spi_lock_down(plat, priv->base);
|
|
|
|
}
|
|
|
|
|
2015-03-26 15:29:26 +00:00
|
|
|
priv->cur_speed = priv->max_speed;
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2017-04-24 07:48:04 +00: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 05:38:30 +00:00
|
|
|
ich_spi_config_opcode(bus);
|
2017-04-24 07:48:04 +00:00
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2015-03-26 15:29:26 +00: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);
|
|
|
|
struct ich_spi_platdata *plat = dev_get_platdata(bus);
|
|
|
|
struct ich_spi_priv *priv = dev_get_priv(bus);
|
2015-09-29 05:32:01 +00:00
|
|
|
struct spi_slave *slave = dev_get_parent_priv(dev);
|
2015-03-26 15:29:26 +00:00
|
|
|
|
|
|
|
/*
|
|
|
|
* Yes this controller can only write a small number of bytes at
|
|
|
|
* once! The limit is typically 64 bytes.
|
|
|
|
*/
|
|
|
|
slave->max_write_size = priv->databytes;
|
|
|
|
/*
|
|
|
|
* ICH 7 SPI controller only supports array read command
|
|
|
|
* and byte program command for SST flash
|
|
|
|
*/
|
2016-08-08 11:42:12 +00:00
|
|
|
if (plat->ich_version == ICHV_7)
|
|
|
|
slave->mode = SPI_RX_SLOW | SPI_TX_BYTE;
|
2015-03-26 15:29:26 +00:00
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2016-02-01 09:40:37 +00:00
|
|
|
static int ich_spi_ofdata_to_platdata(struct udevice *dev)
|
|
|
|
{
|
|
|
|
struct ich_spi_platdata *plat = dev_get_platdata(dev);
|
2017-01-17 23:52:55 +00:00
|
|
|
int node = dev_of_offset(dev);
|
2016-02-01 09:40:37 +00:00
|
|
|
int ret;
|
|
|
|
|
2017-01-17 23:52:55 +00:00
|
|
|
ret = fdt_node_check_compatible(gd->fdt_blob, node, "intel,ich7-spi");
|
2016-02-01 09:40:37 +00:00
|
|
|
if (ret == 0) {
|
2016-02-01 09:40:38 +00:00
|
|
|
plat->ich_version = ICHV_7;
|
2016-02-01 09:40:37 +00:00
|
|
|
} else {
|
2017-01-17 23:52:55 +00:00
|
|
|
ret = fdt_node_check_compatible(gd->fdt_blob, node,
|
2016-02-01 09:40:37 +00:00
|
|
|
"intel,ich9-spi");
|
|
|
|
if (ret == 0)
|
2016-02-01 09:40:38 +00:00
|
|
|
plat->ich_version = ICHV_9;
|
2016-02-01 09:40:37 +00:00
|
|
|
}
|
|
|
|
|
2017-10-19 01:20:57 +00:00
|
|
|
plat->lockdown = fdtdec_get_bool(gd->fdt_blob, node,
|
|
|
|
"intel,spi-lock-down");
|
|
|
|
|
2016-02-01 09:40:37 +00:00
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
2015-03-26 15:29:26 +00:00
|
|
|
static const struct dm_spi_ops ich_spi_ops = {
|
|
|
|
.xfer = ich_spi_xfer,
|
|
|
|
.set_speed = ich_spi_set_speed,
|
|
|
|
.set_mode = ich_spi_set_mode,
|
|
|
|
/*
|
|
|
|
* 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[] = {
|
2016-02-01 09:40:37 +00:00
|
|
|
{ .compatible = "intel,ich7-spi" },
|
|
|
|
{ .compatible = "intel,ich9-spi" },
|
2015-03-26 15:29:26 +00:00
|
|
|
{ }
|
|
|
|
};
|
|
|
|
|
|
|
|
U_BOOT_DRIVER(ich_spi) = {
|
|
|
|
.name = "ich_spi",
|
|
|
|
.id = UCLASS_SPI,
|
|
|
|
.of_match = ich_spi_ids,
|
|
|
|
.ops = &ich_spi_ops,
|
2016-02-01 09:40:37 +00:00
|
|
|
.ofdata_to_platdata = ich_spi_ofdata_to_platdata,
|
2015-03-26 15:29:26 +00:00
|
|
|
.platdata_auto_alloc_size = sizeof(struct ich_spi_platdata),
|
|
|
|
.priv_auto_alloc_size = sizeof(struct ich_spi_priv),
|
|
|
|
.child_pre_probe = ich_spi_child_pre_probe,
|
|
|
|
.probe = ich_spi_probe,
|
2017-04-24 07:48:04 +00:00
|
|
|
.remove = ich_spi_remove,
|
|
|
|
.flags = DM_FLAG_OS_PREPARE,
|
2015-03-26 15:29:26 +00:00
|
|
|
};
|