core.c 18 KB

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