Merge tag 'u-boot-atmel-fixes-2020.07-a' of https://gitlab.denx.de/u-boot/custodians...
[oweals/u-boot.git] / board / Marvell / mvebu_armada-37xx / board.c
1 // SPDX-License-Identifier: GPL-2.0+
2 /*
3  * Copyright (C) 2016 Stefan Roese <sr@denx.de>
4  */
5
6 #include <common.h>
7 #include <dm.h>
8 #include <i2c.h>
9 #include <init.h>
10 #include <phy.h>
11 #include <asm/io.h>
12 #include <asm/arch/cpu.h>
13 #include <asm/arch/soc.h>
14 #include <linux/delay.h>
15
16 DECLARE_GLOBAL_DATA_PTR;
17
18 /* IO expander I2C device */
19 #define I2C_IO_EXP_ADDR         0x22
20 #define I2C_IO_CFG_REG_0        0x6
21 #define I2C_IO_DATA_OUT_REG_0   0x2
22 #define I2C_IO_REG_0_SATA_OFF   2
23 #define I2C_IO_REG_0_USB_H_OFF  1
24
25 /* The pin control values are the same for DB and Espressobin */
26 #define PINCTRL_NB_REG_VALUE    0x000173fa
27 #define PINCTRL_SB_REG_VALUE    0x00007a23
28
29 /* Ethernet switch registers */
30 /* SMI addresses for multi-chip mode */
31 #define MVEBU_PORT_CTRL_SMI_ADDR(p)     (16 + (p))
32 #define MVEBU_SW_G2_SMI_ADDR            (28)
33
34 /* Multi-chip mode */
35 #define MVEBU_SW_SMI_DATA_REG           (1)
36 #define MVEBU_SW_SMI_CMD_REG            (0)
37  #define SW_SMI_CMD_REG_ADDR_OFF        0
38  #define SW_SMI_CMD_DEV_ADDR_OFF        5
39  #define SW_SMI_CMD_SMI_OP_OFF          10
40  #define SW_SMI_CMD_SMI_MODE_OFF        12
41  #define SW_SMI_CMD_SMI_BUSY_OFF        15
42
43 /* Single-chip mode */
44 /* Switch Port Registers */
45 #define MVEBU_SW_LINK_CTRL_REG          (1)
46 #define MVEBU_SW_PORT_CTRL_REG          (4)
47
48 /* Global 2 Registers */
49 #define MVEBU_G2_SMI_PHY_CMD_REG        (24)
50 #define MVEBU_G2_SMI_PHY_DATA_REG       (25)
51
52 int board_early_init_f(void)
53 {
54         return 0;
55 }
56
57 int board_init(void)
58 {
59         /* adress of boot parameters */
60         gd->bd->bi_boot_params = CONFIG_SYS_SDRAM_BASE + 0x100;
61
62         return 0;
63 }
64
65 /* Board specific AHCI / SATA enable code */
66 int board_ahci_enable(void)
67 {
68         struct udevice *dev;
69         int ret;
70         u8 buf[8];
71
72         /* Only DB requres this configuration */
73         if (!of_machine_is_compatible("marvell,armada-3720-db"))
74                 return 0;
75
76         /* Configure IO exander PCA9555: 7bit address 0x22 */
77         ret = i2c_get_chip_for_busnum(0, I2C_IO_EXP_ADDR, 1, &dev);
78         if (ret) {
79                 printf("Cannot find PCA9555: %d\n", ret);
80                 return 0;
81         }
82
83         ret = dm_i2c_read(dev, I2C_IO_CFG_REG_0, buf, 1);
84         if (ret) {
85                 printf("Failed to read IO expander value via I2C\n");
86                 return -EIO;
87         }
88
89         /*
90          * Enable SATA power via IO expander connected via I2C by setting
91          * the corresponding bit to output mode to enable power for SATA
92          */
93         buf[0] &= ~(1 << I2C_IO_REG_0_SATA_OFF);
94         ret = dm_i2c_write(dev, I2C_IO_CFG_REG_0, buf, 1);
95         if (ret) {
96                 printf("Failed to set IO expander via I2C\n");
97                 return -EIO;
98         }
99
100         return 0;
101 }
102
103 /* Board specific xHCI enable code */
104 int board_xhci_enable(fdt_addr_t base)
105 {
106         struct udevice *dev;
107         int ret;
108         u8 buf[8];
109
110         /* Only DB requres this configuration */
111         if (!of_machine_is_compatible("marvell,armada-3720-db"))
112                 return 0;
113
114         /* Configure IO exander PCA9555: 7bit address 0x22 */
115         ret = i2c_get_chip_for_busnum(0, I2C_IO_EXP_ADDR, 1, &dev);
116         if (ret) {
117                 printf("Cannot find PCA9555: %d\n", ret);
118                 return 0;
119         }
120
121         printf("Enable USB VBUS\n");
122
123         /*
124          * Read configuration (direction) and set VBUS pin as output
125          * (reset pin = output)
126          */
127         ret = dm_i2c_read(dev, I2C_IO_CFG_REG_0, buf, 1);
128         if (ret) {
129                 printf("Failed to read IO expander value via I2C\n");
130                 return -EIO;
131         }
132         buf[0] &= ~(1 << I2C_IO_REG_0_USB_H_OFF);
133         ret = dm_i2c_write(dev, I2C_IO_CFG_REG_0, buf, 1);
134         if (ret) {
135                 printf("Failed to set IO expander via I2C\n");
136                 return -EIO;
137         }
138
139         /* Read VBUS output value and disable it */
140         ret = dm_i2c_read(dev, I2C_IO_DATA_OUT_REG_0, buf, 1);
141         if (ret) {
142                 printf("Failed to read IO expander value via I2C\n");
143                 return -EIO;
144         }
145         buf[0] &= ~(1 << I2C_IO_REG_0_USB_H_OFF);
146         ret = dm_i2c_write(dev, I2C_IO_DATA_OUT_REG_0, buf, 1);
147         if (ret) {
148                 printf("Failed to set IO expander via I2C\n");
149                 return -EIO;
150         }
151
152         /*
153          * Required delay for configuration to settle - must wait for
154          * power on port is disabled in case VBUS signal was high,
155          * required 3 seconds delay to let VBUS signal fully settle down
156          */
157         mdelay(3000);
158
159         /* Enable VBUS power: Set output value of VBUS pin as enabled */
160         buf[0] |= (1 << I2C_IO_REG_0_USB_H_OFF);
161         ret = dm_i2c_write(dev, I2C_IO_DATA_OUT_REG_0, buf, 1);
162         if (ret) {
163                 printf("Failed to set IO expander via I2C\n");
164                 return -EIO;
165         }
166
167         mdelay(500); /* required delay to let output value settle */
168
169         return 0;
170 }
171
172 /* Helper function for accessing switch devices in multi-chip connection mode */
173 static int mii_multi_chip_mode_write(struct mii_dev *bus, int dev_smi_addr,
174                                      int smi_addr, int reg, u16 value)
175 {
176         u16 smi_cmd = 0;
177
178         if (bus->write(bus, dev_smi_addr, 0,
179                        MVEBU_SW_SMI_DATA_REG, value) != 0) {
180                 printf("Error writing to the PHY addr=%02x reg=%02x\n",
181                        smi_addr, reg);
182                 return -EFAULT;
183         }
184
185         smi_cmd = (1 << SW_SMI_CMD_SMI_BUSY_OFF) |
186                   (1 << SW_SMI_CMD_SMI_MODE_OFF) |
187                   (1 << SW_SMI_CMD_SMI_OP_OFF) |
188                   (smi_addr << SW_SMI_CMD_DEV_ADDR_OFF) |
189                   (reg << SW_SMI_CMD_REG_ADDR_OFF);
190         if (bus->write(bus, dev_smi_addr, 0,
191                        MVEBU_SW_SMI_CMD_REG, smi_cmd) != 0) {
192                 printf("Error writing to the PHY addr=%02x reg=%02x\n",
193                        smi_addr, reg);
194                 return -EFAULT;
195         }
196
197         return 0;
198 }
199
200 /* Bring-up board-specific network stuff */
201 int board_network_enable(struct mii_dev *bus)
202 {
203         if (!of_machine_is_compatible("marvell,armada-3720-espressobin"))
204                 return 0;
205
206         /*
207          * FIXME: remove this code once Topaz driver gets available
208          * A3720 Community Board Only
209          * Configure Topaz switch (88E6341)
210          * Set port 0,1,2,3 to forwarding Mode (through Switch Port registers)
211          */
212         mii_multi_chip_mode_write(bus, 1, MVEBU_PORT_CTRL_SMI_ADDR(0),
213                                   MVEBU_SW_PORT_CTRL_REG, 0x7f);
214         mii_multi_chip_mode_write(bus, 1, MVEBU_PORT_CTRL_SMI_ADDR(1),
215                                   MVEBU_SW_PORT_CTRL_REG, 0x7f);
216         mii_multi_chip_mode_write(bus, 1, MVEBU_PORT_CTRL_SMI_ADDR(2),
217                                   MVEBU_SW_PORT_CTRL_REG, 0x7f);
218         mii_multi_chip_mode_write(bus, 1, MVEBU_PORT_CTRL_SMI_ADDR(3),
219                                   MVEBU_SW_PORT_CTRL_REG, 0x7f);
220
221         /* RGMII Delay on Port 0 (CPU port), force link to 1000Mbps */
222         mii_multi_chip_mode_write(bus, 1, MVEBU_PORT_CTRL_SMI_ADDR(0),
223                                   MVEBU_SW_LINK_CTRL_REG, 0xe002);
224
225         /* Power up PHY 1, 2, 3 (through Global 2 registers) */
226         mii_multi_chip_mode_write(bus, 1, MVEBU_SW_G2_SMI_ADDR,
227                                   MVEBU_G2_SMI_PHY_DATA_REG, 0x1140);
228         mii_multi_chip_mode_write(bus, 1, MVEBU_SW_G2_SMI_ADDR,
229                                   MVEBU_G2_SMI_PHY_CMD_REG, 0x9620);
230         mii_multi_chip_mode_write(bus, 1, MVEBU_SW_G2_SMI_ADDR,
231                                   MVEBU_G2_SMI_PHY_CMD_REG, 0x9640);
232         mii_multi_chip_mode_write(bus, 1, MVEBU_SW_G2_SMI_ADDR,
233                                   MVEBU_G2_SMI_PHY_CMD_REG, 0x9660);
234
235         return 0;
236 }