Finn Thain | 7f86c76 | 2018-01-13 17:37:14 -0500 | [diff] [blame] | 1 | // SPDX-License-Identifier: GPL-2.0 |
| 2 | // |
| 3 | // Bus implementation for the NuBus subsystem. |
| 4 | // |
| 5 | // Copyright (C) 2017 Finn Thain |
| 6 | |
| 7 | #include <linux/device.h> |
Finn Thain | a8c5cb9 | 2018-06-25 21:46:11 +1000 | [diff] [blame] | 8 | #include <linux/dma-mapping.h> |
Finn Thain | 7f86c76 | 2018-01-13 17:37:14 -0500 | [diff] [blame] | 9 | #include <linux/list.h> |
| 10 | #include <linux/nubus.h> |
| 11 | #include <linux/seq_file.h> |
| 12 | #include <linux/slab.h> |
| 13 | |
| 14 | #define to_nubus_board(d) container_of(d, struct nubus_board, dev) |
| 15 | #define to_nubus_driver(d) container_of(d, struct nubus_driver, driver) |
| 16 | |
Finn Thain | 7f86c76 | 2018-01-13 17:37:14 -0500 | [diff] [blame] | 17 | static int nubus_device_probe(struct device *dev) |
| 18 | { |
| 19 | struct nubus_driver *ndrv = to_nubus_driver(dev->driver); |
| 20 | int err = -ENODEV; |
| 21 | |
| 22 | if (ndrv->probe) |
| 23 | err = ndrv->probe(to_nubus_board(dev)); |
| 24 | return err; |
| 25 | } |
| 26 | |
Uwe Kleine-König | fc7a620 | 2021-07-13 21:35:22 +0200 | [diff] [blame] | 27 | static void nubus_device_remove(struct device *dev) |
Finn Thain | 7f86c76 | 2018-01-13 17:37:14 -0500 | [diff] [blame] | 28 | { |
| 29 | struct nubus_driver *ndrv = to_nubus_driver(dev->driver); |
Finn Thain | 7f86c76 | 2018-01-13 17:37:14 -0500 | [diff] [blame] | 30 | |
Uwe Kleine-König | f52c9cc | 2021-07-30 21:10:32 +0200 | [diff] [blame] | 31 | if (ndrv->remove) |
Uwe Kleine-König | fc7a620 | 2021-07-13 21:35:22 +0200 | [diff] [blame] | 32 | ndrv->remove(to_nubus_board(dev)); |
Finn Thain | 7f86c76 | 2018-01-13 17:37:14 -0500 | [diff] [blame] | 33 | } |
| 34 | |
| 35 | struct bus_type nubus_bus_type = { |
| 36 | .name = "nubus", |
Finn Thain | 7f86c76 | 2018-01-13 17:37:14 -0500 | [diff] [blame] | 37 | .probe = nubus_device_probe, |
| 38 | .remove = nubus_device_remove, |
| 39 | }; |
| 40 | EXPORT_SYMBOL(nubus_bus_type); |
| 41 | |
| 42 | int nubus_driver_register(struct nubus_driver *ndrv) |
| 43 | { |
| 44 | ndrv->driver.bus = &nubus_bus_type; |
| 45 | return driver_register(&ndrv->driver); |
| 46 | } |
| 47 | EXPORT_SYMBOL(nubus_driver_register); |
| 48 | |
| 49 | void nubus_driver_unregister(struct nubus_driver *ndrv) |
| 50 | { |
| 51 | driver_unregister(&ndrv->driver); |
| 52 | } |
| 53 | EXPORT_SYMBOL(nubus_driver_unregister); |
| 54 | |
| 55 | static struct device nubus_parent = { |
| 56 | .init_name = "nubus", |
| 57 | }; |
| 58 | |
Finn Thain | bdeeed0 | 2018-05-09 11:04:48 +1000 | [diff] [blame] | 59 | static int __init nubus_bus_register(void) |
Finn Thain | 7f86c76 | 2018-01-13 17:37:14 -0500 | [diff] [blame] | 60 | { |
Finn Thain | bdeeed0 | 2018-05-09 11:04:48 +1000 | [diff] [blame] | 61 | return bus_register(&nubus_bus_type); |
| 62 | } |
| 63 | postcore_initcall(nubus_bus_register); |
Finn Thain | 7f86c76 | 2018-01-13 17:37:14 -0500 | [diff] [blame] | 64 | |
Finn Thain | bdeeed0 | 2018-05-09 11:04:48 +1000 | [diff] [blame] | 65 | int __init nubus_parent_device_register(void) |
| 66 | { |
| 67 | return device_register(&nubus_parent); |
Finn Thain | 7f86c76 | 2018-01-13 17:37:14 -0500 | [diff] [blame] | 68 | } |
| 69 | |
| 70 | static void nubus_device_release(struct device *dev) |
| 71 | { |
| 72 | struct nubus_board *board = to_nubus_board(dev); |
| 73 | struct nubus_rsrc *fres, *tmp; |
| 74 | |
| 75 | list_for_each_entry_safe(fres, tmp, &nubus_func_rsrcs, list) |
| 76 | if (fres->board == board) { |
| 77 | list_del(&fres->list); |
| 78 | kfree(fres); |
| 79 | } |
| 80 | kfree(board); |
| 81 | } |
| 82 | |
| 83 | int nubus_device_register(struct nubus_board *board) |
| 84 | { |
| 85 | board->dev.parent = &nubus_parent; |
| 86 | board->dev.release = nubus_device_release; |
| 87 | board->dev.bus = &nubus_bus_type; |
| 88 | dev_set_name(&board->dev, "slot.%X", board->slot); |
Finn Thain | a8c5cb9 | 2018-06-25 21:46:11 +1000 | [diff] [blame] | 89 | board->dev.dma_mask = &board->dev.coherent_dma_mask; |
| 90 | dma_set_mask(&board->dev, DMA_BIT_MASK(32)); |
Finn Thain | 7f86c76 | 2018-01-13 17:37:14 -0500 | [diff] [blame] | 91 | return device_register(&board->dev); |
| 92 | } |
| 93 | |
| 94 | static int nubus_print_device_name_fn(struct device *dev, void *data) |
| 95 | { |
| 96 | struct nubus_board *board = to_nubus_board(dev); |
| 97 | struct seq_file *m = data; |
| 98 | |
| 99 | seq_printf(m, "Slot %X: %s\n", board->slot, board->name); |
| 100 | return 0; |
| 101 | } |
| 102 | |
| 103 | int nubus_proc_show(struct seq_file *m, void *data) |
| 104 | { |
| 105 | return bus_for_each_dev(&nubus_bus_type, NULL, m, |
| 106 | nubus_print_device_name_fn); |
| 107 | } |