core.c 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621
  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 <asm/system.h>
  32. #include <mach/hardware.h>
  33. #include <asm/irq.h>
  34. #include <asm/leds.h>
  35. #include <asm/hardware/arm_timer.h>
  36. #include <asm/hardware/icst307.h>
  37. #include <asm/mach/arch.h>
  38. #include <asm/mach/flash.h>
  39. #include <asm/mach/irq.h>
  40. #include <asm/mach/map.h>
  41. #include <asm/mach/mmc.h>
  42. #include <asm/hardware/gic.h>
  43. #include "core.h"
  44. #include "clock.h"
  45. #define REALVIEW_REFCOUNTER (__io_address(REALVIEW_SYS_BASE) + REALVIEW_SYS_24MHz_OFFSET)
  46. /* used by entry-macro.S */
  47. void __iomem *gic_cpu_base_addr;
  48. /*
  49. * This is the RealView sched_clock implementation. This has
  50. * a resolution of 41.7ns, and a maximum value of about 179s.
  51. */
  52. unsigned long long sched_clock(void)
  53. {
  54. unsigned long long v;
  55. v = (unsigned long long)readl(REALVIEW_REFCOUNTER) * 125;
  56. do_div(v, 3);
  57. return v;
  58. }
  59. #define REALVIEW_FLASHCTRL (__io_address(REALVIEW_SYS_BASE) + REALVIEW_SYS_FLASH_OFFSET)
  60. static int realview_flash_init(void)
  61. {
  62. u32 val;
  63. val = __raw_readl(REALVIEW_FLASHCTRL);
  64. val &= ~REALVIEW_FLASHPROG_FLVPPEN;
  65. __raw_writel(val, REALVIEW_FLASHCTRL);
  66. return 0;
  67. }
  68. static void realview_flash_exit(void)
  69. {
  70. u32 val;
  71. val = __raw_readl(REALVIEW_FLASHCTRL);
  72. val &= ~REALVIEW_FLASHPROG_FLVPPEN;
  73. __raw_writel(val, REALVIEW_FLASHCTRL);
  74. }
  75. static void realview_flash_set_vpp(int on)
  76. {
  77. u32 val;
  78. val = __raw_readl(REALVIEW_FLASHCTRL);
  79. if (on)
  80. val |= REALVIEW_FLASHPROG_FLVPPEN;
  81. else
  82. val &= ~REALVIEW_FLASHPROG_FLVPPEN;
  83. __raw_writel(val, REALVIEW_FLASHCTRL);
  84. }
  85. static struct flash_platform_data realview_flash_data = {
  86. .map_name = "cfi_probe",
  87. .width = 4,
  88. .init = realview_flash_init,
  89. .exit = realview_flash_exit,
  90. .set_vpp = realview_flash_set_vpp,
  91. };
  92. struct platform_device realview_flash_device = {
  93. .name = "armflash",
  94. .id = 0,
  95. .dev = {
  96. .platform_data = &realview_flash_data,
  97. },
  98. };
  99. int realview_flash_register(struct resource *res, u32 num)
  100. {
  101. realview_flash_device.resource = res;
  102. realview_flash_device.num_resources = num;
  103. return platform_device_register(&realview_flash_device);
  104. }
  105. static struct resource realview_i2c_resource = {
  106. .start = REALVIEW_I2C_BASE,
  107. .end = REALVIEW_I2C_BASE + SZ_4K - 1,
  108. .flags = IORESOURCE_MEM,
  109. };
  110. struct platform_device realview_i2c_device = {
  111. .name = "versatile-i2c",
  112. .id = -1,
  113. .num_resources = 1,
  114. .resource = &realview_i2c_resource,
  115. };
  116. #define REALVIEW_SYSMCI (__io_address(REALVIEW_SYS_BASE) + REALVIEW_SYS_MCI_OFFSET)
  117. static unsigned int realview_mmc_status(struct device *dev)
  118. {
  119. struct amba_device *adev = container_of(dev, struct amba_device, dev);
  120. u32 mask;
  121. if (adev->res.start == REALVIEW_MMCI0_BASE)
  122. mask = 1;
  123. else
  124. mask = 2;
  125. return readl(REALVIEW_SYSMCI) & mask;
  126. }
  127. struct mmc_platform_data realview_mmc0_plat_data = {
  128. .ocr_mask = MMC_VDD_32_33|MMC_VDD_33_34,
  129. .status = realview_mmc_status,
  130. };
  131. struct mmc_platform_data realview_mmc1_plat_data = {
  132. .ocr_mask = MMC_VDD_32_33|MMC_VDD_33_34,
  133. .status = realview_mmc_status,
  134. };
  135. /*
  136. * Clock handling
  137. */
  138. static const struct icst307_params realview_oscvco_params = {
  139. .ref = 24000,
  140. .vco_max = 200000,
  141. .vd_min = 4 + 8,
  142. .vd_max = 511 + 8,
  143. .rd_min = 1 + 2,
  144. .rd_max = 127 + 2,
  145. };
  146. static void realview_oscvco_set(struct clk *clk, struct icst307_vco vco)
  147. {
  148. void __iomem *sys_lock = __io_address(REALVIEW_SYS_BASE) + REALVIEW_SYS_LOCK_OFFSET;
  149. void __iomem *sys_osc = __io_address(REALVIEW_SYS_BASE) + REALVIEW_SYS_OSC4_OFFSET;
  150. u32 val;
  151. val = readl(sys_osc) & ~0x7ffff;
  152. val |= vco.v | (vco.r << 9) | (vco.s << 16);
  153. writel(0xa05f, sys_lock);
  154. writel(val, sys_osc);
  155. writel(0, sys_lock);
  156. }
  157. struct clk realview_clcd_clk = {
  158. .name = "CLCDCLK",
  159. .params = &realview_oscvco_params,
  160. .setvco = realview_oscvco_set,
  161. };
  162. /*
  163. * CLCD support.
  164. */
  165. #define SYS_CLCD_NLCDIOON (1 << 2)
  166. #define SYS_CLCD_VDDPOSSWITCH (1 << 3)
  167. #define SYS_CLCD_PWR3V5SWITCH (1 << 4)
  168. #define SYS_CLCD_ID_MASK (0x1f << 8)
  169. #define SYS_CLCD_ID_SANYO_3_8 (0x00 << 8)
  170. #define SYS_CLCD_ID_UNKNOWN_8_4 (0x01 << 8)
  171. #define SYS_CLCD_ID_EPSON_2_2 (0x02 << 8)
  172. #define SYS_CLCD_ID_SANYO_2_5 (0x07 << 8)
  173. #define SYS_CLCD_ID_VGA (0x1f << 8)
  174. static struct clcd_panel vga = {
  175. .mode = {
  176. .name = "VGA",
  177. .refresh = 60,
  178. .xres = 640,
  179. .yres = 480,
  180. .pixclock = 39721,
  181. .left_margin = 40,
  182. .right_margin = 24,
  183. .upper_margin = 32,
  184. .lower_margin = 11,
  185. .hsync_len = 96,
  186. .vsync_len = 2,
  187. .sync = 0,
  188. .vmode = FB_VMODE_NONINTERLACED,
  189. },
  190. .width = -1,
  191. .height = -1,
  192. .tim2 = TIM2_BCD | TIM2_IPC,
  193. .cntl = CNTL_LCDTFT | CNTL_LCDVCOMP(1),
  194. .bpp = 16,
  195. };
  196. static struct clcd_panel sanyo_3_8_in = {
  197. .mode = {
  198. .name = "Sanyo QVGA",
  199. .refresh = 116,
  200. .xres = 320,
  201. .yres = 240,
  202. .pixclock = 100000,
  203. .left_margin = 6,
  204. .right_margin = 6,
  205. .upper_margin = 5,
  206. .lower_margin = 5,
  207. .hsync_len = 6,
  208. .vsync_len = 6,
  209. .sync = 0,
  210. .vmode = FB_VMODE_NONINTERLACED,
  211. },
  212. .width = -1,
  213. .height = -1,
  214. .tim2 = TIM2_BCD,
  215. .cntl = CNTL_LCDTFT | CNTL_LCDVCOMP(1),
  216. .bpp = 16,
  217. };
  218. static struct clcd_panel sanyo_2_5_in = {
  219. .mode = {
  220. .name = "Sanyo QVGA Portrait",
  221. .refresh = 116,
  222. .xres = 240,
  223. .yres = 320,
  224. .pixclock = 100000,
  225. .left_margin = 20,
  226. .right_margin = 10,
  227. .upper_margin = 2,
  228. .lower_margin = 2,
  229. .hsync_len = 10,
  230. .vsync_len = 2,
  231. .sync = FB_SYNC_HOR_HIGH_ACT | FB_SYNC_VERT_HIGH_ACT,
  232. .vmode = FB_VMODE_NONINTERLACED,
  233. },
  234. .width = -1,
  235. .height = -1,
  236. .tim2 = TIM2_IVS | TIM2_IHS | TIM2_IPC,
  237. .cntl = CNTL_LCDTFT | CNTL_LCDVCOMP(1),
  238. .bpp = 16,
  239. };
  240. static struct clcd_panel epson_2_2_in = {
  241. .mode = {
  242. .name = "Epson QCIF",
  243. .refresh = 390,
  244. .xres = 176,
  245. .yres = 220,
  246. .pixclock = 62500,
  247. .left_margin = 3,
  248. .right_margin = 2,
  249. .upper_margin = 1,
  250. .lower_margin = 0,
  251. .hsync_len = 3,
  252. .vsync_len = 2,
  253. .sync = 0,
  254. .vmode = FB_VMODE_NONINTERLACED,
  255. },
  256. .width = -1,
  257. .height = -1,
  258. .tim2 = TIM2_BCD | TIM2_IPC,
  259. .cntl = CNTL_LCDTFT | CNTL_LCDVCOMP(1),
  260. .bpp = 16,
  261. };
  262. /*
  263. * Detect which LCD panel is connected, and return the appropriate
  264. * clcd_panel structure. Note: we do not have any information on
  265. * the required timings for the 8.4in panel, so we presently assume
  266. * VGA timings.
  267. */
  268. static struct clcd_panel *realview_clcd_panel(void)
  269. {
  270. void __iomem *sys_clcd = __io_address(REALVIEW_SYS_BASE) + REALVIEW_SYS_CLCD_OFFSET;
  271. struct clcd_panel *panel = &vga;
  272. u32 val;
  273. val = readl(sys_clcd) & SYS_CLCD_ID_MASK;
  274. if (val == SYS_CLCD_ID_SANYO_3_8)
  275. panel = &sanyo_3_8_in;
  276. else if (val == SYS_CLCD_ID_SANYO_2_5)
  277. panel = &sanyo_2_5_in;
  278. else if (val == SYS_CLCD_ID_EPSON_2_2)
  279. panel = &epson_2_2_in;
  280. else if (val == SYS_CLCD_ID_VGA)
  281. panel = &vga;
  282. else {
  283. printk(KERN_ERR "CLCD: unknown LCD panel ID 0x%08x, using VGA\n",
  284. val);
  285. panel = &vga;
  286. }
  287. return panel;
  288. }
  289. /*
  290. * Disable all display connectors on the interface module.
  291. */
  292. static void realview_clcd_disable(struct clcd_fb *fb)
  293. {
  294. void __iomem *sys_clcd = __io_address(REALVIEW_SYS_BASE) + REALVIEW_SYS_CLCD_OFFSET;
  295. u32 val;
  296. val = readl(sys_clcd);
  297. val &= ~SYS_CLCD_NLCDIOON | SYS_CLCD_PWR3V5SWITCH;
  298. writel(val, sys_clcd);
  299. }
  300. /*
  301. * Enable the relevant connector on the interface module.
  302. */
  303. static void realview_clcd_enable(struct clcd_fb *fb)
  304. {
  305. void __iomem *sys_clcd = __io_address(REALVIEW_SYS_BASE) + REALVIEW_SYS_CLCD_OFFSET;
  306. u32 val;
  307. /*
  308. * Enable the PSUs
  309. */
  310. val = readl(sys_clcd);
  311. val |= SYS_CLCD_NLCDIOON | SYS_CLCD_PWR3V5SWITCH;
  312. writel(val, sys_clcd);
  313. }
  314. static unsigned long framesize = SZ_1M;
  315. static int realview_clcd_setup(struct clcd_fb *fb)
  316. {
  317. dma_addr_t dma;
  318. fb->panel = realview_clcd_panel();
  319. fb->fb.screen_base = dma_alloc_writecombine(&fb->dev->dev, framesize,
  320. &dma, GFP_KERNEL);
  321. if (!fb->fb.screen_base) {
  322. printk(KERN_ERR "CLCD: unable to map framebuffer\n");
  323. return -ENOMEM;
  324. }
  325. fb->fb.fix.smem_start = dma;
  326. fb->fb.fix.smem_len = framesize;
  327. return 0;
  328. }
  329. static int realview_clcd_mmap(struct clcd_fb *fb, struct vm_area_struct *vma)
  330. {
  331. return dma_mmap_writecombine(&fb->dev->dev, vma,
  332. fb->fb.screen_base,
  333. fb->fb.fix.smem_start,
  334. fb->fb.fix.smem_len);
  335. }
  336. static void realview_clcd_remove(struct clcd_fb *fb)
  337. {
  338. dma_free_writecombine(&fb->dev->dev, fb->fb.fix.smem_len,
  339. fb->fb.screen_base, fb->fb.fix.smem_start);
  340. }
  341. struct clcd_board clcd_plat_data = {
  342. .name = "RealView",
  343. .check = clcdfb_check,
  344. .decode = clcdfb_decode,
  345. .disable = realview_clcd_disable,
  346. .enable = realview_clcd_enable,
  347. .setup = realview_clcd_setup,
  348. .mmap = realview_clcd_mmap,
  349. .remove = realview_clcd_remove,
  350. };
  351. #ifdef CONFIG_LEDS
  352. #define VA_LEDS_BASE (__io_address(REALVIEW_SYS_BASE) + REALVIEW_SYS_LED_OFFSET)
  353. void realview_leds_event(led_event_t ledevt)
  354. {
  355. unsigned long flags;
  356. u32 val;
  357. local_irq_save(flags);
  358. val = readl(VA_LEDS_BASE);
  359. switch (ledevt) {
  360. case led_idle_start:
  361. val = val & ~REALVIEW_SYS_LED0;
  362. break;
  363. case led_idle_end:
  364. val = val | REALVIEW_SYS_LED0;
  365. break;
  366. case led_timer:
  367. val = val ^ REALVIEW_SYS_LED1;
  368. break;
  369. case led_halted:
  370. val = 0;
  371. break;
  372. default:
  373. break;
  374. }
  375. writel(val, VA_LEDS_BASE);
  376. local_irq_restore(flags);
  377. }
  378. #endif /* CONFIG_LEDS */
  379. /*
  380. * Where is the timer (VA)?
  381. */
  382. void __iomem *timer0_va_base;
  383. void __iomem *timer1_va_base;
  384. void __iomem *timer2_va_base;
  385. void __iomem *timer3_va_base;
  386. /*
  387. * How long is the timer interval?
  388. */
  389. #define TIMER_INTERVAL (TICKS_PER_uSEC * mSEC_10)
  390. #if TIMER_INTERVAL >= 0x100000
  391. #define TIMER_RELOAD (TIMER_INTERVAL >> 8)
  392. #define TIMER_DIVISOR (TIMER_CTRL_DIV256)
  393. #define TICKS2USECS(x) (256 * (x) / TICKS_PER_uSEC)
  394. #elif TIMER_INTERVAL >= 0x10000
  395. #define TIMER_RELOAD (TIMER_INTERVAL >> 4) /* Divide by 16 */
  396. #define TIMER_DIVISOR (TIMER_CTRL_DIV16)
  397. #define TICKS2USECS(x) (16 * (x) / TICKS_PER_uSEC)
  398. #else
  399. #define TIMER_RELOAD (TIMER_INTERVAL)
  400. #define TIMER_DIVISOR (TIMER_CTRL_DIV1)
  401. #define TICKS2USECS(x) ((x) / TICKS_PER_uSEC)
  402. #endif
  403. static void timer_set_mode(enum clock_event_mode mode,
  404. struct clock_event_device *clk)
  405. {
  406. unsigned long ctrl;
  407. switch(mode) {
  408. case CLOCK_EVT_MODE_PERIODIC:
  409. writel(TIMER_RELOAD, timer0_va_base + TIMER_LOAD);
  410. ctrl = TIMER_CTRL_PERIODIC;
  411. ctrl |= TIMER_CTRL_32BIT | TIMER_CTRL_IE | TIMER_CTRL_ENABLE;
  412. break;
  413. case CLOCK_EVT_MODE_ONESHOT:
  414. /* period set, and timer enabled in 'next_event' hook */
  415. ctrl = TIMER_CTRL_ONESHOT;
  416. ctrl |= TIMER_CTRL_32BIT | TIMER_CTRL_IE;
  417. break;
  418. case CLOCK_EVT_MODE_UNUSED:
  419. case CLOCK_EVT_MODE_SHUTDOWN:
  420. default:
  421. ctrl = 0;
  422. }
  423. writel(ctrl, timer0_va_base + TIMER_CTRL);
  424. }
  425. static int timer_set_next_event(unsigned long evt,
  426. struct clock_event_device *unused)
  427. {
  428. unsigned long ctrl = readl(timer0_va_base + TIMER_CTRL);
  429. writel(evt, timer0_va_base + TIMER_LOAD);
  430. writel(ctrl | TIMER_CTRL_ENABLE, timer0_va_base + TIMER_CTRL);
  431. return 0;
  432. }
  433. static struct clock_event_device timer0_clockevent = {
  434. .name = "timer0",
  435. .shift = 32,
  436. .features = CLOCK_EVT_FEAT_PERIODIC | CLOCK_EVT_FEAT_ONESHOT,
  437. .set_mode = timer_set_mode,
  438. .set_next_event = timer_set_next_event,
  439. .rating = 300,
  440. .cpumask = CPU_MASK_ALL,
  441. };
  442. static void __init realview_clockevents_init(unsigned int timer_irq)
  443. {
  444. timer0_clockevent.irq = timer_irq;
  445. timer0_clockevent.mult =
  446. div_sc(1000000, NSEC_PER_SEC, timer0_clockevent.shift);
  447. timer0_clockevent.max_delta_ns =
  448. clockevent_delta2ns(0xffffffff, &timer0_clockevent);
  449. timer0_clockevent.min_delta_ns =
  450. clockevent_delta2ns(0xf, &timer0_clockevent);
  451. clockevents_register_device(&timer0_clockevent);
  452. }
  453. /*
  454. * IRQ handler for the timer
  455. */
  456. static irqreturn_t realview_timer_interrupt(int irq, void *dev_id)
  457. {
  458. struct clock_event_device *evt = &timer0_clockevent;
  459. /* clear the interrupt */
  460. writel(1, timer0_va_base + TIMER_INTCLR);
  461. evt->event_handler(evt);
  462. return IRQ_HANDLED;
  463. }
  464. static struct irqaction realview_timer_irq = {
  465. .name = "RealView Timer Tick",
  466. .flags = IRQF_DISABLED | IRQF_TIMER | IRQF_IRQPOLL,
  467. .handler = realview_timer_interrupt,
  468. };
  469. static cycle_t realview_get_cycles(void)
  470. {
  471. return ~readl(timer3_va_base + TIMER_VALUE);
  472. }
  473. static struct clocksource clocksource_realview = {
  474. .name = "timer3",
  475. .rating = 200,
  476. .read = realview_get_cycles,
  477. .mask = CLOCKSOURCE_MASK(32),
  478. .shift = 20,
  479. .flags = CLOCK_SOURCE_IS_CONTINUOUS,
  480. };
  481. static void __init realview_clocksource_init(void)
  482. {
  483. /* setup timer 0 as free-running clocksource */
  484. writel(0, timer3_va_base + TIMER_CTRL);
  485. writel(0xffffffff, timer3_va_base + TIMER_LOAD);
  486. writel(0xffffffff, timer3_va_base + TIMER_VALUE);
  487. writel(TIMER_CTRL_32BIT | TIMER_CTRL_ENABLE | TIMER_CTRL_PERIODIC,
  488. timer3_va_base + TIMER_CTRL);
  489. clocksource_realview.mult =
  490. clocksource_khz2mult(1000, clocksource_realview.shift);
  491. clocksource_register(&clocksource_realview);
  492. }
  493. /*
  494. * Set up the clock source and clock events devices
  495. */
  496. void __init realview_timer_init(unsigned int timer_irq)
  497. {
  498. u32 val;
  499. #ifdef CONFIG_GENERIC_CLOCKEVENTS_BROADCAST
  500. /*
  501. * The dummy clock device has to be registered before the main device
  502. * so that the latter will broadcast the clock events
  503. */
  504. local_timer_setup(smp_processor_id());
  505. #endif
  506. /*
  507. * set clock frequency:
  508. * REALVIEW_REFCLK is 32KHz
  509. * REALVIEW_TIMCLK is 1MHz
  510. */
  511. val = readl(__io_address(REALVIEW_SCTL_BASE));
  512. writel((REALVIEW_TIMCLK << REALVIEW_TIMER1_EnSel) |
  513. (REALVIEW_TIMCLK << REALVIEW_TIMER2_EnSel) |
  514. (REALVIEW_TIMCLK << REALVIEW_TIMER3_EnSel) |
  515. (REALVIEW_TIMCLK << REALVIEW_TIMER4_EnSel) | val,
  516. __io_address(REALVIEW_SCTL_BASE));
  517. /*
  518. * Initialise to a known state (all timers off)
  519. */
  520. writel(0, timer0_va_base + TIMER_CTRL);
  521. writel(0, timer1_va_base + TIMER_CTRL);
  522. writel(0, timer2_va_base + TIMER_CTRL);
  523. writel(0, timer3_va_base + TIMER_CTRL);
  524. /*
  525. * Make irqs happen for the system timer
  526. */
  527. setup_irq(timer_irq, &realview_timer_irq);
  528. realview_clocksource_init();
  529. realview_clockevents_init(timer_irq);
  530. }