switch_stm32.c 4.93 KB
Newer Older
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172
/*
 * Copyright (C) 2017 Emcraft Systems
 * Sergei Miroshnichenko <sergeimir@emcraft.com>
 *
 * SPDX-License-Identifier:	GPL-2.0+
 */

#include <common.h>
#include <fdt.h>
#include <libfdt.h>
#include <fdt_support.h>
#include <asm/arch/stm32f2_gpio.h>

struct gpio_banks {
	const char *name;
	enum stm32f2_gpio_port port;
};

static struct gpio_banks gpio_banks[] = {
	{ "GPIOA", STM32F2_GPIO_PORT_A },
	{ "GPIOB", STM32F2_GPIO_PORT_B },
	{ "GPIOC", STM32F2_GPIO_PORT_C },
	{ "GPIOD", STM32F2_GPIO_PORT_D },
	{ "GPIOE", STM32F2_GPIO_PORT_E },
	{ "GPIOF", STM32F2_GPIO_PORT_F },
	{ "GPIOG", STM32F2_GPIO_PORT_G },
	{ "GPIOH", STM32F2_GPIO_PORT_H },
	{ "GPIOI", STM32F2_GPIO_PORT_I },
	{ "GPIOJ", STM32F2_GPIO_PORT_J },
	{ "GPIOK", STM32F2_GPIO_PORT_K },
};

void fdt_fixup_switch(void *fdt)
{
	int switch_node = fdt_node_offset_by_compatible(fdt, -1, "gpio-configuration-switch");

	if (switch_node != -FDT_ERR_NOTFOUND) {
		int pinctrl_node, pins_node, offset, nextoffset;
		const u32 *pinctrl_h;
		uint32_t tag;

		pinctrl_h = fdt_getprop(fdt, switch_node, "pinctrl-0", NULL);
		if (!pinctrl_h) {
			error("%s: no pinctrl defined for the switch node\n", __func__);
			return;
		}

		pinctrl_node = fdt_node_offset_by_phandle(fdt, fdt32_to_cpu(*pinctrl_h));
		/*
		 * pins_node is the "st,pins" node
		 */
		pins_node = fdt_next_node(fdt, pinctrl_node, NULL);

		/*
		 * Set up pinctrl for the signal gpios: pullups, etc.
		 */
		offset = pins_node;
		while ((tag = fdt_next_tag(fdt, offset, &nextoffset)) != FDT_END_NODE) {
			if (tag == FDT_PROP) {
				const int pin_size = 6;
				const struct fdt_property *fdt_prop = fdt_offset_ptr(fdt, offset,
										     sizeof(*fdt_prop));
				int len = fdt32_to_cpu(fdt_prop->len);
				u32 *v = (u32*)fdt_prop->data;
				int gpio_bank_off;
				const char *bank_name;
				int i, port = -1;
				struct stm32f2_gpio_dsc iomux;
				int err;
				/*
				 * Example of data format:
				 * status_pullup = <&gpioe 5 IN PULL_UP PUSH_PULL LOW_SPEED>;
				 * v[0] - gpio bank
				 * v[1] - gpio pin
				 * v[2] - direction
				 * v[3] - NO_PULL/PULL_UP/PULL_DOWN
				 * v[4] - PUSH_PULL/OPEN_DRAIN
				 * v[5] - speed
				 */
				int pin = fdt32_to_cpu(v[1]);
				int direction_in = !fdt32_to_cpu(v[2]);
				int pullup = fdt32_to_cpu(v[3]) == 1;

				if (len % (pin_size * sizeof(u32))) {
					printf("%s: invalid pinctrl value\n", __func__);
				}
				gpio_bank_off = fdt_node_offset_by_phandle(fdt, fdt32_to_cpu(v[0]));
				bank_name = (const char *)fdt_getprop(fdt, gpio_bank_off, "st,bank-name", NULL);
				for (i = 0; i < sizeof(gpio_banks)/sizeof(gpio_banks[0]); ++i) {
					if (0 == strcmp(bank_name, gpio_banks[i].name)) {
						port = gpio_banks[i].port;
						break;
					}
				}

				if (port < 0) {
					error("%s: unsupported bank name: %s\n", __func__, bank_name);
					return;
				}

				if (!direction_in) {
					error("%s: only input switch gpios are supported\n", __func__);
					return;
				}

				iomux.port = port;
				iomux.pin = pin;
				err = stm32f2_gpio_config(&iomux, pullup ? STM32F2_GPIO_ROLE_GPIN_PULLUP : STM32F2_GPIO_ROLE_GPIN);
				if (err) {
					error("%s: gpio configuration failed: %d\n", __func__, err);
					return;
				}
			}
			offset = nextoffset;
		}

		/*
		 * Delay for the gpios to establish
		 */
		udelay(200);

		/*
		 * Find every node with the "configuration-switch-gpios" property set and change its status accordingly
		 */
		for (offset = fdt_next_node(fdt, -1, NULL);
		     offset >= 0;
		     offset = fdt_next_node(fdt, offset, NULL)) {
			int len;
			struct fdt_property *prop = fdt_get_property_w(fdt, offset, "configuration-switch-gpios", &len);
			if (prop) {
				const int pin_size = 3;
				int len = fdt32_to_cpu(prop->len);
				u32 *v = (u32*)prop->data;
				int port = -1;
				int pin = fdt32_to_cpu(v[1]);
				int active_low = !!fdt32_to_cpu(v[2]);
				int gpio_value;
				struct stm32f2_gpio_dsc iomux;
				int i;
				int gpio_bank_off = fdt_node_offset_by_phandle(fdt, fdt32_to_cpu(v[0]));
				const char *bank_name = (const char *)fdt_getprop(fdt, gpio_bank_off, "st,bank-name", NULL);
				const char *status;
				for (i = 0; i < sizeof(gpio_banks)/sizeof(gpio_banks[0]); ++i) {
					if (0 == strcmp(bank_name, gpio_banks[i].name)) {
						port = gpio_banks[i].port;
						break;
					}
				}

				if (port < 0) {
					error("%s: unsupported bank name: %s\n", __func__, bank_name);
					return;
				}

				if (len % (pin_size * sizeof(u32))) {
					printf("%s: invalid pinctrl value\n", __func__);
				}

				iomux.port = port;
				iomux.pin = pin;
				gpio_value = stm32f2_gpout_get(&iomux);
				status = ((gpio_value && !active_low) || (!gpio_value && active_low)) ? "okay" : "disabled";
				printf("%s: Setting node %s to \"%s\" as instructed by gpio %d.%d (value %d, active %s)\n",
				       __func__, fdt_get_name(fdt, offset, NULL), status,
				       port, pin, gpio_value, active_low ? "low" : "high");
				fdt_setprop(fdt, offset, "status", status, strlen(status) + 1);
			}
		}
	} else {
		debug("%s: no switch node\n", __func__);
	}
}