core.c 18 KB

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