cpm2_common.c 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457
  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. static void cpm2_dpinit(void);
  46. cpm_cpm2_t __iomem *cpmp; /* Pointer to comm processor space */
  47. /* We allocate this here because it is used almost exclusively for
  48. * the communication processor devices.
  49. */
  50. cpm2_map_t __iomem *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
  55. cpm2_reset(void)
  56. {
  57. #ifdef CONFIG_PPC_85xx
  58. cpm2_immr = ioremap(CPM_MAP_ADDR, CPM_MAP_SIZE);
  59. #else
  60. cpm2_immr = ioremap(get_immrbase(), CPM_MAP_SIZE);
  61. #endif
  62. /* Reclaim the DP memory for our use.
  63. */
  64. cpm2_dpinit();
  65. /* Tell everyone where the comm processor resides.
  66. */
  67. cpmp = &cpm2_immr->im_cpm;
  68. }
  69. /* Set a baud rate generator. This needs lots of work. There are
  70. * eight BRGs, which can be connected to the CPM channels or output
  71. * as clocks. The BRGs are in two different block of internal
  72. * memory mapped space.
  73. * The baud rate clock is the system clock divided by something.
  74. * It was set up long ago during the initial boot phase and is
  75. * is given to us.
  76. * Baud rate clocks are zero-based in the driver code (as that maps
  77. * to port numbers). Documentation uses 1-based numbering.
  78. */
  79. #define BRG_INT_CLK (get_brgfreq())
  80. #define BRG_UART_CLK (BRG_INT_CLK/16)
  81. /* This function is used by UARTS, or anything else that uses a 16x
  82. * oversampled clock.
  83. */
  84. void
  85. cpm_setbrg(uint brg, uint rate)
  86. {
  87. u32 __iomem *bp;
  88. /* This is good enough to get SMCs running.....
  89. */
  90. if (brg < 4) {
  91. bp = cpm2_map_size(im_brgc1, 16);
  92. } else {
  93. bp = cpm2_map_size(im_brgc5, 16);
  94. brg -= 4;
  95. }
  96. bp += brg;
  97. out_be32(bp, (((BRG_UART_CLK / rate) - 1) << 1) | CPM_BRG_EN);
  98. cpm2_unmap(bp);
  99. }
  100. /* This function is used to set high speed synchronous baud rate
  101. * clocks.
  102. */
  103. void
  104. cpm2_fastbrg(uint brg, uint rate, int div16)
  105. {
  106. u32 __iomem *bp;
  107. u32 val;
  108. if (brg < 4) {
  109. bp = cpm2_map_size(im_brgc1, 16);
  110. }
  111. else {
  112. bp = cpm2_map_size(im_brgc5, 16);
  113. brg -= 4;
  114. }
  115. bp += brg;
  116. val = ((BRG_INT_CLK / rate) << 1) | CPM_BRG_EN;
  117. if (div16)
  118. val |= CPM_BRG_DIV16;
  119. out_be32(bp, val);
  120. cpm2_unmap(bp);
  121. }
  122. int cpm2_clk_setup(enum cpm_clk_target target, int clock, int mode)
  123. {
  124. int ret = 0;
  125. int shift;
  126. int i, bits = 0;
  127. cpmux_t __iomem *im_cpmux;
  128. u32 __iomem *reg;
  129. u32 mask = 7;
  130. u8 clk_map[][3] = {
  131. {CPM_CLK_FCC1, CPM_BRG5, 0},
  132. {CPM_CLK_FCC1, CPM_BRG6, 1},
  133. {CPM_CLK_FCC1, CPM_BRG7, 2},
  134. {CPM_CLK_FCC1, CPM_BRG8, 3},
  135. {CPM_CLK_FCC1, CPM_CLK9, 4},
  136. {CPM_CLK_FCC1, CPM_CLK10, 5},
  137. {CPM_CLK_FCC1, CPM_CLK11, 6},
  138. {CPM_CLK_FCC1, CPM_CLK12, 7},
  139. {CPM_CLK_FCC2, CPM_BRG5, 0},
  140. {CPM_CLK_FCC2, CPM_BRG6, 1},
  141. {CPM_CLK_FCC2, CPM_BRG7, 2},
  142. {CPM_CLK_FCC2, CPM_BRG8, 3},
  143. {CPM_CLK_FCC2, CPM_CLK13, 4},
  144. {CPM_CLK_FCC2, CPM_CLK14, 5},
  145. {CPM_CLK_FCC2, CPM_CLK15, 6},
  146. {CPM_CLK_FCC2, CPM_CLK16, 7},
  147. {CPM_CLK_FCC3, CPM_BRG5, 0},
  148. {CPM_CLK_FCC3, CPM_BRG6, 1},
  149. {CPM_CLK_FCC3, CPM_BRG7, 2},
  150. {CPM_CLK_FCC3, CPM_BRG8, 3},
  151. {CPM_CLK_FCC3, CPM_CLK13, 4},
  152. {CPM_CLK_FCC3, CPM_CLK14, 5},
  153. {CPM_CLK_FCC3, CPM_CLK15, 6},
  154. {CPM_CLK_FCC3, CPM_CLK16, 7},
  155. {CPM_CLK_SCC1, CPM_BRG1, 0},
  156. {CPM_CLK_SCC1, CPM_BRG2, 1},
  157. {CPM_CLK_SCC1, CPM_BRG3, 2},
  158. {CPM_CLK_SCC1, CPM_BRG4, 3},
  159. {CPM_CLK_SCC1, CPM_CLK11, 4},
  160. {CPM_CLK_SCC1, CPM_CLK12, 5},
  161. {CPM_CLK_SCC1, CPM_CLK3, 6},
  162. {CPM_CLK_SCC1, CPM_CLK4, 7},
  163. {CPM_CLK_SCC2, CPM_BRG1, 0},
  164. {CPM_CLK_SCC2, CPM_BRG2, 1},
  165. {CPM_CLK_SCC2, CPM_BRG3, 2},
  166. {CPM_CLK_SCC2, CPM_BRG4, 3},
  167. {CPM_CLK_SCC2, CPM_CLK11, 4},
  168. {CPM_CLK_SCC2, CPM_CLK12, 5},
  169. {CPM_CLK_SCC2, CPM_CLK3, 6},
  170. {CPM_CLK_SCC2, CPM_CLK4, 7},
  171. {CPM_CLK_SCC3, CPM_BRG1, 0},
  172. {CPM_CLK_SCC3, CPM_BRG2, 1},
  173. {CPM_CLK_SCC3, CPM_BRG3, 2},
  174. {CPM_CLK_SCC3, CPM_BRG4, 3},
  175. {CPM_CLK_SCC3, CPM_CLK5, 4},
  176. {CPM_CLK_SCC3, CPM_CLK6, 5},
  177. {CPM_CLK_SCC3, CPM_CLK7, 6},
  178. {CPM_CLK_SCC3, CPM_CLK8, 7},
  179. {CPM_CLK_SCC4, CPM_BRG1, 0},
  180. {CPM_CLK_SCC4, CPM_BRG2, 1},
  181. {CPM_CLK_SCC4, CPM_BRG3, 2},
  182. {CPM_CLK_SCC4, CPM_BRG4, 3},
  183. {CPM_CLK_SCC4, CPM_CLK5, 4},
  184. {CPM_CLK_SCC4, CPM_CLK6, 5},
  185. {CPM_CLK_SCC4, CPM_CLK7, 6},
  186. {CPM_CLK_SCC4, CPM_CLK8, 7},
  187. };
  188. im_cpmux = cpm2_map(im_cpmux);
  189. switch (target) {
  190. case CPM_CLK_SCC1:
  191. reg = &im_cpmux->cmx_scr;
  192. shift = 24;
  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. /*
  286. * dpalloc / dpfree bits.
  287. */
  288. static spinlock_t cpm_dpmem_lock;
  289. /* 16 blocks should be enough to satisfy all requests
  290. * until the memory subsystem goes up... */
  291. static rh_block_t cpm_boot_dpmem_rh_block[16];
  292. static rh_info_t cpm_dpmem_info;
  293. static u8 __iomem *im_dprambase;
  294. static void cpm2_dpinit(void)
  295. {
  296. struct resource r;
  297. #ifdef CONFIG_PPC_CPM_NEW_BINDING
  298. struct device_node *np;
  299. np = of_find_compatible_node(NULL, NULL, "fsl,cpm2");
  300. if (!np)
  301. panic("Cannot find CPM2 node");
  302. if (of_address_to_resource(np, 1, &r))
  303. panic("Cannot get CPM2 resource 1");
  304. of_node_put(np);
  305. #else
  306. r.start = CPM_MAP_ADDR;
  307. r.end = r.start + CPM_DATAONLY_BASE + CPM_DATAONLY_SIZE - 1;
  308. #endif
  309. im_dprambase = ioremap(r.start, r.end - r.start + 1);
  310. if (!im_dprambase)
  311. panic("Cannot map DPRAM");
  312. spin_lock_init(&cpm_dpmem_lock);
  313. /* initialize the info header */
  314. rh_init(&cpm_dpmem_info, 1,
  315. sizeof(cpm_boot_dpmem_rh_block) /
  316. sizeof(cpm_boot_dpmem_rh_block[0]),
  317. cpm_boot_dpmem_rh_block);
  318. /* Attach the usable dpmem area */
  319. /* XXX: This is actually crap. CPM_DATAONLY_BASE and
  320. * CPM_DATAONLY_SIZE is only a subset of the available dpram. It
  321. * varies with the processor and the microcode patches activated.
  322. * But the following should be at least safe.
  323. */
  324. rh_attach_region(&cpm_dpmem_info, 0, r.end - r.start + 1);
  325. }
  326. /* This function returns an index into the DPRAM area.
  327. */
  328. unsigned long cpm_dpalloc(uint size, uint align)
  329. {
  330. unsigned long start;
  331. unsigned long flags;
  332. spin_lock_irqsave(&cpm_dpmem_lock, flags);
  333. cpm_dpmem_info.alignment = align;
  334. start = rh_alloc(&cpm_dpmem_info, size, "commproc");
  335. spin_unlock_irqrestore(&cpm_dpmem_lock, flags);
  336. return (uint)start;
  337. }
  338. EXPORT_SYMBOL(cpm_dpalloc);
  339. int cpm_dpfree(unsigned long offset)
  340. {
  341. int ret;
  342. unsigned long flags;
  343. spin_lock_irqsave(&cpm_dpmem_lock, flags);
  344. ret = rh_free(&cpm_dpmem_info, offset);
  345. spin_unlock_irqrestore(&cpm_dpmem_lock, flags);
  346. return ret;
  347. }
  348. EXPORT_SYMBOL(cpm_dpfree);
  349. /* not sure if this is ever needed */
  350. unsigned long cpm_dpalloc_fixed(unsigned long offset, uint size, uint align)
  351. {
  352. unsigned long start;
  353. unsigned long flags;
  354. spin_lock_irqsave(&cpm_dpmem_lock, flags);
  355. cpm_dpmem_info.alignment = align;
  356. start = rh_alloc_fixed(&cpm_dpmem_info, offset, size, "commproc");
  357. spin_unlock_irqrestore(&cpm_dpmem_lock, flags);
  358. return start;
  359. }
  360. EXPORT_SYMBOL(cpm_dpalloc_fixed);
  361. void cpm_dpdump(void)
  362. {
  363. rh_dump(&cpm_dpmem_info);
  364. }
  365. EXPORT_SYMBOL(cpm_dpdump);
  366. void *cpm_dpram_addr(unsigned long offset)
  367. {
  368. return (void *)(im_dprambase + offset);
  369. }
  370. EXPORT_SYMBOL(cpm_dpram_addr);
  371. struct cpm2_ioports {
  372. u32 dir, par, sor, odr, dat;
  373. u32 res[3];
  374. };
  375. void cpm2_set_pin(int port, int pin, int flags)
  376. {
  377. struct cpm2_ioports __iomem *iop =
  378. (struct cpm2_ioports __iomem *)&cpm2_immr->im_ioport;
  379. pin = 1 << (31 - pin);
  380. if (flags & CPM_PIN_OUTPUT)
  381. setbits32(&iop[port].dir, pin);
  382. else
  383. clrbits32(&iop[port].dir, pin);
  384. if (!(flags & CPM_PIN_GPIO))
  385. setbits32(&iop[port].par, pin);
  386. else
  387. clrbits32(&iop[port].par, pin);
  388. if (flags & CPM_PIN_SECONDARY)
  389. setbits32(&iop[port].sor, pin);
  390. else
  391. clrbits32(&iop[port].sor, pin);
  392. if (flags & CPM_PIN_OPENDRAIN)
  393. setbits32(&iop[port].odr, pin);
  394. else
  395. clrbits32(&iop[port].odr, pin);
  396. }