core.c 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562
  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/dma-mapping.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 <linux/io.h>
  36. #include <linux/i2c.h>
  37. #include <linux/i2c-gpio.h>
  38. #include <asm/types.h>
  39. #include <asm/setup.h>
  40. #include <asm/memory.h>
  41. #include <mach/hardware.h>
  42. #include <asm/irq.h>
  43. #include <asm/system.h>
  44. #include <asm/tlbflush.h>
  45. #include <asm/pgtable.h>
  46. #include <asm/mach/map.h>
  47. #include <asm/mach/time.h>
  48. #include <asm/mach/irq.h>
  49. #include <mach/gpio.h>
  50. #include <asm/hardware/vic.h>
  51. /*************************************************************************
  52. * Static I/O mappings that are needed for all EP93xx platforms
  53. *************************************************************************/
  54. static struct map_desc ep93xx_io_desc[] __initdata = {
  55. {
  56. .virtual = EP93XX_AHB_VIRT_BASE,
  57. .pfn = __phys_to_pfn(EP93XX_AHB_PHYS_BASE),
  58. .length = EP93XX_AHB_SIZE,
  59. .type = MT_DEVICE,
  60. }, {
  61. .virtual = EP93XX_APB_VIRT_BASE,
  62. .pfn = __phys_to_pfn(EP93XX_APB_PHYS_BASE),
  63. .length = EP93XX_APB_SIZE,
  64. .type = MT_DEVICE,
  65. },
  66. };
  67. void __init ep93xx_map_io(void)
  68. {
  69. iotable_init(ep93xx_io_desc, ARRAY_SIZE(ep93xx_io_desc));
  70. }
  71. /*************************************************************************
  72. * Timer handling for EP93xx
  73. *************************************************************************
  74. * The ep93xx has four internal timers. Timers 1, 2 (both 16 bit) and
  75. * 3 (32 bit) count down at 508 kHz, are self-reloading, and can generate
  76. * an interrupt on underflow. Timer 4 (40 bit) counts down at 983.04 kHz,
  77. * is free-running, and can't generate interrupts.
  78. *
  79. * The 508 kHz timers are ideal for use for the timer interrupt, as the
  80. * most common values of HZ divide 508 kHz nicely. We pick one of the 16
  81. * bit timers (timer 1) since we don't need more than 16 bits of reload
  82. * value as long as HZ >= 8.
  83. *
  84. * The higher clock rate of timer 4 makes it a better choice than the
  85. * other timers for use in gettimeoffset(), while the fact that it can't
  86. * generate interrupts means we don't have to worry about not being able
  87. * to use this timer for something else. We also use timer 4 for keeping
  88. * track of lost jiffies.
  89. */
  90. static unsigned int last_jiffy_time;
  91. #define TIMER4_TICKS_PER_JIFFY ((CLOCK_TICK_RATE + (HZ/2)) / HZ)
  92. static int ep93xx_timer_interrupt(int irq, void *dev_id)
  93. {
  94. __raw_writel(1, EP93XX_TIMER1_CLEAR);
  95. while ((signed long)
  96. (__raw_readl(EP93XX_TIMER4_VALUE_LOW) - last_jiffy_time)
  97. >= TIMER4_TICKS_PER_JIFFY) {
  98. last_jiffy_time += TIMER4_TICKS_PER_JIFFY;
  99. timer_tick();
  100. }
  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. static unsigned char gpio_int_debouce[3];
  137. /* Port ordering is: A B F */
  138. static const u8 int_type1_register_offset[3] = { 0x90, 0xac, 0x4c };
  139. static const u8 int_type2_register_offset[3] = { 0x94, 0xb0, 0x50 };
  140. static const u8 eoi_register_offset[3] = { 0x98, 0xb4, 0x54 };
  141. static const u8 int_en_register_offset[3] = { 0x9c, 0xb8, 0x58 };
  142. static const u8 int_debounce_register_offset[3] = { 0xa8, 0xc4, 0x64 };
  143. void ep93xx_gpio_update_int_params(unsigned port)
  144. {
  145. BUG_ON(port > 2);
  146. __raw_writeb(0, EP93XX_GPIO_REG(int_en_register_offset[port]));
  147. __raw_writeb(gpio_int_type2[port],
  148. EP93XX_GPIO_REG(int_type2_register_offset[port]));
  149. __raw_writeb(gpio_int_type1[port],
  150. EP93XX_GPIO_REG(int_type1_register_offset[port]));
  151. __raw_writeb(gpio_int_unmasked[port] & gpio_int_enabled[port],
  152. EP93XX_GPIO_REG(int_en_register_offset[port]));
  153. }
  154. void ep93xx_gpio_int_mask(unsigned line)
  155. {
  156. gpio_int_unmasked[line >> 3] &= ~(1 << (line & 7));
  157. }
  158. void ep93xx_gpio_int_debounce(unsigned int irq, int enable)
  159. {
  160. int line = irq_to_gpio(irq);
  161. int port = line >> 3;
  162. int port_mask = 1 << (line & 7);
  163. if (enable)
  164. gpio_int_debouce[port] |= port_mask;
  165. else
  166. gpio_int_debouce[port] &= ~port_mask;
  167. __raw_writeb(gpio_int_debouce[port],
  168. EP93XX_GPIO_REG(int_debounce_register_offset[port]));
  169. }
  170. EXPORT_SYMBOL(ep93xx_gpio_int_debounce);
  171. /*************************************************************************
  172. * EP93xx IRQ handling
  173. *************************************************************************/
  174. static void ep93xx_gpio_ab_irq_handler(unsigned int irq, struct irq_desc *desc)
  175. {
  176. unsigned char status;
  177. int i;
  178. status = __raw_readb(EP93XX_GPIO_A_INT_STATUS);
  179. for (i = 0; i < 8; i++) {
  180. if (status & (1 << i)) {
  181. int gpio_irq = gpio_to_irq(EP93XX_GPIO_LINE_A(0)) + i;
  182. generic_handle_irq(gpio_irq);
  183. }
  184. }
  185. status = __raw_readb(EP93XX_GPIO_B_INT_STATUS);
  186. for (i = 0; i < 8; i++) {
  187. if (status & (1 << i)) {
  188. int gpio_irq = gpio_to_irq(EP93XX_GPIO_LINE_B(0)) + i;
  189. desc = irq_desc + gpio_irq;
  190. generic_handle_irq(gpio_irq);
  191. }
  192. }
  193. }
  194. static void ep93xx_gpio_f_irq_handler(unsigned int irq, struct irq_desc *desc)
  195. {
  196. /*
  197. * map discontiguous hw irq range to continous sw irq range:
  198. *
  199. * IRQ_EP93XX_GPIO{0..7}MUX -> gpio_to_irq(EP93XX_GPIO_LINE_F({0..7})
  200. */
  201. int port_f_idx = ((irq + 1) & 7) ^ 4; /* {19..22,47..50} -> {0..7} */
  202. int gpio_irq = gpio_to_irq(EP93XX_GPIO_LINE_F(0)) + port_f_idx;
  203. generic_handle_irq(gpio_irq);
  204. }
  205. static void ep93xx_gpio_irq_ack(unsigned int irq)
  206. {
  207. int line = irq_to_gpio(irq);
  208. int port = line >> 3;
  209. int port_mask = 1 << (line & 7);
  210. if ((irq_desc[irq].status & IRQ_TYPE_SENSE_MASK) == IRQ_TYPE_EDGE_BOTH) {
  211. gpio_int_type2[port] ^= port_mask; /* switch edge direction */
  212. ep93xx_gpio_update_int_params(port);
  213. }
  214. __raw_writeb(port_mask, EP93XX_GPIO_REG(eoi_register_offset[port]));
  215. }
  216. static void ep93xx_gpio_irq_mask_ack(unsigned int irq)
  217. {
  218. int line = irq_to_gpio(irq);
  219. int port = line >> 3;
  220. int port_mask = 1 << (line & 7);
  221. if ((irq_desc[irq].status & IRQ_TYPE_SENSE_MASK) == IRQ_TYPE_EDGE_BOTH)
  222. gpio_int_type2[port] ^= port_mask; /* switch edge direction */
  223. gpio_int_unmasked[port] &= ~port_mask;
  224. ep93xx_gpio_update_int_params(port);
  225. __raw_writeb(port_mask, EP93XX_GPIO_REG(eoi_register_offset[port]));
  226. }
  227. static void ep93xx_gpio_irq_mask(unsigned int irq)
  228. {
  229. int line = irq_to_gpio(irq);
  230. int port = line >> 3;
  231. gpio_int_unmasked[port] &= ~(1 << (line & 7));
  232. ep93xx_gpio_update_int_params(port);
  233. }
  234. static void ep93xx_gpio_irq_unmask(unsigned int irq)
  235. {
  236. int line = irq_to_gpio(irq);
  237. int port = line >> 3;
  238. gpio_int_unmasked[port] |= 1 << (line & 7);
  239. ep93xx_gpio_update_int_params(port);
  240. }
  241. /*
  242. * gpio_int_type1 controls whether the interrupt is level (0) or
  243. * edge (1) triggered, while gpio_int_type2 controls whether it
  244. * triggers on low/falling (0) or high/rising (1).
  245. */
  246. static int ep93xx_gpio_irq_type(unsigned int irq, unsigned int type)
  247. {
  248. struct irq_desc *desc = irq_desc + irq;
  249. const int gpio = irq_to_gpio(irq);
  250. const int port = gpio >> 3;
  251. const int port_mask = 1 << (gpio & 7);
  252. gpio_direction_input(gpio);
  253. switch (type) {
  254. case IRQ_TYPE_EDGE_RISING:
  255. gpio_int_type1[port] |= port_mask;
  256. gpio_int_type2[port] |= port_mask;
  257. desc->handle_irq = handle_edge_irq;
  258. break;
  259. case IRQ_TYPE_EDGE_FALLING:
  260. gpio_int_type1[port] |= port_mask;
  261. gpio_int_type2[port] &= ~port_mask;
  262. desc->handle_irq = handle_edge_irq;
  263. break;
  264. case IRQ_TYPE_LEVEL_HIGH:
  265. gpio_int_type1[port] &= ~port_mask;
  266. gpio_int_type2[port] |= port_mask;
  267. desc->handle_irq = handle_level_irq;
  268. break;
  269. case IRQ_TYPE_LEVEL_LOW:
  270. gpio_int_type1[port] &= ~port_mask;
  271. gpio_int_type2[port] &= ~port_mask;
  272. desc->handle_irq = handle_level_irq;
  273. break;
  274. case IRQ_TYPE_EDGE_BOTH:
  275. gpio_int_type1[port] |= port_mask;
  276. /* set initial polarity based on current input level */
  277. if (gpio_get_value(gpio))
  278. gpio_int_type2[port] &= ~port_mask; /* falling */
  279. else
  280. gpio_int_type2[port] |= port_mask; /* rising */
  281. desc->handle_irq = handle_edge_irq;
  282. break;
  283. default:
  284. pr_err("ep93xx: failed to set irq type %d for gpio %d\n",
  285. type, gpio);
  286. return -EINVAL;
  287. }
  288. gpio_int_enabled[port] |= port_mask;
  289. desc->status &= ~IRQ_TYPE_SENSE_MASK;
  290. desc->status |= type & IRQ_TYPE_SENSE_MASK;
  291. ep93xx_gpio_update_int_params(port);
  292. return 0;
  293. }
  294. static struct irq_chip ep93xx_gpio_irq_chip = {
  295. .name = "GPIO",
  296. .ack = ep93xx_gpio_irq_ack,
  297. .mask_ack = ep93xx_gpio_irq_mask_ack,
  298. .mask = ep93xx_gpio_irq_mask,
  299. .unmask = ep93xx_gpio_irq_unmask,
  300. .set_type = ep93xx_gpio_irq_type,
  301. };
  302. void __init ep93xx_init_irq(void)
  303. {
  304. int gpio_irq;
  305. vic_init((void *)EP93XX_VIC1_BASE, 0, EP93XX_VIC1_VALID_IRQ_MASK);
  306. vic_init((void *)EP93XX_VIC2_BASE, 32, EP93XX_VIC2_VALID_IRQ_MASK);
  307. for (gpio_irq = gpio_to_irq(0);
  308. gpio_irq <= gpio_to_irq(EP93XX_GPIO_LINE_MAX_IRQ); ++gpio_irq) {
  309. set_irq_chip(gpio_irq, &ep93xx_gpio_irq_chip);
  310. set_irq_handler(gpio_irq, handle_level_irq);
  311. set_irq_flags(gpio_irq, IRQF_VALID);
  312. }
  313. set_irq_chained_handler(IRQ_EP93XX_GPIO_AB, ep93xx_gpio_ab_irq_handler);
  314. set_irq_chained_handler(IRQ_EP93XX_GPIO0MUX, ep93xx_gpio_f_irq_handler);
  315. set_irq_chained_handler(IRQ_EP93XX_GPIO1MUX, ep93xx_gpio_f_irq_handler);
  316. set_irq_chained_handler(IRQ_EP93XX_GPIO2MUX, ep93xx_gpio_f_irq_handler);
  317. set_irq_chained_handler(IRQ_EP93XX_GPIO3MUX, ep93xx_gpio_f_irq_handler);
  318. set_irq_chained_handler(IRQ_EP93XX_GPIO4MUX, ep93xx_gpio_f_irq_handler);
  319. set_irq_chained_handler(IRQ_EP93XX_GPIO5MUX, ep93xx_gpio_f_irq_handler);
  320. set_irq_chained_handler(IRQ_EP93XX_GPIO6MUX, ep93xx_gpio_f_irq_handler);
  321. set_irq_chained_handler(IRQ_EP93XX_GPIO7MUX, ep93xx_gpio_f_irq_handler);
  322. }
  323. /*************************************************************************
  324. * EP93xx peripheral handling
  325. *************************************************************************/
  326. #define EP93XX_UART_MCR_OFFSET (0x0100)
  327. static void ep93xx_uart_set_mctrl(struct amba_device *dev,
  328. void __iomem *base, unsigned int mctrl)
  329. {
  330. unsigned int mcr;
  331. mcr = 0;
  332. if (!(mctrl & TIOCM_RTS))
  333. mcr |= 2;
  334. if (!(mctrl & TIOCM_DTR))
  335. mcr |= 1;
  336. __raw_writel(mcr, base + EP93XX_UART_MCR_OFFSET);
  337. }
  338. static struct amba_pl010_data ep93xx_uart_data = {
  339. .set_mctrl = ep93xx_uart_set_mctrl,
  340. };
  341. static struct amba_device uart1_device = {
  342. .dev = {
  343. .init_name = "apb:uart1",
  344. .platform_data = &ep93xx_uart_data,
  345. },
  346. .res = {
  347. .start = EP93XX_UART1_PHYS_BASE,
  348. .end = EP93XX_UART1_PHYS_BASE + 0x0fff,
  349. .flags = IORESOURCE_MEM,
  350. },
  351. .irq = { IRQ_EP93XX_UART1, NO_IRQ },
  352. .periphid = 0x00041010,
  353. };
  354. static struct amba_device uart2_device = {
  355. .dev = {
  356. .init_name = "apb:uart2",
  357. .platform_data = &ep93xx_uart_data,
  358. },
  359. .res = {
  360. .start = EP93XX_UART2_PHYS_BASE,
  361. .end = EP93XX_UART2_PHYS_BASE + 0x0fff,
  362. .flags = IORESOURCE_MEM,
  363. },
  364. .irq = { IRQ_EP93XX_UART2, NO_IRQ },
  365. .periphid = 0x00041010,
  366. };
  367. static struct amba_device uart3_device = {
  368. .dev = {
  369. .init_name = "apb:uart3",
  370. .platform_data = &ep93xx_uart_data,
  371. },
  372. .res = {
  373. .start = EP93XX_UART3_PHYS_BASE,
  374. .end = EP93XX_UART3_PHYS_BASE + 0x0fff,
  375. .flags = IORESOURCE_MEM,
  376. },
  377. .irq = { IRQ_EP93XX_UART3, NO_IRQ },
  378. .periphid = 0x00041010,
  379. };
  380. static struct platform_device ep93xx_rtc_device = {
  381. .name = "ep93xx-rtc",
  382. .id = -1,
  383. .num_resources = 0,
  384. };
  385. static struct resource ep93xx_ohci_resources[] = {
  386. [0] = {
  387. .start = EP93XX_USB_PHYS_BASE,
  388. .end = EP93XX_USB_PHYS_BASE + 0x0fff,
  389. .flags = IORESOURCE_MEM,
  390. },
  391. [1] = {
  392. .start = IRQ_EP93XX_USB,
  393. .end = IRQ_EP93XX_USB,
  394. .flags = IORESOURCE_IRQ,
  395. },
  396. };
  397. static struct platform_device ep93xx_ohci_device = {
  398. .name = "ep93xx-ohci",
  399. .id = -1,
  400. .dev = {
  401. .dma_mask = &ep93xx_ohci_device.dev.coherent_dma_mask,
  402. .coherent_dma_mask = DMA_BIT_MASK(32),
  403. },
  404. .num_resources = ARRAY_SIZE(ep93xx_ohci_resources),
  405. .resource = ep93xx_ohci_resources,
  406. };
  407. static struct ep93xx_eth_data ep93xx_eth_data;
  408. static struct resource ep93xx_eth_resource[] = {
  409. {
  410. .start = EP93XX_ETHERNET_PHYS_BASE,
  411. .end = EP93XX_ETHERNET_PHYS_BASE + 0xffff,
  412. .flags = IORESOURCE_MEM,
  413. }, {
  414. .start = IRQ_EP93XX_ETHERNET,
  415. .end = IRQ_EP93XX_ETHERNET,
  416. .flags = IORESOURCE_IRQ,
  417. }
  418. };
  419. static struct platform_device ep93xx_eth_device = {
  420. .name = "ep93xx-eth",
  421. .id = -1,
  422. .dev = {
  423. .platform_data = &ep93xx_eth_data,
  424. },
  425. .num_resources = ARRAY_SIZE(ep93xx_eth_resource),
  426. .resource = ep93xx_eth_resource,
  427. };
  428. void __init ep93xx_register_eth(struct ep93xx_eth_data *data, int copy_addr)
  429. {
  430. if (copy_addr) {
  431. memcpy(data->dev_addr,
  432. (void *)(EP93XX_ETHERNET_BASE + 0x50), 6);
  433. }
  434. ep93xx_eth_data = *data;
  435. platform_device_register(&ep93xx_eth_device);
  436. }
  437. static struct i2c_gpio_platform_data ep93xx_i2c_data = {
  438. .sda_pin = EP93XX_GPIO_LINE_EEDAT,
  439. .sda_is_open_drain = 0,
  440. .scl_pin = EP93XX_GPIO_LINE_EECLK,
  441. .scl_is_open_drain = 0,
  442. .udelay = 2,
  443. };
  444. static struct platform_device ep93xx_i2c_device = {
  445. .name = "i2c-gpio",
  446. .id = 0,
  447. .dev.platform_data = &ep93xx_i2c_data,
  448. };
  449. void __init ep93xx_register_i2c(struct i2c_board_info *devices, int num)
  450. {
  451. i2c_register_board_info(0, devices, num);
  452. platform_device_register(&ep93xx_i2c_device);
  453. }
  454. extern void ep93xx_gpio_init(void);
  455. void __init ep93xx_init_devices(void)
  456. {
  457. unsigned int v;
  458. /*
  459. * Disallow access to MaverickCrunch initially.
  460. */
  461. v = __raw_readl(EP93XX_SYSCON_DEVICE_CONFIG);
  462. v &= ~EP93XX_SYSCON_DEVICE_CONFIG_CRUNCH_ENABLE;
  463. __raw_writel(0xaa, EP93XX_SYSCON_SWLOCK);
  464. __raw_writel(v, EP93XX_SYSCON_DEVICE_CONFIG);
  465. ep93xx_gpio_init();
  466. amba_device_register(&uart1_device, &iomem_resource);
  467. amba_device_register(&uart2_device, &iomem_resource);
  468. amba_device_register(&uart3_device, &iomem_resource);
  469. platform_device_register(&ep93xx_rtc_device);
  470. platform_device_register(&ep93xx_ohci_device);
  471. }