1 // SPDX-License-Identifier: GPL-2.0-or-later
3 * Driver for TANBAC TB0219 base board.
5 * Copyright (C) 2005 Yoichi Yuasa <yuasa@linux-mips.org>
7 #include <linux/platform_device.h>
9 #include <linux/init.h>
10 #include <linux/module.h>
11 #include <linux/uaccess.h>
14 #include <asm/reboot.h>
15 #include <asm/vr41xx/giu.h>
16 #include <asm/vr41xx/tb0219.h>
18 MODULE_AUTHOR("Yoichi Yuasa <yuasa@linux-mips.org>");
19 MODULE_DESCRIPTION("TANBAC TB0219 base board driver");
20 MODULE_LICENSE("GPL");
22 static int major
; /* default is dynamic major device number */
23 module_param(major
, int, 0);
24 MODULE_PARM_DESC(major
, "Major device number");
26 static void (*old_machine_restart
)(char *command
);
27 static void __iomem
*tb0219_base
;
28 static DEFINE_SPINLOCK(tb0219_lock
);
30 #define tb0219_read(offset) readw(tb0219_base + (offset))
31 #define tb0219_write(offset, value) writew((value), tb0219_base + (offset))
33 #define TB0219_START 0x0a000000UL
34 #define TB0219_SIZE 0x20UL
36 #define TB0219_LED 0x00
37 #define TB0219_GPIO_INPUT 0x02
38 #define TB0219_GPIO_OUTPUT 0x04
39 #define TB0219_DIP_SWITCH 0x06
40 #define TB0219_MISC 0x08
41 #define TB0219_RESET 0x0e
42 #define TB0219_PCI_SLOT1_IRQ_STATUS 0x10
43 #define TB0219_PCI_SLOT2_IRQ_STATUS 0x12
44 #define TB0219_PCI_SLOT3_IRQ_STATUS 0x14
83 static inline char get_led(void)
85 return (char)tb0219_read(TB0219_LED
);
88 static inline char get_gpio_input_pin(unsigned int pin
)
92 values
= tb0219_read(TB0219_GPIO_INPUT
);
93 if (values
& (1 << pin
))
99 static inline char get_gpio_output_pin(unsigned int pin
)
103 values
= tb0219_read(TB0219_GPIO_OUTPUT
);
104 if (values
& (1 << pin
))
110 static inline char get_dip_switch(unsigned int pin
)
114 values
= tb0219_read(TB0219_DIP_SWITCH
);
115 if (values
& (1 << pin
))
121 static inline int set_led(char command
)
123 tb0219_write(TB0219_LED
, command
);
128 static inline int set_gpio_output_pin(unsigned int pin
, char command
)
133 if (command
!= '0' && command
!= '1')
136 spin_lock_irqsave(&tb0219_lock
, flags
);
137 value
= tb0219_read(TB0219_GPIO_OUTPUT
);
139 value
&= ~(1 << pin
);
142 tb0219_write(TB0219_GPIO_OUTPUT
, value
);
143 spin_unlock_irqrestore(&tb0219_lock
, flags
);
149 static ssize_t
tanbac_tb0219_read(struct file
*file
, char __user
*buf
, size_t len
,
155 minor
= iminor(file_inode(file
));
161 value
= get_gpio_input_pin(minor
- 16);
164 value
= get_gpio_output_pin(minor
- 32);
167 value
= get_dip_switch(minor
- 48);
176 if (put_user(value
, buf
))
182 static ssize_t
tanbac_tb0219_write(struct file
*file
, const char __user
*data
,
183 size_t len
, loff_t
*ppos
)
191 minor
= iminor(file_inode(file
));
197 type
= TYPE_GPIO_OUTPUT
;
203 for (i
= 0; i
< len
; i
++) {
204 if (get_user(c
, data
+ i
))
211 case TYPE_GPIO_OUTPUT
:
212 retval
= set_gpio_output_pin(minor
- 32, c
);
223 static int tanbac_tb0219_open(struct inode
*inode
, struct file
*file
)
227 minor
= iminor(inode
);
233 return stream_open(inode
, file
);
241 static int tanbac_tb0219_release(struct inode
*inode
, struct file
*file
)
246 static const struct file_operations tb0219_fops
= {
247 .owner
= THIS_MODULE
,
248 .read
= tanbac_tb0219_read
,
249 .write
= tanbac_tb0219_write
,
250 .open
= tanbac_tb0219_open
,
251 .release
= tanbac_tb0219_release
,
255 static void tb0219_restart(char *command
)
257 tb0219_write(TB0219_RESET
, 0);
260 static void tb0219_pci_irq_init(void)
263 vr41xx_set_irq_trigger(TB0219_PCI_SLOT1_PIN
, IRQ_TRIGGER_LEVEL
, IRQ_SIGNAL_THROUGH
);
264 vr41xx_set_irq_level(TB0219_PCI_SLOT1_PIN
, IRQ_LEVEL_LOW
);
267 vr41xx_set_irq_trigger(TB0219_PCI_SLOT2_PIN
, IRQ_TRIGGER_LEVEL
, IRQ_SIGNAL_THROUGH
);
268 vr41xx_set_irq_level(TB0219_PCI_SLOT2_PIN
, IRQ_LEVEL_LOW
);
271 vr41xx_set_irq_trigger(TB0219_PCI_SLOT3_PIN
, IRQ_TRIGGER_LEVEL
, IRQ_SIGNAL_THROUGH
);
272 vr41xx_set_irq_level(TB0219_PCI_SLOT3_PIN
, IRQ_LEVEL_LOW
);
275 static int tb0219_probe(struct platform_device
*dev
)
279 if (request_mem_region(TB0219_START
, TB0219_SIZE
, "TB0219") == NULL
)
282 tb0219_base
= ioremap(TB0219_START
, TB0219_SIZE
);
283 if (tb0219_base
== NULL
) {
284 release_mem_region(TB0219_START
, TB0219_SIZE
);
288 retval
= register_chrdev(major
, "TB0219", &tb0219_fops
);
290 iounmap(tb0219_base
);
292 release_mem_region(TB0219_START
, TB0219_SIZE
);
296 old_machine_restart
= _machine_restart
;
297 _machine_restart
= tb0219_restart
;
299 tb0219_pci_irq_init();
303 printk(KERN_INFO
"TB0219: major number %d\n", major
);
309 static int tb0219_remove(struct platform_device
*dev
)
311 _machine_restart
= old_machine_restart
;
313 iounmap(tb0219_base
);
316 release_mem_region(TB0219_START
, TB0219_SIZE
);
321 static struct platform_device
*tb0219_platform_device
;
323 static struct platform_driver tb0219_device_driver
= {
324 .probe
= tb0219_probe
,
325 .remove
= tb0219_remove
,
331 static int __init
tanbac_tb0219_init(void)
335 tb0219_platform_device
= platform_device_alloc("TB0219", -1);
336 if (!tb0219_platform_device
)
339 retval
= platform_device_add(tb0219_platform_device
);
341 platform_device_put(tb0219_platform_device
);
345 retval
= platform_driver_register(&tb0219_device_driver
);
347 platform_device_unregister(tb0219_platform_device
);
352 static void __exit
tanbac_tb0219_exit(void)
354 platform_driver_unregister(&tb0219_device_driver
);
355 platform_device_unregister(tb0219_platform_device
);
358 module_init(tanbac_tb0219_init
);
359 module_exit(tanbac_tb0219_exit
);