mrst_max3110.c 20 KB

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