qfp_fuse.c 9.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427
  1. /* Copyright (c) 2011, 2014 The Linux Foundation. All rights reserved.
  2. *
  3. * This program is free software; you can redistribute it and/or modify
  4. * it under the terms of the GNU General Public License version 2 and
  5. * only version 2 as published by the Free Software Foundation.
  6. *
  7. * This program is distributed in the hope that it will be useful,
  8. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  9. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  10. * GNU General Public License for more details.
  11. */
  12. #define pr_fmt(fmt) "%s: " fmt, __func__
  13. #include <linux/init.h>
  14. #include <linux/module.h>
  15. #include <linux/fs.h>
  16. #include <linux/slab.h>
  17. #include <linux/miscdevice.h>
  18. #include <linux/delay.h>
  19. #include <linux/qfp_fuse.h>
  20. #include <linux/io.h>
  21. #include <linux/uaccess.h>
  22. #include <linux/regulator/consumer.h>
  23. #include <linux/platform_device.h>
  24. #include <linux/mutex.h>
  25. /*
  26. * Time QFPROM requires to reliably burn a fuse.
  27. */
  28. #define QFPROM_BLOW_TIMEOUT_US 20
  29. #define QFPROM_BLOW_TIMER_OFFSET 0x2038
  30. /*
  31. * Denotes number of cycles required to blow the fuse.
  32. */
  33. #define QFPROM_BLOW_TIMER_VALUE (QFPROM_BLOW_TIMEOUT_US * 83)
  34. #define QFPROM_BLOW_STATUS_OFFSET 0x204C
  35. #define QFPROM_BLOW_STATUS_BUSY 0x01
  36. #define QFPROM_BLOW_STATUS_ERROR 0x02
  37. #define QFP_FUSE_READY 0x01
  38. #define QFP_FUSE_OFF 0x00
  39. #define QFP_FUSE_BUF_SIZE 64
  40. #define UINT32_MAX (0xFFFFFFFFU)
  41. struct qfp_priv_t {
  42. uint32_t base;
  43. uint32_t end;
  44. struct mutex lock;
  45. struct regulator *fuse_vdd;
  46. u8 state;
  47. };
  48. /* We need only one instance of this for the driver */
  49. static struct qfp_priv_t *qfp_priv;
  50. static inline bool is_usr_req_valid(const struct qfp_fuse_req *req)
  51. {
  52. uint32_t size = qfp_priv->end - qfp_priv->base;
  53. uint32_t req_size;
  54. if (req->size >= (UINT32_MAX / sizeof(uint32_t)))
  55. return false;
  56. req_size = req->size * sizeof(uint32_t);
  57. if ((req_size == 0) || (req_size > size))
  58. return false;
  59. if (req->offset >= size)
  60. return false;
  61. if ((req->offset + req_size) > size)
  62. return false;
  63. return true;
  64. }
  65. static int qfp_fuse_open(struct inode *inode, struct file *filp)
  66. {
  67. if (qfp_priv == NULL)
  68. return -ENODEV;
  69. filp->private_data = qfp_priv;
  70. return 0;
  71. }
  72. static int qfp_fuse_release(struct inode *inode, struct file *filp)
  73. {
  74. filp->private_data = NULL;
  75. return 0;
  76. }
  77. static inline int qfp_fuse_wait_for_fuse_blow(u32 *status)
  78. {
  79. u32 timeout = QFPROM_BLOW_TIMEOUT_US;
  80. /* wait for 400us before checking for the first time */
  81. udelay(400);
  82. do {
  83. *status = readl_relaxed(
  84. qfp_priv->base + QFPROM_BLOW_STATUS_OFFSET);
  85. if (!(*status & QFPROM_BLOW_STATUS_BUSY))
  86. return 0;
  87. timeout--;
  88. udelay(1);
  89. } while (timeout);
  90. pr_err("Timeout waiting for FUSE blow, status = %x\n", *status);
  91. return -ETIMEDOUT;
  92. }
  93. static inline int qfp_fuse_enable_regulator(void)
  94. {
  95. int err;
  96. err = regulator_enable(qfp_priv->fuse_vdd);
  97. if (err != 0)
  98. pr_err("Error (%d) enabling regulator\n", err);
  99. return err;
  100. }
  101. static inline int qfp_fuse_disable_regulator(void)
  102. {
  103. int err;
  104. err = regulator_disable(qfp_priv->fuse_vdd);
  105. if (err != 0)
  106. pr_err("Error (%d) disabling regulator\n", err);
  107. return err;
  108. }
  109. static int qfp_fuse_write_word(u32 *addr, u32 data)
  110. {
  111. u32 blow_status = 0;
  112. u32 read_data;
  113. int err;
  114. /* Set QFPROM blow timer register */
  115. writel_relaxed(QFPROM_BLOW_TIMER_VALUE,
  116. qfp_priv->base + QFPROM_BLOW_TIMER_OFFSET);
  117. mb();
  118. /* Enable LVS0 regulator */
  119. err = qfp_fuse_enable_regulator();
  120. if (err != 0)
  121. return err;
  122. /*
  123. * Wait for about 1ms. However msleep(1) can sleep for
  124. * up to 20ms as per Documentation/timers/timers-howto.txt.
  125. * Time is not a constraint here.
  126. */
  127. msleep(20);
  128. /* Write data */
  129. __raw_writel(data, addr);
  130. mb();
  131. /* blow_status = QFPROM_BLOW_STATUS_BUSY; */
  132. err = qfp_fuse_wait_for_fuse_blow(&blow_status);
  133. if (err) {
  134. qfp_fuse_disable_regulator();
  135. return err;
  136. }
  137. /* Check error status */
  138. if (blow_status & QFPROM_BLOW_STATUS_ERROR) {
  139. pr_err("Fuse blow status error: %d\n", blow_status);
  140. qfp_fuse_disable_regulator();
  141. return -EFAULT;
  142. }
  143. /* Disable regulator */
  144. qfp_fuse_disable_regulator();
  145. /*
  146. * Wait for about 1ms. However msleep(1) can sleep for
  147. * up to 20ms as per Documentation/timers/timers-howto.txt.
  148. * Time is not a constraint here.
  149. */
  150. msleep(20);
  151. /* Verify written data */
  152. read_data = readl_relaxed(addr);
  153. if (read_data != data) {
  154. pr_err("Error: read/write data mismatch\n");
  155. pr_err("Address = %p written data = %x read data = %x\n",
  156. addr, data, read_data);
  157. return -EFAULT;
  158. }
  159. return 0;
  160. }
  161. static long
  162. qfp_fuse_ioctl(struct file *filp, unsigned int cmd, unsigned long arg)
  163. {
  164. int err = 0;
  165. struct qfp_fuse_req req;
  166. u32 fuse_buf[QFP_FUSE_BUF_SIZE];
  167. u32 *buf = fuse_buf;
  168. u32 *ptr = NULL;
  169. int i;
  170. /* Verify user arguments. */
  171. if (_IOC_TYPE(cmd) != QFP_FUSE_IOC_MAGIC)
  172. return -ENOTTY;
  173. switch (cmd) {
  174. case QFP_FUSE_IOC_READ:
  175. if (arg == 0) {
  176. pr_err("user space arg not supplied\n");
  177. err = -EFAULT;
  178. break;
  179. }
  180. if (copy_from_user(&req, (void __user *)arg, sizeof(req))) {
  181. pr_err("Error copying req from user space\n");
  182. err = -EFAULT;
  183. break;
  184. }
  185. /* Check for limits */
  186. if (is_usr_req_valid(&req) == false) {
  187. pr_err("Invalid request\n");
  188. err = -EINVAL;
  189. break;
  190. }
  191. if (req.size > QFP_FUSE_BUF_SIZE) {
  192. /* Allocate memory for buffer */
  193. ptr = kzalloc(req.size * 4, GFP_KERNEL);
  194. if (ptr == NULL) {
  195. pr_alert("No memory for data\n");
  196. err = -ENOMEM;
  197. break;
  198. }
  199. buf = ptr;
  200. }
  201. if (mutex_lock_interruptible(&qfp_priv->lock)) {
  202. err = -ERESTARTSYS;
  203. break;
  204. }
  205. /* Read data */
  206. for (i = 0; i < req.size; i++)
  207. buf[i] = readl_relaxed(
  208. ((u32 *) (qfp_priv->base + req.offset)) + i);
  209. if (copy_to_user((void __user *)req.data, buf, 4*(req.size))) {
  210. pr_err("Error copying to user space\n");
  211. err = -EFAULT;
  212. }
  213. mutex_unlock(&qfp_priv->lock);
  214. break;
  215. case QFP_FUSE_IOC_WRITE:
  216. if (arg == 0) {
  217. pr_err("user space arg not supplied\n");
  218. err = -EFAULT;
  219. break;
  220. }
  221. if (copy_from_user(&req, (void __user *)arg, sizeof(req))) {
  222. pr_err("Error copying req from user space\n");
  223. err = -EFAULT;
  224. break;
  225. }
  226. /* Check for limits */
  227. if (is_usr_req_valid(&req) == false) {
  228. pr_err("Invalid request\n");
  229. err = -EINVAL;
  230. break;
  231. }
  232. if (req.size > QFP_FUSE_BUF_SIZE) {
  233. /* Allocate memory for buffer */
  234. ptr = kzalloc(req.size * 4, GFP_KERNEL);
  235. if (ptr == NULL) {
  236. pr_alert("No memory for data\n");
  237. err = -ENOMEM;
  238. break;
  239. }
  240. buf = ptr;
  241. }
  242. /* Copy user data to local buffer */
  243. if (copy_from_user(buf, (void __user *)req.data,
  244. 4 * (req.size))) {
  245. pr_err("Error copying data from user space\n");
  246. err = -EFAULT;
  247. break;
  248. }
  249. if (mutex_lock_interruptible(&qfp_priv->lock)) {
  250. err = -ERESTARTSYS;
  251. break;
  252. }
  253. /* Write data word at a time */
  254. for (i = 0; i < req.size && !err; i++) {
  255. err = qfp_fuse_write_word(((u32 *) (
  256. qfp_priv->base + req.offset) + i), buf[i]);
  257. }
  258. mutex_unlock(&qfp_priv->lock);
  259. break;
  260. default:
  261. pr_err("Invalid ioctl command.\n");
  262. return -ENOTTY;
  263. }
  264. kfree(ptr);
  265. return err;
  266. }
  267. static const struct file_operations qfp_fuse_fops = {
  268. .owner = THIS_MODULE,
  269. .unlocked_ioctl = qfp_fuse_ioctl,
  270. .open = qfp_fuse_open,
  271. .release = qfp_fuse_release
  272. };
  273. static struct miscdevice qfp_fuse_dev = {
  274. .minor = MISC_DYNAMIC_MINOR,
  275. .name = "qfpfuse",
  276. .fops = &qfp_fuse_fops
  277. };
  278. static int qfp_fuse_probe(struct platform_device *pdev)
  279. {
  280. int ret = 0;
  281. struct resource *res;
  282. const char *regulator_name = pdev->dev.platform_data;
  283. res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
  284. if (!res)
  285. return -ENODEV;
  286. if (!regulator_name)
  287. return -EINVAL;
  288. /* Initialize */
  289. qfp_priv = kzalloc(sizeof(struct qfp_priv_t), GFP_KERNEL);
  290. if (qfp_priv == NULL) {
  291. pr_alert("Not enough memory to initialize device\n");
  292. return -ENOMEM;
  293. }
  294. /* The driver is passed ioremapped address */
  295. qfp_priv->base = res->start;
  296. qfp_priv->end = res->end;
  297. /* Get regulator for QFPROM writes */
  298. qfp_priv->fuse_vdd = regulator_get(NULL, regulator_name);
  299. if (IS_ERR(qfp_priv->fuse_vdd)) {
  300. ret = PTR_ERR(qfp_priv->fuse_vdd);
  301. pr_err("Err (%d) getting %s\n", ret, regulator_name);
  302. qfp_priv->fuse_vdd = NULL;
  303. goto err;
  304. }
  305. mutex_init(&qfp_priv->lock);
  306. ret = misc_register(&qfp_fuse_dev);
  307. if (ret < 0)
  308. goto err;
  309. pr_info("Fuse driver base:%x end:%x\n", qfp_priv->base, qfp_priv->end);
  310. return 0;
  311. err:
  312. if (qfp_priv->fuse_vdd)
  313. regulator_put(qfp_priv->fuse_vdd);
  314. kfree(qfp_priv);
  315. qfp_priv = NULL;
  316. return ret;
  317. }
  318. static int __devexit qfp_fuse_remove(struct platform_device *plat)
  319. {
  320. if (qfp_priv && qfp_priv->fuse_vdd)
  321. regulator_put(qfp_priv->fuse_vdd);
  322. kfree(qfp_priv);
  323. qfp_priv = NULL;
  324. misc_deregister(&qfp_fuse_dev);
  325. pr_info("Removing Fuse driver\n");
  326. return 0;
  327. }
  328. static struct platform_driver qfp_fuse_driver = {
  329. .probe = qfp_fuse_probe,
  330. .remove = qfp_fuse_remove,
  331. .driver = {
  332. .name = "qfp_fuse_driver",
  333. .owner = THIS_MODULE,
  334. },
  335. };
  336. static int __init qfp_fuse_init(void)
  337. {
  338. return platform_driver_register(&qfp_fuse_driver);
  339. }
  340. static void __exit qfp_fuse_exit(void)
  341. {
  342. platform_driver_unregister(&qfp_fuse_driver);
  343. }
  344. MODULE_LICENSE("GPL v2");
  345. MODULE_AUTHOR("Rohit Vaswani <rvaswani@codeaurora.org>");
  346. MODULE_DESCRIPTION("Driver to read/write to QFPROM fuses.");
  347. MODULE_VERSION("1.01");
  348. module_init(qfp_fuse_init);
  349. module_exit(qfp_fuse_exit);