irq.c 35 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490
  1. #include <linux/io.h>
  2. #include <linux/module.h>
  3. #include <linux/smp.h>
  4. #include <linux/interrupt.h>
  5. #include <linux/cpu.h>
  6. #include <linux/notifier.h>
  7. #include <linux/irqchip/arm-gic.h>
  8. #include <linux/of.h>
  9. #include <linux/of_address.h>
  10. #include <linux/of_irq.h>
  11. #include <linux/irqdomain.h>
  12. #include <linux/device.h>
  13. #include <linux/platform_device.h>
  14. #if defined(CONFIG_FIQ_GLUE)
  15. #include <asm/fiq.h>
  16. #include <asm/fiq_glue.h>
  17. #endif
  18. #include <linux/irqchip/mt-gic.h>
  19. #include <mt-plat/sync_write.h>
  20. #include <mt-plat/aee.h>
  21. #include <mt-plat/mtk_ram_console.h>
  22. #include <mach/irqs.h>
  23. #include <mach/mt_secure_api.h>
  24. /*
  25. #include <mach/mt_spm_idle.h>
  26. */
  27. void __iomem *GIC_DIST_BASE;
  28. void __iomem *GIC_CPU_BASE;
  29. void __iomem *INT_POL_CTL0;
  30. phys_addr_t INT_POL_CTL0_phys;
  31. /*
  32. #define LDVT
  33. */
  34. #define GIC_ICDISR (GIC_DIST_BASE + 0x80)
  35. #define GIC_ICDISER0 (GIC_DIST_BASE + 0x100)
  36. #define GIC_ICDISER1 (GIC_DIST_BASE + 0x104)
  37. #define GIC_ICDISER2 (GIC_DIST_BASE + 0x108)
  38. #define GIC_ICDISER3 (GIC_DIST_BASE + 0x10C)
  39. #define GIC_ICDISER4 (GIC_DIST_BASE + 0x110)
  40. #define GIC_ICDISER5 (GIC_DIST_BASE + 0x114)
  41. #define GIC_ICDISER6 (GIC_DIST_BASE + 0x118)
  42. #define GIC_ICDISER7 (GIC_DIST_BASE + 0x11C)
  43. #define GIC_ICDISER8 (GIC_DIST_BASE + 0x120)
  44. #define GIC_ICDICER0 (GIC_DIST_BASE + 0x180)
  45. #define GIC_ICDICER1 (GIC_DIST_BASE + 0x184)
  46. #define GIC_ICDICER2 (GIC_DIST_BASE + 0x188)
  47. #define GIC_ICDICER3 (GIC_DIST_BASE + 0x18C)
  48. #define GIC_ICDICER4 (GIC_DIST_BASE + 0x190)
  49. #define GIC_ICDICER5 (GIC_DIST_BASE + 0x194)
  50. #define GIC_ICDICER6 (GIC_DIST_BASE + 0x198)
  51. #define GIC_ICDICER7 (GIC_DIST_BASE + 0x19C)
  52. #define GIC_ICDICER8 (GIC_DIST_BASE + 0x1A0)
  53. static spinlock_t irq_lock;
  54. /* irq_total_secondary_cpus will be initialized in smp_init_cpus() of mt-smp.c */
  55. unsigned int irq_total_secondary_cpus;
  56. #if defined(__CHECK_IRQ_TYPE)
  57. #define X_DEFINE_IRQ(__name, __num, __polarity, __sensitivity) \
  58. { .num = __num, .polarity = __polarity, .sensitivity = __sensitivity, },
  59. #define L 0
  60. #define H 1
  61. #define EDGE MT_EDGE_SENSITIVE
  62. #define LEVEL MT_LEVEL_SENSITIVE
  63. struct __check_irq_type {
  64. int num;
  65. int polarity;
  66. int sensitivity;
  67. };
  68. struct __check_irq_type __check_irq_type[] = {
  69. #include <mach/x_define_irq.h>
  70. {.num = -1,},
  71. };
  72. #undef X_DEFINE_IRQ
  73. #undef L
  74. #undef H
  75. #undef EDGE
  76. #undef LEVEL
  77. #endif
  78. #ifdef CONFIG_OF
  79. /*
  80. * This macro must be used by the different irqchip drivers to declare
  81. * the association between their DT compatible string and their
  82. * initialization function.
  83. *
  84. * @name: name that must be unique accross all IRQCHIP_DECLARE of the
  85. * same file.
  86. * @compstr: compatible string of the irqchip driver
  87. * @fn: initialization function
  88. */
  89. #define IRQCHIP_DECLARE(name, compstr, fn) \
  90. static const struct of_device_id irqchip_of_match_##name \
  91. __used __section(__irqchip_of_table) \
  92. = { .compatible = compstr, .data = fn }
  93. #endif
  94. /*
  95. * mt_irq_mask: enable an interrupt.
  96. * @data: irq_data
  97. */
  98. void mt_irq_mask(struct irq_data *data)
  99. {
  100. const unsigned int irq = data->irq;
  101. u32 mask = 1 << (irq % 32);
  102. if (irq < NR_GIC_SGI) {
  103. /*Note: workaround for false alarm:"Fail to disable interrupt 14" */
  104. if (irq != FIQ_DBG_SGI)
  105. pr_err("Fail to disable interrupt %d\n", irq);
  106. return;
  107. }
  108. writel(mask, IOMEM(GIC_DIST_BASE + GIC_DIST_ENABLE_CLEAR + irq / 32 * 4));
  109. }
  110. /*
  111. * mt_irq_unmask: disable an interrupt.
  112. * @data: irq_data
  113. */
  114. void mt_irq_unmask(struct irq_data *data)
  115. {
  116. const unsigned int irq = data->irq;
  117. u32 mask = 1 << (irq % 32);
  118. if (irq < NR_GIC_SGI) {
  119. /*Note: workaround for false alarm:"Fail to enable interrupt 14" */
  120. if (irq != FIQ_DBG_SGI)
  121. pr_err("Fail to enable interrupt %d\n", irq);
  122. return;
  123. }
  124. writel(mask, IOMEM(GIC_DIST_BASE + GIC_DIST_ENABLE_SET + irq / 32 * 4));
  125. }
  126. /*
  127. * mt_irq_ack: acknowledge an interrupt
  128. * @data: irq_data
  129. */
  130. static void mt_irq_ack(struct irq_data *data)
  131. {
  132. u32 irq = data->irq;
  133. #if defined(CONFIG_FIQ_GLUE)
  134. writel(irq, IOMEM(GIC_CPU_BASE + GIC_CPU_AEOI));
  135. #else
  136. writel(irq, IOMEM(GIC_CPU_BASE + GIC_CPU_EOI));
  137. #endif
  138. }
  139. /*
  140. * mt_irq_set_sens: set the interrupt sensitivity
  141. * @irq: interrupt id
  142. * @sens: sensitivity
  143. */
  144. void mt_irq_set_sens(unsigned int irq, unsigned int sens)
  145. {
  146. unsigned long flags;
  147. u32 config;
  148. if (irq < (NR_GIC_SGI + NR_GIC_PPI)) {
  149. pr_err("Fail to set sensitivity of interrupt %d\n", irq);
  150. return;
  151. }
  152. spin_lock_irqsave(&irq_lock, flags);
  153. if (sens == MT_EDGE_SENSITIVE) {
  154. config = readl(IOMEM(GIC_DIST_BASE + GIC_DIST_CONFIG + (irq / 16) * 4));
  155. config |= (0x2 << (irq % 16) * 2);
  156. writel(config, IOMEM(GIC_DIST_BASE + GIC_DIST_CONFIG + (irq / 16) * 4));
  157. } else {
  158. config = readl(IOMEM(GIC_DIST_BASE + GIC_DIST_CONFIG + (irq / 16) * 4));
  159. config &= ~(0x2 << (irq % 16) * 2);
  160. writel(config, IOMEM(GIC_DIST_BASE + GIC_DIST_CONFIG + (irq / 16) * 4));
  161. }
  162. spin_unlock_irqrestore(&irq_lock, flags);
  163. mb();
  164. }
  165. EXPORT_SYMBOL(mt_irq_set_sens);
  166. /*
  167. * mt_irq_set_polarity: set the interrupt polarity
  168. * @irq: interrupt id
  169. * @polarity: interrupt polarity
  170. */
  171. void mt_irq_set_polarity(unsigned int irq, unsigned int polarity)
  172. {
  173. unsigned long flags;
  174. u32 offset, reg_index, value;
  175. if (irq < (NR_GIC_SGI + NR_GIC_PPI)) {
  176. pr_err("Fail to set polarity of interrupt %d\n", irq);
  177. return;
  178. }
  179. offset = (irq - GIC_PRIVATE_SIGNALS) & 0x1F;
  180. reg_index = (irq - GIC_PRIVATE_SIGNALS) >> 5;
  181. spin_lock_irqsave(&irq_lock, flags);
  182. if (polarity == 0) {
  183. /* active low */
  184. value = readl(IOMEM(INT_POL_CTL0 + (reg_index * 4)));
  185. value |= (1 << offset);
  186. #if defined(CONFIG_ARM_PSCI) || defined(CONFIG_MTK_PSCI)
  187. mcusys_smc_write_phy(INT_POL_CTL0_phys + (reg_index * 4), value);
  188. #else
  189. mcusys_smc_write((INT_POL_CTL0 + (reg_index * 4)), value);
  190. #endif
  191. } else {
  192. /* active high */
  193. value = readl(IOMEM(INT_POL_CTL0 + (reg_index * 4)));
  194. value &= ~(0x1 << offset);
  195. #if defined(CONFIG_ARM_PSCI) || defined(CONFIG_MTK_PSCI)
  196. mcusys_smc_write_phy(INT_POL_CTL0_phys + (reg_index * 4), value);
  197. #else
  198. mcusys_smc_write((INT_POL_CTL0 + (reg_index * 4)), value);
  199. #endif
  200. }
  201. spin_unlock_irqrestore(&irq_lock, flags);
  202. }
  203. EXPORT_SYMBOL(mt_irq_set_polarity);
  204. /*
  205. * mt_irq_set_type: set interrupt type
  206. * @irq: interrupt id
  207. * @flow_type: interrupt type
  208. * Always return 0.
  209. */
  210. static int mt_irq_set_type(struct irq_data *data, unsigned int flow_type)
  211. {
  212. const unsigned int irq = data->irq;
  213. #if defined(__CHECK_IRQ_TYPE)
  214. if (irq > (NR_GIC_SGI + NR_GIC_PPI)) {
  215. int i = 0;
  216. while (__check_irq_type[i].num >= 0) {
  217. if (__check_irq_type[i].num == irq) {
  218. if (flow_type & (IRQF_TRIGGER_RISING | IRQF_TRIGGER_FALLING)) {
  219. if (__check_irq_type[i].sensitivity != MT_EDGE_SENSITIVE)
  220. pr_err("irq_set_type_error: level-sensitive interrupt %d is set as edge-trigger\n",
  221. irq);
  222. } else if (flow_type & (IRQF_TRIGGER_HIGH | IRQF_TRIGGER_LOW)) {
  223. if (__check_irq_type[i].sensitivity != MT_LEVEL_SENSITIVE)
  224. pr_err("irq_set_type_error: edge-trigger interrupt %d is set as level-sensitive\n",
  225. irq);
  226. }
  227. if (flow_type & (IRQF_TRIGGER_RISING | IRQF_TRIGGER_HIGH)) {
  228. if (__check_irq_type[i].polarity != 1)
  229. pr_err("irq_set_type error: active-low interrupt %d is set as active-high\n",
  230. irq);
  231. } else if (flow_type & (IRQF_TRIGGER_FALLING | IRQF_TRIGGER_LOW)) {
  232. if (__check_irq_type[i].polarity != 0)
  233. pr_err("irq_set_type error: active-high interrupt %d is set as active-low\n",
  234. irq);
  235. }
  236. break;
  237. }
  238. i++;
  239. }
  240. if (__check_irq_type[i].num < 0)
  241. pr_err("irq_set_type error: unknown interrupt %d is configured\n", irq);
  242. }
  243. #endif
  244. if (flow_type & (IRQF_TRIGGER_RISING | IRQF_TRIGGER_FALLING)) {
  245. mt_irq_set_sens(irq, MT_EDGE_SENSITIVE);
  246. mt_irq_set_polarity(irq, (flow_type & IRQF_TRIGGER_FALLING) ? 0 : 1);
  247. __irq_set_handler_locked(irq, handle_edge_irq);
  248. } else if (flow_type & (IRQF_TRIGGER_HIGH | IRQF_TRIGGER_LOW)) {
  249. mt_irq_set_sens(irq, MT_LEVEL_SENSITIVE);
  250. mt_irq_set_polarity(irq, (flow_type & IRQF_TRIGGER_LOW) ? 0 : 1);
  251. __irq_set_handler_locked(irq, handle_level_irq);
  252. }
  253. return 0;
  254. }
  255. static struct irq_chip mt_irq_chip = {
  256. .irq_disable = mt_irq_mask,
  257. .irq_enable = mt_irq_unmask,
  258. .irq_ack = mt_irq_ack,
  259. .irq_mask = mt_irq_mask,
  260. .irq_unmask = mt_irq_unmask,
  261. .irq_set_type = mt_irq_set_type,
  262. };
  263. #ifdef CONFIG_OF
  264. int mt_get_supported_irq_num(void)
  265. {
  266. int ret = 0;
  267. if (GIC_DIST_BASE) {
  268. ret = ((readl_relaxed(GIC_DIST_BASE + GIC_DIST_CTR) & 0x1f) + 1) * 32;
  269. pr_debug("gic supported max = %d\n", ret);
  270. } else
  271. pr_warn("gic dist_base is unknown\n");
  272. return ret;
  273. }
  274. EXPORT_SYMBOL(mt_get_supported_irq_num);
  275. static int mt_gic_irq_domain_map(struct irq_domain *d, unsigned int irq, irq_hw_number_t hw)
  276. {
  277. if (hw < 32) {
  278. irq_set_percpu_devid(irq);
  279. irq_set_chip_and_handler(irq, &mt_irq_chip, handle_percpu_devid_irq);
  280. set_irq_flags(irq, IRQF_VALID | IRQF_NOAUTOEN);
  281. } else {
  282. irq_set_chip_and_handler(irq, &mt_irq_chip, handle_level_irq);
  283. set_irq_flags(irq, IRQF_VALID | IRQF_PROBE);
  284. }
  285. return 0;
  286. }
  287. static int mt_gic_irq_domain_xlate(struct irq_domain *d, struct device_node *controller,
  288. const u32 *intspec, unsigned int intsize,
  289. unsigned long *out_hwirq, unsigned int *out_type)
  290. {
  291. if (d->of_node != controller)
  292. return -EINVAL;
  293. if (intsize < 3)
  294. return -EINVAL;
  295. /* Get the interrupt number and add 16 to skip over SGIs */
  296. *out_hwirq = intspec[1] + 16;
  297. /* For SPIs, we need to add 16 more to get the GIC irq ID number */
  298. if (!intspec[0])
  299. *out_hwirq += 16;
  300. *out_type = intspec[2] & IRQ_TYPE_SENSE_MASK;
  301. return 0;
  302. }
  303. const struct irq_domain_ops mt_gic_irq_domain_ops = {
  304. .map = mt_gic_irq_domain_map,
  305. .xlate = mt_gic_irq_domain_xlate,
  306. };
  307. #endif
  308. static void mt_gic_dist_init(void)
  309. {
  310. unsigned int i;
  311. #ifndef MTK_FORCE_CLUSTER1
  312. u32 cpumask = 1 << smp_processor_id();
  313. #else
  314. u32 cpumask = 1 << 4;
  315. #endif
  316. cpumask |= cpumask << 8;
  317. cpumask |= cpumask << 16;
  318. writel(0, IOMEM(GIC_DIST_BASE + GIC_DIST_CTRL));
  319. /*
  320. * Set all global interrupts to be level triggered, active low.
  321. */
  322. for (i = 32; i < (MT_NR_SPI + 32); i += 16)
  323. writel(0, IOMEM(GIC_DIST_BASE + GIC_DIST_CONFIG + i * 4 / 16));
  324. /*
  325. * Set all global interrupts to this CPU only.
  326. */
  327. for (i = 32; i < (MT_NR_SPI + 32); i += 4)
  328. writel(cpumask, IOMEM(GIC_DIST_BASE + GIC_DIST_TARGET + i * 4 / 4));
  329. /*
  330. * Set priority on all global interrupts.
  331. */
  332. for (i = 32; i < NR_MT_IRQ_LINE; i += 4)
  333. writel(0xA0A0A0A0, IOMEM(GIC_DIST_BASE + GIC_DIST_PRI + i * 4 / 4));
  334. /*
  335. * Disable all global interrupts.
  336. */
  337. for (i = 32; i < NR_MT_IRQ_LINE; i += 32)
  338. writel(0xFFFFFFFF, IOMEM(GIC_DIST_BASE + GIC_DIST_ENABLE_CLEAR + i * 4 / 32));
  339. #ifndef CONFIG_OF
  340. /*
  341. * Setup the Linux IRQ subsystem.
  342. */
  343. for (i = GIC_PPI_OFFSET; i < NR_MT_IRQ_LINE; i++) {
  344. if (i == GIC_PPI_PRIVATE_TIMER || i == GIC_PPI_WATCHDOG_TIMER) {
  345. irq_set_percpu_devid(i);
  346. irq_set_chip_and_handler(i, &mt_irq_chip, handle_percpu_devid_irq);
  347. set_irq_flags(i, IRQF_VALID | IRQF_NOAUTOEN);
  348. } else {
  349. irq_set_chip_and_handler(i, &mt_irq_chip, handle_level_irq);
  350. set_irq_flags(i, IRQF_VALID | IRQF_PROBE);
  351. }
  352. }
  353. #endif
  354. #ifdef CONFIG_FIQ_DEBUGGER
  355. irq_set_chip_and_handler(FIQ_DBG_SGI, &mt_irq_chip, handle_level_irq);
  356. set_irq_flags(FIQ_DBG_SGI, IRQF_VALID | IRQF_PROBE);
  357. #endif
  358. #if defined(CONFIG_FIQ_GLUE)
  359. /* when enable FIQ
  360. * set all global interrupts as non-secure interrupts
  361. */
  362. for (i = 32; i < NR_IRQS; i += 32)
  363. writel(0xFFFFFFFF, IOMEM(GIC_ICDISR + 4 * (i / 32)));
  364. #endif
  365. /*
  366. * enable secure and non-secure interrupts on Distributor
  367. */
  368. writel(3, IOMEM(GIC_DIST_BASE + GIC_DIST_CTRL));
  369. }
  370. static void mt_gic_cpu_init(void)
  371. {
  372. int i;
  373. /*
  374. * Deal with the banked PPI and SGI interrupts - disable all
  375. * PPI interrupts, ensure all SGI interrupts are enabled.
  376. */
  377. writel(0xffff0000, IOMEM(GIC_DIST_BASE + GIC_DIST_ENABLE_CLEAR));
  378. writel(0x0000ffff, IOMEM(GIC_DIST_BASE + GIC_DIST_ENABLE_SET));
  379. /* set priority on PPI and SGI interrupts */
  380. for (i = 0; i < 32; i += 4)
  381. writel(0x80808080, IOMEM(GIC_DIST_BASE + GIC_DIST_PRI + i * 4 / 4));
  382. #if defined(CONFIG_FIQ_GLUE)
  383. /* when enable FIQ */
  384. /* set PPI and SGI interrupts as non-secure interrupts */
  385. writel(0xFFFFFFFF, IOMEM(GIC_ICDISR));
  386. #endif
  387. writel(0xF0, IOMEM(GIC_CPU_BASE + GIC_CPU_PRIMASK));
  388. #if defined(CONFIG_FIQ_GLUE)
  389. /* enable SBPR, FIQEn, EnableNS and EnableS */
  390. /* SBPR=1, IRQ and FIQ use the same BPR bit */
  391. /* FIQEn=1,forward group 0 interrupts using the FIQ signal
  392. * GIC always signals grp1 interrupt using IRQ
  393. * */
  394. /* EnableGrp1=1, enable signaling of grp 1 */
  395. /* EnableGrp0=1, enable signaling of grp 0 */
  396. writel(0x1B, IOMEM(GIC_CPU_BASE + GIC_CPU_CTRL));
  397. #else
  398. /* enable SBPR, EnableNS and EnableS */
  399. writel(0x13, IOMEM(GIC_CPU_BASE + GIC_CPU_CTRL));
  400. #endif
  401. mb();
  402. }
  403. void __cpuinit mt_gic_secondary_init(void)
  404. {
  405. mt_gic_cpu_init();
  406. }
  407. void irq_raise_softirq(const struct cpumask *mask, unsigned int irq)
  408. {
  409. unsigned long map = *cpus_addr(*mask);
  410. int satt, cpu, cpu_bmask;
  411. u32 val;
  412. satt = 1 << 15;
  413. /*
  414. * NoteXXX: CPU1 SGI is configured as secure as default.
  415. * Need to use the secure SGI 1 which is for waking up cpu1.
  416. */
  417. if (irq == CPU_BRINGUP_SGI) {
  418. if (irq_total_secondary_cpus) {
  419. --irq_total_secondary_cpus;
  420. satt = 0;
  421. }
  422. }
  423. val = readl(IOMEM(GIC_ICDISR + 4 * (irq / 32)));
  424. if (!(val & (1 << (irq % 32)))) { /* secure interrupt? */
  425. satt = 0;
  426. }
  427. cpu = 0;
  428. cpu_bmask = 0;
  429. /*
  430. * Ensure that stores to Normal memory are visible to the
  431. * other CPUs before issuing the IPI.
  432. */
  433. mb();
  434. #ifndef MTK_FORCE_CLUSTER1
  435. writel((map << 16) | satt | irq, IOMEM((GIC_DIST_BASE + 0xf00)));
  436. #else
  437. writel((map << (16 + 4)) | satt | irq, IOMEM((GIC_DIST_BASE + 0xf00)));
  438. #endif
  439. }
  440. int mt_irq_is_active(const unsigned int irq)
  441. {
  442. const unsigned int iActive = readl(IOMEM(GIC_DIST_BASE + 0x200 + irq / 32 * 4));
  443. return iActive & (1 << (irq % 32)) ? 1 : 0;
  444. }
  445. /*
  446. * mt_enable_ppi: enable a private peripheral interrupt
  447. * @irq: interrupt id
  448. */
  449. void mt_enable_ppi(int irq)
  450. {
  451. u32 mask = 1 << (irq % 32);
  452. if (irq < NR_GIC_SGI) {
  453. pr_err("Fail to enable PPI %d\n", irq);
  454. return;
  455. }
  456. if (irq >= (NR_GIC_SGI + NR_GIC_PPI)) {
  457. pr_err("Fail to enable PPI %d\n", irq);
  458. return;
  459. }
  460. writel(mask, IOMEM(GIC_DIST_BASE + GIC_DIST_ENABLE_SET + irq / 32 * 4));
  461. }
  462. /*
  463. * mt_PPI_mask_all: disable all PPI interrupts
  464. * @mask: pointer to struct mtk_irq_mask for storing the original mask value.
  465. * Return 0 for success; return negative values for failure.
  466. */
  467. int mt_PPI_mask_all(struct mtk_irq_mask *mask)
  468. {
  469. unsigned long flags;
  470. if (mask) {
  471. #if defined(CONFIG_FIQ_GLUE)
  472. local_fiq_disable();
  473. #endif
  474. spin_lock_irqsave(&irq_lock, flags);
  475. mask->mask0 = readl(IOMEM(GIC_ICDISER0));
  476. mt_reg_sync_writel(0xFFFFFFFF, GIC_ICDICER0);
  477. spin_unlock_irqrestore(&irq_lock, flags);
  478. #if defined(CONFIG_FIQ_GLUE)
  479. local_fiq_enable();
  480. #endif
  481. mask->header = IRQ_MASK_HEADER;
  482. mask->footer = IRQ_MASK_FOOTER;
  483. return 0;
  484. } else {
  485. return -1;
  486. }
  487. }
  488. /*
  489. * mt_PPI_mask_restore: restore all PPI interrupts
  490. * @mask: pointer to struct mtk_irq_mask for storing the original mask value.
  491. * Return 0 for success; return negative values for failure.
  492. */
  493. int mt_PPI_mask_restore(struct mtk_irq_mask *mask)
  494. {
  495. unsigned long flags;
  496. if (!mask)
  497. return -1;
  498. if (mask->header != IRQ_MASK_HEADER)
  499. return -1;
  500. if (mask->footer != IRQ_MASK_FOOTER)
  501. return -1;
  502. #if defined(CONFIG_FIQ_GLUE)
  503. local_fiq_disable();
  504. #endif
  505. spin_lock_irqsave(&irq_lock, flags);
  506. mt_reg_sync_writel(mask->mask0, GIC_ICDISER0);
  507. spin_unlock_irqrestore(&irq_lock, flags);
  508. #if defined(CONFIG_FIQ_GLUE)
  509. local_fiq_enable();
  510. #endif
  511. return 0;
  512. }
  513. /*
  514. * mt_SPI_mask_all: disable all SPI interrupts
  515. * @mask: pointer to struct mtk_irq_mask for storing the original mask value.
  516. * Return 0 for success; return negative values for failure.
  517. */
  518. int mt_SPI_mask_all(struct mtk_irq_mask *mask)
  519. {
  520. unsigned long flags;
  521. if (mask) {
  522. #if defined(CONFIG_FIQ_GLUE)
  523. local_fiq_disable();
  524. #endif
  525. spin_lock_irqsave(&irq_lock, flags);
  526. mask->mask1 = readl(IOMEM(GIC_ICDISER1));
  527. mask->mask2 = readl(IOMEM(GIC_ICDISER2));
  528. mask->mask3 = readl(IOMEM(GIC_ICDISER3));
  529. mask->mask4 = readl(IOMEM(GIC_ICDISER4));
  530. mask->mask5 = readl(IOMEM(GIC_ICDISER5));
  531. mask->mask6 = readl(IOMEM(GIC_ICDISER6));
  532. mask->mask7 = readl(IOMEM(GIC_ICDISER7));
  533. mask->mask8 = readl(IOMEM(GIC_ICDISER8));
  534. writel(0xFFFFFFFF, IOMEM(GIC_ICDICER1));
  535. writel(0xFFFFFFFF, IOMEM(GIC_ICDICER2));
  536. writel(0xFFFFFFFF, IOMEM(GIC_ICDICER3));
  537. writel(0xFFFFFFFF, IOMEM(GIC_ICDICER4));
  538. writel(0xFFFFFFFF, IOMEM(GIC_ICDICER5));
  539. writel(0xFFFFFFFF, IOMEM(GIC_ICDICER6));
  540. writel(0xFFFFFFFF, IOMEM(GIC_ICDICER7));
  541. mt_reg_sync_writel(0xFFFFFFFF, GIC_ICDICER8);
  542. spin_unlock_irqrestore(&irq_lock, flags);
  543. #if defined(CONFIG_FIQ_GLUE)
  544. local_fiq_enable();
  545. #endif
  546. mask->header = IRQ_MASK_HEADER;
  547. mask->footer = IRQ_MASK_FOOTER;
  548. return 0;
  549. } else {
  550. return -1;
  551. }
  552. }
  553. /*
  554. * mt_SPI_mask_restore: restore all SPI interrupts
  555. * @mask: pointer to struct mtk_irq_mask for storing the original mask value.
  556. * Return 0 for success; return negative values for failure.
  557. */
  558. int mt_SPI_mask_restore(struct mtk_irq_mask *mask)
  559. {
  560. unsigned long flags;
  561. if (!mask)
  562. return -1;
  563. if (mask->header != IRQ_MASK_HEADER)
  564. return -1;
  565. if (mask->footer != IRQ_MASK_FOOTER)
  566. return -1;
  567. #if defined(CONFIG_FIQ_GLUE)
  568. local_fiq_disable();
  569. #endif
  570. spin_lock_irqsave(&irq_lock, flags);
  571. writel(mask->mask1, IOMEM(GIC_ICDISER1));
  572. writel(mask->mask2, IOMEM(GIC_ICDISER2));
  573. writel(mask->mask3, IOMEM(GIC_ICDISER3));
  574. writel(mask->mask4, IOMEM(GIC_ICDISER4));
  575. writel(mask->mask5, IOMEM(GIC_ICDISER5));
  576. writel(mask->mask6, IOMEM(GIC_ICDISER6));
  577. writel(mask->mask7, IOMEM(GIC_ICDISER7));
  578. mt_reg_sync_writel(mask->mask8, GIC_ICDISER8);
  579. spin_unlock_irqrestore(&irq_lock, flags);
  580. #if defined(CONFIG_FIQ_GLUE)
  581. local_fiq_enable();
  582. #endif
  583. return 0;
  584. }
  585. /*
  586. * mt_irq_mask_all: disable all interrupts
  587. * @mask: pointer to struct mtk_irq_mask for storing the original mask value.
  588. * Return 0 for success; return negative values for failure.
  589. * (This is ONLY used for the idle current measurement by the factory mode.)
  590. */
  591. int mt_irq_mask_all(struct mtk_irq_mask *mask)
  592. {
  593. unsigned long flags;
  594. if (mask) {
  595. #if defined(CONFIG_FIQ_GLUE)
  596. local_fiq_disable();
  597. #endif
  598. spin_lock_irqsave(&irq_lock, flags);
  599. mask->mask0 = readl(IOMEM(GIC_ICDISER0));
  600. mask->mask1 = readl(IOMEM(GIC_ICDISER1));
  601. mask->mask2 = readl(IOMEM(GIC_ICDISER2));
  602. mask->mask3 = readl(IOMEM(GIC_ICDISER3));
  603. mask->mask4 = readl(IOMEM(GIC_ICDISER4));
  604. mask->mask5 = readl(IOMEM(GIC_ICDISER5));
  605. mask->mask6 = readl(IOMEM(GIC_ICDISER6));
  606. mask->mask7 = readl(IOMEM(GIC_ICDISER7));
  607. mask->mask8 = readl(IOMEM(GIC_ICDISER8));
  608. writel(0xFFFFFFFF, IOMEM(GIC_ICDICER0));
  609. writel(0xFFFFFFFF, IOMEM(GIC_ICDICER1));
  610. writel(0xFFFFFFFF, IOMEM(GIC_ICDICER2));
  611. writel(0xFFFFFFFF, IOMEM(GIC_ICDICER3));
  612. writel(0xFFFFFFFF, IOMEM(GIC_ICDICER4));
  613. writel(0xFFFFFFFF, IOMEM(GIC_ICDICER5));
  614. writel(0xFFFFFFFF, IOMEM(GIC_ICDICER6));
  615. writel(0xFFFFFFFF, IOMEM(GIC_ICDICER7));
  616. mt_reg_sync_writel(0xFFFFFFFF, GIC_ICDICER8);
  617. spin_unlock_irqrestore(&irq_lock, flags);
  618. #if defined(CONFIG_FIQ_GLUE)
  619. local_fiq_enable();
  620. #endif
  621. mask->header = IRQ_MASK_HEADER;
  622. mask->footer = IRQ_MASK_FOOTER;
  623. return 0;
  624. } else {
  625. return -1;
  626. }
  627. }
  628. /*
  629. * mt_irq_mask_restore: restore all interrupts
  630. * @mask: pointer to struct mtk_irq_mask for storing the original mask value.
  631. * Return 0 for success; return negative values for failure.
  632. * (This is ONLY used for the idle current measurement by the factory mode.)
  633. */
  634. int mt_irq_mask_restore(struct mtk_irq_mask *mask)
  635. {
  636. unsigned long flags;
  637. if (!mask)
  638. return -1;
  639. if (mask->header != IRQ_MASK_HEADER)
  640. return -1;
  641. if (mask->footer != IRQ_MASK_FOOTER)
  642. return -1;
  643. #if defined(CONFIG_FIQ_GLUE)
  644. local_fiq_disable();
  645. #endif
  646. spin_lock_irqsave(&irq_lock, flags);
  647. writel(mask->mask0, IOMEM(GIC_ICDISER0));
  648. writel(mask->mask1, IOMEM(GIC_ICDISER1));
  649. writel(mask->mask2, IOMEM(GIC_ICDISER2));
  650. writel(mask->mask3, IOMEM(GIC_ICDISER3));
  651. writel(mask->mask4, IOMEM(GIC_ICDISER4));
  652. writel(mask->mask5, IOMEM(GIC_ICDISER5));
  653. writel(mask->mask6, IOMEM(GIC_ICDISER6));
  654. writel(mask->mask7, IOMEM(GIC_ICDISER7));
  655. mt_reg_sync_writel(mask->mask8, GIC_ICDISER8);
  656. spin_unlock_irqrestore(&irq_lock, flags);
  657. #if defined(CONFIG_FIQ_GLUE)
  658. local_fiq_enable();
  659. #endif
  660. return 0;
  661. }
  662. /*
  663. #define GIC_DIST_PENDING_SET 0x200
  664. #define GIC_DIST_PENDING_CLEAR 0x280
  665. * mt_irq_set_pending_for_sleep: pending an interrupt for the sleep manager's use
  666. * @irq: interrupt id
  667. * (THIS IS ONLY FOR SLEEP FUNCTION USE. DO NOT USE IT YOURSELF!)
  668. */
  669. void mt_irq_set_pending_for_sleep(unsigned int irq)
  670. {
  671. u32 mask = 1 << (irq % 32);
  672. if (irq < NR_GIC_SGI) {
  673. pr_err("Fail to set a pending on interrupt %d\n", irq);
  674. return;
  675. }
  676. writel(mask, IOMEM(GIC_DIST_BASE + GIC_DIST_PENDING_SET + irq / 32 * 4));
  677. pr_err("irq:%d, 0x%p=0x%x\n", irq, GIC_DIST_BASE + GIC_DIST_PENDING_SET + irq / 32 * 4,
  678. mask);
  679. }
  680. /*
  681. * mt_irq_unmask_for_sleep: enable an interrupt for the sleep manager's use
  682. * @irq: interrupt id
  683. * (THIS IS ONLY FOR SLEEP FUNCTION USE. DO NOT USE IT YOURSELF!)
  684. */
  685. void mt_irq_unmask_for_sleep(unsigned int irq)
  686. {
  687. u32 mask = 1 << (irq % 32);
  688. if (irq < NR_GIC_SGI) {
  689. pr_err("Fail to enable interrupt %d\n", irq);
  690. return;
  691. }
  692. writel(mask, IOMEM(GIC_DIST_BASE + GIC_DIST_ENABLE_SET + irq / 32 * 4));
  693. }
  694. /*
  695. * mt_irq_mask_for_sleep: disable an interrupt for the sleep manager's use
  696. * @irq: interrupt id
  697. * (THIS IS ONLY FOR SLEEP FUNCTION USE. DO NOT USE IT YOURSELF!)
  698. */
  699. void mt_irq_mask_for_sleep(unsigned int irq)
  700. {
  701. u32 mask = 1 << (irq % 32);
  702. if (irq < NR_GIC_SGI) {
  703. pr_err("Fail to enable interrupt %d\n", irq);
  704. return;
  705. }
  706. writel(mask, IOMEM(GIC_DIST_BASE + GIC_DIST_ENABLE_CLEAR + irq / 32 * 4));
  707. }
  708. #if defined(CONFIG_FIQ_GLUE)
  709. struct irq2fiq {
  710. int irq;
  711. fiq_isr_handler handler;
  712. void *arg;
  713. };
  714. static struct irq2fiq irqs_to_fiq[] = {
  715. {.irq = UART0_IRQ_BIT_ID,},
  716. {.irq = UART1_IRQ_BIT_ID,},
  717. {.irq = UART2_IRQ_BIT_ID,},
  718. {.irq = UART3_IRQ_BIT_ID,},
  719. {.irq = WDT_IRQ_BIT_ID,},
  720. /* FIX-ME for early porting
  721. { .irq = MT_SPM1_IRQ_ID, },
  722. */
  723. {.irq = GIC_PPI_WATCHDOG_TIMER,},
  724. {.irq = FIQ_SMP_CALL_SGI,}
  725. };
  726. struct fiq_isr_log {
  727. int in_fiq_isr;
  728. int irq;
  729. int smp_call_cnt;
  730. };
  731. static struct fiq_isr_log fiq_isr_logs[NR_CPUS];
  732. static int fiq_glued;
  733. /*
  734. * trigger_sw_irq: force to trigger a GIC SGI.
  735. * @irq: SGI number
  736. */
  737. void trigger_sw_irq(int irq)
  738. {
  739. if (irq < NR_GIC_SGI) {
  740. #ifndef MTK_FORCE_CLUSTER1
  741. writel((0x1 << 16) | (0x1 << 15) | irq, IOMEM(GIC_DIST_BASE + GIC_DIST_SOFTINT));
  742. #else
  743. writel((0x1 << (16 + 4)) | (0x1 << 15) | irq, IOMEM(GIC_DIST_BASE + GIC_DIST_SOFTINT));
  744. #endif
  745. }
  746. }
  747. /*
  748. * mt_disable_fiq: disable an interrupt which is directed to FIQ.
  749. * @irq: interrupt id
  750. * Return error code.
  751. * NoteXXX: Not allow to suspend due to FIQ context.
  752. */
  753. int mt_disable_fiq(int irq)
  754. {
  755. int i;
  756. struct irq_data data;
  757. for (i = 0; i < ARRAY_SIZE(irqs_to_fiq); i++) {
  758. if (irqs_to_fiq[i].irq == irq) {
  759. data.irq = irq;
  760. mt_irq_mask(&data);
  761. return 0;
  762. }
  763. }
  764. return -1;
  765. }
  766. /*
  767. * mt_enable_fiq: enable an interrupt which is directed to FIQ.
  768. * @irq: interrupt id
  769. * Return error code.
  770. * NoteXXX: Not allow to suspend due to FIQ context.
  771. */
  772. int mt_enable_fiq(int irq)
  773. {
  774. int i;
  775. struct irq_data data;
  776. for (i = 0; i < ARRAY_SIZE(irqs_to_fiq); i++) {
  777. if (irqs_to_fiq[i].irq == irq) {
  778. data.irq = irq;
  779. mt_irq_unmask(&data);
  780. return 0;
  781. }
  782. }
  783. return -1;
  784. }
  785. /*
  786. * fiq_isr: FIQ handler.
  787. */
  788. static void fiq_isr(struct fiq_glue_handler *h, void *regs, void *svc_sp)
  789. {
  790. unsigned int iar, irq;
  791. int cpu, i;
  792. iar = readl(IOMEM(GIC_CPU_BASE + GIC_CPU_INTACK));
  793. irq = iar & 0x3FF;
  794. cpu = 0;
  795. asm volatile ("MRC p15, 0, %0, c0, c0, 5\n" "AND %0, %0, #3\n":"+r" (cpu)
  796. : : "cc");
  797. fiq_isr_logs[cpu].irq = irq;
  798. if (irq >= NR_IRQS)
  799. return;
  800. fiq_isr_logs[cpu].in_fiq_isr = 1;
  801. if (irq == FIQ_SMP_CALL_SGI)
  802. fiq_isr_logs[cpu].smp_call_cnt++;
  803. if (irq == WDT_IRQ_BIT_ID)
  804. aee_rr_rec_fiq_step(AEE_FIQ_STEP_FIQ_ISR_BASE);
  805. for (i = 0; i < ARRAY_SIZE(irqs_to_fiq); i++) {
  806. if (irqs_to_fiq[i].irq == irq) {
  807. if (irqs_to_fiq[i].handler)
  808. (irqs_to_fiq[i].handler) (irqs_to_fiq[i].arg, regs, svc_sp);
  809. break;
  810. }
  811. }
  812. if (i == ARRAY_SIZE(irqs_to_fiq)) {
  813. register int sp asm("sp");
  814. struct pt_regs *ptregs = (struct pt_regs *)regs;
  815. asm volatile ("mov %0, %1\n" "mov fp, %2\n":"=r" (sp)
  816. : "r"(svc_sp), "r"(ptregs->ARM_fp)
  817. : "cc");
  818. preempt_disable();
  819. pr_err("Interrupt %d triggers FIQ but it is not registered\n", irq);
  820. }
  821. fiq_isr_logs[cpu].in_fiq_isr = 0;
  822. writel(iar, IOMEM(GIC_CPU_BASE + GIC_CPU_EOI));
  823. }
  824. /*
  825. * get_fiq_isr_log: get fiq_isr_log for debugging.
  826. * @cpu: processor id
  827. * @log: pointer to the address of fiq_isr_log
  828. * @len: length of fiq_isr_log
  829. * Return 0 for success or error code for failure.
  830. */
  831. int get_fiq_isr_log(int cpu, unsigned int *log, unsigned int *len)
  832. {
  833. if (log)
  834. *log = (unsigned int)&(fiq_isr_logs[cpu]);
  835. if (len)
  836. *len = sizeof(struct fiq_isr_log);
  837. return 0;
  838. }
  839. static void __set_security(int irq)
  840. {
  841. u32 val;
  842. unsigned long flags;
  843. spin_lock_irqsave(&irq_lock, flags);
  844. val = readl(IOMEM(GIC_ICDISR + 4 * (irq / 32)));
  845. val &= ~(1 << (irq % 32));
  846. writel(val, IOMEM(GIC_ICDISR + 4 * (irq / 32)));
  847. spin_unlock_irqrestore(&irq_lock, flags);
  848. }
  849. static void __raise_priority(int irq)
  850. {
  851. u32 val;
  852. unsigned long flags;
  853. spin_lock_irqsave(&irq_lock, flags);
  854. val = readl(IOMEM(GIC_DIST_BASE + GIC_DIST_PRI + 4 * (irq / 4)));
  855. val &= ~(0xFF << ((irq % 4) * 8));
  856. val |= (0x50 << ((irq % 4) * 8));
  857. writel(val, IOMEM(GIC_DIST_BASE + GIC_DIST_PRI + 4 * (irq / 4)));
  858. spin_unlock_irqrestore(&irq_lock, flags);
  859. }
  860. static int restore_for_fiq(struct notifier_block *nfb, unsigned long action, void *hcpu)
  861. {
  862. int i;
  863. switch (action) {
  864. case CPU_STARTING:
  865. for (i = 0; i < ARRAY_SIZE(irqs_to_fiq); i++) {
  866. if ((irqs_to_fiq[i].irq < (NR_GIC_SGI + NR_GIC_PPI))
  867. && (irqs_to_fiq[i].handler)) {
  868. __set_security(irqs_to_fiq[i].irq);
  869. __raise_priority(irqs_to_fiq[i].irq);
  870. mb();
  871. }
  872. }
  873. break;
  874. default:
  875. break;
  876. }
  877. return NOTIFY_OK;
  878. }
  879. static struct notifier_block fiq_notifier __cpuinitdata = {
  880. .notifier_call = restore_for_fiq,
  881. };
  882. static struct fiq_glue_handler fiq_handler = {
  883. .fiq = fiq_isr,
  884. };
  885. static int __init_fiq(void)
  886. {
  887. int ret;
  888. register_cpu_notifier(&fiq_notifier);
  889. ret = fiq_glue_register_handler(&fiq_handler);
  890. if (ret)
  891. pr_err("fail to register fiq_glue_handler\n");
  892. else
  893. fiq_glued = 1;
  894. return ret;
  895. }
  896. /*
  897. * request_fiq: direct an interrupt to FIQ and register its handler.
  898. * @irq: interrupt id
  899. * @handler: FIQ handler
  900. * @irq_flags: IRQF_xxx
  901. * @arg: argument to the FIQ handler
  902. * Return error code.
  903. *
  904. * 1.set grp0
  905. * 2.raise priority
  906. */
  907. int request_fiq(int irq, fiq_isr_handler handler, unsigned long irq_flags, void *arg)
  908. {
  909. int i;
  910. unsigned long flags;
  911. struct irq_data data;
  912. if (!fiq_glued)
  913. __init_fiq();
  914. for (i = 0; i < ARRAY_SIZE(irqs_to_fiq); i++) {
  915. spin_lock_irqsave(&irq_lock, flags);
  916. if (irqs_to_fiq[i].irq == irq) {
  917. irqs_to_fiq[i].handler = handler;
  918. irqs_to_fiq[i].arg = arg;
  919. spin_unlock_irqrestore(&irq_lock, flags);
  920. __set_security(irq);
  921. __raise_priority(irq);
  922. data.irq = irq;
  923. mt_irq_set_type(&data, irq_flags);
  924. mb(); /* only unmask after modifications done */
  925. mt_irq_unmask(&data);
  926. return 0;
  927. }
  928. spin_unlock_irqrestore(&irq_lock, flags);
  929. }
  930. return -1;
  931. }
  932. #endif
  933. /* CONFIG_FIQ_GLUE */
  934. void mt_gic_cfg_irq2cpu(unsigned int irq, unsigned int cpu, unsigned int set)
  935. {
  936. uintptr_t reg;
  937. u32 data;
  938. u32 mask;
  939. if (cpu >= num_possible_cpus())
  940. goto error;
  941. if (irq < NR_GIC_SGI)
  942. goto error;
  943. reg = (uintptr_t) GIC_DIST_BASE + (uintptr_t) GIC_DIST_TARGET + (irq / 4) * 4;
  944. data = readl(IOMEM(reg));
  945. #ifndef MTK_FORCE_CLUSTER1
  946. mask = 0x1 << (cpu + (irq % 4) * 8);
  947. #else
  948. mask = 0x1 << ((cpu + 4) + (irq % 4) * 8);
  949. #endif
  950. if (set)
  951. data |= mask;
  952. else
  953. data &= ~mask; /* clear */
  954. writel(data, IOMEM(reg));
  955. mb();
  956. pr_err("set irq %d to cpu %d\n", irq, cpu);
  957. pr_err("reg 0x%08x = 0x%08x\n", (int)reg, readl(IOMEM(reg)));
  958. return;
  959. error:
  960. pr_err("Fail to set irq %d to cpu %d\n", irq, cpu);
  961. }
  962. void __init mt_init_irq(void)
  963. {
  964. spin_lock_init(&irq_lock);
  965. mt_gic_dist_init();
  966. mt_gic_cpu_init();
  967. }
  968. static unsigned int get_mask(int irq)
  969. {
  970. unsigned int bit;
  971. bit = 1 << (irq % 32);
  972. return (readl(IOMEM(GIC_DIST_BASE + GIC_DIST_ENABLE_SET + irq / 32 * 4)) & bit) ? 1 : 0;
  973. }
  974. static unsigned int get_grp(int irq)
  975. {
  976. unsigned int bit;
  977. bit = 1 << (irq % 32);
  978. /* 0x1:irq,0x0:fiq */
  979. return (readl(IOMEM(GIC_DIST_BASE + GIC_DIST_IGROUP + irq / 32 * 4)) & bit) ? 1 : 0;
  980. }
  981. static unsigned int get_pri(int irq)
  982. {
  983. unsigned int bit;
  984. bit = 0xff << ((irq % 4) * 8);
  985. return (readl(IOMEM(GIC_DIST_BASE + GIC_DIST_PRI + irq / 4 * 4)) & bit) >>
  986. ((irq % 4) * 8);
  987. }
  988. static unsigned int get_target(int irq)
  989. {
  990. unsigned int bit;
  991. bit = 0xff << ((irq % 4) * 8);
  992. return (readl(IOMEM(GIC_DIST_BASE + GIC_DIST_TARGET + irq / 4 * 4)) & bit) >>
  993. ((irq % 4) * 8);
  994. }
  995. static unsigned int get_sens(int irq)
  996. {
  997. unsigned int bit;
  998. bit = 0x3 << ((irq % 16) * 2);
  999. /* edge:0x2, level:0x1 */
  1000. return (readl(IOMEM(GIC_DIST_BASE + GIC_DIST_CONFIG + irq / 16 * 4)) & bit) >>
  1001. ((irq % 16) * 2);
  1002. }
  1003. static unsigned int get_pending_status(unsigned int irq)
  1004. {
  1005. unsigned int bit;
  1006. bit = 1 << (irq % 32);
  1007. return (readl(IOMEM(GIC_DIST_BASE + GIC_DIST_PENDING_SET + irq / 32 * 4)) & bit) ? 1 : 0;
  1008. }
  1009. unsigned int mt_irq_get_pending(unsigned int irq)
  1010. {
  1011. return get_pending_status(irq);
  1012. }
  1013. EXPORT_SYMBOL(mt_irq_get_pending);
  1014. static unsigned int get_active_status(unsigned int irq)
  1015. {
  1016. unsigned int bit;
  1017. bit = 1 << (irq % 32);
  1018. return (readl(IOMEM(GIC_DIST_BASE + GIC_DIST_ACTIVE_SET + irq / 32 * 4)) & bit) ? 1 : 0;
  1019. }
  1020. static unsigned int get_pol(unsigned int irq)
  1021. {
  1022. unsigned int bit;
  1023. bit = 1 << (irq % 32);
  1024. /* 0x0: high, 0x1:low */
  1025. return (readl(IOMEM(INT_POL_CTL0 + ((irq - 32) / 32 * 4))) & bit) ? 1 : 0;
  1026. }
  1027. void mt_irq_dump_status(unsigned int irq)
  1028. {
  1029. if (irq >= NR_IRQS)
  1030. return;
  1031. pr_debug("[INT] irq:%d,enable:%x,active:%x,pending:%x,pri:%x,grp:%x,target:%x,sens:%x,pol:%x\n",
  1032. irq, get_mask(irq), get_active_status(irq), get_pending_status(irq), get_pri(irq),
  1033. get_grp(irq), get_target(irq), get_sens(irq), get_pol(irq));
  1034. }
  1035. EXPORT_SYMBOL(mt_irq_dump_status);
  1036. void set_mask(int irq)
  1037. {
  1038. unsigned int bit;
  1039. bit = 1 << (irq % 32);
  1040. writel(bit, IOMEM(GIC_DIST_BASE + GIC_DIST_ENABLE_SET + irq / 32 * 4));
  1041. }
  1042. void set_unmask(int irq)
  1043. {
  1044. unsigned int bit;
  1045. bit = 1 << (irq % 32);
  1046. writel(bit, IOMEM(GIC_DIST_BASE + GIC_DIST_ENABLE_CLEAR + irq / 32 * 4));
  1047. }
  1048. void mt_irq_set_pending(unsigned int irq)
  1049. {
  1050. unsigned int bit;
  1051. bit = 1 << (irq % 32);
  1052. writel(bit, IOMEM(GIC_DIST_BASE + GIC_DIST_PENDING_SET + irq / 32 * 4));
  1053. }
  1054. EXPORT_SYMBOL(mt_irq_set_pending);
  1055. void set_active_status(int irq)
  1056. {
  1057. unsigned int bit;
  1058. bit = 1 << (irq % 32);
  1059. writel(bit, IOMEM(GIC_DIST_BASE + GIC_DIST_ACTIVE_SET + irq / 32 * 4));
  1060. }
  1061. int test_irq_dump_status(void)
  1062. {
  1063. int irq = 162;
  1064. mt_irq_dump_status(irq);
  1065. set_active_status(irq);
  1066. mt_irq_set_pending(irq);
  1067. set_mask(irq);
  1068. mt_irq_dump_status(irq);
  1069. return 0;
  1070. }
  1071. #ifdef LDVT
  1072. int mt_gic_test(void)
  1073. {
  1074. int irq = 162;
  1075. /* test polarity */
  1076. mt_irq_set_polarity(irq, MT_POLARITY_LOW);
  1077. if (get_pol(irq) == !MT_POLARITY_LOW)
  1078. pr_debug("mt_irq_set_polarity GIC_POL_LOW test passed!!!\n");
  1079. else
  1080. pr_debug("mt_irq_set_polarity GIC_POL_LOW test failed!!!\n");
  1081. mt_irq_set_polarity(irq, MT_POLARITY_HIGH);
  1082. if (get_pol(irq) == !MT_POLARITY_HIGH)
  1083. pr_debug("mt_irq_set_polarity GIC_POL_HIGH test passed!!\n");
  1084. else
  1085. pr_debug("mt_irq_set_polarity GIC_POL_HIGH test failed!!\n");
  1086. /*test sensitivity */
  1087. mt_irq_set_sens(irq, MT_LEVEL_SENSITIVE);
  1088. if (!(get_sens(irq) & 0x2))
  1089. pr_debug("mt_irq_set_sens MT_LEVEL_SENSITIVE test passed!!!\n");
  1090. else
  1091. pr_debug("mt_irq_set_sens MT_LEVEL_SENSITIVE test failed!!!\n");
  1092. mt_irq_set_sens(irq, MT_EDGE_SENSITIVE);
  1093. if (get_sens(irq) & 0x2)
  1094. pr_debug("mt_irq_set_sens MT_EDGE_SENSITIVE test passed!!!\n");
  1095. else
  1096. pr_debug("mt_irq_set_sens MT_EDGE_SENSITIVE test failed!!!\n");
  1097. /*test mask */
  1098. set_mask(irq);
  1099. if (get_mask(irq) == 1)
  1100. pr_debug("GIC set_mask test passed!!!\n");
  1101. else
  1102. pr_debug("GIC set_mask test failed!!!\n");
  1103. set_unmask(irq);
  1104. if (get_mask(irq) == 0)
  1105. pr_debug("GIC set_unmask test passed!!!\n");
  1106. else
  1107. pr_debug("GIC set_unmask test failed!!!\n");
  1108. return 0;
  1109. }
  1110. static ssize_t gic_dvt_show(struct device_driver *driver, char *buf)
  1111. {
  1112. return snprintf(buf, PAGE_SIZE, "=== GIC test ===\n"
  1113. "1.Dump GIC status\n" "2.Run GIC test cases\n");
  1114. }
  1115. static ssize_t gic_dvt_store(struct device_driver *driver, const char *buf, size_t count)
  1116. {
  1117. char *p = (char *)buf;
  1118. unsigned int num;
  1119. num = kstrtoul(p, &p, 10);
  1120. switch (num) {
  1121. case 1:
  1122. test_irq_dump_status();
  1123. break;
  1124. case 2:
  1125. mt_gic_test();
  1126. break;
  1127. default:
  1128. break;
  1129. }
  1130. return count;
  1131. }
  1132. DRIVER_ATTR(gic_dvt, 0666, gic_dvt_show, gic_dvt_store);
  1133. /*
  1134. * Define Data Structure
  1135. */
  1136. struct mt_gic_driver {
  1137. struct device_driver driver;
  1138. const struct platform_device_id *id_table;
  1139. };
  1140. /*
  1141. * Define Global Variable
  1142. */
  1143. static struct mt_gic_driver mt_gic_drv = {
  1144. .driver = {
  1145. .name = "gic",
  1146. .bus = &platform_bus_type,
  1147. .owner = THIS_MODULE,
  1148. },
  1149. .id_table = NULL,
  1150. };
  1151. #endif
  1152. /*!LDVT*/
  1153. /*
  1154. static asmlinkage void __exception_irq_entry gic_handle_irq(struct pt_regs *regs)
  1155. {
  1156. u32 irqstat, irqnr;
  1157. struct gic_chip_data *gic = &gic_data[0];
  1158. void __iomem *cpu_base = gic_data_cpu_base(gic);
  1159. do {
  1160. irqstat = readl_relaxed(cpu_base + GIC_CPU_INTACK);
  1161. irqnr = irqstat & ~0x1c00;
  1162. if (likely(irqnr > 15 && irqnr < 1021)) {
  1163. irqnr = irq_find_mapping(gic->domain, irqnr);
  1164. handle_IRQ(irqnr, regs);
  1165. continue;
  1166. }
  1167. if (irqnr < 16) {
  1168. writel_relaxed(irqstat, cpu_base + GIC_CPU_EOI);
  1169. #ifdef CONFIG_SMP
  1170. handle_IPI(irqnr, regs);
  1171. #endif
  1172. continue;
  1173. }
  1174. break;
  1175. } while (1);
  1176. }
  1177. */
  1178. #ifdef CONFIG_OF
  1179. int __init mt_gic_of_init(struct device_node *node, struct device_node *parent)
  1180. {
  1181. int irq_base;
  1182. struct irq_domain *domain;
  1183. struct resource res;
  1184. GIC_DIST_BASE = of_iomap(node, 0);
  1185. WARN(!GIC_DIST_BASE, "unable to map gic dist registers\n");
  1186. GIC_CPU_BASE = of_iomap(node, 1);
  1187. WARN(!GIC_CPU_BASE, "unable to map gic cpu registers\n");
  1188. INT_POL_CTL0 = of_iomap(node, 2);
  1189. WARN(!INT_POL_CTL0, "unable to map pol registers\n");
  1190. if (of_address_to_resource(node, 2, &res))
  1191. WARN(!INT_POL_CTL0, "unable to map pol registers\n");
  1192. INT_POL_CTL0_phys = res.start;
  1193. irq_base = irq_alloc_descs(-1, NR_GIC_SGI, (NR_GIC_PPI + MT_NR_SPI), numa_node_id());
  1194. if (irq_base != NR_GIC_SGI)
  1195. pr_err("[%s] irq_alloc_descs failed! (irq_base = %d)\n", __func__, irq_base);
  1196. domain =
  1197. irq_domain_add_legacy(node, (NR_GIC_PPI + MT_NR_SPI), irq_base, NR_GIC_SGI,
  1198. &mt_gic_irq_domain_ops, NULL);
  1199. if (!domain)
  1200. pr_err("[%s] irq_domain_add_legacy failed!\n", __func__);
  1201. #ifdef CONFIG_SMP
  1202. set_smp_cross_call(irq_raise_softirq);
  1203. #endif
  1204. mt_init_irq();
  1205. return 0;
  1206. }
  1207. IRQCHIP_DECLARE(mt_cortex_a15_gic, "mediatek,mt6735-gic", mt_gic_of_init);
  1208. #endif
  1209. /*
  1210. * mt_gic_init: GIC init function
  1211. * always return 0
  1212. */
  1213. #ifdef LDVT
  1214. static int __init mt_gic_init(void)
  1215. {
  1216. int ret;
  1217. ret = driver_register(&mt_gic_drv.driver);
  1218. if (ret == 0)
  1219. pr_err("GIC init done...\n");
  1220. ret = driver_create_file(&mt_gic_drv.driver, &driver_attr_gic_dvt);
  1221. if (ret == 0)
  1222. pr_err("GIC create sysfs file done...\n");
  1223. return 0;
  1224. }
  1225. #endif
  1226. #ifdef LDVT
  1227. arch_initcall(mt_gic_init);
  1228. #endif