pch_uart.c 40 KB

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