common.c 10 KB

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