core.c 18 KB

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