cpm_common.c 4.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201
  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 = gpiochip_get_data(&mm_gc->gc);
  83. struct cpm2_ioports __iomem *iop = mm_gc->regs;
  84. cpm2_gc->cpdata = in_be32(&iop->dat);
  85. }
  86. static int cpm2_gpio32_get(struct gpio_chip *gc, unsigned int gpio)
  87. {
  88. struct of_mm_gpio_chip *mm_gc = to_of_mm_gpio_chip(gc);
  89. struct cpm2_ioports __iomem *iop = mm_gc->regs;
  90. u32 pin_mask;
  91. pin_mask = 1 << (31 - gpio);
  92. return !!(in_be32(&iop->dat) & pin_mask);
  93. }
  94. static void __cpm2_gpio32_set(struct of_mm_gpio_chip *mm_gc, u32 pin_mask,
  95. int value)
  96. {
  97. struct cpm2_gpio32_chip *cpm2_gc = gpiochip_get_data(&mm_gc->gc);
  98. struct cpm2_ioports __iomem *iop = mm_gc->regs;
  99. if (value)
  100. cpm2_gc->cpdata |= pin_mask;
  101. else
  102. cpm2_gc->cpdata &= ~pin_mask;
  103. out_be32(&iop->dat, cpm2_gc->cpdata);
  104. }
  105. static void cpm2_gpio32_set(struct gpio_chip *gc, unsigned int gpio, int value)
  106. {
  107. struct of_mm_gpio_chip *mm_gc = to_of_mm_gpio_chip(gc);
  108. struct cpm2_gpio32_chip *cpm2_gc = gpiochip_get_data(gc);
  109. unsigned long flags;
  110. u32 pin_mask = 1 << (31 - gpio);
  111. spin_lock_irqsave(&cpm2_gc->lock, flags);
  112. __cpm2_gpio32_set(mm_gc, pin_mask, value);
  113. spin_unlock_irqrestore(&cpm2_gc->lock, flags);
  114. }
  115. static int cpm2_gpio32_dir_out(struct gpio_chip *gc, unsigned int gpio, int val)
  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. struct cpm2_ioports __iomem *iop = mm_gc->regs;
  120. unsigned long flags;
  121. u32 pin_mask = 1 << (31 - gpio);
  122. spin_lock_irqsave(&cpm2_gc->lock, flags);
  123. setbits32(&iop->dir, pin_mask);
  124. __cpm2_gpio32_set(mm_gc, pin_mask, val);
  125. spin_unlock_irqrestore(&cpm2_gc->lock, flags);
  126. return 0;
  127. }
  128. static int cpm2_gpio32_dir_in(struct gpio_chip *gc, unsigned int gpio)
  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. clrbits32(&iop->dir, pin_mask);
  137. spin_unlock_irqrestore(&cpm2_gc->lock, flags);
  138. return 0;
  139. }
  140. int cpm2_gpiochip_add32(struct device_node *np)
  141. {
  142. struct cpm2_gpio32_chip *cpm2_gc;
  143. struct of_mm_gpio_chip *mm_gc;
  144. struct gpio_chip *gc;
  145. cpm2_gc = kzalloc(sizeof(*cpm2_gc), GFP_KERNEL);
  146. if (!cpm2_gc)
  147. return -ENOMEM;
  148. spin_lock_init(&cpm2_gc->lock);
  149. mm_gc = &cpm2_gc->mm_gc;
  150. gc = &mm_gc->gc;
  151. mm_gc->save_regs = cpm2_gpio32_save_regs;
  152. gc->ngpio = 32;
  153. gc->direction_input = cpm2_gpio32_dir_in;
  154. gc->direction_output = cpm2_gpio32_dir_out;
  155. gc->get = cpm2_gpio32_get;
  156. gc->set = cpm2_gpio32_set;
  157. return of_mm_gpiochip_add_data(np, mm_gc, cpm2_gc);
  158. }
  159. #endif /* CONFIG_CPM2 || CONFIG_8xx_GPIO */