sdio_uart.c 28 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166
  1. // SPDX-License-Identifier: GPL-2.0-or-later
  2. /*
  3. * SDIO UART/GPS driver
  4. *
  5. * Based on drivers/serial/8250.c and drivers/serial/serial_core.c
  6. * by Russell King.
  7. *
  8. * Author: Nicolas Pitre
  9. * Created: June 15, 2007
  10. * Copyright: MontaVista Software, Inc.
  11. */
  12. /*
  13. * Note: Although this driver assumes a 16550A-like UART implementation,
  14. * it is not possible to leverage the common 8250/16550 driver, nor the
  15. * core UART infrastructure, as they assumes direct access to the hardware
  16. * registers, often under a spinlock. This is not possible in the SDIO
  17. * context as SDIO access functions must be able to sleep.
  18. *
  19. * Because we need to lock the SDIO host to ensure an exclusive access to
  20. * the card, we simply rely on that lock to also prevent and serialize
  21. * concurrent access to the same port.
  22. */
  23. #include <linux/module.h>
  24. #include <linux/init.h>
  25. #include <linux/kernel.h>
  26. #include <linux/sched.h>
  27. #include <linux/mutex.h>
  28. #include <linux/seq_file.h>
  29. #include <linux/serial.h>
  30. #include <linux/serial_reg.h>
  31. #include <linux/circ_buf.h>
  32. #include <linux/tty.h>
  33. #include <linux/tty_flip.h>
  34. #include <linux/kfifo.h>
  35. #include <linux/slab.h>
  36. #include <linux/mmc/core.h>
  37. #include <linux/mmc/card.h>
  38. #include <linux/mmc/sdio_func.h>
  39. #include <linux/mmc/sdio_ids.h>
  40. #define UART_NR 8 /* Number of UARTs this driver can handle */
  41. #define FIFO_SIZE PAGE_SIZE
  42. #define WAKEUP_CHARS 256
  43. struct uart_icount {
  44. __u32 cts;
  45. __u32 dsr;
  46. __u32 rng;
  47. __u32 dcd;
  48. __u32 rx;
  49. __u32 tx;
  50. __u32 frame;
  51. __u32 overrun;
  52. __u32 parity;
  53. __u32 brk;
  54. };
  55. struct sdio_uart_port {
  56. struct tty_port port;
  57. unsigned int index;
  58. struct sdio_func *func;
  59. struct mutex func_lock;
  60. struct task_struct *in_sdio_uart_irq;
  61. unsigned int regs_offset;
  62. struct kfifo xmit_fifo;
  63. spinlock_t write_lock;
  64. struct uart_icount icount;
  65. unsigned int uartclk;
  66. unsigned int mctrl;
  67. unsigned int rx_mctrl;
  68. unsigned int read_status_mask;
  69. unsigned int ignore_status_mask;
  70. unsigned char x_char;
  71. unsigned char ier;
  72. unsigned char lcr;
  73. };
  74. static struct sdio_uart_port *sdio_uart_table[UART_NR];
  75. static DEFINE_SPINLOCK(sdio_uart_table_lock);
  76. static int sdio_uart_add_port(struct sdio_uart_port *port)
  77. {
  78. int index, ret = -EBUSY;
  79. mutex_init(&port->func_lock);
  80. spin_lock_init(&port->write_lock);
  81. if (kfifo_alloc(&port->xmit_fifo, FIFO_SIZE, GFP_KERNEL))
  82. return -ENOMEM;
  83. spin_lock(&sdio_uart_table_lock);
  84. for (index = 0; index < UART_NR; index++) {
  85. if (!sdio_uart_table[index]) {
  86. port->index = index;
  87. sdio_uart_table[index] = port;
  88. ret = 0;
  89. break;
  90. }
  91. }
  92. spin_unlock(&sdio_uart_table_lock);
  93. return ret;
  94. }
  95. static struct sdio_uart_port *sdio_uart_port_get(unsigned index)
  96. {
  97. struct sdio_uart_port *port;
  98. if (index >= UART_NR)
  99. return NULL;
  100. spin_lock(&sdio_uart_table_lock);
  101. port = sdio_uart_table[index];
  102. if (port)
  103. tty_port_get(&port->port);
  104. spin_unlock(&sdio_uart_table_lock);
  105. return port;
  106. }
  107. static void sdio_uart_port_put(struct sdio_uart_port *port)
  108. {
  109. tty_port_put(&port->port);
  110. }
  111. static void sdio_uart_port_remove(struct sdio_uart_port *port)
  112. {
  113. struct sdio_func *func;
  114. spin_lock(&sdio_uart_table_lock);
  115. sdio_uart_table[port->index] = NULL;
  116. spin_unlock(&sdio_uart_table_lock);
  117. /*
  118. * We're killing a port that potentially still is in use by
  119. * the tty layer. Be careful to prevent any further access
  120. * to the SDIO function and arrange for the tty layer to
  121. * give up on that port ASAP.
  122. * Beware: the lock ordering is critical.
  123. */
  124. mutex_lock(&port->port.mutex);
  125. mutex_lock(&port->func_lock);
  126. func = port->func;
  127. sdio_claim_host(func);
  128. port->func = NULL;
  129. mutex_unlock(&port->func_lock);
  130. /* tty_hangup is async so is this safe as is ?? */
  131. tty_port_tty_hangup(&port->port, false);
  132. mutex_unlock(&port->port.mutex);
  133. sdio_release_irq(func);
  134. sdio_disable_func(func);
  135. sdio_release_host(func);
  136. sdio_uart_port_put(port);
  137. }
  138. static int sdio_uart_claim_func(struct sdio_uart_port *port)
  139. {
  140. mutex_lock(&port->func_lock);
  141. if (unlikely(!port->func)) {
  142. mutex_unlock(&port->func_lock);
  143. return -ENODEV;
  144. }
  145. if (likely(port->in_sdio_uart_irq != current))
  146. sdio_claim_host(port->func);
  147. mutex_unlock(&port->func_lock);
  148. return 0;
  149. }
  150. static inline void sdio_uart_release_func(struct sdio_uart_port *port)
  151. {
  152. if (likely(port->in_sdio_uart_irq != current))
  153. sdio_release_host(port->func);
  154. }
  155. static inline u8 sdio_in(struct sdio_uart_port *port, int offset)
  156. {
  157. return sdio_readb(port->func, port->regs_offset + offset, NULL);
  158. }
  159. static inline void sdio_out(struct sdio_uart_port *port, int offset, int value)
  160. {
  161. sdio_writeb(port->func, value, port->regs_offset + offset, NULL);
  162. }
  163. static unsigned int sdio_uart_get_mctrl(struct sdio_uart_port *port)
  164. {
  165. unsigned int ret;
  166. u8 status;
  167. /* FIXME: What stops this losing the delta bits and breaking
  168. sdio_uart_check_modem_status ? */
  169. status = sdio_in(port, UART_MSR);
  170. ret = 0;
  171. if (status & UART_MSR_DCD)
  172. ret |= TIOCM_CAR;
  173. if (status & UART_MSR_RI)
  174. ret |= TIOCM_RNG;
  175. if (status & UART_MSR_DSR)
  176. ret |= TIOCM_DSR;
  177. if (status & UART_MSR_CTS)
  178. ret |= TIOCM_CTS;
  179. return ret;
  180. }
  181. static void sdio_uart_write_mctrl(struct sdio_uart_port *port,
  182. unsigned int mctrl)
  183. {
  184. unsigned char mcr = 0;
  185. if (mctrl & TIOCM_RTS)
  186. mcr |= UART_MCR_RTS;
  187. if (mctrl & TIOCM_DTR)
  188. mcr |= UART_MCR_DTR;
  189. if (mctrl & TIOCM_OUT1)
  190. mcr |= UART_MCR_OUT1;
  191. if (mctrl & TIOCM_OUT2)
  192. mcr |= UART_MCR_OUT2;
  193. if (mctrl & TIOCM_LOOP)
  194. mcr |= UART_MCR_LOOP;
  195. sdio_out(port, UART_MCR, mcr);
  196. }
  197. static inline void sdio_uart_update_mctrl(struct sdio_uart_port *port,
  198. unsigned int set, unsigned int clear)
  199. {
  200. unsigned int old;
  201. old = port->mctrl;
  202. port->mctrl = (old & ~clear) | set;
  203. if (old != port->mctrl)
  204. sdio_uart_write_mctrl(port, port->mctrl);
  205. }
  206. #define sdio_uart_set_mctrl(port, x) sdio_uart_update_mctrl(port, x, 0)
  207. #define sdio_uart_clear_mctrl(port, x) sdio_uart_update_mctrl(port, 0, x)
  208. static void sdio_uart_change_speed(struct sdio_uart_port *port,
  209. struct ktermios *termios,
  210. const struct ktermios *old)
  211. {
  212. unsigned char cval, fcr = 0;
  213. unsigned int baud, quot;
  214. cval = UART_LCR_WLEN(tty_get_char_size(termios->c_cflag));
  215. if (termios->c_cflag & CSTOPB)
  216. cval |= UART_LCR_STOP;
  217. if (termios->c_cflag & PARENB)
  218. cval |= UART_LCR_PARITY;
  219. if (!(termios->c_cflag & PARODD))
  220. cval |= UART_LCR_EPAR;
  221. for (;;) {
  222. baud = tty_termios_baud_rate(termios);
  223. if (baud == 0)
  224. baud = 9600; /* Special case: B0 rate. */
  225. if (baud <= port->uartclk)
  226. break;
  227. /*
  228. * Oops, the quotient was zero. Try again with the old
  229. * baud rate if possible, otherwise default to 9600.
  230. */
  231. termios->c_cflag &= ~CBAUD;
  232. if (old) {
  233. termios->c_cflag |= old->c_cflag & CBAUD;
  234. old = NULL;
  235. } else
  236. termios->c_cflag |= B9600;
  237. }
  238. quot = (2 * port->uartclk + baud) / (2 * baud);
  239. if (baud < 2400)
  240. fcr = UART_FCR_ENABLE_FIFO | UART_FCR_TRIGGER_1;
  241. else
  242. fcr = UART_FCR_ENABLE_FIFO | UART_FCR_R_TRIG_10;
  243. port->read_status_mask = UART_LSR_OE | UART_LSR_THRE | UART_LSR_DR;
  244. if (termios->c_iflag & INPCK)
  245. port->read_status_mask |= UART_LSR_FE | UART_LSR_PE;
  246. if (termios->c_iflag & (BRKINT | PARMRK))
  247. port->read_status_mask |= UART_LSR_BI;
  248. /*
  249. * Characters to ignore
  250. */
  251. port->ignore_status_mask = 0;
  252. if (termios->c_iflag & IGNPAR)
  253. port->ignore_status_mask |= UART_LSR_PE | UART_LSR_FE;
  254. if (termios->c_iflag & IGNBRK) {
  255. port->ignore_status_mask |= UART_LSR_BI;
  256. /*
  257. * If we're ignoring parity and break indicators,
  258. * ignore overruns too (for real raw support).
  259. */
  260. if (termios->c_iflag & IGNPAR)
  261. port->ignore_status_mask |= UART_LSR_OE;
  262. }
  263. /*
  264. * ignore all characters if CREAD is not set
  265. */
  266. if ((termios->c_cflag & CREAD) == 0)
  267. port->ignore_status_mask |= UART_LSR_DR;
  268. /*
  269. * CTS flow control flag and modem status interrupts
  270. */
  271. port->ier &= ~UART_IER_MSI;
  272. if ((termios->c_cflag & CRTSCTS) || !(termios->c_cflag & CLOCAL))
  273. port->ier |= UART_IER_MSI;
  274. port->lcr = cval;
  275. sdio_out(port, UART_IER, port->ier);
  276. sdio_out(port, UART_LCR, cval | UART_LCR_DLAB);
  277. sdio_out(port, UART_DLL, quot & 0xff);
  278. sdio_out(port, UART_DLM, quot >> 8);
  279. sdio_out(port, UART_LCR, cval);
  280. sdio_out(port, UART_FCR, fcr);
  281. sdio_uart_write_mctrl(port, port->mctrl);
  282. }
  283. static void sdio_uart_start_tx(struct sdio_uart_port *port)
  284. {
  285. if (!(port->ier & UART_IER_THRI)) {
  286. port->ier |= UART_IER_THRI;
  287. sdio_out(port, UART_IER, port->ier);
  288. }
  289. }
  290. static void sdio_uart_stop_tx(struct sdio_uart_port *port)
  291. {
  292. if (port->ier & UART_IER_THRI) {
  293. port->ier &= ~UART_IER_THRI;
  294. sdio_out(port, UART_IER, port->ier);
  295. }
  296. }
  297. static void sdio_uart_stop_rx(struct sdio_uart_port *port)
  298. {
  299. port->ier &= ~UART_IER_RLSI;
  300. port->read_status_mask &= ~UART_LSR_DR;
  301. sdio_out(port, UART_IER, port->ier);
  302. }
  303. static void sdio_uart_receive_chars(struct sdio_uart_port *port, u8 *status)
  304. {
  305. int max_count = 256;
  306. do {
  307. u8 ch = sdio_in(port, UART_RX);
  308. u8 flag = TTY_NORMAL;
  309. port->icount.rx++;
  310. if (unlikely(*status & (UART_LSR_BI | UART_LSR_PE |
  311. UART_LSR_FE | UART_LSR_OE))) {
  312. /*
  313. * For statistics only
  314. */
  315. if (*status & UART_LSR_BI) {
  316. *status &= ~(UART_LSR_FE | UART_LSR_PE);
  317. port->icount.brk++;
  318. } else if (*status & UART_LSR_PE)
  319. port->icount.parity++;
  320. else if (*status & UART_LSR_FE)
  321. port->icount.frame++;
  322. if (*status & UART_LSR_OE)
  323. port->icount.overrun++;
  324. /*
  325. * Mask off conditions which should be ignored.
  326. */
  327. *status &= port->read_status_mask;
  328. if (*status & UART_LSR_BI)
  329. flag = TTY_BREAK;
  330. else if (*status & UART_LSR_PE)
  331. flag = TTY_PARITY;
  332. else if (*status & UART_LSR_FE)
  333. flag = TTY_FRAME;
  334. }
  335. if ((*status & port->ignore_status_mask & ~UART_LSR_OE) == 0)
  336. tty_insert_flip_char(&port->port, ch, flag);
  337. /*
  338. * Overrun is special. Since it's reported immediately,
  339. * it doesn't affect the current character.
  340. */
  341. if (*status & ~port->ignore_status_mask & UART_LSR_OE)
  342. tty_insert_flip_char(&port->port, 0, TTY_OVERRUN);
  343. *status = sdio_in(port, UART_LSR);
  344. } while ((*status & UART_LSR_DR) && (max_count-- > 0));
  345. tty_flip_buffer_push(&port->port);
  346. }
  347. static void sdio_uart_transmit_chars(struct sdio_uart_port *port)
  348. {
  349. struct kfifo *xmit = &port->xmit_fifo;
  350. int count;
  351. struct tty_struct *tty;
  352. u8 iobuf[16];
  353. int len;
  354. if (port->x_char) {
  355. sdio_out(port, UART_TX, port->x_char);
  356. port->icount.tx++;
  357. port->x_char = 0;
  358. return;
  359. }
  360. tty = tty_port_tty_get(&port->port);
  361. if (tty == NULL || !kfifo_len(xmit) ||
  362. tty->flow.stopped || tty->hw_stopped) {
  363. sdio_uart_stop_tx(port);
  364. tty_kref_put(tty);
  365. return;
  366. }
  367. len = kfifo_out_locked(xmit, iobuf, 16, &port->write_lock);
  368. for (count = 0; count < len; count++) {
  369. sdio_out(port, UART_TX, iobuf[count]);
  370. port->icount.tx++;
  371. }
  372. len = kfifo_len(xmit);
  373. if (len < WAKEUP_CHARS) {
  374. tty_wakeup(tty);
  375. if (len == 0)
  376. sdio_uart_stop_tx(port);
  377. }
  378. tty_kref_put(tty);
  379. }
  380. static void sdio_uart_check_modem_status(struct sdio_uart_port *port)
  381. {
  382. struct tty_struct *tty;
  383. u8 status;
  384. status = sdio_in(port, UART_MSR);
  385. if ((status & UART_MSR_ANY_DELTA) == 0)
  386. return;
  387. if (status & UART_MSR_TERI)
  388. port->icount.rng++;
  389. if (status & UART_MSR_DDSR)
  390. port->icount.dsr++;
  391. if (status & UART_MSR_DDCD) {
  392. port->icount.dcd++;
  393. /* DCD raise - wake for open */
  394. if (status & UART_MSR_DCD)
  395. wake_up_interruptible(&port->port.open_wait);
  396. else {
  397. /* DCD drop - hang up if tty attached */
  398. tty_port_tty_hangup(&port->port, false);
  399. }
  400. }
  401. if (status & UART_MSR_DCTS) {
  402. port->icount.cts++;
  403. tty = tty_port_tty_get(&port->port);
  404. if (tty && C_CRTSCTS(tty)) {
  405. int cts = (status & UART_MSR_CTS);
  406. if (tty->hw_stopped) {
  407. if (cts) {
  408. tty->hw_stopped = false;
  409. sdio_uart_start_tx(port);
  410. tty_wakeup(tty);
  411. }
  412. } else {
  413. if (!cts) {
  414. tty->hw_stopped = true;
  415. sdio_uart_stop_tx(port);
  416. }
  417. }
  418. }
  419. tty_kref_put(tty);
  420. }
  421. }
  422. /*
  423. * This handles the interrupt from one port.
  424. */
  425. static void sdio_uart_irq(struct sdio_func *func)
  426. {
  427. struct sdio_uart_port *port = sdio_get_drvdata(func);
  428. u8 iir, lsr;
  429. /*
  430. * In a few places sdio_uart_irq() is called directly instead of
  431. * waiting for the actual interrupt to be raised and the SDIO IRQ
  432. * thread scheduled in order to reduce latency. However, some
  433. * interaction with the tty core may end up calling us back
  434. * (serial echo, flow control, etc.) through those same places
  435. * causing undesirable effects. Let's stop the recursion here.
  436. */
  437. if (unlikely(port->in_sdio_uart_irq == current))
  438. return;
  439. iir = sdio_in(port, UART_IIR);
  440. if (iir & UART_IIR_NO_INT)
  441. return;
  442. port->in_sdio_uart_irq = current;
  443. lsr = sdio_in(port, UART_LSR);
  444. if (lsr & UART_LSR_DR)
  445. sdio_uart_receive_chars(port, &lsr);
  446. sdio_uart_check_modem_status(port);
  447. if (lsr & UART_LSR_THRE)
  448. sdio_uart_transmit_chars(port);
  449. port->in_sdio_uart_irq = NULL;
  450. }
  451. static bool uart_carrier_raised(struct tty_port *tport)
  452. {
  453. struct sdio_uart_port *port =
  454. container_of(tport, struct sdio_uart_port, port);
  455. unsigned int ret = sdio_uart_claim_func(port);
  456. if (ret) /* Missing hardware shouldn't block for carrier */
  457. return 1;
  458. ret = sdio_uart_get_mctrl(port);
  459. sdio_uart_release_func(port);
  460. return ret & TIOCM_CAR;
  461. }
  462. /**
  463. * uart_dtr_rts - port helper to set uart signals
  464. * @tport: tty port to be updated
  465. * @active: set to turn on DTR/RTS
  466. *
  467. * Called by the tty port helpers when the modem signals need to be
  468. * adjusted during an open, close and hangup.
  469. */
  470. static void uart_dtr_rts(struct tty_port *tport, bool active)
  471. {
  472. struct sdio_uart_port *port =
  473. container_of(tport, struct sdio_uart_port, port);
  474. int ret = sdio_uart_claim_func(port);
  475. if (ret)
  476. return;
  477. if (!active)
  478. sdio_uart_clear_mctrl(port, TIOCM_DTR | TIOCM_RTS);
  479. else
  480. sdio_uart_set_mctrl(port, TIOCM_DTR | TIOCM_RTS);
  481. sdio_uart_release_func(port);
  482. }
  483. /**
  484. * sdio_uart_activate - start up hardware
  485. * @tport: tty port to activate
  486. * @tty: tty bound to this port
  487. *
  488. * Activate a tty port. The port locking guarantees us this will be
  489. * run exactly once per set of opens, and if successful will see the
  490. * shutdown method run exactly once to match. Start up and shutdown are
  491. * protected from each other by the internal locking and will not run
  492. * at the same time even during a hangup event.
  493. *
  494. * If we successfully start up the port we take an extra kref as we
  495. * will keep it around until shutdown when the kref is dropped.
  496. */
  497. static int sdio_uart_activate(struct tty_port *tport, struct tty_struct *tty)
  498. {
  499. struct sdio_uart_port *port =
  500. container_of(tport, struct sdio_uart_port, port);
  501. int ret;
  502. /*
  503. * Set the TTY IO error marker - we will only clear this
  504. * once we have successfully opened the port.
  505. */
  506. set_bit(TTY_IO_ERROR, &tty->flags);
  507. kfifo_reset(&port->xmit_fifo);
  508. ret = sdio_uart_claim_func(port);
  509. if (ret)
  510. return ret;
  511. ret = sdio_enable_func(port->func);
  512. if (ret)
  513. goto err1;
  514. ret = sdio_claim_irq(port->func, sdio_uart_irq);
  515. if (ret)
  516. goto err2;
  517. /*
  518. * Clear the FIFO buffers and disable them.
  519. * (they will be reenabled in sdio_change_speed())
  520. */
  521. sdio_out(port, UART_FCR, UART_FCR_ENABLE_FIFO);
  522. sdio_out(port, UART_FCR, UART_FCR_ENABLE_FIFO |
  523. UART_FCR_CLEAR_RCVR | UART_FCR_CLEAR_XMIT);
  524. sdio_out(port, UART_FCR, 0);
  525. /*
  526. * Clear the interrupt registers.
  527. */
  528. (void) sdio_in(port, UART_LSR);
  529. (void) sdio_in(port, UART_RX);
  530. (void) sdio_in(port, UART_IIR);
  531. (void) sdio_in(port, UART_MSR);
  532. /*
  533. * Now, initialize the UART
  534. */
  535. sdio_out(port, UART_LCR, UART_LCR_WLEN8);
  536. port->ier = UART_IER_RLSI|UART_IER_RDI|UART_IER_RTOIE|UART_IER_UUE;
  537. port->mctrl = TIOCM_OUT2;
  538. sdio_uart_change_speed(port, &tty->termios, NULL);
  539. if (C_BAUD(tty))
  540. sdio_uart_set_mctrl(port, TIOCM_RTS | TIOCM_DTR);
  541. if (C_CRTSCTS(tty))
  542. if (!(sdio_uart_get_mctrl(port) & TIOCM_CTS))
  543. tty->hw_stopped = true;
  544. clear_bit(TTY_IO_ERROR, &tty->flags);
  545. /* Kick the IRQ handler once while we're still holding the host lock */
  546. sdio_uart_irq(port->func);
  547. sdio_uart_release_func(port);
  548. return 0;
  549. err2:
  550. sdio_disable_func(port->func);
  551. err1:
  552. sdio_uart_release_func(port);
  553. return ret;
  554. }
  555. /**
  556. * sdio_uart_shutdown - stop hardware
  557. * @tport: tty port to shut down
  558. *
  559. * Deactivate a tty port. The port locking guarantees us this will be
  560. * run only if a successful matching activate already ran. The two are
  561. * protected from each other by the internal locking and will not run
  562. * at the same time even during a hangup event.
  563. */
  564. static void sdio_uart_shutdown(struct tty_port *tport)
  565. {
  566. struct sdio_uart_port *port =
  567. container_of(tport, struct sdio_uart_port, port);
  568. int ret;
  569. ret = sdio_uart_claim_func(port);
  570. if (ret)
  571. return;
  572. sdio_uart_stop_rx(port);
  573. /* Disable interrupts from this port */
  574. sdio_release_irq(port->func);
  575. port->ier = 0;
  576. sdio_out(port, UART_IER, 0);
  577. sdio_uart_clear_mctrl(port, TIOCM_OUT2);
  578. /* Disable break condition and FIFOs. */
  579. port->lcr &= ~UART_LCR_SBC;
  580. sdio_out(port, UART_LCR, port->lcr);
  581. sdio_out(port, UART_FCR, UART_FCR_ENABLE_FIFO |
  582. UART_FCR_CLEAR_RCVR |
  583. UART_FCR_CLEAR_XMIT);
  584. sdio_out(port, UART_FCR, 0);
  585. sdio_disable_func(port->func);
  586. sdio_uart_release_func(port);
  587. }
  588. static void sdio_uart_port_destroy(struct tty_port *tport)
  589. {
  590. struct sdio_uart_port *port =
  591. container_of(tport, struct sdio_uart_port, port);
  592. kfifo_free(&port->xmit_fifo);
  593. kfree(port);
  594. }
  595. /**
  596. * sdio_uart_install - install method
  597. * @driver: the driver in use (sdio_uart in our case)
  598. * @tty: the tty being bound
  599. *
  600. * Look up and bind the tty and the driver together. Initialize
  601. * any needed private data (in our case the termios)
  602. */
  603. static int sdio_uart_install(struct tty_driver *driver, struct tty_struct *tty)
  604. {
  605. int idx = tty->index;
  606. struct sdio_uart_port *port = sdio_uart_port_get(idx);
  607. int ret = tty_standard_install(driver, tty);
  608. if (ret == 0)
  609. /* This is the ref sdio_uart_port get provided */
  610. tty->driver_data = port;
  611. else
  612. sdio_uart_port_put(port);
  613. return ret;
  614. }
  615. /**
  616. * sdio_uart_cleanup - called on the last tty kref drop
  617. * @tty: the tty being destroyed
  618. *
  619. * Called asynchronously when the last reference to the tty is dropped.
  620. * We cannot destroy the tty->driver_data port kref until this point
  621. */
  622. static void sdio_uart_cleanup(struct tty_struct *tty)
  623. {
  624. struct sdio_uart_port *port = tty->driver_data;
  625. tty->driver_data = NULL; /* Bug trap */
  626. sdio_uart_port_put(port);
  627. }
  628. /*
  629. * Open/close/hangup is now entirely boilerplate
  630. */
  631. static int sdio_uart_open(struct tty_struct *tty, struct file *filp)
  632. {
  633. struct sdio_uart_port *port = tty->driver_data;
  634. return tty_port_open(&port->port, tty, filp);
  635. }
  636. static void sdio_uart_close(struct tty_struct *tty, struct file * filp)
  637. {
  638. struct sdio_uart_port *port = tty->driver_data;
  639. tty_port_close(&port->port, tty, filp);
  640. }
  641. static void sdio_uart_hangup(struct tty_struct *tty)
  642. {
  643. struct sdio_uart_port *port = tty->driver_data;
  644. tty_port_hangup(&port->port);
  645. }
  646. static ssize_t sdio_uart_write(struct tty_struct *tty, const u8 *buf,
  647. size_t count)
  648. {
  649. struct sdio_uart_port *port = tty->driver_data;
  650. int ret;
  651. if (!port->func)
  652. return -ENODEV;
  653. ret = kfifo_in_locked(&port->xmit_fifo, buf, count, &port->write_lock);
  654. if (!(port->ier & UART_IER_THRI)) {
  655. int err = sdio_uart_claim_func(port);
  656. if (!err) {
  657. sdio_uart_start_tx(port);
  658. sdio_uart_irq(port->func);
  659. sdio_uart_release_func(port);
  660. } else
  661. ret = err;
  662. }
  663. return ret;
  664. }
  665. static unsigned int sdio_uart_write_room(struct tty_struct *tty)
  666. {
  667. struct sdio_uart_port *port = tty->driver_data;
  668. return FIFO_SIZE - kfifo_len(&port->xmit_fifo);
  669. }
  670. static unsigned int sdio_uart_chars_in_buffer(struct tty_struct *tty)
  671. {
  672. struct sdio_uart_port *port = tty->driver_data;
  673. return kfifo_len(&port->xmit_fifo);
  674. }
  675. static void sdio_uart_send_xchar(struct tty_struct *tty, u8 ch)
  676. {
  677. struct sdio_uart_port *port = tty->driver_data;
  678. port->x_char = ch;
  679. if (ch && !(port->ier & UART_IER_THRI)) {
  680. if (sdio_uart_claim_func(port) != 0)
  681. return;
  682. sdio_uart_start_tx(port);
  683. sdio_uart_irq(port->func);
  684. sdio_uart_release_func(port);
  685. }
  686. }
  687. static void sdio_uart_throttle(struct tty_struct *tty)
  688. {
  689. struct sdio_uart_port *port = tty->driver_data;
  690. if (!I_IXOFF(tty) && !C_CRTSCTS(tty))
  691. return;
  692. if (sdio_uart_claim_func(port) != 0)
  693. return;
  694. if (I_IXOFF(tty)) {
  695. port->x_char = STOP_CHAR(tty);
  696. sdio_uart_start_tx(port);
  697. }
  698. if (C_CRTSCTS(tty))
  699. sdio_uart_clear_mctrl(port, TIOCM_RTS);
  700. sdio_uart_irq(port->func);
  701. sdio_uart_release_func(port);
  702. }
  703. static void sdio_uart_unthrottle(struct tty_struct *tty)
  704. {
  705. struct sdio_uart_port *port = tty->driver_data;
  706. if (!I_IXOFF(tty) && !C_CRTSCTS(tty))
  707. return;
  708. if (sdio_uart_claim_func(port) != 0)
  709. return;
  710. if (I_IXOFF(tty)) {
  711. if (port->x_char) {
  712. port->x_char = 0;
  713. } else {
  714. port->x_char = START_CHAR(tty);
  715. sdio_uart_start_tx(port);
  716. }
  717. }
  718. if (C_CRTSCTS(tty))
  719. sdio_uart_set_mctrl(port, TIOCM_RTS);
  720. sdio_uart_irq(port->func);
  721. sdio_uart_release_func(port);
  722. }
  723. static void sdio_uart_set_termios(struct tty_struct *tty,
  724. const struct ktermios *old_termios)
  725. {
  726. struct sdio_uart_port *port = tty->driver_data;
  727. unsigned int cflag = tty->termios.c_cflag;
  728. if (sdio_uart_claim_func(port) != 0)
  729. return;
  730. sdio_uart_change_speed(port, &tty->termios, old_termios);
  731. /* Handle transition to B0 status */
  732. if ((old_termios->c_cflag & CBAUD) && !(cflag & CBAUD))
  733. sdio_uart_clear_mctrl(port, TIOCM_RTS | TIOCM_DTR);
  734. /* Handle transition away from B0 status */
  735. if (!(old_termios->c_cflag & CBAUD) && (cflag & CBAUD)) {
  736. unsigned int mask = TIOCM_DTR;
  737. if (!(cflag & CRTSCTS) || !tty_throttled(tty))
  738. mask |= TIOCM_RTS;
  739. sdio_uart_set_mctrl(port, mask);
  740. }
  741. /* Handle turning off CRTSCTS */
  742. if ((old_termios->c_cflag & CRTSCTS) && !(cflag & CRTSCTS)) {
  743. tty->hw_stopped = false;
  744. sdio_uart_start_tx(port);
  745. }
  746. /* Handle turning on CRTSCTS */
  747. if (!(old_termios->c_cflag & CRTSCTS) && (cflag & CRTSCTS)) {
  748. if (!(sdio_uart_get_mctrl(port) & TIOCM_CTS)) {
  749. tty->hw_stopped = true;
  750. sdio_uart_stop_tx(port);
  751. }
  752. }
  753. sdio_uart_release_func(port);
  754. }
  755. static int sdio_uart_break_ctl(struct tty_struct *tty, int break_state)
  756. {
  757. struct sdio_uart_port *port = tty->driver_data;
  758. int result;
  759. result = sdio_uart_claim_func(port);
  760. if (result != 0)
  761. return result;
  762. if (break_state == -1)
  763. port->lcr |= UART_LCR_SBC;
  764. else
  765. port->lcr &= ~UART_LCR_SBC;
  766. sdio_out(port, UART_LCR, port->lcr);
  767. sdio_uart_release_func(port);
  768. return 0;
  769. }
  770. static int sdio_uart_tiocmget(struct tty_struct *tty)
  771. {
  772. struct sdio_uart_port *port = tty->driver_data;
  773. int result;
  774. result = sdio_uart_claim_func(port);
  775. if (!result) {
  776. result = port->mctrl | sdio_uart_get_mctrl(port);
  777. sdio_uart_release_func(port);
  778. }
  779. return result;
  780. }
  781. static int sdio_uart_tiocmset(struct tty_struct *tty,
  782. unsigned int set, unsigned int clear)
  783. {
  784. struct sdio_uart_port *port = tty->driver_data;
  785. int result;
  786. result = sdio_uart_claim_func(port);
  787. if (!result) {
  788. sdio_uart_update_mctrl(port, set, clear);
  789. sdio_uart_release_func(port);
  790. }
  791. return result;
  792. }
  793. static int sdio_uart_proc_show(struct seq_file *m, void *v)
  794. {
  795. int i;
  796. seq_printf(m, "serinfo:1.0 driver%s%s revision:%s\n",
  797. "", "", "");
  798. for (i = 0; i < UART_NR; i++) {
  799. struct sdio_uart_port *port = sdio_uart_port_get(i);
  800. if (port) {
  801. seq_printf(m, "%d: uart:SDIO", i);
  802. if (capable(CAP_SYS_ADMIN)) {
  803. seq_printf(m, " tx:%d rx:%d",
  804. port->icount.tx, port->icount.rx);
  805. if (port->icount.frame)
  806. seq_printf(m, " fe:%d",
  807. port->icount.frame);
  808. if (port->icount.parity)
  809. seq_printf(m, " pe:%d",
  810. port->icount.parity);
  811. if (port->icount.brk)
  812. seq_printf(m, " brk:%d",
  813. port->icount.brk);
  814. if (port->icount.overrun)
  815. seq_printf(m, " oe:%d",
  816. port->icount.overrun);
  817. if (port->icount.cts)
  818. seq_printf(m, " cts:%d",
  819. port->icount.cts);
  820. if (port->icount.dsr)
  821. seq_printf(m, " dsr:%d",
  822. port->icount.dsr);
  823. if (port->icount.rng)
  824. seq_printf(m, " rng:%d",
  825. port->icount.rng);
  826. if (port->icount.dcd)
  827. seq_printf(m, " dcd:%d",
  828. port->icount.dcd);
  829. }
  830. sdio_uart_port_put(port);
  831. seq_putc(m, '\n');
  832. }
  833. }
  834. return 0;
  835. }
  836. static const struct tty_port_operations sdio_uart_port_ops = {
  837. .dtr_rts = uart_dtr_rts,
  838. .carrier_raised = uart_carrier_raised,
  839. .shutdown = sdio_uart_shutdown,
  840. .activate = sdio_uart_activate,
  841. .destruct = sdio_uart_port_destroy,
  842. };
  843. static const struct tty_operations sdio_uart_ops = {
  844. .open = sdio_uart_open,
  845. .close = sdio_uart_close,
  846. .write = sdio_uart_write,
  847. .write_room = sdio_uart_write_room,
  848. .chars_in_buffer = sdio_uart_chars_in_buffer,
  849. .send_xchar = sdio_uart_send_xchar,
  850. .throttle = sdio_uart_throttle,
  851. .unthrottle = sdio_uart_unthrottle,
  852. .set_termios = sdio_uart_set_termios,
  853. .hangup = sdio_uart_hangup,
  854. .break_ctl = sdio_uart_break_ctl,
  855. .tiocmget = sdio_uart_tiocmget,
  856. .tiocmset = sdio_uart_tiocmset,
  857. .install = sdio_uart_install,
  858. .cleanup = sdio_uart_cleanup,
  859. .proc_show = sdio_uart_proc_show,
  860. };
  861. static struct tty_driver *sdio_uart_tty_driver;
  862. static int sdio_uart_probe(struct sdio_func *func,
  863. const struct sdio_device_id *id)
  864. {
  865. struct sdio_uart_port *port;
  866. int ret;
  867. port = kzalloc(sizeof(struct sdio_uart_port), GFP_KERNEL);
  868. if (!port)
  869. return -ENOMEM;
  870. if (func->class == SDIO_CLASS_UART) {
  871. pr_warn("%s: need info on UART class basic setup\n",
  872. sdio_func_id(func));
  873. kfree(port);
  874. return -ENOSYS;
  875. } else if (func->class == SDIO_CLASS_GPS) {
  876. /*
  877. * We need tuple 0x91. It contains SUBTPL_SIOREG
  878. * and SUBTPL_RCVCAPS.
  879. */
  880. struct sdio_func_tuple *tpl;
  881. for (tpl = func->tuples; tpl; tpl = tpl->next) {
  882. if (tpl->code != 0x91)
  883. continue;
  884. if (tpl->size < 10)
  885. continue;
  886. if (tpl->data[1] == 0) /* SUBTPL_SIOREG */
  887. break;
  888. }
  889. if (!tpl) {
  890. pr_warn("%s: can't find tuple 0x91 subtuple 0 (SUBTPL_SIOREG) for GPS class\n",
  891. sdio_func_id(func));
  892. kfree(port);
  893. return -EINVAL;
  894. }
  895. pr_debug("%s: Register ID = 0x%02x, Exp ID = 0x%02x\n",
  896. sdio_func_id(func), tpl->data[2], tpl->data[3]);
  897. port->regs_offset = (tpl->data[4] << 0) |
  898. (tpl->data[5] << 8) |
  899. (tpl->data[6] << 16);
  900. pr_debug("%s: regs offset = 0x%x\n",
  901. sdio_func_id(func), port->regs_offset);
  902. port->uartclk = tpl->data[7] * 115200;
  903. if (port->uartclk == 0)
  904. port->uartclk = 115200;
  905. pr_debug("%s: clk %d baudcode %u 4800-div %u\n",
  906. sdio_func_id(func), port->uartclk,
  907. tpl->data[7], tpl->data[8] | (tpl->data[9] << 8));
  908. } else {
  909. kfree(port);
  910. return -EINVAL;
  911. }
  912. port->func = func;
  913. sdio_set_drvdata(func, port);
  914. tty_port_init(&port->port);
  915. port->port.ops = &sdio_uart_port_ops;
  916. ret = sdio_uart_add_port(port);
  917. if (ret) {
  918. kfree(port);
  919. } else {
  920. struct device *dev;
  921. dev = tty_port_register_device(&port->port,
  922. sdio_uart_tty_driver, port->index, &func->dev);
  923. if (IS_ERR(dev)) {
  924. sdio_uart_port_remove(port);
  925. ret = PTR_ERR(dev);
  926. }
  927. }
  928. return ret;
  929. }
  930. static void sdio_uart_remove(struct sdio_func *func)
  931. {
  932. struct sdio_uart_port *port = sdio_get_drvdata(func);
  933. tty_unregister_device(sdio_uart_tty_driver, port->index);
  934. sdio_uart_port_remove(port);
  935. }
  936. static const struct sdio_device_id sdio_uart_ids[] = {
  937. { SDIO_DEVICE_CLASS(SDIO_CLASS_UART) },
  938. { SDIO_DEVICE_CLASS(SDIO_CLASS_GPS) },
  939. { /* end: all zeroes */ },
  940. };
  941. MODULE_DEVICE_TABLE(sdio, sdio_uart_ids);
  942. static struct sdio_driver sdio_uart_driver = {
  943. .probe = sdio_uart_probe,
  944. .remove = sdio_uart_remove,
  945. .name = "sdio_uart",
  946. .id_table = sdio_uart_ids,
  947. };
  948. static int __init sdio_uart_init(void)
  949. {
  950. int ret;
  951. struct tty_driver *tty_drv;
  952. sdio_uart_tty_driver = tty_drv = tty_alloc_driver(UART_NR,
  953. TTY_DRIVER_REAL_RAW | TTY_DRIVER_DYNAMIC_DEV);
  954. if (IS_ERR(tty_drv))
  955. return PTR_ERR(tty_drv);
  956. tty_drv->driver_name = "sdio_uart";
  957. tty_drv->name = "ttySDIO";
  958. tty_drv->major = 0; /* dynamically allocated */
  959. tty_drv->minor_start = 0;
  960. tty_drv->type = TTY_DRIVER_TYPE_SERIAL;
  961. tty_drv->subtype = SERIAL_TYPE_NORMAL;
  962. tty_drv->init_termios = tty_std_termios;
  963. tty_drv->init_termios.c_cflag = B4800 | CS8 | CREAD | HUPCL | CLOCAL;
  964. tty_drv->init_termios.c_ispeed = 4800;
  965. tty_drv->init_termios.c_ospeed = 4800;
  966. tty_set_operations(tty_drv, &sdio_uart_ops);
  967. ret = tty_register_driver(tty_drv);
  968. if (ret)
  969. goto err1;
  970. ret = sdio_register_driver(&sdio_uart_driver);
  971. if (ret)
  972. goto err2;
  973. return 0;
  974. err2:
  975. tty_unregister_driver(tty_drv);
  976. err1:
  977. tty_driver_kref_put(tty_drv);
  978. return ret;
  979. }
  980. static void __exit sdio_uart_exit(void)
  981. {
  982. sdio_unregister_driver(&sdio_uart_driver);
  983. tty_unregister_driver(sdio_uart_tty_driver);
  984. tty_driver_kref_put(sdio_uart_tty_driver);
  985. }
  986. module_init(sdio_uart_init);
  987. module_exit(sdio_uart_exit);
  988. MODULE_AUTHOR("Nicolas Pitre");
  989. MODULE_DESCRIPTION("SDIO UART/GPS driver");
  990. MODULE_LICENSE("GPL");