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