qe_io.c 5.9 KB

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