cpm_common.c 8.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349
  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 <linux/spinlock.h>
  22. #include <linux/of.h>
  23. #include <linux/slab.h>
  24. #include <asm/udbg.h>
  25. #include <asm/io.h>
  26. #include <asm/system.h>
  27. #include <asm/rheap.h>
  28. #include <asm/cpm.h>
  29. #include <mm/mmu_decl.h>
  30. #if defined(CONFIG_CPM2) || defined(CONFIG_8xx_GPIO)
  31. #include <linux/of_gpio.h>
  32. #endif
  33. #ifdef CONFIG_PPC_EARLY_DEBUG_CPM
  34. static u32 __iomem *cpm_udbg_txdesc =
  35. (u32 __iomem __force *)CONFIG_PPC_EARLY_DEBUG_CPM_ADDR;
  36. static void udbg_putc_cpm(char c)
  37. {
  38. u8 __iomem *txbuf = (u8 __iomem __force *)in_be32(&cpm_udbg_txdesc[1]);
  39. if (c == '\n')
  40. udbg_putc_cpm('\r');
  41. while (in_be32(&cpm_udbg_txdesc[0]) & 0x80000000)
  42. ;
  43. out_8(txbuf, c);
  44. out_be32(&cpm_udbg_txdesc[0], 0xa0000001);
  45. }
  46. void __init udbg_init_cpm(void)
  47. {
  48. if (cpm_udbg_txdesc) {
  49. #ifdef CONFIG_CPM2
  50. setbat(1, 0xf0000000, 0xf0000000, 1024*1024, PAGE_KERNEL_NCG);
  51. #endif
  52. udbg_putc = udbg_putc_cpm;
  53. }
  54. }
  55. #endif
  56. static spinlock_t cpm_muram_lock;
  57. static rh_block_t cpm_boot_muram_rh_block[16];
  58. static rh_info_t cpm_muram_info;
  59. static u8 __iomem *muram_vbase;
  60. static phys_addr_t muram_pbase;
  61. /* Max address size we deal with */
  62. #define OF_MAX_ADDR_CELLS 4
  63. int cpm_muram_init(void)
  64. {
  65. struct device_node *np;
  66. struct resource r;
  67. u32 zero[OF_MAX_ADDR_CELLS] = {};
  68. resource_size_t max = 0;
  69. int i = 0;
  70. int ret = 0;
  71. if (muram_pbase)
  72. return 0;
  73. spin_lock_init(&cpm_muram_lock);
  74. /* initialize the info header */
  75. rh_init(&cpm_muram_info, 1,
  76. sizeof(cpm_boot_muram_rh_block) /
  77. sizeof(cpm_boot_muram_rh_block[0]),
  78. cpm_boot_muram_rh_block);
  79. np = of_find_compatible_node(NULL, NULL, "fsl,cpm-muram-data");
  80. if (!np) {
  81. /* try legacy bindings */
  82. np = of_find_node_by_name(NULL, "data-only");
  83. if (!np) {
  84. printk(KERN_ERR "Cannot find CPM muram data node");
  85. ret = -ENODEV;
  86. goto out;
  87. }
  88. }
  89. muram_pbase = of_translate_address(np, zero);
  90. if (muram_pbase == (phys_addr_t)OF_BAD_ADDR) {
  91. printk(KERN_ERR "Cannot translate zero through CPM muram node");
  92. ret = -ENODEV;
  93. goto out;
  94. }
  95. while (of_address_to_resource(np, i++, &r) == 0) {
  96. if (r.end > max)
  97. max = r.end;
  98. rh_attach_region(&cpm_muram_info, r.start - muram_pbase,
  99. r.end - r.start + 1);
  100. }
  101. muram_vbase = ioremap(muram_pbase, max - muram_pbase + 1);
  102. if (!muram_vbase) {
  103. printk(KERN_ERR "Cannot map CPM muram");
  104. ret = -ENOMEM;
  105. }
  106. out:
  107. of_node_put(np);
  108. return ret;
  109. }
  110. /**
  111. * cpm_muram_alloc - allocate the requested size worth of multi-user ram
  112. * @size: number of bytes to allocate
  113. * @align: requested alignment, in bytes
  114. *
  115. * This function returns an offset into the muram area.
  116. * Use cpm_dpram_addr() to get the virtual address of the area.
  117. * Use cpm_muram_free() to free the allocation.
  118. */
  119. unsigned long cpm_muram_alloc(unsigned long size, unsigned long align)
  120. {
  121. unsigned long start;
  122. unsigned long flags;
  123. spin_lock_irqsave(&cpm_muram_lock, flags);
  124. cpm_muram_info.alignment = align;
  125. start = rh_alloc(&cpm_muram_info, size, "commproc");
  126. spin_unlock_irqrestore(&cpm_muram_lock, flags);
  127. return start;
  128. }
  129. EXPORT_SYMBOL(cpm_muram_alloc);
  130. /**
  131. * cpm_muram_free - free a chunk of multi-user ram
  132. * @offset: The beginning of the chunk as returned by cpm_muram_alloc().
  133. */
  134. int cpm_muram_free(unsigned long offset)
  135. {
  136. int ret;
  137. unsigned long flags;
  138. spin_lock_irqsave(&cpm_muram_lock, flags);
  139. ret = rh_free(&cpm_muram_info, offset);
  140. spin_unlock_irqrestore(&cpm_muram_lock, flags);
  141. return ret;
  142. }
  143. EXPORT_SYMBOL(cpm_muram_free);
  144. /**
  145. * cpm_muram_alloc_fixed - reserve a specific region of multi-user ram
  146. * @offset: the offset into the muram area to reserve
  147. * @size: the number of bytes to reserve
  148. *
  149. * This function returns "start" on success, -ENOMEM on failure.
  150. * Use cpm_dpram_addr() to get the virtual address of the area.
  151. * Use cpm_muram_free() to free the allocation.
  152. */
  153. unsigned long cpm_muram_alloc_fixed(unsigned long offset, unsigned long size)
  154. {
  155. unsigned long start;
  156. unsigned long flags;
  157. spin_lock_irqsave(&cpm_muram_lock, flags);
  158. cpm_muram_info.alignment = 1;
  159. start = rh_alloc_fixed(&cpm_muram_info, offset, size, "commproc");
  160. spin_unlock_irqrestore(&cpm_muram_lock, flags);
  161. return start;
  162. }
  163. EXPORT_SYMBOL(cpm_muram_alloc_fixed);
  164. /**
  165. * cpm_muram_addr - turn a muram offset into a virtual address
  166. * @offset: muram offset to convert
  167. */
  168. void __iomem *cpm_muram_addr(unsigned long offset)
  169. {
  170. return muram_vbase + offset;
  171. }
  172. EXPORT_SYMBOL(cpm_muram_addr);
  173. unsigned long cpm_muram_offset(void __iomem *addr)
  174. {
  175. return addr - (void __iomem *)muram_vbase;
  176. }
  177. EXPORT_SYMBOL(cpm_muram_offset);
  178. /**
  179. * cpm_muram_dma - turn a muram virtual address into a DMA address
  180. * @offset: virtual address from cpm_muram_addr() to convert
  181. */
  182. dma_addr_t cpm_muram_dma(void __iomem *addr)
  183. {
  184. return muram_pbase + ((u8 __iomem *)addr - muram_vbase);
  185. }
  186. EXPORT_SYMBOL(cpm_muram_dma);
  187. #if defined(CONFIG_CPM2) || defined(CONFIG_8xx_GPIO)
  188. struct cpm2_ioports {
  189. u32 dir, par, sor, odr, dat;
  190. u32 res[3];
  191. };
  192. struct cpm2_gpio32_chip {
  193. struct of_mm_gpio_chip mm_gc;
  194. spinlock_t lock;
  195. /* shadowed data register to clear/set bits safely */
  196. u32 cpdata;
  197. };
  198. static inline struct cpm2_gpio32_chip *
  199. to_cpm2_gpio32_chip(struct of_mm_gpio_chip *mm_gc)
  200. {
  201. return container_of(mm_gc, struct cpm2_gpio32_chip, mm_gc);
  202. }
  203. static void cpm2_gpio32_save_regs(struct of_mm_gpio_chip *mm_gc)
  204. {
  205. struct cpm2_gpio32_chip *cpm2_gc = to_cpm2_gpio32_chip(mm_gc);
  206. struct cpm2_ioports __iomem *iop = mm_gc->regs;
  207. cpm2_gc->cpdata = in_be32(&iop->dat);
  208. }
  209. static int cpm2_gpio32_get(struct gpio_chip *gc, unsigned int gpio)
  210. {
  211. struct of_mm_gpio_chip *mm_gc = to_of_mm_gpio_chip(gc);
  212. struct cpm2_ioports __iomem *iop = mm_gc->regs;
  213. u32 pin_mask;
  214. pin_mask = 1 << (31 - gpio);
  215. return !!(in_be32(&iop->dat) & pin_mask);
  216. }
  217. static void __cpm2_gpio32_set(struct of_mm_gpio_chip *mm_gc, u32 pin_mask,
  218. int value)
  219. {
  220. struct cpm2_gpio32_chip *cpm2_gc = to_cpm2_gpio32_chip(mm_gc);
  221. struct cpm2_ioports __iomem *iop = mm_gc->regs;
  222. if (value)
  223. cpm2_gc->cpdata |= pin_mask;
  224. else
  225. cpm2_gc->cpdata &= ~pin_mask;
  226. out_be32(&iop->dat, cpm2_gc->cpdata);
  227. }
  228. static void cpm2_gpio32_set(struct gpio_chip *gc, unsigned int gpio, int value)
  229. {
  230. struct of_mm_gpio_chip *mm_gc = to_of_mm_gpio_chip(gc);
  231. struct cpm2_gpio32_chip *cpm2_gc = to_cpm2_gpio32_chip(mm_gc);
  232. unsigned long flags;
  233. u32 pin_mask = 1 << (31 - gpio);
  234. spin_lock_irqsave(&cpm2_gc->lock, flags);
  235. __cpm2_gpio32_set(mm_gc, pin_mask, value);
  236. spin_unlock_irqrestore(&cpm2_gc->lock, flags);
  237. }
  238. static int cpm2_gpio32_dir_out(struct gpio_chip *gc, unsigned int gpio, int val)
  239. {
  240. struct of_mm_gpio_chip *mm_gc = to_of_mm_gpio_chip(gc);
  241. struct cpm2_gpio32_chip *cpm2_gc = to_cpm2_gpio32_chip(mm_gc);
  242. struct cpm2_ioports __iomem *iop = mm_gc->regs;
  243. unsigned long flags;
  244. u32 pin_mask = 1 << (31 - gpio);
  245. spin_lock_irqsave(&cpm2_gc->lock, flags);
  246. setbits32(&iop->dir, pin_mask);
  247. __cpm2_gpio32_set(mm_gc, pin_mask, val);
  248. spin_unlock_irqrestore(&cpm2_gc->lock, flags);
  249. return 0;
  250. }
  251. static int cpm2_gpio32_dir_in(struct gpio_chip *gc, unsigned int gpio)
  252. {
  253. struct of_mm_gpio_chip *mm_gc = to_of_mm_gpio_chip(gc);
  254. struct cpm2_gpio32_chip *cpm2_gc = to_cpm2_gpio32_chip(mm_gc);
  255. struct cpm2_ioports __iomem *iop = mm_gc->regs;
  256. unsigned long flags;
  257. u32 pin_mask = 1 << (31 - gpio);
  258. spin_lock_irqsave(&cpm2_gc->lock, flags);
  259. clrbits32(&iop->dir, pin_mask);
  260. spin_unlock_irqrestore(&cpm2_gc->lock, flags);
  261. return 0;
  262. }
  263. int cpm2_gpiochip_add32(struct device_node *np)
  264. {
  265. struct cpm2_gpio32_chip *cpm2_gc;
  266. struct of_mm_gpio_chip *mm_gc;
  267. struct gpio_chip *gc;
  268. cpm2_gc = kzalloc(sizeof(*cpm2_gc), GFP_KERNEL);
  269. if (!cpm2_gc)
  270. return -ENOMEM;
  271. spin_lock_init(&cpm2_gc->lock);
  272. mm_gc = &cpm2_gc->mm_gc;
  273. gc = &mm_gc->gc;
  274. mm_gc->save_regs = cpm2_gpio32_save_regs;
  275. gc->ngpio = 32;
  276. gc->direction_input = cpm2_gpio32_dir_in;
  277. gc->direction_output = cpm2_gpio32_dir_out;
  278. gc->get = cpm2_gpio32_get;
  279. gc->set = cpm2_gpio32_set;
  280. return of_mm_gpiochip_add(np, mm_gc);
  281. }
  282. #endif /* CONFIG_CPM2 || CONFIG_8xx_GPIO */