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