core.c 18 KB

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