cpm2.c 8.5 KB

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