Joonyoung Shim | 1bcc5aa | 2011-04-08 14:08:50 +0900 | [diff] [blame] | 1 | /* |
| 2 | * SAMSUNG S5P USB HOST EHCI Controller |
| 3 | * |
| 4 | * Copyright (C) 2011 Samsung Electronics Co.Ltd |
| 5 | * Author: Jingoo Han <jg1.han@samsung.com> |
| 6 | * Author: Joonyoung Shim <jy0922.shim@samsung.com> |
| 7 | * |
| 8 | * This program is free software; you can redistribute it and/or modify it |
| 9 | * under the terms of the GNU General Public License as published by the |
| 10 | * Free Software Foundation; either version 2 of the License, or (at your |
| 11 | * option) any later version. |
| 12 | * |
| 13 | */ |
| 14 | |
| 15 | #include <linux/clk.h> |
Vivek Gautam | 2c026e2 | 2012-07-16 11:25:37 +0530 | [diff] [blame] | 16 | #include <linux/of.h> |
Joonyoung Shim | 1bcc5aa | 2011-04-08 14:08:50 +0900 | [diff] [blame] | 17 | #include <linux/platform_device.h> |
Vivek Gautam | fd81d59 | 2012-07-17 10:10:50 +0530 | [diff] [blame] | 18 | #include <linux/of_gpio.h> |
Arnd Bergmann | 436d42c | 2012-08-24 15:22:12 +0200 | [diff] [blame] | 19 | #include <linux/platform_data/usb-ehci-s5p.h> |
Joonyoung Shim | 1bcc5aa | 2011-04-08 14:08:50 +0900 | [diff] [blame] | 20 | #include <plat/usb-phy.h> |
| 21 | |
Jingoo Han | 88555a6 | 2012-03-05 10:40:14 +0900 | [diff] [blame] | 22 | #define EHCI_INSNREG00(base) (base + 0x90) |
| 23 | #define EHCI_INSNREG00_ENA_INCR16 (0x1 << 25) |
| 24 | #define EHCI_INSNREG00_ENA_INCR8 (0x1 << 24) |
| 25 | #define EHCI_INSNREG00_ENA_INCR4 (0x1 << 23) |
| 26 | #define EHCI_INSNREG00_ENA_INCRX_ALIGN (0x1 << 22) |
| 27 | #define EHCI_INSNREG00_ENABLE_DMA_BURST \ |
| 28 | (EHCI_INSNREG00_ENA_INCR16 | EHCI_INSNREG00_ENA_INCR8 | \ |
| 29 | EHCI_INSNREG00_ENA_INCR4 | EHCI_INSNREG00_ENA_INCRX_ALIGN) |
| 30 | |
Joonyoung Shim | 1bcc5aa | 2011-04-08 14:08:50 +0900 | [diff] [blame] | 31 | struct s5p_ehci_hcd { |
| 32 | struct device *dev; |
| 33 | struct usb_hcd *hcd; |
| 34 | struct clk *clk; |
| 35 | }; |
| 36 | |
| 37 | static const struct hc_driver s5p_ehci_hc_driver = { |
| 38 | .description = hcd_name, |
| 39 | .product_desc = "S5P EHCI Host Controller", |
| 40 | .hcd_priv_size = sizeof(struct ehci_hcd), |
| 41 | |
| 42 | .irq = ehci_irq, |
| 43 | .flags = HCD_MEMORY | HCD_USB2, |
| 44 | |
Alan Stern | 1a49e2a | 2012-07-09 15:55:14 -0400 | [diff] [blame] | 45 | .reset = ehci_setup, |
Joonyoung Shim | 1bcc5aa | 2011-04-08 14:08:50 +0900 | [diff] [blame] | 46 | .start = ehci_run, |
| 47 | .stop = ehci_stop, |
| 48 | .shutdown = ehci_shutdown, |
| 49 | |
| 50 | .get_frame_number = ehci_get_frame, |
| 51 | |
| 52 | .urb_enqueue = ehci_urb_enqueue, |
| 53 | .urb_dequeue = ehci_urb_dequeue, |
| 54 | .endpoint_disable = ehci_endpoint_disable, |
| 55 | .endpoint_reset = ehci_endpoint_reset, |
| 56 | |
| 57 | .hub_status_data = ehci_hub_status_data, |
| 58 | .hub_control = ehci_hub_control, |
| 59 | .bus_suspend = ehci_bus_suspend, |
| 60 | .bus_resume = ehci_bus_resume, |
| 61 | |
| 62 | .relinquish_port = ehci_relinquish_port, |
| 63 | .port_handed_over = ehci_port_handed_over, |
| 64 | |
| 65 | .clear_tt_buffer_complete = ehci_clear_tt_buffer_complete, |
| 66 | }; |
| 67 | |
Vivek Gautam | fd81d59 | 2012-07-17 10:10:50 +0530 | [diff] [blame] | 68 | static void s5p_setup_vbus_gpio(struct platform_device *pdev) |
| 69 | { |
| 70 | int err; |
| 71 | int gpio; |
| 72 | |
| 73 | if (!pdev->dev.of_node) |
| 74 | return; |
| 75 | |
| 76 | gpio = of_get_named_gpio(pdev->dev.of_node, |
| 77 | "samsung,vbus-gpio", 0); |
| 78 | if (!gpio_is_valid(gpio)) |
| 79 | return; |
| 80 | |
| 81 | err = gpio_request_one(gpio, GPIOF_OUT_INIT_HIGH, "ehci_vbus_gpio"); |
| 82 | if (err) |
| 83 | dev_err(&pdev->dev, "can't request ehci vbus gpio %d", gpio); |
| 84 | } |
| 85 | |
Vivek Gautam | 2c026e2 | 2012-07-16 11:25:37 +0530 | [diff] [blame] | 86 | static u64 ehci_s5p_dma_mask = DMA_BIT_MASK(32); |
| 87 | |
Jingoo Han | 27362d4 | 2011-05-09 15:28:39 +0900 | [diff] [blame] | 88 | static int __devinit s5p_ehci_probe(struct platform_device *pdev) |
Joonyoung Shim | 1bcc5aa | 2011-04-08 14:08:50 +0900 | [diff] [blame] | 89 | { |
| 90 | struct s5p_ehci_platdata *pdata; |
| 91 | struct s5p_ehci_hcd *s5p_ehci; |
| 92 | struct usb_hcd *hcd; |
| 93 | struct ehci_hcd *ehci; |
| 94 | struct resource *res; |
| 95 | int irq; |
| 96 | int err; |
| 97 | |
| 98 | pdata = pdev->dev.platform_data; |
| 99 | if (!pdata) { |
| 100 | dev_err(&pdev->dev, "No platform data defined\n"); |
| 101 | return -EINVAL; |
| 102 | } |
| 103 | |
Vivek Gautam | 2c026e2 | 2012-07-16 11:25:37 +0530 | [diff] [blame] | 104 | /* |
| 105 | * Right now device-tree probed devices don't get dma_mask set. |
| 106 | * Since shared usb code relies on it, set it here for now. |
| 107 | * Once we move to full device tree support this will vanish off. |
| 108 | */ |
| 109 | if (!pdev->dev.dma_mask) |
| 110 | pdev->dev.dma_mask = &ehci_s5p_dma_mask; |
| 111 | if (!pdev->dev.coherent_dma_mask) |
| 112 | pdev->dev.coherent_dma_mask = DMA_BIT_MASK(32); |
| 113 | |
Vivek Gautam | fd81d59 | 2012-07-17 10:10:50 +0530 | [diff] [blame] | 114 | s5p_setup_vbus_gpio(pdev); |
| 115 | |
Jingoo Han | 9cb0756 | 2012-06-28 16:29:46 +0900 | [diff] [blame] | 116 | s5p_ehci = devm_kzalloc(&pdev->dev, sizeof(struct s5p_ehci_hcd), |
| 117 | GFP_KERNEL); |
Joonyoung Shim | 1bcc5aa | 2011-04-08 14:08:50 +0900 | [diff] [blame] | 118 | if (!s5p_ehci) |
| 119 | return -ENOMEM; |
| 120 | |
| 121 | s5p_ehci->dev = &pdev->dev; |
| 122 | |
| 123 | hcd = usb_create_hcd(&s5p_ehci_hc_driver, &pdev->dev, |
| 124 | dev_name(&pdev->dev)); |
| 125 | if (!hcd) { |
| 126 | dev_err(&pdev->dev, "Unable to create HCD\n"); |
Jingoo Han | 9cb0756 | 2012-06-28 16:29:46 +0900 | [diff] [blame] | 127 | return -ENOMEM; |
Joonyoung Shim | 1bcc5aa | 2011-04-08 14:08:50 +0900 | [diff] [blame] | 128 | } |
| 129 | |
Yulgon Kim | e5d3d44 | 2011-08-18 14:02:45 +0900 | [diff] [blame] | 130 | s5p_ehci->hcd = hcd; |
Julia Lawall | 63fd0ae | 2012-07-30 16:43:44 +0200 | [diff] [blame] | 131 | s5p_ehci->clk = devm_clk_get(&pdev->dev, "usbhost"); |
Joonyoung Shim | 1bcc5aa | 2011-04-08 14:08:50 +0900 | [diff] [blame] | 132 | |
| 133 | if (IS_ERR(s5p_ehci->clk)) { |
| 134 | dev_err(&pdev->dev, "Failed to get usbhost clock\n"); |
| 135 | err = PTR_ERR(s5p_ehci->clk); |
| 136 | goto fail_clk; |
| 137 | } |
| 138 | |
Thomas Abraham | e1deb56 | 2012-10-03 08:40:42 +0900 | [diff] [blame] | 139 | err = clk_prepare_enable(s5p_ehci->clk); |
Joonyoung Shim | 1bcc5aa | 2011-04-08 14:08:50 +0900 | [diff] [blame] | 140 | if (err) |
Julia Lawall | 63fd0ae | 2012-07-30 16:43:44 +0200 | [diff] [blame] | 141 | goto fail_clk; |
Joonyoung Shim | 1bcc5aa | 2011-04-08 14:08:50 +0900 | [diff] [blame] | 142 | |
| 143 | res = platform_get_resource(pdev, IORESOURCE_MEM, 0); |
| 144 | if (!res) { |
| 145 | dev_err(&pdev->dev, "Failed to get I/O memory\n"); |
| 146 | err = -ENXIO; |
| 147 | goto fail_io; |
| 148 | } |
| 149 | |
| 150 | hcd->rsrc_start = res->start; |
| 151 | hcd->rsrc_len = resource_size(res); |
Jingoo Han | 9cb0756 | 2012-06-28 16:29:46 +0900 | [diff] [blame] | 152 | hcd->regs = devm_ioremap(&pdev->dev, res->start, hcd->rsrc_len); |
Joonyoung Shim | 1bcc5aa | 2011-04-08 14:08:50 +0900 | [diff] [blame] | 153 | if (!hcd->regs) { |
| 154 | dev_err(&pdev->dev, "Failed to remap I/O memory\n"); |
| 155 | err = -ENOMEM; |
| 156 | goto fail_io; |
| 157 | } |
| 158 | |
| 159 | irq = platform_get_irq(pdev, 0); |
| 160 | if (!irq) { |
| 161 | dev_err(&pdev->dev, "Failed to get IRQ\n"); |
| 162 | err = -ENODEV; |
Jingoo Han | 9cb0756 | 2012-06-28 16:29:46 +0900 | [diff] [blame] | 163 | goto fail_io; |
Joonyoung Shim | 1bcc5aa | 2011-04-08 14:08:50 +0900 | [diff] [blame] | 164 | } |
| 165 | |
| 166 | if (pdata->phy_init) |
| 167 | pdata->phy_init(pdev, S5P_USB_PHY_HOST); |
| 168 | |
| 169 | ehci = hcd_to_ehci(hcd); |
| 170 | ehci->caps = hcd->regs; |
Joonyoung Shim | 1bcc5aa | 2011-04-08 14:08:50 +0900 | [diff] [blame] | 171 | |
Jingoo Han | 88555a6 | 2012-03-05 10:40:14 +0900 | [diff] [blame] | 172 | /* DMA burst Enable */ |
| 173 | writel(EHCI_INSNREG00_ENABLE_DMA_BURST, EHCI_INSNREG00(hcd->regs)); |
| 174 | |
Yong Zhang | b5dd18d | 2011-09-07 16:10:52 +0800 | [diff] [blame] | 175 | err = usb_add_hcd(hcd, irq, IRQF_SHARED); |
Joonyoung Shim | 1bcc5aa | 2011-04-08 14:08:50 +0900 | [diff] [blame] | 176 | if (err) { |
| 177 | dev_err(&pdev->dev, "Failed to add USB HCD\n"); |
Jingoo Han | 9cb0756 | 2012-06-28 16:29:46 +0900 | [diff] [blame] | 178 | goto fail_io; |
Joonyoung Shim | 1bcc5aa | 2011-04-08 14:08:50 +0900 | [diff] [blame] | 179 | } |
| 180 | |
| 181 | platform_set_drvdata(pdev, s5p_ehci); |
| 182 | |
| 183 | return 0; |
| 184 | |
Joonyoung Shim | 1bcc5aa | 2011-04-08 14:08:50 +0900 | [diff] [blame] | 185 | fail_io: |
Thomas Abraham | e1deb56 | 2012-10-03 08:40:42 +0900 | [diff] [blame] | 186 | clk_disable_unprepare(s5p_ehci->clk); |
Joonyoung Shim | 1bcc5aa | 2011-04-08 14:08:50 +0900 | [diff] [blame] | 187 | fail_clk: |
| 188 | usb_put_hcd(hcd); |
Joonyoung Shim | 1bcc5aa | 2011-04-08 14:08:50 +0900 | [diff] [blame] | 189 | return err; |
| 190 | } |
| 191 | |
Jingoo Han | 27362d4 | 2011-05-09 15:28:39 +0900 | [diff] [blame] | 192 | static int __devexit s5p_ehci_remove(struct platform_device *pdev) |
Joonyoung Shim | 1bcc5aa | 2011-04-08 14:08:50 +0900 | [diff] [blame] | 193 | { |
| 194 | struct s5p_ehci_platdata *pdata = pdev->dev.platform_data; |
| 195 | struct s5p_ehci_hcd *s5p_ehci = platform_get_drvdata(pdev); |
| 196 | struct usb_hcd *hcd = s5p_ehci->hcd; |
| 197 | |
| 198 | usb_remove_hcd(hcd); |
| 199 | |
| 200 | if (pdata && pdata->phy_exit) |
| 201 | pdata->phy_exit(pdev, S5P_USB_PHY_HOST); |
| 202 | |
Thomas Abraham | e1deb56 | 2012-10-03 08:40:42 +0900 | [diff] [blame] | 203 | clk_disable_unprepare(s5p_ehci->clk); |
Joonyoung Shim | 1bcc5aa | 2011-04-08 14:08:50 +0900 | [diff] [blame] | 204 | |
| 205 | usb_put_hcd(hcd); |
Joonyoung Shim | 1bcc5aa | 2011-04-08 14:08:50 +0900 | [diff] [blame] | 206 | |
| 207 | return 0; |
| 208 | } |
| 209 | |
| 210 | static void s5p_ehci_shutdown(struct platform_device *pdev) |
| 211 | { |
| 212 | struct s5p_ehci_hcd *s5p_ehci = platform_get_drvdata(pdev); |
| 213 | struct usb_hcd *hcd = s5p_ehci->hcd; |
| 214 | |
| 215 | if (hcd->driver->shutdown) |
| 216 | hcd->driver->shutdown(hcd); |
| 217 | } |
| 218 | |
Jingoo Han | 1acb30e | 2011-05-20 20:48:33 +0900 | [diff] [blame] | 219 | #ifdef CONFIG_PM |
| 220 | static int s5p_ehci_suspend(struct device *dev) |
| 221 | { |
| 222 | struct s5p_ehci_hcd *s5p_ehci = dev_get_drvdata(dev); |
| 223 | struct usb_hcd *hcd = s5p_ehci->hcd; |
Alan Stern | c5cf921 | 2012-06-28 11:19:02 -0400 | [diff] [blame] | 224 | bool do_wakeup = device_may_wakeup(dev); |
Jingoo Han | 1acb30e | 2011-05-20 20:48:33 +0900 | [diff] [blame] | 225 | struct platform_device *pdev = to_platform_device(dev); |
| 226 | struct s5p_ehci_platdata *pdata = pdev->dev.platform_data; |
Alan Stern | c5cf921 | 2012-06-28 11:19:02 -0400 | [diff] [blame] | 227 | int rc; |
Jingoo Han | 1acb30e | 2011-05-20 20:48:33 +0900 | [diff] [blame] | 228 | |
Alan Stern | c5cf921 | 2012-06-28 11:19:02 -0400 | [diff] [blame] | 229 | rc = ehci_suspend(hcd, do_wakeup); |
Jingoo Han | 1acb30e | 2011-05-20 20:48:33 +0900 | [diff] [blame] | 230 | |
| 231 | if (pdata && pdata->phy_exit) |
| 232 | pdata->phy_exit(pdev, S5P_USB_PHY_HOST); |
| 233 | |
Thomas Abraham | e1deb56 | 2012-10-03 08:40:42 +0900 | [diff] [blame] | 234 | clk_disable_unprepare(s5p_ehci->clk); |
Jingoo Han | 8b4fc8c | 2012-04-13 11:06:36 +0900 | [diff] [blame] | 235 | |
Jingoo Han | 1acb30e | 2011-05-20 20:48:33 +0900 | [diff] [blame] | 236 | return rc; |
| 237 | } |
| 238 | |
| 239 | static int s5p_ehci_resume(struct device *dev) |
| 240 | { |
| 241 | struct s5p_ehci_hcd *s5p_ehci = dev_get_drvdata(dev); |
| 242 | struct usb_hcd *hcd = s5p_ehci->hcd; |
Jingoo Han | 1acb30e | 2011-05-20 20:48:33 +0900 | [diff] [blame] | 243 | struct platform_device *pdev = to_platform_device(dev); |
| 244 | struct s5p_ehci_platdata *pdata = pdev->dev.platform_data; |
| 245 | |
Thomas Abraham | e1deb56 | 2012-10-03 08:40:42 +0900 | [diff] [blame] | 246 | clk_prepare_enable(s5p_ehci->clk); |
Jingoo Han | 8b4fc8c | 2012-04-13 11:06:36 +0900 | [diff] [blame] | 247 | |
Jingoo Han | 1acb30e | 2011-05-20 20:48:33 +0900 | [diff] [blame] | 248 | if (pdata && pdata->phy_init) |
| 249 | pdata->phy_init(pdev, S5P_USB_PHY_HOST); |
| 250 | |
Jingoo Han | 88555a6 | 2012-03-05 10:40:14 +0900 | [diff] [blame] | 251 | /* DMA burst Enable */ |
| 252 | writel(EHCI_INSNREG00_ENABLE_DMA_BURST, EHCI_INSNREG00(hcd->regs)); |
| 253 | |
Alan Stern | c5cf921 | 2012-06-28 11:19:02 -0400 | [diff] [blame] | 254 | ehci_resume(hcd, false); |
Jingoo Han | 1acb30e | 2011-05-20 20:48:33 +0900 | [diff] [blame] | 255 | return 0; |
| 256 | } |
| 257 | #else |
| 258 | #define s5p_ehci_suspend NULL |
| 259 | #define s5p_ehci_resume NULL |
| 260 | #endif |
| 261 | |
| 262 | static const struct dev_pm_ops s5p_ehci_pm_ops = { |
| 263 | .suspend = s5p_ehci_suspend, |
| 264 | .resume = s5p_ehci_resume, |
| 265 | }; |
| 266 | |
Vivek Gautam | 2c026e2 | 2012-07-16 11:25:37 +0530 | [diff] [blame] | 267 | #ifdef CONFIG_OF |
| 268 | static const struct of_device_id exynos_ehci_match[] = { |
| 269 | { .compatible = "samsung,exynos-ehci" }, |
| 270 | {}, |
| 271 | }; |
| 272 | MODULE_DEVICE_TABLE(of, exynos_ehci_match); |
| 273 | #endif |
| 274 | |
Joonyoung Shim | 1bcc5aa | 2011-04-08 14:08:50 +0900 | [diff] [blame] | 275 | static struct platform_driver s5p_ehci_driver = { |
| 276 | .probe = s5p_ehci_probe, |
Bill Pemberton | 7690417 | 2012-11-19 13:21:08 -0500 | [diff] [blame^] | 277 | .remove = s5p_ehci_remove, |
Joonyoung Shim | 1bcc5aa | 2011-04-08 14:08:50 +0900 | [diff] [blame] | 278 | .shutdown = s5p_ehci_shutdown, |
| 279 | .driver = { |
| 280 | .name = "s5p-ehci", |
| 281 | .owner = THIS_MODULE, |
Jingoo Han | 1acb30e | 2011-05-20 20:48:33 +0900 | [diff] [blame] | 282 | .pm = &s5p_ehci_pm_ops, |
Vivek Gautam | 2c026e2 | 2012-07-16 11:25:37 +0530 | [diff] [blame] | 283 | .of_match_table = of_match_ptr(exynos_ehci_match), |
Joonyoung Shim | 1bcc5aa | 2011-04-08 14:08:50 +0900 | [diff] [blame] | 284 | } |
| 285 | }; |
| 286 | |
| 287 | MODULE_ALIAS("platform:s5p-ehci"); |