commproc.c 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446
  1. /*
  2. * General Purpose functions for the global management of the
  3. * Communication Processor Module.
  4. * Copyright (c) 1997 Dan Malek (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. static void m8xx_cpm_dpinit(void);
  42. static uint host_buffer; /* One page of host buffer */
  43. static uint host_end; /* end + 1 */
  44. cpm8xx_t *cpmp; /* Pointer to comm processor space */
  45. /* CPM interrupt vector functions.
  46. */
  47. struct cpm_action {
  48. void (*handler)(void *, struct pt_regs * regs);
  49. void *dev_id;
  50. };
  51. static struct cpm_action cpm_vecs[CPMVEC_NR];
  52. static irqreturn_t cpm_interrupt(int irq, void * dev, struct pt_regs * regs);
  53. static irqreturn_t cpm_error_interrupt(int irq, void *dev, struct pt_regs * regs);
  54. static void alloc_host_memory(void);
  55. /* Define a table of names to identify CPM interrupt handlers in
  56. * /proc/interrupts.
  57. */
  58. const char *cpm_int_name[] =
  59. { "error", "PC4", "PC5", "SMC2",
  60. "SMC1", "SPI", "PC6", "Timer 4",
  61. "", "PC7", "PC8", "PC9",
  62. "Timer 3", "", "PC10", "PC11",
  63. "I2C", "RISC Timer", "Timer 2", "",
  64. "IDMA2", "IDMA1", "SDMA error", "PC12",
  65. "PC13", "Timer 1", "PC14", "SCC4",
  66. "SCC3", "SCC2", "SCC1", "PC15"
  67. };
  68. static void
  69. cpm_mask_irq(unsigned int irq)
  70. {
  71. int cpm_vec = irq - CPM_IRQ_OFFSET;
  72. ((immap_t *)IMAP_ADDR)->im_cpic.cpic_cimr &= ~(1 << cpm_vec);
  73. }
  74. static void
  75. cpm_unmask_irq(unsigned int irq)
  76. {
  77. int cpm_vec = irq - CPM_IRQ_OFFSET;
  78. ((immap_t *)IMAP_ADDR)->im_cpic.cpic_cimr |= (1 << cpm_vec);
  79. }
  80. static void
  81. cpm_ack(unsigned int irq)
  82. {
  83. /* We do not need to do anything here. */
  84. }
  85. static void
  86. cpm_eoi(unsigned int irq)
  87. {
  88. int cpm_vec = irq - CPM_IRQ_OFFSET;
  89. ((immap_t *)IMAP_ADDR)->im_cpic.cpic_cisr = (1 << cpm_vec);
  90. }
  91. struct hw_interrupt_type cpm_pic = {
  92. .typename = " CPM ",
  93. .enable = cpm_unmask_irq,
  94. .disable = cpm_mask_irq,
  95. .ack = cpm_ack,
  96. .end = cpm_eoi,
  97. };
  98. void
  99. m8xx_cpm_reset(void)
  100. {
  101. volatile immap_t *imp;
  102. volatile cpm8xx_t *commproc;
  103. imp = (immap_t *)IMAP_ADDR;
  104. commproc = (cpm8xx_t *)&imp->im_cpm;
  105. #ifdef CONFIG_UCODE_PATCH
  106. /* Perform a reset.
  107. */
  108. commproc->cp_cpcr = (CPM_CR_RST | CPM_CR_FLG);
  109. /* Wait for it.
  110. */
  111. while (commproc->cp_cpcr & CPM_CR_FLG);
  112. cpm_load_patch(imp);
  113. #endif
  114. /* Set SDMA Bus Request priority 5.
  115. * On 860T, this also enables FEC priority 6. I am not sure
  116. * this is what we realy want for some applications, but the
  117. * manual recommends it.
  118. * Bit 25, FAM can also be set to use FEC aggressive mode (860T).
  119. */
  120. imp->im_siu_conf.sc_sdcr = 1;
  121. /* Reclaim the DP memory for our use. */
  122. m8xx_cpm_dpinit();
  123. /* Tell everyone where the comm processor resides.
  124. */
  125. cpmp = (cpm8xx_t *)commproc;
  126. }
  127. /* We used to do this earlier, but have to postpone as long as possible
  128. * to ensure the kernel VM is now running.
  129. */
  130. static void
  131. alloc_host_memory(void)
  132. {
  133. dma_addr_t physaddr;
  134. /* Set the host page for allocation.
  135. */
  136. host_buffer = (uint)dma_alloc_coherent(NULL, PAGE_SIZE, &physaddr,
  137. GFP_KERNEL);
  138. host_end = host_buffer + PAGE_SIZE;
  139. }
  140. /* This is called during init_IRQ. We used to do it above, but this
  141. * was too early since init_IRQ was not yet called.
  142. */
  143. static struct irqaction cpm_error_irqaction = {
  144. .handler = cpm_error_interrupt,
  145. .mask = CPU_MASK_NONE,
  146. };
  147. static struct irqaction cpm_interrupt_irqaction = {
  148. .handler = cpm_interrupt,
  149. .mask = CPU_MASK_NONE,
  150. .name = "CPM cascade",
  151. };
  152. void
  153. cpm_interrupt_init(void)
  154. {
  155. int i;
  156. /* Initialize the CPM interrupt controller.
  157. */
  158. ((immap_t *)IMAP_ADDR)->im_cpic.cpic_cicr =
  159. (CICR_SCD_SCC4 | CICR_SCC_SCC3 | CICR_SCB_SCC2 | CICR_SCA_SCC1) |
  160. ((CPM_INTERRUPT/2) << 13) | CICR_HP_MASK;
  161. ((immap_t *)IMAP_ADDR)->im_cpic.cpic_cimr = 0;
  162. /* install the CPM interrupt controller routines for the CPM
  163. * interrupt vectors
  164. */
  165. for ( i = CPM_IRQ_OFFSET ; i < CPM_IRQ_OFFSET + NR_CPM_INTS ; i++ )
  166. irq_desc[i].handler = &cpm_pic;
  167. /* Set our interrupt handler with the core CPU. */
  168. if (setup_irq(CPM_INTERRUPT, &cpm_interrupt_irqaction))
  169. panic("Could not allocate CPM IRQ!");
  170. /* Install our own error handler. */
  171. cpm_error_irqaction.name = cpm_int_name[CPMVEC_ERROR];
  172. if (setup_irq(CPM_IRQ_OFFSET + CPMVEC_ERROR, &cpm_error_irqaction))
  173. panic("Could not allocate CPM error IRQ!");
  174. ((immap_t *)IMAP_ADDR)->im_cpic.cpic_cicr |= CICR_IEN;
  175. }
  176. /*
  177. * Get the CPM interrupt vector.
  178. */
  179. int
  180. cpm_get_irq(struct pt_regs *regs)
  181. {
  182. int cpm_vec;
  183. /* Get the vector by setting the ACK bit and then reading
  184. * the register.
  185. */
  186. ((volatile immap_t *)IMAP_ADDR)->im_cpic.cpic_civr = 1;
  187. cpm_vec = ((volatile immap_t *)IMAP_ADDR)->im_cpic.cpic_civr;
  188. cpm_vec >>= 11;
  189. return cpm_vec;
  190. }
  191. /* CPM interrupt controller cascade interrupt.
  192. */
  193. static irqreturn_t
  194. cpm_interrupt(int irq, void * dev, struct pt_regs * regs)
  195. {
  196. /* This interrupt handler never actually gets called. It is
  197. * installed only to unmask the CPM cascade interrupt in the SIU
  198. * and to make the CPM cascade interrupt visible in /proc/interrupts.
  199. */
  200. return IRQ_HANDLED;
  201. }
  202. /* The CPM can generate the error interrupt when there is a race condition
  203. * between generating and masking interrupts. All we have to do is ACK it
  204. * and return. This is a no-op function so we don't need any special
  205. * tests in the interrupt handler.
  206. */
  207. static irqreturn_t
  208. cpm_error_interrupt(int irq, void *dev, struct pt_regs *regs)
  209. {
  210. return IRQ_HANDLED;
  211. }
  212. /* A helper function to translate the handler prototype required by
  213. * request_irq() to the handler prototype required by cpm_install_handler().
  214. */
  215. static irqreturn_t
  216. cpm_handler_helper(int irq, void *dev_id, struct pt_regs *regs)
  217. {
  218. int cpm_vec = irq - CPM_IRQ_OFFSET;
  219. (*cpm_vecs[cpm_vec].handler)(dev_id, regs);
  220. return IRQ_HANDLED;
  221. }
  222. /* Install a CPM interrupt handler.
  223. * This routine accepts a CPM interrupt vector in the range 0 to 31.
  224. * This routine is retained for backward compatibility. Rather than using
  225. * this routine to install a CPM interrupt handler, you can now use
  226. * request_irq() with an IRQ in the range CPM_IRQ_OFFSET to
  227. * CPM_IRQ_OFFSET + NR_CPM_INTS - 1 (16 to 47).
  228. *
  229. * Notice that the prototype of the interrupt handler function must be
  230. * different depending on whether you install the handler with
  231. * request_irq() or cpm_install_handler().
  232. */
  233. void
  234. cpm_install_handler(int cpm_vec, void (*handler)(void *, struct pt_regs *regs),
  235. void *dev_id)
  236. {
  237. int err;
  238. /* If null handler, assume we are trying to free the IRQ.
  239. */
  240. if (!handler) {
  241. free_irq(CPM_IRQ_OFFSET + cpm_vec, dev_id);
  242. return;
  243. }
  244. if (cpm_vecs[cpm_vec].handler != 0)
  245. printk(KERN_INFO "CPM interrupt %x replacing %x\n",
  246. (uint)handler, (uint)cpm_vecs[cpm_vec].handler);
  247. cpm_vecs[cpm_vec].handler = handler;
  248. cpm_vecs[cpm_vec].dev_id = dev_id;
  249. if ((err = request_irq(CPM_IRQ_OFFSET + cpm_vec, cpm_handler_helper,
  250. 0, cpm_int_name[cpm_vec], dev_id)))
  251. printk(KERN_ERR "request_irq() returned %d for CPM vector %d\n",
  252. err, cpm_vec);
  253. }
  254. /* Free a CPM interrupt handler.
  255. * This routine accepts a CPM interrupt vector in the range 0 to 31.
  256. * This routine is retained for backward compatibility.
  257. */
  258. void
  259. cpm_free_handler(int cpm_vec)
  260. {
  261. request_irq(CPM_IRQ_OFFSET + cpm_vec, NULL, 0, 0,
  262. cpm_vecs[cpm_vec].dev_id);
  263. cpm_vecs[cpm_vec].handler = NULL;
  264. cpm_vecs[cpm_vec].dev_id = NULL;
  265. }
  266. /* We also own one page of host buffer space for the allocation of
  267. * UART "fifos" and the like.
  268. */
  269. uint
  270. m8xx_cpm_hostalloc(uint size)
  271. {
  272. uint retloc;
  273. if (host_buffer == 0)
  274. alloc_host_memory();
  275. if ((host_buffer + size) >= host_end)
  276. return(0);
  277. retloc = host_buffer;
  278. host_buffer += size;
  279. return(retloc);
  280. }
  281. /* Set a baud rate generator. This needs lots of work. There are
  282. * four BRGs, any of which can be wired to any channel.
  283. * The internal baud rate clock is the system clock divided by 16.
  284. * This assumes the baudrate is 16x oversampled by the uart.
  285. */
  286. #define BRG_INT_CLK (((bd_t *)__res)->bi_intfreq)
  287. #define BRG_UART_CLK (BRG_INT_CLK/16)
  288. #define BRG_UART_CLK_DIV16 (BRG_UART_CLK/16)
  289. void
  290. cpm_setbrg(uint brg, uint rate)
  291. {
  292. volatile uint *bp;
  293. /* This is good enough to get SMCs running.....
  294. */
  295. bp = (uint *)&cpmp->cp_brgc1;
  296. bp += brg;
  297. /* The BRG has a 12-bit counter. For really slow baud rates (or
  298. * really fast processors), we may have to further divide by 16.
  299. */
  300. if (((BRG_UART_CLK / rate) - 1) < 4096)
  301. *bp = (((BRG_UART_CLK / rate) - 1) << 1) | CPM_BRG_EN;
  302. else
  303. *bp = (((BRG_UART_CLK_DIV16 / rate) - 1) << 1) |
  304. CPM_BRG_EN | CPM_BRG_DIV16;
  305. }
  306. /*
  307. * dpalloc / dpfree bits.
  308. */
  309. static spinlock_t cpm_dpmem_lock;
  310. /*
  311. * 16 blocks should be enough to satisfy all requests
  312. * until the memory subsystem goes up...
  313. */
  314. static rh_block_t cpm_boot_dpmem_rh_block[16];
  315. static rh_info_t cpm_dpmem_info;
  316. #define CPM_DPMEM_ALIGNMENT 8
  317. void m8xx_cpm_dpinit(void)
  318. {
  319. spin_lock_init(&cpm_dpmem_lock);
  320. /* Initialize the info header */
  321. rh_init(&cpm_dpmem_info, CPM_DPMEM_ALIGNMENT,
  322. sizeof(cpm_boot_dpmem_rh_block) /
  323. sizeof(cpm_boot_dpmem_rh_block[0]),
  324. cpm_boot_dpmem_rh_block);
  325. /*
  326. * Attach the usable dpmem area.
  327. * XXX: This is actually crap. CPM_DATAONLY_BASE and
  328. * CPM_DATAONLY_SIZE are a subset of the available dparm. It varies
  329. * with the processor and the microcode patches applied / activated.
  330. * But the following should be at least safe.
  331. */
  332. rh_attach_region(&cpm_dpmem_info, (void *)CPM_DATAONLY_BASE, CPM_DATAONLY_SIZE);
  333. }
  334. /*
  335. * Allocate the requested size worth of DP memory.
  336. * This function used to return an index into the DPRAM area.
  337. * Now it returns the actuall physical address of that area.
  338. * use m8xx_cpm_dpram_offset() to get the index
  339. */
  340. uint cpm_dpalloc(uint size, uint align)
  341. {
  342. void *start;
  343. unsigned long flags;
  344. spin_lock_irqsave(&cpm_dpmem_lock, flags);
  345. cpm_dpmem_info.alignment = align;
  346. start = rh_alloc(&cpm_dpmem_info, size, "commproc");
  347. spin_unlock_irqrestore(&cpm_dpmem_lock, flags);
  348. return (uint)start;
  349. }
  350. EXPORT_SYMBOL(cpm_dpalloc);
  351. int cpm_dpfree(uint offset)
  352. {
  353. int ret;
  354. unsigned long flags;
  355. spin_lock_irqsave(&cpm_dpmem_lock, flags);
  356. ret = rh_free(&cpm_dpmem_info, (void *)offset);
  357. spin_unlock_irqrestore(&cpm_dpmem_lock, flags);
  358. return ret;
  359. }
  360. EXPORT_SYMBOL(cpm_dpfree);
  361. uint cpm_dpalloc_fixed(uint offset, uint size, uint align)
  362. {
  363. void *start;
  364. unsigned long flags;
  365. spin_lock_irqsave(&cpm_dpmem_lock, flags);
  366. cpm_dpmem_info.alignment = align;
  367. start = rh_alloc_fixed(&cpm_dpmem_info, (void *)offset, size, "commproc");
  368. spin_unlock_irqrestore(&cpm_dpmem_lock, flags);
  369. return (uint)start;
  370. }
  371. EXPORT_SYMBOL(cpm_dpalloc_fixed);
  372. void cpm_dpdump(void)
  373. {
  374. rh_dump(&cpm_dpmem_info);
  375. }
  376. EXPORT_SYMBOL(cpm_dpdump);
  377. void *cpm_dpram_addr(uint offset)
  378. {
  379. return ((immap_t *)IMAP_ADDR)->im_cpm.cp_dpmem + offset;
  380. }
  381. EXPORT_SYMBOL(cpm_dpram_addr);