Support for OTP memory
v2 cover Support for OTP memory
Following some feedback from Greg, I've updated this series to be more of a generic OTP layer. Everything is now registered under the "otp" bus and I've also converted the blackfin OTP driver to use this framework (which is the only current OTP driver I could find).
Mike, I wasn't 100% sure how big the blackfin OTP is but I found a datasheet talking about 64KB so I've assumed that for now.
Main changes since v1:
- both devices and regions are now on the "otp" bus.
- moved to drivers/otp.
- converted bfin-otp to the generic OTP layer.
Jamie Iles (4):
drivers/otp: add initial support for OTP memory drivers/otp: add support for Picoxcell PC3X3 OTP drivers/otp: allow an ioctl to be specified drivers/otp: convert bfin otp to generic OTP
Documentation/ABI/testing/sysfs-bus-otp | 68 ++
drivers/Kconfig | 2 +
drivers/Makefile | 1 +
drivers/char/Kconfig | 28 -
drivers/char/Makefile | 1 -
drivers/otp/Kconfig | 49 ++
drivers/otp/Makefile | 3 +
drivers/{char => otp}/bfin-otp.c | 174 +++---
drivers/otp/otp.c | 896 +++++++++++++++++++++++++
drivers/otp/otp_pc3x3.c | 1079 +++++++++++++++++++++++++++++++
include/linux/otp.h | 224 +++++++
11 files changed, 2397 insertions(+), 128 deletions(-)
create mode 100644 Documentation/ABI/testing/sysfs-bus-otp
create mode 100644 drivers/otp/Kconfig
create mode 100644 drivers/otp/Makefile
rename drivers/{char => otp}/bfin-otp.c (54%)
create mode 100644 drivers/otp/otp.c
create mode 100644 drivers/otp/otp_pc3x3.c
create mode 100644 include/linux/otp.hSignature · 1 line
1.7.4
RFC PATCH v2 1/4 drivers/otp: add initial support for OTP memory
OTP memory is typically found in some embedded devices and can be used for storing boot code, cryptographic keys and other persistent information onchip. This patch adds a generic layer that devices can register OTP with and allows access through a set of character device nodes.
--- Documentation/ABI/testing/sysfs-bus-otp | 68 +++ drivers/Kconfig | 2 + drivers/Makefile | 1 + drivers/otp/Kconfig | 10 + drivers/otp/Makefile | 1 + drivers/otp/otp.c | 878 +++++++++++++++++++++++++++++++ include/linux/otp.h | 221 ++++++++ 7 files changed, 1181 insertions(+), 0 deletions(-) create mode 100644 Documentation/ABI/testing/sysfs-bus-otp create mode 100644 drivers/otp/Kconfig create mode 100644 drivers/otp/Makefile create mode 100644 drivers/otp/otp.c create mode 100644 include/linux/otp.h
Documentation/ABI/testing/sysfs-bus-otp new file +68 -0
@@ -0,0 +1,68 @@ +What: /sys/bus/otp/ +Date: March 2011 +KernelVersion: 2.6.40+ +Contact: Jamie Iles <[email protected]> +Description: + The otp bus presents a number of devices where each + device represents a region or device in the SoC. Each region + will create a device node which allows the region to be + written with read()/write() calls and the device on the bus + has attributes for controlling the redundancy format and + getting the region size. + +What: /sys/bus/otp[a-z]/write_enable +Date: March 2011 +KernelVersion: 2.6.40+ +Contact: "Jamie Iles" <[email protected]> +Description: + This file controls whether the OTP device can be written to. + If set to "enabled" then the regions may be written, the + number of regions may be changed and the format of any region + may be changed. + +What: /sys/bus/otp[a-z]/num_regions +Date: March 2011 +KernelVersion: 2.6.40+ +Contact: "Jamie Iles" <[email protected]> +Description: + This file controls the number of regions in the OTP device. + Valid values are 1, 2, 4 and 8. The number of regions may be + increased but never decreased. Increasing the number of + regions will create new devices on the otp bus. + +What: /sys/bus/otp[a-z]/strict_programming +Date: March 2011 +KernelVersion: 2.6.40+ +Contact: "Jamie Iles" <[email protected]> +Description: + This file indicates whether all words in a redundant format + must be programmed correctly to indicate success. Disabling + this will mean that programming will be considered a success + if the word can be read back correctly in it's redundant + format. + +What: /sys/bus/otp/devices/otp[a-z][0-9]*/format +Date: March 2011 +KernelVersion: 2.6.40+ +Contact: Jamie Iles <[email protected]> +Description: + The redundancy format of the region. Valid values are: + - single-ended (1 bit of storage per data bit). + - redundant (2 bits of storage, wire-OR'd per data + bit). + - differential (2 bits of storage, differential + voltage per data bit). + - differential-redundant (4 bits of storage, combining + redundant and differential). + It is possible to increase redundancy of a region but care + will be needed if there is data already in the region. + +What: /sys/bus/otp/devices/otp[a-z][0-9]*/size +Date: March 2011 +KernelVersion: 2.6.40+ +Contact: Jamie Iles <[email protected]> +Description: + The effective storage size of the region. This is the amount + of data that a user can store in the region taking into + account the number of regions and the redundancy format of the + region itself.
drivers/Kconfig +2 -0
@@ -119,4 +119,6 @@ source "drivers/platform/Kconfig" source "drivers/clk/Kconfig" source "drivers/hwspinlock/Kconfig" + +source "drivers/otp/Kconfig" endmenu
drivers/Makefile +1 -0
@@ -119,3 +119,4 @@ obj-y += ieee802154/ obj-y += clk/ obj-$(CONFIG_HWSPINLOCK) += hwspinlock/ +obj-$(CONFIG_OTP) += otp/
drivers/otp/Kconfig new file +10 -0
@@ -0,0 +1,10 @@ +# +# Character device configuration +# + +menuconfig OTP + bool "OTP memory support" + help + Say y here to support OTP memory found in some embedded devices. + This memory can commonly be used to store boot code, cryptographic + keys and other persistent data.
drivers/otp/Makefile new file +1 -0
@@ -0,0 +1 @@ +obj-$(CONFIG_OTP) += otp.o
drivers/otp/otp.c new file +878 -0
878 lines not on this page · raw message
include/linux/otp.h new file +221 -0
221 lines not on this page · raw message
Signature · 1 line
1.7.4
Re: [RFC PATCHv2 1/4] drivers/otp: add initial support for OTP memory
Documentation/ABI/testing/sysfs-bus-otp
Quoted 4 lines · Jamie Iles
Documentation/ABI/testing/sysfs-bus-otp new file +4
-0+What: /sys/bus/otp/ +Description: + The otp bus presents a number of devices where each + device represents a region or device in the SoC.
is it limited to OTP devices inside of SoCs ? cant OTP devices be on other busses like I2C or SPI ?
spurious space before "The" ...
Review
Quoted 2 lines
(quoted diff) +2
-0+Contact: Jamie Iles <[email protected]> +Contact: "Jamie Iles" <[email protected]>
sometimes you quote and sometimes you dont ... seems like quotes are useless here
Quoted 1 line
+What: /sys/bus/otp[a-z]/write_enable
what's with the [a-z] ? how about using otp# like most other people are doing now ? [a-z] can be a bit limited/confusing ...
Documentation/ABI/testing/sysfs-bus-otp line 42
Quoted 7 lines
Documentation/ABI/testing/sysfs-bus-otp new file +7
-0+What: /sys/bus/otp[a-z]/strict_programming +Description: + This file indicates whether all words in a redundant format + must be programmed correctly to indicate success. Disabling + this will mean that programming will be considered a success + if the word can be read back correctly in it's redundant + format.
i dont really grok what this is trying to say ...
"it's" -> "its"
Quoted 1 line
+What: /sys/bus/otp/devices/otp[a-z][0-9]*/format
you have /sys/bus/otp[a-z]/ and /sys/bus/otp/devices/ ? why not unify them ?
what are each of these subdirs trying to represent ?
Documentation/ABI/testing/sysfs-bus-otp line 58
Quoted 11 lines
Documentation/ABI/testing/sysfs-bus-otp new file +11
-0+Description: + The redundancy format of the region. Valid values are: + - single-ended (1 bit of storage per data bit). + - redundant (2 bits of storage, wire-OR'd per data + bit). + - differential (2 bits of storage, differential + voltage per data bit). + - differential-redundant (4 bits of storage, combining + redundant and differential). + It is possible to increase redundancy of a region but care + will be needed if there is data already in the region.
where does ECC fit in ? the Blackfin OTP is structured:
- first 4 pages are write control bitfields for all other pages (so
blowing bit 5 of page 0 prevents further writing to page 5)
- each 0x100 page region has 0x20 pages dedicated to ECC for the
other 0x80 pages ... this provides 1 bit error correction and 2 bits of error detection (anymore and you're screwed!)
drivers/otp/Kconfig line 3
Quoted 6 lines
drivers/otp/Kconfig new file +3
-0@@ -0,0 +1,10 @@ +# +# Character device configuration +#
old comment
drivers/otp/Kconfig line 10
Quoted 6 lines
drivers/otp/Kconfig new file +6
-0+menuconfig OTP + bool "OTP memory support" + help + Say y here to support OTP memory found in some embedded devices. + This memory can commonly be used to store boot code, cryptographic + keys and other persistent data.
is this limited to embedded devices ? i guess TPM keys and such are already handled by the TPM layers ...
"y" -> "Y"
drivers/otp/otp.c line 12
Quoted 3 lines
drivers/otp/otp.c new file +1
-0+#undef DEBUG
dead code
Quoted 6 lines
drivers/otp/otp.c new file +6
-0+static int otp_open (struct inode *inode,struct file *filp); +static int otp_release (struct inode *inode,struct file *filp); +static ssize_t otp_write (struct file *filp,const char __user *buf, +size_t len,loff_t *offs); +static ssize_t otp_read (struct file *filp,char __user *buf,size_t len, +loff_t *offs);
drivers/otp/otp.c line 41
Quoted 10 lines
drivers/otp/otp.c new file +10
-0+static loff_t otp_llseek (struct file *filp,loff_t offs,int origin); + +static const struct file_operations otp_fops = { + .owner =THIS_MODULE , + .open = otp_open, + .release = otp_release, + .write = otp_write, + .read = otp_read, + .llseek = otp_llseek, +};
if you moved the fops down to where it is used, you wouldnt need the redundant func prototypes
drivers/otp/otp.c line 46
Quoted 4 lines
drivers/otp/otp.c new file +4
-0+static DEFINE_SEMAPHORE (otp_sem); +static int otp_we, otp_strict_programming; +static struct otp_device *otp; +static dev_t otp_devno;
hrm, having these be global instead of per-device sounds like a really bad idea ...
drivers/otp/otp.c line 60
Quoted 13 lines
drivers/otp/otp.c new file +13
-0+/* +* Given a device for one of the otpN devices, get the corresponding +* otp_region. +*/ +static inline struct otp_region *to_otp_region (struct device *dev) +{ + return dev ? container_of(dev, struct otp_region, dev) : NULL; +} + +static inline struct otp_device *to_otp_device (struct device *dev) +{ + return dev ? container_of(dev, struct otp_device, dev) : NULL; +}
do you need the NULL checks ? none of the callers of these funcs check for NULL ...
Quoted 6 lines
drivers/otp/otp.c new file +6
-0+static ssize_t otp_format_show (struct device *dev, +struct device_attribute *attr,char *buf) +{ +struct otp_region *region =to_otp_region (dev); +enum otp_redundancy_fmt fmt; +const char *fmt_string;
drivers/otp/otp.c line 83
Quoted 10 lines
drivers/otp/otp.c new file +10
-0+ +if (down_interruptible (&otp_sem)) +return -ERESTARTSYS ; + +if (region->ops->get_fmt (region)) + fmt = region->ops->get_fmt (region); +else + fmt =OTP_REDUNDANCY_FMT_SINGLE_ENDED ; + +up (&otp_sem);
why are you using a semaphore when it looks like you're simply treating it as a mutex ? make more sense to use a proper mutex wouldnt it ?
drivers/otp/otp.c line 96
Quoted 12 lines
drivers/otp/otp.c new file +12
-0+if (OTP_REDUNDANCY_FMT_SINGLE_ENDED == fmt) + fmt_string ="single-ended" ; +else if (OTP_REDUNDANCY_FMT_REDUNDANT == fmt) + fmt_string ="redundant" ; +else if (OTP_REDUNDANCY_FMT_DIFFERENTIAL == fmt) + fmt_string ="differential" ; +else if (OTP_REDUNDANCY_FMT_DIFFERENTIAL_REDUNDANT == fmt) + fmt_string ="differential-redundant" ; +else + fmt_string =NULL ; + +return fmt_string ?sprintf (buf,"%s\n" , fmt_string) : -EINVAL ;
i'm pretty sure printf in the kernel can handle NULL with %s, so the NULL check can probably be punted
drivers/otp/otp.c line 162
Quoted 3 lines
drivers/otp/otp.c new file +3
-0+static struct bus_type otp_bus_type = { + .name ="otp" , +};
can this be const ?
Quoted 9 lines
drivers/otp/otp.c new file +9
-0+struct attribute *region_attrs[] = { + &dev_attr_format.attr, + &dev_attr_size.attr, +NULL , +}; + +const struct attribute_group region_attr_group = { + .attrs = region_attrs, +};
drivers/otp/otp.c line 182
Quoted 10 lines
drivers/otp/otp.c new file +10
-0+ +const struct attribute_group *region_attr_groups[] = { + ®ion_attr_group, +NULL , +}; + +struct device_type region_type = { + .name ="region" , + .groups = region_attr_groups, +};
should these be static ?
drivers/otp/otp.c line 191
Quoted 8 lines
drivers/otp/otp.c new file +8
-0+/* +* Show the current write enable state of the OTP. Users can only program the +* OTP when this shows 'enabled'. +*/ +static ssize_t otp_we_show (struct device *dev,struct device_attribute *attr, +char *buf) +{ +int ret;
this func has a ssize_t type but you're using int here. seems to come up a few times in this patch.
Quoted 3 lines
+/*
+ * Set the write enable state of the OTP. 'enabled' will enable programming
+ * and 'disabled' will prevent further programming from occuring. On loading
"occuring" -> "occurring"
Quoted 12 lines
+ * the module, this will default to 'disabled'.
+ */drivers/otp/otp.c new file +10
-0+static ssize_t otp_we_store (struct device *dev,struct device_attribute *attr, +const char *buf,size_t len) +{ +int err =0 ; + +if (down_interruptible (&otp_sem)) +return -ERESTARTSYS ; + +if (sysfs_streq (buf,"enabled" )) + otp_we =1 ;
drivers/otp/otp.c line 227
Quoted 10 lines
drivers/otp/otp.c new file +10
-0+else if (sysfs_streq (buf,"disabled" )) + otp_we =0 ; +else + err = -EINVAL ; + +up (&otp_sem); + +return err ?: len; +} +static DEVICE_ATTR(write_enable, S_IRUSR | S_IWUSR, otp_we_show, otp_we_store);
you output and accept "enabled" and "disabled" for multiple values. how about unifying these ?
return otp_attr_store_enabled(buf, len, &otp_we);
Quoted 7 lines
drivers/otp/otp.c new file +7
-0+static ssize_t otp_num_regions_show (struct device *dev, +struct device_attribute *attr,char *buf) +{ +int nr_regions; + +if (down_interruptible (&otp_sem)) +return -ERESTARTSYS ;
drivers/otp/otp.c line 297
Quoted 10 lines
drivers/otp/otp.c new file +10
-0+ + nr_regions = otp->ops->get_nr_regions (otp); + +up (&otp_sem); + +if (nr_regions <=0 ) +return -EINVAL ; + +return sprintf (buf,"%u\n" , nr_regions); +}
i'm not sure returning -EINVAL here makes sense ... shouldnt it just be showing the user the result of get_nr_regions() ?
Quoted 10 lines
drivers/otp/otp.c new file +10
-0+struct attribute *otp_attrs[] = { + &dev_attr_strict_programming.attr, + &dev_attr_num_regions.attr, + &dev_attr_write_enable.attr, +NULL , +}; + +const struct attribute_group otp_attr_group = { + .attrs = otp_attrs, +};
drivers/otp/otp.c line 350
Quoted 10 lines
drivers/otp/otp.c new file +10
-0+ +const struct attribute_group *otp_attr_groups[] = { + &otp_attr_group, +NULL , +}; + +struct device_type otp_type = { + .name ="otp" , + .groups = otp_attr_groups, +};
static ?
drivers/otp/otp.c line 358
Quoted 7 lines
drivers/otp/otp.c new file +7
-0+static void otp_dev_release (struct device *dev) +{ +struct otp_device *otp =to_otp_device (dev); + +kfree (otp); + otp =NULL ; +}
setting to NULL here is pointless when the pointer is on the stack
drivers/otp/otp.c line 372
Quoted 13 lines
drivers/otp/otp.c new file +13
-0+struct otp_device *otp_device_alloc (struct device *dev, +const struct otp_device_ops *ops, +size_t size) +{ +struct otp_device *otp_dev =NULL ; +int err = -EINVAL ; + +down (&otp_sem); + +if (dev && !get_device (dev)) { + err = -ENODEV ; + goto out; + }
should you really be allowing dev==NULL ? does that setup make sense ?
drivers/otp/otp.c line 378
Quoted 5 lines
drivers/otp/otp.c new file +5
-0+if (otp) { +pr_warning ("an otp device already registered\n" ); + err = -EBUSY ; + goto out_put; + }
you can only register one OTP device in the whole system ??
drivers/otp/otp.c line 501
Quoted 5 lines
drivers/otp/otp.c new file +5
-0+void otp_region_unregister (struct otp_region *region) +{ +device_unregister (®ion->dev); +} +EXPORT_SYMBOL_GPL (otp_region_unregister);
wonder if it'd be better to simply #define this in the global otp.h header
drivers/otp/otp.c line 598
Quoted 6 lines
drivers/otp/otp.c new file +6
-0+static ssize_t otp_write (struct file *filp,const char __user *buf,size_t len, +loff_t *offs) +{ +unsigned pos = (unsigned )*offs; + + len =min (len, region->ops->get_size (region) - (unsigned )*offs);
what's with the unsigned cast ? defeats the point of using loff_t doesnt it ?
drivers/otp/otp.c
Quoted 9 lines
drivers/otp/otp.c new file +9
-0+/* +* We're now aligned to an 8 byte boundary so we can simply copy words +* from userspace and write them into the OTP. +*/ +/* +* We might have less than 8 bytes left so we'll need to do another +* read-modify-write. +*/ +while (len >=OTP_WORD_SIZE ) {
i think "8 byte" should be "necessary byte" ?
drivers/otp/otp.c line 679
Quoted 11 lines
drivers/otp/otp.c new file +11
-0+if (region->ops->read_word (region, pos /OTP_WORD_SIZE , + &word)) { + ret = -EIO ; + goto out; + } + +if (region->ops->write_word (region, pos /OTP_WORD_SIZE , + word)) { + ret = -EIO ; + goto out; + }
shouldnt you pass the ret value up from read/write word ? this would allow the lower layers to better describe the issue than just -EIO wouldnt it ?
last three comments apply to the read func as well ...
drivers/otp/otp.c line 863
Quoted 9 lines
drivers/otp/otp.c new file +9
-0+static int __init otp_init (void ) +{ +int err; + + err =bus_register (&otp_bus_type); +if (err) +return err; + + err =alloc_chrdev_region (&otp_devno,0 ,9 ,"otp" );
where'd that magic 9 come from ?
Quoted 1 line
+MODULE_DESCRIPTION("OTP interface driver");
i think this is also a bus driver ?
drivers/otp/otp.c
Quoted 5 lines
drivers/otp/otp.c new file +5
-0+/* +* The OTP works in 64 bit words. When we are programming or reading, +* everything is done with 64 bit word addresses. +*/ +#define OTP_WORD_SIZE 8
should this be a per-device setting ? or wait until someone who doesnt have 64bit chunks show up ?
Quoted 2 lines
+ * struct
otp_device- a picoxcell OTP device.
+ *otp_device_alloc- create a new picoxcell OTP device.
this is no longer picoxcell-specific
Quoted 1 line
+ *
otp_device_unregister- deregister an existing structotp_device.
"deregister" -> "unregister"
-mike
Re: [RFC PATCHv2 1/4] drivers/otp: add initial support for OTP memory
Hi Mike,
Thanks for the great feedback!
Quoted 8 lines · Mike Frysinger
Documentation/ABI/testing/sysfs-bus-otp
Quoted 4 lines · Jamie Iles
Documentation/ABI/testing/sysfs-bus-otp new file +4
-0+What: /sys/bus/otp/ +Description: + The otp bus presents a number of devices where each + device represents a region or device in the SoC.is it limited to OTP devices inside of SoCs ? cant OTP devices be on other busses like I2C or SPI ?
Yes, that needs clearing up, it shouldn't be SoC specific.
Quoted 8 lines
spurious space before "The" ...
Review
Quoted 2 lines
(quoted diff) +2
-0+Contact: Jamie Iles <[email protected]> +Contact: "Jamie Iles" <[email protected]>sometimes you quote and sometimes you dont ... seems like quotes are useless here
Not sure why I did that, will clean it up.
Quoted 5 lines
Quoted 2 lines
+What: /sys/bus/otp[a-z]/write_enable
what's with the [a-z] ? how about using otp# like most other people are doing now ? [a-z] can be a bit limited/confusing ...
So this should be /sys/bus/devices/otp[a-z]/write_enable. What I was trying to get across (but didn't do a good job of) is that you could have an otp device (e.g. otpa) that could have multiple regions (otpa1, otpa2...).
Quoted 10 lines
Documentation/ABI/testing/sysfs-bus-otp line 42
Quoted 8 lines
Documentation/ABI/testing/sysfs-bus-otp new file +7
-0+What: /sys/bus/otp[a-z]/strict_programming +Description: + This file indicates whether all words in a redundant format + must be programmed correctly to indicate success. Disabling + this will mean that programming will be considered a success + if the word can be read back correctly in it's redundant + format.i dont really grok what this is trying to say ...
Our OTP has some redundancy in there so for example to store one 64 bit word, it may store it in two locations the wire-OR them together to get a stronger read when reading back (there are other formats too). Disabling this attribute means that say one of the redundant words doesn't program correctly but when both words are read together and wire-OR'd the result is correct then we treat that as success. It's basically trying to cope with minor errors for infield programming that may not cause a real problem. I'll try and make that clearer though because it isn't obvious.
Quoted 7 lines
"it's" -> "its"
Quoted 1 line
+What: /sys/bus/otp/devices/otp[a-z][0-9]*/format
you have /sys/bus/otp[a-z]/ and
/sys/bus/otp/devices/? why not unify them ?what are each of these subdirs trying to represent ?
They should all be in /sys/bus/otp/devices and otp[a-z] should be the actual otp device and otp[a-z][0-9]+ should be the regions. I'll try and make that a bit clearer and maybe put an example in there.
Quoted 12 lines
Documentation/ABI/testing/sysfs-bus-otp line 58
Quoted 11 lines
Documentation/ABI/testing/sysfs-bus-otp new file +11
-0+Description: + The redundancy format of the region. Valid values are: + - single-ended (1 bit of storage per data bit). + - redundant (2 bits of storage, wire-OR'd per data + bit). + - differential (2 bits of storage, differential + voltage per data bit). + - differential-redundant (4 bits of storage, combining + redundant and differential). + It is possible to increase redundancy of a region but care + will be needed if there is data already in the region.
Quoted 6 lines
where does ECC fit in ? the Blackfin OTP is structured:
- first 4 pages are write control bitfields for all other pages (so
blowing bit 5 of page 0 prevents further writing to page 5)
- each0x100page region has0x20pages dedicated to ECC for the
other0x80pages ... this provides 1 bit error correction and 2 bits of error detection (anymore and you're screwed!)
How does the ECC correction work for bfin at the moment? Does the user specify it manually when writing then check it themselves when reading back or does the hardware handle that?
The way I was thinking that this sort of thing could be handled would be for the ECC to be transparent to the user. Perhaps for bfin the OTP could be registered as 4 regions, otpa1-otpa4 which default to "single-ended". Writing "ecc" as the format would generate the ecc and program it for that region then check the ECC when reading back.
Quoted 8 lines
drivers/otp/Kconfig line 3
Quoted 6 lines
drivers/otp/Kconfig new file +3
-0@@ -0,0 +1,10 @@ +# +# Character device configuration +#old comment
Will remove.
Quoted 11 lines
drivers/otp/Kconfig line 10
Quoted 6 lines
drivers/otp/Kconfig new file +6
-0+menuconfig OTP + bool "OTP memory support" + help + Say y here to support OTP memory found in some embedded devices. + This memory can commonly be used to store boot code, cryptographic + keys and other persistent data.is this limited to embedded devices ? i guess TPM keys and such are already handled by the TPM layers ...
"y" -> "Y"
OK, that needs cleaning up.
Quoted 6 lines
drivers/otp/otp.c line 12
Quoted 4 lines
drivers/otp/otp.c new file +1
-0+#undef DEBUGdead code
Will remove.
Quoted 11 lines
Quoted 11 lines
drivers/otp/otp.c new file +11
-0+static int otp_open (struct inode *inode,struct file *filp); +static int otp_release (struct inode *inode,struct file *filp); +static ssize_t otp_write (struct file *filp,const char __user *buf, +size_t len,loff_t *offs); +static ssize_t otp_read (struct file *filp,char __user *buf,size_t len, +loff_t *offs); +static loff_t otp_llseek (struct file *filp,loff_t offs,int origin); + +static const struct file_operations otp_fops = { + .owner =THIS_MODULE , + .open = otp_open,
Quoted 8 lines
drivers/otp/otp.c line 41
Quoted 5 lines
drivers/otp/otp.c new file +5
-0+ .release = otp_release, + .write = otp_write, + .read = otp_read, + .llseek = otp_llseek, +};if you moved the fops down to where it is used, you wouldnt need the redundant func prototypes
Ok, will do.
Quoted 7 lines
drivers/otp/otp.c line 46
Quoted 4 lines
drivers/otp/otp.c new file +4
-0+static DEFINE_SEMAPHORE (otp_sem); +static int otp_we, otp_strict_programming; +static struct otp_device *otp; +static dev_t otp_devno;hrm, having these be global instead of per-device sounds like a really bad idea ...
So at the moment the only devices I'm aware of have a single OTP device so I figured the most important thing was to make sure that the sysfs and device node naming permitted for multiple OTP devices in the future without ABI breakage. Having said that, these probably could be moved into the otp_device though so I'll have a go at that.
Quoted 8 lines
Quoted 8 lines
drivers/otp/otp.c new file +8
-0+/* +* Given a device for one of the otpN devices, get the corresponding +* otp_region. +*/ +static inline struct otp_region *to_otp_region (struct device *dev) +{ + return dev ? container_of(dev, struct otp_region, dev) : NULL; +}
Quoted 8 lines
drivers/otp/otp.c line 60
Quoted 5 lines
drivers/otp/otp.c new file +5
-0+ +static inline struct otp_device *to_otp_device (struct device *dev) +{ + return dev ? container_of(dev, struct otp_device, dev) : NULL; +}do you need the NULL checks ? none of the callers of these funcs check for NULL ...
I think so, because at least that way if something does go wrong and you dereference the NULL later you should get a nice backtrace, but if you don't do the check then you could have container_of() yielding something like "NULL - 0x4" giving a potentially valid address. If that's the wrong thing to do then I'm happy to change though.
Quoted 12 lines
Quoted 12 lines
drivers/otp/otp.c new file +12
-0+static ssize_t otp_format_show (struct device *dev, +struct device_attribute *attr,char *buf) +{ +struct otp_region *region =to_otp_region (dev); +enum otp_redundancy_fmt fmt; +const char *fmt_string; + +if (down_interruptible (&otp_sem)) +return -ERESTARTSYS ; + +if (region->ops->get_fmt (region)) + fmt = region->ops->get_fmt (region);
Quoted 8 lines
drivers/otp/otp.c line 83
Quoted 4 lines
drivers/otp/otp.c new file +4
-0+else + fmt =OTP_REDUNDANCY_FMT_SINGLE_ENDED ; + +up (&otp_sem);why are you using a semaphore when it looks like you're simply treating it as a mutex ? make more sense to use a proper mutex wouldnt it ?
Yes, that makes more sense, I'll update to a mutex.
Quoted 13 lines
drivers/otp/otp.c line 96
Quoted 12 lines
drivers/otp/otp.c new file +12
-0+if (OTP_REDUNDANCY_FMT_SINGLE_ENDED == fmt) + fmt_string ="single-ended" ; +else if (OTP_REDUNDANCY_FMT_REDUNDANT == fmt) + fmt_string ="redundant" ; +else if (OTP_REDUNDANCY_FMT_DIFFERENTIAL == fmt) + fmt_string ="differential" ; +else if (OTP_REDUNDANCY_FMT_DIFFERENTIAL_REDUNDANT == fmt) + fmt_string ="differential-redundant" ; +else + fmt_string =NULL ; + +return fmt_string ?sprintf (buf,"%s\n" , fmt_string) : -EINVAL ;
Quoted 2 lines
i'm pretty sure printf in the kernel can handle NULL with %s, so the NULL check can probably be punted
It's more so that the read() returns an error rather than an empty string, but Greg has suggested returning early so I'll do that.
Quoted 6 lines
drivers/otp/otp.c line 162
Quoted 4 lines
drivers/otp/otp.c new file +3
-0+static struct bus_type otp_bus_type = { + .name ="otp" , +};can this be const ?
No, I don't think so; bus_register() doesn't want a const bus_type.
Quoted 13 lines
Quoted 13 lines
drivers/otp/otp.c new file +13
-0+struct attribute *region_attrs[] = { + &dev_attr_format.attr, + &dev_attr_size.attr, +NULL , +}; + +const struct attribute_group region_attr_group = { + .attrs = region_attrs, +}; + +const struct attribute_group *region_attr_groups[] = { + ®ion_attr_group, +NULL ,
Quoted 8 lines
drivers/otp/otp.c line 182
Quoted 6 lines
drivers/otp/otp.c new file +6
-0+}; + +struct device_type region_type = { + .name ="region" , + .groups = region_attr_groups, +};should these be static ?
Yes, they should.
Quoted 12 lines
drivers/otp/otp.c line 191
Quoted 9 lines
drivers/otp/otp.c new file +8
-0+/* +* Show the current write enable state of the OTP. Users can only program the +* OTP when this shows 'enabled'. +*/ +static ssize_t otp_we_show (struct device *dev,struct device_attribute *attr, +char *buf) +{ +int ret;this func has a
ssize_ttype but you're using int here. seems to come up a few times in this patch.
Ok, I'll go over these and fix them up.
Quoted 6 lines
Quoted 4 lines
+/*
+ * Set the write enable state of the OTP. 'enabled' will enable programming
+ * and 'disabled' will prevent further programming from occuring. On loading"occuring" -> "occurring"
Well spotted!
Quoted 26 lines
Quoted 13 lines
+ * the module, this will default to 'disabled'.
+ */drivers/otp/otp.c new file +10
-0+static ssize_t otp_we_store (struct device *dev,struct device_attribute *attr, +const char *buf,size_t len) +{ +int err =0 ; + +if (down_interruptible (&otp_sem)) +return -ERESTARTSYS ; + +if (sysfs_streq (buf,"enabled" )) + otp_we =1 ;drivers/otp/otp.c line 227
Quoted 10 lines
drivers/otp/otp.c new file +10
-0+else if (sysfs_streq (buf,"disabled" )) + otp_we =0 ; +else + err = -EINVAL ; + +up (&otp_sem); + +return err ?: len; +} +static DEVICE_ATTR(write_enable, S_IRUSR | S_IWUSR, otp_we_show, otp_we_store);you output and accept "enabled" and "disabled" for multiple values. how about unifying these ?
Quoted 1 line
return otp_attr_store_enabled(buf, len, &otp_we);
Good idea.
Quoted 13 lines
Quoted 13 lines
drivers/otp/otp.c new file +12
-0+static ssize_t otp_num_regions_show (struct device *dev, +struct device_attribute *attr,char *buf) +{ +int nr_regions; + +if (down_interruptible (&otp_sem)) +return -ERESTARTSYS ; + + nr_regions = otp->ops->get_nr_regions (otp); + +up (&otp_sem); +
Quoted 8 lines
drivers/otp/otp.c line 297
Quoted 5 lines
drivers/otp/otp.c new file +5
-0+if (nr_regions <=0 ) +return -EINVAL ; + +return sprintf (buf,"%u\n" , nr_regions); +}i'm not sure returning
-EINVALhere makes sense ... shouldnt it just be showing the user the result ofget_nr_regions()?
I guess so, but in that case we're saying that get_nr_regions() shouldn't fail in which case the return type for get_nr_regions() should probably be unsigned.
Quoted 15 lines
Quoted 15 lines
drivers/otp/otp.c new file +14
-0+struct attribute *otp_attrs[] = { + &dev_attr_strict_programming.attr, + &dev_attr_num_regions.attr, + &dev_attr_write_enable.attr, +NULL , +}; + +const struct attribute_group otp_attr_group = { + .attrs = otp_attrs, +}; + +const struct attribute_group *otp_attr_groups[] = { + &otp_attr_group, +NULL ,
Quoted 8 lines
drivers/otp/otp.c line 350
Quoted 6 lines
drivers/otp/otp.c new file +6
-0+}; + +struct device_type otp_type = { + .name ="otp" , + .groups = otp_attr_groups, +};static ?
Yes, will do.
Quoted 10 lines
drivers/otp/otp.c line 358
Quoted 8 lines
drivers/otp/otp.c new file +7
-0+static void otp_dev_release (struct device *dev) +{ +struct otp_device *otp =to_otp_device (dev); + +kfree (otp); + otp =NULL ; +}setting to NULL here is pointless when the pointer is on the stack
That's actually a nasty one, I meant that to be the global otp rather than the shadowing one. Ok, that's really bad and needs fixing!
Quoted 16 lines
Quoted 4 lines
drivers/otp/otp.c new file +3
-0+struct otp_device *otp_device_alloc (struct device *dev, +const struct otp_device_ops *ops, +size_t size)drivers/otp/otp.c line 372
Quoted 10 lines
drivers/otp/otp.c new file +10
-0+{ +struct otp_device *otp_dev =NULL ; +int err = -EINVAL ; + +down (&otp_sem); + +if (dev && !get_device (dev)) { + err = -ENODEV ; + goto out; + }should you really be allowing dev==NULL ? does that setup make sense ?
Originally I didn't have that but when I added the bfin driver this doesn't use a device structure so I made this change. Perhaps the right approach is to require a parent device and make the bfin driver use a platform device?
Quoted 8 lines
drivers/otp/otp.c line 378
Quoted 6 lines
drivers/otp/otp.c new file +5
-0+if (otp) { +pr_warning ("an otp device already registered\n" ); + err = -EBUSY ; + goto out_put; + }you can only register one OTP device in the whole system ??
At the moment yes, but that limitation could probably be removed fairly easily. I'll have a look at doing that.
Quoted 8 lines
drivers/otp/otp.c line 501
Quoted 6 lines
drivers/otp/otp.c new file +5
-0+void otp_region_unregister (struct otp_region *region) +{ +device_unregister (®ion->dev); +} +EXPORT_SYMBOL_GPL (otp_region_unregister);wonder if it'd be better to simply #define this in the global
otp.hheader
This was doing more originally, but yes that would be simpler given what it does now.
Quoted 9 lines
drivers/otp/otp.c line 598
Quoted 7 lines
drivers/otp/otp.c new file +6
-0+static ssize_t otp_write (struct file *filp,const char __user *buf,size_t len, +loff_t *offs) +{ +unsigned pos = (unsigned )*offs; + + len =min (len, region->ops->get_size (region) - (unsigned )*offs);what's with the unsigned cast ? defeats the point of using
loff_tdoesnt it ?
Ok, I'll revisit this.
Quoted 11 lines
drivers/otp/otp.c
Quoted 10 lines
drivers/otp/otp.c new file +9
-0+/* +* We're now aligned to an 8 byte boundary so we can simply copy words +* from userspace and write them into the OTP. +*/ +/* +* We might have less than 8 bytes left so we'll need to do another +* read-modify-write. +*/ +while (len >=OTP_WORD_SIZE ) {
Quoted 1 line
i think "8 byte" should be "necessary byte" ?
Yes, it should.
Quoted 13 lines
drivers/otp/otp.c line 679
Quoted 12 lines
drivers/otp/otp.c new file +11
-0+if (region->ops->read_word (region, pos /OTP_WORD_SIZE , + &word)) { + ret = -EIO ; + goto out; + } + +if (region->ops->write_word (region, pos /OTP_WORD_SIZE , + word)) { + ret = -EIO ; + goto out; + }
Quoted 5 lines
shouldnt you pass the ret value up from read/write word ? this would allow the lower layers to better describe the issue than just
-EIOwouldnt it ?last three comments apply to the read func as well ...
Yes, I'll fix these up.
Quoted 12 lines
drivers/otp/otp.c line 863
Quoted 10 lines
drivers/otp/otp.c new file +9
-0+static int __init otp_init (void ) +{ +int err; + + err =bus_register (&otp_bus_type); +if (err) +return err; + + err =alloc_chrdev_region (&otp_devno,0 ,9 ,"otp" );where'd that magic 9 come from ?
Ok, that's not nice. I'm not sure how to best handle this, perhaps register a chrdev region when allocating the initial otp device and have the backend specify the maximum number of regions?
Quoted 4 lines
Quoted 2 lines
+MODULE_DESCRIPTION("OTP interface driver");
i think this is also a bus driver ?
Fair enough.
Quoted 9 lines
drivers/otp/otp.c
Quoted 6 lines
drivers/otp/otp.c new file +5
-0+/* +* The OTP works in 64 bit words. When we are programming or reading, +* everything is done with 64 bit word addresses. +*/ +#define OTP_WORD_SIZE 8should this be a per-device setting ? or wait until someone who doesnt have 64bit chunks show up ?
I guess it probably should to be generic but then the read_word() and write_word() wrappers need to do some masking and shifting and I figured as it was something I couldn't test it I shouldn't do it. Perhaps make this a per device setting and have a runtime check that only supports 64 bit words for now?
Quoted 5 lines
Quoted 3 lines
+ * struct
otp_device- a picoxcell OTP device.
+ *otp_device_alloc- create a new picoxcell OTP device.this is no longer picoxcell-specific
Ok.
Quoted 4 lines
Quoted 2 lines
+ *
otp_device_unregister- deregister an existing structotp_device."deregister" -> "unregister"
Ok.
Jamie
Re: [RFC PATCHv2 1/4] drivers/otp: add initial support for OTP memory
Quoted 11 lines · Jamie Iles
Quoted 6 lines · Mike Frysinger
Quoted 1 line · Jamie Iles
+What: /sys/bus/otp/devices/otp[a-z][0-9]*/format
you have /sys/bus/otp[a-z]/ and
/sys/bus/otp/devices/? why not unify them ?what are each of these subdirs trying to represent ?
They should all be in
/sys/bus/otp/devicesand otp[a-z] should be the actual otp device and otp[a-z][0-9]+ should be the regions. I'll try and make that a bit clearer and maybe put an example in there.
hrm, i dont think that layout is terribly clean. i dont think regions from diff devices should be at the same level. if we think of otp regions as partitions, then we can use existing standards.
so, how about:
/sys/bus/otp/devices/otp0/region0/
/sys/bus/otp/devices/otp0/region1/
/sys/bus/otp/devices/otp1/region0/
...
Quoted 19 lines
Quoted 12 lines
Documentation/ABI/testing/sysfs-bus-otp line 58
Quoted 11 lines
Documentation/ABI/testing/sysfs-bus-otp new file +11
-0+Description: + The redundancy format of the region. Valid values are: + - single-ended (1 bit of storage per data bit). + - redundant (2 bits of storage, wire-OR'd per data + bit). + - differential (2 bits of storage, differential + voltage per data bit). + - differential-redundant (4 bits of storage, combining + redundant and differential). + It is possible to increase redundancy of a region but care + will be needed if there is data already in the region.Quoted 6 lines
where does ECC fit in ? the Blackfin OTP is structured:
- first 4 pages are write control bitfields for all other pages (so
blowing bit 5 of page 0 prevents further writing to page 5)
- each0x100page region has0x20pages dedicated to ECC for the
other0x80pages ... this provides 1 bit error correction and 2 bits of error detection (anymore and you're screwed!)
Quoted 3 lines
How does the ECC correction work for bfin at the moment? Does the user specify it manually when writing then check it themselves when reading back or does the hardware handle that?
it is taken care of by the layers below the Linux driver. when we say "read a half page", the 64bits are returned via pointer args, and the function return value will tell us if the contents read are valid (ECC passed), corrected (1 bit error), or there was a read error (too may bit errors to recover).
Quoted 5 lines
The way I was thinking that this sort of thing could be handled would be for the ECC to be transparent to the user. Perhaps for bfin the OTP could be registered as 4 regions, otpa1-otpa4 which default to "single-ended". Writing "ecc" as the format would generate the ecc and program it for that region then check the ECC when reading back.
reads and writes atm always have ECC enabled, and the driver restricts writes to the full half page (64bits). if you wanted to be crafty, you could blow individual bits in a single half page over multiple writes before writing out the final ECC, but that isnt supported, and no one has complained.
Quoted 8 lines
Quoted 7 lines
drivers/otp/otp.c line 46
Quoted 4 lines
drivers/otp/otp.c new file +4
-0+static DEFINE_SEMAPHORE (otp_sem); +static int otp_we, otp_strict_programming; +static struct otp_device *otp; +static dev_t otp_devno;hrm, having these be global instead of per-device sounds like a really bad idea ...
Quoted 5 lines
So at the moment the only devices I'm aware of have a single OTP device so I figured the most important thing was to make sure that the sysfs and device node naming permitted for multiple OTP devices in the future without ABI breakage. Having said that, these probably could be moved into the
otp_devicethough so I'll have a go at that.
the ABI step is probably one of the most important pieces, but i dont think moving these fields into the otp_device structure would take long.
Quoted 13 lines
Quoted 4 lines
Quoted 4 lines
drivers/otp/otp.c new file +4
-0+static inline struct otp_region *to_otp_region (struct device *dev) +{ + return dev ? container_of(dev, struct otp_region, dev) : NULL; +}Quoted 8 lines
drivers/otp/otp.c line 60
Quoted 5 lines
drivers/otp/otp.c new file +5
-0+ +static inline struct otp_device *to_otp_device (struct device *dev) +{ + return dev ? container_of(dev, struct otp_device, dev) : NULL; +}do you need the NULL checks ? none of the callers of these funcs check for NULL ...
Quoted 5 lines
I think so, because at least that way if something does go wrong and you dereference the NULL later you should get a nice backtrace, but if you don't do the check then you could have
container_of()yielding something like "NULL -0x4" giving a potentially valid address. If that's the wrong thing to do then I'm happy to change though.
ok, but the callers immediately dereference the return of this without checking NULL, so i dont see the crash output being all that different. and if people are mapping the low page (by manually tweaking mmap_min_addr), then this func wont really make a difference
...
so yes, i'd still say punt it. there's no valid reason for a NULL dev to be seen this low down.
Quoted 7 lines
Quoted 5 lines
drivers/otp/otp.c line 162
Quoted 3 lines
drivers/otp/otp.c new file +3
-0+static struct bus_type otp_bus_type = { + .name ="otp" , +};can this be const ?
No, I don't think so;
bus_register()doesn't want a constbus_type.
stupid bus_register() needs updating then. i certainly wont ask you to do that though :).
Quoted 21 lines
Quoted 12 lines
Quoted 12 lines
drivers/otp/otp.c new file +12
-0+static ssize_t otp_num_regions_show (struct device *dev, +struct device_attribute *attr,char *buf) +{ +int nr_regions; + +if (down_interruptible (&otp_sem)) +return -ERESTARTSYS ; + + nr_regions = otp->ops->get_nr_regions (otp); + +up (&otp_sem); +Quoted 8 lines
drivers/otp/otp.c line 297
Quoted 5 lines
drivers/otp/otp.c new file +5
-0+if (nr_regions <=0 ) +return -EINVAL ; + +return sprintf (buf,"%u\n" , nr_regions); +}i'm not sure returning
-EINVALhere makes sense ... shouldnt it just be showing the user the result ofget_nr_regions()?
Quoted 3 lines
I guess so, but in that case we're saying that
get_nr_regions()shouldn't fail in which case the return type forget_nr_regions()should probably be unsigned.
i'm ok with it returning an error if nr_regions is negative, but i think 0 should be a valid value to show to userspace.
Quoted 16 lines
Quoted 7 lines
Quoted 7 lines
drivers/otp/otp.c new file +7
-0+struct otp_device *otp_device_alloc (struct device *dev, +const struct otp_device_ops *ops, +size_t size) +{ +struct otp_device *otp_dev =NULL ; +int err = -EINVAL ; +Quoted 8 lines
drivers/otp/otp.c line 372
Quoted 6 lines
drivers/otp/otp.c new file +6
-0+down (&otp_sem); + +if (dev && !get_device (dev)) { + err = -ENODEV ; + goto out; + }should you really be allowing dev==NULL ? does that setup make sense ?
Quoted 4 lines
Originally I didn't have that but when I added the bfin driver this doesn't use a device structure so I made this change. Perhaps the right approach is to require a parent device and make the bfin driver use a platform device?
i'm OK with that
Quoted 12 lines
Quoted 11 lines
drivers/otp/otp.c line 863
Quoted 9 lines
drivers/otp/otp.c new file +9
-0+static int __init otp_init (void ) +{ +int err; + + err =bus_register (&otp_bus_type); +if (err) +return err; + + err =alloc_chrdev_region (&otp_devno,0 ,9 ,"otp" );where'd that magic 9 come from ?
Quoted 3 lines
Ok, that's not nice. I'm not sure how to best handle this, perhaps register a chrdev region when allocating the initial otp device and have the backend specify the maximum number of regions?
i dont think there's an issue with dynamically allocating an entire major # for yourself. there's plenty available.
Quoted 9 lines
Quoted 8 lines
include/linux/otp.h line 38
Quoted 5 lines
include/linux/otp.h new file +5
-0+/* +* The OTP works in 64 bit words. When we are programming or reading, +* everything is done with 64 bit word addresses. +*/ +#define OTP_WORD_SIZE 8should this be a per-device setting ? or wait until someone who doesnt have 64bit chunks show up ?
Quoted 5 lines
I guess it probably should to be generic but then the
read_word()andwrite_word()wrappers need to do some masking and shifting and I figured as it was something I couldn't test it I shouldn't do it. Perhaps make this a per device setting and have a runtime check that only supports 64 bit words for now?
using constants also lets gcc do nice optimizations where it otherwise might need to call math funcs to do mult/div/etc... operations. so for now, simply adding an if() check to the register point and returning -ENOSUP when the size isnt 8 bytes is OK. Blackfin OTP min transfer size is 64bits :).
-mike
Re: [RFC PATCHv2 1/4] drivers/otp: add initial support for OTP memory
Quoted 38 lines · Mike Frysinger
Quoted 21 lines · Jamie Iles
Quoted 13 lines · Mike Frysinger
Documentation/ABI/testing/sysfs-bus-otp line 58
Quoted 11 lines · Jamie Iles
Documentation/ABI/testing/sysfs-bus-otp new file +11
-0+Description: + The redundancy format of the region. Valid values are: + - single-ended (1 bit of storage per data bit). + - redundant (2 bits of storage, wire-OR'd per data + bit). + - differential (2 bits of storage, differential + voltage per data bit). + - differential-redundant (4 bits of storage, combining + redundant and differential). + It is possible to increase redundancy of a region but care + will be needed if there is data already in the region.Quoted 6 lines
where does ECC fit in ? the Blackfin OTP is structured:
- first 4 pages are write control bitfields for all other pages (so
blowing bit 5 of page 0 prevents further writing to page 5)
- each0x100page region has0x20pages dedicated to ECC for the
other0x80pages ... this provides 1 bit error correction and 2 bits of error detection (anymore and you're screwed!)Quoted 3 lines
How does the ECC correction work for bfin at the moment? Does the user specify it manually when writing then check it themselves when reading back or does the hardware handle that?
it is taken care of by the layers below the Linux driver. when we say "read a half page", the 64bits are returned via pointer args, and the function return value will tell us if the contents read are valid (ECC passed), corrected (1 bit error), or there was a read error (too may bit errors to recover).
Quoted 5 lines
The way I was thinking that this sort of thing could be handled would be for the ECC to be transparent to the user. Perhaps for bfin the OTP could be registered as 4 regions, otpa1-otpa4 which default to "single-ended". Writing "ecc" as the format would generate the ecc and program it for that region then check the ECC when reading back.
Quoted 5 lines
reads and writes atm always have ECC enabled, and the driver restricts writes to the full half page (64bits). if you wanted to be crafty, you could blow individual bits in a single half page over multiple writes before writing out the final ECC, but that isnt supported, and no one has complained.
The current version I have will do read-modify-write if the file offset isn't OTP word aligned so it sounds like I need to add a flag that says only do full word writes.
I guess in this framework we could make the OTP look like a single region of the 0x80 data pages concatenated or provide separate regions for each group of 0x80 pages but the control and ECC pages aren't directly accessible by the user.
The other question that brings up is whether any compatibility with the old driver is required.
Jamie
Re: [RFC PATCHv2 1/4] drivers/otp: add initial support for OTP memory
Quoted 14 lines · Jamie Iles
Quoted 7 lines · Mike Frysinger
Quoted 5 lines · Jamie Iles
The way I was thinking that this sort of thing could be handled would be for the ECC to be transparent to the user. Perhaps for bfin the OTP could be registered as 4 regions, otpa1-otpa4 which default to "single-ended". Writing "ecc" as the format would generate the ecc and program it for that region then check the ECC when reading back.
Quoted 5 lines
reads and writes atm always have ECC enabled, and the driver restricts writes to the full half page (64bits). if you wanted to be crafty, you could blow individual bits in a single half page over multiple writes before writing out the final ECC, but that isnt supported, and no one has complained.
Quoted 3 lines
The current version I have will do read-modify-write if the file offset isn't OTP word aligned so it sounds like I need to add a flag that says only do full word writes.
yes, only full 64bit writes are allowed.
Quoted 4 lines
I guess in this framework we could make the OTP look like a single region of the
0x80data pages concatenated or provide separate regions for each group of0x80pages but the control and ECC pages aren't directly accessible by the user.
well, in the current driver, i let people dump the ECC. for debugging, i think that's valuable.
Quoted 2 lines
The other question that brings up is whether any compatibility with the old driver is required.
it's already going to be changing names ;). but what other compatibility are you thinking of ? the requirements are you have to be able to:
- read it
- write in full/aligned 64bit chunks
- do an OTPLOCK ioctl (the drivers uses this to manage the first few
pages which flags all other pages as "locked")
-mike
Re: [RFC PATCHv2 1/4] drivers/otp: add initial support for OTP memory
Quoted 8 lines · Mike Frysinger
Quoted 4 lines · Jamie Iles
I guess in this framework we could make the OTP look like a single region of the
0x80data pages concatenated or provide separate regions for each group of0x80pages but the control and ECC pages aren't directly accessible by the user.well, in the current driver, i let people dump the ECC. for debugging, i think that's valuable.
Hmm, that does sound potentially useful. In v3 I've created a single partition that excludes the control bits and the ecc. How about I change that for the next spin so there are two partitions where the first contains the raw data and the second is all of the ECC lumped together. It may also be useful to have a name attribute for each region to make this clear.
It's either that or add another ioctl() for getting the ECC but that doesn't feel very nice.
Quoted 10 lines
Quoted 2 lines
The other question that brings up is whether any compatibility with the old driver is required.
it's already going to be changing names ;). but what other compatibility are you thinking of ? the requirements are you have to be able to:
- read it
- write in full/aligned 64bit chunks
- do an OTPLOCK ioctl (the drivers uses this to manage the first few
pages which flags all other pages as "locked")
Well the current version will do all of that so I think if we add the ECC dumping ability then everything is there.
Jamie
Re: [RFC PATCHv2 1/4] drivers/otp: add initial support for OTP memory
Quoted 10 lines · Jamie Iles
Quoted 8 lines · Mike Frysinger
Quoted 4 lines · Jamie Iles
I guess in this framework we could make the OTP look like a single region of the
0x80data pages concatenated or provide separate regions for each group of0x80pages but the control and ECC pages aren't directly accessible by the user.well, in the current driver, i let people dump the ECC. for debugging, i think that's valuable.
Quoted 6 lines
Hmm, that does sound potentially useful. In v3 I've created a single partition that excludes the control bits and the ecc. How about I change that for the next spin so there are two partitions where the first contains the raw data and the second is all of the ECC lumped together. It may also be useful to have a name attribute for each region to make this clear.
this does sound useful. on Blackfin parts, we've already dedicated specific pages to specific functions. so if there was a name attribute to each region, i could potentially make each one a region. -mike
Re: [RFC PATCHv2 1/4] drivers/otp: add initial support for OTP memory
In reply to Jamie IlesQuoted 8 lines · Jamie Iles
+/*
+ * Copyright 2010-2011 Picochip LTD, Jamie Iles
+ * http://www.picochip.com
+ *
+ * This program is free software; you can redistribute it and/or
+ * modify it under the terms of the GNU General Public License
+ * as published by the Free Software Foundation; either version
+ * 2 of the License, or (at your option) any later version.
Do you _really_ mean "any later version"? Be sure about that please. You have that wording in all of your files you add, please be careful about it as you might have copied from code that did not have that wording (I'm not saying you did, just be sure about this.)
drivers/otp/otp.c line 12
Quoted 4 lines
+ */
drivers/otp/otp.c new file +3
-0+#define pr_fmt (fmt) "otp: " fmt + +#undef DEBUG
What is this for?
Quoted 63 lines
drivers/otp/otp.c new file +63
-0+#include <linux/cdev.h> +#include <linux/delay.h> +#include <linux/err.h> +#include <linux/fs.h> +#include <linux/io.h> +#include <linux/list.h> +#include <linux/module.h> +#include <linux/otp.h> +#include <linux/semaphore.h> +#include <linux/slab.h> +#include <linux/sysfs.h> +#include <linux/uaccess.h> + +static int otp_open (struct inode *inode,struct file *filp); +static int otp_release (struct inode *inode,struct file *filp); +static ssize_t otp_write (struct file *filp,const char __user *buf, +size_t len,loff_t *offs); +static ssize_t otp_read (struct file *filp,char __user *buf,size_t len, +loff_t *offs); +static loff_t otp_llseek (struct file *filp,loff_t offs,int origin); + +static const struct file_operations otp_fops = { + .owner =THIS_MODULE , + .open = otp_open, + .release = otp_release, + .write = otp_write, + .read = otp_read, + .llseek = otp_llseek, +}; + +static DEFINE_SEMAPHORE (otp_sem); +static int otp_we, otp_strict_programming; +static struct otp_device *otp; +static dev_t otp_devno; + +/* +* Given a device for one of the otpN devices, get the corresponding +* otp_region. +*/ +static inline struct otp_region *to_otp_region (struct device *dev) +{ + return dev ? container_of(dev, struct otp_region, dev) : NULL; +} + +static inline struct otp_device *to_otp_device (struct device *dev) +{ + return dev ? container_of(dev, struct otp_device, dev) : NULL; +} + +bool otp_strict_programming_enabled (void ) +{ +return otp_strict_programming; +} +EXPORT_SYMBOL_GPL (otp_strict_programming_enabled); + +static ssize_t otp_format_show (struct device *dev, +struct device_attribute *attr,char *buf) +{ +struct otp_region *region =to_otp_region (dev); +enum otp_redundancy_fmt fmt; +const char *fmt_string; + +if (down_interruptible (&otp_sem))
drivers/otp/otp.c line 85
Quoted 10 lines
drivers/otp/otp.c new file +10
-0+return -ERESTARTSYS ; + +if (region->ops->get_fmt (region)) + fmt = region->ops->get_fmt (region); +else + fmt =OTP_REDUNDANCY_FMT_SINGLE_ENDED ; + +up (&otp_sem); + +if (OTP_REDUNDANCY_FMT_SINGLE_ENDED == fmt)
While some people feel this somehow makes it harder to write bad C code, it's not the kernel style. Please reverse this comparison. If you accidentally put a '=' in there instead of '==', gcc would warn you about it.
drivers/otp/otp.c line 94
Quoted 9 lines
drivers/otp/otp.c new file +9
-0+ fmt_string ="single-ended" ; + else if (OTP_REDUNDANCY_FMT_REDUNDANT == fmt) + fmt_string ="redundant" ; + else if (OTP_REDUNDANCY_FMT_DIFFERENTIAL == fmt) + fmt_string ="differential" ; + else if (OTP_REDUNDANCY_FMT_DIFFERENTIAL_REDUNDANT == fmt) + fmt_string ="differential-redundant" ; + else + fmt_string =NULL ;
Just return -EINVAL here.
Review
Quoted 2 lines
(quoted diff) +2
-0+ + return fmt_string ? sprintf(buf, "%s\n", fmt_string) : -EINVAL;
Then you don't have to do the embedded if in this statement.
Same thing goes for your other show/store functions.
Quoted 6 lines
include/linux/otp.h new file +6
-0+/** +* struct otp_device - a picoxcell OTP device. +* +* @ops: The operations to use for manipulating the device. +* @dev: The parent device (typically a platform_device) that +* provides the OTP.
include/linux/otp.h line 89
Quoted 10 lines
include/linux/otp.h new file +10
-0+* @regions: The regions registered to the device. +* @size: The size of the OTP in bytes. +* @driver_data: Private driver data. +*/ +struct otp_device { +const struct otp_device_ops *ops; +struct device dev; +struct list_head regions; +size_t size; +void *driver_data;
Why do you need this pointer, can't you use the one in struct device that is there for this purpose? Then provide a get/set function to access this field so that a driver doesn't go and poke in it directly.
thanks,
greg k-h
Re: [RFC PATCHv2 1/4] drivers/otp: add initial support for OTP memory
Hi Greg,
Thanks for the review, a few comments inline.
Jamie
Quoted 10 lines · Greg KH
Quoted 8 lines · Jamie Iles
+/*
+ * Copyright 2010-2011 Picochip LTD, Jamie Iles
+ * http://www.picochip.com
+ *
+ * This program is free software; you can redistribute it and/or
+ * modify it under the terms of the GNU General Public License
+ * as published by the Free Software Foundation; either version
+ * 2 of the License, or (at your option) any later version.
Quoted 4 lines
Do you _really_ mean "any later version"? Be sure about that please. You have that wording in all of your files you add, please be careful about it as you might have copied from code that did not have that wording (I'm not saying you did, just be sure about this.)
No I didn't mean to do that and I'm not sure where that came from. I'll fix this up and take a look at some of my other patches! Thanks for spotting that one.
Quoted 7 lines
drivers/otp/otp.c line 12
Quoted 5 lines
+ */
drivers/otp/otp.c new file +3
-0+#define pr_fmt (fmt) "otp: " fmt + +#undef DEBUGWhat is this for?
That shouldn't be there, I'll get rid of that.
Quoted 71 lines
Quoted 71 lines
drivers/otp/otp.c new file +70
-0+#include <linux/cdev.h> +#include <linux/delay.h> +#include <linux/err.h> +#include <linux/fs.h> +#include <linux/io.h> +#include <linux/list.h> +#include <linux/module.h> +#include <linux/otp.h> +#include <linux/semaphore.h> +#include <linux/slab.h> +#include <linux/sysfs.h> +#include <linux/uaccess.h> + +static int otp_open (struct inode *inode,struct file *filp); +static int otp_release (struct inode *inode,struct file *filp); +static ssize_t otp_write (struct file *filp,const char __user *buf, +size_t len,loff_t *offs); +static ssize_t otp_read (struct file *filp,char __user *buf,size_t len, +loff_t *offs); +static loff_t otp_llseek (struct file *filp,loff_t offs,int origin); + +static const struct file_operations otp_fops = { + .owner =THIS_MODULE , + .open = otp_open, + .release = otp_release, + .write = otp_write, + .read = otp_read, + .llseek = otp_llseek, +}; + +static DEFINE_SEMAPHORE (otp_sem); +static int otp_we, otp_strict_programming; +static struct otp_device *otp; +static dev_t otp_devno; + +/* +* Given a device for one of the otpN devices, get the corresponding +* otp_region. +*/ +static inline struct otp_region *to_otp_region (struct device *dev) +{ + return dev ? container_of(dev, struct otp_region, dev) : NULL; +} + +static inline struct otp_device *to_otp_device (struct device *dev) +{ + return dev ? container_of(dev, struct otp_device, dev) : NULL; +} + +bool otp_strict_programming_enabled (void ) +{ +return otp_strict_programming; +} +EXPORT_SYMBOL_GPL (otp_strict_programming_enabled); + +static ssize_t otp_format_show (struct device *dev, +struct device_attribute *attr,char *buf) +{ +struct otp_region *region =to_otp_region (dev); +enum otp_redundancy_fmt fmt; +const char *fmt_string; + +if (down_interruptible (&otp_sem)) +return -ERESTARTSYS ; + +if (region->ops->get_fmt (region)) + fmt = region->ops->get_fmt (region); +else + fmt =OTP_REDUNDANCY_FMT_SINGLE_ENDED ; +
Quoted 8 lines
drivers/otp/otp.c line 85
Quoted 3 lines
drivers/otp/otp.c new file +3
-0+up (&otp_sem); + +if (OTP_REDUNDANCY_FMT_SINGLE_ENDED == fmt)While some people feel this somehow makes it harder to write bad C code, it's not the kernel style. Please reverse this comparison. If you accidentally put a '=' in there instead of '==', gcc would warn you about it.
I'm making a conscience effort _not_ to do that but it's still ingrained at the back of my mind and this code has been lingering about for a bit.
Quoted 13 lines
drivers/otp/otp.c line 94
Quoted 10 lines
drivers/otp/otp.c new file +9
-0+ fmt_string ="single-ended" ; + else if (OTP_REDUNDANCY_FMT_REDUNDANT == fmt) + fmt_string ="redundant" ; + else if (OTP_REDUNDANCY_FMT_DIFFERENTIAL == fmt) + fmt_string ="differential" ; + else if (OTP_REDUNDANCY_FMT_DIFFERENTIAL_REDUNDANT == fmt) + fmt_string ="differential-redundant" ; + else + fmt_string =NULL ;Just return
-EINVALhere.
Quoted 6 lines
Review
Quoted 2 lines
(quoted diff) +2
-0+ + return fmt_string ? sprintf(buf, "%s\n", fmt_string) : -EINVAL;Then you don't have to do the embedded if in this statement.
Same thing goes for your other show/store functions.
Ok, I'll clean these up.
Quoted 13 lines
Quoted 13 lines
include/linux/otp.h new file +12
-0+/** +* struct otp_device - a picoxcell OTP device. +* +* @ops: The operations to use for manipulating the device. +* @dev: The parent device (typically a platform_device) that +* provides the OTP. +* @regions: The regions registered to the device. +* @size: The size of the OTP in bytes. +* @driver_data: Private driver data. +*/ +struct otp_device { +const struct otp_device_ops *ops;
Quoted 8 lines
include/linux/otp.h line 89
Quoted 4 lines
include/linux/otp.h new file +4
-0+struct device dev; +struct list_head regions; +size_t size; +void *driver_data;Why do you need this pointer, can't you use the one in struct device that is there for this purpose? Then provide a get/set function to access this field so that a driver doesn't go and poke in it directly.
Hmm, I thought I'd got rid of this; it isn't actually being used. I am using the one in struct device but I haven't added the getter and setter so I'll put those in for next time.
Jamie
Arnd Bergmann
maintainerRe: [RFC PATCHv2 1/4] drivers/otp: add initial support for OTP memory
In reply to Jamie IlesQuoted 5 lines · On Thursday 24 March 2011 16:21:08 Jamie Iles wrote:
OTP memory is typically found in some embedded devices and can be used for storing boot code, cryptographic keys and other persistent information onchip. This patch adds a generic layer that devices can register OTP with and allows access through a set of character device nodes.
Looks very nice overall, but I have a few minor comments.
Quoted 7 lines
Documentation/ABI/testing/sysfs-bus-otp new file +1
-0@@ -0,0 +1,68 @@ +What: /sys/bus/otp/
Documentation/ABI/testing/sysfs-bus-otp line 11
Quoted 10 lines
Documentation/ABI/testing/sysfs-bus-otp new file +10
-0+Date: March 2011 +KernelVersion: 2.6.40+ +Contact: Jamie Iles <[email protected]> +Description: + The otp bus presents a number of devices where each + device represents a region or device in the SoC. Each region + will create a device node which allows the region to be + written with read()/write() calls and the device on the bus + has attributes for controlling the redundancy format and + getting the region size.
Why is this a bus? You don't have any device matching code or similar, and the devices typically are on an existing bus_type (e.g. platform_bus). I think it would make more sense to do this as a class.
Documentation/ABI/testing/sysfs-bus-otp line 18
Quoted 7 lines
Documentation/ABI/testing/sysfs-bus-otp new file +7
-0+ +menuconfig OTP + bool "OTP memory support" + help + Say y here to support OTP memory found in some embedded devices. + This memory can commonly be used to store boot code, cryptographic + keys and other persistent data.
Please spell out OTP at least once here, a lot of people might not know the TLA.
Quoted 7 lines
Documentation/ABI/testing/sysfs-bus-otp new file +7
-0+ +static int otp_open(struct inode *inode, struct file *filp); +static int otp_release(struct inode *inode, struct file *filp); +static ssize_t otp_write(struct file *filp, const char __user *buf, + size_t len, loff_t *offs); +static ssize_t otp_read(struct file *filp, char __user *buf, size_t len, + loff_t *offs);
Documentation/ABI/testing/sysfs-bus-otp line 35
Quoted 10 lines
Documentation/ABI/testing/sysfs-bus-otp new file +10
-0+static loff_t otp_llseek(struct file *filp, loff_t offs, int origin); + +static const struct file_operations otp_fops = { + .owner = THIS_MODULE, + .open = otp_open, + .release = otp_release, + .write = otp_write, + .read = otp_read, + .llseek = otp_llseek, +};
The common way to organize source files is to put all definitions in an order that requires no forward declarations.
Just define the operations first, then the struct file_operations, and then the rest.
Documentation/ABI/testing/sysfs-bus-otp line 37
Quoted 2 lines
Documentation/ABI/testing/sysfs-bus-otp new file +2
-0+ +static DEFINE_SEMAPHORE(otp_sem);
Semaphores are basically deprecated. Use a mutex instead.
Documentation/ABI/testing/sysfs-bus-otp line 40
Quoted 3 lines
Documentation/ABI/testing/sysfs-bus-otp new file +3
-0+static int otp_we, otp_strict_programming; +static struct otp_device *otp; +static dev_t otp_devno;
Making these global variables limits you to a single device. I think you can put everything here into struct otp_device and find that from the device or chardev you get passed when entering the otp code.
Documentation/ABI/testing/sysfs-bus-otp line 47
Quoted 7 lines
Documentation/ABI/testing/sysfs-bus-otp new file +7
-0+static void otp_dev_release(struct device *dev) +{ + struct otp_device *otp = to_otp_device(dev); + + kfree(otp); + otp = NULL; +}
The final assignment is not needed.
Arnd
Re: [RFC PATCHv2 1/4] drivers/otp: add initial support for OTP memory
Quoted 22 lines · Arnd Bergmann
Quoted 5 lines · On Thursday 24 March 2011 16:21:08 Jamie Iles wrote:
OTP memory is typically found in some embedded devices and can be used for storing boot code, cryptographic keys and other persistent information onchip. This patch adds a generic layer that devices can register OTP with and allows access through a set of character device nodes.
Looks very nice overall, but I have a few minor comments.
Quoted 13 lines
Documentation/ABI/testing/sysfs-bus-otp new file +7
-0@@ -0,0 +1,68 @@ +What: /sys/bus/otp/ +Date: March 2011 +KernelVersion: 2.6.40+ +Contact: Jamie Iles <[email protected]> +Description: + The otp bus presents a number of devices where each + device represents a region or device in the SoC. Each region
Quoted 8 lines
Documentation/ABI/testing/sysfs-bus-otp line 11
Quoted 4 lines
Documentation/ABI/testing/sysfs-bus-otp new file +4
-0+ will create a device node which allows the region to be + written with read()/write() calls and the device on the bus + has attributes for controlling the redundancy format and + getting the region size.Why is this a bus? You don't have any device matching code or similar, and the devices typically are on an existing
bus_type(e.g.platform_bus). I think it would make more sense to do this as a class.
No, for new things, we want to use busses instead of classes please. Especially as this does create devices, which are best put on a bus somewhere.
thanks,
greg k-h
Arnd Bergmann
maintainerRe: [RFC PATCHv2 1/4] drivers/otp: add initial support for OTP memory
Quoted 7 lines · On Friday 25 March 2011 22:12:46 Greg KH wrote:
Quoted 3 lines
Why is this a bus? You don't have any device matching code or similar, and the devices typically are on an existing
bus_type(e.g.platform_bus). I think it would make more sense to do this as a class.No, for new things, we want to use busses instead of classes please. Especially as this does create devices, which are best put on a bus somewhere.
I don't understand. Isn't that rather inconsistent?
I realize the same thing came up with the IIO subsystem, where I also didn't understand it.
In my mental model of Linux drivers, a bus is something that physically connects devices and the bus code matches devices with drivers, while a class groups logical devices that get created by the driver itself.
Arnd
Re: [RFC PATCHv2 1/4] drivers/otp: add initial support for OTP memory
Quoted 9 lines · Arnd Bergmann
Quoted 7 lines · On Friday 25 March 2011 22:12:46 Greg KH wrote:
Quoted 3 lines
Why is this a bus? You don't have any device matching code or similar, and the devices typically are on an existing
bus_type(e.g.platform_bus). I think it would make more sense to do this as a class.No, for new things, we want to use busses instead of classes please. Especially as this does create devices, which are best put on a bus somewhere.
Quoted 8 lines
I don't understand. Isn't that rather inconsistent?
I realize the same thing came up with the IIO subsystem, where I also didn't understand it.
In my mental model of Linux drivers, a bus is something that physically connects devices and the bus code matches devices with drivers, while a class groups logical devices that get created by the driver itself.
Yes, that is how it used to be, but then it turns out that both of them are really just "subsystems" as far as it all goes. They are just ways that devices are bound to drivers in a logical manner. We have patches floating around that get rid of both busses and classes to merge them together, which is the end goal here. I know Kay has posted detailed reasons for why this all is on lkml in the past, and had working code about 5 years ago, it's just been slow going...
So we recommend all new subsystems use the bus code, it's simple and works well.
thanks,
greg k-h
Arnd Bergmann
maintainerRe: [RFC PATCHv2 1/4] drivers/otp: add initial support for OTP memory
Quoted 7 lines · On Saturday 26 March 2011 00:28:47 Greg KH wrote:
Yes, that is how it used to be, but then it turns out that both of them are really just "subsystems" as far as it all goes. They are just ways that devices are bound to drivers in a logical manner. We have patches floating around that get rid of both busses and classes to merge them together, which is the end goal here. I know Kay has posted detailed reasons for why this all is on lkml in the past, and had working code about 5 years ago, it's just been slow going...
How will that work? I suppose we can't really change the directory structure anyway, so to users it will still look like it does today, even if the kernel just uses the same code for bus and class internally.
Quoted 2 lines
So we recommend all new subsystems use the bus code, it's simple and works well.
I'm not worried about the code at all, just how it shows up in sysfs, which is rather confusing when things become a bus that are not really buses by any stretch. Obviously there are grey areas and corner cases where a bus makes as much sense as a class, but I think this one is not such a case.
Arnd
Re: [RFC PATCHv2 1/4] drivers/otp: add initial support for OTP memory
Quoted 10 lines · Arnd Bergmann
Quoted 7 lines · On Saturday 26 March 2011 00:28:47 Greg KH wrote:
Yes, that is how it used to be, but then it turns out that both of them are really just "subsystems" as far as it all goes. They are just ways that devices are bound to drivers in a logical manner. We have patches floating around that get rid of both busses and classes to merge them together, which is the end goal here. I know Kay has posted detailed reasons for why this all is on lkml in the past, and had working code about 5 years ago, it's just been slow going...
Quoted 3 lines
How will that work? I suppose we can't really change the directory structure anyway, so to users it will still look like it does today, even if the kernel just uses the same code for bus and class internally.
Yes. But we can have symlinks instead of "real" /sys/class and /sys/bus directories like we do for /sys/block today. All of the major tools that use sysfs work properly with a /sys/subsystem/ setup today thanks to Kay's efforts.
thanks,
greg k-h
Arnd Bergmann
maintainerRe: [RFC PATCHv2 1/4] drivers/otp: add initial support for OTP memory
Quoted 11 lines · Greg KH
Quoted 10 lines · Arnd Bergmann
Quoted 7 lines · On Saturday 26 March 2011 00:28:47 Greg KH wrote:
Yes, that is how it used to be, but then it turns out that both of them are really just "subsystems" as far as it all goes. They are just ways that devices are bound to drivers in a logical manner. We have patches floating around that get rid of both busses and classes to merge them together, which is the end goal here. I know Kay has posted detailed reasons for why this all is on lkml in the past, and had working code about 5 years ago, it's just been slow going...
Quoted 8 lines
Quoted 3 lines
How will that work? I suppose we can't really change the directory structure anyway, so to users it will still look like it does today, even if the kernel just uses the same code for bus and class internally.
Yes. But we can have symlinks instead of "real"
/sys/classand/sys/busdirectories like we do for/sys/blocktoday. All of the major tools that use sysfs work properly with a/sys/subsystem/setup today thanks to Kay's efforts.
I see. The migration from /sys/bus to /sys/subsystem plus symlinks makes sense, but I still think that having symlinks that make sense would be better than having symlinks that are purely there for historical reasons.
One major flaw I see with registering abstract subsystems as a bus is that a bus connected to the concept of a device_driver, and there is a list of drivers with their respective devices in /sys/bus/*/drivers, which would always be empty in this case. Similarly, the /sys/bus/*/devices/*/driver symlinks for these subsystems would point to drivers on other buses, unlike the respective symlinks for real buses.
Arnd
Re: [RFC PATCHv2 1/4] drivers/otp: add initial support for OTP memory
In reply to Arnd BergmannHi Arnd,
Quoted 8 lines · Arnd Bergmann
Quoted 5 lines · On Thursday 24 March 2011 16:21:08 Jamie Iles wrote:
OTP memory is typically found in some embedded devices and can be used for storing boot code, cryptographic keys and other persistent information onchip. This patch adds a generic layer that devices can register OTP with and allows access through a set of character device nodes.
Looks very nice overall, but I have a few minor comments.
Thanks for taking the time to look at this. I think I've addressed all of your comments in the v3 series apart from defining OTP (which really does need to be done so I'll make sure I do that in v4) and the sysfs bus/class stuff. I understand that the intention is for a bus to actually represent a subsystem so that does make sense to me (I think!).
Jamie
Arnd Bergmann
maintainerRe: [RFC PATCHv2 1/4] drivers/otp: add initial support for OTP memory
Quoted 4 lines · On Friday 25 March 2011 23:23:46 Jamie Iles wrote:
Thanks for taking the time to look at this. I think I've addressed all of your comments in the v3 series apart from defining OTP (which really does need to be done so I'll make sure I do that in v4) and the sysfs bus/class stuff.
Wow, that was quick!
One more thing that I just realized:
I think it would be better not to allow arbitrary ioctl commands to be interpreted by the individual drivers. Instead, interpret them in the common code and pass the data to the drivers through separate otp_device_ops function pointers, one per ioctl command.
This will reduce the amount of code needed in each driver when you have multiple ones implementing the same ioctls, and help to ensure that they all treat the arguments in the same way.
Also, you should have a compat_ioctl file operation. As long as your data structures are compatible between 32 and 64 bit, it can point to the same function as the .unlocked_ioctl one.
Arnd
Re: [RFC PATCHv2 1/4] drivers/otp: add initial support for OTP memory
Quoted 9 lines · Arnd Bergmann
One more thing that I just realized:
I think it would be better not to allow arbitrary ioctl commands to be interpreted by the individual drivers. Instead, interpret them in the common code and pass the data to the drivers through separateotp_device_opsfunction pointers, one per ioctl command.This will reduce the amount of code needed in each driver when you have multiple ones implementing the same ioctls, and help to ensure that they all treat the arguments in the same way.
i think we should do what the rtc framework did ... there are common ioctls which call specific function pointers in the driver, and any unhandled ioctls get passed to the driver-specific ioctl function. this should cover everyone's needs.
-mike
Re: [RFC PATCHv2 1/4] drivers/otp: add initial support for OTP memory
Quoted 7 lines · Mike Frysinger
Quoted 6 lines · Arnd Bergmann
One more thing that I just realized:
I think it would be better not to allow arbitrary ioctl commands to be interpreted by the individual drivers. Instead, interpret them in the common code and pass the data to the drivers through separateotp_device_opsfunction pointers, one per ioctl command.
Quoted 8 lines
Quoted 3 lines
This will reduce the amount of code needed in each driver when you have multiple ones implementing the same ioctls, and help to ensure that they all treat the arguments in the same way.
i think we should do what the rtc framework did ... there are common ioctls which call specific function pointers in the driver, and any unhandled ioctls get passed to the driver-specific ioctl function. this should cover everyone's needs.
That's what I intended but I guess it isn't that clear from the code. Perhaps instead of:
mutex_lock(); region->ops->ioctl(); mutex_unlock();
if I change it to:
mutex_lock();
switch (cmd) {
default:
region->ops->ioctl();
}
mutex_unlock();then that's a bit clearer. If there's stuff common across different OTP implementations then we can add it to the region ops and decode it here and if not fall back to the specific implementation.
Jamie
Arnd Bergmann
maintainerRe: [RFC PATCHv2 1/4] drivers/otp: add initial support for OTP memory
Quoted 16 lines · On Friday 25 March 2011 23:52:20 Jamie Iles wrote:
That's what I intended but I guess it isn't that clear from the code. Perhaps instead of:
mutex_lock(); region->ops->ioctl(); mutex_unlock();if I change it to:
mutex_lock(); switch (cmd) { default: region->ops->ioctl(); } mutex_unlock();
Quoted 3 lines
then that's a bit clearer. If there's stuff common across different OTP implementations then we can add it to the region ops and decode it here and if not fall back to the specific implementation.
This will make it a lot harder to support compat_ioctl as well. Just drop the ->ioctl callback into the driver and make one callback per command. Any command that could be reused on another driver will then be defined in the common header file already and just work on new drivers.
If you are tempted to add commands that cannot possibly work on other hardware, that is usually a good indication that this command should not be added, or should be generalized in some way.
Arnd
Re: [RFC PATCHv2 1/4] drivers/otp: add initial support for OTP memory
Quoted 21 lines · Arnd Bergmann
Quoted 16 lines · On Friday 25 March 2011 23:52:20 Jamie Iles wrote:
That's what I intended but I guess it isn't that clear from the code. Perhaps instead of:
mutex_lock(); region->ops->ioctl(); mutex_unlock();if I change it to:
mutex_lock(); switch (cmd) { default: region->ops->ioctl(); } mutex_unlock();Quoted 3 lines
then that's a bit clearer. If there's stuff common across different OTP implementations then we can add it to the region ops and decode it here and if not fall back to the specific implementation.
Quoted 5 lines
This will make it a lot harder to support
compat_ioctlas well. Just drop the ->ioctl callback into the driver and make one callback per command. Any command that could be reused on another driver will then be defined in the common header file already and just work on new drivers.
OK, you've convinced me :-)
The only use of an ioctl() at the moment is for locking portions of the OTP so perhaps we add a new region op .lock(unsigned long start_addr, size_t nr_words).
For the actual ioctl() we should assume byte addressing rather than words though and do the conversion in the driver so we can cope with devices that don't have 64-bit words and do the locking on a looping word-by-word basis.
struct otp_lock_req {
__u32 start_addr;
__u32 byte_count;
};The start_addr would be relative to the start of the region.
Mike, would this be OK with you if we used a different ioctl() to the one bfin-otp is using currently? I notice that it's using the OTPLOCK ioctl() from MTD but I think it's using the argument in a different way.
Jamie
Re: [RFC PATCHv2 1/4] drivers/otp: add initial support for OTP memory
Quoted 9 lines · Jamie Iles
For the actual
ioctl()we should assume byte addressing rather than words though and do the conversion in the driver so we can cope with devices that don't have 64-bit words and do the locking on a looping word-by-word basis.struct
otp_lock_req{
__u32start_addr;
__u32byte_count;
};
i would add an ABI field here too so if in the future we want to add stuff, we can do so without adding new ioctls. like "u16 version; u16 flags;". then in the ioctl, if version isnt 0, we return ENOTSUP. in the future, we can add flags or bump the version.
Quoted 3 lines
Mike, would this be OK with you if we used a different
ioctl()to the one bfin-otp is using currently? I notice that it's using the OTPLOCKioctl()from MTD but I think it's using the argument in a different way.
i re-used OTPLOCK because it's exactly the name i wanted and it was easier than carving out my own namespace, but the args are different. i can see how people might find this undesirable.
-mike
Re: [RFC PATCHv2 1/4] drivers/otp: add initial support for OTP memory
Quoted 11 lines · Mike Frysinger
Quoted 9 lines · Jamie Iles
For the actual
ioctl()we should assume byte addressing rather than words though and do the conversion in the driver so we can cope with devices that don't have 64-bit words and do the locking on a looping word-by-word basis.struct
otp_lock_req{
__u32start_addr;
__u32byte_count;
};
Quoted 4 lines
i would add an ABI field here too so if in the future we want to add stuff, we can do so without adding new ioctls. like "u16 version; u16 flags;". then in the ioctl, if version isnt 0, we return ENOTSUP. in the future, we can add flags or bump the version.
Sounds like a good idea.
Quoted 7 lines
Quoted 3 lines
Mike, would this be OK with you if we used a different
ioctl()to the one bfin-otp is using currently? I notice that it's using the OTPLOCKioctl()from MTD but I think it's using the argument in a different way.i re-used OTPLOCK because it's exactly the name i wanted and it was easier than carving out my own namespace, but the args are different. i can see how people might find this undesirable.
OK, well seeing as we're making this generic we may as well reserve a range of ioctls. I'll reserve a few of them and add OTP_LOCK_AREA for now.
Jamie
Arnd Bergmann
maintainerRe: [RFC PATCHv2 1/4] drivers/otp: add initial support for OTP memory
In reply to Mike FrysingerQuoted 10 lines · On Saturday 26 March 2011 03:16:48 Mike Frysinger wrote:
Quoted 9 lines · Jamie Iles
For the actual
ioctl()we should assume byte addressing rather than words though and do the conversion in the driver so we can cope with devices that don't have 64-bit words and do the locking on a looping word-by-word basis.struct otp_lock_req { __u32 start_addr; __u32 byte_count; };
Looks good to me.
Quoted 4 lines
i would add an ABI field here too so if in the future we want to add stuff, we can do so without adding new ioctls. like "u16 version; u16 flags;". then in the ioctl, if version isnt 0, we return ENOTSUP. in the future, we can add flags or bump the version.
No, versioned APIs are just a pain to maintain, it's easier to add new ioctl commands when necessary.
Note that the kernel would always have to support all versions of the API anyway.
Arnd
Re: [RFC PATCHv2 1/4] drivers/otp: add initial support for OTP memory
Quoted 19 lines · Arnd Bergmann
Quoted 10 lines · On Saturday 26 March 2011 03:16:48 Mike Frysinger wrote:
Quoted 9 lines · Jamie Iles
For the actual
ioctl()we should assume byte addressing rather than words though and do the conversion in the driver so we can cope with devices that don't have 64-bit words and do the locking on a looping word-by-word basis.struct
otp_lock_req{
__u32start_addr;
__u32byte_count;
};Looks good to me.
Quoted 4 lines
i would add an ABI field here too so if in the future we want to add stuff, we can do so without adding new ioctls. like "u16 version; u16 flags;". then in the ioctl, if version isnt 0, we return ENOTSUP. in the future, we can add flags or bump the version.
Quoted 4 lines
No, versioned APIs are just a pain to maintain, it's easier to add new ioctl commands when necessary.
Note that the kernel would always have to support all versions of the API anyway.
adding at least a flags field then should be fine
-mike
Arnd Bergmann
maintainerRe: [RFC PATCHv2 1/4] drivers/otp: add initial support for OTP memory
Quoted 11 lines · On Saturday 26 March 2011 18:55:39 Mike Frysinger wrote:
Quoted 9 lines
Quoted 4 lines
i would add an ABI field here too so if in the future we want to add stuff, we can do so without adding new ioctls. like "u16 version; u16 flags;". then in the ioctl, if version isnt 0, we return ENOTSUP. in the future, we can add flags or bump the version.
No, versioned APIs are just a pain to maintain, it's easier to add new ioctl commands when necessary.
Note that the kernel would always have to support all versions of the API anyway.adding at least a flags field then should be fine
Fine with me, if you can name at least one possible use for a flag.
I think it would be better without a flags field, but it's not a problem if you insist on having it.
Arnd
Re: [RFC PATCHv2 1/4] drivers/otp: add initial support for OTP memory
Quoted 6 lines · Arnd Bergmann
Quoted 5 lines · On Saturday 26 March 2011 18:55:39 Mike Frysinger wrote:
Quoted 5 lines
Quoted 4 lines
i would add an ABI field here too so if in the future we want to add stuff, we can do so without adding new ioctls. like "u16 version; u16 flags;". then in the ioctl, if version isnt 0, we return ENOTSUP. in the future, we can add flags or bump the version.
Quoted 8 lines
Quoted 6 lines
Quoted 4 lines
No, versioned APIs are just a pain to maintain, it's easier to add new ioctl commands when necessary.
Note that the kernel would always have to support all versions of the API anyway.adding at least a flags field then should be fine
Fine with me, if you can name at least one possible use for a flag.
i tend to assume that i cant think of all cases when i first write up something, so better to leave a hole for the future ;) -mike
Arnd Bergmann
maintainerRe: [RFC PATCHv2 1/4] drivers/otp: add initial support for OTP memory
Quoted 5 lines · On Sunday 27 March 2011 05:52:04 Mike Frysinger wrote:
Quoted 1 line · Arnd Bergmann
Fine with me, if you can name at least one possible use for a flag.
i tend to assume that i cant think of all cases when i first write up something, so better to leave a hole for the future ;)
This makes sense for real syscalls, where adding a new one is a lot of work. For an ioctl, adding a flag is almost exactly the same amount of work as adding another ioctl command, but it adds complexity for the first one, e.g. you need to decide how to handle unknown flags passed to the kernel (ignore, bail out, or a combination).
It also makes correctly using the ioctl harder and adds a tiny amount of overhead for parsing the flags and copying the addititional word. Adding a flags word "just in case" is therfore counterproductive.
Arnd
Arnd Bergmann
maintainerRe: [RFC PATCHv2 1/4] drivers/otp: add initial support for OTP memory
In reply to Mike FrysingerQuoted 7 lines · On Friday 25 March 2011 23:38:00 Mike Frysinger wrote:
Quoted 6 lines · Arnd Bergmann
One more thing that I just realized:
I think it would be better not to allow arbitrary ioctl commands to be interpreted by the individual drivers. Instead, interpret them in the common code and pass the data to the drivers through separateotp_device_opsfunction pointers, one per ioctl command.
Quoted 8 lines
Quoted 3 lines
This will reduce the amount of code needed in each driver when you have multiple ones implementing the same ioctls, and help to ensure that they all treat the arguments in the same way.
i think we should do what the rtc framework did ... there are common ioctls which call specific function pointers in the driver, and any unhandled ioctls get passed to the driver-specific ioctl function. this should cover everyone's needs.
It depends on how obscure the ioctls get, but I think the first attempt should always be to standardize them across the subsystem. A lot of things are wrong with the whole concept, but when they are needed, you should at least try to shield individual drivers from them.
Many subsystems end up needing both common and driver specific ioctls as they grow, I just wouldn't do that to start out with. There's always hope that the latest subsystem will be better than the others ;-).
Arnd
RFC PATCH v2 2/4 drivers/otp: add support for Picoxcell PC3X3 OTP
In reply to Jamie IlesThe OTP in PC3X3 is 16KB and can be split into 1, 2, 4 or 8 regions each taking a different redundancy format. The formats increase the redundancy of the memory at the expense of bit storage to help protect against programming errors for devices in the field.
--- drivers/otp/Kconfig | 11 + drivers/otp/Makefile | 1 + drivers/otp/otp_pc3x3.c | 1079 +++++++++++++++++++++++++++++++++++++++++++++++ 3 files changed, 1091 insertions(+), 0 deletions(-) create mode 100644 drivers/otp/otp_pc3x3.c
drivers/otp/Kconfig +11 -0
@@ -8,3 +8,14 @@ menuconfig OTP Say y here to support OTP memory found in some embedded devices. This memory can commonly be used to store boot code, cryptographic keys and other persistent data. + +if OTP + +config OTP_PC3X3 + tristate "Enable support for Picochip PC3X3 OTP" + depends on OTP && ARCH_PICOXCELL + help + Say y or m here to allow support for the OTP found in PC3X3 devices. + If you say m then the module will be called otp_pc3x3. + +endif
drivers/otp/Makefile +1 -0
@@ -1 +1,2 @@ obj-$(CONFIG_OTP) += otp.o +obj-$(CONFIG_OTP_PC3X3) += otp_pc3x3.o
drivers/otp/otp_pc3x3.c new file +1079 -0
1,079 lines not on this page · raw message
Signature · 1 line
1.7.4
Re: [RFC PATCHv2 2/4] drivers/otp: add support for Picoxcell PC3X3 OTP
drivers/otp/Kconfig menuconfig OTP · line 16
Quoted 12 lines · Jamie Iles
drivers/otp/Kconfig +6
-0@@ -8,3 +8,14 @@ menuconfig OTP Say y here to support OTP memory found in some embedded devices. This memory can commonly be used to store boot code, cryptographic keys and other persistent data. + +if OTP + +config OTP_PC3X3 + tristate "Enable support for Picochip PC3X3 OTP" + depends on OTP && ARCH_PICOXCELL
since every driver is going to need "depend OTP", the "if OTP" is redundant then right ?
drivers/otp/Kconfig menuconfig OTP · line 19
Quoted 3 lines
drivers/otp/Kconfig +3
-0+ help + Say y or m here to allow support for the OTP found in PC3X3 devices. + If you say m then the module will be called otp_pc3x3.
"y" -> "y"
"m" -> "M"
Quoted 200 lines
drivers/otp/Kconfig +2
-0+ +endifdrivers/otp/Makefile +1
-0@@ -1 +1,2 @@ obj-$(CONFIG_OTP) += otp.o +obj-$(CONFIG_OTP_PC3X3) += otp_pc3x3.odrivers/otp/otp_pc3x3.c new file +185
-0@@ -0,0 +1,1079 @@ +/* +* Copyright 2010-2011 Picochip LTD, Jamie Iles +* http://www.picochip.com +* +* This program is free software; you can redistribute it and/or +* modify it under the terms of the GNU General Public License +* as published by the Free Software Foundation; either version +* 2 of the License, or (at your option) any later version. +* +* This driver implements a picoxcellotp backend for reading and writing the +* OTP memory in Picochip PC3X3 devices. This OTP can be used for executing +* secure boot code or for the secure storage of keys and any other user data. +*/ +#define pr_fmt (fmt) "pc3x3otp: " fmt + +#include <linux/bitops.h> +#include <linux/clk.h> +#include <linux/delay.h> +#include <linux/err.h> +#include <linux/io.h> +#include <linux/module.h> +#include <linux/otp.h> +#include <linux/platform_device.h> + +/* +* To test the user interface and most of the driver logic, we have a test +* mode whereby rather than writing to OTP we have a RAM buffer that simulates +* the OTP. This means that we can test everything apart from: +* +* - The OTP state machines and commands. +* - Failure to program bits. +*/ +static int test_mode; +module_param (test_mode, bool,S_IRUSR ); +MODULE_PARM_DESC (test_mode, +"Run in test mode (use a memory buffer rather than OTP" ); + +/* +* This is the maximum number of times to try and soak a failed bit. We get +* this from the Sidense documentation. After 16 attempts it is very unlikely +* that anything will change. +*/ +#define MAX_PROGRAM_RETRIES 16 + +#define OTP_MACRO_CMD_REG_OFFSET 0x00 +#define OTP_MACRO_STATUS_REG_OFFSET 0x04 +#define OTP_MACRO_CONFIG_REG_OFFSET 0x08 +#define OTP_MACRO_ADDR_REG_OFFSET 0x0C +#define OTP_MACRO_D_LO_REG_OFFSET 0x10 +#define OTP_MACRO_D_HI_REG_OFFSET 0x14 +#define OTP_MACRO_Q_LO_REG_OFFSET 0x20 +#define OTP_MACRO_Q_HI_REG_OFFSET 0x24 +#define OTP_MACRO_Q_MR_REG_OFFSET 0x28 +#define OTP_MACRO_Q_MRAB_REG_OFFSET 0x2C +#define OTP_MACRO_Q_SR_LO_REG_OFFSET 0x30 +#define OTP_MACRO_Q_SR_HI_REG_OFFSET 0x34 +#define OTP_MACRO_Q_RR_LO_REG_OFFSET 0x38 +#define OTP_MACRO_Q_RR_HI_REG_OFFSET 0x3C +#define OTP_MACRO_TIME_RD_REG_OFFSET 0x40 +#define OTP_MACRO_TIME_WR_REG_OFFSET 0x44 +#define OTP_MACRO_TIME_PGM_REG_OFFSET 0x48 +#define OTP_MACRO_TIME_PCH_REG_OFFSET 0x4C +#define OTP_MACRO_TIME_CMP_REG_OFFSET 0x50 +#define OTP_MACRO_TIME_RST_REG_OFFSET 0x54 +#define OTP_MACRO_TIME_PWR_REG_OFFSET 0x58 +#define OTP_MACRO_DIRECT_IO_REG_OFFSET 0x5C + +/* +* The OTP addresses of the special register. This is in the boot +* sector and we use words 0 and 2 of sector 0 in redundant format. +*/ +#define SR_ADDRESS_0 ((1 << 11) | 0x0) +#define SR_ADDRESS_2 ((1 << 11) | 0x2) + +enum otp_command { +OTP_COMMAND_IDLE , +OTP_COMMAND_WRITE , +OTP_COMMAND_PROGRAM , +OTP_COMMAND_READ , +OTP_COMMAND_WRITE_MR , +OTP_COMMAND_PRECHARGE , +OTP_COMMAND_COMPARE , +OTP_COMMAND_RESET , +OTP_COMMAND_RESET_M , +OTP_COMMAND_POWER_DOWN , +OTP_COMMAND_AUX_UPDATE_A , +OTP_COMMAND_AUX_UPDATE_B , +OTP_COMMAND_WRITE_PROGRAM , +OTP_COMMAND_WRITE_MRA , +OTP_COMMAND_WRITE_MRB , +OTP_COMMAND_RESET_MR , +}; + +/* The control and status registers follow the AXI OTP map. */ +#define OTP_CTRL_BASE 0x4000 + +/* +* The number of words in the OTP device. The device is 16K bytes and the word +* size is 64 bits. +*/ +#define OTP_NUM_WORDS (SZ_16K / OTP_WORD_SIZE) + +/* +* The OTP device representation. We can have a static structure as there is +* only ever one OTP device in a system. +* +* @iomem: the io memory for the device that should be accessed with the I/O +* accessors. +* @mem: the 16KB of OTP memory that can be accessed like normal memory. When +* we probe, we force the __iomem away so we can read it directly. +* @test_mode_sr0, test_mode_sr2 the values of the special register when we're +* in test mode. +*/ +struct pc3x3_otp { +struct otp_device *dev; +void __iomem *iomem; +void *mem; +struct clk *clk; +u64 test_mode_sr0, test_mode_sr2; +unsigned long registered_regions; +}; + +static inline void otp_write_reg (struct pc3x3_otp *otp,unsigned reg_num, +u32 value) +{ +writel (value, otp->iomem +OTP_CTRL_BASE + reg_num); +} + +static inline u32 otp_read_reg (struct pc3x3_otp *otp,unsigned reg_num) +{ +return readl (otp->iomem +OTP_CTRL_BASE + reg_num); +} + +static inline u32 otp_read_sr (struct pc3x3_otp *otp) +{ +if (test_mode) +return otp->test_mode_sr0 | otp->test_mode_sr2; + +return otp_read_reg (otp,OTP_MACRO_Q_SR_LO_REG_OFFSET ); +} + +/* +* Get the region format. The region format encoding and number of regions are +* encoded in the bottom 32 bis of the special register: +* +* 20: enable redundancy replacement. +* [2:0]: AXI address mask - determines the number of address bits to use for +* selecting the region to read from. +* [m:n]: the format for region X where n := (X * 2) + 4 and m := n + 1. +*/ +static enum otp_redundancy_fmt +__pc3x3_otp_region_get_fmt (struct pc3x3_otp *otp, +const struct otp_region *region) +{ +unsigned shift = (region->region_nr *2 ) +4 ; + +return (otp_read_sr (otp) >> shift) &0x3 ; +} + +static enum otp_redundancy_fmt +pc3x3_otp_region_get_fmt (struct otp_region *region) +{ +struct pc3x3_otp *otp =dev_get_drvdata (region->dev.parent); + +return __pc3x3_otp_region_get_fmt (otp, region); +} + +/* +* Find out how many regions the OTP is partitioned into. This can be 1, 2, 4 +* or 8. +*/ +static inline int otp_num_regions (struct pc3x3_otp *otp) +{ +#define SR_AXI_ADDRESS_MASK 0x7 +u32 addr_mask; +int nr_regions; + + addr_mask =otp_read_sr (otp) &SR_AXI_ADDRESS_MASK ; + +if (0 == addr_mask) + nr_regions =1 ; +else if (4 == addr_mask) + nr_regions =2 ; +else if (6 == addr_mask) + nr_regions =4 ;
drivers/otp/otp_pc3x3.c line 195
Quoted 10 lines
drivers/otp/otp_pc3x3.c new file +10
-0+else if (7 == addr_mask) + nr_regions =8 ; +else + nr_regions =0 ; + +if (WARN_ON (0 == nr_regions)) +return -EINVAL ; + +return nr_regions; +}
the "if" style is backwards from most of the kernel ... plus, this would probably look cleaner as a switch() statement
is this sort of a big func to be forcing inline isnt it ?
Quoted 14 lines
drivers/otp/otp_pc3x3.c new file +14
-0+static void otp_charge_pump_enable (struct pc3x3_otp *otp,int enable) +{ +#define OTP_MRA_CHARGE_PUMP_ENABLE_MASK (1 << 12) +#define OTP_MRA_CHARGE_PUMP_MONITOR_MASK (1 << 15) +#define OTP_MRA_READ_REFERENCE_LEVEL9_MASK (1 << 9) +#define OTP_MRA_READ_REFERENCE_LEVEL5_MASK (1 << 5) +#define OTP_STATUS_VPP_APPLIED (1 << 4) +u32 mra = enable ? + (OTP_MRA_CHARGE_PUMP_ENABLE_MASK | +OTP_MRA_CHARGE_PUMP_MONITOR_MASK | +OTP_MRA_READ_REFERENCE_LEVEL9_MASK | +OTP_MRA_READ_REFERENCE_LEVEL5_MASK ) :0 ; + +otp_write_MRA (otp, mra);
drivers/otp/otp_pc3x3.c line 288
Quoted 10 lines
drivers/otp/otp_pc3x3.c new file +10
-0+ +/* Now wait for VPP to reach the correct level. */ +if (enable && !test_mode) { +while (!(otp_read_reg (otp,OTP_MACRO_STATUS_REG_OFFSET ) & +OTP_STATUS_VPP_APPLIED )) +cpu_relax (); + } + +udelay (1 ); +}
is that udelay() really necessary ? could it be refined to a smaller ndelay() ?
drivers/otp/otp_pc3x3.c line 396
Quoted 1 line
drivers/otp/otp_pc3x3.c new file +1
-0+* any read-modify-write that is neccessary. For example if address 0 contains
"neccessary" -> "necessary"
Quoted 4 lines
drivers/otp/otp_pc3x3.c new file +4
-0+static int otp_raw_write_word (struct pc3x3_otp *otp, +unsigned addr,u64 val) +{ +/* The status of the last command. 1 == success. */
drivers/otp/otp_pc3x3.c line 419
Quoted 10 lines
drivers/otp/otp_pc3x3.c new file +10
-0+#define OTP_STATUS_LCS (1 << 1) + +#define OTP_MR_SELF_TIMING (1 << 2) +#define OTP_MR_PROGRAMMABLE_DELAY (1 << 5) +#define OTP_MR_PROGRAMMABLE_DELAY_CONTROL (1 << 8) + +#define OTP_MRB_VREF_ADJUST_0 (1 << 0) +#define OTP_MRB_VREF_ADJUST_1 (1 << 1) +#define OTP_MRB_VREF_ADJUST_3 (1 << 3) +#define OTP_MRB_READ_TIMER_DELAY_CONTROL (1 << 12)
this driver sure likes to inline defines ... usually these are kept all together at the top, or in a sep file like drivers/otp/otp_pc3x3.h
drivers/otp/otp_pc3x3.c line 449
Quoted 8 lines
drivers/otp/otp_pc3x3.c new file +8
-0+/* Enable the charge pump to begin programming. */ +otp_charge_pump_enable (otp,1 ); +otp_write_MRB (otp,OTP_MRB_VREF_ADJUST_3 | +OTP_MRB_READ_TIMER_DELAY_CONTROL ); +otp_write_MR (otp,OTP_MR_SELF_TIMING |OTP_MR_PROGRAMMABLE_DELAY | +OTP_MR_PROGRAMMABLE_DELAY_CONTROL ); +otp_raw_program_word (otp, addr, v); +udelay (1 );
i thought you had a helper func to do this ?
drivers/otp/otp_pc3x3.c line 640
Quoted 13 lines
drivers/otp/otp_pc3x3.c new file +13
-0+static int pc3x3_otp_region_read_word (struct otp_region *region, +unsigned long addr,u64 *word) +{ +switch (fmt) { +case OTP_REDUNDANCY_FMT_SINGLE_ENDED : +case OTP_REDUNDANCY_FMT_REDUNDANT : +case OTP_REDUNDANCY_FMT_DIFFERENTIAL : +case OTP_REDUNDANCY_FMT_DIFFERENTIAL_REDUNDANT : +default : + err = -EINVAL ; + } + + *word = result;
should that write happen even when there's an error ?
drivers/otp/otp_pc3x3.c line 829
Quoted 5 lines
drivers/otp/otp_pc3x3.c new file +5
-0+static ssize_t pc3x3_otp_region_get_size (struct otp_region *region) +{ +size_t region_sz; +return region_sz; +}
return type is ssize_t, but you're returning a size_t ...
Quoted 7 lines
drivers/otp/otp_pc3x3.c new file +7
-0+static int pc3x3_otp_get_nr_regions (struct otp_device *dev) +{ +struct pc3x3_otp *otp =dev_get_drvdata (&dev->dev); +unsigned long sr =otp_read_sr (otp); +u32 addr_mask = sr &SR_AXI_ADDRESS_MASK ; + +if (0 == addr_mask)
drivers/otp/otp_pc3x3.c line 924
Quoted 10 lines
drivers/otp/otp_pc3x3.c new file +10
-0+return 1 ; + else if (4 == addr_mask) + return 2; + else if (6 == addr_mask) + return 4; + else if (7 == addr_mask) + return 8; + +return -EINVAL ; +}
use a switch() statement instead ?
drivers/otp/otp_pc3x3.c line 934
Quoted 1 line
drivers/otp/otp_pc3x3.c new file +1
-0+static int __devinit otp_probe (struct platform_device *pdev)
namespace this ? so call it "pc3x3_otp_probe" ...
drivers/otp/otp_pc3x3.c line 954
Quoted 9 lines
drivers/otp/otp_pc3x3.c new file +9
-0+if (!devm_request_mem_region (&pdev->dev, mem->start, +resource_size (mem),"otp" )) { +dev_err (&pdev->dev,"unable to request i/o memory\n" ); +return -EBUSY ; + } + + pc3x3_dev =devm_kzalloc (&pdev->dev,sizeof (*pc3x3_dev),GFP_KERNEL ); +if (!pc3x3_dev) +return -ENOMEM ;
i'm not familiar with "devm_request_mem_region", but does it not need to be unrequested ?
also, should you be using the driver's name "pc3x3" rather than "otp" when requesting things ?
Quoted 4 lines
drivers/otp/otp_pc3x3.c new file +3
-0+if (test_mode) { +u64 *p =devm_kzalloc (&pdev->dev,SZ_16K +SZ_1K ,GFP_KERNEL ); + }else {
drivers/otp/otp_pc3x3.c line 1011
Quoted 10 lines
drivers/otp/otp_pc3x3.c new file +9
-0+ pc3x3_dev->iomem =devm_ioremap (&pdev->dev, mem->start, +resource_size (mem)); +out_unregister: +otp_device_unregister (otp); +out_clk_disable: +clk_disable (pc3x3_dev->clk); +clk_put (pc3x3_dev->clk); +out: +return err;
hmm, but you dont iounmap or free any of the memory from earlier when you error out ...
drivers/otp/otp_pc3x3.c line 1023
Quoted 10 lines
drivers/otp/otp_pc3x3.c new file +10
-0+static int __devexit otp_remove(struct platform_device *pdev) +{ +struct pc3x3_otp *otp =platform_get_drvdata (pdev); + +otp_device_unregister (otp->dev); +clk_disable (otp->clk); +clk_put (otp->clk); + +return 0 ; +}
seems like you forgot to release iomem here
Quoted 21 lines
drivers/otp/otp_pc3x3.c new file +21
-0+#ifdef CONFIG_PM +static int otp_suspend (struct device *dev) +{ +struct platform_device *pdev =to_platform_device (dev); +struct pc3x3_otp *otp =platform_get_drvdata (pdev); + +otp_write_reg (otp,OTP_MACRO_CMD_REG_OFFSET ,OTP_COMMAND_POWER_DOWN ); +clk_disable (otp->clk); + +return 0 ; +} + +static int otp_resume (struct device *dev) +{ +struct platform_device *pdev =to_platform_device (dev); +struct pc3x3_otp *otp =platform_get_drvdata (pdev); + +clk_enable (otp->clk); +otp_write_reg (otp,OTP_MACRO_CMD_REG_OFFSET ,OTP_COMMAND_IDLE ); + +return 0 ;
drivers/otp/otp_pc3x3.c line 1055
Quoted 10 lines
drivers/otp/otp_pc3x3.c new file +10
-0+} +#else /* CONFIG_PM */ +#define otp_suspend NULL +#define otp_resume NULL +#endif /* CONFIG_PM */ + +static const struct dev_pm_ops otp_pm_ops = { + .suspend = otp_suspend, + .resume = otp_resume, +};
usually people put the dev_pm_ops struct behind CONFIG_PM too and then do:
#ifdef CONFIG_PM
...
#define DEV_PM_OPS &otp_pm_ops
#else
#define DEV_PM_OPS NULL
#endif
drivers/otp/otp_pc3x3.c line 1068
Quoted 12 lines
drivers/otp/otp_pc3x3.c new file +11
-0+static struct platform_driver otp_driver = { + .remove =__devexit_p (otp_remove), + .driver = { + .name ="picoxcell-otp-pc3x3" , + .pm = &otp_pm_ops, + }, +}; +static int __init pc3x3_otp_init (void ) +{ +return platform_driver_probe (&otp_driver, otp_probe); +}
why call probe yourself ? why not platform_driver_register() ? -mike
Re: [RFC PATCHv2 2/4] drivers/otp: add support for Picoxcell PC3X3 OTP
Hi Mike,
Thanks for another great review!
Quoted 16 lines · Mike Frysinger
drivers/otp/Kconfig menuconfig OTP · line 16
Quoted 12 lines · Jamie Iles
drivers/otp/Kconfig +6
-0@@ -8,3 +8,14 @@ menuconfig OTP Say y here to support OTP memory found in some embedded devices. This memory can commonly be used to store boot code, cryptographic keys and other persistent data. + +if OTP + +config OTP_PC3X3 + tristate "Enable support for Picochip PC3X3 OTP" + depends on OTP && ARCH_PICOXCELLsince every driver is going to need "depend OTP", the "if OTP" is redundant then right ?
Yes, I'll drop the "if OTP".
Quoted 7 lines
drivers/otp/Kconfig menuconfig OTP · line 19
Quoted 4 lines
drivers/otp/Kconfig +3
-0+ help + Say y or m here to allow support for the OTP found in PC3X3 devices. + If you say m then the module will be called otp_pc3x3."y" -> "y"
"m" -> "M"
Ok.
Quoted 215 lines
Quoted 201 lines
drivers/otp/Kconfig +2
-0+ +endifdrivers/otp/Makefile +1
-0@@ -1 +1,2 @@ obj-$(CONFIG_OTP) += otp.o +obj-$(CONFIG_OTP_PC3X3) += otp_pc3x3.odrivers/otp/otp_pc3x3.c new file +185
-0@@ -0,0 +1,1079 @@ +/* +* Copyright 2010-2011 Picochip LTD, Jamie Iles +* http://www.picochip.com +* +* This program is free software; you can redistribute it and/or +* modify it under the terms of the GNU General Public License +* as published by the Free Software Foundation; either version +* 2 of the License, or (at your option) any later version. +* +* This driver implements a picoxcellotp backend for reading and writing the +* OTP memory in Picochip PC3X3 devices. This OTP can be used for executing +* secure boot code or for the secure storage of keys and any other user data. +*/ +#define pr_fmt (fmt) "pc3x3otp: " fmt + +#include <linux/bitops.h> +#include <linux/clk.h> +#include <linux/delay.h> +#include <linux/err.h> +#include <linux/io.h> +#include <linux/module.h> +#include <linux/otp.h> +#include <linux/platform_device.h> + +/* +* To test the user interface and most of the driver logic, we have a test +* mode whereby rather than writing to OTP we have a RAM buffer that simulates +* the OTP. This means that we can test everything apart from: +* +* - The OTP state machines and commands. +* - Failure to program bits. +*/ +static int test_mode; +module_param (test_mode, bool,S_IRUSR ); +MODULE_PARM_DESC (test_mode, +"Run in test mode (use a memory buffer rather than OTP" ); + +/* +* This is the maximum number of times to try and soak a failed bit. We get +* this from the Sidense documentation. After 16 attempts it is very unlikely +* that anything will change. +*/ +#define MAX_PROGRAM_RETRIES 16 + +#define OTP_MACRO_CMD_REG_OFFSET 0x00 +#define OTP_MACRO_STATUS_REG_OFFSET 0x04 +#define OTP_MACRO_CONFIG_REG_OFFSET 0x08 +#define OTP_MACRO_ADDR_REG_OFFSET 0x0C +#define OTP_MACRO_D_LO_REG_OFFSET 0x10 +#define OTP_MACRO_D_HI_REG_OFFSET 0x14 +#define OTP_MACRO_Q_LO_REG_OFFSET 0x20 +#define OTP_MACRO_Q_HI_REG_OFFSET 0x24 +#define OTP_MACRO_Q_MR_REG_OFFSET 0x28 +#define OTP_MACRO_Q_MRAB_REG_OFFSET 0x2C +#define OTP_MACRO_Q_SR_LO_REG_OFFSET 0x30 +#define OTP_MACRO_Q_SR_HI_REG_OFFSET 0x34 +#define OTP_MACRO_Q_RR_LO_REG_OFFSET 0x38 +#define OTP_MACRO_Q_RR_HI_REG_OFFSET 0x3C +#define OTP_MACRO_TIME_RD_REG_OFFSET 0x40 +#define OTP_MACRO_TIME_WR_REG_OFFSET 0x44 +#define OTP_MACRO_TIME_PGM_REG_OFFSET 0x48 +#define OTP_MACRO_TIME_PCH_REG_OFFSET 0x4C +#define OTP_MACRO_TIME_CMP_REG_OFFSET 0x50 +#define OTP_MACRO_TIME_RST_REG_OFFSET 0x54 +#define OTP_MACRO_TIME_PWR_REG_OFFSET 0x58 +#define OTP_MACRO_DIRECT_IO_REG_OFFSET 0x5C + +/* +* The OTP addresses of the special register. This is in the boot +* sector and we use words 0 and 2 of sector 0 in redundant format. +*/ +#define SR_ADDRESS_0 ((1 << 11) | 0x0) +#define SR_ADDRESS_2 ((1 << 11) | 0x2) + +enum otp_command { +OTP_COMMAND_IDLE , +OTP_COMMAND_WRITE , +OTP_COMMAND_PROGRAM , +OTP_COMMAND_READ , +OTP_COMMAND_WRITE_MR , +OTP_COMMAND_PRECHARGE , +OTP_COMMAND_COMPARE , +OTP_COMMAND_RESET , +OTP_COMMAND_RESET_M , +OTP_COMMAND_POWER_DOWN , +OTP_COMMAND_AUX_UPDATE_A , +OTP_COMMAND_AUX_UPDATE_B , +OTP_COMMAND_WRITE_PROGRAM , +OTP_COMMAND_WRITE_MRA , +OTP_COMMAND_WRITE_MRB , +OTP_COMMAND_RESET_MR , +}; + +/* The control and status registers follow the AXI OTP map. */ +#define OTP_CTRL_BASE 0x4000 + +/* +* The number of words in the OTP device. The device is 16K bytes and the word +* size is 64 bits. +*/ +#define OTP_NUM_WORDS (SZ_16K / OTP_WORD_SIZE) + +/* +* The OTP device representation. We can have a static structure as there is +* only ever one OTP device in a system. +* +* @iomem: the io memory for the device that should be accessed with the I/O +* accessors. +* @mem: the 16KB of OTP memory that can be accessed like normal memory. When +* we probe, we force the __iomem away so we can read it directly. +* @test_mode_sr0, test_mode_sr2 the values of the special register when we're +* in test mode. +*/ +struct pc3x3_otp { +struct otp_device *dev; +void __iomem *iomem; +void *mem; +struct clk *clk; +u64 test_mode_sr0, test_mode_sr2; +unsigned long registered_regions; +}; + +static inline void otp_write_reg (struct pc3x3_otp *otp,unsigned reg_num, +u32 value) +{ +writel (value, otp->iomem +OTP_CTRL_BASE + reg_num); +} + +static inline u32 otp_read_reg (struct pc3x3_otp *otp,unsigned reg_num) +{ +return readl (otp->iomem +OTP_CTRL_BASE + reg_num); +} + +static inline u32 otp_read_sr (struct pc3x3_otp *otp) +{ +if (test_mode) +return otp->test_mode_sr0 | otp->test_mode_sr2; + +return otp_read_reg (otp,OTP_MACRO_Q_SR_LO_REG_OFFSET ); +} + +/* +* Get the region format. The region format encoding and number of regions are +* encoded in the bottom 32 bis of the special register: +* +* 20: enable redundancy replacement. +* [2:0]: AXI address mask - determines the number of address bits to use for +* selecting the region to read from. +* [m:n]: the format for region X where n := (X * 2) + 4 and m := n + 1. +*/ +static enum otp_redundancy_fmt +__pc3x3_otp_region_get_fmt (struct pc3x3_otp *otp, +const struct otp_region *region) +{ +unsigned shift = (region->region_nr *2 ) +4 ; + +return (otp_read_sr (otp) >> shift) &0x3 ; +} + +static enum otp_redundancy_fmt +pc3x3_otp_region_get_fmt (struct otp_region *region) +{ +struct pc3x3_otp *otp =dev_get_drvdata (region->dev.parent); + +return __pc3x3_otp_region_get_fmt (otp, region); +} + +/* +* Find out how many regions the OTP is partitioned into. This can be 1, 2, 4 +* or 8. +*/ +static inline int otp_num_regions (struct pc3x3_otp *otp) +{ +#define SR_AXI_ADDRESS_MASK 0x7 +u32 addr_mask; +int nr_regions; + + addr_mask =otp_read_sr (otp) &SR_AXI_ADDRESS_MASK ; + +if (0 == addr_mask) + nr_regions =1 ; +else if (4 == addr_mask) + nr_regions =2 ; +else if (6 == addr_mask) + nr_regions =4 ;drivers/otp/otp_pc3x3.c line 195
Quoted 10 lines
drivers/otp/otp_pc3x3.c new file +10
-0+else if (7 == addr_mask) + nr_regions =8 ; +else + nr_regions =0 ; + +if (WARN_ON (0 == nr_regions)) +return -EINVAL ; + +return nr_regions; +}the "if" style is backwards from most of the kernel ... plus, this would probably look cleaner as a
switch()statement
Quoted 1 line
is this sort of a big func to be forcing inline isnt it ?
Fair comments, I'll rework that, and I'm not sure why I made it inline in the first place...
Quoted 19 lines
Quoted 19 lines
drivers/otp/otp_pc3x3.c new file +18
-0+static void otp_charge_pump_enable (struct pc3x3_otp *otp,int enable) +{ +#define OTP_MRA_CHARGE_PUMP_ENABLE_MASK (1 << 12) +#define OTP_MRA_CHARGE_PUMP_MONITOR_MASK (1 << 15) +#define OTP_MRA_READ_REFERENCE_LEVEL9_MASK (1 << 9) +#define OTP_MRA_READ_REFERENCE_LEVEL5_MASK (1 << 5) +#define OTP_STATUS_VPP_APPLIED (1 << 4) +u32 mra = enable ? + (OTP_MRA_CHARGE_PUMP_ENABLE_MASK | +OTP_MRA_CHARGE_PUMP_MONITOR_MASK | +OTP_MRA_READ_REFERENCE_LEVEL9_MASK | +OTP_MRA_READ_REFERENCE_LEVEL5_MASK ) :0 ; + +otp_write_MRA (otp, mra); + +/* Now wait for VPP to reach the correct level. */ +if (enable && !test_mode) { + while (!(otp_read_reg(otp, OTP_MACRO_STATUS_REG_OFFSET) &
Quoted 8 lines
drivers/otp/otp_pc3x3.c line 288
Quoted 6 lines
drivers/otp/otp_pc3x3.c new file +6
-0+ OTP_STATUS_VPP_APPLIED)) +cpu_relax (); + } + +udelay (1 ); +}is that
udelay()really necessary ? could it be refined to a smallerndelay()?
It's what is specifed in the IP vendors datasheets so perhaps it could be less but I'd like to err on the side of caution.
Quoted 4 lines
drivers/otp/otp_pc3x3.c line 396
Quoted 2 lines
drivers/otp/otp_pc3x3.c new file +1
-0+* any read-modify-write that is neccessary. For example if address 0 contains "neccessary" -> "necessary"
OK.
Quoted 10 lines
Quoted 10 lines
drivers/otp/otp_pc3x3.c new file +9
-0+static int otp_raw_write_word (struct pc3x3_otp *otp, +unsigned addr,u64 val) +{ +/* The status of the last command. 1 == success. */ +#define OTP_STATUS_LCS (1 << 1) + +#define OTP_MR_SELF_TIMING (1 << 2) +#define OTP_MR_PROGRAMMABLE_DELAY (1 << 5) +#define OTP_MR_PROGRAMMABLE_DELAY_CONTROL (1 << 8)
Quoted 8 lines
drivers/otp/otp_pc3x3.c line 419
Quoted 5 lines
drivers/otp/otp_pc3x3.c new file +5
-0+ +#define OTP_MRB_VREF_ADJUST_0 (1 << 0) +#define OTP_MRB_VREF_ADJUST_1 (1 << 1) +#define OTP_MRB_VREF_ADJUST_3 (1 << 3) +#define OTP_MRB_READ_TIMER_DELAY_CONTROL (1 << 12)this driver sure likes to inline defines ... usually these are kept all together at the top, or in a sep file like
drivers/otp/otp_pc3x3.h
Something I picked up from another driver but I can appreciate why it isn't liked. I'll move them all to the top of the driver.
Quoted 11 lines
drivers/otp/otp_pc3x3.c line 449
Quoted 9 lines
drivers/otp/otp_pc3x3.c new file +8
-0+/* Enable the charge pump to begin programming. */ +otp_charge_pump_enable (otp,1 ); +otp_write_MRB (otp,OTP_MRB_VREF_ADJUST_3 | +OTP_MRB_READ_TIMER_DELAY_CONTROL ); +otp_write_MR (otp,OTP_MR_SELF_TIMING |OTP_MR_PROGRAMMABLE_DELAY | +OTP_MR_PROGRAMMABLE_DELAY_CONTROL ); +otp_raw_program_word (otp, addr, v); +udelay (1 );i thought you had a helper func to do this ?
I don't think so, there are a few similar looking things with different timing parameters so there may be potential for a helper...
Quoted 16 lines
Quoted 4 lines
drivers/otp/otp_pc3x3.c new file +3
-0+static int pc3x3_otp_region_read_word (struct otp_region *region, +unsigned long addr,u64 *word) +{drivers/otp/otp_pc3x3.c line 640
Quoted 10 lines
drivers/otp/otp_pc3x3.c new file +10
-0+switch (fmt) { +case OTP_REDUNDANCY_FMT_SINGLE_ENDED : +case OTP_REDUNDANCY_FMT_REDUNDANT : +case OTP_REDUNDANCY_FMT_DIFFERENTIAL : +case OTP_REDUNDANCY_FMT_DIFFERENTIAL_REDUNDANT : +default : + err = -EINVAL ; + } + + *word = result;should that write happen even when there's an error ?
I figured that it wouldn't matter as the caller shouldn't use it when it gets an error returned but perhaps it's better to be safe.
Quoted 8 lines
drivers/otp/otp_pc3x3.c line 829
Quoted 6 lines
drivers/otp/otp_pc3x3.c new file +5
-0+static ssize_t pc3x3_otp_region_get_size (struct otp_region *region) +{ +size_t region_sz; +return region_sz; +}return type is
ssize_t, but you're returning asize_t...
Doh! Thanks.
Quoted 12 lines
Quoted 12 lines
drivers/otp/otp_pc3x3.c new file +11
-0+static int pc3x3_otp_get_nr_regions (struct otp_device *dev) +{ +struct pc3x3_otp *otp =dev_get_drvdata (&dev->dev); +unsigned long sr =otp_read_sr (otp); +u32 addr_mask = sr &SR_AXI_ADDRESS_MASK ; + +if (0 == addr_mask) +return 1 ; +else if (4 == addr_mask) +return 2 ; +else if (6 == addr_mask)
Quoted 8 lines
drivers/otp/otp_pc3x3.c line 924
Quoted 6 lines
drivers/otp/otp_pc3x3.c new file +6
-0+return 4 ; + else if (7 == addr_mask) + return 8; + +return -EINVAL ; +}use a
switch()statement instead ?
OK.
Quoted 3 lines
drivers/otp/otp_pc3x3.c line 934
Quoted 1 line
drivers/otp/otp_pc3x3.c new file +1
-0+static int __devinit otp_probe (struct platform_device *pdev)namespace this ? so call it "
pc3x3_otp_probe" ...
Yes, I'll check for others too.
Quoted 11 lines
drivers/otp/otp_pc3x3.c line 954
Quoted 10 lines
drivers/otp/otp_pc3x3.c new file +9
-0+if (!devm_request_mem_region (&pdev->dev, mem->start, +resource_size (mem),"otp" )) { +dev_err (&pdev->dev,"unable to request i/o memory\n" ); +return -EBUSY ; + } + + pc3x3_dev =devm_kzalloc (&pdev->dev,sizeof (*pc3x3_dev),GFP_KERNEL ); +if (!pc3x3_dev) +return -ENOMEM ;
Quoted 5 lines
i'm not familiar with "
devm_request_mem_region", but does it not need to be unrequested ?also, should you be using the driver's name "pc3x3" rather than "otp" when requesting things ?
No, devm_* automatically does the cleanup when either probe() fails or remove() is called, and yes the naming is wrong here, I'll fix that.
Quoted 10 lines
Quoted 10 lines
drivers/otp/otp_pc3x3.c new file +7
-0+if (test_mode) { +u64 *p =devm_kzalloc (&pdev->dev,SZ_16K +SZ_1K ,GFP_KERNEL ); + }else { + pc3x3_dev->iomem =devm_ioremap (&pdev->dev, mem->start, +resource_size (mem)); +out_unregister: +otp_device_unregister (otp);
Quoted 8 lines
drivers/otp/otp_pc3x3.c line 1011
Quoted 5 lines
drivers/otp/otp_pc3x3.c new file +5
-0+out_clk_disable: +clk_disable (pc3x3_dev->clk); +clk_put (pc3x3_dev->clk); +out: +return err;hmm, but you dont iounmap or free any of the memory from earlier when you error out ...
No, that's what the devm_* stuff does for us.
Quoted 13 lines
drivers/otp/otp_pc3x3.c line 1023
Quoted 11 lines
drivers/otp/otp_pc3x3.c new file +10
-0+static int __devexit otp_remove(struct platform_device *pdev) +{ +struct pc3x3_otp *otp =platform_get_drvdata (pdev); + +otp_device_unregister (otp->dev); +clk_disable (otp->clk); +clk_put (otp->clk); + +return 0 ; +}seems like you forgot to release iomem here
devm_ioremap() and devm_request_region() should cover that for us.
Quoted 33 lines
Quoted 22 lines
drivers/otp/otp_pc3x3.c new file +21
-0+#ifdef CONFIG_PM +static int otp_suspend (struct device *dev) +{ +struct platform_device *pdev =to_platform_device (dev); +struct pc3x3_otp *otp =platform_get_drvdata (pdev); + +otp_write_reg (otp,OTP_MACRO_CMD_REG_OFFSET ,OTP_COMMAND_POWER_DOWN ); +clk_disable (otp->clk); + +return 0 ; +} + +static int otp_resume (struct device *dev) +{ +struct platform_device *pdev =to_platform_device (dev); +struct pc3x3_otp *otp =platform_get_drvdata (pdev); + +clk_enable (otp->clk); +otp_write_reg (otp,OTP_MACRO_CMD_REG_OFFSET ,OTP_COMMAND_IDLE ); + +return 0 ;drivers/otp/otp_pc3x3.c line 1055
Quoted 10 lines
drivers/otp/otp_pc3x3.c new file +10
-0+} +#else /* CONFIG_PM */ +#define otp_suspend NULL +#define otp_resume NULL +#endif /* CONFIG_PM */ + +static const struct dev_pm_ops otp_pm_ops = { + .suspend = otp_suspend, + .resume = otp_resume, +};
Quoted 7 lines
usually people put the
dev_pm_opsstruct behindCONFIG_PMtoo and then do:
#ifdefCONFIG_PM
...
#defineDEV_PM_OPS&otp_pm_ops
#else
#defineDEV_PM_OPSNULL
#endif
Yes, that's much nicer. Thanks.
Quoted 9 lines
Quoted 9 lines
drivers/otp/otp_pc3x3.c new file +7
-0+static struct platform_driver otp_driver = { + .remove =__devexit_p (otp_remove), + .driver = { + .name ="picoxcell-otp-pc3x3" , + .pm = &otp_pm_ops, + }, +};
Quoted 6 lines
drivers/otp/otp_pc3x3.c line 1068
Quoted 4 lines
drivers/otp/otp_pc3x3.c new file +4
-0+static int __init pc3x3_otp_init (void ) +{ +return platform_driver_probe (&otp_driver, otp_probe); +}why call probe yourself ? why not
platform_driver_register()?
I made this comment in another driver myself and another reviewer pointed out that if the device isn't some kind of hotplug device then it probably isn't needed to let the bus do the matching and probing but I'm happy to do what you've suggested, it feels a bit more natural anyway!
Jamie
Re: [RFC PATCHv2 2/4] drivers/otp: add support for Picoxcell PC3X3 OTP
Quoted 29 lines · Jamie Iles
Quoted 19 lines · Mike Frysinger
Quoted 18 lines · Jamie Iles
drivers/otp/otp_pc3x3.c new file +18
-0+static void otp_charge_pump_enable (struct pc3x3_otp *otp,int enable) +{ +#define OTP_MRA_CHARGE_PUMP_ENABLE_MASK (1 << 12) +#define OTP_MRA_CHARGE_PUMP_MONITOR_MASK (1 << 15) +#define OTP_MRA_READ_REFERENCE_LEVEL9_MASK (1 << 9) +#define OTP_MRA_READ_REFERENCE_LEVEL5_MASK (1 << 5) +#define OTP_STATUS_VPP_APPLIED (1 << 4) + u32 mra = enable ? + (OTP_MRA_CHARGE_PUMP_ENABLE_MASK | + OTP_MRA_CHARGE_PUMP_MONITOR_MASK | + OTP_MRA_READ_REFERENCE_LEVEL9_MASK | + OTP_MRA_READ_REFERENCE_LEVEL5_MASK) : 0; + + otp_write_MRA(otp, mra); + + /* Now wait for VPP to reach the correct level. */ + if (enable && !test_mode) { + while (!(otp_read_reg(otp, OTP_MACRO_STATUS_REG_OFFSET) &Quoted 8 lines
drivers/otp/otp_pc3x3.c line 288
Quoted 6 lines
+ OTP_STATUS_VPP_APPLIED))
+cpu_relax();
+ }drivers/otp/otp_pc3x3.c new file +3
-0+ + udelay(1); +}is that
udelay()really necessary ? could it be refined to a smallerndelay()?
Quoted 2 lines
It's what is specifed in the IP vendors datasheets so perhaps it could be less but I'd like to err on the side of caution.
if that's what the datasheet says, then np, it's fine. presumably you're not charging it for fun which means you cant background the wait.
Quoted 18 lines
Quoted 9 lines
Quoted 9 lines
+ if (
test_mode) {
+ u64 *p =devm_kzalloc(&pdev->dev, SZ_16K + SZ_1K, GFP_KERNEL);
...
+ } else {
+pc3x3_dev->iomem= devm_ioremap(&pdev->dev,mem->start,
+resource_size(mem));
...
+out_unregister:
+otp_device_unregister(otp);Quoted 8 lines
drivers/otp/otp_pc3x3.c line 1011
Quoted 5 lines
drivers/otp/otp_pc3x3.c new file +5
-0+out_clk_disable: + clk_disable(pc3x3_dev->clk); + clk_put(pc3x3_dev->clk); +out: + return err;hmm, but you dont iounmap or free any of the memory from earlier when you error out ...
Quoted 1 line
No, that's what the devm_* stuff does for us.
hmm, wasnt aware of this devm* stuff. sounds like fun and i could use it elsewhere.
Quoted 15 lines
Quoted 8 lines
Quoted 8 lines
drivers/otp/otp_pc3x3.c new file +7
-0+static struct platform_driver otp_driver = { + .remove = __devexit_p(otp_remove), + .driver = { + .name = "picoxcell-otp-pc3x3", + .pm = &otp_pm_ops, + }, +};Quoted 6 lines
drivers/otp/otp_pc3x3.c line 1068
Quoted 4 lines
drivers/otp/otp_pc3x3.c new file +4
-0+static int __init pc3x3_otp_init (void ) +{ + return platform_driver_probe(&otp_driver, otp_probe); +}why call probe yourself ? why not
platform_driver_register()?
Quoted 4 lines
I made this comment in another driver myself and another reviewer pointed out that if the device isn't some kind of hotplug device then it probably isn't needed to let the bus do the matching and probing but I'm happy to do what you've suggested, it feels a bit more natural anyway!
that's why the pseudo "platform" bus exists in the first place. i could somewhat understand if the probe function actually did probing to see if the device in question existed before going further, but this probe function simply assumes that if it gets called, the device exists. as such, it makes a lot more sense to force people to "opt in" via their platform resources. otherwise, the whole "build one kernel but deploy to multiple boards" pretty much goes out the window. -mike
RFC PATCH v2 3/4 drivers/otp: allow an ioctl to be specified
In reply to Jamie IlesSome drivers may need an ioctl method to provide device specific control such as blackfin for providing locking. Regions can specify an ioctl to be used but this field method is optional.
--- drivers/otp/otp.c | 30 ++++++++++++++++++++++++------ include/linux/otp.h | 3 +++ 2 files changed, 27 insertions(+), 6 deletions(-)
drivers/otp/otp.c +24 -6
@@ -30,14 +30,16 @@ static ssize_t otp_write(struct file *filp, const char __user *buf,static ssize_t otp_read (struct file *filp,char __user *buf,size_t len,loff_t *offs);static loff_t otp_llseek (struct file *filp,loff_t offs,int origin); +static long otp_ioctl (struct file *filp,unsigned cmd,unsigned long arg);static const struct file_operations otp_fops = { - .owner =THIS_MODULE , - .open = otp_open, - .release = otp_release, - .write = otp_write, - .read = otp_read, - .llseek = otp_llseek, + .owner =THIS_MODULE , + .open = otp_open, + .release = otp_release, + .write = otp_write, + .read = otp_read, + .llseek = otp_llseek, + .unlocked_ioctl = otp_ioctl, };static DEFINE_SEMAPHORE (otp_sem);
@@ -691,6 +693,22 @@ out:return ret ?: written; } +static long otp_ioctl (struct file *filp,unsigned cmd,unsigned long arg) +{ +struct otp_region *region = filp->private_data; +long ret = -ENOTTY ; + +if (down_interruptible (&otp_sem)) +return -ERESTARTSYS ; + +if (region->ops->ioctl) + ret = region->ops->ioctl (region, cmd, arg); + +up (&otp_sem); + +return ret; +} +/* * Read an OTP region. This switches the OTP into the appropriate redundancy * format so we can simply read from the beginning of the region and copy it
include/linux/otp.h +3 -0
@@ -100,6 +100,7 @@ struct otp_device {* @get_size: Get the effective storage size of the region. * Depending on the number of regions in the device and * the redundancy format of the region, this may vary. +* @ioctl: Optional region ioctl. */ struct otp_region_ops { int (*set_fmt)(struct otp_region *region,
@@ -112,6 +113,8 @@ struct otp_region_ops {unsigned long word_addr, u64 *value);ssize_t (*get_size)(struct otp_region *region); +long (*ioctl)(struct otp_region *dev, +unsigned cmd,unsigned long arg); };/**
Signature · 1 line
1.7.4
Re: [RFC PATCHv2 3/4] drivers/otp: allow an ioctl to be specified
Quoted 3 lines · Jamie Iles
Some drivers may need an ioctl method to provide device specific control such as blackfin for providing locking. Regions can specify an ioctl to be used but this field method is optional.
shouldnt this just be squashed into patch 1 since you havent merged any of this yet ?
Quoted 5 lines
drivers/otp/otp.c +5
-0+static long otp_ioctl (struct file *filp,unsigned cmd,unsigned long arg) +{ + struct otp_region *region = filp->private_data; + long ret = -ENOTTY; +
drivers/otp/otp.c out: · line 710
Quoted 10 lines
drivers/otp/otp.c +10
-0+ if (down_interruptible(&otp_sem)) + return -ERESTARTSYS; + + if (region->ops->ioctl) + ret = region->ops->ioctl(region, cmd, arg); + + up(&otp_sem); + + return ret; +}
the existence check can be pulled out of the locking
long ret;
if (!region->ops->ioctl)
return -ENOTTY;
if (down_interruptible(&otp_sem))
return -ERESTARTSYS;
ret = region->ops->ioctl(region, cmd, arg);
up(&otp_sem);
return ret;
-mike
Re: [RFC PATCHv2 3/4] drivers/otp: allow an ioctl to be specified
Hi Mike,
Quoted 7 lines · Mike Frysinger
Quoted 3 lines · Jamie Iles
Some drivers may need an ioctl method to provide device specific control such as blackfin for providing locking. Regions can specify an ioctl to be used but this field method is optional.
shouldnt this just be squashed into patch 1 since you havent merged any of this yet ?
Probably yes, I was just trying to keep the patches smaller to review, but in this case it probably isn't a sensible split.
Quoted 26 lines
Quoted 6 lines
drivers/otp/otp.c +5
-0+static long otp_ioctl (struct file *filp,unsigned cmd,unsigned long arg) +{ + struct otp_region *region = filp->private_data; + long ret = -ENOTTY; +drivers/otp/otp.c out: · line 710
Quoted 10 lines
drivers/otp/otp.c +10
-0+ if (down_interruptible(&otp_sem)) + return -ERESTARTSYS; + + if (region->ops->ioctl) + ret = region->ops->ioctl(region, cmd, arg); + + up(&otp_sem); + + return ret; +}the existence check can be pulled out of the locking
long ret;
if (!region->ops->ioctl)
return -ENOTTY;if (
down_interruptible(&otp_sem))
Quoted 8 lines
return -ERESTARTSYS;ret =
region->ops->ioctl(region, cmd, arg);
up(&otp_sem);return ret;
Ok, will do.
Jamie
RFC PATCH v2 4/4 drivers/otp: convert bfin otp to generic OTP
In reply to Jamie IlesConvert the blackfin OTP driver to the generic OTP layer.
---
drivers/char/Kconfig | 28 ------
drivers/char/Makefile | 1 -
drivers/otp/Kconfig | 28 ++++++
drivers/otp/Makefile | 1 +
drivers/{char => otp}/bfin-otp.c | 174 ++++++++++++++++---------------------
5 files changed, 104 insertions(+), 128 deletions(-)
rename drivers/{char => otp}/bfin-otp.c (54%)drivers/char/Kconfig +0 -28
@@ -81,34 +81,6 @@ config BRIQ_PANEL It's safe to say N here. -config BFIN_OTP - tristate "Blackfin On-Chip OTP Memory Support" - depends on BLACKFIN && (BF51x || BF52x || BF54x) - default y - help - If you say Y here, you will get support for a character device - interface into the One Time Programmable memory pages that are - stored on the Blackfin processor. This will not get you access - to the secure memory pages however. You will need to write your - own secure code and reader for that. - - To compile this driver as a module, choose M here: the module - will be called bfin-otp. - - If unsure, it is safe to say Y. - -config BFIN_OTP_WRITE_ENABLE - bool "Enable writing support of OTP pages" - depends on BFIN_OTP - default n - help - If you say Y here, you will enable support for writing of the - OTP pages. This is dangerous by nature as you can only program - the pages once, so only enable this option when you actually - need it so as to not inadvertently clobber data. - - If unsure, say N. - config PRINTER tristate "Parallel printer support" depends on PARPORT
drivers/char/Makefile +0 -1
@@ -16,7 +16,6 @@ obj-$(CONFIG_VIOTAPE) += viotape.o obj-$(CONFIG_IBM_BSR) += bsr.o obj-$(CONFIG_SGI_MBCS) += mbcs.o obj-$(CONFIG_BRIQ_PANEL) += briq_panel.o -obj-$(CONFIG_BFIN_OTP) += bfin-otp.o obj-$(CONFIG_PRINTER) += lp.o
drivers/otp/Kconfig +28 -0
@@ -18,4 +18,32 @@ config OTP_PC3X3 Say y or m here to allow support for the OTP found in PC3X3 devices. If you say m then the module will be called otp_pc3x3. +config BFIN_OTP + tristate "Blackfin On-Chip OTP Memory Support" + depends on BLACKFIN && (BF51x || BF52x || BF54x) && OTP + default y + help + If you say Y here, you will get support for a character device + interface into the One Time Programmable memory pages that are + stored on the Blackfin processor. This will not get you access + to the secure memory pages however. You will need to write your + own secure code and reader for that. + + To compile this driver as a module, choose M here: the module + will be called bfin-otp. + + If unsure, it is safe to say Y. + +config BFIN_OTP_WRITE_ENABLE + bool "Enable writing support of OTP pages" + depends on BFIN_OTP + default n + help + If you say Y here, you will enable support for writing of the + OTP pages. This is dangerous by nature as you can only program + the pages once, so only enable this option when you actually + need it so as to not inadvertently clobber data. + + If unsure, say N. + endif
drivers/otp/Makefile +1 -0
@@ -1,2 +1,3 @@ obj-$(CONFIG_OTP) += otp.o obj-$(CONFIG_OTP_PC3X3) += otp_pc3x3.o +obj-$(CONFIG_BFIN_OTP) += bfin-otp.o
drivers/otp/bfin-otp.c renamed from drivers/char/bfin-otp.c +75 -99
261 lines not on this page · raw message
Signature · 1 line
1.7.4
Re: [RFC PATCHv2 4/4] drivers/otp: convert bfin otp to generic OTP
Quoted 9 lines · Jamie Iles
+config
BFIN_OTP_WRITE_ENABLE
+ bool "Enable writing support of OTP pages"
+ depends onBFIN_OTP
+ default n
+ help
+ If you say Y here, you will enable support for writing of the
+ OTP pages. This is dangerous by nature as you can only program
+ the pages once, so only enable this option when you actually
+ need it so as to not inadvertently clobber data.
if the generic layer is providing write enable access now, is this still necessary ? we could have a common OTP Kconfig i think for "enable OTP write support".
-mike
Re: [RFC PATCHv2 4/4] drivers/otp: convert bfin otp to generic OTP
Quoted 11 lines · Mike Frysinger
Quoted 9 lines · Jamie Iles
+config
BFIN_OTP_WRITE_ENABLE
+ bool "Enable writing support of OTP pages"
+ depends onBFIN_OTP
+ default n
+ help
+ If you say Y here, you will enable support for writing of the
+ OTP pages. This is dangerous by nature as you can only program
+ the pages once, so only enable this option when you actually
+ need it so as to not inadvertently clobber data.
Quoted 3 lines
if the generic layer is providing write enable access now, is this still necessary ? we could have a common OTP
Kconfigi think for "enable OTP write support".
Sounds like a good idea. I'll integrate this into the next revision.
Jamie
Quoted 4 lines · Jamie Iles
Following some feedback from Greg, I've updated this series to be more of a generic OTP layer. Everything is now registered under the "otp" bus and I've also converted the blackfin OTP driver to use this framework (which is the only current OTP driver I could find).
really, i'm the only one who wrote a driver ? that's boring.
i guess this isnt trying to handle OTP stuff that exists in the MTD layer already ?
Quoted 2 lines
Mike, I wasn't 100% sure how big the blackfin OTP is but I found a datasheet talking about 64KB so I've assumed that for now.
the datasheets say 64K *bits* :). i think all our datasheets tend to use bits rather than bytes because they're stupid and bigger numbers always means better parts !
but yes, on-chip OTP on all relevant Blackfin parts today are 8KiB in size. 0x200 128bit pages is how things are organized. -mike
Hi Mike,
Quoted 7 lines · Mike Frysinger
Quoted 4 lines · Jamie Iles
Following some feedback from Greg, I've updated this series to be more of a generic OTP layer. Everything is now registered under the "otp" bus and I've also converted the blackfin OTP driver to use this framework (which is the only current OTP driver I could find).
really, i'm the only one who wrote a driver ? that's boring.
Yes, and I'm quite surprised at that. Perhaps I've missed some.
Quoted 2 lines
i guess this isnt trying to handle OTP stuff that exists in the MTD layer already ?
No, I believe that the OTP stuff in the MTD layer is where some flash chips have a small section that can be write protected so it's more like a permanent write disable to that sector whereas the thing I'm looking at is on a per-bit basis.
I did have a look at if there was some kind of way to fit this stuff into the MTD layer but it felt like it was really shoehorning it in.
Quoted 6 lines
Quoted 2 lines
Mike, I wasn't 100% sure how big the blackfin OTP is but I found a datasheet talking about 64KB so I've assumed that for now.
the datasheets say 64K *bits* :). i think all our datasheets tend to use bits rather than bytes because they're stupid and bigger numbers always means better parts !
That sounds very familiar!
Jamie
Quoted 10 lines · Jamie Iles
Quoted 7 lines · Mike Frysinger
Quoted 2 lines · Jamie Iles
Mike, I wasn't 100% sure how big the blackfin OTP is but I found a datasheet talking about 64KB so I've assumed that for now.
the datasheets say 64K *bits* :). i think all our datasheets tend to use bits rather than bytes because they're stupid and bigger numbers always means better parts !
That sounds very familiar!
just an overview note here ... the Blackfin OTP is actually IP we bought from someone (not sure if i can say more, although i dont know the details myself ... i'd have to go ask around), so much of the feedback i give about the structure of it most likely (hopefully) be applicable to other people who have purchased this IP. if/when anyone who is also using this IP steps forward, we could even see about generalizing the bfin-otp driver.
-mike
Quoted 7 lines · Mike Frysinger
just an overview note here ... the Blackfin OTP is actually IP we bought from someone (not sure if i can say more, although i dont know the details myself ... i'd have to go ask around), so much of the feedback i give about the structure of it most likely (hopefully) be applicable to other people who have purchased this IP. if/when anyone who is also using this IP steps forward, we could even see about generalizing the bfin-otp driver.
Yes, we have a similar thing here - a block of OTP from an IP vendor and a wrapper to provide an AXI interface. As far as I know there are only a few OTP vendors so there may well be plenty of common code in different OTP implementations.
Jamie
Quoted 9 lines · Jamie Iles
Quoted 7 lines · Mike Frysinger
just an overview note here ... the Blackfin OTP is actually IP we bought from someone (not sure if i can say more, although i dont know the details myself ... i'd have to go ask around), so much of the feedback i give about the structure of it most likely (hopefully) be applicable to other people who have purchased this IP. if/when anyone who is also using this IP steps forward, we could even see about generalizing the bfin-otp driver.
Quoted 4 lines
Yes, we have a similar thing here - a block of OTP from an IP vendor and a wrapper to provide an AXI interface. As far as I know there are only a few OTP vendors so there may well be plenty of common code in different OTP implementations.
do you guys wrap the interface ? the Blackfin processors have an on-chip ROM which provides higher level functions like read/write/lock/etc... the Linux driver uses those rather than programming the memory mapped OTP interface itself since this interface is not publicly documented.
-mike
Quoted 15 lines · Mike Frysinger
Quoted 9 lines · Jamie Iles
Quoted 7 lines · Mike Frysinger
just an overview note here ... the Blackfin OTP is actually IP we bought from someone (not sure if i can say more, although i dont know the details myself ... i'd have to go ask around), so much of the feedback i give about the structure of it most likely (hopefully) be applicable to other people who have purchased this IP. if/when anyone who is also using this IP steps forward, we could even see about generalizing the bfin-otp driver.
Quoted 4 lines
Yes, we have a similar thing here - a block of OTP from an IP vendor and a wrapper to provide an AXI interface. As far as I know there are only a few OTP vendors so there may well be plenty of common code in different OTP implementations.
Quoted 5 lines
do you guys wrap the interface ? the Blackfin processors have an on-chip ROM which provides higher level functions like read/write/lock/etc... the Linux driver uses those rather than programming the memory mapped OTP interface itself since this interface is not publicly documented.
I'm not 100% on the exact details but I understand that the macro provides some non-AXI registers and signals that we wrap up into some AXI registers. The wrapper doesn't handle all of the nasties of the macro but it does handle some of the redundancy and region splitting.
Jamie
Quoted 4 lines · Jamie Iles
Yes, we have a similar thing here - a block of OTP from an IP vendor and a wrapper to provide an AXI interface. As far as I know there are only a few OTP vendors so there may well be plenty of common code in different OTP implementations.
Seems possible, though I'm not sure how much of the register interface the OTP vendors provide so how much low level compatibility there might be.