common.c 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584
  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 <linux/gpio.h>
  31. #include <linux/cpu.h>
  32. #include <mach/udc.h>
  33. #include <mach/hardware.h>
  34. #include <mach/io.h>
  35. #include <asm/uaccess.h>
  36. #include <asm/pgtable.h>
  37. #include <asm/page.h>
  38. #include <asm/irq.h>
  39. #include <asm/sched_clock.h>
  40. #include <asm/system_misc.h>
  41. #include <asm/mach/map.h>
  42. #include <asm/mach/irq.h>
  43. #include <asm/mach/time.h>
  44. static void __init ixp4xx_clocksource_init(void);
  45. static void __init ixp4xx_clockevent_init(void);
  46. static struct clock_event_device clockevent_ixp4xx;
  47. /*************************************************************************
  48. * IXP4xx chipset I/O mapping
  49. *************************************************************************/
  50. static struct map_desc ixp4xx_io_desc[] __initdata = {
  51. { /* UART, Interrupt ctrl, GPIO, timers, NPEs, MACs, USB .... */
  52. .virtual = (unsigned long)IXP4XX_PERIPHERAL_BASE_VIRT,
  53. .pfn = __phys_to_pfn(IXP4XX_PERIPHERAL_BASE_PHYS),
  54. .length = IXP4XX_PERIPHERAL_REGION_SIZE,
  55. .type = MT_DEVICE
  56. }, { /* Expansion Bus Config Registers */
  57. .virtual = (unsigned long)IXP4XX_EXP_CFG_BASE_VIRT,
  58. .pfn = __phys_to_pfn(IXP4XX_EXP_CFG_BASE_PHYS),
  59. .length = IXP4XX_EXP_CFG_REGION_SIZE,
  60. .type = MT_DEVICE
  61. }, { /* PCI Registers */
  62. .virtual = (unsigned long)IXP4XX_PCI_CFG_BASE_VIRT,
  63. .pfn = __phys_to_pfn(IXP4XX_PCI_CFG_BASE_PHYS),
  64. .length = IXP4XX_PCI_CFG_REGION_SIZE,
  65. .type = MT_DEVICE
  66. }, { /* Queue Manager */
  67. .virtual = (unsigned long)IXP4XX_QMGR_BASE_VIRT,
  68. .pfn = __phys_to_pfn(IXP4XX_QMGR_BASE_PHYS),
  69. .length = IXP4XX_QMGR_REGION_SIZE,
  70. .type = MT_DEVICE
  71. },
  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. static int ixp4xx_gpio_to_irq(struct gpio_chip *chip, unsigned 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. int irq_to_gpio(unsigned 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(struct irq_data *d, unsigned int type)
  116. {
  117. int line = irq2gpio[d->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 << d->irq);
  152. else
  153. ixp4xx_irq_edge &= ~(1 << d->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[d->irq], IXP4XX_GPIO_IN);
  168. return 0;
  169. }
  170. static void ixp4xx_irq_mask(struct irq_data *d)
  171. {
  172. if ((cpu_is_ixp46x() || cpu_is_ixp43x()) && d->irq >= 32)
  173. *IXP4XX_ICMR2 &= ~(1 << (d->irq - 32));
  174. else
  175. *IXP4XX_ICMR &= ~(1 << d->irq);
  176. }
  177. static void ixp4xx_irq_ack(struct irq_data *d)
  178. {
  179. int line = (d->irq < 32) ? irq2gpio[d->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(struct irq_data *d)
  188. {
  189. if (!(ixp4xx_irq_edge & (1 << d->irq)))
  190. ixp4xx_irq_ack(d);
  191. if ((cpu_is_ixp46x() || cpu_is_ixp43x()) && d->irq >= 32)
  192. *IXP4XX_ICMR2 |= (1 << (d->irq - 32));
  193. else
  194. *IXP4XX_ICMR |= (1 << d->irq);
  195. }
  196. static struct irq_chip ixp4xx_irq_chip = {
  197. .name = "IXP4xx",
  198. .irq_ack = ixp4xx_irq_ack,
  199. .irq_mask = ixp4xx_irq_mask,
  200. .irq_unmask = ixp4xx_irq_unmask,
  201. .irq_set_type = ixp4xx_set_irq_type,
  202. };
  203. void __init ixp4xx_init_irq(void)
  204. {
  205. int i = 0;
  206. /*
  207. * ixp4xx does not implement the XScale PWRMODE register
  208. * so it must not call cpu_do_idle().
  209. */
  210. cpu_idle_poll_ctrl(true);
  211. /* Route all sources to IRQ instead of FIQ */
  212. *IXP4XX_ICLR = 0x0;
  213. /* Disable all interrupt */
  214. *IXP4XX_ICMR = 0x0;
  215. if (cpu_is_ixp46x() || cpu_is_ixp43x()) {
  216. /* Route upper 32 sources to IRQ instead of FIQ */
  217. *IXP4XX_ICLR2 = 0x00;
  218. /* Disable upper 32 interrupts */
  219. *IXP4XX_ICMR2 = 0x00;
  220. }
  221. /* Default to all level triggered */
  222. for(i = 0; i < NR_IRQS; i++) {
  223. irq_set_chip_and_handler(i, &ixp4xx_irq_chip,
  224. handle_level_irq);
  225. set_irq_flags(i, IRQF_VALID);
  226. }
  227. }
  228. /*************************************************************************
  229. * IXP4xx timer tick
  230. * We use OS timer1 on the CPU for the timer tick and the timestamp
  231. * counter as a source of real clock ticks to account for missed jiffies.
  232. *************************************************************************/
  233. static irqreturn_t ixp4xx_timer_interrupt(int irq, void *dev_id)
  234. {
  235. struct clock_event_device *evt = dev_id;
  236. /* Clear Pending Interrupt by writing '1' to it */
  237. *IXP4XX_OSST = IXP4XX_OSST_TIMER_1_PEND;
  238. evt->event_handler(evt);
  239. return IRQ_HANDLED;
  240. }
  241. static struct irqaction ixp4xx_timer_irq = {
  242. .name = "timer1",
  243. .flags = IRQF_DISABLED | IRQF_TIMER | IRQF_IRQPOLL,
  244. .handler = ixp4xx_timer_interrupt,
  245. .dev_id = &clockevent_ixp4xx,
  246. };
  247. void __init ixp4xx_timer_init(void)
  248. {
  249. /* Reset/disable counter */
  250. *IXP4XX_OSRT1 = 0;
  251. /* Clear Pending Interrupt by writing '1' to it */
  252. *IXP4XX_OSST = IXP4XX_OSST_TIMER_1_PEND;
  253. /* Reset time-stamp counter */
  254. *IXP4XX_OSTS = 0;
  255. /* Connect the interrupt handler and enable the interrupt */
  256. setup_irq(IRQ_IXP4XX_TIMER1, &ixp4xx_timer_irq);
  257. ixp4xx_clocksource_init();
  258. ixp4xx_clockevent_init();
  259. }
  260. static struct pxa2xx_udc_mach_info ixp4xx_udc_info;
  261. void __init ixp4xx_set_udc_info(struct pxa2xx_udc_mach_info *info)
  262. {
  263. memcpy(&ixp4xx_udc_info, info, sizeof *info);
  264. }
  265. static struct resource ixp4xx_udc_resources[] = {
  266. [0] = {
  267. .start = 0xc800b000,
  268. .end = 0xc800bfff,
  269. .flags = IORESOURCE_MEM,
  270. },
  271. [1] = {
  272. .start = IRQ_IXP4XX_USB,
  273. .end = IRQ_IXP4XX_USB,
  274. .flags = IORESOURCE_IRQ,
  275. },
  276. };
  277. /*
  278. * USB device controller. The IXP4xx uses the same controller as PXA25X,
  279. * so we just use the same device.
  280. */
  281. static struct platform_device ixp4xx_udc_device = {
  282. .name = "pxa25x-udc",
  283. .id = -1,
  284. .num_resources = 2,
  285. .resource = ixp4xx_udc_resources,
  286. .dev = {
  287. .platform_data = &ixp4xx_udc_info,
  288. },
  289. };
  290. static struct platform_device *ixp4xx_devices[] __initdata = {
  291. &ixp4xx_udc_device,
  292. };
  293. static struct resource ixp46x_i2c_resources[] = {
  294. [0] = {
  295. .start = 0xc8011000,
  296. .end = 0xc801101c,
  297. .flags = IORESOURCE_MEM,
  298. },
  299. [1] = {
  300. .start = IRQ_IXP4XX_I2C,
  301. .end = IRQ_IXP4XX_I2C,
  302. .flags = IORESOURCE_IRQ
  303. }
  304. };
  305. /*
  306. * I2C controller. The IXP46x uses the same block as the IOP3xx, so
  307. * we just use the same device name.
  308. */
  309. static struct platform_device ixp46x_i2c_controller = {
  310. .name = "IOP3xx-I2C",
  311. .id = 0,
  312. .num_resources = 2,
  313. .resource = ixp46x_i2c_resources
  314. };
  315. static struct platform_device *ixp46x_devices[] __initdata = {
  316. &ixp46x_i2c_controller
  317. };
  318. unsigned long ixp4xx_exp_bus_size;
  319. EXPORT_SYMBOL(ixp4xx_exp_bus_size);
  320. static int ixp4xx_gpio_direction_input(struct gpio_chip *chip, unsigned gpio)
  321. {
  322. gpio_line_config(gpio, IXP4XX_GPIO_IN);
  323. return 0;
  324. }
  325. static int ixp4xx_gpio_direction_output(struct gpio_chip *chip, unsigned gpio,
  326. int level)
  327. {
  328. gpio_line_set(gpio, level);
  329. gpio_line_config(gpio, IXP4XX_GPIO_OUT);
  330. return 0;
  331. }
  332. static int ixp4xx_gpio_get_value(struct gpio_chip *chip, unsigned gpio)
  333. {
  334. int value;
  335. gpio_line_get(gpio, &value);
  336. return value;
  337. }
  338. static void ixp4xx_gpio_set_value(struct gpio_chip *chip, unsigned gpio,
  339. int value)
  340. {
  341. gpio_line_set(gpio, value);
  342. }
  343. static struct gpio_chip ixp4xx_gpio_chip = {
  344. .label = "IXP4XX_GPIO_CHIP",
  345. .direction_input = ixp4xx_gpio_direction_input,
  346. .direction_output = ixp4xx_gpio_direction_output,
  347. .get = ixp4xx_gpio_get_value,
  348. .set = ixp4xx_gpio_set_value,
  349. .to_irq = ixp4xx_gpio_to_irq,
  350. .base = 0,
  351. .ngpio = 16,
  352. };
  353. void __init ixp4xx_sys_init(void)
  354. {
  355. ixp4xx_exp_bus_size = SZ_16M;
  356. platform_add_devices(ixp4xx_devices, ARRAY_SIZE(ixp4xx_devices));
  357. gpiochip_add(&ixp4xx_gpio_chip);
  358. if (cpu_is_ixp46x()) {
  359. int region;
  360. platform_add_devices(ixp46x_devices,
  361. ARRAY_SIZE(ixp46x_devices));
  362. for (region = 0; region < 7; region++) {
  363. if((*(IXP4XX_EXP_REG(0x4 * region)) & 0x200)) {
  364. ixp4xx_exp_bus_size = SZ_32M;
  365. break;
  366. }
  367. }
  368. }
  369. printk("IXP4xx: Using %luMiB expansion bus window size\n",
  370. ixp4xx_exp_bus_size >> 20);
  371. }
  372. /*
  373. * sched_clock()
  374. */
  375. static u32 notrace ixp4xx_read_sched_clock(void)
  376. {
  377. return *IXP4XX_OSTS;
  378. }
  379. /*
  380. * clocksource
  381. */
  382. static cycle_t ixp4xx_clocksource_read(struct clocksource *c)
  383. {
  384. return *IXP4XX_OSTS;
  385. }
  386. unsigned long ixp4xx_timer_freq = IXP4XX_TIMER_FREQ;
  387. EXPORT_SYMBOL(ixp4xx_timer_freq);
  388. static void __init ixp4xx_clocksource_init(void)
  389. {
  390. setup_sched_clock(ixp4xx_read_sched_clock, 32, ixp4xx_timer_freq);
  391. clocksource_mmio_init(NULL, "OSTS", ixp4xx_timer_freq, 200, 32,
  392. ixp4xx_clocksource_read);
  393. }
  394. /*
  395. * clockevents
  396. */
  397. static int ixp4xx_set_next_event(unsigned long evt,
  398. struct clock_event_device *unused)
  399. {
  400. unsigned long opts = *IXP4XX_OSRT1 & IXP4XX_OST_RELOAD_MASK;
  401. *IXP4XX_OSRT1 = (evt & ~IXP4XX_OST_RELOAD_MASK) | opts;
  402. return 0;
  403. }
  404. static void ixp4xx_set_mode(enum clock_event_mode mode,
  405. struct clock_event_device *evt)
  406. {
  407. unsigned long opts = *IXP4XX_OSRT1 & IXP4XX_OST_RELOAD_MASK;
  408. unsigned long osrt = *IXP4XX_OSRT1 & ~IXP4XX_OST_RELOAD_MASK;
  409. switch (mode) {
  410. case CLOCK_EVT_MODE_PERIODIC:
  411. osrt = LATCH & ~IXP4XX_OST_RELOAD_MASK;
  412. opts = IXP4XX_OST_ENABLE;
  413. break;
  414. case CLOCK_EVT_MODE_ONESHOT:
  415. /* period set by 'set next_event' */
  416. osrt = 0;
  417. opts = IXP4XX_OST_ENABLE | IXP4XX_OST_ONE_SHOT;
  418. break;
  419. case CLOCK_EVT_MODE_SHUTDOWN:
  420. opts &= ~IXP4XX_OST_ENABLE;
  421. break;
  422. case CLOCK_EVT_MODE_RESUME:
  423. opts |= IXP4XX_OST_ENABLE;
  424. break;
  425. case CLOCK_EVT_MODE_UNUSED:
  426. default:
  427. osrt = opts = 0;
  428. break;
  429. }
  430. *IXP4XX_OSRT1 = osrt | opts;
  431. }
  432. static struct clock_event_device clockevent_ixp4xx = {
  433. .name = "ixp4xx timer1",
  434. .features = CLOCK_EVT_FEAT_PERIODIC | CLOCK_EVT_FEAT_ONESHOT,
  435. .rating = 200,
  436. .set_mode = ixp4xx_set_mode,
  437. .set_next_event = ixp4xx_set_next_event,
  438. };
  439. static void __init ixp4xx_clockevent_init(void)
  440. {
  441. clockevent_ixp4xx.cpumask = cpumask_of(0);
  442. clockevents_config_and_register(&clockevent_ixp4xx, IXP4XX_TIMER_FREQ,
  443. 0xf, 0xfffffffe);
  444. }
  445. void ixp4xx_restart(char mode, const char *cmd)
  446. {
  447. if ( 1 && mode == 's') {
  448. /* Jump into ROM at address 0 */
  449. soft_restart(0);
  450. } else {
  451. /* Use on-chip reset capability */
  452. /* set the "key" register to enable access to
  453. * "timer" and "enable" registers
  454. */
  455. *IXP4XX_OSWK = IXP4XX_WDT_KEY;
  456. /* write 0 to the timer register for an immediate reset */
  457. *IXP4XX_OSWT = 0;
  458. *IXP4XX_OSWE = IXP4XX_WDT_RESET_ENABLE | IXP4XX_WDT_COUNT_ENABLE;
  459. }
  460. }
  461. #ifdef CONFIG_IXP4XX_INDIRECT_PCI
  462. /*
  463. * In the case of using indirect PCI, we simply return the actual PCI
  464. * address and our read/write implementation use that to drive the
  465. * access registers. If something outside of PCI is ioremap'd, we
  466. * fallback to the default.
  467. */
  468. static void __iomem *ixp4xx_ioremap_caller(unsigned long addr, size_t size,
  469. unsigned int mtype, void *caller)
  470. {
  471. if (!is_pci_memory(addr))
  472. return __arm_ioremap_caller(addr, size, mtype, caller);
  473. return (void __iomem *)addr;
  474. }
  475. static void ixp4xx_iounmap(void __iomem *addr)
  476. {
  477. if (!is_pci_memory((__force u32)addr))
  478. __iounmap(addr);
  479. }
  480. void __init ixp4xx_init_early(void)
  481. {
  482. arch_ioremap_caller = ixp4xx_ioremap_caller;
  483. arch_iounmap = ixp4xx_iounmap;
  484. }
  485. #else
  486. void __init ixp4xx_init_early(void) {}
  487. #endif