core.c 6.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273
  1. /*
  2. * linux/arch/arm/mach-integrator/core.c
  3. *
  4. * Copyright (C) 2000-2003 Deep Blue Solutions Ltd
  5. *
  6. * This program is free software; you can redistribute it and/or modify
  7. * it under the terms of the GNU General Public License version 2, as
  8. * published by the Free Software Foundation.
  9. */
  10. #include <linux/types.h>
  11. #include <linux/kernel.h>
  12. #include <linux/init.h>
  13. #include <linux/device.h>
  14. #include <linux/spinlock.h>
  15. #include <linux/interrupt.h>
  16. #include <linux/sched.h>
  17. #include <asm/hardware.h>
  18. #include <asm/irq.h>
  19. #include <asm/io.h>
  20. #include <asm/hardware/amba.h>
  21. #include <asm/arch/cm.h>
  22. #include <asm/system.h>
  23. #include <asm/leds.h>
  24. #include <asm/mach/time.h>
  25. #include "common.h"
  26. static struct amba_device rtc_device = {
  27. .dev = {
  28. .bus_id = "mb:15",
  29. },
  30. .res = {
  31. .start = INTEGRATOR_RTC_BASE,
  32. .end = INTEGRATOR_RTC_BASE + SZ_4K - 1,
  33. .flags = IORESOURCE_MEM,
  34. },
  35. .irq = { IRQ_RTCINT, NO_IRQ },
  36. .periphid = 0x00041030,
  37. };
  38. static struct amba_device uart0_device = {
  39. .dev = {
  40. .bus_id = "mb:16",
  41. },
  42. .res = {
  43. .start = INTEGRATOR_UART0_BASE,
  44. .end = INTEGRATOR_UART0_BASE + SZ_4K - 1,
  45. .flags = IORESOURCE_MEM,
  46. },
  47. .irq = { IRQ_UARTINT0, NO_IRQ },
  48. .periphid = 0x0041010,
  49. };
  50. static struct amba_device uart1_device = {
  51. .dev = {
  52. .bus_id = "mb:17",
  53. },
  54. .res = {
  55. .start = INTEGRATOR_UART1_BASE,
  56. .end = INTEGRATOR_UART1_BASE + SZ_4K - 1,
  57. .flags = IORESOURCE_MEM,
  58. },
  59. .irq = { IRQ_UARTINT1, NO_IRQ },
  60. .periphid = 0x0041010,
  61. };
  62. static struct amba_device kmi0_device = {
  63. .dev = {
  64. .bus_id = "mb:18",
  65. },
  66. .res = {
  67. .start = KMI0_BASE,
  68. .end = KMI0_BASE + SZ_4K - 1,
  69. .flags = IORESOURCE_MEM,
  70. },
  71. .irq = { IRQ_KMIINT0, NO_IRQ },
  72. .periphid = 0x00041050,
  73. };
  74. static struct amba_device kmi1_device = {
  75. .dev = {
  76. .bus_id = "mb:19",
  77. },
  78. .res = {
  79. .start = KMI1_BASE,
  80. .end = KMI1_BASE + SZ_4K - 1,
  81. .flags = IORESOURCE_MEM,
  82. },
  83. .irq = { IRQ_KMIINT1, NO_IRQ },
  84. .periphid = 0x00041050,
  85. };
  86. static struct amba_device *amba_devs[] __initdata = {
  87. &rtc_device,
  88. &uart0_device,
  89. &uart1_device,
  90. &kmi0_device,
  91. &kmi1_device,
  92. };
  93. static int __init integrator_init(void)
  94. {
  95. int i;
  96. for (i = 0; i < ARRAY_SIZE(amba_devs); i++) {
  97. struct amba_device *d = amba_devs[i];
  98. amba_device_register(d, &iomem_resource);
  99. }
  100. return 0;
  101. }
  102. arch_initcall(integrator_init);
  103. #define CM_CTRL IO_ADDRESS(INTEGRATOR_HDR_BASE) + INTEGRATOR_HDR_CTRL_OFFSET
  104. static DEFINE_SPINLOCK(cm_lock);
  105. /**
  106. * cm_control - update the CM_CTRL register.
  107. * @mask: bits to change
  108. * @set: bits to set
  109. */
  110. void cm_control(u32 mask, u32 set)
  111. {
  112. unsigned long flags;
  113. u32 val;
  114. spin_lock_irqsave(&cm_lock, flags);
  115. val = readl(CM_CTRL) & ~mask;
  116. writel(val | set, CM_CTRL);
  117. spin_unlock_irqrestore(&cm_lock, flags);
  118. }
  119. EXPORT_SYMBOL(cm_control);
  120. /*
  121. * Where is the timer (VA)?
  122. */
  123. #define TIMER0_VA_BASE (IO_ADDRESS(INTEGRATOR_CT_BASE)+0x00000000)
  124. #define TIMER1_VA_BASE (IO_ADDRESS(INTEGRATOR_CT_BASE)+0x00000100)
  125. #define TIMER2_VA_BASE (IO_ADDRESS(INTEGRATOR_CT_BASE)+0x00000200)
  126. #define VA_IC_BASE IO_ADDRESS(INTEGRATOR_IC_BASE)
  127. /*
  128. * How long is the timer interval?
  129. */
  130. #define TIMER_INTERVAL (TICKS_PER_uSEC * mSEC_10)
  131. #if TIMER_INTERVAL >= 0x100000
  132. #define TICKS2USECS(x) (256 * (x) / TICKS_PER_uSEC)
  133. #elif TIMER_INTERVAL >= 0x10000
  134. #define TICKS2USECS(x) (16 * (x) / TICKS_PER_uSEC)
  135. #else
  136. #define TICKS2USECS(x) ((x) / TICKS_PER_uSEC)
  137. #endif
  138. /*
  139. * What does it look like?
  140. */
  141. typedef struct TimerStruct {
  142. unsigned long TimerLoad;
  143. unsigned long TimerValue;
  144. unsigned long TimerControl;
  145. unsigned long TimerClear;
  146. } TimerStruct_t;
  147. static unsigned long timer_reload;
  148. /*
  149. * Returns number of ms since last clock interrupt. Note that interrupts
  150. * will have been disabled by do_gettimeoffset()
  151. */
  152. unsigned long integrator_gettimeoffset(void)
  153. {
  154. volatile TimerStruct_t *timer1 = (TimerStruct_t *)TIMER1_VA_BASE;
  155. unsigned long ticks1, ticks2, status;
  156. /*
  157. * Get the current number of ticks. Note that there is a race
  158. * condition between us reading the timer and checking for
  159. * an interrupt. We get around this by ensuring that the
  160. * counter has not reloaded between our two reads.
  161. */
  162. ticks2 = timer1->TimerValue & 0xffff;
  163. do {
  164. ticks1 = ticks2;
  165. status = __raw_readl(VA_IC_BASE + IRQ_RAW_STATUS);
  166. ticks2 = timer1->TimerValue & 0xffff;
  167. } while (ticks2 > ticks1);
  168. /*
  169. * Number of ticks since last interrupt.
  170. */
  171. ticks1 = timer_reload - ticks2;
  172. /*
  173. * Interrupt pending? If so, we've reloaded once already.
  174. */
  175. if (status & (1 << IRQ_TIMERINT1))
  176. ticks1 += timer_reload;
  177. /*
  178. * Convert the ticks to usecs
  179. */
  180. return TICKS2USECS(ticks1);
  181. }
  182. /*
  183. * IRQ handler for the timer
  184. */
  185. static irqreturn_t
  186. integrator_timer_interrupt(int irq, void *dev_id, struct pt_regs *regs)
  187. {
  188. volatile TimerStruct_t *timer1 = (volatile TimerStruct_t *)TIMER1_VA_BASE;
  189. write_seqlock(&xtime_lock);
  190. /*
  191. * clear the interrupt
  192. */
  193. timer1->TimerClear = 1;
  194. timer_tick(regs);
  195. write_sequnlock(&xtime_lock);
  196. return IRQ_HANDLED;
  197. }
  198. static struct irqaction integrator_timer_irq = {
  199. .name = "Integrator Timer Tick",
  200. .flags = SA_INTERRUPT,
  201. .handler = integrator_timer_interrupt
  202. };
  203. /*
  204. * Set up timer interrupt, and return the current time in seconds.
  205. */
  206. void __init integrator_time_init(unsigned long reload, unsigned int ctrl)
  207. {
  208. volatile TimerStruct_t *timer0 = (volatile TimerStruct_t *)TIMER0_VA_BASE;
  209. volatile TimerStruct_t *timer1 = (volatile TimerStruct_t *)TIMER1_VA_BASE;
  210. volatile TimerStruct_t *timer2 = (volatile TimerStruct_t *)TIMER2_VA_BASE;
  211. unsigned int timer_ctrl = 0x80 | 0x40; /* periodic */
  212. timer_reload = reload;
  213. timer_ctrl |= ctrl;
  214. if (timer_reload > 0x100000) {
  215. timer_reload >>= 8;
  216. timer_ctrl |= 0x08; /* /256 */
  217. } else if (timer_reload > 0x010000) {
  218. timer_reload >>= 4;
  219. timer_ctrl |= 0x04; /* /16 */
  220. }
  221. /*
  222. * Initialise to a known state (all timers off)
  223. */
  224. timer0->TimerControl = 0;
  225. timer1->TimerControl = 0;
  226. timer2->TimerControl = 0;
  227. timer1->TimerLoad = timer_reload;
  228. timer1->TimerValue = timer_reload;
  229. timer1->TimerControl = timer_ctrl;
  230. /*
  231. * Make irqs happen for the system timer
  232. */
  233. setup_irq(IRQ_TIMERINT1, &integrator_timer_irq);
  234. }