qe.c 8.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351
  1. /*
  2. * Copyright (C) 2006 Freescale Semicondutor, Inc. All rights reserved.
  3. *
  4. * Authors: Shlomi Gridish <gridish@freescale.com>
  5. * Li Yang <leoli@freescale.com>
  6. * Based on cpm2_common.c from Dan Malek (dmalek@jlc.net)
  7. *
  8. * Description:
  9. * General Purpose functions for the global management of the
  10. * QUICC Engine (QE).
  11. *
  12. * This program is free software; you can redistribute it and/or modify it
  13. * under the terms of the GNU General Public License as published by the
  14. * Free Software Foundation; either version 2 of the License, or (at your
  15. * option) any later version.
  16. */
  17. #include <linux/errno.h>
  18. #include <linux/sched.h>
  19. #include <linux/kernel.h>
  20. #include <linux/param.h>
  21. #include <linux/string.h>
  22. #include <linux/mm.h>
  23. #include <linux/interrupt.h>
  24. #include <linux/bootmem.h>
  25. #include <linux/module.h>
  26. #include <linux/delay.h>
  27. #include <linux/ioport.h>
  28. #include <asm/irq.h>
  29. #include <asm/page.h>
  30. #include <asm/pgtable.h>
  31. #include <asm/immap_qe.h>
  32. #include <asm/qe.h>
  33. #include <asm/prom.h>
  34. #include <asm/rheap.h>
  35. static void qe_snums_init(void);
  36. static void qe_muram_init(void);
  37. static int qe_sdma_init(void);
  38. static DEFINE_SPINLOCK(qe_lock);
  39. /* QE snum state */
  40. enum qe_snum_state {
  41. QE_SNUM_STATE_USED,
  42. QE_SNUM_STATE_FREE
  43. };
  44. /* QE snum */
  45. struct qe_snum {
  46. u8 num;
  47. enum qe_snum_state state;
  48. };
  49. /* We allocate this here because it is used almost exclusively for
  50. * the communication processor devices.
  51. */
  52. struct qe_immap *qe_immr = NULL;
  53. EXPORT_SYMBOL(qe_immr);
  54. static struct qe_snum snums[QE_NUM_OF_SNUM]; /* Dynamically allocated SNUMs */
  55. static phys_addr_t qebase = -1;
  56. phys_addr_t get_qe_base(void)
  57. {
  58. struct device_node *qe;
  59. if (qebase != -1)
  60. return qebase;
  61. qe = of_find_node_by_type(NULL, "qe");
  62. if (qe) {
  63. unsigned int size;
  64. const void *prop = get_property(qe, "reg", &size);
  65. qebase = of_translate_address(qe, prop);
  66. of_node_put(qe);
  67. };
  68. return qebase;
  69. }
  70. EXPORT_SYMBOL(get_qe_base);
  71. void qe_reset(void)
  72. {
  73. if (qe_immr == NULL)
  74. qe_immr = ioremap(get_qe_base(), QE_IMMAP_SIZE);
  75. qe_snums_init();
  76. qe_issue_cmd(QE_RESET, QE_CR_SUBBLOCK_INVALID,
  77. QE_CR_PROTOCOL_UNSPECIFIED, 0);
  78. /* Reclaim the MURAM memory for our use. */
  79. qe_muram_init();
  80. if (qe_sdma_init())
  81. panic("sdma init failed!");
  82. }
  83. int qe_issue_cmd(u32 cmd, u32 device, u8 mcn_protocol, u32 cmd_input)
  84. {
  85. unsigned long flags;
  86. u8 mcn_shift = 0, dev_shift = 0;
  87. spin_lock_irqsave(&qe_lock, flags);
  88. if (cmd == QE_RESET) {
  89. out_be32(&qe_immr->cp.cecr, (u32) (cmd | QE_CR_FLG));
  90. } else {
  91. if (cmd == QE_ASSIGN_PAGE) {
  92. /* Here device is the SNUM, not sub-block */
  93. dev_shift = QE_CR_SNUM_SHIFT;
  94. } else if (cmd == QE_ASSIGN_RISC) {
  95. /* Here device is the SNUM, and mcnProtocol is
  96. * e_QeCmdRiscAssignment value */
  97. dev_shift = QE_CR_SNUM_SHIFT;
  98. mcn_shift = QE_CR_MCN_RISC_ASSIGN_SHIFT;
  99. } else {
  100. if (device == QE_CR_SUBBLOCK_USB)
  101. mcn_shift = QE_CR_MCN_USB_SHIFT;
  102. else
  103. mcn_shift = QE_CR_MCN_NORMAL_SHIFT;
  104. }
  105. out_be32(&qe_immr->cp.cecdr, cmd_input);
  106. out_be32(&qe_immr->cp.cecr,
  107. (cmd | QE_CR_FLG | ((u32) device << dev_shift) | (u32)
  108. mcn_protocol << mcn_shift));
  109. }
  110. /* wait for the QE_CR_FLG to clear */
  111. while(in_be32(&qe_immr->cp.cecr) & QE_CR_FLG)
  112. cpu_relax();
  113. spin_unlock_irqrestore(&qe_lock, flags);
  114. return 0;
  115. }
  116. EXPORT_SYMBOL(qe_issue_cmd);
  117. /* Set a baud rate generator. This needs lots of work. There are
  118. * 16 BRGs, which can be connected to the QE channels or output
  119. * as clocks. The BRGs are in two different block of internal
  120. * memory mapped space.
  121. * The baud rate clock is the system clock divided by something.
  122. * It was set up long ago during the initial boot phase and is
  123. * is given to us.
  124. * Baud rate clocks are zero-based in the driver code (as that maps
  125. * to port numbers). Documentation uses 1-based numbering.
  126. */
  127. static unsigned int brg_clk = 0;
  128. unsigned int get_brg_clk(void)
  129. {
  130. struct device_node *qe;
  131. if (brg_clk)
  132. return brg_clk;
  133. qe = of_find_node_by_type(NULL, "qe");
  134. if (qe) {
  135. unsigned int size;
  136. const u32 *prop = get_property(qe, "brg-frequency", &size);
  137. brg_clk = *prop;
  138. of_node_put(qe);
  139. };
  140. return brg_clk;
  141. }
  142. /* This function is used by UARTS, or anything else that uses a 16x
  143. * oversampled clock.
  144. */
  145. void qe_setbrg(u32 brg, u32 rate)
  146. {
  147. volatile u32 *bp;
  148. u32 divisor, tempval;
  149. int div16 = 0;
  150. bp = &qe_immr->brg.brgc[brg];
  151. divisor = (get_brg_clk() / rate);
  152. if (divisor > QE_BRGC_DIVISOR_MAX + 1) {
  153. div16 = 1;
  154. divisor /= 16;
  155. }
  156. tempval = ((divisor - 1) << QE_BRGC_DIVISOR_SHIFT) | QE_BRGC_ENABLE;
  157. if (div16)
  158. tempval |= QE_BRGC_DIV16;
  159. out_be32(bp, tempval);
  160. }
  161. /* Initialize SNUMs (thread serial numbers) according to
  162. * QE Module Control chapter, SNUM table
  163. */
  164. static void qe_snums_init(void)
  165. {
  166. int i;
  167. static const u8 snum_init[] = {
  168. 0x04, 0x05, 0x0C, 0x0D, 0x14, 0x15, 0x1C, 0x1D,
  169. 0x24, 0x25, 0x2C, 0x2D, 0x34, 0x35, 0x88, 0x89,
  170. 0x98, 0x99, 0xA8, 0xA9, 0xB8, 0xB9, 0xC8, 0xC9,
  171. 0xD8, 0xD9, 0xE8, 0xE9,
  172. };
  173. for (i = 0; i < QE_NUM_OF_SNUM; i++) {
  174. snums[i].num = snum_init[i];
  175. snums[i].state = QE_SNUM_STATE_FREE;
  176. }
  177. }
  178. int qe_get_snum(void)
  179. {
  180. unsigned long flags;
  181. int snum = -EBUSY;
  182. int i;
  183. spin_lock_irqsave(&qe_lock, flags);
  184. for (i = 0; i < QE_NUM_OF_SNUM; i++) {
  185. if (snums[i].state == QE_SNUM_STATE_FREE) {
  186. snums[i].state = QE_SNUM_STATE_USED;
  187. snum = snums[i].num;
  188. break;
  189. }
  190. }
  191. spin_unlock_irqrestore(&qe_lock, flags);
  192. return snum;
  193. }
  194. EXPORT_SYMBOL(qe_get_snum);
  195. void qe_put_snum(u8 snum)
  196. {
  197. int i;
  198. for (i = 0; i < QE_NUM_OF_SNUM; i++) {
  199. if (snums[i].num == snum) {
  200. snums[i].state = QE_SNUM_STATE_FREE;
  201. break;
  202. }
  203. }
  204. }
  205. EXPORT_SYMBOL(qe_put_snum);
  206. static int qe_sdma_init(void)
  207. {
  208. struct sdma *sdma = &qe_immr->sdma;
  209. u32 sdma_buf_offset;
  210. if (!sdma)
  211. return -ENODEV;
  212. /* allocate 2 internal temporary buffers (512 bytes size each) for
  213. * the SDMA */
  214. sdma_buf_offset = qe_muram_alloc(512 * 2, 64);
  215. if (IS_MURAM_ERR(sdma_buf_offset))
  216. return -ENOMEM;
  217. out_be32(&sdma->sdebcr, sdma_buf_offset & QE_SDEBCR_BA_MASK);
  218. out_be32(&sdma->sdmr, (QE_SDMR_GLB_1_MSK | (0x1 >>
  219. QE_SDMR_CEN_SHIFT)));
  220. return 0;
  221. }
  222. /*
  223. * muram_alloc / muram_free bits.
  224. */
  225. static DEFINE_SPINLOCK(qe_muram_lock);
  226. /* 16 blocks should be enough to satisfy all requests
  227. * until the memory subsystem goes up... */
  228. static rh_block_t qe_boot_muram_rh_block[16];
  229. static rh_info_t qe_muram_info;
  230. static void qe_muram_init(void)
  231. {
  232. struct device_node *np;
  233. u32 address;
  234. u64 size;
  235. unsigned int flags;
  236. /* initialize the info header */
  237. rh_init(&qe_muram_info, 1,
  238. sizeof(qe_boot_muram_rh_block) /
  239. sizeof(qe_boot_muram_rh_block[0]), qe_boot_muram_rh_block);
  240. /* Attach the usable muram area */
  241. /* XXX: This is a subset of the available muram. It
  242. * varies with the processor and the microcode patches activated.
  243. */
  244. if ((np = of_find_node_by_name(NULL, "data-only")) != NULL) {
  245. address = *of_get_address(np, 0, &size, &flags);
  246. of_node_put(np);
  247. rh_attach_region(&qe_muram_info,
  248. (void *)address, (int)size);
  249. }
  250. }
  251. /* This function returns an index into the MURAM area.
  252. */
  253. u32 qe_muram_alloc(u32 size, u32 align)
  254. {
  255. void *start;
  256. unsigned long flags;
  257. spin_lock_irqsave(&qe_muram_lock, flags);
  258. start = rh_alloc_align(&qe_muram_info, size, align, "QE");
  259. spin_unlock_irqrestore(&qe_muram_lock, flags);
  260. return (u32) start;
  261. }
  262. EXPORT_SYMBOL(qe_muram_alloc);
  263. int qe_muram_free(u32 offset)
  264. {
  265. int ret;
  266. unsigned long flags;
  267. spin_lock_irqsave(&qe_muram_lock, flags);
  268. ret = rh_free(&qe_muram_info, (void *)offset);
  269. spin_unlock_irqrestore(&qe_muram_lock, flags);
  270. return ret;
  271. }
  272. EXPORT_SYMBOL(qe_muram_free);
  273. /* not sure if this is ever needed */
  274. u32 qe_muram_alloc_fixed(u32 offset, u32 size)
  275. {
  276. void *start;
  277. unsigned long flags;
  278. spin_lock_irqsave(&qe_muram_lock, flags);
  279. start = rh_alloc_fixed(&qe_muram_info, (void *)offset, size, "commproc");
  280. spin_unlock_irqrestore(&qe_muram_lock, flags);
  281. return (u32) start;
  282. }
  283. EXPORT_SYMBOL(qe_muram_alloc_fixed);
  284. void qe_muram_dump(void)
  285. {
  286. rh_dump(&qe_muram_info);
  287. }
  288. EXPORT_SYMBOL(qe_muram_dump);
  289. void *qe_muram_addr(u32 offset)
  290. {
  291. return (void *)&qe_immr->muram[offset];
  292. }
  293. EXPORT_SYMBOL(qe_muram_addr);