xref: /rk3399_rockchip-uboot/board/LaCie/common/cpld-gpio-bus.c (revision 326ea986ac150acdc7656d57fca647db80b50158)
1d5cc3f52SSimon Guinot /*
2d5cc3f52SSimon Guinot  * cpld-gpio-bus.c: provides support for the CPLD GPIO bus found on some LaCie
3d5cc3f52SSimon Guinot  * boards (as the 2Big/5Big Network v2 and the 2Big NAS). This parallel GPIO
4d5cc3f52SSimon Guinot  * bus exposes two registers (address and data). Each of this register is made
5d5cc3f52SSimon Guinot  * up of several dedicated GPIOs. An extra GPIO is used to notify the CPLD that
6d5cc3f52SSimon Guinot  * the registers have been updated.
7d5cc3f52SSimon Guinot  *
8d5cc3f52SSimon Guinot  * Mostly this bus is used to configure the LEDs on LaCie boards.
9d5cc3f52SSimon Guinot  *
10d5cc3f52SSimon Guinot  * Copyright (C) 2013 Simon Guinot <simon.guinot@sequanux.org>
11d5cc3f52SSimon Guinot  *
12*1a459660SWolfgang Denk  * SPDX-License-Identifier:	GPL-2.0+
13d5cc3f52SSimon Guinot  */
14d5cc3f52SSimon Guinot 
15d5cc3f52SSimon Guinot #include <asm/arch/gpio.h>
16d5cc3f52SSimon Guinot #include "cpld-gpio-bus.h"
17d5cc3f52SSimon Guinot 
cpld_gpio_bus_set_addr(struct cpld_gpio_bus * bus,unsigned addr)18d5cc3f52SSimon Guinot static void cpld_gpio_bus_set_addr(struct cpld_gpio_bus *bus, unsigned addr)
19d5cc3f52SSimon Guinot {
20d5cc3f52SSimon Guinot 	int pin;
21d5cc3f52SSimon Guinot 
22d5cc3f52SSimon Guinot 	for (pin = 0; pin < bus->num_addr; pin++)
23d5cc3f52SSimon Guinot 		kw_gpio_set_value(bus->addr[pin], (addr >> pin) & 1);
24d5cc3f52SSimon Guinot }
25d5cc3f52SSimon Guinot 
cpld_gpio_bus_set_data(struct cpld_gpio_bus * bus,unsigned data)26d5cc3f52SSimon Guinot static void cpld_gpio_bus_set_data(struct cpld_gpio_bus *bus, unsigned data)
27d5cc3f52SSimon Guinot {
28d5cc3f52SSimon Guinot 	int pin;
29d5cc3f52SSimon Guinot 
30d5cc3f52SSimon Guinot 	for (pin = 0; pin < bus->num_data; pin++)
31d5cc3f52SSimon Guinot 		kw_gpio_set_value(bus->data[pin], (data >> pin) & 1);
32d5cc3f52SSimon Guinot }
33d5cc3f52SSimon Guinot 
cpld_gpio_bus_enable_select(struct cpld_gpio_bus * bus)34d5cc3f52SSimon Guinot static void cpld_gpio_bus_enable_select(struct cpld_gpio_bus *bus)
35d5cc3f52SSimon Guinot {
36d5cc3f52SSimon Guinot 	/* The transfer is enabled on the raising edge. */
37d5cc3f52SSimon Guinot 	kw_gpio_set_value(bus->enable, 0);
38d5cc3f52SSimon Guinot 	kw_gpio_set_value(bus->enable, 1);
39d5cc3f52SSimon Guinot }
40d5cc3f52SSimon Guinot 
cpld_gpio_bus_write(struct cpld_gpio_bus * bus,unsigned addr,unsigned value)41d5cc3f52SSimon Guinot void cpld_gpio_bus_write(struct cpld_gpio_bus *bus,
42d5cc3f52SSimon Guinot 			 unsigned addr, unsigned value)
43d5cc3f52SSimon Guinot {
44d5cc3f52SSimon Guinot 	cpld_gpio_bus_set_addr(bus, addr);
45d5cc3f52SSimon Guinot 	cpld_gpio_bus_set_data(bus, value);
46d5cc3f52SSimon Guinot 	cpld_gpio_bus_enable_select(bus);
47d5cc3f52SSimon Guinot }
48