1 // SPDX-License-Identifier: GPL-2.0-only
3 * Intel IXP4xx Queue Manager driver for Linux
5 * Copyright (C) 2007 Krzysztof Halasa <khc@pm.waw.pl>
8 #include <linux/ioport.h>
9 #include <linux/interrupt.h>
10 #include <linux/kernel.h>
11 #include <linux/module.h>
13 #include <linux/platform_device.h>
14 #include <linux/soc/ixp4xx/qmgr.h>
16 static struct qmgr_regs __iomem *qmgr_regs;
17 static int qmgr_irq_1;
18 static int qmgr_irq_2;
19 static spinlock_t qmgr_lock;
20 static u32 used_sram_bitmap[4]; /* 128 16-dword pages */
21 static void (*irq_handlers[QUEUES])(void *pdev);
22 static void *irq_pdevs[QUEUES];
25 char qmgr_queue_descs[QUEUES][32];
28 void qmgr_put_entry(unsigned int queue, u32 val)
31 BUG_ON(!qmgr_queue_descs[queue]); /* not yet requested */
33 printk(KERN_DEBUG "Queue %s(%i) put %X\n",
34 qmgr_queue_descs[queue], queue, val);
36 __raw_writel(val, &qmgr_regs->acc[queue][0]);
39 u32 qmgr_get_entry(unsigned int queue)
42 val = __raw_readl(&qmgr_regs->acc[queue][0]);
44 BUG_ON(!qmgr_queue_descs[queue]); /* not yet requested */
46 printk(KERN_DEBUG "Queue %s(%i) get %X\n",
47 qmgr_queue_descs[queue], queue, val);
52 static int __qmgr_get_stat1(unsigned int queue)
54 return (__raw_readl(&qmgr_regs->stat1[queue >> 3])
55 >> ((queue & 7) << 2)) & 0xF;
58 static int __qmgr_get_stat2(unsigned int queue)
60 BUG_ON(queue >= HALF_QUEUES);
61 return (__raw_readl(&qmgr_regs->stat2[queue >> 4])
62 >> ((queue & 0xF) << 1)) & 0x3;
66 * qmgr_stat_empty() - checks if a hardware queue is empty
67 * @queue: queue number
69 * Returns non-zero value if the queue is empty.
71 int qmgr_stat_empty(unsigned int queue)
73 BUG_ON(queue >= HALF_QUEUES);
74 return __qmgr_get_stat1(queue) & QUEUE_STAT1_EMPTY;
78 * qmgr_stat_below_low_watermark() - checks if a queue is below low watermark
79 * @queue: queue number
81 * Returns non-zero value if the queue is below low watermark.
83 int qmgr_stat_below_low_watermark(unsigned int queue)
85 if (queue >= HALF_QUEUES)
86 return (__raw_readl(&qmgr_regs->statne_h) >>
87 (queue - HALF_QUEUES)) & 0x01;
88 return __qmgr_get_stat1(queue) & QUEUE_STAT1_NEARLY_EMPTY;
92 * qmgr_stat_full() - checks if a hardware queue is full
93 * @queue: queue number
95 * Returns non-zero value if the queue is full.
97 int qmgr_stat_full(unsigned int queue)
99 if (queue >= HALF_QUEUES)
100 return (__raw_readl(&qmgr_regs->statf_h) >>
101 (queue - HALF_QUEUES)) & 0x01;
102 return __qmgr_get_stat1(queue) & QUEUE_STAT1_FULL;
106 * qmgr_stat_overflow() - checks if a hardware queue experienced overflow
107 * @queue: queue number
109 * Returns non-zero value if the queue experienced overflow.
111 int qmgr_stat_overflow(unsigned int queue)
113 return __qmgr_get_stat2(queue) & QUEUE_STAT2_OVERFLOW;
116 void qmgr_set_irq(unsigned int queue, int src,
117 void (*handler)(void *pdev), void *pdev)
121 spin_lock_irqsave(&qmgr_lock, flags);
122 if (queue < HALF_QUEUES) {
125 BUG_ON(src > QUEUE_IRQ_SRC_NOT_FULL);
126 reg = &qmgr_regs->irqsrc[queue >> 3]; /* 8 queues per u32 */
127 bit = (queue % 8) * 4; /* 3 bits + 1 reserved bit per queue */
128 __raw_writel((__raw_readl(reg) & ~(7 << bit)) | (src << bit),
131 /* IRQ source for queues 32-63 is fixed */
132 BUG_ON(src != QUEUE_IRQ_SRC_NOT_NEARLY_EMPTY);
134 irq_handlers[queue] = handler;
135 irq_pdevs[queue] = pdev;
136 spin_unlock_irqrestore(&qmgr_lock, flags);
140 static irqreturn_t qmgr_irq1_a0(int irq, void *pdev)
143 u32 en_bitmap, src, stat;
145 /* ACK - it may clear any bits so don't rely on it */
146 __raw_writel(0xFFFFFFFF, &qmgr_regs->irqstat[0]);
148 en_bitmap = __raw_readl(&qmgr_regs->irqen[0]);
150 i = __fls(en_bitmap); /* number of the last "low" queue */
151 en_bitmap &= ~BIT(i);
152 src = __raw_readl(&qmgr_regs->irqsrc[i >> 3]);
153 stat = __raw_readl(&qmgr_regs->stat1[i >> 3]);
154 if (src & 4) /* the IRQ condition is inverted */
156 if (stat & BIT(src & 3)) {
157 irq_handlers[i](irq_pdevs[i]);
165 static irqreturn_t qmgr_irq2_a0(int irq, void *pdev)
170 /* ACK - it may clear any bits so don't rely on it */
171 __raw_writel(0xFFFFFFFF, &qmgr_regs->irqstat[1]);
173 req_bitmap = __raw_readl(&qmgr_regs->irqen[1]) &
174 __raw_readl(&qmgr_regs->statne_h);
176 i = __fls(req_bitmap); /* number of the last "high" queue */
177 req_bitmap &= ~BIT(i);
178 irq_handlers[HALF_QUEUES + i](irq_pdevs[HALF_QUEUES + i]);
185 static irqreturn_t qmgr_irq(int irq, void *pdev)
187 int i, half = (irq == qmgr_irq_1 ? 0 : 1);
188 u32 req_bitmap = __raw_readl(&qmgr_regs->irqstat[half]);
192 __raw_writel(req_bitmap, &qmgr_regs->irqstat[half]); /* ACK */
195 i = __fls(req_bitmap); /* number of the last queue */
196 req_bitmap &= ~BIT(i);
197 i += half * HALF_QUEUES;
198 irq_handlers[i](irq_pdevs[i]);
204 void qmgr_enable_irq(unsigned int queue)
207 int half = queue / 32;
208 u32 mask = 1 << (queue & (HALF_QUEUES - 1));
210 spin_lock_irqsave(&qmgr_lock, flags);
211 __raw_writel(__raw_readl(&qmgr_regs->irqen[half]) | mask,
212 &qmgr_regs->irqen[half]);
213 spin_unlock_irqrestore(&qmgr_lock, flags);
216 void qmgr_disable_irq(unsigned int queue)
219 int half = queue / 32;
220 u32 mask = 1 << (queue & (HALF_QUEUES - 1));
222 spin_lock_irqsave(&qmgr_lock, flags);
223 __raw_writel(__raw_readl(&qmgr_regs->irqen[half]) & ~mask,
224 &qmgr_regs->irqen[half]);
225 __raw_writel(mask, &qmgr_regs->irqstat[half]); /* clear */
226 spin_unlock_irqrestore(&qmgr_lock, flags);
229 static inline void shift_mask(u32 *mask)
231 mask[3] = mask[3] << 1 | mask[2] >> 31;
232 mask[2] = mask[2] << 1 | mask[1] >> 31;
233 mask[1] = mask[1] << 1 | mask[0] >> 31;
238 int qmgr_request_queue(unsigned int queue, unsigned int len /* dwords */,
239 unsigned int nearly_empty_watermark,
240 unsigned int nearly_full_watermark,
241 const char *desc_format, const char* name)
243 int __qmgr_request_queue(unsigned int queue, unsigned int len /* dwords */,
244 unsigned int nearly_empty_watermark,
245 unsigned int nearly_full_watermark)
248 u32 cfg, addr = 0, mask[4]; /* in 16-dwords */
251 BUG_ON(queue >= QUEUES);
253 if ((nearly_empty_watermark | nearly_full_watermark) & ~7)
277 cfg |= nearly_empty_watermark << 26;
278 cfg |= nearly_full_watermark << 29;
279 len /= 16; /* in 16-dwords: 1, 2, 4 or 8 */
280 mask[1] = mask[2] = mask[3] = 0;
282 if (!try_module_get(THIS_MODULE))
285 spin_lock_irq(&qmgr_lock);
286 if (__raw_readl(&qmgr_regs->sram[queue])) {
292 if (!(used_sram_bitmap[0] & mask[0]) &&
293 !(used_sram_bitmap[1] & mask[1]) &&
294 !(used_sram_bitmap[2] & mask[2]) &&
295 !(used_sram_bitmap[3] & mask[3]))
296 break; /* found free space */
300 if (addr + len > ARRAY_SIZE(qmgr_regs->sram)) {
301 printk(KERN_ERR "qmgr: no free SRAM space for"
302 " queue %i\n", queue);
308 used_sram_bitmap[0] |= mask[0];
309 used_sram_bitmap[1] |= mask[1];
310 used_sram_bitmap[2] |= mask[2];
311 used_sram_bitmap[3] |= mask[3];
312 __raw_writel(cfg | (addr << 14), &qmgr_regs->sram[queue]);
314 snprintf(qmgr_queue_descs[queue], sizeof(qmgr_queue_descs[0]),
316 printk(KERN_DEBUG "qmgr: requested queue %s(%i) addr = 0x%02X\n",
317 qmgr_queue_descs[queue], queue, addr);
319 spin_unlock_irq(&qmgr_lock);
323 spin_unlock_irq(&qmgr_lock);
324 module_put(THIS_MODULE);
328 void qmgr_release_queue(unsigned int queue)
330 u32 cfg, addr, mask[4];
332 BUG_ON(queue >= QUEUES); /* not in valid range */
334 spin_lock_irq(&qmgr_lock);
335 cfg = __raw_readl(&qmgr_regs->sram[queue]);
336 addr = (cfg >> 14) & 0xFF;
338 BUG_ON(!addr); /* not requested */
340 switch ((cfg >> 24) & 3) {
341 case 0: mask[0] = 0x1; break;
342 case 1: mask[0] = 0x3; break;
343 case 2: mask[0] = 0xF; break;
344 case 3: mask[0] = 0xFF; break;
347 mask[1] = mask[2] = mask[3] = 0;
353 printk(KERN_DEBUG "qmgr: releasing queue %s(%i)\n",
354 qmgr_queue_descs[queue], queue);
355 qmgr_queue_descs[queue][0] = '\x0';
358 while ((addr = qmgr_get_entry(queue)))
359 printk(KERN_ERR "qmgr: released queue %i not empty: 0x%08X\n",
362 __raw_writel(0, &qmgr_regs->sram[queue]);
364 used_sram_bitmap[0] &= ~mask[0];
365 used_sram_bitmap[1] &= ~mask[1];
366 used_sram_bitmap[2] &= ~mask[2];
367 used_sram_bitmap[3] &= ~mask[3];
368 irq_handlers[queue] = NULL; /* catch IRQ bugs */
369 spin_unlock_irq(&qmgr_lock);
371 module_put(THIS_MODULE);
374 static int ixp4xx_qmgr_probe(struct platform_device *pdev)
377 irq_handler_t handler1, handler2;
378 struct device *dev = &pdev->dev;
379 struct resource *res;
382 res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
385 qmgr_regs = devm_ioremap_resource(dev, res);
386 if (IS_ERR(qmgr_regs))
387 return PTR_ERR(qmgr_regs);
389 irq1 = platform_get_irq(pdev, 0);
391 return irq1 ? irq1 : -EINVAL;
393 irq2 = platform_get_irq(pdev, 1);
395 return irq2 ? irq2 : -EINVAL;
398 /* reset qmgr registers */
399 for (i = 0; i < 4; i++) {
400 __raw_writel(0x33333333, &qmgr_regs->stat1[i]);
401 __raw_writel(0, &qmgr_regs->irqsrc[i]);
403 for (i = 0; i < 2; i++) {
404 __raw_writel(0, &qmgr_regs->stat2[i]);
405 __raw_writel(0xFFFFFFFF, &qmgr_regs->irqstat[i]); /* clear */
406 __raw_writel(0, &qmgr_regs->irqen[i]);
409 __raw_writel(0xFFFFFFFF, &qmgr_regs->statne_h);
410 __raw_writel(0, &qmgr_regs->statf_h);
412 for (i = 0; i < QUEUES; i++)
413 __raw_writel(0, &qmgr_regs->sram[i]);
415 if (cpu_is_ixp42x_rev_a0()) {
416 handler1 = qmgr_irq1_a0;
417 handler2 = qmgr_irq2_a0;
419 handler1 = handler2 = qmgr_irq;
421 err = devm_request_irq(dev, irq1, handler1, 0, "IXP4xx Queue Manager",
424 dev_err(dev, "failed to request IRQ%i (%i)\n",
429 err = devm_request_irq(dev, irq2, handler2, 0, "IXP4xx Queue Manager",
432 dev_err(dev, "failed to request IRQ%i (%i)\n",
437 used_sram_bitmap[0] = 0xF; /* 4 first pages reserved for config */
438 spin_lock_init(&qmgr_lock);
440 dev_info(dev, "IXP4xx Queue Manager initialized.\n");
444 static int ixp4xx_qmgr_remove(struct platform_device *pdev)
446 synchronize_irq(qmgr_irq_1);
447 synchronize_irq(qmgr_irq_2);
451 static const struct of_device_id ixp4xx_qmgr_of_match[] = {
453 .compatible = "intel,ixp4xx-ahb-queue-manager",
458 static struct platform_driver ixp4xx_qmgr_driver = {
460 .name = "ixp4xx-qmgr",
461 .of_match_table = of_match_ptr(ixp4xx_qmgr_of_match),
463 .probe = ixp4xx_qmgr_probe,
464 .remove = ixp4xx_qmgr_remove,
466 module_platform_driver(ixp4xx_qmgr_driver);
468 MODULE_LICENSE("GPL v2");
469 MODULE_AUTHOR("Krzysztof Halasa");
471 EXPORT_SYMBOL(qmgr_put_entry);
472 EXPORT_SYMBOL(qmgr_get_entry);
473 EXPORT_SYMBOL(qmgr_stat_empty);
474 EXPORT_SYMBOL(qmgr_stat_below_low_watermark);
475 EXPORT_SYMBOL(qmgr_stat_full);
476 EXPORT_SYMBOL(qmgr_stat_overflow);
477 EXPORT_SYMBOL(qmgr_set_irq);
478 EXPORT_SYMBOL(qmgr_enable_irq);
479 EXPORT_SYMBOL(qmgr_disable_irq);
481 EXPORT_SYMBOL(qmgr_queue_descs);
482 EXPORT_SYMBOL(qmgr_request_queue);
484 EXPORT_SYMBOL(__qmgr_request_queue);
486 EXPORT_SYMBOL(qmgr_release_queue);