pch_uart.c 47 KB

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