xc.c 5.7 KB

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