1 // SPDX-License-Identifier: GPL-2.0-only 2 /* 3 * Copyright (C) 2022 Schneider-Electric 4 * Author: Miquel Raynal <miquel.raynal@bootlin.com 5 * Based on TI crossbar driver written by Peter Ujfalusi <peter.ujfalusi@ti.com> 6 */ 7 #include <linux/bitops.h> 8 #include <linux/of.h> 9 #include <linux/of_dma.h> 10 #include <linux/of_platform.h> 11 #include <linux/platform_device.h> 12 #include <linux/slab.h> 13 #include <linux/soc/renesas/r9a06g032-sysctrl.h> 14 #include <linux/types.h> 15 16 #define RNZ1_DMAMUX_NCELLS 6 17 #define RZN1_DMAMUX_MAX_LINES 64 18 #define RZN1_DMAMUX_LINES_PER_CTLR 16 19 20 struct rzn1_dmamux_data { 21 struct dma_router dmarouter; 22 DECLARE_BITMAP(used_chans, 2 * RZN1_DMAMUX_LINES_PER_CTLR); 23 }; 24 25 struct rzn1_dmamux_map { 26 unsigned int req_idx; 27 }; 28 29 static void rzn1_dmamux_free(struct device *dev, void *route_data) 30 { 31 struct rzn1_dmamux_data *dmamux = dev_get_drvdata(dev); 32 struct rzn1_dmamux_map *map = route_data; 33 34 dev_dbg(dev, "Unmapping DMAMUX request %u\n", map->req_idx); 35 36 clear_bit(map->req_idx, dmamux->used_chans); 37 38 kfree(map); 39 } 40 41 static void *rzn1_dmamux_route_allocate(struct of_phandle_args *dma_spec, 42 struct of_dma *ofdma) 43 { 44 struct platform_device *pdev = of_find_device_by_node(ofdma->of_node); 45 struct rzn1_dmamux_data *dmamux = platform_get_drvdata(pdev); 46 struct rzn1_dmamux_map *map; 47 unsigned int dmac_idx, chan, val; 48 u32 mask; 49 int ret; 50 51 if (dma_spec->args_count != RNZ1_DMAMUX_NCELLS) { 52 ret = -EINVAL; 53 goto put_device; 54 } 55 56 map = kzalloc(sizeof(*map), GFP_KERNEL); 57 if (!map) { 58 ret = -ENOMEM; 59 goto put_device; 60 } 61 62 chan = dma_spec->args[0]; 63 map->req_idx = dma_spec->args[4]; 64 val = dma_spec->args[5]; 65 dma_spec->args_count -= 2; 66 67 if (chan >= RZN1_DMAMUX_LINES_PER_CTLR) { 68 dev_err(&pdev->dev, "Invalid DMA request line: %u\n", chan); 69 ret = -EINVAL; 70 goto free_map; 71 } 72 73 if (map->req_idx >= RZN1_DMAMUX_MAX_LINES || 74 (map->req_idx % RZN1_DMAMUX_LINES_PER_CTLR) != chan) { 75 dev_err(&pdev->dev, "Invalid MUX request line: %u\n", map->req_idx); 76 ret = -EINVAL; 77 goto free_map; 78 } 79 80 dmac_idx = map->req_idx >= RZN1_DMAMUX_LINES_PER_CTLR ? 1 : 0; 81 dma_spec->np = of_parse_phandle(ofdma->of_node, "dma-masters", dmac_idx); 82 if (!dma_spec->np) { 83 dev_err(&pdev->dev, "Can't get DMA master\n"); 84 ret = -EINVAL; 85 goto free_map; 86 } 87 88 dev_dbg(&pdev->dev, "Mapping DMAMUX request %u to DMAC%u request %u\n", 89 map->req_idx, dmac_idx, chan); 90 91 if (test_and_set_bit(map->req_idx, dmamux->used_chans)) { 92 ret = -EBUSY; 93 goto put_dma_spec_np; 94 } 95 96 mask = BIT(map->req_idx); 97 ret = r9a06g032_sysctrl_set_dmamux(mask, val ? mask : 0); 98 if (ret) 99 goto clear_bitmap; 100 101 put_device(&pdev->dev); 102 return map; 103 104 clear_bitmap: 105 clear_bit(map->req_idx, dmamux->used_chans); 106 put_dma_spec_np: 107 of_node_put(dma_spec->np); 108 free_map: 109 kfree(map); 110 put_device: 111 put_device(&pdev->dev); 112 113 return ERR_PTR(ret); 114 } 115 116 #ifdef CONFIG_OF 117 static const struct of_device_id rzn1_dmac_match[] = { 118 { .compatible = "renesas,rzn1-dma" }, 119 {} 120 }; 121 #endif 122 123 static int rzn1_dmamux_probe(struct platform_device *pdev) 124 { 125 struct device_node *mux_node = pdev->dev.of_node; 126 const struct of_device_id *match; 127 struct device_node *dmac_node; 128 struct rzn1_dmamux_data *dmamux; 129 130 dmamux = devm_kzalloc(&pdev->dev, sizeof(*dmamux), GFP_KERNEL); 131 if (!dmamux) 132 return -ENOMEM; 133 134 dmac_node = of_parse_phandle(mux_node, "dma-masters", 0); 135 if (!dmac_node) 136 return dev_err_probe(&pdev->dev, -ENODEV, "Can't get DMA master node\n"); 137 138 match = of_match_node(rzn1_dmac_match, dmac_node); 139 of_node_put(dmac_node); 140 if (!match) 141 return dev_err_probe(&pdev->dev, -EINVAL, "DMA master is not supported\n"); 142 143 dmamux->dmarouter.dev = &pdev->dev; 144 dmamux->dmarouter.route_free = rzn1_dmamux_free; 145 146 platform_set_drvdata(pdev, dmamux); 147 148 return of_dma_router_register(mux_node, rzn1_dmamux_route_allocate, 149 &dmamux->dmarouter); 150 } 151 152 static const struct of_device_id rzn1_dmamux_match[] = { 153 { .compatible = "renesas,rzn1-dmamux" }, 154 {} 155 }; 156 MODULE_DEVICE_TABLE(of, rzn1_dmamux_match); 157 158 static struct platform_driver rzn1_dmamux_driver = { 159 .driver = { 160 .name = "renesas,rzn1-dmamux", 161 .of_match_table = rzn1_dmamux_match, 162 }, 163 .probe = rzn1_dmamux_probe, 164 }; 165 module_platform_driver(rzn1_dmamux_driver); 166 167 MODULE_LICENSE("GPL"); 168 MODULE_AUTHOR("Miquel Raynal <miquel.raynal@bootlin.com"); 169 MODULE_DESCRIPTION("Renesas RZ/N1 DMAMUX driver"); 170