Nicolas Ferre | 501c9c0 | 2009-07-27 14:47:40 -0700 | [diff] [blame] | 1 | /* |
| 2 | * Driver for EHCI UHP on Atmel chips |
| 3 | * |
| 4 | * Copyright (C) 2009 Atmel Corporation, |
| 5 | * Nicolas Ferre <nicolas.ferre@atmel.com> |
| 6 | * |
| 7 | * Based on various ehci-*.c drivers |
| 8 | * |
| 9 | * This file is subject to the terms and conditions of the GNU General Public |
| 10 | * License. See the file COPYING in the main directory of this archive for |
| 11 | * more details. |
| 12 | */ |
| 13 | |
| 14 | #include <linux/clk.h> |
| 15 | #include <linux/platform_device.h> |
Jean-Christophe PLAGNIOL-VILLARD | 9d84300 | 2011-11-22 12:11:13 +0800 | [diff] [blame] | 16 | #include <linux/of_platform.h> |
Nicolas Ferre | 501c9c0 | 2009-07-27 14:47:40 -0700 | [diff] [blame] | 17 | |
| 18 | /* interface and function clocks */ |
| 19 | static struct clk *iclk, *fclk; |
| 20 | static int clocked; |
| 21 | |
| 22 | /*-------------------------------------------------------------------------*/ |
| 23 | |
| 24 | static void atmel_start_clock(void) |
| 25 | { |
| 26 | clk_enable(iclk); |
| 27 | clk_enable(fclk); |
| 28 | clocked = 1; |
| 29 | } |
| 30 | |
| 31 | static void atmel_stop_clock(void) |
| 32 | { |
| 33 | clk_disable(fclk); |
| 34 | clk_disable(iclk); |
| 35 | clocked = 0; |
| 36 | } |
| 37 | |
| 38 | static void atmel_start_ehci(struct platform_device *pdev) |
| 39 | { |
| 40 | dev_dbg(&pdev->dev, "start\n"); |
| 41 | atmel_start_clock(); |
| 42 | } |
| 43 | |
| 44 | static void atmel_stop_ehci(struct platform_device *pdev) |
| 45 | { |
| 46 | dev_dbg(&pdev->dev, "stop\n"); |
| 47 | atmel_stop_clock(); |
| 48 | } |
| 49 | |
| 50 | /*-------------------------------------------------------------------------*/ |
| 51 | |
| 52 | static int ehci_atmel_setup(struct usb_hcd *hcd) |
| 53 | { |
| 54 | struct ehci_hcd *ehci = hcd_to_ehci(hcd); |
| 55 | int retval = 0; |
| 56 | |
| 57 | /* registers start at offset 0x0 */ |
| 58 | ehci->caps = hcd->regs; |
| 59 | ehci->regs = hcd->regs + |
Jan Andersson | c430131 | 2011-05-03 20:11:57 +0200 | [diff] [blame] | 60 | HC_LENGTH(ehci, ehci_readl(ehci, &ehci->caps->hc_capbase)); |
Nicolas Ferre | 501c9c0 | 2009-07-27 14:47:40 -0700 | [diff] [blame] | 61 | dbg_hcs_params(ehci, "reset"); |
| 62 | dbg_hcc_params(ehci, "reset"); |
| 63 | |
| 64 | /* cache this readonly data; minimize chip reads */ |
| 65 | ehci->hcs_params = ehci_readl(ehci, &ehci->caps->hcs_params); |
| 66 | |
| 67 | retval = ehci_halt(ehci); |
| 68 | if (retval) |
| 69 | return retval; |
| 70 | |
| 71 | /* data structure init */ |
| 72 | retval = ehci_init(hcd); |
| 73 | if (retval) |
| 74 | return retval; |
| 75 | |
| 76 | ehci->sbrn = 0x20; |
| 77 | |
| 78 | ehci_reset(ehci); |
| 79 | ehci_port_power(ehci, 0); |
| 80 | |
| 81 | return retval; |
| 82 | } |
| 83 | |
| 84 | static const struct hc_driver ehci_atmel_hc_driver = { |
| 85 | .description = hcd_name, |
| 86 | .product_desc = "Atmel EHCI UHP HS", |
| 87 | .hcd_priv_size = sizeof(struct ehci_hcd), |
| 88 | |
| 89 | /* generic hardware linkage */ |
| 90 | .irq = ehci_irq, |
| 91 | .flags = HCD_MEMORY | HCD_USB2, |
| 92 | |
| 93 | /* basic lifecycle operations */ |
| 94 | .reset = ehci_atmel_setup, |
| 95 | .start = ehci_run, |
| 96 | .stop = ehci_stop, |
| 97 | .shutdown = ehci_shutdown, |
| 98 | |
| 99 | /* managing i/o requests and associated device resources */ |
| 100 | .urb_enqueue = ehci_urb_enqueue, |
| 101 | .urb_dequeue = ehci_urb_dequeue, |
| 102 | .endpoint_disable = ehci_endpoint_disable, |
Paul Mundt | b48d7f5 | 2010-11-30 17:57:02 +0900 | [diff] [blame] | 103 | .endpoint_reset = ehci_endpoint_reset, |
Nicolas Ferre | 501c9c0 | 2009-07-27 14:47:40 -0700 | [diff] [blame] | 104 | |
| 105 | /* scheduling support */ |
| 106 | .get_frame_number = ehci_get_frame, |
| 107 | |
| 108 | /* root hub support */ |
| 109 | .hub_status_data = ehci_hub_status_data, |
| 110 | .hub_control = ehci_hub_control, |
| 111 | .bus_suspend = ehci_bus_suspend, |
| 112 | .bus_resume = ehci_bus_resume, |
| 113 | .relinquish_port = ehci_relinquish_port, |
| 114 | .port_handed_over = ehci_port_handed_over, |
Paul Mundt | b48d7f5 | 2010-11-30 17:57:02 +0900 | [diff] [blame] | 115 | |
| 116 | .clear_tt_buffer_complete = ehci_clear_tt_buffer_complete, |
Nicolas Ferre | 501c9c0 | 2009-07-27 14:47:40 -0700 | [diff] [blame] | 117 | }; |
| 118 | |
Jean-Christophe PLAGNIOL-VILLARD | 9d84300 | 2011-11-22 12:11:13 +0800 | [diff] [blame] | 119 | static u64 at91_ehci_dma_mask = DMA_BIT_MASK(32); |
| 120 | |
Hubert Feurstein | 0dfeefb | 2011-03-10 10:06:37 +0100 | [diff] [blame] | 121 | static int __devinit ehci_atmel_drv_probe(struct platform_device *pdev) |
Nicolas Ferre | 501c9c0 | 2009-07-27 14:47:40 -0700 | [diff] [blame] | 122 | { |
| 123 | struct usb_hcd *hcd; |
| 124 | const struct hc_driver *driver = &ehci_atmel_hc_driver; |
| 125 | struct resource *res; |
| 126 | int irq; |
| 127 | int retval; |
| 128 | |
| 129 | if (usb_disabled()) |
| 130 | return -ENODEV; |
| 131 | |
| 132 | pr_debug("Initializing Atmel-SoC USB Host Controller\n"); |
| 133 | |
| 134 | irq = platform_get_irq(pdev, 0); |
| 135 | if (irq <= 0) { |
| 136 | dev_err(&pdev->dev, |
| 137 | "Found HC with no IRQ. Check %s setup!\n", |
| 138 | dev_name(&pdev->dev)); |
| 139 | retval = -ENODEV; |
| 140 | goto fail_create_hcd; |
| 141 | } |
| 142 | |
Jean-Christophe PLAGNIOL-VILLARD | 9d84300 | 2011-11-22 12:11:13 +0800 | [diff] [blame] | 143 | /* Right now device-tree probed devices don't get dma_mask set. |
| 144 | * Since shared usb code relies on it, set it here for now. |
| 145 | * Once we have dma capability bindings this can go away. |
| 146 | */ |
| 147 | if (!pdev->dev.dma_mask) |
| 148 | pdev->dev.dma_mask = &at91_ehci_dma_mask; |
| 149 | |
Nicolas Ferre | 501c9c0 | 2009-07-27 14:47:40 -0700 | [diff] [blame] | 150 | hcd = usb_create_hcd(driver, &pdev->dev, dev_name(&pdev->dev)); |
| 151 | if (!hcd) { |
| 152 | retval = -ENOMEM; |
| 153 | goto fail_create_hcd; |
| 154 | } |
| 155 | |
| 156 | res = platform_get_resource(pdev, IORESOURCE_MEM, 0); |
| 157 | if (!res) { |
| 158 | dev_err(&pdev->dev, |
| 159 | "Found HC with no register addr. Check %s setup!\n", |
| 160 | dev_name(&pdev->dev)); |
| 161 | retval = -ENODEV; |
| 162 | goto fail_request_resource; |
| 163 | } |
| 164 | hcd->rsrc_start = res->start; |
H Hartley Sweeten | f65c354 | 2009-12-14 18:15:35 -0500 | [diff] [blame] | 165 | hcd->rsrc_len = resource_size(res); |
Nicolas Ferre | 501c9c0 | 2009-07-27 14:47:40 -0700 | [diff] [blame] | 166 | |
| 167 | if (!request_mem_region(hcd->rsrc_start, hcd->rsrc_len, |
| 168 | driver->description)) { |
| 169 | dev_dbg(&pdev->dev, "controller already in use\n"); |
| 170 | retval = -EBUSY; |
| 171 | goto fail_request_resource; |
| 172 | } |
| 173 | |
| 174 | hcd->regs = ioremap_nocache(hcd->rsrc_start, hcd->rsrc_len); |
| 175 | if (hcd->regs == NULL) { |
| 176 | dev_dbg(&pdev->dev, "error mapping memory\n"); |
| 177 | retval = -EFAULT; |
| 178 | goto fail_ioremap; |
| 179 | } |
| 180 | |
| 181 | iclk = clk_get(&pdev->dev, "ehci_clk"); |
| 182 | if (IS_ERR(iclk)) { |
| 183 | dev_err(&pdev->dev, "Error getting interface clock\n"); |
| 184 | retval = -ENOENT; |
| 185 | goto fail_get_iclk; |
| 186 | } |
| 187 | fclk = clk_get(&pdev->dev, "uhpck"); |
| 188 | if (IS_ERR(fclk)) { |
| 189 | dev_err(&pdev->dev, "Error getting function clock\n"); |
| 190 | retval = -ENOENT; |
| 191 | goto fail_get_fclk; |
| 192 | } |
| 193 | |
| 194 | atmel_start_ehci(pdev); |
| 195 | |
| 196 | retval = usb_add_hcd(hcd, irq, IRQF_SHARED); |
| 197 | if (retval) |
| 198 | goto fail_add_hcd; |
| 199 | |
| 200 | return retval; |
| 201 | |
| 202 | fail_add_hcd: |
| 203 | atmel_stop_ehci(pdev); |
| 204 | clk_put(fclk); |
| 205 | fail_get_fclk: |
| 206 | clk_put(iclk); |
| 207 | fail_get_iclk: |
| 208 | iounmap(hcd->regs); |
| 209 | fail_ioremap: |
| 210 | release_mem_region(hcd->rsrc_start, hcd->rsrc_len); |
| 211 | fail_request_resource: |
| 212 | usb_put_hcd(hcd); |
| 213 | fail_create_hcd: |
| 214 | dev_err(&pdev->dev, "init %s fail, %d\n", |
| 215 | dev_name(&pdev->dev), retval); |
| 216 | |
| 217 | return retval; |
| 218 | } |
| 219 | |
Hubert Feurstein | 0dfeefb | 2011-03-10 10:06:37 +0100 | [diff] [blame] | 220 | static int __devexit ehci_atmel_drv_remove(struct platform_device *pdev) |
Nicolas Ferre | 501c9c0 | 2009-07-27 14:47:40 -0700 | [diff] [blame] | 221 | { |
| 222 | struct usb_hcd *hcd = platform_get_drvdata(pdev); |
| 223 | |
| 224 | ehci_shutdown(hcd); |
| 225 | usb_remove_hcd(hcd); |
| 226 | iounmap(hcd->regs); |
| 227 | release_mem_region(hcd->rsrc_start, hcd->rsrc_len); |
| 228 | usb_put_hcd(hcd); |
| 229 | |
| 230 | atmel_stop_ehci(pdev); |
| 231 | clk_put(fclk); |
| 232 | clk_put(iclk); |
| 233 | fclk = iclk = NULL; |
| 234 | |
| 235 | return 0; |
| 236 | } |
| 237 | |
Jean-Christophe PLAGNIOL-VILLARD | 9d84300 | 2011-11-22 12:11:13 +0800 | [diff] [blame] | 238 | #ifdef CONFIG_OF |
| 239 | static const struct of_device_id atmel_ehci_dt_ids[] = { |
| 240 | { .compatible = "atmel,at91sam9g45-ehci" }, |
| 241 | { /* sentinel */ } |
| 242 | }; |
| 243 | |
| 244 | MODULE_DEVICE_TABLE(of, atmel_ehci_dt_ids); |
| 245 | #endif |
| 246 | |
Nicolas Ferre | 501c9c0 | 2009-07-27 14:47:40 -0700 | [diff] [blame] | 247 | static struct platform_driver ehci_atmel_driver = { |
| 248 | .probe = ehci_atmel_drv_probe, |
Hubert Feurstein | 0dfeefb | 2011-03-10 10:06:37 +0100 | [diff] [blame] | 249 | .remove = __devexit_p(ehci_atmel_drv_remove), |
Nicolas Ferre | 501c9c0 | 2009-07-27 14:47:40 -0700 | [diff] [blame] | 250 | .shutdown = usb_hcd_platform_shutdown, |
Jean-Christophe PLAGNIOL-VILLARD | 9d84300 | 2011-11-22 12:11:13 +0800 | [diff] [blame] | 251 | .driver = { |
| 252 | .name = "atmel-ehci", |
| 253 | .of_match_table = of_match_ptr(atmel_ehci_dt_ids), |
| 254 | }, |
Nicolas Ferre | 501c9c0 | 2009-07-27 14:47:40 -0700 | [diff] [blame] | 255 | }; |