blob: c71470a20429378cd67f9505211abcf20b89ee82 [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 Yamadabc9daa02020-02-14 20:54:42 +09009#include <dm/of.h>
10#include <fdt_support.h>
Masahiro Yamada4e5d28d2015-09-22 00:27:31 +090011#include <linux/ctype.h>
Masahiro Yamada663a23f2015-05-29 17:30:00 +090012#include <linux/io.h>
Masahiro Yamadaefdf3402016-01-09 01:51:13 +090013
14#include "micro-support-card.h"
Masahiro Yamadabb2ff9d2014-10-03 19:21:06 +090015
Masahiro Yamadabc9daa02020-02-14 20:54:42 +090016#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
Masahiro Yamada56a20712015-09-22 00:27:32 +090021
Masahiro Yamadabcd22442019-07-10 20:07:36 +090022static bool support_card_found;
Masahiro Yamadabc9daa02020-02-14 20:54:42 +090023static void __iomem *support_card_base;
Masahiro Yamadabcd22442019-07-10 20:07:36 +090024
25static void support_card_detect(void)
26{
27 DECLARE_GLOBAL_DATA_PTR;
28 const void *fdt = gd->fdt_blob;
29 int offset;
Masahiro Yamadabc9daa02020-02-14 20:54:42 +090030 u64 addr, addr2;
Masahiro Yamadabcd22442019-07-10 20:07:36 +090031
32 offset = fdt_node_offset_by_compatible(fdt, 0, "smsc,lan9118");
33 if (offset < 0)
34 return;
35
Masahiro Yamadabc9daa02020-02-14 20:54:42 +090036 addr = fdt_get_base_address(fdt, offset);
37 if (addr == OF_BAD_ADDR)
38 return;
39 addr -= SMC911X_OFFSET;
40
Masahiro Yamadabcd22442019-07-10 20:07:36 +090041 offset = fdt_node_offset_by_compatible(fdt, 0, "ns16550a");
42 if (offset < 0)
43 return;
44
Masahiro Yamadabc9daa02020-02-14 20:54:42 +090045 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
Masahiro Yamadabcd22442019-07-10 20:07:36 +090056 support_card_found = true;
57}
58
Masahiro Yamadabb2ff9d2014-10-03 19:21:06 +090059/*
60 * 0: reset deassert, 1: reset
61 *
62 * bit[0]: LAN, I2C, LED
63 * bit[1]: UART
64 */
Masahiro Yamada8e032702017-01-15 14:59:06 +090065static void support_card_reset_deassert(void)
Masahiro Yamadabb2ff9d2014-10-03 19:21:06 +090066{
Masahiro Yamadabc9daa02020-02-14 20:54:42 +090067 writel(0x00010000, support_card_base + MICRO_SUPPORT_CARD_RESET);
Masahiro Yamadabb2ff9d2014-10-03 19:21:06 +090068}
69
Masahiro Yamada8e032702017-01-15 14:59:06 +090070static void support_card_reset(void)
Masahiro Yamadabb2ff9d2014-10-03 19:21:06 +090071{
Masahiro Yamadabc9daa02020-02-14 20:54:42 +090072 writel(0x00020003, support_card_base + MICRO_SUPPORT_CARD_RESET);
Masahiro Yamadabb2ff9d2014-10-03 19:21:06 +090073}
74
75static int support_card_show_revision(void)
76{
77 u32 revision;
78
Masahiro Yamadabc9daa02020-02-14 20:54:42 +090079 revision = readl(support_card_base + MICRO_SUPPORT_CARD_REVISION);
Masahiro Yamadad0401702016-08-25 17:02:31 +090080 revision &= 0xff;
81
82 /* revision 3.6.x card changed the revision format */
Masahiro Yamadadf0c0b22017-02-20 17:32:19 +090083 printf("SC: Micro Support Card (CPLD version %s%d.%d)\n",
84 revision >> 4 == 6 ? "3." : "",
Masahiro Yamadad0401702016-08-25 17:02:31 +090085 revision >> 4, revision & 0xf);
86
Masahiro Yamadabb2ff9d2014-10-03 19:21:06 +090087 return 0;
88}
Masahiro Yamadabb2ff9d2014-10-03 19:21:06 +090089
90void support_card_init(void)
91{
Masahiro Yamadabcd22442019-07-10 20:07:36 +090092 support_card_detect();
93
94 if (!support_card_found)
95 return;
96
Masahiro Yamada8e032702017-01-15 14:59:06 +090097 support_card_reset();
Masahiro Yamadabb2ff9d2014-10-03 19:21:06 +090098 /*
99 * After power on, we need to keep the LAN controller in reset state
100 * for a while. (200 usec)
Masahiro Yamadabb2ff9d2014-10-03 19:21:06 +0900101 */
Masahiro Yamadaa53f74f2016-10-08 13:25:29 +0900102 udelay(200);
Masahiro Yamadabb2ff9d2014-10-03 19:21:06 +0900103 support_card_reset_deassert();
Masahiro Yamadadf0c0b22017-02-20 17:32:19 +0900104
105 support_card_show_revision();
Masahiro Yamadabb2ff9d2014-10-03 19:21:06 +0900106}
107
Masahiro Yamadabb2ff9d2014-10-03 19:21:06 +0900108#if defined(CONFIG_SMC911X)
109#include <netdev.h>
110
111int board_eth_init(bd_t *bis)
112{
Masahiro Yamadabcd22442019-07-10 20:07:36 +0900113 if (!support_card_found)
114 return 0;
115
Masahiro Yamadabc9daa02020-02-14 20:54:42 +0900116 return smc911x_initialize(0, (unsigned long)support_card_base + SMC911X_OFFSET);
Masahiro Yamadabb2ff9d2014-10-03 19:21:06 +0900117}
118#endif
119
Masahiro Yamada8cea9b52017-02-11 22:43:54 +0900120#if defined(CONFIG_MTD_NOR_FLASH)
Masahiro Yamadabb2ff9d2014-10-03 19:21:06 +0900121
122#include <mtd/cfi_flash.h>
123
Masahiro Yamada88216812014-12-06 00:03:26 +0900124struct memory_bank {
125 phys_addr_t base;
126 unsigned long size;
127};
Masahiro Yamadabb2ff9d2014-10-03 19:21:06 +0900128
Masahiro Yamada88216812014-12-06 00:03:26 +0900129static int mem_is_flash(const struct memory_bank *mem)
Masahiro Yamadabb2ff9d2014-10-03 19:21:06 +0900130{
131 const int loop = 128;
132 u32 *scratch_addr;
133 u32 saved_value;
134 int ret = 1;
135 int i;
136
Masahiro Yamada88216812014-12-06 00:03:26 +0900137 /* 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);
Masahiro Yamadabb2ff9d2014-10-03 19:21:06 +0900140
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
Masahiro Yamada4903b6d2015-09-11 20:17:45 +0900157/* {address, size} */
158static const struct memory_bank memory_banks[] = {
Masahiro Yamada4b275ea2015-09-11 20:17:47 +0900159 {0x42000000, 0x01f00000},
Masahiro Yamada88216812014-12-06 00:03:26 +0900160};
161
Masahiro Yamada88216812014-12-06 00:03:26 +0900162static const struct memory_bank
163*flash_banks_list[CONFIG_SYS_MAX_FLASH_BANKS_DETECT];
164
165phys_addr_t cfi_flash_bank_addr(int i)
166{
167 return flash_banks_list[i]->base;
168}
169
170unsigned long cfi_flash_bank_size(int i)
Masahiro Yamadabb2ff9d2014-10-03 19:21:06 +0900171{
Masahiro Yamada88216812014-12-06 00:03:26 +0900172 return flash_banks_list[i]->size;
173}
Masahiro Yamadabb2ff9d2014-10-03 19:21:06 +0900174
Masahiro Yamada88216812014-12-06 00:03:26 +0900175static void detect_num_flash_banks(void)
176{
177 const struct memory_bank *memory_bank, *end;
178
179 cfi_flash_num_flash_banks = 0;
180
Masahiro Yamada4903b6d2015-09-11 20:17:45 +0900181 memory_bank = memory_banks;
182 end = memory_bank + ARRAY_SIZE(memory_banks);
Masahiro Yamada88216812014-12-06 00:03:26 +0900183
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",
Masahiro Yamada09350102016-02-26 18:59:45 +0900194 (unsigned long)memory_bank->base,
195 (unsigned long)memory_bank->size);
Masahiro Yamada88216812014-12-06 00:03:26 +0900196 cfi_flash_num_flash_banks++;
Masahiro Yamadabb2ff9d2014-10-03 19:21:06 +0900197 }
Masahiro Yamadabb2ff9d2014-10-03 19:21:06 +0900198 }
199
Masahiro Yamada88216812014-12-06 00:03:26 +0900200 debug("number of flash banks: %d\n", cfi_flash_num_flash_banks);
Masahiro Yamadabb2ff9d2014-10-03 19:21:06 +0900201}
Masahiro Yamada8cea9b52017-02-11 22:43:54 +0900202#else /* CONFIG_MTD_NOR_FLASH */
Masahiro Yamada8e032702017-01-15 14:59:06 +0900203static void detect_num_flash_banks(void)
Masahiro Yamada88216812014-12-06 00:03:26 +0900204{
205};
Masahiro Yamada8cea9b52017-02-11 22:43:54 +0900206#endif /* CONFIG_MTD_NOR_FLASH */
Masahiro Yamada88216812014-12-06 00:03:26 +0900207
208void support_card_late_init(void)
209{
Masahiro Yamadabcd22442019-07-10 20:07:36 +0900210 if (!support_card_found)
211 return;
212
Masahiro Yamada88216812014-12-06 00:03:26 +0900213 detect_num_flash_banks();
214}
Masahiro Yamada4e5d28d2015-09-22 00:27:31 +0900215
216static 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
229static 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
258static 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
268void led_puts(const char *s)
269{
270 int i;
271 u32 val = 0;
272
Masahiro Yamadabcd22442019-07-10 20:07:36 +0900273 if (!support_card_found)
274 return;
275
Masahiro Yamada4e5d28d2015-09-22 00:27:31 +0900276 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
Masahiro Yamadabc9daa02020-02-14 20:54:42 +0900286 writel(~val, support_card_base + LED_OFFSET);
Masahiro Yamada4e5d28d2015-09-22 00:27:31 +0900287}