qe_io.c 5.5 KB

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