qe.c 9.2 KB

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