core.c 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605
  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/config.h>
  22. #include <linux/init.h>
  23. #include <linux/platform_device.h>
  24. #include <linux/dma-mapping.h>
  25. #include <linux/sysdev.h>
  26. #include <linux/interrupt.h>
  27. #include <asm/system.h>
  28. #include <asm/hardware.h>
  29. #include <asm/io.h>
  30. #include <asm/irq.h>
  31. #include <asm/leds.h>
  32. #include <asm/mach-types.h>
  33. #include <asm/hardware/amba.h>
  34. #include <asm/hardware/amba_clcd.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/time.h>
  41. #include <asm/mach/map.h>
  42. #include <asm/mach/mmc.h>
  43. #include <asm/hardware/gic.h>
  44. #include "core.h"
  45. #include "clock.h"
  46. #define REALVIEW_REFCOUNTER (__io_address(REALVIEW_SYS_BASE) + REALVIEW_SYS_24MHz_OFFSET)
  47. /*
  48. * This is the RealView sched_clock implementation. This has
  49. * a resolution of 41.7ns, and a maximum value of about 179s.
  50. */
  51. unsigned long long sched_clock(void)
  52. {
  53. unsigned long long v;
  54. v = (unsigned long long)readl(REALVIEW_REFCOUNTER) * 125;
  55. do_div(v, 3);
  56. return v;
  57. }
  58. #define REALVIEW_FLASHCTRL (__io_address(REALVIEW_SYS_BASE) + REALVIEW_SYS_FLASH_OFFSET)
  59. static int realview_flash_init(void)
  60. {
  61. u32 val;
  62. val = __raw_readl(REALVIEW_FLASHCTRL);
  63. val &= ~REALVIEW_FLASHPROG_FLVPPEN;
  64. __raw_writel(val, REALVIEW_FLASHCTRL);
  65. return 0;
  66. }
  67. static void realview_flash_exit(void)
  68. {
  69. u32 val;
  70. val = __raw_readl(REALVIEW_FLASHCTRL);
  71. val &= ~REALVIEW_FLASHPROG_FLVPPEN;
  72. __raw_writel(val, REALVIEW_FLASHCTRL);
  73. }
  74. static void realview_flash_set_vpp(int on)
  75. {
  76. u32 val;
  77. val = __raw_readl(REALVIEW_FLASHCTRL);
  78. if (on)
  79. val |= REALVIEW_FLASHPROG_FLVPPEN;
  80. else
  81. val &= ~REALVIEW_FLASHPROG_FLVPPEN;
  82. __raw_writel(val, REALVIEW_FLASHCTRL);
  83. }
  84. static struct flash_platform_data realview_flash_data = {
  85. .map_name = "cfi_probe",
  86. .width = 4,
  87. .init = realview_flash_init,
  88. .exit = realview_flash_exit,
  89. .set_vpp = realview_flash_set_vpp,
  90. };
  91. static struct resource realview_flash_resource = {
  92. .start = REALVIEW_FLASH_BASE,
  93. .end = REALVIEW_FLASH_BASE + REALVIEW_FLASH_SIZE,
  94. .flags = IORESOURCE_MEM,
  95. };
  96. struct platform_device realview_flash_device = {
  97. .name = "armflash",
  98. .id = 0,
  99. .dev = {
  100. .platform_data = &realview_flash_data,
  101. },
  102. .num_resources = 1,
  103. .resource = &realview_flash_resource,
  104. };
  105. static struct resource realview_smc91x_resources[] = {
  106. [0] = {
  107. .start = REALVIEW_ETH_BASE,
  108. .end = REALVIEW_ETH_BASE + SZ_64K - 1,
  109. .flags = IORESOURCE_MEM,
  110. },
  111. [1] = {
  112. .start = IRQ_ETH,
  113. .end = IRQ_ETH,
  114. .flags = IORESOURCE_IRQ,
  115. },
  116. };
  117. struct platform_device realview_smc91x_device = {
  118. .name = "smc91x",
  119. .id = 0,
  120. .num_resources = ARRAY_SIZE(realview_smc91x_resources),
  121. .resource = realview_smc91x_resources,
  122. };
  123. #define REALVIEW_SYSMCI (__io_address(REALVIEW_SYS_BASE) + REALVIEW_SYS_MCI_OFFSET)
  124. static unsigned int realview_mmc_status(struct device *dev)
  125. {
  126. struct amba_device *adev = container_of(dev, struct amba_device, dev);
  127. u32 mask;
  128. if (adev->res.start == REALVIEW_MMCI0_BASE)
  129. mask = 1;
  130. else
  131. mask = 2;
  132. return readl(REALVIEW_SYSMCI) & mask;
  133. }
  134. struct mmc_platform_data realview_mmc0_plat_data = {
  135. .ocr_mask = MMC_VDD_32_33|MMC_VDD_33_34,
  136. .status = realview_mmc_status,
  137. };
  138. struct mmc_platform_data realview_mmc1_plat_data = {
  139. .ocr_mask = MMC_VDD_32_33|MMC_VDD_33_34,
  140. .status = realview_mmc_status,
  141. };
  142. /*
  143. * Clock handling
  144. */
  145. static const struct icst307_params realview_oscvco_params = {
  146. .ref = 24000,
  147. .vco_max = 200000,
  148. .vd_min = 4 + 8,
  149. .vd_max = 511 + 8,
  150. .rd_min = 1 + 2,
  151. .rd_max = 127 + 2,
  152. };
  153. static void realview_oscvco_set(struct clk *clk, struct icst307_vco vco)
  154. {
  155. void __iomem *sys_lock = __io_address(REALVIEW_SYS_BASE) + REALVIEW_SYS_LOCK_OFFSET;
  156. void __iomem *sys_osc = __io_address(REALVIEW_SYS_BASE) + REALVIEW_SYS_OSC1_OFFSET;
  157. u32 val;
  158. val = readl(sys_osc) & ~0x7ffff;
  159. val |= vco.v | (vco.r << 9) | (vco.s << 16);
  160. writel(0xa05f, sys_lock);
  161. writel(val, sys_osc);
  162. writel(0, sys_lock);
  163. }
  164. struct clk realview_clcd_clk = {
  165. .name = "CLCDCLK",
  166. .params = &realview_oscvco_params,
  167. .setvco = realview_oscvco_set,
  168. };
  169. /*
  170. * CLCD support.
  171. */
  172. #define SYS_CLCD_MODE_MASK (3 << 0)
  173. #define SYS_CLCD_MODE_888 (0 << 0)
  174. #define SYS_CLCD_MODE_5551 (1 << 0)
  175. #define SYS_CLCD_MODE_565_RLSB (2 << 0)
  176. #define SYS_CLCD_MODE_565_BLSB (3 << 0)
  177. #define SYS_CLCD_NLCDIOON (1 << 2)
  178. #define SYS_CLCD_VDDPOSSWITCH (1 << 3)
  179. #define SYS_CLCD_PWR3V5SWITCH (1 << 4)
  180. #define SYS_CLCD_ID_MASK (0x1f << 8)
  181. #define SYS_CLCD_ID_SANYO_3_8 (0x00 << 8)
  182. #define SYS_CLCD_ID_UNKNOWN_8_4 (0x01 << 8)
  183. #define SYS_CLCD_ID_EPSON_2_2 (0x02 << 8)
  184. #define SYS_CLCD_ID_SANYO_2_5 (0x07 << 8)
  185. #define SYS_CLCD_ID_VGA (0x1f << 8)
  186. static struct clcd_panel vga = {
  187. .mode = {
  188. .name = "VGA",
  189. .refresh = 60,
  190. .xres = 640,
  191. .yres = 480,
  192. .pixclock = 39721,
  193. .left_margin = 40,
  194. .right_margin = 24,
  195. .upper_margin = 32,
  196. .lower_margin = 11,
  197. .hsync_len = 96,
  198. .vsync_len = 2,
  199. .sync = 0,
  200. .vmode = FB_VMODE_NONINTERLACED,
  201. },
  202. .width = -1,
  203. .height = -1,
  204. .tim2 = TIM2_BCD | TIM2_IPC,
  205. .cntl = CNTL_LCDTFT | CNTL_LCDVCOMP(1),
  206. .bpp = 16,
  207. };
  208. static struct clcd_panel sanyo_3_8_in = {
  209. .mode = {
  210. .name = "Sanyo QVGA",
  211. .refresh = 116,
  212. .xres = 320,
  213. .yres = 240,
  214. .pixclock = 100000,
  215. .left_margin = 6,
  216. .right_margin = 6,
  217. .upper_margin = 5,
  218. .lower_margin = 5,
  219. .hsync_len = 6,
  220. .vsync_len = 6,
  221. .sync = 0,
  222. .vmode = FB_VMODE_NONINTERLACED,
  223. },
  224. .width = -1,
  225. .height = -1,
  226. .tim2 = TIM2_BCD,
  227. .cntl = CNTL_LCDTFT | CNTL_LCDVCOMP(1),
  228. .bpp = 16,
  229. };
  230. static struct clcd_panel sanyo_2_5_in = {
  231. .mode = {
  232. .name = "Sanyo QVGA Portrait",
  233. .refresh = 116,
  234. .xres = 240,
  235. .yres = 320,
  236. .pixclock = 100000,
  237. .left_margin = 20,
  238. .right_margin = 10,
  239. .upper_margin = 2,
  240. .lower_margin = 2,
  241. .hsync_len = 10,
  242. .vsync_len = 2,
  243. .sync = FB_SYNC_HOR_HIGH_ACT | FB_SYNC_VERT_HIGH_ACT,
  244. .vmode = FB_VMODE_NONINTERLACED,
  245. },
  246. .width = -1,
  247. .height = -1,
  248. .tim2 = TIM2_IVS | TIM2_IHS | TIM2_IPC,
  249. .cntl = CNTL_LCDTFT | CNTL_LCDVCOMP(1),
  250. .bpp = 16,
  251. };
  252. static struct clcd_panel epson_2_2_in = {
  253. .mode = {
  254. .name = "Epson QCIF",
  255. .refresh = 390,
  256. .xres = 176,
  257. .yres = 220,
  258. .pixclock = 62500,
  259. .left_margin = 3,
  260. .right_margin = 2,
  261. .upper_margin = 1,
  262. .lower_margin = 0,
  263. .hsync_len = 3,
  264. .vsync_len = 2,
  265. .sync = 0,
  266. .vmode = FB_VMODE_NONINTERLACED,
  267. },
  268. .width = -1,
  269. .height = -1,
  270. .tim2 = TIM2_BCD | TIM2_IPC,
  271. .cntl = CNTL_LCDTFT | CNTL_LCDVCOMP(1),
  272. .bpp = 16,
  273. };
  274. /*
  275. * Detect which LCD panel is connected, and return the appropriate
  276. * clcd_panel structure. Note: we do not have any information on
  277. * the required timings for the 8.4in panel, so we presently assume
  278. * VGA timings.
  279. */
  280. static struct clcd_panel *realview_clcd_panel(void)
  281. {
  282. void __iomem *sys_clcd = __io_address(REALVIEW_SYS_BASE) + REALVIEW_SYS_CLCD_OFFSET;
  283. struct clcd_panel *panel = &vga;
  284. u32 val;
  285. val = readl(sys_clcd) & SYS_CLCD_ID_MASK;
  286. if (val == SYS_CLCD_ID_SANYO_3_8)
  287. panel = &sanyo_3_8_in;
  288. else if (val == SYS_CLCD_ID_SANYO_2_5)
  289. panel = &sanyo_2_5_in;
  290. else if (val == SYS_CLCD_ID_EPSON_2_2)
  291. panel = &epson_2_2_in;
  292. else if (val == SYS_CLCD_ID_VGA)
  293. panel = &vga;
  294. else {
  295. printk(KERN_ERR "CLCD: unknown LCD panel ID 0x%08x, using VGA\n",
  296. val);
  297. panel = &vga;
  298. }
  299. return panel;
  300. }
  301. /*
  302. * Disable all display connectors on the interface module.
  303. */
  304. static void realview_clcd_disable(struct clcd_fb *fb)
  305. {
  306. void __iomem *sys_clcd = __io_address(REALVIEW_SYS_BASE) + REALVIEW_SYS_CLCD_OFFSET;
  307. u32 val;
  308. val = readl(sys_clcd);
  309. val &= ~SYS_CLCD_NLCDIOON | SYS_CLCD_PWR3V5SWITCH;
  310. writel(val, sys_clcd);
  311. }
  312. /*
  313. * Enable the relevant connector on the interface module.
  314. */
  315. static void realview_clcd_enable(struct clcd_fb *fb)
  316. {
  317. void __iomem *sys_clcd = __io_address(REALVIEW_SYS_BASE) + REALVIEW_SYS_CLCD_OFFSET;
  318. u32 val;
  319. val = readl(sys_clcd);
  320. val &= ~SYS_CLCD_MODE_MASK;
  321. switch (fb->fb.var.green.length) {
  322. case 5:
  323. val |= SYS_CLCD_MODE_5551;
  324. break;
  325. case 6:
  326. val |= SYS_CLCD_MODE_565_RLSB;
  327. break;
  328. case 8:
  329. val |= SYS_CLCD_MODE_888;
  330. break;
  331. }
  332. /*
  333. * Set the MUX
  334. */
  335. writel(val, sys_clcd);
  336. /*
  337. * And now enable the PSUs
  338. */
  339. val |= SYS_CLCD_NLCDIOON | SYS_CLCD_PWR3V5SWITCH;
  340. writel(val, sys_clcd);
  341. }
  342. static unsigned long framesize = SZ_1M;
  343. static int realview_clcd_setup(struct clcd_fb *fb)
  344. {
  345. dma_addr_t dma;
  346. fb->panel = realview_clcd_panel();
  347. fb->fb.screen_base = dma_alloc_writecombine(&fb->dev->dev, framesize,
  348. &dma, GFP_KERNEL);
  349. if (!fb->fb.screen_base) {
  350. printk(KERN_ERR "CLCD: unable to map framebuffer\n");
  351. return -ENOMEM;
  352. }
  353. fb->fb.fix.smem_start = dma;
  354. fb->fb.fix.smem_len = framesize;
  355. return 0;
  356. }
  357. static int realview_clcd_mmap(struct clcd_fb *fb, struct vm_area_struct *vma)
  358. {
  359. return dma_mmap_writecombine(&fb->dev->dev, vma,
  360. fb->fb.screen_base,
  361. fb->fb.fix.smem_start,
  362. fb->fb.fix.smem_len);
  363. }
  364. static void realview_clcd_remove(struct clcd_fb *fb)
  365. {
  366. dma_free_writecombine(&fb->dev->dev, fb->fb.fix.smem_len,
  367. fb->fb.screen_base, fb->fb.fix.smem_start);
  368. }
  369. struct clcd_board clcd_plat_data = {
  370. .name = "RealView",
  371. .check = clcdfb_check,
  372. .decode = clcdfb_decode,
  373. .disable = realview_clcd_disable,
  374. .enable = realview_clcd_enable,
  375. .setup = realview_clcd_setup,
  376. .mmap = realview_clcd_mmap,
  377. .remove = realview_clcd_remove,
  378. };
  379. #ifdef CONFIG_LEDS
  380. #define VA_LEDS_BASE (__io_address(REALVIEW_SYS_BASE) + REALVIEW_SYS_LED_OFFSET)
  381. void realview_leds_event(led_event_t ledevt)
  382. {
  383. unsigned long flags;
  384. u32 val;
  385. local_irq_save(flags);
  386. val = readl(VA_LEDS_BASE);
  387. switch (ledevt) {
  388. case led_idle_start:
  389. val = val & ~REALVIEW_SYS_LED0;
  390. break;
  391. case led_idle_end:
  392. val = val | REALVIEW_SYS_LED0;
  393. break;
  394. case led_timer:
  395. val = val ^ REALVIEW_SYS_LED1;
  396. break;
  397. case led_halted:
  398. val = 0;
  399. break;
  400. default:
  401. break;
  402. }
  403. writel(val, VA_LEDS_BASE);
  404. local_irq_restore(flags);
  405. }
  406. #endif /* CONFIG_LEDS */
  407. /*
  408. * Where is the timer (VA)?
  409. */
  410. #define TIMER0_VA_BASE __io_address(REALVIEW_TIMER0_1_BASE)
  411. #define TIMER1_VA_BASE (__io_address(REALVIEW_TIMER0_1_BASE) + 0x20)
  412. #define TIMER2_VA_BASE __io_address(REALVIEW_TIMER2_3_BASE)
  413. #define TIMER3_VA_BASE (__io_address(REALVIEW_TIMER2_3_BASE) + 0x20)
  414. /*
  415. * How long is the timer interval?
  416. */
  417. #define TIMER_INTERVAL (TICKS_PER_uSEC * mSEC_10)
  418. #if TIMER_INTERVAL >= 0x100000
  419. #define TIMER_RELOAD (TIMER_INTERVAL >> 8)
  420. #define TIMER_DIVISOR (TIMER_CTRL_DIV256)
  421. #define TICKS2USECS(x) (256 * (x) / TICKS_PER_uSEC)
  422. #elif TIMER_INTERVAL >= 0x10000
  423. #define TIMER_RELOAD (TIMER_INTERVAL >> 4) /* Divide by 16 */
  424. #define TIMER_DIVISOR (TIMER_CTRL_DIV16)
  425. #define TICKS2USECS(x) (16 * (x) / TICKS_PER_uSEC)
  426. #else
  427. #define TIMER_RELOAD (TIMER_INTERVAL)
  428. #define TIMER_DIVISOR (TIMER_CTRL_DIV1)
  429. #define TICKS2USECS(x) ((x) / TICKS_PER_uSEC)
  430. #endif
  431. /*
  432. * Returns number of ms since last clock interrupt. Note that interrupts
  433. * will have been disabled by do_gettimeoffset()
  434. */
  435. static unsigned long realview_gettimeoffset(void)
  436. {
  437. unsigned long ticks1, ticks2, status;
  438. /*
  439. * Get the current number of ticks. Note that there is a race
  440. * condition between us reading the timer and checking for
  441. * an interrupt. We get around this by ensuring that the
  442. * counter has not reloaded between our two reads.
  443. */
  444. ticks2 = readl(TIMER0_VA_BASE + TIMER_VALUE) & 0xffff;
  445. do {
  446. ticks1 = ticks2;
  447. status = __raw_readl(__io_address(REALVIEW_GIC_DIST_BASE + GIC_DIST_PENDING_SET)
  448. + ((IRQ_TIMERINT0_1 >> 5) << 2));
  449. ticks2 = readl(TIMER0_VA_BASE + TIMER_VALUE) & 0xffff;
  450. } while (ticks2 > ticks1);
  451. /*
  452. * Number of ticks since last interrupt.
  453. */
  454. ticks1 = TIMER_RELOAD - ticks2;
  455. /*
  456. * Interrupt pending? If so, we've reloaded once already.
  457. *
  458. * FIXME: Need to check this is effectively timer 0 that expires
  459. */
  460. if (status & IRQMASK_TIMERINT0_1)
  461. ticks1 += TIMER_RELOAD;
  462. /*
  463. * Convert the ticks to usecs
  464. */
  465. return TICKS2USECS(ticks1);
  466. }
  467. /*
  468. * IRQ handler for the timer
  469. */
  470. static irqreturn_t realview_timer_interrupt(int irq, void *dev_id, struct pt_regs *regs)
  471. {
  472. write_seqlock(&xtime_lock);
  473. // ...clear the interrupt
  474. writel(1, TIMER0_VA_BASE + TIMER_INTCLR);
  475. timer_tick(regs);
  476. write_sequnlock(&xtime_lock);
  477. return IRQ_HANDLED;
  478. }
  479. static struct irqaction realview_timer_irq = {
  480. .name = "RealView Timer Tick",
  481. .flags = SA_INTERRUPT | SA_TIMER,
  482. .handler = realview_timer_interrupt,
  483. };
  484. /*
  485. * Set up timer interrupt, and return the current time in seconds.
  486. */
  487. static void __init realview_timer_init(void)
  488. {
  489. u32 val;
  490. /*
  491. * set clock frequency:
  492. * REALVIEW_REFCLK is 32KHz
  493. * REALVIEW_TIMCLK is 1MHz
  494. */
  495. val = readl(__io_address(REALVIEW_SCTL_BASE));
  496. writel((REALVIEW_TIMCLK << REALVIEW_TIMER1_EnSel) |
  497. (REALVIEW_TIMCLK << REALVIEW_TIMER2_EnSel) |
  498. (REALVIEW_TIMCLK << REALVIEW_TIMER3_EnSel) |
  499. (REALVIEW_TIMCLK << REALVIEW_TIMER4_EnSel) | val,
  500. __io_address(REALVIEW_SCTL_BASE));
  501. /*
  502. * Initialise to a known state (all timers off)
  503. */
  504. writel(0, TIMER0_VA_BASE + TIMER_CTRL);
  505. writel(0, TIMER1_VA_BASE + TIMER_CTRL);
  506. writel(0, TIMER2_VA_BASE + TIMER_CTRL);
  507. writel(0, TIMER3_VA_BASE + TIMER_CTRL);
  508. writel(TIMER_RELOAD, TIMER0_VA_BASE + TIMER_LOAD);
  509. writel(TIMER_RELOAD, TIMER0_VA_BASE + TIMER_VALUE);
  510. writel(TIMER_DIVISOR | TIMER_CTRL_ENABLE | TIMER_CTRL_PERIODIC |
  511. TIMER_CTRL_IE, TIMER0_VA_BASE + TIMER_CTRL);
  512. /*
  513. * Make irqs happen for the system timer
  514. */
  515. setup_irq(IRQ_TIMERINT0_1, &realview_timer_irq);
  516. }
  517. struct sys_timer realview_timer = {
  518. .init = realview_timer_init,
  519. .offset = realview_gettimeoffset,
  520. };