blob: 700df2236bd1a4463f1615a4afc0d78f2f4c728d [file] [log] [blame]
Tom Rini10e47792018-05-06 17:58:06 -04001// SPDX-License-Identifier: GPL-2.0+
Peter Tyserae7a7d42009-06-30 17:15:40 -05002/*
3 * Copyright 2004,2007,2008 Freescale Semiconductor, Inc.
4 * (C) Copyright 2002, 2003 Motorola Inc.
5 * Xianghua Xiao (X.Xiao@motorola.com)
6 *
7 * (C) Copyright 2000
8 * Wolfgang Denk, DENX Software Engineering, wd@denx.de.
Peter Tyserae7a7d42009-06-30 17:15:40 -05009 */
10
11#include <config.h>
Tom Riniabb9a042024-05-18 20:20:43 -060012#include <common.h>
Peter Tyser46f2b342009-06-30 17:15:42 -050013#include <asm/io.h>
Peter Tyserae7a7d42009-06-30 17:15:40 -050014#include <asm/fsl_dma.h>
15
Peter Tyser6ac51282009-06-30 17:15:43 -050016/* Controller can only transfer 2^26 - 1 bytes at a time */
17#define FSL_DMA_MAX_SIZE (0x3ffffff)
18
Peter Tyser6f33a352009-06-30 17:15:51 -050019#if defined(CONFIG_MPC83xx)
20#define FSL_DMA_MR_DEFAULT (FSL_DMA_MR_CTM_DIRECT | FSL_DMA_MR_DMSEN)
21#else
22#define FSL_DMA_MR_DEFAULT (FSL_DMA_MR_BWC_DIS | FSL_DMA_MR_CTM_DIRECT)
23#endif
24
25
26#if defined(CONFIG_MPC83xx)
Tom Rinid5c3bf22022-10-28 20:27:12 -040027dma83xx_t *dma_base = (void *)(CFG_SYS_MPC83xx_DMA_ADDR);
Peter Tyser6f33a352009-06-30 17:15:51 -050028#elif defined(CONFIG_MPC85xx)
Tom Rinid5c3bf22022-10-28 20:27:12 -040029ccsr_dma_t *dma_base = (void *)(CFG_SYS_MPC85xx_DMA_ADDR);
Peter Tyserae7a7d42009-06-30 17:15:40 -050030#elif defined(CONFIG_MPC86xx)
Peter Tyser46f2b342009-06-30 17:15:42 -050031ccsr_dma_t *dma_base = (void *)(CONFIG_SYS_MPC86xx_DMA_ADDR);
Peter Tyserae7a7d42009-06-30 17:15:40 -050032#else
33#error "Freescale DMA engine not supported on your processor"
34#endif
35
36static void dma_sync(void)
37{
38#if defined(CONFIG_MPC85xx)
39 asm("sync; isync; msync");
40#elif defined(CONFIG_MPC86xx)
41 asm("sync; isync");
42#endif
43}
44
Peter Tyser6f33a352009-06-30 17:15:51 -050045static void out_dma32(volatile unsigned *addr, int val)
46{
47#if defined(CONFIG_MPC83xx)
48 out_le32(addr, val);
49#else
50 out_be32(addr, val);
51#endif
52}
53
54static uint in_dma32(volatile unsigned *addr)
55{
56#if defined(CONFIG_MPC83xx)
57 return in_le32(addr);
58#else
59 return in_be32(addr);
60#endif
61}
62
Peter Tyserae7a7d42009-06-30 17:15:40 -050063static uint dma_check(void) {
64 volatile fsl_dma_t *dma = &dma_base->dma[0];
Peter Tyser46f2b342009-06-30 17:15:42 -050065 uint status;
Peter Tyserae7a7d42009-06-30 17:15:40 -050066
67 /* While the channel is busy, spin */
Peter Tyser46f2b342009-06-30 17:15:42 -050068 do {
Peter Tyser6f33a352009-06-30 17:15:51 -050069 status = in_dma32(&dma->sr);
Peter Tyser46f2b342009-06-30 17:15:42 -050070 } while (status & FSL_DMA_SR_CB);
Peter Tyserae7a7d42009-06-30 17:15:40 -050071
72 /* clear MR[CS] channel start bit */
Peter Tyser6f33a352009-06-30 17:15:51 -050073 out_dma32(&dma->mr, in_dma32(&dma->mr) & ~FSL_DMA_MR_CS);
Peter Tyserae7a7d42009-06-30 17:15:40 -050074 dma_sync();
75
76 if (status != 0)
77 printf ("DMA Error: status = %x\n", status);
78
79 return status;
80}
81
Peter Tyser6f33a352009-06-30 17:15:51 -050082#if !defined(CONFIG_MPC83xx)
Peter Tyserae7a7d42009-06-30 17:15:40 -050083void dma_init(void) {
84 volatile fsl_dma_t *dma = &dma_base->dma[0];
85
Peter Tyser6f33a352009-06-30 17:15:51 -050086 out_dma32(&dma->satr, FSL_DMA_SATR_SREAD_SNOOP);
87 out_dma32(&dma->datr, FSL_DMA_DATR_DWRITE_SNOOP);
88 out_dma32(&dma->sr, 0xffffffff); /* clear any errors */
Peter Tyserae7a7d42009-06-30 17:15:40 -050089 dma_sync();
90}
Peter Tyser6f33a352009-06-30 17:15:51 -050091#endif
Peter Tyserae7a7d42009-06-30 17:15:40 -050092
Peter Tyser86ff89b2009-06-30 17:15:45 -050093int dmacpy(phys_addr_t dest, phys_addr_t src, phys_size_t count) {
Peter Tyserae7a7d42009-06-30 17:15:40 -050094 volatile fsl_dma_t *dma = &dma_base->dma[0];
Peter Tyser6ac51282009-06-30 17:15:43 -050095 uint xfer_size;
Peter Tyserae7a7d42009-06-30 17:15:40 -050096
Peter Tyser6ac51282009-06-30 17:15:43 -050097 while (count) {
Masahiro Yamadab62b39b2014-09-18 13:28:06 +090098 xfer_size = min(FSL_DMA_MAX_SIZE, count);
Peter Tyserae7a7d42009-06-30 17:15:40 -050099
York Sun32447362010-08-27 16:25:50 -0500100 out_dma32(&dma->dar, (u32) (dest & 0xFFFFFFFF));
101 out_dma32(&dma->sar, (u32) (src & 0xFFFFFFFF));
Ira W. Snyder05e94442011-03-01 14:40:55 -0800102#if !defined(CONFIG_MPC83xx)
York Sun32447362010-08-27 16:25:50 -0500103 out_dma32(&dma->satr,
104 in_dma32(&dma->satr) | (u32)((u64)src >> 32));
105 out_dma32(&dma->datr,
106 in_dma32(&dma->datr) | (u32)((u64)dest >> 32));
Ira W. Snyder05e94442011-03-01 14:40:55 -0800107#endif
Peter Tyser6f33a352009-06-30 17:15:51 -0500108 out_dma32(&dma->bcr, xfer_size);
109 dma_sync();
Peter Tyserae7a7d42009-06-30 17:15:40 -0500110
Peter Tyser6f33a352009-06-30 17:15:51 -0500111 /* Prepare mode register */
112 out_dma32(&dma->mr, FSL_DMA_MR_DEFAULT);
Peter Tyser6ac51282009-06-30 17:15:43 -0500113 dma_sync();
114
115 /* Start the transfer */
Peter Tyser6f33a352009-06-30 17:15:51 -0500116 out_dma32(&dma->mr, FSL_DMA_MR_DEFAULT | FSL_DMA_MR_CS);
Peter Tyser6ac51282009-06-30 17:15:43 -0500117
118 count -= xfer_size;
119 src += xfer_size;
120 dest += xfer_size;
121
122 dma_sync();
123
124 if (dma_check())
125 return -1;
126 }
Peter Tyserae7a7d42009-06-30 17:15:40 -0500127
Peter Tyser6ac51282009-06-30 17:15:43 -0500128 return 0;
Peter Tyserae7a7d42009-06-30 17:15:40 -0500129}
Peter Tyser4e928b52009-06-30 17:15:48 -0500130
Peter Tyser6f33a352009-06-30 17:15:51 -0500131/*
132 * 85xx/86xx use dma to initialize SDRAM when !CONFIG_ECC_INIT_VIA_DDRCONTROLLER
Peter Tyser6f33a352009-06-30 17:15:51 -0500133 */
134#if ((!defined CONFIG_MPC83xx && defined(CONFIG_DDR_ECC) && \
Tom Riniddda5642021-08-21 13:50:12 -0400135 !defined(CONFIG_ECC_INIT_VIA_DDRCONTROLLER)))
Tom Rinid73175a2022-12-02 16:42:35 -0500136void dma_meminit(uint size)
Peter Tyser4e928b52009-06-30 17:15:48 -0500137{
138 uint *p = 0;
139 uint i = 0;
140
141 for (*p = 0; p < (uint *)(8 * 1024); p++) {
142 if (((uint)p & 0x1f) == 0)
143 ppcDcbz((ulong)p);
144
Tom Rinid73175a2022-12-02 16:42:35 -0500145 *p = (uint)0xDEADBEEF;
Peter Tyser4e928b52009-06-30 17:15:48 -0500146
147 if (((uint)p & 0x1c) == 0x1c)
148 ppcDcbf((ulong)p);
149 }
150
151 dmacpy(0x002000, 0, 0x002000); /* 8K */
152 dmacpy(0x004000, 0, 0x004000); /* 16K */
153 dmacpy(0x008000, 0, 0x008000); /* 32K */
154 dmacpy(0x010000, 0, 0x010000); /* 64K */
155 dmacpy(0x020000, 0, 0x020000); /* 128K */
156 dmacpy(0x040000, 0, 0x040000); /* 256K */
157 dmacpy(0x080000, 0, 0x080000); /* 512K */
158 dmacpy(0x100000, 0, 0x100000); /* 1M */
159 dmacpy(0x200000, 0, 0x200000); /* 2M */
160 dmacpy(0x400000, 0, 0x400000); /* 4M */
161
162 for (i = 1; i < size / 0x800000; i++)
163 dmacpy((0x800000 * i), 0, 0x800000);
164}
165#endif