xref: /openbmc/linux/drivers/usb/c67x00/c67x00-drv.c (revision 4ae555a5)
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) which 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/slab.h>
41 #include <linux/module.h>
42 #include <linux/usb.h>
43 #include <linux/usb/c67x00.h>
44 
45 #include "c67x00.h"
46 #include "c67x00-hcd.h"
47 
48 static void c67x00_probe_sie(struct c67x00_sie *sie,
49 			     struct c67x00_device *dev, int sie_num)
50 {
51 	spin_lock_init(&sie->lock);
52 	sie->dev = dev;
53 	sie->sie_num = sie_num;
54 	sie->mode = c67x00_sie_config(dev->pdata->sie_config, sie_num);
55 
56 	switch (sie->mode) {
57 	case C67X00_SIE_HOST:
58 		c67x00_hcd_probe(sie);
59 		break;
60 
61 	case C67X00_SIE_UNUSED:
62 		dev_info(sie_dev(sie),
63 			 "Not using SIE %d as requested\n", sie->sie_num);
64 		break;
65 
66 	default:
67 		dev_err(sie_dev(sie),
68 			"Unsupported configuration: 0x%x for SIE %d\n",
69 			sie->mode, sie->sie_num);
70 		break;
71 	}
72 }
73 
74 static void c67x00_remove_sie(struct c67x00_sie *sie)
75 {
76 	switch (sie->mode) {
77 	case C67X00_SIE_HOST:
78 		c67x00_hcd_remove(sie);
79 		break;
80 
81 	default:
82 		break;
83 	}
84 }
85 
86 static irqreturn_t c67x00_irq(int irq, void *__dev)
87 {
88 	struct c67x00_device *c67x00 = __dev;
89 	struct c67x00_sie *sie;
90 	u16 msg, int_status;
91 	int i, count = 8;
92 
93 	int_status = c67x00_ll_hpi_status(c67x00);
94 	if (!int_status)
95 		return IRQ_NONE;
96 
97 	while (int_status != 0 && (count-- >= 0)) {
98 		c67x00_ll_irq(c67x00, int_status);
99 		for (i = 0; i < C67X00_SIES; i++) {
100 			sie = &c67x00->sie[i];
101 			msg = 0;
102 			if (int_status & SIEMSG_FLG(i))
103 				msg = c67x00_ll_fetch_siemsg(c67x00, i);
104 			if (sie->irq)
105 				sie->irq(sie, int_status, msg);
106 		}
107 		int_status = c67x00_ll_hpi_status(c67x00);
108 	}
109 
110 	if (int_status)
111 		dev_warn(&c67x00->pdev->dev, "Not all interrupts handled! "
112 			 "status = 0x%04x\n", int_status);
113 
114 	return IRQ_HANDLED;
115 }
116 
117 /* ------------------------------------------------------------------------- */
118 
119 static int c67x00_drv_probe(struct platform_device *pdev)
120 {
121 	struct c67x00_device *c67x00;
122 	struct c67x00_platform_data *pdata;
123 	struct resource *res, *res2;
124 	int ret, i;
125 
126 	res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
127 	if (!res)
128 		return -ENODEV;
129 
130 	res2 = platform_get_resource(pdev, IORESOURCE_IRQ, 0);
131 	if (!res2)
132 		return -ENODEV;
133 
134 	pdata = dev_get_platdata(&pdev->dev);
135 	if (!pdata)
136 		return -ENODEV;
137 
138 	c67x00 = kzalloc(sizeof(*c67x00), GFP_KERNEL);
139 	if (!c67x00)
140 		return -ENOMEM;
141 
142 	if (!request_mem_region(res->start, resource_size(res),
143 				pdev->name)) {
144 		dev_err(&pdev->dev, "Memory region busy\n");
145 		ret = -EBUSY;
146 		goto request_mem_failed;
147 	}
148 	c67x00->hpi.base = ioremap(res->start, resource_size(res));
149 	if (!c67x00->hpi.base) {
150 		dev_err(&pdev->dev, "Unable to map HPI registers\n");
151 		ret = -EIO;
152 		goto map_failed;
153 	}
154 
155 	spin_lock_init(&c67x00->hpi.lock);
156 	c67x00->hpi.regstep = pdata->hpi_regstep;
157 	c67x00->pdata = dev_get_platdata(&pdev->dev);
158 	c67x00->pdev = pdev;
159 
160 	c67x00_ll_init(c67x00);
161 	c67x00_ll_hpi_reg_init(c67x00);
162 
163 	ret = request_irq(res2->start, c67x00_irq, 0, pdev->name, c67x00);
164 	if (ret) {
165 		dev_err(&pdev->dev, "Cannot claim IRQ\n");
166 		goto request_irq_failed;
167 	}
168 
169 	ret = c67x00_ll_reset(c67x00);
170 	if (ret) {
171 		dev_err(&pdev->dev, "Device reset failed\n");
172 		goto reset_failed;
173 	}
174 
175 	for (i = 0; i < C67X00_SIES; i++)
176 		c67x00_probe_sie(&c67x00->sie[i], c67x00, i);
177 
178 	platform_set_drvdata(pdev, c67x00);
179 
180 	return 0;
181 
182  reset_failed:
183 	free_irq(res2->start, c67x00);
184  request_irq_failed:
185 	iounmap(c67x00->hpi.base);
186  map_failed:
187 	release_mem_region(res->start, resource_size(res));
188  request_mem_failed:
189 	kfree(c67x00);
190 
191 	return ret;
192 }
193 
194 static int c67x00_drv_remove(struct platform_device *pdev)
195 {
196 	struct c67x00_device *c67x00 = platform_get_drvdata(pdev);
197 	struct resource *res;
198 	int i;
199 
200 	for (i = 0; i < C67X00_SIES; i++)
201 		c67x00_remove_sie(&c67x00->sie[i]);
202 
203 	c67x00_ll_release(c67x00);
204 
205 	res = platform_get_resource(pdev, IORESOURCE_IRQ, 0);
206 	if (res)
207 		free_irq(res->start, c67x00);
208 
209 	iounmap(c67x00->hpi.base);
210 
211 	res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
212 	if (res)
213 		release_mem_region(res->start, resource_size(res));
214 
215 	kfree(c67x00);
216 
217 	return 0;
218 }
219 
220 static struct platform_driver c67x00_driver = {
221 	.probe	= c67x00_drv_probe,
222 	.remove	= c67x00_drv_remove,
223 	.driver	= {
224 		.name = "c67x00",
225 	},
226 };
227 
228 module_platform_driver(c67x00_driver);
229 
230 MODULE_AUTHOR("Peter Korsgaard, Jan Veldeman, Grant Likely");
231 MODULE_DESCRIPTION("Cypress C67X00 USB Controller Driver");
232 MODULE_LICENSE("GPL");
233 MODULE_ALIAS("platform:c67x00");
234