ocotp.c 2.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100
  1. /*
  2. * Copyright 2010 Freescale Semiconductor, Inc. All Rights Reserved.
  3. *
  4. * This program is free software; you can redistribute it and/or modify
  5. * it under the terms of the GNU General Public License as published by
  6. * the Free Software Foundation; either version 2 of the License, or
  7. * (at your option) any later version.
  8. *
  9. * This program is distributed in the hope that it will be useful,
  10. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  11. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  12. * GNU General Public License for more details.
  13. */
  14. #include <linux/delay.h>
  15. #include <linux/err.h>
  16. #include <linux/mutex.h>
  17. #include <linux/of.h>
  18. #include <linux/of_address.h>
  19. #include <asm/processor.h> /* for cpu_relax() */
  20. #include <mach/mxs.h>
  21. #include <mach/common.h>
  22. #define OCOTP_WORD_OFFSET 0x20
  23. #define OCOTP_WORD_COUNT 0x20
  24. #define BM_OCOTP_CTRL_BUSY (1 << 8)
  25. #define BM_OCOTP_CTRL_ERROR (1 << 9)
  26. #define BM_OCOTP_CTRL_RD_BANK_OPEN (1 << 12)
  27. static DEFINE_MUTEX(ocotp_mutex);
  28. static u32 ocotp_words[OCOTP_WORD_COUNT];
  29. const u32 *mxs_get_ocotp(void)
  30. {
  31. struct device_node *np;
  32. void __iomem *ocotp_base;
  33. int timeout = 0x400;
  34. size_t i;
  35. static int once = 0;
  36. if (once)
  37. return ocotp_words;
  38. np = of_find_compatible_node(NULL, NULL, "fsl,ocotp");
  39. ocotp_base = of_iomap(np, 0);
  40. WARN_ON(!ocotp_base);
  41. mutex_lock(&ocotp_mutex);
  42. /*
  43. * clk_enable(hbus_clk) for ocotp can be skipped
  44. * as it must be on when system is running.
  45. */
  46. /* try to clear ERROR bit */
  47. __mxs_clrl(BM_OCOTP_CTRL_ERROR, ocotp_base);
  48. /* check both BUSY and ERROR cleared */
  49. while ((__raw_readl(ocotp_base) &
  50. (BM_OCOTP_CTRL_BUSY | BM_OCOTP_CTRL_ERROR)) && --timeout)
  51. cpu_relax();
  52. if (unlikely(!timeout))
  53. goto error_unlock;
  54. /* open OCOTP banks for read */
  55. __mxs_setl(BM_OCOTP_CTRL_RD_BANK_OPEN, ocotp_base);
  56. /* approximately wait 32 hclk cycles */
  57. udelay(1);
  58. /* poll BUSY bit becoming cleared */
  59. timeout = 0x400;
  60. while ((__raw_readl(ocotp_base) & BM_OCOTP_CTRL_BUSY) && --timeout)
  61. cpu_relax();
  62. if (unlikely(!timeout))
  63. goto error_unlock;
  64. for (i = 0; i < OCOTP_WORD_COUNT; i++)
  65. ocotp_words[i] = __raw_readl(ocotp_base + OCOTP_WORD_OFFSET +
  66. i * 0x10);
  67. /* close banks for power saving */
  68. __mxs_clrl(BM_OCOTP_CTRL_RD_BANK_OPEN, ocotp_base);
  69. once = 1;
  70. mutex_unlock(&ocotp_mutex);
  71. return ocotp_words;
  72. error_unlock:
  73. mutex_unlock(&ocotp_mutex);
  74. pr_err("%s: timeout in reading OCOTP\n", __func__);
  75. return NULL;
  76. }