blob: d4fa65d2633aeee347b931688690b29004e17cbf [file] [log] [blame]
Jorge Ramirez-Ortiz6919d702018-09-23 09:39:48 +02001/*
2 * Copyright (c) 2015-2018, Renesas Electronics Corporation. All rights reserved.
3 *
4 * SPDX-License-Identifier: BSD-3-Clause
5 */
6
7#include <stdint.h>
8#include <arch_helpers.h>
9#include <string.h>
10#include <mmio.h>
11#include "rcar_def.h"
12#include "cpg_registers.h"
13#include "debug.h"
14#include "rcar_private.h"
15
16/* DMA CHANNEL setting (0/16/32) */
17#define DMA_CH 0
18
19#if (DMA_CH == 0)
20#define SYS_DMAC_BIT ((uint32_t)1U << 19U)
21#define DMA_BASE (0xE6700000U)
22#elif (DMA_CH == 16)
23#define SYS_DMAC_BIT ((uint32_t)1U << 18U)
24#define DMA_BASE (0xE7300000U)
25#elif (DMA_CH == 32)
26#define SYS_DMAC_BIT ((uint32_t)1U << 17U)
27#define DMA_BASE (0xE7320000U)
28#else
29#define SYS_DMAC_BIT ((uint32_t)1U << 19U)
30#define DMA_BASE (0xE6700000U)
31#endif
32
33/* DMA operation */
34#define DMA_DMAOR (DMA_BASE + 0x0060U)
35/* DMA secure control */
36#define DMA_DMASEC (DMA_BASE + 0x0030U)
37/* DMA channel clear */
38#define DMA_DMACHCLR (DMA_BASE + 0x0080U)
39/* DMA source address */
40#define DMA_DMASAR (DMA_BASE + 0x8000U)
41/* DMA destination address */
42#define DMA_DMADAR (DMA_BASE + 0x8004U)
43/* DMA transfer count */
44#define DMA_DMATCR (DMA_BASE + 0x8008U)
45/* DMA channel control */
46#define DMA_DMACHCR (DMA_BASE + 0x800CU)
47/* DMA fixed destination address */
48#define DMA_DMAFIXDAR (DMA_BASE + 0x8014U)
49
50#define DMA_USE_CHANNEL (0x00000001U)
51#define DMAOR_INITIAL (0x0301U)
52#define DMACHCLR_CH_ALL (0x0000FFFFU)
53#define DMAFIXDAR_32BIT_SHIFT (32U)
54#define DMAFIXDAR_DAR_MASK (0x000000FFU)
55#define DMADAR_BOUNDARY_ADDR (0x100000000ULL)
56#define DMATCR_CNT_SHIFT (6U)
57#define DMATCR_MAX (0x00FFFFFFU)
58#define DMACHCR_TRN_MODE (0x00105409U)
59#define DMACHCR_DE_BIT (0x00000001U)
60#define DMACHCR_TE_BIT (0x00000002U)
61#define DMACHCR_CHE_BIT (0x80000000U)
62
63#define DMA_SIZE_UNIT FLASH_TRANS_SIZE_UNIT
64#define DMA_FRACTION_MASK (0xFFU)
65#define DMA_DST_LIMIT (0x10000000000ULL)
66
67/* transfer length limit */
68#define DMA_LENGTH_LIMIT ((DMATCR_MAX * (1U << DMATCR_CNT_SHIFT)) \
69 & ~DMA_FRACTION_MASK)
70
71static void dma_enable(void)
72{
73 mstpcr_write(CPG_SMSTPCR2, CPG_MSTPSR2, SYS_DMAC_BIT);
74}
75
76static void dma_setup(void)
77{
78 mmio_write_16(DMA_DMAOR, 0);
79 mmio_write_32(DMA_DMACHCLR, DMACHCLR_CH_ALL);
80}
81
82static void dma_start(uintptr_t dst, uint32_t src, uint32_t len)
83{
84 mmio_write_16(DMA_DMAOR, DMAOR_INITIAL);
85 mmio_write_32(DMA_DMAFIXDAR, (dst >> DMAFIXDAR_32BIT_SHIFT) &
86 DMAFIXDAR_DAR_MASK);
87 mmio_write_32(DMA_DMADAR, dst & UINT32_MAX);
88 mmio_write_32(DMA_DMASAR, src);
89 mmio_write_32(DMA_DMATCR, len >> DMATCR_CNT_SHIFT);
90 mmio_write_32(DMA_DMASEC, DMA_USE_CHANNEL);
91 mmio_write_32(DMA_DMACHCR, DMACHCR_TRN_MODE);
92}
93
94static void dma_end(void)
95{
96 while ((mmio_read_32(DMA_DMACHCR) & DMACHCR_TE_BIT) == 0) {
97 if ((mmio_read_32(DMA_DMACHCR) & DMACHCR_CHE_BIT) != 0U) {
98 ERROR("BL2: DMA - Channel Address Error\n");
99 panic();
100 break;
101 }
102 }
103 /* DMA transfer Disable */
104 mmio_clrbits_32(DMA_DMACHCR, DMACHCR_DE_BIT);
105 while ((mmio_read_32(DMA_DMACHCR) & DMACHCR_DE_BIT) != 0)
106 ;
107
108 mmio_write_32(DMA_DMASEC, 0);
109 mmio_write_16(DMA_DMAOR, 0);
110 mmio_write_32(DMA_DMACHCLR, DMA_USE_CHANNEL);
111}
112
113void rcar_dma_exec(uintptr_t dst, uint32_t src, uint32_t len)
114{
115 uint32_t dma_len = len;
116
117 if (len & DMA_FRACTION_MASK)
118 dma_len = (len + DMA_SIZE_UNIT) & ~DMA_FRACTION_MASK;
119
120 if (!dma_len || dma_len > DMA_LENGTH_LIMIT) {
121 ERROR("BL2: DMA - size invalid, length (0x%x)\n", dma_len);
122 panic();
123 }
124
125 if (src & DMA_FRACTION_MASK) {
126 ERROR("BL2: DMA - source address invalid (0x%x), "
127 "length (0x%x)\n", src, dma_len);
128 panic();
129 }
130
131 if ((dst & UINT32_MAX) + dma_len > DMADAR_BOUNDARY_ADDR ||
132 (dst + dma_len > DMA_DST_LIMIT) ||
133 (dst & DMA_FRACTION_MASK)) {
134 ERROR("BL2: DMA - destination address invalid (0x%lx), "
135 "length (0x%x)\n", dst, dma_len);
136 panic();
137 }
138
139 dma_start(dst, src, dma_len);
140 dma_end();
141}
142
143void rcar_dma_init(void)
144{
145 dma_enable();
146 dma_setup();
147}