core.c 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374
  1. /*
  2. * arch/arm/mach-ep93xx/core.c
  3. * Core routines for Cirrus EP93xx chips.
  4. *
  5. * Copyright (C) 2006 Lennert Buytenhek <buytenh@wantstofly.org>
  6. *
  7. * Thanks go to Michael Burian and Ray Lehtiniemi for their key
  8. * role in the ep93xx linux community.
  9. *
  10. * This program is free software; you can redistribute it and/or modify
  11. * it under the terms of the GNU General Public License as published by
  12. * the Free Software Foundation; either version 2 of the License, or (at
  13. * your option) any later version.
  14. */
  15. #include <linux/config.h>
  16. #include <linux/kernel.h>
  17. #include <linux/init.h>
  18. #include <linux/spinlock.h>
  19. #include <linux/sched.h>
  20. #include <linux/interrupt.h>
  21. #include <linux/serial.h>
  22. #include <linux/tty.h>
  23. #include <linux/bitops.h>
  24. #include <linux/serial.h>
  25. #include <linux/serial_8250.h>
  26. #include <linux/serial_core.h>
  27. #include <linux/device.h>
  28. #include <linux/mm.h>
  29. #include <linux/time.h>
  30. #include <linux/timex.h>
  31. #include <linux/delay.h>
  32. #include <linux/amba/bus.h>
  33. #include <asm/types.h>
  34. #include <asm/setup.h>
  35. #include <asm/memory.h>
  36. #include <asm/hardware.h>
  37. #include <asm/irq.h>
  38. #include <asm/system.h>
  39. #include <asm/tlbflush.h>
  40. #include <asm/pgtable.h>
  41. #include <asm/io.h>
  42. #include <asm/mach/map.h>
  43. #include <asm/mach/time.h>
  44. #include <asm/mach/irq.h>
  45. #include <asm/arch/gpio.h>
  46. #include <asm/hardware/vic.h>
  47. /*************************************************************************
  48. * Static I/O mappings that are needed for all EP93xx platforms
  49. *************************************************************************/
  50. static struct map_desc ep93xx_io_desc[] __initdata = {
  51. {
  52. .virtual = EP93XX_AHB_VIRT_BASE,
  53. .pfn = __phys_to_pfn(EP93XX_AHB_PHYS_BASE),
  54. .length = EP93XX_AHB_SIZE,
  55. .type = MT_DEVICE,
  56. }, {
  57. .virtual = EP93XX_APB_VIRT_BASE,
  58. .pfn = __phys_to_pfn(EP93XX_APB_PHYS_BASE),
  59. .length = EP93XX_APB_SIZE,
  60. .type = MT_DEVICE,
  61. },
  62. };
  63. void __init ep93xx_map_io(void)
  64. {
  65. iotable_init(ep93xx_io_desc, ARRAY_SIZE(ep93xx_io_desc));
  66. }
  67. /*************************************************************************
  68. * Timer handling for EP93xx
  69. *************************************************************************
  70. * The ep93xx has four internal timers. Timers 1, 2 (both 16 bit) and
  71. * 3 (32 bit) count down at 508 kHz, are self-reloading, and can generate
  72. * an interrupt on underflow. Timer 4 (40 bit) counts down at 983.04 kHz,
  73. * is free-running, and can't generate interrupts.
  74. *
  75. * The 508 kHz timers are ideal for use for the timer interrupt, as the
  76. * most common values of HZ divide 508 kHz nicely. We pick one of the 16
  77. * bit timers (timer 1) since we don't need more than 16 bits of reload
  78. * value as long as HZ >= 8.
  79. *
  80. * The higher clock rate of timer 4 makes it a better choice than the
  81. * other timers for use in gettimeoffset(), while the fact that it can't
  82. * generate interrupts means we don't have to worry about not being able
  83. * to use this timer for something else. We also use timer 4 for keeping
  84. * track of lost jiffies.
  85. */
  86. static unsigned int last_jiffy_time;
  87. #define TIMER4_TICKS_PER_JIFFY ((CLOCK_TICK_RATE + (HZ/2)) / HZ)
  88. static int ep93xx_timer_interrupt(int irq, void *dev_id, struct pt_regs *regs)
  89. {
  90. write_seqlock(&xtime_lock);
  91. __raw_writel(1, EP93XX_TIMER1_CLEAR);
  92. while (__raw_readl(EP93XX_TIMER4_VALUE_LOW) - last_jiffy_time
  93. >= TIMER4_TICKS_PER_JIFFY) {
  94. last_jiffy_time += TIMER4_TICKS_PER_JIFFY;
  95. timer_tick(regs);
  96. }
  97. write_sequnlock(&xtime_lock);
  98. return IRQ_HANDLED;
  99. }
  100. static struct irqaction ep93xx_timer_irq = {
  101. .name = "ep93xx timer",
  102. .flags = SA_INTERRUPT | SA_TIMER,
  103. .handler = ep93xx_timer_interrupt,
  104. };
  105. static void __init ep93xx_timer_init(void)
  106. {
  107. /* Enable periodic HZ timer. */
  108. __raw_writel(0x48, EP93XX_TIMER1_CONTROL);
  109. __raw_writel((508000 / HZ) - 1, EP93XX_TIMER1_LOAD);
  110. __raw_writel(0xc8, EP93XX_TIMER1_CONTROL);
  111. /* Enable lost jiffy timer. */
  112. __raw_writel(0x100, EP93XX_TIMER4_VALUE_HIGH);
  113. setup_irq(IRQ_EP93XX_TIMER1, &ep93xx_timer_irq);
  114. }
  115. static unsigned long ep93xx_gettimeoffset(void)
  116. {
  117. int offset;
  118. offset = __raw_readl(EP93XX_TIMER4_VALUE_LOW) - last_jiffy_time;
  119. /* Calculate (1000000 / 983040) * offset. */
  120. return offset + (53 * offset / 3072);
  121. }
  122. struct sys_timer ep93xx_timer = {
  123. .init = ep93xx_timer_init,
  124. .offset = ep93xx_gettimeoffset,
  125. };
  126. /*************************************************************************
  127. * GPIO handling for EP93xx
  128. *************************************************************************/
  129. static unsigned char gpio_int_enable[2];
  130. static unsigned char gpio_int_type1[2];
  131. static unsigned char gpio_int_type2[2];
  132. static void update_gpio_ab_int_params(int port)
  133. {
  134. if (port == 0) {
  135. __raw_writeb(0, EP93XX_GPIO_A_INT_ENABLE);
  136. __raw_writeb(gpio_int_type2[0], EP93XX_GPIO_A_INT_TYPE2);
  137. __raw_writeb(gpio_int_type1[0], EP93XX_GPIO_A_INT_TYPE1);
  138. __raw_writeb(gpio_int_enable[0], EP93XX_GPIO_A_INT_ENABLE);
  139. } else if (port == 1) {
  140. __raw_writeb(0, EP93XX_GPIO_B_INT_ENABLE);
  141. __raw_writeb(gpio_int_type2[1], EP93XX_GPIO_B_INT_TYPE2);
  142. __raw_writeb(gpio_int_type1[1], EP93XX_GPIO_B_INT_TYPE1);
  143. __raw_writeb(gpio_int_enable[1], EP93XX_GPIO_B_INT_ENABLE);
  144. }
  145. }
  146. static unsigned char data_register_offset[8] = {
  147. 0x00, 0x04, 0x08, 0x0c, 0x20, 0x30, 0x38, 0x40,
  148. };
  149. static unsigned char data_direction_register_offset[8] = {
  150. 0x10, 0x14, 0x18, 0x1c, 0x24, 0x34, 0x3c, 0x44,
  151. };
  152. void gpio_line_config(int line, int direction)
  153. {
  154. unsigned int data_direction_register;
  155. unsigned long flags;
  156. unsigned char v;
  157. data_direction_register =
  158. EP93XX_GPIO_REG(data_direction_register_offset[line >> 3]);
  159. local_irq_save(flags);
  160. if (direction == GPIO_OUT) {
  161. if (line >= 0 && line < 16) {
  162. gpio_int_enable[line >> 3] &= ~(1 << (line & 7));
  163. update_gpio_ab_int_params(line >> 3);
  164. }
  165. v = __raw_readb(data_direction_register);
  166. v |= 1 << (line & 7);
  167. __raw_writeb(v, data_direction_register);
  168. } else if (direction == GPIO_IN) {
  169. v = __raw_readb(data_direction_register);
  170. v &= ~(1 << (line & 7));
  171. __raw_writeb(v, data_direction_register);
  172. }
  173. local_irq_restore(flags);
  174. }
  175. EXPORT_SYMBOL(gpio_line_config);
  176. int gpio_line_get(int line)
  177. {
  178. unsigned int data_register;
  179. data_register = EP93XX_GPIO_REG(data_register_offset[line >> 3]);
  180. return !!(__raw_readb(data_register) & (1 << (line & 7)));
  181. }
  182. EXPORT_SYMBOL(gpio_line_get);
  183. void gpio_line_set(int line, int value)
  184. {
  185. unsigned int data_register;
  186. unsigned long flags;
  187. unsigned char v;
  188. data_register = EP93XX_GPIO_REG(data_register_offset[line >> 3]);
  189. local_irq_save(flags);
  190. if (value == EP93XX_GPIO_HIGH) {
  191. v = __raw_readb(data_register);
  192. v |= 1 << (line & 7);
  193. __raw_writeb(v, data_register);
  194. } else if (value == EP93XX_GPIO_LOW) {
  195. v = __raw_readb(data_register);
  196. v &= ~(1 << (line & 7));
  197. __raw_writeb(v, data_register);
  198. }
  199. local_irq_restore(flags);
  200. }
  201. EXPORT_SYMBOL(gpio_line_set);
  202. /*************************************************************************
  203. * EP93xx IRQ handling
  204. *************************************************************************/
  205. static void ep93xx_gpio_ab_irq_handler(unsigned int irq,
  206. struct irqdesc *desc, struct pt_regs *regs)
  207. {
  208. unsigned char status;
  209. int i;
  210. status = __raw_readb(EP93XX_GPIO_A_INT_STATUS);
  211. for (i = 0; i < 8; i++) {
  212. if (status & (1 << i)) {
  213. desc = irq_desc + IRQ_EP93XX_GPIO(0) + i;
  214. desc_handle_irq(IRQ_EP93XX_GPIO(0) + i, desc, regs);
  215. }
  216. }
  217. status = __raw_readb(EP93XX_GPIO_B_INT_STATUS);
  218. for (i = 0; i < 8; i++) {
  219. if (status & (1 << i)) {
  220. desc = irq_desc + IRQ_EP93XX_GPIO(8) + i;
  221. desc_handle_irq(IRQ_EP93XX_GPIO(8) + i, desc, regs);
  222. }
  223. }
  224. }
  225. static void ep93xx_gpio_ab_irq_mask_ack(unsigned int irq)
  226. {
  227. int line = irq - IRQ_EP93XX_GPIO(0);
  228. int port = line >> 3;
  229. gpio_int_enable[port] &= ~(1 << (line & 7));
  230. update_gpio_ab_int_params(port);
  231. if (line >> 3) {
  232. __raw_writel(1 << (line & 7), EP93XX_GPIO_B_INT_ACK);
  233. } else {
  234. __raw_writel(1 << (line & 7), EP93XX_GPIO_A_INT_ACK);
  235. }
  236. }
  237. static void ep93xx_gpio_ab_irq_mask(unsigned int irq)
  238. {
  239. int line = irq - IRQ_EP93XX_GPIO(0);
  240. int port = line >> 3;
  241. gpio_int_enable[port] &= ~(1 << (line & 7));
  242. update_gpio_ab_int_params(port);
  243. }
  244. static void ep93xx_gpio_ab_irq_unmask(unsigned int irq)
  245. {
  246. int line = irq - IRQ_EP93XX_GPIO(0);
  247. int port = line >> 3;
  248. gpio_int_enable[port] |= 1 << (line & 7);
  249. update_gpio_ab_int_params(port);
  250. }
  251. /*
  252. * gpio_int_type1 controls whether the interrupt is level (0) or
  253. * edge (1) triggered, while gpio_int_type2 controls whether it
  254. * triggers on low/falling (0) or high/rising (1).
  255. */
  256. static int ep93xx_gpio_ab_irq_type(unsigned int irq, unsigned int type)
  257. {
  258. int port;
  259. int line;
  260. line = irq - IRQ_EP93XX_GPIO(0);
  261. gpio_line_config(line, GPIO_IN);
  262. port = line >> 3;
  263. line &= 7;
  264. if (type & IRQT_RISING) {
  265. gpio_int_type1[port] |= 1 << line;
  266. gpio_int_type2[port] |= 1 << line;
  267. } else if (type & IRQT_FALLING) {
  268. gpio_int_type1[port] |= 1 << line;
  269. gpio_int_type2[port] &= ~(1 << line);
  270. } else if (type & IRQT_HIGH) {
  271. gpio_int_type1[port] &= ~(1 << line);
  272. gpio_int_type2[port] |= 1 << line;
  273. } else if (type & IRQT_LOW) {
  274. gpio_int_type1[port] &= ~(1 << line);
  275. gpio_int_type2[port] &= ~(1 << line);
  276. }
  277. update_gpio_ab_int_params(port);
  278. return 0;
  279. }
  280. static struct irqchip ep93xx_gpio_ab_irq_chip = {
  281. .ack = ep93xx_gpio_ab_irq_mask_ack,
  282. .mask = ep93xx_gpio_ab_irq_mask,
  283. .unmask = ep93xx_gpio_ab_irq_unmask,
  284. .set_type = ep93xx_gpio_ab_irq_type,
  285. };
  286. void __init ep93xx_init_irq(void)
  287. {
  288. int irq;
  289. vic_init((void *)EP93XX_VIC1_BASE, 0, EP93XX_VIC1_VALID_IRQ_MASK);
  290. vic_init((void *)EP93XX_VIC2_BASE, 32, EP93XX_VIC2_VALID_IRQ_MASK);
  291. for (irq = IRQ_EP93XX_GPIO(0) ; irq <= IRQ_EP93XX_GPIO(15); irq++) {
  292. set_irq_chip(irq, &ep93xx_gpio_ab_irq_chip);
  293. set_irq_handler(irq, do_level_IRQ);
  294. set_irq_flags(irq, IRQF_VALID);
  295. }
  296. set_irq_chained_handler(IRQ_EP93XX_GPIO_AB, ep93xx_gpio_ab_irq_handler);
  297. }
  298. /*************************************************************************
  299. * EP93xx peripheral handling
  300. *************************************************************************/
  301. void __init ep93xx_init_devices(void)
  302. {
  303. unsigned int v;
  304. /*
  305. * Disallow access to MaverickCrunch initially.
  306. */
  307. v = __raw_readl(EP93XX_SYSCON_DEVICE_CONFIG);
  308. v &= ~EP93XX_SYSCON_DEVICE_CONFIG_CRUNCH_ENABLE;
  309. __raw_writel(0xaa, EP93XX_SYSCON_SWLOCK);
  310. __raw_writel(v, EP93XX_SYSCON_DEVICE_CONFIG);
  311. }