ixp4xx_npe.c 21 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741
  1. /*
  2. * Intel IXP4xx Network Processor Engine driver for Linux
  3. *
  4. * Copyright (C) 2007 Krzysztof Halasa <khc@pm.waw.pl>
  5. *
  6. * This program is free software; you can redistribute it and/or modify it
  7. * under the terms of version 2 of the GNU General Public License
  8. * as published by the Free Software Foundation.
  9. *
  10. * The code is based on publicly available information:
  11. * - Intel IXP4xx Developer's Manual and other e-papers
  12. * - Intel IXP400 Access Library Software (BSD license)
  13. * - previous works by Christian Hohnstaedt <chohnstaedt@innominate.com>
  14. * Thanks, Christian.
  15. */
  16. #include <linux/delay.h>
  17. #include <linux/dma-mapping.h>
  18. #include <linux/firmware.h>
  19. #include <linux/io.h>
  20. #include <linux/kernel.h>
  21. #include <linux/module.h>
  22. #include <mach/npe.h>
  23. #define DEBUG_MSG 0
  24. #define DEBUG_FW 0
  25. #define NPE_COUNT 3
  26. #define MAX_RETRIES 1000 /* microseconds */
  27. #define NPE_42X_DATA_SIZE 0x800 /* in dwords */
  28. #define NPE_46X_DATA_SIZE 0x1000
  29. #define NPE_A_42X_INSTR_SIZE 0x1000
  30. #define NPE_B_AND_C_42X_INSTR_SIZE 0x800
  31. #define NPE_46X_INSTR_SIZE 0x1000
  32. #define REGS_SIZE 0x1000
  33. #define NPE_PHYS_REG 32
  34. #define FW_MAGIC 0xFEEDF00D
  35. #define FW_BLOCK_TYPE_INSTR 0x0
  36. #define FW_BLOCK_TYPE_DATA 0x1
  37. #define FW_BLOCK_TYPE_EOF 0xF
  38. /* NPE exec status (read) and command (write) */
  39. #define CMD_NPE_STEP 0x01
  40. #define CMD_NPE_START 0x02
  41. #define CMD_NPE_STOP 0x03
  42. #define CMD_NPE_CLR_PIPE 0x04
  43. #define CMD_CLR_PROFILE_CNT 0x0C
  44. #define CMD_RD_INS_MEM 0x10 /* instruction memory */
  45. #define CMD_WR_INS_MEM 0x11
  46. #define CMD_RD_DATA_MEM 0x12 /* data memory */
  47. #define CMD_WR_DATA_MEM 0x13
  48. #define CMD_RD_ECS_REG 0x14 /* exec access register */
  49. #define CMD_WR_ECS_REG 0x15
  50. #define STAT_RUN 0x80000000
  51. #define STAT_STOP 0x40000000
  52. #define STAT_CLEAR 0x20000000
  53. #define STAT_ECS_K 0x00800000 /* pipeline clean */
  54. #define NPE_STEVT 0x1B
  55. #define NPE_STARTPC 0x1C
  56. #define NPE_REGMAP 0x1E
  57. #define NPE_CINDEX 0x1F
  58. #define INSTR_WR_REG_SHORT 0x0000C000
  59. #define INSTR_WR_REG_BYTE 0x00004000
  60. #define INSTR_RD_FIFO 0x0F888220
  61. #define INSTR_RESET_MBOX 0x0FAC8210
  62. #define ECS_BG_CTXT_REG_0 0x00 /* Background Executing Context */
  63. #define ECS_BG_CTXT_REG_1 0x01 /* Stack level */
  64. #define ECS_BG_CTXT_REG_2 0x02
  65. #define ECS_PRI_1_CTXT_REG_0 0x04 /* Priority 1 Executing Context */
  66. #define ECS_PRI_1_CTXT_REG_1 0x05 /* Stack level */
  67. #define ECS_PRI_1_CTXT_REG_2 0x06
  68. #define ECS_PRI_2_CTXT_REG_0 0x08 /* Priority 2 Executing Context */
  69. #define ECS_PRI_2_CTXT_REG_1 0x09 /* Stack level */
  70. #define ECS_PRI_2_CTXT_REG_2 0x0A
  71. #define ECS_DBG_CTXT_REG_0 0x0C /* Debug Executing Context */
  72. #define ECS_DBG_CTXT_REG_1 0x0D /* Stack level */
  73. #define ECS_DBG_CTXT_REG_2 0x0E
  74. #define ECS_INSTRUCT_REG 0x11 /* NPE Instruction Register */
  75. #define ECS_REG_0_ACTIVE 0x80000000 /* all levels */
  76. #define ECS_REG_0_NEXTPC_MASK 0x1FFF0000 /* BG/PRI1/PRI2 levels */
  77. #define ECS_REG_0_LDUR_BITS 8
  78. #define ECS_REG_0_LDUR_MASK 0x00000700 /* all levels */
  79. #define ECS_REG_1_CCTXT_BITS 16
  80. #define ECS_REG_1_CCTXT_MASK 0x000F0000 /* all levels */
  81. #define ECS_REG_1_SELCTXT_BITS 0
  82. #define ECS_REG_1_SELCTXT_MASK 0x0000000F /* all levels */
  83. #define ECS_DBG_REG_2_IF 0x00100000 /* debug level */
  84. #define ECS_DBG_REG_2_IE 0x00080000 /* debug level */
  85. /* NPE watchpoint_fifo register bit */
  86. #define WFIFO_VALID 0x80000000
  87. /* NPE messaging_status register bit definitions */
  88. #define MSGSTAT_OFNE 0x00010000 /* OutFifoNotEmpty */
  89. #define MSGSTAT_IFNF 0x00020000 /* InFifoNotFull */
  90. #define MSGSTAT_OFNF 0x00040000 /* OutFifoNotFull */
  91. #define MSGSTAT_IFNE 0x00080000 /* InFifoNotEmpty */
  92. #define MSGSTAT_MBINT 0x00100000 /* Mailbox interrupt */
  93. #define MSGSTAT_IFINT 0x00200000 /* InFifo interrupt */
  94. #define MSGSTAT_OFINT 0x00400000 /* OutFifo interrupt */
  95. #define MSGSTAT_WFINT 0x00800000 /* WatchFifo interrupt */
  96. /* NPE messaging_control register bit definitions */
  97. #define MSGCTL_OUT_FIFO 0x00010000 /* enable output FIFO */
  98. #define MSGCTL_IN_FIFO 0x00020000 /* enable input FIFO */
  99. #define MSGCTL_OUT_FIFO_WRITE 0x01000000 /* enable FIFO + WRITE */
  100. #define MSGCTL_IN_FIFO_WRITE 0x02000000
  101. /* NPE mailbox_status value for reset */
  102. #define RESET_MBOX_STAT 0x0000F0F0
  103. #define NPE_A_FIRMWARE "NPE-A"
  104. #define NPE_B_FIRMWARE "NPE-B"
  105. #define NPE_C_FIRMWARE "NPE-C"
  106. const char *npe_names[] = { NPE_A_FIRMWARE, NPE_B_FIRMWARE, NPE_C_FIRMWARE };
  107. #define print_npe(pri, npe, fmt, ...) \
  108. printk(pri "%s: " fmt, npe_name(npe), ## __VA_ARGS__)
  109. #if DEBUG_MSG
  110. #define debug_msg(npe, fmt, ...) \
  111. print_npe(KERN_DEBUG, npe, fmt, ## __VA_ARGS__)
  112. #else
  113. #define debug_msg(npe, fmt, ...)
  114. #endif
  115. static struct {
  116. u32 reg, val;
  117. } ecs_reset[] = {
  118. { ECS_BG_CTXT_REG_0, 0xA0000000 },
  119. { ECS_BG_CTXT_REG_1, 0x01000000 },
  120. { ECS_BG_CTXT_REG_2, 0x00008000 },
  121. { ECS_PRI_1_CTXT_REG_0, 0x20000080 },
  122. { ECS_PRI_1_CTXT_REG_1, 0x01000000 },
  123. { ECS_PRI_1_CTXT_REG_2, 0x00008000 },
  124. { ECS_PRI_2_CTXT_REG_0, 0x20000080 },
  125. { ECS_PRI_2_CTXT_REG_1, 0x01000000 },
  126. { ECS_PRI_2_CTXT_REG_2, 0x00008000 },
  127. { ECS_DBG_CTXT_REG_0, 0x20000000 },
  128. { ECS_DBG_CTXT_REG_1, 0x00000000 },
  129. { ECS_DBG_CTXT_REG_2, 0x001E0000 },
  130. { ECS_INSTRUCT_REG, 0x1003C00F },
  131. };
  132. static struct npe npe_tab[NPE_COUNT] = {
  133. {
  134. .id = 0,
  135. .regs = (struct npe_regs __iomem *)IXP4XX_NPEA_BASE_VIRT,
  136. .regs_phys = IXP4XX_NPEA_BASE_PHYS,
  137. }, {
  138. .id = 1,
  139. .regs = (struct npe_regs __iomem *)IXP4XX_NPEB_BASE_VIRT,
  140. .regs_phys = IXP4XX_NPEB_BASE_PHYS,
  141. }, {
  142. .id = 2,
  143. .regs = (struct npe_regs __iomem *)IXP4XX_NPEC_BASE_VIRT,
  144. .regs_phys = IXP4XX_NPEC_BASE_PHYS,
  145. }
  146. };
  147. int npe_running(struct npe *npe)
  148. {
  149. return (__raw_readl(&npe->regs->exec_status_cmd) & STAT_RUN) != 0;
  150. }
  151. static void npe_cmd_write(struct npe *npe, u32 addr, int cmd, u32 data)
  152. {
  153. __raw_writel(data, &npe->regs->exec_data);
  154. __raw_writel(addr, &npe->regs->exec_addr);
  155. __raw_writel(cmd, &npe->regs->exec_status_cmd);
  156. }
  157. static u32 npe_cmd_read(struct npe *npe, u32 addr, int cmd)
  158. {
  159. __raw_writel(addr, &npe->regs->exec_addr);
  160. __raw_writel(cmd, &npe->regs->exec_status_cmd);
  161. /* Iintroduce extra read cycles after issuing read command to NPE
  162. so that we read the register after the NPE has updated it.
  163. This is to overcome race condition between XScale and NPE */
  164. __raw_readl(&npe->regs->exec_data);
  165. __raw_readl(&npe->regs->exec_data);
  166. return __raw_readl(&npe->regs->exec_data);
  167. }
  168. static void npe_clear_active(struct npe *npe, u32 reg)
  169. {
  170. u32 val = npe_cmd_read(npe, reg, CMD_RD_ECS_REG);
  171. npe_cmd_write(npe, reg, CMD_WR_ECS_REG, val & ~ECS_REG_0_ACTIVE);
  172. }
  173. static void npe_start(struct npe *npe)
  174. {
  175. /* ensure only Background Context Stack Level is active */
  176. npe_clear_active(npe, ECS_PRI_1_CTXT_REG_0);
  177. npe_clear_active(npe, ECS_PRI_2_CTXT_REG_0);
  178. npe_clear_active(npe, ECS_DBG_CTXT_REG_0);
  179. __raw_writel(CMD_NPE_CLR_PIPE, &npe->regs->exec_status_cmd);
  180. __raw_writel(CMD_NPE_START, &npe->regs->exec_status_cmd);
  181. }
  182. static void npe_stop(struct npe *npe)
  183. {
  184. __raw_writel(CMD_NPE_STOP, &npe->regs->exec_status_cmd);
  185. __raw_writel(CMD_NPE_CLR_PIPE, &npe->regs->exec_status_cmd); /*FIXME?*/
  186. }
  187. static int __must_check npe_debug_instr(struct npe *npe, u32 instr, u32 ctx,
  188. u32 ldur)
  189. {
  190. u32 wc;
  191. int i;
  192. /* set the Active bit, and the LDUR, in the debug level */
  193. npe_cmd_write(npe, ECS_DBG_CTXT_REG_0, CMD_WR_ECS_REG,
  194. ECS_REG_0_ACTIVE | (ldur << ECS_REG_0_LDUR_BITS));
  195. /* set CCTXT at ECS DEBUG L3 to specify in which context to execute
  196. the instruction, and set SELCTXT at ECS DEBUG Level to specify
  197. which context store to access.
  198. Debug ECS Level Reg 1 has form 0x000n000n, where n = context number
  199. */
  200. npe_cmd_write(npe, ECS_DBG_CTXT_REG_1, CMD_WR_ECS_REG,
  201. (ctx << ECS_REG_1_CCTXT_BITS) |
  202. (ctx << ECS_REG_1_SELCTXT_BITS));
  203. /* clear the pipeline */
  204. __raw_writel(CMD_NPE_CLR_PIPE, &npe->regs->exec_status_cmd);
  205. /* load NPE instruction into the instruction register */
  206. npe_cmd_write(npe, ECS_INSTRUCT_REG, CMD_WR_ECS_REG, instr);
  207. /* we need this value later to wait for completion of NPE execution
  208. step */
  209. wc = __raw_readl(&npe->regs->watch_count);
  210. /* issue a Step One command via the Execution Control register */
  211. __raw_writel(CMD_NPE_STEP, &npe->regs->exec_status_cmd);
  212. /* Watch Count register increments when NPE completes an instruction */
  213. for (i = 0; i < MAX_RETRIES; i++) {
  214. if (wc != __raw_readl(&npe->regs->watch_count))
  215. return 0;
  216. udelay(1);
  217. }
  218. print_npe(KERN_ERR, npe, "reset: npe_debug_instr(): timeout\n");
  219. return -ETIMEDOUT;
  220. }
  221. static int __must_check npe_logical_reg_write8(struct npe *npe, u32 addr,
  222. u8 val, u32 ctx)
  223. {
  224. /* here we build the NPE assembler instruction: mov8 d0, #0 */
  225. u32 instr = INSTR_WR_REG_BYTE | /* OpCode */
  226. addr << 9 | /* base Operand */
  227. (val & 0x1F) << 4 | /* lower 5 bits to immediate data */
  228. (val & ~0x1F) << (18 - 5);/* higher 3 bits to CoProc instr. */
  229. return npe_debug_instr(npe, instr, ctx, 1); /* execute it */
  230. }
  231. static int __must_check npe_logical_reg_write16(struct npe *npe, u32 addr,
  232. u16 val, u32 ctx)
  233. {
  234. /* here we build the NPE assembler instruction: mov16 d0, #0 */
  235. u32 instr = INSTR_WR_REG_SHORT | /* OpCode */
  236. addr << 9 | /* base Operand */
  237. (val & 0x1F) << 4 | /* lower 5 bits to immediate data */
  238. (val & ~0x1F) << (18 - 5);/* higher 11 bits to CoProc instr. */
  239. return npe_debug_instr(npe, instr, ctx, 1); /* execute it */
  240. }
  241. static int __must_check npe_logical_reg_write32(struct npe *npe, u32 addr,
  242. u32 val, u32 ctx)
  243. {
  244. /* write in 16 bit steps first the high and then the low value */
  245. if (npe_logical_reg_write16(npe, addr, val >> 16, ctx))
  246. return -ETIMEDOUT;
  247. return npe_logical_reg_write16(npe, addr + 2, val & 0xFFFF, ctx);
  248. }
  249. static int npe_reset(struct npe *npe)
  250. {
  251. u32 val, ctl, exec_count, ctx_reg2;
  252. int i;
  253. ctl = (__raw_readl(&npe->regs->messaging_control) | 0x3F000000) &
  254. 0x3F3FFFFF;
  255. /* disable parity interrupt */
  256. __raw_writel(ctl & 0x3F00FFFF, &npe->regs->messaging_control);
  257. /* pre exec - debug instruction */
  258. /* turn off the halt bit by clearing Execution Count register. */
  259. exec_count = __raw_readl(&npe->regs->exec_count);
  260. __raw_writel(0, &npe->regs->exec_count);
  261. /* ensure that IF and IE are on (temporarily), so that we don't end up
  262. stepping forever */
  263. ctx_reg2 = npe_cmd_read(npe, ECS_DBG_CTXT_REG_2, CMD_RD_ECS_REG);
  264. npe_cmd_write(npe, ECS_DBG_CTXT_REG_2, CMD_WR_ECS_REG, ctx_reg2 |
  265. ECS_DBG_REG_2_IF | ECS_DBG_REG_2_IE);
  266. /* clear the FIFOs */
  267. while (__raw_readl(&npe->regs->watchpoint_fifo) & WFIFO_VALID)
  268. ;
  269. while (__raw_readl(&npe->regs->messaging_status) & MSGSTAT_OFNE)
  270. /* read from the outFIFO until empty */
  271. print_npe(KERN_DEBUG, npe, "npe_reset: read FIFO = 0x%X\n",
  272. __raw_readl(&npe->regs->in_out_fifo));
  273. while (__raw_readl(&npe->regs->messaging_status) & MSGSTAT_IFNE)
  274. /* step execution of the NPE intruction to read inFIFO using
  275. the Debug Executing Context stack */
  276. if (npe_debug_instr(npe, INSTR_RD_FIFO, 0, 0))
  277. return -ETIMEDOUT;
  278. /* reset the mailbox reg from the XScale side */
  279. __raw_writel(RESET_MBOX_STAT, &npe->regs->mailbox_status);
  280. /* from NPE side */
  281. if (npe_debug_instr(npe, INSTR_RESET_MBOX, 0, 0))
  282. return -ETIMEDOUT;
  283. /* Reset the physical registers in the NPE register file */
  284. for (val = 0; val < NPE_PHYS_REG; val++) {
  285. if (npe_logical_reg_write16(npe, NPE_REGMAP, val >> 1, 0))
  286. return -ETIMEDOUT;
  287. /* address is either 0 or 4 */
  288. if (npe_logical_reg_write32(npe, (val & 1) * 4, 0, 0))
  289. return -ETIMEDOUT;
  290. }
  291. /* Reset the context store = each context's Context Store registers */
  292. /* Context 0 has no STARTPC. Instead, this value is used to set NextPC
  293. for Background ECS, to set where NPE starts executing code */
  294. val = npe_cmd_read(npe, ECS_BG_CTXT_REG_0, CMD_RD_ECS_REG);
  295. val &= ~ECS_REG_0_NEXTPC_MASK;
  296. val |= (0 /* NextPC */ << 16) & ECS_REG_0_NEXTPC_MASK;
  297. npe_cmd_write(npe, ECS_BG_CTXT_REG_0, CMD_WR_ECS_REG, val);
  298. for (i = 0; i < 16; i++) {
  299. if (i) { /* Context 0 has no STEVT nor STARTPC */
  300. /* STEVT = off, 0x80 */
  301. if (npe_logical_reg_write8(npe, NPE_STEVT, 0x80, i))
  302. return -ETIMEDOUT;
  303. if (npe_logical_reg_write16(npe, NPE_STARTPC, 0, i))
  304. return -ETIMEDOUT;
  305. }
  306. /* REGMAP = d0->p0, d8->p2, d16->p4 */
  307. if (npe_logical_reg_write16(npe, NPE_REGMAP, 0x820, i))
  308. return -ETIMEDOUT;
  309. if (npe_logical_reg_write8(npe, NPE_CINDEX, 0, i))
  310. return -ETIMEDOUT;
  311. }
  312. /* post exec */
  313. /* clear active bit in debug level */
  314. npe_cmd_write(npe, ECS_DBG_CTXT_REG_0, CMD_WR_ECS_REG, 0);
  315. /* clear the pipeline */
  316. __raw_writel(CMD_NPE_CLR_PIPE, &npe->regs->exec_status_cmd);
  317. /* restore previous values */
  318. __raw_writel(exec_count, &npe->regs->exec_count);
  319. npe_cmd_write(npe, ECS_DBG_CTXT_REG_2, CMD_WR_ECS_REG, ctx_reg2);
  320. /* write reset values to Execution Context Stack registers */
  321. for (val = 0; val < ARRAY_SIZE(ecs_reset); val++)
  322. npe_cmd_write(npe, ecs_reset[val].reg, CMD_WR_ECS_REG,
  323. ecs_reset[val].val);
  324. /* clear the profile counter */
  325. __raw_writel(CMD_CLR_PROFILE_CNT, &npe->regs->exec_status_cmd);
  326. __raw_writel(0, &npe->regs->exec_count);
  327. __raw_writel(0, &npe->regs->action_points[0]);
  328. __raw_writel(0, &npe->regs->action_points[1]);
  329. __raw_writel(0, &npe->regs->action_points[2]);
  330. __raw_writel(0, &npe->regs->action_points[3]);
  331. __raw_writel(0, &npe->regs->watch_count);
  332. val = ixp4xx_read_feature_bits();
  333. /* reset the NPE */
  334. ixp4xx_write_feature_bits(val &
  335. ~(IXP4XX_FEATURE_RESET_NPEA << npe->id));
  336. /* deassert reset */
  337. ixp4xx_write_feature_bits(val |
  338. (IXP4XX_FEATURE_RESET_NPEA << npe->id));
  339. for (i = 0; i < MAX_RETRIES; i++) {
  340. if (ixp4xx_read_feature_bits() &
  341. (IXP4XX_FEATURE_RESET_NPEA << npe->id))
  342. break; /* NPE is back alive */
  343. udelay(1);
  344. }
  345. if (i == MAX_RETRIES)
  346. return -ETIMEDOUT;
  347. npe_stop(npe);
  348. /* restore NPE configuration bus Control Register - parity settings */
  349. __raw_writel(ctl, &npe->regs->messaging_control);
  350. return 0;
  351. }
  352. int npe_send_message(struct npe *npe, const void *msg, const char *what)
  353. {
  354. const u32 *send = msg;
  355. int cycles = 0;
  356. debug_msg(npe, "Trying to send message %s [%08X:%08X]\n",
  357. what, send[0], send[1]);
  358. if (__raw_readl(&npe->regs->messaging_status) & MSGSTAT_IFNE) {
  359. debug_msg(npe, "NPE input FIFO not empty\n");
  360. return -EIO;
  361. }
  362. __raw_writel(send[0], &npe->regs->in_out_fifo);
  363. if (!(__raw_readl(&npe->regs->messaging_status) & MSGSTAT_IFNF)) {
  364. debug_msg(npe, "NPE input FIFO full\n");
  365. return -EIO;
  366. }
  367. __raw_writel(send[1], &npe->regs->in_out_fifo);
  368. while ((cycles < MAX_RETRIES) &&
  369. (__raw_readl(&npe->regs->messaging_status) & MSGSTAT_IFNE)) {
  370. udelay(1);
  371. cycles++;
  372. }
  373. if (cycles == MAX_RETRIES) {
  374. debug_msg(npe, "Timeout sending message\n");
  375. return -ETIMEDOUT;
  376. }
  377. #if DEBUG_MSG > 1
  378. debug_msg(npe, "Sending a message took %i cycles\n", cycles);
  379. #endif
  380. return 0;
  381. }
  382. int npe_recv_message(struct npe *npe, void *msg, const char *what)
  383. {
  384. u32 *recv = msg;
  385. int cycles = 0, cnt = 0;
  386. debug_msg(npe, "Trying to receive message %s\n", what);
  387. while (cycles < MAX_RETRIES) {
  388. if (__raw_readl(&npe->regs->messaging_status) & MSGSTAT_OFNE) {
  389. recv[cnt++] = __raw_readl(&npe->regs->in_out_fifo);
  390. if (cnt == 2)
  391. break;
  392. } else {
  393. udelay(1);
  394. cycles++;
  395. }
  396. }
  397. switch(cnt) {
  398. case 1:
  399. debug_msg(npe, "Received [%08X]\n", recv[0]);
  400. break;
  401. case 2:
  402. debug_msg(npe, "Received [%08X:%08X]\n", recv[0], recv[1]);
  403. break;
  404. }
  405. if (cycles == MAX_RETRIES) {
  406. debug_msg(npe, "Timeout waiting for message\n");
  407. return -ETIMEDOUT;
  408. }
  409. #if DEBUG_MSG > 1
  410. debug_msg(npe, "Receiving a message took %i cycles\n", cycles);
  411. #endif
  412. return 0;
  413. }
  414. int npe_send_recv_message(struct npe *npe, void *msg, const char *what)
  415. {
  416. int result;
  417. u32 *send = msg, recv[2];
  418. if ((result = npe_send_message(npe, msg, what)) != 0)
  419. return result;
  420. if ((result = npe_recv_message(npe, recv, what)) != 0)
  421. return result;
  422. if ((recv[0] != send[0]) || (recv[1] != send[1])) {
  423. debug_msg(npe, "Message %s: unexpected message received\n",
  424. what);
  425. return -EIO;
  426. }
  427. return 0;
  428. }
  429. int npe_load_firmware(struct npe *npe, const char *name, struct device *dev)
  430. {
  431. const struct firmware *fw_entry;
  432. struct dl_block {
  433. u32 type;
  434. u32 offset;
  435. } *blk;
  436. struct dl_image {
  437. u32 magic;
  438. u32 id;
  439. u32 size;
  440. union {
  441. u32 data[0];
  442. struct dl_block blocks[0];
  443. };
  444. } *image;
  445. struct dl_codeblock {
  446. u32 npe_addr;
  447. u32 size;
  448. u32 data[0];
  449. } *cb;
  450. int i, j, err, data_size, instr_size, blocks, table_end;
  451. u32 cmd;
  452. if ((err = reject_firmware(&fw_entry, name, dev)) != 0)
  453. return err;
  454. err = -EINVAL;
  455. if (fw_entry->size < sizeof(struct dl_image)) {
  456. print_npe(KERN_ERR, npe, "incomplete firmware file\n");
  457. goto err;
  458. }
  459. image = (struct dl_image*)fw_entry->data;
  460. #if DEBUG_FW
  461. print_npe(KERN_DEBUG, npe, "firmware: %08X %08X %08X (0x%X bytes)\n",
  462. image->magic, image->id, image->size, image->size * 4);
  463. #endif
  464. if (image->magic == swab32(FW_MAGIC)) { /* swapped file */
  465. image->id = swab32(image->id);
  466. image->size = swab32(image->size);
  467. } else if (image->magic != FW_MAGIC) {
  468. print_npe(KERN_ERR, npe, "bad firmware file magic: 0x%X\n",
  469. image->magic);
  470. goto err;
  471. }
  472. if ((image->size * 4 + sizeof(struct dl_image)) != fw_entry->size) {
  473. print_npe(KERN_ERR, npe,
  474. "inconsistent size of firmware file\n");
  475. goto err;
  476. }
  477. if (((image->id >> 24) & 0xF /* NPE ID */) != npe->id) {
  478. print_npe(KERN_ERR, npe, "firmware file NPE ID mismatch\n");
  479. goto err;
  480. }
  481. if (image->magic == swab32(FW_MAGIC))
  482. for (i = 0; i < image->size; i++)
  483. image->data[i] = swab32(image->data[i]);
  484. if (cpu_is_ixp42x() && ((image->id >> 28) & 0xF /* device ID */)) {
  485. print_npe(KERN_INFO, npe, "IXP43x/IXP46x firmware ignored on "
  486. "IXP42x\n");
  487. goto err;
  488. }
  489. if (npe_running(npe)) {
  490. print_npe(KERN_INFO, npe, "unable to load firmware, NPE is "
  491. "already running\n");
  492. err = -EBUSY;
  493. goto err;
  494. }
  495. #if 0
  496. npe_stop(npe);
  497. npe_reset(npe);
  498. #endif
  499. print_npe(KERN_INFO, npe, "firmware functionality 0x%X, "
  500. "revision 0x%X:%X\n", (image->id >> 16) & 0xFF,
  501. (image->id >> 8) & 0xFF, image->id & 0xFF);
  502. if (cpu_is_ixp42x()) {
  503. if (!npe->id)
  504. instr_size = NPE_A_42X_INSTR_SIZE;
  505. else
  506. instr_size = NPE_B_AND_C_42X_INSTR_SIZE;
  507. data_size = NPE_42X_DATA_SIZE;
  508. } else {
  509. instr_size = NPE_46X_INSTR_SIZE;
  510. data_size = NPE_46X_DATA_SIZE;
  511. }
  512. for (blocks = 0; blocks * sizeof(struct dl_block) / 4 < image->size;
  513. blocks++)
  514. if (image->blocks[blocks].type == FW_BLOCK_TYPE_EOF)
  515. break;
  516. if (blocks * sizeof(struct dl_block) / 4 >= image->size) {
  517. print_npe(KERN_INFO, npe, "firmware EOF block marker not "
  518. "found\n");
  519. goto err;
  520. }
  521. #if DEBUG_FW
  522. print_npe(KERN_DEBUG, npe, "%i firmware blocks found\n", blocks);
  523. #endif
  524. table_end = blocks * sizeof(struct dl_block) / 4 + 1 /* EOF marker */;
  525. for (i = 0, blk = image->blocks; i < blocks; i++, blk++) {
  526. if (blk->offset > image->size - sizeof(struct dl_codeblock) / 4
  527. || blk->offset < table_end) {
  528. print_npe(KERN_INFO, npe, "invalid offset 0x%X of "
  529. "firmware block #%i\n", blk->offset, i);
  530. goto err;
  531. }
  532. cb = (struct dl_codeblock*)&image->data[blk->offset];
  533. if (blk->type == FW_BLOCK_TYPE_INSTR) {
  534. if (cb->npe_addr + cb->size > instr_size)
  535. goto too_big;
  536. cmd = CMD_WR_INS_MEM;
  537. } else if (blk->type == FW_BLOCK_TYPE_DATA) {
  538. if (cb->npe_addr + cb->size > data_size)
  539. goto too_big;
  540. cmd = CMD_WR_DATA_MEM;
  541. } else {
  542. print_npe(KERN_INFO, npe, "invalid firmware block #%i "
  543. "type 0x%X\n", i, blk->type);
  544. goto err;
  545. }
  546. if (blk->offset + sizeof(*cb) / 4 + cb->size > image->size) {
  547. print_npe(KERN_INFO, npe, "firmware block #%i doesn't "
  548. "fit in firmware image: type %c, start 0x%X,"
  549. " length 0x%X\n", i,
  550. blk->type == FW_BLOCK_TYPE_INSTR ? 'I' : 'D',
  551. cb->npe_addr, cb->size);
  552. goto err;
  553. }
  554. for (j = 0; j < cb->size; j++)
  555. npe_cmd_write(npe, cb->npe_addr + j, cmd, cb->data[j]);
  556. }
  557. npe_start(npe);
  558. if (!npe_running(npe))
  559. print_npe(KERN_ERR, npe, "unable to start\n");
  560. release_firmware(fw_entry);
  561. return 0;
  562. too_big:
  563. print_npe(KERN_INFO, npe, "firmware block #%i doesn't fit in NPE "
  564. "memory: type %c, start 0x%X, length 0x%X\n", i,
  565. blk->type == FW_BLOCK_TYPE_INSTR ? 'I' : 'D',
  566. cb->npe_addr, cb->size);
  567. err:
  568. release_firmware(fw_entry);
  569. return err;
  570. }
  571. struct npe *npe_request(unsigned id)
  572. {
  573. if (id < NPE_COUNT)
  574. if (npe_tab[id].valid)
  575. if (try_module_get(THIS_MODULE))
  576. return &npe_tab[id];
  577. return NULL;
  578. }
  579. void npe_release(struct npe *npe)
  580. {
  581. module_put(THIS_MODULE);
  582. }
  583. static int __init npe_init_module(void)
  584. {
  585. int i, found = 0;
  586. for (i = 0; i < NPE_COUNT; i++) {
  587. struct npe *npe = &npe_tab[i];
  588. if (!(ixp4xx_read_feature_bits() &
  589. (IXP4XX_FEATURE_RESET_NPEA << i)))
  590. continue; /* NPE already disabled or not present */
  591. if (!(npe->mem_res = request_mem_region(npe->regs_phys,
  592. REGS_SIZE,
  593. npe_name(npe)))) {
  594. print_npe(KERN_ERR, npe,
  595. "failed to request memory region\n");
  596. continue;
  597. }
  598. if (npe_reset(npe))
  599. continue;
  600. npe->valid = 1;
  601. found++;
  602. }
  603. if (!found)
  604. return -ENODEV;
  605. return 0;
  606. }
  607. static void __exit npe_cleanup_module(void)
  608. {
  609. int i;
  610. for (i = 0; i < NPE_COUNT; i++)
  611. if (npe_tab[i].mem_res) {
  612. npe_reset(&npe_tab[i]);
  613. release_resource(npe_tab[i].mem_res);
  614. }
  615. }
  616. module_init(npe_init_module);
  617. module_exit(npe_cleanup_module);
  618. MODULE_AUTHOR("Krzysztof Halasa");
  619. MODULE_LICENSE("GPL v2");
  620. /*(DEBLOBBED)*/
  621. EXPORT_SYMBOL(npe_names);
  622. EXPORT_SYMBOL(npe_running);
  623. EXPORT_SYMBOL(npe_request);
  624. EXPORT_SYMBOL(npe_release);
  625. EXPORT_SYMBOL(npe_load_firmware);
  626. EXPORT_SYMBOL(npe_send_message);
  627. EXPORT_SYMBOL(npe_recv_message);
  628. EXPORT_SYMBOL(npe_send_recv_message);