commproc.c 9.8 KB

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