0006-ithc.patch 92 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897189818991900190119021903190419051906190719081909191019111912191319141915191619171918191919201921192219231924192519261927192819291930193119321933193419351936193719381939194019411942194319441945194619471948194919501951195219531954195519561957195819591960196119621963196419651966196719681969197019711972197319741975197619771978197919801981198219831984198519861987198819891990199119921993199419951996199719981999200020012002200320042005200620072008200920102011201220132014201520162017201820192020202120222023202420252026202720282029203020312032203320342035203620372038203920402041204220432044204520462047204820492050205120522053205420552056205720582059206020612062206320642065206620672068206920702071207220732074207520762077207820792080208120822083208420852086208720882089209020912092209320942095209620972098209921002101210221032104210521062107210821092110211121122113211421152116211721182119212021212122212321242125212621272128212921302131213221332134213521362137213821392140214121422143214421452146214721482149215021512152215321542155215621572158215921602161216221632164216521662167216821692170217121722173217421752176217721782179218021812182218321842185218621872188218921902191219221932194219521962197219821992200220122022203220422052206220722082209221022112212221322142215221622172218221922202221222222232224222522262227222822292230223122322233223422352236223722382239224022412242224322442245224622472248224922502251225222532254225522562257225822592260226122622263226422652266226722682269227022712272227322742275227622772278227922802281228222832284228522862287228822892290229122922293229422952296229722982299230023012302230323042305230623072308230923102311231223132314231523162317231823192320232123222323232423252326232723282329233023312332233323342335233623372338233923402341234223432344234523462347234823492350235123522353235423552356235723582359236023612362236323642365236623672368236923702371237223732374237523762377237823792380238123822383238423852386238723882389239023912392239323942395239623972398239924002401240224032404240524062407240824092410241124122413241424152416241724182419242024212422242324242425242624272428242924302431243224332434243524362437243824392440244124422443244424452446244724482449245024512452245324542455245624572458245924602461246224632464246524662467246824692470247124722473247424752476247724782479248024812482248324842485248624872488248924902491249224932494249524962497249824992500250125022503250425052506250725082509251025112512251325142515251625172518251925202521252225232524252525262527252825292530253125322533253425352536253725382539254025412542254325442545254625472548254925502551255225532554255525562557255825592560256125622563256425652566256725682569257025712572257325742575257625772578257925802581258225832584258525862587258825892590259125922593259425952596259725982599260026012602260326042605260626072608260926102611261226132614261526162617261826192620262126222623262426252626262726282629263026312632263326342635263626372638263926402641264226432644264526462647264826492650265126522653265426552656265726582659266026612662266326642665266626672668266926702671267226732674267526762677267826792680268126822683268426852686268726882689269026912692269326942695269626972698269927002701270227032704270527062707270827092710271127122713271427152716271727182719272027212722272327242725272627272728272927302731273227332734273527362737273827392740274127422743274427452746274727482749275027512752275327542755275627572758275927602761276227632764276527662767276827692770277127722773
  1. From 85f3a4111005d23749f1a5b0424264ba38bbb3f4 Mon Sep 17 00:00:00 2001
  2. From: Dorian Stoll <dorian.stoll@tmsp.io>
  3. Date: Sun, 11 Dec 2022 12:03:38 +0100
  4. Subject: [PATCH] iommu: intel: Disable source id verification for ITHC
  5. Signed-off-by: Dorian Stoll <dorian.stoll@tmsp.io>
  6. Patchset: ithc
  7. ---
  8. drivers/iommu/intel/irq_remapping.c | 16 ++++++++++++++++
  9. 1 file changed, 16 insertions(+)
  10. diff --git a/drivers/iommu/intel/irq_remapping.c b/drivers/iommu/intel/irq_remapping.c
  11. index ad795c772f21..2c10bca9e06b 100644
  12. --- a/drivers/iommu/intel/irq_remapping.c
  13. +++ b/drivers/iommu/intel/irq_remapping.c
  14. @@ -388,6 +388,22 @@ static int set_msi_sid(struct irte *irte, struct pci_dev *dev)
  15. data.busmatch_count = 0;
  16. pci_for_each_dma_alias(dev, set_msi_sid_cb, &data);
  17. + /*
  18. + * The Intel Touch Host Controller is at 00:10.6, but for some reason
  19. + * the MSI interrupts have request id 01:05.0.
  20. + * Disable id verification to work around this.
  21. + * FIXME Find proper fix or turn this into a quirk.
  22. + */
  23. + if (dev->vendor == PCI_VENDOR_ID_INTEL && (dev->class >> 8) == PCI_CLASS_INPUT_PEN) {
  24. + switch(dev->device) {
  25. + case 0x98d0: case 0x98d1: // LKF
  26. + case 0xa0d0: case 0xa0d1: // TGL LP
  27. + case 0x43d0: case 0x43d1: // TGL H
  28. + set_irte_sid(irte, SVT_NO_VERIFY, SQ_ALL_16, 0);
  29. + return 0;
  30. + }
  31. + }
  32. +
  33. /*
  34. * DMA alias provides us with a PCI device and alias. The only case
  35. * where the it will return an alias on a different bus than the
  36. --
  37. 2.49.0
  38. From 3e386a2ed36c9d620f4de23d671daa75a509d797 Mon Sep 17 00:00:00 2001
  39. From: quo <tuple@list.ru>
  40. Date: Sun, 11 Dec 2022 12:10:54 +0100
  41. Subject: [PATCH] hid: Add support for Intel Touch Host Controller
  42. Based on quo/ithc-linux@34539af4726d.
  43. Signed-off-by: Maximilian Stoll <luzmaximilian@gmail.com>
  44. Patchset: ithc
  45. ---
  46. drivers/hid/Kconfig | 2 +
  47. drivers/hid/Makefile | 1 +
  48. drivers/hid/ithc/Kbuild | 6 +
  49. drivers/hid/ithc/Kconfig | 12 +
  50. drivers/hid/ithc/ithc-debug.c | 149 ++++++++
  51. drivers/hid/ithc/ithc-debug.h | 7 +
  52. drivers/hid/ithc/ithc-dma.c | 312 ++++++++++++++++
  53. drivers/hid/ithc/ithc-dma.h | 47 +++
  54. drivers/hid/ithc/ithc-hid.c | 207 +++++++++++
  55. drivers/hid/ithc/ithc-hid.h | 32 ++
  56. drivers/hid/ithc/ithc-legacy.c | 254 +++++++++++++
  57. drivers/hid/ithc/ithc-legacy.h | 8 +
  58. drivers/hid/ithc/ithc-main.c | 431 ++++++++++++++++++++++
  59. drivers/hid/ithc/ithc-quickspi.c | 607 +++++++++++++++++++++++++++++++
  60. drivers/hid/ithc/ithc-quickspi.h | 39 ++
  61. drivers/hid/ithc/ithc-regs.c | 154 ++++++++
  62. drivers/hid/ithc/ithc-regs.h | 211 +++++++++++
  63. drivers/hid/ithc/ithc.h | 89 +++++
  64. 18 files changed, 2568 insertions(+)
  65. create mode 100644 drivers/hid/ithc/Kbuild
  66. create mode 100644 drivers/hid/ithc/Kconfig
  67. create mode 100644 drivers/hid/ithc/ithc-debug.c
  68. create mode 100644 drivers/hid/ithc/ithc-debug.h
  69. create mode 100644 drivers/hid/ithc/ithc-dma.c
  70. create mode 100644 drivers/hid/ithc/ithc-dma.h
  71. create mode 100644 drivers/hid/ithc/ithc-hid.c
  72. create mode 100644 drivers/hid/ithc/ithc-hid.h
  73. create mode 100644 drivers/hid/ithc/ithc-legacy.c
  74. create mode 100644 drivers/hid/ithc/ithc-legacy.h
  75. create mode 100644 drivers/hid/ithc/ithc-main.c
  76. create mode 100644 drivers/hid/ithc/ithc-quickspi.c
  77. create mode 100644 drivers/hid/ithc/ithc-quickspi.h
  78. create mode 100644 drivers/hid/ithc/ithc-regs.c
  79. create mode 100644 drivers/hid/ithc/ithc-regs.h
  80. create mode 100644 drivers/hid/ithc/ithc.h
  81. diff --git a/drivers/hid/Kconfig b/drivers/hid/Kconfig
  82. index f91ba8a06cfc..e945736e216f 100644
  83. --- a/drivers/hid/Kconfig
  84. +++ b/drivers/hid/Kconfig
  85. @@ -1389,6 +1389,8 @@ source "drivers/hid/intel-thc-hid/Kconfig"
  86. source "drivers/hid/ipts/Kconfig"
  87. +source "drivers/hid/ithc/Kconfig"
  88. +
  89. endif # HID
  90. # USB support may be used with HID disabled
  91. diff --git a/drivers/hid/Makefile b/drivers/hid/Makefile
  92. index c86c9e80d225..046eb5fd8017 100644
  93. --- a/drivers/hid/Makefile
  94. +++ b/drivers/hid/Makefile
  95. @@ -174,3 +174,4 @@ obj-$(CONFIG_SURFACE_HID_CORE) += surface-hid/
  96. obj-$(CONFIG_INTEL_THC_HID) += intel-thc-hid/
  97. obj-$(CONFIG_HID_IPTS) += ipts/
  98. +obj-$(CONFIG_HID_ITHC) += ithc/
  99. diff --git a/drivers/hid/ithc/Kbuild b/drivers/hid/ithc/Kbuild
  100. new file mode 100644
  101. index 000000000000..4937ba131297
  102. --- /dev/null
  103. +++ b/drivers/hid/ithc/Kbuild
  104. @@ -0,0 +1,6 @@
  105. +obj-$(CONFIG_HID_ITHC) := ithc.o
  106. +
  107. +ithc-objs := ithc-main.o ithc-regs.o ithc-dma.o ithc-hid.o ithc-legacy.o ithc-quickspi.o ithc-debug.o
  108. +
  109. +ccflags-y := -std=gnu11 -Wno-declaration-after-statement
  110. +
  111. diff --git a/drivers/hid/ithc/Kconfig b/drivers/hid/ithc/Kconfig
  112. new file mode 100644
  113. index 000000000000..ede713023609
  114. --- /dev/null
  115. +++ b/drivers/hid/ithc/Kconfig
  116. @@ -0,0 +1,12 @@
  117. +config HID_ITHC
  118. + tristate "Intel Touch Host Controller"
  119. + depends on PCI
  120. + depends on HID
  121. + help
  122. + Say Y here if your system has a touchscreen using Intels
  123. + Touch Host Controller (ITHC / IPTS) technology.
  124. +
  125. + If unsure say N.
  126. +
  127. + To compile this driver as a module, choose M here: the
  128. + module will be called ithc.
  129. diff --git a/drivers/hid/ithc/ithc-debug.c b/drivers/hid/ithc/ithc-debug.c
  130. new file mode 100644
  131. index 000000000000..2d8c6afe9966
  132. --- /dev/null
  133. +++ b/drivers/hid/ithc/ithc-debug.c
  134. @@ -0,0 +1,149 @@
  135. +// SPDX-License-Identifier: GPL-2.0 OR BSD-2-Clause
  136. +
  137. +#include "ithc.h"
  138. +
  139. +void ithc_log_regs(struct ithc *ithc)
  140. +{
  141. + if (!ithc->prev_regs)
  142. + return;
  143. + u32 __iomem *cur = (__iomem void *)ithc->regs;
  144. + u32 *prev = (void *)ithc->prev_regs;
  145. + for (int i = 1024; i < sizeof(*ithc->regs) / 4; i++) {
  146. + u32 x = readl(cur + i);
  147. + if (x != prev[i]) {
  148. + pci_info(ithc->pci, "reg %04x: %08x -> %08x\n", i * 4, prev[i], x);
  149. + prev[i] = x;
  150. + }
  151. + }
  152. +}
  153. +
  154. +static ssize_t ithc_debugfs_cmd_write(struct file *f, const char __user *buf, size_t len,
  155. + loff_t *offset)
  156. +{
  157. + // Debug commands consist of a single letter followed by a list of numbers (decimal or
  158. + // hexadecimal, space-separated).
  159. + struct ithc *ithc = file_inode(f)->i_private;
  160. + char cmd[256];
  161. + if (!ithc || !ithc->pci)
  162. + return -ENODEV;
  163. + if (!len)
  164. + return -EINVAL;
  165. + if (len >= sizeof(cmd))
  166. + return -EINVAL;
  167. + if (copy_from_user(cmd, buf, len))
  168. + return -EFAULT;
  169. + cmd[len] = 0;
  170. + if (cmd[len-1] == '\n')
  171. + cmd[len-1] = 0;
  172. + pci_info(ithc->pci, "debug command: %s\n", cmd);
  173. +
  174. + // Parse the list of arguments into a u32 array.
  175. + u32 n = 0;
  176. + const char *s = cmd + 1;
  177. + u32 a[32];
  178. + while (*s && *s != '\n') {
  179. + if (n >= ARRAY_SIZE(a))
  180. + return -EINVAL;
  181. + if (*s++ != ' ')
  182. + return -EINVAL;
  183. + char *e;
  184. + a[n++] = simple_strtoul(s, &e, 0);
  185. + if (e == s)
  186. + return -EINVAL;
  187. + s = e;
  188. + }
  189. + ithc_log_regs(ithc);
  190. +
  191. + // Execute the command.
  192. + switch (cmd[0]) {
  193. + case 'x': // reset
  194. + ithc_reset(ithc);
  195. + break;
  196. + case 'w': // write register: offset mask value
  197. + if (n != 3 || (a[0] & 3))
  198. + return -EINVAL;
  199. + pci_info(ithc->pci, "debug write 0x%04x = 0x%08x (mask 0x%08x)\n",
  200. + a[0], a[2], a[1]);
  201. + bitsl(((__iomem u32 *)ithc->regs) + a[0] / 4, a[1], a[2]);
  202. + break;
  203. + case 'r': // read register: offset
  204. + if (n != 1 || (a[0] & 3))
  205. + return -EINVAL;
  206. + pci_info(ithc->pci, "debug read 0x%04x = 0x%08x\n", a[0],
  207. + readl(((__iomem u32 *)ithc->regs) + a[0] / 4));
  208. + break;
  209. + case 's': // spi command: cmd offset len data...
  210. + // read config: s 4 0 64 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0
  211. + // set touch cfg: s 6 12 4 XX
  212. + if (n < 3 || a[2] > (n - 3) * 4)
  213. + return -EINVAL;
  214. + pci_info(ithc->pci, "debug spi command %u with %u bytes of data\n", a[0], a[2]);
  215. + if (!CHECK(ithc_spi_command, ithc, a[0], a[1], a[2], a + 3))
  216. + for (u32 i = 0; i < (a[2] + 3) / 4; i++)
  217. + pci_info(ithc->pci, "resp %u = 0x%08x\n", i, a[3+i]);
  218. + break;
  219. + case 'd': // dma command: cmd len data...
  220. + // get report descriptor: d 7 8 0 0
  221. + // enable multitouch: d 3 2 0x0105
  222. + if (n < 1)
  223. + return -EINVAL;
  224. + pci_info(ithc->pci, "debug dma command with %u bytes of data\n", n * 4);
  225. + struct ithc_data data = { .type = ITHC_DATA_RAW, .size = n * 4, .data = a };
  226. + if (ithc_dma_tx(ithc, &data))
  227. + pci_err(ithc->pci, "dma tx failed\n");
  228. + break;
  229. + default:
  230. + return -EINVAL;
  231. + }
  232. + ithc_log_regs(ithc);
  233. + return len;
  234. +}
  235. +
  236. +static struct dentry *dbg_dir;
  237. +
  238. +void __init ithc_debug_init_module(void)
  239. +{
  240. + struct dentry *d = debugfs_create_dir(DEVNAME, NULL);
  241. + if (IS_ERR(d))
  242. + pr_warn("failed to create debugfs dir (%li)\n", PTR_ERR(d));
  243. + else
  244. + dbg_dir = d;
  245. +}
  246. +
  247. +void __exit ithc_debug_exit_module(void)
  248. +{
  249. + debugfs_remove_recursive(dbg_dir);
  250. + dbg_dir = NULL;
  251. +}
  252. +
  253. +static const struct file_operations ithc_debugfops_cmd = {
  254. + .owner = THIS_MODULE,
  255. + .write = ithc_debugfs_cmd_write,
  256. +};
  257. +
  258. +static void ithc_debugfs_devres_release(struct device *dev, void *res)
  259. +{
  260. + struct dentry **dbgm = res;
  261. + debugfs_remove_recursive(*dbgm);
  262. +}
  263. +
  264. +int ithc_debug_init_device(struct ithc *ithc)
  265. +{
  266. + if (!dbg_dir)
  267. + return -ENOENT;
  268. + struct dentry **dbgm = devres_alloc(ithc_debugfs_devres_release, sizeof(*dbgm), GFP_KERNEL);
  269. + if (!dbgm)
  270. + return -ENOMEM;
  271. + devres_add(&ithc->pci->dev, dbgm);
  272. + struct dentry *dbg = debugfs_create_dir(pci_name(ithc->pci), dbg_dir);
  273. + if (IS_ERR(dbg))
  274. + return PTR_ERR(dbg);
  275. + *dbgm = dbg;
  276. +
  277. + struct dentry *cmd = debugfs_create_file("cmd", 0220, dbg, ithc, &ithc_debugfops_cmd);
  278. + if (IS_ERR(cmd))
  279. + return PTR_ERR(cmd);
  280. +
  281. + return 0;
  282. +}
  283. +
  284. diff --git a/drivers/hid/ithc/ithc-debug.h b/drivers/hid/ithc/ithc-debug.h
  285. new file mode 100644
  286. index 000000000000..38c53d916bdb
  287. --- /dev/null
  288. +++ b/drivers/hid/ithc/ithc-debug.h
  289. @@ -0,0 +1,7 @@
  290. +/* SPDX-License-Identifier: GPL-2.0 OR BSD-2-Clause */
  291. +
  292. +void ithc_debug_init_module(void);
  293. +void ithc_debug_exit_module(void);
  294. +int ithc_debug_init_device(struct ithc *ithc);
  295. +void ithc_log_regs(struct ithc *ithc);
  296. +
  297. diff --git a/drivers/hid/ithc/ithc-dma.c b/drivers/hid/ithc/ithc-dma.c
  298. new file mode 100644
  299. index 000000000000..bf4eab33062b
  300. --- /dev/null
  301. +++ b/drivers/hid/ithc/ithc-dma.c
  302. @@ -0,0 +1,312 @@
  303. +// SPDX-License-Identifier: GPL-2.0 OR BSD-2-Clause
  304. +
  305. +#include "ithc.h"
  306. +
  307. +// The THC uses tables of PRDs (physical region descriptors) to describe the TX and RX data buffers.
  308. +// Each PRD contains the DMA address and size of a block of DMA memory, and some status flags.
  309. +// This allows each data buffer to consist of multiple non-contiguous blocks of memory.
  310. +
  311. +static int ithc_dma_prd_alloc(struct ithc *ithc, struct ithc_dma_prd_buffer *p,
  312. + unsigned int num_buffers, unsigned int num_pages, enum dma_data_direction dir)
  313. +{
  314. + p->num_pages = num_pages;
  315. + p->dir = dir;
  316. + // We allocate enough space to have one PRD per data buffer page, however if the data
  317. + // buffer pages happen to be contiguous, we can describe the buffer using fewer PRDs, so
  318. + // some will remain unused (which is fine).
  319. + p->size = round_up(num_buffers * num_pages * sizeof(struct ithc_phys_region_desc), PAGE_SIZE);
  320. + p->addr = dmam_alloc_coherent(&ithc->pci->dev, p->size, &p->dma_addr, GFP_KERNEL);
  321. + if (!p->addr)
  322. + return -ENOMEM;
  323. + if (p->dma_addr & (PAGE_SIZE - 1))
  324. + return -EFAULT;
  325. + return 0;
  326. +}
  327. +
  328. +// Devres managed sg_table wrapper.
  329. +struct ithc_sg_table {
  330. + void *addr;
  331. + struct sg_table sgt;
  332. + enum dma_data_direction dir;
  333. +};
  334. +static void ithc_dma_sgtable_free(struct sg_table *sgt)
  335. +{
  336. + struct scatterlist *sg;
  337. + int i;
  338. + for_each_sgtable_sg(sgt, sg, i) {
  339. + struct page *p = sg_page(sg);
  340. + if (p)
  341. + __free_page(p);
  342. + }
  343. + sg_free_table(sgt);
  344. +}
  345. +static void ithc_dma_data_devres_release(struct device *dev, void *res)
  346. +{
  347. + struct ithc_sg_table *sgt = res;
  348. + if (sgt->addr)
  349. + vunmap(sgt->addr);
  350. + dma_unmap_sgtable(dev, &sgt->sgt, sgt->dir, 0);
  351. + ithc_dma_sgtable_free(&sgt->sgt);
  352. +}
  353. +
  354. +static int ithc_dma_data_alloc(struct ithc *ithc, struct ithc_dma_prd_buffer *prds,
  355. + struct ithc_dma_data_buffer *b)
  356. +{
  357. + // We don't use dma_alloc_coherent() for data buffers, because they don't have to be
  358. + // coherent (they are unidirectional) or contiguous (we can use one PRD per page).
  359. + // We could use dma_alloc_noncontiguous(), however this still always allocates a single
  360. + // DMA mapped segment, which is more restrictive than what we need.
  361. + // Instead we use an sg_table of individually allocated pages.
  362. + struct page *pages[16];
  363. + if (prds->num_pages == 0 || prds->num_pages > ARRAY_SIZE(pages))
  364. + return -EINVAL;
  365. + b->active_idx = -1;
  366. + struct ithc_sg_table *sgt = devres_alloc(
  367. + ithc_dma_data_devres_release, sizeof(*sgt), GFP_KERNEL);
  368. + if (!sgt)
  369. + return -ENOMEM;
  370. + sgt->dir = prds->dir;
  371. +
  372. + if (!sg_alloc_table(&sgt->sgt, prds->num_pages, GFP_KERNEL)) {
  373. + struct scatterlist *sg;
  374. + int i;
  375. + bool ok = true;
  376. + for_each_sgtable_sg(&sgt->sgt, sg, i) {
  377. + // NOTE: don't need __GFP_DMA for PCI DMA
  378. + struct page *p = pages[i] = alloc_page(GFP_KERNEL | __GFP_ZERO);
  379. + if (!p) {
  380. + ok = false;
  381. + break;
  382. + }
  383. + sg_set_page(sg, p, PAGE_SIZE, 0);
  384. + }
  385. + if (ok && !dma_map_sgtable(&ithc->pci->dev, &sgt->sgt, prds->dir, 0)) {
  386. + devres_add(&ithc->pci->dev, sgt);
  387. + b->sgt = &sgt->sgt;
  388. + b->addr = sgt->addr = vmap(pages, prds->num_pages, 0, PAGE_KERNEL);
  389. + if (!b->addr)
  390. + return -ENOMEM;
  391. + return 0;
  392. + }
  393. + ithc_dma_sgtable_free(&sgt->sgt);
  394. + }
  395. + devres_free(sgt);
  396. + return -ENOMEM;
  397. +}
  398. +
  399. +static int ithc_dma_data_buffer_put(struct ithc *ithc, struct ithc_dma_prd_buffer *prds,
  400. + struct ithc_dma_data_buffer *b, unsigned int idx)
  401. +{
  402. + // Give a buffer to the THC.
  403. + struct ithc_phys_region_desc *prd = prds->addr;
  404. + prd += idx * prds->num_pages;
  405. + if (b->active_idx >= 0) {
  406. + pci_err(ithc->pci, "buffer already active\n");
  407. + return -EINVAL;
  408. + }
  409. + b->active_idx = idx;
  410. + if (prds->dir == DMA_TO_DEVICE) {
  411. + // TX buffer: Caller should have already filled the data buffer, so just fill
  412. + // the PRD and flush.
  413. + // (TODO: Support multi-page TX buffers. So far no device seems to use or need
  414. + // these though.)
  415. + if (b->data_size > PAGE_SIZE)
  416. + return -EINVAL;
  417. + prd->addr = sg_dma_address(b->sgt->sgl) >> 10;
  418. + prd->size = b->data_size | PRD_FLAG_END;
  419. + flush_kernel_vmap_range(b->addr, b->data_size);
  420. + } else if (prds->dir == DMA_FROM_DEVICE) {
  421. + // RX buffer: Reset PRDs.
  422. + struct scatterlist *sg;
  423. + int i;
  424. + for_each_sgtable_dma_sg(b->sgt, sg, i) {
  425. + prd->addr = sg_dma_address(sg) >> 10;
  426. + prd->size = sg_dma_len(sg);
  427. + prd++;
  428. + }
  429. + prd[-1].size |= PRD_FLAG_END;
  430. + }
  431. + dma_wmb(); // for the prds
  432. + dma_sync_sgtable_for_device(&ithc->pci->dev, b->sgt, prds->dir);
  433. + return 0;
  434. +}
  435. +
  436. +static int ithc_dma_data_buffer_get(struct ithc *ithc, struct ithc_dma_prd_buffer *prds,
  437. + struct ithc_dma_data_buffer *b, unsigned int idx)
  438. +{
  439. + // Take a buffer from the THC.
  440. + struct ithc_phys_region_desc *prd = prds->addr;
  441. + prd += idx * prds->num_pages;
  442. + // This is purely a sanity check. We don't strictly need the idx parameter for this
  443. + // function, because it should always be the same as active_idx, unless we have a bug.
  444. + if (b->active_idx != idx) {
  445. + pci_err(ithc->pci, "wrong buffer index\n");
  446. + return -EINVAL;
  447. + }
  448. + b->active_idx = -1;
  449. + if (prds->dir == DMA_FROM_DEVICE) {
  450. + // RX buffer: Calculate actual received data size from PRDs.
  451. + dma_rmb(); // for the prds
  452. + b->data_size = 0;
  453. + struct scatterlist *sg;
  454. + int i;
  455. + for_each_sgtable_dma_sg(b->sgt, sg, i) {
  456. + unsigned int size = prd->size;
  457. + b->data_size += size & PRD_SIZE_MASK;
  458. + if (size & PRD_FLAG_END)
  459. + break;
  460. + if ((size & PRD_SIZE_MASK) != sg_dma_len(sg)) {
  461. + pci_err(ithc->pci, "truncated prd\n");
  462. + break;
  463. + }
  464. + prd++;
  465. + }
  466. + invalidate_kernel_vmap_range(b->addr, b->data_size);
  467. + }
  468. + dma_sync_sgtable_for_cpu(&ithc->pci->dev, b->sgt, prds->dir);
  469. + return 0;
  470. +}
  471. +
  472. +int ithc_dma_rx_init(struct ithc *ithc, u8 channel)
  473. +{
  474. + struct ithc_dma_rx *rx = &ithc->dma_rx[channel];
  475. + mutex_init(&rx->mutex);
  476. +
  477. + // Allocate buffers.
  478. + unsigned int num_pages = (ithc->max_rx_size + PAGE_SIZE - 1) / PAGE_SIZE;
  479. + pci_dbg(ithc->pci, "allocating rx buffers: num = %u, size = %u, pages = %u\n",
  480. + NUM_RX_BUF, ithc->max_rx_size, num_pages);
  481. + CHECK_RET(ithc_dma_prd_alloc, ithc, &rx->prds, NUM_RX_BUF, num_pages, DMA_FROM_DEVICE);
  482. + for (unsigned int i = 0; i < NUM_RX_BUF; i++)
  483. + CHECK_RET(ithc_dma_data_alloc, ithc, &rx->prds, &rx->bufs[i]);
  484. +
  485. + // Init registers.
  486. + writeb(DMA_RX_CONTROL2_RESET, &ithc->regs->dma_rx[channel].control2);
  487. + lo_hi_writeq(rx->prds.dma_addr, &ithc->regs->dma_rx[channel].addr);
  488. + writeb(NUM_RX_BUF - 1, &ithc->regs->dma_rx[channel].num_bufs);
  489. + writeb(num_pages - 1, &ithc->regs->dma_rx[channel].num_prds);
  490. + u8 head = readb(&ithc->regs->dma_rx[channel].head);
  491. + if (head) {
  492. + pci_err(ithc->pci, "head is nonzero (%u)\n", head);
  493. + return -EIO;
  494. + }
  495. +
  496. + // Init buffers.
  497. + for (unsigned int i = 0; i < NUM_RX_BUF; i++)
  498. + CHECK_RET(ithc_dma_data_buffer_put, ithc, &rx->prds, &rx->bufs[i], i);
  499. +
  500. + writeb(head ^ DMA_RX_WRAP_FLAG, &ithc->regs->dma_rx[channel].tail);
  501. + return 0;
  502. +}
  503. +
  504. +void ithc_dma_rx_enable(struct ithc *ithc, u8 channel)
  505. +{
  506. + bitsb_set(&ithc->regs->dma_rx[channel].control,
  507. + DMA_RX_CONTROL_ENABLE | DMA_RX_CONTROL_IRQ_ERROR | DMA_RX_CONTROL_IRQ_DATA);
  508. + CHECK(waitl, ithc, &ithc->regs->dma_rx[channel].status,
  509. + DMA_RX_STATUS_ENABLED, DMA_RX_STATUS_ENABLED);
  510. +}
  511. +
  512. +int ithc_dma_tx_init(struct ithc *ithc)
  513. +{
  514. + struct ithc_dma_tx *tx = &ithc->dma_tx;
  515. + mutex_init(&tx->mutex);
  516. +
  517. + // Allocate buffers.
  518. + unsigned int num_pages = (ithc->max_tx_size + PAGE_SIZE - 1) / PAGE_SIZE;
  519. + pci_dbg(ithc->pci, "allocating tx buffers: size = %u, pages = %u\n",
  520. + ithc->max_tx_size, num_pages);
  521. + CHECK_RET(ithc_dma_prd_alloc, ithc, &tx->prds, 1, num_pages, DMA_TO_DEVICE);
  522. + CHECK_RET(ithc_dma_data_alloc, ithc, &tx->prds, &tx->buf);
  523. +
  524. + // Init registers.
  525. + lo_hi_writeq(tx->prds.dma_addr, &ithc->regs->dma_tx.addr);
  526. + writeb(num_pages - 1, &ithc->regs->dma_tx.num_prds);
  527. +
  528. + // Init buffers.
  529. + CHECK_RET(ithc_dma_data_buffer_put, ithc, &ithc->dma_tx.prds, &ithc->dma_tx.buf, 0);
  530. + return 0;
  531. +}
  532. +
  533. +static int ithc_dma_rx_unlocked(struct ithc *ithc, u8 channel)
  534. +{
  535. + // Process all filled RX buffers from the ringbuffer.
  536. + struct ithc_dma_rx *rx = &ithc->dma_rx[channel];
  537. + unsigned int n = rx->num_received;
  538. + u8 head_wrap = readb(&ithc->regs->dma_rx[channel].head);
  539. + while (1) {
  540. + u8 tail = n % NUM_RX_BUF;
  541. + u8 tail_wrap = tail | ((n / NUM_RX_BUF) & 1 ? 0 : DMA_RX_WRAP_FLAG);
  542. + writeb(tail_wrap, &ithc->regs->dma_rx[channel].tail);
  543. + // ringbuffer is full if tail_wrap == head_wrap
  544. + // ringbuffer is empty if tail_wrap == head_wrap ^ WRAP_FLAG
  545. + if (tail_wrap == (head_wrap ^ DMA_RX_WRAP_FLAG))
  546. + return 0;
  547. +
  548. + // take the buffer that the device just filled
  549. + struct ithc_dma_data_buffer *b = &rx->bufs[n % NUM_RX_BUF];
  550. + CHECK_RET(ithc_dma_data_buffer_get, ithc, &rx->prds, b, tail);
  551. + rx->num_received = ++n;
  552. +
  553. + // process data
  554. + struct ithc_data d;
  555. + if ((ithc->use_quickspi ? ithc_quickspi_decode_rx : ithc_legacy_decode_rx)
  556. + (ithc, b->addr, b->data_size, &d) < 0) {
  557. + pci_err(ithc->pci, "invalid dma rx data! channel %u, buffer %u, size %u: %*ph\n",
  558. + channel, tail, b->data_size, min((int)b->data_size, 64), b->addr);
  559. + print_hex_dump_debug(DEVNAME " data: ", DUMP_PREFIX_OFFSET, 32, 1,
  560. + b->addr, min(b->data_size, 0x400u), 0);
  561. + } else {
  562. + ithc_hid_process_data(ithc, &d);
  563. + }
  564. +
  565. + // give the buffer back to the device
  566. + CHECK_RET(ithc_dma_data_buffer_put, ithc, &rx->prds, b, tail);
  567. + }
  568. +}
  569. +int ithc_dma_rx(struct ithc *ithc, u8 channel)
  570. +{
  571. + struct ithc_dma_rx *rx = &ithc->dma_rx[channel];
  572. + mutex_lock(&rx->mutex);
  573. + int ret = ithc_dma_rx_unlocked(ithc, channel);
  574. + mutex_unlock(&rx->mutex);
  575. + return ret;
  576. +}
  577. +
  578. +static int ithc_dma_tx_unlocked(struct ithc *ithc, const struct ithc_data *data)
  579. +{
  580. + // Send a single TX buffer to the THC.
  581. + pci_dbg(ithc->pci, "dma tx data type %u, size %u\n", data->type, data->size);
  582. + CHECK_RET(ithc_dma_data_buffer_get, ithc, &ithc->dma_tx.prds, &ithc->dma_tx.buf, 0);
  583. +
  584. + // Fill the TX buffer with header and data.
  585. + ssize_t sz;
  586. + if (data->type == ITHC_DATA_RAW) {
  587. + sz = min(data->size, ithc->max_tx_size);
  588. + memcpy(ithc->dma_tx.buf.addr, data->data, sz);
  589. + } else {
  590. + sz = (ithc->use_quickspi ? ithc_quickspi_encode_tx : ithc_legacy_encode_tx)
  591. + (ithc, data, ithc->dma_tx.buf.addr, ithc->max_tx_size);
  592. + }
  593. + ithc->dma_tx.buf.data_size = sz < 0 ? 0 : sz;
  594. + CHECK_RET(ithc_dma_data_buffer_put, ithc, &ithc->dma_tx.prds, &ithc->dma_tx.buf, 0);
  595. + if (sz < 0) {
  596. + pci_err(ithc->pci, "failed to encode tx data type %i, size %u, error %i\n",
  597. + data->type, data->size, (int)sz);
  598. + return -EINVAL;
  599. + }
  600. +
  601. + // Let the THC process the buffer.
  602. + bitsb_set(&ithc->regs->dma_tx.control, DMA_TX_CONTROL_SEND);
  603. + CHECK_RET(waitb, ithc, &ithc->regs->dma_tx.control, DMA_TX_CONTROL_SEND, 0);
  604. + writel(DMA_TX_STATUS_DONE, &ithc->regs->dma_tx.status);
  605. + return 0;
  606. +}
  607. +int ithc_dma_tx(struct ithc *ithc, const struct ithc_data *data)
  608. +{
  609. + mutex_lock(&ithc->dma_tx.mutex);
  610. + int ret = ithc_dma_tx_unlocked(ithc, data);
  611. + mutex_unlock(&ithc->dma_tx.mutex);
  612. + return ret;
  613. +}
  614. +
  615. diff --git a/drivers/hid/ithc/ithc-dma.h b/drivers/hid/ithc/ithc-dma.h
  616. new file mode 100644
  617. index 000000000000..1749a5819b3e
  618. --- /dev/null
  619. +++ b/drivers/hid/ithc/ithc-dma.h
  620. @@ -0,0 +1,47 @@
  621. +/* SPDX-License-Identifier: GPL-2.0 OR BSD-2-Clause */
  622. +
  623. +#define PRD_SIZE_MASK 0xffffff
  624. +#define PRD_FLAG_END 0x1000000
  625. +#define PRD_FLAG_SUCCESS 0x2000000
  626. +#define PRD_FLAG_ERROR 0x4000000
  627. +
  628. +struct ithc_phys_region_desc {
  629. + u64 addr; // physical addr/1024
  630. + u32 size; // num bytes, PRD_FLAG_END marks last prd for data split over multiple prds
  631. + u32 unused;
  632. +};
  633. +
  634. +struct ithc_dma_prd_buffer {
  635. + void *addr;
  636. + dma_addr_t dma_addr;
  637. + u32 size;
  638. + u32 num_pages; // per data buffer
  639. + enum dma_data_direction dir;
  640. +};
  641. +
  642. +struct ithc_dma_data_buffer {
  643. + void *addr;
  644. + struct sg_table *sgt;
  645. + int active_idx;
  646. + u32 data_size;
  647. +};
  648. +
  649. +struct ithc_dma_tx {
  650. + struct mutex mutex;
  651. + struct ithc_dma_prd_buffer prds;
  652. + struct ithc_dma_data_buffer buf;
  653. +};
  654. +
  655. +struct ithc_dma_rx {
  656. + struct mutex mutex;
  657. + u32 num_received;
  658. + struct ithc_dma_prd_buffer prds;
  659. + struct ithc_dma_data_buffer bufs[NUM_RX_BUF];
  660. +};
  661. +
  662. +int ithc_dma_rx_init(struct ithc *ithc, u8 channel);
  663. +void ithc_dma_rx_enable(struct ithc *ithc, u8 channel);
  664. +int ithc_dma_tx_init(struct ithc *ithc);
  665. +int ithc_dma_rx(struct ithc *ithc, u8 channel);
  666. +int ithc_dma_tx(struct ithc *ithc, const struct ithc_data *data);
  667. +
  668. diff --git a/drivers/hid/ithc/ithc-hid.c b/drivers/hid/ithc/ithc-hid.c
  669. new file mode 100644
  670. index 000000000000..065646ab499e
  671. --- /dev/null
  672. +++ b/drivers/hid/ithc/ithc-hid.c
  673. @@ -0,0 +1,207 @@
  674. +// SPDX-License-Identifier: GPL-2.0 OR BSD-2-Clause
  675. +
  676. +#include "ithc.h"
  677. +
  678. +static int ithc_hid_start(struct hid_device *hdev) { return 0; }
  679. +static void ithc_hid_stop(struct hid_device *hdev) { }
  680. +static int ithc_hid_open(struct hid_device *hdev) { return 0; }
  681. +static void ithc_hid_close(struct hid_device *hdev) { }
  682. +
  683. +static int ithc_hid_parse(struct hid_device *hdev)
  684. +{
  685. + struct ithc *ithc = hdev->driver_data;
  686. + const struct ithc_data get_report_desc = { .type = ITHC_DATA_REPORT_DESCRIPTOR };
  687. + WRITE_ONCE(ithc->hid.parse_done, false);
  688. + for (int retries = 0; ; retries++) {
  689. + ithc_log_regs(ithc);
  690. + CHECK_RET(ithc_dma_tx, ithc, &get_report_desc);
  691. + if (wait_event_timeout(ithc->hid.wait_parse, READ_ONCE(ithc->hid.parse_done),
  692. + msecs_to_jiffies(200))) {
  693. + ithc_log_regs(ithc);
  694. + return 0;
  695. + }
  696. + if (retries > 5) {
  697. + ithc_log_regs(ithc);
  698. + pci_err(ithc->pci, "failed to read report descriptor\n");
  699. + return -ETIMEDOUT;
  700. + }
  701. + pci_warn(ithc->pci, "failed to read report descriptor, retrying\n");
  702. + }
  703. +}
  704. +
  705. +static int ithc_hid_raw_request(struct hid_device *hdev, unsigned char reportnum, __u8 *buf,
  706. + size_t len, unsigned char rtype, int reqtype)
  707. +{
  708. + struct ithc *ithc = hdev->driver_data;
  709. + if (!buf || !len)
  710. + return -EINVAL;
  711. +
  712. + struct ithc_data d = { .size = len, .data = buf };
  713. + buf[0] = reportnum;
  714. +
  715. + if (rtype == HID_OUTPUT_REPORT && reqtype == HID_REQ_SET_REPORT) {
  716. + d.type = ITHC_DATA_OUTPUT_REPORT;
  717. + CHECK_RET(ithc_dma_tx, ithc, &d);
  718. + return 0;
  719. + }
  720. +
  721. + if (rtype == HID_FEATURE_REPORT && reqtype == HID_REQ_SET_REPORT) {
  722. + d.type = ITHC_DATA_SET_FEATURE;
  723. + CHECK_RET(ithc_dma_tx, ithc, &d);
  724. + return 0;
  725. + }
  726. +
  727. + if (rtype == HID_FEATURE_REPORT && reqtype == HID_REQ_GET_REPORT) {
  728. + d.type = ITHC_DATA_GET_FEATURE;
  729. + d.data = &reportnum;
  730. + d.size = 1;
  731. +
  732. + // Prepare for response.
  733. + mutex_lock(&ithc->hid.get_feature_mutex);
  734. + ithc->hid.get_feature_buf = buf;
  735. + ithc->hid.get_feature_size = len;
  736. + mutex_unlock(&ithc->hid.get_feature_mutex);
  737. +
  738. + // Transmit 'get feature' request.
  739. + int r = CHECK(ithc_dma_tx, ithc, &d);
  740. + if (!r) {
  741. + r = wait_event_interruptible_timeout(ithc->hid.wait_get_feature,
  742. + !ithc->hid.get_feature_buf, msecs_to_jiffies(1000));
  743. + if (!r)
  744. + r = -ETIMEDOUT;
  745. + else if (r < 0)
  746. + r = -EINTR;
  747. + else
  748. + r = 0;
  749. + }
  750. +
  751. + // If everything went ok, the buffer has been filled with the response data.
  752. + // Return the response size.
  753. + mutex_lock(&ithc->hid.get_feature_mutex);
  754. + ithc->hid.get_feature_buf = NULL;
  755. + if (!r)
  756. + r = ithc->hid.get_feature_size;
  757. + mutex_unlock(&ithc->hid.get_feature_mutex);
  758. + return r;
  759. + }
  760. +
  761. + pci_err(ithc->pci, "unhandled hid request %i %i for report id %i\n",
  762. + rtype, reqtype, reportnum);
  763. + return -EINVAL;
  764. +}
  765. +
  766. +// FIXME hid_input_report()/hid_parse_report() currently don't take const buffers, so we have to
  767. +// cast away the const to avoid a compiler warning...
  768. +#define NOCONST(x) ((void *)x)
  769. +
  770. +void ithc_hid_process_data(struct ithc *ithc, struct ithc_data *d)
  771. +{
  772. + WARN_ON(!ithc->hid.dev);
  773. + if (!ithc->hid.dev)
  774. + return;
  775. +
  776. + switch (d->type) {
  777. +
  778. + case ITHC_DATA_IGNORE:
  779. + return;
  780. +
  781. + case ITHC_DATA_ERROR:
  782. + CHECK(ithc_reset, ithc);
  783. + return;
  784. +
  785. + case ITHC_DATA_REPORT_DESCRIPTOR:
  786. + // Response to the report descriptor request sent by ithc_hid_parse().
  787. + CHECK(hid_parse_report, ithc->hid.dev, NOCONST(d->data), d->size);
  788. + WRITE_ONCE(ithc->hid.parse_done, true);
  789. + wake_up(&ithc->hid.wait_parse);
  790. + return;
  791. +
  792. + case ITHC_DATA_INPUT_REPORT:
  793. + {
  794. + // Standard HID input report.
  795. + int r = hid_input_report(ithc->hid.dev, HID_INPUT_REPORT, NOCONST(d->data), d->size, 1);
  796. + if (r < 0) {
  797. + pci_warn(ithc->pci, "hid_input_report failed with %i (size %u, report ID 0x%02x)\n",
  798. + r, d->size, d->size ? *(u8 *)d->data : 0);
  799. + print_hex_dump_debug(DEVNAME " report: ", DUMP_PREFIX_OFFSET, 32, 1,
  800. + d->data, min(d->size, 0x400u), 0);
  801. + }
  802. + return;
  803. + }
  804. +
  805. + case ITHC_DATA_GET_FEATURE:
  806. + {
  807. + // Response to a 'get feature' request sent by ithc_hid_raw_request().
  808. + bool done = false;
  809. + mutex_lock(&ithc->hid.get_feature_mutex);
  810. + if (ithc->hid.get_feature_buf) {
  811. + if (d->size < ithc->hid.get_feature_size)
  812. + ithc->hid.get_feature_size = d->size;
  813. + memcpy(ithc->hid.get_feature_buf, d->data, ithc->hid.get_feature_size);
  814. + ithc->hid.get_feature_buf = NULL;
  815. + done = true;
  816. + }
  817. + mutex_unlock(&ithc->hid.get_feature_mutex);
  818. + if (done) {
  819. + wake_up(&ithc->hid.wait_get_feature);
  820. + } else {
  821. + // Received data without a matching request, or the request already
  822. + // timed out. (XXX What's the correct thing to do here?)
  823. + CHECK(hid_input_report, ithc->hid.dev, HID_FEATURE_REPORT,
  824. + NOCONST(d->data), d->size, 1);
  825. + }
  826. + return;
  827. + }
  828. +
  829. + default:
  830. + pci_err(ithc->pci, "unhandled data type %i\n", d->type);
  831. + return;
  832. + }
  833. +}
  834. +
  835. +static struct hid_ll_driver ithc_ll_driver = {
  836. + .start = ithc_hid_start,
  837. + .stop = ithc_hid_stop,
  838. + .open = ithc_hid_open,
  839. + .close = ithc_hid_close,
  840. + .parse = ithc_hid_parse,
  841. + .raw_request = ithc_hid_raw_request,
  842. +};
  843. +
  844. +static void ithc_hid_devres_release(struct device *dev, void *res)
  845. +{
  846. + struct hid_device **hidm = res;
  847. + if (*hidm)
  848. + hid_destroy_device(*hidm);
  849. +}
  850. +
  851. +int ithc_hid_init(struct ithc *ithc)
  852. +{
  853. + struct hid_device **hidm = devres_alloc(ithc_hid_devres_release, sizeof(*hidm), GFP_KERNEL);
  854. + if (!hidm)
  855. + return -ENOMEM;
  856. + devres_add(&ithc->pci->dev, hidm);
  857. + struct hid_device *hid = hid_allocate_device();
  858. + if (IS_ERR(hid))
  859. + return PTR_ERR(hid);
  860. + *hidm = hid;
  861. +
  862. + strscpy(hid->name, DEVFULLNAME, sizeof(hid->name));
  863. + strscpy(hid->phys, ithc->phys, sizeof(hid->phys));
  864. + hid->ll_driver = &ithc_ll_driver;
  865. + hid->bus = BUS_PCI;
  866. + hid->vendor = ithc->vendor_id;
  867. + hid->product = ithc->product_id;
  868. + hid->version = 0x100;
  869. + hid->dev.parent = &ithc->pci->dev;
  870. + hid->driver_data = ithc;
  871. +
  872. + ithc->hid.dev = hid;
  873. +
  874. + init_waitqueue_head(&ithc->hid.wait_parse);
  875. + init_waitqueue_head(&ithc->hid.wait_get_feature);
  876. + mutex_init(&ithc->hid.get_feature_mutex);
  877. +
  878. + return 0;
  879. +}
  880. +
  881. diff --git a/drivers/hid/ithc/ithc-hid.h b/drivers/hid/ithc/ithc-hid.h
  882. new file mode 100644
  883. index 000000000000..599eb912c8c8
  884. --- /dev/null
  885. +++ b/drivers/hid/ithc/ithc-hid.h
  886. @@ -0,0 +1,32 @@
  887. +/* SPDX-License-Identifier: GPL-2.0 OR BSD-2-Clause */
  888. +
  889. +enum ithc_data_type {
  890. + ITHC_DATA_IGNORE,
  891. + ITHC_DATA_RAW,
  892. + ITHC_DATA_ERROR,
  893. + ITHC_DATA_REPORT_DESCRIPTOR,
  894. + ITHC_DATA_INPUT_REPORT,
  895. + ITHC_DATA_OUTPUT_REPORT,
  896. + ITHC_DATA_GET_FEATURE,
  897. + ITHC_DATA_SET_FEATURE,
  898. +};
  899. +
  900. +struct ithc_data {
  901. + enum ithc_data_type type;
  902. + u32 size;
  903. + const void *data;
  904. +};
  905. +
  906. +struct ithc_hid {
  907. + struct hid_device *dev;
  908. + bool parse_done;
  909. + wait_queue_head_t wait_parse;
  910. + wait_queue_head_t wait_get_feature;
  911. + struct mutex get_feature_mutex;
  912. + void *get_feature_buf;
  913. + size_t get_feature_size;
  914. +};
  915. +
  916. +int ithc_hid_init(struct ithc *ithc);
  917. +void ithc_hid_process_data(struct ithc *ithc, struct ithc_data *d);
  918. +
  919. diff --git a/drivers/hid/ithc/ithc-legacy.c b/drivers/hid/ithc/ithc-legacy.c
  920. new file mode 100644
  921. index 000000000000..8883987fb352
  922. --- /dev/null
  923. +++ b/drivers/hid/ithc/ithc-legacy.c
  924. @@ -0,0 +1,254 @@
  925. +// SPDX-License-Identifier: GPL-2.0 OR BSD-2-Clause
  926. +
  927. +#include "ithc.h"
  928. +
  929. +#define DEVCFG_DMA_RX_SIZE(x) ((((x) & 0x3fff) + 1) << 6)
  930. +#define DEVCFG_DMA_TX_SIZE(x) (((((x) >> 14) & 0x3ff) + 1) << 6)
  931. +
  932. +#define DEVCFG_TOUCH_MASK 0x3f
  933. +#define DEVCFG_TOUCH_ENABLE BIT(0)
  934. +#define DEVCFG_TOUCH_PROP_DATA_ENABLE BIT(1)
  935. +#define DEVCFG_TOUCH_HID_REPORT_ENABLE BIT(2)
  936. +#define DEVCFG_TOUCH_POWER_STATE(x) (((x) & 7) << 3)
  937. +#define DEVCFG_TOUCH_UNKNOWN_6 BIT(6)
  938. +
  939. +#define DEVCFG_DEVICE_ID_TIC 0x43495424 // "$TIC"
  940. +
  941. +#define DEVCFG_SPI_CLKDIV(x) (((x) >> 1) & 7)
  942. +#define DEVCFG_SPI_CLKDIV_8 BIT(4)
  943. +#define DEVCFG_SPI_SUPPORTS_SINGLE BIT(5)
  944. +#define DEVCFG_SPI_SUPPORTS_DUAL BIT(6)
  945. +#define DEVCFG_SPI_SUPPORTS_QUAD BIT(7)
  946. +#define DEVCFG_SPI_MAX_TOUCH_POINTS(x) (((x) >> 8) & 0x3f)
  947. +#define DEVCFG_SPI_MIN_RESET_TIME(x) (((x) >> 16) & 0xf)
  948. +#define DEVCFG_SPI_NEEDS_HEARTBEAT BIT(20) // TODO implement heartbeat
  949. +#define DEVCFG_SPI_HEARTBEAT_INTERVAL(x) (((x) >> 21) & 7)
  950. +#define DEVCFG_SPI_UNKNOWN_25 BIT(25)
  951. +#define DEVCFG_SPI_UNKNOWN_26 BIT(26)
  952. +#define DEVCFG_SPI_UNKNOWN_27 BIT(27)
  953. +#define DEVCFG_SPI_DELAY(x) (((x) >> 28) & 7) // TODO use this
  954. +#define DEVCFG_SPI_USE_EXT_READ_CFG BIT(31) // TODO use this?
  955. +
  956. +struct ithc_device_config { // (Example values are from an SP7+.)
  957. + u32 irq_cause; // 00 = 0xe0000402 (0xe0000401 after DMA_RX_CODE_RESET)
  958. + u32 error; // 04 = 0x00000000
  959. + u32 dma_buf_sizes; // 08 = 0x000a00ff
  960. + u32 touch_cfg; // 0c = 0x0000001c
  961. + u32 touch_state; // 10 = 0x0000001c
  962. + u32 device_id; // 14 = 0x43495424 = "$TIC"
  963. + u32 spi_config; // 18 = 0xfda00a2e
  964. + u16 vendor_id; // 1c = 0x045e = Microsoft Corp.
  965. + u16 product_id; // 1e = 0x0c1a
  966. + u32 revision; // 20 = 0x00000001
  967. + u32 fw_version; // 24 = 0x05008a8b = 5.0.138.139 (this value looks more random on newer devices)
  968. + u32 command; // 28 = 0x00000000
  969. + u32 fw_mode; // 2c = 0x00000000 (for fw update?)
  970. + u32 _unknown_30; // 30 = 0x00000000
  971. + u8 eds_minor_ver; // 34 = 0x5e
  972. + u8 eds_major_ver; // 35 = 0x03
  973. + u8 interface_rev; // 36 = 0x04
  974. + u8 eu_kernel_ver; // 37 = 0x04
  975. + u32 _unknown_38; // 38 = 0x000001c0 (0x000001c1 after DMA_RX_CODE_RESET)
  976. + u32 _unknown_3c; // 3c = 0x00000002
  977. +};
  978. +static_assert(sizeof(struct ithc_device_config) == 64);
  979. +
  980. +#define RX_CODE_INPUT_REPORT 3
  981. +#define RX_CODE_FEATURE_REPORT 4
  982. +#define RX_CODE_REPORT_DESCRIPTOR 5
  983. +#define RX_CODE_RESET 7
  984. +
  985. +#define TX_CODE_SET_FEATURE 3
  986. +#define TX_CODE_GET_FEATURE 4
  987. +#define TX_CODE_OUTPUT_REPORT 5
  988. +#define TX_CODE_GET_REPORT_DESCRIPTOR 7
  989. +
  990. +static int ithc_set_device_enabled(struct ithc *ithc, bool enable)
  991. +{
  992. + u32 x = ithc->legacy_touch_cfg =
  993. + (ithc->legacy_touch_cfg & ~(u32)DEVCFG_TOUCH_MASK) |
  994. + DEVCFG_TOUCH_HID_REPORT_ENABLE |
  995. + (enable ? DEVCFG_TOUCH_ENABLE | DEVCFG_TOUCH_POWER_STATE(3) : 0);
  996. + return ithc_spi_command(ithc, SPI_CMD_CODE_WRITE,
  997. + offsetof(struct ithc_device_config, touch_cfg), sizeof(x), &x);
  998. +}
  999. +
  1000. +int ithc_legacy_init(struct ithc *ithc)
  1001. +{
  1002. + // Since we don't yet know which SPI config the device wants, use default speed and mode
  1003. + // initially for reading config data.
  1004. + CHECK(ithc_set_spi_config, ithc, 2, true, SPI_MODE_SINGLE, SPI_MODE_SINGLE);
  1005. +
  1006. + // Setting the following bit seems to make reading the config more reliable.
  1007. + bitsl_set(&ithc->regs->dma_rx[0].init_unknown, INIT_UNKNOWN_31);
  1008. +
  1009. + // Setting this bit may be necessary on ADL devices.
  1010. + switch (ithc->pci->device) {
  1011. + case PCI_DEVICE_ID_INTEL_THC_ADL_S_PORT1:
  1012. + case PCI_DEVICE_ID_INTEL_THC_ADL_S_PORT2:
  1013. + case PCI_DEVICE_ID_INTEL_THC_ADL_P_PORT1:
  1014. + case PCI_DEVICE_ID_INTEL_THC_ADL_P_PORT2:
  1015. + case PCI_DEVICE_ID_INTEL_THC_ADL_M_PORT1:
  1016. + case PCI_DEVICE_ID_INTEL_THC_ADL_M_PORT2:
  1017. + bitsl_set(&ithc->regs->dma_rx[0].init_unknown, INIT_UNKNOWN_5);
  1018. + break;
  1019. + }
  1020. +
  1021. + // Take the touch device out of reset.
  1022. + bitsl(&ithc->regs->control_bits, CONTROL_QUIESCE, 0);
  1023. + CHECK_RET(waitl, ithc, &ithc->regs->control_bits, CONTROL_IS_QUIESCED, 0);
  1024. + for (int retries = 0; ; retries++) {
  1025. + ithc_log_regs(ithc);
  1026. + bitsl_set(&ithc->regs->control_bits, CONTROL_NRESET);
  1027. + if (!waitl(ithc, &ithc->regs->irq_cause, 0xf, 2))
  1028. + break;
  1029. + if (retries > 5) {
  1030. + pci_err(ithc->pci, "failed to reset device, irq_cause = 0x%08x\n",
  1031. + readl(&ithc->regs->irq_cause));
  1032. + return -ETIMEDOUT;
  1033. + }
  1034. + pci_warn(ithc->pci, "invalid irq_cause, retrying reset\n");
  1035. + bitsl(&ithc->regs->control_bits, CONTROL_NRESET, 0);
  1036. + if (msleep_interruptible(1000))
  1037. + return -EINTR;
  1038. + }
  1039. + ithc_log_regs(ithc);
  1040. +
  1041. + CHECK(waitl, ithc, &ithc->regs->dma_rx[0].status, DMA_RX_STATUS_READY, DMA_RX_STATUS_READY);
  1042. +
  1043. + // Read configuration data.
  1044. + u32 spi_cfg;
  1045. + for (int retries = 0; ; retries++) {
  1046. + ithc_log_regs(ithc);
  1047. + struct ithc_device_config config = { 0 };
  1048. + CHECK_RET(ithc_spi_command, ithc, SPI_CMD_CODE_READ, 0, sizeof(config), &config);
  1049. + u32 *p = (void *)&config;
  1050. + pci_info(ithc->pci, "config: %08x %08x %08x %08x %08x %08x %08x %08x %08x %08x %08x %08x %08x %08x %08x %08x\n",
  1051. + p[0], p[1], p[2], p[3], p[4], p[5], p[6], p[7], p[8], p[9], p[10], p[11], p[12], p[13], p[14], p[15]);
  1052. + if (config.device_id == DEVCFG_DEVICE_ID_TIC) {
  1053. + spi_cfg = config.spi_config;
  1054. + ithc->vendor_id = config.vendor_id;
  1055. + ithc->product_id = config.product_id;
  1056. + ithc->product_rev = config.revision;
  1057. + ithc->max_rx_size = DEVCFG_DMA_RX_SIZE(config.dma_buf_sizes);
  1058. + ithc->max_tx_size = DEVCFG_DMA_TX_SIZE(config.dma_buf_sizes);
  1059. + ithc->legacy_touch_cfg = config.touch_cfg;
  1060. + ithc->have_config = true;
  1061. + break;
  1062. + }
  1063. + if (retries > 10) {
  1064. + pci_err(ithc->pci, "failed to read config, unknown device ID 0x%08x\n",
  1065. + config.device_id);
  1066. + return -EIO;
  1067. + }
  1068. + pci_warn(ithc->pci, "failed to read config, retrying\n");
  1069. + if (msleep_interruptible(100))
  1070. + return -EINTR;
  1071. + }
  1072. + ithc_log_regs(ithc);
  1073. +
  1074. + // Apply SPI config and enable touch device.
  1075. + CHECK_RET(ithc_set_spi_config, ithc,
  1076. + DEVCFG_SPI_CLKDIV(spi_cfg), (spi_cfg & DEVCFG_SPI_CLKDIV_8) != 0,
  1077. + spi_cfg & DEVCFG_SPI_SUPPORTS_QUAD ? SPI_MODE_QUAD :
  1078. + spi_cfg & DEVCFG_SPI_SUPPORTS_DUAL ? SPI_MODE_DUAL :
  1079. + SPI_MODE_SINGLE,
  1080. + SPI_MODE_SINGLE);
  1081. + CHECK_RET(ithc_set_device_enabled, ithc, true);
  1082. + ithc_log_regs(ithc);
  1083. + return 0;
  1084. +}
  1085. +
  1086. +void ithc_legacy_exit(struct ithc *ithc)
  1087. +{
  1088. + CHECK(ithc_set_device_enabled, ithc, false);
  1089. +}
  1090. +
  1091. +int ithc_legacy_decode_rx(struct ithc *ithc, const void *src, size_t len, struct ithc_data *dest)
  1092. +{
  1093. + const struct {
  1094. + u32 code;
  1095. + u32 data_size;
  1096. + u32 _unknown[14];
  1097. + } *hdr = src;
  1098. +
  1099. + if (len < sizeof(*hdr))
  1100. + return -ENODATA;
  1101. + // Note: RX data is not padded, even though TX data must be padded.
  1102. + if (len != sizeof(*hdr) + hdr->data_size)
  1103. + return -EMSGSIZE;
  1104. +
  1105. + dest->data = hdr + 1;
  1106. + dest->size = hdr->data_size;
  1107. +
  1108. + switch (hdr->code) {
  1109. + case RX_CODE_RESET:
  1110. + // The THC sends a reset request when we need to reinitialize the device.
  1111. + // This usually only happens if we send an invalid command or put the device
  1112. + // in a bad state.
  1113. + dest->type = ITHC_DATA_ERROR;
  1114. + return 0;
  1115. + case RX_CODE_REPORT_DESCRIPTOR:
  1116. + // The descriptor is preceded by 8 nul bytes.
  1117. + if (hdr->data_size < 8)
  1118. + return -ENODATA;
  1119. + dest->type = ITHC_DATA_REPORT_DESCRIPTOR;
  1120. + dest->data = (char *)(hdr + 1) + 8;
  1121. + dest->size = hdr->data_size - 8;
  1122. + return 0;
  1123. + case RX_CODE_INPUT_REPORT:
  1124. + dest->type = ITHC_DATA_INPUT_REPORT;
  1125. + return 0;
  1126. + case RX_CODE_FEATURE_REPORT:
  1127. + dest->type = ITHC_DATA_GET_FEATURE;
  1128. + return 0;
  1129. + default:
  1130. + return -EINVAL;
  1131. + }
  1132. +}
  1133. +
  1134. +ssize_t ithc_legacy_encode_tx(struct ithc *ithc, const struct ithc_data *src, void *dest,
  1135. + size_t maxlen)
  1136. +{
  1137. + struct {
  1138. + u32 code;
  1139. + u32 data_size;
  1140. + } *hdr = dest;
  1141. +
  1142. + size_t src_size = src->size;
  1143. + const void *src_data = src->data;
  1144. + const u64 get_report_desc_data = 0;
  1145. + u32 code;
  1146. +
  1147. + switch (src->type) {
  1148. + case ITHC_DATA_SET_FEATURE:
  1149. + code = TX_CODE_SET_FEATURE;
  1150. + break;
  1151. + case ITHC_DATA_GET_FEATURE:
  1152. + code = TX_CODE_GET_FEATURE;
  1153. + break;
  1154. + case ITHC_DATA_OUTPUT_REPORT:
  1155. + code = TX_CODE_OUTPUT_REPORT;
  1156. + break;
  1157. + case ITHC_DATA_REPORT_DESCRIPTOR:
  1158. + code = TX_CODE_GET_REPORT_DESCRIPTOR;
  1159. + src_size = sizeof(get_report_desc_data);
  1160. + src_data = &get_report_desc_data;
  1161. + break;
  1162. + default:
  1163. + return -EINVAL;
  1164. + }
  1165. +
  1166. + // Data must be padded to next 4-byte boundary.
  1167. + size_t padded = round_up(src_size, 4);
  1168. + if (sizeof(*hdr) + padded > maxlen)
  1169. + return -EOVERFLOW;
  1170. +
  1171. + // Fill the TX buffer with header and data.
  1172. + hdr->code = code;
  1173. + hdr->data_size = src_size;
  1174. + memcpy_and_pad(hdr + 1, padded, src_data, src_size, 0);
  1175. +
  1176. + return sizeof(*hdr) + padded;
  1177. +}
  1178. +
  1179. diff --git a/drivers/hid/ithc/ithc-legacy.h b/drivers/hid/ithc/ithc-legacy.h
  1180. new file mode 100644
  1181. index 000000000000..28d692462072
  1182. --- /dev/null
  1183. +++ b/drivers/hid/ithc/ithc-legacy.h
  1184. @@ -0,0 +1,8 @@
  1185. +/* SPDX-License-Identifier: GPL-2.0 OR BSD-2-Clause */
  1186. +
  1187. +int ithc_legacy_init(struct ithc *ithc);
  1188. +void ithc_legacy_exit(struct ithc *ithc);
  1189. +int ithc_legacy_decode_rx(struct ithc *ithc, const void *src, size_t len, struct ithc_data *dest);
  1190. +ssize_t ithc_legacy_encode_tx(struct ithc *ithc, const struct ithc_data *src, void *dest,
  1191. + size_t maxlen);
  1192. +
  1193. diff --git a/drivers/hid/ithc/ithc-main.c b/drivers/hid/ithc/ithc-main.c
  1194. new file mode 100644
  1195. index 000000000000..ac56c253674b
  1196. --- /dev/null
  1197. +++ b/drivers/hid/ithc/ithc-main.c
  1198. @@ -0,0 +1,431 @@
  1199. +// SPDX-License-Identifier: GPL-2.0 OR BSD-2-Clause
  1200. +
  1201. +#include "ithc.h"
  1202. +
  1203. +MODULE_DESCRIPTION("Intel Touch Host Controller driver");
  1204. +MODULE_LICENSE("Dual BSD/GPL");
  1205. +
  1206. +static const struct pci_device_id ithc_pci_tbl[] = {
  1207. + {
  1208. + .vendor = PCI_VENDOR_ID_INTEL,
  1209. + .device = PCI_ANY_ID,
  1210. + .subvendor = PCI_ANY_ID,
  1211. + .subdevice = PCI_ANY_ID,
  1212. + .class = PCI_CLASS_INPUT_PEN << 8,
  1213. + .class_mask = ~0,
  1214. + },
  1215. + {}
  1216. +};
  1217. +MODULE_DEVICE_TABLE(pci, ithc_pci_tbl);
  1218. +
  1219. +// Module parameters
  1220. +
  1221. +static bool ithc_use_polling = false;
  1222. +module_param_named(poll, ithc_use_polling, bool, 0);
  1223. +MODULE_PARM_DESC(poll, "Use polling instead of interrupts");
  1224. +
  1225. +// Since all known devices seem to use only channel 1, by default we disable channel 0.
  1226. +static bool ithc_use_rx0 = false;
  1227. +module_param_named(rx0, ithc_use_rx0, bool, 0);
  1228. +MODULE_PARM_DESC(rx0, "Use DMA RX channel 0");
  1229. +
  1230. +static bool ithc_use_rx1 = true;
  1231. +module_param_named(rx1, ithc_use_rx1, bool, 0);
  1232. +MODULE_PARM_DESC(rx1, "Use DMA RX channel 1");
  1233. +
  1234. +static int ithc_active_ltr_us = -1;
  1235. +module_param_named(activeltr, ithc_active_ltr_us, int, 0);
  1236. +MODULE_PARM_DESC(activeltr, "Active LTR value override (in microseconds)");
  1237. +
  1238. +static int ithc_idle_ltr_us = -1;
  1239. +module_param_named(idleltr, ithc_idle_ltr_us, int, 0);
  1240. +MODULE_PARM_DESC(idleltr, "Idle LTR value override (in microseconds)");
  1241. +
  1242. +static unsigned int ithc_idle_delay_ms = 1000;
  1243. +module_param_named(idledelay, ithc_idle_delay_ms, uint, 0);
  1244. +MODULE_PARM_DESC(idleltr, "Minimum idle time before applying idle LTR value (in milliseconds)");
  1245. +
  1246. +static bool ithc_log_regs_enabled = false;
  1247. +module_param_named(logregs, ithc_log_regs_enabled, bool, 0);
  1248. +MODULE_PARM_DESC(logregs, "Log changes in register values (for debugging)");
  1249. +
  1250. +// Interrupts/polling
  1251. +
  1252. +static void ithc_disable_interrupts(struct ithc *ithc)
  1253. +{
  1254. + writel(0, &ithc->regs->error_control);
  1255. + bitsb(&ithc->regs->spi_cmd.control, SPI_CMD_CONTROL_IRQ, 0);
  1256. + bitsb(&ithc->regs->dma_rx[0].control, DMA_RX_CONTROL_IRQ_UNKNOWN_1 | DMA_RX_CONTROL_IRQ_ERROR | DMA_RX_CONTROL_IRQ_READY | DMA_RX_CONTROL_IRQ_DATA, 0);
  1257. + bitsb(&ithc->regs->dma_rx[1].control, DMA_RX_CONTROL_IRQ_UNKNOWN_1 | DMA_RX_CONTROL_IRQ_ERROR | DMA_RX_CONTROL_IRQ_READY | DMA_RX_CONTROL_IRQ_DATA, 0);
  1258. + bitsb(&ithc->regs->dma_tx.control, DMA_TX_CONTROL_IRQ, 0);
  1259. +}
  1260. +
  1261. +static void ithc_clear_dma_rx_interrupts(struct ithc *ithc, unsigned int channel)
  1262. +{
  1263. + writel(DMA_RX_STATUS_ERROR | DMA_RX_STATUS_READY | DMA_RX_STATUS_HAVE_DATA,
  1264. + &ithc->regs->dma_rx[channel].status);
  1265. +}
  1266. +
  1267. +static void ithc_clear_interrupts(struct ithc *ithc)
  1268. +{
  1269. + writel(0xffffffff, &ithc->regs->error_flags);
  1270. + writel(ERROR_STATUS_DMA | ERROR_STATUS_SPI, &ithc->regs->error_status);
  1271. + writel(SPI_CMD_STATUS_DONE | SPI_CMD_STATUS_ERROR, &ithc->regs->spi_cmd.status);
  1272. + ithc_clear_dma_rx_interrupts(ithc, 0);
  1273. + ithc_clear_dma_rx_interrupts(ithc, 1);
  1274. + writel(DMA_TX_STATUS_DONE | DMA_TX_STATUS_ERROR | DMA_TX_STATUS_UNKNOWN_2,
  1275. + &ithc->regs->dma_tx.status);
  1276. +}
  1277. +
  1278. +static void ithc_idle_timer_callback(struct timer_list *t)
  1279. +{
  1280. + struct ithc *ithc = container_of(t, struct ithc, idle_timer);
  1281. + ithc_set_ltr_idle(ithc);
  1282. +}
  1283. +
  1284. +static void ithc_process(struct ithc *ithc)
  1285. +{
  1286. + ithc_log_regs(ithc);
  1287. +
  1288. + // The THC automatically transitions from LTR idle to active at the start of a DMA transfer.
  1289. + // It does not appear to automatically go back to idle, so we switch it back after a delay.
  1290. + mod_timer(&ithc->idle_timer, jiffies + msecs_to_jiffies(ithc_idle_delay_ms));
  1291. +
  1292. + bool rx0 = ithc_use_rx0 && (readl(&ithc->regs->dma_rx[0].status) & (DMA_RX_STATUS_ERROR | DMA_RX_STATUS_HAVE_DATA)) != 0;
  1293. + bool rx1 = ithc_use_rx1 && (readl(&ithc->regs->dma_rx[1].status) & (DMA_RX_STATUS_ERROR | DMA_RX_STATUS_HAVE_DATA)) != 0;
  1294. +
  1295. + // Read and clear error bits
  1296. + u32 err = readl(&ithc->regs->error_flags);
  1297. + if (err) {
  1298. + writel(err, &ithc->regs->error_flags);
  1299. + if (err & ~ERROR_FLAG_DMA_RX_TIMEOUT)
  1300. + pci_err(ithc->pci, "error flags: 0x%08x\n", err);
  1301. + if (err & ERROR_FLAG_DMA_RX_TIMEOUT)
  1302. + pci_err(ithc->pci, "DMA RX timeout/error (try decreasing activeltr/idleltr if this happens frequently)\n");
  1303. + }
  1304. +
  1305. + // Process DMA rx
  1306. + if (ithc_use_rx0) {
  1307. + ithc_clear_dma_rx_interrupts(ithc, 0);
  1308. + if (rx0)
  1309. + ithc_dma_rx(ithc, 0);
  1310. + }
  1311. + if (ithc_use_rx1) {
  1312. + ithc_clear_dma_rx_interrupts(ithc, 1);
  1313. + if (rx1)
  1314. + ithc_dma_rx(ithc, 1);
  1315. + }
  1316. +
  1317. + ithc_log_regs(ithc);
  1318. +}
  1319. +
  1320. +static irqreturn_t ithc_interrupt_thread(int irq, void *arg)
  1321. +{
  1322. + struct ithc *ithc = arg;
  1323. + pci_dbg(ithc->pci, "IRQ! err=%08x/%08x/%08x, cmd=%02x/%08x, rx0=%02x/%08x, rx1=%02x/%08x, tx=%02x/%08x\n",
  1324. + readl(&ithc->regs->error_control), readl(&ithc->regs->error_status), readl(&ithc->regs->error_flags),
  1325. + readb(&ithc->regs->spi_cmd.control), readl(&ithc->regs->spi_cmd.status),
  1326. + readb(&ithc->regs->dma_rx[0].control), readl(&ithc->regs->dma_rx[0].status),
  1327. + readb(&ithc->regs->dma_rx[1].control), readl(&ithc->regs->dma_rx[1].status),
  1328. + readb(&ithc->regs->dma_tx.control), readl(&ithc->regs->dma_tx.status));
  1329. + ithc_process(ithc);
  1330. + return IRQ_HANDLED;
  1331. +}
  1332. +
  1333. +static int ithc_poll_thread(void *arg)
  1334. +{
  1335. + struct ithc *ithc = arg;
  1336. + unsigned int sleep = 100;
  1337. + while (!kthread_should_stop()) {
  1338. + u32 n = ithc->dma_rx[1].num_received;
  1339. + ithc_process(ithc);
  1340. + // Decrease polling interval to 20ms if we received data, otherwise slowly
  1341. + // increase it up to 200ms.
  1342. + sleep = n != ithc->dma_rx[1].num_received ? 20
  1343. + : min(200u, sleep + (sleep >> 4) + 1);
  1344. + msleep_interruptible(sleep);
  1345. + }
  1346. + return 0;
  1347. +}
  1348. +
  1349. +// Device initialization and shutdown
  1350. +
  1351. +static void ithc_disable(struct ithc *ithc)
  1352. +{
  1353. + bitsl_set(&ithc->regs->control_bits, CONTROL_QUIESCE);
  1354. + CHECK(waitl, ithc, &ithc->regs->control_bits, CONTROL_IS_QUIESCED, CONTROL_IS_QUIESCED);
  1355. + bitsl(&ithc->regs->control_bits, CONTROL_NRESET, 0);
  1356. + bitsb(&ithc->regs->spi_cmd.control, SPI_CMD_CONTROL_SEND, 0);
  1357. + bitsb(&ithc->regs->dma_tx.control, DMA_TX_CONTROL_SEND, 0);
  1358. + bitsb(&ithc->regs->dma_rx[0].control, DMA_RX_CONTROL_ENABLE, 0);
  1359. + bitsb(&ithc->regs->dma_rx[1].control, DMA_RX_CONTROL_ENABLE, 0);
  1360. + ithc_disable_interrupts(ithc);
  1361. + ithc_clear_interrupts(ithc);
  1362. +}
  1363. +
  1364. +static int ithc_init_device(struct ithc *ithc)
  1365. +{
  1366. + // Read ACPI config for QuickSPI mode
  1367. + struct ithc_acpi_config cfg = { 0 };
  1368. + CHECK_RET(ithc_read_acpi_config, ithc, &cfg);
  1369. + if (!cfg.has_config)
  1370. + pci_info(ithc->pci, "no ACPI config, using legacy mode\n");
  1371. + else
  1372. + ithc_print_acpi_config(ithc, &cfg);
  1373. + ithc->use_quickspi = cfg.has_config;
  1374. +
  1375. + // Shut down device
  1376. + ithc_log_regs(ithc);
  1377. + bool was_enabled = (readl(&ithc->regs->control_bits) & CONTROL_NRESET) != 0;
  1378. + ithc_disable(ithc);
  1379. + CHECK_RET(waitl, ithc, &ithc->regs->control_bits, CONTROL_READY, CONTROL_READY);
  1380. + ithc_log_regs(ithc);
  1381. +
  1382. + // If the device was previously enabled, wait a bit to make sure it's fully shut down.
  1383. + if (was_enabled)
  1384. + if (msleep_interruptible(100))
  1385. + return -EINTR;
  1386. +
  1387. + // Set Latency Tolerance Reporting config. The device will automatically
  1388. + // apply these values depending on whether it is active or idle.
  1389. + // If active value is too high, DMA buffer data can become truncated.
  1390. + // By default, we set the active LTR value to 50us, and idle to 100ms.
  1391. + u64 active_ltr_ns = ithc_active_ltr_us >= 0 ? (u64)ithc_active_ltr_us * 1000
  1392. + : cfg.has_config && cfg.has_active_ltr ? (u64)cfg.active_ltr << 10
  1393. + : 50 * 1000;
  1394. + u64 idle_ltr_ns = ithc_idle_ltr_us >= 0 ? (u64)ithc_idle_ltr_us * 1000
  1395. + : cfg.has_config && cfg.has_idle_ltr ? (u64)cfg.idle_ltr << 10
  1396. + : 100 * 1000 * 1000;
  1397. + ithc_set_ltr_config(ithc, active_ltr_ns, idle_ltr_ns);
  1398. +
  1399. + if (ithc->use_quickspi)
  1400. + CHECK_RET(ithc_quickspi_init, ithc, &cfg);
  1401. + else
  1402. + CHECK_RET(ithc_legacy_init, ithc);
  1403. +
  1404. + return 0;
  1405. +}
  1406. +
  1407. +int ithc_reset(struct ithc *ithc)
  1408. +{
  1409. + // FIXME This should probably do devres_release_group()+ithc_start().
  1410. + // But because this is called during DMA processing, that would have to be done
  1411. + // asynchronously (schedule_work()?). And with extra locking?
  1412. + pci_err(ithc->pci, "reset\n");
  1413. + CHECK(ithc_init_device, ithc);
  1414. + if (ithc_use_rx0)
  1415. + ithc_dma_rx_enable(ithc, 0);
  1416. + if (ithc_use_rx1)
  1417. + ithc_dma_rx_enable(ithc, 1);
  1418. + ithc_log_regs(ithc);
  1419. + pci_dbg(ithc->pci, "reset completed\n");
  1420. + return 0;
  1421. +}
  1422. +
  1423. +static void ithc_stop(void *res)
  1424. +{
  1425. + struct ithc *ithc = res;
  1426. + pci_dbg(ithc->pci, "stopping\n");
  1427. + ithc_log_regs(ithc);
  1428. +
  1429. + if (ithc->poll_thread)
  1430. + CHECK(kthread_stop, ithc->poll_thread);
  1431. + if (ithc->irq >= 0)
  1432. + disable_irq(ithc->irq);
  1433. + if (ithc->use_quickspi)
  1434. + ithc_quickspi_exit(ithc);
  1435. + else
  1436. + ithc_legacy_exit(ithc);
  1437. + ithc_disable(ithc);
  1438. + del_timer_sync(&ithc->idle_timer);
  1439. +
  1440. + // Clear DMA config.
  1441. + for (unsigned int i = 0; i < 2; i++) {
  1442. + CHECK(waitl, ithc, &ithc->regs->dma_rx[i].status, DMA_RX_STATUS_ENABLED, 0);
  1443. + lo_hi_writeq(0, &ithc->regs->dma_rx[i].addr);
  1444. + writeb(0, &ithc->regs->dma_rx[i].num_bufs);
  1445. + writeb(0, &ithc->regs->dma_rx[i].num_prds);
  1446. + }
  1447. + lo_hi_writeq(0, &ithc->regs->dma_tx.addr);
  1448. + writeb(0, &ithc->regs->dma_tx.num_prds);
  1449. +
  1450. + ithc_log_regs(ithc);
  1451. + pci_dbg(ithc->pci, "stopped\n");
  1452. +}
  1453. +
  1454. +static void ithc_clear_drvdata(void *res)
  1455. +{
  1456. + struct pci_dev *pci = res;
  1457. + pci_set_drvdata(pci, NULL);
  1458. +}
  1459. +
  1460. +static int ithc_start(struct pci_dev *pci)
  1461. +{
  1462. + pci_dbg(pci, "starting\n");
  1463. + if (pci_get_drvdata(pci)) {
  1464. + pci_err(pci, "device already initialized\n");
  1465. + return -EINVAL;
  1466. + }
  1467. + if (!devres_open_group(&pci->dev, ithc_start, GFP_KERNEL))
  1468. + return -ENOMEM;
  1469. +
  1470. + // Allocate/init main driver struct.
  1471. + struct ithc *ithc = devm_kzalloc(&pci->dev, sizeof(*ithc), GFP_KERNEL);
  1472. + if (!ithc)
  1473. + return -ENOMEM;
  1474. + ithc->irq = -1;
  1475. + ithc->pci = pci;
  1476. + snprintf(ithc->phys, sizeof(ithc->phys), "pci-%s/" DEVNAME, pci_name(pci));
  1477. + pci_set_drvdata(pci, ithc);
  1478. + CHECK_RET(devm_add_action_or_reset, &pci->dev, ithc_clear_drvdata, pci);
  1479. + if (ithc_log_regs_enabled)
  1480. + ithc->prev_regs = devm_kzalloc(&pci->dev, sizeof(*ithc->prev_regs), GFP_KERNEL);
  1481. +
  1482. + // PCI initialization.
  1483. + CHECK_RET(pcim_enable_device, pci);
  1484. + pci_set_master(pci);
  1485. + CHECK_RET(pcim_iomap_regions, pci, BIT(0), DEVNAME " regs");
  1486. + CHECK_RET(dma_set_mask_and_coherent, &pci->dev, DMA_BIT_MASK(64));
  1487. + CHECK_RET(pci_set_power_state, pci, PCI_D0);
  1488. + ithc->regs = pcim_iomap_table(pci)[0];
  1489. +
  1490. + // Allocate IRQ.
  1491. + if (!ithc_use_polling) {
  1492. + CHECK_RET(pci_alloc_irq_vectors, pci, 1, 1, PCI_IRQ_MSI | PCI_IRQ_MSIX);
  1493. + ithc->irq = CHECK(pci_irq_vector, pci, 0);
  1494. + if (ithc->irq < 0)
  1495. + return ithc->irq;
  1496. + }
  1497. +
  1498. + // Initialize THC and touch device.
  1499. + CHECK_RET(ithc_init_device, ithc);
  1500. +
  1501. + // Initialize HID and DMA.
  1502. + CHECK_RET(ithc_hid_init, ithc);
  1503. + if (ithc_use_rx0)
  1504. + CHECK_RET(ithc_dma_rx_init, ithc, 0);
  1505. + if (ithc_use_rx1)
  1506. + CHECK_RET(ithc_dma_rx_init, ithc, 1);
  1507. + CHECK_RET(ithc_dma_tx_init, ithc);
  1508. +
  1509. + timer_setup(&ithc->idle_timer, ithc_idle_timer_callback, 0);
  1510. +
  1511. + // Add ithc_stop() callback AFTER setting up DMA buffers, so that polling/irqs/DMA are
  1512. + // disabled BEFORE the buffers are freed.
  1513. + CHECK_RET(devm_add_action_or_reset, &pci->dev, ithc_stop, ithc);
  1514. +
  1515. + // Start polling/IRQ.
  1516. + if (ithc_use_polling) {
  1517. + pci_info(pci, "using polling instead of irq\n");
  1518. + // Use a thread instead of simple timer because we want to be able to sleep.
  1519. + ithc->poll_thread = kthread_run(ithc_poll_thread, ithc, DEVNAME "poll");
  1520. + if (IS_ERR(ithc->poll_thread)) {
  1521. + int err = PTR_ERR(ithc->poll_thread);
  1522. + ithc->poll_thread = NULL;
  1523. + return err;
  1524. + }
  1525. + } else {
  1526. + CHECK_RET(devm_request_threaded_irq, &pci->dev, ithc->irq, NULL,
  1527. + ithc_interrupt_thread, IRQF_TRIGGER_HIGH | IRQF_ONESHOT, DEVNAME, ithc);
  1528. + }
  1529. +
  1530. + if (ithc_use_rx0)
  1531. + ithc_dma_rx_enable(ithc, 0);
  1532. + if (ithc_use_rx1)
  1533. + ithc_dma_rx_enable(ithc, 1);
  1534. +
  1535. + // hid_add_device() can only be called after irq/polling is started and DMA is enabled,
  1536. + // because it calls ithc_hid_parse() which reads the report descriptor via DMA.
  1537. + CHECK_RET(hid_add_device, ithc->hid.dev);
  1538. +
  1539. + CHECK(ithc_debug_init_device, ithc);
  1540. +
  1541. + ithc_set_ltr_idle(ithc);
  1542. +
  1543. + pci_dbg(pci, "started\n");
  1544. + return 0;
  1545. +}
  1546. +
  1547. +static int ithc_probe(struct pci_dev *pci, const struct pci_device_id *id)
  1548. +{
  1549. + pci_dbg(pci, "device probe\n");
  1550. + return ithc_start(pci);
  1551. +}
  1552. +
  1553. +static void ithc_remove(struct pci_dev *pci)
  1554. +{
  1555. + pci_dbg(pci, "device remove\n");
  1556. + // all cleanup is handled by devres
  1557. +}
  1558. +
  1559. +// For suspend/resume, we just deinitialize and reinitialize everything.
  1560. +// TODO It might be cleaner to keep the HID device around, however we would then have to signal
  1561. +// to userspace that the touch device has lost state and userspace needs to e.g. resend 'set
  1562. +// feature' requests. Hidraw does not seem to have a facility to do that.
  1563. +static int ithc_suspend(struct device *dev)
  1564. +{
  1565. + struct pci_dev *pci = to_pci_dev(dev);
  1566. + pci_dbg(pci, "pm suspend\n");
  1567. + devres_release_group(dev, ithc_start);
  1568. + return 0;
  1569. +}
  1570. +
  1571. +static int ithc_resume(struct device *dev)
  1572. +{
  1573. + struct pci_dev *pci = to_pci_dev(dev);
  1574. + pci_dbg(pci, "pm resume\n");
  1575. + return ithc_start(pci);
  1576. +}
  1577. +
  1578. +static int ithc_freeze(struct device *dev)
  1579. +{
  1580. + struct pci_dev *pci = to_pci_dev(dev);
  1581. + pci_dbg(pci, "pm freeze\n");
  1582. + devres_release_group(dev, ithc_start);
  1583. + return 0;
  1584. +}
  1585. +
  1586. +static int ithc_thaw(struct device *dev)
  1587. +{
  1588. + struct pci_dev *pci = to_pci_dev(dev);
  1589. + pci_dbg(pci, "pm thaw\n");
  1590. + return ithc_start(pci);
  1591. +}
  1592. +
  1593. +static int ithc_restore(struct device *dev)
  1594. +{
  1595. + struct pci_dev *pci = to_pci_dev(dev);
  1596. + pci_dbg(pci, "pm restore\n");
  1597. + return ithc_start(pci);
  1598. +}
  1599. +
  1600. +static struct pci_driver ithc_driver = {
  1601. + .name = DEVNAME,
  1602. + .id_table = ithc_pci_tbl,
  1603. + .probe = ithc_probe,
  1604. + .remove = ithc_remove,
  1605. + .driver.pm = &(const struct dev_pm_ops) {
  1606. + .suspend = ithc_suspend,
  1607. + .resume = ithc_resume,
  1608. + .freeze = ithc_freeze,
  1609. + .thaw = ithc_thaw,
  1610. + .restore = ithc_restore,
  1611. + },
  1612. + .driver.probe_type = PROBE_PREFER_ASYNCHRONOUS,
  1613. +};
  1614. +
  1615. +static int __init ithc_init(void)
  1616. +{
  1617. + ithc_debug_init_module();
  1618. + return pci_register_driver(&ithc_driver);
  1619. +}
  1620. +
  1621. +static void __exit ithc_exit(void)
  1622. +{
  1623. + pci_unregister_driver(&ithc_driver);
  1624. + ithc_debug_exit_module();
  1625. +}
  1626. +
  1627. +module_init(ithc_init);
  1628. +module_exit(ithc_exit);
  1629. +
  1630. diff --git a/drivers/hid/ithc/ithc-quickspi.c b/drivers/hid/ithc/ithc-quickspi.c
  1631. new file mode 100644
  1632. index 000000000000..e2d1690b8cf8
  1633. --- /dev/null
  1634. +++ b/drivers/hid/ithc/ithc-quickspi.c
  1635. @@ -0,0 +1,607 @@
  1636. +// SPDX-License-Identifier: GPL-2.0 OR BSD-2-Clause
  1637. +
  1638. +// Some public THC/QuickSPI documentation can be found in:
  1639. +// - Intel Firmware Support Package repo: https://github.com/intel/FSP
  1640. +// - HID over SPI (HIDSPI) spec: https://www.microsoft.com/en-us/download/details.aspx?id=103325
  1641. +
  1642. +#include "ithc.h"
  1643. +
  1644. +static const guid_t guid_hidspi =
  1645. + GUID_INIT(0x6e2ac436, 0x0fcf, 0x41af, 0xa2, 0x65, 0xb3, 0x2a, 0x22, 0x0d, 0xcf, 0xab);
  1646. +static const guid_t guid_thc_quickspi =
  1647. + GUID_INIT(0x300d35b7, 0xac20, 0x413e, 0x8e, 0x9c, 0x92, 0xe4, 0xda, 0xfd, 0x0a, 0xfe);
  1648. +static const guid_t guid_thc_ltr =
  1649. + GUID_INIT(0x84005682, 0x5b71, 0x41a4, 0x8d, 0x66, 0x81, 0x30, 0xf7, 0x87, 0xa1, 0x38);
  1650. +
  1651. +// TODO The HIDSPI spec says revision should be 3. Should we try both?
  1652. +#define DSM_REV 2
  1653. +
  1654. +struct hidspi_header {
  1655. + u8 type;
  1656. + u16 len;
  1657. + u8 id;
  1658. +} __packed;
  1659. +static_assert(sizeof(struct hidspi_header) == 4);
  1660. +
  1661. +#define HIDSPI_INPUT_TYPE_DATA 1
  1662. +#define HIDSPI_INPUT_TYPE_RESET_RESPONSE 3
  1663. +#define HIDSPI_INPUT_TYPE_COMMAND_RESPONSE 4
  1664. +#define HIDSPI_INPUT_TYPE_GET_FEATURE_RESPONSE 5
  1665. +#define HIDSPI_INPUT_TYPE_DEVICE_DESCRIPTOR 7
  1666. +#define HIDSPI_INPUT_TYPE_REPORT_DESCRIPTOR 8
  1667. +#define HIDSPI_INPUT_TYPE_SET_FEATURE_RESPONSE 9
  1668. +#define HIDSPI_INPUT_TYPE_OUTPUT_REPORT_RESPONSE 10
  1669. +#define HIDSPI_INPUT_TYPE_GET_INPUT_REPORT_RESPONSE 11
  1670. +
  1671. +#define HIDSPI_OUTPUT_TYPE_DEVICE_DESCRIPTOR_REQUEST 1
  1672. +#define HIDSPI_OUTPUT_TYPE_REPORT_DESCRIPTOR_REQUEST 2
  1673. +#define HIDSPI_OUTPUT_TYPE_SET_FEATURE 3
  1674. +#define HIDSPI_OUTPUT_TYPE_GET_FEATURE 4
  1675. +#define HIDSPI_OUTPUT_TYPE_OUTPUT_REPORT 5
  1676. +#define HIDSPI_OUTPUT_TYPE_INPUT_REPORT_REQUEST 6
  1677. +#define HIDSPI_OUTPUT_TYPE_COMMAND 7
  1678. +
  1679. +struct hidspi_device_descriptor {
  1680. + u16 wDeviceDescLength;
  1681. + u16 bcdVersion;
  1682. + u16 wReportDescLength;
  1683. + u16 wMaxInputLength;
  1684. + u16 wMaxOutputLength;
  1685. + u16 wMaxFragmentLength;
  1686. + u16 wVendorID;
  1687. + u16 wProductID;
  1688. + u16 wVersionID;
  1689. + u16 wFlags;
  1690. + u32 dwReserved;
  1691. +};
  1692. +static_assert(sizeof(struct hidspi_device_descriptor) == 24);
  1693. +
  1694. +static int read_acpi_u32(struct ithc *ithc, const guid_t *guid, u32 func, u32 *dest)
  1695. +{
  1696. + acpi_handle handle = ACPI_HANDLE(&ithc->pci->dev);
  1697. + union acpi_object *o = acpi_evaluate_dsm(handle, guid, DSM_REV, func, NULL);
  1698. + if (!o)
  1699. + return 0;
  1700. + if (o->type != ACPI_TYPE_INTEGER) {
  1701. + pci_err(ithc->pci, "DSM %pUl %u returned type %i instead of integer\n",
  1702. + guid, func, o->type);
  1703. + ACPI_FREE(o);
  1704. + return -1;
  1705. + }
  1706. + pci_dbg(ithc->pci, "DSM %pUl %u = 0x%08x\n", guid, func, (u32)o->integer.value);
  1707. + *dest = (u32)o->integer.value;
  1708. + ACPI_FREE(o);
  1709. + return 1;
  1710. +}
  1711. +
  1712. +static int read_acpi_buf(struct ithc *ithc, const guid_t *guid, u32 func, size_t len, u8 *dest)
  1713. +{
  1714. + acpi_handle handle = ACPI_HANDLE(&ithc->pci->dev);
  1715. + union acpi_object *o = acpi_evaluate_dsm(handle, guid, DSM_REV, func, NULL);
  1716. + if (!o)
  1717. + return 0;
  1718. + if (o->type != ACPI_TYPE_BUFFER) {
  1719. + pci_err(ithc->pci, "DSM %pUl %u returned type %i instead of buffer\n",
  1720. + guid, func, o->type);
  1721. + ACPI_FREE(o);
  1722. + return -1;
  1723. + }
  1724. + if (o->buffer.length != len) {
  1725. + pci_err(ithc->pci, "DSM %pUl %u returned len %u instead of %zu\n",
  1726. + guid, func, o->buffer.length, len);
  1727. + ACPI_FREE(o);
  1728. + return -1;
  1729. + }
  1730. + memcpy(dest, o->buffer.pointer, len);
  1731. + pci_dbg(ithc->pci, "DSM %pUl %u = 0x%02x\n", guid, func, dest[0]);
  1732. + ACPI_FREE(o);
  1733. + return 1;
  1734. +}
  1735. +
  1736. +int ithc_read_acpi_config(struct ithc *ithc, struct ithc_acpi_config *cfg)
  1737. +{
  1738. + int r;
  1739. + acpi_handle handle = ACPI_HANDLE(&ithc->pci->dev);
  1740. +
  1741. + cfg->has_config = acpi_check_dsm(handle, &guid_hidspi, DSM_REV, BIT(0));
  1742. + if (!cfg->has_config)
  1743. + return 0;
  1744. +
  1745. + // HIDSPI settings
  1746. +
  1747. + r = read_acpi_u32(ithc, &guid_hidspi, 1, &cfg->input_report_header_address);
  1748. + if (r < 0)
  1749. + return r;
  1750. + cfg->has_input_report_header_address = r > 0;
  1751. + if (r > 0 && cfg->input_report_header_address > 0xffffff) {
  1752. + pci_err(ithc->pci, "Invalid input report header address 0x%x\n",
  1753. + cfg->input_report_header_address);
  1754. + return -1;
  1755. + }
  1756. +
  1757. + r = read_acpi_u32(ithc, &guid_hidspi, 2, &cfg->input_report_body_address);
  1758. + if (r < 0)
  1759. + return r;
  1760. + cfg->has_input_report_body_address = r > 0;
  1761. + if (r > 0 && cfg->input_report_body_address > 0xffffff) {
  1762. + pci_err(ithc->pci, "Invalid input report body address 0x%x\n",
  1763. + cfg->input_report_body_address);
  1764. + return -1;
  1765. + }
  1766. +
  1767. + r = read_acpi_u32(ithc, &guid_hidspi, 3, &cfg->output_report_body_address);
  1768. + if (r < 0)
  1769. + return r;
  1770. + cfg->has_output_report_body_address = r > 0;
  1771. + if (r > 0 && cfg->output_report_body_address > 0xffffff) {
  1772. + pci_err(ithc->pci, "Invalid output report body address 0x%x\n",
  1773. + cfg->output_report_body_address);
  1774. + return -1;
  1775. + }
  1776. +
  1777. + r = read_acpi_buf(ithc, &guid_hidspi, 4, sizeof(cfg->read_opcode), &cfg->read_opcode);
  1778. + if (r < 0)
  1779. + return r;
  1780. + cfg->has_read_opcode = r > 0;
  1781. +
  1782. + r = read_acpi_buf(ithc, &guid_hidspi, 5, sizeof(cfg->write_opcode), &cfg->write_opcode);
  1783. + if (r < 0)
  1784. + return r;
  1785. + cfg->has_write_opcode = r > 0;
  1786. +
  1787. + u32 flags;
  1788. + r = read_acpi_u32(ithc, &guid_hidspi, 6, &flags);
  1789. + if (r < 0)
  1790. + return r;
  1791. + cfg->has_read_mode = cfg->has_write_mode = r > 0;
  1792. + if (r > 0) {
  1793. + cfg->read_mode = (flags >> 14) & 3;
  1794. + cfg->write_mode = flags & BIT(13) ? cfg->read_mode : SPI_MODE_SINGLE;
  1795. + }
  1796. +
  1797. + // Quick SPI settings
  1798. +
  1799. + r = read_acpi_u32(ithc, &guid_thc_quickspi, 1, &cfg->spi_frequency);
  1800. + if (r < 0)
  1801. + return r;
  1802. + cfg->has_spi_frequency = r > 0;
  1803. +
  1804. + r = read_acpi_u32(ithc, &guid_thc_quickspi, 2, &cfg->limit_packet_size);
  1805. + if (r < 0)
  1806. + return r;
  1807. + cfg->has_limit_packet_size = r > 0;
  1808. +
  1809. + r = read_acpi_u32(ithc, &guid_thc_quickspi, 3, &cfg->tx_delay);
  1810. + if (r < 0)
  1811. + return r;
  1812. + cfg->has_tx_delay = r > 0;
  1813. + if (r > 0)
  1814. + cfg->tx_delay &= 0xffff;
  1815. +
  1816. + // LTR settings
  1817. +
  1818. + r = read_acpi_u32(ithc, &guid_thc_ltr, 1, &cfg->active_ltr);
  1819. + if (r < 0)
  1820. + return r;
  1821. + cfg->has_active_ltr = r > 0;
  1822. + if (r > 0 && (!cfg->active_ltr || cfg->active_ltr > 0x3ff)) {
  1823. + if (cfg->active_ltr != 0xffffffff)
  1824. + pci_warn(ithc->pci, "Ignoring invalid active LTR value 0x%x\n",
  1825. + cfg->active_ltr);
  1826. + cfg->active_ltr = 500;
  1827. + }
  1828. +
  1829. + r = read_acpi_u32(ithc, &guid_thc_ltr, 2, &cfg->idle_ltr);
  1830. + if (r < 0)
  1831. + return r;
  1832. + cfg->has_idle_ltr = r > 0;
  1833. + if (r > 0 && (!cfg->idle_ltr || cfg->idle_ltr > 0x3ff)) {
  1834. + if (cfg->idle_ltr != 0xffffffff)
  1835. + pci_warn(ithc->pci, "Ignoring invalid idle LTR value 0x%x\n",
  1836. + cfg->idle_ltr);
  1837. + cfg->idle_ltr = 500;
  1838. + if (cfg->has_active_ltr && cfg->active_ltr > cfg->idle_ltr)
  1839. + cfg->idle_ltr = cfg->active_ltr;
  1840. + }
  1841. +
  1842. + return 0;
  1843. +}
  1844. +
  1845. +void ithc_print_acpi_config(struct ithc *ithc, const struct ithc_acpi_config *cfg)
  1846. +{
  1847. + if (!cfg->has_config) {
  1848. + pci_info(ithc->pci, "No ACPI config");
  1849. + return;
  1850. + }
  1851. +
  1852. + char input_report_header_address[16] = "-";
  1853. + if (cfg->has_input_report_header_address)
  1854. + sprintf(input_report_header_address, "0x%x", cfg->input_report_header_address);
  1855. + char input_report_body_address[16] = "-";
  1856. + if (cfg->has_input_report_body_address)
  1857. + sprintf(input_report_body_address, "0x%x", cfg->input_report_body_address);
  1858. + char output_report_body_address[16] = "-";
  1859. + if (cfg->has_output_report_body_address)
  1860. + sprintf(output_report_body_address, "0x%x", cfg->output_report_body_address);
  1861. + char read_opcode[16] = "-";
  1862. + if (cfg->has_read_opcode)
  1863. + sprintf(read_opcode, "0x%02x", cfg->read_opcode);
  1864. + char write_opcode[16] = "-";
  1865. + if (cfg->has_write_opcode)
  1866. + sprintf(write_opcode, "0x%02x", cfg->write_opcode);
  1867. + char read_mode[16] = "-";
  1868. + if (cfg->has_read_mode)
  1869. + sprintf(read_mode, "%i", cfg->read_mode);
  1870. + char write_mode[16] = "-";
  1871. + if (cfg->has_write_mode)
  1872. + sprintf(write_mode, "%i", cfg->write_mode);
  1873. + char spi_frequency[16] = "-";
  1874. + if (cfg->has_spi_frequency)
  1875. + sprintf(spi_frequency, "%u", cfg->spi_frequency);
  1876. + char limit_packet_size[16] = "-";
  1877. + if (cfg->has_limit_packet_size)
  1878. + sprintf(limit_packet_size, "%u", cfg->limit_packet_size);
  1879. + char tx_delay[16] = "-";
  1880. + if (cfg->has_tx_delay)
  1881. + sprintf(tx_delay, "%u", cfg->tx_delay);
  1882. + char active_ltr[16] = "-";
  1883. + if (cfg->has_active_ltr)
  1884. + sprintf(active_ltr, "%u", cfg->active_ltr);
  1885. + char idle_ltr[16] = "-";
  1886. + if (cfg->has_idle_ltr)
  1887. + sprintf(idle_ltr, "%u", cfg->idle_ltr);
  1888. +
  1889. + pci_info(ithc->pci, "ACPI config: InputHeaderAddr=%s InputBodyAddr=%s OutputBodyAddr=%s ReadOpcode=%s WriteOpcode=%s ReadMode=%s WriteMode=%s Frequency=%s LimitPacketSize=%s TxDelay=%s ActiveLTR=%s IdleLTR=%s\n",
  1890. + input_report_header_address, input_report_body_address, output_report_body_address,
  1891. + read_opcode, write_opcode, read_mode, write_mode,
  1892. + spi_frequency, limit_packet_size, tx_delay, active_ltr, idle_ltr);
  1893. +}
  1894. +
  1895. +static void set_opcode(struct ithc *ithc, size_t i, u8 opcode)
  1896. +{
  1897. + writeb(opcode, &ithc->regs->opcode[i].header);
  1898. + writeb(opcode, &ithc->regs->opcode[i].single);
  1899. + writeb(opcode, &ithc->regs->opcode[i].dual);
  1900. + writeb(opcode, &ithc->regs->opcode[i].quad);
  1901. +}
  1902. +
  1903. +static int ithc_quickspi_init_regs(struct ithc *ithc, const struct ithc_acpi_config *cfg)
  1904. +{
  1905. + pci_dbg(ithc->pci, "initializing QuickSPI registers\n");
  1906. +
  1907. + // SPI frequency and mode
  1908. + if (!cfg->has_spi_frequency || !cfg->spi_frequency) {
  1909. + pci_err(ithc->pci, "Missing SPI frequency in configuration\n");
  1910. + return -EINVAL;
  1911. + }
  1912. + unsigned int clkdiv = DIV_ROUND_UP(SPI_CLK_FREQ_BASE, cfg->spi_frequency);
  1913. + bool clkdiv8 = clkdiv > 7;
  1914. + if (clkdiv8)
  1915. + clkdiv = min(7u, DIV_ROUND_UP(clkdiv, 8u));
  1916. + if (!clkdiv)
  1917. + clkdiv = 1;
  1918. + CHECK_RET(ithc_set_spi_config, ithc, clkdiv, clkdiv8,
  1919. + cfg->has_read_mode ? cfg->read_mode : SPI_MODE_SINGLE,
  1920. + cfg->has_write_mode ? cfg->write_mode : SPI_MODE_SINGLE);
  1921. +
  1922. + // SPI addresses and opcodes
  1923. + if (cfg->has_input_report_header_address)
  1924. + writel(cfg->input_report_header_address, &ithc->regs->spi_header_addr);
  1925. + if (cfg->has_input_report_body_address) {
  1926. + writel(cfg->input_report_body_address, &ithc->regs->dma_rx[0].spi_addr);
  1927. + writel(cfg->input_report_body_address, &ithc->regs->dma_rx[1].spi_addr);
  1928. + }
  1929. + if (cfg->has_output_report_body_address)
  1930. + writel(cfg->output_report_body_address, &ithc->regs->dma_tx.spi_addr);
  1931. +
  1932. + switch (ithc->pci->device) {
  1933. + // LKF/TGL don't support QuickSPI.
  1934. + // For ADL, opcode layout is RX/TX/unused.
  1935. + case PCI_DEVICE_ID_INTEL_THC_ADL_S_PORT1:
  1936. + case PCI_DEVICE_ID_INTEL_THC_ADL_S_PORT2:
  1937. + case PCI_DEVICE_ID_INTEL_THC_ADL_P_PORT1:
  1938. + case PCI_DEVICE_ID_INTEL_THC_ADL_P_PORT2:
  1939. + case PCI_DEVICE_ID_INTEL_THC_ADL_M_PORT1:
  1940. + case PCI_DEVICE_ID_INTEL_THC_ADL_M_PORT2:
  1941. + if (cfg->has_read_opcode) {
  1942. + set_opcode(ithc, 0, cfg->read_opcode);
  1943. + }
  1944. + if (cfg->has_write_opcode) {
  1945. + set_opcode(ithc, 1, cfg->write_opcode);
  1946. + }
  1947. + break;
  1948. + // For MTL, opcode layout was changed to RX/RX/TX.
  1949. + // (RPL layout is unknown.)
  1950. + default:
  1951. + if (cfg->has_read_opcode) {
  1952. + set_opcode(ithc, 0, cfg->read_opcode);
  1953. + set_opcode(ithc, 1, cfg->read_opcode);
  1954. + }
  1955. + if (cfg->has_write_opcode) {
  1956. + set_opcode(ithc, 2, cfg->write_opcode);
  1957. + }
  1958. + break;
  1959. + }
  1960. +
  1961. + ithc_log_regs(ithc);
  1962. +
  1963. + // The rest...
  1964. + bitsl_set(&ithc->regs->dma_rx[0].init_unknown, INIT_UNKNOWN_31);
  1965. +
  1966. + bitsl(&ithc->regs->quickspi_config1,
  1967. + QUICKSPI_CONFIG1_UNKNOWN_0(0xff) | QUICKSPI_CONFIG1_UNKNOWN_5(0xff) |
  1968. + QUICKSPI_CONFIG1_UNKNOWN_10(0xff) | QUICKSPI_CONFIG1_UNKNOWN_16(0xffff),
  1969. + QUICKSPI_CONFIG1_UNKNOWN_0(4) | QUICKSPI_CONFIG1_UNKNOWN_5(4) |
  1970. + QUICKSPI_CONFIG1_UNKNOWN_10(22) | QUICKSPI_CONFIG1_UNKNOWN_16(2));
  1971. +
  1972. + bitsl(&ithc->regs->quickspi_config2,
  1973. + QUICKSPI_CONFIG2_UNKNOWN_0(0xff) | QUICKSPI_CONFIG2_UNKNOWN_5(0xff) |
  1974. + QUICKSPI_CONFIG2_UNKNOWN_12(0xff),
  1975. + QUICKSPI_CONFIG2_UNKNOWN_0(8) | QUICKSPI_CONFIG2_UNKNOWN_5(14) |
  1976. + QUICKSPI_CONFIG2_UNKNOWN_12(2));
  1977. +
  1978. + u32 pktsize = cfg->has_limit_packet_size && cfg->limit_packet_size == 1 ? 4 : 0x80;
  1979. + bitsl(&ithc->regs->spi_config,
  1980. + SPI_CONFIG_READ_PACKET_SIZE(0xfff) | SPI_CONFIG_WRITE_PACKET_SIZE(0xfff),
  1981. + SPI_CONFIG_READ_PACKET_SIZE(pktsize) | SPI_CONFIG_WRITE_PACKET_SIZE(pktsize));
  1982. +
  1983. + bitsl_set(&ithc->regs->quickspi_config2,
  1984. + QUICKSPI_CONFIG2_UNKNOWN_16 | QUICKSPI_CONFIG2_UNKNOWN_17);
  1985. + bitsl(&ithc->regs->quickspi_config2,
  1986. + QUICKSPI_CONFIG2_DISABLE_READ_ADDRESS_INCREMENT |
  1987. + QUICKSPI_CONFIG2_DISABLE_WRITE_ADDRESS_INCREMENT |
  1988. + QUICKSPI_CONFIG2_ENABLE_WRITE_STREAMING_MODE, 0);
  1989. +
  1990. + return 0;
  1991. +}
  1992. +
  1993. +static int wait_for_report(struct ithc *ithc)
  1994. +{
  1995. + CHECK_RET(waitl, ithc, &ithc->regs->dma_rx[0].status,
  1996. + DMA_RX_STATUS_READY, DMA_RX_STATUS_READY);
  1997. + writel(DMA_RX_STATUS_READY, &ithc->regs->dma_rx[0].status);
  1998. +
  1999. + u32 h = readl(&ithc->regs->input_header);
  2000. + ithc_log_regs(ithc);
  2001. + if (INPUT_HEADER_SYNC(h) != INPUT_HEADER_SYNC_VALUE
  2002. + || INPUT_HEADER_VERSION(h) != INPUT_HEADER_VERSION_VALUE) {
  2003. + pci_err(ithc->pci, "invalid input report frame header 0x%08x\n", h);
  2004. + return -ENODATA;
  2005. + }
  2006. + return INPUT_HEADER_REPORT_LENGTH(h) * 4;
  2007. +}
  2008. +
  2009. +static int ithc_quickspi_init_hidspi(struct ithc *ithc, const struct ithc_acpi_config *cfg)
  2010. +{
  2011. + pci_dbg(ithc->pci, "initializing HIDSPI\n");
  2012. +
  2013. + // HIDSPI initialization sequence:
  2014. + // "1. The host shall invoke the ACPI reset method to clear the device state."
  2015. + acpi_status s = acpi_evaluate_object(ACPI_HANDLE(&ithc->pci->dev), "_RST", NULL, NULL);
  2016. + if (ACPI_FAILURE(s)) {
  2017. + pci_err(ithc->pci, "ACPI reset failed\n");
  2018. + return -EIO;
  2019. + }
  2020. +
  2021. + bitsl(&ithc->regs->control_bits, CONTROL_QUIESCE, 0);
  2022. +
  2023. + // "2. Within 1 second, the device shall signal an interrupt and make available to the host
  2024. + // an input report containing a device reset response."
  2025. + int size = wait_for_report(ithc);
  2026. + if (size < 0)
  2027. + return size;
  2028. + if (size < sizeof(struct hidspi_header)) {
  2029. + pci_err(ithc->pci, "SPI data size too small for reset response (%u)\n", size);
  2030. + return -EMSGSIZE;
  2031. + }
  2032. +
  2033. + // "3. The host shall read the reset response from the device at the Input Report addresses
  2034. + // specified in ACPI."
  2035. + u32 in_addr = cfg->has_input_report_body_address ? cfg->input_report_body_address : 0x1000;
  2036. + struct {
  2037. + struct hidspi_header header;
  2038. + union {
  2039. + struct hidspi_device_descriptor device_desc;
  2040. + u32 data[16];
  2041. + };
  2042. + } resp = { 0 };
  2043. + if (size > sizeof(resp)) {
  2044. + pci_err(ithc->pci, "SPI data size for reset response too big (%u)\n", size);
  2045. + return -EMSGSIZE;
  2046. + }
  2047. + CHECK_RET(ithc_spi_command, ithc, SPI_CMD_CODE_READ, in_addr, size, &resp);
  2048. + if (resp.header.type != HIDSPI_INPUT_TYPE_RESET_RESPONSE) {
  2049. + pci_err(ithc->pci, "received type %i instead of reset response\n", resp.header.type);
  2050. + return -ENOMSG;
  2051. + }
  2052. +
  2053. + // "4. The host shall then write an Output Report to the device at the Output Report Address
  2054. + // specified in ACPI, requesting the Device Descriptor from the device."
  2055. + u32 out_addr = cfg->has_output_report_body_address ? cfg->output_report_body_address : 0x1000;
  2056. + struct hidspi_header req = { .type = HIDSPI_OUTPUT_TYPE_DEVICE_DESCRIPTOR_REQUEST };
  2057. + CHECK_RET(ithc_spi_command, ithc, SPI_CMD_CODE_WRITE, out_addr, sizeof(req), &req);
  2058. +
  2059. + // "5. Within 1 second, the device shall signal an interrupt and make available to the host
  2060. + // an input report containing the Device Descriptor."
  2061. + size = wait_for_report(ithc);
  2062. + if (size < 0)
  2063. + return size;
  2064. + if (size < sizeof(resp.header) + sizeof(resp.device_desc)) {
  2065. + pci_err(ithc->pci, "SPI data size too small for device descriptor (%u)\n", size);
  2066. + return -EMSGSIZE;
  2067. + }
  2068. +
  2069. + // "6. The host shall read the Device Descriptor from the Input Report addresses specified
  2070. + // in ACPI."
  2071. + if (size > sizeof(resp)) {
  2072. + pci_err(ithc->pci, "SPI data size for device descriptor too big (%u)\n", size);
  2073. + return -EMSGSIZE;
  2074. + }
  2075. + memset(&resp, 0, sizeof(resp));
  2076. + CHECK_RET(ithc_spi_command, ithc, SPI_CMD_CODE_READ, in_addr, size, &resp);
  2077. + if (resp.header.type != HIDSPI_INPUT_TYPE_DEVICE_DESCRIPTOR) {
  2078. + pci_err(ithc->pci, "received type %i instead of device descriptor\n",
  2079. + resp.header.type);
  2080. + return -ENOMSG;
  2081. + }
  2082. + struct hidspi_device_descriptor *d = &resp.device_desc;
  2083. + if (resp.header.len < sizeof(*d)) {
  2084. + pci_err(ithc->pci, "response too small for device descriptor (%u)\n",
  2085. + resp.header.len);
  2086. + return -EMSGSIZE;
  2087. + }
  2088. + if (d->wDeviceDescLength != sizeof(*d)) {
  2089. + pci_err(ithc->pci, "invalid device descriptor length (%u)\n",
  2090. + d->wDeviceDescLength);
  2091. + return -EMSGSIZE;
  2092. + }
  2093. +
  2094. + pci_info(ithc->pci, "Device descriptor: bcdVersion=0x%04x wReportDescLength=%u wMaxInputLength=%u wMaxOutputLength=%u wMaxFragmentLength=%u wVendorID=0x%04x wProductID=0x%04x wVersionID=0x%04x wFlags=0x%04x dwReserved=0x%08x\n",
  2095. + d->bcdVersion, d->wReportDescLength,
  2096. + d->wMaxInputLength, d->wMaxOutputLength, d->wMaxFragmentLength,
  2097. + d->wVendorID, d->wProductID, d->wVersionID,
  2098. + d->wFlags, d->dwReserved);
  2099. +
  2100. + ithc->vendor_id = d->wVendorID;
  2101. + ithc->product_id = d->wProductID;
  2102. + ithc->product_rev = d->wVersionID;
  2103. + ithc->max_rx_size = max_t(u32, d->wMaxInputLength,
  2104. + d->wReportDescLength + sizeof(struct hidspi_header));
  2105. + ithc->max_tx_size = d->wMaxOutputLength;
  2106. + ithc->have_config = true;
  2107. +
  2108. + // "7. The device and host shall then enter their "Ready" states - where the device may
  2109. + // begin sending Input Reports, and the device shall be prepared for Output Reports from
  2110. + // the host."
  2111. +
  2112. + return 0;
  2113. +}
  2114. +
  2115. +int ithc_quickspi_init(struct ithc *ithc, const struct ithc_acpi_config *cfg)
  2116. +{
  2117. + bitsl_set(&ithc->regs->control_bits, CONTROL_QUIESCE);
  2118. + CHECK_RET(waitl, ithc, &ithc->regs->control_bits, CONTROL_IS_QUIESCED, CONTROL_IS_QUIESCED);
  2119. +
  2120. + ithc_log_regs(ithc);
  2121. + CHECK_RET(ithc_quickspi_init_regs, ithc, cfg);
  2122. + ithc_log_regs(ithc);
  2123. + CHECK_RET(ithc_quickspi_init_hidspi, ithc, cfg);
  2124. + ithc_log_regs(ithc);
  2125. +
  2126. + // This value is set to 2 in ithc_quickspi_init_regs(). It needs to be set to 1 here,
  2127. + // otherwise DMA will not work. Maybe selects between DMA and PIO mode?
  2128. + bitsl(&ithc->regs->quickspi_config1,
  2129. + QUICKSPI_CONFIG1_UNKNOWN_16(0xffff), QUICKSPI_CONFIG1_UNKNOWN_16(1));
  2130. +
  2131. + // TODO Do we need to set any of the following bits here?
  2132. + //bitsb_set(&ithc->regs->dma_rx[1].control2, DMA_RX_CONTROL2_UNKNOWN_4);
  2133. + //bitsb_set(&ithc->regs->dma_rx[0].control2, DMA_RX_CONTROL2_UNKNOWN_5);
  2134. + //bitsb_set(&ithc->regs->dma_rx[1].control2, DMA_RX_CONTROL2_UNKNOWN_5);
  2135. + //bitsl_set(&ithc->regs->dma_rx[0].init_unknown, INIT_UNKNOWN_3);
  2136. + //bitsl_set(&ithc->regs->dma_rx[0].init_unknown, INIT_UNKNOWN_31);
  2137. +
  2138. + ithc_log_regs(ithc);
  2139. +
  2140. + return 0;
  2141. +}
  2142. +
  2143. +void ithc_quickspi_exit(struct ithc *ithc)
  2144. +{
  2145. + // TODO Should we send HIDSPI 'power off' command?
  2146. + //struct hidspi_header h = { .type = HIDSPI_OUTPUT_TYPE_COMMAND, .id = 3, };
  2147. + //struct ithc_data d = { .type = ITHC_DATA_RAW, .data = &h, .size = sizeof(h) };
  2148. + //CHECK(ithc_dma_tx, ithc, &d); // or ithc_spi_command()
  2149. +}
  2150. +
  2151. +int ithc_quickspi_decode_rx(struct ithc *ithc, const void *src, size_t len, struct ithc_data *dest)
  2152. +{
  2153. + const struct hidspi_header *hdr = src;
  2154. +
  2155. + if (len < sizeof(*hdr))
  2156. + return -ENODATA;
  2157. + // TODO Do we need to handle HIDSPI packet fragmentation?
  2158. + if (len < sizeof(*hdr) + hdr->len)
  2159. + return -EMSGSIZE;
  2160. + if (len > round_up(sizeof(*hdr) + hdr->len, 4))
  2161. + return -EMSGSIZE;
  2162. +
  2163. + switch (hdr->type) {
  2164. + case HIDSPI_INPUT_TYPE_RESET_RESPONSE:
  2165. + // TODO "When the device detects an error condition, it may interrupt and make
  2166. + // available to the host an Input Report containing an unsolicited Reset Response.
  2167. + // After receiving an unsolicited Reset Response, the host shall initiate the
  2168. + // request procedure from step (4) in the [HIDSPI initialization] process."
  2169. + dest->type = ITHC_DATA_ERROR;
  2170. + return 0;
  2171. + case HIDSPI_INPUT_TYPE_REPORT_DESCRIPTOR:
  2172. + dest->type = ITHC_DATA_REPORT_DESCRIPTOR;
  2173. + dest->data = hdr + 1;
  2174. + dest->size = hdr->len;
  2175. + return 0;
  2176. + case HIDSPI_INPUT_TYPE_DATA:
  2177. + case HIDSPI_INPUT_TYPE_GET_INPUT_REPORT_RESPONSE:
  2178. + dest->type = ITHC_DATA_INPUT_REPORT;
  2179. + dest->data = &hdr->id;
  2180. + dest->size = hdr->len + 1;
  2181. + return 0;
  2182. + case HIDSPI_INPUT_TYPE_GET_FEATURE_RESPONSE:
  2183. + dest->type = ITHC_DATA_GET_FEATURE;
  2184. + dest->data = &hdr->id;
  2185. + dest->size = hdr->len + 1;
  2186. + return 0;
  2187. + case HIDSPI_INPUT_TYPE_SET_FEATURE_RESPONSE:
  2188. + case HIDSPI_INPUT_TYPE_OUTPUT_REPORT_RESPONSE:
  2189. + dest->type = ITHC_DATA_IGNORE;
  2190. + return 0;
  2191. + default:
  2192. + return -EINVAL;
  2193. + }
  2194. +}
  2195. +
  2196. +ssize_t ithc_quickspi_encode_tx(struct ithc *ithc, const struct ithc_data *src, void *dest,
  2197. + size_t maxlen)
  2198. +{
  2199. + struct hidspi_header *hdr = dest;
  2200. +
  2201. + size_t src_size = src->size;
  2202. + const u8 *src_data = src->data;
  2203. + u8 type;
  2204. +
  2205. + switch (src->type) {
  2206. + case ITHC_DATA_SET_FEATURE:
  2207. + type = HIDSPI_OUTPUT_TYPE_SET_FEATURE;
  2208. + break;
  2209. + case ITHC_DATA_GET_FEATURE:
  2210. + type = HIDSPI_OUTPUT_TYPE_GET_FEATURE;
  2211. + break;
  2212. + case ITHC_DATA_OUTPUT_REPORT:
  2213. + type = HIDSPI_OUTPUT_TYPE_OUTPUT_REPORT;
  2214. + break;
  2215. + case ITHC_DATA_REPORT_DESCRIPTOR:
  2216. + type = HIDSPI_OUTPUT_TYPE_REPORT_DESCRIPTOR_REQUEST;
  2217. + src_size = 0;
  2218. + break;
  2219. + default:
  2220. + return -EINVAL;
  2221. + }
  2222. +
  2223. + u8 id = 0;
  2224. + if (src_size) {
  2225. + id = *src_data++;
  2226. + src_size--;
  2227. + }
  2228. +
  2229. + // Data must be padded to next 4-byte boundary.
  2230. + size_t padded = round_up(src_size, 4);
  2231. + if (sizeof(*hdr) + padded > maxlen)
  2232. + return -EOVERFLOW;
  2233. +
  2234. + // Fill the TX buffer with header and data.
  2235. + hdr->type = type;
  2236. + hdr->len = (u16)src_size;
  2237. + hdr->id = id;
  2238. + memcpy_and_pad(hdr + 1, padded, src_data, src_size, 0);
  2239. +
  2240. + return sizeof(*hdr) + padded;
  2241. +}
  2242. +
  2243. diff --git a/drivers/hid/ithc/ithc-quickspi.h b/drivers/hid/ithc/ithc-quickspi.h
  2244. new file mode 100644
  2245. index 000000000000..74d882f6b2f0
  2246. --- /dev/null
  2247. +++ b/drivers/hid/ithc/ithc-quickspi.h
  2248. @@ -0,0 +1,39 @@
  2249. +/* SPDX-License-Identifier: GPL-2.0 OR BSD-2-Clause */
  2250. +
  2251. +struct ithc_acpi_config {
  2252. + bool has_config: 1;
  2253. + bool has_input_report_header_address: 1;
  2254. + bool has_input_report_body_address: 1;
  2255. + bool has_output_report_body_address: 1;
  2256. + bool has_read_opcode: 1;
  2257. + bool has_write_opcode: 1;
  2258. + bool has_read_mode: 1;
  2259. + bool has_write_mode: 1;
  2260. + bool has_spi_frequency: 1;
  2261. + bool has_limit_packet_size: 1;
  2262. + bool has_tx_delay: 1;
  2263. + bool has_active_ltr: 1;
  2264. + bool has_idle_ltr: 1;
  2265. + u32 input_report_header_address;
  2266. + u32 input_report_body_address;
  2267. + u32 output_report_body_address;
  2268. + u8 read_opcode;
  2269. + u8 write_opcode;
  2270. + u8 read_mode;
  2271. + u8 write_mode;
  2272. + u32 spi_frequency;
  2273. + u32 limit_packet_size;
  2274. + u32 tx_delay; // us/10 // TODO use?
  2275. + u32 active_ltr; // ns/1024
  2276. + u32 idle_ltr; // ns/1024
  2277. +};
  2278. +
  2279. +int ithc_read_acpi_config(struct ithc *ithc, struct ithc_acpi_config *cfg);
  2280. +void ithc_print_acpi_config(struct ithc *ithc, const struct ithc_acpi_config *cfg);
  2281. +
  2282. +int ithc_quickspi_init(struct ithc *ithc, const struct ithc_acpi_config *cfg);
  2283. +void ithc_quickspi_exit(struct ithc *ithc);
  2284. +int ithc_quickspi_decode_rx(struct ithc *ithc, const void *src, size_t len, struct ithc_data *dest);
  2285. +ssize_t ithc_quickspi_encode_tx(struct ithc *ithc, const struct ithc_data *src, void *dest,
  2286. + size_t maxlen);
  2287. +
  2288. diff --git a/drivers/hid/ithc/ithc-regs.c b/drivers/hid/ithc/ithc-regs.c
  2289. new file mode 100644
  2290. index 000000000000..c0f13506af20
  2291. --- /dev/null
  2292. +++ b/drivers/hid/ithc/ithc-regs.c
  2293. @@ -0,0 +1,154 @@
  2294. +// SPDX-License-Identifier: GPL-2.0 OR BSD-2-Clause
  2295. +
  2296. +#include "ithc.h"
  2297. +
  2298. +#define reg_num(r) (0x1fff & (u16)(__force u64)(r))
  2299. +
  2300. +void bitsl(__iomem u32 *reg, u32 mask, u32 val)
  2301. +{
  2302. + if (val & ~mask)
  2303. + pr_err("register 0x%x: invalid value 0x%x for bitmask 0x%x\n",
  2304. + reg_num(reg), val, mask);
  2305. + writel((readl(reg) & ~mask) | (val & mask), reg);
  2306. +}
  2307. +
  2308. +void bitsb(__iomem u8 *reg, u8 mask, u8 val)
  2309. +{
  2310. + if (val & ~mask)
  2311. + pr_err("register 0x%x: invalid value 0x%x for bitmask 0x%x\n",
  2312. + reg_num(reg), val, mask);
  2313. + writeb((readb(reg) & ~mask) | (val & mask), reg);
  2314. +}
  2315. +
  2316. +int waitl(struct ithc *ithc, __iomem u32 *reg, u32 mask, u32 val)
  2317. +{
  2318. + ithc_log_regs(ithc);
  2319. + pci_dbg(ithc->pci, "waiting for reg 0x%04x mask 0x%08x val 0x%08x\n",
  2320. + reg_num(reg), mask, val);
  2321. + u32 x;
  2322. + if (readl_poll_timeout(reg, x, (x & mask) == val, 200, 1000*1000)) {
  2323. + ithc_log_regs(ithc);
  2324. + pci_err(ithc->pci, "timed out waiting for reg 0x%04x mask 0x%08x val 0x%08x\n",
  2325. + reg_num(reg), mask, val);
  2326. + return -ETIMEDOUT;
  2327. + }
  2328. + ithc_log_regs(ithc);
  2329. + pci_dbg(ithc->pci, "done waiting\n");
  2330. + return 0;
  2331. +}
  2332. +
  2333. +int waitb(struct ithc *ithc, __iomem u8 *reg, u8 mask, u8 val)
  2334. +{
  2335. + ithc_log_regs(ithc);
  2336. + pci_dbg(ithc->pci, "waiting for reg 0x%04x mask 0x%02x val 0x%02x\n",
  2337. + reg_num(reg), mask, val);
  2338. + u8 x;
  2339. + if (readb_poll_timeout(reg, x, (x & mask) == val, 200, 1000*1000)) {
  2340. + ithc_log_regs(ithc);
  2341. + pci_err(ithc->pci, "timed out waiting for reg 0x%04x mask 0x%02x val 0x%02x\n",
  2342. + reg_num(reg), mask, val);
  2343. + return -ETIMEDOUT;
  2344. + }
  2345. + ithc_log_regs(ithc);
  2346. + pci_dbg(ithc->pci, "done waiting\n");
  2347. + return 0;
  2348. +}
  2349. +
  2350. +static void calc_ltr(u64 *ns, unsigned int *val, unsigned int *scale)
  2351. +{
  2352. + unsigned int s = 0;
  2353. + u64 v = *ns;
  2354. + while (v > 0x3ff) {
  2355. + s++;
  2356. + v >>= 5;
  2357. + }
  2358. + if (s > 5) {
  2359. + s = 5;
  2360. + v = 0x3ff;
  2361. + }
  2362. + *val = v;
  2363. + *scale = s;
  2364. + *ns = v << (5 * s);
  2365. +}
  2366. +
  2367. +void ithc_set_ltr_config(struct ithc *ithc, u64 active_ltr_ns, u64 idle_ltr_ns)
  2368. +{
  2369. + unsigned int active_val, active_scale, idle_val, idle_scale;
  2370. + calc_ltr(&active_ltr_ns, &active_val, &active_scale);
  2371. + calc_ltr(&idle_ltr_ns, &idle_val, &idle_scale);
  2372. + pci_dbg(ithc->pci, "setting active LTR value to %llu ns, idle LTR value to %llu ns\n",
  2373. + active_ltr_ns, idle_ltr_ns);
  2374. + writel(LTR_CONFIG_ENABLE_ACTIVE | LTR_CONFIG_ENABLE_IDLE | LTR_CONFIG_APPLY |
  2375. + LTR_CONFIG_ACTIVE_LTR_SCALE(active_scale) | LTR_CONFIG_ACTIVE_LTR_VALUE(active_val) |
  2376. + LTR_CONFIG_IDLE_LTR_SCALE(idle_scale) | LTR_CONFIG_IDLE_LTR_VALUE(idle_val),
  2377. + &ithc->regs->ltr_config);
  2378. +}
  2379. +
  2380. +void ithc_set_ltr_idle(struct ithc *ithc)
  2381. +{
  2382. + u32 ltr = readl(&ithc->regs->ltr_config);
  2383. + switch (ltr & (LTR_CONFIG_STATUS_ACTIVE | LTR_CONFIG_STATUS_IDLE)) {
  2384. + case LTR_CONFIG_STATUS_IDLE:
  2385. + break;
  2386. + case LTR_CONFIG_STATUS_ACTIVE:
  2387. + writel(ltr | LTR_CONFIG_TOGGLE | LTR_CONFIG_APPLY, &ithc->regs->ltr_config);
  2388. + break;
  2389. + default:
  2390. + pci_err(ithc->pci, "invalid LTR state 0x%08x\n", ltr);
  2391. + break;
  2392. + }
  2393. +}
  2394. +
  2395. +int ithc_set_spi_config(struct ithc *ithc, u8 clkdiv, bool clkdiv8, u8 read_mode, u8 write_mode)
  2396. +{
  2397. + if (clkdiv == 0 || clkdiv > 7 || read_mode > SPI_MODE_QUAD || write_mode > SPI_MODE_QUAD)
  2398. + return -EINVAL;
  2399. + static const char * const modes[] = { "single", "dual", "quad" };
  2400. + pci_dbg(ithc->pci, "setting SPI frequency to %i Hz, %s read, %s write\n",
  2401. + SPI_CLK_FREQ_BASE / (clkdiv * (clkdiv8 ? 8 : 1)),
  2402. + modes[read_mode], modes[write_mode]);
  2403. + bitsl(&ithc->regs->spi_config,
  2404. + SPI_CONFIG_READ_MODE(0xff) | SPI_CONFIG_READ_CLKDIV(0xff) |
  2405. + SPI_CONFIG_WRITE_MODE(0xff) | SPI_CONFIG_WRITE_CLKDIV(0xff) |
  2406. + SPI_CONFIG_CLKDIV_8,
  2407. + SPI_CONFIG_READ_MODE(read_mode) | SPI_CONFIG_READ_CLKDIV(clkdiv) |
  2408. + SPI_CONFIG_WRITE_MODE(write_mode) | SPI_CONFIG_WRITE_CLKDIV(clkdiv) |
  2409. + (clkdiv8 ? SPI_CONFIG_CLKDIV_8 : 0));
  2410. + return 0;
  2411. +}
  2412. +
  2413. +int ithc_spi_command(struct ithc *ithc, u8 command, u32 offset, u32 size, void *data)
  2414. +{
  2415. + pci_dbg(ithc->pci, "SPI command %u, size %u, offset 0x%x\n", command, size, offset);
  2416. + if (size > sizeof(ithc->regs->spi_cmd.data))
  2417. + return -EINVAL;
  2418. +
  2419. + // Wait if the device is still busy.
  2420. + CHECK_RET(waitl, ithc, &ithc->regs->spi_cmd.status, SPI_CMD_STATUS_BUSY, 0);
  2421. + // Clear result flags.
  2422. + writel(SPI_CMD_STATUS_DONE | SPI_CMD_STATUS_ERROR, &ithc->regs->spi_cmd.status);
  2423. +
  2424. + // Init SPI command data.
  2425. + writeb(command, &ithc->regs->spi_cmd.code);
  2426. + writew(size, &ithc->regs->spi_cmd.size);
  2427. + writel(offset, &ithc->regs->spi_cmd.offset);
  2428. + u32 *p = data, n = (size + 3) / 4;
  2429. + for (u32 i = 0; i < n; i++)
  2430. + writel(p[i], &ithc->regs->spi_cmd.data[i]);
  2431. +
  2432. + // Start transmission.
  2433. + bitsb_set(&ithc->regs->spi_cmd.control, SPI_CMD_CONTROL_SEND);
  2434. + CHECK_RET(waitl, ithc, &ithc->regs->spi_cmd.status, SPI_CMD_STATUS_BUSY, 0);
  2435. +
  2436. + // Read response.
  2437. + if ((readl(&ithc->regs->spi_cmd.status) & (SPI_CMD_STATUS_DONE | SPI_CMD_STATUS_ERROR)) != SPI_CMD_STATUS_DONE)
  2438. + return -EIO;
  2439. + if (readw(&ithc->regs->spi_cmd.size) != size)
  2440. + return -EMSGSIZE;
  2441. + for (u32 i = 0; i < n; i++)
  2442. + p[i] = readl(&ithc->regs->spi_cmd.data[i]);
  2443. +
  2444. + writel(SPI_CMD_STATUS_DONE | SPI_CMD_STATUS_ERROR, &ithc->regs->spi_cmd.status);
  2445. + return 0;
  2446. +}
  2447. +
  2448. diff --git a/drivers/hid/ithc/ithc-regs.h b/drivers/hid/ithc/ithc-regs.h
  2449. new file mode 100644
  2450. index 000000000000..4f541fe533fa
  2451. --- /dev/null
  2452. +++ b/drivers/hid/ithc/ithc-regs.h
  2453. @@ -0,0 +1,211 @@
  2454. +/* SPDX-License-Identifier: GPL-2.0 OR BSD-2-Clause */
  2455. +
  2456. +#define LTR_CONFIG_ENABLE_ACTIVE BIT(0)
  2457. +#define LTR_CONFIG_TOGGLE BIT(1)
  2458. +#define LTR_CONFIG_ENABLE_IDLE BIT(2)
  2459. +#define LTR_CONFIG_APPLY BIT(3)
  2460. +#define LTR_CONFIG_IDLE_LTR_SCALE(x) (((x) & 7) << 4)
  2461. +#define LTR_CONFIG_IDLE_LTR_VALUE(x) (((x) & 0x3ff) << 7)
  2462. +#define LTR_CONFIG_ACTIVE_LTR_SCALE(x) (((x) & 7) << 17)
  2463. +#define LTR_CONFIG_ACTIVE_LTR_VALUE(x) (((x) & 0x3ff) << 20)
  2464. +#define LTR_CONFIG_STATUS_ACTIVE BIT(30)
  2465. +#define LTR_CONFIG_STATUS_IDLE BIT(31)
  2466. +
  2467. +#define CONTROL_QUIESCE BIT(1)
  2468. +#define CONTROL_IS_QUIESCED BIT(2)
  2469. +#define CONTROL_NRESET BIT(3)
  2470. +#define CONTROL_UNKNOWN_24(x) (((x) & 3) << 24)
  2471. +#define CONTROL_READY BIT(29)
  2472. +
  2473. +#define SPI_CONFIG_READ_MODE(x) (((x) & 3) << 2)
  2474. +#define SPI_CONFIG_READ_CLKDIV(x) (((x) & 7) << 4)
  2475. +#define SPI_CONFIG_READ_PACKET_SIZE(x) (((x) & 0x1ff) << 7)
  2476. +#define SPI_CONFIG_WRITE_MODE(x) (((x) & 3) << 18)
  2477. +#define SPI_CONFIG_WRITE_CLKDIV(x) (((x) & 7) << 20)
  2478. +#define SPI_CONFIG_CLKDIV_8 BIT(23) // additionally divide clk by 8, for both read and write
  2479. +#define SPI_CONFIG_WRITE_PACKET_SIZE(x) (((x) & 0xff) << 24)
  2480. +
  2481. +#define SPI_CLK_FREQ_BASE 125000000
  2482. +#define SPI_MODE_SINGLE 0
  2483. +#define SPI_MODE_DUAL 1
  2484. +#define SPI_MODE_QUAD 2
  2485. +
  2486. +#define ERROR_CONTROL_UNKNOWN_0 BIT(0)
  2487. +#define ERROR_CONTROL_DISABLE_DMA BIT(1) // clears DMA_RX_CONTROL_ENABLE when a DMA error occurs
  2488. +#define ERROR_CONTROL_UNKNOWN_2 BIT(2)
  2489. +#define ERROR_CONTROL_UNKNOWN_3 BIT(3)
  2490. +#define ERROR_CONTROL_IRQ_DMA_UNKNOWN_9 BIT(9)
  2491. +#define ERROR_CONTROL_IRQ_DMA_UNKNOWN_10 BIT(10)
  2492. +#define ERROR_CONTROL_IRQ_DMA_UNKNOWN_12 BIT(12)
  2493. +#define ERROR_CONTROL_IRQ_DMA_UNKNOWN_13 BIT(13)
  2494. +#define ERROR_CONTROL_UNKNOWN_16(x) (((x) & 0xff) << 16) // spi error code irq?
  2495. +#define ERROR_CONTROL_SET_DMA_STATUS BIT(29) // sets DMA_RX_STATUS_ERROR when a DMA error occurs
  2496. +
  2497. +#define ERROR_STATUS_DMA BIT(28)
  2498. +#define ERROR_STATUS_SPI BIT(30)
  2499. +
  2500. +#define ERROR_FLAG_DMA_UNKNOWN_9 BIT(9)
  2501. +#define ERROR_FLAG_DMA_UNKNOWN_10 BIT(10)
  2502. +#define ERROR_FLAG_DMA_RX_TIMEOUT BIT(12) // set when we receive a truncated DMA message
  2503. +#define ERROR_FLAG_DMA_UNKNOWN_13 BIT(13)
  2504. +#define ERROR_FLAG_SPI_BUS_TURNAROUND BIT(16)
  2505. +#define ERROR_FLAG_SPI_RESPONSE_TIMEOUT BIT(17)
  2506. +#define ERROR_FLAG_SPI_INTRA_PACKET_TIMEOUT BIT(18)
  2507. +#define ERROR_FLAG_SPI_INVALID_RESPONSE BIT(19)
  2508. +#define ERROR_FLAG_SPI_HS_RX_TIMEOUT BIT(20)
  2509. +#define ERROR_FLAG_SPI_TOUCH_IC_INIT BIT(21)
  2510. +
  2511. +#define SPI_CMD_CONTROL_SEND BIT(0) // cleared by device when sending is complete
  2512. +#define SPI_CMD_CONTROL_IRQ BIT(1)
  2513. +
  2514. +#define SPI_CMD_CODE_READ 4
  2515. +#define SPI_CMD_CODE_WRITE 6
  2516. +
  2517. +#define SPI_CMD_STATUS_DONE BIT(0)
  2518. +#define SPI_CMD_STATUS_ERROR BIT(1)
  2519. +#define SPI_CMD_STATUS_BUSY BIT(3)
  2520. +
  2521. +#define DMA_TX_CONTROL_SEND BIT(0) // cleared by device when sending is complete
  2522. +#define DMA_TX_CONTROL_IRQ BIT(3)
  2523. +
  2524. +#define DMA_TX_STATUS_DONE BIT(0)
  2525. +#define DMA_TX_STATUS_ERROR BIT(1)
  2526. +#define DMA_TX_STATUS_UNKNOWN_2 BIT(2)
  2527. +#define DMA_TX_STATUS_UNKNOWN_3 BIT(3) // busy?
  2528. +
  2529. +#define INPUT_HEADER_VERSION(x) ((x) & 0xf)
  2530. +#define INPUT_HEADER_REPORT_LENGTH(x) (((x) >> 8) & 0x3fff)
  2531. +#define INPUT_HEADER_SYNC(x) ((x) >> 24)
  2532. +#define INPUT_HEADER_VERSION_VALUE 3
  2533. +#define INPUT_HEADER_SYNC_VALUE 0x5a
  2534. +
  2535. +#define QUICKSPI_CONFIG1_UNKNOWN_0(x) (((x) & 0x1f) << 0)
  2536. +#define QUICKSPI_CONFIG1_UNKNOWN_5(x) (((x) & 0x1f) << 5)
  2537. +#define QUICKSPI_CONFIG1_UNKNOWN_10(x) (((x) & 0x1f) << 10)
  2538. +#define QUICKSPI_CONFIG1_UNKNOWN_16(x) (((x) & 0xffff) << 16)
  2539. +
  2540. +#define QUICKSPI_CONFIG2_UNKNOWN_0(x) (((x) & 0x1f) << 0)
  2541. +#define QUICKSPI_CONFIG2_UNKNOWN_5(x) (((x) & 0x1f) << 5)
  2542. +#define QUICKSPI_CONFIG2_UNKNOWN_12(x) (((x) & 0xf) << 12)
  2543. +#define QUICKSPI_CONFIG2_UNKNOWN_16 BIT(16)
  2544. +#define QUICKSPI_CONFIG2_UNKNOWN_17 BIT(17)
  2545. +#define QUICKSPI_CONFIG2_DISABLE_READ_ADDRESS_INCREMENT BIT(24)
  2546. +#define QUICKSPI_CONFIG2_DISABLE_WRITE_ADDRESS_INCREMENT BIT(25)
  2547. +#define QUICKSPI_CONFIG2_ENABLE_WRITE_STREAMING_MODE BIT(27)
  2548. +#define QUICKSPI_CONFIG2_IRQ_POLARITY BIT(28)
  2549. +
  2550. +#define DMA_RX_CONTROL_ENABLE BIT(0)
  2551. +#define DMA_RX_CONTROL_IRQ_UNKNOWN_1 BIT(1) // rx1 only?
  2552. +#define DMA_RX_CONTROL_IRQ_ERROR BIT(3) // rx1 only?
  2553. +#define DMA_RX_CONTROL_IRQ_READY BIT(4) // rx0 only
  2554. +#define DMA_RX_CONTROL_IRQ_DATA BIT(5)
  2555. +
  2556. +#define DMA_RX_CONTROL2_UNKNOWN_4 BIT(4) // rx1 only?
  2557. +#define DMA_RX_CONTROL2_UNKNOWN_5 BIT(5) // rx0 only?
  2558. +#define DMA_RX_CONTROL2_RESET BIT(7) // resets ringbuffer indices
  2559. +
  2560. +#define DMA_RX_WRAP_FLAG BIT(7)
  2561. +
  2562. +#define DMA_RX_STATUS_ERROR BIT(3)
  2563. +#define DMA_RX_STATUS_READY BIT(4) // set in rx0 after using CONTROL_NRESET when it becomes possible to read config (can take >100ms)
  2564. +#define DMA_RX_STATUS_HAVE_DATA BIT(5)
  2565. +#define DMA_RX_STATUS_ENABLED BIT(8)
  2566. +
  2567. +#define INIT_UNKNOWN_GUC_2 BIT(2)
  2568. +#define INIT_UNKNOWN_3 BIT(3)
  2569. +#define INIT_UNKNOWN_GUC_4 BIT(4)
  2570. +#define INIT_UNKNOWN_5 BIT(5)
  2571. +#define INIT_UNKNOWN_31 BIT(31)
  2572. +
  2573. +// COUNTER_RESET can be written to counter registers to reset them to zero. However, in some cases this can mess up the THC.
  2574. +#define COUNTER_RESET BIT(31)
  2575. +
  2576. +struct ithc_registers {
  2577. + /* 0000 */ u32 _unknown_0000[5];
  2578. + /* 0014 */ u32 ltr_config;
  2579. + /* 0018 */ u32 _unknown_0018[1018];
  2580. + /* 1000 */ u32 _unknown_1000;
  2581. + /* 1004 */ u32 _unknown_1004;
  2582. + /* 1008 */ u32 control_bits;
  2583. + /* 100c */ u32 _unknown_100c;
  2584. + /* 1010 */ u32 spi_config;
  2585. + struct {
  2586. + /* 1014/1018/101c */ u8 header;
  2587. + /* 1015/1019/101d */ u8 quad;
  2588. + /* 1016/101a/101e */ u8 dual;
  2589. + /* 1017/101b/101f */ u8 single;
  2590. + } opcode[3];
  2591. + /* 1020 */ u32 error_control;
  2592. + /* 1024 */ u32 error_status; // write to clear
  2593. + /* 1028 */ u32 error_flags; // write to clear
  2594. + /* 102c */ u32 _unknown_102c[5];
  2595. + struct {
  2596. + /* 1040 */ u8 control;
  2597. + /* 1041 */ u8 code;
  2598. + /* 1042 */ u16 size;
  2599. + /* 1044 */ u32 status; // write to clear
  2600. + /* 1048 */ u32 offset;
  2601. + /* 104c */ u32 data[16];
  2602. + /* 108c */ u32 _unknown_108c;
  2603. + } spi_cmd;
  2604. + struct {
  2605. + /* 1090 */ u64 addr; // cannot be written with writeq(), must use lo_hi_writeq()
  2606. + /* 1098 */ u8 control;
  2607. + /* 1099 */ u8 _unknown_1099;
  2608. + /* 109a */ u8 _unknown_109a;
  2609. + /* 109b */ u8 num_prds;
  2610. + /* 109c */ u32 status; // write to clear
  2611. + /* 10a0 */ u32 _unknown_10a0[5];
  2612. + /* 10b4 */ u32 spi_addr;
  2613. + } dma_tx;
  2614. + /* 10b8 */ u32 spi_header_addr;
  2615. + union {
  2616. + /* 10bc */ u32 irq_cause; // in legacy THC mode
  2617. + /* 10bc */ u32 input_header; // in QuickSPI mode (see HIDSPI spec)
  2618. + };
  2619. + /* 10c0 */ u32 _unknown_10c0[8];
  2620. + /* 10e0 */ u32 _unknown_10e0_counters[3];
  2621. + /* 10ec */ u32 quickspi_config1;
  2622. + /* 10f0 */ u32 quickspi_config2;
  2623. + /* 10f4 */ u32 _unknown_10f4[3];
  2624. + struct {
  2625. + /* 1100/1200 */ u64 addr; // cannot be written with writeq(), must use lo_hi_writeq()
  2626. + /* 1108/1208 */ u8 num_bufs;
  2627. + /* 1109/1209 */ u8 num_prds;
  2628. + /* 110a/120a */ u16 _unknown_110a;
  2629. + /* 110c/120c */ u8 control;
  2630. + /* 110d/120d */ u8 head;
  2631. + /* 110e/120e */ u8 tail;
  2632. + /* 110f/120f */ u8 control2;
  2633. + /* 1110/1210 */ u32 status; // write to clear
  2634. + /* 1114/1214 */ u32 _unknown_1114;
  2635. + /* 1118/1218 */ u64 _unknown_1118_guc_addr;
  2636. + /* 1120/1220 */ u32 _unknown_1120_guc;
  2637. + /* 1124/1224 */ u32 _unknown_1124_guc;
  2638. + /* 1128/1228 */ u32 init_unknown;
  2639. + /* 112c/122c */ u32 _unknown_112c;
  2640. + /* 1130/1230 */ u64 _unknown_1130_guc_addr;
  2641. + /* 1138/1238 */ u32 _unknown_1138_guc;
  2642. + /* 113c/123c */ u32 _unknown_113c;
  2643. + /* 1140/1240 */ u32 _unknown_1140_guc;
  2644. + /* 1144/1244 */ u32 _unknown_1144[11];
  2645. + /* 1170/1270 */ u32 spi_addr;
  2646. + /* 1174/1274 */ u32 _unknown_1174[11];
  2647. + /* 11a0/12a0 */ u32 _unknown_11a0_counters[6];
  2648. + /* 11b8/12b8 */ u32 _unknown_11b8[18];
  2649. + } dma_rx[2];
  2650. +};
  2651. +static_assert(sizeof(struct ithc_registers) == 0x1300);
  2652. +
  2653. +void bitsl(__iomem u32 *reg, u32 mask, u32 val);
  2654. +void bitsb(__iomem u8 *reg, u8 mask, u8 val);
  2655. +#define bitsl_set(reg, x) bitsl(reg, x, x)
  2656. +#define bitsb_set(reg, x) bitsb(reg, x, x)
  2657. +int waitl(struct ithc *ithc, __iomem u32 *reg, u32 mask, u32 val);
  2658. +int waitb(struct ithc *ithc, __iomem u8 *reg, u8 mask, u8 val);
  2659. +
  2660. +void ithc_set_ltr_config(struct ithc *ithc, u64 active_ltr_ns, u64 idle_ltr_ns);
  2661. +void ithc_set_ltr_idle(struct ithc *ithc);
  2662. +int ithc_set_spi_config(struct ithc *ithc, u8 clkdiv, bool clkdiv8, u8 read_mode, u8 write_mode);
  2663. +int ithc_spi_command(struct ithc *ithc, u8 command, u32 offset, u32 size, void *data);
  2664. +
  2665. diff --git a/drivers/hid/ithc/ithc.h b/drivers/hid/ithc/ithc.h
  2666. new file mode 100644
  2667. index 000000000000..aec320d4e945
  2668. --- /dev/null
  2669. +++ b/drivers/hid/ithc/ithc.h
  2670. @@ -0,0 +1,89 @@
  2671. +/* SPDX-License-Identifier: GPL-2.0 OR BSD-2-Clause */
  2672. +
  2673. +#include <linux/acpi.h>
  2674. +#include <linux/debugfs.h>
  2675. +#include <linux/delay.h>
  2676. +#include <linux/dma-mapping.h>
  2677. +#include <linux/hid.h>
  2678. +#include <linux/highmem.h>
  2679. +#include <linux/input.h>
  2680. +#include <linux/io-64-nonatomic-lo-hi.h>
  2681. +#include <linux/iopoll.h>
  2682. +#include <linux/kthread.h>
  2683. +#include <linux/miscdevice.h>
  2684. +#include <linux/module.h>
  2685. +#include <linux/pci.h>
  2686. +#include <linux/poll.h>
  2687. +#include <linux/timer.h>
  2688. +#include <linux/vmalloc.h>
  2689. +
  2690. +#define DEVNAME "ithc"
  2691. +#define DEVFULLNAME "Intel Touch Host Controller"
  2692. +
  2693. +#undef pr_fmt
  2694. +#define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
  2695. +
  2696. +#define CHECK(fn, ...) ({ int r = fn(__VA_ARGS__); if (r < 0) pci_err(ithc->pci, "%s: %s failed with %i\n", __func__, #fn, r); r; })
  2697. +#define CHECK_RET(...) do { int r = CHECK(__VA_ARGS__); if (r < 0) return r; } while (0)
  2698. +
  2699. +#define NUM_RX_BUF 16
  2700. +
  2701. +// PCI device IDs:
  2702. +// Lakefield
  2703. +#define PCI_DEVICE_ID_INTEL_THC_LKF_PORT1 0x98d0
  2704. +#define PCI_DEVICE_ID_INTEL_THC_LKF_PORT2 0x98d1
  2705. +// Tiger Lake
  2706. +#define PCI_DEVICE_ID_INTEL_THC_TGL_LP_PORT1 0xa0d0
  2707. +#define PCI_DEVICE_ID_INTEL_THC_TGL_LP_PORT2 0xa0d1
  2708. +#define PCI_DEVICE_ID_INTEL_THC_TGL_H_PORT1 0x43d0
  2709. +#define PCI_DEVICE_ID_INTEL_THC_TGL_H_PORT2 0x43d1
  2710. +// Alder Lake
  2711. +#define PCI_DEVICE_ID_INTEL_THC_ADL_S_PORT1 0x7ad8
  2712. +#define PCI_DEVICE_ID_INTEL_THC_ADL_S_PORT2 0x7ad9
  2713. +#define PCI_DEVICE_ID_INTEL_THC_ADL_P_PORT1 0x51d0
  2714. +#define PCI_DEVICE_ID_INTEL_THC_ADL_P_PORT2 0x51d1
  2715. +#define PCI_DEVICE_ID_INTEL_THC_ADL_M_PORT1 0x54d0
  2716. +#define PCI_DEVICE_ID_INTEL_THC_ADL_M_PORT2 0x54d1
  2717. +// Raptor Lake
  2718. +#define PCI_DEVICE_ID_INTEL_THC_RPL_S_PORT1 0x7a58
  2719. +#define PCI_DEVICE_ID_INTEL_THC_RPL_S_PORT2 0x7a59
  2720. +// Meteor Lake
  2721. +#define PCI_DEVICE_ID_INTEL_THC_MTL_S_PORT1 0x7f59
  2722. +#define PCI_DEVICE_ID_INTEL_THC_MTL_S_PORT2 0x7f5b
  2723. +#define PCI_DEVICE_ID_INTEL_THC_MTL_MP_PORT1 0x7e49
  2724. +#define PCI_DEVICE_ID_INTEL_THC_MTL_MP_PORT2 0x7e4b
  2725. +
  2726. +struct ithc;
  2727. +
  2728. +#include "ithc-regs.h"
  2729. +#include "ithc-hid.h"
  2730. +#include "ithc-dma.h"
  2731. +#include "ithc-legacy.h"
  2732. +#include "ithc-quickspi.h"
  2733. +#include "ithc-debug.h"
  2734. +
  2735. +struct ithc {
  2736. + char phys[32];
  2737. + struct pci_dev *pci;
  2738. + int irq;
  2739. + struct task_struct *poll_thread;
  2740. + struct timer_list idle_timer;
  2741. +
  2742. + struct ithc_registers __iomem *regs;
  2743. + struct ithc_registers *prev_regs; // for debugging
  2744. + struct ithc_dma_rx dma_rx[2];
  2745. + struct ithc_dma_tx dma_tx;
  2746. + struct ithc_hid hid;
  2747. +
  2748. + bool use_quickspi;
  2749. + bool have_config;
  2750. + u16 vendor_id;
  2751. + u16 product_id;
  2752. + u32 product_rev;
  2753. + u32 max_rx_size;
  2754. + u32 max_tx_size;
  2755. + u32 legacy_touch_cfg;
  2756. +};
  2757. +
  2758. +int ithc_reset(struct ithc *ithc);
  2759. +
  2760. --
  2761. 2.49.0