1 // SPDX-License-Identifier: GPL-2.0-only
2 /* uctrl.c: TS102 Microcontroller interface on Tadpole Sparcbook 3
4 * Copyright 1999 Derrick J Brashear (shadow@dementia.org)
5 * Copyright 2008 David S. Miller (davem@davemloft.net)
8 #include <linux/module.h>
9 #include <linux/errno.h>
10 #include <linux/delay.h>
11 #include <linux/interrupt.h>
12 #include <linux/slab.h>
13 #include <linux/mutex.h>
14 #include <linux/ioport.h>
15 #include <linux/miscdevice.h>
18 #include <linux/of_device.h>
20 #include <asm/openprom.h>
21 #include <asm/oplib.h>
27 #define dprintk(x) printk x
55 /* Bits for uctrl_intr register */
56 #define UCTRL_INTR_TXE_REQ 0x01 /* transmit FIFO empty int req */
57 #define UCTRL_INTR_TXNF_REQ 0x02 /* transmit FIFO not full int req */
58 #define UCTRL_INTR_RXNE_REQ 0x04 /* receive FIFO not empty int req */
59 #define UCTRL_INTR_RXO_REQ 0x08 /* receive FIFO overflow int req */
60 #define UCTRL_INTR_TXE_MSK 0x10 /* transmit FIFO empty mask */
61 #define UCTRL_INTR_TXNF_MSK 0x20 /* transmit FIFO not full mask */
62 #define UCTRL_INTR_RXNE_MSK 0x40 /* receive FIFO not empty mask */
63 #define UCTRL_INTR_RXO_MSK 0x80 /* receive FIFO overflow mask */
65 /* Bits for uctrl_stat register */
66 #define UCTRL_STAT_TXE_STA 0x01 /* transmit FIFO empty status */
67 #define UCTRL_STAT_TXNF_STA 0x02 /* transmit FIFO not full status */
68 #define UCTRL_STAT_RXNE_STA 0x04 /* receive FIFO not empty status */
69 #define UCTRL_STAT_RXO_STA 0x08 /* receive FIFO overflow status */
71 static DEFINE_MUTEX(uctrl_mutex
);
72 static const char *uctrl_extstatus
[16] = {
73 "main power available",
74 "internal battery attached",
75 "external battery attached",
76 "external VGA attached",
77 "external keyboard attached",
78 "external mouse attached",
80 "internal battery currently charging",
81 "external battery currently charging",
82 "internal battery currently discharging",
83 "external battery currently discharging",
86 /* Everything required for one transaction with the uctrl */
96 u8 current_temp
; /* 0x07 */
97 u8 reset_status
; /* 0x0b */
98 u16 event_status
; /* 0x0c */
99 u16 error_status
; /* 0x10 */
100 u16 external_status
; /* 0x11, 0x1b */
101 u8 internal_charge
; /* 0x18 */
102 u8 external_charge
; /* 0x19 */
103 u16 control_lcd
; /* 0x20 */
104 u8 control_bitport
; /* 0x21 */
105 u8 speaker_volume
; /* 0x23 */
106 u8 control_tft_brightness
; /* 0x24 */
107 u8 control_kbd_repeat_delay
; /* 0x28 */
108 u8 control_kbd_repeat_period
; /* 0x29 */
109 u8 control_screen_contrast
; /* 0x2F */
113 READ_SERIAL_NUMBER
=0x1,
114 READ_ETHERNET_ADDRESS
=0x2,
115 READ_HARDWARE_VERSION
=0x3,
116 READ_MICROCONTROLLER_VERSION
=0x4,
117 READ_MAX_TEMPERATURE
=0x5,
118 READ_MIN_TEMPERATURE
=0x6,
119 READ_CURRENT_TEMPERATURE
=0x7,
120 READ_SYSTEM_VARIANT
=0x8,
121 READ_POWERON_CYCLES
=0x9,
122 READ_POWERON_SECONDS
=0xA,
123 READ_RESET_STATUS
=0xB,
124 READ_EVENT_STATUS
=0xC,
125 READ_REAL_TIME_CLOCK
=0xD,
126 READ_EXTERNAL_VGA_PORT
=0xE,
127 READ_MICROCONTROLLER_ROM_CHECKSUM
=0xF,
128 READ_ERROR_STATUS
=0x10,
129 READ_EXTERNAL_STATUS
=0x11,
130 READ_USER_CONFIGURATION_AREA
=0x12,
131 READ_MICROCONTROLLER_VOLTAGE
=0x13,
132 READ_INTERNAL_BATTERY_VOLTAGE
=0x14,
133 READ_DCIN_VOLTAGE
=0x15,
134 READ_HORIZONTAL_POINTER_VOLTAGE
=0x16,
135 READ_VERTICAL_POINTER_VOLTAGE
=0x17,
136 READ_INTERNAL_BATTERY_CHARGE_LEVEL
=0x18,
137 READ_EXTERNAL_BATTERY_CHARGE_LEVEL
=0x19,
138 READ_REAL_TIME_CLOCK_ALARM
=0x1A,
139 READ_EVENT_STATUS_NO_RESET
=0x1B,
140 READ_INTERNAL_KEYBOARD_LAYOUT
=0x1C,
141 READ_EXTERNAL_KEYBOARD_LAYOUT
=0x1D,
142 READ_EEPROM_STATUS
=0x1E,
144 CONTROL_BITPORT
=0x21,
146 CONTROL_TFT_BRIGHTNESS
=0x24,
147 CONTROL_WATCHDOG
=0x25,
148 CONTROL_FACTORY_EEPROM_AREA
=0x26,
149 CONTROL_KBD_TIME_UNTIL_REPEAT
=0x28,
150 CONTROL_KBD_TIME_BETWEEN_REPEATS
=0x29,
151 CONTROL_TIMEZONE
=0x2A,
152 CONTROL_MARK_SPACE_RATIO
=0x2B,
153 CONTROL_DIAGNOSTIC_MODE
=0x2E,
154 CONTROL_SCREEN_CONTRAST
=0x2F,
156 SET_DIAGNOSTIC_STATUS
=0x32,
157 CLEAR_KEY_COMBINATION_TABLE
=0x33,
158 PERFORM_SOFTWARE_RESET
=0x34,
159 SET_REAL_TIME_CLOCK
=0x35,
160 RECALIBRATE_POINTING_STICK
=0x36,
161 SET_BELL_FREQUENCY
=0x37,
162 SET_INTERNAL_BATTERY_CHARGE_RATE
=0x39,
163 SET_EXTERNAL_BATTERY_CHARGE_RATE
=0x3A,
164 SET_REAL_TIME_CLOCK_ALARM
=0x3B,
167 WRITE_TO_STATUS_DISPLAY
=0x42,
168 DEFINE_SPECIAL_CHARACTER
=0x43,
169 DEFINE_KEY_COMBINATION_ENTRY
=0x50,
170 DEFINE_STRING_TABLE_ENTRY
=0x51,
171 DEFINE_STATUS_SCREEN_DISPLAY
=0x52,
172 PERFORM_EMU_COMMANDS
=0x64,
173 READ_EMU_REGISTER
=0x65,
174 WRITE_EMU_REGISTER
=0x66,
177 READ_BQ_REGISTER
=0x69,
178 WRITE_BQ_REGISTER
=0x6A,
179 SET_USER_PASSWORD
=0x70,
180 VERIFY_USER_PASSWORD
=0x71,
181 GET_SYSTEM_PASSWORD_KEY
=0x72,
182 VERIFY_SYSTEM_PASSWORD
=0x73,
187 static struct uctrl_driver
{
188 struct uctrl_regs __iomem
*regs
;
191 struct uctrl_status status
;
194 static void uctrl_get_event_status(struct uctrl_driver
*);
195 static void uctrl_get_external_status(struct uctrl_driver
*);
198 uctrl_ioctl(struct file
*file
, unsigned int cmd
, unsigned long arg
)
208 uctrl_open(struct inode
*inode
, struct file
*file
)
210 mutex_lock(&uctrl_mutex
);
211 uctrl_get_event_status(global_driver
);
212 uctrl_get_external_status(global_driver
);
213 mutex_unlock(&uctrl_mutex
);
217 static irqreturn_t
uctrl_interrupt(int irq
, void *dev_id
)
222 static const struct file_operations uctrl_fops
= {
223 .owner
= THIS_MODULE
,
225 .unlocked_ioctl
= uctrl_ioctl
,
229 static struct miscdevice uctrl_dev
= {
235 /* Wait for space to write, then write to it */
236 #define WRITEUCTLDATA(value) \
239 for (i = 0; i < 10000; i++) { \
240 if (UCTRL_STAT_TXNF_STA & sbus_readl(&driver->regs->uctrl_stat)) \
243 dprintk(("write data 0x%02x\n", value)); \
244 sbus_writel(value, &driver->regs->uctrl_data); \
247 /* Wait for something to read, read it, then clear the bit */
248 #define READUCTLDATA(value) \
252 for (i = 0; i < 10000; i++) { \
253 if ((UCTRL_STAT_RXNE_STA & sbus_readl(&driver->regs->uctrl_stat)) == 0) \
257 value = sbus_readl(&driver->regs->uctrl_data); \
258 dprintk(("read data 0x%02x\n", value)); \
259 sbus_writel(UCTRL_STAT_RXNE_STA, &driver->regs->uctrl_stat); \
262 static void uctrl_do_txn(struct uctrl_driver
*driver
, struct uctrl_txn
*txn
)
264 int stat
, incnt
, outcnt
, bytecnt
, intr
;
267 stat
= sbus_readl(&driver
->regs
->uctrl_stat
);
268 intr
= sbus_readl(&driver
->regs
->uctrl_intr
);
269 sbus_writel(stat
, &driver
->regs
->uctrl_stat
);
271 dprintk(("interrupt stat 0x%x int 0x%x\n", stat
, intr
));
274 outcnt
= txn
->outbits
;
275 byte
= (txn
->opcode
<< 8);
280 byte
= (txn
->inbuf
[bytecnt
] << 8);
288 dprintk(("ack was %x\n", (byte
>> 8)));
293 txn
->outbuf
[bytecnt
] = (byte
>> 8);
294 dprintk(("set byte to %02x\n", byte
));
300 static void uctrl_get_event_status(struct uctrl_driver
*driver
)
302 struct uctrl_txn txn
;
305 txn
.opcode
= READ_EVENT_STATUS
;
309 txn
.outbuf
= outbits
;
311 uctrl_do_txn(driver
, &txn
);
313 dprintk(("bytes %x %x\n", (outbits
[0] & 0xff), (outbits
[1] & 0xff)));
314 driver
->status
.event_status
=
315 ((outbits
[0] & 0xff) << 8) | (outbits
[1] & 0xff);
316 dprintk(("ev is %x\n", driver
->status
.event_status
));
319 static void uctrl_get_external_status(struct uctrl_driver
*driver
)
321 struct uctrl_txn txn
;
325 txn
.opcode
= READ_EXTERNAL_STATUS
;
329 txn
.outbuf
= outbits
;
331 uctrl_do_txn(driver
, &txn
);
333 dprintk(("bytes %x %x\n", (outbits
[0] & 0xff), (outbits
[1] & 0xff)));
334 driver
->status
.external_status
=
335 ((outbits
[0] * 256) + (outbits
[1]));
336 dprintk(("ex is %x\n", driver
->status
.external_status
));
337 v
= driver
->status
.external_status
;
338 for (i
= 0; v
!= 0; i
++, v
>>= 1) {
340 dprintk(("%s%s", " ", uctrl_extstatus
[i
]));
347 static int uctrl_probe(struct platform_device
*op
)
349 struct uctrl_driver
*p
;
352 p
= kzalloc(sizeof(*p
), GFP_KERNEL
);
354 printk(KERN_ERR
"uctrl: Unable to allocate device struct.\n");
358 p
->regs
= of_ioremap(&op
->resource
[0], 0,
359 resource_size(&op
->resource
[0]),
362 printk(KERN_ERR
"uctrl: Unable to map registers.\n");
366 p
->irq
= op
->archdata
.irqs
[0];
367 err
= request_irq(p
->irq
, uctrl_interrupt
, 0, "uctrl", p
);
369 printk(KERN_ERR
"uctrl: Unable to register irq.\n");
373 err
= misc_register(&uctrl_dev
);
375 printk(KERN_ERR
"uctrl: Unable to register misc device.\n");
379 sbus_writel(UCTRL_INTR_RXNE_REQ
|UCTRL_INTR_RXNE_MSK
, &p
->regs
->uctrl_intr
);
380 printk(KERN_INFO
"%pOF: uctrl regs[0x%p] (irq %d)\n",
381 op
->dev
.of_node
, p
->regs
, p
->irq
);
382 uctrl_get_event_status(p
);
383 uctrl_get_external_status(p
);
385 dev_set_drvdata(&op
->dev
, p
);
395 of_iounmap(&op
->resource
[0], p
->regs
, resource_size(&op
->resource
[0]));
402 static int uctrl_remove(struct platform_device
*op
)
404 struct uctrl_driver
*p
= dev_get_drvdata(&op
->dev
);
407 misc_deregister(&uctrl_dev
);
409 of_iounmap(&op
->resource
[0], p
->regs
, resource_size(&op
->resource
[0]));
415 static const struct of_device_id uctrl_match
[] = {
421 MODULE_DEVICE_TABLE(of
, uctrl_match
);
423 static struct platform_driver uctrl_driver
= {
426 .of_match_table
= uctrl_match
,
428 .probe
= uctrl_probe
,
429 .remove
= uctrl_remove
,
433 module_platform_driver(uctrl_driver
);
435 MODULE_LICENSE("GPL");