core.c 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520
  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. * Copyright (C) 2007 Herbert Valerio Riedel <hvr@gnu.org>
  7. *
  8. * Thanks go to Michael Burian and Ray Lehtiniemi for their key
  9. * role in the ep93xx linux community.
  10. *
  11. * This program is free software; you can redistribute it and/or modify
  12. * it under the terms of the GNU General Public License as published by
  13. * the Free Software Foundation; either version 2 of the License, or (at
  14. * your option) any later version.
  15. */
  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_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 <linux/io.h>
  35. #include <asm/types.h>
  36. #include <asm/setup.h>
  37. #include <asm/memory.h>
  38. #include <mach/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/mach/map.h>
  44. #include <asm/mach/time.h>
  45. #include <asm/mach/irq.h>
  46. #include <mach/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. __raw_writel(1, EP93XX_TIMER1_CLEAR);
  92. while ((signed long)
  93. (__raw_readl(EP93XX_TIMER4_VALUE_LOW) - last_jiffy_time)
  94. >= TIMER4_TICKS_PER_JIFFY) {
  95. last_jiffy_time += TIMER4_TICKS_PER_JIFFY;
  96. timer_tick();
  97. }
  98. return IRQ_HANDLED;
  99. }
  100. static struct irqaction ep93xx_timer_irq = {
  101. .name = "ep93xx timer",
  102. .flags = IRQF_DISABLED | IRQF_TIMER | IRQF_IRQPOLL,
  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((508469 / 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_unmasked[3];
  130. static unsigned char gpio_int_enabled[3];
  131. static unsigned char gpio_int_type1[3];
  132. static unsigned char gpio_int_type2[3];
  133. /* Port ordering is: A B F */
  134. static const u8 int_type1_register_offset[3] = { 0x90, 0xac, 0x4c };
  135. static const u8 int_type2_register_offset[3] = { 0x94, 0xb0, 0x50 };
  136. static const u8 eoi_register_offset[3] = { 0x98, 0xb4, 0x54 };
  137. static const u8 int_en_register_offset[3] = { 0x9c, 0xb8, 0x58 };
  138. void ep93xx_gpio_update_int_params(unsigned port)
  139. {
  140. BUG_ON(port > 2);
  141. __raw_writeb(0, EP93XX_GPIO_REG(int_en_register_offset[port]));
  142. __raw_writeb(gpio_int_type2[port],
  143. EP93XX_GPIO_REG(int_type2_register_offset[port]));
  144. __raw_writeb(gpio_int_type1[port],
  145. EP93XX_GPIO_REG(int_type1_register_offset[port]));
  146. __raw_writeb(gpio_int_unmasked[port] & gpio_int_enabled[port],
  147. EP93XX_GPIO_REG(int_en_register_offset[port]));
  148. }
  149. void ep93xx_gpio_int_mask(unsigned line)
  150. {
  151. gpio_int_unmasked[line >> 3] &= ~(1 << (line & 7));
  152. }
  153. /*************************************************************************
  154. * EP93xx IRQ handling
  155. *************************************************************************/
  156. static void ep93xx_gpio_ab_irq_handler(unsigned int irq, struct irq_desc *desc)
  157. {
  158. unsigned char status;
  159. int i;
  160. status = __raw_readb(EP93XX_GPIO_A_INT_STATUS);
  161. for (i = 0; i < 8; i++) {
  162. if (status & (1 << i)) {
  163. int gpio_irq = gpio_to_irq(EP93XX_GPIO_LINE_A(0)) + i;
  164. generic_handle_irq(gpio_irq);
  165. }
  166. }
  167. status = __raw_readb(EP93XX_GPIO_B_INT_STATUS);
  168. for (i = 0; i < 8; i++) {
  169. if (status & (1 << i)) {
  170. int gpio_irq = gpio_to_irq(EP93XX_GPIO_LINE_B(0)) + i;
  171. desc = irq_desc + gpio_irq;
  172. generic_handle_irq(gpio_irq);
  173. }
  174. }
  175. }
  176. static void ep93xx_gpio_f_irq_handler(unsigned int irq, struct irq_desc *desc)
  177. {
  178. /*
  179. * map discontiguous hw irq range to continous sw irq range:
  180. *
  181. * IRQ_EP93XX_GPIO{0..7}MUX -> gpio_to_irq(EP93XX_GPIO_LINE_F({0..7})
  182. */
  183. int port_f_idx = ((irq + 1) & 7) ^ 4; /* {19..22,47..50} -> {0..7} */
  184. int gpio_irq = gpio_to_irq(EP93XX_GPIO_LINE_F(0)) + port_f_idx;
  185. generic_handle_irq(gpio_irq);
  186. }
  187. static void ep93xx_gpio_irq_ack(unsigned int irq)
  188. {
  189. int line = irq_to_gpio(irq);
  190. int port = line >> 3;
  191. int port_mask = 1 << (line & 7);
  192. if ((irq_desc[irq].status & IRQ_TYPE_SENSE_MASK) == IRQ_TYPE_EDGE_BOTH) {
  193. gpio_int_type2[port] ^= port_mask; /* switch edge direction */
  194. ep93xx_gpio_update_int_params(port);
  195. }
  196. __raw_writeb(port_mask, EP93XX_GPIO_REG(eoi_register_offset[port]));
  197. }
  198. static void ep93xx_gpio_irq_mask_ack(unsigned int irq)
  199. {
  200. int line = irq_to_gpio(irq);
  201. int port = line >> 3;
  202. int port_mask = 1 << (line & 7);
  203. if ((irq_desc[irq].status & IRQ_TYPE_SENSE_MASK) == IRQ_TYPE_EDGE_BOTH)
  204. gpio_int_type2[port] ^= port_mask; /* switch edge direction */
  205. gpio_int_unmasked[port] &= ~port_mask;
  206. ep93xx_gpio_update_int_params(port);
  207. __raw_writeb(port_mask, EP93XX_GPIO_REG(eoi_register_offset[port]));
  208. }
  209. static void ep93xx_gpio_irq_mask(unsigned int irq)
  210. {
  211. int line = irq_to_gpio(irq);
  212. int port = line >> 3;
  213. gpio_int_unmasked[port] &= ~(1 << (line & 7));
  214. ep93xx_gpio_update_int_params(port);
  215. }
  216. static void ep93xx_gpio_irq_unmask(unsigned int irq)
  217. {
  218. int line = irq_to_gpio(irq);
  219. int port = line >> 3;
  220. gpio_int_unmasked[port] |= 1 << (line & 7);
  221. ep93xx_gpio_update_int_params(port);
  222. }
  223. /*
  224. * gpio_int_type1 controls whether the interrupt is level (0) or
  225. * edge (1) triggered, while gpio_int_type2 controls whether it
  226. * triggers on low/falling (0) or high/rising (1).
  227. */
  228. static int ep93xx_gpio_irq_type(unsigned int irq, unsigned int type)
  229. {
  230. struct irq_desc *desc = irq_desc + irq;
  231. const int gpio = irq_to_gpio(irq);
  232. const int port = gpio >> 3;
  233. const int port_mask = 1 << (gpio & 7);
  234. gpio_direction_input(gpio);
  235. switch (type) {
  236. case IRQ_TYPE_EDGE_RISING:
  237. gpio_int_type1[port] |= port_mask;
  238. gpio_int_type2[port] |= port_mask;
  239. desc->handle_irq = handle_edge_irq;
  240. break;
  241. case IRQ_TYPE_EDGE_FALLING:
  242. gpio_int_type1[port] |= port_mask;
  243. gpio_int_type2[port] &= ~port_mask;
  244. desc->handle_irq = handle_edge_irq;
  245. break;
  246. case IRQ_TYPE_LEVEL_HIGH:
  247. gpio_int_type1[port] &= ~port_mask;
  248. gpio_int_type2[port] |= port_mask;
  249. desc->handle_irq = handle_level_irq;
  250. break;
  251. case IRQ_TYPE_LEVEL_LOW:
  252. gpio_int_type1[port] &= ~port_mask;
  253. gpio_int_type2[port] &= ~port_mask;
  254. desc->handle_irq = handle_level_irq;
  255. break;
  256. case IRQ_TYPE_EDGE_BOTH:
  257. gpio_int_type1[port] |= port_mask;
  258. /* set initial polarity based on current input level */
  259. if (gpio_get_value(gpio))
  260. gpio_int_type2[port] &= ~port_mask; /* falling */
  261. else
  262. gpio_int_type2[port] |= port_mask; /* rising */
  263. desc->handle_irq = handle_edge_irq;
  264. break;
  265. default:
  266. pr_err("ep93xx: failed to set irq type %d for gpio %d\n",
  267. type, gpio);
  268. return -EINVAL;
  269. }
  270. gpio_int_enabled[port] |= port_mask;
  271. desc->status &= ~IRQ_TYPE_SENSE_MASK;
  272. desc->status |= type & IRQ_TYPE_SENSE_MASK;
  273. ep93xx_gpio_update_int_params(port);
  274. return 0;
  275. }
  276. static struct irq_chip ep93xx_gpio_irq_chip = {
  277. .name = "GPIO",
  278. .ack = ep93xx_gpio_irq_ack,
  279. .mask_ack = ep93xx_gpio_irq_mask_ack,
  280. .mask = ep93xx_gpio_irq_mask,
  281. .unmask = ep93xx_gpio_irq_unmask,
  282. .set_type = ep93xx_gpio_irq_type,
  283. };
  284. void __init ep93xx_init_irq(void)
  285. {
  286. int gpio_irq;
  287. vic_init((void *)EP93XX_VIC1_BASE, 0, EP93XX_VIC1_VALID_IRQ_MASK);
  288. vic_init((void *)EP93XX_VIC2_BASE, 32, EP93XX_VIC2_VALID_IRQ_MASK);
  289. for (gpio_irq = gpio_to_irq(0);
  290. gpio_irq <= gpio_to_irq(EP93XX_GPIO_LINE_MAX_IRQ); ++gpio_irq) {
  291. set_irq_chip(gpio_irq, &ep93xx_gpio_irq_chip);
  292. set_irq_handler(gpio_irq, handle_level_irq);
  293. set_irq_flags(gpio_irq, IRQF_VALID);
  294. }
  295. set_irq_chained_handler(IRQ_EP93XX_GPIO_AB, ep93xx_gpio_ab_irq_handler);
  296. set_irq_chained_handler(IRQ_EP93XX_GPIO0MUX, ep93xx_gpio_f_irq_handler);
  297. set_irq_chained_handler(IRQ_EP93XX_GPIO1MUX, ep93xx_gpio_f_irq_handler);
  298. set_irq_chained_handler(IRQ_EP93XX_GPIO2MUX, ep93xx_gpio_f_irq_handler);
  299. set_irq_chained_handler(IRQ_EP93XX_GPIO3MUX, ep93xx_gpio_f_irq_handler);
  300. set_irq_chained_handler(IRQ_EP93XX_GPIO4MUX, ep93xx_gpio_f_irq_handler);
  301. set_irq_chained_handler(IRQ_EP93XX_GPIO5MUX, ep93xx_gpio_f_irq_handler);
  302. set_irq_chained_handler(IRQ_EP93XX_GPIO6MUX, ep93xx_gpio_f_irq_handler);
  303. set_irq_chained_handler(IRQ_EP93XX_GPIO7MUX, ep93xx_gpio_f_irq_handler);
  304. }
  305. /*************************************************************************
  306. * EP93xx peripheral handling
  307. *************************************************************************/
  308. #define EP93XX_UART_MCR_OFFSET (0x0100)
  309. static void ep93xx_uart_set_mctrl(struct amba_device *dev,
  310. void __iomem *base, unsigned int mctrl)
  311. {
  312. unsigned int mcr;
  313. mcr = 0;
  314. if (!(mctrl & TIOCM_RTS))
  315. mcr |= 2;
  316. if (!(mctrl & TIOCM_DTR))
  317. mcr |= 1;
  318. __raw_writel(mcr, base + EP93XX_UART_MCR_OFFSET);
  319. }
  320. static struct amba_pl010_data ep93xx_uart_data = {
  321. .set_mctrl = ep93xx_uart_set_mctrl,
  322. };
  323. static struct amba_device uart1_device = {
  324. .dev = {
  325. .bus_id = "apb:uart1",
  326. .platform_data = &ep93xx_uart_data,
  327. },
  328. .res = {
  329. .start = EP93XX_UART1_PHYS_BASE,
  330. .end = EP93XX_UART1_PHYS_BASE + 0x0fff,
  331. .flags = IORESOURCE_MEM,
  332. },
  333. .irq = { IRQ_EP93XX_UART1, NO_IRQ },
  334. .periphid = 0x00041010,
  335. };
  336. static struct amba_device uart2_device = {
  337. .dev = {
  338. .bus_id = "apb:uart2",
  339. .platform_data = &ep93xx_uart_data,
  340. },
  341. .res = {
  342. .start = EP93XX_UART2_PHYS_BASE,
  343. .end = EP93XX_UART2_PHYS_BASE + 0x0fff,
  344. .flags = IORESOURCE_MEM,
  345. },
  346. .irq = { IRQ_EP93XX_UART2, NO_IRQ },
  347. .periphid = 0x00041010,
  348. };
  349. static struct amba_device uart3_device = {
  350. .dev = {
  351. .bus_id = "apb:uart3",
  352. .platform_data = &ep93xx_uart_data,
  353. },
  354. .res = {
  355. .start = EP93XX_UART3_PHYS_BASE,
  356. .end = EP93XX_UART3_PHYS_BASE + 0x0fff,
  357. .flags = IORESOURCE_MEM,
  358. },
  359. .irq = { IRQ_EP93XX_UART3, NO_IRQ },
  360. .periphid = 0x00041010,
  361. };
  362. static struct platform_device ep93xx_rtc_device = {
  363. .name = "ep93xx-rtc",
  364. .id = -1,
  365. .num_resources = 0,
  366. };
  367. static struct resource ep93xx_ohci_resources[] = {
  368. [0] = {
  369. .start = EP93XX_USB_PHYS_BASE,
  370. .end = EP93XX_USB_PHYS_BASE + 0x0fff,
  371. .flags = IORESOURCE_MEM,
  372. },
  373. [1] = {
  374. .start = IRQ_EP93XX_USB,
  375. .end = IRQ_EP93XX_USB,
  376. .flags = IORESOURCE_IRQ,
  377. },
  378. };
  379. static struct platform_device ep93xx_ohci_device = {
  380. .name = "ep93xx-ohci",
  381. .id = -1,
  382. .dev = {
  383. .dma_mask = (void *)0xffffffff,
  384. .coherent_dma_mask = 0xffffffff,
  385. },
  386. .num_resources = ARRAY_SIZE(ep93xx_ohci_resources),
  387. .resource = ep93xx_ohci_resources,
  388. };
  389. static struct ep93xx_eth_data ep93xx_eth_data;
  390. static struct resource ep93xx_eth_resource[] = {
  391. {
  392. .start = EP93XX_ETHERNET_PHYS_BASE,
  393. .end = EP93XX_ETHERNET_PHYS_BASE + 0xffff,
  394. .flags = IORESOURCE_MEM,
  395. }, {
  396. .start = IRQ_EP93XX_ETHERNET,
  397. .end = IRQ_EP93XX_ETHERNET,
  398. .flags = IORESOURCE_IRQ,
  399. }
  400. };
  401. static struct platform_device ep93xx_eth_device = {
  402. .name = "ep93xx-eth",
  403. .id = -1,
  404. .dev = {
  405. .platform_data = &ep93xx_eth_data,
  406. },
  407. .num_resources = ARRAY_SIZE(ep93xx_eth_resource),
  408. .resource = ep93xx_eth_resource,
  409. };
  410. void __init ep93xx_register_eth(struct ep93xx_eth_data *data, int copy_addr)
  411. {
  412. if (copy_addr) {
  413. memcpy(data->dev_addr,
  414. (void *)(EP93XX_ETHERNET_BASE + 0x50), 6);
  415. }
  416. ep93xx_eth_data = *data;
  417. platform_device_register(&ep93xx_eth_device);
  418. }
  419. extern void ep93xx_gpio_init(void);
  420. void __init ep93xx_init_devices(void)
  421. {
  422. unsigned int v;
  423. /*
  424. * Disallow access to MaverickCrunch initially.
  425. */
  426. v = __raw_readl(EP93XX_SYSCON_DEVICE_CONFIG);
  427. v &= ~EP93XX_SYSCON_DEVICE_CONFIG_CRUNCH_ENABLE;
  428. __raw_writel(0xaa, EP93XX_SYSCON_SWLOCK);
  429. __raw_writel(v, EP93XX_SYSCON_DEVICE_CONFIG);
  430. ep93xx_gpio_init();
  431. amba_device_register(&uart1_device, &iomem_resource);
  432. amba_device_register(&uart2_device, &iomem_resource);
  433. amba_device_register(&uart3_device, &iomem_resource);
  434. platform_device_register(&ep93xx_rtc_device);
  435. platform_device_register(&ep93xx_ohci_device);
  436. }