pch_uart.c 40 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609
  1. /*
  2. *Copyright (C) 2010 OKI SEMICONDUCTOR CO., LTD.
  3. *
  4. *This program is free software; you can redistribute it and/or modify
  5. *it under the terms of the GNU General Public License as published by
  6. *the Free Software Foundation; version 2 of the License.
  7. *
  8. *This program is distributed in the hope that it will be useful,
  9. *but WITHOUT ANY WARRANTY; without even the implied warranty of
  10. *MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  11. *GNU General Public License for more details.
  12. *
  13. *You should have received a copy of the GNU General Public License
  14. *along with this program; if not, write to the Free Software
  15. *Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307, USA.
  16. */
  17. #include <linux/serial_reg.h>
  18. #include <linux/module.h>
  19. #include <linux/pci.h>
  20. #include <linux/serial_core.h>
  21. #include <linux/interrupt.h>
  22. #include <linux/io.h>
  23. #include <linux/dmi.h>
  24. #include <linux/dmaengine.h>
  25. #include <linux/pch_dma.h>
  26. enum {
  27. PCH_UART_HANDLED_RX_INT_SHIFT,
  28. PCH_UART_HANDLED_TX_INT_SHIFT,
  29. PCH_UART_HANDLED_RX_ERR_INT_SHIFT,
  30. PCH_UART_HANDLED_RX_TRG_INT_SHIFT,
  31. PCH_UART_HANDLED_MS_INT_SHIFT,
  32. };
  33. enum {
  34. PCH_UART_8LINE,
  35. PCH_UART_2LINE,
  36. };
  37. #define PCH_UART_DRIVER_DEVICE "ttyPCH"
  38. /* Set the max number of UART port
  39. * Intel EG20T PCH: 4 port
  40. * OKI SEMICONDUCTOR ML7213 IOH: 3 port
  41. */
  42. #define PCH_UART_NR 4
  43. #define PCH_UART_HANDLED_RX_INT (1<<((PCH_UART_HANDLED_RX_INT_SHIFT)<<1))
  44. #define PCH_UART_HANDLED_TX_INT (1<<((PCH_UART_HANDLED_TX_INT_SHIFT)<<1))
  45. #define PCH_UART_HANDLED_RX_ERR_INT (1<<((\
  46. PCH_UART_HANDLED_RX_ERR_INT_SHIFT)<<1))
  47. #define PCH_UART_HANDLED_RX_TRG_INT (1<<((\
  48. PCH_UART_HANDLED_RX_TRG_INT_SHIFT)<<1))
  49. #define PCH_UART_HANDLED_MS_INT (1<<((PCH_UART_HANDLED_MS_INT_SHIFT)<<1))
  50. #define PCH_UART_RBR 0x00
  51. #define PCH_UART_THR 0x00
  52. #define PCH_UART_IER_MASK (PCH_UART_IER_ERBFI|PCH_UART_IER_ETBEI|\
  53. PCH_UART_IER_ELSI|PCH_UART_IER_EDSSI)
  54. #define PCH_UART_IER_ERBFI 0x00000001
  55. #define PCH_UART_IER_ETBEI 0x00000002
  56. #define PCH_UART_IER_ELSI 0x00000004
  57. #define PCH_UART_IER_EDSSI 0x00000008
  58. #define PCH_UART_IIR_IP 0x00000001
  59. #define PCH_UART_IIR_IID 0x00000006
  60. #define PCH_UART_IIR_MSI 0x00000000
  61. #define PCH_UART_IIR_TRI 0x00000002
  62. #define PCH_UART_IIR_RRI 0x00000004
  63. #define PCH_UART_IIR_REI 0x00000006
  64. #define PCH_UART_IIR_TOI 0x00000008
  65. #define PCH_UART_IIR_FIFO256 0x00000020
  66. #define PCH_UART_IIR_FIFO64 PCH_UART_IIR_FIFO256
  67. #define PCH_UART_IIR_FE 0x000000C0
  68. #define PCH_UART_FCR_FIFOE 0x00000001
  69. #define PCH_UART_FCR_RFR 0x00000002
  70. #define PCH_UART_FCR_TFR 0x00000004
  71. #define PCH_UART_FCR_DMS 0x00000008
  72. #define PCH_UART_FCR_FIFO256 0x00000020
  73. #define PCH_UART_FCR_RFTL 0x000000C0
  74. #define PCH_UART_FCR_RFTL1 0x00000000
  75. #define PCH_UART_FCR_RFTL64 0x00000040
  76. #define PCH_UART_FCR_RFTL128 0x00000080
  77. #define PCH_UART_FCR_RFTL224 0x000000C0
  78. #define PCH_UART_FCR_RFTL16 PCH_UART_FCR_RFTL64
  79. #define PCH_UART_FCR_RFTL32 PCH_UART_FCR_RFTL128
  80. #define PCH_UART_FCR_RFTL56 PCH_UART_FCR_RFTL224
  81. #define PCH_UART_FCR_RFTL4 PCH_UART_FCR_RFTL64
  82. #define PCH_UART_FCR_RFTL8 PCH_UART_FCR_RFTL128
  83. #define PCH_UART_FCR_RFTL14 PCH_UART_FCR_RFTL224
  84. #define PCH_UART_FCR_RFTL_SHIFT 6
  85. #define PCH_UART_LCR_WLS 0x00000003
  86. #define PCH_UART_LCR_STB 0x00000004
  87. #define PCH_UART_LCR_PEN 0x00000008
  88. #define PCH_UART_LCR_EPS 0x00000010
  89. #define PCH_UART_LCR_SP 0x00000020
  90. #define PCH_UART_LCR_SB 0x00000040
  91. #define PCH_UART_LCR_DLAB 0x00000080
  92. #define PCH_UART_LCR_NP 0x00000000
  93. #define PCH_UART_LCR_OP PCH_UART_LCR_PEN
  94. #define PCH_UART_LCR_EP (PCH_UART_LCR_PEN | PCH_UART_LCR_EPS)
  95. #define PCH_UART_LCR_1P (PCH_UART_LCR_PEN | PCH_UART_LCR_SP)
  96. #define PCH_UART_LCR_0P (PCH_UART_LCR_PEN | PCH_UART_LCR_EPS |\
  97. PCH_UART_LCR_SP)
  98. #define PCH_UART_LCR_5BIT 0x00000000
  99. #define PCH_UART_LCR_6BIT 0x00000001
  100. #define PCH_UART_LCR_7BIT 0x00000002
  101. #define PCH_UART_LCR_8BIT 0x00000003
  102. #define PCH_UART_MCR_DTR 0x00000001
  103. #define PCH_UART_MCR_RTS 0x00000002
  104. #define PCH_UART_MCR_OUT 0x0000000C
  105. #define PCH_UART_MCR_LOOP 0x00000010
  106. #define PCH_UART_MCR_AFE 0x00000020
  107. #define PCH_UART_LSR_DR 0x00000001
  108. #define PCH_UART_LSR_ERR (1<<7)
  109. #define PCH_UART_MSR_DCTS 0x00000001
  110. #define PCH_UART_MSR_DDSR 0x00000002
  111. #define PCH_UART_MSR_TERI 0x00000004
  112. #define PCH_UART_MSR_DDCD 0x00000008
  113. #define PCH_UART_MSR_CTS 0x00000010
  114. #define PCH_UART_MSR_DSR 0x00000020
  115. #define PCH_UART_MSR_RI 0x00000040
  116. #define PCH_UART_MSR_DCD 0x00000080
  117. #define PCH_UART_MSR_DELTA (PCH_UART_MSR_DCTS | PCH_UART_MSR_DDSR |\
  118. PCH_UART_MSR_TERI | PCH_UART_MSR_DDCD)
  119. #define PCH_UART_DLL 0x00
  120. #define PCH_UART_DLM 0x01
  121. #define DIV_ROUND(a, b) (((a) + ((b)/2)) / (b))
  122. #define PCH_UART_IID_RLS (PCH_UART_IIR_REI)
  123. #define PCH_UART_IID_RDR (PCH_UART_IIR_RRI)
  124. #define PCH_UART_IID_RDR_TO (PCH_UART_IIR_RRI | PCH_UART_IIR_TOI)
  125. #define PCH_UART_IID_THRE (PCH_UART_IIR_TRI)
  126. #define PCH_UART_IID_MS (PCH_UART_IIR_MSI)
  127. #define PCH_UART_HAL_PARITY_NONE (PCH_UART_LCR_NP)
  128. #define PCH_UART_HAL_PARITY_ODD (PCH_UART_LCR_OP)
  129. #define PCH_UART_HAL_PARITY_EVEN (PCH_UART_LCR_EP)
  130. #define PCH_UART_HAL_PARITY_FIX1 (PCH_UART_LCR_1P)
  131. #define PCH_UART_HAL_PARITY_FIX0 (PCH_UART_LCR_0P)
  132. #define PCH_UART_HAL_5BIT (PCH_UART_LCR_5BIT)
  133. #define PCH_UART_HAL_6BIT (PCH_UART_LCR_6BIT)
  134. #define PCH_UART_HAL_7BIT (PCH_UART_LCR_7BIT)
  135. #define PCH_UART_HAL_8BIT (PCH_UART_LCR_8BIT)
  136. #define PCH_UART_HAL_STB1 0
  137. #define PCH_UART_HAL_STB2 (PCH_UART_LCR_STB)
  138. #define PCH_UART_HAL_CLR_TX_FIFO (PCH_UART_FCR_TFR)
  139. #define PCH_UART_HAL_CLR_RX_FIFO (PCH_UART_FCR_RFR)
  140. #define PCH_UART_HAL_CLR_ALL_FIFO (PCH_UART_HAL_CLR_TX_FIFO | \
  141. PCH_UART_HAL_CLR_RX_FIFO)
  142. #define PCH_UART_HAL_DMA_MODE0 0
  143. #define PCH_UART_HAL_FIFO_DIS 0
  144. #define PCH_UART_HAL_FIFO16 (PCH_UART_FCR_FIFOE)
  145. #define PCH_UART_HAL_FIFO256 (PCH_UART_FCR_FIFOE | \
  146. PCH_UART_FCR_FIFO256)
  147. #define PCH_UART_HAL_FIFO64 (PCH_UART_HAL_FIFO256)
  148. #define PCH_UART_HAL_TRIGGER1 (PCH_UART_FCR_RFTL1)
  149. #define PCH_UART_HAL_TRIGGER64 (PCH_UART_FCR_RFTL64)
  150. #define PCH_UART_HAL_TRIGGER128 (PCH_UART_FCR_RFTL128)
  151. #define PCH_UART_HAL_TRIGGER224 (PCH_UART_FCR_RFTL224)
  152. #define PCH_UART_HAL_TRIGGER16 (PCH_UART_FCR_RFTL16)
  153. #define PCH_UART_HAL_TRIGGER32 (PCH_UART_FCR_RFTL32)
  154. #define PCH_UART_HAL_TRIGGER56 (PCH_UART_FCR_RFTL56)
  155. #define PCH_UART_HAL_TRIGGER4 (PCH_UART_FCR_RFTL4)
  156. #define PCH_UART_HAL_TRIGGER8 (PCH_UART_FCR_RFTL8)
  157. #define PCH_UART_HAL_TRIGGER14 (PCH_UART_FCR_RFTL14)
  158. #define PCH_UART_HAL_TRIGGER_L (PCH_UART_FCR_RFTL64)
  159. #define PCH_UART_HAL_TRIGGER_M (PCH_UART_FCR_RFTL128)
  160. #define PCH_UART_HAL_TRIGGER_H (PCH_UART_FCR_RFTL224)
  161. #define PCH_UART_HAL_RX_INT (PCH_UART_IER_ERBFI)
  162. #define PCH_UART_HAL_TX_INT (PCH_UART_IER_ETBEI)
  163. #define PCH_UART_HAL_RX_ERR_INT (PCH_UART_IER_ELSI)
  164. #define PCH_UART_HAL_MS_INT (PCH_UART_IER_EDSSI)
  165. #define PCH_UART_HAL_ALL_INT (PCH_UART_IER_MASK)
  166. #define PCH_UART_HAL_DTR (PCH_UART_MCR_DTR)
  167. #define PCH_UART_HAL_RTS (PCH_UART_MCR_RTS)
  168. #define PCH_UART_HAL_OUT (PCH_UART_MCR_OUT)
  169. #define PCH_UART_HAL_LOOP (PCH_UART_MCR_LOOP)
  170. #define PCH_UART_HAL_AFE (PCH_UART_MCR_AFE)
  171. #define PCI_VENDOR_ID_ROHM 0x10DB
  172. struct pch_uart_buffer {
  173. unsigned char *buf;
  174. int size;
  175. };
  176. struct eg20t_port {
  177. struct uart_port port;
  178. int port_type;
  179. void __iomem *membase;
  180. resource_size_t mapbase;
  181. unsigned int iobase;
  182. struct pci_dev *pdev;
  183. int fifo_size;
  184. int base_baud;
  185. int start_tx;
  186. int start_rx;
  187. int tx_empty;
  188. int int_dis_flag;
  189. int trigger;
  190. int trigger_level;
  191. struct pch_uart_buffer rxbuf;
  192. unsigned int dmsr;
  193. unsigned int fcr;
  194. unsigned int mcr;
  195. unsigned int use_dma;
  196. unsigned int use_dma_flag;
  197. struct dma_async_tx_descriptor *desc_tx;
  198. struct dma_async_tx_descriptor *desc_rx;
  199. struct pch_dma_slave param_tx;
  200. struct pch_dma_slave param_rx;
  201. struct dma_chan *chan_tx;
  202. struct dma_chan *chan_rx;
  203. struct scatterlist *sg_tx_p;
  204. int nent;
  205. struct scatterlist sg_rx;
  206. int tx_dma_use;
  207. void *rx_buf_virt;
  208. dma_addr_t rx_buf_dma;
  209. };
  210. /**
  211. * struct pch_uart_driver_data - private data structure for UART-DMA
  212. * @port_type: The number of DMA channel
  213. * @line_no: UART port line number (0, 1, 2...)
  214. */
  215. struct pch_uart_driver_data {
  216. int port_type;
  217. int line_no;
  218. };
  219. enum pch_uart_num_t {
  220. pch_et20t_uart0 = 0,
  221. pch_et20t_uart1,
  222. pch_et20t_uart2,
  223. pch_et20t_uart3,
  224. pch_ml7213_uart0,
  225. pch_ml7213_uart1,
  226. pch_ml7213_uart2,
  227. pch_ml7223_uart0,
  228. pch_ml7223_uart1,
  229. };
  230. static struct pch_uart_driver_data drv_dat[] = {
  231. [pch_et20t_uart0] = {PCH_UART_8LINE, 0},
  232. [pch_et20t_uart1] = {PCH_UART_2LINE, 1},
  233. [pch_et20t_uart2] = {PCH_UART_2LINE, 2},
  234. [pch_et20t_uart3] = {PCH_UART_2LINE, 3},
  235. [pch_ml7213_uart0] = {PCH_UART_8LINE, 0},
  236. [pch_ml7213_uart1] = {PCH_UART_2LINE, 1},
  237. [pch_ml7213_uart2] = {PCH_UART_2LINE, 2},
  238. [pch_ml7223_uart0] = {PCH_UART_8LINE, 0},
  239. [pch_ml7223_uart1] = {PCH_UART_2LINE, 1},
  240. };
  241. static unsigned int default_baud = 9600;
  242. static const int trigger_level_256[4] = { 1, 64, 128, 224 };
  243. static const int trigger_level_64[4] = { 1, 16, 32, 56 };
  244. static const int trigger_level_16[4] = { 1, 4, 8, 14 };
  245. static const int trigger_level_1[4] = { 1, 1, 1, 1 };
  246. static void pch_uart_hal_request(struct pci_dev *pdev, int fifosize,
  247. int base_baud)
  248. {
  249. struct eg20t_port *priv = pci_get_drvdata(pdev);
  250. priv->trigger_level = 1;
  251. priv->fcr = 0;
  252. }
  253. static unsigned int get_msr(struct eg20t_port *priv, void __iomem *base)
  254. {
  255. unsigned int msr = ioread8(base + UART_MSR);
  256. priv->dmsr |= msr & PCH_UART_MSR_DELTA;
  257. return msr;
  258. }
  259. static void pch_uart_hal_enable_interrupt(struct eg20t_port *priv,
  260. unsigned int flag)
  261. {
  262. u8 ier = ioread8(priv->membase + UART_IER);
  263. ier |= flag & PCH_UART_IER_MASK;
  264. iowrite8(ier, priv->membase + UART_IER);
  265. }
  266. static void pch_uart_hal_disable_interrupt(struct eg20t_port *priv,
  267. unsigned int flag)
  268. {
  269. u8 ier = ioread8(priv->membase + UART_IER);
  270. ier &= ~(flag & PCH_UART_IER_MASK);
  271. iowrite8(ier, priv->membase + UART_IER);
  272. }
  273. static int pch_uart_hal_set_line(struct eg20t_port *priv, int baud,
  274. unsigned int parity, unsigned int bits,
  275. unsigned int stb)
  276. {
  277. unsigned int dll, dlm, lcr;
  278. int div;
  279. div = DIV_ROUND(priv->base_baud / 16, baud);
  280. if (div < 0 || USHRT_MAX <= div) {
  281. dev_err(priv->port.dev, "Invalid Baud(div=0x%x)\n", div);
  282. return -EINVAL;
  283. }
  284. dll = (unsigned int)div & 0x00FFU;
  285. dlm = ((unsigned int)div >> 8) & 0x00FFU;
  286. if (parity & ~(PCH_UART_LCR_PEN | PCH_UART_LCR_EPS | PCH_UART_LCR_SP)) {
  287. dev_err(priv->port.dev, "Invalid parity(0x%x)\n", parity);
  288. return -EINVAL;
  289. }
  290. if (bits & ~PCH_UART_LCR_WLS) {
  291. dev_err(priv->port.dev, "Invalid bits(0x%x)\n", bits);
  292. return -EINVAL;
  293. }
  294. if (stb & ~PCH_UART_LCR_STB) {
  295. dev_err(priv->port.dev, "Invalid STB(0x%x)\n", stb);
  296. return -EINVAL;
  297. }
  298. lcr = parity;
  299. lcr |= bits;
  300. lcr |= stb;
  301. dev_dbg(priv->port.dev, "%s:baud = %d, div = %04x, lcr = %02x (%lu)\n",
  302. __func__, baud, div, lcr, jiffies);
  303. iowrite8(PCH_UART_LCR_DLAB, priv->membase + UART_LCR);
  304. iowrite8(dll, priv->membase + PCH_UART_DLL);
  305. iowrite8(dlm, priv->membase + PCH_UART_DLM);
  306. iowrite8(lcr, priv->membase + UART_LCR);
  307. return 0;
  308. }
  309. static int pch_uart_hal_fifo_reset(struct eg20t_port *priv,
  310. unsigned int flag)
  311. {
  312. if (flag & ~(PCH_UART_FCR_TFR | PCH_UART_FCR_RFR)) {
  313. dev_err(priv->port.dev, "%s:Invalid flag(0x%x)\n",
  314. __func__, flag);
  315. return -EINVAL;
  316. }
  317. iowrite8(PCH_UART_FCR_FIFOE | priv->fcr, priv->membase + UART_FCR);
  318. iowrite8(PCH_UART_FCR_FIFOE | priv->fcr | flag,
  319. priv->membase + UART_FCR);
  320. iowrite8(priv->fcr, priv->membase + UART_FCR);
  321. return 0;
  322. }
  323. static int pch_uart_hal_set_fifo(struct eg20t_port *priv,
  324. unsigned int dmamode,
  325. unsigned int fifo_size, unsigned int trigger)
  326. {
  327. u8 fcr;
  328. if (dmamode & ~PCH_UART_FCR_DMS) {
  329. dev_err(priv->port.dev, "%s:Invalid DMA Mode(0x%x)\n",
  330. __func__, dmamode);
  331. return -EINVAL;
  332. }
  333. if (fifo_size & ~(PCH_UART_FCR_FIFOE | PCH_UART_FCR_FIFO256)) {
  334. dev_err(priv->port.dev, "%s:Invalid FIFO SIZE(0x%x)\n",
  335. __func__, fifo_size);
  336. return -EINVAL;
  337. }
  338. if (trigger & ~PCH_UART_FCR_RFTL) {
  339. dev_err(priv->port.dev, "%s:Invalid TRIGGER(0x%x)\n",
  340. __func__, trigger);
  341. return -EINVAL;
  342. }
  343. switch (priv->fifo_size) {
  344. case 256:
  345. priv->trigger_level =
  346. trigger_level_256[trigger >> PCH_UART_FCR_RFTL_SHIFT];
  347. break;
  348. case 64:
  349. priv->trigger_level =
  350. trigger_level_64[trigger >> PCH_UART_FCR_RFTL_SHIFT];
  351. break;
  352. case 16:
  353. priv->trigger_level =
  354. trigger_level_16[trigger >> PCH_UART_FCR_RFTL_SHIFT];
  355. break;
  356. default:
  357. priv->trigger_level =
  358. trigger_level_1[trigger >> PCH_UART_FCR_RFTL_SHIFT];
  359. break;
  360. }
  361. fcr =
  362. dmamode | fifo_size | trigger | PCH_UART_FCR_RFR | PCH_UART_FCR_TFR;
  363. iowrite8(PCH_UART_FCR_FIFOE, priv->membase + UART_FCR);
  364. iowrite8(PCH_UART_FCR_FIFOE | PCH_UART_FCR_RFR | PCH_UART_FCR_TFR,
  365. priv->membase + UART_FCR);
  366. iowrite8(fcr, priv->membase + UART_FCR);
  367. priv->fcr = fcr;
  368. return 0;
  369. }
  370. static u8 pch_uart_hal_get_modem(struct eg20t_port *priv)
  371. {
  372. priv->dmsr = 0;
  373. return get_msr(priv, priv->membase);
  374. }
  375. static void pch_uart_hal_write(struct eg20t_port *priv,
  376. const unsigned char *buf, int tx_size)
  377. {
  378. int i;
  379. unsigned int thr;
  380. for (i = 0; i < tx_size;) {
  381. thr = buf[i++];
  382. iowrite8(thr, priv->membase + PCH_UART_THR);
  383. }
  384. }
  385. static int pch_uart_hal_read(struct eg20t_port *priv, unsigned char *buf,
  386. int rx_size)
  387. {
  388. int i;
  389. u8 rbr, lsr;
  390. lsr = ioread8(priv->membase + UART_LSR);
  391. for (i = 0, lsr = ioread8(priv->membase + UART_LSR);
  392. i < rx_size && lsr & UART_LSR_DR;
  393. lsr = ioread8(priv->membase + UART_LSR)) {
  394. rbr = ioread8(priv->membase + PCH_UART_RBR);
  395. buf[i++] = rbr;
  396. }
  397. return i;
  398. }
  399. static unsigned int pch_uart_hal_get_iid(struct eg20t_port *priv)
  400. {
  401. unsigned int iir;
  402. int ret;
  403. iir = ioread8(priv->membase + UART_IIR);
  404. ret = (iir & (PCH_UART_IIR_IID | PCH_UART_IIR_TOI | PCH_UART_IIR_IP));
  405. return ret;
  406. }
  407. static u8 pch_uart_hal_get_line_status(struct eg20t_port *priv)
  408. {
  409. return ioread8(priv->membase + UART_LSR);
  410. }
  411. static void pch_uart_hal_set_break(struct eg20t_port *priv, int on)
  412. {
  413. unsigned int lcr;
  414. lcr = ioread8(priv->membase + UART_LCR);
  415. if (on)
  416. lcr |= PCH_UART_LCR_SB;
  417. else
  418. lcr &= ~PCH_UART_LCR_SB;
  419. iowrite8(lcr, priv->membase + UART_LCR);
  420. }
  421. static int push_rx(struct eg20t_port *priv, const unsigned char *buf,
  422. int size)
  423. {
  424. struct uart_port *port;
  425. struct tty_struct *tty;
  426. port = &priv->port;
  427. tty = tty_port_tty_get(&port->state->port);
  428. if (!tty) {
  429. dev_dbg(priv->port.dev, "%s:tty is busy now", __func__);
  430. return -EBUSY;
  431. }
  432. tty_insert_flip_string(tty, buf, size);
  433. tty_flip_buffer_push(tty);
  434. tty_kref_put(tty);
  435. return 0;
  436. }
  437. static int pop_tx_x(struct eg20t_port *priv, unsigned char *buf)
  438. {
  439. int ret;
  440. struct uart_port *port = &priv->port;
  441. if (port->x_char) {
  442. dev_dbg(priv->port.dev, "%s:X character send %02x (%lu)\n",
  443. __func__, port->x_char, jiffies);
  444. buf[0] = port->x_char;
  445. port->x_char = 0;
  446. ret = 1;
  447. } else {
  448. ret = 0;
  449. }
  450. return ret;
  451. }
  452. static int dma_push_rx(struct eg20t_port *priv, int size)
  453. {
  454. struct tty_struct *tty;
  455. int room;
  456. struct uart_port *port = &priv->port;
  457. port = &priv->port;
  458. tty = tty_port_tty_get(&port->state->port);
  459. if (!tty) {
  460. dev_dbg(priv->port.dev, "%s:tty is busy now", __func__);
  461. return 0;
  462. }
  463. room = tty_buffer_request_room(tty, size);
  464. if (room < size)
  465. dev_warn(port->dev, "Rx overrun: dropping %u bytes\n",
  466. size - room);
  467. if (!room)
  468. return room;
  469. tty_insert_flip_string(tty, sg_virt(&priv->sg_rx), size);
  470. port->icount.rx += room;
  471. tty_kref_put(tty);
  472. return room;
  473. }
  474. static void pch_free_dma(struct uart_port *port)
  475. {
  476. struct eg20t_port *priv;
  477. priv = container_of(port, struct eg20t_port, port);
  478. if (priv->chan_tx) {
  479. dma_release_channel(priv->chan_tx);
  480. priv->chan_tx = NULL;
  481. }
  482. if (priv->chan_rx) {
  483. dma_release_channel(priv->chan_rx);
  484. priv->chan_rx = NULL;
  485. }
  486. if (sg_dma_address(&priv->sg_rx))
  487. dma_free_coherent(port->dev, port->fifosize,
  488. sg_virt(&priv->sg_rx),
  489. sg_dma_address(&priv->sg_rx));
  490. return;
  491. }
  492. static bool filter(struct dma_chan *chan, void *slave)
  493. {
  494. struct pch_dma_slave *param = slave;
  495. if ((chan->chan_id == param->chan_id) && (param->dma_dev ==
  496. chan->device->dev)) {
  497. chan->private = param;
  498. return true;
  499. } else {
  500. return false;
  501. }
  502. }
  503. static void pch_request_dma(struct uart_port *port)
  504. {
  505. dma_cap_mask_t mask;
  506. struct dma_chan *chan;
  507. struct pci_dev *dma_dev;
  508. struct pch_dma_slave *param;
  509. struct eg20t_port *priv =
  510. container_of(port, struct eg20t_port, port);
  511. dma_cap_zero(mask);
  512. dma_cap_set(DMA_SLAVE, mask);
  513. dma_dev = pci_get_bus_and_slot(2, PCI_DEVFN(0xa, 0)); /* Get DMA's dev
  514. information */
  515. /* Set Tx DMA */
  516. param = &priv->param_tx;
  517. param->dma_dev = &dma_dev->dev;
  518. param->chan_id = priv->port.line * 2; /* Tx = 0, 2, 4, ... */
  519. param->tx_reg = port->mapbase + UART_TX;
  520. chan = dma_request_channel(mask, filter, param);
  521. if (!chan) {
  522. dev_err(priv->port.dev, "%s:dma_request_channel FAILS(Tx)\n",
  523. __func__);
  524. return;
  525. }
  526. priv->chan_tx = chan;
  527. /* Set Rx DMA */
  528. param = &priv->param_rx;
  529. param->dma_dev = &dma_dev->dev;
  530. param->chan_id = priv->port.line * 2 + 1; /* Rx = Tx + 1 */
  531. param->rx_reg = port->mapbase + UART_RX;
  532. chan = dma_request_channel(mask, filter, param);
  533. if (!chan) {
  534. dev_err(priv->port.dev, "%s:dma_request_channel FAILS(Rx)\n",
  535. __func__);
  536. dma_release_channel(priv->chan_tx);
  537. return;
  538. }
  539. /* Get Consistent memory for DMA */
  540. priv->rx_buf_virt = dma_alloc_coherent(port->dev, port->fifosize,
  541. &priv->rx_buf_dma, GFP_KERNEL);
  542. priv->chan_rx = chan;
  543. }
  544. static void pch_dma_rx_complete(void *arg)
  545. {
  546. struct eg20t_port *priv = arg;
  547. struct uart_port *port = &priv->port;
  548. struct tty_struct *tty = tty_port_tty_get(&port->state->port);
  549. int count;
  550. if (!tty) {
  551. dev_dbg(priv->port.dev, "%s:tty is busy now", __func__);
  552. return;
  553. }
  554. dma_sync_sg_for_cpu(port->dev, &priv->sg_rx, 1, DMA_FROM_DEVICE);
  555. count = dma_push_rx(priv, priv->trigger_level);
  556. if (count)
  557. tty_flip_buffer_push(tty);
  558. tty_kref_put(tty);
  559. async_tx_ack(priv->desc_rx);
  560. pch_uart_hal_enable_interrupt(priv, PCH_UART_HAL_RX_INT);
  561. }
  562. static void pch_dma_tx_complete(void *arg)
  563. {
  564. struct eg20t_port *priv = arg;
  565. struct uart_port *port = &priv->port;
  566. struct circ_buf *xmit = &port->state->xmit;
  567. struct scatterlist *sg = priv->sg_tx_p;
  568. int i;
  569. for (i = 0; i < priv->nent; i++, sg++) {
  570. xmit->tail += sg_dma_len(sg);
  571. port->icount.tx += sg_dma_len(sg);
  572. }
  573. xmit->tail &= UART_XMIT_SIZE - 1;
  574. async_tx_ack(priv->desc_tx);
  575. dma_unmap_sg(port->dev, sg, priv->nent, DMA_TO_DEVICE);
  576. priv->tx_dma_use = 0;
  577. priv->nent = 0;
  578. kfree(priv->sg_tx_p);
  579. pch_uart_hal_enable_interrupt(priv, PCH_UART_HAL_TX_INT);
  580. }
  581. static int pop_tx(struct eg20t_port *priv, int size)
  582. {
  583. int count = 0;
  584. struct uart_port *port = &priv->port;
  585. struct circ_buf *xmit = &port->state->xmit;
  586. if (uart_tx_stopped(port) || uart_circ_empty(xmit) || count >= size)
  587. goto pop_tx_end;
  588. do {
  589. int cnt_to_end =
  590. CIRC_CNT_TO_END(xmit->head, xmit->tail, UART_XMIT_SIZE);
  591. int sz = min(size - count, cnt_to_end);
  592. pch_uart_hal_write(priv, &xmit->buf[xmit->tail], sz);
  593. xmit->tail = (xmit->tail + sz) & (UART_XMIT_SIZE - 1);
  594. count += sz;
  595. } while (!uart_circ_empty(xmit) && count < size);
  596. pop_tx_end:
  597. dev_dbg(priv->port.dev, "%d characters. Remained %d characters.(%lu)\n",
  598. count, size - count, jiffies);
  599. return count;
  600. }
  601. static int handle_rx_to(struct eg20t_port *priv)
  602. {
  603. struct pch_uart_buffer *buf;
  604. int rx_size;
  605. int ret;
  606. if (!priv->start_rx) {
  607. pch_uart_hal_disable_interrupt(priv, PCH_UART_HAL_RX_INT);
  608. return 0;
  609. }
  610. buf = &priv->rxbuf;
  611. do {
  612. rx_size = pch_uart_hal_read(priv, buf->buf, buf->size);
  613. ret = push_rx(priv, buf->buf, rx_size);
  614. if (ret)
  615. return 0;
  616. } while (rx_size == buf->size);
  617. return PCH_UART_HANDLED_RX_INT;
  618. }
  619. static int handle_rx(struct eg20t_port *priv)
  620. {
  621. return handle_rx_to(priv);
  622. }
  623. static int dma_handle_rx(struct eg20t_port *priv)
  624. {
  625. struct uart_port *port = &priv->port;
  626. struct dma_async_tx_descriptor *desc;
  627. struct scatterlist *sg;
  628. priv = container_of(port, struct eg20t_port, port);
  629. sg = &priv->sg_rx;
  630. sg_init_table(&priv->sg_rx, 1); /* Initialize SG table */
  631. sg_dma_len(sg) = priv->trigger_level;
  632. sg_set_page(&priv->sg_rx, virt_to_page(priv->rx_buf_virt),
  633. sg_dma_len(sg), (unsigned long)priv->rx_buf_virt &
  634. ~PAGE_MASK);
  635. sg_dma_address(sg) = priv->rx_buf_dma;
  636. desc = priv->chan_rx->device->device_prep_slave_sg(priv->chan_rx,
  637. sg, 1, DMA_FROM_DEVICE,
  638. DMA_PREP_INTERRUPT | DMA_CTRL_ACK);
  639. if (!desc)
  640. return 0;
  641. priv->desc_rx = desc;
  642. desc->callback = pch_dma_rx_complete;
  643. desc->callback_param = priv;
  644. desc->tx_submit(desc);
  645. dma_async_issue_pending(priv->chan_rx);
  646. return PCH_UART_HANDLED_RX_INT;
  647. }
  648. static unsigned int handle_tx(struct eg20t_port *priv)
  649. {
  650. struct uart_port *port = &priv->port;
  651. struct circ_buf *xmit = &port->state->xmit;
  652. int fifo_size;
  653. int tx_size;
  654. int size;
  655. int tx_empty;
  656. if (!priv->start_tx) {
  657. dev_info(priv->port.dev, "%s:Tx isn't started. (%lu)\n",
  658. __func__, jiffies);
  659. pch_uart_hal_disable_interrupt(priv, PCH_UART_HAL_TX_INT);
  660. priv->tx_empty = 1;
  661. return 0;
  662. }
  663. fifo_size = max(priv->fifo_size, 1);
  664. tx_empty = 1;
  665. if (pop_tx_x(priv, xmit->buf)) {
  666. pch_uart_hal_write(priv, xmit->buf, 1);
  667. port->icount.tx++;
  668. tx_empty = 0;
  669. fifo_size--;
  670. }
  671. size = min(xmit->head - xmit->tail, fifo_size);
  672. if (size < 0)
  673. size = fifo_size;
  674. tx_size = pop_tx(priv, size);
  675. if (tx_size > 0) {
  676. port->icount.tx += tx_size;
  677. tx_empty = 0;
  678. }
  679. priv->tx_empty = tx_empty;
  680. if (tx_empty) {
  681. pch_uart_hal_disable_interrupt(priv, PCH_UART_HAL_TX_INT);
  682. uart_write_wakeup(port);
  683. }
  684. return PCH_UART_HANDLED_TX_INT;
  685. }
  686. static unsigned int dma_handle_tx(struct eg20t_port *priv)
  687. {
  688. struct uart_port *port = &priv->port;
  689. struct circ_buf *xmit = &port->state->xmit;
  690. struct scatterlist *sg;
  691. int nent;
  692. int fifo_size;
  693. int tx_empty;
  694. struct dma_async_tx_descriptor *desc;
  695. int num;
  696. int i;
  697. int bytes;
  698. int size;
  699. int rem;
  700. if (!priv->start_tx) {
  701. dev_info(priv->port.dev, "%s:Tx isn't started. (%lu)\n",
  702. __func__, jiffies);
  703. pch_uart_hal_disable_interrupt(priv, PCH_UART_HAL_TX_INT);
  704. priv->tx_empty = 1;
  705. return 0;
  706. }
  707. if (priv->tx_dma_use) {
  708. dev_dbg(priv->port.dev, "%s:Tx is not completed. (%lu)\n",
  709. __func__, jiffies);
  710. pch_uart_hal_disable_interrupt(priv, PCH_UART_HAL_TX_INT);
  711. priv->tx_empty = 1;
  712. return 0;
  713. }
  714. fifo_size = max(priv->fifo_size, 1);
  715. tx_empty = 1;
  716. if (pop_tx_x(priv, xmit->buf)) {
  717. pch_uart_hal_write(priv, xmit->buf, 1);
  718. port->icount.tx++;
  719. tx_empty = 0;
  720. fifo_size--;
  721. }
  722. bytes = min((int)CIRC_CNT(xmit->head, xmit->tail,
  723. UART_XMIT_SIZE), CIRC_CNT_TO_END(xmit->head,
  724. xmit->tail, UART_XMIT_SIZE));
  725. if (!bytes) {
  726. dev_dbg(priv->port.dev, "%s 0 bytes return\n", __func__);
  727. pch_uart_hal_disable_interrupt(priv, PCH_UART_HAL_TX_INT);
  728. uart_write_wakeup(port);
  729. return 0;
  730. }
  731. if (bytes > fifo_size) {
  732. num = bytes / fifo_size + 1;
  733. size = fifo_size;
  734. rem = bytes % fifo_size;
  735. } else {
  736. num = 1;
  737. size = bytes;
  738. rem = bytes;
  739. }
  740. dev_dbg(priv->port.dev, "%s num=%d size=%d rem=%d\n",
  741. __func__, num, size, rem);
  742. priv->tx_dma_use = 1;
  743. priv->sg_tx_p = kzalloc(sizeof(struct scatterlist)*num, GFP_ATOMIC);
  744. sg_init_table(priv->sg_tx_p, num); /* Initialize SG table */
  745. sg = priv->sg_tx_p;
  746. for (i = 0; i < num; i++, sg++) {
  747. if (i == (num - 1))
  748. sg_set_page(sg, virt_to_page(xmit->buf),
  749. rem, fifo_size * i);
  750. else
  751. sg_set_page(sg, virt_to_page(xmit->buf),
  752. size, fifo_size * i);
  753. }
  754. sg = priv->sg_tx_p;
  755. nent = dma_map_sg(port->dev, sg, num, DMA_TO_DEVICE);
  756. if (!nent) {
  757. dev_err(priv->port.dev, "%s:dma_map_sg Failed\n", __func__);
  758. return 0;
  759. }
  760. priv->nent = nent;
  761. for (i = 0; i < nent; i++, sg++) {
  762. sg->offset = (xmit->tail & (UART_XMIT_SIZE - 1)) +
  763. fifo_size * i;
  764. sg_dma_address(sg) = (sg_dma_address(sg) &
  765. ~(UART_XMIT_SIZE - 1)) + sg->offset;
  766. if (i == (nent - 1))
  767. sg_dma_len(sg) = rem;
  768. else
  769. sg_dma_len(sg) = size;
  770. }
  771. desc = priv->chan_tx->device->device_prep_slave_sg(priv->chan_tx,
  772. priv->sg_tx_p, nent, DMA_TO_DEVICE,
  773. DMA_PREP_INTERRUPT | DMA_CTRL_ACK);
  774. if (!desc) {
  775. dev_err(priv->port.dev, "%s:device_prep_slave_sg Failed\n",
  776. __func__);
  777. return 0;
  778. }
  779. dma_sync_sg_for_device(port->dev, priv->sg_tx_p, nent, DMA_TO_DEVICE);
  780. priv->desc_tx = desc;
  781. desc->callback = pch_dma_tx_complete;
  782. desc->callback_param = priv;
  783. desc->tx_submit(desc);
  784. dma_async_issue_pending(priv->chan_tx);
  785. return PCH_UART_HANDLED_TX_INT;
  786. }
  787. static void pch_uart_err_ir(struct eg20t_port *priv, unsigned int lsr)
  788. {
  789. u8 fcr = ioread8(priv->membase + UART_FCR);
  790. /* Reset FIFO */
  791. fcr |= UART_FCR_CLEAR_RCVR;
  792. iowrite8(fcr, priv->membase + UART_FCR);
  793. if (lsr & PCH_UART_LSR_ERR)
  794. dev_err(&priv->pdev->dev, "Error data in FIFO\n");
  795. if (lsr & UART_LSR_FE)
  796. dev_err(&priv->pdev->dev, "Framing Error\n");
  797. if (lsr & UART_LSR_PE)
  798. dev_err(&priv->pdev->dev, "Parity Error\n");
  799. if (lsr & UART_LSR_OE)
  800. dev_err(&priv->pdev->dev, "Overrun Error\n");
  801. }
  802. static irqreturn_t pch_uart_interrupt(int irq, void *dev_id)
  803. {
  804. struct eg20t_port *priv = dev_id;
  805. unsigned int handled;
  806. u8 lsr;
  807. int ret = 0;
  808. unsigned int iid;
  809. unsigned long flags;
  810. spin_lock_irqsave(&priv->port.lock, flags);
  811. handled = 0;
  812. while ((iid = pch_uart_hal_get_iid(priv)) > 1) {
  813. switch (iid) {
  814. case PCH_UART_IID_RLS: /* Receiver Line Status */
  815. lsr = pch_uart_hal_get_line_status(priv);
  816. if (lsr & (PCH_UART_LSR_ERR | UART_LSR_FE |
  817. UART_LSR_PE | UART_LSR_OE)) {
  818. pch_uart_err_ir(priv, lsr);
  819. ret = PCH_UART_HANDLED_RX_ERR_INT;
  820. }
  821. break;
  822. case PCH_UART_IID_RDR: /* Received Data Ready */
  823. if (priv->use_dma) {
  824. pch_uart_hal_disable_interrupt(priv,
  825. PCH_UART_HAL_RX_INT);
  826. ret = dma_handle_rx(priv);
  827. if (!ret)
  828. pch_uart_hal_enable_interrupt(priv,
  829. PCH_UART_HAL_RX_INT);
  830. } else {
  831. ret = handle_rx(priv);
  832. }
  833. break;
  834. case PCH_UART_IID_RDR_TO: /* Received Data Ready
  835. (FIFO Timeout) */
  836. ret = handle_rx_to(priv);
  837. break;
  838. case PCH_UART_IID_THRE: /* Transmitter Holding Register
  839. Empty */
  840. if (priv->use_dma)
  841. ret = dma_handle_tx(priv);
  842. else
  843. ret = handle_tx(priv);
  844. break;
  845. case PCH_UART_IID_MS: /* Modem Status */
  846. ret = PCH_UART_HANDLED_MS_INT;
  847. break;
  848. default: /* Never junp to this label */
  849. dev_err(priv->port.dev, "%s:iid=%d (%lu)\n", __func__,
  850. iid, jiffies);
  851. ret = -1;
  852. break;
  853. }
  854. handled |= (unsigned int)ret;
  855. }
  856. if (handled == 0 && iid <= 1) {
  857. if (priv->int_dis_flag)
  858. priv->int_dis_flag = 0;
  859. }
  860. spin_unlock_irqrestore(&priv->port.lock, flags);
  861. return IRQ_RETVAL(handled);
  862. }
  863. /* This function tests whether the transmitter fifo and shifter for the port
  864. described by 'port' is empty. */
  865. static unsigned int pch_uart_tx_empty(struct uart_port *port)
  866. {
  867. struct eg20t_port *priv;
  868. int ret;
  869. priv = container_of(port, struct eg20t_port, port);
  870. if (priv->tx_empty)
  871. ret = TIOCSER_TEMT;
  872. else
  873. ret = 0;
  874. return ret;
  875. }
  876. /* Returns the current state of modem control inputs. */
  877. static unsigned int pch_uart_get_mctrl(struct uart_port *port)
  878. {
  879. struct eg20t_port *priv;
  880. u8 modem;
  881. unsigned int ret = 0;
  882. priv = container_of(port, struct eg20t_port, port);
  883. modem = pch_uart_hal_get_modem(priv);
  884. if (modem & UART_MSR_DCD)
  885. ret |= TIOCM_CAR;
  886. if (modem & UART_MSR_RI)
  887. ret |= TIOCM_RNG;
  888. if (modem & UART_MSR_DSR)
  889. ret |= TIOCM_DSR;
  890. if (modem & UART_MSR_CTS)
  891. ret |= TIOCM_CTS;
  892. return ret;
  893. }
  894. static void pch_uart_set_mctrl(struct uart_port *port, unsigned int mctrl)
  895. {
  896. u32 mcr = 0;
  897. struct eg20t_port *priv = container_of(port, struct eg20t_port, port);
  898. if (mctrl & TIOCM_DTR)
  899. mcr |= UART_MCR_DTR;
  900. if (mctrl & TIOCM_RTS)
  901. mcr |= UART_MCR_RTS;
  902. if (mctrl & TIOCM_LOOP)
  903. mcr |= UART_MCR_LOOP;
  904. if (priv->mcr & UART_MCR_AFE)
  905. mcr |= UART_MCR_AFE;
  906. if (mctrl)
  907. iowrite8(mcr, priv->membase + UART_MCR);
  908. }
  909. static void pch_uart_stop_tx(struct uart_port *port)
  910. {
  911. struct eg20t_port *priv;
  912. priv = container_of(port, struct eg20t_port, port);
  913. priv->start_tx = 0;
  914. priv->tx_dma_use = 0;
  915. }
  916. static void pch_uart_start_tx(struct uart_port *port)
  917. {
  918. struct eg20t_port *priv;
  919. priv = container_of(port, struct eg20t_port, port);
  920. if (priv->use_dma) {
  921. if (priv->tx_dma_use) {
  922. dev_dbg(priv->port.dev, "%s : Tx DMA is NOT empty.\n",
  923. __func__);
  924. return;
  925. }
  926. }
  927. priv->start_tx = 1;
  928. pch_uart_hal_enable_interrupt(priv, PCH_UART_HAL_TX_INT);
  929. }
  930. static void pch_uart_stop_rx(struct uart_port *port)
  931. {
  932. struct eg20t_port *priv;
  933. priv = container_of(port, struct eg20t_port, port);
  934. priv->start_rx = 0;
  935. pch_uart_hal_disable_interrupt(priv, PCH_UART_HAL_RX_INT);
  936. priv->int_dis_flag = 1;
  937. }
  938. /* Enable the modem status interrupts. */
  939. static void pch_uart_enable_ms(struct uart_port *port)
  940. {
  941. struct eg20t_port *priv;
  942. priv = container_of(port, struct eg20t_port, port);
  943. pch_uart_hal_enable_interrupt(priv, PCH_UART_HAL_MS_INT);
  944. }
  945. /* Control the transmission of a break signal. */
  946. static void pch_uart_break_ctl(struct uart_port *port, int ctl)
  947. {
  948. struct eg20t_port *priv;
  949. unsigned long flags;
  950. priv = container_of(port, struct eg20t_port, port);
  951. spin_lock_irqsave(&port->lock, flags);
  952. pch_uart_hal_set_break(priv, ctl);
  953. spin_unlock_irqrestore(&port->lock, flags);
  954. }
  955. /* Grab any interrupt resources and initialise any low level driver state. */
  956. static int pch_uart_startup(struct uart_port *port)
  957. {
  958. struct eg20t_port *priv;
  959. int ret;
  960. int fifo_size;
  961. int trigger_level;
  962. priv = container_of(port, struct eg20t_port, port);
  963. priv->tx_empty = 1;
  964. if (port->uartclk)
  965. priv->base_baud = port->uartclk;
  966. else
  967. port->uartclk = priv->base_baud;
  968. pch_uart_hal_disable_interrupt(priv, PCH_UART_HAL_ALL_INT);
  969. ret = pch_uart_hal_set_line(priv, default_baud,
  970. PCH_UART_HAL_PARITY_NONE, PCH_UART_HAL_8BIT,
  971. PCH_UART_HAL_STB1);
  972. if (ret)
  973. return ret;
  974. switch (priv->fifo_size) {
  975. case 256:
  976. fifo_size = PCH_UART_HAL_FIFO256;
  977. break;
  978. case 64:
  979. fifo_size = PCH_UART_HAL_FIFO64;
  980. break;
  981. case 16:
  982. fifo_size = PCH_UART_HAL_FIFO16;
  983. case 1:
  984. default:
  985. fifo_size = PCH_UART_HAL_FIFO_DIS;
  986. break;
  987. }
  988. switch (priv->trigger) {
  989. case PCH_UART_HAL_TRIGGER1:
  990. trigger_level = 1;
  991. break;
  992. case PCH_UART_HAL_TRIGGER_L:
  993. trigger_level = priv->fifo_size / 4;
  994. break;
  995. case PCH_UART_HAL_TRIGGER_M:
  996. trigger_level = priv->fifo_size / 2;
  997. break;
  998. case PCH_UART_HAL_TRIGGER_H:
  999. default:
  1000. trigger_level = priv->fifo_size - (priv->fifo_size / 8);
  1001. break;
  1002. }
  1003. priv->trigger_level = trigger_level;
  1004. ret = pch_uart_hal_set_fifo(priv, PCH_UART_HAL_DMA_MODE0,
  1005. fifo_size, priv->trigger);
  1006. if (ret < 0)
  1007. return ret;
  1008. ret = request_irq(priv->port.irq, pch_uart_interrupt, IRQF_SHARED,
  1009. KBUILD_MODNAME, priv);
  1010. if (ret < 0)
  1011. return ret;
  1012. if (priv->use_dma)
  1013. pch_request_dma(port);
  1014. priv->start_rx = 1;
  1015. pch_uart_hal_enable_interrupt(priv, PCH_UART_HAL_RX_INT);
  1016. uart_update_timeout(port, CS8, default_baud);
  1017. return 0;
  1018. }
  1019. static void pch_uart_shutdown(struct uart_port *port)
  1020. {
  1021. struct eg20t_port *priv;
  1022. int ret;
  1023. priv = container_of(port, struct eg20t_port, port);
  1024. pch_uart_hal_disable_interrupt(priv, PCH_UART_HAL_ALL_INT);
  1025. pch_uart_hal_fifo_reset(priv, PCH_UART_HAL_CLR_ALL_FIFO);
  1026. ret = pch_uart_hal_set_fifo(priv, PCH_UART_HAL_DMA_MODE0,
  1027. PCH_UART_HAL_FIFO_DIS, PCH_UART_HAL_TRIGGER1);
  1028. if (ret)
  1029. dev_err(priv->port.dev,
  1030. "pch_uart_hal_set_fifo Failed(ret=%d)\n", ret);
  1031. if (priv->use_dma_flag)
  1032. pch_free_dma(port);
  1033. free_irq(priv->port.irq, priv);
  1034. }
  1035. /* Change the port parameters, including word length, parity, stop
  1036. *bits. Update read_status_mask and ignore_status_mask to indicate
  1037. *the types of events we are interested in receiving. */
  1038. static void pch_uart_set_termios(struct uart_port *port,
  1039. struct ktermios *termios, struct ktermios *old)
  1040. {
  1041. int baud;
  1042. int rtn;
  1043. unsigned int parity, bits, stb;
  1044. struct eg20t_port *priv;
  1045. unsigned long flags;
  1046. priv = container_of(port, struct eg20t_port, port);
  1047. switch (termios->c_cflag & CSIZE) {
  1048. case CS5:
  1049. bits = PCH_UART_HAL_5BIT;
  1050. break;
  1051. case CS6:
  1052. bits = PCH_UART_HAL_6BIT;
  1053. break;
  1054. case CS7:
  1055. bits = PCH_UART_HAL_7BIT;
  1056. break;
  1057. default: /* CS8 */
  1058. bits = PCH_UART_HAL_8BIT;
  1059. break;
  1060. }
  1061. if (termios->c_cflag & CSTOPB)
  1062. stb = PCH_UART_HAL_STB2;
  1063. else
  1064. stb = PCH_UART_HAL_STB1;
  1065. if (termios->c_cflag & PARENB) {
  1066. if (!(termios->c_cflag & PARODD))
  1067. parity = PCH_UART_HAL_PARITY_ODD;
  1068. else
  1069. parity = PCH_UART_HAL_PARITY_EVEN;
  1070. } else {
  1071. parity = PCH_UART_HAL_PARITY_NONE;
  1072. }
  1073. /* Only UART0 has auto hardware flow function */
  1074. if ((termios->c_cflag & CRTSCTS) && (priv->fifo_size == 256))
  1075. priv->mcr |= UART_MCR_AFE;
  1076. else
  1077. priv->mcr &= ~UART_MCR_AFE;
  1078. termios->c_cflag &= ~CMSPAR; /* Mark/Space parity is not supported */
  1079. baud = uart_get_baud_rate(port, termios, old, 0, port->uartclk / 16);
  1080. spin_lock_irqsave(&port->lock, flags);
  1081. uart_update_timeout(port, termios->c_cflag, baud);
  1082. rtn = pch_uart_hal_set_line(priv, baud, parity, bits, stb);
  1083. if (rtn)
  1084. goto out;
  1085. /* Don't rewrite B0 */
  1086. if (tty_termios_baud_rate(termios))
  1087. tty_termios_encode_baud_rate(termios, baud, baud);
  1088. out:
  1089. spin_unlock_irqrestore(&port->lock, flags);
  1090. }
  1091. static const char *pch_uart_type(struct uart_port *port)
  1092. {
  1093. return KBUILD_MODNAME;
  1094. }
  1095. static void pch_uart_release_port(struct uart_port *port)
  1096. {
  1097. struct eg20t_port *priv;
  1098. priv = container_of(port, struct eg20t_port, port);
  1099. pci_iounmap(priv->pdev, priv->membase);
  1100. pci_release_regions(priv->pdev);
  1101. }
  1102. static int pch_uart_request_port(struct uart_port *port)
  1103. {
  1104. struct eg20t_port *priv;
  1105. int ret;
  1106. void __iomem *membase;
  1107. priv = container_of(port, struct eg20t_port, port);
  1108. ret = pci_request_regions(priv->pdev, KBUILD_MODNAME);
  1109. if (ret < 0)
  1110. return -EBUSY;
  1111. membase = pci_iomap(priv->pdev, 1, 0);
  1112. if (!membase) {
  1113. pci_release_regions(priv->pdev);
  1114. return -EBUSY;
  1115. }
  1116. priv->membase = port->membase = membase;
  1117. return 0;
  1118. }
  1119. static void pch_uart_config_port(struct uart_port *port, int type)
  1120. {
  1121. struct eg20t_port *priv;
  1122. priv = container_of(port, struct eg20t_port, port);
  1123. if (type & UART_CONFIG_TYPE) {
  1124. port->type = priv->port_type;
  1125. pch_uart_request_port(port);
  1126. }
  1127. }
  1128. static int pch_uart_verify_port(struct uart_port *port,
  1129. struct serial_struct *serinfo)
  1130. {
  1131. struct eg20t_port *priv;
  1132. priv = container_of(port, struct eg20t_port, port);
  1133. if (serinfo->flags & UPF_LOW_LATENCY) {
  1134. dev_info(priv->port.dev,
  1135. "PCH UART : Use PIO Mode (without DMA)\n");
  1136. priv->use_dma = 0;
  1137. serinfo->flags &= ~UPF_LOW_LATENCY;
  1138. } else {
  1139. #ifndef CONFIG_PCH_DMA
  1140. dev_err(priv->port.dev, "%s : PCH DMA is not Loaded.\n",
  1141. __func__);
  1142. return -EOPNOTSUPP;
  1143. #endif
  1144. priv->use_dma = 1;
  1145. priv->use_dma_flag = 1;
  1146. dev_info(priv->port.dev, "PCH UART : Use DMA Mode\n");
  1147. }
  1148. return 0;
  1149. }
  1150. static struct uart_ops pch_uart_ops = {
  1151. .tx_empty = pch_uart_tx_empty,
  1152. .set_mctrl = pch_uart_set_mctrl,
  1153. .get_mctrl = pch_uart_get_mctrl,
  1154. .stop_tx = pch_uart_stop_tx,
  1155. .start_tx = pch_uart_start_tx,
  1156. .stop_rx = pch_uart_stop_rx,
  1157. .enable_ms = pch_uart_enable_ms,
  1158. .break_ctl = pch_uart_break_ctl,
  1159. .startup = pch_uart_startup,
  1160. .shutdown = pch_uart_shutdown,
  1161. .set_termios = pch_uart_set_termios,
  1162. /* .pm = pch_uart_pm, Not supported yet */
  1163. /* .set_wake = pch_uart_set_wake, Not supported yet */
  1164. .type = pch_uart_type,
  1165. .release_port = pch_uart_release_port,
  1166. .request_port = pch_uart_request_port,
  1167. .config_port = pch_uart_config_port,
  1168. .verify_port = pch_uart_verify_port
  1169. };
  1170. static struct uart_driver pch_uart_driver = {
  1171. .owner = THIS_MODULE,
  1172. .driver_name = KBUILD_MODNAME,
  1173. .dev_name = PCH_UART_DRIVER_DEVICE,
  1174. .major = 0,
  1175. .minor = 0,
  1176. .nr = PCH_UART_NR,
  1177. };
  1178. static struct eg20t_port *pch_uart_init_port(struct pci_dev *pdev,
  1179. const struct pci_device_id *id)
  1180. {
  1181. struct eg20t_port *priv;
  1182. int ret;
  1183. unsigned int iobase;
  1184. unsigned int mapbase;
  1185. unsigned char *rxbuf;
  1186. int fifosize, base_baud;
  1187. int port_type;
  1188. struct pch_uart_driver_data *board;
  1189. board = &drv_dat[id->driver_data];
  1190. port_type = board->port_type;
  1191. priv = kzalloc(sizeof(struct eg20t_port), GFP_KERNEL);
  1192. if (priv == NULL)
  1193. goto init_port_alloc_err;
  1194. rxbuf = (unsigned char *)__get_free_page(GFP_KERNEL);
  1195. if (!rxbuf)
  1196. goto init_port_free_txbuf;
  1197. base_baud = 1843200; /* 1.8432MHz */
  1198. /* quirk for CM-iTC board */
  1199. if (strstr(dmi_get_system_info(DMI_BOARD_NAME), "CM-iTC"))
  1200. base_baud = 192000000; /* 192.0MHz */
  1201. switch (port_type) {
  1202. case PORT_UNKNOWN:
  1203. fifosize = 256; /* EG20T/ML7213: UART0 */
  1204. break;
  1205. case PORT_8250:
  1206. fifosize = 64; /* EG20T:UART1~3 ML7213: UART1~2*/
  1207. break;
  1208. default:
  1209. dev_err(&pdev->dev, "Invalid Port Type(=%d)\n", port_type);
  1210. goto init_port_hal_free;
  1211. }
  1212. iobase = pci_resource_start(pdev, 0);
  1213. mapbase = pci_resource_start(pdev, 1);
  1214. priv->mapbase = mapbase;
  1215. priv->iobase = iobase;
  1216. priv->pdev = pdev;
  1217. priv->tx_empty = 1;
  1218. priv->rxbuf.buf = rxbuf;
  1219. priv->rxbuf.size = PAGE_SIZE;
  1220. priv->fifo_size = fifosize;
  1221. priv->base_baud = base_baud;
  1222. priv->port_type = PORT_MAX_8250 + port_type + 1;
  1223. priv->port.dev = &pdev->dev;
  1224. priv->port.iobase = iobase;
  1225. priv->port.membase = NULL;
  1226. priv->port.mapbase = mapbase;
  1227. priv->port.irq = pdev->irq;
  1228. priv->port.iotype = UPIO_PORT;
  1229. priv->port.ops = &pch_uart_ops;
  1230. priv->port.flags = UPF_BOOT_AUTOCONF;
  1231. priv->port.fifosize = fifosize;
  1232. priv->port.line = board->line_no;
  1233. priv->trigger = PCH_UART_HAL_TRIGGER_M;
  1234. spin_lock_init(&priv->port.lock);
  1235. pci_set_drvdata(pdev, priv);
  1236. pch_uart_hal_request(pdev, fifosize, base_baud);
  1237. ret = uart_add_one_port(&pch_uart_driver, &priv->port);
  1238. if (ret < 0)
  1239. goto init_port_hal_free;
  1240. return priv;
  1241. init_port_hal_free:
  1242. free_page((unsigned long)rxbuf);
  1243. init_port_free_txbuf:
  1244. kfree(priv);
  1245. init_port_alloc_err:
  1246. return NULL;
  1247. }
  1248. static void pch_uart_exit_port(struct eg20t_port *priv)
  1249. {
  1250. uart_remove_one_port(&pch_uart_driver, &priv->port);
  1251. pci_set_drvdata(priv->pdev, NULL);
  1252. free_page((unsigned long)priv->rxbuf.buf);
  1253. }
  1254. static void pch_uart_pci_remove(struct pci_dev *pdev)
  1255. {
  1256. struct eg20t_port *priv;
  1257. priv = (struct eg20t_port *)pci_get_drvdata(pdev);
  1258. pch_uart_exit_port(priv);
  1259. pci_disable_device(pdev);
  1260. kfree(priv);
  1261. return;
  1262. }
  1263. #ifdef CONFIG_PM
  1264. static int pch_uart_pci_suspend(struct pci_dev *pdev, pm_message_t state)
  1265. {
  1266. struct eg20t_port *priv = pci_get_drvdata(pdev);
  1267. uart_suspend_port(&pch_uart_driver, &priv->port);
  1268. pci_save_state(pdev);
  1269. pci_set_power_state(pdev, pci_choose_state(pdev, state));
  1270. return 0;
  1271. }
  1272. static int pch_uart_pci_resume(struct pci_dev *pdev)
  1273. {
  1274. struct eg20t_port *priv = pci_get_drvdata(pdev);
  1275. int ret;
  1276. pci_set_power_state(pdev, PCI_D0);
  1277. pci_restore_state(pdev);
  1278. ret = pci_enable_device(pdev);
  1279. if (ret) {
  1280. dev_err(&pdev->dev,
  1281. "%s-pci_enable_device failed(ret=%d) ", __func__, ret);
  1282. return ret;
  1283. }
  1284. uart_resume_port(&pch_uart_driver, &priv->port);
  1285. return 0;
  1286. }
  1287. #else
  1288. #define pch_uart_pci_suspend NULL
  1289. #define pch_uart_pci_resume NULL
  1290. #endif
  1291. static DEFINE_PCI_DEVICE_TABLE(pch_uart_pci_id) = {
  1292. {PCI_DEVICE(PCI_VENDOR_ID_INTEL, 0x8811),
  1293. .driver_data = pch_et20t_uart0},
  1294. {PCI_DEVICE(PCI_VENDOR_ID_INTEL, 0x8812),
  1295. .driver_data = pch_et20t_uart1},
  1296. {PCI_DEVICE(PCI_VENDOR_ID_INTEL, 0x8813),
  1297. .driver_data = pch_et20t_uart2},
  1298. {PCI_DEVICE(PCI_VENDOR_ID_INTEL, 0x8814),
  1299. .driver_data = pch_et20t_uart3},
  1300. {PCI_DEVICE(PCI_VENDOR_ID_ROHM, 0x8027),
  1301. .driver_data = pch_ml7213_uart0},
  1302. {PCI_DEVICE(PCI_VENDOR_ID_ROHM, 0x8028),
  1303. .driver_data = pch_ml7213_uart1},
  1304. {PCI_DEVICE(PCI_VENDOR_ID_ROHM, 0x8029),
  1305. .driver_data = pch_ml7213_uart2},
  1306. {PCI_DEVICE(PCI_VENDOR_ID_ROHM, 0x800C),
  1307. .driver_data = pch_ml7223_uart0},
  1308. {PCI_DEVICE(PCI_VENDOR_ID_ROHM, 0x800D),
  1309. .driver_data = pch_ml7223_uart1},
  1310. {0,},
  1311. };
  1312. static int __devinit pch_uart_pci_probe(struct pci_dev *pdev,
  1313. const struct pci_device_id *id)
  1314. {
  1315. int ret;
  1316. struct eg20t_port *priv;
  1317. ret = pci_enable_device(pdev);
  1318. if (ret < 0)
  1319. goto probe_error;
  1320. priv = pch_uart_init_port(pdev, id);
  1321. if (!priv) {
  1322. ret = -EBUSY;
  1323. goto probe_disable_device;
  1324. }
  1325. pci_set_drvdata(pdev, priv);
  1326. return ret;
  1327. probe_disable_device:
  1328. pci_disable_device(pdev);
  1329. probe_error:
  1330. return ret;
  1331. }
  1332. static struct pci_driver pch_uart_pci_driver = {
  1333. .name = "pch_uart",
  1334. .id_table = pch_uart_pci_id,
  1335. .probe = pch_uart_pci_probe,
  1336. .remove = __devexit_p(pch_uart_pci_remove),
  1337. .suspend = pch_uart_pci_suspend,
  1338. .resume = pch_uart_pci_resume,
  1339. };
  1340. static int __init pch_uart_module_init(void)
  1341. {
  1342. int ret;
  1343. /* register as UART driver */
  1344. ret = uart_register_driver(&pch_uart_driver);
  1345. if (ret < 0)
  1346. return ret;
  1347. /* register as PCI driver */
  1348. ret = pci_register_driver(&pch_uart_pci_driver);
  1349. if (ret < 0)
  1350. uart_unregister_driver(&pch_uart_driver);
  1351. return ret;
  1352. }
  1353. module_init(pch_uart_module_init);
  1354. static void __exit pch_uart_module_exit(void)
  1355. {
  1356. pci_unregister_driver(&pch_uart_pci_driver);
  1357. uart_unregister_driver(&pch_uart_driver);
  1358. }
  1359. module_exit(pch_uart_module_exit);
  1360. MODULE_LICENSE("GPL v2");
  1361. MODULE_DESCRIPTION("Intel EG20T PCH UART PCI Driver");
  1362. module_param(default_baud, uint, S_IRUGO);