cpm2_common.c 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445
  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. #ifndef CONFIG_PPC_CPM_NEW_BINDING
  46. static void cpm2_dpinit(void);
  47. #endif
  48. cpm_cpm2_t __iomem *cpmp; /* Pointer to comm processor space */
  49. /* We allocate this here because it is used almost exclusively for
  50. * the communication processor devices.
  51. */
  52. cpm2_map_t __iomem *cpm2_immr;
  53. #define CPM_MAP_SIZE (0x40000) /* 256k - the PQ3 reserve this amount
  54. of space for CPM as it is larger
  55. than on PQ2 */
  56. void __init cpm2_reset(void)
  57. {
  58. #ifdef CONFIG_PPC_85xx
  59. cpm2_immr = ioremap(CPM_MAP_ADDR, CPM_MAP_SIZE);
  60. #else
  61. cpm2_immr = ioremap(get_immrbase(), CPM_MAP_SIZE);
  62. #endif
  63. /* Reclaim the DP memory for our use.
  64. */
  65. #ifdef CONFIG_PPC_CPM_NEW_BINDING
  66. cpm_muram_init();
  67. #else
  68. cpm2_dpinit();
  69. #endif
  70. /* Tell everyone where the comm processor resides.
  71. */
  72. cpmp = &cpm2_immr->im_cpm;
  73. }
  74. /* Set a baud rate generator. This needs lots of work. There are
  75. * eight BRGs, which can be connected to the CPM channels or output
  76. * as clocks. The BRGs are in two different block of internal
  77. * memory mapped space.
  78. * The baud rate clock is the system clock divided by something.
  79. * It was set up long ago during the initial boot phase and is
  80. * is given to us.
  81. * Baud rate clocks are zero-based in the driver code (as that maps
  82. * to port numbers). Documentation uses 1-based numbering.
  83. */
  84. #define BRG_INT_CLK (get_brgfreq())
  85. #define BRG_UART_CLK (BRG_INT_CLK/16)
  86. /* This function is used by UARTS, or anything else that uses a 16x
  87. * oversampled clock.
  88. */
  89. void
  90. cpm_setbrg(uint brg, uint rate)
  91. {
  92. u32 __iomem *bp;
  93. /* This is good enough to get SMCs running.....
  94. */
  95. if (brg < 4) {
  96. bp = cpm2_map_size(im_brgc1, 16);
  97. } else {
  98. bp = cpm2_map_size(im_brgc5, 16);
  99. brg -= 4;
  100. }
  101. bp += brg;
  102. out_be32(bp, (((BRG_UART_CLK / rate) - 1) << 1) | CPM_BRG_EN);
  103. cpm2_unmap(bp);
  104. }
  105. /* This function is used to set high speed synchronous baud rate
  106. * clocks.
  107. */
  108. void
  109. cpm2_fastbrg(uint brg, uint rate, int div16)
  110. {
  111. u32 __iomem *bp;
  112. u32 val;
  113. if (brg < 4) {
  114. bp = cpm2_map_size(im_brgc1, 16);
  115. }
  116. else {
  117. bp = cpm2_map_size(im_brgc5, 16);
  118. brg -= 4;
  119. }
  120. bp += brg;
  121. val = ((BRG_INT_CLK / rate) << 1) | CPM_BRG_EN;
  122. if (div16)
  123. val |= CPM_BRG_DIV16;
  124. out_be32(bp, val);
  125. cpm2_unmap(bp);
  126. }
  127. int cpm2_clk_setup(enum cpm_clk_target target, int clock, int mode)
  128. {
  129. int ret = 0;
  130. int shift;
  131. int i, bits = 0;
  132. cpmux_t __iomem *im_cpmux;
  133. u32 __iomem *reg;
  134. u32 mask = 7;
  135. u8 clk_map[][3] = {
  136. {CPM_CLK_FCC1, CPM_BRG5, 0},
  137. {CPM_CLK_FCC1, CPM_BRG6, 1},
  138. {CPM_CLK_FCC1, CPM_BRG7, 2},
  139. {CPM_CLK_FCC1, CPM_BRG8, 3},
  140. {CPM_CLK_FCC1, CPM_CLK9, 4},
  141. {CPM_CLK_FCC1, CPM_CLK10, 5},
  142. {CPM_CLK_FCC1, CPM_CLK11, 6},
  143. {CPM_CLK_FCC1, CPM_CLK12, 7},
  144. {CPM_CLK_FCC2, CPM_BRG5, 0},
  145. {CPM_CLK_FCC2, CPM_BRG6, 1},
  146. {CPM_CLK_FCC2, CPM_BRG7, 2},
  147. {CPM_CLK_FCC2, CPM_BRG8, 3},
  148. {CPM_CLK_FCC2, CPM_CLK13, 4},
  149. {CPM_CLK_FCC2, CPM_CLK14, 5},
  150. {CPM_CLK_FCC2, CPM_CLK15, 6},
  151. {CPM_CLK_FCC2, CPM_CLK16, 7},
  152. {CPM_CLK_FCC3, CPM_BRG5, 0},
  153. {CPM_CLK_FCC3, CPM_BRG6, 1},
  154. {CPM_CLK_FCC3, CPM_BRG7, 2},
  155. {CPM_CLK_FCC3, CPM_BRG8, 3},
  156. {CPM_CLK_FCC3, CPM_CLK13, 4},
  157. {CPM_CLK_FCC3, CPM_CLK14, 5},
  158. {CPM_CLK_FCC3, CPM_CLK15, 6},
  159. {CPM_CLK_FCC3, CPM_CLK16, 7},
  160. {CPM_CLK_SCC1, CPM_BRG1, 0},
  161. {CPM_CLK_SCC1, CPM_BRG2, 1},
  162. {CPM_CLK_SCC1, CPM_BRG3, 2},
  163. {CPM_CLK_SCC1, CPM_BRG4, 3},
  164. {CPM_CLK_SCC1, CPM_CLK11, 4},
  165. {CPM_CLK_SCC1, CPM_CLK12, 5},
  166. {CPM_CLK_SCC1, CPM_CLK3, 6},
  167. {CPM_CLK_SCC1, CPM_CLK4, 7},
  168. {CPM_CLK_SCC2, CPM_BRG1, 0},
  169. {CPM_CLK_SCC2, CPM_BRG2, 1},
  170. {CPM_CLK_SCC2, CPM_BRG3, 2},
  171. {CPM_CLK_SCC2, CPM_BRG4, 3},
  172. {CPM_CLK_SCC2, CPM_CLK11, 4},
  173. {CPM_CLK_SCC2, CPM_CLK12, 5},
  174. {CPM_CLK_SCC2, CPM_CLK3, 6},
  175. {CPM_CLK_SCC2, CPM_CLK4, 7},
  176. {CPM_CLK_SCC3, CPM_BRG1, 0},
  177. {CPM_CLK_SCC3, CPM_BRG2, 1},
  178. {CPM_CLK_SCC3, CPM_BRG3, 2},
  179. {CPM_CLK_SCC3, CPM_BRG4, 3},
  180. {CPM_CLK_SCC3, CPM_CLK5, 4},
  181. {CPM_CLK_SCC3, CPM_CLK6, 5},
  182. {CPM_CLK_SCC3, CPM_CLK7, 6},
  183. {CPM_CLK_SCC3, CPM_CLK8, 7},
  184. {CPM_CLK_SCC4, CPM_BRG1, 0},
  185. {CPM_CLK_SCC4, CPM_BRG2, 1},
  186. {CPM_CLK_SCC4, CPM_BRG3, 2},
  187. {CPM_CLK_SCC4, CPM_BRG4, 3},
  188. {CPM_CLK_SCC4, CPM_CLK5, 4},
  189. {CPM_CLK_SCC4, CPM_CLK6, 5},
  190. {CPM_CLK_SCC4, CPM_CLK7, 6},
  191. {CPM_CLK_SCC4, CPM_CLK8, 7},
  192. };
  193. im_cpmux = cpm2_map(im_cpmux);
  194. switch (target) {
  195. case CPM_CLK_SCC1:
  196. reg = &im_cpmux->cmx_scr;
  197. shift = 24;
  198. case CPM_CLK_SCC2:
  199. reg = &im_cpmux->cmx_scr;
  200. shift = 16;
  201. break;
  202. case CPM_CLK_SCC3:
  203. reg = &im_cpmux->cmx_scr;
  204. shift = 8;
  205. break;
  206. case CPM_CLK_SCC4:
  207. reg = &im_cpmux->cmx_scr;
  208. shift = 0;
  209. break;
  210. case CPM_CLK_FCC1:
  211. reg = &im_cpmux->cmx_fcr;
  212. shift = 24;
  213. break;
  214. case CPM_CLK_FCC2:
  215. reg = &im_cpmux->cmx_fcr;
  216. shift = 16;
  217. break;
  218. case CPM_CLK_FCC3:
  219. reg = &im_cpmux->cmx_fcr;
  220. shift = 8;
  221. break;
  222. default:
  223. printk(KERN_ERR "cpm2_clock_setup: invalid clock target\n");
  224. return -EINVAL;
  225. }
  226. if (mode == CPM_CLK_RX)
  227. shift += 3;
  228. for (i = 0; i < ARRAY_SIZE(clk_map); i++) {
  229. if (clk_map[i][0] == target && clk_map[i][1] == clock) {
  230. bits = clk_map[i][2];
  231. break;
  232. }
  233. }
  234. if (i == ARRAY_SIZE(clk_map))
  235. ret = -EINVAL;
  236. bits <<= shift;
  237. mask <<= shift;
  238. out_be32(reg, (in_be32(reg) & ~mask) | bits);
  239. cpm2_unmap(im_cpmux);
  240. return ret;
  241. }
  242. int cpm2_smc_clk_setup(enum cpm_clk_target target, int clock)
  243. {
  244. int ret = 0;
  245. int shift;
  246. int i, bits = 0;
  247. cpmux_t __iomem *im_cpmux;
  248. u8 __iomem *reg;
  249. u8 mask = 3;
  250. u8 clk_map[][3] = {
  251. {CPM_CLK_SMC1, CPM_BRG1, 0},
  252. {CPM_CLK_SMC1, CPM_BRG7, 1},
  253. {CPM_CLK_SMC1, CPM_CLK7, 2},
  254. {CPM_CLK_SMC1, CPM_CLK9, 3},
  255. {CPM_CLK_SMC2, CPM_BRG2, 0},
  256. {CPM_CLK_SMC2, CPM_BRG8, 1},
  257. {CPM_CLK_SMC2, CPM_CLK4, 2},
  258. {CPM_CLK_SMC2, CPM_CLK15, 3},
  259. };
  260. im_cpmux = cpm2_map(im_cpmux);
  261. switch (target) {
  262. case CPM_CLK_SMC1:
  263. reg = &im_cpmux->cmx_smr;
  264. mask = 3;
  265. shift = 4;
  266. break;
  267. case CPM_CLK_SMC2:
  268. reg = &im_cpmux->cmx_smr;
  269. mask = 3;
  270. shift = 0;
  271. break;
  272. default:
  273. printk(KERN_ERR "cpm2_smc_clock_setup: invalid clock target\n");
  274. return -EINVAL;
  275. }
  276. for (i = 0; i < ARRAY_SIZE(clk_map); i++) {
  277. if (clk_map[i][0] == target && clk_map[i][1] == clock) {
  278. bits = clk_map[i][2];
  279. break;
  280. }
  281. }
  282. if (i == ARRAY_SIZE(clk_map))
  283. ret = -EINVAL;
  284. bits <<= shift;
  285. mask <<= shift;
  286. out_8(reg, (in_8(reg) & ~mask) | bits);
  287. cpm2_unmap(im_cpmux);
  288. return ret;
  289. }
  290. #ifndef CONFIG_PPC_CPM_NEW_BINDING
  291. /*
  292. * dpalloc / dpfree bits.
  293. */
  294. static spinlock_t cpm_dpmem_lock;
  295. /* 16 blocks should be enough to satisfy all requests
  296. * until the memory subsystem goes up... */
  297. static rh_block_t cpm_boot_dpmem_rh_block[16];
  298. static rh_info_t cpm_dpmem_info;
  299. static u8 __iomem *im_dprambase;
  300. static void cpm2_dpinit(void)
  301. {
  302. spin_lock_init(&cpm_dpmem_lock);
  303. /* initialize the info header */
  304. rh_init(&cpm_dpmem_info, 1,
  305. sizeof(cpm_boot_dpmem_rh_block) /
  306. sizeof(cpm_boot_dpmem_rh_block[0]),
  307. cpm_boot_dpmem_rh_block);
  308. im_dprambase = cpm2_immr;
  309. /* Attach the usable dpmem area */
  310. /* XXX: This is actually crap. CPM_DATAONLY_BASE and
  311. * CPM_DATAONLY_SIZE is only a subset of the available dpram. It
  312. * varies with the processor and the microcode patches activated.
  313. * But the following should be at least safe.
  314. */
  315. rh_attach_region(&cpm_dpmem_info, CPM_DATAONLY_BASE, CPM_DATAONLY_SIZE);
  316. }
  317. /* This function returns an index into the DPRAM area.
  318. */
  319. unsigned long cpm_dpalloc(uint size, uint align)
  320. {
  321. unsigned long start;
  322. unsigned long flags;
  323. spin_lock_irqsave(&cpm_dpmem_lock, flags);
  324. cpm_dpmem_info.alignment = align;
  325. start = rh_alloc(&cpm_dpmem_info, size, "commproc");
  326. spin_unlock_irqrestore(&cpm_dpmem_lock, flags);
  327. return (uint)start;
  328. }
  329. EXPORT_SYMBOL(cpm_dpalloc);
  330. int cpm_dpfree(unsigned long offset)
  331. {
  332. int ret;
  333. unsigned long flags;
  334. spin_lock_irqsave(&cpm_dpmem_lock, flags);
  335. ret = rh_free(&cpm_dpmem_info, offset);
  336. spin_unlock_irqrestore(&cpm_dpmem_lock, flags);
  337. return ret;
  338. }
  339. EXPORT_SYMBOL(cpm_dpfree);
  340. /* not sure if this is ever needed */
  341. unsigned long cpm_dpalloc_fixed(unsigned long offset, uint size, uint align)
  342. {
  343. unsigned long start;
  344. unsigned long flags;
  345. spin_lock_irqsave(&cpm_dpmem_lock, flags);
  346. cpm_dpmem_info.alignment = align;
  347. start = rh_alloc_fixed(&cpm_dpmem_info, offset, size, "commproc");
  348. spin_unlock_irqrestore(&cpm_dpmem_lock, flags);
  349. return start;
  350. }
  351. EXPORT_SYMBOL(cpm_dpalloc_fixed);
  352. void cpm_dpdump(void)
  353. {
  354. rh_dump(&cpm_dpmem_info);
  355. }
  356. EXPORT_SYMBOL(cpm_dpdump);
  357. void *cpm_dpram_addr(unsigned long offset)
  358. {
  359. return (void *)(im_dprambase + offset);
  360. }
  361. EXPORT_SYMBOL(cpm_dpram_addr);
  362. #endif /* !CONFIG_PPC_CPM_NEW_BINDING */
  363. struct cpm2_ioports {
  364. u32 dir, par, sor, odr, dat;
  365. u32 res[3];
  366. };
  367. void cpm2_set_pin(int port, int pin, int flags)
  368. {
  369. struct cpm2_ioports __iomem *iop =
  370. (struct cpm2_ioports __iomem *)&cpm2_immr->im_ioport;
  371. pin = 1 << (31 - pin);
  372. if (flags & CPM_PIN_OUTPUT)
  373. setbits32(&iop[port].dir, pin);
  374. else
  375. clrbits32(&iop[port].dir, pin);
  376. if (!(flags & CPM_PIN_GPIO))
  377. setbits32(&iop[port].par, pin);
  378. else
  379. clrbits32(&iop[port].par, pin);
  380. if (flags & CPM_PIN_SECONDARY)
  381. setbits32(&iop[port].sor, pin);
  382. else
  383. clrbits32(&iop[port].sor, pin);
  384. if (flags & CPM_PIN_OPENDRAIN)
  385. setbits32(&iop[port].odr, pin);
  386. else
  387. clrbits32(&iop[port].odr, pin);
  388. }