Eli Billauer | 48bae05 | 2013-06-24 18:55:47 +0300 | [diff] [blame] | 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> |
Eli Billauer | 9267462 | 2014-05-11 19:53:46 +0300 | [diff] [blame] | 22 | #include <linux/err.h> |
Eli Billauer | 48bae05 | 2013-06-24 18:55:47 +0300 | [diff] [blame] | 23 | #include "xillybus.h" |
| 24 | |
| 25 | MODULE_DESCRIPTION("Xillybus driver for Open Firmware"); |
| 26 | MODULE_AUTHOR("Eli Billauer, Xillybus Ltd."); |
| 27 | MODULE_VERSION("1.06"); |
| 28 | MODULE_ALIAS("xillybus_of"); |
| 29 | MODULE_LICENSE("GPL v2"); |
| 30 | |
Eli Billauer | e71042f | 2013-07-27 00:24:00 +0300 | [diff] [blame] | 31 | static const char xillyname[] = "xillybus_of"; |
| 32 | |
Eli Billauer | 48bae05 | 2013-06-24 18:55:47 +0300 | [diff] [blame] | 33 | /* Match table for of_platform binding */ |
Fabian Frederick | da2ff52 | 2015-03-16 20:17:13 +0100 | [diff] [blame] | 34 | static const struct of_device_id xillybus_of_match[] = { |
Eli Billauer | 24b33c9 | 2013-12-30 23:16:24 +0200 | [diff] [blame] | 35 | { .compatible = "xillybus,xillybus-1.00.a", }, |
| 36 | { .compatible = "xlnx,xillybus-1.00.a", }, /* Deprecated */ |
Eli Billauer | 48bae05 | 2013-06-24 18:55:47 +0300 | [diff] [blame] | 37 | {} |
| 38 | }; |
| 39 | |
| 40 | MODULE_DEVICE_TABLE(of, xillybus_of_match); |
| 41 | |
| 42 | static void xilly_dma_sync_single_for_cpu_of(struct xilly_endpoint *ep, |
| 43 | dma_addr_t dma_handle, |
| 44 | size_t size, |
| 45 | int direction) |
| 46 | { |
| 47 | dma_sync_single_for_cpu(ep->dev, dma_handle, size, direction); |
| 48 | } |
| 49 | |
| 50 | static void xilly_dma_sync_single_for_device_of(struct xilly_endpoint *ep, |
| 51 | dma_addr_t dma_handle, |
| 52 | size_t size, |
| 53 | int direction) |
| 54 | { |
| 55 | dma_sync_single_for_device(ep->dev, dma_handle, size, direction); |
| 56 | } |
| 57 | |
Eli Billauer | cc21255 | 2013-12-30 23:16:25 +0200 | [diff] [blame] | 58 | static void xilly_dma_sync_single_nop(struct xilly_endpoint *ep, |
| 59 | dma_addr_t dma_handle, |
| 60 | size_t size, |
| 61 | int direction) |
| 62 | { |
| 63 | } |
| 64 | |
Eli Billauer | 525be90 | 2014-06-21 14:07:12 +0300 | [diff] [blame] | 65 | static void xilly_of_unmap(void *ptr) |
| 66 | { |
| 67 | struct xilly_mapping *data = ptr; |
| 68 | |
| 69 | dma_unmap_single(data->device, data->dma_addr, |
| 70 | data->size, data->direction); |
| 71 | |
| 72 | kfree(ptr); |
| 73 | } |
| 74 | |
| 75 | static int xilly_map_single_of(struct xilly_endpoint *ep, |
| 76 | void *ptr, |
| 77 | size_t size, |
| 78 | int direction, |
| 79 | dma_addr_t *ret_dma_handle |
Eli Billauer | 48bae05 | 2013-06-24 18:55:47 +0300 | [diff] [blame] | 80 | ) |
| 81 | { |
Eli Billauer | 525be90 | 2014-06-21 14:07:12 +0300 | [diff] [blame] | 82 | dma_addr_t addr; |
| 83 | struct xilly_mapping *this; |
| 84 | int rc; |
Eli Billauer | 48bae05 | 2013-06-24 18:55:47 +0300 | [diff] [blame] | 85 | |
Eli Billauer | 525be90 | 2014-06-21 14:07:12 +0300 | [diff] [blame] | 86 | this = kzalloc(sizeof(*this), GFP_KERNEL); |
Eli Billauer | 48bae05 | 2013-06-24 18:55:47 +0300 | [diff] [blame] | 87 | if (!this) |
Eli Billauer | 525be90 | 2014-06-21 14:07:12 +0300 | [diff] [blame] | 88 | return -ENOMEM; |
Eli Billauer | 48bae05 | 2013-06-24 18:55:47 +0300 | [diff] [blame] | 89 | |
| 90 | addr = dma_map_single(ep->dev, ptr, size, direction); |
Eli Billauer | 48bae05 | 2013-06-24 18:55:47 +0300 | [diff] [blame] | 91 | |
| 92 | if (dma_mapping_error(ep->dev, addr)) { |
| 93 | kfree(this); |
Eli Billauer | 525be90 | 2014-06-21 14:07:12 +0300 | [diff] [blame] | 94 | return -ENODEV; |
Eli Billauer | 48bae05 | 2013-06-24 18:55:47 +0300 | [diff] [blame] | 95 | } |
| 96 | |
Eli Billauer | 525be90 | 2014-06-21 14:07:12 +0300 | [diff] [blame] | 97 | this->device = ep->dev; |
Eli Billauer | 48bae05 | 2013-06-24 18:55:47 +0300 | [diff] [blame] | 98 | this->dma_addr = addr; |
Eli Billauer | 48bae05 | 2013-06-24 18:55:47 +0300 | [diff] [blame] | 99 | this->size = size; |
Eli Billauer | 525be90 | 2014-06-21 14:07:12 +0300 | [diff] [blame] | 100 | this->direction = direction; |
Eli Billauer | 48bae05 | 2013-06-24 18:55:47 +0300 | [diff] [blame] | 101 | |
Eli Billauer | 525be90 | 2014-06-21 14:07:12 +0300 | [diff] [blame] | 102 | *ret_dma_handle = addr; |
Eli Billauer | 48bae05 | 2013-06-24 18:55:47 +0300 | [diff] [blame] | 103 | |
Eli Billauer | 525be90 | 2014-06-21 14:07:12 +0300 | [diff] [blame] | 104 | rc = devm_add_action(ep->dev, xilly_of_unmap, this); |
Eli Billauer | 48bae05 | 2013-06-24 18:55:47 +0300 | [diff] [blame] | 105 | |
Eli Billauer | 525be90 | 2014-06-21 14:07:12 +0300 | [diff] [blame] | 106 | if (rc) { |
| 107 | dma_unmap_single(ep->dev, addr, size, direction); |
| 108 | kfree(this); |
Eli Billauer | 40931bb | 2014-09-04 17:47:48 +0300 | [diff] [blame] | 109 | return rc; |
Eli Billauer | 525be90 | 2014-06-21 14:07:12 +0300 | [diff] [blame] | 110 | } |
| 111 | |
Eli Billauer | 40931bb | 2014-09-04 17:47:48 +0300 | [diff] [blame] | 112 | return 0; |
Eli Billauer | 48bae05 | 2013-06-24 18:55:47 +0300 | [diff] [blame] | 113 | } |
| 114 | |
| 115 | static struct xilly_endpoint_hardware of_hw = { |
| 116 | .owner = THIS_MODULE, |
Eli Billauer | 7ee9ded | 2013-07-31 11:22:43 +0300 | [diff] [blame] | 117 | .hw_sync_sgl_for_cpu = xilly_dma_sync_single_for_cpu_of, |
| 118 | .hw_sync_sgl_for_device = xilly_dma_sync_single_for_device_of, |
Eli Billauer | 48bae05 | 2013-06-24 18:55:47 +0300 | [diff] [blame] | 119 | .map_single = xilly_map_single_of, |
Eli Billauer | 48bae05 | 2013-06-24 18:55:47 +0300 | [diff] [blame] | 120 | }; |
| 121 | |
Eli Billauer | cc21255 | 2013-12-30 23:16:25 +0200 | [diff] [blame] | 122 | static struct xilly_endpoint_hardware of_hw_coherent = { |
| 123 | .owner = THIS_MODULE, |
| 124 | .hw_sync_sgl_for_cpu = xilly_dma_sync_single_nop, |
| 125 | .hw_sync_sgl_for_device = xilly_dma_sync_single_nop, |
| 126 | .map_single = xilly_map_single_of, |
Eli Billauer | cc21255 | 2013-12-30 23:16:25 +0200 | [diff] [blame] | 127 | }; |
| 128 | |
Eli Billauer | 48bae05 | 2013-06-24 18:55:47 +0300 | [diff] [blame] | 129 | static int xilly_drv_probe(struct platform_device *op) |
| 130 | { |
| 131 | struct device *dev = &op->dev; |
| 132 | struct xilly_endpoint *endpoint; |
Eli Billauer | 40931bb | 2014-09-04 17:47:48 +0300 | [diff] [blame] | 133 | int rc; |
Eli Billauer | 48bae05 | 2013-06-24 18:55:47 +0300 | [diff] [blame] | 134 | int irq; |
Eli Billauer | 9267462 | 2014-05-11 19:53:46 +0300 | [diff] [blame] | 135 | struct resource res; |
Eli Billauer | cc21255 | 2013-12-30 23:16:25 +0200 | [diff] [blame] | 136 | struct xilly_endpoint_hardware *ephw = &of_hw; |
Eli Billauer | 48bae05 | 2013-06-24 18:55:47 +0300 | [diff] [blame] | 137 | |
Eli Billauer | cc21255 | 2013-12-30 23:16:25 +0200 | [diff] [blame] | 138 | if (of_property_read_bool(dev->of_node, "dma-coherent")) |
| 139 | ephw = &of_hw_coherent; |
| 140 | |
| 141 | endpoint = xillybus_init_endpoint(NULL, dev, ephw); |
Eli Billauer | 48bae05 | 2013-06-24 18:55:47 +0300 | [diff] [blame] | 142 | |
| 143 | if (!endpoint) |
| 144 | return -ENOMEM; |
| 145 | |
| 146 | dev_set_drvdata(dev, endpoint); |
| 147 | |
Eli Billauer | 9267462 | 2014-05-11 19:53:46 +0300 | [diff] [blame] | 148 | rc = of_address_to_resource(dev->of_node, 0, &res); |
Eli Billauer | 9267462 | 2014-05-11 19:53:46 +0300 | [diff] [blame] | 149 | endpoint->registers = devm_ioremap_resource(dev, &res); |
Eli Billauer | 48bae05 | 2013-06-24 18:55:47 +0300 | [diff] [blame] | 150 | |
Eli Billauer | 9267462 | 2014-05-11 19:53:46 +0300 | [diff] [blame] | 151 | if (IS_ERR(endpoint->registers)) |
| 152 | return PTR_ERR(endpoint->registers); |
Eli Billauer | 48bae05 | 2013-06-24 18:55:47 +0300 | [diff] [blame] | 153 | |
| 154 | irq = irq_of_parse_and_map(dev->of_node, 0); |
| 155 | |
Eli Billauer | 9267462 | 2014-05-11 19:53:46 +0300 | [diff] [blame] | 156 | rc = devm_request_irq(dev, irq, xillybus_isr, 0, xillyname, endpoint); |
Eli Billauer | 48bae05 | 2013-06-24 18:55:47 +0300 | [diff] [blame] | 157 | |
| 158 | if (rc) { |
Eli Billauer | 1a2f9a9 | 2013-10-19 01:02:27 +0300 | [diff] [blame] | 159 | dev_err(endpoint->dev, |
| 160 | "Failed to register IRQ handler. Aborting.\n"); |
Eli Billauer | 9267462 | 2014-05-11 19:53:46 +0300 | [diff] [blame] | 161 | return -ENODEV; |
Eli Billauer | 48bae05 | 2013-06-24 18:55:47 +0300 | [diff] [blame] | 162 | } |
| 163 | |
Eli Billauer | 525be90 | 2014-06-21 14:07:12 +0300 | [diff] [blame] | 164 | return xillybus_endpoint_discovery(endpoint); |
Eli Billauer | 48bae05 | 2013-06-24 18:55:47 +0300 | [diff] [blame] | 165 | } |
| 166 | |
| 167 | static int xilly_drv_remove(struct platform_device *op) |
| 168 | { |
| 169 | struct device *dev = &op->dev; |
| 170 | struct xilly_endpoint *endpoint = dev_get_drvdata(dev); |
Eli Billauer | 48bae05 | 2013-06-24 18:55:47 +0300 | [diff] [blame] | 171 | |
| 172 | xillybus_endpoint_remove(endpoint); |
| 173 | |
Eli Billauer | 48bae05 | 2013-06-24 18:55:47 +0300 | [diff] [blame] | 174 | return 0; |
| 175 | } |
| 176 | |
| 177 | static struct platform_driver xillybus_platform_driver = { |
| 178 | .probe = xilly_drv_probe, |
| 179 | .remove = xilly_drv_remove, |
| 180 | .driver = { |
| 181 | .name = xillyname, |
Eli Billauer | 48bae05 | 2013-06-24 18:55:47 +0300 | [diff] [blame] | 182 | .of_match_table = xillybus_of_match, |
| 183 | }, |
| 184 | }; |
| 185 | |
Sachin Kamat | 7a620a6 | 2013-10-10 09:30:04 +0530 | [diff] [blame] | 186 | module_platform_driver(xillybus_platform_driver); |