common.c 14 KB

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