core.c 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367
  1. /*
  2. * derived from linux/arch/arm/mach-versatile/core.c
  3. * linux/arch/arm/mach-bcmring/core.c
  4. *
  5. * Copyright (C) 1999 - 2003 ARM Limited
  6. * Copyright (C) 2000 Deep Blue Solutions Ltd
  7. *
  8. * This program is free software; you can redistribute it and/or modify
  9. * it under the terms of the GNU General Public License as published by
  10. * the Free Software Foundation; either version 2 of the License, or
  11. * (at your option) any later version.
  12. *
  13. * This program is distributed in the hope that it will be useful,
  14. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  15. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  16. * GNU General Public License for more details.
  17. *
  18. * You should have received a copy of the GNU General Public License
  19. * along with this program; if not, write to the Free Software
  20. * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
  21. */
  22. /* Portions copyright Broadcom 2008 */
  23. #include <linux/init.h>
  24. #include <linux/device.h>
  25. #include <linux/dma-mapping.h>
  26. #include <linux/platform_device.h>
  27. #include <linux/sysdev.h>
  28. #include <linux/interrupt.h>
  29. #include <linux/amba/bus.h>
  30. #include <linux/clocksource.h>
  31. #include <linux/clockchips.h>
  32. #include <linux/amba/bus.h>
  33. #include <mach/csp/mm_addr.h>
  34. #include <mach/hardware.h>
  35. #include <asm/clkdev.h>
  36. #include <linux/io.h>
  37. #include <asm/irq.h>
  38. #include <asm/hardware/arm_timer.h>
  39. #include <asm/mach-types.h>
  40. #include <asm/mach/arch.h>
  41. #include <asm/mach/flash.h>
  42. #include <asm/mach/irq.h>
  43. #include <asm/mach/time.h>
  44. #include <asm/mach/map.h>
  45. #include <asm/mach/mmc.h>
  46. #include <cfg_global.h>
  47. #include "clock.h"
  48. #include <csp/secHw.h>
  49. #include <mach/csp/secHw_def.h>
  50. #include <mach/csp/chipcHw_inline.h>
  51. #include <mach/csp/tmrHw_reg.h>
  52. #define AMBA_DEVICE(name, initname, base, plat, size) \
  53. static struct amba_device name##_device = { \
  54. .dev = { \
  55. .coherent_dma_mask = ~0, \
  56. .init_name = initname, \
  57. .platform_data = plat \
  58. }, \
  59. .res = { \
  60. .start = MM_ADDR_IO_##base, \
  61. .end = MM_ADDR_IO_##base + (size) - 1, \
  62. .flags = IORESOURCE_MEM \
  63. }, \
  64. .dma_mask = ~0, \
  65. .irq = { \
  66. IRQ_##base \
  67. } \
  68. }
  69. AMBA_DEVICE(uartA, "uarta", UARTA, NULL, SZ_4K);
  70. AMBA_DEVICE(uartB, "uartb", UARTB, NULL, SZ_4K);
  71. static struct clk pll1_clk = {
  72. .name = "PLL1",
  73. .type = CLK_TYPE_PRIMARY | CLK_TYPE_PLL1,
  74. .rate_hz = 2000000000,
  75. .use_cnt = 7,
  76. };
  77. static struct clk uart_clk = {
  78. .name = "UART",
  79. .type = CLK_TYPE_PROGRAMMABLE,
  80. .csp_id = chipcHw_CLOCK_UART,
  81. .rate_hz = HW_CFG_UART_CLK_HZ,
  82. .parent = &pll1_clk,
  83. };
  84. static struct clk_lookup lookups[] = {
  85. { /* UART0 */
  86. .dev_id = "uarta",
  87. .clk = &uart_clk,
  88. }, { /* UART1 */
  89. .dev_id = "uartb",
  90. .clk = &uart_clk,
  91. }
  92. };
  93. static struct amba_device *amba_devs[] __initdata = {
  94. &uartA_device,
  95. &uartB_device,
  96. };
  97. void __init bcmring_amba_init(void)
  98. {
  99. int i;
  100. u32 bus_clock;
  101. /* Linux is run initially in non-secure mode. Secure peripherals */
  102. /* generate FIQ, and must be handled in secure mode. Until we have */
  103. /* a linux security monitor implementation, keep everything in */
  104. /* non-secure mode. */
  105. chipcHw_busInterfaceClockEnable(chipcHw_REG_BUS_CLOCK_SPU);
  106. secHw_setUnsecure(secHw_BLK_MASK_CHIP_CONTROL |
  107. secHw_BLK_MASK_KEY_SCAN |
  108. secHw_BLK_MASK_TOUCH_SCREEN |
  109. secHw_BLK_MASK_UART0 |
  110. secHw_BLK_MASK_UART1 |
  111. secHw_BLK_MASK_WATCHDOG |
  112. secHw_BLK_MASK_SPUM |
  113. secHw_BLK_MASK_DDR2 |
  114. secHw_BLK_MASK_SPU |
  115. secHw_BLK_MASK_PKA |
  116. secHw_BLK_MASK_RNG |
  117. secHw_BLK_MASK_RTC |
  118. secHw_BLK_MASK_OTP |
  119. secHw_BLK_MASK_BOOT |
  120. secHw_BLK_MASK_MPU |
  121. secHw_BLK_MASK_TZCTRL | secHw_BLK_MASK_INTR);
  122. /* Only the devices attached to the AMBA bus are enabled just before the bus is */
  123. /* scanned and the drivers are loaded. The clocks need to be on for the AMBA bus */
  124. /* driver to access these blocks. The bus is probed, and the drivers are loaded. */
  125. /* FIXME Need to remove enable of PIF once CLCD clock enable used properly in FPGA. */
  126. bus_clock = chipcHw_REG_BUS_CLOCK_GE
  127. | chipcHw_REG_BUS_CLOCK_SDIO0 | chipcHw_REG_BUS_CLOCK_SDIO1;
  128. chipcHw_busInterfaceClockEnable(bus_clock);
  129. for (i = 0; i < ARRAY_SIZE(lookups); i++)
  130. clkdev_add(&lookups[i]);
  131. for (i = 0; i < ARRAY_SIZE(amba_devs); i++) {
  132. struct amba_device *d = amba_devs[i];
  133. amba_device_register(d, &iomem_resource);
  134. }
  135. }
  136. /*
  137. * Where is the timer (VA)?
  138. */
  139. #define TIMER0_VA_BASE MM_IO_BASE_TMR
  140. #define TIMER1_VA_BASE (MM_IO_BASE_TMR + 0x20)
  141. #define TIMER2_VA_BASE (MM_IO_BASE_TMR + 0x40)
  142. #define TIMER3_VA_BASE (MM_IO_BASE_TMR + 0x60)
  143. /* Timer 0 - 25 MHz, Timer3 at bus clock rate, typically 150-166 MHz */
  144. #if defined(CONFIG_ARCH_FPGA11107)
  145. /* fpga cpu/bus are currently 30 times slower so scale frequency as well to */
  146. /* slow down Linux's sense of time */
  147. #define TIMER0_FREQUENCY_MHZ (tmrHw_LOW_FREQUENCY_MHZ * 30)
  148. #define TIMER1_FREQUENCY_MHZ (tmrHw_LOW_FREQUENCY_MHZ * 30)
  149. #define TIMER3_FREQUENCY_MHZ (tmrHw_HIGH_FREQUENCY_MHZ * 30)
  150. #define TIMER3_FREQUENCY_KHZ (tmrHw_HIGH_FREQUENCY_HZ / 1000 * 30)
  151. #else
  152. #define TIMER0_FREQUENCY_MHZ tmrHw_LOW_FREQUENCY_MHZ
  153. #define TIMER1_FREQUENCY_MHZ tmrHw_LOW_FREQUENCY_MHZ
  154. #define TIMER3_FREQUENCY_MHZ tmrHw_HIGH_FREQUENCY_MHZ
  155. #define TIMER3_FREQUENCY_KHZ (tmrHw_HIGH_FREQUENCY_HZ / 1000)
  156. #endif
  157. #define TICKS_PER_uSEC TIMER0_FREQUENCY_MHZ
  158. /*
  159. * These are useconds NOT ticks.
  160. *
  161. */
  162. #define mSEC_1 1000
  163. #define mSEC_5 (mSEC_1 * 5)
  164. #define mSEC_10 (mSEC_1 * 10)
  165. #define mSEC_25 (mSEC_1 * 25)
  166. #define SEC_1 (mSEC_1 * 1000)
  167. /*
  168. * How long is the timer interval?
  169. */
  170. #define TIMER_INTERVAL (TICKS_PER_uSEC * mSEC_10)
  171. #if TIMER_INTERVAL >= 0x100000
  172. #define TIMER_RELOAD (TIMER_INTERVAL >> 8)
  173. #define TIMER_DIVISOR (TIMER_CTRL_DIV256)
  174. #define TICKS2USECS(x) (256 * (x) / TICKS_PER_uSEC)
  175. #elif TIMER_INTERVAL >= 0x10000
  176. #define TIMER_RELOAD (TIMER_INTERVAL >> 4) /* Divide by 16 */
  177. #define TIMER_DIVISOR (TIMER_CTRL_DIV16)
  178. #define TICKS2USECS(x) (16 * (x) / TICKS_PER_uSEC)
  179. #else
  180. #define TIMER_RELOAD (TIMER_INTERVAL)
  181. #define TIMER_DIVISOR (TIMER_CTRL_DIV1)
  182. #define TICKS2USECS(x) ((x) / TICKS_PER_uSEC)
  183. #endif
  184. static void timer_set_mode(enum clock_event_mode mode,
  185. struct clock_event_device *clk)
  186. {
  187. unsigned long ctrl;
  188. switch (mode) {
  189. case CLOCK_EVT_MODE_PERIODIC:
  190. writel(TIMER_RELOAD, TIMER0_VA_BASE + TIMER_LOAD);
  191. ctrl = TIMER_CTRL_PERIODIC;
  192. ctrl |=
  193. TIMER_DIVISOR | TIMER_CTRL_32BIT | TIMER_CTRL_IE |
  194. TIMER_CTRL_ENABLE;
  195. break;
  196. case CLOCK_EVT_MODE_ONESHOT:
  197. /* period set, and timer enabled in 'next_event' hook */
  198. ctrl = TIMER_CTRL_ONESHOT;
  199. ctrl |= TIMER_DIVISOR | TIMER_CTRL_32BIT | TIMER_CTRL_IE;
  200. break;
  201. case CLOCK_EVT_MODE_UNUSED:
  202. case CLOCK_EVT_MODE_SHUTDOWN:
  203. default:
  204. ctrl = 0;
  205. }
  206. writel(ctrl, TIMER0_VA_BASE + TIMER_CTRL);
  207. }
  208. static int timer_set_next_event(unsigned long evt,
  209. struct clock_event_device *unused)
  210. {
  211. unsigned long ctrl = readl(TIMER0_VA_BASE + TIMER_CTRL);
  212. writel(evt, TIMER0_VA_BASE + TIMER_LOAD);
  213. writel(ctrl | TIMER_CTRL_ENABLE, TIMER0_VA_BASE + TIMER_CTRL);
  214. return 0;
  215. }
  216. static struct clock_event_device timer0_clockevent = {
  217. .name = "timer0",
  218. .shift = 32,
  219. .features = CLOCK_EVT_FEAT_PERIODIC | CLOCK_EVT_FEAT_ONESHOT,
  220. .set_mode = timer_set_mode,
  221. .set_next_event = timer_set_next_event,
  222. };
  223. /*
  224. * IRQ handler for the timer
  225. */
  226. static irqreturn_t bcmring_timer_interrupt(int irq, void *dev_id)
  227. {
  228. struct clock_event_device *evt = &timer0_clockevent;
  229. writel(1, TIMER0_VA_BASE + TIMER_INTCLR);
  230. evt->event_handler(evt);
  231. return IRQ_HANDLED;
  232. }
  233. static struct irqaction bcmring_timer_irq = {
  234. .name = "bcmring Timer Tick",
  235. .flags = IRQF_DISABLED | IRQF_TIMER | IRQF_IRQPOLL,
  236. .handler = bcmring_timer_interrupt,
  237. };
  238. static cycle_t bcmring_get_cycles_timer1(void)
  239. {
  240. return ~readl(TIMER1_VA_BASE + TIMER_VALUE);
  241. }
  242. static cycle_t bcmring_get_cycles_timer3(void)
  243. {
  244. return ~readl(TIMER3_VA_BASE + TIMER_VALUE);
  245. }
  246. static struct clocksource clocksource_bcmring_timer1 = {
  247. .name = "timer1",
  248. .rating = 200,
  249. .read = bcmring_get_cycles_timer1,
  250. .mask = CLOCKSOURCE_MASK(32),
  251. .shift = 20,
  252. .flags = CLOCK_SOURCE_IS_CONTINUOUS,
  253. };
  254. static struct clocksource clocksource_bcmring_timer3 = {
  255. .name = "timer3",
  256. .rating = 100,
  257. .read = bcmring_get_cycles_timer3,
  258. .mask = CLOCKSOURCE_MASK(32),
  259. .shift = 20,
  260. .flags = CLOCK_SOURCE_IS_CONTINUOUS,
  261. };
  262. static int __init bcmring_clocksource_init(void)
  263. {
  264. /* setup timer1 as free-running clocksource */
  265. writel(0, TIMER1_VA_BASE + TIMER_CTRL);
  266. writel(0xffffffff, TIMER1_VA_BASE + TIMER_LOAD);
  267. writel(0xffffffff, TIMER1_VA_BASE + TIMER_VALUE);
  268. writel(TIMER_CTRL_32BIT | TIMER_CTRL_ENABLE | TIMER_CTRL_PERIODIC,
  269. TIMER1_VA_BASE + TIMER_CTRL);
  270. clocksource_bcmring_timer1.mult =
  271. clocksource_khz2mult(TIMER1_FREQUENCY_MHZ * 1000,
  272. clocksource_bcmring_timer1.shift);
  273. clocksource_register(&clocksource_bcmring_timer1);
  274. /* setup timer3 as free-running clocksource */
  275. writel(0, TIMER3_VA_BASE + TIMER_CTRL);
  276. writel(0xffffffff, TIMER3_VA_BASE + TIMER_LOAD);
  277. writel(0xffffffff, TIMER3_VA_BASE + TIMER_VALUE);
  278. writel(TIMER_CTRL_32BIT | TIMER_CTRL_ENABLE | TIMER_CTRL_PERIODIC,
  279. TIMER3_VA_BASE + TIMER_CTRL);
  280. clocksource_bcmring_timer3.mult =
  281. clocksource_khz2mult(TIMER3_FREQUENCY_KHZ,
  282. clocksource_bcmring_timer3.shift);
  283. clocksource_register(&clocksource_bcmring_timer3);
  284. return 0;
  285. }
  286. /*
  287. * Set up timer interrupt, and return the current time in seconds.
  288. */
  289. void __init bcmring_init_timer(void)
  290. {
  291. printk(KERN_INFO "bcmring_init_timer\n");
  292. /*
  293. * Initialise to a known state (all timers off)
  294. */
  295. writel(0, TIMER0_VA_BASE + TIMER_CTRL);
  296. writel(0, TIMER1_VA_BASE + TIMER_CTRL);
  297. writel(0, TIMER2_VA_BASE + TIMER_CTRL);
  298. writel(0, TIMER3_VA_BASE + TIMER_CTRL);
  299. /*
  300. * Make irqs happen for the system timer
  301. */
  302. setup_irq(IRQ_TIMER0, &bcmring_timer_irq);
  303. bcmring_clocksource_init();
  304. timer0_clockevent.mult =
  305. div_sc(1000000, NSEC_PER_SEC, timer0_clockevent.shift);
  306. timer0_clockevent.max_delta_ns =
  307. clockevent_delta2ns(0xffffffff, &timer0_clockevent);
  308. timer0_clockevent.min_delta_ns =
  309. clockevent_delta2ns(0xf, &timer0_clockevent);
  310. timer0_clockevent.cpumask = cpumask_of(0);
  311. clockevents_register_device(&timer0_clockevent);
  312. }
  313. struct sys_timer bcmring_timer = {
  314. .init = bcmring_init_timer,
  315. };