2 * Compaq iPAQ h3xxx Atmel microcontroller companion support
4 * This is an Atmel AT90LS8535 with a special flashed-in firmware that
5 * implements the special protocol used by this driver.
7 * based on previous kernel 2.4 version by Andrew Christian
8 * Author : Alessandro Gardich <gremlin@gremlin.it>
9 * Author : Dmitry Artamonow <mad_soft@inbox.ru>
10 * Author : Linus Walleij <linus.walleij@linaro.org>
12 * This program is free software; you can redistribute it and/or modify
13 * it under the terms of the GNU General Public License version 2 as
14 * published by the Free Software Foundation.
17 #include <linux/module.h>
18 #include <linux/init.h>
19 #include <linux/interrupt.h>
21 #include <linux/delay.h>
22 #include <linux/device.h>
23 #include <linux/platform_device.h>
25 #include <linux/mfd/core.h>
26 #include <linux/mfd/ipaq-micro.h>
27 #include <linux/string.h>
28 #include <linux/random.h>
29 #include <linux/slab.h>
30 #include <linux/list.h>
32 #include <mach/hardware.h>
34 static void ipaq_micro_trigger_tx(struct ipaq_micro
*micro
)
36 struct ipaq_micro_txdev
*tx
= µ
->tx
;
37 struct ipaq_micro_msg
*msg
= micro
->msg
;
43 tx
->buf
[bp
++] = CHAR_SOF
;
45 checksum
= ((msg
->id
& 0x0f) << 4) | (msg
->tx_len
& 0x0f);
46 tx
->buf
[bp
++] = checksum
;
48 for (i
= 0; i
< msg
->tx_len
; i
++) {
49 tx
->buf
[bp
++] = msg
->tx_data
[i
];
50 checksum
+= msg
->tx_data
[i
];
53 tx
->buf
[bp
++] = checksum
;
57 /* Enable interrupt */
58 val
= readl(micro
->base
+ UTCR3
);
60 writel(val
, micro
->base
+ UTCR3
);
63 int ipaq_micro_tx_msg(struct ipaq_micro
*micro
, struct ipaq_micro_msg
*msg
)
67 dev_dbg(micro
->dev
, "TX msg: %02x, %d bytes\n", msg
->id
, msg
->tx_len
);
69 spin_lock_irqsave(µ
->lock
, flags
);
71 list_add_tail(&msg
->node
, µ
->queue
);
72 spin_unlock_irqrestore(µ
->lock
, flags
);
76 ipaq_micro_trigger_tx(micro
);
77 spin_unlock_irqrestore(µ
->lock
, flags
);
80 EXPORT_SYMBOL(ipaq_micro_tx_msg
);
82 static void micro_rx_msg(struct ipaq_micro
*micro
, u8 id
, int len
, u8
*data
)
86 dev_dbg(micro
->dev
, "RX msg: %02x, %d bytes\n", id
, len
);
88 spin_lock(µ
->lock
);
92 case MSG_EEPROM_WRITE
:
95 case MSG_THERMAL_SENSOR
:
97 /* Handle synchronous messages */
98 if (micro
->msg
&& micro
->msg
->id
== id
) {
99 struct ipaq_micro_msg
*msg
= micro
->msg
;
101 memcpy(msg
->rx_data
, data
, len
);
103 complete(µ
->msg
->ack
);
104 if (!list_empty(µ
->queue
)) {
105 micro
->msg
= list_entry(micro
->queue
.next
,
106 struct ipaq_micro_msg
,
108 list_del_init(µ
->msg
->node
);
109 ipaq_micro_trigger_tx(micro
);
112 dev_dbg(micro
->dev
, "OK RX message 0x%02x\n", id
);
115 "out of band RX message 0x%02x\n", id
);
117 dev_info(micro
->dev
, "no message queued\n");
119 dev_info(micro
->dev
, "expected message %02x\n",
125 micro
->key(micro
->key_data
, len
, data
);
127 dev_dbg(micro
->dev
, "key message ignored, no handle\n");
129 case MSG_TOUCHSCREEN
:
131 micro
->ts(micro
->ts_data
, len
, data
);
133 dev_dbg(micro
->dev
, "touchscreen message ignored, no handle\n");
137 "unknown msg %d [%d] ", id
, len
);
138 for (i
= 0; i
< len
; ++i
)
139 pr_cont("0x%02x ", data
[i
]);
142 spin_unlock(µ
->lock
);
145 static void micro_process_char(struct ipaq_micro
*micro
, u8 ch
)
147 struct ipaq_micro_rxdev
*rx
= µ
->rx
;
150 case STATE_SOF
: /* Looking for SOF */
152 rx
->state
= STATE_ID
; /* Next byte is the id and len */
154 case STATE_ID
: /* Looking for id and len byte */
155 rx
->id
= (ch
& 0xf0) >> 4;
156 rx
->len
= (ch
& 0x0f);
159 rx
->state
= (rx
->len
> 0) ? STATE_DATA
: STATE_CHKSUM
;
161 case STATE_DATA
: /* Looking for 'len' data bytes */
163 rx
->buf
[rx
->index
] = ch
;
164 if (++rx
->index
== rx
->len
)
165 rx
->state
= STATE_CHKSUM
;
167 case STATE_CHKSUM
: /* Looking for the checksum */
168 if (ch
== rx
->chksum
)
169 micro_rx_msg(micro
, rx
->id
, rx
->len
, rx
->buf
);
170 rx
->state
= STATE_SOF
;
175 static void micro_rx_chars(struct ipaq_micro
*micro
)
179 while ((status
= readl(micro
->base
+ UTSR1
)) & UTSR1_RNE
) {
180 ch
= readl(micro
->base
+ UTDR
);
181 if (status
& UTSR1_PRE
)
182 dev_err(micro
->dev
, "rx: parity error\n");
183 else if (status
& UTSR1_FRE
)
184 dev_err(micro
->dev
, "rx: framing error\n");
185 else if (status
& UTSR1_ROR
)
186 dev_err(micro
->dev
, "rx: overrun error\n");
187 micro_process_char(micro
, ch
);
191 static void ipaq_micro_get_version(struct ipaq_micro
*micro
)
193 struct ipaq_micro_msg msg
= {
197 ipaq_micro_tx_msg_sync(micro
, &msg
);
198 if (msg
.rx_len
== 4) {
199 memcpy(micro
->version
, msg
.rx_data
, 4);
200 micro
->version
[4] = '\0';
201 } else if (msg
.rx_len
== 9) {
202 memcpy(micro
->version
, msg
.rx_data
, 4);
203 micro
->version
[4] = '\0';
204 /* Bytes 4-7 are "pack", byte 8 is "boot type" */
207 "illegal version message %d bytes\n", msg
.rx_len
);
211 static void ipaq_micro_eeprom_read(struct ipaq_micro
*micro
,
212 u8 address
, u8 len
, u8
*data
)
214 struct ipaq_micro_msg msg
= {
215 .id
= MSG_EEPROM_READ
,
219 for (i
= 0; i
< len
; i
++) {
220 msg
.tx_data
[0] = address
+ i
;
223 ipaq_micro_tx_msg_sync(micro
, &msg
);
224 memcpy(data
+ (i
* 2), msg
.rx_data
, 2);
228 static char *ipaq_micro_str(u8
*wchar
, u8 len
)
233 for (i
= 0; i
< len
/ 2; i
++)
234 retstr
[i
] = wchar
[i
* 2];
235 return kstrdup(retstr
, GFP_KERNEL
);
238 static u16
ipaq_micro_to_u16(u8
*data
)
240 return data
[1] << 8 | data
[0];
243 static void __init
ipaq_micro_eeprom_dump(struct ipaq_micro
*micro
)
248 ipaq_micro_eeprom_read(micro
, 0, 128, dump
);
249 str
= ipaq_micro_str(dump
, 10);
251 dev_info(micro
->dev
, "HW version %s\n", str
);
254 str
= ipaq_micro_str(dump
+10, 40);
256 dev_info(micro
->dev
, "serial number: %s\n", str
);
257 /* Feed the random pool with this */
258 add_device_randomness(str
, strlen(str
));
261 str
= ipaq_micro_str(dump
+50, 20);
263 dev_info(micro
->dev
, "module ID: %s\n", str
);
266 str
= ipaq_micro_str(dump
+70, 10);
268 dev_info(micro
->dev
, "product revision: %s\n", str
);
271 dev_info(micro
->dev
, "product ID: %u\n", ipaq_micro_to_u16(dump
+80));
272 dev_info(micro
->dev
, "frame rate: %u fps\n",
273 ipaq_micro_to_u16(dump
+82));
274 dev_info(micro
->dev
, "page mode: %u\n", ipaq_micro_to_u16(dump
+84));
275 dev_info(micro
->dev
, "country ID: %u\n", ipaq_micro_to_u16(dump
+86));
276 dev_info(micro
->dev
, "color display: %s\n",
277 ipaq_micro_to_u16(dump
+88) ? "yes" : "no");
278 dev_info(micro
->dev
, "ROM size: %u MiB\n", ipaq_micro_to_u16(dump
+90));
279 dev_info(micro
->dev
, "RAM size: %u KiB\n", ipaq_micro_to_u16(dump
+92));
280 dev_info(micro
->dev
, "screen: %u x %u\n",
281 ipaq_micro_to_u16(dump
+94), ipaq_micro_to_u16(dump
+96));
284 static void micro_tx_chars(struct ipaq_micro
*micro
)
286 struct ipaq_micro_txdev
*tx
= µ
->tx
;
289 while ((tx
->index
< tx
->len
) &&
290 (readl(micro
->base
+ UTSR1
) & UTSR1_TNF
)) {
291 writel(tx
->buf
[tx
->index
], micro
->base
+ UTDR
);
295 /* Stop interrupts */
296 val
= readl(micro
->base
+ UTCR3
);
298 writel(val
, micro
->base
+ UTCR3
);
301 static void micro_reset_comm(struct ipaq_micro
*micro
)
303 struct ipaq_micro_rxdev
*rx
= µ
->rx
;
307 complete(µ
->msg
->ack
);
309 /* Initialize Serial channel protocol frame */
310 rx
->state
= STATE_SOF
; /* Reset the state machine */
312 /* Set up interrupts */
313 writel(0x01, micro
->sdlc
+ 0x0); /* Select UART mode */
316 writel(0x0, micro
->base
+ UTCR3
);
319 writel(UTCR0_8BitData
| UTCR0_1StpBit
, micro
->base
+ UTCR0
);
321 /* Baud rate: 115200 */
322 writel(0x0, micro
->base
+ UTCR1
);
323 writel(0x1, micro
->base
+ UTCR2
);
326 writel(0xff, micro
->base
+ UTSR0
);
328 /* Enable RX int, disable TX int */
329 writel(UTCR3_TXE
| UTCR3_RXE
| UTCR3_RIE
, micro
->base
+ UTCR3
);
330 val
= readl(micro
->base
+ UTCR3
);
332 writel(val
, micro
->base
+ UTCR3
);
335 static irqreturn_t
micro_serial_isr(int irq
, void *dev_id
)
337 struct ipaq_micro
*micro
= dev_id
;
338 struct ipaq_micro_txdev
*tx
= µ
->tx
;
341 status
= readl(micro
->base
+ UTSR0
);
343 if (status
& (UTSR0_RID
| UTSR0_RFS
)) {
344 if (status
& UTSR0_RID
)
345 /* Clear the Receiver IDLE bit */
346 writel(UTSR0_RID
, micro
->base
+ UTSR0
);
347 micro_rx_chars(micro
);
350 /* Clear break bits */
351 if (status
& (UTSR0_RBB
| UTSR0_REB
))
352 writel(status
& (UTSR0_RBB
| UTSR0_REB
),
353 micro
->base
+ UTSR0
);
355 if (status
& UTSR0_TFS
)
356 micro_tx_chars(micro
);
358 status
= readl(micro
->base
+ UTSR0
);
360 } while (((tx
->index
< tx
->len
) && (status
& UTSR0_TFS
)) ||
361 (status
& (UTSR0_RFS
| UTSR0_RID
)));
366 static const struct mfd_cell micro_cells
[] = {
367 { .name
= "ipaq-micro-backlight", },
368 { .name
= "ipaq-micro-battery", },
369 { .name
= "ipaq-micro-keys", },
370 { .name
= "ipaq-micro-ts", },
371 { .name
= "ipaq-micro-leds", },
374 static int __maybe_unused
micro_resume(struct device
*dev
)
376 struct ipaq_micro
*micro
= dev_get_drvdata(dev
);
378 micro_reset_comm(micro
);
384 static int __init
micro_probe(struct platform_device
*pdev
)
386 struct ipaq_micro
*micro
;
387 struct resource
*res
;
391 micro
= devm_kzalloc(&pdev
->dev
, sizeof(*micro
), GFP_KERNEL
);
395 micro
->dev
= &pdev
->dev
;
397 res
= platform_get_resource(pdev
, IORESOURCE_MEM
, 0);
398 micro
->base
= devm_ioremap_resource(&pdev
->dev
, res
);
399 if (IS_ERR(micro
->base
))
400 return PTR_ERR(micro
->base
);
402 res
= platform_get_resource(pdev
, IORESOURCE_MEM
, 1);
406 micro
->sdlc
= devm_ioremap_resource(&pdev
->dev
, res
);
407 if (IS_ERR(micro
->sdlc
))
408 return PTR_ERR(micro
->sdlc
);
410 micro_reset_comm(micro
);
412 irq
= platform_get_irq(pdev
, 0);
415 ret
= devm_request_irq(&pdev
->dev
, irq
, micro_serial_isr
,
416 IRQF_SHARED
, "ipaq-micro",
419 dev_err(&pdev
->dev
, "unable to grab serial port IRQ\n");
422 dev_info(&pdev
->dev
, "grabbed serial port IRQ\n");
424 spin_lock_init(µ
->lock
);
425 INIT_LIST_HEAD(µ
->queue
);
426 platform_set_drvdata(pdev
, micro
);
428 ret
= mfd_add_devices(&pdev
->dev
, pdev
->id
, micro_cells
,
429 ARRAY_SIZE(micro_cells
), NULL
, 0, NULL
);
431 dev_err(&pdev
->dev
, "error adding MFD cells");
436 ipaq_micro_get_version(micro
);
437 dev_info(&pdev
->dev
, "Atmel micro ASIC version %s\n", micro
->version
);
438 ipaq_micro_eeprom_dump(micro
);
443 static const struct dev_pm_ops micro_dev_pm_ops
= {
444 SET_SYSTEM_SLEEP_PM_OPS(NULL
, micro_resume
)
447 static struct platform_driver micro_device_driver
= {
449 .name
= "ipaq-h3xxx-micro",
450 .pm
= µ_dev_pm_ops
,
451 .suppress_bind_attrs
= true,
454 builtin_platform_driver_probe(micro_device_driver
, micro_probe
);