cpm_common.c 8.6 KB

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