common.c 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446
  1. /*
  2. * Copyright (c) 2011 Samsung Electronics Co., Ltd.
  3. * http://www.samsung.com
  4. *
  5. * Copyright 2008 Openmoko, Inc.
  6. * Copyright 2008 Simtec Electronics
  7. * Ben Dooks <ben@simtec.co.uk>
  8. * http://armlinux.simtec.co.uk/
  9. *
  10. * Common Codes for S3C64XX machines
  11. *
  12. * This program is free software; you can redistribute it and/or modify
  13. * it under the terms of the GNU General Public License version 2 as
  14. * published by the Free Software Foundation.
  15. */
  16. /*
  17. * NOTE: Code in this file is not used when booting with Device Tree support.
  18. */
  19. #include <linux/kernel.h>
  20. #include <linux/init.h>
  21. #include <linux/module.h>
  22. #include <linux/clk-provider.h>
  23. #include <linux/interrupt.h>
  24. #include <linux/ioport.h>
  25. #include <linux/serial_core.h>
  26. #include <linux/platform_device.h>
  27. #include <linux/reboot.h>
  28. #include <linux/io.h>
  29. #include <linux/dma-mapping.h>
  30. #include <linux/irq.h>
  31. #include <linux/gpio.h>
  32. #include <linux/irqchip/arm-vic.h>
  33. #include <clocksource/samsung_pwm.h>
  34. #include <asm/mach/arch.h>
  35. #include <asm/mach/map.h>
  36. #include <asm/system_misc.h>
  37. #include <mach/map.h>
  38. #include <mach/hardware.h>
  39. #include <mach/regs-gpio.h>
  40. #include <plat/cpu.h>
  41. #include <plat/devs.h>
  42. #include <plat/pm.h>
  43. #include <plat/gpio-cfg.h>
  44. #include <plat/irq-uart.h>
  45. #include <plat/pwm-core.h>
  46. #include <plat/regs-irqtype.h>
  47. #include <plat/regs-serial.h>
  48. #include <plat/watchdog-reset.h>
  49. #include "common.h"
  50. /* External clock frequency */
  51. static unsigned long xtal_f = 12000000, xusbxti_f = 48000000;
  52. void __init s3c64xx_set_xtal_freq(unsigned long freq)
  53. {
  54. xtal_f = freq;
  55. }
  56. void __init s3c64xx_set_xusbxti_freq(unsigned long freq)
  57. {
  58. xusbxti_f = freq;
  59. }
  60. /* uart registration process */
  61. static void __init s3c64xx_init_uarts(struct s3c2410_uartcfg *cfg, int no)
  62. {
  63. s3c24xx_init_uartdevs("s3c6400-uart", s3c64xx_uart_resources, cfg, no);
  64. }
  65. /* table of supported CPUs */
  66. static const char name_s3c6400[] = "S3C6400";
  67. static const char name_s3c6410[] = "S3C6410";
  68. static struct cpu_table cpu_ids[] __initdata = {
  69. {
  70. .idcode = S3C6400_CPU_ID,
  71. .idmask = S3C64XX_CPU_MASK,
  72. .map_io = s3c6400_map_io,
  73. .init_uarts = s3c64xx_init_uarts,
  74. .init = s3c6400_init,
  75. .name = name_s3c6400,
  76. }, {
  77. .idcode = S3C6410_CPU_ID,
  78. .idmask = S3C64XX_CPU_MASK,
  79. .map_io = s3c6410_map_io,
  80. .init_uarts = s3c64xx_init_uarts,
  81. .init = s3c6410_init,
  82. .name = name_s3c6410,
  83. },
  84. };
  85. /* minimal IO mapping */
  86. /* see notes on uart map in arch/arm/mach-s3c64xx/include/mach/debug-macro.S */
  87. #define UART_OFFS (S3C_PA_UART & 0xfffff)
  88. static struct map_desc s3c_iodesc[] __initdata = {
  89. {
  90. .virtual = (unsigned long)S3C_VA_SYS,
  91. .pfn = __phys_to_pfn(S3C64XX_PA_SYSCON),
  92. .length = SZ_4K,
  93. .type = MT_DEVICE,
  94. }, {
  95. .virtual = (unsigned long)S3C_VA_MEM,
  96. .pfn = __phys_to_pfn(S3C64XX_PA_SROM),
  97. .length = SZ_4K,
  98. .type = MT_DEVICE,
  99. }, {
  100. .virtual = (unsigned long)(S3C_VA_UART + UART_OFFS),
  101. .pfn = __phys_to_pfn(S3C_PA_UART),
  102. .length = SZ_4K,
  103. .type = MT_DEVICE,
  104. }, {
  105. .virtual = (unsigned long)VA_VIC0,
  106. .pfn = __phys_to_pfn(S3C64XX_PA_VIC0),
  107. .length = SZ_16K,
  108. .type = MT_DEVICE,
  109. }, {
  110. .virtual = (unsigned long)VA_VIC1,
  111. .pfn = __phys_to_pfn(S3C64XX_PA_VIC1),
  112. .length = SZ_16K,
  113. .type = MT_DEVICE,
  114. }, {
  115. .virtual = (unsigned long)S3C_VA_TIMER,
  116. .pfn = __phys_to_pfn(S3C_PA_TIMER),
  117. .length = SZ_16K,
  118. .type = MT_DEVICE,
  119. }, {
  120. .virtual = (unsigned long)S3C64XX_VA_GPIO,
  121. .pfn = __phys_to_pfn(S3C64XX_PA_GPIO),
  122. .length = SZ_4K,
  123. .type = MT_DEVICE,
  124. }, {
  125. .virtual = (unsigned long)S3C64XX_VA_MODEM,
  126. .pfn = __phys_to_pfn(S3C64XX_PA_MODEM),
  127. .length = SZ_4K,
  128. .type = MT_DEVICE,
  129. }, {
  130. .virtual = (unsigned long)S3C_VA_WATCHDOG,
  131. .pfn = __phys_to_pfn(S3C64XX_PA_WATCHDOG),
  132. .length = SZ_4K,
  133. .type = MT_DEVICE,
  134. }, {
  135. .virtual = (unsigned long)S3C_VA_USB_HSPHY,
  136. .pfn = __phys_to_pfn(S3C64XX_PA_USB_HSPHY),
  137. .length = SZ_1K,
  138. .type = MT_DEVICE,
  139. },
  140. };
  141. static struct bus_type s3c64xx_subsys = {
  142. .name = "s3c64xx-core",
  143. .dev_name = "s3c64xx-core",
  144. };
  145. static struct device s3c64xx_dev = {
  146. .bus = &s3c64xx_subsys,
  147. };
  148. static struct samsung_pwm_variant s3c64xx_pwm_variant = {
  149. .bits = 32,
  150. .div_base = 0,
  151. .has_tint_cstat = true,
  152. .tclk_mask = (1 << 7) | (1 << 6) | (1 << 5),
  153. };
  154. void __init samsung_set_timer_source(unsigned int event, unsigned int source)
  155. {
  156. s3c64xx_pwm_variant.output_mask = BIT(SAMSUNG_PWM_NUM) - 1;
  157. s3c64xx_pwm_variant.output_mask &= ~(BIT(event) | BIT(source));
  158. }
  159. void __init samsung_timer_init(void)
  160. {
  161. unsigned int timer_irqs[SAMSUNG_PWM_NUM] = {
  162. IRQ_TIMER0_VIC, IRQ_TIMER1_VIC, IRQ_TIMER2_VIC,
  163. IRQ_TIMER3_VIC, IRQ_TIMER4_VIC,
  164. };
  165. samsung_pwm_clocksource_init(S3C_VA_TIMER,
  166. timer_irqs, &s3c64xx_pwm_variant);
  167. }
  168. /* read cpu identification code */
  169. void __init s3c64xx_init_io(struct map_desc *mach_desc, int size)
  170. {
  171. /* initialise the io descriptors we need for initialisation */
  172. iotable_init(s3c_iodesc, ARRAY_SIZE(s3c_iodesc));
  173. iotable_init(mach_desc, size);
  174. /* detect cpu id */
  175. s3c64xx_init_cpu();
  176. s3c_init_cpu(samsung_cpu_id, cpu_ids, ARRAY_SIZE(cpu_ids));
  177. samsung_pwm_set_platdata(&s3c64xx_pwm_variant);
  178. }
  179. static __init int s3c64xx_dev_init(void)
  180. {
  181. /* Not applicable when using DT. */
  182. if (of_have_populated_dt())
  183. return 0;
  184. subsys_system_register(&s3c64xx_subsys, NULL);
  185. return device_register(&s3c64xx_dev);
  186. }
  187. core_initcall(s3c64xx_dev_init);
  188. /*
  189. * setup the sources the vic should advertise resume
  190. * for, even though it is not doing the wake
  191. * (set_irq_wake needs to be valid)
  192. */
  193. #define IRQ_VIC0_RESUME (1 << (IRQ_RTC_TIC - IRQ_VIC0_BASE))
  194. #define IRQ_VIC1_RESUME (1 << (IRQ_RTC_ALARM - IRQ_VIC1_BASE) | \
  195. 1 << (IRQ_PENDN - IRQ_VIC1_BASE) | \
  196. 1 << (IRQ_HSMMC0 - IRQ_VIC1_BASE) | \
  197. 1 << (IRQ_HSMMC1 - IRQ_VIC1_BASE) | \
  198. 1 << (IRQ_HSMMC2 - IRQ_VIC1_BASE))
  199. void __init s3c64xx_init_irq(u32 vic0_valid, u32 vic1_valid)
  200. {
  201. /*
  202. * FIXME: there is no better place to put this at the moment
  203. * (s3c64xx_clk_init needs ioremap and must happen before init_time
  204. * samsung_wdt_reset_init needs clocks)
  205. */
  206. s3c64xx_clk_init(NULL, xtal_f, xusbxti_f, soc_is_s3c6400(), S3C_VA_SYS);
  207. samsung_wdt_reset_init(S3C_VA_WATCHDOG);
  208. printk(KERN_DEBUG "%s: initialising interrupts\n", __func__);
  209. /* initialise the pair of VICs */
  210. vic_init(VA_VIC0, IRQ_VIC0_BASE, vic0_valid, IRQ_VIC0_RESUME);
  211. vic_init(VA_VIC1, IRQ_VIC1_BASE, vic1_valid, IRQ_VIC1_RESUME);
  212. }
  213. #define eint_offset(irq) ((irq) - IRQ_EINT(0))
  214. #define eint_irq_to_bit(irq) ((u32)(1 << eint_offset(irq)))
  215. static inline void s3c_irq_eint_mask(struct irq_data *data)
  216. {
  217. u32 mask;
  218. mask = __raw_readl(S3C64XX_EINT0MASK);
  219. mask |= (u32)data->chip_data;
  220. __raw_writel(mask, S3C64XX_EINT0MASK);
  221. }
  222. static void s3c_irq_eint_unmask(struct irq_data *data)
  223. {
  224. u32 mask;
  225. mask = __raw_readl(S3C64XX_EINT0MASK);
  226. mask &= ~((u32)data->chip_data);
  227. __raw_writel(mask, S3C64XX_EINT0MASK);
  228. }
  229. static inline void s3c_irq_eint_ack(struct irq_data *data)
  230. {
  231. __raw_writel((u32)data->chip_data, S3C64XX_EINT0PEND);
  232. }
  233. static void s3c_irq_eint_maskack(struct irq_data *data)
  234. {
  235. /* compiler should in-line these */
  236. s3c_irq_eint_mask(data);
  237. s3c_irq_eint_ack(data);
  238. }
  239. static int s3c_irq_eint_set_type(struct irq_data *data, unsigned int type)
  240. {
  241. int offs = eint_offset(data->irq);
  242. int pin, pin_val;
  243. int shift;
  244. u32 ctrl, mask;
  245. u32 newvalue = 0;
  246. void __iomem *reg;
  247. if (offs > 27)
  248. return -EINVAL;
  249. if (offs <= 15)
  250. reg = S3C64XX_EINT0CON0;
  251. else
  252. reg = S3C64XX_EINT0CON1;
  253. switch (type) {
  254. case IRQ_TYPE_NONE:
  255. printk(KERN_WARNING "No edge setting!\n");
  256. break;
  257. case IRQ_TYPE_EDGE_RISING:
  258. newvalue = S3C2410_EXTINT_RISEEDGE;
  259. break;
  260. case IRQ_TYPE_EDGE_FALLING:
  261. newvalue = S3C2410_EXTINT_FALLEDGE;
  262. break;
  263. case IRQ_TYPE_EDGE_BOTH:
  264. newvalue = S3C2410_EXTINT_BOTHEDGE;
  265. break;
  266. case IRQ_TYPE_LEVEL_LOW:
  267. newvalue = S3C2410_EXTINT_LOWLEV;
  268. break;
  269. case IRQ_TYPE_LEVEL_HIGH:
  270. newvalue = S3C2410_EXTINT_HILEV;
  271. break;
  272. default:
  273. printk(KERN_ERR "No such irq type %d", type);
  274. return -1;
  275. }
  276. if (offs <= 15)
  277. shift = (offs / 2) * 4;
  278. else
  279. shift = ((offs - 16) / 2) * 4;
  280. mask = 0x7 << shift;
  281. ctrl = __raw_readl(reg);
  282. ctrl &= ~mask;
  283. ctrl |= newvalue << shift;
  284. __raw_writel(ctrl, reg);
  285. /* set the GPIO pin appropriately */
  286. if (offs < 16) {
  287. pin = S3C64XX_GPN(offs);
  288. pin_val = S3C_GPIO_SFN(2);
  289. } else if (offs < 23) {
  290. pin = S3C64XX_GPL(offs + 8 - 16);
  291. pin_val = S3C_GPIO_SFN(3);
  292. } else {
  293. pin = S3C64XX_GPM(offs - 23);
  294. pin_val = S3C_GPIO_SFN(3);
  295. }
  296. s3c_gpio_cfgpin(pin, pin_val);
  297. return 0;
  298. }
  299. static struct irq_chip s3c_irq_eint = {
  300. .name = "s3c-eint",
  301. .irq_mask = s3c_irq_eint_mask,
  302. .irq_unmask = s3c_irq_eint_unmask,
  303. .irq_mask_ack = s3c_irq_eint_maskack,
  304. .irq_ack = s3c_irq_eint_ack,
  305. .irq_set_type = s3c_irq_eint_set_type,
  306. .irq_set_wake = s3c_irqext_wake,
  307. };
  308. /* s3c_irq_demux_eint
  309. *
  310. * This function demuxes the IRQ from the group0 external interrupts,
  311. * from IRQ_EINT(0) to IRQ_EINT(27). It is designed to be inlined into
  312. * the specific handlers s3c_irq_demux_eintX_Y.
  313. */
  314. static inline void s3c_irq_demux_eint(unsigned int start, unsigned int end)
  315. {
  316. u32 status = __raw_readl(S3C64XX_EINT0PEND);
  317. u32 mask = __raw_readl(S3C64XX_EINT0MASK);
  318. unsigned int irq;
  319. status &= ~mask;
  320. status >>= start;
  321. status &= (1 << (end - start + 1)) - 1;
  322. for (irq = IRQ_EINT(start); irq <= IRQ_EINT(end); irq++) {
  323. if (status & 1)
  324. generic_handle_irq(irq);
  325. status >>= 1;
  326. }
  327. }
  328. static void s3c_irq_demux_eint0_3(unsigned int irq, struct irq_desc *desc)
  329. {
  330. s3c_irq_demux_eint(0, 3);
  331. }
  332. static void s3c_irq_demux_eint4_11(unsigned int irq, struct irq_desc *desc)
  333. {
  334. s3c_irq_demux_eint(4, 11);
  335. }
  336. static void s3c_irq_demux_eint12_19(unsigned int irq, struct irq_desc *desc)
  337. {
  338. s3c_irq_demux_eint(12, 19);
  339. }
  340. static void s3c_irq_demux_eint20_27(unsigned int irq, struct irq_desc *desc)
  341. {
  342. s3c_irq_demux_eint(20, 27);
  343. }
  344. static int __init s3c64xx_init_irq_eint(void)
  345. {
  346. int irq;
  347. /* On DT-enabled systems EINTs are handled by pinctrl-s3c64xx driver. */
  348. if (of_have_populated_dt())
  349. return -ENODEV;
  350. for (irq = IRQ_EINT(0); irq <= IRQ_EINT(27); irq++) {
  351. irq_set_chip_and_handler(irq, &s3c_irq_eint, handle_level_irq);
  352. irq_set_chip_data(irq, (void *)eint_irq_to_bit(irq));
  353. set_irq_flags(irq, IRQF_VALID);
  354. }
  355. irq_set_chained_handler(IRQ_EINT0_3, s3c_irq_demux_eint0_3);
  356. irq_set_chained_handler(IRQ_EINT4_11, s3c_irq_demux_eint4_11);
  357. irq_set_chained_handler(IRQ_EINT12_19, s3c_irq_demux_eint12_19);
  358. irq_set_chained_handler(IRQ_EINT20_27, s3c_irq_demux_eint20_27);
  359. return 0;
  360. }
  361. arch_initcall(s3c64xx_init_irq_eint);
  362. void s3c64xx_restart(enum reboot_mode mode, const char *cmd)
  363. {
  364. if (mode != REBOOT_SOFT)
  365. samsung_wdt_reset();
  366. /* if all else fails, or mode was for soft, jump to 0 */
  367. soft_restart(0);
  368. }
  369. void __init s3c64xx_init_late(void)
  370. {
  371. s3c64xx_pm_late_initcall();
  372. }