1 // SPDX-License-Identifier: GPL-2.0
2 /*
3 * OMAP ulpi viewport support
4 * Based on drivers/usb/ulpi/ulpi-viewport.c
5 *
6 * Copyright (C) 2011 Texas Instruments Incorporated - http://www.ti.com
7 * Author: Govindraj R <govindraj.raja@ti.com>
8 */
9
10 #include <common.h>
11 #include <asm/io.h>
12 #include <usb/ulpi.h>
13
14 #define OMAP_ULPI_WR_OPSEL (2 << 22)
15 #define OMAP_ULPI_RD_OPSEL (3 << 22)
16 #define OMAP_ULPI_START (1 << 31)
17
18 /*
19 * Wait for having ulpi in done state
20 */
ulpi_wait(struct ulpi_viewport * ulpi_vp,u32 mask)21 static int ulpi_wait(struct ulpi_viewport *ulpi_vp, u32 mask)
22 {
23 int timeout = CONFIG_USB_ULPI_TIMEOUT;
24
25 while (--timeout) {
26 if (!(readl(ulpi_vp->viewport_addr) & mask))
27 return 0;
28
29 udelay(1);
30 }
31
32 return ULPI_ERROR;
33 }
34
35 /*
36 * Issue a ULPI read/write request
37 */
ulpi_request(struct ulpi_viewport * ulpi_vp,u32 value)38 static int ulpi_request(struct ulpi_viewport *ulpi_vp, u32 value)
39 {
40 int err;
41
42 writel(value, ulpi_vp->viewport_addr);
43
44 err = ulpi_wait(ulpi_vp, OMAP_ULPI_START);
45 if (err)
46 debug("ULPI request timed out\n");
47
48 return err;
49 }
50
ulpi_write(struct ulpi_viewport * ulpi_vp,u8 * reg,u32 value)51 int ulpi_write(struct ulpi_viewport *ulpi_vp, u8 *reg, u32 value)
52 {
53 u32 val = OMAP_ULPI_START | (((ulpi_vp->port_num + 1) & 0xf) << 24) |
54 OMAP_ULPI_WR_OPSEL | ((u32)reg << 16) | (value & 0xff);
55
56 return ulpi_request(ulpi_vp, val);
57 }
58
ulpi_read(struct ulpi_viewport * ulpi_vp,u8 * reg)59 u32 ulpi_read(struct ulpi_viewport *ulpi_vp, u8 *reg)
60 {
61 int err;
62 u32 val = OMAP_ULPI_START | (((ulpi_vp->port_num + 1) & 0xf) << 24) |
63 OMAP_ULPI_RD_OPSEL | ((u32)reg << 16);
64
65 err = ulpi_request(ulpi_vp, val);
66 if (err)
67 return err;
68
69 return readl(ulpi_vp->viewport_addr) & 0xff;
70 }
71