blob: f5bb549b7c0bf5863dd46401174336a2a1f3a63e [file] [log] [blame]
Tom Rini10e47792018-05-06 17:58:06 -04001// SPDX-License-Identifier: GPL-2.0
Vladimir Barinov8c946272015-07-20 20:49:59 +03002/*
3 * Stout board CPLD access support
4 *
5 * Copyright (C) 2015 Renesas Electronics Europe GmbH
6 * Copyright (C) 2015 Renesas Electronics Corporation
7 * Copyright (C) 2015 Cogent Embedded, Inc.
Vladimir Barinov8c946272015-07-20 20:49:59 +03008 */
9
Simon Glassed38aef2020-05-10 11:40:03 -060010#include <command.h>
Simon Glassafb02152019-12-28 10:45:01 -070011#include <cpu_func.h>
Vladimir Barinov8c946272015-07-20 20:49:59 +030012#include <asm/io.h>
13#include <asm/gpio.h>
14#include "cpld.h"
15
Marek Vasut3320a222018-04-12 15:23:46 +020016#define SCLK (92 + 24)
17#define SSTBZ (92 + 25)
18#define MOSI (92 + 26)
19#define MISO (92 + 27)
Vladimir Barinov8c946272015-07-20 20:49:59 +030020
21#define CPLD_ADDR_MODE 0x00 /* RW */
22#define CPLD_ADDR_MUX 0x01 /* RW */
23#define CPLD_ADDR_HDMI 0x02 /* RW */
24#define CPLD_ADDR_DIPSW 0x08 /* R */
25#define CPLD_ADDR_RESET 0x80 /* RW */
26#define CPLD_ADDR_VERSION 0xFF /* R */
27
28static u32 cpld_read(u8 addr)
29{
30 int i;
31 u32 data = 0;
32
33 for (i = 0; i < 8; i++) {
34 gpio_set_value(MOSI, addr & 0x80); /* MSB first */
35 gpio_set_value(SCLK, 1);
36 addr <<= 1;
37 gpio_set_value(SCLK, 0);
38 }
39
40 gpio_set_value(MOSI, 0); /* READ */
41 gpio_set_value(SSTBZ, 0);
42 gpio_set_value(SCLK, 1);
43 gpio_set_value(SCLK, 0);
44 gpio_set_value(SSTBZ, 1);
45
46 for (i = 0; i < 32; i++) {
47 gpio_set_value(SCLK, 1);
48 data <<= 1;
49 data |= gpio_get_value(MISO); /* MSB first */
50 gpio_set_value(SCLK, 0);
51 }
52
53 return data;
54}
55
56static void cpld_write(u8 addr, u32 data)
57{
58 int i;
59
60 for (i = 0; i < 32; i++) {
61 gpio_set_value(MOSI, data & (1 << 31)); /* MSB first */
62 gpio_set_value(SCLK, 1);
63 data <<= 1;
64 gpio_set_value(SCLK, 0);
65 }
66
67 for (i = 0; i < 8; i++) {
68 gpio_set_value(MOSI, addr & 0x80); /* MSB first */
69 gpio_set_value(SCLK, 1);
70 addr <<= 1;
71 gpio_set_value(SCLK, 0);
72 }
73
74 gpio_set_value(MOSI, 1); /* WRITE */
75 gpio_set_value(SSTBZ, 0);
76 gpio_set_value(SCLK, 1);
77 gpio_set_value(SCLK, 0);
78 gpio_set_value(SSTBZ, 1);
79}
80
81/* LSI pin pull-up control */
82#define PUPR3 0xe606010C
83#define PUPR3_SD3_DAT1 (1 << 27)
84
85void cpld_init(void)
86{
87 u32 val;
88
89 /* PULL-UP on MISO line */
90 val = readl(PUPR3);
91 val |= PUPR3_SD3_DAT1;
92 writel(val, PUPR3);
93
Marek Vasut3320a222018-04-12 15:23:46 +020094 gpio_request(SCLK, "SCLK");
95 gpio_request(SSTBZ, "SSTBZ");
96 gpio_request(MOSI, "MOSI");
97 gpio_request(MISO, "MISO");
Vladimir Barinov8c946272015-07-20 20:49:59 +030098
99 gpio_direction_output(SCLK, 0);
100 gpio_direction_output(SSTBZ, 1);
101 gpio_direction_output(MOSI, 0);
102 gpio_direction_input(MISO);
103
104 /* dummy read */
105 cpld_read(CPLD_ADDR_VERSION);
106
107 printf("CPLD version: 0x%08x\n",
108 cpld_read(CPLD_ADDR_VERSION));
109 printf("H2 Mode setting (MD0..28): 0x%08x\n",
110 cpld_read(CPLD_ADDR_MODE));
111 printf("Multiplexer settings: 0x%08x\n",
112 cpld_read(CPLD_ADDR_MUX));
113 printf("HDMI setting: 0x%08x\n",
114 cpld_read(CPLD_ADDR_HDMI));
115 printf("DIPSW (SW3): 0x%08x\n",
116 cpld_read(CPLD_ADDR_DIPSW));
117
118#ifdef CONFIG_SH_SDHI
119 /* switch MUX to SD0 */
120 val = cpld_read(CPLD_ADDR_MUX);
121 val &= ~MUX_MSK_SD0;
122 val |= MUX_VAL_SD0;
123 cpld_write(CPLD_ADDR_MUX, val);
124#endif
125}
126
Simon Glassed38aef2020-05-10 11:40:03 -0600127static int do_cpld(struct cmd_tbl *cmdtp, int flag, int argc,
128 char *const argv[])
Vladimir Barinov8c946272015-07-20 20:49:59 +0300129{
130 u32 addr, val;
131
132 if (argc < 3)
133 return CMD_RET_USAGE;
134
Simon Glass3ff49ec2021-07-24 09:03:29 -0600135 addr = hextoul(argv[2], NULL);
Vladimir Barinov8c946272015-07-20 20:49:59 +0300136 if (!(addr == CPLD_ADDR_VERSION || addr == CPLD_ADDR_MODE ||
137 addr == CPLD_ADDR_MUX || addr == CPLD_ADDR_HDMI ||
138 addr == CPLD_ADDR_DIPSW || addr == CPLD_ADDR_RESET)) {
139 printf("cpld invalid addr\n");
140 return CMD_RET_USAGE;
141 }
142
143 if (argc == 3 && strcmp(argv[1], "read") == 0) {
144 printf("0x%x\n", cpld_read(addr));
145 } else if (argc == 4 && strcmp(argv[1], "write") == 0) {
Simon Glass3ff49ec2021-07-24 09:03:29 -0600146 val = hextoul(argv[3], NULL);
Vladimir Barinov8c946272015-07-20 20:49:59 +0300147 if (addr == CPLD_ADDR_MUX) {
148 /* never mask SCIFA0 console */
149 val &= ~MUX_MSK_SCIFA0_USB;
150 val |= MUX_VAL_SCIFA0_USB;
151 }
152 cpld_write(addr, val);
153 }
154
155 return 0;
156}
157
158U_BOOT_CMD(
159 cpld, 4, 1, do_cpld,
160 "CPLD access",
161 "read addr\n"
162 "cpld write addr val\n"
163);
164
Harald Seiler6f14d5f2020-12-15 16:47:52 +0100165void reset_cpu(void)
Vladimir Barinov8c946272015-07-20 20:49:59 +0300166{
167 cpld_write(CPLD_ADDR_RESET, 1);
168}