Vivek Gautam | dc2377d | 2013-03-14 15:59:10 +0530 | [diff] [blame] | 1 | /* linux/drivers/usb/phy/phy-samsung-usb.c |
Praveen Paneri | 337dc3a | 2012-11-23 16:03:06 +0530 | [diff] [blame] | 2 | * |
| 3 | * Copyright (c) 2012 Samsung Electronics Co., Ltd. |
| 4 | * http://www.samsung.com |
| 5 | * |
| 6 | * Author: Praveen Paneri <p.paneri@samsung.com> |
| 7 | * |
Vivek Gautam | dc2377d | 2013-03-14 15:59:10 +0530 | [diff] [blame] | 8 | * Samsung USB-PHY helper driver with common function calls; |
| 9 | * interacts with Samsung USB 2.0 PHY controller driver and later |
| 10 | * with Samsung USB 3.0 PHY driver. |
Praveen Paneri | 337dc3a | 2012-11-23 16:03:06 +0530 | [diff] [blame] | 11 | * |
| 12 | * This program is free software; you can redistribute it and/or modify |
| 13 | * it under the terms of the GNU General Public License version 2 as |
| 14 | * published by the Free Software Foundation. |
| 15 | * |
| 16 | * This program is distributed in the hope that it will be useful, |
| 17 | * but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 18 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 19 | * GNU General Public License for more details. |
| 20 | */ |
| 21 | |
| 22 | #include <linux/module.h> |
| 23 | #include <linux/platform_device.h> |
| 24 | #include <linux/clk.h> |
Vivek Gautam | 8c1b3e1 | 2013-01-22 18:30:41 +0530 | [diff] [blame] | 25 | #include <linux/device.h> |
Praveen Paneri | 337dc3a | 2012-11-23 16:03:06 +0530 | [diff] [blame] | 26 | #include <linux/err.h> |
| 27 | #include <linux/io.h> |
| 28 | #include <linux/of.h> |
Vivek Gautam | 69f0946 | 2013-01-15 11:40:25 +0530 | [diff] [blame] | 29 | #include <linux/of_address.h> |
Vivek Gautam | 8c1b3e1 | 2013-01-22 18:30:41 +0530 | [diff] [blame] | 30 | #include <linux/usb/samsung_usb_phy.h> |
Praveen Paneri | 337dc3a | 2012-11-23 16:03:06 +0530 | [diff] [blame] | 31 | |
Vivek Gautam | dc2377d | 2013-03-14 15:59:10 +0530 | [diff] [blame] | 32 | #include "phy-samsung-usb.h" |
Praveen Paneri | 337dc3a | 2012-11-23 16:03:06 +0530 | [diff] [blame] | 33 | |
Vivek Gautam | dc2377d | 2013-03-14 15:59:10 +0530 | [diff] [blame] | 34 | int samsung_usbphy_parse_dt(struct samsung_usbphy *sphy) |
Vivek Gautam | 69f0946 | 2013-01-15 11:40:25 +0530 | [diff] [blame] | 35 | { |
| 36 | struct device_node *usbphy_sys; |
| 37 | |
| 38 | /* Getting node for system controller interface for usb-phy */ |
| 39 | usbphy_sys = of_get_child_by_name(sphy->dev->of_node, "usbphy-sys"); |
| 40 | if (!usbphy_sys) { |
| 41 | dev_err(sphy->dev, "No sys-controller interface for usb-phy\n"); |
| 42 | return -ENODEV; |
| 43 | } |
| 44 | |
| 45 | sphy->pmuregs = of_iomap(usbphy_sys, 0); |
| 46 | |
Vivek Gautam | 69f0946 | 2013-01-15 11:40:25 +0530 | [diff] [blame] | 47 | if (sphy->pmuregs == NULL) { |
| 48 | dev_err(sphy->dev, "Can't get usb-phy pmu control register\n"); |
Vivek Gautam | 8c1b3e1 | 2013-01-22 18:30:41 +0530 | [diff] [blame] | 49 | goto err0; |
Vivek Gautam | 69f0946 | 2013-01-15 11:40:25 +0530 | [diff] [blame] | 50 | } |
| 51 | |
Vivek Gautam | 8c1b3e1 | 2013-01-22 18:30:41 +0530 | [diff] [blame] | 52 | sphy->sysreg = of_iomap(usbphy_sys, 1); |
| 53 | |
| 54 | /* |
| 55 | * Not returning error code here, since this situation is not fatal. |
| 56 | * Few SoCs may not have this switch available |
| 57 | */ |
| 58 | if (sphy->sysreg == NULL) |
| 59 | dev_warn(sphy->dev, "Can't get usb-phy sysreg cfg register\n"); |
| 60 | |
| 61 | of_node_put(usbphy_sys); |
| 62 | |
Vivek Gautam | 69f0946 | 2013-01-15 11:40:25 +0530 | [diff] [blame] | 63 | return 0; |
Vivek Gautam | 8c1b3e1 | 2013-01-22 18:30:41 +0530 | [diff] [blame] | 64 | |
| 65 | err0: |
| 66 | of_node_put(usbphy_sys); |
| 67 | return -ENXIO; |
Vivek Gautam | 69f0946 | 2013-01-15 11:40:25 +0530 | [diff] [blame] | 68 | } |
Vivek Gautam | dc2377d | 2013-03-14 15:59:10 +0530 | [diff] [blame] | 69 | EXPORT_SYMBOL_GPL(samsung_usbphy_parse_dt); |
Vivek Gautam | 69f0946 | 2013-01-15 11:40:25 +0530 | [diff] [blame] | 70 | |
| 71 | /* |
| 72 | * Set isolation here for phy. |
| 73 | * Here 'on = true' would mean USB PHY block is isolated, hence |
| 74 | * de-activated and vice-versa. |
| 75 | */ |
Tomasz Figa | 3f33907 | 2013-05-16 11:57:09 +0200 | [diff] [blame^] | 76 | void samsung_usbphy_set_isolation_4210(struct samsung_usbphy *sphy, bool on) |
Vivek Gautam | 69f0946 | 2013-01-15 11:40:25 +0530 | [diff] [blame] | 77 | { |
Vivek Gautam | 8c1b3e1 | 2013-01-22 18:30:41 +0530 | [diff] [blame] | 78 | void __iomem *reg = NULL; |
Vivek Gautam | 69f0946 | 2013-01-15 11:40:25 +0530 | [diff] [blame] | 79 | u32 reg_val; |
Vivek Gautam | 8c1b3e1 | 2013-01-22 18:30:41 +0530 | [diff] [blame] | 80 | u32 en_mask = 0; |
Vivek Gautam | 69f0946 | 2013-01-15 11:40:25 +0530 | [diff] [blame] | 81 | |
| 82 | if (!sphy->pmuregs) { |
| 83 | dev_warn(sphy->dev, "Can't set pmu isolation\n"); |
| 84 | return; |
| 85 | } |
| 86 | |
Tomasz Figa | 3f33907 | 2013-05-16 11:57:09 +0200 | [diff] [blame^] | 87 | if (sphy->phy_type == USB_PHY_TYPE_DEVICE) { |
| 88 | reg = sphy->pmuregs + sphy->drv_data->devphy_reg_offset; |
| 89 | en_mask = sphy->drv_data->devphy_en_mask; |
| 90 | } else if (sphy->phy_type == USB_PHY_TYPE_HOST) { |
| 91 | reg = sphy->pmuregs + sphy->drv_data->hostphy_reg_offset; |
| 92 | en_mask = sphy->drv_data->hostphy_en_mask; |
Vivek Gautam | 8c1b3e1 | 2013-01-22 18:30:41 +0530 | [diff] [blame] | 93 | } |
Vivek Gautam | 69f0946 | 2013-01-15 11:40:25 +0530 | [diff] [blame] | 94 | |
| 95 | reg_val = readl(reg); |
| 96 | |
| 97 | if (on) |
| 98 | reg_val &= ~en_mask; |
| 99 | else |
| 100 | reg_val |= en_mask; |
| 101 | |
| 102 | writel(reg_val, reg); |
| 103 | } |
Tomasz Figa | 3f33907 | 2013-05-16 11:57:09 +0200 | [diff] [blame^] | 104 | EXPORT_SYMBOL_GPL(samsung_usbphy_set_isolation_4210); |
Vivek Gautam | 69f0946 | 2013-01-15 11:40:25 +0530 | [diff] [blame] | 105 | |
Praveen Paneri | 337dc3a | 2012-11-23 16:03:06 +0530 | [diff] [blame] | 106 | /* |
Vivek Gautam | 8c1b3e1 | 2013-01-22 18:30:41 +0530 | [diff] [blame] | 107 | * Configure the mode of working of usb-phy here: HOST/DEVICE. |
| 108 | */ |
Vivek Gautam | dc2377d | 2013-03-14 15:59:10 +0530 | [diff] [blame] | 109 | void samsung_usbphy_cfg_sel(struct samsung_usbphy *sphy) |
Vivek Gautam | 8c1b3e1 | 2013-01-22 18:30:41 +0530 | [diff] [blame] | 110 | { |
| 111 | u32 reg; |
| 112 | |
| 113 | if (!sphy->sysreg) { |
| 114 | dev_warn(sphy->dev, "Can't configure specified phy mode\n"); |
| 115 | return; |
| 116 | } |
| 117 | |
| 118 | reg = readl(sphy->sysreg); |
| 119 | |
| 120 | if (sphy->phy_type == USB_PHY_TYPE_DEVICE) |
| 121 | reg &= ~EXYNOS_USB20PHY_CFG_HOST_LINK; |
| 122 | else if (sphy->phy_type == USB_PHY_TYPE_HOST) |
| 123 | reg |= EXYNOS_USB20PHY_CFG_HOST_LINK; |
| 124 | |
| 125 | writel(reg, sphy->sysreg); |
| 126 | } |
Vivek Gautam | dc2377d | 2013-03-14 15:59:10 +0530 | [diff] [blame] | 127 | EXPORT_SYMBOL_GPL(samsung_usbphy_cfg_sel); |
Vivek Gautam | 8c1b3e1 | 2013-01-22 18:30:41 +0530 | [diff] [blame] | 128 | |
| 129 | /* |
| 130 | * PHYs are different for USB Device and USB Host. |
| 131 | * This make sure that correct PHY type is selected before |
| 132 | * any operation on PHY. |
| 133 | */ |
Vivek Gautam | dc2377d | 2013-03-14 15:59:10 +0530 | [diff] [blame] | 134 | int samsung_usbphy_set_type(struct usb_phy *phy, |
Vivek Gautam | 8c1b3e1 | 2013-01-22 18:30:41 +0530 | [diff] [blame] | 135 | enum samsung_usb_phy_type phy_type) |
| 136 | { |
| 137 | struct samsung_usbphy *sphy = phy_to_sphy(phy); |
| 138 | |
| 139 | sphy->phy_type = phy_type; |
| 140 | |
| 141 | return 0; |
| 142 | } |
Vivek Gautam | dc2377d | 2013-03-14 15:59:10 +0530 | [diff] [blame] | 143 | EXPORT_SYMBOL_GPL(samsung_usbphy_set_type); |
Vivek Gautam | 8c1b3e1 | 2013-01-22 18:30:41 +0530 | [diff] [blame] | 144 | |
Tomasz Figa | 0aa823a | 2013-05-16 11:57:08 +0200 | [diff] [blame] | 145 | int samsung_usbphy_rate_to_clksel_64xx(struct samsung_usbphy *sphy, |
| 146 | unsigned long rate) |
| 147 | { |
| 148 | unsigned int clksel; |
| 149 | |
| 150 | switch (rate) { |
| 151 | case 12 * MHZ: |
| 152 | clksel = PHYCLK_CLKSEL_12M; |
| 153 | break; |
| 154 | case 24 * MHZ: |
| 155 | clksel = PHYCLK_CLKSEL_24M; |
| 156 | break; |
| 157 | case 48 * MHZ: |
| 158 | clksel = PHYCLK_CLKSEL_48M; |
| 159 | break; |
| 160 | default: |
| 161 | dev_err(sphy->dev, |
| 162 | "Invalid reference clock frequency: %lu\n", rate); |
| 163 | return -EINVAL; |
| 164 | } |
| 165 | |
| 166 | return clksel; |
| 167 | } |
| 168 | EXPORT_SYMBOL_GPL(samsung_usbphy_rate_to_clksel_64xx); |
| 169 | |
| 170 | int samsung_usbphy_rate_to_clksel_4x12(struct samsung_usbphy *sphy, |
| 171 | unsigned long rate) |
| 172 | { |
| 173 | unsigned int clksel; |
| 174 | |
| 175 | switch (rate) { |
| 176 | case 9600 * KHZ: |
| 177 | clksel = FSEL_CLKSEL_9600K; |
| 178 | break; |
| 179 | case 10 * MHZ: |
| 180 | clksel = FSEL_CLKSEL_10M; |
| 181 | break; |
| 182 | case 12 * MHZ: |
| 183 | clksel = FSEL_CLKSEL_12M; |
| 184 | break; |
| 185 | case 19200 * KHZ: |
| 186 | clksel = FSEL_CLKSEL_19200K; |
| 187 | break; |
| 188 | case 20 * MHZ: |
| 189 | clksel = FSEL_CLKSEL_20M; |
| 190 | break; |
| 191 | case 24 * MHZ: |
| 192 | clksel = FSEL_CLKSEL_24M; |
| 193 | break; |
| 194 | case 50 * MHZ: |
| 195 | clksel = FSEL_CLKSEL_50M; |
| 196 | break; |
| 197 | default: |
| 198 | dev_err(sphy->dev, |
| 199 | "Invalid reference clock frequency: %lu\n", rate); |
| 200 | return -EINVAL; |
| 201 | } |
| 202 | |
| 203 | return clksel; |
| 204 | } |
| 205 | EXPORT_SYMBOL_GPL(samsung_usbphy_rate_to_clksel_4x12); |
| 206 | |
Vivek Gautam | 8c1b3e1 | 2013-01-22 18:30:41 +0530 | [diff] [blame] | 207 | /* |
Praveen Paneri | 337dc3a | 2012-11-23 16:03:06 +0530 | [diff] [blame] | 208 | * Returns reference clock frequency selection value |
| 209 | */ |
Vivek Gautam | dc2377d | 2013-03-14 15:59:10 +0530 | [diff] [blame] | 210 | int samsung_usbphy_get_refclk_freq(struct samsung_usbphy *sphy) |
Praveen Paneri | 337dc3a | 2012-11-23 16:03:06 +0530 | [diff] [blame] | 211 | { |
| 212 | struct clk *ref_clk; |
Tomasz Figa | 0aa823a | 2013-05-16 11:57:08 +0200 | [diff] [blame] | 213 | unsigned long rate; |
| 214 | int refclk_freq; |
Praveen Paneri | 337dc3a | 2012-11-23 16:03:06 +0530 | [diff] [blame] | 215 | |
Vivek Gautam | 8c1b3e1 | 2013-01-22 18:30:41 +0530 | [diff] [blame] | 216 | /* |
| 217 | * In exynos5250 USB host and device PHY use |
| 218 | * external crystal clock XXTI |
| 219 | */ |
| 220 | if (sphy->drv_data->cpu_type == TYPE_EXYNOS5250) |
Tomasz Figa | 87331b0 | 2013-05-16 11:57:07 +0200 | [diff] [blame] | 221 | ref_clk = clk_get(sphy->dev, "ext_xtal"); |
Vivek Gautam | 8c1b3e1 | 2013-01-22 18:30:41 +0530 | [diff] [blame] | 222 | else |
Tomasz Figa | 87331b0 | 2013-05-16 11:57:07 +0200 | [diff] [blame] | 223 | ref_clk = clk_get(sphy->dev, "xusbxti"); |
Praveen Paneri | 337dc3a | 2012-11-23 16:03:06 +0530 | [diff] [blame] | 224 | if (IS_ERR(ref_clk)) { |
| 225 | dev_err(sphy->dev, "Failed to get reference clock\n"); |
| 226 | return PTR_ERR(ref_clk); |
| 227 | } |
| 228 | |
Tomasz Figa | 0aa823a | 2013-05-16 11:57:08 +0200 | [diff] [blame] | 229 | rate = clk_get_rate(ref_clk); |
| 230 | refclk_freq = sphy->drv_data->rate_to_clksel(sphy, rate); |
| 231 | |
Praveen Paneri | 337dc3a | 2012-11-23 16:03:06 +0530 | [diff] [blame] | 232 | clk_put(ref_clk); |
| 233 | |
| 234 | return refclk_freq; |
| 235 | } |
Vivek Gautam | dc2377d | 2013-03-14 15:59:10 +0530 | [diff] [blame] | 236 | EXPORT_SYMBOL_GPL(samsung_usbphy_get_refclk_freq); |