mrst_max3110.c 19 KB

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