core.c 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579
  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.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)
  91. {
  92. write_seqlock(&xtime_lock);
  93. __raw_writel(1, EP93XX_TIMER1_CLEAR);
  94. while ((signed long)
  95. (__raw_readl(EP93XX_TIMER4_VALUE_LOW) - last_jiffy_time)
  96. >= TIMER4_TICKS_PER_JIFFY) {
  97. last_jiffy_time += TIMER4_TICKS_PER_JIFFY;
  98. timer_tick();
  99. }
  100. write_sequnlock(&xtime_lock);
  101. return IRQ_HANDLED;
  102. }
  103. static struct irqaction ep93xx_timer_irq = {
  104. .name = "ep93xx timer",
  105. .flags = IRQF_DISABLED | IRQF_TIMER | IRQF_IRQPOLL,
  106. .handler = ep93xx_timer_interrupt,
  107. };
  108. static void __init ep93xx_timer_init(void)
  109. {
  110. /* Enable periodic HZ timer. */
  111. __raw_writel(0x48, EP93XX_TIMER1_CONTROL);
  112. __raw_writel((508469 / HZ) - 1, EP93XX_TIMER1_LOAD);
  113. __raw_writel(0xc8, EP93XX_TIMER1_CONTROL);
  114. /* Enable lost jiffy timer. */
  115. __raw_writel(0x100, EP93XX_TIMER4_VALUE_HIGH);
  116. setup_irq(IRQ_EP93XX_TIMER1, &ep93xx_timer_irq);
  117. }
  118. static unsigned long ep93xx_gettimeoffset(void)
  119. {
  120. int offset;
  121. offset = __raw_readl(EP93XX_TIMER4_VALUE_LOW) - last_jiffy_time;
  122. /* Calculate (1000000 / 983040) * offset. */
  123. return offset + (53 * offset / 3072);
  124. }
  125. struct sys_timer ep93xx_timer = {
  126. .init = ep93xx_timer_init,
  127. .offset = ep93xx_gettimeoffset,
  128. };
  129. /*************************************************************************
  130. * GPIO handling for EP93xx
  131. *************************************************************************/
  132. static unsigned char gpio_int_unmasked[3];
  133. static unsigned char gpio_int_enabled[3];
  134. static unsigned char gpio_int_type1[3];
  135. static unsigned char gpio_int_type2[3];
  136. /* Port ordering is: A B F */
  137. static const u8 int_type1_register_offset[3] = { 0x90, 0xac, 0x4c };
  138. static const u8 int_type2_register_offset[3] = { 0x94, 0xb0, 0x50 };
  139. static const u8 eoi_register_offset[3] = { 0x98, 0xb4, 0x54 };
  140. static const u8 int_en_register_offset[3] = { 0x9c, 0xb8, 0x5c };
  141. static void update_gpio_int_params(unsigned port)
  142. {
  143. BUG_ON(port > 2);
  144. __raw_writeb(0, EP93XX_GPIO_REG(int_en_register_offset[port]));
  145. __raw_writeb(gpio_int_type2[port],
  146. EP93XX_GPIO_REG(int_type2_register_offset[port]));
  147. __raw_writeb(gpio_int_type1[port],
  148. EP93XX_GPIO_REG(int_type1_register_offset[port]));
  149. __raw_writeb(gpio_int_unmasked[port] & gpio_int_enabled[port],
  150. EP93XX_GPIO_REG(int_en_register_offset[port]));
  151. }
  152. /* Port ordering is: A B F D E C G H */
  153. static const u8 data_register_offset[8] = {
  154. 0x00, 0x04, 0x30, 0x0c, 0x20, 0x08, 0x38, 0x40,
  155. };
  156. static const u8 data_direction_register_offset[8] = {
  157. 0x10, 0x14, 0x34, 0x1c, 0x24, 0x18, 0x3c, 0x44,
  158. };
  159. static void ep93xx_gpio_set_direction(unsigned line, int direction)
  160. {
  161. unsigned int data_direction_register;
  162. unsigned long flags;
  163. unsigned char v;
  164. data_direction_register =
  165. EP93XX_GPIO_REG(data_direction_register_offset[line >> 3]);
  166. local_irq_save(flags);
  167. if (direction == GPIO_OUT) {
  168. if (line >= 0 && line <= EP93XX_GPIO_LINE_MAX_IRQ) {
  169. /* Port A/B/F */
  170. gpio_int_unmasked[line >> 3] &= ~(1 << (line & 7));
  171. update_gpio_int_params(line >> 3);
  172. }
  173. v = __raw_readb(data_direction_register);
  174. v |= 1 << (line & 7);
  175. __raw_writeb(v, data_direction_register);
  176. } else if (direction == GPIO_IN) {
  177. v = __raw_readb(data_direction_register);
  178. v &= ~(1 << (line & 7));
  179. __raw_writeb(v, data_direction_register);
  180. }
  181. local_irq_restore(flags);
  182. }
  183. void __deprecated gpio_line_config(int line, int direction)
  184. {
  185. ep93xx_gpio_set_direction(line, direction);
  186. }
  187. EXPORT_SYMBOL(gpio_line_config);
  188. int gpio_direction_input(unsigned gpio)
  189. {
  190. if (gpio > EP93XX_GPIO_LINE_MAX)
  191. return -EINVAL;
  192. ep93xx_gpio_set_direction(gpio, GPIO_IN);
  193. return 0;
  194. }
  195. EXPORT_SYMBOL(gpio_direction_input);
  196. int gpio_direction_output(unsigned gpio, int value)
  197. {
  198. if (gpio > EP93XX_GPIO_LINE_MAX)
  199. return -EINVAL;
  200. gpio_set_value(gpio, value);
  201. ep93xx_gpio_set_direction(gpio, GPIO_OUT);
  202. return 0;
  203. }
  204. EXPORT_SYMBOL(gpio_direction_output);
  205. int gpio_get_value(unsigned gpio)
  206. {
  207. unsigned int data_register;
  208. data_register = EP93XX_GPIO_REG(data_register_offset[gpio >> 3]);
  209. return !!(__raw_readb(data_register) & (1 << (gpio & 7)));
  210. }
  211. EXPORT_SYMBOL(gpio_get_value);
  212. void gpio_set_value(unsigned gpio, int value)
  213. {
  214. unsigned int data_register;
  215. unsigned long flags;
  216. unsigned char v;
  217. data_register = EP93XX_GPIO_REG(data_register_offset[gpio >> 3]);
  218. local_irq_save(flags);
  219. v = __raw_readb(data_register);
  220. if (value)
  221. v |= 1 << (gpio & 7);
  222. else
  223. v &= ~(1 << (gpio & 7));
  224. __raw_writeb(v, data_register);
  225. local_irq_restore(flags);
  226. }
  227. EXPORT_SYMBOL(gpio_set_value);
  228. /*************************************************************************
  229. * EP93xx IRQ handling
  230. *************************************************************************/
  231. static void ep93xx_gpio_ab_irq_handler(unsigned int irq, struct irq_desc *desc)
  232. {
  233. unsigned char status;
  234. int i;
  235. status = __raw_readb(EP93XX_GPIO_A_INT_STATUS);
  236. for (i = 0; i < 8; i++) {
  237. if (status & (1 << i)) {
  238. int gpio_irq = gpio_to_irq(EP93XX_GPIO_LINE_A(0)) + i;
  239. desc = irq_desc + gpio_irq;
  240. desc_handle_irq(gpio_irq, desc);
  241. }
  242. }
  243. status = __raw_readb(EP93XX_GPIO_B_INT_STATUS);
  244. for (i = 0; i < 8; i++) {
  245. if (status & (1 << i)) {
  246. int gpio_irq = gpio_to_irq(EP93XX_GPIO_LINE_B(0)) + i;
  247. desc = irq_desc + gpio_irq;
  248. desc_handle_irq(gpio_irq, desc);
  249. }
  250. }
  251. }
  252. static void ep93xx_gpio_f_irq_handler(unsigned int irq, struct irq_desc *desc)
  253. {
  254. /*
  255. * map discontiguous hw irq range to continous sw irq range:
  256. *
  257. * IRQ_EP93XX_GPIO{0..7}MUX -> gpio_to_irq(EP93XX_GPIO_LINE_F({0..7})
  258. */
  259. int port_f_idx = ((irq + 1) & 7) ^ 4; /* {19..22,47..50} -> {0..7} */
  260. int gpio_irq = gpio_to_irq(EP93XX_GPIO_LINE_F(0)) + port_f_idx;
  261. desc_handle_irq(gpio_irq, irq_desc + gpio_irq);
  262. }
  263. static void ep93xx_gpio_irq_ack(unsigned int irq)
  264. {
  265. int line = irq_to_gpio(irq);
  266. int port = line >> 3;
  267. int port_mask = 1 << (line & 7);
  268. if ((irq_desc[irq].status & IRQ_TYPE_SENSE_MASK) == IRQT_BOTHEDGE) {
  269. gpio_int_type2[port] ^= port_mask; /* switch edge direction */
  270. update_gpio_int_params(port);
  271. }
  272. __raw_writeb(port_mask, EP93XX_GPIO_REG(eoi_register_offset[port]));
  273. }
  274. static void ep93xx_gpio_irq_mask_ack(unsigned int irq)
  275. {
  276. int line = irq_to_gpio(irq);
  277. int port = line >> 3;
  278. int port_mask = 1 << (line & 7);
  279. if ((irq_desc[irq].status & IRQ_TYPE_SENSE_MASK) == IRQT_BOTHEDGE)
  280. gpio_int_type2[port] ^= port_mask; /* switch edge direction */
  281. gpio_int_unmasked[port] &= ~port_mask;
  282. update_gpio_int_params(port);
  283. __raw_writeb(port_mask, EP93XX_GPIO_REG(eoi_register_offset[port]));
  284. }
  285. static void ep93xx_gpio_irq_mask(unsigned int irq)
  286. {
  287. int line = irq_to_gpio(irq);
  288. int port = line >> 3;
  289. gpio_int_unmasked[port] &= ~(1 << (line & 7));
  290. update_gpio_int_params(port);
  291. }
  292. static void ep93xx_gpio_irq_unmask(unsigned int irq)
  293. {
  294. int line = irq_to_gpio(irq);
  295. int port = line >> 3;
  296. gpio_int_unmasked[port] |= 1 << (line & 7);
  297. update_gpio_int_params(port);
  298. }
  299. /*
  300. * gpio_int_type1 controls whether the interrupt is level (0) or
  301. * edge (1) triggered, while gpio_int_type2 controls whether it
  302. * triggers on low/falling (0) or high/rising (1).
  303. */
  304. static int ep93xx_gpio_irq_type(unsigned int irq, unsigned int type)
  305. {
  306. struct irq_desc *desc = irq_desc + irq;
  307. const int gpio = irq_to_gpio(irq);
  308. const int port = gpio >> 3;
  309. const int port_mask = 1 << (gpio & 7);
  310. ep93xx_gpio_set_direction(gpio, GPIO_IN);
  311. switch (type) {
  312. case IRQT_RISING:
  313. gpio_int_type1[port] |= port_mask;
  314. gpio_int_type2[port] |= port_mask;
  315. desc->handle_irq = handle_edge_irq;
  316. break;
  317. case IRQT_FALLING:
  318. gpio_int_type1[port] |= port_mask;
  319. gpio_int_type2[port] &= ~port_mask;
  320. desc->handle_irq = handle_edge_irq;
  321. break;
  322. case IRQT_HIGH:
  323. gpio_int_type1[port] &= ~port_mask;
  324. gpio_int_type2[port] |= port_mask;
  325. desc->handle_irq = handle_level_irq;
  326. break;
  327. case IRQT_LOW:
  328. gpio_int_type1[port] &= ~port_mask;
  329. gpio_int_type2[port] &= ~port_mask;
  330. desc->handle_irq = handle_level_irq;
  331. break;
  332. case IRQT_BOTHEDGE:
  333. gpio_int_type1[port] |= port_mask;
  334. /* set initial polarity based on current input level */
  335. if (gpio_get_value(gpio))
  336. gpio_int_type2[port] &= ~port_mask; /* falling */
  337. else
  338. gpio_int_type2[port] |= port_mask; /* rising */
  339. desc->handle_irq = handle_edge_irq;
  340. break;
  341. default:
  342. pr_err("ep93xx: failed to set irq type %d for gpio %d\n",
  343. type, gpio);
  344. return -EINVAL;
  345. }
  346. gpio_int_enabled[port] |= port_mask;
  347. desc->status &= ~IRQ_TYPE_SENSE_MASK;
  348. desc->status |= type & IRQ_TYPE_SENSE_MASK;
  349. update_gpio_int_params(port);
  350. return 0;
  351. }
  352. static struct irq_chip ep93xx_gpio_irq_chip = {
  353. .name = "GPIO",
  354. .ack = ep93xx_gpio_irq_ack,
  355. .mask_ack = ep93xx_gpio_irq_mask_ack,
  356. .mask = ep93xx_gpio_irq_mask,
  357. .unmask = ep93xx_gpio_irq_unmask,
  358. .set_type = ep93xx_gpio_irq_type,
  359. };
  360. void __init ep93xx_init_irq(void)
  361. {
  362. int gpio_irq;
  363. vic_init((void *)EP93XX_VIC1_BASE, 0, EP93XX_VIC1_VALID_IRQ_MASK);
  364. vic_init((void *)EP93XX_VIC2_BASE, 32, EP93XX_VIC2_VALID_IRQ_MASK);
  365. for (gpio_irq = gpio_to_irq(0);
  366. gpio_irq <= gpio_to_irq(EP93XX_GPIO_LINE_MAX_IRQ); ++gpio_irq) {
  367. set_irq_chip(gpio_irq, &ep93xx_gpio_irq_chip);
  368. set_irq_handler(gpio_irq, handle_level_irq);
  369. set_irq_flags(gpio_irq, IRQF_VALID);
  370. }
  371. set_irq_chained_handler(IRQ_EP93XX_GPIO_AB, ep93xx_gpio_ab_irq_handler);
  372. set_irq_chained_handler(IRQ_EP93XX_GPIO0MUX, ep93xx_gpio_f_irq_handler);
  373. set_irq_chained_handler(IRQ_EP93XX_GPIO1MUX, ep93xx_gpio_f_irq_handler);
  374. set_irq_chained_handler(IRQ_EP93XX_GPIO2MUX, ep93xx_gpio_f_irq_handler);
  375. set_irq_chained_handler(IRQ_EP93XX_GPIO3MUX, ep93xx_gpio_f_irq_handler);
  376. set_irq_chained_handler(IRQ_EP93XX_GPIO4MUX, ep93xx_gpio_f_irq_handler);
  377. set_irq_chained_handler(IRQ_EP93XX_GPIO5MUX, ep93xx_gpio_f_irq_handler);
  378. set_irq_chained_handler(IRQ_EP93XX_GPIO6MUX, ep93xx_gpio_f_irq_handler);
  379. set_irq_chained_handler(IRQ_EP93XX_GPIO7MUX, ep93xx_gpio_f_irq_handler);
  380. }
  381. /*************************************************************************
  382. * EP93xx peripheral handling
  383. *************************************************************************/
  384. #define EP93XX_UART_MCR_OFFSET (0x0100)
  385. static void ep93xx_uart_set_mctrl(struct amba_device *dev,
  386. void __iomem *base, unsigned int mctrl)
  387. {
  388. unsigned int mcr;
  389. mcr = 0;
  390. if (!(mctrl & TIOCM_RTS))
  391. mcr |= 2;
  392. if (!(mctrl & TIOCM_DTR))
  393. mcr |= 1;
  394. __raw_writel(mcr, base + EP93XX_UART_MCR_OFFSET);
  395. }
  396. static struct amba_pl010_data ep93xx_uart_data = {
  397. .set_mctrl = ep93xx_uart_set_mctrl,
  398. };
  399. static struct amba_device uart1_device = {
  400. .dev = {
  401. .bus_id = "apb:uart1",
  402. .platform_data = &ep93xx_uart_data,
  403. },
  404. .res = {
  405. .start = EP93XX_UART1_PHYS_BASE,
  406. .end = EP93XX_UART1_PHYS_BASE + 0x0fff,
  407. .flags = IORESOURCE_MEM,
  408. },
  409. .irq = { IRQ_EP93XX_UART1, NO_IRQ },
  410. .periphid = 0x00041010,
  411. };
  412. static struct amba_device uart2_device = {
  413. .dev = {
  414. .bus_id = "apb:uart2",
  415. .platform_data = &ep93xx_uart_data,
  416. },
  417. .res = {
  418. .start = EP93XX_UART2_PHYS_BASE,
  419. .end = EP93XX_UART2_PHYS_BASE + 0x0fff,
  420. .flags = IORESOURCE_MEM,
  421. },
  422. .irq = { IRQ_EP93XX_UART2, NO_IRQ },
  423. .periphid = 0x00041010,
  424. };
  425. static struct amba_device uart3_device = {
  426. .dev = {
  427. .bus_id = "apb:uart3",
  428. .platform_data = &ep93xx_uart_data,
  429. },
  430. .res = {
  431. .start = EP93XX_UART3_PHYS_BASE,
  432. .end = EP93XX_UART3_PHYS_BASE + 0x0fff,
  433. .flags = IORESOURCE_MEM,
  434. },
  435. .irq = { IRQ_EP93XX_UART3, NO_IRQ },
  436. .periphid = 0x00041010,
  437. };
  438. static struct platform_device ep93xx_rtc_device = {
  439. .name = "ep93xx-rtc",
  440. .id = -1,
  441. .num_resources = 0,
  442. };
  443. static struct resource ep93xx_ohci_resources[] = {
  444. [0] = {
  445. .start = EP93XX_USB_PHYS_BASE,
  446. .end = EP93XX_USB_PHYS_BASE + 0x0fff,
  447. .flags = IORESOURCE_MEM,
  448. },
  449. [1] = {
  450. .start = IRQ_EP93XX_USB,
  451. .end = IRQ_EP93XX_USB,
  452. .flags = IORESOURCE_IRQ,
  453. },
  454. };
  455. static struct platform_device ep93xx_ohci_device = {
  456. .name = "ep93xx-ohci",
  457. .id = -1,
  458. .dev = {
  459. .dma_mask = (void *)0xffffffff,
  460. .coherent_dma_mask = 0xffffffff,
  461. },
  462. .num_resources = ARRAY_SIZE(ep93xx_ohci_resources),
  463. .resource = ep93xx_ohci_resources,
  464. };
  465. void __init ep93xx_init_devices(void)
  466. {
  467. unsigned int v;
  468. /*
  469. * Disallow access to MaverickCrunch initially.
  470. */
  471. v = __raw_readl(EP93XX_SYSCON_DEVICE_CONFIG);
  472. v &= ~EP93XX_SYSCON_DEVICE_CONFIG_CRUNCH_ENABLE;
  473. __raw_writel(0xaa, EP93XX_SYSCON_SWLOCK);
  474. __raw_writel(v, EP93XX_SYSCON_DEVICE_CONFIG);
  475. amba_device_register(&uart1_device, &iomem_resource);
  476. amba_device_register(&uart2_device, &iomem_resource);
  477. amba_device_register(&uart3_device, &iomem_resource);
  478. platform_device_register(&ep93xx_rtc_device);
  479. platform_device_register(&ep93xx_ohci_device);
  480. }