1 /* 2 * SAMSUNG EXYNOS USB HOST OHCI Controller 3 * 4 * Copyright (C) 2011 Samsung Electronics Co.Ltd 5 * Author: Jingoo Han <jg1.han@samsung.com> 6 * 7 * This program is free software; you can redistribute it and/or modify it 8 * under the terms of the GNU General Public License as published by the 9 * Free Software Foundation; either version 2 of the License, or (at your 10 * option) any later version. 11 * 12 */ 13 14 #include <linux/clk.h> 15 #include <linux/dma-mapping.h> 16 #include <linux/io.h> 17 #include <linux/kernel.h> 18 #include <linux/module.h> 19 #include <linux/of.h> 20 #include <linux/platform_device.h> 21 #include <linux/phy/phy.h> 22 #include <linux/usb/phy.h> 23 #include <linux/usb/samsung_usb_phy.h> 24 #include <linux/usb.h> 25 #include <linux/usb/hcd.h> 26 #include <linux/usb/otg.h> 27 28 #include "ohci.h" 29 30 #define DRIVER_DESC "OHCI EXYNOS driver" 31 32 static const char hcd_name[] = "ohci-exynos"; 33 static struct hc_driver __read_mostly exynos_ohci_hc_driver; 34 35 #define to_exynos_ohci(hcd) (struct exynos_ohci_hcd *)(hcd_to_ohci(hcd)->priv) 36 37 #define PHY_NUMBER 3 38 39 struct exynos_ohci_hcd { 40 struct clk *clk; 41 struct usb_phy *phy; 42 struct usb_otg *otg; 43 struct phy *phy_g[PHY_NUMBER]; 44 }; 45 46 static int exynos_ohci_get_phy(struct device *dev, 47 struct exynos_ohci_hcd *exynos_ohci) 48 { 49 struct device_node *child; 50 struct phy *phy; 51 int phy_number; 52 int ret = 0; 53 54 exynos_ohci->phy = devm_usb_get_phy(dev, USB_PHY_TYPE_USB2); 55 if (IS_ERR(exynos_ohci->phy)) { 56 ret = PTR_ERR(exynos_ohci->phy); 57 if (ret != -ENXIO && ret != -ENODEV) { 58 dev_err(dev, "no usb2 phy configured\n"); 59 return ret; 60 } 61 dev_dbg(dev, "Failed to get usb2 phy\n"); 62 } else { 63 exynos_ohci->otg = exynos_ohci->phy->otg; 64 } 65 66 /* 67 * Getting generic phy: 68 * We are keeping both types of phys as a part of transiting OHCI 69 * to generic phy framework, so as to maintain backward compatibilty 70 * with old DTB. 71 * If there are existing devices using DTB files built from them, 72 * to remove the support for old bindings in this driver, 73 * we need to make sure that such devices have their DTBs 74 * updated to ones built from new DTS. 75 */ 76 for_each_available_child_of_node(dev->of_node, child) { 77 ret = of_property_read_u32(child, "reg", &phy_number); 78 if (ret) { 79 dev_err(dev, "Failed to parse device tree\n"); 80 of_node_put(child); 81 return ret; 82 } 83 84 if (phy_number >= PHY_NUMBER) { 85 dev_err(dev, "Invalid number of PHYs\n"); 86 of_node_put(child); 87 return -EINVAL; 88 } 89 90 phy = devm_of_phy_get(dev, child, 0); 91 of_node_put(child); 92 if (IS_ERR(phy)) { 93 ret = PTR_ERR(phy); 94 if (ret != -ENOSYS && ret != -ENODEV) { 95 dev_err(dev, "no usb2 phy configured\n"); 96 return ret; 97 } 98 dev_dbg(dev, "Failed to get usb2 phy\n"); 99 } 100 exynos_ohci->phy_g[phy_number] = phy; 101 } 102 103 return ret; 104 } 105 106 static int exynos_ohci_phy_enable(struct device *dev) 107 { 108 struct usb_hcd *hcd = dev_get_drvdata(dev); 109 struct exynos_ohci_hcd *exynos_ohci = to_exynos_ohci(hcd); 110 int i; 111 int ret = 0; 112 113 if (!IS_ERR(exynos_ohci->phy)) 114 return usb_phy_init(exynos_ohci->phy); 115 116 for (i = 0; ret == 0 && i < PHY_NUMBER; i++) 117 if (!IS_ERR(exynos_ohci->phy_g[i])) 118 ret = phy_power_on(exynos_ohci->phy_g[i]); 119 if (ret) 120 for (i--; i >= 0; i--) 121 if (!IS_ERR(exynos_ohci->phy_g[i])) 122 phy_power_off(exynos_ohci->phy_g[i]); 123 124 return ret; 125 } 126 127 static void exynos_ohci_phy_disable(struct device *dev) 128 { 129 struct usb_hcd *hcd = dev_get_drvdata(dev); 130 struct exynos_ohci_hcd *exynos_ohci = to_exynos_ohci(hcd); 131 int i; 132 133 if (!IS_ERR(exynos_ohci->phy)) { 134 usb_phy_shutdown(exynos_ohci->phy); 135 return; 136 } 137 138 for (i = 0; i < PHY_NUMBER; i++) 139 if (!IS_ERR(exynos_ohci->phy_g[i])) 140 phy_power_off(exynos_ohci->phy_g[i]); 141 } 142 143 static int exynos_ohci_probe(struct platform_device *pdev) 144 { 145 struct exynos_ohci_hcd *exynos_ohci; 146 struct usb_hcd *hcd; 147 struct resource *res; 148 int irq; 149 int err; 150 151 /* 152 * Right now device-tree probed devices don't get dma_mask set. 153 * Since shared usb code relies on it, set it here for now. 154 * Once we move to full device tree support this will vanish off. 155 */ 156 err = dma_coerce_mask_and_coherent(&pdev->dev, DMA_BIT_MASK(32)); 157 if (err) 158 return err; 159 160 hcd = usb_create_hcd(&exynos_ohci_hc_driver, 161 &pdev->dev, dev_name(&pdev->dev)); 162 if (!hcd) { 163 dev_err(&pdev->dev, "Unable to create HCD\n"); 164 return -ENOMEM; 165 } 166 167 exynos_ohci = to_exynos_ohci(hcd); 168 169 if (of_device_is_compatible(pdev->dev.of_node, 170 "samsung,exynos5440-ohci")) 171 goto skip_phy; 172 173 err = exynos_ohci_get_phy(&pdev->dev, exynos_ohci); 174 if (err) 175 goto fail_clk; 176 177 skip_phy: 178 exynos_ohci->clk = devm_clk_get(&pdev->dev, "usbhost"); 179 180 if (IS_ERR(exynos_ohci->clk)) { 181 dev_err(&pdev->dev, "Failed to get usbhost clock\n"); 182 err = PTR_ERR(exynos_ohci->clk); 183 goto fail_clk; 184 } 185 186 err = clk_prepare_enable(exynos_ohci->clk); 187 if (err) 188 goto fail_clk; 189 190 res = platform_get_resource(pdev, IORESOURCE_MEM, 0); 191 if (!res) { 192 dev_err(&pdev->dev, "Failed to get I/O memory\n"); 193 err = -ENXIO; 194 goto fail_io; 195 } 196 197 hcd->rsrc_start = res->start; 198 hcd->rsrc_len = resource_size(res); 199 hcd->regs = devm_ioremap_resource(&pdev->dev, res); 200 if (IS_ERR(hcd->regs)) { 201 err = PTR_ERR(hcd->regs); 202 goto fail_io; 203 } 204 205 irq = platform_get_irq(pdev, 0); 206 if (!irq) { 207 dev_err(&pdev->dev, "Failed to get IRQ\n"); 208 err = -ENODEV; 209 goto fail_io; 210 } 211 212 if (exynos_ohci->otg) 213 exynos_ohci->otg->set_host(exynos_ohci->otg, &hcd->self); 214 215 platform_set_drvdata(pdev, hcd); 216 217 err = exynos_ohci_phy_enable(&pdev->dev); 218 if (err) { 219 dev_err(&pdev->dev, "Failed to enable USB phy\n"); 220 goto fail_io; 221 } 222 223 err = usb_add_hcd(hcd, irq, IRQF_SHARED); 224 if (err) { 225 dev_err(&pdev->dev, "Failed to add USB HCD\n"); 226 goto fail_add_hcd; 227 } 228 device_wakeup_enable(hcd->self.controller); 229 return 0; 230 231 fail_add_hcd: 232 exynos_ohci_phy_disable(&pdev->dev); 233 fail_io: 234 clk_disable_unprepare(exynos_ohci->clk); 235 fail_clk: 236 usb_put_hcd(hcd); 237 return err; 238 } 239 240 static int exynos_ohci_remove(struct platform_device *pdev) 241 { 242 struct usb_hcd *hcd = platform_get_drvdata(pdev); 243 struct exynos_ohci_hcd *exynos_ohci = to_exynos_ohci(hcd); 244 245 usb_remove_hcd(hcd); 246 247 if (exynos_ohci->otg) 248 exynos_ohci->otg->set_host(exynos_ohci->otg, &hcd->self); 249 250 exynos_ohci_phy_disable(&pdev->dev); 251 252 clk_disable_unprepare(exynos_ohci->clk); 253 254 usb_put_hcd(hcd); 255 256 return 0; 257 } 258 259 static void exynos_ohci_shutdown(struct platform_device *pdev) 260 { 261 struct usb_hcd *hcd = platform_get_drvdata(pdev); 262 263 if (hcd->driver->shutdown) 264 hcd->driver->shutdown(hcd); 265 } 266 267 #ifdef CONFIG_PM 268 static int exynos_ohci_suspend(struct device *dev) 269 { 270 struct usb_hcd *hcd = dev_get_drvdata(dev); 271 struct exynos_ohci_hcd *exynos_ohci = to_exynos_ohci(hcd); 272 bool do_wakeup = device_may_wakeup(dev); 273 int rc = ohci_suspend(hcd, do_wakeup); 274 275 if (rc) 276 return rc; 277 278 if (exynos_ohci->otg) 279 exynos_ohci->otg->set_host(exynos_ohci->otg, &hcd->self); 280 281 exynos_ohci_phy_disable(dev); 282 283 clk_disable_unprepare(exynos_ohci->clk); 284 285 return 0; 286 } 287 288 static int exynos_ohci_resume(struct device *dev) 289 { 290 struct usb_hcd *hcd = dev_get_drvdata(dev); 291 struct exynos_ohci_hcd *exynos_ohci = to_exynos_ohci(hcd); 292 int ret; 293 294 clk_prepare_enable(exynos_ohci->clk); 295 296 if (exynos_ohci->otg) 297 exynos_ohci->otg->set_host(exynos_ohci->otg, &hcd->self); 298 299 ret = exynos_ohci_phy_enable(dev); 300 if (ret) { 301 dev_err(dev, "Failed to enable USB phy\n"); 302 clk_disable_unprepare(exynos_ohci->clk); 303 return ret; 304 } 305 306 ohci_resume(hcd, false); 307 308 return 0; 309 } 310 #else 311 #define exynos_ohci_suspend NULL 312 #define exynos_ohci_resume NULL 313 #endif 314 315 static const struct ohci_driver_overrides exynos_overrides __initconst = { 316 .extra_priv_size = sizeof(struct exynos_ohci_hcd), 317 }; 318 319 static const struct dev_pm_ops exynos_ohci_pm_ops = { 320 .suspend = exynos_ohci_suspend, 321 .resume = exynos_ohci_resume, 322 }; 323 324 #ifdef CONFIG_OF 325 static const struct of_device_id exynos_ohci_match[] = { 326 { .compatible = "samsung,exynos4210-ohci" }, 327 { .compatible = "samsung,exynos5440-ohci" }, 328 {}, 329 }; 330 MODULE_DEVICE_TABLE(of, exynos_ohci_match); 331 #endif 332 333 static struct platform_driver exynos_ohci_driver = { 334 .probe = exynos_ohci_probe, 335 .remove = exynos_ohci_remove, 336 .shutdown = exynos_ohci_shutdown, 337 .driver = { 338 .name = "exynos-ohci", 339 .owner = THIS_MODULE, 340 .pm = &exynos_ohci_pm_ops, 341 .of_match_table = of_match_ptr(exynos_ohci_match), 342 } 343 }; 344 static int __init ohci_exynos_init(void) 345 { 346 if (usb_disabled()) 347 return -ENODEV; 348 349 pr_info("%s: " DRIVER_DESC "\n", hcd_name); 350 ohci_init_driver(&exynos_ohci_hc_driver, &exynos_overrides); 351 return platform_driver_register(&exynos_ohci_driver); 352 } 353 module_init(ohci_exynos_init); 354 355 static void __exit ohci_exynos_cleanup(void) 356 { 357 platform_driver_unregister(&exynos_ohci_driver); 358 } 359 module_exit(ohci_exynos_cleanup); 360 361 MODULE_ALIAS("platform:exynos-ohci"); 362 MODULE_AUTHOR("Jingoo Han <jg1.han@samsung.com>"); 363 MODULE_LICENSE("GPL v2"); 364