cpm_common.c 5.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219
  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 <soc/fsl/qe/qe.h>
  30. #include <mm/mmu_decl.h>
  31. #if defined(CONFIG_CPM2) || defined(CONFIG_8xx_GPIO)
  32. #include <linux/of_gpio.h>
  33. #endif
  34. static int __init cpm_init(void)
  35. {
  36. struct device_node *np;
  37. np = of_find_compatible_node(NULL, NULL, "fsl,cpm1");
  38. if (!np)
  39. np = of_find_compatible_node(NULL, NULL, "fsl,cpm2");
  40. if (!np)
  41. return -ENODEV;
  42. cpm_muram_init();
  43. of_node_put(np);
  44. return 0;
  45. }
  46. subsys_initcall(cpm_init);
  47. #ifdef CONFIG_PPC_EARLY_DEBUG_CPM
  48. static u32 __iomem *cpm_udbg_txdesc;
  49. static u8 __iomem *cpm_udbg_txbuf;
  50. static void udbg_putc_cpm(char c)
  51. {
  52. if (c == '\n')
  53. udbg_putc_cpm('\r');
  54. while (in_be32(&cpm_udbg_txdesc[0]) & 0x80000000)
  55. ;
  56. out_8(cpm_udbg_txbuf, c);
  57. out_be32(&cpm_udbg_txdesc[0], 0xa0000001);
  58. }
  59. void __init udbg_init_cpm(void)
  60. {
  61. #ifdef CONFIG_PPC_8xx
  62. cpm_udbg_txdesc = (u32 __iomem __force *)
  63. (CONFIG_PPC_EARLY_DEBUG_CPM_ADDR - PHYS_IMMR_BASE +
  64. VIRT_IMMR_BASE);
  65. cpm_udbg_txbuf = (u8 __iomem __force *)
  66. (in_be32(&cpm_udbg_txdesc[1]) - PHYS_IMMR_BASE +
  67. VIRT_IMMR_BASE);
  68. #else
  69. cpm_udbg_txdesc = (u32 __iomem __force *)
  70. CONFIG_PPC_EARLY_DEBUG_CPM_ADDR;
  71. cpm_udbg_txbuf = (u8 __iomem __force *)in_be32(&cpm_udbg_txdesc[1]);
  72. #endif
  73. if (cpm_udbg_txdesc) {
  74. #ifdef CONFIG_CPM2
  75. setbat(1, 0xf0000000, 0xf0000000, 1024*1024, PAGE_KERNEL_NCG);
  76. #endif
  77. udbg_putc = udbg_putc_cpm;
  78. }
  79. }
  80. #endif
  81. #if defined(CONFIG_CPM2) || defined(CONFIG_8xx_GPIO)
  82. struct cpm2_ioports {
  83. u32 dir, par, sor, odr, dat;
  84. u32 res[3];
  85. };
  86. struct cpm2_gpio32_chip {
  87. struct of_mm_gpio_chip mm_gc;
  88. spinlock_t lock;
  89. /* shadowed data register to clear/set bits safely */
  90. u32 cpdata;
  91. };
  92. static void cpm2_gpio32_save_regs(struct of_mm_gpio_chip *mm_gc)
  93. {
  94. struct cpm2_gpio32_chip *cpm2_gc =
  95. container_of(mm_gc, struct cpm2_gpio32_chip, mm_gc);
  96. struct cpm2_ioports __iomem *iop = mm_gc->regs;
  97. cpm2_gc->cpdata = in_be32(&iop->dat);
  98. }
  99. static int cpm2_gpio32_get(struct gpio_chip *gc, unsigned int gpio)
  100. {
  101. struct of_mm_gpio_chip *mm_gc = to_of_mm_gpio_chip(gc);
  102. struct cpm2_ioports __iomem *iop = mm_gc->regs;
  103. u32 pin_mask;
  104. pin_mask = 1 << (31 - gpio);
  105. return !!(in_be32(&iop->dat) & pin_mask);
  106. }
  107. static void __cpm2_gpio32_set(struct of_mm_gpio_chip *mm_gc, u32 pin_mask,
  108. int value)
  109. {
  110. struct cpm2_gpio32_chip *cpm2_gc = gpiochip_get_data(&mm_gc->gc);
  111. struct cpm2_ioports __iomem *iop = mm_gc->regs;
  112. if (value)
  113. cpm2_gc->cpdata |= pin_mask;
  114. else
  115. cpm2_gc->cpdata &= ~pin_mask;
  116. out_be32(&iop->dat, cpm2_gc->cpdata);
  117. }
  118. static void cpm2_gpio32_set(struct gpio_chip *gc, unsigned int gpio, int value)
  119. {
  120. struct of_mm_gpio_chip *mm_gc = to_of_mm_gpio_chip(gc);
  121. struct cpm2_gpio32_chip *cpm2_gc = gpiochip_get_data(gc);
  122. unsigned long flags;
  123. u32 pin_mask = 1 << (31 - gpio);
  124. spin_lock_irqsave(&cpm2_gc->lock, flags);
  125. __cpm2_gpio32_set(mm_gc, pin_mask, value);
  126. spin_unlock_irqrestore(&cpm2_gc->lock, flags);
  127. }
  128. static int cpm2_gpio32_dir_out(struct gpio_chip *gc, unsigned int gpio, int val)
  129. {
  130. struct of_mm_gpio_chip *mm_gc = to_of_mm_gpio_chip(gc);
  131. struct cpm2_gpio32_chip *cpm2_gc = gpiochip_get_data(gc);
  132. struct cpm2_ioports __iomem *iop = mm_gc->regs;
  133. unsigned long flags;
  134. u32 pin_mask = 1 << (31 - gpio);
  135. spin_lock_irqsave(&cpm2_gc->lock, flags);
  136. setbits32(&iop->dir, pin_mask);
  137. __cpm2_gpio32_set(mm_gc, pin_mask, val);
  138. spin_unlock_irqrestore(&cpm2_gc->lock, flags);
  139. return 0;
  140. }
  141. static int cpm2_gpio32_dir_in(struct gpio_chip *gc, unsigned int gpio)
  142. {
  143. struct of_mm_gpio_chip *mm_gc = to_of_mm_gpio_chip(gc);
  144. struct cpm2_gpio32_chip *cpm2_gc = gpiochip_get_data(gc);
  145. struct cpm2_ioports __iomem *iop = mm_gc->regs;
  146. unsigned long flags;
  147. u32 pin_mask = 1 << (31 - gpio);
  148. spin_lock_irqsave(&cpm2_gc->lock, flags);
  149. clrbits32(&iop->dir, pin_mask);
  150. spin_unlock_irqrestore(&cpm2_gc->lock, flags);
  151. return 0;
  152. }
  153. int cpm2_gpiochip_add32(struct device *dev)
  154. {
  155. struct device_node *np = dev->of_node;
  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. gc->parent = dev;
  172. gc->owner = THIS_MODULE;
  173. return of_mm_gpiochip_add_data(np, mm_gc, cpm2_gc);
  174. }
  175. #endif /* CONFIG_CPM2 || CONFIG_8xx_GPIO */