qe.c 8.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364
  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, QE_BRG1 - QE_BRG16
  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. int qe_setbrg(enum qe_clock brg, unsigned int rate, unsigned int multiplier)
  151. {
  152. u32 divisor, tempval;
  153. u32 div16 = 0;
  154. if ((brg < QE_BRG1) || (brg > QE_BRG16))
  155. return -EINVAL;
  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 - QE_BRG1], tempval);
  169. return 0;
  170. }
  171. EXPORT_SYMBOL(qe_setbrg);
  172. /* Initialize SNUMs (thread serial numbers) according to
  173. * QE Module Control chapter, SNUM table
  174. */
  175. static void qe_snums_init(void)
  176. {
  177. int i;
  178. static const u8 snum_init[] = {
  179. 0x04, 0x05, 0x0C, 0x0D, 0x14, 0x15, 0x1C, 0x1D,
  180. 0x24, 0x25, 0x2C, 0x2D, 0x34, 0x35, 0x88, 0x89,
  181. 0x98, 0x99, 0xA8, 0xA9, 0xB8, 0xB9, 0xC8, 0xC9,
  182. 0xD8, 0xD9, 0xE8, 0xE9,
  183. };
  184. for (i = 0; i < QE_NUM_OF_SNUM; i++) {
  185. snums[i].num = snum_init[i];
  186. snums[i].state = QE_SNUM_STATE_FREE;
  187. }
  188. }
  189. int qe_get_snum(void)
  190. {
  191. unsigned long flags;
  192. int snum = -EBUSY;
  193. int i;
  194. spin_lock_irqsave(&qe_lock, flags);
  195. for (i = 0; i < QE_NUM_OF_SNUM; i++) {
  196. if (snums[i].state == QE_SNUM_STATE_FREE) {
  197. snums[i].state = QE_SNUM_STATE_USED;
  198. snum = snums[i].num;
  199. break;
  200. }
  201. }
  202. spin_unlock_irqrestore(&qe_lock, flags);
  203. return snum;
  204. }
  205. EXPORT_SYMBOL(qe_get_snum);
  206. void qe_put_snum(u8 snum)
  207. {
  208. int i;
  209. for (i = 0; i < QE_NUM_OF_SNUM; i++) {
  210. if (snums[i].num == snum) {
  211. snums[i].state = QE_SNUM_STATE_FREE;
  212. break;
  213. }
  214. }
  215. }
  216. EXPORT_SYMBOL(qe_put_snum);
  217. static int qe_sdma_init(void)
  218. {
  219. struct sdma *sdma = &qe_immr->sdma;
  220. unsigned long sdma_buf_offset;
  221. if (!sdma)
  222. return -ENODEV;
  223. /* allocate 2 internal temporary buffers (512 bytes size each) for
  224. * the SDMA */
  225. sdma_buf_offset = qe_muram_alloc(512 * 2, 4096);
  226. if (IS_ERR_VALUE(sdma_buf_offset))
  227. return -ENOMEM;
  228. out_be32(&sdma->sdebcr, (u32) sdma_buf_offset & QE_SDEBCR_BA_MASK);
  229. out_be32(&sdma->sdmr, (QE_SDMR_GLB_1_MSK |
  230. (0x1 << QE_SDMR_CEN_SHIFT)));
  231. return 0;
  232. }
  233. /*
  234. * muram_alloc / muram_free bits.
  235. */
  236. static DEFINE_SPINLOCK(qe_muram_lock);
  237. /* 16 blocks should be enough to satisfy all requests
  238. * until the memory subsystem goes up... */
  239. static rh_block_t qe_boot_muram_rh_block[16];
  240. static rh_info_t qe_muram_info;
  241. static void qe_muram_init(void)
  242. {
  243. struct device_node *np;
  244. u32 address;
  245. u64 size;
  246. unsigned int flags;
  247. /* initialize the info header */
  248. rh_init(&qe_muram_info, 1,
  249. sizeof(qe_boot_muram_rh_block) /
  250. sizeof(qe_boot_muram_rh_block[0]), qe_boot_muram_rh_block);
  251. /* Attach the usable muram area */
  252. /* XXX: This is a subset of the available muram. It
  253. * varies with the processor and the microcode patches activated.
  254. */
  255. if ((np = of_find_node_by_name(NULL, "data-only")) != NULL) {
  256. address = *of_get_address(np, 0, &size, &flags);
  257. of_node_put(np);
  258. rh_attach_region(&qe_muram_info, address, (int) size);
  259. }
  260. }
  261. /* This function returns an index into the MURAM area.
  262. */
  263. unsigned long qe_muram_alloc(int size, int align)
  264. {
  265. unsigned long start;
  266. unsigned long flags;
  267. spin_lock_irqsave(&qe_muram_lock, flags);
  268. start = rh_alloc_align(&qe_muram_info, size, align, "QE");
  269. spin_unlock_irqrestore(&qe_muram_lock, flags);
  270. return start;
  271. }
  272. EXPORT_SYMBOL(qe_muram_alloc);
  273. int qe_muram_free(unsigned long offset)
  274. {
  275. int ret;
  276. unsigned long flags;
  277. spin_lock_irqsave(&qe_muram_lock, flags);
  278. ret = rh_free(&qe_muram_info, offset);
  279. spin_unlock_irqrestore(&qe_muram_lock, flags);
  280. return ret;
  281. }
  282. EXPORT_SYMBOL(qe_muram_free);
  283. /* not sure if this is ever needed */
  284. unsigned long qe_muram_alloc_fixed(unsigned long offset, int size)
  285. {
  286. unsigned long start;
  287. unsigned long flags;
  288. spin_lock_irqsave(&qe_muram_lock, flags);
  289. start = rh_alloc_fixed(&qe_muram_info, offset, size, "commproc");
  290. spin_unlock_irqrestore(&qe_muram_lock, flags);
  291. return start;
  292. }
  293. EXPORT_SYMBOL(qe_muram_alloc_fixed);
  294. void qe_muram_dump(void)
  295. {
  296. rh_dump(&qe_muram_info);
  297. }
  298. EXPORT_SYMBOL(qe_muram_dump);
  299. void *qe_muram_addr(unsigned long offset)
  300. {
  301. return (void *)&qe_immr->muram[offset];
  302. }
  303. EXPORT_SYMBOL(qe_muram_addr);