integrator_ap.c 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569
  1. /*
  2. * linux/arch/arm/mach-integrator/integrator_ap.c
  3. *
  4. * Copyright (C) 2000-2003 Deep Blue Solutions Ltd
  5. *
  6. * This program is free software; you can redistribute it and/or modify
  7. * it under the terms of the GNU General Public License as published by
  8. * the Free Software Foundation; either version 2 of the License, or
  9. * (at your option) any later version.
  10. *
  11. * This program is distributed in the hope that it will be useful,
  12. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  13. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  14. * GNU General Public License for more details.
  15. *
  16. * You should have received a copy of the GNU General Public License
  17. * along with this program; if not, write to the Free Software
  18. * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
  19. */
  20. #include <linux/types.h>
  21. #include <linux/kernel.h>
  22. #include <linux/init.h>
  23. #include <linux/list.h>
  24. #include <linux/platform_device.h>
  25. #include <linux/slab.h>
  26. #include <linux/string.h>
  27. #include <linux/syscore_ops.h>
  28. #include <linux/amba/bus.h>
  29. #include <linux/amba/kmi.h>
  30. #include <linux/clocksource.h>
  31. #include <linux/clockchips.h>
  32. #include <linux/interrupt.h>
  33. #include <linux/io.h>
  34. #include <linux/irqchip/versatile-fpga.h>
  35. #include <linux/mtd/physmap.h>
  36. #include <linux/clk.h>
  37. #include <linux/platform_data/clk-integrator.h>
  38. #include <linux/of_irq.h>
  39. #include <linux/of_address.h>
  40. #include <linux/of_platform.h>
  41. #include <linux/stat.h>
  42. #include <linux/sys_soc.h>
  43. #include <linux/termios.h>
  44. #include <linux/sched_clock.h>
  45. #include <mach/hardware.h>
  46. #include <mach/platform.h>
  47. #include <asm/hardware/arm_timer.h>
  48. #include <asm/setup.h>
  49. #include <asm/param.h> /* HZ */
  50. #include <asm/mach-types.h>
  51. #include <mach/lm.h>
  52. #include <asm/mach/arch.h>
  53. #include <asm/mach/irq.h>
  54. #include <asm/mach/map.h>
  55. #include <asm/mach/time.h>
  56. #include "cm.h"
  57. #include "common.h"
  58. #include "pci_v3.h"
  59. /* Base address to the AP system controller */
  60. void __iomem *ap_syscon_base;
  61. /*
  62. * All IO addresses are mapped onto VA 0xFFFx.xxxx, where x.xxxx
  63. * is the (PA >> 12).
  64. *
  65. * Setup a VA for the Integrator interrupt controller (for header #0,
  66. * just for now).
  67. */
  68. #define VA_IC_BASE __io_address(INTEGRATOR_IC_BASE)
  69. #define VA_EBI_BASE __io_address(INTEGRATOR_EBI_BASE)
  70. #define VA_CMIC_BASE __io_address(INTEGRATOR_HDR_IC)
  71. /*
  72. * Logical Physical
  73. * ef000000 Cache flush
  74. * f1000000 10000000 Core module registers
  75. * f1100000 11000000 System controller registers
  76. * f1200000 12000000 EBI registers
  77. * f1300000 13000000 Counter/Timer
  78. * f1400000 14000000 Interrupt controller
  79. * f1600000 16000000 UART 0
  80. * f1700000 17000000 UART 1
  81. * f1a00000 1a000000 Debug LEDs
  82. * f1b00000 1b000000 GPIO
  83. */
  84. static struct map_desc ap_io_desc[] __initdata __maybe_unused = {
  85. {
  86. .virtual = IO_ADDRESS(INTEGRATOR_HDR_BASE),
  87. .pfn = __phys_to_pfn(INTEGRATOR_HDR_BASE),
  88. .length = SZ_4K,
  89. .type = MT_DEVICE
  90. }, {
  91. .virtual = IO_ADDRESS(INTEGRATOR_EBI_BASE),
  92. .pfn = __phys_to_pfn(INTEGRATOR_EBI_BASE),
  93. .length = SZ_4K,
  94. .type = MT_DEVICE
  95. }, {
  96. .virtual = IO_ADDRESS(INTEGRATOR_CT_BASE),
  97. .pfn = __phys_to_pfn(INTEGRATOR_CT_BASE),
  98. .length = SZ_4K,
  99. .type = MT_DEVICE
  100. }, {
  101. .virtual = IO_ADDRESS(INTEGRATOR_IC_BASE),
  102. .pfn = __phys_to_pfn(INTEGRATOR_IC_BASE),
  103. .length = SZ_4K,
  104. .type = MT_DEVICE
  105. }, {
  106. .virtual = IO_ADDRESS(INTEGRATOR_UART0_BASE),
  107. .pfn = __phys_to_pfn(INTEGRATOR_UART0_BASE),
  108. .length = SZ_4K,
  109. .type = MT_DEVICE
  110. }, {
  111. .virtual = IO_ADDRESS(INTEGRATOR_DBG_BASE),
  112. .pfn = __phys_to_pfn(INTEGRATOR_DBG_BASE),
  113. .length = SZ_4K,
  114. .type = MT_DEVICE
  115. }, {
  116. .virtual = IO_ADDRESS(INTEGRATOR_AP_GPIO_BASE),
  117. .pfn = __phys_to_pfn(INTEGRATOR_AP_GPIO_BASE),
  118. .length = SZ_4K,
  119. .type = MT_DEVICE
  120. }
  121. };
  122. static void __init ap_map_io(void)
  123. {
  124. iotable_init(ap_io_desc, ARRAY_SIZE(ap_io_desc));
  125. pci_v3_early_init();
  126. }
  127. #ifdef CONFIG_PM
  128. static unsigned long ic_irq_enable;
  129. static int irq_suspend(void)
  130. {
  131. ic_irq_enable = readl(VA_IC_BASE + IRQ_ENABLE);
  132. return 0;
  133. }
  134. static void irq_resume(void)
  135. {
  136. /* disable all irq sources */
  137. cm_clear_irqs();
  138. writel(-1, VA_IC_BASE + IRQ_ENABLE_CLEAR);
  139. writel(-1, VA_IC_BASE + FIQ_ENABLE_CLEAR);
  140. writel(ic_irq_enable, VA_IC_BASE + IRQ_ENABLE_SET);
  141. }
  142. #else
  143. #define irq_suspend NULL
  144. #define irq_resume NULL
  145. #endif
  146. static struct syscore_ops irq_syscore_ops = {
  147. .suspend = irq_suspend,
  148. .resume = irq_resume,
  149. };
  150. static int __init irq_syscore_init(void)
  151. {
  152. register_syscore_ops(&irq_syscore_ops);
  153. return 0;
  154. }
  155. device_initcall(irq_syscore_init);
  156. /*
  157. * Flash handling.
  158. */
  159. #define EBI_CSR1 (VA_EBI_BASE + INTEGRATOR_EBI_CSR1_OFFSET)
  160. #define EBI_LOCK (VA_EBI_BASE + INTEGRATOR_EBI_LOCK_OFFSET)
  161. static int ap_flash_init(struct platform_device *dev)
  162. {
  163. u32 tmp;
  164. writel(INTEGRATOR_SC_CTRL_nFLVPPEN | INTEGRATOR_SC_CTRL_nFLWP,
  165. ap_syscon_base + INTEGRATOR_SC_CTRLC_OFFSET);
  166. tmp = readl(EBI_CSR1) | INTEGRATOR_EBI_WRITE_ENABLE;
  167. writel(tmp, EBI_CSR1);
  168. if (!(readl(EBI_CSR1) & INTEGRATOR_EBI_WRITE_ENABLE)) {
  169. writel(0xa05f, EBI_LOCK);
  170. writel(tmp, EBI_CSR1);
  171. writel(0, EBI_LOCK);
  172. }
  173. return 0;
  174. }
  175. static void ap_flash_exit(struct platform_device *dev)
  176. {
  177. u32 tmp;
  178. writel(INTEGRATOR_SC_CTRL_nFLVPPEN | INTEGRATOR_SC_CTRL_nFLWP,
  179. ap_syscon_base + INTEGRATOR_SC_CTRLC_OFFSET);
  180. tmp = readl(EBI_CSR1) & ~INTEGRATOR_EBI_WRITE_ENABLE;
  181. writel(tmp, EBI_CSR1);
  182. if (readl(EBI_CSR1) & INTEGRATOR_EBI_WRITE_ENABLE) {
  183. writel(0xa05f, EBI_LOCK);
  184. writel(tmp, EBI_CSR1);
  185. writel(0, EBI_LOCK);
  186. }
  187. }
  188. static void ap_flash_set_vpp(struct platform_device *pdev, int on)
  189. {
  190. if (on)
  191. writel(INTEGRATOR_SC_CTRL_nFLVPPEN,
  192. ap_syscon_base + INTEGRATOR_SC_CTRLS_OFFSET);
  193. else
  194. writel(INTEGRATOR_SC_CTRL_nFLVPPEN,
  195. ap_syscon_base + INTEGRATOR_SC_CTRLC_OFFSET);
  196. }
  197. static struct physmap_flash_data ap_flash_data = {
  198. .width = 4,
  199. .init = ap_flash_init,
  200. .exit = ap_flash_exit,
  201. .set_vpp = ap_flash_set_vpp,
  202. };
  203. /*
  204. * For the PL010 found in the Integrator/AP some of the UART control is
  205. * implemented in the system controller and accessed using a callback
  206. * from the driver.
  207. */
  208. static void integrator_uart_set_mctrl(struct amba_device *dev,
  209. void __iomem *base, unsigned int mctrl)
  210. {
  211. unsigned int ctrls = 0, ctrlc = 0, rts_mask, dtr_mask;
  212. u32 phybase = dev->res.start;
  213. if (phybase == INTEGRATOR_UART0_BASE) {
  214. /* UART0 */
  215. rts_mask = 1 << 4;
  216. dtr_mask = 1 << 5;
  217. } else {
  218. /* UART1 */
  219. rts_mask = 1 << 6;
  220. dtr_mask = 1 << 7;
  221. }
  222. if (mctrl & TIOCM_RTS)
  223. ctrlc |= rts_mask;
  224. else
  225. ctrls |= rts_mask;
  226. if (mctrl & TIOCM_DTR)
  227. ctrlc |= dtr_mask;
  228. else
  229. ctrls |= dtr_mask;
  230. __raw_writel(ctrls, ap_syscon_base + INTEGRATOR_SC_CTRLS_OFFSET);
  231. __raw_writel(ctrlc, ap_syscon_base + INTEGRATOR_SC_CTRLC_OFFSET);
  232. }
  233. struct amba_pl010_data ap_uart_data = {
  234. .set_mctrl = integrator_uart_set_mctrl,
  235. };
  236. /*
  237. * Where is the timer (VA)?
  238. */
  239. #define TIMER0_VA_BASE __io_address(INTEGRATOR_TIMER0_BASE)
  240. #define TIMER1_VA_BASE __io_address(INTEGRATOR_TIMER1_BASE)
  241. #define TIMER2_VA_BASE __io_address(INTEGRATOR_TIMER2_BASE)
  242. static unsigned long timer_reload;
  243. static u32 notrace integrator_read_sched_clock(void)
  244. {
  245. return -readl((void __iomem *) TIMER2_VA_BASE + TIMER_VALUE);
  246. }
  247. static void integrator_clocksource_init(unsigned long inrate,
  248. void __iomem *base)
  249. {
  250. u32 ctrl = TIMER_CTRL_ENABLE | TIMER_CTRL_PERIODIC;
  251. unsigned long rate = inrate;
  252. if (rate >= 1500000) {
  253. rate /= 16;
  254. ctrl |= TIMER_CTRL_DIV16;
  255. }
  256. writel(0xffff, base + TIMER_LOAD);
  257. writel(ctrl, base + TIMER_CTRL);
  258. clocksource_mmio_init(base + TIMER_VALUE, "timer2",
  259. rate, 200, 16, clocksource_mmio_readl_down);
  260. setup_sched_clock(integrator_read_sched_clock, 16, rate);
  261. }
  262. static void __iomem * clkevt_base;
  263. /*
  264. * IRQ handler for the timer
  265. */
  266. static irqreturn_t integrator_timer_interrupt(int irq, void *dev_id)
  267. {
  268. struct clock_event_device *evt = dev_id;
  269. /* clear the interrupt */
  270. writel(1, clkevt_base + TIMER_INTCLR);
  271. evt->event_handler(evt);
  272. return IRQ_HANDLED;
  273. }
  274. static void clkevt_set_mode(enum clock_event_mode mode, struct clock_event_device *evt)
  275. {
  276. u32 ctrl = readl(clkevt_base + TIMER_CTRL) & ~TIMER_CTRL_ENABLE;
  277. /* Disable timer */
  278. writel(ctrl, clkevt_base + TIMER_CTRL);
  279. switch (mode) {
  280. case CLOCK_EVT_MODE_PERIODIC:
  281. /* Enable the timer and start the periodic tick */
  282. writel(timer_reload, clkevt_base + TIMER_LOAD);
  283. ctrl |= TIMER_CTRL_PERIODIC | TIMER_CTRL_ENABLE;
  284. writel(ctrl, clkevt_base + TIMER_CTRL);
  285. break;
  286. case CLOCK_EVT_MODE_ONESHOT:
  287. /* Leave the timer disabled, .set_next_event will enable it */
  288. ctrl &= ~TIMER_CTRL_PERIODIC;
  289. writel(ctrl, clkevt_base + TIMER_CTRL);
  290. break;
  291. case CLOCK_EVT_MODE_UNUSED:
  292. case CLOCK_EVT_MODE_SHUTDOWN:
  293. case CLOCK_EVT_MODE_RESUME:
  294. default:
  295. /* Just leave in disabled state */
  296. break;
  297. }
  298. }
  299. static int clkevt_set_next_event(unsigned long next, struct clock_event_device *evt)
  300. {
  301. unsigned long ctrl = readl(clkevt_base + TIMER_CTRL);
  302. writel(ctrl & ~TIMER_CTRL_ENABLE, clkevt_base + TIMER_CTRL);
  303. writel(next, clkevt_base + TIMER_LOAD);
  304. writel(ctrl | TIMER_CTRL_ENABLE, clkevt_base + TIMER_CTRL);
  305. return 0;
  306. }
  307. static struct clock_event_device integrator_clockevent = {
  308. .name = "timer1",
  309. .features = CLOCK_EVT_FEAT_PERIODIC | CLOCK_EVT_FEAT_ONESHOT,
  310. .set_mode = clkevt_set_mode,
  311. .set_next_event = clkevt_set_next_event,
  312. .rating = 300,
  313. };
  314. static struct irqaction integrator_timer_irq = {
  315. .name = "timer",
  316. .flags = IRQF_DISABLED | IRQF_TIMER | IRQF_IRQPOLL,
  317. .handler = integrator_timer_interrupt,
  318. .dev_id = &integrator_clockevent,
  319. };
  320. static void integrator_clockevent_init(unsigned long inrate,
  321. void __iomem *base, int irq)
  322. {
  323. unsigned long rate = inrate;
  324. unsigned int ctrl = 0;
  325. clkevt_base = base;
  326. /* Calculate and program a divisor */
  327. if (rate > 0x100000 * HZ) {
  328. rate /= 256;
  329. ctrl |= TIMER_CTRL_DIV256;
  330. } else if (rate > 0x10000 * HZ) {
  331. rate /= 16;
  332. ctrl |= TIMER_CTRL_DIV16;
  333. }
  334. timer_reload = rate / HZ;
  335. writel(ctrl, clkevt_base + TIMER_CTRL);
  336. setup_irq(irq, &integrator_timer_irq);
  337. clockevents_config_and_register(&integrator_clockevent,
  338. rate,
  339. 1,
  340. 0xffffU);
  341. }
  342. void __init ap_init_early(void)
  343. {
  344. }
  345. static void __init ap_of_timer_init(void)
  346. {
  347. struct device_node *node;
  348. const char *path;
  349. void __iomem *base;
  350. int err;
  351. int irq;
  352. struct clk *clk;
  353. unsigned long rate;
  354. clk = clk_get_sys("ap_timer", NULL);
  355. BUG_ON(IS_ERR(clk));
  356. clk_prepare_enable(clk);
  357. rate = clk_get_rate(clk);
  358. err = of_property_read_string(of_aliases,
  359. "arm,timer-primary", &path);
  360. if (WARN_ON(err))
  361. return;
  362. node = of_find_node_by_path(path);
  363. base = of_iomap(node, 0);
  364. if (WARN_ON(!base))
  365. return;
  366. writel(0, base + TIMER_CTRL);
  367. integrator_clocksource_init(rate, base);
  368. err = of_property_read_string(of_aliases,
  369. "arm,timer-secondary", &path);
  370. if (WARN_ON(err))
  371. return;
  372. node = of_find_node_by_path(path);
  373. base = of_iomap(node, 0);
  374. if (WARN_ON(!base))
  375. return;
  376. irq = irq_of_parse_and_map(node, 0);
  377. writel(0, base + TIMER_CTRL);
  378. integrator_clockevent_init(rate, base, irq);
  379. }
  380. static const struct of_device_id fpga_irq_of_match[] __initconst = {
  381. { .compatible = "arm,versatile-fpga-irq", .data = fpga_irq_of_init, },
  382. { /* Sentinel */ }
  383. };
  384. static void __init ap_init_irq_of(void)
  385. {
  386. cm_init();
  387. of_irq_init(fpga_irq_of_match);
  388. integrator_clk_init(false);
  389. }
  390. /* For the Device Tree, add in the UART callbacks as AUXDATA */
  391. static struct of_dev_auxdata ap_auxdata_lookup[] __initdata = {
  392. OF_DEV_AUXDATA("arm,primecell", INTEGRATOR_RTC_BASE,
  393. "rtc", NULL),
  394. OF_DEV_AUXDATA("arm,primecell", INTEGRATOR_UART0_BASE,
  395. "uart0", &ap_uart_data),
  396. OF_DEV_AUXDATA("arm,primecell", INTEGRATOR_UART1_BASE,
  397. "uart1", &ap_uart_data),
  398. OF_DEV_AUXDATA("arm,primecell", KMI0_BASE,
  399. "kmi0", NULL),
  400. OF_DEV_AUXDATA("arm,primecell", KMI1_BASE,
  401. "kmi1", NULL),
  402. OF_DEV_AUXDATA("cfi-flash", INTEGRATOR_FLASH_BASE,
  403. "physmap-flash", &ap_flash_data),
  404. { /* sentinel */ },
  405. };
  406. static const struct of_device_id ap_syscon_match[] = {
  407. { .compatible = "arm,integrator-ap-syscon"},
  408. { },
  409. };
  410. static void __init ap_init_of(void)
  411. {
  412. unsigned long sc_dec;
  413. struct device_node *root;
  414. struct device_node *syscon;
  415. struct device *parent;
  416. struct soc_device *soc_dev;
  417. struct soc_device_attribute *soc_dev_attr;
  418. u32 ap_sc_id;
  419. int err;
  420. int i;
  421. /* Here we create an SoC device for the root node */
  422. root = of_find_node_by_path("/");
  423. if (!root)
  424. return;
  425. syscon = of_find_matching_node(root, ap_syscon_match);
  426. if (!syscon)
  427. return;
  428. ap_syscon_base = of_iomap(syscon, 0);
  429. if (!ap_syscon_base)
  430. return;
  431. ap_sc_id = readl(ap_syscon_base);
  432. soc_dev_attr = kzalloc(sizeof(*soc_dev_attr), GFP_KERNEL);
  433. if (!soc_dev_attr)
  434. return;
  435. err = of_property_read_string(root, "compatible",
  436. &soc_dev_attr->soc_id);
  437. if (err)
  438. return;
  439. err = of_property_read_string(root, "model", &soc_dev_attr->machine);
  440. if (err)
  441. return;
  442. soc_dev_attr->family = "Integrator";
  443. soc_dev_attr->revision = kasprintf(GFP_KERNEL, "%c",
  444. 'A' + (ap_sc_id & 0x0f));
  445. soc_dev = soc_device_register(soc_dev_attr);
  446. if (IS_ERR(soc_dev)) {
  447. kfree(soc_dev_attr->revision);
  448. kfree(soc_dev_attr);
  449. return;
  450. }
  451. parent = soc_device_to_device(soc_dev);
  452. integrator_init_sysfs(parent, ap_sc_id);
  453. of_platform_populate(root, of_default_bus_match_table,
  454. ap_auxdata_lookup, parent);
  455. sc_dec = readl(ap_syscon_base + INTEGRATOR_SC_DEC_OFFSET);
  456. for (i = 0; i < 4; i++) {
  457. struct lm_device *lmdev;
  458. if ((sc_dec & (16 << i)) == 0)
  459. continue;
  460. lmdev = kzalloc(sizeof(struct lm_device), GFP_KERNEL);
  461. if (!lmdev)
  462. continue;
  463. lmdev->resource.start = 0xc0000000 + 0x10000000 * i;
  464. lmdev->resource.end = lmdev->resource.start + 0x0fffffff;
  465. lmdev->resource.flags = IORESOURCE_MEM;
  466. lmdev->irq = irq_of_parse_and_map(syscon, i);
  467. lmdev->id = i;
  468. lm_device_register(lmdev);
  469. }
  470. }
  471. static const char * ap_dt_board_compat[] = {
  472. "arm,integrator-ap",
  473. NULL,
  474. };
  475. DT_MACHINE_START(INTEGRATOR_AP_DT, "ARM Integrator/AP (Device Tree)")
  476. .reserve = integrator_reserve,
  477. .map_io = ap_map_io,
  478. .init_early = ap_init_early,
  479. .init_irq = ap_init_irq_of,
  480. .handle_irq = fpga_handle_irq,
  481. .init_time = ap_of_timer_init,
  482. .init_machine = ap_init_of,
  483. .restart = integrator_restart,
  484. .dt_compat = ap_dt_board_compat,
  485. MACHINE_END