cpm_common.c 4.7 KB

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