common.c 8.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361
  1. /*
  2. * arch/arm/mach-ixp4xx/common.c
  3. *
  4. * Generic code shared across all IXP4XX platforms
  5. *
  6. * Maintainer: Deepak Saxena <dsaxena@plexity.net>
  7. *
  8. * Copyright 2002 (c) Intel Corporation
  9. * Copyright 2003-2004 (c) MontaVista, Software, Inc.
  10. *
  11. * This file is licensed under the terms of the GNU General Public
  12. * License version 2. This program is licensed "as is" without any
  13. * warranty of any kind, whether express or implied.
  14. */
  15. #include <linux/config.h>
  16. #include <linux/kernel.h>
  17. #include <linux/mm.h>
  18. #include <linux/init.h>
  19. #include <linux/serial.h>
  20. #include <linux/sched.h>
  21. #include <linux/tty.h>
  22. #include <linux/serial_core.h>
  23. #include <linux/bootmem.h>
  24. #include <linux/interrupt.h>
  25. #include <linux/bitops.h>
  26. #include <linux/time.h>
  27. #include <linux/timex.h>
  28. #include <asm/hardware.h>
  29. #include <asm/uaccess.h>
  30. #include <asm/io.h>
  31. #include <asm/pgtable.h>
  32. #include <asm/page.h>
  33. #include <asm/irq.h>
  34. #include <asm/mach/map.h>
  35. #include <asm/mach/irq.h>
  36. #include <asm/mach/time.h>
  37. enum ixp4xx_irq_type {
  38. IXP4XX_IRQ_LEVEL, IXP4XX_IRQ_EDGE
  39. };
  40. static void ixp4xx_config_irq(unsigned irq, enum ixp4xx_irq_type type);
  41. /*************************************************************************
  42. * GPIO acces functions
  43. *************************************************************************/
  44. /*
  45. * Configure GPIO line for input, interrupt, or output operation
  46. *
  47. * TODO: Enable/disable the irq_desc based on interrupt or output mode.
  48. * TODO: Should these be named ixp4xx_gpio_?
  49. */
  50. void gpio_line_config(u8 line, u32 style)
  51. {
  52. static const int gpio2irq[] = {
  53. 6, 7, 19, 20, 21, 22, 23, 24, 25, 26, 27, 28, 29
  54. };
  55. u32 enable;
  56. volatile u32 *int_reg;
  57. u32 int_style;
  58. enum ixp4xx_irq_type irq_type;
  59. enable = *IXP4XX_GPIO_GPOER;
  60. if (style & IXP4XX_GPIO_OUT) {
  61. enable &= ~((1) << line);
  62. } else if (style & IXP4XX_GPIO_IN) {
  63. enable |= ((1) << line);
  64. switch (style & IXP4XX_GPIO_INTSTYLE_MASK)
  65. {
  66. case (IXP4XX_GPIO_ACTIVE_HIGH):
  67. int_style = IXP4XX_GPIO_STYLE_ACTIVE_HIGH;
  68. irq_type = IXP4XX_IRQ_LEVEL;
  69. break;
  70. case (IXP4XX_GPIO_ACTIVE_LOW):
  71. int_style = IXP4XX_GPIO_STYLE_ACTIVE_LOW;
  72. irq_type = IXP4XX_IRQ_LEVEL;
  73. break;
  74. case (IXP4XX_GPIO_RISING_EDGE):
  75. int_style = IXP4XX_GPIO_STYLE_RISING_EDGE;
  76. irq_type = IXP4XX_IRQ_EDGE;
  77. break;
  78. case (IXP4XX_GPIO_FALLING_EDGE):
  79. int_style = IXP4XX_GPIO_STYLE_FALLING_EDGE;
  80. irq_type = IXP4XX_IRQ_EDGE;
  81. break;
  82. case (IXP4XX_GPIO_TRANSITIONAL):
  83. int_style = IXP4XX_GPIO_STYLE_TRANSITIONAL;
  84. irq_type = IXP4XX_IRQ_EDGE;
  85. break;
  86. default:
  87. int_style = IXP4XX_GPIO_STYLE_ACTIVE_HIGH;
  88. irq_type = IXP4XX_IRQ_LEVEL;
  89. break;
  90. }
  91. if (style & IXP4XX_GPIO_INTSTYLE_MASK)
  92. ixp4xx_config_irq(gpio2irq[line], irq_type);
  93. if (line >= 8) { /* pins 8-15 */
  94. line -= 8;
  95. int_reg = IXP4XX_GPIO_GPIT2R;
  96. }
  97. else { /* pins 0-7 */
  98. int_reg = IXP4XX_GPIO_GPIT1R;
  99. }
  100. /* Clear the style for the appropriate pin */
  101. *int_reg &= ~(IXP4XX_GPIO_STYLE_CLEAR <<
  102. (line * IXP4XX_GPIO_STYLE_SIZE));
  103. /* Set the new style */
  104. *int_reg |= (int_style << (line * IXP4XX_GPIO_STYLE_SIZE));
  105. }
  106. *IXP4XX_GPIO_GPOER = enable;
  107. }
  108. EXPORT_SYMBOL(gpio_line_config);
  109. /*************************************************************************
  110. * IXP4xx chipset I/O mapping
  111. *************************************************************************/
  112. static struct map_desc ixp4xx_io_desc[] __initdata = {
  113. { /* UART, Interrupt ctrl, GPIO, timers, NPEs, MACs, USB .... */
  114. .virtual = IXP4XX_PERIPHERAL_BASE_VIRT,
  115. .physical = IXP4XX_PERIPHERAL_BASE_PHYS,
  116. .length = IXP4XX_PERIPHERAL_REGION_SIZE,
  117. .type = MT_DEVICE
  118. }, { /* Expansion Bus Config Registers */
  119. .virtual = IXP4XX_EXP_CFG_BASE_VIRT,
  120. .physical = IXP4XX_EXP_CFG_BASE_PHYS,
  121. .length = IXP4XX_EXP_CFG_REGION_SIZE,
  122. .type = MT_DEVICE
  123. }, { /* PCI Registers */
  124. .virtual = IXP4XX_PCI_CFG_BASE_VIRT,
  125. .physical = IXP4XX_PCI_CFG_BASE_PHYS,
  126. .length = IXP4XX_PCI_CFG_REGION_SIZE,
  127. .type = MT_DEVICE
  128. },
  129. #ifdef CONFIG_DEBUG_LL
  130. { /* Debug UART mapping */
  131. .virtual = IXP4XX_DEBUG_UART_BASE_VIRT,
  132. .physical = IXP4XX_DEBUG_UART_BASE_PHYS,
  133. .length = IXP4XX_DEBUG_UART_REGION_SIZE,
  134. .type = MT_DEVICE
  135. }
  136. #endif
  137. };
  138. void __init ixp4xx_map_io(void)
  139. {
  140. iotable_init(ixp4xx_io_desc, ARRAY_SIZE(ixp4xx_io_desc));
  141. }
  142. /*************************************************************************
  143. * IXP4xx chipset IRQ handling
  144. *
  145. * TODO: GPIO IRQs should be marked invalid until the user of the IRQ
  146. * (be it PCI or something else) configures that GPIO line
  147. * as an IRQ.
  148. **************************************************************************/
  149. static void ixp4xx_irq_mask(unsigned int irq)
  150. {
  151. if (cpu_is_ixp46x() && irq >= 32)
  152. *IXP4XX_ICMR2 &= ~(1 << (irq - 32));
  153. else
  154. *IXP4XX_ICMR &= ~(1 << irq);
  155. }
  156. static void ixp4xx_irq_unmask(unsigned int irq)
  157. {
  158. if (cpu_is_ixp46x() && irq >= 32)
  159. *IXP4XX_ICMR2 |= (1 << (irq - 32));
  160. else
  161. *IXP4XX_ICMR |= (1 << irq);
  162. }
  163. static void ixp4xx_irq_ack(unsigned int irq)
  164. {
  165. static int irq2gpio[32] = {
  166. -1, -1, -1, -1, -1, -1, 0, 1,
  167. -1, -1, -1, -1, -1, -1, -1, -1,
  168. -1, -1, -1, 2, 3, 4, 5, 6,
  169. 7, 8, 9, 10, 11, 12, -1, -1,
  170. };
  171. int line = (irq < 32) ? irq2gpio[irq] : -1;
  172. if (line >= 0)
  173. gpio_line_isr_clear(line);
  174. }
  175. /*
  176. * Level triggered interrupts on GPIO lines can only be cleared when the
  177. * interrupt condition disappears.
  178. */
  179. static void ixp4xx_irq_level_unmask(unsigned int irq)
  180. {
  181. ixp4xx_irq_ack(irq);
  182. ixp4xx_irq_unmask(irq);
  183. }
  184. static struct irqchip ixp4xx_irq_level_chip = {
  185. .ack = ixp4xx_irq_mask,
  186. .mask = ixp4xx_irq_mask,
  187. .unmask = ixp4xx_irq_level_unmask,
  188. };
  189. static struct irqchip ixp4xx_irq_edge_chip = {
  190. .ack = ixp4xx_irq_ack,
  191. .mask = ixp4xx_irq_mask,
  192. .unmask = ixp4xx_irq_unmask,
  193. };
  194. static void ixp4xx_config_irq(unsigned irq, enum ixp4xx_irq_type type)
  195. {
  196. switch (type) {
  197. case IXP4XX_IRQ_LEVEL:
  198. set_irq_chip(irq, &ixp4xx_irq_level_chip);
  199. set_irq_handler(irq, do_level_IRQ);
  200. break;
  201. case IXP4XX_IRQ_EDGE:
  202. set_irq_chip(irq, &ixp4xx_irq_edge_chip);
  203. set_irq_handler(irq, do_edge_IRQ);
  204. break;
  205. }
  206. set_irq_flags(irq, IRQF_VALID);
  207. }
  208. void __init ixp4xx_init_irq(void)
  209. {
  210. int i = 0;
  211. /* Route all sources to IRQ instead of FIQ */
  212. *IXP4XX_ICLR = 0x0;
  213. /* Disable all interrupt */
  214. *IXP4XX_ICMR = 0x0;
  215. if (cpu_is_ixp46x()) {
  216. /* Route upper 32 sources to IRQ instead of FIQ */
  217. *IXP4XX_ICLR2 = 0x00;
  218. /* Disable upper 32 interrupts */
  219. *IXP4XX_ICMR2 = 0x00;
  220. }
  221. /* Default to all level triggered */
  222. for(i = 0; i < NR_IRQS; i++)
  223. ixp4xx_config_irq(i, IXP4XX_IRQ_LEVEL);
  224. }
  225. /*************************************************************************
  226. * IXP4xx timer tick
  227. * We use OS timer1 on the CPU for the timer tick and the timestamp
  228. * counter as a source of real clock ticks to account for missed jiffies.
  229. *************************************************************************/
  230. static unsigned volatile last_jiffy_time;
  231. #define CLOCK_TICKS_PER_USEC ((CLOCK_TICK_RATE + USEC_PER_SEC/2) / USEC_PER_SEC)
  232. /* IRQs are disabled before entering here from do_gettimeofday() */
  233. static unsigned long ixp4xx_gettimeoffset(void)
  234. {
  235. u32 elapsed;
  236. elapsed = *IXP4XX_OSTS - last_jiffy_time;
  237. return elapsed / CLOCK_TICKS_PER_USEC;
  238. }
  239. static irqreturn_t ixp4xx_timer_interrupt(int irq, void *dev_id, struct pt_regs *regs)
  240. {
  241. write_seqlock(&xtime_lock);
  242. /* Clear Pending Interrupt by writing '1' to it */
  243. *IXP4XX_OSST = IXP4XX_OSST_TIMER_1_PEND;
  244. /*
  245. * Catch up with the real idea of time
  246. */
  247. while ((*IXP4XX_OSTS - last_jiffy_time) > LATCH) {
  248. timer_tick(regs);
  249. last_jiffy_time += LATCH;
  250. }
  251. write_sequnlock(&xtime_lock);
  252. return IRQ_HANDLED;
  253. }
  254. static struct irqaction ixp4xx_timer_irq = {
  255. .name = "IXP4xx Timer Tick",
  256. .flags = SA_INTERRUPT | SA_TIMER,
  257. .handler = ixp4xx_timer_interrupt,
  258. };
  259. static void __init ixp4xx_timer_init(void)
  260. {
  261. /* Clear Pending Interrupt by writing '1' to it */
  262. *IXP4XX_OSST = IXP4XX_OSST_TIMER_1_PEND;
  263. /* Setup the Timer counter value */
  264. *IXP4XX_OSRT1 = (LATCH & ~IXP4XX_OST_RELOAD_MASK) | IXP4XX_OST_ENABLE;
  265. /* Reset time-stamp counter */
  266. *IXP4XX_OSTS = 0;
  267. last_jiffy_time = 0;
  268. /* Connect the interrupt handler and enable the interrupt */
  269. setup_irq(IRQ_IXP4XX_TIMER1, &ixp4xx_timer_irq);
  270. }
  271. struct sys_timer ixp4xx_timer = {
  272. .init = ixp4xx_timer_init,
  273. .offset = ixp4xx_gettimeoffset,
  274. };
  275. static struct resource ixp46x_i2c_resources[] = {
  276. [0] = {
  277. .start = 0xc8011000,
  278. .end = 0xc801101c,
  279. .flags = IORESOURCE_MEM,
  280. },
  281. [1] = {
  282. .start = IRQ_IXP4XX_I2C,
  283. .end = IRQ_IXP4XX_I2C,
  284. .flags = IORESOURCE_IRQ
  285. }
  286. };
  287. /*
  288. * I2C controller. The IXP46x uses the same block as the IOP3xx, so
  289. * we just use the same device name.
  290. */
  291. static struct platform_device ixp46x_i2c_controller = {
  292. .name = "IOP3xx-I2C",
  293. .id = 0,
  294. .num_resources = 2,
  295. .resource = ixp46x_i2c_resources
  296. };
  297. static struct platform_device *ixp46x_devices[] __initdata = {
  298. &ixp46x_i2c_controller
  299. };
  300. void __init ixp4xx_sys_init(void)
  301. {
  302. if (cpu_is_ixp46x()) {
  303. platform_add_devices(ixp46x_devices,
  304. ARRAY_SIZE(ixp46x_devices));
  305. }
  306. }