common.c 10 KB

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