1 /*
2  * amd5536udc_pci.c -- AMD 5536 UDC high/full speed USB device controller
3  *
4  * Copyright (C) 2005-2007 AMD (http://www.amd.com)
5  * Author: Thomas Dahlmann
6  *
7  * This program is free software; you can redistribute it and/or modify
8  * it under the terms of the GNU General Public License as published by
9  * the Free Software Foundation; either version 2 of the License, or
10  * (at your option) any later version.
11  */
12 
13 /*
14  * The AMD5536 UDC is part of the x86 southbridge AMD Geode CS5536.
15  * It is a USB Highspeed DMA capable USB device controller. Beside ep0 it
16  * provides 4 IN and 4 OUT endpoints (bulk or interrupt type).
17  *
18  * Make sure that UDC is assigned to port 4 by BIOS settings (port can also
19  * be used as host port) and UOC bits PAD_EN and APU are set (should be done
20  * by BIOS init).
21  *
22  * UDC DMA requires 32-bit aligned buffers so DMA with gadget ether does not
23  * work without updating NET_IP_ALIGN. Or PIO mode (module param "use_dma=0")
24  * can be used with gadget ether.
25  *
26  * This file does pci device registration, and the core driver implementation
27  * is done in amd5536udc.c
28  *
29  * The driver is split so as to use the core UDC driver which is based on
30  * Synopsys device controller IP (different than HS OTG IP) in UDCs
31  * integrated to SoC platforms.
32  *
33  */
34 
35 /* Driver strings */
36 #define UDC_MOD_DESCRIPTION		"AMD 5536 UDC - USB Device Controller"
37 
38 /* system */
39 #include <linux/device.h>
40 #include <linux/dmapool.h>
41 #include <linux/interrupt.h>
42 #include <linux/io.h>
43 #include <linux/irq.h>
44 #include <linux/module.h>
45 #include <linux/moduleparam.h>
46 #include <linux/prefetch.h>
47 #include <linux/pci.h>
48 
49 /* udc specific */
50 #include "amd5536udc.h"
51 
52 /* pointer to device object */
53 static struct udc *udc;
54 
55 /* description */
56 static const char mod_desc[] = UDC_MOD_DESCRIPTION;
57 static const char name[] = "amd5536udc-pci";
58 
59 /* Reset all pci context */
60 static void udc_pci_remove(struct pci_dev *pdev)
61 {
62 	struct udc		*dev;
63 
64 	dev = pci_get_drvdata(pdev);
65 
66 	usb_del_gadget_udc(&udc->gadget);
67 	/* gadget driver must not be registered */
68 	if (WARN_ON(dev->driver))
69 		return;
70 
71 	/* dma pool cleanup */
72 	free_dma_pools(dev);
73 
74 	/* reset controller */
75 	writel(AMD_BIT(UDC_DEVCFG_SOFTRESET), &dev->regs->cfg);
76 	free_irq(pdev->irq, dev);
77 	iounmap(dev->virt_addr);
78 	release_mem_region(pci_resource_start(pdev, 0),
79 			   pci_resource_len(pdev, 0));
80 	pci_disable_device(pdev);
81 
82 	udc_remove(dev);
83 }
84 
85 /* Called by pci bus driver to init pci context */
86 static int udc_pci_probe(
87 	struct pci_dev *pdev,
88 	const struct pci_device_id *id
89 )
90 {
91 	struct udc		*dev;
92 	unsigned long		resource;
93 	unsigned long		len;
94 	int			retval = 0;
95 
96 	/* one udc only */
97 	if (udc) {
98 		dev_dbg(&pdev->dev, "already probed\n");
99 		return -EBUSY;
100 	}
101 
102 	/* init */
103 	dev = kzalloc(sizeof(struct udc), GFP_KERNEL);
104 	if (!dev)
105 		return -ENOMEM;
106 
107 	/* pci setup */
108 	if (pci_enable_device(pdev) < 0) {
109 		retval = -ENODEV;
110 		goto err_pcidev;
111 	}
112 
113 	/* PCI resource allocation */
114 	resource = pci_resource_start(pdev, 0);
115 	len = pci_resource_len(pdev, 0);
116 
117 	if (!request_mem_region(resource, len, name)) {
118 		dev_dbg(&pdev->dev, "pci device used already\n");
119 		retval = -EBUSY;
120 		goto err_memreg;
121 	}
122 
123 	dev->virt_addr = ioremap_nocache(resource, len);
124 	if (!dev->virt_addr) {
125 		dev_dbg(&pdev->dev, "start address cannot be mapped\n");
126 		retval = -EFAULT;
127 		goto err_ioremap;
128 	}
129 
130 	if (!pdev->irq) {
131 		dev_err(&pdev->dev, "irq not set\n");
132 		retval = -ENODEV;
133 		goto err_irq;
134 	}
135 
136 	spin_lock_init(&dev->lock);
137 	/* udc csr registers base */
138 	dev->csr = dev->virt_addr + UDC_CSR_ADDR;
139 	/* dev registers base */
140 	dev->regs = dev->virt_addr + UDC_DEVCFG_ADDR;
141 	/* ep registers base */
142 	dev->ep_regs = dev->virt_addr + UDC_EPREGS_ADDR;
143 	/* fifo's base */
144 	dev->rxfifo = (u32 __iomem *)(dev->virt_addr + UDC_RXFIFO_ADDR);
145 	dev->txfifo = (u32 __iomem *)(dev->virt_addr + UDC_TXFIFO_ADDR);
146 
147 	if (request_irq(pdev->irq, udc_irq, IRQF_SHARED, name, dev) != 0) {
148 		dev_dbg(&pdev->dev, "request_irq(%d) fail\n", pdev->irq);
149 		retval = -EBUSY;
150 		goto err_irq;
151 	}
152 
153 	pci_set_drvdata(pdev, dev);
154 
155 	/* chip revision for Hs AMD5536 */
156 	dev->chiprev = pdev->revision;
157 
158 	pci_set_master(pdev);
159 	pci_try_set_mwi(pdev);
160 
161 	/* init dma pools */
162 	if (use_dma) {
163 		retval = init_dma_pools(dev);
164 		if (retval != 0)
165 			goto err_dma;
166 	}
167 
168 	dev->phys_addr = resource;
169 	dev->irq = pdev->irq;
170 	dev->pdev = pdev;
171 	dev->dev = &pdev->dev;
172 
173 	/* general probing */
174 	if (udc_probe(dev)) {
175 		retval = -ENODEV;
176 		goto err_probe;
177 	}
178 	return 0;
179 
180 err_probe:
181 	if (use_dma)
182 		free_dma_pools(dev);
183 err_dma:
184 	free_irq(pdev->irq, dev);
185 err_irq:
186 	iounmap(dev->virt_addr);
187 err_ioremap:
188 	release_mem_region(resource, len);
189 err_memreg:
190 	pci_disable_device(pdev);
191 err_pcidev:
192 	kfree(dev);
193 	return retval;
194 }
195 
196 /* PCI device parameters */
197 static const struct pci_device_id pci_id[] = {
198 	{
199 		PCI_DEVICE(PCI_VENDOR_ID_AMD, 0x2096),
200 		.class =	PCI_CLASS_SERIAL_USB_DEVICE,
201 		.class_mask =	0xffffffff,
202 	},
203 	{},
204 };
205 MODULE_DEVICE_TABLE(pci, pci_id);
206 
207 /* PCI functions */
208 static struct pci_driver udc_pci_driver = {
209 	.name =		(char *) name,
210 	.id_table =	pci_id,
211 	.probe =	udc_pci_probe,
212 	.remove =	udc_pci_remove,
213 };
214 module_pci_driver(udc_pci_driver);
215 
216 MODULE_DESCRIPTION(UDC_MOD_DESCRIPTION);
217 MODULE_AUTHOR("Thomas Dahlmann");
218 MODULE_LICENSE("GPL");
219