ste_dma40.c 94.7 KB
Newer Older
1
/*
2 3
 * Copyright (C) Ericsson AB 2007-2008
 * Copyright (C) ST-Ericsson SA 2008-2010
4
 * Author: Per Forlin <per.forlin@stericsson.com> for ST-Ericsson
5
 * Author: Jonas Aaberg <jonas.aberg@stericsson.com> for ST-Ericsson
6 7 8
 * License terms: GNU General Public License (GPL) version 2
 */

9
#include <linux/dma-mapping.h>
10 11
#include <linux/kernel.h>
#include <linux/slab.h>
12
#include <linux/export.h>
13 14 15 16
#include <linux/dmaengine.h>
#include <linux/platform_device.h>
#include <linux/clk.h>
#include <linux/delay.h>
17 18
#include <linux/pm.h>
#include <linux/pm_runtime.h>
19
#include <linux/err.h>
20
#include <linux/of.h>
21
#include <linux/of_dma.h>
22
#include <linux/amba/bus.h>
23
#include <linux/regulator/consumer.h>
24
#include <linux/platform_data/dma-ste-dma40.h>
25

26
#include "dmaengine.h"
27 28 29 30 31 32 33 34 35 36 37 38 39
#include "ste_dma40_ll.h"

#define D40_NAME "dma40"

#define D40_PHY_CHAN -1

/* For masking out/in 2 bit channel positions */
#define D40_CHAN_POS(chan)  (2 * (chan / 2))
#define D40_CHAN_POS_MASK(chan) (0x3 << D40_CHAN_POS(chan))

/* Maximum iterations taken before giving up suspending a channel */
#define D40_SUSPEND_MAX_IT 500

40 41 42
/* Milliseconds */
#define DMA40_AUTOSUSPEND_DELAY	100

43 44
/* Hardware requirement on LCLA alignment */
#define LCLA_ALIGNMENT 0x40000
45 46 47 48 49

/* Max number of links per event group */
#define D40_LCLA_LINK_PER_EVENT_GRP 128
#define D40_LCLA_END D40_LCLA_LINK_PER_EVENT_GRP

50 51 52
/* Max number of logical channels per physical channel */
#define D40_MAX_LOG_CHAN_PER_PHY 32

53 54 55 56
/* Attempts before giving up to trying to get pages that are aligned */
#define MAX_LCLA_ALLOC_ATTEMPTS 256

/* Bit markings for allocation map */
57 58
#define D40_ALLOC_FREE		BIT(31)
#define D40_ALLOC_PHY		BIT(30)
59 60
#define D40_ALLOC_LOG_FREE	0

61
/* Reserved event lines for memcpy only. */
62 63 64 65 66 67 68 69 70 71 72 73 74 75 76
#define DB8500_DMA_MEMCPY_EV_0	51
#define DB8500_DMA_MEMCPY_EV_1	56
#define DB8500_DMA_MEMCPY_EV_2	57
#define DB8500_DMA_MEMCPY_EV_3	58
#define DB8500_DMA_MEMCPY_EV_4	59
#define DB8500_DMA_MEMCPY_EV_5	60

static int dma40_memcpy_channels[] = {
	DB8500_DMA_MEMCPY_EV_0,
	DB8500_DMA_MEMCPY_EV_1,
	DB8500_DMA_MEMCPY_EV_2,
	DB8500_DMA_MEMCPY_EV_3,
	DB8500_DMA_MEMCPY_EV_4,
	DB8500_DMA_MEMCPY_EV_5,
};
77

78 79 80
/* Default configuration for physcial memcpy */
struct stedma40_chan_cfg dma40_memcpy_conf_phy = {
	.mode = STEDMA40_MODE_PHYSICAL,
81
	.dir = DMA_MEM_TO_MEM,
82

83
	.src_info.data_width = DMA_SLAVE_BUSWIDTH_1_BYTE,
84 85 86
	.src_info.psize = STEDMA40_PSIZE_PHY_1,
	.src_info.flow_ctrl = STEDMA40_NO_FLOW_CTRL,

87
	.dst_info.data_width = DMA_SLAVE_BUSWIDTH_1_BYTE,
88 89 90 91 92 93 94
	.dst_info.psize = STEDMA40_PSIZE_PHY_1,
	.dst_info.flow_ctrl = STEDMA40_NO_FLOW_CTRL,
};

/* Default configuration for logical memcpy */
struct stedma40_chan_cfg dma40_memcpy_conf_log = {
	.mode = STEDMA40_MODE_LOGICAL,
95
	.dir = DMA_MEM_TO_MEM,
96

97
	.src_info.data_width = DMA_SLAVE_BUSWIDTH_1_BYTE,
98 99 100
	.src_info.psize = STEDMA40_PSIZE_LOG_1,
	.src_info.flow_ctrl = STEDMA40_NO_FLOW_CTRL,

101
	.dst_info.data_width = DMA_SLAVE_BUSWIDTH_1_BYTE,
102 103 104 105
	.dst_info.psize = STEDMA40_PSIZE_LOG_1,
	.dst_info.flow_ctrl = STEDMA40_NO_FLOW_CTRL,
};

106 107 108 109 110 111 112 113 114 115 116 117 118 119 120
/**
 * enum 40_command - The different commands and/or statuses.
 *
 * @D40_DMA_STOP: DMA channel command STOP or status STOPPED,
 * @D40_DMA_RUN: The DMA channel is RUNNING of the command RUN.
 * @D40_DMA_SUSPEND_REQ: Request the DMA to SUSPEND as soon as possible.
 * @D40_DMA_SUSPENDED: The DMA channel is SUSPENDED.
 */
enum d40_command {
	D40_DMA_STOP		= 0,
	D40_DMA_RUN		= 1,
	D40_DMA_SUSPEND_REQ	= 2,
	D40_DMA_SUSPENDED	= 3
};

121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136
/*
 * enum d40_events - The different Event Enables for the event lines.
 *
 * @D40_DEACTIVATE_EVENTLINE: De-activate Event line, stopping the logical chan.
 * @D40_ACTIVATE_EVENTLINE: Activate the Event line, to start a logical chan.
 * @D40_SUSPEND_REQ_EVENTLINE: Requesting for suspending a event line.
 * @D40_ROUND_EVENTLINE: Status check for event line.
 */

enum d40_events {
	D40_DEACTIVATE_EVENTLINE	= 0,
	D40_ACTIVATE_EVENTLINE		= 1,
	D40_SUSPEND_REQ_EVENTLINE	= 2,
	D40_ROUND_EVENTLINE		= 3
};

137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152
/*
 * These are the registers that has to be saved and later restored
 * when the DMA hw is powered off.
 * TODO: Add save/restore of D40_DREG_GCC on dma40 v3 or later, if that works.
 */
static u32 d40_backup_regs[] = {
	D40_DREG_LCPA,
	D40_DREG_LCLA,
	D40_DREG_PRMSE,
	D40_DREG_PRMSO,
	D40_DREG_PRMOE,
	D40_DREG_PRMOO,
};

#define BACKUP_REGS_SZ ARRAY_SIZE(d40_backup_regs)

153 154 155 156 157 158 159 160 161 162 163 164 165
/*
 * since 9540 and 8540 has the same HW revision
 * use v4a for 9540 or ealier
 * use v4b for 8540 or later
 * HW revision:
 * DB8500ed has revision 0
 * DB8500v1 has revision 2
 * DB8500v2 has revision 3
 * AP9540v1 has revision 4
 * DB8540v1 has revision 4
 * TODO: Check if all these registers have to be saved/restored on dma40 v4a
 */
static u32 d40_backup_regs_v4a[] = {
166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183
	D40_DREG_PSEG1,
	D40_DREG_PSEG2,
	D40_DREG_PSEG3,
	D40_DREG_PSEG4,
	D40_DREG_PCEG1,
	D40_DREG_PCEG2,
	D40_DREG_PCEG3,
	D40_DREG_PCEG4,
	D40_DREG_RSEG1,
	D40_DREG_RSEG2,
	D40_DREG_RSEG3,
	D40_DREG_RSEG4,
	D40_DREG_RCEG1,
	D40_DREG_RCEG2,
	D40_DREG_RCEG3,
	D40_DREG_RCEG4,
};

184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209
#define BACKUP_REGS_SZ_V4A ARRAY_SIZE(d40_backup_regs_v4a)

static u32 d40_backup_regs_v4b[] = {
	D40_DREG_CPSEG1,
	D40_DREG_CPSEG2,
	D40_DREG_CPSEG3,
	D40_DREG_CPSEG4,
	D40_DREG_CPSEG5,
	D40_DREG_CPCEG1,
	D40_DREG_CPCEG2,
	D40_DREG_CPCEG3,
	D40_DREG_CPCEG4,
	D40_DREG_CPCEG5,
	D40_DREG_CRSEG1,
	D40_DREG_CRSEG2,
	D40_DREG_CRSEG3,
	D40_DREG_CRSEG4,
	D40_DREG_CRSEG5,
	D40_DREG_CRCEG1,
	D40_DREG_CRCEG2,
	D40_DREG_CRCEG3,
	D40_DREG_CRCEG4,
	D40_DREG_CRCEG5,
};

#define BACKUP_REGS_SZ_V4B ARRAY_SIZE(d40_backup_regs_v4b)
210 211 212 213 214 215 216 217 218 219 220 221

static u32 d40_backup_regs_chan[] = {
	D40_CHAN_REG_SSCFG,
	D40_CHAN_REG_SSELT,
	D40_CHAN_REG_SSPTR,
	D40_CHAN_REG_SSLNK,
	D40_CHAN_REG_SDCFG,
	D40_CHAN_REG_SDELT,
	D40_CHAN_REG_SDPTR,
	D40_CHAN_REG_SDLNK,
};

222 223 224
#define BACKUP_REGS_SZ_MAX ((BACKUP_REGS_SZ_V4A > BACKUP_REGS_SZ_V4B) ? \
			     BACKUP_REGS_SZ_V4A : BACKUP_REGS_SZ_V4B)

225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320
/**
 * struct d40_interrupt_lookup - lookup table for interrupt handler
 *
 * @src: Interrupt mask register.
 * @clr: Interrupt clear register.
 * @is_error: true if this is an error interrupt.
 * @offset: start delta in the lookup_log_chans in d40_base. If equals to
 * D40_PHY_CHAN, the lookup_phy_chans shall be used instead.
 */
struct d40_interrupt_lookup {
	u32 src;
	u32 clr;
	bool is_error;
	int offset;
};


static struct d40_interrupt_lookup il_v4a[] = {
	{D40_DREG_LCTIS0, D40_DREG_LCICR0, false,  0},
	{D40_DREG_LCTIS1, D40_DREG_LCICR1, false, 32},
	{D40_DREG_LCTIS2, D40_DREG_LCICR2, false, 64},
	{D40_DREG_LCTIS3, D40_DREG_LCICR3, false, 96},
	{D40_DREG_LCEIS0, D40_DREG_LCICR0, true,   0},
	{D40_DREG_LCEIS1, D40_DREG_LCICR1, true,  32},
	{D40_DREG_LCEIS2, D40_DREG_LCICR2, true,  64},
	{D40_DREG_LCEIS3, D40_DREG_LCICR3, true,  96},
	{D40_DREG_PCTIS,  D40_DREG_PCICR,  false, D40_PHY_CHAN},
	{D40_DREG_PCEIS,  D40_DREG_PCICR,  true,  D40_PHY_CHAN},
};

static struct d40_interrupt_lookup il_v4b[] = {
	{D40_DREG_CLCTIS1, D40_DREG_CLCICR1, false,  0},
	{D40_DREG_CLCTIS2, D40_DREG_CLCICR2, false, 32},
	{D40_DREG_CLCTIS3, D40_DREG_CLCICR3, false, 64},
	{D40_DREG_CLCTIS4, D40_DREG_CLCICR4, false, 96},
	{D40_DREG_CLCTIS5, D40_DREG_CLCICR5, false, 128},
	{D40_DREG_CLCEIS1, D40_DREG_CLCICR1, true,   0},
	{D40_DREG_CLCEIS2, D40_DREG_CLCICR2, true,  32},
	{D40_DREG_CLCEIS3, D40_DREG_CLCICR3, true,  64},
	{D40_DREG_CLCEIS4, D40_DREG_CLCICR4, true,  96},
	{D40_DREG_CLCEIS5, D40_DREG_CLCICR5, true,  128},
	{D40_DREG_CPCTIS,  D40_DREG_CPCICR,  false, D40_PHY_CHAN},
	{D40_DREG_CPCEIS,  D40_DREG_CPCICR,  true,  D40_PHY_CHAN},
};

/**
 * struct d40_reg_val - simple lookup struct
 *
 * @reg: The register.
 * @val: The value that belongs to the register in reg.
 */
struct d40_reg_val {
	unsigned int reg;
	unsigned int val;
};

static __initdata struct d40_reg_val dma_init_reg_v4a[] = {
	/* Clock every part of the DMA block from start */
	{ .reg = D40_DREG_GCC,    .val = D40_DREG_GCC_ENABLE_ALL},

	/* Interrupts on all logical channels */
	{ .reg = D40_DREG_LCMIS0, .val = 0xFFFFFFFF},
	{ .reg = D40_DREG_LCMIS1, .val = 0xFFFFFFFF},
	{ .reg = D40_DREG_LCMIS2, .val = 0xFFFFFFFF},
	{ .reg = D40_DREG_LCMIS3, .val = 0xFFFFFFFF},
	{ .reg = D40_DREG_LCICR0, .val = 0xFFFFFFFF},
	{ .reg = D40_DREG_LCICR1, .val = 0xFFFFFFFF},
	{ .reg = D40_DREG_LCICR2, .val = 0xFFFFFFFF},
	{ .reg = D40_DREG_LCICR3, .val = 0xFFFFFFFF},
	{ .reg = D40_DREG_LCTIS0, .val = 0xFFFFFFFF},
	{ .reg = D40_DREG_LCTIS1, .val = 0xFFFFFFFF},
	{ .reg = D40_DREG_LCTIS2, .val = 0xFFFFFFFF},
	{ .reg = D40_DREG_LCTIS3, .val = 0xFFFFFFFF}
};
static __initdata struct d40_reg_val dma_init_reg_v4b[] = {
	/* Clock every part of the DMA block from start */
	{ .reg = D40_DREG_GCC,    .val = D40_DREG_GCC_ENABLE_ALL},

	/* Interrupts on all logical channels */
	{ .reg = D40_DREG_CLCMIS1, .val = 0xFFFFFFFF},
	{ .reg = D40_DREG_CLCMIS2, .val = 0xFFFFFFFF},
	{ .reg = D40_DREG_CLCMIS3, .val = 0xFFFFFFFF},
	{ .reg = D40_DREG_CLCMIS4, .val = 0xFFFFFFFF},
	{ .reg = D40_DREG_CLCMIS5, .val = 0xFFFFFFFF},
	{ .reg = D40_DREG_CLCICR1, .val = 0xFFFFFFFF},
	{ .reg = D40_DREG_CLCICR2, .val = 0xFFFFFFFF},
	{ .reg = D40_DREG_CLCICR3, .val = 0xFFFFFFFF},
	{ .reg = D40_DREG_CLCICR4, .val = 0xFFFFFFFF},
	{ .reg = D40_DREG_CLCICR5, .val = 0xFFFFFFFF},
	{ .reg = D40_DREG_CLCTIS1, .val = 0xFFFFFFFF},
	{ .reg = D40_DREG_CLCTIS2, .val = 0xFFFFFFFF},
	{ .reg = D40_DREG_CLCTIS3, .val = 0xFFFFFFFF},
	{ .reg = D40_DREG_CLCTIS4, .val = 0xFFFFFFFF},
	{ .reg = D40_DREG_CLCTIS5, .val = 0xFFFFFFFF}
};

321 322 323 324 325 326
/**
 * struct d40_lli_pool - Structure for keeping LLIs in memory
 *
 * @base: Pointer to memory area when the pre_alloc_lli's are not large
 * enough, IE bigger than the most common case, 1 dst and 1 src. NULL if
 * pre_alloc_lli is used.
327
 * @dma_addr: DMA address, if mapped
328 329 330 331 332 333
 * @size: The size in bytes of the memory at base or the size of pre_alloc_lli.
 * @pre_alloc_lli: Pre allocated area for the most common case of transfers,
 * one buffer to one buffer.
 */
struct d40_lli_pool {
	void	*base;
334
	int	 size;
335
	dma_addr_t	dma_addr;
336
	/* Space for dst and src, plus an extra for padding */
337
	u8	 pre_alloc_lli[3 * sizeof(struct d40_phy_lli)];
338 339 340 341 342 343 344 345 346 347
};

/**
 * struct d40_desc - A descriptor is one DMA job.
 *
 * @lli_phy: LLI settings for physical channel. Both src and dst=
 * points into the lli_pool, to base if lli_len > 1 or to pre_alloc_lli if
 * lli_len equals one.
 * @lli_log: Same as above but for logical channels.
 * @lli_pool: The pool with two entries pre-allocated.
348
 * @lli_len: Number of llis of current descriptor.
L
Lucas De Marchi 已提交
349
 * @lli_current: Number of transferred llis.
350
 * @lcla_alloc: Number of LCLA entries allocated.
351 352 353 354
 * @txd: DMA engine struct. Used for among other things for communication
 * during a transfer.
 * @node: List entry.
 * @is_in_client_list: true if the client owns this descriptor.
355
 * @cyclic: true if this is a cyclic job
356 357 358 359 360 361 362 363 364 365
 *
 * This descriptor is used for both logical and physical transfers.
 */
struct d40_desc {
	/* LLI physical */
	struct d40_phy_lli_bidir	 lli_phy;
	/* LLI logical */
	struct d40_log_lli_bidir	 lli_log;

	struct d40_lli_pool		 lli_pool;
366
	int				 lli_len;
367 368
	int				 lli_current;
	int				 lcla_alloc;
369 370 371 372 373

	struct dma_async_tx_descriptor	 txd;
	struct list_head		 node;

	bool				 is_in_client_list;
R
Rabin Vincent 已提交
374
	bool				 cyclic;
375 376 377 378 379
};

/**
 * struct d40_lcla_pool - LCLA pool settings and data.
 *
380 381 382 383 384
 * @base: The virtual address of LCLA. 18 bit aligned.
 * @base_unaligned: The orignal kmalloc pointer, if kmalloc is used.
 * This pointer is only there for clean-up on error.
 * @pages: The number of pages needed for all physical channels.
 * Only used later for clean-up on error
385
 * @lock: Lock to protect the content in this struct.
386
 * @alloc_map: big map over which LCLA entry is own by which job.
387 388 389
 */
struct d40_lcla_pool {
	void		*base;
390
	dma_addr_t	dma_addr;
391 392
	void		*base_unaligned;
	int		 pages;
393
	spinlock_t	 lock;
394
	struct d40_desc	**alloc_map;
395 396 397 398 399 400 401
};

/**
 * struct d40_phy_res - struct for handling eventlines mapped to physical
 * channels.
 *
 * @lock: A lock protection this entity.
402
 * @reserved: True if used by secure world or otherwise.
403 404 405 406 407
 * @num: The physical channel number of this entity.
 * @allocated_src: Bit mapped to show which src event line's are mapped to
 * this physical channel. Can also be free or physically allocated.
 * @allocated_dst: Same as for src but is dst.
 * allocated_dst and allocated_src uses the D40_ALLOC* defines as well as
408
 * event line number.
409
 * @use_soft_lli: To mark if the linked lists of channel are managed by SW.
410 411 412
 */
struct d40_phy_res {
	spinlock_t lock;
413
	bool	   reserved;
414 415 416
	int	   num;
	u32	   allocated_src;
	u32	   allocated_dst;
417
	bool	   use_soft_lli;
418 419 420 421 422 423 424 425 426 427 428 429
};

struct d40_base;

/**
 * struct d40_chan - Struct that describes a channel.
 *
 * @lock: A spinlock to protect this struct.
 * @log_num: The logical number, if any of this channel.
 * @pending_tx: The number of pending transfers. Used between interrupt handler
 * and tasklet.
 * @busy: Set to true when transfer is ongoing on this channel.
430 431
 * @phy_chan: Pointer to physical channel which this instance runs on. If this
 * point is NULL, then the channel is not allocated.
432 433 434 435
 * @chan: DMA engine handle.
 * @tasklet: Tasklet that gets scheduled from interrupt context to complete a
 * transfer and call client callback.
 * @client: Cliented owned descriptor list.
436
 * @pending_queue: Submitted jobs, to be issued by issue_pending()
437
 * @active: Active descriptor.
438
 * @done: Completed jobs
439
 * @queue: Queued jobs.
440
 * @prepare_queue: Prepared jobs.
441
 * @dma_cfg: The client configuration of this dma channel.
442
 * @configured: whether the dma_cfg configuration is valid
443 444 445 446 447
 * @base: Pointer to the device instance struct.
 * @src_def_cfg: Default cfg register setting for src.
 * @dst_def_cfg: Default cfg register setting for dst.
 * @log_def: Default logical channel settings.
 * @lcpa: Pointer to dst and src lcpa settings.
448 449
 * @runtime_addr: runtime configured address.
 * @runtime_direction: runtime configured direction.
450 451 452 453 454 455 456 457 458 459 460 461
 *
 * This struct can either "be" a logical or a physical channel.
 */
struct d40_chan {
	spinlock_t			 lock;
	int				 log_num;
	int				 pending_tx;
	bool				 busy;
	struct d40_phy_res		*phy_chan;
	struct dma_chan			 chan;
	struct tasklet_struct		 tasklet;
	struct list_head		 client;
462
	struct list_head		 pending_queue;
463
	struct list_head		 active;
464
	struct list_head		 done;
465
	struct list_head		 queue;
466
	struct list_head		 prepare_queue;
467
	struct stedma40_chan_cfg	 dma_cfg;
468
	bool				 configured;
469 470 471 472 473 474
	struct d40_base			*base;
	/* Default register configurations */
	u32				 src_def_cfg;
	u32				 dst_def_cfg;
	struct d40_def_lcsp		 log_def;
	struct d40_log_lli_full		*lcpa;
475 476
	/* Runtime reconfiguration */
	dma_addr_t			runtime_addr;
477
	enum dma_transfer_direction	runtime_direction;
478 479
};

480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511
/**
 * struct d40_gen_dmac - generic values to represent u8500/u8540 DMA
 * controller
 *
 * @backup: the pointer to the registers address array for backup
 * @backup_size: the size of the registers address array for backup
 * @realtime_en: the realtime enable register
 * @realtime_clear: the realtime clear register
 * @high_prio_en: the high priority enable register
 * @high_prio_clear: the high priority clear register
 * @interrupt_en: the interrupt enable register
 * @interrupt_clear: the interrupt clear register
 * @il: the pointer to struct d40_interrupt_lookup
 * @il_size: the size of d40_interrupt_lookup array
 * @init_reg: the pointer to the struct d40_reg_val
 * @init_reg_size: the size of d40_reg_val array
 */
struct d40_gen_dmac {
	u32				*backup;
	u32				 backup_size;
	u32				 realtime_en;
	u32				 realtime_clear;
	u32				 high_prio_en;
	u32				 high_prio_clear;
	u32				 interrupt_en;
	u32				 interrupt_clear;
	struct d40_interrupt_lookup	*il;
	u32				 il_size;
	struct d40_reg_val		*init_reg;
	u32				 init_reg_size;
};

512 513 514 515 516 517 518 519
/**
 * struct d40_base - The big global struct, one for each probe'd instance.
 *
 * @interrupt_lock: Lock used to make sure one interrupt is handle a time.
 * @execmd_lock: Lock for execute command usage since several channels share
 * the same physical register.
 * @dev: The device structure.
 * @virtbase: The virtual base address of the DMA's register.
520
 * @rev: silicon revision detected.
521 522 523 524 525 526 527 528 529 530 531 532
 * @clk: Pointer to the DMA clock structure.
 * @phy_start: Physical memory start of the DMA registers.
 * @phy_size: Size of the DMA register map.
 * @irq: The IRQ number.
 * @num_phy_chans: The number of physical channels. Read from HW. This
 * is the number of available channels for this driver, not counting "Secure
 * mode" allocated physical channels.
 * @num_log_chans: The number of logical channels. Calculated from
 * num_phy_chans.
 * @dma_both: dma_device channels that can do both memcpy and slave transfers.
 * @dma_slave: dma_device channels that can do only do slave transfers.
 * @dma_memcpy: dma_device channels that can do only do memcpy transfers.
533
 * @phy_chans: Room for all possible physical channels in system.
534 535 536 537 538 539 540
 * @log_chans: Room for all possible logical channels in system.
 * @lookup_log_chans: Used to map interrupt number to logical channel. Points
 * to log_chans entries.
 * @lookup_phy_chans: Used to map interrupt number to physical channel. Points
 * to phy_chans entries.
 * @plat_data: Pointer to provided platform_data which is the driver
 * configuration.
541
 * @lcpa_regulator: Pointer to hold the regulator for the esram bank for lcla.
542 543 544 545 546
 * @phy_res: Vector containing all physical channels.
 * @lcla_pool: lcla pool settings and data.
 * @lcpa_base: The virtual mapped address of LCPA.
 * @phy_lcpa: The physical address of the LCPA.
 * @lcpa_size: The size of the LCPA area.
547
 * @desc_slab: cache for descriptors.
548 549
 * @reg_val_backup: Here the values of some hardware registers are stored
 * before the DMA is powered off. They are restored when the power is back on.
550 551
 * @reg_val_backup_v4: Backup of registers that only exits on dma40 v3 and
 * later
552 553 554
 * @reg_val_backup_chan: Backup data for standard channel parameter registers.
 * @gcc_pwr_off_mask: Mask to maintain the channels that can be turned off.
 * @initialized: true if the dma has been initialized
555 556
 * @gen_dmac: the struct for generic registers values to represent u8500/8540
 * DMA controller
557 558 559 560 561 562
 */
struct d40_base {
	spinlock_t			 interrupt_lock;
	spinlock_t			 execmd_lock;
	struct device			 *dev;
	void __iomem			 *virtbase;
563
	u8				  rev:4;
564 565 566 567 568 569
	struct clk			 *clk;
	phys_addr_t			  phy_start;
	resource_size_t			  phy_size;
	int				  irq;
	int				  num_phy_chans;
	int				  num_log_chans;
570
	struct device_dma_parameters	  dma_parms;
571 572 573 574 575 576 577 578
	struct dma_device		  dma_both;
	struct dma_device		  dma_slave;
	struct dma_device		  dma_memcpy;
	struct d40_chan			 *phy_chans;
	struct d40_chan			 *log_chans;
	struct d40_chan			**lookup_log_chans;
	struct d40_chan			**lookup_phy_chans;
	struct stedma40_platform_data	 *plat_data;
579
	struct regulator		 *lcpa_regulator;
580 581 582 583 584 585
	/* Physical half channels */
	struct d40_phy_res		 *phy_res;
	struct d40_lcla_pool		  lcla_pool;
	void				 *lcpa_base;
	dma_addr_t			  phy_lcpa;
	resource_size_t			  lcpa_size;
586
	struct kmem_cache		 *desc_slab;
587
	u32				  reg_val_backup[BACKUP_REGS_SZ];
588
	u32				  reg_val_backup_v4[BACKUP_REGS_SZ_MAX];
589 590 591
	u32				 *reg_val_backup_chan;
	u16				  gcc_pwr_off_mask;
	bool				  initialized;
592
	struct d40_gen_dmac		  gen_dmac;
593 594
};

595 596 597 598 599
static struct device *chan2dev(struct d40_chan *d40c)
{
	return &d40c->chan.dev->device;
}

600 601 602 603 604 605 606 607 608 609
static bool chan_is_physical(struct d40_chan *chan)
{
	return chan->log_num == D40_PHY_CHAN;
}

static bool chan_is_logical(struct d40_chan *chan)
{
	return !chan_is_physical(chan);
}

610 611 612 613 614 615
static void __iomem *chan_base(struct d40_chan *chan)
{
	return chan->base->virtbase + D40_DREG_PCBASE +
	       chan->phy_chan->num * D40_DREG_PCDELTA;
}

616 617 618 619 620 621
#define d40_err(dev, format, arg...)		\
	dev_err(dev, "[%s] " format, __func__, ## arg)

#define chan_err(d40c, format, arg...)		\
	d40_err(chan2dev(d40c), format, ## arg)

622
static int d40_pool_lli_alloc(struct d40_chan *d40c, struct d40_desc *d40d,
623
			      int lli_len)
624
{
625
	bool is_log = chan_is_logical(d40c);
626 627 628 629 630 631 632 633 634 635 636 637 638
	u32 align;
	void *base;

	if (is_log)
		align = sizeof(struct d40_log_lli);
	else
		align = sizeof(struct d40_phy_lli);

	if (lli_len == 1) {
		base = d40d->lli_pool.pre_alloc_lli;
		d40d->lli_pool.size = sizeof(d40d->lli_pool.pre_alloc_lli);
		d40d->lli_pool.base = NULL;
	} else {
639
		d40d->lli_pool.size = lli_len * 2 * align;
640 641 642 643 644 645 646 647 648

		base = kmalloc(d40d->lli_pool.size + align, GFP_NOWAIT);
		d40d->lli_pool.base = base;

		if (d40d->lli_pool.base == NULL)
			return -ENOMEM;
	}

	if (is_log) {
R
Rabin Vincent 已提交
649
		d40d->lli_log.src = PTR_ALIGN(base, align);
650
		d40d->lli_log.dst = d40d->lli_log.src + lli_len;
651 652

		d40d->lli_pool.dma_addr = 0;
653
	} else {
R
Rabin Vincent 已提交
654
		d40d->lli_phy.src = PTR_ALIGN(base, align);
655
		d40d->lli_phy.dst = d40d->lli_phy.src + lli_len;
656 657 658 659 660 661 662 663 664 665 666 667 668

		d40d->lli_pool.dma_addr = dma_map_single(d40c->base->dev,
							 d40d->lli_phy.src,
							 d40d->lli_pool.size,
							 DMA_TO_DEVICE);

		if (dma_mapping_error(d40c->base->dev,
				      d40d->lli_pool.dma_addr)) {
			kfree(d40d->lli_pool.base);
			d40d->lli_pool.base = NULL;
			d40d->lli_pool.dma_addr = 0;
			return -ENOMEM;
		}
669 670 671 672 673
	}

	return 0;
}

674
static void d40_pool_lli_free(struct d40_chan *d40c, struct d40_desc *d40d)
675
{
676 677 678 679
	if (d40d->lli_pool.dma_addr)
		dma_unmap_single(d40c->base->dev, d40d->lli_pool.dma_addr,
				 d40d->lli_pool.size, DMA_TO_DEVICE);

680 681 682 683 684 685 686 687 688
	kfree(d40d->lli_pool.base);
	d40d->lli_pool.base = NULL;
	d40d->lli_pool.size = 0;
	d40d->lli_log.src = NULL;
	d40d->lli_log.dst = NULL;
	d40d->lli_phy.src = NULL;
	d40d->lli_phy.dst = NULL;
}

689 690 691 692 693 694 695 696 697 698 699 700 701 702
static int d40_lcla_alloc_one(struct d40_chan *d40c,
			      struct d40_desc *d40d)
{
	unsigned long flags;
	int i;
	int ret = -EINVAL;

	spin_lock_irqsave(&d40c->base->lcla_pool.lock, flags);

	/*
	 * Allocate both src and dst at the same time, therefore the half
	 * start on 1 since 0 can't be used since zero is used as end marker.
	 */
	for (i = 1 ; i < D40_LCLA_LINK_PER_EVENT_GRP / 2; i++) {
703 704 705 706
		int idx = d40c->phy_chan->num * D40_LCLA_LINK_PER_EVENT_GRP + i;

		if (!d40c->base->lcla_pool.alloc_map[idx]) {
			d40c->base->lcla_pool.alloc_map[idx] = d40d;
707 708 709 710 711 712 713 714 715 716 717 718 719 720 721 722 723 724
			d40d->lcla_alloc++;
			ret = i;
			break;
		}
	}

	spin_unlock_irqrestore(&d40c->base->lcla_pool.lock, flags);

	return ret;
}

static int d40_lcla_free_all(struct d40_chan *d40c,
			     struct d40_desc *d40d)
{
	unsigned long flags;
	int i;
	int ret = -EINVAL;

725
	if (chan_is_physical(d40c))
726 727 728 729 730
		return 0;

	spin_lock_irqsave(&d40c->base->lcla_pool.lock, flags);

	for (i = 1 ; i < D40_LCLA_LINK_PER_EVENT_GRP / 2; i++) {
731 732 733 734
		int idx = d40c->phy_chan->num * D40_LCLA_LINK_PER_EVENT_GRP + i;

		if (d40c->base->lcla_pool.alloc_map[idx] == d40d) {
			d40c->base->lcla_pool.alloc_map[idx] = NULL;
735 736 737 738 739 740 741 742 743 744 745 746 747 748
			d40d->lcla_alloc--;
			if (d40d->lcla_alloc == 0) {
				ret = 0;
				break;
			}
		}
	}

	spin_unlock_irqrestore(&d40c->base->lcla_pool.lock, flags);

	return ret;

}

749 750 751 752 753 754 755
static void d40_desc_remove(struct d40_desc *d40d)
{
	list_del(&d40d->node);
}

static struct d40_desc *d40_desc_get(struct d40_chan *d40c)
{
R
Rabin Vincent 已提交
756
	struct d40_desc *desc = NULL;
757 758

	if (!list_empty(&d40c->client)) {
R
Rabin Vincent 已提交
759 760 761
		struct d40_desc *d;
		struct d40_desc *_d;

762
		list_for_each_entry_safe(d, _d, &d40c->client, node) {
763 764
			if (async_tx_test_ack(&d->txd)) {
				d40_desc_remove(d);
R
Rabin Vincent 已提交
765 766
				desc = d;
				memset(desc, 0, sizeof(*desc));
767
				break;
768
			}
769
		}
770
	}
R
Rabin Vincent 已提交
771 772 773 774 775 776 777 778

	if (!desc)
		desc = kmem_cache_zalloc(d40c->base->desc_slab, GFP_NOWAIT);

	if (desc)
		INIT_LIST_HEAD(&desc->node);

	return desc;
779 780 781 782
}

static void d40_desc_free(struct d40_chan *d40c, struct d40_desc *d40d)
{
783

784
	d40_pool_lli_free(d40c, d40d);
785
	d40_lcla_free_all(d40c, d40d);
786
	kmem_cache_free(d40c->base->desc_slab, d40d);
787 788 789 790 791 792 793
}

static void d40_desc_submit(struct d40_chan *d40c, struct d40_desc *desc)
{
	list_add_tail(&desc->node, &d40c->active);
}

794 795 796 797 798 799 800 801 802 803 804 805 806 807 808 809 810
static void d40_phy_lli_load(struct d40_chan *chan, struct d40_desc *desc)
{
	struct d40_phy_lli *lli_dst = desc->lli_phy.dst;
	struct d40_phy_lli *lli_src = desc->lli_phy.src;
	void __iomem *base = chan_base(chan);

	writel(lli_src->reg_cfg, base + D40_CHAN_REG_SSCFG);
	writel(lli_src->reg_elt, base + D40_CHAN_REG_SSELT);
	writel(lli_src->reg_ptr, base + D40_CHAN_REG_SSPTR);
	writel(lli_src->reg_lnk, base + D40_CHAN_REG_SSLNK);

	writel(lli_dst->reg_cfg, base + D40_CHAN_REG_SDCFG);
	writel(lli_dst->reg_elt, base + D40_CHAN_REG_SDELT);
	writel(lli_dst->reg_ptr, base + D40_CHAN_REG_SDPTR);
	writel(lli_dst->reg_lnk, base + D40_CHAN_REG_SDLNK);
}

811 812 813 814 815
static void d40_desc_done(struct d40_chan *d40c, struct d40_desc *desc)
{
	list_add_tail(&desc->node, &d40c->done);
}

816
static void d40_log_lli_to_lcxa(struct d40_chan *chan, struct d40_desc *desc)
817
{
818 819 820 821
	struct d40_lcla_pool *pool = &chan->base->lcla_pool;
	struct d40_log_lli_bidir *lli = &desc->lli_log;
	int lli_current = desc->lli_current;
	int lli_len = desc->lli_len;
R
Rabin Vincent 已提交
822
	bool cyclic = desc->cyclic;
823
	int curr_lcla = -EINVAL;
R
Rabin Vincent 已提交
824
	int first_lcla = 0;
825
	bool use_esram_lcla = chan->base->plat_data->use_esram_lcla;
R
Rabin Vincent 已提交
826
	bool linkback;
827

R
Rabin Vincent 已提交
828 829 830 831 832 833 834 835 836 837 838
	/*
	 * We may have partially running cyclic transfers, in case we did't get
	 * enough LCLA entries.
	 */
	linkback = cyclic && lli_current == 0;

	/*
	 * For linkback, we need one LCLA even with only one link, because we
	 * can't link back to the one in LCPA space
	 */
	if (linkback || (lli_len - lli_current > 1)) {
839 840 841 842 843 844 845
		/*
		 * If the channel is expected to use only soft_lli don't
		 * allocate a lcla. This is to avoid a HW issue that exists
		 * in some controller during a peripheral to memory transfer
		 * that uses linked lists.
		 */
		if (!(chan->phy_chan->use_soft_lli &&
846
			chan->dma_cfg.dir == DMA_DEV_TO_MEM))
847 848
			curr_lcla = d40_lcla_alloc_one(chan, desc);

R
Rabin Vincent 已提交
849 850 851 852 853 854 855 856 857 858 859
		first_lcla = curr_lcla;
	}

	/*
	 * For linkback, we normally load the LCPA in the loop since we need to
	 * link it to the second LCLA and not the first.  However, if we
	 * couldn't even get a first LCLA, then we have to run in LCPA and
	 * reload manually.
	 */
	if (!linkback || curr_lcla == -EINVAL) {
		unsigned int flags = 0;
860

R
Rabin Vincent 已提交
861 862
		if (curr_lcla == -EINVAL)
			flags |= LLI_TERM_INT;
863

R
Rabin Vincent 已提交
864 865 866 867 868 869 870
		d40_log_lli_lcpa_write(chan->lcpa,
				       &lli->dst[lli_current],
				       &lli->src[lli_current],
				       curr_lcla,
				       flags);
		lli_current++;
	}
871 872 873 874

	if (curr_lcla < 0)
		goto out;

875 876 877 878
	for (; lli_current < lli_len; lli_current++) {
		unsigned int lcla_offset = chan->phy_chan->num * 1024 +
					   8 * curr_lcla * 2;
		struct d40_log_lli *lcla = pool->base + lcla_offset;
R
Rabin Vincent 已提交
879
		unsigned int flags = 0;
880 881 882 883 884
		int next_lcla;

		if (lli_current + 1 < lli_len)
			next_lcla = d40_lcla_alloc_one(chan, desc);
		else
R
Rabin Vincent 已提交
885 886 887 888
			next_lcla = linkback ? first_lcla : -EINVAL;

		if (cyclic || next_lcla == -EINVAL)
			flags |= LLI_TERM_INT;
889

R
Rabin Vincent 已提交
890 891 892 893 894 895 896 897 898 899 900 901
		if (linkback && curr_lcla == first_lcla) {
			/* First link goes in both LCPA and LCLA */
			d40_log_lli_lcpa_write(chan->lcpa,
					       &lli->dst[lli_current],
					       &lli->src[lli_current],
					       next_lcla, flags);
		}

		/*
		 * One unused LCLA in the cyclic case if the very first
		 * next_lcla fails...
		 */
902 903 904
		d40_log_lli_lcla_write(lcla,
				       &lli->dst[lli_current],
				       &lli->src[lli_current],
R
Rabin Vincent 已提交
905
				       next_lcla, flags);
906

907 908 909 910 911 912 913 914 915 916
		/*
		 * Cache maintenance is not needed if lcla is
		 * mapped in esram
		 */
		if (!use_esram_lcla) {
			dma_sync_single_range_for_device(chan->base->dev,
						pool->dma_addr, lcla_offset,
						2 * sizeof(struct d40_log_lli),
						DMA_TO_DEVICE);
		}
917 918
		curr_lcla = next_lcla;

R
Rabin Vincent 已提交
919
		if (curr_lcla == -EINVAL || curr_lcla == first_lcla) {
920 921 922 923 924
			lli_current++;
			break;
		}
	}

925
out:
926 927
	desc->lli_current = lli_current;
}
928

929 930
static void d40_desc_load(struct d40_chan *d40c, struct d40_desc *d40d)
{
931
	if (chan_is_physical(d40c)) {
932
		d40_phy_lli_load(d40c, d40d);
933
		d40d->lli_current = d40d->lli_len;
934 935
	} else
		d40_log_lli_to_lcxa(d40c, d40d);
936 937
}

938 939 940 941 942 943 944 945 946 947 948 949 950
static struct d40_desc *d40_first_active_get(struct d40_chan *d40c)
{
	struct d40_desc *d;

	if (list_empty(&d40c->active))
		return NULL;

	d = list_first_entry(&d40c->active,
			     struct d40_desc,
			     node);
	return d;
}

951
/* remove desc from current queue and add it to the pending_queue */
952 953
static void d40_desc_queue(struct d40_chan *d40c, struct d40_desc *desc)
{
954 955
	d40_desc_remove(desc);
	desc->is_in_client_list = false;
956 957 958 959 960 961 962 963 964 965 966 967 968 969
	list_add_tail(&desc->node, &d40c->pending_queue);
}

static struct d40_desc *d40_first_pending(struct d40_chan *d40c)
{
	struct d40_desc *d;

	if (list_empty(&d40c->pending_queue))
		return NULL;

	d = list_first_entry(&d40c->pending_queue,
			     struct d40_desc,
			     node);
	return d;
970 971 972 973 974 975 976 977 978 979 980 981 982 983 984
}

static struct d40_desc *d40_first_queued(struct d40_chan *d40c)
{
	struct d40_desc *d;

	if (list_empty(&d40c->queue))
		return NULL;

	d = list_first_entry(&d40c->queue,
			     struct d40_desc,
			     node);
	return d;
}

985 986 987 988 989 990 991 992
static struct d40_desc *d40_first_done(struct d40_chan *d40c)
{
	if (list_empty(&d40c->done))
		return NULL;

	return list_first_entry(&d40c->done, struct d40_desc, node);
}

993 994 995 996 997 998 999 1000 1001 1002 1003 1004 1005 1006 1007
static int d40_psize_2_burst_size(bool is_log, int psize)
{
	if (is_log) {
		if (psize == STEDMA40_PSIZE_LOG_1)
			return 1;
	} else {
		if (psize == STEDMA40_PSIZE_PHY_1)
			return 1;
	}

	return 2 << psize;
}

/*
 * The dma only supports transmitting packages up to
1008 1009 1010
 * STEDMA40_MAX_SEG_SIZE * data_width, where data_width is stored in Bytes.
 *
 * Calculate the total number of dma elements required to send the entire sg list.
1011 1012 1013 1014 1015 1016
 */
static int d40_size_2_dmalen(int size, u32 data_width1, u32 data_width2)
{
	int dmalen;
	u32 max_w = max(data_width1, data_width2);
	u32 min_w = min(data_width1, data_width2);
1017
	u32 seg_max = ALIGN(STEDMA40_MAX_SEG_SIZE * min_w, max_w);
1018 1019

	if (seg_max > STEDMA40_MAX_SEG_SIZE)
1020
		seg_max -= max_w;
1021

1022
	if (!IS_ALIGNED(size, max_w))
1023 1024 1025 1026 1027 1028 1029 1030 1031 1032 1033 1034 1035 1036 1037 1038 1039 1040 1041 1042 1043 1044 1045 1046 1047 1048 1049 1050 1051
		return -EINVAL;

	if (size <= seg_max)
		dmalen = 1;
	else {
		dmalen = size / seg_max;
		if (dmalen * seg_max < size)
			dmalen++;
	}
	return dmalen;
}

static int d40_sg_2_dmalen(struct scatterlist *sgl, int sg_len,
			   u32 data_width1, u32 data_width2)
{
	struct scatterlist *sg;
	int i;
	int len = 0;
	int ret;

	for_each_sg(sgl, sg, sg_len, i) {
		ret = d40_size_2_dmalen(sg_dma_len(sg),
					data_width1, data_width2);
		if (ret < 0)
			return ret;
		len += ret;
	}
	return len;
}
1052

1053 1054 1055 1056 1057 1058 1059 1060 1061 1062 1063 1064 1065 1066 1067 1068 1069 1070 1071 1072 1073 1074 1075 1076 1077 1078 1079 1080 1081 1082 1083 1084 1085 1086 1087 1088 1089 1090 1091 1092 1093 1094 1095 1096

#ifdef CONFIG_PM
static void dma40_backup(void __iomem *baseaddr, u32 *backup,
			 u32 *regaddr, int num, bool save)
{
	int i;

	for (i = 0; i < num; i++) {
		void __iomem *addr = baseaddr + regaddr[i];

		if (save)
			backup[i] = readl_relaxed(addr);
		else
			writel_relaxed(backup[i], addr);
	}
}

static void d40_save_restore_registers(struct d40_base *base, bool save)
{
	int i;

	/* Save/Restore channel specific registers */
	for (i = 0; i < base->num_phy_chans; i++) {
		void __iomem *addr;
		int idx;

		if (base->phy_res[i].reserved)
			continue;

		addr = base->virtbase + D40_DREG_PCBASE + i * D40_DREG_PCDELTA;
		idx = i * ARRAY_SIZE(d40_backup_regs_chan);

		dma40_backup(addr, &base->reg_val_backup_chan[idx],
			     d40_backup_regs_chan,
			     ARRAY_SIZE(d40_backup_regs_chan),
			     save);
	}

	/* Save/Restore global registers */
	dma40_backup(base->virtbase, base->reg_val_backup,
		     d40_backup_regs, ARRAY_SIZE(d40_backup_regs),
		     save);

	/* Save/Restore registers only existing on dma40 v3 and later */
1097 1098 1099 1100 1101
	if (base->gen_dmac.backup)
		dma40_backup(base->virtbase, base->reg_val_backup_v4,
			     base->gen_dmac.backup,
			base->gen_dmac.backup_size,
			save);
1102 1103 1104 1105 1106 1107
}
#else
static void d40_save_restore_registers(struct d40_base *base, bool save)
{
}
#endif
1108

1109 1110
static int __d40_execute_command_phy(struct d40_chan *d40c,
				     enum d40_command command)
1111
{
1112 1113
	u32 status;
	int i;
1114 1115 1116
	void __iomem *active_reg;
	int ret = 0;
	unsigned long flags;
1117
	u32 wmask;
1118

1119 1120 1121 1122 1123 1124
	if (command == D40_DMA_STOP) {
		ret = __d40_execute_command_phy(d40c, D40_DMA_SUSPEND_REQ);
		if (ret)
			return ret;
	}

1125 1126 1127 1128 1129 1130 1131 1132 1133 1134 1135 1136 1137 1138 1139 1140
	spin_lock_irqsave(&d40c->base->execmd_lock, flags);

	if (d40c->phy_chan->num % 2 == 0)
		active_reg = d40c->base->virtbase + D40_DREG_ACTIVE;
	else
		active_reg = d40c->base->virtbase + D40_DREG_ACTIVO;

	if (command == D40_DMA_SUSPEND_REQ) {
		status = (readl(active_reg) &
			  D40_CHAN_POS_MASK(d40c->phy_chan->num)) >>
			D40_CHAN_POS(d40c->phy_chan->num);

		if (status == D40_DMA_SUSPENDED || status == D40_DMA_STOP)
			goto done;
	}

1141 1142 1143
	wmask = 0xffffffff & ~(D40_CHAN_POS_MASK(d40c->phy_chan->num));
	writel(wmask | (command << D40_CHAN_POS(d40c->phy_chan->num)),
	       active_reg);
1144 1145 1146 1147 1148 1149 1150 1151 1152 1153 1154 1155 1156 1157 1158 1159 1160 1161 1162 1163 1164

	if (command == D40_DMA_SUSPEND_REQ) {

		for (i = 0 ; i < D40_SUSPEND_MAX_IT; i++) {
			status = (readl(active_reg) &
				  D40_CHAN_POS_MASK(d40c->phy_chan->num)) >>
				D40_CHAN_POS(d40c->phy_chan->num);

			cpu_relax();
			/*
			 * Reduce the number of bus accesses while
			 * waiting for the DMA to suspend.
			 */
			udelay(3);

			if (status == D40_DMA_STOP ||
			    status == D40_DMA_SUSPENDED)
				break;
		}

		if (i == D40_SUSPEND_MAX_IT) {
1165 1166 1167
			chan_err(d40c,
				"unable to suspend the chl %d (log: %d) status %x\n",
				d40c->phy_chan->num, d40c->log_num,
1168 1169 1170 1171 1172 1173 1174 1175 1176 1177 1178 1179 1180 1181
				status);
			dump_stack();
			ret = -EBUSY;
		}

	}
done:
	spin_unlock_irqrestore(&d40c->base->execmd_lock, flags);
	return ret;
}

static void d40_term_all(struct d40_chan *d40c)
{
	struct d40_desc *d40d;
1182
	struct d40_desc *_d;
1183

1184 1185 1186 1187 1188 1189
	/* Release completed descriptors */
	while ((d40d = d40_first_done(d40c))) {
		d40_desc_remove(d40d);
		d40_desc_free(d40c, d40d);
	}

1190 1191 1192 1193 1194 1195 1196 1197 1198 1199 1200 1201
	/* Release active descriptors */
	while ((d40d = d40_first_active_get(d40c))) {
		d40_desc_remove(d40d);
		d40_desc_free(d40c, d40d);
	}

	/* Release queued descriptors waiting for transfer */
	while ((d40d = d40_first_queued(d40c))) {
		d40_desc_remove(d40d);
		d40_desc_free(d40c, d40d);
	}

1202 1203 1204 1205 1206
	/* Release pending descriptors */
	while ((d40d = d40_first_pending(d40c))) {
		d40_desc_remove(d40d);
		d40_desc_free(d40c, d40d);
	}
1207

1208 1209 1210 1211 1212 1213 1214
	/* Release client owned descriptors */
	if (!list_empty(&d40c->client))
		list_for_each_entry_safe(d40d, _d, &d40c->client, node) {
			d40_desc_remove(d40d);
			d40_desc_free(d40c, d40d);
		}

1215 1216 1217 1218 1219 1220 1221
	/* Release descriptors in prepare queue */
	if (!list_empty(&d40c->prepare_queue))
		list_for_each_entry_safe(d40d, _d,
					 &d40c->prepare_queue, node) {
			d40_desc_remove(d40d);
			d40_desc_free(d40c, d40d);
		}
1222

1223 1224 1225
	d40c->pending_tx = 0;
}

1226 1227 1228
static void __d40_config_set_event(struct d40_chan *d40c,
				   enum d40_events event_type, u32 event,
				   int reg)
1229
{
1230
	void __iomem *addr = chan_base(d40c) + reg;
1231
	int tries;
1232 1233 1234 1235 1236
	u32 status;

	switch (event_type) {

	case D40_DEACTIVATE_EVENTLINE:
1237 1238 1239

		writel((D40_DEACTIVATE_EVENTLINE << D40_EVENTLINE_POS(event))
		       | ~D40_EVENTLINE_MASK(event), addr);
1240 1241 1242 1243 1244 1245 1246 1247 1248
		break;

	case D40_SUSPEND_REQ_EVENTLINE:
		status = (readl(addr) & D40_EVENTLINE_MASK(event)) >>
			  D40_EVENTLINE_POS(event);

		if (status == D40_DEACTIVATE_EVENTLINE ||
		    status == D40_SUSPEND_REQ_EVENTLINE)
			break;
1249

1250 1251 1252 1253 1254 1255 1256 1257 1258 1259 1260 1261 1262 1263 1264 1265 1266 1267 1268 1269 1270 1271 1272 1273 1274 1275 1276 1277
		writel((D40_SUSPEND_REQ_EVENTLINE << D40_EVENTLINE_POS(event))
		       | ~D40_EVENTLINE_MASK(event), addr);

		for (tries = 0 ; tries < D40_SUSPEND_MAX_IT; tries++) {

			status = (readl(addr) & D40_EVENTLINE_MASK(event)) >>
				  D40_EVENTLINE_POS(event);

			cpu_relax();
			/*
			 * Reduce the number of bus accesses while
			 * waiting for the DMA to suspend.
			 */
			udelay(3);

			if (status == D40_DEACTIVATE_EVENTLINE)
				break;
		}

		if (tries == D40_SUSPEND_MAX_IT) {
			chan_err(d40c,
				"unable to stop the event_line chl %d (log: %d)"
				"status %x\n", d40c->phy_chan->num,
				 d40c->log_num, status);
		}
		break;

	case D40_ACTIVATE_EVENTLINE:
1278 1279 1280 1281 1282
	/*
	 * The hardware sometimes doesn't register the enable when src and dst
	 * event lines are active on the same logical channel.  Retry to ensure
	 * it does.  Usually only one retry is sufficient.
	 */
1283 1284 1285 1286 1287
		tries = 100;
		while (--tries) {
			writel((D40_ACTIVATE_EVENTLINE <<
				D40_EVENTLINE_POS(event)) |
				~D40_EVENTLINE_MASK(event), addr);
1288

1289 1290 1291
			if (readl(addr) & D40_EVENTLINE_MASK(event))
				break;
		}
1292

1293 1294 1295 1296 1297
		if (tries != 99)
			dev_dbg(chan2dev(d40c),
				"[%s] workaround enable S%cLNK (%d tries)\n",
				__func__, reg == D40_CHAN_REG_SSLNK ? 'S' : 'D',
				100 - tries);
1298

1299 1300
		WARN_ON(!tries);
		break;
1301

1302 1303 1304
	case D40_ROUND_EVENTLINE:
		BUG();
		break;
1305

1306 1307
	}
}
1308

1309 1310 1311
static void d40_config_set_event(struct d40_chan *d40c,
				 enum d40_events event_type)
{
1312 1313
	u32 event = D40_TYPE_TO_EVENT(d40c->dma_cfg.dev_type);

1314
	/* Enable event line connected to device (or memcpy) */
1315 1316
	if ((d40c->dma_cfg.dir == DMA_DEV_TO_MEM) ||
	    (d40c->dma_cfg.dir == DMA_DEV_TO_DEV))
1317
		__d40_config_set_event(d40c, event_type, event,
1318
				       D40_CHAN_REG_SSLNK);
1319

1320
	if (d40c->dma_cfg.dir !=  DMA_DEV_TO_MEM)
1321
		__d40_config_set_event(d40c, event_type, event,
1322
				       D40_CHAN_REG_SDLNK);
1323 1324
}

1325
static u32 d40_chan_has_events(struct d40_chan *d40c)
1326
{
1327
	void __iomem *chanbase = chan_base(d40c);
1328
	u32 val;
1329

1330 1331
	val = readl(chanbase + D40_CHAN_REG_SSLNK);
	val |= readl(chanbase + D40_CHAN_REG_SDLNK);
1332

1333
	return val;
1334 1335
}

1336 1337 1338 1339 1340 1341 1342 1343 1344 1345 1346 1347 1348 1349 1350 1351 1352 1353 1354 1355 1356 1357 1358 1359 1360 1361 1362 1363 1364 1365 1366 1367 1368 1369 1370 1371 1372 1373 1374 1375 1376 1377 1378 1379 1380 1381 1382 1383 1384 1385 1386 1387 1388 1389 1390 1391 1392 1393
static int
__d40_execute_command_log(struct d40_chan *d40c, enum d40_command command)
{
	unsigned long flags;
	int ret = 0;
	u32 active_status;
	void __iomem *active_reg;

	if (d40c->phy_chan->num % 2 == 0)
		active_reg = d40c->base->virtbase + D40_DREG_ACTIVE;
	else
		active_reg = d40c->base->virtbase + D40_DREG_ACTIVO;


	spin_lock_irqsave(&d40c->phy_chan->lock, flags);

	switch (command) {
	case D40_DMA_STOP:
	case D40_DMA_SUSPEND_REQ:

		active_status = (readl(active_reg) &
				 D40_CHAN_POS_MASK(d40c->phy_chan->num)) >>
				 D40_CHAN_POS(d40c->phy_chan->num);

		if (active_status == D40_DMA_RUN)
			d40_config_set_event(d40c, D40_SUSPEND_REQ_EVENTLINE);
		else
			d40_config_set_event(d40c, D40_DEACTIVATE_EVENTLINE);

		if (!d40_chan_has_events(d40c) && (command == D40_DMA_STOP))
			ret = __d40_execute_command_phy(d40c, command);

		break;

	case D40_DMA_RUN:

		d40_config_set_event(d40c, D40_ACTIVATE_EVENTLINE);
		ret = __d40_execute_command_phy(d40c, command);
		break;

	case D40_DMA_SUSPENDED:
		BUG();
		break;
	}

	spin_unlock_irqrestore(&d40c->phy_chan->lock, flags);
	return ret;
}

static int d40_channel_execute_command(struct d40_chan *d40c,
				       enum d40_command command)
{
	if (chan_is_logical(d40c))
		return __d40_execute_command_log(d40c, command);
	else
		return __d40_execute_command_phy(d40c, command);
}

1394 1395 1396 1397 1398 1399 1400 1401 1402 1403 1404 1405 1406 1407 1408 1409 1410 1411 1412
static u32 d40_get_prmo(struct d40_chan *d40c)
{
	static const unsigned int phy_map[] = {
		[STEDMA40_PCHAN_BASIC_MODE]
			= D40_DREG_PRMO_PCHAN_BASIC,
		[STEDMA40_PCHAN_MODULO_MODE]
			= D40_DREG_PRMO_PCHAN_MODULO,
		[STEDMA40_PCHAN_DOUBLE_DST_MODE]
			= D40_DREG_PRMO_PCHAN_DOUBLE_DST,
	};
	static const unsigned int log_map[] = {
		[STEDMA40_LCHAN_SRC_PHY_DST_LOG]
			= D40_DREG_PRMO_LCHAN_SRC_PHY_DST_LOG,
		[STEDMA40_LCHAN_SRC_LOG_DST_PHY]
			= D40_DREG_PRMO_LCHAN_SRC_LOG_DST_PHY,
		[STEDMA40_LCHAN_SRC_LOG_DST_LOG]
			= D40_DREG_PRMO_LCHAN_SRC_LOG_DST_LOG,
	};

1413
	if (chan_is_physical(d40c))
1414 1415 1416 1417 1418
		return phy_map[d40c->dma_cfg.mode_opt];
	else
		return log_map[d40c->dma_cfg.mode_opt];
}

1419
static void d40_config_write(struct d40_chan *d40c)
1420 1421 1422 1423 1424 1425 1426
{
	u32 addr_base;
	u32 var;

	/* Odd addresses are even addresses + 4 */
	addr_base = (d40c->phy_chan->num % 2) * 4;
	/* Setup channel mode to logical or physical */
1427
	var = ((u32)(chan_is_logical(d40c)) + 1) <<
1428 1429 1430 1431
		D40_CHAN_POS(d40c->phy_chan->num);
	writel(var, d40c->base->virtbase + D40_DREG_PRMSE + addr_base);

	/* Setup operational mode option register */
1432
	var = d40_get_prmo(d40c) << D40_CHAN_POS(d40c->phy_chan->num);
1433 1434 1435

	writel(var, d40c->base->virtbase + D40_DREG_PRMOE + addr_base);

1436
	if (chan_is_logical(d40c)) {
1437 1438 1439 1440
		int lidx = (d40c->phy_chan->num << D40_SREG_ELEM_LOG_LIDX_POS)
			   & D40_SREG_ELEM_LOG_LIDX_MASK;
		void __iomem *chanbase = chan_base(d40c);

1441
		/* Set default config for CFG reg */
1442 1443
		writel(d40c->src_def_cfg, chanbase + D40_CHAN_REG_SSCFG);
		writel(d40c->dst_def_cfg, chanbase + D40_CHAN_REG_SDCFG);
1444

1445
		/* Set LIDX for lcla */
1446 1447
		writel(lidx, chanbase + D40_CHAN_REG_SSELT);
		writel(lidx, chanbase + D40_CHAN_REG_SDELT);
1448 1449 1450 1451

		/* Clear LNK which will be used by d40_chan_has_events() */
		writel(0, chanbase + D40_CHAN_REG_SSLNK);
		writel(0, chanbase + D40_CHAN_REG_SDLNK);
1452 1453 1454
	}
}

1455 1456 1457 1458
static u32 d40_residue(struct d40_chan *d40c)
{
	u32 num_elt;

1459
	if (chan_is_logical(d40c))
1460 1461
		num_elt = (readl(&d40c->lcpa->lcsp2) & D40_MEM_LCSP2_ECNT_MASK)
			>> D40_MEM_LCSP2_ECNT_POS;
1462 1463 1464 1465 1466 1467
	else {
		u32 val = readl(chan_base(d40c) + D40_CHAN_REG_SDELT);
		num_elt = (val & D40_SREG_ELEM_PHY_ECNT_MASK)
			  >> D40_SREG_ELEM_PHY_ECNT_POS;
	}

1468
	return num_elt * d40c->dma_cfg.dst_info.data_width;
1469 1470 1471 1472 1473 1474
}

static bool d40_tx_is_linked(struct d40_chan *d40c)
{
	bool is_link;

1475
	if (chan_is_logical(d40c))
1476 1477
		is_link = readl(&d40c->lcpa->lcsp3) &  D40_MEM_LCSP3_DLOS_MASK;
	else
1478 1479 1480
		is_link = readl(chan_base(d40c) + D40_CHAN_REG_SDLNK)
			  & D40_SREG_LNK_PHYS_LNK_MASK;

1481 1482 1483
	return is_link;
}

1484
static int d40_pause(struct d40_chan *d40c)
1485 1486 1487 1488
{
	int res = 0;
	unsigned long flags;

1489 1490 1491
	if (!d40c->busy)
		return 0;

1492
	pm_runtime_get_sync(d40c->base->dev);
1493 1494 1495
	spin_lock_irqsave(&d40c->lock, flags);

	res = d40_channel_execute_command(d40c, D40_DMA_SUSPEND_REQ);
1496

1497 1498
	pm_runtime_mark_last_busy(d40c->base->dev);
	pm_runtime_put_autosuspend(d40c->base->dev);
1499 1500 1501 1502
	spin_unlock_irqrestore(&d40c->lock, flags);
	return res;
}

1503
static int d40_resume(struct d40_chan *d40c)
1504 1505 1506 1507
{
	int res = 0;
	unsigned long flags;

1508 1509 1510
	if (!d40c->busy)
		return 0;

1511
	spin_lock_irqsave(&d40c->lock, flags);
1512
	pm_runtime_get_sync(d40c->base->dev);
1513 1514

	/* If bytes left to transfer or linked tx resume job */
1515
	if (d40_residue(d40c) || d40_tx_is_linked(d40c))
1516 1517
		res = d40_channel_execute_command(d40c, D40_DMA_RUN);

1518 1519
	pm_runtime_mark_last_busy(d40c->base->dev);
	pm_runtime_put_autosuspend(d40c->base->dev);
1520 1521 1522 1523
	spin_unlock_irqrestore(&d40c->lock, flags);
	return res;
}

1524 1525 1526 1527 1528 1529 1530
static dma_cookie_t d40_tx_submit(struct dma_async_tx_descriptor *tx)
{
	struct d40_chan *d40c = container_of(tx->chan,
					     struct d40_chan,
					     chan);
	struct d40_desc *d40d = container_of(tx, struct d40_desc, txd);
	unsigned long flags;
1531
	dma_cookie_t cookie;
1532 1533

	spin_lock_irqsave(&d40c->lock, flags);
1534
	cookie = dma_cookie_assign(tx);
1535 1536 1537
	d40_desc_queue(d40c, d40d);
	spin_unlock_irqrestore(&d40c->lock, flags);

1538
	return cookie;
1539 1540 1541 1542
}

static int d40_start(struct d40_chan *d40c)
{
1543
	return d40_channel_execute_command(d40c, D40_DMA_RUN);
1544 1545 1546 1547 1548 1549 1550 1551 1552 1553 1554
}

static struct d40_desc *d40_queue_start(struct d40_chan *d40c)
{
	struct d40_desc *d40d;
	int err;

	/* Start queued jobs, if any */
	d40d = d40_first_queued(d40c);

	if (d40d != NULL) {
1555
		if (!d40c->busy) {
1556
			d40c->busy = true;
1557 1558
			pm_runtime_get_sync(d40c->base->dev);
		}
1559 1560 1561 1562 1563 1564 1565

		/* Remove from queue */
		d40_desc_remove(d40d);

		/* Add to active queue */
		d40_desc_submit(d40c, d40d);

1566 1567
		/* Initiate DMA job */
		d40_desc_load(d40c, d40d);
1568

1569 1570
		/* Start dma job */
		err = d40_start(d40c);
1571

1572 1573
		if (err)
			return NULL;
1574 1575 1576 1577 1578 1579 1580 1581 1582 1583 1584 1585 1586 1587 1588 1589
	}

	return d40d;
}

/* called from interrupt context */
static void dma_tc_handle(struct d40_chan *d40c)
{
	struct d40_desc *d40d;

	/* Get first active entry from list */
	d40d = d40_first_active_get(d40c);

	if (d40d == NULL)
		return;

R
Rabin Vincent 已提交
1590 1591 1592 1593 1594 1595 1596 1597 1598 1599 1600 1601 1602
	if (d40d->cyclic) {
		/*
		 * If this was a paritially loaded list, we need to reloaded
		 * it, and only when the list is completed.  We need to check
		 * for done because the interrupt will hit for every link, and
		 * not just the last one.
		 */
		if (d40d->lli_current < d40d->lli_len
		    && !d40_tx_is_linked(d40c)
		    && !d40_residue(d40c)) {
			d40_lcla_free_all(d40c, d40d);
			d40_desc_load(d40c, d40d);
			(void) d40_start(d40c);
1603

R
Rabin Vincent 已提交
1604 1605 1606 1607 1608
			if (d40d->lli_current == d40d->lli_len)
				d40d->lli_current = 0;
		}
	} else {
		d40_lcla_free_all(d40c, d40d);
1609

R
Rabin Vincent 已提交
1610 1611 1612 1613 1614 1615 1616 1617 1618
		if (d40d->lli_current < d40d->lli_len) {
			d40_desc_load(d40c, d40d);
			/* Start dma job */
			(void) d40_start(d40c);
			return;
		}

		if (d40_queue_start(d40c) == NULL)
			d40c->busy = false;
1619 1620
		pm_runtime_mark_last_busy(d40c->base->dev);
		pm_runtime_put_autosuspend(d40c->base->dev);
1621

1622 1623 1624
		d40_desc_remove(d40d);
		d40_desc_done(d40c, d40d);
	}
1625

1626 1627 1628 1629 1630 1631 1632 1633
	d40c->pending_tx++;
	tasklet_schedule(&d40c->tasklet);

}

static void dma_tasklet(unsigned long data)
{
	struct d40_chan *d40c = (struct d40_chan *) data;
1634
	struct d40_desc *d40d;
1635 1636 1637 1638 1639 1640
	unsigned long flags;
	dma_async_tx_callback callback;
	void *callback_param;

	spin_lock_irqsave(&d40c->lock, flags);

1641 1642 1643 1644 1645 1646 1647 1648
	/* Get first entry from the done list */
	d40d = d40_first_done(d40c);
	if (d40d == NULL) {
		/* Check if we have reached here for cyclic job */
		d40d = d40_first_active_get(d40c);
		if (d40d == NULL || !d40d->cyclic)
			goto err;
	}
1649

R
Rabin Vincent 已提交
1650
	if (!d40d->cyclic)
1651
		dma_cookie_complete(&d40d->txd);
1652 1653 1654 1655 1656 1657 1658 1659 1660 1661 1662

	/*
	 * If terminating a channel pending_tx is set to zero.
	 * This prevents any finished active jobs to return to the client.
	 */
	if (d40c->pending_tx == 0) {
		spin_unlock_irqrestore(&d40c->lock, flags);
		return;
	}

	/* Callback to client */
1663 1664 1665
	callback = d40d->txd.callback;
	callback_param = d40d->txd.callback_param;

R
Rabin Vincent 已提交
1666 1667
	if (!d40d->cyclic) {
		if (async_tx_test_ack(&d40d->txd)) {
1668
			d40_desc_remove(d40d);
R
Rabin Vincent 已提交
1669
			d40_desc_free(d40c, d40d);
1670 1671 1672 1673 1674
		} else if (!d40d->is_in_client_list) {
			d40_desc_remove(d40d);
			d40_lcla_free_all(d40c, d40d);
			list_add_tail(&d40d->node, &d40c->client);
			d40d->is_in_client_list = true;
1675 1676 1677 1678 1679 1680 1681 1682 1683 1684
		}
	}

	d40c->pending_tx--;

	if (d40c->pending_tx)
		tasklet_schedule(&d40c->tasklet);

	spin_unlock_irqrestore(&d40c->lock, flags);

1685
	if (callback && (d40d->txd.flags & DMA_PREP_INTERRUPT))
1686 1687 1688 1689
		callback(callback_param);

	return;

1690 1691
err:
	/* Rescue manouver if receiving double interrupts */
1692 1693 1694 1695 1696 1697 1698 1699 1700 1701 1702 1703 1704 1705
	if (d40c->pending_tx > 0)
		d40c->pending_tx--;
	spin_unlock_irqrestore(&d40c->lock, flags);
}

static irqreturn_t d40_handle_interrupt(int irq, void *data)
{
	int i;
	u32 idx;
	u32 row;
	long chan = -1;
	struct d40_chan *d40c;
	unsigned long flags;
	struct d40_base *base = data;
1706 1707 1708
	u32 regs[base->gen_dmac.il_size];
	struct d40_interrupt_lookup *il = base->gen_dmac.il;
	u32 il_size = base->gen_dmac.il_size;
1709 1710 1711 1712

	spin_lock_irqsave(&base->interrupt_lock, flags);

	/* Read interrupt status of both logical and physical channels */
1713
	for (i = 0; i < il_size; i++)
1714 1715 1716 1717 1718
		regs[i] = readl(base->virtbase + il[i].src);

	for (;;) {

		chan = find_next_bit((unsigned long *)regs,
1719
				     BITS_PER_LONG * il_size, chan + 1);
1720 1721

		/* No more set bits found? */
1722
		if (chan == BITS_PER_LONG * il_size)
1723 1724 1725 1726 1727 1728 1729 1730 1731
			break;

		row = chan / BITS_PER_LONG;
		idx = chan & (BITS_PER_LONG - 1);

		if (il[row].offset == D40_PHY_CHAN)
			d40c = base->lookup_phy_chans[idx];
		else
			d40c = base->lookup_log_chans[il[row].offset + idx];
1732 1733 1734 1735 1736 1737 1738 1739 1740 1741

		if (!d40c) {
			/*
			 * No error because this can happen if something else
			 * in the system is using the channel.
			 */
			continue;
		}

		/* ACK interrupt */
1742
		writel(BIT(idx), base->virtbase + il[row].clr);
1743

1744 1745 1746 1747 1748
		spin_lock(&d40c->lock);

		if (!il[row].is_error)
			dma_tc_handle(d40c);
		else
1749 1750
			d40_err(base->dev, "IRQ chan: %ld offset %d idx %d\n",
				chan, il[row].offset, idx);
1751 1752 1753 1754 1755 1756 1757 1758 1759 1760 1761 1762 1763

		spin_unlock(&d40c->lock);
	}

	spin_unlock_irqrestore(&base->interrupt_lock, flags);

	return IRQ_HANDLED;
}

static int d40_validate_conf(struct d40_chan *d40c,
			     struct stedma40_chan_cfg *conf)
{
	int res = 0;
1764
	bool is_log = conf->mode == STEDMA40_MODE_LOGICAL;
1765

1766
	if (!conf->dir) {
1767
		chan_err(d40c, "Invalid direction.\n");
1768 1769 1770
		res = -EINVAL;
	}

1771 1772 1773 1774
	if ((is_log && conf->dev_type > d40c->base->num_log_chans)  ||
	    (!is_log && conf->dev_type > d40c->base->num_phy_chans) ||
	    (conf->dev_type < 0)) {
		chan_err(d40c, "Invalid device type (%d)\n", conf->dev_type);
1775 1776 1777
		res = -EINVAL;
	}

1778
	if (conf->dir == DMA_DEV_TO_DEV) {
1779 1780 1781 1782
		/*
		 * DMAC HW supports it. Will be added to this driver,
		 * in case any dma client requires it.
		 */
1783
		chan_err(d40c, "periph to periph not supported\n");
1784 1785 1786
		res = -EINVAL;
	}

1787
	if (d40_psize_2_burst_size(is_log, conf->src_info.psize) *
1788
	    conf->src_info.data_width !=
1789
	    d40_psize_2_burst_size(is_log, conf->dst_info.psize) *
1790
	    conf->dst_info.data_width) {
1791 1792 1793 1794 1795
		/*
		 * The DMAC hardware only supports
		 * src (burst x width) == dst (burst x width)
		 */

1796
		chan_err(d40c, "src (burst x width) != dst (burst x width)\n");
1797 1798 1799
		res = -EINVAL;
	}

1800 1801 1802
	return res;
}

1803 1804 1805
static bool d40_alloc_mask_set(struct d40_phy_res *phy,
			       bool is_src, int log_event_line, bool is_log,
			       bool *first_user)
1806 1807 1808
{
	unsigned long flags;
	spin_lock_irqsave(&phy->lock, flags);
1809 1810 1811 1812

	*first_user = ((phy->allocated_src | phy->allocated_dst)
			== D40_ALLOC_FREE);

1813
	if (!is_log) {
1814 1815 1816 1817 1818 1819 1820 1821 1822 1823 1824 1825 1826 1827 1828 1829 1830 1831
		/* Physical interrupts are masked per physical full channel */
		if (phy->allocated_src == D40_ALLOC_FREE &&
		    phy->allocated_dst == D40_ALLOC_FREE) {
			phy->allocated_dst = D40_ALLOC_PHY;
			phy->allocated_src = D40_ALLOC_PHY;
			goto found;
		} else
			goto not_found;
	}

	/* Logical channel */
	if (is_src) {
		if (phy->allocated_src == D40_ALLOC_PHY)
			goto not_found;

		if (phy->allocated_src == D40_ALLOC_FREE)
			phy->allocated_src = D40_ALLOC_LOG_FREE;

1832 1833
		if (!(phy->allocated_src & BIT(log_event_line))) {
			phy->allocated_src |= BIT(log_event_line);
1834 1835 1836 1837 1838 1839 1840 1841 1842 1843
			goto found;
		} else
			goto not_found;
	} else {
		if (phy->allocated_dst == D40_ALLOC_PHY)
			goto not_found;

		if (phy->allocated_dst == D40_ALLOC_FREE)
			phy->allocated_dst = D40_ALLOC_LOG_FREE;

1844 1845
		if (!(phy->allocated_dst & BIT(log_event_line))) {
			phy->allocated_dst |= BIT(log_event_line);
1846 1847 1848 1849 1850 1851 1852 1853 1854 1855 1856 1857 1858 1859 1860 1861 1862 1863 1864 1865 1866 1867 1868 1869 1870 1871 1872 1873 1874
			goto found;
		} else
			goto not_found;
	}

not_found:
	spin_unlock_irqrestore(&phy->lock, flags);
	return false;
found:
	spin_unlock_irqrestore(&phy->lock, flags);
	return true;
}

static bool d40_alloc_mask_free(struct d40_phy_res *phy, bool is_src,
			       int log_event_line)
{
	unsigned long flags;
	bool is_free = false;

	spin_lock_irqsave(&phy->lock, flags);
	if (!log_event_line) {
		phy->allocated_dst = D40_ALLOC_FREE;
		phy->allocated_src = D40_ALLOC_FREE;
		is_free = true;
		goto out;
	}

	/* Logical channel */
	if (is_src) {
1875
		phy->allocated_src &= ~BIT(log_event_line);
1876 1877 1878
		if (phy->allocated_src == D40_ALLOC_LOG_FREE)
			phy->allocated_src = D40_ALLOC_FREE;
	} else {
1879
		phy->allocated_dst &= ~BIT(log_event_line);
1880 1881 1882 1883 1884 1885 1886 1887 1888 1889 1890 1891 1892
		if (phy->allocated_dst == D40_ALLOC_LOG_FREE)
			phy->allocated_dst = D40_ALLOC_FREE;
	}

	is_free = ((phy->allocated_src | phy->allocated_dst) ==
		   D40_ALLOC_FREE);

out:
	spin_unlock_irqrestore(&phy->lock, flags);

	return is_free;
}

1893
static int d40_allocate_channel(struct d40_chan *d40c, bool *first_phy_user)
1894
{
1895
	int dev_type = d40c->dma_cfg.dev_type;
1896 1897 1898 1899 1900 1901
	int event_group;
	int event_line;
	struct d40_phy_res *phys;
	int i;
	int j;
	int log_num;
1902
	int num_phy_chans;
1903
	bool is_src;
1904
	bool is_log = d40c->dma_cfg.mode == STEDMA40_MODE_LOGICAL;
1905 1906

	phys = d40c->base->phy_res;
1907
	num_phy_chans = d40c->base->num_phy_chans;
1908

1909
	if (d40c->dma_cfg.dir == DMA_DEV_TO_MEM) {
1910 1911
		log_num = 2 * dev_type;
		is_src = true;
1912 1913
	} else if (d40c->dma_cfg.dir == DMA_MEM_TO_DEV ||
		   d40c->dma_cfg.dir == DMA_MEM_TO_MEM) {
1914 1915 1916 1917 1918 1919 1920 1921 1922 1923
		/* dst event lines are used for logical memcpy */
		log_num = 2 * dev_type + 1;
		is_src = false;
	} else
		return -EINVAL;

	event_group = D40_TYPE_TO_GROUP(dev_type);
	event_line = D40_TYPE_TO_EVENT(dev_type);

	if (!is_log) {
1924
		if (d40c->dma_cfg.dir == DMA_MEM_TO_MEM) {
1925
			/* Find physical half channel */
1926 1927
			if (d40c->dma_cfg.use_fixed_channel) {
				i = d40c->dma_cfg.phy_channel;
1928
				if (d40_alloc_mask_set(&phys[i], is_src,
1929 1930
						       0, is_log,
						       first_phy_user))
1931
					goto found_phy;
1932 1933 1934 1935 1936 1937 1938
			} else {
				for (i = 0; i < num_phy_chans; i++) {
					if (d40_alloc_mask_set(&phys[i], is_src,
						       0, is_log,
						       first_phy_user))
						goto found_phy;
				}
1939 1940 1941 1942 1943
			}
		} else
			for (j = 0; j < d40c->base->num_phy_chans; j += 8) {
				int phy_num = j  + event_group * 2;
				for (i = phy_num; i < phy_num + 2; i++) {
1944 1945 1946
					if (d40_alloc_mask_set(&phys[i],
							       is_src,
							       0,
1947 1948
							       is_log,
							       first_phy_user))
1949 1950 1951 1952 1953 1954 1955 1956 1957 1958 1959 1960 1961 1962 1963
						goto found_phy;
				}
			}
		return -EINVAL;
found_phy:
		d40c->phy_chan = &phys[i];
		d40c->log_num = D40_PHY_CHAN;
		goto out;
	}
	if (dev_type == -1)
		return -EINVAL;

	/* Find logical channel */
	for (j = 0; j < d40c->base->num_phy_chans; j += 8) {
		int phy_num = j + event_group * 2;
1964 1965 1966 1967 1968 1969 1970 1971 1972 1973 1974 1975 1976 1977 1978 1979 1980 1981 1982

		if (d40c->dma_cfg.use_fixed_channel) {
			i = d40c->dma_cfg.phy_channel;

			if ((i != phy_num) && (i != phy_num + 1)) {
				dev_err(chan2dev(d40c),
					"invalid fixed phy channel %d\n", i);
				return -EINVAL;
			}

			if (d40_alloc_mask_set(&phys[i], is_src, event_line,
					       is_log, first_phy_user))
				goto found_log;

			dev_err(chan2dev(d40c),
				"could not allocate fixed phy channel %d\n", i);
			return -EINVAL;
		}

1983 1984 1985 1986 1987 1988 1989 1990
		/*
		 * Spread logical channels across all available physical rather
		 * than pack every logical channel at the first available phy
		 * channels.
		 */
		if (is_src) {
			for (i = phy_num; i < phy_num + 2; i++) {
				if (d40_alloc_mask_set(&phys[i], is_src,
1991 1992
						       event_line, is_log,
						       first_phy_user))
1993 1994 1995 1996 1997
					goto found_log;
			}
		} else {
			for (i = phy_num + 1; i >= phy_num; i--) {
				if (d40_alloc_mask_set(&phys[i], is_src,
1998 1999
						       event_line, is_log,
						       first_phy_user))
2000 2001 2002 2003 2004 2005 2006 2007 2008 2009 2010 2011 2012 2013 2014 2015 2016 2017 2018 2019 2020 2021 2022 2023 2024
					goto found_log;
			}
		}
	}
	return -EINVAL;

found_log:
	d40c->phy_chan = &phys[i];
	d40c->log_num = log_num;
out:

	if (is_log)
		d40c->base->lookup_log_chans[d40c->log_num] = d40c;
	else
		d40c->base->lookup_phy_chans[d40c->phy_chan->num] = d40c;

	return 0;

}

static int d40_config_memcpy(struct d40_chan *d40c)
{
	dma_cap_mask_t cap = d40c->chan.device->cap_mask;

	if (dma_has_cap(DMA_MEMCPY, cap) && !dma_has_cap(DMA_SLAVE, cap)) {
2025
		d40c->dma_cfg = dma40_memcpy_conf_log;
2026
		d40c->dma_cfg.dev_type = dma40_memcpy_channels[d40c->chan.chan_id];
2027

2028 2029 2030
		d40_log_cfg(&d40c->dma_cfg,
			    &d40c->log_def.lcsp1, &d40c->log_def.lcsp3);

2031 2032
	} else if (dma_has_cap(DMA_MEMCPY, cap) &&
		   dma_has_cap(DMA_SLAVE, cap)) {
2033
		d40c->dma_cfg = dma40_memcpy_conf_phy;
2034 2035 2036 2037 2038 2039 2040 2041

		/* Generate interrrupt at end of transfer or relink. */
		d40c->dst_def_cfg |= BIT(D40_SREG_CFG_TIM_POS);

		/* Generate interrupt on error. */
		d40c->src_def_cfg |= BIT(D40_SREG_CFG_EIM_POS);
		d40c->dst_def_cfg |= BIT(D40_SREG_CFG_EIM_POS);

2042
	} else {
2043
		chan_err(d40c, "No memcpy\n");
2044 2045 2046 2047 2048 2049 2050 2051 2052 2053
		return -EINVAL;
	}

	return 0;
}

static int d40_free_dma(struct d40_chan *d40c)
{

	int res = 0;
2054
	u32 event = D40_TYPE_TO_EVENT(d40c->dma_cfg.dev_type);
2055 2056 2057 2058 2059 2060 2061
	struct d40_phy_res *phy = d40c->phy_chan;
	bool is_src;

	/* Terminate all queued and active transfers */
	d40_term_all(d40c);

	if (phy == NULL) {
2062
		chan_err(d40c, "phy == null\n");
2063 2064 2065 2066 2067
		return -EINVAL;
	}

	if (phy->allocated_src == D40_ALLOC_FREE &&
	    phy->allocated_dst == D40_ALLOC_FREE) {
2068
		chan_err(d40c, "channel already free\n");
2069 2070 2071
		return -EINVAL;
	}

2072 2073
	if (d40c->dma_cfg.dir == DMA_MEM_TO_DEV ||
	    d40c->dma_cfg.dir == DMA_MEM_TO_MEM)
2074
		is_src = false;
2075
	else if (d40c->dma_cfg.dir == DMA_DEV_TO_MEM)
2076
		is_src = true;
2077
	else {
2078
		chan_err(d40c, "Unknown direction\n");
2079 2080 2081
		return -EINVAL;
	}

2082
	pm_runtime_get_sync(d40c->base->dev);
2083
	res = d40_channel_execute_command(d40c, D40_DMA_STOP);
2084
	if (res) {
2085
		chan_err(d40c, "stop failed\n");
2086
		goto out;
2087 2088
	}

2089
	d40_alloc_mask_free(phy, is_src, chan_is_logical(d40c) ? event : 0);
2090

2091
	if (chan_is_logical(d40c))
2092
		d40c->base->lookup_log_chans[d40c->log_num] = NULL;
2093 2094
	else
		d40c->base->lookup_phy_chans[phy->num] = NULL;
2095 2096 2097 2098 2099 2100 2101

	if (d40c->busy) {
		pm_runtime_mark_last_busy(d40c->base->dev);
		pm_runtime_put_autosuspend(d40c->base->dev);
	}

	d40c->busy = false;
2102
	d40c->phy_chan = NULL;
2103
	d40c->configured = false;
2104
out:
2105

2106 2107 2108
	pm_runtime_mark_last_busy(d40c->base->dev);
	pm_runtime_put_autosuspend(d40c->base->dev);
	return res;
2109 2110
}

2111 2112
static bool d40_is_paused(struct d40_chan *d40c)
{
2113
	void __iomem *chanbase = chan_base(d40c);
2114 2115 2116 2117
	bool is_paused = false;
	unsigned long flags;
	void __iomem *active_reg;
	u32 status;
2118
	u32 event = D40_TYPE_TO_EVENT(d40c->dma_cfg.dev_type);
2119 2120 2121

	spin_lock_irqsave(&d40c->lock, flags);

2122
	if (chan_is_physical(d40c)) {
2123 2124 2125 2126 2127 2128 2129 2130 2131 2132 2133 2134 2135 2136
		if (d40c->phy_chan->num % 2 == 0)
			active_reg = d40c->base->virtbase + D40_DREG_ACTIVE;
		else
			active_reg = d40c->base->virtbase + D40_DREG_ACTIVO;

		status = (readl(active_reg) &
			  D40_CHAN_POS_MASK(d40c->phy_chan->num)) >>
			D40_CHAN_POS(d40c->phy_chan->num);
		if (status == D40_DMA_SUSPENDED || status == D40_DMA_STOP)
			is_paused = true;

		goto _exit;
	}

2137 2138
	if (d40c->dma_cfg.dir == DMA_MEM_TO_DEV ||
	    d40c->dma_cfg.dir == DMA_MEM_TO_MEM) {
2139
		status = readl(chanbase + D40_CHAN_REG_SDLNK);
2140
	} else if (d40c->dma_cfg.dir == DMA_DEV_TO_MEM) {
2141
		status = readl(chanbase + D40_CHAN_REG_SSLNK);
2142
	} else {
2143
		chan_err(d40c, "Unknown direction\n");
2144 2145
		goto _exit;
	}
2146

2147 2148 2149 2150 2151 2152 2153 2154 2155 2156 2157
	status = (status & D40_EVENTLINE_MASK(event)) >>
		D40_EVENTLINE_POS(event);

	if (status != D40_DMA_RUN)
		is_paused = true;
_exit:
	spin_unlock_irqrestore(&d40c->lock, flags);
	return is_paused;

}

2158 2159 2160 2161 2162 2163 2164 2165 2166 2167 2168 2169 2170 2171
static u32 stedma40_residue(struct dma_chan *chan)
{
	struct d40_chan *d40c =
		container_of(chan, struct d40_chan, chan);
	u32 bytes_left;
	unsigned long flags;

	spin_lock_irqsave(&d40c->lock, flags);
	bytes_left = d40_residue(d40c);
	spin_unlock_irqrestore(&d40c->lock, flags);

	return bytes_left;
}

2172 2173 2174
static int
d40_prep_sg_log(struct d40_chan *chan, struct d40_desc *desc,
		struct scatterlist *sg_src, struct scatterlist *sg_dst,
R
Rabin Vincent 已提交
2175 2176
		unsigned int sg_len, dma_addr_t src_dev_addr,
		dma_addr_t dst_dev_addr)
2177 2178 2179 2180
{
	struct stedma40_chan_cfg *cfg = &chan->dma_cfg;
	struct stedma40_half_channel_info *src_info = &cfg->src_info;
	struct stedma40_half_channel_info *dst_info = &cfg->dst_info;
2181
	int ret;
2182

2183 2184 2185 2186 2187 2188 2189 2190 2191 2192 2193 2194 2195 2196 2197
	ret = d40_log_sg_to_lli(sg_src, sg_len,
				src_dev_addr,
				desc->lli_log.src,
				chan->log_def.lcsp1,
				src_info->data_width,
				dst_info->data_width);

	ret = d40_log_sg_to_lli(sg_dst, sg_len,
				dst_dev_addr,
				desc->lli_log.dst,
				chan->log_def.lcsp3,
				dst_info->data_width,
				src_info->data_width);

	return ret < 0 ? ret : 0;
2198 2199 2200 2201 2202
}

static int
d40_prep_sg_phy(struct d40_chan *chan, struct d40_desc *desc,
		struct scatterlist *sg_src, struct scatterlist *sg_dst,
R
Rabin Vincent 已提交
2203 2204
		unsigned int sg_len, dma_addr_t src_dev_addr,
		dma_addr_t dst_dev_addr)
2205 2206 2207 2208
{
	struct stedma40_chan_cfg *cfg = &chan->dma_cfg;
	struct stedma40_half_channel_info *src_info = &cfg->src_info;
	struct stedma40_half_channel_info *dst_info = &cfg->dst_info;
R
Rabin Vincent 已提交
2209
	unsigned long flags = 0;
2210 2211
	int ret;

R
Rabin Vincent 已提交
2212 2213 2214
	if (desc->cyclic)
		flags |= LLI_CYCLIC | LLI_TERM_INT;

2215 2216 2217 2218
	ret = d40_phy_sg_to_lli(sg_src, sg_len, src_dev_addr,
				desc->lli_phy.src,
				virt_to_phys(desc->lli_phy.src),
				chan->src_def_cfg,
R
Rabin Vincent 已提交
2219
				src_info, dst_info, flags);
2220 2221 2222 2223 2224

	ret = d40_phy_sg_to_lli(sg_dst, sg_len, dst_dev_addr,
				desc->lli_phy.dst,
				virt_to_phys(desc->lli_phy.dst),
				chan->dst_def_cfg,
R
Rabin Vincent 已提交
2225
				dst_info, src_info, flags);
2226 2227 2228 2229 2230 2231 2232

	dma_sync_single_for_device(chan->base->dev, desc->lli_pool.dma_addr,
				   desc->lli_pool.size, DMA_TO_DEVICE);

	return ret < 0 ? ret : 0;
}

2233 2234 2235 2236 2237 2238
static struct d40_desc *
d40_prep_desc(struct d40_chan *chan, struct scatterlist *sg,
	      unsigned int sg_len, unsigned long dma_flags)
{
	struct stedma40_chan_cfg *cfg = &chan->dma_cfg;
	struct d40_desc *desc;
2239
	int ret;
2240 2241 2242 2243 2244 2245 2246 2247 2248

	desc = d40_desc_get(chan);
	if (!desc)
		return NULL;

	desc->lli_len = d40_sg_2_dmalen(sg, sg_len, cfg->src_info.data_width,
					cfg->dst_info.data_width);
	if (desc->lli_len < 0) {
		chan_err(chan, "Unaligned size\n");
2249 2250
		goto err;
	}
2251

2252 2253 2254 2255
	ret = d40_pool_lli_alloc(chan, desc, desc->lli_len);
	if (ret < 0) {
		chan_err(chan, "Could not allocate lli\n");
		goto err;
2256 2257 2258 2259 2260 2261 2262 2263 2264
	}

	desc->lli_current = 0;
	desc->txd.flags = dma_flags;
	desc->txd.tx_submit = d40_tx_submit;

	dma_async_tx_descriptor_init(&desc->txd, &chan->chan);

	return desc;
2265 2266 2267 2268

err:
	d40_desc_free(chan, desc);
	return NULL;
2269 2270
}

2271 2272 2273
static struct dma_async_tx_descriptor *
d40_prep_sg(struct dma_chan *dchan, struct scatterlist *sg_src,
	    struct scatterlist *sg_dst, unsigned int sg_len,
2274
	    enum dma_transfer_direction direction, unsigned long dma_flags)
2275 2276
{
	struct d40_chan *chan = container_of(dchan, struct d40_chan, chan);
R
Rabin Vincent 已提交
2277 2278
	dma_addr_t src_dev_addr = 0;
	dma_addr_t dst_dev_addr = 0;
2279
	struct d40_desc *desc;
2280
	unsigned long flags;
2281
	int ret;
2282

2283 2284 2285
	if (!chan->phy_chan) {
		chan_err(chan, "Cannot prepare unallocated channel\n");
		return NULL;
2286 2287
	}

2288
	spin_lock_irqsave(&chan->lock, flags);
2289

2290 2291
	desc = d40_prep_desc(chan, sg_src, sg_len, dma_flags);
	if (desc == NULL)
2292 2293
		goto err;

R
Rabin Vincent 已提交
2294 2295 2296
	if (sg_next(&sg_src[sg_len - 1]) == sg_src)
		desc->cyclic = true;

2297 2298 2299 2300
	if (direction == DMA_DEV_TO_MEM)
		src_dev_addr = chan->runtime_addr;
	else if (direction == DMA_MEM_TO_DEV)
		dst_dev_addr = chan->runtime_addr;
2301 2302 2303

	if (chan_is_logical(chan))
		ret = d40_prep_sg_log(chan, desc, sg_src, sg_dst,
R
Rabin Vincent 已提交
2304
				      sg_len, src_dev_addr, dst_dev_addr);
2305 2306
	else
		ret = d40_prep_sg_phy(chan, desc, sg_src, sg_dst,
R
Rabin Vincent 已提交
2307
				      sg_len, src_dev_addr, dst_dev_addr);
2308 2309 2310 2311 2312

	if (ret) {
		chan_err(chan, "Failed to prepare %s sg job: %d\n",
			 chan_is_logical(chan) ? "log" : "phy", ret);
		goto err;
2313 2314
	}

2315 2316 2317 2318 2319 2320
	/*
	 * add descriptor to the prepare queue in order to be able
	 * to free them later in terminate_all
	 */
	list_add_tail(&desc->node, &chan->prepare_queue);

2321 2322 2323
	spin_unlock_irqrestore(&chan->lock, flags);

	return &desc->txd;
2324 2325

err:
2326 2327 2328
	if (desc)
		d40_desc_free(chan, desc);
	spin_unlock_irqrestore(&chan->lock, flags);
2329 2330 2331 2332 2333 2334 2335 2336 2337 2338 2339 2340 2341 2342 2343 2344 2345
	return NULL;
}

bool stedma40_filter(struct dma_chan *chan, void *data)
{
	struct stedma40_chan_cfg *info = data;
	struct d40_chan *d40c =
		container_of(chan, struct d40_chan, chan);
	int err;

	if (data) {
		err = d40_validate_conf(d40c, info);
		if (!err)
			d40c->dma_cfg = *info;
	} else
		err = d40_config_memcpy(d40c);

2346 2347 2348
	if (!err)
		d40c->configured = true;

2349 2350 2351 2352
	return err == 0;
}
EXPORT_SYMBOL(stedma40_filter);

2353 2354 2355 2356
static void __d40_set_prio_rt(struct d40_chan *d40c, int dev_type, bool src)
{
	bool realtime = d40c->dma_cfg.realtime;
	bool highprio = d40c->dma_cfg.high_priority;
2357
	u32 rtreg;
2358 2359
	u32 event = D40_TYPE_TO_EVENT(dev_type);
	u32 group = D40_TYPE_TO_GROUP(dev_type);
2360
	u32 bit = BIT(event);
2361
	u32 prioreg;
2362
	struct d40_gen_dmac *dmac = &d40c->base->gen_dmac;
2363

2364
	rtreg = realtime ? dmac->realtime_en : dmac->realtime_clear;
2365 2366 2367 2368 2369 2370 2371 2372 2373 2374 2375
	/*
	 * Due to a hardware bug, in some cases a logical channel triggered by
	 * a high priority destination event line can generate extra packet
	 * transactions.
	 *
	 * The workaround is to not set the high priority level for the
	 * destination event lines that trigger logical channels.
	 */
	if (!src && chan_is_logical(d40c))
		highprio = false;

2376
	prioreg = highprio ? dmac->high_prio_en : dmac->high_prio_clear;
2377 2378 2379 2380 2381 2382 2383 2384 2385 2386 2387 2388 2389 2390

	/* Destination event lines are stored in the upper halfword */
	if (!src)
		bit <<= 16;

	writel(bit, d40c->base->virtbase + prioreg + group * 4);
	writel(bit, d40c->base->virtbase + rtreg + group * 4);
}

static void d40_set_prio_realtime(struct d40_chan *d40c)
{
	if (d40c->base->rev < 3)
		return;

2391 2392
	if ((d40c->dma_cfg.dir ==  DMA_DEV_TO_MEM) ||
	    (d40c->dma_cfg.dir == DMA_DEV_TO_DEV))
2393
		__d40_set_prio_rt(d40c, d40c->dma_cfg.dev_type, true);
2394

2395 2396
	if ((d40c->dma_cfg.dir ==  DMA_MEM_TO_DEV) ||
	    (d40c->dma_cfg.dir == DMA_DEV_TO_DEV))
2397
		__d40_set_prio_rt(d40c, d40c->dma_cfg.dev_type, false);
2398 2399
}

2400 2401 2402 2403 2404 2405 2406 2407 2408 2409 2410 2411 2412 2413 2414 2415 2416 2417 2418 2419 2420 2421 2422 2423 2424 2425 2426
#define D40_DT_FLAGS_MODE(flags)       ((flags >> 0) & 0x1)
#define D40_DT_FLAGS_DIR(flags)        ((flags >> 1) & 0x1)
#define D40_DT_FLAGS_BIG_ENDIAN(flags) ((flags >> 2) & 0x1)
#define D40_DT_FLAGS_FIXED_CHAN(flags) ((flags >> 3) & 0x1)

static struct dma_chan *d40_xlate(struct of_phandle_args *dma_spec,
				  struct of_dma *ofdma)
{
	struct stedma40_chan_cfg cfg;
	dma_cap_mask_t cap;
	u32 flags;

	memset(&cfg, 0, sizeof(struct stedma40_chan_cfg));

	dma_cap_zero(cap);
	dma_cap_set(DMA_SLAVE, cap);

	cfg.dev_type = dma_spec->args[0];
	flags = dma_spec->args[2];

	switch (D40_DT_FLAGS_MODE(flags)) {
	case 0: cfg.mode = STEDMA40_MODE_LOGICAL; break;
	case 1: cfg.mode = STEDMA40_MODE_PHYSICAL; break;
	}

	switch (D40_DT_FLAGS_DIR(flags)) {
	case 0:
2427
		cfg.dir = DMA_MEM_TO_DEV;
2428 2429 2430
		cfg.dst_info.big_endian = D40_DT_FLAGS_BIG_ENDIAN(flags);
		break;
	case 1:
2431
		cfg.dir = DMA_DEV_TO_MEM;
2432 2433 2434 2435 2436 2437 2438 2439 2440 2441 2442 2443
		cfg.src_info.big_endian = D40_DT_FLAGS_BIG_ENDIAN(flags);
		break;
	}

	if (D40_DT_FLAGS_FIXED_CHAN(flags)) {
		cfg.phy_channel = dma_spec->args[1];
		cfg.use_fixed_channel = true;
	}

	return dma_request_channel(cap, stedma40_filter, &cfg);
}

2444 2445 2446 2447 2448 2449 2450
/* DMA ENGINE functions */
static int d40_alloc_chan_resources(struct dma_chan *chan)
{
	int err;
	unsigned long flags;
	struct d40_chan *d40c =
		container_of(chan, struct d40_chan, chan);
2451
	bool is_free_phy;
2452 2453
	spin_lock_irqsave(&d40c->lock, flags);

2454
	dma_cookie_init(chan);
2455

2456 2457
	/* If no dma configuration is set use default configuration (memcpy) */
	if (!d40c->configured) {
2458
		err = d40_config_memcpy(d40c);
2459
		if (err) {
2460
			chan_err(d40c, "Failed to configure memcpy channel\n");
2461 2462
			goto fail;
		}
2463 2464
	}

2465
	err = d40_allocate_channel(d40c, &is_free_phy);
2466
	if (err) {
2467
		chan_err(d40c, "Failed to allocate channel\n");
2468
		d40c->configured = false;
2469
		goto fail;
2470 2471
	}

2472
	pm_runtime_get_sync(d40c->base->dev);
2473

2474 2475
	d40_set_prio_realtime(d40c);

2476
	if (chan_is_logical(d40c)) {
2477
		if (d40c->dma_cfg.dir == DMA_DEV_TO_MEM)
2478
			d40c->lcpa = d40c->base->lcpa_base +
2479
				d40c->dma_cfg.dev_type * D40_LCPA_CHAN_SIZE;
2480 2481
		else
			d40c->lcpa = d40c->base->lcpa_base +
2482
				d40c->dma_cfg.dev_type *
2483
				D40_LCPA_CHAN_SIZE + D40_LCPA_CHAN_DST_DELTA;
2484 2485 2486 2487

		/* Unmask the Global Interrupt Mask. */
		d40c->src_def_cfg |= BIT(D40_SREG_CFG_LOG_GIM_POS);
		d40c->dst_def_cfg |= BIT(D40_SREG_CFG_LOG_GIM_POS);
2488 2489
	}

2490 2491 2492 2493 2494 2495
	dev_dbg(chan2dev(d40c), "allocated %s channel (phy %d%s)\n",
		 chan_is_logical(d40c) ? "logical" : "physical",
		 d40c->phy_chan->num,
		 d40c->dma_cfg.use_fixed_channel ? ", fixed" : "");


2496 2497 2498 2499 2500
	/*
	 * Only write channel configuration to the DMA if the physical
	 * resource is free. In case of multiple logical channels
	 * on the same physical resource, only the first write is necessary.
	 */
2501 2502
	if (is_free_phy)
		d40_config_write(d40c);
2503
fail:
2504 2505
	pm_runtime_mark_last_busy(d40c->base->dev);
	pm_runtime_put_autosuspend(d40c->base->dev);
2506
	spin_unlock_irqrestore(&d40c->lock, flags);
2507
	return err;
2508 2509 2510 2511 2512 2513 2514 2515 2516
}

static void d40_free_chan_resources(struct dma_chan *chan)
{
	struct d40_chan *d40c =
		container_of(chan, struct d40_chan, chan);
	int err;
	unsigned long flags;

2517
	if (d40c->phy_chan == NULL) {
2518
		chan_err(d40c, "Cannot free unallocated channel\n");
2519 2520 2521
		return;
	}

2522 2523 2524 2525 2526
	spin_lock_irqsave(&d40c->lock, flags);

	err = d40_free_dma(d40c);

	if (err)
2527
		chan_err(d40c, "Failed to free channel\n");
2528 2529 2530 2531 2532 2533 2534
	spin_unlock_irqrestore(&d40c->lock, flags);
}

static struct dma_async_tx_descriptor *d40_prep_memcpy(struct dma_chan *chan,
						       dma_addr_t dst,
						       dma_addr_t src,
						       size_t size,
2535
						       unsigned long dma_flags)
2536
{
2537 2538
	struct scatterlist dst_sg;
	struct scatterlist src_sg;
2539

2540 2541
	sg_init_table(&dst_sg, 1);
	sg_init_table(&src_sg, 1);
2542

2543 2544
	sg_dma_address(&dst_sg) = dst;
	sg_dma_address(&src_sg) = src;
2545

2546 2547
	sg_dma_len(&dst_sg) = size;
	sg_dma_len(&src_sg) = size;
2548

2549
	return d40_prep_sg(chan, &src_sg, &dst_sg, 1, DMA_NONE, dma_flags);
2550 2551
}

2552
static struct dma_async_tx_descriptor *
2553 2554 2555 2556
d40_prep_memcpy_sg(struct dma_chan *chan,
		   struct scatterlist *dst_sg, unsigned int dst_nents,
		   struct scatterlist *src_sg, unsigned int src_nents,
		   unsigned long dma_flags)
2557 2558 2559 2560
{
	if (dst_nents != src_nents)
		return NULL;

2561
	return d40_prep_sg(chan, src_sg, dst_sg, src_nents, DMA_NONE, dma_flags);
2562 2563
}

2564 2565 2566 2567
static struct dma_async_tx_descriptor *
d40_prep_slave_sg(struct dma_chan *chan, struct scatterlist *sgl,
		  unsigned int sg_len, enum dma_transfer_direction direction,
		  unsigned long dma_flags, void *context)
2568
{
2569
	if (!is_slave_direction(direction))
2570 2571
		return NULL;

2572
	return d40_prep_sg(chan, sgl, sgl, sg_len, direction, dma_flags);
2573 2574
}

R
Rabin Vincent 已提交
2575 2576 2577
static struct dma_async_tx_descriptor *
dma40_prep_dma_cyclic(struct dma_chan *chan, dma_addr_t dma_addr,
		     size_t buf_len, size_t period_len,
2578 2579
		     enum dma_transfer_direction direction, unsigned long flags,
		     void *context)
R
Rabin Vincent 已提交
2580 2581 2582 2583 2584 2585
{
	unsigned int periods = buf_len / period_len;
	struct dma_async_tx_descriptor *txd;
	struct scatterlist *sg;
	int i;

2586
	sg = kcalloc(periods + 1, sizeof(struct scatterlist), GFP_NOWAIT);
R
Rabin Vincent 已提交
2587 2588 2589 2590 2591 2592 2593
	for (i = 0; i < periods; i++) {
		sg_dma_address(&sg[i]) = dma_addr;
		sg_dma_len(&sg[i]) = period_len;
		dma_addr += period_len;
	}

	sg[periods].offset = 0;
2594
	sg_dma_len(&sg[periods]) = 0;
R
Rabin Vincent 已提交
2595 2596 2597 2598 2599 2600 2601 2602 2603 2604 2605
	sg[periods].page_link =
		((unsigned long)sg | 0x01) & ~0x02;

	txd = d40_prep_sg(chan, sg, sg, periods, direction,
			  DMA_PREP_INTERRUPT);

	kfree(sg);

	return txd;
}

2606 2607 2608 2609 2610
static enum dma_status d40_tx_status(struct dma_chan *chan,
				     dma_cookie_t cookie,
				     struct dma_tx_state *txstate)
{
	struct d40_chan *d40c = container_of(chan, struct d40_chan, chan);
2611
	enum dma_status ret;
2612

2613
	if (d40c->phy_chan == NULL) {
2614
		chan_err(d40c, "Cannot read status of unallocated channel\n");
2615 2616 2617
		return -EINVAL;
	}

2618 2619 2620
	ret = dma_cookie_status(chan, cookie, txstate);
	if (ret != DMA_SUCCESS)
		dma_set_residue(txstate, stedma40_residue(chan));
2621

2622 2623
	if (d40_is_paused(d40c))
		ret = DMA_PAUSED;
2624 2625 2626 2627 2628 2629 2630 2631 2632

	return ret;
}

static void d40_issue_pending(struct dma_chan *chan)
{
	struct d40_chan *d40c = container_of(chan, struct d40_chan, chan);
	unsigned long flags;

2633
	if (d40c->phy_chan == NULL) {
2634
		chan_err(d40c, "Channel is not allocated!\n");
2635 2636 2637
		return;
	}

2638 2639
	spin_lock_irqsave(&d40c->lock, flags);

2640 2641 2642
	list_splice_tail_init(&d40c->pending_queue, &d40c->queue);

	/* Busy means that queued jobs are already being processed */
2643 2644 2645 2646 2647 2648
	if (!d40c->busy)
		(void) d40_queue_start(d40c);

	spin_unlock_irqrestore(&d40c->lock, flags);
}

2649 2650 2651 2652 2653 2654 2655 2656 2657 2658 2659 2660 2661 2662 2663 2664 2665 2666 2667 2668 2669 2670 2671 2672 2673
static void d40_terminate_all(struct dma_chan *chan)
{
	unsigned long flags;
	struct d40_chan *d40c = container_of(chan, struct d40_chan, chan);
	int ret;

	spin_lock_irqsave(&d40c->lock, flags);

	pm_runtime_get_sync(d40c->base->dev);
	ret = d40_channel_execute_command(d40c, D40_DMA_STOP);
	if (ret)
		chan_err(d40c, "Failed to stop channel\n");

	d40_term_all(d40c);
	pm_runtime_mark_last_busy(d40c->base->dev);
	pm_runtime_put_autosuspend(d40c->base->dev);
	if (d40c->busy) {
		pm_runtime_mark_last_busy(d40c->base->dev);
		pm_runtime_put_autosuspend(d40c->base->dev);
	}
	d40c->busy = false;

	spin_unlock_irqrestore(&d40c->lock, flags);
}

2674 2675 2676 2677 2678 2679 2680 2681 2682 2683 2684 2685 2686 2687 2688 2689 2690 2691 2692 2693 2694 2695 2696 2697 2698 2699 2700 2701 2702 2703 2704 2705 2706
static int
dma40_config_to_halfchannel(struct d40_chan *d40c,
			    struct stedma40_half_channel_info *info,
			    u32 maxburst)
{
	int psize;

	if (chan_is_logical(d40c)) {
		if (maxburst >= 16)
			psize = STEDMA40_PSIZE_LOG_16;
		else if (maxburst >= 8)
			psize = STEDMA40_PSIZE_LOG_8;
		else if (maxburst >= 4)
			psize = STEDMA40_PSIZE_LOG_4;
		else
			psize = STEDMA40_PSIZE_LOG_1;
	} else {
		if (maxburst >= 16)
			psize = STEDMA40_PSIZE_PHY_16;
		else if (maxburst >= 8)
			psize = STEDMA40_PSIZE_PHY_8;
		else if (maxburst >= 4)
			psize = STEDMA40_PSIZE_PHY_4;
		else
			psize = STEDMA40_PSIZE_PHY_1;
	}

	info->psize = psize;
	info->flow_ctrl = STEDMA40_NO_FLOW_CTRL;

	return 0;
}

2707
/* Runtime reconfiguration extension */
2708 2709
static int d40_set_runtime_config(struct dma_chan *chan,
				  struct dma_slave_config *config)
2710 2711 2712
{
	struct d40_chan *d40c = container_of(chan, struct d40_chan, chan);
	struct stedma40_chan_cfg *cfg = &d40c->dma_cfg;
2713
	enum dma_slave_buswidth src_addr_width, dst_addr_width;
2714
	dma_addr_t config_addr;
2715 2716 2717 2718 2719 2720 2721
	u32 src_maxburst, dst_maxburst;
	int ret;

	src_addr_width = config->src_addr_width;
	src_maxburst = config->src_maxburst;
	dst_addr_width = config->dst_addr_width;
	dst_maxburst = config->dst_maxburst;
2722

2723
	if (config->direction == DMA_DEV_TO_MEM) {
2724
		config_addr = config->src_addr;
2725

2726
		if (cfg->dir != DMA_DEV_TO_MEM)
2727 2728 2729 2730
			dev_dbg(d40c->base->dev,
				"channel was not configured for peripheral "
				"to memory transfer (%d) overriding\n",
				cfg->dir);
2731
		cfg->dir = DMA_DEV_TO_MEM;
2732

2733 2734 2735 2736 2737
		/* Configure the memory side */
		if (dst_addr_width == DMA_SLAVE_BUSWIDTH_UNDEFINED)
			dst_addr_width = src_addr_width;
		if (dst_maxburst == 0)
			dst_maxburst = src_maxburst;
2738

2739
	} else if (config->direction == DMA_MEM_TO_DEV) {
2740
		config_addr = config->dst_addr;
2741

2742
		if (cfg->dir != DMA_MEM_TO_DEV)
2743 2744 2745 2746
			dev_dbg(d40c->base->dev,
				"channel was not configured for memory "
				"to peripheral transfer (%d) overriding\n",
				cfg->dir);
2747
		cfg->dir = DMA_MEM_TO_DEV;
2748

2749 2750 2751 2752 2753
		/* Configure the memory side */
		if (src_addr_width == DMA_SLAVE_BUSWIDTH_UNDEFINED)
			src_addr_width = dst_addr_width;
		if (src_maxburst == 0)
			src_maxburst = dst_maxburst;
2754 2755 2756 2757
	} else {
		dev_err(d40c->base->dev,
			"unrecognized channel direction %d\n",
			config->direction);
2758
		return -EINVAL;
2759 2760
	}

2761 2762 2763 2764 2765
	if (config_addr <= 0) {
		dev_err(d40c->base->dev, "no address supplied\n");
		return -EINVAL;
	}

2766
	if (src_maxburst * src_addr_width != dst_maxburst * dst_addr_width) {
2767
		dev_err(d40c->base->dev,
2768 2769 2770 2771 2772 2773
			"src/dst width/maxburst mismatch: %d*%d != %d*%d\n",
			src_maxburst,
			src_addr_width,
			dst_maxburst,
			dst_addr_width);
		return -EINVAL;
2774 2775
	}

2776 2777 2778 2779 2780 2781 2782 2783
	if (src_maxburst > 16) {
		src_maxburst = 16;
		dst_maxburst = src_maxburst * src_addr_width / dst_addr_width;
	} else if (dst_maxburst > 16) {
		dst_maxburst = 16;
		src_maxburst = dst_maxburst * dst_addr_width / src_addr_width;
	}

2784 2785 2786 2787 2788 2789 2790 2791 2792 2793 2794 2795
	/* Only valid widths are; 1, 2, 4 and 8. */
	if (src_addr_width <= DMA_SLAVE_BUSWIDTH_UNDEFINED ||
	    src_addr_width >  DMA_SLAVE_BUSWIDTH_8_BYTES   ||
	    dst_addr_width <= DMA_SLAVE_BUSWIDTH_UNDEFINED ||
	    dst_addr_width >  DMA_SLAVE_BUSWIDTH_8_BYTES   ||
	    ((src_addr_width > 1) && (src_addr_width & 1)) ||
	    ((dst_addr_width > 1) && (dst_addr_width & 1)))
		return -EINVAL;

	cfg->src_info.data_width = src_addr_width;
	cfg->dst_info.data_width = dst_addr_width;

2796 2797 2798 2799
	ret = dma40_config_to_halfchannel(d40c, &cfg->src_info,
					  src_maxburst);
	if (ret)
		return ret;
2800

2801 2802 2803 2804
	ret = dma40_config_to_halfchannel(d40c, &cfg->dst_info,
					  dst_maxburst);
	if (ret)
		return ret;
2805

2806
	/* Fill in register values */
2807
	if (chan_is_logical(d40c))
2808 2809
		d40_log_cfg(cfg, &d40c->log_def.lcsp1, &d40c->log_def.lcsp3);
	else
2810
		d40_phy_cfg(cfg, &d40c->src_def_cfg, &d40c->dst_def_cfg);
2811

2812 2813 2814 2815
	/* These settings will take precedence later */
	d40c->runtime_addr = config_addr;
	d40c->runtime_direction = config->direction;
	dev_dbg(d40c->base->dev,
2816 2817
		"configured channel %s for %s, data width %d/%d, "
		"maxburst %d/%d elements, LE, no flow control\n",
2818
		dma_chan_name(chan),
2819
		(config->direction == DMA_DEV_TO_MEM) ? "RX" : "TX",
2820 2821 2822 2823
		src_addr_width, dst_addr_width,
		src_maxburst, dst_maxburst);

	return 0;
2824 2825
}

2826 2827
static int d40_control(struct dma_chan *chan, enum dma_ctrl_cmd cmd,
		       unsigned long arg)
2828 2829 2830
{
	struct d40_chan *d40c = container_of(chan, struct d40_chan, chan);

2831
	if (d40c->phy_chan == NULL) {
2832
		chan_err(d40c, "Channel is not allocated!\n");
2833 2834 2835
		return -EINVAL;
	}

2836 2837
	switch (cmd) {
	case DMA_TERMINATE_ALL:
2838 2839
		d40_terminate_all(chan);
		return 0;
2840
	case DMA_PAUSE:
2841
		return d40_pause(d40c);
2842
	case DMA_RESUME:
2843
		return d40_resume(d40c);
2844
	case DMA_SLAVE_CONFIG:
2845
		return d40_set_runtime_config(chan,
2846 2847 2848
			(struct dma_slave_config *) arg);
	default:
		break;
2849 2850 2851 2852 2853 2854 2855 2856 2857 2858 2859 2860 2861 2862 2863 2864 2865 2866 2867 2868 2869 2870 2871 2872 2873 2874
	}

	/* Other commands are unimplemented */
	return -ENXIO;
}

/* Initialization functions */

static void __init d40_chan_init(struct d40_base *base, struct dma_device *dma,
				 struct d40_chan *chans, int offset,
				 int num_chans)
{
	int i = 0;
	struct d40_chan *d40c;

	INIT_LIST_HEAD(&dma->channels);

	for (i = offset; i < offset + num_chans; i++) {
		d40c = &chans[i];
		d40c->base = base;
		d40c->chan.device = dma;

		spin_lock_init(&d40c->lock);

		d40c->log_num = D40_PHY_CHAN;

2875
		INIT_LIST_HEAD(&d40c->done);
2876 2877
		INIT_LIST_HEAD(&d40c->active);
		INIT_LIST_HEAD(&d40c->queue);
2878
		INIT_LIST_HEAD(&d40c->pending_queue);
2879
		INIT_LIST_HEAD(&d40c->client);
2880
		INIT_LIST_HEAD(&d40c->prepare_queue);
2881 2882 2883 2884 2885 2886 2887 2888 2889

		tasklet_init(&d40c->tasklet, dma_tasklet,
			     (unsigned long) d40c);

		list_add_tail(&d40c->chan.device_node,
			      &dma->channels);
	}
}

2890 2891 2892 2893 2894 2895 2896 2897 2898 2899 2900 2901 2902 2903 2904 2905 2906 2907
static void d40_ops_init(struct d40_base *base, struct dma_device *dev)
{
	if (dma_has_cap(DMA_SLAVE, dev->cap_mask))
		dev->device_prep_slave_sg = d40_prep_slave_sg;

	if (dma_has_cap(DMA_MEMCPY, dev->cap_mask)) {
		dev->device_prep_dma_memcpy = d40_prep_memcpy;

		/*
		 * This controller can only access address at even
		 * 32bit boundaries, i.e. 2^2
		 */
		dev->copy_align = 2;
	}

	if (dma_has_cap(DMA_SG, dev->cap_mask))
		dev->device_prep_dma_sg = d40_prep_memcpy_sg;

R
Rabin Vincent 已提交
2908 2909 2910
	if (dma_has_cap(DMA_CYCLIC, dev->cap_mask))
		dev->device_prep_dma_cyclic = dma40_prep_dma_cyclic;

2911 2912 2913 2914 2915 2916 2917 2918
	dev->device_alloc_chan_resources = d40_alloc_chan_resources;
	dev->device_free_chan_resources = d40_free_chan_resources;
	dev->device_issue_pending = d40_issue_pending;
	dev->device_tx_status = d40_tx_status;
	dev->device_control = d40_control;
	dev->dev = base->dev;
}

2919 2920 2921 2922 2923 2924 2925 2926 2927 2928
static int __init d40_dmaengine_init(struct d40_base *base,
				     int num_reserved_chans)
{
	int err ;

	d40_chan_init(base, &base->dma_slave, base->log_chans,
		      0, base->num_log_chans);

	dma_cap_zero(base->dma_slave.cap_mask);
	dma_cap_set(DMA_SLAVE, base->dma_slave.cap_mask);
R
Rabin Vincent 已提交
2929
	dma_cap_set(DMA_CYCLIC, base->dma_slave.cap_mask);
2930

2931
	d40_ops_init(base, &base->dma_slave);
2932 2933 2934 2935

	err = dma_async_device_register(&base->dma_slave);

	if (err) {
2936
		d40_err(base->dev, "Failed to register slave channels\n");
2937 2938 2939 2940
		goto failure1;
	}

	d40_chan_init(base, &base->dma_memcpy, base->log_chans,
2941
		      base->num_log_chans, ARRAY_SIZE(dma40_memcpy_channels));
2942 2943 2944

	dma_cap_zero(base->dma_memcpy.cap_mask);
	dma_cap_set(DMA_MEMCPY, base->dma_memcpy.cap_mask);
2945 2946 2947
	dma_cap_set(DMA_SG, base->dma_memcpy.cap_mask);

	d40_ops_init(base, &base->dma_memcpy);
2948 2949 2950 2951

	err = dma_async_device_register(&base->dma_memcpy);

	if (err) {
2952 2953
		d40_err(base->dev,
			"Failed to regsiter memcpy only channels\n");
2954 2955 2956 2957 2958 2959 2960 2961 2962
		goto failure2;
	}

	d40_chan_init(base, &base->dma_both, base->phy_chans,
		      0, num_reserved_chans);

	dma_cap_zero(base->dma_both.cap_mask);
	dma_cap_set(DMA_SLAVE, base->dma_both.cap_mask);
	dma_cap_set(DMA_MEMCPY, base->dma_both.cap_mask);
2963
	dma_cap_set(DMA_SG, base->dma_both.cap_mask);
R
Rabin Vincent 已提交
2964
	dma_cap_set(DMA_CYCLIC, base->dma_slave.cap_mask);
2965 2966

	d40_ops_init(base, &base->dma_both);
2967 2968 2969
	err = dma_async_device_register(&base->dma_both);

	if (err) {
2970 2971
		d40_err(base->dev,
			"Failed to register logical and physical capable channels\n");
2972 2973 2974 2975 2976 2977 2978 2979 2980 2981 2982
		goto failure3;
	}
	return 0;
failure3:
	dma_async_device_unregister(&base->dma_memcpy);
failure2:
	dma_async_device_unregister(&base->dma_slave);
failure1:
	return err;
}

2983 2984 2985 2986
/* Suspend resume functionality */
#ifdef CONFIG_PM
static int dma40_pm_suspend(struct device *dev)
{
2987 2988 2989
	struct platform_device *pdev = to_platform_device(dev);
	struct d40_base *base = platform_get_drvdata(pdev);
	int ret = 0;
2990

2991 2992 2993
	if (base->lcpa_regulator)
		ret = regulator_disable(base->lcpa_regulator);
	return ret;
2994 2995 2996 2997 2998 2999 3000 3001 3002 3003 3004 3005 3006 3007 3008 3009 3010 3011 3012 3013 3014 3015 3016 3017 3018 3019 3020 3021 3022 3023
}

static int dma40_runtime_suspend(struct device *dev)
{
	struct platform_device *pdev = to_platform_device(dev);
	struct d40_base *base = platform_get_drvdata(pdev);

	d40_save_restore_registers(base, true);

	/* Don't disable/enable clocks for v1 due to HW bugs */
	if (base->rev != 1)
		writel_relaxed(base->gcc_pwr_off_mask,
			       base->virtbase + D40_DREG_GCC);

	return 0;
}

static int dma40_runtime_resume(struct device *dev)
{
	struct platform_device *pdev = to_platform_device(dev);
	struct d40_base *base = platform_get_drvdata(pdev);

	if (base->initialized)
		d40_save_restore_registers(base, false);

	writel_relaxed(D40_DREG_GCC_ENABLE_ALL,
		       base->virtbase + D40_DREG_GCC);
	return 0;
}

3024 3025 3026 3027 3028 3029 3030 3031 3032 3033 3034
static int dma40_resume(struct device *dev)
{
	struct platform_device *pdev = to_platform_device(dev);
	struct d40_base *base = platform_get_drvdata(pdev);
	int ret = 0;

	if (base->lcpa_regulator)
		ret = regulator_enable(base->lcpa_regulator);

	return ret;
}
3035 3036 3037 3038 3039

static const struct dev_pm_ops dma40_pm_ops = {
	.suspend		= dma40_pm_suspend,
	.runtime_suspend	= dma40_runtime_suspend,
	.runtime_resume		= dma40_runtime_resume,
3040
	.resume			= dma40_resume,
3041 3042 3043 3044 3045 3046
};
#define DMA40_PM_OPS	(&dma40_pm_ops)
#else
#define DMA40_PM_OPS	NULL
#endif

3047 3048 3049 3050 3051 3052 3053 3054
/* Initialization functions. */

static int __init d40_phy_res_init(struct d40_base *base)
{
	int i;
	int num_phy_chans_avail = 0;
	u32 val[2];
	int odd_even_bit = -2;
3055
	int gcc = D40_DREG_GCC_ENA;
3056 3057 3058 3059 3060 3061 3062 3063 3064 3065 3066

	val[0] = readl(base->virtbase + D40_DREG_PRSME);
	val[1] = readl(base->virtbase + D40_DREG_PRSMO);

	for (i = 0; i < base->num_phy_chans; i++) {
		base->phy_res[i].num = i;
		odd_even_bit += 2 * ((i % 2) == 0);
		if (((val[i % 2] >> odd_even_bit) & 3) == 1) {
			/* Mark security only channels as occupied */
			base->phy_res[i].allocated_src = D40_ALLOC_PHY;
			base->phy_res[i].allocated_dst = D40_ALLOC_PHY;
3067 3068 3069 3070 3071 3072 3073
			base->phy_res[i].reserved = true;
			gcc |= D40_DREG_GCC_EVTGRP_ENA(D40_PHYS_TO_GROUP(i),
						       D40_DREG_GCC_SRC);
			gcc |= D40_DREG_GCC_EVTGRP_ENA(D40_PHYS_TO_GROUP(i),
						       D40_DREG_GCC_DST);


3074 3075 3076
		} else {
			base->phy_res[i].allocated_src = D40_ALLOC_FREE;
			base->phy_res[i].allocated_dst = D40_ALLOC_FREE;
3077
			base->phy_res[i].reserved = false;
3078 3079 3080 3081
			num_phy_chans_avail++;
		}
		spin_lock_init(&base->phy_res[i].lock);
	}
3082 3083 3084

	/* Mark disabled channels as occupied */
	for (i = 0; base->plat_data->disabled_channels[i] != -1; i++) {
3085 3086 3087 3088
		int chan = base->plat_data->disabled_channels[i];

		base->phy_res[chan].allocated_src = D40_ALLOC_PHY;
		base->phy_res[chan].allocated_dst = D40_ALLOC_PHY;
3089 3090 3091 3092 3093
		base->phy_res[chan].reserved = true;
		gcc |= D40_DREG_GCC_EVTGRP_ENA(D40_PHYS_TO_GROUP(chan),
					       D40_DREG_GCC_SRC);
		gcc |= D40_DREG_GCC_EVTGRP_ENA(D40_PHYS_TO_GROUP(chan),
					       D40_DREG_GCC_DST);
3094
		num_phy_chans_avail--;
3095 3096
	}

3097 3098 3099 3100 3101 3102 3103
	/* Mark soft_lli channels */
	for (i = 0; i < base->plat_data->num_of_soft_lli_chans; i++) {
		int chan = base->plat_data->soft_lli_chans[i];

		base->phy_res[chan].use_soft_lli = true;
	}

3104 3105 3106 3107 3108 3109 3110 3111 3112 3113 3114 3115 3116 3117 3118 3119 3120
	dev_info(base->dev, "%d of %d physical DMA channels available\n",
		 num_phy_chans_avail, base->num_phy_chans);

	/* Verify settings extended vs standard */
	val[0] = readl(base->virtbase + D40_DREG_PRTYP);

	for (i = 0; i < base->num_phy_chans; i++) {

		if (base->phy_res[i].allocated_src == D40_ALLOC_FREE &&
		    (val[0] & 0x3) != 1)
			dev_info(base->dev,
				 "[%s] INFO: channel %d is misconfigured (%d)\n",
				 __func__, i, val[0] & 0x3);

		val[0] = val[0] >> 2;
	}

3121 3122 3123 3124 3125 3126 3127 3128 3129
	/*
	 * To keep things simple, Enable all clocks initially.
	 * The clocks will get managed later post channel allocation.
	 * The clocks for the event lines on which reserved channels exists
	 * are not managed here.
	 */
	writel(D40_DREG_GCC_ENABLE_ALL, base->virtbase + D40_DREG_GCC);
	base->gcc_pwr_off_mask = gcc;

3130 3131 3132 3133 3134
	return num_phy_chans_avail;
}

static struct d40_base * __init d40_hw_detect_init(struct platform_device *pdev)
{
3135
	struct stedma40_platform_data *plat_data = pdev->dev.platform_data;
3136 3137 3138 3139 3140 3141
	struct clk *clk = NULL;
	void __iomem *virtbase = NULL;
	struct resource *res = NULL;
	struct d40_base *base = NULL;
	int num_log_chans = 0;
	int num_phy_chans;
3142
	int clk_ret = -EINVAL;
3143
	int i;
3144 3145 3146
	u32 pid;
	u32 cid;
	u8 rev;
3147 3148 3149

	clk = clk_get(&pdev->dev, NULL);
	if (IS_ERR(clk)) {
3150
		d40_err(&pdev->dev, "No matching clock found\n");
3151 3152 3153
		goto failure;
	}

3154 3155 3156 3157 3158
	clk_ret = clk_prepare_enable(clk);
	if (clk_ret) {
		d40_err(&pdev->dev, "Failed to prepare/enable clock\n");
		goto failure;
	}
3159 3160 3161 3162 3163 3164 3165 3166 3167 3168 3169 3170 3171 3172

	/* Get IO for DMAC base address */
	res = platform_get_resource_byname(pdev, IORESOURCE_MEM, "base");
	if (!res)
		goto failure;

	if (request_mem_region(res->start, resource_size(res),
			       D40_NAME " I/O base") == NULL)
		goto failure;

	virtbase = ioremap(res->start, resource_size(res));
	if (!virtbase)
		goto failure;

3173 3174 3175 3176 3177 3178 3179
	/* This is just a regular AMBA PrimeCell ID actually */
	for (pid = 0, i = 0; i < 4; i++)
		pid |= (readl(virtbase + resource_size(res) - 0x20 + 4 * i)
			& 255) << (i * 8);
	for (cid = 0, i = 0; i < 4; i++)
		cid |= (readl(virtbase + resource_size(res) - 0x10 + 4 * i)
			& 255) << (i * 8);
3180

3181 3182 3183 3184 3185
	if (cid != AMBA_CID) {
		d40_err(&pdev->dev, "Unknown hardware! No PrimeCell ID\n");
		goto failure;
	}
	if (AMBA_MANF_BITS(pid) != AMBA_VENDOR_ST) {
3186
		d40_err(&pdev->dev, "Unknown designer! Got %x wanted %x\n",
3187 3188
			AMBA_MANF_BITS(pid),
			AMBA_VENDOR_ST);
3189 3190
		goto failure;
	}
3191 3192 3193 3194 3195 3196
	/*
	 * HW revision:
	 * DB8500ed has revision 0
	 * ? has revision 1
	 * DB8500v1 has revision 2
	 * DB8500v2 has revision 3
3197 3198
	 * AP9540v1 has revision 4
	 * DB8540v1 has revision 4
3199 3200
	 */
	rev = AMBA_REV_BITS(pid);
3201 3202 3203 3204
	if (rev < 2) {
		d40_err(&pdev->dev, "hardware revision: %d is not supported", rev);
		goto failure;
	}
3205

3206
	/* The number of physical channels on this HW */
3207 3208 3209 3210
	if (plat_data->num_of_phy_chans)
		num_phy_chans = plat_data->num_of_phy_chans;
	else
		num_phy_chans = 4 * (readl(virtbase + D40_DREG_ICFG) & 0x7) + 4;
3211

3212 3213
	num_log_chans = num_phy_chans * D40_MAX_LOG_CHAN_PER_PHY;

3214 3215 3216
	dev_info(&pdev->dev,
		 "hardware rev: %d @ 0x%x with %d physical and %d logical channels\n",
		 rev, res->start, num_phy_chans, num_log_chans);
3217 3218

	base = kzalloc(ALIGN(sizeof(struct d40_base), 4) +
3219
		       (num_phy_chans + num_log_chans + ARRAY_SIZE(dma40_memcpy_channels)) *
3220 3221 3222
		       sizeof(struct d40_chan), GFP_KERNEL);

	if (base == NULL) {
3223
		d40_err(&pdev->dev, "Out of memory\n");
3224 3225 3226
		goto failure;
	}

3227
	base->rev = rev;
3228 3229 3230 3231 3232 3233 3234 3235 3236 3237 3238
	base->clk = clk;
	base->num_phy_chans = num_phy_chans;
	base->num_log_chans = num_log_chans;
	base->phy_start = res->start;
	base->phy_size = resource_size(res);
	base->virtbase = virtbase;
	base->plat_data = plat_data;
	base->dev = &pdev->dev;
	base->phy_chans = ((void *)base) + ALIGN(sizeof(struct d40_base), 4);
	base->log_chans = &base->phy_chans[num_phy_chans];

3239 3240 3241 3242 3243 3244 3245 3246 3247 3248 3249 3250 3251 3252 3253 3254 3255 3256 3257 3258 3259 3260 3261 3262 3263 3264 3265 3266 3267 3268
	if (base->plat_data->num_of_phy_chans == 14) {
		base->gen_dmac.backup = d40_backup_regs_v4b;
		base->gen_dmac.backup_size = BACKUP_REGS_SZ_V4B;
		base->gen_dmac.interrupt_en = D40_DREG_CPCMIS;
		base->gen_dmac.interrupt_clear = D40_DREG_CPCICR;
		base->gen_dmac.realtime_en = D40_DREG_CRSEG1;
		base->gen_dmac.realtime_clear = D40_DREG_CRCEG1;
		base->gen_dmac.high_prio_en = D40_DREG_CPSEG1;
		base->gen_dmac.high_prio_clear = D40_DREG_CPCEG1;
		base->gen_dmac.il = il_v4b;
		base->gen_dmac.il_size = ARRAY_SIZE(il_v4b);
		base->gen_dmac.init_reg = dma_init_reg_v4b;
		base->gen_dmac.init_reg_size = ARRAY_SIZE(dma_init_reg_v4b);
	} else {
		if (base->rev >= 3) {
			base->gen_dmac.backup = d40_backup_regs_v4a;
			base->gen_dmac.backup_size = BACKUP_REGS_SZ_V4A;
		}
		base->gen_dmac.interrupt_en = D40_DREG_PCMIS;
		base->gen_dmac.interrupt_clear = D40_DREG_PCICR;
		base->gen_dmac.realtime_en = D40_DREG_RSEG1;
		base->gen_dmac.realtime_clear = D40_DREG_RCEG1;
		base->gen_dmac.high_prio_en = D40_DREG_PSEG1;
		base->gen_dmac.high_prio_clear = D40_DREG_PCEG1;
		base->gen_dmac.il = il_v4a;
		base->gen_dmac.il_size = ARRAY_SIZE(il_v4a);
		base->gen_dmac.init_reg = dma_init_reg_v4a;
		base->gen_dmac.init_reg_size = ARRAY_SIZE(dma_init_reg_v4a);
	}

3269 3270 3271 3272 3273 3274 3275 3276 3277 3278 3279
	base->phy_res = kzalloc(num_phy_chans * sizeof(struct d40_phy_res),
				GFP_KERNEL);
	if (!base->phy_res)
		goto failure;

	base->lookup_phy_chans = kzalloc(num_phy_chans *
					 sizeof(struct d40_chan *),
					 GFP_KERNEL);
	if (!base->lookup_phy_chans)
		goto failure;

3280 3281 3282 3283 3284
	base->lookup_log_chans = kzalloc(num_log_chans *
					 sizeof(struct d40_chan *),
					 GFP_KERNEL);
	if (!base->lookup_log_chans)
		goto failure;
3285

3286 3287
	base->reg_val_backup_chan = kmalloc(base->num_phy_chans *
					    sizeof(d40_backup_regs_chan),
3288
					    GFP_KERNEL);
3289 3290 3291 3292 3293 3294
	if (!base->reg_val_backup_chan)
		goto failure;

	base->lcla_pool.alloc_map =
		kzalloc(num_phy_chans * sizeof(struct d40_desc *)
			* D40_LCLA_LINK_PER_EVENT_GRP, GFP_KERNEL);
3295 3296 3297
	if (!base->lcla_pool.alloc_map)
		goto failure;

3298 3299 3300 3301 3302 3303
	base->desc_slab = kmem_cache_create(D40_NAME, sizeof(struct d40_desc),
					    0, SLAB_HWCACHE_ALIGN,
					    NULL);
	if (base->desc_slab == NULL)
		goto failure;

3304 3305 3306
	return base;

failure:
3307 3308 3309
	if (!clk_ret)
		clk_disable_unprepare(clk);
	if (!IS_ERR(clk))
3310 3311 3312 3313 3314 3315 3316 3317 3318 3319 3320
		clk_put(clk);
	if (virtbase)
		iounmap(virtbase);
	if (res)
		release_mem_region(res->start,
				   resource_size(res));
	if (virtbase)
		iounmap(virtbase);

	if (base) {
		kfree(base->lcla_pool.alloc_map);
3321
		kfree(base->reg_val_backup_chan);
3322 3323 3324 3325 3326 3327 3328 3329 3330 3331 3332 3333 3334 3335 3336 3337 3338
		kfree(base->lookup_log_chans);
		kfree(base->lookup_phy_chans);
		kfree(base->phy_res);
		kfree(base);
	}

	return NULL;
}

static void __init d40_hw_init(struct d40_base *base)
{

	int i;
	u32 prmseo[2] = {0, 0};
	u32 activeo[2] = {0xFFFFFFFF, 0xFFFFFFFF};
	u32 pcmis = 0;
	u32 pcicr = 0;
3339 3340
	struct d40_reg_val *dma_init_reg = base->gen_dmac.init_reg;
	u32 reg_size = base->gen_dmac.init_reg_size;
3341

3342
	for (i = 0; i < reg_size; i++)
3343 3344 3345 3346 3347 3348 3349 3350 3351 3352 3353 3354 3355 3356 3357 3358 3359 3360 3361 3362 3363 3364 3365 3366 3367 3368 3369 3370 3371 3372 3373 3374
		writel(dma_init_reg[i].val,
		       base->virtbase + dma_init_reg[i].reg);

	/* Configure all our dma channels to default settings */
	for (i = 0; i < base->num_phy_chans; i++) {

		activeo[i % 2] = activeo[i % 2] << 2;

		if (base->phy_res[base->num_phy_chans - i - 1].allocated_src
		    == D40_ALLOC_PHY) {
			activeo[i % 2] |= 3;
			continue;
		}

		/* Enable interrupt # */
		pcmis = (pcmis << 1) | 1;

		/* Clear interrupt # */
		pcicr = (pcicr << 1) | 1;

		/* Set channel to physical mode */
		prmseo[i % 2] = prmseo[i % 2] << 2;
		prmseo[i % 2] |= 1;

	}

	writel(prmseo[1], base->virtbase + D40_DREG_PRMSE);
	writel(prmseo[0], base->virtbase + D40_DREG_PRMSO);
	writel(activeo[1], base->virtbase + D40_DREG_ACTIVE);
	writel(activeo[0], base->virtbase + D40_DREG_ACTIVO);

	/* Write which interrupt to enable */
3375
	writel(pcmis, base->virtbase + base->gen_dmac.interrupt_en);
3376 3377

	/* Write which interrupt to clear */
3378
	writel(pcicr, base->virtbase + base->gen_dmac.interrupt_clear);
3379

3380 3381 3382
	/* These are __initdata and cannot be accessed after init */
	base->gen_dmac.init_reg = NULL;
	base->gen_dmac.init_reg_size = 0;
3383 3384
}

3385 3386
static int __init d40_lcla_allocate(struct d40_base *base)
{
3387
	struct d40_lcla_pool *pool = &base->lcla_pool;
3388 3389 3390 3391 3392 3393 3394 3395 3396 3397 3398 3399 3400 3401 3402 3403 3404 3405 3406 3407 3408 3409 3410 3411 3412
	unsigned long *page_list;
	int i, j;
	int ret = 0;

	/*
	 * This is somewhat ugly. We need 8192 bytes that are 18 bit aligned,
	 * To full fill this hardware requirement without wasting 256 kb
	 * we allocate pages until we get an aligned one.
	 */
	page_list = kmalloc(sizeof(unsigned long) * MAX_LCLA_ALLOC_ATTEMPTS,
			    GFP_KERNEL);

	if (!page_list) {
		ret = -ENOMEM;
		goto failure;
	}

	/* Calculating how many pages that are required */
	base->lcla_pool.pages = SZ_1K * base->num_phy_chans / PAGE_SIZE;

	for (i = 0; i < MAX_LCLA_ALLOC_ATTEMPTS; i++) {
		page_list[i] = __get_free_pages(GFP_KERNEL,
						base->lcla_pool.pages);
		if (!page_list[i]) {

3413 3414
			d40_err(base->dev, "Failed to allocate %d pages.\n",
				base->lcla_pool.pages);
3415 3416 3417 3418 3419 3420 3421 3422 3423 3424 3425 3426 3427 3428 3429 3430 3431

			for (j = 0; j < i; j++)
				free_pages(page_list[j], base->lcla_pool.pages);
			goto failure;
		}

		if ((virt_to_phys((void *)page_list[i]) &
		     (LCLA_ALIGNMENT - 1)) == 0)
			break;
	}

	for (j = 0; j < i; j++)
		free_pages(page_list[j], base->lcla_pool.pages);

	if (i < MAX_LCLA_ALLOC_ATTEMPTS) {
		base->lcla_pool.base = (void *)page_list[i];
	} else {
3432 3433 3434 3435
		/*
		 * After many attempts and no succees with finding the correct
		 * alignment, try with allocating a big buffer.
		 */
3436 3437 3438 3439 3440 3441 3442 3443 3444 3445 3446 3447 3448 3449 3450 3451
		dev_warn(base->dev,
			 "[%s] Failed to get %d pages @ 18 bit align.\n",
			 __func__, base->lcla_pool.pages);
		base->lcla_pool.base_unaligned = kmalloc(SZ_1K *
							 base->num_phy_chans +
							 LCLA_ALIGNMENT,
							 GFP_KERNEL);
		if (!base->lcla_pool.base_unaligned) {
			ret = -ENOMEM;
			goto failure;
		}

		base->lcla_pool.base = PTR_ALIGN(base->lcla_pool.base_unaligned,
						 LCLA_ALIGNMENT);
	}

3452 3453 3454 3455 3456 3457 3458 3459 3460
	pool->dma_addr = dma_map_single(base->dev, pool->base,
					SZ_1K * base->num_phy_chans,
					DMA_TO_DEVICE);
	if (dma_mapping_error(base->dev, pool->dma_addr)) {
		pool->dma_addr = 0;
		ret = -ENOMEM;
		goto failure;
	}

3461 3462 3463 3464 3465 3466 3467
	writel(virt_to_phys(base->lcla_pool.base),
	       base->virtbase + D40_DREG_LCLA);
failure:
	kfree(page_list);
	return ret;
}

3468 3469 3470 3471 3472 3473 3474 3475 3476 3477 3478 3479 3480 3481 3482 3483 3484 3485 3486 3487 3488 3489
static int __init d40_of_probe(struct platform_device *pdev,
			       struct device_node *np)
{
	struct stedma40_platform_data *pdata;

	/*
	 * FIXME: Fill in this routine as more support is added.
	 * First platform enabled (u8500) doens't need any extra
	 * properties to run, so this is fairly sparce currently.
	 */

	pdata = devm_kzalloc(&pdev->dev,
			     sizeof(struct stedma40_platform_data),
			     GFP_KERNEL);
	if (!pdata)
		return -ENOMEM;

	pdev->dev.platform_data = pdata;

	return 0;
}

3490 3491
static int __init d40_probe(struct platform_device *pdev)
{
3492 3493
	struct stedma40_platform_data *plat_data = pdev->dev.platform_data;
	struct device_node *np = pdev->dev.of_node;
3494 3495
	int err;
	int ret = -ENOENT;
3496
	struct d40_base *base = NULL;
3497 3498 3499 3500
	struct resource *res = NULL;
	int num_reserved_chans;
	u32 val;

3501 3502 3503 3504 3505 3506 3507 3508 3509 3510 3511
	if (!plat_data) {
		if (np) {
			if(d40_of_probe(pdev, np)) {
				ret = -ENOMEM;
				goto failure;
			}
		} else {
			d40_err(&pdev->dev, "No pdata or Device Tree provided\n");
			goto failure;
		}
	}
3512

3513
	base = d40_hw_detect_init(pdev);
3514 3515 3516 3517 3518 3519 3520 3521 3522 3523 3524 3525 3526 3527
	if (!base)
		goto failure;

	num_reserved_chans = d40_phy_res_init(base);

	platform_set_drvdata(pdev, base);

	spin_lock_init(&base->interrupt_lock);
	spin_lock_init(&base->execmd_lock);

	/* Get IO for logical channel parameter address */
	res = platform_get_resource_byname(pdev, IORESOURCE_MEM, "lcpa");
	if (!res) {
		ret = -ENOENT;
3528
		d40_err(&pdev->dev, "No \"lcpa\" memory resource\n");
3529 3530 3531 3532 3533 3534 3535 3536
		goto failure;
	}
	base->lcpa_size = resource_size(res);
	base->phy_lcpa = res->start;

	if (request_mem_region(res->start, resource_size(res),
			       D40_NAME " I/O lcpa") == NULL) {
		ret = -EBUSY;
3537 3538 3539
		d40_err(&pdev->dev,
			"Failed to request LCPA region 0x%x-0x%x\n",
			res->start, res->end);
3540 3541 3542 3543 3544 3545 3546 3547 3548 3549 3550 3551 3552 3553 3554
		goto failure;
	}

	/* We make use of ESRAM memory for this. */
	val = readl(base->virtbase + D40_DREG_LCPA);
	if (res->start != val && val != 0) {
		dev_warn(&pdev->dev,
			 "[%s] Mismatch LCPA dma 0x%x, def 0x%x\n",
			 __func__, val, res->start);
	} else
		writel(res->start, base->virtbase + D40_DREG_LCPA);

	base->lcpa_base = ioremap(res->start, resource_size(res));
	if (!base->lcpa_base) {
		ret = -ENOMEM;
3555
		d40_err(&pdev->dev, "Failed to ioremap LCPA region\n");
3556 3557
		goto failure;
	}
3558 3559 3560 3561 3562 3563 3564 3565 3566 3567 3568 3569 3570 3571 3572 3573 3574 3575
	/* If lcla has to be located in ESRAM we don't need to allocate */
	if (base->plat_data->use_esram_lcla) {
		res = platform_get_resource_byname(pdev, IORESOURCE_MEM,
							"lcla_esram");
		if (!res) {
			ret = -ENOENT;
			d40_err(&pdev->dev,
				"No \"lcla_esram\" memory resource\n");
			goto failure;
		}
		base->lcla_pool.base = ioremap(res->start,
						resource_size(res));
		if (!base->lcla_pool.base) {
			ret = -ENOMEM;
			d40_err(&pdev->dev, "Failed to ioremap LCLA region\n");
			goto failure;
		}
		writel(res->start, base->virtbase + D40_DREG_LCLA);
3576

3577 3578 3579 3580 3581 3582
	} else {
		ret = d40_lcla_allocate(base);
		if (ret) {
			d40_err(&pdev->dev, "Failed to allocate LCLA area\n");
			goto failure;
		}
3583 3584 3585 3586 3587 3588 3589 3590
	}

	spin_lock_init(&base->lcla_pool.lock);

	base->irq = platform_get_irq(pdev, 0);

	ret = request_irq(base->irq, d40_handle_interrupt, 0, D40_NAME, base);
	if (ret) {
3591
		d40_err(&pdev->dev, "No IRQ defined\n");
3592 3593 3594
		goto failure;
	}

3595 3596 3597 3598 3599
	pm_runtime_irq_safe(base->dev);
	pm_runtime_set_autosuspend_delay(base->dev, DMA40_AUTOSUSPEND_DELAY);
	pm_runtime_use_autosuspend(base->dev);
	pm_runtime_enable(base->dev);
	pm_runtime_resume(base->dev);
3600 3601 3602 3603 3604 3605 3606 3607 3608 3609 3610 3611 3612 3613 3614 3615 3616 3617 3618 3619

	if (base->plat_data->use_esram_lcla) {

		base->lcpa_regulator = regulator_get(base->dev, "lcla_esram");
		if (IS_ERR(base->lcpa_regulator)) {
			d40_err(&pdev->dev, "Failed to get lcpa_regulator\n");
			base->lcpa_regulator = NULL;
			goto failure;
		}

		ret = regulator_enable(base->lcpa_regulator);
		if (ret) {
			d40_err(&pdev->dev,
				"Failed to enable lcpa_regulator\n");
			regulator_put(base->lcpa_regulator);
			base->lcpa_regulator = NULL;
			goto failure;
		}
	}

3620
	base->initialized = true;
3621 3622 3623 3624
	err = d40_dmaengine_init(base, num_reserved_chans);
	if (err)
		goto failure;

3625 3626 3627 3628 3629 3630 3631
	base->dev->dma_parms = &base->dma_parms;
	err = dma_set_max_seg_size(base->dev, STEDMA40_MAX_SEG_SIZE);
	if (err) {
		d40_err(&pdev->dev, "Failed to set dma max seg size\n");
		goto failure;
	}

3632 3633
	d40_hw_init(base);

3634 3635 3636 3637 3638 3639 3640
	if (np) {
		err = of_dma_controller_register(np, d40_xlate, NULL);
		if (err && err != -ENODEV)
			dev_err(&pdev->dev,
				"could not register of_dma_controller\n");
	}

3641 3642 3643 3644 3645
	dev_info(base->dev, "initialized\n");
	return 0;

failure:
	if (base) {
3646 3647
		if (base->desc_slab)
			kmem_cache_destroy(base->desc_slab);
3648 3649
		if (base->virtbase)
			iounmap(base->virtbase);
3650

3651 3652 3653 3654 3655
		if (base->lcla_pool.base && base->plat_data->use_esram_lcla) {
			iounmap(base->lcla_pool.base);
			base->lcla_pool.base = NULL;
		}

3656 3657 3658 3659 3660
		if (base->lcla_pool.dma_addr)
			dma_unmap_single(base->dev, base->lcla_pool.dma_addr,
					 SZ_1K * base->num_phy_chans,
					 DMA_TO_DEVICE);

3661 3662 3663
		if (!base->lcla_pool.base_unaligned && base->lcla_pool.base)
			free_pages((unsigned long)base->lcla_pool.base,
				   base->lcla_pool.pages);
3664 3665 3666

		kfree(base->lcla_pool.base_unaligned);

3667 3668 3669 3670 3671 3672 3673
		if (base->phy_lcpa)
			release_mem_region(base->phy_lcpa,
					   base->lcpa_size);
		if (base->phy_start)
			release_mem_region(base->phy_start,
					   base->phy_size);
		if (base->clk) {
3674
			clk_disable_unprepare(base->clk);
3675 3676 3677
			clk_put(base->clk);
		}

3678 3679 3680 3681 3682
		if (base->lcpa_regulator) {
			regulator_disable(base->lcpa_regulator);
			regulator_put(base->lcpa_regulator);
		}

3683 3684 3685 3686 3687 3688 3689
		kfree(base->lcla_pool.alloc_map);
		kfree(base->lookup_log_chans);
		kfree(base->lookup_phy_chans);
		kfree(base->phy_res);
		kfree(base);
	}

3690
	d40_err(&pdev->dev, "probe failed\n");
3691 3692 3693
	return ret;
}

3694 3695 3696 3697 3698
static const struct of_device_id d40_match[] = {
        { .compatible = "stericsson,dma40", },
        {}
};

3699 3700 3701 3702
static struct platform_driver d40_driver = {
	.driver = {
		.owner = THIS_MODULE,
		.name  = D40_NAME,
3703
		.pm = DMA40_PM_OPS,
3704
		.of_match_table = d40_match,
3705 3706 3707
	},
};

R
Rabin Vincent 已提交
3708
static int __init stedma40_init(void)
3709 3710 3711
{
	return platform_driver_probe(&d40_driver, d40_probe);
}
L
Linus Walleij 已提交
3712
subsys_initcall(stedma40_init);