blob: 3a766728a6f718359cc7319fcdedd647c9adb7dd [file] [log] [blame]
Tom Rini10e47792018-05-06 17:58:06 -04001// SPDX-License-Identifier: GPL-2.0+
Patrick Bruennba81b042016-11-04 11:57:02 +01002/*
3 * Copyright (C) 2015 Beckhoff Automation GmbH & Co. KG
4 * Patrick Bruenn <p.bruenn@beckhoff.com>
5 *
6 * Based on <u-boot>/board/freescale/mx53loco/mx53loco.c
7 * Copyright (C) 2011 Freescale Semiconductor, Inc.
Patrick Bruennba81b042016-11-04 11:57:02 +01008 */
9
Simon Glass63334482019-11-14 12:57:39 -070010#include <cpu_func.h>
Simon Glassa7b51302019-11-14 12:57:46 -070011#include <init.h>
Patrick Bruennba81b042016-11-04 11:57:02 +010012#include <asm/arch/sys_proto.h>
Patrick Bruennba81b042016-11-04 11:57:02 +010013#include <asm/arch/clock.h>
14#include <asm/arch/iomux-mx53.h>
Simon Glass3ba929a2020-10-30 21:38:53 -060015#include <asm/global_data.h>
Stefano Babic33731bc2017-06-29 10:16:06 +020016#include <asm/mach-imx/mx5_video.h>
Patrick Bruennba81b042016-11-04 11:57:02 +010017#include <ACEX1K.h>
Patrick Bruennba81b042016-11-04 11:57:02 +010018#include <asm/gpio.h>
Simon Glassdbd79542020-05-10 11:40:11 -060019#include <linux/delay.h>
Patrick Bruennba81b042016-11-04 11:57:02 +010020
21enum LED_GPIOS {
22 GPIO_SD1_CD = IMX_GPIO_NR(1, 1),
23 GPIO_SD2_CD = IMX_GPIO_NR(1, 4),
24 GPIO_LED_SD2_R = IMX_GPIO_NR(3, 16),
25 GPIO_LED_SD2_B = IMX_GPIO_NR(3, 17),
26 GPIO_LED_SD2_G = IMX_GPIO_NR(3, 18),
27 GPIO_LED_SD1_R = IMX_GPIO_NR(3, 19),
28 GPIO_LED_SD1_B = IMX_GPIO_NR(3, 20),
29 GPIO_LED_SD1_G = IMX_GPIO_NR(3, 21),
30 GPIO_LED_PWR_R = IMX_GPIO_NR(3, 22),
31 GPIO_LED_PWR_B = IMX_GPIO_NR(3, 23),
32 GPIO_LED_PWR_G = IMX_GPIO_NR(3, 24),
33 GPIO_SUPS_INT = IMX_GPIO_NR(3, 31),
34 GPIO_C3_CONFIG = IMX_GPIO_NR(6, 8),
35 GPIO_C3_STATUS = IMX_GPIO_NR(6, 7),
36 GPIO_C3_DONE = IMX_GPIO_NR(6, 9),
37};
38
39#define CCAT_BASE_ADDR ((void *)0xf0000000)
40#define CCAT_END_ADDR (CCAT_BASE_ADDR + (1024 * 1024 * 32))
41#define CCAT_SIZE 1191788
42#define CCAT_SIGN_ADDR (CCAT_BASE_ADDR + 12)
43static const char CCAT_SIGNATURE[] = "CCAT";
44
45static const u32 CCAT_MODE_CONFIG = 0x0024DC81;
46static const u32 CCAT_MODE_RUN = 0x0033DC8F;
47
48DECLARE_GLOBAL_DATA_PTR;
49
Tom Rini4cc38852021-08-30 09:16:30 -040050#ifdef CONFIG_REVISION_TAG
Patrick Bruennba81b042016-11-04 11:57:02 +010051u32 get_board_rev(void)
52{
53 struct iim_regs *iim = (struct iim_regs *)IMX_IIM_BASE;
54 struct fuse_bank *bank = &iim->bank[0];
55 struct fuse_bank0_regs *fuse =
56 (struct fuse_bank0_regs *)bank->fuse_regs;
57
58 int rev = readl(&fuse->gp[6]);
59
60 return (get_cpu_rev() & ~(0xF << 8)) | (rev & 0xF) << 8;
61}
Tom Rini4cc38852021-08-30 09:16:30 -040062#endif
Patrick Bruennba81b042016-11-04 11:57:02 +010063
64/*
65 * Set CCAT mode
66 * @mode: use CCAT_MODE_CONFIG or CCAT_MODE_RUN
67 */
68void weim_cs0_settings(u32 mode)
69{
70 struct weim *weim_regs = (struct weim *)WEIM_BASE_ADDR;
71
72 writel(0x0, &weim_regs->cs0gcr1);
73 writel(mode, &weim_regs->cs0gcr1);
74 writel(0x00001002, &weim_regs->cs0gcr2);
75
76 writel(0x04000000, &weim_regs->cs0rcr1);
77 writel(0x00000000, &weim_regs->cs0rcr2);
78
79 writel(0x04000000, &weim_regs->cs0wcr1);
80 writel(0x00000000, &weim_regs->cs0wcr2);
81}
82
83static void setup_gpio_eim(void)
84{
Steffen Dirkwinkel098e9ef2019-04-17 13:57:14 +020085 gpio_request(GPIO_C3_STATUS, "GPIO_C3_STATUS");
86 gpio_request(GPIO_C3_DONE, "GPIO_C3_DONE");
87 gpio_request(GPIO_C3_CONFIG, "GPIO_C3_CONFIG");
Patrick Bruennba81b042016-11-04 11:57:02 +010088 gpio_direction_input(GPIO_C3_STATUS);
89 gpio_direction_input(GPIO_C3_DONE);
90 gpio_direction_output(GPIO_C3_CONFIG, 1);
91
92 weim_cs0_settings(CCAT_MODE_RUN);
93}
94
95static void setup_gpio_sups(void)
96{
Steffen Dirkwinkel098e9ef2019-04-17 13:57:14 +020097 gpio_request(GPIO_SUPS_INT, "GPIO_SUPS_INT");
Patrick Bruennba81b042016-11-04 11:57:02 +010098 gpio_direction_input(GPIO_SUPS_INT);
99
100 static const int BLINK_INTERVALL = 50000;
101 int status = 1;
102 while (gpio_get_value(GPIO_SUPS_INT)) {
103 /* signal "CX SUPS power fail" */
104 gpio_set_value(GPIO_LED_PWR_R,
105 (++status / BLINK_INTERVALL) % 2);
106 }
107
108 /* signal "CX power up" */
109 gpio_set_value(GPIO_LED_PWR_R, 1);
110}
111
112static void setup_gpio_leds(void)
113{
Steffen Dirkwinkel098e9ef2019-04-17 13:57:14 +0200114 gpio_request(GPIO_LED_SD2_R, "GPIO_LED_SD2_R");
115 gpio_request(GPIO_LED_SD2_B, "GPIO_LED_SD2_B");
116 gpio_request(GPIO_LED_SD2_G, "GPIO_LED_SD2_G");
117 gpio_request(GPIO_LED_SD1_R, "GPIO_LED_SD1_R");
118 gpio_request(GPIO_LED_SD1_B, "GPIO_LED_SD1_B");
119 gpio_request(GPIO_LED_SD1_G, "GPIO_LED_SD1_G");
120 gpio_request(GPIO_LED_PWR_R, "GPIO_LED_PWR_R");
121 gpio_request(GPIO_LED_PWR_B, "GPIO_LED_PWR_B");
122 gpio_request(GPIO_LED_PWR_G, "GPIO_LED_PWR_G");
123
Patrick Bruennba81b042016-11-04 11:57:02 +0100124 gpio_direction_output(GPIO_LED_SD2_R, 0);
125 gpio_direction_output(GPIO_LED_SD2_B, 0);
126 gpio_direction_output(GPIO_LED_SD2_G, 0);
127 gpio_direction_output(GPIO_LED_SD1_R, 0);
128 gpio_direction_output(GPIO_LED_SD1_B, 0);
129 gpio_direction_output(GPIO_LED_SD1_G, 0);
130 gpio_direction_output(GPIO_LED_PWR_R, 0);
131 gpio_direction_output(GPIO_LED_PWR_B, 0);
132 gpio_direction_output(GPIO_LED_PWR_G, 0);
133}
134
Patrick Bruennba81b042016-11-04 11:57:02 +0100135static int power_init(void)
136{
137 /* nothing to do on CX9020 */
138 return 0;
139}
140
141static void clock_1GHz(void)
142{
143 int ret;
144 u32 ref_clk = MXC_HCLK;
145 /*
146 * After increasing voltage to 1.25V, we can switch
147 * CPU clock to 1GHz and DDR to 400MHz safely
148 */
149 ret = mxc_set_clock(ref_clk, 1000, MXC_ARM_CLK);
150 if (ret)
151 printf("CPU: Switch CPU clock to 1GHZ failed\n");
152
153 ret = mxc_set_clock(ref_clk, 400, MXC_PERIPH_CLK);
154 ret |= mxc_set_clock(ref_clk, 400, MXC_DDR_CLK);
155 if (ret)
156 printf("CPU: Switch DDR clock to 400MHz failed\n");
157}
158
159int board_early_init_f(void)
160{
Patrick Bruennba81b042016-11-04 11:57:02 +0100161
162 return 0;
163}
164
Patrick Bruennba81b042016-11-04 11:57:02 +0100165int board_init(void)
166{
167 gd->bd->bi_boot_params = PHYS_SDRAM_1 + 0x100;
168
169 mxc_set_sata_internal_clock();
170
Steffen Dirkwinkel098e9ef2019-04-17 13:57:14 +0200171 setup_gpio_leds();
172 setup_gpio_sups();
173 setup_gpio_eim();
174 setup_iomux_lcd();
175
Patrick Bruennba81b042016-11-04 11:57:02 +0100176 return 0;
177}
178
179int checkboard(void)
180{
181 puts("Board: Beckhoff CX9020\n");
182
183 return 0;
184}
185
186static int ccat_config_fn(int assert_config, int flush, int cookie)
187{
188 /* prepare FPGA for programming */
189 weim_cs0_settings(CCAT_MODE_CONFIG);
190 gpio_set_value(GPIO_C3_CONFIG, 0);
191 udelay(1);
192 gpio_set_value(GPIO_C3_CONFIG, 1);
193 udelay(230);
194
195 return FPGA_SUCCESS;
196}
197
198static int ccat_status_fn(int cookie)
199{
200 return FPGA_FAIL;
201}
202
203static int ccat_write_fn(const void *buf, size_t buf_len, int flush, int cookie)
204{
205 const uint8_t *const buffer = buf;
206
207 /* program CCAT */
208 int i;
209 for (i = 0; i < buf_len; ++i)
210 writeb(buffer[i], CCAT_BASE_ADDR);
211
212 writeb(0xff, CCAT_BASE_ADDR);
213 writeb(0xff, CCAT_BASE_ADDR);
214
215 return FPGA_SUCCESS;
216}
217
218static int ccat_done_fn(int cookie)
219{
220 /* programming complete? */
221 return gpio_get_value(GPIO_C3_DONE);
222}
223
224static int ccat_post_fn(int cookie)
225{
226 /* switch to FPGA run mode */
227 weim_cs0_settings(CCAT_MODE_RUN);
228 invalidate_dcache_range((ulong) CCAT_BASE_ADDR, (ulong) CCAT_END_ADDR);
229
230 if (memcmp(CCAT_SIGN_ADDR, CCAT_SIGNATURE, sizeof(CCAT_SIGNATURE))) {
231 printf("Verifing CCAT firmware failed, signature not found\n");
232 return FPGA_FAIL;
233 }
234
235 /* signal "CX booting OS" */
236 gpio_set_value(GPIO_LED_PWR_R, 1);
237 gpio_set_value(GPIO_LED_PWR_G, 1);
238 gpio_set_value(GPIO_LED_PWR_B, 0);
239 return FPGA_SUCCESS;
240}
241
242static Altera_CYC2_Passive_Serial_fns ccat_fns = {
243 .config = ccat_config_fn,
244 .status = ccat_status_fn,
245 .done = ccat_done_fn,
246 .write = ccat_write_fn,
247 .abort = ccat_post_fn,
248 .post = ccat_post_fn,
249};
250
251static Altera_desc ccat_fpga = {
252 .family = Altera_CYC2,
253 .iface = passive_serial,
254 .size = CCAT_SIZE,
255 .iface_fns = &ccat_fns,
256 .base = CCAT_BASE_ADDR,
257};
258
259int board_late_init(void)
260{
261 if (!power_init())
262 clock_1GHz();
263
264 fpga_init();
265 fpga_add(fpga_altera, &ccat_fpga);
266
267 return 0;
268}