Stefan Roese | 7be1b9b | 2016-05-25 08:21:21 +0200 | [diff] [blame] | 1 | /* |
| 2 | * Copyright (C) 2016 Stefan Roese <sr@denx.de> |
| 3 | * |
| 4 | * SPDX-License-Identifier: GPL-2.0+ |
| 5 | */ |
| 6 | |
| 7 | #include <common.h> |
| 8 | #include <i2c.h> |
| 9 | #include <asm/io.h> |
| 10 | #include <asm/arch/cpu.h> |
| 11 | #include <asm/arch/soc.h> |
| 12 | |
| 13 | DECLARE_GLOBAL_DATA_PTR; |
| 14 | |
| 15 | /* IO expander I2C device */ |
| 16 | #define I2C_IO_EXP_ADDR 0x21 |
| 17 | #define I2C_IO_CFG_REG_0 0x6 |
| 18 | #define I2C_IO_DATA_OUT_REG_0 0x2 |
| 19 | /* VBus enable */ |
| 20 | #define I2C_IO_REG_0_USB_H0_OFF 0 |
| 21 | #define I2C_IO_REG_0_USB_H1_OFF 1 |
| 22 | #define I2C_IO_REG_VBUS ((1 << I2C_IO_REG_0_USB_H0_OFF) | \ |
| 23 | (1 << I2C_IO_REG_0_USB_H1_OFF)) |
| 24 | /* Current limit */ |
| 25 | #define I2C_IO_REG_0_USB_H0_CL 4 |
| 26 | #define I2C_IO_REG_0_USB_H1_CL 5 |
| 27 | #define I2C_IO_REG_CL ((1 << I2C_IO_REG_0_USB_H0_CL) | \ |
| 28 | (1 << I2C_IO_REG_0_USB_H1_CL)) |
| 29 | |
| 30 | static int usb_enabled = 0; |
| 31 | |
| 32 | /* Board specific xHCI dis-/enable code */ |
| 33 | |
| 34 | /* |
| 35 | * Set USB VBUS signals (via I2C IO expander/GPIO) as output and set |
| 36 | * output value as disabled |
| 37 | * |
| 38 | * Set USB Current Limit signals (via I2C IO expander/GPIO) as output |
| 39 | * and set output value as enabled |
| 40 | */ |
| 41 | int board_xhci_config(void) |
| 42 | { |
| 43 | struct udevice *dev; |
| 44 | int ret; |
| 45 | u8 buf[8]; |
| 46 | |
| 47 | /* Configure IO exander PCA9555: 7bit address 0x21 */ |
| 48 | ret = i2c_get_chip_for_busnum(0, I2C_IO_EXP_ADDR, 1, &dev); |
| 49 | if (ret) { |
| 50 | printf("Cannot find PCA9555: %d\n", ret); |
| 51 | return 0; |
| 52 | } |
| 53 | |
| 54 | /* |
| 55 | * Read configuration (direction) and set VBUS pin as output |
| 56 | * (reset pin = output) |
| 57 | */ |
| 58 | ret = dm_i2c_read(dev, I2C_IO_CFG_REG_0, buf, 1); |
| 59 | if (ret) { |
| 60 | printf("Failed to read IO expander value via I2C\n"); |
| 61 | return -EIO; |
| 62 | } |
| 63 | buf[0] &= ~I2C_IO_REG_VBUS; |
| 64 | buf[0] &= ~I2C_IO_REG_CL; |
| 65 | ret = dm_i2c_write(dev, I2C_IO_CFG_REG_0, buf, 1); |
| 66 | if (ret) { |
| 67 | printf("Failed to set IO expander via I2C\n"); |
| 68 | return -EIO; |
| 69 | } |
| 70 | |
| 71 | /* Read output value and configure it */ |
| 72 | ret = dm_i2c_read(dev, I2C_IO_DATA_OUT_REG_0, buf, 1); |
| 73 | if (ret) { |
| 74 | printf("Failed to read IO expander value via I2C\n"); |
| 75 | return -EIO; |
| 76 | } |
| 77 | buf[0] &= ~I2C_IO_REG_VBUS; |
| 78 | buf[0] |= I2C_IO_REG_CL; |
| 79 | ret = dm_i2c_write(dev, I2C_IO_DATA_OUT_REG_0, buf, 1); |
| 80 | if (ret) { |
| 81 | printf("Failed to set IO expander via I2C\n"); |
| 82 | return -EIO; |
| 83 | } |
| 84 | |
| 85 | mdelay(500); /* required delay to let output value settle */ |
| 86 | |
| 87 | return 0; |
| 88 | } |
| 89 | |
| 90 | int board_xhci_enable(void) |
| 91 | { |
| 92 | struct udevice *dev; |
| 93 | int ret; |
| 94 | u8 buf[8]; |
| 95 | |
| 96 | /* |
| 97 | * This function enables all USB ports simultaniously, |
| 98 | * it only needs to get called once |
| 99 | */ |
| 100 | if (usb_enabled) |
| 101 | return 0; |
| 102 | |
| 103 | /* Configure IO exander PCA9555: 7bit address 0x21 */ |
| 104 | ret = i2c_get_chip_for_busnum(0, I2C_IO_EXP_ADDR, 1, &dev); |
| 105 | if (ret) { |
| 106 | printf("Cannot find PCA9555: %d\n", ret); |
| 107 | return 0; |
| 108 | } |
| 109 | |
| 110 | /* Read VBUS output value */ |
| 111 | ret = dm_i2c_read(dev, I2C_IO_DATA_OUT_REG_0, buf, 1); |
| 112 | if (ret) { |
| 113 | printf("Failed to read IO expander value via I2C\n"); |
| 114 | return -EIO; |
| 115 | } |
| 116 | |
| 117 | /* Enable VBUS power: Set output value of VBUS pin as enabled */ |
| 118 | buf[0] |= I2C_IO_REG_VBUS; |
| 119 | ret = dm_i2c_write(dev, I2C_IO_DATA_OUT_REG_0, buf, 1); |
| 120 | if (ret) { |
| 121 | printf("Failed to set IO expander via I2C\n"); |
| 122 | return -EIO; |
| 123 | } |
| 124 | |
| 125 | mdelay(500); /* required delay to let output value settle */ |
| 126 | usb_enabled = 1; |
| 127 | |
| 128 | return 0; |
| 129 | } |
| 130 | |
| 131 | int board_early_init_f(void) |
| 132 | { |
| 133 | /* Nothing to do (yet), perhaps later some pin-muxing etc */ |
| 134 | |
| 135 | return 0; |
| 136 | } |
| 137 | |
| 138 | int board_init(void) |
| 139 | { |
| 140 | /* adress of boot parameters */ |
| 141 | gd->bd->bi_boot_params = CONFIG_SYS_SDRAM_BASE + 0x100; |
| 142 | |
| 143 | return 0; |
| 144 | } |
| 145 | |
| 146 | int board_late_init(void) |
| 147 | { |
| 148 | /* Pre-configure the USB ports (overcurrent, VBus) */ |
| 149 | board_xhci_config(); |
| 150 | |
| 151 | return 0; |
| 152 | } |