]> git.ipfire.org Git - people/ms/u-boot.git/blobdiff - drivers/spi/ich.c
spi: ich: Lock down controller settings if required
[people/ms/u-boot.git] / drivers / spi / ich.c
index 46dd9a873c7e36999ad0e0ff66c7dade4e1f45aa..927bbd708f11ee0be1efc9321fb576453ead45fa 100644 (file)
@@ -126,8 +126,6 @@ static int ich_init_controller(struct udevice *dev,
        if (plat->ich_version == ICHV_7) {
                struct ich7_spi_regs *ich7_spi = sbase;
 
-               ich7_spi = (struct ich7_spi_regs *)sbase;
-               ctlr->ichspi_lock = readw(&ich7_spi->spis) & SPIS_LOCK;
                ctlr->opmenu = offsetof(struct ich7_spi_regs, opmenu);
                ctlr->menubytes = sizeof(ich7_spi->opmenu);
                ctlr->optype = offsetof(struct ich7_spi_regs, optype);
@@ -142,7 +140,6 @@ static int ich_init_controller(struct udevice *dev,
        } else if (plat->ich_version == ICHV_9) {
                struct ich9_spi_regs *ich9_spi = sbase;
 
-               ctlr->ichspi_lock = readw(&ich9_spi->hsfs) & HSFS_FLOCKDN;
                ctlr->opmenu = offsetof(struct ich9_spi_regs, opmenu);
                ctlr->menubytes = sizeof(ich9_spi->opmenu);
                ctlr->optype = offsetof(struct ich9_spi_regs, optype);
@@ -187,6 +184,36 @@ static inline void spi_use_in(struct spi_trans *trans, unsigned bytes)
        trans->bytesin -= bytes;
 }
 
+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);
+       }
+}
+
+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;
+}
+
 static void spi_setup_type(struct spi_trans *trans, int data_bytes)
 {
        trans->type = 0xFF;
@@ -220,14 +247,15 @@ static void spi_setup_type(struct spi_trans *trans, int data_bytes)
        }
 }
 
-static int spi_setup_opcode(struct ich_spi_priv *ctlr, struct spi_trans *trans)
+static int spi_setup_opcode(struct ich_spi_priv *ctlr, struct spi_trans *trans,
+                           bool lock)
 {
        uint16_t optypes;
        uint8_t opmenu[ctlr->menubytes];
 
        trans->opcode = trans->out[0];
        spi_use_out(trans, 1);
-       if (!ctlr->ichspi_lock) {
+       if (!lock) {
                /* The lock is off, so just use index 0. */
                ich_writeb(ctlr, trans->opcode, ctlr->opmenu);
                optypes = ich_readw(ctlr, ctlr->optype);
@@ -323,6 +351,21 @@ static int ich_status_poll(struct ich_spi_priv *ctlr, u16 bitmask,
        return -ETIMEDOUT;
 }
 
+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));
+}
+
 static int ich_spi_xfer(struct udevice *dev, unsigned int bitlen,
                        const void *dout, void *din, unsigned long flags)
 {
@@ -337,6 +380,7 @@ static int ich_spi_xfer(struct udevice *dev, unsigned int bitlen,
        struct spi_trans *trans = &ctlr->trans;
        unsigned type = flags & (SPI_XFER_BEGIN | SPI_XFER_END);
        int using_cmd = 0;
+       bool lock = spi_lock_status(plat, ctlr->base);
        int ret;
 
        /* We don't support writing partial bytes */
@@ -400,7 +444,7 @@ static int ich_spi_xfer(struct udevice *dev, unsigned int bitlen,
                ich_writeb(ctlr, SPIS_CDS | SPIS_FCERR, ctlr->status);
 
        spi_setup_type(trans, using_cmd ? bytes : 0);
-       opcode_index = spi_setup_opcode(ctlr, trans);
+       opcode_index = spi_setup_opcode(ctlr, trans, lock);
        if (opcode_index < 0)
                return -EINVAL;
        with_address = spi_setup_offset(trans);
@@ -413,7 +457,7 @@ static int ich_spi_xfer(struct udevice *dev, unsigned int bitlen,
                 * in order to prevent the Management Engine from
                 * issuing a transaction between WREN and DATA.
                 */
-               if (!ctlr->ichspi_lock)
+               if (!lock)
                        ich_writew(ctlr, trans->opcode, ctlr->preop);
                return 0;
        }
@@ -437,8 +481,6 @@ static int ich_spi_xfer(struct udevice *dev, unsigned int bitlen,
        }
 
        /* Preset control fields */
-       control = ich_readw(ctlr, ctlr->control);
-       control &= ~SSFC_RESERVED;
        control = SPIC_SCGO | ((opcode_index & 0x07) << 4);
 
        /* Issue atomic preop cycle if needed */
@@ -534,7 +576,8 @@ static int ich_spi_xfer(struct udevice *dev, unsigned int bitlen,
        }
 
        /* Clear atomic preop now that xfer is done */
-       ich_writew(ctlr, 0, ctlr->preop);
+       if (!lock)
+               ich_writew(ctlr, 0, ctlr->preop);
 
        return 0;
 }
@@ -562,6 +605,12 @@ static int ich_spi_probe(struct udevice *dev)
                return ret;
        }
 
+       /* Lock down SPI controller settings if required */
+       if (plat->lockdown) {
+               ich_spi_config_opcode(dev);
+               spi_lock_down(plat, priv->base);
+       }
+
        priv->cur_speed = priv->max_speed;
 
        return 0;
@@ -569,16 +618,11 @@ static int ich_spi_probe(struct udevice *dev)
 
 static int ich_spi_remove(struct udevice *bus)
 {
-       struct ich_spi_priv *ctlr = dev_get_priv(bus);
-
        /*
         * Configure SPI controller so that the Linux MTD driver can fully
         * access the SPI NOR chip
         */
-       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));
+       ich_spi_config_opcode(bus);
 
        return 0;
 }
@@ -637,6 +681,9 @@ static int ich_spi_ofdata_to_platdata(struct udevice *dev)
                        plat->ich_version = ICHV_9;
        }
 
+       plat->lockdown = fdtdec_get_bool(gd->fdt_blob, node,
+                                        "intel,spi-lock-down");
+
        return ret;
 }