common.c 13 KB

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