]> git.ipfire.org Git - thirdparty/u-boot.git/blob - arch/arm/mach-uniphier/micro-support-card.c
c71470a20429378cd67f9505211abcf20b89ee82
[thirdparty/u-boot.git] / arch / arm / mach-uniphier / micro-support-card.c
1 // SPDX-License-Identifier: GPL-2.0+
2 /*
3 * Copyright (C) 2012-2015 Panasonic Corporation
4 * Copyright (C) 2015-2020 Socionext Inc.
5 * Author: Masahiro Yamada <yamada.masahiro@socionext.com>
6 */
7
8 #include <common.h>
9 #include <dm/of.h>
10 #include <fdt_support.h>
11 #include <linux/ctype.h>
12 #include <linux/io.h>
13
14 #include "micro-support-card.h"
15
16 #define SMC911X_OFFSET 0x00000
17 #define LED_OFFSET 0x90000
18 #define NS16550A_OFFSET 0xb0000
19 #define MICRO_SUPPORT_CARD_RESET 0xd0034
20 #define MICRO_SUPPORT_CARD_REVISION 0xd00e0
21
22 static bool support_card_found;
23 static void __iomem *support_card_base;
24
25 static void support_card_detect(void)
26 {
27 DECLARE_GLOBAL_DATA_PTR;
28 const void *fdt = gd->fdt_blob;
29 int offset;
30 u64 addr, addr2;
31
32 offset = fdt_node_offset_by_compatible(fdt, 0, "smsc,lan9118");
33 if (offset < 0)
34 return;
35
36 addr = fdt_get_base_address(fdt, offset);
37 if (addr == OF_BAD_ADDR)
38 return;
39 addr -= SMC911X_OFFSET;
40
41 offset = fdt_node_offset_by_compatible(fdt, 0, "ns16550a");
42 if (offset < 0)
43 return;
44
45 addr2 = fdt_get_base_address(fdt, offset);
46 if (addr2 == OF_BAD_ADDR)
47 return;
48 addr2 -= NS16550A_OFFSET;
49
50 /* sanity check */
51 if (addr != addr2)
52 return;
53
54 support_card_base = ioremap(addr, 0x100000);
55
56 support_card_found = true;
57 }
58
59 /*
60 * 0: reset deassert, 1: reset
61 *
62 * bit[0]: LAN, I2C, LED
63 * bit[1]: UART
64 */
65 static void support_card_reset_deassert(void)
66 {
67 writel(0x00010000, support_card_base + MICRO_SUPPORT_CARD_RESET);
68 }
69
70 static void support_card_reset(void)
71 {
72 writel(0x00020003, support_card_base + MICRO_SUPPORT_CARD_RESET);
73 }
74
75 static int support_card_show_revision(void)
76 {
77 u32 revision;
78
79 revision = readl(support_card_base + MICRO_SUPPORT_CARD_REVISION);
80 revision &= 0xff;
81
82 /* revision 3.6.x card changed the revision format */
83 printf("SC: Micro Support Card (CPLD version %s%d.%d)\n",
84 revision >> 4 == 6 ? "3." : "",
85 revision >> 4, revision & 0xf);
86
87 return 0;
88 }
89
90 void support_card_init(void)
91 {
92 support_card_detect();
93
94 if (!support_card_found)
95 return;
96
97 support_card_reset();
98 /*
99 * After power on, we need to keep the LAN controller in reset state
100 * for a while. (200 usec)
101 */
102 udelay(200);
103 support_card_reset_deassert();
104
105 support_card_show_revision();
106 }
107
108 #if defined(CONFIG_SMC911X)
109 #include <netdev.h>
110
111 int board_eth_init(bd_t *bis)
112 {
113 if (!support_card_found)
114 return 0;
115
116 return smc911x_initialize(0, (unsigned long)support_card_base + SMC911X_OFFSET);
117 }
118 #endif
119
120 #if defined(CONFIG_MTD_NOR_FLASH)
121
122 #include <mtd/cfi_flash.h>
123
124 struct memory_bank {
125 phys_addr_t base;
126 unsigned long size;
127 };
128
129 static int mem_is_flash(const struct memory_bank *mem)
130 {
131 const int loop = 128;
132 u32 *scratch_addr;
133 u32 saved_value;
134 int ret = 1;
135 int i;
136
137 /* just in case, use the tail of the memory bank */
138 scratch_addr = map_physmem(mem->base + mem->size - sizeof(u32) * loop,
139 sizeof(u32) * loop, MAP_NOCACHE);
140
141 for (i = 0; i < loop; i++, scratch_addr++) {
142 saved_value = readl(scratch_addr);
143 writel(~saved_value, scratch_addr);
144 if (readl(scratch_addr) != saved_value) {
145 /* We assume no memory or SRAM here. */
146 writel(saved_value, scratch_addr);
147 ret = 0;
148 break;
149 }
150 }
151
152 unmap_physmem(scratch_addr, MAP_NOCACHE);
153
154 return ret;
155 }
156
157 /* {address, size} */
158 static const struct memory_bank memory_banks[] = {
159 {0x42000000, 0x01f00000},
160 };
161
162 static const struct memory_bank
163 *flash_banks_list[CONFIG_SYS_MAX_FLASH_BANKS_DETECT];
164
165 phys_addr_t cfi_flash_bank_addr(int i)
166 {
167 return flash_banks_list[i]->base;
168 }
169
170 unsigned long cfi_flash_bank_size(int i)
171 {
172 return flash_banks_list[i]->size;
173 }
174
175 static void detect_num_flash_banks(void)
176 {
177 const struct memory_bank *memory_bank, *end;
178
179 cfi_flash_num_flash_banks = 0;
180
181 memory_bank = memory_banks;
182 end = memory_bank + ARRAY_SIZE(memory_banks);
183
184 for (; memory_bank < end; memory_bank++) {
185 if (cfi_flash_num_flash_banks >=
186 CONFIG_SYS_MAX_FLASH_BANKS_DETECT)
187 break;
188
189 if (mem_is_flash(memory_bank)) {
190 flash_banks_list[cfi_flash_num_flash_banks] =
191 memory_bank;
192
193 debug("flash bank found: base = 0x%lx, size = 0x%lx\n",
194 (unsigned long)memory_bank->base,
195 (unsigned long)memory_bank->size);
196 cfi_flash_num_flash_banks++;
197 }
198 }
199
200 debug("number of flash banks: %d\n", cfi_flash_num_flash_banks);
201 }
202 #else /* CONFIG_MTD_NOR_FLASH */
203 static void detect_num_flash_banks(void)
204 {
205 };
206 #endif /* CONFIG_MTD_NOR_FLASH */
207
208 void support_card_late_init(void)
209 {
210 if (!support_card_found)
211 return;
212
213 detect_num_flash_banks();
214 }
215
216 static const u8 ledval_num[] = {
217 0x7e, /* 0 */
218 0x0c, /* 1 */
219 0xb6, /* 2 */
220 0x9e, /* 3 */
221 0xcc, /* 4 */
222 0xda, /* 5 */
223 0xfa, /* 6 */
224 0x4e, /* 7 */
225 0xfe, /* 8 */
226 0xde, /* 9 */
227 };
228
229 static const u8 ledval_alpha[] = {
230 0xee, /* A */
231 0xf8, /* B */
232 0x72, /* C */
233 0xbc, /* D */
234 0xf2, /* E */
235 0xe2, /* F */
236 0x7a, /* G */
237 0xe8, /* H */
238 0x08, /* I */
239 0x3c, /* J */
240 0xea, /* K */
241 0x70, /* L */
242 0x6e, /* M */
243 0xa8, /* N */
244 0xb8, /* O */
245 0xe6, /* P */
246 0xce, /* Q */
247 0xa0, /* R */
248 0xc8, /* S */
249 0x8c, /* T */
250 0x7c, /* U */
251 0x54, /* V */
252 0xfc, /* W */
253 0xec, /* X */
254 0xdc, /* Y */
255 0xa4, /* Z */
256 };
257
258 static u8 char2ledval(char c)
259 {
260 if (isdigit(c))
261 return ledval_num[c - '0'];
262 else if (isalpha(c))
263 return ledval_alpha[toupper(c) - 'A'];
264
265 return 0;
266 }
267
268 void led_puts(const char *s)
269 {
270 int i;
271 u32 val = 0;
272
273 if (!support_card_found)
274 return;
275
276 if (!s)
277 return;
278
279 for (i = 0; i < 4; i++) {
280 val <<= 8;
281 val |= char2ledval(*s);
282 if (*s != '\0')
283 s++;
284 }
285
286 writel(~val, support_card_base + LED_OFFSET);
287 }