Tom Rini | 10e4779 | 2018-05-06 17:58:06 -0400 | [diff] [blame] | 1 | // SPDX-License-Identifier: GPL-2.0+ |
Mugunthan V N | 8c3c918 | 2016-02-15 15:31:37 +0530 | [diff] [blame] | 2 | /* |
| 3 | * Direct Memory Access U-Class driver |
| 4 | * |
Álvaro Fernández Rojas | d8cedab | 2018-11-28 19:17:50 +0100 | [diff] [blame] | 5 | * Copyright (C) 2018 Álvaro Fernández Rojas <noltari@gmail.com> |
| 6 | * Copyright (C) 2015 - 2018 Texas Instruments Incorporated <www.ti.com> |
| 7 | * Written by Mugunthan V N <mugunthanvnm@ti.com> |
Mugunthan V N | 8c3c918 | 2016-02-15 15:31:37 +0530 | [diff] [blame] | 8 | * |
| 9 | * Author: Mugunthan V N <mugunthanvnm@ti.com> |
Mugunthan V N | 8c3c918 | 2016-02-15 15:31:37 +0530 | [diff] [blame] | 10 | */ |
| 11 | |
Patrick Delaunay | 8131335 | 2021-04-27 11:02:19 +0200 | [diff] [blame] | 12 | #define LOG_CATEGORY UCLASS_DMA |
| 13 | |
Tom Rini | abb9a04 | 2024-05-18 20:20:43 -0600 | [diff] [blame] | 14 | #include <common.h> |
Simon Glass | 6333448 | 2019-11-14 12:57:39 -0700 | [diff] [blame] | 15 | #include <cpu_func.h> |
Mugunthan V N | 8c3c918 | 2016-02-15 15:31:37 +0530 | [diff] [blame] | 16 | #include <dm.h> |
Simon Glass | 0f2af88 | 2020-05-10 11:40:05 -0600 | [diff] [blame] | 17 | #include <log.h> |
Simon Glass | 9bc1564 | 2020-02-03 07:36:16 -0700 | [diff] [blame] | 18 | #include <malloc.h> |
Simon Glass | 274e0b0 | 2020-05-10 11:39:56 -0600 | [diff] [blame] | 19 | #include <asm/cache.h> |
Álvaro Fernández Rojas | d8cedab | 2018-11-28 19:17:50 +0100 | [diff] [blame] | 20 | #include <dm/read.h> |
Álvaro Fernández Rojas | 308cd6f | 2018-11-28 19:17:49 +0100 | [diff] [blame] | 21 | #include <dma-uclass.h> |
Andrew Davis | afea0a2 | 2022-10-07 12:11:11 -0500 | [diff] [blame] | 22 | #include <linux/dma-mapping.h> |
Álvaro Fernández Rojas | d8cedab | 2018-11-28 19:17:50 +0100 | [diff] [blame] | 23 | #include <dt-structs.h> |
Mugunthan V N | 8c3c918 | 2016-02-15 15:31:37 +0530 | [diff] [blame] | 24 | #include <errno.h> |
Simon Glass | bdd5f81 | 2023-09-14 18:21:46 -0600 | [diff] [blame] | 25 | #include <linux/printk.h> |
Mugunthan V N | 8c3c918 | 2016-02-15 15:31:37 +0530 | [diff] [blame] | 26 | |
Álvaro Fernández Rojas | d8cedab | 2018-11-28 19:17:50 +0100 | [diff] [blame] | 27 | #ifdef CONFIG_DMA_CHANNELS |
| 28 | static inline struct dma_ops *dma_dev_ops(struct udevice *dev) |
| 29 | { |
| 30 | return (struct dma_ops *)dev->driver->ops; |
| 31 | } |
| 32 | |
| 33 | # if CONFIG_IS_ENABLED(OF_CONTROL) |
| 34 | static int dma_of_xlate_default(struct dma *dma, |
| 35 | struct ofnode_phandle_args *args) |
| 36 | { |
| 37 | debug("%s(dma=%p)\n", __func__, dma); |
| 38 | |
| 39 | if (args->args_count > 1) { |
Sean Anderson | a1b654b | 2021-12-01 14:26:53 -0500 | [diff] [blame] | 40 | pr_err("Invalid args_count: %d\n", args->args_count); |
Álvaro Fernández Rojas | d8cedab | 2018-11-28 19:17:50 +0100 | [diff] [blame] | 41 | return -EINVAL; |
| 42 | } |
| 43 | |
| 44 | if (args->args_count) |
| 45 | dma->id = args->args[0]; |
| 46 | else |
| 47 | dma->id = 0; |
| 48 | |
| 49 | return 0; |
| 50 | } |
| 51 | |
| 52 | int dma_get_by_index(struct udevice *dev, int index, struct dma *dma) |
| 53 | { |
| 54 | int ret; |
| 55 | struct ofnode_phandle_args args; |
| 56 | struct udevice *dev_dma; |
| 57 | const struct dma_ops *ops; |
| 58 | |
| 59 | debug("%s(dev=%p, index=%d, dma=%p)\n", __func__, dev, index, dma); |
| 60 | |
| 61 | assert(dma); |
| 62 | dma->dev = NULL; |
| 63 | |
| 64 | ret = dev_read_phandle_with_args(dev, "dmas", "#dma-cells", 0, index, |
| 65 | &args); |
| 66 | if (ret) { |
| 67 | pr_err("%s: dev_read_phandle_with_args failed: err=%d\n", |
| 68 | __func__, ret); |
| 69 | return ret; |
| 70 | } |
| 71 | |
| 72 | ret = uclass_get_device_by_ofnode(UCLASS_DMA, args.node, &dev_dma); |
| 73 | if (ret) { |
| 74 | pr_err("%s: uclass_get_device_by_ofnode failed: err=%d\n", |
| 75 | __func__, ret); |
| 76 | return ret; |
| 77 | } |
| 78 | |
| 79 | dma->dev = dev_dma; |
| 80 | |
| 81 | ops = dma_dev_ops(dev_dma); |
| 82 | |
| 83 | if (ops->of_xlate) |
| 84 | ret = ops->of_xlate(dma, &args); |
| 85 | else |
| 86 | ret = dma_of_xlate_default(dma, &args); |
| 87 | if (ret) { |
| 88 | pr_err("of_xlate() failed: %d\n", ret); |
| 89 | return ret; |
| 90 | } |
| 91 | |
| 92 | return dma_request(dev_dma, dma); |
| 93 | } |
| 94 | |
| 95 | int dma_get_by_name(struct udevice *dev, const char *name, struct dma *dma) |
| 96 | { |
| 97 | int index; |
| 98 | |
| 99 | debug("%s(dev=%p, name=%s, dma=%p)\n", __func__, dev, name, dma); |
| 100 | dma->dev = NULL; |
| 101 | |
| 102 | index = dev_read_stringlist_search(dev, "dma-names", name); |
| 103 | if (index < 0) { |
| 104 | pr_err("dev_read_stringlist_search() failed: %d\n", index); |
| 105 | return index; |
| 106 | } |
| 107 | |
| 108 | return dma_get_by_index(dev, index, dma); |
| 109 | } |
| 110 | # endif /* OF_CONTROL */ |
| 111 | |
| 112 | int dma_request(struct udevice *dev, struct dma *dma) |
| 113 | { |
| 114 | struct dma_ops *ops = dma_dev_ops(dev); |
| 115 | |
| 116 | debug("%s(dev=%p, dma=%p)\n", __func__, dev, dma); |
| 117 | |
| 118 | dma->dev = dev; |
| 119 | |
| 120 | if (!ops->request) |
| 121 | return 0; |
| 122 | |
| 123 | return ops->request(dma); |
| 124 | } |
| 125 | |
| 126 | int dma_free(struct dma *dma) |
| 127 | { |
| 128 | struct dma_ops *ops = dma_dev_ops(dma->dev); |
| 129 | |
| 130 | debug("%s(dma=%p)\n", __func__, dma); |
| 131 | |
Simon Glass | 75c0ad6 | 2020-02-03 07:35:55 -0700 | [diff] [blame] | 132 | if (!ops->rfree) |
Álvaro Fernández Rojas | d8cedab | 2018-11-28 19:17:50 +0100 | [diff] [blame] | 133 | return 0; |
| 134 | |
Simon Glass | 75c0ad6 | 2020-02-03 07:35:55 -0700 | [diff] [blame] | 135 | return ops->rfree(dma); |
Álvaro Fernández Rojas | d8cedab | 2018-11-28 19:17:50 +0100 | [diff] [blame] | 136 | } |
| 137 | |
| 138 | int dma_enable(struct dma *dma) |
| 139 | { |
| 140 | struct dma_ops *ops = dma_dev_ops(dma->dev); |
| 141 | |
| 142 | debug("%s(dma=%p)\n", __func__, dma); |
| 143 | |
| 144 | if (!ops->enable) |
| 145 | return -ENOSYS; |
| 146 | |
| 147 | return ops->enable(dma); |
| 148 | } |
| 149 | |
| 150 | int dma_disable(struct dma *dma) |
| 151 | { |
| 152 | struct dma_ops *ops = dma_dev_ops(dma->dev); |
| 153 | |
| 154 | debug("%s(dma=%p)\n", __func__, dma); |
| 155 | |
| 156 | if (!ops->disable) |
| 157 | return -ENOSYS; |
| 158 | |
| 159 | return ops->disable(dma); |
| 160 | } |
| 161 | |
| 162 | int dma_prepare_rcv_buf(struct dma *dma, void *dst, size_t size) |
| 163 | { |
| 164 | struct dma_ops *ops = dma_dev_ops(dma->dev); |
| 165 | |
| 166 | debug("%s(dma=%p)\n", __func__, dma); |
| 167 | |
| 168 | if (!ops->prepare_rcv_buf) |
| 169 | return -1; |
| 170 | |
| 171 | return ops->prepare_rcv_buf(dma, dst, size); |
| 172 | } |
| 173 | |
| 174 | int dma_receive(struct dma *dma, void **dst, void *metadata) |
| 175 | { |
| 176 | struct dma_ops *ops = dma_dev_ops(dma->dev); |
| 177 | |
| 178 | debug("%s(dma=%p)\n", __func__, dma); |
| 179 | |
| 180 | if (!ops->receive) |
| 181 | return -ENOSYS; |
| 182 | |
| 183 | return ops->receive(dma, dst, metadata); |
| 184 | } |
| 185 | |
| 186 | int dma_send(struct dma *dma, void *src, size_t len, void *metadata) |
| 187 | { |
| 188 | struct dma_ops *ops = dma_dev_ops(dma->dev); |
| 189 | |
| 190 | debug("%s(dma=%p)\n", __func__, dma); |
| 191 | |
| 192 | if (!ops->send) |
| 193 | return -ENOSYS; |
| 194 | |
| 195 | return ops->send(dma, src, len, metadata); |
| 196 | } |
Vignesh Raghavendra | b18fb7e | 2019-12-04 22:17:20 +0530 | [diff] [blame] | 197 | |
| 198 | int dma_get_cfg(struct dma *dma, u32 cfg_id, void **cfg_data) |
| 199 | { |
| 200 | struct dma_ops *ops = dma_dev_ops(dma->dev); |
| 201 | |
| 202 | debug("%s(dma=%p)\n", __func__, dma); |
| 203 | |
| 204 | if (!ops->get_cfg) |
| 205 | return -ENOSYS; |
| 206 | |
| 207 | return ops->get_cfg(dma, cfg_id, cfg_data); |
| 208 | } |
Álvaro Fernández Rojas | d8cedab | 2018-11-28 19:17:50 +0100 | [diff] [blame] | 209 | #endif /* CONFIG_DMA_CHANNELS */ |
| 210 | |
Mugunthan V N | 8c3c918 | 2016-02-15 15:31:37 +0530 | [diff] [blame] | 211 | int dma_get_device(u32 transfer_type, struct udevice **devp) |
| 212 | { |
| 213 | struct udevice *dev; |
Mugunthan V N | 8c3c918 | 2016-02-15 15:31:37 +0530 | [diff] [blame] | 214 | |
Michal Suchanek | 91c96fe | 2022-10-12 21:58:08 +0200 | [diff] [blame] | 215 | for (uclass_first_device(UCLASS_DMA, &dev); dev; |
| 216 | uclass_next_device(&dev)) { |
Mugunthan V N | 8c3c918 | 2016-02-15 15:31:37 +0530 | [diff] [blame] | 217 | struct dma_dev_priv *uc_priv; |
| 218 | |
| 219 | uc_priv = dev_get_uclass_priv(dev); |
| 220 | if (uc_priv->supported & transfer_type) |
| 221 | break; |
| 222 | } |
| 223 | |
| 224 | if (!dev) { |
Vignesh Raghavendra | 518d924 | 2020-09-17 16:53:07 +0530 | [diff] [blame] | 225 | pr_debug("No DMA device found that supports %x type\n", |
| 226 | transfer_type); |
Mugunthan V N | 8c3c918 | 2016-02-15 15:31:37 +0530 | [diff] [blame] | 227 | return -EPROTONOSUPPORT; |
| 228 | } |
| 229 | |
| 230 | *devp = dev; |
| 231 | |
Michal Suchanek | 91c96fe | 2022-10-12 21:58:08 +0200 | [diff] [blame] | 232 | return 0; |
Mugunthan V N | 8c3c918 | 2016-02-15 15:31:37 +0530 | [diff] [blame] | 233 | } |
| 234 | |
| 235 | int dma_memcpy(void *dst, void *src, size_t len) |
| 236 | { |
| 237 | struct udevice *dev; |
| 238 | const struct dma_ops *ops; |
Andrew Davis | afea0a2 | 2022-10-07 12:11:11 -0500 | [diff] [blame] | 239 | dma_addr_t destination; |
| 240 | dma_addr_t source; |
Mugunthan V N | 8c3c918 | 2016-02-15 15:31:37 +0530 | [diff] [blame] | 241 | int ret; |
| 242 | |
| 243 | ret = dma_get_device(DMA_SUPPORTS_MEM_TO_MEM, &dev); |
| 244 | if (ret < 0) |
| 245 | return ret; |
| 246 | |
| 247 | ops = device_get_ops(dev); |
| 248 | if (!ops->transfer) |
| 249 | return -ENOSYS; |
| 250 | |
Andrew Davis | afea0a2 | 2022-10-07 12:11:11 -0500 | [diff] [blame] | 251 | /* Clean the areas, so no writeback into the RAM races with DMA */ |
| 252 | destination = dma_map_single(dst, len, DMA_FROM_DEVICE); |
| 253 | source = dma_map_single(src, len, DMA_TO_DEVICE); |
Mugunthan V N | 8c3c918 | 2016-02-15 15:31:37 +0530 | [diff] [blame] | 254 | |
Andrew Davis | d2da284 | 2022-10-07 12:11:13 -0500 | [diff] [blame] | 255 | ret = ops->transfer(dev, DMA_MEM_TO_MEM, destination, source, len); |
Andrew Davis | afea0a2 | 2022-10-07 12:11:11 -0500 | [diff] [blame] | 256 | |
| 257 | /* Clean+Invalidate the areas after, so we can see DMA'd data */ |
| 258 | dma_unmap_single(destination, len, DMA_FROM_DEVICE); |
| 259 | dma_unmap_single(source, len, DMA_TO_DEVICE); |
| 260 | |
| 261 | return ret; |
Mugunthan V N | 8c3c918 | 2016-02-15 15:31:37 +0530 | [diff] [blame] | 262 | } |
| 263 | |
| 264 | UCLASS_DRIVER(dma) = { |
| 265 | .id = UCLASS_DMA, |
| 266 | .name = "dma", |
| 267 | .flags = DM_UC_FLAG_SEQ_ALIAS, |
Simon Glass | 8a2b47f | 2020-12-03 16:55:17 -0700 | [diff] [blame] | 268 | .per_device_auto = sizeof(struct dma_dev_priv), |
Mugunthan V N | 8c3c918 | 2016-02-15 15:31:37 +0530 | [diff] [blame] | 269 | }; |