xc.c 5.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257
  1. /*
  2. * arch/arm/mach-netx/xc.c
  3. *
  4. * Copyright (c) 2005 Sascha Hauer <s.hauer@pengutronix.de>, Pengutronix
  5. *
  6. * This program is free software; you can redistribute it and/or modify
  7. * it under the terms of the GNU General Public License version 2
  8. * as published by the Free Software Foundation.
  9. *
  10. * This program is distributed in the hope that it will be useful,
  11. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  12. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  13. * GNU General Public License for more details.
  14. *
  15. * You should have received a copy of the GNU General Public License
  16. * along with this program; if not, write to the Free Software
  17. * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
  18. */
  19. #include <linux/init.h>
  20. #include <linux/device.h>
  21. #include <linux/firmware.h>
  22. #include <linux/mutex.h>
  23. #include <linux/slab.h>
  24. #include <linux/io.h>
  25. #include <mach/hardware.h>
  26. #include <mach/irqs.h>
  27. #include <mach/netx-regs.h>
  28. #include <mach/xc.h>
  29. static DEFINE_MUTEX(xc_lock);
  30. static int xc_in_use = 0;
  31. struct fw_desc {
  32. unsigned int ofs;
  33. unsigned int size;
  34. unsigned int patch_ofs;
  35. unsigned int patch_entries;
  36. };
  37. struct fw_header {
  38. unsigned int magic;
  39. unsigned int type;
  40. unsigned int version;
  41. unsigned int reserved[5];
  42. struct fw_desc fw_desc[3];
  43. } __attribute__ ((packed));
  44. int xc_stop(struct xc *x)
  45. {
  46. writel(RPU_HOLD_PC, x->xmac_base + NETX_XMAC_RPU_HOLD_PC_OFS);
  47. writel(TPU_HOLD_PC, x->xmac_base + NETX_XMAC_TPU_HOLD_PC_OFS);
  48. writel(XPU_HOLD_PC, x->xpec_base + NETX_XPEC_XPU_HOLD_PC_OFS);
  49. return 0;
  50. }
  51. int xc_start(struct xc *x)
  52. {
  53. writel(0, x->xmac_base + NETX_XMAC_RPU_HOLD_PC_OFS);
  54. writel(0, x->xmac_base + NETX_XMAC_TPU_HOLD_PC_OFS);
  55. writel(0, x->xpec_base + NETX_XPEC_XPU_HOLD_PC_OFS);
  56. return 0;
  57. }
  58. int xc_running(struct xc *x)
  59. {
  60. return (readl(x->xmac_base + NETX_XMAC_RPU_HOLD_PC_OFS) & RPU_HOLD_PC)
  61. || (readl(x->xmac_base + NETX_XMAC_TPU_HOLD_PC_OFS) & TPU_HOLD_PC)
  62. || (readl(x->xpec_base + NETX_XPEC_XPU_HOLD_PC_OFS) & XPU_HOLD_PC) ?
  63. 0 : 1;
  64. }
  65. int xc_reset(struct xc *x)
  66. {
  67. writel(0, x->xpec_base + NETX_XPEC_PC_OFS);
  68. return 0;
  69. }
  70. static int xc_check_ptr(struct xc *x, unsigned long adr, unsigned int size)
  71. {
  72. if (adr >= NETX_PA_XMAC(x->no) &&
  73. adr + size < NETX_PA_XMAC(x->no) + XMAC_MEM_SIZE)
  74. return 0;
  75. if (adr >= NETX_PA_XPEC(x->no) &&
  76. adr + size < NETX_PA_XPEC(x->no) + XPEC_MEM_SIZE)
  77. return 0;
  78. dev_err(x->dev, "Illegal pointer in firmware found. aborting\n");
  79. return -1;
  80. }
  81. static int xc_patch(struct xc *x, const void *patch, int count)
  82. {
  83. unsigned int val, adr;
  84. const unsigned int *data = patch;
  85. int i;
  86. for (i = 0; i < count; i++) {
  87. adr = *data++;
  88. val = *data++;
  89. if (xc_check_ptr(x, adr, 4) < 0)
  90. return -EINVAL;
  91. writel(val, (void __iomem *)io_p2v(adr));
  92. }
  93. return 0;
  94. }
  95. int xc_request_firmware(struct xc *x)
  96. {
  97. int ret;
  98. char name[16];
  99. const struct firmware *fw;
  100. struct fw_header *head;
  101. unsigned int size;
  102. int i;
  103. const void *src;
  104. unsigned long dst;
  105. sprintf(name, "xc%d.bin", x->no);
  106. ret = request_firmware(&fw, name, x->dev);
  107. if (ret < 0) {
  108. dev_err(x->dev, "request_firmware failed\n");
  109. return ret;
  110. }
  111. head = (struct fw_header *)fw->data;
  112. if (head->magic != 0x4e657458) {
  113. if (head->magic == 0x5874654e) {
  114. dev_err(x->dev,
  115. "firmware magic is 'XteN'. Endianess problems?\n");
  116. ret = -ENODEV;
  117. goto exit_release_firmware;
  118. }
  119. dev_err(x->dev, "unrecognized firmware magic 0x%08x\n",
  120. head->magic);
  121. ret = -ENODEV;
  122. goto exit_release_firmware;
  123. }
  124. x->type = head->type;
  125. x->version = head->version;
  126. ret = -EINVAL;
  127. for (i = 0; i < 3; i++) {
  128. src = fw->data + head->fw_desc[i].ofs;
  129. dst = *(unsigned int *)src;
  130. src += sizeof (unsigned int);
  131. size = head->fw_desc[i].size - sizeof (unsigned int);
  132. if (xc_check_ptr(x, dst, size))
  133. goto exit_release_firmware;
  134. memcpy((void *)io_p2v(dst), src, size);
  135. src = fw->data + head->fw_desc[i].patch_ofs;
  136. size = head->fw_desc[i].patch_entries;
  137. ret = xc_patch(x, src, size);
  138. if (ret < 0)
  139. goto exit_release_firmware;
  140. }
  141. ret = 0;
  142. exit_release_firmware:
  143. release_firmware(fw);
  144. return ret;
  145. }
  146. struct xc *request_xc(int xcno, struct device *dev)
  147. {
  148. struct xc *x = NULL;
  149. mutex_lock(&xc_lock);
  150. if (xcno > 3)
  151. goto exit;
  152. if (xc_in_use & (1 << xcno))
  153. goto exit;
  154. x = kmalloc(sizeof (struct xc), GFP_KERNEL);
  155. if (!x)
  156. goto exit;
  157. if (!request_mem_region
  158. (NETX_PA_XPEC(xcno), XPEC_MEM_SIZE, kobject_name(&dev->kobj)))
  159. goto exit_free;
  160. if (!request_mem_region
  161. (NETX_PA_XMAC(xcno), XMAC_MEM_SIZE, kobject_name(&dev->kobj)))
  162. goto exit_release_1;
  163. if (!request_mem_region
  164. (SRAM_INTERNAL_PHYS(xcno), SRAM_MEM_SIZE, kobject_name(&dev->kobj)))
  165. goto exit_release_2;
  166. x->xpec_base = (void * __iomem)io_p2v(NETX_PA_XPEC(xcno));
  167. x->xmac_base = (void * __iomem)io_p2v(NETX_PA_XMAC(xcno));
  168. x->sram_base = ioremap(SRAM_INTERNAL_PHYS(xcno), SRAM_MEM_SIZE);
  169. if (!x->sram_base)
  170. goto exit_release_3;
  171. x->irq = NETX_IRQ_XPEC(xcno);
  172. x->no = xcno;
  173. x->dev = dev;
  174. xc_in_use |= (1 << xcno);
  175. goto exit;
  176. exit_release_3:
  177. release_mem_region(SRAM_INTERNAL_PHYS(xcno), SRAM_MEM_SIZE);
  178. exit_release_2:
  179. release_mem_region(NETX_PA_XMAC(xcno), XMAC_MEM_SIZE);
  180. exit_release_1:
  181. release_mem_region(NETX_PA_XPEC(xcno), XPEC_MEM_SIZE);
  182. exit_free:
  183. kfree(x);
  184. x = NULL;
  185. exit:
  186. mutex_unlock(&xc_lock);
  187. return x;
  188. }
  189. void free_xc(struct xc *x)
  190. {
  191. int xcno = x->no;
  192. mutex_lock(&xc_lock);
  193. iounmap(x->sram_base);
  194. release_mem_region(SRAM_INTERNAL_PHYS(xcno), SRAM_MEM_SIZE);
  195. release_mem_region(NETX_PA_XMAC(xcno), XMAC_MEM_SIZE);
  196. release_mem_region(NETX_PA_XPEC(xcno), XPEC_MEM_SIZE);
  197. xc_in_use &= ~(1 << x->no);
  198. kfree(x);
  199. mutex_unlock(&xc_lock);
  200. }
  201. EXPORT_SYMBOL(free_xc);
  202. EXPORT_SYMBOL(request_xc);
  203. EXPORT_SYMBOL(xc_request_firmware);
  204. EXPORT_SYMBOL(xc_reset);
  205. EXPORT_SYMBOL(xc_running);
  206. EXPORT_SYMBOL(xc_start);
  207. EXPORT_SYMBOL(xc_stop);