qe.c 8.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360
  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 = of_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 BRG clock is the QE clock divided by 2.
  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 = of_get_property(qe, "brg-frequency", &size);
  137. brg_clk = *prop;
  138. of_node_put(qe);
  139. };
  140. return brg_clk;
  141. }
  142. /* Program the BRG to the given sampling rate and multiplier
  143. *
  144. * @brg: the BRG, 1-16
  145. * @rate: the desired sampling rate
  146. * @multiplier: corresponds to the value programmed in GUMR_L[RDCR] or
  147. * GUMR_L[TDCR]. E.g., if this BRG is the RX clock, and GUMR_L[RDCR]=01,
  148. * then 'multiplier' should be 8.
  149. *
  150. * Also note that the value programmed into the BRGC register must be even.
  151. */
  152. void qe_setbrg(unsigned int brg, unsigned int rate, unsigned int multiplier)
  153. {
  154. u32 divisor, tempval;
  155. u32 div16 = 0;
  156. divisor = get_brg_clk() / (rate * multiplier);
  157. if (divisor > QE_BRGC_DIVISOR_MAX + 1) {
  158. div16 = QE_BRGC_DIV16;
  159. divisor /= 16;
  160. }
  161. /* Errata QE_General4, which affects some MPC832x and MPC836x SOCs, says
  162. that the BRG divisor must be even if you're not using divide-by-16
  163. mode. */
  164. if (!div16 && (divisor & 1))
  165. divisor++;
  166. tempval = ((divisor - 1) << QE_BRGC_DIVISOR_SHIFT) |
  167. QE_BRGC_ENABLE | div16;
  168. out_be32(&qe_immr->brg.brgc[brg - 1], tempval);
  169. }
  170. /* Initialize SNUMs (thread serial numbers) according to
  171. * QE Module Control chapter, SNUM table
  172. */
  173. static void qe_snums_init(void)
  174. {
  175. int i;
  176. static const u8 snum_init[] = {
  177. 0x04, 0x05, 0x0C, 0x0D, 0x14, 0x15, 0x1C, 0x1D,
  178. 0x24, 0x25, 0x2C, 0x2D, 0x34, 0x35, 0x88, 0x89,
  179. 0x98, 0x99, 0xA8, 0xA9, 0xB8, 0xB9, 0xC8, 0xC9,
  180. 0xD8, 0xD9, 0xE8, 0xE9,
  181. };
  182. for (i = 0; i < QE_NUM_OF_SNUM; i++) {
  183. snums[i].num = snum_init[i];
  184. snums[i].state = QE_SNUM_STATE_FREE;
  185. }
  186. }
  187. int qe_get_snum(void)
  188. {
  189. unsigned long flags;
  190. int snum = -EBUSY;
  191. int i;
  192. spin_lock_irqsave(&qe_lock, flags);
  193. for (i = 0; i < QE_NUM_OF_SNUM; i++) {
  194. if (snums[i].state == QE_SNUM_STATE_FREE) {
  195. snums[i].state = QE_SNUM_STATE_USED;
  196. snum = snums[i].num;
  197. break;
  198. }
  199. }
  200. spin_unlock_irqrestore(&qe_lock, flags);
  201. return snum;
  202. }
  203. EXPORT_SYMBOL(qe_get_snum);
  204. void qe_put_snum(u8 snum)
  205. {
  206. int i;
  207. for (i = 0; i < QE_NUM_OF_SNUM; i++) {
  208. if (snums[i].num == snum) {
  209. snums[i].state = QE_SNUM_STATE_FREE;
  210. break;
  211. }
  212. }
  213. }
  214. EXPORT_SYMBOL(qe_put_snum);
  215. static int qe_sdma_init(void)
  216. {
  217. struct sdma *sdma = &qe_immr->sdma;
  218. unsigned long sdma_buf_offset;
  219. if (!sdma)
  220. return -ENODEV;
  221. /* allocate 2 internal temporary buffers (512 bytes size each) for
  222. * the SDMA */
  223. sdma_buf_offset = qe_muram_alloc(512 * 2, 4096);
  224. if (IS_ERR_VALUE(sdma_buf_offset))
  225. return -ENOMEM;
  226. out_be32(&sdma->sdebcr, (u32) sdma_buf_offset & QE_SDEBCR_BA_MASK);
  227. out_be32(&sdma->sdmr, (QE_SDMR_GLB_1_MSK |
  228. (0x1 << QE_SDMR_CEN_SHIFT)));
  229. return 0;
  230. }
  231. /*
  232. * muram_alloc / muram_free bits.
  233. */
  234. static DEFINE_SPINLOCK(qe_muram_lock);
  235. /* 16 blocks should be enough to satisfy all requests
  236. * until the memory subsystem goes up... */
  237. static rh_block_t qe_boot_muram_rh_block[16];
  238. static rh_info_t qe_muram_info;
  239. static void qe_muram_init(void)
  240. {
  241. struct device_node *np;
  242. u32 address;
  243. u64 size;
  244. unsigned int flags;
  245. /* initialize the info header */
  246. rh_init(&qe_muram_info, 1,
  247. sizeof(qe_boot_muram_rh_block) /
  248. sizeof(qe_boot_muram_rh_block[0]), qe_boot_muram_rh_block);
  249. /* Attach the usable muram area */
  250. /* XXX: This is a subset of the available muram. It
  251. * varies with the processor and the microcode patches activated.
  252. */
  253. if ((np = of_find_node_by_name(NULL, "data-only")) != NULL) {
  254. address = *of_get_address(np, 0, &size, &flags);
  255. of_node_put(np);
  256. rh_attach_region(&qe_muram_info, address, (int) size);
  257. }
  258. }
  259. /* This function returns an index into the MURAM area.
  260. */
  261. unsigned long qe_muram_alloc(int size, int align)
  262. {
  263. unsigned long start;
  264. unsigned long flags;
  265. spin_lock_irqsave(&qe_muram_lock, flags);
  266. start = rh_alloc_align(&qe_muram_info, size, align, "QE");
  267. spin_unlock_irqrestore(&qe_muram_lock, flags);
  268. return start;
  269. }
  270. EXPORT_SYMBOL(qe_muram_alloc);
  271. int qe_muram_free(unsigned long offset)
  272. {
  273. int ret;
  274. unsigned long flags;
  275. spin_lock_irqsave(&qe_muram_lock, flags);
  276. ret = rh_free(&qe_muram_info, offset);
  277. spin_unlock_irqrestore(&qe_muram_lock, flags);
  278. return ret;
  279. }
  280. EXPORT_SYMBOL(qe_muram_free);
  281. /* not sure if this is ever needed */
  282. unsigned long qe_muram_alloc_fixed(unsigned long offset, int size)
  283. {
  284. unsigned long start;
  285. unsigned long flags;
  286. spin_lock_irqsave(&qe_muram_lock, flags);
  287. start = rh_alloc_fixed(&qe_muram_info, offset, size, "commproc");
  288. spin_unlock_irqrestore(&qe_muram_lock, flags);
  289. return start;
  290. }
  291. EXPORT_SYMBOL(qe_muram_alloc_fixed);
  292. void qe_muram_dump(void)
  293. {
  294. rh_dump(&qe_muram_info);
  295. }
  296. EXPORT_SYMBOL(qe_muram_dump);
  297. void *qe_muram_addr(unsigned long offset)
  298. {
  299. return (void *)&qe_immr->muram[offset];
  300. }
  301. EXPORT_SYMBOL(qe_muram_addr);