bestcomm.c 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528
  1. /*
  2. * Driver for MPC52xx processor BestComm peripheral controller
  3. *
  4. *
  5. * Copyright (C) 2006-2007 Sylvain Munaut <tnt@246tNt.com>
  6. * Copyright (C) 2005 Varma Electronics Oy,
  7. * ( by Andrey Volkov <avolkov@varma-el.com> )
  8. * Copyright (C) 2003-2004 MontaVista, Software, Inc.
  9. * ( by Dale Farnsworth <dfarnsworth@mvista.com> )
  10. *
  11. * This file is licensed under the terms of the GNU General Public License
  12. * version 2. This program is licensed "as is" without any warranty of any
  13. * kind, whether express or implied.
  14. */
  15. #include <linux/module.h>
  16. #include <linux/kernel.h>
  17. #include <linux/slab.h>
  18. #include <linux/of.h>
  19. #include <linux/of_device.h>
  20. #include <linux/of_platform.h>
  21. #include <asm/io.h>
  22. #include <asm/irq.h>
  23. #include <asm/mpc52xx.h>
  24. #include "sram.h"
  25. #include "bestcomm_priv.h"
  26. #include "bestcomm.h"
  27. #define DRIVER_NAME "bestcomm-core"
  28. struct bcom_engine *bcom_eng = NULL;
  29. EXPORT_SYMBOL_GPL(bcom_eng); /* needed for inline functions */
  30. /* ======================================================================== */
  31. /* Public and private API */
  32. /* ======================================================================== */
  33. /* Private API */
  34. struct bcom_task *
  35. bcom_task_alloc(int bd_count, int bd_size, int priv_size)
  36. {
  37. int i, tasknum = -1;
  38. struct bcom_task *tsk;
  39. /* Get and reserve a task num */
  40. spin_lock(&bcom_eng->lock);
  41. for (i=0; i<BCOM_MAX_TASKS; i++)
  42. if (!bcom_eng->tdt[i].stop) { /* we use stop as a marker */
  43. bcom_eng->tdt[i].stop = 0xfffffffful; /* dummy addr */
  44. tasknum = i;
  45. break;
  46. }
  47. spin_unlock(&bcom_eng->lock);
  48. if (tasknum < 0)
  49. return NULL;
  50. /* Allocate our structure */
  51. tsk = kzalloc(sizeof(struct bcom_task) + priv_size, GFP_KERNEL);
  52. if (!tsk)
  53. goto error;
  54. tsk->tasknum = tasknum;
  55. if (priv_size)
  56. tsk->priv = (void*)tsk + sizeof(struct bcom_task);
  57. /* Get IRQ of that task */
  58. tsk->irq = irq_of_parse_and_map(bcom_eng->ofnode, tsk->tasknum);
  59. if (tsk->irq == NO_IRQ)
  60. goto error;
  61. /* Init the BDs, if needed */
  62. if (bd_count) {
  63. tsk->cookie = kmalloc(sizeof(void*) * bd_count, GFP_KERNEL);
  64. if (!tsk->cookie)
  65. goto error;
  66. tsk->bd = bcom_sram_alloc(bd_count * bd_size, 4, &tsk->bd_pa);
  67. if (!tsk->bd)
  68. goto error;
  69. memset(tsk->bd, 0x00, bd_count * bd_size);
  70. tsk->num_bd = bd_count;
  71. tsk->bd_size = bd_size;
  72. }
  73. return tsk;
  74. error:
  75. if (tsk) {
  76. if (tsk->irq != NO_IRQ)
  77. irq_dispose_mapping(tsk->irq);
  78. bcom_sram_free(tsk->bd);
  79. kfree(tsk->cookie);
  80. kfree(tsk);
  81. }
  82. bcom_eng->tdt[tasknum].stop = 0;
  83. return NULL;
  84. }
  85. EXPORT_SYMBOL_GPL(bcom_task_alloc);
  86. void
  87. bcom_task_free(struct bcom_task *tsk)
  88. {
  89. /* Stop the task */
  90. bcom_disable_task(tsk->tasknum);
  91. /* Clear TDT */
  92. bcom_eng->tdt[tsk->tasknum].start = 0;
  93. bcom_eng->tdt[tsk->tasknum].stop = 0;
  94. /* Free everything */
  95. irq_dispose_mapping(tsk->irq);
  96. bcom_sram_free(tsk->bd);
  97. kfree(tsk->cookie);
  98. kfree(tsk);
  99. }
  100. EXPORT_SYMBOL_GPL(bcom_task_free);
  101. int
  102. bcom_load_image(int task, u32 *task_image)
  103. {
  104. struct bcom_task_header *hdr = (struct bcom_task_header *)task_image;
  105. struct bcom_tdt *tdt;
  106. u32 *desc, *var, *inc;
  107. u32 *desc_src, *var_src, *inc_src;
  108. /* Safety checks */
  109. if (hdr->magic != BCOM_TASK_MAGIC) {
  110. printk(KERN_ERR DRIVER_NAME
  111. ": Trying to load invalid microcode\n");
  112. return -EINVAL;
  113. }
  114. if ((task < 0) || (task >= BCOM_MAX_TASKS)) {
  115. printk(KERN_ERR DRIVER_NAME
  116. ": Trying to load invalid task %d\n", task);
  117. return -EINVAL;
  118. }
  119. /* Initial load or reload */
  120. tdt = &bcom_eng->tdt[task];
  121. if (tdt->start) {
  122. desc = bcom_task_desc(task);
  123. if (hdr->desc_size != bcom_task_num_descs(task)) {
  124. printk(KERN_ERR DRIVER_NAME
  125. ": Trying to reload wrong task image "
  126. "(%d size %d/%d)!\n",
  127. task,
  128. hdr->desc_size,
  129. bcom_task_num_descs(task));
  130. return -EINVAL;
  131. }
  132. } else {
  133. phys_addr_t start_pa;
  134. desc = bcom_sram_alloc(hdr->desc_size * sizeof(u32), 4, &start_pa);
  135. if (!desc)
  136. return -ENOMEM;
  137. tdt->start = start_pa;
  138. tdt->stop = start_pa + ((hdr->desc_size-1) * sizeof(u32));
  139. }
  140. var = bcom_task_var(task);
  141. inc = bcom_task_inc(task);
  142. /* Clear & copy */
  143. memset(var, 0x00, BCOM_VAR_SIZE);
  144. memset(inc, 0x00, BCOM_INC_SIZE);
  145. desc_src = (u32 *)(hdr + 1);
  146. var_src = desc_src + hdr->desc_size;
  147. inc_src = var_src + hdr->var_size;
  148. memcpy(desc, desc_src, hdr->desc_size * sizeof(u32));
  149. memcpy(var + hdr->first_var, var_src, hdr->var_size * sizeof(u32));
  150. memcpy(inc, inc_src, hdr->inc_size * sizeof(u32));
  151. return 0;
  152. }
  153. EXPORT_SYMBOL_GPL(bcom_load_image);
  154. void
  155. bcom_set_initiator(int task, int initiator)
  156. {
  157. int i;
  158. int num_descs;
  159. u32 *desc;
  160. int next_drd_has_initiator;
  161. bcom_set_tcr_initiator(task, initiator);
  162. /* Just setting tcr is apparently not enough due to some problem */
  163. /* with it. So we just go thru all the microcode and replace in */
  164. /* the DRD directly */
  165. desc = bcom_task_desc(task);
  166. next_drd_has_initiator = 1;
  167. num_descs = bcom_task_num_descs(task);
  168. for (i=0; i<num_descs; i++, desc++) {
  169. if (!bcom_desc_is_drd(*desc))
  170. continue;
  171. if (next_drd_has_initiator)
  172. if (bcom_desc_initiator(*desc) != BCOM_INITIATOR_ALWAYS)
  173. bcom_set_desc_initiator(desc, initiator);
  174. next_drd_has_initiator = !bcom_drd_is_extended(*desc);
  175. }
  176. }
  177. EXPORT_SYMBOL_GPL(bcom_set_initiator);
  178. /* Public API */
  179. void
  180. bcom_enable(struct bcom_task *tsk)
  181. {
  182. bcom_enable_task(tsk->tasknum);
  183. }
  184. EXPORT_SYMBOL_GPL(bcom_enable);
  185. void
  186. bcom_disable(struct bcom_task *tsk)
  187. {
  188. bcom_disable_task(tsk->tasknum);
  189. }
  190. EXPORT_SYMBOL_GPL(bcom_disable);
  191. /* ======================================================================== */
  192. /* Engine init/cleanup */
  193. /* ======================================================================== */
  194. /* Function Descriptor table */
  195. /* this will need to be updated if Freescale changes their task code FDT */
  196. static u32 fdt_ops[] = {
  197. 0xa0045670, /* FDT[48] - load_acc() */
  198. 0x80045670, /* FDT[49] - unload_acc() */
  199. 0x21800000, /* FDT[50] - and() */
  200. 0x21e00000, /* FDT[51] - or() */
  201. 0x21500000, /* FDT[52] - xor() */
  202. 0x21400000, /* FDT[53] - andn() */
  203. 0x21500000, /* FDT[54] - not() */
  204. 0x20400000, /* FDT[55] - add() */
  205. 0x20500000, /* FDT[56] - sub() */
  206. 0x20800000, /* FDT[57] - lsh() */
  207. 0x20a00000, /* FDT[58] - rsh() */
  208. 0xc0170000, /* FDT[59] - crc8() */
  209. 0xc0145670, /* FDT[60] - crc16() */
  210. 0xc0345670, /* FDT[61] - crc32() */
  211. 0xa0076540, /* FDT[62] - endian32() */
  212. 0xa0000760, /* FDT[63] - endian16() */
  213. };
  214. static int __devinit
  215. bcom_engine_init(void)
  216. {
  217. int task;
  218. phys_addr_t tdt_pa, ctx_pa, var_pa, fdt_pa;
  219. unsigned int tdt_size, ctx_size, var_size, fdt_size;
  220. /* Allocate & clear SRAM zones for FDT, TDTs, contexts and vars/incs */
  221. tdt_size = BCOM_MAX_TASKS * sizeof(struct bcom_tdt);
  222. ctx_size = BCOM_MAX_TASKS * BCOM_CTX_SIZE;
  223. var_size = BCOM_MAX_TASKS * (BCOM_VAR_SIZE + BCOM_INC_SIZE);
  224. fdt_size = BCOM_FDT_SIZE;
  225. bcom_eng->tdt = bcom_sram_alloc(tdt_size, sizeof(u32), &tdt_pa);
  226. bcom_eng->ctx = bcom_sram_alloc(ctx_size, BCOM_CTX_ALIGN, &ctx_pa);
  227. bcom_eng->var = bcom_sram_alloc(var_size, BCOM_VAR_ALIGN, &var_pa);
  228. bcom_eng->fdt = bcom_sram_alloc(fdt_size, BCOM_FDT_ALIGN, &fdt_pa);
  229. if (!bcom_eng->tdt || !bcom_eng->ctx || !bcom_eng->var || !bcom_eng->fdt) {
  230. printk(KERN_ERR "DMA: SRAM alloc failed in engine init !\n");
  231. bcom_sram_free(bcom_eng->tdt);
  232. bcom_sram_free(bcom_eng->ctx);
  233. bcom_sram_free(bcom_eng->var);
  234. bcom_sram_free(bcom_eng->fdt);
  235. return -ENOMEM;
  236. }
  237. memset(bcom_eng->tdt, 0x00, tdt_size);
  238. memset(bcom_eng->ctx, 0x00, ctx_size);
  239. memset(bcom_eng->var, 0x00, var_size);
  240. memset(bcom_eng->fdt, 0x00, fdt_size);
  241. /* Copy the FDT for the EU#3 */
  242. memcpy(&bcom_eng->fdt[48], fdt_ops, sizeof(fdt_ops));
  243. /* Initialize Task base structure */
  244. for (task=0; task<BCOM_MAX_TASKS; task++)
  245. {
  246. out_be16(&bcom_eng->regs->tcr[task], 0);
  247. out_8(&bcom_eng->regs->ipr[task], 0);
  248. bcom_eng->tdt[task].context = ctx_pa;
  249. bcom_eng->tdt[task].var = var_pa;
  250. bcom_eng->tdt[task].fdt = fdt_pa;
  251. var_pa += BCOM_VAR_SIZE + BCOM_INC_SIZE;
  252. ctx_pa += BCOM_CTX_SIZE;
  253. }
  254. out_be32(&bcom_eng->regs->taskBar, tdt_pa);
  255. /* Init 'always' initiator */
  256. out_8(&bcom_eng->regs->ipr[BCOM_INITIATOR_ALWAYS], BCOM_IPR_ALWAYS);
  257. /* Disable COMM Bus Prefetch, apparently it's not reliable yet */
  258. /* FIXME: This should be done on 5200 and not 5200B ... */
  259. out_be16(&bcom_eng->regs->PtdCntrl, in_be16(&bcom_eng->regs->PtdCntrl) | 1);
  260. /* Init lock */
  261. spin_lock_init(&bcom_eng->lock);
  262. return 0;
  263. }
  264. static void
  265. bcom_engine_cleanup(void)
  266. {
  267. int task;
  268. /* Stop all tasks */
  269. for (task=0; task<BCOM_MAX_TASKS; task++)
  270. {
  271. out_be16(&bcom_eng->regs->tcr[task], 0);
  272. out_8(&bcom_eng->regs->ipr[task], 0);
  273. }
  274. out_be32(&bcom_eng->regs->taskBar, 0ul);
  275. /* Release the SRAM zones */
  276. bcom_sram_free(bcom_eng->tdt);
  277. bcom_sram_free(bcom_eng->ctx);
  278. bcom_sram_free(bcom_eng->var);
  279. bcom_sram_free(bcom_eng->fdt);
  280. }
  281. /* ======================================================================== */
  282. /* OF platform driver */
  283. /* ======================================================================== */
  284. static int __devinit
  285. mpc52xx_bcom_probe(struct of_device *op, const struct of_device_id *match)
  286. {
  287. struct device_node *ofn_sram;
  288. struct resource res_bcom;
  289. int rv;
  290. /* Inform user we're ok so far */
  291. printk(KERN_INFO "DMA: MPC52xx BestComm driver\n");
  292. /* Get the bestcomm node */
  293. of_node_get(op->node);
  294. /* Prepare SRAM */
  295. ofn_sram = of_find_compatible_node(NULL, "sram", "mpc5200-sram");
  296. if (!ofn_sram) {
  297. printk(KERN_ERR DRIVER_NAME ": "
  298. "No SRAM found in device tree\n");
  299. rv = -ENODEV;
  300. goto error_ofput;
  301. }
  302. rv = bcom_sram_init(ofn_sram, DRIVER_NAME);
  303. of_node_put(ofn_sram);
  304. if (rv) {
  305. printk(KERN_ERR DRIVER_NAME ": "
  306. "Error in SRAM init\n");
  307. goto error_ofput;
  308. }
  309. /* Get a clean struct */
  310. bcom_eng = kzalloc(sizeof(struct bcom_engine), GFP_KERNEL);
  311. if (!bcom_eng) {
  312. printk(KERN_ERR DRIVER_NAME ": "
  313. "Can't allocate state structure\n");
  314. rv = -ENOMEM;
  315. goto error_sramclean;
  316. }
  317. /* Save the node */
  318. bcom_eng->ofnode = op->node;
  319. /* Get, reserve & map io */
  320. if (of_address_to_resource(op->node, 0, &res_bcom)) {
  321. printk(KERN_ERR DRIVER_NAME ": "
  322. "Can't get resource\n");
  323. rv = -EINVAL;
  324. goto error_sramclean;
  325. }
  326. if (!request_mem_region(res_bcom.start, sizeof(struct mpc52xx_sdma),
  327. DRIVER_NAME)) {
  328. printk(KERN_ERR DRIVER_NAME ": "
  329. "Can't request registers region\n");
  330. rv = -EBUSY;
  331. goto error_sramclean;
  332. }
  333. bcom_eng->regs_base = res_bcom.start;
  334. bcom_eng->regs = ioremap(res_bcom.start, sizeof(struct mpc52xx_sdma));
  335. if (!bcom_eng->regs) {
  336. printk(KERN_ERR DRIVER_NAME ": "
  337. "Can't map registers\n");
  338. rv = -ENOMEM;
  339. goto error_release;
  340. }
  341. /* Now, do the real init */
  342. rv = bcom_engine_init();
  343. if (rv)
  344. goto error_unmap;
  345. /* Done ! */
  346. printk(KERN_INFO "DMA: MPC52xx BestComm engine @%08lx ok !\n",
  347. bcom_eng->regs_base);
  348. return 0;
  349. /* Error path */
  350. error_unmap:
  351. iounmap(bcom_eng->regs);
  352. error_release:
  353. release_mem_region(res_bcom.start, sizeof(struct mpc52xx_sdma));
  354. error_sramclean:
  355. kfree(bcom_eng);
  356. bcom_sram_cleanup();
  357. error_ofput:
  358. of_node_put(op->node);
  359. printk(KERN_ERR "DMA: MPC52xx BestComm init failed !\n");
  360. return rv;
  361. }
  362. static int
  363. mpc52xx_bcom_remove(struct of_device *op)
  364. {
  365. /* Clean up the engine */
  366. bcom_engine_cleanup();
  367. /* Cleanup SRAM */
  368. bcom_sram_cleanup();
  369. /* Release regs */
  370. iounmap(bcom_eng->regs);
  371. release_mem_region(bcom_eng->regs_base, sizeof(struct mpc52xx_sdma));
  372. /* Release the node */
  373. of_node_put(bcom_eng->ofnode);
  374. /* Release memory */
  375. kfree(bcom_eng);
  376. bcom_eng = NULL;
  377. return 0;
  378. }
  379. static struct of_device_id mpc52xx_bcom_of_match[] = {
  380. {
  381. .type = "dma-controller",
  382. .compatible = "mpc5200-bestcomm",
  383. },
  384. {},
  385. };
  386. MODULE_DEVICE_TABLE(of, mpc52xx_bcom_of_match);
  387. static struct of_platform_driver mpc52xx_bcom_of_platform_driver = {
  388. .owner = THIS_MODULE,
  389. .name = DRIVER_NAME,
  390. .match_table = mpc52xx_bcom_of_match,
  391. .probe = mpc52xx_bcom_probe,
  392. .remove = mpc52xx_bcom_remove,
  393. .driver = {
  394. .name = DRIVER_NAME,
  395. .owner = THIS_MODULE,
  396. },
  397. };
  398. /* ======================================================================== */
  399. /* Module */
  400. /* ======================================================================== */
  401. static int __init
  402. mpc52xx_bcom_init(void)
  403. {
  404. return of_register_platform_driver(&mpc52xx_bcom_of_platform_driver);
  405. }
  406. static void __exit
  407. mpc52xx_bcom_exit(void)
  408. {
  409. of_unregister_platform_driver(&mpc52xx_bcom_of_platform_driver);
  410. }
  411. /* If we're not a module, we must make sure everything is setup before */
  412. /* anyone tries to use us ... that's why we use subsys_initcall instead */
  413. /* of module_init. */
  414. subsys_initcall(mpc52xx_bcom_init);
  415. module_exit(mpc52xx_bcom_exit);
  416. MODULE_DESCRIPTION("Freescale MPC52xx BestComm DMA");
  417. MODULE_AUTHOR("Sylvain Munaut <tnt@246tNt.com>");
  418. MODULE_AUTHOR("Andrey Volkov <avolkov@varma-el.com>");
  419. MODULE_AUTHOR("Dale Farnsworth <dfarnsworth@mvista.com>");
  420. MODULE_LICENSE("GPL v2");