core.c 18 KB

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