ucc_uart.c 41 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547
  1. /*
  2. * Freescale QUICC Engine UART device driver
  3. *
  4. * Author: Timur Tabi <timur@freescale.com>
  5. *
  6. * Copyright 2007 Freescale Semiconductor, Inc. This file is licensed under
  7. * the terms of the GNU General Public License version 2. This program
  8. * is licensed "as is" without any warranty of any kind, whether express
  9. * or implied.
  10. *
  11. * This driver adds support for UART devices via Freescale's QUICC Engine
  12. * found on some Freescale SOCs.
  13. *
  14. * If Soft-UART support is needed but not already present, then this driver
  15. * will request and upload the "Soft-UART" microcode upon probe. The
  16. * filename of the microcode should be fsl_qe_ucode_uart_X_YZ.bin, where "X"
  17. * is the name of the SOC (e.g. 8323), and YZ is the revision of the SOC,
  18. * (e.g. "11" for 1.1).
  19. */
  20. #include <linux/module.h>
  21. #include <linux/serial.h>
  22. #include <linux/serial_core.h>
  23. #include <linux/slab.h>
  24. #include <linux/tty.h>
  25. #include <linux/tty_flip.h>
  26. #include <linux/io.h>
  27. #include <linux/of_address.h>
  28. #include <linux/of_irq.h>
  29. #include <linux/of_platform.h>
  30. #include <linux/dma-mapping.h>
  31. #include <linux/fs_uart_pd.h>
  32. #include <asm/ucc_slow.h>
  33. #include <linux/firmware.h>
  34. #include <asm/reg.h>
  35. /*
  36. * The GUMR flag for Soft UART. This would normally be defined in qe.h,
  37. * but Soft-UART is a hack and we want to keep everything related to it in
  38. * this file.
  39. */
  40. #define UCC_SLOW_GUMR_H_SUART 0x00004000 /* Soft-UART */
  41. /*
  42. * soft_uart is 1 if we need to use Soft-UART mode
  43. */
  44. static int soft_uart;
  45. /*
  46. * firmware_loaded is 1 if the firmware has been loaded, 0 otherwise.
  47. */
  48. static int firmware_loaded;
  49. /* Enable this macro to configure all serial ports in internal loopback
  50. mode */
  51. /* #define LOOPBACK */
  52. /* The major and minor device numbers are defined in
  53. * http://www.lanana.org/docs/device-list/devices-2.6+.txt. For the QE
  54. * UART, we have major number 204 and minor numbers 46 - 49, which are the
  55. * same as for the CPM2. This decision was made because no Freescale part
  56. * has both a CPM and a QE.
  57. */
  58. #define SERIAL_QE_MAJOR 204
  59. #define SERIAL_QE_MINOR 46
  60. /* Since we only have minor numbers 46 - 49, there is a hard limit of 4 ports */
  61. #define UCC_MAX_UART 4
  62. /* The number of buffer descriptors for receiving characters. */
  63. #define RX_NUM_FIFO 4
  64. /* The number of buffer descriptors for transmitting characters. */
  65. #define TX_NUM_FIFO 4
  66. /* The maximum size of the character buffer for a single RX BD. */
  67. #define RX_BUF_SIZE 32
  68. /* The maximum size of the character buffer for a single TX BD. */
  69. #define TX_BUF_SIZE 32
  70. /*
  71. * The number of jiffies to wait after receiving a close command before the
  72. * device is actually closed. This allows the last few characters to be
  73. * sent over the wire.
  74. */
  75. #define UCC_WAIT_CLOSING 100
  76. struct ucc_uart_pram {
  77. struct ucc_slow_pram common;
  78. u8 res1[8]; /* reserved */
  79. __be16 maxidl; /* Maximum idle chars */
  80. __be16 idlc; /* temp idle counter */
  81. __be16 brkcr; /* Break count register */
  82. __be16 parec; /* receive parity error counter */
  83. __be16 frmec; /* receive framing error counter */
  84. __be16 nosec; /* receive noise counter */
  85. __be16 brkec; /* receive break condition counter */
  86. __be16 brkln; /* last received break length */
  87. __be16 uaddr[2]; /* UART address character 1 & 2 */
  88. __be16 rtemp; /* Temp storage */
  89. __be16 toseq; /* Transmit out of sequence char */
  90. __be16 cchars[8]; /* control characters 1-8 */
  91. __be16 rccm; /* receive control character mask */
  92. __be16 rccr; /* receive control character register */
  93. __be16 rlbc; /* receive last break character */
  94. __be16 res2; /* reserved */
  95. __be32 res3; /* reserved, should be cleared */
  96. u8 res4; /* reserved, should be cleared */
  97. u8 res5[3]; /* reserved, should be cleared */
  98. __be32 res6; /* reserved, should be cleared */
  99. __be32 res7; /* reserved, should be cleared */
  100. __be32 res8; /* reserved, should be cleared */
  101. __be32 res9; /* reserved, should be cleared */
  102. __be32 res10; /* reserved, should be cleared */
  103. __be32 res11; /* reserved, should be cleared */
  104. __be32 res12; /* reserved, should be cleared */
  105. __be32 res13; /* reserved, should be cleared */
  106. /* The rest is for Soft-UART only */
  107. __be16 supsmr; /* 0x90, Shadow UPSMR */
  108. __be16 res92; /* 0x92, reserved, initialize to 0 */
  109. __be32 rx_state; /* 0x94, RX state, initialize to 0 */
  110. __be32 rx_cnt; /* 0x98, RX count, initialize to 0 */
  111. u8 rx_length; /* 0x9C, Char length, set to 1+CL+PEN+1+SL */
  112. u8 rx_bitmark; /* 0x9D, reserved, initialize to 0 */
  113. u8 rx_temp_dlst_qe; /* 0x9E, reserved, initialize to 0 */
  114. u8 res14[0xBC - 0x9F]; /* reserved */
  115. __be32 dump_ptr; /* 0xBC, Dump pointer */
  116. __be32 rx_frame_rem; /* 0xC0, reserved, initialize to 0 */
  117. u8 rx_frame_rem_size; /* 0xC4, reserved, initialize to 0 */
  118. u8 tx_mode; /* 0xC5, mode, 0=AHDLC, 1=UART */
  119. __be16 tx_state; /* 0xC6, TX state */
  120. u8 res15[0xD0 - 0xC8]; /* reserved */
  121. __be32 resD0; /* 0xD0, reserved, initialize to 0 */
  122. u8 resD4; /* 0xD4, reserved, initialize to 0 */
  123. __be16 resD5; /* 0xD5, reserved, initialize to 0 */
  124. } __attribute__ ((packed));
  125. /* SUPSMR definitions, for Soft-UART only */
  126. #define UCC_UART_SUPSMR_SL 0x8000
  127. #define UCC_UART_SUPSMR_RPM_MASK 0x6000
  128. #define UCC_UART_SUPSMR_RPM_ODD 0x0000
  129. #define UCC_UART_SUPSMR_RPM_LOW 0x2000
  130. #define UCC_UART_SUPSMR_RPM_EVEN 0x4000
  131. #define UCC_UART_SUPSMR_RPM_HIGH 0x6000
  132. #define UCC_UART_SUPSMR_PEN 0x1000
  133. #define UCC_UART_SUPSMR_TPM_MASK 0x0C00
  134. #define UCC_UART_SUPSMR_TPM_ODD 0x0000
  135. #define UCC_UART_SUPSMR_TPM_LOW 0x0400
  136. #define UCC_UART_SUPSMR_TPM_EVEN 0x0800
  137. #define UCC_UART_SUPSMR_TPM_HIGH 0x0C00
  138. #define UCC_UART_SUPSMR_FRZ 0x0100
  139. #define UCC_UART_SUPSMR_UM_MASK 0x00c0
  140. #define UCC_UART_SUPSMR_UM_NORMAL 0x0000
  141. #define UCC_UART_SUPSMR_UM_MAN_MULTI 0x0040
  142. #define UCC_UART_SUPSMR_UM_AUTO_MULTI 0x00c0
  143. #define UCC_UART_SUPSMR_CL_MASK 0x0030
  144. #define UCC_UART_SUPSMR_CL_8 0x0030
  145. #define UCC_UART_SUPSMR_CL_7 0x0020
  146. #define UCC_UART_SUPSMR_CL_6 0x0010
  147. #define UCC_UART_SUPSMR_CL_5 0x0000
  148. #define UCC_UART_TX_STATE_AHDLC 0x00
  149. #define UCC_UART_TX_STATE_UART 0x01
  150. #define UCC_UART_TX_STATE_X1 0x00
  151. #define UCC_UART_TX_STATE_X16 0x80
  152. #define UCC_UART_PRAM_ALIGNMENT 0x100
  153. #define UCC_UART_SIZE_OF_BD UCC_SLOW_SIZE_OF_BD
  154. #define NUM_CONTROL_CHARS 8
  155. /* Private per-port data structure */
  156. struct uart_qe_port {
  157. struct uart_port port;
  158. struct ucc_slow __iomem *uccp;
  159. struct ucc_uart_pram __iomem *uccup;
  160. struct ucc_slow_info us_info;
  161. struct ucc_slow_private *us_private;
  162. struct device_node *np;
  163. unsigned int ucc_num; /* First ucc is 0, not 1 */
  164. u16 rx_nrfifos;
  165. u16 rx_fifosize;
  166. u16 tx_nrfifos;
  167. u16 tx_fifosize;
  168. int wait_closing;
  169. u32 flags;
  170. struct qe_bd *rx_bd_base;
  171. struct qe_bd *rx_cur;
  172. struct qe_bd *tx_bd_base;
  173. struct qe_bd *tx_cur;
  174. unsigned char *tx_buf;
  175. unsigned char *rx_buf;
  176. void *bd_virt; /* virtual address of the BD buffers */
  177. dma_addr_t bd_dma_addr; /* bus address of the BD buffers */
  178. unsigned int bd_size; /* size of BD buffer space */
  179. };
  180. static struct uart_driver ucc_uart_driver = {
  181. .owner = THIS_MODULE,
  182. .driver_name = "ucc_uart",
  183. .dev_name = "ttyQE",
  184. .major = SERIAL_QE_MAJOR,
  185. .minor = SERIAL_QE_MINOR,
  186. .nr = UCC_MAX_UART,
  187. };
  188. /*
  189. * Virtual to physical address translation.
  190. *
  191. * Given the virtual address for a character buffer, this function returns
  192. * the physical (DMA) equivalent.
  193. */
  194. static inline dma_addr_t cpu2qe_addr(void *addr, struct uart_qe_port *qe_port)
  195. {
  196. if (likely((addr >= qe_port->bd_virt)) &&
  197. (addr < (qe_port->bd_virt + qe_port->bd_size)))
  198. return qe_port->bd_dma_addr + (addr - qe_port->bd_virt);
  199. /* something nasty happened */
  200. printk(KERN_ERR "%s: addr=%p\n", __func__, addr);
  201. BUG();
  202. return 0;
  203. }
  204. /*
  205. * Physical to virtual address translation.
  206. *
  207. * Given the physical (DMA) address for a character buffer, this function
  208. * returns the virtual equivalent.
  209. */
  210. static inline void *qe2cpu_addr(dma_addr_t addr, struct uart_qe_port *qe_port)
  211. {
  212. /* sanity check */
  213. if (likely((addr >= qe_port->bd_dma_addr) &&
  214. (addr < (qe_port->bd_dma_addr + qe_port->bd_size))))
  215. return qe_port->bd_virt + (addr - qe_port->bd_dma_addr);
  216. /* something nasty happened */
  217. printk(KERN_ERR "%s: addr=%llx\n", __func__, (u64)addr);
  218. BUG();
  219. return NULL;
  220. }
  221. /*
  222. * Return 1 if the QE is done transmitting all buffers for this port
  223. *
  224. * This function scans each BD in sequence. If we find a BD that is not
  225. * ready (READY=1), then we return 0 indicating that the QE is still sending
  226. * data. If we reach the last BD (WRAP=1), then we know we've scanned
  227. * the entire list, and all BDs are done.
  228. */
  229. static unsigned int qe_uart_tx_empty(struct uart_port *port)
  230. {
  231. struct uart_qe_port *qe_port =
  232. container_of(port, struct uart_qe_port, port);
  233. struct qe_bd *bdp = qe_port->tx_bd_base;
  234. while (1) {
  235. if (in_be16(&bdp->status) & BD_SC_READY)
  236. /* This BD is not done, so return "not done" */
  237. return 0;
  238. if (in_be16(&bdp->status) & BD_SC_WRAP)
  239. /*
  240. * This BD is done and it's the last one, so return
  241. * "done"
  242. */
  243. return 1;
  244. bdp++;
  245. };
  246. }
  247. /*
  248. * Set the modem control lines
  249. *
  250. * Although the QE can control the modem control lines (e.g. CTS), we
  251. * don't need that support. This function must exist, however, otherwise
  252. * the kernel will panic.
  253. */
  254. void qe_uart_set_mctrl(struct uart_port *port, unsigned int mctrl)
  255. {
  256. }
  257. /*
  258. * Get the current modem control line status
  259. *
  260. * Although the QE can control the modem control lines (e.g. CTS), this
  261. * driver currently doesn't support that, so we always return Carrier
  262. * Detect, Data Set Ready, and Clear To Send.
  263. */
  264. static unsigned int qe_uart_get_mctrl(struct uart_port *port)
  265. {
  266. return TIOCM_CAR | TIOCM_DSR | TIOCM_CTS;
  267. }
  268. /*
  269. * Disable the transmit interrupt.
  270. *
  271. * Although this function is called "stop_tx", it does not actually stop
  272. * transmission of data. Instead, it tells the QE to not generate an
  273. * interrupt when the UCC is finished sending characters.
  274. */
  275. static void qe_uart_stop_tx(struct uart_port *port)
  276. {
  277. struct uart_qe_port *qe_port =
  278. container_of(port, struct uart_qe_port, port);
  279. clrbits16(&qe_port->uccp->uccm, UCC_UART_UCCE_TX);
  280. }
  281. /*
  282. * Transmit as many characters to the HW as possible.
  283. *
  284. * This function will attempt to stuff of all the characters from the
  285. * kernel's transmit buffer into TX BDs.
  286. *
  287. * A return value of non-zero indicates that it successfully stuffed all
  288. * characters from the kernel buffer.
  289. *
  290. * A return value of zero indicates that there are still characters in the
  291. * kernel's buffer that have not been transmitted, but there are no more BDs
  292. * available. This function should be called again after a BD has been made
  293. * available.
  294. */
  295. static int qe_uart_tx_pump(struct uart_qe_port *qe_port)
  296. {
  297. struct qe_bd *bdp;
  298. unsigned char *p;
  299. unsigned int count;
  300. struct uart_port *port = &qe_port->port;
  301. struct circ_buf *xmit = &port->state->xmit;
  302. bdp = qe_port->rx_cur;
  303. /* Handle xon/xoff */
  304. if (port->x_char) {
  305. /* Pick next descriptor and fill from buffer */
  306. bdp = qe_port->tx_cur;
  307. p = qe2cpu_addr(bdp->buf, qe_port);
  308. *p++ = port->x_char;
  309. out_be16(&bdp->length, 1);
  310. setbits16(&bdp->status, BD_SC_READY);
  311. /* Get next BD. */
  312. if (in_be16(&bdp->status) & BD_SC_WRAP)
  313. bdp = qe_port->tx_bd_base;
  314. else
  315. bdp++;
  316. qe_port->tx_cur = bdp;
  317. port->icount.tx++;
  318. port->x_char = 0;
  319. return 1;
  320. }
  321. if (uart_circ_empty(xmit) || uart_tx_stopped(port)) {
  322. qe_uart_stop_tx(port);
  323. return 0;
  324. }
  325. /* Pick next descriptor and fill from buffer */
  326. bdp = qe_port->tx_cur;
  327. while (!(in_be16(&bdp->status) & BD_SC_READY) &&
  328. (xmit->tail != xmit->head)) {
  329. count = 0;
  330. p = qe2cpu_addr(bdp->buf, qe_port);
  331. while (count < qe_port->tx_fifosize) {
  332. *p++ = xmit->buf[xmit->tail];
  333. xmit->tail = (xmit->tail + 1) & (UART_XMIT_SIZE - 1);
  334. port->icount.tx++;
  335. count++;
  336. if (xmit->head == xmit->tail)
  337. break;
  338. }
  339. out_be16(&bdp->length, count);
  340. setbits16(&bdp->status, BD_SC_READY);
  341. /* Get next BD. */
  342. if (in_be16(&bdp->status) & BD_SC_WRAP)
  343. bdp = qe_port->tx_bd_base;
  344. else
  345. bdp++;
  346. }
  347. qe_port->tx_cur = bdp;
  348. if (uart_circ_chars_pending(xmit) < WAKEUP_CHARS)
  349. uart_write_wakeup(port);
  350. if (uart_circ_empty(xmit)) {
  351. /* The kernel buffer is empty, so turn off TX interrupts. We
  352. don't need to be told when the QE is finished transmitting
  353. the data. */
  354. qe_uart_stop_tx(port);
  355. return 0;
  356. }
  357. return 1;
  358. }
  359. /*
  360. * Start transmitting data
  361. *
  362. * This function will start transmitting any available data, if the port
  363. * isn't already transmitting data.
  364. */
  365. static void qe_uart_start_tx(struct uart_port *port)
  366. {
  367. struct uart_qe_port *qe_port =
  368. container_of(port, struct uart_qe_port, port);
  369. /* If we currently are transmitting, then just return */
  370. if (in_be16(&qe_port->uccp->uccm) & UCC_UART_UCCE_TX)
  371. return;
  372. /* Otherwise, pump the port and start transmission */
  373. if (qe_uart_tx_pump(qe_port))
  374. setbits16(&qe_port->uccp->uccm, UCC_UART_UCCE_TX);
  375. }
  376. /*
  377. * Stop transmitting data
  378. */
  379. static void qe_uart_stop_rx(struct uart_port *port)
  380. {
  381. struct uart_qe_port *qe_port =
  382. container_of(port, struct uart_qe_port, port);
  383. clrbits16(&qe_port->uccp->uccm, UCC_UART_UCCE_RX);
  384. }
  385. /*
  386. * Enable status change interrupts
  387. *
  388. * We don't support status change interrupts, but we need to define this
  389. * function otherwise the kernel will panic.
  390. */
  391. static void qe_uart_enable_ms(struct uart_port *port)
  392. {
  393. }
  394. /* Start or stop sending break signal
  395. *
  396. * This function controls the sending of a break signal. If break_state=1,
  397. * then we start sending a break signal. If break_state=0, then we stop
  398. * sending the break signal.
  399. */
  400. static void qe_uart_break_ctl(struct uart_port *port, int break_state)
  401. {
  402. struct uart_qe_port *qe_port =
  403. container_of(port, struct uart_qe_port, port);
  404. if (break_state)
  405. ucc_slow_stop_tx(qe_port->us_private);
  406. else
  407. ucc_slow_restart_tx(qe_port->us_private);
  408. }
  409. /* ISR helper function for receiving character.
  410. *
  411. * This function is called by the ISR to handling receiving characters
  412. */
  413. static void qe_uart_int_rx(struct uart_qe_port *qe_port)
  414. {
  415. int i;
  416. unsigned char ch, *cp;
  417. struct uart_port *port = &qe_port->port;
  418. struct tty_port *tport = &port->state->port;
  419. struct qe_bd *bdp;
  420. u16 status;
  421. unsigned int flg;
  422. /* Just loop through the closed BDs and copy the characters into
  423. * the buffer.
  424. */
  425. bdp = qe_port->rx_cur;
  426. while (1) {
  427. status = in_be16(&bdp->status);
  428. /* If this one is empty, then we assume we've read them all */
  429. if (status & BD_SC_EMPTY)
  430. break;
  431. /* get number of characters, and check space in RX buffer */
  432. i = in_be16(&bdp->length);
  433. /* If we don't have enough room in RX buffer for the entire BD,
  434. * then we try later, which will be the next RX interrupt.
  435. */
  436. if (tty_buffer_request_room(tport, i) < i) {
  437. dev_dbg(port->dev, "ucc-uart: no room in RX buffer\n");
  438. return;
  439. }
  440. /* get pointer */
  441. cp = qe2cpu_addr(bdp->buf, qe_port);
  442. /* loop through the buffer */
  443. while (i-- > 0) {
  444. ch = *cp++;
  445. port->icount.rx++;
  446. flg = TTY_NORMAL;
  447. if (!i && status &
  448. (BD_SC_BR | BD_SC_FR | BD_SC_PR | BD_SC_OV))
  449. goto handle_error;
  450. if (uart_handle_sysrq_char(port, ch))
  451. continue;
  452. error_return:
  453. tty_insert_flip_char(tport, ch, flg);
  454. }
  455. /* This BD is ready to be used again. Clear status. get next */
  456. clrsetbits_be16(&bdp->status, BD_SC_BR | BD_SC_FR | BD_SC_PR |
  457. BD_SC_OV | BD_SC_ID, BD_SC_EMPTY);
  458. if (in_be16(&bdp->status) & BD_SC_WRAP)
  459. bdp = qe_port->rx_bd_base;
  460. else
  461. bdp++;
  462. }
  463. /* Write back buffer pointer */
  464. qe_port->rx_cur = bdp;
  465. /* Activate BH processing */
  466. tty_flip_buffer_push(tport);
  467. return;
  468. /* Error processing */
  469. handle_error:
  470. /* Statistics */
  471. if (status & BD_SC_BR)
  472. port->icount.brk++;
  473. if (status & BD_SC_PR)
  474. port->icount.parity++;
  475. if (status & BD_SC_FR)
  476. port->icount.frame++;
  477. if (status & BD_SC_OV)
  478. port->icount.overrun++;
  479. /* Mask out ignored conditions */
  480. status &= port->read_status_mask;
  481. /* Handle the remaining ones */
  482. if (status & BD_SC_BR)
  483. flg = TTY_BREAK;
  484. else if (status & BD_SC_PR)
  485. flg = TTY_PARITY;
  486. else if (status & BD_SC_FR)
  487. flg = TTY_FRAME;
  488. /* Overrun does not affect the current character ! */
  489. if (status & BD_SC_OV)
  490. tty_insert_flip_char(tport, 0, TTY_OVERRUN);
  491. #ifdef SUPPORT_SYSRQ
  492. port->sysrq = 0;
  493. #endif
  494. goto error_return;
  495. }
  496. /* Interrupt handler
  497. *
  498. * This interrupt handler is called after a BD is processed.
  499. */
  500. static irqreturn_t qe_uart_int(int irq, void *data)
  501. {
  502. struct uart_qe_port *qe_port = (struct uart_qe_port *) data;
  503. struct ucc_slow __iomem *uccp = qe_port->uccp;
  504. u16 events;
  505. /* Clear the interrupts */
  506. events = in_be16(&uccp->ucce);
  507. out_be16(&uccp->ucce, events);
  508. if (events & UCC_UART_UCCE_BRKE)
  509. uart_handle_break(&qe_port->port);
  510. if (events & UCC_UART_UCCE_RX)
  511. qe_uart_int_rx(qe_port);
  512. if (events & UCC_UART_UCCE_TX)
  513. qe_uart_tx_pump(qe_port);
  514. return events ? IRQ_HANDLED : IRQ_NONE;
  515. }
  516. /* Initialize buffer descriptors
  517. *
  518. * This function initializes all of the RX and TX buffer descriptors.
  519. */
  520. static void qe_uart_initbd(struct uart_qe_port *qe_port)
  521. {
  522. int i;
  523. void *bd_virt;
  524. struct qe_bd *bdp;
  525. /* Set the physical address of the host memory buffers in the buffer
  526. * descriptors, and the virtual address for us to work with.
  527. */
  528. bd_virt = qe_port->bd_virt;
  529. bdp = qe_port->rx_bd_base;
  530. qe_port->rx_cur = qe_port->rx_bd_base;
  531. for (i = 0; i < (qe_port->rx_nrfifos - 1); i++) {
  532. out_be16(&bdp->status, BD_SC_EMPTY | BD_SC_INTRPT);
  533. out_be32(&bdp->buf, cpu2qe_addr(bd_virt, qe_port));
  534. out_be16(&bdp->length, 0);
  535. bd_virt += qe_port->rx_fifosize;
  536. bdp++;
  537. }
  538. /* */
  539. out_be16(&bdp->status, BD_SC_WRAP | BD_SC_EMPTY | BD_SC_INTRPT);
  540. out_be32(&bdp->buf, cpu2qe_addr(bd_virt, qe_port));
  541. out_be16(&bdp->length, 0);
  542. /* Set the physical address of the host memory
  543. * buffers in the buffer descriptors, and the
  544. * virtual address for us to work with.
  545. */
  546. bd_virt = qe_port->bd_virt +
  547. L1_CACHE_ALIGN(qe_port->rx_nrfifos * qe_port->rx_fifosize);
  548. qe_port->tx_cur = qe_port->tx_bd_base;
  549. bdp = qe_port->tx_bd_base;
  550. for (i = 0; i < (qe_port->tx_nrfifos - 1); i++) {
  551. out_be16(&bdp->status, BD_SC_INTRPT);
  552. out_be32(&bdp->buf, cpu2qe_addr(bd_virt, qe_port));
  553. out_be16(&bdp->length, 0);
  554. bd_virt += qe_port->tx_fifosize;
  555. bdp++;
  556. }
  557. /* Loopback requires the preamble bit to be set on the first TX BD */
  558. #ifdef LOOPBACK
  559. setbits16(&qe_port->tx_cur->status, BD_SC_P);
  560. #endif
  561. out_be16(&bdp->status, BD_SC_WRAP | BD_SC_INTRPT);
  562. out_be32(&bdp->buf, cpu2qe_addr(bd_virt, qe_port));
  563. out_be16(&bdp->length, 0);
  564. }
  565. /*
  566. * Initialize a UCC for UART.
  567. *
  568. * This function configures a given UCC to be used as a UART device. Basic
  569. * UCC initialization is handled in qe_uart_request_port(). This function
  570. * does all the UART-specific stuff.
  571. */
  572. static void qe_uart_init_ucc(struct uart_qe_port *qe_port)
  573. {
  574. u32 cecr_subblock;
  575. struct ucc_slow __iomem *uccp = qe_port->uccp;
  576. struct ucc_uart_pram *uccup = qe_port->uccup;
  577. unsigned int i;
  578. /* First, disable TX and RX in the UCC */
  579. ucc_slow_disable(qe_port->us_private, COMM_DIR_RX_AND_TX);
  580. /* Program the UCC UART parameter RAM */
  581. out_8(&uccup->common.rbmr, UCC_BMR_GBL | UCC_BMR_BO_BE);
  582. out_8(&uccup->common.tbmr, UCC_BMR_GBL | UCC_BMR_BO_BE);
  583. out_be16(&uccup->common.mrblr, qe_port->rx_fifosize);
  584. out_be16(&uccup->maxidl, 0x10);
  585. out_be16(&uccup->brkcr, 1);
  586. out_be16(&uccup->parec, 0);
  587. out_be16(&uccup->frmec, 0);
  588. out_be16(&uccup->nosec, 0);
  589. out_be16(&uccup->brkec, 0);
  590. out_be16(&uccup->uaddr[0], 0);
  591. out_be16(&uccup->uaddr[1], 0);
  592. out_be16(&uccup->toseq, 0);
  593. for (i = 0; i < 8; i++)
  594. out_be16(&uccup->cchars[i], 0xC000);
  595. out_be16(&uccup->rccm, 0xc0ff);
  596. /* Configure the GUMR registers for UART */
  597. if (soft_uart) {
  598. /* Soft-UART requires a 1X multiplier for TX */
  599. clrsetbits_be32(&uccp->gumr_l,
  600. UCC_SLOW_GUMR_L_MODE_MASK | UCC_SLOW_GUMR_L_TDCR_MASK |
  601. UCC_SLOW_GUMR_L_RDCR_MASK,
  602. UCC_SLOW_GUMR_L_MODE_UART | UCC_SLOW_GUMR_L_TDCR_1 |
  603. UCC_SLOW_GUMR_L_RDCR_16);
  604. clrsetbits_be32(&uccp->gumr_h, UCC_SLOW_GUMR_H_RFW,
  605. UCC_SLOW_GUMR_H_TRX | UCC_SLOW_GUMR_H_TTX);
  606. } else {
  607. clrsetbits_be32(&uccp->gumr_l,
  608. UCC_SLOW_GUMR_L_MODE_MASK | UCC_SLOW_GUMR_L_TDCR_MASK |
  609. UCC_SLOW_GUMR_L_RDCR_MASK,
  610. UCC_SLOW_GUMR_L_MODE_UART | UCC_SLOW_GUMR_L_TDCR_16 |
  611. UCC_SLOW_GUMR_L_RDCR_16);
  612. clrsetbits_be32(&uccp->gumr_h,
  613. UCC_SLOW_GUMR_H_TRX | UCC_SLOW_GUMR_H_TTX,
  614. UCC_SLOW_GUMR_H_RFW);
  615. }
  616. #ifdef LOOPBACK
  617. clrsetbits_be32(&uccp->gumr_l, UCC_SLOW_GUMR_L_DIAG_MASK,
  618. UCC_SLOW_GUMR_L_DIAG_LOOP);
  619. clrsetbits_be32(&uccp->gumr_h,
  620. UCC_SLOW_GUMR_H_CTSP | UCC_SLOW_GUMR_H_RSYN,
  621. UCC_SLOW_GUMR_H_CDS);
  622. #endif
  623. /* Disable rx interrupts and clear all pending events. */
  624. out_be16(&uccp->uccm, 0);
  625. out_be16(&uccp->ucce, 0xffff);
  626. out_be16(&uccp->udsr, 0x7e7e);
  627. /* Initialize UPSMR */
  628. out_be16(&uccp->upsmr, 0);
  629. if (soft_uart) {
  630. out_be16(&uccup->supsmr, 0x30);
  631. out_be16(&uccup->res92, 0);
  632. out_be32(&uccup->rx_state, 0);
  633. out_be32(&uccup->rx_cnt, 0);
  634. out_8(&uccup->rx_bitmark, 0);
  635. out_8(&uccup->rx_length, 10);
  636. out_be32(&uccup->dump_ptr, 0x4000);
  637. out_8(&uccup->rx_temp_dlst_qe, 0);
  638. out_be32(&uccup->rx_frame_rem, 0);
  639. out_8(&uccup->rx_frame_rem_size, 0);
  640. /* Soft-UART requires TX to be 1X */
  641. out_8(&uccup->tx_mode,
  642. UCC_UART_TX_STATE_UART | UCC_UART_TX_STATE_X1);
  643. out_be16(&uccup->tx_state, 0);
  644. out_8(&uccup->resD4, 0);
  645. out_be16(&uccup->resD5, 0);
  646. /* Set UART mode.
  647. * Enable receive and transmit.
  648. */
  649. /* From the microcode errata:
  650. * 1.GUMR_L register, set mode=0010 (QMC).
  651. * 2.Set GUMR_H[17] bit. (UART/AHDLC mode).
  652. * 3.Set GUMR_H[19:20] (Transparent mode)
  653. * 4.Clear GUMR_H[26] (RFW)
  654. * ...
  655. * 6.Receiver must use 16x over sampling
  656. */
  657. clrsetbits_be32(&uccp->gumr_l,
  658. UCC_SLOW_GUMR_L_MODE_MASK | UCC_SLOW_GUMR_L_TDCR_MASK |
  659. UCC_SLOW_GUMR_L_RDCR_MASK,
  660. UCC_SLOW_GUMR_L_MODE_QMC | UCC_SLOW_GUMR_L_TDCR_16 |
  661. UCC_SLOW_GUMR_L_RDCR_16);
  662. clrsetbits_be32(&uccp->gumr_h,
  663. UCC_SLOW_GUMR_H_RFW | UCC_SLOW_GUMR_H_RSYN,
  664. UCC_SLOW_GUMR_H_SUART | UCC_SLOW_GUMR_H_TRX |
  665. UCC_SLOW_GUMR_H_TTX | UCC_SLOW_GUMR_H_TFL);
  666. #ifdef LOOPBACK
  667. clrsetbits_be32(&uccp->gumr_l, UCC_SLOW_GUMR_L_DIAG_MASK,
  668. UCC_SLOW_GUMR_L_DIAG_LOOP);
  669. clrbits32(&uccp->gumr_h, UCC_SLOW_GUMR_H_CTSP |
  670. UCC_SLOW_GUMR_H_CDS);
  671. #endif
  672. cecr_subblock = ucc_slow_get_qe_cr_subblock(qe_port->ucc_num);
  673. qe_issue_cmd(QE_INIT_TX_RX, cecr_subblock,
  674. QE_CR_PROTOCOL_UNSPECIFIED, 0);
  675. } else {
  676. cecr_subblock = ucc_slow_get_qe_cr_subblock(qe_port->ucc_num);
  677. qe_issue_cmd(QE_INIT_TX_RX, cecr_subblock,
  678. QE_CR_PROTOCOL_UART, 0);
  679. }
  680. }
  681. /*
  682. * Initialize the port.
  683. */
  684. static int qe_uart_startup(struct uart_port *port)
  685. {
  686. struct uart_qe_port *qe_port =
  687. container_of(port, struct uart_qe_port, port);
  688. int ret;
  689. /*
  690. * If we're using Soft-UART mode, then we need to make sure the
  691. * firmware has been uploaded first.
  692. */
  693. if (soft_uart && !firmware_loaded) {
  694. dev_err(port->dev, "Soft-UART firmware not uploaded\n");
  695. return -ENODEV;
  696. }
  697. qe_uart_initbd(qe_port);
  698. qe_uart_init_ucc(qe_port);
  699. /* Install interrupt handler. */
  700. ret = request_irq(port->irq, qe_uart_int, IRQF_SHARED, "ucc-uart",
  701. qe_port);
  702. if (ret) {
  703. dev_err(port->dev, "could not claim IRQ %u\n", port->irq);
  704. return ret;
  705. }
  706. /* Startup rx-int */
  707. setbits16(&qe_port->uccp->uccm, UCC_UART_UCCE_RX);
  708. ucc_slow_enable(qe_port->us_private, COMM_DIR_RX_AND_TX);
  709. return 0;
  710. }
  711. /*
  712. * Shutdown the port.
  713. */
  714. static void qe_uart_shutdown(struct uart_port *port)
  715. {
  716. struct uart_qe_port *qe_port =
  717. container_of(port, struct uart_qe_port, port);
  718. struct ucc_slow __iomem *uccp = qe_port->uccp;
  719. unsigned int timeout = 20;
  720. /* Disable RX and TX */
  721. /* Wait for all the BDs marked sent */
  722. while (!qe_uart_tx_empty(port)) {
  723. if (!--timeout) {
  724. dev_warn(port->dev, "shutdown timeout\n");
  725. break;
  726. }
  727. set_current_state(TASK_UNINTERRUPTIBLE);
  728. schedule_timeout(2);
  729. }
  730. if (qe_port->wait_closing) {
  731. /* Wait a bit longer */
  732. set_current_state(TASK_UNINTERRUPTIBLE);
  733. schedule_timeout(qe_port->wait_closing);
  734. }
  735. /* Stop uarts */
  736. ucc_slow_disable(qe_port->us_private, COMM_DIR_RX_AND_TX);
  737. clrbits16(&uccp->uccm, UCC_UART_UCCE_TX | UCC_UART_UCCE_RX);
  738. /* Shut them really down and reinit buffer descriptors */
  739. ucc_slow_graceful_stop_tx(qe_port->us_private);
  740. qe_uart_initbd(qe_port);
  741. free_irq(port->irq, qe_port);
  742. }
  743. /*
  744. * Set the serial port parameters.
  745. */
  746. static void qe_uart_set_termios(struct uart_port *port,
  747. struct ktermios *termios, struct ktermios *old)
  748. {
  749. struct uart_qe_port *qe_port =
  750. container_of(port, struct uart_qe_port, port);
  751. struct ucc_slow __iomem *uccp = qe_port->uccp;
  752. unsigned int baud;
  753. unsigned long flags;
  754. u16 upsmr = in_be16(&uccp->upsmr);
  755. struct ucc_uart_pram __iomem *uccup = qe_port->uccup;
  756. u16 supsmr = in_be16(&uccup->supsmr);
  757. u8 char_length = 2; /* 1 + CL + PEN + 1 + SL */
  758. /* Character length programmed into the mode register is the
  759. * sum of: 1 start bit, number of data bits, 0 or 1 parity bit,
  760. * 1 or 2 stop bits, minus 1.
  761. * The value 'bits' counts this for us.
  762. */
  763. /* byte size */
  764. upsmr &= UCC_UART_UPSMR_CL_MASK;
  765. supsmr &= UCC_UART_SUPSMR_CL_MASK;
  766. switch (termios->c_cflag & CSIZE) {
  767. case CS5:
  768. upsmr |= UCC_UART_UPSMR_CL_5;
  769. supsmr |= UCC_UART_SUPSMR_CL_5;
  770. char_length += 5;
  771. break;
  772. case CS6:
  773. upsmr |= UCC_UART_UPSMR_CL_6;
  774. supsmr |= UCC_UART_SUPSMR_CL_6;
  775. char_length += 6;
  776. break;
  777. case CS7:
  778. upsmr |= UCC_UART_UPSMR_CL_7;
  779. supsmr |= UCC_UART_SUPSMR_CL_7;
  780. char_length += 7;
  781. break;
  782. default: /* case CS8 */
  783. upsmr |= UCC_UART_UPSMR_CL_8;
  784. supsmr |= UCC_UART_SUPSMR_CL_8;
  785. char_length += 8;
  786. break;
  787. }
  788. /* If CSTOPB is set, we want two stop bits */
  789. if (termios->c_cflag & CSTOPB) {
  790. upsmr |= UCC_UART_UPSMR_SL;
  791. supsmr |= UCC_UART_SUPSMR_SL;
  792. char_length++; /* + SL */
  793. }
  794. if (termios->c_cflag & PARENB) {
  795. upsmr |= UCC_UART_UPSMR_PEN;
  796. supsmr |= UCC_UART_SUPSMR_PEN;
  797. char_length++; /* + PEN */
  798. if (!(termios->c_cflag & PARODD)) {
  799. upsmr &= ~(UCC_UART_UPSMR_RPM_MASK |
  800. UCC_UART_UPSMR_TPM_MASK);
  801. upsmr |= UCC_UART_UPSMR_RPM_EVEN |
  802. UCC_UART_UPSMR_TPM_EVEN;
  803. supsmr &= ~(UCC_UART_SUPSMR_RPM_MASK |
  804. UCC_UART_SUPSMR_TPM_MASK);
  805. supsmr |= UCC_UART_SUPSMR_RPM_EVEN |
  806. UCC_UART_SUPSMR_TPM_EVEN;
  807. }
  808. }
  809. /*
  810. * Set up parity check flag
  811. */
  812. port->read_status_mask = BD_SC_EMPTY | BD_SC_OV;
  813. if (termios->c_iflag & INPCK)
  814. port->read_status_mask |= BD_SC_FR | BD_SC_PR;
  815. if (termios->c_iflag & (BRKINT | PARMRK))
  816. port->read_status_mask |= BD_SC_BR;
  817. /*
  818. * Characters to ignore
  819. */
  820. port->ignore_status_mask = 0;
  821. if (termios->c_iflag & IGNPAR)
  822. port->ignore_status_mask |= BD_SC_PR | BD_SC_FR;
  823. if (termios->c_iflag & IGNBRK) {
  824. port->ignore_status_mask |= BD_SC_BR;
  825. /*
  826. * If we're ignore parity and break indicators, ignore
  827. * overruns too. (For real raw support).
  828. */
  829. if (termios->c_iflag & IGNPAR)
  830. port->ignore_status_mask |= BD_SC_OV;
  831. }
  832. /*
  833. * !!! ignore all characters if CREAD is not set
  834. */
  835. if ((termios->c_cflag & CREAD) == 0)
  836. port->read_status_mask &= ~BD_SC_EMPTY;
  837. baud = uart_get_baud_rate(port, termios, old, 0, 115200);
  838. /* Do we really need a spinlock here? */
  839. spin_lock_irqsave(&port->lock, flags);
  840. /* Update the per-port timeout. */
  841. uart_update_timeout(port, termios->c_cflag, baud);
  842. out_be16(&uccp->upsmr, upsmr);
  843. if (soft_uart) {
  844. out_be16(&uccup->supsmr, supsmr);
  845. out_8(&uccup->rx_length, char_length);
  846. /* Soft-UART requires a 1X multiplier for TX */
  847. qe_setbrg(qe_port->us_info.rx_clock, baud, 16);
  848. qe_setbrg(qe_port->us_info.tx_clock, baud, 1);
  849. } else {
  850. qe_setbrg(qe_port->us_info.rx_clock, baud, 16);
  851. qe_setbrg(qe_port->us_info.tx_clock, baud, 16);
  852. }
  853. spin_unlock_irqrestore(&port->lock, flags);
  854. }
  855. /*
  856. * Return a pointer to a string that describes what kind of port this is.
  857. */
  858. static const char *qe_uart_type(struct uart_port *port)
  859. {
  860. return "QE";
  861. }
  862. /*
  863. * Allocate any memory and I/O resources required by the port.
  864. */
  865. static int qe_uart_request_port(struct uart_port *port)
  866. {
  867. int ret;
  868. struct uart_qe_port *qe_port =
  869. container_of(port, struct uart_qe_port, port);
  870. struct ucc_slow_info *us_info = &qe_port->us_info;
  871. struct ucc_slow_private *uccs;
  872. unsigned int rx_size, tx_size;
  873. void *bd_virt;
  874. dma_addr_t bd_dma_addr = 0;
  875. ret = ucc_slow_init(us_info, &uccs);
  876. if (ret) {
  877. dev_err(port->dev, "could not initialize UCC%u\n",
  878. qe_port->ucc_num);
  879. return ret;
  880. }
  881. qe_port->us_private = uccs;
  882. qe_port->uccp = uccs->us_regs;
  883. qe_port->uccup = (struct ucc_uart_pram *) uccs->us_pram;
  884. qe_port->rx_bd_base = uccs->rx_bd;
  885. qe_port->tx_bd_base = uccs->tx_bd;
  886. /*
  887. * Allocate the transmit and receive data buffers.
  888. */
  889. rx_size = L1_CACHE_ALIGN(qe_port->rx_nrfifos * qe_port->rx_fifosize);
  890. tx_size = L1_CACHE_ALIGN(qe_port->tx_nrfifos * qe_port->tx_fifosize);
  891. bd_virt = dma_alloc_coherent(port->dev, rx_size + tx_size, &bd_dma_addr,
  892. GFP_KERNEL);
  893. if (!bd_virt) {
  894. dev_err(port->dev, "could not allocate buffer descriptors\n");
  895. return -ENOMEM;
  896. }
  897. qe_port->bd_virt = bd_virt;
  898. qe_port->bd_dma_addr = bd_dma_addr;
  899. qe_port->bd_size = rx_size + tx_size;
  900. qe_port->rx_buf = bd_virt;
  901. qe_port->tx_buf = qe_port->rx_buf + rx_size;
  902. return 0;
  903. }
  904. /*
  905. * Configure the port.
  906. *
  907. * We say we're a CPM-type port because that's mostly true. Once the device
  908. * is configured, this driver operates almost identically to the CPM serial
  909. * driver.
  910. */
  911. static void qe_uart_config_port(struct uart_port *port, int flags)
  912. {
  913. if (flags & UART_CONFIG_TYPE) {
  914. port->type = PORT_CPM;
  915. qe_uart_request_port(port);
  916. }
  917. }
  918. /*
  919. * Release any memory and I/O resources that were allocated in
  920. * qe_uart_request_port().
  921. */
  922. static void qe_uart_release_port(struct uart_port *port)
  923. {
  924. struct uart_qe_port *qe_port =
  925. container_of(port, struct uart_qe_port, port);
  926. struct ucc_slow_private *uccs = qe_port->us_private;
  927. dma_free_coherent(port->dev, qe_port->bd_size, qe_port->bd_virt,
  928. qe_port->bd_dma_addr);
  929. ucc_slow_free(uccs);
  930. }
  931. /*
  932. * Verify that the data in serial_struct is suitable for this device.
  933. */
  934. static int qe_uart_verify_port(struct uart_port *port,
  935. struct serial_struct *ser)
  936. {
  937. if (ser->type != PORT_UNKNOWN && ser->type != PORT_CPM)
  938. return -EINVAL;
  939. if (ser->irq < 0 || ser->irq >= nr_irqs)
  940. return -EINVAL;
  941. if (ser->baud_base < 9600)
  942. return -EINVAL;
  943. return 0;
  944. }
  945. /* UART operations
  946. *
  947. * Details on these functions can be found in Documentation/serial/driver
  948. */
  949. static struct uart_ops qe_uart_pops = {
  950. .tx_empty = qe_uart_tx_empty,
  951. .set_mctrl = qe_uart_set_mctrl,
  952. .get_mctrl = qe_uart_get_mctrl,
  953. .stop_tx = qe_uart_stop_tx,
  954. .start_tx = qe_uart_start_tx,
  955. .stop_rx = qe_uart_stop_rx,
  956. .enable_ms = qe_uart_enable_ms,
  957. .break_ctl = qe_uart_break_ctl,
  958. .startup = qe_uart_startup,
  959. .shutdown = qe_uart_shutdown,
  960. .set_termios = qe_uart_set_termios,
  961. .type = qe_uart_type,
  962. .release_port = qe_uart_release_port,
  963. .request_port = qe_uart_request_port,
  964. .config_port = qe_uart_config_port,
  965. .verify_port = qe_uart_verify_port,
  966. };
  967. /*
  968. * Obtain the SOC model number and revision level
  969. *
  970. * This function parses the device tree to obtain the SOC model. It then
  971. * reads the SVR register to the revision.
  972. *
  973. * The device tree stores the SOC model two different ways.
  974. *
  975. * The new way is:
  976. *
  977. * cpu@0 {
  978. * compatible = "PowerPC,8323";
  979. * device_type = "cpu";
  980. * ...
  981. *
  982. *
  983. * The old way is:
  984. * PowerPC,8323@0 {
  985. * device_type = "cpu";
  986. * ...
  987. *
  988. * This code first checks the new way, and then the old way.
  989. */
  990. static unsigned int soc_info(unsigned int *rev_h, unsigned int *rev_l)
  991. {
  992. struct device_node *np;
  993. const char *soc_string;
  994. unsigned int svr;
  995. unsigned int soc;
  996. /* Find the CPU node */
  997. np = of_find_node_by_type(NULL, "cpu");
  998. if (!np)
  999. return 0;
  1000. /* Find the compatible property */
  1001. soc_string = of_get_property(np, "compatible", NULL);
  1002. if (!soc_string)
  1003. /* No compatible property, so try the name. */
  1004. soc_string = np->name;
  1005. /* Extract the SOC number from the "PowerPC," string */
  1006. if ((sscanf(soc_string, "PowerPC,%u", &soc) != 1) || !soc)
  1007. return 0;
  1008. /* Get the revision from the SVR */
  1009. svr = mfspr(SPRN_SVR);
  1010. *rev_h = (svr >> 4) & 0xf;
  1011. *rev_l = svr & 0xf;
  1012. return soc;
  1013. }
  1014. /*
  1015. * requst_firmware_nowait() callback function
  1016. *
  1017. * This function is called by the kernel when a firmware is made available,
  1018. * or if it times out waiting for the firmware.
  1019. */
  1020. static void uart_firmware_cont(const struct firmware *fw, void *context)
  1021. {
  1022. struct qe_firmware *firmware;
  1023. struct device *dev = context;
  1024. int ret;
  1025. if (!fw) {
  1026. dev_err(dev, "firmware not found\n");
  1027. return;
  1028. }
  1029. firmware = (struct qe_firmware *) fw->data;
  1030. if (firmware->header.length != fw->size) {
  1031. dev_err(dev, "invalid firmware\n");
  1032. goto out;
  1033. }
  1034. ret = qe_upload_firmware(firmware);
  1035. if (ret) {
  1036. dev_err(dev, "could not load firmware\n");
  1037. goto out;
  1038. }
  1039. firmware_loaded = 1;
  1040. out:
  1041. release_firmware(fw);
  1042. }
  1043. static int ucc_uart_probe(struct platform_device *ofdev)
  1044. {
  1045. struct device_node *np = ofdev->dev.of_node;
  1046. const unsigned int *iprop; /* Integer OF properties */
  1047. const char *sprop; /* String OF properties */
  1048. struct uart_qe_port *qe_port = NULL;
  1049. struct resource res;
  1050. int ret;
  1051. /*
  1052. * Determine if we need Soft-UART mode
  1053. */
  1054. if (of_find_property(np, "soft-uart", NULL)) {
  1055. dev_dbg(&ofdev->dev, "using Soft-UART mode\n");
  1056. soft_uart = 1;
  1057. }
  1058. /*
  1059. * If we are using Soft-UART, determine if we need to upload the
  1060. * firmware, too.
  1061. */
  1062. if (soft_uart) {
  1063. struct qe_firmware_info *qe_fw_info;
  1064. qe_fw_info = qe_get_firmware_info();
  1065. /* Check if the firmware has been uploaded. */
  1066. if (qe_fw_info && strstr(qe_fw_info->id, "Soft-UART")) {
  1067. firmware_loaded = 1;
  1068. } else {
  1069. char filename[32];
  1070. unsigned int soc;
  1071. unsigned int rev_h;
  1072. unsigned int rev_l;
  1073. soc = soc_info(&rev_h, &rev_l);
  1074. if (!soc) {
  1075. dev_err(&ofdev->dev, "unknown CPU model\n");
  1076. return -ENXIO;
  1077. }
  1078. sprintf(filename, "fsl_qe_ucode_uart_%u_%u%u.bin",
  1079. soc, rev_h, rev_l);
  1080. dev_info(&ofdev->dev, "waiting for firmware %s\n",
  1081. filename);
  1082. /*
  1083. * We call request_firmware_nowait instead of
  1084. * request_firmware so that the driver can load and
  1085. * initialize the ports without holding up the rest of
  1086. * the kernel. If hotplug support is enabled in the
  1087. * kernel, then we use it.
  1088. */
  1089. ret = request_firmware_nowait(THIS_MODULE,
  1090. FW_ACTION_HOTPLUG, filename, &ofdev->dev,
  1091. GFP_KERNEL, &ofdev->dev, uart_firmware_cont);
  1092. if (ret) {
  1093. dev_err(&ofdev->dev,
  1094. "could not load firmware %s\n",
  1095. filename);
  1096. return ret;
  1097. }
  1098. }
  1099. }
  1100. qe_port = kzalloc(sizeof(struct uart_qe_port), GFP_KERNEL);
  1101. if (!qe_port) {
  1102. dev_err(&ofdev->dev, "can't allocate QE port structure\n");
  1103. return -ENOMEM;
  1104. }
  1105. /* Search for IRQ and mapbase */
  1106. ret = of_address_to_resource(np, 0, &res);
  1107. if (ret) {
  1108. dev_err(&ofdev->dev, "missing 'reg' property in device tree\n");
  1109. goto out_free;
  1110. }
  1111. if (!res.start) {
  1112. dev_err(&ofdev->dev, "invalid 'reg' property in device tree\n");
  1113. ret = -EINVAL;
  1114. goto out_free;
  1115. }
  1116. qe_port->port.mapbase = res.start;
  1117. /* Get the UCC number (device ID) */
  1118. /* UCCs are numbered 1-7 */
  1119. iprop = of_get_property(np, "cell-index", NULL);
  1120. if (!iprop) {
  1121. iprop = of_get_property(np, "device-id", NULL);
  1122. if (!iprop) {
  1123. dev_err(&ofdev->dev, "UCC is unspecified in "
  1124. "device tree\n");
  1125. ret = -EINVAL;
  1126. goto out_free;
  1127. }
  1128. }
  1129. if ((*iprop < 1) || (*iprop > UCC_MAX_NUM)) {
  1130. dev_err(&ofdev->dev, "no support for UCC%u\n", *iprop);
  1131. ret = -ENODEV;
  1132. goto out_free;
  1133. }
  1134. qe_port->ucc_num = *iprop - 1;
  1135. /*
  1136. * In the future, we should not require the BRG to be specified in the
  1137. * device tree. If no clock-source is specified, then just pick a BRG
  1138. * to use. This requires a new QE library function that manages BRG
  1139. * assignments.
  1140. */
  1141. sprop = of_get_property(np, "rx-clock-name", NULL);
  1142. if (!sprop) {
  1143. dev_err(&ofdev->dev, "missing rx-clock-name in device tree\n");
  1144. ret = -ENODEV;
  1145. goto out_free;
  1146. }
  1147. qe_port->us_info.rx_clock = qe_clock_source(sprop);
  1148. if ((qe_port->us_info.rx_clock < QE_BRG1) ||
  1149. (qe_port->us_info.rx_clock > QE_BRG16)) {
  1150. dev_err(&ofdev->dev, "rx-clock-name must be a BRG for UART\n");
  1151. ret = -ENODEV;
  1152. goto out_free;
  1153. }
  1154. #ifdef LOOPBACK
  1155. /* In internal loopback mode, TX and RX must use the same clock */
  1156. qe_port->us_info.tx_clock = qe_port->us_info.rx_clock;
  1157. #else
  1158. sprop = of_get_property(np, "tx-clock-name", NULL);
  1159. if (!sprop) {
  1160. dev_err(&ofdev->dev, "missing tx-clock-name in device tree\n");
  1161. ret = -ENODEV;
  1162. goto out_free;
  1163. }
  1164. qe_port->us_info.tx_clock = qe_clock_source(sprop);
  1165. #endif
  1166. if ((qe_port->us_info.tx_clock < QE_BRG1) ||
  1167. (qe_port->us_info.tx_clock > QE_BRG16)) {
  1168. dev_err(&ofdev->dev, "tx-clock-name must be a BRG for UART\n");
  1169. ret = -ENODEV;
  1170. goto out_free;
  1171. }
  1172. /* Get the port number, numbered 0-3 */
  1173. iprop = of_get_property(np, "port-number", NULL);
  1174. if (!iprop) {
  1175. dev_err(&ofdev->dev, "missing port-number in device tree\n");
  1176. ret = -EINVAL;
  1177. goto out_free;
  1178. }
  1179. qe_port->port.line = *iprop;
  1180. if (qe_port->port.line >= UCC_MAX_UART) {
  1181. dev_err(&ofdev->dev, "port-number must be 0-%u\n",
  1182. UCC_MAX_UART - 1);
  1183. ret = -EINVAL;
  1184. goto out_free;
  1185. }
  1186. qe_port->port.irq = irq_of_parse_and_map(np, 0);
  1187. if (qe_port->port.irq == 0) {
  1188. dev_err(&ofdev->dev, "could not map IRQ for UCC%u\n",
  1189. qe_port->ucc_num + 1);
  1190. ret = -EINVAL;
  1191. goto out_free;
  1192. }
  1193. /*
  1194. * Newer device trees have an "fsl,qe" compatible property for the QE
  1195. * node, but we still need to support older device trees.
  1196. */
  1197. np = of_find_compatible_node(NULL, NULL, "fsl,qe");
  1198. if (!np) {
  1199. np = of_find_node_by_type(NULL, "qe");
  1200. if (!np) {
  1201. dev_err(&ofdev->dev, "could not find 'qe' node\n");
  1202. ret = -EINVAL;
  1203. goto out_free;
  1204. }
  1205. }
  1206. iprop = of_get_property(np, "brg-frequency", NULL);
  1207. if (!iprop) {
  1208. dev_err(&ofdev->dev,
  1209. "missing brg-frequency in device tree\n");
  1210. ret = -EINVAL;
  1211. goto out_np;
  1212. }
  1213. if (*iprop)
  1214. qe_port->port.uartclk = *iprop;
  1215. else {
  1216. /*
  1217. * Older versions of U-Boot do not initialize the brg-frequency
  1218. * property, so in this case we assume the BRG frequency is
  1219. * half the QE bus frequency.
  1220. */
  1221. iprop = of_get_property(np, "bus-frequency", NULL);
  1222. if (!iprop) {
  1223. dev_err(&ofdev->dev,
  1224. "missing QE bus-frequency in device tree\n");
  1225. ret = -EINVAL;
  1226. goto out_np;
  1227. }
  1228. if (*iprop)
  1229. qe_port->port.uartclk = *iprop / 2;
  1230. else {
  1231. dev_err(&ofdev->dev,
  1232. "invalid QE bus-frequency in device tree\n");
  1233. ret = -EINVAL;
  1234. goto out_np;
  1235. }
  1236. }
  1237. spin_lock_init(&qe_port->port.lock);
  1238. qe_port->np = np;
  1239. qe_port->port.dev = &ofdev->dev;
  1240. qe_port->port.ops = &qe_uart_pops;
  1241. qe_port->port.iotype = UPIO_MEM;
  1242. qe_port->tx_nrfifos = TX_NUM_FIFO;
  1243. qe_port->tx_fifosize = TX_BUF_SIZE;
  1244. qe_port->rx_nrfifos = RX_NUM_FIFO;
  1245. qe_port->rx_fifosize = RX_BUF_SIZE;
  1246. qe_port->wait_closing = UCC_WAIT_CLOSING;
  1247. qe_port->port.fifosize = 512;
  1248. qe_port->port.flags = UPF_BOOT_AUTOCONF | UPF_IOREMAP;
  1249. qe_port->us_info.ucc_num = qe_port->ucc_num;
  1250. qe_port->us_info.regs = (phys_addr_t) res.start;
  1251. qe_port->us_info.irq = qe_port->port.irq;
  1252. qe_port->us_info.rx_bd_ring_len = qe_port->rx_nrfifos;
  1253. qe_port->us_info.tx_bd_ring_len = qe_port->tx_nrfifos;
  1254. /* Make sure ucc_slow_init() initializes both TX and RX */
  1255. qe_port->us_info.init_tx = 1;
  1256. qe_port->us_info.init_rx = 1;
  1257. /* Add the port to the uart sub-system. This will cause
  1258. * qe_uart_config_port() to be called, so the us_info structure must
  1259. * be initialized.
  1260. */
  1261. ret = uart_add_one_port(&ucc_uart_driver, &qe_port->port);
  1262. if (ret) {
  1263. dev_err(&ofdev->dev, "could not add /dev/ttyQE%u\n",
  1264. qe_port->port.line);
  1265. goto out_np;
  1266. }
  1267. platform_set_drvdata(ofdev, qe_port);
  1268. dev_info(&ofdev->dev, "UCC%u assigned to /dev/ttyQE%u\n",
  1269. qe_port->ucc_num + 1, qe_port->port.line);
  1270. /* Display the mknod command for this device */
  1271. dev_dbg(&ofdev->dev, "mknod command is 'mknod /dev/ttyQE%u c %u %u'\n",
  1272. qe_port->port.line, SERIAL_QE_MAJOR,
  1273. SERIAL_QE_MINOR + qe_port->port.line);
  1274. return 0;
  1275. out_np:
  1276. of_node_put(np);
  1277. out_free:
  1278. kfree(qe_port);
  1279. return ret;
  1280. }
  1281. static int ucc_uart_remove(struct platform_device *ofdev)
  1282. {
  1283. struct uart_qe_port *qe_port = platform_get_drvdata(ofdev);
  1284. dev_info(&ofdev->dev, "removing /dev/ttyQE%u\n", qe_port->port.line);
  1285. uart_remove_one_port(&ucc_uart_driver, &qe_port->port);
  1286. kfree(qe_port);
  1287. return 0;
  1288. }
  1289. static struct of_device_id ucc_uart_match[] = {
  1290. {
  1291. .type = "serial",
  1292. .compatible = "ucc_uart",
  1293. },
  1294. {},
  1295. };
  1296. MODULE_DEVICE_TABLE(of, ucc_uart_match);
  1297. static struct platform_driver ucc_uart_of_driver = {
  1298. .driver = {
  1299. .name = "ucc_uart",
  1300. .owner = THIS_MODULE,
  1301. .of_match_table = ucc_uart_match,
  1302. },
  1303. .probe = ucc_uart_probe,
  1304. .remove = ucc_uart_remove,
  1305. };
  1306. static int __init ucc_uart_init(void)
  1307. {
  1308. int ret;
  1309. printk(KERN_INFO "Freescale QUICC Engine UART device driver\n");
  1310. #ifdef LOOPBACK
  1311. printk(KERN_INFO "ucc-uart: Using loopback mode\n");
  1312. #endif
  1313. ret = uart_register_driver(&ucc_uart_driver);
  1314. if (ret) {
  1315. printk(KERN_ERR "ucc-uart: could not register UART driver\n");
  1316. return ret;
  1317. }
  1318. ret = platform_driver_register(&ucc_uart_of_driver);
  1319. if (ret) {
  1320. printk(KERN_ERR
  1321. "ucc-uart: could not register platform driver\n");
  1322. uart_unregister_driver(&ucc_uart_driver);
  1323. }
  1324. return ret;
  1325. }
  1326. static void __exit ucc_uart_exit(void)
  1327. {
  1328. printk(KERN_INFO
  1329. "Freescale QUICC Engine UART device driver unloading\n");
  1330. platform_driver_unregister(&ucc_uart_of_driver);
  1331. uart_unregister_driver(&ucc_uart_driver);
  1332. }
  1333. module_init(ucc_uart_init);
  1334. module_exit(ucc_uart_exit);
  1335. MODULE_DESCRIPTION("Freescale QUICC Engine (QE) UART");
  1336. MODULE_AUTHOR("Timur Tabi <timur@freescale.com>");
  1337. MODULE_LICENSE("GPL v2");
  1338. MODULE_ALIAS_CHARDEV_MAJOR(SERIAL_QE_MAJOR);