system.c 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156
  1. /*
  2. * Copyright (C) 1999 ARM Limited
  3. * Copyright (C) 2000 Deep Blue Solutions Ltd
  4. * Copyright 2006-2007,2010 Freescale Semiconductor, Inc. All Rights Reserved.
  5. * Copyright 2008 Juergen Beisert, kernel@pengutronix.de
  6. * Copyright 2009 Ilya Yanok, Emcraft Systems Ltd, yanok@emcraft.com
  7. *
  8. * This program is free software; you can redistribute it and/or modify
  9. * it under the terms of the GNU General Public License as published by
  10. * the Free Software Foundation; either version 2 of the License, or
  11. * (at your option) any later version.
  12. *
  13. * This program is distributed in the hope that it will be useful,
  14. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  15. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  16. * GNU General Public License for more details.
  17. */
  18. #include <linux/kernel.h>
  19. #include <linux/clk.h>
  20. #include <linux/io.h>
  21. #include <linux/err.h>
  22. #include <linux/delay.h>
  23. #include <linux/init.h>
  24. #include <linux/module.h>
  25. #include <asm/proc-fns.h>
  26. #include <asm/system_misc.h>
  27. #include <mach/mxs.h>
  28. #include <mach/common.h>
  29. #define MX23_CLKCTRL_RESET_OFFSET 0x120
  30. #define MX28_CLKCTRL_RESET_OFFSET 0x1e0
  31. #define MXS_CLKCTRL_RESET_CHIP (1 << 1)
  32. #define MXS_MODULE_CLKGATE (1 << 30)
  33. #define MXS_MODULE_SFTRST (1 << 31)
  34. #define CLKCTRL_TIMEOUT 10 /* 10 ms */
  35. static void __iomem *mxs_clkctrl_reset_addr;
  36. /*
  37. * Reset the system. It is called by machine_restart().
  38. */
  39. void mxs_restart(char mode, const char *cmd)
  40. {
  41. /* reset the chip */
  42. __mxs_setl(MXS_CLKCTRL_RESET_CHIP, mxs_clkctrl_reset_addr);
  43. pr_err("Failed to assert the chip reset\n");
  44. /* Delay to allow the serial port to show the message */
  45. mdelay(50);
  46. /* We'll take a jump through zero as a poor second */
  47. soft_restart(0);
  48. }
  49. static int __init mxs_arch_reset_init(void)
  50. {
  51. struct clk *clk;
  52. mxs_clkctrl_reset_addr = MXS_IO_ADDRESS(MXS_CLKCTRL_BASE_ADDR) +
  53. (cpu_is_mx23() ? MX23_CLKCTRL_RESET_OFFSET :
  54. MX28_CLKCTRL_RESET_OFFSET);
  55. clk = clk_get_sys("rtc", NULL);
  56. if (!IS_ERR(clk))
  57. clk_prepare_enable(clk);
  58. return 0;
  59. }
  60. core_initcall(mxs_arch_reset_init);
  61. /*
  62. * Clear the bit and poll it cleared. This is usually called with
  63. * a reset address and mask being either SFTRST(bit 31) or CLKGATE
  64. * (bit 30).
  65. */
  66. static int clear_poll_bit(void __iomem *addr, u32 mask)
  67. {
  68. int timeout = 0x400;
  69. /* clear the bit */
  70. __mxs_clrl(mask, addr);
  71. /*
  72. * SFTRST needs 3 GPMI clocks to settle, the reference manual
  73. * recommends to wait 1us.
  74. */
  75. udelay(1);
  76. /* poll the bit becoming clear */
  77. while ((__raw_readl(addr) & mask) && --timeout)
  78. /* nothing */;
  79. return !timeout;
  80. }
  81. int mxs_reset_block(void __iomem *reset_addr)
  82. {
  83. int ret;
  84. int timeout = 0x400;
  85. /* clear and poll SFTRST */
  86. ret = clear_poll_bit(reset_addr, MXS_MODULE_SFTRST);
  87. if (unlikely(ret))
  88. goto error;
  89. /* clear CLKGATE */
  90. __mxs_clrl(MXS_MODULE_CLKGATE, reset_addr);
  91. /* set SFTRST to reset the block */
  92. __mxs_setl(MXS_MODULE_SFTRST, reset_addr);
  93. udelay(1);
  94. /* poll CLKGATE becoming set */
  95. while ((!(__raw_readl(reset_addr) & MXS_MODULE_CLKGATE)) && --timeout)
  96. /* nothing */;
  97. if (unlikely(!timeout))
  98. goto error;
  99. /* clear and poll SFTRST */
  100. ret = clear_poll_bit(reset_addr, MXS_MODULE_SFTRST);
  101. if (unlikely(ret))
  102. goto error;
  103. /* clear and poll CLKGATE */
  104. ret = clear_poll_bit(reset_addr, MXS_MODULE_CLKGATE);
  105. if (unlikely(ret))
  106. goto error;
  107. return 0;
  108. error:
  109. pr_err("%s(%p): module reset timeout\n", __func__, reset_addr);
  110. return -ETIMEDOUT;
  111. }
  112. EXPORT_SYMBOL(mxs_reset_block);
  113. int mxs_clkctrl_timeout(unsigned int reg_offset, unsigned int mask)
  114. {
  115. unsigned long timeout = jiffies + msecs_to_jiffies(CLKCTRL_TIMEOUT);
  116. while (readl_relaxed(MXS_IO_ADDRESS(MXS_CLKCTRL_BASE_ADDR)
  117. + reg_offset) & mask) {
  118. if (time_after(jiffies, timeout)) {
  119. pr_err("Timeout at CLKCTRL + 0x%x\n", reg_offset);
  120. return -ETIMEDOUT;
  121. }
  122. }
  123. return 0;
  124. }