ipaq-micro.c 11 KB

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