bestcomm.c 13 KB

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