ocotp.c 2.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293
  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 <asm/processor.h> /* for cpu_relax() */
  18. #include <mach/mxs.h>
  19. #include <mach/common.h>
  20. #define OCOTP_WORD_OFFSET 0x20
  21. #define OCOTP_WORD_COUNT 0x20
  22. #define BM_OCOTP_CTRL_BUSY (1 << 8)
  23. #define BM_OCOTP_CTRL_ERROR (1 << 9)
  24. #define BM_OCOTP_CTRL_RD_BANK_OPEN (1 << 12)
  25. static DEFINE_MUTEX(ocotp_mutex);
  26. static u32 ocotp_words[OCOTP_WORD_COUNT];
  27. const u32 *mxs_get_ocotp(void)
  28. {
  29. void __iomem *ocotp_base = MXS_IO_ADDRESS(MXS_OCOTP_BASE_ADDR);
  30. int timeout = 0x400;
  31. size_t i;
  32. static int once = 0;
  33. if (once)
  34. return ocotp_words;
  35. mutex_lock(&ocotp_mutex);
  36. /*
  37. * clk_enable(hbus_clk) for ocotp can be skipped
  38. * as it must be on when system is running.
  39. */
  40. /* try to clear ERROR bit */
  41. __mxs_clrl(BM_OCOTP_CTRL_ERROR, ocotp_base);
  42. /* check both BUSY and ERROR cleared */
  43. while ((__raw_readl(ocotp_base) &
  44. (BM_OCOTP_CTRL_BUSY | BM_OCOTP_CTRL_ERROR)) && --timeout)
  45. cpu_relax();
  46. if (unlikely(!timeout))
  47. goto error_unlock;
  48. /* open OCOTP banks for read */
  49. __mxs_setl(BM_OCOTP_CTRL_RD_BANK_OPEN, ocotp_base);
  50. /* approximately wait 32 hclk cycles */
  51. udelay(1);
  52. /* poll BUSY bit becoming cleared */
  53. timeout = 0x400;
  54. while ((__raw_readl(ocotp_base) & BM_OCOTP_CTRL_BUSY) && --timeout)
  55. cpu_relax();
  56. if (unlikely(!timeout))
  57. goto error_unlock;
  58. for (i = 0; i < OCOTP_WORD_COUNT; i++)
  59. ocotp_words[i] = __raw_readl(ocotp_base + OCOTP_WORD_OFFSET +
  60. i * 0x10);
  61. /* close banks for power saving */
  62. __mxs_clrl(BM_OCOTP_CTRL_RD_BANK_OPEN, ocotp_base);
  63. once = 1;
  64. mutex_unlock(&ocotp_mutex);
  65. return ocotp_words;
  66. error_unlock:
  67. mutex_unlock(&ocotp_mutex);
  68. pr_err("%s: timeout in reading OCOTP\n", __func__);
  69. return NULL;
  70. }