2 * Intel IXP4xx Queue Manager driver for Linux
4 * Copyright (C) 2007 Krzysztof Halasa <khc@pm.waw.pl>
6 * This program is free software; you can redistribute it and/or modify it
7 * under the terms of version 2 of the GNU General Public License
8 * as published by the Free Software Foundation.
11 #include <linux/ioport.h>
12 #include <linux/interrupt.h>
13 #include <linux/kernel.h>
14 #include <linux/module.h>
16 #include <linux/platform_device.h>
17 #include <linux/soc/ixp4xx/qmgr.h>
19 static struct qmgr_regs __iomem *qmgr_regs;
20 static int qmgr_irq_1;
21 static int qmgr_irq_2;
22 static spinlock_t qmgr_lock;
23 static u32 used_sram_bitmap[4]; /* 128 16-dword pages */
24 static void (*irq_handlers[QUEUES])(void *pdev);
25 static void *irq_pdevs[QUEUES];
28 char qmgr_queue_descs[QUEUES][32];
31 void qmgr_put_entry(unsigned int queue, u32 val)
34 BUG_ON(!qmgr_queue_descs[queue]); /* not yet requested */
36 printk(KERN_DEBUG "Queue %s(%i) put %X\n",
37 qmgr_queue_descs[queue], queue, val);
39 __raw_writel(val, &qmgr_regs->acc[queue][0]);
42 u32 qmgr_get_entry(unsigned int queue)
45 val = __raw_readl(&qmgr_regs->acc[queue][0]);
47 BUG_ON(!qmgr_queue_descs[queue]); /* not yet requested */
49 printk(KERN_DEBUG "Queue %s(%i) get %X\n",
50 qmgr_queue_descs[queue], queue, val);
55 static int __qmgr_get_stat1(unsigned int queue)
57 return (__raw_readl(&qmgr_regs->stat1[queue >> 3])
58 >> ((queue & 7) << 2)) & 0xF;
61 static int __qmgr_get_stat2(unsigned int queue)
63 BUG_ON(queue >= HALF_QUEUES);
64 return (__raw_readl(&qmgr_regs->stat2[queue >> 4])
65 >> ((queue & 0xF) << 1)) & 0x3;
69 * qmgr_stat_empty() - checks if a hardware queue is empty
70 * @queue: queue number
72 * Returns non-zero value if the queue is empty.
74 int qmgr_stat_empty(unsigned int queue)
76 BUG_ON(queue >= HALF_QUEUES);
77 return __qmgr_get_stat1(queue) & QUEUE_STAT1_EMPTY;
81 * qmgr_stat_below_low_watermark() - checks if a queue is below low watermark
82 * @queue: queue number
84 * Returns non-zero value if the queue is below low watermark.
86 int qmgr_stat_below_low_watermark(unsigned int queue)
88 if (queue >= HALF_QUEUES)
89 return (__raw_readl(&qmgr_regs->statne_h) >>
90 (queue - HALF_QUEUES)) & 0x01;
91 return __qmgr_get_stat1(queue) & QUEUE_STAT1_NEARLY_EMPTY;
95 * qmgr_stat_full() - checks if a hardware queue is full
96 * @queue: queue number
98 * Returns non-zero value if the queue is full.
100 int qmgr_stat_full(unsigned int queue)
102 if (queue >= HALF_QUEUES)
103 return (__raw_readl(&qmgr_regs->statf_h) >>
104 (queue - HALF_QUEUES)) & 0x01;
105 return __qmgr_get_stat1(queue) & QUEUE_STAT1_FULL;
109 * qmgr_stat_overflow() - checks if a hardware queue experienced overflow
110 * @queue: queue number
112 * Returns non-zero value if the queue experienced overflow.
114 int qmgr_stat_overflow(unsigned int queue)
116 return __qmgr_get_stat2(queue) & QUEUE_STAT2_OVERFLOW;
119 void qmgr_set_irq(unsigned int queue, int src,
120 void (*handler)(void *pdev), void *pdev)
124 spin_lock_irqsave(&qmgr_lock, flags);
125 if (queue < HALF_QUEUES) {
128 BUG_ON(src > QUEUE_IRQ_SRC_NOT_FULL);
129 reg = &qmgr_regs->irqsrc[queue >> 3]; /* 8 queues per u32 */
130 bit = (queue % 8) * 4; /* 3 bits + 1 reserved bit per queue */
131 __raw_writel((__raw_readl(reg) & ~(7 << bit)) | (src << bit),
134 /* IRQ source for queues 32-63 is fixed */
135 BUG_ON(src != QUEUE_IRQ_SRC_NOT_NEARLY_EMPTY);
137 irq_handlers[queue] = handler;
138 irq_pdevs[queue] = pdev;
139 spin_unlock_irqrestore(&qmgr_lock, flags);
143 static irqreturn_t qmgr_irq1_a0(int irq, void *pdev)
146 u32 en_bitmap, src, stat;
148 /* ACK - it may clear any bits so don't rely on it */
149 __raw_writel(0xFFFFFFFF, &qmgr_regs->irqstat[0]);
151 en_bitmap = qmgr_regs->irqen[0];
153 i = __fls(en_bitmap); /* number of the last "low" queue */
154 en_bitmap &= ~BIT(i);
155 src = qmgr_regs->irqsrc[i >> 3];
156 stat = qmgr_regs->stat1[i >> 3];
157 if (src & 4) /* the IRQ condition is inverted */
159 if (stat & BIT(src & 3)) {
160 irq_handlers[i](irq_pdevs[i]);
168 static irqreturn_t qmgr_irq2_a0(int irq, void *pdev)
173 /* ACK - it may clear any bits so don't rely on it */
174 __raw_writel(0xFFFFFFFF, &qmgr_regs->irqstat[1]);
176 req_bitmap = qmgr_regs->irqen[1] & qmgr_regs->statne_h;
178 i = __fls(req_bitmap); /* number of the last "high" queue */
179 req_bitmap &= ~BIT(i);
180 irq_handlers[HALF_QUEUES + i](irq_pdevs[HALF_QUEUES + i]);
187 static irqreturn_t qmgr_irq(int irq, void *pdev)
189 int i, half = (irq == qmgr_irq_1 ? 0 : 1);
190 u32 req_bitmap = __raw_readl(&qmgr_regs->irqstat[half]);
194 __raw_writel(req_bitmap, &qmgr_regs->irqstat[half]); /* ACK */
197 i = __fls(req_bitmap); /* number of the last queue */
198 req_bitmap &= ~BIT(i);
199 i += half * HALF_QUEUES;
200 irq_handlers[i](irq_pdevs[i]);
206 void qmgr_enable_irq(unsigned int queue)
209 int half = queue / 32;
210 u32 mask = 1 << (queue & (HALF_QUEUES - 1));
212 spin_lock_irqsave(&qmgr_lock, flags);
213 __raw_writel(__raw_readl(&qmgr_regs->irqen[half]) | mask,
214 &qmgr_regs->irqen[half]);
215 spin_unlock_irqrestore(&qmgr_lock, flags);
218 void qmgr_disable_irq(unsigned int queue)
221 int half = queue / 32;
222 u32 mask = 1 << (queue & (HALF_QUEUES - 1));
224 spin_lock_irqsave(&qmgr_lock, flags);
225 __raw_writel(__raw_readl(&qmgr_regs->irqen[half]) & ~mask,
226 &qmgr_regs->irqen[half]);
227 __raw_writel(mask, &qmgr_regs->irqstat[half]); /* clear */
228 spin_unlock_irqrestore(&qmgr_lock, flags);
231 static inline void shift_mask(u32 *mask)
233 mask[3] = mask[3] << 1 | mask[2] >> 31;
234 mask[2] = mask[2] << 1 | mask[1] >> 31;
235 mask[1] = mask[1] << 1 | mask[0] >> 31;
240 int qmgr_request_queue(unsigned int queue, unsigned int len /* dwords */,
241 unsigned int nearly_empty_watermark,
242 unsigned int nearly_full_watermark,
243 const char *desc_format, const char* name)
245 int __qmgr_request_queue(unsigned int queue, unsigned int len /* dwords */,
246 unsigned int nearly_empty_watermark,
247 unsigned int nearly_full_watermark)
250 u32 cfg, addr = 0, mask[4]; /* in 16-dwords */
253 BUG_ON(queue >= QUEUES);
255 if ((nearly_empty_watermark | nearly_full_watermark) & ~7)
279 cfg |= nearly_empty_watermark << 26;
280 cfg |= nearly_full_watermark << 29;
281 len /= 16; /* in 16-dwords: 1, 2, 4 or 8 */
282 mask[1] = mask[2] = mask[3] = 0;
284 if (!try_module_get(THIS_MODULE))
287 spin_lock_irq(&qmgr_lock);
288 if (__raw_readl(&qmgr_regs->sram[queue])) {
294 if (!(used_sram_bitmap[0] & mask[0]) &&
295 !(used_sram_bitmap[1] & mask[1]) &&
296 !(used_sram_bitmap[2] & mask[2]) &&
297 !(used_sram_bitmap[3] & mask[3]))
298 break; /* found free space */
302 if (addr + len > ARRAY_SIZE(qmgr_regs->sram)) {
303 printk(KERN_ERR "qmgr: no free SRAM space for"
304 " queue %i\n", queue);
310 used_sram_bitmap[0] |= mask[0];
311 used_sram_bitmap[1] |= mask[1];
312 used_sram_bitmap[2] |= mask[2];
313 used_sram_bitmap[3] |= mask[3];
314 __raw_writel(cfg | (addr << 14), &qmgr_regs->sram[queue]);
316 snprintf(qmgr_queue_descs[queue], sizeof(qmgr_queue_descs[0]),
318 printk(KERN_DEBUG "qmgr: requested queue %s(%i) addr = 0x%02X\n",
319 qmgr_queue_descs[queue], queue, addr);
321 spin_unlock_irq(&qmgr_lock);
325 spin_unlock_irq(&qmgr_lock);
326 module_put(THIS_MODULE);
330 void qmgr_release_queue(unsigned int queue)
332 u32 cfg, addr, mask[4];
334 BUG_ON(queue >= QUEUES); /* not in valid range */
336 spin_lock_irq(&qmgr_lock);
337 cfg = __raw_readl(&qmgr_regs->sram[queue]);
338 addr = (cfg >> 14) & 0xFF;
340 BUG_ON(!addr); /* not requested */
342 switch ((cfg >> 24) & 3) {
343 case 0: mask[0] = 0x1; break;
344 case 1: mask[0] = 0x3; break;
345 case 2: mask[0] = 0xF; break;
346 case 3: mask[0] = 0xFF; break;
349 mask[1] = mask[2] = mask[3] = 0;
355 printk(KERN_DEBUG "qmgr: releasing queue %s(%i)\n",
356 qmgr_queue_descs[queue], queue);
357 qmgr_queue_descs[queue][0] = '\x0';
360 while ((addr = qmgr_get_entry(queue)))
361 printk(KERN_ERR "qmgr: released queue %i not empty: 0x%08X\n",
364 __raw_writel(0, &qmgr_regs->sram[queue]);
366 used_sram_bitmap[0] &= ~mask[0];
367 used_sram_bitmap[1] &= ~mask[1];
368 used_sram_bitmap[2] &= ~mask[2];
369 used_sram_bitmap[3] &= ~mask[3];
370 irq_handlers[queue] = NULL; /* catch IRQ bugs */
371 spin_unlock_irq(&qmgr_lock);
373 module_put(THIS_MODULE);
376 static int ixp4xx_qmgr_probe(struct platform_device *pdev)
379 irq_handler_t handler1, handler2;
380 struct device *dev = &pdev->dev;
381 struct resource *res;
384 res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
387 qmgr_regs = devm_ioremap_resource(dev, res);
388 if (IS_ERR(qmgr_regs))
389 return PTR_ERR(qmgr_regs);
391 irq1 = platform_get_irq(pdev, 0);
393 return irq1 ? irq1 : -EINVAL;
395 irq2 = platform_get_irq(pdev, 1);
397 return irq2 ? irq2 : -EINVAL;
400 /* reset qmgr registers */
401 for (i = 0; i < 4; i++) {
402 __raw_writel(0x33333333, &qmgr_regs->stat1[i]);
403 __raw_writel(0, &qmgr_regs->irqsrc[i]);
405 for (i = 0; i < 2; i++) {
406 __raw_writel(0, &qmgr_regs->stat2[i]);
407 __raw_writel(0xFFFFFFFF, &qmgr_regs->irqstat[i]); /* clear */
408 __raw_writel(0, &qmgr_regs->irqen[i]);
411 __raw_writel(0xFFFFFFFF, &qmgr_regs->statne_h);
412 __raw_writel(0, &qmgr_regs->statf_h);
414 for (i = 0; i < QUEUES; i++)
415 __raw_writel(0, &qmgr_regs->sram[i]);
417 if (cpu_is_ixp42x_rev_a0()) {
418 handler1 = qmgr_irq1_a0;
419 handler2 = qmgr_irq2_a0;
421 handler1 = handler2 = qmgr_irq;
423 err = devm_request_irq(dev, irq1, handler1, 0, "IXP4xx Queue Manager",
426 dev_err(dev, "failed to request IRQ%i (%i)\n",
431 err = devm_request_irq(dev, irq2, handler2, 0, "IXP4xx Queue Manager",
434 dev_err(dev, "failed to request IRQ%i (%i)\n",
439 used_sram_bitmap[0] = 0xF; /* 4 first pages reserved for config */
440 spin_lock_init(&qmgr_lock);
442 dev_info(dev, "IXP4xx Queue Manager initialized.\n");
446 static int ixp4xx_qmgr_remove(struct platform_device *pdev)
448 synchronize_irq(qmgr_irq_1);
449 synchronize_irq(qmgr_irq_2);
453 static const struct of_device_id ixp4xx_qmgr_of_match[] = {
455 .compatible = "intel,ixp4xx-ahb-queue-manager",
460 static struct platform_driver ixp4xx_qmgr_driver = {
462 .name = "ixp4xx-qmgr",
463 .of_match_table = of_match_ptr(ixp4xx_qmgr_of_match),
465 .probe = ixp4xx_qmgr_probe,
466 .remove = ixp4xx_qmgr_remove,
468 module_platform_driver(ixp4xx_qmgr_driver);
470 MODULE_LICENSE("GPL v2");
471 MODULE_AUTHOR("Krzysztof Halasa");
473 EXPORT_SYMBOL(qmgr_put_entry);
474 EXPORT_SYMBOL(qmgr_get_entry);
475 EXPORT_SYMBOL(qmgr_stat_empty);
476 EXPORT_SYMBOL(qmgr_stat_below_low_watermark);
477 EXPORT_SYMBOL(qmgr_stat_full);
478 EXPORT_SYMBOL(qmgr_stat_overflow);
479 EXPORT_SYMBOL(qmgr_set_irq);
480 EXPORT_SYMBOL(qmgr_enable_irq);
481 EXPORT_SYMBOL(qmgr_disable_irq);
483 EXPORT_SYMBOL(qmgr_queue_descs);
484 EXPORT_SYMBOL(qmgr_request_queue);
486 EXPORT_SYMBOL(__qmgr_request_queue);
488 EXPORT_SYMBOL(qmgr_release_queue);