common.c 10.0 KB

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