xc.c 5.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256
  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/io.h>
  24. #include <mach/hardware.h>
  25. #include <mach/irqs.h>
  26. #include <mach/netx-regs.h>
  27. #include <mach/xc.h>
  28. static DEFINE_MUTEX(xc_lock);
  29. static int xc_in_use = 0;
  30. struct fw_desc {
  31. unsigned int ofs;
  32. unsigned int size;
  33. unsigned int patch_ofs;
  34. unsigned int patch_entries;
  35. };
  36. struct fw_header {
  37. unsigned int magic;
  38. unsigned int type;
  39. unsigned int version;
  40. unsigned int reserved[5];
  41. struct fw_desc fw_desc[3];
  42. } __attribute__ ((packed));
  43. int xc_stop(struct xc *x)
  44. {
  45. writel(RPU_HOLD_PC, x->xmac_base + NETX_XMAC_RPU_HOLD_PC_OFS);
  46. writel(TPU_HOLD_PC, x->xmac_base + NETX_XMAC_TPU_HOLD_PC_OFS);
  47. writel(XPU_HOLD_PC, x->xpec_base + NETX_XPEC_XPU_HOLD_PC_OFS);
  48. return 0;
  49. }
  50. int xc_start(struct xc *x)
  51. {
  52. writel(0, x->xmac_base + NETX_XMAC_RPU_HOLD_PC_OFS);
  53. writel(0, x->xmac_base + NETX_XMAC_TPU_HOLD_PC_OFS);
  54. writel(0, x->xpec_base + NETX_XPEC_XPU_HOLD_PC_OFS);
  55. return 0;
  56. }
  57. int xc_running(struct xc *x)
  58. {
  59. return (readl(x->xmac_base + NETX_XMAC_RPU_HOLD_PC_OFS) & RPU_HOLD_PC)
  60. || (readl(x->xmac_base + NETX_XMAC_TPU_HOLD_PC_OFS) & TPU_HOLD_PC)
  61. || (readl(x->xpec_base + NETX_XPEC_XPU_HOLD_PC_OFS) & XPU_HOLD_PC) ?
  62. 0 : 1;
  63. }
  64. int xc_reset(struct xc *x)
  65. {
  66. writel(0, x->xpec_base + NETX_XPEC_PC_OFS);
  67. return 0;
  68. }
  69. static int xc_check_ptr(struct xc *x, unsigned long adr, unsigned int size)
  70. {
  71. if (adr >= NETX_PA_XMAC(x->no) &&
  72. adr + size < NETX_PA_XMAC(x->no) + XMAC_MEM_SIZE)
  73. return 0;
  74. if (adr >= NETX_PA_XPEC(x->no) &&
  75. adr + size < NETX_PA_XPEC(x->no) + XPEC_MEM_SIZE)
  76. return 0;
  77. dev_err(x->dev, "Illegal pointer in firmware found. aborting\n");
  78. return -1;
  79. }
  80. static int xc_patch(struct xc *x, const void *patch, int count)
  81. {
  82. unsigned int val, adr;
  83. const unsigned int *data = patch;
  84. int i;
  85. for (i = 0; i < count; i++) {
  86. adr = *data++;
  87. val = *data++;
  88. if (xc_check_ptr(x, adr, 4) < 0)
  89. return -EINVAL;
  90. writel(val, (void __iomem *)io_p2v(adr));
  91. }
  92. return 0;
  93. }
  94. int xc_request_firmware(struct xc *x)
  95. {
  96. int ret;
  97. char name[16];
  98. const struct firmware *fw;
  99. struct fw_header *head;
  100. unsigned int size;
  101. int i;
  102. const void *src;
  103. unsigned long dst;
  104. sprintf(name, "xc%d.bin", x->no);
  105. ret = request_firmware(&fw, name, x->dev);
  106. if (ret < 0) {
  107. dev_err(x->dev, "request_firmware failed\n");
  108. return ret;
  109. }
  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'. Endianess 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);