cpm_common.c 5.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218
  1. /*
  2. * Common CPM code
  3. *
  4. * Author: Scott Wood <scottwood@freescale.com>
  5. *
  6. * Copyright 2007-2008,2010 Freescale Semiconductor, Inc.
  7. *
  8. * Some parts derived from commproc.c/cpm2_common.c, which is:
  9. * Copyright (c) 1997 Dan error_act (dmalek@jlc.net)
  10. * Copyright (c) 1999-2001 Dan Malek <dan@embeddedalley.com>
  11. * Copyright (c) 2000 MontaVista Software, Inc (source@mvista.com)
  12. * 2006 (c) MontaVista Software, Inc.
  13. * Vitaly Bordug <vbordug@ru.mvista.com>
  14. *
  15. * This program is free software; you can redistribute it and/or modify
  16. * it under the terms of version 2 of the GNU General Public License as
  17. * published by the Free Software Foundation.
  18. */
  19. #include <linux/init.h>
  20. #include <linux/of_device.h>
  21. #include <linux/spinlock.h>
  22. #include <linux/export.h>
  23. #include <linux/of.h>
  24. #include <linux/of_address.h>
  25. #include <linux/slab.h>
  26. #include <asm/udbg.h>
  27. #include <asm/io.h>
  28. #include <asm/cpm.h>
  29. #include <asm/fixmap.h>
  30. #include <soc/fsl/qe/qe.h>
  31. #include <mm/mmu_decl.h>
  32. #if defined(CONFIG_CPM2) || defined(CONFIG_8xx_GPIO)
  33. #include <linux/of_gpio.h>
  34. #endif
  35. static int __init cpm_init(void)
  36. {
  37. struct device_node *np;
  38. np = of_find_compatible_node(NULL, NULL, "fsl,cpm1");
  39. if (!np)
  40. np = of_find_compatible_node(NULL, NULL, "fsl,cpm2");
  41. if (!np)
  42. return -ENODEV;
  43. cpm_muram_init();
  44. of_node_put(np);
  45. return 0;
  46. }
  47. subsys_initcall(cpm_init);
  48. #ifdef CONFIG_PPC_EARLY_DEBUG_CPM
  49. static u32 __iomem *cpm_udbg_txdesc;
  50. static u8 __iomem *cpm_udbg_txbuf;
  51. static void udbg_putc_cpm(char c)
  52. {
  53. if (c == '\n')
  54. udbg_putc_cpm('\r');
  55. while (in_be32(&cpm_udbg_txdesc[0]) & 0x80000000)
  56. ;
  57. out_8(cpm_udbg_txbuf, c);
  58. out_be32(&cpm_udbg_txdesc[0], 0xa0000001);
  59. }
  60. void __init udbg_init_cpm(void)
  61. {
  62. #ifdef CONFIG_PPC_8xx
  63. cpm_udbg_txdesc = (u32 __iomem __force *)
  64. (CONFIG_PPC_EARLY_DEBUG_CPM_ADDR - PHYS_IMMR_BASE +
  65. VIRT_IMMR_BASE);
  66. cpm_udbg_txbuf = (u8 __iomem __force *)
  67. (in_be32(&cpm_udbg_txdesc[1]) - PHYS_IMMR_BASE +
  68. VIRT_IMMR_BASE);
  69. #else
  70. cpm_udbg_txdesc = (u32 __iomem __force *)
  71. CONFIG_PPC_EARLY_DEBUG_CPM_ADDR;
  72. cpm_udbg_txbuf = (u8 __iomem __force *)in_be32(&cpm_udbg_txdesc[1]);
  73. #endif
  74. if (cpm_udbg_txdesc) {
  75. #ifdef CONFIG_CPM2
  76. setbat(1, 0xf0000000, 0xf0000000, 1024*1024, PAGE_KERNEL_NCG);
  77. #endif
  78. udbg_putc = udbg_putc_cpm;
  79. }
  80. }
  81. #endif
  82. #if defined(CONFIG_CPM2) || defined(CONFIG_8xx_GPIO)
  83. struct cpm2_ioports {
  84. u32 dir, par, sor, odr, dat;
  85. u32 res[3];
  86. };
  87. struct cpm2_gpio32_chip {
  88. struct of_mm_gpio_chip mm_gc;
  89. spinlock_t lock;
  90. /* shadowed data register to clear/set bits safely */
  91. u32 cpdata;
  92. };
  93. static void cpm2_gpio32_save_regs(struct of_mm_gpio_chip *mm_gc)
  94. {
  95. struct cpm2_gpio32_chip *cpm2_gc =
  96. container_of(mm_gc, struct cpm2_gpio32_chip, mm_gc);
  97. struct cpm2_ioports __iomem *iop = mm_gc->regs;
  98. cpm2_gc->cpdata = in_be32(&iop->dat);
  99. }
  100. static int cpm2_gpio32_get(struct gpio_chip *gc, unsigned int gpio)
  101. {
  102. struct of_mm_gpio_chip *mm_gc = to_of_mm_gpio_chip(gc);
  103. struct cpm2_ioports __iomem *iop = mm_gc->regs;
  104. u32 pin_mask;
  105. pin_mask = 1 << (31 - gpio);
  106. return !!(in_be32(&iop->dat) & pin_mask);
  107. }
  108. static void __cpm2_gpio32_set(struct of_mm_gpio_chip *mm_gc, u32 pin_mask,
  109. int value)
  110. {
  111. struct cpm2_gpio32_chip *cpm2_gc = gpiochip_get_data(&mm_gc->gc);
  112. struct cpm2_ioports __iomem *iop = mm_gc->regs;
  113. if (value)
  114. cpm2_gc->cpdata |= pin_mask;
  115. else
  116. cpm2_gc->cpdata &= ~pin_mask;
  117. out_be32(&iop->dat, cpm2_gc->cpdata);
  118. }
  119. static void cpm2_gpio32_set(struct gpio_chip *gc, unsigned int gpio, int value)
  120. {
  121. struct of_mm_gpio_chip *mm_gc = to_of_mm_gpio_chip(gc);
  122. struct cpm2_gpio32_chip *cpm2_gc = gpiochip_get_data(gc);
  123. unsigned long flags;
  124. u32 pin_mask = 1 << (31 - gpio);
  125. spin_lock_irqsave(&cpm2_gc->lock, flags);
  126. __cpm2_gpio32_set(mm_gc, pin_mask, value);
  127. spin_unlock_irqrestore(&cpm2_gc->lock, flags);
  128. }
  129. static int cpm2_gpio32_dir_out(struct gpio_chip *gc, unsigned int gpio, int val)
  130. {
  131. struct of_mm_gpio_chip *mm_gc = to_of_mm_gpio_chip(gc);
  132. struct cpm2_gpio32_chip *cpm2_gc = gpiochip_get_data(gc);
  133. struct cpm2_ioports __iomem *iop = mm_gc->regs;
  134. unsigned long flags;
  135. u32 pin_mask = 1 << (31 - gpio);
  136. spin_lock_irqsave(&cpm2_gc->lock, flags);
  137. setbits32(&iop->dir, pin_mask);
  138. __cpm2_gpio32_set(mm_gc, pin_mask, val);
  139. spin_unlock_irqrestore(&cpm2_gc->lock, flags);
  140. return 0;
  141. }
  142. static int cpm2_gpio32_dir_in(struct gpio_chip *gc, unsigned int gpio)
  143. {
  144. struct of_mm_gpio_chip *mm_gc = to_of_mm_gpio_chip(gc);
  145. struct cpm2_gpio32_chip *cpm2_gc = gpiochip_get_data(gc);
  146. struct cpm2_ioports __iomem *iop = mm_gc->regs;
  147. unsigned long flags;
  148. u32 pin_mask = 1 << (31 - gpio);
  149. spin_lock_irqsave(&cpm2_gc->lock, flags);
  150. clrbits32(&iop->dir, pin_mask);
  151. spin_unlock_irqrestore(&cpm2_gc->lock, flags);
  152. return 0;
  153. }
  154. int cpm2_gpiochip_add32(struct device_node *np)
  155. {
  156. struct cpm2_gpio32_chip *cpm2_gc;
  157. struct of_mm_gpio_chip *mm_gc;
  158. struct gpio_chip *gc;
  159. cpm2_gc = kzalloc(sizeof(*cpm2_gc), GFP_KERNEL);
  160. if (!cpm2_gc)
  161. return -ENOMEM;
  162. spin_lock_init(&cpm2_gc->lock);
  163. mm_gc = &cpm2_gc->mm_gc;
  164. gc = &mm_gc->gc;
  165. mm_gc->save_regs = cpm2_gpio32_save_regs;
  166. gc->ngpio = 32;
  167. gc->direction_input = cpm2_gpio32_dir_in;
  168. gc->direction_output = cpm2_gpio32_dir_out;
  169. gc->get = cpm2_gpio32_get;
  170. gc->set = cpm2_gpio32_set;
  171. return of_mm_gpiochip_add_data(np, mm_gc, cpm2_gc);
  172. }
  173. #endif /* CONFIG_CPM2 || CONFIG_8xx_GPIO */