1 // SPDX-License-Identifier: GPL-2.0-only
3 * Compaq iPAQ h3xxx Atmel microcontroller companion support
5 * This is an Atmel AT90LS8535 with a special flashed-in firmware that
6 * implements the special protocol used by this driver.
8 * based on previous kernel 2.4 version by Andrew Christian
9 * Author : Alessandro Gardich <gremlin@gremlin.it>
10 * Author : Dmitry Artamonow <mad_soft@inbox.ru>
11 * Author : Linus Walleij <linus.walleij@linaro.org>
14 #include <linux/module.h>
15 #include <linux/init.h>
16 #include <linux/interrupt.h>
18 #include <linux/delay.h>
19 #include <linux/device.h>
20 #include <linux/platform_device.h>
22 #include <linux/mfd/core.h>
23 #include <linux/mfd/ipaq-micro.h>
24 #include <linux/string.h>
25 #include <linux/random.h>
26 #include <linux/slab.h>
27 #include <linux/list.h>
29 #include <mach/hardware.h>
31 static void ipaq_micro_trigger_tx(struct ipaq_micro
*micro
)
33 struct ipaq_micro_txdev
*tx
= µ
->tx
;
34 struct ipaq_micro_msg
*msg
= micro
->msg
;
40 tx
->buf
[bp
++] = CHAR_SOF
;
42 checksum
= ((msg
->id
& 0x0f) << 4) | (msg
->tx_len
& 0x0f);
43 tx
->buf
[bp
++] = checksum
;
45 for (i
= 0; i
< msg
->tx_len
; i
++) {
46 tx
->buf
[bp
++] = msg
->tx_data
[i
];
47 checksum
+= msg
->tx_data
[i
];
50 tx
->buf
[bp
++] = checksum
;
54 /* Enable interrupt */
55 val
= readl(micro
->base
+ UTCR3
);
57 writel(val
, micro
->base
+ UTCR3
);
60 int ipaq_micro_tx_msg(struct ipaq_micro
*micro
, struct ipaq_micro_msg
*msg
)
64 dev_dbg(micro
->dev
, "TX msg: %02x, %d bytes\n", msg
->id
, msg
->tx_len
);
66 spin_lock_irqsave(µ
->lock
, flags
);
68 list_add_tail(&msg
->node
, µ
->queue
);
69 spin_unlock_irqrestore(µ
->lock
, flags
);
73 ipaq_micro_trigger_tx(micro
);
74 spin_unlock_irqrestore(µ
->lock
, flags
);
77 EXPORT_SYMBOL(ipaq_micro_tx_msg
);
79 static void micro_rx_msg(struct ipaq_micro
*micro
, u8 id
, int len
, u8
*data
)
83 dev_dbg(micro
->dev
, "RX msg: %02x, %d bytes\n", id
, len
);
85 spin_lock(µ
->lock
);
89 case MSG_EEPROM_WRITE
:
92 case MSG_THERMAL_SENSOR
:
94 /* Handle synchronous messages */
95 if (micro
->msg
&& micro
->msg
->id
== id
) {
96 struct ipaq_micro_msg
*msg
= micro
->msg
;
98 memcpy(msg
->rx_data
, data
, len
);
100 complete(µ
->msg
->ack
);
101 if (!list_empty(µ
->queue
)) {
102 micro
->msg
= list_entry(micro
->queue
.next
,
103 struct ipaq_micro_msg
,
105 list_del_init(µ
->msg
->node
);
106 ipaq_micro_trigger_tx(micro
);
109 dev_dbg(micro
->dev
, "OK RX message 0x%02x\n", id
);
112 "out of band RX message 0x%02x\n", id
);
114 dev_info(micro
->dev
, "no message queued\n");
116 dev_info(micro
->dev
, "expected message %02x\n",
122 micro
->key(micro
->key_data
, len
, data
);
124 dev_dbg(micro
->dev
, "key message ignored, no handle\n");
126 case MSG_TOUCHSCREEN
:
128 micro
->ts(micro
->ts_data
, len
, data
);
130 dev_dbg(micro
->dev
, "touchscreen message ignored, no handle\n");
134 "unknown msg %d [%d] ", id
, len
);
135 for (i
= 0; i
< len
; ++i
)
136 pr_cont("0x%02x ", data
[i
]);
139 spin_unlock(µ
->lock
);
142 static void micro_process_char(struct ipaq_micro
*micro
, u8 ch
)
144 struct ipaq_micro_rxdev
*rx
= µ
->rx
;
147 case STATE_SOF
: /* Looking for SOF */
149 rx
->state
= STATE_ID
; /* Next byte is the id and len */
151 case STATE_ID
: /* Looking for id and len byte */
152 rx
->id
= (ch
& 0xf0) >> 4;
153 rx
->len
= (ch
& 0x0f);
156 rx
->state
= (rx
->len
> 0) ? STATE_DATA
: STATE_CHKSUM
;
158 case STATE_DATA
: /* Looking for 'len' data bytes */
160 rx
->buf
[rx
->index
] = ch
;
161 if (++rx
->index
== rx
->len
)
162 rx
->state
= STATE_CHKSUM
;
164 case STATE_CHKSUM
: /* Looking for the checksum */
165 if (ch
== rx
->chksum
)
166 micro_rx_msg(micro
, rx
->id
, rx
->len
, rx
->buf
);
167 rx
->state
= STATE_SOF
;
172 static void micro_rx_chars(struct ipaq_micro
*micro
)
176 while ((status
= readl(micro
->base
+ UTSR1
)) & UTSR1_RNE
) {
177 ch
= readl(micro
->base
+ UTDR
);
178 if (status
& UTSR1_PRE
)
179 dev_err(micro
->dev
, "rx: parity error\n");
180 else if (status
& UTSR1_FRE
)
181 dev_err(micro
->dev
, "rx: framing error\n");
182 else if (status
& UTSR1_ROR
)
183 dev_err(micro
->dev
, "rx: overrun error\n");
184 micro_process_char(micro
, ch
);
188 static void ipaq_micro_get_version(struct ipaq_micro
*micro
)
190 struct ipaq_micro_msg msg
= {
194 ipaq_micro_tx_msg_sync(micro
, &msg
);
195 if (msg
.rx_len
== 4) {
196 memcpy(micro
->version
, msg
.rx_data
, 4);
197 micro
->version
[4] = '\0';
198 } else if (msg
.rx_len
== 9) {
199 memcpy(micro
->version
, msg
.rx_data
, 4);
200 micro
->version
[4] = '\0';
201 /* Bytes 4-7 are "pack", byte 8 is "boot type" */
204 "illegal version message %d bytes\n", msg
.rx_len
);
208 static void ipaq_micro_eeprom_read(struct ipaq_micro
*micro
,
209 u8 address
, u8 len
, u8
*data
)
211 struct ipaq_micro_msg msg
= {
212 .id
= MSG_EEPROM_READ
,
216 for (i
= 0; i
< len
; i
++) {
217 msg
.tx_data
[0] = address
+ i
;
220 ipaq_micro_tx_msg_sync(micro
, &msg
);
221 memcpy(data
+ (i
* 2), msg
.rx_data
, 2);
225 static char *ipaq_micro_str(u8
*wchar
, u8 len
)
230 for (i
= 0; i
< len
/ 2; i
++)
231 retstr
[i
] = wchar
[i
* 2];
232 return kstrdup(retstr
, GFP_KERNEL
);
235 static u16
ipaq_micro_to_u16(u8
*data
)
237 return data
[1] << 8 | data
[0];
240 static void __init
ipaq_micro_eeprom_dump(struct ipaq_micro
*micro
)
245 ipaq_micro_eeprom_read(micro
, 0, 128, dump
);
246 str
= ipaq_micro_str(dump
, 10);
248 dev_info(micro
->dev
, "HW version %s\n", str
);
251 str
= ipaq_micro_str(dump
+10, 40);
253 dev_info(micro
->dev
, "serial number: %s\n", str
);
254 /* Feed the random pool with this */
255 add_device_randomness(str
, strlen(str
));
258 str
= ipaq_micro_str(dump
+50, 20);
260 dev_info(micro
->dev
, "module ID: %s\n", str
);
263 str
= ipaq_micro_str(dump
+70, 10);
265 dev_info(micro
->dev
, "product revision: %s\n", str
);
268 dev_info(micro
->dev
, "product ID: %u\n", ipaq_micro_to_u16(dump
+80));
269 dev_info(micro
->dev
, "frame rate: %u fps\n",
270 ipaq_micro_to_u16(dump
+82));
271 dev_info(micro
->dev
, "page mode: %u\n", ipaq_micro_to_u16(dump
+84));
272 dev_info(micro
->dev
, "country ID: %u\n", ipaq_micro_to_u16(dump
+86));
273 dev_info(micro
->dev
, "color display: %s\n",
274 ipaq_micro_to_u16(dump
+88) ? "yes" : "no");
275 dev_info(micro
->dev
, "ROM size: %u MiB\n", ipaq_micro_to_u16(dump
+90));
276 dev_info(micro
->dev
, "RAM size: %u KiB\n", ipaq_micro_to_u16(dump
+92));
277 dev_info(micro
->dev
, "screen: %u x %u\n",
278 ipaq_micro_to_u16(dump
+94), ipaq_micro_to_u16(dump
+96));
281 static void micro_tx_chars(struct ipaq_micro
*micro
)
283 struct ipaq_micro_txdev
*tx
= µ
->tx
;
286 while ((tx
->index
< tx
->len
) &&
287 (readl(micro
->base
+ UTSR1
) & UTSR1_TNF
)) {
288 writel(tx
->buf
[tx
->index
], micro
->base
+ UTDR
);
292 /* Stop interrupts */
293 val
= readl(micro
->base
+ UTCR3
);
295 writel(val
, micro
->base
+ UTCR3
);
298 static void micro_reset_comm(struct ipaq_micro
*micro
)
300 struct ipaq_micro_rxdev
*rx
= µ
->rx
;
304 complete(µ
->msg
->ack
);
306 /* Initialize Serial channel protocol frame */
307 rx
->state
= STATE_SOF
; /* Reset the state machine */
309 /* Set up interrupts */
310 writel(0x01, micro
->sdlc
+ 0x0); /* Select UART mode */
313 writel(0x0, micro
->base
+ UTCR3
);
316 writel(UTCR0_8BitData
| UTCR0_1StpBit
, micro
->base
+ UTCR0
);
318 /* Baud rate: 115200 */
319 writel(0x0, micro
->base
+ UTCR1
);
320 writel(0x1, micro
->base
+ UTCR2
);
323 writel(0xff, micro
->base
+ UTSR0
);
325 /* Enable RX int, disable TX int */
326 writel(UTCR3_TXE
| UTCR3_RXE
| UTCR3_RIE
, micro
->base
+ UTCR3
);
327 val
= readl(micro
->base
+ UTCR3
);
329 writel(val
, micro
->base
+ UTCR3
);
332 static irqreturn_t
micro_serial_isr(int irq
, void *dev_id
)
334 struct ipaq_micro
*micro
= dev_id
;
335 struct ipaq_micro_txdev
*tx
= µ
->tx
;
338 status
= readl(micro
->base
+ UTSR0
);
340 if (status
& (UTSR0_RID
| UTSR0_RFS
)) {
341 if (status
& UTSR0_RID
)
342 /* Clear the Receiver IDLE bit */
343 writel(UTSR0_RID
, micro
->base
+ UTSR0
);
344 micro_rx_chars(micro
);
347 /* Clear break bits */
348 if (status
& (UTSR0_RBB
| UTSR0_REB
))
349 writel(status
& (UTSR0_RBB
| UTSR0_REB
),
350 micro
->base
+ UTSR0
);
352 if (status
& UTSR0_TFS
)
353 micro_tx_chars(micro
);
355 status
= readl(micro
->base
+ UTSR0
);
357 } while (((tx
->index
< tx
->len
) && (status
& UTSR0_TFS
)) ||
358 (status
& (UTSR0_RFS
| UTSR0_RID
)));
363 static const struct mfd_cell micro_cells
[] = {
364 { .name
= "ipaq-micro-backlight", },
365 { .name
= "ipaq-micro-battery", },
366 { .name
= "ipaq-micro-keys", },
367 { .name
= "ipaq-micro-ts", },
368 { .name
= "ipaq-micro-leds", },
371 static int __maybe_unused
micro_resume(struct device
*dev
)
373 struct ipaq_micro
*micro
= dev_get_drvdata(dev
);
375 micro_reset_comm(micro
);
381 static int __init
micro_probe(struct platform_device
*pdev
)
383 struct ipaq_micro
*micro
;
384 struct resource
*res
;
388 micro
= devm_kzalloc(&pdev
->dev
, sizeof(*micro
), GFP_KERNEL
);
392 micro
->dev
= &pdev
->dev
;
394 res
= platform_get_resource(pdev
, IORESOURCE_MEM
, 0);
395 micro
->base
= devm_ioremap_resource(&pdev
->dev
, res
);
396 if (IS_ERR(micro
->base
))
397 return PTR_ERR(micro
->base
);
399 micro
->sdlc
= devm_platform_ioremap_resource(pdev
, 1);
400 if (IS_ERR(micro
->sdlc
))
401 return PTR_ERR(micro
->sdlc
);
403 micro_reset_comm(micro
);
405 irq
= platform_get_irq(pdev
, 0);
408 ret
= devm_request_irq(&pdev
->dev
, irq
, micro_serial_isr
,
409 IRQF_SHARED
, "ipaq-micro",
412 dev_err(&pdev
->dev
, "unable to grab serial port IRQ\n");
415 dev_info(&pdev
->dev
, "grabbed serial port IRQ\n");
417 spin_lock_init(µ
->lock
);
418 INIT_LIST_HEAD(µ
->queue
);
419 platform_set_drvdata(pdev
, micro
);
421 ret
= mfd_add_devices(&pdev
->dev
, pdev
->id
, micro_cells
,
422 ARRAY_SIZE(micro_cells
), NULL
, 0, NULL
);
424 dev_err(&pdev
->dev
, "error adding MFD cells");
429 ipaq_micro_get_version(micro
);
430 dev_info(&pdev
->dev
, "Atmel micro ASIC version %s\n", micro
->version
);
431 ipaq_micro_eeprom_dump(micro
);
436 static const struct dev_pm_ops micro_dev_pm_ops
= {
437 SET_SYSTEM_SLEEP_PM_OPS(NULL
, micro_resume
)
440 static struct platform_driver micro_device_driver
= {
442 .name
= "ipaq-h3xxx-micro",
443 .pm
= µ_dev_pm_ops
,
444 .suppress_bind_attrs
= true,
447 builtin_platform_driver_probe(micro_device_driver
, micro_probe
);