mfp.c 7.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286
  1. /*
  2. * linux/arch/arm/plat-pxa/mfp.c
  3. *
  4. * Multi-Function Pin Support
  5. *
  6. * Copyright (C) 2007 Marvell Internation Ltd.
  7. *
  8. * 2007-08-21: eric miao <eric.miao@marvell.com>
  9. * initial version
  10. *
  11. * This program is free software; you can redistribute it and/or modify
  12. * it under the terms of the GNU General Public License version 2 as
  13. * published by the Free Software Foundation.
  14. */
  15. #include <linux/module.h>
  16. #include <linux/kernel.h>
  17. #include <linux/init.h>
  18. #include <linux/io.h>
  19. #include <plat/mfp.h>
  20. #define MFPR_SIZE (PAGE_SIZE)
  21. /* MFPR register bit definitions */
  22. #define MFPR_PULL_SEL (0x1 << 15)
  23. #define MFPR_PULLUP_EN (0x1 << 14)
  24. #define MFPR_PULLDOWN_EN (0x1 << 13)
  25. #define MFPR_SLEEP_SEL (0x1 << 9)
  26. #define MFPR_SLEEP_OE_N (0x1 << 7)
  27. #define MFPR_EDGE_CLEAR (0x1 << 6)
  28. #define MFPR_EDGE_FALL_EN (0x1 << 5)
  29. #define MFPR_EDGE_RISE_EN (0x1 << 4)
  30. #define MFPR_SLEEP_DATA(x) ((x) << 8)
  31. #define MFPR_DRIVE(x) (((x) & 0x7) << 10)
  32. #define MFPR_AF_SEL(x) (((x) & 0x7) << 0)
  33. #define MFPR_EDGE_NONE (0)
  34. #define MFPR_EDGE_RISE (MFPR_EDGE_RISE_EN)
  35. #define MFPR_EDGE_FALL (MFPR_EDGE_FALL_EN)
  36. #define MFPR_EDGE_BOTH (MFPR_EDGE_RISE | MFPR_EDGE_FALL)
  37. /*
  38. * Table that determines the low power modes outputs, with actual settings
  39. * used in parentheses for don't-care values. Except for the float output,
  40. * the configured driven and pulled levels match, so if there is a need for
  41. * non-LPM pulled output, the same configuration could probably be used.
  42. *
  43. * Output value sleep_oe_n sleep_data pullup_en pulldown_en pull_sel
  44. * (bit 7) (bit 8) (bit 14) (bit 13) (bit 15)
  45. *
  46. * Input 0 X(0) X(0) X(0) 0
  47. * Drive 0 0 0 0 X(1) 0
  48. * Drive 1 0 1 X(1) 0 0
  49. * Pull hi (1) 1 X(1) 1 0 0
  50. * Pull lo (0) 1 X(0) 0 1 0
  51. * Z (float) 1 X(0) 0 0 0
  52. */
  53. #define MFPR_LPM_INPUT (0)
  54. #define MFPR_LPM_DRIVE_LOW (MFPR_SLEEP_DATA(0) | MFPR_PULLDOWN_EN)
  55. #define MFPR_LPM_DRIVE_HIGH (MFPR_SLEEP_DATA(1) | MFPR_PULLUP_EN)
  56. #define MFPR_LPM_PULL_LOW (MFPR_LPM_DRIVE_LOW | MFPR_SLEEP_OE_N)
  57. #define MFPR_LPM_PULL_HIGH (MFPR_LPM_DRIVE_HIGH | MFPR_SLEEP_OE_N)
  58. #define MFPR_LPM_FLOAT (MFPR_SLEEP_OE_N)
  59. #define MFPR_LPM_MASK (0xe080)
  60. /*
  61. * The pullup and pulldown state of the MFP pin at run mode is by default
  62. * determined by the selected alternate function. In case that some buggy
  63. * devices need to override this default behavior, the definitions below
  64. * indicates the setting of corresponding MFPR bits
  65. *
  66. * Definition pull_sel pullup_en pulldown_en
  67. * MFPR_PULL_NONE 0 0 0
  68. * MFPR_PULL_LOW 1 0 1
  69. * MFPR_PULL_HIGH 1 1 0
  70. * MFPR_PULL_BOTH 1 1 1
  71. * MFPR_PULL_FLOAT 1 0 0
  72. */
  73. #define MFPR_PULL_NONE (0)
  74. #define MFPR_PULL_LOW (MFPR_PULL_SEL | MFPR_PULLDOWN_EN)
  75. #define MFPR_PULL_BOTH (MFPR_PULL_LOW | MFPR_PULLUP_EN)
  76. #define MFPR_PULL_HIGH (MFPR_PULL_SEL | MFPR_PULLUP_EN)
  77. #define MFPR_PULL_FLOAT (MFPR_PULL_SEL)
  78. /* mfp_spin_lock is used to ensure that MFP register configuration
  79. * (most likely a read-modify-write operation) is atomic, and that
  80. * mfp_table[] is consistent
  81. */
  82. static DEFINE_SPINLOCK(mfp_spin_lock);
  83. static void __iomem *mfpr_mmio_base;
  84. struct mfp_pin {
  85. unsigned long config; /* -1 for not configured */
  86. unsigned long mfpr_off; /* MFPRxx Register offset */
  87. unsigned long mfpr_run; /* Run-Mode Register Value */
  88. unsigned long mfpr_lpm; /* Low Power Mode Register Value */
  89. };
  90. static struct mfp_pin mfp_table[MFP_PIN_MAX];
  91. /* mapping of MFP_LPM_* definitions to MFPR_LPM_* register bits */
  92. static const unsigned long mfpr_lpm[] = {
  93. MFPR_LPM_INPUT,
  94. MFPR_LPM_DRIVE_LOW,
  95. MFPR_LPM_DRIVE_HIGH,
  96. MFPR_LPM_PULL_LOW,
  97. MFPR_LPM_PULL_HIGH,
  98. MFPR_LPM_FLOAT,
  99. MFPR_LPM_INPUT,
  100. };
  101. /* mapping of MFP_PULL_* definitions to MFPR_PULL_* register bits */
  102. static const unsigned long mfpr_pull[] = {
  103. MFPR_PULL_NONE,
  104. MFPR_PULL_LOW,
  105. MFPR_PULL_HIGH,
  106. MFPR_PULL_BOTH,
  107. MFPR_PULL_FLOAT,
  108. };
  109. /* mapping of MFP_LPM_EDGE_* definitions to MFPR_EDGE_* register bits */
  110. static const unsigned long mfpr_edge[] = {
  111. MFPR_EDGE_NONE,
  112. MFPR_EDGE_RISE,
  113. MFPR_EDGE_FALL,
  114. MFPR_EDGE_BOTH,
  115. };
  116. #define mfpr_readl(off) \
  117. __raw_readl(mfpr_mmio_base + (off))
  118. #define mfpr_writel(off, val) \
  119. __raw_writel(val, mfpr_mmio_base + (off))
  120. #define mfp_configured(p) ((p)->config != -1)
  121. /*
  122. * perform a read-back of any valid MFPR register to make sure the
  123. * previous writings are finished
  124. */
  125. static unsigned long mfpr_off_readback;
  126. #define mfpr_sync() (void)__raw_readl(mfpr_mmio_base + mfpr_off_readback)
  127. static inline void __mfp_config_run(struct mfp_pin *p)
  128. {
  129. if (mfp_configured(p))
  130. mfpr_writel(p->mfpr_off, p->mfpr_run);
  131. }
  132. static inline void __mfp_config_lpm(struct mfp_pin *p)
  133. {
  134. if (mfp_configured(p)) {
  135. unsigned long mfpr_clr = (p->mfpr_run & ~MFPR_EDGE_BOTH) | MFPR_EDGE_CLEAR;
  136. if (mfpr_clr != p->mfpr_run)
  137. mfpr_writel(p->mfpr_off, mfpr_clr);
  138. if (p->mfpr_lpm != mfpr_clr)
  139. mfpr_writel(p->mfpr_off, p->mfpr_lpm);
  140. }
  141. }
  142. void mfp_config(unsigned long *mfp_cfgs, int num)
  143. {
  144. unsigned long flags;
  145. int i;
  146. spin_lock_irqsave(&mfp_spin_lock, flags);
  147. for (i = 0; i < num; i++, mfp_cfgs++) {
  148. unsigned long tmp, c = *mfp_cfgs;
  149. struct mfp_pin *p;
  150. int pin, af, drv, lpm, edge, pull;
  151. pin = MFP_PIN(c);
  152. BUG_ON(pin >= MFP_PIN_MAX);
  153. p = &mfp_table[pin];
  154. af = MFP_AF(c);
  155. drv = MFP_DS(c);
  156. lpm = MFP_LPM_STATE(c);
  157. edge = MFP_LPM_EDGE(c);
  158. pull = MFP_PULL(c);
  159. /* run-mode pull settings will conflict with MFPR bits of
  160. * low power mode state, calculate mfpr_run and mfpr_lpm
  161. * individually if pull != MFP_PULL_NONE
  162. */
  163. tmp = MFPR_AF_SEL(af) | MFPR_DRIVE(drv);
  164. if (likely(pull == MFP_PULL_NONE)) {
  165. p->mfpr_run = tmp | mfpr_lpm[lpm] | mfpr_edge[edge];
  166. p->mfpr_lpm = p->mfpr_run;
  167. } else {
  168. p->mfpr_lpm = tmp | mfpr_lpm[lpm] | mfpr_edge[edge];
  169. p->mfpr_run = tmp | mfpr_pull[pull];
  170. }
  171. p->config = c; __mfp_config_run(p);
  172. }
  173. mfpr_sync();
  174. spin_unlock_irqrestore(&mfp_spin_lock, flags);
  175. }
  176. unsigned long mfp_read(int mfp)
  177. {
  178. unsigned long val, flags;
  179. BUG_ON(mfp < 0 || mfp >= MFP_PIN_MAX);
  180. spin_lock_irqsave(&mfp_spin_lock, flags);
  181. val = mfpr_readl(mfp_table[mfp].mfpr_off);
  182. spin_unlock_irqrestore(&mfp_spin_lock, flags);
  183. return val;
  184. }
  185. void mfp_write(int mfp, unsigned long val)
  186. {
  187. unsigned long flags;
  188. BUG_ON(mfp < 0 || mfp >= MFP_PIN_MAX);
  189. spin_lock_irqsave(&mfp_spin_lock, flags);
  190. mfpr_writel(mfp_table[mfp].mfpr_off, val);
  191. mfpr_sync();
  192. spin_unlock_irqrestore(&mfp_spin_lock, flags);
  193. }
  194. void __init mfp_init_base(void __iomem *mfpr_base)
  195. {
  196. int i;
  197. /* initialize the table with default - unconfigured */
  198. for (i = 0; i < ARRAY_SIZE(mfp_table); i++)
  199. mfp_table[i].config = -1;
  200. mfpr_mmio_base = mfpr_base;
  201. }
  202. void __init mfp_init_addr(struct mfp_addr_map *map)
  203. {
  204. struct mfp_addr_map *p;
  205. unsigned long offset, flags;
  206. int i;
  207. spin_lock_irqsave(&mfp_spin_lock, flags);
  208. /* mfp offset for readback */
  209. mfpr_off_readback = map[0].offset;
  210. for (p = map; p->start != MFP_PIN_INVALID; p++) {
  211. offset = p->offset;
  212. i = p->start;
  213. do {
  214. mfp_table[i].mfpr_off = offset;
  215. mfp_table[i].mfpr_run = 0;
  216. mfp_table[i].mfpr_lpm = 0;
  217. offset += 4; i++;
  218. } while ((i <= p->end) && (p->end != -1));
  219. }
  220. spin_unlock_irqrestore(&mfp_spin_lock, flags);
  221. }
  222. void mfp_config_lpm(void)
  223. {
  224. struct mfp_pin *p = &mfp_table[0];
  225. int pin;
  226. for (pin = 0; pin < ARRAY_SIZE(mfp_table); pin++, p++)
  227. __mfp_config_lpm(p);
  228. }
  229. void mfp_config_run(void)
  230. {
  231. struct mfp_pin *p = &mfp_table[0];
  232. int pin;
  233. for (pin = 0; pin < ARRAY_SIZE(mfp_table); pin++, p++)
  234. __mfp_config_run(p);
  235. }