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