interrupt.c 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450
  1. /*
  2. * Copyright (c) 2006-2021, RT-Thread Development Team
  3. *
  4. * SPDX-License-Identifier: Apache-2.0
  5. *
  6. * Change Logs:
  7. * Date Author Notes
  8. * 2011-01-13 weety first version
  9. */
  10. #include <rthw.h>
  11. #include "at91sam926x.h"
  12. #include "interrupt.h"
  13. #define MAX_HANDLERS (AIC_IRQS + PIN_IRQS)
  14. extern rt_uint32_t rt_interrupt_nest;
  15. /* exception and interrupt handler table */
  16. struct rt_irq_desc irq_desc[MAX_HANDLERS];
  17. rt_uint32_t rt_interrupt_from_thread, rt_interrupt_to_thread;
  18. rt_uint32_t rt_thread_switch_interrupt_flag;
  19. /* --------------------------------------------------------------------
  20. * Interrupt initialization
  21. * -------------------------------------------------------------------- */
  22. rt_uint32_t at91_extern_irq;
  23. #define is_extern_irq(irq) ((1 << (irq)) & at91_extern_irq)
  24. /*
  25. * The default interrupt priority levels (0 = lowest, 7 = highest).
  26. */
  27. static rt_uint32_t at91sam9260_default_irq_priority[MAX_HANDLERS] = {
  28. 7, /* Advanced Interrupt Controller */
  29. 7, /* System Peripherals */
  30. 1, /* Parallel IO Controller A */
  31. 1, /* Parallel IO Controller B */
  32. 1, /* Parallel IO Controller C */
  33. 0, /* Analog-to-Digital Converter */
  34. 5, /* USART 0 */
  35. 5, /* USART 1 */
  36. 5, /* USART 2 */
  37. 0, /* Multimedia Card Interface */
  38. 2, /* USB Device Port */
  39. 6, /* Two-Wire Interface */
  40. 5, /* Serial Peripheral Interface 0 */
  41. 5, /* Serial Peripheral Interface 1 */
  42. 5, /* Serial Synchronous Controller */
  43. 0,
  44. 0,
  45. 0, /* Timer Counter 0 */
  46. 0, /* Timer Counter 1 */
  47. 0, /* Timer Counter 2 */
  48. 2, /* USB Host port */
  49. 3, /* Ethernet */
  50. 0, /* Image Sensor Interface */
  51. 5, /* USART 3 */
  52. 5, /* USART 4 */
  53. 5, /* USART 5 */
  54. 0, /* Timer Counter 3 */
  55. 0, /* Timer Counter 4 */
  56. 0, /* Timer Counter 5 */
  57. 0, /* Advanced Interrupt Controller */
  58. 0, /* Advanced Interrupt Controller */
  59. 0, /* Advanced Interrupt Controller */
  60. };
  61. /**
  62. * @addtogroup AT91SAM926X
  63. */
  64. /*@{*/
  65. void rt_hw_interrupt_mask(int irq);
  66. void rt_hw_interrupt_umask(int irq);
  67. rt_isr_handler_t rt_hw_interrupt_handle(rt_uint32_t vector, void *param)
  68. {
  69. rt_kprintf("Unhandled interrupt %d occured!!!\n", vector);
  70. return RT_NULL;
  71. }
  72. rt_isr_handler_t at91_gpio_irq_handle(rt_uint32_t vector, void *param)
  73. {
  74. rt_uint32_t isr, pio, irq_n;
  75. void *parameter;
  76. if (vector == AT91SAM9260_ID_PIOA)
  77. {
  78. pio = AT91_PIOA;
  79. irq_n = AIC_IRQS;
  80. }
  81. else if (vector == AT91SAM9260_ID_PIOB)
  82. {
  83. pio = AT91_PIOB;
  84. irq_n = AIC_IRQS + 32;
  85. }
  86. else if (vector == AT91SAM9260_ID_PIOC)
  87. {
  88. pio = AT91_PIOC;
  89. irq_n = AIC_IRQS + 32*2;
  90. }
  91. else
  92. return RT_NULL;
  93. isr = at91_sys_read(pio+PIO_ISR) & at91_sys_read(pio+PIO_IMR);
  94. while (isr)
  95. {
  96. if (isr & 1)
  97. {
  98. parameter = irq_desc[irq_n].param;
  99. irq_desc[irq_n].handler(irq_n, parameter);
  100. }
  101. isr >>= 1;
  102. irq_n++;
  103. }
  104. return RT_NULL;
  105. }
  106. /*
  107. * Initialize the AIC interrupt controller.
  108. */
  109. void at91_aic_init(rt_uint32_t *priority)
  110. {
  111. rt_uint32_t i;
  112. /*
  113. * The IVR is used by macro get_irqnr_and_base to read and verify.
  114. * The irq number is NR_AIC_IRQS when a spurious interrupt has occurred.
  115. */
  116. for (i = 0; i < AIC_IRQS; i++) {
  117. /* Put irq number in Source Vector Register: */
  118. at91_sys_write(AT91_AIC_SVR(i), i); // no-used
  119. /* Active Low interrupt, with the specified priority */
  120. at91_sys_write(AT91_AIC_SMR(i), AT91_AIC_SRCTYPE_LOW | priority[i]);
  121. //AT91_AIC_SRCTYPE_FALLING
  122. /* Perform 8 End Of Interrupt Command to make sure AIC will not Lock out nIRQ */
  123. if (i < 8)
  124. at91_sys_write(AT91_AIC_EOICR, 0);
  125. }
  126. /*
  127. * Spurious Interrupt ID in Spurious Vector Register is NR_AIC_IRQS
  128. * When there is no current interrupt, the IRQ Vector Register reads the value stored in AIC_SPU
  129. */
  130. at91_sys_write(AT91_AIC_SPU, AIC_IRQS);
  131. /* No debugging in AIC: Debug (Protect) Control Register */
  132. at91_sys_write(AT91_AIC_DCR, 0);
  133. /* Disable and clear all interrupts initially */
  134. at91_sys_write(AT91_AIC_IDCR, 0xFFFFFFFF);
  135. at91_sys_write(AT91_AIC_ICCR, 0xFFFFFFFF);
  136. }
  137. static void at91_gpio_irq_init()
  138. {
  139. int i, idx;
  140. char *name[] = {"PIOA", "PIOB", "PIOC"};
  141. at91_sys_write(AT91_PIOA+PIO_IDR, 0xffffffff);
  142. at91_sys_write(AT91_PIOB+PIO_IDR, 0xffffffff);
  143. at91_sys_write(AT91_PIOC+PIO_IDR, 0xffffffff);
  144. idx = AT91SAM9260_ID_PIOA;
  145. for (i = 0; i < 3; i++)
  146. {
  147. irq_desc[idx].handler = (rt_isr_handler_t)at91_gpio_irq_handle;
  148. irq_desc[idx].param = RT_NULL;
  149. #ifdef RT_USING_INTERRUPT_INFO
  150. rt_snprintf(irq_desc[idx].name, RT_NAME_MAX - 1, name[i]);
  151. irq_desc[idx].counter = 0;
  152. #endif
  153. idx++;
  154. }
  155. rt_hw_interrupt_umask(AT91SAM9260_ID_PIOA);
  156. rt_hw_interrupt_umask(AT91SAM9260_ID_PIOB);
  157. rt_hw_interrupt_umask(AT91SAM9260_ID_PIOC);
  158. }
  159. /**
  160. * This function will initialize hardware interrupt
  161. */
  162. void rt_hw_interrupt_init(void)
  163. {
  164. register rt_uint32_t idx;
  165. rt_uint32_t *priority = at91sam9260_default_irq_priority;
  166. at91_extern_irq = (1UL << AT91SAM9260_ID_IRQ0) | (1UL << AT91SAM9260_ID_IRQ1)
  167. | (1UL << AT91SAM9260_ID_IRQ2);
  168. /* Initialize the AIC interrupt controller */
  169. at91_aic_init(priority);
  170. /* init exceptions table */
  171. for(idx=0; idx < MAX_HANDLERS; idx++)
  172. {
  173. irq_desc[idx].handler = (rt_isr_handler_t)rt_hw_interrupt_handle;
  174. irq_desc[idx].param = RT_NULL;
  175. #ifdef RT_USING_INTERRUPT_INFO
  176. rt_snprintf(irq_desc[idx].name, RT_NAME_MAX - 1, "default");
  177. irq_desc[idx].counter = 0;
  178. #endif
  179. }
  180. at91_gpio_irq_init();
  181. /* init interrupt nest, and context in thread sp */
  182. rt_interrupt_nest = 0;
  183. rt_interrupt_from_thread = 0;
  184. rt_interrupt_to_thread = 0;
  185. rt_thread_switch_interrupt_flag = 0;
  186. }
  187. static void at91_gpio_irq_mask(int irq)
  188. {
  189. rt_uint32_t pin, pio, bank;
  190. bank = (irq - AIC_IRQS)>>5;
  191. if (bank == 0)
  192. {
  193. pio = AT91_PIOA;
  194. }
  195. else if (bank == 1)
  196. {
  197. pio = AT91_PIOB;
  198. }
  199. else if (bank == 2)
  200. {
  201. pio = AT91_PIOC;
  202. }
  203. else
  204. return;
  205. pin = 1 << ((irq - AIC_IRQS) & 31);
  206. at91_sys_write(pio+PIO_IDR, pin);
  207. }
  208. /**
  209. * This function will mask a interrupt.
  210. * @param vector the interrupt number
  211. */
  212. void rt_hw_interrupt_mask(int irq)
  213. {
  214. if (irq >= AIC_IRQS)
  215. {
  216. at91_gpio_irq_mask(irq);
  217. }
  218. else
  219. {
  220. /* Disable interrupt on AIC */
  221. at91_sys_write(AT91_AIC_IDCR, 1 << irq);
  222. }
  223. }
  224. static void at91_gpio_irq_umask(int irq)
  225. {
  226. rt_uint32_t pin, pio, bank;
  227. bank = (irq - AIC_IRQS)>>5;
  228. if (bank == 0)
  229. {
  230. pio = AT91_PIOA;
  231. }
  232. else if (bank == 1)
  233. {
  234. pio = AT91_PIOB;
  235. }
  236. else if (bank == 2)
  237. {
  238. pio = AT91_PIOC;
  239. }
  240. else
  241. return;
  242. pin = 1 << ((irq - AIC_IRQS) & 31);
  243. at91_sys_write(pio+PIO_IER, pin);
  244. }
  245. /**
  246. * This function will un-mask a interrupt.
  247. * @param vector the interrupt number
  248. */
  249. void rt_hw_interrupt_umask(int irq)
  250. {
  251. if (irq >= AIC_IRQS)
  252. {
  253. at91_gpio_irq_umask(irq);
  254. }
  255. else
  256. {
  257. /* Enable interrupt on AIC */
  258. at91_sys_write(AT91_AIC_IECR, 1 << irq);
  259. }
  260. }
  261. /**
  262. * This function will install a interrupt service routine to a interrupt.
  263. * @param vector the interrupt number
  264. * @param handler the interrupt service routine to be installed
  265. * @param param the interrupt service function parameter
  266. * @param name the interrupt name
  267. * @return old handler
  268. */
  269. rt_isr_handler_t rt_hw_interrupt_install(int vector, rt_isr_handler_t handler,
  270. void *param, const char *name)
  271. {
  272. rt_isr_handler_t old_handler = RT_NULL;
  273. if(vector < MAX_HANDLERS)
  274. {
  275. old_handler = irq_desc[vector].handler;
  276. if (handler != RT_NULL)
  277. {
  278. irq_desc[vector].handler = (rt_isr_handler_t)handler;
  279. irq_desc[vector].param = param;
  280. #ifdef RT_USING_INTERRUPT_INFO
  281. rt_snprintf(irq_desc[vector].name, RT_NAME_MAX - 1, "%s", name);
  282. irq_desc[vector].counter = 0;
  283. #endif
  284. }
  285. }
  286. return old_handler;
  287. }
  288. /*@}*/
  289. /*
  290. static int at91_aic_set_type(unsigned irq, unsigned type)
  291. {
  292. unsigned int smr, srctype;
  293. switch (type) {
  294. case IRQ_TYPE_LEVEL_HIGH:
  295. srctype = AT91_AIC_SRCTYPE_HIGH;
  296. break;
  297. case IRQ_TYPE_EDGE_RISING:
  298. srctype = AT91_AIC_SRCTYPE_RISING;
  299. break;
  300. case IRQ_TYPE_LEVEL_LOW:
  301. // only supported on external interrupts
  302. if ((irq == AT91_ID_FIQ) || is_extern_irq(irq))
  303. srctype = AT91_AIC_SRCTYPE_LOW;
  304. else
  305. return -1;
  306. break;
  307. case IRQ_TYPE_EDGE_FALLING:
  308. // only supported on external interrupts
  309. if ((irq == AT91_ID_FIQ) || is_extern_irq(irq))
  310. srctype = AT91_AIC_SRCTYPE_FALLING;
  311. else
  312. return -1;
  313. break;
  314. default:
  315. return -1;
  316. }
  317. smr = at91_sys_read(AT91_AIC_SMR(irq)) & ~AT91_AIC_SRCTYPE;
  318. at91_sys_write(AT91_AIC_SMR(irq), smr | srctype);
  319. return 0;
  320. }
  321. */
  322. rt_uint32_t rt_hw_interrupt_get_active(rt_uint32_t fiq_irq)
  323. {
  324. //volatile rt_uint32_t irqstat;
  325. rt_uint32_t id;
  326. if (fiq_irq == INT_FIQ)
  327. return 0;
  328. //IRQ
  329. /* AIC need this dummy read */
  330. at91_sys_read(AT91_AIC_IVR);
  331. /* clear pending register */
  332. id = at91_sys_read(AT91_AIC_ISR);
  333. return id;
  334. }
  335. void rt_hw_interrupt_ack(rt_uint32_t fiq_irq, rt_uint32_t id)
  336. {
  337. /* new FIQ generation */
  338. if (fiq_irq == INT_FIQ)
  339. return;
  340. /* new IRQ generation */
  341. // EIOCR must be write any value after interrupt,
  342. // or else can't response next interrupt
  343. at91_sys_write(AT91_AIC_EOICR, 0x0);
  344. }
  345. void rt_interrupt_dispatch(rt_uint32_t fiq_irq)
  346. {
  347. rt_isr_handler_t isr_func;
  348. rt_uint32_t irq;
  349. void *param;
  350. /* get irq number */
  351. irq = rt_hw_interrupt_get_active(fiq_irq);
  352. /* get interrupt service routine */
  353. isr_func = irq_desc[irq].handler;
  354. param = irq_desc[irq].param;
  355. /* turn to interrupt service routine */
  356. isr_func(irq, param);
  357. rt_hw_interrupt_ack(fiq_irq, irq);
  358. #ifdef RT_USING_INTERRUPT_INFO
  359. irq_desc[irq].counter ++;
  360. #endif
  361. }
  362. #ifdef RT_USING_FINSH
  363. #ifdef RT_USING_INTERRUPT_INFO
  364. void list_irq(void)
  365. {
  366. int irq;
  367. rt_kprintf("number\tcount\tname\n");
  368. for (irq = 0; irq < MAX_HANDLERS; irq++)
  369. {
  370. if (rt_strncmp(irq_desc[irq].name, "default", sizeof("default")))
  371. {
  372. rt_kprintf("%02ld: %10ld %s\n", irq, irq_desc[irq].counter, irq_desc[irq].name);
  373. }
  374. }
  375. }
  376. #include <finsh.h>
  377. #ifdef FINSH_USING_MSH
  378. int cmd_list_irq(int argc, char** argv)
  379. {
  380. list_irq();
  381. return 0;
  382. }
  383. MSH_CMD_EXPORT_ALIAS(cmd_list_irq, list_irq, list system irq);
  384. #endif
  385. #endif
  386. #endif