12
#include <drivers/common/memory.h>
13
#include <drivers/fpga.h>
14
#include <embox/unit.h>
15
#include <framework/mod/options.h>
16
#include <kernel/irq.h>
17
#include <kernel/panic.h>
21
#define SOCFPGA_L3_REGS 0xFF800000
22
#define SOCFPGA_L3_REMAP (SOCFPGA_L3_REGS + 0x00)
23
#define SOCFPGA_SDRAM_REGS 0xFFC20000
24
#define SOCFPGA_SDRAM_CONFIG (SOCFPGA_SDRAM_REGS + 0x5080)
25
#define SOCFPGA_RESETMGR_REGS 0xFFD05000
26
#define SOCFPGA_RESETMGR_AXI_BRIDGE (SOCFPGA_RESETMGR_REGS + 0x1C)
27
#define SOCFPGA_SYSMGR_REGS 0xFFD08000
28
#define SOCFPGA_SYSMGR_INTF (SOCFPGA_SYSMGR_REGS + 0x28)
30
#define SOCFPGA_MGR_STAT 0x00
31
# define SOCFPGA_MODE_MASK 0x7
32
# define SOCFPGA_MODE_OFF 0x0
33
# define SOCFPGA_MODE_RESET 0x1
34
# define SOCFPGA_MODE_CONFIG 0x2
35
# define SOCFPGA_MODE_INIT 0x3
36
# define SOCFPGA_MODE_USER 0x4
37
# define SOCFPGA_MODE_UNKNOWN 0x5
38
#define SOCFPGA_MGR_CTRL 0x04
39
# define SOCFPGA_CTRL_EN (1 << 0)
40
# define SOCFPGA_CTRL_NCE (1 << 1)
41
# define SOCFPGA_CTRL_NCONFIGPULL (1 << 2)
42
# define SOCFPGA_CTRL_CDRATIO_OFFT 6
43
# define SOCFPGA_CTRL_AXICFEN (1 << 8)
44
# define SOCFPGA_CTRL_CFGWDTH (1 << 9)
45
#define SOCFPGA_MGR_DCLKCNT 0x08
46
#define SOCFPGA_MGR_DCLKSTAT 0x0C
47
# define SOCFPGA_DCLKSTAT_DONE (1 << 0)
48
#define SOCFPGA_MGR_GPO 0x10
49
#define SOCFPGA_MGR_GPI 0x14
50
#define SOCFPGA_MGR_MISCI 0x18
52
#define SOCFPGA_MGR_GPIO_INTEN 0x830
53
#define SOCFPGA_MGR_GPIO_INTMASK 0x834
54
#define SOCFPGA_MGR_GPIO_INTTYPE_LEVEL 0x838
55
#define SOCFPGA_MGR_GPIO_INT_POLARITY 0x83c
56
#define SOCFPGA_MGR_GPIO_INTSTATUS 0x840
57
#define SOCFPGA_MGR_GPIO_RAW_INTSTATUS 0x848
58
#define SOCFPGA_MGR_GPIO_PORTA_EOI 0x84c
59
#define SOCFPGA_MGR_GPIO_EXT_PORTA 0x850
60
# define SOCFPGA_GPIO_EXT_PORTA_NSP (1 << 9)
61
# define SOCFPGA_GPIO_EXT_PORTA_CDP (1 << 10)
62
#define SOCFPGA_MGR_GPIO_LS_SYNC 0x860
63
#define SOCFPGA_MGR_GPIO_VER_ID_CODE 0x86C
64
#define SOCFPGA_MGR_GPIO_CONFIG_REG2 0x870
65
#define SOCFPGA_MGR_GPIO_CONFIG_REG1 0x874
72
static void bridge_enable_handoff(void) {
78
static const int (*fpga2sdram_apply)(void) = (void *) 0x3ff7d52c;
79
REG32_STORE(SOCFPGA_SYSMGR_INTF, 0);
81
REG32_STORE(SOCFPGA_SDRAM_CONFIG, 0);
82
REG32_STORE(SOCFPGA_RESETMGR_AXI_BRIDGE, 0);
83
REG32_STORE(SOCFPGA_L3_REMAP, 0x19);
89
uintptr_t mgr_data_addr;
92
static void socfpga_or(struct socfpga *socfpga, int offt, uint32_t val) {
93
uint32_t tmp = REG32_LOAD(socfpga->mgr_base + offt);
95
REG32_STORE(socfpga->mgr_base + offt, val | tmp);
98
static void socfpga_clear(struct socfpga *socfpga, int offt, uint32_t val) {
99
uint32_t tmp = REG32_LOAD(socfpga->mgr_base + offt);
101
REG32_STORE(socfpga->mgr_base + offt, tmp & ~val);
104
static uint32_t socfpga_read(struct socfpga *socfpga, int offt) {
105
return REG32_LOAD(socfpga->mgr_base + offt);
108
static void socfpga_write(struct socfpga *socfpga, int offt, uint32_t val) {
109
REG32_STORE(socfpga->mgr_base + offt, val);
112
static void socfpga_data_write(struct socfpga *socfpga, uint32_t val) {
113
REG32_STORE(socfpga->mgr_data_addr, val);
116
static void socfpga_regdump(struct socfpga *socfpga) {
117
log_debug(" STAT %08x",
118
socfpga_read(socfpga, SOCFPGA_MGR_STAT));
119
log_debug(" CTRL %08x",
120
socfpga_read(socfpga, SOCFPGA_MGR_CTRL));
121
log_debug(" DCLKCNT %08x",
122
socfpga_read(socfpga, SOCFPGA_MGR_DCLKCNT));
123
log_debug(" DCLKSTAT %08x",
124
socfpga_read(socfpga, SOCFPGA_MGR_DCLKSTAT));
125
log_debug(" GPO %08x",
126
socfpga_read(socfpga, SOCFPGA_MGR_GPO));
127
log_debug(" GPI %08x",
128
socfpga_read(socfpga, SOCFPGA_MGR_GPI));
129
log_debug(" MISCI %08x",
130
socfpga_read(socfpga, SOCFPGA_MGR_MISCI));
131
log_debug(" GPIO_INTEN %08x",
132
socfpga_read(socfpga, SOCFPGA_MGR_GPIO_INTEN));
133
log_debug(" GPIO_INTMASK %08x",
134
socfpga_read(socfpga, SOCFPGA_MGR_GPIO_INTMASK));
135
log_debug(" GPIO_INTTYPE_LEV %08x",
136
socfpga_read(socfpga, SOCFPGA_MGR_GPIO_INTTYPE_LEVEL));
137
log_debug(" GPIO_INT_POLARIT %08x",
138
socfpga_read(socfpga, SOCFPGA_MGR_GPIO_INT_POLARITY));
139
log_debug(" GPIO_INTSTATUS %08x",
140
socfpga_read(socfpga, SOCFPGA_MGR_GPIO_INTSTATUS));
141
log_debug(" GPIO_RAW_INTSTAT %08x",
142
socfpga_read(socfpga, SOCFPGA_MGR_GPIO_RAW_INTSTATUS));
143
log_debug(" GPIO_PORTA_EOI %08x",
144
socfpga_read(socfpga, SOCFPGA_MGR_GPIO_PORTA_EOI));
145
log_debug(" GPIO_EXT_PORTA %08x",
146
socfpga_read(socfpga, SOCFPGA_MGR_GPIO_EXT_PORTA));
147
log_debug(" GPIO_LS_SYNC %08x",
148
socfpga_read(socfpga, SOCFPGA_MGR_GPIO_LS_SYNC));
149
log_debug(" GPIO_VER_ID_CODE %08x",
150
socfpga_read(socfpga, SOCFPGA_MGR_GPIO_VER_ID_CODE));
151
log_debug(" GPIO_CONFIG_REG2 %08x",
152
socfpga_read(socfpga, SOCFPGA_MGR_GPIO_CONFIG_REG2));
153
log_debug(" GPIO_CONFIG_REG1 %08x",
154
socfpga_read(socfpga, SOCFPGA_MGR_GPIO_CONFIG_REG1));
157
static uint32_t socfpga_mode(struct socfpga *socfpga) {
159
socfpga_read(socfpga, SOCFPGA_MGR_STAT) & SOCFPGA_MODE_MASK;
161
case SOCFPGA_MODE_MASK:
162
case SOCFPGA_MODE_OFF:
163
case SOCFPGA_MODE_RESET:
164
case SOCFPGA_MODE_CONFIG:
165
case SOCFPGA_MODE_INIT:
166
case SOCFPGA_MODE_USER:
167
case SOCFPGA_MODE_UNKNOWN:
170
log_error("incorrect socfpga state: 0x%x", ret);
175
static int socfpga_mode_wait(struct socfpga *socfpga, uint32_t mode) {
179
if (socfpga_mode(socfpga) == mode) {
187
log_error("FPGA reset timeout!");
194
static int socfpga_reset(struct socfpga *socfpga) {
198
socfpga_or(socfpga, SOCFPGA_MGR_CTRL,
202
socfpga_or(socfpga, SOCFPGA_MGR_CTRL,
203
SOCFPGA_CTRL_NCONFIGPULL);
205
if (socfpga_mode_wait(socfpga, SOCFPGA_MODE_RESET)) {
210
socfpga_clear(socfpga, SOCFPGA_MGR_CTRL,
211
SOCFPGA_CTRL_NCONFIGPULL);
220
static int socfpga_config_init(struct fpga *fpga) {
221
struct socfpga *socfpga = fpga->priv;
228
tmp = socfpga_read(socfpga, SOCFPGA_MGR_CTRL);
229
tmp &= ~SOCFPGA_CTRL_CDRATIO_OFFT;
230
tmp &= ~SOCFPGA_CTRL_CFGWDTH;
232
socfpga_write(socfpga, SOCFPGA_MGR_CTRL, tmp);
234
err = socfpga_reset(socfpga);
239
err = socfpga_mode_wait(socfpga, SOCFPGA_MODE_CONFIG);
245
socfpga_or(socfpga, SOCFPGA_MGR_CTRL, SOCFPGA_CTRL_AXICFEN);
253
static int socfpga_config_write(struct fpga *fpga, const uint8_t *buf, size_t len) {
254
struct socfpga *socfpga = fpga->priv;
255
uint32_t *buf32 = (uint32_t *) buf;
258
while (len > sizeof(uint32_t)) {
259
socfpga_data_write(socfpga, *buf32);
266
memcpy(buf32, &tmp, len);
267
socfpga_data_write(socfpga, tmp);
276
static int socfpga_config_complete(struct fpga *fpga) {
277
struct socfpga *socfpga = fpga->priv;
279
int timeout = TIMEOUT;
283
while (timeout-- >= 0) {
284
tmp = socfpga_read(socfpga, SOCFPGA_MGR_GPIO_EXT_PORTA);
285
if ((tmp & SOCFPGA_GPIO_EXT_PORTA_NSP) &&
286
(tmp & SOCFPGA_GPIO_EXT_PORTA_CDP)) {
293
log_error("Timeout waiting for config write finish!");
298
socfpga_clear(socfpga, SOCFPGA_MGR_CTRL, SOCFPGA_CTRL_AXICFEN);
303
socfpga_write(socfpga, SOCFPGA_MGR_DCLKSTAT,
304
SOCFPGA_DCLKSTAT_DONE);
305
socfpga_write(socfpga, SOCFPGA_MGR_DCLKCNT, 4);
307
while (timeout-- >= 0) {
308
tmp = socfpga_read(socfpga, SOCFPGA_MGR_DCLKSTAT);
309
if (tmp & SOCFPGA_DCLKSTAT_DONE) {
315
log_error("Timeout waiting for dclk!");
319
err = socfpga_mode_wait(socfpga, SOCFPGA_MODE_USER);
321
log_error("Timeout switching to User Mode!");
327
socfpga_clear(socfpga, SOCFPGA_MGR_CTRL, SOCFPGA_CTRL_EN);
332
static struct fpga_ops socfpga_fpga_ops = {
333
.config_init = socfpga_config_init,
334
.config_write = socfpga_config_write,
335
.config_complete = socfpga_config_complete,
338
#define FPGA_MGR_BASE OPTION_GET(NUMBER, mgr_base_addr)
339
#define FPGA_MGR_DATA OPTION_GET(NUMBER, mgr_data_addr)
340
static struct socfpga socfpga0 = {
341
.mgr_base = FPGA_MGR_BASE,
342
.mgr_data_addr = FPGA_MGR_DATA,
344
PERIPH_MEMORY_DEFINE(socfpga_mgr, FPGA_MGR_BASE, 0x878);
345
PERIPH_MEMORY_DEFINE(socfpga_mgr_data, FPGA_MGR_DATA, 0x4);
350
PERIPH_MEMORY_DEFINE(socfpga_l3, SOCFPGA_L3_REGS, 0x1000);
351
PERIPH_MEMORY_DEFINE(socfpga_sdram, SOCFPGA_SDRAM_REGS, 0x6000);
352
PERIPH_MEMORY_DEFINE(socfpga_resetmgr, SOCFPGA_RESETMGR_REGS, 0x1000);
353
PERIPH_MEMORY_DEFINE(socfpga_sysmgr, SOCFPGA_SYSMGR_REGS, 0x1000);
355
static irq_return_t socfpga_irq_handler(unsigned int irq_nr, void *data) {
356
log_debug("enter irq");
360
EMBOX_UNIT_INIT(socfpga_init);
361
static int socfpga_init(void) {
362
bridge_enable_handoff();
364
fpga_register(&socfpga_fpga_ops, &socfpga0);
366
socfpga_regdump(&socfpga0);
368
return irq_attach(OPTION_GET(NUMBER, irq_num),
372
"socfpga IRQ handler");