pch_uart.c 40 KB

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