1*4882a593Smuzhiyun /*
2*4882a593Smuzhiyun * Copyright (C) 2016 Stefan Roese <sr@denx.de>
3*4882a593Smuzhiyun *
4*4882a593Smuzhiyun * SPDX-License-Identifier: GPL-2.0+
5*4882a593Smuzhiyun */
6*4882a593Smuzhiyun
7*4882a593Smuzhiyun #include <common.h>
8*4882a593Smuzhiyun #include <altera.h>
9*4882a593Smuzhiyun #include <errno.h>
10*4882a593Smuzhiyun #include <asm/gpio.h>
11*4882a593Smuzhiyun #include <asm/io.h>
12*4882a593Smuzhiyun #include <asm/arch/cpu.h>
13*4882a593Smuzhiyun #include <asm/arch/soc.h>
14*4882a593Smuzhiyun #include <asm/arch-mvebu/spi.h>
15*4882a593Smuzhiyun #include "theadorable.h"
16*4882a593Smuzhiyun
17*4882a593Smuzhiyun /*
18*4882a593Smuzhiyun * FPGA programming support
19*4882a593Smuzhiyun */
fpga_pre_fn(int cookie)20*4882a593Smuzhiyun static int fpga_pre_fn(int cookie)
21*4882a593Smuzhiyun {
22*4882a593Smuzhiyun int gpio_config = COOKIE2CONFIG(cookie);
23*4882a593Smuzhiyun int gpio_done = COOKIE2DONE(cookie);
24*4882a593Smuzhiyun int ret;
25*4882a593Smuzhiyun
26*4882a593Smuzhiyun debug("%s (%d): cookie=%08x gpio_config=%d gpio_done=%d\n",
27*4882a593Smuzhiyun __func__, __LINE__, cookie, gpio_config, gpio_done);
28*4882a593Smuzhiyun
29*4882a593Smuzhiyun /* Configure config pin */
30*4882a593Smuzhiyun /* Set to output */
31*4882a593Smuzhiyun ret = gpio_request(gpio_config, "CONFIG");
32*4882a593Smuzhiyun if (ret < 0)
33*4882a593Smuzhiyun return ret;
34*4882a593Smuzhiyun gpio_direction_output(gpio_config, 1);
35*4882a593Smuzhiyun
36*4882a593Smuzhiyun /* Configure done pin */
37*4882a593Smuzhiyun /* Set to input */
38*4882a593Smuzhiyun ret = gpio_request(gpio_done, "DONE");
39*4882a593Smuzhiyun if (ret < 0)
40*4882a593Smuzhiyun return ret;
41*4882a593Smuzhiyun
42*4882a593Smuzhiyun gpio_direction_input(gpio_done);
43*4882a593Smuzhiyun
44*4882a593Smuzhiyun return 0;
45*4882a593Smuzhiyun }
46*4882a593Smuzhiyun
fpga_config_fn(int assert,int flush,int cookie)47*4882a593Smuzhiyun static int fpga_config_fn(int assert, int flush, int cookie)
48*4882a593Smuzhiyun {
49*4882a593Smuzhiyun int gpio_config = COOKIE2CONFIG(cookie);
50*4882a593Smuzhiyun
51*4882a593Smuzhiyun debug("%s (%d): cookie=%08x gpio_config=%d\n",
52*4882a593Smuzhiyun __func__, __LINE__, cookie, gpio_config);
53*4882a593Smuzhiyun
54*4882a593Smuzhiyun if (assert)
55*4882a593Smuzhiyun gpio_set_value(gpio_config, 1);
56*4882a593Smuzhiyun else
57*4882a593Smuzhiyun gpio_set_value(gpio_config, 0);
58*4882a593Smuzhiyun
59*4882a593Smuzhiyun return 0;
60*4882a593Smuzhiyun }
61*4882a593Smuzhiyun
fpga_write_fn(const void * buf,size_t len,int flush,int cookie)62*4882a593Smuzhiyun static int fpga_write_fn(const void *buf, size_t len, int flush, int cookie)
63*4882a593Smuzhiyun {
64*4882a593Smuzhiyun int spi_bus = COOKIE2SPI_BUS(cookie);
65*4882a593Smuzhiyun int spi_dev = COOKIE2SPI_DEV(cookie);
66*4882a593Smuzhiyun struct kwspi_registers *reg;
67*4882a593Smuzhiyun u32 control_reg;
68*4882a593Smuzhiyun u32 config_reg;
69*4882a593Smuzhiyun void *dst;
70*4882a593Smuzhiyun
71*4882a593Smuzhiyun /*
72*4882a593Smuzhiyun * Write data to FPGA attached to SPI bus via SPI direct write.
73*4882a593Smuzhiyun * This results in the fastest and easiest way to program the
74*4882a593Smuzhiyun * bitstream into the FPGA.
75*4882a593Smuzhiyun */
76*4882a593Smuzhiyun debug("%s (%d): cookie=%08x spi_bus=%d spi_dev=%d\n",
77*4882a593Smuzhiyun __func__, __LINE__, cookie, spi_bus, spi_dev);
78*4882a593Smuzhiyun
79*4882a593Smuzhiyun if (spi_bus == 0) {
80*4882a593Smuzhiyun reg = (struct kwspi_registers *)MVEBU_REGISTER(0x10600);
81*4882a593Smuzhiyun dst = (void *)SPI_BUS0_DEV1_BASE;
82*4882a593Smuzhiyun } else {
83*4882a593Smuzhiyun reg = (struct kwspi_registers *)MVEBU_REGISTER(0x10680);
84*4882a593Smuzhiyun dst = (void *)SPI_BUS1_DEV2_BASE;
85*4882a593Smuzhiyun }
86*4882a593Smuzhiyun
87*4882a593Smuzhiyun /* Configure SPI controller for direct access mode */
88*4882a593Smuzhiyun control_reg = readl(®->ctrl);
89*4882a593Smuzhiyun config_reg = readl(®->cfg);
90*4882a593Smuzhiyun writel(0x00000214, ®->cfg); /* 27MHz clock */
91*4882a593Smuzhiyun writel(0x00000000, ®->dw_cfg); /* don't de-asset CS */
92*4882a593Smuzhiyun writel(KWSPI_CSN_ACT, ®->ctrl); /* activate CS */
93*4882a593Smuzhiyun
94*4882a593Smuzhiyun /* Copy data to the SPI direct mapped window */
95*4882a593Smuzhiyun memcpy(dst, buf, len);
96*4882a593Smuzhiyun
97*4882a593Smuzhiyun /* Restore original register values */
98*4882a593Smuzhiyun writel(control_reg, ®->ctrl);
99*4882a593Smuzhiyun writel(config_reg, ®->cfg);
100*4882a593Smuzhiyun
101*4882a593Smuzhiyun return 0;
102*4882a593Smuzhiyun }
103*4882a593Smuzhiyun
104*4882a593Smuzhiyun /* Returns the state of CONF_DONE Pin */
fpga_done_fn(int cookie)105*4882a593Smuzhiyun static int fpga_done_fn(int cookie)
106*4882a593Smuzhiyun {
107*4882a593Smuzhiyun int gpio_done = COOKIE2DONE(cookie);
108*4882a593Smuzhiyun unsigned long ts;
109*4882a593Smuzhiyun
110*4882a593Smuzhiyun debug("%s (%d): cookie=%08x gpio_done=%d\n",
111*4882a593Smuzhiyun __func__, __LINE__, cookie, gpio_done);
112*4882a593Smuzhiyun
113*4882a593Smuzhiyun ts = get_timer(0);
114*4882a593Smuzhiyun do {
115*4882a593Smuzhiyun if (gpio_get_value(gpio_done))
116*4882a593Smuzhiyun return 0;
117*4882a593Smuzhiyun } while (get_timer(ts) < 1000);
118*4882a593Smuzhiyun
119*4882a593Smuzhiyun /* timeout so return error */
120*4882a593Smuzhiyun return -ENODEV;
121*4882a593Smuzhiyun }
122*4882a593Smuzhiyun
123*4882a593Smuzhiyun static altera_board_specific_func stratixv_fns = {
124*4882a593Smuzhiyun .pre = fpga_pre_fn,
125*4882a593Smuzhiyun .config = fpga_config_fn,
126*4882a593Smuzhiyun .write = fpga_write_fn,
127*4882a593Smuzhiyun .done = fpga_done_fn,
128*4882a593Smuzhiyun };
129*4882a593Smuzhiyun
130*4882a593Smuzhiyun static Altera_desc altera_fpga[] = {
131*4882a593Smuzhiyun {
132*4882a593Smuzhiyun /* Family */
133*4882a593Smuzhiyun Altera_StratixV,
134*4882a593Smuzhiyun /* Interface type */
135*4882a593Smuzhiyun passive_serial,
136*4882a593Smuzhiyun /* No limitation as additional data will be ignored */
137*4882a593Smuzhiyun -1,
138*4882a593Smuzhiyun /* Device function table */
139*4882a593Smuzhiyun (void *)&stratixv_fns,
140*4882a593Smuzhiyun /* Base interface address specified in driver */
141*4882a593Smuzhiyun NULL,
142*4882a593Smuzhiyun /* Cookie implementation */
143*4882a593Smuzhiyun /*
144*4882a593Smuzhiyun * In this 32bit word the following information is coded:
145*4882a593Smuzhiyun * Bit 31 ... Bit 0
146*4882a593Smuzhiyun * SPI-Bus | SPI-Dev | Config-Pin | Done-Pin
147*4882a593Smuzhiyun */
148*4882a593Smuzhiyun FPGA_COOKIE(0, 1, 26, 7)
149*4882a593Smuzhiyun },
150*4882a593Smuzhiyun {
151*4882a593Smuzhiyun /* Family */
152*4882a593Smuzhiyun Altera_StratixV,
153*4882a593Smuzhiyun /* Interface type */
154*4882a593Smuzhiyun passive_serial,
155*4882a593Smuzhiyun /* No limitation as additional data will be ignored */
156*4882a593Smuzhiyun -1,
157*4882a593Smuzhiyun /* Device function table */
158*4882a593Smuzhiyun (void *)&stratixv_fns,
159*4882a593Smuzhiyun /* Base interface address specified in driver */
160*4882a593Smuzhiyun NULL,
161*4882a593Smuzhiyun /* Cookie implementation */
162*4882a593Smuzhiyun /*
163*4882a593Smuzhiyun * In this 32bit word the following information is coded:
164*4882a593Smuzhiyun * Bit 31 ... Bit 0
165*4882a593Smuzhiyun * SPI-Bus | SPI-Dev | Config-Pin | Done-Pin
166*4882a593Smuzhiyun */
167*4882a593Smuzhiyun FPGA_COOKIE(1, 2, 29, 9)
168*4882a593Smuzhiyun },
169*4882a593Smuzhiyun };
170*4882a593Smuzhiyun
171*4882a593Smuzhiyun /* Add device descriptor to FPGA device table */
board_fpga_add(void)172*4882a593Smuzhiyun void board_fpga_add(void)
173*4882a593Smuzhiyun {
174*4882a593Smuzhiyun int i;
175*4882a593Smuzhiyun
176*4882a593Smuzhiyun fpga_init();
177*4882a593Smuzhiyun for (i = 0; i < ARRAY_SIZE(altera_fpga); i++)
178*4882a593Smuzhiyun fpga_add(fpga_altera, &altera_fpga[i]);
179*4882a593Smuzhiyun }
180