hw.c 2.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293
  1. // SPDX-License-Identifier: GPL-2.0
  2. /*
  3. * Wireless Host Controller (WHC) hardware access helpers.
  4. *
  5. * Copyright (C) 2007 Cambridge Silicon Radio Ltd.
  6. */
  7. #include <linux/kernel.h>
  8. #include <linux/dma-mapping.h>
  9. #include <linux/uwb/umc.h>
  10. #include "../../wusbcore/wusbhc.h"
  11. #include "whcd.h"
  12. void whc_write_wusbcmd(struct whc *whc, u32 mask, u32 val)
  13. {
  14. unsigned long flags;
  15. u32 cmd;
  16. spin_lock_irqsave(&whc->lock, flags);
  17. cmd = le_readl(whc->base + WUSBCMD);
  18. cmd = (cmd & ~mask) | val;
  19. le_writel(cmd, whc->base + WUSBCMD);
  20. spin_unlock_irqrestore(&whc->lock, flags);
  21. }
  22. /**
  23. * whc_do_gencmd - start a generic command via the WUSBGENCMDSTS register
  24. * @whc: the WHCI HC
  25. * @cmd: command to start.
  26. * @params: parameters for the command (the WUSBGENCMDPARAMS register value).
  27. * @addr: pointer to any data for the command (may be NULL).
  28. * @len: length of the data (if any).
  29. */
  30. int whc_do_gencmd(struct whc *whc, u32 cmd, u32 params, void *addr, size_t len)
  31. {
  32. unsigned long flags;
  33. dma_addr_t dma_addr;
  34. int t;
  35. int ret = 0;
  36. mutex_lock(&whc->mutex);
  37. /* Wait for previous command to complete. */
  38. t = wait_event_timeout(whc->cmd_wq,
  39. (le_readl(whc->base + WUSBGENCMDSTS) & WUSBGENCMDSTS_ACTIVE) == 0,
  40. WHC_GENCMD_TIMEOUT_MS);
  41. if (t == 0) {
  42. dev_err(&whc->umc->dev, "generic command timeout (%04x/%04x)\n",
  43. le_readl(whc->base + WUSBGENCMDSTS),
  44. le_readl(whc->base + WUSBGENCMDPARAMS));
  45. ret = -ETIMEDOUT;
  46. goto out;
  47. }
  48. if (addr) {
  49. memcpy(whc->gen_cmd_buf, addr, len);
  50. dma_addr = whc->gen_cmd_buf_dma;
  51. } else
  52. dma_addr = 0;
  53. /* Poke registers to start cmd. */
  54. spin_lock_irqsave(&whc->lock, flags);
  55. le_writel(params, whc->base + WUSBGENCMDPARAMS);
  56. le_writeq(dma_addr, whc->base + WUSBGENADDR);
  57. le_writel(WUSBGENCMDSTS_ACTIVE | WUSBGENCMDSTS_IOC | cmd,
  58. whc->base + WUSBGENCMDSTS);
  59. spin_unlock_irqrestore(&whc->lock, flags);
  60. out:
  61. mutex_unlock(&whc->mutex);
  62. return ret;
  63. }
  64. /**
  65. * whc_hw_error - recover from a hardware error
  66. * @whc: the WHCI HC that broke.
  67. * @reason: a description of the failure.
  68. *
  69. * Recover from broken hardware with a full reset.
  70. */
  71. void whc_hw_error(struct whc *whc, const char *reason)
  72. {
  73. struct wusbhc *wusbhc = &whc->wusbhc;
  74. dev_err(&whc->umc->dev, "hardware error: %s\n", reason);
  75. wusbhc_reset_all(wusbhc);
  76. }