blob: a2158c9bb85cba9e9b9c41060381a1221f3c931e [file] [log] [blame]
Tom Rini10e47792018-05-06 17:58:06 -04001// SPDX-License-Identifier: GPL-2.0
Jana Rapavaa5235952011-12-05 11:07:00 +02002/*
3 * Copyright (C) 2011 Jana Rapava <fermata7@gmail.com>
4 * Copyright (C) 2011 CompuLab, Ltd. <www.compulab.co.il>
5 *
6 * Authors: Jana Rapava <fermata7@gmail.com>
7 * Igor Grinberg <grinberg@compulab.co.il>
8 *
9 * Based on:
10 * linux/drivers/usb/otg/ulpi_viewport.c
11 *
12 * Original Copyright follow:
13 * Copyright (C) 2011 Google, Inc.
Jana Rapavaa5235952011-12-05 11:07:00 +020014 */
15
16#include <common.h>
17#include <asm/io.h>
18#include <usb/ulpi.h>
19
20/* ULPI viewport control bits */
21#define ULPI_SS (1 << 27)
22#define ULPI_RWCTRL (1 << 29)
23#define ULPI_RWRUN (1 << 30)
24#define ULPI_WU (1 << 31)
25
26/*
27 * Wait for the ULPI request to complete
28 *
29 * @ulpi_viewport - the address of the viewport
30 * @mask - expected value to wait for
31 *
32 * returns 0 on mask match, ULPI_ERROR on time out.
33 */
Govindraj.R6645d9e2012-02-06 03:55:31 +000034static int ulpi_wait(struct ulpi_viewport *ulpi_vp, u32 mask)
Jana Rapavaa5235952011-12-05 11:07:00 +020035{
36 int timeout = CONFIG_USB_ULPI_TIMEOUT;
37
38 /* Wait for the bits in mask to become zero. */
39 while (--timeout) {
Govindraj.R6645d9e2012-02-06 03:55:31 +000040 if ((readl(ulpi_vp->viewport_addr) & mask) == 0)
Jana Rapavaa5235952011-12-05 11:07:00 +020041 return 0;
42
43 udelay(1);
44 }
45
46 return ULPI_ERROR;
47}
48
49/*
50 * Wake the ULPI PHY up for communication
51 *
52 * returns 0 on success.
53 */
Govindraj.R6645d9e2012-02-06 03:55:31 +000054static int ulpi_wakeup(struct ulpi_viewport *ulpi_vp)
Jana Rapavaa5235952011-12-05 11:07:00 +020055{
56 int err;
57
Govindraj.R6645d9e2012-02-06 03:55:31 +000058 if (readl(ulpi_vp->viewport_addr) & ULPI_SS)
Jana Rapavaa5235952011-12-05 11:07:00 +020059 return 0; /* already awake */
60
Govindraj.R6645d9e2012-02-06 03:55:31 +000061 writel(ULPI_WU, ulpi_vp->viewport_addr);
Jana Rapavaa5235952011-12-05 11:07:00 +020062
Govindraj.R6645d9e2012-02-06 03:55:31 +000063 err = ulpi_wait(ulpi_vp, ULPI_WU);
Jana Rapavaa5235952011-12-05 11:07:00 +020064 if (err)
65 printf("ULPI wakeup timed out\n");
66
67 return err;
68}
69
70/*
71 * Issue a ULPI read/write request
72 *
73 * @value - the ULPI request
74 */
Govindraj.R6645d9e2012-02-06 03:55:31 +000075static int ulpi_request(struct ulpi_viewport *ulpi_vp, u32 value)
Jana Rapavaa5235952011-12-05 11:07:00 +020076{
77 int err;
78
Govindraj.R6645d9e2012-02-06 03:55:31 +000079 err = ulpi_wakeup(ulpi_vp);
Jana Rapavaa5235952011-12-05 11:07:00 +020080 if (err)
81 return err;
82
Govindraj.R6645d9e2012-02-06 03:55:31 +000083 writel(value, ulpi_vp->viewport_addr);
Jana Rapavaa5235952011-12-05 11:07:00 +020084
Govindraj.R6645d9e2012-02-06 03:55:31 +000085 err = ulpi_wait(ulpi_vp, ULPI_RWRUN);
Jana Rapavaa5235952011-12-05 11:07:00 +020086 if (err)
87 printf("ULPI request timed out\n");
88
89 return err;
90}
91
Govindraj.R6645d9e2012-02-06 03:55:31 +000092int ulpi_write(struct ulpi_viewport *ulpi_vp, u8 *reg, u32 value)
Jana Rapavaa5235952011-12-05 11:07:00 +020093{
Mateusz Kulikowski4374f332016-03-31 23:12:21 +020094 u32 addr = (uintptr_t)reg & 0xFF;
95 u32 val = ULPI_RWRUN | ULPI_RWCTRL | addr << 16 | (value & 0xff);
Jana Rapavaa5235952011-12-05 11:07:00 +020096
Govindraj.R6645d9e2012-02-06 03:55:31 +000097 val |= (ulpi_vp->port_num & 0x7) << 24;
98 return ulpi_request(ulpi_vp, val);
Jana Rapavaa5235952011-12-05 11:07:00 +020099}
100
Govindraj.R6645d9e2012-02-06 03:55:31 +0000101u32 ulpi_read(struct ulpi_viewport *ulpi_vp, u8 *reg)
Jana Rapavaa5235952011-12-05 11:07:00 +0200102{
Igor Grinberg7b4d7682011-12-12 12:08:33 +0200103 int err;
Mateusz Kulikowski4374f332016-03-31 23:12:21 +0200104 u32 val = ULPI_RWRUN | ((uintptr_t)reg & 0xFF) << 16;
Jana Rapavaa5235952011-12-05 11:07:00 +0200105
Govindraj.R6645d9e2012-02-06 03:55:31 +0000106 val |= (ulpi_vp->port_num & 0x7) << 24;
107 err = ulpi_request(ulpi_vp, val);
Jana Rapavaa5235952011-12-05 11:07:00 +0200108 if (err)
109 return err;
110
Govindraj.R6645d9e2012-02-06 03:55:31 +0000111 return (readl(ulpi_vp->viewport_addr) >> 8) & 0xff;
Jana Rapavaa5235952011-12-05 11:07:00 +0200112}