15fd54aceSGreg Kroah-Hartman // SPDX-License-Identifier: GPL-2.0+
2b02b371eSPeter Korsgaard /*
3b02b371eSPeter Korsgaard * c67x00-drv.c: Cypress C67X00 USB Common infrastructure
4b02b371eSPeter Korsgaard *
5b02b371eSPeter Korsgaard * Copyright (C) 2006-2008 Barco N.V.
6b02b371eSPeter Korsgaard * Derived from the Cypress cy7c67200/300 ezusb linux driver and
7b02b371eSPeter Korsgaard * based on multiple host controller drivers inside the linux kernel.
8b02b371eSPeter Korsgaard */
9b02b371eSPeter Korsgaard
10b02b371eSPeter Korsgaard /*
11b02b371eSPeter Korsgaard * This file implements the common infrastructure for using the c67x00.
12b02b371eSPeter Korsgaard * It is both the link between the platform configuration and subdrivers and
13b02b371eSPeter Korsgaard * the link between the common hardware parts and the subdrivers (e.g.
14b02b371eSPeter Korsgaard * interrupt handling).
15b02b371eSPeter Korsgaard *
1625985edcSLucas De Marchi * The c67x00 has 2 SIE's (serial interface engine) which can be configured
17b02b371eSPeter Korsgaard * to be host, device or OTG (with some limitations, E.G. only SIE1 can be OTG).
18b02b371eSPeter Korsgaard *
19b02b371eSPeter Korsgaard * Depending on the platform configuration, the SIE's are created and
20b02b371eSPeter Korsgaard * the corresponding subdriver is initialized (c67x00_probe_sie).
21b02b371eSPeter Korsgaard */
22b02b371eSPeter Korsgaard
23b02b371eSPeter Korsgaard #include <linux/device.h>
24b02b371eSPeter Korsgaard #include <linux/io.h>
25b02b371eSPeter Korsgaard #include <linux/list.h>
265a0e3ad6STejun Heo #include <linux/slab.h>
276eb0de82SPaul Gortmaker #include <linux/module.h>
28b02b371eSPeter Korsgaard #include <linux/usb.h>
29b02b371eSPeter Korsgaard #include <linux/usb/c67x00.h>
30b02b371eSPeter Korsgaard
31b02b371eSPeter Korsgaard #include "c67x00.h"
32e9b29ffcSPeter Korsgaard #include "c67x00-hcd.h"
33b02b371eSPeter Korsgaard
c67x00_probe_sie(struct c67x00_sie * sie,struct c67x00_device * dev,int sie_num)34b02b371eSPeter Korsgaard static void c67x00_probe_sie(struct c67x00_sie *sie,
35b02b371eSPeter Korsgaard struct c67x00_device *dev, int sie_num)
36b02b371eSPeter Korsgaard {
37b02b371eSPeter Korsgaard spin_lock_init(&sie->lock);
38b02b371eSPeter Korsgaard sie->dev = dev;
39b02b371eSPeter Korsgaard sie->sie_num = sie_num;
40b02b371eSPeter Korsgaard sie->mode = c67x00_sie_config(dev->pdata->sie_config, sie_num);
41b02b371eSPeter Korsgaard
42b02b371eSPeter Korsgaard switch (sie->mode) {
43e9b29ffcSPeter Korsgaard case C67X00_SIE_HOST:
44e9b29ffcSPeter Korsgaard c67x00_hcd_probe(sie);
45e9b29ffcSPeter Korsgaard break;
46e9b29ffcSPeter Korsgaard
47b02b371eSPeter Korsgaard case C67X00_SIE_UNUSED:
48b02b371eSPeter Korsgaard dev_info(sie_dev(sie),
49b02b371eSPeter Korsgaard "Not using SIE %d as requested\n", sie->sie_num);
50b02b371eSPeter Korsgaard break;
51b02b371eSPeter Korsgaard
52b02b371eSPeter Korsgaard default:
53b02b371eSPeter Korsgaard dev_err(sie_dev(sie),
54b02b371eSPeter Korsgaard "Unsupported configuration: 0x%x for SIE %d\n",
55b02b371eSPeter Korsgaard sie->mode, sie->sie_num);
56b02b371eSPeter Korsgaard break;
57b02b371eSPeter Korsgaard }
58b02b371eSPeter Korsgaard }
59b02b371eSPeter Korsgaard
c67x00_remove_sie(struct c67x00_sie * sie)60b02b371eSPeter Korsgaard static void c67x00_remove_sie(struct c67x00_sie *sie)
61b02b371eSPeter Korsgaard {
62e9b29ffcSPeter Korsgaard switch (sie->mode) {
63e9b29ffcSPeter Korsgaard case C67X00_SIE_HOST:
64e9b29ffcSPeter Korsgaard c67x00_hcd_remove(sie);
65e9b29ffcSPeter Korsgaard break;
66e9b29ffcSPeter Korsgaard
67e9b29ffcSPeter Korsgaard default:
68e9b29ffcSPeter Korsgaard break;
69e9b29ffcSPeter Korsgaard }
70b02b371eSPeter Korsgaard }
71b02b371eSPeter Korsgaard
c67x00_irq(int irq,void * __dev)72b02b371eSPeter Korsgaard static irqreturn_t c67x00_irq(int irq, void *__dev)
73b02b371eSPeter Korsgaard {
74b02b371eSPeter Korsgaard struct c67x00_device *c67x00 = __dev;
75b02b371eSPeter Korsgaard struct c67x00_sie *sie;
76b02b371eSPeter Korsgaard u16 msg, int_status;
77b02b371eSPeter Korsgaard int i, count = 8;
78b02b371eSPeter Korsgaard
79b02b371eSPeter Korsgaard int_status = c67x00_ll_hpi_status(c67x00);
80b02b371eSPeter Korsgaard if (!int_status)
81b02b371eSPeter Korsgaard return IRQ_NONE;
82b02b371eSPeter Korsgaard
83b02b371eSPeter Korsgaard while (int_status != 0 && (count-- >= 0)) {
84b02b371eSPeter Korsgaard c67x00_ll_irq(c67x00, int_status);
85b02b371eSPeter Korsgaard for (i = 0; i < C67X00_SIES; i++) {
86b02b371eSPeter Korsgaard sie = &c67x00->sie[i];
87b02b371eSPeter Korsgaard msg = 0;
88b02b371eSPeter Korsgaard if (int_status & SIEMSG_FLG(i))
89b02b371eSPeter Korsgaard msg = c67x00_ll_fetch_siemsg(c67x00, i);
90b02b371eSPeter Korsgaard if (sie->irq)
91b02b371eSPeter Korsgaard sie->irq(sie, int_status, msg);
92b02b371eSPeter Korsgaard }
93b02b371eSPeter Korsgaard int_status = c67x00_ll_hpi_status(c67x00);
94b02b371eSPeter Korsgaard }
95b02b371eSPeter Korsgaard
96b02b371eSPeter Korsgaard if (int_status)
97b02b371eSPeter Korsgaard dev_warn(&c67x00->pdev->dev, "Not all interrupts handled! "
98b02b371eSPeter Korsgaard "status = 0x%04x\n", int_status);
99b02b371eSPeter Korsgaard
100b02b371eSPeter Korsgaard return IRQ_HANDLED;
101b02b371eSPeter Korsgaard }
102b02b371eSPeter Korsgaard
103b02b371eSPeter Korsgaard /* ------------------------------------------------------------------------- */
104b02b371eSPeter Korsgaard
c67x00_drv_probe(struct platform_device * pdev)10541ac7b3aSBill Pemberton static int c67x00_drv_probe(struct platform_device *pdev)
106b02b371eSPeter Korsgaard {
107b02b371eSPeter Korsgaard struct c67x00_device *c67x00;
108b02b371eSPeter Korsgaard struct c67x00_platform_data *pdata;
109b02b371eSPeter Korsgaard struct resource *res, *res2;
110b02b371eSPeter Korsgaard int ret, i;
111b02b371eSPeter Korsgaard
112b02b371eSPeter Korsgaard res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
113b02b371eSPeter Korsgaard if (!res)
114b02b371eSPeter Korsgaard return -ENODEV;
115b02b371eSPeter Korsgaard
116b02b371eSPeter Korsgaard res2 = platform_get_resource(pdev, IORESOURCE_IRQ, 0);
117b02b371eSPeter Korsgaard if (!res2)
118b02b371eSPeter Korsgaard return -ENODEV;
119b02b371eSPeter Korsgaard
120720ce6e4SJingoo Han pdata = dev_get_platdata(&pdev->dev);
121b02b371eSPeter Korsgaard if (!pdata)
122b02b371eSPeter Korsgaard return -ENODEV;
123b02b371eSPeter Korsgaard
124b02b371eSPeter Korsgaard c67x00 = kzalloc(sizeof(*c67x00), GFP_KERNEL);
125b02b371eSPeter Korsgaard if (!c67x00)
126b02b371eSPeter Korsgaard return -ENOMEM;
127b02b371eSPeter Korsgaard
128c38b9401SThiago Farina if (!request_mem_region(res->start, resource_size(res),
129b02b371eSPeter Korsgaard pdev->name)) {
130b02b371eSPeter Korsgaard dev_err(&pdev->dev, "Memory region busy\n");
131b02b371eSPeter Korsgaard ret = -EBUSY;
132b02b371eSPeter Korsgaard goto request_mem_failed;
133b02b371eSPeter Korsgaard }
134c38b9401SThiago Farina c67x00->hpi.base = ioremap(res->start, resource_size(res));
135b02b371eSPeter Korsgaard if (!c67x00->hpi.base) {
136b02b371eSPeter Korsgaard dev_err(&pdev->dev, "Unable to map HPI registers\n");
137b02b371eSPeter Korsgaard ret = -EIO;
138b02b371eSPeter Korsgaard goto map_failed;
139b02b371eSPeter Korsgaard }
140b02b371eSPeter Korsgaard
141b02b371eSPeter Korsgaard spin_lock_init(&c67x00->hpi.lock);
142b02b371eSPeter Korsgaard c67x00->hpi.regstep = pdata->hpi_regstep;
143720ce6e4SJingoo Han c67x00->pdata = dev_get_platdata(&pdev->dev);
144b02b371eSPeter Korsgaard c67x00->pdev = pdev;
145b02b371eSPeter Korsgaard
146b02b371eSPeter Korsgaard c67x00_ll_init(c67x00);
147b02b371eSPeter Korsgaard c67x00_ll_hpi_reg_init(c67x00);
148b02b371eSPeter Korsgaard
149b02b371eSPeter Korsgaard ret = request_irq(res2->start, c67x00_irq, 0, pdev->name, c67x00);
150b02b371eSPeter Korsgaard if (ret) {
151b02b371eSPeter Korsgaard dev_err(&pdev->dev, "Cannot claim IRQ\n");
152b02b371eSPeter Korsgaard goto request_irq_failed;
153b02b371eSPeter Korsgaard }
154b02b371eSPeter Korsgaard
155b02b371eSPeter Korsgaard ret = c67x00_ll_reset(c67x00);
156b02b371eSPeter Korsgaard if (ret) {
157b02b371eSPeter Korsgaard dev_err(&pdev->dev, "Device reset failed\n");
158b02b371eSPeter Korsgaard goto reset_failed;
159b02b371eSPeter Korsgaard }
160b02b371eSPeter Korsgaard
161b02b371eSPeter Korsgaard for (i = 0; i < C67X00_SIES; i++)
162b02b371eSPeter Korsgaard c67x00_probe_sie(&c67x00->sie[i], c67x00, i);
163b02b371eSPeter Korsgaard
164b02b371eSPeter Korsgaard platform_set_drvdata(pdev, c67x00);
165b02b371eSPeter Korsgaard
166b02b371eSPeter Korsgaard return 0;
167b02b371eSPeter Korsgaard
168b02b371eSPeter Korsgaard reset_failed:
169b02b371eSPeter Korsgaard free_irq(res2->start, c67x00);
170b02b371eSPeter Korsgaard request_irq_failed:
171b02b371eSPeter Korsgaard iounmap(c67x00->hpi.base);
172b02b371eSPeter Korsgaard map_failed:
173c38b9401SThiago Farina release_mem_region(res->start, resource_size(res));
174b02b371eSPeter Korsgaard request_mem_failed:
175b02b371eSPeter Korsgaard kfree(c67x00);
176b02b371eSPeter Korsgaard
177b02b371eSPeter Korsgaard return ret;
178b02b371eSPeter Korsgaard }
179b02b371eSPeter Korsgaard
c67x00_drv_remove(struct platform_device * pdev)180*e8fa2dd9SUwe Kleine-König static void c67x00_drv_remove(struct platform_device *pdev)
181b02b371eSPeter Korsgaard {
182b02b371eSPeter Korsgaard struct c67x00_device *c67x00 = platform_get_drvdata(pdev);
183b02b371eSPeter Korsgaard struct resource *res;
184b02b371eSPeter Korsgaard int i;
185b02b371eSPeter Korsgaard
186b02b371eSPeter Korsgaard for (i = 0; i < C67X00_SIES; i++)
187b02b371eSPeter Korsgaard c67x00_remove_sie(&c67x00->sie[i]);
188b02b371eSPeter Korsgaard
189b02b371eSPeter Korsgaard c67x00_ll_release(c67x00);
190b02b371eSPeter Korsgaard
191b02b371eSPeter Korsgaard res = platform_get_resource(pdev, IORESOURCE_IRQ, 0);
192b02b371eSPeter Korsgaard free_irq(res->start, c67x00);
193b02b371eSPeter Korsgaard
194b02b371eSPeter Korsgaard iounmap(c67x00->hpi.base);
195b02b371eSPeter Korsgaard
196b02b371eSPeter Korsgaard res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
197c38b9401SThiago Farina release_mem_region(res->start, resource_size(res));
198b02b371eSPeter Korsgaard
199b02b371eSPeter Korsgaard kfree(c67x00);
200b02b371eSPeter Korsgaard }
201b02b371eSPeter Korsgaard
202b02b371eSPeter Korsgaard static struct platform_driver c67x00_driver = {
203b02b371eSPeter Korsgaard .probe = c67x00_drv_probe,
204*e8fa2dd9SUwe Kleine-König .remove_new = c67x00_drv_remove,
205b02b371eSPeter Korsgaard .driver = {
206b02b371eSPeter Korsgaard .name = "c67x00",
207b02b371eSPeter Korsgaard },
208b02b371eSPeter Korsgaard };
209b02b371eSPeter Korsgaard
210cc27c96cSAxel Lin module_platform_driver(c67x00_driver);
211b02b371eSPeter Korsgaard
212b02b371eSPeter Korsgaard MODULE_AUTHOR("Peter Korsgaard, Jan Veldeman, Grant Likely");
213b02b371eSPeter Korsgaard MODULE_DESCRIPTION("Cypress C67X00 USB Controller Driver");
214b02b371eSPeter Korsgaard MODULE_LICENSE("GPL");
215cc27c96cSAxel Lin MODULE_ALIAS("platform:c67x00");
216