commproc.c 12 KB

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