18c2ecf20Sopenharmony_ci// SPDX-License-Identifier: GPL-2.0
28c2ecf20Sopenharmony_ci//
38c2ecf20Sopenharmony_ci// Bus implementation for the NuBus subsystem.
48c2ecf20Sopenharmony_ci//
58c2ecf20Sopenharmony_ci// Copyright (C) 2017 Finn Thain
68c2ecf20Sopenharmony_ci
78c2ecf20Sopenharmony_ci#include <linux/device.h>
88c2ecf20Sopenharmony_ci#include <linux/dma-mapping.h>
98c2ecf20Sopenharmony_ci#include <linux/list.h>
108c2ecf20Sopenharmony_ci#include <linux/nubus.h>
118c2ecf20Sopenharmony_ci#include <linux/seq_file.h>
128c2ecf20Sopenharmony_ci#include <linux/slab.h>
138c2ecf20Sopenharmony_ci
148c2ecf20Sopenharmony_ci#define to_nubus_board(d)       container_of(d, struct nubus_board, dev)
158c2ecf20Sopenharmony_ci#define to_nubus_driver(d)      container_of(d, struct nubus_driver, driver)
168c2ecf20Sopenharmony_ci
178c2ecf20Sopenharmony_cistatic int nubus_bus_match(struct device *dev, struct device_driver *driver)
188c2ecf20Sopenharmony_ci{
198c2ecf20Sopenharmony_ci	return 1;
208c2ecf20Sopenharmony_ci}
218c2ecf20Sopenharmony_ci
228c2ecf20Sopenharmony_cistatic int nubus_device_probe(struct device *dev)
238c2ecf20Sopenharmony_ci{
248c2ecf20Sopenharmony_ci	struct nubus_driver *ndrv = to_nubus_driver(dev->driver);
258c2ecf20Sopenharmony_ci	int err = -ENODEV;
268c2ecf20Sopenharmony_ci
278c2ecf20Sopenharmony_ci	if (ndrv->probe)
288c2ecf20Sopenharmony_ci		err = ndrv->probe(to_nubus_board(dev));
298c2ecf20Sopenharmony_ci	return err;
308c2ecf20Sopenharmony_ci}
318c2ecf20Sopenharmony_ci
328c2ecf20Sopenharmony_cistatic int nubus_device_remove(struct device *dev)
338c2ecf20Sopenharmony_ci{
348c2ecf20Sopenharmony_ci	struct nubus_driver *ndrv = to_nubus_driver(dev->driver);
358c2ecf20Sopenharmony_ci	int err = -ENODEV;
368c2ecf20Sopenharmony_ci
378c2ecf20Sopenharmony_ci	if (dev->driver && ndrv->remove)
388c2ecf20Sopenharmony_ci		err = ndrv->remove(to_nubus_board(dev));
398c2ecf20Sopenharmony_ci	return err;
408c2ecf20Sopenharmony_ci}
418c2ecf20Sopenharmony_ci
428c2ecf20Sopenharmony_cistruct bus_type nubus_bus_type = {
438c2ecf20Sopenharmony_ci	.name		= "nubus",
448c2ecf20Sopenharmony_ci	.match		= nubus_bus_match,
458c2ecf20Sopenharmony_ci	.probe		= nubus_device_probe,
468c2ecf20Sopenharmony_ci	.remove		= nubus_device_remove,
478c2ecf20Sopenharmony_ci};
488c2ecf20Sopenharmony_ciEXPORT_SYMBOL(nubus_bus_type);
498c2ecf20Sopenharmony_ci
508c2ecf20Sopenharmony_ciint nubus_driver_register(struct nubus_driver *ndrv)
518c2ecf20Sopenharmony_ci{
528c2ecf20Sopenharmony_ci	ndrv->driver.bus = &nubus_bus_type;
538c2ecf20Sopenharmony_ci	return driver_register(&ndrv->driver);
548c2ecf20Sopenharmony_ci}
558c2ecf20Sopenharmony_ciEXPORT_SYMBOL(nubus_driver_register);
568c2ecf20Sopenharmony_ci
578c2ecf20Sopenharmony_civoid nubus_driver_unregister(struct nubus_driver *ndrv)
588c2ecf20Sopenharmony_ci{
598c2ecf20Sopenharmony_ci	driver_unregister(&ndrv->driver);
608c2ecf20Sopenharmony_ci}
618c2ecf20Sopenharmony_ciEXPORT_SYMBOL(nubus_driver_unregister);
628c2ecf20Sopenharmony_ci
638c2ecf20Sopenharmony_cistatic struct device nubus_parent = {
648c2ecf20Sopenharmony_ci	.init_name	= "nubus",
658c2ecf20Sopenharmony_ci};
668c2ecf20Sopenharmony_ci
678c2ecf20Sopenharmony_cistatic int __init nubus_bus_register(void)
688c2ecf20Sopenharmony_ci{
698c2ecf20Sopenharmony_ci	return bus_register(&nubus_bus_type);
708c2ecf20Sopenharmony_ci}
718c2ecf20Sopenharmony_cipostcore_initcall(nubus_bus_register);
728c2ecf20Sopenharmony_ci
738c2ecf20Sopenharmony_ciint __init nubus_parent_device_register(void)
748c2ecf20Sopenharmony_ci{
758c2ecf20Sopenharmony_ci	return device_register(&nubus_parent);
768c2ecf20Sopenharmony_ci}
778c2ecf20Sopenharmony_ci
788c2ecf20Sopenharmony_cistatic void nubus_device_release(struct device *dev)
798c2ecf20Sopenharmony_ci{
808c2ecf20Sopenharmony_ci	struct nubus_board *board = to_nubus_board(dev);
818c2ecf20Sopenharmony_ci	struct nubus_rsrc *fres, *tmp;
828c2ecf20Sopenharmony_ci
838c2ecf20Sopenharmony_ci	list_for_each_entry_safe(fres, tmp, &nubus_func_rsrcs, list)
848c2ecf20Sopenharmony_ci		if (fres->board == board) {
858c2ecf20Sopenharmony_ci			list_del(&fres->list);
868c2ecf20Sopenharmony_ci			kfree(fres);
878c2ecf20Sopenharmony_ci		}
888c2ecf20Sopenharmony_ci	kfree(board);
898c2ecf20Sopenharmony_ci}
908c2ecf20Sopenharmony_ci
918c2ecf20Sopenharmony_ciint nubus_device_register(struct nubus_board *board)
928c2ecf20Sopenharmony_ci{
938c2ecf20Sopenharmony_ci	board->dev.parent = &nubus_parent;
948c2ecf20Sopenharmony_ci	board->dev.release = nubus_device_release;
958c2ecf20Sopenharmony_ci	board->dev.bus = &nubus_bus_type;
968c2ecf20Sopenharmony_ci	dev_set_name(&board->dev, "slot.%X", board->slot);
978c2ecf20Sopenharmony_ci	board->dev.dma_mask = &board->dev.coherent_dma_mask;
988c2ecf20Sopenharmony_ci	dma_set_mask(&board->dev, DMA_BIT_MASK(32));
998c2ecf20Sopenharmony_ci	return device_register(&board->dev);
1008c2ecf20Sopenharmony_ci}
1018c2ecf20Sopenharmony_ci
1028c2ecf20Sopenharmony_cistatic int nubus_print_device_name_fn(struct device *dev, void *data)
1038c2ecf20Sopenharmony_ci{
1048c2ecf20Sopenharmony_ci	struct nubus_board *board = to_nubus_board(dev);
1058c2ecf20Sopenharmony_ci	struct seq_file *m = data;
1068c2ecf20Sopenharmony_ci
1078c2ecf20Sopenharmony_ci	seq_printf(m, "Slot %X: %s\n", board->slot, board->name);
1088c2ecf20Sopenharmony_ci	return 0;
1098c2ecf20Sopenharmony_ci}
1108c2ecf20Sopenharmony_ci
1118c2ecf20Sopenharmony_ciint nubus_proc_show(struct seq_file *m, void *data)
1128c2ecf20Sopenharmony_ci{
1138c2ecf20Sopenharmony_ci	return bus_for_each_dev(&nubus_bus_type, NULL, m,
1148c2ecf20Sopenharmony_ci				nubus_print_device_name_fn);
1158c2ecf20Sopenharmony_ci}
116