pch_uart.c 39 KB

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