stmp_device.c 2.1 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980
  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/module.h>
  18. #include <linux/stmp_device.h>
  19. #define STMP_MODULE_CLKGATE (1 << 30)
  20. #define STMP_MODULE_SFTRST (1 << 31)
  21. /*
  22. * Clear the bit and poll it cleared. This is usually called with
  23. * a reset address and mask being either SFTRST(bit 31) or CLKGATE
  24. * (bit 30).
  25. */
  26. static int stmp_clear_poll_bit(void __iomem *addr, u32 mask)
  27. {
  28. int timeout = 0x400;
  29. writel(mask, addr + STMP_OFFSET_REG_CLR);
  30. udelay(1);
  31. while ((readl(addr) & mask) && --timeout)
  32. /* nothing */;
  33. return !timeout;
  34. }
  35. int stmp_reset_block(void __iomem *reset_addr)
  36. {
  37. int ret;
  38. int timeout = 0x400;
  39. /* clear and poll SFTRST */
  40. ret = stmp_clear_poll_bit(reset_addr, STMP_MODULE_SFTRST);
  41. if (unlikely(ret))
  42. goto error;
  43. /* clear CLKGATE */
  44. writel(STMP_MODULE_CLKGATE, reset_addr + STMP_OFFSET_REG_CLR);
  45. /* set SFTRST to reset the block */
  46. writel(STMP_MODULE_SFTRST, reset_addr + STMP_OFFSET_REG_SET);
  47. udelay(1);
  48. /* poll CLKGATE becoming set */
  49. while ((!(readl(reset_addr) & STMP_MODULE_CLKGATE)) && --timeout)
  50. /* nothing */;
  51. if (unlikely(!timeout))
  52. goto error;
  53. /* clear and poll SFTRST */
  54. ret = stmp_clear_poll_bit(reset_addr, STMP_MODULE_SFTRST);
  55. if (unlikely(ret))
  56. goto error;
  57. /* clear and poll CLKGATE */
  58. ret = stmp_clear_poll_bit(reset_addr, STMP_MODULE_CLKGATE);
  59. if (unlikely(ret))
  60. goto error;
  61. return 0;
  62. error:
  63. pr_err("%s(%p): module reset timeout\n", __func__, reset_addr);
  64. return -ETIMEDOUT;
  65. }
  66. EXPORT_SYMBOL(stmp_reset_block);