blob: 35b4b7ee887b3137bedd608c3d3a0a874eaf957a [file] [log] [blame]
huang lind5fb66e2014-08-26 18:22:08 +08001/*
2 * This file is part of the coreboot project.
3 *
4 * Copyright 2014 Rockchip Inc.
5 *
6 * This program is free software; you can redistribute it and/or modify
7 * it under the terms of the GNU General Public License as published by
8 * the Free Software Foundation; version 2 of the License.
9 *
10 * This program is distributed in the hope that it will be useful,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 * GNU General Public License for more details.
huang lind5fb66e2014-08-26 18:22:08 +080014 */
15
huang lind5fb66e2014-08-26 18:22:08 +080016#include <arch/io.h>
Julius Werner7a453eb2014-10-20 13:14:55 -070017#include <console/console.h>
Julius Wernereaa9c452014-09-24 15:40:49 -070018#include <gpio.h>
Julius Werner7a453eb2014-10-20 13:14:55 -070019#include <soc/grf.h>
20#include <soc/pmu.h>
Julius Wernereaa9c452014-09-24 15:40:49 -070021#include <soc/soc.h>
huang lind5fb66e2014-08-26 18:22:08 +080022#include <stdlib.h>
huang lind5fb66e2014-08-26 18:22:08 +080023
24struct rk3288_gpio_regs *gpio_port[] = {
25 (struct rk3288_gpio_regs *)0xff750000,
26 (struct rk3288_gpio_regs *)0xff780000,
27 (struct rk3288_gpio_regs *)0xff790000,
28 (struct rk3288_gpio_regs *)0xff7a0000,
29 (struct rk3288_gpio_regs *)0xff7b0000,
30 (struct rk3288_gpio_regs *)0xff7c0000,
31 (struct rk3288_gpio_regs *)0xff7d0000,
32 (struct rk3288_gpio_regs *)0xff7e0000,
33 (struct rk3288_gpio_regs *)0xff7f0000
34};
35
36enum {
37 PULLNONE = 0,
38 PULLUP,
39 PULLDOWN
40};
41
42#define PMU_GPIO_PORT 0
43
44static void __gpio_input(gpio_t gpio, u32 pull)
45{
46 clrbits_le32(&gpio_port[gpio.port]->swporta_ddr, 1 << gpio.num);
47 if (gpio.port == PMU_GPIO_PORT)
48 clrsetbits_le32(&rk3288_pmu->gpio0pull[gpio.bank],
49 3 << (gpio.idx * 2), pull << (gpio.idx * 2));
50 else
Julius Werner2f37bd62015-02-19 14:51:15 -080051 write32(&rk3288_grf->gpio1_p[(gpio.port - 1)][gpio.bank],
Julius Werner94184762015-02-19 20:19:23 -080052 RK_CLRSETBITS(3 << (gpio.idx * 2),
53 pull << (gpio.idx * 2)));
huang lind5fb66e2014-08-26 18:22:08 +080054}
55
56void gpio_input(gpio_t gpio)
57{
58 __gpio_input(gpio, PULLNONE);
59}
60
61void gpio_input_pulldown(gpio_t gpio)
62{
63 __gpio_input(gpio, PULLDOWN);
64}
65
66void gpio_input_pullup(gpio_t gpio)
67{
68 __gpio_input(gpio, PULLUP);
69}
70
Julius Wernereaa9c452014-09-24 15:40:49 -070071int gpio_get(gpio_t gpio)
huang lind5fb66e2014-08-26 18:22:08 +080072{
Julius Werner2f37bd62015-02-19 14:51:15 -080073 return (read32(&gpio_port[gpio.port]->ext_porta) >> gpio.num) & 0x1;
huang lind5fb66e2014-08-26 18:22:08 +080074}
75
76void gpio_output(gpio_t gpio, int value)
77{
78 setbits_le32(&gpio_port[gpio.port]->swporta_ddr, 1 << gpio.num);
79 clrsetbits_le32(&gpio_port[gpio.port]->swporta_dr, 1 << gpio.num,
80 !!value << gpio.num);
81}