cpm2.c 8.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364
  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. void __cpm2_setbrg(uint brg, uint rate, uint clk, int div16, int src)
  102. {
  103. u32 __iomem *bp;
  104. u32 val;
  105. /* This is good enough to get SMCs running.....
  106. */
  107. if (brg < 4) {
  108. bp = cpm2_map_size(im_brgc1, 16);
  109. } else {
  110. bp = cpm2_map_size(im_brgc5, 16);
  111. brg -= 4;
  112. }
  113. bp += brg;
  114. val = (((clk / rate) - 1) << 1) | CPM_BRG_EN | src;
  115. if (div16)
  116. val |= CPM_BRG_DIV16;
  117. out_be32(bp, val);
  118. cpm2_unmap(bp);
  119. }
  120. EXPORT_SYMBOL(__cpm2_setbrg);
  121. int cpm2_clk_setup(enum cpm_clk_target target, int clock, int mode)
  122. {
  123. int ret = 0;
  124. int shift;
  125. int i, bits = 0;
  126. cpmux_t __iomem *im_cpmux;
  127. u32 __iomem *reg;
  128. u32 mask = 7;
  129. u8 clk_map[][3] = {
  130. {CPM_CLK_FCC1, CPM_BRG5, 0},
  131. {CPM_CLK_FCC1, CPM_BRG6, 1},
  132. {CPM_CLK_FCC1, CPM_BRG7, 2},
  133. {CPM_CLK_FCC1, CPM_BRG8, 3},
  134. {CPM_CLK_FCC1, CPM_CLK9, 4},
  135. {CPM_CLK_FCC1, CPM_CLK10, 5},
  136. {CPM_CLK_FCC1, CPM_CLK11, 6},
  137. {CPM_CLK_FCC1, CPM_CLK12, 7},
  138. {CPM_CLK_FCC2, CPM_BRG5, 0},
  139. {CPM_CLK_FCC2, CPM_BRG6, 1},
  140. {CPM_CLK_FCC2, CPM_BRG7, 2},
  141. {CPM_CLK_FCC2, CPM_BRG8, 3},
  142. {CPM_CLK_FCC2, CPM_CLK13, 4},
  143. {CPM_CLK_FCC2, CPM_CLK14, 5},
  144. {CPM_CLK_FCC2, CPM_CLK15, 6},
  145. {CPM_CLK_FCC2, CPM_CLK16, 7},
  146. {CPM_CLK_FCC3, CPM_BRG5, 0},
  147. {CPM_CLK_FCC3, CPM_BRG6, 1},
  148. {CPM_CLK_FCC3, CPM_BRG7, 2},
  149. {CPM_CLK_FCC3, CPM_BRG8, 3},
  150. {CPM_CLK_FCC3, CPM_CLK13, 4},
  151. {CPM_CLK_FCC3, CPM_CLK14, 5},
  152. {CPM_CLK_FCC3, CPM_CLK15, 6},
  153. {CPM_CLK_FCC3, CPM_CLK16, 7},
  154. {CPM_CLK_SCC1, CPM_BRG1, 0},
  155. {CPM_CLK_SCC1, CPM_BRG2, 1},
  156. {CPM_CLK_SCC1, CPM_BRG3, 2},
  157. {CPM_CLK_SCC1, CPM_BRG4, 3},
  158. {CPM_CLK_SCC1, CPM_CLK11, 4},
  159. {CPM_CLK_SCC1, CPM_CLK12, 5},
  160. {CPM_CLK_SCC1, CPM_CLK3, 6},
  161. {CPM_CLK_SCC1, CPM_CLK4, 7},
  162. {CPM_CLK_SCC2, CPM_BRG1, 0},
  163. {CPM_CLK_SCC2, CPM_BRG2, 1},
  164. {CPM_CLK_SCC2, CPM_BRG3, 2},
  165. {CPM_CLK_SCC2, CPM_BRG4, 3},
  166. {CPM_CLK_SCC2, CPM_CLK11, 4},
  167. {CPM_CLK_SCC2, CPM_CLK12, 5},
  168. {CPM_CLK_SCC2, CPM_CLK3, 6},
  169. {CPM_CLK_SCC2, CPM_CLK4, 7},
  170. {CPM_CLK_SCC3, CPM_BRG1, 0},
  171. {CPM_CLK_SCC3, CPM_BRG2, 1},
  172. {CPM_CLK_SCC3, CPM_BRG3, 2},
  173. {CPM_CLK_SCC3, CPM_BRG4, 3},
  174. {CPM_CLK_SCC3, CPM_CLK5, 4},
  175. {CPM_CLK_SCC3, CPM_CLK6, 5},
  176. {CPM_CLK_SCC3, CPM_CLK7, 6},
  177. {CPM_CLK_SCC3, CPM_CLK8, 7},
  178. {CPM_CLK_SCC4, CPM_BRG1, 0},
  179. {CPM_CLK_SCC4, CPM_BRG2, 1},
  180. {CPM_CLK_SCC4, CPM_BRG3, 2},
  181. {CPM_CLK_SCC4, CPM_BRG4, 3},
  182. {CPM_CLK_SCC4, CPM_CLK5, 4},
  183. {CPM_CLK_SCC4, CPM_CLK6, 5},
  184. {CPM_CLK_SCC4, CPM_CLK7, 6},
  185. {CPM_CLK_SCC4, CPM_CLK8, 7},
  186. };
  187. im_cpmux = cpm2_map(im_cpmux);
  188. switch (target) {
  189. case CPM_CLK_SCC1:
  190. reg = &im_cpmux->cmx_scr;
  191. shift = 24;
  192. break;
  193. case CPM_CLK_SCC2:
  194. reg = &im_cpmux->cmx_scr;
  195. shift = 16;
  196. break;
  197. case CPM_CLK_SCC3:
  198. reg = &im_cpmux->cmx_scr;
  199. shift = 8;
  200. break;
  201. case CPM_CLK_SCC4:
  202. reg = &im_cpmux->cmx_scr;
  203. shift = 0;
  204. break;
  205. case CPM_CLK_FCC1:
  206. reg = &im_cpmux->cmx_fcr;
  207. shift = 24;
  208. break;
  209. case CPM_CLK_FCC2:
  210. reg = &im_cpmux->cmx_fcr;
  211. shift = 16;
  212. break;
  213. case CPM_CLK_FCC3:
  214. reg = &im_cpmux->cmx_fcr;
  215. shift = 8;
  216. break;
  217. default:
  218. printk(KERN_ERR "cpm2_clock_setup: invalid clock target\n");
  219. return -EINVAL;
  220. }
  221. if (mode == CPM_CLK_RX)
  222. shift += 3;
  223. for (i = 0; i < ARRAY_SIZE(clk_map); i++) {
  224. if (clk_map[i][0] == target && clk_map[i][1] == clock) {
  225. bits = clk_map[i][2];
  226. break;
  227. }
  228. }
  229. if (i == ARRAY_SIZE(clk_map))
  230. ret = -EINVAL;
  231. bits <<= shift;
  232. mask <<= shift;
  233. out_be32(reg, (in_be32(reg) & ~mask) | bits);
  234. cpm2_unmap(im_cpmux);
  235. return ret;
  236. }
  237. int cpm2_smc_clk_setup(enum cpm_clk_target target, int clock)
  238. {
  239. int ret = 0;
  240. int shift;
  241. int i, bits = 0;
  242. cpmux_t __iomem *im_cpmux;
  243. u8 __iomem *reg;
  244. u8 mask = 3;
  245. u8 clk_map[][3] = {
  246. {CPM_CLK_SMC1, CPM_BRG1, 0},
  247. {CPM_CLK_SMC1, CPM_BRG7, 1},
  248. {CPM_CLK_SMC1, CPM_CLK7, 2},
  249. {CPM_CLK_SMC1, CPM_CLK9, 3},
  250. {CPM_CLK_SMC2, CPM_BRG2, 0},
  251. {CPM_CLK_SMC2, CPM_BRG8, 1},
  252. {CPM_CLK_SMC2, CPM_CLK4, 2},
  253. {CPM_CLK_SMC2, CPM_CLK15, 3},
  254. };
  255. im_cpmux = cpm2_map(im_cpmux);
  256. switch (target) {
  257. case CPM_CLK_SMC1:
  258. reg = &im_cpmux->cmx_smr;
  259. mask = 3;
  260. shift = 4;
  261. break;
  262. case CPM_CLK_SMC2:
  263. reg = &im_cpmux->cmx_smr;
  264. mask = 3;
  265. shift = 0;
  266. break;
  267. default:
  268. printk(KERN_ERR "cpm2_smc_clock_setup: invalid clock target\n");
  269. return -EINVAL;
  270. }
  271. for (i = 0; i < ARRAY_SIZE(clk_map); i++) {
  272. if (clk_map[i][0] == target && clk_map[i][1] == clock) {
  273. bits = clk_map[i][2];
  274. break;
  275. }
  276. }
  277. if (i == ARRAY_SIZE(clk_map))
  278. ret = -EINVAL;
  279. bits <<= shift;
  280. mask <<= shift;
  281. out_8(reg, (in_8(reg) & ~mask) | bits);
  282. cpm2_unmap(im_cpmux);
  283. return ret;
  284. }
  285. struct cpm2_ioports {
  286. u32 dir, par, sor, odr, dat;
  287. u32 res[3];
  288. };
  289. void cpm2_set_pin(int port, int pin, int flags)
  290. {
  291. struct cpm2_ioports __iomem *iop =
  292. (struct cpm2_ioports __iomem *)&cpm2_immr->im_ioport;
  293. pin = 1 << (31 - pin);
  294. if (flags & CPM_PIN_OUTPUT)
  295. setbits32(&iop[port].dir, pin);
  296. else
  297. clrbits32(&iop[port].dir, pin);
  298. if (!(flags & CPM_PIN_GPIO))
  299. setbits32(&iop[port].par, pin);
  300. else
  301. clrbits32(&iop[port].par, pin);
  302. if (flags & CPM_PIN_SECONDARY)
  303. setbits32(&iop[port].sor, pin);
  304. else
  305. clrbits32(&iop[port].sor, pin);
  306. if (flags & CPM_PIN_OPENDRAIN)
  307. setbits32(&iop[port].odr, pin);
  308. else
  309. clrbits32(&iop[port].odr, pin);
  310. }
  311. static int cpm_init_par_io(void)
  312. {
  313. struct device_node *np;
  314. for_each_compatible_node(np, NULL, "fsl,cpm2-pario-bank")
  315. cpm2_gpiochip_add32(np);
  316. return 0;
  317. }
  318. arch_initcall(cpm_init_par_io);