]> git.karo-electronics.de Git - karo-tx-linux.git/blob - drivers/staging/xillybus/xillybus_of.c
Linux 3.12-rc6
[karo-tx-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 = "xlnx,xillybus-1.00.a", },
35         {}
36 };
37
38 MODULE_DEVICE_TABLE(of, xillybus_of_match);
39
40 static void xilly_dma_sync_single_for_cpu_of(struct xilly_endpoint *ep,
41                                              dma_addr_t dma_handle,
42                                              size_t size,
43                                              int direction)
44 {
45         dma_sync_single_for_cpu(ep->dev, dma_handle, size, direction);
46 }
47
48 static void xilly_dma_sync_single_for_device_of(struct xilly_endpoint *ep,
49                                                 dma_addr_t dma_handle,
50                                                 size_t size,
51                                                 int direction)
52 {
53         dma_sync_single_for_device(ep->dev, dma_handle, size, direction);
54 }
55
56 static dma_addr_t xilly_map_single_of(struct xilly_cleanup *mem,
57                                       struct xilly_endpoint *ep,
58                                       void *ptr,
59                                       size_t size,
60                                       int direction
61         )
62 {
63
64         dma_addr_t addr = 0;
65         struct xilly_dma *this;
66
67         this = kmalloc(sizeof(struct xilly_dma), GFP_KERNEL);
68         if (!this)
69                 return 0;
70
71         addr = dma_map_single(ep->dev, ptr, size, direction);
72         this->direction = direction;
73
74         if (dma_mapping_error(ep->dev, addr)) {
75                 kfree(this);
76                 return 0;
77         }
78
79         this->dma_addr = addr;
80         this->dev = ep->dev;
81         this->size = size;
82
83         list_add_tail(&this->node, &mem->to_unmap);
84
85         return addr;
86 }
87
88 static void xilly_unmap_single_of(struct xilly_dma *entry)
89 {
90         dma_unmap_single(entry->dev,
91                          entry->dma_addr,
92                          entry->size,
93                          entry->direction);
94 }
95
96 static struct xilly_endpoint_hardware of_hw = {
97         .owner = THIS_MODULE,
98         .hw_sync_sgl_for_cpu = xilly_dma_sync_single_for_cpu_of,
99         .hw_sync_sgl_for_device = xilly_dma_sync_single_for_device_of,
100         .map_single = xilly_map_single_of,
101         .unmap_single = xilly_unmap_single_of
102 };
103
104 static int xilly_drv_probe(struct platform_device *op)
105 {
106         struct device *dev = &op->dev;
107         struct xilly_endpoint *endpoint;
108         int rc = 0;
109         int irq;
110
111         endpoint = xillybus_init_endpoint(NULL, dev, &of_hw);
112
113         if (!endpoint)
114                 return -ENOMEM;
115
116         dev_set_drvdata(dev, endpoint);
117
118         rc = of_address_to_resource(dev->of_node, 0, &endpoint->res);
119         if (rc) {
120                 pr_warn("xillybus: Failed to obtain device tree "
121                         "resource\n");
122                 goto failed_request_regions;
123         }
124
125         if  (!request_mem_region(endpoint->res.start,
126                                  resource_size(&endpoint->res), xillyname)) {
127                 pr_err("xillybus: request_mem_region failed. Aborting.\n");
128                 rc = -EBUSY;
129                 goto failed_request_regions;
130         }
131
132         endpoint->registers = of_iomap(dev->of_node, 0);
133
134         if (!endpoint->registers) {
135                 pr_err("xillybus: Failed to map I/O memory. Aborting.\n");
136                 goto failed_iomap0;
137         }
138
139         irq = irq_of_parse_and_map(dev->of_node, 0);
140
141         rc = request_irq(irq, xillybus_isr, 0, xillyname, endpoint);
142
143         if (rc) {
144                 pr_err("xillybus: Failed to register IRQ handler. "
145                        "Aborting.\n");
146                 rc = -ENODEV;
147                 goto failed_register_irq;
148         }
149
150         rc = xillybus_endpoint_discovery(endpoint);
151
152         if (!rc)
153                 return 0;
154
155         free_irq(irq, endpoint);
156
157 failed_register_irq:
158         iounmap(endpoint->registers);
159 failed_iomap0:
160         release_mem_region(endpoint->res.start,
161                            resource_size(&endpoint->res));
162
163 failed_request_regions:
164         xillybus_do_cleanup(&endpoint->cleanup, endpoint);
165
166         kfree(endpoint);
167         return rc;
168 }
169
170 static int xilly_drv_remove(struct platform_device *op)
171 {
172         struct device *dev = &op->dev;
173         struct xilly_endpoint *endpoint = dev_get_drvdata(dev);
174         int irq = irq_of_parse_and_map(dev->of_node, 0);
175
176         xillybus_endpoint_remove(endpoint);
177
178         free_irq(irq, endpoint);
179
180         iounmap(endpoint->registers);
181         release_mem_region(endpoint->res.start,
182                            resource_size(&endpoint->res));
183
184         xillybus_do_cleanup(&endpoint->cleanup, endpoint);
185
186         kfree(endpoint);
187
188         return 0;
189 }
190
191 static struct platform_driver xillybus_platform_driver = {
192         .probe = xilly_drv_probe,
193         .remove = xilly_drv_remove,
194         .driver = {
195                 .name = xillyname,
196                 .owner = THIS_MODULE,
197                 .of_match_table = xillybus_of_match,
198         },
199 };
200
201 static int __init xillybus_of_init(void)
202 {
203         return platform_driver_register(&xillybus_platform_driver);
204 }
205
206 static void __exit xillybus_of_exit(void)
207 {
208         platform_driver_unregister(&xillybus_platform_driver);
209 }
210
211 module_init(xillybus_of_init);
212 module_exit(xillybus_of_exit);