blob: cd762d08f116f1edfa18678ec24413e3eab078b3 [file] [log] [blame]
Linus Walleijdcc21cc2014-04-03 17:45:15 +02001/*
2 * Compaq iPAQ h3xxx Atmel microcontroller companion support
3 *
4 * This is an Atmel AT90LS8535 with a special flashed-in firmware that
5 * implements the special protocol used by this driver.
6 *
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>
11 *
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.
15 */
16
17#include <linux/module.h>
18#include <linux/init.h>
19#include <linux/interrupt.h>
20#include <linux/pm.h>
21#include <linux/delay.h>
22#include <linux/device.h>
23#include <linux/platform_device.h>
24#include <linux/io.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>
31
32#include <mach/hardware.h>
33
34static void ipaq_micro_trigger_tx(struct ipaq_micro *micro)
35{
36 struct ipaq_micro_txdev *tx = &micro->tx;
37 struct ipaq_micro_msg *msg = micro->msg;
38 int i, bp;
39 u8 checksum;
40 u32 val;
41
42 bp = 0;
43 tx->buf[bp++] = CHAR_SOF;
44
45 checksum = ((msg->id & 0x0f) << 4) | (msg->tx_len & 0x0f);
46 tx->buf[bp++] = checksum;
47
48 for (i = 0; i < msg->tx_len; i++) {
49 tx->buf[bp++] = msg->tx_data[i];
50 checksum += msg->tx_data[i];
51 }
52
53 tx->buf[bp++] = checksum;
54 tx->len = bp;
55 tx->index = 0;
Linus Walleijdcc21cc2014-04-03 17:45:15 +020056
57 /* Enable interrupt */
58 val = readl(micro->base + UTCR3);
59 val |= UTCR3_TIE;
60 writel(val, micro->base + UTCR3);
61}
62
63int ipaq_micro_tx_msg(struct ipaq_micro *micro, struct ipaq_micro_msg *msg)
64{
65 unsigned long flags;
66
67 dev_dbg(micro->dev, "TX msg: %02x, %d bytes\n", msg->id, msg->tx_len);
68
69 spin_lock_irqsave(&micro->lock, flags);
70 if (micro->msg) {
71 list_add_tail(&msg->node, &micro->queue);
72 spin_unlock_irqrestore(&micro->lock, flags);
73 return 0;
74 }
75 micro->msg = msg;
76 ipaq_micro_trigger_tx(micro);
77 spin_unlock_irqrestore(&micro->lock, flags);
78 return 0;
79}
80EXPORT_SYMBOL(ipaq_micro_tx_msg);
81
82static void micro_rx_msg(struct ipaq_micro *micro, u8 id, int len, u8 *data)
83{
84 int i;
85
86 dev_dbg(micro->dev, "RX msg: %02x, %d bytes\n", id, len);
87
88 spin_lock(&micro->lock);
89 switch (id) {
90 case MSG_VERSION:
91 case MSG_EEPROM_READ:
92 case MSG_EEPROM_WRITE:
93 case MSG_BACKLIGHT:
94 case MSG_NOTIFY_LED:
95 case MSG_THERMAL_SENSOR:
96 case MSG_BATTERY:
97 /* Handle synchronous messages */
98 if (micro->msg && micro->msg->id == id) {
99 struct ipaq_micro_msg *msg = micro->msg;
100
101 memcpy(msg->rx_data, data, len);
102 msg->rx_len = len;
103 complete(&micro->msg->ack);
104 if (!list_empty(&micro->queue)) {
105 micro->msg = list_entry(micro->queue.next,
106 struct ipaq_micro_msg,
107 node);
108 list_del_init(&micro->msg->node);
109 ipaq_micro_trigger_tx(micro);
110 } else
111 micro->msg = NULL;
112 dev_dbg(micro->dev, "OK RX message 0x%02x\n", id);
113 } else {
114 dev_err(micro->dev,
115 "out of band RX message 0x%02x\n", id);
Lee Jones9336fe92014-07-21 16:08:55 +0100116 if (!micro->msg)
Linus Walleijdcc21cc2014-04-03 17:45:15 +0200117 dev_info(micro->dev, "no message queued\n");
118 else
119 dev_info(micro->dev, "expected message %02x\n",
120 micro->msg->id);
121 }
122 break;
123 case MSG_KEYBOARD:
124 if (micro->key)
125 micro->key(micro->key_data, len, data);
126 else
Lee Jones9336fe92014-07-21 16:08:55 +0100127 dev_dbg(micro->dev, "key message ignored, no handle\n");
Linus Walleijdcc21cc2014-04-03 17:45:15 +0200128 break;
129 case MSG_TOUCHSCREEN:
130 if (micro->ts)
131 micro->ts(micro->ts_data, len, data);
132 else
Lee Jones9336fe92014-07-21 16:08:55 +0100133 dev_dbg(micro->dev, "touchscreen message ignored, no handle\n");
Linus Walleijdcc21cc2014-04-03 17:45:15 +0200134 break;
135 default:
136 dev_err(micro->dev,
137 "unknown msg %d [%d] ", id, len);
138 for (i = 0; i < len; ++i)
139 pr_cont("0x%02x ", data[i]);
140 pr_cont("\n");
141 }
142 spin_unlock(&micro->lock);
143}
144
145static void micro_process_char(struct ipaq_micro *micro, u8 ch)
146{
147 struct ipaq_micro_rxdev *rx = &micro->rx;
148
149 switch (rx->state) {
150 case STATE_SOF: /* Looking for SOF */
151 if (ch == CHAR_SOF)
152 rx->state = STATE_ID; /* Next byte is the id and len */
153 break;
154 case STATE_ID: /* Looking for id and len byte */
Lee Jones9336fe92014-07-21 16:08:55 +0100155 rx->id = (ch & 0xf0) >> 4;
Linus Walleijdcc21cc2014-04-03 17:45:15 +0200156 rx->len = (ch & 0x0f);
157 rx->index = 0;
158 rx->chksum = ch;
159 rx->state = (rx->len > 0) ? STATE_DATA : STATE_CHKSUM;
160 break;
161 case STATE_DATA: /* Looking for 'len' data bytes */
162 rx->chksum += ch;
163 rx->buf[rx->index] = ch;
164 if (++rx->index == rx->len)
165 rx->state = STATE_CHKSUM;
166 break;
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;
171 break;
172 }
173}
174
175static void micro_rx_chars(struct ipaq_micro *micro)
176{
177 u32 status, ch;
178
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);
188 }
189}
190
191static void ipaq_micro_get_version(struct ipaq_micro *micro)
192{
193 struct ipaq_micro_msg msg = {
194 .id = MSG_VERSION,
195 };
196
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" */
205 } else {
206 dev_err(micro->dev,
207 "illegal version message %d bytes\n", msg.rx_len);
208 }
209}
210
211static void ipaq_micro_eeprom_read(struct ipaq_micro *micro,
212 u8 address, u8 len, u8 *data)
213{
214 struct ipaq_micro_msg msg = {
215 .id = MSG_EEPROM_READ,
216 };
217 u8 i;
218
219 for (i = 0; i < len; i++) {
220 msg.tx_data[0] = address + i;
221 msg.tx_data[1] = 1;
222 msg.tx_len = 2;
223 ipaq_micro_tx_msg_sync(micro, &msg);
224 memcpy(data + (i * 2), msg.rx_data, 2);
225 }
226}
227
228static char *ipaq_micro_str(u8 *wchar, u8 len)
229{
230 char retstr[256];
231 u8 i;
232
233 for (i = 0; i < len / 2; i++)
234 retstr[i] = wchar[i * 2];
235 return kstrdup(retstr, GFP_KERNEL);
236}
237
238static u16 ipaq_micro_to_u16(u8 *data)
239{
240 return data[1] << 8 | data[0];
241}
242
Linus Walleij035faf42015-07-22 09:55:43 +0200243static void __init ipaq_micro_eeprom_dump(struct ipaq_micro *micro)
Linus Walleijdcc21cc2014-04-03 17:45:15 +0200244{
245 u8 dump[256];
246 char *str;
247
248 ipaq_micro_eeprom_read(micro, 0, 128, dump);
249 str = ipaq_micro_str(dump, 10);
250 if (str) {
Linus Walleij7d60bfc2015-07-22 09:55:41 +0200251 dev_info(micro->dev, "HW version %s\n", str);
Linus Walleijdcc21cc2014-04-03 17:45:15 +0200252 kfree(str);
253 }
254 str = ipaq_micro_str(dump+10, 40);
255 if (str) {
256 dev_info(micro->dev, "serial number: %s\n", str);
257 /* Feed the random pool with this */
258 add_device_randomness(str, strlen(str));
259 kfree(str);
260 }
261 str = ipaq_micro_str(dump+50, 20);
262 if (str) {
263 dev_info(micro->dev, "module ID: %s\n", str);
264 kfree(str);
265 }
266 str = ipaq_micro_str(dump+70, 10);
267 if (str) {
268 dev_info(micro->dev, "product revision: %s\n", str);
269 kfree(str);
270 }
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));
Linus Walleijdcc21cc2014-04-03 17:45:15 +0200282}
283
284static void micro_tx_chars(struct ipaq_micro *micro)
285{
286 struct ipaq_micro_txdev *tx = &micro->tx;
287 u32 val;
288
289 while ((tx->index < tx->len) &&
290 (readl(micro->base + UTSR1) & UTSR1_TNF)) {
291 writel(tx->buf[tx->index], micro->base + UTDR);
292 tx->index++;
293 }
294
295 /* Stop interrupts */
296 val = readl(micro->base + UTCR3);
297 val &= ~UTCR3_TIE;
298 writel(val, micro->base + UTCR3);
299}
300
301static void micro_reset_comm(struct ipaq_micro *micro)
302{
303 struct ipaq_micro_rxdev *rx = &micro->rx;
304 u32 val;
305
306 if (micro->msg)
307 complete(&micro->msg->ack);
308
309 /* Initialize Serial channel protocol frame */
310 rx->state = STATE_SOF; /* Reset the state machine */
311
312 /* Set up interrupts */
313 writel(0x01, micro->sdlc + 0x0); /* Select UART mode */
314
315 /* Clean up CR3 */
316 writel(0x0, micro->base + UTCR3);
317
318 /* Format: 8N1 */
319 writel(UTCR0_8BitData | UTCR0_1StpBit, micro->base + UTCR0);
320
321 /* Baud rate: 115200 */
322 writel(0x0, micro->base + UTCR1);
323 writel(0x1, micro->base + UTCR2);
324
325 /* Clear SR0 */
326 writel(0xff, micro->base + UTSR0);
327
328 /* Enable RX int, disable TX int */
329 writel(UTCR3_TXE | UTCR3_RXE | UTCR3_RIE, micro->base + UTCR3);
330 val = readl(micro->base + UTCR3);
331 val &= ~UTCR3_TIE;
332 writel(val, micro->base + UTCR3);
333}
334
335static irqreturn_t micro_serial_isr(int irq, void *dev_id)
336{
337 struct ipaq_micro *micro = dev_id;
338 struct ipaq_micro_txdev *tx = &micro->tx;
339 u32 status;
340
341 status = readl(micro->base + UTSR0);
342 do {
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);
348 }
349
350 /* Clear break bits */
351 if (status & (UTSR0_RBB | UTSR0_REB))
352 writel(status & (UTSR0_RBB | UTSR0_REB),
353 micro->base + UTSR0);
354
355 if (status & UTSR0_TFS)
356 micro_tx_chars(micro);
357
358 status = readl(micro->base + UTSR0);
359
360 } while (((tx->index < tx->len) && (status & UTSR0_TFS)) ||
361 (status & (UTSR0_RFS | UTSR0_RID)));
362
363 return IRQ_HANDLED;
364}
365
Krzysztof Kozlowski165b1cb2014-05-13 12:58:40 +0200366static const struct mfd_cell micro_cells[] = {
Linus Walleijdcc21cc2014-04-03 17:45:15 +0200367 { .name = "ipaq-micro-backlight", },
368 { .name = "ipaq-micro-battery", },
369 { .name = "ipaq-micro-keys", },
370 { .name = "ipaq-micro-ts", },
371 { .name = "ipaq-micro-leds", },
372};
373
Arnd Bergmanndcdf1172016-03-02 16:58:57 +0100374static int __maybe_unused micro_resume(struct device *dev)
Linus Walleijdcc21cc2014-04-03 17:45:15 +0200375{
376 struct ipaq_micro *micro = dev_get_drvdata(dev);
377
378 micro_reset_comm(micro);
379 mdelay(10);
380
381 return 0;
382}
383
Linus Walleij035faf42015-07-22 09:55:43 +0200384static int __init micro_probe(struct platform_device *pdev)
Linus Walleijdcc21cc2014-04-03 17:45:15 +0200385{
386 struct ipaq_micro *micro;
387 struct resource *res;
388 int ret;
389 int irq;
390
391 micro = devm_kzalloc(&pdev->dev, sizeof(*micro), GFP_KERNEL);
392 if (!micro)
393 return -ENOMEM;
394
395 micro->dev = &pdev->dev;
396
397 res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
Jingoo Han63c348c2014-05-14 12:17:14 +0900398 micro->base = devm_ioremap_resource(&pdev->dev, res);
399 if (IS_ERR(micro->base))
400 return PTR_ERR(micro->base);
Linus Walleijdcc21cc2014-04-03 17:45:15 +0200401
402 res = platform_get_resource(pdev, IORESOURCE_MEM, 1);
403 if (!res)
404 return -EINVAL;
405
Jingoo Han63c348c2014-05-14 12:17:14 +0900406 micro->sdlc = devm_ioremap_resource(&pdev->dev, res);
407 if (IS_ERR(micro->sdlc))
408 return PTR_ERR(micro->sdlc);
Linus Walleijdcc21cc2014-04-03 17:45:15 +0200409
410 micro_reset_comm(micro);
411
412 irq = platform_get_irq(pdev, 0);
413 if (!irq)
414 return -EINVAL;
415 ret = devm_request_irq(&pdev->dev, irq, micro_serial_isr,
416 IRQF_SHARED, "ipaq-micro",
417 micro);
418 if (ret) {
419 dev_err(&pdev->dev, "unable to grab serial port IRQ\n");
420 return ret;
421 } else
422 dev_info(&pdev->dev, "grabbed serial port IRQ\n");
423
424 spin_lock_init(&micro->lock);
425 INIT_LIST_HEAD(&micro->queue);
426 platform_set_drvdata(pdev, micro);
427
428 ret = mfd_add_devices(&pdev->dev, pdev->id, micro_cells,
429 ARRAY_SIZE(micro_cells), NULL, 0, NULL);
430 if (ret) {
431 dev_err(&pdev->dev, "error adding MFD cells");
432 return ret;
433 }
434
435 /* Check version */
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);
439
440 return 0;
441}
442
Linus Walleijdcc21cc2014-04-03 17:45:15 +0200443static const struct dev_pm_ops micro_dev_pm_ops = {
444 SET_SYSTEM_SLEEP_PM_OPS(NULL, micro_resume)
445};
446
447static struct platform_driver micro_device_driver = {
448 .driver = {
449 .name = "ipaq-h3xxx-micro",
450 .pm = &micro_dev_pm_ops,
Linus Walleij035faf42015-07-22 09:55:43 +0200451 .suppress_bind_attrs = true,
Linus Walleijdcc21cc2014-04-03 17:45:15 +0200452 },
Linus Walleijdcc21cc2014-04-03 17:45:15 +0200453};
Linus Walleij035faf42015-07-22 09:55:43 +0200454builtin_platform_driver_probe(micro_device_driver, micro_probe);