blob: d23c0bd12927cbe4aecee5955f8f455b01a3bd78 [file] [log] [blame]
Tom Rini10e47792018-05-06 17:58:06 -04001// SPDX-License-Identifier: GPL-2.0+
Masahiro Yamadabb2ff9d2014-10-03 19:21:06 +09002/*
Masahiro Yamadad0401702016-08-25 17:02:31 +09003 * Copyright (C) 2012-2015 Panasonic Corporation
Masahiro Yamadabc9daa02020-02-14 20:54:42 +09004 * Copyright (C) 2015-2020 Socionext Inc.
Masahiro Yamadad0401702016-08-25 17:02:31 +09005 * Author: Masahiro Yamada <yamada.masahiro@socionext.com>
Masahiro Yamadabb2ff9d2014-10-03 19:21:06 +09006 */
7
8#include <common.h>
Masahiro Yamadabbec08c2020-05-20 12:31:24 +09009#include <dm/of.h>
Masahiro Yamadabc9daa02020-02-14 20:54:42 +090010#include <fdt_support.h>
Simon Glass0f2af882020-05-10 11:40:05 -060011#include <log.h>
Masahiro Yamada4e5d28d2015-09-22 00:27:31 +090012#include <linux/ctype.h>
Simon Glassdbd79542020-05-10 11:40:11 -060013#include <linux/delay.h>
Masahiro Yamada663a23f2015-05-29 17:30:00 +090014#include <linux/io.h>
Masahiro Yamadaefdf3402016-01-09 01:51:13 +090015
16#include "micro-support-card.h"
Masahiro Yamadabb2ff9d2014-10-03 19:21:06 +090017
Masahiro Yamadabc9daa02020-02-14 20:54:42 +090018#define SMC911X_OFFSET 0x00000
19#define LED_OFFSET 0x90000
20#define NS16550A_OFFSET 0xb0000
21#define MICRO_SUPPORT_CARD_RESET 0xd0034
22#define MICRO_SUPPORT_CARD_REVISION 0xd00e0
Masahiro Yamada56a20712015-09-22 00:27:32 +090023
Masahiro Yamadabcd22442019-07-10 20:07:36 +090024static bool support_card_found;
Masahiro Yamadabc9daa02020-02-14 20:54:42 +090025static void __iomem *support_card_base;
Masahiro Yamadabcd22442019-07-10 20:07:36 +090026
27static void support_card_detect(void)
28{
29 DECLARE_GLOBAL_DATA_PTR;
30 const void *fdt = gd->fdt_blob;
31 int offset;
Masahiro Yamadabc9daa02020-02-14 20:54:42 +090032 u64 addr, addr2;
Masahiro Yamadabcd22442019-07-10 20:07:36 +090033
34 offset = fdt_node_offset_by_compatible(fdt, 0, "smsc,lan9118");
35 if (offset < 0)
36 return;
37
Masahiro Yamadabc9daa02020-02-14 20:54:42 +090038 addr = fdt_get_base_address(fdt, offset);
39 if (addr == OF_BAD_ADDR)
40 return;
41 addr -= SMC911X_OFFSET;
42
Masahiro Yamadabcd22442019-07-10 20:07:36 +090043 offset = fdt_node_offset_by_compatible(fdt, 0, "ns16550a");
44 if (offset < 0)
45 return;
46
Masahiro Yamadabc9daa02020-02-14 20:54:42 +090047 addr2 = fdt_get_base_address(fdt, offset);
48 if (addr2 == OF_BAD_ADDR)
49 return;
50 addr2 -= NS16550A_OFFSET;
51
52 /* sanity check */
53 if (addr != addr2)
54 return;
55
56 support_card_base = ioremap(addr, 0x100000);
57
Masahiro Yamadabcd22442019-07-10 20:07:36 +090058 support_card_found = true;
59}
60
Masahiro Yamadabb2ff9d2014-10-03 19:21:06 +090061/*
62 * 0: reset deassert, 1: reset
63 *
64 * bit[0]: LAN, I2C, LED
65 * bit[1]: UART
66 */
Masahiro Yamada8e032702017-01-15 14:59:06 +090067static void support_card_reset_deassert(void)
Masahiro Yamadabb2ff9d2014-10-03 19:21:06 +090068{
Masahiro Yamadabc9daa02020-02-14 20:54:42 +090069 writel(0x00010000, support_card_base + MICRO_SUPPORT_CARD_RESET);
Masahiro Yamadabb2ff9d2014-10-03 19:21:06 +090070}
71
Masahiro Yamada8e032702017-01-15 14:59:06 +090072static void support_card_reset(void)
Masahiro Yamadabb2ff9d2014-10-03 19:21:06 +090073{
Masahiro Yamadabc9daa02020-02-14 20:54:42 +090074 writel(0x00020003, support_card_base + MICRO_SUPPORT_CARD_RESET);
Masahiro Yamadabb2ff9d2014-10-03 19:21:06 +090075}
76
77static int support_card_show_revision(void)
78{
79 u32 revision;
80
Masahiro Yamadabc9daa02020-02-14 20:54:42 +090081 revision = readl(support_card_base + MICRO_SUPPORT_CARD_REVISION);
Masahiro Yamadad0401702016-08-25 17:02:31 +090082 revision &= 0xff;
83
84 /* revision 3.6.x card changed the revision format */
Masahiro Yamadadf0c0b22017-02-20 17:32:19 +090085 printf("SC: Micro Support Card (CPLD version %s%d.%d)\n",
86 revision >> 4 == 6 ? "3." : "",
Masahiro Yamadad0401702016-08-25 17:02:31 +090087 revision >> 4, revision & 0xf);
88
Masahiro Yamadabb2ff9d2014-10-03 19:21:06 +090089 return 0;
90}
Masahiro Yamadabb2ff9d2014-10-03 19:21:06 +090091
92void support_card_init(void)
93{
Masahiro Yamadabcd22442019-07-10 20:07:36 +090094 support_card_detect();
95
96 if (!support_card_found)
97 return;
98
Masahiro Yamada8e032702017-01-15 14:59:06 +090099 support_card_reset();
Masahiro Yamadabb2ff9d2014-10-03 19:21:06 +0900100 /*
101 * After power on, we need to keep the LAN controller in reset state
102 * for a while. (200 usec)
Masahiro Yamadabb2ff9d2014-10-03 19:21:06 +0900103 */
Masahiro Yamadaa53f74f2016-10-08 13:25:29 +0900104 udelay(200);
Masahiro Yamadabb2ff9d2014-10-03 19:21:06 +0900105 support_card_reset_deassert();
Masahiro Yamadadf0c0b22017-02-20 17:32:19 +0900106
107 support_card_show_revision();
Masahiro Yamadabb2ff9d2014-10-03 19:21:06 +0900108}
109
Masahiro Yamadabb2ff9d2014-10-03 19:21:06 +0900110#if defined(CONFIG_SMC911X)
111#include <netdev.h>
112
113int board_eth_init(bd_t *bis)
114{
Masahiro Yamadabcd22442019-07-10 20:07:36 +0900115 if (!support_card_found)
116 return 0;
117
Masahiro Yamadabc9daa02020-02-14 20:54:42 +0900118 return smc911x_initialize(0, (unsigned long)support_card_base + SMC911X_OFFSET);
Masahiro Yamadabb2ff9d2014-10-03 19:21:06 +0900119}
120#endif
121
Masahiro Yamada8cea9b52017-02-11 22:43:54 +0900122#if defined(CONFIG_MTD_NOR_FLASH)
Masahiro Yamadabb2ff9d2014-10-03 19:21:06 +0900123
124#include <mtd/cfi_flash.h>
125
Masahiro Yamada88216812014-12-06 00:03:26 +0900126struct memory_bank {
127 phys_addr_t base;
128 unsigned long size;
129};
Masahiro Yamadabb2ff9d2014-10-03 19:21:06 +0900130
Masahiro Yamada88216812014-12-06 00:03:26 +0900131static int mem_is_flash(const struct memory_bank *mem)
Masahiro Yamadabb2ff9d2014-10-03 19:21:06 +0900132{
133 const int loop = 128;
134 u32 *scratch_addr;
135 u32 saved_value;
136 int ret = 1;
137 int i;
138
Masahiro Yamada88216812014-12-06 00:03:26 +0900139 /* just in case, use the tail of the memory bank */
140 scratch_addr = map_physmem(mem->base + mem->size - sizeof(u32) * loop,
141 sizeof(u32) * loop, MAP_NOCACHE);
Masahiro Yamadabb2ff9d2014-10-03 19:21:06 +0900142
143 for (i = 0; i < loop; i++, scratch_addr++) {
144 saved_value = readl(scratch_addr);
145 writel(~saved_value, scratch_addr);
146 if (readl(scratch_addr) != saved_value) {
147 /* We assume no memory or SRAM here. */
148 writel(saved_value, scratch_addr);
149 ret = 0;
150 break;
151 }
152 }
153
154 unmap_physmem(scratch_addr, MAP_NOCACHE);
155
156 return ret;
157}
158
Masahiro Yamada4903b6d2015-09-11 20:17:45 +0900159/* {address, size} */
160static const struct memory_bank memory_banks[] = {
Masahiro Yamada4b275ea2015-09-11 20:17:47 +0900161 {0x42000000, 0x01f00000},
Masahiro Yamada88216812014-12-06 00:03:26 +0900162};
163
Masahiro Yamada88216812014-12-06 00:03:26 +0900164static const struct memory_bank
165*flash_banks_list[CONFIG_SYS_MAX_FLASH_BANKS_DETECT];
166
167phys_addr_t cfi_flash_bank_addr(int i)
168{
169 return flash_banks_list[i]->base;
170}
171
172unsigned long cfi_flash_bank_size(int i)
Masahiro Yamadabb2ff9d2014-10-03 19:21:06 +0900173{
Masahiro Yamada88216812014-12-06 00:03:26 +0900174 return flash_banks_list[i]->size;
175}
Masahiro Yamadabb2ff9d2014-10-03 19:21:06 +0900176
Masahiro Yamada88216812014-12-06 00:03:26 +0900177static void detect_num_flash_banks(void)
178{
179 const struct memory_bank *memory_bank, *end;
180
181 cfi_flash_num_flash_banks = 0;
182
Masahiro Yamada4903b6d2015-09-11 20:17:45 +0900183 memory_bank = memory_banks;
184 end = memory_bank + ARRAY_SIZE(memory_banks);
Masahiro Yamada88216812014-12-06 00:03:26 +0900185
186 for (; memory_bank < end; memory_bank++) {
187 if (cfi_flash_num_flash_banks >=
188 CONFIG_SYS_MAX_FLASH_BANKS_DETECT)
189 break;
190
191 if (mem_is_flash(memory_bank)) {
192 flash_banks_list[cfi_flash_num_flash_banks] =
193 memory_bank;
194
195 debug("flash bank found: base = 0x%lx, size = 0x%lx\n",
Masahiro Yamada09350102016-02-26 18:59:45 +0900196 (unsigned long)memory_bank->base,
197 (unsigned long)memory_bank->size);
Masahiro Yamada88216812014-12-06 00:03:26 +0900198 cfi_flash_num_flash_banks++;
Masahiro Yamadabb2ff9d2014-10-03 19:21:06 +0900199 }
Masahiro Yamadabb2ff9d2014-10-03 19:21:06 +0900200 }
201
Masahiro Yamada88216812014-12-06 00:03:26 +0900202 debug("number of flash banks: %d\n", cfi_flash_num_flash_banks);
Masahiro Yamadabb2ff9d2014-10-03 19:21:06 +0900203}
Masahiro Yamada8cea9b52017-02-11 22:43:54 +0900204#else /* CONFIG_MTD_NOR_FLASH */
Masahiro Yamada8e032702017-01-15 14:59:06 +0900205static void detect_num_flash_banks(void)
Masahiro Yamada88216812014-12-06 00:03:26 +0900206{
207};
Masahiro Yamada8cea9b52017-02-11 22:43:54 +0900208#endif /* CONFIG_MTD_NOR_FLASH */
Masahiro Yamada88216812014-12-06 00:03:26 +0900209
210void support_card_late_init(void)
211{
Masahiro Yamadabcd22442019-07-10 20:07:36 +0900212 if (!support_card_found)
213 return;
214
Masahiro Yamada88216812014-12-06 00:03:26 +0900215 detect_num_flash_banks();
216}
Masahiro Yamada4e5d28d2015-09-22 00:27:31 +0900217
218static const u8 ledval_num[] = {
219 0x7e, /* 0 */
220 0x0c, /* 1 */
221 0xb6, /* 2 */
222 0x9e, /* 3 */
223 0xcc, /* 4 */
224 0xda, /* 5 */
225 0xfa, /* 6 */
226 0x4e, /* 7 */
227 0xfe, /* 8 */
228 0xde, /* 9 */
229};
230
231static const u8 ledval_alpha[] = {
232 0xee, /* A */
233 0xf8, /* B */
234 0x72, /* C */
235 0xbc, /* D */
236 0xf2, /* E */
237 0xe2, /* F */
238 0x7a, /* G */
239 0xe8, /* H */
240 0x08, /* I */
241 0x3c, /* J */
242 0xea, /* K */
243 0x70, /* L */
244 0x6e, /* M */
245 0xa8, /* N */
246 0xb8, /* O */
247 0xe6, /* P */
248 0xce, /* Q */
249 0xa0, /* R */
250 0xc8, /* S */
251 0x8c, /* T */
252 0x7c, /* U */
253 0x54, /* V */
254 0xfc, /* W */
255 0xec, /* X */
256 0xdc, /* Y */
257 0xa4, /* Z */
258};
259
260static u8 char2ledval(char c)
261{
262 if (isdigit(c))
263 return ledval_num[c - '0'];
264 else if (isalpha(c))
265 return ledval_alpha[toupper(c) - 'A'];
266
267 return 0;
268}
269
270void led_puts(const char *s)
271{
272 int i;
273 u32 val = 0;
274
Masahiro Yamadabcd22442019-07-10 20:07:36 +0900275 if (!support_card_found)
276 return;
277
Masahiro Yamada4e5d28d2015-09-22 00:27:31 +0900278 if (!s)
279 return;
280
281 for (i = 0; i < 4; i++) {
282 val <<= 8;
283 val |= char2ledval(*s);
284 if (*s != '\0')
285 s++;
286 }
287
Masahiro Yamadabc9daa02020-02-14 20:54:42 +0900288 writel(~val, support_card_base + LED_OFFSET);
Masahiro Yamada4e5d28d2015-09-22 00:27:31 +0900289}