2 * Copyright (C) 2016 Stefan Roese <sr@denx.de>
4 * SPDX-License-Identifier: GPL-2.0+
12 #include <asm/arch/cpu.h>
13 #include <asm/arch/soc.h>
14 #include <asm/arch-mvebu/spi.h>
15 #include "theadorable.h"
18 * FPGA programming support
20 static int fpga_pre_fn(int cookie)
22 int gpio_config = COOKIE2CONFIG(cookie);
23 int gpio_done = COOKIE2DONE(cookie);
26 debug("%s (%d): cookie=%08x gpio_config=%d gpio_done=%d\n",
27 __func__, __LINE__, cookie, gpio_config, gpio_done);
29 /* Configure config pin */
31 ret = gpio_request(gpio_config, "CONFIG");
34 gpio_direction_output(gpio_config, 1);
36 /* Configure done pin */
38 ret = gpio_request(gpio_done, "DONE");
42 gpio_direction_input(gpio_done);
47 static int fpga_config_fn(int assert, int flush, int cookie)
49 int gpio_config = COOKIE2CONFIG(cookie);
51 debug("%s (%d): cookie=%08x gpio_config=%d\n",
52 __func__, __LINE__, cookie, gpio_config);
55 gpio_set_value(gpio_config, 1);
57 gpio_set_value(gpio_config, 0);
62 static int fpga_write_fn(const void *buf, size_t len, int flush, int cookie)
64 int spi_bus = COOKIE2SPI_BUS(cookie);
65 int spi_dev = COOKIE2SPI_DEV(cookie);
66 struct kwspi_registers *reg;
72 * Write data to FPGA attached to SPI bus via SPI direct write.
73 * This results in the fastest and easiest way to program the
74 * bitstream into the FPGA.
76 debug("%s (%d): cookie=%08x spi_bus=%d spi_dev=%d\n",
77 __func__, __LINE__, cookie, spi_bus, spi_dev);
80 reg = (struct kwspi_registers *)MVEBU_REGISTER(0x10600);
81 dst = (void *)SPI_BUS0_DEV1_BASE;
83 reg = (struct kwspi_registers *)MVEBU_REGISTER(0x10680);
84 dst = (void *)SPI_BUS1_DEV2_BASE;
87 /* Configure SPI controller for direct access mode */
88 control_reg = readl(®->ctrl);
89 config_reg = readl(®->cfg);
90 writel(0x00000214, ®->cfg); /* 27MHz clock */
91 writel(0x00000000, ®->dw_cfg); /* don't de-asset CS */
92 writel(KWSPI_CSN_ACT, ®->ctrl); /* activate CS */
94 /* Copy data to the SPI direct mapped window */
95 memcpy(dst, buf, len);
97 /* Restore original register values */
98 writel(control_reg, ®->ctrl);
99 writel(config_reg, ®->cfg);
104 /* Returns the state of CONF_DONE Pin */
105 static int fpga_done_fn(int cookie)
107 int gpio_done = COOKIE2DONE(cookie);
110 debug("%s (%d): cookie=%08x gpio_done=%d\n",
111 __func__, __LINE__, cookie, gpio_done);
115 if (gpio_get_value(gpio_done))
117 } while (get_timer(ts) < 1000);
119 /* timeout so return error */
123 static altera_board_specific_func stratixv_fns = {
125 .config = fpga_config_fn,
126 .write = fpga_write_fn,
127 .done = fpga_done_fn,
130 static Altera_desc altera_fpga[] = {
136 /* No limitation as additional data will be ignored */
138 /* Device function table */
139 (void *)&stratixv_fns,
140 /* Base interface address specified in driver */
142 /* Cookie implementation */
144 * In this 32bit word the following information is coded:
146 * SPI-Bus | SPI-Dev | Config-Pin | Done-Pin
148 FPGA_COOKIE(0, 1, 26, 7)
155 /* No limitation as additional data will be ignored */
157 /* Device function table */
158 (void *)&stratixv_fns,
159 /* Base interface address specified in driver */
161 /* Cookie implementation */
163 * In this 32bit word the following information is coded:
165 * SPI-Bus | SPI-Dev | Config-Pin | Done-Pin
167 FPGA_COOKIE(1, 2, 29, 9)
171 /* Add device descriptor to FPGA device table */
172 void board_fpga_add(void)
177 for (i = 0; i < ARRAY_SIZE(altera_fpga); i++)
178 fpga_add(fpga_altera, &altera_fpga[i]);