1 // SPDX-License-Identifier: GPL-2.0-only 2 /* 3 * Driver for the Atmel Extensible DMA Controller (aka XDMAC on AT91 systems) 4 * 5 * Copyright (C) 2014 Atmel Corporation 6 * 7 * Author: Ludovic Desroches <ludovic.desroches@atmel.com> 8 */ 9 10 #include <asm/barrier.h> 11 #include <dt-bindings/dma/at91.h> 12 #include <linux/clk.h> 13 #include <linux/dmaengine.h> 14 #include <linux/dmapool.h> 15 #include <linux/interrupt.h> 16 #include <linux/irq.h> 17 #include <linux/kernel.h> 18 #include <linux/list.h> 19 #include <linux/module.h> 20 #include <linux/of_dma.h> 21 #include <linux/of_platform.h> 22 #include <linux/platform_device.h> 23 #include <linux/pm.h> 24 #include <linux/pm_runtime.h> 25 26 #include "dmaengine.h" 27 28 /* Global registers */ 29 #define AT_XDMAC_GTYPE 0x00 /* Global Type Register */ 30 #define AT_XDMAC_NB_CH(i) (((i) & 0x1F) + 1) /* Number of Channels Minus One */ 31 #define AT_XDMAC_FIFO_SZ(i) (((i) >> 5) & 0x7FF) /* Number of Bytes */ 32 #define AT_XDMAC_NB_REQ(i) ((((i) >> 16) & 0x3F) + 1) /* Number of Peripheral Requests Minus One */ 33 #define AT_XDMAC_GCFG 0x04 /* Global Configuration Register */ 34 #define AT_XDMAC_WRHP(i) (((i) & 0xF) << 4) 35 #define AT_XDMAC_WRMP(i) (((i) & 0xF) << 8) 36 #define AT_XDMAC_WRLP(i) (((i) & 0xF) << 12) 37 #define AT_XDMAC_RDHP(i) (((i) & 0xF) << 16) 38 #define AT_XDMAC_RDMP(i) (((i) & 0xF) << 20) 39 #define AT_XDMAC_RDLP(i) (((i) & 0xF) << 24) 40 #define AT_XDMAC_RDSG(i) (((i) & 0xF) << 28) 41 #define AT_XDMAC_GCFG_M2M (AT_XDMAC_RDLP(0xF) | AT_XDMAC_WRLP(0xF)) 42 #define AT_XDMAC_GCFG_P2M (AT_XDMAC_RDSG(0x1) | AT_XDMAC_RDHP(0x3) | \ 43 AT_XDMAC_WRHP(0x5)) 44 #define AT_XDMAC_GWAC 0x08 /* Global Weighted Arbiter Configuration Register */ 45 #define AT_XDMAC_PW0(i) (((i) & 0xF) << 0) 46 #define AT_XDMAC_PW1(i) (((i) & 0xF) << 4) 47 #define AT_XDMAC_PW2(i) (((i) & 0xF) << 8) 48 #define AT_XDMAC_PW3(i) (((i) & 0xF) << 12) 49 #define AT_XDMAC_GWAC_M2M 0 50 #define AT_XDMAC_GWAC_P2M (AT_XDMAC_PW0(0xF) | AT_XDMAC_PW2(0xF)) 51 52 #define AT_XDMAC_GIE 0x0C /* Global Interrupt Enable Register */ 53 #define AT_XDMAC_GID 0x10 /* Global Interrupt Disable Register */ 54 #define AT_XDMAC_GIM 0x14 /* Global Interrupt Mask Register */ 55 #define AT_XDMAC_GIS 0x18 /* Global Interrupt Status Register */ 56 #define AT_XDMAC_GE 0x1C /* Global Channel Enable Register */ 57 #define AT_XDMAC_GD 0x20 /* Global Channel Disable Register */ 58 #define AT_XDMAC_GS 0x24 /* Global Channel Status Register */ 59 #define AT_XDMAC_VERSION 0xFFC /* XDMAC Version Register */ 60 61 /* Channel relative registers offsets */ 62 #define AT_XDMAC_CIE 0x00 /* Channel Interrupt Enable Register */ 63 #define AT_XDMAC_CIE_BIE BIT(0) /* End of Block Interrupt Enable Bit */ 64 #define AT_XDMAC_CIE_LIE BIT(1) /* End of Linked List Interrupt Enable Bit */ 65 #define AT_XDMAC_CIE_DIE BIT(2) /* End of Disable Interrupt Enable Bit */ 66 #define AT_XDMAC_CIE_FIE BIT(3) /* End of Flush Interrupt Enable Bit */ 67 #define AT_XDMAC_CIE_RBEIE BIT(4) /* Read Bus Error Interrupt Enable Bit */ 68 #define AT_XDMAC_CIE_WBEIE BIT(5) /* Write Bus Error Interrupt Enable Bit */ 69 #define AT_XDMAC_CIE_ROIE BIT(6) /* Request Overflow Interrupt Enable Bit */ 70 #define AT_XDMAC_CID 0x04 /* Channel Interrupt Disable Register */ 71 #define AT_XDMAC_CID_BID BIT(0) /* End of Block Interrupt Disable Bit */ 72 #define AT_XDMAC_CID_LID BIT(1) /* End of Linked List Interrupt Disable Bit */ 73 #define AT_XDMAC_CID_DID BIT(2) /* End of Disable Interrupt Disable Bit */ 74 #define AT_XDMAC_CID_FID BIT(3) /* End of Flush Interrupt Disable Bit */ 75 #define AT_XDMAC_CID_RBEID BIT(4) /* Read Bus Error Interrupt Disable Bit */ 76 #define AT_XDMAC_CID_WBEID BIT(5) /* Write Bus Error Interrupt Disable Bit */ 77 #define AT_XDMAC_CID_ROID BIT(6) /* Request Overflow Interrupt Disable Bit */ 78 #define AT_XDMAC_CIM 0x08 /* Channel Interrupt Mask Register */ 79 #define AT_XDMAC_CIM_BIM BIT(0) /* End of Block Interrupt Mask Bit */ 80 #define AT_XDMAC_CIM_LIM BIT(1) /* End of Linked List Interrupt Mask Bit */ 81 #define AT_XDMAC_CIM_DIM BIT(2) /* End of Disable Interrupt Mask Bit */ 82 #define AT_XDMAC_CIM_FIM BIT(3) /* End of Flush Interrupt Mask Bit */ 83 #define AT_XDMAC_CIM_RBEIM BIT(4) /* Read Bus Error Interrupt Mask Bit */ 84 #define AT_XDMAC_CIM_WBEIM BIT(5) /* Write Bus Error Interrupt Mask Bit */ 85 #define AT_XDMAC_CIM_ROIM BIT(6) /* Request Overflow Interrupt Mask Bit */ 86 #define AT_XDMAC_CIS 0x0C /* Channel Interrupt Status Register */ 87 #define AT_XDMAC_CIS_BIS BIT(0) /* End of Block Interrupt Status Bit */ 88 #define AT_XDMAC_CIS_LIS BIT(1) /* End of Linked List Interrupt Status Bit */ 89 #define AT_XDMAC_CIS_DIS BIT(2) /* End of Disable Interrupt Status Bit */ 90 #define AT_XDMAC_CIS_FIS BIT(3) /* End of Flush Interrupt Status Bit */ 91 #define AT_XDMAC_CIS_RBEIS BIT(4) /* Read Bus Error Interrupt Status Bit */ 92 #define AT_XDMAC_CIS_WBEIS BIT(5) /* Write Bus Error Interrupt Status Bit */ 93 #define AT_XDMAC_CIS_ROIS BIT(6) /* Request Overflow Interrupt Status Bit */ 94 #define AT_XDMAC_CSA 0x10 /* Channel Source Address Register */ 95 #define AT_XDMAC_CDA 0x14 /* Channel Destination Address Register */ 96 #define AT_XDMAC_CNDA 0x18 /* Channel Next Descriptor Address Register */ 97 #define AT_XDMAC_CNDA_NDAIF(i) ((i) & 0x1) /* Channel x Next Descriptor Interface */ 98 #define AT_XDMAC_CNDA_NDA(i) ((i) & 0xfffffffc) /* Channel x Next Descriptor Address */ 99 #define AT_XDMAC_CNDC 0x1C /* Channel Next Descriptor Control Register */ 100 #define AT_XDMAC_CNDC_NDE (0x1 << 0) /* Channel x Next Descriptor Enable */ 101 #define AT_XDMAC_CNDC_NDSUP (0x1 << 1) /* Channel x Next Descriptor Source Update */ 102 #define AT_XDMAC_CNDC_NDDUP (0x1 << 2) /* Channel x Next Descriptor Destination Update */ 103 #define AT_XDMAC_CNDC_NDVIEW_MASK GENMASK(28, 27) 104 #define AT_XDMAC_CNDC_NDVIEW_NDV0 (0x0 << 3) /* Channel x Next Descriptor View 0 */ 105 #define AT_XDMAC_CNDC_NDVIEW_NDV1 (0x1 << 3) /* Channel x Next Descriptor View 1 */ 106 #define AT_XDMAC_CNDC_NDVIEW_NDV2 (0x2 << 3) /* Channel x Next Descriptor View 2 */ 107 #define AT_XDMAC_CNDC_NDVIEW_NDV3 (0x3 << 3) /* Channel x Next Descriptor View 3 */ 108 #define AT_XDMAC_CUBC 0x20 /* Channel Microblock Control Register */ 109 #define AT_XDMAC_CBC 0x24 /* Channel Block Control Register */ 110 #define AT_XDMAC_CC 0x28 /* Channel Configuration Register */ 111 #define AT_XDMAC_CC_TYPE (0x1 << 0) /* Channel Transfer Type */ 112 #define AT_XDMAC_CC_TYPE_MEM_TRAN (0x0 << 0) /* Memory to Memory Transfer */ 113 #define AT_XDMAC_CC_TYPE_PER_TRAN (0x1 << 0) /* Peripheral to Memory or Memory to Peripheral Transfer */ 114 #define AT_XDMAC_CC_MBSIZE_MASK (0x3 << 1) 115 #define AT_XDMAC_CC_MBSIZE_SINGLE (0x0 << 1) 116 #define AT_XDMAC_CC_MBSIZE_FOUR (0x1 << 1) 117 #define AT_XDMAC_CC_MBSIZE_EIGHT (0x2 << 1) 118 #define AT_XDMAC_CC_MBSIZE_SIXTEEN (0x3 << 1) 119 #define AT_XDMAC_CC_DSYNC (0x1 << 4) /* Channel Synchronization */ 120 #define AT_XDMAC_CC_DSYNC_PER2MEM (0x0 << 4) 121 #define AT_XDMAC_CC_DSYNC_MEM2PER (0x1 << 4) 122 #define AT_XDMAC_CC_PROT (0x1 << 5) /* Channel Protection */ 123 #define AT_XDMAC_CC_PROT_SEC (0x0 << 5) 124 #define AT_XDMAC_CC_PROT_UNSEC (0x1 << 5) 125 #define AT_XDMAC_CC_SWREQ (0x1 << 6) /* Channel Software Request Trigger */ 126 #define AT_XDMAC_CC_SWREQ_HWR_CONNECTED (0x0 << 6) 127 #define AT_XDMAC_CC_SWREQ_SWR_CONNECTED (0x1 << 6) 128 #define AT_XDMAC_CC_MEMSET (0x1 << 7) /* Channel Fill Block of memory */ 129 #define AT_XDMAC_CC_MEMSET_NORMAL_MODE (0x0 << 7) 130 #define AT_XDMAC_CC_MEMSET_HW_MODE (0x1 << 7) 131 #define AT_XDMAC_CC_CSIZE(i) ((0x7 & (i)) << 8) /* Channel Chunk Size */ 132 #define AT_XDMAC_CC_DWIDTH_OFFSET 11 133 #define AT_XDMAC_CC_DWIDTH_MASK (0x3 << AT_XDMAC_CC_DWIDTH_OFFSET) 134 #define AT_XDMAC_CC_DWIDTH(i) ((0x3 & (i)) << AT_XDMAC_CC_DWIDTH_OFFSET) /* Channel Data Width */ 135 #define AT_XDMAC_CC_DWIDTH_BYTE 0x0 136 #define AT_XDMAC_CC_DWIDTH_HALFWORD 0x1 137 #define AT_XDMAC_CC_DWIDTH_WORD 0x2 138 #define AT_XDMAC_CC_DWIDTH_DWORD 0x3 139 #define AT_XDMAC_CC_SIF(i) ((0x1 & (i)) << 13) /* Channel Source Interface Identifier */ 140 #define AT_XDMAC_CC_DIF(i) ((0x1 & (i)) << 14) /* Channel Destination Interface Identifier */ 141 #define AT_XDMAC_CC_SAM_MASK (0x3 << 16) /* Channel Source Addressing Mode */ 142 #define AT_XDMAC_CC_SAM_FIXED_AM (0x0 << 16) 143 #define AT_XDMAC_CC_SAM_INCREMENTED_AM (0x1 << 16) 144 #define AT_XDMAC_CC_SAM_UBS_AM (0x2 << 16) 145 #define AT_XDMAC_CC_SAM_UBS_DS_AM (0x3 << 16) 146 #define AT_XDMAC_CC_DAM_MASK (0x3 << 18) /* Channel Source Addressing Mode */ 147 #define AT_XDMAC_CC_DAM_FIXED_AM (0x0 << 18) 148 #define AT_XDMAC_CC_DAM_INCREMENTED_AM (0x1 << 18) 149 #define AT_XDMAC_CC_DAM_UBS_AM (0x2 << 18) 150 #define AT_XDMAC_CC_DAM_UBS_DS_AM (0x3 << 18) 151 #define AT_XDMAC_CC_INITD (0x1 << 21) /* Channel Initialization Terminated (read only) */ 152 #define AT_XDMAC_CC_INITD_TERMINATED (0x0 << 21) 153 #define AT_XDMAC_CC_INITD_IN_PROGRESS (0x1 << 21) 154 #define AT_XDMAC_CC_RDIP (0x1 << 22) /* Read in Progress (read only) */ 155 #define AT_XDMAC_CC_RDIP_DONE (0x0 << 22) 156 #define AT_XDMAC_CC_RDIP_IN_PROGRESS (0x1 << 22) 157 #define AT_XDMAC_CC_WRIP (0x1 << 23) /* Write in Progress (read only) */ 158 #define AT_XDMAC_CC_WRIP_DONE (0x0 << 23) 159 #define AT_XDMAC_CC_WRIP_IN_PROGRESS (0x1 << 23) 160 #define AT_XDMAC_CC_PERID(i) ((0x7f & (i)) << 24) /* Channel Peripheral Identifier */ 161 #define AT_XDMAC_CDS_MSP 0x2C /* Channel Data Stride Memory Set Pattern */ 162 #define AT_XDMAC_CSUS 0x30 /* Channel Source Microblock Stride */ 163 #define AT_XDMAC_CDUS 0x34 /* Channel Destination Microblock Stride */ 164 165 /* Microblock control members */ 166 #define AT_XDMAC_MBR_UBC_UBLEN_MAX 0xFFFFFFUL /* Maximum Microblock Length */ 167 #define AT_XDMAC_MBR_UBC_NDE (0x1 << 24) /* Next Descriptor Enable */ 168 #define AT_XDMAC_MBR_UBC_NSEN (0x1 << 25) /* Next Descriptor Source Update */ 169 #define AT_XDMAC_MBR_UBC_NDEN (0x1 << 26) /* Next Descriptor Destination Update */ 170 #define AT_XDMAC_MBR_UBC_NDV0 (0x0 << 27) /* Next Descriptor View 0 */ 171 #define AT_XDMAC_MBR_UBC_NDV1 (0x1 << 27) /* Next Descriptor View 1 */ 172 #define AT_XDMAC_MBR_UBC_NDV2 (0x2 << 27) /* Next Descriptor View 2 */ 173 #define AT_XDMAC_MBR_UBC_NDV3 (0x3 << 27) /* Next Descriptor View 3 */ 174 175 #define AT_XDMAC_MAX_CHAN 0x20 176 #define AT_XDMAC_MAX_CSIZE 16 /* 16 data */ 177 #define AT_XDMAC_MAX_DWIDTH 8 /* 64 bits */ 178 #define AT_XDMAC_RESIDUE_MAX_RETRIES 5 179 180 #define AT_XDMAC_DMA_BUSWIDTHS\ 181 (BIT(DMA_SLAVE_BUSWIDTH_UNDEFINED) |\ 182 BIT(DMA_SLAVE_BUSWIDTH_1_BYTE) |\ 183 BIT(DMA_SLAVE_BUSWIDTH_2_BYTES) |\ 184 BIT(DMA_SLAVE_BUSWIDTH_4_BYTES) |\ 185 BIT(DMA_SLAVE_BUSWIDTH_8_BYTES)) 186 187 enum atc_status { 188 AT_XDMAC_CHAN_IS_CYCLIC = 0, 189 AT_XDMAC_CHAN_IS_PAUSED, 190 AT_XDMAC_CHAN_IS_PAUSED_INTERNAL, 191 }; 192 193 struct at_xdmac_layout { 194 /* Global Channel Read Suspend Register */ 195 u8 grs; 196 /* Global Write Suspend Register */ 197 u8 gws; 198 /* Global Channel Read Write Suspend Register */ 199 u8 grws; 200 /* Global Channel Read Write Resume Register */ 201 u8 grwr; 202 /* Global Channel Software Request Register */ 203 u8 gswr; 204 /* Global channel Software Request Status Register */ 205 u8 gsws; 206 /* Global Channel Software Flush Request Register */ 207 u8 gswf; 208 /* Channel reg base */ 209 u8 chan_cc_reg_base; 210 /* Source/Destination Interface must be specified or not */ 211 bool sdif; 212 /* AXI queue priority configuration supported */ 213 bool axi_config; 214 }; 215 216 /* ----- Channels ----- */ 217 struct at_xdmac_chan { 218 struct dma_chan chan; 219 void __iomem *ch_regs; 220 u32 mask; /* Channel Mask */ 221 u32 cfg; /* Channel Configuration Register */ 222 u8 perid; /* Peripheral ID */ 223 u8 perif; /* Peripheral Interface */ 224 u8 memif; /* Memory Interface */ 225 u32 save_cc; 226 u32 save_cim; 227 u32 save_cnda; 228 u32 save_cndc; 229 u32 irq_status; 230 unsigned long status; 231 struct tasklet_struct tasklet; 232 struct dma_slave_config sconfig; 233 234 spinlock_t lock; 235 236 struct list_head xfers_list; 237 struct list_head free_descs_list; 238 }; 239 240 241 /* ----- Controller ----- */ 242 struct at_xdmac { 243 struct dma_device dma; 244 void __iomem *regs; 245 struct device *dev; 246 int irq; 247 struct clk *clk; 248 u32 save_gim; 249 u32 save_gs; 250 struct dma_pool *at_xdmac_desc_pool; 251 const struct at_xdmac_layout *layout; 252 struct at_xdmac_chan chan[]; 253 }; 254 255 256 /* ----- Descriptors ----- */ 257 258 /* Linked List Descriptor */ 259 struct at_xdmac_lld { 260 u32 mbr_nda; /* Next Descriptor Member */ 261 u32 mbr_ubc; /* Microblock Control Member */ 262 u32 mbr_sa; /* Source Address Member */ 263 u32 mbr_da; /* Destination Address Member */ 264 u32 mbr_cfg; /* Configuration Register */ 265 u32 mbr_bc; /* Block Control Register */ 266 u32 mbr_ds; /* Data Stride Register */ 267 u32 mbr_sus; /* Source Microblock Stride Register */ 268 u32 mbr_dus; /* Destination Microblock Stride Register */ 269 }; 270 271 /* 64-bit alignment needed to update CNDA and CUBC registers in an atomic way. */ 272 struct at_xdmac_desc { 273 struct at_xdmac_lld lld; 274 enum dma_transfer_direction direction; 275 struct dma_async_tx_descriptor tx_dma_desc; 276 struct list_head desc_node; 277 /* Following members are only used by the first descriptor */ 278 bool active_xfer; 279 unsigned int xfer_size; 280 struct list_head descs_list; 281 struct list_head xfer_node; 282 } __aligned(sizeof(u64)); 283 284 static const struct at_xdmac_layout at_xdmac_sama5d4_layout = { 285 .grs = 0x28, 286 .gws = 0x2C, 287 .grws = 0x30, 288 .grwr = 0x34, 289 .gswr = 0x38, 290 .gsws = 0x3C, 291 .gswf = 0x40, 292 .chan_cc_reg_base = 0x50, 293 .sdif = true, 294 .axi_config = false, 295 }; 296 297 static const struct at_xdmac_layout at_xdmac_sama7g5_layout = { 298 .grs = 0x30, 299 .gws = 0x38, 300 .grws = 0x40, 301 .grwr = 0x44, 302 .gswr = 0x48, 303 .gsws = 0x4C, 304 .gswf = 0x50, 305 .chan_cc_reg_base = 0x60, 306 .sdif = false, 307 .axi_config = true, 308 }; 309 310 static inline void __iomem *at_xdmac_chan_reg_base(struct at_xdmac *atxdmac, unsigned int chan_nb) 311 { 312 return atxdmac->regs + (atxdmac->layout->chan_cc_reg_base + chan_nb * 0x40); 313 } 314 315 #define at_xdmac_read(atxdmac, reg) readl_relaxed((atxdmac)->regs + (reg)) 316 #define at_xdmac_write(atxdmac, reg, value) \ 317 writel_relaxed((value), (atxdmac)->regs + (reg)) 318 319 #define at_xdmac_chan_read(atchan, reg) readl_relaxed((atchan)->ch_regs + (reg)) 320 #define at_xdmac_chan_write(atchan, reg, value) writel_relaxed((value), (atchan)->ch_regs + (reg)) 321 322 static inline struct at_xdmac_chan *to_at_xdmac_chan(struct dma_chan *dchan) 323 { 324 return container_of(dchan, struct at_xdmac_chan, chan); 325 } 326 327 static inline struct at_xdmac *to_at_xdmac(struct dma_device *ddev) 328 { 329 return container_of(ddev, struct at_xdmac, dma); 330 } 331 332 static inline struct at_xdmac_desc *txd_to_at_desc(struct dma_async_tx_descriptor *txd) 333 { 334 return container_of(txd, struct at_xdmac_desc, tx_dma_desc); 335 } 336 337 static inline int at_xdmac_chan_is_cyclic(struct at_xdmac_chan *atchan) 338 { 339 return test_bit(AT_XDMAC_CHAN_IS_CYCLIC, &atchan->status); 340 } 341 342 static inline int at_xdmac_chan_is_paused(struct at_xdmac_chan *atchan) 343 { 344 return test_bit(AT_XDMAC_CHAN_IS_PAUSED, &atchan->status); 345 } 346 347 static inline int at_xdmac_chan_is_paused_internal(struct at_xdmac_chan *atchan) 348 { 349 return test_bit(AT_XDMAC_CHAN_IS_PAUSED_INTERNAL, &atchan->status); 350 } 351 352 static inline bool at_xdmac_chan_is_peripheral_xfer(u32 cfg) 353 { 354 return cfg & AT_XDMAC_CC_TYPE_PER_TRAN; 355 } 356 357 static inline u8 at_xdmac_get_dwidth(u32 cfg) 358 { 359 return (cfg & AT_XDMAC_CC_DWIDTH_MASK) >> AT_XDMAC_CC_DWIDTH_OFFSET; 360 }; 361 362 static unsigned int init_nr_desc_per_channel = 64; 363 module_param(init_nr_desc_per_channel, uint, 0644); 364 MODULE_PARM_DESC(init_nr_desc_per_channel, 365 "initial descriptors per channel (default: 64)"); 366 367 368 static void at_xdmac_runtime_suspend_descriptors(struct at_xdmac_chan *atchan) 369 { 370 struct at_xdmac *atxdmac = to_at_xdmac(atchan->chan.device); 371 struct at_xdmac_desc *desc, *_desc; 372 373 list_for_each_entry_safe(desc, _desc, &atchan->xfers_list, xfer_node) { 374 if (!desc->active_xfer) 375 continue; 376 377 pm_runtime_put_autosuspend(atxdmac->dev); 378 } 379 } 380 381 static int at_xdmac_runtime_resume_descriptors(struct at_xdmac_chan *atchan) 382 { 383 struct at_xdmac *atxdmac = to_at_xdmac(atchan->chan.device); 384 struct at_xdmac_desc *desc, *_desc; 385 int ret; 386 387 list_for_each_entry_safe(desc, _desc, &atchan->xfers_list, xfer_node) { 388 if (!desc->active_xfer) 389 continue; 390 391 ret = pm_runtime_resume_and_get(atxdmac->dev); 392 if (ret < 0) 393 return ret; 394 } 395 396 return 0; 397 } 398 399 static bool at_xdmac_chan_is_enabled(struct at_xdmac_chan *atchan) 400 { 401 struct at_xdmac *atxdmac = to_at_xdmac(atchan->chan.device); 402 int ret; 403 404 ret = pm_runtime_resume_and_get(atxdmac->dev); 405 if (ret < 0) 406 return false; 407 408 ret = !!(at_xdmac_chan_read(atchan, AT_XDMAC_GS) & atchan->mask); 409 410 pm_runtime_put_autosuspend(atxdmac->dev); 411 412 return ret; 413 } 414 415 static void at_xdmac_off(struct at_xdmac *atxdmac, bool suspend_descriptors) 416 { 417 struct dma_chan *chan, *_chan; 418 struct at_xdmac_chan *atchan; 419 int ret; 420 421 ret = pm_runtime_resume_and_get(atxdmac->dev); 422 if (ret < 0) 423 return; 424 425 at_xdmac_write(atxdmac, AT_XDMAC_GD, -1L); 426 427 /* Wait that all chans are disabled. */ 428 while (at_xdmac_read(atxdmac, AT_XDMAC_GS)) 429 cpu_relax(); 430 431 at_xdmac_write(atxdmac, AT_XDMAC_GID, -1L); 432 433 /* Decrement runtime PM ref counter for each active descriptor. */ 434 if (!list_empty(&atxdmac->dma.channels) && suspend_descriptors) { 435 list_for_each_entry_safe(chan, _chan, &atxdmac->dma.channels, 436 device_node) { 437 atchan = to_at_xdmac_chan(chan); 438 at_xdmac_runtime_suspend_descriptors(atchan); 439 } 440 } 441 442 pm_runtime_put_autosuspend(atxdmac->dev); 443 } 444 445 /* Call with lock hold. */ 446 static void at_xdmac_start_xfer(struct at_xdmac_chan *atchan, 447 struct at_xdmac_desc *first) 448 { 449 struct at_xdmac *atxdmac = to_at_xdmac(atchan->chan.device); 450 u32 reg; 451 int ret; 452 453 ret = pm_runtime_resume_and_get(atxdmac->dev); 454 if (ret < 0) 455 return; 456 457 dev_vdbg(dmaengine_chan_dev(&atchan->chan), "%s: desc 0x%p\n", __func__, first); 458 459 /* Set transfer as active to not try to start it again. */ 460 first->active_xfer = true; 461 462 /* Tell xdmac where to get the first descriptor. */ 463 reg = AT_XDMAC_CNDA_NDA(first->tx_dma_desc.phys); 464 if (atxdmac->layout->sdif) 465 reg |= AT_XDMAC_CNDA_NDAIF(atchan->memif); 466 467 at_xdmac_chan_write(atchan, AT_XDMAC_CNDA, reg); 468 469 /* 470 * When doing non cyclic transfer we need to use the next 471 * descriptor view 2 since some fields of the configuration register 472 * depend on transfer size and src/dest addresses. 473 */ 474 if (at_xdmac_chan_is_cyclic(atchan)) 475 reg = AT_XDMAC_CNDC_NDVIEW_NDV1; 476 else if ((first->lld.mbr_ubc & 477 AT_XDMAC_CNDC_NDVIEW_MASK) == AT_XDMAC_MBR_UBC_NDV3) 478 reg = AT_XDMAC_CNDC_NDVIEW_NDV3; 479 else 480 reg = AT_XDMAC_CNDC_NDVIEW_NDV2; 481 /* 482 * Even if the register will be updated from the configuration in the 483 * descriptor when using view 2 or higher, the PROT bit won't be set 484 * properly. This bit can be modified only by using the channel 485 * configuration register. 486 */ 487 at_xdmac_chan_write(atchan, AT_XDMAC_CC, first->lld.mbr_cfg); 488 489 reg |= AT_XDMAC_CNDC_NDDUP 490 | AT_XDMAC_CNDC_NDSUP 491 | AT_XDMAC_CNDC_NDE; 492 at_xdmac_chan_write(atchan, AT_XDMAC_CNDC, reg); 493 494 dev_vdbg(dmaengine_chan_dev(&atchan->chan), 495 "%s: CC=0x%08x CNDA=0x%08x, CNDC=0x%08x, CSA=0x%08x, CDA=0x%08x, CUBC=0x%08x\n", 496 __func__, at_xdmac_chan_read(atchan, AT_XDMAC_CC), 497 at_xdmac_chan_read(atchan, AT_XDMAC_CNDA), 498 at_xdmac_chan_read(atchan, AT_XDMAC_CNDC), 499 at_xdmac_chan_read(atchan, AT_XDMAC_CSA), 500 at_xdmac_chan_read(atchan, AT_XDMAC_CDA), 501 at_xdmac_chan_read(atchan, AT_XDMAC_CUBC)); 502 503 at_xdmac_chan_write(atchan, AT_XDMAC_CID, 0xffffffff); 504 reg = AT_XDMAC_CIE_RBEIE | AT_XDMAC_CIE_WBEIE; 505 /* 506 * Request Overflow Error is only for peripheral synchronized transfers 507 */ 508 if (at_xdmac_chan_is_peripheral_xfer(first->lld.mbr_cfg)) 509 reg |= AT_XDMAC_CIE_ROIE; 510 511 /* 512 * There is no end of list when doing cyclic dma, we need to get 513 * an interrupt after each periods. 514 */ 515 if (at_xdmac_chan_is_cyclic(atchan)) 516 at_xdmac_chan_write(atchan, AT_XDMAC_CIE, 517 reg | AT_XDMAC_CIE_BIE); 518 else 519 at_xdmac_chan_write(atchan, AT_XDMAC_CIE, 520 reg | AT_XDMAC_CIE_LIE); 521 at_xdmac_write(atxdmac, AT_XDMAC_GIE, atchan->mask); 522 dev_vdbg(dmaengine_chan_dev(&atchan->chan), 523 "%s: enable channel (0x%08x)\n", __func__, atchan->mask); 524 wmb(); 525 at_xdmac_write(atxdmac, AT_XDMAC_GE, atchan->mask); 526 527 dev_vdbg(dmaengine_chan_dev(&atchan->chan), 528 "%s: CC=0x%08x CNDA=0x%08x, CNDC=0x%08x, CSA=0x%08x, CDA=0x%08x, CUBC=0x%08x\n", 529 __func__, at_xdmac_chan_read(atchan, AT_XDMAC_CC), 530 at_xdmac_chan_read(atchan, AT_XDMAC_CNDA), 531 at_xdmac_chan_read(atchan, AT_XDMAC_CNDC), 532 at_xdmac_chan_read(atchan, AT_XDMAC_CSA), 533 at_xdmac_chan_read(atchan, AT_XDMAC_CDA), 534 at_xdmac_chan_read(atchan, AT_XDMAC_CUBC)); 535 } 536 537 static dma_cookie_t at_xdmac_tx_submit(struct dma_async_tx_descriptor *tx) 538 { 539 struct at_xdmac_desc *desc = txd_to_at_desc(tx); 540 struct at_xdmac_chan *atchan = to_at_xdmac_chan(tx->chan); 541 dma_cookie_t cookie; 542 unsigned long irqflags; 543 544 spin_lock_irqsave(&atchan->lock, irqflags); 545 cookie = dma_cookie_assign(tx); 546 547 list_add_tail(&desc->xfer_node, &atchan->xfers_list); 548 spin_unlock_irqrestore(&atchan->lock, irqflags); 549 550 dev_vdbg(dmaengine_chan_dev(tx->chan), "%s: atchan 0x%p, add desc 0x%p to xfers_list\n", 551 __func__, atchan, desc); 552 553 return cookie; 554 } 555 556 static struct at_xdmac_desc *at_xdmac_alloc_desc(struct dma_chan *chan, 557 gfp_t gfp_flags) 558 { 559 struct at_xdmac_desc *desc; 560 struct at_xdmac *atxdmac = to_at_xdmac(chan->device); 561 dma_addr_t phys; 562 563 desc = dma_pool_zalloc(atxdmac->at_xdmac_desc_pool, gfp_flags, &phys); 564 if (desc) { 565 INIT_LIST_HEAD(&desc->descs_list); 566 dma_async_tx_descriptor_init(&desc->tx_dma_desc, chan); 567 desc->tx_dma_desc.tx_submit = at_xdmac_tx_submit; 568 desc->tx_dma_desc.phys = phys; 569 } 570 571 return desc; 572 } 573 574 static void at_xdmac_init_used_desc(struct at_xdmac_desc *desc) 575 { 576 memset(&desc->lld, 0, sizeof(desc->lld)); 577 INIT_LIST_HEAD(&desc->descs_list); 578 desc->direction = DMA_TRANS_NONE; 579 desc->xfer_size = 0; 580 desc->active_xfer = false; 581 } 582 583 /* Call must be protected by lock. */ 584 static struct at_xdmac_desc *at_xdmac_get_desc(struct at_xdmac_chan *atchan) 585 { 586 struct at_xdmac_desc *desc; 587 588 if (list_empty(&atchan->free_descs_list)) { 589 desc = at_xdmac_alloc_desc(&atchan->chan, GFP_NOWAIT); 590 } else { 591 desc = list_first_entry(&atchan->free_descs_list, 592 struct at_xdmac_desc, desc_node); 593 list_del(&desc->desc_node); 594 at_xdmac_init_used_desc(desc); 595 } 596 597 return desc; 598 } 599 600 static void at_xdmac_queue_desc(struct dma_chan *chan, 601 struct at_xdmac_desc *prev, 602 struct at_xdmac_desc *desc) 603 { 604 if (!prev || !desc) 605 return; 606 607 prev->lld.mbr_nda = desc->tx_dma_desc.phys; 608 prev->lld.mbr_ubc |= AT_XDMAC_MBR_UBC_NDE; 609 610 dev_dbg(dmaengine_chan_dev(chan), "%s: chain lld: prev=0x%p, mbr_nda=%pad\n", 611 __func__, prev, &prev->lld.mbr_nda); 612 } 613 614 static inline void at_xdmac_increment_block_count(struct dma_chan *chan, 615 struct at_xdmac_desc *desc) 616 { 617 if (!desc) 618 return; 619 620 desc->lld.mbr_bc++; 621 622 dev_dbg(dmaengine_chan_dev(chan), 623 "%s: incrementing the block count of the desc 0x%p\n", 624 __func__, desc); 625 } 626 627 static struct dma_chan *at_xdmac_xlate(struct of_phandle_args *dma_spec, 628 struct of_dma *of_dma) 629 { 630 struct at_xdmac *atxdmac = of_dma->of_dma_data; 631 struct at_xdmac_chan *atchan; 632 struct dma_chan *chan; 633 struct device *dev = atxdmac->dma.dev; 634 635 if (dma_spec->args_count != 1) { 636 dev_err(dev, "dma phandler args: bad number of args\n"); 637 return NULL; 638 } 639 640 chan = dma_get_any_slave_channel(&atxdmac->dma); 641 if (!chan) { 642 dev_err(dev, "can't get a dma channel\n"); 643 return NULL; 644 } 645 646 atchan = to_at_xdmac_chan(chan); 647 atchan->memif = AT91_XDMAC_DT_GET_MEM_IF(dma_spec->args[0]); 648 atchan->perif = AT91_XDMAC_DT_GET_PER_IF(dma_spec->args[0]); 649 atchan->perid = AT91_XDMAC_DT_GET_PERID(dma_spec->args[0]); 650 dev_dbg(dev, "chan dt cfg: memif=%u perif=%u perid=%u\n", 651 atchan->memif, atchan->perif, atchan->perid); 652 653 return chan; 654 } 655 656 static int at_xdmac_compute_chan_conf(struct dma_chan *chan, 657 enum dma_transfer_direction direction) 658 { 659 struct at_xdmac_chan *atchan = to_at_xdmac_chan(chan); 660 struct at_xdmac *atxdmac = to_at_xdmac(atchan->chan.device); 661 int csize, dwidth; 662 663 if (direction == DMA_DEV_TO_MEM) { 664 atchan->cfg = 665 AT91_XDMAC_DT_PERID(atchan->perid) 666 | AT_XDMAC_CC_DAM_INCREMENTED_AM 667 | AT_XDMAC_CC_SAM_FIXED_AM 668 | AT_XDMAC_CC_SWREQ_HWR_CONNECTED 669 | AT_XDMAC_CC_DSYNC_PER2MEM 670 | AT_XDMAC_CC_MBSIZE_SIXTEEN 671 | AT_XDMAC_CC_TYPE_PER_TRAN; 672 if (atxdmac->layout->sdif) 673 atchan->cfg |= AT_XDMAC_CC_DIF(atchan->memif) | 674 AT_XDMAC_CC_SIF(atchan->perif); 675 676 csize = ffs(atchan->sconfig.src_maxburst) - 1; 677 if (csize < 0) { 678 dev_err(dmaengine_chan_dev(chan), "invalid src maxburst value\n"); 679 return -EINVAL; 680 } 681 atchan->cfg |= AT_XDMAC_CC_CSIZE(csize); 682 dwidth = ffs(atchan->sconfig.src_addr_width) - 1; 683 if (dwidth < 0) { 684 dev_err(dmaengine_chan_dev(chan), "invalid src addr width value\n"); 685 return -EINVAL; 686 } 687 atchan->cfg |= AT_XDMAC_CC_DWIDTH(dwidth); 688 } else if (direction == DMA_MEM_TO_DEV) { 689 atchan->cfg = 690 AT91_XDMAC_DT_PERID(atchan->perid) 691 | AT_XDMAC_CC_DAM_FIXED_AM 692 | AT_XDMAC_CC_SAM_INCREMENTED_AM 693 | AT_XDMAC_CC_SWREQ_HWR_CONNECTED 694 | AT_XDMAC_CC_DSYNC_MEM2PER 695 | AT_XDMAC_CC_MBSIZE_SIXTEEN 696 | AT_XDMAC_CC_TYPE_PER_TRAN; 697 if (atxdmac->layout->sdif) 698 atchan->cfg |= AT_XDMAC_CC_DIF(atchan->perif) | 699 AT_XDMAC_CC_SIF(atchan->memif); 700 701 csize = ffs(atchan->sconfig.dst_maxburst) - 1; 702 if (csize < 0) { 703 dev_err(dmaengine_chan_dev(chan), "invalid src maxburst value\n"); 704 return -EINVAL; 705 } 706 atchan->cfg |= AT_XDMAC_CC_CSIZE(csize); 707 dwidth = ffs(atchan->sconfig.dst_addr_width) - 1; 708 if (dwidth < 0) { 709 dev_err(dmaengine_chan_dev(chan), "invalid dst addr width value\n"); 710 return -EINVAL; 711 } 712 atchan->cfg |= AT_XDMAC_CC_DWIDTH(dwidth); 713 } 714 715 dev_dbg(dmaengine_chan_dev(chan), "%s: cfg=0x%08x\n", __func__, atchan->cfg); 716 717 return 0; 718 } 719 720 /* 721 * Only check that maxburst and addr width values are supported by 722 * the controller but not that the configuration is good to perform the 723 * transfer since we don't know the direction at this stage. 724 */ 725 static int at_xdmac_check_slave_config(struct dma_slave_config *sconfig) 726 { 727 if ((sconfig->src_maxburst > AT_XDMAC_MAX_CSIZE) 728 || (sconfig->dst_maxburst > AT_XDMAC_MAX_CSIZE)) 729 return -EINVAL; 730 731 if ((sconfig->src_addr_width > AT_XDMAC_MAX_DWIDTH) 732 || (sconfig->dst_addr_width > AT_XDMAC_MAX_DWIDTH)) 733 return -EINVAL; 734 735 return 0; 736 } 737 738 static int at_xdmac_set_slave_config(struct dma_chan *chan, 739 struct dma_slave_config *sconfig) 740 { 741 struct at_xdmac_chan *atchan = to_at_xdmac_chan(chan); 742 743 if (at_xdmac_check_slave_config(sconfig)) { 744 dev_err(dmaengine_chan_dev(chan), "invalid slave configuration\n"); 745 return -EINVAL; 746 } 747 748 memcpy(&atchan->sconfig, sconfig, sizeof(atchan->sconfig)); 749 750 return 0; 751 } 752 753 static struct dma_async_tx_descriptor * 754 at_xdmac_prep_slave_sg(struct dma_chan *chan, struct scatterlist *sgl, 755 unsigned int sg_len, enum dma_transfer_direction direction, 756 unsigned long flags, void *context) 757 { 758 struct at_xdmac_chan *atchan = to_at_xdmac_chan(chan); 759 struct at_xdmac_desc *first = NULL, *prev = NULL; 760 struct scatterlist *sg; 761 int i; 762 unsigned int xfer_size = 0; 763 unsigned long irqflags; 764 struct dma_async_tx_descriptor *ret = NULL; 765 766 if (!sgl) 767 return NULL; 768 769 if (!is_slave_direction(direction)) { 770 dev_err(dmaengine_chan_dev(chan), "invalid DMA direction\n"); 771 return NULL; 772 } 773 774 dev_dbg(dmaengine_chan_dev(chan), "%s: sg_len=%d, dir=%s, flags=0x%lx\n", 775 __func__, sg_len, 776 direction == DMA_MEM_TO_DEV ? "to device" : "from device", 777 flags); 778 779 /* Protect dma_sconfig field that can be modified by set_slave_conf. */ 780 spin_lock_irqsave(&atchan->lock, irqflags); 781 782 if (at_xdmac_compute_chan_conf(chan, direction)) 783 goto spin_unlock; 784 785 /* Prepare descriptors. */ 786 for_each_sg(sgl, sg, sg_len, i) { 787 struct at_xdmac_desc *desc = NULL; 788 u32 len, mem, dwidth, fixed_dwidth; 789 790 len = sg_dma_len(sg); 791 mem = sg_dma_address(sg); 792 if (unlikely(!len)) { 793 dev_err(dmaengine_chan_dev(chan), "sg data length is zero\n"); 794 goto spin_unlock; 795 } 796 dev_dbg(dmaengine_chan_dev(chan), "%s: * sg%d len=%u, mem=0x%08x\n", 797 __func__, i, len, mem); 798 799 desc = at_xdmac_get_desc(atchan); 800 if (!desc) { 801 dev_err(dmaengine_chan_dev(chan), "can't get descriptor\n"); 802 if (first) 803 list_splice_tail_init(&first->descs_list, 804 &atchan->free_descs_list); 805 goto spin_unlock; 806 } 807 808 /* Linked list descriptor setup. */ 809 if (direction == DMA_DEV_TO_MEM) { 810 desc->lld.mbr_sa = atchan->sconfig.src_addr; 811 desc->lld.mbr_da = mem; 812 } else { 813 desc->lld.mbr_sa = mem; 814 desc->lld.mbr_da = atchan->sconfig.dst_addr; 815 } 816 dwidth = at_xdmac_get_dwidth(atchan->cfg); 817 fixed_dwidth = IS_ALIGNED(len, 1 << dwidth) 818 ? dwidth 819 : AT_XDMAC_CC_DWIDTH_BYTE; 820 desc->lld.mbr_ubc = AT_XDMAC_MBR_UBC_NDV2 /* next descriptor view */ 821 | AT_XDMAC_MBR_UBC_NDEN /* next descriptor dst parameter update */ 822 | AT_XDMAC_MBR_UBC_NSEN /* next descriptor src parameter update */ 823 | (len >> fixed_dwidth); /* microblock length */ 824 desc->lld.mbr_cfg = (atchan->cfg & ~AT_XDMAC_CC_DWIDTH_MASK) | 825 AT_XDMAC_CC_DWIDTH(fixed_dwidth); 826 dev_dbg(dmaengine_chan_dev(chan), 827 "%s: lld: mbr_sa=%pad, mbr_da=%pad, mbr_ubc=0x%08x\n", 828 __func__, &desc->lld.mbr_sa, &desc->lld.mbr_da, desc->lld.mbr_ubc); 829 830 /* Chain lld. */ 831 if (prev) 832 at_xdmac_queue_desc(chan, prev, desc); 833 834 prev = desc; 835 if (!first) 836 first = desc; 837 838 dev_dbg(dmaengine_chan_dev(chan), "%s: add desc 0x%p to descs_list 0x%p\n", 839 __func__, desc, first); 840 list_add_tail(&desc->desc_node, &first->descs_list); 841 xfer_size += len; 842 } 843 844 845 first->tx_dma_desc.flags = flags; 846 first->xfer_size = xfer_size; 847 first->direction = direction; 848 ret = &first->tx_dma_desc; 849 850 spin_unlock: 851 spin_unlock_irqrestore(&atchan->lock, irqflags); 852 return ret; 853 } 854 855 static struct dma_async_tx_descriptor * 856 at_xdmac_prep_dma_cyclic(struct dma_chan *chan, dma_addr_t buf_addr, 857 size_t buf_len, size_t period_len, 858 enum dma_transfer_direction direction, 859 unsigned long flags) 860 { 861 struct at_xdmac_chan *atchan = to_at_xdmac_chan(chan); 862 struct at_xdmac_desc *first = NULL, *prev = NULL; 863 unsigned int periods = buf_len / period_len; 864 int i; 865 unsigned long irqflags; 866 867 dev_dbg(dmaengine_chan_dev(chan), "%s: buf_addr=%pad, buf_len=%zd, period_len=%zd, dir=%s, flags=0x%lx\n", 868 __func__, &buf_addr, buf_len, period_len, 869 direction == DMA_MEM_TO_DEV ? "mem2per" : "per2mem", flags); 870 871 if (!is_slave_direction(direction)) { 872 dev_err(dmaengine_chan_dev(chan), "invalid DMA direction\n"); 873 return NULL; 874 } 875 876 if (test_and_set_bit(AT_XDMAC_CHAN_IS_CYCLIC, &atchan->status)) { 877 dev_err(dmaengine_chan_dev(chan), "channel currently used\n"); 878 return NULL; 879 } 880 881 if (at_xdmac_compute_chan_conf(chan, direction)) 882 return NULL; 883 884 for (i = 0; i < periods; i++) { 885 struct at_xdmac_desc *desc = NULL; 886 887 spin_lock_irqsave(&atchan->lock, irqflags); 888 desc = at_xdmac_get_desc(atchan); 889 if (!desc) { 890 dev_err(dmaengine_chan_dev(chan), "can't get descriptor\n"); 891 if (first) 892 list_splice_tail_init(&first->descs_list, 893 &atchan->free_descs_list); 894 spin_unlock_irqrestore(&atchan->lock, irqflags); 895 return NULL; 896 } 897 spin_unlock_irqrestore(&atchan->lock, irqflags); 898 dev_dbg(dmaengine_chan_dev(chan), 899 "%s: desc=0x%p, tx_dma_desc.phys=%pad\n", 900 __func__, desc, &desc->tx_dma_desc.phys); 901 902 if (direction == DMA_DEV_TO_MEM) { 903 desc->lld.mbr_sa = atchan->sconfig.src_addr; 904 desc->lld.mbr_da = buf_addr + i * period_len; 905 } else { 906 desc->lld.mbr_sa = buf_addr + i * period_len; 907 desc->lld.mbr_da = atchan->sconfig.dst_addr; 908 } 909 desc->lld.mbr_cfg = atchan->cfg; 910 desc->lld.mbr_ubc = AT_XDMAC_MBR_UBC_NDV1 911 | AT_XDMAC_MBR_UBC_NDEN 912 | AT_XDMAC_MBR_UBC_NSEN 913 | period_len >> at_xdmac_get_dwidth(desc->lld.mbr_cfg); 914 915 dev_dbg(dmaengine_chan_dev(chan), 916 "%s: lld: mbr_sa=%pad, mbr_da=%pad, mbr_ubc=0x%08x\n", 917 __func__, &desc->lld.mbr_sa, &desc->lld.mbr_da, desc->lld.mbr_ubc); 918 919 /* Chain lld. */ 920 if (prev) 921 at_xdmac_queue_desc(chan, prev, desc); 922 923 prev = desc; 924 if (!first) 925 first = desc; 926 927 dev_dbg(dmaengine_chan_dev(chan), "%s: add desc 0x%p to descs_list 0x%p\n", 928 __func__, desc, first); 929 list_add_tail(&desc->desc_node, &first->descs_list); 930 } 931 932 at_xdmac_queue_desc(chan, prev, first); 933 first->tx_dma_desc.flags = flags; 934 first->xfer_size = buf_len; 935 first->direction = direction; 936 937 return &first->tx_dma_desc; 938 } 939 940 static inline u32 at_xdmac_align_width(struct dma_chan *chan, dma_addr_t addr) 941 { 942 u32 width; 943 944 /* 945 * Check address alignment to select the greater data width we 946 * can use. 947 * 948 * Some XDMAC implementations don't provide dword transfer, in 949 * this case selecting dword has the same behavior as 950 * selecting word transfers. 951 */ 952 if (!(addr & 7)) { 953 width = AT_XDMAC_CC_DWIDTH_DWORD; 954 dev_dbg(dmaengine_chan_dev(chan), "%s: dwidth: double word\n", __func__); 955 } else if (!(addr & 3)) { 956 width = AT_XDMAC_CC_DWIDTH_WORD; 957 dev_dbg(dmaengine_chan_dev(chan), "%s: dwidth: word\n", __func__); 958 } else if (!(addr & 1)) { 959 width = AT_XDMAC_CC_DWIDTH_HALFWORD; 960 dev_dbg(dmaengine_chan_dev(chan), "%s: dwidth: half word\n", __func__); 961 } else { 962 width = AT_XDMAC_CC_DWIDTH_BYTE; 963 dev_dbg(dmaengine_chan_dev(chan), "%s: dwidth: byte\n", __func__); 964 } 965 966 return width; 967 } 968 969 static struct at_xdmac_desc * 970 at_xdmac_interleaved_queue_desc(struct dma_chan *chan, 971 struct at_xdmac_chan *atchan, 972 struct at_xdmac_desc *prev, 973 dma_addr_t src, dma_addr_t dst, 974 struct dma_interleaved_template *xt, 975 struct data_chunk *chunk) 976 { 977 struct at_xdmac_desc *desc; 978 u32 dwidth; 979 unsigned long flags; 980 size_t ublen; 981 /* 982 * WARNING: The channel configuration is set here since there is no 983 * dmaengine_slave_config call in this case. Moreover we don't know the 984 * direction, it involves we can't dynamically set the source and dest 985 * interface so we have to use the same one. Only interface 0 allows EBI 986 * access. Hopefully we can access DDR through both ports (at least on 987 * SAMA5D4x), so we can use the same interface for source and dest, 988 * that solves the fact we don't know the direction. 989 * ERRATA: Even if useless for memory transfers, the PERID has to not 990 * match the one of another channel. If not, it could lead to spurious 991 * flag status. 992 * For SAMA7G5x case, the SIF and DIF fields are no longer used. 993 * Thus, no need to have the SIF/DIF interfaces here. 994 * For SAMA5D4x and SAMA5D2x the SIF and DIF are already configured as 995 * zero. 996 */ 997 u32 chan_cc = AT_XDMAC_CC_PERID(0x7f) 998 | AT_XDMAC_CC_MBSIZE_SIXTEEN 999 | AT_XDMAC_CC_TYPE_MEM_TRAN; 1000 1001 dwidth = at_xdmac_align_width(chan, src | dst | chunk->size); 1002 if (chunk->size >= (AT_XDMAC_MBR_UBC_UBLEN_MAX << dwidth)) { 1003 dev_dbg(dmaengine_chan_dev(chan), 1004 "%s: chunk too big (%zu, max size %lu)...\n", 1005 __func__, chunk->size, 1006 AT_XDMAC_MBR_UBC_UBLEN_MAX << dwidth); 1007 return NULL; 1008 } 1009 1010 if (prev) 1011 dev_dbg(dmaengine_chan_dev(chan), 1012 "Adding items at the end of desc 0x%p\n", prev); 1013 1014 if (xt->src_inc) { 1015 if (xt->src_sgl) 1016 chan_cc |= AT_XDMAC_CC_SAM_UBS_AM; 1017 else 1018 chan_cc |= AT_XDMAC_CC_SAM_INCREMENTED_AM; 1019 } 1020 1021 if (xt->dst_inc) { 1022 if (xt->dst_sgl) 1023 chan_cc |= AT_XDMAC_CC_DAM_UBS_AM; 1024 else 1025 chan_cc |= AT_XDMAC_CC_DAM_INCREMENTED_AM; 1026 } 1027 1028 spin_lock_irqsave(&atchan->lock, flags); 1029 desc = at_xdmac_get_desc(atchan); 1030 spin_unlock_irqrestore(&atchan->lock, flags); 1031 if (!desc) { 1032 dev_err(dmaengine_chan_dev(chan), "can't get descriptor\n"); 1033 return NULL; 1034 } 1035 1036 chan_cc |= AT_XDMAC_CC_DWIDTH(dwidth); 1037 1038 ublen = chunk->size >> dwidth; 1039 1040 desc->lld.mbr_sa = src; 1041 desc->lld.mbr_da = dst; 1042 desc->lld.mbr_sus = dmaengine_get_src_icg(xt, chunk); 1043 desc->lld.mbr_dus = dmaengine_get_dst_icg(xt, chunk); 1044 1045 desc->lld.mbr_ubc = AT_XDMAC_MBR_UBC_NDV3 1046 | AT_XDMAC_MBR_UBC_NDEN 1047 | AT_XDMAC_MBR_UBC_NSEN 1048 | ublen; 1049 desc->lld.mbr_cfg = chan_cc; 1050 1051 dev_dbg(dmaengine_chan_dev(chan), 1052 "%s: lld: mbr_sa=%pad, mbr_da=%pad, mbr_ubc=0x%08x, mbr_cfg=0x%08x\n", 1053 __func__, &desc->lld.mbr_sa, &desc->lld.mbr_da, 1054 desc->lld.mbr_ubc, desc->lld.mbr_cfg); 1055 1056 /* Chain lld. */ 1057 if (prev) 1058 at_xdmac_queue_desc(chan, prev, desc); 1059 1060 return desc; 1061 } 1062 1063 static struct dma_async_tx_descriptor * 1064 at_xdmac_prep_interleaved(struct dma_chan *chan, 1065 struct dma_interleaved_template *xt, 1066 unsigned long flags) 1067 { 1068 struct at_xdmac_chan *atchan = to_at_xdmac_chan(chan); 1069 struct at_xdmac_desc *prev = NULL, *first = NULL; 1070 dma_addr_t dst_addr, src_addr; 1071 size_t src_skip = 0, dst_skip = 0, len = 0; 1072 struct data_chunk *chunk; 1073 int i; 1074 1075 if (!xt || !xt->numf || (xt->dir != DMA_MEM_TO_MEM)) 1076 return NULL; 1077 1078 /* 1079 * TODO: Handle the case where we have to repeat a chain of 1080 * descriptors... 1081 */ 1082 if ((xt->numf > 1) && (xt->frame_size > 1)) 1083 return NULL; 1084 1085 dev_dbg(dmaengine_chan_dev(chan), "%s: src=%pad, dest=%pad, numf=%zu, frame_size=%zu, flags=0x%lx\n", 1086 __func__, &xt->src_start, &xt->dst_start, xt->numf, 1087 xt->frame_size, flags); 1088 1089 src_addr = xt->src_start; 1090 dst_addr = xt->dst_start; 1091 1092 if (xt->numf > 1) { 1093 first = at_xdmac_interleaved_queue_desc(chan, atchan, 1094 NULL, 1095 src_addr, dst_addr, 1096 xt, xt->sgl); 1097 if (!first) 1098 return NULL; 1099 1100 /* Length of the block is (BLEN+1) microblocks. */ 1101 for (i = 0; i < xt->numf - 1; i++) 1102 at_xdmac_increment_block_count(chan, first); 1103 1104 dev_dbg(dmaengine_chan_dev(chan), "%s: add desc 0x%p to descs_list 0x%p\n", 1105 __func__, first, first); 1106 list_add_tail(&first->desc_node, &first->descs_list); 1107 } else { 1108 for (i = 0; i < xt->frame_size; i++) { 1109 size_t src_icg = 0, dst_icg = 0; 1110 struct at_xdmac_desc *desc; 1111 1112 chunk = xt->sgl + i; 1113 1114 dst_icg = dmaengine_get_dst_icg(xt, chunk); 1115 src_icg = dmaengine_get_src_icg(xt, chunk); 1116 1117 src_skip = chunk->size + src_icg; 1118 dst_skip = chunk->size + dst_icg; 1119 1120 dev_dbg(dmaengine_chan_dev(chan), 1121 "%s: chunk size=%zu, src icg=%zu, dst icg=%zu\n", 1122 __func__, chunk->size, src_icg, dst_icg); 1123 1124 desc = at_xdmac_interleaved_queue_desc(chan, atchan, 1125 prev, 1126 src_addr, dst_addr, 1127 xt, chunk); 1128 if (!desc) { 1129 if (first) 1130 list_splice_tail_init(&first->descs_list, 1131 &atchan->free_descs_list); 1132 return NULL; 1133 } 1134 1135 if (!first) 1136 first = desc; 1137 1138 dev_dbg(dmaengine_chan_dev(chan), "%s: add desc 0x%p to descs_list 0x%p\n", 1139 __func__, desc, first); 1140 list_add_tail(&desc->desc_node, &first->descs_list); 1141 1142 if (xt->src_sgl) 1143 src_addr += src_skip; 1144 1145 if (xt->dst_sgl) 1146 dst_addr += dst_skip; 1147 1148 len += chunk->size; 1149 prev = desc; 1150 } 1151 } 1152 1153 first->tx_dma_desc.cookie = -EBUSY; 1154 first->tx_dma_desc.flags = flags; 1155 first->xfer_size = len; 1156 1157 return &first->tx_dma_desc; 1158 } 1159 1160 static struct dma_async_tx_descriptor * 1161 at_xdmac_prep_dma_memcpy(struct dma_chan *chan, dma_addr_t dest, dma_addr_t src, 1162 size_t len, unsigned long flags) 1163 { 1164 struct at_xdmac_chan *atchan = to_at_xdmac_chan(chan); 1165 struct at_xdmac_desc *first = NULL, *prev = NULL; 1166 size_t remaining_size = len, xfer_size = 0, ublen; 1167 dma_addr_t src_addr = src, dst_addr = dest; 1168 u32 dwidth; 1169 /* 1170 * WARNING: We don't know the direction, it involves we can't 1171 * dynamically set the source and dest interface so we have to use the 1172 * same one. Only interface 0 allows EBI access. Hopefully we can 1173 * access DDR through both ports (at least on SAMA5D4x), so we can use 1174 * the same interface for source and dest, that solves the fact we 1175 * don't know the direction. 1176 * ERRATA: Even if useless for memory transfers, the PERID has to not 1177 * match the one of another channel. If not, it could lead to spurious 1178 * flag status. 1179 * For SAMA7G5x case, the SIF and DIF fields are no longer used. 1180 * Thus, no need to have the SIF/DIF interfaces here. 1181 * For SAMA5D4x and SAMA5D2x the SIF and DIF are already configured as 1182 * zero. 1183 */ 1184 u32 chan_cc = AT_XDMAC_CC_PERID(0x7f) 1185 | AT_XDMAC_CC_DAM_INCREMENTED_AM 1186 | AT_XDMAC_CC_SAM_INCREMENTED_AM 1187 | AT_XDMAC_CC_MBSIZE_SIXTEEN 1188 | AT_XDMAC_CC_TYPE_MEM_TRAN; 1189 unsigned long irqflags; 1190 1191 dev_dbg(dmaengine_chan_dev(chan), "%s: src=%pad, dest=%pad, len=%zd, flags=0x%lx\n", 1192 __func__, &src, &dest, len, flags); 1193 1194 if (unlikely(!len)) 1195 return NULL; 1196 1197 dwidth = at_xdmac_align_width(chan, src_addr | dst_addr); 1198 1199 /* Prepare descriptors. */ 1200 while (remaining_size) { 1201 struct at_xdmac_desc *desc = NULL; 1202 1203 dev_dbg(dmaengine_chan_dev(chan), "%s: remaining_size=%zu\n", __func__, remaining_size); 1204 1205 spin_lock_irqsave(&atchan->lock, irqflags); 1206 desc = at_xdmac_get_desc(atchan); 1207 spin_unlock_irqrestore(&atchan->lock, irqflags); 1208 if (!desc) { 1209 dev_err(dmaengine_chan_dev(chan), "can't get descriptor\n"); 1210 if (first) 1211 list_splice_tail_init(&first->descs_list, 1212 &atchan->free_descs_list); 1213 return NULL; 1214 } 1215 1216 /* Update src and dest addresses. */ 1217 src_addr += xfer_size; 1218 dst_addr += xfer_size; 1219 1220 if (remaining_size >= AT_XDMAC_MBR_UBC_UBLEN_MAX << dwidth) 1221 xfer_size = AT_XDMAC_MBR_UBC_UBLEN_MAX << dwidth; 1222 else 1223 xfer_size = remaining_size; 1224 1225 dev_dbg(dmaengine_chan_dev(chan), "%s: xfer_size=%zu\n", __func__, xfer_size); 1226 1227 /* Check remaining length and change data width if needed. */ 1228 dwidth = at_xdmac_align_width(chan, 1229 src_addr | dst_addr | xfer_size); 1230 chan_cc &= ~AT_XDMAC_CC_DWIDTH_MASK; 1231 chan_cc |= AT_XDMAC_CC_DWIDTH(dwidth); 1232 1233 ublen = xfer_size >> dwidth; 1234 remaining_size -= xfer_size; 1235 1236 desc->lld.mbr_sa = src_addr; 1237 desc->lld.mbr_da = dst_addr; 1238 desc->lld.mbr_ubc = AT_XDMAC_MBR_UBC_NDV2 1239 | AT_XDMAC_MBR_UBC_NDEN 1240 | AT_XDMAC_MBR_UBC_NSEN 1241 | ublen; 1242 desc->lld.mbr_cfg = chan_cc; 1243 1244 dev_dbg(dmaengine_chan_dev(chan), 1245 "%s: lld: mbr_sa=%pad, mbr_da=%pad, mbr_ubc=0x%08x, mbr_cfg=0x%08x\n", 1246 __func__, &desc->lld.mbr_sa, &desc->lld.mbr_da, desc->lld.mbr_ubc, desc->lld.mbr_cfg); 1247 1248 /* Chain lld. */ 1249 if (prev) 1250 at_xdmac_queue_desc(chan, prev, desc); 1251 1252 prev = desc; 1253 if (!first) 1254 first = desc; 1255 1256 dev_dbg(dmaengine_chan_dev(chan), "%s: add desc 0x%p to descs_list 0x%p\n", 1257 __func__, desc, first); 1258 list_add_tail(&desc->desc_node, &first->descs_list); 1259 } 1260 1261 first->tx_dma_desc.flags = flags; 1262 first->xfer_size = len; 1263 1264 return &first->tx_dma_desc; 1265 } 1266 1267 static struct at_xdmac_desc *at_xdmac_memset_create_desc(struct dma_chan *chan, 1268 struct at_xdmac_chan *atchan, 1269 dma_addr_t dst_addr, 1270 size_t len, 1271 int value) 1272 { 1273 struct at_xdmac_desc *desc; 1274 unsigned long flags; 1275 size_t ublen; 1276 u32 dwidth; 1277 char pattern; 1278 /* 1279 * WARNING: The channel configuration is set here since there is no 1280 * dmaengine_slave_config call in this case. Moreover we don't know the 1281 * direction, it involves we can't dynamically set the source and dest 1282 * interface so we have to use the same one. Only interface 0 allows EBI 1283 * access. Hopefully we can access DDR through both ports (at least on 1284 * SAMA5D4x), so we can use the same interface for source and dest, 1285 * that solves the fact we don't know the direction. 1286 * ERRATA: Even if useless for memory transfers, the PERID has to not 1287 * match the one of another channel. If not, it could lead to spurious 1288 * flag status. 1289 * For SAMA7G5x case, the SIF and DIF fields are no longer used. 1290 * Thus, no need to have the SIF/DIF interfaces here. 1291 * For SAMA5D4x and SAMA5D2x the SIF and DIF are already configured as 1292 * zero. 1293 */ 1294 u32 chan_cc = AT_XDMAC_CC_PERID(0x7f) 1295 | AT_XDMAC_CC_DAM_UBS_AM 1296 | AT_XDMAC_CC_SAM_INCREMENTED_AM 1297 | AT_XDMAC_CC_MBSIZE_SIXTEEN 1298 | AT_XDMAC_CC_MEMSET_HW_MODE 1299 | AT_XDMAC_CC_TYPE_MEM_TRAN; 1300 1301 dwidth = at_xdmac_align_width(chan, dst_addr); 1302 1303 if (len >= (AT_XDMAC_MBR_UBC_UBLEN_MAX << dwidth)) { 1304 dev_err(dmaengine_chan_dev(chan), 1305 "%s: Transfer too large, aborting...\n", 1306 __func__); 1307 return NULL; 1308 } 1309 1310 spin_lock_irqsave(&atchan->lock, flags); 1311 desc = at_xdmac_get_desc(atchan); 1312 spin_unlock_irqrestore(&atchan->lock, flags); 1313 if (!desc) { 1314 dev_err(dmaengine_chan_dev(chan), "can't get descriptor\n"); 1315 return NULL; 1316 } 1317 1318 chan_cc |= AT_XDMAC_CC_DWIDTH(dwidth); 1319 1320 /* Only the first byte of value is to be used according to dmaengine */ 1321 pattern = (char)value; 1322 1323 ublen = len >> dwidth; 1324 1325 desc->lld.mbr_da = dst_addr; 1326 desc->lld.mbr_ds = (pattern << 24) | 1327 (pattern << 16) | 1328 (pattern << 8) | 1329 pattern; 1330 desc->lld.mbr_ubc = AT_XDMAC_MBR_UBC_NDV3 1331 | AT_XDMAC_MBR_UBC_NDEN 1332 | AT_XDMAC_MBR_UBC_NSEN 1333 | ublen; 1334 desc->lld.mbr_cfg = chan_cc; 1335 1336 dev_dbg(dmaengine_chan_dev(chan), 1337 "%s: lld: mbr_da=%pad, mbr_ds=0x%08x, mbr_ubc=0x%08x, mbr_cfg=0x%08x\n", 1338 __func__, &desc->lld.mbr_da, desc->lld.mbr_ds, desc->lld.mbr_ubc, 1339 desc->lld.mbr_cfg); 1340 1341 return desc; 1342 } 1343 1344 static struct dma_async_tx_descriptor * 1345 at_xdmac_prep_dma_memset(struct dma_chan *chan, dma_addr_t dest, int value, 1346 size_t len, unsigned long flags) 1347 { 1348 struct at_xdmac_chan *atchan = to_at_xdmac_chan(chan); 1349 struct at_xdmac_desc *desc; 1350 1351 dev_dbg(dmaengine_chan_dev(chan), "%s: dest=%pad, len=%zu, pattern=0x%x, flags=0x%lx\n", 1352 __func__, &dest, len, value, flags); 1353 1354 if (unlikely(!len)) 1355 return NULL; 1356 1357 desc = at_xdmac_memset_create_desc(chan, atchan, dest, len, value); 1358 if (!desc) 1359 return NULL; 1360 list_add_tail(&desc->desc_node, &desc->descs_list); 1361 1362 desc->tx_dma_desc.cookie = -EBUSY; 1363 desc->tx_dma_desc.flags = flags; 1364 desc->xfer_size = len; 1365 1366 return &desc->tx_dma_desc; 1367 } 1368 1369 static struct dma_async_tx_descriptor * 1370 at_xdmac_prep_dma_memset_sg(struct dma_chan *chan, struct scatterlist *sgl, 1371 unsigned int sg_len, int value, 1372 unsigned long flags) 1373 { 1374 struct at_xdmac_chan *atchan = to_at_xdmac_chan(chan); 1375 struct at_xdmac_desc *desc, *pdesc = NULL, 1376 *ppdesc = NULL, *first = NULL; 1377 struct scatterlist *sg, *psg = NULL, *ppsg = NULL; 1378 size_t stride = 0, pstride = 0, len = 0; 1379 int i; 1380 1381 if (!sgl) 1382 return NULL; 1383 1384 dev_dbg(dmaengine_chan_dev(chan), "%s: sg_len=%d, value=0x%x, flags=0x%lx\n", 1385 __func__, sg_len, value, flags); 1386 1387 /* Prepare descriptors. */ 1388 for_each_sg(sgl, sg, sg_len, i) { 1389 dev_dbg(dmaengine_chan_dev(chan), "%s: dest=%pad, len=%d, pattern=0x%x, flags=0x%lx\n", 1390 __func__, &sg_dma_address(sg), sg_dma_len(sg), 1391 value, flags); 1392 desc = at_xdmac_memset_create_desc(chan, atchan, 1393 sg_dma_address(sg), 1394 sg_dma_len(sg), 1395 value); 1396 if (!desc && first) 1397 list_splice_tail_init(&first->descs_list, 1398 &atchan->free_descs_list); 1399 1400 if (!first) 1401 first = desc; 1402 1403 /* Update our strides */ 1404 pstride = stride; 1405 if (psg) 1406 stride = sg_dma_address(sg) - 1407 (sg_dma_address(psg) + sg_dma_len(psg)); 1408 1409 /* 1410 * The scatterlist API gives us only the address and 1411 * length of each elements. 1412 * 1413 * Unfortunately, we don't have the stride, which we 1414 * will need to compute. 1415 * 1416 * That make us end up in a situation like this one: 1417 * len stride len stride len 1418 * +-------+ +-------+ +-------+ 1419 * | N-2 | | N-1 | | N | 1420 * +-------+ +-------+ +-------+ 1421 * 1422 * We need all these three elements (N-2, N-1 and N) 1423 * to actually take the decision on whether we need to 1424 * queue N-1 or reuse N-2. 1425 * 1426 * We will only consider N if it is the last element. 1427 */ 1428 if (ppdesc && pdesc) { 1429 if ((stride == pstride) && 1430 (sg_dma_len(ppsg) == sg_dma_len(psg))) { 1431 dev_dbg(dmaengine_chan_dev(chan), 1432 "%s: desc 0x%p can be merged with desc 0x%p\n", 1433 __func__, pdesc, ppdesc); 1434 1435 /* 1436 * Increment the block count of the 1437 * N-2 descriptor 1438 */ 1439 at_xdmac_increment_block_count(chan, ppdesc); 1440 ppdesc->lld.mbr_dus = stride; 1441 1442 /* 1443 * Put back the N-1 descriptor in the 1444 * free descriptor list 1445 */ 1446 list_add_tail(&pdesc->desc_node, 1447 &atchan->free_descs_list); 1448 1449 /* 1450 * Make our N-1 descriptor pointer 1451 * point to the N-2 since they were 1452 * actually merged. 1453 */ 1454 pdesc = ppdesc; 1455 1456 /* 1457 * Rule out the case where we don't have 1458 * pstride computed yet (our second sg 1459 * element) 1460 * 1461 * We also want to catch the case where there 1462 * would be a negative stride, 1463 */ 1464 } else if (pstride || 1465 sg_dma_address(sg) < sg_dma_address(psg)) { 1466 /* 1467 * Queue the N-1 descriptor after the 1468 * N-2 1469 */ 1470 at_xdmac_queue_desc(chan, ppdesc, pdesc); 1471 1472 /* 1473 * Add the N-1 descriptor to the list 1474 * of the descriptors used for this 1475 * transfer 1476 */ 1477 list_add_tail(&desc->desc_node, 1478 &first->descs_list); 1479 dev_dbg(dmaengine_chan_dev(chan), 1480 "%s: add desc 0x%p to descs_list 0x%p\n", 1481 __func__, desc, first); 1482 } 1483 } 1484 1485 /* 1486 * If we are the last element, just see if we have the 1487 * same size than the previous element. 1488 * 1489 * If so, we can merge it with the previous descriptor 1490 * since we don't care about the stride anymore. 1491 */ 1492 if ((i == (sg_len - 1)) && 1493 sg_dma_len(psg) == sg_dma_len(sg)) { 1494 dev_dbg(dmaengine_chan_dev(chan), 1495 "%s: desc 0x%p can be merged with desc 0x%p\n", 1496 __func__, desc, pdesc); 1497 1498 /* 1499 * Increment the block count of the N-1 1500 * descriptor 1501 */ 1502 at_xdmac_increment_block_count(chan, pdesc); 1503 pdesc->lld.mbr_dus = stride; 1504 1505 /* 1506 * Put back the N descriptor in the free 1507 * descriptor list 1508 */ 1509 list_add_tail(&desc->desc_node, 1510 &atchan->free_descs_list); 1511 } 1512 1513 /* Update our descriptors */ 1514 ppdesc = pdesc; 1515 pdesc = desc; 1516 1517 /* Update our scatter pointers */ 1518 ppsg = psg; 1519 psg = sg; 1520 1521 len += sg_dma_len(sg); 1522 } 1523 1524 first->tx_dma_desc.cookie = -EBUSY; 1525 first->tx_dma_desc.flags = flags; 1526 first->xfer_size = len; 1527 1528 return &first->tx_dma_desc; 1529 } 1530 1531 static enum dma_status 1532 at_xdmac_tx_status(struct dma_chan *chan, dma_cookie_t cookie, 1533 struct dma_tx_state *txstate) 1534 { 1535 struct at_xdmac_chan *atchan = to_at_xdmac_chan(chan); 1536 struct at_xdmac *atxdmac = to_at_xdmac(atchan->chan.device); 1537 struct at_xdmac_desc *desc, *_desc, *iter; 1538 struct list_head *descs_list; 1539 enum dma_status ret; 1540 int residue, retry, pm_status; 1541 u32 cur_nda, check_nda, cur_ubc, mask, value; 1542 u8 dwidth = 0; 1543 unsigned long flags; 1544 bool initd; 1545 1546 ret = dma_cookie_status(chan, cookie, txstate); 1547 if (ret == DMA_COMPLETE || !txstate) 1548 return ret; 1549 1550 pm_status = pm_runtime_resume_and_get(atxdmac->dev); 1551 if (pm_status < 0) 1552 return DMA_ERROR; 1553 1554 spin_lock_irqsave(&atchan->lock, flags); 1555 1556 desc = list_first_entry(&atchan->xfers_list, struct at_xdmac_desc, xfer_node); 1557 1558 /* 1559 * If the transfer has not been started yet, don't need to compute the 1560 * residue, it's the transfer length. 1561 */ 1562 if (!desc->active_xfer) { 1563 dma_set_residue(txstate, desc->xfer_size); 1564 goto spin_unlock; 1565 } 1566 1567 residue = desc->xfer_size; 1568 /* 1569 * Flush FIFO: only relevant when the transfer is source peripheral 1570 * synchronized. Flush is needed before reading CUBC because data in 1571 * the FIFO are not reported by CUBC. Reporting a residue of the 1572 * transfer length while we have data in FIFO can cause issue. 1573 * Usecase: atmel USART has a timeout which means I have received 1574 * characters but there is no more character received for a while. On 1575 * timeout, it requests the residue. If the data are in the DMA FIFO, 1576 * we will return a residue of the transfer length. It means no data 1577 * received. If an application is waiting for these data, it will hang 1578 * since we won't have another USART timeout without receiving new 1579 * data. 1580 */ 1581 mask = AT_XDMAC_CC_TYPE | AT_XDMAC_CC_DSYNC; 1582 value = AT_XDMAC_CC_TYPE_PER_TRAN | AT_XDMAC_CC_DSYNC_PER2MEM; 1583 if ((desc->lld.mbr_cfg & mask) == value) { 1584 at_xdmac_write(atxdmac, atxdmac->layout->gswf, atchan->mask); 1585 while (!(at_xdmac_chan_read(atchan, AT_XDMAC_CIS) & AT_XDMAC_CIS_FIS)) 1586 cpu_relax(); 1587 } 1588 1589 /* 1590 * The easiest way to compute the residue should be to pause the DMA 1591 * but doing this can lead to miss some data as some devices don't 1592 * have FIFO. 1593 * We need to read several registers because: 1594 * - DMA is running therefore a descriptor change is possible while 1595 * reading these registers 1596 * - When the block transfer is done, the value of the CUBC register 1597 * is set to its initial value until the fetch of the next descriptor. 1598 * This value will corrupt the residue calculation so we have to skip 1599 * it. 1600 * 1601 * INITD -------- ------------ 1602 * |____________________| 1603 * _______________________ _______________ 1604 * NDA @desc2 \/ @desc3 1605 * _______________________/\_______________ 1606 * __________ ___________ _______________ 1607 * CUBC 0 \/ MAX desc1 \/ MAX desc2 1608 * __________/\___________/\_______________ 1609 * 1610 * Since descriptors are aligned on 64 bits, we can assume that 1611 * the update of NDA and CUBC is atomic. 1612 * Memory barriers are used to ensure the read order of the registers. 1613 * A max number of retries is set because unlikely it could never ends. 1614 */ 1615 for (retry = 0; retry < AT_XDMAC_RESIDUE_MAX_RETRIES; retry++) { 1616 check_nda = at_xdmac_chan_read(atchan, AT_XDMAC_CNDA) & 0xfffffffc; 1617 rmb(); 1618 cur_ubc = at_xdmac_chan_read(atchan, AT_XDMAC_CUBC); 1619 rmb(); 1620 initd = !!(at_xdmac_chan_read(atchan, AT_XDMAC_CC) & AT_XDMAC_CC_INITD); 1621 rmb(); 1622 cur_nda = at_xdmac_chan_read(atchan, AT_XDMAC_CNDA) & 0xfffffffc; 1623 rmb(); 1624 1625 if ((check_nda == cur_nda) && initd) 1626 break; 1627 } 1628 1629 if (unlikely(retry >= AT_XDMAC_RESIDUE_MAX_RETRIES)) { 1630 ret = DMA_ERROR; 1631 goto spin_unlock; 1632 } 1633 1634 /* 1635 * Flush FIFO: only relevant when the transfer is source peripheral 1636 * synchronized. Another flush is needed here because CUBC is updated 1637 * when the controller sends the data write command. It can lead to 1638 * report data that are not written in the memory or the device. The 1639 * FIFO flush ensures that data are really written. 1640 */ 1641 if ((desc->lld.mbr_cfg & mask) == value) { 1642 at_xdmac_write(atxdmac, atxdmac->layout->gswf, atchan->mask); 1643 while (!(at_xdmac_chan_read(atchan, AT_XDMAC_CIS) & AT_XDMAC_CIS_FIS)) 1644 cpu_relax(); 1645 } 1646 1647 /* 1648 * Remove size of all microblocks already transferred and the current 1649 * one. Then add the remaining size to transfer of the current 1650 * microblock. 1651 */ 1652 descs_list = &desc->descs_list; 1653 list_for_each_entry_safe(iter, _desc, descs_list, desc_node) { 1654 dwidth = at_xdmac_get_dwidth(iter->lld.mbr_cfg); 1655 residue -= (iter->lld.mbr_ubc & 0xffffff) << dwidth; 1656 if ((iter->lld.mbr_nda & 0xfffffffc) == cur_nda) { 1657 desc = iter; 1658 break; 1659 } 1660 } 1661 residue += cur_ubc << dwidth; 1662 1663 dma_set_residue(txstate, residue); 1664 1665 dev_dbg(dmaengine_chan_dev(chan), 1666 "%s: desc=0x%p, tx_dma_desc.phys=%pad, tx_status=%d, cookie=%d, residue=%d\n", 1667 __func__, desc, &desc->tx_dma_desc.phys, ret, cookie, residue); 1668 1669 spin_unlock: 1670 spin_unlock_irqrestore(&atchan->lock, flags); 1671 pm_runtime_put_autosuspend(atxdmac->dev); 1672 return ret; 1673 } 1674 1675 static void at_xdmac_advance_work(struct at_xdmac_chan *atchan) 1676 { 1677 struct at_xdmac_desc *desc; 1678 1679 /* 1680 * If channel is enabled, do nothing, advance_work will be triggered 1681 * after the interruption. 1682 */ 1683 if (at_xdmac_chan_is_enabled(atchan) || list_empty(&atchan->xfers_list)) 1684 return; 1685 1686 desc = list_first_entry(&atchan->xfers_list, struct at_xdmac_desc, 1687 xfer_node); 1688 dev_vdbg(dmaengine_chan_dev(&atchan->chan), "%s: desc 0x%p\n", __func__, desc); 1689 if (!desc->active_xfer) 1690 at_xdmac_start_xfer(atchan, desc); 1691 } 1692 1693 static void at_xdmac_handle_cyclic(struct at_xdmac_chan *atchan) 1694 { 1695 struct at_xdmac_desc *desc; 1696 struct dma_async_tx_descriptor *txd; 1697 1698 spin_lock_irq(&atchan->lock); 1699 dev_dbg(dmaengine_chan_dev(&atchan->chan), "%s: status=0x%08x\n", 1700 __func__, atchan->irq_status); 1701 if (list_empty(&atchan->xfers_list)) { 1702 spin_unlock_irq(&atchan->lock); 1703 return; 1704 } 1705 desc = list_first_entry(&atchan->xfers_list, struct at_xdmac_desc, 1706 xfer_node); 1707 spin_unlock_irq(&atchan->lock); 1708 txd = &desc->tx_dma_desc; 1709 if (txd->flags & DMA_PREP_INTERRUPT) 1710 dmaengine_desc_get_callback_invoke(txd, NULL); 1711 } 1712 1713 /* Called with atchan->lock held. */ 1714 static void at_xdmac_handle_error(struct at_xdmac_chan *atchan) 1715 { 1716 struct at_xdmac *atxdmac = to_at_xdmac(atchan->chan.device); 1717 struct at_xdmac_desc *bad_desc; 1718 int ret; 1719 1720 ret = pm_runtime_resume_and_get(atxdmac->dev); 1721 if (ret < 0) 1722 return; 1723 1724 /* 1725 * The descriptor currently at the head of the active list is 1726 * broken. Since we don't have any way to report errors, we'll 1727 * just have to scream loudly and try to continue with other 1728 * descriptors queued (if any). 1729 */ 1730 if (atchan->irq_status & AT_XDMAC_CIS_RBEIS) 1731 dev_err(dmaengine_chan_dev(&atchan->chan), "read bus error!!!"); 1732 if (atchan->irq_status & AT_XDMAC_CIS_WBEIS) 1733 dev_err(dmaengine_chan_dev(&atchan->chan), "write bus error!!!"); 1734 if (atchan->irq_status & AT_XDMAC_CIS_ROIS) 1735 dev_err(dmaengine_chan_dev(&atchan->chan), "request overflow error!!!"); 1736 1737 /* Channel must be disabled first as it's not done automatically */ 1738 at_xdmac_write(atxdmac, AT_XDMAC_GD, atchan->mask); 1739 while (at_xdmac_read(atxdmac, AT_XDMAC_GS) & atchan->mask) 1740 cpu_relax(); 1741 1742 bad_desc = list_first_entry(&atchan->xfers_list, 1743 struct at_xdmac_desc, 1744 xfer_node); 1745 1746 /* Print bad descriptor's details if needed */ 1747 dev_dbg(dmaengine_chan_dev(&atchan->chan), 1748 "%s: lld: mbr_sa=%pad, mbr_da=%pad, mbr_ubc=0x%08x\n", 1749 __func__, &bad_desc->lld.mbr_sa, &bad_desc->lld.mbr_da, 1750 bad_desc->lld.mbr_ubc); 1751 1752 pm_runtime_put_autosuspend(atxdmac->dev); 1753 1754 /* Then continue with usual descriptor management */ 1755 } 1756 1757 static void at_xdmac_tasklet(struct tasklet_struct *t) 1758 { 1759 struct at_xdmac_chan *atchan = from_tasklet(atchan, t, tasklet); 1760 struct at_xdmac *atxdmac = to_at_xdmac(atchan->chan.device); 1761 struct at_xdmac_desc *desc; 1762 struct dma_async_tx_descriptor *txd; 1763 u32 error_mask; 1764 1765 if (at_xdmac_chan_is_cyclic(atchan)) 1766 return at_xdmac_handle_cyclic(atchan); 1767 1768 error_mask = AT_XDMAC_CIS_RBEIS | AT_XDMAC_CIS_WBEIS | 1769 AT_XDMAC_CIS_ROIS; 1770 1771 spin_lock_irq(&atchan->lock); 1772 1773 dev_dbg(dmaengine_chan_dev(&atchan->chan), "%s: status=0x%08x\n", 1774 __func__, atchan->irq_status); 1775 1776 if (!(atchan->irq_status & AT_XDMAC_CIS_LIS) && 1777 !(atchan->irq_status & error_mask)) { 1778 spin_unlock_irq(&atchan->lock); 1779 return; 1780 } 1781 1782 if (atchan->irq_status & error_mask) 1783 at_xdmac_handle_error(atchan); 1784 1785 desc = list_first_entry(&atchan->xfers_list, struct at_xdmac_desc, 1786 xfer_node); 1787 dev_vdbg(dmaengine_chan_dev(&atchan->chan), "%s: desc 0x%p\n", __func__, desc); 1788 if (!desc->active_xfer) { 1789 dev_err(dmaengine_chan_dev(&atchan->chan), "Xfer not active: exiting"); 1790 spin_unlock_irq(&atchan->lock); 1791 return; 1792 } 1793 1794 txd = &desc->tx_dma_desc; 1795 dma_cookie_complete(txd); 1796 /* Remove the transfer from the transfer list. */ 1797 list_del(&desc->xfer_node); 1798 spin_unlock_irq(&atchan->lock); 1799 1800 if (txd->flags & DMA_PREP_INTERRUPT) 1801 dmaengine_desc_get_callback_invoke(txd, NULL); 1802 1803 dma_run_dependencies(txd); 1804 1805 spin_lock_irq(&atchan->lock); 1806 /* Move the xfer descriptors into the free descriptors list. */ 1807 list_splice_tail_init(&desc->descs_list, &atchan->free_descs_list); 1808 at_xdmac_advance_work(atchan); 1809 spin_unlock_irq(&atchan->lock); 1810 1811 /* 1812 * Decrement runtime PM ref counter incremented in 1813 * at_xdmac_start_xfer(). 1814 */ 1815 pm_runtime_put_autosuspend(atxdmac->dev); 1816 } 1817 1818 static irqreturn_t at_xdmac_interrupt(int irq, void *dev_id) 1819 { 1820 struct at_xdmac *atxdmac = (struct at_xdmac *)dev_id; 1821 struct at_xdmac_chan *atchan; 1822 u32 imr, status, pending; 1823 u32 chan_imr, chan_status; 1824 int i, ret = IRQ_NONE; 1825 1826 do { 1827 imr = at_xdmac_read(atxdmac, AT_XDMAC_GIM); 1828 status = at_xdmac_read(atxdmac, AT_XDMAC_GIS); 1829 pending = status & imr; 1830 1831 dev_vdbg(atxdmac->dma.dev, 1832 "%s: status=0x%08x, imr=0x%08x, pending=0x%08x\n", 1833 __func__, status, imr, pending); 1834 1835 if (!pending) 1836 break; 1837 1838 /* We have to find which channel has generated the interrupt. */ 1839 for (i = 0; i < atxdmac->dma.chancnt; i++) { 1840 if (!((1 << i) & pending)) 1841 continue; 1842 1843 atchan = &atxdmac->chan[i]; 1844 chan_imr = at_xdmac_chan_read(atchan, AT_XDMAC_CIM); 1845 chan_status = at_xdmac_chan_read(atchan, AT_XDMAC_CIS); 1846 atchan->irq_status = chan_status & chan_imr; 1847 dev_vdbg(atxdmac->dma.dev, 1848 "%s: chan%d: imr=0x%x, status=0x%x\n", 1849 __func__, i, chan_imr, chan_status); 1850 dev_vdbg(dmaengine_chan_dev(&atchan->chan), 1851 "%s: CC=0x%08x CNDA=0x%08x, CNDC=0x%08x, CSA=0x%08x, CDA=0x%08x, CUBC=0x%08x\n", 1852 __func__, 1853 at_xdmac_chan_read(atchan, AT_XDMAC_CC), 1854 at_xdmac_chan_read(atchan, AT_XDMAC_CNDA), 1855 at_xdmac_chan_read(atchan, AT_XDMAC_CNDC), 1856 at_xdmac_chan_read(atchan, AT_XDMAC_CSA), 1857 at_xdmac_chan_read(atchan, AT_XDMAC_CDA), 1858 at_xdmac_chan_read(atchan, AT_XDMAC_CUBC)); 1859 1860 if (atchan->irq_status & (AT_XDMAC_CIS_RBEIS | AT_XDMAC_CIS_WBEIS)) 1861 at_xdmac_write(atxdmac, AT_XDMAC_GD, atchan->mask); 1862 1863 tasklet_schedule(&atchan->tasklet); 1864 ret = IRQ_HANDLED; 1865 } 1866 1867 } while (pending); 1868 1869 return ret; 1870 } 1871 1872 static void at_xdmac_issue_pending(struct dma_chan *chan) 1873 { 1874 struct at_xdmac_chan *atchan = to_at_xdmac_chan(chan); 1875 unsigned long flags; 1876 1877 dev_dbg(dmaengine_chan_dev(&atchan->chan), "%s\n", __func__); 1878 1879 spin_lock_irqsave(&atchan->lock, flags); 1880 at_xdmac_advance_work(atchan); 1881 spin_unlock_irqrestore(&atchan->lock, flags); 1882 1883 return; 1884 } 1885 1886 static int at_xdmac_device_config(struct dma_chan *chan, 1887 struct dma_slave_config *config) 1888 { 1889 struct at_xdmac_chan *atchan = to_at_xdmac_chan(chan); 1890 int ret; 1891 unsigned long flags; 1892 1893 dev_dbg(dmaengine_chan_dev(chan), "%s\n", __func__); 1894 1895 spin_lock_irqsave(&atchan->lock, flags); 1896 ret = at_xdmac_set_slave_config(chan, config); 1897 spin_unlock_irqrestore(&atchan->lock, flags); 1898 1899 return ret; 1900 } 1901 1902 static void at_xdmac_device_pause_set(struct at_xdmac *atxdmac, 1903 struct at_xdmac_chan *atchan) 1904 { 1905 at_xdmac_write(atxdmac, atxdmac->layout->grws, atchan->mask); 1906 while (at_xdmac_chan_read(atchan, AT_XDMAC_CC) & 1907 (AT_XDMAC_CC_WRIP | AT_XDMAC_CC_RDIP)) 1908 cpu_relax(); 1909 } 1910 1911 static void at_xdmac_device_pause_internal(struct at_xdmac_chan *atchan) 1912 { 1913 struct at_xdmac *atxdmac = to_at_xdmac(atchan->chan.device); 1914 unsigned long flags; 1915 1916 spin_lock_irqsave(&atchan->lock, flags); 1917 set_bit(AT_XDMAC_CHAN_IS_PAUSED_INTERNAL, &atchan->status); 1918 at_xdmac_device_pause_set(atxdmac, atchan); 1919 spin_unlock_irqrestore(&atchan->lock, flags); 1920 } 1921 1922 static int at_xdmac_device_pause(struct dma_chan *chan) 1923 { 1924 struct at_xdmac_chan *atchan = to_at_xdmac_chan(chan); 1925 struct at_xdmac *atxdmac = to_at_xdmac(atchan->chan.device); 1926 unsigned long flags; 1927 int ret; 1928 1929 dev_dbg(dmaengine_chan_dev(chan), "%s\n", __func__); 1930 1931 if (test_and_set_bit(AT_XDMAC_CHAN_IS_PAUSED, &atchan->status)) 1932 return 0; 1933 1934 ret = pm_runtime_resume_and_get(atxdmac->dev); 1935 if (ret < 0) 1936 return ret; 1937 1938 spin_lock_irqsave(&atchan->lock, flags); 1939 1940 at_xdmac_device_pause_set(atxdmac, atchan); 1941 /* Decrement runtime PM ref counter for each active descriptor. */ 1942 at_xdmac_runtime_suspend_descriptors(atchan); 1943 1944 spin_unlock_irqrestore(&atchan->lock, flags); 1945 1946 pm_runtime_put_autosuspend(atxdmac->dev); 1947 1948 return 0; 1949 } 1950 1951 static void at_xdmac_device_resume_internal(struct at_xdmac_chan *atchan) 1952 { 1953 struct at_xdmac *atxdmac = to_at_xdmac(atchan->chan.device); 1954 unsigned long flags; 1955 1956 spin_lock_irqsave(&atchan->lock, flags); 1957 at_xdmac_write(atxdmac, atxdmac->layout->grwr, atchan->mask); 1958 clear_bit(AT_XDMAC_CHAN_IS_PAUSED_INTERNAL, &atchan->status); 1959 spin_unlock_irqrestore(&atchan->lock, flags); 1960 } 1961 1962 static int at_xdmac_device_resume(struct dma_chan *chan) 1963 { 1964 struct at_xdmac_chan *atchan = to_at_xdmac_chan(chan); 1965 struct at_xdmac *atxdmac = to_at_xdmac(atchan->chan.device); 1966 unsigned long flags; 1967 int ret; 1968 1969 dev_dbg(dmaengine_chan_dev(chan), "%s\n", __func__); 1970 1971 ret = pm_runtime_resume_and_get(atxdmac->dev); 1972 if (ret < 0) 1973 return ret; 1974 1975 spin_lock_irqsave(&atchan->lock, flags); 1976 if (!at_xdmac_chan_is_paused(atchan)) 1977 goto unlock; 1978 1979 /* Increment runtime PM ref counter for each active descriptor. */ 1980 ret = at_xdmac_runtime_resume_descriptors(atchan); 1981 if (ret < 0) 1982 goto unlock; 1983 1984 at_xdmac_write(atxdmac, atxdmac->layout->grwr, atchan->mask); 1985 clear_bit(AT_XDMAC_CHAN_IS_PAUSED, &atchan->status); 1986 1987 unlock: 1988 spin_unlock_irqrestore(&atchan->lock, flags); 1989 pm_runtime_put_autosuspend(atxdmac->dev); 1990 1991 return ret; 1992 } 1993 1994 static int at_xdmac_device_terminate_all(struct dma_chan *chan) 1995 { 1996 struct at_xdmac_desc *desc, *_desc; 1997 struct at_xdmac_chan *atchan = to_at_xdmac_chan(chan); 1998 struct at_xdmac *atxdmac = to_at_xdmac(atchan->chan.device); 1999 unsigned long flags; 2000 int ret; 2001 2002 dev_dbg(dmaengine_chan_dev(chan), "%s\n", __func__); 2003 2004 ret = pm_runtime_resume_and_get(atxdmac->dev); 2005 if (ret < 0) 2006 return ret; 2007 2008 spin_lock_irqsave(&atchan->lock, flags); 2009 at_xdmac_write(atxdmac, AT_XDMAC_GD, atchan->mask); 2010 while (at_xdmac_read(atxdmac, AT_XDMAC_GS) & atchan->mask) 2011 cpu_relax(); 2012 2013 /* Cancel all pending transfers. */ 2014 list_for_each_entry_safe(desc, _desc, &atchan->xfers_list, xfer_node) { 2015 list_del(&desc->xfer_node); 2016 list_splice_tail_init(&desc->descs_list, 2017 &atchan->free_descs_list); 2018 /* 2019 * We incremented the runtime PM reference count on 2020 * at_xdmac_start_xfer() for this descriptor. Now it's time 2021 * to release it. 2022 */ 2023 if (desc->active_xfer) 2024 pm_runtime_put_noidle(atxdmac->dev); 2025 } 2026 2027 clear_bit(AT_XDMAC_CHAN_IS_PAUSED, &atchan->status); 2028 clear_bit(AT_XDMAC_CHAN_IS_CYCLIC, &atchan->status); 2029 spin_unlock_irqrestore(&atchan->lock, flags); 2030 2031 pm_runtime_put_autosuspend(atxdmac->dev); 2032 2033 return 0; 2034 } 2035 2036 static int at_xdmac_alloc_chan_resources(struct dma_chan *chan) 2037 { 2038 struct at_xdmac_chan *atchan = to_at_xdmac_chan(chan); 2039 struct at_xdmac_desc *desc; 2040 int i; 2041 2042 if (at_xdmac_chan_is_enabled(atchan)) { 2043 dev_err(dmaengine_chan_dev(chan), 2044 "can't allocate channel resources (channel enabled)\n"); 2045 return -EIO; 2046 } 2047 2048 if (!list_empty(&atchan->free_descs_list)) { 2049 dev_err(dmaengine_chan_dev(chan), 2050 "can't allocate channel resources (channel not free from a previous use)\n"); 2051 return -EIO; 2052 } 2053 2054 for (i = 0; i < init_nr_desc_per_channel; i++) { 2055 desc = at_xdmac_alloc_desc(chan, GFP_KERNEL); 2056 if (!desc) { 2057 if (i == 0) { 2058 dev_warn(dmaengine_chan_dev(chan), 2059 "can't allocate any descriptors\n"); 2060 return -EIO; 2061 } 2062 dev_warn(dmaengine_chan_dev(chan), 2063 "only %d descriptors have been allocated\n", i); 2064 break; 2065 } 2066 list_add_tail(&desc->desc_node, &atchan->free_descs_list); 2067 } 2068 2069 dma_cookie_init(chan); 2070 2071 dev_dbg(dmaengine_chan_dev(chan), "%s: allocated %d descriptors\n", __func__, i); 2072 2073 return i; 2074 } 2075 2076 static void at_xdmac_free_chan_resources(struct dma_chan *chan) 2077 { 2078 struct at_xdmac_chan *atchan = to_at_xdmac_chan(chan); 2079 struct at_xdmac *atxdmac = to_at_xdmac(chan->device); 2080 struct at_xdmac_desc *desc, *_desc; 2081 2082 list_for_each_entry_safe(desc, _desc, &atchan->free_descs_list, desc_node) { 2083 dev_dbg(dmaengine_chan_dev(chan), "%s: freeing descriptor %p\n", __func__, desc); 2084 list_del(&desc->desc_node); 2085 dma_pool_free(atxdmac->at_xdmac_desc_pool, desc, desc->tx_dma_desc.phys); 2086 } 2087 2088 return; 2089 } 2090 2091 static void at_xdmac_axi_config(struct platform_device *pdev) 2092 { 2093 struct at_xdmac *atxdmac = (struct at_xdmac *)platform_get_drvdata(pdev); 2094 bool dev_m2m = false; 2095 u32 dma_requests; 2096 2097 if (!atxdmac->layout->axi_config) 2098 return; /* Not supported */ 2099 2100 if (!of_property_read_u32(pdev->dev.of_node, "dma-requests", 2101 &dma_requests)) { 2102 dev_info(&pdev->dev, "controller in mem2mem mode.\n"); 2103 dev_m2m = true; 2104 } 2105 2106 if (dev_m2m) { 2107 at_xdmac_write(atxdmac, AT_XDMAC_GCFG, AT_XDMAC_GCFG_M2M); 2108 at_xdmac_write(atxdmac, AT_XDMAC_GWAC, AT_XDMAC_GWAC_M2M); 2109 } else { 2110 at_xdmac_write(atxdmac, AT_XDMAC_GCFG, AT_XDMAC_GCFG_P2M); 2111 at_xdmac_write(atxdmac, AT_XDMAC_GWAC, AT_XDMAC_GWAC_P2M); 2112 } 2113 } 2114 2115 static int __maybe_unused atmel_xdmac_prepare(struct device *dev) 2116 { 2117 struct at_xdmac *atxdmac = dev_get_drvdata(dev); 2118 struct dma_chan *chan, *_chan; 2119 2120 list_for_each_entry_safe(chan, _chan, &atxdmac->dma.channels, device_node) { 2121 struct at_xdmac_chan *atchan = to_at_xdmac_chan(chan); 2122 2123 /* Wait for transfer completion, except in cyclic case. */ 2124 if (at_xdmac_chan_is_enabled(atchan) && !at_xdmac_chan_is_cyclic(atchan)) 2125 return -EAGAIN; 2126 } 2127 return 0; 2128 } 2129 2130 static int __maybe_unused atmel_xdmac_suspend(struct device *dev) 2131 { 2132 struct at_xdmac *atxdmac = dev_get_drvdata(dev); 2133 struct dma_chan *chan, *_chan; 2134 int ret; 2135 2136 ret = pm_runtime_resume_and_get(atxdmac->dev); 2137 if (ret < 0) 2138 return ret; 2139 2140 list_for_each_entry_safe(chan, _chan, &atxdmac->dma.channels, device_node) { 2141 struct at_xdmac_chan *atchan = to_at_xdmac_chan(chan); 2142 2143 atchan->save_cc = at_xdmac_chan_read(atchan, AT_XDMAC_CC); 2144 if (at_xdmac_chan_is_cyclic(atchan)) { 2145 if (!at_xdmac_chan_is_paused(atchan)) { 2146 dev_warn(dmaengine_chan_dev(chan), "%s: channel %d not paused\n", 2147 __func__, chan->chan_id); 2148 at_xdmac_device_pause_internal(atchan); 2149 at_xdmac_runtime_suspend_descriptors(atchan); 2150 } 2151 atchan->save_cim = at_xdmac_chan_read(atchan, AT_XDMAC_CIM); 2152 atchan->save_cnda = at_xdmac_chan_read(atchan, AT_XDMAC_CNDA); 2153 atchan->save_cndc = at_xdmac_chan_read(atchan, AT_XDMAC_CNDC); 2154 } 2155 } 2156 atxdmac->save_gim = at_xdmac_read(atxdmac, AT_XDMAC_GIM); 2157 atxdmac->save_gs = at_xdmac_read(atxdmac, AT_XDMAC_GS); 2158 2159 at_xdmac_off(atxdmac, false); 2160 pm_runtime_mark_last_busy(atxdmac->dev); 2161 pm_runtime_put_noidle(atxdmac->dev); 2162 clk_disable_unprepare(atxdmac->clk); 2163 2164 return 0; 2165 } 2166 2167 static int __maybe_unused atmel_xdmac_resume(struct device *dev) 2168 { 2169 struct at_xdmac *atxdmac = dev_get_drvdata(dev); 2170 struct at_xdmac_chan *atchan; 2171 struct dma_chan *chan, *_chan; 2172 struct platform_device *pdev = container_of(dev, struct platform_device, dev); 2173 int i, ret; 2174 2175 ret = clk_prepare_enable(atxdmac->clk); 2176 if (ret) 2177 return ret; 2178 2179 pm_runtime_get_noresume(atxdmac->dev); 2180 2181 at_xdmac_axi_config(pdev); 2182 2183 /* Clear pending interrupts. */ 2184 for (i = 0; i < atxdmac->dma.chancnt; i++) { 2185 atchan = &atxdmac->chan[i]; 2186 while (at_xdmac_chan_read(atchan, AT_XDMAC_CIS)) 2187 cpu_relax(); 2188 } 2189 2190 at_xdmac_write(atxdmac, AT_XDMAC_GIE, atxdmac->save_gim); 2191 list_for_each_entry_safe(chan, _chan, &atxdmac->dma.channels, device_node) { 2192 atchan = to_at_xdmac_chan(chan); 2193 2194 at_xdmac_chan_write(atchan, AT_XDMAC_CC, atchan->save_cc); 2195 if (at_xdmac_chan_is_cyclic(atchan)) { 2196 /* 2197 * Resume only channels not explicitly paused by 2198 * consumers. 2199 */ 2200 if (at_xdmac_chan_is_paused_internal(atchan)) { 2201 ret = at_xdmac_runtime_resume_descriptors(atchan); 2202 if (ret < 0) 2203 return ret; 2204 at_xdmac_device_resume_internal(atchan); 2205 } 2206 2207 /* 2208 * We may resume from a deep sleep state where power 2209 * to DMA controller is cut-off. Thus, restore the 2210 * suspend state of channels set though dmaengine API. 2211 */ 2212 else if (at_xdmac_chan_is_paused(atchan)) 2213 at_xdmac_device_pause_set(atxdmac, atchan); 2214 2215 at_xdmac_chan_write(atchan, AT_XDMAC_CNDA, atchan->save_cnda); 2216 at_xdmac_chan_write(atchan, AT_XDMAC_CNDC, atchan->save_cndc); 2217 at_xdmac_chan_write(atchan, AT_XDMAC_CIE, atchan->save_cim); 2218 wmb(); 2219 if (atxdmac->save_gs & atchan->mask) 2220 at_xdmac_write(atxdmac, AT_XDMAC_GE, atchan->mask); 2221 } 2222 } 2223 2224 pm_runtime_put_autosuspend(atxdmac->dev); 2225 2226 return 0; 2227 } 2228 2229 static int __maybe_unused atmel_xdmac_runtime_suspend(struct device *dev) 2230 { 2231 struct at_xdmac *atxdmac = dev_get_drvdata(dev); 2232 2233 clk_disable(atxdmac->clk); 2234 2235 return 0; 2236 } 2237 2238 static int __maybe_unused atmel_xdmac_runtime_resume(struct device *dev) 2239 { 2240 struct at_xdmac *atxdmac = dev_get_drvdata(dev); 2241 2242 return clk_enable(atxdmac->clk); 2243 } 2244 2245 static inline int at_xdmac_get_channel_number(struct platform_device *pdev, 2246 u32 reg, u32 *pchannels) 2247 { 2248 int ret; 2249 2250 if (reg) { 2251 *pchannels = AT_XDMAC_NB_CH(reg); 2252 return 0; 2253 } 2254 2255 ret = of_property_read_u32(pdev->dev.of_node, "dma-channels", pchannels); 2256 if (ret) 2257 dev_err(&pdev->dev, "can't get number of channels\n"); 2258 2259 return ret; 2260 } 2261 2262 static int at_xdmac_probe(struct platform_device *pdev) 2263 { 2264 struct at_xdmac *atxdmac; 2265 int irq, ret; 2266 void __iomem *base; 2267 u32 nr_channels, i, reg; 2268 2269 irq = platform_get_irq(pdev, 0); 2270 if (irq < 0) 2271 return irq; 2272 2273 base = devm_platform_ioremap_resource(pdev, 0); 2274 if (IS_ERR(base)) 2275 return PTR_ERR(base); 2276 2277 /* 2278 * Read number of xdmac channels, read helper function can't be used 2279 * since atxdmac is not yet allocated and we need to know the number 2280 * of channels to do the allocation. 2281 */ 2282 reg = readl_relaxed(base + AT_XDMAC_GTYPE); 2283 ret = at_xdmac_get_channel_number(pdev, reg, &nr_channels); 2284 if (ret) 2285 return ret; 2286 2287 if (nr_channels > AT_XDMAC_MAX_CHAN) { 2288 dev_err(&pdev->dev, "invalid number of channels (%u)\n", 2289 nr_channels); 2290 return -EINVAL; 2291 } 2292 2293 atxdmac = devm_kzalloc(&pdev->dev, 2294 struct_size(atxdmac, chan, nr_channels), 2295 GFP_KERNEL); 2296 if (!atxdmac) { 2297 dev_err(&pdev->dev, "can't allocate at_xdmac structure\n"); 2298 return -ENOMEM; 2299 } 2300 2301 atxdmac->regs = base; 2302 atxdmac->irq = irq; 2303 atxdmac->dev = &pdev->dev; 2304 2305 atxdmac->layout = of_device_get_match_data(&pdev->dev); 2306 if (!atxdmac->layout) 2307 return -ENODEV; 2308 2309 atxdmac->clk = devm_clk_get(&pdev->dev, "dma_clk"); 2310 if (IS_ERR(atxdmac->clk)) { 2311 dev_err(&pdev->dev, "can't get dma_clk\n"); 2312 return PTR_ERR(atxdmac->clk); 2313 } 2314 2315 /* Do not use dev res to prevent races with tasklet */ 2316 ret = request_irq(atxdmac->irq, at_xdmac_interrupt, 0, "at_xdmac", atxdmac); 2317 if (ret) { 2318 dev_err(&pdev->dev, "can't request irq\n"); 2319 return ret; 2320 } 2321 2322 ret = clk_prepare_enable(atxdmac->clk); 2323 if (ret) { 2324 dev_err(&pdev->dev, "can't prepare or enable clock\n"); 2325 goto err_free_irq; 2326 } 2327 2328 atxdmac->at_xdmac_desc_pool = 2329 dmam_pool_create(dev_name(&pdev->dev), &pdev->dev, 2330 sizeof(struct at_xdmac_desc), 4, 0); 2331 if (!atxdmac->at_xdmac_desc_pool) { 2332 dev_err(&pdev->dev, "no memory for descriptors dma pool\n"); 2333 ret = -ENOMEM; 2334 goto err_clk_disable; 2335 } 2336 2337 dma_cap_set(DMA_CYCLIC, atxdmac->dma.cap_mask); 2338 dma_cap_set(DMA_INTERLEAVE, atxdmac->dma.cap_mask); 2339 dma_cap_set(DMA_MEMCPY, atxdmac->dma.cap_mask); 2340 dma_cap_set(DMA_MEMSET, atxdmac->dma.cap_mask); 2341 dma_cap_set(DMA_MEMSET_SG, atxdmac->dma.cap_mask); 2342 dma_cap_set(DMA_SLAVE, atxdmac->dma.cap_mask); 2343 /* 2344 * Without DMA_PRIVATE the driver is not able to allocate more than 2345 * one channel, second allocation fails in private_candidate. 2346 */ 2347 dma_cap_set(DMA_PRIVATE, atxdmac->dma.cap_mask); 2348 atxdmac->dma.dev = &pdev->dev; 2349 atxdmac->dma.device_alloc_chan_resources = at_xdmac_alloc_chan_resources; 2350 atxdmac->dma.device_free_chan_resources = at_xdmac_free_chan_resources; 2351 atxdmac->dma.device_tx_status = at_xdmac_tx_status; 2352 atxdmac->dma.device_issue_pending = at_xdmac_issue_pending; 2353 atxdmac->dma.device_prep_dma_cyclic = at_xdmac_prep_dma_cyclic; 2354 atxdmac->dma.device_prep_interleaved_dma = at_xdmac_prep_interleaved; 2355 atxdmac->dma.device_prep_dma_memcpy = at_xdmac_prep_dma_memcpy; 2356 atxdmac->dma.device_prep_dma_memset = at_xdmac_prep_dma_memset; 2357 atxdmac->dma.device_prep_dma_memset_sg = at_xdmac_prep_dma_memset_sg; 2358 atxdmac->dma.device_prep_slave_sg = at_xdmac_prep_slave_sg; 2359 atxdmac->dma.device_config = at_xdmac_device_config; 2360 atxdmac->dma.device_pause = at_xdmac_device_pause; 2361 atxdmac->dma.device_resume = at_xdmac_device_resume; 2362 atxdmac->dma.device_terminate_all = at_xdmac_device_terminate_all; 2363 atxdmac->dma.src_addr_widths = AT_XDMAC_DMA_BUSWIDTHS; 2364 atxdmac->dma.dst_addr_widths = AT_XDMAC_DMA_BUSWIDTHS; 2365 atxdmac->dma.directions = BIT(DMA_DEV_TO_MEM) | BIT(DMA_MEM_TO_DEV); 2366 atxdmac->dma.residue_granularity = DMA_RESIDUE_GRANULARITY_BURST; 2367 2368 platform_set_drvdata(pdev, atxdmac); 2369 2370 pm_runtime_set_autosuspend_delay(&pdev->dev, 500); 2371 pm_runtime_use_autosuspend(&pdev->dev); 2372 pm_runtime_set_active(&pdev->dev); 2373 pm_runtime_enable(&pdev->dev); 2374 pm_runtime_get_noresume(&pdev->dev); 2375 2376 /* Init channels. */ 2377 INIT_LIST_HEAD(&atxdmac->dma.channels); 2378 2379 /* Disable all chans and interrupts. */ 2380 at_xdmac_off(atxdmac, true); 2381 2382 for (i = 0; i < nr_channels; i++) { 2383 struct at_xdmac_chan *atchan = &atxdmac->chan[i]; 2384 2385 atchan->chan.device = &atxdmac->dma; 2386 list_add_tail(&atchan->chan.device_node, 2387 &atxdmac->dma.channels); 2388 2389 atchan->ch_regs = at_xdmac_chan_reg_base(atxdmac, i); 2390 atchan->mask = 1 << i; 2391 2392 spin_lock_init(&atchan->lock); 2393 INIT_LIST_HEAD(&atchan->xfers_list); 2394 INIT_LIST_HEAD(&atchan->free_descs_list); 2395 tasklet_setup(&atchan->tasklet, at_xdmac_tasklet); 2396 2397 /* Clear pending interrupts. */ 2398 while (at_xdmac_chan_read(atchan, AT_XDMAC_CIS)) 2399 cpu_relax(); 2400 } 2401 2402 ret = dma_async_device_register(&atxdmac->dma); 2403 if (ret) { 2404 dev_err(&pdev->dev, "fail to register DMA engine device\n"); 2405 goto err_pm_disable; 2406 } 2407 2408 ret = of_dma_controller_register(pdev->dev.of_node, 2409 at_xdmac_xlate, atxdmac); 2410 if (ret) { 2411 dev_err(&pdev->dev, "could not register of dma controller\n"); 2412 goto err_dma_unregister; 2413 } 2414 2415 dev_info(&pdev->dev, "%d channels, mapped at 0x%p\n", 2416 nr_channels, atxdmac->regs); 2417 2418 at_xdmac_axi_config(pdev); 2419 2420 pm_runtime_put_autosuspend(&pdev->dev); 2421 2422 return 0; 2423 2424 err_dma_unregister: 2425 dma_async_device_unregister(&atxdmac->dma); 2426 err_pm_disable: 2427 pm_runtime_put_noidle(&pdev->dev); 2428 pm_runtime_disable(&pdev->dev); 2429 pm_runtime_set_suspended(&pdev->dev); 2430 pm_runtime_dont_use_autosuspend(&pdev->dev); 2431 err_clk_disable: 2432 clk_disable_unprepare(atxdmac->clk); 2433 err_free_irq: 2434 free_irq(atxdmac->irq, atxdmac); 2435 return ret; 2436 } 2437 2438 static void at_xdmac_remove(struct platform_device *pdev) 2439 { 2440 struct at_xdmac *atxdmac = (struct at_xdmac *)platform_get_drvdata(pdev); 2441 int i; 2442 2443 at_xdmac_off(atxdmac, true); 2444 of_dma_controller_free(pdev->dev.of_node); 2445 dma_async_device_unregister(&atxdmac->dma); 2446 pm_runtime_disable(atxdmac->dev); 2447 pm_runtime_set_suspended(&pdev->dev); 2448 pm_runtime_dont_use_autosuspend(&pdev->dev); 2449 clk_disable_unprepare(atxdmac->clk); 2450 2451 free_irq(atxdmac->irq, atxdmac); 2452 2453 for (i = 0; i < atxdmac->dma.chancnt; i++) { 2454 struct at_xdmac_chan *atchan = &atxdmac->chan[i]; 2455 2456 tasklet_kill(&atchan->tasklet); 2457 at_xdmac_free_chan_resources(&atchan->chan); 2458 } 2459 } 2460 2461 static const struct dev_pm_ops __maybe_unused atmel_xdmac_dev_pm_ops = { 2462 .prepare = atmel_xdmac_prepare, 2463 SET_LATE_SYSTEM_SLEEP_PM_OPS(atmel_xdmac_suspend, atmel_xdmac_resume) 2464 SET_RUNTIME_PM_OPS(atmel_xdmac_runtime_suspend, 2465 atmel_xdmac_runtime_resume, NULL) 2466 }; 2467 2468 static const struct of_device_id atmel_xdmac_dt_ids[] = { 2469 { 2470 .compatible = "atmel,sama5d4-dma", 2471 .data = &at_xdmac_sama5d4_layout, 2472 }, { 2473 .compatible = "microchip,sama7g5-dma", 2474 .data = &at_xdmac_sama7g5_layout, 2475 }, { 2476 /* sentinel */ 2477 } 2478 }; 2479 MODULE_DEVICE_TABLE(of, atmel_xdmac_dt_ids); 2480 2481 static struct platform_driver at_xdmac_driver = { 2482 .probe = at_xdmac_probe, 2483 .remove = at_xdmac_remove, 2484 .driver = { 2485 .name = "at_xdmac", 2486 .of_match_table = of_match_ptr(atmel_xdmac_dt_ids), 2487 .pm = pm_ptr(&atmel_xdmac_dev_pm_ops), 2488 } 2489 }; 2490 2491 static int __init at_xdmac_init(void) 2492 { 2493 return platform_driver_register(&at_xdmac_driver); 2494 } 2495 subsys_initcall(at_xdmac_init); 2496 2497 static void __exit at_xdmac_exit(void) 2498 { 2499 platform_driver_unregister(&at_xdmac_driver); 2500 } 2501 module_exit(at_xdmac_exit); 2502 2503 MODULE_DESCRIPTION("Atmel Extended DMA Controller driver"); 2504 MODULE_AUTHOR("Ludovic Desroches <ludovic.desroches@atmel.com>"); 2505 MODULE_LICENSE("GPL"); 2506