core.c 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519
  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/kernel.h>
  16. #include <linux/init.h>
  17. #include <linux/spinlock.h>
  18. #include <linux/sched.h>
  19. #include <linux/interrupt.h>
  20. #include <linux/serial.h>
  21. #include <linux/tty.h>
  22. #include <linux/bitops.h>
  23. #include <linux/serial.h>
  24. #include <linux/serial_8250.h>
  25. #include <linux/serial_core.h>
  26. #include <linux/device.h>
  27. #include <linux/mm.h>
  28. #include <linux/time.h>
  29. #include <linux/timex.h>
  30. #include <linux/delay.h>
  31. #include <linux/termios.h>
  32. #include <linux/amba/bus.h>
  33. #include <linux/amba/serial.h>
  34. #include <asm/types.h>
  35. #include <asm/setup.h>
  36. #include <asm/memory.h>
  37. #include <asm/hardware.h>
  38. #include <asm/irq.h>
  39. #include <asm/system.h>
  40. #include <asm/tlbflush.h>
  41. #include <asm/pgtable.h>
  42. #include <asm/io.h>
  43. #include <asm/mach/map.h>
  44. #include <asm/mach/time.h>
  45. #include <asm/mach/irq.h>
  46. #include <asm/arch/gpio.h>
  47. #include <asm/hardware/vic.h>
  48. /*************************************************************************
  49. * Static I/O mappings that are needed for all EP93xx platforms
  50. *************************************************************************/
  51. static struct map_desc ep93xx_io_desc[] __initdata = {
  52. {
  53. .virtual = EP93XX_AHB_VIRT_BASE,
  54. .pfn = __phys_to_pfn(EP93XX_AHB_PHYS_BASE),
  55. .length = EP93XX_AHB_SIZE,
  56. .type = MT_DEVICE,
  57. }, {
  58. .virtual = EP93XX_APB_VIRT_BASE,
  59. .pfn = __phys_to_pfn(EP93XX_APB_PHYS_BASE),
  60. .length = EP93XX_APB_SIZE,
  61. .type = MT_DEVICE,
  62. },
  63. };
  64. void __init ep93xx_map_io(void)
  65. {
  66. iotable_init(ep93xx_io_desc, ARRAY_SIZE(ep93xx_io_desc));
  67. }
  68. /*************************************************************************
  69. * Timer handling for EP93xx
  70. *************************************************************************
  71. * The ep93xx has four internal timers. Timers 1, 2 (both 16 bit) and
  72. * 3 (32 bit) count down at 508 kHz, are self-reloading, and can generate
  73. * an interrupt on underflow. Timer 4 (40 bit) counts down at 983.04 kHz,
  74. * is free-running, and can't generate interrupts.
  75. *
  76. * The 508 kHz timers are ideal for use for the timer interrupt, as the
  77. * most common values of HZ divide 508 kHz nicely. We pick one of the 16
  78. * bit timers (timer 1) since we don't need more than 16 bits of reload
  79. * value as long as HZ >= 8.
  80. *
  81. * The higher clock rate of timer 4 makes it a better choice than the
  82. * other timers for use in gettimeoffset(), while the fact that it can't
  83. * generate interrupts means we don't have to worry about not being able
  84. * to use this timer for something else. We also use timer 4 for keeping
  85. * track of lost jiffies.
  86. */
  87. static unsigned int last_jiffy_time;
  88. #define TIMER4_TICKS_PER_JIFFY ((CLOCK_TICK_RATE + (HZ/2)) / HZ)
  89. static int ep93xx_timer_interrupt(int irq, void *dev_id)
  90. {
  91. write_seqlock(&xtime_lock);
  92. __raw_writel(1, EP93XX_TIMER1_CLEAR);
  93. while ((signed long)
  94. (__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();
  98. }
  99. write_sequnlock(&xtime_lock);
  100. return IRQ_HANDLED;
  101. }
  102. static struct irqaction ep93xx_timer_irq = {
  103. .name = "ep93xx timer",
  104. .flags = IRQF_DISABLED | IRQF_TIMER | IRQF_IRQPOLL,
  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((508469 / 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_unmasked[3];
  132. static unsigned char gpio_int_enabled[3];
  133. static unsigned char gpio_int_type1[3];
  134. static unsigned char gpio_int_type2[3];
  135. static void update_gpio_int_params(int abf)
  136. {
  137. if (abf == 0) {
  138. __raw_writeb(0, EP93XX_GPIO_A_INT_ENABLE);
  139. __raw_writeb(gpio_int_type2[0], EP93XX_GPIO_A_INT_TYPE2);
  140. __raw_writeb(gpio_int_type1[0], EP93XX_GPIO_A_INT_TYPE1);
  141. __raw_writeb(gpio_int_unmasked[0] & gpio_int_enabled[0], EP93XX_GPIO_A_INT_ENABLE);
  142. } else if (abf == 1) {
  143. __raw_writeb(0, EP93XX_GPIO_B_INT_ENABLE);
  144. __raw_writeb(gpio_int_type2[1], EP93XX_GPIO_B_INT_TYPE2);
  145. __raw_writeb(gpio_int_type1[1], EP93XX_GPIO_B_INT_TYPE1);
  146. __raw_writeb(gpio_int_unmasked[1] & gpio_int_enabled[1], EP93XX_GPIO_B_INT_ENABLE);
  147. } else if (abf == 2) {
  148. __raw_writeb(0, EP93XX_GPIO_F_INT_ENABLE);
  149. __raw_writeb(gpio_int_type2[2], EP93XX_GPIO_F_INT_TYPE2);
  150. __raw_writeb(gpio_int_type1[2], EP93XX_GPIO_F_INT_TYPE1);
  151. __raw_writeb(gpio_int_unmasked[2] & gpio_int_enabled[2], EP93XX_GPIO_F_INT_ENABLE);
  152. } else {
  153. BUG();
  154. }
  155. }
  156. static unsigned char data_register_offset[8] = {
  157. 0x00, 0x04, 0x08, 0x0c, 0x20, 0x30, 0x38, 0x40,
  158. };
  159. static unsigned char data_direction_register_offset[8] = {
  160. 0x10, 0x14, 0x18, 0x1c, 0x24, 0x34, 0x3c, 0x44,
  161. };
  162. void gpio_line_config(int line, int direction)
  163. {
  164. unsigned int data_direction_register;
  165. unsigned long flags;
  166. unsigned char v;
  167. data_direction_register =
  168. EP93XX_GPIO_REG(data_direction_register_offset[line >> 3]);
  169. local_irq_save(flags);
  170. if (direction == GPIO_OUT) {
  171. if (line >= 0 && line < 16) {
  172. /* Port A/B. */
  173. gpio_int_unmasked[line >> 3] &= ~(1 << (line & 7));
  174. update_gpio_int_params(line >> 3);
  175. } else if (line >= 40 && line < 48) {
  176. /* Port F. */
  177. gpio_int_unmasked[2] &= ~(1 << (line & 7));
  178. update_gpio_int_params(2);
  179. }
  180. v = __raw_readb(data_direction_register);
  181. v |= 1 << (line & 7);
  182. __raw_writeb(v, data_direction_register);
  183. } else if (direction == GPIO_IN) {
  184. v = __raw_readb(data_direction_register);
  185. v &= ~(1 << (line & 7));
  186. __raw_writeb(v, data_direction_register);
  187. }
  188. local_irq_restore(flags);
  189. }
  190. EXPORT_SYMBOL(gpio_line_config);
  191. int gpio_line_get(int line)
  192. {
  193. unsigned int data_register;
  194. data_register = EP93XX_GPIO_REG(data_register_offset[line >> 3]);
  195. return !!(__raw_readb(data_register) & (1 << (line & 7)));
  196. }
  197. EXPORT_SYMBOL(gpio_line_get);
  198. void gpio_line_set(int line, int value)
  199. {
  200. unsigned int data_register;
  201. unsigned long flags;
  202. unsigned char v;
  203. data_register = EP93XX_GPIO_REG(data_register_offset[line >> 3]);
  204. local_irq_save(flags);
  205. if (value == EP93XX_GPIO_HIGH) {
  206. v = __raw_readb(data_register);
  207. v |= 1 << (line & 7);
  208. __raw_writeb(v, data_register);
  209. } else if (value == EP93XX_GPIO_LOW) {
  210. v = __raw_readb(data_register);
  211. v &= ~(1 << (line & 7));
  212. __raw_writeb(v, data_register);
  213. }
  214. local_irq_restore(flags);
  215. }
  216. EXPORT_SYMBOL(gpio_line_set);
  217. /*************************************************************************
  218. * EP93xx IRQ handling
  219. *************************************************************************/
  220. static void ep93xx_gpio_ab_irq_handler(unsigned int irq, struct irq_desc *desc)
  221. {
  222. unsigned char status;
  223. int i;
  224. status = __raw_readb(EP93XX_GPIO_A_INT_STATUS);
  225. for (i = 0; i < 8; i++) {
  226. if (status & (1 << i)) {
  227. desc = irq_desc + IRQ_EP93XX_GPIO(0) + i;
  228. desc_handle_irq(IRQ_EP93XX_GPIO(0) + i, desc);
  229. }
  230. }
  231. status = __raw_readb(EP93XX_GPIO_B_INT_STATUS);
  232. for (i = 0; i < 8; i++) {
  233. if (status & (1 << i)) {
  234. desc = irq_desc + IRQ_EP93XX_GPIO(8) + i;
  235. desc_handle_irq(IRQ_EP93XX_GPIO(8) + i, desc);
  236. }
  237. }
  238. }
  239. static void ep93xx_gpio_f_irq_handler(unsigned int irq, struct irq_desc *desc)
  240. {
  241. int gpio_irq = IRQ_EP93XX_GPIO(16) + (((irq + 1) & 7) ^ 4);
  242. desc_handle_irq(gpio_irq, irq_desc + gpio_irq);
  243. }
  244. static void ep93xx_gpio_irq_mask_ack(unsigned int irq)
  245. {
  246. int line = irq - IRQ_EP93XX_GPIO(0);
  247. int port = line >> 3;
  248. gpio_int_unmasked[port] &= ~(1 << (line & 7));
  249. update_gpio_int_params(port);
  250. if (port == 0) {
  251. __raw_writel(1 << (line & 7), EP93XX_GPIO_A_INT_ACK);
  252. } else if (port == 1) {
  253. __raw_writel(1 << (line & 7), EP93XX_GPIO_B_INT_ACK);
  254. } else if (port == 2) {
  255. __raw_writel(1 << (line & 7), EP93XX_GPIO_F_INT_ACK);
  256. }
  257. }
  258. static void ep93xx_gpio_irq_mask(unsigned int irq)
  259. {
  260. int line = irq - IRQ_EP93XX_GPIO(0);
  261. int port = line >> 3;
  262. gpio_int_unmasked[port] &= ~(1 << (line & 7));
  263. update_gpio_int_params(port);
  264. }
  265. static void ep93xx_gpio_irq_unmask(unsigned int irq)
  266. {
  267. int line = irq - IRQ_EP93XX_GPIO(0);
  268. int port = line >> 3;
  269. gpio_int_unmasked[port] |= 1 << (line & 7);
  270. update_gpio_int_params(port);
  271. }
  272. /*
  273. * gpio_int_type1 controls whether the interrupt is level (0) or
  274. * edge (1) triggered, while gpio_int_type2 controls whether it
  275. * triggers on low/falling (0) or high/rising (1).
  276. */
  277. static int ep93xx_gpio_irq_type(unsigned int irq, unsigned int type)
  278. {
  279. int port;
  280. int line;
  281. line = irq - IRQ_EP93XX_GPIO(0);
  282. if (line >= 0 && line < 16) {
  283. gpio_line_config(line, GPIO_IN);
  284. } else {
  285. gpio_line_config(EP93XX_GPIO_LINE_F(line-16), GPIO_IN);
  286. }
  287. port = line >> 3;
  288. line &= 7;
  289. if (type & IRQT_RISING) {
  290. gpio_int_enabled[port] |= 1 << line;
  291. gpio_int_type1[port] |= 1 << line;
  292. gpio_int_type2[port] |= 1 << line;
  293. } else if (type & IRQT_FALLING) {
  294. gpio_int_enabled[port] |= 1 << line;
  295. gpio_int_type1[port] |= 1 << line;
  296. gpio_int_type2[port] &= ~(1 << line);
  297. } else if (type & IRQT_HIGH) {
  298. gpio_int_enabled[port] |= 1 << line;
  299. gpio_int_type1[port] &= ~(1 << line);
  300. gpio_int_type2[port] |= 1 << line;
  301. } else if (type & IRQT_LOW) {
  302. gpio_int_enabled[port] |= 1 << line;
  303. gpio_int_type1[port] &= ~(1 << line);
  304. gpio_int_type2[port] &= ~(1 << line);
  305. } else {
  306. gpio_int_enabled[port] &= ~(1 << line);
  307. }
  308. update_gpio_int_params(port);
  309. return 0;
  310. }
  311. static struct irq_chip ep93xx_gpio_irq_chip = {
  312. .name = "GPIO",
  313. .ack = ep93xx_gpio_irq_mask_ack,
  314. .mask = ep93xx_gpio_irq_mask,
  315. .unmask = ep93xx_gpio_irq_unmask,
  316. .set_type = ep93xx_gpio_irq_type,
  317. };
  318. void __init ep93xx_init_irq(void)
  319. {
  320. int irq;
  321. vic_init((void *)EP93XX_VIC1_BASE, 0, EP93XX_VIC1_VALID_IRQ_MASK);
  322. vic_init((void *)EP93XX_VIC2_BASE, 32, EP93XX_VIC2_VALID_IRQ_MASK);
  323. for (irq = IRQ_EP93XX_GPIO(0); irq <= IRQ_EP93XX_GPIO(23); irq++) {
  324. set_irq_chip(irq, &ep93xx_gpio_irq_chip);
  325. set_irq_handler(irq, handle_level_irq);
  326. set_irq_flags(irq, IRQF_VALID);
  327. }
  328. set_irq_chained_handler(IRQ_EP93XX_GPIO_AB, ep93xx_gpio_ab_irq_handler);
  329. set_irq_chained_handler(IRQ_EP93XX_GPIO0MUX, ep93xx_gpio_f_irq_handler);
  330. set_irq_chained_handler(IRQ_EP93XX_GPIO1MUX, ep93xx_gpio_f_irq_handler);
  331. set_irq_chained_handler(IRQ_EP93XX_GPIO2MUX, ep93xx_gpio_f_irq_handler);
  332. set_irq_chained_handler(IRQ_EP93XX_GPIO3MUX, ep93xx_gpio_f_irq_handler);
  333. set_irq_chained_handler(IRQ_EP93XX_GPIO4MUX, ep93xx_gpio_f_irq_handler);
  334. set_irq_chained_handler(IRQ_EP93XX_GPIO5MUX, ep93xx_gpio_f_irq_handler);
  335. set_irq_chained_handler(IRQ_EP93XX_GPIO6MUX, ep93xx_gpio_f_irq_handler);
  336. set_irq_chained_handler(IRQ_EP93XX_GPIO7MUX, ep93xx_gpio_f_irq_handler);
  337. }
  338. /*************************************************************************
  339. * EP93xx peripheral handling
  340. *************************************************************************/
  341. #define EP93XX_UART_MCR_OFFSET (0x0100)
  342. static void ep93xx_uart_set_mctrl(struct amba_device *dev,
  343. void __iomem *base, unsigned int mctrl)
  344. {
  345. unsigned int mcr;
  346. mcr = 0;
  347. if (!(mctrl & TIOCM_RTS))
  348. mcr |= 2;
  349. if (!(mctrl & TIOCM_DTR))
  350. mcr |= 1;
  351. __raw_writel(mcr, base + EP93XX_UART_MCR_OFFSET);
  352. }
  353. static struct amba_pl010_data ep93xx_uart_data = {
  354. .set_mctrl = ep93xx_uart_set_mctrl,
  355. };
  356. static struct amba_device uart1_device = {
  357. .dev = {
  358. .bus_id = "apb:uart1",
  359. .platform_data = &ep93xx_uart_data,
  360. },
  361. .res = {
  362. .start = EP93XX_UART1_PHYS_BASE,
  363. .end = EP93XX_UART1_PHYS_BASE + 0x0fff,
  364. .flags = IORESOURCE_MEM,
  365. },
  366. .irq = { IRQ_EP93XX_UART1, NO_IRQ },
  367. .periphid = 0x00041010,
  368. };
  369. static struct amba_device uart2_device = {
  370. .dev = {
  371. .bus_id = "apb:uart2",
  372. .platform_data = &ep93xx_uart_data,
  373. },
  374. .res = {
  375. .start = EP93XX_UART2_PHYS_BASE,
  376. .end = EP93XX_UART2_PHYS_BASE + 0x0fff,
  377. .flags = IORESOURCE_MEM,
  378. },
  379. .irq = { IRQ_EP93XX_UART2, NO_IRQ },
  380. .periphid = 0x00041010,
  381. };
  382. static struct amba_device uart3_device = {
  383. .dev = {
  384. .bus_id = "apb:uart3",
  385. .platform_data = &ep93xx_uart_data,
  386. },
  387. .res = {
  388. .start = EP93XX_UART3_PHYS_BASE,
  389. .end = EP93XX_UART3_PHYS_BASE + 0x0fff,
  390. .flags = IORESOURCE_MEM,
  391. },
  392. .irq = { IRQ_EP93XX_UART3, NO_IRQ },
  393. .periphid = 0x00041010,
  394. };
  395. static struct platform_device ep93xx_rtc_device = {
  396. .name = "ep93xx-rtc",
  397. .id = -1,
  398. .num_resources = 0,
  399. };
  400. static struct resource ep93xx_ohci_resources[] = {
  401. [0] = {
  402. .start = EP93XX_USB_PHYS_BASE,
  403. .end = EP93XX_USB_PHYS_BASE + 0x0fff,
  404. .flags = IORESOURCE_MEM,
  405. },
  406. [1] = {
  407. .start = IRQ_EP93XX_USB,
  408. .end = IRQ_EP93XX_USB,
  409. .flags = IORESOURCE_IRQ,
  410. },
  411. };
  412. static struct platform_device ep93xx_ohci_device = {
  413. .name = "ep93xx-ohci",
  414. .id = -1,
  415. .dev = {
  416. .dma_mask = (void *)0xffffffff,
  417. .coherent_dma_mask = 0xffffffff,
  418. },
  419. .num_resources = ARRAY_SIZE(ep93xx_ohci_resources),
  420. .resource = ep93xx_ohci_resources,
  421. };
  422. void __init ep93xx_init_devices(void)
  423. {
  424. unsigned int v;
  425. /*
  426. * Disallow access to MaverickCrunch initially.
  427. */
  428. v = __raw_readl(EP93XX_SYSCON_DEVICE_CONFIG);
  429. v &= ~EP93XX_SYSCON_DEVICE_CONFIG_CRUNCH_ENABLE;
  430. __raw_writel(0xaa, EP93XX_SYSCON_SWLOCK);
  431. __raw_writel(v, EP93XX_SYSCON_DEVICE_CONFIG);
  432. amba_device_register(&uart1_device, &iomem_resource);
  433. amba_device_register(&uart2_device, &iomem_resource);
  434. amba_device_register(&uart3_device, &iomem_resource);
  435. platform_device_register(&ep93xx_rtc_device);
  436. platform_device_register(&ep93xx_ohci_device);
  437. }