core.c 9.8 KB

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