common.c 12 KB

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