xref: /openbmc/linux/arch/powerpc/include/asm/macio.h (revision 81d7cac4)
1b2441318SGreg Kroah-Hartman /* SPDX-License-Identifier: GPL-2.0 */
2b8b572e1SStephen Rothwell #ifndef __MACIO_ASIC_H__
3b8b572e1SStephen Rothwell #define __MACIO_ASIC_H__
4b8b572e1SStephen Rothwell #ifdef __KERNEL__
5b8b572e1SStephen Rothwell 
6*81d7cac4SRob Herring #include <linux/of.h>
7*81d7cac4SRob Herring #include <linux/platform_device.h>
8b8b572e1SStephen Rothwell 
9b8b572e1SStephen Rothwell extern struct bus_type macio_bus_type;
10b8b572e1SStephen Rothwell 
11b8b572e1SStephen Rothwell /* MacIO device driver is defined later */
12b8b572e1SStephen Rothwell struct macio_driver;
13b8b572e1SStephen Rothwell struct macio_chip;
14b8b572e1SStephen Rothwell 
15b8b572e1SStephen Rothwell #define MACIO_DEV_COUNT_RESOURCES	8
16b8b572e1SStephen Rothwell #define MACIO_DEV_COUNT_IRQS		8
17b8b572e1SStephen Rothwell 
18b8b572e1SStephen Rothwell /*
19b8b572e1SStephen Rothwell  * the macio_bus structure is used to describe a "virtual" bus
20b8b572e1SStephen Rothwell  * within a MacIO ASIC. It's typically provided by a macio_pci_asic
21b8b572e1SStephen Rothwell  * PCI device, but could be provided differently as well (nubus
22b8b572e1SStephen Rothwell  * machines using a fake OF tree).
23b8b572e1SStephen Rothwell  *
24b8b572e1SStephen Rothwell  * The pdev field can be NULL on non-PCI machines
25b8b572e1SStephen Rothwell  */
26b8b572e1SStephen Rothwell struct macio_bus
27b8b572e1SStephen Rothwell {
28b8b572e1SStephen Rothwell 	struct macio_chip	*chip;		/* macio_chip (private use) */
29b8b572e1SStephen Rothwell 	int			index;		/* macio chip index in system */
30b8b572e1SStephen Rothwell #ifdef CONFIG_PCI
31b8b572e1SStephen Rothwell 	struct pci_dev		*pdev;		/* PCI device hosting this bus */
32b8b572e1SStephen Rothwell #endif
33b8b572e1SStephen Rothwell };
34b8b572e1SStephen Rothwell 
35b8b572e1SStephen Rothwell /*
36b8b572e1SStephen Rothwell  * the macio_dev structure is used to describe a device
37b8b572e1SStephen Rothwell  * within an Apple MacIO ASIC.
38b8b572e1SStephen Rothwell  */
39b8b572e1SStephen Rothwell struct macio_dev
40b8b572e1SStephen Rothwell {
41b8b572e1SStephen Rothwell 	struct macio_bus	*bus;		/* macio bus this device is on */
42b8b572e1SStephen Rothwell 	struct macio_dev	*media_bay;	/* Device is part of a media bay */
4394a0cb1fSGrant Likely 	struct platform_device	ofdev;
44128b4a0eSBenjamin Herrenschmidt 	struct device_dma_parameters dma_parms; /* ide needs that */
45b8b572e1SStephen Rothwell 	int			n_resources;
46b8b572e1SStephen Rothwell 	struct resource		resource[MACIO_DEV_COUNT_RESOURCES];
47b8b572e1SStephen Rothwell 	int			n_interrupts;
48b8b572e1SStephen Rothwell 	struct resource		interrupt[MACIO_DEV_COUNT_IRQS];
49b8b572e1SStephen Rothwell };
50b8b572e1SStephen Rothwell #define	to_macio_device(d) container_of(d, struct macio_dev, ofdev.dev)
51b8b572e1SStephen Rothwell #define	of_to_macio_device(d) container_of(d, struct macio_dev, ofdev)
52b8b572e1SStephen Rothwell 
53b8b572e1SStephen Rothwell extern struct macio_dev *macio_dev_get(struct macio_dev *dev);
54b8b572e1SStephen Rothwell extern void macio_dev_put(struct macio_dev *dev);
55b8b572e1SStephen Rothwell 
56b8b572e1SStephen Rothwell /*
57b8b572e1SStephen Rothwell  * Accessors to resources & interrupts and other device
58b8b572e1SStephen Rothwell  * fields
59b8b572e1SStephen Rothwell  */
60b8b572e1SStephen Rothwell 
macio_resource_count(struct macio_dev * dev)61b8b572e1SStephen Rothwell static inline int macio_resource_count(struct macio_dev *dev)
62b8b572e1SStephen Rothwell {
63b8b572e1SStephen Rothwell 	return dev->n_resources;
64b8b572e1SStephen Rothwell }
65b8b572e1SStephen Rothwell 
macio_resource_start(struct macio_dev * dev,int resource_no)66b8b572e1SStephen Rothwell static inline unsigned long macio_resource_start(struct macio_dev *dev, int resource_no)
67b8b572e1SStephen Rothwell {
68b8b572e1SStephen Rothwell 	return dev->resource[resource_no].start;
69b8b572e1SStephen Rothwell }
70b8b572e1SStephen Rothwell 
macio_resource_end(struct macio_dev * dev,int resource_no)71b8b572e1SStephen Rothwell static inline unsigned long macio_resource_end(struct macio_dev *dev, int resource_no)
72b8b572e1SStephen Rothwell {
73b8b572e1SStephen Rothwell 	return dev->resource[resource_no].end;
74b8b572e1SStephen Rothwell }
75b8b572e1SStephen Rothwell 
macio_resource_len(struct macio_dev * dev,int resource_no)76b8b572e1SStephen Rothwell static inline unsigned long macio_resource_len(struct macio_dev *dev, int resource_no)
77b8b572e1SStephen Rothwell {
78b8b572e1SStephen Rothwell 	struct resource *res = &dev->resource[resource_no];
79b8b572e1SStephen Rothwell 	if (res->start == 0 || res->end == 0 || res->end < res->start)
80b8b572e1SStephen Rothwell 		return 0;
8128f65c11SJoe Perches 	return resource_size(res);
82b8b572e1SStephen Rothwell }
83b8b572e1SStephen Rothwell 
847fb19ea0SBenjamin Herrenschmidt extern int macio_enable_devres(struct macio_dev *dev);
857fb19ea0SBenjamin Herrenschmidt 
86b8b572e1SStephen Rothwell extern int macio_request_resource(struct macio_dev *dev, int resource_no, const char *name);
87b8b572e1SStephen Rothwell extern void macio_release_resource(struct macio_dev *dev, int resource_no);
88b8b572e1SStephen Rothwell extern int macio_request_resources(struct macio_dev *dev, const char *name);
89b8b572e1SStephen Rothwell extern void macio_release_resources(struct macio_dev *dev);
90b8b572e1SStephen Rothwell 
macio_irq_count(struct macio_dev * dev)91b8b572e1SStephen Rothwell static inline int macio_irq_count(struct macio_dev *dev)
92b8b572e1SStephen Rothwell {
93b8b572e1SStephen Rothwell 	return dev->n_interrupts;
94b8b572e1SStephen Rothwell }
95b8b572e1SStephen Rothwell 
macio_irq(struct macio_dev * dev,int irq_no)96b8b572e1SStephen Rothwell static inline int macio_irq(struct macio_dev *dev, int irq_no)
97b8b572e1SStephen Rothwell {
98b8b572e1SStephen Rothwell 	return dev->interrupt[irq_no].start;
99b8b572e1SStephen Rothwell }
100b8b572e1SStephen Rothwell 
macio_set_drvdata(struct macio_dev * dev,void * data)101b8b572e1SStephen Rothwell static inline void macio_set_drvdata(struct macio_dev *dev, void *data)
102b8b572e1SStephen Rothwell {
103b8b572e1SStephen Rothwell 	dev_set_drvdata(&dev->ofdev.dev, data);
104b8b572e1SStephen Rothwell }
105b8b572e1SStephen Rothwell 
macio_get_drvdata(struct macio_dev * dev)106b8b572e1SStephen Rothwell static inline void* macio_get_drvdata(struct macio_dev *dev)
107b8b572e1SStephen Rothwell {
108b8b572e1SStephen Rothwell 	return dev_get_drvdata(&dev->ofdev.dev);
109b8b572e1SStephen Rothwell }
110b8b572e1SStephen Rothwell 
macio_get_of_node(struct macio_dev * mdev)111b8b572e1SStephen Rothwell static inline struct device_node *macio_get_of_node(struct macio_dev *mdev)
112b8b572e1SStephen Rothwell {
11361c7a080SGrant Likely 	return mdev->ofdev.dev.of_node;
114b8b572e1SStephen Rothwell }
115b8b572e1SStephen Rothwell 
116b8b572e1SStephen Rothwell #ifdef CONFIG_PCI
macio_get_pci_dev(struct macio_dev * mdev)117b8b572e1SStephen Rothwell static inline struct pci_dev *macio_get_pci_dev(struct macio_dev *mdev)
118b8b572e1SStephen Rothwell {
119b8b572e1SStephen Rothwell 	return mdev->bus->pdev;
120b8b572e1SStephen Rothwell }
121b8b572e1SStephen Rothwell #endif
122b8b572e1SStephen Rothwell 
123b8b572e1SStephen Rothwell /*
124b8b572e1SStephen Rothwell  * A driver for a mac-io chip based device
125b8b572e1SStephen Rothwell  */
126b8b572e1SStephen Rothwell struct macio_driver
127b8b572e1SStephen Rothwell {
128b8b572e1SStephen Rothwell 	int	(*probe)(struct macio_dev* dev, const struct of_device_id *match);
129b8b572e1SStephen Rothwell 	int	(*remove)(struct macio_dev* dev);
130b8b572e1SStephen Rothwell 
131b8b572e1SStephen Rothwell 	int	(*suspend)(struct macio_dev* dev, pm_message_t state);
132b8b572e1SStephen Rothwell 	int	(*resume)(struct macio_dev* dev);
133b8b572e1SStephen Rothwell 	int	(*shutdown)(struct macio_dev* dev);
134b8b572e1SStephen Rothwell 
135d58b0c39SBenjamin Herrenschmidt #ifdef CONFIG_PMAC_MEDIABAY
136d58b0c39SBenjamin Herrenschmidt 	void	(*mediabay_event)(struct macio_dev* dev, int mb_state);
137d58b0c39SBenjamin Herrenschmidt #endif
138b8b572e1SStephen Rothwell 	struct device_driver	driver;
139b8b572e1SStephen Rothwell };
140b8b572e1SStephen Rothwell #define	to_macio_driver(drv) container_of(drv,struct macio_driver, driver)
141b8b572e1SStephen Rothwell 
142b8b572e1SStephen Rothwell extern int macio_register_driver(struct macio_driver *);
143b8b572e1SStephen Rothwell extern void macio_unregister_driver(struct macio_driver *);
144b8b572e1SStephen Rothwell 
145b8b572e1SStephen Rothwell #endif /* __KERNEL__ */
146b8b572e1SStephen Rothwell #endif /* __MACIO_ASIC_H__ */
147