gpio-aspeed.c (acfeb6defcb9310b1ff44db1e633798ba766337d) gpio-aspeed.c (55cb93fd243bad2c6e15f9151a32f575d2f5371f)
1// SPDX-License-Identifier: GPL-2.0-or-later
2/*
3 * Copyright 2015 IBM Corp.
4 *
5 * Joel Stanley <joel@jms.id.au>
6 */
7
8#include <linux/clk.h>

--- 16 unchanged lines hidden (view full) ---

25 * These two headers aren't meant to be used by GPIO drivers. We need
26 * them in order to access gpio_chip_hwgpio() which we need to implement
27 * the aspeed specific API which allows the coprocessor to request
28 * access to some GPIOs and to arbitrate between coprocessor and ARM.
29 */
30#include <linux/gpio/consumer.h>
31#include "gpiolib.h"
32
1// SPDX-License-Identifier: GPL-2.0-or-later
2/*
3 * Copyright 2015 IBM Corp.
4 *
5 * Joel Stanley <joel@jms.id.au>
6 */
7
8#include <linux/clk.h>

--- 16 unchanged lines hidden (view full) ---

25 * These two headers aren't meant to be used by GPIO drivers. We need
26 * them in order to access gpio_chip_hwgpio() which we need to implement
27 * the aspeed specific API which allows the coprocessor to request
28 * access to some GPIOs and to arbitrate between coprocessor and ARM.
29 */
30#include <linux/gpio/consumer.h>
31#include "gpiolib.h"
32
33/* Non-constant mask variant of FIELD_GET() and FIELD_PREP() */
34#define field_get(_mask, _reg) (((_reg) & (_mask)) >> (ffs(_mask) - 1))
35#define field_prep(_mask, _val) (((_val) << (ffs(_mask) - 1)) & (_mask))
36
37#define GPIO_G7_IRQ_STS_BASE 0x100
38#define GPIO_G7_IRQ_STS_OFFSET(x) (GPIO_G7_IRQ_STS_BASE + (x) * 0x4)
39#define GPIO_G7_CTRL_REG_BASE 0x180
40#define GPIO_G7_CTRL_REG_OFFSET(x) (GPIO_G7_CTRL_REG_BASE + (x) * 0x4)
41#define GPIO_G7_CTRL_OUT_DATA BIT(0)
42#define GPIO_G7_CTRL_DIR BIT(1)
43#define GPIO_G7_CTRL_IRQ_EN BIT(2)
44#define GPIO_G7_CTRL_IRQ_TYPE0 BIT(3)
45#define GPIO_G7_CTRL_IRQ_TYPE1 BIT(4)
46#define GPIO_G7_CTRL_IRQ_TYPE2 BIT(5)
47#define GPIO_G7_CTRL_RST_TOLERANCE BIT(6)
48#define GPIO_G7_CTRL_DEBOUNCE_SEL1 BIT(7)
49#define GPIO_G7_CTRL_DEBOUNCE_SEL2 BIT(8)
50#define GPIO_G7_CTRL_INPUT_MASK BIT(9)
51#define GPIO_G7_CTRL_IRQ_STS BIT(12)
52#define GPIO_G7_CTRL_IN_DATA BIT(13)
53
33struct aspeed_bank_props {
34 unsigned int bank;
35 u32 input;
36 u32 output;
37};
38
39struct aspeed_gpio_config {
40 unsigned int nr_gpios;
41 const struct aspeed_bank_props *props;
54struct aspeed_bank_props {
55 unsigned int bank;
56 u32 input;
57 u32 output;
58};
59
60struct aspeed_gpio_config {
61 unsigned int nr_gpios;
62 const struct aspeed_bank_props *props;
63 const struct aspeed_gpio_llops *llops;
64 const int *debounce_timers_array;
65 int debounce_timers_num;
66 bool require_dcache;
42};
43
44/*
45 * @offset_timer: Maps an offset to an @timer_users index, or zero if disabled
46 * @timer_users: Tracks the number of users for each timer
47 *
48 * The @timer_users has four elements but the first element is unused. This is
49 * to simplify accounting and indexing, as a zero value in @offset_timer

--- 22 unchanged lines hidden (view full) ---

72 uint16_t val_regs; /* +0: Rd: read input value, Wr: set write latch
73 * +4: Rd/Wr: Direction (0=in, 1=out)
74 */
75 uint16_t rdata_reg; /* Rd: read write latch, Wr: <none> */
76 uint16_t irq_regs;
77 uint16_t debounce_regs;
78 uint16_t tolerance_regs;
79 uint16_t cmdsrc_regs;
67};
68
69/*
70 * @offset_timer: Maps an offset to an @timer_users index, or zero if disabled
71 * @timer_users: Tracks the number of users for each timer
72 *
73 * The @timer_users has four elements but the first element is unused. This is
74 * to simplify accounting and indexing, as a zero value in @offset_timer

--- 22 unchanged lines hidden (view full) ---

97 uint16_t val_regs; /* +0: Rd: read input value, Wr: set write latch
98 * +4: Rd/Wr: Direction (0=in, 1=out)
99 */
100 uint16_t rdata_reg; /* Rd: read write latch, Wr: <none> */
101 uint16_t irq_regs;
102 uint16_t debounce_regs;
103 uint16_t tolerance_regs;
104 uint16_t cmdsrc_regs;
80 const char names[4][3];
81};
82
83/*
84 * Note: The "value" register returns the input value sampled on the
85 * line even when the GPIO is configured as an output. Since
86 * that input goes through synchronizers, writing, then reading
87 * back may not return the written value right away.
88 *
89 * The "rdata" register returns the content of the write latch
90 * and thus can be used to read back what was last written
91 * reliably.
92 */
93
94static const int debounce_timers[4] = { 0x00, 0x50, 0x54, 0x58 };
105};
106
107/*
108 * Note: The "value" register returns the input value sampled on the
109 * line even when the GPIO is configured as an output. Since
110 * that input goes through synchronizers, writing, then reading
111 * back may not return the written value right away.
112 *
113 * The "rdata" register returns the content of the write latch
114 * and thus can be used to read back what was last written
115 * reliably.
116 */
117
118static const int debounce_timers[4] = { 0x00, 0x50, 0x54, 0x58 };
119static const int g7_debounce_timers[4] = { 0x00, 0x00, 0x04, 0x08 };
95
120
121/*
122 * The debounce timers array is used to configure the debounce timer settings.Here’s how it works:
123 * Array Value: Indicates the offset for configuring the debounce timer.
124 * Array Index: Corresponds to the debounce setting register.
125 * The debounce timers array follows this pattern for configuring the debounce setting registers:
126 * Array Index 0: No debounce timer is set;
127 * Array Value is irrelevant (don’t care).
128 * Array Index 1: Debounce setting #2 is set to 1, and debounce setting #1 is set to 0.
129 * Array Value: offset for configuring debounce timer 0 (g4: 0x50, g7: 0x00)
130 * Array Index 2: Debounce setting #2 is set to 0, and debounce setting #1 is set to 1.
131 * Array Value: offset for configuring debounce timer 1 (g4: 0x54, g7: 0x04)
132 * Array Index 3: Debounce setting #2 is set to 1, and debounce setting #1 is set to 1.
133 * Array Value: offset for configuring debounce timer 2 (g4: 0x58, g7: 0x8)
134 */
135
96static const struct aspeed_gpio_copro_ops *copro_ops;
97static void *copro_data;
98
99static const struct aspeed_gpio_bank aspeed_gpio_banks[] = {
100 {
101 .val_regs = 0x0000,
102 .rdata_reg = 0x00c0,
103 .irq_regs = 0x0008,
104 .debounce_regs = 0x0040,
105 .tolerance_regs = 0x001c,
106 .cmdsrc_regs = 0x0060,
136static const struct aspeed_gpio_copro_ops *copro_ops;
137static void *copro_data;
138
139static const struct aspeed_gpio_bank aspeed_gpio_banks[] = {
140 {
141 .val_regs = 0x0000,
142 .rdata_reg = 0x00c0,
143 .irq_regs = 0x0008,
144 .debounce_regs = 0x0040,
145 .tolerance_regs = 0x001c,
146 .cmdsrc_regs = 0x0060,
107 .names = { "A", "B", "C", "D" },
108 },
109 {
110 .val_regs = 0x0020,
111 .rdata_reg = 0x00c4,
112 .irq_regs = 0x0028,
113 .debounce_regs = 0x0048,
114 .tolerance_regs = 0x003c,
115 .cmdsrc_regs = 0x0068,
147 },
148 {
149 .val_regs = 0x0020,
150 .rdata_reg = 0x00c4,
151 .irq_regs = 0x0028,
152 .debounce_regs = 0x0048,
153 .tolerance_regs = 0x003c,
154 .cmdsrc_regs = 0x0068,
116 .names = { "E", "F", "G", "H" },
117 },
118 {
119 .val_regs = 0x0070,
120 .rdata_reg = 0x00c8,
121 .irq_regs = 0x0098,
122 .debounce_regs = 0x00b0,
123 .tolerance_regs = 0x00ac,
124 .cmdsrc_regs = 0x0090,
155 },
156 {
157 .val_regs = 0x0070,
158 .rdata_reg = 0x00c8,
159 .irq_regs = 0x0098,
160 .debounce_regs = 0x00b0,
161 .tolerance_regs = 0x00ac,
162 .cmdsrc_regs = 0x0090,
125 .names = { "I", "J", "K", "L" },
126 },
127 {
128 .val_regs = 0x0078,
129 .rdata_reg = 0x00cc,
130 .irq_regs = 0x00e8,
131 .debounce_regs = 0x0100,
132 .tolerance_regs = 0x00fc,
133 .cmdsrc_regs = 0x00e0,
163 },
164 {
165 .val_regs = 0x0078,
166 .rdata_reg = 0x00cc,
167 .irq_regs = 0x00e8,
168 .debounce_regs = 0x0100,
169 .tolerance_regs = 0x00fc,
170 .cmdsrc_regs = 0x00e0,
134 .names = { "M", "N", "O", "P" },
135 },
136 {
137 .val_regs = 0x0080,
138 .rdata_reg = 0x00d0,
139 .irq_regs = 0x0118,
140 .debounce_regs = 0x0130,
141 .tolerance_regs = 0x012c,
142 .cmdsrc_regs = 0x0110,
171 },
172 {
173 .val_regs = 0x0080,
174 .rdata_reg = 0x00d0,
175 .irq_regs = 0x0118,
176 .debounce_regs = 0x0130,
177 .tolerance_regs = 0x012c,
178 .cmdsrc_regs = 0x0110,
143 .names = { "Q", "R", "S", "T" },
144 },
145 {
146 .val_regs = 0x0088,
147 .rdata_reg = 0x00d4,
148 .irq_regs = 0x0148,
149 .debounce_regs = 0x0160,
150 .tolerance_regs = 0x015c,
151 .cmdsrc_regs = 0x0140,
179 },
180 {
181 .val_regs = 0x0088,
182 .rdata_reg = 0x00d4,
183 .irq_regs = 0x0148,
184 .debounce_regs = 0x0160,
185 .tolerance_regs = 0x015c,
186 .cmdsrc_regs = 0x0140,
152 .names = { "U", "V", "W", "X" },
153 },
154 {
155 .val_regs = 0x01E0,
156 .rdata_reg = 0x00d8,
157 .irq_regs = 0x0178,
158 .debounce_regs = 0x0190,
159 .tolerance_regs = 0x018c,
160 .cmdsrc_regs = 0x0170,
187 },
188 {
189 .val_regs = 0x01E0,
190 .rdata_reg = 0x00d8,
191 .irq_regs = 0x0178,
192 .debounce_regs = 0x0190,
193 .tolerance_regs = 0x018c,
194 .cmdsrc_regs = 0x0170,
161 .names = { "Y", "Z", "AA", "AB" },
162 },
163 {
164 .val_regs = 0x01e8,
165 .rdata_reg = 0x00dc,
166 .irq_regs = 0x01a8,
167 .debounce_regs = 0x01c0,
168 .tolerance_regs = 0x01bc,
169 .cmdsrc_regs = 0x01a0,
195 },
196 {
197 .val_regs = 0x01e8,
198 .rdata_reg = 0x00dc,
199 .irq_regs = 0x01a8,
200 .debounce_regs = 0x01c0,
201 .tolerance_regs = 0x01bc,
202 .cmdsrc_regs = 0x01a0,
170 .names = { "AC", "", "", "" },
171 },
172};
173
174enum aspeed_gpio_reg {
175 reg_val,
176 reg_rdata,
177 reg_dir,
178 reg_irq_enable,
179 reg_irq_type0,
180 reg_irq_type1,
181 reg_irq_type2,
182 reg_irq_status,
183 reg_debounce_sel1,
184 reg_debounce_sel2,
185 reg_tolerance,
186 reg_cmdsrc0,
187 reg_cmdsrc1,
188};
189
203 },
204};
205
206enum aspeed_gpio_reg {
207 reg_val,
208 reg_rdata,
209 reg_dir,
210 reg_irq_enable,
211 reg_irq_type0,
212 reg_irq_type1,
213 reg_irq_type2,
214 reg_irq_status,
215 reg_debounce_sel1,
216 reg_debounce_sel2,
217 reg_tolerance,
218 reg_cmdsrc0,
219 reg_cmdsrc1,
220};
221
222struct aspeed_gpio_llops {
223 void (*reg_bit_set)(struct aspeed_gpio *gpio, unsigned int offset,
224 const enum aspeed_gpio_reg reg, bool val);
225 bool (*reg_bit_get)(struct aspeed_gpio *gpio, unsigned int offset,
226 const enum aspeed_gpio_reg reg);
227 int (*reg_bank_get)(struct aspeed_gpio *gpio, unsigned int offset,
228 const enum aspeed_gpio_reg reg);
229 void (*privilege_ctrl)(struct aspeed_gpio *gpio, unsigned int offset, int owner);
230 void (*privilege_init)(struct aspeed_gpio *gpio);
231 bool (*copro_request)(struct aspeed_gpio *gpio, unsigned int offset);
232 void (*copro_release)(struct aspeed_gpio *gpio, unsigned int offset);
233};
234
190#define GPIO_VAL_VALUE 0x00
191#define GPIO_VAL_DIR 0x04
192
193#define GPIO_IRQ_ENABLE 0x00
194#define GPIO_IRQ_TYPE0 0x04
195#define GPIO_IRQ_TYPE1 0x08
196#define GPIO_IRQ_TYPE2 0x0c
197#define GPIO_IRQ_STATUS 0x10

--- 4 unchanged lines hidden (view full) ---

202#define GPIO_CMDSRC_0 0x00
203#define GPIO_CMDSRC_1 0x04
204#define GPIO_CMDSRC_ARM 0
205#define GPIO_CMDSRC_LPC 1
206#define GPIO_CMDSRC_COLDFIRE 2
207#define GPIO_CMDSRC_RESERVED 3
208
209/* This will be resolved at compile time */
235#define GPIO_VAL_VALUE 0x00
236#define GPIO_VAL_DIR 0x04
237
238#define GPIO_IRQ_ENABLE 0x00
239#define GPIO_IRQ_TYPE0 0x04
240#define GPIO_IRQ_TYPE1 0x08
241#define GPIO_IRQ_TYPE2 0x0c
242#define GPIO_IRQ_STATUS 0x10

--- 4 unchanged lines hidden (view full) ---

247#define GPIO_CMDSRC_0 0x00
248#define GPIO_CMDSRC_1 0x04
249#define GPIO_CMDSRC_ARM 0
250#define GPIO_CMDSRC_LPC 1
251#define GPIO_CMDSRC_COLDFIRE 2
252#define GPIO_CMDSRC_RESERVED 3
253
254/* This will be resolved at compile time */
210static inline void __iomem *bank_reg(struct aspeed_gpio *gpio,
211 const struct aspeed_gpio_bank *bank,
212 const enum aspeed_gpio_reg reg)
255static void __iomem *aspeed_gpio_g4_bank_reg(struct aspeed_gpio *gpio,
256 const struct aspeed_gpio_bank *bank,
257 const enum aspeed_gpio_reg reg)
213{
214 switch (reg) {
215 case reg_val:
216 return gpio->base + bank->val_regs + GPIO_VAL_VALUE;
217 case reg_rdata:
218 return gpio->base + bank->rdata_reg;
219 case reg_dir:
220 return gpio->base + bank->val_regs + GPIO_VAL_DIR;

--- 16 unchanged lines hidden (view full) ---

237 case reg_cmdsrc0:
238 return gpio->base + bank->cmdsrc_regs + GPIO_CMDSRC_0;
239 case reg_cmdsrc1:
240 return gpio->base + bank->cmdsrc_regs + GPIO_CMDSRC_1;
241 }
242 BUG();
243}
244
258{
259 switch (reg) {
260 case reg_val:
261 return gpio->base + bank->val_regs + GPIO_VAL_VALUE;
262 case reg_rdata:
263 return gpio->base + bank->rdata_reg;
264 case reg_dir:
265 return gpio->base + bank->val_regs + GPIO_VAL_DIR;

--- 16 unchanged lines hidden (view full) ---

282 case reg_cmdsrc0:
283 return gpio->base + bank->cmdsrc_regs + GPIO_CMDSRC_0;
284 case reg_cmdsrc1:
285 return gpio->base + bank->cmdsrc_regs + GPIO_CMDSRC_1;
286 }
287 BUG();
288}
289
290static u32 aspeed_gpio_g7_reg_mask(const enum aspeed_gpio_reg reg)
291{
292 switch (reg) {
293 case reg_val:
294 return GPIO_G7_CTRL_OUT_DATA;
295 case reg_dir:
296 return GPIO_G7_CTRL_DIR;
297 case reg_irq_enable:
298 return GPIO_G7_CTRL_IRQ_EN;
299 case reg_irq_type0:
300 return GPIO_G7_CTRL_IRQ_TYPE0;
301 case reg_irq_type1:
302 return GPIO_G7_CTRL_IRQ_TYPE1;
303 case reg_irq_type2:
304 return GPIO_G7_CTRL_IRQ_TYPE2;
305 case reg_tolerance:
306 return GPIO_G7_CTRL_RST_TOLERANCE;
307 case reg_debounce_sel1:
308 return GPIO_G7_CTRL_DEBOUNCE_SEL1;
309 case reg_debounce_sel2:
310 return GPIO_G7_CTRL_DEBOUNCE_SEL2;
311 case reg_rdata:
312 return GPIO_G7_CTRL_OUT_DATA;
313 case reg_irq_status:
314 return GPIO_G7_CTRL_IRQ_STS;
315 case reg_cmdsrc0:
316 case reg_cmdsrc1:
317 default:
318 WARN_ON_ONCE(1);
319 return 0;
320 }
321}
322
245#define GPIO_BANK(x) ((x) >> 5)
246#define GPIO_OFFSET(x) ((x) & 0x1f)
247#define GPIO_BIT(x) BIT(GPIO_OFFSET(x))
248
323#define GPIO_BANK(x) ((x) >> 5)
324#define GPIO_OFFSET(x) ((x) & 0x1f)
325#define GPIO_BIT(x) BIT(GPIO_OFFSET(x))
326
249#define _GPIO_SET_DEBOUNCE(t, o, i) ((!!((t) & BIT(i))) << GPIO_OFFSET(o))
250#define GPIO_SET_DEBOUNCE1(t, o) _GPIO_SET_DEBOUNCE(t, o, 1)
251#define GPIO_SET_DEBOUNCE2(t, o) _GPIO_SET_DEBOUNCE(t, o, 0)
252
253static const struct aspeed_gpio_bank *to_bank(unsigned int offset)
254{
255 unsigned int bank = GPIO_BANK(offset);
256
257 WARN_ON(bank >= ARRAY_SIZE(aspeed_gpio_banks));
258 return &aspeed_gpio_banks[bank];
259}
260

--- 14 unchanged lines hidden (view full) ---

275 }
276
277 return NULL;
278}
279
280static inline bool have_gpio(struct aspeed_gpio *gpio, unsigned int offset)
281{
282 const struct aspeed_bank_props *props = find_bank_props(gpio, offset);
327static const struct aspeed_gpio_bank *to_bank(unsigned int offset)
328{
329 unsigned int bank = GPIO_BANK(offset);
330
331 WARN_ON(bank >= ARRAY_SIZE(aspeed_gpio_banks));
332 return &aspeed_gpio_banks[bank];
333}
334

--- 14 unchanged lines hidden (view full) ---

349 }
350
351 return NULL;
352}
353
354static inline bool have_gpio(struct aspeed_gpio *gpio, unsigned int offset)
355{
356 const struct aspeed_bank_props *props = find_bank_props(gpio, offset);
283 const struct aspeed_gpio_bank *bank = to_bank(offset);
284 unsigned int group = GPIO_OFFSET(offset) / 8;
285
357
286 return bank->names[group][0] != '\0' &&
287 (!props || ((props->input | props->output) & GPIO_BIT(offset)));
358 if (offset >= gpio->chip.ngpio)
359 return false;
360
361 return (!props || ((props->input | props->output) & GPIO_BIT(offset)));
288}
289
290static inline bool have_input(struct aspeed_gpio *gpio, unsigned int offset)
291{
292 const struct aspeed_bank_props *props = find_bank_props(gpio, offset);
293
294 return !props || (props->input & GPIO_BIT(offset));
295}
296
297#define have_irq(g, o) have_input((g), (o))
298#define have_debounce(g, o) have_input((g), (o))
299
300static inline bool have_output(struct aspeed_gpio *gpio, unsigned int offset)
301{
302 const struct aspeed_bank_props *props = find_bank_props(gpio, offset);
303
304 return !props || (props->output & GPIO_BIT(offset));
305}
306
362}
363
364static inline bool have_input(struct aspeed_gpio *gpio, unsigned int offset)
365{
366 const struct aspeed_bank_props *props = find_bank_props(gpio, offset);
367
368 return !props || (props->input & GPIO_BIT(offset));
369}
370
371#define have_irq(g, o) have_input((g), (o))
372#define have_debounce(g, o) have_input((g), (o))
373
374static inline bool have_output(struct aspeed_gpio *gpio, unsigned int offset)
375{
376 const struct aspeed_bank_props *props = find_bank_props(gpio, offset);
377
378 return !props || (props->output & GPIO_BIT(offset));
379}
380
307static void aspeed_gpio_change_cmd_source(struct aspeed_gpio *gpio,
308 const struct aspeed_gpio_bank *bank,
309 int bindex, int cmdsrc)
381static void aspeed_gpio_change_cmd_source(struct aspeed_gpio *gpio, unsigned int offset, int cmdsrc)
310{
382{
311 void __iomem *c0 = bank_reg(gpio, bank, reg_cmdsrc0);
312 void __iomem *c1 = bank_reg(gpio, bank, reg_cmdsrc1);
313 u32 bit, reg;
314
315 /*
316 * Each register controls 4 banks, so take the bottom 2
317 * bits of the bank index, and use them to select the
318 * right control bit (0, 8, 16 or 24).
319 */
320 bit = BIT((bindex & 3) << 3);
321
322 /* Source 1 first to avoid illegal 11 combination */
323 reg = ioread32(c1);
324 if (cmdsrc & 2)
325 reg |= bit;
326 else
327 reg &= ~bit;
328 iowrite32(reg, c1);
329
330 /* Then Source 0 */
331 reg = ioread32(c0);
332 if (cmdsrc & 1)
333 reg |= bit;
334 else
335 reg &= ~bit;
336 iowrite32(reg, c0);
383 if (gpio->config->llops->privilege_ctrl)
384 gpio->config->llops->privilege_ctrl(gpio, offset, cmdsrc);
337}
338
339static bool aspeed_gpio_copro_request(struct aspeed_gpio *gpio,
340 unsigned int offset)
341{
385}
386
387static bool aspeed_gpio_copro_request(struct aspeed_gpio *gpio,
388 unsigned int offset)
389{
342 const struct aspeed_gpio_bank *bank = to_bank(offset);
390 if (gpio->config->llops->copro_request)
391 return gpio->config->llops->copro_request(gpio, offset);
343
392
344 if (!copro_ops || !gpio->cf_copro_bankmap)
345 return false;
346 if (!gpio->cf_copro_bankmap[offset >> 3])
347 return false;
348 if (!copro_ops->request_access)
349 return false;
350
351 /* Pause the coprocessor */
352 copro_ops->request_access(copro_data);
353
354 /* Change command source back to ARM */
355 aspeed_gpio_change_cmd_source(gpio, bank, offset >> 3, GPIO_CMDSRC_ARM);
356
357 /* Update cache */
358 gpio->dcache[GPIO_BANK(offset)] = ioread32(bank_reg(gpio, bank, reg_rdata));
359
360 return true;
393 return false;
361}
362
363static void aspeed_gpio_copro_release(struct aspeed_gpio *gpio,
364 unsigned int offset)
365{
394}
395
396static void aspeed_gpio_copro_release(struct aspeed_gpio *gpio,
397 unsigned int offset)
398{
366 const struct aspeed_gpio_bank *bank = to_bank(offset);
399 if (gpio->config->llops->copro_release)
400 gpio->config->llops->copro_release(gpio, offset);
401}
367
402
368 if (!copro_ops || !gpio->cf_copro_bankmap)
369 return;
370 if (!gpio->cf_copro_bankmap[offset >> 3])
371 return;
372 if (!copro_ops->release_access)
373 return;
374
375 /* Change command source back to ColdFire */
376 aspeed_gpio_change_cmd_source(gpio, bank, offset >> 3,
377 GPIO_CMDSRC_COLDFIRE);
378
379 /* Restart the coprocessor */
380 copro_ops->release_access(copro_data);
403static bool aspeed_gpio_support_copro(struct aspeed_gpio *gpio)
404{
405 return gpio->config->llops->copro_request && gpio->config->llops->copro_release &&
406 gpio->config->llops->privilege_ctrl && gpio->config->llops->privilege_init;
381}
382
383static int aspeed_gpio_get(struct gpio_chip *gc, unsigned int offset)
384{
385 struct aspeed_gpio *gpio = gpiochip_get_data(gc);
407}
408
409static int aspeed_gpio_get(struct gpio_chip *gc, unsigned int offset)
410{
411 struct aspeed_gpio *gpio = gpiochip_get_data(gc);
386 const struct aspeed_gpio_bank *bank = to_bank(offset);
387
412
388 return !!(ioread32(bank_reg(gpio, bank, reg_val)) & GPIO_BIT(offset));
413 return gpio->config->llops->reg_bit_get(gpio, offset, reg_val);
389}
390
391static void __aspeed_gpio_set(struct gpio_chip *gc, unsigned int offset,
392 int val)
393{
394 struct aspeed_gpio *gpio = gpiochip_get_data(gc);
414}
415
416static void __aspeed_gpio_set(struct gpio_chip *gc, unsigned int offset,
417 int val)
418{
419 struct aspeed_gpio *gpio = gpiochip_get_data(gc);
395 const struct aspeed_gpio_bank *bank = to_bank(offset);
396 void __iomem *addr;
397 u32 reg;
398
420
399 addr = bank_reg(gpio, bank, reg_val);
400 reg = gpio->dcache[GPIO_BANK(offset)];
401
402 if (val)
403 reg |= GPIO_BIT(offset);
404 else
405 reg &= ~GPIO_BIT(offset);
406 gpio->dcache[GPIO_BANK(offset)] = reg;
407
408 iowrite32(reg, addr);
421 gpio->config->llops->reg_bit_set(gpio, offset, reg_val, val);
409 /* Flush write */
422 /* Flush write */
410 ioread32(addr);
423 gpio->config->llops->reg_bit_get(gpio, offset, reg_val);
411}
412
413static void aspeed_gpio_set(struct gpio_chip *gc, unsigned int offset,
414 int val)
415{
416 struct aspeed_gpio *gpio = gpiochip_get_data(gc);
417 unsigned long flags;
424}
425
426static void aspeed_gpio_set(struct gpio_chip *gc, unsigned int offset,
427 int val)
428{
429 struct aspeed_gpio *gpio = gpiochip_get_data(gc);
430 unsigned long flags;
418 bool copro;
431 bool copro = false;
419
420 raw_spin_lock_irqsave(&gpio->lock, flags);
421 copro = aspeed_gpio_copro_request(gpio, offset);
422
423 __aspeed_gpio_set(gc, offset, val);
424
425 if (copro)
426 aspeed_gpio_copro_release(gpio, offset);
427 raw_spin_unlock_irqrestore(&gpio->lock, flags);
428}
429
430static int aspeed_gpio_dir_in(struct gpio_chip *gc, unsigned int offset)
431{
432 struct aspeed_gpio *gpio = gpiochip_get_data(gc);
432
433 raw_spin_lock_irqsave(&gpio->lock, flags);
434 copro = aspeed_gpio_copro_request(gpio, offset);
435
436 __aspeed_gpio_set(gc, offset, val);
437
438 if (copro)
439 aspeed_gpio_copro_release(gpio, offset);
440 raw_spin_unlock_irqrestore(&gpio->lock, flags);
441}
442
443static int aspeed_gpio_dir_in(struct gpio_chip *gc, unsigned int offset)
444{
445 struct aspeed_gpio *gpio = gpiochip_get_data(gc);
433 const struct aspeed_gpio_bank *bank = to_bank(offset);
434 void __iomem *addr = bank_reg(gpio, bank, reg_dir);
435 unsigned long flags;
446 unsigned long flags;
436 bool copro;
437 u32 reg;
447 bool copro = false;
438
439 if (!have_input(gpio, offset))
440 return -ENOTSUPP;
441
442 raw_spin_lock_irqsave(&gpio->lock, flags);
443
448
449 if (!have_input(gpio, offset))
450 return -ENOTSUPP;
451
452 raw_spin_lock_irqsave(&gpio->lock, flags);
453
444 reg = ioread32(addr);
445 reg &= ~GPIO_BIT(offset);
446
447 copro = aspeed_gpio_copro_request(gpio, offset);
454 copro = aspeed_gpio_copro_request(gpio, offset);
448 iowrite32(reg, addr);
455 gpio->config->llops->reg_bit_set(gpio, offset, reg_dir, 0);
449 if (copro)
450 aspeed_gpio_copro_release(gpio, offset);
451
452 raw_spin_unlock_irqrestore(&gpio->lock, flags);
453
454 return 0;
455}
456
457static int aspeed_gpio_dir_out(struct gpio_chip *gc,
458 unsigned int offset, int val)
459{
460 struct aspeed_gpio *gpio = gpiochip_get_data(gc);
456 if (copro)
457 aspeed_gpio_copro_release(gpio, offset);
458
459 raw_spin_unlock_irqrestore(&gpio->lock, flags);
460
461 return 0;
462}
463
464static int aspeed_gpio_dir_out(struct gpio_chip *gc,
465 unsigned int offset, int val)
466{
467 struct aspeed_gpio *gpio = gpiochip_get_data(gc);
461 const struct aspeed_gpio_bank *bank = to_bank(offset);
462 void __iomem *addr = bank_reg(gpio, bank, reg_dir);
463 unsigned long flags;
468 unsigned long flags;
464 bool copro;
465 u32 reg;
469 bool copro = false;
466
467 if (!have_output(gpio, offset))
468 return -ENOTSUPP;
469
470 raw_spin_lock_irqsave(&gpio->lock, flags);
471
470
471 if (!have_output(gpio, offset))
472 return -ENOTSUPP;
473
474 raw_spin_lock_irqsave(&gpio->lock, flags);
475
472 reg = ioread32(addr);
473 reg |= GPIO_BIT(offset);
474
475 copro = aspeed_gpio_copro_request(gpio, offset);
476 __aspeed_gpio_set(gc, offset, val);
476 copro = aspeed_gpio_copro_request(gpio, offset);
477 __aspeed_gpio_set(gc, offset, val);
477 iowrite32(reg, addr);
478 gpio->config->llops->reg_bit_set(gpio, offset, reg_dir, 1);
478
479 if (copro)
480 aspeed_gpio_copro_release(gpio, offset);
481 raw_spin_unlock_irqrestore(&gpio->lock, flags);
482
483 return 0;
484}
485
486static int aspeed_gpio_get_direction(struct gpio_chip *gc, unsigned int offset)
487{
488 struct aspeed_gpio *gpio = gpiochip_get_data(gc);
479
480 if (copro)
481 aspeed_gpio_copro_release(gpio, offset);
482 raw_spin_unlock_irqrestore(&gpio->lock, flags);
483
484 return 0;
485}
486
487static int aspeed_gpio_get_direction(struct gpio_chip *gc, unsigned int offset)
488{
489 struct aspeed_gpio *gpio = gpiochip_get_data(gc);
489 const struct aspeed_gpio_bank *bank = to_bank(offset);
490 unsigned long flags;
491 u32 val;
492
493 if (!have_input(gpio, offset))
494 return GPIO_LINE_DIRECTION_OUT;
495
496 if (!have_output(gpio, offset))
497 return GPIO_LINE_DIRECTION_IN;
498
499 raw_spin_lock_irqsave(&gpio->lock, flags);
500
490 unsigned long flags;
491 u32 val;
492
493 if (!have_input(gpio, offset))
494 return GPIO_LINE_DIRECTION_OUT;
495
496 if (!have_output(gpio, offset))
497 return GPIO_LINE_DIRECTION_IN;
498
499 raw_spin_lock_irqsave(&gpio->lock, flags);
500
501 val = ioread32(bank_reg(gpio, bank, reg_dir)) & GPIO_BIT(offset);
501 val = gpio->config->llops->reg_bit_get(gpio, offset, reg_dir);
502
503 raw_spin_unlock_irqrestore(&gpio->lock, flags);
504
505 return val ? GPIO_LINE_DIRECTION_OUT : GPIO_LINE_DIRECTION_IN;
506}
507
508static inline int irqd_to_aspeed_gpio_data(struct irq_data *d,
509 struct aspeed_gpio **gpio,
502
503 raw_spin_unlock_irqrestore(&gpio->lock, flags);
504
505 return val ? GPIO_LINE_DIRECTION_OUT : GPIO_LINE_DIRECTION_IN;
506}
507
508static inline int irqd_to_aspeed_gpio_data(struct irq_data *d,
509 struct aspeed_gpio **gpio,
510 const struct aspeed_gpio_bank **bank,
511 u32 *bit, int *offset)
510 int *offset)
512{
513 struct aspeed_gpio *internal;
514
515 *offset = irqd_to_hwirq(d);
516
517 internal = irq_data_get_irq_chip_data(d);
518
519 /* This might be a bit of a questionable place to check */
520 if (!have_irq(internal, *offset))
521 return -ENOTSUPP;
522
523 *gpio = internal;
511{
512 struct aspeed_gpio *internal;
513
514 *offset = irqd_to_hwirq(d);
515
516 internal = irq_data_get_irq_chip_data(d);
517
518 /* This might be a bit of a questionable place to check */
519 if (!have_irq(internal, *offset))
520 return -ENOTSUPP;
521
522 *gpio = internal;
524 *bank = to_bank(*offset);
525 *bit = GPIO_BIT(*offset);
526
527 return 0;
528}
529
530static void aspeed_gpio_irq_ack(struct irq_data *d)
531{
523
524 return 0;
525}
526
527static void aspeed_gpio_irq_ack(struct irq_data *d)
528{
532 const struct aspeed_gpio_bank *bank;
533 struct aspeed_gpio *gpio;
534 unsigned long flags;
529 struct aspeed_gpio *gpio;
530 unsigned long flags;
535 void __iomem *status_addr;
536 int rc, offset;
531 int rc, offset;
537 bool copro;
538 u32 bit;
532 bool copro = false;
539
533
540 rc = irqd_to_aspeed_gpio_data(d, &gpio, &bank, &bit, &offset);
534 rc = irqd_to_aspeed_gpio_data(d, &gpio, &offset);
541 if (rc)
542 return;
543
535 if (rc)
536 return;
537
544 status_addr = bank_reg(gpio, bank, reg_irq_status);
545
546 raw_spin_lock_irqsave(&gpio->lock, flags);
547 copro = aspeed_gpio_copro_request(gpio, offset);
548
538 raw_spin_lock_irqsave(&gpio->lock, flags);
539 copro = aspeed_gpio_copro_request(gpio, offset);
540
549 iowrite32(bit, status_addr);
541 gpio->config->llops->reg_bit_set(gpio, offset, reg_irq_status, 1);
550
551 if (copro)
552 aspeed_gpio_copro_release(gpio, offset);
553 raw_spin_unlock_irqrestore(&gpio->lock, flags);
554}
555
556static void aspeed_gpio_irq_set_mask(struct irq_data *d, bool set)
557{
542
543 if (copro)
544 aspeed_gpio_copro_release(gpio, offset);
545 raw_spin_unlock_irqrestore(&gpio->lock, flags);
546}
547
548static void aspeed_gpio_irq_set_mask(struct irq_data *d, bool set)
549{
558 const struct aspeed_gpio_bank *bank;
559 struct aspeed_gpio *gpio;
560 unsigned long flags;
550 struct aspeed_gpio *gpio;
551 unsigned long flags;
561 u32 reg, bit;
562 void __iomem *addr;
563 int rc, offset;
552 int rc, offset;
564 bool copro;
553 bool copro = false;
565
554
566 rc = irqd_to_aspeed_gpio_data(d, &gpio, &bank, &bit, &offset);
555 rc = irqd_to_aspeed_gpio_data(d, &gpio, &offset);
567 if (rc)
568 return;
569
556 if (rc)
557 return;
558
570 addr = bank_reg(gpio, bank, reg_irq_enable);
571
572 /* Unmasking the IRQ */
573 if (set)
574 gpiochip_enable_irq(&gpio->chip, irqd_to_hwirq(d));
575
576 raw_spin_lock_irqsave(&gpio->lock, flags);
577 copro = aspeed_gpio_copro_request(gpio, offset);
578
559 /* Unmasking the IRQ */
560 if (set)
561 gpiochip_enable_irq(&gpio->chip, irqd_to_hwirq(d));
562
563 raw_spin_lock_irqsave(&gpio->lock, flags);
564 copro = aspeed_gpio_copro_request(gpio, offset);
565
579 reg = ioread32(addr);
580 if (set)
581 reg |= bit;
582 else
583 reg &= ~bit;
584 iowrite32(reg, addr);
566 gpio->config->llops->reg_bit_set(gpio, offset, reg_irq_enable, set);
585
586 if (copro)
587 aspeed_gpio_copro_release(gpio, offset);
588 raw_spin_unlock_irqrestore(&gpio->lock, flags);
589
590 /* Masking the IRQ */
591 if (!set)
592 gpiochip_disable_irq(&gpio->chip, irqd_to_hwirq(d));

--- 9 unchanged lines hidden (view full) ---

602 aspeed_gpio_irq_set_mask(d, true);
603}
604
605static int aspeed_gpio_set_type(struct irq_data *d, unsigned int type)
606{
607 u32 type0 = 0;
608 u32 type1 = 0;
609 u32 type2 = 0;
567
568 if (copro)
569 aspeed_gpio_copro_release(gpio, offset);
570 raw_spin_unlock_irqrestore(&gpio->lock, flags);
571
572 /* Masking the IRQ */
573 if (!set)
574 gpiochip_disable_irq(&gpio->chip, irqd_to_hwirq(d));

--- 9 unchanged lines hidden (view full) ---

584 aspeed_gpio_irq_set_mask(d, true);
585}
586
587static int aspeed_gpio_set_type(struct irq_data *d, unsigned int type)
588{
589 u32 type0 = 0;
590 u32 type1 = 0;
591 u32 type2 = 0;
610 u32 bit, reg;
611 const struct aspeed_gpio_bank *bank;
612 irq_flow_handler_t handler;
613 struct aspeed_gpio *gpio;
614 unsigned long flags;
592 irq_flow_handler_t handler;
593 struct aspeed_gpio *gpio;
594 unsigned long flags;
615 void __iomem *addr;
616 int rc, offset;
595 int rc, offset;
617 bool copro;
596 bool copro = false;
618
597
619 rc = irqd_to_aspeed_gpio_data(d, &gpio, &bank, &bit, &offset);
598 rc = irqd_to_aspeed_gpio_data(d, &gpio, &offset);
620 if (rc)
621 return -EINVAL;
622
623 switch (type & IRQ_TYPE_SENSE_MASK) {
624 case IRQ_TYPE_EDGE_BOTH:
599 if (rc)
600 return -EINVAL;
601
602 switch (type & IRQ_TYPE_SENSE_MASK) {
603 case IRQ_TYPE_EDGE_BOTH:
625 type2 |= bit;
604 type2 = 1;
626 fallthrough;
627 case IRQ_TYPE_EDGE_RISING:
605 fallthrough;
606 case IRQ_TYPE_EDGE_RISING:
628 type0 |= bit;
607 type0 = 1;
629 fallthrough;
630 case IRQ_TYPE_EDGE_FALLING:
631 handler = handle_edge_irq;
632 break;
633 case IRQ_TYPE_LEVEL_HIGH:
608 fallthrough;
609 case IRQ_TYPE_EDGE_FALLING:
610 handler = handle_edge_irq;
611 break;
612 case IRQ_TYPE_LEVEL_HIGH:
634 type0 |= bit;
613 type0 = 1;
635 fallthrough;
636 case IRQ_TYPE_LEVEL_LOW:
614 fallthrough;
615 case IRQ_TYPE_LEVEL_LOW:
637 type1 |= bit;
616 type1 = 1;
638 handler = handle_level_irq;
639 break;
640 default:
641 return -EINVAL;
642 }
643
644 raw_spin_lock_irqsave(&gpio->lock, flags);
645 copro = aspeed_gpio_copro_request(gpio, offset);
646
617 handler = handle_level_irq;
618 break;
619 default:
620 return -EINVAL;
621 }
622
623 raw_spin_lock_irqsave(&gpio->lock, flags);
624 copro = aspeed_gpio_copro_request(gpio, offset);
625
647 addr = bank_reg(gpio, bank, reg_irq_type0);
648 reg = ioread32(addr);
649 reg = (reg & ~bit) | type0;
650 iowrite32(reg, addr);
626 gpio->config->llops->reg_bit_set(gpio, offset, reg_irq_type0, type0);
627 gpio->config->llops->reg_bit_set(gpio, offset, reg_irq_type1, type1);
628 gpio->config->llops->reg_bit_set(gpio, offset, reg_irq_type2, type2);
651
629
652 addr = bank_reg(gpio, bank, reg_irq_type1);
653 reg = ioread32(addr);
654 reg = (reg & ~bit) | type1;
655 iowrite32(reg, addr);
656
657 addr = bank_reg(gpio, bank, reg_irq_type2);
658 reg = ioread32(addr);
659 reg = (reg & ~bit) | type2;
660 iowrite32(reg, addr);
661
662 if (copro)
663 aspeed_gpio_copro_release(gpio, offset);
664 raw_spin_unlock_irqrestore(&gpio->lock, flags);
665
666 irq_set_handler_locked(d, handler);
667
668 return 0;
669}
670
671static void aspeed_gpio_irq_handler(struct irq_desc *desc)
672{
673 struct gpio_chip *gc = irq_desc_get_handler_data(desc);
674 struct irq_chip *ic = irq_desc_get_chip(desc);
630 if (copro)
631 aspeed_gpio_copro_release(gpio, offset);
632 raw_spin_unlock_irqrestore(&gpio->lock, flags);
633
634 irq_set_handler_locked(d, handler);
635
636 return 0;
637}
638
639static void aspeed_gpio_irq_handler(struct irq_desc *desc)
640{
641 struct gpio_chip *gc = irq_desc_get_handler_data(desc);
642 struct irq_chip *ic = irq_desc_get_chip(desc);
675 struct aspeed_gpio *data = gpiochip_get_data(gc);
676 unsigned int i, p, banks;
677 unsigned long reg;
678 struct aspeed_gpio *gpio = gpiochip_get_data(gc);
679
680 chained_irq_enter(ic, desc);
681
682 banks = DIV_ROUND_UP(gpio->chip.ngpio, 32);
683 for (i = 0; i < banks; i++) {
643 unsigned int i, p, banks;
644 unsigned long reg;
645 struct aspeed_gpio *gpio = gpiochip_get_data(gc);
646
647 chained_irq_enter(ic, desc);
648
649 banks = DIV_ROUND_UP(gpio->chip.ngpio, 32);
650 for (i = 0; i < banks; i++) {
684 const struct aspeed_gpio_bank *bank = &aspeed_gpio_banks[i];
651 reg = gpio->config->llops->reg_bank_get(gpio, i * 32, reg_irq_status);
685
652
686 reg = ioread32(bank_reg(data, bank, reg_irq_status));
687
688 for_each_set_bit(p, &reg, 32)
689 generic_handle_domain_irq(gc->irq.domain, i * 32 + p);
690 }
691
692 chained_irq_exit(ic, desc);
693}
694
695static void aspeed_init_irq_valid_mask(struct gpio_chip *gc,

--- 21 unchanged lines hidden (view full) ---

717 }
718}
719
720static int aspeed_gpio_reset_tolerance(struct gpio_chip *chip,
721 unsigned int offset, bool enable)
722{
723 struct aspeed_gpio *gpio = gpiochip_get_data(chip);
724 unsigned long flags;
653 for_each_set_bit(p, &reg, 32)
654 generic_handle_domain_irq(gc->irq.domain, i * 32 + p);
655 }
656
657 chained_irq_exit(ic, desc);
658}
659
660static void aspeed_init_irq_valid_mask(struct gpio_chip *gc,

--- 21 unchanged lines hidden (view full) ---

682 }
683}
684
685static int aspeed_gpio_reset_tolerance(struct gpio_chip *chip,
686 unsigned int offset, bool enable)
687{
688 struct aspeed_gpio *gpio = gpiochip_get_data(chip);
689 unsigned long flags;
725 void __iomem *treg;
726 bool copro;
727 u32 val;
690 bool copro = false;
728
691
729 treg = bank_reg(gpio, to_bank(offset), reg_tolerance);
730
731 raw_spin_lock_irqsave(&gpio->lock, flags);
732 copro = aspeed_gpio_copro_request(gpio, offset);
733
692 raw_spin_lock_irqsave(&gpio->lock, flags);
693 copro = aspeed_gpio_copro_request(gpio, offset);
694
734 val = readl(treg);
695 gpio->config->llops->reg_bit_set(gpio, offset, reg_tolerance, enable);
735
696
736 if (enable)
737 val |= GPIO_BIT(offset);
738 else
739 val &= ~GPIO_BIT(offset);
740
741 writel(val, treg);
742
743 if (copro)
744 aspeed_gpio_copro_release(gpio, offset);
745 raw_spin_unlock_irqrestore(&gpio->lock, flags);
746
747 return 0;
748}
749
750static int aspeed_gpio_request(struct gpio_chip *chip, unsigned int offset)

--- 76 unchanged lines hidden (view full) ---

827{
828 return gpio->offset_timer[offset] > 0;
829}
830
831/* Call under gpio->lock */
832static void configure_timer(struct aspeed_gpio *gpio, unsigned int offset,
833 unsigned int timer)
834{
697 if (copro)
698 aspeed_gpio_copro_release(gpio, offset);
699 raw_spin_unlock_irqrestore(&gpio->lock, flags);
700
701 return 0;
702}
703
704static int aspeed_gpio_request(struct gpio_chip *chip, unsigned int offset)

--- 76 unchanged lines hidden (view full) ---

781{
782 return gpio->offset_timer[offset] > 0;
783}
784
785/* Call under gpio->lock */
786static void configure_timer(struct aspeed_gpio *gpio, unsigned int offset,
787 unsigned int timer)
788{
835 const struct aspeed_gpio_bank *bank = to_bank(offset);
836 const u32 mask = GPIO_BIT(offset);
837 void __iomem *addr;
838 u32 val;
839
840 /* Note: Debounce timer isn't under control of the command
841 * source registers, so no need to sync with the coprocessor
842 */
789 /* Note: Debounce timer isn't under control of the command
790 * source registers, so no need to sync with the coprocessor
791 */
843 addr = bank_reg(gpio, bank, reg_debounce_sel1);
844 val = ioread32(addr);
845 iowrite32((val & ~mask) | GPIO_SET_DEBOUNCE1(timer, offset), addr);
846
847 addr = bank_reg(gpio, bank, reg_debounce_sel2);
848 val = ioread32(addr);
849 iowrite32((val & ~mask) | GPIO_SET_DEBOUNCE2(timer, offset), addr);
792 gpio->config->llops->reg_bit_set(gpio, offset, reg_debounce_sel1, !!(timer & BIT(1)));
793 gpio->config->llops->reg_bit_set(gpio, offset, reg_debounce_sel2, !!(timer & BIT(0)));
850}
851
852static int enable_debounce(struct gpio_chip *chip, unsigned int offset,
853 unsigned long usecs)
854{
855 struct aspeed_gpio *gpio = gpiochip_get_data(chip);
856 u32 requested_cycles;
857 unsigned long flags;

--- 14 unchanged lines hidden (view full) ---

872
873 if (timer_allocation_registered(gpio, offset)) {
874 rc = unregister_allocated_timer(gpio, offset);
875 if (rc < 0)
876 goto out;
877 }
878
879 /* Try to find a timer already configured for the debounce period */
794}
795
796static int enable_debounce(struct gpio_chip *chip, unsigned int offset,
797 unsigned long usecs)
798{
799 struct aspeed_gpio *gpio = gpiochip_get_data(chip);
800 u32 requested_cycles;
801 unsigned long flags;

--- 14 unchanged lines hidden (view full) ---

816
817 if (timer_allocation_registered(gpio, offset)) {
818 rc = unregister_allocated_timer(gpio, offset);
819 if (rc < 0)
820 goto out;
821 }
822
823 /* Try to find a timer already configured for the debounce period */
880 for (i = 1; i < ARRAY_SIZE(debounce_timers); i++) {
824 for (i = 1; i < gpio->config->debounce_timers_num; i++) {
881 u32 cycles;
882
825 u32 cycles;
826
883 cycles = ioread32(gpio->base + debounce_timers[i]);
827 cycles = ioread32(gpio->base + gpio->config->debounce_timers_array[i]);
884 if (requested_cycles == cycles)
885 break;
886 }
887
828 if (requested_cycles == cycles)
829 break;
830 }
831
888 if (i == ARRAY_SIZE(debounce_timers)) {
832 if (i == gpio->config->debounce_timers_num) {
889 int j;
890
891 /*
892 * As there are no timers configured for the requested debounce
893 * period, find an unused timer instead
894 */
895 for (j = 1; j < ARRAY_SIZE(gpio->timer_users); j++) {
896 if (gpio->timer_users[j] == 0)
897 break;
898 }
899
900 if (j == ARRAY_SIZE(gpio->timer_users)) {
901 dev_warn(chip->parent,
833 int j;
834
835 /*
836 * As there are no timers configured for the requested debounce
837 * period, find an unused timer instead
838 */
839 for (j = 1; j < ARRAY_SIZE(gpio->timer_users); j++) {
840 if (gpio->timer_users[j] == 0)
841 break;
842 }
843
844 if (j == ARRAY_SIZE(gpio->timer_users)) {
845 dev_warn(chip->parent,
902 "Debounce timers exhausted, cannot debounce for period %luus\n",
903 usecs);
846 "Debounce timers exhausted, cannot debounce for period %luus\n",
847 usecs);
904
905 rc = -EPERM;
906
907 /*
908 * We already adjusted the accounting to remove @offset
909 * as a user of its previous timer, so also configure
910 * the hardware so @offset has timers disabled for
911 * consistency.
912 */
913 configure_timer(gpio, offset, 0);
914 goto out;
915 }
916
917 i = j;
918
848
849 rc = -EPERM;
850
851 /*
852 * We already adjusted the accounting to remove @offset
853 * as a user of its previous timer, so also configure
854 * the hardware so @offset has timers disabled for
855 * consistency.
856 */
857 configure_timer(gpio, offset, 0);
858 goto out;
859 }
860
861 i = j;
862
919 iowrite32(requested_cycles, gpio->base + debounce_timers[i]);
863 iowrite32(requested_cycles, gpio->base + gpio->config->debounce_timers_array[i]);
920 }
921
922 if (WARN(i == 0, "Cannot register index of disabled timer\n")) {
923 rc = -EINVAL;
924 goto out;
925 }
926
927 register_allocated_timer(gpio, offset, i);

--- 86 unchanged lines hidden (view full) ---

1014 u16 *vreg_offset, u16 *dreg_offset, u8 *bit)
1015{
1016 struct gpio_chip *chip = gpiod_to_chip(desc);
1017 struct aspeed_gpio *gpio = gpiochip_get_data(chip);
1018 int rc = 0, bindex, offset = gpio_chip_hwgpio(desc);
1019 const struct aspeed_gpio_bank *bank = to_bank(offset);
1020 unsigned long flags;
1021
864 }
865
866 if (WARN(i == 0, "Cannot register index of disabled timer\n")) {
867 rc = -EINVAL;
868 goto out;
869 }
870
871 register_allocated_timer(gpio, offset, i);

--- 86 unchanged lines hidden (view full) ---

958 u16 *vreg_offset, u16 *dreg_offset, u8 *bit)
959{
960 struct gpio_chip *chip = gpiod_to_chip(desc);
961 struct aspeed_gpio *gpio = gpiochip_get_data(chip);
962 int rc = 0, bindex, offset = gpio_chip_hwgpio(desc);
963 const struct aspeed_gpio_bank *bank = to_bank(offset);
964 unsigned long flags;
965
966 if (!aspeed_gpio_support_copro(gpio))
967 return -EOPNOTSUPP;
968
1022 if (!gpio->cf_copro_bankmap)
1023 gpio->cf_copro_bankmap = kzalloc(gpio->chip.ngpio >> 3, GFP_KERNEL);
1024 if (!gpio->cf_copro_bankmap)
1025 return -ENOMEM;
1026 if (offset < 0 || offset > gpio->chip.ngpio)
1027 return -EINVAL;
1028 bindex = offset >> 3;
1029
1030 raw_spin_lock_irqsave(&gpio->lock, flags);
1031
1032 /* Sanity check, this shouldn't happen */
1033 if (gpio->cf_copro_bankmap[bindex] == 0xff) {
1034 rc = -EIO;
1035 goto bail;
1036 }
1037 gpio->cf_copro_bankmap[bindex]++;
1038
1039 /* Switch command source */
1040 if (gpio->cf_copro_bankmap[bindex] == 1)
969 if (!gpio->cf_copro_bankmap)
970 gpio->cf_copro_bankmap = kzalloc(gpio->chip.ngpio >> 3, GFP_KERNEL);
971 if (!gpio->cf_copro_bankmap)
972 return -ENOMEM;
973 if (offset < 0 || offset > gpio->chip.ngpio)
974 return -EINVAL;
975 bindex = offset >> 3;
976
977 raw_spin_lock_irqsave(&gpio->lock, flags);
978
979 /* Sanity check, this shouldn't happen */
980 if (gpio->cf_copro_bankmap[bindex] == 0xff) {
981 rc = -EIO;
982 goto bail;
983 }
984 gpio->cf_copro_bankmap[bindex]++;
985
986 /* Switch command source */
987 if (gpio->cf_copro_bankmap[bindex] == 1)
1041 aspeed_gpio_change_cmd_source(gpio, bank, bindex,
988 aspeed_gpio_change_cmd_source(gpio, offset,
1042 GPIO_CMDSRC_COLDFIRE);
1043
1044 if (vreg_offset)
1045 *vreg_offset = bank->val_regs;
1046 if (dreg_offset)
1047 *dreg_offset = bank->rdata_reg;
1048 if (bit)
1049 *bit = GPIO_OFFSET(offset);

--- 7 unchanged lines hidden (view full) ---

1057 * aspeed_gpio_copro_release_gpio - Unmark a GPIO used by the coprocessor.
1058 * @desc: The GPIO to be marked
1059 */
1060int aspeed_gpio_copro_release_gpio(struct gpio_desc *desc)
1061{
1062 struct gpio_chip *chip = gpiod_to_chip(desc);
1063 struct aspeed_gpio *gpio = gpiochip_get_data(chip);
1064 int rc = 0, bindex, offset = gpio_chip_hwgpio(desc);
989 GPIO_CMDSRC_COLDFIRE);
990
991 if (vreg_offset)
992 *vreg_offset = bank->val_regs;
993 if (dreg_offset)
994 *dreg_offset = bank->rdata_reg;
995 if (bit)
996 *bit = GPIO_OFFSET(offset);

--- 7 unchanged lines hidden (view full) ---

1004 * aspeed_gpio_copro_release_gpio - Unmark a GPIO used by the coprocessor.
1005 * @desc: The GPIO to be marked
1006 */
1007int aspeed_gpio_copro_release_gpio(struct gpio_desc *desc)
1008{
1009 struct gpio_chip *chip = gpiod_to_chip(desc);
1010 struct aspeed_gpio *gpio = gpiochip_get_data(chip);
1011 int rc = 0, bindex, offset = gpio_chip_hwgpio(desc);
1065 const struct aspeed_gpio_bank *bank = to_bank(offset);
1066 unsigned long flags;
1067
1012 unsigned long flags;
1013
1014 if (!aspeed_gpio_support_copro(gpio))
1015 return -EOPNOTSUPP;
1016
1068 if (!gpio->cf_copro_bankmap)
1069 return -ENXIO;
1070
1071 if (offset < 0 || offset > gpio->chip.ngpio)
1072 return -EINVAL;
1073 bindex = offset >> 3;
1074
1075 raw_spin_lock_irqsave(&gpio->lock, flags);
1076
1077 /* Sanity check, this shouldn't happen */
1078 if (gpio->cf_copro_bankmap[bindex] == 0) {
1079 rc = -EIO;
1080 goto bail;
1081 }
1082 gpio->cf_copro_bankmap[bindex]--;
1083
1084 /* Switch command source */
1085 if (gpio->cf_copro_bankmap[bindex] == 0)
1017 if (!gpio->cf_copro_bankmap)
1018 return -ENXIO;
1019
1020 if (offset < 0 || offset > gpio->chip.ngpio)
1021 return -EINVAL;
1022 bindex = offset >> 3;
1023
1024 raw_spin_lock_irqsave(&gpio->lock, flags);
1025
1026 /* Sanity check, this shouldn't happen */
1027 if (gpio->cf_copro_bankmap[bindex] == 0) {
1028 rc = -EIO;
1029 goto bail;
1030 }
1031 gpio->cf_copro_bankmap[bindex]--;
1032
1033 /* Switch command source */
1034 if (gpio->cf_copro_bankmap[bindex] == 0)
1086 aspeed_gpio_change_cmd_source(gpio, bank, bindex,
1035 aspeed_gpio_change_cmd_source(gpio, offset,
1087 GPIO_CMDSRC_ARM);
1088 bail:
1089 raw_spin_unlock_irqrestore(&gpio->lock, flags);
1090 return rc;
1091}
1092EXPORT_SYMBOL_GPL(aspeed_gpio_copro_release_gpio);
1093
1094static void aspeed_gpio_irq_print_chip(struct irq_data *d, struct seq_file *p)
1095{
1036 GPIO_CMDSRC_ARM);
1037 bail:
1038 raw_spin_unlock_irqrestore(&gpio->lock, flags);
1039 return rc;
1040}
1041EXPORT_SYMBOL_GPL(aspeed_gpio_copro_release_gpio);
1042
1043static void aspeed_gpio_irq_print_chip(struct irq_data *d, struct seq_file *p)
1044{
1096 const struct aspeed_gpio_bank *bank;
1097 struct aspeed_gpio *gpio;
1045 struct aspeed_gpio *gpio;
1098 u32 bit;
1099 int rc, offset;
1100
1046 int rc, offset;
1047
1101 rc = irqd_to_aspeed_gpio_data(d, &gpio, &bank, &bit, &offset);
1048 rc = irqd_to_aspeed_gpio_data(d, &gpio, &offset);
1102 if (rc)
1103 return;
1104
1105 seq_puts(p, dev_name(gpio->dev));
1106}
1107
1108static const struct irq_chip aspeed_gpio_irq_chip = {
1109 .irq_ack = aspeed_gpio_irq_ack,
1110 .irq_mask = aspeed_gpio_irq_mask,
1111 .irq_unmask = aspeed_gpio_irq_unmask,
1112 .irq_set_type = aspeed_gpio_set_type,
1113 .irq_print_chip = aspeed_gpio_irq_print_chip,
1114 .flags = IRQCHIP_IMMUTABLE,
1115 GPIOCHIP_IRQ_RESOURCE_HELPERS,
1116};
1117
1049 if (rc)
1050 return;
1051
1052 seq_puts(p, dev_name(gpio->dev));
1053}
1054
1055static const struct irq_chip aspeed_gpio_irq_chip = {
1056 .irq_ack = aspeed_gpio_irq_ack,
1057 .irq_mask = aspeed_gpio_irq_mask,
1058 .irq_unmask = aspeed_gpio_irq_unmask,
1059 .irq_set_type = aspeed_gpio_set_type,
1060 .irq_print_chip = aspeed_gpio_irq_print_chip,
1061 .flags = IRQCHIP_IMMUTABLE,
1062 GPIOCHIP_IRQ_RESOURCE_HELPERS,
1063};
1064
1065static void aspeed_g4_reg_bit_set(struct aspeed_gpio *gpio, unsigned int offset,
1066 const enum aspeed_gpio_reg reg, bool val)
1067{
1068 const struct aspeed_gpio_bank *bank = to_bank(offset);
1069 void __iomem *addr = aspeed_gpio_g4_bank_reg(gpio, bank, reg);
1070 u32 temp;
1071
1072 if (reg == reg_val)
1073 temp = gpio->dcache[GPIO_BANK(offset)];
1074 else
1075 temp = ioread32(addr);
1076
1077 if (val)
1078 temp |= GPIO_BIT(offset);
1079 else
1080 temp &= ~GPIO_BIT(offset);
1081
1082 if (reg == reg_val)
1083 gpio->dcache[GPIO_BANK(offset)] = temp;
1084 iowrite32(temp, addr);
1085}
1086
1087static bool aspeed_g4_reg_bit_get(struct aspeed_gpio *gpio, unsigned int offset,
1088 const enum aspeed_gpio_reg reg)
1089{
1090 const struct aspeed_gpio_bank *bank = to_bank(offset);
1091 void __iomem *addr = aspeed_gpio_g4_bank_reg(gpio, bank, reg);
1092
1093 return !!(ioread32(addr) & GPIO_BIT(offset));
1094}
1095
1096static int aspeed_g4_reg_bank_get(struct aspeed_gpio *gpio, unsigned int offset,
1097 const enum aspeed_gpio_reg reg)
1098{
1099 const struct aspeed_gpio_bank *bank = to_bank(offset);
1100 void __iomem *addr = aspeed_gpio_g4_bank_reg(gpio, bank, reg);
1101
1102 if (reg == reg_rdata || reg == reg_irq_status)
1103 return ioread32(addr);
1104 else
1105 return -EOPNOTSUPP;
1106}
1107
1108static void aspeed_g4_privilege_ctrl(struct aspeed_gpio *gpio, unsigned int offset, int cmdsrc)
1109{
1110 /*
1111 * The command source register is only valid in bits 0, 8, 16, and 24, so we use
1112 * (offset & ~(0x7)) to ensure that reg_bits_set always targets a valid bit.
1113 */
1114 /* Source 1 first to avoid illegal 11 combination */
1115 aspeed_g4_reg_bit_set(gpio, offset & ~(0x7), reg_cmdsrc1, !!(cmdsrc & BIT(1)));
1116 /* Then Source 0 */
1117 aspeed_g4_reg_bit_set(gpio, offset & ~(0x7), reg_cmdsrc0, !!(cmdsrc & BIT(0)));
1118}
1119
1120static void aspeed_g4_privilege_init(struct aspeed_gpio *gpio)
1121{
1122 u32 i;
1123
1124 /* Switch all command sources to the ARM by default */
1125 for (i = 0; i < DIV_ROUND_UP(gpio->chip.ngpio, 32); i++) {
1126 aspeed_g4_privilege_ctrl(gpio, (i << 5) + 0, GPIO_CMDSRC_ARM);
1127 aspeed_g4_privilege_ctrl(gpio, (i << 5) + 8, GPIO_CMDSRC_ARM);
1128 aspeed_g4_privilege_ctrl(gpio, (i << 5) + 16, GPIO_CMDSRC_ARM);
1129 aspeed_g4_privilege_ctrl(gpio, (i << 5) + 24, GPIO_CMDSRC_ARM);
1130 }
1131}
1132
1133static bool aspeed_g4_copro_request(struct aspeed_gpio *gpio, unsigned int offset)
1134{
1135 if (!copro_ops || !gpio->cf_copro_bankmap)
1136 return false;
1137 if (!gpio->cf_copro_bankmap[offset >> 3])
1138 return false;
1139 if (!copro_ops->request_access)
1140 return false;
1141
1142 /* Pause the coprocessor */
1143 copro_ops->request_access(copro_data);
1144
1145 /* Change command source back to ARM */
1146 aspeed_g4_privilege_ctrl(gpio, offset, GPIO_CMDSRC_ARM);
1147
1148 /* Update cache */
1149 gpio->dcache[GPIO_BANK(offset)] = aspeed_g4_reg_bank_get(gpio, offset, reg_rdata);
1150
1151 return true;
1152}
1153
1154static void aspeed_g4_copro_release(struct aspeed_gpio *gpio, unsigned int offset)
1155{
1156 if (!copro_ops || !gpio->cf_copro_bankmap)
1157 return;
1158 if (!gpio->cf_copro_bankmap[offset >> 3])
1159 return;
1160 if (!copro_ops->release_access)
1161 return;
1162
1163 /* Change command source back to ColdFire */
1164 aspeed_g4_privilege_ctrl(gpio, offset, GPIO_CMDSRC_COLDFIRE);
1165
1166 /* Restart the coprocessor */
1167 copro_ops->release_access(copro_data);
1168}
1169
1170static const struct aspeed_gpio_llops aspeed_g4_llops = {
1171 .reg_bit_set = aspeed_g4_reg_bit_set,
1172 .reg_bit_get = aspeed_g4_reg_bit_get,
1173 .reg_bank_get = aspeed_g4_reg_bank_get,
1174 .privilege_ctrl = aspeed_g4_privilege_ctrl,
1175 .privilege_init = aspeed_g4_privilege_init,
1176 .copro_request = aspeed_g4_copro_request,
1177 .copro_release = aspeed_g4_copro_release,
1178};
1179
1180static void aspeed_g7_reg_bit_set(struct aspeed_gpio *gpio, unsigned int offset,
1181 const enum aspeed_gpio_reg reg, bool val)
1182{
1183 u32 mask = aspeed_gpio_g7_reg_mask(reg);
1184 void __iomem *addr = gpio->base + GPIO_G7_CTRL_REG_OFFSET(offset);
1185 u32 write_val;
1186
1187 if (mask) {
1188 write_val = (ioread32(addr) & ~(mask)) | field_prep(mask, val);
1189 iowrite32(write_val, addr);
1190 }
1191}
1192
1193static bool aspeed_g7_reg_bit_get(struct aspeed_gpio *gpio, unsigned int offset,
1194 const enum aspeed_gpio_reg reg)
1195{
1196 u32 mask = aspeed_gpio_g7_reg_mask(reg);
1197 void __iomem *addr;
1198
1199 addr = gpio->base + GPIO_G7_CTRL_REG_OFFSET(offset);
1200 if (reg == reg_val)
1201 mask = GPIO_G7_CTRL_IN_DATA;
1202
1203 if (mask)
1204 return field_get(mask, ioread32(addr));
1205 else
1206 return 0;
1207}
1208
1209static int aspeed_g7_reg_bank_get(struct aspeed_gpio *gpio, unsigned int offset,
1210 const enum aspeed_gpio_reg reg)
1211{
1212 void __iomem *addr;
1213
1214 if (reg == reg_irq_status) {
1215 addr = gpio->base + GPIO_G7_IRQ_STS_OFFSET(offset >> 5);
1216 return ioread32(addr);
1217 } else {
1218 return -EOPNOTSUPP;
1219 }
1220}
1221
1222static const struct aspeed_gpio_llops aspeed_g7_llops = {
1223 .reg_bit_set = aspeed_g7_reg_bit_set,
1224 .reg_bit_get = aspeed_g7_reg_bit_get,
1225 .reg_bank_get = aspeed_g7_reg_bank_get,
1226 .privilege_ctrl = NULL,
1227 .privilege_init = NULL,
1228 .copro_request = NULL,
1229 .copro_release = NULL,
1230};
1231
1118/*
1119 * Any banks not specified in a struct aspeed_bank_props array are assumed to
1120 * have the properties:
1121 *
1122 * { .input = 0xffffffff, .output = 0xffffffff }
1123 */
1124
1125static const struct aspeed_bank_props ast2400_bank_props[] = {
1126 /* input output */
1127 { 5, 0xffffffff, 0x0000ffff }, /* U/V/W/X */
1128 { 6, 0x0000000f, 0x0fffff0f }, /* Y/Z/AA/AB, two 4-GPIO holes */
1129 { },
1130};
1131
1132static const struct aspeed_gpio_config ast2400_config =
1133 /* 220 for simplicity, really 216 with two 4-GPIO holes, four at end */
1232/*
1233 * Any banks not specified in a struct aspeed_bank_props array are assumed to
1234 * have the properties:
1235 *
1236 * { .input = 0xffffffff, .output = 0xffffffff }
1237 */
1238
1239static const struct aspeed_bank_props ast2400_bank_props[] = {
1240 /* input output */
1241 { 5, 0xffffffff, 0x0000ffff }, /* U/V/W/X */
1242 { 6, 0x0000000f, 0x0fffff0f }, /* Y/Z/AA/AB, two 4-GPIO holes */
1243 { },
1244};
1245
1246static const struct aspeed_gpio_config ast2400_config =
1247 /* 220 for simplicity, really 216 with two 4-GPIO holes, four at end */
1134 { .nr_gpios = 220, .props = ast2400_bank_props, };
1248 {
1249 .nr_gpios = 220,
1250 .props = ast2400_bank_props,
1251 .llops = &aspeed_g4_llops,
1252 .debounce_timers_array = debounce_timers,
1253 .debounce_timers_num = ARRAY_SIZE(debounce_timers),
1254 .require_dcache = true,
1255 };
1135
1136static const struct aspeed_bank_props ast2500_bank_props[] = {
1137 /* input output */
1138 { 5, 0xffffffff, 0x0000ffff }, /* U/V/W/X */
1139 { 6, 0x0fffffff, 0x0fffffff }, /* Y/Z/AA/AB, 4-GPIO hole */
1140 { 7, 0x000000ff, 0x000000ff }, /* AC */
1141 { },
1142};
1143
1144static const struct aspeed_gpio_config ast2500_config =
1145 /* 232 for simplicity, actual number is 228 (4-GPIO hole in GPIOAB) */
1256
1257static const struct aspeed_bank_props ast2500_bank_props[] = {
1258 /* input output */
1259 { 5, 0xffffffff, 0x0000ffff }, /* U/V/W/X */
1260 { 6, 0x0fffffff, 0x0fffffff }, /* Y/Z/AA/AB, 4-GPIO hole */
1261 { 7, 0x000000ff, 0x000000ff }, /* AC */
1262 { },
1263};
1264
1265static const struct aspeed_gpio_config ast2500_config =
1266 /* 232 for simplicity, actual number is 228 (4-GPIO hole in GPIOAB) */
1146 { .nr_gpios = 232, .props = ast2500_bank_props, };
1267 {
1268 .nr_gpios = 232,
1269 .props = ast2500_bank_props,
1270 .llops = &aspeed_g4_llops,
1271 .debounce_timers_array = debounce_timers,
1272 .debounce_timers_num = ARRAY_SIZE(debounce_timers),
1273 .require_dcache = true,
1274 };
1147
1148static const struct aspeed_bank_props ast2600_bank_props[] = {
1149 /* input output */
1150 {4, 0xffffffff, 0x00ffffff}, /* Q/R/S/T */
1151 {5, 0xffffffff, 0xffffff00}, /* U/V/W/X */
1152 {6, 0x0000ffff, 0x0000ffff}, /* Y/Z */
1153 { },
1154};
1155
1156static const struct aspeed_gpio_config ast2600_config =
1157 /*
1158 * ast2600 has two controllers one with 208 GPIOs and one with 36 GPIOs.
1159 * We expect ngpio being set in the device tree and this is a fallback
1160 * option.
1161 */
1275
1276static const struct aspeed_bank_props ast2600_bank_props[] = {
1277 /* input output */
1278 {4, 0xffffffff, 0x00ffffff}, /* Q/R/S/T */
1279 {5, 0xffffffff, 0xffffff00}, /* U/V/W/X */
1280 {6, 0x0000ffff, 0x0000ffff}, /* Y/Z */
1281 { },
1282};
1283
1284static const struct aspeed_gpio_config ast2600_config =
1285 /*
1286 * ast2600 has two controllers one with 208 GPIOs and one with 36 GPIOs.
1287 * We expect ngpio being set in the device tree and this is a fallback
1288 * option.
1289 */
1162 { .nr_gpios = 208, .props = ast2600_bank_props, };
1290 {
1291 .nr_gpios = 208,
1292 .props = ast2600_bank_props,
1293 .llops = &aspeed_g4_llops,
1294 .debounce_timers_array = debounce_timers,
1295 .debounce_timers_num = ARRAY_SIZE(debounce_timers),
1296 .require_dcache = true,
1297 };
1163
1298
1299static const struct aspeed_bank_props ast2700_bank_props[] = {
1300 /* input output */
1301 { 1, 0x0fffffff, 0x0fffffff }, /* E/F/G/H, 4-GPIO hole */
1302 { 6, 0x00ffffff, 0x00ff0000 }, /* Y/Z/AA */
1303 {},
1304};
1305
1306static const struct aspeed_gpio_config ast2700_config =
1307 /*
1308 * ast2700 has two controllers one with 212 GPIOs and one with 16 GPIOs.
1309 * 216 for simplicity, actual number is 212 (4-GPIO hole in GPIOH)
1310 * We expect ngpio being set in the device tree and this is a fallback
1311 * option.
1312 */
1313 {
1314 .nr_gpios = 216,
1315 .props = ast2700_bank_props,
1316 .llops = &aspeed_g7_llops,
1317 .debounce_timers_array = g7_debounce_timers,
1318 .debounce_timers_num = ARRAY_SIZE(g7_debounce_timers),
1319 .require_dcache = false,
1320 };
1321
1164static const struct of_device_id aspeed_gpio_of_table[] = {
1165 { .compatible = "aspeed,ast2400-gpio", .data = &ast2400_config, },
1166 { .compatible = "aspeed,ast2500-gpio", .data = &ast2500_config, },
1167 { .compatible = "aspeed,ast2600-gpio", .data = &ast2600_config, },
1322static const struct of_device_id aspeed_gpio_of_table[] = {
1323 { .compatible = "aspeed,ast2400-gpio", .data = &ast2400_config, },
1324 { .compatible = "aspeed,ast2500-gpio", .data = &ast2500_config, },
1325 { .compatible = "aspeed,ast2600-gpio", .data = &ast2600_config, },
1326 { .compatible = "aspeed,ast2700-gpio", .data = &ast2700_config, },
1168 {}
1169};
1170MODULE_DEVICE_TABLE(of, aspeed_gpio_of_table);
1171
1327 {}
1328};
1329MODULE_DEVICE_TABLE(of, aspeed_gpio_of_table);
1330
1172static int __init aspeed_gpio_probe(struct platform_device *pdev)
1331static int aspeed_gpio_probe(struct platform_device *pdev)
1173{
1174 const struct of_device_id *gpio_id;
1175 struct gpio_irq_chip *girq;
1176 struct aspeed_gpio *gpio;
1177 int rc, irq, i, banks, err;
1178 u32 ngpio;
1179
1180 gpio = devm_kzalloc(&pdev->dev, sizeof(*gpio), GFP_KERNEL);

--- 16 unchanged lines hidden (view full) ---

1197 if (IS_ERR(gpio->clk)) {
1198 dev_warn(&pdev->dev,
1199 "Failed to get clock from devicetree, debouncing disabled\n");
1200 gpio->clk = NULL;
1201 }
1202
1203 gpio->config = gpio_id->data;
1204
1332{
1333 const struct of_device_id *gpio_id;
1334 struct gpio_irq_chip *girq;
1335 struct aspeed_gpio *gpio;
1336 int rc, irq, i, banks, err;
1337 u32 ngpio;
1338
1339 gpio = devm_kzalloc(&pdev->dev, sizeof(*gpio), GFP_KERNEL);

--- 16 unchanged lines hidden (view full) ---

1356 if (IS_ERR(gpio->clk)) {
1357 dev_warn(&pdev->dev,
1358 "Failed to get clock from devicetree, debouncing disabled\n");
1359 gpio->clk = NULL;
1360 }
1361
1362 gpio->config = gpio_id->data;
1363
1364 if (!gpio->config->llops->reg_bit_set || !gpio->config->llops->reg_bit_get ||
1365 !gpio->config->llops->reg_bank_get)
1366 return -EINVAL;
1367
1205 gpio->chip.parent = &pdev->dev;
1206 err = of_property_read_u32(pdev->dev.of_node, "ngpios", &ngpio);
1207 gpio->chip.ngpio = (u16) ngpio;
1208 if (err)
1209 gpio->chip.ngpio = gpio->config->nr_gpios;
1210 gpio->chip.direction_input = aspeed_gpio_dir_in;
1211 gpio->chip.direction_output = aspeed_gpio_dir_out;
1212 gpio->chip.get_direction = aspeed_gpio_get_direction;
1213 gpio->chip.request = aspeed_gpio_request;
1214 gpio->chip.free = aspeed_gpio_free;
1215 gpio->chip.get = aspeed_gpio_get;
1216 gpio->chip.set = aspeed_gpio_set;
1217 gpio->chip.set_config = aspeed_gpio_set_config;
1218 gpio->chip.label = dev_name(&pdev->dev);
1219 gpio->chip.base = -1;
1220
1368 gpio->chip.parent = &pdev->dev;
1369 err = of_property_read_u32(pdev->dev.of_node, "ngpios", &ngpio);
1370 gpio->chip.ngpio = (u16) ngpio;
1371 if (err)
1372 gpio->chip.ngpio = gpio->config->nr_gpios;
1373 gpio->chip.direction_input = aspeed_gpio_dir_in;
1374 gpio->chip.direction_output = aspeed_gpio_dir_out;
1375 gpio->chip.get_direction = aspeed_gpio_get_direction;
1376 gpio->chip.request = aspeed_gpio_request;
1377 gpio->chip.free = aspeed_gpio_free;
1378 gpio->chip.get = aspeed_gpio_get;
1379 gpio->chip.set = aspeed_gpio_set;
1380 gpio->chip.set_config = aspeed_gpio_set_config;
1381 gpio->chip.label = dev_name(&pdev->dev);
1382 gpio->chip.base = -1;
1383
1221 /* Allocate a cache of the output registers */
1222 banks = DIV_ROUND_UP(gpio->chip.ngpio, 32);
1223 gpio->dcache = devm_kcalloc(&pdev->dev,
1224 banks, sizeof(u32), GFP_KERNEL);
1225 if (!gpio->dcache)
1226 return -ENOMEM;
1227
1228 /*
1229 * Populate it with initial values read from the HW and switch
1230 * all command sources to the ARM by default
1231 */
1232 for (i = 0; i < banks; i++) {
1233 const struct aspeed_gpio_bank *bank = &aspeed_gpio_banks[i];
1234 void __iomem *addr = bank_reg(gpio, bank, reg_rdata);
1235 gpio->dcache[i] = ioread32(addr);
1236 aspeed_gpio_change_cmd_source(gpio, bank, 0, GPIO_CMDSRC_ARM);
1237 aspeed_gpio_change_cmd_source(gpio, bank, 1, GPIO_CMDSRC_ARM);
1238 aspeed_gpio_change_cmd_source(gpio, bank, 2, GPIO_CMDSRC_ARM);
1239 aspeed_gpio_change_cmd_source(gpio, bank, 3, GPIO_CMDSRC_ARM);
1384 if (gpio->config->require_dcache) {
1385 /* Allocate a cache of the output registers */
1386 banks = DIV_ROUND_UP(gpio->chip.ngpio, 32);
1387 gpio->dcache = devm_kcalloc(&pdev->dev, banks, sizeof(u32), GFP_KERNEL);
1388 if (!gpio->dcache)
1389 return -ENOMEM;
1390 /*
1391 * Populate it with initial values read from the HW
1392 */
1393 for (i = 0; i < banks; i++)
1394 gpio->dcache[i] =
1395 gpio->config->llops->reg_bank_get(gpio, (i << 5), reg_rdata);
1240 }
1241
1396 }
1397
1398 if (gpio->config->llops->privilege_init)
1399 gpio->config->llops->privilege_init(gpio);
1400
1242 /* Set up an irqchip */
1243 irq = platform_get_irq(pdev, 0);
1244 if (irq < 0)
1245 return irq;
1246 gpio->irq = irq;
1247 girq = &gpio->chip.irq;
1248 gpio_irq_chip_set_chip(girq, &aspeed_gpio_irq_chip);
1249

--- 15 unchanged lines hidden (view full) ---

1265 rc = devm_gpiochip_add_data(&pdev->dev, &gpio->chip, gpio);
1266 if (rc < 0)
1267 return rc;
1268
1269 return 0;
1270}
1271
1272static struct platform_driver aspeed_gpio_driver = {
1401 /* Set up an irqchip */
1402 irq = platform_get_irq(pdev, 0);
1403 if (irq < 0)
1404 return irq;
1405 gpio->irq = irq;
1406 girq = &gpio->chip.irq;
1407 gpio_irq_chip_set_chip(girq, &aspeed_gpio_irq_chip);
1408

--- 15 unchanged lines hidden (view full) ---

1424 rc = devm_gpiochip_add_data(&pdev->dev, &gpio->chip, gpio);
1425 if (rc < 0)
1426 return rc;
1427
1428 return 0;
1429}
1430
1431static struct platform_driver aspeed_gpio_driver = {
1432 .probe = aspeed_gpio_probe,
1273 .driver = {
1274 .name = KBUILD_MODNAME,
1275 .of_match_table = aspeed_gpio_of_table,
1276 },
1277};
1278
1433 .driver = {
1434 .name = KBUILD_MODNAME,
1435 .of_match_table = aspeed_gpio_of_table,
1436 },
1437};
1438
1279module_platform_driver_probe(aspeed_gpio_driver, aspeed_gpio_probe);
1439module_platform_driver(aspeed_gpio_driver);
1280
1281MODULE_DESCRIPTION("Aspeed GPIO Driver");
1282MODULE_LICENSE("GPL");
1440
1441MODULE_DESCRIPTION("Aspeed GPIO Driver");
1442MODULE_LICENSE("GPL");