2018-09-07 16:00:13 +01:00
|
|
|
/*
|
2019-04-08 03:00:49 +01:00
|
|
|
* Copyright (c) 2018-2019 Atmosphère-NX
|
2018-09-07 16:00:13 +01:00
|
|
|
*
|
|
|
|
* This program is free software; you can redistribute it and/or modify it
|
|
|
|
* under the terms and conditions of the GNU General Public License,
|
|
|
|
* version 2, as published by the Free Software Foundation.
|
|
|
|
*
|
|
|
|
* This program is distributed in the hope 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, see <http://www.gnu.org/licenses/>.
|
|
|
|
*/
|
|
|
|
|
2018-02-23 12:13:18 +00:00
|
|
|
#include "i2c.h"
|
|
|
|
#include "timer.h"
|
|
|
|
|
2018-02-23 20:07:02 +00:00
|
|
|
/* Prototypes for internal commands. */
|
|
|
|
void i2c_load_config(void);
|
|
|
|
int i2c_write(unsigned int device, uint32_t val, unsigned int num_bytes);
|
|
|
|
int i2c_send_byte_command(unsigned int device, unsigned char reg, unsigned char b);
|
|
|
|
|
|
|
|
/* Load hardware config for I2C4. */
|
2018-02-23 12:13:18 +00:00
|
|
|
void i2c_load_config(void) {
|
|
|
|
/* Set MSTR_CONFIG_LOAD, TIMEOUT_CONFIG_LOAD, undocumented bit. */
|
|
|
|
I2C_I2C_CONFIG_LOAD_0 = 0x25;
|
2018-02-23 12:56:23 +00:00
|
|
|
|
2018-02-23 12:13:18 +00:00
|
|
|
/* Wait a bit for master config to be loaded. */
|
|
|
|
for (unsigned int i = 0; i < 20; i++) {
|
|
|
|
timer_wait(1);
|
|
|
|
if (!(I2C_I2C_CONFIG_LOAD_0 & 1)) {
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-02-23 20:07:02 +00:00
|
|
|
/* Initialize I2C4. */
|
2018-02-23 12:13:18 +00:00
|
|
|
void i2c_init(void) {
|
|
|
|
/* Setup divisor, and clear the bus. */
|
|
|
|
I2C_I2C_CLK_DIVISOR_REGISTER_0 = 0x50001;
|
|
|
|
I2C_I2C_BUS_CLEAR_CONFIG_0 = 0x90003;
|
2018-02-23 12:56:23 +00:00
|
|
|
|
2018-02-23 12:13:18 +00:00
|
|
|
/* Load hardware configuration. */
|
|
|
|
i2c_load_config();
|
2018-02-23 12:56:23 +00:00
|
|
|
|
2018-02-23 12:13:18 +00:00
|
|
|
/* Wait a while until BUS_CLEAR_DONE is set. */
|
|
|
|
for (unsigned int i = 0; i < 10; i++) {
|
|
|
|
timer_wait(20000);
|
|
|
|
if (I2C_INTERRUPT_STATUS_REGISTER_0 & 0x800) {
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
2018-02-23 12:56:23 +00:00
|
|
|
|
2018-02-23 12:13:18 +00:00
|
|
|
/* Read the BUS_CLEAR_STATUS. Result doesn't matter. */
|
2018-02-23 12:56:23 +00:00
|
|
|
I2C_I2C_BUS_CLEAR_STATUS_0;
|
|
|
|
|
2018-02-23 12:13:18 +00:00
|
|
|
/* Read and set the Interrupt Status. */
|
|
|
|
uint32_t int_status = I2C_INTERRUPT_STATUS_REGISTER_0;
|
|
|
|
I2C_INTERRUPT_STATUS_REGISTER_0 = int_status;
|
|
|
|
}
|
|
|
|
|
2018-02-23 20:07:02 +00:00
|
|
|
/* Writes a value to an i2c device. */
|
2018-02-23 12:13:18 +00:00
|
|
|
int i2c_write(unsigned int device, uint32_t val, unsigned int num_bytes) {
|
|
|
|
if (num_bytes > 4) {
|
|
|
|
return 0;
|
|
|
|
}
|
2018-02-23 12:56:23 +00:00
|
|
|
|
2018-02-23 12:13:18 +00:00
|
|
|
/* Set device for 7-bit mode. */
|
|
|
|
I2C_I2C_CMD_ADDR0_0 = device << 1;
|
2018-02-23 12:56:23 +00:00
|
|
|
|
2018-02-23 12:13:18 +00:00
|
|
|
/* Load in data to write. */
|
|
|
|
I2C_I2C_CMD_DATA1_0 = val;
|
2018-02-23 12:56:23 +00:00
|
|
|
|
2018-02-23 12:13:18 +00:00
|
|
|
/* Set config with LENGTH = num_bytes, NEW_MASTER_FSM, DEBOUNCE_CNT = 4T. */
|
|
|
|
I2C_I2C_CNFG_0 = ((num_bytes << 1) - 2) | 0x2800;
|
2018-02-23 12:56:23 +00:00
|
|
|
|
2018-02-23 12:13:18 +00:00
|
|
|
i2c_load_config();
|
2018-02-23 12:56:23 +00:00
|
|
|
|
2018-02-23 12:13:18 +00:00
|
|
|
/* Config |= SEND; */
|
|
|
|
I2C_I2C_CNFG_0 |= 0x200;
|
2018-02-23 12:56:23 +00:00
|
|
|
|
|
|
|
|
2018-02-23 12:13:18 +00:00
|
|
|
while (I2C_I2C_STATUS_0 & 0x100) {
|
|
|
|
/* Wait until not busy. */
|
|
|
|
}
|
2018-02-23 12:56:23 +00:00
|
|
|
|
2018-02-23 12:13:18 +00:00
|
|
|
/* Return CMD1_STAT == SL1_XFER_SUCCESSFUL. */
|
2018-02-24 06:03:13 +00:00
|
|
|
return (I2C_I2C_STATUS_0 & 0xF) == 0;
|
2018-02-23 12:13:18 +00:00
|
|
|
}
|
|
|
|
|
2018-02-23 20:07:02 +00:00
|
|
|
/* Writes a byte val to reg for given device. */
|
2018-02-23 12:13:18 +00:00
|
|
|
int i2c_send_byte_command(unsigned int device, unsigned char reg, unsigned char b) {
|
|
|
|
uint32_t val = (reg) | (b << 8);
|
|
|
|
/* Write 1 byte (reg) + 1 byte (value) */
|
|
|
|
return i2c_write(device, val, 2);
|
|
|
|
}
|
|
|
|
|
2018-02-23 20:07:02 +00:00
|
|
|
/* Actually reset device 27. This might turn off the screen? */
|
2018-02-23 12:13:18 +00:00
|
|
|
int i2c_send_reset_cmd(void) {
|
|
|
|
/* Write 00 to Device 27 Reg 00. */
|
|
|
|
return i2c_send_byte_command(27, 0, 0);
|
2018-02-23 12:56:23 +00:00
|
|
|
}
|