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