Commit | Line | Data |
---|---|---|
b02b371e PK |
1 | /* |
2 | * c67x00-drv.c: Cypress C67X00 USB Common infrastructure | |
3 | * | |
4 | * Copyright (C) 2006-2008 Barco N.V. | |
5 | * Derived from the Cypress cy7c67200/300 ezusb linux driver and | |
6 | * based on multiple host controller drivers inside the linux kernel. | |
7 | * | |
8 | * This program is free software; you can redistribute it and/or modify | |
9 | * it under the terms of the GNU General Public License as published by | |
10 | * the Free Software Foundation; either version 2 of the License, or | |
11 | * (at your option) any later version. | |
12 | * | |
13 | * This program is distributed in the hope that it will be useful, | |
14 | * but WITHOUT ANY WARRANTY; without even the implied warranty of | |
15 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | |
16 | * GNU General Public License for more details. | |
17 | * | |
18 | * You should have received a copy of the GNU General Public License | |
19 | * along with this program; if not, write to the Free Software | |
20 | * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, | |
21 | * MA 02110-1301 USA. | |
22 | */ | |
23 | ||
24 | /* | |
25 | * This file implements the common infrastructure for using the c67x00. | |
26 | * It is both the link between the platform configuration and subdrivers and | |
27 | * the link between the common hardware parts and the subdrivers (e.g. | |
28 | * interrupt handling). | |
29 | * | |
30 | * The c67x00 has 2 SIE's (serial interface engine) wich can be configured | |
31 | * to be host, device or OTG (with some limitations, E.G. only SIE1 can be OTG). | |
32 | * | |
33 | * Depending on the platform configuration, the SIE's are created and | |
34 | * the corresponding subdriver is initialized (c67x00_probe_sie). | |
35 | */ | |
36 | ||
37 | #include <linux/device.h> | |
38 | #include <linux/io.h> | |
39 | #include <linux/list.h> | |
40 | #include <linux/usb.h> | |
41 | #include <linux/usb/c67x00.h> | |
42 | ||
43 | #include "c67x00.h" | |
e9b29ffc | 44 | #include "c67x00-hcd.h" |
b02b371e PK |
45 | |
46 | static void c67x00_probe_sie(struct c67x00_sie *sie, | |
47 | struct c67x00_device *dev, int sie_num) | |
48 | { | |
49 | spin_lock_init(&sie->lock); | |
50 | sie->dev = dev; | |
51 | sie->sie_num = sie_num; | |
52 | sie->mode = c67x00_sie_config(dev->pdata->sie_config, sie_num); | |
53 | ||
54 | switch (sie->mode) { | |
e9b29ffc PK |
55 | case C67X00_SIE_HOST: |
56 | c67x00_hcd_probe(sie); | |
57 | break; | |
58 | ||
b02b371e PK |
59 | case C67X00_SIE_UNUSED: |
60 | dev_info(sie_dev(sie), | |
61 | "Not using SIE %d as requested\n", sie->sie_num); | |
62 | break; | |
63 | ||
64 | default: | |
65 | dev_err(sie_dev(sie), | |
66 | "Unsupported configuration: 0x%x for SIE %d\n", | |
67 | sie->mode, sie->sie_num); | |
68 | break; | |
69 | } | |
70 | } | |
71 | ||
72 | static void c67x00_remove_sie(struct c67x00_sie *sie) | |
73 | { | |
e9b29ffc PK |
74 | switch (sie->mode) { |
75 | case C67X00_SIE_HOST: | |
76 | c67x00_hcd_remove(sie); | |
77 | break; | |
78 | ||
79 | default: | |
80 | break; | |
81 | } | |
b02b371e PK |
82 | } |
83 | ||
84 | static irqreturn_t c67x00_irq(int irq, void *__dev) | |
85 | { | |
86 | struct c67x00_device *c67x00 = __dev; | |
87 | struct c67x00_sie *sie; | |
88 | u16 msg, int_status; | |
89 | int i, count = 8; | |
90 | ||
91 | int_status = c67x00_ll_hpi_status(c67x00); | |
92 | if (!int_status) | |
93 | return IRQ_NONE; | |
94 | ||
95 | while (int_status != 0 && (count-- >= 0)) { | |
96 | c67x00_ll_irq(c67x00, int_status); | |
97 | for (i = 0; i < C67X00_SIES; i++) { | |
98 | sie = &c67x00->sie[i]; | |
99 | msg = 0; | |
100 | if (int_status & SIEMSG_FLG(i)) | |
101 | msg = c67x00_ll_fetch_siemsg(c67x00, i); | |
102 | if (sie->irq) | |
103 | sie->irq(sie, int_status, msg); | |
104 | } | |
105 | int_status = c67x00_ll_hpi_status(c67x00); | |
106 | } | |
107 | ||
108 | if (int_status) | |
109 | dev_warn(&c67x00->pdev->dev, "Not all interrupts handled! " | |
110 | "status = 0x%04x\n", int_status); | |
111 | ||
112 | return IRQ_HANDLED; | |
113 | } | |
114 | ||
115 | /* ------------------------------------------------------------------------- */ | |
116 | ||
117 | static int __devinit c67x00_drv_probe(struct platform_device *pdev) | |
118 | { | |
119 | struct c67x00_device *c67x00; | |
120 | struct c67x00_platform_data *pdata; | |
121 | struct resource *res, *res2; | |
122 | int ret, i; | |
123 | ||
124 | res = platform_get_resource(pdev, IORESOURCE_MEM, 0); | |
125 | if (!res) | |
126 | return -ENODEV; | |
127 | ||
128 | res2 = platform_get_resource(pdev, IORESOURCE_IRQ, 0); | |
129 | if (!res2) | |
130 | return -ENODEV; | |
131 | ||
132 | pdata = pdev->dev.platform_data; | |
133 | if (!pdata) | |
134 | return -ENODEV; | |
135 | ||
136 | c67x00 = kzalloc(sizeof(*c67x00), GFP_KERNEL); | |
137 | if (!c67x00) | |
138 | return -ENOMEM; | |
139 | ||
140 | if (!request_mem_region(res->start, res->end - res->start + 1, | |
141 | pdev->name)) { | |
142 | dev_err(&pdev->dev, "Memory region busy\n"); | |
143 | ret = -EBUSY; | |
144 | goto request_mem_failed; | |
145 | } | |
146 | c67x00->hpi.base = ioremap(res->start, res->end - res->start + 1); | |
147 | if (!c67x00->hpi.base) { | |
148 | dev_err(&pdev->dev, "Unable to map HPI registers\n"); | |
149 | ret = -EIO; | |
150 | goto map_failed; | |
151 | } | |
152 | ||
153 | spin_lock_init(&c67x00->hpi.lock); | |
154 | c67x00->hpi.regstep = pdata->hpi_regstep; | |
155 | c67x00->pdata = pdev->dev.platform_data; | |
156 | c67x00->pdev = pdev; | |
157 | ||
158 | c67x00_ll_init(c67x00); | |
159 | c67x00_ll_hpi_reg_init(c67x00); | |
160 | ||
161 | ret = request_irq(res2->start, c67x00_irq, 0, pdev->name, c67x00); | |
162 | if (ret) { | |
163 | dev_err(&pdev->dev, "Cannot claim IRQ\n"); | |
164 | goto request_irq_failed; | |
165 | } | |
166 | ||
167 | ret = c67x00_ll_reset(c67x00); | |
168 | if (ret) { | |
169 | dev_err(&pdev->dev, "Device reset failed\n"); | |
170 | goto reset_failed; | |
171 | } | |
172 | ||
173 | for (i = 0; i < C67X00_SIES; i++) | |
174 | c67x00_probe_sie(&c67x00->sie[i], c67x00, i); | |
175 | ||
176 | platform_set_drvdata(pdev, c67x00); | |
177 | ||
178 | return 0; | |
179 | ||
180 | reset_failed: | |
181 | free_irq(res2->start, c67x00); | |
182 | request_irq_failed: | |
183 | iounmap(c67x00->hpi.base); | |
184 | map_failed: | |
185 | release_mem_region(res->start, res->end - res->start + 1); | |
186 | request_mem_failed: | |
187 | kfree(c67x00); | |
188 | ||
189 | return ret; | |
190 | } | |
191 | ||
192 | static int __devexit c67x00_drv_remove(struct platform_device *pdev) | |
193 | { | |
194 | struct c67x00_device *c67x00 = platform_get_drvdata(pdev); | |
195 | struct resource *res; | |
196 | int i; | |
197 | ||
198 | for (i = 0; i < C67X00_SIES; i++) | |
199 | c67x00_remove_sie(&c67x00->sie[i]); | |
200 | ||
201 | c67x00_ll_release(c67x00); | |
202 | ||
203 | res = platform_get_resource(pdev, IORESOURCE_IRQ, 0); | |
204 | if (res) | |
205 | free_irq(res->start, c67x00); | |
206 | ||
207 | iounmap(c67x00->hpi.base); | |
208 | ||
209 | res = platform_get_resource(pdev, IORESOURCE_MEM, 0); | |
210 | if (res) | |
211 | release_mem_region(res->start, res->end - res->start + 1); | |
212 | ||
213 | kfree(c67x00); | |
214 | ||
215 | return 0; | |
216 | } | |
217 | ||
218 | static struct platform_driver c67x00_driver = { | |
219 | .probe = c67x00_drv_probe, | |
220 | .remove = __devexit_p(c67x00_drv_remove), | |
221 | .driver = { | |
222 | .owner = THIS_MODULE, | |
223 | .name = "c67x00", | |
224 | }, | |
225 | }; | |
226 | MODULE_ALIAS("platform:c67x00"); | |
227 | ||
228 | static int __init c67x00_init(void) | |
229 | { | |
230 | return platform_driver_register(&c67x00_driver); | |
231 | } | |
232 | ||
233 | static void __exit c67x00_exit(void) | |
234 | { | |
235 | platform_driver_unregister(&c67x00_driver); | |
236 | } | |
237 | ||
238 | module_init(c67x00_init); | |
239 | module_exit(c67x00_exit); | |
240 | ||
241 | MODULE_AUTHOR("Peter Korsgaard, Jan Veldeman, Grant Likely"); | |
242 | MODULE_DESCRIPTION("Cypress C67X00 USB Controller Driver"); | |
243 | MODULE_LICENSE("GPL"); |