1 // SPDX-License-Identifier: GPL-2.0-only
3 * Copyright 2011-2012, Meador Inge, Mentor Graphics Corporation.
5 * Some ideas based on un-pushed work done by Vivek Mahajan, Jason Jin, and
6 * Mingkai Hu from Freescale Semiconductor, Inc.
9 #include <linux/list.h>
10 #include <linux/of_platform.h>
11 #include <linux/errno.h>
12 #include <linux/err.h>
13 #include <linux/export.h>
14 #include <linux/slab.h>
16 #include <asm/hw_irq.h>
17 #include <asm/ppc-pci.h>
18 #include <asm/mpic_msgr.h>
20 #define MPIC_MSGR_REGISTERS_PER_BLOCK 4
21 #define MPIC_MSGR_STRIDE 0x10
22 #define MPIC_MSGR_MER_OFFSET 0x100
26 static struct mpic_msgr
**mpic_msgrs
;
27 static unsigned int mpic_msgr_count
;
28 static DEFINE_RAW_SPINLOCK(msgrs_lock
);
30 static inline void _mpic_msgr_mer_write(struct mpic_msgr
*msgr
, u32 value
)
32 out_be32(msgr
->mer
, value
);
35 static inline u32
_mpic_msgr_mer_read(struct mpic_msgr
*msgr
)
37 return in_be32(msgr
->mer
);
40 static inline void _mpic_msgr_disable(struct mpic_msgr
*msgr
)
42 u32 mer
= _mpic_msgr_mer_read(msgr
);
44 _mpic_msgr_mer_write(msgr
, mer
& ~(1 << msgr
->num
));
47 struct mpic_msgr
*mpic_msgr_get(unsigned int reg_num
)
50 struct mpic_msgr
*msgr
;
52 /* Assume busy until proven otherwise. */
53 msgr
= ERR_PTR(-EBUSY
);
55 if (reg_num
>= mpic_msgr_count
)
56 return ERR_PTR(-ENODEV
);
58 raw_spin_lock_irqsave(&msgrs_lock
, flags
);
59 msgr
= mpic_msgrs
[reg_num
];
60 if (msgr
->in_use
== MSGR_FREE
)
61 msgr
->in_use
= MSGR_INUSE
;
62 raw_spin_unlock_irqrestore(&msgrs_lock
, flags
);
66 EXPORT_SYMBOL_GPL(mpic_msgr_get
);
68 void mpic_msgr_put(struct mpic_msgr
*msgr
)
72 raw_spin_lock_irqsave(&msgr
->lock
, flags
);
73 msgr
->in_use
= MSGR_FREE
;
74 _mpic_msgr_disable(msgr
);
75 raw_spin_unlock_irqrestore(&msgr
->lock
, flags
);
77 EXPORT_SYMBOL_GPL(mpic_msgr_put
);
79 void mpic_msgr_enable(struct mpic_msgr
*msgr
)
84 raw_spin_lock_irqsave(&msgr
->lock
, flags
);
85 mer
= _mpic_msgr_mer_read(msgr
);
86 _mpic_msgr_mer_write(msgr
, mer
| (1 << msgr
->num
));
87 raw_spin_unlock_irqrestore(&msgr
->lock
, flags
);
89 EXPORT_SYMBOL_GPL(mpic_msgr_enable
);
91 void mpic_msgr_disable(struct mpic_msgr
*msgr
)
95 raw_spin_lock_irqsave(&msgr
->lock
, flags
);
96 _mpic_msgr_disable(msgr
);
97 raw_spin_unlock_irqrestore(&msgr
->lock
, flags
);
99 EXPORT_SYMBOL_GPL(mpic_msgr_disable
);
101 /* The following three functions are used to compute the order and number of
102 * the message register blocks. They are clearly very inefficent. However,
103 * they are called *only* a few times during device initialization.
105 static unsigned int mpic_msgr_number_of_blocks(void)
108 struct device_node
*aliases
;
111 aliases
= of_find_node_by_name(NULL
, "aliases");
117 snprintf(buf
, sizeof(buf
), "mpic-msgr-block%d", count
);
118 if (!of_find_property(aliases
, buf
, NULL
))
128 static unsigned int mpic_msgr_number_of_registers(void)
130 return mpic_msgr_number_of_blocks() * MPIC_MSGR_REGISTERS_PER_BLOCK
;
133 static int mpic_msgr_block_number(struct device_node
*node
)
135 struct device_node
*aliases
;
136 unsigned int index
, number_of_blocks
;
139 number_of_blocks
= mpic_msgr_number_of_blocks();
140 aliases
= of_find_node_by_name(NULL
, "aliases");
144 for (index
= 0; index
< number_of_blocks
; ++index
) {
145 struct property
*prop
;
147 snprintf(buf
, sizeof(buf
), "mpic-msgr-block%d", index
);
148 prop
= of_find_property(aliases
, buf
, NULL
);
149 if (node
== of_find_node_by_path(prop
->value
))
153 return index
== number_of_blocks
? -1 : index
;
156 /* The probe function for a single message register block.
158 static int mpic_msgr_probe(struct platform_device
*dev
)
160 void __iomem
*msgr_block_addr
;
162 struct resource rsrc
;
164 unsigned int irq_index
;
165 struct device_node
*np
= dev
->dev
.of_node
;
166 unsigned int receive_mask
;
167 const unsigned int *prop
;
170 dev_err(&dev
->dev
, "Device OF-Node is NULL");
174 /* Allocate the message register array upon the first device
178 mpic_msgr_count
= mpic_msgr_number_of_registers();
179 dev_info(&dev
->dev
, "Found %d message registers\n",
182 mpic_msgrs
= kcalloc(mpic_msgr_count
, sizeof(*mpic_msgrs
),
186 "No memory for message register blocks\n");
190 dev_info(&dev
->dev
, "Of-device full name %pOF\n", np
);
192 /* IO map the message register block. */
193 of_address_to_resource(np
, 0, &rsrc
);
194 msgr_block_addr
= devm_ioremap(&dev
->dev
, rsrc
.start
, resource_size(&rsrc
));
195 if (!msgr_block_addr
) {
196 dev_err(&dev
->dev
, "Failed to iomap MPIC message registers");
200 /* Ensure the block has a defined order. */
201 block_number
= mpic_msgr_block_number(np
);
202 if (block_number
< 0) {
204 "Failed to find message register block alias\n");
207 dev_info(&dev
->dev
, "Setting up message register block %d\n",
210 /* Grab the receive mask which specifies what registers can receive
213 prop
= of_get_property(np
, "mpic-msgr-receive-mask", NULL
);
214 receive_mask
= (prop
) ? *prop
: 0xF;
216 /* Build up the appropriate message register data structures. */
217 for (i
= 0, irq_index
= 0; i
< MPIC_MSGR_REGISTERS_PER_BLOCK
; ++i
) {
218 struct mpic_msgr
*msgr
;
219 unsigned int reg_number
;
221 msgr
= kzalloc(sizeof(struct mpic_msgr
), GFP_KERNEL
);
223 dev_err(&dev
->dev
, "No memory for message register\n");
227 reg_number
= block_number
* MPIC_MSGR_REGISTERS_PER_BLOCK
+ i
;
228 msgr
->base
= msgr_block_addr
+ i
* MPIC_MSGR_STRIDE
;
229 msgr
->mer
= (u32
*)((u8
*)msgr
->base
+ MPIC_MSGR_MER_OFFSET
);
230 msgr
->in_use
= MSGR_FREE
;
232 raw_spin_lock_init(&msgr
->lock
);
234 if (receive_mask
& (1 << i
)) {
235 msgr
->irq
= irq_of_parse_and_map(np
, irq_index
);
238 "Missing interrupt specifier");
247 mpic_msgrs
[reg_number
] = msgr
;
248 mpic_msgr_disable(msgr
);
249 dev_info(&dev
->dev
, "Register %d initialized: irq %d\n",
250 reg_number
, msgr
->irq
);
257 static const struct of_device_id mpic_msgr_ids
[] = {
259 .compatible
= "fsl,mpic-v3.1-msgr",
265 static struct platform_driver mpic_msgr_driver
= {
268 .of_match_table
= mpic_msgr_ids
,
270 .probe
= mpic_msgr_probe
,
273 static __init
int mpic_msgr_init(void)
275 return platform_driver_register(&mpic_msgr_driver
);
277 subsys_initcall(mpic_msgr_init
);