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
)
81 dev_dbg(micro
->dev
, "RX msg: %02x, %d bytes\n", id
, len
);
83 spin_lock(µ
->lock
);
87 case MSG_EEPROM_WRITE
:
90 case MSG_THERMAL_SENSOR
:
92 /* Handle synchronous messages */
93 if (micro
->msg
&& micro
->msg
->id
== id
) {
94 struct ipaq_micro_msg
*msg
= micro
->msg
;
96 memcpy(msg
->rx_data
, data
, len
);
98 complete(µ
->msg
->ack
);
99 if (!list_empty(µ
->queue
)) {
100 micro
->msg
= list_entry(micro
->queue
.next
,
101 struct ipaq_micro_msg
,
103 list_del_init(µ
->msg
->node
);
104 ipaq_micro_trigger_tx(micro
);
107 dev_dbg(micro
->dev
, "OK RX message 0x%02x\n", id
);
110 "out of band RX message 0x%02x\n", id
);
112 dev_info(micro
->dev
, "no message queued\n");
114 dev_info(micro
->dev
, "expected message %02x\n",
120 micro
->key(micro
->key_data
, len
, data
);
122 dev_dbg(micro
->dev
, "key message ignored, no handle\n");
124 case MSG_TOUCHSCREEN
:
126 micro
->ts(micro
->ts_data
, len
, data
);
128 dev_dbg(micro
->dev
, "touchscreen message ignored, no handle\n");
132 "unknown msg %d [%d] %*ph\n", id
, len
, len
, data
);
135 spin_unlock(µ
->lock
);
138 static void micro_process_char(struct ipaq_micro
*micro
, u8 ch
)
140 struct ipaq_micro_rxdev
*rx
= µ
->rx
;
143 case STATE_SOF
: /* Looking for SOF */
145 rx
->state
= STATE_ID
; /* Next byte is the id and len */
147 case STATE_ID
: /* Looking for id and len byte */
148 rx
->id
= (ch
& 0xf0) >> 4;
149 rx
->len
= (ch
& 0x0f);
152 rx
->state
= (rx
->len
> 0) ? STATE_DATA
: STATE_CHKSUM
;
154 case STATE_DATA
: /* Looking for 'len' data bytes */
156 rx
->buf
[rx
->index
] = ch
;
157 if (++rx
->index
== rx
->len
)
158 rx
->state
= STATE_CHKSUM
;
160 case STATE_CHKSUM
: /* Looking for the checksum */
161 if (ch
== rx
->chksum
)
162 micro_rx_msg(micro
, rx
->id
, rx
->len
, rx
->buf
);
163 rx
->state
= STATE_SOF
;
168 static void micro_rx_chars(struct ipaq_micro
*micro
)
172 while ((status
= readl(micro
->base
+ UTSR1
)) & UTSR1_RNE
) {
173 ch
= readl(micro
->base
+ UTDR
);
174 if (status
& UTSR1_PRE
)
175 dev_err(micro
->dev
, "rx: parity error\n");
176 else if (status
& UTSR1_FRE
)
177 dev_err(micro
->dev
, "rx: framing error\n");
178 else if (status
& UTSR1_ROR
)
179 dev_err(micro
->dev
, "rx: overrun error\n");
180 micro_process_char(micro
, ch
);
184 static void ipaq_micro_get_version(struct ipaq_micro
*micro
)
186 struct ipaq_micro_msg msg
= {
190 ipaq_micro_tx_msg_sync(micro
, &msg
);
191 if (msg
.rx_len
== 4) {
192 memcpy(micro
->version
, msg
.rx_data
, 4);
193 micro
->version
[4] = '\0';
194 } else if (msg
.rx_len
== 9) {
195 memcpy(micro
->version
, msg
.rx_data
, 4);
196 micro
->version
[4] = '\0';
197 /* Bytes 4-7 are "pack", byte 8 is "boot type" */
200 "illegal version message %d bytes\n", msg
.rx_len
);
204 static void ipaq_micro_eeprom_read(struct ipaq_micro
*micro
,
205 u8 address
, u8 len
, u8
*data
)
207 struct ipaq_micro_msg msg
= {
208 .id
= MSG_EEPROM_READ
,
212 for (i
= 0; i
< len
; i
++) {
213 msg
.tx_data
[0] = address
+ i
;
216 ipaq_micro_tx_msg_sync(micro
, &msg
);
217 memcpy(data
+ (i
* 2), msg
.rx_data
, 2);
221 static char *ipaq_micro_str(u8
*wchar
, u8 len
)
226 for (i
= 0; i
< len
/ 2; i
++)
227 retstr
[i
] = wchar
[i
* 2];
228 return kstrdup(retstr
, GFP_KERNEL
);
231 static u16
ipaq_micro_to_u16(u8
*data
)
233 return data
[1] << 8 | data
[0];
236 static void __init
ipaq_micro_eeprom_dump(struct ipaq_micro
*micro
)
241 ipaq_micro_eeprom_read(micro
, 0, 128, dump
);
242 str
= ipaq_micro_str(dump
, 10);
244 dev_info(micro
->dev
, "HW version %s\n", str
);
247 str
= ipaq_micro_str(dump
+10, 40);
249 dev_info(micro
->dev
, "serial number: %s\n", str
);
250 /* Feed the random pool with this */
251 add_device_randomness(str
, strlen(str
));
254 str
= ipaq_micro_str(dump
+50, 20);
256 dev_info(micro
->dev
, "module ID: %s\n", str
);
259 str
= ipaq_micro_str(dump
+70, 10);
261 dev_info(micro
->dev
, "product revision: %s\n", str
);
264 dev_info(micro
->dev
, "product ID: %u\n", ipaq_micro_to_u16(dump
+80));
265 dev_info(micro
->dev
, "frame rate: %u fps\n",
266 ipaq_micro_to_u16(dump
+82));
267 dev_info(micro
->dev
, "page mode: %u\n", ipaq_micro_to_u16(dump
+84));
268 dev_info(micro
->dev
, "country ID: %u\n", ipaq_micro_to_u16(dump
+86));
269 dev_info(micro
->dev
, "color display: %s\n",
270 ipaq_micro_to_u16(dump
+88) ? "yes" : "no");
271 dev_info(micro
->dev
, "ROM size: %u MiB\n", ipaq_micro_to_u16(dump
+90));
272 dev_info(micro
->dev
, "RAM size: %u KiB\n", ipaq_micro_to_u16(dump
+92));
273 dev_info(micro
->dev
, "screen: %u x %u\n",
274 ipaq_micro_to_u16(dump
+94), ipaq_micro_to_u16(dump
+96));
277 static void micro_tx_chars(struct ipaq_micro
*micro
)
279 struct ipaq_micro_txdev
*tx
= µ
->tx
;
282 while ((tx
->index
< tx
->len
) &&
283 (readl(micro
->base
+ UTSR1
) & UTSR1_TNF
)) {
284 writel(tx
->buf
[tx
->index
], micro
->base
+ UTDR
);
288 /* Stop interrupts */
289 val
= readl(micro
->base
+ UTCR3
);
291 writel(val
, micro
->base
+ UTCR3
);
294 static void micro_reset_comm(struct ipaq_micro
*micro
)
296 struct ipaq_micro_rxdev
*rx
= µ
->rx
;
300 complete(µ
->msg
->ack
);
302 /* Initialize Serial channel protocol frame */
303 rx
->state
= STATE_SOF
; /* Reset the state machine */
305 /* Set up interrupts */
306 writel(0x01, micro
->sdlc
+ 0x0); /* Select UART mode */
309 writel(0x0, micro
->base
+ UTCR3
);
312 writel(UTCR0_8BitData
| UTCR0_1StpBit
, micro
->base
+ UTCR0
);
314 /* Baud rate: 115200 */
315 writel(0x0, micro
->base
+ UTCR1
);
316 writel(0x1, micro
->base
+ UTCR2
);
319 writel(0xff, micro
->base
+ UTSR0
);
321 /* Enable RX int, disable TX int */
322 writel(UTCR3_TXE
| UTCR3_RXE
| UTCR3_RIE
, micro
->base
+ UTCR3
);
323 val
= readl(micro
->base
+ UTCR3
);
325 writel(val
, micro
->base
+ UTCR3
);
328 static irqreturn_t
micro_serial_isr(int irq
, void *dev_id
)
330 struct ipaq_micro
*micro
= dev_id
;
331 struct ipaq_micro_txdev
*tx
= µ
->tx
;
334 status
= readl(micro
->base
+ UTSR0
);
336 if (status
& (UTSR0_RID
| UTSR0_RFS
)) {
337 if (status
& UTSR0_RID
)
338 /* Clear the Receiver IDLE bit */
339 writel(UTSR0_RID
, micro
->base
+ UTSR0
);
340 micro_rx_chars(micro
);
343 /* Clear break bits */
344 if (status
& (UTSR0_RBB
| UTSR0_REB
))
345 writel(status
& (UTSR0_RBB
| UTSR0_REB
),
346 micro
->base
+ UTSR0
);
348 if (status
& UTSR0_TFS
)
349 micro_tx_chars(micro
);
351 status
= readl(micro
->base
+ UTSR0
);
353 } while (((tx
->index
< tx
->len
) && (status
& UTSR0_TFS
)) ||
354 (status
& (UTSR0_RFS
| UTSR0_RID
)));
359 static const struct mfd_cell micro_cells
[] = {
360 { .name
= "ipaq-micro-backlight", },
361 { .name
= "ipaq-micro-battery", },
362 { .name
= "ipaq-micro-keys", },
363 { .name
= "ipaq-micro-ts", },
364 { .name
= "ipaq-micro-leds", },
367 static int __maybe_unused
micro_resume(struct device
*dev
)
369 struct ipaq_micro
*micro
= dev_get_drvdata(dev
);
371 micro_reset_comm(micro
);
377 static int __init
micro_probe(struct platform_device
*pdev
)
379 struct ipaq_micro
*micro
;
383 micro
= devm_kzalloc(&pdev
->dev
, sizeof(*micro
), GFP_KERNEL
);
387 micro
->dev
= &pdev
->dev
;
389 micro
->base
= devm_platform_get_and_ioremap_resource(pdev
, 0, NULL
);
390 if (IS_ERR(micro
->base
))
391 return PTR_ERR(micro
->base
);
393 micro
->sdlc
= devm_platform_ioremap_resource(pdev
, 1);
394 if (IS_ERR(micro
->sdlc
))
395 return PTR_ERR(micro
->sdlc
);
397 micro_reset_comm(micro
);
399 irq
= platform_get_irq(pdev
, 0);
402 ret
= devm_request_irq(&pdev
->dev
, irq
, micro_serial_isr
,
403 IRQF_SHARED
, "ipaq-micro",
406 dev_err(&pdev
->dev
, "unable to grab serial port IRQ\n");
409 dev_info(&pdev
->dev
, "grabbed serial port IRQ\n");
411 spin_lock_init(µ
->lock
);
412 INIT_LIST_HEAD(µ
->queue
);
413 platform_set_drvdata(pdev
, micro
);
415 ret
= mfd_add_devices(&pdev
->dev
, pdev
->id
, micro_cells
,
416 ARRAY_SIZE(micro_cells
), NULL
, 0, NULL
);
418 dev_err(&pdev
->dev
, "error adding MFD cells");
423 ipaq_micro_get_version(micro
);
424 dev_info(&pdev
->dev
, "Atmel micro ASIC version %s\n", micro
->version
);
425 ipaq_micro_eeprom_dump(micro
);
430 static const struct dev_pm_ops micro_dev_pm_ops
= {
431 SET_SYSTEM_SLEEP_PM_OPS(NULL
, micro_resume
)
434 static struct platform_driver micro_device_driver
= {
436 .name
= "ipaq-h3xxx-micro",
437 .pm
= µ_dev_pm_ops
,
438 .suppress_bind_attrs
= true,
441 builtin_platform_driver_probe(micro_device_driver
, micro_probe
);