cpm_common.c 5.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203
  1. /*
  2. * Common CPM code
  3. *
  4. * Author: Scott Wood <scottwood@freescale.com>
  5. *
  6. * Copyright 2007 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 <asm/udbg.h>
  22. #include <asm/io.h>
  23. #include <asm/system.h>
  24. #include <asm/rheap.h>
  25. #include <asm/cpm.h>
  26. #include <mm/mmu_decl.h>
  27. #ifdef CONFIG_PPC_EARLY_DEBUG_CPM
  28. static u32 __iomem *cpm_udbg_txdesc =
  29. (u32 __iomem __force *)CONFIG_PPC_EARLY_DEBUG_CPM_ADDR;
  30. static void udbg_putc_cpm(char c)
  31. {
  32. u8 __iomem *txbuf = (u8 __iomem __force *)in_be32(&cpm_udbg_txdesc[1]);
  33. if (c == '\n')
  34. udbg_putc('\r');
  35. while (in_be32(&cpm_udbg_txdesc[0]) & 0x80000000)
  36. ;
  37. out_8(txbuf, c);
  38. out_be32(&cpm_udbg_txdesc[0], 0xa0000001);
  39. }
  40. void __init udbg_init_cpm(void)
  41. {
  42. if (cpm_udbg_txdesc) {
  43. #ifdef CONFIG_CPM2
  44. setbat(1, 0xf0000000, 0xf0000000, 1024*1024, _PAGE_IO);
  45. #endif
  46. udbg_putc = udbg_putc_cpm;
  47. udbg_putc('X');
  48. }
  49. }
  50. #endif
  51. #ifdef CONFIG_PPC_CPM_NEW_BINDING
  52. static spinlock_t cpm_muram_lock;
  53. static rh_block_t cpm_boot_muram_rh_block[16];
  54. static rh_info_t cpm_muram_info;
  55. static u8 __iomem *muram_vbase;
  56. static phys_addr_t muram_pbase;
  57. /* Max address size we deal with */
  58. #define OF_MAX_ADDR_CELLS 4
  59. int __init cpm_muram_init(void)
  60. {
  61. struct device_node *np;
  62. struct resource r;
  63. u32 zero[OF_MAX_ADDR_CELLS] = {};
  64. resource_size_t max = 0;
  65. int i = 0;
  66. int ret = 0;
  67. spin_lock_init(&cpm_muram_lock);
  68. /* initialize the info header */
  69. rh_init(&cpm_muram_info, 1,
  70. sizeof(cpm_boot_muram_rh_block) /
  71. sizeof(cpm_boot_muram_rh_block[0]),
  72. cpm_boot_muram_rh_block);
  73. np = of_find_compatible_node(NULL, NULL, "fsl,cpm-muram-data");
  74. if (!np) {
  75. printk(KERN_ERR "Cannot find CPM muram data node");
  76. ret = -ENODEV;
  77. goto out;
  78. }
  79. muram_pbase = of_translate_address(np, zero);
  80. if (muram_pbase == (phys_addr_t)OF_BAD_ADDR) {
  81. printk(KERN_ERR "Cannot translate zero through CPM muram node");
  82. ret = -ENODEV;
  83. goto out;
  84. }
  85. while (of_address_to_resource(np, i++, &r) == 0) {
  86. if (r.end > max)
  87. max = r.end;
  88. rh_attach_region(&cpm_muram_info, r.start - muram_pbase,
  89. r.end - r.start + 1);
  90. }
  91. muram_vbase = ioremap(muram_pbase, max - muram_pbase + 1);
  92. if (!muram_vbase) {
  93. printk(KERN_ERR "Cannot map CPM muram");
  94. ret = -ENOMEM;
  95. }
  96. out:
  97. of_node_put(np);
  98. return ret;
  99. }
  100. /**
  101. * cpm_muram_alloc - allocate the requested size worth of multi-user ram
  102. * @size: number of bytes to allocate
  103. * @align: requested alignment, in bytes
  104. *
  105. * This function returns an offset into the muram area.
  106. * Use cpm_dpram_addr() to get the virtual address of the area.
  107. * Use cpm_muram_free() to free the allocation.
  108. */
  109. unsigned long cpm_muram_alloc(unsigned long size, unsigned long align)
  110. {
  111. unsigned long start;
  112. unsigned long flags;
  113. spin_lock_irqsave(&cpm_muram_lock, flags);
  114. cpm_muram_info.alignment = align;
  115. start = rh_alloc(&cpm_muram_info, size, "commproc");
  116. spin_unlock_irqrestore(&cpm_muram_lock, flags);
  117. return start;
  118. }
  119. EXPORT_SYMBOL(cpm_muram_alloc);
  120. /**
  121. * cpm_muram_free - free a chunk of multi-user ram
  122. * @offset: The beginning of the chunk as returned by cpm_muram_alloc().
  123. */
  124. int cpm_muram_free(unsigned long offset)
  125. {
  126. int ret;
  127. unsigned long flags;
  128. spin_lock_irqsave(&cpm_muram_lock, flags);
  129. ret = rh_free(&cpm_muram_info, offset);
  130. spin_unlock_irqrestore(&cpm_muram_lock, flags);
  131. return ret;
  132. }
  133. EXPORT_SYMBOL(cpm_muram_free);
  134. /**
  135. * cpm_muram_alloc_fixed - reserve a specific region of multi-user ram
  136. * @offset: the offset into the muram area to reserve
  137. * @size: the number of bytes to reserve
  138. *
  139. * This function returns "start" on success, -ENOMEM on failure.
  140. * Use cpm_dpram_addr() to get the virtual address of the area.
  141. * Use cpm_muram_free() to free the allocation.
  142. */
  143. unsigned long cpm_muram_alloc_fixed(unsigned long offset, unsigned long size)
  144. {
  145. unsigned long start;
  146. unsigned long flags;
  147. spin_lock_irqsave(&cpm_muram_lock, flags);
  148. cpm_muram_info.alignment = 1;
  149. start = rh_alloc_fixed(&cpm_muram_info, offset, size, "commproc");
  150. spin_unlock_irqrestore(&cpm_muram_lock, flags);
  151. return start;
  152. }
  153. EXPORT_SYMBOL(cpm_muram_alloc_fixed);
  154. /**
  155. * cpm_muram_addr - turn a muram offset into a virtual address
  156. * @offset: muram offset to convert
  157. */
  158. void __iomem *cpm_muram_addr(unsigned long offset)
  159. {
  160. return muram_vbase + offset;
  161. }
  162. EXPORT_SYMBOL(cpm_muram_addr);
  163. /**
  164. * cpm_muram_dma - turn a muram virtual address into a DMA address
  165. * @offset: virtual address from cpm_muram_addr() to convert
  166. */
  167. dma_addr_t cpm_muram_dma(void __iomem *addr)
  168. {
  169. return muram_pbase + ((u8 __iomem *)addr - muram_vbase);
  170. }
  171. EXPORT_SYMBOL(cpm_muram_dma);
  172. #endif /* CONFIG_PPC_CPM_NEW_BINDING */