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