mrst_max3110.c 20 KB

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