ucc_uart.c 41 KB

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