u-boot/board/nm/netbird_v2/shield_gnss_can.c

223 lines
5.7 KiB
C

#define DEBUG
#include <common.h>
#include <asm/gpio.h>
#include <asm/arch/mux.h>
#include "shield.h"
#include "board.h"
#define NETBIRD_GPIO_RST_SHIELD_N GPIO_TO_PIN(0, 27)
#define NETBIRD_GPIO_LATCH GPIO_TO_PIN(0, 7)
#define NETBIRD_GPIO_MODE_1 GPIO_TO_PIN(1, 10)
static int shield_slot_initialized = 0;
static struct module_pin_mux can_shield_netbird_pin_mux_config[] = {
/* Leave UART0 unconfigured because we want to configure it as needed by linux (can/spi/uart/etc) */
{OFFSET(uart0_ctsn), (MODE(7) | PULLUDEN | PULLUP_EN)}, /* CAN1 tx */
//{OFFSET(uart0_rxd), (MODE(7) | PULLUDEN | PULLUP_EN)}, /* CAN0 tx */
{OFFSET(ecap0_in_pwm0_out), (MODE(7) | PULLUDEN | PULLUP_EN)}, /* Latch EN */
{-1},
};
static struct module_pin_mux can_shield_netbird_pin_mux_final[] = {
/* Leave UART0 unconfigured because we want to configure it as needed by linux (can/spi/uart/etc) */
{OFFSET(uart0_ctsn), (MODE(2) | PULLUDEN | PULLUP_EN)}, /* E18 CAN1 tx */
{OFFSET(uart0_rtsn), (MODE(2) | PULLUDDIS | RXACTIVE)}, /* E17 CAN1 rx */
{OFFSET(uart0_rxd), (MODE(7) | PULLUDDIS)}, /* (E15) UART0_RXD */
{OFFSET(uart0_txd), (MODE(7) | PULLUDEN | PULLUP_EN)}, /* (E16) UART0_TXD */
//{OFFSET(uart0_txd), (MODE(2) | PULLUDDIS | RXACTIVE)}, /* CAN0 rx */
//{OFFSET(uart0_rxd), (MODE(2) | PULLUDEN | PULLUP_EN)}, /* CAN0 tx */
{-1},
};
static struct module_pin_mux gnss_shield_netbird_pin_mux_uart[] = {
/* Leave UART0 unconfigured because we want to configure it as needed by linux (can/spi/uart/etc) */
{OFFSET(uart0_rxd), (MODE(7) | PULLUDDIS)}, /* (E15) UART0_RXD */
{OFFSET(uart0_txd), (MODE(7) | PULLUDEN | PULLUP_EN)}, /* (E16) UART0_TXD */
{-1},
};
static struct module_pin_mux gnss_shield_netbird_pin_mux_i2c[] = {
{OFFSET(uart0_rxd), (MODE(3) | PULLUDDIS)}, /* (E15) UART0_RXD - I2C2_SDA */
{OFFSET(uart0_txd), (MODE(3) | PULLUDEN | PULLUP_EN)}, /* (E16) UART0_TXD - I2C_SCL */
{-1},
};
static int request_gpios(void)
{
int ret;
debug("Shield configure gpios\n");
ret = shield_gpio_request_as_input(NETBIRD_GPIO_RST_SHIELD_N, "shield-rst");
if ((ret < 0))
return -1;
ret = shield_gpio_request_as_input(NETBIRD_GPIO_LATCH, "shield-load");
if ((ret < 0))
return -1;
/*
ret = shield_gpio_request_as_input(NETBIRD_GPIO_MODE_0, "shield-mode0");
if ((ret < 0))
return -1;
*/
ret = shield_gpio_request_as_input(NETBIRD_GPIO_MODE_1, "shield-mode1");
if ((ret < 0))
return -1;
shield_slot_initialized = 1;
return 0;
}
static int configure_can_termination(int termination)
{
int ret;
debug ("Set can termination to %d\n", termination);
if (!shield_slot_initialized) {
if (request_gpios()) {
puts("Failed to request gpios\n");
return -1;
}
}
debug("Configure shield pin muxing for configuration\n");
configure_module_pin_mux(can_shield_netbird_pin_mux_config);
debug("Make sure shield module is in reset\n");
ret = gpio_direction_output(NETBIRD_GPIO_RST_SHIELD_N, 0);
if (ret < 0) {
puts("Can not set shield-rst as output\n");
return -1;
}
udelay(10);
debug("Set latch to high\n");
ret = gpio_direction_output(NETBIRD_GPIO_LATCH, 1);
if (ret < 0) {
puts("Can not set shield-load as output\n");
return -1;
}
udelay(10);
debug("Write termination to GPIOs\n");
/*
ret = gpio_direction_output(NETBIRD_GPIO_MODE_0, mode & 0x01);
if (ret < 0) {
puts("Can not set shield-mode0 as output\n");
return -1;
}
*/
ret = gpio_direction_output(NETBIRD_GPIO_MODE_1, termination);
if (ret < 0) {
puts("Can not set shield-mode1 as output\n"); ///////////////// 1
return -1;
}
udelay(10);
debug("Set latch to low\n");
gpio_set_value(NETBIRD_GPIO_LATCH, 0);
udelay(10);
debug("Set mode0 and mode1 to highz again\n");
/* ret = gpio_direction_input(NETBIRD_GPIO_MODE_0);
if ((ret < 0)) {
puts("Could not configure shield slot mode0 gpio as input\n");
return -1;
}
*/
ret = gpio_direction_input(NETBIRD_GPIO_MODE_1);
if ((ret < 0)) {
puts("Could not configure shield slot mode1 gpio as input\n");
return -1;
}
udelay(10);
debug("Take shield out of reset\n");
gpio_set_value(NETBIRD_GPIO_RST_SHIELD_N, 1);
udelay(10);
debug("Set final can shield muxing\n");
configure_module_pin_mux(can_shield_netbird_pin_mux_final);
return 0;
}
static int get_termination(const char* termination)
{
if (strcmp("on", termination) == 0) {
return 1;
}
else if (strcmp("off", termination) == 0) {
return 0;
}
debug ("Invalid termination mode %s (falling back to off)", termination);
return -1;
}
static int get_termination_from_args(char * const argv[], int argc)
{
int termination;
if(argc != 2){
debug("2 arguments are needed for can configuration\n");
return -1;
}
if (strcmp ("termination", argv[0])) {
debug("The only option for can is terminations\n");
return -1;
}
termination = get_termination(argv[1]);
if (termination < 0) {
return -1;
}
/* Termination is inverse */
return !termination;
}
// shield gnss-can gnss-mode uart can termination off
static int set_shieldmode(char * const argv[], int argc)
{
int ret = 0;
if (argc != 5) {
printf("Too few arguments for gnss-can\n");
return -1;
}
if (strcmp ("gnss-mode", argv[0])) {
printf("gnss can module want a gnss-mode\n");
return -1;
}
ret = 0;//configure_gnss_mode(argv[1]);
if(ret != 0) return ret;
if (strcmp ("can", argv[2])) {
printf("gnss can module wants a gnss-mode\n");
return -1;
}
printf("configure_can_termination");
ret = configure_can_termination(get_termination_from_args(&argv[3], 2));
if(ret != 0) return ret;
return 0;
}
struct shield_t gnss_can_shield = {
"gnss-can", set_shieldmode
};
void gnss_can_shield_init(void)
{
shield_register(&gnss_can_shield);
}