cpm_common.c 5.1 KB

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