19 #include <linux/bitops.h>
28 #include <linux/module.h>
33 #include <linux/slab.h>
38 #define TEGRA_APBDMA_GENERAL 0x0
39 #define TEGRA_APBDMA_GENERAL_ENABLE BIT(31)
41 #define TEGRA_APBDMA_CONTROL 0x010
42 #define TEGRA_APBDMA_IRQ_MASK 0x01c
43 #define TEGRA_APBDMA_IRQ_MASK_SET 0x020
46 #define TEGRA_APBDMA_CHAN_CSR 0x00
47 #define TEGRA_APBDMA_CSR_ENB BIT(31)
48 #define TEGRA_APBDMA_CSR_IE_EOC BIT(30)
49 #define TEGRA_APBDMA_CSR_HOLD BIT(29)
50 #define TEGRA_APBDMA_CSR_DIR BIT(28)
51 #define TEGRA_APBDMA_CSR_ONCE BIT(27)
52 #define TEGRA_APBDMA_CSR_FLOW BIT(21)
53 #define TEGRA_APBDMA_CSR_REQ_SEL_SHIFT 16
54 #define TEGRA_APBDMA_CSR_WCOUNT_MASK 0xFFFC
57 #define TEGRA_APBDMA_CHAN_STATUS 0x004
58 #define TEGRA_APBDMA_STATUS_BUSY BIT(31)
59 #define TEGRA_APBDMA_STATUS_ISE_EOC BIT(30)
60 #define TEGRA_APBDMA_STATUS_HALT BIT(29)
61 #define TEGRA_APBDMA_STATUS_PING_PONG BIT(28)
62 #define TEGRA_APBDMA_STATUS_COUNT_SHIFT 2
63 #define TEGRA_APBDMA_STATUS_COUNT_MASK 0xFFFC
66 #define TEGRA_APBDMA_CHAN_AHBPTR 0x010
69 #define TEGRA_APBDMA_CHAN_AHBSEQ 0x14
70 #define TEGRA_APBDMA_AHBSEQ_INTR_ENB BIT(31)
71 #define TEGRA_APBDMA_AHBSEQ_BUS_WIDTH_8 (0 << 28)
72 #define TEGRA_APBDMA_AHBSEQ_BUS_WIDTH_16 (1 << 28)
73 #define TEGRA_APBDMA_AHBSEQ_BUS_WIDTH_32 (2 << 28)
74 #define TEGRA_APBDMA_AHBSEQ_BUS_WIDTH_64 (3 << 28)
75 #define TEGRA_APBDMA_AHBSEQ_BUS_WIDTH_128 (4 << 28)
76 #define TEGRA_APBDMA_AHBSEQ_DATA_SWAP BIT(27)
77 #define TEGRA_APBDMA_AHBSEQ_BURST_1 (4 << 24)
78 #define TEGRA_APBDMA_AHBSEQ_BURST_4 (5 << 24)
79 #define TEGRA_APBDMA_AHBSEQ_BURST_8 (6 << 24)
80 #define TEGRA_APBDMA_AHBSEQ_DBL_BUF BIT(19)
81 #define TEGRA_APBDMA_AHBSEQ_WRAP_SHIFT 16
82 #define TEGRA_APBDMA_AHBSEQ_WRAP_NONE 0
85 #define TEGRA_APBDMA_CHAN_APBPTR 0x018
88 #define TEGRA_APBDMA_CHAN_APBSEQ 0x01c
89 #define TEGRA_APBDMA_APBSEQ_BUS_WIDTH_8 (0 << 28)
90 #define TEGRA_APBDMA_APBSEQ_BUS_WIDTH_16 (1 << 28)
91 #define TEGRA_APBDMA_APBSEQ_BUS_WIDTH_32 (2 << 28)
92 #define TEGRA_APBDMA_APBSEQ_BUS_WIDTH_64 (3 << 28)
93 #define TEGRA_APBDMA_APBSEQ_BUS_WIDTH_128 (4 << 28)
94 #define TEGRA_APBDMA_APBSEQ_DATA_SWAP BIT(27)
95 #define TEGRA_APBDMA_APBSEQ_WRAP_WORD_1 (1 << 16)
101 #define TEGRA_APBDMA_BURST_COMPLETE_TIME 20
104 #define TEGRA_APBDMA_CHANNEL_BASE_ADD_OFFSET 0x1000
107 #define TEGRA_APBDMA_CHANNEL_REGISTER_SIZE 0x20
252 static int tegra_dma_runtime_suspend(
struct device *
dev);
253 static int tegra_dma_runtime_resume(
struct device *
dev);
266 if (async_tx_test_ack(&dma_desc->
txd)) {
268 spin_unlock_irqrestore(&tdc->
lock, flags);
273 spin_unlock_irqrestore(&tdc->
lock, flags);
276 dma_desc = kzalloc(
sizeof(*dma_desc),
GFP_ATOMIC);
278 dev_err(tdc2dev(tdc),
"dma_desc alloc failed\n");
283 dma_desc->
txd.tx_submit = tegra_dma_tx_submit;
284 dma_desc->
txd.flags = 0;
294 if (!list_empty(&dma_desc->
tx_list))
297 spin_unlock_irqrestore(&tdc->
lock, flags);
311 spin_unlock_irqrestore(&tdc->
lock, flags);
314 spin_unlock_irqrestore(&tdc->
lock, flags);
318 dev_err(tdc2dev(tdc),
"sg_req alloc failed\n");
322 static int tegra_dma_slave_config(
struct dma_chan *
dc,
328 dev_err(tdc2dev(tdc),
"Configuration not allowed\n");
338 bool wait_for_burst_complete)
344 if (wait_for_burst_complete)
373 dev_dbg(tdc2dev(tdc),
"%s():clearing interrupt\n", __func__);
411 tegra_dma_global_pause(tdc,
false);
418 if (status & TEGRA_APBDMA_STATUS_ISE_EOC) {
420 "Skipping new configuration as interrupt is pending\n");
421 tegra_dma_global_resume(tdc);
432 tegra_dma_global_resume(tdc);
444 tegra_dma_start(tdc, sg_req);
461 tegra_dma_configure_for_next(tdc, hnsgreq);
501 dev_err(tdc2dev(tdc),
"Dma is running without req\n");
514 dev_err(tdc2dev(tdc),
"Error in dma transfer, aborting dma\n");
515 tegra_dma_abort_all(tdc);
521 tdc_configure_next_head_desc(tdc);
539 dma_cookie_complete(&dma_desc->
txd);
551 tdc_start_head_req(tdc);
575 st = handle_continuous_head_request(tdc, sgreq, to_terminate);
582 static void tegra_dma_tasklet(
unsigned long data)
592 while (!list_empty(&tdc->
cb_desc)) {
596 callback = dma_desc->
txd.callback;
597 callback_param = dma_desc->
txd.callback_param;
600 spin_unlock_irqrestore(&tdc->
lock, flags);
601 while (cb_count-- && callback)
605 spin_unlock_irqrestore(&tdc->
lock, flags);
617 if (status & TEGRA_APBDMA_STATUS_ISE_EOC) {
620 tasklet_schedule(&tdc->
tasklet);
621 spin_unlock_irqrestore(&tdc->
lock, flags);
625 spin_unlock_irqrestore(&tdc->
lock, flags);
627 "Interrupt already served status 0x%08lx\n", status);
640 cookie = dma_cookie_assign(&dma_desc->
txd);
642 spin_unlock_irqrestore(&tdc->
lock, flags);
646 static void tegra_dma_issue_pending(
struct dma_chan *dc)
653 dev_err(tdc2dev(tdc),
"No DMA request\n");
657 tdc_start_head_req(tdc);
666 tdc_configure_next_head_desc(tdc);
670 spin_unlock_irqrestore(&tdc->
lock, flags);
674 static void tegra_dma_terminate_all(
struct dma_chan *dc)
685 spin_unlock_irqrestore(&tdc->
lock, flags);
693 tegra_dma_global_pause(tdc,
true);
696 if (status & TEGRA_APBDMA_STATUS_ISE_EOC) {
697 dev_dbg(tdc2dev(tdc),
"%s():handling isr\n", __func__);
702 was_busy = tdc->
busy;
708 sgreq->
dma_desc->bytes_transferred +=
709 get_current_xferred_count(tdc, sgreq, status);
711 tegra_dma_global_resume(tdc);
714 tegra_dma_abort_all(tdc);
716 while (!list_empty(&tdc->
cb_desc)) {
722 spin_unlock_irqrestore(&tdc->
lock, flags);
737 ret = dma_cookie_status(dc, cookie, txstate);
739 dma_set_residue(txstate, 0);
740 spin_unlock_irqrestore(&tdc->
lock, flags);
746 if (dma_desc->
txd.cookie == cookie) {
750 dma_set_residue(txstate, residual);
752 spin_unlock_irqrestore(&tdc->
lock, flags);
760 if (dma_desc->
txd.cookie == cookie) {
764 dma_set_residue(txstate, residual);
766 spin_unlock_irqrestore(&tdc->
lock, flags);
771 dev_dbg(tdc2dev(tdc),
"cookie %d does not found\n", cookie);
772 spin_unlock_irqrestore(&tdc->
lock, flags);
781 return tegra_dma_slave_config(dc,
785 tegra_dma_terminate_all(dc);
809 "slave bw is not supported, using 32bits\n");
824 burst_byte = burst_size * slave_bw;
825 burst_ahb_width = burst_byte / 4;
828 if (!burst_ahb_width) {
831 else if ((len >> 4) & 0x1)
836 if (burst_ahb_width < 4)
838 else if (burst_ahb_width < 8)
846 unsigned long *apb_seq,
unsigned long *csr,
unsigned int *burst_size,
853 *apb_seq = get_bus_width(tdc, tdc->
dma_sconfig.dst_addr_width);
861 *apb_seq = get_bus_width(tdc, tdc->
dma_sconfig.src_addr_width);
868 dev_err(tdc2dev(tdc),
"Dma direction is not supported\n");
883 unsigned long csr, ahb_seq, apb_ptr, apb_seq;
891 dev_err(tdc2dev(tdc),
"dma channel is not configured\n");
895 dev_err(tdc2dev(tdc),
"Invalid segment length %d\n", sg_len);
899 ret = get_transfer_param(tdc, direction, &apb_ptr, &apb_seq, &csr,
900 &burst_size, &slave_bw);
904 INIT_LIST_HEAD(&req_list);
918 dma_desc = tegra_dma_desc_get(tdc);
920 dev_err(tdc2dev(tdc),
"Dma descriptors not available\n");
923 INIT_LIST_HEAD(&dma_desc->
tx_list);
924 INIT_LIST_HEAD(&dma_desc->
cb_node);
937 if ((len & 3) || (mem & 3) ||
938 (len > tdc->
tdma->chip_data->max_dma_count)) {
940 "Dma length/memory address is not supported\n");
941 tegra_dma_desc_put(tdc, dma_desc);
945 sg_req = tegra_dma_sg_req_get(tdc);
947 dev_err(tdc2dev(tdc),
"Dma sg-req not available\n");
948 tegra_dma_desc_put(tdc, dma_desc);
952 ahb_seq |= get_burst_size(tdc, burst_size, slave_bw, len);
955 sg_req->
ch_regs.apb_ptr = apb_ptr;
957 sg_req->
ch_regs.csr = csr | ((len - 4) & 0xFFFC);
958 sg_req->
ch_regs.apb_seq = apb_seq;
959 sg_req->
ch_regs.ahb_seq = ahb_seq;
980 dev_err(tdc2dev(tdc),
"DMA configured in cyclic mode\n");
981 tegra_dma_desc_put(tdc, dma_desc);
986 return &dma_desc->
txd;
992 unsigned long flags,
void *context)
997 unsigned long csr, ahb_seq, apb_ptr, apb_seq;
1005 if (!buf_len || !period_len) {
1006 dev_err(tdc2dev(tdc),
"Invalid buffer/period len\n");
1011 dev_err(tdc2dev(tdc),
"DMA slave is not configured\n");
1022 dev_err(tdc2dev(tdc),
"Request not allowed when dma running\n");
1030 if (buf_len % period_len) {
1031 dev_err(tdc2dev(tdc),
"buf_len is not multiple of period_len\n");
1036 if ((len & 3) || (buf_addr & 3) ||
1037 (len > tdc->
tdma->chip_data->max_dma_count)) {
1038 dev_err(tdc2dev(tdc),
"Req len/mem address is not correct\n");
1042 ret = get_transfer_param(tdc, direction, &apb_ptr, &apb_seq, &csr,
1043 &burst_size, &slave_bw);
1058 dma_desc = tegra_dma_desc_get(tdc);
1060 dev_err(tdc2dev(tdc),
"not enough descriptors available\n");
1064 INIT_LIST_HEAD(&dma_desc->
tx_list);
1065 INIT_LIST_HEAD(&dma_desc->
cb_node);
1070 remain_len = buf_len;
1073 while (remain_len) {
1074 sg_req = tegra_dma_sg_req_get(tdc);
1076 dev_err(tdc2dev(tdc),
"Dma sg-req not available\n");
1077 tegra_dma_desc_put(tdc, dma_desc);
1081 ahb_seq |= get_burst_size(tdc, burst_size, slave_bw, len);
1082 sg_req->
ch_regs.apb_ptr = apb_ptr;
1084 sg_req->
ch_regs.csr = csr | ((len - 4) & 0xFFFC);
1085 sg_req->
ch_regs.apb_seq = apb_seq;
1086 sg_req->
ch_regs.ahb_seq = ahb_seq;
1098 dma_desc->
txd.flags = 0;
1105 tdc->
isr_handler = handle_cont_sngl_cycle_dma_done;
1109 dev_err(tdc2dev(tdc),
"DMA configuration conflict\n");
1110 tegra_dma_desc_put(tdc, dma_desc);
1115 return &dma_desc->
txd;
1118 static int tegra_dma_alloc_chan_resources(
struct dma_chan *dc)
1126 ret = clk_prepare_enable(tdma->
dma_clk);
1128 dev_err(tdc2dev(tdc),
"clk_prepare_enable failed: %d\n", ret);
1132 static void tegra_dma_free_chan_resources(
struct dma_chan *dc)
1141 unsigned long flags;
1143 INIT_LIST_HEAD(&dma_desc_list);
1144 INIT_LIST_HEAD(&sg_req_list);
1146 dev_dbg(tdc2dev(tdc),
"Freeing channel %d\n", tdc->
id);
1149 tegra_dma_terminate_all(dc);
1153 list_splice_init(&tdc->
free_sg_req, &sg_req_list);
1155 INIT_LIST_HEAD(&tdc->
cb_desc);
1157 spin_unlock_irqrestore(&tdc->
lock, flags);
1159 while (!list_empty(&dma_desc_list)) {
1166 while (!list_empty(&sg_req_list)) {
1171 clk_disable_unprepare(tdma->
dma_clk);
1177 .max_dma_count = 1024
UL * 64,
1180 #if defined(CONFIG_OF)
1184 .max_dma_count = 1024
UL * 64,
1189 .compatible =
"nvidia,tegra30-apbdma",
1190 .data = &tegra30_dma_chip_data,
1192 .compatible =
"nvidia,tegra20-apbdma",
1193 .data = &tegra20_dma_chip_data,
1208 if (pdev->
dev.of_node) {
1213 dev_err(&pdev->
dev,
"Error: No device match found\n");
1216 cdata = match->
data;
1219 cdata = &tegra20_dma_chip_data;
1225 dev_err(&pdev->
dev,
"Error: memory allocation failed\n");
1231 platform_set_drvdata(pdev, tdma);
1235 dev_err(&pdev->
dev,
"No mem resource for DMA\n");
1242 "Cannot request memregion/iomap dma address\n");
1248 dev_err(&pdev->
dev,
"Error: Missing controller clock\n");
1249 return PTR_ERR(tdma->
dma_clk);
1255 if (!pm_runtime_enabled(&pdev->
dev)) {
1256 ret = tegra_dma_runtime_resume(&pdev->
dev);
1258 dev_err(&pdev->
dev,
"dma_runtime_resume failed %d\n",
1260 goto err_pm_disable;
1265 ret = clk_prepare_enable(tdma->
dma_clk);
1267 dev_err(&pdev->
dev,
"clk_prepare_enable failed: %d\n", ret);
1268 goto err_pm_disable;
1281 clk_disable_unprepare(tdma->
dma_clk);
1283 INIT_LIST_HEAD(&tdma->
dma_dev.channels);
1293 dev_err(&pdev->
dev,
"No irq resource for chan %d\n", i);
1298 ret = devm_request_irq(&pdev->
dev, tdc->
irq,
1299 tegra_dma_isr, 0, tdc->
name, tdc);
1302 "request_irq failed with err %d channel %d\n",
1315 (
unsigned long)tdc);
1321 INIT_LIST_HEAD(&tdc->
cb_desc);
1329 tdma->
dma_dev.device_alloc_chan_resources =
1330 tegra_dma_alloc_chan_resources;
1331 tdma->
dma_dev.device_free_chan_resources =
1332 tegra_dma_free_chan_resources;
1333 tdma->
dma_dev.device_prep_slave_sg = tegra_dma_prep_slave_sg;
1335 tdma->
dma_dev.device_control = tegra_dma_device_control;
1336 tdma->
dma_dev.device_tx_status = tegra_dma_tx_status;
1337 tdma->
dma_dev.device_issue_pending = tegra_dma_issue_pending;
1342 "Tegra20 APB DMA driver registration failed %d\n", ret);
1346 dev_info(&pdev->
dev,
"Tegra20 APB DMA driver register %d channels\n",
1357 pm_runtime_disable(&pdev->
dev);
1358 if (!pm_runtime_status_suspended(&pdev->
dev))
1359 tegra_dma_runtime_suspend(&pdev->
dev);
1365 struct tegra_dma *tdma = platform_get_drvdata(pdev);
1371 for (i = 0; i < tdma->
chip_data->nr_channels; ++
i) {
1376 pm_runtime_disable(&pdev->
dev);
1377 if (!pm_runtime_status_suspended(&pdev->
dev))
1378 tegra_dma_runtime_suspend(&pdev->
dev);
1383 static int tegra_dma_runtime_suspend(
struct device *
dev)
1386 struct tegra_dma *tdma = platform_get_drvdata(pdev);
1388 clk_disable_unprepare(tdma->
dma_clk);
1392 static int tegra_dma_runtime_resume(
struct device *
dev)
1395 struct tegra_dma *tdma = platform_get_drvdata(pdev);
1398 ret = clk_prepare_enable(tdma->
dma_clk);
1400 dev_err(dev,
"clk_enable failed: %d\n", ret);
1406 static const struct dev_pm_ops tegra_dma_dev_pm_ops __devinitconst = {
1407 #ifdef CONFIG_PM_RUNTIME
1408 .runtime_suspend = tegra_dma_runtime_suspend,
1409 .runtime_resume = tegra_dma_runtime_resume,
1415 .name =
"tegra-apbdma",
1417 .pm = &tegra_dma_dev_pm_ops,
1420 .probe = tegra_dma_probe,