brcm63xx: fix gpio register usage
[openwrt.git] / target / linux / brcm63xx / patches-3.14 / 375-MIPS-BCM63XX-switch-to-new-gpio-driver.patch
1 From cc99dca188bb63ba390008e2f7fa62d0300233e0 Mon Sep 17 00:00:00 2001
2 From: Jonas Gorski <jogo@openwrt.org>
3 Date: Fri, 20 Feb 2015 23:58:54 +0100
4 Subject: [PATCH 2/6] MIPS: BCM63XX: switch to new gpio driver
5
6 Signed-off-by: Jonas Gorski <jogo@openwrt.org>
7 ---
8  arch/mips/bcm63xx/boards/board_common.c |    2 +
9  arch/mips/bcm63xx/gpio.c                |  147 +++++++------------------------
10  arch/mips/bcm63xx/prom.c                |    3 -
11  3 files changed, 33 insertions(+), 119 deletions(-)
12
13 --- a/arch/mips/bcm63xx/boards/board_common.c
14 +++ b/arch/mips/bcm63xx/boards/board_common.c
15 @@ -204,6 +204,8 @@ int __init board_register_devices(void)
16         }
17  #endif
18  
19 +       bcm63xx_gpio_init();
20 +
21         if (board.has_uart0)
22                 bcm63xx_uart_register(0);
23  
24 --- a/arch/mips/bcm63xx/gpio.c
25 +++ b/arch/mips/bcm63xx/gpio.c
26 @@ -5,147 +5,62 @@
27   *
28   * Copyright (C) 2008 Maxime Bizon <mbizon@freebox.fr>
29   * Copyright (C) 2008-2011 Florian Fainelli <florian@openwrt.org>
30 + * Copyright (C) Jonas Gorski <jogo@openwrt.org>
31   */
32  
33  #include <linux/kernel.h>
34 -#include <linux/module.h>
35 -#include <linux/spinlock.h>
36  #include <linux/platform_device.h>
37 +#include <linux/basic_mmio_gpio.h>
38  #include <linux/gpio.h>
39  
40  #include <bcm63xx_cpu.h>
41  #include <bcm63xx_gpio.h>
42 -#include <bcm63xx_io.h>
43  #include <bcm63xx_regs.h>
44  
45 -static u32 gpio_out_low_reg;
46 -
47 -static void bcm63xx_gpio_out_low_reg_init(void)
48 +static void __init bcm63xx_gpio_init_one(int id, int dir, int data, int ngpio)
49  {
50 -       switch (bcm63xx_get_cpu_id()) {
51 -       case BCM6345_CPU_ID:
52 -               gpio_out_low_reg = GPIO_DATA_LO_REG_6345;
53 -               break;
54 -       default:
55 -               gpio_out_low_reg = GPIO_DATA_LO_REG;
56 -               break;
57 -       }
58 -}
59 -
60 -static DEFINE_SPINLOCK(bcm63xx_gpio_lock);
61 -static u32 gpio_out_low, gpio_out_high;
62 +       struct resource res[2];
63 +       struct bgpio_pdata pdata;
64  
65 -static void bcm63xx_gpio_set(struct gpio_chip *chip,
66 -                            unsigned gpio, int val)
67 -{
68 -       u32 reg;
69 -       u32 mask;
70 -       u32 *v;
71 -       unsigned long flags;
72 -
73 -       if (gpio >= chip->ngpio)
74 -               BUG();
75 -
76 -       if (gpio < 32) {
77 -               reg = gpio_out_low_reg;
78 -               mask = 1 << gpio;
79 -               v = &gpio_out_low;
80 -       } else {
81 -               reg = GPIO_DATA_HI_REG;
82 -               mask = 1 << (gpio - 32);
83 -               v = &gpio_out_high;
84 -       }
85 -
86 -       spin_lock_irqsave(&bcm63xx_gpio_lock, flags);
87 -       if (val)
88 -               *v |= mask;
89 -       else
90 -               *v &= ~mask;
91 -       bcm_gpio_writel(*v, reg);
92 -       spin_unlock_irqrestore(&bcm63xx_gpio_lock, flags);
93 -}
94 +       memset(res, 0, sizeof(res));
95 +       memset(&pdata, 0, sizeof(pdata));
96  
97 -static int bcm63xx_gpio_get(struct gpio_chip *chip, unsigned gpio)
98 -{
99 -       u32 reg;
100 -       u32 mask;
101 +       res[0].flags = IORESOURCE_MEM;
102 +       res[0].start = bcm63xx_regset_address(RSET_GPIO);
103 +       res[0].start += dir;
104  
105 -       if (gpio >= chip->ngpio)
106 -               BUG();
107 +       res[0].end = res[0].start + 3;
108  
109 -       if (gpio < 32) {
110 -               reg = gpio_out_low_reg;
111 -               mask = 1 << gpio;
112 -       } else {
113 -               reg = GPIO_DATA_HI_REG;
114 -               mask = 1 << (gpio - 32);
115 -       }
116 +       res[1].flags = IORESOURCE_MEM;
117 +       res[1].start = bcm63xx_regset_address(RSET_GPIO);
118 +       res[1].start += data;
119  
120 -       return !!(bcm_gpio_readl(reg) & mask);
121 -}
122 +       res[1].end = res[1].start + 3;
123  
124 -static int bcm63xx_gpio_set_direction(struct gpio_chip *chip,
125 -                                     unsigned gpio, int dir)
126 -{
127 -       u32 reg;
128 -       u32 mask;
129 -       u32 tmp;
130 -       unsigned long flags;
131 -
132 -       if (gpio >= chip->ngpio)
133 -               BUG();
134 -
135 -       if (gpio < 32) {
136 -               reg = GPIO_CTL_LO_REG;
137 -               mask = 1 << gpio;
138 -       } else {
139 -               reg = GPIO_CTL_HI_REG;
140 -               mask = 1 << (gpio - 32);
141 -       }
142 -
143 -       spin_lock_irqsave(&bcm63xx_gpio_lock, flags);
144 -       tmp = bcm_gpio_readl(reg);
145 -       if (dir == BCM63XX_GPIO_DIR_IN)
146 -               tmp &= ~mask;
147 -       else
148 -               tmp |= mask;
149 -       bcm_gpio_writel(tmp, reg);
150 -       spin_unlock_irqrestore(&bcm63xx_gpio_lock, flags);
151 +       pdata.base = id * 32;
152 +       pdata.ngpio = ngpio;
153  
154 -       return 0;
155 +       platform_device_register_resndata(NULL, "bcm63xx-gpio", id, res, 2,
156 +                                         &pdata, sizeof(pdata));
157  }
158  
159 -static int bcm63xx_gpio_direction_input(struct gpio_chip *chip, unsigned gpio)
160 +int __init bcm63xx_gpio_init(void)
161  {
162 -       return bcm63xx_gpio_set_direction(chip, gpio, BCM63XX_GPIO_DIR_IN);
163 -}
164 +       int ngpio = bcm63xx_gpio_count();
165 +       int data_low_reg;
166  
167 -static int bcm63xx_gpio_direction_output(struct gpio_chip *chip,
168 -                                        unsigned gpio, int value)
169 -{
170 -       bcm63xx_gpio_set(chip, gpio, value);
171 -       return bcm63xx_gpio_set_direction(chip, gpio, BCM63XX_GPIO_DIR_OUT);
172 -}
173 +       if (BCMCPU_IS_6345())
174 +               data_low_reg = GPIO_DATA_LO_REG_6345;
175 +       else
176 +               data_low_reg = GPIO_DATA_LO_REG;
177  
178 +       bcm63xx_gpio_init_one(0, GPIO_CTL_LO_REG, data_low_reg, min(ngpio, 32));
179  
180 -static struct gpio_chip bcm63xx_gpio_chip = {
181 -       .label                  = "bcm63xx-gpio",
182 -       .direction_input        = bcm63xx_gpio_direction_input,
183 -       .direction_output       = bcm63xx_gpio_direction_output,
184 -       .get                    = bcm63xx_gpio_get,
185 -       .set                    = bcm63xx_gpio_set,
186 -       .base                   = 0,
187 -};
188 +       if (ngpio <= 32)
189 +               return 0;
190  
191 -int __init bcm63xx_gpio_init(void)
192 -{
193 -       bcm63xx_gpio_out_low_reg_init();
194 +       bcm63xx_gpio_init_one(1, GPIO_CTL_HI_REG, GPIO_DATA_HI_REG, ngpio - 32);
195  
196 -       gpio_out_low = bcm_gpio_readl(gpio_out_low_reg);
197 -       if (!BCMCPU_IS_6345())
198 -               gpio_out_high = bcm_gpio_readl(GPIO_DATA_HI_REG);
199 -       bcm63xx_gpio_chip.ngpio = bcm63xx_gpio_count();
200 -       pr_info("registering %d GPIOs\n", bcm63xx_gpio_chip.ngpio);
201 +       return 0;
202  
203 -       return gpiochip_add(&bcm63xx_gpio_chip);
204  }
205 --- a/arch/mips/bcm63xx/prom.c
206 +++ b/arch/mips/bcm63xx/prom.c
207 @@ -54,9 +54,6 @@ void __init prom_init(void)
208         reg &= ~mask;
209         bcm_perf_writel(reg, PERF_CKCTL_REG);
210  
211 -       /* register gpiochip */
212 -       bcm63xx_gpio_init();
213 -
214         /* detect and setup flash access */
215         bcm63xx_flash_detect();
216