2  * Blackfin On-Chip OTP Memory Interface
 
   5  * Copyright 2007-2008 Analog Devices Inc.
 
   7  * Enter bugs at http://blackfin.uclinux.org/
 
   9  * Licensed under the GPL-2 or later.
 
  12 #include <linux/device.h>
 
  13 #include <linux/errno.h>
 
  15 #include <linux/init.h>
 
  16 #include <linux/miscdevice.h>
 
  17 #include <linux/module.h>
 
  18 #include <linux/mutex.h>
 
  19 #include <linux/types.h>
 
  21 #include <asm/blackfin.h>
 
  22 #include <asm/uaccess.h>
 
  24 #define stamp(fmt, args...) pr_debug("%s:%i: " fmt "\n", __func__, __LINE__, ## args)
 
  25 #define stampit() stamp("here i am")
 
  26 #define pr_init(fmt, args...) ({ static const __initconst char __fmt[] = fmt; printk(__fmt, ## args); })
 
  28 #define DRIVER_NAME "bfin-otp"
 
  29 #define PFX DRIVER_NAME ": "
 
  31 static DEFINE_MUTEX(bfin_otp_lock);
 
  33 /* OTP Boot ROM functions */
 
  34 #define _BOOTROM_OTP_COMMAND           0xEF000018
 
  35 #define _BOOTROM_OTP_READ              0xEF00001A
 
  36 #define _BOOTROM_OTP_WRITE             0xEF00001C
 
  38 static u32 (* const otp_command)(u32 command, u32 value) = (void *)_BOOTROM_OTP_COMMAND;
 
  39 static u32 (* const otp_read)(u32 page, u32 flags, u64 *page_content) = (void *)_BOOTROM_OTP_READ;
 
  40 static u32 (* const otp_write)(u32 page, u32 flags, u64 *page_content) = (void *)_BOOTROM_OTP_WRITE;
 
  42 /* otp_command(): defines for "command" */
 
  43 #define OTP_INIT             0x00000001
 
  44 #define OTP_CLOSE            0x00000002
 
  46 /* otp_{read,write}(): defines for "flags" */
 
  47 #define OTP_LOWER_HALF       0x00000000 /* select upper/lower 64-bit half (bit 0) */
 
  48 #define OTP_UPPER_HALF       0x00000001
 
  49 #define OTP_NO_ECC           0x00000010 /* do not use ECC */
 
  50 #define OTP_LOCK             0x00000020 /* sets page protection bit for page */
 
  51 #define OTP_ACCESS_READ      0x00001000
 
  52 #define OTP_ACCESS_READWRITE 0x00002000
 
  54 /* Return values for all functions */
 
  55 #define OTP_SUCCESS          0x00000000
 
  56 #define OTP_MASTER_ERROR     0x001
 
  57 #define OTP_WRITE_ERROR      0x003
 
  58 #define OTP_READ_ERROR       0x005
 
  59 #define OTP_ACC_VIO_ERROR    0x009
 
  60 #define OTP_DATA_MULT_ERROR  0x011
 
  61 #define OTP_ECC_MULT_ERROR   0x021
 
  62 #define OTP_PREV_WR_ERROR    0x041
 
  63 #define OTP_DATA_SB_WARN     0x100
 
  64 #define OTP_ECC_SB_WARN      0x200
 
  67  *      bfin_otp_read - Read OTP pages
 
  69  *      All reads must be in half page chunks (half page == 64 bits).
 
  71 static ssize_t bfin_otp_read(struct file *file, char __user *buff, size_t count, loff_t *pos)
 
  79         if (count % sizeof(u64))
 
  82         if (mutex_lock_interruptible(&bfin_otp_lock))
 
  86         page = *pos / (sizeof(u64) * 2);
 
  87         while (bytes_done < count) {
 
  88                 flags = (*pos % (sizeof(u64) * 2) ? OTP_UPPER_HALF : OTP_LOWER_HALF);
 
  89                 stamp("processing page %i (%s)", page, (flags == OTP_UPPER_HALF ? "upper" : "lower"));
 
  90                 ret = otp_read(page, flags, &content);
 
  91                 if (ret & OTP_MASTER_ERROR) {
 
  95                 if (copy_to_user(buff + bytes_done, &content, sizeof(content))) {
 
  99                 if (flags == OTP_UPPER_HALF)
 
 101                 bytes_done += sizeof(content);
 
 102                 *pos += sizeof(content);
 
 105         mutex_unlock(&bfin_otp_lock);
 
 110 #ifdef CONFIG_BFIN_OTP_WRITE_ENABLE
 
 112  *      bfin_otp_write - Write OTP pages
 
 114  *      All writes must be in half page chunks (half page == 64 bits).
 
 116 static ssize_t bfin_otp_write(struct file *filp, const char __user *buff, size_t count, loff_t *pos)
 
 120         if (count % sizeof(u64))
 
 123         if (mutex_lock_interruptible(&bfin_otp_lock))
 
 126         /* need otp_init() documentation before this can be implemented */
 
 128         mutex_unlock(&bfin_otp_lock);
 
 133 # define bfin_otp_write NULL
 
 136 static struct file_operations bfin_otp_fops = {
 
 137         .owner    = THIS_MODULE,
 
 138         .read     = bfin_otp_read,
 
 139         .write    = bfin_otp_write,
 
 142 static struct miscdevice bfin_otp_misc_device = {
 
 143         .minor    = MISC_DYNAMIC_MINOR,
 
 145         .fops     = &bfin_otp_fops,
 
 149  *      bfin_otp_init - Initialize module
 
 151  *      Registers the device and notifier handler. Actual device
 
 152  *      initialization is handled by bfin_otp_open().
 
 154 static int __init bfin_otp_init(void)
 
 160         ret = misc_register(&bfin_otp_misc_device);
 
 162                 pr_init(KERN_ERR PFX "unable to register a misc device\n");
 
 166         pr_init(KERN_INFO PFX "initialized\n");
 
 172  *      bfin_otp_exit - Deinitialize module
 
 174  *      Unregisters the device and notifier handler. Actual device
 
 175  *      deinitialization is handled by bfin_otp_close().
 
 177 static void __exit bfin_otp_exit(void)
 
 181         misc_deregister(&bfin_otp_misc_device);
 
 184 module_init(bfin_otp_init);
 
 185 module_exit(bfin_otp_exit);
 
 187 MODULE_AUTHOR("Mike Frysinger <vapier@gentoo.org>");
 
 188 MODULE_DESCRIPTION("Blackfin OTP Memory Interface");
 
 189 MODULE_LICENSE("GPL");