cpm2_common.c 7.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309
  1. /*
  2. * General Purpose functions for the global management of the
  3. * 8260 Communication Processor Module.
  4. * Copyright (c) 1999-2001 Dan Malek <dan@embeddedalley.com>
  5. * Copyright (c) 2000 MontaVista Software, Inc (source@mvista.com)
  6. * 2.3.99 Updates
  7. *
  8. * 2006 (c) MontaVista Software, Inc.
  9. * Vitaly Bordug <vbordug@ru.mvista.com>
  10. * Merged to arch/powerpc from arch/ppc/syslib/cpm2_common.c
  11. *
  12. * This file is licensed under the terms of the GNU General Public License
  13. * version 2. This program is licensed "as is" without any warranty of any
  14. * kind, whether express or implied.
  15. */
  16. /*
  17. *
  18. * In addition to the individual control of the communication
  19. * channels, there are a few functions that globally affect the
  20. * communication processor.
  21. *
  22. * Buffer descriptors must be allocated from the dual ported memory
  23. * space. The allocator for that is here. When the communication
  24. * process is reset, we reclaim the memory available. There is
  25. * currently no deallocator for this memory.
  26. */
  27. #include <linux/errno.h>
  28. #include <linux/sched.h>
  29. #include <linux/kernel.h>
  30. #include <linux/param.h>
  31. #include <linux/string.h>
  32. #include <linux/mm.h>
  33. #include <linux/interrupt.h>
  34. #include <linux/module.h>
  35. #include <asm/io.h>
  36. #include <asm/irq.h>
  37. #include <asm/mpc8260.h>
  38. #include <asm/page.h>
  39. #include <asm/pgtable.h>
  40. #include <asm/cpm2.h>
  41. #include <asm/rheap.h>
  42. #include <asm/fs_pd.h>
  43. #include <sysdev/fsl_soc.h>
  44. static void cpm2_dpinit(void);
  45. cpm_cpm2_t *cpmp; /* Pointer to comm processor space */
  46. /* We allocate this here because it is used almost exclusively for
  47. * the communication processor devices.
  48. */
  49. cpm2_map_t *cpm2_immr;
  50. intctl_cpm2_t *cpm2_intctl;
  51. #define CPM_MAP_SIZE (0x40000) /* 256k - the PQ3 reserve this amount
  52. of space for CPM as it is larger
  53. than on PQ2 */
  54. void
  55. cpm2_reset(void)
  56. {
  57. cpm2_immr = (cpm2_map_t *)ioremap(CPM_MAP_ADDR, CPM_MAP_SIZE);
  58. cpm2_intctl = cpm2_map(im_intctl);
  59. /* Reclaim the DP memory for our use.
  60. */
  61. cpm2_dpinit();
  62. /* Tell everyone where the comm processor resides.
  63. */
  64. cpmp = &cpm2_immr->im_cpm;
  65. }
  66. /* Set a baud rate generator. This needs lots of work. There are
  67. * eight BRGs, which can be connected to the CPM channels or output
  68. * as clocks. The BRGs are in two different block of internal
  69. * memory mapped space.
  70. * The baud rate clock is the system clock divided by something.
  71. * It was set up long ago during the initial boot phase and is
  72. * is given to us.
  73. * Baud rate clocks are zero-based in the driver code (as that maps
  74. * to port numbers). Documentation uses 1-based numbering.
  75. */
  76. #define BRG_INT_CLK (get_brgfreq())
  77. #define BRG_UART_CLK (BRG_INT_CLK/16)
  78. /* This function is used by UARTS, or anything else that uses a 16x
  79. * oversampled clock.
  80. */
  81. void
  82. cpm_setbrg(uint brg, uint rate)
  83. {
  84. volatile uint *bp;
  85. /* This is good enough to get SMCs running.....
  86. */
  87. if (brg < 4) {
  88. bp = cpm2_map_size(im_brgc1, 16);
  89. } else {
  90. bp = cpm2_map_size(im_brgc5, 16);
  91. brg -= 4;
  92. }
  93. bp += brg;
  94. *bp = ((BRG_UART_CLK / rate) << 1) | CPM_BRG_EN;
  95. cpm2_unmap(bp);
  96. }
  97. /* This function is used to set high speed synchronous baud rate
  98. * clocks.
  99. */
  100. void
  101. cpm2_fastbrg(uint brg, uint rate, int div16)
  102. {
  103. volatile uint *bp;
  104. if (brg < 4) {
  105. bp = cpm2_map_size(im_brgc1, 16);
  106. }
  107. else {
  108. bp = cpm2_map_size(im_brgc5, 16);
  109. brg -= 4;
  110. }
  111. bp += brg;
  112. *bp = ((BRG_INT_CLK / rate) << 1) | CPM_BRG_EN;
  113. if (div16)
  114. *bp |= CPM_BRG_DIV16;
  115. cpm2_unmap(bp);
  116. }
  117. int cpm2_clk_setup(enum cpm_clk_target target, int clock, int mode)
  118. {
  119. int ret = 0;
  120. int shift;
  121. int i, bits = 0;
  122. cpmux_t *im_cpmux;
  123. u32 *reg;
  124. u32 mask = 7;
  125. u8 clk_map [24][3] = {
  126. {CPM_CLK_FCC1, CPM_BRG5, 0},
  127. {CPM_CLK_FCC1, CPM_BRG6, 1},
  128. {CPM_CLK_FCC1, CPM_BRG7, 2},
  129. {CPM_CLK_FCC1, CPM_BRG8, 3},
  130. {CPM_CLK_FCC1, CPM_CLK9, 4},
  131. {CPM_CLK_FCC1, CPM_CLK10, 5},
  132. {CPM_CLK_FCC1, CPM_CLK11, 6},
  133. {CPM_CLK_FCC1, CPM_CLK12, 7},
  134. {CPM_CLK_FCC2, CPM_BRG5, 0},
  135. {CPM_CLK_FCC2, CPM_BRG6, 1},
  136. {CPM_CLK_FCC2, CPM_BRG7, 2},
  137. {CPM_CLK_FCC2, CPM_BRG8, 3},
  138. {CPM_CLK_FCC2, CPM_CLK13, 4},
  139. {CPM_CLK_FCC2, CPM_CLK14, 5},
  140. {CPM_CLK_FCC2, CPM_CLK15, 6},
  141. {CPM_CLK_FCC2, CPM_CLK16, 7},
  142. {CPM_CLK_FCC3, CPM_BRG5, 0},
  143. {CPM_CLK_FCC3, CPM_BRG6, 1},
  144. {CPM_CLK_FCC3, CPM_BRG7, 2},
  145. {CPM_CLK_FCC3, CPM_BRG8, 3},
  146. {CPM_CLK_FCC3, CPM_CLK13, 4},
  147. {CPM_CLK_FCC3, CPM_CLK14, 5},
  148. {CPM_CLK_FCC3, CPM_CLK15, 6},
  149. {CPM_CLK_FCC3, CPM_CLK16, 7}
  150. };
  151. im_cpmux = cpm2_map(im_cpmux);
  152. switch (target) {
  153. case CPM_CLK_SCC1:
  154. reg = &im_cpmux->cmx_scr;
  155. shift = 24;
  156. case CPM_CLK_SCC2:
  157. reg = &im_cpmux->cmx_scr;
  158. shift = 16;
  159. break;
  160. case CPM_CLK_SCC3:
  161. reg = &im_cpmux->cmx_scr;
  162. shift = 8;
  163. break;
  164. case CPM_CLK_SCC4:
  165. reg = &im_cpmux->cmx_scr;
  166. shift = 0;
  167. break;
  168. case CPM_CLK_FCC1:
  169. reg = &im_cpmux->cmx_fcr;
  170. shift = 24;
  171. break;
  172. case CPM_CLK_FCC2:
  173. reg = &im_cpmux->cmx_fcr;
  174. shift = 16;
  175. break;
  176. case CPM_CLK_FCC3:
  177. reg = &im_cpmux->cmx_fcr;
  178. shift = 8;
  179. break;
  180. default:
  181. printk(KERN_ERR "cpm2_clock_setup: invalid clock target\n");
  182. return -EINVAL;
  183. }
  184. if (mode == CPM_CLK_RX)
  185. shift +=3;
  186. for (i=0; i<24; i++) {
  187. if (clk_map[i][0] == target && clk_map[i][1] == clock) {
  188. bits = clk_map[i][2];
  189. break;
  190. }
  191. }
  192. if (i == sizeof(clk_map)/3)
  193. ret = -EINVAL;
  194. bits <<= shift;
  195. mask <<= shift;
  196. out_be32(reg, (in_be32(reg) & ~mask) | bits);
  197. cpm2_unmap(im_cpmux);
  198. return ret;
  199. }
  200. /*
  201. * dpalloc / dpfree bits.
  202. */
  203. static spinlock_t cpm_dpmem_lock;
  204. /* 16 blocks should be enough to satisfy all requests
  205. * until the memory subsystem goes up... */
  206. static rh_block_t cpm_boot_dpmem_rh_block[16];
  207. static rh_info_t cpm_dpmem_info;
  208. static u8* im_dprambase;
  209. static void cpm2_dpinit(void)
  210. {
  211. spin_lock_init(&cpm_dpmem_lock);
  212. im_dprambase = ioremap(CPM_MAP_ADDR, CPM_DATAONLY_BASE + CPM_DATAONLY_SIZE);
  213. /* initialize the info header */
  214. rh_init(&cpm_dpmem_info, 1,
  215. sizeof(cpm_boot_dpmem_rh_block) /
  216. sizeof(cpm_boot_dpmem_rh_block[0]),
  217. cpm_boot_dpmem_rh_block);
  218. /* Attach the usable dpmem area */
  219. /* XXX: This is actually crap. CPM_DATAONLY_BASE and
  220. * CPM_DATAONLY_SIZE is only a subset of the available dpram. It
  221. * varies with the processor and the microcode patches activated.
  222. * But the following should be at least safe.
  223. */
  224. rh_attach_region(&cpm_dpmem_info, (void *)CPM_DATAONLY_BASE,
  225. CPM_DATAONLY_SIZE);
  226. }
  227. /* This function returns an index into the DPRAM area.
  228. */
  229. uint cpm_dpalloc(uint size, uint align)
  230. {
  231. void *start;
  232. unsigned long flags;
  233. spin_lock_irqsave(&cpm_dpmem_lock, flags);
  234. cpm_dpmem_info.alignment = align;
  235. start = rh_alloc(&cpm_dpmem_info, size, "commproc");
  236. spin_unlock_irqrestore(&cpm_dpmem_lock, flags);
  237. return (uint)start;
  238. }
  239. EXPORT_SYMBOL(cpm_dpalloc);
  240. int cpm_dpfree(uint offset)
  241. {
  242. int ret;
  243. unsigned long flags;
  244. spin_lock_irqsave(&cpm_dpmem_lock, flags);
  245. ret = rh_free(&cpm_dpmem_info, (void *)offset);
  246. spin_unlock_irqrestore(&cpm_dpmem_lock, flags);
  247. return ret;
  248. }
  249. EXPORT_SYMBOL(cpm_dpfree);
  250. /* not sure if this is ever needed */
  251. uint cpm_dpalloc_fixed(uint offset, uint size, uint align)
  252. {
  253. void *start;
  254. unsigned long flags;
  255. spin_lock_irqsave(&cpm_dpmem_lock, flags);
  256. cpm_dpmem_info.alignment = align;
  257. start = rh_alloc_fixed(&cpm_dpmem_info, (void *)offset, size, "commproc");
  258. spin_unlock_irqrestore(&cpm_dpmem_lock, flags);
  259. return (uint)start;
  260. }
  261. EXPORT_SYMBOL(cpm_dpalloc_fixed);
  262. void cpm_dpdump(void)
  263. {
  264. rh_dump(&cpm_dpmem_info);
  265. }
  266. EXPORT_SYMBOL(cpm_dpdump);
  267. void *cpm_dpram_addr(uint offset)
  268. {
  269. return (void *)(im_dprambase + offset);
  270. }
  271. EXPORT_SYMBOL(cpm_dpram_addr);