pio.c 9.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452
  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. /*
  112. * Undo a previous pin reservation. Will not affect the hardware
  113. * configuration.
  114. */
  115. void at32_deselect_pin(unsigned int pin)
  116. {
  117. struct pio_device *pio;
  118. unsigned int pin_index = pin & 0x1f;
  119. pio = gpio_to_pio(pin);
  120. if (unlikely(!pio)) {
  121. printk("pio: invalid pin %u\n", pin);
  122. dump_stack();
  123. return;
  124. }
  125. clear_bit(pin_index, &pio->pinmux_mask);
  126. }
  127. /* Reserve a pin, preventing anyone else from changing its configuration. */
  128. void __init at32_reserve_pin(unsigned int pin)
  129. {
  130. struct pio_device *pio;
  131. unsigned int pin_index = pin & 0x1f;
  132. pio = gpio_to_pio(pin);
  133. if (unlikely(!pio)) {
  134. printk("pio: invalid pin %u\n", pin);
  135. goto fail;
  136. }
  137. if (unlikely(test_and_set_bit(pin_index, &pio->pinmux_mask))) {
  138. printk("%s: pin %u is busy\n", pio->name, pin_index);
  139. goto fail;
  140. }
  141. return;
  142. fail:
  143. dump_stack();
  144. }
  145. /*--------------------------------------------------------------------------*/
  146. /* GPIO API */
  147. static int direction_input(struct gpio_chip *chip, unsigned offset)
  148. {
  149. struct pio_device *pio = container_of(chip, struct pio_device, chip);
  150. u32 mask = 1 << offset;
  151. if (!(pio_readl(pio, PSR) & mask))
  152. return -EINVAL;
  153. pio_writel(pio, ODR, mask);
  154. return 0;
  155. }
  156. static int gpio_get(struct gpio_chip *chip, unsigned offset)
  157. {
  158. struct pio_device *pio = container_of(chip, struct pio_device, chip);
  159. return (pio_readl(pio, PDSR) >> offset) & 1;
  160. }
  161. static void gpio_set(struct gpio_chip *chip, unsigned offset, int value);
  162. static int direction_output(struct gpio_chip *chip, unsigned offset, int value)
  163. {
  164. struct pio_device *pio = container_of(chip, struct pio_device, chip);
  165. u32 mask = 1 << offset;
  166. if (!(pio_readl(pio, PSR) & mask))
  167. return -EINVAL;
  168. gpio_set(chip, offset, value);
  169. pio_writel(pio, OER, mask);
  170. return 0;
  171. }
  172. static void gpio_set(struct gpio_chip *chip, unsigned offset, int value)
  173. {
  174. struct pio_device *pio = container_of(chip, struct pio_device, chip);
  175. u32 mask = 1 << offset;
  176. if (value)
  177. pio_writel(pio, SODR, mask);
  178. else
  179. pio_writel(pio, CODR, mask);
  180. }
  181. /*--------------------------------------------------------------------------*/
  182. /* GPIO IRQ support */
  183. static void gpio_irq_mask(unsigned irq)
  184. {
  185. unsigned gpio = irq_to_gpio(irq);
  186. struct pio_device *pio = &pio_dev[gpio >> 5];
  187. pio_writel(pio, IDR, 1 << (gpio & 0x1f));
  188. }
  189. static void gpio_irq_unmask(unsigned irq)
  190. {
  191. unsigned gpio = irq_to_gpio(irq);
  192. struct pio_device *pio = &pio_dev[gpio >> 5];
  193. pio_writel(pio, IER, 1 << (gpio & 0x1f));
  194. }
  195. static int gpio_irq_type(unsigned irq, unsigned type)
  196. {
  197. if (type != IRQ_TYPE_EDGE_BOTH && type != IRQ_TYPE_NONE)
  198. return -EINVAL;
  199. return 0;
  200. }
  201. static struct irq_chip gpio_irqchip = {
  202. .name = "gpio",
  203. .mask = gpio_irq_mask,
  204. .unmask = gpio_irq_unmask,
  205. .set_type = gpio_irq_type,
  206. };
  207. static void gpio_irq_handler(unsigned irq, struct irq_desc *desc)
  208. {
  209. struct pio_device *pio = get_irq_chip_data(irq);
  210. unsigned gpio_irq;
  211. gpio_irq = (unsigned) get_irq_data(irq);
  212. for (;;) {
  213. u32 isr;
  214. struct irq_desc *d;
  215. /* ack pending GPIO interrupts */
  216. isr = pio_readl(pio, ISR) & pio_readl(pio, IMR);
  217. if (!isr)
  218. break;
  219. do {
  220. int i;
  221. i = ffs(isr) - 1;
  222. isr &= ~(1 << i);
  223. i += gpio_irq;
  224. d = &irq_desc[i];
  225. d->handle_irq(i, d);
  226. } while (isr);
  227. }
  228. }
  229. static void __init
  230. gpio_irq_setup(struct pio_device *pio, int irq, int gpio_irq)
  231. {
  232. unsigned i;
  233. set_irq_chip_data(irq, pio);
  234. set_irq_data(irq, (void *) gpio_irq);
  235. for (i = 0; i < 32; i++, gpio_irq++) {
  236. set_irq_chip_data(gpio_irq, pio);
  237. set_irq_chip_and_handler(gpio_irq, &gpio_irqchip,
  238. handle_simple_irq);
  239. }
  240. set_irq_chained_handler(irq, gpio_irq_handler);
  241. }
  242. /*--------------------------------------------------------------------------*/
  243. #ifdef CONFIG_DEBUG_FS
  244. #include <linux/seq_file.h>
  245. /*
  246. * This shows more info than the generic gpio dump code:
  247. * pullups, deglitching, open drain drive.
  248. */
  249. static void pio_bank_show(struct seq_file *s, struct gpio_chip *chip)
  250. {
  251. struct pio_device *pio = container_of(chip, struct pio_device, chip);
  252. u32 psr, osr, imr, pdsr, pusr, ifsr, mdsr;
  253. unsigned i;
  254. u32 mask;
  255. char bank;
  256. psr = pio_readl(pio, PSR);
  257. osr = pio_readl(pio, OSR);
  258. imr = pio_readl(pio, IMR);
  259. pdsr = pio_readl(pio, PDSR);
  260. pusr = pio_readl(pio, PUSR);
  261. ifsr = pio_readl(pio, IFSR);
  262. mdsr = pio_readl(pio, MDSR);
  263. bank = 'A' + pio->pdev->id;
  264. for (i = 0, mask = 1; i < 32; i++, mask <<= 1) {
  265. const char *label;
  266. label = gpiochip_is_requested(chip, i);
  267. if (!label && (imr & mask))
  268. label = "[irq]";
  269. if (!label)
  270. continue;
  271. seq_printf(s, " gpio-%-3d P%c%-2d (%-12s) %s %s %s",
  272. chip->base + i, bank, i,
  273. label,
  274. (osr & mask) ? "out" : "in ",
  275. (mask & pdsr) ? "hi" : "lo",
  276. (mask & pusr) ? " " : "up");
  277. if (ifsr & mask)
  278. seq_printf(s, " deglitch");
  279. if ((osr & mdsr) & mask)
  280. seq_printf(s, " open-drain");
  281. if (imr & mask)
  282. seq_printf(s, " irq-%d edge-both",
  283. gpio_to_irq(chip->base + i));
  284. seq_printf(s, "\n");
  285. }
  286. }
  287. #else
  288. #define pio_bank_show NULL
  289. #endif
  290. /*--------------------------------------------------------------------------*/
  291. static int __init pio_probe(struct platform_device *pdev)
  292. {
  293. struct pio_device *pio = NULL;
  294. int irq = platform_get_irq(pdev, 0);
  295. int gpio_irq_base = GPIO_IRQ_BASE + pdev->id * 32;
  296. BUG_ON(pdev->id >= MAX_NR_PIO_DEVICES);
  297. pio = &pio_dev[pdev->id];
  298. BUG_ON(!pio->regs);
  299. pio->chip.label = pio->name;
  300. pio->chip.base = pdev->id * 32;
  301. pio->chip.ngpio = 32;
  302. pio->chip.dev = &pdev->dev;
  303. pio->chip.owner = THIS_MODULE;
  304. pio->chip.direction_input = direction_input;
  305. pio->chip.get = gpio_get;
  306. pio->chip.direction_output = direction_output;
  307. pio->chip.set = gpio_set;
  308. pio->chip.dbg_show = pio_bank_show;
  309. gpiochip_add(&pio->chip);
  310. gpio_irq_setup(pio, irq, gpio_irq_base);
  311. platform_set_drvdata(pdev, pio);
  312. printk(KERN_DEBUG "%s: base 0x%p, irq %d chains %d..%d\n",
  313. pio->name, pio->regs, irq, gpio_irq_base, gpio_irq_base + 31);
  314. return 0;
  315. }
  316. static struct platform_driver pio_driver = {
  317. .driver = {
  318. .name = "pio",
  319. },
  320. };
  321. static int __init pio_init(void)
  322. {
  323. return platform_driver_probe(&pio_driver, pio_probe);
  324. }
  325. postcore_initcall(pio_init);
  326. void __init at32_init_pio(struct platform_device *pdev)
  327. {
  328. struct resource *regs;
  329. struct pio_device *pio;
  330. if (pdev->id > MAX_NR_PIO_DEVICES) {
  331. dev_err(&pdev->dev, "only %d PIO devices supported\n",
  332. MAX_NR_PIO_DEVICES);
  333. return;
  334. }
  335. pio = &pio_dev[pdev->id];
  336. snprintf(pio->name, sizeof(pio->name), "pio%d", pdev->id);
  337. regs = platform_get_resource(pdev, IORESOURCE_MEM, 0);
  338. if (!regs) {
  339. dev_err(&pdev->dev, "no mmio resource defined\n");
  340. return;
  341. }
  342. pio->clk = clk_get(&pdev->dev, "mck");
  343. if (IS_ERR(pio->clk))
  344. /*
  345. * This is a fatal error, but if we continue we might
  346. * be so lucky that we manage to initialize the
  347. * console and display this message...
  348. */
  349. dev_err(&pdev->dev, "no mck clock defined\n");
  350. else
  351. clk_enable(pio->clk);
  352. pio->pdev = pdev;
  353. pio->regs = ioremap(regs->start, regs->end - regs->start + 1);
  354. /* start with irqs disabled and acked */
  355. pio_writel(pio, IDR, ~0UL);
  356. (void) pio_readl(pio, ISR);
  357. }