qe.c 8.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353
  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,
  106. immrbar_virt_to_phys((void *)cmd_input));
  107. out_be32(&qe_immr->cp.cecr,
  108. (cmd | QE_CR_FLG | ((u32) device << dev_shift) | (u32)
  109. mcn_protocol << mcn_shift));
  110. }
  111. /* wait for the QE_CR_FLG to clear */
  112. while(in_be32(&qe_immr->cp.cecr) & QE_CR_FLG)
  113. cpu_relax();
  114. spin_unlock_irqrestore(&qe_lock, flags);
  115. return 0;
  116. }
  117. EXPORT_SYMBOL(qe_issue_cmd);
  118. /* Set a baud rate generator. This needs lots of work. There are
  119. * 16 BRGs, which can be connected to the QE channels or output
  120. * as clocks. The BRGs are in two different block of internal
  121. * memory mapped space.
  122. * The baud rate clock is the system clock divided by something.
  123. * It was set up long ago during the initial boot phase and is
  124. * is given to us.
  125. * Baud rate clocks are zero-based in the driver code (as that maps
  126. * to port numbers). Documentation uses 1-based numbering.
  127. */
  128. static unsigned int brg_clk = 0;
  129. unsigned int get_brg_clk(void)
  130. {
  131. struct device_node *qe;
  132. if (brg_clk)
  133. return brg_clk;
  134. qe = of_find_node_by_type(NULL, "qe");
  135. if (qe) {
  136. unsigned int size;
  137. const u32 *prop = get_property(qe, "brg-frequency", &size);
  138. brg_clk = *prop;
  139. of_node_put(qe);
  140. };
  141. return brg_clk;
  142. }
  143. /* This function is used by UARTS, or anything else that uses a 16x
  144. * oversampled clock.
  145. */
  146. void qe_setbrg(u32 brg, u32 rate)
  147. {
  148. volatile u32 *bp;
  149. u32 divisor, tempval;
  150. int div16 = 0;
  151. bp = &qe_immr->brg.brgc1;
  152. bp += brg;
  153. divisor = (get_brg_clk() / rate);
  154. if (divisor > QE_BRGC_DIVISOR_MAX + 1) {
  155. div16 = 1;
  156. divisor /= 16;
  157. }
  158. tempval = ((divisor - 1) << QE_BRGC_DIVISOR_SHIFT) | QE_BRGC_ENABLE;
  159. if (div16)
  160. tempval |= QE_BRGC_DIV16;
  161. out_be32(bp, tempval);
  162. }
  163. /* Initialize SNUMs (thread serial numbers) according to
  164. * QE Module Control chapter, SNUM table
  165. */
  166. static void qe_snums_init(void)
  167. {
  168. int i;
  169. static const u8 snum_init[] = {
  170. 0x04, 0x05, 0x0C, 0x0D, 0x14, 0x15, 0x1C, 0x1D,
  171. 0x24, 0x25, 0x2C, 0x2D, 0x34, 0x35, 0x88, 0x89,
  172. 0x98, 0x99, 0xA8, 0xA9, 0xB8, 0xB9, 0xC8, 0xC9,
  173. 0xD8, 0xD9, 0xE8, 0xE9,
  174. };
  175. for (i = 0; i < QE_NUM_OF_SNUM; i++) {
  176. snums[i].num = snum_init[i];
  177. snums[i].state = QE_SNUM_STATE_FREE;
  178. }
  179. }
  180. int qe_get_snum(void)
  181. {
  182. unsigned long flags;
  183. int snum = -EBUSY;
  184. int i;
  185. spin_lock_irqsave(&qe_lock, flags);
  186. for (i = 0; i < QE_NUM_OF_SNUM; i++) {
  187. if (snums[i].state == QE_SNUM_STATE_FREE) {
  188. snums[i].state = QE_SNUM_STATE_USED;
  189. snum = snums[i].num;
  190. break;
  191. }
  192. }
  193. spin_unlock_irqrestore(&qe_lock, flags);
  194. return snum;
  195. }
  196. EXPORT_SYMBOL(qe_get_snum);
  197. void qe_put_snum(u8 snum)
  198. {
  199. int i;
  200. for (i = 0; i < QE_NUM_OF_SNUM; i++) {
  201. if (snums[i].num == snum) {
  202. snums[i].state = QE_SNUM_STATE_FREE;
  203. break;
  204. }
  205. }
  206. }
  207. EXPORT_SYMBOL(qe_put_snum);
  208. static int qe_sdma_init(void)
  209. {
  210. struct sdma *sdma = &qe_immr->sdma;
  211. u32 sdma_buf_offset;
  212. if (!sdma)
  213. return -ENODEV;
  214. /* allocate 2 internal temporary buffers (512 bytes size each) for
  215. * the SDMA */
  216. sdma_buf_offset = qe_muram_alloc(512 * 2, 64);
  217. if (IS_MURAM_ERR(sdma_buf_offset))
  218. return -ENOMEM;
  219. out_be32(&sdma->sdebcr, sdma_buf_offset & QE_SDEBCR_BA_MASK);
  220. out_be32(&sdma->sdmr, (QE_SDMR_GLB_1_MSK | (0x1 >>
  221. QE_SDMR_CEN_SHIFT)));
  222. return 0;
  223. }
  224. /*
  225. * muram_alloc / muram_free bits.
  226. */
  227. static DEFINE_SPINLOCK(qe_muram_lock);
  228. /* 16 blocks should be enough to satisfy all requests
  229. * until the memory subsystem goes up... */
  230. static rh_block_t qe_boot_muram_rh_block[16];
  231. static rh_info_t qe_muram_info;
  232. static void qe_muram_init(void)
  233. {
  234. struct device_node *np;
  235. u32 address;
  236. u64 size;
  237. unsigned int flags;
  238. /* initialize the info header */
  239. rh_init(&qe_muram_info, 1,
  240. sizeof(qe_boot_muram_rh_block) /
  241. sizeof(qe_boot_muram_rh_block[0]), qe_boot_muram_rh_block);
  242. /* Attach the usable muram area */
  243. /* XXX: This is a subset of the available muram. It
  244. * varies with the processor and the microcode patches activated.
  245. */
  246. if ((np = of_find_node_by_name(NULL, "data-only")) != NULL) {
  247. address = *of_get_address(np, 0, &size, &flags);
  248. of_node_put(np);
  249. rh_attach_region(&qe_muram_info,
  250. (void *)address, (int)size);
  251. }
  252. }
  253. /* This function returns an index into the MURAM area.
  254. */
  255. u32 qe_muram_alloc(u32 size, u32 align)
  256. {
  257. void *start;
  258. unsigned long flags;
  259. spin_lock_irqsave(&qe_muram_lock, flags);
  260. start = rh_alloc_align(&qe_muram_info, size, align, "QE");
  261. spin_unlock_irqrestore(&qe_muram_lock, flags);
  262. return (u32) start;
  263. }
  264. EXPORT_SYMBOL(qe_muram_alloc);
  265. int qe_muram_free(u32 offset)
  266. {
  267. int ret;
  268. unsigned long flags;
  269. spin_lock_irqsave(&qe_muram_lock, flags);
  270. ret = rh_free(&qe_muram_info, (void *)offset);
  271. spin_unlock_irqrestore(&qe_muram_lock, flags);
  272. return ret;
  273. }
  274. EXPORT_SYMBOL(qe_muram_free);
  275. /* not sure if this is ever needed */
  276. u32 qe_muram_alloc_fixed(u32 offset, u32 size)
  277. {
  278. void *start;
  279. unsigned long flags;
  280. spin_lock_irqsave(&qe_muram_lock, flags);
  281. start = rh_alloc_fixed(&qe_muram_info, (void *)offset, size, "commproc");
  282. spin_unlock_irqrestore(&qe_muram_lock, flags);
  283. return (u32) start;
  284. }
  285. EXPORT_SYMBOL(qe_muram_alloc_fixed);
  286. void qe_muram_dump(void)
  287. {
  288. rh_dump(&qe_muram_info);
  289. }
  290. EXPORT_SYMBOL(qe_muram_dump);
  291. void *qe_muram_addr(u32 offset)
  292. {
  293. return (void *)&qe_immr->muram[offset];
  294. }
  295. EXPORT_SYMBOL(qe_muram_addr);