core.c 11 KB

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