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