core.c 17 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734
  1. /*
  2. * linux/arch/arm/mach-realview/core.c
  3. *
  4. * Copyright (C) 1999 - 2003 ARM Limited
  5. * Copyright (C) 2000 Deep Blue Solutions Ltd
  6. *
  7. * This program is free software; you can redistribute it and/or modify
  8. * it under the terms of the GNU General Public License as published by
  9. * the Free Software Foundation; either version 2 of the License, or
  10. * (at your option) any later version.
  11. *
  12. * This program is distributed in the hope that it will be useful,
  13. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  14. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  15. * GNU General Public License for more details.
  16. *
  17. * You should have received a copy of the GNU General Public License
  18. * along with this program; if not, write to the Free Software
  19. * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
  20. */
  21. #include <linux/init.h>
  22. #include <linux/platform_device.h>
  23. #include <linux/dma-mapping.h>
  24. #include <linux/sysdev.h>
  25. #include <linux/interrupt.h>
  26. #include <linux/amba/bus.h>
  27. #include <linux/amba/clcd.h>
  28. #include <linux/clocksource.h>
  29. #include <linux/clockchips.h>
  30. #include <linux/io.h>
  31. #include <linux/smc911x.h>
  32. #include <asm/clkdev.h>
  33. #include <asm/system.h>
  34. #include <mach/hardware.h>
  35. #include <asm/irq.h>
  36. #include <asm/leds.h>
  37. #include <asm/mach-types.h>
  38. #include <asm/hardware/arm_timer.h>
  39. #include <asm/hardware/icst307.h>
  40. #include <asm/mach/arch.h>
  41. #include <asm/mach/flash.h>
  42. #include <asm/mach/irq.h>
  43. #include <asm/mach/map.h>
  44. #include <asm/mach/mmc.h>
  45. #include <asm/hardware/gic.h>
  46. #include "core.h"
  47. #include "clock.h"
  48. #define REALVIEW_REFCOUNTER (__io_address(REALVIEW_SYS_BASE) + REALVIEW_SYS_24MHz_OFFSET)
  49. /* used by entry-macro.S and platsmp.c */
  50. void __iomem *gic_cpu_base_addr;
  51. /*
  52. * This is the RealView sched_clock implementation. This has
  53. * a resolution of 41.7ns, and a maximum value of about 179s.
  54. */
  55. unsigned long long sched_clock(void)
  56. {
  57. unsigned long long v;
  58. v = (unsigned long long)readl(REALVIEW_REFCOUNTER) * 125;
  59. do_div(v, 3);
  60. return v;
  61. }
  62. #define REALVIEW_FLASHCTRL (__io_address(REALVIEW_SYS_BASE) + REALVIEW_SYS_FLASH_OFFSET)
  63. static int realview_flash_init(void)
  64. {
  65. u32 val;
  66. val = __raw_readl(REALVIEW_FLASHCTRL);
  67. val &= ~REALVIEW_FLASHPROG_FLVPPEN;
  68. __raw_writel(val, REALVIEW_FLASHCTRL);
  69. return 0;
  70. }
  71. static void realview_flash_exit(void)
  72. {
  73. u32 val;
  74. val = __raw_readl(REALVIEW_FLASHCTRL);
  75. val &= ~REALVIEW_FLASHPROG_FLVPPEN;
  76. __raw_writel(val, REALVIEW_FLASHCTRL);
  77. }
  78. static void realview_flash_set_vpp(int on)
  79. {
  80. u32 val;
  81. val = __raw_readl(REALVIEW_FLASHCTRL);
  82. if (on)
  83. val |= REALVIEW_FLASHPROG_FLVPPEN;
  84. else
  85. val &= ~REALVIEW_FLASHPROG_FLVPPEN;
  86. __raw_writel(val, REALVIEW_FLASHCTRL);
  87. }
  88. static struct flash_platform_data realview_flash_data = {
  89. .map_name = "cfi_probe",
  90. .width = 4,
  91. .init = realview_flash_init,
  92. .exit = realview_flash_exit,
  93. .set_vpp = realview_flash_set_vpp,
  94. };
  95. struct platform_device realview_flash_device = {
  96. .name = "armflash",
  97. .id = 0,
  98. .dev = {
  99. .platform_data = &realview_flash_data,
  100. },
  101. };
  102. int realview_flash_register(struct resource *res, u32 num)
  103. {
  104. realview_flash_device.resource = res;
  105. realview_flash_device.num_resources = num;
  106. return platform_device_register(&realview_flash_device);
  107. }
  108. static struct smc911x_platdata realview_smc911x_platdata = {
  109. .flags = SMC911X_USE_32BIT,
  110. .irq_flags = IRQF_SHARED,
  111. .irq_polarity = 1,
  112. };
  113. static struct platform_device realview_eth_device = {
  114. .name = "smc911x",
  115. .id = 0,
  116. .num_resources = 2,
  117. };
  118. int realview_eth_register(const char *name, struct resource *res)
  119. {
  120. if (name)
  121. realview_eth_device.name = name;
  122. realview_eth_device.resource = res;
  123. if (strcmp(realview_eth_device.name, "smc911x") == 0)
  124. realview_eth_device.dev.platform_data = &realview_smc911x_platdata;
  125. return platform_device_register(&realview_eth_device);
  126. }
  127. static struct resource realview_i2c_resource = {
  128. .start = REALVIEW_I2C_BASE,
  129. .end = REALVIEW_I2C_BASE + SZ_4K - 1,
  130. .flags = IORESOURCE_MEM,
  131. };
  132. struct platform_device realview_i2c_device = {
  133. .name = "versatile-i2c",
  134. .id = -1,
  135. .num_resources = 1,
  136. .resource = &realview_i2c_resource,
  137. };
  138. #define REALVIEW_SYSMCI (__io_address(REALVIEW_SYS_BASE) + REALVIEW_SYS_MCI_OFFSET)
  139. static unsigned int realview_mmc_status(struct device *dev)
  140. {
  141. struct amba_device *adev = container_of(dev, struct amba_device, dev);
  142. u32 mask;
  143. if (adev->res.start == REALVIEW_MMCI0_BASE)
  144. mask = 1;
  145. else
  146. mask = 2;
  147. return readl(REALVIEW_SYSMCI) & mask;
  148. }
  149. struct mmc_platform_data realview_mmc0_plat_data = {
  150. .ocr_mask = MMC_VDD_32_33|MMC_VDD_33_34,
  151. .status = realview_mmc_status,
  152. };
  153. struct mmc_platform_data realview_mmc1_plat_data = {
  154. .ocr_mask = MMC_VDD_32_33|MMC_VDD_33_34,
  155. .status = realview_mmc_status,
  156. };
  157. /*
  158. * Clock handling
  159. */
  160. static const struct icst307_params realview_oscvco_params = {
  161. .ref = 24000,
  162. .vco_max = 200000,
  163. .vd_min = 4 + 8,
  164. .vd_max = 511 + 8,
  165. .rd_min = 1 + 2,
  166. .rd_max = 127 + 2,
  167. };
  168. static void realview_oscvco_set(struct clk *clk, struct icst307_vco vco)
  169. {
  170. void __iomem *sys_lock = __io_address(REALVIEW_SYS_BASE) + REALVIEW_SYS_LOCK_OFFSET;
  171. void __iomem *sys_osc;
  172. u32 val;
  173. if (machine_is_realview_pb1176())
  174. sys_osc = __io_address(REALVIEW_SYS_BASE) + REALVIEW_SYS_OSC0_OFFSET;
  175. else
  176. sys_osc = __io_address(REALVIEW_SYS_BASE) + REALVIEW_SYS_OSC4_OFFSET;
  177. val = readl(sys_osc) & ~0x7ffff;
  178. val |= vco.v | (vco.r << 9) | (vco.s << 16);
  179. writel(0xa05f, sys_lock);
  180. writel(val, sys_osc);
  181. writel(0, sys_lock);
  182. }
  183. static struct clk oscvco_clk = {
  184. .params = &realview_oscvco_params,
  185. .setvco = realview_oscvco_set,
  186. };
  187. /*
  188. * These are fixed clocks.
  189. */
  190. static struct clk ref24_clk = {
  191. .rate = 24000000,
  192. };
  193. static struct clk_lookup lookups[] = {
  194. { /* UART0 */
  195. .dev_id = "dev:f1",
  196. .clk = &ref24_clk,
  197. }, { /* UART1 */
  198. .dev_id = "dev:f2",
  199. .clk = &ref24_clk,
  200. }, { /* UART2 */
  201. .dev_id = "dev:f3",
  202. .clk = &ref24_clk,
  203. }, { /* UART3 */
  204. .dev_id = "fpga:09",
  205. .clk = &ref24_clk,
  206. }, { /* KMI0 */
  207. .dev_id = "fpga:06",
  208. .clk = &ref24_clk,
  209. }, { /* KMI1 */
  210. .dev_id = "fpga:07",
  211. .clk = &ref24_clk,
  212. }, { /* MMC0 */
  213. .dev_id = "fpga:05",
  214. .clk = &ref24_clk,
  215. }, { /* EB:CLCD */
  216. .dev_id = "dev:20",
  217. .clk = &oscvco_clk,
  218. }, { /* PB:CLCD */
  219. .dev_id = "issp:20",
  220. .clk = &oscvco_clk,
  221. }
  222. };
  223. static int __init clk_init(void)
  224. {
  225. int i;
  226. for (i = 0; i < ARRAY_SIZE(lookups); i++)
  227. clkdev_add(&lookups[i]);
  228. return 0;
  229. }
  230. arch_initcall(clk_init);
  231. /*
  232. * CLCD support.
  233. */
  234. #define SYS_CLCD_NLCDIOON (1 << 2)
  235. #define SYS_CLCD_VDDPOSSWITCH (1 << 3)
  236. #define SYS_CLCD_PWR3V5SWITCH (1 << 4)
  237. #define SYS_CLCD_ID_MASK (0x1f << 8)
  238. #define SYS_CLCD_ID_SANYO_3_8 (0x00 << 8)
  239. #define SYS_CLCD_ID_UNKNOWN_8_4 (0x01 << 8)
  240. #define SYS_CLCD_ID_EPSON_2_2 (0x02 << 8)
  241. #define SYS_CLCD_ID_SANYO_2_5 (0x07 << 8)
  242. #define SYS_CLCD_ID_VGA (0x1f << 8)
  243. static struct clcd_panel vga = {
  244. .mode = {
  245. .name = "VGA",
  246. .refresh = 60,
  247. .xres = 640,
  248. .yres = 480,
  249. .pixclock = 39721,
  250. .left_margin = 40,
  251. .right_margin = 24,
  252. .upper_margin = 32,
  253. .lower_margin = 11,
  254. .hsync_len = 96,
  255. .vsync_len = 2,
  256. .sync = 0,
  257. .vmode = FB_VMODE_NONINTERLACED,
  258. },
  259. .width = -1,
  260. .height = -1,
  261. .tim2 = TIM2_BCD | TIM2_IPC,
  262. .cntl = CNTL_LCDTFT | CNTL_BGR | CNTL_LCDVCOMP(1),
  263. .bpp = 16,
  264. };
  265. static struct clcd_panel xvga = {
  266. .mode = {
  267. .name = "XVGA",
  268. .refresh = 60,
  269. .xres = 1024,
  270. .yres = 768,
  271. .pixclock = 15748,
  272. .left_margin = 152,
  273. .right_margin = 48,
  274. .upper_margin = 23,
  275. .lower_margin = 3,
  276. .hsync_len = 104,
  277. .vsync_len = 4,
  278. .sync = 0,
  279. .vmode = FB_VMODE_NONINTERLACED,
  280. },
  281. .width = -1,
  282. .height = -1,
  283. .tim2 = TIM2_BCD | TIM2_IPC,
  284. .cntl = CNTL_LCDTFT | CNTL_BGR | CNTL_LCDVCOMP(1),
  285. .bpp = 16,
  286. };
  287. static struct clcd_panel sanyo_3_8_in = {
  288. .mode = {
  289. .name = "Sanyo QVGA",
  290. .refresh = 116,
  291. .xres = 320,
  292. .yres = 240,
  293. .pixclock = 100000,
  294. .left_margin = 6,
  295. .right_margin = 6,
  296. .upper_margin = 5,
  297. .lower_margin = 5,
  298. .hsync_len = 6,
  299. .vsync_len = 6,
  300. .sync = 0,
  301. .vmode = FB_VMODE_NONINTERLACED,
  302. },
  303. .width = -1,
  304. .height = -1,
  305. .tim2 = TIM2_BCD,
  306. .cntl = CNTL_LCDTFT | CNTL_BGR | CNTL_LCDVCOMP(1),
  307. .bpp = 16,
  308. };
  309. static struct clcd_panel sanyo_2_5_in = {
  310. .mode = {
  311. .name = "Sanyo QVGA Portrait",
  312. .refresh = 116,
  313. .xres = 240,
  314. .yres = 320,
  315. .pixclock = 100000,
  316. .left_margin = 20,
  317. .right_margin = 10,
  318. .upper_margin = 2,
  319. .lower_margin = 2,
  320. .hsync_len = 10,
  321. .vsync_len = 2,
  322. .sync = FB_SYNC_HOR_HIGH_ACT | FB_SYNC_VERT_HIGH_ACT,
  323. .vmode = FB_VMODE_NONINTERLACED,
  324. },
  325. .width = -1,
  326. .height = -1,
  327. .tim2 = TIM2_IVS | TIM2_IHS | TIM2_IPC,
  328. .cntl = CNTL_LCDTFT | CNTL_BGR | CNTL_LCDVCOMP(1),
  329. .bpp = 16,
  330. };
  331. static struct clcd_panel epson_2_2_in = {
  332. .mode = {
  333. .name = "Epson QCIF",
  334. .refresh = 390,
  335. .xres = 176,
  336. .yres = 220,
  337. .pixclock = 62500,
  338. .left_margin = 3,
  339. .right_margin = 2,
  340. .upper_margin = 1,
  341. .lower_margin = 0,
  342. .hsync_len = 3,
  343. .vsync_len = 2,
  344. .sync = 0,
  345. .vmode = FB_VMODE_NONINTERLACED,
  346. },
  347. .width = -1,
  348. .height = -1,
  349. .tim2 = TIM2_BCD | TIM2_IPC,
  350. .cntl = CNTL_LCDTFT | CNTL_BGR | CNTL_LCDVCOMP(1),
  351. .bpp = 16,
  352. };
  353. /*
  354. * Detect which LCD panel is connected, and return the appropriate
  355. * clcd_panel structure. Note: we do not have any information on
  356. * the required timings for the 8.4in panel, so we presently assume
  357. * VGA timings.
  358. */
  359. static struct clcd_panel *realview_clcd_panel(void)
  360. {
  361. void __iomem *sys_clcd = __io_address(REALVIEW_SYS_BASE) + REALVIEW_SYS_CLCD_OFFSET;
  362. struct clcd_panel *vga_panel;
  363. struct clcd_panel *panel;
  364. u32 val;
  365. if (machine_is_realview_eb())
  366. vga_panel = &vga;
  367. else
  368. vga_panel = &xvga;
  369. val = readl(sys_clcd) & SYS_CLCD_ID_MASK;
  370. if (val == SYS_CLCD_ID_SANYO_3_8)
  371. panel = &sanyo_3_8_in;
  372. else if (val == SYS_CLCD_ID_SANYO_2_5)
  373. panel = &sanyo_2_5_in;
  374. else if (val == SYS_CLCD_ID_EPSON_2_2)
  375. panel = &epson_2_2_in;
  376. else if (val == SYS_CLCD_ID_VGA)
  377. panel = vga_panel;
  378. else {
  379. printk(KERN_ERR "CLCD: unknown LCD panel ID 0x%08x, using VGA\n",
  380. val);
  381. panel = vga_panel;
  382. }
  383. return panel;
  384. }
  385. /*
  386. * Disable all display connectors on the interface module.
  387. */
  388. static void realview_clcd_disable(struct clcd_fb *fb)
  389. {
  390. void __iomem *sys_clcd = __io_address(REALVIEW_SYS_BASE) + REALVIEW_SYS_CLCD_OFFSET;
  391. u32 val;
  392. val = readl(sys_clcd);
  393. val &= ~SYS_CLCD_NLCDIOON | SYS_CLCD_PWR3V5SWITCH;
  394. writel(val, sys_clcd);
  395. }
  396. /*
  397. * Enable the relevant connector on the interface module.
  398. */
  399. static void realview_clcd_enable(struct clcd_fb *fb)
  400. {
  401. void __iomem *sys_clcd = __io_address(REALVIEW_SYS_BASE) + REALVIEW_SYS_CLCD_OFFSET;
  402. u32 val;
  403. /*
  404. * Enable the PSUs
  405. */
  406. val = readl(sys_clcd);
  407. val |= SYS_CLCD_NLCDIOON | SYS_CLCD_PWR3V5SWITCH;
  408. writel(val, sys_clcd);
  409. }
  410. static int realview_clcd_setup(struct clcd_fb *fb)
  411. {
  412. unsigned long framesize;
  413. dma_addr_t dma;
  414. if (machine_is_realview_eb())
  415. /* VGA, 16bpp */
  416. framesize = 640 * 480 * 2;
  417. else
  418. /* XVGA, 16bpp */
  419. framesize = 1024 * 768 * 2;
  420. fb->panel = realview_clcd_panel();
  421. fb->fb.screen_base = dma_alloc_writecombine(&fb->dev->dev, framesize,
  422. &dma, GFP_KERNEL);
  423. if (!fb->fb.screen_base) {
  424. printk(KERN_ERR "CLCD: unable to map framebuffer\n");
  425. return -ENOMEM;
  426. }
  427. fb->fb.fix.smem_start = dma;
  428. fb->fb.fix.smem_len = framesize;
  429. return 0;
  430. }
  431. static int realview_clcd_mmap(struct clcd_fb *fb, struct vm_area_struct *vma)
  432. {
  433. return dma_mmap_writecombine(&fb->dev->dev, vma,
  434. fb->fb.screen_base,
  435. fb->fb.fix.smem_start,
  436. fb->fb.fix.smem_len);
  437. }
  438. static void realview_clcd_remove(struct clcd_fb *fb)
  439. {
  440. dma_free_writecombine(&fb->dev->dev, fb->fb.fix.smem_len,
  441. fb->fb.screen_base, fb->fb.fix.smem_start);
  442. }
  443. struct clcd_board clcd_plat_data = {
  444. .name = "RealView",
  445. .check = clcdfb_check,
  446. .decode = clcdfb_decode,
  447. .disable = realview_clcd_disable,
  448. .enable = realview_clcd_enable,
  449. .setup = realview_clcd_setup,
  450. .mmap = realview_clcd_mmap,
  451. .remove = realview_clcd_remove,
  452. };
  453. #ifdef CONFIG_LEDS
  454. #define VA_LEDS_BASE (__io_address(REALVIEW_SYS_BASE) + REALVIEW_SYS_LED_OFFSET)
  455. void realview_leds_event(led_event_t ledevt)
  456. {
  457. unsigned long flags;
  458. u32 val;
  459. local_irq_save(flags);
  460. val = readl(VA_LEDS_BASE);
  461. switch (ledevt) {
  462. case led_idle_start:
  463. val = val & ~REALVIEW_SYS_LED0;
  464. break;
  465. case led_idle_end:
  466. val = val | REALVIEW_SYS_LED0;
  467. break;
  468. case led_timer:
  469. val = val ^ REALVIEW_SYS_LED1;
  470. break;
  471. case led_halted:
  472. val = 0;
  473. break;
  474. default:
  475. break;
  476. }
  477. writel(val, VA_LEDS_BASE);
  478. local_irq_restore(flags);
  479. }
  480. #endif /* CONFIG_LEDS */
  481. /*
  482. * Where is the timer (VA)?
  483. */
  484. void __iomem *timer0_va_base;
  485. void __iomem *timer1_va_base;
  486. void __iomem *timer2_va_base;
  487. void __iomem *timer3_va_base;
  488. /*
  489. * How long is the timer interval?
  490. */
  491. #define TIMER_INTERVAL (TICKS_PER_uSEC * mSEC_10)
  492. #if TIMER_INTERVAL >= 0x100000
  493. #define TIMER_RELOAD (TIMER_INTERVAL >> 8)
  494. #define TIMER_DIVISOR (TIMER_CTRL_DIV256)
  495. #define TICKS2USECS(x) (256 * (x) / TICKS_PER_uSEC)
  496. #elif TIMER_INTERVAL >= 0x10000
  497. #define TIMER_RELOAD (TIMER_INTERVAL >> 4) /* Divide by 16 */
  498. #define TIMER_DIVISOR (TIMER_CTRL_DIV16)
  499. #define TICKS2USECS(x) (16 * (x) / TICKS_PER_uSEC)
  500. #else
  501. #define TIMER_RELOAD (TIMER_INTERVAL)
  502. #define TIMER_DIVISOR (TIMER_CTRL_DIV1)
  503. #define TICKS2USECS(x) ((x) / TICKS_PER_uSEC)
  504. #endif
  505. static void timer_set_mode(enum clock_event_mode mode,
  506. struct clock_event_device *clk)
  507. {
  508. unsigned long ctrl;
  509. switch(mode) {
  510. case CLOCK_EVT_MODE_PERIODIC:
  511. writel(TIMER_RELOAD, timer0_va_base + TIMER_LOAD);
  512. ctrl = TIMER_CTRL_PERIODIC;
  513. ctrl |= TIMER_CTRL_32BIT | TIMER_CTRL_IE | TIMER_CTRL_ENABLE;
  514. break;
  515. case CLOCK_EVT_MODE_ONESHOT:
  516. /* period set, and timer enabled in 'next_event' hook */
  517. ctrl = TIMER_CTRL_ONESHOT;
  518. ctrl |= TIMER_CTRL_32BIT | TIMER_CTRL_IE;
  519. break;
  520. case CLOCK_EVT_MODE_UNUSED:
  521. case CLOCK_EVT_MODE_SHUTDOWN:
  522. default:
  523. ctrl = 0;
  524. }
  525. writel(ctrl, timer0_va_base + TIMER_CTRL);
  526. }
  527. static int timer_set_next_event(unsigned long evt,
  528. struct clock_event_device *unused)
  529. {
  530. unsigned long ctrl = readl(timer0_va_base + TIMER_CTRL);
  531. writel(evt, timer0_va_base + TIMER_LOAD);
  532. writel(ctrl | TIMER_CTRL_ENABLE, timer0_va_base + TIMER_CTRL);
  533. return 0;
  534. }
  535. static struct clock_event_device timer0_clockevent = {
  536. .name = "timer0",
  537. .shift = 32,
  538. .features = CLOCK_EVT_FEAT_PERIODIC | CLOCK_EVT_FEAT_ONESHOT,
  539. .set_mode = timer_set_mode,
  540. .set_next_event = timer_set_next_event,
  541. .rating = 300,
  542. .cpumask = cpu_all_mask,
  543. };
  544. static void __init realview_clockevents_init(unsigned int timer_irq)
  545. {
  546. timer0_clockevent.irq = timer_irq;
  547. timer0_clockevent.mult =
  548. div_sc(1000000, NSEC_PER_SEC, timer0_clockevent.shift);
  549. timer0_clockevent.max_delta_ns =
  550. clockevent_delta2ns(0xffffffff, &timer0_clockevent);
  551. timer0_clockevent.min_delta_ns =
  552. clockevent_delta2ns(0xf, &timer0_clockevent);
  553. clockevents_register_device(&timer0_clockevent);
  554. }
  555. /*
  556. * IRQ handler for the timer
  557. */
  558. static irqreturn_t realview_timer_interrupt(int irq, void *dev_id)
  559. {
  560. struct clock_event_device *evt = &timer0_clockevent;
  561. /* clear the interrupt */
  562. writel(1, timer0_va_base + TIMER_INTCLR);
  563. evt->event_handler(evt);
  564. return IRQ_HANDLED;
  565. }
  566. static struct irqaction realview_timer_irq = {
  567. .name = "RealView Timer Tick",
  568. .flags = IRQF_DISABLED | IRQF_TIMER | IRQF_IRQPOLL,
  569. .handler = realview_timer_interrupt,
  570. };
  571. static cycle_t realview_get_cycles(void)
  572. {
  573. return ~readl(timer3_va_base + TIMER_VALUE);
  574. }
  575. static struct clocksource clocksource_realview = {
  576. .name = "timer3",
  577. .rating = 200,
  578. .read = realview_get_cycles,
  579. .mask = CLOCKSOURCE_MASK(32),
  580. .shift = 20,
  581. .flags = CLOCK_SOURCE_IS_CONTINUOUS,
  582. };
  583. static void __init realview_clocksource_init(void)
  584. {
  585. /* setup timer 0 as free-running clocksource */
  586. writel(0, timer3_va_base + TIMER_CTRL);
  587. writel(0xffffffff, timer3_va_base + TIMER_LOAD);
  588. writel(0xffffffff, timer3_va_base + TIMER_VALUE);
  589. writel(TIMER_CTRL_32BIT | TIMER_CTRL_ENABLE | TIMER_CTRL_PERIODIC,
  590. timer3_va_base + TIMER_CTRL);
  591. clocksource_realview.mult =
  592. clocksource_khz2mult(1000, clocksource_realview.shift);
  593. clocksource_register(&clocksource_realview);
  594. }
  595. /*
  596. * Set up the clock source and clock events devices
  597. */
  598. void __init realview_timer_init(unsigned int timer_irq)
  599. {
  600. u32 val;
  601. #ifdef CONFIG_GENERIC_CLOCKEVENTS_BROADCAST
  602. /*
  603. * The dummy clock device has to be registered before the main device
  604. * so that the latter will broadcast the clock events
  605. */
  606. local_timer_setup();
  607. #endif
  608. /*
  609. * set clock frequency:
  610. * REALVIEW_REFCLK is 32KHz
  611. * REALVIEW_TIMCLK is 1MHz
  612. */
  613. val = readl(__io_address(REALVIEW_SCTL_BASE));
  614. writel((REALVIEW_TIMCLK << REALVIEW_TIMER1_EnSel) |
  615. (REALVIEW_TIMCLK << REALVIEW_TIMER2_EnSel) |
  616. (REALVIEW_TIMCLK << REALVIEW_TIMER3_EnSel) |
  617. (REALVIEW_TIMCLK << REALVIEW_TIMER4_EnSel) | val,
  618. __io_address(REALVIEW_SCTL_BASE));
  619. /*
  620. * Initialise to a known state (all timers off)
  621. */
  622. writel(0, timer0_va_base + TIMER_CTRL);
  623. writel(0, timer1_va_base + TIMER_CTRL);
  624. writel(0, timer2_va_base + TIMER_CTRL);
  625. writel(0, timer3_va_base + TIMER_CTRL);
  626. /*
  627. * Make irqs happen for the system timer
  628. */
  629. setup_irq(timer_irq, &realview_timer_irq);
  630. realview_clocksource_init();
  631. realview_clockevents_init(timer_irq);
  632. }