cpm2_common.c 10 KB

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