integrator_ap.c 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563
  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 "common.h"
  57. #include "pci_v3.h"
  58. /* Base address to the AP system controller */
  59. void __iomem *ap_syscon_base;
  60. /*
  61. * All IO addresses are mapped onto VA 0xFFFx.xxxx, where x.xxxx
  62. * is the (PA >> 12).
  63. *
  64. * Setup a VA for the Integrator interrupt controller (for header #0,
  65. * just for now).
  66. */
  67. #define VA_IC_BASE __io_address(INTEGRATOR_IC_BASE)
  68. #define VA_EBI_BASE __io_address(INTEGRATOR_EBI_BASE)
  69. #define VA_CMIC_BASE __io_address(INTEGRATOR_HDR_IC)
  70. /*
  71. * Logical Physical
  72. * ef000000 Cache flush
  73. * f1000000 10000000 Core module registers
  74. * f1100000 11000000 System controller registers
  75. * f1200000 12000000 EBI registers
  76. * f1300000 13000000 Counter/Timer
  77. * f1400000 14000000 Interrupt controller
  78. * f1600000 16000000 UART 0
  79. * f1700000 17000000 UART 1
  80. * f1a00000 1a000000 Debug LEDs
  81. * f1b00000 1b000000 GPIO
  82. */
  83. static struct map_desc ap_io_desc[] __initdata __maybe_unused = {
  84. {
  85. .virtual = IO_ADDRESS(INTEGRATOR_HDR_BASE),
  86. .pfn = __phys_to_pfn(INTEGRATOR_HDR_BASE),
  87. .length = SZ_4K,
  88. .type = MT_DEVICE
  89. }, {
  90. .virtual = IO_ADDRESS(INTEGRATOR_EBI_BASE),
  91. .pfn = __phys_to_pfn(INTEGRATOR_EBI_BASE),
  92. .length = SZ_4K,
  93. .type = MT_DEVICE
  94. }, {
  95. .virtual = IO_ADDRESS(INTEGRATOR_CT_BASE),
  96. .pfn = __phys_to_pfn(INTEGRATOR_CT_BASE),
  97. .length = SZ_4K,
  98. .type = MT_DEVICE
  99. }, {
  100. .virtual = IO_ADDRESS(INTEGRATOR_IC_BASE),
  101. .pfn = __phys_to_pfn(INTEGRATOR_IC_BASE),
  102. .length = SZ_4K,
  103. .type = MT_DEVICE
  104. }, {
  105. .virtual = IO_ADDRESS(INTEGRATOR_UART0_BASE),
  106. .pfn = __phys_to_pfn(INTEGRATOR_UART0_BASE),
  107. .length = SZ_4K,
  108. .type = MT_DEVICE
  109. }, {
  110. .virtual = IO_ADDRESS(INTEGRATOR_DBG_BASE),
  111. .pfn = __phys_to_pfn(INTEGRATOR_DBG_BASE),
  112. .length = SZ_4K,
  113. .type = MT_DEVICE
  114. }, {
  115. .virtual = IO_ADDRESS(INTEGRATOR_AP_GPIO_BASE),
  116. .pfn = __phys_to_pfn(INTEGRATOR_AP_GPIO_BASE),
  117. .length = SZ_4K,
  118. .type = MT_DEVICE
  119. }
  120. };
  121. static void __init ap_map_io(void)
  122. {
  123. iotable_init(ap_io_desc, ARRAY_SIZE(ap_io_desc));
  124. pci_v3_early_init();
  125. }
  126. #ifdef CONFIG_PM
  127. static unsigned long ic_irq_enable;
  128. static int irq_suspend(void)
  129. {
  130. ic_irq_enable = readl(VA_IC_BASE + IRQ_ENABLE);
  131. return 0;
  132. }
  133. static void irq_resume(void)
  134. {
  135. /* disable all irq sources */
  136. writel(-1, VA_CMIC_BASE + IRQ_ENABLE_CLEAR);
  137. writel(-1, VA_IC_BASE + IRQ_ENABLE_CLEAR);
  138. writel(-1, VA_IC_BASE + FIQ_ENABLE_CLEAR);
  139. writel(ic_irq_enable, VA_IC_BASE + IRQ_ENABLE_SET);
  140. }
  141. #else
  142. #define irq_suspend NULL
  143. #define irq_resume NULL
  144. #endif
  145. static struct syscore_ops irq_syscore_ops = {
  146. .suspend = irq_suspend,
  147. .resume = irq_resume,
  148. };
  149. static int __init irq_syscore_init(void)
  150. {
  151. register_syscore_ops(&irq_syscore_ops);
  152. return 0;
  153. }
  154. device_initcall(irq_syscore_init);
  155. /*
  156. * Flash handling.
  157. */
  158. #define EBI_CSR1 (VA_EBI_BASE + INTEGRATOR_EBI_CSR1_OFFSET)
  159. #define EBI_LOCK (VA_EBI_BASE + INTEGRATOR_EBI_LOCK_OFFSET)
  160. static int ap_flash_init(struct platform_device *dev)
  161. {
  162. u32 tmp;
  163. writel(INTEGRATOR_SC_CTRL_nFLVPPEN | INTEGRATOR_SC_CTRL_nFLWP,
  164. ap_syscon_base + INTEGRATOR_SC_CTRLC_OFFSET);
  165. tmp = readl(EBI_CSR1) | INTEGRATOR_EBI_WRITE_ENABLE;
  166. writel(tmp, EBI_CSR1);
  167. if (!(readl(EBI_CSR1) & INTEGRATOR_EBI_WRITE_ENABLE)) {
  168. writel(0xa05f, EBI_LOCK);
  169. writel(tmp, EBI_CSR1);
  170. writel(0, EBI_LOCK);
  171. }
  172. return 0;
  173. }
  174. static void ap_flash_exit(struct platform_device *dev)
  175. {
  176. u32 tmp;
  177. writel(INTEGRATOR_SC_CTRL_nFLVPPEN | INTEGRATOR_SC_CTRL_nFLWP,
  178. ap_syscon_base + INTEGRATOR_SC_CTRLC_OFFSET);
  179. tmp = readl(EBI_CSR1) & ~INTEGRATOR_EBI_WRITE_ENABLE;
  180. writel(tmp, EBI_CSR1);
  181. if (readl(EBI_CSR1) & INTEGRATOR_EBI_WRITE_ENABLE) {
  182. writel(0xa05f, EBI_LOCK);
  183. writel(tmp, EBI_CSR1);
  184. writel(0, EBI_LOCK);
  185. }
  186. }
  187. static void ap_flash_set_vpp(struct platform_device *pdev, int on)
  188. {
  189. if (on)
  190. writel(INTEGRATOR_SC_CTRL_nFLVPPEN,
  191. ap_syscon_base + INTEGRATOR_SC_CTRLS_OFFSET);
  192. else
  193. writel(INTEGRATOR_SC_CTRL_nFLVPPEN,
  194. ap_syscon_base + INTEGRATOR_SC_CTRLC_OFFSET);
  195. }
  196. static struct physmap_flash_data ap_flash_data = {
  197. .width = 4,
  198. .init = ap_flash_init,
  199. .exit = ap_flash_exit,
  200. .set_vpp = ap_flash_set_vpp,
  201. };
  202. /*
  203. * For the PL010 found in the Integrator/AP some of the UART control is
  204. * implemented in the system controller and accessed using a callback
  205. * from the driver.
  206. */
  207. static void integrator_uart_set_mctrl(struct amba_device *dev,
  208. void __iomem *base, unsigned int mctrl)
  209. {
  210. unsigned int ctrls = 0, ctrlc = 0, rts_mask, dtr_mask;
  211. u32 phybase = dev->res.start;
  212. if (phybase == INTEGRATOR_UART0_BASE) {
  213. /* UART0 */
  214. rts_mask = 1 << 4;
  215. dtr_mask = 1 << 5;
  216. } else {
  217. /* UART1 */
  218. rts_mask = 1 << 6;
  219. dtr_mask = 1 << 7;
  220. }
  221. if (mctrl & TIOCM_RTS)
  222. ctrlc |= rts_mask;
  223. else
  224. ctrls |= rts_mask;
  225. if (mctrl & TIOCM_DTR)
  226. ctrlc |= dtr_mask;
  227. else
  228. ctrls |= dtr_mask;
  229. __raw_writel(ctrls, ap_syscon_base + INTEGRATOR_SC_CTRLS_OFFSET);
  230. __raw_writel(ctrlc, ap_syscon_base + INTEGRATOR_SC_CTRLC_OFFSET);
  231. }
  232. struct amba_pl010_data ap_uart_data = {
  233. .set_mctrl = integrator_uart_set_mctrl,
  234. };
  235. /*
  236. * Where is the timer (VA)?
  237. */
  238. #define TIMER0_VA_BASE __io_address(INTEGRATOR_TIMER0_BASE)
  239. #define TIMER1_VA_BASE __io_address(INTEGRATOR_TIMER1_BASE)
  240. #define TIMER2_VA_BASE __io_address(INTEGRATOR_TIMER2_BASE)
  241. static unsigned long timer_reload;
  242. static u32 notrace integrator_read_sched_clock(void)
  243. {
  244. return -readl((void __iomem *) TIMER2_VA_BASE + TIMER_VALUE);
  245. }
  246. static void integrator_clocksource_init(unsigned long inrate,
  247. void __iomem *base)
  248. {
  249. u32 ctrl = TIMER_CTRL_ENABLE | TIMER_CTRL_PERIODIC;
  250. unsigned long rate = inrate;
  251. if (rate >= 1500000) {
  252. rate /= 16;
  253. ctrl |= TIMER_CTRL_DIV16;
  254. }
  255. writel(0xffff, base + TIMER_LOAD);
  256. writel(ctrl, base + TIMER_CTRL);
  257. clocksource_mmio_init(base + TIMER_VALUE, "timer2",
  258. rate, 200, 16, clocksource_mmio_readl_down);
  259. setup_sched_clock(integrator_read_sched_clock, 16, rate);
  260. }
  261. static void __iomem * clkevt_base;
  262. /*
  263. * IRQ handler for the timer
  264. */
  265. static irqreturn_t integrator_timer_interrupt(int irq, void *dev_id)
  266. {
  267. struct clock_event_device *evt = dev_id;
  268. /* clear the interrupt */
  269. writel(1, clkevt_base + TIMER_INTCLR);
  270. evt->event_handler(evt);
  271. return IRQ_HANDLED;
  272. }
  273. static void clkevt_set_mode(enum clock_event_mode mode, struct clock_event_device *evt)
  274. {
  275. u32 ctrl = readl(clkevt_base + TIMER_CTRL) & ~TIMER_CTRL_ENABLE;
  276. /* Disable timer */
  277. writel(ctrl, clkevt_base + TIMER_CTRL);
  278. switch (mode) {
  279. case CLOCK_EVT_MODE_PERIODIC:
  280. /* Enable the timer and start the periodic tick */
  281. writel(timer_reload, clkevt_base + TIMER_LOAD);
  282. ctrl |= TIMER_CTRL_PERIODIC | TIMER_CTRL_ENABLE;
  283. writel(ctrl, clkevt_base + TIMER_CTRL);
  284. break;
  285. case CLOCK_EVT_MODE_ONESHOT:
  286. /* Leave the timer disabled, .set_next_event will enable it */
  287. ctrl &= ~TIMER_CTRL_PERIODIC;
  288. writel(ctrl, clkevt_base + TIMER_CTRL);
  289. break;
  290. case CLOCK_EVT_MODE_UNUSED:
  291. case CLOCK_EVT_MODE_SHUTDOWN:
  292. case CLOCK_EVT_MODE_RESUME:
  293. default:
  294. /* Just leave in disabled state */
  295. break;
  296. }
  297. }
  298. static int clkevt_set_next_event(unsigned long next, struct clock_event_device *evt)
  299. {
  300. unsigned long ctrl = readl(clkevt_base + TIMER_CTRL);
  301. writel(ctrl & ~TIMER_CTRL_ENABLE, clkevt_base + TIMER_CTRL);
  302. writel(next, clkevt_base + TIMER_LOAD);
  303. writel(ctrl | TIMER_CTRL_ENABLE, clkevt_base + TIMER_CTRL);
  304. return 0;
  305. }
  306. static struct clock_event_device integrator_clockevent = {
  307. .name = "timer1",
  308. .features = CLOCK_EVT_FEAT_PERIODIC | CLOCK_EVT_FEAT_ONESHOT,
  309. .set_mode = clkevt_set_mode,
  310. .set_next_event = clkevt_set_next_event,
  311. .rating = 300,
  312. };
  313. static struct irqaction integrator_timer_irq = {
  314. .name = "timer",
  315. .flags = IRQF_DISABLED | IRQF_TIMER | IRQF_IRQPOLL,
  316. .handler = integrator_timer_interrupt,
  317. .dev_id = &integrator_clockevent,
  318. };
  319. static void integrator_clockevent_init(unsigned long inrate,
  320. void __iomem *base, int irq)
  321. {
  322. unsigned long rate = inrate;
  323. unsigned int ctrl = 0;
  324. clkevt_base = base;
  325. /* Calculate and program a divisor */
  326. if (rate > 0x100000 * HZ) {
  327. rate /= 256;
  328. ctrl |= TIMER_CTRL_DIV256;
  329. } else if (rate > 0x10000 * HZ) {
  330. rate /= 16;
  331. ctrl |= TIMER_CTRL_DIV16;
  332. }
  333. timer_reload = rate / HZ;
  334. writel(ctrl, clkevt_base + TIMER_CTRL);
  335. setup_irq(irq, &integrator_timer_irq);
  336. clockevents_config_and_register(&integrator_clockevent,
  337. rate,
  338. 1,
  339. 0xffffU);
  340. }
  341. void __init ap_init_early(void)
  342. {
  343. }
  344. static void __init ap_of_timer_init(void)
  345. {
  346. struct device_node *node;
  347. const char *path;
  348. void __iomem *base;
  349. int err;
  350. int irq;
  351. struct clk *clk;
  352. unsigned long rate;
  353. clk = clk_get_sys("ap_timer", NULL);
  354. BUG_ON(IS_ERR(clk));
  355. clk_prepare_enable(clk);
  356. rate = clk_get_rate(clk);
  357. err = of_property_read_string(of_aliases,
  358. "arm,timer-primary", &path);
  359. if (WARN_ON(err))
  360. return;
  361. node = of_find_node_by_path(path);
  362. base = of_iomap(node, 0);
  363. if (WARN_ON(!base))
  364. return;
  365. writel(0, base + TIMER_CTRL);
  366. integrator_clocksource_init(rate, base);
  367. err = of_property_read_string(of_aliases,
  368. "arm,timer-secondary", &path);
  369. if (WARN_ON(err))
  370. return;
  371. node = of_find_node_by_path(path);
  372. base = of_iomap(node, 0);
  373. if (WARN_ON(!base))
  374. return;
  375. irq = irq_of_parse_and_map(node, 0);
  376. writel(0, base + TIMER_CTRL);
  377. integrator_clockevent_init(rate, base, irq);
  378. }
  379. static const struct of_device_id fpga_irq_of_match[] __initconst = {
  380. { .compatible = "arm,versatile-fpga-irq", .data = fpga_irq_of_init, },
  381. { /* Sentinel */ }
  382. };
  383. static void __init ap_init_irq_of(void)
  384. {
  385. /* disable core module IRQs */
  386. writel(0xffffffffU, VA_CMIC_BASE + IRQ_ENABLE_CLEAR);
  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 void __init ap_init_of(void)
  407. {
  408. unsigned long sc_dec;
  409. struct device_node *root;
  410. struct device_node *syscon;
  411. struct device *parent;
  412. struct soc_device *soc_dev;
  413. struct soc_device_attribute *soc_dev_attr;
  414. u32 ap_sc_id;
  415. int err;
  416. int i;
  417. /* Here we create an SoC device for the root node */
  418. root = of_find_node_by_path("/");
  419. if (!root)
  420. return;
  421. syscon = of_find_node_by_path("/syscon");
  422. if (!syscon)
  423. return;
  424. ap_syscon_base = of_iomap(syscon, 0);
  425. if (!ap_syscon_base)
  426. return;
  427. ap_sc_id = readl(ap_syscon_base);
  428. soc_dev_attr = kzalloc(sizeof(*soc_dev_attr), GFP_KERNEL);
  429. if (!soc_dev_attr)
  430. return;
  431. err = of_property_read_string(root, "compatible",
  432. &soc_dev_attr->soc_id);
  433. if (err)
  434. return;
  435. err = of_property_read_string(root, "model", &soc_dev_attr->machine);
  436. if (err)
  437. return;
  438. soc_dev_attr->family = "Integrator";
  439. soc_dev_attr->revision = kasprintf(GFP_KERNEL, "%c",
  440. 'A' + (ap_sc_id & 0x0f));
  441. soc_dev = soc_device_register(soc_dev_attr);
  442. if (IS_ERR(soc_dev)) {
  443. kfree(soc_dev_attr->revision);
  444. kfree(soc_dev_attr);
  445. return;
  446. }
  447. parent = soc_device_to_device(soc_dev);
  448. integrator_init_sysfs(parent, ap_sc_id);
  449. of_platform_populate(root, of_default_bus_match_table,
  450. ap_auxdata_lookup, parent);
  451. sc_dec = readl(ap_syscon_base + INTEGRATOR_SC_DEC_OFFSET);
  452. for (i = 0; i < 4; i++) {
  453. struct lm_device *lmdev;
  454. if ((sc_dec & (16 << i)) == 0)
  455. continue;
  456. lmdev = kzalloc(sizeof(struct lm_device), GFP_KERNEL);
  457. if (!lmdev)
  458. continue;
  459. lmdev->resource.start = 0xc0000000 + 0x10000000 * i;
  460. lmdev->resource.end = lmdev->resource.start + 0x0fffffff;
  461. lmdev->resource.flags = IORESOURCE_MEM;
  462. lmdev->irq = irq_of_parse_and_map(syscon, i);
  463. lmdev->id = i;
  464. lm_device_register(lmdev);
  465. }
  466. }
  467. static const char * ap_dt_board_compat[] = {
  468. "arm,integrator-ap",
  469. NULL,
  470. };
  471. DT_MACHINE_START(INTEGRATOR_AP_DT, "ARM Integrator/AP (Device Tree)")
  472. .reserve = integrator_reserve,
  473. .map_io = ap_map_io,
  474. .init_early = ap_init_early,
  475. .init_irq = ap_init_irq_of,
  476. .handle_irq = fpga_handle_irq,
  477. .init_time = ap_of_timer_init,
  478. .init_machine = ap_init_of,
  479. .restart = integrator_restart,
  480. .dt_compat = ap_dt_board_compat,
  481. MACHINE_END