commproc.c 9.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398
  1. /*
  2. * General Purpose functions for the global management of the
  3. * Communication Processor Module.
  4. * Copyright (c) 1997 Dan error_act (dmalek@jlc.net)
  5. *
  6. * In addition to the individual control of the communication
  7. * channels, there are a few functions that globally affect the
  8. * communication processor.
  9. *
  10. * Buffer descriptors must be allocated from the dual ported memory
  11. * space. The allocator for that is here. When the communication
  12. * process is reset, we reclaim the memory available. There is
  13. * currently no deallocator for this memory.
  14. * The amount of space available is platform dependent. On the
  15. * MBX, the EPPC software loads additional microcode into the
  16. * communication processor, and uses some of the DP ram for this
  17. * purpose. Current, the first 512 bytes and the last 256 bytes of
  18. * memory are used. Right now I am conservative and only use the
  19. * memory that can never be used for microcode. If there are
  20. * applications that require more DP ram, we can expand the boundaries
  21. * but then we have to be careful of any downloaded microcode.
  22. */
  23. #include <linux/errno.h>
  24. #include <linux/sched.h>
  25. #include <linux/kernel.h>
  26. #include <linux/dma-mapping.h>
  27. #include <linux/param.h>
  28. #include <linux/string.h>
  29. #include <linux/mm.h>
  30. #include <linux/interrupt.h>
  31. #include <linux/irq.h>
  32. #include <linux/module.h>
  33. #include <asm/mpc8xx.h>
  34. #include <asm/page.h>
  35. #include <asm/pgtable.h>
  36. #include <asm/8xx_immap.h>
  37. #include <asm/commproc.h>
  38. #include <asm/io.h>
  39. #include <asm/tlbflush.h>
  40. #include <asm/rheap.h>
  41. #include <asm/prom.h>
  42. #include <asm/fs_pd.h>
  43. #define CPM_MAP_SIZE (0x4000)
  44. static void m8xx_cpm_dpinit(void);
  45. static uint host_buffer; /* One page of host buffer */
  46. static uint host_end; /* end + 1 */
  47. cpm8xx_t *cpmp; /* Pointer to comm processor space */
  48. cpic8xx_t *cpic_reg;
  49. static struct device_node *cpm_pic_node;
  50. static struct irq_host *cpm_pic_host;
  51. static void cpm_mask_irq(unsigned int irq)
  52. {
  53. unsigned int cpm_vec = (unsigned int)irq_map[irq].hwirq;
  54. clrbits32(&cpic_reg->cpic_cimr, (1 << cpm_vec));
  55. }
  56. static void cpm_unmask_irq(unsigned int irq)
  57. {
  58. unsigned int cpm_vec = (unsigned int)irq_map[irq].hwirq;
  59. setbits32(&cpic_reg->cpic_cimr, (1 << cpm_vec));
  60. }
  61. static void cpm_end_irq(unsigned int irq)
  62. {
  63. unsigned int cpm_vec = (unsigned int)irq_map[irq].hwirq;
  64. out_be32(&cpic_reg->cpic_cisr, (1 << cpm_vec));
  65. }
  66. static struct irq_chip cpm_pic = {
  67. .typename = " CPM PIC ",
  68. .mask = cpm_mask_irq,
  69. .unmask = cpm_unmask_irq,
  70. .eoi = cpm_end_irq,
  71. };
  72. int cpm_get_irq(void)
  73. {
  74. int cpm_vec;
  75. /* Get the vector by setting the ACK bit and then reading
  76. * the register.
  77. */
  78. out_be16(&cpic_reg->cpic_civr, 1);
  79. cpm_vec = in_be16(&cpic_reg->cpic_civr);
  80. cpm_vec >>= 11;
  81. return irq_linear_revmap(cpm_pic_host, cpm_vec);
  82. }
  83. static int cpm_pic_host_match(struct irq_host *h, struct device_node *node)
  84. {
  85. return cpm_pic_node == node;
  86. }
  87. static int cpm_pic_host_map(struct irq_host *h, unsigned int virq,
  88. irq_hw_number_t hw)
  89. {
  90. pr_debug("cpm_pic_host_map(%d, 0x%lx)\n", virq, hw);
  91. get_irq_desc(virq)->status |= IRQ_LEVEL;
  92. set_irq_chip_and_handler(virq, &cpm_pic, handle_fasteoi_irq);
  93. return 0;
  94. }
  95. /* The CPM can generate the error interrupt when there is a race condition
  96. * between generating and masking interrupts. All we have to do is ACK it
  97. * and return. This is a no-op function so we don't need any special
  98. * tests in the interrupt handler.
  99. */
  100. static irqreturn_t cpm_error_interrupt(int irq, void *dev)
  101. {
  102. return IRQ_HANDLED;
  103. }
  104. static struct irqaction cpm_error_irqaction = {
  105. .handler = cpm_error_interrupt,
  106. .mask = CPU_MASK_NONE,
  107. .name = "error",
  108. };
  109. static struct irq_host_ops cpm_pic_host_ops = {
  110. .match = cpm_pic_host_match,
  111. .map = cpm_pic_host_map,
  112. };
  113. unsigned int cpm_pic_init(void)
  114. {
  115. struct device_node *np = NULL;
  116. struct resource res;
  117. unsigned int sirq = NO_IRQ, hwirq, eirq;
  118. int ret;
  119. pr_debug("cpm_pic_init\n");
  120. np = of_find_compatible_node(NULL, "cpm-pic", "CPM");
  121. if (np == NULL) {
  122. printk(KERN_ERR "CPM PIC init: can not find cpm-pic node\n");
  123. return sirq;
  124. }
  125. ret = of_address_to_resource(np, 0, &res);
  126. if (ret)
  127. goto end;
  128. cpic_reg = (void *)ioremap(res.start, res.end - res.start + 1);
  129. if (cpic_reg == NULL)
  130. goto end;
  131. sirq = irq_of_parse_and_map(np, 0);
  132. if (sirq == NO_IRQ)
  133. goto end;
  134. /* Initialize the CPM interrupt controller. */
  135. hwirq = (unsigned int)irq_map[sirq].hwirq;
  136. out_be32(&cpic_reg->cpic_cicr,
  137. (CICR_SCD_SCC4 | CICR_SCC_SCC3 | CICR_SCB_SCC2 | CICR_SCA_SCC1) |
  138. ((hwirq/2) << 13) | CICR_HP_MASK);
  139. out_be32(&cpic_reg->cpic_cimr, 0);
  140. cpm_pic_node = of_node_get(np);
  141. cpm_pic_host = irq_alloc_host(IRQ_HOST_MAP_LINEAR, 64, &cpm_pic_host_ops, 64);
  142. if (cpm_pic_host == NULL) {
  143. printk(KERN_ERR "CPM2 PIC: failed to allocate irq host!\n");
  144. sirq = NO_IRQ;
  145. goto end;
  146. }
  147. of_node_put(np);
  148. /* Install our own error handler. */
  149. np = of_find_node_by_type(NULL, "cpm");
  150. if (np == NULL) {
  151. printk(KERN_ERR "CPM PIC init: can not find cpm node\n");
  152. goto end;
  153. }
  154. eirq= irq_of_parse_and_map(np, 0);
  155. if (eirq == NO_IRQ)
  156. goto end;
  157. if (setup_irq(eirq, &cpm_error_irqaction))
  158. printk(KERN_ERR "Could not allocate CPM error IRQ!");
  159. setbits32(&cpic_reg->cpic_cicr, CICR_IEN);
  160. end:
  161. of_node_put(np);
  162. return sirq;
  163. }
  164. void cpm_reset(void)
  165. {
  166. cpm8xx_t *commproc;
  167. sysconf8xx_t *siu_conf;
  168. commproc = (cpm8xx_t *)ioremap(CPM_MAP_ADDR, CPM_MAP_SIZE);
  169. #ifdef CONFIG_UCODE_PATCH
  170. /* Perform a reset.
  171. */
  172. out_be16(&commproc->cp_cpcr, CPM_CR_RST | CPM_CR_FLG);
  173. /* Wait for it.
  174. */
  175. while (in_be16(&commproc->cp_cpcr) & CPM_CR_FLG);
  176. cpm_load_patch(commproc);
  177. #endif
  178. /* Set SDMA Bus Request priority 5.
  179. * On 860T, this also enables FEC priority 6. I am not sure
  180. * this is what we realy want for some applications, but the
  181. * manual recommends it.
  182. * Bit 25, FAM can also be set to use FEC aggressive mode (860T).
  183. */
  184. siu_conf = (sysconf8xx_t*)immr_map(im_siu_conf);
  185. out_be32(&siu_conf->sc_sdcr, 1);
  186. immr_unmap(siu_conf);
  187. /* Reclaim the DP memory for our use. */
  188. m8xx_cpm_dpinit();
  189. /* Tell everyone where the comm processor resides.
  190. */
  191. cpmp = commproc;
  192. }
  193. /* We used to do this earlier, but have to postpone as long as possible
  194. * to ensure the kernel VM is now running.
  195. */
  196. static void
  197. alloc_host_memory(void)
  198. {
  199. dma_addr_t physaddr;
  200. /* Set the host page for allocation.
  201. */
  202. host_buffer = (uint)dma_alloc_coherent(NULL, PAGE_SIZE, &physaddr,
  203. GFP_KERNEL);
  204. host_end = host_buffer + PAGE_SIZE;
  205. }
  206. /* We also own one page of host buffer space for the allocation of
  207. * UART "fifos" and the like.
  208. */
  209. uint
  210. m8xx_cpm_hostalloc(uint size)
  211. {
  212. uint retloc;
  213. if (host_buffer == 0)
  214. alloc_host_memory();
  215. if ((host_buffer + size) >= host_end)
  216. return(0);
  217. retloc = host_buffer;
  218. host_buffer += size;
  219. return(retloc);
  220. }
  221. /* Set a baud rate generator. This needs lots of work. There are
  222. * four BRGs, any of which can be wired to any channel.
  223. * The internal baud rate clock is the system clock divided by 16.
  224. * This assumes the baudrate is 16x oversampled by the uart.
  225. */
  226. #define BRG_INT_CLK (get_brgfreq())
  227. #define BRG_UART_CLK (BRG_INT_CLK/16)
  228. #define BRG_UART_CLK_DIV16 (BRG_UART_CLK/16)
  229. void
  230. cpm_setbrg(uint brg, uint rate)
  231. {
  232. volatile uint *bp;
  233. /* This is good enough to get SMCs running.....
  234. */
  235. bp = (uint *)&cpmp->cp_brgc1;
  236. bp += brg;
  237. /* The BRG has a 12-bit counter. For really slow baud rates (or
  238. * really fast processors), we may have to further divide by 16.
  239. */
  240. if (((BRG_UART_CLK / rate) - 1) < 4096)
  241. *bp = (((BRG_UART_CLK / rate) - 1) << 1) | CPM_BRG_EN;
  242. else
  243. *bp = (((BRG_UART_CLK_DIV16 / rate) - 1) << 1) |
  244. CPM_BRG_EN | CPM_BRG_DIV16;
  245. }
  246. /*
  247. * dpalloc / dpfree bits.
  248. */
  249. static spinlock_t cpm_dpmem_lock;
  250. /*
  251. * 16 blocks should be enough to satisfy all requests
  252. * until the memory subsystem goes up...
  253. */
  254. static rh_block_t cpm_boot_dpmem_rh_block[16];
  255. static rh_info_t cpm_dpmem_info;
  256. #define CPM_DPMEM_ALIGNMENT 8
  257. static u8* dpram_vbase;
  258. static uint dpram_pbase;
  259. void m8xx_cpm_dpinit(void)
  260. {
  261. spin_lock_init(&cpm_dpmem_lock);
  262. dpram_vbase = immr_map_size(im_cpm.cp_dpmem, CPM_DATAONLY_BASE + CPM_DATAONLY_SIZE);
  263. dpram_pbase = (uint)&((immap_t *)IMAP_ADDR)->im_cpm.cp_dpmem;
  264. /* Initialize the info header */
  265. rh_init(&cpm_dpmem_info, CPM_DPMEM_ALIGNMENT,
  266. sizeof(cpm_boot_dpmem_rh_block) /
  267. sizeof(cpm_boot_dpmem_rh_block[0]),
  268. cpm_boot_dpmem_rh_block);
  269. /*
  270. * Attach the usable dpmem area.
  271. * XXX: This is actually crap. CPM_DATAONLY_BASE and
  272. * CPM_DATAONLY_SIZE are a subset of the available dparm. It varies
  273. * with the processor and the microcode patches applied / activated.
  274. * But the following should be at least safe.
  275. */
  276. rh_attach_region(&cpm_dpmem_info, CPM_DATAONLY_BASE, CPM_DATAONLY_SIZE);
  277. }
  278. /*
  279. * Allocate the requested size worth of DP memory.
  280. * This function returns an offset into the DPRAM area.
  281. * Use cpm_dpram_addr() to get the virtual address of the area.
  282. */
  283. unsigned long cpm_dpalloc(uint size, uint align)
  284. {
  285. unsigned long start;
  286. unsigned long flags;
  287. spin_lock_irqsave(&cpm_dpmem_lock, flags);
  288. cpm_dpmem_info.alignment = align;
  289. start = rh_alloc(&cpm_dpmem_info, size, "commproc");
  290. spin_unlock_irqrestore(&cpm_dpmem_lock, flags);
  291. return (uint)start;
  292. }
  293. EXPORT_SYMBOL(cpm_dpalloc);
  294. int cpm_dpfree(unsigned long offset)
  295. {
  296. int ret;
  297. unsigned long flags;
  298. spin_lock_irqsave(&cpm_dpmem_lock, flags);
  299. ret = rh_free(&cpm_dpmem_info, offset);
  300. spin_unlock_irqrestore(&cpm_dpmem_lock, flags);
  301. return ret;
  302. }
  303. EXPORT_SYMBOL(cpm_dpfree);
  304. unsigned long cpm_dpalloc_fixed(unsigned long offset, uint size, uint align)
  305. {
  306. unsigned long start;
  307. unsigned long flags;
  308. spin_lock_irqsave(&cpm_dpmem_lock, flags);
  309. cpm_dpmem_info.alignment = align;
  310. start = rh_alloc_fixed(&cpm_dpmem_info, offset, size, "commproc");
  311. spin_unlock_irqrestore(&cpm_dpmem_lock, flags);
  312. return start;
  313. }
  314. EXPORT_SYMBOL(cpm_dpalloc_fixed);
  315. void cpm_dpdump(void)
  316. {
  317. rh_dump(&cpm_dpmem_info);
  318. }
  319. EXPORT_SYMBOL(cpm_dpdump);
  320. void *cpm_dpram_addr(unsigned long offset)
  321. {
  322. return (void *)(dpram_vbase + offset);
  323. }
  324. EXPORT_SYMBOL(cpm_dpram_addr);
  325. uint cpm_dpram_phys(u8* addr)
  326. {
  327. return (dpram_pbase + (uint)(addr - dpram_vbase));
  328. }
  329. EXPORT_SYMBOL(cpm_dpram_addr);