pinmux.c 7.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310
  1. /*
  2. * Allocator for I/O pins. All pins are allocated to GPIO at bootup.
  3. * Unassigned pins and GPIO pins can be allocated to a fixed interface
  4. * or the I/O processor instead.
  5. *
  6. * Copyright (c) 2004-2007 Axis Communications AB.
  7. */
  8. #include <linux/init.h>
  9. #include <linux/errno.h>
  10. #include <linux/kernel.h>
  11. #include <linux/string.h>
  12. #include <linux/spinlock.h>
  13. #include <hwregs/reg_map.h>
  14. #include <hwregs/reg_rdwr.h>
  15. #include <pinmux.h>
  16. #include <hwregs/pinmux_defs.h>
  17. #undef DEBUG
  18. #define PORT_PINS 18
  19. #define PORTS 4
  20. static char pins[PORTS][PORT_PINS];
  21. static DEFINE_SPINLOCK(pinmux_lock);
  22. static void crisv32_pinmux_set(int port);
  23. int crisv32_pinmux_init(void)
  24. {
  25. static int initialized;
  26. if (!initialized) {
  27. reg_pinmux_rw_pa pa = REG_RD(pinmux, regi_pinmux, rw_pa);
  28. initialized = 1;
  29. REG_WR_INT(pinmux, regi_pinmux, rw_hwprot, 0);
  30. pa.pa0 = pa.pa1 = pa.pa2 = pa.pa3 =
  31. pa.pa4 = pa.pa5 = pa.pa6 = pa.pa7 = regk_pinmux_yes;
  32. REG_WR(pinmux, regi_pinmux, rw_pa, pa);
  33. crisv32_pinmux_alloc(PORT_B, 0, PORT_PINS - 1, pinmux_gpio);
  34. crisv32_pinmux_alloc(PORT_C, 0, PORT_PINS - 1, pinmux_gpio);
  35. crisv32_pinmux_alloc(PORT_D, 0, PORT_PINS - 1, pinmux_gpio);
  36. crisv32_pinmux_alloc(PORT_E, 0, PORT_PINS - 1, pinmux_gpio);
  37. }
  38. return 0;
  39. }
  40. int
  41. crisv32_pinmux_alloc(int port, int first_pin, int last_pin, enum pin_mode mode)
  42. {
  43. int i;
  44. unsigned long flags;
  45. crisv32_pinmux_init();
  46. if (port > PORTS || port < 0)
  47. return -EINVAL;
  48. spin_lock_irqsave(&pinmux_lock, flags);
  49. for (i = first_pin; i <= last_pin; i++) {
  50. if ((pins[port][i] != pinmux_none)
  51. && (pins[port][i] != pinmux_gpio)
  52. && (pins[port][i] != mode)) {
  53. spin_unlock_irqrestore(&pinmux_lock, flags);
  54. #ifdef DEBUG
  55. panic("Pinmux alloc failed!\n");
  56. #endif
  57. return -EPERM;
  58. }
  59. }
  60. for (i = first_pin; i <= last_pin; i++)
  61. pins[port][i] = mode;
  62. crisv32_pinmux_set(port);
  63. spin_unlock_irqrestore(&pinmux_lock, flags);
  64. return 0;
  65. }
  66. int crisv32_pinmux_alloc_fixed(enum fixed_function function)
  67. {
  68. int ret = -EINVAL;
  69. char saved[sizeof pins];
  70. unsigned long flags;
  71. spin_lock_irqsave(&pinmux_lock, flags);
  72. /* Save internal data for recovery */
  73. memcpy(saved, pins, sizeof pins);
  74. crisv32_pinmux_init(); /* Must be done before we read rw_hwprot */
  75. reg_pinmux_rw_hwprot hwprot = REG_RD(pinmux, regi_pinmux, rw_hwprot);
  76. switch (function) {
  77. case pinmux_ser1:
  78. ret = crisv32_pinmux_alloc(PORT_C, 4, 7, pinmux_fixed);
  79. hwprot.ser1 = regk_pinmux_yes;
  80. break;
  81. case pinmux_ser2:
  82. ret = crisv32_pinmux_alloc(PORT_C, 8, 11, pinmux_fixed);
  83. hwprot.ser2 = regk_pinmux_yes;
  84. break;
  85. case pinmux_ser3:
  86. ret = crisv32_pinmux_alloc(PORT_C, 12, 15, pinmux_fixed);
  87. hwprot.ser3 = regk_pinmux_yes;
  88. break;
  89. case pinmux_sser0:
  90. ret = crisv32_pinmux_alloc(PORT_C, 0, 3, pinmux_fixed);
  91. ret |= crisv32_pinmux_alloc(PORT_C, 16, 16, pinmux_fixed);
  92. hwprot.sser0 = regk_pinmux_yes;
  93. break;
  94. case pinmux_sser1:
  95. ret = crisv32_pinmux_alloc(PORT_D, 0, 4, pinmux_fixed);
  96. hwprot.sser1 = regk_pinmux_yes;
  97. break;
  98. case pinmux_ata0:
  99. ret = crisv32_pinmux_alloc(PORT_D, 5, 7, pinmux_fixed);
  100. ret |= crisv32_pinmux_alloc(PORT_D, 15, 17, pinmux_fixed);
  101. hwprot.ata0 = regk_pinmux_yes;
  102. break;
  103. case pinmux_ata1:
  104. ret = crisv32_pinmux_alloc(PORT_D, 0, 4, pinmux_fixed);
  105. ret |= crisv32_pinmux_alloc(PORT_E, 17, 17, pinmux_fixed);
  106. hwprot.ata1 = regk_pinmux_yes;
  107. break;
  108. case pinmux_ata2:
  109. ret = crisv32_pinmux_alloc(PORT_C, 11, 15, pinmux_fixed);
  110. ret |= crisv32_pinmux_alloc(PORT_E, 3, 3, pinmux_fixed);
  111. hwprot.ata2 = regk_pinmux_yes;
  112. break;
  113. case pinmux_ata3:
  114. ret = crisv32_pinmux_alloc(PORT_C, 8, 10, pinmux_fixed);
  115. ret |= crisv32_pinmux_alloc(PORT_C, 0, 2, pinmux_fixed);
  116. hwprot.ata2 = regk_pinmux_yes;
  117. break;
  118. case pinmux_ata:
  119. ret = crisv32_pinmux_alloc(PORT_B, 0, 15, pinmux_fixed);
  120. ret |= crisv32_pinmux_alloc(PORT_D, 8, 15, pinmux_fixed);
  121. hwprot.ata = regk_pinmux_yes;
  122. break;
  123. case pinmux_eth1:
  124. ret = crisv32_pinmux_alloc(PORT_E, 0, 17, pinmux_fixed);
  125. hwprot.eth1 = regk_pinmux_yes;
  126. hwprot.eth1_mgm = regk_pinmux_yes;
  127. break;
  128. case pinmux_timer:
  129. ret = crisv32_pinmux_alloc(PORT_C, 16, 16, pinmux_fixed);
  130. hwprot.timer = regk_pinmux_yes;
  131. spin_unlock_irqrestore(&pinmux_lock, flags);
  132. return ret;
  133. }
  134. if (!ret)
  135. REG_WR(pinmux, regi_pinmux, rw_hwprot, hwprot);
  136. else
  137. memcpy(pins, saved, sizeof pins);
  138. spin_unlock_irqrestore(&pinmux_lock, flags);
  139. return ret;
  140. }
  141. void crisv32_pinmux_set(int port)
  142. {
  143. int i;
  144. int gpio_val = 0;
  145. int iop_val = 0;
  146. for (i = 0; i < PORT_PINS; i++) {
  147. if (pins[port][i] == pinmux_gpio)
  148. gpio_val |= (1 << i);
  149. else if (pins[port][i] == pinmux_iop)
  150. iop_val |= (1 << i);
  151. }
  152. REG_WRITE(int, regi_pinmux + REG_RD_ADDR_pinmux_rw_pb_gio + 8 * port,
  153. gpio_val);
  154. REG_WRITE(int, regi_pinmux + REG_RD_ADDR_pinmux_rw_pb_iop + 8 * port,
  155. iop_val);
  156. #ifdef DEBUG
  157. crisv32_pinmux_dump();
  158. #endif
  159. }
  160. int crisv32_pinmux_dealloc(int port, int first_pin, int last_pin)
  161. {
  162. int i;
  163. unsigned long flags;
  164. crisv32_pinmux_init();
  165. if (port > PORTS || port < 0)
  166. return -EINVAL;
  167. spin_lock_irqsave(&pinmux_lock, flags);
  168. for (i = first_pin; i <= last_pin; i++)
  169. pins[port][i] = pinmux_none;
  170. crisv32_pinmux_set(port);
  171. spin_unlock_irqrestore(&pinmux_lock, flags);
  172. return 0;
  173. }
  174. int crisv32_pinmux_dealloc_fixed(enum fixed_function function)
  175. {
  176. int ret = -EINVAL;
  177. char saved[sizeof pins];
  178. unsigned long flags;
  179. spin_lock_irqsave(&pinmux_lock, flags);
  180. /* Save internal data for recovery */
  181. memcpy(saved, pins, sizeof pins);
  182. crisv32_pinmux_init(); /* Must be done before we read rw_hwprot */
  183. reg_pinmux_rw_hwprot hwprot = REG_RD(pinmux, regi_pinmux, rw_hwprot);
  184. switch (function) {
  185. case pinmux_ser1:
  186. ret = crisv32_pinmux_dealloc(PORT_C, 4, 7);
  187. hwprot.ser1 = regk_pinmux_no;
  188. break;
  189. case pinmux_ser2:
  190. ret = crisv32_pinmux_dealloc(PORT_C, 8, 11);
  191. hwprot.ser2 = regk_pinmux_no;
  192. break;
  193. case pinmux_ser3:
  194. ret = crisv32_pinmux_dealloc(PORT_C, 12, 15);
  195. hwprot.ser3 = regk_pinmux_no;
  196. break;
  197. case pinmux_sser0:
  198. ret = crisv32_pinmux_dealloc(PORT_C, 0, 3);
  199. ret |= crisv32_pinmux_dealloc(PORT_C, 16, 16);
  200. hwprot.sser0 = regk_pinmux_no;
  201. break;
  202. case pinmux_sser1:
  203. ret = crisv32_pinmux_dealloc(PORT_D, 0, 4);
  204. hwprot.sser1 = regk_pinmux_no;
  205. break;
  206. case pinmux_ata0:
  207. ret = crisv32_pinmux_dealloc(PORT_D, 5, 7);
  208. ret |= crisv32_pinmux_dealloc(PORT_D, 15, 17);
  209. hwprot.ata0 = regk_pinmux_no;
  210. break;
  211. case pinmux_ata1:
  212. ret = crisv32_pinmux_dealloc(PORT_D, 0, 4);
  213. ret |= crisv32_pinmux_dealloc(PORT_E, 17, 17);
  214. hwprot.ata1 = regk_pinmux_no;
  215. break;
  216. case pinmux_ata2:
  217. ret = crisv32_pinmux_dealloc(PORT_C, 11, 15);
  218. ret |= crisv32_pinmux_dealloc(PORT_E, 3, 3);
  219. hwprot.ata2 = regk_pinmux_no;
  220. break;
  221. case pinmux_ata3:
  222. ret = crisv32_pinmux_dealloc(PORT_C, 8, 10);
  223. ret |= crisv32_pinmux_dealloc(PORT_C, 0, 2);
  224. hwprot.ata2 = regk_pinmux_no;
  225. break;
  226. case pinmux_ata:
  227. ret = crisv32_pinmux_dealloc(PORT_B, 0, 15);
  228. ret |= crisv32_pinmux_dealloc(PORT_D, 8, 15);
  229. hwprot.ata = regk_pinmux_no;
  230. break;
  231. case pinmux_eth1:
  232. ret = crisv32_pinmux_dealloc(PORT_E, 0, 17);
  233. hwprot.eth1 = regk_pinmux_no;
  234. hwprot.eth1_mgm = regk_pinmux_no;
  235. break;
  236. case pinmux_timer:
  237. ret = crisv32_pinmux_dealloc(PORT_C, 16, 16);
  238. hwprot.timer = regk_pinmux_no;
  239. spin_unlock_irqrestore(&pinmux_lock, flags);
  240. return ret;
  241. }
  242. if (!ret)
  243. REG_WR(pinmux, regi_pinmux, rw_hwprot, hwprot);
  244. else
  245. memcpy(pins, saved, sizeof pins);
  246. spin_unlock_irqrestore(&pinmux_lock, flags);
  247. return ret;
  248. }
  249. void crisv32_pinmux_dump(void)
  250. {
  251. int i, j;
  252. crisv32_pinmux_init();
  253. for (i = 0; i < PORTS; i++) {
  254. printk(KERN_DEBUG "Port %c\n", 'B' + i);
  255. for (j = 0; j < PORT_PINS; j++)
  256. printk(KERN_DEBUG " Pin %d = %d\n", j, pins[i][j]);
  257. }
  258. }
  259. __initcall(crisv32_pinmux_init);