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