bfin-otp.c 4.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189
  1. /*
  2. * Blackfin On-Chip OTP Memory Interface
  3. * Supports BF52x/BF54x
  4. *
  5. * Copyright 2007-2008 Analog Devices Inc.
  6. *
  7. * Enter bugs at http://blackfin.uclinux.org/
  8. *
  9. * Licensed under the GPL-2 or later.
  10. */
  11. #include <linux/device.h>
  12. #include <linux/errno.h>
  13. #include <linux/fs.h>
  14. #include <linux/init.h>
  15. #include <linux/miscdevice.h>
  16. #include <linux/module.h>
  17. #include <linux/mutex.h>
  18. #include <linux/types.h>
  19. #include <asm/blackfin.h>
  20. #include <asm/uaccess.h>
  21. #define stamp(fmt, args...) pr_debug("%s:%i: " fmt "\n", __func__, __LINE__, ## args)
  22. #define stampit() stamp("here i am")
  23. #define pr_init(fmt, args...) ({ static const __initconst char __fmt[] = fmt; printk(__fmt, ## args); })
  24. #define DRIVER_NAME "bfin-otp"
  25. #define PFX DRIVER_NAME ": "
  26. static DEFINE_MUTEX(bfin_otp_lock);
  27. /* OTP Boot ROM functions */
  28. #define _BOOTROM_OTP_COMMAND 0xEF000018
  29. #define _BOOTROM_OTP_READ 0xEF00001A
  30. #define _BOOTROM_OTP_WRITE 0xEF00001C
  31. static u32 (* const otp_command)(u32 command, u32 value) = (void *)_BOOTROM_OTP_COMMAND;
  32. static u32 (* const otp_read)(u32 page, u32 flags, u64 *page_content) = (void *)_BOOTROM_OTP_READ;
  33. static u32 (* const otp_write)(u32 page, u32 flags, u64 *page_content) = (void *)_BOOTROM_OTP_WRITE;
  34. /* otp_command(): defines for "command" */
  35. #define OTP_INIT 0x00000001
  36. #define OTP_CLOSE 0x00000002
  37. /* otp_{read,write}(): defines for "flags" */
  38. #define OTP_LOWER_HALF 0x00000000 /* select upper/lower 64-bit half (bit 0) */
  39. #define OTP_UPPER_HALF 0x00000001
  40. #define OTP_NO_ECC 0x00000010 /* do not use ECC */
  41. #define OTP_LOCK 0x00000020 /* sets page protection bit for page */
  42. #define OTP_ACCESS_READ 0x00001000
  43. #define OTP_ACCESS_READWRITE 0x00002000
  44. /* Return values for all functions */
  45. #define OTP_SUCCESS 0x00000000
  46. #define OTP_MASTER_ERROR 0x001
  47. #define OTP_WRITE_ERROR 0x003
  48. #define OTP_READ_ERROR 0x005
  49. #define OTP_ACC_VIO_ERROR 0x009
  50. #define OTP_DATA_MULT_ERROR 0x011
  51. #define OTP_ECC_MULT_ERROR 0x021
  52. #define OTP_PREV_WR_ERROR 0x041
  53. #define OTP_DATA_SB_WARN 0x100
  54. #define OTP_ECC_SB_WARN 0x200
  55. /**
  56. * bfin_otp_read - Read OTP pages
  57. *
  58. * All reads must be in half page chunks (half page == 64 bits).
  59. */
  60. static ssize_t bfin_otp_read(struct file *file, char __user *buff, size_t count, loff_t *pos)
  61. {
  62. ssize_t bytes_done;
  63. u32 page, flags, ret;
  64. u64 content;
  65. stampit();
  66. if (count % sizeof(u64))
  67. return -EMSGSIZE;
  68. if (mutex_lock_interruptible(&bfin_otp_lock))
  69. return -ERESTARTSYS;
  70. bytes_done = 0;
  71. page = *pos / (sizeof(u64) * 2);
  72. while (bytes_done < count) {
  73. flags = (*pos % (sizeof(u64) * 2) ? OTP_UPPER_HALF : OTP_LOWER_HALF);
  74. stamp("processing page %i (%s)", page, (flags == OTP_UPPER_HALF ? "upper" : "lower"));
  75. ret = otp_read(page, flags, &content);
  76. if (ret & OTP_MASTER_ERROR) {
  77. bytes_done = -EIO;
  78. break;
  79. }
  80. if (copy_to_user(buff + bytes_done, &content, sizeof(content))) {
  81. bytes_done = -EFAULT;
  82. break;
  83. }
  84. if (flags == OTP_UPPER_HALF)
  85. ++page;
  86. bytes_done += sizeof(content);
  87. *pos += sizeof(content);
  88. }
  89. mutex_unlock(&bfin_otp_lock);
  90. return bytes_done;
  91. }
  92. #ifdef CONFIG_BFIN_OTP_WRITE_ENABLE
  93. /**
  94. * bfin_otp_write - Write OTP pages
  95. *
  96. * All writes must be in half page chunks (half page == 64 bits).
  97. */
  98. static ssize_t bfin_otp_write(struct file *filp, const char __user *buff, size_t count, loff_t *pos)
  99. {
  100. stampit();
  101. if (count % sizeof(u64))
  102. return -EMSGSIZE;
  103. if (mutex_lock_interruptible(&bfin_otp_lock))
  104. return -ERESTARTSYS;
  105. /* need otp_init() documentation before this can be implemented */
  106. mutex_unlock(&bfin_otp_lock);
  107. return -EINVAL;
  108. }
  109. #else
  110. # define bfin_otp_write NULL
  111. #endif
  112. static struct file_operations bfin_otp_fops = {
  113. .owner = THIS_MODULE,
  114. .read = bfin_otp_read,
  115. .write = bfin_otp_write,
  116. };
  117. static struct miscdevice bfin_otp_misc_device = {
  118. .minor = MISC_DYNAMIC_MINOR,
  119. .name = DRIVER_NAME,
  120. .fops = &bfin_otp_fops,
  121. };
  122. /**
  123. * bfin_otp_init - Initialize module
  124. *
  125. * Registers the device and notifier handler. Actual device
  126. * initialization is handled by bfin_otp_open().
  127. */
  128. static int __init bfin_otp_init(void)
  129. {
  130. int ret;
  131. stampit();
  132. ret = misc_register(&bfin_otp_misc_device);
  133. if (ret) {
  134. pr_init(KERN_ERR PFX "unable to register a misc device\n");
  135. return ret;
  136. }
  137. pr_init(KERN_INFO PFX "initialized\n");
  138. return 0;
  139. }
  140. /**
  141. * bfin_otp_exit - Deinitialize module
  142. *
  143. * Unregisters the device and notifier handler. Actual device
  144. * deinitialization is handled by bfin_otp_close().
  145. */
  146. static void __exit bfin_otp_exit(void)
  147. {
  148. stampit();
  149. misc_deregister(&bfin_otp_misc_device);
  150. }
  151. module_init(bfin_otp_init);
  152. module_exit(bfin_otp_exit);
  153. MODULE_AUTHOR("Mike Frysinger <vapier@gentoo.org>");
  154. MODULE_DESCRIPTION("Blackfin OTP Memory Interface");
  155. MODULE_LICENSE("GPL");