pinmux.c 8.0 KB

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