mrst_max3110.c 20 KB

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