1 // SPDX-License-Identifier: GPL-2.0+
2 /*
3  * Copyright (C) 2011 Simon Guinot <sguinot@lacie.com>
4  *
5  * Based on Kirkwood support:
6  * (C) Copyright 2009
7  * Marvell Semiconductor <www.marvell.com>
8  * Written-by: Prafulla Wadaskar <prafulla@marvell.com>
9  */
10 
11 #include <common.h>
12 #include <command.h>
13 #include <environment.h>
14 #include <i2c.h>
15 #include <asm/mach-types.h>
16 #include <asm/arch/cpu.h>
17 #include <asm/arch/soc.h>
18 #include <asm/arch/mpp.h>
19 #include <asm/arch/gpio.h>
20 
21 #include "net2big_v2.h"
22 #include "../common/common.h"
23 #include "../common/cpld-gpio-bus.h"
24 
25 DECLARE_GLOBAL_DATA_PTR;
26 
27 int board_early_init_f(void)
28 {
29 	/* GPIO configuration */
30 	mvebu_config_gpio(NET2BIG_V2_OE_VAL_LOW, NET2BIG_V2_OE_VAL_HIGH,
31 			  NET2BIG_V2_OE_LOW, NET2BIG_V2_OE_HIGH);
32 
33 	/* Multi-Purpose Pins Functionality configuration */
34 	static const u32 kwmpp_config[] = {
35 		MPP0_SPI_SCn,
36 		MPP1_SPI_MOSI,
37 		MPP2_SPI_SCK,
38 		MPP3_SPI_MISO,
39 		MPP6_SYSRST_OUTn,
40 		MPP7_GPO,		/* Request power-off */
41 		MPP8_TW_SDA,
42 		MPP9_TW_SCK,
43 		MPP10_UART0_TXD,
44 		MPP11_UART0_RXD,
45 		MPP13_GPIO,		/* Rear power switch (on|auto) */
46 		MPP14_GPIO,		/* USB fuse alarm */
47 		MPP15_GPIO,		/* Rear power switch (auto|off) */
48 		MPP16_GPIO,		/* SATA HDD1 power */
49 		MPP17_GPIO,		/* SATA HDD2 power */
50 		MPP20_SATA1_ACTn,
51 		MPP21_SATA0_ACTn,
52 		MPP24_GPIO,		/* USB mode select */
53 		MPP26_GPIO,		/* USB device vbus */
54 		MPP28_GPIO,		/* USB enable host vbus */
55 		MPP29_GPIO,		/* CPLD GPIO bus ALE */
56 		MPP34_GPIO,		/* Rear Push button 0=on 1=off */
57 		MPP35_GPIO,		/* Inhibit switch power-off */
58 		MPP36_GPIO,		/* SATA HDD1 presence */
59 		MPP37_GPIO,		/* SATA HDD2 presence */
60 		MPP40_GPIO,		/* eSATA presence */
61 		MPP44_GPIO,		/* CPLD GPIO bus (data 0) */
62 		MPP45_GPIO,		/* CPLD GPIO bus (data 1) */
63 		MPP46_GPIO,		/* CPLD GPIO bus (data 2) */
64 		MPP47_GPIO,		/* CPLD GPIO bus (addr 0) */
65 		MPP48_GPIO,		/* CPLD GPIO bus (addr 1) */
66 		MPP49_GPIO,		/* CPLD GPIO bus (addr 2) */
67 		0
68 	};
69 
70 	kirkwood_mpp_conf(kwmpp_config, NULL);
71 
72 	return 0;
73 }
74 
75 int board_init(void)
76 {
77 	/* Machine number */
78 	gd->bd->bi_arch_number = MACH_TYPE_NET2BIG_V2;
79 
80 	/* Boot parameters address */
81 	gd->bd->bi_boot_params = mvebu_sdram_bar(0) + 0x100;
82 
83 	return 0;
84 }
85 
86 #if defined(CONFIG_MISC_INIT_R)
87 
88 #if defined(CONFIG_CMD_I2C) && defined(CONFIG_SYS_I2C_G762_ADDR)
89 /*
90  * Start I2C fan (GMT G762 controller)
91  */
92 static void init_fan(void)
93 {
94 	u8 data;
95 
96 	i2c_set_bus_num(0);
97 
98 	/* Enable open-loop and PWM modes */
99 	data = 0x20;
100 	if (i2c_write(CONFIG_SYS_I2C_G762_ADDR,
101 		      G762_REG_FAN_CMD1, 1, &data, 1) != 0)
102 		goto err;
103 	data = 0;
104 	if (i2c_write(CONFIG_SYS_I2C_G762_ADDR,
105 		      G762_REG_SET_CNT, 1, &data, 1) != 0)
106 		goto err;
107 	/*
108 	 * RPM to PWM (set_out register) fan speed conversion array:
109 	 * 0    0x00
110 	 * 1500	0x04
111 	 * 2800	0x08
112 	 * 3400	0x0C
113 	 * 3700	0x10
114 	 * 4400	0x20
115 	 * 4700	0x30
116 	 * 4800	0x50
117 	 * 5200	0x80
118 	 * 5400	0xC0
119 	 * 5500	0xFF
120 	 *
121 	 * Start fan at low speed (2800 RPM):
122 	 */
123 	data = 0x08;
124 	if (i2c_write(CONFIG_SYS_I2C_G762_ADDR,
125 		      G762_REG_SET_OUT, 1, &data, 1) != 0)
126 		goto err;
127 
128 	return;
129 err:
130 	printf("Error: failed to start I2C fan @%02x\n",
131 	       CONFIG_SYS_I2C_G762_ADDR);
132 }
133 #else
134 static void init_fan(void) {}
135 #endif /* CONFIG_CMD_I2C && CONFIG_SYS_I2C_G762_ADDR */
136 
137 #if defined(CONFIG_NET2BIG_V2) && defined(CONFIG_KIRKWOOD_GPIO)
138 /*
139  * CPLD GPIO bus:
140  *
141  * - address register : bit [0-2] -> GPIO [47-49]
142  * - data register    : bit [0-2] -> GPIO [44-46]
143  * - enable register  : GPIO 29
144  */
145 static unsigned cpld_gpio_bus_addr[] = { 47, 48, 49 };
146 static unsigned cpld_gpio_bus_data[] = { 44, 45, 46 };
147 
148 static struct cpld_gpio_bus cpld_gpio_bus = {
149 	.addr		= cpld_gpio_bus_addr,
150 	.num_addr	= ARRAY_SIZE(cpld_gpio_bus_addr),
151 	.data		= cpld_gpio_bus_data,
152 	.num_data	= ARRAY_SIZE(cpld_gpio_bus_data),
153 	.enable		= 29,
154 };
155 
156 /*
157  * LEDs configuration:
158  *
159  * The LEDs are controlled by a CPLD and can be configured through
160  * the CPLD GPIO bus.
161  *
162  * Address register selection:
163  *
164  * addr | register
165  * ----------------------------
166  *   0  | front LED
167  *   1  | front LED brightness
168  *   2  | SATA LED brightness
169  *   3  | SATA0 LED
170  *   4  | SATA1 LED
171  *   5  | SATA2 LED
172  *   6  | SATA3 LED
173  *   7  | SATA4 LED
174  *
175  * Data register configuration:
176  *
177  * data | LED brightness
178  * -------------------------------------------------
179  *   0  | min (off)
180  *   -  | -
181  *   7  | max
182  *
183  * data | front LED mode
184  * -------------------------------------------------
185  *   0  | fix off
186  *   1  | fix blue on
187  *   2  | fix red on
188  *   3  | blink blue on=1 sec and blue off=1 sec
189  *   4  | blink red on=1 sec and red off=1 sec
190  *   5  | blink blue on=2.5 sec and red on=0.5 sec
191  *   6  | blink blue on=1 sec and red on=1 sec
192  *   7  | blink blue on=0.5 sec and blue off=2.5 sec
193  *
194  * data | SATA LED mode
195  * -------------------------------------------------
196  *   0  | fix off
197  *   1  | SATA activity blink
198  *   2  | fix red on
199  *   3  | blink blue on=1 sec and blue off=1 sec
200  *   4  | blink red on=1 sec and red off=1 sec
201  *   5  | blink blue on=2.5 sec and red on=0.5 sec
202  *   6  | blink blue on=1 sec and red on=1 sec
203  *   7  | fix blue on
204  */
205 static void init_leds(void)
206 {
207 	/* Enable the front blue LED */
208 	cpld_gpio_bus_write(&cpld_gpio_bus, 0, 1);
209 	cpld_gpio_bus_write(&cpld_gpio_bus, 1, 3);
210 
211 	/* Configure SATA LEDs to blink in relation with the SATA activity */
212 	cpld_gpio_bus_write(&cpld_gpio_bus, 3, 1);
213 	cpld_gpio_bus_write(&cpld_gpio_bus, 4, 1);
214 	cpld_gpio_bus_write(&cpld_gpio_bus, 2, 3);
215 }
216 #else
217 static void init_leds(void) {}
218 #endif /* CONFIG_NET2BIG_V2 && CONFIG_KIRKWOOD_GPIO */
219 
220 int misc_init_r(void)
221 {
222 	init_fan();
223 #if defined(CONFIG_CMD_I2C) && defined(CONFIG_SYS_I2C_EEPROM_ADDR)
224 	if (!env_get("ethaddr")) {
225 		uchar mac[6];
226 		if (lacie_read_mac_address(mac) == 0)
227 			eth_env_set_enetaddr("ethaddr", mac);
228 	}
229 #endif
230 	init_leds();
231 
232 	return 0;
233 }
234 #endif /* CONFIG_MISC_INIT_R */
235 
236 #if defined(CONFIG_CMD_NET) && defined(CONFIG_RESET_PHY_R)
237 /* Configure and initialize PHY */
238 void reset_phy(void)
239 {
240 	mv_phy_88e1116_init("egiga0", 8);
241 }
242 #endif
243 
244 #if defined(CONFIG_KIRKWOOD_GPIO)
245 /* Return GPIO push button status */
246 static int
247 do_read_push_button(cmd_tbl_t *cmdtp, int flag, int argc, char * const argv[])
248 {
249 	return !kw_gpio_get_value(NET2BIG_V2_GPIO_PUSH_BUTTON);
250 }
251 
252 U_BOOT_CMD(button, 1, 1, do_read_push_button,
253 	   "Return GPIO push button status 0=off 1=on", "");
254 #endif
255