qe_io.c 5.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219
  1. /*
  2. * arch/powerpc/sysdev/qe_lib/qe_io.c
  3. *
  4. * QE Parallel I/O ports configuration routines
  5. *
  6. * Copyright (C) Freescale Semicondutor, Inc. 2006. All rights reserved.
  7. *
  8. * Author: Li Yang <LeoLi@freescale.com>
  9. * Based on code from Shlomi Gridish <gridish@freescale.com>
  10. *
  11. * This program is free software; you can redistribute it and/or modify it
  12. * under the terms of the GNU General Public License as published by the
  13. * Free Software Foundation; either version 2 of the License, or (at your
  14. * option) any later version.
  15. */
  16. #include <linux/stddef.h>
  17. #include <linux/kernel.h>
  18. #include <linux/init.h>
  19. #include <linux/errno.h>
  20. #include <linux/module.h>
  21. #include <linux/ioport.h>
  22. #include <asm/io.h>
  23. #include <asm/qe.h>
  24. #include <asm/prom.h>
  25. #include <sysdev/fsl_soc.h>
  26. #undef DEBUG
  27. static struct qe_pio_regs __iomem *par_io;
  28. static int num_par_io_ports = 0;
  29. int par_io_init(struct device_node *np)
  30. {
  31. struct resource res;
  32. int ret;
  33. const u32 *num_ports;
  34. /* Map Parallel I/O ports registers */
  35. ret = of_address_to_resource(np, 0, &res);
  36. if (ret)
  37. return ret;
  38. par_io = ioremap(res.start, resource_size(&res));
  39. num_ports = of_get_property(np, "num-ports", NULL);
  40. if (num_ports)
  41. num_par_io_ports = *num_ports;
  42. return 0;
  43. }
  44. void __par_io_config_pin(struct qe_pio_regs __iomem *par_io, u8 pin, int dir,
  45. int open_drain, int assignment, int has_irq)
  46. {
  47. u32 pin_mask1bit;
  48. u32 pin_mask2bits;
  49. u32 new_mask2bits;
  50. u32 tmp_val;
  51. /* calculate pin location for single and 2 bits information */
  52. pin_mask1bit = (u32) (1 << (QE_PIO_PINS - (pin + 1)));
  53. /* Set open drain, if required */
  54. tmp_val = in_be32(&par_io->cpodr);
  55. if (open_drain)
  56. out_be32(&par_io->cpodr, pin_mask1bit | tmp_val);
  57. else
  58. out_be32(&par_io->cpodr, ~pin_mask1bit & tmp_val);
  59. /* define direction */
  60. tmp_val = (pin > (QE_PIO_PINS / 2) - 1) ?
  61. in_be32(&par_io->cpdir2) :
  62. in_be32(&par_io->cpdir1);
  63. /* get all bits mask for 2 bit per port */
  64. pin_mask2bits = (u32) (0x3 << (QE_PIO_PINS -
  65. (pin % (QE_PIO_PINS / 2) + 1) * 2));
  66. /* Get the final mask we need for the right definition */
  67. new_mask2bits = (u32) (dir << (QE_PIO_PINS -
  68. (pin % (QE_PIO_PINS / 2) + 1) * 2));
  69. /* clear and set 2 bits mask */
  70. if (pin > (QE_PIO_PINS / 2) - 1) {
  71. out_be32(&par_io->cpdir2,
  72. ~pin_mask2bits & tmp_val);
  73. tmp_val &= ~pin_mask2bits;
  74. out_be32(&par_io->cpdir2, new_mask2bits | tmp_val);
  75. } else {
  76. out_be32(&par_io->cpdir1,
  77. ~pin_mask2bits & tmp_val);
  78. tmp_val &= ~pin_mask2bits;
  79. out_be32(&par_io->cpdir1, new_mask2bits | tmp_val);
  80. }
  81. /* define pin assignment */
  82. tmp_val = (pin > (QE_PIO_PINS / 2) - 1) ?
  83. in_be32(&par_io->cppar2) :
  84. in_be32(&par_io->cppar1);
  85. new_mask2bits = (u32) (assignment << (QE_PIO_PINS -
  86. (pin % (QE_PIO_PINS / 2) + 1) * 2));
  87. /* clear and set 2 bits mask */
  88. if (pin > (QE_PIO_PINS / 2) - 1) {
  89. out_be32(&par_io->cppar2,
  90. ~pin_mask2bits & tmp_val);
  91. tmp_val &= ~pin_mask2bits;
  92. out_be32(&par_io->cppar2, new_mask2bits | tmp_val);
  93. } else {
  94. out_be32(&par_io->cppar1,
  95. ~pin_mask2bits & tmp_val);
  96. tmp_val &= ~pin_mask2bits;
  97. out_be32(&par_io->cppar1, new_mask2bits | tmp_val);
  98. }
  99. }
  100. EXPORT_SYMBOL(__par_io_config_pin);
  101. int par_io_config_pin(u8 port, u8 pin, int dir, int open_drain,
  102. int assignment, int has_irq)
  103. {
  104. if (!par_io || port >= num_par_io_ports)
  105. return -EINVAL;
  106. __par_io_config_pin(&par_io[port], pin, dir, open_drain, assignment,
  107. has_irq);
  108. return 0;
  109. }
  110. EXPORT_SYMBOL(par_io_config_pin);
  111. int par_io_data_set(u8 port, u8 pin, u8 val)
  112. {
  113. u32 pin_mask, tmp_val;
  114. if (port >= num_par_io_ports)
  115. return -EINVAL;
  116. if (pin >= QE_PIO_PINS)
  117. return -EINVAL;
  118. /* calculate pin location */
  119. pin_mask = (u32) (1 << (QE_PIO_PINS - 1 - pin));
  120. tmp_val = in_be32(&par_io[port].cpdata);
  121. if (val == 0) /* clear */
  122. out_be32(&par_io[port].cpdata, ~pin_mask & tmp_val);
  123. else /* set */
  124. out_be32(&par_io[port].cpdata, pin_mask | tmp_val);
  125. return 0;
  126. }
  127. EXPORT_SYMBOL(par_io_data_set);
  128. int par_io_of_config(struct device_node *np)
  129. {
  130. struct device_node *pio;
  131. const phandle *ph;
  132. int pio_map_len;
  133. const unsigned int *pio_map;
  134. if (par_io == NULL) {
  135. printk(KERN_ERR "par_io not initialized\n");
  136. return -1;
  137. }
  138. ph = of_get_property(np, "pio-handle", NULL);
  139. if (ph == NULL) {
  140. printk(KERN_ERR "pio-handle not available\n");
  141. return -1;
  142. }
  143. pio = of_find_node_by_phandle(*ph);
  144. pio_map = of_get_property(pio, "pio-map", &pio_map_len);
  145. if (pio_map == NULL) {
  146. printk(KERN_ERR "pio-map is not set!\n");
  147. return -1;
  148. }
  149. pio_map_len /= sizeof(unsigned int);
  150. if ((pio_map_len % 6) != 0) {
  151. printk(KERN_ERR "pio-map format wrong!\n");
  152. return -1;
  153. }
  154. while (pio_map_len > 0) {
  155. par_io_config_pin((u8) pio_map[0], (u8) pio_map[1],
  156. (int) pio_map[2], (int) pio_map[3],
  157. (int) pio_map[4], (int) pio_map[5]);
  158. pio_map += 6;
  159. pio_map_len -= 6;
  160. }
  161. of_node_put(pio);
  162. return 0;
  163. }
  164. EXPORT_SYMBOL(par_io_of_config);
  165. #ifdef DEBUG
  166. static void dump_par_io(void)
  167. {
  168. unsigned int i;
  169. printk(KERN_INFO "%s: par_io=%p\n", __func__, par_io);
  170. for (i = 0; i < num_par_io_ports; i++) {
  171. printk(KERN_INFO " cpodr[%u]=%08x\n", i,
  172. in_be32(&par_io[i].cpodr));
  173. printk(KERN_INFO " cpdata[%u]=%08x\n", i,
  174. in_be32(&par_io[i].cpdata));
  175. printk(KERN_INFO " cpdir1[%u]=%08x\n", i,
  176. in_be32(&par_io[i].cpdir1));
  177. printk(KERN_INFO " cpdir2[%u]=%08x\n", i,
  178. in_be32(&par_io[i].cpdir2));
  179. printk(KERN_INFO " cppar1[%u]=%08x\n", i,
  180. in_be32(&par_io[i].cppar1));
  181. printk(KERN_INFO " cppar2[%u]=%08x\n", i,
  182. in_be32(&par_io[i].cppar2));
  183. }
  184. }
  185. EXPORT_SYMBOL(dump_par_io);
  186. #endif /* DEBUG */