core.c 18 KB

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