blob: 9ba5654cde110dda279fe83498ab5a1c25d33144 [file] [log] [blame]
Haavard Skinnemoen5f97f7f2006-09-25 23:32:13 -07001/*
2 * Atmel PIO2 Port Multiplexer support
3 *
4 * Copyright (C) 2004-2006 Atmel Corporation
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 version 2 as
8 * published by the Free Software Foundation.
9 */
10
11#include <linux/clk.h>
12#include <linux/debugfs.h>
13#include <linux/fs.h>
14#include <linux/platform_device.h>
Haavard Skinnemoen6a4e5222007-02-05 16:57:13 +010015#include <linux/irq.h>
Haavard Skinnemoen5f97f7f2006-09-25 23:32:13 -070016
Haavard Skinnemoen6a4e5222007-02-05 16:57:13 +010017#include <asm/gpio.h>
Haavard Skinnemoen5f97f7f2006-09-25 23:32:13 -070018#include <asm/io.h>
19
20#include <asm/arch/portmux.h>
21
22#include "pio.h"
23
24#define MAX_NR_PIO_DEVICES 8
25
26struct pio_device {
27 void __iomem *regs;
28 const struct platform_device *pdev;
29 struct clk *clk;
Haavard Skinnemoenc3e2a792006-12-04 13:46:52 +010030 u32 pinmux_mask;
Haavard Skinnemoen6a4e5222007-02-05 16:57:13 +010031 u32 gpio_mask;
32 char name[8];
Haavard Skinnemoen5f97f7f2006-09-25 23:32:13 -070033};
34
35static struct pio_device pio_dev[MAX_NR_PIO_DEVICES];
36
Haavard Skinnemoenc3e2a792006-12-04 13:46:52 +010037static struct pio_device *gpio_to_pio(unsigned int gpio)
Haavard Skinnemoen5f97f7f2006-09-25 23:32:13 -070038{
39 struct pio_device *pio;
Haavard Skinnemoenc3e2a792006-12-04 13:46:52 +010040 unsigned int index;
Haavard Skinnemoen5f97f7f2006-09-25 23:32:13 -070041
Haavard Skinnemoenc3e2a792006-12-04 13:46:52 +010042 index = gpio >> 5;
43 if (index >= MAX_NR_PIO_DEVICES)
44 return NULL;
45 pio = &pio_dev[index];
46 if (!pio->regs)
47 return NULL;
Haavard Skinnemoen5f97f7f2006-09-25 23:32:13 -070048
Haavard Skinnemoenc3e2a792006-12-04 13:46:52 +010049 return pio;
50}
Haavard Skinnemoen5f97f7f2006-09-25 23:32:13 -070051
Haavard Skinnemoenc3e2a792006-12-04 13:46:52 +010052/* Pin multiplexing API */
53
54void __init at32_select_periph(unsigned int pin, unsigned int periph,
55 unsigned long flags)
56{
57 struct pio_device *pio;
58 unsigned int pin_index = pin & 0x1f;
59 u32 mask = 1 << pin_index;
60
61 pio = gpio_to_pio(pin);
62 if (unlikely(!pio)) {
63 printk("pio: invalid pin %u\n", pin);
64 goto fail;
65 }
66
67 if (unlikely(test_and_set_bit(pin_index, &pio->pinmux_mask))) {
68 printk("%s: pin %u is busy\n", pio->name, pin_index);
69 goto fail;
70 }
71
72 pio_writel(pio, PUER, mask);
73 if (periph)
Haavard Skinnemoen5f97f7f2006-09-25 23:32:13 -070074 pio_writel(pio, BSR, mask);
75 else
76 pio_writel(pio, ASR, mask);
Haavard Skinnemoenc3e2a792006-12-04 13:46:52 +010077
Haavard Skinnemoen5f97f7f2006-09-25 23:32:13 -070078 pio_writel(pio, PDR, mask);
Haavard Skinnemoenc3e2a792006-12-04 13:46:52 +010079 if (!(flags & AT32_GPIOF_PULLUP))
80 pio_writel(pio, PUDR, mask);
81
Haavard Skinnemoen6a4e5222007-02-05 16:57:13 +010082 /* gpio_request NOT allowed */
83 set_bit(pin_index, &pio->gpio_mask);
84
Haavard Skinnemoenc3e2a792006-12-04 13:46:52 +010085 return;
86
87fail:
88 dump_stack();
89}
90
91void __init at32_select_gpio(unsigned int pin, unsigned long flags)
92{
93 struct pio_device *pio;
94 unsigned int pin_index = pin & 0x1f;
95 u32 mask = 1 << pin_index;
96
97 pio = gpio_to_pio(pin);
98 if (unlikely(!pio)) {
99 printk("pio: invalid pin %u\n", pin);
100 goto fail;
101 }
102
103 if (unlikely(test_and_set_bit(pin_index, &pio->pinmux_mask))) {
104 printk("%s: pin %u is busy\n", pio->name, pin_index);
105 goto fail;
106 }
107
Haavard Skinnemoen6a4e5222007-02-05 16:57:13 +0100108 if (flags & AT32_GPIOF_OUTPUT) {
109 if (flags & AT32_GPIOF_HIGH)
110 pio_writel(pio, SODR, mask);
111 else
112 pio_writel(pio, CODR, mask);
113 pio_writel(pio, PUDR, mask);
Haavard Skinnemoenc3e2a792006-12-04 13:46:52 +0100114 pio_writel(pio, OER, mask);
Haavard Skinnemoen6a4e5222007-02-05 16:57:13 +0100115 } else {
116 if (flags & AT32_GPIOF_PULLUP)
117 pio_writel(pio, PUER, mask);
118 else
119 pio_writel(pio, PUDR, mask);
120 if (flags & AT32_GPIOF_DEGLITCH)
121 pio_writel(pio, IFER, mask);
122 else
123 pio_writel(pio, IFDR, mask);
Haavard Skinnemoenc3e2a792006-12-04 13:46:52 +0100124 pio_writel(pio, ODR, mask);
Haavard Skinnemoen6a4e5222007-02-05 16:57:13 +0100125 }
Haavard Skinnemoenc3e2a792006-12-04 13:46:52 +0100126
127 pio_writel(pio, PER, mask);
Haavard Skinnemoen6a4e5222007-02-05 16:57:13 +0100128
129 /* gpio_request now allowed */
130 clear_bit(pin_index, &pio->gpio_mask);
Haavard Skinnemoenc3e2a792006-12-04 13:46:52 +0100131
132 return;
133
134fail:
135 dump_stack();
Haavard Skinnemoen5f97f7f2006-09-25 23:32:13 -0700136}
137
Haavard Skinnemoene7f70b82007-01-30 11:01:23 +0100138/* Reserve a pin, preventing anyone else from changing its configuration. */
139void __init at32_reserve_pin(unsigned int pin)
140{
141 struct pio_device *pio;
142 unsigned int pin_index = pin & 0x1f;
143
144 pio = gpio_to_pio(pin);
145 if (unlikely(!pio)) {
146 printk("pio: invalid pin %u\n", pin);
147 goto fail;
148 }
149
150 if (unlikely(test_and_set_bit(pin_index, &pio->pinmux_mask))) {
151 printk("%s: pin %u is busy\n", pio->name, pin_index);
152 goto fail;
153 }
154
155 return;
156
157fail:
158 dump_stack();
159}
Haavard Skinnemoen6a4e5222007-02-05 16:57:13 +0100160
161/*--------------------------------------------------------------------------*/
162
163/* GPIO API */
164
165int gpio_request(unsigned int gpio, const char *label)
166{
167 struct pio_device *pio;
168 unsigned int pin;
169
170 pio = gpio_to_pio(gpio);
171 if (!pio)
172 return -ENODEV;
173
174 pin = gpio & 0x1f;
175 if (test_and_set_bit(pin, &pio->gpio_mask))
176 return -EBUSY;
177
178 return 0;
179}
180EXPORT_SYMBOL(gpio_request);
181
182void gpio_free(unsigned int gpio)
183{
184 struct pio_device *pio;
185 unsigned int pin;
186
187 pio = gpio_to_pio(gpio);
188 if (!pio) {
189 printk(KERN_ERR
190 "gpio: attempted to free invalid pin %u\n", gpio);
191 return;
192 }
193
194 pin = gpio & 0x1f;
195 if (!test_and_clear_bit(pin, &pio->gpio_mask))
196 printk(KERN_ERR "gpio: freeing free or non-gpio pin %s-%u\n",
197 pio->name, pin);
198}
199EXPORT_SYMBOL(gpio_free);
200
201int gpio_direction_input(unsigned int gpio)
202{
203 struct pio_device *pio;
204 unsigned int pin;
205
206 pio = gpio_to_pio(gpio);
207 if (!pio)
208 return -ENODEV;
209
210 pin = gpio & 0x1f;
211 pio_writel(pio, ODR, 1 << pin);
212
213 return 0;
214}
215EXPORT_SYMBOL(gpio_direction_input);
216
217int gpio_direction_output(unsigned int gpio)
218{
219 struct pio_device *pio;
220 unsigned int pin;
221
222 pio = gpio_to_pio(gpio);
223 if (!pio)
224 return -ENODEV;
225
226 pin = gpio & 0x1f;
227 pio_writel(pio, OER, 1 << pin);
228
229 return 0;
230}
231EXPORT_SYMBOL(gpio_direction_output);
232
233int gpio_get_value(unsigned int gpio)
234{
235 struct pio_device *pio = &pio_dev[gpio >> 5];
236
237 return (pio_readl(pio, PDSR) >> (gpio & 0x1f)) & 1;
238}
239EXPORT_SYMBOL(gpio_get_value);
240
241void gpio_set_value(unsigned int gpio, int value)
242{
243 struct pio_device *pio = &pio_dev[gpio >> 5];
244 u32 mask;
245
246 mask = 1 << (gpio & 0x1f);
247 if (value)
248 pio_writel(pio, SODR, mask);
249 else
250 pio_writel(pio, CODR, mask);
251}
252EXPORT_SYMBOL(gpio_set_value);
253
254/*--------------------------------------------------------------------------*/
255
256/* GPIO IRQ support */
257
258static void gpio_irq_mask(unsigned irq)
259{
260 unsigned gpio = irq_to_gpio(irq);
261 struct pio_device *pio = &pio_dev[gpio >> 5];
262
263 pio_writel(pio, IDR, 1 << (gpio & 0x1f));
264}
265
266static void gpio_irq_unmask(unsigned irq)
267{
268 unsigned gpio = irq_to_gpio(irq);
269 struct pio_device *pio = &pio_dev[gpio >> 5];
270
271 pio_writel(pio, IER, 1 << (gpio & 0x1f));
272}
273
274static int gpio_irq_type(unsigned irq, unsigned type)
275{
276 if (type != IRQ_TYPE_EDGE_BOTH && type != IRQ_TYPE_NONE)
277 return -EINVAL;
278
279 return 0;
280}
281
282static struct irq_chip gpio_irqchip = {
283 .name = "gpio",
284 .mask = gpio_irq_mask,
285 .unmask = gpio_irq_unmask,
286 .set_type = gpio_irq_type,
287};
288
289static void gpio_irq_handler(unsigned irq, struct irq_desc *desc)
290{
291 struct pio_device *pio = get_irq_chip_data(irq);
292 unsigned gpio_irq;
293
294 gpio_irq = (unsigned) get_irq_data(irq);
295 for (;;) {
296 u32 isr;
297 struct irq_desc *d;
298
299 /* ack pending GPIO interrupts */
300 isr = pio_readl(pio, ISR) & pio_readl(pio, IMR);
301 if (!isr)
302 break;
303 do {
304 int i;
305
306 i = ffs(isr) - 1;
307 isr &= ~(1 << i);
308
309 i += gpio_irq;
310 d = &irq_desc[i];
311
312 d->handle_irq(i, d);
313 } while (isr);
314 }
315}
316
317static void __init
318gpio_irq_setup(struct pio_device *pio, int irq, int gpio_irq)
319{
320 unsigned i;
321
322 set_irq_chip_data(irq, pio);
323 set_irq_data(irq, (void *) gpio_irq);
324
325 for (i = 0; i < 32; i++, gpio_irq++) {
326 set_irq_chip_data(gpio_irq, pio);
327 set_irq_chip_and_handler(gpio_irq, &gpio_irqchip,
328 handle_simple_irq);
329 }
330
331 set_irq_chained_handler(irq, gpio_irq_handler);
332}
333
334/*--------------------------------------------------------------------------*/
335
Haavard Skinnemoen5f97f7f2006-09-25 23:32:13 -0700336static int __init pio_probe(struct platform_device *pdev)
337{
338 struct pio_device *pio = NULL;
Haavard Skinnemoen6a4e5222007-02-05 16:57:13 +0100339 int irq = platform_get_irq(pdev, 0);
340 int gpio_irq_base = GPIO_IRQ_BASE + pdev->id * 32;
Haavard Skinnemoen5f97f7f2006-09-25 23:32:13 -0700341
342 BUG_ON(pdev->id >= MAX_NR_PIO_DEVICES);
343 pio = &pio_dev[pdev->id];
344 BUG_ON(!pio->regs);
345
Haavard Skinnemoen6a4e5222007-02-05 16:57:13 +0100346 gpio_irq_setup(pio, irq, gpio_irq_base);
Haavard Skinnemoen5f97f7f2006-09-25 23:32:13 -0700347
348 platform_set_drvdata(pdev, pio);
349
Haavard Skinnemoen6a4e5222007-02-05 16:57:13 +0100350 printk(KERN_DEBUG "%s: base 0x%p, irq %d chains %d..%d\n",
351 pio->name, pio->regs, irq, gpio_irq_base, gpio_irq_base + 31);
Haavard Skinnemoen5f97f7f2006-09-25 23:32:13 -0700352
353 return 0;
354}
355
356static struct platform_driver pio_driver = {
357 .probe = pio_probe,
358 .driver = {
359 .name = "pio",
360 },
361};
362
363static int __init pio_init(void)
364{
365 return platform_driver_register(&pio_driver);
366}
Haavard Skinnemoen6a4e5222007-02-05 16:57:13 +0100367postcore_initcall(pio_init);
Haavard Skinnemoen5f97f7f2006-09-25 23:32:13 -0700368
369void __init at32_init_pio(struct platform_device *pdev)
370{
371 struct resource *regs;
372 struct pio_device *pio;
373
374 if (pdev->id > MAX_NR_PIO_DEVICES) {
375 dev_err(&pdev->dev, "only %d PIO devices supported\n",
376 MAX_NR_PIO_DEVICES);
377 return;
378 }
379
380 pio = &pio_dev[pdev->id];
381 snprintf(pio->name, sizeof(pio->name), "pio%d", pdev->id);
382
383 regs = platform_get_resource(pdev, IORESOURCE_MEM, 0);
384 if (!regs) {
385 dev_err(&pdev->dev, "no mmio resource defined\n");
386 return;
387 }
388
389 pio->clk = clk_get(&pdev->dev, "mck");
390 if (IS_ERR(pio->clk))
391 /*
392 * This is a fatal error, but if we continue we might
393 * be so lucky that we manage to initialize the
394 * console and display this message...
395 */
396 dev_err(&pdev->dev, "no mck clock defined\n");
397 else
398 clk_enable(pio->clk);
399
400 pio->pdev = pdev;
401 pio->regs = ioremap(regs->start, regs->end - regs->start + 1);
402
Haavard Skinnemoen6a4e5222007-02-05 16:57:13 +0100403 /*
404 * request_gpio() is only valid for pins that have been
405 * explicitly configured as GPIO and not previously requested
406 */
407 pio->gpio_mask = ~0UL;
408
Haavard Skinnemoen6a4e5222007-02-05 16:57:13 +0100409 /* start with irqs disabled and acked */
410 pio_writel(pio, IDR, ~0UL);
411 (void) pio_readl(pio, ISR);
Haavard Skinnemoen5f97f7f2006-09-25 23:32:13 -0700412}