pch_uart.c 41 KB

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