cpm_common.c 5.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200
  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. static spinlock_t cpm_muram_lock;
  52. static rh_block_t cpm_boot_muram_rh_block[16];
  53. static rh_info_t cpm_muram_info;
  54. static u8 __iomem *muram_vbase;
  55. static phys_addr_t muram_pbase;
  56. /* Max address size we deal with */
  57. #define OF_MAX_ADDR_CELLS 4
  58. int __init cpm_muram_init(void)
  59. {
  60. struct device_node *np;
  61. struct resource r;
  62. u32 zero[OF_MAX_ADDR_CELLS] = {};
  63. resource_size_t max = 0;
  64. int i = 0;
  65. int ret = 0;
  66. spin_lock_init(&cpm_muram_lock);
  67. /* initialize the info header */
  68. rh_init(&cpm_muram_info, 1,
  69. sizeof(cpm_boot_muram_rh_block) /
  70. sizeof(cpm_boot_muram_rh_block[0]),
  71. cpm_boot_muram_rh_block);
  72. np = of_find_compatible_node(NULL, NULL, "fsl,cpm-muram-data");
  73. if (!np) {
  74. printk(KERN_ERR "Cannot find CPM muram data node");
  75. ret = -ENODEV;
  76. goto out;
  77. }
  78. muram_pbase = of_translate_address(np, zero);
  79. if (muram_pbase == (phys_addr_t)OF_BAD_ADDR) {
  80. printk(KERN_ERR "Cannot translate zero through CPM muram node");
  81. ret = -ENODEV;
  82. goto out;
  83. }
  84. while (of_address_to_resource(np, i++, &r) == 0) {
  85. if (r.end > max)
  86. max = r.end;
  87. rh_attach_region(&cpm_muram_info, r.start - muram_pbase,
  88. r.end - r.start + 1);
  89. }
  90. muram_vbase = ioremap(muram_pbase, max - muram_pbase + 1);
  91. if (!muram_vbase) {
  92. printk(KERN_ERR "Cannot map CPM muram");
  93. ret = -ENOMEM;
  94. }
  95. out:
  96. of_node_put(np);
  97. return ret;
  98. }
  99. /**
  100. * cpm_muram_alloc - allocate the requested size worth of multi-user ram
  101. * @size: number of bytes to allocate
  102. * @align: requested alignment, in bytes
  103. *
  104. * This function returns an offset into the muram area.
  105. * Use cpm_dpram_addr() to get the virtual address of the area.
  106. * Use cpm_muram_free() to free the allocation.
  107. */
  108. unsigned long cpm_muram_alloc(unsigned long size, unsigned long align)
  109. {
  110. unsigned long start;
  111. unsigned long flags;
  112. spin_lock_irqsave(&cpm_muram_lock, flags);
  113. cpm_muram_info.alignment = align;
  114. start = rh_alloc(&cpm_muram_info, size, "commproc");
  115. spin_unlock_irqrestore(&cpm_muram_lock, flags);
  116. return start;
  117. }
  118. EXPORT_SYMBOL(cpm_muram_alloc);
  119. /**
  120. * cpm_muram_free - free a chunk of multi-user ram
  121. * @offset: The beginning of the chunk as returned by cpm_muram_alloc().
  122. */
  123. int cpm_muram_free(unsigned long offset)
  124. {
  125. int ret;
  126. unsigned long flags;
  127. spin_lock_irqsave(&cpm_muram_lock, flags);
  128. ret = rh_free(&cpm_muram_info, offset);
  129. spin_unlock_irqrestore(&cpm_muram_lock, flags);
  130. return ret;
  131. }
  132. EXPORT_SYMBOL(cpm_muram_free);
  133. /**
  134. * cpm_muram_alloc_fixed - reserve a specific region of multi-user ram
  135. * @offset: the offset into the muram area to reserve
  136. * @size: the number of bytes to reserve
  137. *
  138. * This function returns "start" on success, -ENOMEM on failure.
  139. * Use cpm_dpram_addr() to get the virtual address of the area.
  140. * Use cpm_muram_free() to free the allocation.
  141. */
  142. unsigned long cpm_muram_alloc_fixed(unsigned long offset, unsigned long size)
  143. {
  144. unsigned long start;
  145. unsigned long flags;
  146. spin_lock_irqsave(&cpm_muram_lock, flags);
  147. cpm_muram_info.alignment = 1;
  148. start = rh_alloc_fixed(&cpm_muram_info, offset, size, "commproc");
  149. spin_unlock_irqrestore(&cpm_muram_lock, flags);
  150. return start;
  151. }
  152. EXPORT_SYMBOL(cpm_muram_alloc_fixed);
  153. /**
  154. * cpm_muram_addr - turn a muram offset into a virtual address
  155. * @offset: muram offset to convert
  156. */
  157. void __iomem *cpm_muram_addr(unsigned long offset)
  158. {
  159. return muram_vbase + offset;
  160. }
  161. EXPORT_SYMBOL(cpm_muram_addr);
  162. /**
  163. * cpm_muram_dma - turn a muram virtual address into a DMA address
  164. * @offset: virtual address from cpm_muram_addr() to convert
  165. */
  166. dma_addr_t cpm_muram_dma(void __iomem *addr)
  167. {
  168. return muram_pbase + ((u8 __iomem *)addr - muram_vbase);
  169. }
  170. EXPORT_SYMBOL(cpm_muram_dma);