summaryrefslogtreecommitdiff
path: root/arch/arm/plat-mxs/dma-apbx.c
blob: 6f412310648c77767127bafc0066d0ae34fd35a3 (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
/*
 * Copyright (C) 2009-2010 Freescale Semiconductor, Inc. All Rights Reserved.
 *
 * This program is free software; you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation; either version 2 of the License, or
 * (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License along
 * with this program; if not, write to the Free Software Foundation, Inc.,
 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
 */

#include <linux/kernel.h>
#include <linux/init.h>
#include <linux/err.h>
#include <linux/list.h>
#include <linux/device.h>
#include <linux/platform_device.h>
#include <linux/dmapool.h>
#include <linux/delay.h>
#include <linux/io.h>

#include <mach/device.h>
#include <mach/dmaengine.h>
#include <mach/hardware.h>

#include "regs-apbx.h"

static int mxs_dma_apbx_enable(struct mxs_dma_chan *pchan, unsigned int chan)
{
	unsigned int sem;
	struct mxs_dma_device *pdev = pchan->dma;
	struct mxs_dma_desc *pdesc;

	pdesc = list_first_entry(&pchan->active, struct mxs_dma_desc, node);
	if (pdesc == NULL)
		return -EFAULT;
	sem = __raw_readl(pdev->base + HW_APBX_CHn_SEMA(chan));
	sem = (sem & BM_APBX_CHn_SEMA_PHORE) >> BP_APBX_CHn_SEMA_PHORE;
	if (pchan->flags & MXS_DMA_FLAGS_BUSY) {
		if (pdesc->cmd.cmd.bits.chain == 0)
			return 0;
		if (sem < 2) {
			if (!sem)
				return 0;
			pdesc = list_entry(pdesc->node.next,
					   struct mxs_dma_desc, node);
			__raw_writel(mxs_dma_cmd_address(pdesc),
				     pdev->base + HW_APBX_CHn_NXTCMDAR(chan));
		}
		sem = pchan->pending_num;
		pchan->pending_num = 0;
		__raw_writel(BF_APBX_CHn_SEMA_INCREMENT_SEMA(sem),
			     pdev->base + HW_APBX_CHn_SEMA(chan));
		pchan->active_num += sem;
		return 0;
	}
	pchan->active_num += pchan->pending_num;
	pchan->pending_num = 0;
	__raw_writel(mxs_dma_cmd_address(pdesc),
		     pdev->base + HW_APBX_CHn_NXTCMDAR(chan));
	__raw_writel(pchan->active_num, pdev->base + HW_APBX_CHn_SEMA(chan));
	return 0;
}

static void mxs_dma_apbx_disable(struct mxs_dma_chan *pchan, unsigned int chan)
{
	struct mxs_dma_device *pdev = pchan->dma;
	__raw_writel(0, pdev->base + HW_APBX_CHn_SEMA(chan));
}

static void mxs_dma_apbx_reset(struct mxs_dma_device *pdev, unsigned int chan)
{
	__raw_writel(1 << (chan + BP_APBX_CHANNEL_CTRL_RESET_CHANNEL),
		     pdev->base + HW_APBX_CHANNEL_CTRL_SET);
}

static void mxs_dma_apbx_freeze(struct mxs_dma_device *pdev, unsigned int chan)
{
	__raw_writel(1 << chan, pdev->base + HW_APBX_CHANNEL_CTRL_SET);
}

static void
mxs_dma_apbx_unfreeze(struct mxs_dma_device *pdev, unsigned int chan)
{
	__raw_writel(1 << chan, pdev->base + HW_APBX_CHANNEL_CTRL_CLR);
}

static int
mxs_dma_apbx_read_semaphore(struct mxs_dma_device *pdev, unsigned int chan)
{
	unsigned int reg;
	reg = __raw_readl(pdev->base + HW_APBX_CHn_SEMA(chan));
	return (reg & BM_APBX_CHn_SEMA_PHORE) >> BP_APBX_CHn_SEMA_PHORE;
}

static void
mxs_dma_apbx_enable_irq(struct mxs_dma_device *pdev,
			unsigned int chan, int enable)
{
	if (enable) {
		__raw_writel(1 << (chan + 16), pdev->base + HW_APBX_CTRL1_SET);
		__raw_writel(1 << (chan + 16), pdev->base + HW_APBX_CTRL2_SET);
	} else {
		__raw_writel(1 << (chan + 16), pdev->base + HW_APBX_CTRL1_CLR);
		__raw_writel(1 << (chan + 16), pdev->base + HW_APBX_CTRL2_CLR);
	}
}

static int
mxs_dma_apbx_irq_is_pending(struct mxs_dma_device *pdev, unsigned int chan)
{
	unsigned int reg;
	reg = __raw_readl(pdev->base + HW_APBX_CTRL1);
	reg |= __raw_readl(pdev->base + HW_APBX_CTRL2);
	return reg & (1 << chan);
}

static void mxs_dma_apbx_ack_irq(struct mxs_dma_device *pdev, unsigned int chan)
{
	__raw_writel(1 << chan, pdev->base + HW_APBX_CTRL1_CLR);
	__raw_writel(1 << chan, pdev->base + HW_APBX_CTRL2_CLR);
}

static struct mxs_dma_device mxs_dma_apbx = {
	.name = "mxs-dma-apbx",
	.enable = mxs_dma_apbx_enable,
	.disable = mxs_dma_apbx_disable,
	.reset = mxs_dma_apbx_reset,
	.freeze = mxs_dma_apbx_freeze,
	.unfreeze = mxs_dma_apbx_unfreeze,
	.read_semaphore = mxs_dma_apbx_read_semaphore,
	.enable_irq = mxs_dma_apbx_enable_irq,
	.irq_is_pending = mxs_dma_apbx_irq_is_pending,
	.ack_irq = mxs_dma_apbx_ack_irq,
};

static int __devinit dma_apbx_probe(struct platform_device *pdev)
{
	int i;
	struct resource *res;
	struct mxs_dma_plat_data *plat;
	res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
	if (!res)
		return -ENOMEM;
	mxs_dma_apbx.base = IO_ADDRESS(res->start);
	__raw_writel(BM_APBX_CTRL0_SFTRST,
		     mxs_dma_apbx.base + HW_APBX_CTRL0_CLR);
	for (i = 0; i < 10000; i++) {
		if (!(__raw_readl(mxs_dma_apbx.base + HW_APBX_CTRL0_CLR) &
		      BM_APBX_CTRL0_SFTRST))
			break;
		udelay(2);
	}
	if (i >= 10000)
		return -ETIME;
	__raw_writel(BM_APBX_CTRL0_CLKGATE,
		     mxs_dma_apbx.base + HW_APBX_CTRL0_CLR);

	plat = (struct mxs_dma_plat_data *)pdev->dev.platform_data;
	if (!plat)
		return -ENODEV;

	mxs_dma_apbx.pdev = pdev;
	mxs_dma_apbx.chan_base = plat->chan_base;
	mxs_dma_apbx.chan_num = plat->chan_num;
	platform_set_drvdata(pdev, &mxs_dma_apbx);
	return mxs_dma_device_register(&mxs_dma_apbx);
}

static int __devexit dma_apbx_remove(struct platform_device *pdev)
{
	return 0;
}

static struct platform_driver dma_apbx_driver = {
	.probe = dma_apbx_probe,
	.remove = __devexit_p(dma_apbx_remove),
	.driver = {
		   .name = "mxs-dma-apbx"},
};

static int __init mxs_dma_apbx_init(void)
{
	return platform_driver_register(&dma_apbx_driver);
}

fs_initcall(mxs_dma_apbx_init);