stmp_device.c 2.1 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182
  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. * Copyright (C) 2011 Wolfram Sang, Pengutronix e.K.
  8. *
  9. * This program is free software; you can redistribute it and/or modify
  10. * it under the terms of the GNU General Public License as published by
  11. * the Free Software Foundation; either version 2 of the License, or
  12. * (at your option) any later version.
  13. */
  14. #include <linux/io.h>
  15. #include <linux/errno.h>
  16. #include <linux/delay.h>
  17. #include <linux/compiler.h>
  18. #include <linux/export.h>
  19. #include <linux/stmp_device.h>
  20. #define STMP_MODULE_CLKGATE (1 << 30)
  21. #define STMP_MODULE_SFTRST (1 << 31)
  22. /*
  23. * Clear the bit and poll it cleared. This is usually called with
  24. * a reset address and mask being either SFTRST(bit 31) or CLKGATE
  25. * (bit 30).
  26. */
  27. static int stmp_clear_poll_bit(void __iomem *addr, u32 mask)
  28. {
  29. int timeout = 0x400;
  30. writel(mask, addr + STMP_OFFSET_REG_CLR);
  31. udelay(1);
  32. while ((readl(addr) & mask) && --timeout)
  33. /* nothing */;
  34. return !timeout;
  35. }
  36. int stmp_reset_block(void __iomem *reset_addr)
  37. {
  38. int ret;
  39. int timeout = 0x400;
  40. /* clear and poll SFTRST */
  41. ret = stmp_clear_poll_bit(reset_addr, STMP_MODULE_SFTRST);
  42. if (unlikely(ret))
  43. goto error;
  44. /* clear CLKGATE */
  45. writel(STMP_MODULE_CLKGATE, reset_addr + STMP_OFFSET_REG_CLR);
  46. /* set SFTRST to reset the block */
  47. writel(STMP_MODULE_SFTRST, reset_addr + STMP_OFFSET_REG_SET);
  48. udelay(1);
  49. /* poll CLKGATE becoming set */
  50. while ((!(readl(reset_addr) & STMP_MODULE_CLKGATE)) && --timeout)
  51. /* nothing */;
  52. if (unlikely(!timeout))
  53. goto error;
  54. /* clear and poll SFTRST */
  55. ret = stmp_clear_poll_bit(reset_addr, STMP_MODULE_SFTRST);
  56. if (unlikely(ret))
  57. goto error;
  58. /* clear and poll CLKGATE */
  59. ret = stmp_clear_poll_bit(reset_addr, STMP_MODULE_CLKGATE);
  60. if (unlikely(ret))
  61. goto error;
  62. return 0;
  63. error:
  64. pr_err("%s(%p): module reset timeout\n", __func__, reset_addr);
  65. return -ETIMEDOUT;
  66. }
  67. EXPORT_SYMBOL(stmp_reset_block);