Skip to contentSkip to conversation

mtd: cfi_cmdset_0002: Support Persistent Protection Bits (PPB) locking

PATCH Superseded Stefan Roese 4 participants 6 messages Dec 7, 2012 – Dec 12, 2012 4 messages not in the archive

Subsystems (2)

v1 of 3 Superseded Latest is v3 →

lore
Share & tools
lore
#0 lore raw

PATCH 1/1 mtd: cfi_cmdset_0002: Support Persistent Protection Bits (PPB) locking

Currently cfi_cmdset_0002.c does not support PPB locking of sectors. This patch adds support for this locking/unlocking mechanism. It is needed on some platforms, since newer U-Boot versions do support this PPB locking and protect for example their environment sector(s) this way.

This PPB locking/unlocking will be enabled for all devices supported by cfi_cmdset_0002 reporting 8 in the CFI word 0x49 (Sector Protect/Unprotect scheme).

Please note that PPB locking does support sector-by-sector locking. But the whole chip can only be unlocked together. So unlocking one sector will automatically unlock all sectors of this device. Because of this chip limitation, the PPB unlocking function saves the current locking status of all sectors before unlocking the whole device. After unlocking the saved locking status is re-configured. This way only the addressed sectors will be unlocked.

To selectively enable this advanced sector protection mechanism, the device-tree property "use-advanced-sector-protection" has been created. To enable support for this locking this property needs to be present in the flash DT node. E.g.:

nor_flash@0,0 {
	compatible = "amd,s29gl256n", "cfi-flash";
	bank-width = <2>;
	use-advanced-sector-protection;
	...

Tested with Spanion S29GL512S10THI and Micron JS28F512M29EWx flash devices.

Cc Holger Brunck <[email protected]>
Cc Artem Bityutskiy <[email protected]>
---
 .../devicetree/bindings/mtd/mtd-physmap.txt        |   3 +
 drivers/mtd/chips/cfi_cmdset_0002.c                | 211 +++++++++++++++++++++
 drivers/mtd/maps/physmap_of.c                      |   1 +
 include/linux/mtd/map.h                            |   1 +
 4 files changed, 216 insertions(+)
Documentation/devicetree/bindings/mtd/mtd-physmap.txt +3 -0
index 94de19b..15902b8 100644
@@ -23,6 +23,9 @@ file systems on embedded devices.
    unaligned accesses as implemented in the JFFS2 code via memcpy().
    By defining "no-unaligned-direct-access", the flash will not be
    exposed directly to the MTD users (e.g. JFFS2) any more.
+ - use-advanced-sector-protection: boolean to enable support for the
+   advanced sector protection (Spansion: PPB - Persistent Protection
+   Bits) locking.
 
 For JEDEC compatible devices, the following additional properties
 are defined:
drivers/mtd/chips/cfi_cmdset_0002.c +211 -0

241 lines not on this page · raw message

drivers/mtd/maps/physmap_of.c +1 -0
index 6f19aca..d2999fc 100644
@@ -238,6 +238,7 @@ static int __devinit of_flash_probe(struct platform_device *dev)
 		info->list[i].map.phys = res.start;
 		info->list[i].map.size = res_size;
 		info->list[i].map.bankwidth = be32_to_cpup(width);
+		info->list[i].map.device_node = dp;
 
 		err = -ENOMEM;
 		info->list[i].map.virt = ioremap(info->list[i].map.phys,
include/linux/mtd/map.h +1 -0
index 3595a02..41db531 100644
@@ -245,6 +245,7 @@ struct map_info {
 	unsigned long pfow_base;
 	unsigned long map_priv_1;
 	unsigned long map_priv_2;
+	struct device_node *device_node;
 	void *fldrv_priv;
 	struct mtd_chip_driver *fldrv;
 };
Signature · 6 lines
1.8.0.1


______________________________________________________
Linux MTD discussion mailing list
http://lists.infradead.org/mailman/listinfo/linux-mtd/
Tested-by on v1 1/1
#1 lore raw

Hi Stefan,

Quoted 34 lines · Stefan Roese

Currently cfi_cmdset_0002.c does not support PPB locking of sectors. This patch adds support for this locking/unlocking mechanism. It is needed on some platforms, since newer U-Boot versions do support this PPB locking and protect for example their environment sector(s) this way.

This PPB locking/unlocking will be enabled for all devices supported by cfi_cmdset_0002 reporting 8 in the CFI word 0x49 (Sector Protect/Unprotect scheme).

Please note that PPB locking does support sector-by-sector locking. But the whole chip can only be unlocked together. So unlocking one sector will automatically unlock all sectors of this device. Because of this chip limitation, the PPB unlocking function saves the current locking status of all sectors before unlocking the whole device. After unlocking the saved locking status is re-configured. This way only the addressed sectors will be unlocked.

To selectively enable this advanced sector protection mechanism, the device-tree property "use-advanced-sector-protection" has been created. To enable support for this locking this property needs to be present in the flash DT node. E.g.:

nor_flash@0,0 {
	compatible = "amd,s29gl256n", "cfi-flash";
	bank-width = <2>;
	use-advanced-sector-protection;
	...

Tested with Spanion S29GL512S10THI and Micron JS28F512M29EWx flash devices.

Cc Holger Brunck <[email protected]>
Cc Artem Bityutskiy <[email protected]>
Quoted 7 lines
---
 .../devicetree/bindings/mtd/mtd-physmap.txt        |   3 +
 drivers/mtd/chips/cfi_cmdset_0002.c                | 211 +++++++++++++++++++++
 drivers/mtd/maps/physmap_of.c                      |   1 +
 include/linux/mtd/map.h                            |   1 +
 4 files changed, 216 insertions(+)

with an Spanion S29GL512S10THI.

Regards
Holger

______________________________________________________
Linux MTD discussion mailing list
http://lists.infradead.org/mailman/listinfo/linux-mtd/

#3 lore raw
In reply to a message not in the archive

drivers/mtd/chips/cfi_cmdset_0002.c static int cfi_atmel_unlock(struct mtd_info *mtd, loff_t ofs, uint64_t len) · line 2240

Quoted 4 lines · Stefan Roese
drivers/mtd/chips/cfi_cmdset_0002.c +4 -0
+       /*
+        * Wait for some time as unlocking of all sectors takes quite long
+        */
+       timeo = jiffies + (2 * HZ);     /* 2s max (un)locking */

Please, use msecs_to_jiffies() instead.

drivers/mtd/chips/cfi_cmdset_0002.c static int cfi_atmel_unlock(struct mtd_info *mtd, loff_t ofs, uint64_t len) · line 2253

Quoted 13 lines
drivers/mtd/chips/cfi_cmdset_0002.c +13 -0
+       for (;;) {
+               if (chip_ready(map, adr))
+                       break;
+
+               if (time_after(jiffies, timeo)) {
+                       printk(KERN_ERR "Waiting for chip to be ready timed out.\n");
+                       ret = -EIO;
+                       break;
+               }
+               mutex_unlock(&chip->mutex);
+               cfi_udelay(1);
+               mutex_lock(&chip->mutex);
+       } 

Would you please educate me a bit and explain what is protected by 'chip->mutex' and by 'get_chip()'.

Why you need to drop the mutex here?

Why is it not an ABBA deadlock to do this:

Task 1: In the loop above, has chip locked, doing

        mutex_lock(&chip->mutex);

Task 2: done mutex_lock(&chip->mutex), now doing

        ret = get_chip(map, chip, adr + chip->start, FL_LOCKING);
Signature · 2 lines
Best Regards,
Artem Bityutskiy
#5 lore raw
In reply to a message not in the archive
Quoted 7 lines · Artem Bityutskiy

drivers/mtd/chips/cfi_cmdset_0002.c static int cfi_atmel_unlock(struct mtd_info *mtd, loff_t ofs, uint64_t len) · line 2240

Quoted 4 lines · Stefan Roese
drivers/mtd/chips/cfi_cmdset_0002.c +4 -0
+       /*
+        * Wait for some time as unlocking of all sectors takes quite long
+        */
+       timeo = jiffies + (2 * HZ);     /* 2s max (un)locking */

Please, use msecs_to_jiffies() instead.

Sure, thats better.

Quoted 14 lines

drivers/mtd/chips/cfi_cmdset_0002.c static int cfi_atmel_unlock(struct mtd_info *mtd, loff_t ofs, uint64_t len) · line 2253

Quoted 13 lines
drivers/mtd/chips/cfi_cmdset_0002.c +13 -0
+       for (;;) {
+               if (chip_ready(map, adr))
+                       break;
+
+               if (time_after(jiffies, timeo)) {
+                       printk(KERN_ERR "Waiting for chip to be ready timed out.\n");
+                       ret = -EIO;
+                       break;
+               }
+               mutex_unlock(&chip->mutex);
+               cfi_udelay(1);
+               mutex_lock(&chip->mutex);
+       } 
Quoted 2 lines

Would you please educate me a bit and explain what is protected by 'chip->mutex' and by 'get_chip()'.

AFAIK, chip->mutex protects the access to the chip itself. So that sequences are not interrupted.

I have to admit that I haven't looked into get_chip() so far. It seems to handle a state machine. Normally (idle state) it will just fall through (FL_READY).

Quoted 1 line

Why you need to drop the mutex here?

Not sure, that might not be necessary. Copy and past from another loop in the same file.

Quoted 7 lines

Why is it not an ABBA deadlock to do this:

Task 1: In the loop above, has chip locked, doing

        mutex_lock(&chip->mutex);

Task 2: done mutex_lock(&chip->mutex), now doing

        ret = get_chip(map, chip, adr + chip->start, FL_LOCKING);

I don't see two different locks/mutexes (only A) here. As get_chip() does no request any real mutex. Please correct me if I'm wrong.

In many other places UDELAY() is called:

#define UDELAY(map, chip, adr, usec) \

do {  \
	mutex_unlock(&chip->mutex);  \
	cfi_udelay(usec);  \
	mutex_lock(&chip->mutex);  \
} while (0)

So dropping this lock seems to be quite common in this driver.

Thanks,
Stefan

#7 lore raw
In reply to a message not in the archive
Quoted 10 lines · Stefan Roese
Quoted 7 lines · Artem Bityutskiy

drivers/mtd/chips/cfi_cmdset_0002.c static int cfi_atmel_unlock(struct mtd_info *mtd, loff_t ofs, uint64_t len) · line 2240

Quoted 4 lines · Stefan Roese
drivers/mtd/chips/cfi_cmdset_0002.c +4 -0
+       /*
+        * Wait for some time as unlocking of all sectors takes quite long
+        */
+       timeo = jiffies + (2 * HZ);     /* 2s max (un)locking */

Please, use msecs_to_jiffies() instead.

Sure, thats better.

Would you please do this instead?

Quoted 6 lines

AFAIK, chip->mutex protects the access to the chip itself. So that sequences are not interrupted.

I have to admit that I haven't looked into get_chip() so far. It seems to handle a state machine. Normally (idle state) it will just fall through (FL_READY).

So it looks like the idea is that you first take the mutex, then call get_chip() which will wait for the chip becoming really ready, and then you can safely use it.

Quoted 5 lines
Quoted 2 lines

Why you need to drop the mutex here?

Not sure, that might not be necessary. Copy and past from another loop in the same file.

Probably from 'get_chip()' ?

Quoted 10 lines
Quoted 7 lines

Why is it not an ABBA deadlock to do this:

Task 1: In the loop above, has chip locked, doing

        mutex_lock(&chip->mutex);

Task 2: done mutex_lock(&chip->mutex), now doing

        ret = get_chip(map, chip, adr + chip->start, FL_LOCKING);

I don't see two different locks/mutexes (only A) here. As get_chip() does no request any real mutex. Please correct me if I'm wrong.

Right, there is indeed no deadlock.

Quoted 8 lines

In many other places UDELAY() is called:

#define UDELAY(map, chip, adr, usec) \

do {  \
	mutex_unlock(&chip->mutex);  \
	cfi_udelay(usec);  \
	mutex_lock(&chip->mutex);  \
} while (0)

Why not to use this as well then for consistency?

Signature · 2 lines
Best Regards,
Artem Bityutskiy
#9 lore raw
In reply to a message not in the archive
Quoted 8 lines · Artem Bityutskiy
Quoted 7 lines · Stefan Roese
Quoted 6 lines · Artem Bityutskiy
Quoted 4 lines · Stefan Roese
(quoted diff) +4 -0
+       /*
+        * Wait for some time as unlocking of all sectors takes quite long
+        */
+       timeo = jiffies + (2 * HZ);     /* 2s max (un)locking */
Quoted 5 lines
Quoted 3 lines
Quoted 1 line

Please, use msecs_to_jiffies() instead.

Sure, thats better.

Would you please do this instead?

Yes. I was just waiting for some further comments.

Quoted 10 lines
Quoted 6 lines

AFAIK, chip->mutex protects the access to the chip itself. So that sequences are not interrupted.

I have to admit that I haven't looked into get_chip() so far. It seems to handle a state machine. Normally (idle state) it will just fall through (FL_READY).

So it looks like the idea is that you first take the mutex, then call get_chip() which will wait for the chip becoming really ready, and then you can safely use it.

Thats it.

Quoted 7 lines
Quoted 5 lines
Quoted 2 lines

Why you need to drop the mutex here?

Not sure, that might not be necessary. Copy and past from another loop in the same file.

Probably from 'get_chip()' ?

Yes, most likely.

Quoted 15 lines
Quoted 10 lines
Quoted 7 lines

Why is it not an ABBA deadlock to do this:

Task 1: In the loop above, has chip locked, doing

        mutex_lock(&chip->mutex);

Task 2: done mutex_lock(&chip->mutex), now doing

        ret = get_chip(map, chip, adr + chip->start, FL_LOCKING);

I don't see two different locks/mutexes (only A) here. As get_chip() does no request any real mutex. Please correct me if I'm wrong.

Right, there is indeed no deadlock.

Quoted 2 lines

In many other places UDELAY() is called:

Quoted 8 lines
Quoted 6 lines

#define UDELAY(map, chip, adr, usec) \

do {  \
	mutex_unlock(&chip->mutex);  \
	cfi_udelay(usec);  \
	mutex_lock(&chip->mutex);  \
} while (0)

Why not to use this as well then for consistency?

Okay, will do.

I'll send a new patch version today.

Thanks for the review,
Stefan