summaryrefslogtreecommitdiffstats
path: root/libopencm3/lib/stm32/f4/fmc.c
blob: 0de8a4ad668d41b3308a389ebf42851a9f0f005d (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
/*
 *
 * This file is part of the libopencm3 project.
 *
 * This library is free software: you can redistribute it and/or modify
 * it under the terms of the GNU Lesser General Public License as published by
 * the Free Software Foundation, either version 3 of the License, or
 * (at your option) any later version.
 *
 * This library is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU Lesser General Public License for more details.
 *
 * You should have received a copy of the GNU Lesser General Public License
 * along with this library.  If not, see <http://www.gnu.org/licenses/>.
 */

/* Utility functions for the SDRAM component of the FMC */

#include <stdint.h>
#include <libopencm3/stm32/fsmc.h>

/*
 * Install various timing values into the correct place in the
 * SDRAM Timing Control Register format.
 *
 * Note that the register is 'zero' based to save bits so 1 cycle
 * is stored as '0'. This command takes actual cycles and adjusts
 * by subtracting 1.
 */
uint32_t
sdram_timing(struct sdram_timing *t) {
	uint32_t result;

	result = 0;
	result |= ((t->trcd - 1) & 0xf) * FMC_SDTR_TRCD_SHIFT;
	result |= ((t->trp - 1) & 0xf) * FMC_SDTR_TRP_SHIFT;
	result |= ((t->twr - 1) & 0xf) * FMC_SDTR_TWR_SHIFT;
	result |= ((t->trc - 1) & 0xf) * FMC_SDTR_TRC_SHIFT;
	result |= ((t->tras - 1) & 0xf) * FMC_SDTR_TRAS_SHIFT;
	result |= ((t->txsr - 1) & 0xf) * FMC_SDTR_TXSR_SHIFT;
	result |= ((t->tmrd - 1) & 0xf) * FMC_SDTR_TMRD_SHIFT;
	return result;
}

/*
 * Send a command to the SDRAM controller, wait until it is not
 * busy before sending. This allows you to chain sending commands
 * and the code will pause as needed between them.
 */
void
sdram_command(enum fmc_sdram_bank bank,
              enum fmc_sdram_command cmd, int autorefresh, int modereg) {
	uint32_t tmp_reg = 0;

	switch (bank) {
	case SDRAM_BANK1:
		tmp_reg = FMC_SDCMR_CTB1;
		break;
	case SDRAM_BANK2:
		tmp_reg = FMC_SDCMR_CTB2;
		break;
	case SDRAM_BOTH_BANKS:
		tmp_reg = FMC_SDCMR_CTB1 | FMC_SDCMR_CTB2;
		break;
	}
	tmp_reg |= autorefresh * FMC_SDCMR_NRFS_SHIFT;
	tmp_reg |= modereg * FMC_SDCMR_MRD_SHIFT;
	switch (cmd) {
		case SDRAM_CLK_CONF:
			tmp_reg |= FMC_SDCMR_MODE_CLOCK_CONFIG_ENA;
			break;
		case SDRAM_AUTO_REFRESH:
			tmp_reg |= FMC_SDCMR_MODE_AUTO_REFRESH;
			break;
		case SDRAM_LOAD_MODE:
			tmp_reg |= FMC_SDCMR_MODE_LOAD_MODE_REGISTER;
			break;
		case SDRAM_PALL:
			tmp_reg |= FMC_SDCMR_MODE_PALL;
			break;
		case SDRAM_SELF_REFRESH:
			tmp_reg |= FMC_SDCMR_MODE_SELF_REFRESH;
			break;
		case SDRAM_POWER_DOWN:
			tmp_reg |= FMC_SDCMR_MODE_POWER_DOWN;
			break;
		case SDRAM_NORMAL:
		default:
			break;
	}

	/* Wait for the next chance to talk to the controller */
	while (FMC_SDSR & FMC_SDSR_BUSY) ;

	/* Send the next command */
	FMC_SDCMR = tmp_reg;
}