Tom Rini | 83d290c | 2018-05-06 17:58:06 -0400 | [diff] [blame] | 1 | // SPDX-License-Identifier: GPL-2.0 |
Jana Rapava | f93022c | 2011-12-05 11:07:00 +0200 | [diff] [blame] | 2 | /* |
| 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 Rapava | f93022c | 2011-12-05 11:07:00 +0200 | [diff] [blame] | 14 | */ |
| 15 | |
| 16 | #include <common.h> |
| 17 | #include <asm/io.h> |
Simon Glass | c05ed00 | 2020-05-10 11:40:11 -0600 | [diff] [blame^] | 18 | #include <linux/delay.h> |
Jana Rapava | f93022c | 2011-12-05 11:07:00 +0200 | [diff] [blame] | 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.R | 3e6e809 | 2012-02-06 03:55:31 +0000 | [diff] [blame] | 35 | static int ulpi_wait(struct ulpi_viewport *ulpi_vp, u32 mask) |
Jana Rapava | f93022c | 2011-12-05 11:07:00 +0200 | [diff] [blame] | 36 | { |
| 37 | int timeout = CONFIG_USB_ULPI_TIMEOUT; |
| 38 | |
| 39 | /* Wait for the bits in mask to become zero. */ |
| 40 | while (--timeout) { |
Govindraj.R | 3e6e809 | 2012-02-06 03:55:31 +0000 | [diff] [blame] | 41 | if ((readl(ulpi_vp->viewport_addr) & mask) == 0) |
Jana Rapava | f93022c | 2011-12-05 11:07:00 +0200 | [diff] [blame] | 42 | 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.R | 3e6e809 | 2012-02-06 03:55:31 +0000 | [diff] [blame] | 55 | static int ulpi_wakeup(struct ulpi_viewport *ulpi_vp) |
Jana Rapava | f93022c | 2011-12-05 11:07:00 +0200 | [diff] [blame] | 56 | { |
| 57 | int err; |
| 58 | |
Govindraj.R | 3e6e809 | 2012-02-06 03:55:31 +0000 | [diff] [blame] | 59 | if (readl(ulpi_vp->viewport_addr) & ULPI_SS) |
Jana Rapava | f93022c | 2011-12-05 11:07:00 +0200 | [diff] [blame] | 60 | return 0; /* already awake */ |
| 61 | |
Govindraj.R | 3e6e809 | 2012-02-06 03:55:31 +0000 | [diff] [blame] | 62 | writel(ULPI_WU, ulpi_vp->viewport_addr); |
Jana Rapava | f93022c | 2011-12-05 11:07:00 +0200 | [diff] [blame] | 63 | |
Govindraj.R | 3e6e809 | 2012-02-06 03:55:31 +0000 | [diff] [blame] | 64 | err = ulpi_wait(ulpi_vp, ULPI_WU); |
Jana Rapava | f93022c | 2011-12-05 11:07:00 +0200 | [diff] [blame] | 65 | 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.R | 3e6e809 | 2012-02-06 03:55:31 +0000 | [diff] [blame] | 76 | static int ulpi_request(struct ulpi_viewport *ulpi_vp, u32 value) |
Jana Rapava | f93022c | 2011-12-05 11:07:00 +0200 | [diff] [blame] | 77 | { |
| 78 | int err; |
| 79 | |
Govindraj.R | 3e6e809 | 2012-02-06 03:55:31 +0000 | [diff] [blame] | 80 | err = ulpi_wakeup(ulpi_vp); |
Jana Rapava | f93022c | 2011-12-05 11:07:00 +0200 | [diff] [blame] | 81 | if (err) |
| 82 | return err; |
| 83 | |
Govindraj.R | 3e6e809 | 2012-02-06 03:55:31 +0000 | [diff] [blame] | 84 | writel(value, ulpi_vp->viewport_addr); |
Jana Rapava | f93022c | 2011-12-05 11:07:00 +0200 | [diff] [blame] | 85 | |
Govindraj.R | 3e6e809 | 2012-02-06 03:55:31 +0000 | [diff] [blame] | 86 | err = ulpi_wait(ulpi_vp, ULPI_RWRUN); |
Jana Rapava | f93022c | 2011-12-05 11:07:00 +0200 | [diff] [blame] | 87 | if (err) |
| 88 | printf("ULPI request timed out\n"); |
| 89 | |
| 90 | return err; |
| 91 | } |
| 92 | |
Govindraj.R | 3e6e809 | 2012-02-06 03:55:31 +0000 | [diff] [blame] | 93 | int ulpi_write(struct ulpi_viewport *ulpi_vp, u8 *reg, u32 value) |
Jana Rapava | f93022c | 2011-12-05 11:07:00 +0200 | [diff] [blame] | 94 | { |
Mateusz Kulikowski | d3d844f | 2016-03-31 23:12:21 +0200 | [diff] [blame] | 95 | u32 addr = (uintptr_t)reg & 0xFF; |
| 96 | u32 val = ULPI_RWRUN | ULPI_RWCTRL | addr << 16 | (value & 0xff); |
Jana Rapava | f93022c | 2011-12-05 11:07:00 +0200 | [diff] [blame] | 97 | |
Govindraj.R | 3e6e809 | 2012-02-06 03:55:31 +0000 | [diff] [blame] | 98 | val |= (ulpi_vp->port_num & 0x7) << 24; |
| 99 | return ulpi_request(ulpi_vp, val); |
Jana Rapava | f93022c | 2011-12-05 11:07:00 +0200 | [diff] [blame] | 100 | } |
| 101 | |
Govindraj.R | 3e6e809 | 2012-02-06 03:55:31 +0000 | [diff] [blame] | 102 | u32 ulpi_read(struct ulpi_viewport *ulpi_vp, u8 *reg) |
Jana Rapava | f93022c | 2011-12-05 11:07:00 +0200 | [diff] [blame] | 103 | { |
Igor Grinberg | 4256101 | 2011-12-12 12:08:33 +0200 | [diff] [blame] | 104 | int err; |
Mateusz Kulikowski | d3d844f | 2016-03-31 23:12:21 +0200 | [diff] [blame] | 105 | u32 val = ULPI_RWRUN | ((uintptr_t)reg & 0xFF) << 16; |
Jana Rapava | f93022c | 2011-12-05 11:07:00 +0200 | [diff] [blame] | 106 | |
Govindraj.R | 3e6e809 | 2012-02-06 03:55:31 +0000 | [diff] [blame] | 107 | val |= (ulpi_vp->port_num & 0x7) << 24; |
| 108 | err = ulpi_request(ulpi_vp, val); |
Jana Rapava | f93022c | 2011-12-05 11:07:00 +0200 | [diff] [blame] | 109 | if (err) |
| 110 | return err; |
| 111 | |
Govindraj.R | 3e6e809 | 2012-02-06 03:55:31 +0000 | [diff] [blame] | 112 | return (readl(ulpi_vp->viewport_addr) >> 8) & 0xff; |
Jana Rapava | f93022c | 2011-12-05 11:07:00 +0200 | [diff] [blame] | 113 | } |