1 // SPDX-License-Identifier: GPL-2.0+
3 * Copyright (C) 2016 Stefan Roese <sr@denx.de>
11 #include <asm/arch/cpu.h>
12 #include <asm/arch/soc.h>
13 #include <asm/arch-mvebu/spi.h>
14 #include "theadorable.h"
17 * FPGA programming support
19 static int fpga_pre_fn(int cookie)
21 int gpio_config = COOKIE2CONFIG(cookie);
22 int gpio_done = COOKIE2DONE(cookie);
25 debug("%s (%d): cookie=%08x gpio_config=%d gpio_done=%d\n",
26 __func__, __LINE__, cookie, gpio_config, gpio_done);
28 /* Configure config pin */
30 ret = gpio_request(gpio_config, "CONFIG");
33 gpio_direction_output(gpio_config, 1);
35 /* Configure done pin */
37 ret = gpio_request(gpio_done, "DONE");
41 gpio_direction_input(gpio_done);
46 static int fpga_config_fn(int assert, int flush, int cookie)
48 int gpio_config = COOKIE2CONFIG(cookie);
50 debug("%s (%d): cookie=%08x gpio_config=%d\n",
51 __func__, __LINE__, cookie, gpio_config);
54 gpio_set_value(gpio_config, 1);
56 gpio_set_value(gpio_config, 0);
61 static int fpga_write_fn(const void *buf, size_t len, int flush, int cookie)
63 int spi_bus = COOKIE2SPI_BUS(cookie);
64 int spi_dev = COOKIE2SPI_DEV(cookie);
65 struct kwspi_registers *reg;
71 * Write data to FPGA attached to SPI bus via SPI direct write.
72 * This results in the fastest and easiest way to program the
73 * bitstream into the FPGA.
75 debug("%s (%d): cookie=%08x spi_bus=%d spi_dev=%d\n",
76 __func__, __LINE__, cookie, spi_bus, spi_dev);
79 reg = (struct kwspi_registers *)MVEBU_REGISTER(0x10600);
80 dst = (void *)SPI_BUS0_DEV1_BASE;
82 reg = (struct kwspi_registers *)MVEBU_REGISTER(0x10680);
83 dst = (void *)SPI_BUS1_DEV2_BASE;
86 /* Configure SPI controller for direct access mode */
87 control_reg = readl(®->ctrl);
88 config_reg = readl(®->cfg);
89 writel(0x00000214, ®->cfg); /* 27MHz clock */
90 writel(0x00000000, ®->dw_cfg); /* don't de-asset CS */
91 writel(KWSPI_CSN_ACT, ®->ctrl); /* activate CS */
93 /* Copy data to the SPI direct mapped window */
94 memcpy(dst, buf, len);
96 /* Restore original register values */
97 writel(control_reg, ®->ctrl);
98 writel(config_reg, ®->cfg);
103 /* Returns the state of CONF_DONE Pin */
104 static int fpga_done_fn(int cookie)
106 int gpio_done = COOKIE2DONE(cookie);
109 debug("%s (%d): cookie=%08x gpio_done=%d\n",
110 __func__, __LINE__, cookie, gpio_done);
114 if (gpio_get_value(gpio_done))
116 } while (get_timer(ts) < 1000);
118 /* timeout so return error */
122 static altera_board_specific_func stratixv_fns = {
124 .config = fpga_config_fn,
125 .write = fpga_write_fn,
126 .done = fpga_done_fn,
129 static Altera_desc altera_fpga[] = {
135 /* No limitation as additional data will be ignored */
137 /* Device function table */
138 (void *)&stratixv_fns,
139 /* Base interface address specified in driver */
141 /* Cookie implementation */
143 * In this 32bit word the following information is coded:
145 * SPI-Bus | SPI-Dev | Config-Pin | Done-Pin
147 FPGA_COOKIE(0, 1, 26, 7)
154 /* No limitation as additional data will be ignored */
156 /* Device function table */
157 (void *)&stratixv_fns,
158 /* Base interface address specified in driver */
160 /* Cookie implementation */
162 * In this 32bit word the following information is coded:
164 * SPI-Bus | SPI-Dev | Config-Pin | Done-Pin
166 FPGA_COOKIE(1, 2, 29, 9)
170 /* Add device descriptor to FPGA device table */
171 void board_fpga_add(void)
176 for (i = 0; i < ARRAY_SIZE(altera_fpga); i++)
177 fpga_add(fpga_altera, &altera_fpga[i]);