cpm2.c 11 KB

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