qe_io.c 5.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222
  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. #define NUM_OF_PINS 32
  28. struct port_regs {
  29. __be32 cpodr; /* Open drain register */
  30. __be32 cpdata; /* Data register */
  31. __be32 cpdir1; /* Direction register */
  32. __be32 cpdir2; /* Direction register */
  33. __be32 cppar1; /* Pin assignment register */
  34. __be32 cppar2; /* Pin assignment register */
  35. #ifdef CONFIG_PPC_85xx
  36. u8 pad[8];
  37. #endif
  38. };
  39. static struct port_regs __iomem *par_io;
  40. static int num_par_io_ports = 0;
  41. int par_io_init(struct device_node *np)
  42. {
  43. struct resource res;
  44. int ret;
  45. const u32 *num_ports;
  46. /* Map Parallel I/O ports registers */
  47. ret = of_address_to_resource(np, 0, &res);
  48. if (ret)
  49. return ret;
  50. par_io = ioremap(res.start, res.end - res.start + 1);
  51. num_ports = of_get_property(np, "num-ports", NULL);
  52. if (num_ports)
  53. num_par_io_ports = *num_ports;
  54. return 0;
  55. }
  56. int par_io_config_pin(u8 port, u8 pin, int dir, int open_drain,
  57. int assignment, int has_irq)
  58. {
  59. u32 pin_mask1bit, pin_mask2bits, new_mask2bits, tmp_val;
  60. if (!par_io)
  61. return -1;
  62. /* calculate pin location for single and 2 bits information */
  63. pin_mask1bit = (u32) (1 << (NUM_OF_PINS - (pin + 1)));
  64. /* Set open drain, if required */
  65. tmp_val = in_be32(&par_io[port].cpodr);
  66. if (open_drain)
  67. out_be32(&par_io[port].cpodr, pin_mask1bit | tmp_val);
  68. else
  69. out_be32(&par_io[port].cpodr, ~pin_mask1bit & tmp_val);
  70. /* define direction */
  71. tmp_val = (pin > (NUM_OF_PINS / 2) - 1) ?
  72. in_be32(&par_io[port].cpdir2) :
  73. in_be32(&par_io[port].cpdir1);
  74. /* get all bits mask for 2 bit per port */
  75. pin_mask2bits = (u32) (0x3 << (NUM_OF_PINS -
  76. (pin % (NUM_OF_PINS / 2) + 1) * 2));
  77. /* Get the final mask we need for the right definition */
  78. new_mask2bits = (u32) (dir << (NUM_OF_PINS -
  79. (pin % (NUM_OF_PINS / 2) + 1) * 2));
  80. /* clear and set 2 bits mask */
  81. if (pin > (NUM_OF_PINS / 2) - 1) {
  82. out_be32(&par_io[port].cpdir2,
  83. ~pin_mask2bits & tmp_val);
  84. tmp_val &= ~pin_mask2bits;
  85. out_be32(&par_io[port].cpdir2, new_mask2bits | tmp_val);
  86. } else {
  87. out_be32(&par_io[port].cpdir1,
  88. ~pin_mask2bits & tmp_val);
  89. tmp_val &= ~pin_mask2bits;
  90. out_be32(&par_io[port].cpdir1, new_mask2bits | tmp_val);
  91. }
  92. /* define pin assignment */
  93. tmp_val = (pin > (NUM_OF_PINS / 2) - 1) ?
  94. in_be32(&par_io[port].cppar2) :
  95. in_be32(&par_io[port].cppar1);
  96. new_mask2bits = (u32) (assignment << (NUM_OF_PINS -
  97. (pin % (NUM_OF_PINS / 2) + 1) * 2));
  98. /* clear and set 2 bits mask */
  99. if (pin > (NUM_OF_PINS / 2) - 1) {
  100. out_be32(&par_io[port].cppar2,
  101. ~pin_mask2bits & tmp_val);
  102. tmp_val &= ~pin_mask2bits;
  103. out_be32(&par_io[port].cppar2, new_mask2bits | tmp_val);
  104. } else {
  105. out_be32(&par_io[port].cppar1,
  106. ~pin_mask2bits & tmp_val);
  107. tmp_val &= ~pin_mask2bits;
  108. out_be32(&par_io[port].cppar1, new_mask2bits | tmp_val);
  109. }
  110. return 0;
  111. }
  112. EXPORT_SYMBOL(par_io_config_pin);
  113. int par_io_data_set(u8 port, u8 pin, u8 val)
  114. {
  115. u32 pin_mask, tmp_val;
  116. if (port >= num_par_io_ports)
  117. return -EINVAL;
  118. if (pin >= NUM_OF_PINS)
  119. return -EINVAL;
  120. /* calculate pin location */
  121. pin_mask = (u32) (1 << (NUM_OF_PINS - 1 - pin));
  122. tmp_val = in_be32(&par_io[port].cpdata);
  123. if (val == 0) /* clear */
  124. out_be32(&par_io[port].cpdata, ~pin_mask & tmp_val);
  125. else /* set */
  126. out_be32(&par_io[port].cpdata, pin_mask | tmp_val);
  127. return 0;
  128. }
  129. EXPORT_SYMBOL(par_io_data_set);
  130. int par_io_of_config(struct device_node *np)
  131. {
  132. struct device_node *pio;
  133. const phandle *ph;
  134. int pio_map_len;
  135. const unsigned int *pio_map;
  136. if (par_io == NULL) {
  137. printk(KERN_ERR "par_io not initialized \n");
  138. return -1;
  139. }
  140. ph = of_get_property(np, "pio-handle", NULL);
  141. if (ph == NULL) {
  142. printk(KERN_ERR "pio-handle not available \n");
  143. return -1;
  144. }
  145. pio = of_find_node_by_phandle(*ph);
  146. pio_map = of_get_property(pio, "pio-map", &pio_map_len);
  147. if (pio_map == NULL) {
  148. printk(KERN_ERR "pio-map is not set! \n");
  149. return -1;
  150. }
  151. pio_map_len /= sizeof(unsigned int);
  152. if ((pio_map_len % 6) != 0) {
  153. printk(KERN_ERR "pio-map format wrong! \n");
  154. return -1;
  155. }
  156. while (pio_map_len > 0) {
  157. par_io_config_pin((u8) pio_map[0], (u8) pio_map[1],
  158. (int) pio_map[2], (int) pio_map[3],
  159. (int) pio_map[4], (int) pio_map[5]);
  160. pio_map += 6;
  161. pio_map_len -= 6;
  162. }
  163. of_node_put(pio);
  164. return 0;
  165. }
  166. EXPORT_SYMBOL(par_io_of_config);
  167. #ifdef DEBUG
  168. static void dump_par_io(void)
  169. {
  170. unsigned int i;
  171. printk(KERN_INFO "%s: par_io=%p\n", __func__, par_io);
  172. for (i = 0; i < num_par_io_ports; i++) {
  173. printk(KERN_INFO " cpodr[%u]=%08x\n", i,
  174. in_be32(&par_io[i].cpodr));
  175. printk(KERN_INFO " cpdata[%u]=%08x\n", i,
  176. in_be32(&par_io[i].cpdata));
  177. printk(KERN_INFO " cpdir1[%u]=%08x\n", i,
  178. in_be32(&par_io[i].cpdir1));
  179. printk(KERN_INFO " cpdir2[%u]=%08x\n", i,
  180. in_be32(&par_io[i].cpdir2));
  181. printk(KERN_INFO " cppar1[%u]=%08x\n", i,
  182. in_be32(&par_io[i].cppar1));
  183. printk(KERN_INFO " cppar2[%u]=%08x\n", i,
  184. in_be32(&par_io[i].cppar2));
  185. }
  186. }
  187. EXPORT_SYMBOL(dump_par_io);
  188. #endif /* DEBUG */