qe_io.c 5.9 KB

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