pch_uart.c 50 KB

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