blob: 3e4b46f8aa6721164d44e980018a6837e10681aa [file] [log] [blame]
Greg Kroah-Hartman5fd54ac2017-11-03 11:28:30 +01001// SPDX-License-Identifier: GPL-2.0+
Peter Korsgaardb02b3712008-04-27 08:59:44 +02002/*
3 * c67x00-drv.c: Cypress C67X00 USB Common infrastructure
4 *
5 * Copyright (C) 2006-2008 Barco N.V.
6 * Derived from the Cypress cy7c67200/300 ezusb linux driver and
7 * based on multiple host controller drivers inside the linux kernel.
8 *
9 * This program is free software; you can redistribute it and/or modify
10 * it under the terms of the GNU General Public License as published by
11 * the Free Software Foundation; either version 2 of the License, or
12 * (at your option) any later version.
13 *
14 * This program is distributed in the hope that it will be useful,
15 * but WITHOUT ANY WARRANTY; without even the implied warranty of
16 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
17 * GNU General Public License for more details.
18 *
19 * You should have received a copy of the GNU General Public License
20 * along with this program; if not, write to the Free Software
21 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston,
22 * MA 02110-1301 USA.
23 */
24
25/*
26 * This file implements the common infrastructure for using the c67x00.
27 * It is both the link between the platform configuration and subdrivers and
28 * the link between the common hardware parts and the subdrivers (e.g.
29 * interrupt handling).
30 *
Lucas De Marchi25985ed2011-03-30 22:57:33 -030031 * The c67x00 has 2 SIE's (serial interface engine) which can be configured
Peter Korsgaardb02b3712008-04-27 08:59:44 +020032 * to be host, device or OTG (with some limitations, E.G. only SIE1 can be OTG).
33 *
34 * Depending on the platform configuration, the SIE's are created and
35 * the corresponding subdriver is initialized (c67x00_probe_sie).
36 */
37
38#include <linux/device.h>
39#include <linux/io.h>
40#include <linux/list.h>
Tejun Heo5a0e3ad2010-03-24 17:04:11 +090041#include <linux/slab.h>
Paul Gortmaker6eb0de82011-07-03 16:09:31 -040042#include <linux/module.h>
Peter Korsgaardb02b3712008-04-27 08:59:44 +020043#include <linux/usb.h>
44#include <linux/usb/c67x00.h>
45
46#include "c67x00.h"
Peter Korsgaarde9b29ff2008-04-27 08:59:45 +020047#include "c67x00-hcd.h"
Peter Korsgaardb02b3712008-04-27 08:59:44 +020048
49static void c67x00_probe_sie(struct c67x00_sie *sie,
50 struct c67x00_device *dev, int sie_num)
51{
52 spin_lock_init(&sie->lock);
53 sie->dev = dev;
54 sie->sie_num = sie_num;
55 sie->mode = c67x00_sie_config(dev->pdata->sie_config, sie_num);
56
57 switch (sie->mode) {
Peter Korsgaarde9b29ff2008-04-27 08:59:45 +020058 case C67X00_SIE_HOST:
59 c67x00_hcd_probe(sie);
60 break;
61
Peter Korsgaardb02b3712008-04-27 08:59:44 +020062 case C67X00_SIE_UNUSED:
63 dev_info(sie_dev(sie),
64 "Not using SIE %d as requested\n", sie->sie_num);
65 break;
66
67 default:
68 dev_err(sie_dev(sie),
69 "Unsupported configuration: 0x%x for SIE %d\n",
70 sie->mode, sie->sie_num);
71 break;
72 }
73}
74
75static void c67x00_remove_sie(struct c67x00_sie *sie)
76{
Peter Korsgaarde9b29ff2008-04-27 08:59:45 +020077 switch (sie->mode) {
78 case C67X00_SIE_HOST:
79 c67x00_hcd_remove(sie);
80 break;
81
82 default:
83 break;
84 }
Peter Korsgaardb02b3712008-04-27 08:59:44 +020085}
86
87static irqreturn_t c67x00_irq(int irq, void *__dev)
88{
89 struct c67x00_device *c67x00 = __dev;
90 struct c67x00_sie *sie;
91 u16 msg, int_status;
92 int i, count = 8;
93
94 int_status = c67x00_ll_hpi_status(c67x00);
95 if (!int_status)
96 return IRQ_NONE;
97
98 while (int_status != 0 && (count-- >= 0)) {
99 c67x00_ll_irq(c67x00, int_status);
100 for (i = 0; i < C67X00_SIES; i++) {
101 sie = &c67x00->sie[i];
102 msg = 0;
103 if (int_status & SIEMSG_FLG(i))
104 msg = c67x00_ll_fetch_siemsg(c67x00, i);
105 if (sie->irq)
106 sie->irq(sie, int_status, msg);
107 }
108 int_status = c67x00_ll_hpi_status(c67x00);
109 }
110
111 if (int_status)
112 dev_warn(&c67x00->pdev->dev, "Not all interrupts handled! "
113 "status = 0x%04x\n", int_status);
114
115 return IRQ_HANDLED;
116}
117
118/* ------------------------------------------------------------------------- */
119
Bill Pemberton41ac7b32012-11-19 13:21:48 -0500120static int c67x00_drv_probe(struct platform_device *pdev)
Peter Korsgaardb02b3712008-04-27 08:59:44 +0200121{
122 struct c67x00_device *c67x00;
123 struct c67x00_platform_data *pdata;
124 struct resource *res, *res2;
125 int ret, i;
126
127 res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
128 if (!res)
129 return -ENODEV;
130
131 res2 = platform_get_resource(pdev, IORESOURCE_IRQ, 0);
132 if (!res2)
133 return -ENODEV;
134
Jingoo Han720ce6e2013-07-30 17:25:19 +0900135 pdata = dev_get_platdata(&pdev->dev);
Peter Korsgaardb02b3712008-04-27 08:59:44 +0200136 if (!pdata)
137 return -ENODEV;
138
139 c67x00 = kzalloc(sizeof(*c67x00), GFP_KERNEL);
140 if (!c67x00)
141 return -ENOMEM;
142
Thiago Farinac38b9402010-01-01 16:42:34 -0500143 if (!request_mem_region(res->start, resource_size(res),
Peter Korsgaardb02b3712008-04-27 08:59:44 +0200144 pdev->name)) {
145 dev_err(&pdev->dev, "Memory region busy\n");
146 ret = -EBUSY;
147 goto request_mem_failed;
148 }
Thiago Farinac38b9402010-01-01 16:42:34 -0500149 c67x00->hpi.base = ioremap(res->start, resource_size(res));
Peter Korsgaardb02b3712008-04-27 08:59:44 +0200150 if (!c67x00->hpi.base) {
151 dev_err(&pdev->dev, "Unable to map HPI registers\n");
152 ret = -EIO;
153 goto map_failed;
154 }
155
156 spin_lock_init(&c67x00->hpi.lock);
157 c67x00->hpi.regstep = pdata->hpi_regstep;
Jingoo Han720ce6e2013-07-30 17:25:19 +0900158 c67x00->pdata = dev_get_platdata(&pdev->dev);
Peter Korsgaardb02b3712008-04-27 08:59:44 +0200159 c67x00->pdev = pdev;
160
161 c67x00_ll_init(c67x00);
162 c67x00_ll_hpi_reg_init(c67x00);
163
164 ret = request_irq(res2->start, c67x00_irq, 0, pdev->name, c67x00);
165 if (ret) {
166 dev_err(&pdev->dev, "Cannot claim IRQ\n");
167 goto request_irq_failed;
168 }
169
170 ret = c67x00_ll_reset(c67x00);
171 if (ret) {
172 dev_err(&pdev->dev, "Device reset failed\n");
173 goto reset_failed;
174 }
175
176 for (i = 0; i < C67X00_SIES; i++)
177 c67x00_probe_sie(&c67x00->sie[i], c67x00, i);
178
179 platform_set_drvdata(pdev, c67x00);
180
181 return 0;
182
183 reset_failed:
184 free_irq(res2->start, c67x00);
185 request_irq_failed:
186 iounmap(c67x00->hpi.base);
187 map_failed:
Thiago Farinac38b9402010-01-01 16:42:34 -0500188 release_mem_region(res->start, resource_size(res));
Peter Korsgaardb02b3712008-04-27 08:59:44 +0200189 request_mem_failed:
190 kfree(c67x00);
191
192 return ret;
193}
194
Bill Pembertonfb4e98a2012-11-19 13:26:20 -0500195static int c67x00_drv_remove(struct platform_device *pdev)
Peter Korsgaardb02b3712008-04-27 08:59:44 +0200196{
197 struct c67x00_device *c67x00 = platform_get_drvdata(pdev);
198 struct resource *res;
199 int i;
200
201 for (i = 0; i < C67X00_SIES; i++)
202 c67x00_remove_sie(&c67x00->sie[i]);
203
204 c67x00_ll_release(c67x00);
205
206 res = platform_get_resource(pdev, IORESOURCE_IRQ, 0);
207 if (res)
208 free_irq(res->start, c67x00);
209
210 iounmap(c67x00->hpi.base);
211
212 res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
213 if (res)
Thiago Farinac38b9402010-01-01 16:42:34 -0500214 release_mem_region(res->start, resource_size(res));
Peter Korsgaardb02b3712008-04-27 08:59:44 +0200215
216 kfree(c67x00);
217
218 return 0;
219}
220
221static struct platform_driver c67x00_driver = {
222 .probe = c67x00_drv_probe,
Bill Pemberton76904172012-11-19 13:21:08 -0500223 .remove = c67x00_drv_remove,
Peter Korsgaardb02b3712008-04-27 08:59:44 +0200224 .driver = {
Peter Korsgaardb02b3712008-04-27 08:59:44 +0200225 .name = "c67x00",
226 },
227};
Peter Korsgaardb02b3712008-04-27 08:59:44 +0200228
Axel Lincc27c962011-11-27 20:16:27 +0800229module_platform_driver(c67x00_driver);
Peter Korsgaardb02b3712008-04-27 08:59:44 +0200230
231MODULE_AUTHOR("Peter Korsgaard, Jan Veldeman, Grant Likely");
232MODULE_DESCRIPTION("Cypress C67X00 USB Controller Driver");
233MODULE_LICENSE("GPL");
Axel Lincc27c962011-11-27 20:16:27 +0800234MODULE_ALIAS("platform:c67x00");