ucc_uart.c 41 KB

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