]> 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 22b5419368f74000d5a5ced0f13bc2be262e32fc..927bbd708f11ee0be1efc9321fb576453ead45fa 100644 (file)
@@ -18,6 +18,8 @@
 
 #include "ich.h"
 
+DECLARE_GLOBAL_DATA_PTR;
+
 #ifdef DEBUG_TRACE
 #define debug_trace(fmt, args...) debug(fmt, ##args)
 #else
@@ -117,15 +119,13 @@ static int ich_init_controller(struct udevice *dev,
        void *sbase;
 
        /* SBASE is similar */
-       pch_get_sbase(dev->parent, &sbase_addr);
+       pch_get_spi_base(dev->parent, &sbase_addr);
        sbase = (void *)sbase_addr;
        debug("%s: sbase=%p\n", __func__, sbase);
 
-       if (plat->ich_version == PCHV_7) {
+       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);
@@ -137,10 +137,9 @@ static int ich_init_controller(struct udevice *dev,
                ctlr->bbar = offsetof(struct ich7_spi_regs, bbar);
                ctlr->preop = offsetof(struct ich7_spi_regs, preop);
                ctlr->base = ich7_spi;
-       } else if (plat->ich_version == PCHV_9) {
+       } 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);
@@ -163,7 +162,7 @@ static int ich_init_controller(struct udevice *dev,
 
        /* Work out the maximum speed we can support */
        ctlr->max_speed = 20000000;
-       if (plat->ich_version == PCHV_9 && ich9_can_do_33mhz(dev))
+       if (plat->ich_version == ICHV_9 && ich9_can_do_33mhz(dev))
                ctlr->max_speed = 33000000;
        debug("ICH SPI: Version ID %d detected at %p, speed %ld\n",
              plat->ich_version, ctlr->base, ctlr->max_speed);
@@ -185,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;
@@ -218,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);
@@ -321,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)
 {
@@ -335,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 */
@@ -392,13 +438,13 @@ static int ich_spi_xfer(struct udevice *dev, unsigned int bitlen,
        if (ret < 0)
                return ret;
 
-       if (plat->ich_version == PCHV_7)
+       if (plat->ich_version == ICHV_7)
                ich_writew(ctlr, SPIS_CDS | SPIS_FCERR, ctlr->status);
        else
                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);
@@ -411,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;
        }
@@ -435,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 */
@@ -532,57 +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);
-
-       return 0;
-}
-
-/*
- * This uses the SPI controller from the Intel Cougar Point and Panther Point
- * PCH to write-protect portions of the SPI flash until reboot. The changes
- * don't actually take effect until the HSFS[FLOCKDN] bit is set, but that's
- * done elsewhere.
- */
-int spi_write_protect_region(struct udevice *dev, uint32_t lower_limit,
-                            uint32_t length, int hint)
-{
-       struct udevice *bus = dev->parent;
-       struct ich_spi_priv *ctlr = dev_get_priv(bus);
-       uint32_t tmplong;
-       uint32_t upper_limit;
-
-       if (!ctlr->pr) {
-               printf("%s: operation not supported on this chipset\n",
-                      __func__);
-               return -ENOSYS;
-       }
-
-       if (length == 0 ||
-           lower_limit > (0xFFFFFFFFUL - length) + 1 ||
-           hint < 0 || hint > 4) {
-               printf("%s(0x%x, 0x%x, %d): invalid args\n", __func__,
-                      lower_limit, length, hint);
-               return -EPERM;
-       }
-
-       upper_limit = lower_limit + length - 1;
-
-       /*
-        * Determine bits to write, as follows:
-        *  31     Write-protection enable (includes erase operation)
-        *  30:29  reserved
-        *  28:16  Upper Limit (FLA address bits 24:12, with 11:0 == 0xfff)
-        *  15     Read-protection enable
-        *  14:13  reserved
-        *  12:0   Lower Limit (FLA address bits 24:12, with 11:0 == 0x000)
-        */
-       tmplong = 0x80000000 |
-               ((upper_limit & 0x01fff000) << 4) |
-               ((lower_limit & 0x01fff000) >> 12);
-
-       printf("%s: writing 0x%08x to %p\n", __func__, tmplong,
-              &ctlr->pr[hint]);
-       ctlr->pr[hint] = tmplong;
+       if (!lock)
+               ich_writew(ctlr, 0, ctlr->preop);
 
        return 0;
 }
@@ -594,9 +589,6 @@ static int ich_spi_probe(struct udevice *dev)
        uint8_t bios_cntl;
        int ret;
 
-       /* Check the ICH version */
-       plat->ich_version = pch_get_version(dev->parent);
-
        ret = ich_init_controller(dev, plat, priv);
        if (ret)
                return ret;
@@ -613,11 +605,28 @@ 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;
 }
 
+static int ich_spi_remove(struct udevice *bus)
+{
+       /*
+        * Configure SPI controller so that the Linux MTD driver can fully
+        * access the SPI NOR chip
+        */
+       ich_spi_config_opcode(bus);
+
+       return 0;
+}
+
 static int ich_spi_set_speed(struct udevice *bus, uint speed)
 {
        struct ich_spi_priv *priv = dev_get_priv(bus);
@@ -650,14 +659,34 @@ static int ich_spi_child_pre_probe(struct udevice *dev)
         * ICH 7 SPI controller only supports array read command
         * and byte program command for SST flash
         */
-       if (plat->ich_version == PCHV_7) {
-               slave->mode_rx = SPI_RX_SLOW;
-               slave->mode = SPI_TX_BYTE;
-       }
+       if (plat->ich_version == ICHV_7)
+               slave->mode = SPI_RX_SLOW | SPI_TX_BYTE;
 
        return 0;
 }
 
+static int ich_spi_ofdata_to_platdata(struct udevice *dev)
+{
+       struct ich_spi_platdata *plat = dev_get_platdata(dev);
+       int node = dev_of_offset(dev);
+       int ret;
+
+       ret = fdt_node_check_compatible(gd->fdt_blob, node, "intel,ich7-spi");
+       if (ret == 0) {
+               plat->ich_version = ICHV_7;
+       } else {
+               ret = fdt_node_check_compatible(gd->fdt_blob, node,
+                                               "intel,ich9-spi");
+               if (ret == 0)
+                       plat->ich_version = ICHV_9;
+       }
+
+       plat->lockdown = fdtdec_get_bool(gd->fdt_blob, node,
+                                        "intel,spi-lock-down");
+
+       return ret;
+}
+
 static const struct dm_spi_ops ich_spi_ops = {
        .xfer           = ich_spi_xfer,
        .set_speed      = ich_spi_set_speed,
@@ -669,7 +698,8 @@ static const struct dm_spi_ops ich_spi_ops = {
 };
 
 static const struct udevice_id ich_spi_ids[] = {
-       { .compatible = "intel,ich-spi" },
+       { .compatible = "intel,ich7-spi" },
+       { .compatible = "intel,ich9-spi" },
        { }
 };
 
@@ -678,8 +708,11 @@ U_BOOT_DRIVER(ich_spi) = {
        .id     = UCLASS_SPI,
        .of_match = ich_spi_ids,
        .ops    = &ich_spi_ops,
+       .ofdata_to_platdata = ich_spi_ofdata_to_platdata,
        .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,
+       .remove = ich_spi_remove,
+       .flags  = DM_FLAG_OS_PREPARE,
 };