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