blob: 72a06de910681c093ec07641a84d43baca3643c0 [file] [log] [blame]
Jana Rapavaa5235952011-12-05 11:07:00 +02001/*
2 * Copyright (C) 2011 Jana Rapava <fermata7@gmail.com>
3 * Copyright (C) 2011 CompuLab, Ltd. <www.compulab.co.il>
4 *
5 * Authors: Jana Rapava <fermata7@gmail.com>
6 * Igor Grinberg <grinberg@compulab.co.il>
7 *
8 * Based on:
9 * linux/drivers/usb/otg/ulpi_viewport.c
10 *
11 * Original Copyright follow:
12 * Copyright (C) 2011 Google, Inc.
13 *
Tom Rinie2378802016-01-14 22:05:13 -050014 * SPDX-License-Identifier: GPL-2.0
Jana Rapavaa5235952011-12-05 11:07:00 +020015 */
16
17#include <common.h>
18#include <asm/io.h>
19#include <usb/ulpi.h>
20
21/* ULPI viewport control bits */
22#define ULPI_SS (1 << 27)
23#define ULPI_RWCTRL (1 << 29)
24#define ULPI_RWRUN (1 << 30)
25#define ULPI_WU (1 << 31)
26
27/*
28 * Wait for the ULPI request to complete
29 *
30 * @ulpi_viewport - the address of the viewport
31 * @mask - expected value to wait for
32 *
33 * returns 0 on mask match, ULPI_ERROR on time out.
34 */
Govindraj.R6645d9e2012-02-06 03:55:31 +000035static int ulpi_wait(struct ulpi_viewport *ulpi_vp, u32 mask)
Jana Rapavaa5235952011-12-05 11:07:00 +020036{
37 int timeout = CONFIG_USB_ULPI_TIMEOUT;
38
39 /* Wait for the bits in mask to become zero. */
40 while (--timeout) {
Govindraj.R6645d9e2012-02-06 03:55:31 +000041 if ((readl(ulpi_vp->viewport_addr) & mask) == 0)
Jana Rapavaa5235952011-12-05 11:07:00 +020042 return 0;
43
44 udelay(1);
45 }
46
47 return ULPI_ERROR;
48}
49
50/*
51 * Wake the ULPI PHY up for communication
52 *
53 * returns 0 on success.
54 */
Govindraj.R6645d9e2012-02-06 03:55:31 +000055static int ulpi_wakeup(struct ulpi_viewport *ulpi_vp)
Jana Rapavaa5235952011-12-05 11:07:00 +020056{
57 int err;
58
Govindraj.R6645d9e2012-02-06 03:55:31 +000059 if (readl(ulpi_vp->viewport_addr) & ULPI_SS)
Jana Rapavaa5235952011-12-05 11:07:00 +020060 return 0; /* already awake */
61
Govindraj.R6645d9e2012-02-06 03:55:31 +000062 writel(ULPI_WU, ulpi_vp->viewport_addr);
Jana Rapavaa5235952011-12-05 11:07:00 +020063
Govindraj.R6645d9e2012-02-06 03:55:31 +000064 err = ulpi_wait(ulpi_vp, ULPI_WU);
Jana Rapavaa5235952011-12-05 11:07:00 +020065 if (err)
66 printf("ULPI wakeup timed out\n");
67
68 return err;
69}
70
71/*
72 * Issue a ULPI read/write request
73 *
74 * @value - the ULPI request
75 */
Govindraj.R6645d9e2012-02-06 03:55:31 +000076static int ulpi_request(struct ulpi_viewport *ulpi_vp, u32 value)
Jana Rapavaa5235952011-12-05 11:07:00 +020077{
78 int err;
79
Govindraj.R6645d9e2012-02-06 03:55:31 +000080 err = ulpi_wakeup(ulpi_vp);
Jana Rapavaa5235952011-12-05 11:07:00 +020081 if (err)
82 return err;
83
Govindraj.R6645d9e2012-02-06 03:55:31 +000084 writel(value, ulpi_vp->viewport_addr);
Jana Rapavaa5235952011-12-05 11:07:00 +020085
Govindraj.R6645d9e2012-02-06 03:55:31 +000086 err = ulpi_wait(ulpi_vp, ULPI_RWRUN);
Jana Rapavaa5235952011-12-05 11:07:00 +020087 if (err)
88 printf("ULPI request timed out\n");
89
90 return err;
91}
92
Govindraj.R6645d9e2012-02-06 03:55:31 +000093int ulpi_write(struct ulpi_viewport *ulpi_vp, u8 *reg, u32 value)
Jana Rapavaa5235952011-12-05 11:07:00 +020094{
95 u32 val = ULPI_RWRUN | ULPI_RWCTRL | ((u32)reg << 16) | (value & 0xff);
96
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;
Jana Rapavaa5235952011-12-05 11:07:00 +0200104 u32 val = ULPI_RWRUN | ((u32)reg << 16);
105
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}