core.c 11 KB

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