1 // SPDX-License-Identifier: GPL-2.0-only
2 /*
3 * linux/drivers/misc/xillybus_of.c
4 *
5 * Copyright 2011 Xillybus Ltd, http://xillybus.com
6 *
7 * Driver for the Xillybus FPGA/host framework using Open Firmware.
8 */
9
10 #include <linux/module.h>
11 #include <linux/device.h>
12 #include <linux/slab.h>
13 #include <linux/platform_device.h>
14 #include <linux/of.h>
15 #include <linux/err.h>
16 #include "xillybus.h"
17
18 MODULE_DESCRIPTION("Xillybus driver for Open Firmware");
19 MODULE_AUTHOR("Eli Billauer, Xillybus Ltd.");
20 MODULE_ALIAS("xillybus_of");
21 MODULE_LICENSE("GPL v2");
22
23 static const char xillyname[] = "xillybus_of";
24
25 /* Match table for of_platform binding */
26 static const struct of_device_id xillybus_of_match[] = {
27 { .compatible = "xillybus,xillybus-1.00.a", },
28 { .compatible = "xlnx,xillybus-1.00.a", }, /* Deprecated */
29 {}
30 };
31
32 MODULE_DEVICE_TABLE(of, xillybus_of_match);
33
xilly_dma_sync_single_for_cpu_of(struct xilly_endpoint * ep,dma_addr_t dma_handle,size_t size,int direction)34 static void xilly_dma_sync_single_for_cpu_of(struct xilly_endpoint *ep,
35 dma_addr_t dma_handle,
36 size_t size,
37 int direction)
38 {
39 dma_sync_single_for_cpu(ep->dev, dma_handle, size, direction);
40 }
41
xilly_dma_sync_single_for_device_of(struct xilly_endpoint * ep,dma_addr_t dma_handle,size_t size,int direction)42 static void xilly_dma_sync_single_for_device_of(struct xilly_endpoint *ep,
43 dma_addr_t dma_handle,
44 size_t size,
45 int direction)
46 {
47 dma_sync_single_for_device(ep->dev, dma_handle, size, direction);
48 }
49
xilly_dma_sync_single_nop(struct xilly_endpoint * ep,dma_addr_t dma_handle,size_t size,int direction)50 static void xilly_dma_sync_single_nop(struct xilly_endpoint *ep,
51 dma_addr_t dma_handle,
52 size_t size,
53 int direction)
54 {
55 }
56
xilly_of_unmap(void * ptr)57 static void xilly_of_unmap(void *ptr)
58 {
59 struct xilly_mapping *data = ptr;
60
61 dma_unmap_single(data->device, data->dma_addr,
62 data->size, data->direction);
63
64 kfree(ptr);
65 }
66
xilly_map_single_of(struct xilly_endpoint * ep,void * ptr,size_t size,int direction,dma_addr_t * ret_dma_handle)67 static int xilly_map_single_of(struct xilly_endpoint *ep,
68 void *ptr,
69 size_t size,
70 int direction,
71 dma_addr_t *ret_dma_handle
72 )
73 {
74 dma_addr_t addr;
75 struct xilly_mapping *this;
76
77 this = kzalloc(sizeof(*this), GFP_KERNEL);
78 if (!this)
79 return -ENOMEM;
80
81 addr = dma_map_single(ep->dev, ptr, size, direction);
82
83 if (dma_mapping_error(ep->dev, addr)) {
84 kfree(this);
85 return -ENODEV;
86 }
87
88 this->device = ep->dev;
89 this->dma_addr = addr;
90 this->size = size;
91 this->direction = direction;
92
93 *ret_dma_handle = addr;
94
95 return devm_add_action_or_reset(ep->dev, xilly_of_unmap, this);
96 }
97
98 static struct xilly_endpoint_hardware of_hw = {
99 .owner = THIS_MODULE,
100 .hw_sync_sgl_for_cpu = xilly_dma_sync_single_for_cpu_of,
101 .hw_sync_sgl_for_device = xilly_dma_sync_single_for_device_of,
102 .map_single = xilly_map_single_of,
103 };
104
105 static struct xilly_endpoint_hardware of_hw_coherent = {
106 .owner = THIS_MODULE,
107 .hw_sync_sgl_for_cpu = xilly_dma_sync_single_nop,
108 .hw_sync_sgl_for_device = xilly_dma_sync_single_nop,
109 .map_single = xilly_map_single_of,
110 };
111
xilly_drv_probe(struct platform_device * op)112 static int xilly_drv_probe(struct platform_device *op)
113 {
114 struct device *dev = &op->dev;
115 struct xilly_endpoint *endpoint;
116 int rc;
117 int irq;
118 struct xilly_endpoint_hardware *ephw = &of_hw;
119
120 if (of_property_read_bool(dev->of_node, "dma-coherent"))
121 ephw = &of_hw_coherent;
122
123 endpoint = xillybus_init_endpoint(NULL, dev, ephw);
124
125 if (!endpoint)
126 return -ENOMEM;
127
128 dev_set_drvdata(dev, endpoint);
129
130 endpoint->registers = devm_platform_ioremap_resource(op, 0);
131 if (IS_ERR(endpoint->registers))
132 return PTR_ERR(endpoint->registers);
133
134 irq = platform_get_irq(op, 0);
135
136 rc = devm_request_irq(dev, irq, xillybus_isr, 0, xillyname, endpoint);
137
138 if (rc) {
139 dev_err(endpoint->dev,
140 "Failed to register IRQ handler. Aborting.\n");
141 return -ENODEV;
142 }
143
144 return xillybus_endpoint_discovery(endpoint);
145 }
146
xilly_drv_remove(struct platform_device * op)147 static int xilly_drv_remove(struct platform_device *op)
148 {
149 struct device *dev = &op->dev;
150 struct xilly_endpoint *endpoint = dev_get_drvdata(dev);
151
152 xillybus_endpoint_remove(endpoint);
153
154 return 0;
155 }
156
157 static struct platform_driver xillybus_platform_driver = {
158 .probe = xilly_drv_probe,
159 .remove = xilly_drv_remove,
160 .driver = {
161 .name = xillyname,
162 .of_match_table = xillybus_of_match,
163 },
164 };
165
166 module_platform_driver(xillybus_platform_driver);
167