qe.c 16 KB

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