core.c 18 KB

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