common.c 14 KB

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