pio.c 9.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434
  1. /*
  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. #include <linux/clk.h>
  11. #include <linux/debugfs.h>
  12. #include <linux/fs.h>
  13. #include <linux/platform_device.h>
  14. #include <linux/irq.h>
  15. #include <asm/gpio.h>
  16. #include <asm/io.h>
  17. #include <mach/portmux.h>
  18. #include "pio.h"
  19. #define MAX_NR_PIO_DEVICES 8
  20. struct pio_device {
  21. struct gpio_chip chip;
  22. void __iomem *regs;
  23. const struct platform_device *pdev;
  24. struct clk *clk;
  25. u32 pinmux_mask;
  26. char name[8];
  27. };
  28. static struct pio_device pio_dev[MAX_NR_PIO_DEVICES];
  29. static struct pio_device *gpio_to_pio(unsigned int gpio)
  30. {
  31. struct pio_device *pio;
  32. unsigned int index;
  33. index = gpio >> 5;
  34. if (index >= MAX_NR_PIO_DEVICES)
  35. return NULL;
  36. pio = &pio_dev[index];
  37. if (!pio->regs)
  38. return NULL;
  39. return pio;
  40. }
  41. /* Pin multiplexing API */
  42. void __init at32_select_periph(unsigned int pin, unsigned int periph,
  43. unsigned long flags)
  44. {
  45. struct pio_device *pio;
  46. unsigned int pin_index = pin & 0x1f;
  47. u32 mask = 1 << pin_index;
  48. pio = gpio_to_pio(pin);
  49. if (unlikely(!pio)) {
  50. printk("pio: invalid pin %u\n", pin);
  51. goto fail;
  52. }
  53. if (unlikely(test_and_set_bit(pin_index, &pio->pinmux_mask)
  54. || gpiochip_is_requested(&pio->chip, pin_index))) {
  55. printk("%s: pin %u is busy\n", pio->name, pin_index);
  56. goto fail;
  57. }
  58. pio_writel(pio, PUER, mask);
  59. if (periph)
  60. pio_writel(pio, BSR, mask);
  61. else
  62. pio_writel(pio, ASR, mask);
  63. pio_writel(pio, PDR, mask);
  64. if (!(flags & AT32_GPIOF_PULLUP))
  65. pio_writel(pio, PUDR, mask);
  66. return;
  67. fail:
  68. dump_stack();
  69. }
  70. void __init at32_select_gpio(unsigned int pin, unsigned long flags)
  71. {
  72. struct pio_device *pio;
  73. unsigned int pin_index = pin & 0x1f;
  74. u32 mask = 1 << pin_index;
  75. pio = gpio_to_pio(pin);
  76. if (unlikely(!pio)) {
  77. printk("pio: invalid pin %u\n", pin);
  78. goto fail;
  79. }
  80. if (unlikely(test_and_set_bit(pin_index, &pio->pinmux_mask))) {
  81. printk("%s: pin %u is busy\n", pio->name, pin_index);
  82. goto fail;
  83. }
  84. if (flags & AT32_GPIOF_OUTPUT) {
  85. if (flags & AT32_GPIOF_HIGH)
  86. pio_writel(pio, SODR, mask);
  87. else
  88. pio_writel(pio, CODR, mask);
  89. if (flags & AT32_GPIOF_MULTIDRV)
  90. pio_writel(pio, MDER, mask);
  91. else
  92. pio_writel(pio, MDDR, mask);
  93. pio_writel(pio, PUDR, mask);
  94. pio_writel(pio, OER, mask);
  95. } else {
  96. if (flags & AT32_GPIOF_PULLUP)
  97. pio_writel(pio, PUER, mask);
  98. else
  99. pio_writel(pio, PUDR, mask);
  100. if (flags & AT32_GPIOF_DEGLITCH)
  101. pio_writel(pio, IFER, mask);
  102. else
  103. pio_writel(pio, IFDR, mask);
  104. pio_writel(pio, ODR, mask);
  105. }
  106. pio_writel(pio, PER, mask);
  107. return;
  108. fail:
  109. dump_stack();
  110. }
  111. /* Reserve a pin, preventing anyone else from changing its configuration. */
  112. void __init at32_reserve_pin(unsigned int pin)
  113. {
  114. struct pio_device *pio;
  115. unsigned int pin_index = pin & 0x1f;
  116. pio = gpio_to_pio(pin);
  117. if (unlikely(!pio)) {
  118. printk("pio: invalid pin %u\n", pin);
  119. goto fail;
  120. }
  121. if (unlikely(test_and_set_bit(pin_index, &pio->pinmux_mask))) {
  122. printk("%s: pin %u is busy\n", pio->name, pin_index);
  123. goto fail;
  124. }
  125. return;
  126. fail:
  127. dump_stack();
  128. }
  129. /*--------------------------------------------------------------------------*/
  130. /* GPIO API */
  131. static int direction_input(struct gpio_chip *chip, unsigned offset)
  132. {
  133. struct pio_device *pio = container_of(chip, struct pio_device, chip);
  134. u32 mask = 1 << offset;
  135. if (!(pio_readl(pio, PSR) & mask))
  136. return -EINVAL;
  137. pio_writel(pio, ODR, mask);
  138. return 0;
  139. }
  140. static int gpio_get(struct gpio_chip *chip, unsigned offset)
  141. {
  142. struct pio_device *pio = container_of(chip, struct pio_device, chip);
  143. return (pio_readl(pio, PDSR) >> offset) & 1;
  144. }
  145. static void gpio_set(struct gpio_chip *chip, unsigned offset, int value);
  146. static int direction_output(struct gpio_chip *chip, unsigned offset, int value)
  147. {
  148. struct pio_device *pio = container_of(chip, struct pio_device, chip);
  149. u32 mask = 1 << offset;
  150. if (!(pio_readl(pio, PSR) & mask))
  151. return -EINVAL;
  152. gpio_set(chip, offset, value);
  153. pio_writel(pio, OER, mask);
  154. return 0;
  155. }
  156. static void gpio_set(struct gpio_chip *chip, unsigned offset, int value)
  157. {
  158. struct pio_device *pio = container_of(chip, struct pio_device, chip);
  159. u32 mask = 1 << offset;
  160. if (value)
  161. pio_writel(pio, SODR, mask);
  162. else
  163. pio_writel(pio, CODR, mask);
  164. }
  165. /*--------------------------------------------------------------------------*/
  166. /* GPIO IRQ support */
  167. static void gpio_irq_mask(unsigned irq)
  168. {
  169. unsigned gpio = irq_to_gpio(irq);
  170. struct pio_device *pio = &pio_dev[gpio >> 5];
  171. pio_writel(pio, IDR, 1 << (gpio & 0x1f));
  172. }
  173. static void gpio_irq_unmask(unsigned irq)
  174. {
  175. unsigned gpio = irq_to_gpio(irq);
  176. struct pio_device *pio = &pio_dev[gpio >> 5];
  177. pio_writel(pio, IER, 1 << (gpio & 0x1f));
  178. }
  179. static int gpio_irq_type(unsigned irq, unsigned type)
  180. {
  181. if (type != IRQ_TYPE_EDGE_BOTH && type != IRQ_TYPE_NONE)
  182. return -EINVAL;
  183. return 0;
  184. }
  185. static struct irq_chip gpio_irqchip = {
  186. .name = "gpio",
  187. .mask = gpio_irq_mask,
  188. .unmask = gpio_irq_unmask,
  189. .set_type = gpio_irq_type,
  190. };
  191. static void gpio_irq_handler(unsigned irq, struct irq_desc *desc)
  192. {
  193. struct pio_device *pio = get_irq_chip_data(irq);
  194. unsigned gpio_irq;
  195. gpio_irq = (unsigned) get_irq_data(irq);
  196. for (;;) {
  197. u32 isr;
  198. struct irq_desc *d;
  199. /* ack pending GPIO interrupts */
  200. isr = pio_readl(pio, ISR) & pio_readl(pio, IMR);
  201. if (!isr)
  202. break;
  203. do {
  204. int i;
  205. i = ffs(isr) - 1;
  206. isr &= ~(1 << i);
  207. i += gpio_irq;
  208. d = &irq_desc[i];
  209. d->handle_irq(i, d);
  210. } while (isr);
  211. }
  212. }
  213. static void __init
  214. gpio_irq_setup(struct pio_device *pio, int irq, int gpio_irq)
  215. {
  216. unsigned i;
  217. set_irq_chip_data(irq, pio);
  218. set_irq_data(irq, (void *) gpio_irq);
  219. for (i = 0; i < 32; i++, gpio_irq++) {
  220. set_irq_chip_data(gpio_irq, pio);
  221. set_irq_chip_and_handler(gpio_irq, &gpio_irqchip,
  222. handle_simple_irq);
  223. }
  224. set_irq_chained_handler(irq, gpio_irq_handler);
  225. }
  226. /*--------------------------------------------------------------------------*/
  227. #ifdef CONFIG_DEBUG_FS
  228. #include <linux/seq_file.h>
  229. /*
  230. * This shows more info than the generic gpio dump code:
  231. * pullups, deglitching, open drain drive.
  232. */
  233. static void pio_bank_show(struct seq_file *s, struct gpio_chip *chip)
  234. {
  235. struct pio_device *pio = container_of(chip, struct pio_device, chip);
  236. u32 psr, osr, imr, pdsr, pusr, ifsr, mdsr;
  237. unsigned i;
  238. u32 mask;
  239. char bank;
  240. psr = pio_readl(pio, PSR);
  241. osr = pio_readl(pio, OSR);
  242. imr = pio_readl(pio, IMR);
  243. pdsr = pio_readl(pio, PDSR);
  244. pusr = pio_readl(pio, PUSR);
  245. ifsr = pio_readl(pio, IFSR);
  246. mdsr = pio_readl(pio, MDSR);
  247. bank = 'A' + pio->pdev->id;
  248. for (i = 0, mask = 1; i < 32; i++, mask <<= 1) {
  249. const char *label;
  250. label = gpiochip_is_requested(chip, i);
  251. if (!label && (imr & mask))
  252. label = "[irq]";
  253. if (!label)
  254. continue;
  255. seq_printf(s, " gpio-%-3d P%c%-2d (%-12s) %s %s %s",
  256. chip->base + i, bank, i,
  257. label,
  258. (osr & mask) ? "out" : "in ",
  259. (mask & pdsr) ? "hi" : "lo",
  260. (mask & pusr) ? " " : "up");
  261. if (ifsr & mask)
  262. seq_printf(s, " deglitch");
  263. if ((osr & mdsr) & mask)
  264. seq_printf(s, " open-drain");
  265. if (imr & mask)
  266. seq_printf(s, " irq-%d edge-both",
  267. gpio_to_irq(chip->base + i));
  268. seq_printf(s, "\n");
  269. }
  270. }
  271. #else
  272. #define pio_bank_show NULL
  273. #endif
  274. /*--------------------------------------------------------------------------*/
  275. static int __init pio_probe(struct platform_device *pdev)
  276. {
  277. struct pio_device *pio = NULL;
  278. int irq = platform_get_irq(pdev, 0);
  279. int gpio_irq_base = GPIO_IRQ_BASE + pdev->id * 32;
  280. BUG_ON(pdev->id >= MAX_NR_PIO_DEVICES);
  281. pio = &pio_dev[pdev->id];
  282. BUG_ON(!pio->regs);
  283. pio->chip.label = pio->name;
  284. pio->chip.base = pdev->id * 32;
  285. pio->chip.ngpio = 32;
  286. pio->chip.dev = &pdev->dev;
  287. pio->chip.owner = THIS_MODULE;
  288. pio->chip.direction_input = direction_input;
  289. pio->chip.get = gpio_get;
  290. pio->chip.direction_output = direction_output;
  291. pio->chip.set = gpio_set;
  292. pio->chip.dbg_show = pio_bank_show;
  293. gpiochip_add(&pio->chip);
  294. gpio_irq_setup(pio, irq, gpio_irq_base);
  295. platform_set_drvdata(pdev, pio);
  296. printk(KERN_DEBUG "%s: base 0x%p, irq %d chains %d..%d\n",
  297. pio->name, pio->regs, irq, gpio_irq_base, gpio_irq_base + 31);
  298. return 0;
  299. }
  300. static struct platform_driver pio_driver = {
  301. .probe = pio_probe,
  302. .driver = {
  303. .name = "pio",
  304. },
  305. };
  306. static int __init pio_init(void)
  307. {
  308. return platform_driver_register(&pio_driver);
  309. }
  310. postcore_initcall(pio_init);
  311. void __init at32_init_pio(struct platform_device *pdev)
  312. {
  313. struct resource *regs;
  314. struct pio_device *pio;
  315. if (pdev->id > MAX_NR_PIO_DEVICES) {
  316. dev_err(&pdev->dev, "only %d PIO devices supported\n",
  317. MAX_NR_PIO_DEVICES);
  318. return;
  319. }
  320. pio = &pio_dev[pdev->id];
  321. snprintf(pio->name, sizeof(pio->name), "pio%d", pdev->id);
  322. regs = platform_get_resource(pdev, IORESOURCE_MEM, 0);
  323. if (!regs) {
  324. dev_err(&pdev->dev, "no mmio resource defined\n");
  325. return;
  326. }
  327. pio->clk = clk_get(&pdev->dev, "mck");
  328. if (IS_ERR(pio->clk))
  329. /*
  330. * This is a fatal error, but if we continue we might
  331. * be so lucky that we manage to initialize the
  332. * console and display this message...
  333. */
  334. dev_err(&pdev->dev, "no mck clock defined\n");
  335. else
  336. clk_enable(pio->clk);
  337. pio->pdev = pdev;
  338. pio->regs = ioremap(regs->start, regs->end - regs->start + 1);
  339. /* start with irqs disabled and acked */
  340. pio_writel(pio, IDR, ~0UL);
  341. (void) pio_readl(pio, ISR);
  342. }