common.c 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494
  1. /*
  2. * arch/arm/mach-ixp4xx/common.c
  3. *
  4. * Generic code shared across all IXP4XX platforms
  5. *
  6. * Maintainer: Deepak Saxena <dsaxena@plexity.net>
  7. *
  8. * Copyright 2002 (c) Intel Corporation
  9. * Copyright 2003-2004 (c) MontaVista, Software, Inc.
  10. *
  11. * This file is licensed under the terms of the GNU General Public
  12. * License version 2. This program is licensed "as is" without any
  13. * warranty of any kind, whether express or implied.
  14. */
  15. #include <linux/kernel.h>
  16. #include <linux/mm.h>
  17. #include <linux/init.h>
  18. #include <linux/serial.h>
  19. #include <linux/sched.h>
  20. #include <linux/tty.h>
  21. #include <linux/platform_device.h>
  22. #include <linux/serial_core.h>
  23. #include <linux/bootmem.h>
  24. #include <linux/interrupt.h>
  25. #include <linux/bitops.h>
  26. #include <linux/time.h>
  27. #include <linux/timex.h>
  28. #include <linux/clocksource.h>
  29. #include <linux/clockchips.h>
  30. #include <linux/io.h>
  31. #include <mach/udc.h>
  32. #include <mach/hardware.h>
  33. #include <asm/uaccess.h>
  34. #include <asm/pgtable.h>
  35. #include <asm/page.h>
  36. #include <asm/irq.h>
  37. #include <asm/mach/map.h>
  38. #include <asm/mach/irq.h>
  39. #include <asm/mach/time.h>
  40. static int __init ixp4xx_clocksource_init(void);
  41. static int __init ixp4xx_clockevent_init(void);
  42. static struct clock_event_device clockevent_ixp4xx;
  43. /*************************************************************************
  44. * IXP4xx chipset I/O mapping
  45. *************************************************************************/
  46. static struct map_desc ixp4xx_io_desc[] __initdata = {
  47. { /* UART, Interrupt ctrl, GPIO, timers, NPEs, MACs, USB .... */
  48. .virtual = IXP4XX_PERIPHERAL_BASE_VIRT,
  49. .pfn = __phys_to_pfn(IXP4XX_PERIPHERAL_BASE_PHYS),
  50. .length = IXP4XX_PERIPHERAL_REGION_SIZE,
  51. .type = MT_DEVICE
  52. }, { /* Expansion Bus Config Registers */
  53. .virtual = IXP4XX_EXP_CFG_BASE_VIRT,
  54. .pfn = __phys_to_pfn(IXP4XX_EXP_CFG_BASE_PHYS),
  55. .length = IXP4XX_EXP_CFG_REGION_SIZE,
  56. .type = MT_DEVICE
  57. }, { /* PCI Registers */
  58. .virtual = IXP4XX_PCI_CFG_BASE_VIRT,
  59. .pfn = __phys_to_pfn(IXP4XX_PCI_CFG_BASE_PHYS),
  60. .length = IXP4XX_PCI_CFG_REGION_SIZE,
  61. .type = MT_DEVICE
  62. },
  63. #ifdef CONFIG_DEBUG_LL
  64. { /* Debug UART mapping */
  65. .virtual = IXP4XX_DEBUG_UART_BASE_VIRT,
  66. .pfn = __phys_to_pfn(IXP4XX_DEBUG_UART_BASE_PHYS),
  67. .length = IXP4XX_DEBUG_UART_REGION_SIZE,
  68. .type = MT_DEVICE
  69. }
  70. #endif
  71. };
  72. void __init ixp4xx_map_io(void)
  73. {
  74. iotable_init(ixp4xx_io_desc, ARRAY_SIZE(ixp4xx_io_desc));
  75. }
  76. /*************************************************************************
  77. * IXP4xx chipset IRQ handling
  78. *
  79. * TODO: GPIO IRQs should be marked invalid until the user of the IRQ
  80. * (be it PCI or something else) configures that GPIO line
  81. * as an IRQ.
  82. **************************************************************************/
  83. enum ixp4xx_irq_type {
  84. IXP4XX_IRQ_LEVEL, IXP4XX_IRQ_EDGE
  85. };
  86. /* Each bit represents an IRQ: 1: edge-triggered, 0: level triggered */
  87. static unsigned long long ixp4xx_irq_edge = 0;
  88. /*
  89. * IRQ -> GPIO mapping table
  90. */
  91. static signed char irq2gpio[32] = {
  92. -1, -1, -1, -1, -1, -1, 0, 1,
  93. -1, -1, -1, -1, -1, -1, -1, -1,
  94. -1, -1, -1, 2, 3, 4, 5, 6,
  95. 7, 8, 9, 10, 11, 12, -1, -1,
  96. };
  97. int gpio_to_irq(int gpio)
  98. {
  99. int irq;
  100. for (irq = 0; irq < 32; irq++) {
  101. if (irq2gpio[irq] == gpio)
  102. return irq;
  103. }
  104. return -EINVAL;
  105. }
  106. EXPORT_SYMBOL(gpio_to_irq);
  107. int irq_to_gpio(int irq)
  108. {
  109. int gpio = (irq < 32) ? irq2gpio[irq] : -EINVAL;
  110. if (gpio == -1)
  111. return -EINVAL;
  112. return gpio;
  113. }
  114. EXPORT_SYMBOL(irq_to_gpio);
  115. static int ixp4xx_set_irq_type(unsigned int irq, unsigned int type)
  116. {
  117. int line = irq2gpio[irq];
  118. u32 int_style;
  119. enum ixp4xx_irq_type irq_type;
  120. volatile u32 *int_reg;
  121. /*
  122. * Only for GPIO IRQs
  123. */
  124. if (line < 0)
  125. return -EINVAL;
  126. switch (type){
  127. case IRQ_TYPE_EDGE_BOTH:
  128. int_style = IXP4XX_GPIO_STYLE_TRANSITIONAL;
  129. irq_type = IXP4XX_IRQ_EDGE;
  130. break;
  131. case IRQ_TYPE_EDGE_RISING:
  132. int_style = IXP4XX_GPIO_STYLE_RISING_EDGE;
  133. irq_type = IXP4XX_IRQ_EDGE;
  134. break;
  135. case IRQ_TYPE_EDGE_FALLING:
  136. int_style = IXP4XX_GPIO_STYLE_FALLING_EDGE;
  137. irq_type = IXP4XX_IRQ_EDGE;
  138. break;
  139. case IRQ_TYPE_LEVEL_HIGH:
  140. int_style = IXP4XX_GPIO_STYLE_ACTIVE_HIGH;
  141. irq_type = IXP4XX_IRQ_LEVEL;
  142. break;
  143. case IRQ_TYPE_LEVEL_LOW:
  144. int_style = IXP4XX_GPIO_STYLE_ACTIVE_LOW;
  145. irq_type = IXP4XX_IRQ_LEVEL;
  146. break;
  147. default:
  148. return -EINVAL;
  149. }
  150. if (irq_type == IXP4XX_IRQ_EDGE)
  151. ixp4xx_irq_edge |= (1 << irq);
  152. else
  153. ixp4xx_irq_edge &= ~(1 << irq);
  154. if (line >= 8) { /* pins 8-15 */
  155. line -= 8;
  156. int_reg = IXP4XX_GPIO_GPIT2R;
  157. } else { /* pins 0-7 */
  158. int_reg = IXP4XX_GPIO_GPIT1R;
  159. }
  160. /* Clear the style for the appropriate pin */
  161. *int_reg &= ~(IXP4XX_GPIO_STYLE_CLEAR <<
  162. (line * IXP4XX_GPIO_STYLE_SIZE));
  163. *IXP4XX_GPIO_GPISR = (1 << line);
  164. /* Set the new style */
  165. *int_reg |= (int_style << (line * IXP4XX_GPIO_STYLE_SIZE));
  166. /* Configure the line as an input */
  167. gpio_line_config(irq2gpio[irq], IXP4XX_GPIO_IN);
  168. return 0;
  169. }
  170. static void ixp4xx_irq_mask(unsigned int irq)
  171. {
  172. if ((cpu_is_ixp46x() || cpu_is_ixp43x()) && irq >= 32)
  173. *IXP4XX_ICMR2 &= ~(1 << (irq - 32));
  174. else
  175. *IXP4XX_ICMR &= ~(1 << irq);
  176. }
  177. static void ixp4xx_irq_ack(unsigned int irq)
  178. {
  179. int line = (irq < 32) ? irq2gpio[irq] : -1;
  180. if (line >= 0)
  181. *IXP4XX_GPIO_GPISR = (1 << line);
  182. }
  183. /*
  184. * Level triggered interrupts on GPIO lines can only be cleared when the
  185. * interrupt condition disappears.
  186. */
  187. static void ixp4xx_irq_unmask(unsigned int irq)
  188. {
  189. if (!(ixp4xx_irq_edge & (1 << irq)))
  190. ixp4xx_irq_ack(irq);
  191. if ((cpu_is_ixp46x() || cpu_is_ixp43x()) && irq >= 32)
  192. *IXP4XX_ICMR2 |= (1 << (irq - 32));
  193. else
  194. *IXP4XX_ICMR |= (1 << irq);
  195. }
  196. static struct irq_chip ixp4xx_irq_chip = {
  197. .name = "IXP4xx",
  198. .ack = ixp4xx_irq_ack,
  199. .mask = ixp4xx_irq_mask,
  200. .unmask = ixp4xx_irq_unmask,
  201. .set_type = ixp4xx_set_irq_type,
  202. };
  203. void __init ixp4xx_init_irq(void)
  204. {
  205. int i = 0;
  206. /* Route all sources to IRQ instead of FIQ */
  207. *IXP4XX_ICLR = 0x0;
  208. /* Disable all interrupt */
  209. *IXP4XX_ICMR = 0x0;
  210. if (cpu_is_ixp46x() || cpu_is_ixp43x()) {
  211. /* Route upper 32 sources to IRQ instead of FIQ */
  212. *IXP4XX_ICLR2 = 0x00;
  213. /* Disable upper 32 interrupts */
  214. *IXP4XX_ICMR2 = 0x00;
  215. }
  216. /* Default to all level triggered */
  217. for(i = 0; i < NR_IRQS; i++) {
  218. set_irq_chip(i, &ixp4xx_irq_chip);
  219. set_irq_handler(i, handle_level_irq);
  220. set_irq_flags(i, IRQF_VALID);
  221. }
  222. }
  223. /*************************************************************************
  224. * IXP4xx timer tick
  225. * We use OS timer1 on the CPU for the timer tick and the timestamp
  226. * counter as a source of real clock ticks to account for missed jiffies.
  227. *************************************************************************/
  228. static irqreturn_t ixp4xx_timer_interrupt(int irq, void *dev_id)
  229. {
  230. struct clock_event_device *evt = &clockevent_ixp4xx;
  231. /* Clear Pending Interrupt by writing '1' to it */
  232. *IXP4XX_OSST = IXP4XX_OSST_TIMER_1_PEND;
  233. evt->event_handler(evt);
  234. return IRQ_HANDLED;
  235. }
  236. static struct irqaction ixp4xx_timer_irq = {
  237. .name = "timer1",
  238. .flags = IRQF_DISABLED | IRQF_TIMER | IRQF_IRQPOLL,
  239. .handler = ixp4xx_timer_interrupt,
  240. };
  241. void __init ixp4xx_timer_init(void)
  242. {
  243. /* Reset/disable counter */
  244. *IXP4XX_OSRT1 = 0;
  245. /* Clear Pending Interrupt by writing '1' to it */
  246. *IXP4XX_OSST = IXP4XX_OSST_TIMER_1_PEND;
  247. /* Reset time-stamp counter */
  248. *IXP4XX_OSTS = 0;
  249. /* Connect the interrupt handler and enable the interrupt */
  250. setup_irq(IRQ_IXP4XX_TIMER1, &ixp4xx_timer_irq);
  251. ixp4xx_clocksource_init();
  252. ixp4xx_clockevent_init();
  253. }
  254. struct sys_timer ixp4xx_timer = {
  255. .init = ixp4xx_timer_init,
  256. };
  257. static struct pxa2xx_udc_mach_info ixp4xx_udc_info;
  258. void __init ixp4xx_set_udc_info(struct pxa2xx_udc_mach_info *info)
  259. {
  260. memcpy(&ixp4xx_udc_info, info, sizeof *info);
  261. }
  262. static struct resource ixp4xx_udc_resources[] = {
  263. [0] = {
  264. .start = 0xc800b000,
  265. .end = 0xc800bfff,
  266. .flags = IORESOURCE_MEM,
  267. },
  268. [1] = {
  269. .start = IRQ_IXP4XX_USB,
  270. .end = IRQ_IXP4XX_USB,
  271. .flags = IORESOURCE_IRQ,
  272. },
  273. };
  274. /*
  275. * USB device controller. The IXP4xx uses the same controller as PXA25X,
  276. * so we just use the same device.
  277. */
  278. static struct platform_device ixp4xx_udc_device = {
  279. .name = "pxa25x-udc",
  280. .id = -1,
  281. .num_resources = 2,
  282. .resource = ixp4xx_udc_resources,
  283. .dev = {
  284. .platform_data = &ixp4xx_udc_info,
  285. },
  286. };
  287. static struct platform_device *ixp4xx_devices[] __initdata = {
  288. &ixp4xx_udc_device,
  289. };
  290. static struct resource ixp46x_i2c_resources[] = {
  291. [0] = {
  292. .start = 0xc8011000,
  293. .end = 0xc801101c,
  294. .flags = IORESOURCE_MEM,
  295. },
  296. [1] = {
  297. .start = IRQ_IXP4XX_I2C,
  298. .end = IRQ_IXP4XX_I2C,
  299. .flags = IORESOURCE_IRQ
  300. }
  301. };
  302. /*
  303. * I2C controller. The IXP46x uses the same block as the IOP3xx, so
  304. * we just use the same device name.
  305. */
  306. static struct platform_device ixp46x_i2c_controller = {
  307. .name = "IOP3xx-I2C",
  308. .id = 0,
  309. .num_resources = 2,
  310. .resource = ixp46x_i2c_resources
  311. };
  312. static struct platform_device *ixp46x_devices[] __initdata = {
  313. &ixp46x_i2c_controller
  314. };
  315. unsigned long ixp4xx_exp_bus_size;
  316. EXPORT_SYMBOL(ixp4xx_exp_bus_size);
  317. void __init ixp4xx_sys_init(void)
  318. {
  319. ixp4xx_exp_bus_size = SZ_16M;
  320. platform_add_devices(ixp4xx_devices, ARRAY_SIZE(ixp4xx_devices));
  321. if (cpu_is_ixp46x()) {
  322. int region;
  323. platform_add_devices(ixp46x_devices,
  324. ARRAY_SIZE(ixp46x_devices));
  325. for (region = 0; region < 7; region++) {
  326. if((*(IXP4XX_EXP_REG(0x4 * region)) & 0x200)) {
  327. ixp4xx_exp_bus_size = SZ_32M;
  328. break;
  329. }
  330. }
  331. }
  332. printk("IXP4xx: Using %luMiB expansion bus window size\n",
  333. ixp4xx_exp_bus_size >> 20);
  334. }
  335. /*
  336. * clocksource
  337. */
  338. cycle_t ixp4xx_get_cycles(struct clocksource *cs)
  339. {
  340. return *IXP4XX_OSTS;
  341. }
  342. static struct clocksource clocksource_ixp4xx = {
  343. .name = "OSTS",
  344. .rating = 200,
  345. .read = ixp4xx_get_cycles,
  346. .mask = CLOCKSOURCE_MASK(32),
  347. .shift = 20,
  348. .flags = CLOCK_SOURCE_IS_CONTINUOUS,
  349. };
  350. unsigned long ixp4xx_timer_freq = FREQ;
  351. static int __init ixp4xx_clocksource_init(void)
  352. {
  353. clocksource_ixp4xx.mult =
  354. clocksource_hz2mult(ixp4xx_timer_freq,
  355. clocksource_ixp4xx.shift);
  356. clocksource_register(&clocksource_ixp4xx);
  357. return 0;
  358. }
  359. /*
  360. * clockevents
  361. */
  362. static int ixp4xx_set_next_event(unsigned long evt,
  363. struct clock_event_device *unused)
  364. {
  365. unsigned long opts = *IXP4XX_OSRT1 & IXP4XX_OST_RELOAD_MASK;
  366. *IXP4XX_OSRT1 = (evt & ~IXP4XX_OST_RELOAD_MASK) | opts;
  367. return 0;
  368. }
  369. static void ixp4xx_set_mode(enum clock_event_mode mode,
  370. struct clock_event_device *evt)
  371. {
  372. unsigned long opts = *IXP4XX_OSRT1 & IXP4XX_OST_RELOAD_MASK;
  373. unsigned long osrt = *IXP4XX_OSRT1 & ~IXP4XX_OST_RELOAD_MASK;
  374. switch (mode) {
  375. case CLOCK_EVT_MODE_PERIODIC:
  376. osrt = LATCH & ~IXP4XX_OST_RELOAD_MASK;
  377. opts = IXP4XX_OST_ENABLE;
  378. break;
  379. case CLOCK_EVT_MODE_ONESHOT:
  380. /* period set by 'set next_event' */
  381. osrt = 0;
  382. opts = IXP4XX_OST_ENABLE | IXP4XX_OST_ONE_SHOT;
  383. break;
  384. case CLOCK_EVT_MODE_SHUTDOWN:
  385. opts &= ~IXP4XX_OST_ENABLE;
  386. break;
  387. case CLOCK_EVT_MODE_RESUME:
  388. opts |= IXP4XX_OST_ENABLE;
  389. break;
  390. case CLOCK_EVT_MODE_UNUSED:
  391. default:
  392. osrt = opts = 0;
  393. break;
  394. }
  395. *IXP4XX_OSRT1 = osrt | opts;
  396. }
  397. static struct clock_event_device clockevent_ixp4xx = {
  398. .name = "ixp4xx timer1",
  399. .features = CLOCK_EVT_FEAT_PERIODIC | CLOCK_EVT_FEAT_ONESHOT,
  400. .rating = 200,
  401. .shift = 24,
  402. .set_mode = ixp4xx_set_mode,
  403. .set_next_event = ixp4xx_set_next_event,
  404. };
  405. static int __init ixp4xx_clockevent_init(void)
  406. {
  407. clockevent_ixp4xx.mult = div_sc(FREQ, NSEC_PER_SEC,
  408. clockevent_ixp4xx.shift);
  409. clockevent_ixp4xx.max_delta_ns =
  410. clockevent_delta2ns(0xfffffffe, &clockevent_ixp4xx);
  411. clockevent_ixp4xx.min_delta_ns =
  412. clockevent_delta2ns(0xf, &clockevent_ixp4xx);
  413. clockevent_ixp4xx.cpumask = cpumask_of(0);
  414. clockevents_register_device(&clockevent_ixp4xx);
  415. return 0;
  416. }