pio.c 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189
  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 <asm/io.h>
  15. #include <asm/arch/portmux.h>
  16. #include "pio.h"
  17. #define MAX_NR_PIO_DEVICES 8
  18. struct pio_device {
  19. void __iomem *regs;
  20. const struct platform_device *pdev;
  21. struct clk *clk;
  22. u32 pinmux_mask;
  23. char name[32];
  24. };
  25. static struct pio_device pio_dev[MAX_NR_PIO_DEVICES];
  26. static struct pio_device *gpio_to_pio(unsigned int gpio)
  27. {
  28. struct pio_device *pio;
  29. unsigned int index;
  30. index = gpio >> 5;
  31. if (index >= MAX_NR_PIO_DEVICES)
  32. return NULL;
  33. pio = &pio_dev[index];
  34. if (!pio->regs)
  35. return NULL;
  36. return pio;
  37. }
  38. /* Pin multiplexing API */
  39. void __init at32_select_periph(unsigned int pin, unsigned int periph,
  40. unsigned long flags)
  41. {
  42. struct pio_device *pio;
  43. unsigned int pin_index = pin & 0x1f;
  44. u32 mask = 1 << pin_index;
  45. pio = gpio_to_pio(pin);
  46. if (unlikely(!pio)) {
  47. printk("pio: invalid pin %u\n", pin);
  48. goto fail;
  49. }
  50. if (unlikely(test_and_set_bit(pin_index, &pio->pinmux_mask))) {
  51. printk("%s: pin %u is busy\n", pio->name, pin_index);
  52. goto fail;
  53. }
  54. pio_writel(pio, PUER, mask);
  55. if (periph)
  56. pio_writel(pio, BSR, mask);
  57. else
  58. pio_writel(pio, ASR, mask);
  59. pio_writel(pio, PDR, mask);
  60. if (!(flags & AT32_GPIOF_PULLUP))
  61. pio_writel(pio, PUDR, mask);
  62. return;
  63. fail:
  64. dump_stack();
  65. }
  66. void __init at32_select_gpio(unsigned int pin, unsigned long flags)
  67. {
  68. struct pio_device *pio;
  69. unsigned int pin_index = pin & 0x1f;
  70. u32 mask = 1 << pin_index;
  71. pio = gpio_to_pio(pin);
  72. if (unlikely(!pio)) {
  73. printk("pio: invalid pin %u\n", pin);
  74. goto fail;
  75. }
  76. if (unlikely(test_and_set_bit(pin_index, &pio->pinmux_mask))) {
  77. printk("%s: pin %u is busy\n", pio->name, pin_index);
  78. goto fail;
  79. }
  80. pio_writel(pio, PUER, mask);
  81. if (flags & AT32_GPIOF_HIGH)
  82. pio_writel(pio, SODR, mask);
  83. else
  84. pio_writel(pio, CODR, mask);
  85. if (flags & AT32_GPIOF_OUTPUT)
  86. pio_writel(pio, OER, mask);
  87. else
  88. pio_writel(pio, ODR, mask);
  89. pio_writel(pio, PER, mask);
  90. if (!(flags & AT32_GPIOF_PULLUP))
  91. pio_writel(pio, PUDR, mask);
  92. return;
  93. fail:
  94. dump_stack();
  95. }
  96. static int __init pio_probe(struct platform_device *pdev)
  97. {
  98. struct pio_device *pio = NULL;
  99. BUG_ON(pdev->id >= MAX_NR_PIO_DEVICES);
  100. pio = &pio_dev[pdev->id];
  101. BUG_ON(!pio->regs);
  102. /* TODO: Interrupts */
  103. platform_set_drvdata(pdev, pio);
  104. printk(KERN_INFO "%s: Atmel Port Multiplexer at 0x%p (irq %d)\n",
  105. pio->name, pio->regs, platform_get_irq(pdev, 0));
  106. return 0;
  107. }
  108. static struct platform_driver pio_driver = {
  109. .probe = pio_probe,
  110. .driver = {
  111. .name = "pio",
  112. },
  113. };
  114. static int __init pio_init(void)
  115. {
  116. return platform_driver_register(&pio_driver);
  117. }
  118. subsys_initcall(pio_init);
  119. void __init at32_init_pio(struct platform_device *pdev)
  120. {
  121. struct resource *regs;
  122. struct pio_device *pio;
  123. if (pdev->id > MAX_NR_PIO_DEVICES) {
  124. dev_err(&pdev->dev, "only %d PIO devices supported\n",
  125. MAX_NR_PIO_DEVICES);
  126. return;
  127. }
  128. pio = &pio_dev[pdev->id];
  129. snprintf(pio->name, sizeof(pio->name), "pio%d", pdev->id);
  130. regs = platform_get_resource(pdev, IORESOURCE_MEM, 0);
  131. if (!regs) {
  132. dev_err(&pdev->dev, "no mmio resource defined\n");
  133. return;
  134. }
  135. pio->clk = clk_get(&pdev->dev, "mck");
  136. if (IS_ERR(pio->clk))
  137. /*
  138. * This is a fatal error, but if we continue we might
  139. * be so lucky that we manage to initialize the
  140. * console and display this message...
  141. */
  142. dev_err(&pdev->dev, "no mck clock defined\n");
  143. else
  144. clk_enable(pio->clk);
  145. pio->pdev = pdev;
  146. pio->regs = ioremap(regs->start, regs->end - regs->start + 1);
  147. pio_writel(pio, ODR, ~0UL);
  148. pio_writel(pio, PER, ~0UL);
  149. }