1 // SPDX-License-Identifier: GPL-2.0
3 * Turris Mox module configuration bus driver
5 * Copyright (C) 2019 Marek Behun <marek.behun@nic.cz>
8 #include <dt-bindings/bus/moxtet.h>
9 #include <linux/bitops.h>
10 #include <linux/debugfs.h>
11 #include <linux/interrupt.h>
12 #include <linux/module.h>
13 #include <linux/moxtet.h>
14 #include <linux/mutex.h>
15 #include <linux/of_device.h>
16 #include <linux/of_irq.h>
17 #include <linux/spi/spi.h>
20 * @name: module name for sysfs
21 * @hwirq_base: base index for IRQ for this module (-1 if no IRQs)
22 * @nirqs: how many interrupts does the shift register provide
23 * @desc: module description for kernel log
30 } mox_module_table
[] = {
31 /* do not change order of this array! */
33 { "sfp", -1, 0, "MOX D (SFP cage)" },
34 { "pci", MOXTET_IRQ_PCI
, 1, "MOX B (Mini-PCIe)" },
35 { "topaz", MOXTET_IRQ_TOPAZ
, 1, "MOX C (4 port switch)" },
36 { "peridot", MOXTET_IRQ_PERIDOT(0), 1, "MOX E (8 port switch)" },
37 { "usb3", MOXTET_IRQ_USB3
, 2, "MOX F (USB 3.0)" },
38 { "pci-bridge", -1, 0, "MOX G (Mini-PCIe bridge)" },
41 static inline bool mox_module_known(unsigned int id
)
43 return id
>= TURRIS_MOX_MODULE_FIRST
&& id
<= TURRIS_MOX_MODULE_LAST
;
46 static inline const char *mox_module_name(unsigned int id
)
48 if (mox_module_known(id
))
49 return mox_module_table
[id
].name
;
54 #define DEF_MODULE_ATTR(name, fmt, ...) \
56 module_##name##_show(struct device *dev, struct device_attribute *a, \
59 struct moxtet_device *mdev = to_moxtet_device(dev); \
60 return sprintf(buf, (fmt), __VA_ARGS__); \
62 static DEVICE_ATTR_RO(module_##name)
64 DEF_MODULE_ATTR(id
, "0x%x\n", mdev
->id
);
65 DEF_MODULE_ATTR(name
, "%s\n", mox_module_name(mdev
->id
));
66 DEF_MODULE_ATTR(description
, "%s\n",
67 mox_module_known(mdev
->id
) ? mox_module_table
[mdev
->id
].desc
70 static struct attribute
*moxtet_dev_attrs
[] = {
71 &dev_attr_module_id
.attr
,
72 &dev_attr_module_name
.attr
,
73 &dev_attr_module_description
.attr
,
77 static const struct attribute_group moxtet_dev_group
= {
78 .attrs
= moxtet_dev_attrs
,
81 static const struct attribute_group
*moxtet_dev_groups
[] = {
86 static int moxtet_match(struct device
*dev
, struct device_driver
*drv
)
88 struct moxtet_device
*mdev
= to_moxtet_device(dev
);
89 struct moxtet_driver
*tdrv
= to_moxtet_driver(drv
);
90 const enum turris_mox_module_id
*t
;
92 if (of_driver_match_device(dev
, drv
))
98 for (t
= tdrv
->id_table
; *t
; ++t
)
105 struct bus_type moxtet_bus_type
= {
107 .dev_groups
= moxtet_dev_groups
,
108 .match
= moxtet_match
,
110 EXPORT_SYMBOL_GPL(moxtet_bus_type
);
112 int __moxtet_register_driver(struct module
*owner
,
113 struct moxtet_driver
*mdrv
)
115 mdrv
->driver
.owner
= owner
;
116 mdrv
->driver
.bus
= &moxtet_bus_type
;
117 return driver_register(&mdrv
->driver
);
119 EXPORT_SYMBOL_GPL(__moxtet_register_driver
);
121 static int moxtet_dev_check(struct device
*dev
, void *data
)
123 struct moxtet_device
*mdev
= to_moxtet_device(dev
);
124 struct moxtet_device
*new_dev
= data
;
126 if (mdev
->moxtet
== new_dev
->moxtet
&& mdev
->id
== new_dev
->id
&&
127 mdev
->idx
== new_dev
->idx
)
132 static void moxtet_dev_release(struct device
*dev
)
134 struct moxtet_device
*mdev
= to_moxtet_device(dev
);
136 put_device(mdev
->moxtet
->dev
);
140 static struct moxtet_device
*
141 moxtet_alloc_device(struct moxtet
*moxtet
)
143 struct moxtet_device
*dev
;
145 if (!get_device(moxtet
->dev
))
148 dev
= kzalloc(sizeof(*dev
), GFP_KERNEL
);
150 put_device(moxtet
->dev
);
154 dev
->moxtet
= moxtet
;
155 dev
->dev
.parent
= moxtet
->dev
;
156 dev
->dev
.bus
= &moxtet_bus_type
;
157 dev
->dev
.release
= moxtet_dev_release
;
159 device_initialize(&dev
->dev
);
164 static int moxtet_add_device(struct moxtet_device
*dev
)
166 static DEFINE_MUTEX(add_mutex
);
169 if (dev
->idx
>= TURRIS_MOX_MAX_MODULES
|| dev
->id
> 0xf)
172 dev_set_name(&dev
->dev
, "moxtet-%s.%u", mox_module_name(dev
->id
),
175 mutex_lock(&add_mutex
);
177 ret
= bus_for_each_dev(&moxtet_bus_type
, NULL
, dev
,
182 ret
= device_add(&dev
->dev
);
184 dev_err(dev
->moxtet
->dev
, "can't add %s, status %d\n",
185 dev_name(dev
->moxtet
->dev
), ret
);
188 mutex_unlock(&add_mutex
);
192 static int __unregister(struct device
*dev
, void *null
)
195 of_node_clear_flag(dev
->of_node
, OF_POPULATED
);
196 of_node_put(dev
->of_node
);
199 device_unregister(dev
);
204 static struct moxtet_device
*
205 of_register_moxtet_device(struct moxtet
*moxtet
, struct device_node
*nc
)
207 struct moxtet_device
*dev
;
211 dev
= moxtet_alloc_device(moxtet
);
214 "Moxtet device alloc error for %pOF\n", nc
);
215 return ERR_PTR(-ENOMEM
);
218 ret
= of_property_read_u32(nc
, "reg", &val
);
220 dev_err(moxtet
->dev
, "%pOF has no valid 'reg' property (%d)\n",
227 if (dev
->idx
>= TURRIS_MOX_MAX_MODULES
) {
228 dev_err(moxtet
->dev
, "%pOF Moxtet address 0x%x out of range\n",
234 dev
->id
= moxtet
->modules
[dev
->idx
];
237 dev_err(moxtet
->dev
, "%pOF Moxtet address 0x%x is empty\n", nc
,
244 dev
->dev
.of_node
= nc
;
246 ret
= moxtet_add_device(dev
);
249 "Moxtet device register error for %pOF\n", nc
);
257 put_device(&dev
->dev
);
261 static void of_register_moxtet_devices(struct moxtet
*moxtet
)
263 struct moxtet_device
*dev
;
264 struct device_node
*nc
;
266 if (!moxtet
->dev
->of_node
)
269 for_each_available_child_of_node(moxtet
->dev
->of_node
, nc
) {
270 if (of_node_test_and_set_flag(nc
, OF_POPULATED
))
272 dev
= of_register_moxtet_device(moxtet
, nc
);
274 dev_warn(moxtet
->dev
,
275 "Failed to create Moxtet device for %pOF\n",
277 of_node_clear_flag(nc
, OF_POPULATED
);
283 moxtet_register_devices_from_topology(struct moxtet
*moxtet
)
285 struct moxtet_device
*dev
;
288 for (i
= 0; i
< moxtet
->count
; ++i
) {
289 dev
= moxtet_alloc_device(moxtet
);
291 dev_err(moxtet
->dev
, "Moxtet device %u alloc error\n",
297 dev
->id
= moxtet
->modules
[i
];
299 ret
= moxtet_add_device(dev
);
300 if (ret
&& ret
!= -EBUSY
) {
301 put_device(&dev
->dev
);
303 "Moxtet device %u register error: %i\n", i
,
310 * @nsame: how many modules with same id are already in moxtet->modules
312 static int moxtet_set_irq(struct moxtet
*moxtet
, int idx
, int id
, int nsame
)
315 struct moxtet_irqpos
*pos
;
317 first
= mox_module_table
[id
].hwirq_base
+
318 nsame
* mox_module_table
[id
].nirqs
;
320 if (first
+ mox_module_table
[id
].nirqs
> MOXTET_NIRQS
)
323 for (i
= 0; i
< mox_module_table
[id
].nirqs
; ++i
) {
324 pos
= &moxtet
->irq
.position
[first
+ i
];
327 moxtet
->irq
.exists
|= BIT(first
+ i
);
333 static int moxtet_find_topology(struct moxtet
*moxtet
)
335 u8 buf
[TURRIS_MOX_MAX_MODULES
];
336 int cnts
[TURRIS_MOX_MODULE_LAST
];
339 memset(cnts
, 0, sizeof(cnts
));
341 ret
= spi_read(to_spi_device(moxtet
->dev
), buf
, TURRIS_MOX_MAX_MODULES
);
345 if (buf
[0] == TURRIS_MOX_CPU_ID_EMMC
) {
346 dev_info(moxtet
->dev
, "Found MOX A (eMMC CPU) module\n");
347 } else if (buf
[0] == TURRIS_MOX_CPU_ID_SD
) {
348 dev_info(moxtet
->dev
, "Found MOX A (CPU) module\n");
350 dev_err(moxtet
->dev
, "Invalid Turris MOX A CPU module 0x%02x\n",
357 for (i
= 1; i
< TURRIS_MOX_MAX_MODULES
; ++i
) {
365 moxtet
->modules
[i
-1] = id
;
368 if (mox_module_known(id
)) {
369 dev_info(moxtet
->dev
, "Found %s module\n",
370 mox_module_table
[id
].desc
);
372 if (moxtet_set_irq(moxtet
, i
-1, id
, cnts
[id
]++) < 0)
374 " Cannot set IRQ for module %s\n",
375 mox_module_table
[id
].desc
);
377 dev_warn(moxtet
->dev
,
378 "Unknown Moxtet module found (ID 0x%02x)\n",
386 static int moxtet_spi_read(struct moxtet
*moxtet
, u8
*buf
)
388 struct spi_transfer xfer
= {
390 .tx_buf
= moxtet
->tx
,
391 .len
= moxtet
->count
+ 1
395 mutex_lock(&moxtet
->lock
);
397 ret
= spi_sync_transfer(to_spi_device(moxtet
->dev
), &xfer
, 1);
399 mutex_unlock(&moxtet
->lock
);
404 int moxtet_device_read(struct device
*dev
)
406 struct moxtet_device
*mdev
= to_moxtet_device(dev
);
407 struct moxtet
*moxtet
= mdev
->moxtet
;
408 u8 buf
[TURRIS_MOX_MAX_MODULES
];
411 if (mdev
->idx
>= moxtet
->count
)
414 ret
= moxtet_spi_read(moxtet
, buf
);
418 return buf
[mdev
->idx
+ 1] >> 4;
420 EXPORT_SYMBOL_GPL(moxtet_device_read
);
422 int moxtet_device_write(struct device
*dev
, u8 val
)
424 struct moxtet_device
*mdev
= to_moxtet_device(dev
);
425 struct moxtet
*moxtet
= mdev
->moxtet
;
428 if (mdev
->idx
>= moxtet
->count
)
431 mutex_lock(&moxtet
->lock
);
433 moxtet
->tx
[moxtet
->count
- mdev
->idx
] = val
;
435 ret
= spi_write(to_spi_device(moxtet
->dev
), moxtet
->tx
,
438 mutex_unlock(&moxtet
->lock
);
442 EXPORT_SYMBOL_GPL(moxtet_device_write
);
444 int moxtet_device_written(struct device
*dev
)
446 struct moxtet_device
*mdev
= to_moxtet_device(dev
);
447 struct moxtet
*moxtet
= mdev
->moxtet
;
449 if (mdev
->idx
>= moxtet
->count
)
452 return moxtet
->tx
[moxtet
->count
- mdev
->idx
];
454 EXPORT_SYMBOL_GPL(moxtet_device_written
);
456 #ifdef CONFIG_DEBUG_FS
457 static int moxtet_debug_open(struct inode
*inode
, struct file
*file
)
459 file
->private_data
= inode
->i_private
;
461 return nonseekable_open(inode
, file
);
464 static ssize_t
input_read(struct file
*file
, char __user
*buf
, size_t len
,
467 struct moxtet
*moxtet
= file
->private_data
;
468 u8 bin
[TURRIS_MOX_MAX_MODULES
];
469 u8 hex
[sizeof(buf
) * 2 + 1];
472 ret
= moxtet_spi_read(moxtet
, bin
);
476 n
= moxtet
->count
+ 1;
477 bin2hex(hex
, bin
, n
);
481 return simple_read_from_buffer(buf
, len
, ppos
, hex
, 2*n
+ 1);
484 static const struct file_operations input_fops
= {
485 .owner
= THIS_MODULE
,
486 .open
= moxtet_debug_open
,
491 static ssize_t
output_read(struct file
*file
, char __user
*buf
, size_t len
,
494 struct moxtet
*moxtet
= file
->private_data
;
495 u8 hex
[TURRIS_MOX_MAX_MODULES
* 2 + 1];
499 mutex_lock(&moxtet
->lock
);
501 for (i
= 0; i
< moxtet
->count
; ++i
)
502 p
= hex_byte_pack(p
, moxtet
->tx
[moxtet
->count
- i
]);
504 mutex_unlock(&moxtet
->lock
);
508 return simple_read_from_buffer(buf
, len
, ppos
, hex
, p
- hex
);
511 static ssize_t
output_write(struct file
*file
, const char __user
*buf
,
512 size_t len
, loff_t
*ppos
)
514 struct moxtet
*moxtet
= file
->private_data
;
515 u8 bin
[TURRIS_MOX_MAX_MODULES
];
516 u8 hex
[sizeof(bin
) * 2 + 1];
521 if (len
> 2 * moxtet
->count
+ 1 || len
< 2 * moxtet
->count
)
524 res
= simple_write_to_buffer(hex
, sizeof(hex
), &dummy
, buf
, len
);
528 if (len
% 2 == 1 && hex
[len
- 1] != '\n')
531 err
= hex2bin(bin
, hex
, moxtet
->count
);
535 mutex_lock(&moxtet
->lock
);
537 for (i
= 0; i
< moxtet
->count
; ++i
)
538 moxtet
->tx
[moxtet
->count
- i
] = bin
[i
];
540 err
= spi_write(to_spi_device(moxtet
->dev
), moxtet
->tx
,
543 mutex_unlock(&moxtet
->lock
);
545 return err
< 0 ? err
: len
;
548 static const struct file_operations output_fops
= {
549 .owner
= THIS_MODULE
,
550 .open
= moxtet_debug_open
,
552 .write
= output_write
,
556 static int moxtet_register_debugfs(struct moxtet
*moxtet
)
558 struct dentry
*root
, *entry
;
560 root
= debugfs_create_dir("moxtet", NULL
);
563 return PTR_ERR(root
);
565 entry
= debugfs_create_file_unsafe("input", 0444, root
, moxtet
,
570 entry
= debugfs_create_file_unsafe("output", 0644, root
, moxtet
,
575 moxtet
->debugfs_root
= root
;
579 debugfs_remove_recursive(root
);
580 return PTR_ERR(entry
);
583 static void moxtet_unregister_debugfs(struct moxtet
*moxtet
)
585 debugfs_remove_recursive(moxtet
->debugfs_root
);
588 static inline int moxtet_register_debugfs(struct moxtet
*moxtet
)
593 static inline void moxtet_unregister_debugfs(struct moxtet
*moxtet
)
598 static int moxtet_irq_domain_map(struct irq_domain
*d
, unsigned int irq
,
601 struct moxtet
*moxtet
= d
->host_data
;
603 if (hw
>= MOXTET_NIRQS
|| !(moxtet
->irq
.exists
& BIT(hw
))) {
604 dev_err(moxtet
->dev
, "Invalid hw irq number\n");
608 irq_set_chip_data(irq
, d
->host_data
);
609 irq_set_chip_and_handler(irq
, &moxtet
->irq
.chip
, handle_level_irq
);
614 static int moxtet_irq_domain_xlate(struct irq_domain
*d
,
615 struct device_node
*ctrlr
,
616 const u32
*intspec
, unsigned int intsize
,
617 unsigned long *out_hwirq
,
618 unsigned int *out_type
)
620 struct moxtet
*moxtet
= d
->host_data
;
623 if (WARN_ON(intsize
< 1))
628 if (irq
>= MOXTET_NIRQS
|| !(moxtet
->irq
.exists
& BIT(irq
)))
632 *out_type
= IRQ_TYPE_NONE
;
636 static const struct irq_domain_ops moxtet_irq_domain
= {
637 .map
= moxtet_irq_domain_map
,
638 .xlate
= moxtet_irq_domain_xlate
,
641 static void moxtet_irq_mask(struct irq_data
*d
)
643 struct moxtet
*moxtet
= irq_data_get_irq_chip_data(d
);
645 moxtet
->irq
.masked
|= BIT(d
->hwirq
);
648 static void moxtet_irq_unmask(struct irq_data
*d
)
650 struct moxtet
*moxtet
= irq_data_get_irq_chip_data(d
);
652 moxtet
->irq
.masked
&= ~BIT(d
->hwirq
);
655 static void moxtet_irq_print_chip(struct irq_data
*d
, struct seq_file
*p
)
657 struct moxtet
*moxtet
= irq_data_get_irq_chip_data(d
);
658 struct moxtet_irqpos
*pos
= &moxtet
->irq
.position
[d
->hwirq
];
661 id
= moxtet
->modules
[pos
->idx
];
663 seq_printf(p
, " moxtet-%s.%i#%i", mox_module_name(id
), pos
->idx
,
667 static const struct irq_chip moxtet_irq_chip
= {
669 .irq_mask
= moxtet_irq_mask
,
670 .irq_unmask
= moxtet_irq_unmask
,
671 .irq_print_chip
= moxtet_irq_print_chip
,
674 static int moxtet_irq_read(struct moxtet
*moxtet
, unsigned long *map
)
676 struct moxtet_irqpos
*pos
= moxtet
->irq
.position
;
677 u8 buf
[TURRIS_MOX_MAX_MODULES
];
680 ret
= moxtet_spi_read(moxtet
, buf
);
686 for_each_set_bit(i
, &moxtet
->irq
.exists
, MOXTET_NIRQS
) {
687 if (!(buf
[pos
[i
].idx
+ 1] & BIT(4 + pos
[i
].bit
)))
694 static irqreturn_t
moxtet_irq_thread_fn(int irq
, void *data
)
696 struct moxtet
*moxtet
= data
;
698 int nhandled
= 0, i
, sub_irq
, ret
;
700 ret
= moxtet_irq_read(moxtet
, &set
);
704 set
&= ~moxtet
->irq
.masked
;
707 for_each_set_bit(i
, &set
, MOXTET_NIRQS
) {
708 sub_irq
= irq_find_mapping(moxtet
->irq
.domain
, i
);
709 handle_nested_irq(sub_irq
);
710 dev_dbg(moxtet
->dev
, "%i irq\n", i
);
714 ret
= moxtet_irq_read(moxtet
, &set
);
718 set
&= ~moxtet
->irq
.masked
;
722 return (nhandled
> 0 ? IRQ_HANDLED
: IRQ_NONE
);
725 static void moxtet_irq_free(struct moxtet
*moxtet
)
729 for (i
= 0; i
< MOXTET_NIRQS
; ++i
) {
730 if (moxtet
->irq
.exists
& BIT(i
)) {
731 irq
= irq_find_mapping(moxtet
->irq
.domain
, i
);
732 irq_dispose_mapping(irq
);
736 irq_domain_remove(moxtet
->irq
.domain
);
739 static int moxtet_irq_setup(struct moxtet
*moxtet
)
743 moxtet
->irq
.domain
= irq_domain_add_simple(moxtet
->dev
->of_node
,
745 &moxtet_irq_domain
, moxtet
);
746 if (moxtet
->irq
.domain
== NULL
) {
747 dev_err(moxtet
->dev
, "Could not add IRQ domain\n");
751 for (i
= 0; i
< MOXTET_NIRQS
; ++i
)
752 if (moxtet
->irq
.exists
& BIT(i
))
753 irq_create_mapping(moxtet
->irq
.domain
, i
);
755 moxtet
->irq
.chip
= moxtet_irq_chip
;
756 moxtet
->irq
.masked
= ~0;
758 ret
= request_threaded_irq(moxtet
->dev_irq
, NULL
, moxtet_irq_thread_fn
,
759 IRQF_ONESHOT
, "moxtet", moxtet
);
766 moxtet_irq_free(moxtet
);
770 static int moxtet_probe(struct spi_device
*spi
)
772 struct moxtet
*moxtet
;
775 ret
= spi_setup(spi
);
779 moxtet
= devm_kzalloc(&spi
->dev
, sizeof(struct moxtet
),
784 moxtet
->dev
= &spi
->dev
;
785 spi_set_drvdata(spi
, moxtet
);
787 mutex_init(&moxtet
->lock
);
789 moxtet
->dev_irq
= of_irq_get(moxtet
->dev
->of_node
, 0);
790 if (moxtet
->dev_irq
== -EPROBE_DEFER
)
791 return -EPROBE_DEFER
;
793 if (moxtet
->dev_irq
<= 0) {
794 dev_err(moxtet
->dev
, "No IRQ resource found\n");
798 ret
= moxtet_find_topology(moxtet
);
802 if (moxtet
->irq
.exists
) {
803 ret
= moxtet_irq_setup(moxtet
);
808 of_register_moxtet_devices(moxtet
);
809 moxtet_register_devices_from_topology(moxtet
);
811 ret
= moxtet_register_debugfs(moxtet
);
813 dev_warn(moxtet
->dev
, "Failed creating debugfs entries: %i\n",
819 static int moxtet_remove(struct spi_device
*spi
)
821 struct moxtet
*moxtet
= spi_get_drvdata(spi
);
823 free_irq(moxtet
->dev_irq
, moxtet
);
825 moxtet_irq_free(moxtet
);
827 moxtet_unregister_debugfs(moxtet
);
829 device_for_each_child(moxtet
->dev
, NULL
, __unregister
);
831 mutex_destroy(&moxtet
->lock
);
836 static const struct of_device_id moxtet_dt_ids
[] = {
837 { .compatible
= "cznic,moxtet" },
840 MODULE_DEVICE_TABLE(of
, moxtet_dt_ids
);
842 static struct spi_driver moxtet_spi_driver
= {
845 .of_match_table
= moxtet_dt_ids
,
847 .probe
= moxtet_probe
,
848 .remove
= moxtet_remove
,
851 static int __init
moxtet_init(void)
855 ret
= bus_register(&moxtet_bus_type
);
857 pr_err("moxtet bus registration failed: %d\n", ret
);
861 ret
= spi_register_driver(&moxtet_spi_driver
);
863 pr_err("moxtet spi driver registration failed: %d\n", ret
);
870 bus_unregister(&moxtet_bus_type
);
874 postcore_initcall_sync(moxtet_init
);
876 static void __exit
moxtet_exit(void)
878 spi_unregister_driver(&moxtet_spi_driver
);
879 bus_unregister(&moxtet_bus_type
);
881 module_exit(moxtet_exit
);
883 MODULE_AUTHOR("Marek Behun <marek.behun@nic.cz>");
884 MODULE_DESCRIPTION("CZ.NIC's Turris Mox module configuration bus");
885 MODULE_LICENSE("GPL v2");