Patrick Rudolph has uploaded this change for review.

View Change

drivers/spi/spi_flash: Add support for flash protections

If the SPI controller doesn't have protected regions, try to directly
lock the flash. Requires vendor specific code (Winbond ATM).

Tested on Cavium EVB CN81xx using W25Q128.

Change-Id: I03eac2fc1c3ac6cd15125b1042e31c507ea94e65
Signed-off-by: Patrick Rudolph <patrick.rudolph@9elements.com>
---
M src/drivers/spi/spi_flash.c
M src/include/spi_flash.h
2 files changed, 29 insertions(+), 6 deletions(-)

git pull ssh://review.coreboot.org:29418/coreboot refs/changes/51/27751/1
diff --git a/src/drivers/spi/spi_flash.c b/src/drivers/spi/spi_flash.c
index a9bc23a..4656df6 100644
--- a/src/drivers/spi/spi_flash.c
+++ b/src/drivers/spi/spi_flash.c
@@ -543,6 +543,7 @@
const struct region *region)
{
const struct spi_ctrlr *ctrlr;
+ const struct spi_flash_ops *ops;
struct region flash_region = { 0 };

if (!flash)
@@ -554,13 +555,20 @@
return -1;

ctrlr = flash->spi.ctrlr;
+ ops = flash->ops;

- if (!ctrlr)
- return -1;
-
- if (ctrlr->flash_protect)
+ /* Lock access in the controller using "Protected Regions" */
+ if (ctrlr && ctrlr->flash_protect)
return ctrlr->flash_protect(flash, region);

+ /* Try to directly lock the region in flash */
+ if (ops && ops->get_write_protection)
+ if (ops->get_write_protection(flash, region))
+ return 0;
+
+ if (ops && ops->set_write_protection)
+ return ops->set_write_protection(flash, region);
+
return -1;
}

diff --git a/src/include/spi_flash.h b/src/include/spi_flash.h
index 9ba0c82..e3aad9f 100644
--- a/src/include/spi_flash.h
+++ b/src/include/spi_flash.h
@@ -159,8 +159,23 @@
* if CONFIG_BOOT_DEVICE_SPI_FLASH is enabled. */
const struct spi_flash *boot_device_spi_flash(void);

-/* Protect a region of spi flash using its controller, if available. Returns
- * < 0 on error, else 0 on success. */
+/*
+ * Protect a region of SPI flash.
+ * It first tries to use its controller, if available.
+ * If the controller doesn't support flash protections, try to directly lock
+ * the region in the flash device (requires flash vendor specific support).
+ *
+ * @param flash The flash device to operate on
+ * @param region The region to lock
+ * @Return < 0 on error, else 0 on success.
+ *
+ * Possible errors:
+ * * Invalid region specified
+ * * No more free "Protected Region" slots in the controller
+ * * SPI flash is already locked
+ * * Requested region layout can't be directly applied on flash
+ * * SPI bus error
+ */
int spi_flash_ctrlr_protect_region(const struct spi_flash *flash,
const struct region *region);


To view, visit change 27751. To unsubscribe, or for help writing mail filters, visit settings.

Gerrit-Project: coreboot
Gerrit-Branch: master
Gerrit-MessageType: newchange
Gerrit-Change-Id: I03eac2fc1c3ac6cd15125b1042e31c507ea94e65
Gerrit-Change-Number: 27751
Gerrit-PatchSet: 1
Gerrit-Owner: Patrick Rudolph <patrick.rudolph@9elements.com>