qe.c 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643
  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 <linux/crc32.h>
  29. #include <asm/irq.h>
  30. #include <asm/page.h>
  31. #include <asm/pgtable.h>
  32. #include <asm/immap_qe.h>
  33. #include <asm/qe.h>
  34. #include <asm/prom.h>
  35. #include <asm/rheap.h>
  36. static void qe_snums_init(void);
  37. static void qe_muram_init(void);
  38. static int qe_sdma_init(void);
  39. static DEFINE_SPINLOCK(qe_lock);
  40. /* QE snum state */
  41. enum qe_snum_state {
  42. QE_SNUM_STATE_USED,
  43. QE_SNUM_STATE_FREE
  44. };
  45. /* QE snum */
  46. struct qe_snum {
  47. u8 num;
  48. enum qe_snum_state state;
  49. };
  50. /* We allocate this here because it is used almost exclusively for
  51. * the communication processor devices.
  52. */
  53. struct qe_immap *qe_immr = NULL;
  54. EXPORT_SYMBOL(qe_immr);
  55. static struct qe_snum snums[QE_NUM_OF_SNUM]; /* Dynamically allocated SNUMs */
  56. static phys_addr_t qebase = -1;
  57. phys_addr_t get_qe_base(void)
  58. {
  59. struct device_node *qe;
  60. if (qebase != -1)
  61. return qebase;
  62. qe = of_find_node_by_type(NULL, "qe");
  63. if (qe) {
  64. unsigned int size;
  65. const void *prop = of_get_property(qe, "reg", &size);
  66. qebase = of_translate_address(qe, prop);
  67. of_node_put(qe);
  68. };
  69. return qebase;
  70. }
  71. EXPORT_SYMBOL(get_qe_base);
  72. void qe_reset(void)
  73. {
  74. if (qe_immr == NULL)
  75. qe_immr = ioremap(get_qe_base(), QE_IMMAP_SIZE);
  76. qe_snums_init();
  77. qe_issue_cmd(QE_RESET, QE_CR_SUBBLOCK_INVALID,
  78. QE_CR_PROTOCOL_UNSPECIFIED, 0);
  79. /* Reclaim the MURAM memory for our use. */
  80. qe_muram_init();
  81. if (qe_sdma_init())
  82. panic("sdma init failed!");
  83. }
  84. int qe_issue_cmd(u32 cmd, u32 device, u8 mcn_protocol, u32 cmd_input)
  85. {
  86. unsigned long flags;
  87. u8 mcn_shift = 0, dev_shift = 0;
  88. spin_lock_irqsave(&qe_lock, flags);
  89. if (cmd == QE_RESET) {
  90. out_be32(&qe_immr->cp.cecr, (u32) (cmd | QE_CR_FLG));
  91. } else {
  92. if (cmd == QE_ASSIGN_PAGE) {
  93. /* Here device is the SNUM, not sub-block */
  94. dev_shift = QE_CR_SNUM_SHIFT;
  95. } else if (cmd == QE_ASSIGN_RISC) {
  96. /* Here device is the SNUM, and mcnProtocol is
  97. * e_QeCmdRiscAssignment value */
  98. dev_shift = QE_CR_SNUM_SHIFT;
  99. mcn_shift = QE_CR_MCN_RISC_ASSIGN_SHIFT;
  100. } else {
  101. if (device == QE_CR_SUBBLOCK_USB)
  102. mcn_shift = QE_CR_MCN_USB_SHIFT;
  103. else
  104. mcn_shift = QE_CR_MCN_NORMAL_SHIFT;
  105. }
  106. out_be32(&qe_immr->cp.cecdr, 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 BRG clock is the QE clock divided by 2.
  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 = of_get_property(qe, "brg-frequency", &size);
  138. brg_clk = *prop;
  139. of_node_put(qe);
  140. };
  141. return brg_clk;
  142. }
  143. /* Program the BRG to the given sampling rate and multiplier
  144. *
  145. * @brg: the BRG, QE_BRG1 - QE_BRG16
  146. * @rate: the desired sampling rate
  147. * @multiplier: corresponds to the value programmed in GUMR_L[RDCR] or
  148. * GUMR_L[TDCR]. E.g., if this BRG is the RX clock, and GUMR_L[RDCR]=01,
  149. * then 'multiplier' should be 8.
  150. */
  151. int qe_setbrg(enum qe_clock brg, unsigned int rate, unsigned int multiplier)
  152. {
  153. u32 divisor, tempval;
  154. u32 div16 = 0;
  155. if ((brg < QE_BRG1) || (brg > QE_BRG16))
  156. return -EINVAL;
  157. divisor = get_brg_clk() / (rate * multiplier);
  158. if (divisor > QE_BRGC_DIVISOR_MAX + 1) {
  159. div16 = QE_BRGC_DIV16;
  160. divisor /= 16;
  161. }
  162. /* Errata QE_General4, which affects some MPC832x and MPC836x SOCs, says
  163. that the BRG divisor must be even if you're not using divide-by-16
  164. mode. */
  165. if (!div16 && (divisor & 1))
  166. divisor++;
  167. tempval = ((divisor - 1) << QE_BRGC_DIVISOR_SHIFT) |
  168. QE_BRGC_ENABLE | div16;
  169. out_be32(&qe_immr->brg.brgc[brg - QE_BRG1], tempval);
  170. return 0;
  171. }
  172. EXPORT_SYMBOL(qe_setbrg);
  173. /* Convert a string to a QE clock source enum
  174. *
  175. * This function takes a string, typically from a property in the device
  176. * tree, and returns the corresponding "enum qe_clock" value.
  177. */
  178. enum qe_clock qe_clock_source(const char *source)
  179. {
  180. unsigned int i;
  181. if (strcasecmp(source, "none") == 0)
  182. return QE_CLK_NONE;
  183. if (strncasecmp(source, "brg", 3) == 0) {
  184. i = simple_strtoul(source + 3, NULL, 10);
  185. if ((i >= 1) && (i <= 16))
  186. return (QE_BRG1 - 1) + i;
  187. else
  188. return QE_CLK_DUMMY;
  189. }
  190. if (strncasecmp(source, "clk", 3) == 0) {
  191. i = simple_strtoul(source + 3, NULL, 10);
  192. if ((i >= 1) && (i <= 24))
  193. return (QE_CLK1 - 1) + i;
  194. else
  195. return QE_CLK_DUMMY;
  196. }
  197. return QE_CLK_DUMMY;
  198. }
  199. EXPORT_SYMBOL(qe_clock_source);
  200. /* Initialize SNUMs (thread serial numbers) according to
  201. * QE Module Control chapter, SNUM table
  202. */
  203. static void qe_snums_init(void)
  204. {
  205. int i;
  206. static const u8 snum_init[] = {
  207. 0x04, 0x05, 0x0C, 0x0D, 0x14, 0x15, 0x1C, 0x1D,
  208. 0x24, 0x25, 0x2C, 0x2D, 0x34, 0x35, 0x88, 0x89,
  209. 0x98, 0x99, 0xA8, 0xA9, 0xB8, 0xB9, 0xC8, 0xC9,
  210. 0xD8, 0xD9, 0xE8, 0xE9,
  211. };
  212. for (i = 0; i < QE_NUM_OF_SNUM; i++) {
  213. snums[i].num = snum_init[i];
  214. snums[i].state = QE_SNUM_STATE_FREE;
  215. }
  216. }
  217. int qe_get_snum(void)
  218. {
  219. unsigned long flags;
  220. int snum = -EBUSY;
  221. int i;
  222. spin_lock_irqsave(&qe_lock, flags);
  223. for (i = 0; i < QE_NUM_OF_SNUM; i++) {
  224. if (snums[i].state == QE_SNUM_STATE_FREE) {
  225. snums[i].state = QE_SNUM_STATE_USED;
  226. snum = snums[i].num;
  227. break;
  228. }
  229. }
  230. spin_unlock_irqrestore(&qe_lock, flags);
  231. return snum;
  232. }
  233. EXPORT_SYMBOL(qe_get_snum);
  234. void qe_put_snum(u8 snum)
  235. {
  236. int i;
  237. for (i = 0; i < QE_NUM_OF_SNUM; i++) {
  238. if (snums[i].num == snum) {
  239. snums[i].state = QE_SNUM_STATE_FREE;
  240. break;
  241. }
  242. }
  243. }
  244. EXPORT_SYMBOL(qe_put_snum);
  245. static int qe_sdma_init(void)
  246. {
  247. struct sdma *sdma = &qe_immr->sdma;
  248. unsigned long sdma_buf_offset;
  249. if (!sdma)
  250. return -ENODEV;
  251. /* allocate 2 internal temporary buffers (512 bytes size each) for
  252. * the SDMA */
  253. sdma_buf_offset = qe_muram_alloc(512 * 2, 4096);
  254. if (IS_ERR_VALUE(sdma_buf_offset))
  255. return -ENOMEM;
  256. out_be32(&sdma->sdebcr, (u32) sdma_buf_offset & QE_SDEBCR_BA_MASK);
  257. out_be32(&sdma->sdmr, (QE_SDMR_GLB_1_MSK |
  258. (0x1 << QE_SDMR_CEN_SHIFT)));
  259. return 0;
  260. }
  261. /*
  262. * muram_alloc / muram_free bits.
  263. */
  264. static DEFINE_SPINLOCK(qe_muram_lock);
  265. /* 16 blocks should be enough to satisfy all requests
  266. * until the memory subsystem goes up... */
  267. static rh_block_t qe_boot_muram_rh_block[16];
  268. static rh_info_t qe_muram_info;
  269. static void qe_muram_init(void)
  270. {
  271. struct device_node *np;
  272. u32 address;
  273. u64 size;
  274. unsigned int flags;
  275. /* initialize the info header */
  276. rh_init(&qe_muram_info, 1,
  277. sizeof(qe_boot_muram_rh_block) /
  278. sizeof(qe_boot_muram_rh_block[0]), qe_boot_muram_rh_block);
  279. /* Attach the usable muram area */
  280. /* XXX: This is a subset of the available muram. It
  281. * varies with the processor and the microcode patches activated.
  282. */
  283. if ((np = of_find_node_by_name(NULL, "data-only")) != NULL) {
  284. address = *of_get_address(np, 0, &size, &flags);
  285. of_node_put(np);
  286. rh_attach_region(&qe_muram_info, address, (int) size);
  287. }
  288. }
  289. /* This function returns an index into the MURAM area.
  290. */
  291. unsigned long qe_muram_alloc(int size, int align)
  292. {
  293. unsigned long start;
  294. unsigned long flags;
  295. spin_lock_irqsave(&qe_muram_lock, flags);
  296. start = rh_alloc_align(&qe_muram_info, size, align, "QE");
  297. spin_unlock_irqrestore(&qe_muram_lock, flags);
  298. return start;
  299. }
  300. EXPORT_SYMBOL(qe_muram_alloc);
  301. int qe_muram_free(unsigned long offset)
  302. {
  303. int ret;
  304. unsigned long flags;
  305. spin_lock_irqsave(&qe_muram_lock, flags);
  306. ret = rh_free(&qe_muram_info, offset);
  307. spin_unlock_irqrestore(&qe_muram_lock, flags);
  308. return ret;
  309. }
  310. EXPORT_SYMBOL(qe_muram_free);
  311. /* not sure if this is ever needed */
  312. unsigned long qe_muram_alloc_fixed(unsigned long offset, int size)
  313. {
  314. unsigned long start;
  315. unsigned long flags;
  316. spin_lock_irqsave(&qe_muram_lock, flags);
  317. start = rh_alloc_fixed(&qe_muram_info, offset, size, "commproc");
  318. spin_unlock_irqrestore(&qe_muram_lock, flags);
  319. return start;
  320. }
  321. EXPORT_SYMBOL(qe_muram_alloc_fixed);
  322. void qe_muram_dump(void)
  323. {
  324. rh_dump(&qe_muram_info);
  325. }
  326. EXPORT_SYMBOL(qe_muram_dump);
  327. void *qe_muram_addr(unsigned long offset)
  328. {
  329. return (void *)&qe_immr->muram[offset];
  330. }
  331. EXPORT_SYMBOL(qe_muram_addr);
  332. /* The maximum number of RISCs we support */
  333. #define MAX_QE_RISC 2
  334. /* Firmware information stored here for qe_get_firmware_info() */
  335. static struct qe_firmware_info qe_firmware_info;
  336. /*
  337. * Set to 1 if QE firmware has been uploaded, and therefore
  338. * qe_firmware_info contains valid data.
  339. */
  340. static int qe_firmware_uploaded;
  341. /*
  342. * Upload a QE microcode
  343. *
  344. * This function is a worker function for qe_upload_firmware(). It does
  345. * the actual uploading of the microcode.
  346. */
  347. static void qe_upload_microcode(const void *base,
  348. const struct qe_microcode *ucode)
  349. {
  350. const __be32 *code = base + be32_to_cpu(ucode->code_offset);
  351. unsigned int i;
  352. if (ucode->major || ucode->minor || ucode->revision)
  353. printk(KERN_INFO "qe-firmware: "
  354. "uploading microcode '%s' version %u.%u.%u\n",
  355. ucode->id, ucode->major, ucode->minor, ucode->revision);
  356. else
  357. printk(KERN_INFO "qe-firmware: "
  358. "uploading microcode '%s'\n", ucode->id);
  359. /* Use auto-increment */
  360. out_be32(&qe_immr->iram.iadd, be32_to_cpu(ucode->iram_offset) |
  361. QE_IRAM_IADD_AIE | QE_IRAM_IADD_BADDR);
  362. for (i = 0; i < be32_to_cpu(ucode->count); i++)
  363. out_be32(&qe_immr->iram.idata, be32_to_cpu(code[i]));
  364. }
  365. /*
  366. * Upload a microcode to the I-RAM at a specific address.
  367. *
  368. * See Documentation/powerpc/qe-firmware.txt for information on QE microcode
  369. * uploading.
  370. *
  371. * Currently, only version 1 is supported, so the 'version' field must be
  372. * set to 1.
  373. *
  374. * The SOC model and revision are not validated, they are only displayed for
  375. * informational purposes.
  376. *
  377. * 'calc_size' is the calculated size, in bytes, of the firmware structure and
  378. * all of the microcode structures, minus the CRC.
  379. *
  380. * 'length' is the size that the structure says it is, including the CRC.
  381. */
  382. int qe_upload_firmware(const struct qe_firmware *firmware)
  383. {
  384. unsigned int i;
  385. unsigned int j;
  386. u32 crc;
  387. size_t calc_size = sizeof(struct qe_firmware);
  388. size_t length;
  389. const struct qe_header *hdr;
  390. if (!firmware) {
  391. printk(KERN_ERR "qe-firmware: invalid pointer\n");
  392. return -EINVAL;
  393. }
  394. hdr = &firmware->header;
  395. length = be32_to_cpu(hdr->length);
  396. /* Check the magic */
  397. if ((hdr->magic[0] != 'Q') || (hdr->magic[1] != 'E') ||
  398. (hdr->magic[2] != 'F')) {
  399. printk(KERN_ERR "qe-firmware: not a microcode\n");
  400. return -EPERM;
  401. }
  402. /* Check the version */
  403. if (hdr->version != 1) {
  404. printk(KERN_ERR "qe-firmware: unsupported version\n");
  405. return -EPERM;
  406. }
  407. /* Validate some of the fields */
  408. if ((firmware->count < 1) || (firmware->count >= MAX_QE_RISC)) {
  409. printk(KERN_ERR "qe-firmware: invalid data\n");
  410. return -EINVAL;
  411. }
  412. /* Validate the length and check if there's a CRC */
  413. calc_size += (firmware->count - 1) * sizeof(struct qe_microcode);
  414. for (i = 0; i < firmware->count; i++)
  415. /*
  416. * For situations where the second RISC uses the same microcode
  417. * as the first, the 'code_offset' and 'count' fields will be
  418. * zero, so it's okay to add those.
  419. */
  420. calc_size += sizeof(__be32) *
  421. be32_to_cpu(firmware->microcode[i].count);
  422. /* Validate the length */
  423. if (length != calc_size + sizeof(__be32)) {
  424. printk(KERN_ERR "qe-firmware: invalid length\n");
  425. return -EPERM;
  426. }
  427. /* Validate the CRC */
  428. crc = be32_to_cpu(*(__be32 *)((void *)firmware + calc_size));
  429. if (crc != crc32(0, firmware, calc_size)) {
  430. printk(KERN_ERR "qe-firmware: firmware CRC is invalid\n");
  431. return -EIO;
  432. }
  433. /*
  434. * If the microcode calls for it, split the I-RAM.
  435. */
  436. if (!firmware->split)
  437. setbits16(&qe_immr->cp.cercr, QE_CP_CERCR_CIR);
  438. if (firmware->soc.model)
  439. printk(KERN_INFO
  440. "qe-firmware: firmware '%s' for %u V%u.%u\n",
  441. firmware->id, be16_to_cpu(firmware->soc.model),
  442. firmware->soc.major, firmware->soc.minor);
  443. else
  444. printk(KERN_INFO "qe-firmware: firmware '%s'\n",
  445. firmware->id);
  446. /*
  447. * The QE only supports one microcode per RISC, so clear out all the
  448. * saved microcode information and put in the new.
  449. */
  450. memset(&qe_firmware_info, 0, sizeof(qe_firmware_info));
  451. strcpy(qe_firmware_info.id, firmware->id);
  452. qe_firmware_info.extended_modes = firmware->extended_modes;
  453. memcpy(qe_firmware_info.vtraps, firmware->vtraps,
  454. sizeof(firmware->vtraps));
  455. /* Loop through each microcode. */
  456. for (i = 0; i < firmware->count; i++) {
  457. const struct qe_microcode *ucode = &firmware->microcode[i];
  458. /* Upload a microcode if it's present */
  459. if (ucode->code_offset)
  460. qe_upload_microcode(firmware, ucode);
  461. /* Program the traps for this processor */
  462. for (j = 0; j < 16; j++) {
  463. u32 trap = be32_to_cpu(ucode->traps[j]);
  464. if (trap)
  465. out_be32(&qe_immr->rsp[i].tibcr[j], trap);
  466. }
  467. /* Enable traps */
  468. out_be32(&qe_immr->rsp[i].eccr, be32_to_cpu(ucode->eccr));
  469. }
  470. qe_firmware_uploaded = 1;
  471. return 0;
  472. }
  473. EXPORT_SYMBOL(qe_upload_firmware);
  474. /*
  475. * Get info on the currently-loaded firmware
  476. *
  477. * This function also checks the device tree to see if the boot loader has
  478. * uploaded a firmware already.
  479. */
  480. struct qe_firmware_info *qe_get_firmware_info(void)
  481. {
  482. static int initialized;
  483. struct property *prop;
  484. struct device_node *qe;
  485. struct device_node *fw = NULL;
  486. const char *sprop;
  487. unsigned int i;
  488. /*
  489. * If we haven't checked yet, and a driver hasn't uploaded a firmware
  490. * yet, then check the device tree for information.
  491. */
  492. if (initialized || qe_firmware_uploaded)
  493. return NULL;
  494. initialized = 1;
  495. /*
  496. * Newer device trees have an "fsl,qe" compatible property for the QE
  497. * node, but we still need to support older device trees.
  498. */
  499. qe = of_find_compatible_node(NULL, NULL, "fsl,qe");
  500. if (!qe) {
  501. qe = of_find_node_by_type(NULL, "qe");
  502. if (!qe)
  503. return NULL;
  504. }
  505. /* Find the 'firmware' child node */
  506. for_each_child_of_node(qe, fw) {
  507. if (strcmp(fw->name, "firmware") == 0)
  508. break;
  509. }
  510. of_node_put(qe);
  511. /* Did we find the 'firmware' node? */
  512. if (!fw)
  513. return NULL;
  514. qe_firmware_uploaded = 1;
  515. /* Copy the data into qe_firmware_info*/
  516. sprop = of_get_property(fw, "id", NULL);
  517. if (sprop)
  518. strncpy(qe_firmware_info.id, sprop,
  519. sizeof(qe_firmware_info.id) - 1);
  520. prop = of_find_property(fw, "extended-modes", NULL);
  521. if (prop && (prop->length == sizeof(u64))) {
  522. const u64 *iprop = prop->value;
  523. qe_firmware_info.extended_modes = *iprop;
  524. }
  525. prop = of_find_property(fw, "virtual-traps", NULL);
  526. if (prop && (prop->length == 32)) {
  527. const u32 *iprop = prop->value;
  528. for (i = 0; i < ARRAY_SIZE(qe_firmware_info.vtraps); i++)
  529. qe_firmware_info.vtraps[i] = iprop[i];
  530. }
  531. of_node_put(fw);
  532. return &qe_firmware_info;
  533. }
  534. EXPORT_SYMBOL(qe_get_firmware_info);