Merge branch 'x86-build-for-linus' of git://git.kernel.org/pub/scm/linux/kernel/git...
[cascardo/linux.git] / drivers / staging / xillybus / xillybus_of.c
1 /*
2  * linux/drivers/misc/xillybus_of.c
3  *
4  * Copyright 2011 Xillybus Ltd, http://xillybus.com
5  *
6  * Driver for the Xillybus FPGA/host framework using Open Firmware.
7  *
8  * This program is free software; you can redistribute it and/or modify
9  * it under the smems of the GNU General Public License as published by
10  * the Free Software Foundation; version 2 of the License.
11  */
12
13 #include <linux/module.h>
14 #include <linux/device.h>
15 #include <linux/slab.h>
16 #include <linux/platform_device.h>
17 #include <linux/of.h>
18 #include <linux/of_irq.h>
19 #include <linux/of_address.h>
20 #include <linux/of_device.h>
21 #include <linux/of_platform.h>
22 #include "xillybus.h"
23
24 MODULE_DESCRIPTION("Xillybus driver for Open Firmware");
25 MODULE_AUTHOR("Eli Billauer, Xillybus Ltd.");
26 MODULE_VERSION("1.06");
27 MODULE_ALIAS("xillybus_of");
28 MODULE_LICENSE("GPL v2");
29
30 static const char xillyname[] = "xillybus_of";
31
32 /* Match table for of_platform binding */
33 static struct of_device_id xillybus_of_match[] = {
34         { .compatible = "xillybus,xillybus-1.00.a", },
35         { .compatible = "xlnx,xillybus-1.00.a", }, /* Deprecated */
36         {}
37 };
38
39 MODULE_DEVICE_TABLE(of, xillybus_of_match);
40
41 static void xilly_dma_sync_single_for_cpu_of(struct xilly_endpoint *ep,
42                                              dma_addr_t dma_handle,
43                                              size_t size,
44                                              int direction)
45 {
46         dma_sync_single_for_cpu(ep->dev, dma_handle, size, direction);
47 }
48
49 static void xilly_dma_sync_single_for_device_of(struct xilly_endpoint *ep,
50                                                 dma_addr_t dma_handle,
51                                                 size_t size,
52                                                 int direction)
53 {
54         dma_sync_single_for_device(ep->dev, dma_handle, size, direction);
55 }
56
57 static void xilly_dma_sync_single_nop(struct xilly_endpoint *ep,
58                                       dma_addr_t dma_handle,
59                                       size_t size,
60                                       int direction)
61 {
62 }
63
64 static dma_addr_t xilly_map_single_of(struct xilly_cleanup *mem,
65                                       struct xilly_endpoint *ep,
66                                       void *ptr,
67                                       size_t size,
68                                       int direction
69         )
70 {
71
72         dma_addr_t addr = 0;
73         struct xilly_dma *this;
74
75         this = kmalloc(sizeof(struct xilly_dma), GFP_KERNEL);
76         if (!this)
77                 return 0;
78
79         addr = dma_map_single(ep->dev, ptr, size, direction);
80         this->direction = direction;
81
82         if (dma_mapping_error(ep->dev, addr)) {
83                 kfree(this);
84                 return 0;
85         }
86
87         this->dma_addr = addr;
88         this->dev = ep->dev;
89         this->size = size;
90
91         list_add_tail(&this->node, &mem->to_unmap);
92
93         return addr;
94 }
95
96 static void xilly_unmap_single_of(struct xilly_dma *entry)
97 {
98         dma_unmap_single(entry->dev,
99                          entry->dma_addr,
100                          entry->size,
101                          entry->direction);
102 }
103
104 static struct xilly_endpoint_hardware of_hw = {
105         .owner = THIS_MODULE,
106         .hw_sync_sgl_for_cpu = xilly_dma_sync_single_for_cpu_of,
107         .hw_sync_sgl_for_device = xilly_dma_sync_single_for_device_of,
108         .map_single = xilly_map_single_of,
109         .unmap_single = xilly_unmap_single_of
110 };
111
112 static struct xilly_endpoint_hardware of_hw_coherent = {
113         .owner = THIS_MODULE,
114         .hw_sync_sgl_for_cpu = xilly_dma_sync_single_nop,
115         .hw_sync_sgl_for_device = xilly_dma_sync_single_nop,
116         .map_single = xilly_map_single_of,
117         .unmap_single = xilly_unmap_single_of
118 };
119
120 static int xilly_drv_probe(struct platform_device *op)
121 {
122         struct device *dev = &op->dev;
123         struct xilly_endpoint *endpoint;
124         int rc = 0;
125         int irq;
126         struct xilly_endpoint_hardware *ephw = &of_hw;
127
128         if (of_property_read_bool(dev->of_node, "dma-coherent"))
129                 ephw = &of_hw_coherent;
130
131         endpoint = xillybus_init_endpoint(NULL, dev, ephw);
132
133         if (!endpoint)
134                 return -ENOMEM;
135
136         dev_set_drvdata(dev, endpoint);
137
138         rc = of_address_to_resource(dev->of_node, 0, &endpoint->res);
139         if (rc) {
140                 dev_warn(endpoint->dev,
141                          "Failed to obtain device tree resource\n");
142                 goto failed_request_regions;
143         }
144
145         if  (!request_mem_region(endpoint->res.start,
146                                  resource_size(&endpoint->res), xillyname)) {
147                 dev_err(endpoint->dev,
148                         "request_mem_region failed. Aborting.\n");
149                 rc = -EBUSY;
150                 goto failed_request_regions;
151         }
152
153         endpoint->registers = of_iomap(dev->of_node, 0);
154         if (!endpoint->registers) {
155                 dev_err(endpoint->dev,
156                         "Failed to map I/O memory. Aborting.\n");
157                 rc = -EIO;
158                 goto failed_iomap0;
159         }
160
161         irq = irq_of_parse_and_map(dev->of_node, 0);
162
163         rc = request_irq(irq, xillybus_isr, 0, xillyname, endpoint);
164
165         if (rc) {
166                 dev_err(endpoint->dev,
167                         "Failed to register IRQ handler. Aborting.\n");
168                 rc = -ENODEV;
169                 goto failed_register_irq;
170         }
171
172         rc = xillybus_endpoint_discovery(endpoint);
173
174         if (!rc)
175                 return 0;
176
177         free_irq(irq, endpoint);
178
179 failed_register_irq:
180         iounmap(endpoint->registers);
181 failed_iomap0:
182         release_mem_region(endpoint->res.start,
183                            resource_size(&endpoint->res));
184
185 failed_request_regions:
186         xillybus_do_cleanup(&endpoint->cleanup, endpoint);
187
188         kfree(endpoint);
189         return rc;
190 }
191
192 static int xilly_drv_remove(struct platform_device *op)
193 {
194         struct device *dev = &op->dev;
195         struct xilly_endpoint *endpoint = dev_get_drvdata(dev);
196         int irq = irq_of_parse_and_map(dev->of_node, 0);
197
198         xillybus_endpoint_remove(endpoint);
199
200         free_irq(irq, endpoint);
201
202         iounmap(endpoint->registers);
203         release_mem_region(endpoint->res.start,
204                            resource_size(&endpoint->res));
205
206         xillybus_do_cleanup(&endpoint->cleanup, endpoint);
207
208         kfree(endpoint);
209
210         return 0;
211 }
212
213 static struct platform_driver xillybus_platform_driver = {
214         .probe = xilly_drv_probe,
215         .remove = xilly_drv_remove,
216         .driver = {
217                 .name = xillyname,
218                 .owner = THIS_MODULE,
219                 .of_match_table = xillybus_of_match,
220         },
221 };
222
223 module_platform_driver(xillybus_platform_driver);