mrst_max3110.c 20 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917
  1. /*
  2. * mrst_max3110.c - spi uart protocol driver for Maxim 3110
  3. *
  4. * Copyright (c) 2008-2010, Intel Corporation.
  5. *
  6. * This program is free software; you can redistribute it and/or modify it
  7. * under the terms and conditions of the GNU General Public License,
  8. * version 2, as published by the Free Software Foundation.
  9. *
  10. * This program is distributed in the hope it will be useful, but WITHOUT
  11. * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
  12. * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for
  13. * more details.
  14. *
  15. * You should have received a copy of the GNU General Public License along with
  16. * this program; if not, write to the Free Software Foundation, Inc.,
  17. * 51 Franklin St - Fifth Floor, Boston, MA 02110-1301 USA.
  18. */
  19. /*
  20. * Note:
  21. * 1. From Max3110 spec, the Rx FIFO has 8 words, while the Tx FIFO only has
  22. * 1 word. If SPI master controller doesn't support sclk frequency change,
  23. * then the char need be sent out one by one with some delay
  24. *
  25. * 2. Currently only RX available interrupt is used, no need for waiting TXE
  26. * interrupt for a low speed UART device
  27. */
  28. #ifdef CONFIG_MAGIC_SYSRQ
  29. #define SUPPORT_SYSRQ
  30. #endif
  31. #include <linux/module.h>
  32. #include <linux/ioport.h>
  33. #include <linux/irq.h>
  34. #include <linux/init.h>
  35. #include <linux/console.h>
  36. #include <linux/tty.h>
  37. #include <linux/tty_flip.h>
  38. #include <linux/serial_core.h>
  39. #include <linux/serial_reg.h>
  40. #include <linux/kthread.h>
  41. #include <linux/spi/spi.h>
  42. #include <linux/pm.h>
  43. #include "mrst_max3110.h"
  44. #define PR_FMT "mrst_max3110: "
  45. #define UART_TX_NEEDED 1
  46. #define CON_TX_NEEDED 2
  47. #define BIT_IRQ_PENDING 3
  48. struct uart_max3110 {
  49. struct uart_port port;
  50. struct spi_device *spi;
  51. char name[SPI_NAME_SIZE];
  52. wait_queue_head_t wq;
  53. struct task_struct *main_thread;
  54. struct task_struct *read_thread;
  55. struct mutex thread_mutex;
  56. struct mutex io_mutex;
  57. u32 baud;
  58. u16 cur_conf;
  59. u8 clock;
  60. u8 parity, word_7bits;
  61. u16 irq;
  62. unsigned long uart_flags;
  63. /* console related */
  64. struct circ_buf con_xmit;
  65. };
  66. /* global data structure, may need be removed */
  67. static struct uart_max3110 *pmax;
  68. static int receive_chars(struct uart_max3110 *max,
  69. unsigned short *str, int len);
  70. static int max3110_read_multi(struct uart_max3110 *max);
  71. static void max3110_con_receive(struct uart_max3110 *max);
  72. static int max3110_write_then_read(struct uart_max3110 *max,
  73. const void *txbuf, void *rxbuf, unsigned len, int always_fast)
  74. {
  75. struct spi_device *spi = max->spi;
  76. struct spi_message message;
  77. struct spi_transfer x;
  78. int ret;
  79. mutex_lock(&max->io_mutex);
  80. spi_message_init(&message);
  81. memset(&x, 0, sizeof x);
  82. x.len = len;
  83. x.tx_buf = txbuf;
  84. x.rx_buf = rxbuf;
  85. spi_message_add_tail(&x, &message);
  86. if (always_fast)
  87. x.speed_hz = spi->max_speed_hz;
  88. else if (max->baud)
  89. x.speed_hz = max->baud;
  90. /* Do the i/o */
  91. ret = spi_sync(spi, &message);
  92. mutex_unlock(&max->io_mutex);
  93. return ret;
  94. }
  95. /* Write a 16b word to the device */
  96. static int max3110_out(struct uart_max3110 *max, const u16 out)
  97. {
  98. void *buf;
  99. u16 *obuf, *ibuf;
  100. int ret;
  101. buf = kzalloc(8, GFP_KERNEL | GFP_DMA);
  102. if (!buf)
  103. return -ENOMEM;
  104. obuf = buf;
  105. ibuf = buf + 4;
  106. *obuf = out;
  107. ret = max3110_write_then_read(max, obuf, ibuf, 2, 1);
  108. if (ret) {
  109. pr_warning(PR_FMT "%s(): get err msg %d when sending 0x%x\n",
  110. __func__, ret, out);
  111. goto exit;
  112. }
  113. receive_chars(max, ibuf, 1);
  114. exit:
  115. kfree(buf);
  116. return ret;
  117. }
  118. /*
  119. * This is usually used to read data from SPIC RX FIFO, which doesn't
  120. * need any delay like flushing character out.
  121. *
  122. * Return how many valide bytes are read back
  123. */
  124. static int max3110_read_multi(struct uart_max3110 *max)
  125. {
  126. void *buf;
  127. u16 *obuf, *ibuf;
  128. int ret, blen;
  129. blen = M3110_RX_FIFO_DEPTH * sizeof(u16);
  130. buf = kzalloc(blen * 2, GFP_KERNEL | GFP_DMA);
  131. if (!buf) {
  132. pr_warning(PR_FMT "%s(): fail to alloc dma buffer\n", __func__);
  133. return 0;
  134. }
  135. /* tx/rx always have the same length */
  136. obuf = buf;
  137. ibuf = buf + blen;
  138. if (max3110_write_then_read(max, obuf, ibuf, blen, 1)) {
  139. kfree(buf);
  140. return 0;
  141. }
  142. ret = receive_chars(max, ibuf, M3110_RX_FIFO_DEPTH);
  143. kfree(buf);
  144. return ret;
  145. }
  146. static void serial_m3110_con_putchar(struct uart_port *port, int ch)
  147. {
  148. struct uart_max3110 *max =
  149. container_of(port, struct uart_max3110, port);
  150. struct circ_buf *xmit = &max->con_xmit;
  151. if (uart_circ_chars_free(xmit)) {
  152. xmit->buf[xmit->head] = (char)ch;
  153. xmit->head = (xmit->head + 1) & (PAGE_SIZE - 1);
  154. }
  155. }
  156. /*
  157. * Print a string to the serial port trying not to disturb
  158. * any possible real use of the port...
  159. *
  160. * The console_lock must be held when we get here.
  161. */
  162. static void serial_m3110_con_write(struct console *co,
  163. const char *s, unsigned int count)
  164. {
  165. if (!pmax)
  166. return;
  167. uart_console_write(&pmax->port, s, count, serial_m3110_con_putchar);
  168. if (!test_and_set_bit(CON_TX_NEEDED, &pmax->uart_flags))
  169. wake_up(&pmax->wq);
  170. }
  171. static int __init
  172. serial_m3110_con_setup(struct console *co, char *options)
  173. {
  174. struct uart_max3110 *max = pmax;
  175. int baud = 115200;
  176. int bits = 8;
  177. int parity = 'n';
  178. int flow = 'n';
  179. pr_info(PR_FMT "setting up console\n");
  180. if (co->index == -1)
  181. co->index = 0;
  182. if (!max) {
  183. pr_err(PR_FMT "pmax is NULL, return");
  184. return -ENODEV;
  185. }
  186. if (options)
  187. uart_parse_options(options, &baud, &parity, &bits, &flow);
  188. return uart_set_options(&max->port, co, baud, parity, bits, flow);
  189. }
  190. static struct tty_driver *serial_m3110_con_device(struct console *co,
  191. int *index)
  192. {
  193. struct uart_driver *p = co->data;
  194. *index = co->index;
  195. return p->tty_driver;
  196. }
  197. static struct uart_driver serial_m3110_reg;
  198. static struct console serial_m3110_console = {
  199. .name = "ttyS",
  200. .write = serial_m3110_con_write,
  201. .device = serial_m3110_con_device,
  202. .setup = serial_m3110_con_setup,
  203. .flags = CON_PRINTBUFFER,
  204. .index = -1,
  205. .data = &serial_m3110_reg,
  206. };
  207. static unsigned int serial_m3110_tx_empty(struct uart_port *port)
  208. {
  209. return 1;
  210. }
  211. static void serial_m3110_stop_tx(struct uart_port *port)
  212. {
  213. return;
  214. }
  215. /* stop_rx will be called in spin_lock env */
  216. static void serial_m3110_stop_rx(struct uart_port *port)
  217. {
  218. return;
  219. }
  220. #define WORDS_PER_XFER 128
  221. static void send_circ_buf(struct uart_max3110 *max,
  222. struct circ_buf *xmit)
  223. {
  224. void *buf;
  225. u16 *obuf, *ibuf;
  226. int i, len, blen, dma_size, left, ret = 0;
  227. dma_size = WORDS_PER_XFER * sizeof(u16) * 2;
  228. buf = kzalloc(dma_size, GFP_KERNEL | GFP_DMA);
  229. if (!buf)
  230. return;
  231. obuf = buf;
  232. ibuf = buf + dma_size/2;
  233. while (!uart_circ_empty(xmit)) {
  234. left = uart_circ_chars_pending(xmit);
  235. while (left) {
  236. len = min(left, WORDS_PER_XFER);
  237. blen = len * sizeof(u16);
  238. memset(ibuf, 0, blen);
  239. for (i = 0; i < len; i++) {
  240. obuf[i] = (u8)xmit->buf[xmit->tail] | WD_TAG;
  241. xmit->tail = (xmit->tail + 1) &
  242. (UART_XMIT_SIZE - 1);
  243. }
  244. /* Fail to send msg to console is not very critical */
  245. ret = max3110_write_then_read(max, obuf, ibuf, blen, 0);
  246. if (ret)
  247. pr_warning(PR_FMT "%s(): get err msg %d\n",
  248. __func__, ret);
  249. receive_chars(max, ibuf, len);
  250. max->port.icount.tx += len;
  251. left -= len;
  252. }
  253. }
  254. kfree(buf);
  255. }
  256. static void transmit_char(struct uart_max3110 *max)
  257. {
  258. struct uart_port *port = &max->port;
  259. struct circ_buf *xmit = &port->state->xmit;
  260. if (uart_circ_empty(xmit) || uart_tx_stopped(port))
  261. return;
  262. send_circ_buf(max, xmit);
  263. if (uart_circ_chars_pending(xmit) < WAKEUP_CHARS)
  264. uart_write_wakeup(port);
  265. if (uart_circ_empty(xmit))
  266. serial_m3110_stop_tx(port);
  267. }
  268. /*
  269. * This will be called by uart_write() and tty_write, can't
  270. * go to sleep
  271. */
  272. static void serial_m3110_start_tx(struct uart_port *port)
  273. {
  274. struct uart_max3110 *max =
  275. container_of(port, struct uart_max3110, port);
  276. if (!test_and_set_bit(UART_TX_NEEDED, &max->uart_flags))
  277. wake_up(&max->wq);
  278. }
  279. static int
  280. receive_chars(struct uart_max3110 *max, unsigned short *str, int len)
  281. {
  282. struct uart_port *port = &max->port;
  283. struct tty_port *tport;
  284. char buf[M3110_RX_FIFO_DEPTH];
  285. int r, w, usable;
  286. /* If uart is not opened, just return */
  287. if (!port->state)
  288. return 0;
  289. tport = &port->state->port;
  290. for (r = 0, w = 0; r < len; r++) {
  291. if (str[r] & MAX3110_BREAK &&
  292. uart_handle_break(port))
  293. continue;
  294. if (str[r] & MAX3110_READ_DATA_AVAILABLE) {
  295. if (uart_handle_sysrq_char(port, str[r] & 0xff))
  296. continue;
  297. buf[w++] = str[r] & 0xff;
  298. }
  299. }
  300. if (!w)
  301. return 0;
  302. for (r = 0; w; r += usable, w -= usable) {
  303. usable = tty_buffer_request_room(tport, w);
  304. if (usable) {
  305. tty_insert_flip_string(tport, buf + r, usable);
  306. port->icount.rx += usable;
  307. }
  308. }
  309. tty_flip_buffer_push(tport);
  310. return r;
  311. }
  312. /*
  313. * This routine will be used in read_thread or RX IRQ handling,
  314. * it will first do one round buffer read(8 words), if there is some
  315. * valid RX data, will try to read 5 more rounds till all data
  316. * is read out.
  317. *
  318. * Use stack space as data buffer to save some system load, and chose
  319. * 504 Btyes as a threadhold to do a bulk push to upper tty layer when
  320. * receiving bulk data, a much bigger buffer may cause stack overflow
  321. */
  322. static void max3110_con_receive(struct uart_max3110 *max)
  323. {
  324. int loop = 1, num;
  325. do {
  326. num = max3110_read_multi(max);
  327. if (num) {
  328. loop = 5;
  329. }
  330. } while (--loop);
  331. }
  332. static int max3110_main_thread(void *_max)
  333. {
  334. struct uart_max3110 *max = _max;
  335. wait_queue_head_t *wq = &max->wq;
  336. int ret = 0;
  337. struct circ_buf *xmit = &max->con_xmit;
  338. pr_info(PR_FMT "start main thread\n");
  339. do {
  340. wait_event_interruptible(*wq,
  341. max->uart_flags || kthread_should_stop());
  342. mutex_lock(&max->thread_mutex);
  343. if (test_and_clear_bit(BIT_IRQ_PENDING, &max->uart_flags))
  344. max3110_con_receive(max);
  345. /* first handle console output */
  346. if (test_and_clear_bit(CON_TX_NEEDED, &max->uart_flags))
  347. send_circ_buf(max, xmit);
  348. /* handle uart output */
  349. if (test_and_clear_bit(UART_TX_NEEDED, &max->uart_flags))
  350. transmit_char(max);
  351. mutex_unlock(&max->thread_mutex);
  352. } while (!kthread_should_stop());
  353. return ret;
  354. }
  355. static irqreturn_t serial_m3110_irq(int irq, void *dev_id)
  356. {
  357. struct uart_max3110 *max = dev_id;
  358. /* max3110's irq is a falling edge, not level triggered,
  359. * so no need to disable the irq */
  360. if (!test_and_set_bit(BIT_IRQ_PENDING, &max->uart_flags))
  361. wake_up(&max->wq);
  362. return IRQ_HANDLED;
  363. }
  364. /* if don't use RX IRQ, then need a thread to polling read */
  365. static int max3110_read_thread(void *_max)
  366. {
  367. struct uart_max3110 *max = _max;
  368. pr_info(PR_FMT "start read thread\n");
  369. do {
  370. /*
  371. * If can't acquire the mutex, it means the main thread
  372. * is running which will also perform the rx job
  373. */
  374. if (mutex_trylock(&max->thread_mutex)) {
  375. max3110_con_receive(max);
  376. mutex_unlock(&max->thread_mutex);
  377. }
  378. set_current_state(TASK_INTERRUPTIBLE);
  379. schedule_timeout(HZ / 20);
  380. } while (!kthread_should_stop());
  381. return 0;
  382. }
  383. static int serial_m3110_startup(struct uart_port *port)
  384. {
  385. struct uart_max3110 *max =
  386. container_of(port, struct uart_max3110, port);
  387. u16 config = 0;
  388. int ret = 0;
  389. if (port->line != 0) {
  390. pr_err(PR_FMT "uart port startup failed\n");
  391. return -1;
  392. }
  393. /* Disable all IRQ and config it to 115200, 8n1 */
  394. config = WC_TAG | WC_FIFO_ENABLE
  395. | WC_1_STOPBITS
  396. | WC_8BIT_WORD
  397. | WC_BAUD_DR2;
  398. /* as we use thread to handle tx/rx, need set low latency */
  399. port->state->port.low_latency = 1;
  400. if (max->irq) {
  401. /* Enable RX IRQ only */
  402. config |= WC_RXA_IRQ_ENABLE;
  403. } else {
  404. /* If IRQ is disabled, start a read thread for input data */
  405. max->read_thread =
  406. kthread_run(max3110_read_thread, max, "max3110_read");
  407. if (IS_ERR(max->read_thread)) {
  408. ret = PTR_ERR(max->read_thread);
  409. max->read_thread = NULL;
  410. pr_err(PR_FMT "Can't create read thread!\n");
  411. return ret;
  412. }
  413. }
  414. ret = max3110_out(max, config);
  415. if (ret) {
  416. if (max->read_thread)
  417. kthread_stop(max->read_thread);
  418. max->read_thread = NULL;
  419. return ret;
  420. }
  421. max->cur_conf = config;
  422. return 0;
  423. }
  424. static void serial_m3110_shutdown(struct uart_port *port)
  425. {
  426. struct uart_max3110 *max =
  427. container_of(port, struct uart_max3110, port);
  428. u16 config;
  429. if (max->read_thread) {
  430. kthread_stop(max->read_thread);
  431. max->read_thread = NULL;
  432. }
  433. /* Disable interrupts from this port */
  434. config = WC_TAG | WC_SW_SHDI;
  435. max3110_out(max, config);
  436. }
  437. static void serial_m3110_release_port(struct uart_port *port)
  438. {
  439. }
  440. static int serial_m3110_request_port(struct uart_port *port)
  441. {
  442. return 0;
  443. }
  444. static void serial_m3110_config_port(struct uart_port *port, int flags)
  445. {
  446. port->type = PORT_MAX3100;
  447. }
  448. static int
  449. serial_m3110_verify_port(struct uart_port *port, struct serial_struct *ser)
  450. {
  451. /* we don't want the core code to modify any port params */
  452. return -EINVAL;
  453. }
  454. static const char *serial_m3110_type(struct uart_port *port)
  455. {
  456. struct uart_max3110 *max =
  457. container_of(port, struct uart_max3110, port);
  458. return max->name;
  459. }
  460. static void
  461. serial_m3110_set_termios(struct uart_port *port, struct ktermios *termios,
  462. struct ktermios *old)
  463. {
  464. struct uart_max3110 *max =
  465. container_of(port, struct uart_max3110, port);
  466. unsigned char cval;
  467. unsigned int baud, parity = 0;
  468. int clk_div = -1;
  469. u16 new_conf = max->cur_conf;
  470. switch (termios->c_cflag & CSIZE) {
  471. case CS7:
  472. cval = UART_LCR_WLEN7;
  473. new_conf |= WC_7BIT_WORD;
  474. break;
  475. default:
  476. /* We only support CS7 & CS8 */
  477. termios->c_cflag &= ~CSIZE;
  478. termios->c_cflag |= CS8;
  479. case CS8:
  480. cval = UART_LCR_WLEN8;
  481. new_conf |= WC_8BIT_WORD;
  482. break;
  483. }
  484. baud = uart_get_baud_rate(port, termios, old, 0, 230400);
  485. /* First calc the div for 1.8MHZ clock case */
  486. switch (baud) {
  487. case 300:
  488. clk_div = WC_BAUD_DR384;
  489. break;
  490. case 600:
  491. clk_div = WC_BAUD_DR192;
  492. break;
  493. case 1200:
  494. clk_div = WC_BAUD_DR96;
  495. break;
  496. case 2400:
  497. clk_div = WC_BAUD_DR48;
  498. break;
  499. case 4800:
  500. clk_div = WC_BAUD_DR24;
  501. break;
  502. case 9600:
  503. clk_div = WC_BAUD_DR12;
  504. break;
  505. case 19200:
  506. clk_div = WC_BAUD_DR6;
  507. break;
  508. case 38400:
  509. clk_div = WC_BAUD_DR3;
  510. break;
  511. case 57600:
  512. clk_div = WC_BAUD_DR2;
  513. break;
  514. case 115200:
  515. clk_div = WC_BAUD_DR1;
  516. break;
  517. case 230400:
  518. if (max->clock & MAX3110_HIGH_CLK)
  519. break;
  520. default:
  521. /* Pick the previous baud rate */
  522. baud = max->baud;
  523. clk_div = max->cur_conf & WC_BAUD_DIV_MASK;
  524. tty_termios_encode_baud_rate(termios, baud, baud);
  525. }
  526. if (max->clock & MAX3110_HIGH_CLK) {
  527. clk_div += 1;
  528. /* High clk version max3110 doesn't support B300 */
  529. if (baud == 300) {
  530. baud = 600;
  531. clk_div = WC_BAUD_DR384;
  532. }
  533. if (baud == 230400)
  534. clk_div = WC_BAUD_DR1;
  535. tty_termios_encode_baud_rate(termios, baud, baud);
  536. }
  537. new_conf = (new_conf & ~WC_BAUD_DIV_MASK) | clk_div;
  538. if (unlikely(termios->c_cflag & CMSPAR))
  539. termios->c_cflag &= ~CMSPAR;
  540. if (termios->c_cflag & CSTOPB)
  541. new_conf |= WC_2_STOPBITS;
  542. else
  543. new_conf &= ~WC_2_STOPBITS;
  544. if (termios->c_cflag & PARENB) {
  545. new_conf |= WC_PARITY_ENABLE;
  546. parity |= UART_LCR_PARITY;
  547. } else
  548. new_conf &= ~WC_PARITY_ENABLE;
  549. if (!(termios->c_cflag & PARODD))
  550. parity |= UART_LCR_EPAR;
  551. max->parity = parity;
  552. uart_update_timeout(port, termios->c_cflag, baud);
  553. new_conf |= WC_TAG;
  554. if (new_conf != max->cur_conf) {
  555. if (!max3110_out(max, new_conf)) {
  556. max->cur_conf = new_conf;
  557. max->baud = baud;
  558. }
  559. }
  560. }
  561. /* Don't handle hw handshaking */
  562. static unsigned int serial_m3110_get_mctrl(struct uart_port *port)
  563. {
  564. return TIOCM_DSR | TIOCM_CAR | TIOCM_DSR;
  565. }
  566. static void serial_m3110_set_mctrl(struct uart_port *port, unsigned int mctrl)
  567. {
  568. }
  569. static void serial_m3110_break_ctl(struct uart_port *port, int break_state)
  570. {
  571. }
  572. static void serial_m3110_pm(struct uart_port *port, unsigned int state,
  573. unsigned int oldstate)
  574. {
  575. }
  576. static void serial_m3110_enable_ms(struct uart_port *port)
  577. {
  578. }
  579. static struct uart_ops serial_m3110_ops = {
  580. .tx_empty = serial_m3110_tx_empty,
  581. .set_mctrl = serial_m3110_set_mctrl,
  582. .get_mctrl = serial_m3110_get_mctrl,
  583. .stop_tx = serial_m3110_stop_tx,
  584. .start_tx = serial_m3110_start_tx,
  585. .stop_rx = serial_m3110_stop_rx,
  586. .enable_ms = serial_m3110_enable_ms,
  587. .break_ctl = serial_m3110_break_ctl,
  588. .startup = serial_m3110_startup,
  589. .shutdown = serial_m3110_shutdown,
  590. .set_termios = serial_m3110_set_termios,
  591. .pm = serial_m3110_pm,
  592. .type = serial_m3110_type,
  593. .release_port = serial_m3110_release_port,
  594. .request_port = serial_m3110_request_port,
  595. .config_port = serial_m3110_config_port,
  596. .verify_port = serial_m3110_verify_port,
  597. };
  598. static struct uart_driver serial_m3110_reg = {
  599. .owner = THIS_MODULE,
  600. .driver_name = "MRST serial",
  601. .dev_name = "ttyS",
  602. .major = TTY_MAJOR,
  603. .minor = 64,
  604. .nr = 1,
  605. .cons = &serial_m3110_console,
  606. };
  607. #ifdef CONFIG_PM_SLEEP
  608. static int serial_m3110_suspend(struct device *dev)
  609. {
  610. struct spi_device *spi = to_spi_device(dev);
  611. struct uart_max3110 *max = spi_get_drvdata(spi);
  612. if (max->irq > 0)
  613. disable_irq(max->irq);
  614. uart_suspend_port(&serial_m3110_reg, &max->port);
  615. max3110_out(max, max->cur_conf | WC_SW_SHDI);
  616. return 0;
  617. }
  618. static int serial_m3110_resume(struct device *dev)
  619. {
  620. struct spi_device *spi = to_spi_device(dev);
  621. struct uart_max3110 *max = spi_get_drvdata(spi);
  622. max3110_out(max, max->cur_conf);
  623. uart_resume_port(&serial_m3110_reg, &max->port);
  624. if (max->irq > 0)
  625. enable_irq(max->irq);
  626. return 0;
  627. }
  628. static SIMPLE_DEV_PM_OPS(serial_m3110_pm_ops, serial_m3110_suspend,
  629. serial_m3110_resume);
  630. #define SERIAL_M3110_PM_OPS (&serial_m3110_pm_ops)
  631. #else
  632. #define SERIAL_M3110_PM_OPS NULL
  633. #endif
  634. static int serial_m3110_probe(struct spi_device *spi)
  635. {
  636. struct uart_max3110 *max;
  637. void *buffer;
  638. u16 res;
  639. int ret = 0;
  640. max = kzalloc(sizeof(*max), GFP_KERNEL);
  641. if (!max)
  642. return -ENOMEM;
  643. /* Set spi info */
  644. spi->bits_per_word = 16;
  645. max->clock = MAX3110_HIGH_CLK;
  646. spi_setup(spi);
  647. max->port.type = PORT_MAX3100;
  648. max->port.fifosize = 2; /* Only have 16b buffer */
  649. max->port.ops = &serial_m3110_ops;
  650. max->port.line = 0;
  651. max->port.dev = &spi->dev;
  652. max->port.uartclk = 115200;
  653. max->spi = spi;
  654. strcpy(max->name, spi->modalias);
  655. max->irq = (u16)spi->irq;
  656. mutex_init(&max->thread_mutex);
  657. mutex_init(&max->io_mutex);
  658. max->word_7bits = 0;
  659. max->parity = 0;
  660. max->baud = 0;
  661. max->cur_conf = 0;
  662. max->uart_flags = 0;
  663. /* Check if reading configuration register returns something sane */
  664. res = RC_TAG;
  665. ret = max3110_write_then_read(max, (u8 *)&res, (u8 *)&res, 2, 0);
  666. if (ret < 0 || res == 0 || res == 0xffff) {
  667. dev_dbg(&spi->dev, "MAX3111 deemed not present (conf reg %04x)",
  668. res);
  669. ret = -ENODEV;
  670. goto err_get_page;
  671. }
  672. buffer = (void *)__get_free_page(GFP_KERNEL);
  673. if (!buffer) {
  674. ret = -ENOMEM;
  675. goto err_get_page;
  676. }
  677. max->con_xmit.buf = buffer;
  678. max->con_xmit.head = 0;
  679. max->con_xmit.tail = 0;
  680. init_waitqueue_head(&max->wq);
  681. max->main_thread = kthread_run(max3110_main_thread,
  682. max, "max3110_main");
  683. if (IS_ERR(max->main_thread)) {
  684. ret = PTR_ERR(max->main_thread);
  685. goto err_kthread;
  686. }
  687. if (max->irq) {
  688. ret = request_irq(max->irq, serial_m3110_irq,
  689. IRQ_TYPE_EDGE_FALLING, "max3110", max);
  690. if (ret) {
  691. max->irq = 0;
  692. dev_warn(&spi->dev,
  693. "unable to allocate IRQ, will use polling method\n");
  694. }
  695. }
  696. spi_set_drvdata(spi, max);
  697. pmax = max;
  698. /* Give membase a psudo value to pass serial_core's check */
  699. max->port.membase = (unsigned char __iomem *)0xff110000;
  700. uart_add_one_port(&serial_m3110_reg, &max->port);
  701. return 0;
  702. err_kthread:
  703. free_page((unsigned long)buffer);
  704. err_get_page:
  705. kfree(max);
  706. return ret;
  707. }
  708. static int serial_m3110_remove(struct spi_device *dev)
  709. {
  710. struct uart_max3110 *max = spi_get_drvdata(dev);
  711. if (!max)
  712. return 0;
  713. uart_remove_one_port(&serial_m3110_reg, &max->port);
  714. free_page((unsigned long)max->con_xmit.buf);
  715. if (max->irq)
  716. free_irq(max->irq, max);
  717. if (max->main_thread)
  718. kthread_stop(max->main_thread);
  719. kfree(max);
  720. return 0;
  721. }
  722. static struct spi_driver uart_max3110_driver = {
  723. .driver = {
  724. .name = "spi_max3111",
  725. .owner = THIS_MODULE,
  726. .pm = SERIAL_M3110_PM_OPS,
  727. },
  728. .probe = serial_m3110_probe,
  729. .remove = serial_m3110_remove,
  730. };
  731. static int __init serial_m3110_init(void)
  732. {
  733. int ret = 0;
  734. ret = uart_register_driver(&serial_m3110_reg);
  735. if (ret)
  736. return ret;
  737. ret = spi_register_driver(&uart_max3110_driver);
  738. if (ret)
  739. uart_unregister_driver(&serial_m3110_reg);
  740. return ret;
  741. }
  742. static void __exit serial_m3110_exit(void)
  743. {
  744. spi_unregister_driver(&uart_max3110_driver);
  745. uart_unregister_driver(&serial_m3110_reg);
  746. }
  747. module_init(serial_m3110_init);
  748. module_exit(serial_m3110_exit);
  749. MODULE_LICENSE("GPL v2");
  750. MODULE_ALIAS("spi:max3110-uart");