cpm2.c 8.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379
  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 <linux/of.h>
  36. #include <asm/io.h>
  37. #include <asm/irq.h>
  38. #include <asm/mpc8260.h>
  39. #include <asm/page.h>
  40. #include <asm/pgtable.h>
  41. #include <asm/cpm2.h>
  42. #include <asm/rheap.h>
  43. #include <asm/fs_pd.h>
  44. #include <sysdev/fsl_soc.h>
  45. cpm_cpm2_t __iomem *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 __iomem *cpm2_immr;
  50. #define CPM_MAP_SIZE (0x40000) /* 256k - the PQ3 reserve this amount
  51. of space for CPM as it is larger
  52. than on PQ2 */
  53. void __init cpm2_reset(void)
  54. {
  55. #ifdef CONFIG_PPC_85xx
  56. cpm2_immr = ioremap(CPM_MAP_ADDR, CPM_MAP_SIZE);
  57. #else
  58. cpm2_immr = ioremap(get_immrbase(), CPM_MAP_SIZE);
  59. #endif
  60. /* Reclaim the DP memory for our use.
  61. */
  62. cpm_muram_init();
  63. /* Tell everyone where the comm processor resides.
  64. */
  65. cpmp = &cpm2_immr->im_cpm;
  66. #ifndef CONFIG_PPC_EARLY_DEBUG_CPM
  67. /* Reset the CPM.
  68. */
  69. cpm_command(CPM_CR_RST, 0);
  70. #endif
  71. }
  72. static DEFINE_SPINLOCK(cmd_lock);
  73. #define MAX_CR_CMD_LOOPS 10000
  74. int cpm_command(u32 command, u8 opcode)
  75. {
  76. int i, ret;
  77. unsigned long flags;
  78. spin_lock_irqsave(&cmd_lock, flags);
  79. ret = 0;
  80. out_be32(&cpmp->cp_cpcr, command | opcode | CPM_CR_FLG);
  81. for (i = 0; i < MAX_CR_CMD_LOOPS; i++)
  82. if ((in_be32(&cpmp->cp_cpcr) & CPM_CR_FLG) == 0)
  83. goto out;
  84. printk(KERN_ERR "%s(): Not able to issue CPM command\n", __func__);
  85. ret = -EIO;
  86. out:
  87. spin_unlock_irqrestore(&cmd_lock, flags);
  88. return ret;
  89. }
  90. EXPORT_SYMBOL(cpm_command);
  91. /* Set a baud rate generator. This needs lots of work. There are
  92. * eight BRGs, which can be connected to the CPM channels or output
  93. * as clocks. The BRGs are in two different block of internal
  94. * memory mapped space.
  95. * The baud rate clock is the system clock divided by something.
  96. * It was set up long ago during the initial boot phase and is
  97. * is given to us.
  98. * Baud rate clocks are zero-based in the driver code (as that maps
  99. * to port numbers). Documentation uses 1-based numbering.
  100. */
  101. #define BRG_INT_CLK (get_brgfreq())
  102. #define BRG_UART_CLK (BRG_INT_CLK/16)
  103. /* This function is used by UARTS, or anything else that uses a 16x
  104. * oversampled clock.
  105. */
  106. void
  107. cpm_setbrg(uint brg, uint rate)
  108. {
  109. u32 __iomem *bp;
  110. /* This is good enough to get SMCs running.....
  111. */
  112. if (brg < 4) {
  113. bp = cpm2_map_size(im_brgc1, 16);
  114. } else {
  115. bp = cpm2_map_size(im_brgc5, 16);
  116. brg -= 4;
  117. }
  118. bp += brg;
  119. out_be32(bp, (((BRG_UART_CLK / rate) - 1) << 1) | CPM_BRG_EN);
  120. cpm2_unmap(bp);
  121. }
  122. /* This function is used to set high speed synchronous baud rate
  123. * clocks.
  124. */
  125. void
  126. cpm2_fastbrg(uint brg, uint rate, int div16)
  127. {
  128. u32 __iomem *bp;
  129. u32 val;
  130. if (brg < 4) {
  131. bp = cpm2_map_size(im_brgc1, 16);
  132. } else {
  133. bp = cpm2_map_size(im_brgc5, 16);
  134. brg -= 4;
  135. }
  136. bp += brg;
  137. val = ((BRG_INT_CLK / rate) << 1) | CPM_BRG_EN;
  138. if (div16)
  139. val |= CPM_BRG_DIV16;
  140. out_be32(bp, val);
  141. cpm2_unmap(bp);
  142. }
  143. int cpm2_clk_setup(enum cpm_clk_target target, int clock, int mode)
  144. {
  145. int ret = 0;
  146. int shift;
  147. int i, bits = 0;
  148. cpmux_t __iomem *im_cpmux;
  149. u32 __iomem *reg;
  150. u32 mask = 7;
  151. u8 clk_map[][3] = {
  152. {CPM_CLK_FCC1, CPM_BRG5, 0},
  153. {CPM_CLK_FCC1, CPM_BRG6, 1},
  154. {CPM_CLK_FCC1, CPM_BRG7, 2},
  155. {CPM_CLK_FCC1, CPM_BRG8, 3},
  156. {CPM_CLK_FCC1, CPM_CLK9, 4},
  157. {CPM_CLK_FCC1, CPM_CLK10, 5},
  158. {CPM_CLK_FCC1, CPM_CLK11, 6},
  159. {CPM_CLK_FCC1, CPM_CLK12, 7},
  160. {CPM_CLK_FCC2, CPM_BRG5, 0},
  161. {CPM_CLK_FCC2, CPM_BRG6, 1},
  162. {CPM_CLK_FCC2, CPM_BRG7, 2},
  163. {CPM_CLK_FCC2, CPM_BRG8, 3},
  164. {CPM_CLK_FCC2, CPM_CLK13, 4},
  165. {CPM_CLK_FCC2, CPM_CLK14, 5},
  166. {CPM_CLK_FCC2, CPM_CLK15, 6},
  167. {CPM_CLK_FCC2, CPM_CLK16, 7},
  168. {CPM_CLK_FCC3, CPM_BRG5, 0},
  169. {CPM_CLK_FCC3, CPM_BRG6, 1},
  170. {CPM_CLK_FCC3, CPM_BRG7, 2},
  171. {CPM_CLK_FCC3, CPM_BRG8, 3},
  172. {CPM_CLK_FCC3, CPM_CLK13, 4},
  173. {CPM_CLK_FCC3, CPM_CLK14, 5},
  174. {CPM_CLK_FCC3, CPM_CLK15, 6},
  175. {CPM_CLK_FCC3, CPM_CLK16, 7},
  176. {CPM_CLK_SCC1, CPM_BRG1, 0},
  177. {CPM_CLK_SCC1, CPM_BRG2, 1},
  178. {CPM_CLK_SCC1, CPM_BRG3, 2},
  179. {CPM_CLK_SCC1, CPM_BRG4, 3},
  180. {CPM_CLK_SCC1, CPM_CLK11, 4},
  181. {CPM_CLK_SCC1, CPM_CLK12, 5},
  182. {CPM_CLK_SCC1, CPM_CLK3, 6},
  183. {CPM_CLK_SCC1, CPM_CLK4, 7},
  184. {CPM_CLK_SCC2, CPM_BRG1, 0},
  185. {CPM_CLK_SCC2, CPM_BRG2, 1},
  186. {CPM_CLK_SCC2, CPM_BRG3, 2},
  187. {CPM_CLK_SCC2, CPM_BRG4, 3},
  188. {CPM_CLK_SCC2, CPM_CLK11, 4},
  189. {CPM_CLK_SCC2, CPM_CLK12, 5},
  190. {CPM_CLK_SCC2, CPM_CLK3, 6},
  191. {CPM_CLK_SCC2, CPM_CLK4, 7},
  192. {CPM_CLK_SCC3, CPM_BRG1, 0},
  193. {CPM_CLK_SCC3, CPM_BRG2, 1},
  194. {CPM_CLK_SCC3, CPM_BRG3, 2},
  195. {CPM_CLK_SCC3, CPM_BRG4, 3},
  196. {CPM_CLK_SCC3, CPM_CLK5, 4},
  197. {CPM_CLK_SCC3, CPM_CLK6, 5},
  198. {CPM_CLK_SCC3, CPM_CLK7, 6},
  199. {CPM_CLK_SCC3, CPM_CLK8, 7},
  200. {CPM_CLK_SCC4, CPM_BRG1, 0},
  201. {CPM_CLK_SCC4, CPM_BRG2, 1},
  202. {CPM_CLK_SCC4, CPM_BRG3, 2},
  203. {CPM_CLK_SCC4, CPM_BRG4, 3},
  204. {CPM_CLK_SCC4, CPM_CLK5, 4},
  205. {CPM_CLK_SCC4, CPM_CLK6, 5},
  206. {CPM_CLK_SCC4, CPM_CLK7, 6},
  207. {CPM_CLK_SCC4, CPM_CLK8, 7},
  208. };
  209. im_cpmux = cpm2_map(im_cpmux);
  210. switch (target) {
  211. case CPM_CLK_SCC1:
  212. reg = &im_cpmux->cmx_scr;
  213. shift = 24;
  214. break;
  215. case CPM_CLK_SCC2:
  216. reg = &im_cpmux->cmx_scr;
  217. shift = 16;
  218. break;
  219. case CPM_CLK_SCC3:
  220. reg = &im_cpmux->cmx_scr;
  221. shift = 8;
  222. break;
  223. case CPM_CLK_SCC4:
  224. reg = &im_cpmux->cmx_scr;
  225. shift = 0;
  226. break;
  227. case CPM_CLK_FCC1:
  228. reg = &im_cpmux->cmx_fcr;
  229. shift = 24;
  230. break;
  231. case CPM_CLK_FCC2:
  232. reg = &im_cpmux->cmx_fcr;
  233. shift = 16;
  234. break;
  235. case CPM_CLK_FCC3:
  236. reg = &im_cpmux->cmx_fcr;
  237. shift = 8;
  238. break;
  239. default:
  240. printk(KERN_ERR "cpm2_clock_setup: invalid clock target\n");
  241. return -EINVAL;
  242. }
  243. if (mode == CPM_CLK_RX)
  244. shift += 3;
  245. for (i = 0; i < ARRAY_SIZE(clk_map); i++) {
  246. if (clk_map[i][0] == target && clk_map[i][1] == clock) {
  247. bits = clk_map[i][2];
  248. break;
  249. }
  250. }
  251. if (i == ARRAY_SIZE(clk_map))
  252. ret = -EINVAL;
  253. bits <<= shift;
  254. mask <<= shift;
  255. out_be32(reg, (in_be32(reg) & ~mask) | bits);
  256. cpm2_unmap(im_cpmux);
  257. return ret;
  258. }
  259. int cpm2_smc_clk_setup(enum cpm_clk_target target, int clock)
  260. {
  261. int ret = 0;
  262. int shift;
  263. int i, bits = 0;
  264. cpmux_t __iomem *im_cpmux;
  265. u8 __iomem *reg;
  266. u8 mask = 3;
  267. u8 clk_map[][3] = {
  268. {CPM_CLK_SMC1, CPM_BRG1, 0},
  269. {CPM_CLK_SMC1, CPM_BRG7, 1},
  270. {CPM_CLK_SMC1, CPM_CLK7, 2},
  271. {CPM_CLK_SMC1, CPM_CLK9, 3},
  272. {CPM_CLK_SMC2, CPM_BRG2, 0},
  273. {CPM_CLK_SMC2, CPM_BRG8, 1},
  274. {CPM_CLK_SMC2, CPM_CLK4, 2},
  275. {CPM_CLK_SMC2, CPM_CLK15, 3},
  276. };
  277. im_cpmux = cpm2_map(im_cpmux);
  278. switch (target) {
  279. case CPM_CLK_SMC1:
  280. reg = &im_cpmux->cmx_smr;
  281. mask = 3;
  282. shift = 4;
  283. break;
  284. case CPM_CLK_SMC2:
  285. reg = &im_cpmux->cmx_smr;
  286. mask = 3;
  287. shift = 0;
  288. break;
  289. default:
  290. printk(KERN_ERR "cpm2_smc_clock_setup: invalid clock target\n");
  291. return -EINVAL;
  292. }
  293. for (i = 0; i < ARRAY_SIZE(clk_map); i++) {
  294. if (clk_map[i][0] == target && clk_map[i][1] == clock) {
  295. bits = clk_map[i][2];
  296. break;
  297. }
  298. }
  299. if (i == ARRAY_SIZE(clk_map))
  300. ret = -EINVAL;
  301. bits <<= shift;
  302. mask <<= shift;
  303. out_8(reg, (in_8(reg) & ~mask) | bits);
  304. cpm2_unmap(im_cpmux);
  305. return ret;
  306. }
  307. struct cpm2_ioports {
  308. u32 dir, par, sor, odr, dat;
  309. u32 res[3];
  310. };
  311. void cpm2_set_pin(int port, int pin, int flags)
  312. {
  313. struct cpm2_ioports __iomem *iop =
  314. (struct cpm2_ioports __iomem *)&cpm2_immr->im_ioport;
  315. pin = 1 << (31 - pin);
  316. if (flags & CPM_PIN_OUTPUT)
  317. setbits32(&iop[port].dir, pin);
  318. else
  319. clrbits32(&iop[port].dir, pin);
  320. if (!(flags & CPM_PIN_GPIO))
  321. setbits32(&iop[port].par, pin);
  322. else
  323. clrbits32(&iop[port].par, pin);
  324. if (flags & CPM_PIN_SECONDARY)
  325. setbits32(&iop[port].sor, pin);
  326. else
  327. clrbits32(&iop[port].sor, pin);
  328. if (flags & CPM_PIN_OPENDRAIN)
  329. setbits32(&iop[port].odr, pin);
  330. else
  331. clrbits32(&iop[port].odr, pin);
  332. }