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