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