core.c 17 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748
  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 = 0,
  135. .num_resources = 1,
  136. .resource = &realview_i2c_resource,
  137. };
  138. static struct i2c_board_info realview_i2c_board_info[] = {
  139. {
  140. I2C_BOARD_INFO("rtc-ds1307", 0xd0 >> 1),
  141. .type = "ds1338",
  142. },
  143. };
  144. static int __init realview_i2c_init(void)
  145. {
  146. return i2c_register_board_info(0, realview_i2c_board_info,
  147. ARRAY_SIZE(realview_i2c_board_info));
  148. }
  149. arch_initcall(realview_i2c_init);
  150. #define REALVIEW_SYSMCI (__io_address(REALVIEW_SYS_BASE) + REALVIEW_SYS_MCI_OFFSET)
  151. static unsigned int realview_mmc_status(struct device *dev)
  152. {
  153. struct amba_device *adev = container_of(dev, struct amba_device, dev);
  154. u32 mask;
  155. if (adev->res.start == REALVIEW_MMCI0_BASE)
  156. mask = 1;
  157. else
  158. mask = 2;
  159. return readl(REALVIEW_SYSMCI) & mask;
  160. }
  161. struct mmc_platform_data realview_mmc0_plat_data = {
  162. .ocr_mask = MMC_VDD_32_33|MMC_VDD_33_34,
  163. .status = realview_mmc_status,
  164. };
  165. struct mmc_platform_data realview_mmc1_plat_data = {
  166. .ocr_mask = MMC_VDD_32_33|MMC_VDD_33_34,
  167. .status = realview_mmc_status,
  168. };
  169. /*
  170. * Clock handling
  171. */
  172. static const struct icst307_params realview_oscvco_params = {
  173. .ref = 24000,
  174. .vco_max = 200000,
  175. .vd_min = 4 + 8,
  176. .vd_max = 511 + 8,
  177. .rd_min = 1 + 2,
  178. .rd_max = 127 + 2,
  179. };
  180. static void realview_oscvco_set(struct clk *clk, struct icst307_vco vco)
  181. {
  182. void __iomem *sys_lock = __io_address(REALVIEW_SYS_BASE) + REALVIEW_SYS_LOCK_OFFSET;
  183. void __iomem *sys_osc;
  184. u32 val;
  185. if (machine_is_realview_pb1176())
  186. sys_osc = __io_address(REALVIEW_SYS_BASE) + REALVIEW_SYS_OSC0_OFFSET;
  187. else
  188. sys_osc = __io_address(REALVIEW_SYS_BASE) + REALVIEW_SYS_OSC4_OFFSET;
  189. val = readl(sys_osc) & ~0x7ffff;
  190. val |= vco.v | (vco.r << 9) | (vco.s << 16);
  191. writel(0xa05f, sys_lock);
  192. writel(val, sys_osc);
  193. writel(0, sys_lock);
  194. }
  195. static struct clk oscvco_clk = {
  196. .params = &realview_oscvco_params,
  197. .setvco = realview_oscvco_set,
  198. };
  199. /*
  200. * These are fixed clocks.
  201. */
  202. static struct clk ref24_clk = {
  203. .rate = 24000000,
  204. };
  205. static struct clk_lookup lookups[] = {
  206. { /* UART0 */
  207. .dev_id = "dev:f1",
  208. .clk = &ref24_clk,
  209. }, { /* UART1 */
  210. .dev_id = "dev:f2",
  211. .clk = &ref24_clk,
  212. }, { /* UART2 */
  213. .dev_id = "dev:f3",
  214. .clk = &ref24_clk,
  215. }, { /* UART3 */
  216. .dev_id = "fpga:09",
  217. .clk = &ref24_clk,
  218. }, { /* KMI0 */
  219. .dev_id = "fpga:06",
  220. .clk = &ref24_clk,
  221. }, { /* KMI1 */
  222. .dev_id = "fpga:07",
  223. .clk = &ref24_clk,
  224. }, { /* MMC0 */
  225. .dev_id = "fpga:05",
  226. .clk = &ref24_clk,
  227. }, { /* EB:CLCD */
  228. .dev_id = "dev:20",
  229. .clk = &oscvco_clk,
  230. }, { /* PB:CLCD */
  231. .dev_id = "issp:20",
  232. .clk = &oscvco_clk,
  233. }
  234. };
  235. static int __init clk_init(void)
  236. {
  237. int i;
  238. for (i = 0; i < ARRAY_SIZE(lookups); i++)
  239. clkdev_add(&lookups[i]);
  240. return 0;
  241. }
  242. arch_initcall(clk_init);
  243. /*
  244. * CLCD support.
  245. */
  246. #define SYS_CLCD_NLCDIOON (1 << 2)
  247. #define SYS_CLCD_VDDPOSSWITCH (1 << 3)
  248. #define SYS_CLCD_PWR3V5SWITCH (1 << 4)
  249. #define SYS_CLCD_ID_MASK (0x1f << 8)
  250. #define SYS_CLCD_ID_SANYO_3_8 (0x00 << 8)
  251. #define SYS_CLCD_ID_UNKNOWN_8_4 (0x01 << 8)
  252. #define SYS_CLCD_ID_EPSON_2_2 (0x02 << 8)
  253. #define SYS_CLCD_ID_SANYO_2_5 (0x07 << 8)
  254. #define SYS_CLCD_ID_VGA (0x1f << 8)
  255. static struct clcd_panel vga = {
  256. .mode = {
  257. .name = "VGA",
  258. .refresh = 60,
  259. .xres = 640,
  260. .yres = 480,
  261. .pixclock = 39721,
  262. .left_margin = 40,
  263. .right_margin = 24,
  264. .upper_margin = 32,
  265. .lower_margin = 11,
  266. .hsync_len = 96,
  267. .vsync_len = 2,
  268. .sync = 0,
  269. .vmode = FB_VMODE_NONINTERLACED,
  270. },
  271. .width = -1,
  272. .height = -1,
  273. .tim2 = TIM2_BCD | TIM2_IPC,
  274. .cntl = CNTL_LCDTFT | CNTL_BGR | CNTL_LCDVCOMP(1),
  275. .bpp = 16,
  276. };
  277. static struct clcd_panel xvga = {
  278. .mode = {
  279. .name = "XVGA",
  280. .refresh = 60,
  281. .xres = 1024,
  282. .yres = 768,
  283. .pixclock = 15748,
  284. .left_margin = 152,
  285. .right_margin = 48,
  286. .upper_margin = 23,
  287. .lower_margin = 3,
  288. .hsync_len = 104,
  289. .vsync_len = 4,
  290. .sync = 0,
  291. .vmode = FB_VMODE_NONINTERLACED,
  292. },
  293. .width = -1,
  294. .height = -1,
  295. .tim2 = TIM2_BCD | TIM2_IPC,
  296. .cntl = CNTL_LCDTFT | CNTL_BGR | CNTL_LCDVCOMP(1),
  297. .bpp = 16,
  298. };
  299. static struct clcd_panel sanyo_3_8_in = {
  300. .mode = {
  301. .name = "Sanyo QVGA",
  302. .refresh = 116,
  303. .xres = 320,
  304. .yres = 240,
  305. .pixclock = 100000,
  306. .left_margin = 6,
  307. .right_margin = 6,
  308. .upper_margin = 5,
  309. .lower_margin = 5,
  310. .hsync_len = 6,
  311. .vsync_len = 6,
  312. .sync = 0,
  313. .vmode = FB_VMODE_NONINTERLACED,
  314. },
  315. .width = -1,
  316. .height = -1,
  317. .tim2 = TIM2_BCD,
  318. .cntl = CNTL_LCDTFT | CNTL_BGR | CNTL_LCDVCOMP(1),
  319. .bpp = 16,
  320. };
  321. static struct clcd_panel sanyo_2_5_in = {
  322. .mode = {
  323. .name = "Sanyo QVGA Portrait",
  324. .refresh = 116,
  325. .xres = 240,
  326. .yres = 320,
  327. .pixclock = 100000,
  328. .left_margin = 20,
  329. .right_margin = 10,
  330. .upper_margin = 2,
  331. .lower_margin = 2,
  332. .hsync_len = 10,
  333. .vsync_len = 2,
  334. .sync = FB_SYNC_HOR_HIGH_ACT | FB_SYNC_VERT_HIGH_ACT,
  335. .vmode = FB_VMODE_NONINTERLACED,
  336. },
  337. .width = -1,
  338. .height = -1,
  339. .tim2 = TIM2_IVS | TIM2_IHS | TIM2_IPC,
  340. .cntl = CNTL_LCDTFT | CNTL_BGR | CNTL_LCDVCOMP(1),
  341. .bpp = 16,
  342. };
  343. static struct clcd_panel epson_2_2_in = {
  344. .mode = {
  345. .name = "Epson QCIF",
  346. .refresh = 390,
  347. .xres = 176,
  348. .yres = 220,
  349. .pixclock = 62500,
  350. .left_margin = 3,
  351. .right_margin = 2,
  352. .upper_margin = 1,
  353. .lower_margin = 0,
  354. .hsync_len = 3,
  355. .vsync_len = 2,
  356. .sync = 0,
  357. .vmode = FB_VMODE_NONINTERLACED,
  358. },
  359. .width = -1,
  360. .height = -1,
  361. .tim2 = TIM2_BCD | TIM2_IPC,
  362. .cntl = CNTL_LCDTFT | CNTL_BGR | CNTL_LCDVCOMP(1),
  363. .bpp = 16,
  364. };
  365. /*
  366. * Detect which LCD panel is connected, and return the appropriate
  367. * clcd_panel structure. Note: we do not have any information on
  368. * the required timings for the 8.4in panel, so we presently assume
  369. * VGA timings.
  370. */
  371. static struct clcd_panel *realview_clcd_panel(void)
  372. {
  373. void __iomem *sys_clcd = __io_address(REALVIEW_SYS_BASE) + REALVIEW_SYS_CLCD_OFFSET;
  374. struct clcd_panel *vga_panel;
  375. struct clcd_panel *panel;
  376. u32 val;
  377. if (machine_is_realview_eb())
  378. vga_panel = &vga;
  379. else
  380. vga_panel = &xvga;
  381. val = readl(sys_clcd) & SYS_CLCD_ID_MASK;
  382. if (val == SYS_CLCD_ID_SANYO_3_8)
  383. panel = &sanyo_3_8_in;
  384. else if (val == SYS_CLCD_ID_SANYO_2_5)
  385. panel = &sanyo_2_5_in;
  386. else if (val == SYS_CLCD_ID_EPSON_2_2)
  387. panel = &epson_2_2_in;
  388. else if (val == SYS_CLCD_ID_VGA)
  389. panel = vga_panel;
  390. else {
  391. printk(KERN_ERR "CLCD: unknown LCD panel ID 0x%08x, using VGA\n",
  392. val);
  393. panel = vga_panel;
  394. }
  395. return panel;
  396. }
  397. /*
  398. * Disable all display connectors on the interface module.
  399. */
  400. static void realview_clcd_disable(struct clcd_fb *fb)
  401. {
  402. void __iomem *sys_clcd = __io_address(REALVIEW_SYS_BASE) + REALVIEW_SYS_CLCD_OFFSET;
  403. u32 val;
  404. val = readl(sys_clcd);
  405. val &= ~SYS_CLCD_NLCDIOON | SYS_CLCD_PWR3V5SWITCH;
  406. writel(val, sys_clcd);
  407. }
  408. /*
  409. * Enable the relevant connector on the interface module.
  410. */
  411. static void realview_clcd_enable(struct clcd_fb *fb)
  412. {
  413. void __iomem *sys_clcd = __io_address(REALVIEW_SYS_BASE) + REALVIEW_SYS_CLCD_OFFSET;
  414. u32 val;
  415. /*
  416. * Enable the PSUs
  417. */
  418. val = readl(sys_clcd);
  419. val |= SYS_CLCD_NLCDIOON | SYS_CLCD_PWR3V5SWITCH;
  420. writel(val, sys_clcd);
  421. }
  422. static int realview_clcd_setup(struct clcd_fb *fb)
  423. {
  424. unsigned long framesize;
  425. dma_addr_t dma;
  426. if (machine_is_realview_eb())
  427. /* VGA, 16bpp */
  428. framesize = 640 * 480 * 2;
  429. else
  430. /* XVGA, 16bpp */
  431. framesize = 1024 * 768 * 2;
  432. fb->panel = realview_clcd_panel();
  433. fb->fb.screen_base = dma_alloc_writecombine(&fb->dev->dev, framesize,
  434. &dma, GFP_KERNEL);
  435. if (!fb->fb.screen_base) {
  436. printk(KERN_ERR "CLCD: unable to map framebuffer\n");
  437. return -ENOMEM;
  438. }
  439. fb->fb.fix.smem_start = dma;
  440. fb->fb.fix.smem_len = framesize;
  441. return 0;
  442. }
  443. static int realview_clcd_mmap(struct clcd_fb *fb, struct vm_area_struct *vma)
  444. {
  445. return dma_mmap_writecombine(&fb->dev->dev, vma,
  446. fb->fb.screen_base,
  447. fb->fb.fix.smem_start,
  448. fb->fb.fix.smem_len);
  449. }
  450. static void realview_clcd_remove(struct clcd_fb *fb)
  451. {
  452. dma_free_writecombine(&fb->dev->dev, fb->fb.fix.smem_len,
  453. fb->fb.screen_base, fb->fb.fix.smem_start);
  454. }
  455. struct clcd_board clcd_plat_data = {
  456. .name = "RealView",
  457. .check = clcdfb_check,
  458. .decode = clcdfb_decode,
  459. .disable = realview_clcd_disable,
  460. .enable = realview_clcd_enable,
  461. .setup = realview_clcd_setup,
  462. .mmap = realview_clcd_mmap,
  463. .remove = realview_clcd_remove,
  464. };
  465. #ifdef CONFIG_LEDS
  466. #define VA_LEDS_BASE (__io_address(REALVIEW_SYS_BASE) + REALVIEW_SYS_LED_OFFSET)
  467. void realview_leds_event(led_event_t ledevt)
  468. {
  469. unsigned long flags;
  470. u32 val;
  471. local_irq_save(flags);
  472. val = readl(VA_LEDS_BASE);
  473. switch (ledevt) {
  474. case led_idle_start:
  475. val = val & ~REALVIEW_SYS_LED0;
  476. break;
  477. case led_idle_end:
  478. val = val | REALVIEW_SYS_LED0;
  479. break;
  480. case led_timer:
  481. val = val ^ REALVIEW_SYS_LED1;
  482. break;
  483. case led_halted:
  484. val = 0;
  485. break;
  486. default:
  487. break;
  488. }
  489. writel(val, VA_LEDS_BASE);
  490. local_irq_restore(flags);
  491. }
  492. #endif /* CONFIG_LEDS */
  493. /*
  494. * Where is the timer (VA)?
  495. */
  496. void __iomem *timer0_va_base;
  497. void __iomem *timer1_va_base;
  498. void __iomem *timer2_va_base;
  499. void __iomem *timer3_va_base;
  500. /*
  501. * How long is the timer interval?
  502. */
  503. #define TIMER_INTERVAL (TICKS_PER_uSEC * mSEC_10)
  504. #if TIMER_INTERVAL >= 0x100000
  505. #define TIMER_RELOAD (TIMER_INTERVAL >> 8)
  506. #define TIMER_DIVISOR (TIMER_CTRL_DIV256)
  507. #define TICKS2USECS(x) (256 * (x) / TICKS_PER_uSEC)
  508. #elif TIMER_INTERVAL >= 0x10000
  509. #define TIMER_RELOAD (TIMER_INTERVAL >> 4) /* Divide by 16 */
  510. #define TIMER_DIVISOR (TIMER_CTRL_DIV16)
  511. #define TICKS2USECS(x) (16 * (x) / TICKS_PER_uSEC)
  512. #else
  513. #define TIMER_RELOAD (TIMER_INTERVAL)
  514. #define TIMER_DIVISOR (TIMER_CTRL_DIV1)
  515. #define TICKS2USECS(x) ((x) / TICKS_PER_uSEC)
  516. #endif
  517. static void timer_set_mode(enum clock_event_mode mode,
  518. struct clock_event_device *clk)
  519. {
  520. unsigned long ctrl;
  521. switch(mode) {
  522. case CLOCK_EVT_MODE_PERIODIC:
  523. writel(TIMER_RELOAD, timer0_va_base + TIMER_LOAD);
  524. ctrl = TIMER_CTRL_PERIODIC;
  525. ctrl |= TIMER_CTRL_32BIT | TIMER_CTRL_IE | TIMER_CTRL_ENABLE;
  526. break;
  527. case CLOCK_EVT_MODE_ONESHOT:
  528. /* period set, and timer enabled in 'next_event' hook */
  529. ctrl = TIMER_CTRL_ONESHOT;
  530. ctrl |= TIMER_CTRL_32BIT | TIMER_CTRL_IE;
  531. break;
  532. case CLOCK_EVT_MODE_UNUSED:
  533. case CLOCK_EVT_MODE_SHUTDOWN:
  534. default:
  535. ctrl = 0;
  536. }
  537. writel(ctrl, timer0_va_base + TIMER_CTRL);
  538. }
  539. static int timer_set_next_event(unsigned long evt,
  540. struct clock_event_device *unused)
  541. {
  542. unsigned long ctrl = readl(timer0_va_base + TIMER_CTRL);
  543. writel(evt, timer0_va_base + TIMER_LOAD);
  544. writel(ctrl | TIMER_CTRL_ENABLE, timer0_va_base + TIMER_CTRL);
  545. return 0;
  546. }
  547. static struct clock_event_device timer0_clockevent = {
  548. .name = "timer0",
  549. .shift = 32,
  550. .features = CLOCK_EVT_FEAT_PERIODIC | CLOCK_EVT_FEAT_ONESHOT,
  551. .set_mode = timer_set_mode,
  552. .set_next_event = timer_set_next_event,
  553. .rating = 300,
  554. .cpumask = cpu_all_mask,
  555. };
  556. static void __init realview_clockevents_init(unsigned int timer_irq)
  557. {
  558. timer0_clockevent.irq = timer_irq;
  559. timer0_clockevent.mult =
  560. div_sc(1000000, NSEC_PER_SEC, timer0_clockevent.shift);
  561. timer0_clockevent.max_delta_ns =
  562. clockevent_delta2ns(0xffffffff, &timer0_clockevent);
  563. timer0_clockevent.min_delta_ns =
  564. clockevent_delta2ns(0xf, &timer0_clockevent);
  565. clockevents_register_device(&timer0_clockevent);
  566. }
  567. /*
  568. * IRQ handler for the timer
  569. */
  570. static irqreturn_t realview_timer_interrupt(int irq, void *dev_id)
  571. {
  572. struct clock_event_device *evt = &timer0_clockevent;
  573. /* clear the interrupt */
  574. writel(1, timer0_va_base + TIMER_INTCLR);
  575. evt->event_handler(evt);
  576. return IRQ_HANDLED;
  577. }
  578. static struct irqaction realview_timer_irq = {
  579. .name = "RealView Timer Tick",
  580. .flags = IRQF_DISABLED | IRQF_TIMER | IRQF_IRQPOLL,
  581. .handler = realview_timer_interrupt,
  582. };
  583. static cycle_t realview_get_cycles(void)
  584. {
  585. return ~readl(timer3_va_base + TIMER_VALUE);
  586. }
  587. static struct clocksource clocksource_realview = {
  588. .name = "timer3",
  589. .rating = 200,
  590. .read = realview_get_cycles,
  591. .mask = CLOCKSOURCE_MASK(32),
  592. .shift = 20,
  593. .flags = CLOCK_SOURCE_IS_CONTINUOUS,
  594. };
  595. static void __init realview_clocksource_init(void)
  596. {
  597. /* setup timer 0 as free-running clocksource */
  598. writel(0, timer3_va_base + TIMER_CTRL);
  599. writel(0xffffffff, timer3_va_base + TIMER_LOAD);
  600. writel(0xffffffff, timer3_va_base + TIMER_VALUE);
  601. writel(TIMER_CTRL_32BIT | TIMER_CTRL_ENABLE | TIMER_CTRL_PERIODIC,
  602. timer3_va_base + TIMER_CTRL);
  603. clocksource_realview.mult =
  604. clocksource_khz2mult(1000, clocksource_realview.shift);
  605. clocksource_register(&clocksource_realview);
  606. }
  607. /*
  608. * Set up the clock source and clock events devices
  609. */
  610. void __init realview_timer_init(unsigned int timer_irq)
  611. {
  612. u32 val;
  613. #ifdef CONFIG_GENERIC_CLOCKEVENTS_BROADCAST
  614. /*
  615. * The dummy clock device has to be registered before the main device
  616. * so that the latter will broadcast the clock events
  617. */
  618. local_timer_setup();
  619. #endif
  620. /*
  621. * set clock frequency:
  622. * REALVIEW_REFCLK is 32KHz
  623. * REALVIEW_TIMCLK is 1MHz
  624. */
  625. val = readl(__io_address(REALVIEW_SCTL_BASE));
  626. writel((REALVIEW_TIMCLK << REALVIEW_TIMER1_EnSel) |
  627. (REALVIEW_TIMCLK << REALVIEW_TIMER2_EnSel) |
  628. (REALVIEW_TIMCLK << REALVIEW_TIMER3_EnSel) |
  629. (REALVIEW_TIMCLK << REALVIEW_TIMER4_EnSel) | val,
  630. __io_address(REALVIEW_SCTL_BASE));
  631. /*
  632. * Initialise to a known state (all timers off)
  633. */
  634. writel(0, timer0_va_base + TIMER_CTRL);
  635. writel(0, timer1_va_base + TIMER_CTRL);
  636. writel(0, timer2_va_base + TIMER_CTRL);
  637. writel(0, timer3_va_base + TIMER_CTRL);
  638. /*
  639. * Make irqs happen for the system timer
  640. */
  641. setup_irq(timer_irq, &realview_timer_irq);
  642. realview_clocksource_init();
  643. realview_clockevents_init(timer_irq);
  644. }