Tom Rini | 10e4779 | 2018-05-06 17:58:06 -0400 | [diff] [blame] | 1 | // SPDX-License-Identifier: GPL-2.0+ |
Masahiro Yamada | bb2ff9d | 2014-10-03 19:21:06 +0900 | [diff] [blame] | 2 | /* |
Masahiro Yamada | d040170 | 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 | bb2ff9d | 2014-10-03 19:21:06 +0900 | [diff] [blame] | 6 | */ |
| 7 | |
| 8 | #include <common.h> |
Masahiro Yamada | 4e5d28d | 2015-09-22 00:27:31 +0900 | [diff] [blame] | 9 | #include <linux/ctype.h> |
Masahiro Yamada | 663a23f | 2015-05-29 17:30:00 +0900 | [diff] [blame] | 10 | #include <linux/io.h> |
Masahiro Yamada | efdf340 | 2016-01-09 01:51:13 +0900 | [diff] [blame] | 11 | |
| 12 | #include "micro-support-card.h" |
Masahiro Yamada | bb2ff9d | 2014-10-03 19:21:06 +0900 | [diff] [blame] | 13 | |
Masahiro Yamada | 56a2071 | 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 | bcd2244 | 2019-07-10 20:07:36 +0900 | [diff] [blame] | 21 | static bool support_card_found; |
| 22 | |
| 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 | |
Masahiro Yamada | bb2ff9d | 2014-10-03 19:21:06 +0900 | [diff] [blame] | 40 | /* |
| 41 | * 0: reset deassert, 1: reset |
| 42 | * |
| 43 | * bit[0]: LAN, I2C, LED |
| 44 | * bit[1]: UART |
| 45 | */ |
Masahiro Yamada | 8e03270 | 2017-01-15 14:59:06 +0900 | [diff] [blame] | 46 | static void support_card_reset_deassert(void) |
Masahiro Yamada | bb2ff9d | 2014-10-03 19:21:06 +0900 | [diff] [blame] | 47 | { |
Masahiro Yamada | 97edea5 | 2016-03-18 16:41:46 +0900 | [diff] [blame] | 48 | writel(0x00010000, MICRO_SUPPORT_CARD_RESET); |
Masahiro Yamada | bb2ff9d | 2014-10-03 19:21:06 +0900 | [diff] [blame] | 49 | } |
| 50 | |
Masahiro Yamada | 8e03270 | 2017-01-15 14:59:06 +0900 | [diff] [blame] | 51 | static void support_card_reset(void) |
Masahiro Yamada | bb2ff9d | 2014-10-03 19:21:06 +0900 | [diff] [blame] | 52 | { |
Masahiro Yamada | 97edea5 | 2016-03-18 16:41:46 +0900 | [diff] [blame] | 53 | writel(0x00020003, MICRO_SUPPORT_CARD_RESET); |
Masahiro Yamada | bb2ff9d | 2014-10-03 19:21:06 +0900 | [diff] [blame] | 54 | } |
| 55 | |
| 56 | static int support_card_show_revision(void) |
| 57 | { |
| 58 | u32 revision; |
| 59 | |
Masahiro Yamada | 4903b6d | 2015-09-11 20:17:45 +0900 | [diff] [blame] | 60 | revision = readl(MICRO_SUPPORT_CARD_REVISION); |
Masahiro Yamada | d040170 | 2016-08-25 17:02:31 +0900 | [diff] [blame] | 61 | revision &= 0xff; |
| 62 | |
| 63 | /* revision 3.6.x card changed the revision format */ |
Masahiro Yamada | df0c0b2 | 2017-02-20 17:32:19 +0900 | [diff] [blame] | 64 | printf("SC: Micro Support Card (CPLD version %s%d.%d)\n", |
| 65 | revision >> 4 == 6 ? "3." : "", |
Masahiro Yamada | d040170 | 2016-08-25 17:02:31 +0900 | [diff] [blame] | 66 | revision >> 4, revision & 0xf); |
| 67 | |
Masahiro Yamada | bb2ff9d | 2014-10-03 19:21:06 +0900 | [diff] [blame] | 68 | return 0; |
| 69 | } |
Masahiro Yamada | bb2ff9d | 2014-10-03 19:21:06 +0900 | [diff] [blame] | 70 | |
| 71 | void support_card_init(void) |
| 72 | { |
Masahiro Yamada | bcd2244 | 2019-07-10 20:07:36 +0900 | [diff] [blame] | 73 | support_card_detect(); |
| 74 | |
| 75 | if (!support_card_found) |
| 76 | return; |
| 77 | |
Masahiro Yamada | 8e03270 | 2017-01-15 14:59:06 +0900 | [diff] [blame] | 78 | support_card_reset(); |
Masahiro Yamada | bb2ff9d | 2014-10-03 19:21:06 +0900 | [diff] [blame] | 79 | /* |
| 80 | * After power on, we need to keep the LAN controller in reset state |
| 81 | * for a while. (200 usec) |
Masahiro Yamada | bb2ff9d | 2014-10-03 19:21:06 +0900 | [diff] [blame] | 82 | */ |
Masahiro Yamada | a53f74f | 2016-10-08 13:25:29 +0900 | [diff] [blame] | 83 | udelay(200); |
Masahiro Yamada | bb2ff9d | 2014-10-03 19:21:06 +0900 | [diff] [blame] | 84 | support_card_reset_deassert(); |
Masahiro Yamada | df0c0b2 | 2017-02-20 17:32:19 +0900 | [diff] [blame] | 85 | |
| 86 | support_card_show_revision(); |
Masahiro Yamada | bb2ff9d | 2014-10-03 19:21:06 +0900 | [diff] [blame] | 87 | } |
| 88 | |
Masahiro Yamada | bb2ff9d | 2014-10-03 19:21:06 +0900 | [diff] [blame] | 89 | #if defined(CONFIG_SMC911X) |
| 90 | #include <netdev.h> |
| 91 | |
| 92 | int board_eth_init(bd_t *bis) |
| 93 | { |
Masahiro Yamada | bcd2244 | 2019-07-10 20:07:36 +0900 | [diff] [blame] | 94 | if (!support_card_found) |
| 95 | return 0; |
| 96 | |
Masahiro Yamada | 56a2071 | 2015-09-22 00:27:32 +0900 | [diff] [blame] | 97 | return smc911x_initialize(0, SMC911X_BASE); |
Masahiro Yamada | bb2ff9d | 2014-10-03 19:21:06 +0900 | [diff] [blame] | 98 | } |
| 99 | #endif |
| 100 | |
Masahiro Yamada | 8cea9b5 | 2017-02-11 22:43:54 +0900 | [diff] [blame] | 101 | #if defined(CONFIG_MTD_NOR_FLASH) |
Masahiro Yamada | bb2ff9d | 2014-10-03 19:21:06 +0900 | [diff] [blame] | 102 | |
| 103 | #include <mtd/cfi_flash.h> |
| 104 | |
Masahiro Yamada | 8821681 | 2014-12-06 00:03:26 +0900 | [diff] [blame] | 105 | struct memory_bank { |
| 106 | phys_addr_t base; |
| 107 | unsigned long size; |
| 108 | }; |
Masahiro Yamada | bb2ff9d | 2014-10-03 19:21:06 +0900 | [diff] [blame] | 109 | |
Masahiro Yamada | 8821681 | 2014-12-06 00:03:26 +0900 | [diff] [blame] | 110 | static int mem_is_flash(const struct memory_bank *mem) |
Masahiro Yamada | bb2ff9d | 2014-10-03 19:21:06 +0900 | [diff] [blame] | 111 | { |
| 112 | const int loop = 128; |
| 113 | u32 *scratch_addr; |
| 114 | u32 saved_value; |
| 115 | int ret = 1; |
| 116 | int i; |
| 117 | |
Masahiro Yamada | 8821681 | 2014-12-06 00:03:26 +0900 | [diff] [blame] | 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); |
Masahiro Yamada | bb2ff9d | 2014-10-03 19:21:06 +0900 | [diff] [blame] | 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 | |
Masahiro Yamada | 4903b6d | 2015-09-11 20:17:45 +0900 | [diff] [blame] | 138 | /* {address, size} */ |
| 139 | static const struct memory_bank memory_banks[] = { |
Masahiro Yamada | 4b275ea | 2015-09-11 20:17:47 +0900 | [diff] [blame] | 140 | {0x42000000, 0x01f00000}, |
Masahiro Yamada | 8821681 | 2014-12-06 00:03:26 +0900 | [diff] [blame] | 141 | }; |
| 142 | |
Masahiro Yamada | 8821681 | 2014-12-06 00:03:26 +0900 | [diff] [blame] | 143 | static const struct memory_bank |
| 144 | *flash_banks_list[CONFIG_SYS_MAX_FLASH_BANKS_DETECT]; |
| 145 | |
| 146 | phys_addr_t cfi_flash_bank_addr(int i) |
| 147 | { |
| 148 | return flash_banks_list[i]->base; |
| 149 | } |
| 150 | |
| 151 | unsigned long cfi_flash_bank_size(int i) |
Masahiro Yamada | bb2ff9d | 2014-10-03 19:21:06 +0900 | [diff] [blame] | 152 | { |
Masahiro Yamada | 8821681 | 2014-12-06 00:03:26 +0900 | [diff] [blame] | 153 | return flash_banks_list[i]->size; |
| 154 | } |
Masahiro Yamada | bb2ff9d | 2014-10-03 19:21:06 +0900 | [diff] [blame] | 155 | |
Masahiro Yamada | 8821681 | 2014-12-06 00:03:26 +0900 | [diff] [blame] | 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 | |
Masahiro Yamada | 4903b6d | 2015-09-11 20:17:45 +0900 | [diff] [blame] | 162 | memory_bank = memory_banks; |
| 163 | end = memory_bank + ARRAY_SIZE(memory_banks); |
Masahiro Yamada | 8821681 | 2014-12-06 00:03:26 +0900 | [diff] [blame] | 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", |
Masahiro Yamada | 0935010 | 2016-02-26 18:59:45 +0900 | [diff] [blame] | 175 | (unsigned long)memory_bank->base, |
| 176 | (unsigned long)memory_bank->size); |
Masahiro Yamada | 8821681 | 2014-12-06 00:03:26 +0900 | [diff] [blame] | 177 | cfi_flash_num_flash_banks++; |
Masahiro Yamada | bb2ff9d | 2014-10-03 19:21:06 +0900 | [diff] [blame] | 178 | } |
Masahiro Yamada | bb2ff9d | 2014-10-03 19:21:06 +0900 | [diff] [blame] | 179 | } |
| 180 | |
Masahiro Yamada | 8821681 | 2014-12-06 00:03:26 +0900 | [diff] [blame] | 181 | debug("number of flash banks: %d\n", cfi_flash_num_flash_banks); |
Masahiro Yamada | bb2ff9d | 2014-10-03 19:21:06 +0900 | [diff] [blame] | 182 | } |
Masahiro Yamada | 8cea9b5 | 2017-02-11 22:43:54 +0900 | [diff] [blame] | 183 | #else /* CONFIG_MTD_NOR_FLASH */ |
Masahiro Yamada | 8e03270 | 2017-01-15 14:59:06 +0900 | [diff] [blame] | 184 | static void detect_num_flash_banks(void) |
Masahiro Yamada | 8821681 | 2014-12-06 00:03:26 +0900 | [diff] [blame] | 185 | { |
| 186 | }; |
Masahiro Yamada | 8cea9b5 | 2017-02-11 22:43:54 +0900 | [diff] [blame] | 187 | #endif /* CONFIG_MTD_NOR_FLASH */ |
Masahiro Yamada | 8821681 | 2014-12-06 00:03:26 +0900 | [diff] [blame] | 188 | |
| 189 | void support_card_late_init(void) |
| 190 | { |
Masahiro Yamada | bcd2244 | 2019-07-10 20:07:36 +0900 | [diff] [blame] | 191 | if (!support_card_found) |
| 192 | return; |
| 193 | |
Masahiro Yamada | 8821681 | 2014-12-06 00:03:26 +0900 | [diff] [blame] | 194 | detect_num_flash_banks(); |
| 195 | } |
Masahiro Yamada | 4e5d28d | 2015-09-22 00:27:31 +0900 | [diff] [blame] | 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 | |
| 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 | |
| 249 | void led_puts(const char *s) |
| 250 | { |
| 251 | int i; |
| 252 | u32 val = 0; |
| 253 | |
Masahiro Yamada | bcd2244 | 2019-07-10 20:07:36 +0900 | [diff] [blame] | 254 | if (!support_card_found) |
| 255 | return; |
| 256 | |
Masahiro Yamada | 4e5d28d | 2015-09-22 00:27:31 +0900 | [diff] [blame] | 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 | |
Masahiro Yamada | 56a2071 | 2015-09-22 00:27:32 +0900 | [diff] [blame] | 267 | writel(~val, LED_BASE); |
Masahiro Yamada | 4e5d28d | 2015-09-22 00:27:31 +0900 | [diff] [blame] | 268 | } |