cpm_uart.c 38 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604
  1. // SPDX-License-Identifier: GPL-2.0+
  2. /*
  3. * Driver for CPM (SCC/SMC) serial ports; core driver
  4. *
  5. * Based on arch/ppc/cpm2_io/uart.c by Dan Malek
  6. * Based on ppc8xx.c by Thomas Gleixner
  7. * Based on drivers/serial/amba.c by Russell King
  8. *
  9. * Maintainer: Kumar Gala (galak@kernel.crashing.org) (CPM2)
  10. * Pantelis Antoniou (panto@intracom.gr) (CPM1)
  11. *
  12. * Copyright (C) 2004, 2007 Freescale Semiconductor, Inc.
  13. * (C) 2004 Intracom, S.A.
  14. * (C) 2005-2006 MontaVista Software, Inc.
  15. * Vitaly Bordug <vbordug@ru.mvista.com>
  16. */
  17. #include <linux/module.h>
  18. #include <linux/tty.h>
  19. #include <linux/tty_flip.h>
  20. #include <linux/ioport.h>
  21. #include <linux/init.h>
  22. #include <linux/serial.h>
  23. #include <linux/console.h>
  24. #include <linux/sysrq.h>
  25. #include <linux/device.h>
  26. #include <linux/memblock.h>
  27. #include <linux/dma-mapping.h>
  28. #include <linux/of_address.h>
  29. #include <linux/of_irq.h>
  30. #include <linux/of_platform.h>
  31. #include <linux/gpio/consumer.h>
  32. #include <linux/clk.h>
  33. #include <sysdev/fsl_soc.h>
  34. #include <asm/io.h>
  35. #include <asm/irq.h>
  36. #include <asm/delay.h>
  37. #include <asm/udbg.h>
  38. #include <linux/serial_core.h>
  39. #include <linux/kernel.h>
  40. #include "cpm_uart.h"
  41. /**************************************************************/
  42. static int cpm_uart_tx_pump(struct uart_port *port);
  43. static void cpm_uart_initbd(struct uart_cpm_port *pinfo);
  44. /**************************************************************/
  45. #define HW_BUF_SPD_THRESHOLD 2400
  46. static void cpm_line_cr_cmd(struct uart_cpm_port *port, int cmd)
  47. {
  48. cpm_command(port->command, cmd);
  49. }
  50. /*
  51. * Check, if transmit buffers are processed
  52. */
  53. static unsigned int cpm_uart_tx_empty(struct uart_port *port)
  54. {
  55. struct uart_cpm_port *pinfo =
  56. container_of(port, struct uart_cpm_port, port);
  57. cbd_t __iomem *bdp = pinfo->tx_bd_base;
  58. int ret = 0;
  59. while (1) {
  60. if (in_be16(&bdp->cbd_sc) & BD_SC_READY)
  61. break;
  62. if (in_be16(&bdp->cbd_sc) & BD_SC_WRAP) {
  63. ret = TIOCSER_TEMT;
  64. break;
  65. }
  66. bdp++;
  67. }
  68. pr_debug("CPM uart[%d]:tx_empty: %d\n", port->line, ret);
  69. return ret;
  70. }
  71. static void cpm_uart_set_mctrl(struct uart_port *port, unsigned int mctrl)
  72. {
  73. struct uart_cpm_port *pinfo =
  74. container_of(port, struct uart_cpm_port, port);
  75. if (pinfo->gpios[GPIO_RTS])
  76. gpiod_set_value(pinfo->gpios[GPIO_RTS], !(mctrl & TIOCM_RTS));
  77. if (pinfo->gpios[GPIO_DTR])
  78. gpiod_set_value(pinfo->gpios[GPIO_DTR], !(mctrl & TIOCM_DTR));
  79. }
  80. static unsigned int cpm_uart_get_mctrl(struct uart_port *port)
  81. {
  82. struct uart_cpm_port *pinfo =
  83. container_of(port, struct uart_cpm_port, port);
  84. unsigned int mctrl = TIOCM_CTS | TIOCM_DSR | TIOCM_CAR;
  85. if (pinfo->gpios[GPIO_CTS]) {
  86. if (gpiod_get_value(pinfo->gpios[GPIO_CTS]))
  87. mctrl &= ~TIOCM_CTS;
  88. }
  89. if (pinfo->gpios[GPIO_DSR]) {
  90. if (gpiod_get_value(pinfo->gpios[GPIO_DSR]))
  91. mctrl &= ~TIOCM_DSR;
  92. }
  93. if (pinfo->gpios[GPIO_DCD]) {
  94. if (gpiod_get_value(pinfo->gpios[GPIO_DCD]))
  95. mctrl &= ~TIOCM_CAR;
  96. }
  97. if (pinfo->gpios[GPIO_RI]) {
  98. if (!gpiod_get_value(pinfo->gpios[GPIO_RI]))
  99. mctrl |= TIOCM_RNG;
  100. }
  101. return mctrl;
  102. }
  103. /*
  104. * Stop transmitter
  105. */
  106. static void cpm_uart_stop_tx(struct uart_port *port)
  107. {
  108. struct uart_cpm_port *pinfo =
  109. container_of(port, struct uart_cpm_port, port);
  110. smc_t __iomem *smcp = pinfo->smcp;
  111. scc_t __iomem *sccp = pinfo->sccp;
  112. pr_debug("CPM uart[%d]:stop tx\n", port->line);
  113. if (IS_SMC(pinfo))
  114. clrbits8(&smcp->smc_smcm, SMCM_TX);
  115. else
  116. clrbits16(&sccp->scc_sccm, UART_SCCM_TX);
  117. }
  118. /*
  119. * Start transmitter
  120. */
  121. static void cpm_uart_start_tx(struct uart_port *port)
  122. {
  123. struct uart_cpm_port *pinfo =
  124. container_of(port, struct uart_cpm_port, port);
  125. smc_t __iomem *smcp = pinfo->smcp;
  126. scc_t __iomem *sccp = pinfo->sccp;
  127. pr_debug("CPM uart[%d]:start tx\n", port->line);
  128. if (IS_SMC(pinfo)) {
  129. if (in_8(&smcp->smc_smcm) & SMCM_TX)
  130. return;
  131. } else {
  132. if (in_be16(&sccp->scc_sccm) & UART_SCCM_TX)
  133. return;
  134. }
  135. if (cpm_uart_tx_pump(port) != 0) {
  136. if (IS_SMC(pinfo)) {
  137. setbits8(&smcp->smc_smcm, SMCM_TX);
  138. } else {
  139. setbits16(&sccp->scc_sccm, UART_SCCM_TX);
  140. }
  141. }
  142. }
  143. /*
  144. * Stop receiver
  145. */
  146. static void cpm_uart_stop_rx(struct uart_port *port)
  147. {
  148. struct uart_cpm_port *pinfo =
  149. container_of(port, struct uart_cpm_port, port);
  150. smc_t __iomem *smcp = pinfo->smcp;
  151. scc_t __iomem *sccp = pinfo->sccp;
  152. pr_debug("CPM uart[%d]:stop rx\n", port->line);
  153. if (IS_SMC(pinfo))
  154. clrbits8(&smcp->smc_smcm, SMCM_RX);
  155. else
  156. clrbits16(&sccp->scc_sccm, UART_SCCM_RX);
  157. }
  158. /*
  159. * Generate a break.
  160. */
  161. static void cpm_uart_break_ctl(struct uart_port *port, int break_state)
  162. {
  163. struct uart_cpm_port *pinfo =
  164. container_of(port, struct uart_cpm_port, port);
  165. pr_debug("CPM uart[%d]:break ctrl, break_state: %d\n", port->line,
  166. break_state);
  167. if (break_state)
  168. cpm_line_cr_cmd(pinfo, CPM_CR_STOP_TX);
  169. else
  170. cpm_line_cr_cmd(pinfo, CPM_CR_RESTART_TX);
  171. }
  172. /*
  173. * Transmit characters, refill buffer descriptor, if possible
  174. */
  175. static void cpm_uart_int_tx(struct uart_port *port)
  176. {
  177. pr_debug("CPM uart[%d]:TX INT\n", port->line);
  178. cpm_uart_tx_pump(port);
  179. }
  180. #ifdef CONFIG_CONSOLE_POLL
  181. static int serial_polled;
  182. #endif
  183. /*
  184. * Receive characters
  185. */
  186. static void cpm_uart_int_rx(struct uart_port *port)
  187. {
  188. int i;
  189. unsigned char ch;
  190. u8 *cp;
  191. struct tty_port *tport = &port->state->port;
  192. struct uart_cpm_port *pinfo =
  193. container_of(port, struct uart_cpm_port, port);
  194. cbd_t __iomem *bdp;
  195. u16 status;
  196. unsigned int flg;
  197. pr_debug("CPM uart[%d]:RX INT\n", port->line);
  198. /* Just loop through the closed BDs and copy the characters into
  199. * the buffer.
  200. */
  201. bdp = pinfo->rx_cur;
  202. for (;;) {
  203. #ifdef CONFIG_CONSOLE_POLL
  204. if (unlikely(serial_polled)) {
  205. serial_polled = 0;
  206. return;
  207. }
  208. #endif
  209. /* get status */
  210. status = in_be16(&bdp->cbd_sc);
  211. /* If this one is empty, return happy */
  212. if (status & BD_SC_EMPTY)
  213. break;
  214. /* get number of characters, and check spce in flip-buffer */
  215. i = in_be16(&bdp->cbd_datlen);
  216. /* If we have not enough room in tty flip buffer, then we try
  217. * later, which will be the next rx-interrupt or a timeout
  218. */
  219. if (tty_buffer_request_room(tport, i) < i) {
  220. printk(KERN_WARNING "No room in flip buffer\n");
  221. return;
  222. }
  223. /* get pointer */
  224. cp = cpm2cpu_addr(in_be32(&bdp->cbd_bufaddr), pinfo);
  225. /* loop through the buffer */
  226. while (i-- > 0) {
  227. ch = *cp++;
  228. port->icount.rx++;
  229. flg = TTY_NORMAL;
  230. if (status &
  231. (BD_SC_BR | BD_SC_FR | BD_SC_PR | BD_SC_OV))
  232. goto handle_error;
  233. if (uart_handle_sysrq_char(port, ch))
  234. continue;
  235. #ifdef CONFIG_CONSOLE_POLL
  236. if (unlikely(serial_polled)) {
  237. serial_polled = 0;
  238. return;
  239. }
  240. #endif
  241. error_return:
  242. tty_insert_flip_char(tport, ch, flg);
  243. } /* End while (i--) */
  244. /* This BD is ready to be used again. Clear status. get next */
  245. clrbits16(&bdp->cbd_sc, BD_SC_BR | BD_SC_FR | BD_SC_PR |
  246. BD_SC_OV | BD_SC_ID);
  247. setbits16(&bdp->cbd_sc, BD_SC_EMPTY);
  248. if (in_be16(&bdp->cbd_sc) & BD_SC_WRAP)
  249. bdp = pinfo->rx_bd_base;
  250. else
  251. bdp++;
  252. } /* End for (;;) */
  253. /* Write back buffer pointer */
  254. pinfo->rx_cur = bdp;
  255. /* activate BH processing */
  256. tty_flip_buffer_push(tport);
  257. return;
  258. /* Error processing */
  259. handle_error:
  260. /* Statistics */
  261. if (status & BD_SC_BR)
  262. port->icount.brk++;
  263. if (status & BD_SC_PR)
  264. port->icount.parity++;
  265. if (status & BD_SC_FR)
  266. port->icount.frame++;
  267. if (status & BD_SC_OV)
  268. port->icount.overrun++;
  269. /* Mask out ignored conditions */
  270. status &= port->read_status_mask;
  271. /* Handle the remaining ones */
  272. if (status & BD_SC_BR)
  273. flg = TTY_BREAK;
  274. else if (status & BD_SC_PR)
  275. flg = TTY_PARITY;
  276. else if (status & BD_SC_FR)
  277. flg = TTY_FRAME;
  278. /* overrun does not affect the current character ! */
  279. if (status & BD_SC_OV) {
  280. ch = 0;
  281. flg = TTY_OVERRUN;
  282. /* We skip this buffer */
  283. /* CHECK: Is really nothing senseful there */
  284. /* ASSUMPTION: it contains nothing valid */
  285. i = 0;
  286. }
  287. port->sysrq = 0;
  288. goto error_return;
  289. }
  290. /*
  291. * Asynchron mode interrupt handler
  292. */
  293. static irqreturn_t cpm_uart_int(int irq, void *data)
  294. {
  295. u8 events;
  296. struct uart_port *port = data;
  297. struct uart_cpm_port *pinfo = (struct uart_cpm_port *)port;
  298. smc_t __iomem *smcp = pinfo->smcp;
  299. scc_t __iomem *sccp = pinfo->sccp;
  300. pr_debug("CPM uart[%d]:IRQ\n", port->line);
  301. if (IS_SMC(pinfo)) {
  302. events = in_8(&smcp->smc_smce);
  303. out_8(&smcp->smc_smce, events);
  304. if (events & SMCM_BRKE)
  305. uart_handle_break(port);
  306. if (events & SMCM_RX)
  307. cpm_uart_int_rx(port);
  308. if (events & SMCM_TX)
  309. cpm_uart_int_tx(port);
  310. } else {
  311. events = in_be16(&sccp->scc_scce);
  312. out_be16(&sccp->scc_scce, events);
  313. if (events & UART_SCCM_BRKE)
  314. uart_handle_break(port);
  315. if (events & UART_SCCM_RX)
  316. cpm_uart_int_rx(port);
  317. if (events & UART_SCCM_TX)
  318. cpm_uart_int_tx(port);
  319. }
  320. return (events) ? IRQ_HANDLED : IRQ_NONE;
  321. }
  322. static int cpm_uart_startup(struct uart_port *port)
  323. {
  324. int retval;
  325. struct uart_cpm_port *pinfo =
  326. container_of(port, struct uart_cpm_port, port);
  327. pr_debug("CPM uart[%d]:startup\n", port->line);
  328. /* If the port is not the console, make sure rx is disabled. */
  329. if (!(pinfo->flags & FLAG_CONSOLE)) {
  330. /* Disable UART rx */
  331. if (IS_SMC(pinfo)) {
  332. clrbits16(&pinfo->smcp->smc_smcmr, SMCMR_REN);
  333. clrbits8(&pinfo->smcp->smc_smcm, SMCM_RX);
  334. } else {
  335. clrbits32(&pinfo->sccp->scc_gsmrl, SCC_GSMRL_ENR);
  336. clrbits16(&pinfo->sccp->scc_sccm, UART_SCCM_RX);
  337. }
  338. cpm_uart_initbd(pinfo);
  339. if (IS_SMC(pinfo)) {
  340. out_be32(&pinfo->smcup->smc_rstate, 0);
  341. out_be32(&pinfo->smcup->smc_tstate, 0);
  342. out_be16(&pinfo->smcup->smc_rbptr,
  343. in_be16(&pinfo->smcup->smc_rbase));
  344. out_be16(&pinfo->smcup->smc_tbptr,
  345. in_be16(&pinfo->smcup->smc_tbase));
  346. } else {
  347. cpm_line_cr_cmd(pinfo, CPM_CR_INIT_TRX);
  348. }
  349. }
  350. /* Install interrupt handler. */
  351. retval = request_irq(port->irq, cpm_uart_int, 0, "cpm_uart", port);
  352. if (retval)
  353. return retval;
  354. /* Startup rx-int */
  355. if (IS_SMC(pinfo)) {
  356. setbits8(&pinfo->smcp->smc_smcm, SMCM_RX);
  357. setbits16(&pinfo->smcp->smc_smcmr, (SMCMR_REN | SMCMR_TEN));
  358. } else {
  359. setbits16(&pinfo->sccp->scc_sccm, UART_SCCM_RX);
  360. setbits32(&pinfo->sccp->scc_gsmrl, (SCC_GSMRL_ENR | SCC_GSMRL_ENT));
  361. }
  362. return 0;
  363. }
  364. inline void cpm_uart_wait_until_send(struct uart_cpm_port *pinfo)
  365. {
  366. set_current_state(TASK_UNINTERRUPTIBLE);
  367. schedule_timeout(pinfo->wait_closing);
  368. }
  369. /*
  370. * Shutdown the uart
  371. */
  372. static void cpm_uart_shutdown(struct uart_port *port)
  373. {
  374. struct uart_cpm_port *pinfo =
  375. container_of(port, struct uart_cpm_port, port);
  376. pr_debug("CPM uart[%d]:shutdown\n", port->line);
  377. /* free interrupt handler */
  378. free_irq(port->irq, port);
  379. /* If the port is not the console, disable Rx and Tx. */
  380. if (!(pinfo->flags & FLAG_CONSOLE)) {
  381. /* Wait for all the BDs marked sent */
  382. while(!cpm_uart_tx_empty(port)) {
  383. set_current_state(TASK_UNINTERRUPTIBLE);
  384. schedule_timeout(2);
  385. }
  386. if (pinfo->wait_closing)
  387. cpm_uart_wait_until_send(pinfo);
  388. /* Stop uarts */
  389. if (IS_SMC(pinfo)) {
  390. smc_t __iomem *smcp = pinfo->smcp;
  391. clrbits16(&smcp->smc_smcmr, SMCMR_REN | SMCMR_TEN);
  392. clrbits8(&smcp->smc_smcm, SMCM_RX | SMCM_TX);
  393. } else {
  394. scc_t __iomem *sccp = pinfo->sccp;
  395. clrbits32(&sccp->scc_gsmrl, SCC_GSMRL_ENR | SCC_GSMRL_ENT);
  396. clrbits16(&sccp->scc_sccm, UART_SCCM_TX | UART_SCCM_RX);
  397. }
  398. /* Shut them really down and reinit buffer descriptors */
  399. if (IS_SMC(pinfo)) {
  400. out_be16(&pinfo->smcup->smc_brkcr, 0);
  401. cpm_line_cr_cmd(pinfo, CPM_CR_STOP_TX);
  402. } else {
  403. out_be16(&pinfo->sccup->scc_brkcr, 0);
  404. cpm_line_cr_cmd(pinfo, CPM_CR_GRA_STOP_TX);
  405. }
  406. cpm_uart_initbd(pinfo);
  407. }
  408. }
  409. static void cpm_uart_set_termios(struct uart_port *port,
  410. struct ktermios *termios,
  411. const struct ktermios *old)
  412. {
  413. int baud;
  414. unsigned long flags;
  415. u16 cval, scval, prev_mode;
  416. struct uart_cpm_port *pinfo =
  417. container_of(port, struct uart_cpm_port, port);
  418. smc_t __iomem *smcp = pinfo->smcp;
  419. scc_t __iomem *sccp = pinfo->sccp;
  420. int maxidl;
  421. pr_debug("CPM uart[%d]:set_termios\n", port->line);
  422. baud = uart_get_baud_rate(port, termios, old, 0, port->uartclk / 16);
  423. if (baud < HW_BUF_SPD_THRESHOLD || port->flags & UPF_LOW_LATENCY)
  424. pinfo->rx_fifosize = 1;
  425. else
  426. pinfo->rx_fifosize = RX_BUF_SIZE;
  427. /* MAXIDL is the timeout after which a receive buffer is closed
  428. * when not full if no more characters are received.
  429. * We calculate it from the baudrate so that the duration is
  430. * always the same at standard rates: about 4ms.
  431. */
  432. maxidl = baud / 2400;
  433. if (maxidl < 1)
  434. maxidl = 1;
  435. if (maxidl > 0x10)
  436. maxidl = 0x10;
  437. cval = 0;
  438. scval = 0;
  439. if (termios->c_cflag & CSTOPB) {
  440. cval |= SMCMR_SL; /* Two stops */
  441. scval |= SCU_PSMR_SL;
  442. }
  443. if (termios->c_cflag & PARENB) {
  444. cval |= SMCMR_PEN;
  445. scval |= SCU_PSMR_PEN;
  446. if (!(termios->c_cflag & PARODD)) {
  447. cval |= SMCMR_PM_EVEN;
  448. scval |= (SCU_PSMR_REVP | SCU_PSMR_TEVP);
  449. }
  450. }
  451. /*
  452. * Update the timeout
  453. */
  454. uart_update_timeout(port, termios->c_cflag, baud);
  455. /*
  456. * Set up parity check flag
  457. */
  458. port->read_status_mask = (BD_SC_EMPTY | BD_SC_OV);
  459. if (termios->c_iflag & INPCK)
  460. port->read_status_mask |= BD_SC_FR | BD_SC_PR;
  461. if ((termios->c_iflag & BRKINT) || (termios->c_iflag & PARMRK))
  462. port->read_status_mask |= BD_SC_BR;
  463. /*
  464. * Characters to ignore
  465. */
  466. port->ignore_status_mask = 0;
  467. if (termios->c_iflag & IGNPAR)
  468. port->ignore_status_mask |= BD_SC_PR | BD_SC_FR;
  469. if (termios->c_iflag & IGNBRK) {
  470. port->ignore_status_mask |= BD_SC_BR;
  471. /*
  472. * If we're ignore parity and break indicators, ignore
  473. * overruns too. (For real raw support).
  474. */
  475. if (termios->c_iflag & IGNPAR)
  476. port->ignore_status_mask |= BD_SC_OV;
  477. }
  478. /*
  479. * !!! ignore all characters if CREAD is not set
  480. */
  481. if ((termios->c_cflag & CREAD) == 0)
  482. port->read_status_mask &= ~BD_SC_EMPTY;
  483. uart_port_lock_irqsave(port, &flags);
  484. if (IS_SMC(pinfo)) {
  485. unsigned int bits = tty_get_frame_size(termios->c_cflag);
  486. /*
  487. * MRBLR can be changed while an SMC/SCC is operating only
  488. * if it is done in a single bus cycle with one 16-bit move
  489. * (not two 8-bit bus cycles back-to-back). This occurs when
  490. * the cp shifts control to the next RxBD, so the change does
  491. * not take effect immediately. To guarantee the exact RxBD
  492. * on which the change occurs, change MRBLR only while the
  493. * SMC/SCC receiver is disabled.
  494. */
  495. out_be16(&pinfo->smcup->smc_mrblr, pinfo->rx_fifosize);
  496. out_be16(&pinfo->smcup->smc_maxidl, maxidl);
  497. /* Set the mode register. We want to keep a copy of the
  498. * enables, because we want to put them back if they were
  499. * present.
  500. */
  501. prev_mode = in_be16(&smcp->smc_smcmr) & (SMCMR_REN | SMCMR_TEN);
  502. /* Output in *one* operation, so we don't interrupt RX/TX if they
  503. * were already enabled.
  504. * Character length programmed into the register is frame bits minus 1.
  505. */
  506. out_be16(&smcp->smc_smcmr, smcr_mk_clen(bits - 1) | cval |
  507. SMCMR_SM_UART | prev_mode);
  508. } else {
  509. unsigned int bits = tty_get_char_size(termios->c_cflag);
  510. out_be16(&pinfo->sccup->scc_genscc.scc_mrblr, pinfo->rx_fifosize);
  511. out_be16(&pinfo->sccup->scc_maxidl, maxidl);
  512. out_be16(&sccp->scc_psmr, (UART_LCR_WLEN(bits) << 12) | scval);
  513. }
  514. if (pinfo->clk)
  515. clk_set_rate(pinfo->clk, baud);
  516. else
  517. cpm_setbrg(pinfo->brg - 1, baud);
  518. uart_port_unlock_irqrestore(port, flags);
  519. }
  520. static const char *cpm_uart_type(struct uart_port *port)
  521. {
  522. pr_debug("CPM uart[%d]:uart_type\n", port->line);
  523. return port->type == PORT_CPM ? "CPM UART" : NULL;
  524. }
  525. /*
  526. * verify the new serial_struct (for TIOCSSERIAL).
  527. */
  528. static int cpm_uart_verify_port(struct uart_port *port,
  529. struct serial_struct *ser)
  530. {
  531. int ret = 0;
  532. pr_debug("CPM uart[%d]:verify_port\n", port->line);
  533. if (ser->type != PORT_UNKNOWN && ser->type != PORT_CPM)
  534. ret = -EINVAL;
  535. if (ser->irq < 0 || ser->irq >= irq_get_nr_irqs())
  536. ret = -EINVAL;
  537. if (ser->baud_base < 9600)
  538. ret = -EINVAL;
  539. return ret;
  540. }
  541. /*
  542. * Transmit characters, refill buffer descriptor, if possible
  543. */
  544. static int cpm_uart_tx_pump(struct uart_port *port)
  545. {
  546. cbd_t __iomem *bdp;
  547. u8 *p;
  548. int count;
  549. struct uart_cpm_port *pinfo =
  550. container_of(port, struct uart_cpm_port, port);
  551. struct tty_port *tport = &port->state->port;
  552. /* Handle xon/xoff */
  553. if (port->x_char) {
  554. /* Pick next descriptor and fill from buffer */
  555. bdp = pinfo->tx_cur;
  556. p = cpm2cpu_addr(in_be32(&bdp->cbd_bufaddr), pinfo);
  557. *p++ = port->x_char;
  558. out_be16(&bdp->cbd_datlen, 1);
  559. setbits16(&bdp->cbd_sc, BD_SC_READY);
  560. /* Get next BD. */
  561. if (in_be16(&bdp->cbd_sc) & BD_SC_WRAP)
  562. bdp = pinfo->tx_bd_base;
  563. else
  564. bdp++;
  565. pinfo->tx_cur = bdp;
  566. port->icount.tx++;
  567. port->x_char = 0;
  568. return 1;
  569. }
  570. if (kfifo_is_empty(&tport->xmit_fifo) || uart_tx_stopped(port)) {
  571. cpm_uart_stop_tx(port);
  572. return 0;
  573. }
  574. /* Pick next descriptor and fill from buffer */
  575. bdp = pinfo->tx_cur;
  576. while (!(in_be16(&bdp->cbd_sc) & BD_SC_READY) &&
  577. !kfifo_is_empty(&tport->xmit_fifo)) {
  578. p = cpm2cpu_addr(in_be32(&bdp->cbd_bufaddr), pinfo);
  579. count = uart_fifo_out(port, p, pinfo->tx_fifosize);
  580. out_be16(&bdp->cbd_datlen, count);
  581. setbits16(&bdp->cbd_sc, BD_SC_READY);
  582. /* Get next BD. */
  583. if (in_be16(&bdp->cbd_sc) & BD_SC_WRAP)
  584. bdp = pinfo->tx_bd_base;
  585. else
  586. bdp++;
  587. }
  588. pinfo->tx_cur = bdp;
  589. if (kfifo_len(&tport->xmit_fifo) < WAKEUP_CHARS)
  590. uart_write_wakeup(port);
  591. if (kfifo_is_empty(&tport->xmit_fifo)) {
  592. cpm_uart_stop_tx(port);
  593. return 0;
  594. }
  595. return 1;
  596. }
  597. /*
  598. * init buffer descriptors
  599. */
  600. static void cpm_uart_initbd(struct uart_cpm_port *pinfo)
  601. {
  602. int i;
  603. u8 *mem_addr;
  604. cbd_t __iomem *bdp;
  605. pr_debug("CPM uart[%d]:initbd\n", pinfo->port.line);
  606. /* Set the physical address of the host memory
  607. * buffers in the buffer descriptors, and the
  608. * virtual address for us to work with.
  609. */
  610. mem_addr = pinfo->mem_addr;
  611. bdp = pinfo->rx_cur = pinfo->rx_bd_base;
  612. for (i = 0; i < (pinfo->rx_nrfifos - 1); i++, bdp++) {
  613. out_be32(&bdp->cbd_bufaddr, cpu2cpm_addr(mem_addr, pinfo));
  614. out_be16(&bdp->cbd_sc, BD_SC_EMPTY | BD_SC_INTRPT);
  615. mem_addr += pinfo->rx_fifosize;
  616. }
  617. out_be32(&bdp->cbd_bufaddr, cpu2cpm_addr(mem_addr, pinfo));
  618. out_be16(&bdp->cbd_sc, BD_SC_WRAP | BD_SC_EMPTY | BD_SC_INTRPT);
  619. /* Set the physical address of the host memory
  620. * buffers in the buffer descriptors, and the
  621. * virtual address for us to work with.
  622. */
  623. mem_addr = pinfo->mem_addr + L1_CACHE_ALIGN(pinfo->rx_nrfifos * pinfo->rx_fifosize);
  624. bdp = pinfo->tx_cur = pinfo->tx_bd_base;
  625. for (i = 0; i < (pinfo->tx_nrfifos - 1); i++, bdp++) {
  626. out_be32(&bdp->cbd_bufaddr, cpu2cpm_addr(mem_addr, pinfo));
  627. out_be16(&bdp->cbd_sc, BD_SC_INTRPT);
  628. mem_addr += pinfo->tx_fifosize;
  629. }
  630. out_be32(&bdp->cbd_bufaddr, cpu2cpm_addr(mem_addr, pinfo));
  631. out_be16(&bdp->cbd_sc, BD_SC_WRAP | BD_SC_INTRPT);
  632. }
  633. static void cpm_uart_init_scc(struct uart_cpm_port *pinfo)
  634. {
  635. scc_t __iomem *scp;
  636. scc_uart_t __iomem *sup;
  637. pr_debug("CPM uart[%d]:init_scc\n", pinfo->port.line);
  638. scp = pinfo->sccp;
  639. sup = pinfo->sccup;
  640. /* Store address */
  641. out_be16(&pinfo->sccup->scc_genscc.scc_rbase,
  642. (u8 __iomem *)pinfo->rx_bd_base - DPRAM_BASE);
  643. out_be16(&pinfo->sccup->scc_genscc.scc_tbase,
  644. (u8 __iomem *)pinfo->tx_bd_base - DPRAM_BASE);
  645. /* Set up the uart parameters in the
  646. * parameter ram.
  647. */
  648. out_8(&sup->scc_genscc.scc_rfcr, CPMFCR_GBL | CPMFCR_EB);
  649. out_8(&sup->scc_genscc.scc_tfcr, CPMFCR_GBL | CPMFCR_EB);
  650. out_be16(&sup->scc_genscc.scc_mrblr, pinfo->rx_fifosize);
  651. out_be16(&sup->scc_maxidl, 0x10);
  652. out_be16(&sup->scc_brkcr, 1);
  653. out_be16(&sup->scc_parec, 0);
  654. out_be16(&sup->scc_frmec, 0);
  655. out_be16(&sup->scc_nosec, 0);
  656. out_be16(&sup->scc_brkec, 0);
  657. out_be16(&sup->scc_uaddr1, 0);
  658. out_be16(&sup->scc_uaddr2, 0);
  659. out_be16(&sup->scc_toseq, 0);
  660. out_be16(&sup->scc_char1, 0x8000);
  661. out_be16(&sup->scc_char2, 0x8000);
  662. out_be16(&sup->scc_char3, 0x8000);
  663. out_be16(&sup->scc_char4, 0x8000);
  664. out_be16(&sup->scc_char5, 0x8000);
  665. out_be16(&sup->scc_char6, 0x8000);
  666. out_be16(&sup->scc_char7, 0x8000);
  667. out_be16(&sup->scc_char8, 0x8000);
  668. out_be16(&sup->scc_rccm, 0xc0ff);
  669. /* Send the CPM an initialize command.
  670. */
  671. cpm_line_cr_cmd(pinfo, CPM_CR_INIT_TRX);
  672. /* Set UART mode, 8 bit, no parity, one stop.
  673. * Enable receive and transmit.
  674. */
  675. out_be32(&scp->scc_gsmrh, 0);
  676. out_be32(&scp->scc_gsmrl,
  677. SCC_GSMRL_MODE_UART | SCC_GSMRL_TDCR_16 | SCC_GSMRL_RDCR_16);
  678. /* Enable rx interrupts and clear all pending events. */
  679. out_be16(&scp->scc_sccm, 0);
  680. out_be16(&scp->scc_scce, 0xffff);
  681. out_be16(&scp->scc_dsr, 0x7e7e);
  682. out_be16(&scp->scc_psmr, 0x3000);
  683. setbits32(&scp->scc_gsmrl, SCC_GSMRL_ENR | SCC_GSMRL_ENT);
  684. }
  685. static void cpm_uart_init_smc(struct uart_cpm_port *pinfo)
  686. {
  687. smc_t __iomem *sp;
  688. smc_uart_t __iomem *up;
  689. pr_debug("CPM uart[%d]:init_smc\n", pinfo->port.line);
  690. sp = pinfo->smcp;
  691. up = pinfo->smcup;
  692. /* Store address */
  693. out_be16(&pinfo->smcup->smc_rbase,
  694. (u8 __iomem *)pinfo->rx_bd_base - DPRAM_BASE);
  695. out_be16(&pinfo->smcup->smc_tbase,
  696. (u8 __iomem *)pinfo->tx_bd_base - DPRAM_BASE);
  697. /*
  698. * In case SMC is being relocated...
  699. */
  700. out_be16(&up->smc_rbptr, in_be16(&pinfo->smcup->smc_rbase));
  701. out_be16(&up->smc_tbptr, in_be16(&pinfo->smcup->smc_tbase));
  702. out_be32(&up->smc_rstate, 0);
  703. out_be32(&up->smc_tstate, 0);
  704. out_be16(&up->smc_brkcr, 1); /* number of break chars */
  705. out_be16(&up->smc_brkec, 0);
  706. /* Set up the uart parameters in the
  707. * parameter ram.
  708. */
  709. out_8(&up->smc_rfcr, CPMFCR_GBL | CPMFCR_EB);
  710. out_8(&up->smc_tfcr, CPMFCR_GBL | CPMFCR_EB);
  711. /* Using idle character time requires some additional tuning. */
  712. out_be16(&up->smc_mrblr, pinfo->rx_fifosize);
  713. out_be16(&up->smc_maxidl, 0x10);
  714. out_be16(&up->smc_brklen, 0);
  715. out_be16(&up->smc_brkec, 0);
  716. out_be16(&up->smc_brkcr, 1);
  717. /* Set UART mode, 8 bit, no parity, one stop.
  718. * Enable receive and transmit.
  719. */
  720. out_be16(&sp->smc_smcmr, smcr_mk_clen(9) | SMCMR_SM_UART);
  721. /* Enable only rx interrupts clear all pending events. */
  722. out_8(&sp->smc_smcm, 0);
  723. out_8(&sp->smc_smce, 0xff);
  724. setbits16(&sp->smc_smcmr, SMCMR_REN | SMCMR_TEN);
  725. }
  726. /*
  727. * Allocate DP-Ram and memory buffers. We need to allocate a transmit and
  728. * receive buffer descriptors from dual port ram, and a character
  729. * buffer area from host mem. If we are allocating for the console we need
  730. * to do it from bootmem
  731. */
  732. static int cpm_uart_allocbuf(struct uart_cpm_port *pinfo, unsigned int is_con)
  733. {
  734. int dpmemsz, memsz;
  735. u8 __iomem *dp_mem;
  736. unsigned long dp_offset;
  737. u8 *mem_addr;
  738. dma_addr_t dma_addr = 0;
  739. pr_debug("CPM uart[%d]:allocbuf\n", pinfo->port.line);
  740. dpmemsz = sizeof(cbd_t) * (pinfo->rx_nrfifos + pinfo->tx_nrfifos);
  741. dp_offset = cpm_muram_alloc(dpmemsz, 8);
  742. if (IS_ERR_VALUE(dp_offset)) {
  743. pr_err("%s: could not allocate buffer descriptors\n", __func__);
  744. return -ENOMEM;
  745. }
  746. dp_mem = cpm_muram_addr(dp_offset);
  747. memsz = L1_CACHE_ALIGN(pinfo->rx_nrfifos * pinfo->rx_fifosize) +
  748. L1_CACHE_ALIGN(pinfo->tx_nrfifos * pinfo->tx_fifosize);
  749. if (IS_ENABLED(CONFIG_CPM1) && is_con) {
  750. /* was hostalloc but changed cause it blows away the */
  751. /* large tlb mapping when pinning the kernel area */
  752. mem_addr = (u8 __force *)cpm_muram_addr(cpm_muram_alloc(memsz, 8));
  753. dma_addr = cpm_muram_dma((void __iomem *)mem_addr);
  754. } else if (is_con) {
  755. mem_addr = kzalloc(memsz, GFP_NOWAIT);
  756. dma_addr = virt_to_bus(mem_addr);
  757. } else {
  758. mem_addr = dma_alloc_coherent(pinfo->port.dev, memsz, &dma_addr,
  759. GFP_KERNEL);
  760. }
  761. if (!mem_addr) {
  762. cpm_muram_free(dp_offset);
  763. pr_err("%s: could not allocate coherent memory\n", __func__);
  764. return -ENOMEM;
  765. }
  766. pinfo->dp_addr = dp_offset;
  767. pinfo->mem_addr = mem_addr;
  768. pinfo->dma_addr = dma_addr;
  769. pinfo->mem_size = memsz;
  770. pinfo->rx_buf = mem_addr;
  771. pinfo->tx_buf = pinfo->rx_buf + L1_CACHE_ALIGN(pinfo->rx_nrfifos
  772. * pinfo->rx_fifosize);
  773. pinfo->rx_bd_base = (cbd_t __iomem *)dp_mem;
  774. pinfo->tx_bd_base = pinfo->rx_bd_base + pinfo->rx_nrfifos;
  775. return 0;
  776. }
  777. static void cpm_uart_freebuf(struct uart_cpm_port *pinfo)
  778. {
  779. dma_free_coherent(pinfo->port.dev, L1_CACHE_ALIGN(pinfo->rx_nrfifos *
  780. pinfo->rx_fifosize) +
  781. L1_CACHE_ALIGN(pinfo->tx_nrfifos *
  782. pinfo->tx_fifosize), (void __force *)pinfo->mem_addr,
  783. pinfo->dma_addr);
  784. cpm_muram_free(pinfo->dp_addr);
  785. }
  786. /*
  787. * Initialize port. This is called from early_console stuff
  788. * so we have to be careful here !
  789. */
  790. static int cpm_uart_request_port(struct uart_port *port)
  791. {
  792. struct uart_cpm_port *pinfo =
  793. container_of(port, struct uart_cpm_port, port);
  794. int ret;
  795. pr_debug("CPM uart[%d]:request port\n", port->line);
  796. if (pinfo->flags & FLAG_CONSOLE)
  797. return 0;
  798. if (IS_SMC(pinfo)) {
  799. clrbits8(&pinfo->smcp->smc_smcm, SMCM_RX | SMCM_TX);
  800. clrbits16(&pinfo->smcp->smc_smcmr, SMCMR_REN | SMCMR_TEN);
  801. } else {
  802. clrbits16(&pinfo->sccp->scc_sccm, UART_SCCM_TX | UART_SCCM_RX);
  803. clrbits32(&pinfo->sccp->scc_gsmrl, SCC_GSMRL_ENR | SCC_GSMRL_ENT);
  804. }
  805. ret = cpm_uart_allocbuf(pinfo, 0);
  806. if (ret)
  807. return ret;
  808. cpm_uart_initbd(pinfo);
  809. if (IS_SMC(pinfo))
  810. cpm_uart_init_smc(pinfo);
  811. else
  812. cpm_uart_init_scc(pinfo);
  813. return 0;
  814. }
  815. static void cpm_uart_release_port(struct uart_port *port)
  816. {
  817. struct uart_cpm_port *pinfo =
  818. container_of(port, struct uart_cpm_port, port);
  819. if (!(pinfo->flags & FLAG_CONSOLE))
  820. cpm_uart_freebuf(pinfo);
  821. }
  822. /*
  823. * Configure/autoconfigure the port.
  824. */
  825. static void cpm_uart_config_port(struct uart_port *port, int flags)
  826. {
  827. pr_debug("CPM uart[%d]:config_port\n", port->line);
  828. if (flags & UART_CONFIG_TYPE) {
  829. port->type = PORT_CPM;
  830. cpm_uart_request_port(port);
  831. }
  832. }
  833. #if defined(CONFIG_CONSOLE_POLL) || defined(CONFIG_SERIAL_CPM_CONSOLE)
  834. /*
  835. * Write a string to the serial port
  836. * Note that this is called with interrupts already disabled
  837. */
  838. static void cpm_uart_early_write(struct uart_cpm_port *pinfo,
  839. const char *string, u_int count, bool handle_linefeed)
  840. {
  841. unsigned int i;
  842. cbd_t __iomem *bdp, *bdbase;
  843. unsigned char *cpm_outp_addr;
  844. /* Get the address of the host memory buffer.
  845. */
  846. bdp = pinfo->tx_cur;
  847. bdbase = pinfo->tx_bd_base;
  848. /*
  849. * Now, do each character. This is not as bad as it looks
  850. * since this is a holding FIFO and not a transmitting FIFO.
  851. * We could add the complexity of filling the entire transmit
  852. * buffer, but we would just wait longer between accesses......
  853. */
  854. for (i = 0; i < count; i++, string++) {
  855. /* Wait for transmitter fifo to empty.
  856. * Ready indicates output is ready, and xmt is doing
  857. * that, not that it is ready for us to send.
  858. */
  859. while ((in_be16(&bdp->cbd_sc) & BD_SC_READY) != 0)
  860. ;
  861. /* Send the character out.
  862. * If the buffer address is in the CPM DPRAM, don't
  863. * convert it.
  864. */
  865. cpm_outp_addr = cpm2cpu_addr(in_be32(&bdp->cbd_bufaddr),
  866. pinfo);
  867. *cpm_outp_addr = *string;
  868. out_be16(&bdp->cbd_datlen, 1);
  869. setbits16(&bdp->cbd_sc, BD_SC_READY);
  870. if (in_be16(&bdp->cbd_sc) & BD_SC_WRAP)
  871. bdp = bdbase;
  872. else
  873. bdp++;
  874. /* if a LF, also do CR... */
  875. if (handle_linefeed && *string == 10) {
  876. while ((in_be16(&bdp->cbd_sc) & BD_SC_READY) != 0)
  877. ;
  878. cpm_outp_addr = cpm2cpu_addr(in_be32(&bdp->cbd_bufaddr),
  879. pinfo);
  880. *cpm_outp_addr = 13;
  881. out_be16(&bdp->cbd_datlen, 1);
  882. setbits16(&bdp->cbd_sc, BD_SC_READY);
  883. if (in_be16(&bdp->cbd_sc) & BD_SC_WRAP)
  884. bdp = bdbase;
  885. else
  886. bdp++;
  887. }
  888. }
  889. /*
  890. * Finally, Wait for transmitter & holding register to empty
  891. * and restore the IER
  892. */
  893. while ((in_be16(&bdp->cbd_sc) & BD_SC_READY) != 0)
  894. ;
  895. pinfo->tx_cur = bdp;
  896. }
  897. #endif
  898. #ifdef CONFIG_CONSOLE_POLL
  899. /* Serial polling routines for writing and reading from the uart while
  900. * in an interrupt or debug context.
  901. */
  902. #define GDB_BUF_SIZE 512 /* power of 2, please */
  903. static char poll_buf[GDB_BUF_SIZE];
  904. static char *pollp;
  905. static int poll_chars;
  906. static int poll_wait_key(char *obuf, struct uart_cpm_port *pinfo)
  907. {
  908. u_char c, *cp;
  909. volatile cbd_t *bdp;
  910. int i;
  911. /* Get the address of the host memory buffer.
  912. */
  913. bdp = pinfo->rx_cur;
  914. if (bdp->cbd_sc & BD_SC_EMPTY)
  915. return NO_POLL_CHAR;
  916. /* If the buffer address is in the CPM DPRAM, don't
  917. * convert it.
  918. */
  919. cp = cpm2cpu_addr(bdp->cbd_bufaddr, pinfo);
  920. if (obuf) {
  921. i = c = bdp->cbd_datlen;
  922. while (i-- > 0)
  923. *obuf++ = *cp++;
  924. } else
  925. c = *cp;
  926. bdp->cbd_sc &= ~(BD_SC_BR | BD_SC_FR | BD_SC_PR | BD_SC_OV | BD_SC_ID);
  927. bdp->cbd_sc |= BD_SC_EMPTY;
  928. if (bdp->cbd_sc & BD_SC_WRAP)
  929. bdp = pinfo->rx_bd_base;
  930. else
  931. bdp++;
  932. pinfo->rx_cur = (cbd_t *)bdp;
  933. return (int)c;
  934. }
  935. static int cpm_get_poll_char(struct uart_port *port)
  936. {
  937. struct uart_cpm_port *pinfo =
  938. container_of(port, struct uart_cpm_port, port);
  939. if (!serial_polled) {
  940. serial_polled = 1;
  941. poll_chars = 0;
  942. }
  943. if (poll_chars <= 0) {
  944. int ret = poll_wait_key(poll_buf, pinfo);
  945. if (ret == NO_POLL_CHAR)
  946. return ret;
  947. poll_chars = ret;
  948. pollp = poll_buf;
  949. }
  950. poll_chars--;
  951. return *pollp++;
  952. }
  953. static void cpm_put_poll_char(struct uart_port *port,
  954. unsigned char c)
  955. {
  956. struct uart_cpm_port *pinfo =
  957. container_of(port, struct uart_cpm_port, port);
  958. static char ch[2];
  959. ch[0] = (char)c;
  960. cpm_uart_early_write(pinfo, ch, 1, false);
  961. }
  962. #ifdef CONFIG_SERIAL_CPM_CONSOLE
  963. static struct uart_port *udbg_port;
  964. static void udbg_cpm_putc(char c)
  965. {
  966. if (c == '\n')
  967. cpm_put_poll_char(udbg_port, '\r');
  968. cpm_put_poll_char(udbg_port, c);
  969. }
  970. static int udbg_cpm_getc_poll(void)
  971. {
  972. int c = cpm_get_poll_char(udbg_port);
  973. return c == NO_POLL_CHAR ? -1 : c;
  974. }
  975. static int udbg_cpm_getc(void)
  976. {
  977. int c;
  978. while ((c = udbg_cpm_getc_poll()) == -1)
  979. cpu_relax();
  980. return c;
  981. }
  982. #endif /* CONFIG_SERIAL_CPM_CONSOLE */
  983. #endif /* CONFIG_CONSOLE_POLL */
  984. static const struct uart_ops cpm_uart_pops = {
  985. .tx_empty = cpm_uart_tx_empty,
  986. .set_mctrl = cpm_uart_set_mctrl,
  987. .get_mctrl = cpm_uart_get_mctrl,
  988. .stop_tx = cpm_uart_stop_tx,
  989. .start_tx = cpm_uart_start_tx,
  990. .stop_rx = cpm_uart_stop_rx,
  991. .break_ctl = cpm_uart_break_ctl,
  992. .startup = cpm_uart_startup,
  993. .shutdown = cpm_uart_shutdown,
  994. .set_termios = cpm_uart_set_termios,
  995. .type = cpm_uart_type,
  996. .release_port = cpm_uart_release_port,
  997. .request_port = cpm_uart_request_port,
  998. .config_port = cpm_uart_config_port,
  999. .verify_port = cpm_uart_verify_port,
  1000. #ifdef CONFIG_CONSOLE_POLL
  1001. .poll_get_char = cpm_get_poll_char,
  1002. .poll_put_char = cpm_put_poll_char,
  1003. #endif
  1004. };
  1005. static struct uart_cpm_port cpm_uart_ports[UART_NR];
  1006. static void __iomem *cpm_uart_map_pram(struct uart_cpm_port *port,
  1007. struct device_node *np)
  1008. {
  1009. void __iomem *pram;
  1010. unsigned long offset;
  1011. struct resource res;
  1012. resource_size_t len;
  1013. /* Don't remap parameter RAM if it has already been initialized
  1014. * during console setup.
  1015. */
  1016. if (IS_SMC(port) && port->smcup)
  1017. return port->smcup;
  1018. else if (!IS_SMC(port) && port->sccup)
  1019. return port->sccup;
  1020. if (of_address_to_resource(np, 1, &res))
  1021. return NULL;
  1022. len = resource_size(&res);
  1023. pram = ioremap(res.start, len);
  1024. if (!pram)
  1025. return NULL;
  1026. if (!IS_ENABLED(CONFIG_CPM2) || !IS_SMC(port))
  1027. return pram;
  1028. if (len != 2) {
  1029. pr_warn("cpm_uart[%d]: device tree references "
  1030. "SMC pram, using boot loader/wrapper pram mapping. "
  1031. "Please fix your device tree to reference the pram "
  1032. "base register instead.\n",
  1033. port->port.line);
  1034. return pram;
  1035. }
  1036. offset = cpm_muram_alloc(64, 64);
  1037. out_be16(pram, offset);
  1038. iounmap(pram);
  1039. return cpm_muram_addr(offset);
  1040. }
  1041. static void cpm_uart_unmap_pram(struct uart_cpm_port *port, void __iomem *pram)
  1042. {
  1043. if (!IS_ENABLED(CONFIG_CPM2) || !IS_SMC(port))
  1044. iounmap(pram);
  1045. }
  1046. static int cpm_uart_init_port(struct device_node *np,
  1047. struct uart_cpm_port *pinfo)
  1048. {
  1049. const u32 *data;
  1050. void __iomem *mem, *pram;
  1051. struct device *dev = pinfo->port.dev;
  1052. int len;
  1053. int ret;
  1054. int i;
  1055. data = of_get_property(np, "clock", NULL);
  1056. if (data) {
  1057. struct clk *clk = clk_get(NULL, (const char*)data);
  1058. if (!IS_ERR(clk))
  1059. pinfo->clk = clk;
  1060. }
  1061. if (!pinfo->clk) {
  1062. data = of_get_property(np, "fsl,cpm-brg", &len);
  1063. if (!data || len != 4) {
  1064. printk(KERN_ERR "CPM UART %pOFn has no/invalid "
  1065. "fsl,cpm-brg property.\n", np);
  1066. return -EINVAL;
  1067. }
  1068. pinfo->brg = *data;
  1069. }
  1070. data = of_get_property(np, "fsl,cpm-command", &len);
  1071. if (!data || len != 4) {
  1072. printk(KERN_ERR "CPM UART %pOFn has no/invalid "
  1073. "fsl,cpm-command property.\n", np);
  1074. return -EINVAL;
  1075. }
  1076. pinfo->command = *data;
  1077. mem = of_iomap(np, 0);
  1078. if (!mem)
  1079. return -ENOMEM;
  1080. if (of_device_is_compatible(np, "fsl,cpm1-scc-uart") ||
  1081. of_device_is_compatible(np, "fsl,cpm2-scc-uart")) {
  1082. pinfo->sccp = mem;
  1083. pinfo->sccup = pram = cpm_uart_map_pram(pinfo, np);
  1084. } else if (of_device_is_compatible(np, "fsl,cpm1-smc-uart") ||
  1085. of_device_is_compatible(np, "fsl,cpm2-smc-uart")) {
  1086. pinfo->flags |= FLAG_SMC;
  1087. pinfo->smcp = mem;
  1088. pinfo->smcup = pram = cpm_uart_map_pram(pinfo, np);
  1089. } else {
  1090. ret = -ENODEV;
  1091. goto out_mem;
  1092. }
  1093. if (!pram) {
  1094. ret = -ENOMEM;
  1095. goto out_mem;
  1096. }
  1097. pinfo->tx_nrfifos = TX_NUM_FIFO;
  1098. pinfo->tx_fifosize = TX_BUF_SIZE;
  1099. pinfo->rx_nrfifos = RX_NUM_FIFO;
  1100. pinfo->rx_fifosize = RX_BUF_SIZE;
  1101. pinfo->port.uartclk = ppc_proc_freq;
  1102. pinfo->port.mapbase = (unsigned long)mem;
  1103. pinfo->port.type = PORT_CPM;
  1104. pinfo->port.ops = &cpm_uart_pops;
  1105. pinfo->port.has_sysrq = IS_ENABLED(CONFIG_SERIAL_CPM_CONSOLE);
  1106. pinfo->port.iotype = UPIO_MEM;
  1107. pinfo->port.fifosize = pinfo->tx_nrfifos * pinfo->tx_fifosize;
  1108. spin_lock_init(&pinfo->port.lock);
  1109. for (i = 0; i < NUM_GPIOS; i++) {
  1110. struct gpio_desc *gpiod;
  1111. pinfo->gpios[i] = NULL;
  1112. gpiod = devm_gpiod_get_index_optional(dev, NULL, i, GPIOD_ASIS);
  1113. if (IS_ERR(gpiod)) {
  1114. ret = PTR_ERR(gpiod);
  1115. goto out_pram;
  1116. }
  1117. if (gpiod) {
  1118. if (i == GPIO_RTS || i == GPIO_DTR)
  1119. ret = gpiod_direction_output(gpiod, 0);
  1120. else
  1121. ret = gpiod_direction_input(gpiod);
  1122. if (ret) {
  1123. pr_err("can't set direction for gpio #%d: %d\n",
  1124. i, ret);
  1125. continue;
  1126. }
  1127. pinfo->gpios[i] = gpiod;
  1128. }
  1129. }
  1130. #ifdef CONFIG_PPC_EARLY_DEBUG_CPM
  1131. #if defined(CONFIG_CONSOLE_POLL) && defined(CONFIG_SERIAL_CPM_CONSOLE)
  1132. if (!udbg_port)
  1133. #endif
  1134. udbg_putc = NULL;
  1135. #endif
  1136. return cpm_uart_request_port(&pinfo->port);
  1137. out_pram:
  1138. cpm_uart_unmap_pram(pinfo, pram);
  1139. out_mem:
  1140. iounmap(mem);
  1141. return ret;
  1142. }
  1143. #ifdef CONFIG_SERIAL_CPM_CONSOLE
  1144. /*
  1145. * Print a string to the serial port trying not to disturb
  1146. * any possible real use of the port...
  1147. *
  1148. * Note that this is called with interrupts already disabled
  1149. */
  1150. static void cpm_uart_console_write(struct console *co, const char *s,
  1151. u_int count)
  1152. {
  1153. struct uart_cpm_port *pinfo = &cpm_uart_ports[co->index];
  1154. unsigned long flags;
  1155. if (unlikely(oops_in_progress)) {
  1156. local_irq_save(flags);
  1157. cpm_uart_early_write(pinfo, s, count, true);
  1158. local_irq_restore(flags);
  1159. } else {
  1160. uart_port_lock_irqsave(&pinfo->port, &flags);
  1161. cpm_uart_early_write(pinfo, s, count, true);
  1162. uart_port_unlock_irqrestore(&pinfo->port, flags);
  1163. }
  1164. }
  1165. static int __init cpm_uart_console_setup(struct console *co, char *options)
  1166. {
  1167. int baud = 38400;
  1168. int bits = 8;
  1169. int parity = 'n';
  1170. int flow = 'n';
  1171. int ret;
  1172. struct uart_cpm_port *pinfo;
  1173. struct uart_port *port;
  1174. struct device_node *np;
  1175. int i = 0;
  1176. if (co->index >= UART_NR) {
  1177. printk(KERN_ERR "cpm_uart: console index %d too high\n",
  1178. co->index);
  1179. return -ENODEV;
  1180. }
  1181. for_each_node_by_type(np, "serial") {
  1182. if (!of_device_is_compatible(np, "fsl,cpm1-smc-uart") &&
  1183. !of_device_is_compatible(np, "fsl,cpm1-scc-uart") &&
  1184. !of_device_is_compatible(np, "fsl,cpm2-smc-uart") &&
  1185. !of_device_is_compatible(np, "fsl,cpm2-scc-uart"))
  1186. continue;
  1187. if (i++ == co->index)
  1188. break;
  1189. }
  1190. if (!np)
  1191. return -ENODEV;
  1192. pinfo = &cpm_uart_ports[co->index];
  1193. pinfo->flags |= FLAG_CONSOLE;
  1194. port = &pinfo->port;
  1195. ret = cpm_uart_init_port(np, pinfo);
  1196. of_node_put(np);
  1197. if (ret)
  1198. return ret;
  1199. if (options) {
  1200. uart_parse_options(options, &baud, &parity, &bits, &flow);
  1201. } else {
  1202. baud = get_baudrate();
  1203. if (baud == -1)
  1204. baud = 9600;
  1205. }
  1206. if (IS_SMC(pinfo)) {
  1207. out_be16(&pinfo->smcup->smc_brkcr, 0);
  1208. cpm_line_cr_cmd(pinfo, CPM_CR_STOP_TX);
  1209. clrbits8(&pinfo->smcp->smc_smcm, SMCM_RX | SMCM_TX);
  1210. clrbits16(&pinfo->smcp->smc_smcmr, SMCMR_REN | SMCMR_TEN);
  1211. } else {
  1212. out_be16(&pinfo->sccup->scc_brkcr, 0);
  1213. cpm_line_cr_cmd(pinfo, CPM_CR_GRA_STOP_TX);
  1214. clrbits16(&pinfo->sccp->scc_sccm, UART_SCCM_TX | UART_SCCM_RX);
  1215. clrbits32(&pinfo->sccp->scc_gsmrl, SCC_GSMRL_ENR | SCC_GSMRL_ENT);
  1216. }
  1217. ret = cpm_uart_allocbuf(pinfo, 1);
  1218. if (ret)
  1219. return ret;
  1220. cpm_uart_initbd(pinfo);
  1221. if (IS_SMC(pinfo))
  1222. cpm_uart_init_smc(pinfo);
  1223. else
  1224. cpm_uart_init_scc(pinfo);
  1225. uart_set_options(port, co, baud, parity, bits, flow);
  1226. cpm_line_cr_cmd(pinfo, CPM_CR_RESTART_TX);
  1227. #ifdef CONFIG_CONSOLE_POLL
  1228. if (!udbg_port) {
  1229. udbg_port = &pinfo->port;
  1230. udbg_putc = udbg_cpm_putc;
  1231. udbg_getc = udbg_cpm_getc;
  1232. udbg_getc_poll = udbg_cpm_getc_poll;
  1233. }
  1234. #endif
  1235. return 0;
  1236. }
  1237. static struct uart_driver cpm_reg;
  1238. static struct console cpm_scc_uart_console = {
  1239. .name = "ttyCPM",
  1240. .write = cpm_uart_console_write,
  1241. .device = uart_console_device,
  1242. .setup = cpm_uart_console_setup,
  1243. .flags = CON_PRINTBUFFER,
  1244. .index = -1,
  1245. .data = &cpm_reg,
  1246. };
  1247. static int __init cpm_uart_console_init(void)
  1248. {
  1249. cpm_muram_init();
  1250. register_console(&cpm_scc_uart_console);
  1251. return 0;
  1252. }
  1253. console_initcall(cpm_uart_console_init);
  1254. #define CPM_UART_CONSOLE &cpm_scc_uart_console
  1255. #else
  1256. #define CPM_UART_CONSOLE NULL
  1257. #endif
  1258. static struct uart_driver cpm_reg = {
  1259. .owner = THIS_MODULE,
  1260. .driver_name = "ttyCPM",
  1261. .dev_name = "ttyCPM",
  1262. .major = SERIAL_CPM_MAJOR,
  1263. .minor = SERIAL_CPM_MINOR,
  1264. .cons = CPM_UART_CONSOLE,
  1265. .nr = UART_NR,
  1266. };
  1267. static int probe_index;
  1268. static int cpm_uart_probe(struct platform_device *ofdev)
  1269. {
  1270. int index = probe_index++;
  1271. struct uart_cpm_port *pinfo = &cpm_uart_ports[index];
  1272. int ret;
  1273. pinfo->port.line = index;
  1274. if (index >= UART_NR)
  1275. return -ENODEV;
  1276. platform_set_drvdata(ofdev, pinfo);
  1277. /* initialize the device pointer for the port */
  1278. pinfo->port.dev = &ofdev->dev;
  1279. pinfo->port.irq = irq_of_parse_and_map(ofdev->dev.of_node, 0);
  1280. if (!pinfo->port.irq)
  1281. return -EINVAL;
  1282. ret = cpm_uart_init_port(ofdev->dev.of_node, pinfo);
  1283. if (!ret)
  1284. return uart_add_one_port(&cpm_reg, &pinfo->port);
  1285. irq_dispose_mapping(pinfo->port.irq);
  1286. return ret;
  1287. }
  1288. static void cpm_uart_remove(struct platform_device *ofdev)
  1289. {
  1290. struct uart_cpm_port *pinfo = platform_get_drvdata(ofdev);
  1291. uart_remove_one_port(&cpm_reg, &pinfo->port);
  1292. }
  1293. static const struct of_device_id cpm_uart_match[] = {
  1294. {
  1295. .compatible = "fsl,cpm1-smc-uart",
  1296. },
  1297. {
  1298. .compatible = "fsl,cpm1-scc-uart",
  1299. },
  1300. {
  1301. .compatible = "fsl,cpm2-smc-uart",
  1302. },
  1303. {
  1304. .compatible = "fsl,cpm2-scc-uart",
  1305. },
  1306. {}
  1307. };
  1308. MODULE_DEVICE_TABLE(of, cpm_uart_match);
  1309. static struct platform_driver cpm_uart_driver = {
  1310. .driver = {
  1311. .name = "cpm_uart",
  1312. .of_match_table = cpm_uart_match,
  1313. },
  1314. .probe = cpm_uart_probe,
  1315. .remove = cpm_uart_remove,
  1316. };
  1317. static int __init cpm_uart_init(void)
  1318. {
  1319. int ret = uart_register_driver(&cpm_reg);
  1320. if (ret)
  1321. return ret;
  1322. ret = platform_driver_register(&cpm_uart_driver);
  1323. if (ret)
  1324. uart_unregister_driver(&cpm_reg);
  1325. return ret;
  1326. }
  1327. static void __exit cpm_uart_exit(void)
  1328. {
  1329. platform_driver_unregister(&cpm_uart_driver);
  1330. uart_unregister_driver(&cpm_reg);
  1331. }
  1332. module_init(cpm_uart_init);
  1333. module_exit(cpm_uart_exit);
  1334. MODULE_AUTHOR("Kumar Gala/Antoniou Pantelis");
  1335. MODULE_DESCRIPTION("CPM SCC/SMC port driver $Revision: 0.01 $");
  1336. MODULE_LICENSE("GPL");
  1337. MODULE_ALIAS_CHARDEV(SERIAL_CPM_MAJOR, SERIAL_CPM_MINOR);