Greg Kroah-Hartman | 5fd54ac | 2017-11-03 11:28:30 +0100 | [diff] [blame] | 1 | // SPDX-License-Identifier: GPL-2.0 |
Benoit Goby | ee398ba | 2011-03-09 16:28:54 -0800 | [diff] [blame] | 2 | /* |
| 3 | * Copyright (C) 2011 Google, Inc. |
Benoit Goby | ee398ba | 2011-03-09 16:28:54 -0800 | [diff] [blame] | 4 | */ |
| 5 | |
Manjunath Goudar | 9a0e511 | 2013-06-13 11:24:07 -0600 | [diff] [blame] | 6 | #include <linux/export.h> |
Benoit Goby | ee398ba | 2011-03-09 16:28:54 -0800 | [diff] [blame] | 7 | #include <linux/kernel.h> |
| 8 | #include <linux/usb.h> |
| 9 | #include <linux/io.h> |
| 10 | #include <linux/usb/otg.h> |
| 11 | #include <linux/usb/ulpi.h> |
| 12 | |
| 13 | #define ULPI_VIEW_WAKEUP (1 << 31) |
| 14 | #define ULPI_VIEW_RUN (1 << 30) |
| 15 | #define ULPI_VIEW_WRITE (1 << 29) |
| 16 | #define ULPI_VIEW_READ (0 << 29) |
| 17 | #define ULPI_VIEW_ADDR(x) (((x) & 0xff) << 16) |
| 18 | #define ULPI_VIEW_DATA_READ(x) (((x) >> 8) & 0xff) |
| 19 | #define ULPI_VIEW_DATA_WRITE(x) ((x) & 0xff) |
| 20 | |
| 21 | static int ulpi_viewport_wait(void __iomem *view, u32 mask) |
| 22 | { |
| 23 | unsigned long usec = 2000; |
| 24 | |
| 25 | while (usec--) { |
| 26 | if (!(readl(view) & mask)) |
| 27 | return 0; |
| 28 | |
| 29 | udelay(1); |
Joe Perches | 2b84f92 | 2013-10-08 16:01:37 -0700 | [diff] [blame] | 30 | } |
Benoit Goby | ee398ba | 2011-03-09 16:28:54 -0800 | [diff] [blame] | 31 | |
| 32 | return -ETIMEDOUT; |
| 33 | } |
| 34 | |
Heikki Krogerus | 8675381 | 2012-02-13 13:24:02 +0200 | [diff] [blame] | 35 | static int ulpi_viewport_read(struct usb_phy *otg, u32 reg) |
Benoit Goby | ee398ba | 2011-03-09 16:28:54 -0800 | [diff] [blame] | 36 | { |
| 37 | int ret; |
| 38 | void __iomem *view = otg->io_priv; |
| 39 | |
| 40 | writel(ULPI_VIEW_WAKEUP | ULPI_VIEW_WRITE, view); |
| 41 | ret = ulpi_viewport_wait(view, ULPI_VIEW_WAKEUP); |
| 42 | if (ret) |
| 43 | return ret; |
| 44 | |
| 45 | writel(ULPI_VIEW_RUN | ULPI_VIEW_READ | ULPI_VIEW_ADDR(reg), view); |
| 46 | ret = ulpi_viewport_wait(view, ULPI_VIEW_RUN); |
| 47 | if (ret) |
| 48 | return ret; |
| 49 | |
| 50 | return ULPI_VIEW_DATA_READ(readl(view)); |
| 51 | } |
| 52 | |
Heikki Krogerus | 8675381 | 2012-02-13 13:24:02 +0200 | [diff] [blame] | 53 | static int ulpi_viewport_write(struct usb_phy *otg, u32 val, u32 reg) |
Benoit Goby | ee398ba | 2011-03-09 16:28:54 -0800 | [diff] [blame] | 54 | { |
| 55 | int ret; |
| 56 | void __iomem *view = otg->io_priv; |
| 57 | |
| 58 | writel(ULPI_VIEW_WAKEUP | ULPI_VIEW_WRITE, view); |
| 59 | ret = ulpi_viewport_wait(view, ULPI_VIEW_WAKEUP); |
| 60 | if (ret) |
| 61 | return ret; |
| 62 | |
| 63 | writel(ULPI_VIEW_RUN | ULPI_VIEW_WRITE | ULPI_VIEW_DATA_WRITE(val) | |
| 64 | ULPI_VIEW_ADDR(reg), view); |
| 65 | |
| 66 | return ulpi_viewport_wait(view, ULPI_VIEW_RUN); |
| 67 | } |
| 68 | |
Heikki Krogerus | 298b083 | 2012-02-13 13:24:13 +0200 | [diff] [blame] | 69 | struct usb_phy_io_ops ulpi_viewport_access_ops = { |
Benoit Goby | ee398ba | 2011-03-09 16:28:54 -0800 | [diff] [blame] | 70 | .read = ulpi_viewport_read, |
| 71 | .write = ulpi_viewport_write, |
| 72 | }; |
Manjunath Goudar | 9a0e511 | 2013-06-13 11:24:07 -0600 | [diff] [blame] | 73 | EXPORT_SYMBOL_GPL(ulpi_viewport_access_ops); |