qe.c 18 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736
  1. /*
  2. * Copyright (C) 2006-2010 Freescale Semiconductor, 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/spinlock.h>
  23. #include <linux/mm.h>
  24. #include <linux/interrupt.h>
  25. #include <linux/module.h>
  26. #include <linux/delay.h>
  27. #include <linux/ioport.h>
  28. #include <linux/crc32.h>
  29. #include <linux/mod_devicetable.h>
  30. #include <linux/of_platform.h>
  31. #include <asm/irq.h>
  32. #include <asm/page.h>
  33. #include <asm/pgtable.h>
  34. #include <soc/fsl/qe/immap_qe.h>
  35. #include <soc/fsl/qe/qe.h>
  36. #include <asm/prom.h>
  37. #include <asm/rheap.h>
  38. static void qe_snums_init(void);
  39. static int qe_sdma_init(void);
  40. static DEFINE_SPINLOCK(qe_lock);
  41. DEFINE_SPINLOCK(cmxgcr_lock);
  42. EXPORT_SYMBOL(cmxgcr_lock);
  43. /* QE snum state */
  44. enum qe_snum_state {
  45. QE_SNUM_STATE_USED,
  46. QE_SNUM_STATE_FREE
  47. };
  48. /* QE snum */
  49. struct qe_snum {
  50. u8 num;
  51. enum qe_snum_state state;
  52. };
  53. /* We allocate this here because it is used almost exclusively for
  54. * the communication processor devices.
  55. */
  56. struct qe_immap __iomem *qe_immr;
  57. EXPORT_SYMBOL(qe_immr);
  58. static struct qe_snum snums[QE_NUM_OF_SNUM]; /* Dynamically allocated SNUMs */
  59. static unsigned int qe_num_of_snum;
  60. static phys_addr_t qebase = -1;
  61. static phys_addr_t get_qe_base(void)
  62. {
  63. struct device_node *qe;
  64. int ret;
  65. struct resource res;
  66. if (qebase != -1)
  67. return qebase;
  68. qe = of_find_compatible_node(NULL, NULL, "fsl,qe");
  69. if (!qe) {
  70. qe = of_find_node_by_type(NULL, "qe");
  71. if (!qe)
  72. return qebase;
  73. }
  74. ret = of_address_to_resource(qe, 0, &res);
  75. if (!ret)
  76. qebase = res.start;
  77. of_node_put(qe);
  78. return qebase;
  79. }
  80. void qe_reset(void)
  81. {
  82. if (qe_immr == NULL)
  83. qe_immr = ioremap(get_qe_base(), QE_IMMAP_SIZE);
  84. qe_snums_init();
  85. qe_issue_cmd(QE_RESET, QE_CR_SUBBLOCK_INVALID,
  86. QE_CR_PROTOCOL_UNSPECIFIED, 0);
  87. /* Reclaim the MURAM memory for our use. */
  88. qe_muram_init();
  89. if (qe_sdma_init())
  90. panic("sdma init failed!");
  91. }
  92. int qe_issue_cmd(u32 cmd, u32 device, u8 mcn_protocol, u32 cmd_input)
  93. {
  94. unsigned long flags;
  95. u8 mcn_shift = 0, dev_shift = 0;
  96. u32 ret;
  97. spin_lock_irqsave(&qe_lock, flags);
  98. if (cmd == QE_RESET) {
  99. out_be32(&qe_immr->cp.cecr, (u32) (cmd | QE_CR_FLG));
  100. } else {
  101. if (cmd == QE_ASSIGN_PAGE) {
  102. /* Here device is the SNUM, not sub-block */
  103. dev_shift = QE_CR_SNUM_SHIFT;
  104. } else if (cmd == QE_ASSIGN_RISC) {
  105. /* Here device is the SNUM, and mcnProtocol is
  106. * e_QeCmdRiscAssignment value */
  107. dev_shift = QE_CR_SNUM_SHIFT;
  108. mcn_shift = QE_CR_MCN_RISC_ASSIGN_SHIFT;
  109. } else {
  110. if (device == QE_CR_SUBBLOCK_USB)
  111. mcn_shift = QE_CR_MCN_USB_SHIFT;
  112. else
  113. mcn_shift = QE_CR_MCN_NORMAL_SHIFT;
  114. }
  115. out_be32(&qe_immr->cp.cecdr, cmd_input);
  116. out_be32(&qe_immr->cp.cecr,
  117. (cmd | QE_CR_FLG | ((u32) device << dev_shift) | (u32)
  118. mcn_protocol << mcn_shift));
  119. }
  120. /* wait for the QE_CR_FLG to clear */
  121. ret = spin_event_timeout((in_be32(&qe_immr->cp.cecr) & QE_CR_FLG) == 0,
  122. 100, 0);
  123. /* On timeout (e.g. failure), the expression will be false (ret == 0),
  124. otherwise it will be true (ret == 1). */
  125. spin_unlock_irqrestore(&qe_lock, flags);
  126. return ret == 1;
  127. }
  128. EXPORT_SYMBOL(qe_issue_cmd);
  129. /* Set a baud rate generator. This needs lots of work. There are
  130. * 16 BRGs, which can be connected to the QE channels or output
  131. * as clocks. The BRGs are in two different block of internal
  132. * memory mapped space.
  133. * The BRG clock is the QE clock divided by 2.
  134. * It was set up long ago during the initial boot phase and is
  135. * is given to us.
  136. * Baud rate clocks are zero-based in the driver code (as that maps
  137. * to port numbers). Documentation uses 1-based numbering.
  138. */
  139. static unsigned int brg_clk = 0;
  140. #define CLK_GRAN (1000)
  141. #define CLK_GRAN_LIMIT (5)
  142. unsigned int qe_get_brg_clk(void)
  143. {
  144. struct device_node *qe;
  145. int size;
  146. const u32 *prop;
  147. unsigned int mod;
  148. if (brg_clk)
  149. return brg_clk;
  150. qe = of_find_compatible_node(NULL, NULL, "fsl,qe");
  151. if (!qe) {
  152. qe = of_find_node_by_type(NULL, "qe");
  153. if (!qe)
  154. return brg_clk;
  155. }
  156. prop = of_get_property(qe, "brg-frequency", &size);
  157. if (prop && size == sizeof(*prop))
  158. brg_clk = *prop;
  159. of_node_put(qe);
  160. /* round this if near to a multiple of CLK_GRAN */
  161. mod = brg_clk % CLK_GRAN;
  162. if (mod) {
  163. if (mod < CLK_GRAN_LIMIT)
  164. brg_clk -= mod;
  165. else if (mod > (CLK_GRAN - CLK_GRAN_LIMIT))
  166. brg_clk += CLK_GRAN - mod;
  167. }
  168. return brg_clk;
  169. }
  170. EXPORT_SYMBOL(qe_get_brg_clk);
  171. #define PVR_VER_836x 0x8083
  172. #define PVR_VER_832x 0x8084
  173. /* Program the BRG to the given sampling rate and multiplier
  174. *
  175. * @brg: the BRG, QE_BRG1 - QE_BRG16
  176. * @rate: the desired sampling rate
  177. * @multiplier: corresponds to the value programmed in GUMR_L[RDCR] or
  178. * GUMR_L[TDCR]. E.g., if this BRG is the RX clock, and GUMR_L[RDCR]=01,
  179. * then 'multiplier' should be 8.
  180. */
  181. int qe_setbrg(enum qe_clock brg, unsigned int rate, unsigned int multiplier)
  182. {
  183. u32 divisor, tempval;
  184. u32 div16 = 0;
  185. if ((brg < QE_BRG1) || (brg > QE_BRG16))
  186. return -EINVAL;
  187. divisor = qe_get_brg_clk() / (rate * multiplier);
  188. if (divisor > QE_BRGC_DIVISOR_MAX + 1) {
  189. div16 = QE_BRGC_DIV16;
  190. divisor /= 16;
  191. }
  192. /* Errata QE_General4, which affects some MPC832x and MPC836x SOCs, says
  193. that the BRG divisor must be even if you're not using divide-by-16
  194. mode. */
  195. if (pvr_version_is(PVR_VER_836x) || pvr_version_is(PVR_VER_832x))
  196. if (!div16 && (divisor & 1) && (divisor > 3))
  197. divisor++;
  198. tempval = ((divisor - 1) << QE_BRGC_DIVISOR_SHIFT) |
  199. QE_BRGC_ENABLE | div16;
  200. out_be32(&qe_immr->brg.brgc[brg - QE_BRG1], tempval);
  201. return 0;
  202. }
  203. EXPORT_SYMBOL(qe_setbrg);
  204. /* Convert a string to a QE clock source enum
  205. *
  206. * This function takes a string, typically from a property in the device
  207. * tree, and returns the corresponding "enum qe_clock" value.
  208. */
  209. enum qe_clock qe_clock_source(const char *source)
  210. {
  211. unsigned int i;
  212. if (strcasecmp(source, "none") == 0)
  213. return QE_CLK_NONE;
  214. if (strcmp(source, "tsync_pin") == 0)
  215. return QE_TSYNC_PIN;
  216. if (strcmp(source, "rsync_pin") == 0)
  217. return QE_RSYNC_PIN;
  218. if (strncasecmp(source, "brg", 3) == 0) {
  219. i = simple_strtoul(source + 3, NULL, 10);
  220. if ((i >= 1) && (i <= 16))
  221. return (QE_BRG1 - 1) + i;
  222. else
  223. return QE_CLK_DUMMY;
  224. }
  225. if (strncasecmp(source, "clk", 3) == 0) {
  226. i = simple_strtoul(source + 3, NULL, 10);
  227. if ((i >= 1) && (i <= 24))
  228. return (QE_CLK1 - 1) + i;
  229. else
  230. return QE_CLK_DUMMY;
  231. }
  232. return QE_CLK_DUMMY;
  233. }
  234. EXPORT_SYMBOL(qe_clock_source);
  235. /* Initialize SNUMs (thread serial numbers) according to
  236. * QE Module Control chapter, SNUM table
  237. */
  238. static void qe_snums_init(void)
  239. {
  240. int i;
  241. static const u8 snum_init_76[] = {
  242. 0x04, 0x05, 0x0C, 0x0D, 0x14, 0x15, 0x1C, 0x1D,
  243. 0x24, 0x25, 0x2C, 0x2D, 0x34, 0x35, 0x88, 0x89,
  244. 0x98, 0x99, 0xA8, 0xA9, 0xB8, 0xB9, 0xC8, 0xC9,
  245. 0xD8, 0xD9, 0xE8, 0xE9, 0x44, 0x45, 0x4C, 0x4D,
  246. 0x54, 0x55, 0x5C, 0x5D, 0x64, 0x65, 0x6C, 0x6D,
  247. 0x74, 0x75, 0x7C, 0x7D, 0x84, 0x85, 0x8C, 0x8D,
  248. 0x94, 0x95, 0x9C, 0x9D, 0xA4, 0xA5, 0xAC, 0xAD,
  249. 0xB4, 0xB5, 0xBC, 0xBD, 0xC4, 0xC5, 0xCC, 0xCD,
  250. 0xD4, 0xD5, 0xDC, 0xDD, 0xE4, 0xE5, 0xEC, 0xED,
  251. 0xF4, 0xF5, 0xFC, 0xFD,
  252. };
  253. static const u8 snum_init_46[] = {
  254. 0x04, 0x05, 0x0C, 0x0D, 0x14, 0x15, 0x1C, 0x1D,
  255. 0x24, 0x25, 0x2C, 0x2D, 0x34, 0x35, 0x88, 0x89,
  256. 0x98, 0x99, 0xA8, 0xA9, 0xB8, 0xB9, 0xC8, 0xC9,
  257. 0xD8, 0xD9, 0xE8, 0xE9, 0x08, 0x09, 0x18, 0x19,
  258. 0x28, 0x29, 0x38, 0x39, 0x48, 0x49, 0x58, 0x59,
  259. 0x68, 0x69, 0x78, 0x79, 0x80, 0x81,
  260. };
  261. static const u8 *snum_init;
  262. qe_num_of_snum = qe_get_num_of_snums();
  263. if (qe_num_of_snum == 76)
  264. snum_init = snum_init_76;
  265. else
  266. snum_init = snum_init_46;
  267. for (i = 0; i < qe_num_of_snum; i++) {
  268. snums[i].num = snum_init[i];
  269. snums[i].state = QE_SNUM_STATE_FREE;
  270. }
  271. }
  272. int qe_get_snum(void)
  273. {
  274. unsigned long flags;
  275. int snum = -EBUSY;
  276. int i;
  277. spin_lock_irqsave(&qe_lock, flags);
  278. for (i = 0; i < qe_num_of_snum; i++) {
  279. if (snums[i].state == QE_SNUM_STATE_FREE) {
  280. snums[i].state = QE_SNUM_STATE_USED;
  281. snum = snums[i].num;
  282. break;
  283. }
  284. }
  285. spin_unlock_irqrestore(&qe_lock, flags);
  286. return snum;
  287. }
  288. EXPORT_SYMBOL(qe_get_snum);
  289. void qe_put_snum(u8 snum)
  290. {
  291. int i;
  292. for (i = 0; i < qe_num_of_snum; i++) {
  293. if (snums[i].num == snum) {
  294. snums[i].state = QE_SNUM_STATE_FREE;
  295. break;
  296. }
  297. }
  298. }
  299. EXPORT_SYMBOL(qe_put_snum);
  300. static int qe_sdma_init(void)
  301. {
  302. struct sdma __iomem *sdma = &qe_immr->sdma;
  303. static unsigned long sdma_buf_offset = (unsigned long)-ENOMEM;
  304. if (!sdma)
  305. return -ENODEV;
  306. /* allocate 2 internal temporary buffers (512 bytes size each) for
  307. * the SDMA */
  308. if (IS_ERR_VALUE(sdma_buf_offset)) {
  309. sdma_buf_offset = qe_muram_alloc(512 * 2, 4096);
  310. if (IS_ERR_VALUE(sdma_buf_offset))
  311. return -ENOMEM;
  312. }
  313. out_be32(&sdma->sdebcr, (u32) sdma_buf_offset & QE_SDEBCR_BA_MASK);
  314. out_be32(&sdma->sdmr, (QE_SDMR_GLB_1_MSK |
  315. (0x1 << QE_SDMR_CEN_SHIFT)));
  316. return 0;
  317. }
  318. /* The maximum number of RISCs we support */
  319. #define MAX_QE_RISC 4
  320. /* Firmware information stored here for qe_get_firmware_info() */
  321. static struct qe_firmware_info qe_firmware_info;
  322. /*
  323. * Set to 1 if QE firmware has been uploaded, and therefore
  324. * qe_firmware_info contains valid data.
  325. */
  326. static int qe_firmware_uploaded;
  327. /*
  328. * Upload a QE microcode
  329. *
  330. * This function is a worker function for qe_upload_firmware(). It does
  331. * the actual uploading of the microcode.
  332. */
  333. static void qe_upload_microcode(const void *base,
  334. const struct qe_microcode *ucode)
  335. {
  336. const __be32 *code = base + be32_to_cpu(ucode->code_offset);
  337. unsigned int i;
  338. if (ucode->major || ucode->minor || ucode->revision)
  339. printk(KERN_INFO "qe-firmware: "
  340. "uploading microcode '%s' version %u.%u.%u\n",
  341. ucode->id, ucode->major, ucode->minor, ucode->revision);
  342. else
  343. printk(KERN_INFO "qe-firmware: "
  344. "uploading microcode '%s'\n", ucode->id);
  345. /* Use auto-increment */
  346. out_be32(&qe_immr->iram.iadd, be32_to_cpu(ucode->iram_offset) |
  347. QE_IRAM_IADD_AIE | QE_IRAM_IADD_BADDR);
  348. for (i = 0; i < be32_to_cpu(ucode->count); i++)
  349. out_be32(&qe_immr->iram.idata, be32_to_cpu(code[i]));
  350. /* Set I-RAM Ready Register */
  351. out_be32(&qe_immr->iram.iready, be32_to_cpu(QE_IRAM_READY));
  352. }
  353. /*
  354. * Upload a microcode to the I-RAM at a specific address.
  355. *
  356. * See Documentation/powerpc/qe_firmware.txt for information on QE microcode
  357. * uploading.
  358. *
  359. * Currently, only version 1 is supported, so the 'version' field must be
  360. * set to 1.
  361. *
  362. * The SOC model and revision are not validated, they are only displayed for
  363. * informational purposes.
  364. *
  365. * 'calc_size' is the calculated size, in bytes, of the firmware structure and
  366. * all of the microcode structures, minus the CRC.
  367. *
  368. * 'length' is the size that the structure says it is, including the CRC.
  369. */
  370. int qe_upload_firmware(const struct qe_firmware *firmware)
  371. {
  372. unsigned int i;
  373. unsigned int j;
  374. u32 crc;
  375. size_t calc_size = sizeof(struct qe_firmware);
  376. size_t length;
  377. const struct qe_header *hdr;
  378. if (!firmware) {
  379. printk(KERN_ERR "qe-firmware: invalid pointer\n");
  380. return -EINVAL;
  381. }
  382. hdr = &firmware->header;
  383. length = be32_to_cpu(hdr->length);
  384. /* Check the magic */
  385. if ((hdr->magic[0] != 'Q') || (hdr->magic[1] != 'E') ||
  386. (hdr->magic[2] != 'F')) {
  387. printk(KERN_ERR "qe-firmware: not a microcode\n");
  388. return -EPERM;
  389. }
  390. /* Check the version */
  391. if (hdr->version != 1) {
  392. printk(KERN_ERR "qe-firmware: unsupported version\n");
  393. return -EPERM;
  394. }
  395. /* Validate some of the fields */
  396. if ((firmware->count < 1) || (firmware->count > MAX_QE_RISC)) {
  397. printk(KERN_ERR "qe-firmware: invalid data\n");
  398. return -EINVAL;
  399. }
  400. /* Validate the length and check if there's a CRC */
  401. calc_size += (firmware->count - 1) * sizeof(struct qe_microcode);
  402. for (i = 0; i < firmware->count; i++)
  403. /*
  404. * For situations where the second RISC uses the same microcode
  405. * as the first, the 'code_offset' and 'count' fields will be
  406. * zero, so it's okay to add those.
  407. */
  408. calc_size += sizeof(__be32) *
  409. be32_to_cpu(firmware->microcode[i].count);
  410. /* Validate the length */
  411. if (length != calc_size + sizeof(__be32)) {
  412. printk(KERN_ERR "qe-firmware: invalid length\n");
  413. return -EPERM;
  414. }
  415. /* Validate the CRC */
  416. crc = be32_to_cpu(*(__be32 *)((void *)firmware + calc_size));
  417. if (crc != crc32(0, firmware, calc_size)) {
  418. printk(KERN_ERR "qe-firmware: firmware CRC is invalid\n");
  419. return -EIO;
  420. }
  421. /*
  422. * If the microcode calls for it, split the I-RAM.
  423. */
  424. if (!firmware->split)
  425. setbits16(&qe_immr->cp.cercr, QE_CP_CERCR_CIR);
  426. if (firmware->soc.model)
  427. printk(KERN_INFO
  428. "qe-firmware: firmware '%s' for %u V%u.%u\n",
  429. firmware->id, be16_to_cpu(firmware->soc.model),
  430. firmware->soc.major, firmware->soc.minor);
  431. else
  432. printk(KERN_INFO "qe-firmware: firmware '%s'\n",
  433. firmware->id);
  434. /*
  435. * The QE only supports one microcode per RISC, so clear out all the
  436. * saved microcode information and put in the new.
  437. */
  438. memset(&qe_firmware_info, 0, sizeof(qe_firmware_info));
  439. strlcpy(qe_firmware_info.id, firmware->id, sizeof(qe_firmware_info.id));
  440. qe_firmware_info.extended_modes = firmware->extended_modes;
  441. memcpy(qe_firmware_info.vtraps, firmware->vtraps,
  442. sizeof(firmware->vtraps));
  443. /* Loop through each microcode. */
  444. for (i = 0; i < firmware->count; i++) {
  445. const struct qe_microcode *ucode = &firmware->microcode[i];
  446. /* Upload a microcode if it's present */
  447. if (ucode->code_offset)
  448. qe_upload_microcode(firmware, ucode);
  449. /* Program the traps for this processor */
  450. for (j = 0; j < 16; j++) {
  451. u32 trap = be32_to_cpu(ucode->traps[j]);
  452. if (trap)
  453. out_be32(&qe_immr->rsp[i].tibcr[j], trap);
  454. }
  455. /* Enable traps */
  456. out_be32(&qe_immr->rsp[i].eccr, be32_to_cpu(ucode->eccr));
  457. }
  458. qe_firmware_uploaded = 1;
  459. return 0;
  460. }
  461. EXPORT_SYMBOL(qe_upload_firmware);
  462. /*
  463. * Get info on the currently-loaded firmware
  464. *
  465. * This function also checks the device tree to see if the boot loader has
  466. * uploaded a firmware already.
  467. */
  468. struct qe_firmware_info *qe_get_firmware_info(void)
  469. {
  470. static int initialized;
  471. struct property *prop;
  472. struct device_node *qe;
  473. struct device_node *fw = NULL;
  474. const char *sprop;
  475. unsigned int i;
  476. /*
  477. * If we haven't checked yet, and a driver hasn't uploaded a firmware
  478. * yet, then check the device tree for information.
  479. */
  480. if (qe_firmware_uploaded)
  481. return &qe_firmware_info;
  482. if (initialized)
  483. return NULL;
  484. initialized = 1;
  485. /*
  486. * Newer device trees have an "fsl,qe" compatible property for the QE
  487. * node, but we still need to support older device trees.
  488. */
  489. qe = of_find_compatible_node(NULL, NULL, "fsl,qe");
  490. if (!qe) {
  491. qe = of_find_node_by_type(NULL, "qe");
  492. if (!qe)
  493. return NULL;
  494. }
  495. /* Find the 'firmware' child node */
  496. for_each_child_of_node(qe, fw) {
  497. if (strcmp(fw->name, "firmware") == 0)
  498. break;
  499. }
  500. of_node_put(qe);
  501. /* Did we find the 'firmware' node? */
  502. if (!fw)
  503. return NULL;
  504. qe_firmware_uploaded = 1;
  505. /* Copy the data into qe_firmware_info*/
  506. sprop = of_get_property(fw, "id", NULL);
  507. if (sprop)
  508. strlcpy(qe_firmware_info.id, sprop,
  509. sizeof(qe_firmware_info.id));
  510. prop = of_find_property(fw, "extended-modes", NULL);
  511. if (prop && (prop->length == sizeof(u64))) {
  512. const u64 *iprop = prop->value;
  513. qe_firmware_info.extended_modes = *iprop;
  514. }
  515. prop = of_find_property(fw, "virtual-traps", NULL);
  516. if (prop && (prop->length == 32)) {
  517. const u32 *iprop = prop->value;
  518. for (i = 0; i < ARRAY_SIZE(qe_firmware_info.vtraps); i++)
  519. qe_firmware_info.vtraps[i] = iprop[i];
  520. }
  521. of_node_put(fw);
  522. return &qe_firmware_info;
  523. }
  524. EXPORT_SYMBOL(qe_get_firmware_info);
  525. unsigned int qe_get_num_of_risc(void)
  526. {
  527. struct device_node *qe;
  528. int size;
  529. unsigned int num_of_risc = 0;
  530. const u32 *prop;
  531. qe = of_find_compatible_node(NULL, NULL, "fsl,qe");
  532. if (!qe) {
  533. /* Older devices trees did not have an "fsl,qe"
  534. * compatible property, so we need to look for
  535. * the QE node by name.
  536. */
  537. qe = of_find_node_by_type(NULL, "qe");
  538. if (!qe)
  539. return num_of_risc;
  540. }
  541. prop = of_get_property(qe, "fsl,qe-num-riscs", &size);
  542. if (prop && size == sizeof(*prop))
  543. num_of_risc = *prop;
  544. of_node_put(qe);
  545. return num_of_risc;
  546. }
  547. EXPORT_SYMBOL(qe_get_num_of_risc);
  548. unsigned int qe_get_num_of_snums(void)
  549. {
  550. struct device_node *qe;
  551. int size;
  552. unsigned int num_of_snums;
  553. const u32 *prop;
  554. num_of_snums = 28; /* The default number of snum for threads is 28 */
  555. qe = of_find_compatible_node(NULL, NULL, "fsl,qe");
  556. if (!qe) {
  557. /* Older devices trees did not have an "fsl,qe"
  558. * compatible property, so we need to look for
  559. * the QE node by name.
  560. */
  561. qe = of_find_node_by_type(NULL, "qe");
  562. if (!qe)
  563. return num_of_snums;
  564. }
  565. prop = of_get_property(qe, "fsl,qe-num-snums", &size);
  566. if (prop && size == sizeof(*prop)) {
  567. num_of_snums = *prop;
  568. if ((num_of_snums < 28) || (num_of_snums > QE_NUM_OF_SNUM)) {
  569. /* No QE ever has fewer than 28 SNUMs */
  570. pr_err("QE: number of snum is invalid\n");
  571. of_node_put(qe);
  572. return -EINVAL;
  573. }
  574. }
  575. of_node_put(qe);
  576. return num_of_snums;
  577. }
  578. EXPORT_SYMBOL(qe_get_num_of_snums);
  579. static int __init qe_init(void)
  580. {
  581. struct device_node *np;
  582. np = of_find_compatible_node(NULL, NULL, "fsl,qe");
  583. if (!np)
  584. return -ENODEV;
  585. qe_reset();
  586. of_node_put(np);
  587. return 0;
  588. }
  589. subsys_initcall(qe_init);
  590. #if defined(CONFIG_SUSPEND) && defined(CONFIG_PPC_85xx)
  591. static int qe_resume(struct platform_device *ofdev)
  592. {
  593. if (!qe_alive_during_sleep())
  594. qe_reset();
  595. return 0;
  596. }
  597. static int qe_probe(struct platform_device *ofdev)
  598. {
  599. return 0;
  600. }
  601. static const struct of_device_id qe_ids[] = {
  602. { .compatible = "fsl,qe", },
  603. { },
  604. };
  605. static struct platform_driver qe_driver = {
  606. .driver = {
  607. .name = "fsl-qe",
  608. .of_match_table = qe_ids,
  609. },
  610. .probe = qe_probe,
  611. .resume = qe_resume,
  612. };
  613. builtin_platform_driver(qe_driver);
  614. #endif /* defined(CONFIG_SUSPEND) && defined(CONFIG_PPC_85xx) */