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