dm: ppc: T4240: add i2c DM support
[platform/kernel/u-boot.git] / board / freescale / t4qds / t4240qds.c
index 89e56a3..869c01d 100644 (file)
@@ -1,28 +1,16 @@
+// SPDX-License-Identifier: GPL-2.0+
 /*
  * Copyright 2009-2012 Freescale Semiconductor, Inc.
- *
- * See file CREDITS for list of people who contributed to this
- * project.
- *
- * This program is free software; you can redistribute it and/or
- * modify it under the terms of the GNU General Public License as
- * published by the Free Software Foundation; either version 2 of
- * the License, or (at your option) any later version.
- *
- * This program is distributed in the hope that it will be useful,
- * but WITHOUT ANY WARRANTY; without even the implied warranty of
- * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
- * GNU General Public License for more details.
- *
- * You should have received a copy of the GNU General Public License
- * along with this program; if not, write to the Free Software
- * Foundation, Inc., 59 Temple Place, Suite 330, Boston,
- * MA 02111-1307 USA
+ * Copyright 2020 NXP
  */
 
 #include <common.h>
 #include <command.h>
+#include <env.h>
+#include <fdt_support.h>
 #include <i2c.h>
+#include <init.h>
+#include <irq_func.h>
 #include <netdev.h>
 #include <linux/compiler.h>
 #include <asm/mmu.h>
@@ -31,7 +19,6 @@
 #include <asm/immap_85xx.h>
 #include <asm/fsl_law.h>
 #include <asm/fsl_serdes.h>
-#include <asm/fsl_portals.h>
 #include <asm/fsl_liodn.h>
 #include <fm_eth.h>
 
 
 DECLARE_GLOBAL_DATA_PTR;
 
-static const int8_t vsc3316_fsm1_tx[8][2] = { {0, 0}, {1, 1}, {6, 6}, {7, 7},
+static int8_t vsc3316_fsm1_tx[8][2] = { {0, 0}, {1, 1}, {6, 6}, {7, 7},
                                {8, 8}, {9, 9}, {14, 14}, {15, 15} };
 
-static const int8_t vsc3316_fsm2_tx[8][2] = { {2, 2}, {3, 3}, {4, 4}, {5, 5},
+static int8_t vsc3316_fsm2_tx[8][2] = { {2, 2}, {3, 3}, {4, 4}, {5, 5},
                                {10, 10}, {11, 11}, {12, 12}, {13, 13} };
 
-static const int8_t vsc3316_fsm1_rx[8][2] = { {2, 12}, {3, 13}, {4, 5}, {5, 4},
+static int8_t vsc3316_fsm1_rx[8][2] = { {2, 12}, {3, 13}, {4, 5}, {5, 4},
                                {10, 11}, {11, 10}, {12, 2}, {13, 3} };
 
-static const int8_t vsc3316_fsm2_rx[8][2] = { {0, 15}, {1, 14}, {6, 7}, {7, 6},
+static int8_t vsc3316_fsm2_rx[8][2] = { {0, 15}, {1, 14}, {6, 7}, {7, 6},
                                {8, 9}, {9, 8}, {14, 1}, {15, 0} };
 
 int checkboard(void)
@@ -105,11 +92,25 @@ int checkboard(void)
        return 0;
 }
 
-int select_i2c_ch_pca9547(u8 ch)
+int select_i2c_ch_pca9547(u8 ch, int bus_num)
 {
        int ret;
 
+#ifdef CONFIG_DM_I2C
+       struct udevice *dev;
+
+       ret = i2c_get_chip_for_busnum(bus_num, I2C_MUX_PCA_ADDR_PRI,
+                                     1, &dev);
+       if (ret) {
+               printf("%s: Cannot find udev for a bus %d\n", __func__,
+                      bus_num);
+               return ret;
+       }
+
+       ret = dm_i2c_write(dev, 0, &ch, 1);
+#else
        ret = i2c_write(I2C_MUX_PCA_ADDR_PRI, 0, 1, &ch, 1);
+#endif
        if (ret) {
                puts("PCA: failed to select proper channel\n");
                return ret;
@@ -129,10 +130,28 @@ static inline int read_voltage(void)
 {
        int i, ret, voltage_read = 0;
        u16 vol_mon;
+#ifdef CONFIG_DM_I2C
+       struct udevice *dev;
+       int bus_num = 0;
+#endif
 
        for (i = 0; i < NUM_READINGS; i++) {
+#ifdef CONFIG_DM_I2C
+               ret = i2c_get_chip_for_busnum(bus_num, I2C_VOL_MONITOR_ADDR,
+                                             1, &dev);
+               if (ret) {
+                       printf("%s: Cannot find udev for a bus %d\n", __func__,
+                              bus_num);
+                       return ret;
+               }
+
+               ret = dm_i2c_read(dev,
+                                 I2C_VOL_MONITOR_BUS_V_OFFSET,
+                                 (void *)&vol_mon, 2);
+#else
                ret = i2c_read(I2C_VOL_MONITOR_ADDR,
                        I2C_VOL_MONITOR_BUS_V_OFFSET, 1, (void *)&vol_mon, 2);
+#endif
                if (ret) {
                        printf("VID: failed to read core voltage\n");
                        return ret;
@@ -264,7 +283,7 @@ static int adjust_vdd(ulong vdd_override)
                unsigned voltage;
        };
 
-       ret = select_i2c_ch_pca9547(I2C_MUX_CH_VOL_MONITOR);
+       ret = select_i2c_ch_pca9547(I2C_MUX_CH_VOL_MONITOR, 0);
        if (ret) {
                debug("VID: I2c failed to switch channel\n");
                ret = -1;
@@ -282,7 +301,7 @@ static int adjust_vdd(ulong vdd_override)
        vdd_target = vdd[vid];
 
        /* check override variable for overriding VDD */
-       vdd_string = getenv("t4240qds_vdd_mv");
+       vdd_string = env_get("t4240qds_vdd_mv");
        if (vdd_override == 0 && vdd_string &&
            !strict_strtoul(vdd_string, 10, &vdd_string_override))
                vdd_override = vdd_string_override;
@@ -362,32 +381,78 @@ int config_frontside_crossbar_vsc3316(void)
        u32 srds_prtcl_s1, srds_prtcl_s2;
        int ret;
 
-       ret = select_i2c_ch_pca9547(I2C_MUX_CH_VSC3316_FS);
+       ret = select_i2c_ch_pca9547(I2C_MUX_CH_VSC3316_FS, 0);
        if (ret)
                return ret;
 
        srds_prtcl_s1 = in_be32(&gur->rcwsr[4]) &
                        FSL_CORENET2_RCWSR4_SRDS1_PRTCL;
        srds_prtcl_s1 >>= FSL_CORENET2_RCWSR4_SRDS1_PRTCL_SHIFT;
-       if (srds_prtcl_s1) {
+       switch (srds_prtcl_s1) {
+       case 37:
+       case 38:
+               /* swap first lane and third lane on slot1 */
+               vsc3316_fsm1_tx[0][1] = 14;
+               vsc3316_fsm1_tx[6][1] = 0;
+               vsc3316_fsm1_rx[1][1] = 2;
+               vsc3316_fsm1_rx[6][1] = 13;
+       case 39:
+       case 40:
+       case 45:
+       case 46:
+       case 47:
+       case 48:
+               /* swap first lane and third lane on slot2 */
+               vsc3316_fsm1_tx[2][1] = 8;
+               vsc3316_fsm1_tx[4][1] = 6;
+               vsc3316_fsm1_rx[2][1] = 10;
+               vsc3316_fsm1_rx[5][1] = 5;
+       default:
                ret = vsc3316_config(VSC3316_FSM_TX_ADDR, vsc3316_fsm1_tx, 8);
                if (ret)
                        return ret;
                ret = vsc3316_config(VSC3316_FSM_RX_ADDR, vsc3316_fsm1_rx, 8);
                if (ret)
                        return ret;
+               break;
        }
 
        srds_prtcl_s2 = in_be32(&gur->rcwsr[4]) &
                                FSL_CORENET2_RCWSR4_SRDS2_PRTCL;
        srds_prtcl_s2 >>= FSL_CORENET2_RCWSR4_SRDS2_PRTCL_SHIFT;
-       if (srds_prtcl_s2) {
+       switch (srds_prtcl_s2) {
+       case 37:
+       case 38:
+               /* swap first lane and third lane on slot3 */
+               vsc3316_fsm2_tx[2][1] = 11;
+               vsc3316_fsm2_tx[5][1] = 4;
+               vsc3316_fsm2_rx[2][1] = 9;
+               vsc3316_fsm2_rx[4][1] = 7;
+       case 39:
+       case 40:
+       case 45:
+       case 46:
+       case 47:
+       case 48:
+       case 49:
+       case 50:
+       case 51:
+       case 52:
+       case 53:
+       case 54:
+               /* swap first lane and third lane on slot4 */
+               vsc3316_fsm2_tx[6][1] = 3;
+               vsc3316_fsm2_tx[1][1] = 12;
+               vsc3316_fsm2_rx[0][1] = 1;
+               vsc3316_fsm2_rx[6][1] = 15;
+       default:
                ret = vsc3316_config(VSC3316_FSM_TX_ADDR, vsc3316_fsm2_tx, 8);
                if (ret)
                        return ret;
                ret = vsc3316_config(VSC3316_FSM_RX_ADDR, vsc3316_fsm2_rx, 8);
                if (ret)
                        return ret;
+               break;
        }
 
        return 0;
@@ -406,6 +471,7 @@ int config_backside_crossbar_mux(void)
        case 0:
                /* SerDes3 is not enabled */
                break;
+       case 1:
        case 2:
        case 9:
        case 10:
@@ -415,13 +481,20 @@ int config_backside_crossbar_mux(void)
                brdcfg |= BRDCFG12_SD3MX_SLOT5;
                QIXIS_WRITE(brdcfg[12], brdcfg);
                break;
+       case 3:
        case 4:
+       case 5:
        case 6:
+       case 7:
        case 8:
+       case 11:
        case 12:
+       case 13:
        case 14:
+       case 15:
        case 16:
        case 17:
+       case 18:
        case 19:
        case 20:
                /* SD3(4:7) => SLOT6(0:3) */
@@ -443,6 +516,7 @@ int config_backside_crossbar_mux(void)
        case 0:
                /* SerDes4 is not enabled */
                break;
+       case 1:
        case 2:
                /* 10b, SD4(0:7) => SLOT7(0:7) */
                brdcfg = QIXIS_READ(brdcfg[12]);
@@ -450,8 +524,11 @@ int config_backside_crossbar_mux(void)
                brdcfg |= BRDCFG12_SD4MX_SLOT7;
                QIXIS_WRITE(brdcfg[12], brdcfg);
                break;
+       case 3:
        case 4:
+       case 5:
        case 6:
+       case 7:
        case 8:
                /* x1b, SD4(4:7) => SLOT8(0:3) */
                brdcfg = QIXIS_READ(brdcfg[12]);
@@ -459,9 +536,13 @@ int config_backside_crossbar_mux(void)
                brdcfg |= BRDCFG12_SD4MX_SLOT8;
                QIXIS_WRITE(brdcfg[12], brdcfg);
                break;
+       case 9:
        case 10:
+       case 11:
        case 12:
+       case 13:
        case 14:
+       case 15:
        case 16:
        case 18:
                /* 00b, SD4(4:5) => AURORA, SD4(6:7) => SATA */
@@ -482,7 +563,7 @@ int config_backside_crossbar_mux(void)
 int board_early_init_r(void)
 {
        const unsigned int flashbase = CONFIG_SYS_FLASH_BASE;
-       const u8 flash_esel = find_tlb_idx((void *)flashbase, 1);
+       int flash_esel = find_tlb_idx((void *)flashbase, 1);
 
        /*
         * Remap Boot flash + PROMJET region to caching-inhibited
@@ -493,18 +574,19 @@ int board_early_init_r(void)
        flush_dcache();
        invalidate_icache();
 
-       /* invalidate existing TLB entry for flash + promjet */
-       disable_tlb(flash_esel);
+       if (flash_esel == -1) {
+               /* very unlikely unless something is messed up */
+               puts("Error: Could not find TLB for FLASH BASE\n");
+               flash_esel = 2; /* give our best effort to continue */
+       } else {
+               /* invalidate existing TLB entry for flash + promjet */
+               disable_tlb(flash_esel);
+       }
 
        set_tlb(1, flashbase, CONFIG_SYS_FLASH_BASE_PHYS,
                MAS3_SX|MAS3_SW|MAS3_SR, MAS2_I|MAS2_G,
                0, flash_esel, BOOKE_PAGESZ_256M, 1);
 
-       set_liodns();
-#ifdef CONFIG_SYS_DPAA_QBMAN
-       setup_portals();
-#endif
-
        /* Disable remote I2C connection to qixis fpga */
        QIXIS_WRITE(brdcfg[5], QIXIS_READ(brdcfg[5]) & ~BRDCFG5_IRE);
 
@@ -518,7 +600,7 @@ int board_early_init_r(void)
        /* Configure board SERDES ports crossbar */
        config_frontside_crossbar_vsc3316();
        config_backside_crossbar_mux();
-       select_i2c_ch_pca9547(I2C_MUX_CH_DEFAULT);
+       select_i2c_ch_pca9547(I2C_MUX_CH_DEFAULT, 0);
 
        return 0;
 }
@@ -589,28 +671,13 @@ unsigned long get_board_ddr_clk(void)
        return 66666666;
 }
 
-static const char *serdes_clock_to_string(u32 clock)
-{
-       switch (clock) {
-       case SRDS_PLLCR0_RFCK_SEL_100:
-               return "100";
-       case SRDS_PLLCR0_RFCK_SEL_125:
-               return "125";
-       case SRDS_PLLCR0_RFCK_SEL_156_25:
-               return "156.25";
-       case SRDS_PLLCR0_RFCK_SEL_161_13:
-               return "161.1328125";
-       default:
-               return "???";
-       }
-}
-
 int misc_init_r(void)
 {
        u8 sw;
-       serdes_corenet_t *srds_regs =
-               (void *)CONFIG_SYS_FSL_CORENET_SERDES_ADDR;
+       void *srds_base = (void *)CONFIG_SYS_FSL_CORENET_SERDES_ADDR;
+       serdes_corenet_t *srds_regs;
        u32 actual[MAX_SERDES];
+       u32 pllcr0, expected;
        unsigned int i;
 
        sw = QIXIS_READ(brdcfg[2]);
@@ -633,8 +700,9 @@ int misc_init_r(void)
        }
 
        for (i = 0; i < MAX_SERDES; i++) {
-               u32 pllcr0 = srds_regs->bank[i].pllcr0;
-               u32 expected = pllcr0 & SRDS_PLLCR0_RFCK_SEL_MASK;
+               srds_regs = srds_base + i * 0x1000;
+               pllcr0 = srds_regs->bank[0].pllcr0;
+               expected = pllcr0 & SRDS_PLLCR0_RFCK_SEL_MASK;
                if (expected != actual[i]) {
                        printf("Warning: SERDES%u expects reference clock %sMHz, but actual is %sMHz\n",
                               i + 1, serdes_clock_to_string(expected),
@@ -645,15 +713,15 @@ int misc_init_r(void)
        return 0;
 }
 
-void ft_board_setup(void *blob, bd_t *bd)
+int ft_board_setup(void *blob, bd_t *bd)
 {
        phys_addr_t base;
        phys_size_t size;
 
        ft_cpu_setup(blob, bd);
 
-       base = getenv_bootm_low();
-       size = getenv_bootm_size();
+       base = env_get_bootm_low();
+       size = env_get_bootm_size();
 
        fdt_fixup_memory(blob, (u64)base, (u64)size);
 
@@ -662,12 +730,14 @@ void ft_board_setup(void *blob, bd_t *bd)
 #endif
 
        fdt_fixup_liodn(blob);
-       fdt_fixup_dr_usb(blob, bd);
+       fsl_fdt_fixup_dr_usb(blob, bd);
 
 #ifdef CONFIG_SYS_DPAA_FMAN
        fdt_fixup_fman_ethernet(blob);
        fdt_fixup_board_enet(blob);
 #endif
+
+       return 0;
 }
 
 /*
@@ -695,11 +765,11 @@ void board_detail(void)
        }
 
        /* Voltage secion */
-       if (!select_i2c_ch_pca9547(I2C_MUX_CH_VOL_MONITOR)) {
+       if (!select_i2c_ch_pca9547(I2C_MUX_CH_VOL_MONITOR, 0)) {
                vdd = read_voltage();
                if (vdd > 0)
                        printf("Core voltage= %d mV\n", vdd);
-               select_i2c_ch_pca9547(I2C_MUX_CH_DEFAULT);
+               select_i2c_ch_pca9547(I2C_MUX_CH_DEFAULT, 0);
        }
 
        printf("XVDD        = 1.%d V\n", ((brdcfg[8] & 0xf) - 4) * 5 + 25);