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>
15 #include <linux/soc/ixp4xx/cpu.h>
17 static struct qmgr_regs __iomem *qmgr_regs;
18 static int qmgr_irq_1;
19 static int qmgr_irq_2;
20 static spinlock_t qmgr_lock;
21 static u32 used_sram_bitmap[4]; /* 128 16-dword pages */
22 static void (*irq_handlers[QUEUES])(void *pdev);
23 static void *irq_pdevs[QUEUES];
26 char qmgr_queue_descs[QUEUES][32];
29 void qmgr_put_entry(unsigned int queue, u32 val)
32 BUG_ON(!qmgr_queue_descs[queue]); /* not yet requested */
34 printk(KERN_DEBUG "Queue %s(%i) put %X\n",
35 qmgr_queue_descs[queue], queue, val);
37 __raw_writel(val, &qmgr_regs->acc[queue][0]);
40 u32 qmgr_get_entry(unsigned int queue)
43 val = __raw_readl(&qmgr_regs->acc[queue][0]);
45 BUG_ON(!qmgr_queue_descs[queue]); /* not yet requested */
47 printk(KERN_DEBUG "Queue %s(%i) get %X\n",
48 qmgr_queue_descs[queue], queue, val);
53 static int __qmgr_get_stat1(unsigned int queue)
55 return (__raw_readl(&qmgr_regs->stat1[queue >> 3])
56 >> ((queue & 7) << 2)) & 0xF;
59 static int __qmgr_get_stat2(unsigned int queue)
61 BUG_ON(queue >= HALF_QUEUES);
62 return (__raw_readl(&qmgr_regs->stat2[queue >> 4])
63 >> ((queue & 0xF) << 1)) & 0x3;
67 * qmgr_stat_empty() - checks if a hardware queue is empty
68 * @queue: queue number
70 * Returns non-zero value if the queue is empty.
72 int qmgr_stat_empty(unsigned int queue)
74 BUG_ON(queue >= HALF_QUEUES);
75 return __qmgr_get_stat1(queue) & QUEUE_STAT1_EMPTY;
79 * qmgr_stat_below_low_watermark() - checks if a queue is below low watermark
80 * @queue: queue number
82 * Returns non-zero value if the queue is below low watermark.
84 int qmgr_stat_below_low_watermark(unsigned int queue)
86 if (queue >= HALF_QUEUES)
87 return (__raw_readl(&qmgr_regs->statne_h) >>
88 (queue - HALF_QUEUES)) & 0x01;
89 return __qmgr_get_stat1(queue) & QUEUE_STAT1_NEARLY_EMPTY;
93 * qmgr_stat_full() - checks if a hardware queue is full
94 * @queue: queue number
96 * Returns non-zero value if the queue is full.
98 int qmgr_stat_full(unsigned int queue)
100 if (queue >= HALF_QUEUES)
101 return (__raw_readl(&qmgr_regs->statf_h) >>
102 (queue - HALF_QUEUES)) & 0x01;
103 return __qmgr_get_stat1(queue) & QUEUE_STAT1_FULL;
107 * qmgr_stat_overflow() - checks if a hardware queue experienced overflow
108 * @queue: queue number
110 * Returns non-zero value if the queue experienced overflow.
112 int qmgr_stat_overflow(unsigned int queue)
114 return __qmgr_get_stat2(queue) & QUEUE_STAT2_OVERFLOW;
117 void qmgr_set_irq(unsigned int queue, int src,
118 void (*handler)(void *pdev), void *pdev)
122 spin_lock_irqsave(&qmgr_lock, flags);
123 if (queue < HALF_QUEUES) {
126 BUG_ON(src > QUEUE_IRQ_SRC_NOT_FULL);
127 reg = &qmgr_regs->irqsrc[queue >> 3]; /* 8 queues per u32 */
128 bit = (queue % 8) * 4; /* 3 bits + 1 reserved bit per queue */
129 __raw_writel((__raw_readl(reg) & ~(7 << bit)) | (src << bit),
132 /* IRQ source for queues 32-63 is fixed */
133 BUG_ON(src != QUEUE_IRQ_SRC_NOT_NEARLY_EMPTY);
135 irq_handlers[queue] = handler;
136 irq_pdevs[queue] = pdev;
137 spin_unlock_irqrestore(&qmgr_lock, flags);
141 static irqreturn_t qmgr_irq1_a0(int irq, void *pdev)
144 u32 en_bitmap, src, stat;
146 /* ACK - it may clear any bits so don't rely on it */
147 __raw_writel(0xFFFFFFFF, &qmgr_regs->irqstat[0]);
149 en_bitmap = __raw_readl(&qmgr_regs->irqen[0]);
151 i = __fls(en_bitmap); /* number of the last "low" queue */
152 en_bitmap &= ~BIT(i);
153 src = __raw_readl(&qmgr_regs->irqsrc[i >> 3]);
154 stat = __raw_readl(&qmgr_regs->stat1[i >> 3]);
155 if (src & 4) /* the IRQ condition is inverted */
157 if (stat & BIT(src & 3)) {
158 irq_handlers[i](irq_pdevs[i]);
166 static irqreturn_t qmgr_irq2_a0(int irq, void *pdev)
171 /* ACK - it may clear any bits so don't rely on it */
172 __raw_writel(0xFFFFFFFF, &qmgr_regs->irqstat[1]);
174 req_bitmap = __raw_readl(&qmgr_regs->irqen[1]) &
175 __raw_readl(&qmgr_regs->statne_h);
177 i = __fls(req_bitmap); /* number of the last "high" queue */
178 req_bitmap &= ~BIT(i);
179 irq_handlers[HALF_QUEUES + i](irq_pdevs[HALF_QUEUES + i]);
186 static irqreturn_t qmgr_irq(int irq, void *pdev)
188 int i, half = (irq == qmgr_irq_1 ? 0 : 1);
189 u32 req_bitmap = __raw_readl(&qmgr_regs->irqstat[half]);
193 __raw_writel(req_bitmap, &qmgr_regs->irqstat[half]); /* ACK */
196 i = __fls(req_bitmap); /* number of the last queue */
197 req_bitmap &= ~BIT(i);
198 i += half * HALF_QUEUES;
199 irq_handlers[i](irq_pdevs[i]);
205 void qmgr_enable_irq(unsigned int queue)
208 int half = queue / 32;
209 u32 mask = 1 << (queue & (HALF_QUEUES - 1));
211 spin_lock_irqsave(&qmgr_lock, flags);
212 __raw_writel(__raw_readl(&qmgr_regs->irqen[half]) | mask,
213 &qmgr_regs->irqen[half]);
214 spin_unlock_irqrestore(&qmgr_lock, flags);
217 void qmgr_disable_irq(unsigned int queue)
220 int half = queue / 32;
221 u32 mask = 1 << (queue & (HALF_QUEUES - 1));
223 spin_lock_irqsave(&qmgr_lock, flags);
224 __raw_writel(__raw_readl(&qmgr_regs->irqen[half]) & ~mask,
225 &qmgr_regs->irqen[half]);
226 __raw_writel(mask, &qmgr_regs->irqstat[half]); /* clear */
227 spin_unlock_irqrestore(&qmgr_lock, flags);
230 static inline void shift_mask(u32 *mask)
232 mask[3] = mask[3] << 1 | mask[2] >> 31;
233 mask[2] = mask[2] << 1 | mask[1] >> 31;
234 mask[1] = mask[1] << 1 | mask[0] >> 31;
239 int qmgr_request_queue(unsigned int queue, unsigned int len /* dwords */,
240 unsigned int nearly_empty_watermark,
241 unsigned int nearly_full_watermark,
242 const char *desc_format, const char* name)
244 int __qmgr_request_queue(unsigned int queue, unsigned int len /* dwords */,
245 unsigned int nearly_empty_watermark,
246 unsigned int nearly_full_watermark)
249 u32 cfg, addr = 0, mask[4]; /* in 16-dwords */
252 BUG_ON(queue >= QUEUES);
254 if ((nearly_empty_watermark | nearly_full_watermark) & ~7)
278 cfg |= nearly_empty_watermark << 26;
279 cfg |= nearly_full_watermark << 29;
280 len /= 16; /* in 16-dwords: 1, 2, 4 or 8 */
281 mask[1] = mask[2] = mask[3] = 0;
283 if (!try_module_get(THIS_MODULE))
286 spin_lock_irq(&qmgr_lock);
287 if (__raw_readl(&qmgr_regs->sram[queue])) {
293 if (!(used_sram_bitmap[0] & mask[0]) &&
294 !(used_sram_bitmap[1] & mask[1]) &&
295 !(used_sram_bitmap[2] & mask[2]) &&
296 !(used_sram_bitmap[3] & mask[3]))
297 break; /* found free space */
301 if (addr + len > ARRAY_SIZE(qmgr_regs->sram)) {
302 printk(KERN_ERR "qmgr: no free SRAM space for"
303 " queue %i\n", queue);
309 used_sram_bitmap[0] |= mask[0];
310 used_sram_bitmap[1] |= mask[1];
311 used_sram_bitmap[2] |= mask[2];
312 used_sram_bitmap[3] |= mask[3];
313 __raw_writel(cfg | (addr << 14), &qmgr_regs->sram[queue]);
315 snprintf(qmgr_queue_descs[queue], sizeof(qmgr_queue_descs[0]),
317 printk(KERN_DEBUG "qmgr: requested queue %s(%i) addr = 0x%02X\n",
318 qmgr_queue_descs[queue], queue, addr);
320 spin_unlock_irq(&qmgr_lock);
324 spin_unlock_irq(&qmgr_lock);
325 module_put(THIS_MODULE);
329 void qmgr_release_queue(unsigned int queue)
331 u32 cfg, addr, mask[4];
333 BUG_ON(queue >= QUEUES); /* not in valid range */
335 spin_lock_irq(&qmgr_lock);
336 cfg = __raw_readl(&qmgr_regs->sram[queue]);
337 addr = (cfg >> 14) & 0xFF;
339 BUG_ON(!addr); /* not requested */
341 switch ((cfg >> 24) & 3) {
342 case 0: mask[0] = 0x1; break;
343 case 1: mask[0] = 0x3; break;
344 case 2: mask[0] = 0xF; break;
345 case 3: mask[0] = 0xFF; break;
348 mask[1] = mask[2] = mask[3] = 0;
354 printk(KERN_DEBUG "qmgr: releasing queue %s(%i)\n",
355 qmgr_queue_descs[queue], queue);
356 qmgr_queue_descs[queue][0] = '\x0';
359 while ((addr = qmgr_get_entry(queue)))
360 printk(KERN_ERR "qmgr: released queue %i not empty: 0x%08X\n",
363 __raw_writel(0, &qmgr_regs->sram[queue]);
365 used_sram_bitmap[0] &= ~mask[0];
366 used_sram_bitmap[1] &= ~mask[1];
367 used_sram_bitmap[2] &= ~mask[2];
368 used_sram_bitmap[3] &= ~mask[3];
369 irq_handlers[queue] = NULL; /* catch IRQ bugs */
370 spin_unlock_irq(&qmgr_lock);
372 module_put(THIS_MODULE);
375 static int ixp4xx_qmgr_probe(struct platform_device *pdev)
378 irq_handler_t handler1, handler2;
379 struct device *dev = &pdev->dev;
380 struct resource *res;
383 res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
386 qmgr_regs = devm_ioremap_resource(dev, res);
387 if (IS_ERR(qmgr_regs))
388 return PTR_ERR(qmgr_regs);
390 irq1 = platform_get_irq(pdev, 0);
392 return irq1 ? irq1 : -EINVAL;
394 irq2 = platform_get_irq(pdev, 1);
396 return irq2 ? irq2 : -EINVAL;
399 /* reset qmgr registers */
400 for (i = 0; i < 4; i++) {
401 __raw_writel(0x33333333, &qmgr_regs->stat1[i]);
402 __raw_writel(0, &qmgr_regs->irqsrc[i]);
404 for (i = 0; i < 2; i++) {
405 __raw_writel(0, &qmgr_regs->stat2[i]);
406 __raw_writel(0xFFFFFFFF, &qmgr_regs->irqstat[i]); /* clear */
407 __raw_writel(0, &qmgr_regs->irqen[i]);
410 __raw_writel(0xFFFFFFFF, &qmgr_regs->statne_h);
411 __raw_writel(0, &qmgr_regs->statf_h);
413 for (i = 0; i < QUEUES; i++)
414 __raw_writel(0, &qmgr_regs->sram[i]);
416 if (cpu_is_ixp42x_rev_a0()) {
417 handler1 = qmgr_irq1_a0;
418 handler2 = qmgr_irq2_a0;
420 handler1 = handler2 = qmgr_irq;
422 err = devm_request_irq(dev, irq1, handler1, 0, "IXP4xx Queue Manager",
425 dev_err(dev, "failed to request IRQ%i (%i)\n",
430 err = devm_request_irq(dev, irq2, handler2, 0, "IXP4xx Queue Manager",
433 dev_err(dev, "failed to request IRQ%i (%i)\n",
438 used_sram_bitmap[0] = 0xF; /* 4 first pages reserved for config */
439 spin_lock_init(&qmgr_lock);
441 dev_info(dev, "IXP4xx Queue Manager initialized.\n");
445 static int ixp4xx_qmgr_remove(struct platform_device *pdev)
447 synchronize_irq(qmgr_irq_1);
448 synchronize_irq(qmgr_irq_2);
452 static const struct of_device_id ixp4xx_qmgr_of_match[] = {
454 .compatible = "intel,ixp4xx-ahb-queue-manager",
459 static struct platform_driver ixp4xx_qmgr_driver = {
461 .name = "ixp4xx-qmgr",
462 .of_match_table = of_match_ptr(ixp4xx_qmgr_of_match),
464 .probe = ixp4xx_qmgr_probe,
465 .remove = ixp4xx_qmgr_remove,
467 module_platform_driver(ixp4xx_qmgr_driver);
469 MODULE_LICENSE("GPL v2");
470 MODULE_AUTHOR("Krzysztof Halasa");
472 EXPORT_SYMBOL(qmgr_put_entry);
473 EXPORT_SYMBOL(qmgr_get_entry);
474 EXPORT_SYMBOL(qmgr_stat_empty);
475 EXPORT_SYMBOL(qmgr_stat_below_low_watermark);
476 EXPORT_SYMBOL(qmgr_stat_full);
477 EXPORT_SYMBOL(qmgr_stat_overflow);
478 EXPORT_SYMBOL(qmgr_set_irq);
479 EXPORT_SYMBOL(qmgr_enable_irq);
480 EXPORT_SYMBOL(qmgr_disable_irq);
482 EXPORT_SYMBOL(qmgr_queue_descs);
483 EXPORT_SYMBOL(qmgr_request_queue);
485 EXPORT_SYMBOL(__qmgr_request_queue);
487 EXPORT_SYMBOL(qmgr_release_queue);