qe.c 14 KB

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