cpm2.c 8.5 KB

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