mrst_max3110.c 20 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913
  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_struct *tty;
  280. char buf[M3110_RX_FIFO_DEPTH];
  281. int r, w, usable;
  282. /* If uart is not opened, just return */
  283. if (!port->state)
  284. return 0;
  285. tty = tty_port_tty_get(&port->state->port);
  286. if (!tty)
  287. return 0;
  288. for (r = 0, w = 0; r < len; r++) {
  289. if (str[r] & MAX3110_BREAK &&
  290. uart_handle_break(port))
  291. continue;
  292. if (str[r] & MAX3110_READ_DATA_AVAILABLE) {
  293. if (uart_handle_sysrq_char(port, str[r] & 0xff))
  294. continue;
  295. buf[w++] = str[r] & 0xff;
  296. }
  297. }
  298. if (!w) {
  299. tty_kref_put(tty);
  300. return 0;
  301. }
  302. for (r = 0; w; r += usable, w -= usable) {
  303. usable = tty_buffer_request_room(tty, w);
  304. if (usable) {
  305. tty_insert_flip_string(tty, buf + r, usable);
  306. port->icount.rx += usable;
  307. }
  308. }
  309. tty_flip_buffer_push(tty);
  310. tty_kref_put(tty);
  311. return r;
  312. }
  313. /*
  314. * This routine will be used in read_thread or RX IRQ handling,
  315. * it will first do one round buffer read(8 words), if there is some
  316. * valid RX data, will try to read 5 more rounds till all data
  317. * is read out.
  318. *
  319. * Use stack space as data buffer to save some system load, and chose
  320. * 504 Btyes as a threadhold to do a bulk push to upper tty layer when
  321. * receiving bulk data, a much bigger buffer may cause stack overflow
  322. */
  323. static void max3110_con_receive(struct uart_max3110 *max)
  324. {
  325. int loop = 1, num;
  326. do {
  327. num = max3110_read_multi(max);
  328. if (num) {
  329. loop = 5;
  330. }
  331. } while (--loop);
  332. }
  333. static int max3110_main_thread(void *_max)
  334. {
  335. struct uart_max3110 *max = _max;
  336. wait_queue_head_t *wq = &max->wq;
  337. int ret = 0;
  338. struct circ_buf *xmit = &max->con_xmit;
  339. pr_info(PR_FMT "start main thread\n");
  340. do {
  341. wait_event_interruptible(*wq,
  342. max->uart_flags || kthread_should_stop());
  343. mutex_lock(&max->thread_mutex);
  344. if (test_and_clear_bit(BIT_IRQ_PENDING, &max->uart_flags))
  345. max3110_con_receive(max);
  346. /* first handle console output */
  347. if (test_and_clear_bit(CON_TX_NEEDED, &max->uart_flags))
  348. send_circ_buf(max, xmit);
  349. /* handle uart output */
  350. if (test_and_clear_bit(UART_TX_NEEDED, &max->uart_flags))
  351. transmit_char(max);
  352. mutex_unlock(&max->thread_mutex);
  353. } while (!kthread_should_stop());
  354. return ret;
  355. }
  356. static irqreturn_t serial_m3110_irq(int irq, void *dev_id)
  357. {
  358. struct uart_max3110 *max = dev_id;
  359. /* max3110's irq is a falling edge, not level triggered,
  360. * so no need to disable the irq */
  361. if (!test_and_set_bit(BIT_IRQ_PENDING, &max->uart_flags))
  362. wake_up(&max->wq);
  363. return IRQ_HANDLED;
  364. }
  365. /* if don't use RX IRQ, then need a thread to polling read */
  366. static int max3110_read_thread(void *_max)
  367. {
  368. struct uart_max3110 *max = _max;
  369. pr_info(PR_FMT "start read thread\n");
  370. do {
  371. /*
  372. * If can't acquire the mutex, it means the main thread
  373. * is running which will also perform the rx job
  374. */
  375. if (mutex_trylock(&max->thread_mutex)) {
  376. max3110_con_receive(max);
  377. mutex_unlock(&max->thread_mutex);
  378. }
  379. set_current_state(TASK_INTERRUPTIBLE);
  380. schedule_timeout(HZ / 20);
  381. } while (!kthread_should_stop());
  382. return 0;
  383. }
  384. static int serial_m3110_startup(struct uart_port *port)
  385. {
  386. struct uart_max3110 *max =
  387. container_of(port, struct uart_max3110, port);
  388. u16 config = 0;
  389. int ret = 0;
  390. if (port->line != 0) {
  391. pr_err(PR_FMT "uart port startup failed\n");
  392. return -1;
  393. }
  394. /* Disable all IRQ and config it to 115200, 8n1 */
  395. config = WC_TAG | WC_FIFO_ENABLE
  396. | WC_1_STOPBITS
  397. | WC_8BIT_WORD
  398. | WC_BAUD_DR2;
  399. /* as we use thread to handle tx/rx, need set low latency */
  400. port->state->port.tty->low_latency = 1;
  401. if (max->irq) {
  402. max->read_thread = NULL;
  403. ret = request_irq(max->irq, serial_m3110_irq,
  404. IRQ_TYPE_EDGE_FALLING, "max3110", max);
  405. if (ret) {
  406. max->irq = 0;
  407. pr_err(PR_FMT "unable to allocate IRQ, polling\n");
  408. } else {
  409. /* Enable RX IRQ only */
  410. config |= WC_RXA_IRQ_ENABLE;
  411. }
  412. }
  413. if (max->irq == 0) {
  414. /* If IRQ is disabled, start a read thread for input data */
  415. max->read_thread =
  416. kthread_run(max3110_read_thread, max, "max3110_read");
  417. if (IS_ERR(max->read_thread)) {
  418. ret = PTR_ERR(max->read_thread);
  419. max->read_thread = NULL;
  420. pr_err(PR_FMT "Can't create read thread!\n");
  421. return ret;
  422. }
  423. }
  424. ret = max3110_out(max, config);
  425. if (ret) {
  426. if (max->irq)
  427. free_irq(max->irq, max);
  428. if (max->read_thread)
  429. kthread_stop(max->read_thread);
  430. max->read_thread = NULL;
  431. return ret;
  432. }
  433. max->cur_conf = config;
  434. return 0;
  435. }
  436. static void serial_m3110_shutdown(struct uart_port *port)
  437. {
  438. struct uart_max3110 *max =
  439. container_of(port, struct uart_max3110, port);
  440. u16 config;
  441. if (max->read_thread) {
  442. kthread_stop(max->read_thread);
  443. max->read_thread = NULL;
  444. }
  445. if (max->irq)
  446. free_irq(max->irq, max);
  447. /* Disable interrupts from this port */
  448. config = WC_TAG | WC_SW_SHDI;
  449. max3110_out(max, config);
  450. }
  451. static void serial_m3110_release_port(struct uart_port *port)
  452. {
  453. }
  454. static int serial_m3110_request_port(struct uart_port *port)
  455. {
  456. return 0;
  457. }
  458. static void serial_m3110_config_port(struct uart_port *port, int flags)
  459. {
  460. port->type = PORT_MAX3100;
  461. }
  462. static int
  463. serial_m3110_verify_port(struct uart_port *port, struct serial_struct *ser)
  464. {
  465. /* we don't want the core code to modify any port params */
  466. return -EINVAL;
  467. }
  468. static const char *serial_m3110_type(struct uart_port *port)
  469. {
  470. struct uart_max3110 *max =
  471. container_of(port, struct uart_max3110, port);
  472. return max->name;
  473. }
  474. static void
  475. serial_m3110_set_termios(struct uart_port *port, struct ktermios *termios,
  476. struct ktermios *old)
  477. {
  478. struct uart_max3110 *max =
  479. container_of(port, struct uart_max3110, port);
  480. unsigned char cval;
  481. unsigned int baud, parity = 0;
  482. int clk_div = -1;
  483. u16 new_conf = max->cur_conf;
  484. switch (termios->c_cflag & CSIZE) {
  485. case CS7:
  486. cval = UART_LCR_WLEN7;
  487. new_conf |= WC_7BIT_WORD;
  488. break;
  489. default:
  490. /* We only support CS7 & CS8 */
  491. termios->c_cflag &= ~CSIZE;
  492. termios->c_cflag |= CS8;
  493. case CS8:
  494. cval = UART_LCR_WLEN8;
  495. new_conf |= WC_8BIT_WORD;
  496. break;
  497. }
  498. baud = uart_get_baud_rate(port, termios, old, 0, 230400);
  499. /* First calc the div for 1.8MHZ clock case */
  500. switch (baud) {
  501. case 300:
  502. clk_div = WC_BAUD_DR384;
  503. break;
  504. case 600:
  505. clk_div = WC_BAUD_DR192;
  506. break;
  507. case 1200:
  508. clk_div = WC_BAUD_DR96;
  509. break;
  510. case 2400:
  511. clk_div = WC_BAUD_DR48;
  512. break;
  513. case 4800:
  514. clk_div = WC_BAUD_DR24;
  515. break;
  516. case 9600:
  517. clk_div = WC_BAUD_DR12;
  518. break;
  519. case 19200:
  520. clk_div = WC_BAUD_DR6;
  521. break;
  522. case 38400:
  523. clk_div = WC_BAUD_DR3;
  524. break;
  525. case 57600:
  526. clk_div = WC_BAUD_DR2;
  527. break;
  528. case 115200:
  529. clk_div = WC_BAUD_DR1;
  530. break;
  531. case 230400:
  532. if (max->clock & MAX3110_HIGH_CLK)
  533. break;
  534. default:
  535. /* Pick the previous baud rate */
  536. baud = max->baud;
  537. clk_div = max->cur_conf & WC_BAUD_DIV_MASK;
  538. tty_termios_encode_baud_rate(termios, baud, baud);
  539. }
  540. if (max->clock & MAX3110_HIGH_CLK) {
  541. clk_div += 1;
  542. /* High clk version max3110 doesn't support B300 */
  543. if (baud == 300) {
  544. baud = 600;
  545. clk_div = WC_BAUD_DR384;
  546. }
  547. if (baud == 230400)
  548. clk_div = WC_BAUD_DR1;
  549. tty_termios_encode_baud_rate(termios, baud, baud);
  550. }
  551. new_conf = (new_conf & ~WC_BAUD_DIV_MASK) | clk_div;
  552. if (unlikely(termios->c_cflag & CMSPAR))
  553. termios->c_cflag &= ~CMSPAR;
  554. if (termios->c_cflag & CSTOPB)
  555. new_conf |= WC_2_STOPBITS;
  556. else
  557. new_conf &= ~WC_2_STOPBITS;
  558. if (termios->c_cflag & PARENB) {
  559. new_conf |= WC_PARITY_ENABLE;
  560. parity |= UART_LCR_PARITY;
  561. } else
  562. new_conf &= ~WC_PARITY_ENABLE;
  563. if (!(termios->c_cflag & PARODD))
  564. parity |= UART_LCR_EPAR;
  565. max->parity = parity;
  566. uart_update_timeout(port, termios->c_cflag, baud);
  567. new_conf |= WC_TAG;
  568. if (new_conf != max->cur_conf) {
  569. if (!max3110_out(max, new_conf)) {
  570. max->cur_conf = new_conf;
  571. max->baud = baud;
  572. }
  573. }
  574. }
  575. /* Don't handle hw handshaking */
  576. static unsigned int serial_m3110_get_mctrl(struct uart_port *port)
  577. {
  578. return TIOCM_DSR | TIOCM_CAR | TIOCM_DSR;
  579. }
  580. static void serial_m3110_set_mctrl(struct uart_port *port, unsigned int mctrl)
  581. {
  582. }
  583. static void serial_m3110_break_ctl(struct uart_port *port, int break_state)
  584. {
  585. }
  586. static void serial_m3110_pm(struct uart_port *port, unsigned int state,
  587. unsigned int oldstate)
  588. {
  589. }
  590. static void serial_m3110_enable_ms(struct uart_port *port)
  591. {
  592. }
  593. struct uart_ops serial_m3110_ops = {
  594. .tx_empty = serial_m3110_tx_empty,
  595. .set_mctrl = serial_m3110_set_mctrl,
  596. .get_mctrl = serial_m3110_get_mctrl,
  597. .stop_tx = serial_m3110_stop_tx,
  598. .start_tx = serial_m3110_start_tx,
  599. .stop_rx = serial_m3110_stop_rx,
  600. .enable_ms = serial_m3110_enable_ms,
  601. .break_ctl = serial_m3110_break_ctl,
  602. .startup = serial_m3110_startup,
  603. .shutdown = serial_m3110_shutdown,
  604. .set_termios = serial_m3110_set_termios,
  605. .pm = serial_m3110_pm,
  606. .type = serial_m3110_type,
  607. .release_port = serial_m3110_release_port,
  608. .request_port = serial_m3110_request_port,
  609. .config_port = serial_m3110_config_port,
  610. .verify_port = serial_m3110_verify_port,
  611. };
  612. static struct uart_driver serial_m3110_reg = {
  613. .owner = THIS_MODULE,
  614. .driver_name = "MRST serial",
  615. .dev_name = "ttyS",
  616. .major = TTY_MAJOR,
  617. .minor = 64,
  618. .nr = 1,
  619. .cons = &serial_m3110_console,
  620. };
  621. #ifdef CONFIG_PM
  622. static int serial_m3110_suspend(struct spi_device *spi, pm_message_t state)
  623. {
  624. struct uart_max3110 *max = spi_get_drvdata(spi);
  625. disable_irq(max->irq);
  626. uart_suspend_port(&serial_m3110_reg, &max->port);
  627. max3110_out(max, max->cur_conf | WC_SW_SHDI);
  628. return 0;
  629. }
  630. static int serial_m3110_resume(struct spi_device *spi)
  631. {
  632. struct uart_max3110 *max = spi_get_drvdata(spi);
  633. max3110_out(max, max->cur_conf);
  634. uart_resume_port(&serial_m3110_reg, &max->port);
  635. enable_irq(max->irq);
  636. return 0;
  637. }
  638. #else
  639. #define serial_m3110_suspend NULL
  640. #define serial_m3110_resume NULL
  641. #endif
  642. static int __devinit serial_m3110_probe(struct spi_device *spi)
  643. {
  644. struct uart_max3110 *max;
  645. void *buffer;
  646. u16 res;
  647. int ret = 0;
  648. max = kzalloc(sizeof(*max), GFP_KERNEL);
  649. if (!max)
  650. return -ENOMEM;
  651. /* Set spi info */
  652. spi->bits_per_word = 16;
  653. max->clock = MAX3110_HIGH_CLK;
  654. spi_setup(spi);
  655. max->port.type = PORT_MAX3100;
  656. max->port.fifosize = 2; /* Only have 16b buffer */
  657. max->port.ops = &serial_m3110_ops;
  658. max->port.line = 0;
  659. max->port.dev = &spi->dev;
  660. max->port.uartclk = 115200;
  661. max->spi = spi;
  662. strcpy(max->name, spi->modalias);
  663. max->irq = (u16)spi->irq;
  664. mutex_init(&max->thread_mutex);
  665. max->word_7bits = 0;
  666. max->parity = 0;
  667. max->baud = 0;
  668. max->cur_conf = 0;
  669. max->uart_flags = 0;
  670. /* Check if reading configuration register returns something sane */
  671. res = RC_TAG;
  672. ret = max3110_write_then_read(max, (u8 *)&res, (u8 *)&res, 2, 0);
  673. if (ret < 0 || res == 0 || res == 0xffff) {
  674. dev_dbg(&spi->dev, "MAX3111 deemed not present (conf reg %04x)",
  675. res);
  676. ret = -ENODEV;
  677. goto err_get_page;
  678. }
  679. buffer = (void *)__get_free_page(GFP_KERNEL);
  680. if (!buffer) {
  681. ret = -ENOMEM;
  682. goto err_get_page;
  683. }
  684. max->con_xmit.buf = buffer;
  685. max->con_xmit.head = 0;
  686. max->con_xmit.tail = 0;
  687. init_waitqueue_head(&max->wq);
  688. max->main_thread = kthread_run(max3110_main_thread,
  689. max, "max3110_main");
  690. if (IS_ERR(max->main_thread)) {
  691. ret = PTR_ERR(max->main_thread);
  692. goto err_kthread;
  693. }
  694. spi_set_drvdata(spi, max);
  695. pmax = max;
  696. /* Give membase a psudo value to pass serial_core's check */
  697. max->port.membase = (void *)0xff110000;
  698. uart_add_one_port(&serial_m3110_reg, &max->port);
  699. return 0;
  700. err_kthread:
  701. free_page((unsigned long)buffer);
  702. err_get_page:
  703. kfree(max);
  704. return ret;
  705. }
  706. static int __devexit serial_m3110_remove(struct spi_device *dev)
  707. {
  708. struct uart_max3110 *max = spi_get_drvdata(dev);
  709. if (!max)
  710. return 0;
  711. uart_remove_one_port(&serial_m3110_reg, &max->port);
  712. free_page((unsigned long)max->con_xmit.buf);
  713. if (max->main_thread)
  714. kthread_stop(max->main_thread);
  715. kfree(max);
  716. return 0;
  717. }
  718. static struct spi_driver uart_max3110_driver = {
  719. .driver = {
  720. .name = "spi_max3111",
  721. .owner = THIS_MODULE,
  722. },
  723. .probe = serial_m3110_probe,
  724. .remove = __devexit_p(serial_m3110_remove),
  725. .suspend = serial_m3110_suspend,
  726. .resume = serial_m3110_resume,
  727. };
  728. static int __init serial_m3110_init(void)
  729. {
  730. int ret = 0;
  731. ret = uart_register_driver(&serial_m3110_reg);
  732. if (ret)
  733. return ret;
  734. ret = spi_register_driver(&uart_max3110_driver);
  735. if (ret)
  736. uart_unregister_driver(&serial_m3110_reg);
  737. return ret;
  738. }
  739. static void __exit serial_m3110_exit(void)
  740. {
  741. spi_unregister_driver(&uart_max3110_driver);
  742. uart_unregister_driver(&serial_m3110_reg);
  743. }
  744. module_init(serial_m3110_init);
  745. module_exit(serial_m3110_exit);
  746. MODULE_LICENSE("GPL v2");
  747. MODULE_ALIAS("spi:max3110-uart");