xc.c 5.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259
  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 <linux/export.h>
  26. #include <mach/hardware.h>
  27. #include <mach/irqs.h>
  28. #include <mach/netx-regs.h>
  29. #include <mach/xc.h>
  30. static DEFINE_MUTEX(xc_lock);
  31. static int xc_in_use = 0;
  32. struct fw_desc {
  33. unsigned int ofs;
  34. unsigned int size;
  35. unsigned int patch_ofs;
  36. unsigned int patch_entries;
  37. };
  38. struct fw_header {
  39. unsigned int magic;
  40. unsigned int type;
  41. unsigned int version;
  42. unsigned int reserved[5];
  43. struct fw_desc fw_desc[3];
  44. } __attribute__ ((packed));
  45. int xc_stop(struct xc *x)
  46. {
  47. writel(RPU_HOLD_PC, x->xmac_base + NETX_XMAC_RPU_HOLD_PC_OFS);
  48. writel(TPU_HOLD_PC, x->xmac_base + NETX_XMAC_TPU_HOLD_PC_OFS);
  49. writel(XPU_HOLD_PC, x->xpec_base + NETX_XPEC_XPU_HOLD_PC_OFS);
  50. return 0;
  51. }
  52. int xc_start(struct xc *x)
  53. {
  54. writel(0, x->xmac_base + NETX_XMAC_RPU_HOLD_PC_OFS);
  55. writel(0, x->xmac_base + NETX_XMAC_TPU_HOLD_PC_OFS);
  56. writel(0, x->xpec_base + NETX_XPEC_XPU_HOLD_PC_OFS);
  57. return 0;
  58. }
  59. int xc_running(struct xc *x)
  60. {
  61. return (readl(x->xmac_base + NETX_XMAC_RPU_HOLD_PC_OFS) & RPU_HOLD_PC)
  62. || (readl(x->xmac_base + NETX_XMAC_TPU_HOLD_PC_OFS) & TPU_HOLD_PC)
  63. || (readl(x->xpec_base + NETX_XPEC_XPU_HOLD_PC_OFS) & XPU_HOLD_PC) ?
  64. 0 : 1;
  65. }
  66. int xc_reset(struct xc *x)
  67. {
  68. writel(0, x->xpec_base + NETX_XPEC_PC_OFS);
  69. return 0;
  70. }
  71. static int xc_check_ptr(struct xc *x, unsigned long adr, unsigned int size)
  72. {
  73. if (adr >= NETX_PA_XMAC(x->no) &&
  74. adr + size < NETX_PA_XMAC(x->no) + XMAC_MEM_SIZE)
  75. return 0;
  76. if (adr >= NETX_PA_XPEC(x->no) &&
  77. adr + size < NETX_PA_XPEC(x->no) + XPEC_MEM_SIZE)
  78. return 0;
  79. dev_err(x->dev, "Illegal pointer in firmware found. aborting\n");
  80. return -1;
  81. }
  82. static int xc_patch(struct xc *x, const void *patch, int count)
  83. {
  84. unsigned int val, adr;
  85. const unsigned int *data = patch;
  86. int i;
  87. for (i = 0; i < count; i++) {
  88. adr = *data++;
  89. val = *data++;
  90. if (xc_check_ptr(x, adr, 4) < 0)
  91. return -EINVAL;
  92. writel(val, (void __iomem *)io_p2v(adr));
  93. }
  94. return 0;
  95. }
  96. int xc_request_firmware(struct xc *x)
  97. {
  98. int ret;
  99. char name[16];
  100. const struct firmware *fw;
  101. struct fw_header *head;
  102. unsigned int size;
  103. int i;
  104. const void *src;
  105. unsigned long dst;
  106. sprintf(name, "/*(DEBLOBBED)*/", x->no);
  107. ret = reject_firmware(&fw, name, x->dev);
  108. if (ret < 0) {
  109. dev_err(x->dev, "request_firmware failed\n");
  110. return ret;
  111. }
  112. head = (struct fw_header *)fw->data;
  113. if (head->magic != 0x4e657458) {
  114. if (head->magic == 0x5874654e) {
  115. dev_err(x->dev,
  116. "firmware magic is 'XteN'. Endianness problems?\n");
  117. ret = -ENODEV;
  118. goto exit_release_firmware;
  119. }
  120. dev_err(x->dev, "unrecognized firmware magic 0x%08x\n",
  121. head->magic);
  122. ret = -ENODEV;
  123. goto exit_release_firmware;
  124. }
  125. x->type = head->type;
  126. x->version = head->version;
  127. ret = -EINVAL;
  128. for (i = 0; i < 3; i++) {
  129. src = fw->data + head->fw_desc[i].ofs;
  130. dst = *(unsigned int *)src;
  131. src += sizeof (unsigned int);
  132. size = head->fw_desc[i].size - sizeof (unsigned int);
  133. if (xc_check_ptr(x, dst, size))
  134. goto exit_release_firmware;
  135. memcpy((void *)io_p2v(dst), src, size);
  136. src = fw->data + head->fw_desc[i].patch_ofs;
  137. size = head->fw_desc[i].patch_entries;
  138. ret = xc_patch(x, src, size);
  139. if (ret < 0)
  140. goto exit_release_firmware;
  141. }
  142. ret = 0;
  143. exit_release_firmware:
  144. release_firmware(fw);
  145. return ret;
  146. }
  147. struct xc *request_xc(int xcno, struct device *dev)
  148. {
  149. struct xc *x = NULL;
  150. mutex_lock(&xc_lock);
  151. if (xcno > 3)
  152. goto exit;
  153. if (xc_in_use & (1 << xcno))
  154. goto exit;
  155. x = kmalloc(sizeof (struct xc), GFP_KERNEL);
  156. if (!x)
  157. goto exit;
  158. if (!request_mem_region
  159. (NETX_PA_XPEC(xcno), XPEC_MEM_SIZE, kobject_name(&dev->kobj)))
  160. goto exit_free;
  161. if (!request_mem_region
  162. (NETX_PA_XMAC(xcno), XMAC_MEM_SIZE, kobject_name(&dev->kobj)))
  163. goto exit_release_1;
  164. if (!request_mem_region
  165. (SRAM_INTERNAL_PHYS(xcno), SRAM_MEM_SIZE, kobject_name(&dev->kobj)))
  166. goto exit_release_2;
  167. x->xpec_base = (void * __iomem)io_p2v(NETX_PA_XPEC(xcno));
  168. x->xmac_base = (void * __iomem)io_p2v(NETX_PA_XMAC(xcno));
  169. x->sram_base = ioremap(SRAM_INTERNAL_PHYS(xcno), SRAM_MEM_SIZE);
  170. if (!x->sram_base)
  171. goto exit_release_3;
  172. x->irq = NETX_IRQ_XPEC(xcno);
  173. x->no = xcno;
  174. x->dev = dev;
  175. xc_in_use |= (1 << xcno);
  176. goto exit;
  177. exit_release_3:
  178. release_mem_region(SRAM_INTERNAL_PHYS(xcno), SRAM_MEM_SIZE);
  179. exit_release_2:
  180. release_mem_region(NETX_PA_XMAC(xcno), XMAC_MEM_SIZE);
  181. exit_release_1:
  182. release_mem_region(NETX_PA_XPEC(xcno), XPEC_MEM_SIZE);
  183. exit_free:
  184. kfree(x);
  185. x = NULL;
  186. exit:
  187. mutex_unlock(&xc_lock);
  188. return x;
  189. }
  190. void free_xc(struct xc *x)
  191. {
  192. int xcno = x->no;
  193. mutex_lock(&xc_lock);
  194. iounmap(x->sram_base);
  195. release_mem_region(SRAM_INTERNAL_PHYS(xcno), SRAM_MEM_SIZE);
  196. release_mem_region(NETX_PA_XMAC(xcno), XMAC_MEM_SIZE);
  197. release_mem_region(NETX_PA_XPEC(xcno), XPEC_MEM_SIZE);
  198. xc_in_use &= ~(1 << x->no);
  199. kfree(x);
  200. mutex_unlock(&xc_lock);
  201. }
  202. EXPORT_SYMBOL(free_xc);
  203. EXPORT_SYMBOL(request_xc);
  204. EXPORT_SYMBOL(xc_request_firmware);
  205. EXPORT_SYMBOL(xc_reset);
  206. EXPORT_SYMBOL(xc_running);
  207. EXPORT_SYMBOL(xc_start);
  208. EXPORT_SYMBOL(xc_stop);