hiddenDangerAnalysis.c 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451
  1. /*
  2. * linux/arch/arm/mach-pxa/viper.c
  3. *
  4. * Support for the Arcom VIPER SBC.
  5. *
  6. * Author: Ian Campbell
  7. * Created: Feb 03, 2003
  8. * Copyright: Arcom Control Systems
  9. *
  10. * Maintained by Marc Zyngier <maz@misterjones.org>
  11. * <marc.zyngier@altran.com>
  12. *
  13. * Based on lubbock.c:
  14. * Author: Nicolas Pitre
  15. * Created: Jun 15, 2001
  16. * Copyright: MontaVista Software Inc.
  17. *
  18. * This program is free software; you can redistribute it and/or modify
  19. * it under the terms of the GNU General Public License version 2 as
  20. * published by the Free Software Foundation.
  21. */
  22. #include <linux/types.h>
  23. #include <linux/memory.h>
  24. #include <linux/cpu.h>
  25. #include <linux/cpufreq.h>
  26. #include <linux/delay.h>
  27. #include <linux/fs.h>
  28. #include <linux/init.h>
  29. #include <linux/slab.h>
  30. #include <linux/interrupt.h>
  31. #include <linux/major.h>
  32. #include <linux/module.h>
  33. #include <linux/pm.h>
  34. #include <linux/sched.h>
  35. #include <linux/gpio.h>
  36. #include <linux/jiffies.h>
  37. #include <linux/i2c-gpio.h>
  38. #include <linux/i2c/pxa-i2c.h>
  39. #include <linux/serial_8250.h>
  40. #include <linux/smc91x.h>
  41. #include <linux/pwm_backlight.h>
  42. #include <linux/usb/isp116x.h>
  43. #include <linux/mtd/mtd.h>
  44. #include <linux/mtd/partitions.h>
  45. #include <linux/mtd/physmap.h>
  46. #include <linux/syscore_ops.h>
  47. #include <mach/pxa25x.h>
  48. #include <mach/audio.h>
  49. #include <linux/platform_data/video-pxafb.h>
  50. #include <mach/regs-uart.h>
  51. #include <linux/platform_data/pcmcia-pxa2xx_viper.h>
  52. #include <mach/viper.h>
  53. #include <asm/setup.h>
  54. #include <asm/mach-types.h>
  55. #include <asm/irq.h>
  56. #include <asm/sizes.h>
  57. #include <asm/system_info.h>
  58. #include <asm/mach/arch.h>
  59. #include <asm/mach/map.h>
  60. #include <asm/mach/irq.h>
  61. #include "generic.h"
  62. #include "devices.h"
  63. static unsigned int icr;
  64. static void viper_icr_set_bit(unsigned int bit)
  65. {
  66. icr |= bit;
  67. VIPER_ICR = icr;
  68. }
  69. static void viper_icr_clear_bit(unsigned int bit)
  70. {
  71. icr &= ~bit;
  72. VIPER_ICR = icr;
  73. }
  74. /* This function is used from the pcmcia module to reset the CF */
  75. static void viper_cf_reset(int state)
  76. {
  77. if (state)
  78. viper_icr_set_bit(VIPER_ICR_CF_RST);
  79. else
  80. viper_icr_clear_bit(VIPER_ICR_CF_RST);
  81. }
  82. static struct arcom_pcmcia_pdata viper_pcmcia_info = {
  83. .cd_gpio = VIPER_CF_CD_GPIO,
  84. .rdy_gpio = VIPER_CF_RDY_GPIO,
  85. .pwr_gpio = VIPER_CF_POWER_GPIO,
  86. .reset = viper_cf_reset,
  87. };
  88. static struct platform_device viper_pcmcia_device = {
  89. .name = "viper-pcmcia",
  90. .id = -1,
  91. .dev = {
  92. .platform_data = &viper_pcmcia_info,
  93. },
  94. };
  95. /*
  96. * The CPLD version register was not present on VIPER boards prior to
  97. * v2i1. On v1 boards where the version register is not present we
  98. * will just read back the previous value from the databus.
  99. *
  100. * Therefore we do two reads. The first time we write 0 to the
  101. * (read-only) register before reading and the second time we write
  102. * 0xff first. If the two reads do not match or they read back as 0xff
  103. * or 0x00 then we have version 1 hardware.
  104. */
  105. static u8 viper_hw_version(void)
  106. {
  107. u8 v1, v2;
  108. unsigned long flags;
  109. local_irq_save(flags);
  110. VIPER_VERSION = 0;
  111. v1 = VIPER_VERSION;
  112. VIPER_VERSION = 0xff;
  113. v2 = VIPER_VERSION;
  114. v1 = (v1 != v2 || v1 == 0xff) ? 0 : v1;
  115. local_irq_restore(flags);
  116. return v1;
  117. }
  118. /* CPU system core operations. */
  119. static int viper_cpu_suspend(void)
  120. {
  121. viper_icr_set_bit(VIPER_ICR_R_DIS);
  122. return 0;
  123. }
  124. static void viper_cpu_resume(void)
  125. {
  126. viper_icr_clear_bit(VIPER_ICR_R_DIS);
  127. }
  128. static struct syscore_ops viper_cpu_syscore_ops = {
  129. .suspend = viper_cpu_suspend,
  130. .resume = viper_cpu_resume,
  131. };
  132. static unsigned int current_voltage_divisor;
  133. /*
  134. * If force is not true then step from existing to new divisor. If
  135. * force is true then jump straight to the new divisor. Stepping is
  136. * used because if the jump in voltage is too large, the VCC can dip
  137. * too low and the regulator cuts out.
  138. *
  139. * force can be used to initialize the divisor to a know state by
  140. * setting the value for the current clock speed, since we are already
  141. * running at that speed we know the voltage should be pretty close so
  142. * the jump won't be too large
  143. */
  144. static void viper_set_core_cpu_voltage(unsigned long khz, int force)
  145. {
  146. int i = 0;
  147. unsigned int divisor = 0;
  148. const char *v;
  149. if (khz < 200000) {
  150. v = "1.0"; divisor = 0xfff;
  151. } else if (khz < 300000) {
  152. v = "1.1"; divisor = 0xde5;
  153. } else {
  154. v = "1.3"; divisor = 0x325;
  155. }
  156. pr_debug("viper: setting CPU core voltage to %sV at %d.%03dMHz\n",
  157. v, (int)khz / 1000, (int)khz % 1000);
  158. #define STEP 0x100
  159. do {
  160. int step;
  161. if (force)
  162. step = divisor;
  163. else if (current_voltage_divisor < divisor - STEP)
  164. step = current_voltage_divisor + STEP;
  165. else if (current_voltage_divisor > divisor + STEP)
  166. step = current_voltage_divisor - STEP;
  167. else
  168. step = divisor;
  169. force = 0;
  170. gpio_set_value(VIPER_PSU_CLK_GPIO, 0);
  171. gpio_set_value(VIPER_PSU_nCS_LD_GPIO, 0);
  172. for (i = 1 << 11 ; i > 0 ; i >>= 1) {
  173. udelay(1);
  174. gpio_set_value(VIPER_PSU_DATA_GPIO, step & i);
  175. udelay(1);
  176. gpio_set_value(VIPER_PSU_CLK_GPIO, 1);
  177. udelay(1);
  178. gpio_set_value(VIPER_PSU_CLK_GPIO, 0);
  179. }
  180. udelay(1);
  181. gpio_set_value(VIPER_PSU_nCS_LD_GPIO, 1);
  182. udelay(1);
  183. gpio_set_value(VIPER_PSU_nCS_LD_GPIO, 0);
  184. current_voltage_divisor = step;
  185. } while (current_voltage_divisor != divisor);
  186. }
  187. /* Interrupt handling */
  188. static unsigned long viper_irq_enabled_mask;
  189. static const int viper_isa_irqs[] = { 3, 4, 5, 6, 7, 10, 11, 12, 9, 14, 15 };
  190. static const int viper_isa_irq_map[] = {
  191. 0, /* ISA irq #0, invalid */
  192. 0, /* ISA irq #1, invalid */
  193. 0, /* ISA irq #2, invalid */
  194. 1 << 0, /* ISA irq #3 */
  195. 1 << 1, /* ISA irq #4 */
  196. 1 << 2, /* ISA irq #5 */
  197. 1 << 3, /* ISA irq #6 */
  198. 1 << 4, /* ISA irq #7 */
  199. 0, /* ISA irq #8, invalid */
  200. 1 << 8, /* ISA irq #9 */
  201. 1 << 5, /* ISA irq #10 */
  202. 1 << 6, /* ISA irq #11 */
  203. 1 << 7, /* ISA irq #12 */
  204. 0, /* ISA irq #13, invalid */
  205. 1 << 9, /* ISA irq #14 */
  206. 1 << 10, /* ISA irq #15 */
  207. };
  208. static inline int viper_irq_to_bitmask(unsigned int irq)
  209. {
  210. return viper_isa_irq_map[irq - PXA_ISA_IRQ(0)];
  211. }
  212. static inline int viper_bit_to_irq(int bit)
  213. {
  214. return viper_isa_irqs[bit] + PXA_ISA_IRQ(0);
  215. }
  216. static void viper_ack_irq(struct irq_data *d)
  217. {
  218. int viper_irq = viper_irq_to_bitmask(d->irq);
  219. if (viper_irq & 0xff)
  220. VIPER_LO_IRQ_STATUS = viper_irq;
  221. else
  222. VIPER_HI_IRQ_STATUS = (viper_irq >> 8);
  223. }
  224. static void viper_mask_irq(struct irq_data *d)
  225. {
  226. viper_irq_enabled_mask &= ~(viper_irq_to_bitmask(d->irq));
  227. }
  228. static void viper_unmask_irq(struct irq_data *d)
  229. {
  230. viper_irq_enabled_mask |= viper_irq_to_bitmask(d->irq);
  231. }
  232. static inline unsigned long viper_irq_pending(void)
  233. {
  234. return (VIPER_HI_IRQ_STATUS << 8 | VIPER_LO_IRQ_STATUS) &
  235. viper_irq_enabled_mask;
  236. }
  237. static void viper_irq_handler(unsigned int irq, struct irq_desc *desc)
  238. {
  239. unsigned long pending;
  240. pending = viper_irq_pending();
  241. do {
  242. /* we're in a chained irq handler,
  243. * so ack the interrupt by hand */
  244. desc->irq_data.chip->irq_ack(&desc->irq_data);
  245. if (likely(pending)) {
  246. irq = viper_bit_to_irq(__ffs(pending));
  247. generic_handle_irq(irq);
  248. }
  249. pending = viper_irq_pending();
  250. } while (pending);
  251. }
  252. static struct irq_chip viper_irq_chip = {
  253. .name = "ISA",
  254. .irq_ack = viper_ack_irq,
  255. .irq_mask = viper_mask_irq,
  256. .irq_unmask = viper_unmask_irq
  257. };
  258. static void __init viper_init_irq(void)
  259. {
  260. int level;
  261. int isa_irq;
  262. pxa25x_init_irq();
  263. /* setup ISA IRQs */
  264. for (level = 0; level < ARRAY_SIZE(viper_isa_irqs); level++) {
  265. isa_irq = viper_bit_to_irq(level);
  266. irq_set_chip_and_handler(isa_irq, &viper_irq_chip,
  267. handle_edge_irq);
  268. set_irq_flags(isa_irq, IRQF_VALID | IRQF_PROBE);
  269. }
  270. irq_set_chained_handler(gpio_to_irq(VIPER_CPLD_GPIO),
  271. viper_irq_handler);
  272. irq_set_irq_type(gpio_to_irq(VIPER_CPLD_GPIO), IRQ_TYPE_EDGE_BOTH);
  273. }
  274. /* Flat Panel */
  275. static struct pxafb_mode_info fb_mode_info[] = {
  276. {
  277. .pixclock = 157500,
  278. .xres = 320,
  279. .yres = 240,
  280. .bpp = 16,
  281. .hsync_len = 63,
  282. .left_margin = 7,
  283. .right_margin = 13,
  284. .vsync_len = 20,
  285. .upper_margin = 0,
  286. .lower_margin = 0,
  287. .sync = 0,
  288. },
  289. };
  290. static struct pxafb_mach_info fb_info = {
  291. .modes = fb_mode_info,
  292. .num_modes = 1,
  293. .lcd_conn = LCD_COLOR_TFT_16BPP | LCD_PCLK_EDGE_FALL,
  294. };
  295. static int viper_backlight_init(struct device *dev)
  296. {
  297. int ret;
  298. /* GPIO9 and 10 control FB backlight. Initialise to off */
  299. ret = gpio_request(VIPER_BCKLIGHT_EN_GPIO, "Backlight");
  300. if (ret)
  301. goto err_request_bckl;
  302. ret = gpio_request(VIPER_LCD_EN_GPIO, "LCD");
  303. if (ret)
  304. goto err_request_lcd;
  305. ret = gpio_direction_output(VIPER_BCKLIGHT_EN_GPIO, 0);
  306. if (ret)
  307. goto err_dir;
  308. ret = gpio_direction_output(VIPER_LCD_EN_GPIO, 0);
  309. if (ret)
  310. goto err_dir;
  311. return 0;
  312. err_dir:
  313. gpio_free(VIPER_LCD_EN_GPIO);
  314. err_request_lcd:
  315. gpio_free(VIPER_BCKLIGHT_EN_GPIO);
  316. err_request_bckl:
  317. dev_err(dev, "Failed to setup LCD GPIOs\n");
  318. return ret;
  319. }
  320. static int viper_backlight_notify(struct device *dev, int brightness)
  321. {
  322. gpio_set_value(VIPER_LCD_EN_GPIO, !!brightness);
  323. gpio_set_value(VIPER_BCKLIGHT_EN_GPIO, !!brightness);
  324. return brightness;
  325. }
  326. static void viper_backlight_exit(struct device *dev)
  327. {
  328. gpio_free(VIPER_LCD_EN_GPIO);
  329. gpio_free(VIPER_BCKLIGHT_EN_GPIO);
  330. }
  331. static struct platform_pwm_backlight_data viper_backlight_data = {
  332. .pwm_id = 0,
  333. .max_brightness = 100,
  334. .dft_brightness = 100,
  335. .pwm_period_ns = 1000000,
  336. .init = viper_backlight_init,
  337. .notify = viper_backlight_notify,
  338. .exit = viper_backlight_exit,
  339. };
  340. static struct platform_device viper_backlight_device = {
  341. .name = "pwm-backlight",
  342. .dev = {
  343. .parent = &pxa25x_device_pwm0.dev,
  344. .platform_data = &viper_backlight_data,
  345. },
  346. };
  347. /* Ethernet */
  348. static struct resource smc91x_resources[] = {
  349. [0] = {
  350. .name = "smc91x-regs",
  351. .start = VIPER_ETH_PHYS + 0x300,
  352. .end = VIPER_ETH_PHYS + 0x30f,
  353. .flags = IORESOURCE_MEM,
  354. },
  355. [1] = {
  356. .start = PXA_GPIO_TO_IRQ(VIPER_ETH_GPIO),
  357. .end = PXA_GPIO_TO_IRQ(VIPER_ETH_GPIO),
  358. .flags = IORESOURCE_IRQ | IORESOURCE_IRQ_HIGHEDGE,
  359. },
  360. [2] = {
  361. .name = "smc91x-data32",
  362. .start = VIPER_ETH_DATA_PHYS,
  363. .end = VIPER_ETH_DATA_PHYS + 3,
  364. .flags = IORESOURCE_MEM,
  365. },
  366. };
  367. static struct smc91x_platdata viper_smc91x_info = {
  368. .flags = SMC91X_USE_16BIT | SMC91X_NOWAIT,
  369. .leda = RPC_LED_100_10,
  370. .ledb = RPC_LED_TX_RX,
  371. };
  372. static struct platform_device smc91x_device = {
  373. .name = "smc91x",
  374. .id = -1,
  375. .num_resources = ARRAY_SIZE(smc91x_resources),
  376. .resource = smc91x_resources,
  377. .dev = {
  378. .platform_data = &viper_smc91x_info,
  379. },