commproc.c 12 KB

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