cpm_common.c 4.9 KB

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