stmp_device.c 1.9 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677
  1. // SPDX-License-Identifier: GPL-2.0-or-later
  2. /*
  3. * Copyright (C) 1999 ARM Limited
  4. * Copyright (C) 2000 Deep Blue Solutions Ltd
  5. * Copyright 2006-2007,2010 Freescale Semiconductor, Inc. All Rights Reserved.
  6. * Copyright 2008 Juergen Beisert, kernel@pengutronix.de
  7. * Copyright 2009 Ilya Yanok, Emcraft Systems Ltd, yanok@emcraft.com
  8. * Copyright (C) 2011 Wolfram Sang, Pengutronix e.K.
  9. */
  10. #include <linux/io.h>
  11. #include <linux/errno.h>
  12. #include <linux/delay.h>
  13. #include <linux/compiler.h>
  14. #include <linux/export.h>
  15. #include <linux/stmp_device.h>
  16. #define STMP_MODULE_CLKGATE (1 << 30)
  17. #define STMP_MODULE_SFTRST (1 << 31)
  18. /*
  19. * Clear the bit and poll it cleared. This is usually called with
  20. * a reset address and mask being either SFTRST(bit 31) or CLKGATE
  21. * (bit 30).
  22. */
  23. static int stmp_clear_poll_bit(void __iomem *addr, u32 mask)
  24. {
  25. int timeout = 0x400;
  26. writel(mask, addr + STMP_OFFSET_REG_CLR);
  27. udelay(1);
  28. while ((readl(addr) & mask) && --timeout)
  29. /* nothing */;
  30. return !timeout;
  31. }
  32. int stmp_reset_block(void __iomem *reset_addr)
  33. {
  34. int ret;
  35. int timeout = 0x400;
  36. /* clear and poll SFTRST */
  37. ret = stmp_clear_poll_bit(reset_addr, STMP_MODULE_SFTRST);
  38. if (unlikely(ret))
  39. goto error;
  40. /* clear CLKGATE */
  41. writel(STMP_MODULE_CLKGATE, reset_addr + STMP_OFFSET_REG_CLR);
  42. /* set SFTRST to reset the block */
  43. writel(STMP_MODULE_SFTRST, reset_addr + STMP_OFFSET_REG_SET);
  44. udelay(1);
  45. /* poll CLKGATE becoming set */
  46. while ((!(readl(reset_addr) & STMP_MODULE_CLKGATE)) && --timeout)
  47. /* nothing */;
  48. if (unlikely(!timeout))
  49. goto error;
  50. /* clear and poll SFTRST */
  51. ret = stmp_clear_poll_bit(reset_addr, STMP_MODULE_SFTRST);
  52. if (unlikely(ret))
  53. goto error;
  54. /* clear and poll CLKGATE */
  55. ret = stmp_clear_poll_bit(reset_addr, STMP_MODULE_CLKGATE);
  56. if (unlikely(ret))
  57. goto error;
  58. return 0;
  59. error:
  60. pr_err("%s(%p): module reset timeout\n", __func__, reset_addr);
  61. return -ETIMEDOUT;
  62. }
  63. EXPORT_SYMBOL(stmp_reset_block);