summaryrefslogtreecommitdiff
path: root/drivers/dma/ls2x-apb-dma.c
blob: 9652e86667224b442e8c3c84c171aca1cb9984c5 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
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
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
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
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
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
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
// SPDX-License-Identifier: GPL-2.0-or-later
/*
 * Driver for the Loongson LS2X APB DMA Controller
 *
 * Copyright (C) 2017-2023 Loongson Corporation
 */

#include <linux/clk.h>
#include <linux/dma-mapping.h>
#include <linux/dmapool.h>
#include <linux/interrupt.h>
#include <linux/io.h>
#include <linux/io-64-nonatomic-lo-hi.h>
#include <linux/module.h>
#include <linux/of.h>
#include <linux/of_dma.h>
#include <linux/platform_device.h>
#include <linux/slab.h>

#include "dmaengine.h"
#include "virt-dma.h"

/* Global Configuration Register */
#define LDMA_ORDER_ERG		0x0

/* Bitfield definitions */

/* Bitfields in Global Configuration Register */
#define LDMA_64BIT_EN		BIT(0) /* 1: 64 bit support */
#define LDMA_UNCOHERENT_EN	BIT(1) /* 0: cache, 1: uncache */
#define LDMA_ASK_VALID		BIT(2)
#define LDMA_START		BIT(3) /* DMA start operation */
#define LDMA_STOP		BIT(4) /* DMA stop operation */
#define LDMA_CONFIG_MASK	GENMASK(4, 0) /* DMA controller config bits mask */

/* Bitfields in ndesc_addr field of HW descriptor */
#define LDMA_DESC_EN		BIT(0) /*1: The next descriptor is valid */
#define LDMA_DESC_ADDR_LOW	GENMASK(31, 1)

/* Bitfields in cmd field of HW descriptor */
#define LDMA_INT		BIT(1) /* Enable DMA interrupts */
#define LDMA_DATA_DIRECTION	BIT(12) /* 1: write to device, 0: read from device */

#define LDMA_SLAVE_BUSWIDTHS	(BIT(DMA_SLAVE_BUSWIDTH_4_BYTES) | \
				 BIT(DMA_SLAVE_BUSWIDTH_8_BYTES))

#define LDMA_MAX_TRANS_LEN	U32_MAX

/*--  descriptors  -----------------------------------------------------*/

/*
 * struct ls2x_dma_hw_desc - DMA HW descriptor
 * @ndesc_addr: the next descriptor low address.
 * @mem_addr: memory low address.
 * @apb_addr: device buffer address.
 * @len: length of a piece of carried content, in words.
 * @step_len: length between two moved memory data blocks.
 * @step_times: number of blocks to be carried in a single DMA operation.
 * @cmd: descriptor command or state.
 * @stats: DMA status.
 * @high_ndesc_addr: the next descriptor high address.
 * @high_mem_addr: memory high address.
 * @reserved: reserved
 */
struct ls2x_dma_hw_desc {
	u32 ndesc_addr;
	u32 mem_addr;
	u32 apb_addr;
	u32 len;
	u32 step_len;
	u32 step_times;
	u32 cmd;
	u32 stats;
	u32 high_ndesc_addr;
	u32 high_mem_addr;
	u32 reserved[2];
} __packed;

/*
 * struct ls2x_dma_sg - ls2x dma scatter gather entry
 * @hw: the pointer to DMA HW descriptor.
 * @llp: physical address of the DMA HW descriptor.
 * @phys: destination or source address(mem).
 * @len: number of Bytes to read.
 */
struct ls2x_dma_sg {
	struct ls2x_dma_hw_desc	*hw;
	dma_addr_t		llp;
	dma_addr_t		phys;
	u32			len;
};

/*
 * struct ls2x_dma_desc - software descriptor
 * @vdesc: pointer to the virtual dma descriptor.
 * @cyclic: flag to dma cyclic
 * @burst_size: burst size of transaction, in words.
 * @desc_num: number of sg entries.
 * @direction: transfer direction, to or from device.
 * @status: dma controller status.
 * @sg: array of sgs.
 */
struct ls2x_dma_desc {
	struct virt_dma_desc		vdesc;
	bool				cyclic;
	size_t				burst_size;
	u32				desc_num;
	enum dma_transfer_direction	direction;
	enum dma_status			status;
	struct ls2x_dma_sg		sg[] __counted_by(desc_num);
};

/*--  Channels  --------------------------------------------------------*/

/*
 * struct ls2x_dma_chan - internal representation of an LS2X APB DMA channel
 * @vchan: virtual dma channel entry.
 * @desc: pointer to the ls2x sw dma descriptor.
 * @pool: hw desc table
 * @irq: irq line
 * @sconfig: configuration for slave transfers, passed via .device_config
 */
struct ls2x_dma_chan {
	struct virt_dma_chan	vchan;
	struct ls2x_dma_desc	*desc;
	void			*pool;
	int			irq;
	struct dma_slave_config	sconfig;
};

/*--  Controller  ------------------------------------------------------*/

/*
 * struct ls2x_dma_priv - LS2X APB DMAC specific information
 * @ddev: dmaengine dma_device object members
 * @dma_clk: DMAC clock source
 * @regs: memory mapped register base
 * @lchan: channel to store ls2x_dma_chan structures
 */
struct ls2x_dma_priv {
	struct dma_device	ddev;
	struct clk		*dma_clk;
	void __iomem		*regs;
	struct ls2x_dma_chan	lchan;
};

/*--  Helper functions  ------------------------------------------------*/

static inline struct ls2x_dma_desc *to_ldma_desc(struct virt_dma_desc *vdesc)
{
	return container_of(vdesc, struct ls2x_dma_desc, vdesc);
}

static inline struct ls2x_dma_chan *to_ldma_chan(struct dma_chan *chan)
{
	return container_of(chan, struct ls2x_dma_chan, vchan.chan);
}

static inline struct ls2x_dma_priv *to_ldma_priv(struct dma_device *ddev)
{
	return container_of(ddev, struct ls2x_dma_priv, ddev);
}

static struct device *chan2dev(struct dma_chan *chan)
{
	return &chan->dev->device;
}

static void ls2x_dma_desc_free(struct virt_dma_desc *vdesc)
{
	struct ls2x_dma_chan *lchan = to_ldma_chan(vdesc->tx.chan);
	struct ls2x_dma_desc *desc = to_ldma_desc(vdesc);
	int i;

	for (i = 0; i < desc->desc_num; i++) {
		if (desc->sg[i].hw)
			dma_pool_free(lchan->pool, desc->sg[i].hw,
				      desc->sg[i].llp);
	}

	kfree(desc);
}

static void ls2x_dma_write_cmd(struct ls2x_dma_chan *lchan, bool cmd)
{
	struct ls2x_dma_priv *priv = to_ldma_priv(lchan->vchan.chan.device);
	u64 val;

	val = lo_hi_readq(priv->regs + LDMA_ORDER_ERG) & ~LDMA_CONFIG_MASK;
	val |= LDMA_64BIT_EN | cmd;
	lo_hi_writeq(val, priv->regs + LDMA_ORDER_ERG);
}

static void ls2x_dma_start_transfer(struct ls2x_dma_chan *lchan)
{
	struct ls2x_dma_priv *priv = to_ldma_priv(lchan->vchan.chan.device);
	struct ls2x_dma_sg *ldma_sg;
	struct virt_dma_desc *vdesc;
	u64 val;

	/* Get the next descriptor */
	vdesc = vchan_next_desc(&lchan->vchan);
	if (!vdesc) {
		lchan->desc = NULL;
		return;
	}

	list_del(&vdesc->node);
	lchan->desc = to_ldma_desc(vdesc);
	ldma_sg = &lchan->desc->sg[0];

	/* Start DMA */
	lo_hi_writeq(0, priv->regs + LDMA_ORDER_ERG);
	val = (ldma_sg->llp & ~LDMA_CONFIG_MASK) | LDMA_64BIT_EN | LDMA_START;
	lo_hi_writeq(val, priv->regs + LDMA_ORDER_ERG);
}

static size_t ls2x_dmac_detect_burst(struct ls2x_dma_chan *lchan)
{
	u32 maxburst, buswidth;

	/* Reject definitely invalid configurations */
	if ((lchan->sconfig.src_addr_width & LDMA_SLAVE_BUSWIDTHS) &&
	    (lchan->sconfig.dst_addr_width & LDMA_SLAVE_BUSWIDTHS))
		return 0;

	if (lchan->sconfig.direction == DMA_MEM_TO_DEV) {
		maxburst = lchan->sconfig.dst_maxburst;
		buswidth = lchan->sconfig.dst_addr_width;
	} else {
		maxburst = lchan->sconfig.src_maxburst;
		buswidth = lchan->sconfig.src_addr_width;
	}

	/* If maxburst is zero, fallback to LDMA_MAX_TRANS_LEN */
	return maxburst ? (maxburst * buswidth) >> 2 : LDMA_MAX_TRANS_LEN;
}

static void ls2x_dma_fill_desc(struct ls2x_dma_chan *lchan, u32 sg_index,
			       struct ls2x_dma_desc *desc)
{
	struct ls2x_dma_sg *ldma_sg = &desc->sg[sg_index];
	u32 num_segments, segment_size;

	if (desc->direction == DMA_MEM_TO_DEV) {
		ldma_sg->hw->cmd = LDMA_INT | LDMA_DATA_DIRECTION;
		ldma_sg->hw->apb_addr = lchan->sconfig.dst_addr;
	} else {
		ldma_sg->hw->cmd = LDMA_INT;
		ldma_sg->hw->apb_addr = lchan->sconfig.src_addr;
	}

	ldma_sg->hw->mem_addr = lower_32_bits(ldma_sg->phys);
	ldma_sg->hw->high_mem_addr = upper_32_bits(ldma_sg->phys);

	/* Split into multiple equally sized segments if necessary */
	num_segments = DIV_ROUND_UP((ldma_sg->len + 3) >> 2, desc->burst_size);
	segment_size = DIV_ROUND_UP((ldma_sg->len + 3) >> 2, num_segments);

	/* Word count register takes input in words */
	ldma_sg->hw->len = segment_size;
	ldma_sg->hw->step_times = num_segments;
	ldma_sg->hw->step_len = 0;

	/* lets make a link list */
	if (sg_index) {
		desc->sg[sg_index - 1].hw->ndesc_addr = ldma_sg->llp | LDMA_DESC_EN;
		desc->sg[sg_index - 1].hw->high_ndesc_addr = upper_32_bits(ldma_sg->llp);
	}
}

/*--  DMA Engine API  --------------------------------------------------*/

/*
 * ls2x_dma_alloc_chan_resources - allocate resources for DMA channel
 * @chan: allocate descriptor resources for this channel
 *
 * return - the number of allocated descriptors
 */
static int ls2x_dma_alloc_chan_resources(struct dma_chan *chan)
{
	struct ls2x_dma_chan *lchan = to_ldma_chan(chan);

	/* Create a pool of consistent memory blocks for hardware descriptors */
	lchan->pool = dma_pool_create(dev_name(chan2dev(chan)),
				      chan->device->dev, PAGE_SIZE,
				      __alignof__(struct ls2x_dma_hw_desc), 0);
	if (!lchan->pool) {
		dev_err(chan2dev(chan), "No memory for descriptors\n");
		return -ENOMEM;
	}

	return 1;
}

/*
 * ls2x_dma_free_chan_resources - free all channel resources
 * @chan: DMA channel
 */
static void ls2x_dma_free_chan_resources(struct dma_chan *chan)
{
	struct ls2x_dma_chan *lchan = to_ldma_chan(chan);

	vchan_free_chan_resources(to_virt_chan(chan));
	dma_pool_destroy(lchan->pool);
	lchan->pool = NULL;
}

/*
 * ls2x_dma_prep_slave_sg - prepare descriptors for a DMA_SLAVE transaction
 * @chan: DMA channel
 * @sgl: scatterlist to transfer to/from
 * @sg_len: number of entries in @scatterlist
 * @direction: DMA direction
 * @flags: tx descriptor status flags
 * @context: transaction context (ignored)
 *
 * Return: Async transaction descriptor on success and NULL on failure
 */
static struct dma_async_tx_descriptor *
ls2x_dma_prep_slave_sg(struct dma_chan *chan, struct scatterlist *sgl,
		       u32 sg_len, enum dma_transfer_direction direction,
		       unsigned long flags, void *context)
{
	struct ls2x_dma_chan *lchan = to_ldma_chan(chan);
	struct ls2x_dma_desc *desc;
	struct scatterlist *sg;
	size_t burst_size;
	int i;

	if (unlikely(!sg_len || !is_slave_direction(direction)))
		return NULL;

	burst_size = ls2x_dmac_detect_burst(lchan);
	if (!burst_size)
		return NULL;

	desc = kzalloc(struct_size(desc, sg, sg_len), GFP_NOWAIT);
	if (!desc)
		return NULL;

	desc->desc_num = sg_len;
	desc->direction = direction;
	desc->burst_size = burst_size;

	for_each_sg(sgl, sg, sg_len, i) {
		struct ls2x_dma_sg *ldma_sg = &desc->sg[i];

		/* Allocate DMA capable memory for hardware descriptor */
		ldma_sg->hw = dma_pool_alloc(lchan->pool, GFP_NOWAIT, &ldma_sg->llp);
		if (!ldma_sg->hw) {
			desc->desc_num = i;
			ls2x_dma_desc_free(&desc->vdesc);
			return NULL;
		}

		ldma_sg->phys = sg_dma_address(sg);
		ldma_sg->len = sg_dma_len(sg);

		ls2x_dma_fill_desc(lchan, i, desc);
	}

	/* Setting the last descriptor enable bit */
	desc->sg[sg_len - 1].hw->ndesc_addr &= ~LDMA_DESC_EN;
	desc->status = DMA_IN_PROGRESS;

	return vchan_tx_prep(&lchan->vchan, &desc->vdesc, flags);
}

/*
 * ls2x_dma_prep_dma_cyclic - prepare the cyclic DMA transfer
 * @chan: the DMA channel to prepare
 * @buf_addr: physical DMA address where the buffer starts
 * @buf_len: total number of bytes for the entire buffer
 * @period_len: number of bytes for each period
 * @direction: transfer direction, to or from device
 * @flags: tx descriptor status flags
 *
 * Return: Async transaction descriptor on success and NULL on failure
 */
static struct dma_async_tx_descriptor *
ls2x_dma_prep_dma_cyclic(struct dma_chan *chan, dma_addr_t buf_addr, size_t buf_len,
			 size_t period_len, enum dma_transfer_direction direction,
			 unsigned long flags)
{
	struct ls2x_dma_chan *lchan = to_ldma_chan(chan);
	struct ls2x_dma_desc *desc;
	size_t burst_size;
	u32 num_periods;
	int i;

	if (unlikely(!buf_len || !period_len))
		return NULL;

	if (unlikely(!is_slave_direction(direction)))
		return NULL;

	burst_size = ls2x_dmac_detect_burst(lchan);
	if (!burst_size)
		return NULL;

	num_periods = buf_len / period_len;
	desc = kzalloc(struct_size(desc, sg, num_periods), GFP_NOWAIT);
	if (!desc)
		return NULL;

	desc->desc_num = num_periods;
	desc->direction = direction;
	desc->burst_size = burst_size;

	/* Build cyclic linked list */
	for (i = 0; i < num_periods; i++) {
		struct ls2x_dma_sg *ldma_sg = &desc->sg[i];

		/* Allocate DMA capable memory for hardware descriptor */
		ldma_sg->hw = dma_pool_alloc(lchan->pool, GFP_NOWAIT, &ldma_sg->llp);
		if (!ldma_sg->hw) {
			desc->desc_num = i;
			ls2x_dma_desc_free(&desc->vdesc);
			return NULL;
		}

		ldma_sg->phys = buf_addr + period_len * i;
		ldma_sg->len = period_len;

		ls2x_dma_fill_desc(lchan, i, desc);
	}

	/* Lets make a cyclic list */
	desc->sg[num_periods - 1].hw->ndesc_addr = desc->sg[0].llp | LDMA_DESC_EN;
	desc->sg[num_periods - 1].hw->high_ndesc_addr = upper_32_bits(desc->sg[0].llp);
	desc->cyclic = true;
	desc->status = DMA_IN_PROGRESS;

	return vchan_tx_prep(&lchan->vchan, &desc->vdesc, flags);
}

/*
 * ls2x_slave_config - set slave configuration for channel
 * @chan: dma channel
 * @cfg: slave configuration
 *
 * Sets slave configuration for channel
 */
static int ls2x_dma_slave_config(struct dma_chan *chan,
				 struct dma_slave_config *config)
{
	struct ls2x_dma_chan *lchan = to_ldma_chan(chan);

	memcpy(&lchan->sconfig, config, sizeof(*config));
	return 0;
}

/*
 * ls2x_dma_issue_pending - push pending transactions to the hardware
 * @chan: channel
 *
 * When this function is called, all pending transactions are pushed to the
 * hardware and executed.
 */
static void ls2x_dma_issue_pending(struct dma_chan *chan)
{
	struct ls2x_dma_chan *lchan = to_ldma_chan(chan);
	unsigned long flags;

	spin_lock_irqsave(&lchan->vchan.lock, flags);
	if (vchan_issue_pending(&lchan->vchan) && !lchan->desc)
		ls2x_dma_start_transfer(lchan);
	spin_unlock_irqrestore(&lchan->vchan.lock, flags);
}

/*
 * ls2x_dma_terminate_all - terminate all transactions
 * @chan: channel
 *
 * Stops all DMA transactions.
 */
static int ls2x_dma_terminate_all(struct dma_chan *chan)
{
	struct ls2x_dma_chan *lchan = to_ldma_chan(chan);
	unsigned long flags;
	LIST_HEAD(head);

	spin_lock_irqsave(&lchan->vchan.lock, flags);
	/* Setting stop cmd */
	ls2x_dma_write_cmd(lchan, LDMA_STOP);
	if (lchan->desc) {
		vchan_terminate_vdesc(&lchan->desc->vdesc);
		lchan->desc = NULL;
	}

	vchan_get_all_descriptors(&lchan->vchan, &head);
	spin_unlock_irqrestore(&lchan->vchan.lock, flags);

	vchan_dma_desc_free_list(&lchan->vchan, &head);
	return 0;
}

/*
 * ls2x_dma_synchronize - Synchronizes the termination of transfers to the
 * current context.
 * @chan: channel
 */
static void ls2x_dma_synchronize(struct dma_chan *chan)
{
	struct ls2x_dma_chan *lchan = to_ldma_chan(chan);

	vchan_synchronize(&lchan->vchan);
}

static int ls2x_dma_pause(struct dma_chan *chan)
{
	struct ls2x_dma_chan *lchan = to_ldma_chan(chan);
	unsigned long flags;

	spin_lock_irqsave(&lchan->vchan.lock, flags);
	if (lchan->desc && lchan->desc->status == DMA_IN_PROGRESS) {
		ls2x_dma_write_cmd(lchan, LDMA_STOP);
		lchan->desc->status = DMA_PAUSED;
	}
	spin_unlock_irqrestore(&lchan->vchan.lock, flags);

	return 0;
}

static int ls2x_dma_resume(struct dma_chan *chan)
{
	struct ls2x_dma_chan *lchan = to_ldma_chan(chan);
	unsigned long flags;

	spin_lock_irqsave(&lchan->vchan.lock, flags);
	if (lchan->desc && lchan->desc->status == DMA_PAUSED) {
		lchan->desc->status = DMA_IN_PROGRESS;
		ls2x_dma_write_cmd(lchan, LDMA_START);
	}
	spin_unlock_irqrestore(&lchan->vchan.lock, flags);

	return 0;
}

/*
 * ls2x_dma_isr - LS2X DMA Interrupt handler
 * @irq: IRQ number
 * @dev_id: Pointer to ls2x_dma_chan
 *
 * Return: IRQ_HANDLED/IRQ_NONE
 */
static irqreturn_t ls2x_dma_isr(int irq, void *dev_id)
{
	struct ls2x_dma_chan *lchan = dev_id;
	struct ls2x_dma_desc *desc;

	spin_lock(&lchan->vchan.lock);
	desc = lchan->desc;
	if (desc) {
		if (desc->cyclic) {
			vchan_cyclic_callback(&desc->vdesc);
		} else {
			desc->status = DMA_COMPLETE;
			vchan_cookie_complete(&desc->vdesc);
			ls2x_dma_start_transfer(lchan);
		}

		/* ls2x_dma_start_transfer() updates lchan->desc */
		if (!lchan->desc)
			ls2x_dma_write_cmd(lchan, LDMA_STOP);
	}
	spin_unlock(&lchan->vchan.lock);

	return IRQ_HANDLED;
}

static int ls2x_dma_chan_init(struct platform_device *pdev,
			      struct ls2x_dma_priv *priv)
{
	struct ls2x_dma_chan *lchan = &priv->lchan;
	struct device *dev = &pdev->dev;
	int ret;

	lchan->irq = platform_get_irq(pdev, 0);
	if (lchan->irq < 0)
		return lchan->irq;

	ret = devm_request_irq(dev, lchan->irq, ls2x_dma_isr, IRQF_TRIGGER_RISING,
			       dev_name(&pdev->dev), lchan);
	if (ret)
		return ret;

	/* Initialize channels related values */
	INIT_LIST_HEAD(&priv->ddev.channels);
	lchan->vchan.desc_free = ls2x_dma_desc_free;
	vchan_init(&lchan->vchan, &priv->ddev);

	return 0;
}

/*
 * ls2x_dma_probe - Driver probe function
 * @pdev: Pointer to the platform_device structure
 *
 * Return: '0' on success and failure value on error
 */
static int ls2x_dma_probe(struct platform_device *pdev)
{
	struct device *dev = &pdev->dev;
	struct ls2x_dma_priv *priv;
	struct dma_device *ddev;
	int ret;

	priv = devm_kzalloc(dev, sizeof(*priv), GFP_KERNEL);
	if (!priv)
		return -ENOMEM;

	priv->regs = devm_platform_ioremap_resource(pdev, 0);
	if (IS_ERR(priv->regs))
		return dev_err_probe(dev, PTR_ERR(priv->regs),
				     "devm_platform_ioremap_resource failed.\n");

	priv->dma_clk = devm_clk_get(&pdev->dev, NULL);
	if (IS_ERR(priv->dma_clk))
		return dev_err_probe(dev, PTR_ERR(priv->dma_clk), "devm_clk_get failed.\n");

	ret = clk_prepare_enable(priv->dma_clk);
	if (ret)
		return dev_err_probe(dev, ret, "clk_prepare_enable failed.\n");

	ret = ls2x_dma_chan_init(pdev, priv);
	if (ret)
		goto disable_clk;

	ddev = &priv->ddev;
	ddev->dev = dev;
	dma_cap_zero(ddev->cap_mask);
	dma_cap_set(DMA_SLAVE, ddev->cap_mask);
	dma_cap_set(DMA_CYCLIC, ddev->cap_mask);

	ddev->device_alloc_chan_resources = ls2x_dma_alloc_chan_resources;
	ddev->device_free_chan_resources = ls2x_dma_free_chan_resources;
	ddev->device_tx_status = dma_cookie_status;
	ddev->device_issue_pending = ls2x_dma_issue_pending;
	ddev->device_prep_slave_sg = ls2x_dma_prep_slave_sg;
	ddev->device_prep_dma_cyclic = ls2x_dma_prep_dma_cyclic;
	ddev->device_config = ls2x_dma_slave_config;
	ddev->device_terminate_all = ls2x_dma_terminate_all;
	ddev->device_synchronize = ls2x_dma_synchronize;
	ddev->device_pause = ls2x_dma_pause;
	ddev->device_resume = ls2x_dma_resume;

	ddev->src_addr_widths = LDMA_SLAVE_BUSWIDTHS;
	ddev->dst_addr_widths = LDMA_SLAVE_BUSWIDTHS;
	ddev->directions = BIT(DMA_DEV_TO_MEM) | BIT(DMA_MEM_TO_DEV);

	ret = dma_async_device_register(&priv->ddev);
	if (ret < 0)
		goto disable_clk;

	ret = of_dma_controller_register(dev->of_node, of_dma_xlate_by_chan_id, priv);
	if (ret < 0)
		goto unregister_dmac;

	platform_set_drvdata(pdev, priv);

	dev_info(dev, "Loongson LS2X APB DMA driver registered successfully.\n");
	return 0;

unregister_dmac:
	dma_async_device_unregister(&priv->ddev);
disable_clk:
	clk_disable_unprepare(priv->dma_clk);

	return ret;
}

/*
 * ls2x_dma_remove - Driver remove function
 * @pdev: Pointer to the platform_device structure
 */
static void ls2x_dma_remove(struct platform_device *pdev)
{
	struct ls2x_dma_priv *priv = platform_get_drvdata(pdev);

	of_dma_controller_free(pdev->dev.of_node);
	dma_async_device_unregister(&priv->ddev);
	clk_disable_unprepare(priv->dma_clk);
}

static const struct of_device_id ls2x_dma_of_match_table[] = {
	{ .compatible = "loongson,ls2k1000-apbdma" },
	{ /* sentinel */ }
};
MODULE_DEVICE_TABLE(of, ls2x_dma_of_match_table);

static struct platform_driver ls2x_dmac_driver = {
	.probe		= ls2x_dma_probe,
	.remove_new	= ls2x_dma_remove,
	.driver = {
		.name	= "ls2x-apbdma",
		.of_match_table	= ls2x_dma_of_match_table,
	},
};
module_platform_driver(ls2x_dmac_driver);

MODULE_DESCRIPTION("Loongson LS2X APB DMA Controller driver");
MODULE_AUTHOR("Loongson Technology Corporation Limited");
MODULE_LICENSE("GPL");