Tom Rini | 10e4779 | 2018-05-06 17:58:06 -0400 | [diff] [blame] | 1 | // SPDX-License-Identifier: GPL-2.0+ |
Xu Ziyuan | 1ecf3e4 | 2016-07-14 14:52:32 +0800 | [diff] [blame] | 2 | /* |
| 3 | * Copyright 2016 Rockchip Electronics Co., Ltd |
Xu Ziyuan | 1ecf3e4 | 2016-07-14 14:52:32 +0800 | [diff] [blame] | 4 | */ |
| 5 | |
| 6 | #include <common.h> |
Simon Glass | f11478f | 2019-12-28 10:45:07 -0700 | [diff] [blame] | 7 | #include <hang.h> |
Simon Glass | 0f2af88 | 2020-05-10 11:40:05 -0600 | [diff] [blame] | 8 | #include <log.h> |
Xu Ziyuan | 1ecf3e4 | 2016-07-14 14:52:32 +0800 | [diff] [blame] | 9 | #include <asm/io.h> |
Simon Glass | 4dcacfc | 2020-05-10 11:40:13 -0600 | [diff] [blame] | 10 | #include <linux/bitops.h> |
Simon Glass | dbd7954 | 2020-05-10 11:40:11 -0600 | [diff] [blame] | 11 | #include <linux/delay.h> |
Xu Ziyuan | 1ecf3e4 | 2016-07-14 14:52:32 +0800 | [diff] [blame] | 12 | |
| 13 | #include "../gadget/dwc2_udc_otg_priv.h" |
| 14 | |
| 15 | DECLARE_GLOBAL_DATA_PTR; |
| 16 | |
| 17 | #define BIT_WRITEABLE_SHIFT 16 |
| 18 | |
| 19 | struct usb2phy_reg { |
| 20 | unsigned int offset; |
| 21 | unsigned int bitend; |
| 22 | unsigned int bitstart; |
| 23 | unsigned int disable; |
| 24 | unsigned int enable; |
| 25 | }; |
| 26 | |
| 27 | /** |
| 28 | * struct rockchip_usb2_phy_cfg: usb-phy port configuration |
| 29 | * @port_reset: usb otg per-port reset register |
| 30 | * @soft_con: software control usb otg register |
| 31 | * @suspend: phy suspend register |
| 32 | */ |
| 33 | struct rockchip_usb2_phy_cfg { |
| 34 | struct usb2phy_reg port_reset; |
| 35 | struct usb2phy_reg soft_con; |
| 36 | struct usb2phy_reg suspend; |
| 37 | }; |
| 38 | |
| 39 | struct rockchip_usb2_phy_dt_id { |
| 40 | char compatible[128]; |
| 41 | const void *data; |
| 42 | }; |
| 43 | |
| 44 | static const struct rockchip_usb2_phy_cfg rk3288_pdata = { |
| 45 | .port_reset = {0x00, 12, 12, 0, 1}, |
| 46 | .soft_con = {0x08, 2, 2, 0, 1}, |
| 47 | .suspend = {0x0c, 5, 0, 0x01, 0x2A}, |
| 48 | }; |
| 49 | |
| 50 | static struct rockchip_usb2_phy_dt_id rockchip_usb2_phy_dt_ids[] = { |
| 51 | { .compatible = "rockchip,rk3288-usb-phy", .data = &rk3288_pdata }, |
| 52 | {} |
| 53 | }; |
| 54 | |
| 55 | static void property_enable(struct dwc2_plat_otg_data *pdata, |
| 56 | const struct usb2phy_reg *reg, bool en) |
| 57 | { |
| 58 | unsigned int val, mask, tmp; |
| 59 | |
| 60 | tmp = en ? reg->enable : reg->disable; |
| 61 | mask = GENMASK(reg->bitend, reg->bitstart); |
| 62 | val = (tmp << reg->bitstart) | (mask << BIT_WRITEABLE_SHIFT); |
| 63 | |
| 64 | writel(val, pdata->regs_phy + reg->offset); |
| 65 | } |
| 66 | |
| 67 | |
| 68 | void otg_phy_init(struct dwc2_udc *dev) |
| 69 | { |
| 70 | struct dwc2_plat_otg_data *pdata = dev->pdata; |
| 71 | struct rockchip_usb2_phy_cfg *phy_cfg = NULL; |
| 72 | struct rockchip_usb2_phy_dt_id *of_id; |
| 73 | int i; |
| 74 | |
| 75 | for (i = 0; i < ARRAY_SIZE(rockchip_usb2_phy_dt_ids); i++) { |
| 76 | of_id = &rockchip_usb2_phy_dt_ids[i]; |
Kever Yang | 45bda03 | 2019-10-16 17:13:31 +0800 | [diff] [blame] | 77 | if (ofnode_device_is_compatible(pdata->phy_of_node, |
| 78 | of_id->compatible)){ |
Xu Ziyuan | 1ecf3e4 | 2016-07-14 14:52:32 +0800 | [diff] [blame] | 79 | phy_cfg = (struct rockchip_usb2_phy_cfg *)of_id->data; |
| 80 | break; |
| 81 | } |
| 82 | } |
| 83 | if (!phy_cfg) { |
| 84 | debug("Can't find device platform data\n"); |
| 85 | |
| 86 | hang(); |
| 87 | return; |
| 88 | } |
| 89 | pdata->priv = phy_cfg; |
| 90 | /* disable software control */ |
| 91 | property_enable(pdata, &phy_cfg->soft_con, false); |
| 92 | |
| 93 | /* reset otg port */ |
| 94 | property_enable(pdata, &phy_cfg->port_reset, true); |
| 95 | mdelay(1); |
| 96 | property_enable(pdata, &phy_cfg->port_reset, false); |
| 97 | udelay(1); |
| 98 | } |
| 99 | |
| 100 | void otg_phy_off(struct dwc2_udc *dev) |
| 101 | { |
| 102 | struct dwc2_plat_otg_data *pdata = dev->pdata; |
| 103 | struct rockchip_usb2_phy_cfg *phy_cfg = pdata->priv; |
| 104 | |
| 105 | /* enable software control */ |
| 106 | property_enable(pdata, &phy_cfg->soft_con, true); |
| 107 | /* enter suspend */ |
| 108 | property_enable(pdata, &phy_cfg->suspend, true); |
| 109 | } |