void interface_init(void) { debug("i2c init"); /* allocate DMA handles and initialise DMA */ rx_dma = stm32_dmachannel(DMACHAN_I2C1_RX); i2c_rx_setup(); tx_dma = stm32_dmachannel(DMACHAN_I2C1_TX); i2c_tx_setup(); /* enable the i2c block clock and reset it */ modifyreg32(STM32_RCC_APB1ENR, 0, RCC_APB1ENR_I2C1EN); modifyreg32(STM32_RCC_APB1RSTR, 0, RCC_APB1RSTR_I2C1RST); modifyreg32(STM32_RCC_APB1RSTR, RCC_APB1RSTR_I2C1RST, 0); /* configure the i2c GPIOs */ stm32_configgpio(GPIO_I2C1_SCL); stm32_configgpio(GPIO_I2C1_SDA); /* soft-reset the block */ rCR1 |= I2C_CR1_SWRST; rCR1 = 0; /* set for DMA operation */ rCR2 |= I2C_CR2_ITEVFEN | I2C_CR2_ITERREN | I2C_CR2_DMAEN; /* set the frequency value in CR2 */ rCR2 &= ~I2C_CR2_FREQ_MASK; rCR2 |= STM32_PCLK1_FREQUENCY / 1000000; /* set divisor and risetime for fast mode */ uint16_t result = STM32_PCLK1_FREQUENCY / (400000 * 25); if (result < 1) { result = 1; } result = 3; rCCR &= ~I2C_CCR_CCR_MASK; rCCR |= I2C_CCR_DUTY | I2C_CCR_FS | result; rTRISE = (uint16_t)((((STM32_PCLK1_FREQUENCY / 1000000) * 300) / 1000) + 1); /* set our device address */ rOAR1 = 0x1a << 1; /* enable event interrupts */ irq_attach(STM32_IRQ_I2C1EV, i2c_interrupt); irq_attach(STM32_IRQ_I2C1ER, i2c_interrupt); up_enable_irq(STM32_IRQ_I2C1EV); up_enable_irq(STM32_IRQ_I2C1ER); /* and enable the I2C port */ rCR1 |= I2C_CR1_ACK | I2C_CR1_PE; #ifdef DEBUG i2c_dump(); #endif }
int google_chromeec_command(struct chromeec_command *cec_command) { EcCommandI2c cmd; EcResponseI2c resp; int bus = CONFIG_EC_GOOGLE_CHROMEEC_I2C_BUS; int chip = CONFIG_EC_GOOGLE_CHROMEEC_I2C_CHIP; size_t size_i2c_cmd = (sizeof(cmd) - sizeof(cmd.data) + cec_command->cmd_size_in + 1), size_i2c_resp = (sizeof(resp) - sizeof(resp.data) + cec_command->cmd_size_out + 1); if (cec_command->cmd_size_in > MAX_I2C_DATA_SIZE || cec_command->cmd_size_out > MAX_I2C_DATA_SIZE) { printk(BIOS_ERR, "%s: Command data size too large (%d,%d)\n", __func__, cec_command->cmd_size_in, cec_command->cmd_size_out); cec_command->cmd_code = EC_RES_INVALID_PARAM; return 1; } /* Construct command. */ cmd.version = EC_CMD_VERSION0 + cec_command->cmd_version; cmd.command = cec_command->cmd_code; cmd.length = cec_command->cmd_size_in; memcpy(cmd.data, cec_command->cmd_data_in, cmd.length); ec_fill_checksum(&cmd); /* Start I2C communication */ i2c_dump(bus, chip, (const uint8_t *)&cmd, size_i2c_cmd); if (i2c_write(bus, chip, 0, 0, (uint8_t *)&cmd, size_i2c_cmd) != 0) { printk(BIOS_ERR, "%s: Cannot complete write to i2c-%d:%#x\n", __func__, bus, chip); cec_command->cmd_code = EC_RES_ERROR; return 1; } if (i2c_read(bus, chip, 0, 0, (uint8_t *)&resp, size_i2c_resp) != 0) { printk(BIOS_ERR, "%s: Cannot complete read from i2c-%d:%#x\n", __func__, bus, chip); cec_command->cmd_code = EC_RES_ERROR; return 1; } /* Verify and return response */ cec_command->cmd_code = resp.response; if (resp.response != EC_RES_SUCCESS) { printk(BIOS_DEBUG, "%s: Received bad result code %d\n", __func__, (int)resp.response); return 1; } if (resp.length > cec_command->cmd_size_out) { printk(BIOS_ERR, "%s: Received len %#02x too large\n", __func__, (int)resp.length); cec_command->cmd_code = EC_RES_INVALID_RESPONSE; return 1; } if (!ec_verify_checksum(&resp)) { cec_command->cmd_code = EC_RES_INVALID_CHECKSUM; return 1; } cec_command->cmd_size_out = resp.length; memcpy(cec_command->cmd_data_out, resp.data, resp.length); return 0; }