pch_uart.c 40 KB

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