core.c 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452
  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/termios.h>
  33. #include <linux/amba/bus.h>
  34. #include <linux/amba/serial.h>
  35. #include <asm/types.h>
  36. #include <asm/setup.h>
  37. #include <asm/memory.h>
  38. #include <asm/hardware.h>
  39. #include <asm/irq.h>
  40. #include <asm/system.h>
  41. #include <asm/tlbflush.h>
  42. #include <asm/pgtable.h>
  43. #include <asm/io.h>
  44. #include <asm/mach/map.h>
  45. #include <asm/mach/time.h>
  46. #include <asm/mach/irq.h>
  47. #include <asm/arch/gpio.h>
  48. #include <asm/hardware/vic.h>
  49. /*************************************************************************
  50. * Static I/O mappings that are needed for all EP93xx platforms
  51. *************************************************************************/
  52. static struct map_desc ep93xx_io_desc[] __initdata = {
  53. {
  54. .virtual = EP93XX_AHB_VIRT_BASE,
  55. .pfn = __phys_to_pfn(EP93XX_AHB_PHYS_BASE),
  56. .length = EP93XX_AHB_SIZE,
  57. .type = MT_DEVICE,
  58. }, {
  59. .virtual = EP93XX_APB_VIRT_BASE,
  60. .pfn = __phys_to_pfn(EP93XX_APB_PHYS_BASE),
  61. .length = EP93XX_APB_SIZE,
  62. .type = MT_DEVICE,
  63. },
  64. };
  65. void __init ep93xx_map_io(void)
  66. {
  67. iotable_init(ep93xx_io_desc, ARRAY_SIZE(ep93xx_io_desc));
  68. }
  69. /*************************************************************************
  70. * Timer handling for EP93xx
  71. *************************************************************************
  72. * The ep93xx has four internal timers. Timers 1, 2 (both 16 bit) and
  73. * 3 (32 bit) count down at 508 kHz, are self-reloading, and can generate
  74. * an interrupt on underflow. Timer 4 (40 bit) counts down at 983.04 kHz,
  75. * is free-running, and can't generate interrupts.
  76. *
  77. * The 508 kHz timers are ideal for use for the timer interrupt, as the
  78. * most common values of HZ divide 508 kHz nicely. We pick one of the 16
  79. * bit timers (timer 1) since we don't need more than 16 bits of reload
  80. * value as long as HZ >= 8.
  81. *
  82. * The higher clock rate of timer 4 makes it a better choice than the
  83. * other timers for use in gettimeoffset(), while the fact that it can't
  84. * generate interrupts means we don't have to worry about not being able
  85. * to use this timer for something else. We also use timer 4 for keeping
  86. * track of lost jiffies.
  87. */
  88. static unsigned int last_jiffy_time;
  89. #define TIMER4_TICKS_PER_JIFFY ((CLOCK_TICK_RATE + (HZ/2)) / HZ)
  90. static int ep93xx_timer_interrupt(int irq, void *dev_id, struct pt_regs *regs)
  91. {
  92. write_seqlock(&xtime_lock);
  93. __raw_writel(1, EP93XX_TIMER1_CLEAR);
  94. while (__raw_readl(EP93XX_TIMER4_VALUE_LOW) - last_jiffy_time
  95. >= TIMER4_TICKS_PER_JIFFY) {
  96. last_jiffy_time += TIMER4_TICKS_PER_JIFFY;
  97. timer_tick(regs);
  98. }
  99. write_sequnlock(&xtime_lock);
  100. return IRQ_HANDLED;
  101. }
  102. static struct irqaction ep93xx_timer_irq = {
  103. .name = "ep93xx timer",
  104. .flags = SA_INTERRUPT | SA_TIMER,
  105. .handler = ep93xx_timer_interrupt,
  106. };
  107. static void __init ep93xx_timer_init(void)
  108. {
  109. /* Enable periodic HZ timer. */
  110. __raw_writel(0x48, EP93XX_TIMER1_CONTROL);
  111. __raw_writel((508000 / HZ) - 1, EP93XX_TIMER1_LOAD);
  112. __raw_writel(0xc8, EP93XX_TIMER1_CONTROL);
  113. /* Enable lost jiffy timer. */
  114. __raw_writel(0x100, EP93XX_TIMER4_VALUE_HIGH);
  115. setup_irq(IRQ_EP93XX_TIMER1, &ep93xx_timer_irq);
  116. }
  117. static unsigned long ep93xx_gettimeoffset(void)
  118. {
  119. int offset;
  120. offset = __raw_readl(EP93XX_TIMER4_VALUE_LOW) - last_jiffy_time;
  121. /* Calculate (1000000 / 983040) * offset. */
  122. return offset + (53 * offset / 3072);
  123. }
  124. struct sys_timer ep93xx_timer = {
  125. .init = ep93xx_timer_init,
  126. .offset = ep93xx_gettimeoffset,
  127. };
  128. /*************************************************************************
  129. * GPIO handling for EP93xx
  130. *************************************************************************/
  131. static unsigned char gpio_int_enable[2];
  132. static unsigned char gpio_int_type1[2];
  133. static unsigned char gpio_int_type2[2];
  134. static void update_gpio_ab_int_params(int port)
  135. {
  136. if (port == 0) {
  137. __raw_writeb(0, EP93XX_GPIO_A_INT_ENABLE);
  138. __raw_writeb(gpio_int_type2[0], EP93XX_GPIO_A_INT_TYPE2);
  139. __raw_writeb(gpio_int_type1[0], EP93XX_GPIO_A_INT_TYPE1);
  140. __raw_writeb(gpio_int_enable[0], EP93XX_GPIO_A_INT_ENABLE);
  141. } else if (port == 1) {
  142. __raw_writeb(0, EP93XX_GPIO_B_INT_ENABLE);
  143. __raw_writeb(gpio_int_type2[1], EP93XX_GPIO_B_INT_TYPE2);
  144. __raw_writeb(gpio_int_type1[1], EP93XX_GPIO_B_INT_TYPE1);
  145. __raw_writeb(gpio_int_enable[1], EP93XX_GPIO_B_INT_ENABLE);
  146. }
  147. }
  148. static unsigned char data_register_offset[8] = {
  149. 0x00, 0x04, 0x08, 0x0c, 0x20, 0x30, 0x38, 0x40,
  150. };
  151. static unsigned char data_direction_register_offset[8] = {
  152. 0x10, 0x14, 0x18, 0x1c, 0x24, 0x34, 0x3c, 0x44,
  153. };
  154. void gpio_line_config(int line, int direction)
  155. {
  156. unsigned int data_direction_register;
  157. unsigned long flags;
  158. unsigned char v;
  159. data_direction_register =
  160. EP93XX_GPIO_REG(data_direction_register_offset[line >> 3]);
  161. local_irq_save(flags);
  162. if (direction == GPIO_OUT) {
  163. if (line >= 0 && line < 16) {
  164. gpio_int_enable[line >> 3] &= ~(1 << (line & 7));
  165. update_gpio_ab_int_params(line >> 3);
  166. }
  167. v = __raw_readb(data_direction_register);
  168. v |= 1 << (line & 7);
  169. __raw_writeb(v, data_direction_register);
  170. } else if (direction == GPIO_IN) {
  171. v = __raw_readb(data_direction_register);
  172. v &= ~(1 << (line & 7));
  173. __raw_writeb(v, data_direction_register);
  174. }
  175. local_irq_restore(flags);
  176. }
  177. EXPORT_SYMBOL(gpio_line_config);
  178. int gpio_line_get(int line)
  179. {
  180. unsigned int data_register;
  181. data_register = EP93XX_GPIO_REG(data_register_offset[line >> 3]);
  182. return !!(__raw_readb(data_register) & (1 << (line & 7)));
  183. }
  184. EXPORT_SYMBOL(gpio_line_get);
  185. void gpio_line_set(int line, int value)
  186. {
  187. unsigned int data_register;
  188. unsigned long flags;
  189. unsigned char v;
  190. data_register = EP93XX_GPIO_REG(data_register_offset[line >> 3]);
  191. local_irq_save(flags);
  192. if (value == EP93XX_GPIO_HIGH) {
  193. v = __raw_readb(data_register);
  194. v |= 1 << (line & 7);
  195. __raw_writeb(v, data_register);
  196. } else if (value == EP93XX_GPIO_LOW) {
  197. v = __raw_readb(data_register);
  198. v &= ~(1 << (line & 7));
  199. __raw_writeb(v, data_register);
  200. }
  201. local_irq_restore(flags);
  202. }
  203. EXPORT_SYMBOL(gpio_line_set);
  204. /*************************************************************************
  205. * EP93xx IRQ handling
  206. *************************************************************************/
  207. static void ep93xx_gpio_ab_irq_handler(unsigned int irq,
  208. struct irqdesc *desc, struct pt_regs *regs)
  209. {
  210. unsigned char status;
  211. int i;
  212. status = __raw_readb(EP93XX_GPIO_A_INT_STATUS);
  213. for (i = 0; i < 8; i++) {
  214. if (status & (1 << i)) {
  215. desc = irq_desc + IRQ_EP93XX_GPIO(0) + i;
  216. desc_handle_irq(IRQ_EP93XX_GPIO(0) + i, desc, regs);
  217. }
  218. }
  219. status = __raw_readb(EP93XX_GPIO_B_INT_STATUS);
  220. for (i = 0; i < 8; i++) {
  221. if (status & (1 << i)) {
  222. desc = irq_desc + IRQ_EP93XX_GPIO(8) + i;
  223. desc_handle_irq(IRQ_EP93XX_GPIO(8) + i, desc, regs);
  224. }
  225. }
  226. }
  227. static void ep93xx_gpio_ab_irq_mask_ack(unsigned int irq)
  228. {
  229. int line = irq - IRQ_EP93XX_GPIO(0);
  230. int port = line >> 3;
  231. gpio_int_enable[port] &= ~(1 << (line & 7));
  232. update_gpio_ab_int_params(port);
  233. if (line >> 3) {
  234. __raw_writel(1 << (line & 7), EP93XX_GPIO_B_INT_ACK);
  235. } else {
  236. __raw_writel(1 << (line & 7), EP93XX_GPIO_A_INT_ACK);
  237. }
  238. }
  239. static void ep93xx_gpio_ab_irq_mask(unsigned int irq)
  240. {
  241. int line = irq - IRQ_EP93XX_GPIO(0);
  242. int port = line >> 3;
  243. gpio_int_enable[port] &= ~(1 << (line & 7));
  244. update_gpio_ab_int_params(port);
  245. }
  246. static void ep93xx_gpio_ab_irq_unmask(unsigned int irq)
  247. {
  248. int line = irq - IRQ_EP93XX_GPIO(0);
  249. int port = line >> 3;
  250. gpio_int_enable[port] |= 1 << (line & 7);
  251. update_gpio_ab_int_params(port);
  252. }
  253. /*
  254. * gpio_int_type1 controls whether the interrupt is level (0) or
  255. * edge (1) triggered, while gpio_int_type2 controls whether it
  256. * triggers on low/falling (0) or high/rising (1).
  257. */
  258. static int ep93xx_gpio_ab_irq_type(unsigned int irq, unsigned int type)
  259. {
  260. int port;
  261. int line;
  262. line = irq - IRQ_EP93XX_GPIO(0);
  263. gpio_line_config(line, GPIO_IN);
  264. port = line >> 3;
  265. line &= 7;
  266. if (type & IRQT_RISING) {
  267. gpio_int_type1[port] |= 1 << line;
  268. gpio_int_type2[port] |= 1 << line;
  269. } else if (type & IRQT_FALLING) {
  270. gpio_int_type1[port] |= 1 << line;
  271. gpio_int_type2[port] &= ~(1 << line);
  272. } else if (type & IRQT_HIGH) {
  273. gpio_int_type1[port] &= ~(1 << line);
  274. gpio_int_type2[port] |= 1 << line;
  275. } else if (type & IRQT_LOW) {
  276. gpio_int_type1[port] &= ~(1 << line);
  277. gpio_int_type2[port] &= ~(1 << line);
  278. }
  279. update_gpio_ab_int_params(port);
  280. return 0;
  281. }
  282. static struct irqchip ep93xx_gpio_ab_irq_chip = {
  283. .ack = ep93xx_gpio_ab_irq_mask_ack,
  284. .mask = ep93xx_gpio_ab_irq_mask,
  285. .unmask = ep93xx_gpio_ab_irq_unmask,
  286. .set_type = ep93xx_gpio_ab_irq_type,
  287. };
  288. void __init ep93xx_init_irq(void)
  289. {
  290. int irq;
  291. vic_init((void *)EP93XX_VIC1_BASE, 0, EP93XX_VIC1_VALID_IRQ_MASK);
  292. vic_init((void *)EP93XX_VIC2_BASE, 32, EP93XX_VIC2_VALID_IRQ_MASK);
  293. for (irq = IRQ_EP93XX_GPIO(0) ; irq <= IRQ_EP93XX_GPIO(15); irq++) {
  294. set_irq_chip(irq, &ep93xx_gpio_ab_irq_chip);
  295. set_irq_handler(irq, do_level_IRQ);
  296. set_irq_flags(irq, IRQF_VALID);
  297. }
  298. set_irq_chained_handler(IRQ_EP93XX_GPIO_AB, ep93xx_gpio_ab_irq_handler);
  299. }
  300. /*************************************************************************
  301. * EP93xx peripheral handling
  302. *************************************************************************/
  303. #define EP93XX_UART_MCR_OFFSET (0x0100)
  304. static void ep93xx_uart_set_mctrl(struct amba_device *dev,
  305. void __iomem *base, unsigned int mctrl)
  306. {
  307. unsigned int mcr;
  308. mcr = 0;
  309. if (!(mctrl & TIOCM_RTS))
  310. mcr |= 2;
  311. if (!(mctrl & TIOCM_DTR))
  312. mcr |= 1;
  313. __raw_writel(mcr, base + EP93XX_UART_MCR_OFFSET);
  314. }
  315. static struct amba_pl010_data ep93xx_uart_data = {
  316. .set_mctrl = ep93xx_uart_set_mctrl,
  317. };
  318. static struct amba_device uart1_device = {
  319. .dev = {
  320. .bus_id = "apb:uart1",
  321. .platform_data = &ep93xx_uart_data,
  322. },
  323. .res = {
  324. .start = EP93XX_UART1_PHYS_BASE,
  325. .end = EP93XX_UART1_PHYS_BASE + 0x0fff,
  326. .flags = IORESOURCE_MEM,
  327. },
  328. .irq = { IRQ_EP93XX_UART1, NO_IRQ },
  329. .periphid = 0x00041010,
  330. };
  331. static struct amba_device uart2_device = {
  332. .dev = {
  333. .bus_id = "apb:uart2",
  334. .platform_data = &ep93xx_uart_data,
  335. },
  336. .res = {
  337. .start = EP93XX_UART2_PHYS_BASE,
  338. .end = EP93XX_UART2_PHYS_BASE + 0x0fff,
  339. .flags = IORESOURCE_MEM,
  340. },
  341. .irq = { IRQ_EP93XX_UART2, NO_IRQ },
  342. .periphid = 0x00041010,
  343. };
  344. static struct amba_device uart3_device = {
  345. .dev = {
  346. .bus_id = "apb:uart3",
  347. .platform_data = &ep93xx_uart_data,
  348. },
  349. .res = {
  350. .start = EP93XX_UART3_PHYS_BASE,
  351. .end = EP93XX_UART3_PHYS_BASE + 0x0fff,
  352. .flags = IORESOURCE_MEM,
  353. },
  354. .irq = { IRQ_EP93XX_UART3, NO_IRQ },
  355. .periphid = 0x00041010,
  356. };
  357. static struct platform_device ep93xx_rtc_device = {
  358. .name = "ep93xx-rtc",
  359. .id = -1,
  360. .num_resources = 0,
  361. };
  362. void __init ep93xx_init_devices(void)
  363. {
  364. unsigned int v;
  365. /*
  366. * Disallow access to MaverickCrunch initially.
  367. */
  368. v = __raw_readl(EP93XX_SYSCON_DEVICE_CONFIG);
  369. v &= ~EP93XX_SYSCON_DEVICE_CONFIG_CRUNCH_ENABLE;
  370. __raw_writel(0xaa, EP93XX_SYSCON_SWLOCK);
  371. __raw_writel(v, EP93XX_SYSCON_DEVICE_CONFIG);
  372. amba_device_register(&uart1_device, &iomem_resource);
  373. amba_device_register(&uart2_device, &iomem_resource);
  374. amba_device_register(&uart3_device, &iomem_resource);
  375. platform_device_register(&ep93xx_rtc_device);
  376. }