ixp4xx_npe.c 21 KB

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