blob: 95780f79c2c3b002645896177bf4e83497b88b61 [file] [log] [blame]
Tom Rini83d290c2018-05-06 17:58:06 -04001// SPDX-License-Identifier: GPL-2.0+
Masahiro Yamada5894ca02014-10-03 19:21:06 +09002/*
Masahiro Yamada499c8672016-08-25 17:02:31 +09003 * Copyright (C) 2012-2015 Panasonic Corporation
Masahiro Yamada2ba1d392020-02-14 20:54:42 +09004 * Copyright (C) 2015-2020 Socionext Inc.
Masahiro Yamada499c8672016-08-25 17:02:31 +09005 * Author: Masahiro Yamada <yamada.masahiro@socionext.com>
Masahiro Yamada5894ca02014-10-03 19:21:06 +09006 */
7
Masahiro Yamada86227492020-07-09 15:08:19 +09008#include <dm.h>
Masahiro Yamada2ba1d392020-02-14 20:54:42 +09009#include <fdt_support.h>
Masahiro Yamada84697002015-09-22 00:27:31 +090010#include <linux/ctype.h>
Simon Glassc05ed002020-05-10 11:40:11 -060011#include <linux/delay.h>
Masahiro Yamadaf6e7f072015-05-29 17:30:00 +090012#include <linux/io.h>
Masahiro Yamada6717e152020-05-20 12:31:27 +090013#include <asm/global_data.h>
Masahiro Yamada107b3fb2016-01-09 01:51:13 +090014
15#include "micro-support-card.h"
Masahiro Yamada5894ca02014-10-03 19:21:06 +090016
Masahiro Yamada2ba1d392020-02-14 20:54:42 +090017#define SMC911X_OFFSET 0x00000
18#define LED_OFFSET 0x90000
19#define NS16550A_OFFSET 0xb0000
20#define MICRO_SUPPORT_CARD_RESET 0xd0034
21#define MICRO_SUPPORT_CARD_REVISION 0xd00e0
Masahiro Yamadad7728aa2015-09-22 00:27:32 +090022
Masahiro Yamada92d65242019-07-10 20:07:36 +090023static bool support_card_found;
Masahiro Yamada2ba1d392020-02-14 20:54:42 +090024static void __iomem *support_card_base;
Masahiro Yamada92d65242019-07-10 20:07:36 +090025
26static void support_card_detect(void)
27{
28 DECLARE_GLOBAL_DATA_PTR;
29 const void *fdt = gd->fdt_blob;
30 int offset;
Masahiro Yamada2ba1d392020-02-14 20:54:42 +090031 u64 addr, addr2;
Masahiro Yamada92d65242019-07-10 20:07:36 +090032
33 offset = fdt_node_offset_by_compatible(fdt, 0, "smsc,lan9118");
34 if (offset < 0)
35 return;
36
Masahiro Yamada2ba1d392020-02-14 20:54:42 +090037 addr = fdt_get_base_address(fdt, offset);
38 if (addr == OF_BAD_ADDR)
39 return;
40 addr -= SMC911X_OFFSET;
41
Masahiro Yamada92d65242019-07-10 20:07:36 +090042 offset = fdt_node_offset_by_compatible(fdt, 0, "ns16550a");
43 if (offset < 0)
44 return;
45
Masahiro Yamada2ba1d392020-02-14 20:54:42 +090046 addr2 = fdt_get_base_address(fdt, offset);
47 if (addr2 == OF_BAD_ADDR)
48 return;
49 addr2 -= NS16550A_OFFSET;
50
51 /* sanity check */
52 if (addr != addr2)
53 return;
54
55 support_card_base = ioremap(addr, 0x100000);
56
Masahiro Yamada92d65242019-07-10 20:07:36 +090057 support_card_found = true;
58}
59
Masahiro Yamada5894ca02014-10-03 19:21:06 +090060/*
61 * 0: reset deassert, 1: reset
62 *
63 * bit[0]: LAN, I2C, LED
64 * bit[1]: UART
65 */
Masahiro Yamadaef07a992017-01-15 14:59:06 +090066static void support_card_reset_deassert(void)
Masahiro Yamada5894ca02014-10-03 19:21:06 +090067{
Masahiro Yamada2ba1d392020-02-14 20:54:42 +090068 writel(0x00010000, support_card_base + MICRO_SUPPORT_CARD_RESET);
Masahiro Yamada5894ca02014-10-03 19:21:06 +090069}
70
Masahiro Yamadaef07a992017-01-15 14:59:06 +090071static void support_card_reset(void)
Masahiro Yamada5894ca02014-10-03 19:21:06 +090072{
Masahiro Yamada2ba1d392020-02-14 20:54:42 +090073 writel(0x00020003, support_card_base + MICRO_SUPPORT_CARD_RESET);
Masahiro Yamada5894ca02014-10-03 19:21:06 +090074}
75
76static int support_card_show_revision(void)
77{
78 u32 revision;
79
Masahiro Yamada2ba1d392020-02-14 20:54:42 +090080 revision = readl(support_card_base + MICRO_SUPPORT_CARD_REVISION);
Masahiro Yamada499c8672016-08-25 17:02:31 +090081 revision &= 0xff;
82
83 /* revision 3.6.x card changed the revision format */
Masahiro Yamada6fc84912017-02-20 17:32:19 +090084 printf("SC: Micro Support Card (CPLD version %s%d.%d)\n",
85 revision >> 4 == 6 ? "3." : "",
Masahiro Yamada499c8672016-08-25 17:02:31 +090086 revision >> 4, revision & 0xf);
87
Masahiro Yamada5894ca02014-10-03 19:21:06 +090088 return 0;
89}
Masahiro Yamada5894ca02014-10-03 19:21:06 +090090
91void support_card_init(void)
92{
Masahiro Yamada86227492020-07-09 15:08:19 +090093 struct udevice *dev;
94 int ret;
95
96 /* The system bus must be initialized for access to the support card. */
97 ret = uclass_get_device_by_driver(UCLASS_SIMPLE_BUS,
Simon Glass65e25be2020-12-28 20:34:56 -070098 DM_DRIVER_GET(uniphier_system_bus_driver),
Masahiro Yamada86227492020-07-09 15:08:19 +090099 &dev);
100 if (ret)
101 return;
102
103 /* Check DT to see if this board has the support card. */
Masahiro Yamada92d65242019-07-10 20:07:36 +0900104 support_card_detect();
105
106 if (!support_card_found)
107 return;
108
Masahiro Yamadaef07a992017-01-15 14:59:06 +0900109 support_card_reset();
Masahiro Yamada5894ca02014-10-03 19:21:06 +0900110 /*
111 * After power on, we need to keep the LAN controller in reset state
112 * for a while. (200 usec)
Masahiro Yamada5894ca02014-10-03 19:21:06 +0900113 */
Masahiro Yamada66e3efe2016-10-08 13:25:29 +0900114 udelay(200);
Masahiro Yamada5894ca02014-10-03 19:21:06 +0900115 support_card_reset_deassert();
Masahiro Yamada6fc84912017-02-20 17:32:19 +0900116
117 support_card_show_revision();
Masahiro Yamada5894ca02014-10-03 19:21:06 +0900118}
119
Masahiro Yamada84697002015-09-22 00:27:31 +0900120static const u8 ledval_num[] = {
121 0x7e, /* 0 */
122 0x0c, /* 1 */
123 0xb6, /* 2 */
124 0x9e, /* 3 */
125 0xcc, /* 4 */
126 0xda, /* 5 */
127 0xfa, /* 6 */
128 0x4e, /* 7 */
129 0xfe, /* 8 */
130 0xde, /* 9 */
131};
132
133static const u8 ledval_alpha[] = {
134 0xee, /* A */
135 0xf8, /* B */
136 0x72, /* C */
137 0xbc, /* D */
138 0xf2, /* E */
139 0xe2, /* F */
140 0x7a, /* G */
141 0xe8, /* H */
142 0x08, /* I */
143 0x3c, /* J */
144 0xea, /* K */
145 0x70, /* L */
146 0x6e, /* M */
147 0xa8, /* N */
148 0xb8, /* O */
149 0xe6, /* P */
150 0xce, /* Q */
151 0xa0, /* R */
152 0xc8, /* S */
153 0x8c, /* T */
154 0x7c, /* U */
155 0x54, /* V */
156 0xfc, /* W */
157 0xec, /* X */
158 0xdc, /* Y */
159 0xa4, /* Z */
160};
161
162static u8 char2ledval(char c)
163{
164 if (isdigit(c))
165 return ledval_num[c - '0'];
166 else if (isalpha(c))
167 return ledval_alpha[toupper(c) - 'A'];
168
169 return 0;
170}
171
172void led_puts(const char *s)
173{
174 int i;
175 u32 val = 0;
176
Masahiro Yamada92d65242019-07-10 20:07:36 +0900177 if (!support_card_found)
178 return;
179
Masahiro Yamada84697002015-09-22 00:27:31 +0900180 if (!s)
181 return;
182
183 for (i = 0; i < 4; i++) {
184 val <<= 8;
185 val |= char2ledval(*s);
186 if (*s != '\0')
187 s++;
188 }
189
Masahiro Yamada2ba1d392020-02-14 20:54:42 +0900190 writel(~val, support_card_base + LED_OFFSET);
Masahiro Yamada84697002015-09-22 00:27:31 +0900191}