Peter Ujfalusi | a074ae3 | 2015-04-09 12:35:49 +0300 | [diff] [blame] | 1 | /* |
| 2 | * Copyright (C) 2015 Texas Instruments Incorporated - http://www.ti.com |
| 3 | * Author: Peter Ujfalusi <peter.ujfalusi@ti.com> |
| 4 | * |
| 5 | * This program is free software; you can redistribute it and/or modify |
| 6 | * it under the terms of the GNU General Public License version 2 as |
| 7 | * published by the Free Software Foundation. |
| 8 | * |
| 9 | */ |
| 10 | #include <linux/slab.h> |
| 11 | #include <linux/err.h> |
| 12 | #include <linux/init.h> |
| 13 | #include <linux/list.h> |
| 14 | #include <linux/io.h> |
Peter Ujfalusi | a074ae3 | 2015-04-09 12:35:49 +0300 | [diff] [blame] | 15 | #include <linux/of_address.h> |
| 16 | #include <linux/of_device.h> |
| 17 | #include <linux/of_dma.h> |
| 18 | |
Peter Ujfalusi | 42dbdcc | 2015-10-16 10:18:08 +0300 | [diff] [blame] | 19 | #define TI_XBAR_DRA7 0 |
| 20 | #define TI_XBAR_AM335X 1 |
Peter Ujfalusi | 5f9367a | 2016-09-21 15:41:32 +0300 | [diff] [blame] | 21 | static const u32 ti_xbar_type[] = { |
| 22 | [TI_XBAR_DRA7] = TI_XBAR_DRA7, |
| 23 | [TI_XBAR_AM335X] = TI_XBAR_AM335X, |
| 24 | }; |
Peter Ujfalusi | 42dbdcc | 2015-10-16 10:18:08 +0300 | [diff] [blame] | 25 | |
| 26 | static const struct of_device_id ti_dma_xbar_match[] = { |
| 27 | { |
| 28 | .compatible = "ti,dra7-dma-crossbar", |
Peter Ujfalusi | 5f9367a | 2016-09-21 15:41:32 +0300 | [diff] [blame] | 29 | .data = &ti_xbar_type[TI_XBAR_DRA7], |
Peter Ujfalusi | 42dbdcc | 2015-10-16 10:18:08 +0300 | [diff] [blame] | 30 | }, |
| 31 | { |
| 32 | .compatible = "ti,am335x-edma-crossbar", |
Peter Ujfalusi | 5f9367a | 2016-09-21 15:41:32 +0300 | [diff] [blame] | 33 | .data = &ti_xbar_type[TI_XBAR_AM335X], |
Peter Ujfalusi | 42dbdcc | 2015-10-16 10:18:08 +0300 | [diff] [blame] | 34 | }, |
| 35 | {}, |
| 36 | }; |
| 37 | |
| 38 | /* Crossbar on AM335x/AM437x family */ |
| 39 | #define TI_AM335X_XBAR_LINES 64 |
| 40 | |
| 41 | struct ti_am335x_xbar_data { |
| 42 | void __iomem *iomem; |
| 43 | |
| 44 | struct dma_router dmarouter; |
| 45 | |
| 46 | u32 xbar_events; /* maximum number of events to select in xbar */ |
| 47 | u32 dma_requests; /* number of DMA requests on eDMA */ |
| 48 | }; |
| 49 | |
| 50 | struct ti_am335x_xbar_map { |
| 51 | u16 dma_line; |
| 52 | u16 mux_val; |
| 53 | }; |
| 54 | |
| 55 | static inline void ti_am335x_xbar_write(void __iomem *iomem, int event, u16 val) |
| 56 | { |
| 57 | writeb_relaxed(val & 0x1f, iomem + event); |
| 58 | } |
| 59 | |
| 60 | static void ti_am335x_xbar_free(struct device *dev, void *route_data) |
| 61 | { |
| 62 | struct ti_am335x_xbar_data *xbar = dev_get_drvdata(dev); |
| 63 | struct ti_am335x_xbar_map *map = route_data; |
| 64 | |
| 65 | dev_dbg(dev, "Unmapping XBAR event %u on channel %u\n", |
| 66 | map->mux_val, map->dma_line); |
| 67 | |
| 68 | ti_am335x_xbar_write(xbar->iomem, map->dma_line, 0); |
| 69 | kfree(map); |
| 70 | } |
| 71 | |
| 72 | static void *ti_am335x_xbar_route_allocate(struct of_phandle_args *dma_spec, |
| 73 | struct of_dma *ofdma) |
| 74 | { |
| 75 | struct platform_device *pdev = of_find_device_by_node(ofdma->of_node); |
| 76 | struct ti_am335x_xbar_data *xbar = platform_get_drvdata(pdev); |
| 77 | struct ti_am335x_xbar_map *map; |
| 78 | |
| 79 | if (dma_spec->args_count != 3) |
| 80 | return ERR_PTR(-EINVAL); |
| 81 | |
| 82 | if (dma_spec->args[2] >= xbar->xbar_events) { |
| 83 | dev_err(&pdev->dev, "Invalid XBAR event number: %d\n", |
| 84 | dma_spec->args[2]); |
| 85 | return ERR_PTR(-EINVAL); |
| 86 | } |
| 87 | |
| 88 | if (dma_spec->args[0] >= xbar->dma_requests) { |
| 89 | dev_err(&pdev->dev, "Invalid DMA request line number: %d\n", |
| 90 | dma_spec->args[0]); |
| 91 | return ERR_PTR(-EINVAL); |
| 92 | } |
| 93 | |
| 94 | /* The of_node_put() will be done in the core for the node */ |
| 95 | dma_spec->np = of_parse_phandle(ofdma->of_node, "dma-masters", 0); |
| 96 | if (!dma_spec->np) { |
| 97 | dev_err(&pdev->dev, "Can't get DMA master\n"); |
| 98 | return ERR_PTR(-EINVAL); |
| 99 | } |
| 100 | |
| 101 | map = kzalloc(sizeof(*map), GFP_KERNEL); |
| 102 | if (!map) { |
| 103 | of_node_put(dma_spec->np); |
| 104 | return ERR_PTR(-ENOMEM); |
| 105 | } |
| 106 | |
| 107 | map->dma_line = (u16)dma_spec->args[0]; |
| 108 | map->mux_val = (u16)dma_spec->args[2]; |
| 109 | |
| 110 | dma_spec->args[2] = 0; |
| 111 | dma_spec->args_count = 2; |
| 112 | |
| 113 | dev_dbg(&pdev->dev, "Mapping XBAR event%u to DMA%u\n", |
| 114 | map->mux_val, map->dma_line); |
| 115 | |
| 116 | ti_am335x_xbar_write(xbar->iomem, map->dma_line, map->mux_val); |
| 117 | |
| 118 | return map; |
| 119 | } |
| 120 | |
| 121 | static const struct of_device_id ti_am335x_master_match[] = { |
| 122 | { .compatible = "ti,edma3-tpcc", }, |
| 123 | {}, |
| 124 | }; |
| 125 | |
| 126 | static int ti_am335x_xbar_probe(struct platform_device *pdev) |
| 127 | { |
| 128 | struct device_node *node = pdev->dev.of_node; |
| 129 | const struct of_device_id *match; |
| 130 | struct device_node *dma_node; |
| 131 | struct ti_am335x_xbar_data *xbar; |
| 132 | struct resource *res; |
| 133 | void __iomem *iomem; |
| 134 | int i, ret; |
| 135 | |
| 136 | if (!node) |
| 137 | return -ENODEV; |
| 138 | |
| 139 | xbar = devm_kzalloc(&pdev->dev, sizeof(*xbar), GFP_KERNEL); |
| 140 | if (!xbar) |
| 141 | return -ENOMEM; |
| 142 | |
| 143 | dma_node = of_parse_phandle(node, "dma-masters", 0); |
| 144 | if (!dma_node) { |
| 145 | dev_err(&pdev->dev, "Can't get DMA master node\n"); |
| 146 | return -ENODEV; |
| 147 | } |
| 148 | |
| 149 | match = of_match_node(ti_am335x_master_match, dma_node); |
| 150 | if (!match) { |
| 151 | dev_err(&pdev->dev, "DMA master is not supported\n"); |
| 152 | return -EINVAL; |
| 153 | } |
| 154 | |
| 155 | if (of_property_read_u32(dma_node, "dma-requests", |
| 156 | &xbar->dma_requests)) { |
| 157 | dev_info(&pdev->dev, |
| 158 | "Missing XBAR output information, using %u.\n", |
| 159 | TI_AM335X_XBAR_LINES); |
| 160 | xbar->dma_requests = TI_AM335X_XBAR_LINES; |
| 161 | } |
| 162 | of_node_put(dma_node); |
| 163 | |
| 164 | if (of_property_read_u32(node, "dma-requests", &xbar->xbar_events)) { |
| 165 | dev_info(&pdev->dev, |
| 166 | "Missing XBAR input information, using %u.\n", |
| 167 | TI_AM335X_XBAR_LINES); |
| 168 | xbar->xbar_events = TI_AM335X_XBAR_LINES; |
| 169 | } |
| 170 | |
| 171 | res = platform_get_resource(pdev, IORESOURCE_MEM, 0); |
| 172 | iomem = devm_ioremap_resource(&pdev->dev, res); |
| 173 | if (IS_ERR(iomem)) |
| 174 | return PTR_ERR(iomem); |
| 175 | |
| 176 | xbar->iomem = iomem; |
| 177 | |
| 178 | xbar->dmarouter.dev = &pdev->dev; |
| 179 | xbar->dmarouter.route_free = ti_am335x_xbar_free; |
| 180 | |
| 181 | platform_set_drvdata(pdev, xbar); |
| 182 | |
| 183 | /* Reset the crossbar */ |
| 184 | for (i = 0; i < xbar->dma_requests; i++) |
| 185 | ti_am335x_xbar_write(xbar->iomem, i, 0); |
| 186 | |
| 187 | ret = of_dma_router_register(node, ti_am335x_xbar_route_allocate, |
| 188 | &xbar->dmarouter); |
| 189 | |
| 190 | return ret; |
| 191 | } |
| 192 | |
| 193 | /* Crossbar on DRA7xx family */ |
| 194 | #define TI_DRA7_XBAR_OUTPUTS 127 |
| 195 | #define TI_DRA7_XBAR_INPUTS 256 |
Peter Ujfalusi | a074ae3 | 2015-04-09 12:35:49 +0300 | [diff] [blame] | 196 | |
Peter Ujfalusi | 42dbdcc | 2015-10-16 10:18:08 +0300 | [diff] [blame] | 197 | struct ti_dra7_xbar_data { |
Peter Ujfalusi | a074ae3 | 2015-04-09 12:35:49 +0300 | [diff] [blame] | 198 | void __iomem *iomem; |
| 199 | |
| 200 | struct dma_router dmarouter; |
Peter Ujfalusi | ec9bfa1 | 2015-10-30 10:00:36 +0200 | [diff] [blame] | 201 | struct mutex mutex; |
| 202 | unsigned long *dma_inuse; |
Peter Ujfalusi | a074ae3 | 2015-04-09 12:35:49 +0300 | [diff] [blame] | 203 | |
| 204 | u16 safe_val; /* Value to rest the crossbar lines */ |
| 205 | u32 xbar_requests; /* number of DMA requests connected to XBAR */ |
| 206 | u32 dma_requests; /* number of DMA requests forwarded to DMA */ |
Peter Ujfalusi | 1eb995b | 2015-07-22 11:48:10 +0300 | [diff] [blame] | 207 | u32 dma_offset; |
Peter Ujfalusi | a074ae3 | 2015-04-09 12:35:49 +0300 | [diff] [blame] | 208 | }; |
| 209 | |
Peter Ujfalusi | 42dbdcc | 2015-10-16 10:18:08 +0300 | [diff] [blame] | 210 | struct ti_dra7_xbar_map { |
Peter Ujfalusi | a074ae3 | 2015-04-09 12:35:49 +0300 | [diff] [blame] | 211 | u16 xbar_in; |
| 212 | int xbar_out; |
| 213 | }; |
| 214 | |
Peter Ujfalusi | 42dbdcc | 2015-10-16 10:18:08 +0300 | [diff] [blame] | 215 | static inline void ti_dra7_xbar_write(void __iomem *iomem, int xbar, u16 val) |
Peter Ujfalusi | a074ae3 | 2015-04-09 12:35:49 +0300 | [diff] [blame] | 216 | { |
| 217 | writew_relaxed(val, iomem + (xbar * 2)); |
| 218 | } |
| 219 | |
Peter Ujfalusi | 42dbdcc | 2015-10-16 10:18:08 +0300 | [diff] [blame] | 220 | static void ti_dra7_xbar_free(struct device *dev, void *route_data) |
Peter Ujfalusi | a074ae3 | 2015-04-09 12:35:49 +0300 | [diff] [blame] | 221 | { |
Peter Ujfalusi | 42dbdcc | 2015-10-16 10:18:08 +0300 | [diff] [blame] | 222 | struct ti_dra7_xbar_data *xbar = dev_get_drvdata(dev); |
| 223 | struct ti_dra7_xbar_map *map = route_data; |
Peter Ujfalusi | a074ae3 | 2015-04-09 12:35:49 +0300 | [diff] [blame] | 224 | |
| 225 | dev_dbg(dev, "Unmapping XBAR%u (was routed to %d)\n", |
| 226 | map->xbar_in, map->xbar_out); |
| 227 | |
Peter Ujfalusi | 42dbdcc | 2015-10-16 10:18:08 +0300 | [diff] [blame] | 228 | ti_dra7_xbar_write(xbar->iomem, map->xbar_out, xbar->safe_val); |
Peter Ujfalusi | ec9bfa1 | 2015-10-30 10:00:36 +0200 | [diff] [blame] | 229 | mutex_lock(&xbar->mutex); |
| 230 | clear_bit(map->xbar_out, xbar->dma_inuse); |
| 231 | mutex_unlock(&xbar->mutex); |
Peter Ujfalusi | a074ae3 | 2015-04-09 12:35:49 +0300 | [diff] [blame] | 232 | kfree(map); |
| 233 | } |
| 234 | |
Peter Ujfalusi | 42dbdcc | 2015-10-16 10:18:08 +0300 | [diff] [blame] | 235 | static void *ti_dra7_xbar_route_allocate(struct of_phandle_args *dma_spec, |
| 236 | struct of_dma *ofdma) |
Peter Ujfalusi | a074ae3 | 2015-04-09 12:35:49 +0300 | [diff] [blame] | 237 | { |
| 238 | struct platform_device *pdev = of_find_device_by_node(ofdma->of_node); |
Peter Ujfalusi | 42dbdcc | 2015-10-16 10:18:08 +0300 | [diff] [blame] | 239 | struct ti_dra7_xbar_data *xbar = platform_get_drvdata(pdev); |
| 240 | struct ti_dra7_xbar_map *map; |
Peter Ujfalusi | a074ae3 | 2015-04-09 12:35:49 +0300 | [diff] [blame] | 241 | |
| 242 | if (dma_spec->args[0] >= xbar->xbar_requests) { |
| 243 | dev_err(&pdev->dev, "Invalid XBAR request number: %d\n", |
| 244 | dma_spec->args[0]); |
| 245 | return ERR_PTR(-EINVAL); |
| 246 | } |
| 247 | |
| 248 | /* The of_node_put() will be done in the core for the node */ |
| 249 | dma_spec->np = of_parse_phandle(ofdma->of_node, "dma-masters", 0); |
| 250 | if (!dma_spec->np) { |
| 251 | dev_err(&pdev->dev, "Can't get DMA master\n"); |
| 252 | return ERR_PTR(-EINVAL); |
| 253 | } |
| 254 | |
| 255 | map = kzalloc(sizeof(*map), GFP_KERNEL); |
| 256 | if (!map) { |
| 257 | of_node_put(dma_spec->np); |
| 258 | return ERR_PTR(-ENOMEM); |
| 259 | } |
| 260 | |
Peter Ujfalusi | ec9bfa1 | 2015-10-30 10:00:36 +0200 | [diff] [blame] | 261 | mutex_lock(&xbar->mutex); |
| 262 | map->xbar_out = find_first_zero_bit(xbar->dma_inuse, |
| 263 | xbar->dma_requests); |
| 264 | mutex_unlock(&xbar->mutex); |
| 265 | if (map->xbar_out == xbar->dma_requests) { |
| 266 | dev_err(&pdev->dev, "Run out of free DMA requests\n"); |
| 267 | kfree(map); |
| 268 | return ERR_PTR(-ENOMEM); |
| 269 | } |
| 270 | set_bit(map->xbar_out, xbar->dma_inuse); |
| 271 | |
Peter Ujfalusi | a074ae3 | 2015-04-09 12:35:49 +0300 | [diff] [blame] | 272 | map->xbar_in = (u16)dma_spec->args[0]; |
| 273 | |
Peter Ujfalusi | 1eb995b | 2015-07-22 11:48:10 +0300 | [diff] [blame] | 274 | dma_spec->args[0] = map->xbar_out + xbar->dma_offset; |
Peter Ujfalusi | a074ae3 | 2015-04-09 12:35:49 +0300 | [diff] [blame] | 275 | |
| 276 | dev_dbg(&pdev->dev, "Mapping XBAR%u to DMA%d\n", |
| 277 | map->xbar_in, map->xbar_out); |
| 278 | |
Peter Ujfalusi | 42dbdcc | 2015-10-16 10:18:08 +0300 | [diff] [blame] | 279 | ti_dra7_xbar_write(xbar->iomem, map->xbar_out, map->xbar_in); |
Peter Ujfalusi | a074ae3 | 2015-04-09 12:35:49 +0300 | [diff] [blame] | 280 | |
| 281 | return map; |
| 282 | } |
| 283 | |
Peter Ujfalusi | 5f9367a | 2016-09-21 15:41:32 +0300 | [diff] [blame] | 284 | #define TI_XBAR_EDMA_OFFSET 0 |
| 285 | #define TI_XBAR_SDMA_OFFSET 1 |
| 286 | static const u32 ti_dma_offset[] = { |
| 287 | [TI_XBAR_EDMA_OFFSET] = 0, |
| 288 | [TI_XBAR_SDMA_OFFSET] = 1, |
| 289 | }; |
| 290 | |
Peter Ujfalusi | 42dbdcc | 2015-10-16 10:18:08 +0300 | [diff] [blame] | 291 | static const struct of_device_id ti_dra7_master_match[] = { |
Peter Ujfalusi | 1eb995b | 2015-07-22 11:48:10 +0300 | [diff] [blame] | 292 | { |
| 293 | .compatible = "ti,omap4430-sdma", |
Peter Ujfalusi | 5f9367a | 2016-09-21 15:41:32 +0300 | [diff] [blame] | 294 | .data = &ti_dma_offset[TI_XBAR_SDMA_OFFSET], |
Peter Ujfalusi | 1eb995b | 2015-07-22 11:48:10 +0300 | [diff] [blame] | 295 | }, |
| 296 | { |
| 297 | .compatible = "ti,edma3", |
Peter Ujfalusi | 5f9367a | 2016-09-21 15:41:32 +0300 | [diff] [blame] | 298 | .data = &ti_dma_offset[TI_XBAR_EDMA_OFFSET], |
Peter Ujfalusi | 1eb995b | 2015-07-22 11:48:10 +0300 | [diff] [blame] | 299 | }, |
Peter Ujfalusi | 2adb274 | 2015-10-30 10:00:38 +0200 | [diff] [blame] | 300 | { |
| 301 | .compatible = "ti,edma3-tpcc", |
Peter Ujfalusi | 5f9367a | 2016-09-21 15:41:32 +0300 | [diff] [blame] | 302 | .data = &ti_dma_offset[TI_XBAR_EDMA_OFFSET], |
Peter Ujfalusi | 2adb274 | 2015-10-30 10:00:38 +0200 | [diff] [blame] | 303 | }, |
Peter Ujfalusi | 1eb995b | 2015-07-22 11:48:10 +0300 | [diff] [blame] | 304 | {}, |
| 305 | }; |
| 306 | |
Peter Ujfalusi | 0f73f3e | 2015-10-30 10:00:37 +0200 | [diff] [blame] | 307 | static inline void ti_dra7_xbar_reserve(int offset, int len, unsigned long *p) |
| 308 | { |
| 309 | for (; len > 0; len--) |
| 310 | clear_bit(offset + (len - 1), p); |
| 311 | } |
| 312 | |
Peter Ujfalusi | 42dbdcc | 2015-10-16 10:18:08 +0300 | [diff] [blame] | 313 | static int ti_dra7_xbar_probe(struct platform_device *pdev) |
Peter Ujfalusi | a074ae3 | 2015-04-09 12:35:49 +0300 | [diff] [blame] | 314 | { |
| 315 | struct device_node *node = pdev->dev.of_node; |
Peter Ujfalusi | 1eb995b | 2015-07-22 11:48:10 +0300 | [diff] [blame] | 316 | const struct of_device_id *match; |
Peter Ujfalusi | a074ae3 | 2015-04-09 12:35:49 +0300 | [diff] [blame] | 317 | struct device_node *dma_node; |
Peter Ujfalusi | 42dbdcc | 2015-10-16 10:18:08 +0300 | [diff] [blame] | 318 | struct ti_dra7_xbar_data *xbar; |
Peter Ujfalusi | 0f73f3e | 2015-10-30 10:00:37 +0200 | [diff] [blame] | 319 | struct property *prop; |
Peter Ujfalusi | a074ae3 | 2015-04-09 12:35:49 +0300 | [diff] [blame] | 320 | struct resource *res; |
| 321 | u32 safe_val; |
Peter Ujfalusi | e7282b6 | 2016-09-21 15:41:31 +0300 | [diff] [blame] | 322 | int sz; |
Peter Ujfalusi | a074ae3 | 2015-04-09 12:35:49 +0300 | [diff] [blame] | 323 | void __iomem *iomem; |
| 324 | int i, ret; |
| 325 | |
| 326 | if (!node) |
| 327 | return -ENODEV; |
| 328 | |
| 329 | xbar = devm_kzalloc(&pdev->dev, sizeof(*xbar), GFP_KERNEL); |
| 330 | if (!xbar) |
| 331 | return -ENOMEM; |
| 332 | |
Peter Ujfalusi | a074ae3 | 2015-04-09 12:35:49 +0300 | [diff] [blame] | 333 | dma_node = of_parse_phandle(node, "dma-masters", 0); |
| 334 | if (!dma_node) { |
| 335 | dev_err(&pdev->dev, "Can't get DMA master node\n"); |
| 336 | return -ENODEV; |
| 337 | } |
| 338 | |
Peter Ujfalusi | 42dbdcc | 2015-10-16 10:18:08 +0300 | [diff] [blame] | 339 | match = of_match_node(ti_dra7_master_match, dma_node); |
Peter Ujfalusi | 1eb995b | 2015-07-22 11:48:10 +0300 | [diff] [blame] | 340 | if (!match) { |
| 341 | dev_err(&pdev->dev, "DMA master is not supported\n"); |
| 342 | return -EINVAL; |
| 343 | } |
| 344 | |
Peter Ujfalusi | a074ae3 | 2015-04-09 12:35:49 +0300 | [diff] [blame] | 345 | if (of_property_read_u32(dma_node, "dma-requests", |
| 346 | &xbar->dma_requests)) { |
| 347 | dev_info(&pdev->dev, |
| 348 | "Missing XBAR output information, using %u.\n", |
Peter Ujfalusi | 42dbdcc | 2015-10-16 10:18:08 +0300 | [diff] [blame] | 349 | TI_DRA7_XBAR_OUTPUTS); |
| 350 | xbar->dma_requests = TI_DRA7_XBAR_OUTPUTS; |
Peter Ujfalusi | a074ae3 | 2015-04-09 12:35:49 +0300 | [diff] [blame] | 351 | } |
| 352 | of_node_put(dma_node); |
| 353 | |
Peter Ujfalusi | ec9bfa1 | 2015-10-30 10:00:36 +0200 | [diff] [blame] | 354 | xbar->dma_inuse = devm_kcalloc(&pdev->dev, |
| 355 | BITS_TO_LONGS(xbar->dma_requests), |
| 356 | sizeof(unsigned long), GFP_KERNEL); |
| 357 | if (!xbar->dma_inuse) |
| 358 | return -ENOMEM; |
| 359 | |
Peter Ujfalusi | a074ae3 | 2015-04-09 12:35:49 +0300 | [diff] [blame] | 360 | if (of_property_read_u32(node, "dma-requests", &xbar->xbar_requests)) { |
| 361 | dev_info(&pdev->dev, |
| 362 | "Missing XBAR input information, using %u.\n", |
Peter Ujfalusi | 42dbdcc | 2015-10-16 10:18:08 +0300 | [diff] [blame] | 363 | TI_DRA7_XBAR_INPUTS); |
| 364 | xbar->xbar_requests = TI_DRA7_XBAR_INPUTS; |
Peter Ujfalusi | a074ae3 | 2015-04-09 12:35:49 +0300 | [diff] [blame] | 365 | } |
| 366 | |
| 367 | if (!of_property_read_u32(node, "ti,dma-safe-map", &safe_val)) |
| 368 | xbar->safe_val = (u16)safe_val; |
| 369 | |
Peter Ujfalusi | 0f73f3e | 2015-10-30 10:00:37 +0200 | [diff] [blame] | 370 | |
| 371 | prop = of_find_property(node, "ti,reserved-dma-request-ranges", &sz); |
| 372 | if (prop) { |
| 373 | const char pname[] = "ti,reserved-dma-request-ranges"; |
| 374 | u32 (*rsv_events)[2]; |
| 375 | size_t nelm = sz / sizeof(*rsv_events); |
| 376 | int i; |
| 377 | |
| 378 | if (!nelm) |
| 379 | return -EINVAL; |
| 380 | |
| 381 | rsv_events = kcalloc(nelm, sizeof(*rsv_events), GFP_KERNEL); |
| 382 | if (!rsv_events) |
| 383 | return -ENOMEM; |
| 384 | |
| 385 | ret = of_property_read_u32_array(node, pname, (u32 *)rsv_events, |
| 386 | nelm * 2); |
| 387 | if (ret) |
| 388 | return ret; |
| 389 | |
| 390 | for (i = 0; i < nelm; i++) { |
| 391 | ti_dra7_xbar_reserve(rsv_events[i][0], rsv_events[i][1], |
| 392 | xbar->dma_inuse); |
| 393 | } |
| 394 | kfree(rsv_events); |
| 395 | } |
| 396 | |
Peter Ujfalusi | a074ae3 | 2015-04-09 12:35:49 +0300 | [diff] [blame] | 397 | res = platform_get_resource(pdev, IORESOURCE_MEM, 0); |
Peter Ujfalusi | a074ae3 | 2015-04-09 12:35:49 +0300 | [diff] [blame] | 398 | iomem = devm_ioremap_resource(&pdev->dev, res); |
Axel Lin | 28eb232 | 2015-07-10 22:13:19 +0800 | [diff] [blame] | 399 | if (IS_ERR(iomem)) |
| 400 | return PTR_ERR(iomem); |
Peter Ujfalusi | a074ae3 | 2015-04-09 12:35:49 +0300 | [diff] [blame] | 401 | |
| 402 | xbar->iomem = iomem; |
| 403 | |
| 404 | xbar->dmarouter.dev = &pdev->dev; |
Peter Ujfalusi | 42dbdcc | 2015-10-16 10:18:08 +0300 | [diff] [blame] | 405 | xbar->dmarouter.route_free = ti_dra7_xbar_free; |
Peter Ujfalusi | 5f9367a | 2016-09-21 15:41:32 +0300 | [diff] [blame] | 406 | xbar->dma_offset = *(u32 *)match->data; |
Peter Ujfalusi | a074ae3 | 2015-04-09 12:35:49 +0300 | [diff] [blame] | 407 | |
Peter Ujfalusi | ec9bfa1 | 2015-10-30 10:00:36 +0200 | [diff] [blame] | 408 | mutex_init(&xbar->mutex); |
Peter Ujfalusi | a074ae3 | 2015-04-09 12:35:49 +0300 | [diff] [blame] | 409 | platform_set_drvdata(pdev, xbar); |
| 410 | |
| 411 | /* Reset the crossbar */ |
Peter Ujfalusi | 0f73f3e | 2015-10-30 10:00:37 +0200 | [diff] [blame] | 412 | for (i = 0; i < xbar->dma_requests; i++) { |
| 413 | if (!test_bit(i, xbar->dma_inuse)) |
| 414 | ti_dra7_xbar_write(xbar->iomem, i, xbar->safe_val); |
| 415 | } |
Peter Ujfalusi | a074ae3 | 2015-04-09 12:35:49 +0300 | [diff] [blame] | 416 | |
Peter Ujfalusi | 42dbdcc | 2015-10-16 10:18:08 +0300 | [diff] [blame] | 417 | ret = of_dma_router_register(node, ti_dra7_xbar_route_allocate, |
Peter Ujfalusi | a074ae3 | 2015-04-09 12:35:49 +0300 | [diff] [blame] | 418 | &xbar->dmarouter); |
| 419 | if (ret) { |
| 420 | /* Restore the defaults for the crossbar */ |
Peter Ujfalusi | 0f73f3e | 2015-10-30 10:00:37 +0200 | [diff] [blame] | 421 | for (i = 0; i < xbar->dma_requests; i++) { |
| 422 | if (!test_bit(i, xbar->dma_inuse)) |
| 423 | ti_dra7_xbar_write(xbar->iomem, i, i); |
| 424 | } |
Peter Ujfalusi | a074ae3 | 2015-04-09 12:35:49 +0300 | [diff] [blame] | 425 | } |
| 426 | |
| 427 | return ret; |
| 428 | } |
| 429 | |
Peter Ujfalusi | 42dbdcc | 2015-10-16 10:18:08 +0300 | [diff] [blame] | 430 | static int ti_dma_xbar_probe(struct platform_device *pdev) |
| 431 | { |
| 432 | const struct of_device_id *match; |
| 433 | int ret; |
| 434 | |
| 435 | match = of_match_node(ti_dma_xbar_match, pdev->dev.of_node); |
| 436 | if (unlikely(!match)) |
| 437 | return -EINVAL; |
| 438 | |
Peter Ujfalusi | 5f9367a | 2016-09-21 15:41:32 +0300 | [diff] [blame] | 439 | switch (*(u32 *)match->data) { |
Peter Ujfalusi | 42dbdcc | 2015-10-16 10:18:08 +0300 | [diff] [blame] | 440 | case TI_XBAR_DRA7: |
| 441 | ret = ti_dra7_xbar_probe(pdev); |
| 442 | break; |
| 443 | case TI_XBAR_AM335X: |
| 444 | ret = ti_am335x_xbar_probe(pdev); |
| 445 | break; |
| 446 | default: |
| 447 | dev_err(&pdev->dev, "Unsupported crossbar\n"); |
| 448 | ret = -ENODEV; |
| 449 | break; |
| 450 | } |
| 451 | |
| 452 | return ret; |
| 453 | } |
Peter Ujfalusi | a074ae3 | 2015-04-09 12:35:49 +0300 | [diff] [blame] | 454 | |
| 455 | static struct platform_driver ti_dma_xbar_driver = { |
| 456 | .driver = { |
| 457 | .name = "ti-dma-crossbar", |
| 458 | .of_match_table = of_match_ptr(ti_dma_xbar_match), |
| 459 | }, |
| 460 | .probe = ti_dma_xbar_probe, |
| 461 | }; |
| 462 | |
Ben Dooks | d646162 | 2016-06-21 18:12:39 +0100 | [diff] [blame] | 463 | static int omap_dmaxbar_init(void) |
Peter Ujfalusi | a074ae3 | 2015-04-09 12:35:49 +0300 | [diff] [blame] | 464 | { |
| 465 | return platform_driver_register(&ti_dma_xbar_driver); |
| 466 | } |
| 467 | arch_initcall(omap_dmaxbar_init); |