ucc_uart.c 40 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522
  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. else
  600. clrsetbits_be32(&uccp->gumr_l,
  601. UCC_SLOW_GUMR_L_MODE_MASK | UCC_SLOW_GUMR_L_TDCR_MASK |
  602. UCC_SLOW_GUMR_L_RDCR_MASK,
  603. UCC_SLOW_GUMR_L_MODE_UART | UCC_SLOW_GUMR_L_TDCR_16 |
  604. UCC_SLOW_GUMR_L_RDCR_16);
  605. clrsetbits_be32(&uccp->gumr_h, UCC_SLOW_GUMR_H_RFW,
  606. UCC_SLOW_GUMR_H_TRX | UCC_SLOW_GUMR_H_TTX);
  607. #ifdef LOOPBACK
  608. clrsetbits_be32(&uccp->gumr_l, UCC_SLOW_GUMR_L_DIAG_MASK,
  609. UCC_SLOW_GUMR_L_DIAG_LOOP);
  610. clrsetbits_be32(&uccp->gumr_h,
  611. UCC_SLOW_GUMR_H_CTSP | UCC_SLOW_GUMR_H_RSYN,
  612. UCC_SLOW_GUMR_H_CDS);
  613. #endif
  614. /* Enable rx interrupts and clear all pending events. */
  615. out_be16(&uccp->uccm, 0);
  616. out_be16(&uccp->ucce, 0xffff);
  617. out_be16(&uccp->udsr, 0x7e7e);
  618. /* Initialize UPSMR */
  619. out_be16(&uccp->upsmr, 0);
  620. if (soft_uart) {
  621. out_be16(&uccup->supsmr, 0x30);
  622. out_be16(&uccup->res92, 0);
  623. out_be32(&uccup->rx_state, 0);
  624. out_be32(&uccup->rx_cnt, 0);
  625. out_8(&uccup->rx_bitmark, 0);
  626. out_8(&uccup->rx_length, 10);
  627. out_be32(&uccup->dump_ptr, 0x4000);
  628. out_8(&uccup->rx_temp_dlst_qe, 0);
  629. out_be32(&uccup->rx_frame_rem, 0);
  630. out_8(&uccup->rx_frame_rem_size, 0);
  631. /* Soft-UART requires TX to be 1X */
  632. out_8(&uccup->tx_mode,
  633. UCC_UART_TX_STATE_UART | UCC_UART_TX_STATE_X1);
  634. out_be16(&uccup->tx_state, 0);
  635. out_8(&uccup->resD4, 0);
  636. out_be16(&uccup->resD5, 0);
  637. /* Set UART mode.
  638. * Enable receive and transmit.
  639. */
  640. /* From the microcode errata:
  641. * 1.GUMR_L register, set mode=0010 (QMC).
  642. * 2.Set GUMR_H[17] bit. (UART/AHDLC mode).
  643. * 3.Set GUMR_H[19:20] (Transparent mode)
  644. * 4.Clear GUMR_H[26] (RFW)
  645. * ...
  646. * 6.Receiver must use 16x over sampling
  647. */
  648. clrsetbits_be32(&uccp->gumr_l,
  649. UCC_SLOW_GUMR_L_MODE_MASK | UCC_SLOW_GUMR_L_TDCR_MASK |
  650. UCC_SLOW_GUMR_L_RDCR_MASK,
  651. UCC_SLOW_GUMR_L_MODE_QMC | UCC_SLOW_GUMR_L_TDCR_16 |
  652. UCC_SLOW_GUMR_L_RDCR_16);
  653. clrsetbits_be32(&uccp->gumr_h,
  654. UCC_SLOW_GUMR_H_RFW | UCC_SLOW_GUMR_H_RSYN,
  655. UCC_SLOW_GUMR_H_SUART | UCC_SLOW_GUMR_H_TRX |
  656. UCC_SLOW_GUMR_H_TTX | UCC_SLOW_GUMR_H_TFL);
  657. #ifdef LOOPBACK
  658. clrsetbits_be32(&uccp->gumr_l, UCC_SLOW_GUMR_L_DIAG_MASK,
  659. UCC_SLOW_GUMR_L_DIAG_LOOP);
  660. clrbits32(&uccp->gumr_h, UCC_SLOW_GUMR_H_CTSP |
  661. UCC_SLOW_GUMR_H_CDS);
  662. #endif
  663. cecr_subblock = ucc_slow_get_qe_cr_subblock(qe_port->ucc_num);
  664. qe_issue_cmd(QE_INIT_TX_RX, cecr_subblock,
  665. QE_CR_PROTOCOL_UNSPECIFIED, 0);
  666. }
  667. }
  668. /*
  669. * Initialize the port.
  670. */
  671. static int qe_uart_startup(struct uart_port *port)
  672. {
  673. struct uart_qe_port *qe_port =
  674. container_of(port, struct uart_qe_port, port);
  675. int ret;
  676. /*
  677. * If we're using Soft-UART mode, then we need to make sure the
  678. * firmware has been uploaded first.
  679. */
  680. if (soft_uart && !firmware_loaded) {
  681. dev_err(port->dev, "Soft-UART firmware not uploaded\n");
  682. return -ENODEV;
  683. }
  684. qe_uart_initbd(qe_port);
  685. qe_uart_init_ucc(qe_port);
  686. /* Install interrupt handler. */
  687. ret = request_irq(port->irq, qe_uart_int, IRQF_SHARED, "ucc-uart",
  688. qe_port);
  689. if (ret) {
  690. dev_err(port->dev, "could not claim IRQ %u\n", port->irq);
  691. return ret;
  692. }
  693. /* Startup rx-int */
  694. setbits16(&qe_port->uccp->uccm, UCC_UART_UCCE_RX);
  695. ucc_slow_enable(qe_port->us_private, COMM_DIR_RX_AND_TX);
  696. return 0;
  697. }
  698. /*
  699. * Shutdown the port.
  700. */
  701. static void qe_uart_shutdown(struct uart_port *port)
  702. {
  703. struct uart_qe_port *qe_port =
  704. container_of(port, struct uart_qe_port, port);
  705. struct ucc_slow __iomem *uccp = qe_port->uccp;
  706. unsigned int timeout = 20;
  707. /* Disable RX and TX */
  708. /* Wait for all the BDs marked sent */
  709. while (!qe_uart_tx_empty(port)) {
  710. if (!--timeout) {
  711. dev_warn(port->dev, "shutdown timeout\n");
  712. break;
  713. }
  714. set_current_state(TASK_UNINTERRUPTIBLE);
  715. schedule_timeout(2);
  716. }
  717. if (qe_port->wait_closing) {
  718. /* Wait a bit longer */
  719. set_current_state(TASK_UNINTERRUPTIBLE);
  720. schedule_timeout(qe_port->wait_closing);
  721. }
  722. /* Stop uarts */
  723. ucc_slow_disable(qe_port->us_private, COMM_DIR_RX_AND_TX);
  724. clrbits16(&uccp->uccm, UCC_UART_UCCE_TX | UCC_UART_UCCE_RX);
  725. /* Shut them really down and reinit buffer descriptors */
  726. ucc_slow_graceful_stop_tx(qe_port->us_private);
  727. qe_uart_initbd(qe_port);
  728. free_irq(port->irq, qe_port);
  729. }
  730. /*
  731. * Set the serial port parameters.
  732. */
  733. static void qe_uart_set_termios(struct uart_port *port,
  734. struct ktermios *termios, struct ktermios *old)
  735. {
  736. struct uart_qe_port *qe_port =
  737. container_of(port, struct uart_qe_port, port);
  738. struct ucc_slow __iomem *uccp = qe_port->uccp;
  739. unsigned int baud;
  740. unsigned long flags;
  741. u16 upsmr = in_be16(&uccp->upsmr);
  742. struct ucc_uart_pram __iomem *uccup = qe_port->uccup;
  743. u16 supsmr = in_be16(&uccup->supsmr);
  744. u8 char_length = 2; /* 1 + CL + PEN + 1 + SL */
  745. /* Character length programmed into the mode register is the
  746. * sum of: 1 start bit, number of data bits, 0 or 1 parity bit,
  747. * 1 or 2 stop bits, minus 1.
  748. * The value 'bits' counts this for us.
  749. */
  750. /* byte size */
  751. upsmr &= UCC_UART_UPSMR_CL_MASK;
  752. supsmr &= UCC_UART_SUPSMR_CL_MASK;
  753. switch (termios->c_cflag & CSIZE) {
  754. case CS5:
  755. upsmr |= UCC_UART_UPSMR_CL_5;
  756. supsmr |= UCC_UART_SUPSMR_CL_5;
  757. char_length += 5;
  758. break;
  759. case CS6:
  760. upsmr |= UCC_UART_UPSMR_CL_6;
  761. supsmr |= UCC_UART_SUPSMR_CL_6;
  762. char_length += 6;
  763. break;
  764. case CS7:
  765. upsmr |= UCC_UART_UPSMR_CL_7;
  766. supsmr |= UCC_UART_SUPSMR_CL_7;
  767. char_length += 7;
  768. break;
  769. default: /* case CS8 */
  770. upsmr |= UCC_UART_UPSMR_CL_8;
  771. supsmr |= UCC_UART_SUPSMR_CL_8;
  772. char_length += 8;
  773. break;
  774. }
  775. /* If CSTOPB is set, we want two stop bits */
  776. if (termios->c_cflag & CSTOPB) {
  777. upsmr |= UCC_UART_UPSMR_SL;
  778. supsmr |= UCC_UART_SUPSMR_SL;
  779. char_length++; /* + SL */
  780. }
  781. if (termios->c_cflag & PARENB) {
  782. upsmr |= UCC_UART_UPSMR_PEN;
  783. supsmr |= UCC_UART_SUPSMR_PEN;
  784. char_length++; /* + PEN */
  785. if (!(termios->c_cflag & PARODD)) {
  786. upsmr &= ~(UCC_UART_UPSMR_RPM_MASK |
  787. UCC_UART_UPSMR_TPM_MASK);
  788. upsmr |= UCC_UART_UPSMR_RPM_EVEN |
  789. UCC_UART_UPSMR_TPM_EVEN;
  790. supsmr &= ~(UCC_UART_SUPSMR_RPM_MASK |
  791. UCC_UART_SUPSMR_TPM_MASK);
  792. supsmr |= UCC_UART_SUPSMR_RPM_EVEN |
  793. UCC_UART_SUPSMR_TPM_EVEN;
  794. }
  795. }
  796. /*
  797. * Set up parity check flag
  798. */
  799. port->read_status_mask = BD_SC_EMPTY | BD_SC_OV;
  800. if (termios->c_iflag & INPCK)
  801. port->read_status_mask |= BD_SC_FR | BD_SC_PR;
  802. if (termios->c_iflag & (BRKINT | PARMRK))
  803. port->read_status_mask |= BD_SC_BR;
  804. /*
  805. * Characters to ignore
  806. */
  807. port->ignore_status_mask = 0;
  808. if (termios->c_iflag & IGNPAR)
  809. port->ignore_status_mask |= BD_SC_PR | BD_SC_FR;
  810. if (termios->c_iflag & IGNBRK) {
  811. port->ignore_status_mask |= BD_SC_BR;
  812. /*
  813. * If we're ignore parity and break indicators, ignore
  814. * overruns too. (For real raw support).
  815. */
  816. if (termios->c_iflag & IGNPAR)
  817. port->ignore_status_mask |= BD_SC_OV;
  818. }
  819. /*
  820. * !!! ignore all characters if CREAD is not set
  821. */
  822. if ((termios->c_cflag & CREAD) == 0)
  823. port->read_status_mask &= ~BD_SC_EMPTY;
  824. baud = uart_get_baud_rate(port, termios, old, 0, 115200);
  825. /* Do we really need a spinlock here? */
  826. spin_lock_irqsave(&port->lock, flags);
  827. out_be16(&uccp->upsmr, upsmr);
  828. if (soft_uart) {
  829. out_be16(&uccup->supsmr, supsmr);
  830. out_8(&uccup->rx_length, char_length);
  831. /* Soft-UART requires a 1X multiplier for TX */
  832. qe_setbrg(qe_port->us_info.rx_clock, baud, 16);
  833. qe_setbrg(qe_port->us_info.tx_clock, baud, 1);
  834. } else {
  835. qe_setbrg(qe_port->us_info.rx_clock, baud, 16);
  836. qe_setbrg(qe_port->us_info.tx_clock, baud, 16);
  837. }
  838. spin_unlock_irqrestore(&port->lock, flags);
  839. }
  840. /*
  841. * Return a pointer to a string that describes what kind of port this is.
  842. */
  843. static const char *qe_uart_type(struct uart_port *port)
  844. {
  845. return "QE";
  846. }
  847. /*
  848. * Allocate any memory and I/O resources required by the port.
  849. */
  850. static int qe_uart_request_port(struct uart_port *port)
  851. {
  852. int ret;
  853. struct uart_qe_port *qe_port =
  854. container_of(port, struct uart_qe_port, port);
  855. struct ucc_slow_info *us_info = &qe_port->us_info;
  856. struct ucc_slow_private *uccs;
  857. unsigned int rx_size, tx_size;
  858. void *bd_virt;
  859. dma_addr_t bd_dma_addr = 0;
  860. ret = ucc_slow_init(us_info, &uccs);
  861. if (ret) {
  862. dev_err(port->dev, "could not initialize UCC%u\n",
  863. qe_port->ucc_num);
  864. return ret;
  865. }
  866. qe_port->us_private = uccs;
  867. qe_port->uccp = uccs->us_regs;
  868. qe_port->uccup = (struct ucc_uart_pram *) uccs->us_pram;
  869. qe_port->rx_bd_base = uccs->rx_bd;
  870. qe_port->tx_bd_base = uccs->tx_bd;
  871. /*
  872. * Allocate the transmit and receive data buffers.
  873. */
  874. rx_size = L1_CACHE_ALIGN(qe_port->rx_nrfifos * qe_port->rx_fifosize);
  875. tx_size = L1_CACHE_ALIGN(qe_port->tx_nrfifos * qe_port->tx_fifosize);
  876. bd_virt = dma_alloc_coherent(NULL, rx_size + tx_size, &bd_dma_addr,
  877. GFP_KERNEL);
  878. if (!bd_virt) {
  879. dev_err(port->dev, "could not allocate buffer descriptors\n");
  880. return -ENOMEM;
  881. }
  882. qe_port->bd_virt = bd_virt;
  883. qe_port->bd_dma_addr = bd_dma_addr;
  884. qe_port->bd_size = rx_size + tx_size;
  885. qe_port->rx_buf = bd_virt;
  886. qe_port->tx_buf = qe_port->rx_buf + rx_size;
  887. return 0;
  888. }
  889. /*
  890. * Configure the port.
  891. *
  892. * We say we're a CPM-type port because that's mostly true. Once the device
  893. * is configured, this driver operates almost identically to the CPM serial
  894. * driver.
  895. */
  896. static void qe_uart_config_port(struct uart_port *port, int flags)
  897. {
  898. if (flags & UART_CONFIG_TYPE) {
  899. port->type = PORT_CPM;
  900. qe_uart_request_port(port);
  901. }
  902. }
  903. /*
  904. * Release any memory and I/O resources that were allocated in
  905. * qe_uart_request_port().
  906. */
  907. static void qe_uart_release_port(struct uart_port *port)
  908. {
  909. struct uart_qe_port *qe_port =
  910. container_of(port, struct uart_qe_port, port);
  911. struct ucc_slow_private *uccs = qe_port->us_private;
  912. dma_free_coherent(NULL, qe_port->bd_size, qe_port->bd_virt,
  913. qe_port->bd_dma_addr);
  914. ucc_slow_free(uccs);
  915. }
  916. /*
  917. * Verify that the data in serial_struct is suitable for this device.
  918. */
  919. static int qe_uart_verify_port(struct uart_port *port,
  920. struct serial_struct *ser)
  921. {
  922. if (ser->type != PORT_UNKNOWN && ser->type != PORT_CPM)
  923. return -EINVAL;
  924. if (ser->irq < 0 || ser->irq >= NR_IRQS)
  925. return -EINVAL;
  926. if (ser->baud_base < 9600)
  927. return -EINVAL;
  928. return 0;
  929. }
  930. /* UART operations
  931. *
  932. * Details on these functions can be found in Documentation/serial/driver
  933. */
  934. static struct uart_ops qe_uart_pops = {
  935. .tx_empty = qe_uart_tx_empty,
  936. .set_mctrl = qe_uart_set_mctrl,
  937. .get_mctrl = qe_uart_get_mctrl,
  938. .stop_tx = qe_uart_stop_tx,
  939. .start_tx = qe_uart_start_tx,
  940. .stop_rx = qe_uart_stop_rx,
  941. .enable_ms = qe_uart_enable_ms,
  942. .break_ctl = qe_uart_break_ctl,
  943. .startup = qe_uart_startup,
  944. .shutdown = qe_uart_shutdown,
  945. .set_termios = qe_uart_set_termios,
  946. .type = qe_uart_type,
  947. .release_port = qe_uart_release_port,
  948. .request_port = qe_uart_request_port,
  949. .config_port = qe_uart_config_port,
  950. .verify_port = qe_uart_verify_port,
  951. };
  952. /*
  953. * Obtain the SOC model number and revision level
  954. *
  955. * This function parses the device tree to obtain the SOC model. It then
  956. * reads the SVR register to the revision.
  957. *
  958. * The device tree stores the SOC model two different ways.
  959. *
  960. * The new way is:
  961. *
  962. * cpu@0 {
  963. * compatible = "PowerPC,8323";
  964. * device_type = "cpu";
  965. * ...
  966. *
  967. *
  968. * The old way is:
  969. * PowerPC,8323@0 {
  970. * device_type = "cpu";
  971. * ...
  972. *
  973. * This code first checks the new way, and then the old way.
  974. */
  975. static unsigned int soc_info(unsigned int *rev_h, unsigned int *rev_l)
  976. {
  977. struct device_node *np;
  978. const char *soc_string;
  979. unsigned int svr;
  980. unsigned int soc;
  981. /* Find the CPU node */
  982. np = of_find_node_by_type(NULL, "cpu");
  983. if (!np)
  984. return 0;
  985. /* Find the compatible property */
  986. soc_string = of_get_property(np, "compatible", NULL);
  987. if (!soc_string)
  988. /* No compatible property, so try the name. */
  989. soc_string = np->name;
  990. /* Extract the SOC number from the "PowerPC," string */
  991. if ((sscanf(soc_string, "PowerPC,%u", &soc) != 1) || !soc)
  992. return 0;
  993. /* Get the revision from the SVR */
  994. svr = mfspr(SPRN_SVR);
  995. *rev_h = (svr >> 4) & 0xf;
  996. *rev_l = svr & 0xf;
  997. return soc;
  998. }
  999. /*
  1000. * requst_firmware_nowait() callback function
  1001. *
  1002. * This function is called by the kernel when a firmware is made available,
  1003. * or if it times out waiting for the firmware.
  1004. */
  1005. static void uart_firmware_cont(const struct firmware *fw, void *context)
  1006. {
  1007. struct qe_firmware *firmware;
  1008. struct device *dev = context;
  1009. int ret;
  1010. if (!fw) {
  1011. dev_err(dev, "firmware not found\n");
  1012. return;
  1013. }
  1014. firmware = (struct qe_firmware *) fw->data;
  1015. if (firmware->header.length != fw->size) {
  1016. dev_err(dev, "invalid firmware\n");
  1017. return;
  1018. }
  1019. ret = qe_upload_firmware(firmware);
  1020. if (ret) {
  1021. dev_err(dev, "could not load firmware\n");
  1022. return;
  1023. }
  1024. firmware_loaded = 1;
  1025. }
  1026. static int ucc_uart_probe(struct of_device *ofdev,
  1027. const struct of_device_id *match)
  1028. {
  1029. struct device_node *np = ofdev->node;
  1030. const unsigned int *iprop; /* Integer OF properties */
  1031. const char *sprop; /* String OF properties */
  1032. struct uart_qe_port *qe_port = NULL;
  1033. struct resource res;
  1034. int ret;
  1035. /*
  1036. * Determine if we need Soft-UART mode
  1037. */
  1038. if (of_find_property(np, "soft-uart", NULL)) {
  1039. dev_dbg(&ofdev->dev, "using Soft-UART mode\n");
  1040. soft_uart = 1;
  1041. }
  1042. /*
  1043. * If we are using Soft-UART, determine if we need to upload the
  1044. * firmware, too.
  1045. */
  1046. if (soft_uart) {
  1047. struct qe_firmware_info *qe_fw_info;
  1048. qe_fw_info = qe_get_firmware_info();
  1049. /* Check if the firmware has been uploaded. */
  1050. if (qe_fw_info && strstr(qe_fw_info->id, "Soft-UART")) {
  1051. firmware_loaded = 1;
  1052. } else {
  1053. char filename[32];
  1054. unsigned int soc;
  1055. unsigned int rev_h;
  1056. unsigned int rev_l;
  1057. soc = soc_info(&rev_h, &rev_l);
  1058. if (!soc) {
  1059. dev_err(&ofdev->dev, "unknown CPU model\n");
  1060. return -ENXIO;
  1061. }
  1062. sprintf(filename, "fsl_qe_ucode_uart_%u_%u%u.bin",
  1063. soc, rev_h, rev_l);
  1064. dev_info(&ofdev->dev, "waiting for firmware %s\n",
  1065. filename);
  1066. /*
  1067. * We call request_firmware_nowait instead of
  1068. * request_firmware so that the driver can load and
  1069. * initialize the ports without holding up the rest of
  1070. * the kernel. If hotplug support is enabled in the
  1071. * kernel, then we use it.
  1072. */
  1073. ret = request_firmware_nowait(THIS_MODULE,
  1074. FW_ACTION_HOTPLUG, filename, &ofdev->dev,
  1075. &ofdev->dev, uart_firmware_cont);
  1076. if (ret) {
  1077. dev_err(&ofdev->dev,
  1078. "could not load firmware %s\n",
  1079. filename);
  1080. return ret;
  1081. }
  1082. }
  1083. }
  1084. qe_port = kzalloc(sizeof(struct uart_qe_port), GFP_KERNEL);
  1085. if (!qe_port) {
  1086. dev_err(&ofdev->dev, "can't allocate QE port structure\n");
  1087. return -ENOMEM;
  1088. }
  1089. /* Search for IRQ and mapbase */
  1090. ret = of_address_to_resource(np, 0, &res);
  1091. if (ret) {
  1092. dev_err(&ofdev->dev, "missing 'reg' property in device tree\n");
  1093. kfree(qe_port);
  1094. return ret;
  1095. }
  1096. if (!res.start) {
  1097. dev_err(&ofdev->dev, "invalid 'reg' property in device tree\n");
  1098. kfree(qe_port);
  1099. return -EINVAL;
  1100. }
  1101. qe_port->port.mapbase = res.start;
  1102. /* Get the UCC number (device ID) */
  1103. /* UCCs are numbered 1-7 */
  1104. iprop = of_get_property(np, "cell-index", NULL);
  1105. if (!iprop) {
  1106. iprop = of_get_property(np, "device-id", NULL);
  1107. if (!iprop) {
  1108. dev_err(&ofdev->dev, "UCC is unspecified in "
  1109. "device tree\n");
  1110. return -EINVAL;
  1111. }
  1112. }
  1113. if ((*iprop < 1) || (*iprop > UCC_MAX_NUM)) {
  1114. dev_err(&ofdev->dev, "no support for UCC%u\n", *iprop);
  1115. kfree(qe_port);
  1116. return -ENODEV;
  1117. }
  1118. qe_port->ucc_num = *iprop - 1;
  1119. /*
  1120. * In the future, we should not require the BRG to be specified in the
  1121. * device tree. If no clock-source is specified, then just pick a BRG
  1122. * to use. This requires a new QE library function that manages BRG
  1123. * assignments.
  1124. */
  1125. sprop = of_get_property(np, "rx-clock-name", NULL);
  1126. if (!sprop) {
  1127. dev_err(&ofdev->dev, "missing rx-clock-name in device tree\n");
  1128. kfree(qe_port);
  1129. return -ENODEV;
  1130. }
  1131. qe_port->us_info.rx_clock = qe_clock_source(sprop);
  1132. if ((qe_port->us_info.rx_clock < QE_BRG1) ||
  1133. (qe_port->us_info.rx_clock > QE_BRG16)) {
  1134. dev_err(&ofdev->dev, "rx-clock-name must be a BRG for UART\n");
  1135. kfree(qe_port);
  1136. return -ENODEV;
  1137. }
  1138. #ifdef LOOPBACK
  1139. /* In internal loopback mode, TX and RX must use the same clock */
  1140. qe_port->us_info.tx_clock = qe_port->us_info.rx_clock;
  1141. #else
  1142. sprop = of_get_property(np, "tx-clock-name", NULL);
  1143. if (!sprop) {
  1144. dev_err(&ofdev->dev, "missing tx-clock-name in device tree\n");
  1145. kfree(qe_port);
  1146. return -ENODEV;
  1147. }
  1148. qe_port->us_info.tx_clock = qe_clock_source(sprop);
  1149. #endif
  1150. if ((qe_port->us_info.tx_clock < QE_BRG1) ||
  1151. (qe_port->us_info.tx_clock > QE_BRG16)) {
  1152. dev_err(&ofdev->dev, "tx-clock-name must be a BRG for UART\n");
  1153. kfree(qe_port);
  1154. return -ENODEV;
  1155. }
  1156. /* Get the port number, numbered 0-3 */
  1157. iprop = of_get_property(np, "port-number", NULL);
  1158. if (!iprop) {
  1159. dev_err(&ofdev->dev, "missing port-number in device tree\n");
  1160. kfree(qe_port);
  1161. return -EINVAL;
  1162. }
  1163. qe_port->port.line = *iprop;
  1164. if (qe_port->port.line >= UCC_MAX_UART) {
  1165. dev_err(&ofdev->dev, "port-number must be 0-%u\n",
  1166. UCC_MAX_UART - 1);
  1167. kfree(qe_port);
  1168. return -EINVAL;
  1169. }
  1170. qe_port->port.irq = irq_of_parse_and_map(np, 0);
  1171. if (qe_port->port.irq == NO_IRQ) {
  1172. dev_err(&ofdev->dev, "could not map IRQ for UCC%u\n",
  1173. qe_port->ucc_num + 1);
  1174. kfree(qe_port);
  1175. return -EINVAL;
  1176. }
  1177. /*
  1178. * Newer device trees have an "fsl,qe" compatible property for the QE
  1179. * node, but we still need to support older device trees.
  1180. */
  1181. np = of_find_compatible_node(NULL, NULL, "fsl,qe");
  1182. if (!np) {
  1183. np = of_find_node_by_type(NULL, "qe");
  1184. if (!np) {
  1185. dev_err(&ofdev->dev, "could not find 'qe' node\n");
  1186. kfree(qe_port);
  1187. return -EINVAL;
  1188. }
  1189. }
  1190. iprop = of_get_property(np, "brg-frequency", NULL);
  1191. if (!iprop) {
  1192. dev_err(&ofdev->dev,
  1193. "missing brg-frequency in device tree\n");
  1194. kfree(qe_port);
  1195. return -EINVAL;
  1196. }
  1197. if (*iprop)
  1198. qe_port->port.uartclk = *iprop;
  1199. else {
  1200. /*
  1201. * Older versions of U-Boot do not initialize the brg-frequency
  1202. * property, so in this case we assume the BRG frequency is
  1203. * half the QE bus frequency.
  1204. */
  1205. iprop = of_get_property(np, "bus-frequency", NULL);
  1206. if (!iprop) {
  1207. dev_err(&ofdev->dev,
  1208. "missing QE bus-frequency in device tree\n");
  1209. kfree(qe_port);
  1210. return -EINVAL;
  1211. }
  1212. if (*iprop)
  1213. qe_port->port.uartclk = *iprop / 2;
  1214. else {
  1215. dev_err(&ofdev->dev,
  1216. "invalid QE bus-frequency in device tree\n");
  1217. kfree(qe_port);
  1218. return -EINVAL;
  1219. }
  1220. }
  1221. spin_lock_init(&qe_port->port.lock);
  1222. qe_port->np = np;
  1223. qe_port->port.dev = &ofdev->dev;
  1224. qe_port->port.ops = &qe_uart_pops;
  1225. qe_port->port.iotype = UPIO_MEM;
  1226. qe_port->tx_nrfifos = TX_NUM_FIFO;
  1227. qe_port->tx_fifosize = TX_BUF_SIZE;
  1228. qe_port->rx_nrfifos = RX_NUM_FIFO;
  1229. qe_port->rx_fifosize = RX_BUF_SIZE;
  1230. qe_port->wait_closing = UCC_WAIT_CLOSING;
  1231. qe_port->port.fifosize = 512;
  1232. qe_port->port.flags = UPF_BOOT_AUTOCONF | UPF_IOREMAP;
  1233. qe_port->us_info.ucc_num = qe_port->ucc_num;
  1234. qe_port->us_info.regs = (phys_addr_t) res.start;
  1235. qe_port->us_info.irq = qe_port->port.irq;
  1236. qe_port->us_info.rx_bd_ring_len = qe_port->rx_nrfifos;
  1237. qe_port->us_info.tx_bd_ring_len = qe_port->tx_nrfifos;
  1238. /* Make sure ucc_slow_init() initializes both TX and RX */
  1239. qe_port->us_info.init_tx = 1;
  1240. qe_port->us_info.init_rx = 1;
  1241. /* Add the port to the uart sub-system. This will cause
  1242. * qe_uart_config_port() to be called, so the us_info structure must
  1243. * be initialized.
  1244. */
  1245. ret = uart_add_one_port(&ucc_uart_driver, &qe_port->port);
  1246. if (ret) {
  1247. dev_err(&ofdev->dev, "could not add /dev/ttyQE%u\n",
  1248. qe_port->port.line);
  1249. kfree(qe_port);
  1250. return ret;
  1251. }
  1252. dev_set_drvdata(&ofdev->dev, qe_port);
  1253. dev_info(&ofdev->dev, "UCC%u assigned to /dev/ttyQE%u\n",
  1254. qe_port->ucc_num + 1, qe_port->port.line);
  1255. /* Display the mknod command for this device */
  1256. dev_dbg(&ofdev->dev, "mknod command is 'mknod /dev/ttyQE%u c %u %u'\n",
  1257. qe_port->port.line, SERIAL_QE_MAJOR,
  1258. SERIAL_QE_MINOR + qe_port->port.line);
  1259. return 0;
  1260. }
  1261. static int ucc_uart_remove(struct of_device *ofdev)
  1262. {
  1263. struct uart_qe_port *qe_port = dev_get_drvdata(&ofdev->dev);
  1264. dev_info(&ofdev->dev, "removing /dev/ttyQE%u\n", qe_port->port.line);
  1265. uart_remove_one_port(&ucc_uart_driver, &qe_port->port);
  1266. dev_set_drvdata(&ofdev->dev, NULL);
  1267. kfree(qe_port);
  1268. return 0;
  1269. }
  1270. static struct of_device_id ucc_uart_match[] = {
  1271. {
  1272. .type = "serial",
  1273. .compatible = "ucc_uart",
  1274. },
  1275. {},
  1276. };
  1277. MODULE_DEVICE_TABLE(of, ucc_uart_match);
  1278. static struct of_platform_driver ucc_uart_of_driver = {
  1279. .owner = THIS_MODULE,
  1280. .name = "ucc_uart",
  1281. .match_table = ucc_uart_match,
  1282. .probe = ucc_uart_probe,
  1283. .remove = ucc_uart_remove,
  1284. };
  1285. static int __init ucc_uart_init(void)
  1286. {
  1287. int ret;
  1288. printk(KERN_INFO "Freescale QUICC Engine UART device driver\n");
  1289. #ifdef LOOPBACK
  1290. printk(KERN_INFO "ucc-uart: Using loopback mode\n");
  1291. #endif
  1292. ret = uart_register_driver(&ucc_uart_driver);
  1293. if (ret) {
  1294. printk(KERN_ERR "ucc-uart: could not register UART driver\n");
  1295. return ret;
  1296. }
  1297. ret = of_register_platform_driver(&ucc_uart_of_driver);
  1298. if (ret)
  1299. printk(KERN_ERR
  1300. "ucc-uart: could not register platform driver\n");
  1301. return ret;
  1302. }
  1303. static void __exit ucc_uart_exit(void)
  1304. {
  1305. printk(KERN_INFO
  1306. "Freescale QUICC Engine UART device driver unloading\n");
  1307. of_unregister_platform_driver(&ucc_uart_of_driver);
  1308. uart_unregister_driver(&ucc_uart_driver);
  1309. }
  1310. module_init(ucc_uart_init);
  1311. module_exit(ucc_uart_exit);
  1312. MODULE_DESCRIPTION("Freescale QUICC Engine (QE) UART");
  1313. MODULE_AUTHOR("Timur Tabi <timur@freescale.com>");
  1314. MODULE_LICENSE("GPL v2");
  1315. MODULE_ALIAS_CHARDEV_MAJOR(SERIAL_QE_MAJOR);