xref: /linux/drivers/mfd/ipaq-micro.c (revision 03c11eb3b16dc0058589751dfd91f254be2be613)
1d2912cb1SThomas Gleixner // SPDX-License-Identifier: GPL-2.0-only
2dcc21cc0SLinus Walleij /*
3dcc21cc0SLinus Walleij  * Compaq iPAQ h3xxx Atmel microcontroller companion support
4dcc21cc0SLinus Walleij  *
5dcc21cc0SLinus Walleij  * This is an Atmel AT90LS8535 with a special flashed-in firmware that
6dcc21cc0SLinus Walleij  * implements the special protocol used by this driver.
7dcc21cc0SLinus Walleij  *
8dcc21cc0SLinus Walleij  * based on previous kernel 2.4 version by Andrew Christian
9dcc21cc0SLinus Walleij  * Author : Alessandro Gardich <gremlin@gremlin.it>
10dcc21cc0SLinus Walleij  * Author : Dmitry Artamonow <mad_soft@inbox.ru>
11dcc21cc0SLinus Walleij  * Author : Linus Walleij <linus.walleij@linaro.org>
12dcc21cc0SLinus Walleij  */
13dcc21cc0SLinus Walleij 
14dcc21cc0SLinus Walleij #include <linux/module.h>
15dcc21cc0SLinus Walleij #include <linux/init.h>
16dcc21cc0SLinus Walleij #include <linux/interrupt.h>
17dcc21cc0SLinus Walleij #include <linux/pm.h>
18dcc21cc0SLinus Walleij #include <linux/delay.h>
19dcc21cc0SLinus Walleij #include <linux/device.h>
20dcc21cc0SLinus Walleij #include <linux/platform_device.h>
21dcc21cc0SLinus Walleij #include <linux/io.h>
22dcc21cc0SLinus Walleij #include <linux/mfd/core.h>
23dcc21cc0SLinus Walleij #include <linux/mfd/ipaq-micro.h>
24dcc21cc0SLinus Walleij #include <linux/string.h>
25dcc21cc0SLinus Walleij #include <linux/random.h>
26dcc21cc0SLinus Walleij #include <linux/slab.h>
27dcc21cc0SLinus Walleij #include <linux/list.h>
28dcc21cc0SLinus Walleij 
29dcc21cc0SLinus Walleij #include <mach/hardware.h>
30dcc21cc0SLinus Walleij 
ipaq_micro_trigger_tx(struct ipaq_micro * micro)31dcc21cc0SLinus Walleij static void ipaq_micro_trigger_tx(struct ipaq_micro *micro)
32dcc21cc0SLinus Walleij {
33dcc21cc0SLinus Walleij 	struct ipaq_micro_txdev *tx = &micro->tx;
34dcc21cc0SLinus Walleij 	struct ipaq_micro_msg *msg = micro->msg;
35dcc21cc0SLinus Walleij 	int i, bp;
36dcc21cc0SLinus Walleij 	u8 checksum;
37dcc21cc0SLinus Walleij 	u32 val;
38dcc21cc0SLinus Walleij 
39dcc21cc0SLinus Walleij 	bp = 0;
40dcc21cc0SLinus Walleij 	tx->buf[bp++] = CHAR_SOF;
41dcc21cc0SLinus Walleij 
42dcc21cc0SLinus Walleij 	checksum = ((msg->id & 0x0f) << 4) | (msg->tx_len & 0x0f);
43dcc21cc0SLinus Walleij 	tx->buf[bp++] = checksum;
44dcc21cc0SLinus Walleij 
45dcc21cc0SLinus Walleij 	for (i = 0; i < msg->tx_len; i++) {
46dcc21cc0SLinus Walleij 		tx->buf[bp++] = msg->tx_data[i];
47dcc21cc0SLinus Walleij 		checksum += msg->tx_data[i];
48dcc21cc0SLinus Walleij 	}
49dcc21cc0SLinus Walleij 
50dcc21cc0SLinus Walleij 	tx->buf[bp++] = checksum;
51dcc21cc0SLinus Walleij 	tx->len = bp;
52dcc21cc0SLinus Walleij 	tx->index = 0;
53dcc21cc0SLinus Walleij 
54dcc21cc0SLinus Walleij 	/* Enable interrupt */
55dcc21cc0SLinus Walleij 	val = readl(micro->base + UTCR3);
56dcc21cc0SLinus Walleij 	val |= UTCR3_TIE;
57dcc21cc0SLinus Walleij 	writel(val, micro->base + UTCR3);
58dcc21cc0SLinus Walleij }
59dcc21cc0SLinus Walleij 
ipaq_micro_tx_msg(struct ipaq_micro * micro,struct ipaq_micro_msg * msg)60dcc21cc0SLinus Walleij int ipaq_micro_tx_msg(struct ipaq_micro *micro, struct ipaq_micro_msg *msg)
61dcc21cc0SLinus Walleij {
62dcc21cc0SLinus Walleij 	unsigned long flags;
63dcc21cc0SLinus Walleij 
64dcc21cc0SLinus Walleij 	dev_dbg(micro->dev, "TX msg: %02x, %d bytes\n", msg->id, msg->tx_len);
65dcc21cc0SLinus Walleij 
66dcc21cc0SLinus Walleij 	spin_lock_irqsave(&micro->lock, flags);
67dcc21cc0SLinus Walleij 	if (micro->msg) {
68dcc21cc0SLinus Walleij 		list_add_tail(&msg->node, &micro->queue);
69dcc21cc0SLinus Walleij 		spin_unlock_irqrestore(&micro->lock, flags);
70dcc21cc0SLinus Walleij 		return 0;
71dcc21cc0SLinus Walleij 	}
72dcc21cc0SLinus Walleij 	micro->msg = msg;
73dcc21cc0SLinus Walleij 	ipaq_micro_trigger_tx(micro);
74dcc21cc0SLinus Walleij 	spin_unlock_irqrestore(&micro->lock, flags);
75dcc21cc0SLinus Walleij 	return 0;
76dcc21cc0SLinus Walleij }
77dcc21cc0SLinus Walleij EXPORT_SYMBOL(ipaq_micro_tx_msg);
78dcc21cc0SLinus Walleij 
micro_rx_msg(struct ipaq_micro * micro,u8 id,int len,u8 * data)79dcc21cc0SLinus Walleij static void micro_rx_msg(struct ipaq_micro *micro, u8 id, int len, u8 *data)
80dcc21cc0SLinus Walleij {
81dcc21cc0SLinus Walleij 	dev_dbg(micro->dev, "RX msg: %02x, %d bytes\n", id, len);
82dcc21cc0SLinus Walleij 
83dcc21cc0SLinus Walleij 	spin_lock(&micro->lock);
84dcc21cc0SLinus Walleij 	switch (id) {
85dcc21cc0SLinus Walleij 	case MSG_VERSION:
86dcc21cc0SLinus Walleij 	case MSG_EEPROM_READ:
87dcc21cc0SLinus Walleij 	case MSG_EEPROM_WRITE:
88dcc21cc0SLinus Walleij 	case MSG_BACKLIGHT:
89dcc21cc0SLinus Walleij 	case MSG_NOTIFY_LED:
90dcc21cc0SLinus Walleij 	case MSG_THERMAL_SENSOR:
91dcc21cc0SLinus Walleij 	case MSG_BATTERY:
92dcc21cc0SLinus Walleij 		/* Handle synchronous messages */
93dcc21cc0SLinus Walleij 		if (micro->msg && micro->msg->id == id) {
94dcc21cc0SLinus Walleij 			struct ipaq_micro_msg *msg = micro->msg;
95dcc21cc0SLinus Walleij 
96dcc21cc0SLinus Walleij 			memcpy(msg->rx_data, data, len);
97dcc21cc0SLinus Walleij 			msg->rx_len = len;
98dcc21cc0SLinus Walleij 			complete(&micro->msg->ack);
99dcc21cc0SLinus Walleij 			if (!list_empty(&micro->queue)) {
100dcc21cc0SLinus Walleij 				micro->msg = list_entry(micro->queue.next,
101dcc21cc0SLinus Walleij 							struct ipaq_micro_msg,
102dcc21cc0SLinus Walleij 							node);
103dcc21cc0SLinus Walleij 				list_del_init(&micro->msg->node);
104dcc21cc0SLinus Walleij 				ipaq_micro_trigger_tx(micro);
105dcc21cc0SLinus Walleij 			} else
106dcc21cc0SLinus Walleij 				micro->msg = NULL;
107dcc21cc0SLinus Walleij 			dev_dbg(micro->dev, "OK RX message 0x%02x\n", id);
108dcc21cc0SLinus Walleij 		} else {
109dcc21cc0SLinus Walleij 			dev_err(micro->dev,
110dcc21cc0SLinus Walleij 				"out of band RX message 0x%02x\n", id);
111dcc21cc0SLinus Walleij 			if (!micro->msg)
112dcc21cc0SLinus Walleij 				dev_info(micro->dev, "no message queued\n");
113dcc21cc0SLinus Walleij 			else
114dcc21cc0SLinus Walleij 				dev_info(micro->dev, "expected message %02x\n",
115dcc21cc0SLinus Walleij 					 micro->msg->id);
116dcc21cc0SLinus Walleij 		}
117dcc21cc0SLinus Walleij 		break;
118dcc21cc0SLinus Walleij 	case MSG_KEYBOARD:
119dcc21cc0SLinus Walleij 		if (micro->key)
120dcc21cc0SLinus Walleij 			micro->key(micro->key_data, len, data);
121dcc21cc0SLinus Walleij 		else
122dcc21cc0SLinus Walleij 			dev_dbg(micro->dev, "key message ignored, no handle\n");
123dcc21cc0SLinus Walleij 		break;
124dcc21cc0SLinus Walleij 	case MSG_TOUCHSCREEN:
125dcc21cc0SLinus Walleij 		if (micro->ts)
126dcc21cc0SLinus Walleij 			micro->ts(micro->ts_data, len, data);
127dcc21cc0SLinus Walleij 		else
128dcc21cc0SLinus Walleij 			dev_dbg(micro->dev, "touchscreen message ignored, no handle\n");
129dcc21cc0SLinus Walleij 		break;
130dcc21cc0SLinus Walleij 	default:
131dcc21cc0SLinus Walleij 		dev_err(micro->dev,
132*8325ec08SAndy Shevchenko 			"unknown msg %d [%d] %*ph\n", id, len, len, data);
133dcc21cc0SLinus Walleij 	}
134dcc21cc0SLinus Walleij 	spin_unlock(&micro->lock);
135dcc21cc0SLinus Walleij }
136dcc21cc0SLinus Walleij 
micro_process_char(struct ipaq_micro * micro,u8 ch)137dcc21cc0SLinus Walleij static void micro_process_char(struct ipaq_micro *micro, u8 ch)
138dcc21cc0SLinus Walleij {
139dcc21cc0SLinus Walleij 	struct ipaq_micro_rxdev *rx = &micro->rx;
140dcc21cc0SLinus Walleij 
141dcc21cc0SLinus Walleij 	switch (rx->state) {
142dcc21cc0SLinus Walleij 	case STATE_SOF:	/* Looking for SOF */
143dcc21cc0SLinus Walleij 		if (ch == CHAR_SOF)
144dcc21cc0SLinus Walleij 			rx->state = STATE_ID; /* Next byte is the id and len */
145dcc21cc0SLinus Walleij 		break;
146dcc21cc0SLinus Walleij 	case STATE_ID: /* Looking for id and len byte */
147dcc21cc0SLinus Walleij 		rx->id = (ch & 0xf0) >> 4;
148dcc21cc0SLinus Walleij 		rx->len = (ch & 0x0f);
149dcc21cc0SLinus Walleij 		rx->index = 0;
150dcc21cc0SLinus Walleij 		rx->chksum = ch;
151dcc21cc0SLinus Walleij 		rx->state = (rx->len > 0) ? STATE_DATA : STATE_CHKSUM;
152dcc21cc0SLinus Walleij 		break;
153dcc21cc0SLinus Walleij 	case STATE_DATA: /* Looking for 'len' data bytes */
154dcc21cc0SLinus Walleij 		rx->chksum += ch;
155dcc21cc0SLinus Walleij 		rx->buf[rx->index] = ch;
156dcc21cc0SLinus Walleij 		if (++rx->index == rx->len)
157dcc21cc0SLinus Walleij 			rx->state = STATE_CHKSUM;
158dcc21cc0SLinus Walleij 		break;
159dcc21cc0SLinus Walleij 	case STATE_CHKSUM: /* Looking for the checksum */
160dcc21cc0SLinus Walleij 		if (ch == rx->chksum)
161dcc21cc0SLinus Walleij 			micro_rx_msg(micro, rx->id, rx->len, rx->buf);
162dcc21cc0SLinus Walleij 		rx->state = STATE_SOF;
163dcc21cc0SLinus Walleij 		break;
164dcc21cc0SLinus Walleij 	}
165dcc21cc0SLinus Walleij }
166dcc21cc0SLinus Walleij 
micro_rx_chars(struct ipaq_micro * micro)167dcc21cc0SLinus Walleij static void micro_rx_chars(struct ipaq_micro *micro)
168dcc21cc0SLinus Walleij {
169dcc21cc0SLinus Walleij 	u32 status, ch;
170dcc21cc0SLinus Walleij 
171dcc21cc0SLinus Walleij 	while ((status = readl(micro->base + UTSR1)) & UTSR1_RNE) {
172dcc21cc0SLinus Walleij 		ch = readl(micro->base + UTDR);
173dcc21cc0SLinus Walleij 		if (status & UTSR1_PRE)
174dcc21cc0SLinus Walleij 			dev_err(micro->dev, "rx: parity error\n");
175dcc21cc0SLinus Walleij 		else if (status & UTSR1_FRE)
176dcc21cc0SLinus Walleij 			dev_err(micro->dev, "rx: framing error\n");
177dcc21cc0SLinus Walleij 		else if (status & UTSR1_ROR)
178dcc21cc0SLinus Walleij 			dev_err(micro->dev, "rx: overrun error\n");
179dcc21cc0SLinus Walleij 		micro_process_char(micro, ch);
180dcc21cc0SLinus Walleij 	}
181dcc21cc0SLinus Walleij }
182dcc21cc0SLinus Walleij 
ipaq_micro_get_version(struct ipaq_micro * micro)183dcc21cc0SLinus Walleij static void ipaq_micro_get_version(struct ipaq_micro *micro)
184dcc21cc0SLinus Walleij {
185dcc21cc0SLinus Walleij 	struct ipaq_micro_msg msg = {
186dcc21cc0SLinus Walleij 		.id = MSG_VERSION,
187dcc21cc0SLinus Walleij 	};
188dcc21cc0SLinus Walleij 
189dcc21cc0SLinus Walleij 	ipaq_micro_tx_msg_sync(micro, &msg);
190dcc21cc0SLinus Walleij 	if (msg.rx_len == 4) {
191dcc21cc0SLinus Walleij 		memcpy(micro->version, msg.rx_data, 4);
192dcc21cc0SLinus Walleij 		micro->version[4] = '\0';
193dcc21cc0SLinus Walleij 	} else if (msg.rx_len == 9) {
194dcc21cc0SLinus Walleij 		memcpy(micro->version, msg.rx_data, 4);
195dcc21cc0SLinus Walleij 		micro->version[4] = '\0';
196dcc21cc0SLinus Walleij 		/* Bytes 4-7 are "pack", byte 8 is "boot type" */
197dcc21cc0SLinus Walleij 	} else {
198dcc21cc0SLinus Walleij 		dev_err(micro->dev,
199dcc21cc0SLinus Walleij 			"illegal version message %d bytes\n", msg.rx_len);
200dcc21cc0SLinus Walleij 	}
201dcc21cc0SLinus Walleij }
202dcc21cc0SLinus Walleij 
ipaq_micro_eeprom_read(struct ipaq_micro * micro,u8 address,u8 len,u8 * data)203dcc21cc0SLinus Walleij static void ipaq_micro_eeprom_read(struct ipaq_micro *micro,
204dcc21cc0SLinus Walleij 				   u8 address, u8 len, u8 *data)
205dcc21cc0SLinus Walleij {
206dcc21cc0SLinus Walleij 	struct ipaq_micro_msg msg = {
207dcc21cc0SLinus Walleij 		.id = MSG_EEPROM_READ,
208dcc21cc0SLinus Walleij 	};
209dcc21cc0SLinus Walleij 	u8 i;
210dcc21cc0SLinus Walleij 
211dcc21cc0SLinus Walleij 	for (i = 0; i < len; i++) {
212dcc21cc0SLinus Walleij 		msg.tx_data[0] = address + i;
213dcc21cc0SLinus Walleij 		msg.tx_data[1] = 1;
214dcc21cc0SLinus Walleij 		msg.tx_len = 2;
215dcc21cc0SLinus Walleij 		ipaq_micro_tx_msg_sync(micro, &msg);
216dcc21cc0SLinus Walleij 		memcpy(data + (i * 2), msg.rx_data, 2);
217dcc21cc0SLinus Walleij 	}
218dcc21cc0SLinus Walleij }
219dcc21cc0SLinus Walleij 
ipaq_micro_str(u8 * wchar,u8 len)220dcc21cc0SLinus Walleij static char *ipaq_micro_str(u8 *wchar, u8 len)
221dcc21cc0SLinus Walleij {
222dcc21cc0SLinus Walleij 	char retstr[256];
223dcc21cc0SLinus Walleij 	u8 i;
224dcc21cc0SLinus Walleij 
225dcc21cc0SLinus Walleij 	for (i = 0; i < len / 2; i++)
226dcc21cc0SLinus Walleij 		retstr[i] = wchar[i * 2];
227dcc21cc0SLinus Walleij 	return kstrdup(retstr, GFP_KERNEL);
228dcc21cc0SLinus Walleij }
229dcc21cc0SLinus Walleij 
ipaq_micro_to_u16(u8 * data)230dcc21cc0SLinus Walleij static u16 ipaq_micro_to_u16(u8 *data)
231dcc21cc0SLinus Walleij {
232dcc21cc0SLinus Walleij 	return data[1] << 8 | data[0];
233dcc21cc0SLinus Walleij }
234dcc21cc0SLinus Walleij 
ipaq_micro_eeprom_dump(struct ipaq_micro * micro)235035faf4bSLinus Walleij static void __init ipaq_micro_eeprom_dump(struct ipaq_micro *micro)
236dcc21cc0SLinus Walleij {
237dcc21cc0SLinus Walleij 	u8 dump[256];
238dcc21cc0SLinus Walleij 	char *str;
239dcc21cc0SLinus Walleij 
240dcc21cc0SLinus Walleij 	ipaq_micro_eeprom_read(micro, 0, 128, dump);
241dcc21cc0SLinus Walleij 	str = ipaq_micro_str(dump, 10);
242dcc21cc0SLinus Walleij 	if (str) {
2437d60bfc9SLinus Walleij 		dev_info(micro->dev, "HW version %s\n", str);
244dcc21cc0SLinus Walleij 		kfree(str);
245dcc21cc0SLinus Walleij 	}
246dcc21cc0SLinus Walleij 	str = ipaq_micro_str(dump+10, 40);
247dcc21cc0SLinus Walleij 	if (str) {
248dcc21cc0SLinus Walleij 		dev_info(micro->dev, "serial number: %s\n", str);
249dcc21cc0SLinus Walleij 		/* Feed the random pool with this */
250dcc21cc0SLinus Walleij 		add_device_randomness(str, strlen(str));
251dcc21cc0SLinus Walleij 		kfree(str);
252dcc21cc0SLinus Walleij 	}
253dcc21cc0SLinus Walleij 	str = ipaq_micro_str(dump+50, 20);
254dcc21cc0SLinus Walleij 	if (str) {
255dcc21cc0SLinus Walleij 		dev_info(micro->dev, "module ID: %s\n", str);
256dcc21cc0SLinus Walleij 		kfree(str);
257dcc21cc0SLinus Walleij 	}
258dcc21cc0SLinus Walleij 	str = ipaq_micro_str(dump+70, 10);
259dcc21cc0SLinus Walleij 	if (str) {
260dcc21cc0SLinus Walleij 		dev_info(micro->dev, "product revision: %s\n", str);
261dcc21cc0SLinus Walleij 		kfree(str);
262dcc21cc0SLinus Walleij 	}
263dcc21cc0SLinus Walleij 	dev_info(micro->dev, "product ID: %u\n", ipaq_micro_to_u16(dump+80));
264dcc21cc0SLinus Walleij 	dev_info(micro->dev, "frame rate: %u fps\n",
265dcc21cc0SLinus Walleij 		 ipaq_micro_to_u16(dump+82));
266dcc21cc0SLinus Walleij 	dev_info(micro->dev, "page mode: %u\n", ipaq_micro_to_u16(dump+84));
267dcc21cc0SLinus Walleij 	dev_info(micro->dev, "country ID: %u\n", ipaq_micro_to_u16(dump+86));
268dcc21cc0SLinus Walleij 	dev_info(micro->dev, "color display: %s\n",
269dcc21cc0SLinus Walleij 		 ipaq_micro_to_u16(dump+88) ? "yes" : "no");
270dcc21cc0SLinus Walleij 	dev_info(micro->dev, "ROM size: %u MiB\n", ipaq_micro_to_u16(dump+90));
271dcc21cc0SLinus Walleij 	dev_info(micro->dev, "RAM size: %u KiB\n", ipaq_micro_to_u16(dump+92));
272dcc21cc0SLinus Walleij 	dev_info(micro->dev, "screen: %u x %u\n",
273dcc21cc0SLinus Walleij 		 ipaq_micro_to_u16(dump+94), ipaq_micro_to_u16(dump+96));
274dcc21cc0SLinus Walleij }
275dcc21cc0SLinus Walleij 
micro_tx_chars(struct ipaq_micro * micro)276dcc21cc0SLinus Walleij static void micro_tx_chars(struct ipaq_micro *micro)
277dcc21cc0SLinus Walleij {
278dcc21cc0SLinus Walleij 	struct ipaq_micro_txdev *tx = &micro->tx;
279dcc21cc0SLinus Walleij 	u32 val;
280dcc21cc0SLinus Walleij 
281dcc21cc0SLinus Walleij 	while ((tx->index < tx->len) &&
282dcc21cc0SLinus Walleij 	       (readl(micro->base + UTSR1) & UTSR1_TNF)) {
283dcc21cc0SLinus Walleij 		writel(tx->buf[tx->index], micro->base + UTDR);
284dcc21cc0SLinus Walleij 		tx->index++;
285dcc21cc0SLinus Walleij 	}
286dcc21cc0SLinus Walleij 
287dcc21cc0SLinus Walleij 	/* Stop interrupts */
288dcc21cc0SLinus Walleij 	val = readl(micro->base + UTCR3);
289dcc21cc0SLinus Walleij 	val &= ~UTCR3_TIE;
290dcc21cc0SLinus Walleij 	writel(val, micro->base + UTCR3);
291dcc21cc0SLinus Walleij }
292dcc21cc0SLinus Walleij 
micro_reset_comm(struct ipaq_micro * micro)293dcc21cc0SLinus Walleij static void micro_reset_comm(struct ipaq_micro *micro)
294dcc21cc0SLinus Walleij {
295dcc21cc0SLinus Walleij 	struct ipaq_micro_rxdev *rx = &micro->rx;
296dcc21cc0SLinus Walleij 	u32 val;
297dcc21cc0SLinus Walleij 
298dcc21cc0SLinus Walleij 	if (micro->msg)
299dcc21cc0SLinus Walleij 		complete(&micro->msg->ack);
300dcc21cc0SLinus Walleij 
301dcc21cc0SLinus Walleij 	/* Initialize Serial channel protocol frame */
302dcc21cc0SLinus Walleij 	rx->state = STATE_SOF;  /* Reset the state machine */
303dcc21cc0SLinus Walleij 
304dcc21cc0SLinus Walleij 	/* Set up interrupts */
305dcc21cc0SLinus Walleij 	writel(0x01, micro->sdlc + 0x0); /* Select UART mode */
306dcc21cc0SLinus Walleij 
307dcc21cc0SLinus Walleij 	/* Clean up CR3 */
308dcc21cc0SLinus Walleij 	writel(0x0, micro->base + UTCR3);
309dcc21cc0SLinus Walleij 
310dcc21cc0SLinus Walleij 	/* Format: 8N1 */
311dcc21cc0SLinus Walleij 	writel(UTCR0_8BitData | UTCR0_1StpBit, micro->base + UTCR0);
312dcc21cc0SLinus Walleij 
313dcc21cc0SLinus Walleij 	/* Baud rate: 115200 */
314dcc21cc0SLinus Walleij 	writel(0x0, micro->base + UTCR1);
315dcc21cc0SLinus Walleij 	writel(0x1, micro->base + UTCR2);
316dcc21cc0SLinus Walleij 
317dcc21cc0SLinus Walleij 	/* Clear SR0 */
318dcc21cc0SLinus Walleij 	writel(0xff, micro->base + UTSR0);
319dcc21cc0SLinus Walleij 
320dcc21cc0SLinus Walleij 	/* Enable RX int, disable TX int */
321dcc21cc0SLinus Walleij 	writel(UTCR3_TXE | UTCR3_RXE | UTCR3_RIE, micro->base + UTCR3);
322dcc21cc0SLinus Walleij 	val = readl(micro->base + UTCR3);
323dcc21cc0SLinus Walleij 	val &= ~UTCR3_TIE;
324dcc21cc0SLinus Walleij 	writel(val, micro->base + UTCR3);
325dcc21cc0SLinus Walleij }
326dcc21cc0SLinus Walleij 
micro_serial_isr(int irq,void * dev_id)327dcc21cc0SLinus Walleij static irqreturn_t micro_serial_isr(int irq, void *dev_id)
328dcc21cc0SLinus Walleij {
329dcc21cc0SLinus Walleij 	struct ipaq_micro *micro = dev_id;
330dcc21cc0SLinus Walleij 	struct ipaq_micro_txdev *tx = &micro->tx;
331dcc21cc0SLinus Walleij 	u32 status;
332dcc21cc0SLinus Walleij 
333dcc21cc0SLinus Walleij 	status = readl(micro->base + UTSR0);
334dcc21cc0SLinus Walleij 	do {
335dcc21cc0SLinus Walleij 		if (status & (UTSR0_RID | UTSR0_RFS)) {
336dcc21cc0SLinus Walleij 			if (status & UTSR0_RID)
337dcc21cc0SLinus Walleij 				/* Clear the Receiver IDLE bit */
338dcc21cc0SLinus Walleij 				writel(UTSR0_RID, micro->base + UTSR0);
339dcc21cc0SLinus Walleij 			micro_rx_chars(micro);
340dcc21cc0SLinus Walleij 		}
341dcc21cc0SLinus Walleij 
342dcc21cc0SLinus Walleij 		/* Clear break bits */
343dcc21cc0SLinus Walleij 		if (status & (UTSR0_RBB | UTSR0_REB))
344dcc21cc0SLinus Walleij 			writel(status & (UTSR0_RBB | UTSR0_REB),
345dcc21cc0SLinus Walleij 			       micro->base + UTSR0);
346dcc21cc0SLinus Walleij 
347dcc21cc0SLinus Walleij 		if (status & UTSR0_TFS)
348dcc21cc0SLinus Walleij 			micro_tx_chars(micro);
349dcc21cc0SLinus Walleij 
350dcc21cc0SLinus Walleij 		status = readl(micro->base + UTSR0);
351dcc21cc0SLinus Walleij 
352dcc21cc0SLinus Walleij 	} while (((tx->index < tx->len) && (status & UTSR0_TFS)) ||
353dcc21cc0SLinus Walleij 		 (status & (UTSR0_RFS | UTSR0_RID)));
354dcc21cc0SLinus Walleij 
355dcc21cc0SLinus Walleij 	return IRQ_HANDLED;
356dcc21cc0SLinus Walleij }
357dcc21cc0SLinus Walleij 
358165b1cb1SKrzysztof Kozlowski static const struct mfd_cell micro_cells[] = {
359dcc21cc0SLinus Walleij 	{ .name = "ipaq-micro-backlight", },
360dcc21cc0SLinus Walleij 	{ .name = "ipaq-micro-battery", },
361dcc21cc0SLinus Walleij 	{ .name = "ipaq-micro-keys", },
362dcc21cc0SLinus Walleij 	{ .name = "ipaq-micro-ts", },
363dcc21cc0SLinus Walleij 	{ .name = "ipaq-micro-leds", },
364dcc21cc0SLinus Walleij };
365dcc21cc0SLinus Walleij 
micro_resume(struct device * dev)366dcdf1173SArnd Bergmann static int __maybe_unused micro_resume(struct device *dev)
367dcc21cc0SLinus Walleij {
368dcc21cc0SLinus Walleij 	struct ipaq_micro *micro = dev_get_drvdata(dev);
369dcc21cc0SLinus Walleij 
370dcc21cc0SLinus Walleij 	micro_reset_comm(micro);
371dcc21cc0SLinus Walleij 	mdelay(10);
372dcc21cc0SLinus Walleij 
373dcc21cc0SLinus Walleij 	return 0;
374dcc21cc0SLinus Walleij }
375dcc21cc0SLinus Walleij 
micro_probe(struct platform_device * pdev)376035faf4bSLinus Walleij static int __init micro_probe(struct platform_device *pdev)
377dcc21cc0SLinus Walleij {
378dcc21cc0SLinus Walleij 	struct ipaq_micro *micro;
379dcc21cc0SLinus Walleij 	int ret;
380dcc21cc0SLinus Walleij 	int irq;
381dcc21cc0SLinus Walleij 
382dcc21cc0SLinus Walleij 	micro = devm_kzalloc(&pdev->dev, sizeof(*micro), GFP_KERNEL);
383dcc21cc0SLinus Walleij 	if (!micro)
384dcc21cc0SLinus Walleij 		return -ENOMEM;
385dcc21cc0SLinus Walleij 
386dcc21cc0SLinus Walleij 	micro->dev = &pdev->dev;
387dcc21cc0SLinus Walleij 
3884239bff3SYe Xingchen 	micro->base = devm_platform_get_and_ioremap_resource(pdev, 0, NULL);
38963c348cbSJingoo Han 	if (IS_ERR(micro->base))
39063c348cbSJingoo Han 		return PTR_ERR(micro->base);
391dcc21cc0SLinus Walleij 
392f20781e5SMarkus Elfring 	micro->sdlc = devm_platform_ioremap_resource(pdev, 1);
39363c348cbSJingoo Han 	if (IS_ERR(micro->sdlc))
39463c348cbSJingoo Han 		return PTR_ERR(micro->sdlc);
395dcc21cc0SLinus Walleij 
396dcc21cc0SLinus Walleij 	micro_reset_comm(micro);
397dcc21cc0SLinus Walleij 
398dcc21cc0SLinus Walleij 	irq = platform_get_irq(pdev, 0);
3993b49ae38SLv Ruyi 	if (irq < 0)
400dcc21cc0SLinus Walleij 		return -EINVAL;
401dcc21cc0SLinus Walleij 	ret = devm_request_irq(&pdev->dev, irq, micro_serial_isr,
402dcc21cc0SLinus Walleij 			       IRQF_SHARED, "ipaq-micro",
403dcc21cc0SLinus Walleij 			       micro);
404dcc21cc0SLinus Walleij 	if (ret) {
405dcc21cc0SLinus Walleij 		dev_err(&pdev->dev, "unable to grab serial port IRQ\n");
406dcc21cc0SLinus Walleij 		return ret;
407dcc21cc0SLinus Walleij 	} else
408dcc21cc0SLinus Walleij 		dev_info(&pdev->dev, "grabbed serial port IRQ\n");
409dcc21cc0SLinus Walleij 
410dcc21cc0SLinus Walleij 	spin_lock_init(&micro->lock);
411dcc21cc0SLinus Walleij 	INIT_LIST_HEAD(&micro->queue);
412dcc21cc0SLinus Walleij 	platform_set_drvdata(pdev, micro);
413dcc21cc0SLinus Walleij 
414dcc21cc0SLinus Walleij 	ret = mfd_add_devices(&pdev->dev, pdev->id, micro_cells,
415dcc21cc0SLinus Walleij 			      ARRAY_SIZE(micro_cells), NULL, 0, NULL);
416dcc21cc0SLinus Walleij 	if (ret) {
417dcc21cc0SLinus Walleij 		dev_err(&pdev->dev, "error adding MFD cells");
418dcc21cc0SLinus Walleij 		return ret;
419dcc21cc0SLinus Walleij 	}
420dcc21cc0SLinus Walleij 
421dcc21cc0SLinus Walleij 	/* Check version */
422dcc21cc0SLinus Walleij 	ipaq_micro_get_version(micro);
423dcc21cc0SLinus Walleij 	dev_info(&pdev->dev, "Atmel micro ASIC version %s\n", micro->version);
424dcc21cc0SLinus Walleij 	ipaq_micro_eeprom_dump(micro);
425dcc21cc0SLinus Walleij 
426dcc21cc0SLinus Walleij 	return 0;
427dcc21cc0SLinus Walleij }
428dcc21cc0SLinus Walleij 
429dcc21cc0SLinus Walleij static const struct dev_pm_ops micro_dev_pm_ops = {
430dcc21cc0SLinus Walleij 	SET_SYSTEM_SLEEP_PM_OPS(NULL, micro_resume)
431dcc21cc0SLinus Walleij };
432dcc21cc0SLinus Walleij 
433dcc21cc0SLinus Walleij static struct platform_driver micro_device_driver = {
434dcc21cc0SLinus Walleij 	.driver   = {
435dcc21cc0SLinus Walleij 		.name	= "ipaq-h3xxx-micro",
436dcc21cc0SLinus Walleij 		.pm	= &micro_dev_pm_ops,
437035faf4bSLinus Walleij 		.suppress_bind_attrs = true,
438dcc21cc0SLinus Walleij 	},
439dcc21cc0SLinus Walleij };
440035faf4bSLinus Walleij builtin_platform_driver_probe(micro_device_driver, micro_probe);
441