ipaq-micro.c 11 KB

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