21 #include <linux/module.h>
22 #include <linux/types.h>
23 #include <linux/bitops.h>
28 #include <linux/sched.h>
31 #include <linux/device.h>
34 #include <linux/slab.h>
43 #include <mach/hardware.h>
48 #define SDMA_H_C0PTR 0x000
49 #define SDMA_H_INTR 0x004
50 #define SDMA_H_STATSTOP 0x008
51 #define SDMA_H_START 0x00c
52 #define SDMA_H_EVTOVR 0x010
53 #define SDMA_H_DSPOVR 0x014
54 #define SDMA_H_HOSTOVR 0x018
55 #define SDMA_H_EVTPEND 0x01c
56 #define SDMA_H_DSPENBL 0x020
57 #define SDMA_H_RESET 0x024
58 #define SDMA_H_EVTERR 0x028
59 #define SDMA_H_INTRMSK 0x02c
60 #define SDMA_H_PSW 0x030
61 #define SDMA_H_EVTERRDBG 0x034
62 #define SDMA_H_CONFIG 0x038
63 #define SDMA_ONCE_ENB 0x040
64 #define SDMA_ONCE_DATA 0x044
65 #define SDMA_ONCE_INSTR 0x048
66 #define SDMA_ONCE_STAT 0x04c
67 #define SDMA_ONCE_CMD 0x050
68 #define SDMA_EVT_MIRROR 0x054
69 #define SDMA_ILLINSTADDR 0x058
70 #define SDMA_CHN0ADDR 0x05c
71 #define SDMA_ONCE_RTB 0x060
72 #define SDMA_XTRIG_CONF1 0x070
73 #define SDMA_XTRIG_CONF2 0x074
74 #define SDMA_CHNENBL0_IMX35 0x200
75 #define SDMA_CHNENBL0_IMX31 0x080
76 #define SDMA_CHNPRI_0 0x100
92 #define DND_END_OF_FRAME 0x80
93 #define DND_END_OF_XFER 0x40
95 #define DND_UNUSED 0x01
100 #define BD_IPCV2_END_OF_FRAME 0x40
102 #define IPCV2_MAX_NODES 50
107 #define DATA_ERROR 0x10000000
115 #define C0_SETCTX 0x07
116 #define C0_GETCTX 0x03
117 #define C0_SETDM 0x01
118 #define C0_SETPM 0x04
119 #define C0_GETDM 0x02
120 #define C0_GETPM 0x08
124 #define CHANGE_ENDIANNESS 0x80
231 #define NUM_BD (int)(PAGE_SIZE / sizeof(struct sdma_buffer_descriptor))
277 #define IMX_DMA_SG_LOOP BIT(0)
279 #define MAX_DMA_CHANNELS 32
280 #define MXC_SDMA_DEFAULT_PRIORITY 1
281 #define MXC_SDMA_MIN_PRIORITY 1
282 #define MXC_SDMA_MAX_PRIORITY 7
284 #define SDMA_FIRMWARE_MAGIC 0x414d4453
334 .name =
"imx31-sdma",
337 .name =
"imx35-sdma",
346 { .compatible =
"fsl,imx31-sdma", .data = &sdma_devtypes[
IMX31_SDMA], },
347 { .compatible =
"fsl,imx35-sdma", .data = &sdma_devtypes[
IMX35_SDMA], },
352 #define SDMA_H_CONFIG_DSPDMA BIT(12)
353 #define SDMA_H_CONFIG_RTD_PINS BIT(11)
354 #define SDMA_H_CONFIG_ACR BIT(4)
355 #define SDMA_H_CONFIG_CSM (3)
361 return chnenbl0 +
event * 4;
364 static int sdma_config_ownership(
struct sdma_channel *sdmac,
365 bool event_override,
bool mcu_override,
bool dsp_override)
371 if (event_override && mcu_override && dsp_override)
400 static void sdma_enable_channel(
struct sdma_engine *sdma,
int channel)
408 static int sdma_run_channel0(
struct sdma_engine *sdma)
413 sdma_enable_channel(sdma, 0);
425 dev_err(sdma->
dev,
"Timeout waiting for CH0 ready\n");
451 bd0->
mode.count = size / 2;
455 memcpy(buf_virt, buf, size);
457 ret = sdma_run_channel0(sdma);
471 u32 chnenbl = chnenbl_ofs(sdma, event);
475 writel_relaxed(val, sdma->
regs + chnenbl);
482 u32 chnenbl = chnenbl_ofs(sdma, event);
487 writel_relaxed(val, sdma->
regs + chnenbl);
490 static void sdma_handle_channel_loop(
struct sdma_channel *sdmac)
513 if (sdmac->
desc.callback)
514 sdmac->
desc.callback(sdmac->
desc.callback_param);
518 static void mxc_sdma_handle_channel_normal(
struct sdma_channel *sdmac)
528 for (i = 0; i < sdmac->
num_bd; i++) {
541 dma_cookie_complete(&sdmac->
desc);
542 if (sdmac->
desc.callback)
543 sdmac->
desc.callback(sdmac->
desc.callback_param);
546 static void sdma_tasklet(
unsigned long data)
553 sdma_handle_channel_loop(sdmac);
555 mxc_sdma_handle_channel_normal(sdmac);
569 int channel = fls(stat) - 1;
572 tasklet_schedule(&sdmac->
tasklet);
587 int per_2_emi = 0, emi_2_per = 0;
592 int per_2_per = 0, emi_2_emi = 0;
597 switch (peripheral_type) {
663 static int sdma_load_context(
struct sdma_channel *sdmac)
679 if (load_address < 0)
682 dev_dbg(sdma->
dev,
"load_address = %d\n", load_address);
691 memset(context, 0,
sizeof(*context));
705 bd0->
mode.count =
sizeof(*context) / 4;
708 ret = sdma_run_channel0(sdma);
715 static void sdma_disable_channel(
struct sdma_channel *sdmac)
724 static int sdma_config_channel(
struct sdma_channel *sdmac)
728 sdma_disable_channel(sdmac);
738 sdma_event_enable(sdmac, sdmac->
event_id0);
743 sdma_config_ownership(sdmac,
false,
true,
true);
746 sdma_config_ownership(sdmac,
false,
true,
false);
749 sdma_config_ownership(sdmac,
true,
true,
false);
776 ret = sdma_load_context(sdmac);
781 static int sdma_set_channel_priority(
struct sdma_channel *sdmac,
797 static int sdma_request_channel(
struct sdma_channel *sdmac)
816 init_completion(&sdmac->
done);
837 cookie = dma_cookie_assign(tx);
839 spin_unlock_irqrestore(&sdmac->
lock, flags);
844 static int sdma_alloc_chan_resources(
struct dma_chan *
chan)
872 ret = sdma_request_channel(sdmac);
876 ret = sdma_set_channel_priority(sdmac, prio);
881 sdmac->
desc.tx_submit = sdma_tx_submit;
888 static void sdma_free_chan_resources(
struct dma_chan *chan)
893 sdma_disable_channel(sdmac);
896 sdma_event_disable(sdmac, sdmac->
event_id0);
898 sdma_event_disable(sdmac, sdmac->
event_id1);
903 sdma_set_channel_priority(sdmac, 0);
914 unsigned long flags,
void *context)
930 dev_dbg(sdma->
dev,
"setting up %d entries for channel %d.\n",
934 ret = sdma_load_context(sdmac);
939 dev_err(sdma->
dev,
"SDMA channel %d: maximum number of sg exceeded: %d > %d\n",
954 if (count > 0xffff) {
955 dev_err(sdma->
dev,
"SDMA channel %d: maximum bytes for sg entry exceeded: %d > %d\n",
956 channel, count, 0xffff);
971 bd->
mode.command = 0;
976 bd->
mode.command = 2;
981 bd->
mode.command = 1;
989 if (i + 1 == sg_len) {
995 dev_dbg(sdma->
dev,
"entry %d: count: %d dma: 0x%08x %s%s\n",
998 param &
BD_INTR ?
" intr" :
"");
1006 return &sdmac->
desc;
1015 unsigned long flags,
void *context)
1019 int num_periods = buf_len / period_len;
1021 int ret, i = 0, buf = 0;
1023 dev_dbg(sdma->
dev,
"%s channel: %d\n", __func__, channel);
1034 ret = sdma_load_context(sdmac);
1038 if (num_periods >
NUM_BD) {
1039 dev_err(sdma->
dev,
"SDMA channel %d: maximum number of sg exceeded: %d > %d\n",
1040 channel, num_periods,
NUM_BD);
1044 if (period_len > 0xffff) {
1045 dev_err(sdma->
dev,
"SDMA channel %d: maximum period size exceeded: %d > %d\n",
1046 channel, period_len, 0xffff);
1050 while (buf < buf_len) {
1056 bd->
mode.count = period_len;
1061 bd->
mode.command = 0;
1066 if (i + 1 == num_periods)
1069 dev_dbg(sdma->
dev,
"entry %d: count: %d dma: 0x%08x %s%s\n",
1070 i, period_len, dma_addr,
1071 param &
BD_WRAP ?
"wrap" :
"",
1072 param &
BD_INTR ?
" intr" :
"");
1076 dma_addr += period_len;
1082 sdmac->
num_bd = num_periods;
1085 return &sdmac->
desc;
1099 sdma_disable_channel(sdmac);
1114 return sdma_config_channel(sdmac);
1129 last_used = chan->
cookie;
1137 static void sdma_issue_pending(
struct dma_chan *chan)
1143 sdma_enable_channel(sdma, sdmac->
channel);
1146 #define SDMA_SCRIPT_ADDRS_ARRAY_SIZE_V1 34
1148 static void sdma_add_scripts(
struct sdma_engine *sdma,
1151 s32 *addr_arr = (
u32 *)addr;
1156 if (addr_arr[i] > 0)
1157 saddr_arr[
i] = addr_arr[
i];
1160 static void sdma_load_firmware(
const struct firmware *
fw,
void *context)
1165 unsigned short *ram_code;
1172 if (fw->
size <
sizeof(*header))
1188 sdma_load_script(sdma, ram_code,
1194 sdma_add_scripts(sdma, addr);
1229 dev_err(sdma->
dev,
"Unknown sdma type %d. aborting\n",
1261 writel_relaxed(0, sdma->
regs + chnenbl_ofs(sdma, i));
1267 ret = sdma_request_channel(&sdma->
channel[0]);
1271 sdma_config_ownership(&sdma->
channel[0],
false,
true,
false);
1286 sdma_set_channel_priority(&sdma->
channel[0], 7);
1296 dev_err(sdma->
dev,
"initialisation failed with %d\n", ret);
1324 if (!iores || irq < 0) {
1331 goto err_request_region;
1355 ret =
request_irq(irq, sdma_int_handler, 0,
"sdma", sdma);
1357 goto err_request_irq;
1386 dma_cookie_init(&sdmac->
chan);
1390 (
unsigned long) sdmac);
1401 ret = sdma_init(sdma);
1409 ret = sdma_get_firmware(sdma, pdata->
fw_name);
1411 dev_warn(&pdev->
dev,
"failed to get firmware from platform data\n");
1421 dev_warn(&pdev->
dev,
"failed to get firmware name\n");
1423 ret = sdma_get_firmware(sdma, fw_name);
1425 dev_warn(&pdev->
dev,
"failed to get firmware from device tree\n");
1431 sdma->
dma_device.device_alloc_chan_resources = sdma_alloc_chan_resources;
1432 sdma->
dma_device.device_free_chan_resources = sdma_free_chan_resources;
1433 sdma->
dma_device.device_tx_status = sdma_tx_status;
1434 sdma->
dma_device.device_prep_slave_sg = sdma_prep_slave_sg;
1435 sdma->
dma_device.device_prep_dma_cyclic = sdma_prep_dma_cyclic;
1436 sdma->
dma_device.device_control = sdma_control;
1437 sdma->
dma_device.device_issue_pending = sdma_issue_pending;
1439 dma_set_max_seg_size(sdma->
dma_device.dev, 65535);
1474 .of_match_table = sdma_dt_ids,
1476 .id_table = sdma_devtypes,
1480 static int __init sdma_module_init(
void)