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 <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, "xc%d.bin", x->no);
  107. ret = request_firmware(&fw, name, x->dev);
  108. if (ret)
  109. return ret;
  110. head = (struct fw_header *)fw->data;
  111. if (head->magic != 0x4e657458) {
  112. if (head->magic == 0x5874654e) {
  113. dev_err(x->dev,
  114. "firmware magic is 'XteN'. Endianness problems?\n");
  115. ret = -ENODEV;
  116. goto exit_release_firmware;
  117. }
  118. dev_err(x->dev, "unrecognized firmware magic 0x%08x\n",
  119. head->magic);
  120. ret = -ENODEV;
  121. goto exit_release_firmware;
  122. }
  123. x->type = head->type;
  124. x->version = head->version;
  125. ret = -EINVAL;
  126. for (i = 0; i < 3; i++) {
  127. src = fw->data + head->fw_desc[i].ofs;
  128. dst = *(unsigned int *)src;
  129. src += sizeof (unsigned int);
  130. size = head->fw_desc[i].size - sizeof (unsigned int);
  131. if (xc_check_ptr(x, dst, size))
  132. goto exit_release_firmware;
  133. memcpy((void *)io_p2v(dst), src, size);
  134. src = fw->data + head->fw_desc[i].patch_ofs;
  135. size = head->fw_desc[i].patch_entries;
  136. ret = xc_patch(x, src, size);
  137. if (ret < 0)
  138. goto exit_release_firmware;
  139. }
  140. ret = 0;
  141. exit_release_firmware:
  142. release_firmware(fw);
  143. return ret;
  144. }
  145. struct xc *request_xc(int xcno, struct device *dev)
  146. {
  147. struct xc *x = NULL;
  148. mutex_lock(&xc_lock);
  149. if (xcno > 3)
  150. goto exit;
  151. if (xc_in_use & (1 << xcno))
  152. goto exit;
  153. x = kmalloc(sizeof (struct xc), GFP_KERNEL);
  154. if (!x)
  155. goto exit;
  156. if (!request_mem_region
  157. (NETX_PA_XPEC(xcno), XPEC_MEM_SIZE, kobject_name(&dev->kobj)))
  158. goto exit_free;
  159. if (!request_mem_region
  160. (NETX_PA_XMAC(xcno), XMAC_MEM_SIZE, kobject_name(&dev->kobj)))
  161. goto exit_release_1;
  162. if (!request_mem_region
  163. (SRAM_INTERNAL_PHYS(xcno), SRAM_MEM_SIZE, kobject_name(&dev->kobj)))
  164. goto exit_release_2;
  165. x->xpec_base = (void * __iomem)io_p2v(NETX_PA_XPEC(xcno));
  166. x->xmac_base = (void * __iomem)io_p2v(NETX_PA_XMAC(xcno));
  167. x->sram_base = ioremap(SRAM_INTERNAL_PHYS(xcno), SRAM_MEM_SIZE);
  168. if (!x->sram_base)
  169. goto exit_release_3;
  170. x->irq = NETX_IRQ_XPEC(xcno);
  171. x->no = xcno;
  172. x->dev = dev;
  173. xc_in_use |= (1 << xcno);
  174. goto exit;
  175. exit_release_3:
  176. release_mem_region(SRAM_INTERNAL_PHYS(xcno), SRAM_MEM_SIZE);
  177. exit_release_2:
  178. release_mem_region(NETX_PA_XMAC(xcno), XMAC_MEM_SIZE);
  179. exit_release_1:
  180. release_mem_region(NETX_PA_XPEC(xcno), XPEC_MEM_SIZE);
  181. exit_free:
  182. kfree(x);
  183. x = NULL;
  184. exit:
  185. mutex_unlock(&xc_lock);
  186. return x;
  187. }
  188. void free_xc(struct xc *x)
  189. {
  190. int xcno = x->no;
  191. mutex_lock(&xc_lock);
  192. iounmap(x->sram_base);
  193. release_mem_region(SRAM_INTERNAL_PHYS(xcno), SRAM_MEM_SIZE);
  194. release_mem_region(NETX_PA_XMAC(xcno), XMAC_MEM_SIZE);
  195. release_mem_region(NETX_PA_XPEC(xcno), XPEC_MEM_SIZE);
  196. xc_in_use &= ~(1 << x->no);
  197. kfree(x);
  198. mutex_unlock(&xc_lock);
  199. }
  200. EXPORT_SYMBOL(free_xc);
  201. EXPORT_SYMBOL(request_xc);
  202. EXPORT_SYMBOL(xc_request_firmware);
  203. EXPORT_SYMBOL(xc_reset);
  204. EXPORT_SYMBOL(xc_running);
  205. EXPORT_SYMBOL(xc_start);
  206. EXPORT_SYMBOL(xc_stop);